at24.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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/of_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/delay.h>
  18. #include <linux/mutex.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/property.h>
  24. #include <linux/acpi.h>
  25. #include <linux/i2c.h>
  26. #include <linux/nvmem-provider.h>
  27. #include <linux/regmap.h>
  28. #include <linux/platform_data/at24.h>
  29. #include <linux/pm_runtime.h>
  30. /*
  31. * I2C EEPROMs from most vendors are inexpensive and mostly interchangeable.
  32. * Differences between different vendor product lines (like Atmel AT24C or
  33. * MicroChip 24LC, etc) won't much matter for typical read/write access.
  34. * There are also I2C RAM chips, likewise interchangeable. One example
  35. * would be the PCF8570, which acts like a 24c02 EEPROM (256 bytes).
  36. *
  37. * However, misconfiguration can lose data. "Set 16-bit memory address"
  38. * to a part with 8-bit addressing will overwrite data. Writing with too
  39. * big a page size also loses data. And it's not safe to assume that the
  40. * conventional addresses 0x50..0x57 only hold eeproms; a PCF8563 RTC
  41. * uses 0x51, for just one example.
  42. *
  43. * Accordingly, explicit board-specific configuration data should be used
  44. * in almost all cases. (One partial exception is an SMBus used to access
  45. * "SPD" data for DRAM sticks. Those only use 24c02 EEPROMs.)
  46. *
  47. * So this driver uses "new style" I2C driver binding, expecting to be
  48. * told what devices exist. That may be in arch/X/mach-Y/board-Z.c or
  49. * similar kernel-resident tables; or, configuration data coming from
  50. * a bootloader.
  51. *
  52. * Other than binding model, current differences from "eeprom" driver are
  53. * that this one handles write access and isn't restricted to 24c02 devices.
  54. * It also handles larger devices (32 kbit and up) with two-byte addresses,
  55. * which won't work on pure SMBus systems.
  56. */
  57. struct at24_client {
  58. struct i2c_client *client;
  59. struct regmap *regmap;
  60. };
  61. struct at24_data {
  62. struct at24_platform_data chip;
  63. /*
  64. * Lock protects against activities from other Linux tasks,
  65. * but not from changes by other I2C masters.
  66. */
  67. struct mutex lock;
  68. unsigned write_max;
  69. unsigned num_addresses;
  70. unsigned int offset_adj;
  71. struct nvmem_config nvmem_config;
  72. struct nvmem_device *nvmem;
  73. /*
  74. * Some chips tie up multiple I2C addresses; dummy devices reserve
  75. * them for us, and we'll use them with SMBus calls.
  76. */
  77. struct at24_client client[];
  78. };
  79. /*
  80. * This parameter is to help this driver avoid blocking other drivers out
  81. * of I2C for potentially troublesome amounts of time. With a 100 kHz I2C
  82. * clock, one 256 byte read takes about 1/43 second which is excessive;
  83. * but the 1/170 second it takes at 400 kHz may be quite reasonable; and
  84. * at 1 MHz (Fm+) a 1/430 second delay could easily be invisible.
  85. *
  86. * This value is forced to be a power of two so that writes align on pages.
  87. */
  88. static unsigned io_limit = 128;
  89. module_param(io_limit, uint, 0);
  90. MODULE_PARM_DESC(io_limit, "Maximum bytes per I/O (default 128)");
  91. /*
  92. * Specs often allow 5 msec for a page write, sometimes 20 msec;
  93. * it's important to recover from write timeouts.
  94. */
  95. static unsigned write_timeout = 25;
  96. module_param(write_timeout, uint, 0);
  97. MODULE_PARM_DESC(write_timeout, "Time (in ms) to try writes (default 25)");
  98. #define AT24_SIZE_BYTELEN 5
  99. #define AT24_SIZE_FLAGS 8
  100. #define AT24_BITMASK(x) (BIT(x) - 1)
  101. /* create non-zero magic value for given eeprom parameters */
  102. #define AT24_DEVICE_MAGIC(_len, _flags) \
  103. ((1 << AT24_SIZE_FLAGS | (_flags)) \
  104. << AT24_SIZE_BYTELEN | ilog2(_len))
  105. /*
  106. * Both reads and writes fail if the previous write didn't complete yet. This
  107. * macro loops a few times waiting at least long enough for one entire page
  108. * write to work while making sure that at least one iteration is run before
  109. * checking the break condition.
  110. *
  111. * It takes two parameters: a variable in which the future timeout in jiffies
  112. * will be stored and a temporary variable holding the time of the last
  113. * iteration of processing the request. Both should be unsigned integers
  114. * holding at least 32 bits.
  115. */
  116. #define loop_until_timeout(tout, op_time) \
  117. for (tout = jiffies + msecs_to_jiffies(write_timeout), op_time = 0; \
  118. op_time ? time_before(op_time, tout) : true; \
  119. usleep_range(1000, 1500), op_time = jiffies)
  120. static const struct i2c_device_id at24_ids[] = {
  121. /* needs 8 addresses as A0-A2 are ignored */
  122. { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) },
  123. /* old variants can't be handled with this generic entry! */
  124. { "24c01", AT24_DEVICE_MAGIC(1024 / 8, 0) },
  125. { "24cs01", AT24_DEVICE_MAGIC(16,
  126. AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
  127. { "24c02", AT24_DEVICE_MAGIC(2048 / 8, 0) },
  128. { "24cs02", AT24_DEVICE_MAGIC(16,
  129. AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
  130. { "24mac402", AT24_DEVICE_MAGIC(48 / 8,
  131. AT24_FLAG_MAC | AT24_FLAG_READONLY) },
  132. { "24mac602", AT24_DEVICE_MAGIC(64 / 8,
  133. AT24_FLAG_MAC | AT24_FLAG_READONLY) },
  134. /* spd is a 24c02 in memory DIMMs */
  135. { "spd", AT24_DEVICE_MAGIC(2048 / 8,
  136. AT24_FLAG_READONLY | AT24_FLAG_IRUGO) },
  137. { "24c04", AT24_DEVICE_MAGIC(4096 / 8, 0) },
  138. { "24cs04", AT24_DEVICE_MAGIC(16,
  139. AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
  140. /* 24rf08 quirk is handled at i2c-core */
  141. { "24c08", AT24_DEVICE_MAGIC(8192 / 8, 0) },
  142. { "24cs08", AT24_DEVICE_MAGIC(16,
  143. AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
  144. { "24c16", AT24_DEVICE_MAGIC(16384 / 8, 0) },
  145. { "24cs16", AT24_DEVICE_MAGIC(16,
  146. AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
  147. { "24c32", AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) },
  148. { "24cs32", AT24_DEVICE_MAGIC(16,
  149. AT24_FLAG_ADDR16 |
  150. AT24_FLAG_SERIAL |
  151. AT24_FLAG_READONLY) },
  152. { "24c64", AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) },
  153. { "24cs64", AT24_DEVICE_MAGIC(16,
  154. AT24_FLAG_ADDR16 |
  155. AT24_FLAG_SERIAL |
  156. AT24_FLAG_READONLY) },
  157. { "24c128", AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16) },
  158. { "24c256", AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16) },
  159. { "24c512", AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16) },
  160. { "24c1024", AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16) },
  161. { "at24", 0 },
  162. { /* END OF LIST */ }
  163. };
  164. MODULE_DEVICE_TABLE(i2c, at24_ids);
  165. static const struct of_device_id at24_of_match[] = {
  166. {
  167. .compatible = "atmel,24c00",
  168. .data = (void *)AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR)
  169. },
  170. {
  171. .compatible = "atmel,24c01",
  172. .data = (void *)AT24_DEVICE_MAGIC(1024 / 8, 0)
  173. },
  174. {
  175. .compatible = "atmel,24c02",
  176. .data = (void *)AT24_DEVICE_MAGIC(2048 / 8, 0)
  177. },
  178. {
  179. .compatible = "atmel,spd",
  180. .data = (void *)AT24_DEVICE_MAGIC(2048 / 8,
  181. AT24_FLAG_READONLY | AT24_FLAG_IRUGO)
  182. },
  183. {
  184. .compatible = "atmel,24c04",
  185. .data = (void *)AT24_DEVICE_MAGIC(4096 / 8, 0)
  186. },
  187. {
  188. .compatible = "atmel,24c08",
  189. .data = (void *)AT24_DEVICE_MAGIC(8192 / 8, 0)
  190. },
  191. {
  192. .compatible = "atmel,24c16",
  193. .data = (void *)AT24_DEVICE_MAGIC(16384 / 8, 0)
  194. },
  195. {
  196. .compatible = "atmel,24c32",
  197. .data = (void *)AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16)
  198. },
  199. {
  200. .compatible = "atmel,24c64",
  201. .data = (void *)AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16)
  202. },
  203. {
  204. .compatible = "atmel,24c128",
  205. .data = (void *)AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16)
  206. },
  207. {
  208. .compatible = "atmel,24c256",
  209. .data = (void *)AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16)
  210. },
  211. {
  212. .compatible = "atmel,24c512",
  213. .data = (void *)AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16)
  214. },
  215. {
  216. .compatible = "atmel,24c1024",
  217. .data = (void *)AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16)
  218. },
  219. { },
  220. };
  221. MODULE_DEVICE_TABLE(of, at24_of_match);
  222. static const struct acpi_device_id at24_acpi_ids[] = {
  223. { "INT3499", AT24_DEVICE_MAGIC(8192 / 8, 0) },
  224. { }
  225. };
  226. MODULE_DEVICE_TABLE(acpi, at24_acpi_ids);
  227. /*-------------------------------------------------------------------------*/
  228. /*
  229. * This routine supports chips which consume multiple I2C addresses. It
  230. * computes the addressing information to be used for a given r/w request.
  231. * Assumes that sanity checks for offset happened at sysfs-layer.
  232. *
  233. * Slave address and byte offset derive from the offset. Always
  234. * set the byte address; on a multi-master board, another master
  235. * may have changed the chip's "current" address pointer.
  236. *
  237. * REVISIT some multi-address chips don't rollover page reads to
  238. * the next slave address, so we may need to truncate the count.
  239. * Those chips might need another quirk flag.
  240. *
  241. * If the real hardware used four adjacent 24c02 chips and that
  242. * were misconfigured as one 24c08, that would be a similar effect:
  243. * one "eeprom" file not four, but larger reads would fail when
  244. * they crossed certain pages.
  245. */
  246. static struct at24_client *at24_translate_offset(struct at24_data *at24,
  247. unsigned int *offset)
  248. {
  249. unsigned i;
  250. if (at24->chip.flags & AT24_FLAG_ADDR16) {
  251. i = *offset >> 16;
  252. *offset &= 0xffff;
  253. } else {
  254. i = *offset >> 8;
  255. *offset &= 0xff;
  256. }
  257. return &at24->client[i];
  258. }
  259. static ssize_t at24_regmap_read(struct at24_data *at24, char *buf,
  260. unsigned int offset, size_t count)
  261. {
  262. unsigned long timeout, read_time;
  263. struct at24_client *at24_client;
  264. struct i2c_client *client;
  265. struct regmap *regmap;
  266. int ret;
  267. at24_client = at24_translate_offset(at24, &offset);
  268. regmap = at24_client->regmap;
  269. client = at24_client->client;
  270. if (count > io_limit)
  271. count = io_limit;
  272. /* adjust offset for mac and serial read ops */
  273. offset += at24->offset_adj;
  274. loop_until_timeout(timeout, read_time) {
  275. ret = regmap_bulk_read(regmap, offset, buf, count);
  276. dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
  277. count, offset, ret, jiffies);
  278. if (!ret)
  279. return count;
  280. }
  281. return -ETIMEDOUT;
  282. }
  283. /*
  284. * Note that if the hardware write-protect pin is pulled high, the whole
  285. * chip is normally write protected. But there are plenty of product
  286. * variants here, including OTP fuses and partial chip protect.
  287. *
  288. * We only use page mode writes; the alternative is sloooow. These routines
  289. * write at most one page.
  290. */
  291. static size_t at24_adjust_write_count(struct at24_data *at24,
  292. unsigned int offset, size_t count)
  293. {
  294. unsigned next_page;
  295. /* write_max is at most a page */
  296. if (count > at24->write_max)
  297. count = at24->write_max;
  298. /* Never roll over backwards, to the start of this page */
  299. next_page = roundup(offset + 1, at24->chip.page_size);
  300. if (offset + count > next_page)
  301. count = next_page - offset;
  302. return count;
  303. }
  304. static ssize_t at24_regmap_write(struct at24_data *at24, const char *buf,
  305. unsigned int offset, size_t count)
  306. {
  307. unsigned long timeout, write_time;
  308. struct at24_client *at24_client;
  309. struct i2c_client *client;
  310. struct regmap *regmap;
  311. int ret;
  312. at24_client = at24_translate_offset(at24, &offset);
  313. regmap = at24_client->regmap;
  314. client = at24_client->client;
  315. count = at24_adjust_write_count(at24, offset, count);
  316. loop_until_timeout(timeout, write_time) {
  317. ret = regmap_bulk_write(regmap, offset, buf, count);
  318. dev_dbg(&client->dev, "write %zu@%d --> %d (%ld)\n",
  319. count, offset, ret, jiffies);
  320. if (!ret)
  321. return count;
  322. }
  323. return -ETIMEDOUT;
  324. }
  325. static int at24_read(void *priv, unsigned int off, void *val, size_t count)
  326. {
  327. struct at24_data *at24 = priv;
  328. struct device *dev = &at24->client[0].client->dev;
  329. char *buf = val;
  330. int ret;
  331. if (unlikely(!count))
  332. return count;
  333. if (off + count > at24->chip.byte_len)
  334. return -EINVAL;
  335. ret = pm_runtime_get_sync(dev);
  336. if (ret < 0) {
  337. pm_runtime_put_noidle(dev);
  338. return ret;
  339. }
  340. /*
  341. * Read data from chip, protecting against concurrent updates
  342. * from this host, but not from other I2C masters.
  343. */
  344. mutex_lock(&at24->lock);
  345. while (count) {
  346. int status;
  347. status = at24_regmap_read(at24, buf, off, count);
  348. if (status < 0) {
  349. mutex_unlock(&at24->lock);
  350. pm_runtime_put(dev);
  351. return status;
  352. }
  353. buf += status;
  354. off += status;
  355. count -= status;
  356. }
  357. mutex_unlock(&at24->lock);
  358. pm_runtime_put(dev);
  359. return 0;
  360. }
  361. static int at24_write(void *priv, unsigned int off, void *val, size_t count)
  362. {
  363. struct at24_data *at24 = priv;
  364. struct device *dev = &at24->client[0].client->dev;
  365. char *buf = val;
  366. int ret;
  367. if (unlikely(!count))
  368. return -EINVAL;
  369. if (off + count > at24->chip.byte_len)
  370. return -EINVAL;
  371. ret = pm_runtime_get_sync(dev);
  372. if (ret < 0) {
  373. pm_runtime_put_noidle(dev);
  374. return ret;
  375. }
  376. /*
  377. * Write data to chip, protecting against concurrent updates
  378. * from this host, but not from other I2C masters.
  379. */
  380. mutex_lock(&at24->lock);
  381. while (count) {
  382. int status;
  383. status = at24_regmap_write(at24, buf, off, count);
  384. if (status < 0) {
  385. mutex_unlock(&at24->lock);
  386. pm_runtime_put(dev);
  387. return status;
  388. }
  389. buf += status;
  390. off += status;
  391. count -= status;
  392. }
  393. mutex_unlock(&at24->lock);
  394. pm_runtime_put(dev);
  395. return 0;
  396. }
  397. static void at24_get_pdata(struct device *dev, struct at24_platform_data *chip)
  398. {
  399. int err;
  400. u32 val;
  401. if (device_property_present(dev, "read-only"))
  402. chip->flags |= AT24_FLAG_READONLY;
  403. err = device_property_read_u32(dev, "size", &val);
  404. if (!err)
  405. chip->byte_len = val;
  406. err = device_property_read_u32(dev, "pagesize", &val);
  407. if (!err) {
  408. chip->page_size = val;
  409. } else {
  410. /*
  411. * This is slow, but we can't know all eeproms, so we better
  412. * play safe. Specifying custom eeprom-types via platform_data
  413. * is recommended anyhow.
  414. */
  415. chip->page_size = 1;
  416. }
  417. }
  418. static unsigned int at24_get_offset_adj(u8 flags, unsigned int byte_len)
  419. {
  420. if (flags & AT24_FLAG_MAC) {
  421. /* EUI-48 starts from 0x9a, EUI-64 from 0x98 */
  422. return 0xa0 - byte_len;
  423. } else if (flags & AT24_FLAG_SERIAL && flags & AT24_FLAG_ADDR16) {
  424. /*
  425. * For 16 bit address pointers, the word address must contain
  426. * a '10' sequence in bits 11 and 10 regardless of the
  427. * intended position of the address pointer.
  428. */
  429. return 0x0800;
  430. } else if (flags & AT24_FLAG_SERIAL) {
  431. /*
  432. * Otherwise the word address must begin with a '10' sequence,
  433. * regardless of the intended address.
  434. */
  435. return 0x0080;
  436. } else {
  437. return 0;
  438. }
  439. }
  440. static const struct regmap_config regmap_config_8 = {
  441. .reg_bits = 8,
  442. .val_bits = 8,
  443. };
  444. static const struct regmap_config regmap_config_16 = {
  445. .reg_bits = 16,
  446. .val_bits = 8,
  447. };
  448. static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
  449. {
  450. struct at24_platform_data chip;
  451. kernel_ulong_t magic = 0;
  452. bool writable;
  453. struct at24_data *at24;
  454. int err;
  455. unsigned i, num_addresses;
  456. const struct regmap_config *config;
  457. u8 test_byte;
  458. if (client->dev.platform_data) {
  459. chip = *(struct at24_platform_data *)client->dev.platform_data;
  460. } else {
  461. /*
  462. * The I2C core allows OF nodes compatibles to match against the
  463. * I2C device ID table as a fallback, so check not only if an OF
  464. * node is present but also if it matches an OF device ID entry.
  465. */
  466. if (client->dev.of_node &&
  467. of_match_device(at24_of_match, &client->dev)) {
  468. magic = (kernel_ulong_t)
  469. of_device_get_match_data(&client->dev);
  470. } else if (id) {
  471. magic = id->driver_data;
  472. } else {
  473. const struct acpi_device_id *aid;
  474. aid = acpi_match_device(at24_acpi_ids, &client->dev);
  475. if (aid)
  476. magic = aid->driver_data;
  477. }
  478. if (!magic)
  479. return -ENODEV;
  480. chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN));
  481. magic >>= AT24_SIZE_BYTELEN;
  482. chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS);
  483. at24_get_pdata(&client->dev, &chip);
  484. chip.setup = NULL;
  485. chip.context = NULL;
  486. }
  487. if (!is_power_of_2(chip.byte_len))
  488. dev_warn(&client->dev,
  489. "byte_len looks suspicious (no power of 2)!\n");
  490. if (!chip.page_size) {
  491. dev_err(&client->dev, "page_size must not be 0!\n");
  492. return -EINVAL;
  493. }
  494. if (!is_power_of_2(chip.page_size))
  495. dev_warn(&client->dev,
  496. "page_size looks suspicious (no power of 2)!\n");
  497. /*
  498. * REVISIT: the size of the EUI-48 byte array is 6 in at24mac402, while
  499. * the call to ilog2() in AT24_DEVICE_MAGIC() rounds it down to 4.
  500. *
  501. * Eventually we'll get rid of the magic values altoghether in favor of
  502. * real structs, but for now just manually set the right size.
  503. */
  504. if (chip.flags & AT24_FLAG_MAC && chip.byte_len == 4)
  505. chip.byte_len = 6;
  506. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C) &&
  507. !i2c_check_functionality(client->adapter,
  508. I2C_FUNC_SMBUS_WRITE_I2C_BLOCK))
  509. chip.page_size = 1;
  510. if (chip.flags & AT24_FLAG_TAKE8ADDR)
  511. num_addresses = 8;
  512. else
  513. num_addresses = DIV_ROUND_UP(chip.byte_len,
  514. (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
  515. if (chip.flags & AT24_FLAG_ADDR16)
  516. config = &regmap_config_16;
  517. else
  518. config = &regmap_config_8;
  519. at24 = devm_kzalloc(&client->dev, sizeof(struct at24_data) +
  520. num_addresses * sizeof(struct at24_client), GFP_KERNEL);
  521. if (!at24)
  522. return -ENOMEM;
  523. mutex_init(&at24->lock);
  524. at24->chip = chip;
  525. at24->num_addresses = num_addresses;
  526. at24->offset_adj = at24_get_offset_adj(chip.flags, chip.byte_len);
  527. at24->client[0].client = client;
  528. at24->client[0].regmap = devm_regmap_init_i2c(client, config);
  529. if (IS_ERR(at24->client[0].regmap))
  530. return PTR_ERR(at24->client[0].regmap);
  531. if ((chip.flags & AT24_FLAG_SERIAL) && (chip.flags & AT24_FLAG_MAC)) {
  532. dev_err(&client->dev,
  533. "invalid device data - cannot have both AT24_FLAG_SERIAL & AT24_FLAG_MAC.");
  534. return -EINVAL;
  535. }
  536. writable = !(chip.flags & AT24_FLAG_READONLY);
  537. if (writable) {
  538. at24->write_max = min_t(unsigned int, chip.page_size, io_limit);
  539. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C) &&
  540. at24->write_max > I2C_SMBUS_BLOCK_MAX)
  541. at24->write_max = I2C_SMBUS_BLOCK_MAX;
  542. }
  543. /* use dummy devices for multiple-address chips */
  544. for (i = 1; i < num_addresses; i++) {
  545. at24->client[i].client = i2c_new_dummy(client->adapter,
  546. client->addr + i);
  547. if (!at24->client[i].client) {
  548. dev_err(&client->dev, "address 0x%02x unavailable\n",
  549. client->addr + i);
  550. err = -EADDRINUSE;
  551. goto err_clients;
  552. }
  553. at24->client[i].regmap = devm_regmap_init_i2c(
  554. at24->client[i].client, config);
  555. if (IS_ERR(at24->client[i].regmap)) {
  556. err = PTR_ERR(at24->client[i].regmap);
  557. goto err_clients;
  558. }
  559. }
  560. i2c_set_clientdata(client, at24);
  561. /* enable runtime pm */
  562. pm_runtime_set_active(&client->dev);
  563. pm_runtime_enable(&client->dev);
  564. /*
  565. * Perform a one-byte test read to verify that the
  566. * chip is functional.
  567. */
  568. err = at24_read(at24, 0, &test_byte, 1);
  569. pm_runtime_idle(&client->dev);
  570. if (err) {
  571. err = -ENODEV;
  572. goto err_clients;
  573. }
  574. at24->nvmem_config.name = dev_name(&client->dev);
  575. at24->nvmem_config.dev = &client->dev;
  576. at24->nvmem_config.read_only = !writable;
  577. at24->nvmem_config.root_only = true;
  578. at24->nvmem_config.owner = THIS_MODULE;
  579. at24->nvmem_config.compat = true;
  580. at24->nvmem_config.base_dev = &client->dev;
  581. at24->nvmem_config.reg_read = at24_read;
  582. at24->nvmem_config.reg_write = at24_write;
  583. at24->nvmem_config.priv = at24;
  584. at24->nvmem_config.stride = 1;
  585. at24->nvmem_config.word_size = 1;
  586. at24->nvmem_config.size = chip.byte_len;
  587. at24->nvmem = nvmem_register(&at24->nvmem_config);
  588. if (IS_ERR(at24->nvmem)) {
  589. err = PTR_ERR(at24->nvmem);
  590. goto err_clients;
  591. }
  592. dev_info(&client->dev, "%u byte %s EEPROM, %s, %u bytes/write\n",
  593. chip.byte_len, client->name,
  594. writable ? "writable" : "read-only", at24->write_max);
  595. /* export data to kernel code */
  596. if (chip.setup)
  597. chip.setup(at24->nvmem, chip.context);
  598. return 0;
  599. err_clients:
  600. for (i = 1; i < num_addresses; i++)
  601. if (at24->client[i].client)
  602. i2c_unregister_device(at24->client[i].client);
  603. pm_runtime_disable(&client->dev);
  604. return err;
  605. }
  606. static int at24_remove(struct i2c_client *client)
  607. {
  608. struct at24_data *at24;
  609. int i;
  610. at24 = i2c_get_clientdata(client);
  611. nvmem_unregister(at24->nvmem);
  612. for (i = 1; i < at24->num_addresses; i++)
  613. i2c_unregister_device(at24->client[i].client);
  614. pm_runtime_disable(&client->dev);
  615. pm_runtime_set_suspended(&client->dev);
  616. return 0;
  617. }
  618. /*-------------------------------------------------------------------------*/
  619. static struct i2c_driver at24_driver = {
  620. .driver = {
  621. .name = "at24",
  622. .of_match_table = at24_of_match,
  623. .acpi_match_table = ACPI_PTR(at24_acpi_ids),
  624. },
  625. .probe = at24_probe,
  626. .remove = at24_remove,
  627. .id_table = at24_ids,
  628. };
  629. static int __init at24_init(void)
  630. {
  631. if (!io_limit) {
  632. pr_err("at24: io_limit must not be 0!\n");
  633. return -EINVAL;
  634. }
  635. io_limit = rounddown_pow_of_two(io_limit);
  636. return i2c_add_driver(&at24_driver);
  637. }
  638. module_init(at24_init);
  639. static void __exit at24_exit(void)
  640. {
  641. i2c_del_driver(&at24_driver);
  642. }
  643. module_exit(at24_exit);
  644. MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
  645. MODULE_AUTHOR("David Brownell and Wolfram Sang");
  646. MODULE_LICENSE("GPL");