xref: /openbmc/linux/drivers/rtc/rtc-m41t80.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2caaff562SAtsushi Nemoto /*
3caaff562SAtsushi Nemoto  * I2C client/driver for the ST M41T80 family of i2c rtc chips.
4caaff562SAtsushi Nemoto  *
5caaff562SAtsushi Nemoto  * Author: Alexander Bigga <ab@mycable.de>
6caaff562SAtsushi Nemoto  *
7caaff562SAtsushi Nemoto  * Based on m41t00.c by Mark A. Greer <mgreer@mvista.com>
8caaff562SAtsushi Nemoto  *
9caaff562SAtsushi Nemoto  * 2006 (c) mycable GmbH
10caaff562SAtsushi Nemoto  */
11caaff562SAtsushi Nemoto 
12a737e835SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13a737e835SJoe Perches 
1435aa64f3SMaciej W. Rozycki #include <linux/bcd.h>
151373e77bSGary Bisson #include <linux/clk-provider.h>
1635aa64f3SMaciej W. Rozycki #include <linux/i2c.h>
17caaff562SAtsushi Nemoto #include <linux/init.h>
189fb1f68dSMaciej W. Rozycki #include <linux/kernel.h>
1935aa64f3SMaciej W. Rozycki #include <linux/module.h>
20*48144c28SRob Herring #include <linux/of.h>
2135aa64f3SMaciej W. Rozycki #include <linux/rtc.h>
22caaff562SAtsushi Nemoto #include <linux/slab.h>
23613655faSArnd Bergmann #include <linux/mutex.h>
24caaff562SAtsushi Nemoto #include <linux/string.h>
25617780d2SAtsushi Nemoto #ifdef CONFIG_RTC_DRV_M41T80_WDT
26617780d2SAtsushi Nemoto #include <linux/fs.h>
27617780d2SAtsushi Nemoto #include <linux/ioctl.h>
2835aa64f3SMaciej W. Rozycki #include <linux/miscdevice.h>
2935aa64f3SMaciej W. Rozycki #include <linux/reboot.h>
3035aa64f3SMaciej W. Rozycki #include <linux/watchdog.h>
31617780d2SAtsushi Nemoto #endif
32caaff562SAtsushi Nemoto 
33f2b84ee8SMylène Josserand #define M41T80_REG_SSEC		0x00
34f2b84ee8SMylène Josserand #define M41T80_REG_SEC		0x01
35f2b84ee8SMylène Josserand #define M41T80_REG_MIN		0x02
36f2b84ee8SMylène Josserand #define M41T80_REG_HOUR		0x03
37f2b84ee8SMylène Josserand #define M41T80_REG_WDAY		0x04
38f2b84ee8SMylène Josserand #define M41T80_REG_DAY		0x05
39f2b84ee8SMylène Josserand #define M41T80_REG_MON		0x06
40f2b84ee8SMylène Josserand #define M41T80_REG_YEAR		0x07
41f2b84ee8SMylène Josserand #define M41T80_REG_ALARM_MON	0x0a
42f2b84ee8SMylène Josserand #define M41T80_REG_ALARM_DAY	0x0b
43f2b84ee8SMylène Josserand #define M41T80_REG_ALARM_HOUR	0x0c
44f2b84ee8SMylène Josserand #define M41T80_REG_ALARM_MIN	0x0d
45f2b84ee8SMylène Josserand #define M41T80_REG_ALARM_SEC	0x0e
46f2b84ee8SMylène Josserand #define M41T80_REG_FLAGS	0x0f
47caaff562SAtsushi Nemoto #define M41T80_REG_SQW		0x13
48caaff562SAtsushi Nemoto 
49caaff562SAtsushi Nemoto #define M41T80_DATETIME_REG_SIZE	(M41T80_REG_YEAR + 1)
50caaff562SAtsushi Nemoto #define M41T80_ALARM_REG_SIZE	\
51caaff562SAtsushi Nemoto 	(M41T80_REG_ALARM_SEC + 1 - M41T80_REG_ALARM_MON)
52caaff562SAtsushi Nemoto 
531373e77bSGary Bisson #define M41T80_SQW_MAX_FREQ	32768
541373e77bSGary Bisson 
5554339f3bSMylène Josserand #define M41T80_SEC_ST		BIT(7)	/* ST: Stop Bit */
5654339f3bSMylène Josserand #define M41T80_ALMON_AFE	BIT(7)	/* AFE: AF Enable Bit */
5754339f3bSMylène Josserand #define M41T80_ALMON_SQWE	BIT(6)	/* SQWE: SQW Enable Bit */
5854339f3bSMylène Josserand #define M41T80_ALHOUR_HT	BIT(6)	/* HT: Halt Update Bit */
5905a7f27aSMylène Josserand #define M41T80_FLAGS_OF		BIT(2)	/* OF: Oscillator Failure Bit */
6054339f3bSMylène Josserand #define M41T80_FLAGS_AF		BIT(6)	/* AF: Alarm Flag Bit */
6154339f3bSMylène Josserand #define M41T80_FLAGS_BATT_LOW	BIT(4)	/* BL: Battery Low Bit */
6254339f3bSMylène Josserand #define M41T80_WATCHDOG_RB2	BIT(7)	/* RB: Watchdog resolution */
6354339f3bSMylène Josserand #define M41T80_WATCHDOG_RB1	BIT(1)	/* RB: Watchdog resolution */
6454339f3bSMylène Josserand #define M41T80_WATCHDOG_RB0	BIT(0)	/* RB: Watchdog resolution */
65caaff562SAtsushi Nemoto 
6654339f3bSMylène Josserand #define M41T80_FEATURE_HT	BIT(0)	/* Halt feature */
6754339f3bSMylène Josserand #define M41T80_FEATURE_BL	BIT(1)	/* Battery low indicator */
6854339f3bSMylène Josserand #define M41T80_FEATURE_SQ	BIT(2)	/* Squarewave feature */
6954339f3bSMylène Josserand #define M41T80_FEATURE_WD	BIT(3)	/* Extra watchdog resolution */
7054339f3bSMylène Josserand #define M41T80_FEATURE_SQ_ALT	BIT(4)	/* RSx bits are in reg 4 */
71caaff562SAtsushi Nemoto 
723760f736SJean Delvare static const struct i2c_device_id m41t80_id[] = {
73f30281f4SDaniel Glockner 	{ "m41t62", M41T80_FEATURE_SQ | M41T80_FEATURE_SQ_ALT },
74d3a126fcSSteven A. Falco 	{ "m41t65", M41T80_FEATURE_HT | M41T80_FEATURE_WD },
75d3a126fcSSteven A. Falco 	{ "m41t80", M41T80_FEATURE_SQ },
76d3a126fcSSteven A. Falco 	{ "m41t81", M41T80_FEATURE_HT | M41T80_FEATURE_SQ},
77d3a126fcSSteven A. Falco 	{ "m41t81s", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
78d3a126fcSSteven A. Falco 	{ "m41t82", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
79d3a126fcSSteven A. Falco 	{ "m41t83", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
80d3a126fcSSteven A. Falco 	{ "m41st84", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
81d3a126fcSSteven A. Falco 	{ "m41st85", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
82d3a126fcSSteven A. Falco 	{ "m41st87", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
836b1a5235SWolfram Sang 	{ "rv4162", M41T80_FEATURE_SQ | M41T80_FEATURE_WD | M41T80_FEATURE_SQ_ALT },
843760f736SJean Delvare 	{ }
85caaff562SAtsushi Nemoto };
863760f736SJean Delvare MODULE_DEVICE_TABLE(i2c, m41t80_id);
87caaff562SAtsushi Nemoto 
882717c59eSAlexandre Belloni static const __maybe_unused struct of_device_id m41t80_of_match[] = {
89eb235c56SJavier Martinez Canillas 	{
90eb235c56SJavier Martinez Canillas 		.compatible = "st,m41t62",
91eb235c56SJavier Martinez Canillas 		.data = (void *)(M41T80_FEATURE_SQ | M41T80_FEATURE_SQ_ALT)
92eb235c56SJavier Martinez Canillas 	},
93eb235c56SJavier Martinez Canillas 	{
94eb235c56SJavier Martinez Canillas 		.compatible = "st,m41t65",
95eb235c56SJavier Martinez Canillas 		.data = (void *)(M41T80_FEATURE_HT | M41T80_FEATURE_WD)
96eb235c56SJavier Martinez Canillas 	},
97eb235c56SJavier Martinez Canillas 	{
98eb235c56SJavier Martinez Canillas 		.compatible = "st,m41t80",
99eb235c56SJavier Martinez Canillas 		.data = (void *)(M41T80_FEATURE_SQ)
100eb235c56SJavier Martinez Canillas 	},
101eb235c56SJavier Martinez Canillas 	{
102eb235c56SJavier Martinez Canillas 		.compatible = "st,m41t81",
103eb235c56SJavier Martinez Canillas 		.data = (void *)(M41T80_FEATURE_HT | M41T80_FEATURE_SQ)
104eb235c56SJavier Martinez Canillas 	},
105eb235c56SJavier Martinez Canillas 	{
106eb235c56SJavier Martinez Canillas 		.compatible = "st,m41t81s",
107eb235c56SJavier Martinez Canillas 		.data = (void *)(M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ)
108eb235c56SJavier Martinez Canillas 	},
109eb235c56SJavier Martinez Canillas 	{
110eb235c56SJavier Martinez Canillas 		.compatible = "st,m41t82",
111eb235c56SJavier Martinez Canillas 		.data = (void *)(M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ)
112eb235c56SJavier Martinez Canillas 	},
113eb235c56SJavier Martinez Canillas 	{
114eb235c56SJavier Martinez Canillas 		.compatible = "st,m41t83",
115eb235c56SJavier Martinez Canillas 		.data = (void *)(M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ)
116eb235c56SJavier Martinez Canillas 	},
117eb235c56SJavier Martinez Canillas 	{
118eb235c56SJavier Martinez Canillas 		.compatible = "st,m41t84",
119eb235c56SJavier Martinez Canillas 		.data = (void *)(M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ)
120eb235c56SJavier Martinez Canillas 	},
121eb235c56SJavier Martinez Canillas 	{
122eb235c56SJavier Martinez Canillas 		.compatible = "st,m41t85",
123eb235c56SJavier Martinez Canillas 		.data = (void *)(M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ)
124eb235c56SJavier Martinez Canillas 	},
125eb235c56SJavier Martinez Canillas 	{
126eb235c56SJavier Martinez Canillas 		.compatible = "st,m41t87",
127eb235c56SJavier Martinez Canillas 		.data = (void *)(M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ)
128eb235c56SJavier Martinez Canillas 	},
129eb235c56SJavier Martinez Canillas 	{
130a897bf13SAlexandre Belloni 		.compatible = "microcrystal,rv4162",
131a897bf13SAlexandre Belloni 		.data = (void *)(M41T80_FEATURE_SQ | M41T80_FEATURE_WD | M41T80_FEATURE_SQ_ALT)
132a897bf13SAlexandre Belloni 	},
133a897bf13SAlexandre Belloni 	/* DT compatibility only, do not use compatibles below: */
134a897bf13SAlexandre Belloni 	{
135eb235c56SJavier Martinez Canillas 		.compatible = "st,rv4162",
136eb235c56SJavier Martinez Canillas 		.data = (void *)(M41T80_FEATURE_SQ | M41T80_FEATURE_WD | M41T80_FEATURE_SQ_ALT)
137eb235c56SJavier Martinez Canillas 	},
138eb235c56SJavier Martinez Canillas 	{
139eb235c56SJavier Martinez Canillas 		.compatible = "rv4162",
140eb235c56SJavier Martinez Canillas 		.data = (void *)(M41T80_FEATURE_SQ | M41T80_FEATURE_WD | M41T80_FEATURE_SQ_ALT)
141eb235c56SJavier Martinez Canillas 	},
142eb235c56SJavier Martinez Canillas 	{ }
143eb235c56SJavier Martinez Canillas };
144eb235c56SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, m41t80_of_match);
145eb235c56SJavier Martinez Canillas 
146caaff562SAtsushi Nemoto struct m41t80_data {
147eb235c56SJavier Martinez Canillas 	unsigned long features;
1481373e77bSGary Bisson 	struct i2c_client *client;
149caaff562SAtsushi Nemoto 	struct rtc_device *rtc;
1501373e77bSGary Bisson #ifdef CONFIG_COMMON_CLK
1511373e77bSGary Bisson 	struct clk_hw sqw;
1522cb90ed3STroy Kisky 	unsigned long freq;
15313bb1d78STroy Kisky 	unsigned int sqwe;
1541373e77bSGary Bisson #endif
155caaff562SAtsushi Nemoto };
156caaff562SAtsushi Nemoto 
m41t80_handle_irq(int irq,void * dev_id)1579c6dfed9SMylène Josserand static irqreturn_t m41t80_handle_irq(int irq, void *dev_id)
1589c6dfed9SMylène Josserand {
1599c6dfed9SMylène Josserand 	struct i2c_client *client = dev_id;
1609c6dfed9SMylène Josserand 	struct m41t80_data *m41t80 = i2c_get_clientdata(client);
1619c6dfed9SMylène Josserand 	unsigned long events = 0;
1629c6dfed9SMylène Josserand 	int flags, flags_afe;
1639c6dfed9SMylène Josserand 
16406c6e321SAlexandre Belloni 	rtc_lock(m41t80->rtc);
1659c6dfed9SMylène Josserand 
1669c6dfed9SMylène Josserand 	flags_afe = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
1679c6dfed9SMylène Josserand 	if (flags_afe < 0) {
16806c6e321SAlexandre Belloni 		rtc_unlock(m41t80->rtc);
1699c6dfed9SMylène Josserand 		return IRQ_NONE;
1709c6dfed9SMylène Josserand 	}
1719c6dfed9SMylène Josserand 
1729c6dfed9SMylène Josserand 	flags = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
1739c6dfed9SMylène Josserand 	if (flags <= 0) {
17406c6e321SAlexandre Belloni 		rtc_unlock(m41t80->rtc);
1759c6dfed9SMylène Josserand 		return IRQ_NONE;
1769c6dfed9SMylène Josserand 	}
1779c6dfed9SMylène Josserand 
1789c6dfed9SMylène Josserand 	if (flags & M41T80_FLAGS_AF) {
1799c6dfed9SMylène Josserand 		flags &= ~M41T80_FLAGS_AF;
1809c6dfed9SMylène Josserand 		flags_afe &= ~M41T80_ALMON_AFE;
1819c6dfed9SMylène Josserand 		events |= RTC_AF;
1829c6dfed9SMylène Josserand 	}
1839c6dfed9SMylène Josserand 
1849c6dfed9SMylène Josserand 	if (events) {
1859c6dfed9SMylène Josserand 		rtc_update_irq(m41t80->rtc, 1, events);
1869c6dfed9SMylène Josserand 		i2c_smbus_write_byte_data(client, M41T80_REG_FLAGS, flags);
1879c6dfed9SMylène Josserand 		i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
1889c6dfed9SMylène Josserand 					  flags_afe);
1899c6dfed9SMylène Josserand 	}
1909c6dfed9SMylène Josserand 
19106c6e321SAlexandre Belloni 	rtc_unlock(m41t80->rtc);
1929c6dfed9SMylène Josserand 
1939c6dfed9SMylène Josserand 	return IRQ_HANDLED;
1949c6dfed9SMylène Josserand }
1959c6dfed9SMylène Josserand 
m41t80_rtc_read_time(struct device * dev,struct rtc_time * tm)196e2c8e1a9SAlexandre Belloni static int m41t80_rtc_read_time(struct device *dev, struct rtc_time *tm)
197caaff562SAtsushi Nemoto {
198e2c8e1a9SAlexandre Belloni 	struct i2c_client *client = to_i2c_client(dev);
199f2b84ee8SMylène Josserand 	unsigned char buf[8];
20005a7f27aSMylène Josserand 	int err, flags;
20105a7f27aSMylène Josserand 
20205a7f27aSMylène Josserand 	flags = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
20305a7f27aSMylène Josserand 	if (flags < 0)
20405a7f27aSMylène Josserand 		return flags;
20505a7f27aSMylène Josserand 
20605a7f27aSMylène Josserand 	if (flags & M41T80_FLAGS_OF) {
20705a7f27aSMylène Josserand 		dev_err(&client->dev, "Oscillator failure, data is invalid.\n");
20805a7f27aSMylène Josserand 		return -EINVAL;
20905a7f27aSMylène Josserand 	}
210caaff562SAtsushi Nemoto 
211f2b84ee8SMylène Josserand 	err = i2c_smbus_read_i2c_block_data(client, M41T80_REG_SSEC,
212f2b84ee8SMylène Josserand 					    sizeof(buf), buf);
213f2b84ee8SMylène Josserand 	if (err < 0) {
214f2b84ee8SMylène Josserand 		dev_err(&client->dev, "Unable to read date\n");
215f1bd154dSMaciej W. Rozycki 		return err;
216caaff562SAtsushi Nemoto 	}
217caaff562SAtsushi Nemoto 
218fe20ba70SAdrian Bunk 	tm->tm_sec = bcd2bin(buf[M41T80_REG_SEC] & 0x7f);
219fe20ba70SAdrian Bunk 	tm->tm_min = bcd2bin(buf[M41T80_REG_MIN] & 0x7f);
220fe20ba70SAdrian Bunk 	tm->tm_hour = bcd2bin(buf[M41T80_REG_HOUR] & 0x3f);
221fe20ba70SAdrian Bunk 	tm->tm_mday = bcd2bin(buf[M41T80_REG_DAY] & 0x3f);
222caaff562SAtsushi Nemoto 	tm->tm_wday = buf[M41T80_REG_WDAY] & 0x07;
223fe20ba70SAdrian Bunk 	tm->tm_mon = bcd2bin(buf[M41T80_REG_MON] & 0x1f) - 1;
224caaff562SAtsushi Nemoto 
225caaff562SAtsushi Nemoto 	/* assume 20YY not 19YY, and ignore the Century Bit */
226fe20ba70SAdrian Bunk 	tm->tm_year = bcd2bin(buf[M41T80_REG_YEAR]) + 100;
22722652ba7SAlexandre Belloni 	return 0;
228caaff562SAtsushi Nemoto }
229caaff562SAtsushi Nemoto 
m41t80_rtc_set_time(struct device * dev,struct rtc_time * tm)230e2c8e1a9SAlexandre Belloni static int m41t80_rtc_set_time(struct device *dev, struct rtc_time *tm)
231caaff562SAtsushi Nemoto {
232e2c8e1a9SAlexandre Belloni 	struct i2c_client *client = to_i2c_client(dev);
2330f546b05SGary Bisson 	struct m41t80_data *clientdata = i2c_get_clientdata(client);
234f2b84ee8SMylène Josserand 	unsigned char buf[8];
23505a7f27aSMylène Josserand 	int err, flags;
236caaff562SAtsushi Nemoto 
237f2b84ee8SMylène Josserand 	buf[M41T80_REG_SSEC] = 0;
238f2b84ee8SMylène Josserand 	buf[M41T80_REG_SEC] = bin2bcd(tm->tm_sec);
239f2b84ee8SMylène Josserand 	buf[M41T80_REG_MIN] = bin2bcd(tm->tm_min);
240f2b84ee8SMylène Josserand 	buf[M41T80_REG_HOUR] = bin2bcd(tm->tm_hour);
241f2b84ee8SMylène Josserand 	buf[M41T80_REG_DAY] = bin2bcd(tm->tm_mday);
242f2b84ee8SMylène Josserand 	buf[M41T80_REG_MON] = bin2bcd(tm->tm_mon + 1);
243f2b84ee8SMylène Josserand 	buf[M41T80_REG_YEAR] = bin2bcd(tm->tm_year - 100);
244f2b84ee8SMylène Josserand 	buf[M41T80_REG_WDAY] = tm->tm_wday;
245f2b84ee8SMylène Josserand 
2460f546b05SGary Bisson 	/* If the square wave output is controlled in the weekday register */
2470f546b05SGary Bisson 	if (clientdata->features & M41T80_FEATURE_SQ_ALT) {
2480f546b05SGary Bisson 		int val;
2490f546b05SGary Bisson 
2500f546b05SGary Bisson 		val = i2c_smbus_read_byte_data(client, M41T80_REG_WDAY);
2510f546b05SGary Bisson 		if (val < 0)
2520f546b05SGary Bisson 			return val;
2530f546b05SGary Bisson 
2540f546b05SGary Bisson 		buf[M41T80_REG_WDAY] |= (val & 0xf0);
2550f546b05SGary Bisson 	}
2560f546b05SGary Bisson 
257f2b84ee8SMylène Josserand 	err = i2c_smbus_write_i2c_block_data(client, M41T80_REG_SSEC,
258f2b84ee8SMylène Josserand 					     sizeof(buf), buf);
259f2b84ee8SMylène Josserand 	if (err < 0) {
260f2b84ee8SMylène Josserand 		dev_err(&client->dev, "Unable to write to date registers\n");
261f2b84ee8SMylène Josserand 		return err;
262caaff562SAtsushi Nemoto 	}
263f2b84ee8SMylène Josserand 
26405a7f27aSMylène Josserand 	/* Clear the OF bit of Flags Register */
26505a7f27aSMylène Josserand 	flags = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
26605a7f27aSMylène Josserand 	if (flags < 0)
26705a7f27aSMylène Josserand 		return flags;
26805a7f27aSMylène Josserand 
269f1bd154dSMaciej W. Rozycki 	err = i2c_smbus_write_byte_data(client, M41T80_REG_FLAGS,
270f1bd154dSMaciej W. Rozycki 					flags & ~M41T80_FLAGS_OF);
271f1bd154dSMaciej W. Rozycki 	if (err < 0) {
27205a7f27aSMylène Josserand 		dev_err(&client->dev, "Unable to write flags register\n");
273f1bd154dSMaciej W. Rozycki 		return err;
27405a7f27aSMylène Josserand 	}
27505a7f27aSMylène Josserand 
276f2b84ee8SMylène Josserand 	return err;
277caaff562SAtsushi Nemoto }
278caaff562SAtsushi Nemoto 
m41t80_rtc_proc(struct device * dev,struct seq_file * seq)279caaff562SAtsushi Nemoto static int m41t80_rtc_proc(struct device *dev, struct seq_file *seq)
280caaff562SAtsushi Nemoto {
281caaff562SAtsushi Nemoto 	struct i2c_client *client = to_i2c_client(dev);
282caaff562SAtsushi Nemoto 	struct m41t80_data *clientdata = i2c_get_clientdata(client);
283f1bd154dSMaciej W. Rozycki 	int reg;
284caaff562SAtsushi Nemoto 
2853760f736SJean Delvare 	if (clientdata->features & M41T80_FEATURE_BL) {
286caaff562SAtsushi Nemoto 		reg = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
287f1bd154dSMaciej W. Rozycki 		if (reg < 0)
288f1bd154dSMaciej W. Rozycki 			return reg;
289caaff562SAtsushi Nemoto 		seq_printf(seq, "battery\t\t: %s\n",
290caaff562SAtsushi Nemoto 			   (reg & M41T80_FLAGS_BATT_LOW) ? "exhausted" : "ok");
291caaff562SAtsushi Nemoto 	}
292caaff562SAtsushi Nemoto 	return 0;
293caaff562SAtsushi Nemoto }
294caaff562SAtsushi Nemoto 
m41t80_alarm_irq_enable(struct device * dev,unsigned int enabled)2959c6dfed9SMylène Josserand static int m41t80_alarm_irq_enable(struct device *dev, unsigned int enabled)
2969c6dfed9SMylène Josserand {
2979c6dfed9SMylène Josserand 	struct i2c_client *client = to_i2c_client(dev);
2989c6dfed9SMylène Josserand 	int flags, retval;
2999c6dfed9SMylène Josserand 
3009c6dfed9SMylène Josserand 	flags = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
3019c6dfed9SMylène Josserand 	if (flags < 0)
3029c6dfed9SMylène Josserand 		return flags;
3039c6dfed9SMylène Josserand 
3049c6dfed9SMylène Josserand 	if (enabled)
3059c6dfed9SMylène Josserand 		flags |= M41T80_ALMON_AFE;
3069c6dfed9SMylène Josserand 	else
3079c6dfed9SMylène Josserand 		flags &= ~M41T80_ALMON_AFE;
3089c6dfed9SMylène Josserand 
3099c6dfed9SMylène Josserand 	retval = i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON, flags);
3109c6dfed9SMylène Josserand 	if (retval < 0) {
311e89487feSStefan Christ 		dev_err(dev, "Unable to enable alarm IRQ %d\n", retval);
3129c6dfed9SMylène Josserand 		return retval;
3139c6dfed9SMylène Josserand 	}
3149c6dfed9SMylène Josserand 	return 0;
3159c6dfed9SMylène Josserand }
3169c6dfed9SMylène Josserand 
m41t80_set_alarm(struct device * dev,struct rtc_wkalrm * alrm)3179c6dfed9SMylène Josserand static int m41t80_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
3189c6dfed9SMylène Josserand {
3199c6dfed9SMylène Josserand 	struct i2c_client *client = to_i2c_client(dev);
3209c6dfed9SMylène Josserand 	u8 alarmvals[5];
3219c6dfed9SMylène Josserand 	int ret, err;
3229c6dfed9SMylène Josserand 
3239c6dfed9SMylène Josserand 	alarmvals[0] = bin2bcd(alrm->time.tm_mon + 1);
3249c6dfed9SMylène Josserand 	alarmvals[1] = bin2bcd(alrm->time.tm_mday);
3259c6dfed9SMylène Josserand 	alarmvals[2] = bin2bcd(alrm->time.tm_hour);
3269c6dfed9SMylène Josserand 	alarmvals[3] = bin2bcd(alrm->time.tm_min);
3279c6dfed9SMylène Josserand 	alarmvals[4] = bin2bcd(alrm->time.tm_sec);
3289c6dfed9SMylène Josserand 
3299c6dfed9SMylène Josserand 	/* Clear AF and AFE flags */
3309c6dfed9SMylène Josserand 	ret = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
3319c6dfed9SMylène Josserand 	if (ret < 0)
3329c6dfed9SMylène Josserand 		return ret;
3339c6dfed9SMylène Josserand 	err = i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
3349c6dfed9SMylène Josserand 					ret & ~(M41T80_ALMON_AFE));
3359c6dfed9SMylène Josserand 	if (err < 0) {
3369c6dfed9SMylène Josserand 		dev_err(dev, "Unable to clear AFE bit\n");
3379c6dfed9SMylène Josserand 		return err;
3389c6dfed9SMylène Josserand 	}
3399c6dfed9SMylène Josserand 
3402de9261cSGary Bisson 	/* Keep SQWE bit value */
3412de9261cSGary Bisson 	alarmvals[0] |= (ret & M41T80_ALMON_SQWE);
3422de9261cSGary Bisson 
3439c6dfed9SMylène Josserand 	ret = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
3449c6dfed9SMylène Josserand 	if (ret < 0)
3459c6dfed9SMylène Josserand 		return ret;
3469c6dfed9SMylène Josserand 
3479c6dfed9SMylène Josserand 	err = i2c_smbus_write_byte_data(client, M41T80_REG_FLAGS,
3489c6dfed9SMylène Josserand 					ret & ~(M41T80_FLAGS_AF));
3499c6dfed9SMylène Josserand 	if (err < 0) {
3509c6dfed9SMylène Josserand 		dev_err(dev, "Unable to clear AF bit\n");
3519c6dfed9SMylène Josserand 		return err;
3529c6dfed9SMylène Josserand 	}
3539c6dfed9SMylène Josserand 
3549c6dfed9SMylène Josserand 	/* Write the alarm */
3559c6dfed9SMylène Josserand 	err = i2c_smbus_write_i2c_block_data(client, M41T80_REG_ALARM_MON,
3569c6dfed9SMylène Josserand 					     5, alarmvals);
3579c6dfed9SMylène Josserand 	if (err)
3589c6dfed9SMylène Josserand 		return err;
3599c6dfed9SMylène Josserand 
3609c6dfed9SMylène Josserand 	/* Enable the alarm interrupt */
3619c6dfed9SMylène Josserand 	if (alrm->enabled) {
3629c6dfed9SMylène Josserand 		alarmvals[0] |= M41T80_ALMON_AFE;
3639c6dfed9SMylène Josserand 		err = i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
3649c6dfed9SMylène Josserand 						alarmvals[0]);
3659c6dfed9SMylène Josserand 		if (err)
3669c6dfed9SMylène Josserand 			return err;
3679c6dfed9SMylène Josserand 	}
3689c6dfed9SMylène Josserand 
3699c6dfed9SMylène Josserand 	return 0;
3709c6dfed9SMylène Josserand }
3719c6dfed9SMylène Josserand 
m41t80_read_alarm(struct device * dev,struct rtc_wkalrm * alrm)3729c6dfed9SMylène Josserand static int m41t80_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
3739c6dfed9SMylène Josserand {
3749c6dfed9SMylène Josserand 	struct i2c_client *client = to_i2c_client(dev);
3759c6dfed9SMylène Josserand 	u8 alarmvals[5];
3769c6dfed9SMylène Josserand 	int flags, ret;
3779c6dfed9SMylène Josserand 
3789c6dfed9SMylène Josserand 	ret = i2c_smbus_read_i2c_block_data(client, M41T80_REG_ALARM_MON,
3799c6dfed9SMylène Josserand 					    5, alarmvals);
3809c6dfed9SMylène Josserand 	if (ret != 5)
3819c6dfed9SMylène Josserand 		return ret < 0 ? ret : -EIO;
3829c6dfed9SMylène Josserand 
3839c6dfed9SMylène Josserand 	flags = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
3849c6dfed9SMylène Josserand 	if (flags < 0)
3859c6dfed9SMylène Josserand 		return flags;
3869c6dfed9SMylène Josserand 
3879c6dfed9SMylène Josserand 	alrm->time.tm_sec  = bcd2bin(alarmvals[4] & 0x7f);
3889c6dfed9SMylène Josserand 	alrm->time.tm_min  = bcd2bin(alarmvals[3] & 0x7f);
3899c6dfed9SMylène Josserand 	alrm->time.tm_hour = bcd2bin(alarmvals[2] & 0x3f);
3909c6dfed9SMylène Josserand 	alrm->time.tm_mday = bcd2bin(alarmvals[1] & 0x3f);
3913cc9ffbbSMaciej W. Rozycki 	alrm->time.tm_mon  = bcd2bin(alarmvals[0] & 0x3f) - 1;
3929c6dfed9SMylène Josserand 
3939c6dfed9SMylène Josserand 	alrm->enabled = !!(alarmvals[0] & M41T80_ALMON_AFE);
3949c6dfed9SMylène Josserand 	alrm->pending = (flags & M41T80_FLAGS_AF) && alrm->enabled;
3959c6dfed9SMylène Josserand 
3969c6dfed9SMylène Josserand 	return 0;
3979c6dfed9SMylène Josserand }
3989c6dfed9SMylène Josserand 
3993948a866SAlexandre Belloni static const struct rtc_class_ops m41t80_rtc_ops = {
40048e97667SPaul Bolle 	.read_time = m41t80_rtc_read_time,
40148e97667SPaul Bolle 	.set_time = m41t80_rtc_set_time,
402caaff562SAtsushi Nemoto 	.proc = m41t80_rtc_proc,
4033948a866SAlexandre Belloni 	.read_alarm = m41t80_read_alarm,
4043948a866SAlexandre Belloni 	.set_alarm = m41t80_set_alarm,
4053948a866SAlexandre Belloni 	.alarm_irq_enable = m41t80_alarm_irq_enable,
406caaff562SAtsushi Nemoto };
407caaff562SAtsushi Nemoto 
408ae036af8SStefan Christ #ifdef CONFIG_PM_SLEEP
m41t80_suspend(struct device * dev)409ae036af8SStefan Christ static int m41t80_suspend(struct device *dev)
410ae036af8SStefan Christ {
411ae036af8SStefan Christ 	struct i2c_client *client = to_i2c_client(dev);
412ae036af8SStefan Christ 
413ae036af8SStefan Christ 	if (client->irq >= 0 && device_may_wakeup(dev))
414ae036af8SStefan Christ 		enable_irq_wake(client->irq);
415ae036af8SStefan Christ 
416ae036af8SStefan Christ 	return 0;
417ae036af8SStefan Christ }
418ae036af8SStefan Christ 
m41t80_resume(struct device * dev)419ae036af8SStefan Christ static int m41t80_resume(struct device *dev)
420ae036af8SStefan Christ {
421ae036af8SStefan Christ 	struct i2c_client *client = to_i2c_client(dev);
422ae036af8SStefan Christ 
423ae036af8SStefan Christ 	if (client->irq >= 0 && device_may_wakeup(dev))
424ae036af8SStefan Christ 		disable_irq_wake(client->irq);
425ae036af8SStefan Christ 
426ae036af8SStefan Christ 	return 0;
427ae036af8SStefan Christ }
428ae036af8SStefan Christ #endif
429ae036af8SStefan Christ 
430ae036af8SStefan Christ static SIMPLE_DEV_PM_OPS(m41t80_pm, m41t80_suspend, m41t80_resume);
431ae036af8SStefan Christ 
4321373e77bSGary Bisson #ifdef CONFIG_COMMON_CLK
4331373e77bSGary Bisson #define sqw_to_m41t80_data(_hw) container_of(_hw, struct m41t80_data, sqw)
4341373e77bSGary Bisson 
m41t80_decode_freq(int setting)4352cb90ed3STroy Kisky static unsigned long m41t80_decode_freq(int setting)
4361373e77bSGary Bisson {
4372cb90ed3STroy Kisky 	return (setting == 0) ? 0 : (setting == 1) ? M41T80_SQW_MAX_FREQ :
4382cb90ed3STroy Kisky 		M41T80_SQW_MAX_FREQ >> setting;
4392cb90ed3STroy Kisky }
4402cb90ed3STroy Kisky 
m41t80_get_freq(struct m41t80_data * m41t80)4412cb90ed3STroy Kisky static unsigned long m41t80_get_freq(struct m41t80_data *m41t80)
4422cb90ed3STroy Kisky {
4431373e77bSGary Bisson 	struct i2c_client *client = m41t80->client;
4441373e77bSGary Bisson 	int reg_sqw = (m41t80->features & M41T80_FEATURE_SQ_ALT) ?
4451373e77bSGary Bisson 		M41T80_REG_WDAY : M41T80_REG_SQW;
4461373e77bSGary Bisson 	int ret = i2c_smbus_read_byte_data(client, reg_sqw);
4471373e77bSGary Bisson 
4481373e77bSGary Bisson 	if (ret < 0)
4491373e77bSGary Bisson 		return 0;
4502cb90ed3STroy Kisky 	return m41t80_decode_freq(ret >> 4);
4512cb90ed3STroy Kisky }
4521373e77bSGary Bisson 
m41t80_sqw_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)4532cb90ed3STroy Kisky static unsigned long m41t80_sqw_recalc_rate(struct clk_hw *hw,
4542cb90ed3STroy Kisky 					    unsigned long parent_rate)
4552cb90ed3STroy Kisky {
4562cb90ed3STroy Kisky 	return sqw_to_m41t80_data(hw)->freq;
4571373e77bSGary Bisson }
4581373e77bSGary Bisson 
m41t80_sqw_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * prate)4591373e77bSGary Bisson static long m41t80_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
4601373e77bSGary Bisson 				  unsigned long *prate)
4611373e77bSGary Bisson {
462c8384bb0STroy Kisky 	if (rate >= M41T80_SQW_MAX_FREQ)
463c8384bb0STroy Kisky 		return M41T80_SQW_MAX_FREQ;
464c8384bb0STroy Kisky 	if (rate >= M41T80_SQW_MAX_FREQ / 4)
465c8384bb0STroy Kisky 		return M41T80_SQW_MAX_FREQ / 4;
466c8384bb0STroy Kisky 	if (!rate)
4671373e77bSGary Bisson 		return 0;
468c8384bb0STroy Kisky 	return 1 << ilog2(rate);
4691373e77bSGary Bisson }
4701373e77bSGary Bisson 
m41t80_sqw_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)4711373e77bSGary Bisson static int m41t80_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
4721373e77bSGary Bisson 			       unsigned long parent_rate)
4731373e77bSGary Bisson {
4741373e77bSGary Bisson 	struct m41t80_data *m41t80 = sqw_to_m41t80_data(hw);
4751373e77bSGary Bisson 	struct i2c_client *client = m41t80->client;
4761373e77bSGary Bisson 	int reg_sqw = (m41t80->features & M41T80_FEATURE_SQ_ALT) ?
4771373e77bSGary Bisson 		M41T80_REG_WDAY : M41T80_REG_SQW;
4781373e77bSGary Bisson 	int reg, ret, val = 0;
4791373e77bSGary Bisson 
48005a03bf2STroy Kisky 	if (rate >= M41T80_SQW_MAX_FREQ)
4811373e77bSGary Bisson 		val = 1;
48205a03bf2STroy Kisky 	else if (rate >= M41T80_SQW_MAX_FREQ / 4)
48305a03bf2STroy Kisky 		val = 2;
48405a03bf2STroy Kisky 	else if (rate)
48505a03bf2STroy Kisky 		val = 15 - ilog2(rate);
4861373e77bSGary Bisson 
4871373e77bSGary Bisson 	reg = i2c_smbus_read_byte_data(client, reg_sqw);
4881373e77bSGary Bisson 	if (reg < 0)
4891373e77bSGary Bisson 		return reg;
4901373e77bSGary Bisson 
4911373e77bSGary Bisson 	reg = (reg & 0x0f) | (val << 4);
4921373e77bSGary Bisson 
4931373e77bSGary Bisson 	ret = i2c_smbus_write_byte_data(client, reg_sqw, reg);
4942cb90ed3STroy Kisky 	if (!ret)
4952cb90ed3STroy Kisky 		m41t80->freq = m41t80_decode_freq(val);
4961373e77bSGary Bisson 	return ret;
4971373e77bSGary Bisson }
4981373e77bSGary Bisson 
m41t80_sqw_control(struct clk_hw * hw,bool enable)4991373e77bSGary Bisson static int m41t80_sqw_control(struct clk_hw *hw, bool enable)
5001373e77bSGary Bisson {
5011373e77bSGary Bisson 	struct m41t80_data *m41t80 = sqw_to_m41t80_data(hw);
5021373e77bSGary Bisson 	struct i2c_client *client = m41t80->client;
5031373e77bSGary Bisson 	int ret = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
5041373e77bSGary Bisson 
5051373e77bSGary Bisson 	if (ret < 0)
5061373e77bSGary Bisson 		return ret;
5071373e77bSGary Bisson 
5081373e77bSGary Bisson 	if (enable)
5091373e77bSGary Bisson 		ret |= M41T80_ALMON_SQWE;
5101373e77bSGary Bisson 	else
5111373e77bSGary Bisson 		ret &= ~M41T80_ALMON_SQWE;
5121373e77bSGary Bisson 
51313bb1d78STroy Kisky 	ret = i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON, ret);
51413bb1d78STroy Kisky 	if (!ret)
51513bb1d78STroy Kisky 		m41t80->sqwe = enable;
51613bb1d78STroy Kisky 	return ret;
5171373e77bSGary Bisson }
5181373e77bSGary Bisson 
m41t80_sqw_prepare(struct clk_hw * hw)5191373e77bSGary Bisson static int m41t80_sqw_prepare(struct clk_hw *hw)
5201373e77bSGary Bisson {
5211373e77bSGary Bisson 	return m41t80_sqw_control(hw, 1);
5221373e77bSGary Bisson }
5231373e77bSGary Bisson 
m41t80_sqw_unprepare(struct clk_hw * hw)5241373e77bSGary Bisson static void m41t80_sqw_unprepare(struct clk_hw *hw)
5251373e77bSGary Bisson {
5261373e77bSGary Bisson 	m41t80_sqw_control(hw, 0);
5271373e77bSGary Bisson }
5281373e77bSGary Bisson 
m41t80_sqw_is_prepared(struct clk_hw * hw)5291373e77bSGary Bisson static int m41t80_sqw_is_prepared(struct clk_hw *hw)
5301373e77bSGary Bisson {
53113bb1d78STroy Kisky 	return sqw_to_m41t80_data(hw)->sqwe;
5321373e77bSGary Bisson }
5331373e77bSGary Bisson 
5341373e77bSGary Bisson static const struct clk_ops m41t80_sqw_ops = {
5351373e77bSGary Bisson 	.prepare = m41t80_sqw_prepare,
5361373e77bSGary Bisson 	.unprepare = m41t80_sqw_unprepare,
5371373e77bSGary Bisson 	.is_prepared = m41t80_sqw_is_prepared,
5381373e77bSGary Bisson 	.recalc_rate = m41t80_sqw_recalc_rate,
5391373e77bSGary Bisson 	.round_rate = m41t80_sqw_round_rate,
5401373e77bSGary Bisson 	.set_rate = m41t80_sqw_set_rate,
5411373e77bSGary Bisson };
5421373e77bSGary Bisson 
m41t80_sqw_register_clk(struct m41t80_data * m41t80)5431373e77bSGary Bisson static struct clk *m41t80_sqw_register_clk(struct m41t80_data *m41t80)
5441373e77bSGary Bisson {
5451373e77bSGary Bisson 	struct i2c_client *client = m41t80->client;
5461373e77bSGary Bisson 	struct device_node *node = client->dev.of_node;
547f765e349SSebastian Reichel 	struct device_node *fixed_clock;
5481373e77bSGary Bisson 	struct clk *clk;
5491373e77bSGary Bisson 	struct clk_init_data init;
5501373e77bSGary Bisson 	int ret;
5511373e77bSGary Bisson 
552f765e349SSebastian Reichel 	fixed_clock = of_get_child_by_name(node, "clock");
553f765e349SSebastian Reichel 	if (fixed_clock) {
554f765e349SSebastian Reichel 		/*
555f765e349SSebastian Reichel 		 * skip registering square wave clock when a fixed
556f765e349SSebastian Reichel 		 * clock has been registered. The fixed clock is
557f765e349SSebastian Reichel 		 * registered automatically when being referenced.
558f765e349SSebastian Reichel 		 */
559f765e349SSebastian Reichel 		of_node_put(fixed_clock);
5607caadcfaSColin Ian King 		return NULL;
561f765e349SSebastian Reichel 	}
562f765e349SSebastian Reichel 
5631373e77bSGary Bisson 	/* First disable the clock */
5641373e77bSGary Bisson 	ret = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
5651373e77bSGary Bisson 	if (ret < 0)
5661373e77bSGary Bisson 		return ERR_PTR(ret);
5671373e77bSGary Bisson 	ret = i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
5681373e77bSGary Bisson 					ret & ~(M41T80_ALMON_SQWE));
5691373e77bSGary Bisson 	if (ret < 0)
5701373e77bSGary Bisson 		return ERR_PTR(ret);
5711373e77bSGary Bisson 
5721373e77bSGary Bisson 	init.name = "m41t80-sqw";
5731373e77bSGary Bisson 	init.ops = &m41t80_sqw_ops;
5741373e77bSGary Bisson 	init.flags = 0;
5751373e77bSGary Bisson 	init.parent_names = NULL;
5761373e77bSGary Bisson 	init.num_parents = 0;
5771373e77bSGary Bisson 	m41t80->sqw.init = &init;
5782cb90ed3STroy Kisky 	m41t80->freq = m41t80_get_freq(m41t80);
5791373e77bSGary Bisson 
5801373e77bSGary Bisson 	/* optional override of the clockname */
5811373e77bSGary Bisson 	of_property_read_string(node, "clock-output-names", &init.name);
5821373e77bSGary Bisson 
5831373e77bSGary Bisson 	/* register the clock */
5841373e77bSGary Bisson 	clk = clk_register(&client->dev, &m41t80->sqw);
5851373e77bSGary Bisson 	if (!IS_ERR(clk))
5861373e77bSGary Bisson 		of_clk_add_provider(node, of_clk_src_simple_get, clk);
5871373e77bSGary Bisson 
5881373e77bSGary Bisson 	return clk;
5891373e77bSGary Bisson }
5901373e77bSGary Bisson #endif
5911373e77bSGary Bisson 
592617780d2SAtsushi Nemoto #ifdef CONFIG_RTC_DRV_M41T80_WDT
593617780d2SAtsushi Nemoto /*
594617780d2SAtsushi Nemoto  *****************************************************************************
595617780d2SAtsushi Nemoto  *
596617780d2SAtsushi Nemoto  * Watchdog Driver
597617780d2SAtsushi Nemoto  *
598617780d2SAtsushi Nemoto  *****************************************************************************
599617780d2SAtsushi Nemoto  */
60076384f31SSebastian Andrzej Siewior static DEFINE_MUTEX(m41t80_rtc_mutex);
601617780d2SAtsushi Nemoto static struct i2c_client *save_client;
602617780d2SAtsushi Nemoto 
603617780d2SAtsushi Nemoto /* Default margin */
604617780d2SAtsushi Nemoto #define WD_TIMO 60		/* 1..31 seconds */
605617780d2SAtsushi Nemoto 
606617780d2SAtsushi Nemoto static int wdt_margin = WD_TIMO;
607617780d2SAtsushi Nemoto module_param(wdt_margin, int, 0);
608617780d2SAtsushi Nemoto MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 60s)");
609617780d2SAtsushi Nemoto 
610617780d2SAtsushi Nemoto static unsigned long wdt_is_open;
611617780d2SAtsushi Nemoto static int boot_flag;
612617780d2SAtsushi Nemoto 
613617780d2SAtsushi Nemoto /**
614b958da79SYang Yingliang  *	wdt_ping - Reload counter one with the watchdog timeout.
615b958da79SYang Yingliang  *	We don't bother reloading the cascade counter.
616617780d2SAtsushi Nemoto  */
wdt_ping(void)617617780d2SAtsushi Nemoto static void wdt_ping(void)
618617780d2SAtsushi Nemoto {
619617780d2SAtsushi Nemoto 	unsigned char i2c_data[2];
620617780d2SAtsushi Nemoto 	struct i2c_msg msgs1[1] = {
621617780d2SAtsushi Nemoto 		{
622617780d2SAtsushi Nemoto 			.addr	= save_client->addr,
623617780d2SAtsushi Nemoto 			.flags	= 0,
624617780d2SAtsushi Nemoto 			.len	= 2,
625617780d2SAtsushi Nemoto 			.buf	= i2c_data,
626617780d2SAtsushi Nemoto 		},
627617780d2SAtsushi Nemoto 	};
628d3a126fcSSteven A. Falco 	struct m41t80_data *clientdata = i2c_get_clientdata(save_client);
629d3a126fcSSteven A. Falco 
630617780d2SAtsushi Nemoto 	i2c_data[0] = 0x09;		/* watchdog register */
631617780d2SAtsushi Nemoto 
632617780d2SAtsushi Nemoto 	if (wdt_margin > 31)
633617780d2SAtsushi Nemoto 		i2c_data[1] = (wdt_margin & 0xFC) | 0x83; /* resolution = 4s */
634617780d2SAtsushi Nemoto 	else
635617780d2SAtsushi Nemoto 		/*
636617780d2SAtsushi Nemoto 		 * WDS = 1 (0x80), mulitplier = WD_TIMO, resolution = 1s (0x02)
637617780d2SAtsushi Nemoto 		 */
638617780d2SAtsushi Nemoto 		i2c_data[1] = wdt_margin << 2 | 0x82;
639617780d2SAtsushi Nemoto 
640d3a126fcSSteven A. Falco 	/*
641d3a126fcSSteven A. Falco 	 * M41T65 has three bits for watchdog resolution.  Don't set bit 7, as
642d3a126fcSSteven A. Falco 	 * that would be an invalid resolution.
643d3a126fcSSteven A. Falco 	 */
644d3a126fcSSteven A. Falco 	if (clientdata->features & M41T80_FEATURE_WD)
645d3a126fcSSteven A. Falco 		i2c_data[1] &= ~M41T80_WATCHDOG_RB2;
646d3a126fcSSteven A. Falco 
647617780d2SAtsushi Nemoto 	i2c_transfer(save_client->adapter, msgs1, 1);
648617780d2SAtsushi Nemoto }
649617780d2SAtsushi Nemoto 
650617780d2SAtsushi Nemoto /**
651b958da79SYang Yingliang  *	wdt_disable - disables watchdog.
652617780d2SAtsushi Nemoto  */
wdt_disable(void)653617780d2SAtsushi Nemoto static void wdt_disable(void)
654617780d2SAtsushi Nemoto {
655617780d2SAtsushi Nemoto 	unsigned char i2c_data[2], i2c_buf[0x10];
656617780d2SAtsushi Nemoto 	struct i2c_msg msgs0[2] = {
657617780d2SAtsushi Nemoto 		{
658617780d2SAtsushi Nemoto 			.addr	= save_client->addr,
659617780d2SAtsushi Nemoto 			.flags	= 0,
660617780d2SAtsushi Nemoto 			.len	= 1,
661617780d2SAtsushi Nemoto 			.buf	= i2c_data,
662617780d2SAtsushi Nemoto 		},
663617780d2SAtsushi Nemoto 		{
664617780d2SAtsushi Nemoto 			.addr	= save_client->addr,
665617780d2SAtsushi Nemoto 			.flags	= I2C_M_RD,
666617780d2SAtsushi Nemoto 			.len	= 1,
667617780d2SAtsushi Nemoto 			.buf	= i2c_buf,
668617780d2SAtsushi Nemoto 		},
669617780d2SAtsushi Nemoto 	};
670617780d2SAtsushi Nemoto 	struct i2c_msg msgs1[1] = {
671617780d2SAtsushi Nemoto 		{
672617780d2SAtsushi Nemoto 			.addr	= save_client->addr,
673617780d2SAtsushi Nemoto 			.flags	= 0,
674617780d2SAtsushi Nemoto 			.len	= 2,
675617780d2SAtsushi Nemoto 			.buf	= i2c_data,
676617780d2SAtsushi Nemoto 		},
677617780d2SAtsushi Nemoto 	};
678617780d2SAtsushi Nemoto 
679617780d2SAtsushi Nemoto 	i2c_data[0] = 0x09;
680617780d2SAtsushi Nemoto 	i2c_transfer(save_client->adapter, msgs0, 2);
681617780d2SAtsushi Nemoto 
682617780d2SAtsushi Nemoto 	i2c_data[0] = 0x09;
683617780d2SAtsushi Nemoto 	i2c_data[1] = 0x00;
684617780d2SAtsushi Nemoto 	i2c_transfer(save_client->adapter, msgs1, 1);
685617780d2SAtsushi Nemoto }
686617780d2SAtsushi Nemoto 
687617780d2SAtsushi Nemoto /**
688b958da79SYang Yingliang  *	wdt_write - write to watchdog.
689617780d2SAtsushi Nemoto  *	@file: file handle to the watchdog
690617780d2SAtsushi Nemoto  *	@buf: buffer to write (unused as data does not matter here
691617780d2SAtsushi Nemoto  *	@count: count of bytes
692617780d2SAtsushi Nemoto  *	@ppos: pointer to the position to write. No seeks allowed
693617780d2SAtsushi Nemoto  *
694617780d2SAtsushi Nemoto  *	A write to a watchdog device is defined as a keepalive signal. Any
695b9354487Sshaomin Deng  *	write of data will do, as we don't define content meaning.
696617780d2SAtsushi Nemoto  */
wdt_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)697617780d2SAtsushi Nemoto static ssize_t wdt_write(struct file *file, const char __user *buf,
698617780d2SAtsushi Nemoto 			 size_t count, loff_t *ppos)
699617780d2SAtsushi Nemoto {
700617780d2SAtsushi Nemoto 	if (count) {
701617780d2SAtsushi Nemoto 		wdt_ping();
702617780d2SAtsushi Nemoto 		return 1;
703617780d2SAtsushi Nemoto 	}
704617780d2SAtsushi Nemoto 	return 0;
705617780d2SAtsushi Nemoto }
706617780d2SAtsushi Nemoto 
wdt_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)707617780d2SAtsushi Nemoto static ssize_t wdt_read(struct file *file, char __user *buf,
708617780d2SAtsushi Nemoto 			size_t count, loff_t *ppos)
709617780d2SAtsushi Nemoto {
710617780d2SAtsushi Nemoto 	return 0;
711617780d2SAtsushi Nemoto }
712617780d2SAtsushi Nemoto 
713617780d2SAtsushi Nemoto /**
714b958da79SYang Yingliang  *	wdt_ioctl - ioctl handler to set watchdog.
715617780d2SAtsushi Nemoto  *	@file: file handle to the device
716617780d2SAtsushi Nemoto  *	@cmd: watchdog command
717617780d2SAtsushi Nemoto  *	@arg: argument pointer
718617780d2SAtsushi Nemoto  *
719617780d2SAtsushi Nemoto  *	The watchdog API defines a common set of functions for all watchdogs
720617780d2SAtsushi Nemoto  *	according to their available features. We only actually usefully support
721617780d2SAtsushi Nemoto  *	querying capabilities and current status.
722617780d2SAtsushi Nemoto  */
wdt_ioctl(struct file * file,unsigned int cmd,unsigned long arg)72355929332SArnd Bergmann static int wdt_ioctl(struct file *file, unsigned int cmd,
724617780d2SAtsushi Nemoto 		     unsigned long arg)
725617780d2SAtsushi Nemoto {
726617780d2SAtsushi Nemoto 	int new_margin, rv;
727617780d2SAtsushi Nemoto 	static struct watchdog_info ident = {
728617780d2SAtsushi Nemoto 		.options = WDIOF_POWERUNDER | WDIOF_KEEPALIVEPING |
729617780d2SAtsushi Nemoto 			WDIOF_SETTIMEOUT,
730617780d2SAtsushi Nemoto 		.firmware_version = 1,
731617780d2SAtsushi Nemoto 		.identity = "M41T80 WTD"
732617780d2SAtsushi Nemoto 	};
733617780d2SAtsushi Nemoto 
734617780d2SAtsushi Nemoto 	switch (cmd) {
735617780d2SAtsushi Nemoto 	case WDIOC_GETSUPPORT:
736617780d2SAtsushi Nemoto 		return copy_to_user((struct watchdog_info __user *)arg, &ident,
737617780d2SAtsushi Nemoto 				    sizeof(ident)) ? -EFAULT : 0;
738617780d2SAtsushi Nemoto 
739617780d2SAtsushi Nemoto 	case WDIOC_GETSTATUS:
740617780d2SAtsushi Nemoto 	case WDIOC_GETBOOTSTATUS:
741617780d2SAtsushi Nemoto 		return put_user(boot_flag, (int __user *)arg);
742617780d2SAtsushi Nemoto 	case WDIOC_KEEPALIVE:
743617780d2SAtsushi Nemoto 		wdt_ping();
744617780d2SAtsushi Nemoto 		return 0;
745617780d2SAtsushi Nemoto 	case WDIOC_SETTIMEOUT:
746617780d2SAtsushi Nemoto 		if (get_user(new_margin, (int __user *)arg))
747617780d2SAtsushi Nemoto 			return -EFAULT;
748617780d2SAtsushi Nemoto 		/* Arbitrary, can't find the card's limits */
749617780d2SAtsushi Nemoto 		if (new_margin < 1 || new_margin > 124)
750617780d2SAtsushi Nemoto 			return -EINVAL;
751617780d2SAtsushi Nemoto 		wdt_margin = new_margin;
752617780d2SAtsushi Nemoto 		wdt_ping();
753df561f66SGustavo A. R. Silva 		fallthrough;
754617780d2SAtsushi Nemoto 	case WDIOC_GETTIMEOUT:
755617780d2SAtsushi Nemoto 		return put_user(wdt_margin, (int __user *)arg);
756617780d2SAtsushi Nemoto 
757617780d2SAtsushi Nemoto 	case WDIOC_SETOPTIONS:
758617780d2SAtsushi Nemoto 		if (copy_from_user(&rv, (int __user *)arg, sizeof(int)))
759617780d2SAtsushi Nemoto 			return -EFAULT;
760617780d2SAtsushi Nemoto 
761617780d2SAtsushi Nemoto 		if (rv & WDIOS_DISABLECARD) {
762a737e835SJoe Perches 			pr_info("disable watchdog\n");
763617780d2SAtsushi Nemoto 			wdt_disable();
764617780d2SAtsushi Nemoto 		}
765617780d2SAtsushi Nemoto 
766617780d2SAtsushi Nemoto 		if (rv & WDIOS_ENABLECARD) {
767a737e835SJoe Perches 			pr_info("enable watchdog\n");
768617780d2SAtsushi Nemoto 			wdt_ping();
769617780d2SAtsushi Nemoto 		}
770617780d2SAtsushi Nemoto 
771617780d2SAtsushi Nemoto 		return -EINVAL;
772617780d2SAtsushi Nemoto 	}
773617780d2SAtsushi Nemoto 	return -ENOTTY;
774617780d2SAtsushi Nemoto }
775617780d2SAtsushi Nemoto 
wdt_unlocked_ioctl(struct file * file,unsigned int cmd,unsigned long arg)77655929332SArnd Bergmann static long wdt_unlocked_ioctl(struct file *file, unsigned int cmd,
77755929332SArnd Bergmann 			       unsigned long arg)
77855929332SArnd Bergmann {
77955929332SArnd Bergmann 	int ret;
78055929332SArnd Bergmann 
781613655faSArnd Bergmann 	mutex_lock(&m41t80_rtc_mutex);
78255929332SArnd Bergmann 	ret = wdt_ioctl(file, cmd, arg);
783613655faSArnd Bergmann 	mutex_unlock(&m41t80_rtc_mutex);
78455929332SArnd Bergmann 
78555929332SArnd Bergmann 	return ret;
78655929332SArnd Bergmann }
78755929332SArnd Bergmann 
788617780d2SAtsushi Nemoto /**
789b958da79SYang Yingliang  *	wdt_open - open a watchdog.
790617780d2SAtsushi Nemoto  *	@inode: inode of device
791617780d2SAtsushi Nemoto  *	@file: file handle to device
792617780d2SAtsushi Nemoto  *
793617780d2SAtsushi Nemoto  */
wdt_open(struct inode * inode,struct file * file)794617780d2SAtsushi Nemoto static int wdt_open(struct inode *inode, struct file *file)
795617780d2SAtsushi Nemoto {
7966f24784fSAl Viro 	if (iminor(inode) == WATCHDOG_MINOR) {
797613655faSArnd Bergmann 		mutex_lock(&m41t80_rtc_mutex);
79841012735SArnd Bergmann 		if (test_and_set_bit(0, &wdt_is_open)) {
799613655faSArnd Bergmann 			mutex_unlock(&m41t80_rtc_mutex);
800617780d2SAtsushi Nemoto 			return -EBUSY;
80141012735SArnd Bergmann 		}
802617780d2SAtsushi Nemoto 		/*
803617780d2SAtsushi Nemoto 		 *	Activate
804617780d2SAtsushi Nemoto 		 */
805617780d2SAtsushi Nemoto 		wdt_is_open = 1;
806613655faSArnd Bergmann 		mutex_unlock(&m41t80_rtc_mutex);
807c5bf68feSKirill Smelkov 		return stream_open(inode, file);
808617780d2SAtsushi Nemoto 	}
809617780d2SAtsushi Nemoto 	return -ENODEV;
810617780d2SAtsushi Nemoto }
811617780d2SAtsushi Nemoto 
812617780d2SAtsushi Nemoto /**
813b958da79SYang Yingliang  *	wdt_release - release a watchdog.
814617780d2SAtsushi Nemoto  *	@inode: inode to board
815617780d2SAtsushi Nemoto  *	@file: file handle to board
816617780d2SAtsushi Nemoto  *
817617780d2SAtsushi Nemoto  */
wdt_release(struct inode * inode,struct file * file)818617780d2SAtsushi Nemoto static int wdt_release(struct inode *inode, struct file *file)
819617780d2SAtsushi Nemoto {
8206f24784fSAl Viro 	if (iminor(inode) == WATCHDOG_MINOR)
821617780d2SAtsushi Nemoto 		clear_bit(0, &wdt_is_open);
822617780d2SAtsushi Nemoto 	return 0;
823617780d2SAtsushi Nemoto }
824617780d2SAtsushi Nemoto 
825617780d2SAtsushi Nemoto /**
826b958da79SYang Yingliang  *	wdt_notify_sys - notify to watchdog.
827617780d2SAtsushi Nemoto  *	@this: our notifier block
828617780d2SAtsushi Nemoto  *	@code: the event being reported
829617780d2SAtsushi Nemoto  *	@unused: unused
830617780d2SAtsushi Nemoto  *
831617780d2SAtsushi Nemoto  *	Our notifier is called on system shutdowns. We want to turn the card
832617780d2SAtsushi Nemoto  *	off at reboot otherwise the machine will reboot again during memory
833617780d2SAtsushi Nemoto  *	test or worse yet during the following fsck. This would suck, in fact
834617780d2SAtsushi Nemoto  *	trust me - if it happens it does suck.
835617780d2SAtsushi Nemoto  */
wdt_notify_sys(struct notifier_block * this,unsigned long code,void * unused)836617780d2SAtsushi Nemoto static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
837617780d2SAtsushi Nemoto 			  void *unused)
838617780d2SAtsushi Nemoto {
839617780d2SAtsushi Nemoto 	if (code == SYS_DOWN || code == SYS_HALT)
840617780d2SAtsushi Nemoto 		/* Disable Watchdog */
841617780d2SAtsushi Nemoto 		wdt_disable();
842617780d2SAtsushi Nemoto 	return NOTIFY_DONE;
843617780d2SAtsushi Nemoto }
844617780d2SAtsushi Nemoto 
845617780d2SAtsushi Nemoto static const struct file_operations wdt_fops = {
846617780d2SAtsushi Nemoto 	.owner	= THIS_MODULE,
847617780d2SAtsushi Nemoto 	.read	= wdt_read,
84855929332SArnd Bergmann 	.unlocked_ioctl = wdt_unlocked_ioctl,
849b6dfb247SArnd Bergmann 	.compat_ioctl = compat_ptr_ioctl,
850617780d2SAtsushi Nemoto 	.write	= wdt_write,
851617780d2SAtsushi Nemoto 	.open	= wdt_open,
852617780d2SAtsushi Nemoto 	.release = wdt_release,
8536038f373SArnd Bergmann 	.llseek = no_llseek,
854617780d2SAtsushi Nemoto };
855617780d2SAtsushi Nemoto 
856617780d2SAtsushi Nemoto static struct miscdevice wdt_dev = {
857617780d2SAtsushi Nemoto 	.minor = WATCHDOG_MINOR,
858617780d2SAtsushi Nemoto 	.name = "watchdog",
859617780d2SAtsushi Nemoto 	.fops = &wdt_fops,
860617780d2SAtsushi Nemoto };
861617780d2SAtsushi Nemoto 
862617780d2SAtsushi Nemoto /*
863617780d2SAtsushi Nemoto  *	The WDT card needs to learn about soft shutdowns in order to
864617780d2SAtsushi Nemoto  *	turn the timebomb registers off.
865617780d2SAtsushi Nemoto  */
866617780d2SAtsushi Nemoto static struct notifier_block wdt_notifier = {
867617780d2SAtsushi Nemoto 	.notifier_call = wdt_notify_sys,
868617780d2SAtsushi Nemoto };
869617780d2SAtsushi Nemoto #endif /* CONFIG_RTC_DRV_M41T80_WDT */
870617780d2SAtsushi Nemoto 
871caaff562SAtsushi Nemoto /*
872caaff562SAtsushi Nemoto  *****************************************************************************
873caaff562SAtsushi Nemoto  *
874caaff562SAtsushi Nemoto  *	Driver Interface
875caaff562SAtsushi Nemoto  *
876caaff562SAtsushi Nemoto  *****************************************************************************
877caaff562SAtsushi Nemoto  */
878ef6b3125SMylène Josserand 
m41t80_probe(struct i2c_client * client)87967db6f05SUwe Kleine-König static int m41t80_probe(struct i2c_client *client)
880caaff562SAtsushi Nemoto {
881e5108df4SWolfram Sang 	struct i2c_adapter *adapter = client->adapter;
8823760f736SJean Delvare 	int rc = 0;
883caaff562SAtsushi Nemoto 	struct rtc_time tm;
8849c6dfed9SMylène Josserand 	struct m41t80_data *m41t80_data = NULL;
885d4473b9bSEric Cooper 	bool wakeup_source = false;
886caaff562SAtsushi Nemoto 
887f2b84ee8SMylène Josserand 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK |
888f2b84ee8SMylène Josserand 				     I2C_FUNC_SMBUS_BYTE_DATA)) {
889f2b84ee8SMylène Josserand 		dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
890c67fedfaSWolfram Sang 		return -ENODEV;
891f2b84ee8SMylène Josserand 	}
892caaff562SAtsushi Nemoto 
8939c6dfed9SMylène Josserand 	m41t80_data = devm_kzalloc(&client->dev, sizeof(*m41t80_data),
8944ebabb78SJingoo Han 				   GFP_KERNEL);
8959c6dfed9SMylène Josserand 	if (!m41t80_data)
896c67fedfaSWolfram Sang 		return -ENOMEM;
897caaff562SAtsushi Nemoto 
8981373e77bSGary Bisson 	m41t80_data->client = client;
89967db6f05SUwe Kleine-König 	if (client->dev.of_node) {
900eb235c56SJavier Martinez Canillas 		m41t80_data->features = (unsigned long)
901eb235c56SJavier Martinez Canillas 			of_device_get_match_data(&client->dev);
90267db6f05SUwe Kleine-König 	} else {
90367db6f05SUwe Kleine-König 		const struct i2c_device_id *id = i2c_match_id(m41t80_id, client);
9049c6dfed9SMylène Josserand 		m41t80_data->features = id->driver_data;
90567db6f05SUwe Kleine-König 	}
9069c6dfed9SMylène Josserand 	i2c_set_clientdata(client, m41t80_data);
9079c6dfed9SMylène Josserand 
90810d0c768SAlexandre Belloni 	m41t80_data->rtc =  devm_rtc_allocate_device(&client->dev);
90910d0c768SAlexandre Belloni 	if (IS_ERR(m41t80_data->rtc))
91010d0c768SAlexandre Belloni 		return PTR_ERR(m41t80_data->rtc);
91110d0c768SAlexandre Belloni 
912d4473b9bSEric Cooper #ifdef CONFIG_OF
913d4473b9bSEric Cooper 	wakeup_source = of_property_read_bool(client->dev.of_node,
914d4473b9bSEric Cooper 					      "wakeup-source");
915d4473b9bSEric Cooper #endif
9169c6dfed9SMylène Josserand 	if (client->irq > 0) {
917f181987eSAlexandre Belloni 		unsigned long irqflags = IRQF_TRIGGER_LOW;
918f181987eSAlexandre Belloni 
919f181987eSAlexandre Belloni 		if (dev_fwnode(&client->dev))
920f181987eSAlexandre Belloni 			irqflags = 0;
921f181987eSAlexandre Belloni 
9229c6dfed9SMylène Josserand 		rc = devm_request_threaded_irq(&client->dev, client->irq,
9239c6dfed9SMylène Josserand 					       NULL, m41t80_handle_irq,
924f181987eSAlexandre Belloni 					       irqflags | IRQF_ONESHOT,
9259c6dfed9SMylène Josserand 					       "m41t80", client);
9269c6dfed9SMylène Josserand 		if (rc) {
9279c6dfed9SMylène Josserand 			dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
9289c6dfed9SMylène Josserand 			client->irq = 0;
929d4473b9bSEric Cooper 			wakeup_source = false;
930d4473b9bSEric Cooper 		}
931d4473b9bSEric Cooper 	}
9323948a866SAlexandre Belloni 	if (client->irq > 0 || wakeup_source)
9333726a218SMylène Josserand 		device_init_wakeup(&client->dev, true);
9343948a866SAlexandre Belloni 	else
9353948a866SAlexandre Belloni 		clear_bit(RTC_FEATURE_ALARM, m41t80_data->rtc->features);
936a015dbc1SJohn Stultz 
93710d0c768SAlexandre Belloni 	m41t80_data->rtc->ops = &m41t80_rtc_ops;
938cf79e7c3SAlexandre Belloni 	m41t80_data->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
939cf79e7c3SAlexandre Belloni 	m41t80_data->rtc->range_max = RTC_TIMESTAMP_END_2099;
940caaff562SAtsushi Nemoto 
941ba39374bSAlexandre Belloni 	if (client->irq <= 0)
942ba39374bSAlexandre Belloni 		clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, m41t80_data->rtc->features);
943caaff562SAtsushi Nemoto 
944caaff562SAtsushi Nemoto 	/* Make sure HT (Halt Update) bit is cleared */
945caaff562SAtsushi Nemoto 	rc = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_HOUR);
946caaff562SAtsushi Nemoto 
947c67fedfaSWolfram Sang 	if (rc >= 0 && rc & M41T80_ALHOUR_HT) {
9489c6dfed9SMylène Josserand 		if (m41t80_data->features & M41T80_FEATURE_HT) {
949e2c8e1a9SAlexandre Belloni 			m41t80_rtc_read_time(&client->dev, &tm);
950caaff562SAtsushi Nemoto 			dev_info(&client->dev, "HT bit was set!\n");
95122b844aeSAndy Shevchenko 			dev_info(&client->dev, "Power Down at %ptR\n", &tm);
952caaff562SAtsushi Nemoto 		}
953c67fedfaSWolfram Sang 		rc = i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_HOUR,
954c67fedfaSWolfram Sang 					       rc & ~M41T80_ALHOUR_HT);
955c67fedfaSWolfram Sang 	}
956c67fedfaSWolfram Sang 
957c67fedfaSWolfram Sang 	if (rc < 0) {
958c67fedfaSWolfram Sang 		dev_err(&client->dev, "Can't clear HT bit\n");
95985d77047SWolfram Sang 		return rc;
960caaff562SAtsushi Nemoto 	}
961caaff562SAtsushi Nemoto 
962caaff562SAtsushi Nemoto 	/* Make sure ST (stop) bit is cleared */
963caaff562SAtsushi Nemoto 	rc = i2c_smbus_read_byte_data(client, M41T80_REG_SEC);
964caaff562SAtsushi Nemoto 
965c67fedfaSWolfram Sang 	if (rc >= 0 && rc & M41T80_SEC_ST)
966c67fedfaSWolfram Sang 		rc = i2c_smbus_write_byte_data(client, M41T80_REG_SEC,
967c67fedfaSWolfram Sang 					       rc & ~M41T80_SEC_ST);
968c67fedfaSWolfram Sang 	if (rc < 0) {
969c67fedfaSWolfram Sang 		dev_err(&client->dev, "Can't clear ST bit\n");
97085d77047SWolfram Sang 		return rc;
971caaff562SAtsushi Nemoto 	}
972caaff562SAtsushi Nemoto 
973617780d2SAtsushi Nemoto #ifdef CONFIG_RTC_DRV_M41T80_WDT
9749c6dfed9SMylène Josserand 	if (m41t80_data->features & M41T80_FEATURE_HT) {
975417607d0SMaciej W. Rozycki 		save_client = client;
976617780d2SAtsushi Nemoto 		rc = misc_register(&wdt_dev);
977617780d2SAtsushi Nemoto 		if (rc)
978c67fedfaSWolfram Sang 			return rc;
979617780d2SAtsushi Nemoto 		rc = register_reboot_notifier(&wdt_notifier);
980617780d2SAtsushi Nemoto 		if (rc) {
981617780d2SAtsushi Nemoto 			misc_deregister(&wdt_dev);
982c67fedfaSWolfram Sang 			return rc;
983617780d2SAtsushi Nemoto 		}
984617780d2SAtsushi Nemoto 	}
985617780d2SAtsushi Nemoto #endif
9861373e77bSGary Bisson #ifdef CONFIG_COMMON_CLK
9871373e77bSGary Bisson 	if (m41t80_data->features & M41T80_FEATURE_SQ)
9881373e77bSGary Bisson 		m41t80_sqw_register_clk(m41t80_data);
9891373e77bSGary Bisson #endif
99010d0c768SAlexandre Belloni 
991fdcfd854SBartosz Golaszewski 	rc = devm_rtc_register_device(m41t80_data->rtc);
99210d0c768SAlexandre Belloni 	if (rc)
99310d0c768SAlexandre Belloni 		return rc;
99410d0c768SAlexandre Belloni 
995caaff562SAtsushi Nemoto 	return 0;
996caaff562SAtsushi Nemoto }
997caaff562SAtsushi Nemoto 
m41t80_remove(struct i2c_client * client)998ed5c2f5fSUwe Kleine-König static void m41t80_remove(struct i2c_client *client)
999caaff562SAtsushi Nemoto {
1000617780d2SAtsushi Nemoto #ifdef CONFIG_RTC_DRV_M41T80_WDT
10014ebabb78SJingoo Han 	struct m41t80_data *clientdata = i2c_get_clientdata(client);
10024ebabb78SJingoo Han 
10033760f736SJean Delvare 	if (clientdata->features & M41T80_FEATURE_HT) {
1004617780d2SAtsushi Nemoto 		misc_deregister(&wdt_dev);
1005617780d2SAtsushi Nemoto 		unregister_reboot_notifier(&wdt_notifier);
1006617780d2SAtsushi Nemoto 	}
1007617780d2SAtsushi Nemoto #endif
1008caaff562SAtsushi Nemoto }
1009caaff562SAtsushi Nemoto 
1010caaff562SAtsushi Nemoto static struct i2c_driver m41t80_driver = {
1011caaff562SAtsushi Nemoto 	.driver = {
1012afe1ab4dSDavid Brownell 		.name = "rtc-m41t80",
1013eb235c56SJavier Martinez Canillas 		.of_match_table = of_match_ptr(m41t80_of_match),
1014ae036af8SStefan Christ 		.pm = &m41t80_pm,
1015caaff562SAtsushi Nemoto 	},
101631b0cecbSUwe Kleine-König 	.probe = m41t80_probe,
1017caaff562SAtsushi Nemoto 	.remove = m41t80_remove,
10183760f736SJean Delvare 	.id_table = m41t80_id,
1019caaff562SAtsushi Nemoto };
1020caaff562SAtsushi Nemoto 
10210abc9201SAxel Lin module_i2c_driver(m41t80_driver);
1022caaff562SAtsushi Nemoto 
1023caaff562SAtsushi Nemoto MODULE_AUTHOR("Alexander Bigga <ab@mycable.de>");
1024caaff562SAtsushi Nemoto MODULE_DESCRIPTION("ST Microelectronics M41T80 series RTC I2C Client Driver");
1025caaff562SAtsushi Nemoto MODULE_LICENSE("GPL");
1026