rtc-mc13xxx.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Real Time Clock driver for Freescale MC13XXX PMIC
  3. *
  4. * (C) 2009 Sascha Hauer, Pengutronix
  5. * (C) 2009 Uwe Kleine-Koenig, Pengutronix
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/mfd/mc13xxx.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/slab.h>
  17. #include <linux/rtc.h>
  18. #define DRIVER_NAME "mc13xxx-rtc"
  19. #define MC13XXX_RTCTOD 20
  20. #define MC13XXX_RTCTODA 21
  21. #define MC13XXX_RTCDAY 22
  22. #define MC13XXX_RTCDAYA 23
  23. #define SEC_PER_DAY (24 * 60 * 60)
  24. struct mc13xxx_rtc {
  25. struct rtc_device *rtc;
  26. struct mc13xxx *mc13xxx;
  27. int valid;
  28. };
  29. static int mc13xxx_rtc_irq_enable_unlocked(struct device *dev,
  30. unsigned int enabled, int irq)
  31. {
  32. struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
  33. int (*func)(struct mc13xxx *mc13xxx, int irq);
  34. if (!priv->valid)
  35. return -ENODATA;
  36. func = enabled ? mc13xxx_irq_unmask : mc13xxx_irq_mask;
  37. return func(priv->mc13xxx, irq);
  38. }
  39. static int mc13xxx_rtc_alarm_irq_enable(struct device *dev,
  40. unsigned int enabled)
  41. {
  42. struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
  43. int ret;
  44. mc13xxx_lock(priv->mc13xxx);
  45. ret = mc13xxx_rtc_irq_enable_unlocked(dev, enabled, MC13XXX_IRQ_TODA);
  46. mc13xxx_unlock(priv->mc13xxx);
  47. return ret;
  48. }
  49. static int mc13xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
  50. {
  51. struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
  52. unsigned int seconds, days1, days2;
  53. if (!priv->valid)
  54. return -ENODATA;
  55. do {
  56. int ret;
  57. ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days1);
  58. if (ret)
  59. return ret;
  60. ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTOD, &seconds);
  61. if (ret)
  62. return ret;
  63. ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days2);
  64. if (ret)
  65. return ret;
  66. } while (days1 != days2);
  67. rtc_time64_to_tm((time64_t)days1 * SEC_PER_DAY + seconds, tm);
  68. return 0;
  69. }
  70. static int mc13xxx_rtc_set_mmss(struct device *dev, time64_t secs)
  71. {
  72. struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
  73. unsigned int seconds, days;
  74. unsigned int alarmseconds;
  75. int ret;
  76. days = div_s64_rem(secs, SEC_PER_DAY, &seconds);
  77. mc13xxx_lock(priv->mc13xxx);
  78. /*
  79. * temporarily invalidate alarm to prevent triggering it when the day is
  80. * already updated while the time isn't yet.
  81. */
  82. ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTODA, &alarmseconds);
  83. if (unlikely(ret))
  84. goto out;
  85. if (alarmseconds < SEC_PER_DAY) {
  86. ret = mc13xxx_reg_write(priv->mc13xxx,
  87. MC13XXX_RTCTODA, 0x1ffff);
  88. if (unlikely(ret))
  89. goto out;
  90. }
  91. /*
  92. * write seconds=0 to prevent a day switch between writing days
  93. * and seconds below
  94. */
  95. ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTOD, 0);
  96. if (unlikely(ret))
  97. goto out;
  98. ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCDAY, days);
  99. if (unlikely(ret))
  100. goto out;
  101. ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTOD, seconds);
  102. if (unlikely(ret))
  103. goto out;
  104. /* restore alarm */
  105. if (alarmseconds < SEC_PER_DAY) {
  106. ret = mc13xxx_reg_write(priv->mc13xxx,
  107. MC13XXX_RTCTODA, alarmseconds);
  108. if (unlikely(ret))
  109. goto out;
  110. }
  111. if (!priv->valid) {
  112. ret = mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_RTCRST);
  113. if (unlikely(ret))
  114. goto out;
  115. ret = mc13xxx_irq_unmask(priv->mc13xxx, MC13XXX_IRQ_RTCRST);
  116. }
  117. out:
  118. priv->valid = !ret;
  119. mc13xxx_unlock(priv->mc13xxx);
  120. return ret;
  121. }
  122. static int mc13xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  123. {
  124. struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
  125. unsigned seconds, days;
  126. time64_t s1970;
  127. int enabled, pending;
  128. int ret;
  129. mc13xxx_lock(priv->mc13xxx);
  130. ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTODA, &seconds);
  131. if (unlikely(ret))
  132. goto out;
  133. if (seconds >= SEC_PER_DAY) {
  134. ret = -ENODATA;
  135. goto out;
  136. }
  137. ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days);
  138. if (unlikely(ret))
  139. goto out;
  140. ret = mc13xxx_irq_status(priv->mc13xxx, MC13XXX_IRQ_TODA,
  141. &enabled, &pending);
  142. out:
  143. mc13xxx_unlock(priv->mc13xxx);
  144. if (ret)
  145. return ret;
  146. alarm->enabled = enabled;
  147. alarm->pending = pending;
  148. s1970 = (time64_t)days * SEC_PER_DAY + seconds;
  149. rtc_time64_to_tm(s1970, &alarm->time);
  150. dev_dbg(dev, "%s: %lld\n", __func__, (long long)s1970);
  151. return 0;
  152. }
  153. static int mc13xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  154. {
  155. struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
  156. time64_t s1970;
  157. u32 seconds, days;
  158. int ret;
  159. mc13xxx_lock(priv->mc13xxx);
  160. /* disable alarm to prevent false triggering */
  161. ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTODA, 0x1ffff);
  162. if (unlikely(ret))
  163. goto out;
  164. ret = mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_TODA);
  165. if (unlikely(ret))
  166. goto out;
  167. s1970 = rtc_tm_to_time64(&alarm->time);
  168. dev_dbg(dev, "%s: %s %lld\n", __func__, alarm->enabled ? "on" : "off",
  169. (long long)s1970);
  170. ret = mc13xxx_rtc_irq_enable_unlocked(dev, alarm->enabled,
  171. MC13XXX_IRQ_TODA);
  172. if (unlikely(ret))
  173. goto out;
  174. days = div_s64_rem(s1970, SEC_PER_DAY, &seconds);
  175. ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCDAYA, days);
  176. if (unlikely(ret))
  177. goto out;
  178. ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTODA, seconds);
  179. out:
  180. mc13xxx_unlock(priv->mc13xxx);
  181. return ret;
  182. }
  183. static irqreturn_t mc13xxx_rtc_alarm_handler(int irq, void *dev)
  184. {
  185. struct mc13xxx_rtc *priv = dev;
  186. struct mc13xxx *mc13xxx = priv->mc13xxx;
  187. rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_AF);
  188. mc13xxx_irq_ack(mc13xxx, irq);
  189. return IRQ_HANDLED;
  190. }
  191. static const struct rtc_class_ops mc13xxx_rtc_ops = {
  192. .read_time = mc13xxx_rtc_read_time,
  193. .set_mmss64 = mc13xxx_rtc_set_mmss,
  194. .read_alarm = mc13xxx_rtc_read_alarm,
  195. .set_alarm = mc13xxx_rtc_set_alarm,
  196. .alarm_irq_enable = mc13xxx_rtc_alarm_irq_enable,
  197. };
  198. static irqreturn_t mc13xxx_rtc_reset_handler(int irq, void *dev)
  199. {
  200. struct mc13xxx_rtc *priv = dev;
  201. struct mc13xxx *mc13xxx = priv->mc13xxx;
  202. priv->valid = 0;
  203. mc13xxx_irq_mask(mc13xxx, irq);
  204. return IRQ_HANDLED;
  205. }
  206. static int __init mc13xxx_rtc_probe(struct platform_device *pdev)
  207. {
  208. int ret;
  209. struct mc13xxx_rtc *priv;
  210. struct mc13xxx *mc13xxx;
  211. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  212. if (!priv)
  213. return -ENOMEM;
  214. mc13xxx = dev_get_drvdata(pdev->dev.parent);
  215. priv->mc13xxx = mc13xxx;
  216. priv->valid = 1;
  217. platform_set_drvdata(pdev, priv);
  218. mc13xxx_lock(mc13xxx);
  219. mc13xxx_irq_ack(mc13xxx, MC13XXX_IRQ_RTCRST);
  220. ret = mc13xxx_irq_request(mc13xxx, MC13XXX_IRQ_RTCRST,
  221. mc13xxx_rtc_reset_handler, DRIVER_NAME, priv);
  222. if (ret)
  223. goto err_irq_request;
  224. ret = mc13xxx_irq_request_nounmask(mc13xxx, MC13XXX_IRQ_TODA,
  225. mc13xxx_rtc_alarm_handler, DRIVER_NAME, priv);
  226. if (ret)
  227. goto err_irq_request;
  228. mc13xxx_unlock(mc13xxx);
  229. priv->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  230. &mc13xxx_rtc_ops, THIS_MODULE);
  231. return 0;
  232. err_irq_request:
  233. mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_TODA, priv);
  234. mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_RTCRST, priv);
  235. mc13xxx_unlock(mc13xxx);
  236. return ret;
  237. }
  238. static int mc13xxx_rtc_remove(struct platform_device *pdev)
  239. {
  240. struct mc13xxx_rtc *priv = platform_get_drvdata(pdev);
  241. mc13xxx_lock(priv->mc13xxx);
  242. mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_TODA, priv);
  243. mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_RTCRST, priv);
  244. mc13xxx_unlock(priv->mc13xxx);
  245. return 0;
  246. }
  247. static const struct platform_device_id mc13xxx_rtc_idtable[] = {
  248. {
  249. .name = "mc13783-rtc",
  250. }, {
  251. .name = "mc13892-rtc",
  252. }, {
  253. .name = "mc34708-rtc",
  254. },
  255. { /* sentinel */ }
  256. };
  257. MODULE_DEVICE_TABLE(platform, mc13xxx_rtc_idtable);
  258. static struct platform_driver mc13xxx_rtc_driver = {
  259. .id_table = mc13xxx_rtc_idtable,
  260. .remove = mc13xxx_rtc_remove,
  261. .driver = {
  262. .name = DRIVER_NAME,
  263. },
  264. };
  265. module_platform_driver_probe(mc13xxx_rtc_driver, &mc13xxx_rtc_probe);
  266. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  267. MODULE_DESCRIPTION("RTC driver for Freescale MC13XXX PMIC");
  268. MODULE_LICENSE("GPL v2");