Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mfashe...
[deliverable/linux.git] / drivers / rtc / rtc-m41t80.c
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/module.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/smp_lock.h>
21 #include <linux/string.h>
22 #include <linux/i2c.h>
23 #include <linux/rtc.h>
24 #include <linux/bcd.h>
25 #ifdef CONFIG_RTC_DRV_M41T80_WDT
26 #include <linux/miscdevice.h>
27 #include <linux/watchdog.h>
28 #include <linux/reboot.h>
29 #include <linux/fs.h>
30 #include <linux/ioctl.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 printk(KERN_INFO
635 "rtc-m41t80: disable watchdog\n");
636 wdt_disable();
637 }
638
639 if (rv & WDIOS_ENABLECARD) {
640 printk(KERN_INFO
641 "rtc-m41t80: enable watchdog\n");
642 wdt_ping();
643 }
644
645 return -EINVAL;
646 }
647 return -ENOTTY;
648 }
649
650 /**
651 * wdt_open:
652 * @inode: inode of device
653 * @file: file handle to device
654 *
655 */
656 static int wdt_open(struct inode *inode, struct file *file)
657 {
658 if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) {
659 lock_kernel();
660 if (test_and_set_bit(0, &wdt_is_open)) {
661 unlock_kernel();
662 return -EBUSY;
663 }
664 /*
665 * Activate
666 */
667 wdt_is_open = 1;
668 unlock_kernel();
669 return 0;
670 }
671 return -ENODEV;
672 }
673
674 /**
675 * wdt_close:
676 * @inode: inode to board
677 * @file: file handle to board
678 *
679 */
680 static int wdt_release(struct inode *inode, struct file *file)
681 {
682 if (MINOR(inode->i_rdev) == WATCHDOG_MINOR)
683 clear_bit(0, &wdt_is_open);
684 return 0;
685 }
686
687 /**
688 * notify_sys:
689 * @this: our notifier block
690 * @code: the event being reported
691 * @unused: unused
692 *
693 * Our notifier is called on system shutdowns. We want to turn the card
694 * off at reboot otherwise the machine will reboot again during memory
695 * test or worse yet during the following fsck. This would suck, in fact
696 * trust me - if it happens it does suck.
697 */
698 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
699 void *unused)
700 {
701 if (code == SYS_DOWN || code == SYS_HALT)
702 /* Disable Watchdog */
703 wdt_disable();
704 return NOTIFY_DONE;
705 }
706
707 static const struct file_operations wdt_fops = {
708 .owner = THIS_MODULE,
709 .read = wdt_read,
710 .ioctl = wdt_ioctl,
711 .write = wdt_write,
712 .open = wdt_open,
713 .release = wdt_release,
714 };
715
716 static struct miscdevice wdt_dev = {
717 .minor = WATCHDOG_MINOR,
718 .name = "watchdog",
719 .fops = &wdt_fops,
720 };
721
722 /*
723 * The WDT card needs to learn about soft shutdowns in order to
724 * turn the timebomb registers off.
725 */
726 static struct notifier_block wdt_notifier = {
727 .notifier_call = wdt_notify_sys,
728 };
729 #endif /* CONFIG_RTC_DRV_M41T80_WDT */
730
731 /*
732 *****************************************************************************
733 *
734 * Driver Interface
735 *
736 *****************************************************************************
737 */
738 static int m41t80_probe(struct i2c_client *client,
739 const struct i2c_device_id *id)
740 {
741 int rc = 0;
742 struct rtc_device *rtc = NULL;
743 struct rtc_time tm;
744 struct m41t80_data *clientdata = NULL;
745
746 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C
747 | I2C_FUNC_SMBUS_BYTE_DATA)) {
748 rc = -ENODEV;
749 goto exit;
750 }
751
752 dev_info(&client->dev,
753 "chip found, driver version " DRV_VERSION "\n");
754
755 clientdata = kzalloc(sizeof(*clientdata), GFP_KERNEL);
756 if (!clientdata) {
757 rc = -ENOMEM;
758 goto exit;
759 }
760
761 rtc = rtc_device_register(client->name, &client->dev,
762 &m41t80_rtc_ops, THIS_MODULE);
763 if (IS_ERR(rtc)) {
764 rc = PTR_ERR(rtc);
765 rtc = NULL;
766 goto exit;
767 }
768
769 clientdata->rtc = rtc;
770 clientdata->features = id->driver_data;
771 i2c_set_clientdata(client, clientdata);
772
773 /* Make sure HT (Halt Update) bit is cleared */
774 rc = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_HOUR);
775 if (rc < 0)
776 goto ht_err;
777
778 if (rc & M41T80_ALHOUR_HT) {
779 if (clientdata->features & M41T80_FEATURE_HT) {
780 m41t80_get_datetime(client, &tm);
781 dev_info(&client->dev, "HT bit was set!\n");
782 dev_info(&client->dev,
783 "Power Down at "
784 "%04i-%02i-%02i %02i:%02i:%02i\n",
785 tm.tm_year + 1900,
786 tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
787 tm.tm_min, tm.tm_sec);
788 }
789 if (i2c_smbus_write_byte_data(client,
790 M41T80_REG_ALARM_HOUR,
791 rc & ~M41T80_ALHOUR_HT) < 0)
792 goto ht_err;
793 }
794
795 /* Make sure ST (stop) bit is cleared */
796 rc = i2c_smbus_read_byte_data(client, M41T80_REG_SEC);
797 if (rc < 0)
798 goto st_err;
799
800 if (rc & M41T80_SEC_ST) {
801 if (i2c_smbus_write_byte_data(client, M41T80_REG_SEC,
802 rc & ~M41T80_SEC_ST) < 0)
803 goto st_err;
804 }
805
806 rc = m41t80_sysfs_register(&client->dev);
807 if (rc)
808 goto exit;
809
810 #ifdef CONFIG_RTC_DRV_M41T80_WDT
811 if (clientdata->features & M41T80_FEATURE_HT) {
812 save_client = client;
813 rc = misc_register(&wdt_dev);
814 if (rc)
815 goto exit;
816 rc = register_reboot_notifier(&wdt_notifier);
817 if (rc) {
818 misc_deregister(&wdt_dev);
819 goto exit;
820 }
821 }
822 #endif
823 return 0;
824
825 st_err:
826 rc = -EIO;
827 dev_err(&client->dev, "Can't clear ST bit\n");
828 goto exit;
829 ht_err:
830 rc = -EIO;
831 dev_err(&client->dev, "Can't clear HT bit\n");
832 goto exit;
833
834 exit:
835 if (rtc)
836 rtc_device_unregister(rtc);
837 kfree(clientdata);
838 return rc;
839 }
840
841 static int m41t80_remove(struct i2c_client *client)
842 {
843 struct m41t80_data *clientdata = i2c_get_clientdata(client);
844 struct rtc_device *rtc = clientdata->rtc;
845
846 #ifdef CONFIG_RTC_DRV_M41T80_WDT
847 if (clientdata->features & M41T80_FEATURE_HT) {
848 misc_deregister(&wdt_dev);
849 unregister_reboot_notifier(&wdt_notifier);
850 }
851 #endif
852 if (rtc)
853 rtc_device_unregister(rtc);
854 kfree(clientdata);
855
856 return 0;
857 }
858
859 static struct i2c_driver m41t80_driver = {
860 .driver = {
861 .name = "rtc-m41t80",
862 },
863 .probe = m41t80_probe,
864 .remove = m41t80_remove,
865 .id_table = m41t80_id,
866 };
867
868 static int __init m41t80_rtc_init(void)
869 {
870 return i2c_add_driver(&m41t80_driver);
871 }
872
873 static void __exit m41t80_rtc_exit(void)
874 {
875 i2c_del_driver(&m41t80_driver);
876 }
877
878 MODULE_AUTHOR("Alexander Bigga <ab@mycable.de>");
879 MODULE_DESCRIPTION("ST Microelectronics M41T80 series RTC I2C Client Driver");
880 MODULE_LICENSE("GPL");
881 MODULE_VERSION(DRV_VERSION);
882
883 module_init(m41t80_rtc_init);
884 module_exit(m41t80_rtc_exit);
This page took 0.0517 seconds and 5 git commands to generate.