rtc-spear.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * drivers/rtc/rtc-spear.c
  3. *
  4. * Copyright (C) 2010 ST Microelectronics
  5. * Rajeev Kumar<rajeev-dlh.kumar@st.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/bcd.h>
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <linux/irq.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/rtc.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. /* RTC registers */
  23. #define TIME_REG 0x00
  24. #define DATE_REG 0x04
  25. #define ALARM_TIME_REG 0x08
  26. #define ALARM_DATE_REG 0x0C
  27. #define CTRL_REG 0x10
  28. #define STATUS_REG 0x14
  29. /* TIME_REG & ALARM_TIME_REG */
  30. #define SECONDS_UNITS (0xf<<0) /* seconds units position */
  31. #define SECONDS_TENS (0x7<<4) /* seconds tens position */
  32. #define MINUTES_UNITS (0xf<<8) /* minutes units position */
  33. #define MINUTES_TENS (0x7<<12) /* minutes tens position */
  34. #define HOURS_UNITS (0xf<<16) /* hours units position */
  35. #define HOURS_TENS (0x3<<20) /* hours tens position */
  36. /* DATE_REG & ALARM_DATE_REG */
  37. #define DAYS_UNITS (0xf<<0) /* days units position */
  38. #define DAYS_TENS (0x3<<4) /* days tens position */
  39. #define MONTHS_UNITS (0xf<<8) /* months units position */
  40. #define MONTHS_TENS (0x1<<12) /* months tens position */
  41. #define YEARS_UNITS (0xf<<16) /* years units position */
  42. #define YEARS_TENS (0xf<<20) /* years tens position */
  43. #define YEARS_HUNDREDS (0xf<<24) /* years hundereds position */
  44. #define YEARS_MILLENIUMS (0xf<<28) /* years millenium position */
  45. /* MASK SHIFT TIME_REG & ALARM_TIME_REG*/
  46. #define SECOND_SHIFT 0x00 /* seconds units */
  47. #define MINUTE_SHIFT 0x08 /* minutes units position */
  48. #define HOUR_SHIFT 0x10 /* hours units position */
  49. #define MDAY_SHIFT 0x00 /* Month day shift */
  50. #define MONTH_SHIFT 0x08 /* Month shift */
  51. #define YEAR_SHIFT 0x10 /* Year shift */
  52. #define SECOND_MASK 0x7F
  53. #define MIN_MASK 0x7F
  54. #define HOUR_MASK 0x3F
  55. #define DAY_MASK 0x3F
  56. #define MONTH_MASK 0x7F
  57. #define YEAR_MASK 0xFFFF
  58. /* date reg equal to time reg, for debug only */
  59. #define TIME_BYP (1<<9)
  60. #define INT_ENABLE (1<<31) /* interrupt enable */
  61. /* STATUS_REG */
  62. #define CLK_UNCONNECTED (1<<0)
  63. #define PEND_WR_TIME (1<<2)
  64. #define PEND_WR_DATE (1<<3)
  65. #define LOST_WR_TIME (1<<4)
  66. #define LOST_WR_DATE (1<<5)
  67. #define RTC_INT_MASK (1<<31)
  68. #define STATUS_BUSY (PEND_WR_TIME | PEND_WR_DATE)
  69. #define STATUS_FAIL (LOST_WR_TIME | LOST_WR_DATE)
  70. struct spear_rtc_config {
  71. struct clk *clk;
  72. spinlock_t lock;
  73. void __iomem *ioaddr;
  74. unsigned int irq_wake;
  75. };
  76. static inline void spear_rtc_clear_interrupt(struct spear_rtc_config *config)
  77. {
  78. unsigned int val;
  79. unsigned long flags;
  80. spin_lock_irqsave(&config->lock, flags);
  81. val = readl(config->ioaddr + STATUS_REG);
  82. val |= RTC_INT_MASK;
  83. writel(val, config->ioaddr + STATUS_REG);
  84. spin_unlock_irqrestore(&config->lock, flags);
  85. }
  86. static inline void spear_rtc_enable_interrupt(struct spear_rtc_config *config)
  87. {
  88. unsigned int val;
  89. val = readl(config->ioaddr + CTRL_REG);
  90. if (!(val & INT_ENABLE)) {
  91. spear_rtc_clear_interrupt(config);
  92. val |= INT_ENABLE;
  93. writel(val, config->ioaddr + CTRL_REG);
  94. }
  95. }
  96. static inline void spear_rtc_disable_interrupt(struct spear_rtc_config *config)
  97. {
  98. unsigned int val;
  99. val = readl(config->ioaddr + CTRL_REG);
  100. if (val & INT_ENABLE) {
  101. val &= ~INT_ENABLE;
  102. writel(val, config->ioaddr + CTRL_REG);
  103. }
  104. }
  105. static inline int is_write_complete(struct spear_rtc_config *config)
  106. {
  107. int ret = 0;
  108. unsigned long flags;
  109. spin_lock_irqsave(&config->lock, flags);
  110. if ((readl(config->ioaddr + STATUS_REG)) & STATUS_FAIL)
  111. ret = -EIO;
  112. spin_unlock_irqrestore(&config->lock, flags);
  113. return ret;
  114. }
  115. static void rtc_wait_not_busy(struct spear_rtc_config *config)
  116. {
  117. int status, count = 0;
  118. unsigned long flags;
  119. /* Assuming BUSY may stay active for 80 msec) */
  120. for (count = 0; count < 80; count++) {
  121. spin_lock_irqsave(&config->lock, flags);
  122. status = readl(config->ioaddr + STATUS_REG);
  123. spin_unlock_irqrestore(&config->lock, flags);
  124. if ((status & STATUS_BUSY) == 0)
  125. break;
  126. /* check status busy, after each msec */
  127. msleep(1);
  128. }
  129. }
  130. static irqreturn_t spear_rtc_irq(int irq, void *dev_id)
  131. {
  132. struct rtc_device *rtc = (struct rtc_device *)dev_id;
  133. struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
  134. unsigned long flags, events = 0;
  135. unsigned int irq_data;
  136. spin_lock_irqsave(&config->lock, flags);
  137. irq_data = readl(config->ioaddr + STATUS_REG);
  138. spin_unlock_irqrestore(&config->lock, flags);
  139. if ((irq_data & RTC_INT_MASK)) {
  140. spear_rtc_clear_interrupt(config);
  141. events = RTC_IRQF | RTC_AF;
  142. rtc_update_irq(rtc, 1, events);
  143. return IRQ_HANDLED;
  144. } else
  145. return IRQ_NONE;
  146. }
  147. static int tm2bcd(struct rtc_time *tm)
  148. {
  149. if (rtc_valid_tm(tm) != 0)
  150. return -EINVAL;
  151. tm->tm_sec = bin2bcd(tm->tm_sec);
  152. tm->tm_min = bin2bcd(tm->tm_min);
  153. tm->tm_hour = bin2bcd(tm->tm_hour);
  154. tm->tm_mday = bin2bcd(tm->tm_mday);
  155. tm->tm_mon = bin2bcd(tm->tm_mon + 1);
  156. tm->tm_year = bin2bcd(tm->tm_year);
  157. return 0;
  158. }
  159. static void bcd2tm(struct rtc_time *tm)
  160. {
  161. tm->tm_sec = bcd2bin(tm->tm_sec);
  162. tm->tm_min = bcd2bin(tm->tm_min);
  163. tm->tm_hour = bcd2bin(tm->tm_hour);
  164. tm->tm_mday = bcd2bin(tm->tm_mday);
  165. tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
  166. /* epoch == 1900 */
  167. tm->tm_year = bcd2bin(tm->tm_year);
  168. }
  169. /*
  170. * spear_rtc_read_time - set the time
  171. * @dev: rtc device in use
  172. * @tm: holds date and time
  173. *
  174. * This function read time and date. On success it will return 0
  175. * otherwise -ve error is returned.
  176. */
  177. static int spear_rtc_read_time(struct device *dev, struct rtc_time *tm)
  178. {
  179. struct platform_device *pdev = to_platform_device(dev);
  180. struct rtc_device *rtc = platform_get_drvdata(pdev);
  181. struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
  182. unsigned int time, date;
  183. /* we don't report wday/yday/isdst ... */
  184. rtc_wait_not_busy(config);
  185. time = readl(config->ioaddr + TIME_REG);
  186. date = readl(config->ioaddr + DATE_REG);
  187. tm->tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
  188. tm->tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
  189. tm->tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
  190. tm->tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
  191. tm->tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
  192. tm->tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
  193. bcd2tm(tm);
  194. return 0;
  195. }
  196. /*
  197. * spear_rtc_set_time - set the time
  198. * @dev: rtc device in use
  199. * @tm: holds date and time
  200. *
  201. * This function set time and date. On success it will return 0
  202. * otherwise -ve error is returned.
  203. */
  204. static int spear_rtc_set_time(struct device *dev, struct rtc_time *tm)
  205. {
  206. struct platform_device *pdev = to_platform_device(dev);
  207. struct rtc_device *rtc = platform_get_drvdata(pdev);
  208. struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
  209. unsigned int time, date, err = 0;
  210. if (tm2bcd(tm) < 0)
  211. return -EINVAL;
  212. rtc_wait_not_busy(config);
  213. time = (tm->tm_sec << SECOND_SHIFT) | (tm->tm_min << MINUTE_SHIFT) |
  214. (tm->tm_hour << HOUR_SHIFT);
  215. date = (tm->tm_mday << MDAY_SHIFT) | (tm->tm_mon << MONTH_SHIFT) |
  216. (tm->tm_year << YEAR_SHIFT);
  217. writel(time, config->ioaddr + TIME_REG);
  218. writel(date, config->ioaddr + DATE_REG);
  219. err = is_write_complete(config);
  220. if (err < 0)
  221. return err;
  222. return 0;
  223. }
  224. /*
  225. * spear_rtc_read_alarm - read the alarm time
  226. * @dev: rtc device in use
  227. * @alm: holds alarm date and time
  228. *
  229. * This function read alarm time and date. On success it will return 0
  230. * otherwise -ve error is returned.
  231. */
  232. static int spear_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  233. {
  234. struct platform_device *pdev = to_platform_device(dev);
  235. struct rtc_device *rtc = platform_get_drvdata(pdev);
  236. struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
  237. unsigned int time, date;
  238. rtc_wait_not_busy(config);
  239. time = readl(config->ioaddr + ALARM_TIME_REG);
  240. date = readl(config->ioaddr + ALARM_DATE_REG);
  241. alm->time.tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
  242. alm->time.tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
  243. alm->time.tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
  244. alm->time.tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
  245. alm->time.tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
  246. alm->time.tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
  247. bcd2tm(&alm->time);
  248. alm->enabled = readl(config->ioaddr + CTRL_REG) & INT_ENABLE;
  249. return 0;
  250. }
  251. /*
  252. * spear_rtc_set_alarm - set the alarm time
  253. * @dev: rtc device in use
  254. * @alm: holds alarm date and time
  255. *
  256. * This function set alarm time and date. On success it will return 0
  257. * otherwise -ve error is returned.
  258. */
  259. static int spear_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  260. {
  261. struct platform_device *pdev = to_platform_device(dev);
  262. struct rtc_device *rtc = platform_get_drvdata(pdev);
  263. struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
  264. unsigned int time, date, err = 0;
  265. if (tm2bcd(&alm->time) < 0)
  266. return -EINVAL;
  267. rtc_wait_not_busy(config);
  268. time = (alm->time.tm_sec << SECOND_SHIFT) | (alm->time.tm_min <<
  269. MINUTE_SHIFT) | (alm->time.tm_hour << HOUR_SHIFT);
  270. date = (alm->time.tm_mday << MDAY_SHIFT) | (alm->time.tm_mon <<
  271. MONTH_SHIFT) | (alm->time.tm_year << YEAR_SHIFT);
  272. writel(time, config->ioaddr + ALARM_TIME_REG);
  273. writel(date, config->ioaddr + ALARM_DATE_REG);
  274. err = is_write_complete(config);
  275. if (err < 0)
  276. return err;
  277. if (alm->enabled)
  278. spear_rtc_enable_interrupt(config);
  279. else
  280. spear_rtc_disable_interrupt(config);
  281. return 0;
  282. }
  283. static struct rtc_class_ops spear_rtc_ops = {
  284. .read_time = spear_rtc_read_time,
  285. .set_time = spear_rtc_set_time,
  286. .read_alarm = spear_rtc_read_alarm,
  287. .set_alarm = spear_rtc_set_alarm,
  288. };
  289. static int __devinit spear_rtc_probe(struct platform_device *pdev)
  290. {
  291. struct resource *res;
  292. struct rtc_device *rtc;
  293. struct spear_rtc_config *config;
  294. unsigned int status = 0;
  295. int irq;
  296. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  297. if (!res) {
  298. dev_err(&pdev->dev, "no resource defined\n");
  299. return -EBUSY;
  300. }
  301. if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
  302. dev_err(&pdev->dev, "rtc region already claimed\n");
  303. return -EBUSY;
  304. }
  305. config = kzalloc(sizeof(*config), GFP_KERNEL);
  306. if (!config) {
  307. dev_err(&pdev->dev, "out of memory\n");
  308. status = -ENOMEM;
  309. goto err_release_region;
  310. }
  311. config->clk = clk_get(&pdev->dev, NULL);
  312. if (IS_ERR(config->clk)) {
  313. status = PTR_ERR(config->clk);
  314. goto err_kfree;
  315. }
  316. status = clk_enable(config->clk);
  317. if (status < 0)
  318. goto err_clk_put;
  319. config->ioaddr = ioremap(res->start, resource_size(res));
  320. if (!config->ioaddr) {
  321. dev_err(&pdev->dev, "ioremap fail\n");
  322. status = -ENOMEM;
  323. goto err_disable_clock;
  324. }
  325. spin_lock_init(&config->lock);
  326. rtc = rtc_device_register(pdev->name, &pdev->dev, &spear_rtc_ops,
  327. THIS_MODULE);
  328. if (IS_ERR(rtc)) {
  329. dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
  330. PTR_ERR(rtc));
  331. status = PTR_ERR(rtc);
  332. goto err_iounmap;
  333. }
  334. platform_set_drvdata(pdev, rtc);
  335. dev_set_drvdata(&rtc->dev, config);
  336. /* alarm irqs */
  337. irq = platform_get_irq(pdev, 0);
  338. if (irq < 0) {
  339. dev_err(&pdev->dev, "no update irq?\n");
  340. status = irq;
  341. goto err_clear_platdata;
  342. }
  343. status = request_irq(irq, spear_rtc_irq, 0, pdev->name, rtc);
  344. if (status) {
  345. dev_err(&pdev->dev, "Alarm interrupt IRQ%d already \
  346. claimed\n", irq);
  347. goto err_clear_platdata;
  348. }
  349. if (!device_can_wakeup(&pdev->dev))
  350. device_init_wakeup(&pdev->dev, 1);
  351. return 0;
  352. err_clear_platdata:
  353. platform_set_drvdata(pdev, NULL);
  354. dev_set_drvdata(&rtc->dev, NULL);
  355. rtc_device_unregister(rtc);
  356. err_iounmap:
  357. iounmap(config->ioaddr);
  358. err_disable_clock:
  359. clk_disable(config->clk);
  360. err_clk_put:
  361. clk_put(config->clk);
  362. err_kfree:
  363. kfree(config);
  364. err_release_region:
  365. release_mem_region(res->start, resource_size(res));
  366. return status;
  367. }
  368. static int __devexit spear_rtc_remove(struct platform_device *pdev)
  369. {
  370. struct rtc_device *rtc = platform_get_drvdata(pdev);
  371. struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
  372. int irq;
  373. struct resource *res;
  374. /* leave rtc running, but disable irqs */
  375. spear_rtc_disable_interrupt(config);
  376. device_init_wakeup(&pdev->dev, 0);
  377. irq = platform_get_irq(pdev, 0);
  378. if (irq)
  379. free_irq(irq, pdev);
  380. clk_disable(config->clk);
  381. clk_put(config->clk);
  382. iounmap(config->ioaddr);
  383. kfree(config);
  384. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  385. if (res)
  386. release_mem_region(res->start, resource_size(res));
  387. platform_set_drvdata(pdev, NULL);
  388. dev_set_drvdata(&rtc->dev, NULL);
  389. rtc_device_unregister(rtc);
  390. return 0;
  391. }
  392. #ifdef CONFIG_PM
  393. static int spear_rtc_suspend(struct platform_device *pdev, pm_message_t state)
  394. {
  395. struct rtc_device *rtc = platform_get_drvdata(pdev);
  396. struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
  397. int irq;
  398. irq = platform_get_irq(pdev, 0);
  399. if (device_may_wakeup(&pdev->dev)) {
  400. if (!enable_irq_wake(irq))
  401. config->irq_wake = 1;
  402. } else {
  403. spear_rtc_disable_interrupt(config);
  404. clk_disable(config->clk);
  405. }
  406. return 0;
  407. }
  408. static int spear_rtc_resume(struct platform_device *pdev)
  409. {
  410. struct rtc_device *rtc = platform_get_drvdata(pdev);
  411. struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
  412. int irq;
  413. irq = platform_get_irq(pdev, 0);
  414. if (device_may_wakeup(&pdev->dev)) {
  415. if (config->irq_wake) {
  416. disable_irq_wake(irq);
  417. config->irq_wake = 0;
  418. }
  419. } else {
  420. clk_enable(config->clk);
  421. spear_rtc_enable_interrupt(config);
  422. }
  423. return 0;
  424. }
  425. #else
  426. #define spear_rtc_suspend NULL
  427. #define spear_rtc_resume NULL
  428. #endif
  429. static void spear_rtc_shutdown(struct platform_device *pdev)
  430. {
  431. struct rtc_device *rtc = platform_get_drvdata(pdev);
  432. struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
  433. spear_rtc_disable_interrupt(config);
  434. clk_disable(config->clk);
  435. }
  436. static struct platform_driver spear_rtc_driver = {
  437. .probe = spear_rtc_probe,
  438. .remove = __devexit_p(spear_rtc_remove),
  439. .suspend = spear_rtc_suspend,
  440. .resume = spear_rtc_resume,
  441. .shutdown = spear_rtc_shutdown,
  442. .driver = {
  443. .name = "rtc-spear",
  444. },
  445. };
  446. module_platform_driver(spear_rtc_driver);
  447. MODULE_ALIAS("platform:rtc-spear");
  448. MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
  449. MODULE_DESCRIPTION("ST SPEAr Realtime Clock Driver (RTC)");
  450. MODULE_LICENSE("GPL");