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