rtc-rk808.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. /* Read current time and date in RTC */
  52. static int rk808_rtc_readtime(struct device *dev, struct rtc_time *tm)
  53. {
  54. struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
  55. struct rk808 *rk808 = rk808_rtc->rk808;
  56. u8 rtc_data[NUM_TIME_REGS];
  57. int ret;
  58. /* Force an update of the shadowed registers right now */
  59. ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
  60. BIT_RTC_CTRL_REG_RTC_GET_TIME,
  61. BIT_RTC_CTRL_REG_RTC_GET_TIME);
  62. if (ret) {
  63. dev_err(dev, "Failed to update bits rtc_ctrl: %d\n", ret);
  64. return ret;
  65. }
  66. /*
  67. * After we set the GET_TIME bit, the rtc time can't be read
  68. * immediately. So we should wait up to 31.25 us, about one cycle of
  69. * 32khz. If we clear the GET_TIME bit here, the time of i2c transfer
  70. * certainly more than 31.25us: 16 * 2.5us at 400kHz bus frequency.
  71. */
  72. ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
  73. BIT_RTC_CTRL_REG_RTC_GET_TIME,
  74. 0);
  75. if (ret) {
  76. dev_err(dev, "Failed to update bits rtc_ctrl: %d\n", ret);
  77. return ret;
  78. }
  79. ret = regmap_bulk_read(rk808->regmap, RK808_SECONDS_REG,
  80. rtc_data, NUM_TIME_REGS);
  81. if (ret) {
  82. dev_err(dev, "Failed to bulk read rtc_data: %d\n", ret);
  83. return ret;
  84. }
  85. tm->tm_sec = bcd2bin(rtc_data[0] & SECONDS_REG_MSK);
  86. tm->tm_min = bcd2bin(rtc_data[1] & MINUTES_REG_MAK);
  87. tm->tm_hour = bcd2bin(rtc_data[2] & HOURS_REG_MSK);
  88. tm->tm_mday = bcd2bin(rtc_data[3] & DAYS_REG_MSK);
  89. tm->tm_mon = (bcd2bin(rtc_data[4] & MONTHS_REG_MSK)) - 1;
  90. tm->tm_year = (bcd2bin(rtc_data[5] & YEARS_REG_MSK)) + 100;
  91. tm->tm_wday = bcd2bin(rtc_data[6] & WEEKS_REG_MSK);
  92. dev_dbg(dev, "RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n",
  93. 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
  94. tm->tm_wday, tm->tm_hour , tm->tm_min, tm->tm_sec);
  95. return ret;
  96. }
  97. /* Set current time and date in RTC */
  98. static int rk808_rtc_set_time(struct device *dev, struct rtc_time *tm)
  99. {
  100. struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
  101. struct rk808 *rk808 = rk808_rtc->rk808;
  102. u8 rtc_data[NUM_TIME_REGS];
  103. int ret;
  104. rtc_data[0] = bin2bcd(tm->tm_sec);
  105. rtc_data[1] = bin2bcd(tm->tm_min);
  106. rtc_data[2] = bin2bcd(tm->tm_hour);
  107. rtc_data[3] = bin2bcd(tm->tm_mday);
  108. rtc_data[4] = bin2bcd(tm->tm_mon + 1);
  109. rtc_data[5] = bin2bcd(tm->tm_year - 100);
  110. rtc_data[6] = bin2bcd(tm->tm_wday);
  111. dev_dbg(dev, "set RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n",
  112. 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
  113. tm->tm_wday, tm->tm_hour , tm->tm_min, tm->tm_sec);
  114. /* Stop RTC while updating the RTC registers */
  115. ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
  116. BIT_RTC_CTRL_REG_STOP_RTC_M,
  117. BIT_RTC_CTRL_REG_STOP_RTC_M);
  118. if (ret) {
  119. dev_err(dev, "Failed to update RTC control: %d\n", ret);
  120. return ret;
  121. }
  122. ret = regmap_bulk_write(rk808->regmap, RK808_SECONDS_REG,
  123. rtc_data, NUM_TIME_REGS);
  124. if (ret) {
  125. dev_err(dev, "Failed to bull write rtc_data: %d\n", ret);
  126. return ret;
  127. }
  128. /* Start RTC again */
  129. ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
  130. BIT_RTC_CTRL_REG_STOP_RTC_M, 0);
  131. if (ret) {
  132. dev_err(dev, "Failed to update RTC control: %d\n", ret);
  133. return ret;
  134. }
  135. return 0;
  136. }
  137. /* Read alarm time and date in RTC */
  138. static int rk808_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
  139. {
  140. struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
  141. struct rk808 *rk808 = rk808_rtc->rk808;
  142. u8 alrm_data[NUM_ALARM_REGS];
  143. uint32_t int_reg;
  144. int ret;
  145. ret = regmap_bulk_read(rk808->regmap, RK808_ALARM_SECONDS_REG,
  146. alrm_data, NUM_ALARM_REGS);
  147. alrm->time.tm_sec = bcd2bin(alrm_data[0] & SECONDS_REG_MSK);
  148. alrm->time.tm_min = bcd2bin(alrm_data[1] & MINUTES_REG_MAK);
  149. alrm->time.tm_hour = bcd2bin(alrm_data[2] & HOURS_REG_MSK);
  150. alrm->time.tm_mday = bcd2bin(alrm_data[3] & DAYS_REG_MSK);
  151. alrm->time.tm_mon = (bcd2bin(alrm_data[4] & MONTHS_REG_MSK)) - 1;
  152. alrm->time.tm_year = (bcd2bin(alrm_data[5] & YEARS_REG_MSK)) + 100;
  153. ret = regmap_read(rk808->regmap, RK808_RTC_INT_REG, &int_reg);
  154. if (ret) {
  155. dev_err(dev, "Failed to read RTC INT REG: %d\n", ret);
  156. return ret;
  157. }
  158. dev_dbg(dev, "alrm read RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n",
  159. 1900 + alrm->time.tm_year, alrm->time.tm_mon + 1,
  160. alrm->time.tm_mday, alrm->time.tm_wday, alrm->time.tm_hour,
  161. alrm->time.tm_min, alrm->time.tm_sec);
  162. alrm->enabled = (int_reg & BIT_RTC_INTERRUPTS_REG_IT_ALARM_M) ? 1 : 0;
  163. return 0;
  164. }
  165. static int rk808_rtc_stop_alarm(struct rk808_rtc *rk808_rtc)
  166. {
  167. struct rk808 *rk808 = rk808_rtc->rk808;
  168. int ret;
  169. ret = regmap_update_bits(rk808->regmap, RK808_RTC_INT_REG,
  170. BIT_RTC_INTERRUPTS_REG_IT_ALARM_M, 0);
  171. return ret;
  172. }
  173. static int rk808_rtc_start_alarm(struct rk808_rtc *rk808_rtc)
  174. {
  175. struct rk808 *rk808 = rk808_rtc->rk808;
  176. int ret;
  177. ret = regmap_update_bits(rk808->regmap, RK808_RTC_INT_REG,
  178. BIT_RTC_INTERRUPTS_REG_IT_ALARM_M,
  179. BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
  180. return ret;
  181. }
  182. static int rk808_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  183. {
  184. struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
  185. struct rk808 *rk808 = rk808_rtc->rk808;
  186. u8 alrm_data[NUM_ALARM_REGS];
  187. int ret;
  188. ret = rk808_rtc_stop_alarm(rk808_rtc);
  189. if (ret) {
  190. dev_err(dev, "Failed to stop alarm: %d\n", ret);
  191. return ret;
  192. }
  193. dev_dbg(dev, "alrm set 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_data[0] = bin2bcd(alrm->time.tm_sec);
  198. alrm_data[1] = bin2bcd(alrm->time.tm_min);
  199. alrm_data[2] = bin2bcd(alrm->time.tm_hour);
  200. alrm_data[3] = bin2bcd(alrm->time.tm_mday);
  201. alrm_data[4] = bin2bcd(alrm->time.tm_mon + 1);
  202. alrm_data[5] = bin2bcd(alrm->time.tm_year - 100);
  203. ret = regmap_bulk_write(rk808->regmap, RK808_ALARM_SECONDS_REG,
  204. alrm_data, NUM_ALARM_REGS);
  205. if (ret) {
  206. dev_err(dev, "Failed to bulk write: %d\n", ret);
  207. return ret;
  208. }
  209. if (alrm->enabled) {
  210. ret = rk808_rtc_start_alarm(rk808_rtc);
  211. if (ret) {
  212. dev_err(dev, "Failed to start alarm: %d\n", ret);
  213. return ret;
  214. }
  215. }
  216. return 0;
  217. }
  218. static int rk808_rtc_alarm_irq_enable(struct device *dev,
  219. unsigned int enabled)
  220. {
  221. struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
  222. if (enabled)
  223. return rk808_rtc_start_alarm(rk808_rtc);
  224. return rk808_rtc_stop_alarm(rk808_rtc);
  225. }
  226. /*
  227. * We will just handle setting the frequency and make use the framework for
  228. * reading the periodic interupts.
  229. *
  230. * @freq: Current periodic IRQ freq:
  231. * bit 0: every second
  232. * bit 1: every minute
  233. * bit 2: every hour
  234. * bit 3: every day
  235. */
  236. static irqreturn_t rk808_alarm_irq(int irq, void *data)
  237. {
  238. struct rk808_rtc *rk808_rtc = data;
  239. struct rk808 *rk808 = rk808_rtc->rk808;
  240. struct i2c_client *client = rk808->i2c;
  241. int ret;
  242. ret = regmap_write(rk808->regmap, RK808_RTC_STATUS_REG,
  243. RTC_STATUS_MASK);
  244. if (ret) {
  245. dev_err(&client->dev,
  246. "%s:Failed to update RTC status: %d\n", __func__, ret);
  247. return ret;
  248. }
  249. rtc_update_irq(rk808_rtc->rtc, 1, RTC_IRQF | RTC_AF);
  250. dev_dbg(&client->dev,
  251. "%s:irq=%d\n", __func__, irq);
  252. return IRQ_HANDLED;
  253. }
  254. static const struct rtc_class_ops rk808_rtc_ops = {
  255. .read_time = rk808_rtc_readtime,
  256. .set_time = rk808_rtc_set_time,
  257. .read_alarm = rk808_rtc_readalarm,
  258. .set_alarm = rk808_rtc_setalarm,
  259. .alarm_irq_enable = rk808_rtc_alarm_irq_enable,
  260. };
  261. #ifdef CONFIG_PM_SLEEP
  262. /* Turn off the alarm if it should not be a wake source. */
  263. static int rk808_rtc_suspend(struct device *dev)
  264. {
  265. struct platform_device *pdev = to_platform_device(dev);
  266. struct rk808_rtc *rk808_rtc = dev_get_drvdata(&pdev->dev);
  267. if (device_may_wakeup(dev))
  268. enable_irq_wake(rk808_rtc->irq);
  269. return 0;
  270. }
  271. /* Enable the alarm if it should be enabled (in case it was disabled to
  272. * prevent use as a wake source).
  273. */
  274. static int rk808_rtc_resume(struct device *dev)
  275. {
  276. struct platform_device *pdev = to_platform_device(dev);
  277. struct rk808_rtc *rk808_rtc = dev_get_drvdata(&pdev->dev);
  278. if (device_may_wakeup(dev))
  279. disable_irq_wake(rk808_rtc->irq);
  280. return 0;
  281. }
  282. #endif
  283. static SIMPLE_DEV_PM_OPS(rk808_rtc_pm_ops,
  284. rk808_rtc_suspend, rk808_rtc_resume);
  285. static int rk808_rtc_probe(struct platform_device *pdev)
  286. {
  287. struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
  288. struct rk808_rtc *rk808_rtc;
  289. struct rtc_time tm;
  290. int ret;
  291. rk808_rtc = devm_kzalloc(&pdev->dev, sizeof(*rk808_rtc), GFP_KERNEL);
  292. if (rk808_rtc == NULL)
  293. return -ENOMEM;
  294. platform_set_drvdata(pdev, rk808_rtc);
  295. rk808_rtc->rk808 = rk808;
  296. /* start rtc running by default, and use shadowed timer. */
  297. ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
  298. BIT_RTC_CTRL_REG_STOP_RTC_M |
  299. BIT_RTC_CTRL_REG_RTC_READSEL_M,
  300. BIT_RTC_CTRL_REG_RTC_READSEL_M);
  301. if (ret) {
  302. dev_err(&pdev->dev,
  303. "Failed to update RTC control: %d\n", ret);
  304. return ret;
  305. }
  306. ret = regmap_write(rk808->regmap, RK808_RTC_STATUS_REG,
  307. RTC_STATUS_MASK);
  308. if (ret) {
  309. dev_err(&pdev->dev,
  310. "Failed to write RTC status: %d\n", ret);
  311. return ret;
  312. }
  313. /* set init time */
  314. ret = rk808_rtc_readtime(&pdev->dev, &tm);
  315. if (ret) {
  316. dev_err(&pdev->dev, "Failed to read RTC time\n");
  317. return ret;
  318. }
  319. ret = rtc_valid_tm(&tm);
  320. if (ret)
  321. dev_warn(&pdev->dev, "invalid date/time\n");
  322. device_init_wakeup(&pdev->dev, 1);
  323. rk808_rtc->rtc = devm_rtc_device_register(&pdev->dev, "rk808-rtc",
  324. &rk808_rtc_ops, THIS_MODULE);
  325. if (IS_ERR(rk808_rtc->rtc)) {
  326. ret = PTR_ERR(rk808_rtc->rtc);
  327. return ret;
  328. }
  329. rk808_rtc->irq = platform_get_irq(pdev, 0);
  330. if (rk808_rtc->irq < 0) {
  331. if (rk808_rtc->irq != -EPROBE_DEFER)
  332. dev_err(&pdev->dev, "Wake up is not possible as irq = %d\n",
  333. rk808_rtc->irq);
  334. return rk808_rtc->irq;
  335. }
  336. /* request alarm irq of rk808 */
  337. ret = devm_request_threaded_irq(&pdev->dev, rk808_rtc->irq, NULL,
  338. rk808_alarm_irq, 0,
  339. "RTC alarm", rk808_rtc);
  340. if (ret) {
  341. dev_err(&pdev->dev, "Failed to request alarm IRQ %d: %d\n",
  342. rk808_rtc->irq, ret);
  343. }
  344. return ret;
  345. }
  346. static struct platform_driver rk808_rtc_driver = {
  347. .probe = rk808_rtc_probe,
  348. .driver = {
  349. .name = "rk808-rtc",
  350. .pm = &rk808_rtc_pm_ops,
  351. },
  352. };
  353. module_platform_driver(rk808_rtc_driver);
  354. MODULE_DESCRIPTION("RTC driver for the rk808 series PMICs");
  355. MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
  356. MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
  357. MODULE_LICENSE("GPL");
  358. MODULE_ALIAS("platform:rk808-rtc");