rtc-da9063.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /* rtc-da9063.c - Real time clock device driver for DA9063
  2. * Copyright (C) 2013-2015 Dialog Semiconductor Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/rtc.h>
  23. #include <linux/slab.h>
  24. #include <linux/mfd/da9062/registers.h>
  25. #include <linux/mfd/da9063/registers.h>
  26. #include <linux/mfd/da9063/core.h>
  27. #define YEARS_TO_DA9063(year) ((year) - 100)
  28. #define MONTHS_TO_DA9063(month) ((month) + 1)
  29. #define YEARS_FROM_DA9063(year) ((year) + 100)
  30. #define MONTHS_FROM_DA9063(month) ((month) - 1)
  31. enum {
  32. RTC_SEC = 0,
  33. RTC_MIN = 1,
  34. RTC_HOUR = 2,
  35. RTC_DAY = 3,
  36. RTC_MONTH = 4,
  37. RTC_YEAR = 5,
  38. RTC_DATA_LEN
  39. };
  40. struct da9063_compatible_rtc_regmap {
  41. /* REGS */
  42. int rtc_enable_reg;
  43. int rtc_enable_32k_crystal_reg;
  44. int rtc_alarm_secs_reg;
  45. int rtc_alarm_year_reg;
  46. int rtc_count_secs_reg;
  47. int rtc_count_year_reg;
  48. int rtc_event_reg;
  49. /* MASKS */
  50. int rtc_enable_mask;
  51. int rtc_crystal_mask;
  52. int rtc_event_alarm_mask;
  53. int rtc_alarm_on_mask;
  54. int rtc_alarm_status_mask;
  55. int rtc_tick_on_mask;
  56. int rtc_ready_to_read_mask;
  57. int rtc_count_sec_mask;
  58. int rtc_count_min_mask;
  59. int rtc_count_hour_mask;
  60. int rtc_count_day_mask;
  61. int rtc_count_month_mask;
  62. int rtc_count_year_mask;
  63. /* ALARM CONFIG */
  64. int rtc_data_start;
  65. int rtc_alarm_len;
  66. };
  67. struct da9063_compatible_rtc {
  68. struct rtc_device *rtc_dev;
  69. struct rtc_time alarm_time;
  70. struct regmap *regmap;
  71. const struct da9063_compatible_rtc_regmap *config;
  72. bool rtc_sync;
  73. };
  74. static const struct da9063_compatible_rtc_regmap da9063_ad_regs = {
  75. /* REGS */
  76. .rtc_enable_reg = DA9063_REG_CONTROL_E,
  77. .rtc_alarm_secs_reg = DA9063_AD_REG_ALARM_MI,
  78. .rtc_alarm_year_reg = DA9063_AD_REG_ALARM_Y,
  79. .rtc_count_secs_reg = DA9063_REG_COUNT_S,
  80. .rtc_count_year_reg = DA9063_REG_COUNT_Y,
  81. .rtc_event_reg = DA9063_REG_EVENT_A,
  82. /* MASKS */
  83. .rtc_enable_mask = DA9063_RTC_EN,
  84. .rtc_crystal_mask = DA9063_CRYSTAL,
  85. .rtc_enable_32k_crystal_reg = DA9063_REG_EN_32K,
  86. .rtc_event_alarm_mask = DA9063_E_ALARM,
  87. .rtc_alarm_on_mask = DA9063_ALARM_ON,
  88. .rtc_alarm_status_mask = DA9063_ALARM_STATUS_ALARM |
  89. DA9063_ALARM_STATUS_TICK,
  90. .rtc_tick_on_mask = DA9063_TICK_ON,
  91. .rtc_ready_to_read_mask = DA9063_RTC_READ,
  92. .rtc_count_sec_mask = DA9063_COUNT_SEC_MASK,
  93. .rtc_count_min_mask = DA9063_COUNT_MIN_MASK,
  94. .rtc_count_hour_mask = DA9063_COUNT_HOUR_MASK,
  95. .rtc_count_day_mask = DA9063_COUNT_DAY_MASK,
  96. .rtc_count_month_mask = DA9063_COUNT_MONTH_MASK,
  97. .rtc_count_year_mask = DA9063_COUNT_YEAR_MASK,
  98. /* ALARM CONFIG */
  99. .rtc_data_start = RTC_MIN,
  100. .rtc_alarm_len = RTC_DATA_LEN - 1,
  101. };
  102. static const struct da9063_compatible_rtc_regmap da9063_bb_regs = {
  103. /* REGS */
  104. .rtc_enable_reg = DA9063_REG_CONTROL_E,
  105. .rtc_alarm_secs_reg = DA9063_BB_REG_ALARM_S,
  106. .rtc_alarm_year_reg = DA9063_BB_REG_ALARM_Y,
  107. .rtc_count_secs_reg = DA9063_REG_COUNT_S,
  108. .rtc_count_year_reg = DA9063_REG_COUNT_Y,
  109. .rtc_event_reg = DA9063_REG_EVENT_A,
  110. /* MASKS */
  111. .rtc_enable_mask = DA9063_RTC_EN,
  112. .rtc_crystal_mask = DA9063_CRYSTAL,
  113. .rtc_enable_32k_crystal_reg = DA9063_REG_EN_32K,
  114. .rtc_event_alarm_mask = DA9063_E_ALARM,
  115. .rtc_alarm_on_mask = DA9063_ALARM_ON,
  116. .rtc_alarm_status_mask = DA9063_ALARM_STATUS_ALARM |
  117. DA9063_ALARM_STATUS_TICK,
  118. .rtc_tick_on_mask = DA9063_TICK_ON,
  119. .rtc_ready_to_read_mask = DA9063_RTC_READ,
  120. .rtc_count_sec_mask = DA9063_COUNT_SEC_MASK,
  121. .rtc_count_min_mask = DA9063_COUNT_MIN_MASK,
  122. .rtc_count_hour_mask = DA9063_COUNT_HOUR_MASK,
  123. .rtc_count_day_mask = DA9063_COUNT_DAY_MASK,
  124. .rtc_count_month_mask = DA9063_COUNT_MONTH_MASK,
  125. .rtc_count_year_mask = DA9063_COUNT_YEAR_MASK,
  126. /* ALARM CONFIG */
  127. .rtc_data_start = RTC_SEC,
  128. .rtc_alarm_len = RTC_DATA_LEN,
  129. };
  130. static const struct da9063_compatible_rtc_regmap da9062_aa_regs = {
  131. /* REGS */
  132. .rtc_enable_reg = DA9062AA_CONTROL_E,
  133. .rtc_alarm_secs_reg = DA9062AA_ALARM_S,
  134. .rtc_alarm_year_reg = DA9062AA_ALARM_Y,
  135. .rtc_count_secs_reg = DA9062AA_COUNT_S,
  136. .rtc_count_year_reg = DA9062AA_COUNT_Y,
  137. .rtc_event_reg = DA9062AA_EVENT_A,
  138. /* MASKS */
  139. .rtc_enable_mask = DA9062AA_RTC_EN_MASK,
  140. .rtc_crystal_mask = DA9062AA_CRYSTAL_MASK,
  141. .rtc_enable_32k_crystal_reg = DA9062AA_EN_32K,
  142. .rtc_event_alarm_mask = DA9062AA_M_ALARM_MASK,
  143. .rtc_alarm_on_mask = DA9062AA_ALARM_ON_MASK,
  144. .rtc_alarm_status_mask = (0x02 << 6),
  145. .rtc_tick_on_mask = DA9062AA_TICK_ON_MASK,
  146. .rtc_ready_to_read_mask = DA9062AA_RTC_READ_MASK,
  147. .rtc_count_sec_mask = DA9062AA_COUNT_SEC_MASK,
  148. .rtc_count_min_mask = DA9062AA_COUNT_MIN_MASK,
  149. .rtc_count_hour_mask = DA9062AA_COUNT_HOUR_MASK,
  150. .rtc_count_day_mask = DA9062AA_COUNT_DAY_MASK,
  151. .rtc_count_month_mask = DA9062AA_COUNT_MONTH_MASK,
  152. .rtc_count_year_mask = DA9062AA_COUNT_YEAR_MASK,
  153. /* ALARM CONFIG */
  154. .rtc_data_start = RTC_SEC,
  155. .rtc_alarm_len = RTC_DATA_LEN,
  156. };
  157. static const struct of_device_id da9063_compatible_reg_id_table[] = {
  158. { .compatible = "dlg,da9063-rtc", .data = &da9063_bb_regs },
  159. { .compatible = "dlg,da9062-rtc", .data = &da9062_aa_regs },
  160. { },
  161. };
  162. MODULE_DEVICE_TABLE(of, da9063_compatible_reg_id_table);
  163. static void da9063_data_to_tm(u8 *data, struct rtc_time *tm,
  164. struct da9063_compatible_rtc *rtc)
  165. {
  166. const struct da9063_compatible_rtc_regmap *config = rtc->config;
  167. tm->tm_sec = data[RTC_SEC] & config->rtc_count_sec_mask;
  168. tm->tm_min = data[RTC_MIN] & config->rtc_count_min_mask;
  169. tm->tm_hour = data[RTC_HOUR] & config->rtc_count_hour_mask;
  170. tm->tm_mday = data[RTC_DAY] & config->rtc_count_day_mask;
  171. tm->tm_mon = MONTHS_FROM_DA9063(data[RTC_MONTH] &
  172. config->rtc_count_month_mask);
  173. tm->tm_year = YEARS_FROM_DA9063(data[RTC_YEAR] &
  174. config->rtc_count_year_mask);
  175. }
  176. static void da9063_tm_to_data(struct rtc_time *tm, u8 *data,
  177. struct da9063_compatible_rtc *rtc)
  178. {
  179. const struct da9063_compatible_rtc_regmap *config = rtc->config;
  180. data[RTC_SEC] = tm->tm_sec & config->rtc_count_sec_mask;
  181. data[RTC_MIN] = tm->tm_min & config->rtc_count_min_mask;
  182. data[RTC_HOUR] = tm->tm_hour & config->rtc_count_hour_mask;
  183. data[RTC_DAY] = tm->tm_mday & config->rtc_count_day_mask;
  184. data[RTC_MONTH] = MONTHS_TO_DA9063(tm->tm_mon) &
  185. config->rtc_count_month_mask;
  186. data[RTC_YEAR] = YEARS_TO_DA9063(tm->tm_year) &
  187. config->rtc_count_year_mask;
  188. }
  189. static int da9063_rtc_stop_alarm(struct device *dev)
  190. {
  191. struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
  192. const struct da9063_compatible_rtc_regmap *config = rtc->config;
  193. return regmap_update_bits(rtc->regmap,
  194. config->rtc_alarm_year_reg,
  195. config->rtc_alarm_on_mask,
  196. 0);
  197. }
  198. static int da9063_rtc_start_alarm(struct device *dev)
  199. {
  200. struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
  201. const struct da9063_compatible_rtc_regmap *config = rtc->config;
  202. return regmap_update_bits(rtc->regmap,
  203. config->rtc_alarm_year_reg,
  204. config->rtc_alarm_on_mask,
  205. config->rtc_alarm_on_mask);
  206. }
  207. static int da9063_rtc_read_time(struct device *dev, struct rtc_time *tm)
  208. {
  209. struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
  210. const struct da9063_compatible_rtc_regmap *config = rtc->config;
  211. unsigned long tm_secs;
  212. unsigned long al_secs;
  213. u8 data[RTC_DATA_LEN];
  214. int ret;
  215. ret = regmap_bulk_read(rtc->regmap,
  216. config->rtc_count_secs_reg,
  217. data, RTC_DATA_LEN);
  218. if (ret < 0) {
  219. dev_err(dev, "Failed to read RTC time data: %d\n", ret);
  220. return ret;
  221. }
  222. if (!(data[RTC_SEC] & config->rtc_ready_to_read_mask)) {
  223. dev_dbg(dev, "RTC not yet ready to be read by the host\n");
  224. return -EINVAL;
  225. }
  226. da9063_data_to_tm(data, tm, rtc);
  227. rtc_tm_to_time(tm, &tm_secs);
  228. rtc_tm_to_time(&rtc->alarm_time, &al_secs);
  229. /* handle the rtc synchronisation delay */
  230. if (rtc->rtc_sync == true && al_secs - tm_secs == 1)
  231. memcpy(tm, &rtc->alarm_time, sizeof(struct rtc_time));
  232. else
  233. rtc->rtc_sync = false;
  234. return 0;
  235. }
  236. static int da9063_rtc_set_time(struct device *dev, struct rtc_time *tm)
  237. {
  238. struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
  239. const struct da9063_compatible_rtc_regmap *config = rtc->config;
  240. u8 data[RTC_DATA_LEN];
  241. int ret;
  242. da9063_tm_to_data(tm, data, rtc);
  243. ret = regmap_bulk_write(rtc->regmap,
  244. config->rtc_count_secs_reg,
  245. data, RTC_DATA_LEN);
  246. if (ret < 0)
  247. dev_err(dev, "Failed to set RTC time data: %d\n", ret);
  248. return ret;
  249. }
  250. static int da9063_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  251. {
  252. struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
  253. const struct da9063_compatible_rtc_regmap *config = rtc->config;
  254. u8 data[RTC_DATA_LEN];
  255. int ret;
  256. unsigned int val;
  257. data[RTC_SEC] = 0;
  258. ret = regmap_bulk_read(rtc->regmap,
  259. config->rtc_alarm_secs_reg,
  260. &data[config->rtc_data_start],
  261. config->rtc_alarm_len);
  262. if (ret < 0)
  263. return ret;
  264. da9063_data_to_tm(data, &alrm->time, rtc);
  265. alrm->enabled = !!(data[RTC_YEAR] & config->rtc_alarm_on_mask);
  266. ret = regmap_read(rtc->regmap,
  267. config->rtc_event_reg,
  268. &val);
  269. if (ret < 0)
  270. return ret;
  271. if (val & config->rtc_event_alarm_mask)
  272. alrm->pending = 1;
  273. else
  274. alrm->pending = 0;
  275. return 0;
  276. }
  277. static int da9063_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  278. {
  279. struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
  280. const struct da9063_compatible_rtc_regmap *config = rtc->config;
  281. u8 data[RTC_DATA_LEN];
  282. int ret;
  283. da9063_tm_to_data(&alrm->time, data, rtc);
  284. ret = da9063_rtc_stop_alarm(dev);
  285. if (ret < 0) {
  286. dev_err(dev, "Failed to stop alarm: %d\n", ret);
  287. return ret;
  288. }
  289. ret = regmap_bulk_write(rtc->regmap,
  290. config->rtc_alarm_secs_reg,
  291. &data[config->rtc_data_start],
  292. config->rtc_alarm_len);
  293. if (ret < 0) {
  294. dev_err(dev, "Failed to write alarm: %d\n", ret);
  295. return ret;
  296. }
  297. da9063_data_to_tm(data, &rtc->alarm_time, rtc);
  298. if (alrm->enabled) {
  299. ret = da9063_rtc_start_alarm(dev);
  300. if (ret < 0) {
  301. dev_err(dev, "Failed to start alarm: %d\n", ret);
  302. return ret;
  303. }
  304. }
  305. return ret;
  306. }
  307. static int da9063_rtc_alarm_irq_enable(struct device *dev,
  308. unsigned int enabled)
  309. {
  310. if (enabled)
  311. return da9063_rtc_start_alarm(dev);
  312. else
  313. return da9063_rtc_stop_alarm(dev);
  314. }
  315. static irqreturn_t da9063_alarm_event(int irq, void *data)
  316. {
  317. struct da9063_compatible_rtc *rtc = data;
  318. const struct da9063_compatible_rtc_regmap *config = rtc->config;
  319. regmap_update_bits(rtc->regmap,
  320. config->rtc_alarm_year_reg,
  321. config->rtc_alarm_on_mask,
  322. 0);
  323. rtc->rtc_sync = true;
  324. rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);
  325. return IRQ_HANDLED;
  326. }
  327. static const struct rtc_class_ops da9063_rtc_ops = {
  328. .read_time = da9063_rtc_read_time,
  329. .set_time = da9063_rtc_set_time,
  330. .read_alarm = da9063_rtc_read_alarm,
  331. .set_alarm = da9063_rtc_set_alarm,
  332. .alarm_irq_enable = da9063_rtc_alarm_irq_enable,
  333. };
  334. static int da9063_rtc_probe(struct platform_device *pdev)
  335. {
  336. struct da9063_compatible_rtc *rtc;
  337. const struct da9063_compatible_rtc_regmap *config;
  338. const struct of_device_id *match;
  339. int irq_alarm;
  340. u8 data[RTC_DATA_LEN];
  341. int ret;
  342. if (!pdev->dev.of_node)
  343. return -ENXIO;
  344. match = of_match_node(da9063_compatible_reg_id_table,
  345. pdev->dev.of_node);
  346. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  347. if (!rtc)
  348. return -ENOMEM;
  349. rtc->config = match->data;
  350. if (of_device_is_compatible(pdev->dev.of_node, "dlg,da9063-rtc")) {
  351. struct da9063 *chip = dev_get_drvdata(pdev->dev.parent);
  352. if (chip->variant_code == PMIC_DA9063_AD)
  353. rtc->config = &da9063_ad_regs;
  354. }
  355. rtc->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  356. if (!rtc->regmap) {
  357. dev_warn(&pdev->dev, "Parent regmap unavailable.\n");
  358. return -ENXIO;
  359. }
  360. config = rtc->config;
  361. ret = regmap_update_bits(rtc->regmap,
  362. config->rtc_enable_reg,
  363. config->rtc_enable_mask,
  364. config->rtc_enable_mask);
  365. if (ret < 0) {
  366. dev_err(&pdev->dev, "Failed to enable RTC\n");
  367. return ret;
  368. }
  369. ret = regmap_update_bits(rtc->regmap,
  370. config->rtc_enable_32k_crystal_reg,
  371. config->rtc_crystal_mask,
  372. config->rtc_crystal_mask);
  373. if (ret < 0) {
  374. dev_err(&pdev->dev, "Failed to run 32kHz oscillator\n");
  375. return ret;
  376. }
  377. ret = regmap_update_bits(rtc->regmap,
  378. config->rtc_alarm_secs_reg,
  379. config->rtc_alarm_status_mask,
  380. 0);
  381. if (ret < 0) {
  382. dev_err(&pdev->dev, "Failed to access RTC alarm register\n");
  383. return ret;
  384. }
  385. ret = regmap_update_bits(rtc->regmap,
  386. config->rtc_alarm_secs_reg,
  387. DA9063_ALARM_STATUS_ALARM,
  388. DA9063_ALARM_STATUS_ALARM);
  389. if (ret < 0) {
  390. dev_err(&pdev->dev, "Failed to access RTC alarm register\n");
  391. return ret;
  392. }
  393. ret = regmap_update_bits(rtc->regmap,
  394. config->rtc_alarm_year_reg,
  395. config->rtc_tick_on_mask,
  396. 0);
  397. if (ret < 0) {
  398. dev_err(&pdev->dev, "Failed to disable TICKs\n");
  399. return ret;
  400. }
  401. data[RTC_SEC] = 0;
  402. ret = regmap_bulk_read(rtc->regmap,
  403. config->rtc_alarm_secs_reg,
  404. &data[config->rtc_data_start],
  405. config->rtc_alarm_len);
  406. if (ret < 0) {
  407. dev_err(&pdev->dev, "Failed to read initial alarm data: %d\n",
  408. ret);
  409. return ret;
  410. }
  411. platform_set_drvdata(pdev, rtc);
  412. rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, DA9063_DRVNAME_RTC,
  413. &da9063_rtc_ops, THIS_MODULE);
  414. if (IS_ERR(rtc->rtc_dev))
  415. return PTR_ERR(rtc->rtc_dev);
  416. da9063_data_to_tm(data, &rtc->alarm_time, rtc);
  417. rtc->rtc_sync = false;
  418. /*
  419. * TODO: some models have alarms on a minute boundary but still support
  420. * real hardware interrupts. Add this once the core supports it.
  421. */
  422. if (config->rtc_data_start != RTC_SEC)
  423. rtc->rtc_dev->uie_unsupported = 1;
  424. irq_alarm = platform_get_irq_byname(pdev, "ALARM");
  425. ret = devm_request_threaded_irq(&pdev->dev, irq_alarm, NULL,
  426. da9063_alarm_event,
  427. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  428. "ALARM", rtc);
  429. if (ret)
  430. dev_err(&pdev->dev, "Failed to request ALARM IRQ %d: %d\n",
  431. irq_alarm, ret);
  432. return ret;
  433. }
  434. static struct platform_driver da9063_rtc_driver = {
  435. .probe = da9063_rtc_probe,
  436. .driver = {
  437. .name = DA9063_DRVNAME_RTC,
  438. .of_match_table = da9063_compatible_reg_id_table,
  439. },
  440. };
  441. module_platform_driver(da9063_rtc_driver);
  442. MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
  443. MODULE_DESCRIPTION("Real time clock device driver for Dialog DA9063");
  444. MODULE_LICENSE("GPL");
  445. MODULE_ALIAS("platform:" DA9063_DRVNAME_RTC);