rtc-ab8500.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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/ab8500.h>
  17. #include <linux/delay.h>
  18. #define AB8500_RTC_SOFF_STAT_REG 0x0F00
  19. #define AB8500_RTC_CC_CONF_REG 0x0F01
  20. #define AB8500_RTC_READ_REQ_REG 0x0F02
  21. #define AB8500_RTC_WATCH_TSECMID_REG 0x0F03
  22. #define AB8500_RTC_WATCH_TSECHI_REG 0x0F04
  23. #define AB8500_RTC_WATCH_TMIN_LOW_REG 0x0F05
  24. #define AB8500_RTC_WATCH_TMIN_MID_REG 0x0F06
  25. #define AB8500_RTC_WATCH_TMIN_HI_REG 0x0F07
  26. #define AB8500_RTC_ALRM_MIN_LOW_REG 0x0F08
  27. #define AB8500_RTC_ALRM_MIN_MID_REG 0x0F09
  28. #define AB8500_RTC_ALRM_MIN_HI_REG 0x0F0A
  29. #define AB8500_RTC_STAT_REG 0x0F0B
  30. #define AB8500_RTC_BKUP_CHG_REG 0x0F0C
  31. #define AB8500_RTC_FORCE_BKUP_REG 0x0F0D
  32. #define AB8500_RTC_CALIB_REG 0x0F0E
  33. #define AB8500_RTC_SWITCH_STAT_REG 0x0F0F
  34. #define AB8500_REV_REG 0x1080
  35. /* RtcReadRequest bits */
  36. #define RTC_READ_REQUEST 0x01
  37. #define RTC_WRITE_REQUEST 0x02
  38. /* RtcCtrl bits */
  39. #define RTC_ALARM_ENA 0x04
  40. #define RTC_STATUS_DATA 0x01
  41. #define COUNTS_PER_SEC (0xF000 / 60)
  42. #define AB8500_RTC_EPOCH 2000
  43. static const unsigned long ab8500_rtc_time_regs[] = {
  44. AB8500_RTC_WATCH_TMIN_HI_REG, AB8500_RTC_WATCH_TMIN_MID_REG,
  45. AB8500_RTC_WATCH_TMIN_LOW_REG, AB8500_RTC_WATCH_TSECHI_REG,
  46. AB8500_RTC_WATCH_TSECMID_REG
  47. };
  48. static const unsigned long ab8500_rtc_alarm_regs[] = {
  49. AB8500_RTC_ALRM_MIN_HI_REG, AB8500_RTC_ALRM_MIN_MID_REG,
  50. AB8500_RTC_ALRM_MIN_LOW_REG
  51. };
  52. /* Calculate the seconds from 1970 to 01-01-2000 00:00:00 */
  53. static unsigned long get_elapsed_seconds(int year)
  54. {
  55. unsigned long secs;
  56. struct rtc_time tm = {
  57. .tm_year = year - 1900,
  58. .tm_mday = 1,
  59. };
  60. /*
  61. * This function calculates secs from 1970 and not from
  62. * 1900, even if we supply the offset from year 1900.
  63. */
  64. rtc_tm_to_time(&tm, &secs);
  65. return secs;
  66. }
  67. static int ab8500_rtc_read_time(struct device *dev, struct rtc_time *tm)
  68. {
  69. struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
  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. /* Request a data read */
  75. retval = ab8500_write(ab8500, AB8500_RTC_READ_REQ_REG,
  76. RTC_READ_REQUEST);
  77. if (retval < 0)
  78. return retval;
  79. /* Early AB8500 chips will not clear the rtc read request bit */
  80. if (ab8500->revision == 0) {
  81. msleep(1);
  82. } else {
  83. /* Wait for some cycles after enabling the rtc read in ab8500 */
  84. while (time_before(jiffies, timeout)) {
  85. retval = ab8500_read(ab8500, AB8500_RTC_READ_REQ_REG);
  86. if (retval < 0)
  87. return retval;
  88. if (!(retval & RTC_READ_REQUEST))
  89. break;
  90. msleep(1);
  91. }
  92. }
  93. /* Read the Watchtime registers */
  94. for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
  95. retval = ab8500_read(ab8500, ab8500_rtc_time_regs[i]);
  96. if (retval < 0)
  97. return retval;
  98. buf[i] = retval;
  99. }
  100. mins = (buf[0] << 16) | (buf[1] << 8) | buf[2];
  101. secs = (buf[3] << 8) | buf[4];
  102. secs = secs / COUNTS_PER_SEC;
  103. secs = secs + (mins * 60);
  104. /* Add back the initially subtracted number of seconds */
  105. secs += get_elapsed_seconds(AB8500_RTC_EPOCH);
  106. rtc_time_to_tm(secs, tm);
  107. return rtc_valid_tm(tm);
  108. }
  109. static int ab8500_rtc_set_time(struct device *dev, struct rtc_time *tm)
  110. {
  111. struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
  112. int retval, i;
  113. unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
  114. unsigned long no_secs, no_mins, secs = 0;
  115. if (tm->tm_year < (AB8500_RTC_EPOCH - 1900)) {
  116. dev_dbg(dev, "year should be equal to or greater than %d\n",
  117. AB8500_RTC_EPOCH);
  118. return -EINVAL;
  119. }
  120. /* Get the number of seconds since 1970 */
  121. rtc_tm_to_time(tm, &secs);
  122. /*
  123. * Convert it to the number of seconds since 01-01-2000 00:00:00, since
  124. * we only have a small counter in the RTC.
  125. */
  126. secs -= get_elapsed_seconds(AB8500_RTC_EPOCH);
  127. no_mins = secs / 60;
  128. no_secs = secs % 60;
  129. /* Make the seconds count as per the RTC resolution */
  130. no_secs = no_secs * COUNTS_PER_SEC;
  131. buf[4] = no_secs & 0xFF;
  132. buf[3] = (no_secs >> 8) & 0xFF;
  133. buf[2] = no_mins & 0xFF;
  134. buf[1] = (no_mins >> 8) & 0xFF;
  135. buf[0] = (no_mins >> 16) & 0xFF;
  136. for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
  137. retval = ab8500_write(ab8500, ab8500_rtc_time_regs[i], buf[i]);
  138. if (retval < 0)
  139. return retval;
  140. }
  141. /* Request a data write */
  142. return ab8500_write(ab8500, 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. struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
  147. int retval, i;
  148. int rtc_ctrl;
  149. unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
  150. unsigned long secs, mins;
  151. /* Check if the alarm is enabled or not */
  152. rtc_ctrl = ab8500_read(ab8500, AB8500_RTC_STAT_REG);
  153. if (rtc_ctrl < 0)
  154. return rtc_ctrl;
  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 = ab8500_read(ab8500, ab8500_rtc_alarm_regs[i]);
  162. if (retval < 0)
  163. return retval;
  164. buf[i] = retval;
  165. }
  166. mins = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
  167. secs = mins * 60;
  168. /* Add back the initially subtracted number of seconds */
  169. secs += get_elapsed_seconds(AB8500_RTC_EPOCH);
  170. rtc_time_to_tm(secs, &alarm->time);
  171. return rtc_valid_tm(&alarm->time);
  172. }
  173. static int ab8500_rtc_irq_enable(struct device *dev, unsigned int enabled)
  174. {
  175. struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
  176. return ab8500_set_bits(ab8500, AB8500_RTC_STAT_REG, RTC_ALARM_ENA,
  177. enabled ? RTC_ALARM_ENA : 0);
  178. }
  179. static int ab8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  180. {
  181. struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
  182. int retval, i;
  183. unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
  184. unsigned long mins, secs = 0;
  185. if (alarm->time.tm_year < (AB8500_RTC_EPOCH - 1900)) {
  186. dev_dbg(dev, "year should be equal to or greater than %d\n",
  187. AB8500_RTC_EPOCH);
  188. return -EINVAL;
  189. }
  190. /* Get the number of seconds since 1970 */
  191. rtc_tm_to_time(&alarm->time, &secs);
  192. /*
  193. * Convert it to the number of seconds since 01-01-2000 00:00:00, since
  194. * we only have a small counter in the RTC.
  195. */
  196. secs -= get_elapsed_seconds(AB8500_RTC_EPOCH);
  197. mins = secs / 60;
  198. buf[2] = mins & 0xFF;
  199. buf[1] = (mins >> 8) & 0xFF;
  200. buf[0] = (mins >> 16) & 0xFF;
  201. /* Set the alarm time */
  202. for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
  203. retval = ab8500_write(ab8500, ab8500_rtc_alarm_regs[i], buf[i]);
  204. if (retval < 0)
  205. return retval;
  206. }
  207. return ab8500_rtc_irq_enable(dev, alarm->enabled);
  208. }
  209. static irqreturn_t rtc_alarm_handler(int irq, void *data)
  210. {
  211. struct rtc_device *rtc = data;
  212. unsigned long events = RTC_IRQF | RTC_AF;
  213. dev_dbg(&rtc->dev, "%s\n", __func__);
  214. rtc_update_irq(rtc, 1, events);
  215. return IRQ_HANDLED;
  216. }
  217. static const struct rtc_class_ops ab8500_rtc_ops = {
  218. .read_time = ab8500_rtc_read_time,
  219. .set_time = ab8500_rtc_set_time,
  220. .read_alarm = ab8500_rtc_read_alarm,
  221. .set_alarm = ab8500_rtc_set_alarm,
  222. .alarm_irq_enable = ab8500_rtc_irq_enable,
  223. };
  224. static int __devinit ab8500_rtc_probe(struct platform_device *pdev)
  225. {
  226. struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
  227. int err;
  228. struct rtc_device *rtc;
  229. int rtc_ctrl;
  230. int irq;
  231. irq = platform_get_irq_byname(pdev, "ALARM");
  232. if (irq < 0)
  233. return irq;
  234. /* For RTC supply test */
  235. err = ab8500_set_bits(ab8500, AB8500_RTC_STAT_REG, RTC_STATUS_DATA,
  236. RTC_STATUS_DATA);
  237. if (err < 0)
  238. return err;
  239. /* Wait for reset by the PorRtc */
  240. msleep(1);
  241. rtc_ctrl = ab8500_read(ab8500, AB8500_RTC_STAT_REG);
  242. if (rtc_ctrl < 0)
  243. return rtc_ctrl;
  244. /* Check if the RTC Supply fails */
  245. if (!(rtc_ctrl & RTC_STATUS_DATA)) {
  246. dev_err(&pdev->dev, "RTC supply failure\n");
  247. return -ENODEV;
  248. }
  249. rtc = rtc_device_register("ab8500-rtc", &pdev->dev, &ab8500_rtc_ops,
  250. THIS_MODULE);
  251. if (IS_ERR(rtc)) {
  252. dev_err(&pdev->dev, "Registration failed\n");
  253. err = PTR_ERR(rtc);
  254. return err;
  255. }
  256. err = request_threaded_irq(irq, NULL, rtc_alarm_handler, 0,
  257. "ab8500-rtc", rtc);
  258. if (err < 0) {
  259. rtc_device_unregister(rtc);
  260. return err;
  261. }
  262. platform_set_drvdata(pdev, rtc);
  263. return 0;
  264. }
  265. static int __devexit ab8500_rtc_remove(struct platform_device *pdev)
  266. {
  267. struct rtc_device *rtc = platform_get_drvdata(pdev);
  268. int irq = platform_get_irq_byname(pdev, "ALARM");
  269. free_irq(irq, rtc);
  270. rtc_device_unregister(rtc);
  271. platform_set_drvdata(pdev, NULL);
  272. return 0;
  273. }
  274. static struct platform_driver ab8500_rtc_driver = {
  275. .driver = {
  276. .name = "ab8500-rtc",
  277. .owner = THIS_MODULE,
  278. },
  279. .probe = ab8500_rtc_probe,
  280. .remove = __devexit_p(ab8500_rtc_remove),
  281. };
  282. static int __init ab8500_rtc_init(void)
  283. {
  284. return platform_driver_register(&ab8500_rtc_driver);
  285. }
  286. static void __exit ab8500_rtc_exit(void)
  287. {
  288. platform_driver_unregister(&ab8500_rtc_driver);
  289. }
  290. module_init(ab8500_rtc_init);
  291. module_exit(ab8500_rtc_exit);
  292. MODULE_AUTHOR("Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>");
  293. MODULE_DESCRIPTION("AB8500 RTC Driver");
  294. MODULE_LICENSE("GPL v2");