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,
55 * Driver data (common to all clients)
57 static struct i2c_driver ds1337_driver = {
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)
70 struct i2c_client client;
71 struct list_head list;
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);
94 * Chip access functions
96 static int ds1337_get_datetime(struct i2c_client *client, struct rtc_time *dt)
101 struct i2c_msg msg[2];
105 dev_dbg(&client->dev, "%s: EINVAL: dt=NULL\n", __FUNCTION__);
109 msg[0].addr = client->addr;
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];
119 result = i2c_transfer(client->adapter, msg, 2);
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]);
126 dt->tm_sec = BCD2BIN(buf[0]);
127 dt->tm_min = BCD2BIN(buf[1]);
129 dt->tm_hour = BCD2BIN(val);
130 dt->tm_wday = BCD2BIN(buf[3]) - 1;
131 dt->tm_mday = BCD2BIN(buf[4]);
133 dt->tm_mon = BCD2BIN(val) - 1;
134 dt->tm_year = BCD2BIN(buf[6]);
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);
147 dev_err(&client->dev, "error reading data! %d\n", result);
151 static int ds1337_set_datetime(struct i2c_client *client, struct rtc_time *dt)
156 struct i2c_msg msg[1];
159 dev_dbg(&client->dev, "%s: EINVAL: dt=NULL\n", __FUNCTION__);
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);
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;
180 buf[7] = BIN2BCD(val);
182 msg[0].addr = client->addr;
184 msg[0].len = sizeof(buf);
185 msg[0].buf = &buf[0];
187 result = i2c_transfer(client->adapter, msg, 1);
191 dev_err(&client->dev, "error writing data! %d\n", result);
195 static int ds1337_command(struct i2c_client *client, unsigned int cmd,
198 dev_dbg(&client->dev, "%s: cmd=%d\n", __FUNCTION__, cmd);
201 case DS1337_GET_DATE:
202 return ds1337_get_datetime(client, arg);
204 case DS1337_SET_DATE:
205 return ds1337_set_datetime(client, arg);
213 * Public API for access to specific device. Useful for low-level
214 * RTC access from kernel code.
216 int ds1337_do_command(int id, int cmd, void *arg)
218 struct list_head *walk;
219 struct list_head *tmp;
220 struct ds1337_data *data;
222 list_for_each_safe(walk, tmp, &ds1337_clients) {
223 data = list_entry(walk, struct ds1337_data, list);
225 return ds1337_command(&data->client, cmd, arg);
231 static int ds1337_attach_adapter(struct i2c_adapter *adapter)
233 return i2c_detect(adapter, &addr_data, ds1337_detect);
237 * The following function does more than just detection. If detection
238 * succeeds, it also registers the new chip.
240 static int ds1337_detect(struct i2c_adapter *adapter, int address, int kind)
242 struct i2c_client *new_client;
243 struct ds1337_data *data;
245 const char *name = "";
247 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
251 if (!(data = kmalloc(sizeof(struct ds1337_data), GFP_KERNEL))) {
255 memset(data, 0, sizeof(struct ds1337_data));
256 INIT_LIST_HEAD(&data->list);
258 /* The common I2C client data is placed right before the
259 * DS1337-specific data.
261 new_client = &data->client;
262 i2c_set_clientdata(new_client, data);
263 new_client->addr = address;
264 new_client->adapter = adapter;
265 new_client->driver = &ds1337_driver;
266 new_client->flags = 0;
269 * Now we do the remaining detection. A negative kind means that
270 * the driver was loaded with no force parameter (default), so we
271 * must both detect and identify the chip. A zero kind means that
272 * the driver was loaded with the force parameter, the detection
273 * step shall be skipped. A positive kind means that the driver
274 * was loaded with the force parameter and a given kind of chip is
275 * requested, so both the detection and the identification steps
278 * For detection, we read registers that are most likely to cause
279 * detection failure, i.e. those that have more bits with fixed
280 * or reserved values.
283 /* Default to an DS1337 if forced */
287 if (kind < 0) { /* detection and identification */
290 /* Check that status register bits 6-2 are zero */
291 if ((ds1337_read(new_client, DS1337_REG_STATUS, &data) < 0) ||
295 /* Check for a valid day register value */
296 if ((ds1337_read(new_client, DS1337_REG_DAY, &data) < 0) ||
297 (data == 0) || (data & 0xf8))
300 /* Check for a valid date register value */
301 if ((ds1337_read(new_client, DS1337_REG_DATE, &data) < 0) ||
302 (data == 0) || (data & 0xc0) || ((data & 0x0f) > 9) ||
306 /* Check for a valid month register value */
307 if ((ds1337_read(new_client, DS1337_REG_MONTH, &data) < 0) ||
308 (data == 0) || (data & 0x60) || ((data & 0x0f) > 9) ||
309 ((data >= 0x13) && (data <= 0x19)))
312 /* Check that control register bits 6-5 are zero */
313 if ((ds1337_read(new_client, DS1337_REG_CONTROL, &data) < 0) ||
323 /* We can fill in the remaining client fields */
324 strlcpy(new_client->name, name, I2C_NAME_SIZE);
326 /* Tell the I2C layer a new client has arrived */
327 if ((err = i2c_attach_client(new_client)))
330 /* Initialize the DS1337 chip */
331 ds1337_init_client(new_client);
333 /* Add client to local list */
334 data->id = ds1337_id++;
335 list_add(&data->list, &ds1337_clients);
345 static void ds1337_init_client(struct i2c_client *client)
349 /* Ensure that device is set in 24-hour mode */
350 val = i2c_smbus_read_byte_data(client, DS1337_REG_HOUR);
351 if ((val >= 0) && (val & (1 << 6)) == 0)
352 i2c_smbus_write_byte_data(client, DS1337_REG_HOUR,
356 static int ds1337_detach_client(struct i2c_client *client)
359 struct ds1337_data *data = i2c_get_clientdata(client);
361 if ((err = i2c_detach_client(client))) {
362 dev_err(&client->dev, "Client deregistration failed, "
363 "client not detached.\n");
367 list_del(&data->list);
372 static int __init ds1337_init(void)
374 return i2c_add_driver(&ds1337_driver);
377 static void __exit ds1337_exit(void)
379 i2c_del_driver(&ds1337_driver);
382 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
383 MODULE_DESCRIPTION("DS1337 RTC driver");
384 MODULE_LICENSE("GPL");
386 module_init(ds1337_init);
387 module_exit(ds1337_exit);