HID: trivial fixes in hid-debug
[linux-2.6] / 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 enum ds_type {
32         ds_1307,
33         ds_1337,
34         ds_1338,
35         ds_1339,
36         ds_1340,
37         m41t00,
38         // rs5c372 too?  different address...
39 };
40
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
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 */
57
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.
61  */
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
87
88
89
90 struct ds1307 {
91         u8                      reg_addr;
92         u8                      regs[8];
93         enum ds_type            type;
94         struct i2c_msg          msg[2];
95         struct i2c_client       *client;
96         struct i2c_client       dev;
97         struct rtc_device       *rtc;
98 };
99
100 struct chip_desc {
101         char                    name[9];
102         unsigned                nvram56:1;
103         unsigned                alarm:1;
104         enum ds_type            type;
105 };
106
107 static 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
131 static 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 }
140
141 static int ds1307_get_time(struct device *dev, struct rtc_time *t)
142 {
143         struct ds1307   *ds1307 = dev_get_drvdata(dev);
144         int             tmp;
145
146         /* read the RTC date and time registers all at once */
147         ds1307->msg[1].flags = I2C_M_RD;
148         ds1307->msg[1].len = 7;
149
150         tmp = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent),
151                         ds1307->msg, 2);
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
182         /* initial clock setting can be undefined */
183         return rtc_valid_tm(t);
184 }
185
186 static 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",
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);
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
211         switch (ds1307->type) {
212         case ds_1337:
213         case ds_1339:
214                 buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
215                 break;
216         case ds_1340:
217                 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN
218                                 | DS1340_BIT_CENTURY;
219                 break;
220         default:
221                 break;
222         }
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 ds1307_probe(struct i2c_client *client)
248 {
249         struct ds1307           *ds1307;
250         int                     err = -ENODEV;
251         int                     tmp;
252         const struct chip_desc  *chip;
253         struct i2c_adapter      *adapter = to_i2c_adapter(client->dev.parent);
254
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;
260         }
261
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;
268
269         ds1307->client = client;
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
282         ds1307->type = chip->type;
283
284         switch (ds1307->type) {
285         case ds_1337:
286         case ds_1339:
287                 ds1307->reg_addr = DS1337_REG_CONTROL;
288                 ds1307->msg[1].len = 2;
289
290                 /* get registers that the "rtc" read below won't read... */
291                 tmp = i2c_transfer(adapter, ds1307->msg, 2);
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
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");
311                 }
312                 break;
313         default:
314                 break;
315         }
316
317 read_rtc:
318         /* read RTC registers */
319
320         tmp = i2c_transfer(adapter, ds1307->msg, 2);
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];
332         switch (ds1307->type) {
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                  */
338         case ds_1307:
339         case m41t00:
340                 /* clock halted?  turn it on, so clock can tick. */
341                 if (tmp & DS1307_BIT_CH) {
342                         i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
343                         dev_warn(&client->dev, "SET TIME!\n");
344                         goto read_rtc;
345                 }
346                 break;
347         case ds_1338:
348                 /* clock halted?  turn it on, so clock can tick. */
349                 if (tmp & DS1307_BIT_CH)
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[DS1307_REG_CONTROL]
356                                         & ~DS1338_BIT_OSF);
357                         dev_warn(&client->dev, "SET TIME!\n");
358                         goto read_rtc;
359                 }
360                 break;
361         case ds_1337:
362         case ds_1339:
363                 break;
364         }
365
366         tmp = ds1307->regs[DS1307_REG_SECS];
367         tmp = BCD2BIN(tmp & 0x7f);
368         if (tmp > 60)
369                 goto exit_bad;
370         tmp = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f);
371         if (tmp > 60)
372                 goto exit_bad;
373
374         tmp = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
375         if (tmp == 0 || tmp > 31)
376                 goto exit_bad;
377
378         tmp = BCD2BIN(ds1307->regs[DS1307_REG_MONTH] & 0x1f);
379         if (tmp == 0 || tmp > 12)
380                 goto exit_bad;
381
382         tmp = ds1307->regs[DS1307_REG_HOUR];
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;
402                 i2c_smbus_write_byte_data(client,
403                                 DS1307_REG_HOUR,
404                                 BIN2BCD(tmp));
405         }
406
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");
413                 goto exit_free;
414         }
415
416         return 0;
417
418 exit_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
426 exit_free:
427         kfree(ds1307);
428         return err;
429 }
430
431 static int __devexit ds1307_remove(struct i2c_client *client)
432 {
433         struct ds1307   *ds1307 = i2c_get_clientdata(client);
434
435         rtc_device_unregister(ds1307->rtc);
436         kfree(ds1307);
437         return 0;
438 }
439
440 static struct i2c_driver ds1307_driver = {
441         .driver = {
442                 .name   = "rtc-ds1307",
443                 .owner  = THIS_MODULE,
444         },
445         .probe          = ds1307_probe,
446         .remove         = __devexit_p(ds1307_remove),
447 };
448
449 static int __init ds1307_init(void)
450 {
451         return i2c_add_driver(&ds1307_driver);
452 }
453 module_init(ds1307_init);
454
455 static void __exit ds1307_exit(void)
456 {
457         i2c_del_driver(&ds1307_driver);
458 }
459 module_exit(ds1307_exit);
460
461 MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
462 MODULE_LICENSE("GPL");