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.
37 // rs5c372 too? different address...
41 /* RTC registers don't differ much, except for the century flag */
42 #define DS1307_REG_SECS 0x00 /* 00-59 */
43 # define DS1307_BIT_CH 0x80
44 # define DS1340_BIT_nEOSC 0x80
45 #define DS1307_REG_MIN 0x01 /* 00-59 */
46 #define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
47 # define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
48 # define DS1307_BIT_PM 0x20 /* in REG_HOUR */
49 # define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
50 # define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
51 #define DS1307_REG_WDAY 0x03 /* 01-07 */
52 #define DS1307_REG_MDAY 0x04 /* 01-31 */
53 #define DS1307_REG_MONTH 0x05 /* 01-12 */
54 # define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
55 #define DS1307_REG_YEAR 0x06 /* 00-99 */
57 /* Other registers (control, status, alarms, trickle charge, NVRAM, etc)
58 * start at 7, and they differ a LOT. Only control and status matter for
59 * basic RTC date and time functionality; be careful using them.
61 #define DS1307_REG_CONTROL 0x07 /* or ds1338 */
62 # define DS1307_BIT_OUT 0x80
63 # define DS1338_BIT_OSF 0x20
64 # define DS1307_BIT_SQWE 0x10
65 # define DS1307_BIT_RS1 0x02
66 # define DS1307_BIT_RS0 0x01
67 #define DS1337_REG_CONTROL 0x0e
68 # define DS1337_BIT_nEOSC 0x80
69 # define DS1339_BIT_BBSQI 0x20
70 # define DS1337_BIT_RS2 0x10
71 # define DS1337_BIT_RS1 0x08
72 # define DS1337_BIT_INTCN 0x04
73 # define DS1337_BIT_A2IE 0x02
74 # define DS1337_BIT_A1IE 0x01
75 #define DS1340_REG_CONTROL 0x07
76 # define DS1340_BIT_OUT 0x80
77 # define DS1340_BIT_FT 0x40
78 # define DS1340_BIT_CALIB_SIGN 0x20
79 # define DS1340_M_CALIBRATION 0x1f
80 #define DS1340_REG_FLAG 0x09
81 # define DS1340_BIT_OSF 0x80
82 #define DS1337_REG_STATUS 0x0f
83 # define DS1337_BIT_OSF 0x80
84 # define DS1337_BIT_A2I 0x02
85 # define DS1337_BIT_A1I 0x01
86 #define DS1339_REG_ALARM1_SECS 0x07
87 #define DS1339_REG_TRICKLE 0x10
89 #define RX8025_REG_CTRL1 0x0e
90 # define RX8025_BIT_2412 0x20
91 #define RX8025_REG_CTRL2 0x0f
92 # define RX8025_BIT_PON 0x10
93 # define RX8025_BIT_VDET 0x40
94 # define RX8025_BIT_XST 0x20
98 u8 offset; /* register's offset */
102 #define HAS_NVRAM 0 /* bit 0 == sysfs file active */
103 #define HAS_ALARM 1 /* bit 1 == irq claimed */
104 struct i2c_client *client;
105 struct rtc_device *rtc;
106 struct work_struct work;
107 s32 (*read_block_data)(struct i2c_client *client, u8 command,
108 u8 length, u8 *values);
109 s32 (*write_block_data)(struct i2c_client *client, u8 command,
110 u8 length, const u8 *values);
118 static const struct chip_desc chips[] = {
138 static const struct i2c_device_id ds1307_id[] = {
139 { "ds1307", ds_1307 },
140 { "ds1337", ds_1337 },
141 { "ds1338", ds_1338 },
142 { "ds1339", ds_1339 },
143 { "ds1388", ds_1388 },
144 { "ds1340", ds_1340 },
145 { "m41t00", m41t00 },
146 { "rx8025", rx_8025 },
149 MODULE_DEVICE_TABLE(i2c, ds1307_id);
151 /*----------------------------------------------------------------------*/
153 #define BLOCK_DATA_MAX_TRIES 10
155 static s32 ds1307_read_block_data_once(struct i2c_client *client, u8 command,
156 u8 length, u8 *values)
160 for (i = 0; i < length; i++) {
161 data = i2c_smbus_read_byte_data(client, command + i);
169 static s32 ds1307_read_block_data(struct i2c_client *client, u8 command,
170 u8 length, u8 *values)
172 u8 oldvalues[I2C_SMBUS_BLOCK_MAX];
176 dev_dbg(&client->dev, "ds1307_read_block_data (length=%d)\n", length);
177 ret = ds1307_read_block_data_once(client, command, length, values);
181 if (++tries > BLOCK_DATA_MAX_TRIES) {
182 dev_err(&client->dev,
183 "ds1307_read_block_data failed\n");
186 memcpy(oldvalues, values, length);
187 ret = ds1307_read_block_data_once(client, command, length,
191 } while (memcmp(oldvalues, values, length));
195 static s32 ds1307_write_block_data(struct i2c_client *client, u8 command,
196 u8 length, const u8 *values)
198 u8 currvalues[I2C_SMBUS_BLOCK_MAX];
201 dev_dbg(&client->dev, "ds1307_write_block_data (length=%d)\n", length);
205 if (++tries > BLOCK_DATA_MAX_TRIES) {
206 dev_err(&client->dev,
207 "ds1307_write_block_data failed\n");
210 for (i = 0; i < length; i++) {
211 ret = i2c_smbus_write_byte_data(client, command + i,
216 ret = ds1307_read_block_data_once(client, command, length,
220 } while (memcmp(currvalues, values, length));
224 /*----------------------------------------------------------------------*/
227 * The IRQ logic includes a "real" handler running in IRQ context just
228 * long enough to schedule this workqueue entry. We need a task context
229 * to talk to the RTC, since I2C I/O calls require that; and disable the
230 * IRQ until we clear its status on the chip, so that this handler can
231 * work with any type of triggering (not just falling edge).
233 * The ds1337 and ds1339 both have two alarms, but we only use the first
234 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
235 * signal; ds1339 chips have only one alarm signal.
237 static void ds1307_work(struct work_struct *work)
239 struct ds1307 *ds1307;
240 struct i2c_client *client;
244 ds1307 = container_of(work, struct ds1307, work);
245 client = ds1307->client;
246 lock = &ds1307->rtc->ops_lock;
249 stat = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS);
253 if (stat & DS1337_BIT_A1I) {
254 stat &= ~DS1337_BIT_A1I;
255 i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, stat);
257 control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
261 control &= ~DS1337_BIT_A1IE;
262 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, control);
264 /* rtc_update_irq() assumes that it is called
265 * from IRQ-disabled context.
268 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
273 if (test_bit(HAS_ALARM, &ds1307->flags))
274 enable_irq(client->irq);
278 static irqreturn_t ds1307_irq(int irq, void *dev_id)
280 struct i2c_client *client = dev_id;
281 struct ds1307 *ds1307 = i2c_get_clientdata(client);
283 disable_irq_nosync(irq);
284 schedule_work(&ds1307->work);
288 /*----------------------------------------------------------------------*/
290 static int ds1307_get_time(struct device *dev, struct rtc_time *t)
292 struct ds1307 *ds1307 = dev_get_drvdata(dev);
295 /* read the RTC date and time registers all at once */
296 tmp = ds1307->read_block_data(ds1307->client,
297 ds1307->offset, 7, ds1307->regs);
299 dev_err(dev, "%s error %d\n", "read", tmp);
303 dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
305 ds1307->regs[0], ds1307->regs[1],
306 ds1307->regs[2], ds1307->regs[3],
307 ds1307->regs[4], ds1307->regs[5],
310 t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f);
311 t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
312 tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
313 t->tm_hour = bcd2bin(tmp);
314 t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
315 t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
316 tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
317 t->tm_mon = bcd2bin(tmp) - 1;
319 /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
320 t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
322 dev_dbg(dev, "%s secs=%d, mins=%d, "
323 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
324 "read", t->tm_sec, t->tm_min,
325 t->tm_hour, t->tm_mday,
326 t->tm_mon, t->tm_year, t->tm_wday);
328 /* initial clock setting can be undefined */
329 return rtc_valid_tm(t);
332 static int ds1307_set_time(struct device *dev, struct rtc_time *t)
334 struct ds1307 *ds1307 = dev_get_drvdata(dev);
337 u8 *buf = ds1307->regs;
339 dev_dbg(dev, "%s secs=%d, mins=%d, "
340 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
341 "write", t->tm_sec, t->tm_min,
342 t->tm_hour, t->tm_mday,
343 t->tm_mon, t->tm_year, t->tm_wday);
345 buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
346 buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
347 buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
348 buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
349 buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
350 buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
352 /* assume 20YY not 19YY */
353 tmp = t->tm_year - 100;
354 buf[DS1307_REG_YEAR] = bin2bcd(tmp);
356 switch (ds1307->type) {
359 buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
362 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN
363 | DS1340_BIT_CENTURY;
369 dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
370 "write", buf[0], buf[1], buf[2], buf[3],
371 buf[4], buf[5], buf[6]);
373 result = ds1307->write_block_data(ds1307->client,
374 ds1307->offset, 7, buf);
376 dev_err(dev, "%s error %d\n", "write", result);
382 static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
384 struct i2c_client *client = to_i2c_client(dev);
385 struct ds1307 *ds1307 = i2c_get_clientdata(client);
388 if (!test_bit(HAS_ALARM, &ds1307->flags))
391 /* read all ALARM1, ALARM2, and status registers at once */
392 ret = ds1307->read_block_data(client,
393 DS1339_REG_ALARM1_SECS, 9, ds1307->regs);
395 dev_err(dev, "%s error %d\n", "alarm read", ret);
399 dev_dbg(dev, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n",
401 ds1307->regs[0], ds1307->regs[1],
402 ds1307->regs[2], ds1307->regs[3],
403 ds1307->regs[4], ds1307->regs[5],
404 ds1307->regs[6], ds1307->regs[7],
407 /* report alarm time (ALARM1); assume 24 hour and day-of-month modes,
408 * and that all four fields are checked matches
410 t->time.tm_sec = bcd2bin(ds1307->regs[0] & 0x7f);
411 t->time.tm_min = bcd2bin(ds1307->regs[1] & 0x7f);
412 t->time.tm_hour = bcd2bin(ds1307->regs[2] & 0x3f);
413 t->time.tm_mday = bcd2bin(ds1307->regs[3] & 0x3f);
415 t->time.tm_year = -1;
416 t->time.tm_wday = -1;
417 t->time.tm_yday = -1;
418 t->time.tm_isdst = -1;
421 t->enabled = !!(ds1307->regs[7] & DS1337_BIT_A1IE);
422 t->pending = !!(ds1307->regs[8] & DS1337_BIT_A1I);
424 dev_dbg(dev, "%s secs=%d, mins=%d, "
425 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
426 "alarm read", t->time.tm_sec, t->time.tm_min,
427 t->time.tm_hour, t->time.tm_mday,
428 t->enabled, t->pending);
433 static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
435 struct i2c_client *client = to_i2c_client(dev);
436 struct ds1307 *ds1307 = i2c_get_clientdata(client);
437 unsigned char *buf = ds1307->regs;
441 if (!test_bit(HAS_ALARM, &ds1307->flags))
444 dev_dbg(dev, "%s secs=%d, mins=%d, "
445 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
446 "alarm set", t->time.tm_sec, t->time.tm_min,
447 t->time.tm_hour, t->time.tm_mday,
448 t->enabled, t->pending);
450 /* read current status of both alarms and the chip */
451 ret = ds1307->read_block_data(client,
452 DS1339_REG_ALARM1_SECS, 9, buf);
454 dev_err(dev, "%s error %d\n", "alarm write", ret);
457 control = ds1307->regs[7];
458 status = ds1307->regs[8];
460 dev_dbg(dev, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n",
461 "alarm set (old status)",
462 ds1307->regs[0], ds1307->regs[1],
463 ds1307->regs[2], ds1307->regs[3],
464 ds1307->regs[4], ds1307->regs[5],
465 ds1307->regs[6], control, status);
467 /* set ALARM1, using 24 hour and day-of-month modes */
468 buf[0] = bin2bcd(t->time.tm_sec);
469 buf[1] = bin2bcd(t->time.tm_min);
470 buf[2] = bin2bcd(t->time.tm_hour);
471 buf[3] = bin2bcd(t->time.tm_mday);
473 /* set ALARM2 to non-garbage */
478 /* optionally enable ALARM1 */
479 buf[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
481 dev_dbg(dev, "alarm IRQ armed\n");
482 buf[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
484 buf[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
486 ret = ds1307->write_block_data(client,
487 DS1339_REG_ALARM1_SECS, 9, buf);
489 dev_err(dev, "can't set alarm time\n");
496 static int ds1307_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
498 struct i2c_client *client = to_i2c_client(dev);
499 struct ds1307 *ds1307 = i2c_get_clientdata(client);
504 if (!test_bit(HAS_ALARM, &ds1307->flags))
507 ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
511 ret &= ~DS1337_BIT_A1IE;
513 ret = i2c_smbus_write_byte_data(client,
514 DS1337_REG_CONTROL, ret);
521 if (!test_bit(HAS_ALARM, &ds1307->flags))
524 ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
528 ret |= DS1337_BIT_A1IE;
530 ret = i2c_smbus_write_byte_data(client,
531 DS1337_REG_CONTROL, ret);
544 static const struct rtc_class_ops ds13xx_rtc_ops = {
545 .read_time = ds1307_get_time,
546 .set_time = ds1307_set_time,
547 .read_alarm = ds1337_read_alarm,
548 .set_alarm = ds1337_set_alarm,
549 .ioctl = ds1307_ioctl,
552 /*----------------------------------------------------------------------*/
554 #define NVRAM_SIZE 56
557 ds1307_nvram_read(struct kobject *kobj, struct bin_attribute *attr,
558 char *buf, loff_t off, size_t count)
560 struct i2c_client *client;
561 struct ds1307 *ds1307;
564 client = kobj_to_i2c_client(kobj);
565 ds1307 = i2c_get_clientdata(client);
567 if (unlikely(off >= NVRAM_SIZE))
569 if ((off + count) > NVRAM_SIZE)
570 count = NVRAM_SIZE - off;
571 if (unlikely(!count))
574 result = ds1307->read_block_data(client, 8 + off, count, buf);
576 dev_err(&client->dev, "%s error %d\n", "nvram read", result);
581 ds1307_nvram_write(struct kobject *kobj, struct bin_attribute *attr,
582 char *buf, loff_t off, size_t count)
584 struct i2c_client *client;
585 struct ds1307 *ds1307;
588 client = kobj_to_i2c_client(kobj);
589 ds1307 = i2c_get_clientdata(client);
591 if (unlikely(off >= NVRAM_SIZE))
593 if ((off + count) > NVRAM_SIZE)
594 count = NVRAM_SIZE - off;
595 if (unlikely(!count))
598 result = ds1307->write_block_data(client, 8 + off, count, buf);
600 dev_err(&client->dev, "%s error %d\n", "nvram write", result);
606 static struct bin_attribute nvram = {
609 .mode = S_IRUGO | S_IWUSR,
612 .read = ds1307_nvram_read,
613 .write = ds1307_nvram_write,
617 /*----------------------------------------------------------------------*/
619 static struct i2c_driver ds1307_driver;
621 static int __devinit ds1307_probe(struct i2c_client *client,
622 const struct i2c_device_id *id)
624 struct ds1307 *ds1307;
627 const struct chip_desc *chip = &chips[id->driver_data];
628 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
629 int want_irq = false;
632 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)
633 && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
636 if (!(ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL)))
639 i2c_set_clientdata(client, ds1307);
641 ds1307->client = client;
642 ds1307->type = id->driver_data;
646 if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
647 ds1307->read_block_data = i2c_smbus_read_i2c_block_data;
648 ds1307->write_block_data = i2c_smbus_write_i2c_block_data;
650 ds1307->read_block_data = ds1307_read_block_data;
651 ds1307->write_block_data = ds1307_write_block_data;
654 switch (ds1307->type) {
658 if (ds1307->client->irq > 0 && chip->alarm) {
659 INIT_WORK(&ds1307->work, ds1307_work);
662 /* get registers that the "rtc" read below won't read... */
663 tmp = ds1307->read_block_data(ds1307->client,
664 DS1337_REG_CONTROL, 2, buf);
666 pr_debug("read error %d\n", tmp);
671 /* oscillator off? turn it on, so clock can tick. */
672 if (ds1307->regs[0] & DS1337_BIT_nEOSC)
673 ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
675 /* Using IRQ? Disable the square wave and both alarms.
676 * For ds1339, be sure alarms can trigger when we're
677 * running on Vbackup (BBSQI); we assume ds1337 will
681 ds1307->regs[0] |= DS1337_BIT_INTCN | DS1339_BIT_BBSQI;
682 ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
685 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL,
688 /* oscillator fault? clear flag, and warn */
689 if (ds1307->regs[1] & DS1337_BIT_OSF) {
690 i2c_smbus_write_byte_data(client, DS1337_REG_STATUS,
691 ds1307->regs[1] & ~DS1337_BIT_OSF);
692 dev_warn(&client->dev, "SET TIME!\n");
697 tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
698 RX8025_REG_CTRL1 << 4 | 0x08, 2, buf);
700 pr_debug("read error %d\n", tmp);
705 /* oscillator off? turn it on, so clock can tick. */
706 if (!(ds1307->regs[1] & RX8025_BIT_XST)) {
707 ds1307->regs[1] |= RX8025_BIT_XST;
708 i2c_smbus_write_byte_data(client,
709 RX8025_REG_CTRL2 << 4 | 0x08,
711 dev_warn(&client->dev,
712 "oscillator stop detected - SET TIME!\n");
715 if (ds1307->regs[1] & RX8025_BIT_PON) {
716 ds1307->regs[1] &= ~RX8025_BIT_PON;
717 i2c_smbus_write_byte_data(client,
718 RX8025_REG_CTRL2 << 4 | 0x08,
720 dev_warn(&client->dev, "power-on detected\n");
723 if (ds1307->regs[1] & RX8025_BIT_VDET) {
724 ds1307->regs[1] &= ~RX8025_BIT_VDET;
725 i2c_smbus_write_byte_data(client,
726 RX8025_REG_CTRL2 << 4 | 0x08,
728 dev_warn(&client->dev, "voltage drop detected\n");
731 /* make sure we are running in 24hour mode */
732 if (!(ds1307->regs[0] & RX8025_BIT_2412)) {
735 /* switch to 24 hour mode */
736 i2c_smbus_write_byte_data(client,
737 RX8025_REG_CTRL1 << 4 | 0x08,
741 tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
742 RX8025_REG_CTRL1 << 4 | 0x08, 2, buf);
744 pr_debug("read error %d\n", tmp);
750 hour = bcd2bin(ds1307->regs[DS1307_REG_HOUR]);
753 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
756 i2c_smbus_write_byte_data(client,
757 DS1307_REG_HOUR << 4 | 0x08,
762 ds1307->offset = 1; /* Seconds starts at 1 */
769 /* read RTC registers */
770 tmp = ds1307->read_block_data(ds1307->client, 0, 8, buf);
772 pr_debug("read error %d\n", tmp);
777 /* minimal sanity checking; some chips (like DS1340) don't
778 * specify the extra bits as must-be-zero, but there are
779 * still a few values that are clearly out-of-range.
781 tmp = ds1307->regs[DS1307_REG_SECS];
782 switch (ds1307->type) {
785 /* clock halted? turn it on, so clock can tick. */
786 if (tmp & DS1307_BIT_CH) {
787 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
788 dev_warn(&client->dev, "SET TIME!\n");
793 /* clock halted? turn it on, so clock can tick. */
794 if (tmp & DS1307_BIT_CH)
795 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
797 /* oscillator fault? clear flag, and warn */
798 if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
799 i2c_smbus_write_byte_data(client, DS1307_REG_CONTROL,
800 ds1307->regs[DS1307_REG_CONTROL]
802 dev_warn(&client->dev, "SET TIME!\n");
807 /* clock halted? turn it on, so clock can tick. */
808 if (tmp & DS1340_BIT_nEOSC)
809 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
811 tmp = i2c_smbus_read_byte_data(client, DS1340_REG_FLAG);
813 pr_debug("read error %d\n", tmp);
818 /* oscillator fault? clear flag, and warn */
819 if (tmp & DS1340_BIT_OSF) {
820 i2c_smbus_write_byte_data(client, DS1340_REG_FLAG, 0);
821 dev_warn(&client->dev, "SET TIME!\n");
831 tmp = ds1307->regs[DS1307_REG_HOUR];
832 switch (ds1307->type) {
835 /* NOTE: ignores century bits; fix before deploying
836 * systems that will run through year 2100.
842 if (!(tmp & DS1307_BIT_12HR))
845 /* Be sure we're in 24 hour mode. Multi-master systems
848 tmp = bcd2bin(tmp & 0x1f);
851 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
853 i2c_smbus_write_byte_data(client,
858 ds1307->rtc = rtc_device_register(client->name, &client->dev,
859 &ds13xx_rtc_ops, THIS_MODULE);
860 if (IS_ERR(ds1307->rtc)) {
861 err = PTR_ERR(ds1307->rtc);
862 dev_err(&client->dev,
863 "unable to register the class device\n");
868 err = request_irq(client->irq, ds1307_irq, 0,
869 ds1307->rtc->name, client);
871 dev_err(&client->dev,
872 "unable to request IRQ!\n");
875 set_bit(HAS_ALARM, &ds1307->flags);
876 dev_dbg(&client->dev, "got IRQ %d\n", client->irq);
880 err = sysfs_create_bin_file(&client->dev.kobj, &nvram);
882 set_bit(HAS_NVRAM, &ds1307->flags);
883 dev_info(&client->dev, "56 bytes nvram\n");
891 rtc_device_unregister(ds1307->rtc);
897 static int __devexit ds1307_remove(struct i2c_client *client)
899 struct ds1307 *ds1307 = i2c_get_clientdata(client);
901 if (test_and_clear_bit(HAS_ALARM, &ds1307->flags)) {
902 free_irq(client->irq, client);
903 cancel_work_sync(&ds1307->work);
906 if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags))
907 sysfs_remove_bin_file(&client->dev.kobj, &nvram);
909 rtc_device_unregister(ds1307->rtc);
914 static struct i2c_driver ds1307_driver = {
916 .name = "rtc-ds1307",
917 .owner = THIS_MODULE,
919 .probe = ds1307_probe,
920 .remove = __devexit_p(ds1307_remove),
921 .id_table = ds1307_id,
924 static int __init ds1307_init(void)
926 return i2c_add_driver(&ds1307_driver);
928 module_init(ds1307_init);
930 static void __exit ds1307_exit(void)
932 i2c_del_driver(&ds1307_driver);
934 module_exit(ds1307_exit);
936 MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
937 MODULE_LICENSE("GPL");