rtc-ab8500.c 12 KB

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