rtc-ab8500.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) ST-Ericsson SA 2010
  4. *
  5. * Author: Virupax Sadashivpetimath <[email protected]>
  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 <[email protected]>
  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;
  154. mins = (unsigned long)rtc_tm_to_time64(&alarm->time) / 60;
  155. buf[2] = mins & 0xFF;
  156. buf[1] = (mins >> 8) & 0xFF;
  157. buf[0] = (mins >> 16) & 0xFF;
  158. /* Set the alarm time */
  159. for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
  160. retval = abx500_set_register_interruptible(dev, AB8500_RTC,
  161. ab8500_rtc_alarm_regs[i], buf[i]);
  162. if (retval < 0)
  163. return retval;
  164. }
  165. return ab8500_rtc_irq_enable(dev, alarm->enabled);
  166. }
  167. static int ab8500_rtc_set_calibration(struct device *dev, int calibration)
  168. {
  169. int retval;
  170. u8 rtccal = 0;
  171. /*
  172. * Check that the calibration value (which is in units of 0.5
  173. * parts-per-million) is in the AB8500's range for RtcCalibration
  174. * register. -128 (0x80) is not permitted because the AB8500 uses
  175. * a sign-bit rather than two's complement, so 0x80 is just another
  176. * representation of zero.
  177. */
  178. if ((calibration < -127) || (calibration > 127)) {
  179. dev_err(dev, "RtcCalibration value outside permitted range\n");
  180. return -EINVAL;
  181. }
  182. /*
  183. * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
  184. * so need to convert to this sort of representation before writing
  185. * into RtcCalibration register...
  186. */
  187. if (calibration >= 0)
  188. rtccal = 0x7F & calibration;
  189. else
  190. rtccal = ~(calibration - 1) | 0x80;
  191. retval = abx500_set_register_interruptible(dev, AB8500_RTC,
  192. AB8500_RTC_CALIB_REG, rtccal);
  193. return retval;
  194. }
  195. static int ab8500_rtc_get_calibration(struct device *dev, int *calibration)
  196. {
  197. int retval;
  198. u8 rtccal = 0;
  199. retval = abx500_get_register_interruptible(dev, AB8500_RTC,
  200. AB8500_RTC_CALIB_REG, &rtccal);
  201. if (retval >= 0) {
  202. /*
  203. * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
  204. * so need to convert value from RtcCalibration register into
  205. * a two's complement signed value...
  206. */
  207. if (rtccal & 0x80)
  208. *calibration = 0 - (rtccal & 0x7F);
  209. else
  210. *calibration = 0x7F & rtccal;
  211. }
  212. return retval;
  213. }
  214. static ssize_t ab8500_sysfs_store_rtc_calibration(struct device *dev,
  215. struct device_attribute *attr,
  216. const char *buf, size_t count)
  217. {
  218. int retval;
  219. int calibration = 0;
  220. if (sscanf(buf, " %i ", &calibration) != 1) {
  221. dev_err(dev, "Failed to store RTC calibration attribute\n");
  222. return -EINVAL;
  223. }
  224. retval = ab8500_rtc_set_calibration(dev, calibration);
  225. return retval ? retval : count;
  226. }
  227. static ssize_t ab8500_sysfs_show_rtc_calibration(struct device *dev,
  228. struct device_attribute *attr, char *buf)
  229. {
  230. int retval = 0;
  231. int calibration = 0;
  232. retval = ab8500_rtc_get_calibration(dev, &calibration);
  233. if (retval < 0) {
  234. dev_err(dev, "Failed to read RTC calibration attribute\n");
  235. sprintf(buf, "0\n");
  236. return retval;
  237. }
  238. return sprintf(buf, "%d\n", calibration);
  239. }
  240. static DEVICE_ATTR(rtc_calibration, S_IRUGO | S_IWUSR,
  241. ab8500_sysfs_show_rtc_calibration,
  242. ab8500_sysfs_store_rtc_calibration);
  243. static struct attribute *ab8500_rtc_attrs[] = {
  244. &dev_attr_rtc_calibration.attr,
  245. NULL
  246. };
  247. static const struct attribute_group ab8500_rtc_sysfs_files = {
  248. .attrs = ab8500_rtc_attrs,
  249. };
  250. static irqreturn_t rtc_alarm_handler(int irq, void *data)
  251. {
  252. struct rtc_device *rtc = data;
  253. unsigned long events = RTC_IRQF | RTC_AF;
  254. dev_dbg(&rtc->dev, "%s\n", __func__);
  255. rtc_update_irq(rtc, 1, events);
  256. return IRQ_HANDLED;
  257. }
  258. static const struct rtc_class_ops ab8500_rtc_ops = {
  259. .read_time = ab8500_rtc_read_time,
  260. .set_time = ab8500_rtc_set_time,
  261. .read_alarm = ab8500_rtc_read_alarm,
  262. .set_alarm = ab8500_rtc_set_alarm,
  263. .alarm_irq_enable = ab8500_rtc_irq_enable,
  264. };
  265. static const struct platform_device_id ab85xx_rtc_ids[] = {
  266. { "ab8500-rtc", (kernel_ulong_t)&ab8500_rtc_ops, },
  267. { /* sentinel */ }
  268. };
  269. MODULE_DEVICE_TABLE(platform, ab85xx_rtc_ids);
  270. static int ab8500_rtc_probe(struct platform_device *pdev)
  271. {
  272. const struct platform_device_id *platid = platform_get_device_id(pdev);
  273. int err;
  274. struct rtc_device *rtc;
  275. u8 rtc_ctrl;
  276. int irq;
  277. irq = platform_get_irq_byname(pdev, "ALARM");
  278. if (irq < 0)
  279. return irq;
  280. /* For RTC supply test */
  281. err = abx500_mask_and_set_register_interruptible(&pdev->dev, AB8500_RTC,
  282. AB8500_RTC_STAT_REG, RTC_STATUS_DATA, RTC_STATUS_DATA);
  283. if (err < 0)
  284. return err;
  285. /* Wait for reset by the PorRtc */
  286. usleep_range(1000, 5000);
  287. err = abx500_get_register_interruptible(&pdev->dev, AB8500_RTC,
  288. AB8500_RTC_STAT_REG, &rtc_ctrl);
  289. if (err < 0)
  290. return err;
  291. /* Check if the RTC Supply fails */
  292. if (!(rtc_ctrl & RTC_STATUS_DATA)) {
  293. dev_err(&pdev->dev, "RTC supply failure\n");
  294. return -ENODEV;
  295. }
  296. device_init_wakeup(&pdev->dev, true);
  297. rtc = devm_rtc_allocate_device(&pdev->dev);
  298. if (IS_ERR(rtc))
  299. return PTR_ERR(rtc);
  300. rtc->ops = (struct rtc_class_ops *)platid->driver_data;
  301. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  302. rtc_alarm_handler, IRQF_ONESHOT,
  303. "ab8500-rtc", rtc);
  304. if (err < 0)
  305. return err;
  306. dev_pm_set_wake_irq(&pdev->dev, irq);
  307. platform_set_drvdata(pdev, rtc);
  308. set_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->features);
  309. clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features);
  310. rtc->range_max = (1ULL << 24) * 60 - 1; // 24-bit minutes + 59 secs
  311. rtc->start_secs = RTC_TIMESTAMP_BEGIN_2000;
  312. rtc->set_start_time = true;
  313. err = rtc_add_group(rtc, &ab8500_rtc_sysfs_files);
  314. if (err)
  315. return err;
  316. return devm_rtc_register_device(rtc);
  317. }
  318. static int ab8500_rtc_remove(struct platform_device *pdev)
  319. {
  320. dev_pm_clear_wake_irq(&pdev->dev);
  321. device_init_wakeup(&pdev->dev, false);
  322. return 0;
  323. }
  324. static struct platform_driver ab8500_rtc_driver = {
  325. .driver = {
  326. .name = "ab8500-rtc",
  327. },
  328. .probe = ab8500_rtc_probe,
  329. .remove = ab8500_rtc_remove,
  330. .id_table = ab85xx_rtc_ids,
  331. };
  332. module_platform_driver(ab8500_rtc_driver);
  333. MODULE_AUTHOR("Virupax Sadashivpetimath <[email protected]>");
  334. MODULE_DESCRIPTION("AB8500 RTC Driver");
  335. MODULE_LICENSE("GPL v2");