blob: 434285f495e02f8f9946d6d28094639ddb2e8540 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Haojian Zhuang008b3042011-05-06 17:21:20 +08002/*
3 * Real Time Clock driver for Marvell 88PM860x PMIC
4 *
5 * Copyright (c) 2010 Marvell International Ltd.
6 * Author: Haojian Zhuang <haojian.zhuang@marvell.com>
Haojian Zhuang008b3042011-05-06 17:21:20 +08007 */
8
9#include <linux/kernel.h>
10#include <linux/module.h>
Haojian Zhuang2e57d562012-09-21 18:06:52 +080011#include <linux/of.h>
Haojian Zhuang008b3042011-05-06 17:21:20 +080012#include <linux/platform_device.h>
13#include <linux/slab.h>
14#include <linux/mutex.h>
15#include <linux/rtc.h>
16#include <linux/delay.h>
17#include <linux/mfd/core.h>
18#include <linux/mfd/88pm860x.h>
19
20#define VRTC_CALIBRATION
21
22struct 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
29 int irq;
30 int vrtc;
31 int (*sync)(unsigned int ticks);
32};
33
34#define REG_VRTC_MEAS1 0x7D
35
36#define REG0_ADDR 0xB0
37#define REG1_ADDR 0xB2
38#define REG2_ADDR 0xB4
39#define REG3_ADDR 0xB6
40
41#define REG0_DATA 0xB1
42#define REG1_DATA 0xB3
43#define REG2_DATA 0xB5
44#define REG3_DATA 0xB7
45
46/* bit definitions of Measurement Enable Register 2 (0x51) */
47#define MEAS2_VRTC (1 << 0)
48
49/* bit definitions of RTC Register 1 (0xA0) */
50#define ALARM_EN (1 << 3)
51#define ALARM_WAKEUP (1 << 4)
52#define ALARM (1 << 5)
53#define RTC1_USE_XO (1 << 7)
54
55#define VRTC_CALIB_INTERVAL (HZ * 60 * 10) /* 10 minutes */
56
57static irqreturn_t rtc_update_handler(int irq, void *data)
58{
59 struct pm860x_rtc_info *info = (struct pm860x_rtc_info *)data;
60 int mask;
61
62 mask = ALARM | ALARM_WAKEUP;
63 pm860x_set_bits(info->i2c, PM8607_RTC1, mask | ALARM_EN, mask);
64 rtc_update_irq(info->rtc_dev, 1, RTC_AF);
65 return IRQ_HANDLED;
66}
67
68static int pm860x_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
69{
70 struct pm860x_rtc_info *info = dev_get_drvdata(dev);
71
72 if (enabled)
Jett.Zhoufd835d12012-04-05 14:25:08 -070073 pm860x_set_bits(info->i2c, PM8607_RTC1, ALARM_EN, ALARM_EN);
Haojian Zhuang008b3042011-05-06 17:21:20 +080074 else
Jett.Zhoufd835d12012-04-05 14:25:08 -070075 pm860x_set_bits(info->i2c, PM8607_RTC1, ALARM_EN, 0);
Haojian Zhuang008b3042011-05-06 17:21:20 +080076 return 0;
77}
78
79/*
80 * Calculate the next alarm time given the requested alarm time mask
81 * and the current time.
82 */
83static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now,
84 struct rtc_time *alrm)
85{
86 unsigned long next_time;
87 unsigned long now_time;
88
89 next->tm_year = now->tm_year;
90 next->tm_mon = now->tm_mon;
91 next->tm_mday = now->tm_mday;
92 next->tm_hour = alrm->tm_hour;
93 next->tm_min = alrm->tm_min;
94 next->tm_sec = alrm->tm_sec;
95
96 rtc_tm_to_time(now, &now_time);
97 rtc_tm_to_time(next, &next_time);
98
99 if (next_time < now_time) {
100 /* Advance one day */
101 next_time += 60 * 60 * 24;
102 rtc_time_to_tm(next_time, next);
103 }
104}
105
106static int pm860x_rtc_read_time(struct device *dev, struct rtc_time *tm)
107{
108 struct pm860x_rtc_info *info = dev_get_drvdata(dev);
109 unsigned char buf[8];
110 unsigned long ticks, base, data;
111
112 pm860x_page_bulk_read(info->i2c, REG0_ADDR, 8, buf);
113 dev_dbg(info->dev, "%x-%x-%x-%x-%x-%x-%x-%x\n", buf[0], buf[1],
114 buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
Colin Ian Kingdc9e4712019-02-06 09:50:53 +0000115 base = ((unsigned long)buf[1] << 24) | (buf[3] << 16) |
116 (buf[5] << 8) | buf[7];
Haojian Zhuang008b3042011-05-06 17:21:20 +0800117
118 /* load 32-bit read-only counter */
119 pm860x_bulk_read(info->i2c, PM8607_RTC_COUNTER1, 4, buf);
Colin Ian Kingdc9e4712019-02-06 09:50:53 +0000120 data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
121 (buf[1] << 8) | buf[0];
Haojian Zhuang008b3042011-05-06 17:21:20 +0800122 ticks = base + data;
123 dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
124 base, data, ticks);
125
126 rtc_time_to_tm(ticks, tm);
127
128 return 0;
129}
130
131static int pm860x_rtc_set_time(struct device *dev, struct rtc_time *tm)
132{
133 struct pm860x_rtc_info *info = dev_get_drvdata(dev);
134 unsigned char buf[4];
135 unsigned long ticks, base, data;
136
Alexandre Belloni9dddb1b2018-03-12 03:00:53 +0100137 if (tm->tm_year > 206) {
Haojian Zhuang008b3042011-05-06 17:21:20 +0800138 dev_dbg(info->dev, "Set time %d out of range. "
Alexandre Belloni9dddb1b2018-03-12 03:00:53 +0100139 "Please set time between 1970 to 2106.\n",
Haojian Zhuang008b3042011-05-06 17:21:20 +0800140 1900 + tm->tm_year);
141 return -EINVAL;
142 }
143 rtc_tm_to_time(tm, &ticks);
144
145 /* load 32-bit read-only counter */
146 pm860x_bulk_read(info->i2c, PM8607_RTC_COUNTER1, 4, buf);
Colin Ian Kingdc9e4712019-02-06 09:50:53 +0000147 data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
148 (buf[1] << 8) | buf[0];
Haojian Zhuang008b3042011-05-06 17:21:20 +0800149 base = ticks - data;
150 dev_dbg(info->dev, "set base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
151 base, data, ticks);
152
153 pm860x_page_reg_write(info->i2c, REG0_DATA, (base >> 24) & 0xFF);
154 pm860x_page_reg_write(info->i2c, REG1_DATA, (base >> 16) & 0xFF);
155 pm860x_page_reg_write(info->i2c, REG2_DATA, (base >> 8) & 0xFF);
156 pm860x_page_reg_write(info->i2c, REG3_DATA, base & 0xFF);
157
158 if (info->sync)
159 info->sync(ticks);
160 return 0;
161}
162
163static int pm860x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
164{
165 struct pm860x_rtc_info *info = dev_get_drvdata(dev);
166 unsigned char buf[8];
167 unsigned long ticks, base, data;
168 int ret;
169
170 pm860x_page_bulk_read(info->i2c, REG0_ADDR, 8, buf);
171 dev_dbg(info->dev, "%x-%x-%x-%x-%x-%x-%x-%x\n", buf[0], buf[1],
172 buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
Colin Ian Kingdc9e4712019-02-06 09:50:53 +0000173 base = ((unsigned long)buf[1] << 24) | (buf[3] << 16) |
174 (buf[5] << 8) | buf[7];
Haojian Zhuang008b3042011-05-06 17:21:20 +0800175
176 pm860x_bulk_read(info->i2c, PM8607_RTC_EXPIRE1, 4, buf);
Colin Ian Kingdc9e4712019-02-06 09:50:53 +0000177 data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
178 (buf[1] << 8) | buf[0];
Haojian Zhuang008b3042011-05-06 17:21:20 +0800179 ticks = base + data;
180 dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
181 base, data, ticks);
182
183 rtc_time_to_tm(ticks, &alrm->time);
184 ret = pm860x_reg_read(info->i2c, PM8607_RTC1);
185 alrm->enabled = (ret & ALARM_EN) ? 1 : 0;
186 alrm->pending = (ret & (ALARM | ALARM_WAKEUP)) ? 1 : 0;
187 return 0;
188}
189
190static int pm860x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
191{
192 struct pm860x_rtc_info *info = dev_get_drvdata(dev);
193 struct rtc_time now_tm, alarm_tm;
194 unsigned long ticks, base, data;
195 unsigned char buf[8];
196 int mask;
197
198 pm860x_set_bits(info->i2c, PM8607_RTC1, ALARM_EN, 0);
199
200 pm860x_page_bulk_read(info->i2c, REG0_ADDR, 8, buf);
201 dev_dbg(info->dev, "%x-%x-%x-%x-%x-%x-%x-%x\n", buf[0], buf[1],
202 buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
Colin Ian Kingdc9e4712019-02-06 09:50:53 +0000203 base = ((unsigned long)buf[1] << 24) | (buf[3] << 16) |
204 (buf[5] << 8) | buf[7];
Haojian Zhuang008b3042011-05-06 17:21:20 +0800205
206 /* load 32-bit read-only counter */
207 pm860x_bulk_read(info->i2c, PM8607_RTC_COUNTER1, 4, buf);
Colin Ian Kingdc9e4712019-02-06 09:50:53 +0000208 data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
209 (buf[1] << 8) | buf[0];
Haojian Zhuang008b3042011-05-06 17:21:20 +0800210 ticks = base + data;
211 dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
212 base, data, ticks);
213
214 rtc_time_to_tm(ticks, &now_tm);
215 rtc_next_alarm_time(&alarm_tm, &now_tm, &alrm->time);
216 /* get new ticks for alarm in 24 hours */
217 rtc_tm_to_time(&alarm_tm, &ticks);
218 data = ticks - base;
219
220 buf[0] = data & 0xff;
221 buf[1] = (data >> 8) & 0xff;
222 buf[2] = (data >> 16) & 0xff;
223 buf[3] = (data >> 24) & 0xff;
224 pm860x_bulk_write(info->i2c, PM8607_RTC_EXPIRE1, 4, buf);
225 if (alrm->enabled) {
226 mask = ALARM | ALARM_WAKEUP | ALARM_EN;
227 pm860x_set_bits(info->i2c, PM8607_RTC1, mask, mask);
228 } else {
229 mask = ALARM | ALARM_WAKEUP | ALARM_EN;
230 pm860x_set_bits(info->i2c, PM8607_RTC1, mask,
231 ALARM | ALARM_WAKEUP);
232 }
233 return 0;
234}
235
236static const struct rtc_class_ops pm860x_rtc_ops = {
237 .read_time = pm860x_rtc_read_time,
238 .set_time = pm860x_rtc_set_time,
239 .read_alarm = pm860x_rtc_read_alarm,
240 .set_alarm = pm860x_rtc_set_alarm,
241 .alarm_irq_enable = pm860x_rtc_alarm_irq_enable,
242};
243
244#ifdef VRTC_CALIBRATION
245static void calibrate_vrtc_work(struct work_struct *work)
246{
247 struct pm860x_rtc_info *info = container_of(work,
248 struct pm860x_rtc_info, calib_work.work);
249 unsigned char buf[2];
250 unsigned int sum, data, mean, vrtc_set;
251 int i;
252
253 for (i = 0, sum = 0; i < 16; i++) {
254 msleep(100);
255 pm860x_bulk_read(info->i2c, REG_VRTC_MEAS1, 2, buf);
256 data = (buf[0] << 4) | buf[1];
257 data = (data * 5400) >> 12; /* convert to mv */
258 sum += data;
259 }
260 mean = sum >> 4;
261 vrtc_set = 2700 + (info->vrtc & 0x3) * 200;
262 dev_dbg(info->dev, "mean:%d, vrtc_set:%d\n", mean, vrtc_set);
263
264 sum = pm860x_reg_read(info->i2c, PM8607_RTC_MISC1);
265 data = sum & 0x3;
266 if ((mean + 200) < vrtc_set) {
267 /* try higher voltage */
268 if (++data == 4)
269 goto out;
270 data = (sum & 0xf8) | (data & 0x3);
271 pm860x_reg_write(info->i2c, PM8607_RTC_MISC1, data);
272 } else if ((mean - 200) > vrtc_set) {
273 /* try lower voltage */
274 if (data-- == 0)
275 goto out;
276 data = (sum & 0xf8) | (data & 0x3);
277 pm860x_reg_write(info->i2c, PM8607_RTC_MISC1, data);
278 } else
279 goto out;
280 dev_dbg(info->dev, "set 0x%x to RTC_MISC1\n", data);
281 /* trigger next calibration since VRTC is updated */
282 schedule_delayed_work(&info->calib_work, VRTC_CALIB_INTERVAL);
283 return;
284out:
285 /* disable measurement */
286 pm860x_set_bits(info->i2c, PM8607_MEAS_EN2, MEAS2_VRTC, 0);
287 dev_dbg(info->dev, "finish VRTC calibration\n");
288 return;
289}
290#endif
291
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800292#ifdef CONFIG_OF
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800293static int pm860x_rtc_dt_init(struct platform_device *pdev,
294 struct pm860x_rtc_info *info)
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800295{
296 struct device_node *np = pdev->dev.parent->of_node;
297 int ret;
298 if (!np)
299 return -ENODEV;
Krzysztof Kozlowskie66040c2014-06-06 14:35:51 -0700300 np = of_get_child_by_name(np, "rtc");
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800301 if (!np) {
302 dev_err(&pdev->dev, "failed to find rtc node\n");
303 return -ENODEV;
304 }
305 ret = of_property_read_u32(np, "marvell,88pm860x-vrtc", &info->vrtc);
306 if (ret)
307 info->vrtc = 0;
Krzysztof Kozlowski1e83d482014-06-06 14:35:52 -0700308 of_node_put(np);
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800309 return 0;
310}
311#else
312#define pm860x_rtc_dt_init(x, y) (-1)
313#endif
314
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800315static int pm860x_rtc_probe(struct platform_device *pdev)
Haojian Zhuang008b3042011-05-06 17:21:20 +0800316{
317 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
318 struct pm860x_rtc_pdata *pdata = NULL;
319 struct pm860x_rtc_info *info;
320 struct rtc_time tm;
321 unsigned long ticks = 0;
322 int ret;
323
Jingoo Han82df0b22013-11-12 15:10:37 -0800324 pdata = dev_get_platdata(&pdev->dev);
Haojian Zhuang008b3042011-05-06 17:21:20 +0800325
Sachin Kamat3087b3d2013-04-29 16:20:27 -0700326 info = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_rtc_info),
327 GFP_KERNEL);
Haojian Zhuang008b3042011-05-06 17:21:20 +0800328 if (!info)
329 return -ENOMEM;
330 info->irq = platform_get_irq(pdev, 0);
331 if (info->irq < 0) {
332 dev_err(&pdev->dev, "No IRQ resource!\n");
Sachin Kamat3087b3d2013-04-29 16:20:27 -0700333 return info->irq;
Haojian Zhuang008b3042011-05-06 17:21:20 +0800334 }
335
336 info->chip = chip;
337 info->i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
338 info->dev = &pdev->dev;
339 dev_set_drvdata(&pdev->dev, info);
340
Sachin Kamat3087b3d2013-04-29 16:20:27 -0700341 ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
342 rtc_update_handler, IRQF_ONESHOT, "rtc",
343 info);
Haojian Zhuang008b3042011-05-06 17:21:20 +0800344 if (ret < 0) {
345 dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
346 info->irq, ret);
Sachin Kamat3087b3d2013-04-29 16:20:27 -0700347 return ret;
Haojian Zhuang008b3042011-05-06 17:21:20 +0800348 }
349
350 /* set addresses of 32-bit base value for RTC time */
351 pm860x_page_reg_write(info->i2c, REG0_ADDR, REG0_DATA);
352 pm860x_page_reg_write(info->i2c, REG1_ADDR, REG1_DATA);
353 pm860x_page_reg_write(info->i2c, REG2_ADDR, REG2_DATA);
354 pm860x_page_reg_write(info->i2c, REG3_ADDR, REG3_DATA);
355
356 ret = pm860x_rtc_read_time(&pdev->dev, &tm);
357 if (ret < 0) {
358 dev_err(&pdev->dev, "Failed to read initial time.\n");
Sachin Kamat3087b3d2013-04-29 16:20:27 -0700359 return ret;
Haojian Zhuang008b3042011-05-06 17:21:20 +0800360 }
361 if ((tm.tm_year < 70) || (tm.tm_year > 138)) {
362 tm.tm_year = 70;
363 tm.tm_mon = 0;
364 tm.tm_mday = 1;
365 tm.tm_hour = 0;
366 tm.tm_min = 0;
367 tm.tm_sec = 0;
368 ret = pm860x_rtc_set_time(&pdev->dev, &tm);
369 if (ret < 0) {
370 dev_err(&pdev->dev, "Failed to set initial time.\n");
Sachin Kamat3087b3d2013-04-29 16:20:27 -0700371 return ret;
Haojian Zhuang008b3042011-05-06 17:21:20 +0800372 }
373 }
374 rtc_tm_to_time(&tm, &ticks);
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800375 if (pm860x_rtc_dt_init(pdev, info)) {
376 if (pdata && pdata->sync) {
377 pdata->sync(ticks);
378 info->sync = pdata->sync;
379 }
Haojian Zhuang008b3042011-05-06 17:21:20 +0800380 }
381
Sachin Kamat3087b3d2013-04-29 16:20:27 -0700382 info->rtc_dev = devm_rtc_device_register(&pdev->dev, "88pm860x-rtc",
Haojian Zhuang008b3042011-05-06 17:21:20 +0800383 &pm860x_rtc_ops, THIS_MODULE);
384 ret = PTR_ERR(info->rtc_dev);
385 if (IS_ERR(info->rtc_dev)) {
386 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
Sachin Kamat3087b3d2013-04-29 16:20:27 -0700387 return ret;
Haojian Zhuang008b3042011-05-06 17:21:20 +0800388 }
389
390 /*
391 * enable internal XO instead of internal 3.25MHz clock since it can
392 * free running in PMIC power-down state.
393 */
394 pm860x_set_bits(info->i2c, PM8607_RTC1, RTC1_USE_XO, RTC1_USE_XO);
395
396#ifdef VRTC_CALIBRATION
397 /* <00> -- 2.7V, <01> -- 2.9V, <10> -- 3.1V, <11> -- 3.3V */
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800398 if (pm860x_rtc_dt_init(pdev, info)) {
399 if (pdata && pdata->vrtc)
400 info->vrtc = pdata->vrtc & 0x3;
401 else
402 info->vrtc = 1;
403 }
Haojian Zhuang008b3042011-05-06 17:21:20 +0800404 pm860x_set_bits(info->i2c, PM8607_MEAS_EN2, MEAS2_VRTC, MEAS2_VRTC);
405
406 /* calibrate VRTC */
407 INIT_DELAYED_WORK(&info->calib_work, calibrate_vrtc_work);
408 schedule_delayed_work(&info->calib_work, VRTC_CALIB_INTERVAL);
409#endif /* VRTC_CALIBRATION */
Jett.Zhou28533782012-02-27 15:44:20 +0100410
411 device_init_wakeup(&pdev->dev, 1);
412
Haojian Zhuang008b3042011-05-06 17:21:20 +0800413 return 0;
Haojian Zhuang008b3042011-05-06 17:21:20 +0800414}
415
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800416static int pm860x_rtc_remove(struct platform_device *pdev)
Haojian Zhuang008b3042011-05-06 17:21:20 +0800417{
418 struct pm860x_rtc_info *info = platform_get_drvdata(pdev);
419
420#ifdef VRTC_CALIBRATION
Sven Van Asbroeckf22b1ba2019-04-26 14:36:35 -0400421 cancel_delayed_work_sync(&info->calib_work);
Haojian Zhuang008b3042011-05-06 17:21:20 +0800422 /* disable measurement */
423 pm860x_set_bits(info->i2c, PM8607_MEAS_EN2, MEAS2_VRTC, 0);
424#endif /* VRTC_CALIBRATION */
425
Haojian Zhuang008b3042011-05-06 17:21:20 +0800426 return 0;
427}
428
Jett.Zhou28533782012-02-27 15:44:20 +0100429#ifdef CONFIG_PM_SLEEP
430static int pm860x_rtc_suspend(struct device *dev)
431{
432 struct platform_device *pdev = to_platform_device(dev);
433 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
434
435 if (device_may_wakeup(dev))
436 chip->wakeup_flag |= 1 << PM8607_IRQ_RTC;
437 return 0;
438}
439static int pm860x_rtc_resume(struct device *dev)
440{
441 struct platform_device *pdev = to_platform_device(dev);
442 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
443
444 if (device_may_wakeup(dev))
445 chip->wakeup_flag &= ~(1 << PM8607_IRQ_RTC);
446 return 0;
447}
448#endif
449
450static SIMPLE_DEV_PM_OPS(pm860x_rtc_pm_ops, pm860x_rtc_suspend, pm860x_rtc_resume);
451
Haojian Zhuang008b3042011-05-06 17:21:20 +0800452static struct platform_driver pm860x_rtc_driver = {
453 .driver = {
454 .name = "88pm860x-rtc",
Jett.Zhou28533782012-02-27 15:44:20 +0100455 .pm = &pm860x_rtc_pm_ops,
Haojian Zhuang008b3042011-05-06 17:21:20 +0800456 },
457 .probe = pm860x_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800458 .remove = pm860x_rtc_remove,
Haojian Zhuang008b3042011-05-06 17:21:20 +0800459};
460
Axel Lin0c4eae62012-01-10 15:10:48 -0800461module_platform_driver(pm860x_rtc_driver);
Haojian Zhuang008b3042011-05-06 17:21:20 +0800462
463MODULE_DESCRIPTION("Marvell 88PM860x RTC driver");
464MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
465MODULE_LICENSE("GPL");