2 max6875.c - driver for MAX6874/MAX6875
4 Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
6 Based on i2c/chips/eeprom.c
8 The MAX6875 has two EEPROM sections: config and user.
9 At reset, the config EEPROM is read into the registers.
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
16 In our application, we put device serial & model numbers in user eeprom.
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.
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.
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>
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};
43 /* Insmod parameters */
44 SENSORS_INSMOD_1(max6875);
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"
52 " 1: Read/Write access");
54 /* The MAX6875 can only read/write 16 bytes at a time */
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
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
68 /* MAX6875 commands */
69 #define MAX6875_CMD_BLOCK_WRITE 0x83
70 #define MAX6875_CMD_BLOCK_READ 0x84
71 #define MAX6875_CMD_REBOOT 0x88
73 enum max6875_area_type {
74 max6875_register_config=0,
75 max6875_eeprom_config,
81 enum max6875_area_type type;
86 unsigned long *updated;
90 /* Each client has this additional 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)];
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);
104 /* This is the driver that will be inserted */
105 static struct i2c_driver max6875_driver = {
106 .owner = THIS_MODULE,
108 .flags = I2C_DF_NOTIFY,
109 .attach_adapter = max6875_attach_adapter,
110 .detach_client = max6875_detach_client,
113 static int max6875_update_slice(struct i2c_client *client,
114 struct eeprom_block *blk,
117 struct max6875_data *data = i2c_get_clientdata(client);
118 int i, j, addr, count;
119 u8 rdbuf[SLICE_SIZE];
122 if (slice >= blk->slices)
125 down(&data->update_lock);
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",
133 addr = blk->base + (slice << SLICE_BITS);
134 count = blk->size - (slice << SLICE_BITS);
135 if (count > SLICE_SIZE) {
139 /* Preset the read address */
141 /* select the register */
142 if (i2c_smbus_write_byte(client, addr & 0xFF)) {
143 dev_dbg(&client->dev, "max6875 register select has failed!\n");
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");
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)
164 memcpy(&blk->data[slice << SLICE_BITS], rdbuf, count);
166 for (i = 0; i < count; i++) {
167 j = i2c_smbus_read_byte(client);
173 blk->data[(slice << SLICE_BITS) + i] = (u8) j;
176 blk->updated[slice] = jiffies;
177 blk->valid |= (1 << slice);
180 up(&data->update_lock);
184 static ssize_t max6875_read(struct kobject *kobj, char *buf, loff_t off, size_t count,
185 enum max6875_area_type area_type)
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;
192 blk = &data->blocks[area_type];
196 if (off + count > blk->size)
197 count = blk->size - off;
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);
203 memcpy(buf, &blk->data[off], count);
208 static ssize_t max6875_user_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
210 return max6875_read(kobj, buf, off, count, max6875_eeprom_user);
213 static ssize_t max6875_config_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
215 return max6875_read(kobj, buf, off, count, max6875_eeprom_config);
218 static ssize_t max6875_cfgreg_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
220 return max6875_read(kobj, buf, off, count, max6875_register_config);
224 static ssize_t max6875_write(struct kobject *kobj, char *buf, loff_t off, size_t count,
225 enum max6875_area_type area_type)
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;
233 blk = &data->blocks[area_type];
237 if ((off + count) > blk->size)
238 count = blk->size - off;
240 if (down_interruptible(&data->update_lock))
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++) {
250 retval = i2c_smbus_write_byte_data(client, addr, buf[sent]);
255 /* We are writing to EEPROM */
256 for (sent = 0; sent < count; sent++) {
257 addr = blk->base + off + sent;
259 val = (addr & 0xff) | (buf[sent] << 8); // reversed
264 retval = i2c_smbus_write_word_data(client, cmd, val);
270 /* A write takes up to 11 ms */
275 /* Invalidate the scratch buffer */
276 for (slice = (off >> SLICE_BITS); slice <= ((off + count - 1) >> SLICE_BITS); slice++)
277 blk->valid &= ~(1 << slice);
280 up(&data->update_lock);
285 static ssize_t max6875_user_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
287 return max6875_write(kobj, buf, off, count, max6875_eeprom_user);
290 static ssize_t max6875_config_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
292 return max6875_write(kobj, buf, off, count, max6875_eeprom_config);
295 static ssize_t max6875_cfgreg_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
297 return max6875_write(kobj, buf, off, count, max6875_register_config);
300 static struct bin_attribute user_eeprom_attr = {
302 .name = "eeprom_user",
303 .mode = S_IRUGO | S_IWUSR | S_IWGRP,
304 .owner = THIS_MODULE,
306 .size = USER_EEPROM_SIZE,
307 .read = max6875_user_read,
308 .write = max6875_user_write,
311 static struct bin_attribute config_eeprom_attr = {
313 .name = "eeprom_config",
314 .mode = S_IRUGO | S_IWUSR,
315 .owner = THIS_MODULE,
317 .size = CONFIG_EEPROM_SIZE,
318 .read = max6875_config_read,
319 .write = max6875_config_write,
322 static struct bin_attribute config_register_attr = {
324 .name = "reg_config",
325 .mode = S_IRUGO | S_IWUSR,
326 .owner = THIS_MODULE,
328 .size = CONFIG_EEPROM_SIZE,
329 .read = max6875_cfgreg_read,
330 .write = max6875_cfgreg_write,
333 static int max6875_attach_adapter(struct i2c_adapter *adapter)
335 return i2c_detect(adapter, &addr_data, max6875_detect);
338 /* This function is called by i2c_detect */
339 static int max6875_detect(struct i2c_adapter *adapter, int address, int kind)
341 struct i2c_client *new_client;
342 struct max6875_data *data;
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))
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))) {
363 memset(data, 0, sizeof(struct max6875_data));
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;
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;
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];
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];
397 memset(data->data, 0xff, sizeof(data->data));
399 /* Fill in the remaining client fields */
400 strlcpy(new_client->name, "max6875", I2C_NAME_SIZE);
401 init_MUTEX(&data->update_lock);
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))
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]))
417 /* Tell the I2C layer a new client has arrived */
418 if ((err = i2c_attach_client(new_client)))
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;
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);
442 static int max6875_detach_client(struct i2c_client *client)
446 err = i2c_detach_client(client);
448 dev_err(&client->dev, "Client deregistration failed, client not detached.\n");
452 kfree(i2c_get_clientdata(client));
457 static int __init max6875_init(void)
459 return i2c_add_driver(&max6875_driver);
462 static void __exit max6875_exit(void)
464 i2c_del_driver(&max6875_driver);
468 MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
469 MODULE_DESCRIPTION("MAX6875 driver");
470 MODULE_LICENSE("GPL");
472 module_init(max6875_init);
473 module_exit(max6875_exit);