rtc-jz4740.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
  3. * Copyright (C) 2010, Paul Cercueil <paul@crapouillou.net>
  4. * JZ4740 SoC RTC driver
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * You should have received a copy of the GNU General Public License along
  12. * with this program; if not, write to the Free Software Foundation, Inc.,
  13. * 675 Mass Ave, Cambridge, MA 02139, USA.
  14. *
  15. */
  16. #include <linux/io.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/rtc.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #define JZ_REG_RTC_CTRL 0x00
  24. #define JZ_REG_RTC_SEC 0x04
  25. #define JZ_REG_RTC_SEC_ALARM 0x08
  26. #define JZ_REG_RTC_REGULATOR 0x0C
  27. #define JZ_REG_RTC_HIBERNATE 0x20
  28. #define JZ_REG_RTC_SCRATCHPAD 0x34
  29. #define JZ_RTC_CTRL_WRDY BIT(7)
  30. #define JZ_RTC_CTRL_1HZ BIT(6)
  31. #define JZ_RTC_CTRL_1HZ_IRQ BIT(5)
  32. #define JZ_RTC_CTRL_AF BIT(4)
  33. #define JZ_RTC_CTRL_AF_IRQ BIT(3)
  34. #define JZ_RTC_CTRL_AE BIT(2)
  35. #define JZ_RTC_CTRL_ENABLE BIT(0)
  36. struct jz4740_rtc {
  37. void __iomem *base;
  38. struct rtc_device *rtc;
  39. int irq;
  40. spinlock_t lock;
  41. };
  42. static inline uint32_t jz4740_rtc_reg_read(struct jz4740_rtc *rtc, size_t reg)
  43. {
  44. return readl(rtc->base + reg);
  45. }
  46. static int jz4740_rtc_wait_write_ready(struct jz4740_rtc *rtc)
  47. {
  48. uint32_t ctrl;
  49. int timeout = 1000;
  50. do {
  51. ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
  52. } while (!(ctrl & JZ_RTC_CTRL_WRDY) && --timeout);
  53. return timeout ? 0 : -EIO;
  54. }
  55. static inline int jz4740_rtc_reg_write(struct jz4740_rtc *rtc, size_t reg,
  56. uint32_t val)
  57. {
  58. int ret;
  59. ret = jz4740_rtc_wait_write_ready(rtc);
  60. if (ret == 0)
  61. writel(val, rtc->base + reg);
  62. return ret;
  63. }
  64. static int jz4740_rtc_ctrl_set_bits(struct jz4740_rtc *rtc, uint32_t mask,
  65. bool set)
  66. {
  67. int ret;
  68. unsigned long flags;
  69. uint32_t ctrl;
  70. spin_lock_irqsave(&rtc->lock, flags);
  71. ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
  72. /* Don't clear interrupt flags by accident */
  73. ctrl |= JZ_RTC_CTRL_1HZ | JZ_RTC_CTRL_AF;
  74. if (set)
  75. ctrl |= mask;
  76. else
  77. ctrl &= ~mask;
  78. ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_CTRL, ctrl);
  79. spin_unlock_irqrestore(&rtc->lock, flags);
  80. return ret;
  81. }
  82. static int jz4740_rtc_read_time(struct device *dev, struct rtc_time *time)
  83. {
  84. struct jz4740_rtc *rtc = dev_get_drvdata(dev);
  85. uint32_t secs, secs2;
  86. int timeout = 5;
  87. /* If the seconds register is read while it is updated, it can contain a
  88. * bogus value. This can be avoided by making sure that two consecutive
  89. * reads have the same value.
  90. */
  91. secs = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
  92. secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
  93. while (secs != secs2 && --timeout) {
  94. secs = secs2;
  95. secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
  96. }
  97. if (timeout == 0)
  98. return -EIO;
  99. rtc_time_to_tm(secs, time);
  100. return rtc_valid_tm(time);
  101. }
  102. static int jz4740_rtc_set_mmss(struct device *dev, unsigned long secs)
  103. {
  104. struct jz4740_rtc *rtc = dev_get_drvdata(dev);
  105. return jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC, secs);
  106. }
  107. static int jz4740_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  108. {
  109. struct jz4740_rtc *rtc = dev_get_drvdata(dev);
  110. uint32_t secs;
  111. uint32_t ctrl;
  112. secs = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC_ALARM);
  113. ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
  114. alrm->enabled = !!(ctrl & JZ_RTC_CTRL_AE);
  115. alrm->pending = !!(ctrl & JZ_RTC_CTRL_AF);
  116. rtc_time_to_tm(secs, &alrm->time);
  117. return rtc_valid_tm(&alrm->time);
  118. }
  119. static int jz4740_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  120. {
  121. int ret;
  122. struct jz4740_rtc *rtc = dev_get_drvdata(dev);
  123. unsigned long secs;
  124. rtc_tm_to_time(&alrm->time, &secs);
  125. ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC_ALARM, secs);
  126. if (!ret)
  127. ret = jz4740_rtc_ctrl_set_bits(rtc,
  128. JZ_RTC_CTRL_AE | JZ_RTC_CTRL_AF_IRQ, alrm->enabled);
  129. return ret;
  130. }
  131. static int jz4740_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
  132. {
  133. struct jz4740_rtc *rtc = dev_get_drvdata(dev);
  134. return jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_AF_IRQ, enable);
  135. }
  136. static const struct rtc_class_ops jz4740_rtc_ops = {
  137. .read_time = jz4740_rtc_read_time,
  138. .set_mmss = jz4740_rtc_set_mmss,
  139. .read_alarm = jz4740_rtc_read_alarm,
  140. .set_alarm = jz4740_rtc_set_alarm,
  141. .alarm_irq_enable = jz4740_rtc_alarm_irq_enable,
  142. };
  143. static irqreturn_t jz4740_rtc_irq(int irq, void *data)
  144. {
  145. struct jz4740_rtc *rtc = data;
  146. uint32_t ctrl;
  147. unsigned long events = 0;
  148. ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
  149. if (ctrl & JZ_RTC_CTRL_1HZ)
  150. events |= (RTC_UF | RTC_IRQF);
  151. if (ctrl & JZ_RTC_CTRL_AF)
  152. events |= (RTC_AF | RTC_IRQF);
  153. rtc_update_irq(rtc->rtc, 1, events);
  154. jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_1HZ | JZ_RTC_CTRL_AF, false);
  155. return IRQ_HANDLED;
  156. }
  157. void jz4740_rtc_poweroff(struct device *dev)
  158. {
  159. struct jz4740_rtc *rtc = dev_get_drvdata(dev);
  160. jz4740_rtc_reg_write(rtc, JZ_REG_RTC_HIBERNATE, 1);
  161. }
  162. EXPORT_SYMBOL_GPL(jz4740_rtc_poweroff);
  163. static int jz4740_rtc_probe(struct platform_device *pdev)
  164. {
  165. int ret;
  166. struct jz4740_rtc *rtc;
  167. uint32_t scratchpad;
  168. struct resource *mem;
  169. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  170. if (!rtc)
  171. return -ENOMEM;
  172. rtc->irq = platform_get_irq(pdev, 0);
  173. if (rtc->irq < 0) {
  174. dev_err(&pdev->dev, "Failed to get platform irq\n");
  175. return -ENOENT;
  176. }
  177. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  178. rtc->base = devm_ioremap_resource(&pdev->dev, mem);
  179. if (IS_ERR(rtc->base))
  180. return PTR_ERR(rtc->base);
  181. spin_lock_init(&rtc->lock);
  182. platform_set_drvdata(pdev, rtc);
  183. device_init_wakeup(&pdev->dev, 1);
  184. rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  185. &jz4740_rtc_ops, THIS_MODULE);
  186. if (IS_ERR(rtc->rtc)) {
  187. ret = PTR_ERR(rtc->rtc);
  188. dev_err(&pdev->dev, "Failed to register rtc device: %d\n", ret);
  189. return ret;
  190. }
  191. ret = devm_request_irq(&pdev->dev, rtc->irq, jz4740_rtc_irq, 0,
  192. pdev->name, rtc);
  193. if (ret) {
  194. dev_err(&pdev->dev, "Failed to request rtc irq: %d\n", ret);
  195. return ret;
  196. }
  197. scratchpad = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SCRATCHPAD);
  198. if (scratchpad != 0x12345678) {
  199. ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SCRATCHPAD, 0x12345678);
  200. ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC, 0);
  201. if (ret) {
  202. dev_err(&pdev->dev, "Could not write write to RTC registers\n");
  203. return ret;
  204. }
  205. }
  206. return 0;
  207. }
  208. #ifdef CONFIG_PM
  209. static int jz4740_rtc_suspend(struct device *dev)
  210. {
  211. struct jz4740_rtc *rtc = dev_get_drvdata(dev);
  212. if (device_may_wakeup(dev))
  213. enable_irq_wake(rtc->irq);
  214. return 0;
  215. }
  216. static int jz4740_rtc_resume(struct device *dev)
  217. {
  218. struct jz4740_rtc *rtc = dev_get_drvdata(dev);
  219. if (device_may_wakeup(dev))
  220. disable_irq_wake(rtc->irq);
  221. return 0;
  222. }
  223. static const struct dev_pm_ops jz4740_pm_ops = {
  224. .suspend = jz4740_rtc_suspend,
  225. .resume = jz4740_rtc_resume,
  226. };
  227. #define JZ4740_RTC_PM_OPS (&jz4740_pm_ops)
  228. #else
  229. #define JZ4740_RTC_PM_OPS NULL
  230. #endif /* CONFIG_PM */
  231. static struct platform_driver jz4740_rtc_driver = {
  232. .probe = jz4740_rtc_probe,
  233. .driver = {
  234. .name = "jz4740-rtc",
  235. .pm = JZ4740_RTC_PM_OPS,
  236. },
  237. };
  238. module_platform_driver(jz4740_rtc_driver);
  239. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  240. MODULE_LICENSE("GPL");
  241. MODULE_DESCRIPTION("RTC driver for the JZ4740 SoC\n");
  242. MODULE_ALIAS("platform:jz4740-rtc");