xref: /openbmc/linux/drivers/rtc/rtc-88pm860x.c (revision 4bb9d46d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/of.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>
19 
20 #define VRTC_CALIBRATION
21 
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 
29 	int			irq;
30 	int			vrtc;
31 };
32 
33 #define REG_VRTC_MEAS1		0x7D
34 
35 #define REG0_ADDR		0xB0
36 #define REG1_ADDR		0xB2
37 #define REG2_ADDR		0xB4
38 #define REG3_ADDR		0xB6
39 
40 #define REG0_DATA		0xB1
41 #define REG1_DATA		0xB3
42 #define REG2_DATA		0xB5
43 #define REG3_DATA		0xB7
44 
45 /* bit definitions of Measurement Enable Register 2 (0x51) */
46 #define MEAS2_VRTC		(1 << 0)
47 
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)
53 
54 #define VRTC_CALIB_INTERVAL	(HZ * 60 * 10)		/* 10 minutes */
55 
56 static irqreturn_t rtc_update_handler(int irq, void *data)
57 {
58 	struct pm860x_rtc_info *info = (struct pm860x_rtc_info *)data;
59 	int mask;
60 
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);
64 	return IRQ_HANDLED;
65 }
66 
67 static int pm860x_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
68 {
69 	struct pm860x_rtc_info *info = dev_get_drvdata(dev);
70 
71 	if (enabled)
72 		pm860x_set_bits(info->i2c, PM8607_RTC1, ALARM_EN, ALARM_EN);
73 	else
74 		pm860x_set_bits(info->i2c, PM8607_RTC1, ALARM_EN, 0);
75 	return 0;
76 }
77 
78 static int pm860x_rtc_read_time(struct device *dev, struct rtc_time *tm)
79 {
80 	struct pm860x_rtc_info *info = dev_get_drvdata(dev);
81 	unsigned char buf[8];
82 	unsigned long ticks, base, data;
83 
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];
89 
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];
94 	ticks = base + data;
95 	dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
96 		base, data, ticks);
97 
98 	rtc_time64_to_tm(ticks, tm);
99 
100 	return 0;
101 }
102 
103 static int pm860x_rtc_set_time(struct device *dev, struct rtc_time *tm)
104 {
105 	struct pm860x_rtc_info *info = dev_get_drvdata(dev);
106 	unsigned char buf[4];
107 	unsigned long ticks, base, data;
108 
109 	if (tm->tm_year > 206) {
110 		dev_dbg(info->dev, "Set time %d out of range. "
111 			"Please set time between 1970 to 2106.\n",
112 			1900 + tm->tm_year);
113 		return -EINVAL;
114 	}
115 	ticks = rtc_tm_to_time64(tm);
116 
117 	/* load 32-bit read-only counter */
118 	pm860x_bulk_read(info->i2c, PM8607_RTC_COUNTER1, 4, buf);
119 	data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
120 		(buf[1] << 8) | buf[0];
121 	base = ticks - data;
122 	dev_dbg(info->dev, "set base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
123 		base, data, ticks);
124 
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 
130 	return 0;
131 }
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 
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 = ((unsigned long)buf[1] << 24) | (buf[3] << 16) |
144 		(buf[5] << 8) | buf[7];
145 
146 	pm860x_bulk_read(info->i2c, PM8607_RTC_EXPIRE1, 4, buf);
147 	data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
148 		(buf[1] << 8) | buf[0];
149 	ticks = base + data;
150 	dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
151 		base, data, ticks);
152 
153 	rtc_time64_to_tm(ticks, &alrm->time);
154 	ret = pm860x_reg_read(info->i2c, PM8607_RTC1);
155 	alrm->enabled = (ret & ALARM_EN) ? 1 : 0;
156 	alrm->pending = (ret & (ALARM | ALARM_WAKEUP)) ? 1 : 0;
157 	return 0;
158 }
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 	unsigned long ticks, base, data;
164 	unsigned char buf[8];
165 	int mask;
166 
167 	pm860x_set_bits(info->i2c, PM8607_RTC1, ALARM_EN, 0);
168 
169 	pm860x_page_bulk_read(info->i2c, REG0_ADDR, 8, buf);
170 	dev_dbg(info->dev, "%x-%x-%x-%x-%x-%x-%x-%x\n", buf[0], buf[1],
171 		buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
172 	base = ((unsigned long)buf[1] << 24) | (buf[3] << 16) |
173 		(buf[5] << 8) | buf[7];
174 
175 	ticks = rtc_tm_to_time64(&alrm->time);
176 	data = ticks - base;
177 
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 
194 static const struct rtc_class_ops pm860x_rtc_ops = {
195 	.read_time	= pm860x_rtc_read_time,
196 	.set_time	= pm860x_rtc_set_time,
197 	.read_alarm	= pm860x_rtc_read_alarm,
198 	.set_alarm	= pm860x_rtc_set_alarm,
199 	.alarm_irq_enable = pm860x_rtc_alarm_irq_enable,
200 };
201 
202 #ifdef VRTC_CALIBRATION
203 static void calibrate_vrtc_work(struct work_struct *work)
204 {
205 	struct pm860x_rtc_info *info = container_of(work,
206 		struct pm860x_rtc_info, calib_work.work);
207 	unsigned char buf[2];
208 	unsigned int sum, data, mean, vrtc_set;
209 	int i;
210 
211 	for (i = 0, sum = 0; i < 16; i++) {
212 		msleep(100);
213 		pm860x_bulk_read(info->i2c, REG_VRTC_MEAS1, 2, buf);
214 		data = (buf[0] << 4) | buf[1];
215 		data = (data * 5400) >> 12;	/* convert to mv */
216 		sum += data;
217 	}
218 	mean = sum >> 4;
219 	vrtc_set = 2700 + (info->vrtc & 0x3) * 200;
220 	dev_dbg(info->dev, "mean:%d, vrtc_set:%d\n", mean, vrtc_set);
221 
222 	sum = pm860x_reg_read(info->i2c, PM8607_RTC_MISC1);
223 	data = sum & 0x3;
224 	if ((mean + 200) < vrtc_set) {
225 		/* try higher voltage */
226 		if (++data == 4)
227 			goto out;
228 		data = (sum & 0xf8) | (data & 0x3);
229 		pm860x_reg_write(info->i2c, PM8607_RTC_MISC1, data);
230 	} else if ((mean - 200) > vrtc_set) {
231 		/* try lower voltage */
232 		if (data-- == 0)
233 			goto out;
234 		data = (sum & 0xf8) | (data & 0x3);
235 		pm860x_reg_write(info->i2c, PM8607_RTC_MISC1, data);
236 	} else
237 		goto out;
238 	dev_dbg(info->dev, "set 0x%x to RTC_MISC1\n", data);
239 	/* trigger next calibration since VRTC is updated */
240 	schedule_delayed_work(&info->calib_work, VRTC_CALIB_INTERVAL);
241 	return;
242 out:
243 	/* disable measurement */
244 	pm860x_set_bits(info->i2c, PM8607_MEAS_EN2, MEAS2_VRTC, 0);
245 	dev_dbg(info->dev, "finish VRTC calibration\n");
246 	return;
247 }
248 #endif
249 
250 #ifdef CONFIG_OF
251 static int pm860x_rtc_dt_init(struct platform_device *pdev,
252 			      struct pm860x_rtc_info *info)
253 {
254 	struct device_node *np = pdev->dev.parent->of_node;
255 	int ret;
256 	if (!np)
257 		return -ENODEV;
258 	np = of_get_child_by_name(np, "rtc");
259 	if (!np) {
260 		dev_err(&pdev->dev, "failed to find rtc node\n");
261 		return -ENODEV;
262 	}
263 	ret = of_property_read_u32(np, "marvell,88pm860x-vrtc", &info->vrtc);
264 	if (ret)
265 		info->vrtc = 0;
266 	of_node_put(np);
267 	return 0;
268 }
269 #else
270 #define pm860x_rtc_dt_init(x, y)	do { } while (0)
271 #endif
272 
273 static int pm860x_rtc_probe(struct platform_device *pdev)
274 {
275 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
276 	struct pm860x_rtc_info *info;
277 	int ret;
278 
279 	info = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_rtc_info),
280 			    GFP_KERNEL);
281 	if (!info)
282 		return -ENOMEM;
283 	info->irq = platform_get_irq(pdev, 0);
284 	if (info->irq < 0)
285 		return info->irq;
286 
287 	info->chip = chip;
288 	info->i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
289 	info->dev = &pdev->dev;
290 	dev_set_drvdata(&pdev->dev, info);
291 
292 	info->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
293 	if (IS_ERR(info->rtc_dev))
294 		return PTR_ERR(info->rtc_dev);
295 
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 
305 	/* set addresses of 32-bit base value for RTC time */
306 	pm860x_page_reg_write(info->i2c, REG0_ADDR, REG0_DATA);
307 	pm860x_page_reg_write(info->i2c, REG1_ADDR, REG1_DATA);
308 	pm860x_page_reg_write(info->i2c, REG2_ADDR, REG2_DATA);
309 	pm860x_page_reg_write(info->i2c, REG3_ADDR, REG3_DATA);
310 
311 	pm860x_rtc_dt_init(pdev, info);
312 
313 	info->rtc_dev->ops = &pm860x_rtc_ops;
314 	info->rtc_dev->range_max = U32_MAX;
315 
316 	ret = rtc_register_device(info->rtc_dev);
317 	if (ret)
318 		return ret;
319 
320 	/*
321 	 * enable internal XO instead of internal 3.25MHz clock since it can
322 	 * free running in PMIC power-down state.
323 	 */
324 	pm860x_set_bits(info->i2c, PM8607_RTC1, RTC1_USE_XO, RTC1_USE_XO);
325 
326 #ifdef VRTC_CALIBRATION
327 	/* <00> -- 2.7V, <01> -- 2.9V, <10> -- 3.1V, <11> -- 3.3V */
328 	pm860x_set_bits(info->i2c, PM8607_MEAS_EN2, MEAS2_VRTC, MEAS2_VRTC);
329 
330 	/* calibrate VRTC */
331 	INIT_DELAYED_WORK(&info->calib_work, calibrate_vrtc_work);
332 	schedule_delayed_work(&info->calib_work, VRTC_CALIB_INTERVAL);
333 #endif	/* VRTC_CALIBRATION */
334 
335 	device_init_wakeup(&pdev->dev, 1);
336 
337 	return 0;
338 }
339 
340 static int pm860x_rtc_remove(struct platform_device *pdev)
341 {
342 	struct pm860x_rtc_info *info = platform_get_drvdata(pdev);
343 
344 #ifdef VRTC_CALIBRATION
345 	cancel_delayed_work_sync(&info->calib_work);
346 	/* disable measurement */
347 	pm860x_set_bits(info->i2c, PM8607_MEAS_EN2, MEAS2_VRTC, 0);
348 #endif	/* VRTC_CALIBRATION */
349 
350 	return 0;
351 }
352 
353 #ifdef CONFIG_PM_SLEEP
354 static int pm860x_rtc_suspend(struct device *dev)
355 {
356 	struct platform_device *pdev = to_platform_device(dev);
357 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
358 
359 	if (device_may_wakeup(dev))
360 		chip->wakeup_flag |= 1 << PM8607_IRQ_RTC;
361 	return 0;
362 }
363 static int pm860x_rtc_resume(struct device *dev)
364 {
365 	struct platform_device *pdev = to_platform_device(dev);
366 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
367 
368 	if (device_may_wakeup(dev))
369 		chip->wakeup_flag &= ~(1 << PM8607_IRQ_RTC);
370 	return 0;
371 }
372 #endif
373 
374 static SIMPLE_DEV_PM_OPS(pm860x_rtc_pm_ops, pm860x_rtc_suspend, pm860x_rtc_resume);
375 
376 static struct platform_driver pm860x_rtc_driver = {
377 	.driver		= {
378 		.name	= "88pm860x-rtc",
379 		.pm	= &pm860x_rtc_pm_ops,
380 	},
381 	.probe		= pm860x_rtc_probe,
382 	.remove		= pm860x_rtc_remove,
383 };
384 
385 module_platform_driver(pm860x_rtc_driver);
386 
387 MODULE_DESCRIPTION("Marvell 88PM860x RTC driver");
388 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
389 MODULE_LICENSE("GPL");
390