Linux Audio

Check our new training course

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