rtc-ab8500.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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;
  189. if (alarm->time.tm_year < (AB8500_RTC_EPOCH - 1900)) {
  190. dev_dbg(dev, "year should be equal to or greater than %d\n",
  191. AB8500_RTC_EPOCH);
  192. return -EINVAL;
  193. }
  194. /* Get the number of seconds since 1970 */
  195. rtc_tm_to_time(&alarm->time, &secs);
  196. /*
  197. * Convert it to the number of seconds since 01-01-2000 00:00:00, since
  198. * we only have a small counter in the RTC.
  199. */
  200. secs -= get_elapsed_seconds(AB8500_RTC_EPOCH);
  201. mins = secs / 60;
  202. buf[2] = mins & 0xFF;
  203. buf[1] = (mins >> 8) & 0xFF;
  204. buf[0] = (mins >> 16) & 0xFF;
  205. /* Set the alarm time */
  206. for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
  207. retval = abx500_set_register_interruptible(dev, AB8500_RTC,
  208. ab8500_rtc_alarm_regs[i], buf[i]);
  209. if (retval < 0)
  210. return retval;
  211. }
  212. return ab8500_rtc_irq_enable(dev, alarm->enabled);
  213. }
  214. static int ab8500_rtc_set_calibration(struct device *dev, int calibration)
  215. {
  216. int retval;
  217. u8 rtccal = 0;
  218. /*
  219. * Check that the calibration value (which is in units of 0.5
  220. * parts-per-million) is in the AB8500's range for RtcCalibration
  221. * register. -128 (0x80) is not permitted because the AB8500 uses
  222. * a sign-bit rather than two's complement, so 0x80 is just another
  223. * representation of zero.
  224. */
  225. if ((calibration < -127) || (calibration > 127)) {
  226. dev_err(dev, "RtcCalibration value outside permitted range\n");
  227. return -EINVAL;
  228. }
  229. /*
  230. * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
  231. * so need to convert to this sort of representation before writing
  232. * into RtcCalibration register...
  233. */
  234. if (calibration >= 0)
  235. rtccal = 0x7F & calibration;
  236. else
  237. rtccal = ~(calibration - 1) | 0x80;
  238. retval = abx500_set_register_interruptible(dev, AB8500_RTC,
  239. AB8500_RTC_CALIB_REG, rtccal);
  240. return retval;
  241. }
  242. static int ab8500_rtc_get_calibration(struct device *dev, int *calibration)
  243. {
  244. int retval;
  245. u8 rtccal = 0;
  246. retval = abx500_get_register_interruptible(dev, AB8500_RTC,
  247. AB8500_RTC_CALIB_REG, &rtccal);
  248. if (retval >= 0) {
  249. /*
  250. * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
  251. * so need to convert value from RtcCalibration register into
  252. * a two's complement signed value...
  253. */
  254. if (rtccal & 0x80)
  255. *calibration = 0 - (rtccal & 0x7F);
  256. else
  257. *calibration = 0x7F & rtccal;
  258. }
  259. return retval;
  260. }
  261. static ssize_t ab8500_sysfs_store_rtc_calibration(struct device *dev,
  262. struct device_attribute *attr,
  263. const char *buf, size_t count)
  264. {
  265. int retval;
  266. int calibration = 0;
  267. if (sscanf(buf, " %i ", &calibration) != 1) {
  268. dev_err(dev, "Failed to store RTC calibration attribute\n");
  269. return -EINVAL;
  270. }
  271. retval = ab8500_rtc_set_calibration(dev, calibration);
  272. return retval ? retval : count;
  273. }
  274. static ssize_t ab8500_sysfs_show_rtc_calibration(struct device *dev,
  275. struct device_attribute *attr, char *buf)
  276. {
  277. int retval = 0;
  278. int calibration = 0;
  279. retval = ab8500_rtc_get_calibration(dev, &calibration);
  280. if (retval < 0) {
  281. dev_err(dev, "Failed to read RTC calibration attribute\n");
  282. sprintf(buf, "0\n");
  283. return retval;
  284. }
  285. return sprintf(buf, "%d\n", calibration);
  286. }
  287. static DEVICE_ATTR(rtc_calibration, S_IRUGO | S_IWUSR,
  288. ab8500_sysfs_show_rtc_calibration,
  289. ab8500_sysfs_store_rtc_calibration);
  290. static int ab8500_sysfs_rtc_register(struct device *dev)
  291. {
  292. return device_create_file(dev, &dev_attr_rtc_calibration);
  293. }
  294. static void ab8500_sysfs_rtc_unregister(struct device *dev)
  295. {
  296. device_remove_file(dev, &dev_attr_rtc_calibration);
  297. }
  298. static irqreturn_t rtc_alarm_handler(int irq, void *data)
  299. {
  300. struct rtc_device *rtc = data;
  301. unsigned long events = RTC_IRQF | RTC_AF;
  302. dev_dbg(&rtc->dev, "%s\n", __func__);
  303. rtc_update_irq(rtc, 1, events);
  304. return IRQ_HANDLED;
  305. }
  306. static const struct rtc_class_ops ab8500_rtc_ops = {
  307. .read_time = ab8500_rtc_read_time,
  308. .set_time = ab8500_rtc_set_time,
  309. .read_alarm = ab8500_rtc_read_alarm,
  310. .set_alarm = ab8500_rtc_set_alarm,
  311. .alarm_irq_enable = ab8500_rtc_irq_enable,
  312. };
  313. static int __devinit ab8500_rtc_probe(struct platform_device *pdev)
  314. {
  315. int err;
  316. struct rtc_device *rtc;
  317. u8 rtc_ctrl;
  318. int irq;
  319. irq = platform_get_irq_byname(pdev, "ALARM");
  320. if (irq < 0)
  321. return irq;
  322. /* For RTC supply test */
  323. err = abx500_mask_and_set_register_interruptible(&pdev->dev, AB8500_RTC,
  324. AB8500_RTC_STAT_REG, RTC_STATUS_DATA, RTC_STATUS_DATA);
  325. if (err < 0)
  326. return err;
  327. /* Wait for reset by the PorRtc */
  328. usleep_range(1000, 5000);
  329. err = abx500_get_register_interruptible(&pdev->dev, AB8500_RTC,
  330. AB8500_RTC_STAT_REG, &rtc_ctrl);
  331. if (err < 0)
  332. return err;
  333. /* Check if the RTC Supply fails */
  334. if (!(rtc_ctrl & RTC_STATUS_DATA)) {
  335. dev_err(&pdev->dev, "RTC supply failure\n");
  336. return -ENODEV;
  337. }
  338. device_init_wakeup(&pdev->dev, true);
  339. rtc = rtc_device_register("ab8500-rtc", &pdev->dev, &ab8500_rtc_ops,
  340. THIS_MODULE);
  341. if (IS_ERR(rtc)) {
  342. dev_err(&pdev->dev, "Registration failed\n");
  343. err = PTR_ERR(rtc);
  344. return err;
  345. }
  346. err = request_threaded_irq(irq, NULL, rtc_alarm_handler,
  347. IRQF_NO_SUSPEND | IRQF_ONESHOT, "ab8500-rtc", rtc);
  348. if (err < 0) {
  349. rtc_device_unregister(rtc);
  350. return err;
  351. }
  352. platform_set_drvdata(pdev, rtc);
  353. err = ab8500_sysfs_rtc_register(&pdev->dev);
  354. if (err) {
  355. dev_err(&pdev->dev, "sysfs RTC failed to register\n");
  356. return err;
  357. }
  358. return 0;
  359. }
  360. static int __devexit ab8500_rtc_remove(struct platform_device *pdev)
  361. {
  362. struct rtc_device *rtc = platform_get_drvdata(pdev);
  363. int irq = platform_get_irq_byname(pdev, "ALARM");
  364. ab8500_sysfs_rtc_unregister(&pdev->dev);
  365. free_irq(irq, rtc);
  366. rtc_device_unregister(rtc);
  367. platform_set_drvdata(pdev, NULL);
  368. return 0;
  369. }
  370. static const struct of_device_id ab8500_rtc_match[] = {
  371. { .compatible = "stericsson,ab8500-rtc", },
  372. {}
  373. };
  374. static struct platform_driver ab8500_rtc_driver = {
  375. .driver = {
  376. .name = "ab8500-rtc",
  377. .owner = THIS_MODULE,
  378. .of_match_table = ab8500_rtc_match,
  379. },
  380. .probe = ab8500_rtc_probe,
  381. .remove = __devexit_p(ab8500_rtc_remove),
  382. };
  383. module_platform_driver(ab8500_rtc_driver);
  384. MODULE_AUTHOR("Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>");
  385. MODULE_DESCRIPTION("AB8500 RTC Driver");
  386. MODULE_LICENSE("GPL v2");