rtc-rk808.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * RTC driver for Rockchip RK808
  3. *
  4. * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
  5. *
  6. * Author: Chris Zhong <zyw@rock-chips.com>
  7. * Author: Zhang Qing <zhangqing@rock-chips.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms and conditions of the GNU General Public License,
  11. * version 2, as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/rtc.h>
  21. #include <linux/bcd.h>
  22. #include <linux/mfd/rk808.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/i2c.h>
  25. /* RTC_CTRL_REG bitfields */
  26. #define BIT_RTC_CTRL_REG_STOP_RTC_M BIT(0)
  27. /* RK808 has a shadowed register for saving a "frozen" RTC time.
  28. * When user setting "GET_TIME" to 1, the time will save in this shadowed
  29. * register. If set "READSEL" to 1, user read rtc time register, actually
  30. * get the time of that moment. If we need the real time, clr this bit.
  31. */
  32. #define BIT_RTC_CTRL_REG_RTC_GET_TIME BIT(6)
  33. #define BIT_RTC_CTRL_REG_RTC_READSEL_M BIT(7)
  34. #define BIT_RTC_INTERRUPTS_REG_IT_ALARM_M BIT(3)
  35. #define RTC_STATUS_MASK 0xFE
  36. #define SECONDS_REG_MSK 0x7F
  37. #define MINUTES_REG_MAK 0x7F
  38. #define HOURS_REG_MSK 0x3F
  39. #define DAYS_REG_MSK 0x3F
  40. #define MONTHS_REG_MSK 0x1F
  41. #define YEARS_REG_MSK 0xFF
  42. #define WEEKS_REG_MSK 0x7
  43. /* REG_SECONDS_REG through REG_YEARS_REG is how many registers? */
  44. #define NUM_TIME_REGS (RK808_WEEKS_REG - RK808_SECONDS_REG + 1)
  45. #define NUM_ALARM_REGS (RK808_ALARM_YEARS_REG - RK808_ALARM_SECONDS_REG + 1)
  46. struct rk808_rtc {
  47. struct rk808 *rk808;
  48. struct rtc_device *rtc;
  49. int irq;
  50. };
  51. /*
  52. * The Rockchip calendar used by the RK808 counts November with 31 days. We use
  53. * these translation functions to convert its dates to/from the Gregorian
  54. * calendar used by the rest of the world. We arbitrarily define Jan 1st, 2016
  55. * as the day when both calendars were in sync, and treat all other dates
  56. * relative to that.
  57. * NOTE: Other system software (e.g. firmware) that reads the same hardware must
  58. * implement this exact same conversion algorithm, with the same anchor date.
  59. */
  60. static time64_t nov2dec_transitions(struct rtc_time *tm)
  61. {
  62. return (tm->tm_year + 1900) - 2016 + (tm->tm_mon + 1 > 11 ? 1 : 0);
  63. }
  64. static void rockchip_to_gregorian(struct rtc_time *tm)
  65. {
  66. /* If it's Nov 31st, rtc_tm_to_time64() will count that like Dec 1st */
  67. time64_t time = rtc_tm_to_time64(tm);
  68. rtc_time64_to_tm(time + nov2dec_transitions(tm) * 86400, tm);
  69. }
  70. static void gregorian_to_rockchip(struct rtc_time *tm)
  71. {
  72. time64_t extra_days = nov2dec_transitions(tm);
  73. time64_t time = rtc_tm_to_time64(tm);
  74. rtc_time64_to_tm(time - extra_days * 86400, tm);
  75. /* Compensate if we went back over Nov 31st (will work up to 2381) */
  76. if (nov2dec_transitions(tm) < extra_days) {
  77. if (tm->tm_mon + 1 == 11)
  78. tm->tm_mday++; /* This may result in 31! */
  79. else
  80. rtc_time64_to_tm(time - (extra_days - 1) * 86400, tm);
  81. }
  82. }
  83. /* Read current time and date in RTC */
  84. static int rk808_rtc_readtime(struct device *dev, struct rtc_time *tm)
  85. {
  86. struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
  87. struct rk808 *rk808 = rk808_rtc->rk808;
  88. u8 rtc_data[NUM_TIME_REGS];
  89. int ret;
  90. /* Force an update of the shadowed registers right now */
  91. ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
  92. BIT_RTC_CTRL_REG_RTC_GET_TIME,
  93. BIT_RTC_CTRL_REG_RTC_GET_TIME);
  94. if (ret) {
  95. dev_err(dev, "Failed to update bits rtc_ctrl: %d\n", ret);
  96. return ret;
  97. }
  98. /*
  99. * After we set the GET_TIME bit, the rtc time can't be read
  100. * immediately. So we should wait up to 31.25 us, about one cycle of
  101. * 32khz. If we clear the GET_TIME bit here, the time of i2c transfer
  102. * certainly more than 31.25us: 16 * 2.5us at 400kHz bus frequency.
  103. */
  104. ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
  105. BIT_RTC_CTRL_REG_RTC_GET_TIME,
  106. 0);
  107. if (ret) {
  108. dev_err(dev, "Failed to update bits rtc_ctrl: %d\n", ret);
  109. return ret;
  110. }
  111. ret = regmap_bulk_read(rk808->regmap, RK808_SECONDS_REG,
  112. rtc_data, NUM_TIME_REGS);
  113. if (ret) {
  114. dev_err(dev, "Failed to bulk read rtc_data: %d\n", ret);
  115. return ret;
  116. }
  117. tm->tm_sec = bcd2bin(rtc_data[0] & SECONDS_REG_MSK);
  118. tm->tm_min = bcd2bin(rtc_data[1] & MINUTES_REG_MAK);
  119. tm->tm_hour = bcd2bin(rtc_data[2] & HOURS_REG_MSK);
  120. tm->tm_mday = bcd2bin(rtc_data[3] & DAYS_REG_MSK);
  121. tm->tm_mon = (bcd2bin(rtc_data[4] & MONTHS_REG_MSK)) - 1;
  122. tm->tm_year = (bcd2bin(rtc_data[5] & YEARS_REG_MSK)) + 100;
  123. tm->tm_wday = bcd2bin(rtc_data[6] & WEEKS_REG_MSK);
  124. rockchip_to_gregorian(tm);
  125. dev_dbg(dev, "RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n",
  126. 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
  127. tm->tm_wday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  128. return ret;
  129. }
  130. /* Set current time and date in RTC */
  131. static int rk808_rtc_set_time(struct device *dev, struct rtc_time *tm)
  132. {
  133. struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
  134. struct rk808 *rk808 = rk808_rtc->rk808;
  135. u8 rtc_data[NUM_TIME_REGS];
  136. int ret;
  137. dev_dbg(dev, "set RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n",
  138. 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
  139. tm->tm_wday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  140. gregorian_to_rockchip(tm);
  141. rtc_data[0] = bin2bcd(tm->tm_sec);
  142. rtc_data[1] = bin2bcd(tm->tm_min);
  143. rtc_data[2] = bin2bcd(tm->tm_hour);
  144. rtc_data[3] = bin2bcd(tm->tm_mday);
  145. rtc_data[4] = bin2bcd(tm->tm_mon + 1);
  146. rtc_data[5] = bin2bcd(tm->tm_year - 100);
  147. rtc_data[6] = bin2bcd(tm->tm_wday);
  148. /* Stop RTC while updating the RTC registers */
  149. ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
  150. BIT_RTC_CTRL_REG_STOP_RTC_M,
  151. BIT_RTC_CTRL_REG_STOP_RTC_M);
  152. if (ret) {
  153. dev_err(dev, "Failed to update RTC control: %d\n", ret);
  154. return ret;
  155. }
  156. ret = regmap_bulk_write(rk808->regmap, RK808_SECONDS_REG,
  157. rtc_data, NUM_TIME_REGS);
  158. if (ret) {
  159. dev_err(dev, "Failed to bull write rtc_data: %d\n", ret);
  160. return ret;
  161. }
  162. /* Start RTC again */
  163. ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
  164. BIT_RTC_CTRL_REG_STOP_RTC_M, 0);
  165. if (ret) {
  166. dev_err(dev, "Failed to update RTC control: %d\n", ret);
  167. return ret;
  168. }
  169. return 0;
  170. }
  171. /* Read alarm time and date in RTC */
  172. static int rk808_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
  173. {
  174. struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
  175. struct rk808 *rk808 = rk808_rtc->rk808;
  176. u8 alrm_data[NUM_ALARM_REGS];
  177. uint32_t int_reg;
  178. int ret;
  179. ret = regmap_bulk_read(rk808->regmap, RK808_ALARM_SECONDS_REG,
  180. alrm_data, NUM_ALARM_REGS);
  181. alrm->time.tm_sec = bcd2bin(alrm_data[0] & SECONDS_REG_MSK);
  182. alrm->time.tm_min = bcd2bin(alrm_data[1] & MINUTES_REG_MAK);
  183. alrm->time.tm_hour = bcd2bin(alrm_data[2] & HOURS_REG_MSK);
  184. alrm->time.tm_mday = bcd2bin(alrm_data[3] & DAYS_REG_MSK);
  185. alrm->time.tm_mon = (bcd2bin(alrm_data[4] & MONTHS_REG_MSK)) - 1;
  186. alrm->time.tm_year = (bcd2bin(alrm_data[5] & YEARS_REG_MSK)) + 100;
  187. rockchip_to_gregorian(&alrm->time);
  188. ret = regmap_read(rk808->regmap, RK808_RTC_INT_REG, &int_reg);
  189. if (ret) {
  190. dev_err(dev, "Failed to read RTC INT REG: %d\n", ret);
  191. return ret;
  192. }
  193. dev_dbg(dev, "alrm read RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n",
  194. 1900 + alrm->time.tm_year, alrm->time.tm_mon + 1,
  195. alrm->time.tm_mday, alrm->time.tm_wday, alrm->time.tm_hour,
  196. alrm->time.tm_min, alrm->time.tm_sec);
  197. alrm->enabled = (int_reg & BIT_RTC_INTERRUPTS_REG_IT_ALARM_M) ? 1 : 0;
  198. return 0;
  199. }
  200. static int rk808_rtc_stop_alarm(struct rk808_rtc *rk808_rtc)
  201. {
  202. struct rk808 *rk808 = rk808_rtc->rk808;
  203. int ret;
  204. ret = regmap_update_bits(rk808->regmap, RK808_RTC_INT_REG,
  205. BIT_RTC_INTERRUPTS_REG_IT_ALARM_M, 0);
  206. return ret;
  207. }
  208. static int rk808_rtc_start_alarm(struct rk808_rtc *rk808_rtc)
  209. {
  210. struct rk808 *rk808 = rk808_rtc->rk808;
  211. int ret;
  212. ret = regmap_update_bits(rk808->regmap, RK808_RTC_INT_REG,
  213. BIT_RTC_INTERRUPTS_REG_IT_ALARM_M,
  214. BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
  215. return ret;
  216. }
  217. static int rk808_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  218. {
  219. struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
  220. struct rk808 *rk808 = rk808_rtc->rk808;
  221. u8 alrm_data[NUM_ALARM_REGS];
  222. int ret;
  223. ret = rk808_rtc_stop_alarm(rk808_rtc);
  224. if (ret) {
  225. dev_err(dev, "Failed to stop alarm: %d\n", ret);
  226. return ret;
  227. }
  228. dev_dbg(dev, "alrm set RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n",
  229. 1900 + alrm->time.tm_year, alrm->time.tm_mon + 1,
  230. alrm->time.tm_mday, alrm->time.tm_wday, alrm->time.tm_hour,
  231. alrm->time.tm_min, alrm->time.tm_sec);
  232. gregorian_to_rockchip(&alrm->time);
  233. alrm_data[0] = bin2bcd(alrm->time.tm_sec);
  234. alrm_data[1] = bin2bcd(alrm->time.tm_min);
  235. alrm_data[2] = bin2bcd(alrm->time.tm_hour);
  236. alrm_data[3] = bin2bcd(alrm->time.tm_mday);
  237. alrm_data[4] = bin2bcd(alrm->time.tm_mon + 1);
  238. alrm_data[5] = bin2bcd(alrm->time.tm_year - 100);
  239. ret = regmap_bulk_write(rk808->regmap, RK808_ALARM_SECONDS_REG,
  240. alrm_data, NUM_ALARM_REGS);
  241. if (ret) {
  242. dev_err(dev, "Failed to bulk write: %d\n", ret);
  243. return ret;
  244. }
  245. if (alrm->enabled) {
  246. ret = rk808_rtc_start_alarm(rk808_rtc);
  247. if (ret) {
  248. dev_err(dev, "Failed to start alarm: %d\n", ret);
  249. return ret;
  250. }
  251. }
  252. return 0;
  253. }
  254. static int rk808_rtc_alarm_irq_enable(struct device *dev,
  255. unsigned int enabled)
  256. {
  257. struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
  258. if (enabled)
  259. return rk808_rtc_start_alarm(rk808_rtc);
  260. return rk808_rtc_stop_alarm(rk808_rtc);
  261. }
  262. /*
  263. * We will just handle setting the frequency and make use the framework for
  264. * reading the periodic interupts.
  265. *
  266. * @freq: Current periodic IRQ freq:
  267. * bit 0: every second
  268. * bit 1: every minute
  269. * bit 2: every hour
  270. * bit 3: every day
  271. */
  272. static irqreturn_t rk808_alarm_irq(int irq, void *data)
  273. {
  274. struct rk808_rtc *rk808_rtc = data;
  275. struct rk808 *rk808 = rk808_rtc->rk808;
  276. struct i2c_client *client = rk808->i2c;
  277. int ret;
  278. ret = regmap_write(rk808->regmap, RK808_RTC_STATUS_REG,
  279. RTC_STATUS_MASK);
  280. if (ret) {
  281. dev_err(&client->dev,
  282. "%s:Failed to update RTC status: %d\n", __func__, ret);
  283. return ret;
  284. }
  285. rtc_update_irq(rk808_rtc->rtc, 1, RTC_IRQF | RTC_AF);
  286. dev_dbg(&client->dev,
  287. "%s:irq=%d\n", __func__, irq);
  288. return IRQ_HANDLED;
  289. }
  290. static const struct rtc_class_ops rk808_rtc_ops = {
  291. .read_time = rk808_rtc_readtime,
  292. .set_time = rk808_rtc_set_time,
  293. .read_alarm = rk808_rtc_readalarm,
  294. .set_alarm = rk808_rtc_setalarm,
  295. .alarm_irq_enable = rk808_rtc_alarm_irq_enable,
  296. };
  297. #ifdef CONFIG_PM_SLEEP
  298. /* Turn off the alarm if it should not be a wake source. */
  299. static int rk808_rtc_suspend(struct device *dev)
  300. {
  301. struct platform_device *pdev = to_platform_device(dev);
  302. struct rk808_rtc *rk808_rtc = dev_get_drvdata(&pdev->dev);
  303. if (device_may_wakeup(dev))
  304. enable_irq_wake(rk808_rtc->irq);
  305. return 0;
  306. }
  307. /* Enable the alarm if it should be enabled (in case it was disabled to
  308. * prevent use as a wake source).
  309. */
  310. static int rk808_rtc_resume(struct device *dev)
  311. {
  312. struct platform_device *pdev = to_platform_device(dev);
  313. struct rk808_rtc *rk808_rtc = dev_get_drvdata(&pdev->dev);
  314. if (device_may_wakeup(dev))
  315. disable_irq_wake(rk808_rtc->irq);
  316. return 0;
  317. }
  318. #endif
  319. static SIMPLE_DEV_PM_OPS(rk808_rtc_pm_ops,
  320. rk808_rtc_suspend, rk808_rtc_resume);
  321. static int rk808_rtc_probe(struct platform_device *pdev)
  322. {
  323. struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
  324. struct rk808_rtc *rk808_rtc;
  325. struct rtc_time tm;
  326. int ret;
  327. rk808_rtc = devm_kzalloc(&pdev->dev, sizeof(*rk808_rtc), GFP_KERNEL);
  328. if (rk808_rtc == NULL)
  329. return -ENOMEM;
  330. platform_set_drvdata(pdev, rk808_rtc);
  331. rk808_rtc->rk808 = rk808;
  332. /* start rtc running by default, and use shadowed timer. */
  333. ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
  334. BIT_RTC_CTRL_REG_STOP_RTC_M |
  335. BIT_RTC_CTRL_REG_RTC_READSEL_M,
  336. BIT_RTC_CTRL_REG_RTC_READSEL_M);
  337. if (ret) {
  338. dev_err(&pdev->dev,
  339. "Failed to update RTC control: %d\n", ret);
  340. return ret;
  341. }
  342. ret = regmap_write(rk808->regmap, RK808_RTC_STATUS_REG,
  343. RTC_STATUS_MASK);
  344. if (ret) {
  345. dev_err(&pdev->dev,
  346. "Failed to write RTC status: %d\n", ret);
  347. return ret;
  348. }
  349. /* set init time */
  350. ret = rk808_rtc_readtime(&pdev->dev, &tm);
  351. if (ret) {
  352. dev_err(&pdev->dev, "Failed to read RTC time\n");
  353. return ret;
  354. }
  355. ret = rtc_valid_tm(&tm);
  356. if (ret)
  357. dev_warn(&pdev->dev, "invalid date/time\n");
  358. device_init_wakeup(&pdev->dev, 1);
  359. rk808_rtc->rtc = devm_rtc_device_register(&pdev->dev, "rk808-rtc",
  360. &rk808_rtc_ops, THIS_MODULE);
  361. if (IS_ERR(rk808_rtc->rtc)) {
  362. ret = PTR_ERR(rk808_rtc->rtc);
  363. return ret;
  364. }
  365. rk808_rtc->irq = platform_get_irq(pdev, 0);
  366. if (rk808_rtc->irq < 0) {
  367. if (rk808_rtc->irq != -EPROBE_DEFER)
  368. dev_err(&pdev->dev, "Wake up is not possible as irq = %d\n",
  369. rk808_rtc->irq);
  370. return rk808_rtc->irq;
  371. }
  372. /* request alarm irq of rk808 */
  373. ret = devm_request_threaded_irq(&pdev->dev, rk808_rtc->irq, NULL,
  374. rk808_alarm_irq, 0,
  375. "RTC alarm", rk808_rtc);
  376. if (ret) {
  377. dev_err(&pdev->dev, "Failed to request alarm IRQ %d: %d\n",
  378. rk808_rtc->irq, ret);
  379. }
  380. return ret;
  381. }
  382. static struct platform_driver rk808_rtc_driver = {
  383. .probe = rk808_rtc_probe,
  384. .driver = {
  385. .name = "rk808-rtc",
  386. .pm = &rk808_rtc_pm_ops,
  387. },
  388. };
  389. module_platform_driver(rk808_rtc_driver);
  390. MODULE_DESCRIPTION("RTC driver for the rk808 series PMICs");
  391. MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
  392. MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
  393. MODULE_LICENSE("GPL");
  394. MODULE_ALIAS("platform:rk808-rtc");