at24.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * at24.c - handle most I2C EEPROMs
  3. *
  4. * Copyright (C) 2005-2007 David Brownell
  5. * Copyright (C) 2008 Wolfram Sang, Pengutronix
  6. *
  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.
  11. */
  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/of.h>
  24. #include <linux/acpi.h>
  25. #include <linux/i2c.h>
  26. #include <linux/platform_data/at24.h>
  27. /*
  28. * I2C EEPROMs from most vendors are inexpensive and mostly interchangeable.
  29. * Differences between different vendor product lines (like Atmel AT24C or
  30. * MicroChip 24LC, etc) won't much matter for typical read/write access.
  31. * There are also I2C RAM chips, likewise interchangeable. One example
  32. * would be the PCF8570, which acts like a 24c02 EEPROM (256 bytes).
  33. *
  34. * However, misconfiguration can lose data. "Set 16-bit memory address"
  35. * to a part with 8-bit addressing will overwrite data. Writing with too
  36. * big a page size also loses data. And it's not safe to assume that the
  37. * conventional addresses 0x50..0x57 only hold eeproms; a PCF8563 RTC
  38. * uses 0x51, for just one example.
  39. *
  40. * Accordingly, explicit board-specific configuration data should be used
  41. * in almost all cases. (One partial exception is an SMBus used to access
  42. * "SPD" data for DRAM sticks. Those only use 24c02 EEPROMs.)
  43. *
  44. * So this driver uses "new style" I2C driver binding, expecting to be
  45. * told what devices exist. That may be in arch/X/mach-Y/board-Z.c or
  46. * similar kernel-resident tables; or, configuration data coming from
  47. * a bootloader.
  48. *
  49. * Other than binding model, current differences from "eeprom" driver are
  50. * that this one handles write access and isn't restricted to 24c02 devices.
  51. * It also handles larger devices (32 kbit and up) with two-byte addresses,
  52. * which won't work on pure SMBus systems.
  53. */
  54. struct at24_data {
  55. struct at24_platform_data chip;
  56. struct memory_accessor macc;
  57. int use_smbus;
  58. int use_smbus_write;
  59. /*
  60. * Lock protects against activities from other Linux tasks,
  61. * but not from changes by other I2C masters.
  62. */
  63. struct mutex lock;
  64. struct bin_attribute bin;
  65. u8 *writebuf;
  66. unsigned write_max;
  67. unsigned num_addresses;
  68. /*
  69. * Some chips tie up multiple I2C addresses; dummy devices reserve
  70. * them for us, and we'll use them with SMBus calls.
  71. */
  72. struct i2c_client *client[];
  73. };
  74. /*
  75. * This parameter is to help this driver avoid blocking other drivers out
  76. * of I2C for potentially troublesome amounts of time. With a 100 kHz I2C
  77. * clock, one 256 byte read takes about 1/43 second which is excessive;
  78. * but the 1/170 second it takes at 400 kHz may be quite reasonable; and
  79. * at 1 MHz (Fm+) a 1/430 second delay could easily be invisible.
  80. *
  81. * This value is forced to be a power of two so that writes align on pages.
  82. */
  83. static unsigned io_limit = 128;
  84. module_param(io_limit, uint, 0);
  85. MODULE_PARM_DESC(io_limit, "Maximum bytes per I/O (default 128)");
  86. /*
  87. * Specs often allow 5 msec for a page write, sometimes 20 msec;
  88. * it's important to recover from write timeouts.
  89. */
  90. static unsigned write_timeout = 25;
  91. module_param(write_timeout, uint, 0);
  92. MODULE_PARM_DESC(write_timeout, "Time (in ms) to try writes (default 25)");
  93. #define AT24_SIZE_BYTELEN 5
  94. #define AT24_SIZE_FLAGS 8
  95. #define AT24_BITMASK(x) (BIT(x) - 1)
  96. /* create non-zero magic value for given eeprom parameters */
  97. #define AT24_DEVICE_MAGIC(_len, _flags) \
  98. ((1 << AT24_SIZE_FLAGS | (_flags)) \
  99. << AT24_SIZE_BYTELEN | ilog2(_len))
  100. static const struct i2c_device_id at24_ids[] = {
  101. /* needs 8 addresses as A0-A2 are ignored */
  102. { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) },
  103. /* old variants can't be handled with this generic entry! */
  104. { "24c01", AT24_DEVICE_MAGIC(1024 / 8, 0) },
  105. { "24c02", AT24_DEVICE_MAGIC(2048 / 8, 0) },
  106. /* spd is a 24c02 in memory DIMMs */
  107. { "spd", AT24_DEVICE_MAGIC(2048 / 8,
  108. AT24_FLAG_READONLY | AT24_FLAG_IRUGO) },
  109. { "24c04", AT24_DEVICE_MAGIC(4096 / 8, 0) },
  110. /* 24rf08 quirk is handled at i2c-core */
  111. { "24c08", AT24_DEVICE_MAGIC(8192 / 8, 0) },
  112. { "24c16", AT24_DEVICE_MAGIC(16384 / 8, 0) },
  113. { "24c32", AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) },
  114. { "24c64", AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) },
  115. { "24c128", AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16) },
  116. { "24c256", AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16) },
  117. { "24c512", AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16) },
  118. { "24c1024", AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16) },
  119. { "at24", 0 },
  120. { /* END OF LIST */ }
  121. };
  122. MODULE_DEVICE_TABLE(i2c, at24_ids);
  123. static const struct acpi_device_id at24_acpi_ids[] = {
  124. { "INT3499", AT24_DEVICE_MAGIC(8192 / 8, 0) },
  125. { }
  126. };
  127. MODULE_DEVICE_TABLE(acpi, at24_acpi_ids);
  128. /*-------------------------------------------------------------------------*/
  129. /*
  130. * This routine supports chips which consume multiple I2C addresses. It
  131. * computes the addressing information to be used for a given r/w request.
  132. * Assumes that sanity checks for offset happened at sysfs-layer.
  133. */
  134. static struct i2c_client *at24_translate_offset(struct at24_data *at24,
  135. unsigned *offset)
  136. {
  137. unsigned i;
  138. if (at24->chip.flags & AT24_FLAG_ADDR16) {
  139. i = *offset >> 16;
  140. *offset &= 0xffff;
  141. } else {
  142. i = *offset >> 8;
  143. *offset &= 0xff;
  144. }
  145. return at24->client[i];
  146. }
  147. static ssize_t at24_eeprom_read(struct at24_data *at24, char *buf,
  148. unsigned offset, size_t count)
  149. {
  150. struct i2c_msg msg[2];
  151. u8 msgbuf[2];
  152. struct i2c_client *client;
  153. unsigned long timeout, read_time;
  154. int status, i;
  155. memset(msg, 0, sizeof(msg));
  156. /*
  157. * REVISIT some multi-address chips don't rollover page reads to
  158. * the next slave address, so we may need to truncate the count.
  159. * Those chips might need another quirk flag.
  160. *
  161. * If the real hardware used four adjacent 24c02 chips and that
  162. * were misconfigured as one 24c08, that would be a similar effect:
  163. * one "eeprom" file not four, but larger reads would fail when
  164. * they crossed certain pages.
  165. */
  166. /*
  167. * Slave address and byte offset derive from the offset. Always
  168. * set the byte address; on a multi-master board, another master
  169. * may have changed the chip's "current" address pointer.
  170. */
  171. client = at24_translate_offset(at24, &offset);
  172. if (count > io_limit)
  173. count = io_limit;
  174. if (at24->use_smbus) {
  175. /* Smaller eeproms can work given some SMBus extension calls */
  176. if (count > I2C_SMBUS_BLOCK_MAX)
  177. count = I2C_SMBUS_BLOCK_MAX;
  178. } else {
  179. /*
  180. * When we have a better choice than SMBus calls, use a
  181. * combined I2C message. Write address; then read up to
  182. * io_limit data bytes. Note that read page rollover helps us
  183. * here (unlike writes). msgbuf is u8 and will cast to our
  184. * needs.
  185. */
  186. i = 0;
  187. if (at24->chip.flags & AT24_FLAG_ADDR16)
  188. msgbuf[i++] = offset >> 8;
  189. msgbuf[i++] = offset;
  190. msg[0].addr = client->addr;
  191. msg[0].buf = msgbuf;
  192. msg[0].len = i;
  193. msg[1].addr = client->addr;
  194. msg[1].flags = I2C_M_RD;
  195. msg[1].buf = buf;
  196. msg[1].len = count;
  197. }
  198. /*
  199. * Reads fail if the previous write didn't complete yet. We may
  200. * loop a few times until this one succeeds, waiting at least
  201. * long enough for one entire page write to work.
  202. */
  203. timeout = jiffies + msecs_to_jiffies(write_timeout);
  204. do {
  205. read_time = jiffies;
  206. if (at24->use_smbus) {
  207. status = i2c_smbus_read_i2c_block_data_or_emulated(client, offset,
  208. count, buf);
  209. } else {
  210. status = i2c_transfer(client->adapter, msg, 2);
  211. if (status == 2)
  212. status = count;
  213. }
  214. dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
  215. count, offset, status, jiffies);
  216. if (status == count)
  217. return count;
  218. /* REVISIT: at HZ=100, this is sloooow */
  219. msleep(1);
  220. } while (time_before(read_time, timeout));
  221. return -ETIMEDOUT;
  222. }
  223. static ssize_t at24_read(struct at24_data *at24,
  224. char *buf, loff_t off, size_t count)
  225. {
  226. ssize_t retval = 0;
  227. if (unlikely(!count))
  228. return count;
  229. if (off + count > at24->chip.byte_len)
  230. return -EINVAL;
  231. /*
  232. * Read data from chip, protecting against concurrent updates
  233. * from this host, but not from other I2C masters.
  234. */
  235. mutex_lock(&at24->lock);
  236. while (count) {
  237. ssize_t status;
  238. status = at24_eeprom_read(at24, buf, off, count);
  239. if (status <= 0) {
  240. if (retval == 0)
  241. retval = status;
  242. break;
  243. }
  244. buf += status;
  245. off += status;
  246. count -= status;
  247. retval += status;
  248. }
  249. mutex_unlock(&at24->lock);
  250. return retval;
  251. }
  252. static ssize_t at24_bin_read(struct file *filp, struct kobject *kobj,
  253. struct bin_attribute *attr,
  254. char *buf, loff_t off, size_t count)
  255. {
  256. struct at24_data *at24;
  257. at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
  258. return at24_read(at24, buf, off, count);
  259. }
  260. /*
  261. * Note that if the hardware write-protect pin is pulled high, the whole
  262. * chip is normally write protected. But there are plenty of product
  263. * variants here, including OTP fuses and partial chip protect.
  264. *
  265. * We only use page mode writes; the alternative is sloooow. This routine
  266. * writes at most one page.
  267. */
  268. static ssize_t at24_eeprom_write(struct at24_data *at24, const char *buf,
  269. unsigned offset, size_t count)
  270. {
  271. struct i2c_client *client;
  272. struct i2c_msg msg;
  273. ssize_t status = 0;
  274. unsigned long timeout, write_time;
  275. unsigned next_page;
  276. if (offset + count > at24->chip.byte_len)
  277. return -EINVAL;
  278. /* Get corresponding I2C address and adjust offset */
  279. client = at24_translate_offset(at24, &offset);
  280. /* write_max is at most a page */
  281. if (count > at24->write_max)
  282. count = at24->write_max;
  283. /* Never roll over backwards, to the start of this page */
  284. next_page = roundup(offset + 1, at24->chip.page_size);
  285. if (offset + count > next_page)
  286. count = next_page - offset;
  287. /* If we'll use I2C calls for I/O, set up the message */
  288. if (!at24->use_smbus) {
  289. int i = 0;
  290. msg.addr = client->addr;
  291. msg.flags = 0;
  292. /* msg.buf is u8 and casts will mask the values */
  293. msg.buf = at24->writebuf;
  294. if (at24->chip.flags & AT24_FLAG_ADDR16)
  295. msg.buf[i++] = offset >> 8;
  296. msg.buf[i++] = offset;
  297. memcpy(&msg.buf[i], buf, count);
  298. msg.len = i + count;
  299. }
  300. /*
  301. * Writes fail if the previous one didn't complete yet. We may
  302. * loop a few times until this one succeeds, waiting at least
  303. * long enough for one entire page write to work.
  304. */
  305. timeout = jiffies + msecs_to_jiffies(write_timeout);
  306. do {
  307. write_time = jiffies;
  308. if (at24->use_smbus_write) {
  309. switch (at24->use_smbus_write) {
  310. case I2C_SMBUS_I2C_BLOCK_DATA:
  311. status = i2c_smbus_write_i2c_block_data(client,
  312. offset, count, buf);
  313. break;
  314. case I2C_SMBUS_BYTE_DATA:
  315. status = i2c_smbus_write_byte_data(client,
  316. offset, buf[0]);
  317. break;
  318. }
  319. if (status == 0)
  320. status = count;
  321. } else {
  322. status = i2c_transfer(client->adapter, &msg, 1);
  323. if (status == 1)
  324. status = count;
  325. }
  326. dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
  327. count, offset, status, jiffies);
  328. if (status == count)
  329. return count;
  330. /* REVISIT: at HZ=100, this is sloooow */
  331. msleep(1);
  332. } while (time_before(write_time, timeout));
  333. return -ETIMEDOUT;
  334. }
  335. static ssize_t at24_write(struct at24_data *at24, const char *buf, loff_t off,
  336. size_t count)
  337. {
  338. ssize_t retval = 0;
  339. if (unlikely(!count))
  340. return count;
  341. /*
  342. * Write data to chip, protecting against concurrent updates
  343. * from this host, but not from other I2C masters.
  344. */
  345. mutex_lock(&at24->lock);
  346. while (count) {
  347. ssize_t status;
  348. status = at24_eeprom_write(at24, buf, off, count);
  349. if (status <= 0) {
  350. if (retval == 0)
  351. retval = status;
  352. break;
  353. }
  354. buf += status;
  355. off += status;
  356. count -= status;
  357. retval += status;
  358. }
  359. mutex_unlock(&at24->lock);
  360. return retval;
  361. }
  362. static ssize_t at24_bin_write(struct file *filp, struct kobject *kobj,
  363. struct bin_attribute *attr,
  364. char *buf, loff_t off, size_t count)
  365. {
  366. struct at24_data *at24;
  367. at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
  368. return at24_write(at24, buf, off, count);
  369. }
  370. /*-------------------------------------------------------------------------*/
  371. /*
  372. * This lets other kernel code access the eeprom data. For example, it
  373. * might hold a board's Ethernet address, or board-specific calibration
  374. * data generated on the manufacturing floor.
  375. */
  376. static ssize_t at24_macc_read(struct memory_accessor *macc, char *buf,
  377. off_t offset, size_t count)
  378. {
  379. struct at24_data *at24 = container_of(macc, struct at24_data, macc);
  380. return at24_read(at24, buf, offset, count);
  381. }
  382. static ssize_t at24_macc_write(struct memory_accessor *macc, const char *buf,
  383. off_t offset, size_t count)
  384. {
  385. struct at24_data *at24 = container_of(macc, struct at24_data, macc);
  386. return at24_write(at24, buf, offset, count);
  387. }
  388. /*-------------------------------------------------------------------------*/
  389. #ifdef CONFIG_OF
  390. static void at24_get_ofdata(struct i2c_client *client,
  391. struct at24_platform_data *chip)
  392. {
  393. const __be32 *val;
  394. struct device_node *node = client->dev.of_node;
  395. if (node) {
  396. if (of_get_property(node, "read-only", NULL))
  397. chip->flags |= AT24_FLAG_READONLY;
  398. val = of_get_property(node, "pagesize", NULL);
  399. if (val)
  400. chip->page_size = be32_to_cpup(val);
  401. }
  402. }
  403. #else
  404. static void at24_get_ofdata(struct i2c_client *client,
  405. struct at24_platform_data *chip)
  406. { }
  407. #endif /* CONFIG_OF */
  408. static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
  409. {
  410. struct at24_platform_data chip;
  411. kernel_ulong_t magic = 0;
  412. bool writable;
  413. int use_smbus = 0;
  414. int use_smbus_write = 0;
  415. struct at24_data *at24;
  416. int err;
  417. unsigned i, num_addresses;
  418. if (client->dev.platform_data) {
  419. chip = *(struct at24_platform_data *)client->dev.platform_data;
  420. } else {
  421. if (id) {
  422. magic = id->driver_data;
  423. } else {
  424. const struct acpi_device_id *aid;
  425. aid = acpi_match_device(at24_acpi_ids, &client->dev);
  426. if (aid)
  427. magic = aid->driver_data;
  428. }
  429. if (!magic)
  430. return -ENODEV;
  431. chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN));
  432. magic >>= AT24_SIZE_BYTELEN;
  433. chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS);
  434. /*
  435. * This is slow, but we can't know all eeproms, so we better
  436. * play safe. Specifying custom eeprom-types via platform_data
  437. * is recommended anyhow.
  438. */
  439. chip.page_size = 1;
  440. /* update chipdata if OF is present */
  441. at24_get_ofdata(client, &chip);
  442. chip.setup = NULL;
  443. chip.context = NULL;
  444. }
  445. if (!is_power_of_2(chip.byte_len))
  446. dev_warn(&client->dev,
  447. "byte_len looks suspicious (no power of 2)!\n");
  448. if (!chip.page_size) {
  449. dev_err(&client->dev, "page_size must not be 0!\n");
  450. return -EINVAL;
  451. }
  452. if (!is_power_of_2(chip.page_size))
  453. dev_warn(&client->dev,
  454. "page_size looks suspicious (no power of 2)!\n");
  455. /* Use I2C operations unless we're stuck with SMBus extensions. */
  456. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  457. if (chip.flags & AT24_FLAG_ADDR16)
  458. return -EPFNOSUPPORT;
  459. if (i2c_check_functionality(client->adapter,
  460. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  461. use_smbus = I2C_SMBUS_I2C_BLOCK_DATA;
  462. } else if (i2c_check_functionality(client->adapter,
  463. I2C_FUNC_SMBUS_READ_WORD_DATA)) {
  464. use_smbus = I2C_SMBUS_WORD_DATA;
  465. } else if (i2c_check_functionality(client->adapter,
  466. I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
  467. use_smbus = I2C_SMBUS_BYTE_DATA;
  468. } else {
  469. return -EPFNOSUPPORT;
  470. }
  471. }
  472. /* Use I2C operations unless we're stuck with SMBus extensions. */
  473. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  474. if (i2c_check_functionality(client->adapter,
  475. I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
  476. use_smbus_write = I2C_SMBUS_I2C_BLOCK_DATA;
  477. } else if (i2c_check_functionality(client->adapter,
  478. I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {
  479. use_smbus_write = I2C_SMBUS_BYTE_DATA;
  480. chip.page_size = 1;
  481. }
  482. }
  483. if (chip.flags & AT24_FLAG_TAKE8ADDR)
  484. num_addresses = 8;
  485. else
  486. num_addresses = DIV_ROUND_UP(chip.byte_len,
  487. (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
  488. at24 = devm_kzalloc(&client->dev, sizeof(struct at24_data) +
  489. num_addresses * sizeof(struct i2c_client *), GFP_KERNEL);
  490. if (!at24)
  491. return -ENOMEM;
  492. mutex_init(&at24->lock);
  493. at24->use_smbus = use_smbus;
  494. at24->use_smbus_write = use_smbus_write;
  495. at24->chip = chip;
  496. at24->num_addresses = num_addresses;
  497. /*
  498. * Export the EEPROM bytes through sysfs, since that's convenient.
  499. * By default, only root should see the data (maybe passwords etc)
  500. */
  501. sysfs_bin_attr_init(&at24->bin);
  502. at24->bin.attr.name = "eeprom";
  503. at24->bin.attr.mode = chip.flags & AT24_FLAG_IRUGO ? S_IRUGO : S_IRUSR;
  504. at24->bin.read = at24_bin_read;
  505. at24->bin.size = chip.byte_len;
  506. at24->macc.read = at24_macc_read;
  507. writable = !(chip.flags & AT24_FLAG_READONLY);
  508. if (writable) {
  509. if (!use_smbus || use_smbus_write) {
  510. unsigned write_max = chip.page_size;
  511. at24->macc.write = at24_macc_write;
  512. at24->bin.write = at24_bin_write;
  513. at24->bin.attr.mode |= S_IWUSR;
  514. if (write_max > io_limit)
  515. write_max = io_limit;
  516. if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX)
  517. write_max = I2C_SMBUS_BLOCK_MAX;
  518. at24->write_max = write_max;
  519. /* buffer (data + address at the beginning) */
  520. at24->writebuf = devm_kzalloc(&client->dev,
  521. write_max + 2, GFP_KERNEL);
  522. if (!at24->writebuf)
  523. return -ENOMEM;
  524. } else {
  525. dev_warn(&client->dev,
  526. "cannot write due to controller restrictions.");
  527. }
  528. }
  529. at24->client[0] = client;
  530. /* use dummy devices for multiple-address chips */
  531. for (i = 1; i < num_addresses; i++) {
  532. at24->client[i] = i2c_new_dummy(client->adapter,
  533. client->addr + i);
  534. if (!at24->client[i]) {
  535. dev_err(&client->dev, "address 0x%02x unavailable\n",
  536. client->addr + i);
  537. err = -EADDRINUSE;
  538. goto err_clients;
  539. }
  540. }
  541. err = sysfs_create_bin_file(&client->dev.kobj, &at24->bin);
  542. if (err)
  543. goto err_clients;
  544. i2c_set_clientdata(client, at24);
  545. dev_info(&client->dev, "%zu byte %s EEPROM, %s, %u bytes/write\n",
  546. at24->bin.size, client->name,
  547. writable ? "writable" : "read-only", at24->write_max);
  548. if (use_smbus == I2C_SMBUS_WORD_DATA ||
  549. use_smbus == I2C_SMBUS_BYTE_DATA) {
  550. dev_notice(&client->dev, "Falling back to %s reads, "
  551. "performance will suffer\n", use_smbus ==
  552. I2C_SMBUS_WORD_DATA ? "word" : "byte");
  553. }
  554. /* export data to kernel code */
  555. if (chip.setup)
  556. chip.setup(&at24->macc, chip.context);
  557. return 0;
  558. err_clients:
  559. for (i = 1; i < num_addresses; i++)
  560. if (at24->client[i])
  561. i2c_unregister_device(at24->client[i]);
  562. return err;
  563. }
  564. static int at24_remove(struct i2c_client *client)
  565. {
  566. struct at24_data *at24;
  567. int i;
  568. at24 = i2c_get_clientdata(client);
  569. sysfs_remove_bin_file(&client->dev.kobj, &at24->bin);
  570. for (i = 1; i < at24->num_addresses; i++)
  571. i2c_unregister_device(at24->client[i]);
  572. return 0;
  573. }
  574. /*-------------------------------------------------------------------------*/
  575. static struct i2c_driver at24_driver = {
  576. .driver = {
  577. .name = "at24",
  578. .acpi_match_table = ACPI_PTR(at24_acpi_ids),
  579. },
  580. .probe = at24_probe,
  581. .remove = at24_remove,
  582. .id_table = at24_ids,
  583. };
  584. static int __init at24_init(void)
  585. {
  586. if (!io_limit) {
  587. pr_err("at24: io_limit must not be 0!\n");
  588. return -EINVAL;
  589. }
  590. io_limit = rounddown_pow_of_two(io_limit);
  591. return i2c_add_driver(&at24_driver);
  592. }
  593. module_init(at24_init);
  594. static void __exit at24_exit(void)
  595. {
  596. i2c_del_driver(&at24_driver);
  597. }
  598. module_exit(at24_exit);
  599. MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
  600. MODULE_AUTHOR("David Brownell and Wolfram Sang");
  601. MODULE_LICENSE("GPL");