rtc-ds1307 cleanups
[linux-2.6.git] / drivers / rtc / rtc-ds1307.c
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  *
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...
30  *
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.)
34  */
35 enum ds_type {
36         unknown = 0,
37         ds_1307,
38         ds_1337,
39         ds_1338,
40         ds_1339,
41         ds_1340,
42         m41t00,
43         // rs5c372 too?  different address...
44 };
45
46 static unsigned short normal_i2c[] = { 0x68, I2C_CLIENT_END };
47
48 I2C_CLIENT_INSMOD;
49
50
51
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 */
64
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.
68  */
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
94
95
96
97 struct ds1307 {
98         u8                      reg_addr;
99         u8                      regs[8];
100         enum ds_type            type;
101         struct i2c_msg          msg[2];
102         struct i2c_client       *client;
103         struct i2c_client       dev;
104         struct rtc_device       *rtc;
105 };
106
107 struct chip_desc {
108         char                    name[9];
109         unsigned                nvram56:1;
110         unsigned                alarm:1;
111         enum ds_type            type;
112 };
113
114 static const struct chip_desc chips[] = { {
115         .name           = "ds1307",
116         .type           = ds_1307,
117         .nvram56        = 1,
118 }, {
119         .name           = "ds1337",
120         .type           = ds_1337,
121         .alarm          = 1,
122 }, {
123         .name           = "ds1338",
124         .type           = ds_1338,
125         .nvram56        = 1,
126 }, {
127         .name           = "ds1339",
128         .type           = ds_1339,
129         .alarm          = 1,
130 }, {
131         .name           = "ds1340",
132         .type           = ds_1340,
133 }, {
134         .name           = "m41t00",
135         .type           = m41t00,
136 }, };
137
138 static inline const struct chip_desc *find_chip(const char *s)
139 {
140         unsigned i;
141
142         for (i = 0; i < ARRAY_SIZE(chips); i++)
143                 if (strnicmp(s, chips[i].name, sizeof chips[i].name) == 0)
144                         return &chips[i];
145         return NULL;
146 }
147
148 static int ds1307_get_time(struct device *dev, struct rtc_time *t)
149 {
150         struct ds1307   *ds1307 = dev_get_drvdata(dev);
151         int             tmp;
152
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;
156
157         tmp = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent),
158                         ds1307->msg, 2);
159         if (tmp != 2) {
160                 dev_err(dev, "%s error %d\n", "read", tmp);
161                 return -EIO;
162         }
163
164         dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
165                         "read",
166                         ds1307->regs[0], ds1307->regs[1],
167                         ds1307->regs[2], ds1307->regs[3],
168                         ds1307->regs[4], ds1307->regs[5],
169                         ds1307->regs[6]);
170
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;
179
180         /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
181         t->tm_year = BCD2BIN(ds1307->regs[DS1307_REG_YEAR]) + 100;
182
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);
188
189         /* initial clock setting can be undefined */
190         return rtc_valid_tm(t);
191 }
192
193 static int ds1307_set_time(struct device *dev, struct rtc_time *t)
194 {
195         struct ds1307   *ds1307 = dev_get_drvdata(dev);
196         int             result;
197         int             tmp;
198         u8              *buf = ds1307->regs;
199
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);
205
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);
213
214         /* assume 20YY not 19YY */
215         tmp = t->tm_year - 100;
216         buf[DS1307_REG_YEAR] = BIN2BCD(tmp);
217
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;
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
231         result = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent),
232                         &ds1307->msg[1], 1);
233         if (result != 1) {
234                 dev_err(dev, "%s error %d\n", "write", tmp);
235                 return -EIO;
236         }
237         return 0;
238 }
239
240 static const struct rtc_class_ops ds13xx_rtc_ops = {
241         .read_time      = ds1307_get_time,
242         .set_time       = ds1307_set_time,
243 };
244
245 static struct i2c_driver ds1307_driver;
246
247 static int __devinit
248 ds1307_detect(struct i2c_adapter *adapter, int address, int kind)
249 {
250         struct ds1307           *ds1307;
251         int                     err = -ENODEV;
252         struct i2c_client       *client;
253         int                     tmp;
254         const struct chip_desc  *chip;
255
256         if (!(ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL))) {
257                 err = -ENOMEM;
258                 goto exit;
259         }
260
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)
264          */
265         client = &ds1307->dev;
266         client->addr = address;
267         client->adapter = adapter;
268         client->driver = &ds1307_driver;
269
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...
274          */
275         if (kind >= 0)
276                 chip = find_chip("ds1337");
277         else
278                 chip = find_chip("ds1307");
279         strlcpy(client->name, chip->name, I2C_NAME_SIZE);
280
281         ds1307->client = client;
282         i2c_set_clientdata(client, ds1307);
283
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;
288
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;
293
294         ds1307->type = chip->type;
295
296         switch (ds1307->type) {
297         case ds_1337:
298         case ds_1339:
299                 ds1307->type = ds_1337;
300
301                 ds1307->reg_addr = DS1337_REG_CONTROL;
302                 ds1307->msg[1].len = 2;
303
304                 tmp = i2c_transfer(adapter, ds1307->msg, 2);
305                 if (tmp != 2) {
306                         pr_debug("read error %d\n", tmp);
307                         err = -EIO;
308                         goto exit_free;
309                 }
310
311                 ds1307->reg_addr = 0;
312                 ds1307->msg[1].len = sizeof(ds1307->regs);
313
314                 /* oscillator is off; need to turn it on */
315                 if ((ds1307->regs[0] & DS1337_BIT_nEOSC)
316                                 || (ds1307->regs[1] & DS1337_BIT_OSF)) {
317 no_osc_start:
318                         printk(KERN_ERR "no %s oscillator code\n",
319                                 chip->name);
320                         goto exit_free;
321                 }
322                 break;
323         default:
324                 break;
325         }
326
327 read_rtc:
328         /* read RTC registers */
329
330         tmp = i2c_transfer(adapter, ds1307->msg, 2);
331         if (tmp != 2) {
332                 pr_debug("read error %d\n", tmp);
333                 err = -EIO;
334                 goto exit_free;
335         }
336
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.
340          */
341         tmp = ds1307->regs[DS1307_REG_SECS];
342         switch (ds1307->type) {
343         case ds_1307:
344         case ds_1338:
345         case m41t00:
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");
350                         goto read_rtc;
351                 }
352                 break;
353         case ds_1340:
354                 /* FIXME write code to start the oscillator */
355                 if (tmp & DS1307_BIT_CH)
356                         goto no_osc_start;
357                 break;
358         default:
359                 break;
360         }
361
362         tmp = ds1307->regs[DS1307_REG_SECS];
363         tmp = BCD2BIN(tmp & 0x7f);
364         if (tmp > 60)
365                 goto exit_free;
366         tmp = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f);
367         if (tmp > 60)
368                 goto exit_free;
369
370         tmp = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
371         if (tmp == 0 || tmp > 31)
372                 goto exit_free;
373
374         tmp = BCD2BIN(ds1307->regs[DS1307_REG_MONTH] & 0x1f);
375         if (tmp == 0 || tmp > 12)
376                 goto exit_free;
377
378         /* force into in 24 hour mode (most chips) or
379          * disable century bit (ds1340)
380          *
381          * REVISIT forcing 24 hour mode can prevent multi-master
382          * configs from sharing this RTC ... don't do this.
383          */
384         tmp = ds1307->regs[DS1307_REG_HOUR];
385         if (tmp & (1 << 6)) {
386                 if (tmp & (1 << 5))
387                         tmp = BCD2BIN(tmp & 0x1f) + 12;
388                 else
389                         tmp = BCD2BIN(tmp);
390                 i2c_smbus_write_byte_data(client,
391                                 DS1307_REG_HOUR,
392                                 BIN2BCD(tmp));
393         }
394
395         /* Tell the I2C layer a new client has arrived */
396         if ((err = i2c_attach_client(client)))
397                 goto exit_free;
398
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");
405                 goto exit_detach;
406         }
407
408         return 0;
409
410 exit_detach:
411         i2c_detach_client(client);
412 exit_free:
413         kfree(ds1307);
414 exit:
415         return err;
416 }
417
418 static int __devinit
419 ds1307_attach_adapter(struct i2c_adapter *adapter)
420 {
421         if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
422                 return 0;
423         return i2c_probe(adapter, &addr_data, ds1307_detect);
424 }
425
426 static int __devexit ds1307_detach_client(struct i2c_client *client)
427 {
428         int             err;
429         struct ds1307   *ds1307 = i2c_get_clientdata(client);
430
431         rtc_device_unregister(ds1307->rtc);
432         if ((err = i2c_detach_client(client)))
433                 return err;
434         kfree(ds1307);
435         return 0;
436 }
437
438 static struct i2c_driver ds1307_driver = {
439         .driver = {
440                 .name   = "ds1307",
441                 .owner  = THIS_MODULE,
442         },
443         .attach_adapter = ds1307_attach_adapter,
444         .detach_client  = __devexit_p(ds1307_detach_client),
445 };
446
447 static int __init ds1307_init(void)
448 {
449         return i2c_add_driver(&ds1307_driver);
450 }
451 module_init(ds1307_init);
452
453 static void __exit ds1307_exit(void)
454 {
455         i2c_del_driver(&ds1307_driver);
456 }
457 module_exit(ds1307_exit);
458
459 MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
460 MODULE_LICENSE("GPL");