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