PageRenderTime 235ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 9ms

/drivers/rtc/rtc-ab8500.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2
C | 366 lines | 258 code | 68 blank | 40 comment | 25 complexity | 937bb437446839d58dbfd7020a96566e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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/ab8500.h>
  18. #include <linux/delay.h>
  19. #define AB8500_RTC_SOFF_STAT_REG 0x00
  20. #define AB8500_RTC_CC_CONF_REG 0x01
  21. #define AB8500_RTC_READ_REQ_REG 0x02
  22. #define AB8500_RTC_WATCH_TSECMID_REG 0x03
  23. #define AB8500_RTC_WATCH_TSECHI_REG 0x04
  24. #define AB8500_RTC_WATCH_TMIN_LOW_REG 0x05
  25. #define AB8500_RTC_WATCH_TMIN_MID_REG 0x06
  26. #define AB8500_RTC_WATCH_TMIN_HI_REG 0x07
  27. #define AB8500_RTC_ALRM_MIN_LOW_REG 0x08
  28. #define AB8500_RTC_ALRM_MIN_MID_REG 0x09
  29. #define AB8500_RTC_ALRM_MIN_HI_REG 0x0A
  30. #define AB8500_RTC_STAT_REG 0x0B
  31. #define AB8500_RTC_BKUP_CHG_REG 0x0C
  32. #define AB8500_RTC_FORCE_BKUP_REG 0x0D
  33. #define AB8500_RTC_CALIB_REG 0x0E
  34. #define AB8500_RTC_SWITCH_STAT_REG 0x0F
  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 u8 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 u8 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. unsigned long timeout = jiffies + HZ;
  70. int retval, i;
  71. unsigned long mins, secs;
  72. unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
  73. u8 value;
  74. /* Request a data read */
  75. retval = abx500_set_register_interruptible(dev,
  76. AB8500_RTC, AB8500_RTC_READ_REQ_REG, RTC_READ_REQUEST);
  77. if (retval < 0)
  78. return retval;
  79. /* Early AB8500 chips will not clear the rtc read request bit */
  80. if (abx500_get_chip_id(dev) == 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 = abx500_get_register_interruptible(dev,
  86. AB8500_RTC, AB8500_RTC_READ_REQ_REG, &value);
  87. if (retval < 0)
  88. return retval;
  89. if (!(value & RTC_READ_REQUEST))
  90. break;
  91. msleep(1);
  92. }
  93. }
  94. /* Read the Watchtime registers */
  95. for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
  96. retval = abx500_get_register_interruptible(dev,
  97. AB8500_RTC, ab8500_rtc_time_regs[i], &value);
  98. if (retval < 0)
  99. return retval;
  100. buf[i] = value;
  101. }
  102. mins = (buf[0] << 16) | (buf[1] << 8) | buf[2];
  103. secs = (buf[3] << 8) | buf[4];
  104. secs = secs / COUNTS_PER_SEC;
  105. secs = secs + (mins * 60);
  106. /* Add back the initially subtracted number of seconds */
  107. secs += get_elapsed_seconds(AB8500_RTC_EPOCH);
  108. rtc_time_to_tm(secs, tm);
  109. return rtc_valid_tm(tm);
  110. }
  111. static int ab8500_rtc_set_time(struct device *dev, struct rtc_time *tm)
  112. {
  113. int retval, i;
  114. unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
  115. unsigned long no_secs, no_mins, secs = 0;
  116. if (tm->tm_year < (AB8500_RTC_EPOCH - 1900)) {
  117. dev_dbg(dev, "year should be equal to or greater than %d\n",
  118. AB8500_RTC_EPOCH);
  119. return -EINVAL;
  120. }
  121. /* Get the number of seconds since 1970 */
  122. rtc_tm_to_time(tm, &secs);
  123. /*
  124. * Convert it to the number of seconds since 01-01-2000 00:00:00, since
  125. * we only have a small counter in the RTC.
  126. */
  127. secs -= get_elapsed_seconds(AB8500_RTC_EPOCH);
  128. no_mins = secs / 60;
  129. no_secs = secs % 60;
  130. /* Make the seconds count as per the RTC resolution */
  131. no_secs = no_secs * COUNTS_PER_SEC;
  132. buf[4] = no_secs & 0xFF;
  133. buf[3] = (no_secs >> 8) & 0xFF;
  134. buf[2] = no_mins & 0xFF;
  135. buf[1] = (no_mins >> 8) & 0xFF;
  136. buf[0] = (no_mins >> 16) & 0xFF;
  137. for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
  138. retval = abx500_set_register_interruptible(dev, AB8500_RTC,
  139. ab8500_rtc_time_regs[i], buf[i]);
  140. if (retval < 0)
  141. return retval;
  142. }
  143. /* Request a data write */
  144. return abx500_set_register_interruptible(dev, AB8500_RTC,
  145. AB8500_RTC_READ_REQ_REG, RTC_WRITE_REQUEST);
  146. }
  147. static int ab8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  148. {
  149. int retval, i;
  150. u8 rtc_ctrl, value;
  151. unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
  152. unsigned long secs, mins;
  153. /* Check if the alarm is enabled or not */
  154. retval = abx500_get_register_interruptible(dev, AB8500_RTC,
  155. AB8500_RTC_STAT_REG, &rtc_ctrl);
  156. if (retval < 0)
  157. return retval;
  158. if (rtc_ctrl & RTC_ALARM_ENA)
  159. alarm->enabled = 1;
  160. else
  161. alarm->enabled = 0;
  162. alarm->pending = 0;
  163. for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
  164. retval = abx500_get_register_interruptible(dev, AB8500_RTC,
  165. ab8500_rtc_alarm_regs[i], &value);
  166. if (retval < 0)
  167. return retval;
  168. buf[i] = value;
  169. }
  170. mins = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
  171. secs = mins * 60;
  172. /* Add back the initially subtracted number of seconds */
  173. secs += get_elapsed_seconds(AB8500_RTC_EPOCH);
  174. rtc_time_to_tm(secs, &alarm->time);
  175. return rtc_valid_tm(&alarm->time);
  176. }
  177. static int ab8500_rtc_irq_enable(struct device *dev, unsigned int enabled)
  178. {
  179. return abx500_mask_and_set_register_interruptible(dev, AB8500_RTC,
  180. AB8500_RTC_STAT_REG, RTC_ALARM_ENA,
  181. enabled ? RTC_ALARM_ENA : 0);
  182. }
  183. static int ab8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  184. {
  185. int retval, i;
  186. unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
  187. unsigned long mins, secs = 0;
  188. if (alarm->time.tm_year < (AB8500_RTC_EPOCH - 1900)) {
  189. dev_dbg(dev, "year should be equal to or greater than %d\n",
  190. AB8500_RTC_EPOCH);
  191. return -EINVAL;
  192. }
  193. /* Get the number of seconds since 1970 */
  194. rtc_tm_to_time(&alarm->time, &secs);
  195. /*
  196. * Convert it to the number of seconds since 01-01-2000 00:00:00, since
  197. * we only have a small counter in the RTC.
  198. */
  199. secs -= get_elapsed_seconds(AB8500_RTC_EPOCH);
  200. mins = secs / 60;
  201. buf[2] = mins & 0xFF;
  202. buf[1] = (mins >> 8) & 0xFF;
  203. buf[0] = (mins >> 16) & 0xFF;
  204. /* Set the alarm time */
  205. for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
  206. retval = abx500_set_register_interruptible(dev, AB8500_RTC,
  207. ab8500_rtc_alarm_regs[i], buf[i]);
  208. if (retval < 0)
  209. return retval;
  210. }
  211. return ab8500_rtc_irq_enable(dev, alarm->enabled);
  212. }
  213. static irqreturn_t rtc_alarm_handler(int irq, void *data)
  214. {
  215. struct rtc_device *rtc = data;
  216. unsigned long events = RTC_IRQF | RTC_AF;
  217. dev_dbg(&rtc->dev, "%s\n", __func__);
  218. rtc_update_irq(rtc, 1, events);
  219. return IRQ_HANDLED;
  220. }
  221. static const struct rtc_class_ops ab8500_rtc_ops = {
  222. .read_time = ab8500_rtc_read_time,
  223. .set_time = ab8500_rtc_set_time,
  224. .read_alarm = ab8500_rtc_read_alarm,
  225. .set_alarm = ab8500_rtc_set_alarm,
  226. .alarm_irq_enable = ab8500_rtc_irq_enable,
  227. };
  228. static int __devinit ab8500_rtc_probe(struct platform_device *pdev)
  229. {
  230. int err;
  231. struct rtc_device *rtc;
  232. u8 rtc_ctrl;
  233. int irq;
  234. irq = platform_get_irq_byname(pdev, "ALARM");
  235. if (irq < 0)
  236. return irq;
  237. /* For RTC supply test */
  238. err = abx500_mask_and_set_register_interruptible(&pdev->dev, AB8500_RTC,
  239. AB8500_RTC_STAT_REG, RTC_STATUS_DATA, RTC_STATUS_DATA);
  240. if (err < 0)
  241. return err;
  242. /* Wait for reset by the PorRtc */
  243. msleep(1);
  244. err = abx500_get_register_interruptible(&pdev->dev, AB8500_RTC,
  245. AB8500_RTC_STAT_REG, &rtc_ctrl);
  246. if (err < 0)
  247. return err;
  248. /* Check if the RTC Supply fails */
  249. if (!(rtc_ctrl & RTC_STATUS_DATA)) {
  250. dev_err(&pdev->dev, "RTC supply failure\n");
  251. return -ENODEV;
  252. }
  253. rtc = rtc_device_register("ab8500-rtc", &pdev->dev, &ab8500_rtc_ops,
  254. THIS_MODULE);
  255. if (IS_ERR(rtc)) {
  256. dev_err(&pdev->dev, "Registration failed\n");
  257. err = PTR_ERR(rtc);
  258. return err;
  259. }
  260. err = request_threaded_irq(irq, NULL, rtc_alarm_handler, 0,
  261. "ab8500-rtc", rtc);
  262. if (err < 0) {
  263. rtc_device_unregister(rtc);
  264. return err;
  265. }
  266. platform_set_drvdata(pdev, rtc);
  267. return 0;
  268. }
  269. static int __devexit ab8500_rtc_remove(struct platform_device *pdev)
  270. {
  271. struct rtc_device *rtc = platform_get_drvdata(pdev);
  272. int irq = platform_get_irq_byname(pdev, "ALARM");
  273. free_irq(irq, rtc);
  274. rtc_device_unregister(rtc);
  275. platform_set_drvdata(pdev, NULL);
  276. return 0;
  277. }
  278. static struct platform_driver ab8500_rtc_driver = {
  279. .driver = {
  280. .name = "ab8500-rtc",
  281. .owner = THIS_MODULE,
  282. },
  283. .probe = ab8500_rtc_probe,
  284. .remove = __devexit_p(ab8500_rtc_remove),
  285. };
  286. static int __init ab8500_rtc_init(void)
  287. {
  288. return platform_driver_register(&ab8500_rtc_driver);
  289. }
  290. static void __exit ab8500_rtc_exit(void)
  291. {
  292. platform_driver_unregister(&ab8500_rtc_driver);
  293. }
  294. module_init(ab8500_rtc_init);
  295. module_exit(ab8500_rtc_exit);
  296. MODULE_AUTHOR("Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>");
  297. MODULE_DESCRIPTION("AB8500 RTC Driver");
  298. MODULE_LICENSE("GPL v2");