rtc-ab8500.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) ST-Ericsson SA 2010
  4. *
  5. * Author: Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>
  6. *
  7. * RTC clock driver for the RTC part of the AB8500 Power management chip.
  8. * Based on RTC clock driver for the AB3100 Analog Baseband Chip by
  9. * Linus Walleij <linus.walleij@stericsson.com>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/rtc.h>
  16. #include <linux/mfd/abx500.h>
  17. #include <linux/mfd/abx500/ab8500.h>
  18. #include <linux/delay.h>
  19. #include <linux/of.h>
  20. #include <linux/pm_wakeirq.h>
  21. #define AB8500_RTC_SOFF_STAT_REG 0x00
  22. #define AB8500_RTC_CC_CONF_REG 0x01
  23. #define AB8500_RTC_READ_REQ_REG 0x02
  24. #define AB8500_RTC_WATCH_TSECMID_REG 0x03
  25. #define AB8500_RTC_WATCH_TSECHI_REG 0x04
  26. #define AB8500_RTC_WATCH_TMIN_LOW_REG 0x05
  27. #define AB8500_RTC_WATCH_TMIN_MID_REG 0x06
  28. #define AB8500_RTC_WATCH_TMIN_HI_REG 0x07
  29. #define AB8500_RTC_ALRM_MIN_LOW_REG 0x08
  30. #define AB8500_RTC_ALRM_MIN_MID_REG 0x09
  31. #define AB8500_RTC_ALRM_MIN_HI_REG 0x0A
  32. #define AB8500_RTC_STAT_REG 0x0B
  33. #define AB8500_RTC_BKUP_CHG_REG 0x0C
  34. #define AB8500_RTC_FORCE_BKUP_REG 0x0D
  35. #define AB8500_RTC_CALIB_REG 0x0E
  36. #define AB8500_RTC_SWITCH_STAT_REG 0x0F
  37. /* RtcReadRequest bits */
  38. #define RTC_READ_REQUEST 0x01
  39. #define RTC_WRITE_REQUEST 0x02
  40. /* RtcCtrl bits */
  41. #define RTC_ALARM_ENA 0x04
  42. #define RTC_STATUS_DATA 0x01
  43. #define COUNTS_PER_SEC (0xF000 / 60)
  44. static const u8 ab8500_rtc_time_regs[] = {
  45. AB8500_RTC_WATCH_TMIN_HI_REG, AB8500_RTC_WATCH_TMIN_MID_REG,
  46. AB8500_RTC_WATCH_TMIN_LOW_REG, AB8500_RTC_WATCH_TSECHI_REG,
  47. AB8500_RTC_WATCH_TSECMID_REG
  48. };
  49. static const u8 ab8500_rtc_alarm_regs[] = {
  50. AB8500_RTC_ALRM_MIN_HI_REG, AB8500_RTC_ALRM_MIN_MID_REG,
  51. AB8500_RTC_ALRM_MIN_LOW_REG
  52. };
  53. static int ab8500_rtc_read_time(struct device *dev, struct rtc_time *tm)
  54. {
  55. unsigned long timeout = jiffies + HZ;
  56. int retval, i;
  57. unsigned long mins, secs;
  58. unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
  59. u8 value;
  60. /* Request a data read */
  61. retval = abx500_set_register_interruptible(dev,
  62. AB8500_RTC, AB8500_RTC_READ_REQ_REG, RTC_READ_REQUEST);
  63. if (retval < 0)
  64. return retval;
  65. /* Wait for some cycles after enabling the rtc read in ab8500 */
  66. while (time_before(jiffies, timeout)) {
  67. retval = abx500_get_register_interruptible(dev,
  68. AB8500_RTC, AB8500_RTC_READ_REQ_REG, &value);
  69. if (retval < 0)
  70. return retval;
  71. if (!(value & RTC_READ_REQUEST))
  72. break;
  73. usleep_range(1000, 5000);
  74. }
  75. /* Read the Watchtime registers */
  76. for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
  77. retval = abx500_get_register_interruptible(dev,
  78. AB8500_RTC, ab8500_rtc_time_regs[i], &value);
  79. if (retval < 0)
  80. return retval;
  81. buf[i] = value;
  82. }
  83. mins = (buf[0] << 16) | (buf[1] << 8) | buf[2];
  84. secs = (buf[3] << 8) | buf[4];
  85. secs = secs / COUNTS_PER_SEC;
  86. secs = secs + (mins * 60);
  87. rtc_time64_to_tm(secs, tm);
  88. return 0;
  89. }
  90. static int ab8500_rtc_set_time(struct device *dev, struct rtc_time *tm)
  91. {
  92. int retval, i;
  93. unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
  94. unsigned long no_secs, no_mins, secs = 0;
  95. secs = rtc_tm_to_time64(tm);
  96. no_mins = secs / 60;
  97. no_secs = secs % 60;
  98. /* Make the seconds count as per the RTC resolution */
  99. no_secs = no_secs * COUNTS_PER_SEC;
  100. buf[4] = no_secs & 0xFF;
  101. buf[3] = (no_secs >> 8) & 0xFF;
  102. buf[2] = no_mins & 0xFF;
  103. buf[1] = (no_mins >> 8) & 0xFF;
  104. buf[0] = (no_mins >> 16) & 0xFF;
  105. for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
  106. retval = abx500_set_register_interruptible(dev, AB8500_RTC,
  107. ab8500_rtc_time_regs[i], buf[i]);
  108. if (retval < 0)
  109. return retval;
  110. }
  111. /* Request a data write */
  112. return abx500_set_register_interruptible(dev, AB8500_RTC,
  113. AB8500_RTC_READ_REQ_REG, RTC_WRITE_REQUEST);
  114. }
  115. static int ab8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  116. {
  117. int retval, i;
  118. u8 rtc_ctrl, value;
  119. unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
  120. unsigned long secs, mins;
  121. /* Check if the alarm is enabled or not */
  122. retval = abx500_get_register_interruptible(dev, AB8500_RTC,
  123. AB8500_RTC_STAT_REG, &rtc_ctrl);
  124. if (retval < 0)
  125. return retval;
  126. if (rtc_ctrl & RTC_ALARM_ENA)
  127. alarm->enabled = 1;
  128. else
  129. alarm->enabled = 0;
  130. alarm->pending = 0;
  131. for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
  132. retval = abx500_get_register_interruptible(dev, AB8500_RTC,
  133. ab8500_rtc_alarm_regs[i], &value);
  134. if (retval < 0)
  135. return retval;
  136. buf[i] = value;
  137. }
  138. mins = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
  139. secs = mins * 60;
  140. rtc_time64_to_tm(secs, &alarm->time);
  141. return 0;
  142. }
  143. static int ab8500_rtc_irq_enable(struct device *dev, unsigned int enabled)
  144. {
  145. return abx500_mask_and_set_register_interruptible(dev, AB8500_RTC,
  146. AB8500_RTC_STAT_REG, RTC_ALARM_ENA,
  147. enabled ? RTC_ALARM_ENA : 0);
  148. }
  149. static int ab8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  150. {
  151. int retval, i;
  152. unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
  153. unsigned long mins, secs = 0, cursec = 0;
  154. struct rtc_time curtm;
  155. /* Get the number of seconds since 1970 */
  156. secs = rtc_tm_to_time64(&alarm->time);
  157. /*
  158. * Check whether alarm is set less than 1min.
  159. * Since our RTC doesn't support alarm resolution less than 1min,
  160. * return -EINVAL, so UIE EMUL can take it up, incase of UIE_ON
  161. */
  162. ab8500_rtc_read_time(dev, &curtm); /* Read current time */
  163. cursec = rtc_tm_to_time64(&curtm);
  164. if ((secs - cursec) < 59) {
  165. dev_dbg(dev, "Alarm less than 1 minute not supported\r\n");
  166. return -EINVAL;
  167. }
  168. mins = secs / 60;
  169. buf[2] = mins & 0xFF;
  170. buf[1] = (mins >> 8) & 0xFF;
  171. buf[0] = (mins >> 16) & 0xFF;
  172. /* Set the alarm time */
  173. for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
  174. retval = abx500_set_register_interruptible(dev, AB8500_RTC,
  175. ab8500_rtc_alarm_regs[i], buf[i]);
  176. if (retval < 0)
  177. return retval;
  178. }
  179. return ab8500_rtc_irq_enable(dev, alarm->enabled);
  180. }
  181. static int ab8500_rtc_set_calibration(struct device *dev, int calibration)
  182. {
  183. int retval;
  184. u8 rtccal = 0;
  185. /*
  186. * Check that the calibration value (which is in units of 0.5
  187. * parts-per-million) is in the AB8500's range for RtcCalibration
  188. * register. -128 (0x80) is not permitted because the AB8500 uses
  189. * a sign-bit rather than two's complement, so 0x80 is just another
  190. * representation of zero.
  191. */
  192. if ((calibration < -127) || (calibration > 127)) {
  193. dev_err(dev, "RtcCalibration value outside permitted range\n");
  194. return -EINVAL;
  195. }
  196. /*
  197. * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
  198. * so need to convert to this sort of representation before writing
  199. * into RtcCalibration register...
  200. */
  201. if (calibration >= 0)
  202. rtccal = 0x7F & calibration;
  203. else
  204. rtccal = ~(calibration - 1) | 0x80;
  205. retval = abx500_set_register_interruptible(dev, AB8500_RTC,
  206. AB8500_RTC_CALIB_REG, rtccal);
  207. return retval;
  208. }
  209. static int ab8500_rtc_get_calibration(struct device *dev, int *calibration)
  210. {
  211. int retval;
  212. u8 rtccal = 0;
  213. retval = abx500_get_register_interruptible(dev, AB8500_RTC,
  214. AB8500_RTC_CALIB_REG, &rtccal);
  215. if (retval >= 0) {
  216. /*
  217. * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
  218. * so need to convert value from RtcCalibration register into
  219. * a two's complement signed value...
  220. */
  221. if (rtccal & 0x80)
  222. *calibration = 0 - (rtccal & 0x7F);
  223. else
  224. *calibration = 0x7F & rtccal;
  225. }
  226. return retval;
  227. }
  228. static ssize_t ab8500_sysfs_store_rtc_calibration(struct device *dev,
  229. struct device_attribute *attr,
  230. const char *buf, size_t count)
  231. {
  232. int retval;
  233. int calibration = 0;
  234. if (sscanf(buf, " %i ", &calibration) != 1) {
  235. dev_err(dev, "Failed to store RTC calibration attribute\n");
  236. return -EINVAL;
  237. }
  238. retval = ab8500_rtc_set_calibration(dev, calibration);
  239. return retval ? retval : count;
  240. }
  241. static ssize_t ab8500_sysfs_show_rtc_calibration(struct device *dev,
  242. struct device_attribute *attr, char *buf)
  243. {
  244. int retval = 0;
  245. int calibration = 0;
  246. retval = ab8500_rtc_get_calibration(dev, &calibration);
  247. if (retval < 0) {
  248. dev_err(dev, "Failed to read RTC calibration attribute\n");
  249. sprintf(buf, "0\n");
  250. return retval;
  251. }
  252. return sprintf(buf, "%d\n", calibration);
  253. }
  254. static DEVICE_ATTR(rtc_calibration, S_IRUGO | S_IWUSR,
  255. ab8500_sysfs_show_rtc_calibration,
  256. ab8500_sysfs_store_rtc_calibration);
  257. static struct attribute *ab8500_rtc_attrs[] = {
  258. &dev_attr_rtc_calibration.attr,
  259. NULL
  260. };
  261. static const struct attribute_group ab8500_rtc_sysfs_files = {
  262. .attrs = ab8500_rtc_attrs,
  263. };
  264. static irqreturn_t rtc_alarm_handler(int irq, void *data)
  265. {
  266. struct rtc_device *rtc = data;
  267. unsigned long events = RTC_IRQF | RTC_AF;
  268. dev_dbg(&rtc->dev, "%s\n", __func__);
  269. rtc_update_irq(rtc, 1, events);
  270. return IRQ_HANDLED;
  271. }
  272. static const struct rtc_class_ops ab8500_rtc_ops = {
  273. .read_time = ab8500_rtc_read_time,
  274. .set_time = ab8500_rtc_set_time,
  275. .read_alarm = ab8500_rtc_read_alarm,
  276. .set_alarm = ab8500_rtc_set_alarm,
  277. .alarm_irq_enable = ab8500_rtc_irq_enable,
  278. };
  279. static const struct platform_device_id ab85xx_rtc_ids[] = {
  280. { "ab8500-rtc", (kernel_ulong_t)&ab8500_rtc_ops, },
  281. { /* sentinel */ }
  282. };
  283. MODULE_DEVICE_TABLE(platform, ab85xx_rtc_ids);
  284. static int ab8500_rtc_probe(struct platform_device *pdev)
  285. {
  286. const struct platform_device_id *platid = platform_get_device_id(pdev);
  287. int err;
  288. struct rtc_device *rtc;
  289. u8 rtc_ctrl;
  290. int irq;
  291. irq = platform_get_irq_byname(pdev, "ALARM");
  292. if (irq < 0)
  293. return irq;
  294. /* For RTC supply test */
  295. err = abx500_mask_and_set_register_interruptible(&pdev->dev, AB8500_RTC,
  296. AB8500_RTC_STAT_REG, RTC_STATUS_DATA, RTC_STATUS_DATA);
  297. if (err < 0)
  298. return err;
  299. /* Wait for reset by the PorRtc */
  300. usleep_range(1000, 5000);
  301. err = abx500_get_register_interruptible(&pdev->dev, AB8500_RTC,
  302. AB8500_RTC_STAT_REG, &rtc_ctrl);
  303. if (err < 0)
  304. return err;
  305. /* Check if the RTC Supply fails */
  306. if (!(rtc_ctrl & RTC_STATUS_DATA)) {
  307. dev_err(&pdev->dev, "RTC supply failure\n");
  308. return -ENODEV;
  309. }
  310. device_init_wakeup(&pdev->dev, true);
  311. rtc = devm_rtc_allocate_device(&pdev->dev);
  312. if (IS_ERR(rtc))
  313. return PTR_ERR(rtc);
  314. rtc->ops = (struct rtc_class_ops *)platid->driver_data;
  315. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  316. rtc_alarm_handler, IRQF_ONESHOT,
  317. "ab8500-rtc", rtc);
  318. if (err < 0)
  319. return err;
  320. dev_pm_set_wake_irq(&pdev->dev, irq);
  321. platform_set_drvdata(pdev, rtc);
  322. rtc->uie_unsupported = 1;
  323. rtc->range_max = (1ULL << 24) * 60 - 1; // 24-bit minutes + 59 secs
  324. rtc->start_secs = RTC_TIMESTAMP_BEGIN_2000;
  325. rtc->set_start_time = true;
  326. err = rtc_add_group(rtc, &ab8500_rtc_sysfs_files);
  327. if (err)
  328. return err;
  329. return rtc_register_device(rtc);
  330. }
  331. static int ab8500_rtc_remove(struct platform_device *pdev)
  332. {
  333. dev_pm_clear_wake_irq(&pdev->dev);
  334. device_init_wakeup(&pdev->dev, false);
  335. return 0;
  336. }
  337. static struct platform_driver ab8500_rtc_driver = {
  338. .driver = {
  339. .name = "ab8500-rtc",
  340. },
  341. .probe = ab8500_rtc_probe,
  342. .remove = ab8500_rtc_remove,
  343. .id_table = ab85xx_rtc_ids,
  344. };
  345. module_platform_driver(ab8500_rtc_driver);
  346. MODULE_AUTHOR("Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>");
  347. MODULE_DESCRIPTION("AB8500 RTC Driver");
  348. MODULE_LICENSE("GPL v2");