rtc-mc13xxx.c 8.3 KB

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