Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/drivers/i2c/chips/ds1337.c | |
3 | * | |
4 | * Copyright (C) 2005 James Chapman <jchapman@katalix.com> | |
5 | * | |
3e9d0ba1 | 6 | * based on linux/drivers/acorn/char/pcf8583.c |
1da177e4 LT |
7 | * 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 | */ | |
42 | static unsigned short normal_i2c[] = { 0x68, I2C_CLIENT_END }; | |
43 | static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END }; | |
44 | ||
45 | SENSORS_INSMOD_1(ds1337); | |
46 | ||
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); | |
53 | ||
54 | /* | |
55 | * Driver data (common to all clients) | |
56 | */ | |
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, | |
64 | }; | |
65 | ||
66 | /* | |
67 | * Client data (each client gets its own) | |
68 | */ | |
69 | struct ds1337_data { | |
70 | struct i2c_client client; | |
71 | struct list_head list; | |
72 | int id; | |
73 | }; | |
74 | ||
75 | /* | |
76 | * Internal variables | |
77 | */ | |
78 | static int ds1337_id; | |
79 | static LIST_HEAD(ds1337_clients); | |
80 | ||
81 | static 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 | */ | |
96 | static int ds1337_get_datetime(struct i2c_client *client, struct rtc_time *dt) | |
97 | { | |
1da177e4 LT |
98 | int result; |
99 | u8 buf[7]; | |
100 | u8 val; | |
101 | struct i2c_msg msg[2]; | |
102 | u8 offs = 0; | |
103 | ||
104 | if (!dt) { | |
d01b79d0 | 105 | dev_dbg(&client->dev, "%s: EINVAL: dt=NULL\n", __FUNCTION__); |
1da177e4 LT |
106 | 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 | ||
3e9d0ba1 | 119 | result = i2c_transfer(client->adapter, msg, 2); |
1da177e4 | 120 | |
d01b79d0 | 121 | dev_dbg(&client->dev, "%s: [%d] %02x %02x %02x %02x %02x %02x %02x\n", |
1da177e4 LT |
122 | __FUNCTION__, result, buf[0], buf[1], buf[2], buf[3], |
123 | buf[4], buf[5], buf[6]); | |
124 | ||
125 | if (result >= 0) { | |
6069ffde LM |
126 | dt->tm_sec = BCD2BIN(buf[0]); |
127 | dt->tm_min = BCD2BIN(buf[1]); | |
1da177e4 | 128 | val = buf[2] & 0x3f; |
6069ffde LM |
129 | dt->tm_hour = BCD2BIN(val); |
130 | dt->tm_wday = BCD2BIN(buf[3]) - 1; | |
131 | dt->tm_mday = BCD2BIN(buf[4]); | |
1da177e4 | 132 | val = buf[5] & 0x7f; |
6069ffde LM |
133 | dt->tm_mon = BCD2BIN(val); |
134 | dt->tm_year = 1900 + BCD2BIN(buf[6]); | |
1da177e4 LT |
135 | if (buf[5] & 0x80) |
136 | dt->tm_year += 100; | |
137 | ||
d01b79d0 | 138 | dev_dbg(&client->dev, "%s: secs=%d, mins=%d, " |
1da177e4 LT |
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); | |
143 | } else { | |
d01b79d0 | 144 | dev_err(&client->dev, "error reading data! %d\n", result); |
1da177e4 LT |
145 | result = -EIO; |
146 | } | |
147 | ||
148 | return result; | |
149 | } | |
150 | ||
151 | static int ds1337_set_datetime(struct i2c_client *client, struct rtc_time *dt) | |
152 | { | |
1da177e4 LT |
153 | int result; |
154 | u8 buf[8]; | |
155 | u8 val; | |
156 | struct i2c_msg msg[1]; | |
157 | ||
158 | if (!dt) { | |
d01b79d0 | 159 | dev_dbg(&client->dev, "%s: EINVAL: dt=NULL\n", __FUNCTION__); |
1da177e4 LT |
160 | return -EINVAL; |
161 | } | |
162 | ||
d01b79d0 | 163 | dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, " |
1da177e4 LT |
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); | |
167 | ||
168 | buf[0] = 0; /* reg offset */ | |
6069ffde LM |
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); | |
1da177e4 LT |
175 | if (dt->tm_year >= 2000) { |
176 | val = dt->tm_year - 2000; | |
177 | buf[6] |= (1 << 7); | |
178 | } else { | |
179 | val = dt->tm_year - 1900; | |
180 | } | |
6069ffde | 181 | buf[7] = BIN2BCD(val); |
1da177e4 LT |
182 | |
183 | msg[0].addr = client->addr; | |
184 | msg[0].flags = 0; | |
185 | msg[0].len = sizeof(buf); | |
186 | msg[0].buf = &buf[0]; | |
187 | ||
3e9d0ba1 | 188 | result = i2c_transfer(client->adapter, msg, 1); |
1da177e4 | 189 | if (result < 0) { |
d01b79d0 | 190 | dev_err(&client->dev, "error writing data! %d\n", result); |
1da177e4 LT |
191 | result = -EIO; |
192 | } else { | |
193 | result = 0; | |
194 | } | |
195 | ||
196 | return result; | |
197 | } | |
198 | ||
199 | static int ds1337_command(struct i2c_client *client, unsigned int cmd, | |
200 | void *arg) | |
201 | { | |
d01b79d0 | 202 | dev_dbg(&client->dev, "%s: cmd=%d\n", __FUNCTION__, cmd); |
1da177e4 LT |
203 | |
204 | switch (cmd) { | |
205 | case DS1337_GET_DATE: | |
206 | return ds1337_get_datetime(client, arg); | |
207 | ||
208 | case DS1337_SET_DATE: | |
209 | return ds1337_set_datetime(client, arg); | |
210 | ||
211 | default: | |
212 | return -EINVAL; | |
213 | } | |
214 | } | |
215 | ||
216 | /* | |
217 | * Public API for access to specific device. Useful for low-level | |
218 | * RTC access from kernel code. | |
219 | */ | |
220 | int ds1337_do_command(int id, int cmd, void *arg) | |
221 | { | |
222 | struct list_head *walk; | |
223 | struct list_head *tmp; | |
224 | struct ds1337_data *data; | |
225 | ||
226 | list_for_each_safe(walk, tmp, &ds1337_clients) { | |
227 | data = list_entry(walk, struct ds1337_data, list); | |
228 | if (data->id == id) | |
229 | return ds1337_command(&data->client, cmd, arg); | |
230 | } | |
231 | ||
232 | return -ENODEV; | |
233 | } | |
234 | ||
235 | static int ds1337_attach_adapter(struct i2c_adapter *adapter) | |
236 | { | |
237 | return i2c_detect(adapter, &addr_data, ds1337_detect); | |
238 | } | |
239 | ||
240 | /* | |
241 | * The following function does more than just detection. If detection | |
242 | * succeeds, it also registers the new chip. | |
243 | */ | |
244 | static int ds1337_detect(struct i2c_adapter *adapter, int address, int kind) | |
245 | { | |
246 | struct i2c_client *new_client; | |
247 | struct ds1337_data *data; | |
248 | int err = 0; | |
249 | const char *name = ""; | |
250 | ||
251 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | | |
252 | I2C_FUNC_I2C)) | |
253 | goto exit; | |
254 | ||
255 | if (!(data = kmalloc(sizeof(struct ds1337_data), GFP_KERNEL))) { | |
256 | err = -ENOMEM; | |
257 | goto exit; | |
258 | } | |
259 | memset(data, 0, sizeof(struct ds1337_data)); | |
260 | INIT_LIST_HEAD(&data->list); | |
261 | ||
262 | /* The common I2C client data is placed right before the | |
263 | * DS1337-specific data. | |
264 | */ | |
265 | new_client = &data->client; | |
266 | i2c_set_clientdata(new_client, data); | |
267 | new_client->addr = address; | |
268 | new_client->adapter = adapter; | |
269 | new_client->driver = &ds1337_driver; | |
270 | new_client->flags = 0; | |
271 | ||
272 | /* | |
273 | * Now we do the remaining detection. A negative kind means that | |
274 | * the driver was loaded with no force parameter (default), so we | |
275 | * must both detect and identify the chip. A zero kind means that | |
276 | * the driver was loaded with the force parameter, the detection | |
277 | * step shall be skipped. A positive kind means that the driver | |
278 | * was loaded with the force parameter and a given kind of chip is | |
279 | * requested, so both the detection and the identification steps | |
280 | * are skipped. | |
281 | * | |
282 | * For detection, we read registers that are most likely to cause | |
283 | * detection failure, i.e. those that have more bits with fixed | |
284 | * or reserved values. | |
285 | */ | |
286 | ||
287 | /* Default to an DS1337 if forced */ | |
288 | if (kind == 0) | |
289 | kind = ds1337; | |
290 | ||
291 | if (kind < 0) { /* detection and identification */ | |
292 | u8 data; | |
293 | ||
294 | /* Check that status register bits 6-2 are zero */ | |
295 | if ((ds1337_read(new_client, DS1337_REG_STATUS, &data) < 0) || | |
296 | (data & 0x7c)) | |
297 | goto exit_free; | |
298 | ||
299 | /* Check for a valid day register value */ | |
300 | if ((ds1337_read(new_client, DS1337_REG_DAY, &data) < 0) || | |
301 | (data == 0) || (data & 0xf8)) | |
302 | goto exit_free; | |
303 | ||
304 | /* Check for a valid date register value */ | |
305 | if ((ds1337_read(new_client, DS1337_REG_DATE, &data) < 0) || | |
306 | (data == 0) || (data & 0xc0) || ((data & 0x0f) > 9) || | |
307 | (data >= 0x32)) | |
308 | goto exit_free; | |
309 | ||
310 | /* Check for a valid month register value */ | |
311 | if ((ds1337_read(new_client, DS1337_REG_MONTH, &data) < 0) || | |
312 | (data == 0) || (data & 0x60) || ((data & 0x0f) > 9) || | |
313 | ((data >= 0x13) && (data <= 0x19))) | |
314 | goto exit_free; | |
315 | ||
316 | /* Check that control register bits 6-5 are zero */ | |
317 | if ((ds1337_read(new_client, DS1337_REG_CONTROL, &data) < 0) || | |
318 | (data & 0x60)) | |
319 | goto exit_free; | |
320 | ||
321 | kind = ds1337; | |
322 | } | |
323 | ||
324 | if (kind == ds1337) | |
325 | name = "ds1337"; | |
326 | ||
327 | /* We can fill in the remaining client fields */ | |
328 | strlcpy(new_client->name, name, I2C_NAME_SIZE); | |
329 | ||
330 | /* Tell the I2C layer a new client has arrived */ | |
331 | if ((err = i2c_attach_client(new_client))) | |
332 | goto exit_free; | |
333 | ||
334 | /* Initialize the DS1337 chip */ | |
335 | ds1337_init_client(new_client); | |
336 | ||
337 | /* Add client to local list */ | |
338 | data->id = ds1337_id++; | |
339 | list_add(&data->list, &ds1337_clients); | |
340 | ||
341 | return 0; | |
342 | ||
343 | exit_free: | |
344 | kfree(data); | |
345 | exit: | |
346 | return err; | |
347 | } | |
348 | ||
349 | static void ds1337_init_client(struct i2c_client *client) | |
350 | { | |
351 | s32 val; | |
352 | ||
353 | /* Ensure that device is set in 24-hour mode */ | |
354 | val = i2c_smbus_read_byte_data(client, DS1337_REG_HOUR); | |
355 | if ((val >= 0) && (val & (1 << 6)) == 0) | |
356 | i2c_smbus_write_byte_data(client, DS1337_REG_HOUR, | |
357 | val | (1 << 6)); | |
358 | } | |
359 | ||
360 | static int ds1337_detach_client(struct i2c_client *client) | |
361 | { | |
362 | int err; | |
363 | struct ds1337_data *data = i2c_get_clientdata(client); | |
364 | ||
365 | if ((err = i2c_detach_client(client))) { | |
366 | dev_err(&client->dev, "Client deregistration failed, " | |
367 | "client not detached.\n"); | |
368 | return err; | |
369 | } | |
370 | ||
371 | list_del(&data->list); | |
372 | kfree(data); | |
373 | return 0; | |
374 | } | |
375 | ||
376 | static int __init ds1337_init(void) | |
377 | { | |
378 | return i2c_add_driver(&ds1337_driver); | |
379 | } | |
380 | ||
381 | static void __exit ds1337_exit(void) | |
382 | { | |
383 | i2c_del_driver(&ds1337_driver); | |
384 | } | |
385 | ||
386 | MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); | |
387 | MODULE_DESCRIPTION("DS1337 RTC driver"); | |
388 | MODULE_LICENSE("GPL"); | |
389 | ||
390 | module_init(ds1337_init); | |
391 | module_exit(ds1337_exit); |