rtc-ab8500.c 12 KB

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