blob: 5158a625671f8f4a1de2330cd22b25ed32ea9e4b [file] [log] [blame]
David Brownell1abb0dc2006-06-25 05:48:17 -07001/*
2 * rtc-ds1307.c - RTC driver for some mostly-compatible I2C chips.
3 *
4 * Copyright (C) 2005 James Chapman (ds1337 core)
5 * Copyright (C) 2006 David Brownell
6 *
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.
10 */
11
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>
19
20
21
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.
26 *
David Brownell045e0e82007-07-17 04:04:55 -070027 * 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...
David Brownell1abb0dc2006-06-25 05:48:17 -070030 */
31enum ds_type {
David Brownell045e0e82007-07-17 04:04:55 -070032 ds_1307,
33 ds_1337,
34 ds_1338,
35 ds_1339,
36 ds_1340,
37 m41t00,
David Brownell1abb0dc2006-06-25 05:48:17 -070038 // rs5c372 too? different address...
39};
40
David Brownell1abb0dc2006-06-25 05:48:17 -070041
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
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070045# define DS1340_BIT_nEOSC 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070046#define DS1307_REG_MIN 0x01 /* 00-59 */
47#define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
David Brownellc065f352007-07-17 04:05:10 -070048# define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
49# define DS1307_BIT_PM 0x20 /* in REG_HOUR */
David Brownell1abb0dc2006-06-25 05:48:17 -070050# 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 */
57
58/* Other registers (control, status, alarms, trickle charge, NVRAM, etc)
David Brownell045e0e82007-07-17 04:04:55 -070059 * 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.
David Brownell1abb0dc2006-06-25 05:48:17 -070061 */
David Brownell045e0e82007-07-17 04:04:55 -070062#define DS1307_REG_CONTROL 0x07 /* or ds1338 */
David Brownell1abb0dc2006-06-25 05:48:17 -070063# define DS1307_BIT_OUT 0x80
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070064# define DS1338_BIT_OSF 0x20
David Brownell1abb0dc2006-06-25 05:48:17 -070065# 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
David Brownell045e0e82007-07-17 04:04:55 -070075#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
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -070080#define DS1340_REG_FLAG 0x09
81# define DS1340_BIT_OSF 0x80
David Brownell1abb0dc2006-06-25 05:48:17 -070082#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
87
88
89
90struct ds1307 {
91 u8 reg_addr;
92 u8 regs[8];
93 enum ds_type type;
94 struct i2c_msg msg[2];
David Brownell045e0e82007-07-17 04:04:55 -070095 struct i2c_client *client;
96 struct i2c_client dev;
David Brownell1abb0dc2006-06-25 05:48:17 -070097 struct rtc_device *rtc;
98};
99
David Brownell045e0e82007-07-17 04:04:55 -0700100struct chip_desc {
101 char name[9];
102 unsigned nvram56:1;
103 unsigned alarm:1;
104 enum ds_type type;
105};
106
107static const struct chip_desc chips[] = { {
108 .name = "ds1307",
109 .type = ds_1307,
110 .nvram56 = 1,
111}, {
112 .name = "ds1337",
113 .type = ds_1337,
114 .alarm = 1,
115}, {
116 .name = "ds1338",
117 .type = ds_1338,
118 .nvram56 = 1,
119}, {
120 .name = "ds1339",
121 .type = ds_1339,
122 .alarm = 1,
123}, {
124 .name = "ds1340",
125 .type = ds_1340,
126}, {
127 .name = "m41t00",
128 .type = m41t00,
129}, };
130
131static inline const struct chip_desc *find_chip(const char *s)
132{
133 unsigned i;
134
135 for (i = 0; i < ARRAY_SIZE(chips); i++)
136 if (strnicmp(s, chips[i].name, sizeof chips[i].name) == 0)
137 return &chips[i];
138 return NULL;
139}
David Brownell1abb0dc2006-06-25 05:48:17 -0700140
141static int ds1307_get_time(struct device *dev, struct rtc_time *t)
142{
143 struct ds1307 *ds1307 = dev_get_drvdata(dev);
144 int tmp;
145
David Brownell045e0e82007-07-17 04:04:55 -0700146 /* read the RTC date and time registers all at once */
David Brownell1abb0dc2006-06-25 05:48:17 -0700147 ds1307->msg[1].flags = I2C_M_RD;
148 ds1307->msg[1].len = 7;
149
David Brownell045e0e82007-07-17 04:04:55 -0700150 tmp = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent),
151 ds1307->msg, 2);
David Brownell1abb0dc2006-06-25 05:48:17 -0700152 if (tmp != 2) {
153 dev_err(dev, "%s error %d\n", "read", tmp);
154 return -EIO;
155 }
156
157 dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
158 "read",
159 ds1307->regs[0], ds1307->regs[1],
160 ds1307->regs[2], ds1307->regs[3],
161 ds1307->regs[4], ds1307->regs[5],
162 ds1307->regs[6]);
163
164 t->tm_sec = BCD2BIN(ds1307->regs[DS1307_REG_SECS] & 0x7f);
165 t->tm_min = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f);
166 tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
167 t->tm_hour = BCD2BIN(tmp);
168 t->tm_wday = BCD2BIN(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
169 t->tm_mday = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
170 tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
171 t->tm_mon = BCD2BIN(tmp) - 1;
172
173 /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
174 t->tm_year = BCD2BIN(ds1307->regs[DS1307_REG_YEAR]) + 100;
175
176 dev_dbg(dev, "%s secs=%d, mins=%d, "
177 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
178 "read", t->tm_sec, t->tm_min,
179 t->tm_hour, t->tm_mday,
180 t->tm_mon, t->tm_year, t->tm_wday);
181
David Brownell045e0e82007-07-17 04:04:55 -0700182 /* initial clock setting can be undefined */
183 return rtc_valid_tm(t);
David Brownell1abb0dc2006-06-25 05:48:17 -0700184}
185
186static int ds1307_set_time(struct device *dev, struct rtc_time *t)
187{
188 struct ds1307 *ds1307 = dev_get_drvdata(dev);
189 int result;
190 int tmp;
191 u8 *buf = ds1307->regs;
192
193 dev_dbg(dev, "%s secs=%d, mins=%d, "
194 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
Jeff Garzik11966ad2006-10-04 04:41:53 -0400195 "write", t->tm_sec, t->tm_min,
196 t->tm_hour, t->tm_mday,
197 t->tm_mon, t->tm_year, t->tm_wday);
David Brownell1abb0dc2006-06-25 05:48:17 -0700198
199 *buf++ = 0; /* first register addr */
200 buf[DS1307_REG_SECS] = BIN2BCD(t->tm_sec);
201 buf[DS1307_REG_MIN] = BIN2BCD(t->tm_min);
202 buf[DS1307_REG_HOUR] = BIN2BCD(t->tm_hour);
203 buf[DS1307_REG_WDAY] = BIN2BCD(t->tm_wday + 1);
204 buf[DS1307_REG_MDAY] = BIN2BCD(t->tm_mday);
205 buf[DS1307_REG_MONTH] = BIN2BCD(t->tm_mon + 1);
206
207 /* assume 20YY not 19YY */
208 tmp = t->tm_year - 100;
209 buf[DS1307_REG_YEAR] = BIN2BCD(tmp);
210
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700211 switch (ds1307->type) {
212 case ds_1337:
213 case ds_1339:
David Brownell1abb0dc2006-06-25 05:48:17 -0700214 buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700215 break;
216 case ds_1340:
David Brownell1abb0dc2006-06-25 05:48:17 -0700217 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN
218 | DS1340_BIT_CENTURY;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700219 break;
220 default:
221 break;
222 }
David Brownell1abb0dc2006-06-25 05:48:17 -0700223
224 ds1307->msg[1].flags = 0;
225 ds1307->msg[1].len = 8;
226
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]);
230
David Brownell045e0e82007-07-17 04:04:55 -0700231 result = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent),
232 &ds1307->msg[1], 1);
David Brownell1abb0dc2006-06-25 05:48:17 -0700233 if (result != 1) {
234 dev_err(dev, "%s error %d\n", "write", tmp);
235 return -EIO;
236 }
237 return 0;
238}
239
David Brownellff8371a2006-09-30 23:28:17 -0700240static const struct rtc_class_ops ds13xx_rtc_ops = {
David Brownell1abb0dc2006-06-25 05:48:17 -0700241 .read_time = ds1307_get_time,
242 .set_time = ds1307_set_time,
243};
244
245static struct i2c_driver ds1307_driver;
246
David Brownellc065f352007-07-17 04:05:10 -0700247static int __devinit ds1307_probe(struct i2c_client *client)
David Brownell1abb0dc2006-06-25 05:48:17 -0700248{
249 struct ds1307 *ds1307;
250 int err = -ENODEV;
David Brownell1abb0dc2006-06-25 05:48:17 -0700251 int tmp;
David Brownell045e0e82007-07-17 04:04:55 -0700252 const struct chip_desc *chip;
David Brownellc065f352007-07-17 04:05:10 -0700253 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
David Brownell1abb0dc2006-06-25 05:48:17 -0700254
David Brownellc065f352007-07-17 04:05:10 -0700255 chip = find_chip(client->name);
256 if (!chip) {
257 dev_err(&client->dev, "unknown chip type '%s'\n",
258 client->name);
259 return -ENODEV;
David Brownell1abb0dc2006-06-25 05:48:17 -0700260 }
261
David Brownellc065f352007-07-17 04:05:10 -0700262 if (!i2c_check_functionality(adapter,
263 I2C_FUNC_I2C | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
264 return -EIO;
David Brownell1abb0dc2006-06-25 05:48:17 -0700265
David Brownellc065f352007-07-17 04:05:10 -0700266 if (!(ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL)))
267 return -ENOMEM;
David Brownell045e0e82007-07-17 04:04:55 -0700268
269 ds1307->client = client;
David Brownell1abb0dc2006-06-25 05:48:17 -0700270 i2c_set_clientdata(client, ds1307);
271
272 ds1307->msg[0].addr = client->addr;
273 ds1307->msg[0].flags = 0;
274 ds1307->msg[0].len = 1;
275 ds1307->msg[0].buf = &ds1307->reg_addr;
276
277 ds1307->msg[1].addr = client->addr;
278 ds1307->msg[1].flags = I2C_M_RD;
279 ds1307->msg[1].len = sizeof(ds1307->regs);
280 ds1307->msg[1].buf = ds1307->regs;
281
David Brownell045e0e82007-07-17 04:04:55 -0700282 ds1307->type = chip->type;
283
284 switch (ds1307->type) {
285 case ds_1337:
286 case ds_1339:
David Brownell1abb0dc2006-06-25 05:48:17 -0700287 ds1307->reg_addr = DS1337_REG_CONTROL;
288 ds1307->msg[1].len = 2;
289
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700290 /* get registers that the "rtc" read below won't read... */
David Brownell045e0e82007-07-17 04:04:55 -0700291 tmp = i2c_transfer(adapter, ds1307->msg, 2);
David Brownell1abb0dc2006-06-25 05:48:17 -0700292 if (tmp != 2) {
293 pr_debug("read error %d\n", tmp);
294 err = -EIO;
295 goto exit_free;
296 }
297
298 ds1307->reg_addr = 0;
299 ds1307->msg[1].len = sizeof(ds1307->regs);
300
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700301 /* oscillator off? turn it on, so clock can tick. */
302 if (ds1307->regs[0] & DS1337_BIT_nEOSC)
303 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL,
304 ds1307->regs[0] & ~DS1337_BIT_nEOSC);
305
306 /* oscillator fault? clear flag, and warn */
307 if (ds1307->regs[1] & DS1337_BIT_OSF) {
308 i2c_smbus_write_byte_data(client, DS1337_REG_STATUS,
309 ds1307->regs[1] & ~DS1337_BIT_OSF);
310 dev_warn(&client->dev, "SET TIME!\n");
David Brownell1abb0dc2006-06-25 05:48:17 -0700311 }
David Brownell045e0e82007-07-17 04:04:55 -0700312 break;
313 default:
314 break;
315 }
David Brownell1abb0dc2006-06-25 05:48:17 -0700316
317read_rtc:
318 /* read RTC registers */
319
David Brownell045e0e82007-07-17 04:04:55 -0700320 tmp = i2c_transfer(adapter, ds1307->msg, 2);
David Brownell1abb0dc2006-06-25 05:48:17 -0700321 if (tmp != 2) {
322 pr_debug("read error %d\n", tmp);
323 err = -EIO;
324 goto exit_free;
325 }
326
327 /* minimal sanity checking; some chips (like DS1340) don't
328 * specify the extra bits as must-be-zero, but there are
329 * still a few values that are clearly out-of-range.
330 */
331 tmp = ds1307->regs[DS1307_REG_SECS];
David Brownell045e0e82007-07-17 04:04:55 -0700332 switch (ds1307->type) {
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700333 case ds_1340:
334 /* FIXME read register with DS1340_BIT_OSF, use that to
335 * trigger the "set time" warning (*after* restarting the
336 * oscillator!) instead of this weaker ds1307/m41t00 test.
337 */
David Brownell045e0e82007-07-17 04:04:55 -0700338 case ds_1307:
David Brownell045e0e82007-07-17 04:04:55 -0700339 case m41t00:
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700340 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -0700341 if (tmp & DS1307_BIT_CH) {
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700342 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
343 dev_warn(&client->dev, "SET TIME!\n");
David Brownell045e0e82007-07-17 04:04:55 -0700344 goto read_rtc;
David Brownell1abb0dc2006-06-25 05:48:17 -0700345 }
David Brownell045e0e82007-07-17 04:04:55 -0700346 break;
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700347 case ds_1338:
348 /* clock halted? turn it on, so clock can tick. */
David Brownell045e0e82007-07-17 04:04:55 -0700349 if (tmp & DS1307_BIT_CH)
Rodolfo Giomettibe5f59f2007-07-17 04:05:06 -0700350 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
351
352 /* oscillator fault? clear flag, and warn */
353 if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
354 i2c_smbus_write_byte_data(client, DS1307_REG_CONTROL,
355 ds1307->regs[DS1337_REG_CONTROL]
356 & ~DS1338_BIT_OSF);
357 dev_warn(&client->dev, "SET TIME!\n");
358 goto read_rtc;
359 }
David Brownell045e0e82007-07-17 04:04:55 -0700360 break;
David Brownellc065f352007-07-17 04:05:10 -0700361 case ds_1337:
362 case ds_1339:
David Brownell045e0e82007-07-17 04:04:55 -0700363 break;
David Brownell1abb0dc2006-06-25 05:48:17 -0700364 }
David Brownell045e0e82007-07-17 04:04:55 -0700365
366 tmp = ds1307->regs[DS1307_REG_SECS];
David Brownell1abb0dc2006-06-25 05:48:17 -0700367 tmp = BCD2BIN(tmp & 0x7f);
368 if (tmp > 60)
David Brownellc065f352007-07-17 04:05:10 -0700369 goto exit_bad;
David Brownell1abb0dc2006-06-25 05:48:17 -0700370 tmp = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f);
371 if (tmp > 60)
David Brownellc065f352007-07-17 04:05:10 -0700372 goto exit_bad;
David Brownell1abb0dc2006-06-25 05:48:17 -0700373
374 tmp = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
375 if (tmp == 0 || tmp > 31)
David Brownellc065f352007-07-17 04:05:10 -0700376 goto exit_bad;
David Brownell1abb0dc2006-06-25 05:48:17 -0700377
378 tmp = BCD2BIN(ds1307->regs[DS1307_REG_MONTH] & 0x1f);
379 if (tmp == 0 || tmp > 12)
David Brownellc065f352007-07-17 04:05:10 -0700380 goto exit_bad;
David Brownell1abb0dc2006-06-25 05:48:17 -0700381
David Brownell1abb0dc2006-06-25 05:48:17 -0700382 tmp = ds1307->regs[DS1307_REG_HOUR];
David Brownellc065f352007-07-17 04:05:10 -0700383 switch (ds1307->type) {
384 case ds_1340:
385 case m41t00:
386 /* NOTE: ignores century bits; fix before deploying
387 * systems that will run through year 2100.
388 */
389 break;
390 default:
391 if (!(tmp & DS1307_BIT_12HR))
392 break;
393
394 /* Be sure we're in 24 hour mode. Multi-master systems
395 * take note...
396 */
397 tmp = BCD2BIN(tmp & 0x1f);
398 if (tmp == 12)
399 tmp = 0;
400 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
401 tmp += 12;
David Brownell1abb0dc2006-06-25 05:48:17 -0700402 i2c_smbus_write_byte_data(client,
403 DS1307_REG_HOUR,
404 BIN2BCD(tmp));
405 }
406
David Brownell1abb0dc2006-06-25 05:48:17 -0700407 ds1307->rtc = rtc_device_register(client->name, &client->dev,
408 &ds13xx_rtc_ops, THIS_MODULE);
409 if (IS_ERR(ds1307->rtc)) {
410 err = PTR_ERR(ds1307->rtc);
411 dev_err(&client->dev,
412 "unable to register the class device\n");
David Brownellc065f352007-07-17 04:05:10 -0700413 goto exit_free;
David Brownell1abb0dc2006-06-25 05:48:17 -0700414 }
415
416 return 0;
417
David Brownellc065f352007-07-17 04:05:10 -0700418exit_bad:
419 dev_dbg(&client->dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
420 "bogus register",
421 ds1307->regs[0], ds1307->regs[1],
422 ds1307->regs[2], ds1307->regs[3],
423 ds1307->regs[4], ds1307->regs[5],
424 ds1307->regs[6]);
425
David Brownell1abb0dc2006-06-25 05:48:17 -0700426exit_free:
427 kfree(ds1307);
David Brownell1abb0dc2006-06-25 05:48:17 -0700428 return err;
429}
430
David Brownellc065f352007-07-17 04:05:10 -0700431static int __devexit ds1307_remove(struct i2c_client *client)
David Brownell1abb0dc2006-06-25 05:48:17 -0700432{
David Brownell1abb0dc2006-06-25 05:48:17 -0700433 struct ds1307 *ds1307 = i2c_get_clientdata(client);
434
435 rtc_device_unregister(ds1307->rtc);
David Brownell1abb0dc2006-06-25 05:48:17 -0700436 kfree(ds1307);
437 return 0;
438}
439
440static struct i2c_driver ds1307_driver = {
441 .driver = {
David Brownellc065f352007-07-17 04:05:10 -0700442 .name = "rtc-ds1307",
David Brownell1abb0dc2006-06-25 05:48:17 -0700443 .owner = THIS_MODULE,
444 },
David Brownellc065f352007-07-17 04:05:10 -0700445 .probe = ds1307_probe,
446 .remove = __devexit_p(ds1307_remove),
David Brownell1abb0dc2006-06-25 05:48:17 -0700447};
448
449static int __init ds1307_init(void)
450{
451 return i2c_add_driver(&ds1307_driver);
452}
453module_init(ds1307_init);
454
455static void __exit ds1307_exit(void)
456{
457 i2c_del_driver(&ds1307_driver);
458}
459module_exit(ds1307_exit);
460
461MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
462MODULE_LICENSE("GPL");