sh_cmt.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /*
  2. * SuperH Timer Support - CMT
  3. *
  4. * Copyright (C) 2008 Magnus Damm
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/init.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/ioport.h>
  24. #include <linux/io.h>
  25. #include <linux/clk.h>
  26. #include <linux/irq.h>
  27. #include <linux/err.h>
  28. #include <linux/clocksource.h>
  29. #include <linux/clockchips.h>
  30. #include <linux/sh_timer.h>
  31. struct sh_cmt_priv {
  32. void __iomem *mapbase;
  33. struct clk *clk;
  34. unsigned long width; /* 16 or 32 bit version of hardware block */
  35. unsigned long overflow_bit;
  36. unsigned long clear_bits;
  37. struct irqaction irqaction;
  38. struct platform_device *pdev;
  39. unsigned long flags;
  40. unsigned long flags_suspend;
  41. unsigned long match_value;
  42. unsigned long next_match_value;
  43. unsigned long max_match_value;
  44. unsigned long rate;
  45. spinlock_t lock;
  46. struct clock_event_device ced;
  47. struct clocksource cs;
  48. unsigned long total_cycles;
  49. };
  50. static DEFINE_SPINLOCK(sh_cmt_lock);
  51. #define CMSTR -1 /* shared register */
  52. #define CMCSR 0 /* channel register */
  53. #define CMCNT 1 /* channel register */
  54. #define CMCOR 2 /* channel register */
  55. static inline unsigned long sh_cmt_read(struct sh_cmt_priv *p, int reg_nr)
  56. {
  57. struct sh_timer_config *cfg = p->pdev->dev.platform_data;
  58. void __iomem *base = p->mapbase;
  59. unsigned long offs;
  60. if (reg_nr == CMSTR) {
  61. offs = 0;
  62. base -= cfg->channel_offset;
  63. } else
  64. offs = reg_nr;
  65. if (p->width == 16)
  66. offs <<= 1;
  67. else {
  68. offs <<= 2;
  69. if ((reg_nr == CMCNT) || (reg_nr == CMCOR))
  70. return ioread32(base + offs);
  71. }
  72. return ioread16(base + offs);
  73. }
  74. static inline void sh_cmt_write(struct sh_cmt_priv *p, int reg_nr,
  75. unsigned long value)
  76. {
  77. struct sh_timer_config *cfg = p->pdev->dev.platform_data;
  78. void __iomem *base = p->mapbase;
  79. unsigned long offs;
  80. if (reg_nr == CMSTR) {
  81. offs = 0;
  82. base -= cfg->channel_offset;
  83. } else
  84. offs = reg_nr;
  85. if (p->width == 16)
  86. offs <<= 1;
  87. else {
  88. offs <<= 2;
  89. if ((reg_nr == CMCNT) || (reg_nr == CMCOR)) {
  90. iowrite32(value, base + offs);
  91. return;
  92. }
  93. }
  94. iowrite16(value, base + offs);
  95. }
  96. static unsigned long sh_cmt_get_counter(struct sh_cmt_priv *p,
  97. int *has_wrapped)
  98. {
  99. unsigned long v1, v2, v3;
  100. int o1, o2;
  101. o1 = sh_cmt_read(p, CMCSR) & p->overflow_bit;
  102. /* Make sure the timer value is stable. Stolen from acpi_pm.c */
  103. do {
  104. o2 = o1;
  105. v1 = sh_cmt_read(p, CMCNT);
  106. v2 = sh_cmt_read(p, CMCNT);
  107. v3 = sh_cmt_read(p, CMCNT);
  108. o1 = sh_cmt_read(p, CMCSR) & p->overflow_bit;
  109. } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
  110. || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
  111. *has_wrapped = o1;
  112. return v2;
  113. }
  114. static void sh_cmt_start_stop_ch(struct sh_cmt_priv *p, int start)
  115. {
  116. struct sh_timer_config *cfg = p->pdev->dev.platform_data;
  117. unsigned long flags, value;
  118. /* start stop register shared by multiple timer channels */
  119. spin_lock_irqsave(&sh_cmt_lock, flags);
  120. value = sh_cmt_read(p, CMSTR);
  121. if (start)
  122. value |= 1 << cfg->timer_bit;
  123. else
  124. value &= ~(1 << cfg->timer_bit);
  125. sh_cmt_write(p, CMSTR, value);
  126. spin_unlock_irqrestore(&sh_cmt_lock, flags);
  127. }
  128. static int sh_cmt_enable(struct sh_cmt_priv *p, unsigned long *rate)
  129. {
  130. struct sh_timer_config *cfg = p->pdev->dev.platform_data;
  131. int ret;
  132. /* enable clock */
  133. ret = clk_enable(p->clk);
  134. if (ret) {
  135. pr_err("sh_cmt: cannot enable clock \"%s\"\n", cfg->clk);
  136. return ret;
  137. }
  138. /* make sure channel is disabled */
  139. sh_cmt_start_stop_ch(p, 0);
  140. /* configure channel, periodic mode and maximum timeout */
  141. if (p->width == 16) {
  142. *rate = clk_get_rate(p->clk) / 512;
  143. sh_cmt_write(p, CMCSR, 0x43);
  144. } else {
  145. *rate = clk_get_rate(p->clk) / 8;
  146. sh_cmt_write(p, CMCSR, 0x01a4);
  147. }
  148. sh_cmt_write(p, CMCOR, 0xffffffff);
  149. sh_cmt_write(p, CMCNT, 0);
  150. /* enable channel */
  151. sh_cmt_start_stop_ch(p, 1);
  152. return 0;
  153. }
  154. static void sh_cmt_disable(struct sh_cmt_priv *p)
  155. {
  156. /* disable channel */
  157. sh_cmt_start_stop_ch(p, 0);
  158. /* disable interrupts in CMT block */
  159. sh_cmt_write(p, CMCSR, 0);
  160. /* stop clock */
  161. clk_disable(p->clk);
  162. }
  163. /* private flags */
  164. #define FLAG_CLOCKEVENT (1 << 0)
  165. #define FLAG_CLOCKSOURCE (1 << 1)
  166. #define FLAG_REPROGRAM (1 << 2)
  167. #define FLAG_SKIPEVENT (1 << 3)
  168. #define FLAG_IRQCONTEXT (1 << 4)
  169. static void sh_cmt_clock_event_program_verify(struct sh_cmt_priv *p,
  170. int absolute)
  171. {
  172. unsigned long new_match;
  173. unsigned long value = p->next_match_value;
  174. unsigned long delay = 0;
  175. unsigned long now = 0;
  176. int has_wrapped;
  177. now = sh_cmt_get_counter(p, &has_wrapped);
  178. p->flags |= FLAG_REPROGRAM; /* force reprogram */
  179. if (has_wrapped) {
  180. /* we're competing with the interrupt handler.
  181. * -> let the interrupt handler reprogram the timer.
  182. * -> interrupt number two handles the event.
  183. */
  184. p->flags |= FLAG_SKIPEVENT;
  185. return;
  186. }
  187. if (absolute)
  188. now = 0;
  189. do {
  190. /* reprogram the timer hardware,
  191. * but don't save the new match value yet.
  192. */
  193. new_match = now + value + delay;
  194. if (new_match > p->max_match_value)
  195. new_match = p->max_match_value;
  196. sh_cmt_write(p, CMCOR, new_match);
  197. now = sh_cmt_get_counter(p, &has_wrapped);
  198. if (has_wrapped && (new_match > p->match_value)) {
  199. /* we are changing to a greater match value,
  200. * so this wrap must be caused by the counter
  201. * matching the old value.
  202. * -> first interrupt reprograms the timer.
  203. * -> interrupt number two handles the event.
  204. */
  205. p->flags |= FLAG_SKIPEVENT;
  206. break;
  207. }
  208. if (has_wrapped) {
  209. /* we are changing to a smaller match value,
  210. * so the wrap must be caused by the counter
  211. * matching the new value.
  212. * -> save programmed match value.
  213. * -> let isr handle the event.
  214. */
  215. p->match_value = new_match;
  216. break;
  217. }
  218. /* be safe: verify hardware settings */
  219. if (now < new_match) {
  220. /* timer value is below match value, all good.
  221. * this makes sure we won't miss any match events.
  222. * -> save programmed match value.
  223. * -> let isr handle the event.
  224. */
  225. p->match_value = new_match;
  226. break;
  227. }
  228. /* the counter has reached a value greater
  229. * than our new match value. and since the
  230. * has_wrapped flag isn't set we must have
  231. * programmed a too close event.
  232. * -> increase delay and retry.
  233. */
  234. if (delay)
  235. delay <<= 1;
  236. else
  237. delay = 1;
  238. if (!delay)
  239. pr_warning("sh_cmt: too long delay\n");
  240. } while (delay);
  241. }
  242. static void sh_cmt_set_next(struct sh_cmt_priv *p, unsigned long delta)
  243. {
  244. unsigned long flags;
  245. if (delta > p->max_match_value)
  246. pr_warning("sh_cmt: delta out of range\n");
  247. spin_lock_irqsave(&p->lock, flags);
  248. p->next_match_value = delta;
  249. sh_cmt_clock_event_program_verify(p, 0);
  250. spin_unlock_irqrestore(&p->lock, flags);
  251. }
  252. static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
  253. {
  254. struct sh_cmt_priv *p = dev_id;
  255. /* clear flags */
  256. sh_cmt_write(p, CMCSR, sh_cmt_read(p, CMCSR) & p->clear_bits);
  257. /* update clock source counter to begin with if enabled
  258. * the wrap flag should be cleared by the timer specific
  259. * isr before we end up here.
  260. */
  261. if (p->flags & FLAG_CLOCKSOURCE)
  262. p->total_cycles += p->match_value;
  263. if (!(p->flags & FLAG_REPROGRAM))
  264. p->next_match_value = p->max_match_value;
  265. p->flags |= FLAG_IRQCONTEXT;
  266. if (p->flags & FLAG_CLOCKEVENT) {
  267. if (!(p->flags & FLAG_SKIPEVENT)) {
  268. if (p->ced.mode == CLOCK_EVT_MODE_ONESHOT) {
  269. p->next_match_value = p->max_match_value;
  270. p->flags |= FLAG_REPROGRAM;
  271. }
  272. p->ced.event_handler(&p->ced);
  273. }
  274. }
  275. p->flags &= ~FLAG_SKIPEVENT;
  276. if (p->flags & FLAG_REPROGRAM) {
  277. p->flags &= ~FLAG_REPROGRAM;
  278. sh_cmt_clock_event_program_verify(p, 1);
  279. if (p->flags & FLAG_CLOCKEVENT)
  280. if ((p->ced.mode == CLOCK_EVT_MODE_SHUTDOWN)
  281. || (p->match_value == p->next_match_value))
  282. p->flags &= ~FLAG_REPROGRAM;
  283. }
  284. p->flags &= ~FLAG_IRQCONTEXT;
  285. return IRQ_HANDLED;
  286. }
  287. static int sh_cmt_start(struct sh_cmt_priv *p, unsigned long flag)
  288. {
  289. int ret = 0;
  290. unsigned long flags;
  291. spin_lock_irqsave(&p->lock, flags);
  292. if (!(p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
  293. ret = sh_cmt_enable(p, &p->rate);
  294. if (ret)
  295. goto out;
  296. p->flags |= flag;
  297. /* setup timeout if no clockevent */
  298. if ((flag == FLAG_CLOCKSOURCE) && (!(p->flags & FLAG_CLOCKEVENT)))
  299. sh_cmt_set_next(p, p->max_match_value);
  300. out:
  301. spin_unlock_irqrestore(&p->lock, flags);
  302. return ret;
  303. }
  304. static void sh_cmt_stop(struct sh_cmt_priv *p, unsigned long flag)
  305. {
  306. unsigned long flags;
  307. unsigned long f;
  308. spin_lock_irqsave(&p->lock, flags);
  309. f = p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
  310. p->flags &= ~flag;
  311. if (f && !(p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
  312. sh_cmt_disable(p);
  313. /* adjust the timeout to maximum if only clocksource left */
  314. if ((flag == FLAG_CLOCKEVENT) && (p->flags & FLAG_CLOCKSOURCE))
  315. sh_cmt_set_next(p, p->max_match_value);
  316. spin_unlock_irqrestore(&p->lock, flags);
  317. }
  318. static struct sh_cmt_priv *cs_to_sh_cmt(struct clocksource *cs)
  319. {
  320. return container_of(cs, struct sh_cmt_priv, cs);
  321. }
  322. static cycle_t sh_cmt_clocksource_read(struct clocksource *cs)
  323. {
  324. struct sh_cmt_priv *p = cs_to_sh_cmt(cs);
  325. unsigned long flags, raw;
  326. unsigned long value;
  327. int has_wrapped;
  328. spin_lock_irqsave(&p->lock, flags);
  329. value = p->total_cycles;
  330. raw = sh_cmt_get_counter(p, &has_wrapped);
  331. if (unlikely(has_wrapped))
  332. raw += p->match_value;
  333. spin_unlock_irqrestore(&p->lock, flags);
  334. return value + raw;
  335. }
  336. static int sh_cmt_clocksource_enable(struct clocksource *cs)
  337. {
  338. struct sh_cmt_priv *p = cs_to_sh_cmt(cs);
  339. int ret;
  340. p->total_cycles = 0;
  341. ret = sh_cmt_start(p, FLAG_CLOCKSOURCE);
  342. if (ret)
  343. return ret;
  344. /* TODO: calculate good shift from rate and counter bit width */
  345. cs->shift = 0;
  346. cs->mult = clocksource_hz2mult(p->rate, cs->shift);
  347. return 0;
  348. }
  349. static void sh_cmt_clocksource_disable(struct clocksource *cs)
  350. {
  351. sh_cmt_stop(cs_to_sh_cmt(cs), FLAG_CLOCKSOURCE);
  352. }
  353. static int sh_cmt_register_clocksource(struct sh_cmt_priv *p,
  354. char *name, unsigned long rating)
  355. {
  356. struct clocksource *cs = &p->cs;
  357. cs->name = name;
  358. cs->rating = rating;
  359. cs->read = sh_cmt_clocksource_read;
  360. cs->enable = sh_cmt_clocksource_enable;
  361. cs->disable = sh_cmt_clocksource_disable;
  362. cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
  363. cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
  364. pr_info("sh_cmt: %s used as clock source\n", cs->name);
  365. clocksource_register(cs);
  366. return 0;
  367. }
  368. static struct sh_cmt_priv *ced_to_sh_cmt(struct clock_event_device *ced)
  369. {
  370. return container_of(ced, struct sh_cmt_priv, ced);
  371. }
  372. static void sh_cmt_clock_event_start(struct sh_cmt_priv *p, int periodic)
  373. {
  374. struct clock_event_device *ced = &p->ced;
  375. sh_cmt_start(p, FLAG_CLOCKEVENT);
  376. /* TODO: calculate good shift from rate and counter bit width */
  377. ced->shift = 32;
  378. ced->mult = div_sc(p->rate, NSEC_PER_SEC, ced->shift);
  379. ced->max_delta_ns = clockevent_delta2ns(p->max_match_value, ced);
  380. ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);
  381. if (periodic)
  382. sh_cmt_set_next(p, (p->rate + HZ/2) / HZ);
  383. else
  384. sh_cmt_set_next(p, p->max_match_value);
  385. }
  386. static void sh_cmt_clock_event_mode(enum clock_event_mode mode,
  387. struct clock_event_device *ced)
  388. {
  389. struct sh_cmt_priv *p = ced_to_sh_cmt(ced);
  390. /* deal with old setting first */
  391. switch (ced->mode) {
  392. case CLOCK_EVT_MODE_PERIODIC:
  393. case CLOCK_EVT_MODE_ONESHOT:
  394. sh_cmt_stop(p, FLAG_CLOCKEVENT);
  395. break;
  396. default:
  397. break;
  398. }
  399. switch (mode) {
  400. case CLOCK_EVT_MODE_PERIODIC:
  401. pr_info("sh_cmt: %s used for periodic clock events\n",
  402. ced->name);
  403. sh_cmt_clock_event_start(p, 1);
  404. break;
  405. case CLOCK_EVT_MODE_ONESHOT:
  406. pr_info("sh_cmt: %s used for oneshot clock events\n",
  407. ced->name);
  408. sh_cmt_clock_event_start(p, 0);
  409. break;
  410. case CLOCK_EVT_MODE_SHUTDOWN:
  411. case CLOCK_EVT_MODE_UNUSED:
  412. sh_cmt_stop(p, FLAG_CLOCKEVENT);
  413. break;
  414. default:
  415. break;
  416. }
  417. }
  418. static int sh_cmt_clock_event_next(unsigned long delta,
  419. struct clock_event_device *ced)
  420. {
  421. struct sh_cmt_priv *p = ced_to_sh_cmt(ced);
  422. BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT);
  423. if (likely(p->flags & FLAG_IRQCONTEXT))
  424. p->next_match_value = delta;
  425. else
  426. sh_cmt_set_next(p, delta);
  427. return 0;
  428. }
  429. static void sh_cmt_register_clockevent(struct sh_cmt_priv *p,
  430. char *name, unsigned long rating)
  431. {
  432. struct clock_event_device *ced = &p->ced;
  433. memset(ced, 0, sizeof(*ced));
  434. ced->name = name;
  435. ced->features = CLOCK_EVT_FEAT_PERIODIC;
  436. ced->features |= CLOCK_EVT_FEAT_ONESHOT;
  437. ced->rating = rating;
  438. ced->cpumask = cpumask_of(0);
  439. ced->set_next_event = sh_cmt_clock_event_next;
  440. ced->set_mode = sh_cmt_clock_event_mode;
  441. pr_info("sh_cmt: %s used for clock events\n", ced->name);
  442. clockevents_register_device(ced);
  443. }
  444. static int sh_cmt_register(struct sh_cmt_priv *p, char *name,
  445. unsigned long clockevent_rating,
  446. unsigned long clocksource_rating)
  447. {
  448. if (p->width == (sizeof(p->max_match_value) * 8))
  449. p->max_match_value = ~0;
  450. else
  451. p->max_match_value = (1 << p->width) - 1;
  452. p->match_value = p->max_match_value;
  453. spin_lock_init(&p->lock);
  454. if (clockevent_rating)
  455. sh_cmt_register_clockevent(p, name, clockevent_rating);
  456. if (clocksource_rating)
  457. sh_cmt_register_clocksource(p, name, clocksource_rating);
  458. return 0;
  459. }
  460. static int sh_cmt_setup(struct sh_cmt_priv *p, struct platform_device *pdev)
  461. {
  462. struct sh_timer_config *cfg = pdev->dev.platform_data;
  463. struct resource *res;
  464. int irq, ret;
  465. ret = -ENXIO;
  466. memset(p, 0, sizeof(*p));
  467. p->pdev = pdev;
  468. if (!cfg) {
  469. dev_err(&p->pdev->dev, "missing platform data\n");
  470. goto err0;
  471. }
  472. platform_set_drvdata(pdev, p);
  473. res = platform_get_resource(p->pdev, IORESOURCE_MEM, 0);
  474. if (!res) {
  475. dev_err(&p->pdev->dev, "failed to get I/O memory\n");
  476. goto err0;
  477. }
  478. irq = platform_get_irq(p->pdev, 0);
  479. if (irq < 0) {
  480. dev_err(&p->pdev->dev, "failed to get irq\n");
  481. goto err0;
  482. }
  483. /* map memory, let mapbase point to our channel */
  484. p->mapbase = ioremap_nocache(res->start, resource_size(res));
  485. if (p->mapbase == NULL) {
  486. pr_err("sh_cmt: failed to remap I/O memory\n");
  487. goto err0;
  488. }
  489. /* request irq using setup_irq() (too early for request_irq()) */
  490. p->irqaction.name = cfg->name;
  491. p->irqaction.handler = sh_cmt_interrupt;
  492. p->irqaction.dev_id = p;
  493. p->irqaction.flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL;
  494. ret = setup_irq(irq, &p->irqaction);
  495. if (ret) {
  496. pr_err("sh_cmt: failed to request irq %d\n", irq);
  497. goto err1;
  498. }
  499. /* get hold of clock */
  500. p->clk = clk_get(&p->pdev->dev, cfg->clk);
  501. if (IS_ERR(p->clk)) {
  502. pr_err("sh_cmt: cannot get clock \"%s\"\n", cfg->clk);
  503. ret = PTR_ERR(p->clk);
  504. goto err2;
  505. }
  506. if (resource_size(res) == 6) {
  507. p->width = 16;
  508. p->overflow_bit = 0x80;
  509. p->clear_bits = ~0x80;
  510. } else {
  511. p->width = 32;
  512. p->overflow_bit = 0x8000;
  513. p->clear_bits = ~0xc000;
  514. }
  515. return sh_cmt_register(p, cfg->name,
  516. cfg->clockevent_rating,
  517. cfg->clocksource_rating);
  518. err2:
  519. remove_irq(irq, &p->irqaction);
  520. err1:
  521. iounmap(p->mapbase);
  522. err0:
  523. return ret;
  524. }
  525. static int __devinit sh_cmt_probe(struct platform_device *pdev)
  526. {
  527. struct sh_cmt_priv *p = platform_get_drvdata(pdev);
  528. struct sh_timer_config *cfg = pdev->dev.platform_data;
  529. int ret;
  530. if (p) {
  531. pr_info("sh_cmt: %s kept as earlytimer\n", cfg->name);
  532. return 0;
  533. }
  534. p = kmalloc(sizeof(*p), GFP_KERNEL);
  535. if (p == NULL) {
  536. dev_err(&pdev->dev, "failed to allocate driver data\n");
  537. return -ENOMEM;
  538. }
  539. ret = sh_cmt_setup(p, pdev);
  540. if (ret) {
  541. kfree(p);
  542. platform_set_drvdata(pdev, NULL);
  543. }
  544. return ret;
  545. }
  546. static int __devexit sh_cmt_remove(struct platform_device *pdev)
  547. {
  548. return -EBUSY; /* cannot unregister clockevent and clocksource */
  549. }
  550. static int sh_cmt_suspend(struct device *dev)
  551. {
  552. struct platform_device *pdev = to_platform_device(dev);
  553. struct sh_cmt_priv *p = platform_get_drvdata(pdev);
  554. /* save flag state and stop CMT channel */
  555. p->flags_suspend = p->flags;
  556. sh_cmt_stop(p, p->flags);
  557. return 0;
  558. }
  559. static int sh_cmt_resume(struct device *dev)
  560. {
  561. struct platform_device *pdev = to_platform_device(dev);
  562. struct sh_cmt_priv *p = platform_get_drvdata(pdev);
  563. /* start CMT channel from saved state */
  564. sh_cmt_start(p, p->flags_suspend);
  565. return 0;
  566. }
  567. static struct dev_pm_ops sh_cmt_dev_pm_ops = {
  568. .suspend = sh_cmt_suspend,
  569. .resume = sh_cmt_resume,
  570. };
  571. static struct platform_driver sh_cmt_device_driver = {
  572. .probe = sh_cmt_probe,
  573. .remove = __devexit_p(sh_cmt_remove),
  574. .driver = {
  575. .name = "sh_cmt",
  576. .pm = &sh_cmt_dev_pm_ops,
  577. }
  578. };
  579. static int __init sh_cmt_init(void)
  580. {
  581. return platform_driver_register(&sh_cmt_device_driver);
  582. }
  583. static void __exit sh_cmt_exit(void)
  584. {
  585. platform_driver_unregister(&sh_cmt_device_driver);
  586. }
  587. early_platform_init("earlytimer", &sh_cmt_device_driver);
  588. module_init(sh_cmt_init);
  589. module_exit(sh_cmt_exit);
  590. MODULE_AUTHOR("Magnus Damm");
  591. MODULE_DESCRIPTION("SuperH CMT Timer Driver");
  592. MODULE_LICENSE("GPL v2");