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...
31 * If the I2C "force" mechanism is used, we assume the chip is a ds1337.
32 * (Much better would be board-specific tables of I2C devices, along with
33 * the platform_data drivers would use to sort such issues out.)
43 // rs5c372 too? different address...
46 static unsigned short normal_i2c[] = { 0x68, I2C_CLIENT_END };
52 /* RTC registers don't differ much, except for the century flag */
53 #define DS1307_REG_SECS 0x00 /* 00-59 */
54 # define DS1307_BIT_CH 0x80
55 #define DS1307_REG_MIN 0x01 /* 00-59 */
56 #define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
57 # define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
58 # define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
59 #define DS1307_REG_WDAY 0x03 /* 01-07 */
60 #define DS1307_REG_MDAY 0x04 /* 01-31 */
61 #define DS1307_REG_MONTH 0x05 /* 01-12 */
62 # define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
63 #define DS1307_REG_YEAR 0x06 /* 00-99 */
65 /* Other registers (control, status, alarms, trickle charge, NVRAM, etc)
66 * start at 7, and they differ a LOT. Only control and status matter for
67 * basic RTC date and time functionality; be careful using them.
69 #define DS1307_REG_CONTROL 0x07 /* or ds1338 */
70 # define DS1307_BIT_OUT 0x80
71 # define DS1338_BIT_STOP 0x20
72 # define DS1307_BIT_SQWE 0x10
73 # define DS1307_BIT_RS1 0x02
74 # define DS1307_BIT_RS0 0x01
75 #define DS1337_REG_CONTROL 0x0e
76 # define DS1337_BIT_nEOSC 0x80
77 # define DS1337_BIT_RS2 0x10
78 # define DS1337_BIT_RS1 0x08
79 # define DS1337_BIT_INTCN 0x04
80 # define DS1337_BIT_A2IE 0x02
81 # define DS1337_BIT_A1IE 0x01
82 #define DS1340_REG_CONTROL 0x07
83 # define DS1340_BIT_OUT 0x80
84 # define DS1340_BIT_FT 0x40
85 # define DS1340_BIT_CALIB_SIGN 0x20
86 # define DS1340_M_CALIBRATION 0x1f
87 #define DS1338_REG_FLAG 0x09
88 # define DS1338_BIT_OSF 0x80
89 #define DS1337_REG_STATUS 0x0f
90 # define DS1337_BIT_OSF 0x80
91 # define DS1337_BIT_A2I 0x02
92 # define DS1337_BIT_A1I 0x01
93 #define DS1339_REG_TRICKLE 0x10
101 struct i2c_msg msg[2];
102 struct i2c_client *client;
103 struct i2c_client dev;
104 struct rtc_device *rtc;
114 static const struct chip_desc chips[] = { {
138 static inline const struct chip_desc *find_chip(const char *s)
142 for (i = 0; i < ARRAY_SIZE(chips); i++)
143 if (strnicmp(s, chips[i].name, sizeof chips[i].name) == 0)
148 static int ds1307_get_time(struct device *dev, struct rtc_time *t)
150 struct ds1307 *ds1307 = dev_get_drvdata(dev);
153 /* read the RTC date and time registers all at once */
154 ds1307->msg[1].flags = I2C_M_RD;
155 ds1307->msg[1].len = 7;
157 tmp = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent),
160 dev_err(dev, "%s error %d\n", "read", tmp);
164 dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
166 ds1307->regs[0], ds1307->regs[1],
167 ds1307->regs[2], ds1307->regs[3],
168 ds1307->regs[4], ds1307->regs[5],
171 t->tm_sec = BCD2BIN(ds1307->regs[DS1307_REG_SECS] & 0x7f);
172 t->tm_min = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f);
173 tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
174 t->tm_hour = BCD2BIN(tmp);
175 t->tm_wday = BCD2BIN(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
176 t->tm_mday = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
177 tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
178 t->tm_mon = BCD2BIN(tmp) - 1;
180 /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
181 t->tm_year = BCD2BIN(ds1307->regs[DS1307_REG_YEAR]) + 100;
183 dev_dbg(dev, "%s secs=%d, mins=%d, "
184 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
185 "read", t->tm_sec, t->tm_min,
186 t->tm_hour, t->tm_mday,
187 t->tm_mon, t->tm_year, t->tm_wday);
189 /* initial clock setting can be undefined */
190 return rtc_valid_tm(t);
193 static int ds1307_set_time(struct device *dev, struct rtc_time *t)
195 struct ds1307 *ds1307 = dev_get_drvdata(dev);
198 u8 *buf = ds1307->regs;
200 dev_dbg(dev, "%s secs=%d, mins=%d, "
201 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
202 "write", t->tm_sec, t->tm_min,
203 t->tm_hour, t->tm_mday,
204 t->tm_mon, t->tm_year, t->tm_wday);
206 *buf++ = 0; /* first register addr */
207 buf[DS1307_REG_SECS] = BIN2BCD(t->tm_sec);
208 buf[DS1307_REG_MIN] = BIN2BCD(t->tm_min);
209 buf[DS1307_REG_HOUR] = BIN2BCD(t->tm_hour);
210 buf[DS1307_REG_WDAY] = BIN2BCD(t->tm_wday + 1);
211 buf[DS1307_REG_MDAY] = BIN2BCD(t->tm_mday);
212 buf[DS1307_REG_MONTH] = BIN2BCD(t->tm_mon + 1);
214 /* assume 20YY not 19YY */
215 tmp = t->tm_year - 100;
216 buf[DS1307_REG_YEAR] = BIN2BCD(tmp);
218 if (ds1307->type == ds_1337)
219 buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
220 else if (ds1307->type == ds_1340)
221 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN
222 | DS1340_BIT_CENTURY;
224 ds1307->msg[1].flags = 0;
225 ds1307->msg[1].len = 8;
227 dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
228 "write", buf[0], buf[1], buf[2], buf[3],
229 buf[4], buf[5], buf[6]);
231 result = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent),
234 dev_err(dev, "%s error %d\n", "write", tmp);
240 static const struct rtc_class_ops ds13xx_rtc_ops = {
241 .read_time = ds1307_get_time,
242 .set_time = ds1307_set_time,
245 static struct i2c_driver ds1307_driver;
248 ds1307_detect(struct i2c_adapter *adapter, int address, int kind)
250 struct ds1307 *ds1307;
252 struct i2c_client *client;
254 const struct chip_desc *chip;
256 if (!(ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL))) {
261 /* REVISIT: pending driver model conversion, set up "client"
262 * ourselves, and use a hack to determine the RTC type (instead
263 * of reading the client->name we're given)
265 client = &ds1307->dev;
266 client->addr = address;
267 client->adapter = adapter;
268 client->driver = &ds1307_driver;
270 /* HACK: "force" implies "needs ds1337-style-oscillator setup", and
271 * that's the only kind of chip setup we'll know about. Until the
272 * driver model conversion, here's where to add any board-specific
273 * code to say what kind of chip is present...
276 chip = find_chip("ds1337");
278 chip = find_chip("ds1307");
279 strlcpy(client->name, chip->name, I2C_NAME_SIZE);
281 ds1307->client = client;
282 i2c_set_clientdata(client, ds1307);
284 ds1307->msg[0].addr = client->addr;
285 ds1307->msg[0].flags = 0;
286 ds1307->msg[0].len = 1;
287 ds1307->msg[0].buf = &ds1307->reg_addr;
289 ds1307->msg[1].addr = client->addr;
290 ds1307->msg[1].flags = I2C_M_RD;
291 ds1307->msg[1].len = sizeof(ds1307->regs);
292 ds1307->msg[1].buf = ds1307->regs;
294 ds1307->type = chip->type;
296 switch (ds1307->type) {
299 ds1307->type = ds_1337;
301 ds1307->reg_addr = DS1337_REG_CONTROL;
302 ds1307->msg[1].len = 2;
304 tmp = i2c_transfer(adapter, ds1307->msg, 2);
306 pr_debug("read error %d\n", tmp);
311 ds1307->reg_addr = 0;
312 ds1307->msg[1].len = sizeof(ds1307->regs);
314 /* oscillator is off; need to turn it on */
315 if ((ds1307->regs[0] & DS1337_BIT_nEOSC)
316 || (ds1307->regs[1] & DS1337_BIT_OSF)) {
318 printk(KERN_ERR "no %s oscillator code\n",
328 /* read RTC registers */
330 tmp = i2c_transfer(adapter, ds1307->msg, 2);
332 pr_debug("read error %d\n", tmp);
337 /* minimal sanity checking; some chips (like DS1340) don't
338 * specify the extra bits as must-be-zero, but there are
339 * still a few values that are clearly out-of-range.
341 tmp = ds1307->regs[DS1307_REG_SECS];
342 switch (ds1307->type) {
346 if (tmp & DS1307_BIT_CH) {
347 i2c_smbus_write_byte_data(client, 0, 0);
348 dev_warn(&client->dev,
349 "oscillator started; SET TIME!\n");
354 /* FIXME write code to start the oscillator */
355 if (tmp & DS1307_BIT_CH)
362 tmp = ds1307->regs[DS1307_REG_SECS];
363 tmp = BCD2BIN(tmp & 0x7f);
366 tmp = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f);
370 tmp = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
371 if (tmp == 0 || tmp > 31)
374 tmp = BCD2BIN(ds1307->regs[DS1307_REG_MONTH] & 0x1f);
375 if (tmp == 0 || tmp > 12)
378 /* force into in 24 hour mode (most chips) or
379 * disable century bit (ds1340)
381 * REVISIT forcing 24 hour mode can prevent multi-master
382 * configs from sharing this RTC ... don't do this.
384 tmp = ds1307->regs[DS1307_REG_HOUR];
385 if (tmp & (1 << 6)) {
387 tmp = BCD2BIN(tmp & 0x1f) + 12;
390 i2c_smbus_write_byte_data(client,
395 /* Tell the I2C layer a new client has arrived */
396 if ((err = i2c_attach_client(client)))
399 ds1307->rtc = rtc_device_register(client->name, &client->dev,
400 &ds13xx_rtc_ops, THIS_MODULE);
401 if (IS_ERR(ds1307->rtc)) {
402 err = PTR_ERR(ds1307->rtc);
403 dev_err(&client->dev,
404 "unable to register the class device\n");
411 i2c_detach_client(client);
419 ds1307_attach_adapter(struct i2c_adapter *adapter)
421 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
423 return i2c_probe(adapter, &addr_data, ds1307_detect);
426 static int __devexit ds1307_detach_client(struct i2c_client *client)
429 struct ds1307 *ds1307 = i2c_get_clientdata(client);
431 rtc_device_unregister(ds1307->rtc);
432 if ((err = i2c_detach_client(client)))
438 static struct i2c_driver ds1307_driver = {
441 .owner = THIS_MODULE,
443 .attach_adapter = ds1307_attach_adapter,
444 .detach_client = __devexit_p(ds1307_detach_client),
447 static int __init ds1307_init(void)
449 return i2c_add_driver(&ds1307_driver);
451 module_init(ds1307_init);
453 static void __exit ds1307_exit(void)
455 i2c_del_driver(&ds1307_driver);
457 module_exit(ds1307_exit);
459 MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
460 MODULE_LICENSE("GPL");