rtc-wm8350.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * Real Time Clock driver for Wolfson Microelectronics WM8350
  3. *
  4. * Copyright (C) 2007, 2008 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Liam Girdwood
  7. * linux@wolfsonmicro.com
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/time.h>
  18. #include <linux/rtc.h>
  19. #include <linux/bcd.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/ioctl.h>
  22. #include <linux/completion.h>
  23. #include <linux/mfd/wm8350/rtc.h>
  24. #include <linux/mfd/wm8350/core.h>
  25. #include <linux/delay.h>
  26. #include <linux/platform_device.h>
  27. #define WM8350_SET_ALM_RETRIES 5
  28. #define WM8350_SET_TIME_RETRIES 5
  29. #define WM8350_GET_TIME_RETRIES 5
  30. #define to_wm8350_from_rtc_dev(d) container_of(d, struct wm8350, rtc.pdev.dev)
  31. /*
  32. * Read current time and date in RTC
  33. */
  34. static int wm8350_rtc_readtime(struct device *dev, struct rtc_time *tm)
  35. {
  36. struct wm8350 *wm8350 = dev_get_drvdata(dev);
  37. u16 time1[4], time2[4];
  38. int retries = WM8350_GET_TIME_RETRIES, ret;
  39. /*
  40. * Read the time twice and compare.
  41. * If time1 == time2, then time is valid else retry.
  42. */
  43. do {
  44. ret = wm8350_block_read(wm8350, WM8350_RTC_SECONDS_MINUTES,
  45. 4, time1);
  46. if (ret < 0)
  47. return ret;
  48. ret = wm8350_block_read(wm8350, WM8350_RTC_SECONDS_MINUTES,
  49. 4, time2);
  50. if (ret < 0)
  51. return ret;
  52. if (memcmp(time1, time2, sizeof(time1)) == 0) {
  53. tm->tm_sec = time1[0] & WM8350_RTC_SECS_MASK;
  54. tm->tm_min = (time1[0] & WM8350_RTC_MINS_MASK)
  55. >> WM8350_RTC_MINS_SHIFT;
  56. tm->tm_hour = time1[1] & WM8350_RTC_HRS_MASK;
  57. tm->tm_wday = ((time1[1] >> WM8350_RTC_DAY_SHIFT)
  58. & 0x7) - 1;
  59. tm->tm_mon = ((time1[2] & WM8350_RTC_MTH_MASK)
  60. >> WM8350_RTC_MTH_SHIFT) - 1;
  61. tm->tm_mday = (time1[2] & WM8350_RTC_DATE_MASK);
  62. tm->tm_year = ((time1[3] & WM8350_RTC_YHUNDREDS_MASK)
  63. >> WM8350_RTC_YHUNDREDS_SHIFT) * 100;
  64. tm->tm_year += time1[3] & WM8350_RTC_YUNITS_MASK;
  65. tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon,
  66. tm->tm_year);
  67. tm->tm_year -= 1900;
  68. dev_dbg(dev, "Read (%d left): %04x %04x %04x %04x\n",
  69. retries,
  70. time1[0], time1[1], time1[2], time1[3]);
  71. return 0;
  72. }
  73. } while (retries--);
  74. dev_err(dev, "timed out reading RTC time\n");
  75. return -EIO;
  76. }
  77. /*
  78. * Set current time and date in RTC
  79. */
  80. static int wm8350_rtc_settime(struct device *dev, struct rtc_time *tm)
  81. {
  82. struct wm8350 *wm8350 = dev_get_drvdata(dev);
  83. u16 time[4];
  84. u16 rtc_ctrl;
  85. int ret, retries = WM8350_SET_TIME_RETRIES;
  86. time[0] = tm->tm_sec;
  87. time[0] |= tm->tm_min << WM8350_RTC_MINS_SHIFT;
  88. time[1] = tm->tm_hour;
  89. time[1] |= (tm->tm_wday + 1) << WM8350_RTC_DAY_SHIFT;
  90. time[2] = tm->tm_mday;
  91. time[2] |= (tm->tm_mon + 1) << WM8350_RTC_MTH_SHIFT;
  92. time[3] = ((tm->tm_year + 1900) / 100) << WM8350_RTC_YHUNDREDS_SHIFT;
  93. time[3] |= (tm->tm_year + 1900) % 100;
  94. dev_dbg(dev, "Setting: %04x %04x %04x %04x\n",
  95. time[0], time[1], time[2], time[3]);
  96. /* Set RTC_SET to stop the clock */
  97. ret = wm8350_set_bits(wm8350, WM8350_RTC_TIME_CONTROL, WM8350_RTC_SET);
  98. if (ret < 0)
  99. return ret;
  100. /* Wait until confirmation of stopping */
  101. do {
  102. rtc_ctrl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
  103. schedule_timeout_uninterruptible(msecs_to_jiffies(1));
  104. } while (--retries && !(rtc_ctrl & WM8350_RTC_STS));
  105. if (!retries) {
  106. dev_err(dev, "timed out on set confirmation\n");
  107. return -EIO;
  108. }
  109. /* Write time to RTC */
  110. ret = wm8350_block_write(wm8350, WM8350_RTC_SECONDS_MINUTES, 4, time);
  111. if (ret < 0)
  112. return ret;
  113. /* Clear RTC_SET to start the clock */
  114. ret = wm8350_clear_bits(wm8350, WM8350_RTC_TIME_CONTROL,
  115. WM8350_RTC_SET);
  116. return ret;
  117. }
  118. /*
  119. * Read alarm time and date in RTC
  120. */
  121. static int wm8350_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
  122. {
  123. struct wm8350 *wm8350 = dev_get_drvdata(dev);
  124. struct rtc_time *tm = &alrm->time;
  125. u16 time[4];
  126. int ret;
  127. ret = wm8350_block_read(wm8350, WM8350_ALARM_SECONDS_MINUTES, 4, time);
  128. if (ret < 0)
  129. return ret;
  130. tm->tm_sec = time[0] & WM8350_RTC_ALMSECS_MASK;
  131. if (tm->tm_sec == WM8350_RTC_ALMSECS_MASK)
  132. tm->tm_sec = -1;
  133. tm->tm_min = time[0] & WM8350_RTC_ALMMINS_MASK;
  134. if (tm->tm_min == WM8350_RTC_ALMMINS_MASK)
  135. tm->tm_min = -1;
  136. else
  137. tm->tm_min >>= WM8350_RTC_ALMMINS_SHIFT;
  138. tm->tm_hour = time[1] & WM8350_RTC_ALMHRS_MASK;
  139. if (tm->tm_hour == WM8350_RTC_ALMHRS_MASK)
  140. tm->tm_hour = -1;
  141. tm->tm_wday = ((time[1] >> WM8350_RTC_ALMDAY_SHIFT) & 0x7) - 1;
  142. if (tm->tm_wday > 7)
  143. tm->tm_wday = -1;
  144. tm->tm_mon = time[2] & WM8350_RTC_ALMMTH_MASK;
  145. if (tm->tm_mon == WM8350_RTC_ALMMTH_MASK)
  146. tm->tm_mon = -1;
  147. else
  148. tm->tm_mon = (tm->tm_mon >> WM8350_RTC_ALMMTH_SHIFT) - 1;
  149. tm->tm_mday = (time[2] & WM8350_RTC_ALMDATE_MASK);
  150. if (tm->tm_mday == WM8350_RTC_ALMDATE_MASK)
  151. tm->tm_mday = -1;
  152. tm->tm_year = -1;
  153. alrm->enabled = !(time[3] & WM8350_RTC_ALMSTS);
  154. return 0;
  155. }
  156. static int wm8350_rtc_stop_alarm(struct wm8350 *wm8350)
  157. {
  158. int retries = WM8350_SET_ALM_RETRIES;
  159. u16 rtc_ctrl;
  160. int ret;
  161. /* Set RTC_SET to stop the clock */
  162. ret = wm8350_set_bits(wm8350, WM8350_RTC_TIME_CONTROL,
  163. WM8350_RTC_ALMSET);
  164. if (ret < 0)
  165. return ret;
  166. /* Wait until confirmation of stopping */
  167. do {
  168. rtc_ctrl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
  169. schedule_timeout_uninterruptible(msecs_to_jiffies(1));
  170. } while (retries-- && !(rtc_ctrl & WM8350_RTC_ALMSTS));
  171. if (!(rtc_ctrl & WM8350_RTC_ALMSTS))
  172. return -ETIMEDOUT;
  173. return 0;
  174. }
  175. static int wm8350_rtc_start_alarm(struct wm8350 *wm8350)
  176. {
  177. int ret;
  178. int retries = WM8350_SET_ALM_RETRIES;
  179. u16 rtc_ctrl;
  180. ret = wm8350_clear_bits(wm8350, WM8350_RTC_TIME_CONTROL,
  181. WM8350_RTC_ALMSET);
  182. if (ret < 0)
  183. return ret;
  184. /* Wait until confirmation */
  185. do {
  186. rtc_ctrl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
  187. schedule_timeout_uninterruptible(msecs_to_jiffies(1));
  188. } while (retries-- && rtc_ctrl & WM8350_RTC_ALMSTS);
  189. if (rtc_ctrl & WM8350_RTC_ALMSTS)
  190. return -ETIMEDOUT;
  191. return 0;
  192. }
  193. static int wm8350_rtc_alarm_irq_enable(struct device *dev,
  194. unsigned int enabled)
  195. {
  196. struct wm8350 *wm8350 = dev_get_drvdata(dev);
  197. if (enabled)
  198. return wm8350_rtc_start_alarm(wm8350);
  199. else
  200. return wm8350_rtc_stop_alarm(wm8350);
  201. }
  202. static int wm8350_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  203. {
  204. struct wm8350 *wm8350 = dev_get_drvdata(dev);
  205. struct rtc_time *tm = &alrm->time;
  206. u16 time[3];
  207. int ret;
  208. memset(time, 0, sizeof(time));
  209. if (tm->tm_sec != -1)
  210. time[0] |= tm->tm_sec;
  211. else
  212. time[0] |= WM8350_RTC_ALMSECS_MASK;
  213. if (tm->tm_min != -1)
  214. time[0] |= tm->tm_min << WM8350_RTC_ALMMINS_SHIFT;
  215. else
  216. time[0] |= WM8350_RTC_ALMMINS_MASK;
  217. if (tm->tm_hour != -1)
  218. time[1] |= tm->tm_hour;
  219. else
  220. time[1] |= WM8350_RTC_ALMHRS_MASK;
  221. if (tm->tm_wday != -1)
  222. time[1] |= (tm->tm_wday + 1) << WM8350_RTC_ALMDAY_SHIFT;
  223. else
  224. time[1] |= WM8350_RTC_ALMDAY_MASK;
  225. if (tm->tm_mday != -1)
  226. time[2] |= tm->tm_mday;
  227. else
  228. time[2] |= WM8350_RTC_ALMDATE_MASK;
  229. if (tm->tm_mon != -1)
  230. time[2] |= (tm->tm_mon + 1) << WM8350_RTC_ALMMTH_SHIFT;
  231. else
  232. time[2] |= WM8350_RTC_ALMMTH_MASK;
  233. ret = wm8350_rtc_stop_alarm(wm8350);
  234. if (ret < 0)
  235. return ret;
  236. /* Write time to RTC */
  237. ret = wm8350_block_write(wm8350, WM8350_ALARM_SECONDS_MINUTES,
  238. 3, time);
  239. if (ret < 0)
  240. return ret;
  241. if (alrm->enabled)
  242. ret = wm8350_rtc_start_alarm(wm8350);
  243. return ret;
  244. }
  245. static irqreturn_t wm8350_rtc_alarm_handler(int irq, void *data)
  246. {
  247. struct wm8350 *wm8350 = data;
  248. struct rtc_device *rtc = wm8350->rtc.rtc;
  249. int ret;
  250. rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
  251. /* Make it one shot */
  252. ret = wm8350_set_bits(wm8350, WM8350_RTC_TIME_CONTROL,
  253. WM8350_RTC_ALMSET);
  254. if (ret != 0) {
  255. dev_err(&(wm8350->rtc.pdev->dev),
  256. "Failed to disable alarm: %d\n", ret);
  257. }
  258. return IRQ_HANDLED;
  259. }
  260. static irqreturn_t wm8350_rtc_update_handler(int irq, void *data)
  261. {
  262. struct wm8350 *wm8350 = data;
  263. struct rtc_device *rtc = wm8350->rtc.rtc;
  264. rtc_update_irq(rtc, 1, RTC_IRQF | RTC_UF);
  265. return IRQ_HANDLED;
  266. }
  267. static const struct rtc_class_ops wm8350_rtc_ops = {
  268. .read_time = wm8350_rtc_readtime,
  269. .set_time = wm8350_rtc_settime,
  270. .read_alarm = wm8350_rtc_readalarm,
  271. .set_alarm = wm8350_rtc_setalarm,
  272. .alarm_irq_enable = wm8350_rtc_alarm_irq_enable,
  273. };
  274. #ifdef CONFIG_PM_SLEEP
  275. static int wm8350_rtc_suspend(struct device *dev)
  276. {
  277. struct platform_device *pdev = to_platform_device(dev);
  278. struct wm8350 *wm8350 = dev_get_drvdata(&pdev->dev);
  279. int ret = 0;
  280. u16 reg;
  281. reg = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
  282. if (device_may_wakeup(&wm8350->rtc.pdev->dev) &&
  283. reg & WM8350_RTC_ALMSTS) {
  284. ret = wm8350_rtc_stop_alarm(wm8350);
  285. if (ret != 0)
  286. dev_err(&pdev->dev, "Failed to stop RTC alarm: %d\n",
  287. ret);
  288. }
  289. return ret;
  290. }
  291. static int wm8350_rtc_resume(struct device *dev)
  292. {
  293. struct platform_device *pdev = to_platform_device(dev);
  294. struct wm8350 *wm8350 = dev_get_drvdata(&pdev->dev);
  295. int ret;
  296. if (wm8350->rtc.alarm_enabled) {
  297. ret = wm8350_rtc_start_alarm(wm8350);
  298. if (ret != 0)
  299. dev_err(&pdev->dev,
  300. "Failed to restart RTC alarm: %d\n", ret);
  301. }
  302. return 0;
  303. }
  304. #endif
  305. static int wm8350_rtc_probe(struct platform_device *pdev)
  306. {
  307. struct wm8350 *wm8350 = platform_get_drvdata(pdev);
  308. struct wm8350_rtc *wm_rtc = &wm8350->rtc;
  309. int ret = 0;
  310. u16 timectl, power5;
  311. timectl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
  312. if (timectl & WM8350_RTC_BCD) {
  313. dev_err(&pdev->dev, "RTC BCD mode not supported\n");
  314. return -EINVAL;
  315. }
  316. if (timectl & WM8350_RTC_12HR) {
  317. dev_err(&pdev->dev, "RTC 12 hour mode not supported\n");
  318. return -EINVAL;
  319. }
  320. /* enable the RTC if it's not already enabled */
  321. power5 = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_5);
  322. if (!(power5 & WM8350_RTC_TICK_ENA)) {
  323. dev_info(wm8350->dev, "Starting RTC\n");
  324. wm8350_reg_unlock(wm8350);
  325. ret = wm8350_set_bits(wm8350, WM8350_POWER_MGMT_5,
  326. WM8350_RTC_TICK_ENA);
  327. if (ret < 0) {
  328. dev_err(&pdev->dev, "failed to enable RTC: %d\n", ret);
  329. return ret;
  330. }
  331. wm8350_reg_lock(wm8350);
  332. }
  333. if (timectl & WM8350_RTC_STS) {
  334. int retries;
  335. ret = wm8350_clear_bits(wm8350, WM8350_RTC_TIME_CONTROL,
  336. WM8350_RTC_SET);
  337. if (ret < 0) {
  338. dev_err(&pdev->dev, "failed to start: %d\n", ret);
  339. return ret;
  340. }
  341. retries = WM8350_SET_TIME_RETRIES;
  342. do {
  343. timectl = wm8350_reg_read(wm8350,
  344. WM8350_RTC_TIME_CONTROL);
  345. } while (timectl & WM8350_RTC_STS && --retries);
  346. if (retries == 0) {
  347. dev_err(&pdev->dev, "failed to start: timeout\n");
  348. return -ENODEV;
  349. }
  350. }
  351. device_init_wakeup(&pdev->dev, 1);
  352. wm_rtc->rtc = devm_rtc_device_register(&pdev->dev, "wm8350",
  353. &wm8350_rtc_ops, THIS_MODULE);
  354. if (IS_ERR(wm_rtc->rtc)) {
  355. ret = PTR_ERR(wm_rtc->rtc);
  356. dev_err(&pdev->dev, "failed to register RTC: %d\n", ret);
  357. return ret;
  358. }
  359. wm8350_register_irq(wm8350, WM8350_IRQ_RTC_SEC,
  360. wm8350_rtc_update_handler, 0,
  361. "RTC Seconds", wm8350);
  362. wm8350_mask_irq(wm8350, WM8350_IRQ_RTC_SEC);
  363. wm8350_register_irq(wm8350, WM8350_IRQ_RTC_ALM,
  364. wm8350_rtc_alarm_handler, 0,
  365. "RTC Alarm", wm8350);
  366. return 0;
  367. }
  368. static int wm8350_rtc_remove(struct platform_device *pdev)
  369. {
  370. struct wm8350 *wm8350 = platform_get_drvdata(pdev);
  371. wm8350_free_irq(wm8350, WM8350_IRQ_RTC_SEC, wm8350);
  372. wm8350_free_irq(wm8350, WM8350_IRQ_RTC_ALM, wm8350);
  373. return 0;
  374. }
  375. static SIMPLE_DEV_PM_OPS(wm8350_rtc_pm_ops, wm8350_rtc_suspend,
  376. wm8350_rtc_resume);
  377. static struct platform_driver wm8350_rtc_driver = {
  378. .probe = wm8350_rtc_probe,
  379. .remove = wm8350_rtc_remove,
  380. .driver = {
  381. .name = "wm8350-rtc",
  382. .pm = &wm8350_rtc_pm_ops,
  383. },
  384. };
  385. module_platform_driver(wm8350_rtc_driver);
  386. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  387. MODULE_DESCRIPTION("RTC driver for the WM8350");
  388. MODULE_LICENSE("GPL");
  389. MODULE_ALIAS("platform:wm8350-rtc");