[PATCH] I2C: ds1337 2/4
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / i2c / chips / ds1337.c
blob8a9e751d609ba924bb4e65bc86abea73671409bd
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;
72 int id;
76 * Internal variables
78 static int ds1337_id;
79 static LIST_HEAD(ds1337_clients);
81 static inline int ds1337_read(struct i2c_client *client, u8 reg, u8 *value)
83 s32 tmp = i2c_smbus_read_byte_data(client, reg);
85 if (tmp < 0)
86 return -EIO;
88 *value = tmp;
90 return 0;
94 * Chip access functions
96 static int ds1337_get_datetime(struct i2c_client *client, struct rtc_time *dt)
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;
105 if (!dt) {
106 dev_dbg(&client->adapter->dev, "%s: EINVAL: dt=NULL\n",
107 __FUNCTION__);
109 return -EINVAL;
112 msg[0].addr = client->addr;
113 msg[0].flags = 0;
114 msg[0].len = 1;
115 msg[0].buf = &offs;
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];
122 result = i2c_transfer(client->adapter, msg, 2);
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]);
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;
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;
153 return result;
156 static int ds1337_set_datetime(struct i2c_client *client, struct rtc_time *dt)
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];
164 if (!dt) {
165 dev_dbg(&client->adapter->dev, "%s: EINVAL: dt=NULL\n",
166 __FUNCTION__);
168 return -EINVAL;
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);
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;
189 buf[7] = BIN2BCD(val);
191 msg[0].addr = client->addr;
192 msg[0].flags = 0;
193 msg[0].len = sizeof(buf);
194 msg[0].buf = &buf[0];
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;
205 return result;
208 static int ds1337_command(struct i2c_client *client, unsigned int cmd,
209 void *arg)
211 dev_dbg(&client->adapter->dev, "%s: cmd=%d\n", __FUNCTION__, cmd);
213 switch (cmd) {
214 case DS1337_GET_DATE:
215 return ds1337_get_datetime(client, arg);
217 case DS1337_SET_DATE:
218 return ds1337_set_datetime(client, arg);
220 default:
221 return -EINVAL;
226 * Public API for access to specific device. Useful for low-level
227 * RTC access from kernel code.
229 int ds1337_do_command(int id, int cmd, void *arg)
231 struct list_head *walk;
232 struct list_head *tmp;
233 struct ds1337_data *data;
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);
241 return -ENODEV;
244 static int ds1337_attach_adapter(struct i2c_adapter *adapter)
246 return i2c_detect(adapter, &addr_data, ds1337_detect);
250 * The following function does more than just detection. If detection
251 * succeeds, it also registers the new chip.
253 static int ds1337_detect(struct i2c_adapter *adapter, int address, int kind)
255 struct i2c_client *new_client;
256 struct ds1337_data *data;
257 int err = 0;
258 const char *name = "";
260 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
261 I2C_FUNC_I2C))
262 goto exit;
264 if (!(data = kmalloc(sizeof(struct ds1337_data), GFP_KERNEL))) {
265 err = -ENOMEM;
266 goto exit;
268 memset(data, 0, sizeof(struct ds1337_data));
269 INIT_LIST_HEAD(&data->list);
271 /* The common I2C client data is placed right before the
272 * DS1337-specific data.
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;
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.
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.
296 /* Default to an DS1337 if forced */
297 if (kind == 0)
298 kind = ds1337;
300 if (kind < 0) { /* detection and identification */
301 u8 data;
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;
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;
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;
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;
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;
330 kind = ds1337;
333 if (kind == ds1337)
334 name = "ds1337";
336 /* We can fill in the remaining client fields */
337 strlcpy(new_client->name, name, I2C_NAME_SIZE);
339 /* Tell the I2C layer a new client has arrived */
340 if ((err = i2c_attach_client(new_client)))
341 goto exit_free;
343 /* Initialize the DS1337 chip */
344 ds1337_init_client(new_client);
346 /* Add client to local list */
347 data->id = ds1337_id++;
348 list_add(&data->list, &ds1337_clients);
350 return 0;
352 exit_free:
353 kfree(data);
354 exit:
355 return err;
358 static void ds1337_init_client(struct i2c_client *client)
360 s32 val;
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));
369 static int ds1337_detach_client(struct i2c_client *client)
371 int err;
372 struct ds1337_data *data = i2c_get_clientdata(client);
374 if ((err = i2c_detach_client(client))) {
375 dev_err(&client->dev, "Client deregistration failed, "
376 "client not detached.\n");
377 return err;
380 list_del(&data->list);
381 kfree(data);
382 return 0;
385 static int __init ds1337_init(void)
387 return i2c_add_driver(&ds1337_driver);
390 static void __exit ds1337_exit(void)
392 i2c_del_driver(&ds1337_driver);
395 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
396 MODULE_DESCRIPTION("DS1337 RTC driver");
397 MODULE_LICENSE("GPL");
399 module_init(ds1337_init);
400 module_exit(ds1337_exit);