1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * I2C client/driver for the ST M41T80 family of i2c rtc chips.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Author: Alexander Bigga <ab@mycable.de>
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Based on m41t00.c by Mark A. Greer <mgreer@mvista.com>
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * 2006 (c) mycable GmbH
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <linux/bcd.h>
15*4882a593Smuzhiyun #include <linux/clk-provider.h>
16*4882a593Smuzhiyun #include <linux/i2c.h>
17*4882a593Smuzhiyun #include <linux/init.h>
18*4882a593Smuzhiyun #include <linux/kernel.h>
19*4882a593Smuzhiyun #include <linux/module.h>
20*4882a593Smuzhiyun #include <linux/of_device.h>
21*4882a593Smuzhiyun #include <linux/rtc.h>
22*4882a593Smuzhiyun #include <linux/slab.h>
23*4882a593Smuzhiyun #include <linux/mutex.h>
24*4882a593Smuzhiyun #include <linux/string.h>
25*4882a593Smuzhiyun #ifdef CONFIG_RTC_DRV_M41T80_WDT
26*4882a593Smuzhiyun #include <linux/fs.h>
27*4882a593Smuzhiyun #include <linux/ioctl.h>
28*4882a593Smuzhiyun #include <linux/miscdevice.h>
29*4882a593Smuzhiyun #include <linux/reboot.h>
30*4882a593Smuzhiyun #include <linux/watchdog.h>
31*4882a593Smuzhiyun #endif
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #define M41T80_REG_SSEC 0x00
34*4882a593Smuzhiyun #define M41T80_REG_SEC 0x01
35*4882a593Smuzhiyun #define M41T80_REG_MIN 0x02
36*4882a593Smuzhiyun #define M41T80_REG_HOUR 0x03
37*4882a593Smuzhiyun #define M41T80_REG_WDAY 0x04
38*4882a593Smuzhiyun #define M41T80_REG_DAY 0x05
39*4882a593Smuzhiyun #define M41T80_REG_MON 0x06
40*4882a593Smuzhiyun #define M41T80_REG_YEAR 0x07
41*4882a593Smuzhiyun #define M41T80_REG_ALARM_MON 0x0a
42*4882a593Smuzhiyun #define M41T80_REG_ALARM_DAY 0x0b
43*4882a593Smuzhiyun #define M41T80_REG_ALARM_HOUR 0x0c
44*4882a593Smuzhiyun #define M41T80_REG_ALARM_MIN 0x0d
45*4882a593Smuzhiyun #define M41T80_REG_ALARM_SEC 0x0e
46*4882a593Smuzhiyun #define M41T80_REG_FLAGS 0x0f
47*4882a593Smuzhiyun #define M41T80_REG_SQW 0x13
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #define M41T80_DATETIME_REG_SIZE (M41T80_REG_YEAR + 1)
50*4882a593Smuzhiyun #define M41T80_ALARM_REG_SIZE \
51*4882a593Smuzhiyun (M41T80_REG_ALARM_SEC + 1 - M41T80_REG_ALARM_MON)
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun #define M41T80_SQW_MAX_FREQ 32768
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun #define M41T80_SEC_ST BIT(7) /* ST: Stop Bit */
56*4882a593Smuzhiyun #define M41T80_ALMON_AFE BIT(7) /* AFE: AF Enable Bit */
57*4882a593Smuzhiyun #define M41T80_ALMON_SQWE BIT(6) /* SQWE: SQW Enable Bit */
58*4882a593Smuzhiyun #define M41T80_ALHOUR_HT BIT(6) /* HT: Halt Update Bit */
59*4882a593Smuzhiyun #define M41T80_FLAGS_OF BIT(2) /* OF: Oscillator Failure Bit */
60*4882a593Smuzhiyun #define M41T80_FLAGS_AF BIT(6) /* AF: Alarm Flag Bit */
61*4882a593Smuzhiyun #define M41T80_FLAGS_BATT_LOW BIT(4) /* BL: Battery Low Bit */
62*4882a593Smuzhiyun #define M41T80_WATCHDOG_RB2 BIT(7) /* RB: Watchdog resolution */
63*4882a593Smuzhiyun #define M41T80_WATCHDOG_RB1 BIT(1) /* RB: Watchdog resolution */
64*4882a593Smuzhiyun #define M41T80_WATCHDOG_RB0 BIT(0) /* RB: Watchdog resolution */
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun #define M41T80_FEATURE_HT BIT(0) /* Halt feature */
67*4882a593Smuzhiyun #define M41T80_FEATURE_BL BIT(1) /* Battery low indicator */
68*4882a593Smuzhiyun #define M41T80_FEATURE_SQ BIT(2) /* Squarewave feature */
69*4882a593Smuzhiyun #define M41T80_FEATURE_WD BIT(3) /* Extra watchdog resolution */
70*4882a593Smuzhiyun #define M41T80_FEATURE_SQ_ALT BIT(4) /* RSx bits are in reg 4 */
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun static const struct i2c_device_id m41t80_id[] = {
73*4882a593Smuzhiyun { "m41t62", M41T80_FEATURE_SQ | M41T80_FEATURE_SQ_ALT },
74*4882a593Smuzhiyun { "m41t65", M41T80_FEATURE_HT | M41T80_FEATURE_WD },
75*4882a593Smuzhiyun { "m41t80", M41T80_FEATURE_SQ },
76*4882a593Smuzhiyun { "m41t81", M41T80_FEATURE_HT | M41T80_FEATURE_SQ},
77*4882a593Smuzhiyun { "m41t81s", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
78*4882a593Smuzhiyun { "m41t82", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
79*4882a593Smuzhiyun { "m41t83", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
80*4882a593Smuzhiyun { "m41st84", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
81*4882a593Smuzhiyun { "m41st85", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
82*4882a593Smuzhiyun { "m41st87", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
83*4882a593Smuzhiyun { "rv4162", M41T80_FEATURE_SQ | M41T80_FEATURE_WD | M41T80_FEATURE_SQ_ALT },
84*4882a593Smuzhiyun { }
85*4882a593Smuzhiyun };
86*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, m41t80_id);
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun static const struct of_device_id m41t80_of_match[] = {
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun .compatible = "st,m41t62",
91*4882a593Smuzhiyun .data = (void *)(M41T80_FEATURE_SQ | M41T80_FEATURE_SQ_ALT)
92*4882a593Smuzhiyun },
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun .compatible = "st,m41t65",
95*4882a593Smuzhiyun .data = (void *)(M41T80_FEATURE_HT | M41T80_FEATURE_WD)
96*4882a593Smuzhiyun },
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun .compatible = "st,m41t80",
99*4882a593Smuzhiyun .data = (void *)(M41T80_FEATURE_SQ)
100*4882a593Smuzhiyun },
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun .compatible = "st,m41t81",
103*4882a593Smuzhiyun .data = (void *)(M41T80_FEATURE_HT | M41T80_FEATURE_SQ)
104*4882a593Smuzhiyun },
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun .compatible = "st,m41t81s",
107*4882a593Smuzhiyun .data = (void *)(M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ)
108*4882a593Smuzhiyun },
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun .compatible = "st,m41t82",
111*4882a593Smuzhiyun .data = (void *)(M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ)
112*4882a593Smuzhiyun },
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun .compatible = "st,m41t83",
115*4882a593Smuzhiyun .data = (void *)(M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ)
116*4882a593Smuzhiyun },
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun .compatible = "st,m41t84",
119*4882a593Smuzhiyun .data = (void *)(M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ)
120*4882a593Smuzhiyun },
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun .compatible = "st,m41t85",
123*4882a593Smuzhiyun .data = (void *)(M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ)
124*4882a593Smuzhiyun },
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun .compatible = "st,m41t87",
127*4882a593Smuzhiyun .data = (void *)(M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ)
128*4882a593Smuzhiyun },
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun .compatible = "microcrystal,rv4162",
131*4882a593Smuzhiyun .data = (void *)(M41T80_FEATURE_SQ | M41T80_FEATURE_WD | M41T80_FEATURE_SQ_ALT)
132*4882a593Smuzhiyun },
133*4882a593Smuzhiyun /* DT compatibility only, do not use compatibles below: */
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun .compatible = "st,rv4162",
136*4882a593Smuzhiyun .data = (void *)(M41T80_FEATURE_SQ | M41T80_FEATURE_WD | M41T80_FEATURE_SQ_ALT)
137*4882a593Smuzhiyun },
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun .compatible = "rv4162",
140*4882a593Smuzhiyun .data = (void *)(M41T80_FEATURE_SQ | M41T80_FEATURE_WD | M41T80_FEATURE_SQ_ALT)
141*4882a593Smuzhiyun },
142*4882a593Smuzhiyun { }
143*4882a593Smuzhiyun };
144*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, m41t80_of_match);
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun struct m41t80_data {
147*4882a593Smuzhiyun unsigned long features;
148*4882a593Smuzhiyun struct i2c_client *client;
149*4882a593Smuzhiyun struct rtc_device *rtc;
150*4882a593Smuzhiyun #ifdef CONFIG_COMMON_CLK
151*4882a593Smuzhiyun struct clk_hw sqw;
152*4882a593Smuzhiyun unsigned long freq;
153*4882a593Smuzhiyun unsigned int sqwe;
154*4882a593Smuzhiyun #endif
155*4882a593Smuzhiyun };
156*4882a593Smuzhiyun
m41t80_handle_irq(int irq,void * dev_id)157*4882a593Smuzhiyun static irqreturn_t m41t80_handle_irq(int irq, void *dev_id)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun struct i2c_client *client = dev_id;
160*4882a593Smuzhiyun struct m41t80_data *m41t80 = i2c_get_clientdata(client);
161*4882a593Smuzhiyun struct mutex *lock = &m41t80->rtc->ops_lock;
162*4882a593Smuzhiyun unsigned long events = 0;
163*4882a593Smuzhiyun int flags, flags_afe;
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun mutex_lock(lock);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun flags_afe = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
168*4882a593Smuzhiyun if (flags_afe < 0) {
169*4882a593Smuzhiyun mutex_unlock(lock);
170*4882a593Smuzhiyun return IRQ_NONE;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun flags = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
174*4882a593Smuzhiyun if (flags <= 0) {
175*4882a593Smuzhiyun mutex_unlock(lock);
176*4882a593Smuzhiyun return IRQ_NONE;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun if (flags & M41T80_FLAGS_AF) {
180*4882a593Smuzhiyun flags &= ~M41T80_FLAGS_AF;
181*4882a593Smuzhiyun flags_afe &= ~M41T80_ALMON_AFE;
182*4882a593Smuzhiyun events |= RTC_AF;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun if (events) {
186*4882a593Smuzhiyun rtc_update_irq(m41t80->rtc, 1, events);
187*4882a593Smuzhiyun i2c_smbus_write_byte_data(client, M41T80_REG_FLAGS, flags);
188*4882a593Smuzhiyun i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
189*4882a593Smuzhiyun flags_afe);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun mutex_unlock(lock);
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun return IRQ_HANDLED;
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun
m41t80_rtc_read_time(struct device * dev,struct rtc_time * tm)197*4882a593Smuzhiyun static int m41t80_rtc_read_time(struct device *dev, struct rtc_time *tm)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun struct i2c_client *client = to_i2c_client(dev);
200*4882a593Smuzhiyun unsigned char buf[8];
201*4882a593Smuzhiyun int err, flags;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun flags = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
204*4882a593Smuzhiyun if (flags < 0)
205*4882a593Smuzhiyun return flags;
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun if (flags & M41T80_FLAGS_OF) {
208*4882a593Smuzhiyun dev_err(&client->dev, "Oscillator failure, data is invalid.\n");
209*4882a593Smuzhiyun return -EINVAL;
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun err = i2c_smbus_read_i2c_block_data(client, M41T80_REG_SSEC,
213*4882a593Smuzhiyun sizeof(buf), buf);
214*4882a593Smuzhiyun if (err < 0) {
215*4882a593Smuzhiyun dev_err(&client->dev, "Unable to read date\n");
216*4882a593Smuzhiyun return err;
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun tm->tm_sec = bcd2bin(buf[M41T80_REG_SEC] & 0x7f);
220*4882a593Smuzhiyun tm->tm_min = bcd2bin(buf[M41T80_REG_MIN] & 0x7f);
221*4882a593Smuzhiyun tm->tm_hour = bcd2bin(buf[M41T80_REG_HOUR] & 0x3f);
222*4882a593Smuzhiyun tm->tm_mday = bcd2bin(buf[M41T80_REG_DAY] & 0x3f);
223*4882a593Smuzhiyun tm->tm_wday = buf[M41T80_REG_WDAY] & 0x07;
224*4882a593Smuzhiyun tm->tm_mon = bcd2bin(buf[M41T80_REG_MON] & 0x1f) - 1;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun /* assume 20YY not 19YY, and ignore the Century Bit */
227*4882a593Smuzhiyun tm->tm_year = bcd2bin(buf[M41T80_REG_YEAR]) + 100;
228*4882a593Smuzhiyun return 0;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
m41t80_rtc_set_time(struct device * dev,struct rtc_time * tm)231*4882a593Smuzhiyun static int m41t80_rtc_set_time(struct device *dev, struct rtc_time *tm)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun struct i2c_client *client = to_i2c_client(dev);
234*4882a593Smuzhiyun struct m41t80_data *clientdata = i2c_get_clientdata(client);
235*4882a593Smuzhiyun unsigned char buf[8];
236*4882a593Smuzhiyun int err, flags;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun buf[M41T80_REG_SSEC] = 0;
239*4882a593Smuzhiyun buf[M41T80_REG_SEC] = bin2bcd(tm->tm_sec);
240*4882a593Smuzhiyun buf[M41T80_REG_MIN] = bin2bcd(tm->tm_min);
241*4882a593Smuzhiyun buf[M41T80_REG_HOUR] = bin2bcd(tm->tm_hour);
242*4882a593Smuzhiyun buf[M41T80_REG_DAY] = bin2bcd(tm->tm_mday);
243*4882a593Smuzhiyun buf[M41T80_REG_MON] = bin2bcd(tm->tm_mon + 1);
244*4882a593Smuzhiyun buf[M41T80_REG_YEAR] = bin2bcd(tm->tm_year - 100);
245*4882a593Smuzhiyun buf[M41T80_REG_WDAY] = tm->tm_wday;
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun /* If the square wave output is controlled in the weekday register */
248*4882a593Smuzhiyun if (clientdata->features & M41T80_FEATURE_SQ_ALT) {
249*4882a593Smuzhiyun int val;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun val = i2c_smbus_read_byte_data(client, M41T80_REG_WDAY);
252*4882a593Smuzhiyun if (val < 0)
253*4882a593Smuzhiyun return val;
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun buf[M41T80_REG_WDAY] |= (val & 0xf0);
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun err = i2c_smbus_write_i2c_block_data(client, M41T80_REG_SSEC,
259*4882a593Smuzhiyun sizeof(buf), buf);
260*4882a593Smuzhiyun if (err < 0) {
261*4882a593Smuzhiyun dev_err(&client->dev, "Unable to write to date registers\n");
262*4882a593Smuzhiyun return err;
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun /* Clear the OF bit of Flags Register */
266*4882a593Smuzhiyun flags = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
267*4882a593Smuzhiyun if (flags < 0)
268*4882a593Smuzhiyun return flags;
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun err = i2c_smbus_write_byte_data(client, M41T80_REG_FLAGS,
271*4882a593Smuzhiyun flags & ~M41T80_FLAGS_OF);
272*4882a593Smuzhiyun if (err < 0) {
273*4882a593Smuzhiyun dev_err(&client->dev, "Unable to write flags register\n");
274*4882a593Smuzhiyun return err;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun return err;
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun
m41t80_rtc_proc(struct device * dev,struct seq_file * seq)280*4882a593Smuzhiyun static int m41t80_rtc_proc(struct device *dev, struct seq_file *seq)
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun struct i2c_client *client = to_i2c_client(dev);
283*4882a593Smuzhiyun struct m41t80_data *clientdata = i2c_get_clientdata(client);
284*4882a593Smuzhiyun int reg;
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun if (clientdata->features & M41T80_FEATURE_BL) {
287*4882a593Smuzhiyun reg = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
288*4882a593Smuzhiyun if (reg < 0)
289*4882a593Smuzhiyun return reg;
290*4882a593Smuzhiyun seq_printf(seq, "battery\t\t: %s\n",
291*4882a593Smuzhiyun (reg & M41T80_FLAGS_BATT_LOW) ? "exhausted" : "ok");
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun return 0;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun
m41t80_alarm_irq_enable(struct device * dev,unsigned int enabled)296*4882a593Smuzhiyun static int m41t80_alarm_irq_enable(struct device *dev, unsigned int enabled)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun struct i2c_client *client = to_i2c_client(dev);
299*4882a593Smuzhiyun int flags, retval;
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun flags = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
302*4882a593Smuzhiyun if (flags < 0)
303*4882a593Smuzhiyun return flags;
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun if (enabled)
306*4882a593Smuzhiyun flags |= M41T80_ALMON_AFE;
307*4882a593Smuzhiyun else
308*4882a593Smuzhiyun flags &= ~M41T80_ALMON_AFE;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun retval = i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON, flags);
311*4882a593Smuzhiyun if (retval < 0) {
312*4882a593Smuzhiyun dev_err(dev, "Unable to enable alarm IRQ %d\n", retval);
313*4882a593Smuzhiyun return retval;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun return 0;
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun
m41t80_set_alarm(struct device * dev,struct rtc_wkalrm * alrm)318*4882a593Smuzhiyun static int m41t80_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
319*4882a593Smuzhiyun {
320*4882a593Smuzhiyun struct i2c_client *client = to_i2c_client(dev);
321*4882a593Smuzhiyun u8 alarmvals[5];
322*4882a593Smuzhiyun int ret, err;
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun alarmvals[0] = bin2bcd(alrm->time.tm_mon + 1);
325*4882a593Smuzhiyun alarmvals[1] = bin2bcd(alrm->time.tm_mday);
326*4882a593Smuzhiyun alarmvals[2] = bin2bcd(alrm->time.tm_hour);
327*4882a593Smuzhiyun alarmvals[3] = bin2bcd(alrm->time.tm_min);
328*4882a593Smuzhiyun alarmvals[4] = bin2bcd(alrm->time.tm_sec);
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun /* Clear AF and AFE flags */
331*4882a593Smuzhiyun ret = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
332*4882a593Smuzhiyun if (ret < 0)
333*4882a593Smuzhiyun return ret;
334*4882a593Smuzhiyun err = i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
335*4882a593Smuzhiyun ret & ~(M41T80_ALMON_AFE));
336*4882a593Smuzhiyun if (err < 0) {
337*4882a593Smuzhiyun dev_err(dev, "Unable to clear AFE bit\n");
338*4882a593Smuzhiyun return err;
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun /* Keep SQWE bit value */
342*4882a593Smuzhiyun alarmvals[0] |= (ret & M41T80_ALMON_SQWE);
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun ret = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
345*4882a593Smuzhiyun if (ret < 0)
346*4882a593Smuzhiyun return ret;
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun err = i2c_smbus_write_byte_data(client, M41T80_REG_FLAGS,
349*4882a593Smuzhiyun ret & ~(M41T80_FLAGS_AF));
350*4882a593Smuzhiyun if (err < 0) {
351*4882a593Smuzhiyun dev_err(dev, "Unable to clear AF bit\n");
352*4882a593Smuzhiyun return err;
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun /* Write the alarm */
356*4882a593Smuzhiyun err = i2c_smbus_write_i2c_block_data(client, M41T80_REG_ALARM_MON,
357*4882a593Smuzhiyun 5, alarmvals);
358*4882a593Smuzhiyun if (err)
359*4882a593Smuzhiyun return err;
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun /* Enable the alarm interrupt */
362*4882a593Smuzhiyun if (alrm->enabled) {
363*4882a593Smuzhiyun alarmvals[0] |= M41T80_ALMON_AFE;
364*4882a593Smuzhiyun err = i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
365*4882a593Smuzhiyun alarmvals[0]);
366*4882a593Smuzhiyun if (err)
367*4882a593Smuzhiyun return err;
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun return 0;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun
m41t80_read_alarm(struct device * dev,struct rtc_wkalrm * alrm)373*4882a593Smuzhiyun static int m41t80_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun struct i2c_client *client = to_i2c_client(dev);
376*4882a593Smuzhiyun u8 alarmvals[5];
377*4882a593Smuzhiyun int flags, ret;
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun ret = i2c_smbus_read_i2c_block_data(client, M41T80_REG_ALARM_MON,
380*4882a593Smuzhiyun 5, alarmvals);
381*4882a593Smuzhiyun if (ret != 5)
382*4882a593Smuzhiyun return ret < 0 ? ret : -EIO;
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun flags = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
385*4882a593Smuzhiyun if (flags < 0)
386*4882a593Smuzhiyun return flags;
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun alrm->time.tm_sec = bcd2bin(alarmvals[4] & 0x7f);
389*4882a593Smuzhiyun alrm->time.tm_min = bcd2bin(alarmvals[3] & 0x7f);
390*4882a593Smuzhiyun alrm->time.tm_hour = bcd2bin(alarmvals[2] & 0x3f);
391*4882a593Smuzhiyun alrm->time.tm_mday = bcd2bin(alarmvals[1] & 0x3f);
392*4882a593Smuzhiyun alrm->time.tm_mon = bcd2bin(alarmvals[0] & 0x3f) - 1;
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun alrm->enabled = !!(alarmvals[0] & M41T80_ALMON_AFE);
395*4882a593Smuzhiyun alrm->pending = (flags & M41T80_FLAGS_AF) && alrm->enabled;
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun return 0;
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun static struct rtc_class_ops m41t80_rtc_ops = {
401*4882a593Smuzhiyun .read_time = m41t80_rtc_read_time,
402*4882a593Smuzhiyun .set_time = m41t80_rtc_set_time,
403*4882a593Smuzhiyun .proc = m41t80_rtc_proc,
404*4882a593Smuzhiyun };
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
m41t80_suspend(struct device * dev)407*4882a593Smuzhiyun static int m41t80_suspend(struct device *dev)
408*4882a593Smuzhiyun {
409*4882a593Smuzhiyun struct i2c_client *client = to_i2c_client(dev);
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun if (client->irq >= 0 && device_may_wakeup(dev))
412*4882a593Smuzhiyun enable_irq_wake(client->irq);
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun return 0;
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun
m41t80_resume(struct device * dev)417*4882a593Smuzhiyun static int m41t80_resume(struct device *dev)
418*4882a593Smuzhiyun {
419*4882a593Smuzhiyun struct i2c_client *client = to_i2c_client(dev);
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun if (client->irq >= 0 && device_may_wakeup(dev))
422*4882a593Smuzhiyun disable_irq_wake(client->irq);
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun return 0;
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun #endif
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(m41t80_pm, m41t80_suspend, m41t80_resume);
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun #ifdef CONFIG_COMMON_CLK
431*4882a593Smuzhiyun #define sqw_to_m41t80_data(_hw) container_of(_hw, struct m41t80_data, sqw)
432*4882a593Smuzhiyun
m41t80_decode_freq(int setting)433*4882a593Smuzhiyun static unsigned long m41t80_decode_freq(int setting)
434*4882a593Smuzhiyun {
435*4882a593Smuzhiyun return (setting == 0) ? 0 : (setting == 1) ? M41T80_SQW_MAX_FREQ :
436*4882a593Smuzhiyun M41T80_SQW_MAX_FREQ >> setting;
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun
m41t80_get_freq(struct m41t80_data * m41t80)439*4882a593Smuzhiyun static unsigned long m41t80_get_freq(struct m41t80_data *m41t80)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun struct i2c_client *client = m41t80->client;
442*4882a593Smuzhiyun int reg_sqw = (m41t80->features & M41T80_FEATURE_SQ_ALT) ?
443*4882a593Smuzhiyun M41T80_REG_WDAY : M41T80_REG_SQW;
444*4882a593Smuzhiyun int ret = i2c_smbus_read_byte_data(client, reg_sqw);
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun if (ret < 0)
447*4882a593Smuzhiyun return 0;
448*4882a593Smuzhiyun return m41t80_decode_freq(ret >> 4);
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun
m41t80_sqw_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)451*4882a593Smuzhiyun static unsigned long m41t80_sqw_recalc_rate(struct clk_hw *hw,
452*4882a593Smuzhiyun unsigned long parent_rate)
453*4882a593Smuzhiyun {
454*4882a593Smuzhiyun return sqw_to_m41t80_data(hw)->freq;
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun
m41t80_sqw_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * prate)457*4882a593Smuzhiyun static long m41t80_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
458*4882a593Smuzhiyun unsigned long *prate)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun if (rate >= M41T80_SQW_MAX_FREQ)
461*4882a593Smuzhiyun return M41T80_SQW_MAX_FREQ;
462*4882a593Smuzhiyun if (rate >= M41T80_SQW_MAX_FREQ / 4)
463*4882a593Smuzhiyun return M41T80_SQW_MAX_FREQ / 4;
464*4882a593Smuzhiyun if (!rate)
465*4882a593Smuzhiyun return 0;
466*4882a593Smuzhiyun return 1 << ilog2(rate);
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun
m41t80_sqw_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)469*4882a593Smuzhiyun static int m41t80_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
470*4882a593Smuzhiyun unsigned long parent_rate)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun struct m41t80_data *m41t80 = sqw_to_m41t80_data(hw);
473*4882a593Smuzhiyun struct i2c_client *client = m41t80->client;
474*4882a593Smuzhiyun int reg_sqw = (m41t80->features & M41T80_FEATURE_SQ_ALT) ?
475*4882a593Smuzhiyun M41T80_REG_WDAY : M41T80_REG_SQW;
476*4882a593Smuzhiyun int reg, ret, val = 0;
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun if (rate >= M41T80_SQW_MAX_FREQ)
479*4882a593Smuzhiyun val = 1;
480*4882a593Smuzhiyun else if (rate >= M41T80_SQW_MAX_FREQ / 4)
481*4882a593Smuzhiyun val = 2;
482*4882a593Smuzhiyun else if (rate)
483*4882a593Smuzhiyun val = 15 - ilog2(rate);
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun reg = i2c_smbus_read_byte_data(client, reg_sqw);
486*4882a593Smuzhiyun if (reg < 0)
487*4882a593Smuzhiyun return reg;
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun reg = (reg & 0x0f) | (val << 4);
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun ret = i2c_smbus_write_byte_data(client, reg_sqw, reg);
492*4882a593Smuzhiyun if (!ret)
493*4882a593Smuzhiyun m41t80->freq = m41t80_decode_freq(val);
494*4882a593Smuzhiyun return ret;
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun
m41t80_sqw_control(struct clk_hw * hw,bool enable)497*4882a593Smuzhiyun static int m41t80_sqw_control(struct clk_hw *hw, bool enable)
498*4882a593Smuzhiyun {
499*4882a593Smuzhiyun struct m41t80_data *m41t80 = sqw_to_m41t80_data(hw);
500*4882a593Smuzhiyun struct i2c_client *client = m41t80->client;
501*4882a593Smuzhiyun int ret = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun if (ret < 0)
504*4882a593Smuzhiyun return ret;
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun if (enable)
507*4882a593Smuzhiyun ret |= M41T80_ALMON_SQWE;
508*4882a593Smuzhiyun else
509*4882a593Smuzhiyun ret &= ~M41T80_ALMON_SQWE;
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun ret = i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON, ret);
512*4882a593Smuzhiyun if (!ret)
513*4882a593Smuzhiyun m41t80->sqwe = enable;
514*4882a593Smuzhiyun return ret;
515*4882a593Smuzhiyun }
516*4882a593Smuzhiyun
m41t80_sqw_prepare(struct clk_hw * hw)517*4882a593Smuzhiyun static int m41t80_sqw_prepare(struct clk_hw *hw)
518*4882a593Smuzhiyun {
519*4882a593Smuzhiyun return m41t80_sqw_control(hw, 1);
520*4882a593Smuzhiyun }
521*4882a593Smuzhiyun
m41t80_sqw_unprepare(struct clk_hw * hw)522*4882a593Smuzhiyun static void m41t80_sqw_unprepare(struct clk_hw *hw)
523*4882a593Smuzhiyun {
524*4882a593Smuzhiyun m41t80_sqw_control(hw, 0);
525*4882a593Smuzhiyun }
526*4882a593Smuzhiyun
m41t80_sqw_is_prepared(struct clk_hw * hw)527*4882a593Smuzhiyun static int m41t80_sqw_is_prepared(struct clk_hw *hw)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun return sqw_to_m41t80_data(hw)->sqwe;
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun static const struct clk_ops m41t80_sqw_ops = {
533*4882a593Smuzhiyun .prepare = m41t80_sqw_prepare,
534*4882a593Smuzhiyun .unprepare = m41t80_sqw_unprepare,
535*4882a593Smuzhiyun .is_prepared = m41t80_sqw_is_prepared,
536*4882a593Smuzhiyun .recalc_rate = m41t80_sqw_recalc_rate,
537*4882a593Smuzhiyun .round_rate = m41t80_sqw_round_rate,
538*4882a593Smuzhiyun .set_rate = m41t80_sqw_set_rate,
539*4882a593Smuzhiyun };
540*4882a593Smuzhiyun
m41t80_sqw_register_clk(struct m41t80_data * m41t80)541*4882a593Smuzhiyun static struct clk *m41t80_sqw_register_clk(struct m41t80_data *m41t80)
542*4882a593Smuzhiyun {
543*4882a593Smuzhiyun struct i2c_client *client = m41t80->client;
544*4882a593Smuzhiyun struct device_node *node = client->dev.of_node;
545*4882a593Smuzhiyun struct clk *clk;
546*4882a593Smuzhiyun struct clk_init_data init;
547*4882a593Smuzhiyun int ret;
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun /* First disable the clock */
550*4882a593Smuzhiyun ret = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
551*4882a593Smuzhiyun if (ret < 0)
552*4882a593Smuzhiyun return ERR_PTR(ret);
553*4882a593Smuzhiyun ret = i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
554*4882a593Smuzhiyun ret & ~(M41T80_ALMON_SQWE));
555*4882a593Smuzhiyun if (ret < 0)
556*4882a593Smuzhiyun return ERR_PTR(ret);
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun init.name = "m41t80-sqw";
559*4882a593Smuzhiyun init.ops = &m41t80_sqw_ops;
560*4882a593Smuzhiyun init.flags = 0;
561*4882a593Smuzhiyun init.parent_names = NULL;
562*4882a593Smuzhiyun init.num_parents = 0;
563*4882a593Smuzhiyun m41t80->sqw.init = &init;
564*4882a593Smuzhiyun m41t80->freq = m41t80_get_freq(m41t80);
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun /* optional override of the clockname */
567*4882a593Smuzhiyun of_property_read_string(node, "clock-output-names", &init.name);
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun /* register the clock */
570*4882a593Smuzhiyun clk = clk_register(&client->dev, &m41t80->sqw);
571*4882a593Smuzhiyun if (!IS_ERR(clk))
572*4882a593Smuzhiyun of_clk_add_provider(node, of_clk_src_simple_get, clk);
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun return clk;
575*4882a593Smuzhiyun }
576*4882a593Smuzhiyun #endif
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun #ifdef CONFIG_RTC_DRV_M41T80_WDT
579*4882a593Smuzhiyun /*
580*4882a593Smuzhiyun *****************************************************************************
581*4882a593Smuzhiyun *
582*4882a593Smuzhiyun * Watchdog Driver
583*4882a593Smuzhiyun *
584*4882a593Smuzhiyun *****************************************************************************
585*4882a593Smuzhiyun */
586*4882a593Smuzhiyun static DEFINE_MUTEX(m41t80_rtc_mutex);
587*4882a593Smuzhiyun static struct i2c_client *save_client;
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun /* Default margin */
590*4882a593Smuzhiyun #define WD_TIMO 60 /* 1..31 seconds */
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun static int wdt_margin = WD_TIMO;
593*4882a593Smuzhiyun module_param(wdt_margin, int, 0);
594*4882a593Smuzhiyun MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 60s)");
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun static unsigned long wdt_is_open;
597*4882a593Smuzhiyun static int boot_flag;
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun /**
600*4882a593Smuzhiyun * wdt_ping:
601*4882a593Smuzhiyun *
602*4882a593Smuzhiyun * Reload counter one with the watchdog timeout. We don't bother reloading
603*4882a593Smuzhiyun * the cascade counter.
604*4882a593Smuzhiyun */
wdt_ping(void)605*4882a593Smuzhiyun static void wdt_ping(void)
606*4882a593Smuzhiyun {
607*4882a593Smuzhiyun unsigned char i2c_data[2];
608*4882a593Smuzhiyun struct i2c_msg msgs1[1] = {
609*4882a593Smuzhiyun {
610*4882a593Smuzhiyun .addr = save_client->addr,
611*4882a593Smuzhiyun .flags = 0,
612*4882a593Smuzhiyun .len = 2,
613*4882a593Smuzhiyun .buf = i2c_data,
614*4882a593Smuzhiyun },
615*4882a593Smuzhiyun };
616*4882a593Smuzhiyun struct m41t80_data *clientdata = i2c_get_clientdata(save_client);
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun i2c_data[0] = 0x09; /* watchdog register */
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun if (wdt_margin > 31)
621*4882a593Smuzhiyun i2c_data[1] = (wdt_margin & 0xFC) | 0x83; /* resolution = 4s */
622*4882a593Smuzhiyun else
623*4882a593Smuzhiyun /*
624*4882a593Smuzhiyun * WDS = 1 (0x80), mulitplier = WD_TIMO, resolution = 1s (0x02)
625*4882a593Smuzhiyun */
626*4882a593Smuzhiyun i2c_data[1] = wdt_margin << 2 | 0x82;
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun /*
629*4882a593Smuzhiyun * M41T65 has three bits for watchdog resolution. Don't set bit 7, as
630*4882a593Smuzhiyun * that would be an invalid resolution.
631*4882a593Smuzhiyun */
632*4882a593Smuzhiyun if (clientdata->features & M41T80_FEATURE_WD)
633*4882a593Smuzhiyun i2c_data[1] &= ~M41T80_WATCHDOG_RB2;
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun i2c_transfer(save_client->adapter, msgs1, 1);
636*4882a593Smuzhiyun }
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun /**
639*4882a593Smuzhiyun * wdt_disable:
640*4882a593Smuzhiyun *
641*4882a593Smuzhiyun * disables watchdog.
642*4882a593Smuzhiyun */
wdt_disable(void)643*4882a593Smuzhiyun static void wdt_disable(void)
644*4882a593Smuzhiyun {
645*4882a593Smuzhiyun unsigned char i2c_data[2], i2c_buf[0x10];
646*4882a593Smuzhiyun struct i2c_msg msgs0[2] = {
647*4882a593Smuzhiyun {
648*4882a593Smuzhiyun .addr = save_client->addr,
649*4882a593Smuzhiyun .flags = 0,
650*4882a593Smuzhiyun .len = 1,
651*4882a593Smuzhiyun .buf = i2c_data,
652*4882a593Smuzhiyun },
653*4882a593Smuzhiyun {
654*4882a593Smuzhiyun .addr = save_client->addr,
655*4882a593Smuzhiyun .flags = I2C_M_RD,
656*4882a593Smuzhiyun .len = 1,
657*4882a593Smuzhiyun .buf = i2c_buf,
658*4882a593Smuzhiyun },
659*4882a593Smuzhiyun };
660*4882a593Smuzhiyun struct i2c_msg msgs1[1] = {
661*4882a593Smuzhiyun {
662*4882a593Smuzhiyun .addr = save_client->addr,
663*4882a593Smuzhiyun .flags = 0,
664*4882a593Smuzhiyun .len = 2,
665*4882a593Smuzhiyun .buf = i2c_data,
666*4882a593Smuzhiyun },
667*4882a593Smuzhiyun };
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun i2c_data[0] = 0x09;
670*4882a593Smuzhiyun i2c_transfer(save_client->adapter, msgs0, 2);
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun i2c_data[0] = 0x09;
673*4882a593Smuzhiyun i2c_data[1] = 0x00;
674*4882a593Smuzhiyun i2c_transfer(save_client->adapter, msgs1, 1);
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun /**
678*4882a593Smuzhiyun * wdt_write:
679*4882a593Smuzhiyun * @file: file handle to the watchdog
680*4882a593Smuzhiyun * @buf: buffer to write (unused as data does not matter here
681*4882a593Smuzhiyun * @count: count of bytes
682*4882a593Smuzhiyun * @ppos: pointer to the position to write. No seeks allowed
683*4882a593Smuzhiyun *
684*4882a593Smuzhiyun * A write to a watchdog device is defined as a keepalive signal. Any
685*4882a593Smuzhiyun * write of data will do, as we we don't define content meaning.
686*4882a593Smuzhiyun */
wdt_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)687*4882a593Smuzhiyun static ssize_t wdt_write(struct file *file, const char __user *buf,
688*4882a593Smuzhiyun size_t count, loff_t *ppos)
689*4882a593Smuzhiyun {
690*4882a593Smuzhiyun if (count) {
691*4882a593Smuzhiyun wdt_ping();
692*4882a593Smuzhiyun return 1;
693*4882a593Smuzhiyun }
694*4882a593Smuzhiyun return 0;
695*4882a593Smuzhiyun }
696*4882a593Smuzhiyun
wdt_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)697*4882a593Smuzhiyun static ssize_t wdt_read(struct file *file, char __user *buf,
698*4882a593Smuzhiyun size_t count, loff_t *ppos)
699*4882a593Smuzhiyun {
700*4882a593Smuzhiyun return 0;
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun /**
704*4882a593Smuzhiyun * wdt_ioctl:
705*4882a593Smuzhiyun * @file: file handle to the device
706*4882a593Smuzhiyun * @cmd: watchdog command
707*4882a593Smuzhiyun * @arg: argument pointer
708*4882a593Smuzhiyun *
709*4882a593Smuzhiyun * The watchdog API defines a common set of functions for all watchdogs
710*4882a593Smuzhiyun * according to their available features. We only actually usefully support
711*4882a593Smuzhiyun * querying capabilities and current status.
712*4882a593Smuzhiyun */
wdt_ioctl(struct file * file,unsigned int cmd,unsigned long arg)713*4882a593Smuzhiyun static int wdt_ioctl(struct file *file, unsigned int cmd,
714*4882a593Smuzhiyun unsigned long arg)
715*4882a593Smuzhiyun {
716*4882a593Smuzhiyun int new_margin, rv;
717*4882a593Smuzhiyun static struct watchdog_info ident = {
718*4882a593Smuzhiyun .options = WDIOF_POWERUNDER | WDIOF_KEEPALIVEPING |
719*4882a593Smuzhiyun WDIOF_SETTIMEOUT,
720*4882a593Smuzhiyun .firmware_version = 1,
721*4882a593Smuzhiyun .identity = "M41T80 WTD"
722*4882a593Smuzhiyun };
723*4882a593Smuzhiyun
724*4882a593Smuzhiyun switch (cmd) {
725*4882a593Smuzhiyun case WDIOC_GETSUPPORT:
726*4882a593Smuzhiyun return copy_to_user((struct watchdog_info __user *)arg, &ident,
727*4882a593Smuzhiyun sizeof(ident)) ? -EFAULT : 0;
728*4882a593Smuzhiyun
729*4882a593Smuzhiyun case WDIOC_GETSTATUS:
730*4882a593Smuzhiyun case WDIOC_GETBOOTSTATUS:
731*4882a593Smuzhiyun return put_user(boot_flag, (int __user *)arg);
732*4882a593Smuzhiyun case WDIOC_KEEPALIVE:
733*4882a593Smuzhiyun wdt_ping();
734*4882a593Smuzhiyun return 0;
735*4882a593Smuzhiyun case WDIOC_SETTIMEOUT:
736*4882a593Smuzhiyun if (get_user(new_margin, (int __user *)arg))
737*4882a593Smuzhiyun return -EFAULT;
738*4882a593Smuzhiyun /* Arbitrary, can't find the card's limits */
739*4882a593Smuzhiyun if (new_margin < 1 || new_margin > 124)
740*4882a593Smuzhiyun return -EINVAL;
741*4882a593Smuzhiyun wdt_margin = new_margin;
742*4882a593Smuzhiyun wdt_ping();
743*4882a593Smuzhiyun fallthrough;
744*4882a593Smuzhiyun case WDIOC_GETTIMEOUT:
745*4882a593Smuzhiyun return put_user(wdt_margin, (int __user *)arg);
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun case WDIOC_SETOPTIONS:
748*4882a593Smuzhiyun if (copy_from_user(&rv, (int __user *)arg, sizeof(int)))
749*4882a593Smuzhiyun return -EFAULT;
750*4882a593Smuzhiyun
751*4882a593Smuzhiyun if (rv & WDIOS_DISABLECARD) {
752*4882a593Smuzhiyun pr_info("disable watchdog\n");
753*4882a593Smuzhiyun wdt_disable();
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun
756*4882a593Smuzhiyun if (rv & WDIOS_ENABLECARD) {
757*4882a593Smuzhiyun pr_info("enable watchdog\n");
758*4882a593Smuzhiyun wdt_ping();
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun return -EINVAL;
762*4882a593Smuzhiyun }
763*4882a593Smuzhiyun return -ENOTTY;
764*4882a593Smuzhiyun }
765*4882a593Smuzhiyun
wdt_unlocked_ioctl(struct file * file,unsigned int cmd,unsigned long arg)766*4882a593Smuzhiyun static long wdt_unlocked_ioctl(struct file *file, unsigned int cmd,
767*4882a593Smuzhiyun unsigned long arg)
768*4882a593Smuzhiyun {
769*4882a593Smuzhiyun int ret;
770*4882a593Smuzhiyun
771*4882a593Smuzhiyun mutex_lock(&m41t80_rtc_mutex);
772*4882a593Smuzhiyun ret = wdt_ioctl(file, cmd, arg);
773*4882a593Smuzhiyun mutex_unlock(&m41t80_rtc_mutex);
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun return ret;
776*4882a593Smuzhiyun }
777*4882a593Smuzhiyun
778*4882a593Smuzhiyun /**
779*4882a593Smuzhiyun * wdt_open:
780*4882a593Smuzhiyun * @inode: inode of device
781*4882a593Smuzhiyun * @file: file handle to device
782*4882a593Smuzhiyun *
783*4882a593Smuzhiyun */
wdt_open(struct inode * inode,struct file * file)784*4882a593Smuzhiyun static int wdt_open(struct inode *inode, struct file *file)
785*4882a593Smuzhiyun {
786*4882a593Smuzhiyun if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) {
787*4882a593Smuzhiyun mutex_lock(&m41t80_rtc_mutex);
788*4882a593Smuzhiyun if (test_and_set_bit(0, &wdt_is_open)) {
789*4882a593Smuzhiyun mutex_unlock(&m41t80_rtc_mutex);
790*4882a593Smuzhiyun return -EBUSY;
791*4882a593Smuzhiyun }
792*4882a593Smuzhiyun /*
793*4882a593Smuzhiyun * Activate
794*4882a593Smuzhiyun */
795*4882a593Smuzhiyun wdt_is_open = 1;
796*4882a593Smuzhiyun mutex_unlock(&m41t80_rtc_mutex);
797*4882a593Smuzhiyun return stream_open(inode, file);
798*4882a593Smuzhiyun }
799*4882a593Smuzhiyun return -ENODEV;
800*4882a593Smuzhiyun }
801*4882a593Smuzhiyun
802*4882a593Smuzhiyun /**
803*4882a593Smuzhiyun * wdt_close:
804*4882a593Smuzhiyun * @inode: inode to board
805*4882a593Smuzhiyun * @file: file handle to board
806*4882a593Smuzhiyun *
807*4882a593Smuzhiyun */
wdt_release(struct inode * inode,struct file * file)808*4882a593Smuzhiyun static int wdt_release(struct inode *inode, struct file *file)
809*4882a593Smuzhiyun {
810*4882a593Smuzhiyun if (MINOR(inode->i_rdev) == WATCHDOG_MINOR)
811*4882a593Smuzhiyun clear_bit(0, &wdt_is_open);
812*4882a593Smuzhiyun return 0;
813*4882a593Smuzhiyun }
814*4882a593Smuzhiyun
815*4882a593Smuzhiyun /**
816*4882a593Smuzhiyun * notify_sys:
817*4882a593Smuzhiyun * @this: our notifier block
818*4882a593Smuzhiyun * @code: the event being reported
819*4882a593Smuzhiyun * @unused: unused
820*4882a593Smuzhiyun *
821*4882a593Smuzhiyun * Our notifier is called on system shutdowns. We want to turn the card
822*4882a593Smuzhiyun * off at reboot otherwise the machine will reboot again during memory
823*4882a593Smuzhiyun * test or worse yet during the following fsck. This would suck, in fact
824*4882a593Smuzhiyun * trust me - if it happens it does suck.
825*4882a593Smuzhiyun */
wdt_notify_sys(struct notifier_block * this,unsigned long code,void * unused)826*4882a593Smuzhiyun static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
827*4882a593Smuzhiyun void *unused)
828*4882a593Smuzhiyun {
829*4882a593Smuzhiyun if (code == SYS_DOWN || code == SYS_HALT)
830*4882a593Smuzhiyun /* Disable Watchdog */
831*4882a593Smuzhiyun wdt_disable();
832*4882a593Smuzhiyun return NOTIFY_DONE;
833*4882a593Smuzhiyun }
834*4882a593Smuzhiyun
835*4882a593Smuzhiyun static const struct file_operations wdt_fops = {
836*4882a593Smuzhiyun .owner = THIS_MODULE,
837*4882a593Smuzhiyun .read = wdt_read,
838*4882a593Smuzhiyun .unlocked_ioctl = wdt_unlocked_ioctl,
839*4882a593Smuzhiyun .compat_ioctl = compat_ptr_ioctl,
840*4882a593Smuzhiyun .write = wdt_write,
841*4882a593Smuzhiyun .open = wdt_open,
842*4882a593Smuzhiyun .release = wdt_release,
843*4882a593Smuzhiyun .llseek = no_llseek,
844*4882a593Smuzhiyun };
845*4882a593Smuzhiyun
846*4882a593Smuzhiyun static struct miscdevice wdt_dev = {
847*4882a593Smuzhiyun .minor = WATCHDOG_MINOR,
848*4882a593Smuzhiyun .name = "watchdog",
849*4882a593Smuzhiyun .fops = &wdt_fops,
850*4882a593Smuzhiyun };
851*4882a593Smuzhiyun
852*4882a593Smuzhiyun /*
853*4882a593Smuzhiyun * The WDT card needs to learn about soft shutdowns in order to
854*4882a593Smuzhiyun * turn the timebomb registers off.
855*4882a593Smuzhiyun */
856*4882a593Smuzhiyun static struct notifier_block wdt_notifier = {
857*4882a593Smuzhiyun .notifier_call = wdt_notify_sys,
858*4882a593Smuzhiyun };
859*4882a593Smuzhiyun #endif /* CONFIG_RTC_DRV_M41T80_WDT */
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun /*
862*4882a593Smuzhiyun *****************************************************************************
863*4882a593Smuzhiyun *
864*4882a593Smuzhiyun * Driver Interface
865*4882a593Smuzhiyun *
866*4882a593Smuzhiyun *****************************************************************************
867*4882a593Smuzhiyun */
868*4882a593Smuzhiyun
m41t80_probe(struct i2c_client * client,const struct i2c_device_id * id)869*4882a593Smuzhiyun static int m41t80_probe(struct i2c_client *client,
870*4882a593Smuzhiyun const struct i2c_device_id *id)
871*4882a593Smuzhiyun {
872*4882a593Smuzhiyun struct i2c_adapter *adapter = client->adapter;
873*4882a593Smuzhiyun int rc = 0;
874*4882a593Smuzhiyun struct rtc_time tm;
875*4882a593Smuzhiyun struct m41t80_data *m41t80_data = NULL;
876*4882a593Smuzhiyun bool wakeup_source = false;
877*4882a593Smuzhiyun
878*4882a593Smuzhiyun if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK |
879*4882a593Smuzhiyun I2C_FUNC_SMBUS_BYTE_DATA)) {
880*4882a593Smuzhiyun dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
881*4882a593Smuzhiyun return -ENODEV;
882*4882a593Smuzhiyun }
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun m41t80_data = devm_kzalloc(&client->dev, sizeof(*m41t80_data),
885*4882a593Smuzhiyun GFP_KERNEL);
886*4882a593Smuzhiyun if (!m41t80_data)
887*4882a593Smuzhiyun return -ENOMEM;
888*4882a593Smuzhiyun
889*4882a593Smuzhiyun m41t80_data->client = client;
890*4882a593Smuzhiyun if (client->dev.of_node)
891*4882a593Smuzhiyun m41t80_data->features = (unsigned long)
892*4882a593Smuzhiyun of_device_get_match_data(&client->dev);
893*4882a593Smuzhiyun else
894*4882a593Smuzhiyun m41t80_data->features = id->driver_data;
895*4882a593Smuzhiyun i2c_set_clientdata(client, m41t80_data);
896*4882a593Smuzhiyun
897*4882a593Smuzhiyun m41t80_data->rtc = devm_rtc_allocate_device(&client->dev);
898*4882a593Smuzhiyun if (IS_ERR(m41t80_data->rtc))
899*4882a593Smuzhiyun return PTR_ERR(m41t80_data->rtc);
900*4882a593Smuzhiyun
901*4882a593Smuzhiyun #ifdef CONFIG_OF
902*4882a593Smuzhiyun wakeup_source = of_property_read_bool(client->dev.of_node,
903*4882a593Smuzhiyun "wakeup-source");
904*4882a593Smuzhiyun #endif
905*4882a593Smuzhiyun if (client->irq > 0) {
906*4882a593Smuzhiyun rc = devm_request_threaded_irq(&client->dev, client->irq,
907*4882a593Smuzhiyun NULL, m41t80_handle_irq,
908*4882a593Smuzhiyun IRQF_TRIGGER_LOW | IRQF_ONESHOT,
909*4882a593Smuzhiyun "m41t80", client);
910*4882a593Smuzhiyun if (rc) {
911*4882a593Smuzhiyun dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
912*4882a593Smuzhiyun client->irq = 0;
913*4882a593Smuzhiyun wakeup_source = false;
914*4882a593Smuzhiyun }
915*4882a593Smuzhiyun }
916*4882a593Smuzhiyun if (client->irq > 0 || wakeup_source) {
917*4882a593Smuzhiyun m41t80_rtc_ops.read_alarm = m41t80_read_alarm;
918*4882a593Smuzhiyun m41t80_rtc_ops.set_alarm = m41t80_set_alarm;
919*4882a593Smuzhiyun m41t80_rtc_ops.alarm_irq_enable = m41t80_alarm_irq_enable;
920*4882a593Smuzhiyun /* Enable the wakealarm */
921*4882a593Smuzhiyun device_init_wakeup(&client->dev, true);
922*4882a593Smuzhiyun }
923*4882a593Smuzhiyun
924*4882a593Smuzhiyun m41t80_data->rtc->ops = &m41t80_rtc_ops;
925*4882a593Smuzhiyun m41t80_data->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
926*4882a593Smuzhiyun m41t80_data->rtc->range_max = RTC_TIMESTAMP_END_2099;
927*4882a593Smuzhiyun
928*4882a593Smuzhiyun if (client->irq <= 0) {
929*4882a593Smuzhiyun /* We cannot support UIE mode if we do not have an IRQ line */
930*4882a593Smuzhiyun m41t80_data->rtc->uie_unsupported = 1;
931*4882a593Smuzhiyun }
932*4882a593Smuzhiyun
933*4882a593Smuzhiyun /* Make sure HT (Halt Update) bit is cleared */
934*4882a593Smuzhiyun rc = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_HOUR);
935*4882a593Smuzhiyun
936*4882a593Smuzhiyun if (rc >= 0 && rc & M41T80_ALHOUR_HT) {
937*4882a593Smuzhiyun if (m41t80_data->features & M41T80_FEATURE_HT) {
938*4882a593Smuzhiyun m41t80_rtc_read_time(&client->dev, &tm);
939*4882a593Smuzhiyun dev_info(&client->dev, "HT bit was set!\n");
940*4882a593Smuzhiyun dev_info(&client->dev, "Power Down at %ptR\n", &tm);
941*4882a593Smuzhiyun }
942*4882a593Smuzhiyun rc = i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_HOUR,
943*4882a593Smuzhiyun rc & ~M41T80_ALHOUR_HT);
944*4882a593Smuzhiyun }
945*4882a593Smuzhiyun
946*4882a593Smuzhiyun if (rc < 0) {
947*4882a593Smuzhiyun dev_err(&client->dev, "Can't clear HT bit\n");
948*4882a593Smuzhiyun return rc;
949*4882a593Smuzhiyun }
950*4882a593Smuzhiyun
951*4882a593Smuzhiyun /* Make sure ST (stop) bit is cleared */
952*4882a593Smuzhiyun rc = i2c_smbus_read_byte_data(client, M41T80_REG_SEC);
953*4882a593Smuzhiyun
954*4882a593Smuzhiyun if (rc >= 0 && rc & M41T80_SEC_ST)
955*4882a593Smuzhiyun rc = i2c_smbus_write_byte_data(client, M41T80_REG_SEC,
956*4882a593Smuzhiyun rc & ~M41T80_SEC_ST);
957*4882a593Smuzhiyun if (rc < 0) {
958*4882a593Smuzhiyun dev_err(&client->dev, "Can't clear ST bit\n");
959*4882a593Smuzhiyun return rc;
960*4882a593Smuzhiyun }
961*4882a593Smuzhiyun
962*4882a593Smuzhiyun #ifdef CONFIG_RTC_DRV_M41T80_WDT
963*4882a593Smuzhiyun if (m41t80_data->features & M41T80_FEATURE_HT) {
964*4882a593Smuzhiyun save_client = client;
965*4882a593Smuzhiyun rc = misc_register(&wdt_dev);
966*4882a593Smuzhiyun if (rc)
967*4882a593Smuzhiyun return rc;
968*4882a593Smuzhiyun rc = register_reboot_notifier(&wdt_notifier);
969*4882a593Smuzhiyun if (rc) {
970*4882a593Smuzhiyun misc_deregister(&wdt_dev);
971*4882a593Smuzhiyun return rc;
972*4882a593Smuzhiyun }
973*4882a593Smuzhiyun }
974*4882a593Smuzhiyun #endif
975*4882a593Smuzhiyun #ifdef CONFIG_COMMON_CLK
976*4882a593Smuzhiyun if (m41t80_data->features & M41T80_FEATURE_SQ)
977*4882a593Smuzhiyun m41t80_sqw_register_clk(m41t80_data);
978*4882a593Smuzhiyun #endif
979*4882a593Smuzhiyun
980*4882a593Smuzhiyun rc = rtc_register_device(m41t80_data->rtc);
981*4882a593Smuzhiyun if (rc)
982*4882a593Smuzhiyun return rc;
983*4882a593Smuzhiyun
984*4882a593Smuzhiyun return 0;
985*4882a593Smuzhiyun }
986*4882a593Smuzhiyun
m41t80_remove(struct i2c_client * client)987*4882a593Smuzhiyun static int m41t80_remove(struct i2c_client *client)
988*4882a593Smuzhiyun {
989*4882a593Smuzhiyun #ifdef CONFIG_RTC_DRV_M41T80_WDT
990*4882a593Smuzhiyun struct m41t80_data *clientdata = i2c_get_clientdata(client);
991*4882a593Smuzhiyun
992*4882a593Smuzhiyun if (clientdata->features & M41T80_FEATURE_HT) {
993*4882a593Smuzhiyun misc_deregister(&wdt_dev);
994*4882a593Smuzhiyun unregister_reboot_notifier(&wdt_notifier);
995*4882a593Smuzhiyun }
996*4882a593Smuzhiyun #endif
997*4882a593Smuzhiyun
998*4882a593Smuzhiyun return 0;
999*4882a593Smuzhiyun }
1000*4882a593Smuzhiyun
1001*4882a593Smuzhiyun static struct i2c_driver m41t80_driver = {
1002*4882a593Smuzhiyun .driver = {
1003*4882a593Smuzhiyun .name = "rtc-m41t80",
1004*4882a593Smuzhiyun .of_match_table = of_match_ptr(m41t80_of_match),
1005*4882a593Smuzhiyun .pm = &m41t80_pm,
1006*4882a593Smuzhiyun },
1007*4882a593Smuzhiyun .probe = m41t80_probe,
1008*4882a593Smuzhiyun .remove = m41t80_remove,
1009*4882a593Smuzhiyun .id_table = m41t80_id,
1010*4882a593Smuzhiyun };
1011*4882a593Smuzhiyun
1012*4882a593Smuzhiyun module_i2c_driver(m41t80_driver);
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun MODULE_AUTHOR("Alexander Bigga <ab@mycable.de>");
1015*4882a593Smuzhiyun MODULE_DESCRIPTION("ST Microelectronics M41T80 series RTC I2C Client Driver");
1016*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1017