x86: always set _PAGE_GLOBAL in _PAGE_KERNEL* flags
[linux-2.6/libata-dev.git] / drivers / rtc / rtc-m41t80.c
bloba3e0880b38fb2cdb0116b4168664566fdab84ab4
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/module.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/string.h>
21 #include <linux/i2c.h>
22 #include <linux/rtc.h>
23 #include <linux/bcd.h>
24 #ifdef CONFIG_RTC_DRV_M41T80_WDT
25 #include <linux/miscdevice.h>
26 #include <linux/watchdog.h>
27 #include <linux/reboot.h>
28 #include <linux/fs.h>
29 #include <linux/ioctl.h>
30 #endif
32 #define M41T80_REG_SSEC 0
33 #define M41T80_REG_SEC 1
34 #define M41T80_REG_MIN 2
35 #define M41T80_REG_HOUR 3
36 #define M41T80_REG_WDAY 4
37 #define M41T80_REG_DAY 5
38 #define M41T80_REG_MON 6
39 #define M41T80_REG_YEAR 7
40 #define M41T80_REG_ALARM_MON 0xa
41 #define M41T80_REG_ALARM_DAY 0xb
42 #define M41T80_REG_ALARM_HOUR 0xc
43 #define M41T80_REG_ALARM_MIN 0xd
44 #define M41T80_REG_ALARM_SEC 0xe
45 #define M41T80_REG_FLAGS 0xf
46 #define M41T80_REG_SQW 0x13
48 #define M41T80_DATETIME_REG_SIZE (M41T80_REG_YEAR + 1)
49 #define M41T80_ALARM_REG_SIZE \
50 (M41T80_REG_ALARM_SEC + 1 - M41T80_REG_ALARM_MON)
52 #define M41T80_SEC_ST (1 << 7) /* ST: Stop Bit */
53 #define M41T80_ALMON_AFE (1 << 7) /* AFE: AF Enable Bit */
54 #define M41T80_ALMON_SQWE (1 << 6) /* SQWE: SQW Enable Bit */
55 #define M41T80_ALHOUR_HT (1 << 6) /* HT: Halt Update Bit */
56 #define M41T80_FLAGS_AF (1 << 6) /* AF: Alarm Flag Bit */
57 #define M41T80_FLAGS_BATT_LOW (1 << 4) /* BL: Battery Low Bit */
59 #define M41T80_FEATURE_HT (1 << 0)
60 #define M41T80_FEATURE_BL (1 << 1)
62 #define DRV_VERSION "0.05"
64 static const struct i2c_device_id m41t80_id[] = {
65 { "m41t80", 0 },
66 { "m41t81", M41T80_FEATURE_HT },
67 { "m41t81s", M41T80_FEATURE_HT | M41T80_FEATURE_BL },
68 { "m41t82", M41T80_FEATURE_HT | M41T80_FEATURE_BL },
69 { "m41t83", M41T80_FEATURE_HT | M41T80_FEATURE_BL },
70 { "m41st84", M41T80_FEATURE_HT | M41T80_FEATURE_BL },
71 { "m41st85", M41T80_FEATURE_HT | M41T80_FEATURE_BL },
72 { "m41st87", M41T80_FEATURE_HT | M41T80_FEATURE_BL },
73 { }
75 MODULE_DEVICE_TABLE(i2c, m41t80_id);
77 struct m41t80_data {
78 u8 features;
79 struct rtc_device *rtc;
82 static int m41t80_get_datetime(struct i2c_client *client,
83 struct rtc_time *tm)
85 u8 buf[M41T80_DATETIME_REG_SIZE], dt_addr[1] = { M41T80_REG_SEC };
86 struct i2c_msg msgs[] = {
88 .addr = client->addr,
89 .flags = 0,
90 .len = 1,
91 .buf = dt_addr,
94 .addr = client->addr,
95 .flags = I2C_M_RD,
96 .len = M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC,
97 .buf = buf + M41T80_REG_SEC,
101 if (i2c_transfer(client->adapter, msgs, 2) < 0) {
102 dev_err(&client->dev, "read error\n");
103 return -EIO;
106 tm->tm_sec = BCD2BIN(buf[M41T80_REG_SEC] & 0x7f);
107 tm->tm_min = BCD2BIN(buf[M41T80_REG_MIN] & 0x7f);
108 tm->tm_hour = BCD2BIN(buf[M41T80_REG_HOUR] & 0x3f);
109 tm->tm_mday = BCD2BIN(buf[M41T80_REG_DAY] & 0x3f);
110 tm->tm_wday = buf[M41T80_REG_WDAY] & 0x07;
111 tm->tm_mon = BCD2BIN(buf[M41T80_REG_MON] & 0x1f) - 1;
113 /* assume 20YY not 19YY, and ignore the Century Bit */
114 tm->tm_year = BCD2BIN(buf[M41T80_REG_YEAR]) + 100;
115 return 0;
118 /* Sets the given date and time to the real time clock. */
119 static int m41t80_set_datetime(struct i2c_client *client, struct rtc_time *tm)
121 u8 wbuf[1 + M41T80_DATETIME_REG_SIZE];
122 u8 *buf = &wbuf[1];
123 u8 dt_addr[1] = { M41T80_REG_SEC };
124 struct i2c_msg msgs_in[] = {
126 .addr = client->addr,
127 .flags = 0,
128 .len = 1,
129 .buf = dt_addr,
132 .addr = client->addr,
133 .flags = I2C_M_RD,
134 .len = M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC,
135 .buf = buf + M41T80_REG_SEC,
138 struct i2c_msg msgs[] = {
140 .addr = client->addr,
141 .flags = 0,
142 .len = 1 + M41T80_DATETIME_REG_SIZE,
143 .buf = wbuf,
147 /* Read current reg values into buf[1..7] */
148 if (i2c_transfer(client->adapter, msgs_in, 2) < 0) {
149 dev_err(&client->dev, "read error\n");
150 return -EIO;
153 wbuf[0] = 0; /* offset into rtc's regs */
154 /* Merge time-data and register flags into buf[0..7] */
155 buf[M41T80_REG_SSEC] = 0;
156 buf[M41T80_REG_SEC] =
157 BIN2BCD(tm->tm_sec) | (buf[M41T80_REG_SEC] & ~0x7f);
158 buf[M41T80_REG_MIN] =
159 BIN2BCD(tm->tm_min) | (buf[M41T80_REG_MIN] & ~0x7f);
160 buf[M41T80_REG_HOUR] =
161 BIN2BCD(tm->tm_hour) | (buf[M41T80_REG_HOUR] & ~0x3f) ;
162 buf[M41T80_REG_WDAY] =
163 (tm->tm_wday & 0x07) | (buf[M41T80_REG_WDAY] & ~0x07);
164 buf[M41T80_REG_DAY] =
165 BIN2BCD(tm->tm_mday) | (buf[M41T80_REG_DAY] & ~0x3f);
166 buf[M41T80_REG_MON] =
167 BIN2BCD(tm->tm_mon + 1) | (buf[M41T80_REG_MON] & ~0x1f);
168 /* assume 20YY not 19YY */
169 buf[M41T80_REG_YEAR] = BIN2BCD(tm->tm_year % 100);
171 if (i2c_transfer(client->adapter, msgs, 1) != 1) {
172 dev_err(&client->dev, "write error\n");
173 return -EIO;
175 return 0;
178 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
179 static int m41t80_rtc_proc(struct device *dev, struct seq_file *seq)
181 struct i2c_client *client = to_i2c_client(dev);
182 struct m41t80_data *clientdata = i2c_get_clientdata(client);
183 u8 reg;
185 if (clientdata->features & M41T80_FEATURE_BL) {
186 reg = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
187 seq_printf(seq, "battery\t\t: %s\n",
188 (reg & M41T80_FLAGS_BATT_LOW) ? "exhausted" : "ok");
190 return 0;
192 #else
193 #define m41t80_rtc_proc NULL
194 #endif
196 static int m41t80_rtc_read_time(struct device *dev, struct rtc_time *tm)
198 return m41t80_get_datetime(to_i2c_client(dev), tm);
201 static int m41t80_rtc_set_time(struct device *dev, struct rtc_time *tm)
203 return m41t80_set_datetime(to_i2c_client(dev), tm);
206 #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
207 static int
208 m41t80_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
210 struct i2c_client *client = to_i2c_client(dev);
211 int rc;
213 switch (cmd) {
214 case RTC_AIE_OFF:
215 case RTC_AIE_ON:
216 break;
217 default:
218 return -ENOIOCTLCMD;
221 rc = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
222 if (rc < 0)
223 goto err;
224 switch (cmd) {
225 case RTC_AIE_OFF:
226 rc &= ~M41T80_ALMON_AFE;
227 break;
228 case RTC_AIE_ON:
229 rc |= M41T80_ALMON_AFE;
230 break;
232 if (i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON, rc) < 0)
233 goto err;
234 return 0;
235 err:
236 return -EIO;
238 #else
239 #define m41t80_rtc_ioctl NULL
240 #endif
242 static int m41t80_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *t)
244 struct i2c_client *client = to_i2c_client(dev);
245 u8 wbuf[1 + M41T80_ALARM_REG_SIZE];
246 u8 *buf = &wbuf[1];
247 u8 *reg = buf - M41T80_REG_ALARM_MON;
248 u8 dt_addr[1] = { M41T80_REG_ALARM_MON };
249 struct i2c_msg msgs_in[] = {
251 .addr = client->addr,
252 .flags = 0,
253 .len = 1,
254 .buf = dt_addr,
257 .addr = client->addr,
258 .flags = I2C_M_RD,
259 .len = M41T80_ALARM_REG_SIZE,
260 .buf = buf,
263 struct i2c_msg msgs[] = {
265 .addr = client->addr,
266 .flags = 0,
267 .len = 1 + M41T80_ALARM_REG_SIZE,
268 .buf = wbuf,
272 if (i2c_transfer(client->adapter, msgs_in, 2) < 0) {
273 dev_err(&client->dev, "read error\n");
274 return -EIO;
276 reg[M41T80_REG_ALARM_MON] &= ~(0x1f | M41T80_ALMON_AFE);
277 reg[M41T80_REG_ALARM_DAY] = 0;
278 reg[M41T80_REG_ALARM_HOUR] &= ~(0x3f | 0x80);
279 reg[M41T80_REG_ALARM_MIN] = 0;
280 reg[M41T80_REG_ALARM_SEC] = 0;
282 wbuf[0] = M41T80_REG_ALARM_MON; /* offset into rtc's regs */
283 reg[M41T80_REG_ALARM_SEC] |= t->time.tm_sec >= 0 ?
284 BIN2BCD(t->time.tm_sec) : 0x80;
285 reg[M41T80_REG_ALARM_MIN] |= t->time.tm_min >= 0 ?
286 BIN2BCD(t->time.tm_min) : 0x80;
287 reg[M41T80_REG_ALARM_HOUR] |= t->time.tm_hour >= 0 ?
288 BIN2BCD(t->time.tm_hour) : 0x80;
289 reg[M41T80_REG_ALARM_DAY] |= t->time.tm_mday >= 0 ?
290 BIN2BCD(t->time.tm_mday) : 0x80;
291 if (t->time.tm_mon >= 0)
292 reg[M41T80_REG_ALARM_MON] |= BIN2BCD(t->time.tm_mon + 1);
293 else
294 reg[M41T80_REG_ALARM_DAY] |= 0x40;
296 if (i2c_transfer(client->adapter, msgs, 1) != 1) {
297 dev_err(&client->dev, "write error\n");
298 return -EIO;
301 if (t->enabled) {
302 reg[M41T80_REG_ALARM_MON] |= M41T80_ALMON_AFE;
303 if (i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
304 reg[M41T80_REG_ALARM_MON]) < 0) {
305 dev_err(&client->dev, "write error\n");
306 return -EIO;
309 return 0;
312 static int m41t80_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *t)
314 struct i2c_client *client = to_i2c_client(dev);
315 u8 buf[M41T80_ALARM_REG_SIZE + 1]; /* all alarm regs and flags */
316 u8 dt_addr[1] = { M41T80_REG_ALARM_MON };
317 u8 *reg = buf - M41T80_REG_ALARM_MON;
318 struct i2c_msg msgs[] = {
320 .addr = client->addr,
321 .flags = 0,
322 .len = 1,
323 .buf = dt_addr,
326 .addr = client->addr,
327 .flags = I2C_M_RD,
328 .len = M41T80_ALARM_REG_SIZE + 1,
329 .buf = buf,
333 if (i2c_transfer(client->adapter, msgs, 2) < 0) {
334 dev_err(&client->dev, "read error\n");
335 return -EIO;
337 t->time.tm_sec = -1;
338 t->time.tm_min = -1;
339 t->time.tm_hour = -1;
340 t->time.tm_mday = -1;
341 t->time.tm_mon = -1;
342 if (!(reg[M41T80_REG_ALARM_SEC] & 0x80))
343 t->time.tm_sec = BCD2BIN(reg[M41T80_REG_ALARM_SEC] & 0x7f);
344 if (!(reg[M41T80_REG_ALARM_MIN] & 0x80))
345 t->time.tm_min = BCD2BIN(reg[M41T80_REG_ALARM_MIN] & 0x7f);
346 if (!(reg[M41T80_REG_ALARM_HOUR] & 0x80))
347 t->time.tm_hour = BCD2BIN(reg[M41T80_REG_ALARM_HOUR] & 0x3f);
348 if (!(reg[M41T80_REG_ALARM_DAY] & 0x80))
349 t->time.tm_mday = BCD2BIN(reg[M41T80_REG_ALARM_DAY] & 0x3f);
350 if (!(reg[M41T80_REG_ALARM_DAY] & 0x40))
351 t->time.tm_mon = BCD2BIN(reg[M41T80_REG_ALARM_MON] & 0x1f) - 1;
352 t->time.tm_year = -1;
353 t->time.tm_wday = -1;
354 t->time.tm_yday = -1;
355 t->time.tm_isdst = -1;
356 t->enabled = !!(reg[M41T80_REG_ALARM_MON] & M41T80_ALMON_AFE);
357 t->pending = !!(reg[M41T80_REG_FLAGS] & M41T80_FLAGS_AF);
358 return 0;
361 static struct rtc_class_ops m41t80_rtc_ops = {
362 .read_time = m41t80_rtc_read_time,
363 .set_time = m41t80_rtc_set_time,
364 .read_alarm = m41t80_rtc_read_alarm,
365 .set_alarm = m41t80_rtc_set_alarm,
366 .proc = m41t80_rtc_proc,
367 .ioctl = m41t80_rtc_ioctl,
370 #if defined(CONFIG_RTC_INTF_SYSFS) || defined(CONFIG_RTC_INTF_SYSFS_MODULE)
371 static ssize_t m41t80_sysfs_show_flags(struct device *dev,
372 struct device_attribute *attr, char *buf)
374 struct i2c_client *client = to_i2c_client(dev);
375 int val;
377 val = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
378 if (val < 0)
379 return -EIO;
380 return sprintf(buf, "%#x\n", val);
382 static DEVICE_ATTR(flags, S_IRUGO, m41t80_sysfs_show_flags, NULL);
384 static ssize_t m41t80_sysfs_show_sqwfreq(struct device *dev,
385 struct device_attribute *attr, char *buf)
387 struct i2c_client *client = to_i2c_client(dev);
388 int val;
390 val = i2c_smbus_read_byte_data(client, M41T80_REG_SQW);
391 if (val < 0)
392 return -EIO;
393 val = (val >> 4) & 0xf;
394 switch (val) {
395 case 0:
396 break;
397 case 1:
398 val = 32768;
399 break;
400 default:
401 val = 32768 >> val;
403 return sprintf(buf, "%d\n", val);
405 static ssize_t m41t80_sysfs_set_sqwfreq(struct device *dev,
406 struct device_attribute *attr,
407 const char *buf, size_t count)
409 struct i2c_client *client = to_i2c_client(dev);
410 int almon, sqw;
411 int val = simple_strtoul(buf, NULL, 0);
413 if (val) {
414 if (!is_power_of_2(val))
415 return -EINVAL;
416 val = ilog2(val);
417 if (val == 15)
418 val = 1;
419 else if (val < 14)
420 val = 15 - val;
421 else
422 return -EINVAL;
424 /* disable SQW, set SQW frequency & re-enable */
425 almon = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
426 if (almon < 0)
427 return -EIO;
428 sqw = i2c_smbus_read_byte_data(client, M41T80_REG_SQW);
429 if (sqw < 0)
430 return -EIO;
431 sqw = (sqw & 0x0f) | (val << 4);
432 if (i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
433 almon & ~M41T80_ALMON_SQWE) < 0 ||
434 i2c_smbus_write_byte_data(client, M41T80_REG_SQW, sqw) < 0)
435 return -EIO;
436 if (val && i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
437 almon | M41T80_ALMON_SQWE) < 0)
438 return -EIO;
439 return count;
441 static DEVICE_ATTR(sqwfreq, S_IRUGO | S_IWUSR,
442 m41t80_sysfs_show_sqwfreq, m41t80_sysfs_set_sqwfreq);
444 static struct attribute *attrs[] = {
445 &dev_attr_flags.attr,
446 &dev_attr_sqwfreq.attr,
447 NULL,
449 static struct attribute_group attr_group = {
450 .attrs = attrs,
453 static int m41t80_sysfs_register(struct device *dev)
455 return sysfs_create_group(&dev->kobj, &attr_group);
457 #else
458 static int m41t80_sysfs_register(struct device *dev)
460 return 0;
462 #endif
464 #ifdef CONFIG_RTC_DRV_M41T80_WDT
466 *****************************************************************************
468 * Watchdog Driver
470 *****************************************************************************
472 static struct i2c_client *save_client;
474 /* Default margin */
475 #define WD_TIMO 60 /* 1..31 seconds */
477 static int wdt_margin = WD_TIMO;
478 module_param(wdt_margin, int, 0);
479 MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 60s)");
481 static unsigned long wdt_is_open;
482 static int boot_flag;
485 * wdt_ping:
487 * Reload counter one with the watchdog timeout. We don't bother reloading
488 * the cascade counter.
490 static void wdt_ping(void)
492 unsigned char i2c_data[2];
493 struct i2c_msg msgs1[1] = {
495 .addr = save_client->addr,
496 .flags = 0,
497 .len = 2,
498 .buf = i2c_data,
501 i2c_data[0] = 0x09; /* watchdog register */
503 if (wdt_margin > 31)
504 i2c_data[1] = (wdt_margin & 0xFC) | 0x83; /* resolution = 4s */
505 else
507 * WDS = 1 (0x80), mulitplier = WD_TIMO, resolution = 1s (0x02)
509 i2c_data[1] = wdt_margin<<2 | 0x82;
511 i2c_transfer(save_client->adapter, msgs1, 1);
515 * wdt_disable:
517 * disables watchdog.
519 static void wdt_disable(void)
521 unsigned char i2c_data[2], i2c_buf[0x10];
522 struct i2c_msg msgs0[2] = {
524 .addr = save_client->addr,
525 .flags = 0,
526 .len = 1,
527 .buf = i2c_data,
530 .addr = save_client->addr,
531 .flags = I2C_M_RD,
532 .len = 1,
533 .buf = i2c_buf,
536 struct i2c_msg msgs1[1] = {
538 .addr = save_client->addr,
539 .flags = 0,
540 .len = 2,
541 .buf = i2c_data,
545 i2c_data[0] = 0x09;
546 i2c_transfer(save_client->adapter, msgs0, 2);
548 i2c_data[0] = 0x09;
549 i2c_data[1] = 0x00;
550 i2c_transfer(save_client->adapter, msgs1, 1);
554 * wdt_write:
555 * @file: file handle to the watchdog
556 * @buf: buffer to write (unused as data does not matter here
557 * @count: count of bytes
558 * @ppos: pointer to the position to write. No seeks allowed
560 * A write to a watchdog device is defined as a keepalive signal. Any
561 * write of data will do, as we we don't define content meaning.
563 static ssize_t wdt_write(struct file *file, const char __user *buf,
564 size_t count, loff_t *ppos)
566 /* Can't seek (pwrite) on this device
567 if (ppos != &file->f_pos)
568 return -ESPIPE;
570 if (count) {
571 wdt_ping();
572 return 1;
574 return 0;
577 static ssize_t wdt_read(struct file *file, char __user *buf,
578 size_t count, loff_t *ppos)
580 return 0;
584 * wdt_ioctl:
585 * @inode: inode of the device
586 * @file: file handle to the device
587 * @cmd: watchdog command
588 * @arg: argument pointer
590 * The watchdog API defines a common set of functions for all watchdogs
591 * according to their available features. We only actually usefully support
592 * querying capabilities and current status.
594 static int wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
595 unsigned long arg)
597 int new_margin, rv;
598 static struct watchdog_info ident = {
599 .options = WDIOF_POWERUNDER | WDIOF_KEEPALIVEPING |
600 WDIOF_SETTIMEOUT,
601 .firmware_version = 1,
602 .identity = "M41T80 WTD"
605 switch (cmd) {
606 case WDIOC_GETSUPPORT:
607 return copy_to_user((struct watchdog_info __user *)arg, &ident,
608 sizeof(ident)) ? -EFAULT : 0;
610 case WDIOC_GETSTATUS:
611 case WDIOC_GETBOOTSTATUS:
612 return put_user(boot_flag, (int __user *)arg);
613 case WDIOC_KEEPALIVE:
614 wdt_ping();
615 return 0;
616 case WDIOC_SETTIMEOUT:
617 if (get_user(new_margin, (int __user *)arg))
618 return -EFAULT;
619 /* Arbitrary, can't find the card's limits */
620 if (new_margin < 1 || new_margin > 124)
621 return -EINVAL;
622 wdt_margin = new_margin;
623 wdt_ping();
624 /* Fall */
625 case WDIOC_GETTIMEOUT:
626 return put_user(wdt_margin, (int __user *)arg);
628 case WDIOC_SETOPTIONS:
629 if (copy_from_user(&rv, (int __user *)arg, sizeof(int)))
630 return -EFAULT;
632 if (rv & WDIOS_DISABLECARD) {
633 printk(KERN_INFO
634 "rtc-m41t80: disable watchdog\n");
635 wdt_disable();
638 if (rv & WDIOS_ENABLECARD) {
639 printk(KERN_INFO
640 "rtc-m41t80: enable watchdog\n");
641 wdt_ping();
644 return -EINVAL;
646 return -ENOTTY;
650 * wdt_open:
651 * @inode: inode of device
652 * @file: file handle to device
655 static int wdt_open(struct inode *inode, struct file *file)
657 if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) {
658 if (test_and_set_bit(0, &wdt_is_open))
659 return -EBUSY;
661 * Activate
663 wdt_is_open = 1;
664 return 0;
666 return -ENODEV;
670 * wdt_close:
671 * @inode: inode to board
672 * @file: file handle to board
675 static int wdt_release(struct inode *inode, struct file *file)
677 if (MINOR(inode->i_rdev) == WATCHDOG_MINOR)
678 clear_bit(0, &wdt_is_open);
679 return 0;
683 * notify_sys:
684 * @this: our notifier block
685 * @code: the event being reported
686 * @unused: unused
688 * Our notifier is called on system shutdowns. We want to turn the card
689 * off at reboot otherwise the machine will reboot again during memory
690 * test or worse yet during the following fsck. This would suck, in fact
691 * trust me - if it happens it does suck.
693 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
694 void *unused)
696 if (code == SYS_DOWN || code == SYS_HALT)
697 /* Disable Watchdog */
698 wdt_disable();
699 return NOTIFY_DONE;
702 static const struct file_operations wdt_fops = {
703 .owner = THIS_MODULE,
704 .read = wdt_read,
705 .ioctl = wdt_ioctl,
706 .write = wdt_write,
707 .open = wdt_open,
708 .release = wdt_release,
711 static struct miscdevice wdt_dev = {
712 .minor = WATCHDOG_MINOR,
713 .name = "watchdog",
714 .fops = &wdt_fops,
718 * The WDT card needs to learn about soft shutdowns in order to
719 * turn the timebomb registers off.
721 static struct notifier_block wdt_notifier = {
722 .notifier_call = wdt_notify_sys,
724 #endif /* CONFIG_RTC_DRV_M41T80_WDT */
727 *****************************************************************************
729 * Driver Interface
731 *****************************************************************************
733 static int m41t80_probe(struct i2c_client *client,
734 const struct i2c_device_id *id)
736 int rc = 0;
737 struct rtc_device *rtc = NULL;
738 struct rtc_time tm;
739 struct m41t80_data *clientdata = NULL;
741 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C
742 | I2C_FUNC_SMBUS_BYTE_DATA)) {
743 rc = -ENODEV;
744 goto exit;
747 dev_info(&client->dev,
748 "chip found, driver version " DRV_VERSION "\n");
750 clientdata = kzalloc(sizeof(*clientdata), GFP_KERNEL);
751 if (!clientdata) {
752 rc = -ENOMEM;
753 goto exit;
756 rtc = rtc_device_register(client->name, &client->dev,
757 &m41t80_rtc_ops, THIS_MODULE);
758 if (IS_ERR(rtc)) {
759 rc = PTR_ERR(rtc);
760 rtc = NULL;
761 goto exit;
764 clientdata->rtc = rtc;
765 clientdata->features = id->driver_data;
766 i2c_set_clientdata(client, clientdata);
768 /* Make sure HT (Halt Update) bit is cleared */
769 rc = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_HOUR);
770 if (rc < 0)
771 goto ht_err;
773 if (rc & M41T80_ALHOUR_HT) {
774 if (clientdata->features & M41T80_FEATURE_HT) {
775 m41t80_get_datetime(client, &tm);
776 dev_info(&client->dev, "HT bit was set!\n");
777 dev_info(&client->dev,
778 "Power Down at "
779 "%04i-%02i-%02i %02i:%02i:%02i\n",
780 tm.tm_year + 1900,
781 tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
782 tm.tm_min, tm.tm_sec);
784 if (i2c_smbus_write_byte_data(client,
785 M41T80_REG_ALARM_HOUR,
786 rc & ~M41T80_ALHOUR_HT) < 0)
787 goto ht_err;
790 /* Make sure ST (stop) bit is cleared */
791 rc = i2c_smbus_read_byte_data(client, M41T80_REG_SEC);
792 if (rc < 0)
793 goto st_err;
795 if (rc & M41T80_SEC_ST) {
796 if (i2c_smbus_write_byte_data(client, M41T80_REG_SEC,
797 rc & ~M41T80_SEC_ST) < 0)
798 goto st_err;
801 rc = m41t80_sysfs_register(&client->dev);
802 if (rc)
803 goto exit;
805 #ifdef CONFIG_RTC_DRV_M41T80_WDT
806 if (clientdata->features & M41T80_FEATURE_HT) {
807 save_client = client;
808 rc = misc_register(&wdt_dev);
809 if (rc)
810 goto exit;
811 rc = register_reboot_notifier(&wdt_notifier);
812 if (rc) {
813 misc_deregister(&wdt_dev);
814 goto exit;
817 #endif
818 return 0;
820 st_err:
821 rc = -EIO;
822 dev_err(&client->dev, "Can't clear ST bit\n");
823 goto exit;
824 ht_err:
825 rc = -EIO;
826 dev_err(&client->dev, "Can't clear HT bit\n");
827 goto exit;
829 exit:
830 if (rtc)
831 rtc_device_unregister(rtc);
832 kfree(clientdata);
833 return rc;
836 static int m41t80_remove(struct i2c_client *client)
838 struct m41t80_data *clientdata = i2c_get_clientdata(client);
839 struct rtc_device *rtc = clientdata->rtc;
841 #ifdef CONFIG_RTC_DRV_M41T80_WDT
842 if (clientdata->features & M41T80_FEATURE_HT) {
843 misc_deregister(&wdt_dev);
844 unregister_reboot_notifier(&wdt_notifier);
846 #endif
847 if (rtc)
848 rtc_device_unregister(rtc);
849 kfree(clientdata);
851 return 0;
854 static struct i2c_driver m41t80_driver = {
855 .driver = {
856 .name = "rtc-m41t80",
858 .probe = m41t80_probe,
859 .remove = m41t80_remove,
860 .id_table = m41t80_id,
863 static int __init m41t80_rtc_init(void)
865 return i2c_add_driver(&m41t80_driver);
868 static void __exit m41t80_rtc_exit(void)
870 i2c_del_driver(&m41t80_driver);
873 MODULE_AUTHOR("Alexander Bigga <ab@mycable.de>");
874 MODULE_DESCRIPTION("ST Microelectronics M41T80 series RTC I2C Client Driver");
875 MODULE_LICENSE("GPL");
876 MODULE_VERSION(DRV_VERSION);
878 module_init(m41t80_rtc_init);
879 module_exit(m41t80_rtc_exit);