at24.c 18 KB

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