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.
27 * This is currently a simple no-alarms driver. If your board has the
28 * alarm irq wired up on a ds1337 or ds1339, and you want to use that,
29 * then look at the rtc-rs5c372 driver for code to steal...
38 // rs5c372 too? different address...
42 /* RTC registers don't differ much, except for the century flag */
43 #define DS1307_REG_SECS 0x00 /* 00-59 */
44 # define DS1307_BIT_CH 0x80
45 # define DS1340_BIT_nEOSC 0x80
46 #define DS1307_REG_MIN 0x01 /* 00-59 */
47 #define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
48 # define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
49 # define DS1307_BIT_PM 0x20 /* in REG_HOUR */
50 # define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
51 # define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
52 #define DS1307_REG_WDAY 0x03 /* 01-07 */
53 #define DS1307_REG_MDAY 0x04 /* 01-31 */
54 #define DS1307_REG_MONTH 0x05 /* 01-12 */
55 # define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
56 #define DS1307_REG_YEAR 0x06 /* 00-99 */
58 /* Other registers (control, status, alarms, trickle charge, NVRAM, etc)
59 * start at 7, and they differ a LOT. Only control and status matter for
60 * basic RTC date and time functionality; be careful using them.
62 #define DS1307_REG_CONTROL 0x07 /* or ds1338 */
63 # define DS1307_BIT_OUT 0x80
64 # define DS1338_BIT_OSF 0x20
65 # define DS1307_BIT_SQWE 0x10
66 # define DS1307_BIT_RS1 0x02
67 # define DS1307_BIT_RS0 0x01
68 #define DS1337_REG_CONTROL 0x0e
69 # define DS1337_BIT_nEOSC 0x80
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_TRICKLE 0x10
95 struct i2c_msg msg[2];
96 struct i2c_client *client;
97 struct i2c_client dev;
98 struct rtc_device *rtc;
108 static const struct chip_desc chips[] = { {
132 static inline const struct chip_desc *find_chip(const char *s)
136 for (i = 0; i < ARRAY_SIZE(chips); i++)
137 if (strnicmp(s, chips[i].name, sizeof chips[i].name) == 0)
142 static int ds1307_get_time(struct device *dev, struct rtc_time *t)
144 struct ds1307 *ds1307 = dev_get_drvdata(dev);
147 /* read the RTC date and time registers all at once */
148 ds1307->msg[1].flags = I2C_M_RD;
149 ds1307->msg[1].len = 7;
151 tmp = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent),
154 dev_err(dev, "%s error %d\n", "read", tmp);
158 dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
160 ds1307->regs[0], ds1307->regs[1],
161 ds1307->regs[2], ds1307->regs[3],
162 ds1307->regs[4], ds1307->regs[5],
165 t->tm_sec = BCD2BIN(ds1307->regs[DS1307_REG_SECS] & 0x7f);
166 t->tm_min = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f);
167 tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
168 t->tm_hour = BCD2BIN(tmp);
169 t->tm_wday = BCD2BIN(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
170 t->tm_mday = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
171 tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
172 t->tm_mon = BCD2BIN(tmp) - 1;
174 /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
175 t->tm_year = BCD2BIN(ds1307->regs[DS1307_REG_YEAR]) + 100;
177 dev_dbg(dev, "%s secs=%d, mins=%d, "
178 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
179 "read", t->tm_sec, t->tm_min,
180 t->tm_hour, t->tm_mday,
181 t->tm_mon, t->tm_year, t->tm_wday);
183 /* initial clock setting can be undefined */
184 return rtc_valid_tm(t);
187 static int ds1307_set_time(struct device *dev, struct rtc_time *t)
189 struct ds1307 *ds1307 = dev_get_drvdata(dev);
192 u8 *buf = ds1307->regs;
194 dev_dbg(dev, "%s secs=%d, mins=%d, "
195 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
196 "write", t->tm_sec, t->tm_min,
197 t->tm_hour, t->tm_mday,
198 t->tm_mon, t->tm_year, t->tm_wday);
200 *buf++ = 0; /* first register addr */
201 buf[DS1307_REG_SECS] = BIN2BCD(t->tm_sec);
202 buf[DS1307_REG_MIN] = BIN2BCD(t->tm_min);
203 buf[DS1307_REG_HOUR] = BIN2BCD(t->tm_hour);
204 buf[DS1307_REG_WDAY] = BIN2BCD(t->tm_wday + 1);
205 buf[DS1307_REG_MDAY] = BIN2BCD(t->tm_mday);
206 buf[DS1307_REG_MONTH] = BIN2BCD(t->tm_mon + 1);
208 /* assume 20YY not 19YY */
209 tmp = t->tm_year - 100;
210 buf[DS1307_REG_YEAR] = BIN2BCD(tmp);
212 switch (ds1307->type) {
215 buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
218 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN
219 | DS1340_BIT_CENTURY;
225 ds1307->msg[1].flags = 0;
226 ds1307->msg[1].len = 8;
228 dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
229 "write", buf[0], buf[1], buf[2], buf[3],
230 buf[4], buf[5], buf[6]);
232 result = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent),
235 dev_err(dev, "%s error %d\n", "write", tmp);
241 static const struct rtc_class_ops ds13xx_rtc_ops = {
242 .read_time = ds1307_get_time,
243 .set_time = ds1307_set_time,
246 /*----------------------------------------------------------------------*/
248 #define NVRAM_SIZE 56
251 ds1307_nvram_read(struct kobject *kobj, struct bin_attribute *attr,
252 char *buf, loff_t off, size_t count)
254 struct i2c_client *client;
255 struct ds1307 *ds1307;
256 struct i2c_msg msg[2];
259 client = kobj_to_i2c_client(kobj);
260 ds1307 = i2c_get_clientdata(client);
262 if (unlikely(off >= NVRAM_SIZE))
264 if ((off + count) > NVRAM_SIZE)
265 count = NVRAM_SIZE - off;
266 if (unlikely(!count))
269 msg[0].addr = client->addr;
276 msg[1].addr = client->addr;
277 msg[1].flags = I2C_M_RD;
281 result = i2c_transfer(to_i2c_adapter(client->dev.parent), msg, 2);
283 dev_err(&client->dev, "%s error %d\n", "nvram read", result);
290 ds1307_nvram_write(struct kobject *kobj, struct bin_attribute *attr,
291 char *buf, loff_t off, size_t count)
293 struct i2c_client *client;
294 u8 buffer[NVRAM_SIZE + 1];
297 client = kobj_to_i2c_client(kobj);
299 if (unlikely(off >= NVRAM_SIZE))
301 if ((off + count) > NVRAM_SIZE)
302 count = NVRAM_SIZE - off;
303 if (unlikely(!count))
307 memcpy(buffer + 1, buf, count);
309 ret = i2c_master_send(client, buffer, count + 1);
310 return (ret < 0) ? ret : (ret - 1);
313 static struct bin_attribute nvram = {
316 .mode = S_IRUGO | S_IWUSR,
317 .owner = THIS_MODULE,
320 .read = ds1307_nvram_read,
321 .write = ds1307_nvram_write,
325 /*----------------------------------------------------------------------*/
327 static struct i2c_driver ds1307_driver;
329 static int __devinit ds1307_probe(struct i2c_client *client,
330 const struct i2c_device_id *id)
332 struct ds1307 *ds1307;
335 const struct chip_desc *chip;
336 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
338 chip = find_chip(client->name);
340 dev_err(&client->dev, "unknown chip type '%s'\n",
345 if (!i2c_check_functionality(adapter,
346 I2C_FUNC_I2C | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
349 if (!(ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL)))
352 ds1307->client = client;
353 i2c_set_clientdata(client, ds1307);
355 ds1307->msg[0].addr = client->addr;
356 ds1307->msg[0].flags = 0;
357 ds1307->msg[0].len = 1;
358 ds1307->msg[0].buf = &ds1307->reg_addr;
360 ds1307->msg[1].addr = client->addr;
361 ds1307->msg[1].flags = I2C_M_RD;
362 ds1307->msg[1].len = sizeof(ds1307->regs);
363 ds1307->msg[1].buf = ds1307->regs;
365 ds1307->type = chip->type;
367 switch (ds1307->type) {
370 ds1307->reg_addr = DS1337_REG_CONTROL;
371 ds1307->msg[1].len = 2;
373 /* get registers that the "rtc" read below won't read... */
374 tmp = i2c_transfer(adapter, ds1307->msg, 2);
376 pr_debug("read error %d\n", tmp);
381 ds1307->reg_addr = 0;
382 ds1307->msg[1].len = sizeof(ds1307->regs);
384 /* oscillator off? turn it on, so clock can tick. */
385 if (ds1307->regs[0] & DS1337_BIT_nEOSC)
386 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL,
387 ds1307->regs[0] & ~DS1337_BIT_nEOSC);
389 /* oscillator fault? clear flag, and warn */
390 if (ds1307->regs[1] & DS1337_BIT_OSF) {
391 i2c_smbus_write_byte_data(client, DS1337_REG_STATUS,
392 ds1307->regs[1] & ~DS1337_BIT_OSF);
393 dev_warn(&client->dev, "SET TIME!\n");
401 /* read RTC registers */
403 tmp = i2c_transfer(adapter, ds1307->msg, 2);
405 pr_debug("read error %d\n", tmp);
410 /* minimal sanity checking; some chips (like DS1340) don't
411 * specify the extra bits as must-be-zero, but there are
412 * still a few values that are clearly out-of-range.
414 tmp = ds1307->regs[DS1307_REG_SECS];
415 switch (ds1307->type) {
418 /* clock halted? turn it on, so clock can tick. */
419 if (tmp & DS1307_BIT_CH) {
420 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
421 dev_warn(&client->dev, "SET TIME!\n");
426 /* clock halted? turn it on, so clock can tick. */
427 if (tmp & DS1307_BIT_CH)
428 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
430 /* oscillator fault? clear flag, and warn */
431 if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
432 i2c_smbus_write_byte_data(client, DS1307_REG_CONTROL,
433 ds1307->regs[DS1307_REG_CONTROL]
435 dev_warn(&client->dev, "SET TIME!\n");
440 /* clock halted? turn it on, so clock can tick. */
441 if (tmp & DS1340_BIT_nEOSC)
442 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
444 tmp = i2c_smbus_read_byte_data(client, DS1340_REG_FLAG);
446 pr_debug("read error %d\n", tmp);
451 /* oscillator fault? clear flag, and warn */
452 if (tmp & DS1340_BIT_OSF) {
453 i2c_smbus_write_byte_data(client, DS1340_REG_FLAG, 0);
454 dev_warn(&client->dev, "SET TIME!\n");
462 tmp = ds1307->regs[DS1307_REG_SECS];
463 tmp = BCD2BIN(tmp & 0x7f);
466 tmp = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f);
470 tmp = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
471 if (tmp == 0 || tmp > 31)
474 tmp = BCD2BIN(ds1307->regs[DS1307_REG_MONTH] & 0x1f);
475 if (tmp == 0 || tmp > 12)
478 tmp = ds1307->regs[DS1307_REG_HOUR];
479 switch (ds1307->type) {
482 /* NOTE: ignores century bits; fix before deploying
483 * systems that will run through year 2100.
487 if (!(tmp & DS1307_BIT_12HR))
490 /* Be sure we're in 24 hour mode. Multi-master systems
493 tmp = BCD2BIN(tmp & 0x1f);
496 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
498 i2c_smbus_write_byte_data(client,
503 ds1307->rtc = rtc_device_register(client->name, &client->dev,
504 &ds13xx_rtc_ops, THIS_MODULE);
505 if (IS_ERR(ds1307->rtc)) {
506 err = PTR_ERR(ds1307->rtc);
507 dev_err(&client->dev,
508 "unable to register the class device\n");
513 err = sysfs_create_bin_file(&client->dev.kobj, &nvram);
515 ds1307->has_nvram = true;
516 dev_info(&client->dev, "56 bytes nvram\n");
523 dev_dbg(&client->dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
525 ds1307->regs[0], ds1307->regs[1],
526 ds1307->regs[2], ds1307->regs[3],
527 ds1307->regs[4], ds1307->regs[5],
535 static int __devexit ds1307_remove(struct i2c_client *client)
537 struct ds1307 *ds1307 = i2c_get_clientdata(client);
539 if (ds1307->has_nvram)
540 sysfs_remove_bin_file(&client->dev.kobj, &nvram);
542 rtc_device_unregister(ds1307->rtc);
547 static struct i2c_driver ds1307_driver = {
549 .name = "rtc-ds1307",
550 .owner = THIS_MODULE,
552 .probe = ds1307_probe,
553 .remove = __devexit_p(ds1307_remove),
556 static int __init ds1307_init(void)
558 return i2c_add_driver(&ds1307_driver);
560 module_init(ds1307_init);
562 static void __exit ds1307_exit(void)
564 i2c_del_driver(&ds1307_driver);
566 module_exit(ds1307_exit);
568 MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
569 MODULE_LICENSE("GPL");