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