uwb: add the umc bus
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / rtc / rtc-m41t80.c
blob24bc1689fc74c095d88641cb7c78755e5ee0b9f8
1 /*
2 * I2C client/driver for the ST M41T80 family of i2c rtc chips.
4 * Author: Alexander Bigga <ab@mycable.de>
6 * Based on m41t00.c by Mark A. Greer <mgreer@mvista.com>
8 * 2006 (c) mycable GmbH
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.
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
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
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)
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 */
60 #define M41T80_FEATURE_HT (1 << 0)
61 #define M41T80_FEATURE_BL (1 << 1)
63 #define DRV_VERSION "0.05"
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 { }
76 MODULE_DEVICE_TABLE(i2c, m41t80_id);
78 struct m41t80_data {
79 u8 features;
80 struct rtc_device *rtc;
83 static int m41t80_get_datetime(struct i2c_client *client,
84 struct rtc_time *tm)
86 u8 buf[M41T80_DATETIME_REG_SIZE], dt_addr[1] = { M41T80_REG_SEC };
87 struct i2c_msg msgs[] = {
89 .addr = client->addr,
90 .flags = 0,
91 .len = 1,
92 .buf = dt_addr,
95 .addr = client->addr,
96 .flags = I2C_M_RD,
97 .len = M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC,
98 .buf = buf + M41T80_REG_SEC,
102 if (i2c_transfer(client->adapter, msgs, 2) < 0) {
103 dev_err(&client->dev, "read error\n");
104 return -EIO;
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;
114 /* assume 20YY not 19YY, and ignore the Century Bit */
115 tm->tm_year = BCD2BIN(buf[M41T80_REG_YEAR]) + 100;
116 return 0;
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)
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[] = {
127 .addr = client->addr,
128 .flags = 0,
129 .len = 1,
130 .buf = dt_addr,
133 .addr = client->addr,
134 .flags = I2C_M_RD,
135 .len = M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC,
136 .buf = buf + M41T80_REG_SEC,
139 struct i2c_msg msgs[] = {
141 .addr = client->addr,
142 .flags = 0,
143 .len = 1 + M41T80_DATETIME_REG_SIZE,
144 .buf = wbuf,
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;
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);
172 if (i2c_transfer(client->adapter, msgs, 1) != 1) {
173 dev_err(&client->dev, "write error\n");
174 return -EIO;
176 return 0;
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)
182 struct i2c_client *client = to_i2c_client(dev);
183 struct m41t80_data *clientdata = i2c_get_clientdata(client);
184 u8 reg;
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");
191 return 0;
193 #else
194 #define m41t80_rtc_proc NULL
195 #endif
197 static int m41t80_rtc_read_time(struct device *dev, struct rtc_time *tm)
199 return m41t80_get_datetime(to_i2c_client(dev), tm);
202 static int m41t80_rtc_set_time(struct device *dev, struct rtc_time *tm)
204 return m41t80_set_datetime(to_i2c_client(dev), tm);
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)
211 struct i2c_client *client = to_i2c_client(dev);
212 int rc;
214 switch (cmd) {
215 case RTC_AIE_OFF:
216 case RTC_AIE_ON:
217 break;
218 default:
219 return -ENOIOCTLCMD;
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;
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;
239 #else
240 #define m41t80_rtc_ioctl NULL
241 #endif
243 static int m41t80_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *t)
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[] = {
252 .addr = client->addr,
253 .flags = 0,
254 .len = 1,
255 .buf = dt_addr,
258 .addr = client->addr,
259 .flags = I2C_M_RD,
260 .len = M41T80_ALARM_REG_SIZE,
261 .buf = buf,
264 struct i2c_msg msgs[] = {
266 .addr = client->addr,
267 .flags = 0,
268 .len = 1 + M41T80_ALARM_REG_SIZE,
269 .buf = wbuf,
273 if (i2c_transfer(client->adapter, msgs_in, 2) < 0) {
274 dev_err(&client->dev, "read error\n");
275 return -EIO;
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;
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;
297 if (i2c_transfer(client->adapter, msgs, 1) != 1) {
298 dev_err(&client->dev, "write error\n");
299 return -EIO;
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;
310 return 0;
313 static int m41t80_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *t)
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[] = {
321 .addr = client->addr,
322 .flags = 0,
323 .len = 1,
324 .buf = dt_addr,
327 .addr = client->addr,
328 .flags = I2C_M_RD,
329 .len = M41T80_ALARM_REG_SIZE + 1,
330 .buf = buf,
334 if (i2c_transfer(client->adapter, msgs, 2) < 0) {
335 dev_err(&client->dev, "read error\n");
336 return -EIO;
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;
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,
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)
375 struct i2c_client *client = to_i2c_client(dev);
376 int val;
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);
383 static DEVICE_ATTR(flags, S_IRUGO, m41t80_sysfs_show_flags, NULL);
385 static ssize_t m41t80_sysfs_show_sqwfreq(struct device *dev,
386 struct device_attribute *attr, char *buf)
388 struct i2c_client *client = to_i2c_client(dev);
389 int val;
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;
404 return sprintf(buf, "%d\n", val);
406 static ssize_t m41t80_sysfs_set_sqwfreq(struct device *dev,
407 struct device_attribute *attr,
408 const char *buf, size_t count)
410 struct i2c_client *client = to_i2c_client(dev);
411 int almon, sqw;
412 int val = simple_strtoul(buf, NULL, 0);
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;
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;
442 static DEVICE_ATTR(sqwfreq, S_IRUGO | S_IWUSR,
443 m41t80_sysfs_show_sqwfreq, m41t80_sysfs_set_sqwfreq);
445 static struct attribute *attrs[] = {
446 &dev_attr_flags.attr,
447 &dev_attr_sqwfreq.attr,
448 NULL,
450 static struct attribute_group attr_group = {
451 .attrs = attrs,
454 static int m41t80_sysfs_register(struct device *dev)
456 return sysfs_create_group(&dev->kobj, &attr_group);
458 #else
459 static int m41t80_sysfs_register(struct device *dev)
461 return 0;
463 #endif
465 #ifdef CONFIG_RTC_DRV_M41T80_WDT
467 *****************************************************************************
469 * Watchdog Driver
471 *****************************************************************************
473 static struct i2c_client *save_client;
475 /* Default margin */
476 #define WD_TIMO 60 /* 1..31 seconds */
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)");
482 static unsigned long wdt_is_open;
483 static int boot_flag;
486 * wdt_ping:
488 * Reload counter one with the watchdog timeout. We don't bother reloading
489 * the cascade counter.
491 static void wdt_ping(void)
493 unsigned char i2c_data[2];
494 struct i2c_msg msgs1[1] = {
496 .addr = save_client->addr,
497 .flags = 0,
498 .len = 2,
499 .buf = i2c_data,
502 i2c_data[0] = 0x09; /* watchdog register */
504 if (wdt_margin > 31)
505 i2c_data[1] = (wdt_margin & 0xFC) | 0x83; /* resolution = 4s */
506 else
508 * WDS = 1 (0x80), mulitplier = WD_TIMO, resolution = 1s (0x02)
510 i2c_data[1] = wdt_margin<<2 | 0x82;
512 i2c_transfer(save_client->adapter, msgs1, 1);
516 * wdt_disable:
518 * disables watchdog.
520 static void wdt_disable(void)
522 unsigned char i2c_data[2], i2c_buf[0x10];
523 struct i2c_msg msgs0[2] = {
525 .addr = save_client->addr,
526 .flags = 0,
527 .len = 1,
528 .buf = i2c_data,
531 .addr = save_client->addr,
532 .flags = I2C_M_RD,
533 .len = 1,
534 .buf = i2c_buf,
537 struct i2c_msg msgs1[1] = {
539 .addr = save_client->addr,
540 .flags = 0,
541 .len = 2,
542 .buf = i2c_data,
546 i2c_data[0] = 0x09;
547 i2c_transfer(save_client->adapter, msgs0, 2);
549 i2c_data[0] = 0x09;
550 i2c_data[1] = 0x00;
551 i2c_transfer(save_client->adapter, msgs1, 1);
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
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.
564 static ssize_t wdt_write(struct file *file, const char __user *buf,
565 size_t count, loff_t *ppos)
567 /* Can't seek (pwrite) on this device
568 if (ppos != &file->f_pos)
569 return -ESPIPE;
571 if (count) {
572 wdt_ping();
573 return 1;
575 return 0;
578 static ssize_t wdt_read(struct file *file, char __user *buf,
579 size_t count, loff_t *ppos)
581 return 0;
585 * wdt_ioctl:
586 * @inode: inode of the device
587 * @file: file handle to the device
588 * @cmd: watchdog command
589 * @arg: argument pointer
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.
595 static int wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
596 unsigned long arg)
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"
606 switch (cmd) {
607 case WDIOC_GETSUPPORT:
608 return copy_to_user((struct watchdog_info __user *)arg, &ident,
609 sizeof(ident)) ? -EFAULT : 0;
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);
629 case WDIOC_SETOPTIONS:
630 if (copy_from_user(&rv, (int __user *)arg, sizeof(int)))
631 return -EFAULT;
633 if (rv & WDIOS_DISABLECARD) {
634 pr_info("rtc-m41t80: disable watchdog\n");
635 wdt_disable();
638 if (rv & WDIOS_ENABLECARD) {
639 pr_info("rtc-m41t80: enable watchdog\n");
640 wdt_ping();
643 return -EINVAL;
645 return -ENOTTY;
649 * wdt_open:
650 * @inode: inode of device
651 * @file: file handle to device
654 static int wdt_open(struct inode *inode, struct file *file)
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;
663 * Activate
665 wdt_is_open = 1;
666 unlock_kernel();
667 return 0;
669 return -ENODEV;
673 * wdt_close:
674 * @inode: inode to board
675 * @file: file handle to board
678 static int wdt_release(struct inode *inode, struct file *file)
680 if (MINOR(inode->i_rdev) == WATCHDOG_MINOR)
681 clear_bit(0, &wdt_is_open);
682 return 0;
686 * notify_sys:
687 * @this: our notifier block
688 * @code: the event being reported
689 * @unused: unused
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.
696 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
697 void *unused)
699 if (code == SYS_DOWN || code == SYS_HALT)
700 /* Disable Watchdog */
701 wdt_disable();
702 return NOTIFY_DONE;
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,
714 static struct miscdevice wdt_dev = {
715 .minor = WATCHDOG_MINOR,
716 .name = "watchdog",
717 .fops = &wdt_fops,
721 * The WDT card needs to learn about soft shutdowns in order to
722 * turn the timebomb registers off.
724 static struct notifier_block wdt_notifier = {
725 .notifier_call = wdt_notify_sys,
727 #endif /* CONFIG_RTC_DRV_M41T80_WDT */
730 *****************************************************************************
732 * Driver Interface
734 *****************************************************************************
736 static int m41t80_probe(struct i2c_client *client,
737 const struct i2c_device_id *id)
739 int rc = 0;
740 struct rtc_device *rtc = NULL;
741 struct rtc_time tm;
742 struct m41t80_data *clientdata = NULL;
744 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C
745 | I2C_FUNC_SMBUS_BYTE_DATA)) {
746 rc = -ENODEV;
747 goto exit;
750 dev_info(&client->dev,
751 "chip found, driver version " DRV_VERSION "\n");
753 clientdata = kzalloc(sizeof(*clientdata), GFP_KERNEL);
754 if (!clientdata) {
755 rc = -ENOMEM;
756 goto exit;
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;
767 clientdata->rtc = rtc;
768 clientdata->features = id->driver_data;
769 i2c_set_clientdata(client, clientdata);
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;
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);
787 if (i2c_smbus_write_byte_data(client,
788 M41T80_REG_ALARM_HOUR,
789 rc & ~M41T80_ALHOUR_HT) < 0)
790 goto ht_err;
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;
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;
804 rc = m41t80_sysfs_register(&client->dev);
805 if (rc)
806 goto exit;
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;
820 #endif
821 return 0;
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;
832 exit:
833 if (rtc)
834 rtc_device_unregister(rtc);
835 kfree(clientdata);
836 return rc;
839 static int m41t80_remove(struct i2c_client *client)
841 struct m41t80_data *clientdata = i2c_get_clientdata(client);
842 struct rtc_device *rtc = clientdata->rtc;
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);
849 #endif
850 if (rtc)
851 rtc_device_unregister(rtc);
852 kfree(clientdata);
854 return 0;
857 static struct i2c_driver m41t80_driver = {
858 .driver = {
859 .name = "rtc-m41t80",
861 .probe = m41t80_probe,
862 .remove = m41t80_remove,
863 .id_table = m41t80_id,
866 static int __init m41t80_rtc_init(void)
868 return i2c_add_driver(&m41t80_driver);
871 static void __exit m41t80_rtc_exit(void)
873 i2c_del_driver(&m41t80_driver);
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);
881 module_init(m41t80_rtc_init);
882 module_exit(m41t80_rtc_exit);