rtc-ab8500.c 12 KB

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