rtc-wm8350.c 12 KB

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