rtc-88pm860x.c 13 KB

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