rtc-ab8500.c 12 KB

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