12 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
21 #include <linux/bitops.h>
24 #include <linux/i2c.h>
95 static unsigned write_timeout = 25;
99 #define AT24_SIZE_BYTELEN 5
100 #define AT24_SIZE_FLAGS 8
102 #define AT24_BITMASK(x) (BIT(x) - 1)
105 #define AT24_DEVICE_MAGIC(_len, _flags) \
106 ((1 << AT24_SIZE_FLAGS | (_flags)) \
107 << AT24_SIZE_BYTELEN | ilog2(_len))
157 unsigned offset,
size_t count)
162 unsigned long timeout, read_time;
183 client = at24_translate_offset(at24, &offset);
210 msgbuf[i++] = offset >> 8;
239 buf[0] = status & 0xff;
240 buf[1] = status >> 8;
256 dev_dbg(&client->
dev,
"read %zu@%d --> %d (%ld)\n",
257 count, offset, status, jiffies);
270 char *buf, loff_t off,
size_t count)
286 status = at24_eeprom_read(at24, buf, off, count);
305 char *buf, loff_t off,
size_t count)
310 return at24_read(at24, buf, off, count);
323 unsigned offset,
size_t count)
328 unsigned long timeout, write_time;
332 client = at24_translate_offset(at24, &offset);
339 next_page =
roundup(offset + 1, at24->
chip.page_size);
340 if (offset + count > next_page)
341 count = next_page -
offset;
353 msg.buf[i++] = offset >> 8;
378 dev_dbg(&client->
dev,
"write %zu@%d --> %zd (%ld)\n",
379 count, offset, status, jiffies);
391 static ssize_t at24_write(
struct at24_data *at24,
const char *buf, loff_t off,
408 status = at24_eeprom_write(at24, buf, off, count);
427 char *buf, loff_t off,
size_t count)
432 return at24_write(at24, buf, off, count);
444 off_t offset,
size_t count)
448 return at24_read(at24, buf, offset, count);
452 off_t offset,
size_t count)
456 return at24_write(at24, buf, offset, count);
462 static void at24_get_ofdata(
struct i2c_client *client,
477 static void at24_get_ofdata(
struct i2c_client *client,
492 if (client->
dev.platform_data) {
495 if (!id->driver_data) {
499 magic =
id->driver_data;
511 at24_get_ofdata(client, &chip);
519 "byte_len looks suspicious (no power of 2)!\n");
521 dev_err(&client->
dev,
"page_size must not be 0!\n");
527 "page_size looks suspicious (no power of 2)!\n");
535 if (i2c_check_functionality(client->
adapter,
538 }
else if (i2c_check_functionality(client->
adapter,
541 }
else if (i2c_check_functionality(client->
adapter,
556 at24 = kzalloc(
sizeof(
struct at24_data) +
573 at24->
bin.attr.name =
"eeprom";
575 at24->
bin.read = at24_bin_read;
578 at24->
macc.read = at24_macc_read;
582 if (!use_smbus || i2c_check_functionality(client->
adapter,
587 at24->
macc.write = at24_macc_write;
589 at24->
bin.write = at24_bin_write;
606 "cannot write due to controller restrictions.");
613 for (i = 1; i < num_addresses; i++) {
617 dev_err(&client->
dev,
"address 0x%02x unavailable\n",
628 i2c_set_clientdata(client, at24);
630 dev_info(&client->
dev,
"%zu byte %s EEPROM, %s, %u bytes/write\n",
632 writable ?
"writable" :
"read-only", at24->
write_max);
635 dev_notice(&client->
dev,
"Falling back to %s reads, "
636 "performance will suffer\n", use_smbus ==
647 for (i = 1; i < num_addresses; i++)
655 dev_dbg(&client->
dev,
"probe error %d\n", err);
664 at24 = i2c_get_clientdata(client);
684 .id_table = at24_ids,
687 static int __init at24_init(
void)
690 pr_err(
"at24: io_limit must not be 0!\n");
695 return i2c_add_driver(&at24_driver);
699 static void __exit at24_exit(
void)