at24.c 25 KB

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