rtc-88pm860x.c 13 KB

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