at24.c 26 KB

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