rtc-88pm860x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Real Time Clock driver for Marvell 88PM860x PMIC
  3. *
  4. * Copyright (c) 2010 Marvell International Ltd.
  5. * Author: Haojian Zhuang <haojian.zhuang@marvell.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/mutex.h>
  16. #include <linux/rtc.h>
  17. #include <linux/delay.h>
  18. #include <linux/mfd/core.h>
  19. #include <linux/mfd/88pm860x.h>
  20. #define VRTC_CALIBRATION
  21. struct pm860x_rtc_info {
  22. struct pm860x_chip *chip;
  23. struct i2c_client *i2c;
  24. struct rtc_device *rtc_dev;
  25. struct device *dev;
  26. struct delayed_work calib_work;
  27. int irq;
  28. int vrtc;
  29. int (*sync)(unsigned int ticks);
  30. };
  31. #define REG_VRTC_MEAS1 0x7D
  32. #define REG0_ADDR 0xB0
  33. #define REG1_ADDR 0xB2
  34. #define REG2_ADDR 0xB4
  35. #define REG3_ADDR 0xB6
  36. #define REG0_DATA 0xB1
  37. #define REG1_DATA 0xB3
  38. #define REG2_DATA 0xB5
  39. #define REG3_DATA 0xB7
  40. /* bit definitions of Measurement Enable Register 2 (0x51) */
  41. #define MEAS2_VRTC (1 << 0)
  42. /* bit definitions of RTC Register 1 (0xA0) */
  43. #define ALARM_EN (1 << 3)
  44. #define ALARM_WAKEUP (1 << 4)
  45. #define ALARM (1 << 5)
  46. #define RTC1_USE_XO (1 << 7)
  47. #define VRTC_CALIB_INTERVAL (HZ * 60 * 10) /* 10 minutes */
  48. static irqreturn_t rtc_update_handler(int irq, void *data)
  49. {
  50. struct pm860x_rtc_info *info = (struct pm860x_rtc_info *)data;
  51. int mask;
  52. mask = ALARM | ALARM_WAKEUP;
  53. pm860x_set_bits(info->i2c, PM8607_RTC1, mask | ALARM_EN, mask);
  54. rtc_update_irq(info->rtc_dev, 1, RTC_AF);
  55. return IRQ_HANDLED;
  56. }
  57. static int pm860x_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  58. {
  59. struct pm860x_rtc_info *info = dev_get_drvdata(dev);
  60. if (enabled)
  61. pm860x_set_bits(info->i2c, PM8607_RTC1, ALARM, ALARM);
  62. else
  63. pm860x_set_bits(info->i2c, PM8607_RTC1, ALARM, 0);
  64. return 0;
  65. }
  66. /*
  67. * Calculate the next alarm time given the requested alarm time mask
  68. * and the current time.
  69. */
  70. static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now,
  71. struct rtc_time *alrm)
  72. {
  73. unsigned long next_time;
  74. unsigned long now_time;
  75. next->tm_year = now->tm_year;
  76. next->tm_mon = now->tm_mon;
  77. next->tm_mday = now->tm_mday;
  78. next->tm_hour = alrm->tm_hour;
  79. next->tm_min = alrm->tm_min;
  80. next->tm_sec = alrm->tm_sec;
  81. rtc_tm_to_time(now, &now_time);
  82. rtc_tm_to_time(next, &next_time);
  83. if (next_time < now_time) {
  84. /* Advance one day */
  85. next_time += 60 * 60 * 24;
  86. rtc_time_to_tm(next_time, next);
  87. }
  88. }
  89. static int pm860x_rtc_read_time(struct device *dev, struct rtc_time *tm)
  90. {
  91. struct pm860x_rtc_info *info = dev_get_drvdata(dev);
  92. unsigned char buf[8];
  93. unsigned long ticks, base, data;
  94. pm860x_page_bulk_read(info->i2c, REG0_ADDR, 8, buf);
  95. dev_dbg(info->dev, "%x-%x-%x-%x-%x-%x-%x-%x\n", buf[0], buf[1],
  96. buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
  97. base = (buf[1] << 24) | (buf[3] << 16) | (buf[5] << 8) | buf[7];
  98. /* load 32-bit read-only counter */
  99. pm860x_bulk_read(info->i2c, PM8607_RTC_COUNTER1, 4, buf);
  100. data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  101. ticks = base + data;
  102. dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
  103. base, data, ticks);
  104. rtc_time_to_tm(ticks, tm);
  105. return 0;
  106. }
  107. static int pm860x_rtc_set_time(struct device *dev, struct rtc_time *tm)
  108. {
  109. struct pm860x_rtc_info *info = dev_get_drvdata(dev);
  110. unsigned char buf[4];
  111. unsigned long ticks, base, data;
  112. if ((tm->tm_year < 70) || (tm->tm_year > 138)) {
  113. dev_dbg(info->dev, "Set time %d out of range. "
  114. "Please set time between 1970 to 2038.\n",
  115. 1900 + tm->tm_year);
  116. return -EINVAL;
  117. }
  118. rtc_tm_to_time(tm, &ticks);
  119. /* load 32-bit read-only counter */
  120. pm860x_bulk_read(info->i2c, PM8607_RTC_COUNTER1, 4, buf);
  121. data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  122. base = ticks - data;
  123. dev_dbg(info->dev, "set base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
  124. base, data, ticks);
  125. pm860x_page_reg_write(info->i2c, REG0_DATA, (base >> 24) & 0xFF);
  126. pm860x_page_reg_write(info->i2c, REG1_DATA, (base >> 16) & 0xFF);
  127. pm860x_page_reg_write(info->i2c, REG2_DATA, (base >> 8) & 0xFF);
  128. pm860x_page_reg_write(info->i2c, REG3_DATA, base & 0xFF);
  129. if (info->sync)
  130. info->sync(ticks);
  131. return 0;
  132. }
  133. static int pm860x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  134. {
  135. struct pm860x_rtc_info *info = dev_get_drvdata(dev);
  136. unsigned char buf[8];
  137. unsigned long ticks, base, data;
  138. int ret;
  139. pm860x_page_bulk_read(info->i2c, REG0_ADDR, 8, buf);
  140. dev_dbg(info->dev, "%x-%x-%x-%x-%x-%x-%x-%x\n", buf[0], buf[1],
  141. buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
  142. base = (buf[1] << 24) | (buf[3] << 16) | (buf[5] << 8) | buf[7];
  143. pm860x_bulk_read(info->i2c, PM8607_RTC_EXPIRE1, 4, buf);
  144. data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  145. ticks = base + data;
  146. dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
  147. base, data, ticks);
  148. rtc_time_to_tm(ticks, &alrm->time);
  149. ret = pm860x_reg_read(info->i2c, PM8607_RTC1);
  150. alrm->enabled = (ret & ALARM_EN) ? 1 : 0;
  151. alrm->pending = (ret & (ALARM | ALARM_WAKEUP)) ? 1 : 0;
  152. return 0;
  153. }
  154. static int pm860x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  155. {
  156. struct pm860x_rtc_info *info = dev_get_drvdata(dev);
  157. struct rtc_time now_tm, alarm_tm;
  158. unsigned long ticks, base, data;
  159. unsigned char buf[8];
  160. int mask;
  161. pm860x_set_bits(info->i2c, PM8607_RTC1, ALARM_EN, 0);
  162. pm860x_page_bulk_read(info->i2c, REG0_ADDR, 8, buf);
  163. dev_dbg(info->dev, "%x-%x-%x-%x-%x-%x-%x-%x\n", buf[0], buf[1],
  164. buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
  165. base = (buf[1] << 24) | (buf[3] << 16) | (buf[5] << 8) | buf[7];
  166. /* load 32-bit read-only counter */
  167. pm860x_bulk_read(info->i2c, PM8607_RTC_COUNTER1, 4, buf);
  168. data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  169. ticks = base + data;
  170. dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
  171. base, data, ticks);
  172. rtc_time_to_tm(ticks, &now_tm);
  173. rtc_next_alarm_time(&alarm_tm, &now_tm, &alrm->time);
  174. /* get new ticks for alarm in 24 hours */
  175. rtc_tm_to_time(&alarm_tm, &ticks);
  176. data = ticks - base;
  177. buf[0] = data & 0xff;
  178. buf[1] = (data >> 8) & 0xff;
  179. buf[2] = (data >> 16) & 0xff;
  180. buf[3] = (data >> 24) & 0xff;
  181. pm860x_bulk_write(info->i2c, PM8607_RTC_EXPIRE1, 4, buf);
  182. if (alrm->enabled) {
  183. mask = ALARM | ALARM_WAKEUP | ALARM_EN;
  184. pm860x_set_bits(info->i2c, PM8607_RTC1, mask, mask);
  185. } else {
  186. mask = ALARM | ALARM_WAKEUP | ALARM_EN;
  187. pm860x_set_bits(info->i2c, PM8607_RTC1, mask,
  188. ALARM | ALARM_WAKEUP);
  189. }
  190. return 0;
  191. }
  192. static const struct rtc_class_ops pm860x_rtc_ops = {
  193. .read_time = pm860x_rtc_read_time,
  194. .set_time = pm860x_rtc_set_time,
  195. .read_alarm = pm860x_rtc_read_alarm,
  196. .set_alarm = pm860x_rtc_set_alarm,
  197. .alarm_irq_enable = pm860x_rtc_alarm_irq_enable,
  198. };
  199. #ifdef VRTC_CALIBRATION
  200. static void calibrate_vrtc_work(struct work_struct *work)
  201. {
  202. struct pm860x_rtc_info *info = container_of(work,
  203. struct pm860x_rtc_info, calib_work.work);
  204. unsigned char buf[2];
  205. unsigned int sum, data, mean, vrtc_set;
  206. int i;
  207. for (i = 0, sum = 0; i < 16; i++) {
  208. msleep(100);
  209. pm860x_bulk_read(info->i2c, REG_VRTC_MEAS1, 2, buf);
  210. data = (buf[0] << 4) | buf[1];
  211. data = (data * 5400) >> 12; /* convert to mv */
  212. sum += data;
  213. }
  214. mean = sum >> 4;
  215. vrtc_set = 2700 + (info->vrtc & 0x3) * 200;
  216. dev_dbg(info->dev, "mean:%d, vrtc_set:%d\n", mean, vrtc_set);
  217. sum = pm860x_reg_read(info->i2c, PM8607_RTC_MISC1);
  218. data = sum & 0x3;
  219. if ((mean + 200) < vrtc_set) {
  220. /* try higher voltage */
  221. if (++data == 4)
  222. goto out;
  223. data = (sum & 0xf8) | (data & 0x3);
  224. pm860x_reg_write(info->i2c, PM8607_RTC_MISC1, data);
  225. } else if ((mean - 200) > vrtc_set) {
  226. /* try lower voltage */
  227. if (data-- == 0)
  228. goto out;
  229. data = (sum & 0xf8) | (data & 0x3);
  230. pm860x_reg_write(info->i2c, PM8607_RTC_MISC1, data);
  231. } else
  232. goto out;
  233. dev_dbg(info->dev, "set 0x%x to RTC_MISC1\n", data);
  234. /* trigger next calibration since VRTC is updated */
  235. schedule_delayed_work(&info->calib_work, VRTC_CALIB_INTERVAL);
  236. return;
  237. out:
  238. /* disable measurement */
  239. pm860x_set_bits(info->i2c, PM8607_MEAS_EN2, MEAS2_VRTC, 0);
  240. dev_dbg(info->dev, "finish VRTC calibration\n");
  241. return;
  242. }
  243. #endif
  244. static int __devinit pm860x_rtc_probe(struct platform_device *pdev)
  245. {
  246. struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  247. struct pm860x_rtc_pdata *pdata = NULL;
  248. struct pm860x_rtc_info *info;
  249. struct rtc_time tm;
  250. unsigned long ticks = 0;
  251. int ret;
  252. pdata = pdev->dev.platform_data;
  253. if (pdata == NULL)
  254. dev_warn(&pdev->dev, "No platform data!\n");
  255. info = kzalloc(sizeof(struct pm860x_rtc_info), GFP_KERNEL);
  256. if (!info)
  257. return -ENOMEM;
  258. info->irq = platform_get_irq(pdev, 0);
  259. if (info->irq < 0) {
  260. dev_err(&pdev->dev, "No IRQ resource!\n");
  261. ret = -EINVAL;
  262. goto out;
  263. }
  264. info->chip = chip;
  265. info->i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  266. info->dev = &pdev->dev;
  267. dev_set_drvdata(&pdev->dev, info);
  268. ret = request_threaded_irq(info->irq, NULL, rtc_update_handler,
  269. IRQF_ONESHOT, "rtc", info);
  270. if (ret < 0) {
  271. dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
  272. info->irq, ret);
  273. goto out;
  274. }
  275. /* set addresses of 32-bit base value for RTC time */
  276. pm860x_page_reg_write(info->i2c, REG0_ADDR, REG0_DATA);
  277. pm860x_page_reg_write(info->i2c, REG1_ADDR, REG1_DATA);
  278. pm860x_page_reg_write(info->i2c, REG2_ADDR, REG2_DATA);
  279. pm860x_page_reg_write(info->i2c, REG3_ADDR, REG3_DATA);
  280. ret = pm860x_rtc_read_time(&pdev->dev, &tm);
  281. if (ret < 0) {
  282. dev_err(&pdev->dev, "Failed to read initial time.\n");
  283. goto out_rtc;
  284. }
  285. if ((tm.tm_year < 70) || (tm.tm_year > 138)) {
  286. tm.tm_year = 70;
  287. tm.tm_mon = 0;
  288. tm.tm_mday = 1;
  289. tm.tm_hour = 0;
  290. tm.tm_min = 0;
  291. tm.tm_sec = 0;
  292. ret = pm860x_rtc_set_time(&pdev->dev, &tm);
  293. if (ret < 0) {
  294. dev_err(&pdev->dev, "Failed to set initial time.\n");
  295. goto out_rtc;
  296. }
  297. }
  298. rtc_tm_to_time(&tm, &ticks);
  299. if (pdata && pdata->sync) {
  300. pdata->sync(ticks);
  301. info->sync = pdata->sync;
  302. }
  303. info->rtc_dev = rtc_device_register("88pm860x-rtc", &pdev->dev,
  304. &pm860x_rtc_ops, THIS_MODULE);
  305. ret = PTR_ERR(info->rtc_dev);
  306. if (IS_ERR(info->rtc_dev)) {
  307. dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
  308. goto out_rtc;
  309. }
  310. /*
  311. * enable internal XO instead of internal 3.25MHz clock since it can
  312. * free running in PMIC power-down state.
  313. */
  314. pm860x_set_bits(info->i2c, PM8607_RTC1, RTC1_USE_XO, RTC1_USE_XO);
  315. #ifdef VRTC_CALIBRATION
  316. /* <00> -- 2.7V, <01> -- 2.9V, <10> -- 3.1V, <11> -- 3.3V */
  317. if (pdata && pdata->vrtc)
  318. info->vrtc = pdata->vrtc & 0x3;
  319. else
  320. info->vrtc = 1;
  321. pm860x_set_bits(info->i2c, PM8607_MEAS_EN2, MEAS2_VRTC, MEAS2_VRTC);
  322. /* calibrate VRTC */
  323. INIT_DELAYED_WORK(&info->calib_work, calibrate_vrtc_work);
  324. schedule_delayed_work(&info->calib_work, VRTC_CALIB_INTERVAL);
  325. #endif /* VRTC_CALIBRATION */
  326. return 0;
  327. out_rtc:
  328. free_irq(info->irq, info);
  329. out:
  330. kfree(info);
  331. return ret;
  332. }
  333. static int __devexit pm860x_rtc_remove(struct platform_device *pdev)
  334. {
  335. struct pm860x_rtc_info *info = platform_get_drvdata(pdev);
  336. #ifdef VRTC_CALIBRATION
  337. flush_scheduled_work();
  338. /* disable measurement */
  339. pm860x_set_bits(info->i2c, PM8607_MEAS_EN2, MEAS2_VRTC, 0);
  340. #endif /* VRTC_CALIBRATION */
  341. platform_set_drvdata(pdev, NULL);
  342. rtc_device_unregister(info->rtc_dev);
  343. free_irq(info->irq, info);
  344. kfree(info);
  345. return 0;
  346. }
  347. static struct platform_driver pm860x_rtc_driver = {
  348. .driver = {
  349. .name = "88pm860x-rtc",
  350. .owner = THIS_MODULE,
  351. },
  352. .probe = pm860x_rtc_probe,
  353. .remove = __devexit_p(pm860x_rtc_remove),
  354. };
  355. static int __init pm860x_rtc_init(void)
  356. {
  357. return platform_driver_register(&pm860x_rtc_driver);
  358. }
  359. module_init(pm860x_rtc_init);
  360. static void __exit pm860x_rtc_exit(void)
  361. {
  362. platform_driver_unregister(&pm860x_rtc_driver);
  363. }
  364. module_exit(pm860x_rtc_exit);
  365. MODULE_DESCRIPTION("Marvell 88PM860x RTC driver");
  366. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  367. MODULE_LICENSE("GPL");