rtc-mc13xxx.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. struct mc13xxx_rtc {
  23. struct rtc_device *rtc;
  24. struct mc13xxx *mc13xxx;
  25. int valid;
  26. };
  27. static int mc13xxx_rtc_irq_enable_unlocked(struct device *dev,
  28. unsigned int enabled, int irq)
  29. {
  30. struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
  31. int (*func)(struct mc13xxx *mc13xxx, int irq);
  32. if (!priv->valid)
  33. return -ENODATA;
  34. func = enabled ? mc13xxx_irq_unmask : mc13xxx_irq_mask;
  35. return func(priv->mc13xxx, irq);
  36. }
  37. static int mc13xxx_rtc_irq_enable(struct device *dev,
  38. unsigned int enabled, int irq)
  39. {
  40. struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
  41. int ret;
  42. mc13xxx_lock(priv->mc13xxx);
  43. ret = mc13xxx_rtc_irq_enable_unlocked(dev, enabled, irq);
  44. mc13xxx_unlock(priv->mc13xxx);
  45. return ret;
  46. }
  47. static int mc13xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
  48. {
  49. struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
  50. unsigned int seconds, days1, days2;
  51. unsigned long s1970;
  52. int ret;
  53. mc13xxx_lock(priv->mc13xxx);
  54. if (!priv->valid) {
  55. ret = -ENODATA;
  56. goto out;
  57. }
  58. ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days1);
  59. if (unlikely(ret))
  60. goto out;
  61. ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTOD, &seconds);
  62. if (unlikely(ret))
  63. goto out;
  64. ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days2);
  65. out:
  66. mc13xxx_unlock(priv->mc13xxx);
  67. if (ret)
  68. return ret;
  69. if (days2 == days1 + 1) {
  70. if (seconds >= 86400 / 2)
  71. days2 = days1;
  72. else
  73. days1 = days2;
  74. }
  75. if (days1 != days2)
  76. return -EIO;
  77. s1970 = days1 * 86400 + seconds;
  78. rtc_time_to_tm(s1970, tm);
  79. return rtc_valid_tm(tm);
  80. }
  81. static int mc13xxx_rtc_set_mmss(struct device *dev, unsigned long secs)
  82. {
  83. struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
  84. unsigned int seconds, days;
  85. unsigned int alarmseconds;
  86. int ret;
  87. seconds = secs % 86400;
  88. days = secs / 86400;
  89. mc13xxx_lock(priv->mc13xxx);
  90. /*
  91. * temporarily invalidate alarm to prevent triggering it when the day is
  92. * already updated while the time isn't yet.
  93. */
  94. ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTODA, &alarmseconds);
  95. if (unlikely(ret))
  96. goto out;
  97. if (alarmseconds < 86400) {
  98. ret = mc13xxx_reg_write(priv->mc13xxx,
  99. MC13XXX_RTCTODA, 0x1ffff);
  100. if (unlikely(ret))
  101. goto out;
  102. }
  103. /*
  104. * write seconds=0 to prevent a day switch between writing days
  105. * and seconds below
  106. */
  107. ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTOD, 0);
  108. if (unlikely(ret))
  109. goto out;
  110. ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCDAY, days);
  111. if (unlikely(ret))
  112. goto out;
  113. ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTOD, seconds);
  114. if (unlikely(ret))
  115. goto out;
  116. /* restore alarm */
  117. if (alarmseconds < 86400) {
  118. ret = mc13xxx_reg_write(priv->mc13xxx,
  119. MC13XXX_RTCTODA, alarmseconds);
  120. if (unlikely(ret))
  121. goto out;
  122. }
  123. ret = mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_RTCRST);
  124. if (unlikely(ret))
  125. goto out;
  126. ret = mc13xxx_irq_unmask(priv->mc13xxx, MC13XXX_IRQ_RTCRST);
  127. out:
  128. priv->valid = !ret;
  129. mc13xxx_unlock(priv->mc13xxx);
  130. return ret;
  131. }
  132. static int mc13xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  133. {
  134. struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
  135. unsigned seconds, days;
  136. unsigned long s1970;
  137. int enabled, pending;
  138. int ret;
  139. mc13xxx_lock(priv->mc13xxx);
  140. ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTODA, &seconds);
  141. if (unlikely(ret))
  142. goto out;
  143. if (seconds >= 86400) {
  144. ret = -ENODATA;
  145. goto out;
  146. }
  147. ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days);
  148. if (unlikely(ret))
  149. goto out;
  150. ret = mc13xxx_irq_status(priv->mc13xxx, MC13XXX_IRQ_TODA,
  151. &enabled, &pending);
  152. out:
  153. mc13xxx_unlock(priv->mc13xxx);
  154. if (ret)
  155. return ret;
  156. alarm->enabled = enabled;
  157. alarm->pending = pending;
  158. s1970 = days * 86400 + seconds;
  159. rtc_time_to_tm(s1970, &alarm->time);
  160. dev_dbg(dev, "%s: %lu\n", __func__, s1970);
  161. return 0;
  162. }
  163. static int mc13xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  164. {
  165. struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
  166. unsigned long s1970;
  167. unsigned seconds, days;
  168. int ret;
  169. mc13xxx_lock(priv->mc13xxx);
  170. /* disable alarm to prevent false triggering */
  171. ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTODA, 0x1ffff);
  172. if (unlikely(ret))
  173. goto out;
  174. ret = mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_TODA);
  175. if (unlikely(ret))
  176. goto out;
  177. ret = rtc_tm_to_time(&alarm->time, &s1970);
  178. if (unlikely(ret))
  179. goto out;
  180. dev_dbg(dev, "%s: o%2.s %lu\n", __func__, alarm->enabled ? "n" : "ff",
  181. s1970);
  182. ret = mc13xxx_rtc_irq_enable_unlocked(dev, alarm->enabled,
  183. MC13XXX_IRQ_TODA);
  184. if (unlikely(ret))
  185. goto out;
  186. seconds = s1970 % 86400;
  187. days = s1970 / 86400;
  188. ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCDAYA, days);
  189. if (unlikely(ret))
  190. goto out;
  191. ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTODA, seconds);
  192. out:
  193. mc13xxx_unlock(priv->mc13xxx);
  194. return ret;
  195. }
  196. static irqreturn_t mc13xxx_rtc_alarm_handler(int irq, void *dev)
  197. {
  198. struct mc13xxx_rtc *priv = dev;
  199. struct mc13xxx *mc13xxx = priv->mc13xxx;
  200. dev_dbg(&priv->rtc->dev, "Alarm\n");
  201. rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_AF);
  202. mc13xxx_irq_ack(mc13xxx, irq);
  203. return IRQ_HANDLED;
  204. }
  205. static irqreturn_t mc13xxx_rtc_update_handler(int irq, void *dev)
  206. {
  207. struct mc13xxx_rtc *priv = dev;
  208. struct mc13xxx *mc13xxx = priv->mc13xxx;
  209. dev_dbg(&priv->rtc->dev, "1HZ\n");
  210. rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_UF);
  211. mc13xxx_irq_ack(mc13xxx, irq);
  212. return IRQ_HANDLED;
  213. }
  214. static int mc13xxx_rtc_alarm_irq_enable(struct device *dev,
  215. unsigned int enabled)
  216. {
  217. return mc13xxx_rtc_irq_enable(dev, enabled, MC13XXX_IRQ_TODA);
  218. }
  219. static const struct rtc_class_ops mc13xxx_rtc_ops = {
  220. .read_time = mc13xxx_rtc_read_time,
  221. .set_mmss = mc13xxx_rtc_set_mmss,
  222. .read_alarm = mc13xxx_rtc_read_alarm,
  223. .set_alarm = mc13xxx_rtc_set_alarm,
  224. .alarm_irq_enable = mc13xxx_rtc_alarm_irq_enable,
  225. };
  226. static irqreturn_t mc13xxx_rtc_reset_handler(int irq, void *dev)
  227. {
  228. struct mc13xxx_rtc *priv = dev;
  229. struct mc13xxx *mc13xxx = priv->mc13xxx;
  230. dev_dbg(&priv->rtc->dev, "RTCRST\n");
  231. priv->valid = 0;
  232. mc13xxx_irq_mask(mc13xxx, irq);
  233. return IRQ_HANDLED;
  234. }
  235. static int __init mc13xxx_rtc_probe(struct platform_device *pdev)
  236. {
  237. int ret;
  238. struct mc13xxx_rtc *priv;
  239. struct mc13xxx *mc13xxx;
  240. int rtcrst_pending;
  241. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  242. if (!priv)
  243. return -ENOMEM;
  244. mc13xxx = dev_get_drvdata(pdev->dev.parent);
  245. priv->mc13xxx = mc13xxx;
  246. platform_set_drvdata(pdev, priv);
  247. mc13xxx_lock(mc13xxx);
  248. ret = mc13xxx_irq_request(mc13xxx, MC13XXX_IRQ_RTCRST,
  249. mc13xxx_rtc_reset_handler, DRIVER_NAME, priv);
  250. if (ret)
  251. goto err_reset_irq_request;
  252. ret = mc13xxx_irq_status(mc13xxx, MC13XXX_IRQ_RTCRST,
  253. NULL, &rtcrst_pending);
  254. if (ret)
  255. goto err_reset_irq_status;
  256. priv->valid = !rtcrst_pending;
  257. ret = mc13xxx_irq_request_nounmask(mc13xxx, MC13XXX_IRQ_1HZ,
  258. mc13xxx_rtc_update_handler, DRIVER_NAME, priv);
  259. if (ret)
  260. goto err_update_irq_request;
  261. ret = mc13xxx_irq_request_nounmask(mc13xxx, MC13XXX_IRQ_TODA,
  262. mc13xxx_rtc_alarm_handler, DRIVER_NAME, priv);
  263. if (ret)
  264. goto err_alarm_irq_request;
  265. mc13xxx_unlock(mc13xxx);
  266. priv->rtc = rtc_device_register(pdev->name,
  267. &pdev->dev, &mc13xxx_rtc_ops, THIS_MODULE);
  268. if (IS_ERR(priv->rtc)) {
  269. ret = PTR_ERR(priv->rtc);
  270. mc13xxx_lock(mc13xxx);
  271. mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_TODA, priv);
  272. err_alarm_irq_request:
  273. mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_1HZ, priv);
  274. err_update_irq_request:
  275. err_reset_irq_status:
  276. mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_RTCRST, priv);
  277. err_reset_irq_request:
  278. mc13xxx_unlock(mc13xxx);
  279. platform_set_drvdata(pdev, NULL);
  280. kfree(priv);
  281. }
  282. return ret;
  283. }
  284. static int __exit mc13xxx_rtc_remove(struct platform_device *pdev)
  285. {
  286. struct mc13xxx_rtc *priv = platform_get_drvdata(pdev);
  287. mc13xxx_lock(priv->mc13xxx);
  288. rtc_device_unregister(priv->rtc);
  289. mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_TODA, priv);
  290. mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_1HZ, priv);
  291. mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_RTCRST, priv);
  292. mc13xxx_unlock(priv->mc13xxx);
  293. platform_set_drvdata(pdev, NULL);
  294. kfree(priv);
  295. return 0;
  296. }
  297. static const struct platform_device_id mc13xxx_rtc_idtable[] = {
  298. {
  299. .name = "mc13783-rtc",
  300. }, {
  301. .name = "mc13892-rtc",
  302. },
  303. { }
  304. };
  305. static struct platform_driver mc13xxx_rtc_driver = {
  306. .id_table = mc13xxx_rtc_idtable,
  307. .remove = __exit_p(mc13xxx_rtc_remove),
  308. .driver = {
  309. .name = DRIVER_NAME,
  310. .owner = THIS_MODULE,
  311. },
  312. };
  313. static int __init mc13xxx_rtc_init(void)
  314. {
  315. return platform_driver_probe(&mc13xxx_rtc_driver, &mc13xxx_rtc_probe);
  316. }
  317. module_init(mc13xxx_rtc_init);
  318. static void __exit mc13xxx_rtc_exit(void)
  319. {
  320. platform_driver_unregister(&mc13xxx_rtc_driver);
  321. }
  322. module_exit(mc13xxx_rtc_exit);
  323. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  324. MODULE_DESCRIPTION("RTC driver for Freescale MC13XXX PMIC");
  325. MODULE_LICENSE("GPL v2");
  326. MODULE_ALIAS("platform:" DRIVER_NAME);