xref: /OK3568_Linux_fs/kernel/drivers/rtc/rtc-ds1374.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * RTC client/driver for the Maxim/Dallas DS1374 Real-Time Clock over I2C
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Based on code by Randy Vinson <rvinson@mvista.com>,
5*4882a593Smuzhiyun  * which was based on the m41t00.c by Mark Greer <mgreer@mvista.com>.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (C) 2014 Rose Technology
8*4882a593Smuzhiyun  * Copyright (C) 2006-2007 Freescale Semiconductor
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * 2005 (c) MontaVista Software, Inc. This file is licensed under
11*4882a593Smuzhiyun  * the terms of the GNU General Public License version 2. This program
12*4882a593Smuzhiyun  * is licensed "as is" without any warranty of any kind, whether express
13*4882a593Smuzhiyun  * or implied.
14*4882a593Smuzhiyun  */
15*4882a593Smuzhiyun /*
16*4882a593Smuzhiyun  * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
17*4882a593Smuzhiyun  * recommended in .../Documentation/i2c/writing-clients.rst section
18*4882a593Smuzhiyun  * "Sending and receiving", using SMBus level communication is preferred.
19*4882a593Smuzhiyun  */
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include <linux/kernel.h>
24*4882a593Smuzhiyun #include <linux/module.h>
25*4882a593Smuzhiyun #include <linux/interrupt.h>
26*4882a593Smuzhiyun #include <linux/i2c.h>
27*4882a593Smuzhiyun #include <linux/rtc.h>
28*4882a593Smuzhiyun #include <linux/bcd.h>
29*4882a593Smuzhiyun #include <linux/workqueue.h>
30*4882a593Smuzhiyun #include <linux/slab.h>
31*4882a593Smuzhiyun #include <linux/pm.h>
32*4882a593Smuzhiyun #ifdef CONFIG_RTC_DRV_DS1374_WDT
33*4882a593Smuzhiyun #include <linux/fs.h>
34*4882a593Smuzhiyun #include <linux/ioctl.h>
35*4882a593Smuzhiyun #include <linux/miscdevice.h>
36*4882a593Smuzhiyun #include <linux/reboot.h>
37*4882a593Smuzhiyun #include <linux/watchdog.h>
38*4882a593Smuzhiyun #endif
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #define DS1374_REG_TOD0		0x00 /* Time of Day */
41*4882a593Smuzhiyun #define DS1374_REG_TOD1		0x01
42*4882a593Smuzhiyun #define DS1374_REG_TOD2		0x02
43*4882a593Smuzhiyun #define DS1374_REG_TOD3		0x03
44*4882a593Smuzhiyun #define DS1374_REG_WDALM0	0x04 /* Watchdog/Alarm */
45*4882a593Smuzhiyun #define DS1374_REG_WDALM1	0x05
46*4882a593Smuzhiyun #define DS1374_REG_WDALM2	0x06
47*4882a593Smuzhiyun #define DS1374_REG_CR		0x07 /* Control */
48*4882a593Smuzhiyun #define DS1374_REG_CR_AIE	0x01 /* Alarm Int. Enable */
49*4882a593Smuzhiyun #define DS1374_REG_CR_WDSTR	0x08 /* 1=INT, 0=RST */
50*4882a593Smuzhiyun #define DS1374_REG_CR_WDALM	0x20 /* 1=Watchdog, 0=Alarm */
51*4882a593Smuzhiyun #define DS1374_REG_CR_WACE	0x40 /* WD/Alarm counter enable */
52*4882a593Smuzhiyun #define DS1374_REG_SR		0x08 /* Status */
53*4882a593Smuzhiyun #define DS1374_REG_SR_OSF	0x80 /* Oscillator Stop Flag */
54*4882a593Smuzhiyun #define DS1374_REG_SR_AF	0x01 /* Alarm Flag */
55*4882a593Smuzhiyun #define DS1374_REG_TCR		0x09 /* Trickle Charge */
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun static const struct i2c_device_id ds1374_id[] = {
58*4882a593Smuzhiyun 	{ "ds1374", 0 },
59*4882a593Smuzhiyun 	{ }
60*4882a593Smuzhiyun };
61*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, ds1374_id);
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun #ifdef CONFIG_OF
64*4882a593Smuzhiyun static const struct of_device_id ds1374_of_match[] = {
65*4882a593Smuzhiyun 	{ .compatible = "dallas,ds1374" },
66*4882a593Smuzhiyun 	{ }
67*4882a593Smuzhiyun };
68*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, ds1374_of_match);
69*4882a593Smuzhiyun #endif
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun struct ds1374 {
72*4882a593Smuzhiyun 	struct i2c_client *client;
73*4882a593Smuzhiyun 	struct rtc_device *rtc;
74*4882a593Smuzhiyun 	struct work_struct work;
75*4882a593Smuzhiyun #ifdef CONFIG_RTC_DRV_DS1374_WDT
76*4882a593Smuzhiyun 	struct watchdog_device wdt;
77*4882a593Smuzhiyun #endif
78*4882a593Smuzhiyun 	/* The mutex protects alarm operations, and prevents a race
79*4882a593Smuzhiyun 	 * between the enable_irq() in the workqueue and the free_irq()
80*4882a593Smuzhiyun 	 * in the remove function.
81*4882a593Smuzhiyun 	 */
82*4882a593Smuzhiyun 	struct mutex mutex;
83*4882a593Smuzhiyun 	int exiting;
84*4882a593Smuzhiyun };
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun static struct i2c_driver ds1374_driver;
87*4882a593Smuzhiyun 
ds1374_read_rtc(struct i2c_client * client,u32 * time,int reg,int nbytes)88*4882a593Smuzhiyun static int ds1374_read_rtc(struct i2c_client *client, u32 *time,
89*4882a593Smuzhiyun 			   int reg, int nbytes)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	u8 buf[4];
92*4882a593Smuzhiyun 	int ret;
93*4882a593Smuzhiyun 	int i;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	if (WARN_ON(nbytes > 4))
96*4882a593Smuzhiyun 		return -EINVAL;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	ret = i2c_smbus_read_i2c_block_data(client, reg, nbytes, buf);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	if (ret < 0)
101*4882a593Smuzhiyun 		return ret;
102*4882a593Smuzhiyun 	if (ret < nbytes)
103*4882a593Smuzhiyun 		return -EIO;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	for (i = nbytes - 1, *time = 0; i >= 0; i--)
106*4882a593Smuzhiyun 		*time = (*time << 8) | buf[i];
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	return 0;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun 
ds1374_write_rtc(struct i2c_client * client,u32 time,int reg,int nbytes)111*4882a593Smuzhiyun static int ds1374_write_rtc(struct i2c_client *client, u32 time,
112*4882a593Smuzhiyun 			    int reg, int nbytes)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun 	u8 buf[4];
115*4882a593Smuzhiyun 	int i;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	if (nbytes > 4) {
118*4882a593Smuzhiyun 		WARN_ON(1);
119*4882a593Smuzhiyun 		return -EINVAL;
120*4882a593Smuzhiyun 	}
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	for (i = 0; i < nbytes; i++) {
123*4882a593Smuzhiyun 		buf[i] = time & 0xff;
124*4882a593Smuzhiyun 		time >>= 8;
125*4882a593Smuzhiyun 	}
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	return i2c_smbus_write_i2c_block_data(client, reg, nbytes, buf);
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun 
ds1374_check_rtc_status(struct i2c_client * client)130*4882a593Smuzhiyun static int ds1374_check_rtc_status(struct i2c_client *client)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun 	int ret = 0;
133*4882a593Smuzhiyun 	int control, stat;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
136*4882a593Smuzhiyun 	if (stat < 0)
137*4882a593Smuzhiyun 		return stat;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	if (stat & DS1374_REG_SR_OSF)
140*4882a593Smuzhiyun 		dev_warn(&client->dev,
141*4882a593Smuzhiyun 			 "oscillator discontinuity flagged, time unreliable\n");
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	stat &= ~(DS1374_REG_SR_OSF | DS1374_REG_SR_AF);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	ret = i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
146*4882a593Smuzhiyun 	if (ret < 0)
147*4882a593Smuzhiyun 		return ret;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	/* If the alarm is pending, clear it before requesting
150*4882a593Smuzhiyun 	 * the interrupt, so an interrupt event isn't reported
151*4882a593Smuzhiyun 	 * before everything is initialized.
152*4882a593Smuzhiyun 	 */
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
155*4882a593Smuzhiyun 	if (control < 0)
156*4882a593Smuzhiyun 		return control;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
159*4882a593Smuzhiyun 	return i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
ds1374_read_time(struct device * dev,struct rtc_time * time)162*4882a593Smuzhiyun static int ds1374_read_time(struct device *dev, struct rtc_time *time)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
165*4882a593Smuzhiyun 	u32 itime;
166*4882a593Smuzhiyun 	int ret;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	ret = ds1374_read_rtc(client, &itime, DS1374_REG_TOD0, 4);
169*4882a593Smuzhiyun 	if (!ret)
170*4882a593Smuzhiyun 		rtc_time64_to_tm(itime, time);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	return ret;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
ds1374_set_time(struct device * dev,struct rtc_time * time)175*4882a593Smuzhiyun static int ds1374_set_time(struct device *dev, struct rtc_time *time)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
178*4882a593Smuzhiyun 	unsigned long itime = rtc_tm_to_time64(time);
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	return ds1374_write_rtc(client, itime, DS1374_REG_TOD0, 4);
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun #ifndef CONFIG_RTC_DRV_DS1374_WDT
184*4882a593Smuzhiyun /* The ds1374 has a decrementer for an alarm, rather than a comparator.
185*4882a593Smuzhiyun  * If the time of day is changed, then the alarm will need to be
186*4882a593Smuzhiyun  * reset.
187*4882a593Smuzhiyun  */
ds1374_read_alarm(struct device * dev,struct rtc_wkalrm * alarm)188*4882a593Smuzhiyun static int ds1374_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
191*4882a593Smuzhiyun 	struct ds1374 *ds1374 = i2c_get_clientdata(client);
192*4882a593Smuzhiyun 	u32 now, cur_alarm;
193*4882a593Smuzhiyun 	int cr, sr;
194*4882a593Smuzhiyun 	int ret = 0;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	if (client->irq <= 0)
197*4882a593Smuzhiyun 		return -EINVAL;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	mutex_lock(&ds1374->mutex);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	cr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
202*4882a593Smuzhiyun 	if (ret < 0)
203*4882a593Smuzhiyun 		goto out;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	sr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
206*4882a593Smuzhiyun 	if (ret < 0)
207*4882a593Smuzhiyun 		goto out;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	ret = ds1374_read_rtc(client, &now, DS1374_REG_TOD0, 4);
210*4882a593Smuzhiyun 	if (ret)
211*4882a593Smuzhiyun 		goto out;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	ret = ds1374_read_rtc(client, &cur_alarm, DS1374_REG_WDALM0, 3);
214*4882a593Smuzhiyun 	if (ret)
215*4882a593Smuzhiyun 		goto out;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	rtc_time64_to_tm(now + cur_alarm, &alarm->time);
218*4882a593Smuzhiyun 	alarm->enabled = !!(cr & DS1374_REG_CR_WACE);
219*4882a593Smuzhiyun 	alarm->pending = !!(sr & DS1374_REG_SR_AF);
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun out:
222*4882a593Smuzhiyun 	mutex_unlock(&ds1374->mutex);
223*4882a593Smuzhiyun 	return ret;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
ds1374_set_alarm(struct device * dev,struct rtc_wkalrm * alarm)226*4882a593Smuzhiyun static int ds1374_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
229*4882a593Smuzhiyun 	struct ds1374 *ds1374 = i2c_get_clientdata(client);
230*4882a593Smuzhiyun 	struct rtc_time now;
231*4882a593Smuzhiyun 	unsigned long new_alarm, itime;
232*4882a593Smuzhiyun 	int cr;
233*4882a593Smuzhiyun 	int ret = 0;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	if (client->irq <= 0)
236*4882a593Smuzhiyun 		return -EINVAL;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	ret = ds1374_read_time(dev, &now);
239*4882a593Smuzhiyun 	if (ret < 0)
240*4882a593Smuzhiyun 		return ret;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	new_alarm = rtc_tm_to_time64(&alarm->time);
243*4882a593Smuzhiyun 	itime = rtc_tm_to_time64(&now);
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	/* This can happen due to races, in addition to dates that are
246*4882a593Smuzhiyun 	 * truly in the past.  To avoid requiring the caller to check for
247*4882a593Smuzhiyun 	 * races, dates in the past are assumed to be in the recent past
248*4882a593Smuzhiyun 	 * (i.e. not something that we'd rather the caller know about via
249*4882a593Smuzhiyun 	 * an error), and the alarm is set to go off as soon as possible.
250*4882a593Smuzhiyun 	 */
251*4882a593Smuzhiyun 	if (time_before_eq(new_alarm, itime))
252*4882a593Smuzhiyun 		new_alarm = 1;
253*4882a593Smuzhiyun 	else
254*4882a593Smuzhiyun 		new_alarm -= itime;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	mutex_lock(&ds1374->mutex);
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	ret = cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
259*4882a593Smuzhiyun 	if (ret < 0)
260*4882a593Smuzhiyun 		goto out;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	/* Disable any existing alarm before setting the new one
263*4882a593Smuzhiyun 	 * (or lack thereof). */
264*4882a593Smuzhiyun 	cr &= ~DS1374_REG_CR_WACE;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
267*4882a593Smuzhiyun 	if (ret < 0)
268*4882a593Smuzhiyun 		goto out;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	ret = ds1374_write_rtc(client, new_alarm, DS1374_REG_WDALM0, 3);
271*4882a593Smuzhiyun 	if (ret)
272*4882a593Smuzhiyun 		goto out;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	if (alarm->enabled) {
275*4882a593Smuzhiyun 		cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
276*4882a593Smuzhiyun 		cr &= ~DS1374_REG_CR_WDALM;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 		ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
279*4882a593Smuzhiyun 	}
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun out:
282*4882a593Smuzhiyun 	mutex_unlock(&ds1374->mutex);
283*4882a593Smuzhiyun 	return ret;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun #endif
286*4882a593Smuzhiyun 
ds1374_irq(int irq,void * dev_id)287*4882a593Smuzhiyun static irqreturn_t ds1374_irq(int irq, void *dev_id)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun 	struct i2c_client *client = dev_id;
290*4882a593Smuzhiyun 	struct ds1374 *ds1374 = i2c_get_clientdata(client);
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	disable_irq_nosync(irq);
293*4882a593Smuzhiyun 	schedule_work(&ds1374->work);
294*4882a593Smuzhiyun 	return IRQ_HANDLED;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun 
ds1374_work(struct work_struct * work)297*4882a593Smuzhiyun static void ds1374_work(struct work_struct *work)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun 	struct ds1374 *ds1374 = container_of(work, struct ds1374, work);
300*4882a593Smuzhiyun 	struct i2c_client *client = ds1374->client;
301*4882a593Smuzhiyun 	int stat, control;
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	mutex_lock(&ds1374->mutex);
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
306*4882a593Smuzhiyun 	if (stat < 0)
307*4882a593Smuzhiyun 		goto unlock;
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	if (stat & DS1374_REG_SR_AF) {
310*4882a593Smuzhiyun 		stat &= ~DS1374_REG_SR_AF;
311*4882a593Smuzhiyun 		i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 		control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
314*4882a593Smuzhiyun 		if (control < 0)
315*4882a593Smuzhiyun 			goto out;
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 		control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
318*4882a593Smuzhiyun 		i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 		rtc_update_irq(ds1374->rtc, 1, RTC_AF | RTC_IRQF);
321*4882a593Smuzhiyun 	}
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun out:
324*4882a593Smuzhiyun 	if (!ds1374->exiting)
325*4882a593Smuzhiyun 		enable_irq(client->irq);
326*4882a593Smuzhiyun unlock:
327*4882a593Smuzhiyun 	mutex_unlock(&ds1374->mutex);
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun #ifndef CONFIG_RTC_DRV_DS1374_WDT
ds1374_alarm_irq_enable(struct device * dev,unsigned int enabled)331*4882a593Smuzhiyun static int ds1374_alarm_irq_enable(struct device *dev, unsigned int enabled)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
334*4882a593Smuzhiyun 	struct ds1374 *ds1374 = i2c_get_clientdata(client);
335*4882a593Smuzhiyun 	int ret;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	mutex_lock(&ds1374->mutex);
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
340*4882a593Smuzhiyun 	if (ret < 0)
341*4882a593Smuzhiyun 		goto out;
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	if (enabled) {
344*4882a593Smuzhiyun 		ret |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
345*4882a593Smuzhiyun 		ret &= ~DS1374_REG_CR_WDALM;
346*4882a593Smuzhiyun 	} else {
347*4882a593Smuzhiyun 		ret &= ~DS1374_REG_CR_WACE;
348*4882a593Smuzhiyun 	}
349*4882a593Smuzhiyun 	ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, ret);
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun out:
352*4882a593Smuzhiyun 	mutex_unlock(&ds1374->mutex);
353*4882a593Smuzhiyun 	return ret;
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun #endif
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun static const struct rtc_class_ops ds1374_rtc_ops = {
358*4882a593Smuzhiyun 	.read_time = ds1374_read_time,
359*4882a593Smuzhiyun 	.set_time = ds1374_set_time,
360*4882a593Smuzhiyun #ifndef CONFIG_RTC_DRV_DS1374_WDT
361*4882a593Smuzhiyun 	.read_alarm = ds1374_read_alarm,
362*4882a593Smuzhiyun 	.set_alarm = ds1374_set_alarm,
363*4882a593Smuzhiyun 	.alarm_irq_enable = ds1374_alarm_irq_enable,
364*4882a593Smuzhiyun #endif
365*4882a593Smuzhiyun };
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun #ifdef CONFIG_RTC_DRV_DS1374_WDT
368*4882a593Smuzhiyun /*
369*4882a593Smuzhiyun  *****************************************************************************
370*4882a593Smuzhiyun  *
371*4882a593Smuzhiyun  * Watchdog Driver
372*4882a593Smuzhiyun  *
373*4882a593Smuzhiyun  *****************************************************************************
374*4882a593Smuzhiyun  */
375*4882a593Smuzhiyun /* Default margin */
376*4882a593Smuzhiyun #define TIMER_MARGIN_DEFAULT	32
377*4882a593Smuzhiyun #define TIMER_MARGIN_MIN	1
378*4882a593Smuzhiyun #define TIMER_MARGIN_MAX	4095 /* 24-bit value */
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun static int wdt_margin;
381*4882a593Smuzhiyun module_param(wdt_margin, int, 0);
382*4882a593Smuzhiyun MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 32s)");
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun static bool nowayout = WATCHDOG_NOWAYOUT;
385*4882a593Smuzhiyun module_param(nowayout, bool, 0);
386*4882a593Smuzhiyun MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default ="
387*4882a593Smuzhiyun 		__MODULE_STRING(WATCHDOG_NOWAYOUT)")");
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun static const struct watchdog_info ds1374_wdt_info = {
390*4882a593Smuzhiyun 	.identity       = "DS1374 Watchdog",
391*4882a593Smuzhiyun 	.options        = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
392*4882a593Smuzhiyun 						WDIOF_MAGICCLOSE,
393*4882a593Smuzhiyun };
394*4882a593Smuzhiyun 
ds1374_wdt_settimeout(struct watchdog_device * wdt,unsigned int timeout)395*4882a593Smuzhiyun static int ds1374_wdt_settimeout(struct watchdog_device *wdt, unsigned int timeout)
396*4882a593Smuzhiyun {
397*4882a593Smuzhiyun 	struct ds1374 *ds1374 = watchdog_get_drvdata(wdt);
398*4882a593Smuzhiyun 	struct i2c_client *client = ds1374->client;
399*4882a593Smuzhiyun 	int ret, cr;
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	wdt->timeout = timeout;
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
404*4882a593Smuzhiyun 	if (cr < 0)
405*4882a593Smuzhiyun 		return cr;
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	/* Disable any existing watchdog/alarm before setting the new one */
408*4882a593Smuzhiyun 	cr &= ~DS1374_REG_CR_WACE;
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
411*4882a593Smuzhiyun 	if (ret < 0)
412*4882a593Smuzhiyun 		return ret;
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	/* Set new watchdog time */
415*4882a593Smuzhiyun 	timeout = timeout * 4096;
416*4882a593Smuzhiyun 	ret = ds1374_write_rtc(client, timeout, DS1374_REG_WDALM0, 3);
417*4882a593Smuzhiyun 	if (ret)
418*4882a593Smuzhiyun 		return ret;
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	/* Enable watchdog timer */
421*4882a593Smuzhiyun 	cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_WDALM;
422*4882a593Smuzhiyun 	cr &= ~DS1374_REG_CR_WDSTR;/* for RST PIN */
423*4882a593Smuzhiyun 	cr &= ~DS1374_REG_CR_AIE;
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
426*4882a593Smuzhiyun 	if (ret < 0)
427*4882a593Smuzhiyun 		return ret;
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	return 0;
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun /*
433*4882a593Smuzhiyun  * Reload the watchdog timer.  (ie, pat the watchdog)
434*4882a593Smuzhiyun  */
ds1374_wdt_start(struct watchdog_device * wdt)435*4882a593Smuzhiyun static int ds1374_wdt_start(struct watchdog_device *wdt)
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun 	struct ds1374 *ds1374 = watchdog_get_drvdata(wdt);
438*4882a593Smuzhiyun 	u32 val;
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	return ds1374_read_rtc(ds1374->client, &val, DS1374_REG_WDALM0, 3);
441*4882a593Smuzhiyun }
442*4882a593Smuzhiyun 
ds1374_wdt_stop(struct watchdog_device * wdt)443*4882a593Smuzhiyun static int ds1374_wdt_stop(struct watchdog_device *wdt)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun 	struct ds1374 *ds1374 = watchdog_get_drvdata(wdt);
446*4882a593Smuzhiyun 	struct i2c_client *client = ds1374->client;
447*4882a593Smuzhiyun 	int cr;
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
450*4882a593Smuzhiyun 	if (cr < 0)
451*4882a593Smuzhiyun 		return cr;
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	/* Disable watchdog timer */
454*4882a593Smuzhiyun 	cr &= ~DS1374_REG_CR_WACE;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	return i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun static const struct watchdog_ops ds1374_wdt_ops = {
460*4882a593Smuzhiyun 	.owner          = THIS_MODULE,
461*4882a593Smuzhiyun 	.start          = ds1374_wdt_start,
462*4882a593Smuzhiyun 	.stop           = ds1374_wdt_stop,
463*4882a593Smuzhiyun 	.set_timeout    = ds1374_wdt_settimeout,
464*4882a593Smuzhiyun };
465*4882a593Smuzhiyun #endif /*CONFIG_RTC_DRV_DS1374_WDT*/
466*4882a593Smuzhiyun /*
467*4882a593Smuzhiyun  *****************************************************************************
468*4882a593Smuzhiyun  *
469*4882a593Smuzhiyun  *	Driver Interface
470*4882a593Smuzhiyun  *
471*4882a593Smuzhiyun  *****************************************************************************
472*4882a593Smuzhiyun  */
ds1374_probe(struct i2c_client * client,const struct i2c_device_id * id)473*4882a593Smuzhiyun static int ds1374_probe(struct i2c_client *client,
474*4882a593Smuzhiyun 			const struct i2c_device_id *id)
475*4882a593Smuzhiyun {
476*4882a593Smuzhiyun 	struct ds1374 *ds1374;
477*4882a593Smuzhiyun 	int ret;
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	ds1374 = devm_kzalloc(&client->dev, sizeof(struct ds1374), GFP_KERNEL);
480*4882a593Smuzhiyun 	if (!ds1374)
481*4882a593Smuzhiyun 		return -ENOMEM;
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	ds1374->rtc = devm_rtc_allocate_device(&client->dev);
484*4882a593Smuzhiyun 	if (IS_ERR(ds1374->rtc))
485*4882a593Smuzhiyun 		return PTR_ERR(ds1374->rtc);
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	ds1374->client = client;
488*4882a593Smuzhiyun 	i2c_set_clientdata(client, ds1374);
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	INIT_WORK(&ds1374->work, ds1374_work);
491*4882a593Smuzhiyun 	mutex_init(&ds1374->mutex);
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	ret = ds1374_check_rtc_status(client);
494*4882a593Smuzhiyun 	if (ret)
495*4882a593Smuzhiyun 		return ret;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	if (client->irq > 0) {
498*4882a593Smuzhiyun 		ret = devm_request_irq(&client->dev, client->irq, ds1374_irq, 0,
499*4882a593Smuzhiyun 					"ds1374", client);
500*4882a593Smuzhiyun 		if (ret) {
501*4882a593Smuzhiyun 			dev_err(&client->dev, "unable to request IRQ\n");
502*4882a593Smuzhiyun 			return ret;
503*4882a593Smuzhiyun 		}
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 		device_set_wakeup_capable(&client->dev, 1);
506*4882a593Smuzhiyun 	}
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	ds1374->rtc->ops = &ds1374_rtc_ops;
509*4882a593Smuzhiyun 	ds1374->rtc->range_max = U32_MAX;
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	ret = rtc_register_device(ds1374->rtc);
512*4882a593Smuzhiyun 	if (ret)
513*4882a593Smuzhiyun 		return ret;
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun #ifdef CONFIG_RTC_DRV_DS1374_WDT
516*4882a593Smuzhiyun 	ds1374->wdt.info = &ds1374_wdt_info;
517*4882a593Smuzhiyun 	ds1374->wdt.ops = &ds1374_wdt_ops;
518*4882a593Smuzhiyun 	ds1374->wdt.timeout = TIMER_MARGIN_DEFAULT;
519*4882a593Smuzhiyun 	ds1374->wdt.min_timeout = TIMER_MARGIN_MIN;
520*4882a593Smuzhiyun 	ds1374->wdt.max_timeout = TIMER_MARGIN_MAX;
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	watchdog_init_timeout(&ds1374->wdt, wdt_margin, &client->dev);
523*4882a593Smuzhiyun 	watchdog_set_nowayout(&ds1374->wdt, nowayout);
524*4882a593Smuzhiyun 	watchdog_stop_on_reboot(&ds1374->wdt);
525*4882a593Smuzhiyun 	watchdog_stop_on_unregister(&ds1374->wdt);
526*4882a593Smuzhiyun 	watchdog_set_drvdata(&ds1374->wdt, ds1374);
527*4882a593Smuzhiyun 	ds1374_wdt_settimeout(&ds1374->wdt, ds1374->wdt.timeout);
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 	ret = devm_watchdog_register_device(&client->dev, &ds1374->wdt);
530*4882a593Smuzhiyun 	if (ret)
531*4882a593Smuzhiyun 		return ret;
532*4882a593Smuzhiyun #endif
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	return 0;
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun 
ds1374_remove(struct i2c_client * client)537*4882a593Smuzhiyun static int ds1374_remove(struct i2c_client *client)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun 	struct ds1374 *ds1374 = i2c_get_clientdata(client);
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	if (client->irq > 0) {
542*4882a593Smuzhiyun 		mutex_lock(&ds1374->mutex);
543*4882a593Smuzhiyun 		ds1374->exiting = 1;
544*4882a593Smuzhiyun 		mutex_unlock(&ds1374->mutex);
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 		devm_free_irq(&client->dev, client->irq, client);
547*4882a593Smuzhiyun 		cancel_work_sync(&ds1374->work);
548*4882a593Smuzhiyun 	}
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	return 0;
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
ds1374_suspend(struct device * dev)554*4882a593Smuzhiyun static int ds1374_suspend(struct device *dev)
555*4882a593Smuzhiyun {
556*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 	if (client->irq > 0 && device_may_wakeup(&client->dev))
559*4882a593Smuzhiyun 		enable_irq_wake(client->irq);
560*4882a593Smuzhiyun 	return 0;
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun 
ds1374_resume(struct device * dev)563*4882a593Smuzhiyun static int ds1374_resume(struct device *dev)
564*4882a593Smuzhiyun {
565*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	if (client->irq > 0 && device_may_wakeup(&client->dev))
568*4882a593Smuzhiyun 		disable_irq_wake(client->irq);
569*4882a593Smuzhiyun 	return 0;
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun #endif
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(ds1374_pm, ds1374_suspend, ds1374_resume);
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun static struct i2c_driver ds1374_driver = {
576*4882a593Smuzhiyun 	.driver = {
577*4882a593Smuzhiyun 		.name = "rtc-ds1374",
578*4882a593Smuzhiyun 		.of_match_table = of_match_ptr(ds1374_of_match),
579*4882a593Smuzhiyun 		.pm = &ds1374_pm,
580*4882a593Smuzhiyun 	},
581*4882a593Smuzhiyun 	.probe = ds1374_probe,
582*4882a593Smuzhiyun 	.remove = ds1374_remove,
583*4882a593Smuzhiyun 	.id_table = ds1374_id,
584*4882a593Smuzhiyun };
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun module_i2c_driver(ds1374_driver);
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun MODULE_AUTHOR("Scott Wood <scottwood@freescale.com>");
589*4882a593Smuzhiyun MODULE_DESCRIPTION("Maxim/Dallas DS1374 RTC Driver");
590*4882a593Smuzhiyun MODULE_LICENSE("GPL");
591