fixup s3c24xx build after arch moves
[deliverable/linux.git] / drivers / rtc / rtc-ds1307.c
CommitLineData
1abb0dc9
DB
1/*
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 *
045e0e85
DB
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...
1abb0dc9
DB
30 */
31enum ds_type {
045e0e85
DB
32 ds_1307,
33 ds_1337,
34 ds_1338,
35 ds_1339,
36 ds_1340,
37 m41t00,
1abb0dc9
DB
38 // rs5c372 too? different address...
39};
40
1abb0dc9
DB
41
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
be5f59f4 45# define DS1340_BIT_nEOSC 0x80
1abb0dc9
DB
46#define DS1307_REG_MIN 0x01 /* 00-59 */
47#define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
c065f35c
DB
48# define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
49# define DS1307_BIT_PM 0x20 /* in REG_HOUR */
1abb0dc9
DB
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 */
57
58/* Other registers (control, status, alarms, trickle charge, NVRAM, etc)
045e0e85
DB
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.
1abb0dc9 61 */
045e0e85 62#define DS1307_REG_CONTROL 0x07 /* or ds1338 */
1abb0dc9 63# define DS1307_BIT_OUT 0x80
be5f59f4 64# define DS1338_BIT_OSF 0x20
1abb0dc9
DB
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
045e0e85
DB
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
be5f59f4
RG
80#define DS1340_REG_FLAG 0x09
81# define DS1340_BIT_OSF 0x80
1abb0dc9
DB
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
87
88
89
90struct ds1307 {
91 u8 reg_addr;
92 u8 regs[8];
93 enum ds_type type;
94 struct i2c_msg msg[2];
045e0e85
DB
95 struct i2c_client *client;
96 struct i2c_client dev;
1abb0dc9
DB
97 struct rtc_device *rtc;
98};
99
045e0e85
DB
100struct 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}
1abb0dc9
DB
140
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
045e0e85 146 /* read the RTC date and time registers all at once */
1abb0dc9
DB
147 ds1307->msg[1].flags = I2C_M_RD;
148 ds1307->msg[1].len = 7;
149
045e0e85
DB
150 tmp = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent),
151 ds1307->msg, 2);
1abb0dc9
DB
152 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
045e0e85
DB
182 /* initial clock setting can be undefined */
183 return rtc_valid_tm(t);
1abb0dc9
DB
184}
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",
11966adc
JG
195 "write", t->tm_sec, t->tm_min,
196 t->tm_hour, t->tm_mday,
197 t->tm_mon, t->tm_year, t->tm_wday);
1abb0dc9
DB
198
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
be5f59f4
RG
211 switch (ds1307->type) {
212 case ds_1337:
213 case ds_1339:
1abb0dc9 214 buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
be5f59f4
RG
215 break;
216 case ds_1340:
1abb0dc9
DB
217 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN
218 | DS1340_BIT_CENTURY;
be5f59f4
RG
219 break;
220 default:
221 break;
222 }
1abb0dc9
DB
223
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
045e0e85
DB
231 result = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent),
232 &ds1307->msg[1], 1);
1abb0dc9
DB
233 if (result != 1) {
234 dev_err(dev, "%s error %d\n", "write", tmp);
235 return -EIO;
236 }
237 return 0;
238}
239
ff8371ac 240static const struct rtc_class_ops ds13xx_rtc_ops = {
1abb0dc9
DB
241 .read_time = ds1307_get_time,
242 .set_time = ds1307_set_time,
243};
244
245static struct i2c_driver ds1307_driver;
246
c065f35c 247static int __devinit ds1307_probe(struct i2c_client *client)
1abb0dc9
DB
248{
249 struct ds1307 *ds1307;
250 int err = -ENODEV;
1abb0dc9 251 int tmp;
045e0e85 252 const struct chip_desc *chip;
c065f35c 253 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
1abb0dc9 254
c065f35c
DB
255 chip = find_chip(client->name);
256 if (!chip) {
257 dev_err(&client->dev, "unknown chip type '%s'\n",
258 client->name);
259 return -ENODEV;
1abb0dc9
DB
260 }
261
c065f35c
DB
262 if (!i2c_check_functionality(adapter,
263 I2C_FUNC_I2C | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
264 return -EIO;
265
266 if (!(ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL)))
267 return -ENOMEM;
045e0e85
DB
268
269 ds1307->client = client;
1abb0dc9
DB
270 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
045e0e85
DB
282 ds1307->type = chip->type;
283
284 switch (ds1307->type) {
285 case ds_1337:
286 case ds_1339:
1abb0dc9
DB
287 ds1307->reg_addr = DS1337_REG_CONTROL;
288 ds1307->msg[1].len = 2;
289
be5f59f4 290 /* get registers that the "rtc" read below won't read... */
045e0e85 291 tmp = i2c_transfer(adapter, ds1307->msg, 2);
1abb0dc9
DB
292 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
be5f59f4
RG
301 /* 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");
1abb0dc9 311 }
045e0e85
DB
312 break;
313 default:
314 break;
315 }
1abb0dc9
DB
316
317read_rtc:
318 /* read RTC registers */
319
045e0e85 320 tmp = i2c_transfer(adapter, ds1307->msg, 2);
1abb0dc9
DB
321 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];
045e0e85 332 switch (ds1307->type) {
be5f59f4
RG
333 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 */
045e0e85 338 case ds_1307:
045e0e85 339 case m41t00:
be5f59f4 340 /* clock halted? turn it on, so clock can tick. */
045e0e85 341 if (tmp & DS1307_BIT_CH) {
be5f59f4
RG
342 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
343 dev_warn(&client->dev, "SET TIME!\n");
045e0e85 344 goto read_rtc;
1abb0dc9 345 }
045e0e85 346 break;
be5f59f4
RG
347 case ds_1338:
348 /* clock halted? turn it on, so clock can tick. */
045e0e85 349 if (tmp & DS1307_BIT_CH)
be5f59f4
RG
350 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 }
045e0e85 360 break;
c065f35c
DB
361 case ds_1337:
362 case ds_1339:
045e0e85 363 break;
1abb0dc9 364 }
045e0e85
DB
365
366 tmp = ds1307->regs[DS1307_REG_SECS];
1abb0dc9
DB
367 tmp = BCD2BIN(tmp & 0x7f);
368 if (tmp > 60)
c065f35c 369 goto exit_bad;
1abb0dc9
DB
370 tmp = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f);
371 if (tmp > 60)
c065f35c 372 goto exit_bad;
1abb0dc9
DB
373
374 tmp = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
375 if (tmp == 0 || tmp > 31)
c065f35c 376 goto exit_bad;
1abb0dc9
DB
377
378 tmp = BCD2BIN(ds1307->regs[DS1307_REG_MONTH] & 0x1f);
379 if (tmp == 0 || tmp > 12)
c065f35c 380 goto exit_bad;
1abb0dc9 381
1abb0dc9 382 tmp = ds1307->regs[DS1307_REG_HOUR];
c065f35c
DB
383 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;
1abb0dc9
DB
402 i2c_smbus_write_byte_data(client,
403 DS1307_REG_HOUR,
404 BIN2BCD(tmp));
405 }
406
1abb0dc9
DB
407 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");
c065f35c 413 goto exit_free;
1abb0dc9
DB
414 }
415
416 return 0;
417
c065f35c
DB
418exit_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
1abb0dc9
DB
426exit_free:
427 kfree(ds1307);
1abb0dc9
DB
428 return err;
429}
430
c065f35c 431static int __devexit ds1307_remove(struct i2c_client *client)
1abb0dc9 432{
1abb0dc9
DB
433 struct ds1307 *ds1307 = i2c_get_clientdata(client);
434
435 rtc_device_unregister(ds1307->rtc);
1abb0dc9
DB
436 kfree(ds1307);
437 return 0;
438}
439
440static struct i2c_driver ds1307_driver = {
441 .driver = {
c065f35c 442 .name = "rtc-ds1307",
1abb0dc9
DB
443 .owner = THIS_MODULE,
444 },
c065f35c
DB
445 .probe = ds1307_probe,
446 .remove = __devexit_p(ds1307_remove),
1abb0dc9
DB
447};
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");
This page took 0.153944 seconds and 5 git commands to generate.