at24.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. if (at24->use_smbus) {
  169. /* Smaller eeproms can work given some SMBus extension calls */
  170. if (count > I2C_SMBUS_BLOCK_MAX)
  171. count = I2C_SMBUS_BLOCK_MAX;
  172. } else {
  173. /*
  174. * When we have a better choice than SMBus calls, use a
  175. * combined I2C message. Write address; then read up to
  176. * io_limit data bytes. Note that read page rollover helps us
  177. * here (unlike writes). msgbuf is u8 and will cast to our
  178. * needs.
  179. */
  180. i = 0;
  181. if (at24->chip.flags & AT24_FLAG_ADDR16)
  182. msgbuf[i++] = offset >> 8;
  183. msgbuf[i++] = offset;
  184. msg[0].addr = client->addr;
  185. msg[0].buf = msgbuf;
  186. msg[0].len = i;
  187. msg[1].addr = client->addr;
  188. msg[1].flags = I2C_M_RD;
  189. msg[1].buf = buf;
  190. msg[1].len = count;
  191. }
  192. /*
  193. * Reads fail if the previous write didn't complete yet. We may
  194. * loop a few times until this one succeeds, waiting at least
  195. * long enough for one entire page write to work.
  196. */
  197. timeout = jiffies + msecs_to_jiffies(write_timeout);
  198. do {
  199. read_time = jiffies;
  200. if (at24->use_smbus) {
  201. status = i2c_smbus_read_i2c_block_data_or_emulated(client, offset,
  202. count, buf);
  203. } else {
  204. status = i2c_transfer(client->adapter, msg, 2);
  205. if (status == 2)
  206. status = count;
  207. }
  208. dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
  209. count, offset, status, jiffies);
  210. if (status == count)
  211. return count;
  212. /* REVISIT: at HZ=100, this is sloooow */
  213. msleep(1);
  214. } while (time_before(read_time, timeout));
  215. return -ETIMEDOUT;
  216. }
  217. static ssize_t at24_read(struct at24_data *at24,
  218. char *buf, loff_t off, size_t count)
  219. {
  220. ssize_t retval = 0;
  221. if (unlikely(!count))
  222. return count;
  223. /*
  224. * Read data from chip, protecting against concurrent updates
  225. * from this host, but not from other I2C masters.
  226. */
  227. mutex_lock(&at24->lock);
  228. while (count) {
  229. ssize_t status;
  230. status = at24_eeprom_read(at24, buf, off, count);
  231. if (status <= 0) {
  232. if (retval == 0)
  233. retval = status;
  234. break;
  235. }
  236. buf += status;
  237. off += status;
  238. count -= status;
  239. retval += status;
  240. }
  241. mutex_unlock(&at24->lock);
  242. return retval;
  243. }
  244. static ssize_t at24_bin_read(struct file *filp, struct kobject *kobj,
  245. struct bin_attribute *attr,
  246. char *buf, loff_t off, size_t count)
  247. {
  248. struct at24_data *at24;
  249. at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
  250. return at24_read(at24, buf, off, count);
  251. }
  252. /*
  253. * Note that if the hardware write-protect pin is pulled high, the whole
  254. * chip is normally write protected. But there are plenty of product
  255. * variants here, including OTP fuses and partial chip protect.
  256. *
  257. * We only use page mode writes; the alternative is sloooow. This routine
  258. * writes at most one page.
  259. */
  260. static ssize_t at24_eeprom_write(struct at24_data *at24, const char *buf,
  261. unsigned offset, size_t count)
  262. {
  263. struct i2c_client *client;
  264. struct i2c_msg msg;
  265. ssize_t status = 0;
  266. unsigned long timeout, write_time;
  267. unsigned next_page;
  268. /* Get corresponding I2C address and adjust offset */
  269. client = at24_translate_offset(at24, &offset);
  270. /* write_max is at most a page */
  271. if (count > at24->write_max)
  272. count = at24->write_max;
  273. /* Never roll over backwards, to the start of this page */
  274. next_page = roundup(offset + 1, at24->chip.page_size);
  275. if (offset + count > next_page)
  276. count = next_page - offset;
  277. /* If we'll use I2C calls for I/O, set up the message */
  278. if (!at24->use_smbus) {
  279. int i = 0;
  280. msg.addr = client->addr;
  281. msg.flags = 0;
  282. /* msg.buf is u8 and casts will mask the values */
  283. msg.buf = at24->writebuf;
  284. if (at24->chip.flags & AT24_FLAG_ADDR16)
  285. msg.buf[i++] = offset >> 8;
  286. msg.buf[i++] = offset;
  287. memcpy(&msg.buf[i], buf, count);
  288. msg.len = i + count;
  289. }
  290. /*
  291. * Writes fail if the previous one didn't complete yet. We may
  292. * loop a few times until this one succeeds, waiting at least
  293. * long enough for one entire page write to work.
  294. */
  295. timeout = jiffies + msecs_to_jiffies(write_timeout);
  296. do {
  297. write_time = jiffies;
  298. if (at24->use_smbus_write) {
  299. switch (at24->use_smbus_write) {
  300. case I2C_SMBUS_I2C_BLOCK_DATA:
  301. status = i2c_smbus_write_i2c_block_data(client,
  302. offset, count, buf);
  303. break;
  304. case I2C_SMBUS_BYTE_DATA:
  305. status = i2c_smbus_write_byte_data(client,
  306. offset, buf[0]);
  307. break;
  308. }
  309. if (status == 0)
  310. status = count;
  311. } else {
  312. status = i2c_transfer(client->adapter, &msg, 1);
  313. if (status == 1)
  314. status = count;
  315. }
  316. dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
  317. count, offset, status, jiffies);
  318. if (status == count)
  319. return count;
  320. /* REVISIT: at HZ=100, this is sloooow */
  321. msleep(1);
  322. } while (time_before(write_time, timeout));
  323. return -ETIMEDOUT;
  324. }
  325. static ssize_t at24_write(struct at24_data *at24, const char *buf, loff_t off,
  326. size_t count)
  327. {
  328. ssize_t retval = 0;
  329. if (unlikely(!count))
  330. return count;
  331. /*
  332. * Write data to chip, protecting against concurrent updates
  333. * from this host, but not from other I2C masters.
  334. */
  335. mutex_lock(&at24->lock);
  336. while (count) {
  337. ssize_t status;
  338. status = at24_eeprom_write(at24, buf, off, count);
  339. if (status <= 0) {
  340. if (retval == 0)
  341. retval = status;
  342. break;
  343. }
  344. buf += status;
  345. off += status;
  346. count -= status;
  347. retval += status;
  348. }
  349. mutex_unlock(&at24->lock);
  350. return retval;
  351. }
  352. static ssize_t at24_bin_write(struct file *filp, struct kobject *kobj,
  353. struct bin_attribute *attr,
  354. char *buf, loff_t off, size_t count)
  355. {
  356. struct at24_data *at24;
  357. at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
  358. return at24_write(at24, buf, off, count);
  359. }
  360. /*-------------------------------------------------------------------------*/
  361. /*
  362. * This lets other kernel code access the eeprom data. For example, it
  363. * might hold a board's Ethernet address, or board-specific calibration
  364. * data generated on the manufacturing floor.
  365. */
  366. static ssize_t at24_macc_read(struct memory_accessor *macc, char *buf,
  367. off_t offset, size_t count)
  368. {
  369. struct at24_data *at24 = container_of(macc, struct at24_data, macc);
  370. return at24_read(at24, buf, offset, count);
  371. }
  372. static ssize_t at24_macc_write(struct memory_accessor *macc, const char *buf,
  373. off_t offset, size_t count)
  374. {
  375. struct at24_data *at24 = container_of(macc, struct at24_data, macc);
  376. return at24_write(at24, buf, offset, count);
  377. }
  378. /*-------------------------------------------------------------------------*/
  379. #ifdef CONFIG_OF
  380. static void at24_get_ofdata(struct i2c_client *client,
  381. struct at24_platform_data *chip)
  382. {
  383. const __be32 *val;
  384. struct device_node *node = client->dev.of_node;
  385. if (node) {
  386. if (of_get_property(node, "read-only", NULL))
  387. chip->flags |= AT24_FLAG_READONLY;
  388. val = of_get_property(node, "pagesize", NULL);
  389. if (val)
  390. chip->page_size = be32_to_cpup(val);
  391. }
  392. }
  393. #else
  394. static void at24_get_ofdata(struct i2c_client *client,
  395. struct at24_platform_data *chip)
  396. { }
  397. #endif /* CONFIG_OF */
  398. static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
  399. {
  400. struct at24_platform_data chip;
  401. bool writable;
  402. int use_smbus = 0;
  403. int use_smbus_write = 0;
  404. struct at24_data *at24;
  405. int err;
  406. unsigned i, num_addresses;
  407. kernel_ulong_t magic;
  408. if (client->dev.platform_data) {
  409. chip = *(struct at24_platform_data *)client->dev.platform_data;
  410. } else {
  411. if (!id->driver_data)
  412. return -ENODEV;
  413. magic = id->driver_data;
  414. chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN));
  415. magic >>= AT24_SIZE_BYTELEN;
  416. chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS);
  417. /*
  418. * This is slow, but we can't know all eeproms, so we better
  419. * play safe. Specifying custom eeprom-types via platform_data
  420. * is recommended anyhow.
  421. */
  422. chip.page_size = 1;
  423. /* update chipdata if OF is present */
  424. at24_get_ofdata(client, &chip);
  425. chip.setup = NULL;
  426. chip.context = NULL;
  427. }
  428. if (!is_power_of_2(chip.byte_len))
  429. dev_warn(&client->dev,
  430. "byte_len looks suspicious (no power of 2)!\n");
  431. if (!chip.page_size) {
  432. dev_err(&client->dev, "page_size must not be 0!\n");
  433. return -EINVAL;
  434. }
  435. if (!is_power_of_2(chip.page_size))
  436. dev_warn(&client->dev,
  437. "page_size looks suspicious (no power of 2)!\n");
  438. /* Use I2C operations unless we're stuck with SMBus extensions. */
  439. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  440. if (chip.flags & AT24_FLAG_ADDR16)
  441. return -EPFNOSUPPORT;
  442. if (i2c_check_functionality(client->adapter,
  443. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  444. use_smbus = I2C_SMBUS_I2C_BLOCK_DATA;
  445. } else if (i2c_check_functionality(client->adapter,
  446. I2C_FUNC_SMBUS_READ_WORD_DATA)) {
  447. use_smbus = I2C_SMBUS_WORD_DATA;
  448. } else if (i2c_check_functionality(client->adapter,
  449. I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
  450. use_smbus = I2C_SMBUS_BYTE_DATA;
  451. } else {
  452. return -EPFNOSUPPORT;
  453. }
  454. }
  455. /* Use I2C operations unless we're stuck with SMBus extensions. */
  456. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  457. if (i2c_check_functionality(client->adapter,
  458. I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
  459. use_smbus_write = I2C_SMBUS_I2C_BLOCK_DATA;
  460. } else if (i2c_check_functionality(client->adapter,
  461. I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {
  462. use_smbus_write = I2C_SMBUS_BYTE_DATA;
  463. chip.page_size = 1;
  464. }
  465. }
  466. if (chip.flags & AT24_FLAG_TAKE8ADDR)
  467. num_addresses = 8;
  468. else
  469. num_addresses = DIV_ROUND_UP(chip.byte_len,
  470. (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
  471. at24 = devm_kzalloc(&client->dev, sizeof(struct at24_data) +
  472. num_addresses * sizeof(struct i2c_client *), GFP_KERNEL);
  473. if (!at24)
  474. return -ENOMEM;
  475. mutex_init(&at24->lock);
  476. at24->use_smbus = use_smbus;
  477. at24->use_smbus_write = use_smbus_write;
  478. at24->chip = chip;
  479. at24->num_addresses = num_addresses;
  480. /*
  481. * Export the EEPROM bytes through sysfs, since that's convenient.
  482. * By default, only root should see the data (maybe passwords etc)
  483. */
  484. sysfs_bin_attr_init(&at24->bin);
  485. at24->bin.attr.name = "eeprom";
  486. at24->bin.attr.mode = chip.flags & AT24_FLAG_IRUGO ? S_IRUGO : S_IRUSR;
  487. at24->bin.read = at24_bin_read;
  488. at24->bin.size = chip.byte_len;
  489. at24->macc.read = at24_macc_read;
  490. writable = !(chip.flags & AT24_FLAG_READONLY);
  491. if (writable) {
  492. if (!use_smbus || use_smbus_write) {
  493. unsigned write_max = chip.page_size;
  494. at24->macc.write = at24_macc_write;
  495. at24->bin.write = at24_bin_write;
  496. at24->bin.attr.mode |= S_IWUSR;
  497. if (write_max > io_limit)
  498. write_max = io_limit;
  499. if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX)
  500. write_max = I2C_SMBUS_BLOCK_MAX;
  501. at24->write_max = write_max;
  502. /* buffer (data + address at the beginning) */
  503. at24->writebuf = devm_kzalloc(&client->dev,
  504. write_max + 2, GFP_KERNEL);
  505. if (!at24->writebuf)
  506. return -ENOMEM;
  507. } else {
  508. dev_warn(&client->dev,
  509. "cannot write due to controller restrictions.");
  510. }
  511. }
  512. at24->client[0] = client;
  513. /* use dummy devices for multiple-address chips */
  514. for (i = 1; i < num_addresses; i++) {
  515. at24->client[i] = i2c_new_dummy(client->adapter,
  516. client->addr + i);
  517. if (!at24->client[i]) {
  518. dev_err(&client->dev, "address 0x%02x unavailable\n",
  519. client->addr + i);
  520. err = -EADDRINUSE;
  521. goto err_clients;
  522. }
  523. }
  524. err = sysfs_create_bin_file(&client->dev.kobj, &at24->bin);
  525. if (err)
  526. goto err_clients;
  527. i2c_set_clientdata(client, at24);
  528. dev_info(&client->dev, "%zu byte %s EEPROM, %s, %u bytes/write\n",
  529. at24->bin.size, client->name,
  530. writable ? "writable" : "read-only", at24->write_max);
  531. if (use_smbus == I2C_SMBUS_WORD_DATA ||
  532. use_smbus == I2C_SMBUS_BYTE_DATA) {
  533. dev_notice(&client->dev, "Falling back to %s reads, "
  534. "performance will suffer\n", use_smbus ==
  535. I2C_SMBUS_WORD_DATA ? "word" : "byte");
  536. }
  537. /* export data to kernel code */
  538. if (chip.setup)
  539. chip.setup(&at24->macc, chip.context);
  540. return 0;
  541. err_clients:
  542. for (i = 1; i < num_addresses; i++)
  543. if (at24->client[i])
  544. i2c_unregister_device(at24->client[i]);
  545. return err;
  546. }
  547. static int at24_remove(struct i2c_client *client)
  548. {
  549. struct at24_data *at24;
  550. int i;
  551. at24 = i2c_get_clientdata(client);
  552. sysfs_remove_bin_file(&client->dev.kobj, &at24->bin);
  553. for (i = 1; i < at24->num_addresses; i++)
  554. i2c_unregister_device(at24->client[i]);
  555. return 0;
  556. }
  557. /*-------------------------------------------------------------------------*/
  558. static struct i2c_driver at24_driver = {
  559. .driver = {
  560. .name = "at24",
  561. },
  562. .probe = at24_probe,
  563. .remove = at24_remove,
  564. .id_table = at24_ids,
  565. };
  566. static int __init at24_init(void)
  567. {
  568. if (!io_limit) {
  569. pr_err("at24: io_limit must not be 0!\n");
  570. return -EINVAL;
  571. }
  572. io_limit = rounddown_pow_of_two(io_limit);
  573. return i2c_add_driver(&at24_driver);
  574. }
  575. module_init(at24_init);
  576. static void __exit at24_exit(void)
  577. {
  578. i2c_del_driver(&at24_driver);
  579. }
  580. module_exit(at24_exit);
  581. MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
  582. MODULE_AUTHOR("David Brownell and Wolfram Sang");
  583. MODULE_LICENSE("GPL");