rtc-tps65910.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * rtc-tps65910.c -- TPS65910 Real Time Clock interface
  4. *
  5. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  6. * Author: Venu Byravarasu <[email protected]>
  7. *
  8. * Based on original TI driver rtc-twl.c
  9. * Copyright (C) 2007 MontaVista Software, Inc
  10. * Author: Alexandre Rusev <[email protected]>
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/types.h>
  17. #include <linux/rtc.h>
  18. #include <linux/bcd.h>
  19. #include <linux/math64.h>
  20. #include <linux/property.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/mfd/tps65910.h>
  24. struct tps65910_rtc {
  25. struct rtc_device *rtc;
  26. int irq;
  27. };
  28. /* Total number of RTC registers needed to set time*/
  29. #define NUM_TIME_REGS (TPS65910_YEARS - TPS65910_SECONDS + 1)
  30. /* Total number of RTC registers needed to set compensation registers */
  31. #define NUM_COMP_REGS (TPS65910_RTC_COMP_MSB - TPS65910_RTC_COMP_LSB + 1)
  32. /* Min and max values supported with 'offset' interface (swapped sign) */
  33. #define MIN_OFFSET (-277761)
  34. #define MAX_OFFSET (277778)
  35. /* Number of ticks per hour */
  36. #define TICKS_PER_HOUR (32768 * 3600)
  37. /* Multiplier for ppb conversions */
  38. #define PPB_MULT (1000000000LL)
  39. static int tps65910_rtc_alarm_irq_enable(struct device *dev,
  40. unsigned int enabled)
  41. {
  42. struct tps65910 *tps = dev_get_drvdata(dev->parent);
  43. u8 val = 0;
  44. if (enabled)
  45. val = TPS65910_RTC_INTERRUPTS_IT_ALARM;
  46. return regmap_write(tps->regmap, TPS65910_RTC_INTERRUPTS, val);
  47. }
  48. /*
  49. * Gets current tps65910 RTC time and date parameters.
  50. *
  51. * The RTC's time/alarm representation is not what gmtime(3) requires
  52. * Linux to use:
  53. *
  54. * - Months are 1..12 vs Linux 0-11
  55. * - Years are 0..99 vs Linux 1900..N (we assume 21st century)
  56. */
  57. static int tps65910_rtc_read_time(struct device *dev, struct rtc_time *tm)
  58. {
  59. unsigned char rtc_data[NUM_TIME_REGS];
  60. struct tps65910 *tps = dev_get_drvdata(dev->parent);
  61. int ret;
  62. /* Copy RTC counting registers to static registers or latches */
  63. ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
  64. TPS65910_RTC_CTRL_GET_TIME, TPS65910_RTC_CTRL_GET_TIME);
  65. if (ret < 0) {
  66. dev_err(dev, "RTC CTRL reg update failed with err:%d\n", ret);
  67. return ret;
  68. }
  69. ret = regmap_bulk_read(tps->regmap, TPS65910_SECONDS, rtc_data,
  70. NUM_TIME_REGS);
  71. if (ret < 0) {
  72. dev_err(dev, "reading from RTC failed with err:%d\n", ret);
  73. return ret;
  74. }
  75. tm->tm_sec = bcd2bin(rtc_data[0]);
  76. tm->tm_min = bcd2bin(rtc_data[1]);
  77. tm->tm_hour = bcd2bin(rtc_data[2]);
  78. tm->tm_mday = bcd2bin(rtc_data[3]);
  79. tm->tm_mon = bcd2bin(rtc_data[4]) - 1;
  80. tm->tm_year = bcd2bin(rtc_data[5]) + 100;
  81. return ret;
  82. }
  83. static int tps65910_rtc_set_time(struct device *dev, struct rtc_time *tm)
  84. {
  85. unsigned char rtc_data[NUM_TIME_REGS];
  86. struct tps65910 *tps = dev_get_drvdata(dev->parent);
  87. int ret;
  88. rtc_data[0] = bin2bcd(tm->tm_sec);
  89. rtc_data[1] = bin2bcd(tm->tm_min);
  90. rtc_data[2] = bin2bcd(tm->tm_hour);
  91. rtc_data[3] = bin2bcd(tm->tm_mday);
  92. rtc_data[4] = bin2bcd(tm->tm_mon + 1);
  93. rtc_data[5] = bin2bcd(tm->tm_year - 100);
  94. /* Stop RTC while updating the RTC time registers */
  95. ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
  96. TPS65910_RTC_CTRL_STOP_RTC, 0);
  97. if (ret < 0) {
  98. dev_err(dev, "RTC stop failed with err:%d\n", ret);
  99. return ret;
  100. }
  101. /* update all the time registers in one shot */
  102. ret = regmap_bulk_write(tps->regmap, TPS65910_SECONDS, rtc_data,
  103. NUM_TIME_REGS);
  104. if (ret < 0) {
  105. dev_err(dev, "rtc_set_time error %d\n", ret);
  106. return ret;
  107. }
  108. /* Start back RTC */
  109. ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
  110. TPS65910_RTC_CTRL_STOP_RTC, 1);
  111. if (ret < 0)
  112. dev_err(dev, "RTC start failed with err:%d\n", ret);
  113. return ret;
  114. }
  115. /*
  116. * Gets current tps65910 RTC alarm time.
  117. */
  118. static int tps65910_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  119. {
  120. unsigned char alarm_data[NUM_TIME_REGS];
  121. u32 int_val;
  122. struct tps65910 *tps = dev_get_drvdata(dev->parent);
  123. int ret;
  124. ret = regmap_bulk_read(tps->regmap, TPS65910_ALARM_SECONDS, alarm_data,
  125. NUM_TIME_REGS);
  126. if (ret < 0) {
  127. dev_err(dev, "rtc_read_alarm error %d\n", ret);
  128. return ret;
  129. }
  130. alm->time.tm_sec = bcd2bin(alarm_data[0]);
  131. alm->time.tm_min = bcd2bin(alarm_data[1]);
  132. alm->time.tm_hour = bcd2bin(alarm_data[2]);
  133. alm->time.tm_mday = bcd2bin(alarm_data[3]);
  134. alm->time.tm_mon = bcd2bin(alarm_data[4]) - 1;
  135. alm->time.tm_year = bcd2bin(alarm_data[5]) + 100;
  136. ret = regmap_read(tps->regmap, TPS65910_RTC_INTERRUPTS, &int_val);
  137. if (ret < 0)
  138. return ret;
  139. if (int_val & TPS65910_RTC_INTERRUPTS_IT_ALARM)
  140. alm->enabled = 1;
  141. return ret;
  142. }
  143. static int tps65910_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  144. {
  145. unsigned char alarm_data[NUM_TIME_REGS];
  146. struct tps65910 *tps = dev_get_drvdata(dev->parent);
  147. int ret;
  148. ret = tps65910_rtc_alarm_irq_enable(dev, 0);
  149. if (ret)
  150. return ret;
  151. alarm_data[0] = bin2bcd(alm->time.tm_sec);
  152. alarm_data[1] = bin2bcd(alm->time.tm_min);
  153. alarm_data[2] = bin2bcd(alm->time.tm_hour);
  154. alarm_data[3] = bin2bcd(alm->time.tm_mday);
  155. alarm_data[4] = bin2bcd(alm->time.tm_mon + 1);
  156. alarm_data[5] = bin2bcd(alm->time.tm_year - 100);
  157. /* update all the alarm registers in one shot */
  158. ret = regmap_bulk_write(tps->regmap, TPS65910_ALARM_SECONDS,
  159. alarm_data, NUM_TIME_REGS);
  160. if (ret) {
  161. dev_err(dev, "rtc_set_alarm error %d\n", ret);
  162. return ret;
  163. }
  164. if (alm->enabled)
  165. ret = tps65910_rtc_alarm_irq_enable(dev, 1);
  166. return ret;
  167. }
  168. static int tps65910_rtc_set_calibration(struct device *dev, int calibration)
  169. {
  170. unsigned char comp_data[NUM_COMP_REGS];
  171. struct tps65910 *tps = dev_get_drvdata(dev->parent);
  172. s16 value;
  173. int ret;
  174. /*
  175. * TPS65910 uses two's complement 16 bit value for compensation for RTC
  176. * crystal inaccuracies. One time every hour when seconds counter
  177. * increments from 0 to 1 compensation value will be added to internal
  178. * RTC counter value.
  179. *
  180. * Compensation value 0x7FFF is prohibited value.
  181. *
  182. * Valid range for compensation value: [-32768 .. 32766]
  183. */
  184. if ((calibration < -32768) || (calibration > 32766)) {
  185. dev_err(dev, "RTC calibration value out of range: %d\n",
  186. calibration);
  187. return -EINVAL;
  188. }
  189. value = (s16)calibration;
  190. comp_data[0] = (u16)value & 0xFF;
  191. comp_data[1] = ((u16)value >> 8) & 0xFF;
  192. /* Update all the compensation registers in one shot */
  193. ret = regmap_bulk_write(tps->regmap, TPS65910_RTC_COMP_LSB,
  194. comp_data, NUM_COMP_REGS);
  195. if (ret < 0) {
  196. dev_err(dev, "rtc_set_calibration error: %d\n", ret);
  197. return ret;
  198. }
  199. /* Enable automatic compensation */
  200. ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
  201. TPS65910_RTC_CTRL_AUTO_COMP, TPS65910_RTC_CTRL_AUTO_COMP);
  202. if (ret < 0)
  203. dev_err(dev, "auto_comp enable failed with error: %d\n", ret);
  204. return ret;
  205. }
  206. static int tps65910_rtc_get_calibration(struct device *dev, int *calibration)
  207. {
  208. unsigned char comp_data[NUM_COMP_REGS];
  209. struct tps65910 *tps = dev_get_drvdata(dev->parent);
  210. unsigned int ctrl;
  211. u16 value;
  212. int ret;
  213. ret = regmap_read(tps->regmap, TPS65910_RTC_CTRL, &ctrl);
  214. if (ret < 0)
  215. return ret;
  216. /* If automatic compensation is not enabled report back zero */
  217. if (!(ctrl & TPS65910_RTC_CTRL_AUTO_COMP)) {
  218. *calibration = 0;
  219. return 0;
  220. }
  221. ret = regmap_bulk_read(tps->regmap, TPS65910_RTC_COMP_LSB, comp_data,
  222. NUM_COMP_REGS);
  223. if (ret < 0) {
  224. dev_err(dev, "rtc_get_calibration error: %d\n", ret);
  225. return ret;
  226. }
  227. value = (u16)comp_data[0] | ((u16)comp_data[1] << 8);
  228. *calibration = (s16)value;
  229. return 0;
  230. }
  231. static int tps65910_read_offset(struct device *dev, long *offset)
  232. {
  233. int calibration;
  234. s64 tmp;
  235. int ret;
  236. ret = tps65910_rtc_get_calibration(dev, &calibration);
  237. if (ret < 0)
  238. return ret;
  239. /* Convert from RTC calibration register format to ppb format */
  240. tmp = calibration * (s64)PPB_MULT;
  241. if (tmp < 0)
  242. tmp -= TICKS_PER_HOUR / 2LL;
  243. else
  244. tmp += TICKS_PER_HOUR / 2LL;
  245. tmp = div_s64(tmp, TICKS_PER_HOUR);
  246. /* Offset value operates in negative way, so swap sign */
  247. *offset = (long)-tmp;
  248. return 0;
  249. }
  250. static int tps65910_set_offset(struct device *dev, long offset)
  251. {
  252. int calibration;
  253. s64 tmp;
  254. int ret;
  255. /* Make sure offset value is within supported range */
  256. if (offset < MIN_OFFSET || offset > MAX_OFFSET)
  257. return -ERANGE;
  258. /* Convert from ppb format to RTC calibration register format */
  259. tmp = offset * (s64)TICKS_PER_HOUR;
  260. if (tmp < 0)
  261. tmp -= PPB_MULT / 2LL;
  262. else
  263. tmp += PPB_MULT / 2LL;
  264. tmp = div_s64(tmp, PPB_MULT);
  265. /* Offset value operates in negative way, so swap sign */
  266. calibration = (int)-tmp;
  267. ret = tps65910_rtc_set_calibration(dev, calibration);
  268. return ret;
  269. }
  270. static irqreturn_t tps65910_rtc_interrupt(int irq, void *rtc)
  271. {
  272. struct device *dev = rtc;
  273. unsigned long events = 0;
  274. struct tps65910 *tps = dev_get_drvdata(dev->parent);
  275. struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
  276. int ret;
  277. u32 rtc_reg;
  278. ret = regmap_read(tps->regmap, TPS65910_RTC_STATUS, &rtc_reg);
  279. if (ret)
  280. return IRQ_NONE;
  281. if (rtc_reg & TPS65910_RTC_STATUS_ALARM)
  282. events = RTC_IRQF | RTC_AF;
  283. ret = regmap_write(tps->regmap, TPS65910_RTC_STATUS, rtc_reg);
  284. if (ret)
  285. return IRQ_NONE;
  286. /* Notify RTC core on event */
  287. rtc_update_irq(tps_rtc->rtc, 1, events);
  288. return IRQ_HANDLED;
  289. }
  290. static const struct rtc_class_ops tps65910_rtc_ops = {
  291. .read_time = tps65910_rtc_read_time,
  292. .set_time = tps65910_rtc_set_time,
  293. .read_alarm = tps65910_rtc_read_alarm,
  294. .set_alarm = tps65910_rtc_set_alarm,
  295. .alarm_irq_enable = tps65910_rtc_alarm_irq_enable,
  296. .read_offset = tps65910_read_offset,
  297. .set_offset = tps65910_set_offset,
  298. };
  299. static int tps65910_rtc_probe(struct platform_device *pdev)
  300. {
  301. struct tps65910 *tps65910 = NULL;
  302. struct tps65910_rtc *tps_rtc = NULL;
  303. int ret;
  304. int irq;
  305. u32 rtc_reg;
  306. tps65910 = dev_get_drvdata(pdev->dev.parent);
  307. tps_rtc = devm_kzalloc(&pdev->dev, sizeof(struct tps65910_rtc),
  308. GFP_KERNEL);
  309. if (!tps_rtc)
  310. return -ENOMEM;
  311. tps_rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
  312. if (IS_ERR(tps_rtc->rtc))
  313. return PTR_ERR(tps_rtc->rtc);
  314. /* Clear pending interrupts */
  315. ret = regmap_read(tps65910->regmap, TPS65910_RTC_STATUS, &rtc_reg);
  316. if (ret < 0)
  317. return ret;
  318. ret = regmap_write(tps65910->regmap, TPS65910_RTC_STATUS, rtc_reg);
  319. if (ret < 0)
  320. return ret;
  321. dev_dbg(&pdev->dev, "Enabling rtc-tps65910.\n");
  322. /* Enable RTC digital power domain */
  323. ret = regmap_update_bits(tps65910->regmap, TPS65910_DEVCTRL,
  324. DEVCTRL_RTC_PWDN_MASK, 0 << DEVCTRL_RTC_PWDN_SHIFT);
  325. if (ret < 0)
  326. return ret;
  327. rtc_reg = TPS65910_RTC_CTRL_STOP_RTC;
  328. ret = regmap_write(tps65910->regmap, TPS65910_RTC_CTRL, rtc_reg);
  329. if (ret < 0)
  330. return ret;
  331. platform_set_drvdata(pdev, tps_rtc);
  332. irq = platform_get_irq(pdev, 0);
  333. if (irq <= 0) {
  334. dev_warn(&pdev->dev, "Wake up is not possible as irq = %d\n",
  335. irq);
  336. return -ENXIO;
  337. }
  338. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  339. tps65910_rtc_interrupt, IRQF_TRIGGER_LOW,
  340. dev_name(&pdev->dev), &pdev->dev);
  341. if (ret < 0)
  342. irq = -1;
  343. tps_rtc->irq = irq;
  344. if (irq != -1) {
  345. if (device_property_present(tps65910->dev, "wakeup-source"))
  346. device_init_wakeup(&pdev->dev, 1);
  347. else
  348. device_set_wakeup_capable(&pdev->dev, 1);
  349. } else {
  350. clear_bit(RTC_FEATURE_ALARM, tps_rtc->rtc->features);
  351. }
  352. tps_rtc->rtc->ops = &tps65910_rtc_ops;
  353. tps_rtc->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
  354. tps_rtc->rtc->range_max = RTC_TIMESTAMP_END_2099;
  355. return devm_rtc_register_device(tps_rtc->rtc);
  356. }
  357. #ifdef CONFIG_PM_SLEEP
  358. static int tps65910_rtc_suspend(struct device *dev)
  359. {
  360. struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
  361. if (device_may_wakeup(dev))
  362. enable_irq_wake(tps_rtc->irq);
  363. return 0;
  364. }
  365. static int tps65910_rtc_resume(struct device *dev)
  366. {
  367. struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
  368. if (device_may_wakeup(dev))
  369. disable_irq_wake(tps_rtc->irq);
  370. return 0;
  371. }
  372. #endif
  373. static SIMPLE_DEV_PM_OPS(tps65910_rtc_pm_ops, tps65910_rtc_suspend,
  374. tps65910_rtc_resume);
  375. static struct platform_driver tps65910_rtc_driver = {
  376. .probe = tps65910_rtc_probe,
  377. .driver = {
  378. .name = "tps65910-rtc",
  379. .pm = &tps65910_rtc_pm_ops,
  380. },
  381. };
  382. module_platform_driver(tps65910_rtc_driver);
  383. MODULE_ALIAS("platform:tps65910-rtc");
  384. MODULE_AUTHOR("Venu Byravarasu <[email protected]>");
  385. MODULE_LICENSE("GPL");