[PATCH] constify rtc_class_ops: update drivers
[linux-2.6/mini2440.git] / drivers / rtc / rtc-ds1307.c
blobcc5032b6f42ac45c9aba4f0ff896b17585a6c813
1 /*
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 * If the I2C "force" mechanism is used, we assume the chip is a ds1337.
28 * (Much better would be board-specific tables of I2C devices, along with
29 * the platform_data drivers would use to sort such issues out.)
31 enum ds_type {
32 unknown = 0,
33 ds_1307, /* or ds1338, ... */
34 ds_1337, /* or ds1339, ... */
35 ds_1340, /* or st m41t00, ... */
36 // rs5c372 too? different address...
39 static unsigned short normal_i2c[] = { 0x68, I2C_CLIENT_END };
41 I2C_CLIENT_INSMOD;
45 /* RTC registers don't differ much, except for the century flag */
46 #define DS1307_REG_SECS 0x00 /* 00-59 */
47 # define DS1307_BIT_CH 0x80
48 #define DS1307_REG_MIN 0x01 /* 00-59 */
49 #define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
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 */
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 RTC;
60 * be careful using them.
62 #define DS1307_REG_CONTROL 0x07
63 # define DS1307_BIT_OUT 0x80
64 # define DS1307_BIT_SQWE 0x10
65 # define DS1307_BIT_RS1 0x02
66 # define DS1307_BIT_RS0 0x01
67 #define DS1337_REG_CONTROL 0x0e
68 # define DS1337_BIT_nEOSC 0x80
69 # define DS1337_BIT_RS2 0x10
70 # define DS1337_BIT_RS1 0x08
71 # define DS1337_BIT_INTCN 0x04
72 # define DS1337_BIT_A2IE 0x02
73 # define DS1337_BIT_A1IE 0x01
74 #define DS1337_REG_STATUS 0x0f
75 # define DS1337_BIT_OSF 0x80
76 # define DS1337_BIT_A2I 0x02
77 # define DS1337_BIT_A1I 0x01
78 #define DS1339_REG_TRICKLE 0x10
82 struct ds1307 {
83 u8 reg_addr;
84 u8 regs[8];
85 enum ds_type type;
86 struct i2c_msg msg[2];
87 struct i2c_client client;
88 struct rtc_device *rtc;
92 static int ds1307_get_time(struct device *dev, struct rtc_time *t)
94 struct ds1307 *ds1307 = dev_get_drvdata(dev);
95 int tmp;
97 /* read the RTC registers all at once */
98 ds1307->msg[1].flags = I2C_M_RD;
99 ds1307->msg[1].len = 7;
101 tmp = i2c_transfer(ds1307->client.adapter, ds1307->msg, 2);
102 if (tmp != 2) {
103 dev_err(dev, "%s error %d\n", "read", tmp);
104 return -EIO;
107 dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
108 "read",
109 ds1307->regs[0], ds1307->regs[1],
110 ds1307->regs[2], ds1307->regs[3],
111 ds1307->regs[4], ds1307->regs[5],
112 ds1307->regs[6]);
114 t->tm_sec = BCD2BIN(ds1307->regs[DS1307_REG_SECS] & 0x7f);
115 t->tm_min = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f);
116 tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
117 t->tm_hour = BCD2BIN(tmp);
118 t->tm_wday = BCD2BIN(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
119 t->tm_mday = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
120 tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
121 t->tm_mon = BCD2BIN(tmp) - 1;
123 /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
124 t->tm_year = BCD2BIN(ds1307->regs[DS1307_REG_YEAR]) + 100;
126 dev_dbg(dev, "%s secs=%d, mins=%d, "
127 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
128 "read", t->tm_sec, t->tm_min,
129 t->tm_hour, t->tm_mday,
130 t->tm_mon, t->tm_year, t->tm_wday);
132 return 0;
135 static int ds1307_set_time(struct device *dev, struct rtc_time *t)
137 struct ds1307 *ds1307 = dev_get_drvdata(dev);
138 int result;
139 int tmp;
140 u8 *buf = ds1307->regs;
142 dev_dbg(dev, "%s secs=%d, mins=%d, "
143 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
144 "write", dt->tm_sec, dt->tm_min,
145 dt->tm_hour, dt->tm_mday,
146 dt->tm_mon, dt->tm_year, dt->tm_wday);
148 *buf++ = 0; /* first register addr */
149 buf[DS1307_REG_SECS] = BIN2BCD(t->tm_sec);
150 buf[DS1307_REG_MIN] = BIN2BCD(t->tm_min);
151 buf[DS1307_REG_HOUR] = BIN2BCD(t->tm_hour);
152 buf[DS1307_REG_WDAY] = BIN2BCD(t->tm_wday + 1);
153 buf[DS1307_REG_MDAY] = BIN2BCD(t->tm_mday);
154 buf[DS1307_REG_MONTH] = BIN2BCD(t->tm_mon + 1);
156 /* assume 20YY not 19YY */
157 tmp = t->tm_year - 100;
158 buf[DS1307_REG_YEAR] = BIN2BCD(tmp);
160 if (ds1307->type == ds_1337)
161 buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
162 else if (ds1307->type == ds_1340)
163 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN
164 | DS1340_BIT_CENTURY;
166 ds1307->msg[1].flags = 0;
167 ds1307->msg[1].len = 8;
169 dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
170 "write", buf[0], buf[1], buf[2], buf[3],
171 buf[4], buf[5], buf[6]);
173 result = i2c_transfer(ds1307->client.adapter, &ds1307->msg[1], 1);
174 if (result != 1) {
175 dev_err(dev, "%s error %d\n", "write", tmp);
176 return -EIO;
178 return 0;
181 static const struct rtc_class_ops ds13xx_rtc_ops = {
182 .read_time = ds1307_get_time,
183 .set_time = ds1307_set_time,
186 static struct i2c_driver ds1307_driver;
188 static int __devinit
189 ds1307_detect(struct i2c_adapter *adapter, int address, int kind)
191 struct ds1307 *ds1307;
192 int err = -ENODEV;
193 struct i2c_client *client;
194 int tmp;
196 if (!(ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL))) {
197 err = -ENOMEM;
198 goto exit;
201 client = &ds1307->client;
202 client->addr = address;
203 client->adapter = adapter;
204 client->driver = &ds1307_driver;
205 client->flags = 0;
207 i2c_set_clientdata(client, ds1307);
209 ds1307->msg[0].addr = client->addr;
210 ds1307->msg[0].flags = 0;
211 ds1307->msg[0].len = 1;
212 ds1307->msg[0].buf = &ds1307->reg_addr;
214 ds1307->msg[1].addr = client->addr;
215 ds1307->msg[1].flags = I2C_M_RD;
216 ds1307->msg[1].len = sizeof(ds1307->regs);
217 ds1307->msg[1].buf = ds1307->regs;
219 /* HACK: "force" implies "needs ds1337-style-oscillator setup" */
220 if (kind >= 0) {
221 ds1307->type = ds_1337;
223 ds1307->reg_addr = DS1337_REG_CONTROL;
224 ds1307->msg[1].len = 2;
226 tmp = i2c_transfer(client->adapter, ds1307->msg, 2);
227 if (tmp != 2) {
228 pr_debug("read error %d\n", tmp);
229 err = -EIO;
230 goto exit_free;
233 ds1307->reg_addr = 0;
234 ds1307->msg[1].len = sizeof(ds1307->regs);
236 /* oscillator is off; need to turn it on */
237 if ((ds1307->regs[0] & DS1337_BIT_nEOSC)
238 || (ds1307->regs[1] & DS1337_BIT_OSF)) {
239 printk(KERN_ERR "no ds1337 oscillator code\n");
240 goto exit_free;
242 } else
243 ds1307->type = ds_1307;
245 read_rtc:
246 /* read RTC registers */
248 tmp = i2c_transfer(client->adapter, ds1307->msg, 2);
249 if (tmp != 2) {
250 pr_debug("read error %d\n", tmp);
251 err = -EIO;
252 goto exit_free;
255 /* minimal sanity checking; some chips (like DS1340) don't
256 * specify the extra bits as must-be-zero, but there are
257 * still a few values that are clearly out-of-range.
259 tmp = ds1307->regs[DS1307_REG_SECS];
260 if (tmp & DS1307_BIT_CH) {
261 if (ds1307->type && ds1307->type != ds_1307) {
262 pr_debug("not a ds1307?\n");
263 goto exit_free;
265 ds1307->type = ds_1307;
267 /* this partial initialization should work for ds1307,
268 * ds1338, ds1340, st m41t00, and more.
270 dev_warn(&client->dev, "oscillator started; SET TIME!\n");
271 i2c_smbus_write_byte_data(client, 0, 0);
272 goto read_rtc;
274 tmp = BCD2BIN(tmp & 0x7f);
275 if (tmp > 60)
276 goto exit_free;
277 tmp = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f);
278 if (tmp > 60)
279 goto exit_free;
281 tmp = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
282 if (tmp == 0 || tmp > 31)
283 goto exit_free;
285 tmp = BCD2BIN(ds1307->regs[DS1307_REG_MONTH] & 0x1f);
286 if (tmp == 0 || tmp > 12)
287 goto exit_free;
289 /* force into in 24 hour mode (most chips) or
290 * disable century bit (ds1340)
292 tmp = ds1307->regs[DS1307_REG_HOUR];
293 if (tmp & (1 << 6)) {
294 if (tmp & (1 << 5))
295 tmp = BCD2BIN(tmp & 0x1f) + 12;
296 else
297 tmp = BCD2BIN(tmp);
298 i2c_smbus_write_byte_data(client,
299 DS1307_REG_HOUR,
300 BIN2BCD(tmp));
303 /* FIXME chips like 1337 can generate alarm irqs too; those are
304 * worth exposing through the API (especially when the irq is
305 * wakeup-capable).
308 switch (ds1307->type) {
309 case unknown:
310 strlcpy(client->name, "unknown", I2C_NAME_SIZE);
311 break;
312 case ds_1307:
313 strlcpy(client->name, "ds1307", I2C_NAME_SIZE);
314 break;
315 case ds_1337:
316 strlcpy(client->name, "ds1337", I2C_NAME_SIZE);
317 break;
318 case ds_1340:
319 strlcpy(client->name, "ds1340", I2C_NAME_SIZE);
320 break;
323 /* Tell the I2C layer a new client has arrived */
324 if ((err = i2c_attach_client(client)))
325 goto exit_free;
327 ds1307->rtc = rtc_device_register(client->name, &client->dev,
328 &ds13xx_rtc_ops, THIS_MODULE);
329 if (IS_ERR(ds1307->rtc)) {
330 err = PTR_ERR(ds1307->rtc);
331 dev_err(&client->dev,
332 "unable to register the class device\n");
333 goto exit_detach;
336 return 0;
338 exit_detach:
339 i2c_detach_client(client);
340 exit_free:
341 kfree(ds1307);
342 exit:
343 return err;
346 static int __devinit
347 ds1307_attach_adapter(struct i2c_adapter *adapter)
349 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
350 return 0;
351 return i2c_probe(adapter, &addr_data, ds1307_detect);
354 static int __devexit ds1307_detach_client(struct i2c_client *client)
356 int err;
357 struct ds1307 *ds1307 = i2c_get_clientdata(client);
359 rtc_device_unregister(ds1307->rtc);
360 if ((err = i2c_detach_client(client)))
361 return err;
362 kfree(ds1307);
363 return 0;
366 static struct i2c_driver ds1307_driver = {
367 .driver = {
368 .name = "ds1307",
369 .owner = THIS_MODULE,
371 .attach_adapter = ds1307_attach_adapter,
372 .detach_client = __devexit_p(ds1307_detach_client),
375 static int __init ds1307_init(void)
377 return i2c_add_driver(&ds1307_driver);
379 module_init(ds1307_init);
381 static void __exit ds1307_exit(void)
383 i2c_del_driver(&ds1307_driver);
385 module_exit(ds1307_exit);
387 MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
388 MODULE_LICENSE("GPL");