2 * linux/drivers/i2c/chips/ds1337.c
4 * Copyright (C) 2005 James Chapman <jchapman@katalix.com>
6 * based on linux/drivers/acron/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
)
98 struct ds1337_data
*data
= i2c_get_clientdata(client
);
102 struct i2c_msg msg
[2];
106 dev_dbg(&client
->adapter
->dev
, "%s: EINVAL: dt=NULL\n",
112 msg
[0].addr
= client
->addr
;
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
= client
->adapter
->algo
->master_xfer(client
->adapter
,
125 dev_dbg(&client
->adapter
->dev
,
126 "%s: [%d] %02x %02x %02x %02x %02x %02x %02x\n",
127 __FUNCTION__
, result
, buf
[0], buf
[1], buf
[2], buf
[3],
128 buf
[4], buf
[5], buf
[6]);
131 dt
->tm_sec
= BCD_TO_BIN(buf
[0]);
132 dt
->tm_min
= BCD_TO_BIN(buf
[1]);
134 dt
->tm_hour
= BCD_TO_BIN(val
);
135 dt
->tm_wday
= BCD_TO_BIN(buf
[3]) - 1;
136 dt
->tm_mday
= BCD_TO_BIN(buf
[4]);
138 dt
->tm_mon
= BCD_TO_BIN(val
);
139 dt
->tm_year
= 1900 + BCD_TO_BIN(buf
[6]);
143 dev_dbg(&client
->adapter
->dev
, "%s: secs=%d, mins=%d, "
144 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
145 __FUNCTION__
, dt
->tm_sec
, dt
->tm_min
,
146 dt
->tm_hour
, dt
->tm_mday
,
147 dt
->tm_mon
, dt
->tm_year
, dt
->tm_wday
);
149 dev_err(&client
->adapter
->dev
, "ds1337[%d]: error reading "
150 "data! %d\n", data
->id
, result
);
157 static int ds1337_set_datetime(struct i2c_client
*client
, struct rtc_time
*dt
)
159 struct ds1337_data
*data
= i2c_get_clientdata(client
);
163 struct i2c_msg msg
[1];
166 dev_dbg(&client
->adapter
->dev
, "%s: EINVAL: dt=NULL\n",
172 dev_dbg(&client
->adapter
->dev
, "%s: secs=%d, mins=%d, hours=%d, "
173 "mday=%d, mon=%d, year=%d, wday=%d\n", __FUNCTION__
,
174 dt
->tm_sec
, dt
->tm_min
, dt
->tm_hour
,
175 dt
->tm_mday
, dt
->tm_mon
, dt
->tm_year
, dt
->tm_wday
);
177 buf
[0] = 0; /* reg offset */
178 buf
[1] = BIN_TO_BCD(dt
->tm_sec
);
179 buf
[2] = BIN_TO_BCD(dt
->tm_min
);
180 buf
[3] = BIN_TO_BCD(dt
->tm_hour
) | (1 << 6);
181 buf
[4] = BIN_TO_BCD(dt
->tm_wday
) + 1;
182 buf
[5] = BIN_TO_BCD(dt
->tm_mday
);
183 buf
[6] = BIN_TO_BCD(dt
->tm_mon
);
184 if (dt
->tm_year
>= 2000) {
185 val
= dt
->tm_year
- 2000;
188 val
= dt
->tm_year
- 1900;
190 buf
[7] = BIN_TO_BCD(val
);
192 msg
[0].addr
= client
->addr
;
194 msg
[0].len
= sizeof(buf
);
195 msg
[0].buf
= &buf
[0];
197 result
= client
->adapter
->algo
->master_xfer(client
->adapter
,
200 dev_err(&client
->adapter
->dev
, "ds1337[%d]: error "
201 "writing data! %d\n", data
->id
, result
);
210 static int ds1337_command(struct i2c_client
*client
, unsigned int cmd
,
213 dev_dbg(&client
->adapter
->dev
, "%s: cmd=%d\n", __FUNCTION__
, cmd
);
216 case DS1337_GET_DATE
:
217 return ds1337_get_datetime(client
, arg
);
219 case DS1337_SET_DATE
:
220 return ds1337_set_datetime(client
, arg
);
228 * Public API for access to specific device. Useful for low-level
229 * RTC access from kernel code.
231 int ds1337_do_command(int id
, int cmd
, void *arg
)
233 struct list_head
*walk
;
234 struct list_head
*tmp
;
235 struct ds1337_data
*data
;
237 list_for_each_safe(walk
, tmp
, &ds1337_clients
) {
238 data
= list_entry(walk
, struct ds1337_data
, list
);
240 return ds1337_command(&data
->client
, cmd
, arg
);
246 static int ds1337_attach_adapter(struct i2c_adapter
*adapter
)
248 return i2c_detect(adapter
, &addr_data
, ds1337_detect
);
252 * The following function does more than just detection. If detection
253 * succeeds, it also registers the new chip.
255 static int ds1337_detect(struct i2c_adapter
*adapter
, int address
, int kind
)
257 struct i2c_client
*new_client
;
258 struct ds1337_data
*data
;
260 const char *name
= "";
262 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
|
266 if (!(data
= kmalloc(sizeof(struct ds1337_data
), GFP_KERNEL
))) {
270 memset(data
, 0, sizeof(struct ds1337_data
));
271 INIT_LIST_HEAD(&data
->list
);
273 /* The common I2C client data is placed right before the
274 * DS1337-specific data.
276 new_client
= &data
->client
;
277 i2c_set_clientdata(new_client
, data
);
278 new_client
->addr
= address
;
279 new_client
->adapter
= adapter
;
280 new_client
->driver
= &ds1337_driver
;
281 new_client
->flags
= 0;
284 * Now we do the remaining detection. A negative kind means that
285 * the driver was loaded with no force parameter (default), so we
286 * must both detect and identify the chip. A zero kind means that
287 * the driver was loaded with the force parameter, the detection
288 * step shall be skipped. A positive kind means that the driver
289 * was loaded with the force parameter and a given kind of chip is
290 * requested, so both the detection and the identification steps
293 * For detection, we read registers that are most likely to cause
294 * detection failure, i.e. those that have more bits with fixed
295 * or reserved values.
298 /* Default to an DS1337 if forced */
302 if (kind
< 0) { /* detection and identification */
305 /* Check that status register bits 6-2 are zero */
306 if ((ds1337_read(new_client
, DS1337_REG_STATUS
, &data
) < 0) ||
310 /* Check for a valid day register value */
311 if ((ds1337_read(new_client
, DS1337_REG_DAY
, &data
) < 0) ||
312 (data
== 0) || (data
& 0xf8))
315 /* Check for a valid date register value */
316 if ((ds1337_read(new_client
, DS1337_REG_DATE
, &data
) < 0) ||
317 (data
== 0) || (data
& 0xc0) || ((data
& 0x0f) > 9) ||
321 /* Check for a valid month register value */
322 if ((ds1337_read(new_client
, DS1337_REG_MONTH
, &data
) < 0) ||
323 (data
== 0) || (data
& 0x60) || ((data
& 0x0f) > 9) ||
324 ((data
>= 0x13) && (data
<= 0x19)))
327 /* Check that control register bits 6-5 are zero */
328 if ((ds1337_read(new_client
, DS1337_REG_CONTROL
, &data
) < 0) ||
338 /* We can fill in the remaining client fields */
339 strlcpy(new_client
->name
, name
, I2C_NAME_SIZE
);
341 /* Tell the I2C layer a new client has arrived */
342 if ((err
= i2c_attach_client(new_client
)))
345 /* Initialize the DS1337 chip */
346 ds1337_init_client(new_client
);
348 /* Add client to local list */
349 data
->id
= ds1337_id
++;
350 list_add(&data
->list
, &ds1337_clients
);
360 static void ds1337_init_client(struct i2c_client
*client
)
364 /* Ensure that device is set in 24-hour mode */
365 val
= i2c_smbus_read_byte_data(client
, DS1337_REG_HOUR
);
366 if ((val
>= 0) && (val
& (1 << 6)) == 0)
367 i2c_smbus_write_byte_data(client
, DS1337_REG_HOUR
,
371 static int ds1337_detach_client(struct i2c_client
*client
)
374 struct ds1337_data
*data
= i2c_get_clientdata(client
);
376 if ((err
= i2c_detach_client(client
))) {
377 dev_err(&client
->dev
, "Client deregistration failed, "
378 "client not detached.\n");
382 list_del(&data
->list
);
387 static int __init
ds1337_init(void)
389 return i2c_add_driver(&ds1337_driver
);
392 static void __exit
ds1337_exit(void)
394 i2c_del_driver(&ds1337_driver
);
397 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
398 MODULE_DESCRIPTION("DS1337 RTC driver");
399 MODULE_LICENSE("GPL");
401 module_init(ds1337_init
);
402 module_exit(ds1337_exit
);