at24.c 26 KB

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