at24.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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. /*
  98. * Both reads and writes fail if the previous write didn't complete yet. This
  99. * macro loops a few times waiting at least long enough for one entire page
  100. * write to work while making sure that at least one iteration is run before
  101. * checking the break condition.
  102. *
  103. * It takes two parameters: a variable in which the future timeout in jiffies
  104. * will be stored and a temporary variable holding the time of the last
  105. * iteration of processing the request. Both should be unsigned integers
  106. * holding at least 32 bits.
  107. */
  108. #define at24_loop_until_timeout(tout, op_time) \
  109. for (tout = jiffies + msecs_to_jiffies(at24_write_timeout), \
  110. op_time = 0; \
  111. op_time ? time_before(op_time, tout) : true; \
  112. usleep_range(1000, 1500), op_time = jiffies)
  113. struct at24_chip_data {
  114. /*
  115. * these fields mirror their equivalents in
  116. * struct at24_platform_data
  117. */
  118. u32 byte_len;
  119. u8 flags;
  120. };
  121. #define AT24_CHIP_DATA(_name, _len, _flags) \
  122. static const struct at24_chip_data _name = { \
  123. .byte_len = _len, .flags = _flags, \
  124. }
  125. /* needs 8 addresses as A0-A2 are ignored */
  126. AT24_CHIP_DATA(at24_data_24c00, 128 / 8, AT24_FLAG_TAKE8ADDR);
  127. /* old variants can't be handled with this generic entry! */
  128. AT24_CHIP_DATA(at24_data_24c01, 1024 / 8, 0);
  129. AT24_CHIP_DATA(at24_data_24cs01, 16,
  130. AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
  131. AT24_CHIP_DATA(at24_data_24c02, 2048 / 8, 0);
  132. AT24_CHIP_DATA(at24_data_24cs02, 16,
  133. AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
  134. AT24_CHIP_DATA(at24_data_24mac402, 48 / 8,
  135. AT24_FLAG_MAC | AT24_FLAG_READONLY);
  136. AT24_CHIP_DATA(at24_data_24mac602, 64 / 8,
  137. AT24_FLAG_MAC | AT24_FLAG_READONLY);
  138. /* spd is a 24c02 in memory DIMMs */
  139. AT24_CHIP_DATA(at24_data_spd, 2048 / 8,
  140. AT24_FLAG_READONLY | AT24_FLAG_IRUGO);
  141. AT24_CHIP_DATA(at24_data_24c04, 4096 / 8, 0);
  142. AT24_CHIP_DATA(at24_data_24cs04, 16,
  143. AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
  144. /* 24rf08 quirk is handled at i2c-core */
  145. AT24_CHIP_DATA(at24_data_24c08, 8192 / 8, 0);
  146. AT24_CHIP_DATA(at24_data_24cs08, 16,
  147. AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
  148. AT24_CHIP_DATA(at24_data_24c16, 16384 / 8, 0);
  149. AT24_CHIP_DATA(at24_data_24cs16, 16,
  150. AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
  151. AT24_CHIP_DATA(at24_data_24c32, 32768 / 8, AT24_FLAG_ADDR16);
  152. AT24_CHIP_DATA(at24_data_24cs32, 16,
  153. AT24_FLAG_ADDR16 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
  154. AT24_CHIP_DATA(at24_data_24c64, 65536 / 8, AT24_FLAG_ADDR16);
  155. AT24_CHIP_DATA(at24_data_24cs64, 16,
  156. AT24_FLAG_ADDR16 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
  157. AT24_CHIP_DATA(at24_data_24c128, 131072 / 8, AT24_FLAG_ADDR16);
  158. AT24_CHIP_DATA(at24_data_24c256, 262144 / 8, AT24_FLAG_ADDR16);
  159. AT24_CHIP_DATA(at24_data_24c512, 524288 / 8, AT24_FLAG_ADDR16);
  160. AT24_CHIP_DATA(at24_data_24c1024, 1048576 / 8, AT24_FLAG_ADDR16);
  161. AT24_CHIP_DATA(at24_data_24c2048, 2097152 / 8, AT24_FLAG_ADDR16);
  162. /* identical to 24c08 ? */
  163. AT24_CHIP_DATA(at24_data_INT3499, 8192 / 8, 0);
  164. static const struct i2c_device_id at24_ids[] = {
  165. { "24c00", (kernel_ulong_t)&at24_data_24c00 },
  166. { "24c01", (kernel_ulong_t)&at24_data_24c01 },
  167. { "24cs01", (kernel_ulong_t)&at24_data_24cs01 },
  168. { "24c02", (kernel_ulong_t)&at24_data_24c02 },
  169. { "24cs02", (kernel_ulong_t)&at24_data_24cs02 },
  170. { "24mac402", (kernel_ulong_t)&at24_data_24mac402 },
  171. { "24mac602", (kernel_ulong_t)&at24_data_24mac602 },
  172. { "spd", (kernel_ulong_t)&at24_data_spd },
  173. { "24c04", (kernel_ulong_t)&at24_data_24c04 },
  174. { "24cs04", (kernel_ulong_t)&at24_data_24cs04 },
  175. { "24c08", (kernel_ulong_t)&at24_data_24c08 },
  176. { "24cs08", (kernel_ulong_t)&at24_data_24cs08 },
  177. { "24c16", (kernel_ulong_t)&at24_data_24c16 },
  178. { "24cs16", (kernel_ulong_t)&at24_data_24cs16 },
  179. { "24c32", (kernel_ulong_t)&at24_data_24c32 },
  180. { "24cs32", (kernel_ulong_t)&at24_data_24cs32 },
  181. { "24c64", (kernel_ulong_t)&at24_data_24c64 },
  182. { "24cs64", (kernel_ulong_t)&at24_data_24cs64 },
  183. { "24c128", (kernel_ulong_t)&at24_data_24c128 },
  184. { "24c256", (kernel_ulong_t)&at24_data_24c256 },
  185. { "24c512", (kernel_ulong_t)&at24_data_24c512 },
  186. { "24c1024", (kernel_ulong_t)&at24_data_24c1024 },
  187. { "24c2048", (kernel_ulong_t)&at24_data_24c2048 },
  188. { "at24", 0 },
  189. { /* END OF LIST */ }
  190. };
  191. MODULE_DEVICE_TABLE(i2c, at24_ids);
  192. static const struct of_device_id at24_of_match[] = {
  193. { .compatible = "atmel,24c00", .data = &at24_data_24c00 },
  194. { .compatible = "atmel,24c01", .data = &at24_data_24c01 },
  195. { .compatible = "atmel,24cs01", .data = &at24_data_24cs01 },
  196. { .compatible = "atmel,24c02", .data = &at24_data_24c02 },
  197. { .compatible = "atmel,24cs02", .data = &at24_data_24cs02 },
  198. { .compatible = "atmel,24mac402", .data = &at24_data_24mac402 },
  199. { .compatible = "atmel,24mac602", .data = &at24_data_24mac602 },
  200. { .compatible = "atmel,spd", .data = &at24_data_spd },
  201. { .compatible = "atmel,24c04", .data = &at24_data_24c04 },
  202. { .compatible = "atmel,24cs04", .data = &at24_data_24cs04 },
  203. { .compatible = "atmel,24c08", .data = &at24_data_24c08 },
  204. { .compatible = "atmel,24cs08", .data = &at24_data_24cs08 },
  205. { .compatible = "atmel,24c16", .data = &at24_data_24c16 },
  206. { .compatible = "atmel,24cs16", .data = &at24_data_24cs16 },
  207. { .compatible = "atmel,24c32", .data = &at24_data_24c32 },
  208. { .compatible = "atmel,24cs32", .data = &at24_data_24cs32 },
  209. { .compatible = "atmel,24c64", .data = &at24_data_24c64 },
  210. { .compatible = "atmel,24cs64", .data = &at24_data_24cs64 },
  211. { .compatible = "atmel,24c128", .data = &at24_data_24c128 },
  212. { .compatible = "atmel,24c256", .data = &at24_data_24c256 },
  213. { .compatible = "atmel,24c512", .data = &at24_data_24c512 },
  214. { .compatible = "atmel,24c1024", .data = &at24_data_24c1024 },
  215. { .compatible = "atmel,24c2048", .data = &at24_data_24c2048 },
  216. { /* END OF LIST */ },
  217. };
  218. MODULE_DEVICE_TABLE(of, at24_of_match);
  219. static const struct acpi_device_id at24_acpi_ids[] = {
  220. { "INT3499", (kernel_ulong_t)&at24_data_INT3499 },
  221. { /* END OF LIST */ }
  222. };
  223. MODULE_DEVICE_TABLE(acpi, at24_acpi_ids);
  224. /*
  225. * This routine supports chips which consume multiple I2C addresses. It
  226. * computes the addressing information to be used for a given r/w request.
  227. * Assumes that sanity checks for offset happened at sysfs-layer.
  228. *
  229. * Slave address and byte offset derive from the offset. Always
  230. * set the byte address; on a multi-master board, another master
  231. * may have changed the chip's "current" address pointer.
  232. */
  233. static struct at24_client *at24_translate_offset(struct at24_data *at24,
  234. unsigned int *offset)
  235. {
  236. unsigned int i;
  237. if (at24->flags & AT24_FLAG_ADDR16) {
  238. i = *offset >> 16;
  239. *offset &= 0xffff;
  240. } else {
  241. i = *offset >> 8;
  242. *offset &= 0xff;
  243. }
  244. return &at24->client[i];
  245. }
  246. static struct device *at24_base_client_dev(struct at24_data *at24)
  247. {
  248. return &at24->client[0].client->dev;
  249. }
  250. static size_t at24_adjust_read_count(struct at24_data *at24,
  251. unsigned int offset, size_t count)
  252. {
  253. unsigned int bits;
  254. size_t remainder;
  255. /*
  256. * In case of multi-address chips that don't rollover reads to
  257. * the next slave address: truncate the count to the slave boundary,
  258. * so that the read never straddles slaves.
  259. */
  260. if (at24->flags & AT24_FLAG_NO_RDROL) {
  261. bits = (at24->flags & AT24_FLAG_ADDR16) ? 16 : 8;
  262. remainder = BIT(bits) - offset;
  263. if (count > remainder)
  264. count = remainder;
  265. }
  266. if (count > at24_io_limit)
  267. count = at24_io_limit;
  268. return count;
  269. }
  270. static ssize_t at24_regmap_read(struct at24_data *at24, char *buf,
  271. unsigned int offset, size_t count)
  272. {
  273. unsigned long timeout, read_time;
  274. struct at24_client *at24_client;
  275. struct i2c_client *client;
  276. struct regmap *regmap;
  277. int ret;
  278. at24_client = at24_translate_offset(at24, &offset);
  279. regmap = at24_client->regmap;
  280. client = at24_client->client;
  281. count = at24_adjust_read_count(at24, offset, count);
  282. /* adjust offset for mac and serial read ops */
  283. offset += at24->offset_adj;
  284. at24_loop_until_timeout(timeout, read_time) {
  285. ret = regmap_bulk_read(regmap, offset, buf, count);
  286. dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
  287. count, offset, ret, jiffies);
  288. if (!ret)
  289. return count;
  290. }
  291. return -ETIMEDOUT;
  292. }
  293. /*
  294. * Note that if the hardware write-protect pin is pulled high, the whole
  295. * chip is normally write protected. But there are plenty of product
  296. * variants here, including OTP fuses and partial chip protect.
  297. *
  298. * We only use page mode writes; the alternative is sloooow. These routines
  299. * write at most one page.
  300. */
  301. static size_t at24_adjust_write_count(struct at24_data *at24,
  302. unsigned int offset, size_t count)
  303. {
  304. unsigned int next_page;
  305. /* write_max is at most a page */
  306. if (count > at24->write_max)
  307. count = at24->write_max;
  308. /* Never roll over backwards, to the start of this page */
  309. next_page = roundup(offset + 1, at24->page_size);
  310. if (offset + count > next_page)
  311. count = next_page - offset;
  312. return count;
  313. }
  314. static ssize_t at24_regmap_write(struct at24_data *at24, const char *buf,
  315. unsigned int offset, size_t count)
  316. {
  317. unsigned long timeout, write_time;
  318. struct at24_client *at24_client;
  319. struct i2c_client *client;
  320. struct regmap *regmap;
  321. int ret;
  322. at24_client = at24_translate_offset(at24, &offset);
  323. regmap = at24_client->regmap;
  324. client = at24_client->client;
  325. count = at24_adjust_write_count(at24, offset, count);
  326. at24_loop_until_timeout(timeout, write_time) {
  327. ret = regmap_bulk_write(regmap, offset, buf, count);
  328. dev_dbg(&client->dev, "write %zu@%d --> %d (%ld)\n",
  329. count, offset, ret, jiffies);
  330. if (!ret)
  331. return count;
  332. }
  333. return -ETIMEDOUT;
  334. }
  335. static int at24_read(void *priv, unsigned int off, void *val, size_t count)
  336. {
  337. struct at24_data *at24;
  338. struct device *dev;
  339. char *buf = val;
  340. int ret;
  341. at24 = priv;
  342. dev = at24_base_client_dev(at24);
  343. if (unlikely(!count))
  344. return count;
  345. if (off + count > at24->byte_len)
  346. return -EINVAL;
  347. ret = pm_runtime_get_sync(dev);
  348. if (ret < 0) {
  349. pm_runtime_put_noidle(dev);
  350. return ret;
  351. }
  352. /*
  353. * Read data from chip, protecting against concurrent updates
  354. * from this host, but not from other I2C masters.
  355. */
  356. mutex_lock(&at24->lock);
  357. while (count) {
  358. ret = at24_regmap_read(at24, buf, off, count);
  359. if (ret < 0) {
  360. mutex_unlock(&at24->lock);
  361. pm_runtime_put(dev);
  362. return ret;
  363. }
  364. buf += ret;
  365. off += ret;
  366. count -= ret;
  367. }
  368. mutex_unlock(&at24->lock);
  369. pm_runtime_put(dev);
  370. return 0;
  371. }
  372. static int at24_write(void *priv, unsigned int off, void *val, size_t count)
  373. {
  374. struct at24_data *at24;
  375. struct device *dev;
  376. char *buf = val;
  377. int ret;
  378. at24 = priv;
  379. dev = at24_base_client_dev(at24);
  380. if (unlikely(!count))
  381. return -EINVAL;
  382. if (off + count > at24->byte_len)
  383. return -EINVAL;
  384. ret = pm_runtime_get_sync(dev);
  385. if (ret < 0) {
  386. pm_runtime_put_noidle(dev);
  387. return ret;
  388. }
  389. /*
  390. * Write data to chip, protecting against concurrent updates
  391. * from this host, but not from other I2C masters.
  392. */
  393. mutex_lock(&at24->lock);
  394. gpiod_set_value_cansleep(at24->wp_gpio, 0);
  395. while (count) {
  396. ret = at24_regmap_write(at24, buf, off, count);
  397. if (ret < 0) {
  398. gpiod_set_value_cansleep(at24->wp_gpio, 1);
  399. mutex_unlock(&at24->lock);
  400. pm_runtime_put(dev);
  401. return ret;
  402. }
  403. buf += ret;
  404. off += ret;
  405. count -= ret;
  406. }
  407. gpiod_set_value_cansleep(at24->wp_gpio, 1);
  408. mutex_unlock(&at24->lock);
  409. pm_runtime_put(dev);
  410. return 0;
  411. }
  412. static void at24_properties_to_pdata(struct device *dev,
  413. struct at24_platform_data *chip)
  414. {
  415. int err;
  416. u32 val;
  417. if (device_property_present(dev, "read-only"))
  418. chip->flags |= AT24_FLAG_READONLY;
  419. if (device_property_present(dev, "no-read-rollover"))
  420. chip->flags |= AT24_FLAG_NO_RDROL;
  421. err = device_property_read_u32(dev, "address-width", &val);
  422. if (!err) {
  423. switch (val) {
  424. case 8:
  425. if (chip->flags & AT24_FLAG_ADDR16)
  426. dev_warn(dev, "Override address width to be 8, while default is 16\n");
  427. chip->flags &= ~AT24_FLAG_ADDR16;
  428. break;
  429. case 16:
  430. chip->flags |= AT24_FLAG_ADDR16;
  431. break;
  432. default:
  433. dev_warn(dev, "Bad \"address-width\" property: %u\n",
  434. val);
  435. }
  436. }
  437. err = device_property_read_u32(dev, "size", &val);
  438. if (!err)
  439. chip->byte_len = val;
  440. err = device_property_read_u32(dev, "pagesize", &val);
  441. if (!err) {
  442. chip->page_size = val;
  443. } else {
  444. /*
  445. * This is slow, but we can't know all eeproms, so we better
  446. * play safe. Specifying custom eeprom-types via platform_data
  447. * is recommended anyhow.
  448. */
  449. chip->page_size = 1;
  450. }
  451. }
  452. static int at24_get_pdata(struct device *dev, struct at24_platform_data *pdata)
  453. {
  454. struct device_node *of_node = dev->of_node;
  455. const struct at24_chip_data *cdata;
  456. const struct i2c_device_id *id;
  457. struct at24_platform_data *pd;
  458. pd = dev_get_platdata(dev);
  459. if (pd) {
  460. memcpy(pdata, pd, sizeof(*pdata));
  461. return 0;
  462. }
  463. id = i2c_match_id(at24_ids, to_i2c_client(dev));
  464. /*
  465. * The I2C core allows OF nodes compatibles to match against the
  466. * I2C device ID table as a fallback, so check not only if an OF
  467. * node is present but also if it matches an OF device ID entry.
  468. */
  469. if (of_node && of_match_device(at24_of_match, dev))
  470. cdata = of_device_get_match_data(dev);
  471. else if (id)
  472. cdata = (void *)id->driver_data;
  473. else
  474. cdata = acpi_device_get_match_data(dev);
  475. if (!cdata)
  476. return -ENODEV;
  477. pdata->byte_len = cdata->byte_len;
  478. pdata->flags = cdata->flags;
  479. at24_properties_to_pdata(dev, pdata);
  480. return 0;
  481. }
  482. static void at24_remove_dummy_clients(struct at24_data *at24)
  483. {
  484. int i;
  485. for (i = 1; i < at24->num_addresses; i++)
  486. i2c_unregister_device(at24->client[i].client);
  487. }
  488. static int at24_make_dummy_client(struct at24_data *at24, unsigned int index,
  489. struct regmap_config *regmap_config)
  490. {
  491. struct i2c_client *base_client, *dummy_client;
  492. unsigned short int addr;
  493. struct regmap *regmap;
  494. struct device *dev;
  495. base_client = at24->client[0].client;
  496. dev = &base_client->dev;
  497. addr = base_client->addr + index;
  498. dummy_client = i2c_new_dummy(base_client->adapter,
  499. base_client->addr + index);
  500. if (!dummy_client) {
  501. dev_err(dev, "address 0x%02x unavailable\n", addr);
  502. return -EADDRINUSE;
  503. }
  504. regmap = devm_regmap_init_i2c(dummy_client, regmap_config);
  505. if (IS_ERR(regmap)) {
  506. i2c_unregister_device(dummy_client);
  507. return PTR_ERR(regmap);
  508. }
  509. at24->client[index].client = dummy_client;
  510. at24->client[index].regmap = regmap;
  511. return 0;
  512. }
  513. static unsigned int at24_get_offset_adj(u8 flags, unsigned int byte_len)
  514. {
  515. if (flags & AT24_FLAG_MAC) {
  516. /* EUI-48 starts from 0x9a, EUI-64 from 0x98 */
  517. return 0xa0 - byte_len;
  518. } else if (flags & AT24_FLAG_SERIAL && flags & AT24_FLAG_ADDR16) {
  519. /*
  520. * For 16 bit address pointers, the word address must contain
  521. * a '10' sequence in bits 11 and 10 regardless of the
  522. * intended position of the address pointer.
  523. */
  524. return 0x0800;
  525. } else if (flags & AT24_FLAG_SERIAL) {
  526. /*
  527. * Otherwise the word address must begin with a '10' sequence,
  528. * regardless of the intended address.
  529. */
  530. return 0x0080;
  531. } else {
  532. return 0;
  533. }
  534. }
  535. static int at24_probe(struct i2c_client *client)
  536. {
  537. struct regmap_config regmap_config = { };
  538. struct nvmem_config nvmem_config = { };
  539. struct at24_platform_data pdata = { };
  540. struct device *dev = &client->dev;
  541. bool i2c_fn_i2c, i2c_fn_block;
  542. unsigned int i, num_addresses;
  543. struct at24_data *at24;
  544. struct regmap *regmap;
  545. size_t at24_size;
  546. bool writable;
  547. u8 test_byte;
  548. int err;
  549. i2c_fn_i2c = i2c_check_functionality(client->adapter, I2C_FUNC_I2C);
  550. i2c_fn_block = i2c_check_functionality(client->adapter,
  551. I2C_FUNC_SMBUS_WRITE_I2C_BLOCK);
  552. err = at24_get_pdata(dev, &pdata);
  553. if (err)
  554. return err;
  555. if (!i2c_fn_i2c && !i2c_fn_block)
  556. pdata.page_size = 1;
  557. if (!pdata.page_size) {
  558. dev_err(dev, "page_size must not be 0!\n");
  559. return -EINVAL;
  560. }
  561. if (!is_power_of_2(pdata.page_size))
  562. dev_warn(dev, "page_size looks suspicious (no power of 2)!\n");
  563. if (pdata.flags & AT24_FLAG_TAKE8ADDR)
  564. num_addresses = 8;
  565. else
  566. num_addresses = DIV_ROUND_UP(pdata.byte_len,
  567. (pdata.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
  568. if ((pdata.flags & AT24_FLAG_SERIAL) && (pdata.flags & AT24_FLAG_MAC)) {
  569. dev_err(dev,
  570. "invalid device data - cannot have both AT24_FLAG_SERIAL & AT24_FLAG_MAC.");
  571. return -EINVAL;
  572. }
  573. regmap_config.val_bits = 8;
  574. regmap_config.reg_bits = (pdata.flags & AT24_FLAG_ADDR16) ? 16 : 8;
  575. regmap_config.disable_locking = true;
  576. regmap = devm_regmap_init_i2c(client, &regmap_config);
  577. if (IS_ERR(regmap))
  578. return PTR_ERR(regmap);
  579. at24_size = sizeof(*at24) + num_addresses * sizeof(struct at24_client);
  580. at24 = devm_kzalloc(dev, at24_size, GFP_KERNEL);
  581. if (!at24)
  582. return -ENOMEM;
  583. mutex_init(&at24->lock);
  584. at24->byte_len = pdata.byte_len;
  585. at24->page_size = pdata.page_size;
  586. at24->flags = pdata.flags;
  587. at24->num_addresses = num_addresses;
  588. at24->offset_adj = at24_get_offset_adj(pdata.flags, pdata.byte_len);
  589. at24->client[0].client = client;
  590. at24->client[0].regmap = regmap;
  591. at24->wp_gpio = devm_gpiod_get_optional(dev, "wp", GPIOD_OUT_HIGH);
  592. if (IS_ERR(at24->wp_gpio))
  593. return PTR_ERR(at24->wp_gpio);
  594. writable = !(pdata.flags & AT24_FLAG_READONLY);
  595. if (writable) {
  596. at24->write_max = min_t(unsigned int,
  597. pdata.page_size, at24_io_limit);
  598. if (!i2c_fn_i2c && at24->write_max > I2C_SMBUS_BLOCK_MAX)
  599. at24->write_max = I2C_SMBUS_BLOCK_MAX;
  600. }
  601. /* use dummy devices for multiple-address chips */
  602. for (i = 1; i < num_addresses; i++) {
  603. err = at24_make_dummy_client(at24, i, &regmap_config);
  604. if (err) {
  605. at24_remove_dummy_clients(at24);
  606. return err;
  607. }
  608. }
  609. i2c_set_clientdata(client, at24);
  610. /* enable runtime pm */
  611. pm_runtime_set_active(dev);
  612. pm_runtime_enable(dev);
  613. /*
  614. * Perform a one-byte test read to verify that the
  615. * chip is functional.
  616. */
  617. err = at24_read(at24, 0, &test_byte, 1);
  618. pm_runtime_idle(dev);
  619. if (err) {
  620. err = -ENODEV;
  621. goto err_clients;
  622. }
  623. nvmem_config.name = dev_name(dev);
  624. nvmem_config.dev = dev;
  625. nvmem_config.read_only = !writable;
  626. nvmem_config.root_only = true;
  627. nvmem_config.owner = THIS_MODULE;
  628. nvmem_config.compat = true;
  629. nvmem_config.base_dev = dev;
  630. nvmem_config.reg_read = at24_read;
  631. nvmem_config.reg_write = at24_write;
  632. nvmem_config.priv = at24;
  633. nvmem_config.stride = 1;
  634. nvmem_config.word_size = 1;
  635. nvmem_config.size = pdata.byte_len;
  636. at24->nvmem = devm_nvmem_register(dev, &nvmem_config);
  637. if (IS_ERR(at24->nvmem)) {
  638. err = PTR_ERR(at24->nvmem);
  639. goto err_clients;
  640. }
  641. dev_info(dev, "%u byte %s EEPROM, %s, %u bytes/write\n",
  642. pdata.byte_len, client->name,
  643. writable ? "writable" : "read-only", at24->write_max);
  644. /* export data to kernel code */
  645. if (pdata.setup)
  646. pdata.setup(at24->nvmem, pdata.context);
  647. return 0;
  648. err_clients:
  649. at24_remove_dummy_clients(at24);
  650. pm_runtime_disable(dev);
  651. return err;
  652. }
  653. static int at24_remove(struct i2c_client *client)
  654. {
  655. struct at24_data *at24;
  656. at24 = i2c_get_clientdata(client);
  657. at24_remove_dummy_clients(at24);
  658. pm_runtime_disable(&client->dev);
  659. pm_runtime_set_suspended(&client->dev);
  660. return 0;
  661. }
  662. static struct i2c_driver at24_driver = {
  663. .driver = {
  664. .name = "at24",
  665. .of_match_table = at24_of_match,
  666. .acpi_match_table = ACPI_PTR(at24_acpi_ids),
  667. },
  668. .probe_new = at24_probe,
  669. .remove = at24_remove,
  670. .id_table = at24_ids,
  671. };
  672. static int __init at24_init(void)
  673. {
  674. if (!at24_io_limit) {
  675. pr_err("at24: at24_io_limit must not be 0!\n");
  676. return -EINVAL;
  677. }
  678. at24_io_limit = rounddown_pow_of_two(at24_io_limit);
  679. return i2c_add_driver(&at24_driver);
  680. }
  681. module_init(at24_init);
  682. static void __exit at24_exit(void)
  683. {
  684. i2c_del_driver(&at24_driver);
  685. }
  686. module_exit(at24_exit);
  687. MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
  688. MODULE_AUTHOR("David Brownell and Wolfram Sang");
  689. MODULE_LICENSE("GPL");