[PATCH] I2C: ds1337: search by bus number
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / i2c / chips / ds1337.c
blob8ee818ce0135a60b2fba6fb0c0066b0c55d2542a
1 /*
2 * linux/drivers/i2c/chips/ds1337.c
4 * Copyright (C) 2005 James Chapman <jchapman@katalix.com>
6 * based on linux/drivers/acorn/char/pcf8583.c
7 * Copyright (C) 2000 Russell King
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.
13 * Driver for Dallas Semiconductor DS1337 real time clock chip
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>
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
35 /* FIXME - how do we export these interface constants? */
36 #define DS1337_GET_DATE 0
37 #define DS1337_SET_DATE 1
40 * Functions declaration
42 static unsigned short normal_i2c[] = { 0x68, I2C_CLIENT_END };
43 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
45 SENSORS_INSMOD_1(ds1337);
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);
55 * Driver data (common to all clients)
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,
67 * Client data (each client gets its own)
69 struct ds1337_data {
70 struct i2c_client client;
71 struct list_head list;
75 * Internal variables
77 static LIST_HEAD(ds1337_clients);
79 static inline int ds1337_read(struct i2c_client *client, u8 reg, u8 *value)
81 s32 tmp = i2c_smbus_read_byte_data(client, reg);
83 if (tmp < 0)
84 return -EIO;
86 *value = tmp;
88 return 0;
92 * Chip access functions
94 static int ds1337_get_datetime(struct i2c_client *client, struct rtc_time *dt)
96 int result;
97 u8 buf[7];
98 u8 val;
99 struct i2c_msg msg[2];
100 u8 offs = 0;
102 if (!dt) {
103 dev_dbg(&client->dev, "%s: EINVAL: dt=NULL\n", __FUNCTION__);
104 return -EINVAL;
107 msg[0].addr = client->addr;
108 msg[0].flags = 0;
109 msg[0].len = 1;
110 msg[0].buf = &offs;
112 msg[1].addr = client->addr;
113 msg[1].flags = I2C_M_RD;
114 msg[1].len = sizeof(buf);
115 msg[1].buf = &buf[0];
117 result = i2c_transfer(client->adapter, msg, 2);
119 dev_dbg(&client->dev, "%s: [%d] %02x %02x %02x %02x %02x %02x %02x\n",
120 __FUNCTION__, result, buf[0], buf[1], buf[2], buf[3],
121 buf[4], buf[5], buf[6]);
123 if (result == 2) {
124 dt->tm_sec = BCD2BIN(buf[0]);
125 dt->tm_min = BCD2BIN(buf[1]);
126 val = buf[2] & 0x3f;
127 dt->tm_hour = BCD2BIN(val);
128 dt->tm_wday = BCD2BIN(buf[3]) - 1;
129 dt->tm_mday = BCD2BIN(buf[4]);
130 val = buf[5] & 0x7f;
131 dt->tm_mon = BCD2BIN(val) - 1;
132 dt->tm_year = BCD2BIN(buf[6]);
133 if (buf[5] & 0x80)
134 dt->tm_year += 100;
136 dev_dbg(&client->dev, "%s: secs=%d, mins=%d, "
137 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
138 __FUNCTION__, dt->tm_sec, dt->tm_min,
139 dt->tm_hour, dt->tm_mday,
140 dt->tm_mon, dt->tm_year, dt->tm_wday);
142 return 0;
145 dev_err(&client->dev, "error reading data! %d\n", result);
146 return -EIO;
149 static int ds1337_set_datetime(struct i2c_client *client, struct rtc_time *dt)
151 int result;
152 u8 buf[8];
153 u8 val;
154 struct i2c_msg msg[1];
156 if (!dt) {
157 dev_dbg(&client->dev, "%s: EINVAL: dt=NULL\n", __FUNCTION__);
158 return -EINVAL;
161 dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
162 "mday=%d, mon=%d, year=%d, wday=%d\n", __FUNCTION__,
163 dt->tm_sec, dt->tm_min, dt->tm_hour,
164 dt->tm_mday, dt->tm_mon, dt->tm_year, dt->tm_wday);
166 buf[0] = 0; /* reg offset */
167 buf[1] = BIN2BCD(dt->tm_sec);
168 buf[2] = BIN2BCD(dt->tm_min);
169 buf[3] = BIN2BCD(dt->tm_hour) | (1 << 6);
170 buf[4] = BIN2BCD(dt->tm_wday) + 1;
171 buf[5] = BIN2BCD(dt->tm_mday);
172 buf[6] = BIN2BCD(dt->tm_mon) + 1;
173 val = dt->tm_year;
174 if (val >= 100) {
175 val -= 100;
176 buf[6] |= (1 << 7);
178 buf[7] = BIN2BCD(val);
180 msg[0].addr = client->addr;
181 msg[0].flags = 0;
182 msg[0].len = sizeof(buf);
183 msg[0].buf = &buf[0];
185 result = i2c_transfer(client->adapter, msg, 1);
186 if (result == 1)
187 return 0;
189 dev_err(&client->dev, "error writing data! %d\n", result);
190 return -EIO;
193 static int ds1337_command(struct i2c_client *client, unsigned int cmd,
194 void *arg)
196 dev_dbg(&client->dev, "%s: cmd=%d\n", __FUNCTION__, cmd);
198 switch (cmd) {
199 case DS1337_GET_DATE:
200 return ds1337_get_datetime(client, arg);
202 case DS1337_SET_DATE:
203 return ds1337_set_datetime(client, arg);
205 default:
206 return -EINVAL;
211 * Public API for access to specific device. Useful for low-level
212 * RTC access from kernel code.
214 int ds1337_do_command(int bus, int cmd, void *arg)
216 struct list_head *walk;
217 struct list_head *tmp;
218 struct ds1337_data *data;
220 list_for_each_safe(walk, tmp, &ds1337_clients) {
221 data = list_entry(walk, struct ds1337_data, list);
222 if (data->client.adapter->nr == bus)
223 return ds1337_command(&data->client, cmd, arg);
226 return -ENODEV;
229 static int ds1337_attach_adapter(struct i2c_adapter *adapter)
231 return i2c_detect(adapter, &addr_data, ds1337_detect);
235 * The following function does more than just detection. If detection
236 * succeeds, it also registers the new chip.
238 static int ds1337_detect(struct i2c_adapter *adapter, int address, int kind)
240 struct i2c_client *new_client;
241 struct ds1337_data *data;
242 int err = 0;
243 const char *name = "";
245 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
246 I2C_FUNC_I2C))
247 goto exit;
249 if (!(data = kmalloc(sizeof(struct ds1337_data), GFP_KERNEL))) {
250 err = -ENOMEM;
251 goto exit;
253 memset(data, 0, sizeof(struct ds1337_data));
254 INIT_LIST_HEAD(&data->list);
256 /* The common I2C client data is placed right before the
257 * DS1337-specific data.
259 new_client = &data->client;
260 i2c_set_clientdata(new_client, data);
261 new_client->addr = address;
262 new_client->adapter = adapter;
263 new_client->driver = &ds1337_driver;
264 new_client->flags = 0;
267 * Now we do the remaining detection. A negative kind means that
268 * the driver was loaded with no force parameter (default), so we
269 * must both detect and identify the chip. A zero kind means that
270 * the driver was loaded with the force parameter, the detection
271 * step shall be skipped. A positive kind means that the driver
272 * was loaded with the force parameter and a given kind of chip is
273 * requested, so both the detection and the identification steps
274 * are skipped.
276 * For detection, we read registers that are most likely to cause
277 * detection failure, i.e. those that have more bits with fixed
278 * or reserved values.
281 /* Default to an DS1337 if forced */
282 if (kind == 0)
283 kind = ds1337;
285 if (kind < 0) { /* detection and identification */
286 u8 data;
288 /* Check that status register bits 6-2 are zero */
289 if ((ds1337_read(new_client, DS1337_REG_STATUS, &data) < 0) ||
290 (data & 0x7c))
291 goto exit_free;
293 /* Check for a valid day register value */
294 if ((ds1337_read(new_client, DS1337_REG_DAY, &data) < 0) ||
295 (data == 0) || (data & 0xf8))
296 goto exit_free;
298 /* Check for a valid date register value */
299 if ((ds1337_read(new_client, DS1337_REG_DATE, &data) < 0) ||
300 (data == 0) || (data & 0xc0) || ((data & 0x0f) > 9) ||
301 (data >= 0x32))
302 goto exit_free;
304 /* Check for a valid month register value */
305 if ((ds1337_read(new_client, DS1337_REG_MONTH, &data) < 0) ||
306 (data == 0) || (data & 0x60) || ((data & 0x0f) > 9) ||
307 ((data >= 0x13) && (data <= 0x19)))
308 goto exit_free;
310 /* Check that control register bits 6-5 are zero */
311 if ((ds1337_read(new_client, DS1337_REG_CONTROL, &data) < 0) ||
312 (data & 0x60))
313 goto exit_free;
315 kind = ds1337;
318 if (kind == ds1337)
319 name = "ds1337";
321 /* We can fill in the remaining client fields */
322 strlcpy(new_client->name, name, I2C_NAME_SIZE);
324 /* Tell the I2C layer a new client has arrived */
325 if ((err = i2c_attach_client(new_client)))
326 goto exit_free;
328 /* Initialize the DS1337 chip */
329 ds1337_init_client(new_client);
331 /* Add client to local list */
332 list_add(&data->list, &ds1337_clients);
334 return 0;
336 exit_free:
337 kfree(data);
338 exit:
339 return err;
342 static void ds1337_init_client(struct i2c_client *client)
344 s32 val;
346 /* Ensure that device is set in 24-hour mode */
347 val = i2c_smbus_read_byte_data(client, DS1337_REG_HOUR);
348 if ((val >= 0) && (val & (1 << 6)) == 0)
349 i2c_smbus_write_byte_data(client, DS1337_REG_HOUR,
350 val | (1 << 6));
353 static int ds1337_detach_client(struct i2c_client *client)
355 int err;
356 struct ds1337_data *data = i2c_get_clientdata(client);
358 if ((err = i2c_detach_client(client))) {
359 dev_err(&client->dev, "Client deregistration failed, "
360 "client not detached.\n");
361 return err;
364 list_del(&data->list);
365 kfree(data);
366 return 0;
369 static int __init ds1337_init(void)
371 return i2c_add_driver(&ds1337_driver);
374 static void __exit ds1337_exit(void)
376 i2c_del_driver(&ds1337_driver);
379 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
380 MODULE_DESCRIPTION("DS1337 RTC driver");
381 MODULE_LICENSE("GPL");
383 module_init(ds1337_init);
384 module_exit(ds1337_exit);