rtc-ab8500.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * License terms: GNU General Public License (GPL) version 2
  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. #define AB8540_RTC_ALRM_SEC 0x22
  38. #define AB8540_RTC_ALRM_MIN_LOW_REG 0x23
  39. #define AB8540_RTC_ALRM_MIN_MID_REG 0x24
  40. #define AB8540_RTC_ALRM_MIN_HI_REG 0x25
  41. /* RtcReadRequest bits */
  42. #define RTC_READ_REQUEST 0x01
  43. #define RTC_WRITE_REQUEST 0x02
  44. /* RtcCtrl bits */
  45. #define RTC_ALARM_ENA 0x04
  46. #define RTC_STATUS_DATA 0x01
  47. #define COUNTS_PER_SEC (0xF000 / 60)
  48. #define AB8500_RTC_EPOCH 2000
  49. static const u8 ab8500_rtc_time_regs[] = {
  50. AB8500_RTC_WATCH_TMIN_HI_REG, AB8500_RTC_WATCH_TMIN_MID_REG,
  51. AB8500_RTC_WATCH_TMIN_LOW_REG, AB8500_RTC_WATCH_TSECHI_REG,
  52. AB8500_RTC_WATCH_TSECMID_REG
  53. };
  54. static const u8 ab8500_rtc_alarm_regs[] = {
  55. AB8500_RTC_ALRM_MIN_HI_REG, AB8500_RTC_ALRM_MIN_MID_REG,
  56. AB8500_RTC_ALRM_MIN_LOW_REG
  57. };
  58. static const u8 ab8540_rtc_alarm_regs[] = {
  59. AB8540_RTC_ALRM_MIN_HI_REG, AB8540_RTC_ALRM_MIN_MID_REG,
  60. AB8540_RTC_ALRM_MIN_LOW_REG, AB8540_RTC_ALRM_SEC
  61. };
  62. /* Calculate the seconds from 1970 to 01-01-2000 00:00:00 */
  63. static unsigned long get_elapsed_seconds(int year)
  64. {
  65. unsigned long secs;
  66. struct rtc_time tm = {
  67. .tm_year = year - 1900,
  68. .tm_mday = 1,
  69. };
  70. /*
  71. * This function calculates secs from 1970 and not from
  72. * 1900, even if we supply the offset from year 1900.
  73. */
  74. rtc_tm_to_time(&tm, &secs);
  75. return secs;
  76. }
  77. static int ab8500_rtc_read_time(struct device *dev, struct rtc_time *tm)
  78. {
  79. unsigned long timeout = jiffies + HZ;
  80. int retval, i;
  81. unsigned long mins, secs;
  82. unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
  83. u8 value;
  84. /* Request a data read */
  85. retval = abx500_set_register_interruptible(dev,
  86. AB8500_RTC, AB8500_RTC_READ_REQ_REG, RTC_READ_REQUEST);
  87. if (retval < 0)
  88. return retval;
  89. /* Wait for some cycles after enabling the rtc read in ab8500 */
  90. while (time_before(jiffies, timeout)) {
  91. retval = abx500_get_register_interruptible(dev,
  92. AB8500_RTC, AB8500_RTC_READ_REQ_REG, &value);
  93. if (retval < 0)
  94. return retval;
  95. if (!(value & RTC_READ_REQUEST))
  96. break;
  97. usleep_range(1000, 5000);
  98. }
  99. /* Read the Watchtime registers */
  100. for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
  101. retval = abx500_get_register_interruptible(dev,
  102. AB8500_RTC, ab8500_rtc_time_regs[i], &value);
  103. if (retval < 0)
  104. return retval;
  105. buf[i] = value;
  106. }
  107. mins = (buf[0] << 16) | (buf[1] << 8) | buf[2];
  108. secs = (buf[3] << 8) | buf[4];
  109. secs = secs / COUNTS_PER_SEC;
  110. secs = secs + (mins * 60);
  111. /* Add back the initially subtracted number of seconds */
  112. secs += get_elapsed_seconds(AB8500_RTC_EPOCH);
  113. rtc_time_to_tm(secs, tm);
  114. return rtc_valid_tm(tm);
  115. }
  116. static int ab8500_rtc_set_time(struct device *dev, struct rtc_time *tm)
  117. {
  118. int retval, i;
  119. unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
  120. unsigned long no_secs, no_mins, secs = 0;
  121. if (tm->tm_year < (AB8500_RTC_EPOCH - 1900)) {
  122. dev_dbg(dev, "year should be equal to or greater than %d\n",
  123. AB8500_RTC_EPOCH);
  124. return -EINVAL;
  125. }
  126. /* Get the number of seconds since 1970 */
  127. rtc_tm_to_time(tm, &secs);
  128. /*
  129. * Convert it to the number of seconds since 01-01-2000 00:00:00, since
  130. * we only have a small counter in the RTC.
  131. */
  132. secs -= get_elapsed_seconds(AB8500_RTC_EPOCH);
  133. no_mins = secs / 60;
  134. no_secs = secs % 60;
  135. /* Make the seconds count as per the RTC resolution */
  136. no_secs = no_secs * COUNTS_PER_SEC;
  137. buf[4] = no_secs & 0xFF;
  138. buf[3] = (no_secs >> 8) & 0xFF;
  139. buf[2] = no_mins & 0xFF;
  140. buf[1] = (no_mins >> 8) & 0xFF;
  141. buf[0] = (no_mins >> 16) & 0xFF;
  142. for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
  143. retval = abx500_set_register_interruptible(dev, AB8500_RTC,
  144. ab8500_rtc_time_regs[i], buf[i]);
  145. if (retval < 0)
  146. return retval;
  147. }
  148. /* Request a data write */
  149. return abx500_set_register_interruptible(dev, AB8500_RTC,
  150. AB8500_RTC_READ_REQ_REG, RTC_WRITE_REQUEST);
  151. }
  152. static int ab8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  153. {
  154. int retval, i;
  155. u8 rtc_ctrl, value;
  156. unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
  157. unsigned long secs, mins;
  158. /* Check if the alarm is enabled or not */
  159. retval = abx500_get_register_interruptible(dev, AB8500_RTC,
  160. AB8500_RTC_STAT_REG, &rtc_ctrl);
  161. if (retval < 0)
  162. return retval;
  163. if (rtc_ctrl & RTC_ALARM_ENA)
  164. alarm->enabled = 1;
  165. else
  166. alarm->enabled = 0;
  167. alarm->pending = 0;
  168. for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
  169. retval = abx500_get_register_interruptible(dev, AB8500_RTC,
  170. ab8500_rtc_alarm_regs[i], &value);
  171. if (retval < 0)
  172. return retval;
  173. buf[i] = value;
  174. }
  175. mins = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
  176. secs = mins * 60;
  177. /* Add back the initially subtracted number of seconds */
  178. secs += get_elapsed_seconds(AB8500_RTC_EPOCH);
  179. rtc_time_to_tm(secs, &alarm->time);
  180. return rtc_valid_tm(&alarm->time);
  181. }
  182. static int ab8500_rtc_irq_enable(struct device *dev, unsigned int enabled)
  183. {
  184. return abx500_mask_and_set_register_interruptible(dev, AB8500_RTC,
  185. AB8500_RTC_STAT_REG, RTC_ALARM_ENA,
  186. enabled ? RTC_ALARM_ENA : 0);
  187. }
  188. static int ab8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  189. {
  190. int retval, i;
  191. unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
  192. unsigned long mins, secs = 0, cursec = 0;
  193. struct rtc_time curtm;
  194. if (alarm->time.tm_year < (AB8500_RTC_EPOCH - 1900)) {
  195. dev_dbg(dev, "year should be equal to or greater than %d\n",
  196. AB8500_RTC_EPOCH);
  197. return -EINVAL;
  198. }
  199. /* Get the number of seconds since 1970 */
  200. rtc_tm_to_time(&alarm->time, &secs);
  201. /*
  202. * Check whether alarm is set less than 1min.
  203. * Since our RTC doesn't support alarm resolution less than 1min,
  204. * return -EINVAL, so UIE EMUL can take it up, incase of UIE_ON
  205. */
  206. ab8500_rtc_read_time(dev, &curtm); /* Read current time */
  207. rtc_tm_to_time(&curtm, &cursec);
  208. if ((secs - cursec) < 59) {
  209. dev_dbg(dev, "Alarm less than 1 minute not supported\r\n");
  210. return -EINVAL;
  211. }
  212. /*
  213. * Convert it to the number of seconds since 01-01-2000 00:00:00, since
  214. * we only have a small counter in the RTC.
  215. */
  216. secs -= get_elapsed_seconds(AB8500_RTC_EPOCH);
  217. mins = secs / 60;
  218. buf[2] = mins & 0xFF;
  219. buf[1] = (mins >> 8) & 0xFF;
  220. buf[0] = (mins >> 16) & 0xFF;
  221. /* Set the alarm time */
  222. for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
  223. retval = abx500_set_register_interruptible(dev, AB8500_RTC,
  224. ab8500_rtc_alarm_regs[i], buf[i]);
  225. if (retval < 0)
  226. return retval;
  227. }
  228. return ab8500_rtc_irq_enable(dev, alarm->enabled);
  229. }
  230. static int ab8540_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  231. {
  232. int retval, i;
  233. unsigned char buf[ARRAY_SIZE(ab8540_rtc_alarm_regs)];
  234. unsigned long mins, secs = 0;
  235. if (alarm->time.tm_year < (AB8500_RTC_EPOCH - 1900)) {
  236. dev_dbg(dev, "year should be equal to or greater than %d\n",
  237. AB8500_RTC_EPOCH);
  238. return -EINVAL;
  239. }
  240. /* Get the number of seconds since 1970 */
  241. rtc_tm_to_time(&alarm->time, &secs);
  242. /*
  243. * Convert it to the number of seconds since 01-01-2000 00:00:00
  244. */
  245. secs -= get_elapsed_seconds(AB8500_RTC_EPOCH);
  246. mins = secs / 60;
  247. buf[3] = secs % 60;
  248. buf[2] = mins & 0xFF;
  249. buf[1] = (mins >> 8) & 0xFF;
  250. buf[0] = (mins >> 16) & 0xFF;
  251. /* Set the alarm time */
  252. for (i = 0; i < ARRAY_SIZE(ab8540_rtc_alarm_regs); i++) {
  253. retval = abx500_set_register_interruptible(dev, AB8500_RTC,
  254. ab8540_rtc_alarm_regs[i], buf[i]);
  255. if (retval < 0)
  256. return retval;
  257. }
  258. return ab8500_rtc_irq_enable(dev, alarm->enabled);
  259. }
  260. static int ab8500_rtc_set_calibration(struct device *dev, int calibration)
  261. {
  262. int retval;
  263. u8 rtccal = 0;
  264. /*
  265. * Check that the calibration value (which is in units of 0.5
  266. * parts-per-million) is in the AB8500's range for RtcCalibration
  267. * register. -128 (0x80) is not permitted because the AB8500 uses
  268. * a sign-bit rather than two's complement, so 0x80 is just another
  269. * representation of zero.
  270. */
  271. if ((calibration < -127) || (calibration > 127)) {
  272. dev_err(dev, "RtcCalibration value outside permitted range\n");
  273. return -EINVAL;
  274. }
  275. /*
  276. * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
  277. * so need to convert to this sort of representation before writing
  278. * into RtcCalibration register...
  279. */
  280. if (calibration >= 0)
  281. rtccal = 0x7F & calibration;
  282. else
  283. rtccal = ~(calibration - 1) | 0x80;
  284. retval = abx500_set_register_interruptible(dev, AB8500_RTC,
  285. AB8500_RTC_CALIB_REG, rtccal);
  286. return retval;
  287. }
  288. static int ab8500_rtc_get_calibration(struct device *dev, int *calibration)
  289. {
  290. int retval;
  291. u8 rtccal = 0;
  292. retval = abx500_get_register_interruptible(dev, AB8500_RTC,
  293. AB8500_RTC_CALIB_REG, &rtccal);
  294. if (retval >= 0) {
  295. /*
  296. * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
  297. * so need to convert value from RtcCalibration register into
  298. * a two's complement signed value...
  299. */
  300. if (rtccal & 0x80)
  301. *calibration = 0 - (rtccal & 0x7F);
  302. else
  303. *calibration = 0x7F & rtccal;
  304. }
  305. return retval;
  306. }
  307. static ssize_t ab8500_sysfs_store_rtc_calibration(struct device *dev,
  308. struct device_attribute *attr,
  309. const char *buf, size_t count)
  310. {
  311. int retval;
  312. int calibration = 0;
  313. if (sscanf(buf, " %i ", &calibration) != 1) {
  314. dev_err(dev, "Failed to store RTC calibration attribute\n");
  315. return -EINVAL;
  316. }
  317. retval = ab8500_rtc_set_calibration(dev, calibration);
  318. return retval ? retval : count;
  319. }
  320. static ssize_t ab8500_sysfs_show_rtc_calibration(struct device *dev,
  321. struct device_attribute *attr, char *buf)
  322. {
  323. int retval = 0;
  324. int calibration = 0;
  325. retval = ab8500_rtc_get_calibration(dev, &calibration);
  326. if (retval < 0) {
  327. dev_err(dev, "Failed to read RTC calibration attribute\n");
  328. sprintf(buf, "0\n");
  329. return retval;
  330. }
  331. return sprintf(buf, "%d\n", calibration);
  332. }
  333. static DEVICE_ATTR(rtc_calibration, S_IRUGO | S_IWUSR,
  334. ab8500_sysfs_show_rtc_calibration,
  335. ab8500_sysfs_store_rtc_calibration);
  336. static int ab8500_sysfs_rtc_register(struct device *dev)
  337. {
  338. return device_create_file(dev, &dev_attr_rtc_calibration);
  339. }
  340. static void ab8500_sysfs_rtc_unregister(struct device *dev)
  341. {
  342. device_remove_file(dev, &dev_attr_rtc_calibration);
  343. }
  344. static irqreturn_t rtc_alarm_handler(int irq, void *data)
  345. {
  346. struct rtc_device *rtc = data;
  347. unsigned long events = RTC_IRQF | RTC_AF;
  348. dev_dbg(&rtc->dev, "%s\n", __func__);
  349. rtc_update_irq(rtc, 1, events);
  350. return IRQ_HANDLED;
  351. }
  352. static const struct rtc_class_ops ab8500_rtc_ops = {
  353. .read_time = ab8500_rtc_read_time,
  354. .set_time = ab8500_rtc_set_time,
  355. .read_alarm = ab8500_rtc_read_alarm,
  356. .set_alarm = ab8500_rtc_set_alarm,
  357. .alarm_irq_enable = ab8500_rtc_irq_enable,
  358. };
  359. static const struct rtc_class_ops ab8540_rtc_ops = {
  360. .read_time = ab8500_rtc_read_time,
  361. .set_time = ab8500_rtc_set_time,
  362. .read_alarm = ab8500_rtc_read_alarm,
  363. .set_alarm = ab8540_rtc_set_alarm,
  364. .alarm_irq_enable = ab8500_rtc_irq_enable,
  365. };
  366. static const struct platform_device_id ab85xx_rtc_ids[] = {
  367. { "ab8500-rtc", (kernel_ulong_t)&ab8500_rtc_ops, },
  368. { "ab8540-rtc", (kernel_ulong_t)&ab8540_rtc_ops, },
  369. { /* sentinel */ }
  370. };
  371. MODULE_DEVICE_TABLE(platform, ab85xx_rtc_ids);
  372. static int ab8500_rtc_probe(struct platform_device *pdev)
  373. {
  374. const struct platform_device_id *platid = platform_get_device_id(pdev);
  375. int err;
  376. struct rtc_device *rtc;
  377. u8 rtc_ctrl;
  378. int irq;
  379. irq = platform_get_irq_byname(pdev, "ALARM");
  380. if (irq < 0)
  381. return irq;
  382. /* For RTC supply test */
  383. err = abx500_mask_and_set_register_interruptible(&pdev->dev, AB8500_RTC,
  384. AB8500_RTC_STAT_REG, RTC_STATUS_DATA, RTC_STATUS_DATA);
  385. if (err < 0)
  386. return err;
  387. /* Wait for reset by the PorRtc */
  388. usleep_range(1000, 5000);
  389. err = abx500_get_register_interruptible(&pdev->dev, AB8500_RTC,
  390. AB8500_RTC_STAT_REG, &rtc_ctrl);
  391. if (err < 0)
  392. return err;
  393. /* Check if the RTC Supply fails */
  394. if (!(rtc_ctrl & RTC_STATUS_DATA)) {
  395. dev_err(&pdev->dev, "RTC supply failure\n");
  396. return -ENODEV;
  397. }
  398. device_init_wakeup(&pdev->dev, true);
  399. rtc = devm_rtc_device_register(&pdev->dev, "ab8500-rtc",
  400. (struct rtc_class_ops *)platid->driver_data,
  401. THIS_MODULE);
  402. if (IS_ERR(rtc)) {
  403. dev_err(&pdev->dev, "Registration failed\n");
  404. err = PTR_ERR(rtc);
  405. return err;
  406. }
  407. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  408. rtc_alarm_handler, IRQF_ONESHOT,
  409. "ab8500-rtc", rtc);
  410. if (err < 0)
  411. return err;
  412. dev_pm_set_wake_irq(&pdev->dev, irq);
  413. platform_set_drvdata(pdev, rtc);
  414. err = ab8500_sysfs_rtc_register(&pdev->dev);
  415. if (err) {
  416. dev_err(&pdev->dev, "sysfs RTC failed to register\n");
  417. return err;
  418. }
  419. rtc->uie_unsupported = 1;
  420. return 0;
  421. }
  422. static int ab8500_rtc_remove(struct platform_device *pdev)
  423. {
  424. dev_pm_clear_wake_irq(&pdev->dev);
  425. device_init_wakeup(&pdev->dev, false);
  426. ab8500_sysfs_rtc_unregister(&pdev->dev);
  427. return 0;
  428. }
  429. static struct platform_driver ab8500_rtc_driver = {
  430. .driver = {
  431. .name = "ab8500-rtc",
  432. },
  433. .probe = ab8500_rtc_probe,
  434. .remove = ab8500_rtc_remove,
  435. .id_table = ab85xx_rtc_ids,
  436. };
  437. module_platform_driver(ab8500_rtc_driver);
  438. MODULE_AUTHOR("Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>");
  439. MODULE_DESCRIPTION("AB8500 RTC Driver");
  440. MODULE_LICENSE("GPL v2");