2 * rtc-ds1307.c - RTC driver for some mostly-compatible I2C chips.
4 * Copyright (C) 2005 James Chapman (ds1337 core)
5 * Copyright (C) 2006 David Brownell
6 * Copyright (C) 2009 Matthias Fuchs (rx8025 support)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/i2c.h>
17 #include <linux/string.h>
18 #include <linux/rtc.h>
19 #include <linux/bcd.h>
23 /* We can't determine type by probing, but if we expect pre-Linux code
24 * to have set the chip up as a clock (turning on the oscillator and
25 * setting the date and time), Linux can ignore the non-clock features.
26 * That's a natural job for a factory or repair bench.
39 // rs5c372 too? different address...
43 /* RTC registers don't differ much, except for the century flag */
44 #define DS1307_REG_SECS 0x00 /* 00-59 */
45 # define DS1307_BIT_CH 0x80
46 # define DS1340_BIT_nEOSC 0x80
47 # define MCP7941X_BIT_ST 0x80
48 #define DS1307_REG_MIN 0x01 /* 00-59 */
49 #define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
50 # define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
51 # define DS1307_BIT_PM 0x20 /* in REG_HOUR */
52 # define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
53 # define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
54 #define DS1307_REG_WDAY 0x03 /* 01-07 */
55 # define MCP7941X_BIT_VBATEN 0x08
56 #define DS1307_REG_MDAY 0x04 /* 01-31 */
57 #define DS1307_REG_MONTH 0x05 /* 01-12 */
58 # define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
59 #define DS1307_REG_YEAR 0x06 /* 00-99 */
61 /* Other registers (control, status, alarms, trickle charge, NVRAM, etc)
62 * start at 7, and they differ a LOT. Only control and status matter for
63 * basic RTC date and time functionality; be careful using them.
65 #define DS1307_REG_CONTROL 0x07 /* or ds1338 */
66 # define DS1307_BIT_OUT 0x80
67 # define DS1338_BIT_OSF 0x20
68 # define DS1307_BIT_SQWE 0x10
69 # define DS1307_BIT_RS1 0x02
70 # define DS1307_BIT_RS0 0x01
71 #define DS1337_REG_CONTROL 0x0e
72 # define DS1337_BIT_nEOSC 0x80
73 # define DS1339_BIT_BBSQI 0x20
74 # define DS3231_BIT_BBSQW 0x40 /* same as BBSQI */
75 # define DS1337_BIT_RS2 0x10
76 # define DS1337_BIT_RS1 0x08
77 # define DS1337_BIT_INTCN 0x04
78 # define DS1337_BIT_A2IE 0x02
79 # define DS1337_BIT_A1IE 0x01
80 #define DS1340_REG_CONTROL 0x07
81 # define DS1340_BIT_OUT 0x80
82 # define DS1340_BIT_FT 0x40
83 # define DS1340_BIT_CALIB_SIGN 0x20
84 # define DS1340_M_CALIBRATION 0x1f
85 #define DS1340_REG_FLAG 0x09
86 # define DS1340_BIT_OSF 0x80
87 #define DS1337_REG_STATUS 0x0f
88 # define DS1337_BIT_OSF 0x80
89 # define DS1337_BIT_A2I 0x02
90 # define DS1337_BIT_A1I 0x01
91 #define DS1339_REG_ALARM1_SECS 0x07
92 #define DS1339_REG_TRICKLE 0x10
94 #define RX8025_REG_CTRL1 0x0e
95 # define RX8025_BIT_2412 0x20
96 #define RX8025_REG_CTRL2 0x0f
97 # define RX8025_BIT_PON 0x10
98 # define RX8025_BIT_VDET 0x40
99 # define RX8025_BIT_XST 0x20
103 u8 offset
; /* register's offset */
107 #define HAS_NVRAM 0 /* bit 0 == sysfs file active */
108 #define HAS_ALARM 1 /* bit 1 == irq claimed */
109 struct i2c_client
*client
;
110 struct rtc_device
*rtc
;
111 struct work_struct work
;
112 s32 (*read_block_data
)(const struct i2c_client
*client
, u8 command
,
113 u8 length
, u8
*values
);
114 s32 (*write_block_data
)(const struct i2c_client
*client
, u8 command
,
115 u8 length
, const u8
*values
);
123 static const struct chip_desc chips
[] = {
148 static const struct i2c_device_id ds1307_id
[] = {
149 { "ds1307", ds_1307
},
150 { "ds1337", ds_1337
},
151 { "ds1338", ds_1338
},
152 { "ds1339", ds_1339
},
153 { "ds1388", ds_1388
},
154 { "ds1340", ds_1340
},
155 { "ds3231", ds_3231
},
156 { "m41t00", m41t00
},
157 { "mcp7941x", mcp7941x
},
158 { "pt7c4338", ds_1307
},
159 { "rx8025", rx_8025
},
162 MODULE_DEVICE_TABLE(i2c
, ds1307_id
);
164 /*----------------------------------------------------------------------*/
166 #define BLOCK_DATA_MAX_TRIES 10
168 static s32
ds1307_read_block_data_once(const struct i2c_client
*client
,
169 u8 command
, u8 length
, u8
*values
)
173 for (i
= 0; i
< length
; i
++) {
174 data
= i2c_smbus_read_byte_data(client
, command
+ i
);
182 static s32
ds1307_read_block_data(const struct i2c_client
*client
, u8 command
,
183 u8 length
, u8
*values
)
185 u8 oldvalues
[I2C_SMBUS_BLOCK_MAX
];
189 dev_dbg(&client
->dev
, "ds1307_read_block_data (length=%d)\n", length
);
190 ret
= ds1307_read_block_data_once(client
, command
, length
, values
);
194 if (++tries
> BLOCK_DATA_MAX_TRIES
) {
195 dev_err(&client
->dev
,
196 "ds1307_read_block_data failed\n");
199 memcpy(oldvalues
, values
, length
);
200 ret
= ds1307_read_block_data_once(client
, command
, length
,
204 } while (memcmp(oldvalues
, values
, length
));
208 static s32
ds1307_write_block_data(const struct i2c_client
*client
, u8 command
,
209 u8 length
, const u8
*values
)
211 u8 currvalues
[I2C_SMBUS_BLOCK_MAX
];
214 dev_dbg(&client
->dev
, "ds1307_write_block_data (length=%d)\n", length
);
218 if (++tries
> BLOCK_DATA_MAX_TRIES
) {
219 dev_err(&client
->dev
,
220 "ds1307_write_block_data failed\n");
223 for (i
= 0; i
< length
; i
++) {
224 ret
= i2c_smbus_write_byte_data(client
, command
+ i
,
229 ret
= ds1307_read_block_data_once(client
, command
, length
,
233 } while (memcmp(currvalues
, values
, length
));
237 /*----------------------------------------------------------------------*/
240 * The IRQ logic includes a "real" handler running in IRQ context just
241 * long enough to schedule this workqueue entry. We need a task context
242 * to talk to the RTC, since I2C I/O calls require that; and disable the
243 * IRQ until we clear its status on the chip, so that this handler can
244 * work with any type of triggering (not just falling edge).
246 * The ds1337 and ds1339 both have two alarms, but we only use the first
247 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
248 * signal; ds1339 chips have only one alarm signal.
250 static void ds1307_work(struct work_struct
*work
)
252 struct ds1307
*ds1307
;
253 struct i2c_client
*client
;
257 ds1307
= container_of(work
, struct ds1307
, work
);
258 client
= ds1307
->client
;
259 lock
= &ds1307
->rtc
->ops_lock
;
262 stat
= i2c_smbus_read_byte_data(client
, DS1337_REG_STATUS
);
266 if (stat
& DS1337_BIT_A1I
) {
267 stat
&= ~DS1337_BIT_A1I
;
268 i2c_smbus_write_byte_data(client
, DS1337_REG_STATUS
, stat
);
270 control
= i2c_smbus_read_byte_data(client
, DS1337_REG_CONTROL
);
274 control
&= ~DS1337_BIT_A1IE
;
275 i2c_smbus_write_byte_data(client
, DS1337_REG_CONTROL
, control
);
277 rtc_update_irq(ds1307
->rtc
, 1, RTC_AF
| RTC_IRQF
);
281 if (test_bit(HAS_ALARM
, &ds1307
->flags
))
282 enable_irq(client
->irq
);
286 static irqreturn_t
ds1307_irq(int irq
, void *dev_id
)
288 struct i2c_client
*client
= dev_id
;
289 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
291 disable_irq_nosync(irq
);
292 schedule_work(&ds1307
->work
);
296 /*----------------------------------------------------------------------*/
298 static int ds1307_get_time(struct device
*dev
, struct rtc_time
*t
)
300 struct ds1307
*ds1307
= dev_get_drvdata(dev
);
303 /* read the RTC date and time registers all at once */
304 tmp
= ds1307
->read_block_data(ds1307
->client
,
305 ds1307
->offset
, 7, ds1307
->regs
);
307 dev_err(dev
, "%s error %d\n", "read", tmp
);
311 dev_dbg(dev
, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
313 ds1307
->regs
[0], ds1307
->regs
[1],
314 ds1307
->regs
[2], ds1307
->regs
[3],
315 ds1307
->regs
[4], ds1307
->regs
[5],
318 t
->tm_sec
= bcd2bin(ds1307
->regs
[DS1307_REG_SECS
] & 0x7f);
319 t
->tm_min
= bcd2bin(ds1307
->regs
[DS1307_REG_MIN
] & 0x7f);
320 tmp
= ds1307
->regs
[DS1307_REG_HOUR
] & 0x3f;
321 t
->tm_hour
= bcd2bin(tmp
);
322 t
->tm_wday
= bcd2bin(ds1307
->regs
[DS1307_REG_WDAY
] & 0x07) - 1;
323 t
->tm_mday
= bcd2bin(ds1307
->regs
[DS1307_REG_MDAY
] & 0x3f);
324 tmp
= ds1307
->regs
[DS1307_REG_MONTH
] & 0x1f;
325 t
->tm_mon
= bcd2bin(tmp
) - 1;
327 /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
328 t
->tm_year
= bcd2bin(ds1307
->regs
[DS1307_REG_YEAR
]) + 100;
330 dev_dbg(dev
, "%s secs=%d, mins=%d, "
331 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
332 "read", t
->tm_sec
, t
->tm_min
,
333 t
->tm_hour
, t
->tm_mday
,
334 t
->tm_mon
, t
->tm_year
, t
->tm_wday
);
336 /* initial clock setting can be undefined */
337 return rtc_valid_tm(t
);
340 static int ds1307_set_time(struct device
*dev
, struct rtc_time
*t
)
342 struct ds1307
*ds1307
= dev_get_drvdata(dev
);
345 u8
*buf
= ds1307
->regs
;
347 dev_dbg(dev
, "%s secs=%d, mins=%d, "
348 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
349 "write", t
->tm_sec
, t
->tm_min
,
350 t
->tm_hour
, t
->tm_mday
,
351 t
->tm_mon
, t
->tm_year
, t
->tm_wday
);
353 buf
[DS1307_REG_SECS
] = bin2bcd(t
->tm_sec
);
354 buf
[DS1307_REG_MIN
] = bin2bcd(t
->tm_min
);
355 buf
[DS1307_REG_HOUR
] = bin2bcd(t
->tm_hour
);
356 buf
[DS1307_REG_WDAY
] = bin2bcd(t
->tm_wday
+ 1);
357 buf
[DS1307_REG_MDAY
] = bin2bcd(t
->tm_mday
);
358 buf
[DS1307_REG_MONTH
] = bin2bcd(t
->tm_mon
+ 1);
360 /* assume 20YY not 19YY */
361 tmp
= t
->tm_year
- 100;
362 buf
[DS1307_REG_YEAR
] = bin2bcd(tmp
);
364 switch (ds1307
->type
) {
368 buf
[DS1307_REG_MONTH
] |= DS1337_BIT_CENTURY
;
371 buf
[DS1307_REG_HOUR
] |= DS1340_BIT_CENTURY_EN
372 | DS1340_BIT_CENTURY
;
375 buf
[DS1307_REG_SECS
] |= MCP7941X_BIT_ST
;
376 buf
[DS1307_REG_WDAY
] |= MCP7941X_BIT_VBATEN
;
382 dev_dbg(dev
, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
383 "write", buf
[0], buf
[1], buf
[2], buf
[3],
384 buf
[4], buf
[5], buf
[6]);
386 result
= ds1307
->write_block_data(ds1307
->client
,
387 ds1307
->offset
, 7, buf
);
389 dev_err(dev
, "%s error %d\n", "write", result
);
395 static int ds1337_read_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
397 struct i2c_client
*client
= to_i2c_client(dev
);
398 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
401 if (!test_bit(HAS_ALARM
, &ds1307
->flags
))
404 /* read all ALARM1, ALARM2, and status registers at once */
405 ret
= ds1307
->read_block_data(client
,
406 DS1339_REG_ALARM1_SECS
, 9, ds1307
->regs
);
408 dev_err(dev
, "%s error %d\n", "alarm read", ret
);
412 dev_dbg(dev
, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n",
414 ds1307
->regs
[0], ds1307
->regs
[1],
415 ds1307
->regs
[2], ds1307
->regs
[3],
416 ds1307
->regs
[4], ds1307
->regs
[5],
417 ds1307
->regs
[6], ds1307
->regs
[7],
420 /* report alarm time (ALARM1); assume 24 hour and day-of-month modes,
421 * and that all four fields are checked matches
423 t
->time
.tm_sec
= bcd2bin(ds1307
->regs
[0] & 0x7f);
424 t
->time
.tm_min
= bcd2bin(ds1307
->regs
[1] & 0x7f);
425 t
->time
.tm_hour
= bcd2bin(ds1307
->regs
[2] & 0x3f);
426 t
->time
.tm_mday
= bcd2bin(ds1307
->regs
[3] & 0x3f);
428 t
->time
.tm_year
= -1;
429 t
->time
.tm_wday
= -1;
430 t
->time
.tm_yday
= -1;
431 t
->time
.tm_isdst
= -1;
434 t
->enabled
= !!(ds1307
->regs
[7] & DS1337_BIT_A1IE
);
435 t
->pending
= !!(ds1307
->regs
[8] & DS1337_BIT_A1I
);
437 dev_dbg(dev
, "%s secs=%d, mins=%d, "
438 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
439 "alarm read", t
->time
.tm_sec
, t
->time
.tm_min
,
440 t
->time
.tm_hour
, t
->time
.tm_mday
,
441 t
->enabled
, t
->pending
);
446 static int ds1337_set_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
448 struct i2c_client
*client
= to_i2c_client(dev
);
449 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
450 unsigned char *buf
= ds1307
->regs
;
454 if (!test_bit(HAS_ALARM
, &ds1307
->flags
))
457 dev_dbg(dev
, "%s secs=%d, mins=%d, "
458 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
459 "alarm set", t
->time
.tm_sec
, t
->time
.tm_min
,
460 t
->time
.tm_hour
, t
->time
.tm_mday
,
461 t
->enabled
, t
->pending
);
463 /* read current status of both alarms and the chip */
464 ret
= ds1307
->read_block_data(client
,
465 DS1339_REG_ALARM1_SECS
, 9, buf
);
467 dev_err(dev
, "%s error %d\n", "alarm write", ret
);
470 control
= ds1307
->regs
[7];
471 status
= ds1307
->regs
[8];
473 dev_dbg(dev
, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n",
474 "alarm set (old status)",
475 ds1307
->regs
[0], ds1307
->regs
[1],
476 ds1307
->regs
[2], ds1307
->regs
[3],
477 ds1307
->regs
[4], ds1307
->regs
[5],
478 ds1307
->regs
[6], control
, status
);
480 /* set ALARM1, using 24 hour and day-of-month modes */
481 buf
[0] = bin2bcd(t
->time
.tm_sec
);
482 buf
[1] = bin2bcd(t
->time
.tm_min
);
483 buf
[2] = bin2bcd(t
->time
.tm_hour
);
484 buf
[3] = bin2bcd(t
->time
.tm_mday
);
486 /* set ALARM2 to non-garbage */
491 /* optionally enable ALARM1 */
492 buf
[7] = control
& ~(DS1337_BIT_A1IE
| DS1337_BIT_A2IE
);
494 dev_dbg(dev
, "alarm IRQ armed\n");
495 buf
[7] |= DS1337_BIT_A1IE
; /* only ALARM1 is used */
497 buf
[8] = status
& ~(DS1337_BIT_A1I
| DS1337_BIT_A2I
);
499 ret
= ds1307
->write_block_data(client
,
500 DS1339_REG_ALARM1_SECS
, 9, buf
);
502 dev_err(dev
, "can't set alarm time\n");
509 static int ds1307_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
511 struct i2c_client
*client
= to_i2c_client(dev
);
512 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
515 if (!test_bit(HAS_ALARM
, &ds1307
->flags
))
518 ret
= i2c_smbus_read_byte_data(client
, DS1337_REG_CONTROL
);
523 ret
|= DS1337_BIT_A1IE
;
525 ret
&= ~DS1337_BIT_A1IE
;
527 ret
= i2c_smbus_write_byte_data(client
, DS1337_REG_CONTROL
, ret
);
534 static const struct rtc_class_ops ds13xx_rtc_ops
= {
535 .read_time
= ds1307_get_time
,
536 .set_time
= ds1307_set_time
,
537 .read_alarm
= ds1337_read_alarm
,
538 .set_alarm
= ds1337_set_alarm
,
539 .alarm_irq_enable
= ds1307_alarm_irq_enable
,
542 /*----------------------------------------------------------------------*/
544 #define NVRAM_SIZE 56
547 ds1307_nvram_read(struct file
*filp
, struct kobject
*kobj
,
548 struct bin_attribute
*attr
,
549 char *buf
, loff_t off
, size_t count
)
551 struct i2c_client
*client
;
552 struct ds1307
*ds1307
;
555 client
= kobj_to_i2c_client(kobj
);
556 ds1307
= i2c_get_clientdata(client
);
558 if (unlikely(off
>= NVRAM_SIZE
))
560 if ((off
+ count
) > NVRAM_SIZE
)
561 count
= NVRAM_SIZE
- off
;
562 if (unlikely(!count
))
565 result
= ds1307
->read_block_data(client
, 8 + off
, count
, buf
);
567 dev_err(&client
->dev
, "%s error %d\n", "nvram read", result
);
572 ds1307_nvram_write(struct file
*filp
, struct kobject
*kobj
,
573 struct bin_attribute
*attr
,
574 char *buf
, loff_t off
, size_t count
)
576 struct i2c_client
*client
;
577 struct ds1307
*ds1307
;
580 client
= kobj_to_i2c_client(kobj
);
581 ds1307
= i2c_get_clientdata(client
);
583 if (unlikely(off
>= NVRAM_SIZE
))
585 if ((off
+ count
) > NVRAM_SIZE
)
586 count
= NVRAM_SIZE
- off
;
587 if (unlikely(!count
))
590 result
= ds1307
->write_block_data(client
, 8 + off
, count
, buf
);
592 dev_err(&client
->dev
, "%s error %d\n", "nvram write", result
);
598 static struct bin_attribute nvram
= {
601 .mode
= S_IRUGO
| S_IWUSR
,
604 .read
= ds1307_nvram_read
,
605 .write
= ds1307_nvram_write
,
609 /*----------------------------------------------------------------------*/
611 static struct i2c_driver ds1307_driver
;
613 static int __devinit
ds1307_probe(struct i2c_client
*client
,
614 const struct i2c_device_id
*id
)
616 struct ds1307
*ds1307
;
619 const struct chip_desc
*chip
= &chips
[id
->driver_data
];
620 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
621 int want_irq
= false;
623 static const int bbsqi_bitpos
[] = {
625 [ds_1339
] = DS1339_BIT_BBSQI
,
626 [ds_3231
] = DS3231_BIT_BBSQW
,
629 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)
630 && !i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_I2C_BLOCK
))
633 if (!(ds1307
= kzalloc(sizeof(struct ds1307
), GFP_KERNEL
)))
636 i2c_set_clientdata(client
, ds1307
);
638 ds1307
->client
= client
;
639 ds1307
->type
= id
->driver_data
;
643 if (i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_I2C_BLOCK
)) {
644 ds1307
->read_block_data
= i2c_smbus_read_i2c_block_data
;
645 ds1307
->write_block_data
= i2c_smbus_write_i2c_block_data
;
647 ds1307
->read_block_data
= ds1307_read_block_data
;
648 ds1307
->write_block_data
= ds1307_write_block_data
;
651 switch (ds1307
->type
) {
656 if (ds1307
->client
->irq
> 0 && chip
->alarm
) {
657 INIT_WORK(&ds1307
->work
, ds1307_work
);
660 /* get registers that the "rtc" read below won't read... */
661 tmp
= ds1307
->read_block_data(ds1307
->client
,
662 DS1337_REG_CONTROL
, 2, buf
);
664 pr_debug("read error %d\n", tmp
);
669 /* oscillator off? turn it on, so clock can tick. */
670 if (ds1307
->regs
[0] & DS1337_BIT_nEOSC
)
671 ds1307
->regs
[0] &= ~DS1337_BIT_nEOSC
;
673 /* Using IRQ? Disable the square wave and both alarms.
674 * For some variants, be sure alarms can trigger when we're
675 * running on Vbackup (BBSQI/BBSQW)
678 ds1307
->regs
[0] |= DS1337_BIT_INTCN
679 | bbsqi_bitpos
[ds1307
->type
];
680 ds1307
->regs
[0] &= ~(DS1337_BIT_A2IE
| DS1337_BIT_A1IE
);
683 i2c_smbus_write_byte_data(client
, DS1337_REG_CONTROL
,
686 /* oscillator fault? clear flag, and warn */
687 if (ds1307
->regs
[1] & DS1337_BIT_OSF
) {
688 i2c_smbus_write_byte_data(client
, DS1337_REG_STATUS
,
689 ds1307
->regs
[1] & ~DS1337_BIT_OSF
);
690 dev_warn(&client
->dev
, "SET TIME!\n");
695 tmp
= i2c_smbus_read_i2c_block_data(ds1307
->client
,
696 RX8025_REG_CTRL1
<< 4 | 0x08, 2, buf
);
698 pr_debug("read error %d\n", tmp
);
703 /* oscillator off? turn it on, so clock can tick. */
704 if (!(ds1307
->regs
[1] & RX8025_BIT_XST
)) {
705 ds1307
->regs
[1] |= RX8025_BIT_XST
;
706 i2c_smbus_write_byte_data(client
,
707 RX8025_REG_CTRL2
<< 4 | 0x08,
709 dev_warn(&client
->dev
,
710 "oscillator stop detected - SET TIME!\n");
713 if (ds1307
->regs
[1] & RX8025_BIT_PON
) {
714 ds1307
->regs
[1] &= ~RX8025_BIT_PON
;
715 i2c_smbus_write_byte_data(client
,
716 RX8025_REG_CTRL2
<< 4 | 0x08,
718 dev_warn(&client
->dev
, "power-on detected\n");
721 if (ds1307
->regs
[1] & RX8025_BIT_VDET
) {
722 ds1307
->regs
[1] &= ~RX8025_BIT_VDET
;
723 i2c_smbus_write_byte_data(client
,
724 RX8025_REG_CTRL2
<< 4 | 0x08,
726 dev_warn(&client
->dev
, "voltage drop detected\n");
729 /* make sure we are running in 24hour mode */
730 if (!(ds1307
->regs
[0] & RX8025_BIT_2412
)) {
733 /* switch to 24 hour mode */
734 i2c_smbus_write_byte_data(client
,
735 RX8025_REG_CTRL1
<< 4 | 0x08,
739 tmp
= i2c_smbus_read_i2c_block_data(ds1307
->client
,
740 RX8025_REG_CTRL1
<< 4 | 0x08, 2, buf
);
742 pr_debug("read error %d\n", tmp
);
748 hour
= bcd2bin(ds1307
->regs
[DS1307_REG_HOUR
]);
751 if (ds1307
->regs
[DS1307_REG_HOUR
] & DS1307_BIT_PM
)
754 i2c_smbus_write_byte_data(client
,
755 DS1307_REG_HOUR
<< 4 | 0x08,
760 ds1307
->offset
= 1; /* Seconds starts at 1 */
767 /* read RTC registers */
768 tmp
= ds1307
->read_block_data(ds1307
->client
, ds1307
->offset
, 8, buf
);
770 pr_debug("read error %d\n", tmp
);
775 /* minimal sanity checking; some chips (like DS1340) don't
776 * specify the extra bits as must-be-zero, but there are
777 * still a few values that are clearly out-of-range.
779 tmp
= ds1307
->regs
[DS1307_REG_SECS
];
780 switch (ds1307
->type
) {
783 /* clock halted? turn it on, so clock can tick. */
784 if (tmp
& DS1307_BIT_CH
) {
785 i2c_smbus_write_byte_data(client
, DS1307_REG_SECS
, 0);
786 dev_warn(&client
->dev
, "SET TIME!\n");
791 /* clock halted? turn it on, so clock can tick. */
792 if (tmp
& DS1307_BIT_CH
)
793 i2c_smbus_write_byte_data(client
, DS1307_REG_SECS
, 0);
795 /* oscillator fault? clear flag, and warn */
796 if (ds1307
->regs
[DS1307_REG_CONTROL
] & DS1338_BIT_OSF
) {
797 i2c_smbus_write_byte_data(client
, DS1307_REG_CONTROL
,
798 ds1307
->regs
[DS1307_REG_CONTROL
]
800 dev_warn(&client
->dev
, "SET TIME!\n");
805 /* clock halted? turn it on, so clock can tick. */
806 if (tmp
& DS1340_BIT_nEOSC
)
807 i2c_smbus_write_byte_data(client
, DS1307_REG_SECS
, 0);
809 tmp
= i2c_smbus_read_byte_data(client
, DS1340_REG_FLAG
);
811 pr_debug("read error %d\n", tmp
);
816 /* oscillator fault? clear flag, and warn */
817 if (tmp
& DS1340_BIT_OSF
) {
818 i2c_smbus_write_byte_data(client
, DS1340_REG_FLAG
, 0);
819 dev_warn(&client
->dev
, "SET TIME!\n");
823 /* make sure that the backup battery is enabled */
824 if (!(ds1307
->regs
[DS1307_REG_WDAY
] & MCP7941X_BIT_VBATEN
)) {
825 i2c_smbus_write_byte_data(client
, DS1307_REG_WDAY
,
826 ds1307
->regs
[DS1307_REG_WDAY
]
827 | MCP7941X_BIT_VBATEN
);
830 /* clock halted? turn it on, so clock can tick. */
831 if (!(tmp
& MCP7941X_BIT_ST
)) {
832 i2c_smbus_write_byte_data(client
, DS1307_REG_SECS
,
834 dev_warn(&client
->dev
, "SET TIME!\n");
847 tmp
= ds1307
->regs
[DS1307_REG_HOUR
];
848 switch (ds1307
->type
) {
851 /* NOTE: ignores century bits; fix before deploying
852 * systems that will run through year 2100.
858 if (!(tmp
& DS1307_BIT_12HR
))
861 /* Be sure we're in 24 hour mode. Multi-master systems
864 tmp
= bcd2bin(tmp
& 0x1f);
867 if (ds1307
->regs
[DS1307_REG_HOUR
] & DS1307_BIT_PM
)
869 i2c_smbus_write_byte_data(client
,
870 ds1307
->offset
+ DS1307_REG_HOUR
,
874 ds1307
->rtc
= rtc_device_register(client
->name
, &client
->dev
,
875 &ds13xx_rtc_ops
, THIS_MODULE
);
876 if (IS_ERR(ds1307
->rtc
)) {
877 err
= PTR_ERR(ds1307
->rtc
);
878 dev_err(&client
->dev
,
879 "unable to register the class device\n");
884 err
= request_irq(client
->irq
, ds1307_irq
, IRQF_SHARED
,
885 ds1307
->rtc
->name
, client
);
887 dev_err(&client
->dev
,
888 "unable to request IRQ!\n");
892 device_set_wakeup_capable(&client
->dev
, 1);
893 set_bit(HAS_ALARM
, &ds1307
->flags
);
894 dev_dbg(&client
->dev
, "got IRQ %d\n", client
->irq
);
898 err
= sysfs_create_bin_file(&client
->dev
.kobj
, &nvram
);
900 set_bit(HAS_NVRAM
, &ds1307
->flags
);
901 dev_info(&client
->dev
, "56 bytes nvram\n");
908 rtc_device_unregister(ds1307
->rtc
);
914 static int __devexit
ds1307_remove(struct i2c_client
*client
)
916 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
918 if (test_and_clear_bit(HAS_ALARM
, &ds1307
->flags
)) {
919 free_irq(client
->irq
, client
);
920 cancel_work_sync(&ds1307
->work
);
923 if (test_and_clear_bit(HAS_NVRAM
, &ds1307
->flags
))
924 sysfs_remove_bin_file(&client
->dev
.kobj
, &nvram
);
926 rtc_device_unregister(ds1307
->rtc
);
931 static struct i2c_driver ds1307_driver
= {
933 .name
= "rtc-ds1307",
934 .owner
= THIS_MODULE
,
936 .probe
= ds1307_probe
,
937 .remove
= __devexit_p(ds1307_remove
),
938 .id_table
= ds1307_id
,
941 static int __init
ds1307_init(void)
943 return i2c_add_driver(&ds1307_driver
);
945 module_init(ds1307_init
);
947 static void __exit
ds1307_exit(void)
949 i2c_del_driver(&ds1307_driver
);
951 module_exit(ds1307_exit
);
953 MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
954 MODULE_LICENSE("GPL");