rtc-lpc32xx.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Copyright (C) 2010 NXP Semiconductors
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * You should have received a copy of the GNU General Public License along
  10. * with this program; if not, write to the Free Software Foundation, Inc.,
  11. * 675 Mass Ave, Cambridge, MA 02139, USA.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/rtc.h>
  19. #include <linux/slab.h>
  20. #include <linux/io.h>
  21. /*
  22. * Clock and Power control register offsets
  23. */
  24. #define LPC32XX_RTC_UCOUNT 0x00
  25. #define LPC32XX_RTC_DCOUNT 0x04
  26. #define LPC32XX_RTC_MATCH0 0x08
  27. #define LPC32XX_RTC_MATCH1 0x0C
  28. #define LPC32XX_RTC_CTRL 0x10
  29. #define LPC32XX_RTC_INTSTAT 0x14
  30. #define LPC32XX_RTC_KEY 0x18
  31. #define LPC32XX_RTC_SRAM 0x80
  32. #define LPC32XX_RTC_CTRL_MATCH0 (1 << 0)
  33. #define LPC32XX_RTC_CTRL_MATCH1 (1 << 1)
  34. #define LPC32XX_RTC_CTRL_ONSW_MATCH0 (1 << 2)
  35. #define LPC32XX_RTC_CTRL_ONSW_MATCH1 (1 << 3)
  36. #define LPC32XX_RTC_CTRL_SW_RESET (1 << 4)
  37. #define LPC32XX_RTC_CTRL_CNTR_DIS (1 << 6)
  38. #define LPC32XX_RTC_CTRL_ONSW_FORCE_HI (1 << 7)
  39. #define LPC32XX_RTC_INTSTAT_MATCH0 (1 << 0)
  40. #define LPC32XX_RTC_INTSTAT_MATCH1 (1 << 1)
  41. #define LPC32XX_RTC_INTSTAT_ONSW (1 << 2)
  42. #define LPC32XX_RTC_KEY_ONSW_LOADVAL 0xB5C13F27
  43. #define RTC_NAME "rtc-lpc32xx"
  44. #define rtc_readl(dev, reg) \
  45. __raw_readl((dev)->rtc_base + (reg))
  46. #define rtc_writel(dev, reg, val) \
  47. __raw_writel((val), (dev)->rtc_base + (reg))
  48. struct lpc32xx_rtc {
  49. void __iomem *rtc_base;
  50. int irq;
  51. unsigned char alarm_enabled;
  52. struct rtc_device *rtc;
  53. spinlock_t lock;
  54. };
  55. static int lpc32xx_rtc_read_time(struct device *dev, struct rtc_time *time)
  56. {
  57. unsigned long elapsed_sec;
  58. struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
  59. elapsed_sec = rtc_readl(rtc, LPC32XX_RTC_UCOUNT);
  60. rtc_time_to_tm(elapsed_sec, time);
  61. return rtc_valid_tm(time);
  62. }
  63. static int lpc32xx_rtc_set_mmss(struct device *dev, unsigned long secs)
  64. {
  65. struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
  66. u32 tmp;
  67. spin_lock_irq(&rtc->lock);
  68. /* RTC must be disabled during count update */
  69. tmp = rtc_readl(rtc, LPC32XX_RTC_CTRL);
  70. rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp | LPC32XX_RTC_CTRL_CNTR_DIS);
  71. rtc_writel(rtc, LPC32XX_RTC_UCOUNT, secs);
  72. rtc_writel(rtc, LPC32XX_RTC_DCOUNT, 0xFFFFFFFF - secs);
  73. rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp &= ~LPC32XX_RTC_CTRL_CNTR_DIS);
  74. spin_unlock_irq(&rtc->lock);
  75. return 0;
  76. }
  77. static int lpc32xx_rtc_read_alarm(struct device *dev,
  78. struct rtc_wkalrm *wkalrm)
  79. {
  80. struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
  81. rtc_time_to_tm(rtc_readl(rtc, LPC32XX_RTC_MATCH0), &wkalrm->time);
  82. wkalrm->enabled = rtc->alarm_enabled;
  83. wkalrm->pending = !!(rtc_readl(rtc, LPC32XX_RTC_INTSTAT) &
  84. LPC32XX_RTC_INTSTAT_MATCH0);
  85. return rtc_valid_tm(&wkalrm->time);
  86. }
  87. static int lpc32xx_rtc_set_alarm(struct device *dev,
  88. struct rtc_wkalrm *wkalrm)
  89. {
  90. struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
  91. unsigned long alarmsecs;
  92. u32 tmp;
  93. int ret;
  94. ret = rtc_tm_to_time(&wkalrm->time, &alarmsecs);
  95. if (ret < 0) {
  96. dev_warn(dev, "Failed to convert time: %d\n", ret);
  97. return ret;
  98. }
  99. spin_lock_irq(&rtc->lock);
  100. /* Disable alarm during update */
  101. tmp = rtc_readl(rtc, LPC32XX_RTC_CTRL);
  102. rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp & ~LPC32XX_RTC_CTRL_MATCH0);
  103. rtc_writel(rtc, LPC32XX_RTC_MATCH0, alarmsecs);
  104. rtc->alarm_enabled = wkalrm->enabled;
  105. if (wkalrm->enabled) {
  106. rtc_writel(rtc, LPC32XX_RTC_INTSTAT,
  107. LPC32XX_RTC_INTSTAT_MATCH0);
  108. rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp |
  109. LPC32XX_RTC_CTRL_MATCH0);
  110. }
  111. spin_unlock_irq(&rtc->lock);
  112. return 0;
  113. }
  114. static int lpc32xx_rtc_alarm_irq_enable(struct device *dev,
  115. unsigned int enabled)
  116. {
  117. struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
  118. u32 tmp;
  119. spin_lock_irq(&rtc->lock);
  120. tmp = rtc_readl(rtc, LPC32XX_RTC_CTRL);
  121. if (enabled) {
  122. rtc->alarm_enabled = 1;
  123. tmp |= LPC32XX_RTC_CTRL_MATCH0;
  124. } else {
  125. rtc->alarm_enabled = 0;
  126. tmp &= ~LPC32XX_RTC_CTRL_MATCH0;
  127. }
  128. rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp);
  129. spin_unlock_irq(&rtc->lock);
  130. return 0;
  131. }
  132. static irqreturn_t lpc32xx_rtc_alarm_interrupt(int irq, void *dev)
  133. {
  134. struct lpc32xx_rtc *rtc = dev;
  135. spin_lock(&rtc->lock);
  136. /* Disable alarm interrupt */
  137. rtc_writel(rtc, LPC32XX_RTC_CTRL,
  138. rtc_readl(rtc, LPC32XX_RTC_CTRL) &
  139. ~LPC32XX_RTC_CTRL_MATCH0);
  140. rtc->alarm_enabled = 0;
  141. /*
  142. * Write a large value to the match value so the RTC won't
  143. * keep firing the match status
  144. */
  145. rtc_writel(rtc, LPC32XX_RTC_MATCH0, 0xFFFFFFFF);
  146. rtc_writel(rtc, LPC32XX_RTC_INTSTAT, LPC32XX_RTC_INTSTAT_MATCH0);
  147. spin_unlock(&rtc->lock);
  148. rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
  149. return IRQ_HANDLED;
  150. }
  151. static const struct rtc_class_ops lpc32xx_rtc_ops = {
  152. .read_time = lpc32xx_rtc_read_time,
  153. .set_mmss = lpc32xx_rtc_set_mmss,
  154. .read_alarm = lpc32xx_rtc_read_alarm,
  155. .set_alarm = lpc32xx_rtc_set_alarm,
  156. .alarm_irq_enable = lpc32xx_rtc_alarm_irq_enable,
  157. };
  158. static int __devinit lpc32xx_rtc_probe(struct platform_device *pdev)
  159. {
  160. struct resource *res;
  161. struct lpc32xx_rtc *rtc;
  162. resource_size_t size;
  163. int rtcirq;
  164. u32 tmp;
  165. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  166. if (!res) {
  167. dev_err(&pdev->dev, "Can't get memory resource\n");
  168. return -ENOENT;
  169. }
  170. rtcirq = platform_get_irq(pdev, 0);
  171. if (rtcirq < 0 || rtcirq >= NR_IRQS) {
  172. dev_warn(&pdev->dev, "Can't get interrupt resource\n");
  173. rtcirq = -1;
  174. }
  175. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  176. if (unlikely(!rtc)) {
  177. dev_err(&pdev->dev, "Can't allocate memory\n");
  178. return -ENOMEM;
  179. }
  180. rtc->irq = rtcirq;
  181. size = resource_size(res);
  182. if (!devm_request_mem_region(&pdev->dev, res->start, size,
  183. pdev->name)) {
  184. dev_err(&pdev->dev, "RTC registers are not free\n");
  185. return -EBUSY;
  186. }
  187. rtc->rtc_base = devm_ioremap(&pdev->dev, res->start, size);
  188. if (!rtc->rtc_base) {
  189. dev_err(&pdev->dev, "Can't map memory\n");
  190. return -ENOMEM;
  191. }
  192. spin_lock_init(&rtc->lock);
  193. /*
  194. * The RTC is on a separate power domain and can keep it's state
  195. * across a chip power cycle. If the RTC has never been previously
  196. * setup, then set it up now for the first time.
  197. */
  198. tmp = rtc_readl(rtc, LPC32XX_RTC_CTRL);
  199. if (rtc_readl(rtc, LPC32XX_RTC_KEY) != LPC32XX_RTC_KEY_ONSW_LOADVAL) {
  200. tmp &= ~(LPC32XX_RTC_CTRL_SW_RESET |
  201. LPC32XX_RTC_CTRL_CNTR_DIS |
  202. LPC32XX_RTC_CTRL_MATCH0 |
  203. LPC32XX_RTC_CTRL_MATCH1 |
  204. LPC32XX_RTC_CTRL_ONSW_MATCH0 |
  205. LPC32XX_RTC_CTRL_ONSW_MATCH1 |
  206. LPC32XX_RTC_CTRL_ONSW_FORCE_HI);
  207. rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp);
  208. /* Clear latched interrupt states */
  209. rtc_writel(rtc, LPC32XX_RTC_MATCH0, 0xFFFFFFFF);
  210. rtc_writel(rtc, LPC32XX_RTC_INTSTAT,
  211. LPC32XX_RTC_INTSTAT_MATCH0 |
  212. LPC32XX_RTC_INTSTAT_MATCH1 |
  213. LPC32XX_RTC_INTSTAT_ONSW);
  214. /* Write key value to RTC so it won't reload on reset */
  215. rtc_writel(rtc, LPC32XX_RTC_KEY,
  216. LPC32XX_RTC_KEY_ONSW_LOADVAL);
  217. } else {
  218. rtc_writel(rtc, LPC32XX_RTC_CTRL,
  219. tmp & ~LPC32XX_RTC_CTRL_MATCH0);
  220. }
  221. platform_set_drvdata(pdev, rtc);
  222. rtc->rtc = rtc_device_register(RTC_NAME, &pdev->dev, &lpc32xx_rtc_ops,
  223. THIS_MODULE);
  224. if (IS_ERR(rtc->rtc)) {
  225. dev_err(&pdev->dev, "Can't get RTC\n");
  226. platform_set_drvdata(pdev, NULL);
  227. return PTR_ERR(rtc->rtc);
  228. }
  229. /*
  230. * IRQ is enabled after device registration in case alarm IRQ
  231. * is pending upon suspend exit.
  232. */
  233. if (rtc->irq >= 0) {
  234. if (devm_request_irq(&pdev->dev, rtc->irq,
  235. lpc32xx_rtc_alarm_interrupt,
  236. 0, pdev->name, rtc) < 0) {
  237. dev_warn(&pdev->dev, "Can't request interrupt.\n");
  238. rtc->irq = -1;
  239. } else {
  240. device_init_wakeup(&pdev->dev, 1);
  241. }
  242. }
  243. return 0;
  244. }
  245. static int __devexit lpc32xx_rtc_remove(struct platform_device *pdev)
  246. {
  247. struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
  248. if (rtc->irq >= 0)
  249. device_init_wakeup(&pdev->dev, 0);
  250. platform_set_drvdata(pdev, NULL);
  251. rtc_device_unregister(rtc->rtc);
  252. return 0;
  253. }
  254. #ifdef CONFIG_PM
  255. static int lpc32xx_rtc_suspend(struct device *dev)
  256. {
  257. struct platform_device *pdev = to_platform_device(dev);
  258. struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
  259. if (rtc->irq >= 0) {
  260. if (device_may_wakeup(&pdev->dev))
  261. enable_irq_wake(rtc->irq);
  262. else
  263. disable_irq_wake(rtc->irq);
  264. }
  265. return 0;
  266. }
  267. static int lpc32xx_rtc_resume(struct device *dev)
  268. {
  269. struct platform_device *pdev = to_platform_device(dev);
  270. struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
  271. if (rtc->irq >= 0 && device_may_wakeup(&pdev->dev))
  272. disable_irq_wake(rtc->irq);
  273. return 0;
  274. }
  275. /* Unconditionally disable the alarm */
  276. static int lpc32xx_rtc_freeze(struct device *dev)
  277. {
  278. struct platform_device *pdev = to_platform_device(dev);
  279. struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
  280. spin_lock_irq(&rtc->lock);
  281. rtc_writel(rtc, LPC32XX_RTC_CTRL,
  282. rtc_readl(rtc, LPC32XX_RTC_CTRL) &
  283. ~LPC32XX_RTC_CTRL_MATCH0);
  284. spin_unlock_irq(&rtc->lock);
  285. return 0;
  286. }
  287. static int lpc32xx_rtc_thaw(struct device *dev)
  288. {
  289. struct platform_device *pdev = to_platform_device(dev);
  290. struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
  291. if (rtc->alarm_enabled) {
  292. spin_lock_irq(&rtc->lock);
  293. rtc_writel(rtc, LPC32XX_RTC_CTRL,
  294. rtc_readl(rtc, LPC32XX_RTC_CTRL) |
  295. LPC32XX_RTC_CTRL_MATCH0);
  296. spin_unlock_irq(&rtc->lock);
  297. }
  298. return 0;
  299. }
  300. static const struct dev_pm_ops lpc32xx_rtc_pm_ops = {
  301. .suspend = lpc32xx_rtc_suspend,
  302. .resume = lpc32xx_rtc_resume,
  303. .freeze = lpc32xx_rtc_freeze,
  304. .thaw = lpc32xx_rtc_thaw,
  305. .restore = lpc32xx_rtc_resume
  306. };
  307. #define LPC32XX_RTC_PM_OPS (&lpc32xx_rtc_pm_ops)
  308. #else
  309. #define LPC32XX_RTC_PM_OPS NULL
  310. #endif
  311. static struct platform_driver lpc32xx_rtc_driver = {
  312. .probe = lpc32xx_rtc_probe,
  313. .remove = __devexit_p(lpc32xx_rtc_remove),
  314. .driver = {
  315. .name = RTC_NAME,
  316. .owner = THIS_MODULE,
  317. .pm = LPC32XX_RTC_PM_OPS
  318. },
  319. };
  320. module_platform_driver(lpc32xx_rtc_driver);
  321. MODULE_AUTHOR("Kevin Wells <wellsk40@gmail.com");
  322. MODULE_DESCRIPTION("RTC driver for the LPC32xx SoC");
  323. MODULE_LICENSE("GPL");
  324. MODULE_ALIAS("platform:rtc-lpc32xx");