diff options
author | BGardner@Wabtec.com <BGardner@Wabtec.com> | 2005-06-03 13:03:27 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2005-06-22 00:52:05 -0400 |
commit | c3bc4caedd84ad03360cb9ec04b6c44ab314588b (patch) | |
tree | 5ae34e8b136d2584be6d30f9203c7dba49f27663 /drivers/i2c/chips/max6875.c | |
parent | 20ad93d4e5cf5f0616198b5919ee9f304119dd4b (diff) |
[PATCH] max6875: new i2c device driver
This patch adds support for the MAX6875/MAX6874 chips.
Signed-off-by: Ben Gardner <bgardner@wabtec.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/i2c/chips/max6875.c')
-rw-r--r-- | drivers/i2c/chips/max6875.c | 473 |
1 files changed, 473 insertions, 0 deletions
diff --git a/drivers/i2c/chips/max6875.c b/drivers/i2c/chips/max6875.c new file mode 100644 index 000000000000..fe6b150ec4c2 --- /dev/null +++ b/drivers/i2c/chips/max6875.c | |||
@@ -0,0 +1,473 @@ | |||
1 | /* | ||
2 | max6875.c - driver for MAX6874/MAX6875 | ||
3 | |||
4 | Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com> | ||
5 | |||
6 | Based on i2c/chips/eeprom.c | ||
7 | |||
8 | The MAX6875 has two EEPROM sections: config and user. | ||
9 | At reset, the config EEPROM is read into the registers. | ||
10 | |||
11 | This driver make 3 binary files available in sysfs: | ||
12 | reg_config - direct access to the registers | ||
13 | eeprom_config - acesses configuration eeprom space | ||
14 | eeprom_user - free for application use | ||
15 | |||
16 | In our application, we put device serial & model numbers in user eeprom. | ||
17 | |||
18 | Notes: | ||
19 | 1) The datasheet says that register 0x44 / EEPROM 0x8044 should NOT | ||
20 | be overwritten, so the driver explicitly prevents that. | ||
21 | 2) It's a good idea to keep the config (0x45) locked in config EEPROM. | ||
22 | You can temporarily enable config writes by changing register 0x45. | ||
23 | |||
24 | This program is free software; you can redistribute it and/or modify | ||
25 | it under the terms of the GNU General Public License as published by | ||
26 | the Free Software Foundation; version 2 of the License. | ||
27 | */ | ||
28 | |||
29 | #include <linux/config.h> | ||
30 | #include <linux/kernel.h> | ||
31 | #include <linux/init.h> | ||
32 | #include <linux/module.h> | ||
33 | #include <linux/slab.h> | ||
34 | #include <linux/sched.h> | ||
35 | #include <linux/delay.h> | ||
36 | #include <linux/i2c.h> | ||
37 | #include <linux/i2c-sensor.h> | ||
38 | |||
39 | /* Addresses to scan */ | ||
40 | static unsigned short normal_i2c[] = {0x50, 0x52, I2C_CLIENT_END}; | ||
41 | static unsigned int normal_isa[] = {I2C_CLIENT_ISA_END}; | ||
42 | |||
43 | /* Insmod parameters */ | ||
44 | SENSORS_INSMOD_1(max6875); | ||
45 | |||
46 | /* this param will prevent 'accidental' writes to the eeprom */ | ||
47 | static int allow_write = 0; | ||
48 | module_param(allow_write, int, 0); | ||
49 | MODULE_PARM_DESC(allow_write, | ||
50 | "Enable write access:\n" | ||
51 | "*0: Read only\n" | ||
52 | " 1: Read/Write access"); | ||
53 | |||
54 | /* The MAX6875 can only read/write 16 bytes at a time */ | ||
55 | #define SLICE_SIZE 16 | ||
56 | #define SLICE_BITS 4 | ||
57 | |||
58 | /* CONFIG EEPROM is at addresses 0x8000 - 0x8045, registers are at 0 - 0x45 */ | ||
59 | #define CONFIG_EEPROM_BASE 0x8000 | ||
60 | #define CONFIG_EEPROM_SIZE 0x0046 | ||
61 | #define CONFIG_EEPROM_SLICES 5 | ||
62 | |||
63 | /* USER EEPROM is at addresses 0x8100 - 0x82FF */ | ||
64 | #define USER_EEPROM_BASE 0x8100 | ||
65 | #define USER_EEPROM_SIZE 0x0200 | ||
66 | #define USER_EEPROM_SLICES 32 | ||
67 | |||
68 | /* MAX6875 commands */ | ||
69 | #define MAX6875_CMD_BLOCK_WRITE 0x83 | ||
70 | #define MAX6875_CMD_BLOCK_READ 0x84 | ||
71 | #define MAX6875_CMD_REBOOT 0x88 | ||
72 | |||
73 | enum max6875_area_type { | ||
74 | max6875_register_config=0, | ||
75 | max6875_eeprom_config, | ||
76 | max6875_eeprom_user, | ||
77 | max6857_max | ||
78 | }; | ||
79 | |||
80 | struct eeprom_block { | ||
81 | enum max6875_area_type type; | ||
82 | u8 slices; | ||
83 | u32 size; | ||
84 | u32 valid; | ||
85 | u32 base; | ||
86 | unsigned long *updated; | ||
87 | u8 *data; | ||
88 | }; | ||
89 | |||
90 | /* Each client has this additional data */ | ||
91 | struct max6875_data { | ||
92 | struct i2c_client client; | ||
93 | struct semaphore update_lock; | ||
94 | struct eeprom_block blocks[max6857_max]; | ||
95 | /* the above structs point into the arrays below */ | ||
96 | u8 data[USER_EEPROM_SIZE + (CONFIG_EEPROM_SIZE*2)]; | ||
97 | unsigned long last_updated[USER_EEPROM_SLICES + (CONFIG_EEPROM_SLICES*2)]; | ||
98 | }; | ||
99 | |||
100 | static int max6875_attach_adapter(struct i2c_adapter *adapter); | ||
101 | static int max6875_detect(struct i2c_adapter *adapter, int address, int kind); | ||
102 | static int max6875_detach_client(struct i2c_client *client); | ||
103 | |||
104 | /* This is the driver that will be inserted */ | ||
105 | static struct i2c_driver max6875_driver = { | ||
106 | .owner = THIS_MODULE, | ||
107 | .name = "max6875", | ||
108 | .flags = I2C_DF_NOTIFY, | ||
109 | .attach_adapter = max6875_attach_adapter, | ||
110 | .detach_client = max6875_detach_client, | ||
111 | }; | ||
112 | |||
113 | static int max6875_update_slice(struct i2c_client *client, | ||
114 | struct eeprom_block *blk, | ||
115 | int slice) | ||
116 | { | ||
117 | struct max6875_data *data = i2c_get_clientdata(client); | ||
118 | int i, j, addr, count; | ||
119 | u8 rdbuf[SLICE_SIZE]; | ||
120 | int retval = 0; | ||
121 | |||
122 | if (slice >= blk->slices) | ||
123 | return -1; | ||
124 | |||
125 | down(&data->update_lock); | ||
126 | |||
127 | if (!(blk->valid & (1 << slice)) || | ||
128 | (jiffies - blk->updated[slice] > 300 * HZ) || | ||
129 | (jiffies < blk->updated[slice])) { | ||
130 | dev_dbg(&client->dev, "Starting eeprom update, slice %u, base %u\n", | ||
131 | slice, blk->base); | ||
132 | |||
133 | addr = blk->base + (slice << SLICE_BITS); | ||
134 | count = blk->size - (slice << SLICE_BITS); | ||
135 | if (count > SLICE_SIZE) { | ||
136 | count = SLICE_SIZE; | ||
137 | } | ||
138 | |||
139 | /* Preset the read address */ | ||
140 | if (addr < 0x100) { | ||
141 | /* select the register */ | ||
142 | if (i2c_smbus_write_byte(client, addr & 0xFF)) { | ||
143 | dev_dbg(&client->dev, "max6875 register select has failed!\n"); | ||
144 | retval = -1; | ||
145 | goto exit; | ||
146 | } | ||
147 | } else { | ||
148 | /* select the eeprom */ | ||
149 | if (i2c_smbus_write_byte_data(client, addr >> 8, addr & 0xFF)) { | ||
150 | dev_dbg(&client->dev, "max6875 address set has failed!\n"); | ||
151 | retval = -1; | ||
152 | goto exit; | ||
153 | } | ||
154 | } | ||
155 | |||
156 | if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { | ||
157 | if (i2c_smbus_read_i2c_block_data(client, MAX6875_CMD_BLOCK_READ, | ||
158 | rdbuf) != SLICE_SIZE) | ||
159 | { | ||
160 | retval = -1; | ||
161 | goto exit; | ||
162 | } | ||
163 | |||
164 | memcpy(&blk->data[slice << SLICE_BITS], rdbuf, count); | ||
165 | } else { | ||
166 | for (i = 0; i < count; i++) { | ||
167 | j = i2c_smbus_read_byte(client); | ||
168 | if (j < 0) | ||
169 | { | ||
170 | retval = -1; | ||
171 | goto exit; | ||
172 | } | ||
173 | blk->data[(slice << SLICE_BITS) + i] = (u8) j; | ||
174 | } | ||
175 | } | ||
176 | blk->updated[slice] = jiffies; | ||
177 | blk->valid |= (1 << slice); | ||
178 | } | ||
179 | exit: | ||
180 | up(&data->update_lock); | ||
181 | return retval; | ||
182 | } | ||
183 | |||
184 | static ssize_t max6875_read(struct kobject *kobj, char *buf, loff_t off, size_t count, | ||
185 | enum max6875_area_type area_type) | ||
186 | { | ||
187 | struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj)); | ||
188 | struct max6875_data *data = i2c_get_clientdata(client); | ||
189 | struct eeprom_block *blk; | ||
190 | int slice; | ||
191 | |||
192 | blk = &data->blocks[area_type]; | ||
193 | |||
194 | if (off > blk->size) | ||
195 | return 0; | ||
196 | if (off + count > blk->size) | ||
197 | count = blk->size - off; | ||
198 | |||
199 | /* Only refresh slices which contain requested bytes */ | ||
200 | for (slice = (off >> SLICE_BITS); slice <= ((off + count - 1) >> SLICE_BITS); slice++) | ||
201 | max6875_update_slice(client, blk, slice); | ||
202 | |||
203 | memcpy(buf, &blk->data[off], count); | ||
204 | |||
205 | return count; | ||
206 | } | ||
207 | |||
208 | static ssize_t max6875_user_read(struct kobject *kobj, char *buf, loff_t off, size_t count) | ||
209 | { | ||
210 | return max6875_read(kobj, buf, off, count, max6875_eeprom_user); | ||
211 | } | ||
212 | |||
213 | static ssize_t max6875_config_read(struct kobject *kobj, char *buf, loff_t off, size_t count) | ||
214 | { | ||
215 | return max6875_read(kobj, buf, off, count, max6875_eeprom_config); | ||
216 | } | ||
217 | |||
218 | static ssize_t max6875_cfgreg_read(struct kobject *kobj, char *buf, loff_t off, size_t count) | ||
219 | { | ||
220 | return max6875_read(kobj, buf, off, count, max6875_register_config); | ||
221 | } | ||
222 | |||
223 | |||
224 | static ssize_t max6875_write(struct kobject *kobj, char *buf, loff_t off, size_t count, | ||
225 | enum max6875_area_type area_type) | ||
226 | { | ||
227 | struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj)); | ||
228 | struct max6875_data *data = i2c_get_clientdata(client); | ||
229 | struct eeprom_block *blk; | ||
230 | int slice, addr, retval; | ||
231 | ssize_t sent = 0; | ||
232 | |||
233 | blk = &data->blocks[area_type]; | ||
234 | |||
235 | if (off > blk->size) | ||
236 | return 0; | ||
237 | if ((off + count) > blk->size) | ||
238 | count = blk->size - off; | ||
239 | |||
240 | if (down_interruptible(&data->update_lock)) | ||
241 | return -EAGAIN; | ||
242 | |||
243 | /* writing to a register is done with i2c_smbus_write_byte_data() */ | ||
244 | if (blk->type == max6875_register_config) { | ||
245 | for (sent = 0; sent < count; sent++) { | ||
246 | addr = off + sent; | ||
247 | if (addr == 0x44) | ||
248 | continue; | ||
249 | |||
250 | retval = i2c_smbus_write_byte_data(client, addr, buf[sent]); | ||
251 | } | ||
252 | } else { | ||
253 | int cmd, val; | ||
254 | |||
255 | /* We are writing to EEPROM */ | ||
256 | for (sent = 0; sent < count; sent++) { | ||
257 | addr = blk->base + off + sent; | ||
258 | cmd = addr >> 8; | ||
259 | val = (addr & 0xff) | (buf[sent] << 8); // reversed | ||
260 | |||
261 | if (addr == 0x8044) | ||
262 | continue; | ||
263 | |||
264 | retval = i2c_smbus_write_word_data(client, cmd, val); | ||
265 | |||
266 | if (retval) { | ||
267 | goto error_exit; | ||
268 | } | ||
269 | |||
270 | /* A write takes up to 11 ms */ | ||
271 | msleep(11); | ||
272 | } | ||
273 | } | ||
274 | |||
275 | /* Invalidate the scratch buffer */ | ||
276 | for (slice = (off >> SLICE_BITS); slice <= ((off + count - 1) >> SLICE_BITS); slice++) | ||
277 | blk->valid &= ~(1 << slice); | ||
278 | |||
279 | error_exit: | ||
280 | up(&data->update_lock); | ||
281 | |||
282 | return sent; | ||
283 | } | ||
284 | |||
285 | static ssize_t max6875_user_write(struct kobject *kobj, char *buf, loff_t off, size_t count) | ||
286 | { | ||
287 | return max6875_write(kobj, buf, off, count, max6875_eeprom_user); | ||
288 | } | ||
289 | |||
290 | static ssize_t max6875_config_write(struct kobject *kobj, char *buf, loff_t off, size_t count) | ||
291 | { | ||
292 | return max6875_write(kobj, buf, off, count, max6875_eeprom_config); | ||
293 | } | ||
294 | |||
295 | static ssize_t max6875_cfgreg_write(struct kobject *kobj, char *buf, loff_t off, size_t count) | ||
296 | { | ||
297 | return max6875_write(kobj, buf, off, count, max6875_register_config); | ||
298 | } | ||
299 | |||
300 | static struct bin_attribute user_eeprom_attr = { | ||
301 | .attr = { | ||
302 | .name = "eeprom_user", | ||
303 | .mode = S_IRUGO | S_IWUSR | S_IWGRP, | ||
304 | .owner = THIS_MODULE, | ||
305 | }, | ||
306 | .size = USER_EEPROM_SIZE, | ||
307 | .read = max6875_user_read, | ||
308 | .write = max6875_user_write, | ||
309 | }; | ||
310 | |||
311 | static struct bin_attribute config_eeprom_attr = { | ||
312 | .attr = { | ||
313 | .name = "eeprom_config", | ||
314 | .mode = S_IRUGO | S_IWUSR, | ||
315 | .owner = THIS_MODULE, | ||
316 | }, | ||
317 | .size = CONFIG_EEPROM_SIZE, | ||
318 | .read = max6875_config_read, | ||
319 | .write = max6875_config_write, | ||
320 | }; | ||
321 | |||
322 | static struct bin_attribute config_register_attr = { | ||
323 | .attr = { | ||
324 | .name = "reg_config", | ||
325 | .mode = S_IRUGO | S_IWUSR, | ||
326 | .owner = THIS_MODULE, | ||
327 | }, | ||
328 | .size = CONFIG_EEPROM_SIZE, | ||
329 | .read = max6875_cfgreg_read, | ||
330 | .write = max6875_cfgreg_write, | ||
331 | }; | ||
332 | |||
333 | static int max6875_attach_adapter(struct i2c_adapter *adapter) | ||
334 | { | ||
335 | return i2c_detect(adapter, &addr_data, max6875_detect); | ||
336 | } | ||
337 | |||
338 | /* This function is called by i2c_detect */ | ||
339 | static int max6875_detect(struct i2c_adapter *adapter, int address, int kind) | ||
340 | { | ||
341 | struct i2c_client *new_client; | ||
342 | struct max6875_data *data; | ||
343 | int err = 0; | ||
344 | |||
345 | /* There are three ways we can read the EEPROM data: | ||
346 | (1) I2C block reads (faster, but unsupported by most adapters) | ||
347 | (2) Consecutive byte reads (100% overhead) | ||
348 | (3) Regular byte data reads (200% overhead) | ||
349 | The third method is not implemented by this driver because all | ||
350 | known adapters support at least the second. */ | ||
351 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA | | ||
352 | I2C_FUNC_SMBUS_BYTE | | ||
353 | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) | ||
354 | goto exit; | ||
355 | |||
356 | /* OK. For now, we presume we have a valid client. We now create the | ||
357 | client structure, even though we cannot fill it completely yet. | ||
358 | But it allows us to access eeprom_{read,write}_value. */ | ||
359 | if (!(data = kmalloc(sizeof(struct max6875_data), GFP_KERNEL))) { | ||
360 | err = -ENOMEM; | ||
361 | goto exit; | ||
362 | } | ||
363 | memset(data, 0, sizeof(struct max6875_data)); | ||
364 | |||
365 | new_client = &data->client; | ||
366 | i2c_set_clientdata(new_client, data); | ||
367 | new_client->addr = address; | ||
368 | new_client->adapter = adapter; | ||
369 | new_client->driver = &max6875_driver; | ||
370 | new_client->flags = 0; | ||
371 | |||
372 | /* Setup the user section */ | ||
373 | data->blocks[max6875_eeprom_user].type = max6875_eeprom_user; | ||
374 | data->blocks[max6875_eeprom_user].slices = USER_EEPROM_SLICES; | ||
375 | data->blocks[max6875_eeprom_user].size = USER_EEPROM_SIZE; | ||
376 | data->blocks[max6875_eeprom_user].base = USER_EEPROM_BASE; | ||
377 | data->blocks[max6875_eeprom_user].data = data->data; | ||
378 | data->blocks[max6875_eeprom_user].updated = data->last_updated; | ||
379 | |||
380 | /* Setup the config section */ | ||
381 | data->blocks[max6875_eeprom_config].type = max6875_eeprom_config; | ||
382 | data->blocks[max6875_eeprom_config].slices = CONFIG_EEPROM_SLICES; | ||
383 | data->blocks[max6875_eeprom_config].size = CONFIG_EEPROM_SIZE; | ||
384 | data->blocks[max6875_eeprom_config].base = CONFIG_EEPROM_BASE; | ||
385 | data->blocks[max6875_eeprom_config].data = &data->data[USER_EEPROM_SIZE]; | ||
386 | data->blocks[max6875_eeprom_config].updated = &data->last_updated[USER_EEPROM_SLICES]; | ||
387 | |||
388 | /* Setup the register section */ | ||
389 | data->blocks[max6875_register_config].type = max6875_register_config; | ||
390 | data->blocks[max6875_register_config].slices = CONFIG_EEPROM_SLICES; | ||
391 | data->blocks[max6875_register_config].size = CONFIG_EEPROM_SIZE; | ||
392 | data->blocks[max6875_register_config].base = 0; | ||
393 | data->blocks[max6875_register_config].data = &data->data[USER_EEPROM_SIZE+CONFIG_EEPROM_SIZE]; | ||
394 | data->blocks[max6875_register_config].updated = &data->last_updated[USER_EEPROM_SLICES+CONFIG_EEPROM_SLICES]; | ||
395 | |||
396 | /* Init the data */ | ||
397 | memset(data->data, 0xff, sizeof(data->data)); | ||
398 | |||
399 | /* Fill in the remaining client fields */ | ||
400 | strlcpy(new_client->name, "max6875", I2C_NAME_SIZE); | ||
401 | init_MUTEX(&data->update_lock); | ||
402 | |||
403 | /* Verify that the chip is really what we think it is */ | ||
404 | if ((max6875_update_slice(new_client, &data->blocks[max6875_eeprom_config], 4) < 0) || | ||
405 | (max6875_update_slice(new_client, &data->blocks[max6875_register_config], 4) < 0)) | ||
406 | goto exit_kfree; | ||
407 | |||
408 | /* 0x41,0x42 must be zero and 0x40 must match in eeprom and registers */ | ||
409 | if ((data->blocks[max6875_eeprom_config].data[0x41] != 0) || | ||
410 | (data->blocks[max6875_eeprom_config].data[0x42] != 0) || | ||
411 | (data->blocks[max6875_register_config].data[0x41] != 0) || | ||
412 | (data->blocks[max6875_register_config].data[0x42] != 0) || | ||
413 | (data->blocks[max6875_eeprom_config].data[0x40] != | ||
414 | data->blocks[max6875_register_config].data[0x40])) | ||
415 | goto exit_kfree; | ||
416 | |||
417 | /* Tell the I2C layer a new client has arrived */ | ||
418 | if ((err = i2c_attach_client(new_client))) | ||
419 | goto exit_kfree; | ||
420 | |||
421 | /* create the sysfs eeprom files with the correct permissions */ | ||
422 | if (allow_write == 0) { | ||
423 | user_eeprom_attr.attr.mode &= ~S_IWUGO; | ||
424 | user_eeprom_attr.write = NULL; | ||
425 | config_eeprom_attr.attr.mode &= ~S_IWUGO; | ||
426 | config_eeprom_attr.write = NULL; | ||
427 | config_register_attr.attr.mode &= ~S_IWUGO; | ||
428 | config_register_attr.write = NULL; | ||
429 | } | ||
430 | sysfs_create_bin_file(&new_client->dev.kobj, &user_eeprom_attr); | ||
431 | sysfs_create_bin_file(&new_client->dev.kobj, &config_eeprom_attr); | ||
432 | sysfs_create_bin_file(&new_client->dev.kobj, &config_register_attr); | ||
433 | |||
434 | return 0; | ||
435 | |||
436 | exit_kfree: | ||
437 | kfree(data); | ||
438 | exit: | ||
439 | return err; | ||
440 | } | ||
441 | |||
442 | static int max6875_detach_client(struct i2c_client *client) | ||
443 | { | ||
444 | int err; | ||
445 | |||
446 | err = i2c_detach_client(client); | ||
447 | if (err) { | ||
448 | dev_err(&client->dev, "Client deregistration failed, client not detached.\n"); | ||
449 | return err; | ||
450 | } | ||
451 | |||
452 | kfree(i2c_get_clientdata(client)); | ||
453 | |||
454 | return 0; | ||
455 | } | ||
456 | |||
457 | static int __init max6875_init(void) | ||
458 | { | ||
459 | return i2c_add_driver(&max6875_driver); | ||
460 | } | ||
461 | |||
462 | static void __exit max6875_exit(void) | ||
463 | { | ||
464 | i2c_del_driver(&max6875_driver); | ||
465 | } | ||
466 | |||
467 | |||
468 | MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>"); | ||
469 | MODULE_DESCRIPTION("MAX6875 driver"); | ||
470 | MODULE_LICENSE("GPL"); | ||
471 | |||
472 | module_init(max6875_init); | ||
473 | module_exit(max6875_exit); | ||