rtc-lpc32xx.c 9.7 KB

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