[PATCH] I2C: ds1337 - fix 12/24 hour mode bug
[linux-2.6] / drivers / i2c / chips / ds1337.c
1 /*
2  *  linux/drivers/i2c/chips/ds1337.c
3  *
4  *  Copyright (C) 2005 James Chapman <jchapman@katalix.com>
5  *
6  *      based on linux/drivers/acorn/char/pcf8583.c
7  *  Copyright (C) 2000 Russell King
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  * Driver for Dallas Semiconductor DS1337 and DS1339 real time clock chip
14  */
15
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/i2c.h>
20 #include <linux/i2c-sensor.h>
21 #include <linux/string.h>
22 #include <linux/rtc.h>          /* get the user-level API */
23 #include <linux/bcd.h>
24 #include <linux/list.h>
25
26 /* Device registers */
27 #define DS1337_REG_HOUR         2
28 #define DS1337_REG_DAY          3
29 #define DS1337_REG_DATE         4
30 #define DS1337_REG_MONTH        5
31 #define DS1337_REG_CONTROL      14
32 #define DS1337_REG_STATUS       15
33
34 /* FIXME - how do we export these interface constants? */
35 #define DS1337_GET_DATE         0
36 #define DS1337_SET_DATE         1
37
38 /*
39  * Functions declaration
40  */
41 static unsigned short normal_i2c[] = { 0x68, I2C_CLIENT_END };
42 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
43
44 SENSORS_INSMOD_1(ds1337);
45
46 static int ds1337_attach_adapter(struct i2c_adapter *adapter);
47 static int ds1337_detect(struct i2c_adapter *adapter, int address, int kind);
48 static void ds1337_init_client(struct i2c_client *client);
49 static int ds1337_detach_client(struct i2c_client *client);
50 static int ds1337_command(struct i2c_client *client, unsigned int cmd,
51                           void *arg);
52
53 /*
54  * Driver data (common to all clients)
55  */
56 static struct i2c_driver ds1337_driver = {
57         .owner          = THIS_MODULE,
58         .name           = "ds1337",
59         .flags          = I2C_DF_NOTIFY,
60         .attach_adapter = ds1337_attach_adapter,
61         .detach_client  = ds1337_detach_client,
62         .command        = ds1337_command,
63 };
64
65 /*
66  * Client data (each client gets its own)
67  */
68 struct ds1337_data {
69         struct i2c_client client;
70         struct list_head list;
71 };
72
73 /*
74  * Internal variables
75  */
76 static LIST_HEAD(ds1337_clients);
77
78 static inline int ds1337_read(struct i2c_client *client, u8 reg, u8 *value)
79 {
80         s32 tmp = i2c_smbus_read_byte_data(client, reg);
81
82         if (tmp < 0)
83                 return -EIO;
84
85         *value = tmp;
86
87         return 0;
88 }
89
90 /*
91  * Chip access functions
92  */
93 static int ds1337_get_datetime(struct i2c_client *client, struct rtc_time *dt)
94 {
95         int result;
96         u8 buf[7];
97         u8 val;
98         struct i2c_msg msg[2];
99         u8 offs = 0;
100
101         if (!dt) {
102                 dev_dbg(&client->dev, "%s: EINVAL: dt=NULL\n", __FUNCTION__);
103                 return -EINVAL;
104         }
105
106         msg[0].addr = client->addr;
107         msg[0].flags = 0;
108         msg[0].len = 1;
109         msg[0].buf = &offs;
110
111         msg[1].addr = client->addr;
112         msg[1].flags = I2C_M_RD;
113         msg[1].len = sizeof(buf);
114         msg[1].buf = &buf[0];
115
116         result = i2c_transfer(client->adapter, msg, 2);
117
118         dev_dbg(&client->dev, "%s: [%d] %02x %02x %02x %02x %02x %02x %02x\n",
119                 __FUNCTION__, result, buf[0], buf[1], buf[2], buf[3],
120                 buf[4], buf[5], buf[6]);
121
122         if (result == 2) {
123                 dt->tm_sec = BCD2BIN(buf[0]);
124                 dt->tm_min = BCD2BIN(buf[1]);
125                 val = buf[2] & 0x3f;
126                 dt->tm_hour = BCD2BIN(val);
127                 dt->tm_wday = BCD2BIN(buf[3]) - 1;
128                 dt->tm_mday = BCD2BIN(buf[4]);
129                 val = buf[5] & 0x7f;
130                 dt->tm_mon = BCD2BIN(val) - 1;
131                 dt->tm_year = BCD2BIN(buf[6]);
132                 if (buf[5] & 0x80)
133                         dt->tm_year += 100;
134
135                 dev_dbg(&client->dev, "%s: secs=%d, mins=%d, "
136                         "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
137                         __FUNCTION__, dt->tm_sec, dt->tm_min,
138                         dt->tm_hour, dt->tm_mday,
139                         dt->tm_mon, dt->tm_year, dt->tm_wday);
140
141                 return 0;
142         }
143
144         dev_err(&client->dev, "error reading data! %d\n", result);
145         return -EIO;
146 }
147
148 static int ds1337_set_datetime(struct i2c_client *client, struct rtc_time *dt)
149 {
150         int result;
151         u8 buf[8];
152         u8 val;
153         struct i2c_msg msg[1];
154
155         if (!dt) {
156                 dev_dbg(&client->dev, "%s: EINVAL: dt=NULL\n", __FUNCTION__);
157                 return -EINVAL;
158         }
159
160         dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
161                 "mday=%d, mon=%d, year=%d, wday=%d\n", __FUNCTION__,
162                 dt->tm_sec, dt->tm_min, dt->tm_hour,
163                 dt->tm_mday, dt->tm_mon, dt->tm_year, dt->tm_wday);
164
165         buf[0] = 0;             /* reg offset */
166         buf[1] = BIN2BCD(dt->tm_sec);
167         buf[2] = BIN2BCD(dt->tm_min);
168         buf[3] = BIN2BCD(dt->tm_hour);
169         buf[4] = BIN2BCD(dt->tm_wday) + 1;
170         buf[5] = BIN2BCD(dt->tm_mday);
171         buf[6] = BIN2BCD(dt->tm_mon) + 1;
172         val = dt->tm_year;
173         if (val >= 100) {
174                 val -= 100;
175                 buf[6] |= (1 << 7);
176         }
177         buf[7] = BIN2BCD(val);
178
179         msg[0].addr = client->addr;
180         msg[0].flags = 0;
181         msg[0].len = sizeof(buf);
182         msg[0].buf = &buf[0];
183
184         result = i2c_transfer(client->adapter, msg, 1);
185         if (result == 1)
186                 return 0;
187
188         dev_err(&client->dev, "error writing data! %d\n", result);
189         return -EIO;
190 }
191
192 static int ds1337_command(struct i2c_client *client, unsigned int cmd,
193                           void *arg)
194 {
195         dev_dbg(&client->dev, "%s: cmd=%d\n", __FUNCTION__, cmd);
196
197         switch (cmd) {
198         case DS1337_GET_DATE:
199                 return ds1337_get_datetime(client, arg);
200
201         case DS1337_SET_DATE:
202                 return ds1337_set_datetime(client, arg);
203
204         default:
205                 return -EINVAL;
206         }
207 }
208
209 /*
210  * Public API for access to specific device. Useful for low-level
211  * RTC access from kernel code.
212  */
213 int ds1337_do_command(int bus, int cmd, void *arg)
214 {
215         struct list_head *walk;
216         struct list_head *tmp;
217         struct ds1337_data *data;
218
219         list_for_each_safe(walk, tmp, &ds1337_clients) {
220                 data = list_entry(walk, struct ds1337_data, list);
221                 if (data->client.adapter->nr == bus)
222                         return ds1337_command(&data->client, cmd, arg);
223         }
224
225         return -ENODEV;
226 }
227
228 static int ds1337_attach_adapter(struct i2c_adapter *adapter)
229 {
230         return i2c_detect(adapter, &addr_data, ds1337_detect);
231 }
232
233 /*
234  * The following function does more than just detection. If detection
235  * succeeds, it also registers the new chip.
236  */
237 static int ds1337_detect(struct i2c_adapter *adapter, int address, int kind)
238 {
239         struct i2c_client *new_client;
240         struct ds1337_data *data;
241         int err = 0;
242         const char *name = "";
243
244         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
245                                      I2C_FUNC_I2C))
246                 goto exit;
247
248         if (!(data = kmalloc(sizeof(struct ds1337_data), GFP_KERNEL))) {
249                 err = -ENOMEM;
250                 goto exit;
251         }
252         memset(data, 0, sizeof(struct ds1337_data));
253         INIT_LIST_HEAD(&data->list);
254
255         /* The common I2C client data is placed right before the
256          * DS1337-specific data. 
257          */
258         new_client = &data->client;
259         i2c_set_clientdata(new_client, data);
260         new_client->addr = address;
261         new_client->adapter = adapter;
262         new_client->driver = &ds1337_driver;
263         new_client->flags = 0;
264
265         /*
266          * Now we do the remaining detection. A negative kind means that
267          * the driver was loaded with no force parameter (default), so we
268          * must both detect and identify the chip. A zero kind means that
269          * the driver was loaded with the force parameter, the detection
270          * step shall be skipped. A positive kind means that the driver
271          * was loaded with the force parameter and a given kind of chip is
272          * requested, so both the detection and the identification steps
273          * are skipped.
274          *
275          * For detection, we read registers that are most likely to cause
276          * detection failure, i.e. those that have more bits with fixed
277          * or reserved values.
278          */
279
280         /* Default to an DS1337 if forced */
281         if (kind == 0)
282                 kind = ds1337;
283
284         if (kind < 0) {         /* detection and identification */
285                 u8 data;
286
287                 /* Check that status register bits 6-2 are zero */
288                 if ((ds1337_read(new_client, DS1337_REG_STATUS, &data) < 0) ||
289                     (data & 0x7c))
290                         goto exit_free;
291
292                 /* Check for a valid day register value */
293                 if ((ds1337_read(new_client, DS1337_REG_DAY, &data) < 0) ||
294                     (data == 0) || (data & 0xf8))
295                         goto exit_free;
296
297                 /* Check for a valid date register value */
298                 if ((ds1337_read(new_client, DS1337_REG_DATE, &data) < 0) ||
299                     (data == 0) || (data & 0xc0) || ((data & 0x0f) > 9) ||
300                     (data >= 0x32))
301                         goto exit_free;
302
303                 /* Check for a valid month register value */
304                 if ((ds1337_read(new_client, DS1337_REG_MONTH, &data) < 0) ||
305                     (data == 0) || (data & 0x60) || ((data & 0x0f) > 9) ||
306                     ((data >= 0x13) && (data <= 0x19)))
307                         goto exit_free;
308
309                 /* Check that control register bits 6-5 are zero */
310                 if ((ds1337_read(new_client, DS1337_REG_CONTROL, &data) < 0) ||
311                     (data & 0x60))
312                         goto exit_free;
313
314                 kind = ds1337;
315         }
316
317         if (kind == ds1337)
318                 name = "ds1337";
319
320         /* We can fill in the remaining client fields */
321         strlcpy(new_client->name, name, I2C_NAME_SIZE);
322
323         /* Tell the I2C layer a new client has arrived */
324         if ((err = i2c_attach_client(new_client)))
325                 goto exit_free;
326
327         /* Initialize the DS1337 chip */
328         ds1337_init_client(new_client);
329
330         /* Add client to local list */
331         list_add(&data->list, &ds1337_clients);
332
333         return 0;
334
335 exit_free:
336         kfree(data);
337 exit:
338         return err;
339 }
340
341 static void ds1337_init_client(struct i2c_client *client)
342 {
343         s32 val;
344
345         /* Ensure that device is set in 24-hour mode */
346         val = i2c_smbus_read_byte_data(client, DS1337_REG_HOUR);
347         if ((val >= 0) && (val & (1 << 6)))
348                 i2c_smbus_write_byte_data(client, DS1337_REG_HOUR,
349                                           val & 0x3f);
350 }
351
352 static int ds1337_detach_client(struct i2c_client *client)
353 {
354         int err;
355         struct ds1337_data *data = i2c_get_clientdata(client);
356
357         if ((err = i2c_detach_client(client))) {
358                 dev_err(&client->dev, "Client deregistration failed, "
359                         "client not detached.\n");
360                 return err;
361         }
362
363         list_del(&data->list);
364         kfree(data);
365         return 0;
366 }
367
368 static int __init ds1337_init(void)
369 {
370         return i2c_add_driver(&ds1337_driver);
371 }
372
373 static void __exit ds1337_exit(void)
374 {
375         i2c_del_driver(&ds1337_driver);
376 }
377
378 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
379 MODULE_DESCRIPTION("DS1337 RTC driver");
380 MODULE_LICENSE("GPL");
381
382 EXPORT_SYMBOL_GPL(ds1337_do_command);
383
384 module_init(ds1337_init);
385 module_exit(ds1337_exit);