2 * at24.c - handle most I2C EEPROMs
4 * Copyright (C) 2005-2007 David Brownell
5 * Copyright (C) 2008 Wolfram Sang, Pengutronix
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/delay.h>
17 #include <linux/mutex.h>
18 #include <linux/sysfs.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/log2.h>
21 #include <linux/bitops.h>
22 #include <linux/jiffies.h>
23 #include <linux/i2c.h>
24 #include <linux/i2c/at24.h>
27 * I2C EEPROMs from most vendors are inexpensive and mostly interchangeable.
28 * Differences between different vendor product lines (like Atmel AT24C or
29 * MicroChip 24LC, etc) won't much matter for typical read/write access.
30 * There are also I2C RAM chips, likewise interchangeable. One example
31 * would be the PCF8570, which acts like a 24c02 EEPROM (256 bytes).
33 * However, misconfiguration can lose data. "Set 16-bit memory address"
34 * to a part with 8-bit addressing will overwrite data. Writing with too
35 * big a page size also loses data. And it's not safe to assume that the
36 * conventional addresses 0x50..0x57 only hold eeproms; a PCF8563 RTC
37 * uses 0x51, for just one example.
39 * Accordingly, explicit board-specific configuration data should be used
40 * in almost all cases. (One partial exception is an SMBus used to access
41 * "SPD" data for DRAM sticks. Those only use 24c02 EEPROMs.)
43 * So this driver uses "new style" I2C driver binding, expecting to be
44 * told what devices exist. That may be in arch/X/mach-Y/board-Z.c or
45 * similar kernel-resident tables; or, configuration data coming from
48 * Other than binding model, current differences from "eeprom" driver are
49 * that this one handles write access and isn't restricted to 24c02 devices.
50 * It also handles larger devices (32 kbit and up) with two-byte addresses,
51 * which won't work on pure SMBus systems.
55 struct at24_platform_data chip;
59 * Lock protects against activities from other Linux tasks,
60 * but not from changes by other I2C masters.
63 struct bin_attribute bin;
67 unsigned num_addresses;
70 * Some chips tie up multiple I2C addresses; dummy devices reserve
71 * them for us, and we'll use them with SMBus calls.
73 struct i2c_client *client[];
77 * This parameter is to help this driver avoid blocking other drivers out
78 * of I2C for potentially troublesome amounts of time. With a 100 kHz I2C
79 * clock, one 256 byte read takes about 1/43 second which is excessive;
80 * but the 1/170 second it takes at 400 kHz may be quite reasonable; and
81 * at 1 MHz (Fm+) a 1/430 second delay could easily be invisible.
83 * This value is forced to be a power of two so that writes align on pages.
85 static unsigned io_limit = 128;
86 module_param(io_limit, uint, 0);
87 MODULE_PARM_DESC(io_limit, "Maximum bytes per I/O (default 128)");
90 * Specs often allow 5 msec for a page write, sometimes 20 msec;
91 * it's important to recover from write timeouts.
93 static unsigned write_timeout = 25;
94 module_param(write_timeout, uint, 0);
95 MODULE_PARM_DESC(write_timeout, "Time (in ms) to try writes (default 25)");
97 #define AT24_SIZE_BYTELEN 5
98 #define AT24_SIZE_FLAGS 8
100 #define AT24_BITMASK(x) (BIT(x) - 1)
102 /* create non-zero magic value for given eeprom parameters */
103 #define AT24_DEVICE_MAGIC(_len, _flags) \
104 ((1 << AT24_SIZE_FLAGS | (_flags)) \
105 << AT24_SIZE_BYTELEN | ilog2(_len))
107 static const struct i2c_device_id at24_ids[] = {
108 /* needs 8 addresses as A0-A2 are ignored */
109 { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) },
110 /* old variants can't be handled with this generic entry! */
111 { "24c01", AT24_DEVICE_MAGIC(1024 / 8, 0) },
112 { "24c02", AT24_DEVICE_MAGIC(2048 / 8, 0) },
113 /* spd is a 24c02 in memory DIMMs */
114 { "spd", AT24_DEVICE_MAGIC(2048 / 8,
115 AT24_FLAG_READONLY | AT24_FLAG_IRUGO) },
116 { "24c04", AT24_DEVICE_MAGIC(4096 / 8, 0) },
117 /* 24rf08 quirk is handled at i2c-core */
118 { "24c08", AT24_DEVICE_MAGIC(8192 / 8, 0) },
119 { "24c16", AT24_DEVICE_MAGIC(16384 / 8, 0) },
120 { "24c32", AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) },
121 { "24c64", AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) },
122 { "24c128", AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16) },
123 { "24c256", AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16) },
124 { "24c512", AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16) },
125 { "24c1024", AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16) },
127 { /* END OF LIST */ }
129 MODULE_DEVICE_TABLE(i2c, at24_ids);
131 /*-------------------------------------------------------------------------*/
134 * This routine supports chips which consume multiple I2C addresses. It
135 * computes the addressing information to be used for a given r/w request.
136 * Assumes that sanity checks for offset happened at sysfs-layer.
138 static struct i2c_client *at24_translate_offset(struct at24_data *at24,
143 if (at24->chip.flags & AT24_FLAG_ADDR16) {
151 return at24->client[i];
154 static ssize_t at24_eeprom_read(struct at24_data *at24, char *buf,
155 unsigned offset, size_t count)
157 struct i2c_msg msg[2];
159 struct i2c_client *client;
162 memset(msg, 0, sizeof(msg));
165 * REVISIT some multi-address chips don't rollover page reads to
166 * the next slave address, so we may need to truncate the count.
167 * Those chips might need another quirk flag.
169 * If the real hardware used four adjacent 24c02 chips and that
170 * were misconfigured as one 24c08, that would be a similar effect:
171 * one "eeprom" file not four, but larger reads would fail when
172 * they crossed certain pages.
176 * Slave address and byte offset derive from the offset. Always
177 * set the byte address; on a multi-master board, another master
178 * may have changed the chip's "current" address pointer.
180 client = at24_translate_offset(at24, &offset);
182 if (count > io_limit)
185 /* Smaller eeproms can work given some SMBus extension calls */
186 if (at24->use_smbus) {
187 if (count > I2C_SMBUS_BLOCK_MAX)
188 count = I2C_SMBUS_BLOCK_MAX;
189 status = i2c_smbus_read_i2c_block_data(client, offset,
191 dev_dbg(&client->dev, "smbus read %zu@%d --> %d\n",
192 count, offset, status);
193 return (status < 0) ? -EIO : status;
197 * When we have a better choice than SMBus calls, use a combined
198 * I2C message. Write address; then read up to io_limit data bytes.
199 * Note that read page rollover helps us here (unlike writes).
200 * msgbuf is u8 and will cast to our needs.
203 if (at24->chip.flags & AT24_FLAG_ADDR16)
204 msgbuf[i++] = offset >> 8;
205 msgbuf[i++] = offset;
207 msg[0].addr = client->addr;
211 msg[1].addr = client->addr;
212 msg[1].flags = I2C_M_RD;
216 status = i2c_transfer(client->adapter, msg, 2);
217 dev_dbg(&client->dev, "i2c read %zu@%d --> %d\n",
218 count, offset, status);
222 else if (status >= 0)
228 static ssize_t at24_bin_read(struct kobject *kobj, struct bin_attribute *attr,
229 char *buf, loff_t off, size_t count)
231 struct at24_data *at24;
234 at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
236 if (unlikely(!count))
240 * Read data from chip, protecting against concurrent updates
241 * from this host, but not from other I2C masters.
243 mutex_lock(&at24->lock);
248 status = at24_eeprom_read(at24, buf, off, count);
260 mutex_unlock(&at24->lock);
267 * REVISIT: export at24_bin{read,write}() to let other kernel code use
268 * eeprom data. For example, it might hold a board's Ethernet address, or
269 * board-specific calibration data generated on the manufacturing floor.
274 * Note that if the hardware write-protect pin is pulled high, the whole
275 * chip is normally write protected. But there are plenty of product
276 * variants here, including OTP fuses and partial chip protect.
278 * We only use page mode writes; the alternative is sloooow. This routine
279 * writes at most one page.
281 static ssize_t at24_eeprom_write(struct at24_data *at24, char *buf,
282 unsigned offset, size_t count)
284 struct i2c_client *client;
287 unsigned long timeout, write_time;
290 /* Get corresponding I2C address and adjust offset */
291 client = at24_translate_offset(at24, &offset);
293 /* write_max is at most a page */
294 if (count > at24->write_max)
295 count = at24->write_max;
297 /* Never roll over backwards, to the start of this page */
298 next_page = roundup(offset + 1, at24->chip.page_size);
299 if (offset + count > next_page)
300 count = next_page - offset;
302 /* If we'll use I2C calls for I/O, set up the message */
303 if (!at24->use_smbus) {
306 msg.addr = client->addr;
309 /* msg.buf is u8 and casts will mask the values */
310 msg.buf = at24->writebuf;
311 if (at24->chip.flags & AT24_FLAG_ADDR16)
312 msg.buf[i++] = offset >> 8;
314 msg.buf[i++] = offset;
315 memcpy(&msg.buf[i], buf, count);
320 * Writes fail if the previous one didn't complete yet. We may
321 * loop a few times until this one succeeds, waiting at least
322 * long enough for one entire page write to work.
324 timeout = jiffies + msecs_to_jiffies(write_timeout);
326 write_time = jiffies;
327 if (at24->use_smbus) {
328 status = i2c_smbus_write_i2c_block_data(client,
333 status = i2c_transfer(client->adapter, &msg, 1);
337 dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
338 count, offset, status, jiffies);
343 /* REVISIT: at HZ=100, this is sloooow */
345 } while (time_before(write_time, timeout));
350 static ssize_t at24_bin_write(struct kobject *kobj, struct bin_attribute *attr,
351 char *buf, loff_t off, size_t count)
353 struct at24_data *at24;
356 at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
358 if (unlikely(!count))
362 * Write data to chip, protecting against concurrent updates
363 * from this host, but not from other I2C masters.
365 mutex_lock(&at24->lock);
370 status = at24_eeprom_write(at24, buf, off, count);
382 mutex_unlock(&at24->lock);
387 /*-------------------------------------------------------------------------*/
389 static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
391 struct at24_platform_data chip;
393 bool use_smbus = false;
394 struct at24_data *at24;
396 unsigned i, num_addresses;
397 kernel_ulong_t magic;
399 if (client->dev.platform_data) {
400 chip = *(struct at24_platform_data *)client->dev.platform_data;
402 if (!id->driver_data) {
406 magic = id->driver_data;
407 chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN));
408 magic >>= AT24_SIZE_BYTELEN;
409 chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS);
411 * This is slow, but we can't know all eeproms, so we better
412 * play safe. Specifying custom eeprom-types via platform_data
413 * is recommended anyhow.
418 if (!is_power_of_2(chip.byte_len))
419 dev_warn(&client->dev,
420 "byte_len looks suspicious (no power of 2)!\n");
421 if (!is_power_of_2(chip.page_size))
422 dev_warn(&client->dev,
423 "page_size looks suspicious (no power of 2)!\n");
425 /* Use I2C operations unless we're stuck with SMBus extensions. */
426 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
427 if (chip.flags & AT24_FLAG_ADDR16) {
431 if (!i2c_check_functionality(client->adapter,
432 I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
439 if (chip.flags & AT24_FLAG_TAKE8ADDR)
442 num_addresses = DIV_ROUND_UP(chip.byte_len,
443 (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
445 at24 = kzalloc(sizeof(struct at24_data) +
446 num_addresses * sizeof(struct i2c_client *), GFP_KERNEL);
452 mutex_init(&at24->lock);
453 at24->use_smbus = use_smbus;
455 at24->num_addresses = num_addresses;
458 * Export the EEPROM bytes through sysfs, since that's convenient.
459 * By default, only root should see the data (maybe passwords etc)
461 at24->bin.attr.name = "eeprom";
462 at24->bin.attr.mode = chip.flags & AT24_FLAG_IRUGO ? S_IRUGO : S_IRUSR;
463 at24->bin.read = at24_bin_read;
464 at24->bin.size = chip.byte_len;
466 writable = !(chip.flags & AT24_FLAG_READONLY);
468 if (!use_smbus || i2c_check_functionality(client->adapter,
469 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
471 unsigned write_max = chip.page_size;
473 at24->bin.write = at24_bin_write;
474 at24->bin.attr.mode |= S_IWUSR;
476 if (write_max > io_limit)
477 write_max = io_limit;
478 if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX)
479 write_max = I2C_SMBUS_BLOCK_MAX;
480 at24->write_max = write_max;
482 /* buffer (data + address at the beginning) */
483 at24->writebuf = kmalloc(write_max + 2, GFP_KERNEL);
484 if (!at24->writebuf) {
489 dev_warn(&client->dev,
490 "cannot write due to controller restrictions.");
494 at24->client[0] = client;
496 /* use dummy devices for multiple-address chips */
497 for (i = 1; i < num_addresses; i++) {
498 at24->client[i] = i2c_new_dummy(client->adapter,
500 if (!at24->client[i]) {
501 dev_err(&client->dev, "address 0x%02x unavailable\n",
508 err = sysfs_create_bin_file(&client->dev.kobj, &at24->bin);
512 i2c_set_clientdata(client, at24);
514 dev_info(&client->dev, "%zu byte %s EEPROM %s\n",
515 at24->bin.size, client->name,
516 writable ? "(writable)" : "(read-only)");
517 dev_dbg(&client->dev,
518 "page_size %d, num_addresses %d, write_max %d%s\n",
519 chip.page_size, num_addresses,
521 use_smbus ? ", use_smbus" : "");
526 for (i = 1; i < num_addresses; i++)
528 i2c_unregister_device(at24->client[i]);
530 kfree(at24->writebuf);
534 dev_dbg(&client->dev, "probe error %d\n", err);
538 static int __devexit at24_remove(struct i2c_client *client)
540 struct at24_data *at24;
543 at24 = i2c_get_clientdata(client);
544 sysfs_remove_bin_file(&client->dev.kobj, &at24->bin);
546 for (i = 1; i < at24->num_addresses; i++)
547 i2c_unregister_device(at24->client[i]);
549 kfree(at24->writebuf);
551 i2c_set_clientdata(client, NULL);
555 /*-------------------------------------------------------------------------*/
557 static struct i2c_driver at24_driver = {
560 .owner = THIS_MODULE,
563 .remove = __devexit_p(at24_remove),
564 .id_table = at24_ids,
567 static int __init at24_init(void)
569 io_limit = rounddown_pow_of_two(io_limit);
570 return i2c_add_driver(&at24_driver);
572 module_init(at24_init);
574 static void __exit at24_exit(void)
576 i2c_del_driver(&at24_driver);
578 module_exit(at24_exit);
580 MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
581 MODULE_AUTHOR("David Brownell and Wolfram Sang");
582 MODULE_LICENSE("GPL");