at24.c 22 KB

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