diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/i2c/chips/ds1337.c |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'drivers/i2c/chips/ds1337.c')
-rw-r--r-- | drivers/i2c/chips/ds1337.c | 402 |
1 files changed, 402 insertions, 0 deletions
diff --git a/drivers/i2c/chips/ds1337.c b/drivers/i2c/chips/ds1337.c new file mode 100644 index 000000000000..07f16c3fb084 --- /dev/null +++ b/drivers/i2c/chips/ds1337.c | |||
@@ -0,0 +1,402 @@ | |||
1 | /* | ||
2 | * linux/drivers/i2c/chips/ds1337.c | ||
3 | * | ||
4 | * Copyright (C) 2005 James Chapman <jchapman@katalix.com> | ||
5 | * | ||
6 | * based on linux/drivers/acron/char/pcf8583.c | ||
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 | { | ||
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; | ||
104 | |||
105 | if (!dt) { | ||
106 | dev_dbg(&client->adapter->dev, "%s: EINVAL: dt=NULL\n", | ||
107 | __FUNCTION__); | ||
108 | |||
109 | return -EINVAL; | ||
110 | } | ||
111 | |||
112 | msg[0].addr = client->addr; | ||
113 | msg[0].flags = 0; | ||
114 | msg[0].len = 1; | ||
115 | msg[0].buf = &offs; | ||
116 | |||
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]; | ||
121 | |||
122 | result = client->adapter->algo->master_xfer(client->adapter, | ||
123 | &msg[0], 2); | ||
124 | |||
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]); | ||
129 | |||
130 | if (result >= 0) { | ||
131 | dt->tm_sec = BCD_TO_BIN(buf[0]); | ||
132 | dt->tm_min = BCD_TO_BIN(buf[1]); | ||
133 | val = buf[2] & 0x3f; | ||
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]); | ||
137 | val = buf[5] & 0x7f; | ||
138 | dt->tm_mon = BCD_TO_BIN(val); | ||
139 | dt->tm_year = 1900 + BCD_TO_BIN(buf[6]); | ||
140 | if (buf[5] & 0x80) | ||
141 | dt->tm_year += 100; | ||
142 | |||
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); | ||
148 | } else { | ||
149 | dev_err(&client->adapter->dev, "ds1337[%d]: error reading " | ||
150 | "data! %d\n", data->id, result); | ||
151 | result = -EIO; | ||
152 | } | ||
153 | |||
154 | return result; | ||
155 | } | ||
156 | |||
157 | static int ds1337_set_datetime(struct i2c_client *client, struct rtc_time *dt) | ||
158 | { | ||
159 | struct ds1337_data *data = i2c_get_clientdata(client); | ||
160 | int result; | ||
161 | u8 buf[8]; | ||
162 | u8 val; | ||
163 | struct i2c_msg msg[1]; | ||
164 | |||
165 | if (!dt) { | ||
166 | dev_dbg(&client->adapter->dev, "%s: EINVAL: dt=NULL\n", | ||
167 | __FUNCTION__); | ||
168 | |||
169 | return -EINVAL; | ||
170 | } | ||
171 | |||
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); | ||
176 | |||
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; | ||
186 | buf[6] |= (1 << 7); | ||
187 | } else { | ||
188 | val = dt->tm_year - 1900; | ||
189 | } | ||
190 | buf[7] = BIN_TO_BCD(val); | ||
191 | |||
192 | msg[0].addr = client->addr; | ||
193 | msg[0].flags = 0; | ||
194 | msg[0].len = sizeof(buf); | ||
195 | msg[0].buf = &buf[0]; | ||
196 | |||
197 | result = client->adapter->algo->master_xfer(client->adapter, | ||
198 | &msg[0], 1); | ||
199 | if (result < 0) { | ||
200 | dev_err(&client->adapter->dev, "ds1337[%d]: error " | ||
201 | "writing data! %d\n", data->id, result); | ||
202 | result = -EIO; | ||
203 | } else { | ||
204 | result = 0; | ||
205 | } | ||
206 | |||
207 | return result; | ||
208 | } | ||
209 | |||
210 | static int ds1337_command(struct i2c_client *client, unsigned int cmd, | ||
211 | void *arg) | ||
212 | { | ||
213 | dev_dbg(&client->adapter->dev, "%s: cmd=%d\n", __FUNCTION__, cmd); | ||
214 | |||
215 | switch (cmd) { | ||
216 | case DS1337_GET_DATE: | ||
217 | return ds1337_get_datetime(client, arg); | ||
218 | |||
219 | case DS1337_SET_DATE: | ||
220 | return ds1337_set_datetime(client, arg); | ||
221 | |||
222 | default: | ||
223 | return -EINVAL; | ||
224 | } | ||
225 | } | ||
226 | |||
227 | /* | ||
228 | * Public API for access to specific device. Useful for low-level | ||
229 | * RTC access from kernel code. | ||
230 | */ | ||
231 | int ds1337_do_command(int id, int cmd, void *arg) | ||
232 | { | ||
233 | struct list_head *walk; | ||
234 | struct list_head *tmp; | ||
235 | struct ds1337_data *data; | ||
236 | |||
237 | list_for_each_safe(walk, tmp, &ds1337_clients) { | ||
238 | data = list_entry(walk, struct ds1337_data, list); | ||
239 | if (data->id == id) | ||
240 | return ds1337_command(&data->client, cmd, arg); | ||
241 | } | ||
242 | |||
243 | return -ENODEV; | ||
244 | } | ||
245 | |||
246 | static int ds1337_attach_adapter(struct i2c_adapter *adapter) | ||
247 | { | ||
248 | return i2c_detect(adapter, &addr_data, ds1337_detect); | ||
249 | } | ||
250 | |||
251 | /* | ||
252 | * The following function does more than just detection. If detection | ||
253 | * succeeds, it also registers the new chip. | ||
254 | */ | ||
255 | static int ds1337_detect(struct i2c_adapter *adapter, int address, int kind) | ||
256 | { | ||
257 | struct i2c_client *new_client; | ||
258 | struct ds1337_data *data; | ||
259 | int err = 0; | ||
260 | const char *name = ""; | ||
261 | |||
262 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | | ||
263 | I2C_FUNC_I2C)) | ||
264 | goto exit; | ||
265 | |||
266 | if (!(data = kmalloc(sizeof(struct ds1337_data), GFP_KERNEL))) { | ||
267 | err = -ENOMEM; | ||
268 | goto exit; | ||
269 | } | ||
270 | memset(data, 0, sizeof(struct ds1337_data)); | ||
271 | INIT_LIST_HEAD(&data->list); | ||
272 | |||
273 | /* The common I2C client data is placed right before the | ||
274 | * DS1337-specific data. | ||
275 | */ | ||
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; | ||
282 | |||
283 | /* | ||
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 | ||
291 | * are skipped. | ||
292 | * | ||
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. | ||
296 | */ | ||
297 | |||
298 | /* Default to an DS1337 if forced */ | ||
299 | if (kind == 0) | ||
300 | kind = ds1337; | ||
301 | |||
302 | if (kind < 0) { /* detection and identification */ | ||
303 | u8 data; | ||
304 | |||
305 | /* Check that status register bits 6-2 are zero */ | ||
306 | if ((ds1337_read(new_client, DS1337_REG_STATUS, &data) < 0) || | ||
307 | (data & 0x7c)) | ||
308 | goto exit_free; | ||
309 | |||
310 | /* Check for a valid day register value */ | ||
311 | if ((ds1337_read(new_client, DS1337_REG_DAY, &data) < 0) || | ||
312 | (data == 0) || (data & 0xf8)) | ||
313 | goto exit_free; | ||
314 | |||
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) || | ||
318 | (data >= 0x32)) | ||
319 | goto exit_free; | ||
320 | |||
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))) | ||
325 | goto exit_free; | ||
326 | |||
327 | /* Check that control register bits 6-5 are zero */ | ||
328 | if ((ds1337_read(new_client, DS1337_REG_CONTROL, &data) < 0) || | ||
329 | (data & 0x60)) | ||
330 | goto exit_free; | ||
331 | |||
332 | kind = ds1337; | ||
333 | } | ||
334 | |||
335 | if (kind == ds1337) | ||
336 | name = "ds1337"; | ||
337 | |||
338 | /* We can fill in the remaining client fields */ | ||
339 | strlcpy(new_client->name, name, I2C_NAME_SIZE); | ||
340 | |||
341 | /* Tell the I2C layer a new client has arrived */ | ||
342 | if ((err = i2c_attach_client(new_client))) | ||
343 | goto exit_free; | ||
344 | |||
345 | /* Initialize the DS1337 chip */ | ||
346 | ds1337_init_client(new_client); | ||
347 | |||
348 | /* Add client to local list */ | ||
349 | data->id = ds1337_id++; | ||
350 | list_add(&data->list, &ds1337_clients); | ||
351 | |||
352 | return 0; | ||
353 | |||
354 | exit_free: | ||
355 | kfree(data); | ||
356 | exit: | ||
357 | return err; | ||
358 | } | ||
359 | |||
360 | static void ds1337_init_client(struct i2c_client *client) | ||
361 | { | ||
362 | s32 val; | ||
363 | |||
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, | ||
368 | val | (1 << 6)); | ||
369 | } | ||
370 | |||
371 | static int ds1337_detach_client(struct i2c_client *client) | ||
372 | { | ||
373 | int err; | ||
374 | struct ds1337_data *data = i2c_get_clientdata(client); | ||
375 | |||
376 | if ((err = i2c_detach_client(client))) { | ||
377 | dev_err(&client->dev, "Client deregistration failed, " | ||
378 | "client not detached.\n"); | ||
379 | return err; | ||
380 | } | ||
381 | |||
382 | list_del(&data->list); | ||
383 | kfree(data); | ||
384 | return 0; | ||
385 | } | ||
386 | |||
387 | static int __init ds1337_init(void) | ||
388 | { | ||
389 | return i2c_add_driver(&ds1337_driver); | ||
390 | } | ||
391 | |||
392 | static void __exit ds1337_exit(void) | ||
393 | { | ||
394 | i2c_del_driver(&ds1337_driver); | ||
395 | } | ||
396 | |||
397 | MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); | ||
398 | MODULE_DESCRIPTION("DS1337 RTC driver"); | ||
399 | MODULE_LICENSE("GPL"); | ||
400 | |||
401 | module_init(ds1337_init); | ||
402 | module_exit(ds1337_exit); | ||