xref: /openbmc/linux/drivers/rtc/rtc-m41t80.c (revision f15cbe6f1a4b4d9df59142fc8e4abb973302cf44)
1 /*
2  * I2C client/driver for the ST M41T80 family of i2c rtc chips.
3  *
4  * Author: Alexander Bigga <ab@mycable.de>
5  *
6  * Based on m41t00.c by Mark A. Greer <mgreer@mvista.com>
7  *
8  * 2006 (c) mycable GmbH
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  */
15 
16 #include <linux/bcd.h>
17 #include <linux/i2c.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/rtc.h>
22 #include <linux/slab.h>
23 #include <linux/smp_lock.h>
24 #include <linux/string.h>
25 #ifdef CONFIG_RTC_DRV_M41T80_WDT
26 #include <linux/fs.h>
27 #include <linux/ioctl.h>
28 #include <linux/miscdevice.h>
29 #include <linux/reboot.h>
30 #include <linux/watchdog.h>
31 #endif
32 
33 #define M41T80_REG_SSEC	0
34 #define M41T80_REG_SEC	1
35 #define M41T80_REG_MIN	2
36 #define M41T80_REG_HOUR	3
37 #define M41T80_REG_WDAY	4
38 #define M41T80_REG_DAY	5
39 #define M41T80_REG_MON	6
40 #define M41T80_REG_YEAR	7
41 #define M41T80_REG_ALARM_MON	0xa
42 #define M41T80_REG_ALARM_DAY	0xb
43 #define M41T80_REG_ALARM_HOUR	0xc
44 #define M41T80_REG_ALARM_MIN	0xd
45 #define M41T80_REG_ALARM_SEC	0xe
46 #define M41T80_REG_FLAGS	0xf
47 #define M41T80_REG_SQW	0x13
48 
49 #define M41T80_DATETIME_REG_SIZE	(M41T80_REG_YEAR + 1)
50 #define M41T80_ALARM_REG_SIZE	\
51 	(M41T80_REG_ALARM_SEC + 1 - M41T80_REG_ALARM_MON)
52 
53 #define M41T80_SEC_ST		(1 << 7)	/* ST: Stop Bit */
54 #define M41T80_ALMON_AFE	(1 << 7)	/* AFE: AF Enable Bit */
55 #define M41T80_ALMON_SQWE	(1 << 6)	/* SQWE: SQW Enable Bit */
56 #define M41T80_ALHOUR_HT	(1 << 6)	/* HT: Halt Update Bit */
57 #define M41T80_FLAGS_AF		(1 << 6)	/* AF: Alarm Flag Bit */
58 #define M41T80_FLAGS_BATT_LOW	(1 << 4)	/* BL: Battery Low Bit */
59 
60 #define M41T80_FEATURE_HT	(1 << 0)
61 #define M41T80_FEATURE_BL	(1 << 1)
62 
63 #define DRV_VERSION "0.05"
64 
65 static const struct i2c_device_id m41t80_id[] = {
66 	{ "m41t80", 0 },
67 	{ "m41t81", M41T80_FEATURE_HT },
68 	{ "m41t81s", M41T80_FEATURE_HT | M41T80_FEATURE_BL },
69 	{ "m41t82", M41T80_FEATURE_HT | M41T80_FEATURE_BL },
70 	{ "m41t83", M41T80_FEATURE_HT | M41T80_FEATURE_BL },
71 	{ "m41st84", M41T80_FEATURE_HT | M41T80_FEATURE_BL },
72 	{ "m41st85", M41T80_FEATURE_HT | M41T80_FEATURE_BL },
73 	{ "m41st87", M41T80_FEATURE_HT | M41T80_FEATURE_BL },
74 	{ }
75 };
76 MODULE_DEVICE_TABLE(i2c, m41t80_id);
77 
78 struct m41t80_data {
79 	u8 features;
80 	struct rtc_device *rtc;
81 };
82 
83 static int m41t80_get_datetime(struct i2c_client *client,
84 			       struct rtc_time *tm)
85 {
86 	u8 buf[M41T80_DATETIME_REG_SIZE], dt_addr[1] = { M41T80_REG_SEC };
87 	struct i2c_msg msgs[] = {
88 		{
89 			.addr	= client->addr,
90 			.flags	= 0,
91 			.len	= 1,
92 			.buf	= dt_addr,
93 		},
94 		{
95 			.addr	= client->addr,
96 			.flags	= I2C_M_RD,
97 			.len	= M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC,
98 			.buf	= buf + M41T80_REG_SEC,
99 		},
100 	};
101 
102 	if (i2c_transfer(client->adapter, msgs, 2) < 0) {
103 		dev_err(&client->dev, "read error\n");
104 		return -EIO;
105 	}
106 
107 	tm->tm_sec = BCD2BIN(buf[M41T80_REG_SEC] & 0x7f);
108 	tm->tm_min = BCD2BIN(buf[M41T80_REG_MIN] & 0x7f);
109 	tm->tm_hour = BCD2BIN(buf[M41T80_REG_HOUR] & 0x3f);
110 	tm->tm_mday = BCD2BIN(buf[M41T80_REG_DAY] & 0x3f);
111 	tm->tm_wday = buf[M41T80_REG_WDAY] & 0x07;
112 	tm->tm_mon = BCD2BIN(buf[M41T80_REG_MON] & 0x1f) - 1;
113 
114 	/* assume 20YY not 19YY, and ignore the Century Bit */
115 	tm->tm_year = BCD2BIN(buf[M41T80_REG_YEAR]) + 100;
116 	return 0;
117 }
118 
119 /* Sets the given date and time to the real time clock. */
120 static int m41t80_set_datetime(struct i2c_client *client, struct rtc_time *tm)
121 {
122 	u8 wbuf[1 + M41T80_DATETIME_REG_SIZE];
123 	u8 *buf = &wbuf[1];
124 	u8 dt_addr[1] = { M41T80_REG_SEC };
125 	struct i2c_msg msgs_in[] = {
126 		{
127 			.addr	= client->addr,
128 			.flags	= 0,
129 			.len	= 1,
130 			.buf	= dt_addr,
131 		},
132 		{
133 			.addr	= client->addr,
134 			.flags	= I2C_M_RD,
135 			.len	= M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC,
136 			.buf	= buf + M41T80_REG_SEC,
137 		},
138 	};
139 	struct i2c_msg msgs[] = {
140 		{
141 			.addr	= client->addr,
142 			.flags	= 0,
143 			.len	= 1 + M41T80_DATETIME_REG_SIZE,
144 			.buf	= wbuf,
145 		 },
146 	};
147 
148 	/* Read current reg values into buf[1..7] */
149 	if (i2c_transfer(client->adapter, msgs_in, 2) < 0) {
150 		dev_err(&client->dev, "read error\n");
151 		return -EIO;
152 	}
153 
154 	wbuf[0] = 0; /* offset into rtc's regs */
155 	/* Merge time-data and register flags into buf[0..7] */
156 	buf[M41T80_REG_SSEC] = 0;
157 	buf[M41T80_REG_SEC] =
158 		BIN2BCD(tm->tm_sec) | (buf[M41T80_REG_SEC] & ~0x7f);
159 	buf[M41T80_REG_MIN] =
160 		BIN2BCD(tm->tm_min) | (buf[M41T80_REG_MIN] & ~0x7f);
161 	buf[M41T80_REG_HOUR] =
162 		BIN2BCD(tm->tm_hour) | (buf[M41T80_REG_HOUR] & ~0x3f) ;
163 	buf[M41T80_REG_WDAY] =
164 		(tm->tm_wday & 0x07) | (buf[M41T80_REG_WDAY] & ~0x07);
165 	buf[M41T80_REG_DAY] =
166 		BIN2BCD(tm->tm_mday) | (buf[M41T80_REG_DAY] & ~0x3f);
167 	buf[M41T80_REG_MON] =
168 		BIN2BCD(tm->tm_mon + 1) | (buf[M41T80_REG_MON] & ~0x1f);
169 	/* assume 20YY not 19YY */
170 	buf[M41T80_REG_YEAR] = BIN2BCD(tm->tm_year % 100);
171 
172 	if (i2c_transfer(client->adapter, msgs, 1) != 1) {
173 		dev_err(&client->dev, "write error\n");
174 		return -EIO;
175 	}
176 	return 0;
177 }
178 
179 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
180 static int m41t80_rtc_proc(struct device *dev, struct seq_file *seq)
181 {
182 	struct i2c_client *client = to_i2c_client(dev);
183 	struct m41t80_data *clientdata = i2c_get_clientdata(client);
184 	u8 reg;
185 
186 	if (clientdata->features & M41T80_FEATURE_BL) {
187 		reg = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
188 		seq_printf(seq, "battery\t\t: %s\n",
189 			   (reg & M41T80_FLAGS_BATT_LOW) ? "exhausted" : "ok");
190 	}
191 	return 0;
192 }
193 #else
194 #define m41t80_rtc_proc NULL
195 #endif
196 
197 static int m41t80_rtc_read_time(struct device *dev, struct rtc_time *tm)
198 {
199 	return m41t80_get_datetime(to_i2c_client(dev), tm);
200 }
201 
202 static int m41t80_rtc_set_time(struct device *dev, struct rtc_time *tm)
203 {
204 	return m41t80_set_datetime(to_i2c_client(dev), tm);
205 }
206 
207 #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
208 static int
209 m41t80_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
210 {
211 	struct i2c_client *client = to_i2c_client(dev);
212 	int rc;
213 
214 	switch (cmd) {
215 	case RTC_AIE_OFF:
216 	case RTC_AIE_ON:
217 		break;
218 	default:
219 		return -ENOIOCTLCMD;
220 	}
221 
222 	rc = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
223 	if (rc < 0)
224 		goto err;
225 	switch (cmd) {
226 	case RTC_AIE_OFF:
227 		rc &= ~M41T80_ALMON_AFE;
228 		break;
229 	case RTC_AIE_ON:
230 		rc |= M41T80_ALMON_AFE;
231 		break;
232 	}
233 	if (i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON, rc) < 0)
234 		goto err;
235 	return 0;
236 err:
237 	return -EIO;
238 }
239 #else
240 #define	m41t80_rtc_ioctl NULL
241 #endif
242 
243 static int m41t80_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *t)
244 {
245 	struct i2c_client *client = to_i2c_client(dev);
246 	u8 wbuf[1 + M41T80_ALARM_REG_SIZE];
247 	u8 *buf = &wbuf[1];
248 	u8 *reg = buf - M41T80_REG_ALARM_MON;
249 	u8 dt_addr[1] = { M41T80_REG_ALARM_MON };
250 	struct i2c_msg msgs_in[] = {
251 		{
252 			.addr	= client->addr,
253 			.flags	= 0,
254 			.len	= 1,
255 			.buf	= dt_addr,
256 		},
257 		{
258 			.addr	= client->addr,
259 			.flags	= I2C_M_RD,
260 			.len	= M41T80_ALARM_REG_SIZE,
261 			.buf	= buf,
262 		},
263 	};
264 	struct i2c_msg msgs[] = {
265 		{
266 			.addr	= client->addr,
267 			.flags	= 0,
268 			.len	= 1 + M41T80_ALARM_REG_SIZE,
269 			.buf	= wbuf,
270 		 },
271 	};
272 
273 	if (i2c_transfer(client->adapter, msgs_in, 2) < 0) {
274 		dev_err(&client->dev, "read error\n");
275 		return -EIO;
276 	}
277 	reg[M41T80_REG_ALARM_MON] &= ~(0x1f | M41T80_ALMON_AFE);
278 	reg[M41T80_REG_ALARM_DAY] = 0;
279 	reg[M41T80_REG_ALARM_HOUR] &= ~(0x3f | 0x80);
280 	reg[M41T80_REG_ALARM_MIN] = 0;
281 	reg[M41T80_REG_ALARM_SEC] = 0;
282 
283 	wbuf[0] = M41T80_REG_ALARM_MON; /* offset into rtc's regs */
284 	reg[M41T80_REG_ALARM_SEC] |= t->time.tm_sec >= 0 ?
285 		BIN2BCD(t->time.tm_sec) : 0x80;
286 	reg[M41T80_REG_ALARM_MIN] |= t->time.tm_min >= 0 ?
287 		BIN2BCD(t->time.tm_min) : 0x80;
288 	reg[M41T80_REG_ALARM_HOUR] |= t->time.tm_hour >= 0 ?
289 		BIN2BCD(t->time.tm_hour) : 0x80;
290 	reg[M41T80_REG_ALARM_DAY] |= t->time.tm_mday >= 0 ?
291 		BIN2BCD(t->time.tm_mday) : 0x80;
292 	if (t->time.tm_mon >= 0)
293 		reg[M41T80_REG_ALARM_MON] |= BIN2BCD(t->time.tm_mon + 1);
294 	else
295 		reg[M41T80_REG_ALARM_DAY] |= 0x40;
296 
297 	if (i2c_transfer(client->adapter, msgs, 1) != 1) {
298 		dev_err(&client->dev, "write error\n");
299 		return -EIO;
300 	}
301 
302 	if (t->enabled) {
303 		reg[M41T80_REG_ALARM_MON] |= M41T80_ALMON_AFE;
304 		if (i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
305 					      reg[M41T80_REG_ALARM_MON]) < 0) {
306 			dev_err(&client->dev, "write error\n");
307 			return -EIO;
308 		}
309 	}
310 	return 0;
311 }
312 
313 static int m41t80_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *t)
314 {
315 	struct i2c_client *client = to_i2c_client(dev);
316 	u8 buf[M41T80_ALARM_REG_SIZE + 1]; /* all alarm regs and flags */
317 	u8 dt_addr[1] = { M41T80_REG_ALARM_MON };
318 	u8 *reg = buf - M41T80_REG_ALARM_MON;
319 	struct i2c_msg msgs[] = {
320 		{
321 			.addr	= client->addr,
322 			.flags	= 0,
323 			.len	= 1,
324 			.buf	= dt_addr,
325 		},
326 		{
327 			.addr	= client->addr,
328 			.flags	= I2C_M_RD,
329 			.len	= M41T80_ALARM_REG_SIZE + 1,
330 			.buf	= buf,
331 		},
332 	};
333 
334 	if (i2c_transfer(client->adapter, msgs, 2) < 0) {
335 		dev_err(&client->dev, "read error\n");
336 		return -EIO;
337 	}
338 	t->time.tm_sec = -1;
339 	t->time.tm_min = -1;
340 	t->time.tm_hour = -1;
341 	t->time.tm_mday = -1;
342 	t->time.tm_mon = -1;
343 	if (!(reg[M41T80_REG_ALARM_SEC] & 0x80))
344 		t->time.tm_sec = BCD2BIN(reg[M41T80_REG_ALARM_SEC] & 0x7f);
345 	if (!(reg[M41T80_REG_ALARM_MIN] & 0x80))
346 		t->time.tm_min = BCD2BIN(reg[M41T80_REG_ALARM_MIN] & 0x7f);
347 	if (!(reg[M41T80_REG_ALARM_HOUR] & 0x80))
348 		t->time.tm_hour = BCD2BIN(reg[M41T80_REG_ALARM_HOUR] & 0x3f);
349 	if (!(reg[M41T80_REG_ALARM_DAY] & 0x80))
350 		t->time.tm_mday = BCD2BIN(reg[M41T80_REG_ALARM_DAY] & 0x3f);
351 	if (!(reg[M41T80_REG_ALARM_DAY] & 0x40))
352 		t->time.tm_mon = BCD2BIN(reg[M41T80_REG_ALARM_MON] & 0x1f) - 1;
353 	t->time.tm_year = -1;
354 	t->time.tm_wday = -1;
355 	t->time.tm_yday = -1;
356 	t->time.tm_isdst = -1;
357 	t->enabled = !!(reg[M41T80_REG_ALARM_MON] & M41T80_ALMON_AFE);
358 	t->pending = !!(reg[M41T80_REG_FLAGS] & M41T80_FLAGS_AF);
359 	return 0;
360 }
361 
362 static struct rtc_class_ops m41t80_rtc_ops = {
363 	.read_time = m41t80_rtc_read_time,
364 	.set_time = m41t80_rtc_set_time,
365 	.read_alarm = m41t80_rtc_read_alarm,
366 	.set_alarm = m41t80_rtc_set_alarm,
367 	.proc = m41t80_rtc_proc,
368 	.ioctl = m41t80_rtc_ioctl,
369 };
370 
371 #if defined(CONFIG_RTC_INTF_SYSFS) || defined(CONFIG_RTC_INTF_SYSFS_MODULE)
372 static ssize_t m41t80_sysfs_show_flags(struct device *dev,
373 				struct device_attribute *attr, char *buf)
374 {
375 	struct i2c_client *client = to_i2c_client(dev);
376 	int val;
377 
378 	val = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
379 	if (val < 0)
380 		return -EIO;
381 	return sprintf(buf, "%#x\n", val);
382 }
383 static DEVICE_ATTR(flags, S_IRUGO, m41t80_sysfs_show_flags, NULL);
384 
385 static ssize_t m41t80_sysfs_show_sqwfreq(struct device *dev,
386 				struct device_attribute *attr, char *buf)
387 {
388 	struct i2c_client *client = to_i2c_client(dev);
389 	int val;
390 
391 	val = i2c_smbus_read_byte_data(client, M41T80_REG_SQW);
392 	if (val < 0)
393 		return -EIO;
394 	val = (val >> 4) & 0xf;
395 	switch (val) {
396 	case 0:
397 		break;
398 	case 1:
399 		val = 32768;
400 		break;
401 	default:
402 		val = 32768 >> val;
403 	}
404 	return sprintf(buf, "%d\n", val);
405 }
406 static ssize_t m41t80_sysfs_set_sqwfreq(struct device *dev,
407 				struct device_attribute *attr,
408 				const char *buf, size_t count)
409 {
410 	struct i2c_client *client = to_i2c_client(dev);
411 	int almon, sqw;
412 	int val = simple_strtoul(buf, NULL, 0);
413 
414 	if (val) {
415 		if (!is_power_of_2(val))
416 			return -EINVAL;
417 		val = ilog2(val);
418 		if (val == 15)
419 			val = 1;
420 		else if (val < 14)
421 			val = 15 - val;
422 		else
423 			return -EINVAL;
424 	}
425 	/* disable SQW, set SQW frequency & re-enable */
426 	almon = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
427 	if (almon < 0)
428 		return -EIO;
429 	sqw = i2c_smbus_read_byte_data(client, M41T80_REG_SQW);
430 	if (sqw < 0)
431 		return -EIO;
432 	sqw = (sqw & 0x0f) | (val << 4);
433 	if (i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
434 				      almon & ~M41T80_ALMON_SQWE) < 0 ||
435 	    i2c_smbus_write_byte_data(client, M41T80_REG_SQW, sqw) < 0)
436 		return -EIO;
437 	if (val && i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
438 					     almon | M41T80_ALMON_SQWE) < 0)
439 		return -EIO;
440 	return count;
441 }
442 static DEVICE_ATTR(sqwfreq, S_IRUGO | S_IWUSR,
443 		   m41t80_sysfs_show_sqwfreq, m41t80_sysfs_set_sqwfreq);
444 
445 static struct attribute *attrs[] = {
446 	&dev_attr_flags.attr,
447 	&dev_attr_sqwfreq.attr,
448 	NULL,
449 };
450 static struct attribute_group attr_group = {
451 	.attrs = attrs,
452 };
453 
454 static int m41t80_sysfs_register(struct device *dev)
455 {
456 	return sysfs_create_group(&dev->kobj, &attr_group);
457 }
458 #else
459 static int m41t80_sysfs_register(struct device *dev)
460 {
461 	return 0;
462 }
463 #endif
464 
465 #ifdef CONFIG_RTC_DRV_M41T80_WDT
466 /*
467  *****************************************************************************
468  *
469  * Watchdog Driver
470  *
471  *****************************************************************************
472  */
473 static struct i2c_client *save_client;
474 
475 /* Default margin */
476 #define WD_TIMO 60		/* 1..31 seconds */
477 
478 static int wdt_margin = WD_TIMO;
479 module_param(wdt_margin, int, 0);
480 MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 60s)");
481 
482 static unsigned long wdt_is_open;
483 static int boot_flag;
484 
485 /**
486  *	wdt_ping:
487  *
488  *	Reload counter one with the watchdog timeout. We don't bother reloading
489  *	the cascade counter.
490  */
491 static void wdt_ping(void)
492 {
493 	unsigned char i2c_data[2];
494 	struct i2c_msg msgs1[1] = {
495 		{
496 			.addr	= save_client->addr,
497 			.flags	= 0,
498 			.len	= 2,
499 			.buf	= i2c_data,
500 		},
501 	};
502 	i2c_data[0] = 0x09;		/* watchdog register */
503 
504 	if (wdt_margin > 31)
505 		i2c_data[1] = (wdt_margin & 0xFC) | 0x83; /* resolution = 4s */
506 	else
507 		/*
508 		 * WDS = 1 (0x80), mulitplier = WD_TIMO, resolution = 1s (0x02)
509 		 */
510 		i2c_data[1] = wdt_margin<<2 | 0x82;
511 
512 	i2c_transfer(save_client->adapter, msgs1, 1);
513 }
514 
515 /**
516  *	wdt_disable:
517  *
518  *	disables watchdog.
519  */
520 static void wdt_disable(void)
521 {
522 	unsigned char i2c_data[2], i2c_buf[0x10];
523 	struct i2c_msg msgs0[2] = {
524 		{
525 			.addr	= save_client->addr,
526 			.flags	= 0,
527 			.len	= 1,
528 			.buf	= i2c_data,
529 		},
530 		{
531 			.addr	= save_client->addr,
532 			.flags	= I2C_M_RD,
533 			.len	= 1,
534 			.buf	= i2c_buf,
535 		},
536 	};
537 	struct i2c_msg msgs1[1] = {
538 		{
539 			.addr	= save_client->addr,
540 			.flags	= 0,
541 			.len	= 2,
542 			.buf	= i2c_data,
543 		},
544 	};
545 
546 	i2c_data[0] = 0x09;
547 	i2c_transfer(save_client->adapter, msgs0, 2);
548 
549 	i2c_data[0] = 0x09;
550 	i2c_data[1] = 0x00;
551 	i2c_transfer(save_client->adapter, msgs1, 1);
552 }
553 
554 /**
555  *	wdt_write:
556  *	@file: file handle to the watchdog
557  *	@buf: buffer to write (unused as data does not matter here
558  *	@count: count of bytes
559  *	@ppos: pointer to the position to write. No seeks allowed
560  *
561  *	A write to a watchdog device is defined as a keepalive signal. Any
562  *	write of data will do, as we we don't define content meaning.
563  */
564 static ssize_t wdt_write(struct file *file, const char __user *buf,
565 			 size_t count, loff_t *ppos)
566 {
567 	/*  Can't seek (pwrite) on this device
568 	if (ppos != &file->f_pos)
569 	return -ESPIPE;
570 	*/
571 	if (count) {
572 		wdt_ping();
573 		return 1;
574 	}
575 	return 0;
576 }
577 
578 static ssize_t wdt_read(struct file *file, char __user *buf,
579 			size_t count, loff_t *ppos)
580 {
581 	return 0;
582 }
583 
584 /**
585  *	wdt_ioctl:
586  *	@inode: inode of the device
587  *	@file: file handle to the device
588  *	@cmd: watchdog command
589  *	@arg: argument pointer
590  *
591  *	The watchdog API defines a common set of functions for all watchdogs
592  *	according to their available features. We only actually usefully support
593  *	querying capabilities and current status.
594  */
595 static int wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
596 		     unsigned long arg)
597 {
598 	int new_margin, rv;
599 	static struct watchdog_info ident = {
600 		.options = WDIOF_POWERUNDER | WDIOF_KEEPALIVEPING |
601 			WDIOF_SETTIMEOUT,
602 		.firmware_version = 1,
603 		.identity = "M41T80 WTD"
604 	};
605 
606 	switch (cmd) {
607 	case WDIOC_GETSUPPORT:
608 		return copy_to_user((struct watchdog_info __user *)arg, &ident,
609 				    sizeof(ident)) ? -EFAULT : 0;
610 
611 	case WDIOC_GETSTATUS:
612 	case WDIOC_GETBOOTSTATUS:
613 		return put_user(boot_flag, (int __user *)arg);
614 	case WDIOC_KEEPALIVE:
615 		wdt_ping();
616 		return 0;
617 	case WDIOC_SETTIMEOUT:
618 		if (get_user(new_margin, (int __user *)arg))
619 			return -EFAULT;
620 		/* Arbitrary, can't find the card's limits */
621 		if (new_margin < 1 || new_margin > 124)
622 			return -EINVAL;
623 		wdt_margin = new_margin;
624 		wdt_ping();
625 		/* Fall */
626 	case WDIOC_GETTIMEOUT:
627 		return put_user(wdt_margin, (int __user *)arg);
628 
629 	case WDIOC_SETOPTIONS:
630 		if (copy_from_user(&rv, (int __user *)arg, sizeof(int)))
631 			return -EFAULT;
632 
633 		if (rv & WDIOS_DISABLECARD) {
634 			pr_info("rtc-m41t80: disable watchdog\n");
635 			wdt_disable();
636 		}
637 
638 		if (rv & WDIOS_ENABLECARD) {
639 			pr_info("rtc-m41t80: enable watchdog\n");
640 			wdt_ping();
641 		}
642 
643 		return -EINVAL;
644 	}
645 	return -ENOTTY;
646 }
647 
648 /**
649  *	wdt_open:
650  *	@inode: inode of device
651  *	@file: file handle to device
652  *
653  */
654 static int wdt_open(struct inode *inode, struct file *file)
655 {
656 	if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) {
657 		lock_kernel();
658 		if (test_and_set_bit(0, &wdt_is_open)) {
659 			unlock_kernel();
660 			return -EBUSY;
661 		}
662 		/*
663 		 *	Activate
664 		 */
665 		wdt_is_open = 1;
666 		unlock_kernel();
667 		return 0;
668 	}
669 	return -ENODEV;
670 }
671 
672 /**
673  *	wdt_close:
674  *	@inode: inode to board
675  *	@file: file handle to board
676  *
677  */
678 static int wdt_release(struct inode *inode, struct file *file)
679 {
680 	if (MINOR(inode->i_rdev) == WATCHDOG_MINOR)
681 		clear_bit(0, &wdt_is_open);
682 	return 0;
683 }
684 
685 /**
686  *	notify_sys:
687  *	@this: our notifier block
688  *	@code: the event being reported
689  *	@unused: unused
690  *
691  *	Our notifier is called on system shutdowns. We want to turn the card
692  *	off at reboot otherwise the machine will reboot again during memory
693  *	test or worse yet during the following fsck. This would suck, in fact
694  *	trust me - if it happens it does suck.
695  */
696 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
697 			  void *unused)
698 {
699 	if (code == SYS_DOWN || code == SYS_HALT)
700 		/* Disable Watchdog */
701 		wdt_disable();
702 	return NOTIFY_DONE;
703 }
704 
705 static const struct file_operations wdt_fops = {
706 	.owner	= THIS_MODULE,
707 	.read	= wdt_read,
708 	.ioctl	= wdt_ioctl,
709 	.write	= wdt_write,
710 	.open	= wdt_open,
711 	.release = wdt_release,
712 };
713 
714 static struct miscdevice wdt_dev = {
715 	.minor = WATCHDOG_MINOR,
716 	.name = "watchdog",
717 	.fops = &wdt_fops,
718 };
719 
720 /*
721  *	The WDT card needs to learn about soft shutdowns in order to
722  *	turn the timebomb registers off.
723  */
724 static struct notifier_block wdt_notifier = {
725 	.notifier_call = wdt_notify_sys,
726 };
727 #endif /* CONFIG_RTC_DRV_M41T80_WDT */
728 
729 /*
730  *****************************************************************************
731  *
732  *	Driver Interface
733  *
734  *****************************************************************************
735  */
736 static int m41t80_probe(struct i2c_client *client,
737 			const struct i2c_device_id *id)
738 {
739 	int rc = 0;
740 	struct rtc_device *rtc = NULL;
741 	struct rtc_time tm;
742 	struct m41t80_data *clientdata = NULL;
743 
744 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C
745 				     | I2C_FUNC_SMBUS_BYTE_DATA)) {
746 		rc = -ENODEV;
747 		goto exit;
748 	}
749 
750 	dev_info(&client->dev,
751 		 "chip found, driver version " DRV_VERSION "\n");
752 
753 	clientdata = kzalloc(sizeof(*clientdata), GFP_KERNEL);
754 	if (!clientdata) {
755 		rc = -ENOMEM;
756 		goto exit;
757 	}
758 
759 	rtc = rtc_device_register(client->name, &client->dev,
760 				  &m41t80_rtc_ops, THIS_MODULE);
761 	if (IS_ERR(rtc)) {
762 		rc = PTR_ERR(rtc);
763 		rtc = NULL;
764 		goto exit;
765 	}
766 
767 	clientdata->rtc = rtc;
768 	clientdata->features = id->driver_data;
769 	i2c_set_clientdata(client, clientdata);
770 
771 	/* Make sure HT (Halt Update) bit is cleared */
772 	rc = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_HOUR);
773 	if (rc < 0)
774 		goto ht_err;
775 
776 	if (rc & M41T80_ALHOUR_HT) {
777 		if (clientdata->features & M41T80_FEATURE_HT) {
778 			m41t80_get_datetime(client, &tm);
779 			dev_info(&client->dev, "HT bit was set!\n");
780 			dev_info(&client->dev,
781 				 "Power Down at "
782 				 "%04i-%02i-%02i %02i:%02i:%02i\n",
783 				 tm.tm_year + 1900,
784 				 tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
785 				 tm.tm_min, tm.tm_sec);
786 		}
787 		if (i2c_smbus_write_byte_data(client,
788 					      M41T80_REG_ALARM_HOUR,
789 					      rc & ~M41T80_ALHOUR_HT) < 0)
790 			goto ht_err;
791 	}
792 
793 	/* Make sure ST (stop) bit is cleared */
794 	rc = i2c_smbus_read_byte_data(client, M41T80_REG_SEC);
795 	if (rc < 0)
796 		goto st_err;
797 
798 	if (rc & M41T80_SEC_ST) {
799 		if (i2c_smbus_write_byte_data(client, M41T80_REG_SEC,
800 					      rc & ~M41T80_SEC_ST) < 0)
801 			goto st_err;
802 	}
803 
804 	rc = m41t80_sysfs_register(&client->dev);
805 	if (rc)
806 		goto exit;
807 
808 #ifdef CONFIG_RTC_DRV_M41T80_WDT
809 	if (clientdata->features & M41T80_FEATURE_HT) {
810 		save_client = client;
811 		rc = misc_register(&wdt_dev);
812 		if (rc)
813 			goto exit;
814 		rc = register_reboot_notifier(&wdt_notifier);
815 		if (rc) {
816 			misc_deregister(&wdt_dev);
817 			goto exit;
818 		}
819 	}
820 #endif
821 	return 0;
822 
823 st_err:
824 	rc = -EIO;
825 	dev_err(&client->dev, "Can't clear ST bit\n");
826 	goto exit;
827 ht_err:
828 	rc = -EIO;
829 	dev_err(&client->dev, "Can't clear HT bit\n");
830 	goto exit;
831 
832 exit:
833 	if (rtc)
834 		rtc_device_unregister(rtc);
835 	kfree(clientdata);
836 	return rc;
837 }
838 
839 static int m41t80_remove(struct i2c_client *client)
840 {
841 	struct m41t80_data *clientdata = i2c_get_clientdata(client);
842 	struct rtc_device *rtc = clientdata->rtc;
843 
844 #ifdef CONFIG_RTC_DRV_M41T80_WDT
845 	if (clientdata->features & M41T80_FEATURE_HT) {
846 		misc_deregister(&wdt_dev);
847 		unregister_reboot_notifier(&wdt_notifier);
848 	}
849 #endif
850 	if (rtc)
851 		rtc_device_unregister(rtc);
852 	kfree(clientdata);
853 
854 	return 0;
855 }
856 
857 static struct i2c_driver m41t80_driver = {
858 	.driver = {
859 		.name = "rtc-m41t80",
860 	},
861 	.probe = m41t80_probe,
862 	.remove = m41t80_remove,
863 	.id_table = m41t80_id,
864 };
865 
866 static int __init m41t80_rtc_init(void)
867 {
868 	return i2c_add_driver(&m41t80_driver);
869 }
870 
871 static void __exit m41t80_rtc_exit(void)
872 {
873 	i2c_del_driver(&m41t80_driver);
874 }
875 
876 MODULE_AUTHOR("Alexander Bigga <ab@mycable.de>");
877 MODULE_DESCRIPTION("ST Microelectronics M41T80 series RTC I2C Client Driver");
878 MODULE_LICENSE("GPL");
879 MODULE_VERSION(DRV_VERSION);
880 
881 module_init(m41t80_rtc_init);
882 module_exit(m41t80_rtc_exit);
883