[PATCH] I2C: ds1337: Make time format consistent with other RTC drivers
[powerpc.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         int result;
99         u8 buf[7];
100         u8 val;
101         struct i2c_msg msg[2];
102         u8 offs = 0;
103
104         if (!dt) {
105                 dev_dbg(&client->dev, "%s: EINVAL: dt=NULL\n", __FUNCTION__);
106                 return -EINVAL;
107         }
108
109         msg[0].addr = client->addr;
110         msg[0].flags = 0;
111         msg[0].len = 1;
112         msg[0].buf = &offs;
113
114         msg[1].addr = client->addr;
115         msg[1].flags = I2C_M_RD;
116         msg[1].len = sizeof(buf);
117         msg[1].buf = &buf[0];
118
119         result = i2c_transfer(client->adapter, msg, 2);
120
121         dev_dbg(&client->dev, "%s: [%d] %02x %02x %02x %02x %02x %02x %02x\n",
122                 __FUNCTION__, result, buf[0], buf[1], buf[2], buf[3],
123                 buf[4], buf[5], buf[6]);
124
125         if (result >= 0) {
126                 dt->tm_sec = BCD2BIN(buf[0]);
127                 dt->tm_min = BCD2BIN(buf[1]);
128                 val = buf[2] & 0x3f;
129                 dt->tm_hour = BCD2BIN(val);
130                 dt->tm_wday = BCD2BIN(buf[3]) - 1;
131                 dt->tm_mday = BCD2BIN(buf[4]);
132                 val = buf[5] & 0x7f;
133                 dt->tm_mon = BCD2BIN(val) - 1;
134                 dt->tm_year = BCD2BIN(buf[6]);
135                 if (buf[5] & 0x80)
136                         dt->tm_year += 100;
137
138                 dev_dbg(&client->dev, "%s: secs=%d, mins=%d, "
139                         "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
140                         __FUNCTION__, dt->tm_sec, dt->tm_min,
141                         dt->tm_hour, dt->tm_mday,
142                         dt->tm_mon, dt->tm_year, dt->tm_wday);
143         } else {
144                 dev_err(&client->dev, "error reading data! %d\n", result);
145                 result = -EIO;
146         }
147
148         return result;
149 }
150
151 static int ds1337_set_datetime(struct i2c_client *client, struct rtc_time *dt)
152 {
153         int result;
154         u8 buf[8];
155         u8 val;
156         struct i2c_msg msg[1];
157
158         if (!dt) {
159                 dev_dbg(&client->dev, "%s: EINVAL: dt=NULL\n", __FUNCTION__);
160                 return -EINVAL;
161         }
162
163         dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
164                 "mday=%d, mon=%d, year=%d, wday=%d\n", __FUNCTION__,
165                 dt->tm_sec, dt->tm_min, dt->tm_hour,
166                 dt->tm_mday, dt->tm_mon, dt->tm_year, dt->tm_wday);
167
168         buf[0] = 0;             /* reg offset */
169         buf[1] = BIN2BCD(dt->tm_sec);
170         buf[2] = BIN2BCD(dt->tm_min);
171         buf[3] = BIN2BCD(dt->tm_hour) | (1 << 6);
172         buf[4] = BIN2BCD(dt->tm_wday) + 1;
173         buf[5] = BIN2BCD(dt->tm_mday);
174         buf[6] = BIN2BCD(dt->tm_mon) + 1;
175         val = dt->tm_year;
176         if (val >= 100) {
177                 val -= 100;
178                 buf[6] |= (1 << 7);
179         }
180         buf[7] = BIN2BCD(val);
181
182         msg[0].addr = client->addr;
183         msg[0].flags = 0;
184         msg[0].len = sizeof(buf);
185         msg[0].buf = &buf[0];
186
187         result = i2c_transfer(client->adapter, msg, 1);
188         if (result < 0) {
189                 dev_err(&client->dev, "error writing data! %d\n", result);
190                 result = -EIO;
191         } else {
192                 result = 0;
193         }
194
195         return result;
196 }
197
198 static int ds1337_command(struct i2c_client *client, unsigned int cmd,
199                           void *arg)
200 {
201         dev_dbg(&client->dev, "%s: cmd=%d\n", __FUNCTION__, cmd);
202
203         switch (cmd) {
204         case DS1337_GET_DATE:
205                 return ds1337_get_datetime(client, arg);
206
207         case DS1337_SET_DATE:
208                 return ds1337_set_datetime(client, arg);
209
210         default:
211                 return -EINVAL;
212         }
213 }
214
215 /*
216  * Public API for access to specific device. Useful for low-level
217  * RTC access from kernel code.
218  */
219 int ds1337_do_command(int id, int cmd, void *arg)
220 {
221         struct list_head *walk;
222         struct list_head *tmp;
223         struct ds1337_data *data;
224
225         list_for_each_safe(walk, tmp, &ds1337_clients) {
226                 data = list_entry(walk, struct ds1337_data, list);
227                 if (data->id == id)
228                         return ds1337_command(&data->client, cmd, arg);
229         }
230
231         return -ENODEV;
232 }
233
234 static int ds1337_attach_adapter(struct i2c_adapter *adapter)
235 {
236         return i2c_detect(adapter, &addr_data, ds1337_detect);
237 }
238
239 /*
240  * The following function does more than just detection. If detection
241  * succeeds, it also registers the new chip.
242  */
243 static int ds1337_detect(struct i2c_adapter *adapter, int address, int kind)
244 {
245         struct i2c_client *new_client;
246         struct ds1337_data *data;
247         int err = 0;
248         const char *name = "";
249
250         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
251                                      I2C_FUNC_I2C))
252                 goto exit;
253
254         if (!(data = kmalloc(sizeof(struct ds1337_data), GFP_KERNEL))) {
255                 err = -ENOMEM;
256                 goto exit;
257         }
258         memset(data, 0, sizeof(struct ds1337_data));
259         INIT_LIST_HEAD(&data->list);
260
261         /* The common I2C client data is placed right before the
262          * DS1337-specific data. 
263          */
264         new_client = &data->client;
265         i2c_set_clientdata(new_client, data);
266         new_client->addr = address;
267         new_client->adapter = adapter;
268         new_client->driver = &ds1337_driver;
269         new_client->flags = 0;
270
271         /*
272          * Now we do the remaining detection. A negative kind means that
273          * the driver was loaded with no force parameter (default), so we
274          * must both detect and identify the chip. A zero kind means that
275          * the driver was loaded with the force parameter, the detection
276          * step shall be skipped. A positive kind means that the driver
277          * was loaded with the force parameter and a given kind of chip is
278          * requested, so both the detection and the identification steps
279          * are skipped.
280          *
281          * For detection, we read registers that are most likely to cause
282          * detection failure, i.e. those that have more bits with fixed
283          * or reserved values.
284          */
285
286         /* Default to an DS1337 if forced */
287         if (kind == 0)
288                 kind = ds1337;
289
290         if (kind < 0) {         /* detection and identification */
291                 u8 data;
292
293                 /* Check that status register bits 6-2 are zero */
294                 if ((ds1337_read(new_client, DS1337_REG_STATUS, &data) < 0) ||
295                     (data & 0x7c))
296                         goto exit_free;
297
298                 /* Check for a valid day register value */
299                 if ((ds1337_read(new_client, DS1337_REG_DAY, &data) < 0) ||
300                     (data == 0) || (data & 0xf8))
301                         goto exit_free;
302
303                 /* Check for a valid date register value */
304                 if ((ds1337_read(new_client, DS1337_REG_DATE, &data) < 0) ||
305                     (data == 0) || (data & 0xc0) || ((data & 0x0f) > 9) ||
306                     (data >= 0x32))
307                         goto exit_free;
308
309                 /* Check for a valid month register value */
310                 if ((ds1337_read(new_client, DS1337_REG_MONTH, &data) < 0) ||
311                     (data == 0) || (data & 0x60) || ((data & 0x0f) > 9) ||
312                     ((data >= 0x13) && (data <= 0x19)))
313                         goto exit_free;
314
315                 /* Check that control register bits 6-5 are zero */
316                 if ((ds1337_read(new_client, DS1337_REG_CONTROL, &data) < 0) ||
317                     (data & 0x60))
318                         goto exit_free;
319
320                 kind = ds1337;
321         }
322
323         if (kind == ds1337)
324                 name = "ds1337";
325
326         /* We can fill in the remaining client fields */
327         strlcpy(new_client->name, name, I2C_NAME_SIZE);
328
329         /* Tell the I2C layer a new client has arrived */
330         if ((err = i2c_attach_client(new_client)))
331                 goto exit_free;
332
333         /* Initialize the DS1337 chip */
334         ds1337_init_client(new_client);
335
336         /* Add client to local list */
337         data->id = ds1337_id++;
338         list_add(&data->list, &ds1337_clients);
339
340         return 0;
341
342 exit_free:
343         kfree(data);
344 exit:
345         return err;
346 }
347
348 static void ds1337_init_client(struct i2c_client *client)
349 {
350         s32 val;
351
352         /* Ensure that device is set in 24-hour mode */
353         val = i2c_smbus_read_byte_data(client, DS1337_REG_HOUR);
354         if ((val >= 0) && (val & (1 << 6)) == 0)
355                 i2c_smbus_write_byte_data(client, DS1337_REG_HOUR,
356                                           val | (1 << 6));
357 }
358
359 static int ds1337_detach_client(struct i2c_client *client)
360 {
361         int err;
362         struct ds1337_data *data = i2c_get_clientdata(client);
363
364         if ((err = i2c_detach_client(client))) {
365                 dev_err(&client->dev, "Client deregistration failed, "
366                         "client not detached.\n");
367                 return err;
368         }
369
370         list_del(&data->list);
371         kfree(data);
372         return 0;
373 }
374
375 static int __init ds1337_init(void)
376 {
377         return i2c_add_driver(&ds1337_driver);
378 }
379
380 static void __exit ds1337_exit(void)
381 {
382         i2c_del_driver(&ds1337_driver);
383 }
384
385 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
386 MODULE_DESCRIPTION("DS1337 RTC driver");
387 MODULE_LICENSE("GPL");
388
389 module_init(ds1337_init);
390 module_exit(ds1337_exit);