1 // SPDX-License-Identifier: GPL-2.0-only
3 * Real Time Clock driver for Marvell 88PM860x PMIC
5 * Copyright (c) 2010 Marvell International Ltd.
6 * Author: Haojian Zhuang <haojian.zhuang@marvell.com>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
12 #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>
20 #define VRTC_CALIBRATION
22 struct pm860x_rtc_info {
23 struct pm860x_chip *chip;
24 struct i2c_client *i2c;
25 struct rtc_device *rtc_dev;
27 struct delayed_work calib_work;
33 #define REG_VRTC_MEAS1 0x7D
35 #define REG0_ADDR 0xB0
36 #define REG1_ADDR 0xB2
37 #define REG2_ADDR 0xB4
38 #define REG3_ADDR 0xB6
40 #define REG0_DATA 0xB1
41 #define REG1_DATA 0xB3
42 #define REG2_DATA 0xB5
43 #define REG3_DATA 0xB7
45 /* bit definitions of Measurement Enable Register 2 (0x51) */
46 #define MEAS2_VRTC (1 << 0)
48 /* bit definitions of RTC Register 1 (0xA0) */
49 #define ALARM_EN (1 << 3)
50 #define ALARM_WAKEUP (1 << 4)
51 #define ALARM (1 << 5)
52 #define RTC1_USE_XO (1 << 7)
54 #define VRTC_CALIB_INTERVAL (HZ * 60 * 10) /* 10 minutes */
56 static irqreturn_t rtc_update_handler(int irq, void *data)
58 struct pm860x_rtc_info *info = (struct pm860x_rtc_info *)data;
61 mask = ALARM | ALARM_WAKEUP;
62 pm860x_set_bits(info->i2c, PM8607_RTC1, mask | ALARM_EN, mask);
63 rtc_update_irq(info->rtc_dev, 1, RTC_AF);
67 static int pm860x_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
69 struct pm860x_rtc_info *info = dev_get_drvdata(dev);
72 pm860x_set_bits(info->i2c, PM8607_RTC1, ALARM_EN, ALARM_EN);
74 pm860x_set_bits(info->i2c, PM8607_RTC1, ALARM_EN, 0);
78 static int pm860x_rtc_read_time(struct device *dev, struct rtc_time *tm)
80 struct pm860x_rtc_info *info = dev_get_drvdata(dev);
82 unsigned long ticks, base, data;
84 pm860x_page_bulk_read(info->i2c, REG0_ADDR, 8, buf);
85 dev_dbg(info->dev, "%x-%x-%x-%x-%x-%x-%x-%x\n", buf[0], buf[1],
86 buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
87 base = ((unsigned long)buf[1] << 24) | (buf[3] << 16) |
88 (buf[5] << 8) | buf[7];
90 /* load 32-bit read-only counter */
91 pm860x_bulk_read(info->i2c, PM8607_RTC_COUNTER1, 4, buf);
92 data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
93 (buf[1] << 8) | buf[0];
95 dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
98 rtc_time64_to_tm(ticks, tm);
103 static int pm860x_rtc_set_time(struct device *dev, struct rtc_time *tm)
105 struct pm860x_rtc_info *info = dev_get_drvdata(dev);
106 unsigned char buf[4];
107 unsigned long ticks, base, data;
109 ticks = rtc_tm_to_time64(tm);
111 /* load 32-bit read-only counter */
112 pm860x_bulk_read(info->i2c, PM8607_RTC_COUNTER1, 4, buf);
113 data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
114 (buf[1] << 8) | buf[0];
116 dev_dbg(info->dev, "set base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
119 pm860x_page_reg_write(info->i2c, REG0_DATA, (base >> 24) & 0xFF);
120 pm860x_page_reg_write(info->i2c, REG1_DATA, (base >> 16) & 0xFF);
121 pm860x_page_reg_write(info->i2c, REG2_DATA, (base >> 8) & 0xFF);
122 pm860x_page_reg_write(info->i2c, REG3_DATA, base & 0xFF);
127 static int pm860x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
129 struct pm860x_rtc_info *info = dev_get_drvdata(dev);
130 unsigned char buf[8];
131 unsigned long ticks, base, data;
134 pm860x_page_bulk_read(info->i2c, REG0_ADDR, 8, buf);
135 dev_dbg(info->dev, "%x-%x-%x-%x-%x-%x-%x-%x\n", buf[0], buf[1],
136 buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
137 base = ((unsigned long)buf[1] << 24) | (buf[3] << 16) |
138 (buf[5] << 8) | buf[7];
140 pm860x_bulk_read(info->i2c, PM8607_RTC_EXPIRE1, 4, buf);
141 data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
142 (buf[1] << 8) | buf[0];
144 dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
147 rtc_time64_to_tm(ticks, &alrm->time);
148 ret = pm860x_reg_read(info->i2c, PM8607_RTC1);
149 alrm->enabled = (ret & ALARM_EN) ? 1 : 0;
150 alrm->pending = (ret & (ALARM | ALARM_WAKEUP)) ? 1 : 0;
154 static int pm860x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
156 struct pm860x_rtc_info *info = dev_get_drvdata(dev);
157 unsigned long ticks, base, data;
158 unsigned char buf[8];
161 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 = ((unsigned long)buf[1] << 24) | (buf[3] << 16) |
167 (buf[5] << 8) | buf[7];
169 ticks = rtc_tm_to_time64(&alrm->time);
172 buf[0] = data & 0xff;
173 buf[1] = (data >> 8) & 0xff;
174 buf[2] = (data >> 16) & 0xff;
175 buf[3] = (data >> 24) & 0xff;
176 pm860x_bulk_write(info->i2c, PM8607_RTC_EXPIRE1, 4, buf);
178 mask = ALARM | ALARM_WAKEUP | ALARM_EN;
179 pm860x_set_bits(info->i2c, PM8607_RTC1, mask, mask);
181 mask = ALARM | ALARM_WAKEUP | ALARM_EN;
182 pm860x_set_bits(info->i2c, PM8607_RTC1, mask,
183 ALARM | ALARM_WAKEUP);
188 static const struct rtc_class_ops pm860x_rtc_ops = {
189 .read_time = pm860x_rtc_read_time,
190 .set_time = pm860x_rtc_set_time,
191 .read_alarm = pm860x_rtc_read_alarm,
192 .set_alarm = pm860x_rtc_set_alarm,
193 .alarm_irq_enable = pm860x_rtc_alarm_irq_enable,
196 #ifdef VRTC_CALIBRATION
197 static void calibrate_vrtc_work(struct work_struct *work)
199 struct pm860x_rtc_info *info = container_of(work,
200 struct pm860x_rtc_info, calib_work.work);
201 unsigned char buf[2];
202 unsigned int sum, data, mean, vrtc_set;
205 for (i = 0, sum = 0; i < 16; i++) {
207 pm860x_bulk_read(info->i2c, REG_VRTC_MEAS1, 2, buf);
208 data = (buf[0] << 4) | buf[1];
209 data = (data * 5400) >> 12; /* convert to mv */
213 vrtc_set = 2700 + (info->vrtc & 0x3) * 200;
214 dev_dbg(info->dev, "mean:%d, vrtc_set:%d\n", mean, vrtc_set);
216 sum = pm860x_reg_read(info->i2c, PM8607_RTC_MISC1);
218 if ((mean + 200) < vrtc_set) {
219 /* try higher voltage */
222 data = (sum & 0xf8) | (data & 0x3);
223 pm860x_reg_write(info->i2c, PM8607_RTC_MISC1, data);
224 } else if ((mean - 200) > vrtc_set) {
225 /* try lower voltage */
228 data = (sum & 0xf8) | (data & 0x3);
229 pm860x_reg_write(info->i2c, PM8607_RTC_MISC1, data);
232 dev_dbg(info->dev, "set 0x%x to RTC_MISC1\n", data);
233 /* trigger next calibration since VRTC is updated */
234 schedule_delayed_work(&info->calib_work, VRTC_CALIB_INTERVAL);
237 /* disable measurement */
238 pm860x_set_bits(info->i2c, PM8607_MEAS_EN2, MEAS2_VRTC, 0);
239 dev_dbg(info->dev, "finish VRTC calibration\n");
245 static int pm860x_rtc_dt_init(struct platform_device *pdev,
246 struct pm860x_rtc_info *info)
248 struct device_node *np = pdev->dev.parent->of_node;
252 np = of_get_child_by_name(np, "rtc");
254 dev_err(&pdev->dev, "failed to find rtc node\n");
257 ret = of_property_read_u32(np, "marvell,88pm860x-vrtc", &info->vrtc);
264 #define pm860x_rtc_dt_init(x, y) do { } while (0)
267 static int pm860x_rtc_probe(struct platform_device *pdev)
269 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
270 struct pm860x_rtc_info *info;
273 info = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_rtc_info),
277 info->irq = platform_get_irq(pdev, 0);
282 info->i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
283 info->dev = &pdev->dev;
284 dev_set_drvdata(&pdev->dev, info);
286 info->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
287 if (IS_ERR(info->rtc_dev))
288 return PTR_ERR(info->rtc_dev);
290 ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
291 rtc_update_handler, IRQF_ONESHOT, "rtc",
294 dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
299 /* set addresses of 32-bit base value for RTC time */
300 pm860x_page_reg_write(info->i2c, REG0_ADDR, REG0_DATA);
301 pm860x_page_reg_write(info->i2c, REG1_ADDR, REG1_DATA);
302 pm860x_page_reg_write(info->i2c, REG2_ADDR, REG2_DATA);
303 pm860x_page_reg_write(info->i2c, REG3_ADDR, REG3_DATA);
305 pm860x_rtc_dt_init(pdev, info);
307 info->rtc_dev->ops = &pm860x_rtc_ops;
308 info->rtc_dev->range_max = U32_MAX;
310 ret = rtc_register_device(info->rtc_dev);
315 * enable internal XO instead of internal 3.25MHz clock since it can
316 * free running in PMIC power-down state.
318 pm860x_set_bits(info->i2c, PM8607_RTC1, RTC1_USE_XO, RTC1_USE_XO);
320 #ifdef VRTC_CALIBRATION
321 /* <00> -- 2.7V, <01> -- 2.9V, <10> -- 3.1V, <11> -- 3.3V */
322 pm860x_set_bits(info->i2c, PM8607_MEAS_EN2, MEAS2_VRTC, MEAS2_VRTC);
325 INIT_DELAYED_WORK(&info->calib_work, calibrate_vrtc_work);
326 schedule_delayed_work(&info->calib_work, VRTC_CALIB_INTERVAL);
327 #endif /* VRTC_CALIBRATION */
329 device_init_wakeup(&pdev->dev, 1);
334 static int pm860x_rtc_remove(struct platform_device *pdev)
336 struct pm860x_rtc_info *info = platform_get_drvdata(pdev);
338 #ifdef VRTC_CALIBRATION
339 cancel_delayed_work_sync(&info->calib_work);
340 /* disable measurement */
341 pm860x_set_bits(info->i2c, PM8607_MEAS_EN2, MEAS2_VRTC, 0);
342 #endif /* VRTC_CALIBRATION */
347 #ifdef CONFIG_PM_SLEEP
348 static int pm860x_rtc_suspend(struct device *dev)
350 struct platform_device *pdev = to_platform_device(dev);
351 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
353 if (device_may_wakeup(dev))
354 chip->wakeup_flag |= 1 << PM8607_IRQ_RTC;
357 static int pm860x_rtc_resume(struct device *dev)
359 struct platform_device *pdev = to_platform_device(dev);
360 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
362 if (device_may_wakeup(dev))
363 chip->wakeup_flag &= ~(1 << PM8607_IRQ_RTC);
368 static SIMPLE_DEV_PM_OPS(pm860x_rtc_pm_ops, pm860x_rtc_suspend, pm860x_rtc_resume);
370 static struct platform_driver pm860x_rtc_driver = {
372 .name = "88pm860x-rtc",
373 .pm = &pm860x_rtc_pm_ops,
375 .probe = pm860x_rtc_probe,
376 .remove = pm860x_rtc_remove,
379 module_platform_driver(pm860x_rtc_driver);
381 MODULE_DESCRIPTION("Marvell 88PM860x RTC driver");
382 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
383 MODULE_LICENSE("GPL");