at24.c 19 KB

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