at24.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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/mod_devicetable.h>
  19. #include <linux/log2.h>
  20. #include <linux/bitops.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/of.h>
  23. #include <linux/acpi.h>
  24. #include <linux/i2c.h>
  25. #include <linux/nvmem-provider.h>
  26. #include <linux/regmap.h>
  27. #include <linux/platform_data/at24.h>
  28. /*
  29. * I2C EEPROMs from most vendors are inexpensive and mostly interchangeable.
  30. * Differences between different vendor product lines (like Atmel AT24C or
  31. * MicroChip 24LC, etc) won't much matter for typical read/write access.
  32. * There are also I2C RAM chips, likewise interchangeable. One example
  33. * would be the PCF8570, which acts like a 24c02 EEPROM (256 bytes).
  34. *
  35. * However, misconfiguration can lose data. "Set 16-bit memory address"
  36. * to a part with 8-bit addressing will overwrite data. Writing with too
  37. * big a page size also loses data. And it's not safe to assume that the
  38. * conventional addresses 0x50..0x57 only hold eeproms; a PCF8563 RTC
  39. * uses 0x51, for just one example.
  40. *
  41. * Accordingly, explicit board-specific configuration data should be used
  42. * in almost all cases. (One partial exception is an SMBus used to access
  43. * "SPD" data for DRAM sticks. Those only use 24c02 EEPROMs.)
  44. *
  45. * So this driver uses "new style" I2C driver binding, expecting to be
  46. * told what devices exist. That may be in arch/X/mach-Y/board-Z.c or
  47. * similar kernel-resident tables; or, configuration data coming from
  48. * a bootloader.
  49. *
  50. * Other than binding model, current differences from "eeprom" driver are
  51. * that this one handles write access and isn't restricted to 24c02 devices.
  52. * It also handles larger devices (32 kbit and up) with two-byte addresses,
  53. * which won't work on pure SMBus systems.
  54. */
  55. struct at24_data {
  56. struct at24_platform_data chip;
  57. int use_smbus;
  58. int use_smbus_write;
  59. /*
  60. * Lock protects against activities from other Linux tasks,
  61. * but not from changes by other I2C masters.
  62. */
  63. struct mutex lock;
  64. u8 *writebuf;
  65. unsigned write_max;
  66. unsigned num_addresses;
  67. struct regmap_config regmap_config;
  68. struct nvmem_config nvmem_config;
  69. struct nvmem_device *nvmem;
  70. /*
  71. * Some chips tie up multiple I2C addresses; dummy devices reserve
  72. * them for us, and we'll use them with SMBus calls.
  73. */
  74. struct i2c_client *client[];
  75. };
  76. /*
  77. * This parameter is to help this driver avoid blocking other drivers out
  78. * of I2C for potentially troublesome amounts of time. With a 100 kHz I2C
  79. * clock, one 256 byte read takes about 1/43 second which is excessive;
  80. * but the 1/170 second it takes at 400 kHz may be quite reasonable; and
  81. * at 1 MHz (Fm+) a 1/430 second delay could easily be invisible.
  82. *
  83. * This value is forced to be a power of two so that writes align on pages.
  84. */
  85. static unsigned io_limit = 128;
  86. module_param(io_limit, uint, 0);
  87. MODULE_PARM_DESC(io_limit, "Maximum bytes per I/O (default 128)");
  88. /*
  89. * Specs often allow 5 msec for a page write, sometimes 20 msec;
  90. * it's important to recover from write timeouts.
  91. */
  92. static unsigned write_timeout = 25;
  93. module_param(write_timeout, uint, 0);
  94. MODULE_PARM_DESC(write_timeout, "Time (in ms) to try writes (default 25)");
  95. #define AT24_SIZE_BYTELEN 5
  96. #define AT24_SIZE_FLAGS 8
  97. #define AT24_BITMASK(x) (BIT(x) - 1)
  98. /* create non-zero magic value for given eeprom parameters */
  99. #define AT24_DEVICE_MAGIC(_len, _flags) \
  100. ((1 << AT24_SIZE_FLAGS | (_flags)) \
  101. << AT24_SIZE_BYTELEN | ilog2(_len))
  102. static const struct i2c_device_id at24_ids[] = {
  103. /* needs 8 addresses as A0-A2 are ignored */
  104. { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) },
  105. /* old variants can't be handled with this generic entry! */
  106. { "24c01", AT24_DEVICE_MAGIC(1024 / 8, 0) },
  107. { "24c02", AT24_DEVICE_MAGIC(2048 / 8, 0) },
  108. /* spd is a 24c02 in memory DIMMs */
  109. { "spd", AT24_DEVICE_MAGIC(2048 / 8,
  110. AT24_FLAG_READONLY | AT24_FLAG_IRUGO) },
  111. { "24c04", AT24_DEVICE_MAGIC(4096 / 8, 0) },
  112. /* 24rf08 quirk is handled at i2c-core */
  113. { "24c08", AT24_DEVICE_MAGIC(8192 / 8, 0) },
  114. { "24c16", AT24_DEVICE_MAGIC(16384 / 8, 0) },
  115. { "24c32", AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) },
  116. { "24c64", AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) },
  117. { "24c128", AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16) },
  118. { "24c256", AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16) },
  119. { "24c512", AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16) },
  120. { "24c1024", AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16) },
  121. { "at24", 0 },
  122. { /* END OF LIST */ }
  123. };
  124. MODULE_DEVICE_TABLE(i2c, at24_ids);
  125. static const struct acpi_device_id at24_acpi_ids[] = {
  126. { "INT3499", AT24_DEVICE_MAGIC(8192 / 8, 0) },
  127. { }
  128. };
  129. MODULE_DEVICE_TABLE(acpi, at24_acpi_ids);
  130. /*-------------------------------------------------------------------------*/
  131. /*
  132. * This routine supports chips which consume multiple I2C addresses. It
  133. * computes the addressing information to be used for a given r/w request.
  134. * Assumes that sanity checks for offset happened at sysfs-layer.
  135. */
  136. static struct i2c_client *at24_translate_offset(struct at24_data *at24,
  137. unsigned *offset)
  138. {
  139. unsigned i;
  140. if (at24->chip.flags & AT24_FLAG_ADDR16) {
  141. i = *offset >> 16;
  142. *offset &= 0xffff;
  143. } else {
  144. i = *offset >> 8;
  145. *offset &= 0xff;
  146. }
  147. return at24->client[i];
  148. }
  149. static ssize_t at24_eeprom_read(struct at24_data *at24, char *buf,
  150. unsigned offset, size_t count)
  151. {
  152. struct i2c_msg msg[2];
  153. u8 msgbuf[2];
  154. struct i2c_client *client;
  155. unsigned long timeout, read_time;
  156. int status, i;
  157. memset(msg, 0, sizeof(msg));
  158. /*
  159. * REVISIT some multi-address chips don't rollover page reads to
  160. * the next slave address, so we may need to truncate the count.
  161. * Those chips might need another quirk flag.
  162. *
  163. * If the real hardware used four adjacent 24c02 chips and that
  164. * were misconfigured as one 24c08, that would be a similar effect:
  165. * one "eeprom" file not four, but larger reads would fail when
  166. * they crossed certain pages.
  167. */
  168. /*
  169. * Slave address and byte offset derive from the offset. Always
  170. * set the byte address; on a multi-master board, another master
  171. * may have changed the chip's "current" address pointer.
  172. */
  173. client = at24_translate_offset(at24, &offset);
  174. if (count > io_limit)
  175. count = io_limit;
  176. if (at24->use_smbus) {
  177. /* Smaller eeproms can work given some SMBus extension calls */
  178. if (count > I2C_SMBUS_BLOCK_MAX)
  179. count = I2C_SMBUS_BLOCK_MAX;
  180. } else {
  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. if (at24->use_smbus) {
  209. status = i2c_smbus_read_i2c_block_data_or_emulated(client, offset,
  210. count, buf);
  211. } else {
  212. status = i2c_transfer(client->adapter, msg, 2);
  213. if (status == 2)
  214. status = count;
  215. }
  216. dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
  217. count, offset, status, jiffies);
  218. if (status == count)
  219. return count;
  220. /* REVISIT: at HZ=100, this is sloooow */
  221. msleep(1);
  222. } while (time_before(read_time, timeout));
  223. return -ETIMEDOUT;
  224. }
  225. static ssize_t at24_read(struct at24_data *at24,
  226. char *buf, loff_t off, size_t count)
  227. {
  228. ssize_t retval = 0;
  229. if (unlikely(!count))
  230. return count;
  231. /*
  232. * Read data from chip, protecting against concurrent updates
  233. * from this host, but not from other I2C masters.
  234. */
  235. mutex_lock(&at24->lock);
  236. while (count) {
  237. ssize_t status;
  238. status = at24_eeprom_read(at24, buf, off, count);
  239. if (status <= 0) {
  240. if (retval == 0)
  241. retval = status;
  242. break;
  243. }
  244. buf += status;
  245. off += status;
  246. count -= status;
  247. retval += status;
  248. }
  249. mutex_unlock(&at24->lock);
  250. return retval;
  251. }
  252. /*
  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. /*-------------------------------------------------------------------------*/
  353. /*
  354. * Provide a regmap interface, which is registered with the NVMEM
  355. * framework
  356. */
  357. static int at24_regmap_read(void *context, const void *reg, size_t reg_size,
  358. void *val, size_t val_size)
  359. {
  360. struct at24_data *at24 = context;
  361. off_t offset = *(u32 *)reg;
  362. int err;
  363. err = at24_read(at24, val, offset, val_size);
  364. if (err)
  365. return err;
  366. return 0;
  367. }
  368. static int at24_regmap_write(void *context, const void *data, size_t count)
  369. {
  370. struct at24_data *at24 = context;
  371. const char *buf;
  372. u32 offset;
  373. size_t len;
  374. int err;
  375. memcpy(&offset, data, sizeof(offset));
  376. buf = (const char *)data + sizeof(offset);
  377. len = count - sizeof(offset);
  378. err = at24_write(at24, buf, offset, len);
  379. if (err)
  380. return err;
  381. return 0;
  382. }
  383. static const struct regmap_bus at24_regmap_bus = {
  384. .read = at24_regmap_read,
  385. .write = at24_regmap_write,
  386. .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
  387. };
  388. /*-------------------------------------------------------------------------*/
  389. #ifdef CONFIG_OF
  390. static void at24_get_ofdata(struct i2c_client *client,
  391. struct at24_platform_data *chip)
  392. {
  393. const __be32 *val;
  394. struct device_node *node = client->dev.of_node;
  395. if (node) {
  396. if (of_get_property(node, "read-only", NULL))
  397. chip->flags |= AT24_FLAG_READONLY;
  398. val = of_get_property(node, "pagesize", NULL);
  399. if (val)
  400. chip->page_size = be32_to_cpup(val);
  401. }
  402. }
  403. #else
  404. static void at24_get_ofdata(struct i2c_client *client,
  405. struct at24_platform_data *chip)
  406. { }
  407. #endif /* CONFIG_OF */
  408. static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
  409. {
  410. struct at24_platform_data chip;
  411. kernel_ulong_t magic = 0;
  412. bool writable;
  413. int use_smbus = 0;
  414. int use_smbus_write = 0;
  415. struct at24_data *at24;
  416. int err;
  417. unsigned i, num_addresses;
  418. struct regmap *regmap;
  419. if (client->dev.platform_data) {
  420. chip = *(struct at24_platform_data *)client->dev.platform_data;
  421. } else {
  422. if (id) {
  423. magic = id->driver_data;
  424. } else {
  425. const struct acpi_device_id *aid;
  426. aid = acpi_match_device(at24_acpi_ids, &client->dev);
  427. if (aid)
  428. magic = aid->driver_data;
  429. }
  430. if (!magic)
  431. return -ENODEV;
  432. chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN));
  433. magic >>= AT24_SIZE_BYTELEN;
  434. chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS);
  435. /*
  436. * This is slow, but we can't know all eeproms, so we better
  437. * play safe. Specifying custom eeprom-types via platform_data
  438. * is recommended anyhow.
  439. */
  440. chip.page_size = 1;
  441. /* update chipdata if OF is present */
  442. at24_get_ofdata(client, &chip);
  443. chip.setup = NULL;
  444. chip.context = NULL;
  445. }
  446. if (!is_power_of_2(chip.byte_len))
  447. dev_warn(&client->dev,
  448. "byte_len looks suspicious (no power of 2)!\n");
  449. if (!chip.page_size) {
  450. dev_err(&client->dev, "page_size must not be 0!\n");
  451. return -EINVAL;
  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. return -EPFNOSUPPORT;
  460. if (i2c_check_functionality(client->adapter,
  461. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  462. use_smbus = I2C_SMBUS_I2C_BLOCK_DATA;
  463. } else if (i2c_check_functionality(client->adapter,
  464. I2C_FUNC_SMBUS_READ_WORD_DATA)) {
  465. use_smbus = I2C_SMBUS_WORD_DATA;
  466. } else if (i2c_check_functionality(client->adapter,
  467. I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
  468. use_smbus = I2C_SMBUS_BYTE_DATA;
  469. } else {
  470. return -EPFNOSUPPORT;
  471. }
  472. }
  473. /* Use I2C operations unless we're stuck with SMBus extensions. */
  474. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  475. if (i2c_check_functionality(client->adapter,
  476. I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
  477. use_smbus_write = I2C_SMBUS_I2C_BLOCK_DATA;
  478. } else if (i2c_check_functionality(client->adapter,
  479. I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {
  480. use_smbus_write = I2C_SMBUS_BYTE_DATA;
  481. chip.page_size = 1;
  482. }
  483. }
  484. if (chip.flags & AT24_FLAG_TAKE8ADDR)
  485. num_addresses = 8;
  486. else
  487. num_addresses = DIV_ROUND_UP(chip.byte_len,
  488. (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
  489. at24 = devm_kzalloc(&client->dev, sizeof(struct at24_data) +
  490. num_addresses * sizeof(struct i2c_client *), GFP_KERNEL);
  491. if (!at24)
  492. return -ENOMEM;
  493. mutex_init(&at24->lock);
  494. at24->use_smbus = use_smbus;
  495. at24->use_smbus_write = use_smbus_write;
  496. at24->chip = chip;
  497. at24->num_addresses = num_addresses;
  498. writable = !(chip.flags & AT24_FLAG_READONLY);
  499. if (writable) {
  500. if (!use_smbus || use_smbus_write) {
  501. unsigned write_max = chip.page_size;
  502. if (write_max > io_limit)
  503. write_max = io_limit;
  504. if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX)
  505. write_max = I2C_SMBUS_BLOCK_MAX;
  506. at24->write_max = write_max;
  507. /* buffer (data + address at the beginning) */
  508. at24->writebuf = devm_kzalloc(&client->dev,
  509. write_max + 2, GFP_KERNEL);
  510. if (!at24->writebuf)
  511. return -ENOMEM;
  512. } else {
  513. dev_warn(&client->dev,
  514. "cannot write due to controller restrictions.");
  515. }
  516. }
  517. at24->client[0] = client;
  518. /* use dummy devices for multiple-address chips */
  519. for (i = 1; i < num_addresses; i++) {
  520. at24->client[i] = i2c_new_dummy(client->adapter,
  521. client->addr + i);
  522. if (!at24->client[i]) {
  523. dev_err(&client->dev, "address 0x%02x unavailable\n",
  524. client->addr + i);
  525. err = -EADDRINUSE;
  526. goto err_clients;
  527. }
  528. }
  529. at24->regmap_config.reg_bits = 32;
  530. at24->regmap_config.val_bits = 8;
  531. at24->regmap_config.reg_stride = 1;
  532. at24->regmap_config.max_register = chip.byte_len - 1;
  533. regmap = devm_regmap_init(&client->dev, &at24_regmap_bus, at24,
  534. &at24->regmap_config);
  535. if (IS_ERR(regmap)) {
  536. dev_err(&client->dev, "regmap init failed\n");
  537. err = PTR_ERR(regmap);
  538. goto err_clients;
  539. }
  540. at24->nvmem_config.name = dev_name(&client->dev);
  541. at24->nvmem_config.dev = &client->dev;
  542. at24->nvmem_config.read_only = !writable;
  543. at24->nvmem_config.root_only = true;
  544. at24->nvmem_config.owner = THIS_MODULE;
  545. at24->nvmem_config.compat = true;
  546. at24->nvmem_config.base_dev = &client->dev;
  547. at24->nvmem = nvmem_register(&at24->nvmem_config);
  548. if (IS_ERR(at24->nvmem)) {
  549. err = PTR_ERR(at24->nvmem);
  550. goto err_clients;
  551. }
  552. i2c_set_clientdata(client, at24);
  553. dev_info(&client->dev, "%u byte %s EEPROM, %s, %u bytes/write\n",
  554. chip.byte_len, client->name,
  555. writable ? "writable" : "read-only", at24->write_max);
  556. if (use_smbus == I2C_SMBUS_WORD_DATA ||
  557. use_smbus == I2C_SMBUS_BYTE_DATA) {
  558. dev_notice(&client->dev, "Falling back to %s reads, "
  559. "performance will suffer\n", use_smbus ==
  560. I2C_SMBUS_WORD_DATA ? "word" : "byte");
  561. }
  562. /* export data to kernel code */
  563. if (chip.setup)
  564. chip.setup(at24->nvmem, chip.context);
  565. return 0;
  566. err_clients:
  567. for (i = 1; i < num_addresses; i++)
  568. if (at24->client[i])
  569. i2c_unregister_device(at24->client[i]);
  570. return err;
  571. }
  572. static int at24_remove(struct i2c_client *client)
  573. {
  574. struct at24_data *at24;
  575. int i;
  576. at24 = i2c_get_clientdata(client);
  577. nvmem_unregister(at24->nvmem);
  578. for (i = 1; i < at24->num_addresses; i++)
  579. i2c_unregister_device(at24->client[i]);
  580. return 0;
  581. }
  582. /*-------------------------------------------------------------------------*/
  583. static struct i2c_driver at24_driver = {
  584. .driver = {
  585. .name = "at24",
  586. .acpi_match_table = ACPI_PTR(at24_acpi_ids),
  587. },
  588. .probe = at24_probe,
  589. .remove = at24_remove,
  590. .id_table = at24_ids,
  591. };
  592. static int __init at24_init(void)
  593. {
  594. if (!io_limit) {
  595. pr_err("at24: io_limit must not be 0!\n");
  596. return -EINVAL;
  597. }
  598. io_limit = rounddown_pow_of_two(io_limit);
  599. return i2c_add_driver(&at24_driver);
  600. }
  601. module_init(at24_init);
  602. static void __exit at24_exit(void)
  603. {
  604. i2c_del_driver(&at24_driver);
  605. }
  606. module_exit(at24_exit);
  607. MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
  608. MODULE_AUTHOR("David Brownell and Wolfram Sang");
  609. MODULE_LICENSE("GPL");