blob: c4762ac980a3beab2caaf68df94c06e744c48ef0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/i2c/chips/ds1337.c
3 *
4 * Copyright (C) 2005 James Chapman <jchapman@katalix.com>
5 *
Ladislav Michl3e9d0ba2005-04-08 15:00:21 +02006 * based on linux/drivers/acorn/char/pcf8583.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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 */
42static unsigned short normal_i2c[] = { 0x68, I2C_CLIENT_END };
43static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
44
45SENSORS_INSMOD_1(ds1337);
46
47static int ds1337_attach_adapter(struct i2c_adapter *adapter);
48static int ds1337_detect(struct i2c_adapter *adapter, int address, int kind);
49static void ds1337_init_client(struct i2c_client *client);
50static int ds1337_detach_client(struct i2c_client *client);
51static int ds1337_command(struct i2c_client *client, unsigned int cmd,
52 void *arg);
53
54/*
55 * Driver data (common to all clients)
56 */
57static 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 */
69struct ds1337_data {
70 struct i2c_client client;
71 struct list_head list;
72 int id;
73};
74
75/*
76 * Internal variables
77 */
78static int ds1337_id;
79static LIST_HEAD(ds1337_clients);
80
81static 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 */
96static int ds1337_get_datetime(struct i2c_client *client, struct rtc_time *dt)
97{
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 int result;
99 u8 buf[7];
100 u8 val;
101 struct i2c_msg msg[2];
102 u8 offs = 0;
103
104 if (!dt) {
Ladislav Michld01b79d2005-04-08 15:06:39 +0200105 dev_dbg(&client->dev, "%s: EINVAL: dt=NULL\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 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
Ladislav Michl3e9d0ba2005-04-08 15:00:21 +0200119 result = i2c_transfer(client->adapter, msg, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Ladislav Michld01b79d2005-04-08 15:06:39 +0200121 dev_dbg(&client->dev, "%s: [%d] %02x %02x %02x %02x %02x %02x %02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 __FUNCTION__, result, buf[0], buf[1], buf[2], buf[3],
123 buf[4], buf[5], buf[6]);
124
Ladislav Michl00588242005-05-04 08:13:54 +0200125 if (result == 2) {
Ladislav Michl6069ffd2005-04-08 15:02:16 +0200126 dt->tm_sec = BCD2BIN(buf[0]);
127 dt->tm_min = BCD2BIN(buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 val = buf[2] & 0x3f;
Ladislav Michl6069ffd2005-04-08 15:02:16 +0200129 dt->tm_hour = BCD2BIN(val);
130 dt->tm_wday = BCD2BIN(buf[3]) - 1;
131 dt->tm_mday = BCD2BIN(buf[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 val = buf[5] & 0x7f;
Ladislav Michl0b46e332005-05-04 08:13:13 +0200133 dt->tm_mon = BCD2BIN(val) - 1;
134 dt->tm_year = BCD2BIN(buf[6]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (buf[5] & 0x80)
136 dt->tm_year += 100;
137
Ladislav Michld01b79d2005-04-08 15:06:39 +0200138 dev_dbg(&client->dev, "%s: secs=%d, mins=%d, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 "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);
Ladislav Michl00588242005-05-04 08:13:54 +0200143
144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
Ladislav Michl00588242005-05-04 08:13:54 +0200147 dev_err(&client->dev, "error reading data! %d\n", result);
148 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151static int ds1337_set_datetime(struct i2c_client *client, struct rtc_time *dt)
152{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int result;
154 u8 buf[8];
155 u8 val;
156 struct i2c_msg msg[1];
157
158 if (!dt) {
Ladislav Michld01b79d2005-04-08 15:06:39 +0200159 dev_dbg(&client->dev, "%s: EINVAL: dt=NULL\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return -EINVAL;
161 }
162
Ladislav Michld01b79d2005-04-08 15:06:39 +0200163 dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 "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 */
Ladislav Michl6069ffd2005-04-08 15:02:16 +0200169 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);
Ladislav Michl0b46e332005-05-04 08:13:13 +0200174 buf[6] = BIN2BCD(dt->tm_mon) + 1;
175 val = dt->tm_year;
176 if (val >= 100) {
177 val -= 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 buf[6] |= (1 << 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
Ladislav Michl6069ffd2005-04-08 15:02:16 +0200180 buf[7] = BIN2BCD(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 msg[0].addr = client->addr;
183 msg[0].flags = 0;
184 msg[0].len = sizeof(buf);
185 msg[0].buf = &buf[0];
186
Ladislav Michl3e9d0ba2005-04-08 15:00:21 +0200187 result = i2c_transfer(client->adapter, msg, 1);
Ladislav Michl00588242005-05-04 08:13:54 +0200188 if (result == 1)
189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Ladislav Michl00588242005-05-04 08:13:54 +0200191 dev_err(&client->dev, "error writing data! %d\n", result);
192 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195static int ds1337_command(struct i2c_client *client, unsigned int cmd,
196 void *arg)
197{
Ladislav Michld01b79d2005-04-08 15:06:39 +0200198 dev_dbg(&client->dev, "%s: cmd=%d\n", __FUNCTION__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 switch (cmd) {
201 case DS1337_GET_DATE:
202 return ds1337_get_datetime(client, arg);
203
204 case DS1337_SET_DATE:
205 return ds1337_set_datetime(client, arg);
206
207 default:
208 return -EINVAL;
209 }
210}
211
212/*
213 * Public API for access to specific device. Useful for low-level
214 * RTC access from kernel code.
215 */
216int ds1337_do_command(int id, int cmd, void *arg)
217{
218 struct list_head *walk;
219 struct list_head *tmp;
220 struct ds1337_data *data;
221
222 list_for_each_safe(walk, tmp, &ds1337_clients) {
223 data = list_entry(walk, struct ds1337_data, list);
224 if (data->id == id)
225 return ds1337_command(&data->client, cmd, arg);
226 }
227
228 return -ENODEV;
229}
230
231static int ds1337_attach_adapter(struct i2c_adapter *adapter)
232{
233 return i2c_detect(adapter, &addr_data, ds1337_detect);
234}
235
236/*
237 * The following function does more than just detection. If detection
238 * succeeds, it also registers the new chip.
239 */
240static int ds1337_detect(struct i2c_adapter *adapter, int address, int kind)
241{
242 struct i2c_client *new_client;
243 struct ds1337_data *data;
244 int err = 0;
245 const char *name = "";
246
247 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
248 I2C_FUNC_I2C))
249 goto exit;
250
251 if (!(data = kmalloc(sizeof(struct ds1337_data), GFP_KERNEL))) {
252 err = -ENOMEM;
253 goto exit;
254 }
255 memset(data, 0, sizeof(struct ds1337_data));
256 INIT_LIST_HEAD(&data->list);
257
258 /* The common I2C client data is placed right before the
259 * DS1337-specific data.
260 */
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;
267
268 /*
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
276 * are skipped.
277 *
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.
281 */
282
283 /* Default to an DS1337 if forced */
284 if (kind == 0)
285 kind = ds1337;
286
287 if (kind < 0) { /* detection and identification */
288 u8 data;
289
290 /* Check that status register bits 6-2 are zero */
291 if ((ds1337_read(new_client, DS1337_REG_STATUS, &data) < 0) ||
292 (data & 0x7c))
293 goto exit_free;
294
295 /* Check for a valid day register value */
296 if ((ds1337_read(new_client, DS1337_REG_DAY, &data) < 0) ||
297 (data == 0) || (data & 0xf8))
298 goto exit_free;
299
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) ||
303 (data >= 0x32))
304 goto exit_free;
305
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)))
310 goto exit_free;
311
312 /* Check that control register bits 6-5 are zero */
313 if ((ds1337_read(new_client, DS1337_REG_CONTROL, &data) < 0) ||
314 (data & 0x60))
315 goto exit_free;
316
317 kind = ds1337;
318 }
319
320 if (kind == ds1337)
321 name = "ds1337";
322
323 /* We can fill in the remaining client fields */
324 strlcpy(new_client->name, name, I2C_NAME_SIZE);
325
326 /* Tell the I2C layer a new client has arrived */
327 if ((err = i2c_attach_client(new_client)))
328 goto exit_free;
329
330 /* Initialize the DS1337 chip */
331 ds1337_init_client(new_client);
332
333 /* Add client to local list */
334 data->id = ds1337_id++;
335 list_add(&data->list, &ds1337_clients);
336
337 return 0;
338
339exit_free:
340 kfree(data);
341exit:
342 return err;
343}
344
345static void ds1337_init_client(struct i2c_client *client)
346{
347 s32 val;
348
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,
353 val | (1 << 6));
354}
355
356static int ds1337_detach_client(struct i2c_client *client)
357{
358 int err;
359 struct ds1337_data *data = i2c_get_clientdata(client);
360
361 if ((err = i2c_detach_client(client))) {
362 dev_err(&client->dev, "Client deregistration failed, "
363 "client not detached.\n");
364 return err;
365 }
366
367 list_del(&data->list);
368 kfree(data);
369 return 0;
370}
371
372static int __init ds1337_init(void)
373{
374 return i2c_add_driver(&ds1337_driver);
375}
376
377static void __exit ds1337_exit(void)
378{
379 i2c_del_driver(&ds1337_driver);
380}
381
382MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
383MODULE_DESCRIPTION("DS1337 RTC driver");
384MODULE_LICENSE("GPL");
385
386module_init(ds1337_init);
387module_exit(ds1337_exit);