rtc-ab8500.c 14 KB

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