at24.c 22 KB

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