eeprom: at24: check at24_read/write arguments
[linux-2.6/btrfs-unstable.git] / drivers / misc / eeprom / at24.c
blob305a7a464d091614978e37b26a0573f66a27e8b8
1 /*
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/of_device.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
18 #include <linux/mutex.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/property.h>
24 #include <linux/acpi.h>
25 #include <linux/i2c.h>
26 #include <linux/nvmem-provider.h>
27 #include <linux/platform_data/at24.h>
28 #include <linux/pm_runtime.h>
31 * I2C EEPROMs from most vendors are inexpensive and mostly interchangeable.
32 * Differences between different vendor product lines (like Atmel AT24C or
33 * MicroChip 24LC, etc) won't much matter for typical read/write access.
34 * There are also I2C RAM chips, likewise interchangeable. One example
35 * would be the PCF8570, which acts like a 24c02 EEPROM (256 bytes).
37 * However, misconfiguration can lose data. "Set 16-bit memory address"
38 * to a part with 8-bit addressing will overwrite data. Writing with too
39 * big a page size also loses data. And it's not safe to assume that the
40 * conventional addresses 0x50..0x57 only hold eeproms; a PCF8563 RTC
41 * uses 0x51, for just one example.
43 * Accordingly, explicit board-specific configuration data should be used
44 * in almost all cases. (One partial exception is an SMBus used to access
45 * "SPD" data for DRAM sticks. Those only use 24c02 EEPROMs.)
47 * So this driver uses "new style" I2C driver binding, expecting to be
48 * told what devices exist. That may be in arch/X/mach-Y/board-Z.c or
49 * similar kernel-resident tables; or, configuration data coming from
50 * a bootloader.
52 * Other than binding model, current differences from "eeprom" driver are
53 * that this one handles write access and isn't restricted to 24c02 devices.
54 * It also handles larger devices (32 kbit and up) with two-byte addresses,
55 * which won't work on pure SMBus systems.
58 struct at24_data {
59 struct at24_platform_data chip;
60 int use_smbus;
61 int use_smbus_write;
63 ssize_t (*read_func)(struct at24_data *, char *, unsigned int, size_t);
64 ssize_t (*write_func)(struct at24_data *,
65 const char *, unsigned int, size_t);
68 * Lock protects against activities from other Linux tasks,
69 * but not from changes by other I2C masters.
71 struct mutex lock;
73 u8 *writebuf;
74 unsigned write_max;
75 unsigned num_addresses;
77 struct nvmem_config nvmem_config;
78 struct nvmem_device *nvmem;
81 * Some chips tie up multiple I2C addresses; dummy devices reserve
82 * them for us, and we'll use them with SMBus calls.
84 struct i2c_client *client[];
88 * This parameter is to help this driver avoid blocking other drivers out
89 * of I2C for potentially troublesome amounts of time. With a 100 kHz I2C
90 * clock, one 256 byte read takes about 1/43 second which is excessive;
91 * but the 1/170 second it takes at 400 kHz may be quite reasonable; and
92 * at 1 MHz (Fm+) a 1/430 second delay could easily be invisible.
94 * This value is forced to be a power of two so that writes align on pages.
96 static unsigned io_limit = 128;
97 module_param(io_limit, uint, 0);
98 MODULE_PARM_DESC(io_limit, "Maximum bytes per I/O (default 128)");
101 * Specs often allow 5 msec for a page write, sometimes 20 msec;
102 * it's important to recover from write timeouts.
104 static unsigned write_timeout = 25;
105 module_param(write_timeout, uint, 0);
106 MODULE_PARM_DESC(write_timeout, "Time (in ms) to try writes (default 25)");
108 #define AT24_SIZE_BYTELEN 5
109 #define AT24_SIZE_FLAGS 8
111 #define AT24_BITMASK(x) (BIT(x) - 1)
113 /* create non-zero magic value for given eeprom parameters */
114 #define AT24_DEVICE_MAGIC(_len, _flags) \
115 ((1 << AT24_SIZE_FLAGS | (_flags)) \
116 << AT24_SIZE_BYTELEN | ilog2(_len))
119 * Both reads and writes fail if the previous write didn't complete yet. This
120 * macro loops a few times waiting at least long enough for one entire page
121 * write to work while making sure that at least one iteration is run before
122 * checking the break condition.
124 * It takes two parameters: a variable in which the future timeout in jiffies
125 * will be stored and a temporary variable holding the time of the last
126 * iteration of processing the request. Both should be unsigned integers
127 * holding at least 32 bits.
129 #define loop_until_timeout(tout, op_time) \
130 for (tout = jiffies + msecs_to_jiffies(write_timeout), op_time = 0; \
131 op_time ? time_before(op_time, tout) : true; \
132 usleep_range(1000, 1500), op_time = jiffies)
134 static const struct i2c_device_id at24_ids[] = {
135 /* needs 8 addresses as A0-A2 are ignored */
136 { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) },
137 /* old variants can't be handled with this generic entry! */
138 { "24c01", AT24_DEVICE_MAGIC(1024 / 8, 0) },
139 { "24cs01", AT24_DEVICE_MAGIC(16,
140 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
141 { "24c02", AT24_DEVICE_MAGIC(2048 / 8, 0) },
142 { "24cs02", AT24_DEVICE_MAGIC(16,
143 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
144 { "24mac402", AT24_DEVICE_MAGIC(48 / 8,
145 AT24_FLAG_MAC | AT24_FLAG_READONLY) },
146 { "24mac602", AT24_DEVICE_MAGIC(64 / 8,
147 AT24_FLAG_MAC | AT24_FLAG_READONLY) },
148 /* spd is a 24c02 in memory DIMMs */
149 { "spd", AT24_DEVICE_MAGIC(2048 / 8,
150 AT24_FLAG_READONLY | AT24_FLAG_IRUGO) },
151 { "24c04", AT24_DEVICE_MAGIC(4096 / 8, 0) },
152 { "24cs04", AT24_DEVICE_MAGIC(16,
153 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
154 /* 24rf08 quirk is handled at i2c-core */
155 { "24c08", AT24_DEVICE_MAGIC(8192 / 8, 0) },
156 { "24cs08", AT24_DEVICE_MAGIC(16,
157 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
158 { "24c16", AT24_DEVICE_MAGIC(16384 / 8, 0) },
159 { "24cs16", AT24_DEVICE_MAGIC(16,
160 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
161 { "24c32", AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) },
162 { "24cs32", AT24_DEVICE_MAGIC(16,
163 AT24_FLAG_ADDR16 |
164 AT24_FLAG_SERIAL |
165 AT24_FLAG_READONLY) },
166 { "24c64", AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) },
167 { "24cs64", AT24_DEVICE_MAGIC(16,
168 AT24_FLAG_ADDR16 |
169 AT24_FLAG_SERIAL |
170 AT24_FLAG_READONLY) },
171 { "24c128", AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16) },
172 { "24c256", AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16) },
173 { "24c512", AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16) },
174 { "24c1024", AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16) },
175 { "at24", 0 },
176 { /* END OF LIST */ }
178 MODULE_DEVICE_TABLE(i2c, at24_ids);
180 static const struct of_device_id at24_of_match[] = {
182 .compatible = "atmel,24c00",
183 .data = (void *)AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR)
186 .compatible = "atmel,24c01",
187 .data = (void *)AT24_DEVICE_MAGIC(1024 / 8, 0)
190 .compatible = "atmel,24c02",
191 .data = (void *)AT24_DEVICE_MAGIC(2048 / 8, 0)
194 .compatible = "atmel,spd",
195 .data = (void *)AT24_DEVICE_MAGIC(2048 / 8,
196 AT24_FLAG_READONLY | AT24_FLAG_IRUGO)
199 .compatible = "atmel,24c04",
200 .data = (void *)AT24_DEVICE_MAGIC(4096 / 8, 0)
203 .compatible = "atmel,24c08",
204 .data = (void *)AT24_DEVICE_MAGIC(8192 / 8, 0)
207 .compatible = "atmel,24c16",
208 .data = (void *)AT24_DEVICE_MAGIC(16384 / 8, 0)
211 .compatible = "atmel,24c32",
212 .data = (void *)AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16)
215 .compatible = "atmel,24c64",
216 .data = (void *)AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16)
219 .compatible = "atmel,24c128",
220 .data = (void *)AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16)
223 .compatible = "atmel,24c256",
224 .data = (void *)AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16)
227 .compatible = "atmel,24c512",
228 .data = (void *)AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16)
231 .compatible = "atmel,24c1024",
232 .data = (void *)AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16)
234 { },
236 MODULE_DEVICE_TABLE(of, at24_of_match);
238 static const struct acpi_device_id at24_acpi_ids[] = {
239 { "INT3499", AT24_DEVICE_MAGIC(8192 / 8, 0) },
242 MODULE_DEVICE_TABLE(acpi, at24_acpi_ids);
244 /*-------------------------------------------------------------------------*/
247 * This routine supports chips which consume multiple I2C addresses. It
248 * computes the addressing information to be used for a given r/w request.
249 * Assumes that sanity checks for offset happened at sysfs-layer.
251 * Slave address and byte offset derive from the offset. Always
252 * set the byte address; on a multi-master board, another master
253 * may have changed the chip's "current" address pointer.
255 * REVISIT some multi-address chips don't rollover page reads to
256 * the next slave address, so we may need to truncate the count.
257 * Those chips might need another quirk flag.
259 * If the real hardware used four adjacent 24c02 chips and that
260 * were misconfigured as one 24c08, that would be a similar effect:
261 * one "eeprom" file not four, but larger reads would fail when
262 * they crossed certain pages.
264 static struct i2c_client *at24_translate_offset(struct at24_data *at24,
265 unsigned int *offset)
267 unsigned i;
269 if (at24->chip.flags & AT24_FLAG_ADDR16) {
270 i = *offset >> 16;
271 *offset &= 0xffff;
272 } else {
273 i = *offset >> 8;
274 *offset &= 0xff;
277 return at24->client[i];
280 static ssize_t at24_eeprom_read_smbus(struct at24_data *at24, char *buf,
281 unsigned int offset, size_t count)
283 unsigned long timeout, read_time;
284 struct i2c_client *client;
285 int status;
287 client = at24_translate_offset(at24, &offset);
289 if (count > io_limit)
290 count = io_limit;
292 /* Smaller eeproms can work given some SMBus extension calls */
293 if (count > I2C_SMBUS_BLOCK_MAX)
294 count = I2C_SMBUS_BLOCK_MAX;
296 loop_until_timeout(timeout, read_time) {
297 status = i2c_smbus_read_i2c_block_data_or_emulated(client,
298 offset,
299 count, buf);
301 dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
302 count, offset, status, jiffies);
304 if (status == count)
305 return count;
308 return -ETIMEDOUT;
311 static ssize_t at24_eeprom_read_i2c(struct at24_data *at24, char *buf,
312 unsigned int offset, size_t count)
314 unsigned long timeout, read_time;
315 struct i2c_client *client;
316 struct i2c_msg msg[2];
317 int status, i;
318 u8 msgbuf[2];
320 memset(msg, 0, sizeof(msg));
321 client = at24_translate_offset(at24, &offset);
323 if (count > io_limit)
324 count = io_limit;
327 * When we have a better choice than SMBus calls, use a combined I2C
328 * message. Write address; then read up to io_limit data bytes. Note
329 * that read page rollover helps us here (unlike writes). msgbuf is
330 * u8 and will cast to our needs.
332 i = 0;
333 if (at24->chip.flags & AT24_FLAG_ADDR16)
334 msgbuf[i++] = offset >> 8;
335 msgbuf[i++] = offset;
337 msg[0].addr = client->addr;
338 msg[0].buf = msgbuf;
339 msg[0].len = i;
341 msg[1].addr = client->addr;
342 msg[1].flags = I2C_M_RD;
343 msg[1].buf = buf;
344 msg[1].len = count;
346 loop_until_timeout(timeout, read_time) {
347 status = i2c_transfer(client->adapter, msg, 2);
348 if (status == 2)
349 status = count;
351 dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
352 count, offset, status, jiffies);
354 if (status == count)
355 return count;
358 return -ETIMEDOUT;
361 static ssize_t at24_eeprom_read_serial(struct at24_data *at24, char *buf,
362 unsigned int offset, size_t count)
364 unsigned long timeout, read_time;
365 struct i2c_client *client;
366 struct i2c_msg msg[2];
367 u8 addrbuf[2];
368 int status;
370 client = at24_translate_offset(at24, &offset);
372 memset(msg, 0, sizeof(msg));
373 msg[0].addr = client->addr;
374 msg[0].buf = addrbuf;
377 * The address pointer of the device is shared between the regular
378 * EEPROM array and the serial number block. The dummy write (part of
379 * the sequential read protocol) ensures the address pointer is reset
380 * to the desired position.
382 if (at24->chip.flags & AT24_FLAG_ADDR16) {
384 * For 16 bit address pointers, the word address must contain
385 * a '10' sequence in bits 11 and 10 regardless of the
386 * intended position of the address pointer.
388 addrbuf[0] = 0x08;
389 addrbuf[1] = offset;
390 msg[0].len = 2;
391 } else {
393 * Otherwise the word address must begin with a '10' sequence,
394 * regardless of the intended address.
396 addrbuf[0] = 0x80 + offset;
397 msg[0].len = 1;
400 msg[1].addr = client->addr;
401 msg[1].flags = I2C_M_RD;
402 msg[1].buf = buf;
403 msg[1].len = count;
405 loop_until_timeout(timeout, read_time) {
406 status = i2c_transfer(client->adapter, msg, 2);
407 if (status == 2)
408 return count;
411 return -ETIMEDOUT;
414 static ssize_t at24_eeprom_read_mac(struct at24_data *at24, char *buf,
415 unsigned int offset, size_t count)
417 unsigned long timeout, read_time;
418 struct i2c_client *client;
419 struct i2c_msg msg[2];
420 u8 addrbuf[2];
421 int status;
423 client = at24_translate_offset(at24, &offset);
425 memset(msg, 0, sizeof(msg));
426 msg[0].addr = client->addr;
427 msg[0].buf = addrbuf;
428 /* EUI-48 starts from 0x9a, EUI-64 from 0x98 */
429 addrbuf[0] = 0xa0 - at24->chip.byte_len + offset;
430 msg[0].len = 1;
431 msg[1].addr = client->addr;
432 msg[1].flags = I2C_M_RD;
433 msg[1].buf = buf;
434 msg[1].len = count;
436 loop_until_timeout(timeout, read_time) {
437 status = i2c_transfer(client->adapter, msg, 2);
438 if (status == 2)
439 return count;
442 return -ETIMEDOUT;
446 * Note that if the hardware write-protect pin is pulled high, the whole
447 * chip is normally write protected. But there are plenty of product
448 * variants here, including OTP fuses and partial chip protect.
450 * We only use page mode writes; the alternative is sloooow. These routines
451 * write at most one page.
454 static size_t at24_adjust_write_count(struct at24_data *at24,
455 unsigned int offset, size_t count)
457 unsigned next_page;
459 /* write_max is at most a page */
460 if (count > at24->write_max)
461 count = at24->write_max;
463 /* Never roll over backwards, to the start of this page */
464 next_page = roundup(offset + 1, at24->chip.page_size);
465 if (offset + count > next_page)
466 count = next_page - offset;
468 return count;
471 static ssize_t at24_eeprom_write_smbus_block(struct at24_data *at24,
472 const char *buf,
473 unsigned int offset, size_t count)
475 unsigned long timeout, write_time;
476 struct i2c_client *client;
477 ssize_t status = 0;
479 client = at24_translate_offset(at24, &offset);
480 count = at24_adjust_write_count(at24, offset, count);
482 loop_until_timeout(timeout, write_time) {
483 status = i2c_smbus_write_i2c_block_data(client,
484 offset, count, buf);
485 if (status == 0)
486 status = count;
488 dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
489 count, offset, status, jiffies);
491 if (status == count)
492 return count;
495 return -ETIMEDOUT;
498 static ssize_t at24_eeprom_write_smbus_byte(struct at24_data *at24,
499 const char *buf,
500 unsigned int offset, size_t count)
502 unsigned long timeout, write_time;
503 struct i2c_client *client;
504 ssize_t status = 0;
506 client = at24_translate_offset(at24, &offset);
508 loop_until_timeout(timeout, write_time) {
509 status = i2c_smbus_write_byte_data(client, offset, buf[0]);
510 if (status == 0)
511 status = count;
513 dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
514 count, offset, status, jiffies);
516 if (status == count)
517 return count;
520 return -ETIMEDOUT;
523 static ssize_t at24_eeprom_write_i2c(struct at24_data *at24, const char *buf,
524 unsigned int offset, size_t count)
526 unsigned long timeout, write_time;
527 struct i2c_client *client;
528 struct i2c_msg msg;
529 ssize_t status = 0;
530 int i = 0;
532 client = at24_translate_offset(at24, &offset);
533 count = at24_adjust_write_count(at24, offset, count);
535 msg.addr = client->addr;
536 msg.flags = 0;
538 /* msg.buf is u8 and casts will mask the values */
539 msg.buf = at24->writebuf;
540 if (at24->chip.flags & AT24_FLAG_ADDR16)
541 msg.buf[i++] = offset >> 8;
543 msg.buf[i++] = offset;
544 memcpy(&msg.buf[i], buf, count);
545 msg.len = i + count;
547 loop_until_timeout(timeout, write_time) {
548 status = i2c_transfer(client->adapter, &msg, 1);
549 if (status == 1)
550 status = count;
552 dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
553 count, offset, status, jiffies);
555 if (status == count)
556 return count;
559 return -ETIMEDOUT;
562 static int at24_read(void *priv, unsigned int off, void *val, size_t count)
564 struct at24_data *at24 = priv;
565 struct i2c_client *client;
566 char *buf = val;
567 int ret;
569 if (unlikely(!count))
570 return count;
572 if (off + count > at24->chip.byte_len)
573 return -EINVAL;
575 client = at24_translate_offset(at24, &off);
577 ret = pm_runtime_get_sync(&client->dev);
578 if (ret < 0) {
579 pm_runtime_put_noidle(&client->dev);
580 return ret;
584 * Read data from chip, protecting against concurrent updates
585 * from this host, but not from other I2C masters.
587 mutex_lock(&at24->lock);
589 while (count) {
590 int status;
592 status = at24->read_func(at24, buf, off, count);
593 if (status < 0) {
594 mutex_unlock(&at24->lock);
595 pm_runtime_put(&client->dev);
596 return status;
598 buf += status;
599 off += status;
600 count -= status;
603 mutex_unlock(&at24->lock);
605 pm_runtime_put(&client->dev);
607 return 0;
610 static int at24_write(void *priv, unsigned int off, void *val, size_t count)
612 struct at24_data *at24 = priv;
613 struct i2c_client *client;
614 char *buf = val;
615 int ret;
617 if (unlikely(!count))
618 return -EINVAL;
620 if (off + count > at24->chip.byte_len)
621 return -EINVAL;
623 client = at24_translate_offset(at24, &off);
625 ret = pm_runtime_get_sync(&client->dev);
626 if (ret < 0) {
627 pm_runtime_put_noidle(&client->dev);
628 return ret;
632 * Write data to chip, protecting against concurrent updates
633 * from this host, but not from other I2C masters.
635 mutex_lock(&at24->lock);
637 while (count) {
638 int status;
640 status = at24->write_func(at24, buf, off, count);
641 if (status < 0) {
642 mutex_unlock(&at24->lock);
643 pm_runtime_put(&client->dev);
644 return status;
646 buf += status;
647 off += status;
648 count -= status;
651 mutex_unlock(&at24->lock);
653 pm_runtime_put(&client->dev);
655 return 0;
658 static void at24_get_pdata(struct device *dev, struct at24_platform_data *chip)
660 int err;
661 u32 val;
663 if (device_property_present(dev, "read-only"))
664 chip->flags |= AT24_FLAG_READONLY;
666 err = device_property_read_u32(dev, "size", &val);
667 if (!err)
668 chip->byte_len = val;
670 err = device_property_read_u32(dev, "pagesize", &val);
671 if (!err) {
672 chip->page_size = val;
673 } else {
675 * This is slow, but we can't know all eeproms, so we better
676 * play safe. Specifying custom eeprom-types via platform_data
677 * is recommended anyhow.
679 chip->page_size = 1;
683 static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
685 struct at24_platform_data chip;
686 kernel_ulong_t magic = 0;
687 bool writable;
688 int use_smbus = 0;
689 int use_smbus_write = 0;
690 struct at24_data *at24;
691 int err;
692 unsigned i, num_addresses;
693 u8 test_byte;
695 if (client->dev.platform_data) {
696 chip = *(struct at24_platform_data *)client->dev.platform_data;
697 } else {
699 * The I2C core allows OF nodes compatibles to match against the
700 * I2C device ID table as a fallback, so check not only if an OF
701 * node is present but also if it matches an OF device ID entry.
703 if (client->dev.of_node &&
704 of_match_device(at24_of_match, &client->dev)) {
705 magic = (kernel_ulong_t)
706 of_device_get_match_data(&client->dev);
707 } else if (id) {
708 magic = id->driver_data;
709 } else {
710 const struct acpi_device_id *aid;
712 aid = acpi_match_device(at24_acpi_ids, &client->dev);
713 if (aid)
714 magic = aid->driver_data;
716 if (!magic)
717 return -ENODEV;
719 chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN));
720 magic >>= AT24_SIZE_BYTELEN;
721 chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS);
723 at24_get_pdata(&client->dev, &chip);
725 chip.setup = NULL;
726 chip.context = NULL;
729 if (!is_power_of_2(chip.byte_len))
730 dev_warn(&client->dev,
731 "byte_len looks suspicious (no power of 2)!\n");
732 if (!chip.page_size) {
733 dev_err(&client->dev, "page_size must not be 0!\n");
734 return -EINVAL;
736 if (!is_power_of_2(chip.page_size))
737 dev_warn(&client->dev,
738 "page_size looks suspicious (no power of 2)!\n");
741 * REVISIT: the size of the EUI-48 byte array is 6 in at24mac402, while
742 * the call to ilog2() in AT24_DEVICE_MAGIC() rounds it down to 4.
744 * Eventually we'll get rid of the magic values altoghether in favor of
745 * real structs, but for now just manually set the right size.
747 if (chip.flags & AT24_FLAG_MAC && chip.byte_len == 4)
748 chip.byte_len = 6;
750 /* Use I2C operations unless we're stuck with SMBus extensions. */
751 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
752 if (chip.flags & AT24_FLAG_ADDR16)
753 return -EPFNOSUPPORT;
755 if (i2c_check_functionality(client->adapter,
756 I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
757 use_smbus = I2C_SMBUS_I2C_BLOCK_DATA;
758 } else if (i2c_check_functionality(client->adapter,
759 I2C_FUNC_SMBUS_READ_WORD_DATA)) {
760 use_smbus = I2C_SMBUS_WORD_DATA;
761 } else if (i2c_check_functionality(client->adapter,
762 I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
763 use_smbus = I2C_SMBUS_BYTE_DATA;
764 } else {
765 return -EPFNOSUPPORT;
768 if (i2c_check_functionality(client->adapter,
769 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
770 use_smbus_write = I2C_SMBUS_I2C_BLOCK_DATA;
771 } else if (i2c_check_functionality(client->adapter,
772 I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {
773 use_smbus_write = I2C_SMBUS_BYTE_DATA;
774 chip.page_size = 1;
778 if (chip.flags & AT24_FLAG_TAKE8ADDR)
779 num_addresses = 8;
780 else
781 num_addresses = DIV_ROUND_UP(chip.byte_len,
782 (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
784 at24 = devm_kzalloc(&client->dev, sizeof(struct at24_data) +
785 num_addresses * sizeof(struct i2c_client *), GFP_KERNEL);
786 if (!at24)
787 return -ENOMEM;
789 mutex_init(&at24->lock);
790 at24->use_smbus = use_smbus;
791 at24->use_smbus_write = use_smbus_write;
792 at24->chip = chip;
793 at24->num_addresses = num_addresses;
795 if ((chip.flags & AT24_FLAG_SERIAL) && (chip.flags & AT24_FLAG_MAC)) {
796 dev_err(&client->dev,
797 "invalid device data - cannot have both AT24_FLAG_SERIAL & AT24_FLAG_MAC.");
798 return -EINVAL;
801 if (chip.flags & AT24_FLAG_SERIAL) {
802 at24->read_func = at24_eeprom_read_serial;
803 } else if (chip.flags & AT24_FLAG_MAC) {
804 at24->read_func = at24_eeprom_read_mac;
805 } else {
806 at24->read_func = at24->use_smbus ? at24_eeprom_read_smbus
807 : at24_eeprom_read_i2c;
810 if (at24->use_smbus) {
811 if (at24->use_smbus_write == I2C_SMBUS_I2C_BLOCK_DATA)
812 at24->write_func = at24_eeprom_write_smbus_block;
813 else
814 at24->write_func = at24_eeprom_write_smbus_byte;
815 } else {
816 at24->write_func = at24_eeprom_write_i2c;
819 writable = !(chip.flags & AT24_FLAG_READONLY);
820 if (writable) {
821 if (!use_smbus || use_smbus_write) {
823 unsigned write_max = chip.page_size;
825 if (write_max > io_limit)
826 write_max = io_limit;
827 if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX)
828 write_max = I2C_SMBUS_BLOCK_MAX;
829 at24->write_max = write_max;
831 /* buffer (data + address at the beginning) */
832 at24->writebuf = devm_kzalloc(&client->dev,
833 write_max + 2, GFP_KERNEL);
834 if (!at24->writebuf)
835 return -ENOMEM;
836 } else {
837 dev_warn(&client->dev,
838 "cannot write due to controller restrictions.");
842 at24->client[0] = client;
844 /* use dummy devices for multiple-address chips */
845 for (i = 1; i < num_addresses; i++) {
846 at24->client[i] = i2c_new_dummy(client->adapter,
847 client->addr + i);
848 if (!at24->client[i]) {
849 dev_err(&client->dev, "address 0x%02x unavailable\n",
850 client->addr + i);
851 err = -EADDRINUSE;
852 goto err_clients;
856 i2c_set_clientdata(client, at24);
858 /* enable runtime pm */
859 pm_runtime_set_active(&client->dev);
860 pm_runtime_enable(&client->dev);
863 * Perform a one-byte test read to verify that the
864 * chip is functional.
866 err = at24_read(at24, 0, &test_byte, 1);
867 pm_runtime_idle(&client->dev);
868 if (err) {
869 err = -ENODEV;
870 goto err_clients;
873 at24->nvmem_config.name = dev_name(&client->dev);
874 at24->nvmem_config.dev = &client->dev;
875 at24->nvmem_config.read_only = !writable;
876 at24->nvmem_config.root_only = true;
877 at24->nvmem_config.owner = THIS_MODULE;
878 at24->nvmem_config.compat = true;
879 at24->nvmem_config.base_dev = &client->dev;
880 at24->nvmem_config.reg_read = at24_read;
881 at24->nvmem_config.reg_write = at24_write;
882 at24->nvmem_config.priv = at24;
883 at24->nvmem_config.stride = 4;
884 at24->nvmem_config.word_size = 1;
885 at24->nvmem_config.size = chip.byte_len;
887 at24->nvmem = nvmem_register(&at24->nvmem_config);
889 if (IS_ERR(at24->nvmem)) {
890 err = PTR_ERR(at24->nvmem);
891 goto err_clients;
894 dev_info(&client->dev, "%u byte %s EEPROM, %s, %u bytes/write\n",
895 chip.byte_len, client->name,
896 writable ? "writable" : "read-only", at24->write_max);
897 if (use_smbus == I2C_SMBUS_WORD_DATA ||
898 use_smbus == I2C_SMBUS_BYTE_DATA) {
899 dev_notice(&client->dev, "Falling back to %s reads, "
900 "performance will suffer\n", use_smbus ==
901 I2C_SMBUS_WORD_DATA ? "word" : "byte");
904 /* export data to kernel code */
905 if (chip.setup)
906 chip.setup(at24->nvmem, chip.context);
908 return 0;
910 err_clients:
911 for (i = 1; i < num_addresses; i++)
912 if (at24->client[i])
913 i2c_unregister_device(at24->client[i]);
915 pm_runtime_disable(&client->dev);
917 return err;
920 static int at24_remove(struct i2c_client *client)
922 struct at24_data *at24;
923 int i;
925 at24 = i2c_get_clientdata(client);
927 nvmem_unregister(at24->nvmem);
929 for (i = 1; i < at24->num_addresses; i++)
930 i2c_unregister_device(at24->client[i]);
932 pm_runtime_disable(&client->dev);
933 pm_runtime_set_suspended(&client->dev);
935 return 0;
938 /*-------------------------------------------------------------------------*/
940 static struct i2c_driver at24_driver = {
941 .driver = {
942 .name = "at24",
943 .of_match_table = at24_of_match,
944 .acpi_match_table = ACPI_PTR(at24_acpi_ids),
946 .probe = at24_probe,
947 .remove = at24_remove,
948 .id_table = at24_ids,
951 static int __init at24_init(void)
953 if (!io_limit) {
954 pr_err("at24: io_limit must not be 0!\n");
955 return -EINVAL;
958 io_limit = rounddown_pow_of_two(io_limit);
959 return i2c_add_driver(&at24_driver);
961 module_init(at24_init);
963 static void __exit at24_exit(void)
965 i2c_del_driver(&at24_driver);
967 module_exit(at24_exit);
969 MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
970 MODULE_AUTHOR("David Brownell and Wolfram Sang");
971 MODULE_LICENSE("GPL");