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
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/i2c.h>
16 #include <linux/string.h>
17 #include <linux/rtc.h>
18 #include <linux/bcd.h>
22 /* We can't determine type by probing, but if we expect pre-Linux code
23 * to have set the chip up as a clock (turning on the oscillator and
24 * setting the date and time), Linux can ignore the non-clock features.
25 * That's a natural job for a factory or repair bench.
34 // rs5c372 too? different address...
38 /* RTC registers don't differ much, except for the century flag */
39 #define DS1307_REG_SECS 0x00 /* 00-59 */
40 # define DS1307_BIT_CH 0x80
41 # define DS1340_BIT_nEOSC 0x80
42 #define DS1307_REG_MIN 0x01 /* 00-59 */
43 #define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
44 # define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
45 # define DS1307_BIT_PM 0x20 /* in REG_HOUR */
46 # define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
47 # define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
48 #define DS1307_REG_WDAY 0x03 /* 01-07 */
49 #define DS1307_REG_MDAY 0x04 /* 01-31 */
50 #define DS1307_REG_MONTH 0x05 /* 01-12 */
51 # define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
52 #define DS1307_REG_YEAR 0x06 /* 00-99 */
54 /* Other registers (control, status, alarms, trickle charge, NVRAM, etc)
55 * start at 7, and they differ a LOT. Only control and status matter for
56 * basic RTC date and time functionality; be careful using them.
58 #define DS1307_REG_CONTROL 0x07 /* or ds1338 */
59 # define DS1307_BIT_OUT 0x80
60 # define DS1338_BIT_OSF 0x20
61 # define DS1307_BIT_SQWE 0x10
62 # define DS1307_BIT_RS1 0x02
63 # define DS1307_BIT_RS0 0x01
64 #define DS1337_REG_CONTROL 0x0e
65 # define DS1337_BIT_nEOSC 0x80
66 # define DS1339_BIT_BBSQI 0x20
67 # define DS1337_BIT_RS2 0x10
68 # define DS1337_BIT_RS1 0x08
69 # define DS1337_BIT_INTCN 0x04
70 # define DS1337_BIT_A2IE 0x02
71 # define DS1337_BIT_A1IE 0x01
72 #define DS1340_REG_CONTROL 0x07
73 # define DS1340_BIT_OUT 0x80
74 # define DS1340_BIT_FT 0x40
75 # define DS1340_BIT_CALIB_SIGN 0x20
76 # define DS1340_M_CALIBRATION 0x1f
77 #define DS1340_REG_FLAG 0x09
78 # define DS1340_BIT_OSF 0x80
79 #define DS1337_REG_STATUS 0x0f
80 # define DS1337_BIT_OSF 0x80
81 # define DS1337_BIT_A2I 0x02
82 # define DS1337_BIT_A1I 0x01
83 #define DS1339_REG_ALARM1_SECS 0x07
84 #define DS1339_REG_TRICKLE 0x10
93 #define HAS_NVRAM 0 /* bit 0 == sysfs file active */
94 #define HAS_ALARM 1 /* bit 1 == irq claimed */
95 struct i2c_msg msg
[2];
96 struct i2c_client
*client
;
97 struct rtc_device
*rtc
;
98 struct work_struct work
;
106 static const struct chip_desc chips
[] = {
124 static const struct i2c_device_id ds1307_id
[] = {
125 { "ds1307", ds_1307
},
126 { "ds1337", ds_1337
},
127 { "ds1338", ds_1338
},
128 { "ds1339", ds_1339
},
129 { "ds1340", ds_1340
},
130 { "m41t00", m41t00
},
133 MODULE_DEVICE_TABLE(i2c
, ds1307_id
);
135 /*----------------------------------------------------------------------*/
138 * The IRQ logic includes a "real" handler running in IRQ context just
139 * long enough to schedule this workqueue entry. We need a task context
140 * to talk to the RTC, since I2C I/O calls require that; and disable the
141 * IRQ until we clear its status on the chip, so that this handler can
142 * work with any type of triggering (not just falling edge).
144 * The ds1337 and ds1339 both have two alarms, but we only use the first
145 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
146 * signal; ds1339 chips have only one alarm signal.
148 static void ds1307_work(struct work_struct
*work
)
150 struct ds1307
*ds1307
;
151 struct i2c_client
*client
;
155 ds1307
= container_of(work
, struct ds1307
, work
);
156 client
= ds1307
->client
;
157 lock
= &ds1307
->rtc
->ops_lock
;
160 stat
= i2c_smbus_read_byte_data(client
, DS1337_REG_STATUS
);
164 if (stat
& DS1337_BIT_A1I
) {
165 stat
&= ~DS1337_BIT_A1I
;
166 i2c_smbus_write_byte_data(client
, DS1337_REG_STATUS
, stat
);
168 control
= i2c_smbus_read_byte_data(client
, DS1337_REG_CONTROL
);
172 control
&= ~DS1337_BIT_A1IE
;
173 i2c_smbus_write_byte_data(client
, DS1337_REG_CONTROL
, control
);
175 /* rtc_update_irq() assumes that it is called
176 * from IRQ-disabled context.
179 rtc_update_irq(ds1307
->rtc
, 1, RTC_AF
| RTC_IRQF
);
184 if (test_bit(HAS_ALARM
, &ds1307
->flags
))
185 enable_irq(client
->irq
);
189 static irqreturn_t
ds1307_irq(int irq
, void *dev_id
)
191 struct i2c_client
*client
= dev_id
;
192 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
194 disable_irq_nosync(irq
);
195 schedule_work(&ds1307
->work
);
199 /*----------------------------------------------------------------------*/
201 static int ds1307_get_time(struct device
*dev
, struct rtc_time
*t
)
203 struct ds1307
*ds1307
= dev_get_drvdata(dev
);
206 /* read the RTC date and time registers all at once */
207 ds1307
->reg_addr
= 0;
208 ds1307
->msg
[1].flags
= I2C_M_RD
;
209 ds1307
->msg
[1].len
= 7;
211 tmp
= i2c_transfer(to_i2c_adapter(ds1307
->client
->dev
.parent
),
214 dev_err(dev
, "%s error %d\n", "read", tmp
);
218 dev_dbg(dev
, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
220 ds1307
->regs
[0], ds1307
->regs
[1],
221 ds1307
->regs
[2], ds1307
->regs
[3],
222 ds1307
->regs
[4], ds1307
->regs
[5],
225 t
->tm_sec
= bcd2bin(ds1307
->regs
[DS1307_REG_SECS
] & 0x7f);
226 t
->tm_min
= bcd2bin(ds1307
->regs
[DS1307_REG_MIN
] & 0x7f);
227 tmp
= ds1307
->regs
[DS1307_REG_HOUR
] & 0x3f;
228 t
->tm_hour
= bcd2bin(tmp
);
229 t
->tm_wday
= bcd2bin(ds1307
->regs
[DS1307_REG_WDAY
] & 0x07) - 1;
230 t
->tm_mday
= bcd2bin(ds1307
->regs
[DS1307_REG_MDAY
] & 0x3f);
231 tmp
= ds1307
->regs
[DS1307_REG_MONTH
] & 0x1f;
232 t
->tm_mon
= bcd2bin(tmp
) - 1;
234 /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
235 t
->tm_year
= bcd2bin(ds1307
->regs
[DS1307_REG_YEAR
]) + 100;
237 dev_dbg(dev
, "%s secs=%d, mins=%d, "
238 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
239 "read", t
->tm_sec
, t
->tm_min
,
240 t
->tm_hour
, t
->tm_mday
,
241 t
->tm_mon
, t
->tm_year
, t
->tm_wday
);
243 /* initial clock setting can be undefined */
244 return rtc_valid_tm(t
);
247 static int ds1307_set_time(struct device
*dev
, struct rtc_time
*t
)
249 struct ds1307
*ds1307
= dev_get_drvdata(dev
);
252 u8
*buf
= ds1307
->regs
;
254 dev_dbg(dev
, "%s secs=%d, mins=%d, "
255 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
256 "write", t
->tm_sec
, t
->tm_min
,
257 t
->tm_hour
, t
->tm_mday
,
258 t
->tm_mon
, t
->tm_year
, t
->tm_wday
);
260 *buf
++ = 0; /* first register addr */
261 buf
[DS1307_REG_SECS
] = bin2bcd(t
->tm_sec
);
262 buf
[DS1307_REG_MIN
] = bin2bcd(t
->tm_min
);
263 buf
[DS1307_REG_HOUR
] = bin2bcd(t
->tm_hour
);
264 buf
[DS1307_REG_WDAY
] = bin2bcd(t
->tm_wday
+ 1);
265 buf
[DS1307_REG_MDAY
] = bin2bcd(t
->tm_mday
);
266 buf
[DS1307_REG_MONTH
] = bin2bcd(t
->tm_mon
+ 1);
268 /* assume 20YY not 19YY */
269 tmp
= t
->tm_year
- 100;
270 buf
[DS1307_REG_YEAR
] = bin2bcd(tmp
);
272 switch (ds1307
->type
) {
275 buf
[DS1307_REG_MONTH
] |= DS1337_BIT_CENTURY
;
278 buf
[DS1307_REG_HOUR
] |= DS1340_BIT_CENTURY_EN
279 | DS1340_BIT_CENTURY
;
285 ds1307
->msg
[1].flags
= 0;
286 ds1307
->msg
[1].len
= 8;
288 dev_dbg(dev
, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
289 "write", buf
[0], buf
[1], buf
[2], buf
[3],
290 buf
[4], buf
[5], buf
[6]);
292 result
= i2c_transfer(to_i2c_adapter(ds1307
->client
->dev
.parent
),
295 dev_err(dev
, "%s error %d\n", "write", tmp
);
301 static int ds1307_read_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
303 struct i2c_client
*client
= to_i2c_client(dev
);
304 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
307 if (!test_bit(HAS_ALARM
, &ds1307
->flags
))
310 /* read all ALARM1, ALARM2, and status registers at once */
311 ds1307
->reg_addr
= DS1339_REG_ALARM1_SECS
;
312 ds1307
->msg
[1].flags
= I2C_M_RD
;
313 ds1307
->msg
[1].len
= 9;
315 ret
= i2c_transfer(to_i2c_adapter(client
->dev
.parent
),
318 dev_err(dev
, "%s error %d\n", "alarm read", ret
);
322 dev_dbg(dev
, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n",
324 ds1307
->regs
[0], ds1307
->regs
[1],
325 ds1307
->regs
[2], ds1307
->regs
[3],
326 ds1307
->regs
[4], ds1307
->regs
[5],
327 ds1307
->regs
[6], ds1307
->regs
[7],
330 /* report alarm time (ALARM1); assume 24 hour and day-of-month modes,
331 * and that all four fields are checked matches
333 t
->time
.tm_sec
= bcd2bin(ds1307
->regs
[0] & 0x7f);
334 t
->time
.tm_min
= bcd2bin(ds1307
->regs
[1] & 0x7f);
335 t
->time
.tm_hour
= bcd2bin(ds1307
->regs
[2] & 0x3f);
336 t
->time
.tm_mday
= bcd2bin(ds1307
->regs
[3] & 0x3f);
338 t
->time
.tm_year
= -1;
339 t
->time
.tm_wday
= -1;
340 t
->time
.tm_yday
= -1;
341 t
->time
.tm_isdst
= -1;
344 t
->enabled
= !!(ds1307
->regs
[7] & DS1337_BIT_A1IE
);
345 t
->pending
= !!(ds1307
->regs
[8] & DS1337_BIT_A1I
);
347 dev_dbg(dev
, "%s secs=%d, mins=%d, "
348 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
349 "alarm read", t
->time
.tm_sec
, t
->time
.tm_min
,
350 t
->time
.tm_hour
, t
->time
.tm_mday
,
351 t
->enabled
, t
->pending
);
356 static int ds1307_set_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
358 struct i2c_client
*client
= to_i2c_client(dev
);
359 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
360 unsigned char *buf
= ds1307
->regs
;
364 if (!test_bit(HAS_ALARM
, &ds1307
->flags
))
367 dev_dbg(dev
, "%s secs=%d, mins=%d, "
368 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
369 "alarm set", t
->time
.tm_sec
, t
->time
.tm_min
,
370 t
->time
.tm_hour
, t
->time
.tm_mday
,
371 t
->enabled
, t
->pending
);
373 /* read current status of both alarms and the chip */
374 ds1307
->reg_addr
= DS1339_REG_ALARM1_SECS
;
375 ds1307
->msg
[1].flags
= I2C_M_RD
;
376 ds1307
->msg
[1].len
= 9;
378 ret
= i2c_transfer(to_i2c_adapter(client
->dev
.parent
),
381 dev_err(dev
, "%s error %d\n", "alarm write", ret
);
384 control
= ds1307
->regs
[7];
385 status
= ds1307
->regs
[8];
387 dev_dbg(dev
, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n",
388 "alarm set (old status)",
389 ds1307
->regs
[0], ds1307
->regs
[1],
390 ds1307
->regs
[2], ds1307
->regs
[3],
391 ds1307
->regs
[4], ds1307
->regs
[5],
392 ds1307
->regs
[6], control
, status
);
394 /* set ALARM1, using 24 hour and day-of-month modes */
395 *buf
++ = DS1339_REG_ALARM1_SECS
; /* first register addr */
396 buf
[0] = bin2bcd(t
->time
.tm_sec
);
397 buf
[1] = bin2bcd(t
->time
.tm_min
);
398 buf
[2] = bin2bcd(t
->time
.tm_hour
);
399 buf
[3] = bin2bcd(t
->time
.tm_mday
);
401 /* set ALARM2 to non-garbage */
406 /* optionally enable ALARM1 */
407 buf
[7] = control
& ~(DS1337_BIT_A1IE
| DS1337_BIT_A2IE
);
409 dev_dbg(dev
, "alarm IRQ armed\n");
410 buf
[7] |= DS1337_BIT_A1IE
; /* only ALARM1 is used */
412 buf
[8] = status
& ~(DS1337_BIT_A1I
| DS1337_BIT_A2I
);
414 ds1307
->msg
[1].flags
= 0;
415 ds1307
->msg
[1].len
= 10;
417 ret
= i2c_transfer(to_i2c_adapter(client
->dev
.parent
),
420 dev_err(dev
, "can't set alarm time\n");
427 static int ds1307_ioctl(struct device
*dev
, unsigned int cmd
, unsigned long arg
)
429 struct i2c_client
*client
= to_i2c_client(dev
);
430 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
435 if (!test_bit(HAS_ALARM
, &ds1307
->flags
))
438 ret
= i2c_smbus_read_byte_data(client
, DS1337_REG_CONTROL
);
442 ret
&= ~DS1337_BIT_A1IE
;
444 ret
= i2c_smbus_write_byte_data(client
,
445 DS1337_REG_CONTROL
, ret
);
452 if (!test_bit(HAS_ALARM
, &ds1307
->flags
))
455 ret
= i2c_smbus_read_byte_data(client
, DS1337_REG_CONTROL
);
459 ret
|= DS1337_BIT_A1IE
;
461 ret
= i2c_smbus_write_byte_data(client
,
462 DS1337_REG_CONTROL
, ret
);
475 static const struct rtc_class_ops ds13xx_rtc_ops
= {
476 .read_time
= ds1307_get_time
,
477 .set_time
= ds1307_set_time
,
478 .read_alarm
= ds1307_read_alarm
,
479 .set_alarm
= ds1307_set_alarm
,
480 .ioctl
= ds1307_ioctl
,
483 /*----------------------------------------------------------------------*/
485 #define NVRAM_SIZE 56
488 ds1307_nvram_read(struct kobject
*kobj
, struct bin_attribute
*attr
,
489 char *buf
, loff_t off
, size_t count
)
491 struct i2c_client
*client
;
492 struct ds1307
*ds1307
;
493 struct i2c_msg msg
[2];
496 client
= kobj_to_i2c_client(kobj
);
497 ds1307
= i2c_get_clientdata(client
);
499 if (unlikely(off
>= NVRAM_SIZE
))
501 if ((off
+ count
) > NVRAM_SIZE
)
502 count
= NVRAM_SIZE
- off
;
503 if (unlikely(!count
))
506 msg
[0].addr
= client
->addr
;
513 msg
[1].addr
= client
->addr
;
514 msg
[1].flags
= I2C_M_RD
;
518 result
= i2c_transfer(to_i2c_adapter(client
->dev
.parent
), msg
, 2);
520 dev_err(&client
->dev
, "%s error %d\n", "nvram read", result
);
527 ds1307_nvram_write(struct kobject
*kobj
, struct bin_attribute
*attr
,
528 char *buf
, loff_t off
, size_t count
)
530 struct i2c_client
*client
;
531 u8 buffer
[NVRAM_SIZE
+ 1];
534 client
= kobj_to_i2c_client(kobj
);
536 if (unlikely(off
>= NVRAM_SIZE
))
538 if ((off
+ count
) > NVRAM_SIZE
)
539 count
= NVRAM_SIZE
- off
;
540 if (unlikely(!count
))
544 memcpy(buffer
+ 1, buf
, count
);
546 ret
= i2c_master_send(client
, buffer
, count
+ 1);
547 return (ret
< 0) ? ret
: (ret
- 1);
550 static struct bin_attribute nvram
= {
553 .mode
= S_IRUGO
| S_IWUSR
,
556 .read
= ds1307_nvram_read
,
557 .write
= ds1307_nvram_write
,
561 /*----------------------------------------------------------------------*/
563 static struct i2c_driver ds1307_driver
;
565 static int __devinit
ds1307_probe(struct i2c_client
*client
,
566 const struct i2c_device_id
*id
)
568 struct ds1307
*ds1307
;
571 const struct chip_desc
*chip
= &chips
[id
->driver_data
];
572 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
573 int want_irq
= false;
575 if (!i2c_check_functionality(adapter
,
576 I2C_FUNC_I2C
| I2C_FUNC_SMBUS_WRITE_BYTE_DATA
))
579 if (!(ds1307
= kzalloc(sizeof(struct ds1307
), GFP_KERNEL
)))
582 ds1307
->client
= client
;
583 i2c_set_clientdata(client
, ds1307
);
585 ds1307
->msg
[0].addr
= client
->addr
;
586 ds1307
->msg
[0].flags
= 0;
587 ds1307
->msg
[0].len
= 1;
588 ds1307
->msg
[0].buf
= &ds1307
->reg_addr
;
590 ds1307
->msg
[1].addr
= client
->addr
;
591 ds1307
->msg
[1].flags
= I2C_M_RD
;
592 ds1307
->msg
[1].len
= sizeof(ds1307
->regs
);
593 ds1307
->msg
[1].buf
= ds1307
->regs
;
595 ds1307
->type
= id
->driver_data
;
597 switch (ds1307
->type
) {
601 if (ds1307
->client
->irq
> 0 && chip
->alarm
) {
602 INIT_WORK(&ds1307
->work
, ds1307_work
);
606 ds1307
->reg_addr
= DS1337_REG_CONTROL
;
607 ds1307
->msg
[1].len
= 2;
609 /* get registers that the "rtc" read below won't read... */
610 tmp
= i2c_transfer(adapter
, ds1307
->msg
, 2);
612 pr_debug("read error %d\n", tmp
);
617 ds1307
->reg_addr
= 0;
618 ds1307
->msg
[1].len
= sizeof(ds1307
->regs
);
620 /* oscillator off? turn it on, so clock can tick. */
621 if (ds1307
->regs
[0] & DS1337_BIT_nEOSC
)
622 ds1307
->regs
[0] &= ~DS1337_BIT_nEOSC
;
624 /* Using IRQ? Disable the square wave and both alarms.
625 * For ds1339, be sure alarms can trigger when we're
626 * running on Vbackup (BBSQI); we assume ds1337 will
630 ds1307
->regs
[0] |= DS1337_BIT_INTCN
| DS1339_BIT_BBSQI
;
631 ds1307
->regs
[0] &= ~(DS1337_BIT_A2IE
| DS1337_BIT_A1IE
);
634 i2c_smbus_write_byte_data(client
, DS1337_REG_CONTROL
,
637 /* oscillator fault? clear flag, and warn */
638 if (ds1307
->regs
[1] & DS1337_BIT_OSF
) {
639 i2c_smbus_write_byte_data(client
, DS1337_REG_STATUS
,
640 ds1307
->regs
[1] & ~DS1337_BIT_OSF
);
641 dev_warn(&client
->dev
, "SET TIME!\n");
649 /* read RTC registers */
651 tmp
= i2c_transfer(adapter
, ds1307
->msg
, 2);
653 pr_debug("read error %d\n", tmp
);
658 /* minimal sanity checking; some chips (like DS1340) don't
659 * specify the extra bits as must-be-zero, but there are
660 * still a few values that are clearly out-of-range.
662 tmp
= ds1307
->regs
[DS1307_REG_SECS
];
663 switch (ds1307
->type
) {
666 /* clock halted? turn it on, so clock can tick. */
667 if (tmp
& DS1307_BIT_CH
) {
668 i2c_smbus_write_byte_data(client
, DS1307_REG_SECS
, 0);
669 dev_warn(&client
->dev
, "SET TIME!\n");
674 /* clock halted? turn it on, so clock can tick. */
675 if (tmp
& DS1307_BIT_CH
)
676 i2c_smbus_write_byte_data(client
, DS1307_REG_SECS
, 0);
678 /* oscillator fault? clear flag, and warn */
679 if (ds1307
->regs
[DS1307_REG_CONTROL
] & DS1338_BIT_OSF
) {
680 i2c_smbus_write_byte_data(client
, DS1307_REG_CONTROL
,
681 ds1307
->regs
[DS1307_REG_CONTROL
]
683 dev_warn(&client
->dev
, "SET TIME!\n");
688 /* clock halted? turn it on, so clock can tick. */
689 if (tmp
& DS1340_BIT_nEOSC
)
690 i2c_smbus_write_byte_data(client
, DS1307_REG_SECS
, 0);
692 tmp
= i2c_smbus_read_byte_data(client
, DS1340_REG_FLAG
);
694 pr_debug("read error %d\n", tmp
);
699 /* oscillator fault? clear flag, and warn */
700 if (tmp
& DS1340_BIT_OSF
) {
701 i2c_smbus_write_byte_data(client
, DS1340_REG_FLAG
, 0);
702 dev_warn(&client
->dev
, "SET TIME!\n");
710 tmp
= ds1307
->regs
[DS1307_REG_SECS
];
711 tmp
= bcd2bin(tmp
& 0x7f);
714 tmp
= bcd2bin(ds1307
->regs
[DS1307_REG_MIN
] & 0x7f);
718 tmp
= bcd2bin(ds1307
->regs
[DS1307_REG_MDAY
] & 0x3f);
719 if (tmp
== 0 || tmp
> 31)
722 tmp
= bcd2bin(ds1307
->regs
[DS1307_REG_MONTH
] & 0x1f);
723 if (tmp
== 0 || tmp
> 12)
726 tmp
= ds1307
->regs
[DS1307_REG_HOUR
];
727 switch (ds1307
->type
) {
730 /* NOTE: ignores century bits; fix before deploying
731 * systems that will run through year 2100.
735 if (!(tmp
& DS1307_BIT_12HR
))
738 /* Be sure we're in 24 hour mode. Multi-master systems
741 tmp
= bcd2bin(tmp
& 0x1f);
744 if (ds1307
->regs
[DS1307_REG_HOUR
] & DS1307_BIT_PM
)
746 i2c_smbus_write_byte_data(client
,
751 ds1307
->rtc
= rtc_device_register(client
->name
, &client
->dev
,
752 &ds13xx_rtc_ops
, THIS_MODULE
);
753 if (IS_ERR(ds1307
->rtc
)) {
754 err
= PTR_ERR(ds1307
->rtc
);
755 dev_err(&client
->dev
,
756 "unable to register the class device\n");
761 err
= request_irq(client
->irq
, ds1307_irq
, 0,
762 ds1307
->rtc
->name
, client
);
764 dev_err(&client
->dev
,
765 "unable to request IRQ!\n");
768 set_bit(HAS_ALARM
, &ds1307
->flags
);
769 dev_dbg(&client
->dev
, "got IRQ %d\n", client
->irq
);
773 err
= sysfs_create_bin_file(&client
->dev
.kobj
, &nvram
);
775 set_bit(HAS_NVRAM
, &ds1307
->flags
);
776 dev_info(&client
->dev
, "56 bytes nvram\n");
783 dev_dbg(&client
->dev
, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
785 ds1307
->regs
[0], ds1307
->regs
[1],
786 ds1307
->regs
[2], ds1307
->regs
[3],
787 ds1307
->regs
[4], ds1307
->regs
[5],
791 rtc_device_unregister(ds1307
->rtc
);
797 static int __devexit
ds1307_remove(struct i2c_client
*client
)
799 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
801 if (test_and_clear_bit(HAS_ALARM
, &ds1307
->flags
)) {
802 free_irq(client
->irq
, client
);
803 cancel_work_sync(&ds1307
->work
);
806 if (test_and_clear_bit(HAS_NVRAM
, &ds1307
->flags
))
807 sysfs_remove_bin_file(&client
->dev
.kobj
, &nvram
);
809 rtc_device_unregister(ds1307
->rtc
);
814 static struct i2c_driver ds1307_driver
= {
816 .name
= "rtc-ds1307",
817 .owner
= THIS_MODULE
,
819 .probe
= ds1307_probe
,
820 .remove
= __devexit_p(ds1307_remove
),
821 .id_table
= ds1307_id
,
824 static int __init
ds1307_init(void)
826 return i2c_add_driver(&ds1307_driver
);
828 module_init(ds1307_init
);
830 static void __exit
ds1307_exit(void)
832 i2c_del_driver(&ds1307_driver
);
834 module_exit(ds1307_exit
);
836 MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
837 MODULE_LICENSE("GPL");