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