PageRenderTime 1318ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/misc/eeprom/at24.c

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