rtc-tps6594.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * RTC driver for tps6594 PMIC
  4. *
  5. * Copyright (C) 2023 BayLibre Incorporated - https://www.baylibre.com/
  6. */
  7. #include <linux/bcd.h>
  8. #include <linux/errno.h>
  9. #include <linux/init.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/kernel.h>
  12. #include <linux/limits.h>
  13. #include <linux/math64.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/property.h>
  18. #include <linux/rtc.h>
  19. #include <linux/types.h>
  20. #include <linux/units.h>
  21. #include <linux/mfd/tps6594.h>
  22. // Total number of RTC registers needed to set time
  23. #define NUM_TIME_REGS (TPS6594_REG_RTC_WEEKS - TPS6594_REG_RTC_SECONDS + 1)
  24. // Total number of RTC alarm registers
  25. #define NUM_TIME_ALARM_REGS (NUM_TIME_REGS - 1)
  26. /*
  27. * Min and max values supported by 'offset' interface (swapped sign).
  28. * After conversion, the values do not exceed the range [-32767, 33767]
  29. * which COMP_REG must conform to.
  30. */
  31. #define MIN_OFFSET (-277774)
  32. #define MAX_OFFSET (277774)
  33. // Number of ticks per hour
  34. #define TICKS_PER_HOUR (32768 * 3600LL)
  35. // Multiplier for ppb conversions
  36. #define PPB_MULT NANO
  37. struct tps6594_rtc {
  38. struct rtc_device *rtc_dev;
  39. int irq;
  40. };
  41. static int tps6594_rtc_alarm_irq_enable(struct device *dev,
  42. unsigned int enabled)
  43. {
  44. struct tps6594 *tps = dev_get_drvdata(dev->parent);
  45. u8 val;
  46. val = enabled ? TPS6594_BIT_IT_ALARM : 0;
  47. return regmap_update_bits(tps->regmap, TPS6594_REG_RTC_INTERRUPTS,
  48. TPS6594_BIT_IT_ALARM, val);
  49. }
  50. /* Pulse GET_TIME field of RTC_CTRL_1 to store a timestamp in shadow registers. */
  51. static int tps6594_rtc_shadow_timestamp(struct device *dev, struct tps6594 *tps)
  52. {
  53. int ret;
  54. /*
  55. * Set GET_TIME to 0. Next time we set GET_TIME to 1 we will be sure to store
  56. * an up-to-date timestamp.
  57. */
  58. ret = regmap_clear_bits(tps->regmap, TPS6594_REG_RTC_CTRL_1,
  59. TPS6594_BIT_GET_TIME);
  60. if (ret < 0)
  61. return ret;
  62. /*
  63. * Copy content of RTC registers to shadow registers or latches to read
  64. * a coherent timestamp.
  65. */
  66. return regmap_set_bits(tps->regmap, TPS6594_REG_RTC_CTRL_1,
  67. TPS6594_BIT_GET_TIME);
  68. }
  69. static int tps6594_rtc_read_time(struct device *dev, struct rtc_time *tm)
  70. {
  71. unsigned char rtc_data[NUM_TIME_REGS];
  72. struct tps6594 *tps = dev_get_drvdata(dev->parent);
  73. int ret;
  74. // Check if RTC is running.
  75. ret = regmap_test_bits(tps->regmap, TPS6594_REG_RTC_STATUS,
  76. TPS6594_BIT_RUN);
  77. if (ret < 0)
  78. return ret;
  79. if (ret == 0)
  80. return -EINVAL;
  81. ret = tps6594_rtc_shadow_timestamp(dev, tps);
  82. if (ret < 0)
  83. return ret;
  84. // Read shadowed RTC registers.
  85. ret = regmap_bulk_read(tps->regmap, TPS6594_REG_RTC_SECONDS, rtc_data,
  86. NUM_TIME_REGS);
  87. if (ret < 0)
  88. return ret;
  89. tm->tm_sec = bcd2bin(rtc_data[0]);
  90. tm->tm_min = bcd2bin(rtc_data[1]);
  91. tm->tm_hour = bcd2bin(rtc_data[2]);
  92. tm->tm_mday = bcd2bin(rtc_data[3]);
  93. tm->tm_mon = bcd2bin(rtc_data[4]) - 1;
  94. tm->tm_year = bcd2bin(rtc_data[5]) + 100;
  95. tm->tm_wday = bcd2bin(rtc_data[6]);
  96. return 0;
  97. }
  98. static int tps6594_rtc_set_time(struct device *dev, struct rtc_time *tm)
  99. {
  100. unsigned char rtc_data[NUM_TIME_REGS];
  101. struct tps6594 *tps = dev_get_drvdata(dev->parent);
  102. int ret;
  103. rtc_data[0] = bin2bcd(tm->tm_sec);
  104. rtc_data[1] = bin2bcd(tm->tm_min);
  105. rtc_data[2] = bin2bcd(tm->tm_hour);
  106. rtc_data[3] = bin2bcd(tm->tm_mday);
  107. rtc_data[4] = bin2bcd(tm->tm_mon + 1);
  108. rtc_data[5] = bin2bcd(tm->tm_year - 100);
  109. rtc_data[6] = bin2bcd(tm->tm_wday);
  110. // Stop RTC while updating the RTC time registers.
  111. ret = regmap_clear_bits(tps->regmap, TPS6594_REG_RTC_CTRL_1,
  112. TPS6594_BIT_STOP_RTC);
  113. if (ret < 0)
  114. return ret;
  115. // Update all the time registers in one shot.
  116. ret = regmap_bulk_write(tps->regmap, TPS6594_REG_RTC_SECONDS, rtc_data,
  117. NUM_TIME_REGS);
  118. if (ret < 0)
  119. return ret;
  120. // Start back RTC.
  121. return regmap_set_bits(tps->regmap, TPS6594_REG_RTC_CTRL_1,
  122. TPS6594_BIT_STOP_RTC);
  123. }
  124. static int tps6594_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  125. {
  126. unsigned char alarm_data[NUM_TIME_ALARM_REGS];
  127. u32 int_val;
  128. struct tps6594 *tps = dev_get_drvdata(dev->parent);
  129. int ret;
  130. ret = regmap_bulk_read(tps->regmap, TPS6594_REG_ALARM_SECONDS,
  131. alarm_data, NUM_TIME_ALARM_REGS);
  132. if (ret < 0)
  133. return ret;
  134. alm->time.tm_sec = bcd2bin(alarm_data[0]);
  135. alm->time.tm_min = bcd2bin(alarm_data[1]);
  136. alm->time.tm_hour = bcd2bin(alarm_data[2]);
  137. alm->time.tm_mday = bcd2bin(alarm_data[3]);
  138. alm->time.tm_mon = bcd2bin(alarm_data[4]) - 1;
  139. alm->time.tm_year = bcd2bin(alarm_data[5]) + 100;
  140. ret = regmap_read(tps->regmap, TPS6594_REG_RTC_INTERRUPTS, &int_val);
  141. if (ret < 0)
  142. return ret;
  143. alm->enabled = int_val & TPS6594_BIT_IT_ALARM;
  144. return 0;
  145. }
  146. static int tps6594_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  147. {
  148. unsigned char alarm_data[NUM_TIME_ALARM_REGS];
  149. struct tps6594 *tps = dev_get_drvdata(dev->parent);
  150. int ret;
  151. // Disable alarm irq before changing the alarm timestamp.
  152. ret = tps6594_rtc_alarm_irq_enable(dev, 0);
  153. if (ret)
  154. return ret;
  155. alarm_data[0] = bin2bcd(alm->time.tm_sec);
  156. alarm_data[1] = bin2bcd(alm->time.tm_min);
  157. alarm_data[2] = bin2bcd(alm->time.tm_hour);
  158. alarm_data[3] = bin2bcd(alm->time.tm_mday);
  159. alarm_data[4] = bin2bcd(alm->time.tm_mon + 1);
  160. alarm_data[5] = bin2bcd(alm->time.tm_year - 100);
  161. // Update all the alarm registers in one shot.
  162. ret = regmap_bulk_write(tps->regmap, TPS6594_REG_ALARM_SECONDS,
  163. alarm_data, NUM_TIME_ALARM_REGS);
  164. if (ret < 0)
  165. return ret;
  166. if (alm->enabled)
  167. ret = tps6594_rtc_alarm_irq_enable(dev, 1);
  168. return ret;
  169. }
  170. static int tps6594_rtc_set_calibration(struct device *dev, int calibration)
  171. {
  172. struct tps6594 *tps = dev_get_drvdata(dev->parent);
  173. __le16 value;
  174. int ret;
  175. /*
  176. * TPS6594 uses two's complement 16 bit value for compensation of RTC
  177. * crystal inaccuracies. One time every hour when seconds counter
  178. * increments from 0 to 1 compensation value will be added to internal
  179. * RTC counter value.
  180. *
  181. * Valid range for compensation value: [-32767 .. 32767].
  182. */
  183. if (calibration < S16_MIN + 1 || calibration > S16_MAX)
  184. return -ERANGE;
  185. value = cpu_to_le16(calibration);
  186. // Update all the compensation registers in one shot.
  187. ret = regmap_bulk_write(tps->regmap, TPS6594_REG_RTC_COMP_LSB, &value,
  188. sizeof(value));
  189. if (ret < 0)
  190. return ret;
  191. // Enable automatic compensation.
  192. return regmap_set_bits(tps->regmap, TPS6594_REG_RTC_CTRL_1,
  193. TPS6594_BIT_AUTO_COMP);
  194. }
  195. static int tps6594_rtc_get_calibration(struct device *dev, int *calibration)
  196. {
  197. struct tps6594 *tps = dev_get_drvdata(dev->parent);
  198. unsigned int ctrl;
  199. __le16 value;
  200. int ret;
  201. ret = regmap_read(tps->regmap, TPS6594_REG_RTC_CTRL_1, &ctrl);
  202. if (ret < 0)
  203. return ret;
  204. // If automatic compensation is not enabled report back zero.
  205. if (!(ctrl & TPS6594_BIT_AUTO_COMP)) {
  206. *calibration = 0;
  207. return 0;
  208. }
  209. ret = regmap_bulk_read(tps->regmap, TPS6594_REG_RTC_COMP_LSB, &value,
  210. sizeof(value));
  211. if (ret < 0)
  212. return ret;
  213. *calibration = le16_to_cpu(value);
  214. return 0;
  215. }
  216. static int tps6594_rtc_read_offset(struct device *dev, long *offset)
  217. {
  218. int calibration;
  219. s64 tmp;
  220. int ret;
  221. ret = tps6594_rtc_get_calibration(dev, &calibration);
  222. if (ret < 0)
  223. return ret;
  224. // Convert from RTC calibration register format to ppb format.
  225. tmp = calibration * PPB_MULT;
  226. if (tmp < 0)
  227. tmp -= TICKS_PER_HOUR / 2LL;
  228. else
  229. tmp += TICKS_PER_HOUR / 2LL;
  230. tmp = div_s64(tmp, TICKS_PER_HOUR);
  231. /*
  232. * SAFETY:
  233. * Computatiion is the reverse operation of the one done in
  234. * `tps6594_rtc_set_offset`. The safety remarks applie here too.
  235. */
  236. /*
  237. * Offset value operates in negative way, so swap sign.
  238. * See 8.3.10.5, (32768 - COMP_REG).
  239. */
  240. *offset = (long)-tmp;
  241. return 0;
  242. }
  243. static int tps6594_rtc_set_offset(struct device *dev, long offset)
  244. {
  245. int calibration;
  246. s64 tmp;
  247. // Make sure offset value is within supported range.
  248. if (offset < MIN_OFFSET || offset > MAX_OFFSET)
  249. return -ERANGE;
  250. // Convert from ppb format to RTC calibration register format.
  251. tmp = offset * TICKS_PER_HOUR;
  252. if (tmp < 0)
  253. tmp -= PPB_MULT / 2LL;
  254. else
  255. tmp += PPB_MULT / 2LL;
  256. tmp = div_s64(tmp, PPB_MULT);
  257. /*
  258. * SAFETY:
  259. * - tmp = offset * TICK_PER_HOUR :
  260. * `offset` can't be more than 277774, so `tmp` can't exceed 277774000000000
  261. * which is lower than the maximum value in an `s64` (2^63-1). No overflow here.
  262. *
  263. * - tmp += TICK_PER_HOUR / 2LL :
  264. * tmp will have a maximum value of 277774117964800 which is still inferior to 2^63-1.
  265. */
  266. // Offset value operates in negative way, so swap sign.
  267. calibration = (int)-tmp;
  268. return tps6594_rtc_set_calibration(dev, calibration);
  269. }
  270. static irqreturn_t tps6594_rtc_interrupt(int irq, void *data)
  271. {
  272. struct device *dev = data;
  273. struct tps6594 *tps = dev_get_drvdata(dev->parent);
  274. struct tps6594_rtc *rtc = dev_get_drvdata(dev);
  275. int ret;
  276. u32 rtc_reg;
  277. ret = regmap_read(tps->regmap, TPS6594_REG_RTC_STATUS, &rtc_reg);
  278. if (ret)
  279. return IRQ_NONE;
  280. rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);
  281. return IRQ_HANDLED;
  282. }
  283. static const struct rtc_class_ops tps6594_rtc_ops = {
  284. .read_time = tps6594_rtc_read_time,
  285. .set_time = tps6594_rtc_set_time,
  286. .read_alarm = tps6594_rtc_read_alarm,
  287. .set_alarm = tps6594_rtc_set_alarm,
  288. .alarm_irq_enable = tps6594_rtc_alarm_irq_enable,
  289. .read_offset = tps6594_rtc_read_offset,
  290. .set_offset = tps6594_rtc_set_offset,
  291. };
  292. static int tps6594_rtc_probe(struct platform_device *pdev)
  293. {
  294. struct tps6594 *tps = dev_get_drvdata(pdev->dev.parent);
  295. struct device *dev = &pdev->dev;
  296. struct tps6594_rtc *rtc;
  297. int irq;
  298. int ret;
  299. rtc = devm_kzalloc(dev, sizeof(*rtc), GFP_KERNEL);
  300. if (!rtc)
  301. return -ENOMEM;
  302. rtc->rtc_dev = devm_rtc_allocate_device(dev);
  303. if (IS_ERR(rtc->rtc_dev))
  304. return PTR_ERR(rtc->rtc_dev);
  305. // Enable crystal oscillator.
  306. ret = regmap_set_bits(tps->regmap, TPS6594_REG_RTC_CTRL_2,
  307. TPS6594_BIT_XTAL_EN);
  308. if (ret < 0)
  309. return ret;
  310. ret = regmap_test_bits(tps->regmap, TPS6594_REG_RTC_STATUS,
  311. TPS6594_BIT_RUN);
  312. if (ret < 0)
  313. return ret;
  314. // RTC not running.
  315. if (ret == 0) {
  316. ret = regmap_set_bits(tps->regmap, TPS6594_REG_RTC_CTRL_1,
  317. TPS6594_BIT_STOP_RTC);
  318. if (ret < 0)
  319. return ret;
  320. /*
  321. * On some boards, a 40 ms delay is needed before BIT_RUN is set.
  322. * 80 ms should provide sufficient margin.
  323. */
  324. mdelay(80);
  325. /*
  326. * RTC should be running now. Check if this is the case.
  327. * If not it might be a missing oscillator.
  328. */
  329. ret = regmap_test_bits(tps->regmap, TPS6594_REG_RTC_STATUS,
  330. TPS6594_BIT_RUN);
  331. if (ret < 0)
  332. return ret;
  333. if (ret == 0)
  334. return -ENODEV;
  335. // Stop RTC until first call to `tps6594_rtc_set_time`.
  336. ret = regmap_clear_bits(tps->regmap, TPS6594_REG_RTC_CTRL_1,
  337. TPS6594_BIT_STOP_RTC);
  338. if (ret < 0)
  339. return ret;
  340. }
  341. platform_set_drvdata(pdev, rtc);
  342. irq = platform_get_irq_byname(pdev, TPS6594_IRQ_NAME_ALARM);
  343. if (irq < 0)
  344. return dev_err_probe(dev, irq, "Failed to get irq\n");
  345. rtc->irq = irq;
  346. ret = devm_request_threaded_irq(dev, irq, NULL, tps6594_rtc_interrupt,
  347. IRQF_ONESHOT, TPS6594_IRQ_NAME_ALARM,
  348. dev);
  349. if (ret < 0)
  350. return dev_err_probe(dev, ret,
  351. "Failed to request_threaded_irq\n");
  352. ret = device_init_wakeup(dev, true);
  353. if (ret < 0)
  354. return dev_err_probe(dev, ret,
  355. "Failed to init rtc as wakeup source\n");
  356. rtc->rtc_dev->ops = &tps6594_rtc_ops;
  357. rtc->rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_2000;
  358. rtc->rtc_dev->range_max = RTC_TIMESTAMP_END_2099;
  359. return devm_rtc_register_device(rtc->rtc_dev);
  360. }
  361. static int tps6594_rtc_resume(struct device *dev)
  362. {
  363. struct tps6594 *tps = dev_get_drvdata(dev->parent);
  364. struct tps6594_rtc *rtc = dev_get_drvdata(dev);
  365. int ret;
  366. ret = regmap_test_bits(tps->regmap, TPS6594_REG_INT_STARTUP,
  367. TPS6594_BIT_RTC_INT);
  368. if (ret < 0) {
  369. dev_err(dev, "failed to read REG_INT_STARTUP: %d\n", ret);
  370. goto out;
  371. }
  372. if (ret > 0) {
  373. /*
  374. * If the alarm bit is set, it means that the IRQ has been
  375. * fired. But, the kernel may not have woke up yet when it
  376. * happened. So, we have to clear it.
  377. */
  378. ret = regmap_write(tps->regmap, TPS6594_REG_RTC_STATUS,
  379. TPS6594_BIT_ALARM);
  380. if (ret < 0)
  381. dev_err(dev, "error clearing alarm bit: %d", ret);
  382. rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);
  383. }
  384. out:
  385. disable_irq_wake(rtc->irq);
  386. return 0;
  387. }
  388. static int tps6594_rtc_suspend(struct device *dev)
  389. {
  390. struct tps6594_rtc *rtc = dev_get_drvdata(dev);
  391. enable_irq_wake(rtc->irq);
  392. return 0;
  393. }
  394. static DEFINE_SIMPLE_DEV_PM_OPS(tps6594_rtc_pm_ops, tps6594_rtc_suspend, tps6594_rtc_resume);
  395. static const struct platform_device_id tps6594_rtc_id_table[] = {
  396. { "tps6594-rtc", },
  397. {}
  398. };
  399. MODULE_DEVICE_TABLE(platform, tps6594_rtc_id_table);
  400. static struct platform_driver tps6594_rtc_driver = {
  401. .probe = tps6594_rtc_probe,
  402. .driver = {
  403. .name = "tps6594-rtc",
  404. .pm = pm_sleep_ptr(&tps6594_rtc_pm_ops),
  405. },
  406. .id_table = tps6594_rtc_id_table,
  407. };
  408. module_platform_driver(tps6594_rtc_driver);
  409. MODULE_AUTHOR("Esteban Blanc <eblanc@baylibre.com>");
  410. MODULE_DESCRIPTION("TPS6594 RTC driver");
  411. MODULE_LICENSE("GPL");