at24.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /*
  2. * at24.c - handle most I2C EEPROMs
  3. *
  4. * Copyright (C) 2005-2007 David Brownell
  5. * Copyright (C) 2008 Wolfram Sang, Pengutronix
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/of_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/delay.h>
  18. #include <linux/mutex.h>
  19. #include <linux/mod_devicetable.h>
  20. #include <linux/log2.h>
  21. #include <linux/bitops.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/property.h>
  24. #include <linux/acpi.h>
  25. #include <linux/i2c.h>
  26. #include <linux/nvmem-provider.h>
  27. #include <linux/platform_data/at24.h>
  28. #include <linux/pm_runtime.h>
  29. /*
  30. * I2C EEPROMs from most vendors are inexpensive and mostly interchangeable.
  31. * Differences between different vendor product lines (like Atmel AT24C or
  32. * MicroChip 24LC, etc) won't much matter for typical read/write access.
  33. * There are also I2C RAM chips, likewise interchangeable. One example
  34. * would be the PCF8570, which acts like a 24c02 EEPROM (256 bytes).
  35. *
  36. * However, misconfiguration can lose data. "Set 16-bit memory address"
  37. * to a part with 8-bit addressing will overwrite data. Writing with too
  38. * big a page size also loses data. And it's not safe to assume that the
  39. * conventional addresses 0x50..0x57 only hold eeproms; a PCF8563 RTC
  40. * uses 0x51, for just one example.
  41. *
  42. * Accordingly, explicit board-specific configuration data should be used
  43. * in almost all cases. (One partial exception is an SMBus used to access
  44. * "SPD" data for DRAM sticks. Those only use 24c02 EEPROMs.)
  45. *
  46. * So this driver uses "new style" I2C driver binding, expecting to be
  47. * told what devices exist. That may be in arch/X/mach-Y/board-Z.c or
  48. * similar kernel-resident tables; or, configuration data coming from
  49. * a bootloader.
  50. *
  51. * Other than binding model, current differences from "eeprom" driver are
  52. * that this one handles write access and isn't restricted to 24c02 devices.
  53. * It also handles larger devices (32 kbit and up) with two-byte addresses,
  54. * which won't work on pure SMBus systems.
  55. */
  56. struct at24_data {
  57. struct at24_platform_data chip;
  58. int use_smbus;
  59. int use_smbus_write;
  60. ssize_t (*read_func)(struct at24_data *, char *, unsigned int, size_t);
  61. ssize_t (*write_func)(struct at24_data *,
  62. const char *, unsigned int, size_t);
  63. /*
  64. * Lock protects against activities from other Linux tasks,
  65. * but not from changes by other I2C masters.
  66. */
  67. struct mutex lock;
  68. u8 *writebuf;
  69. unsigned write_max;
  70. unsigned num_addresses;
  71. struct nvmem_config nvmem_config;
  72. struct nvmem_device *nvmem;
  73. /*
  74. * Some chips tie up multiple I2C addresses; dummy devices reserve
  75. * them for us, and we'll use them with SMBus calls.
  76. */
  77. struct i2c_client *client[];
  78. };
  79. /*
  80. * This parameter is to help this driver avoid blocking other drivers out
  81. * of I2C for potentially troublesome amounts of time. With a 100 kHz I2C
  82. * clock, one 256 byte read takes about 1/43 second which is excessive;
  83. * but the 1/170 second it takes at 400 kHz may be quite reasonable; and
  84. * at 1 MHz (Fm+) a 1/430 second delay could easily be invisible.
  85. *
  86. * This value is forced to be a power of two so that writes align on pages.
  87. */
  88. static unsigned io_limit = 128;
  89. module_param(io_limit, uint, 0);
  90. MODULE_PARM_DESC(io_limit, "Maximum bytes per I/O (default 128)");
  91. /*
  92. * Specs often allow 5 msec for a page write, sometimes 20 msec;
  93. * it's important to recover from write timeouts.
  94. */
  95. static unsigned write_timeout = 25;
  96. module_param(write_timeout, uint, 0);
  97. MODULE_PARM_DESC(write_timeout, "Time (in ms) to try writes (default 25)");
  98. #define AT24_SIZE_BYTELEN 5
  99. #define AT24_SIZE_FLAGS 8
  100. #define AT24_BITMASK(x) (BIT(x) - 1)
  101. /* create non-zero magic value for given eeprom parameters */
  102. #define AT24_DEVICE_MAGIC(_len, _flags) \
  103. ((1 << AT24_SIZE_FLAGS | (_flags)) \
  104. << AT24_SIZE_BYTELEN | ilog2(_len))
  105. /*
  106. * Both reads and writes fail if the previous write didn't complete yet. This
  107. * macro loops a few times waiting at least long enough for one entire page
  108. * write to work while making sure that at least one iteration is run before
  109. * checking the break condition.
  110. *
  111. * It takes two parameters: a variable in which the future timeout in jiffies
  112. * will be stored and a temporary variable holding the time of the last
  113. * iteration of processing the request. Both should be unsigned integers
  114. * holding at least 32 bits.
  115. */
  116. #define loop_until_timeout(tout, op_time) \
  117. for (tout = jiffies + msecs_to_jiffies(write_timeout), op_time = 0; \
  118. op_time ? time_before(op_time, tout) : true; \
  119. usleep_range(1000, 1500), op_time = jiffies)
  120. static const struct i2c_device_id at24_ids[] = {
  121. /* needs 8 addresses as A0-A2 are ignored */
  122. { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) },
  123. /* old variants can't be handled with this generic entry! */
  124. { "24c01", AT24_DEVICE_MAGIC(1024 / 8, 0) },
  125. { "24cs01", AT24_DEVICE_MAGIC(16,
  126. AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
  127. { "24c02", AT24_DEVICE_MAGIC(2048 / 8, 0) },
  128. { "24cs02", AT24_DEVICE_MAGIC(16,
  129. AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
  130. { "24mac402", AT24_DEVICE_MAGIC(48 / 8,
  131. AT24_FLAG_MAC | AT24_FLAG_READONLY) },
  132. { "24mac602", AT24_DEVICE_MAGIC(64 / 8,
  133. AT24_FLAG_MAC | AT24_FLAG_READONLY) },
  134. /* spd is a 24c02 in memory DIMMs */
  135. { "spd", AT24_DEVICE_MAGIC(2048 / 8,
  136. AT24_FLAG_READONLY | AT24_FLAG_IRUGO) },
  137. { "24c04", AT24_DEVICE_MAGIC(4096 / 8, 0) },
  138. { "24cs04", AT24_DEVICE_MAGIC(16,
  139. AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
  140. /* 24rf08 quirk is handled at i2c-core */
  141. { "24c08", AT24_DEVICE_MAGIC(8192 / 8, 0) },
  142. { "24cs08", AT24_DEVICE_MAGIC(16,
  143. AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
  144. { "24c16", AT24_DEVICE_MAGIC(16384 / 8, 0) },
  145. { "24cs16", AT24_DEVICE_MAGIC(16,
  146. AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
  147. { "24c32", AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) },
  148. { "24cs32", AT24_DEVICE_MAGIC(16,
  149. AT24_FLAG_ADDR16 |
  150. AT24_FLAG_SERIAL |
  151. AT24_FLAG_READONLY) },
  152. { "24c64", AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) },
  153. { "24cs64", AT24_DEVICE_MAGIC(16,
  154. AT24_FLAG_ADDR16 |
  155. AT24_FLAG_SERIAL |
  156. AT24_FLAG_READONLY) },
  157. { "24c128", AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16) },
  158. { "24c256", AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16) },
  159. { "24c512", AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16) },
  160. { "24c1024", AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16) },
  161. { "at24", 0 },
  162. { /* END OF LIST */ }
  163. };
  164. MODULE_DEVICE_TABLE(i2c, at24_ids);
  165. static const struct of_device_id at24_of_match[] = {
  166. {
  167. .compatible = "atmel,24c00",
  168. .data = (void *)AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR)
  169. },
  170. {
  171. .compatible = "atmel,24c01",
  172. .data = (void *)AT24_DEVICE_MAGIC(1024 / 8, 0)
  173. },
  174. {
  175. .compatible = "atmel,24c02",
  176. .data = (void *)AT24_DEVICE_MAGIC(2048 / 8, 0)
  177. },
  178. {
  179. .compatible = "atmel,spd",
  180. .data = (void *)AT24_DEVICE_MAGIC(2048 / 8,
  181. AT24_FLAG_READONLY | AT24_FLAG_IRUGO)
  182. },
  183. {
  184. .compatible = "atmel,24c04",
  185. .data = (void *)AT24_DEVICE_MAGIC(4096 / 8, 0)
  186. },
  187. {
  188. .compatible = "atmel,24c08",
  189. .data = (void *)AT24_DEVICE_MAGIC(8192 / 8, 0)
  190. },
  191. {
  192. .compatible = "atmel,24c16",
  193. .data = (void *)AT24_DEVICE_MAGIC(16384 / 8, 0)
  194. },
  195. {
  196. .compatible = "atmel,24c32",
  197. .data = (void *)AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16)
  198. },
  199. {
  200. .compatible = "atmel,24c64",
  201. .data = (void *)AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16)
  202. },
  203. {
  204. .compatible = "atmel,24c128",
  205. .data = (void *)AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16)
  206. },
  207. {
  208. .compatible = "atmel,24c256",
  209. .data = (void *)AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16)
  210. },
  211. {
  212. .compatible = "atmel,24c512",
  213. .data = (void *)AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16)
  214. },
  215. {
  216. .compatible = "atmel,24c1024",
  217. .data = (void *)AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16)
  218. },
  219. { },
  220. };
  221. MODULE_DEVICE_TABLE(of, at24_of_match);
  222. static const struct acpi_device_id at24_acpi_ids[] = {
  223. { "INT3499", AT24_DEVICE_MAGIC(8192 / 8, 0) },
  224. { }
  225. };
  226. MODULE_DEVICE_TABLE(acpi, at24_acpi_ids);
  227. /*-------------------------------------------------------------------------*/
  228. /*
  229. * This routine supports chips which consume multiple I2C addresses. It
  230. * computes the addressing information to be used for a given r/w request.
  231. * Assumes that sanity checks for offset happened at sysfs-layer.
  232. *
  233. * Slave address and byte offset derive from the offset. Always
  234. * set the byte address; on a multi-master board, another master
  235. * may have changed the chip's "current" address pointer.
  236. *
  237. * REVISIT some multi-address chips don't rollover page reads to
  238. * the next slave address, so we may need to truncate the count.
  239. * Those chips might need another quirk flag.
  240. *
  241. * If the real hardware used four adjacent 24c02 chips and that
  242. * were misconfigured as one 24c08, that would be a similar effect:
  243. * one "eeprom" file not four, but larger reads would fail when
  244. * they crossed certain pages.
  245. */
  246. static struct i2c_client *at24_translate_offset(struct at24_data *at24,
  247. unsigned int *offset)
  248. {
  249. unsigned i;
  250. if (at24->chip.flags & AT24_FLAG_ADDR16) {
  251. i = *offset >> 16;
  252. *offset &= 0xffff;
  253. } else {
  254. i = *offset >> 8;
  255. *offset &= 0xff;
  256. }
  257. return at24->client[i];
  258. }
  259. static ssize_t at24_eeprom_read_smbus(struct at24_data *at24, char *buf,
  260. unsigned int offset, size_t count)
  261. {
  262. unsigned long timeout, read_time;
  263. struct i2c_client *client;
  264. int status;
  265. client = at24_translate_offset(at24, &offset);
  266. if (count > io_limit)
  267. count = io_limit;
  268. /* Smaller eeproms can work given some SMBus extension calls */
  269. if (count > I2C_SMBUS_BLOCK_MAX)
  270. count = I2C_SMBUS_BLOCK_MAX;
  271. loop_until_timeout(timeout, read_time) {
  272. status = i2c_smbus_read_i2c_block_data_or_emulated(client,
  273. offset,
  274. count, buf);
  275. dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
  276. count, offset, status, jiffies);
  277. if (status == count)
  278. return count;
  279. }
  280. return -ETIMEDOUT;
  281. }
  282. static ssize_t at24_eeprom_read_i2c(struct at24_data *at24, char *buf,
  283. unsigned int offset, size_t count)
  284. {
  285. unsigned long timeout, read_time;
  286. struct i2c_client *client;
  287. struct i2c_msg msg[2];
  288. int status, i;
  289. u8 msgbuf[2];
  290. memset(msg, 0, sizeof(msg));
  291. client = at24_translate_offset(at24, &offset);
  292. if (count > io_limit)
  293. count = io_limit;
  294. /*
  295. * When we have a better choice than SMBus calls, use a combined I2C
  296. * message. Write address; then read up to io_limit data bytes. Note
  297. * that read page rollover helps us here (unlike writes). msgbuf is
  298. * u8 and will cast to our needs.
  299. */
  300. i = 0;
  301. if (at24->chip.flags & AT24_FLAG_ADDR16)
  302. msgbuf[i++] = offset >> 8;
  303. msgbuf[i++] = offset;
  304. msg[0].addr = client->addr;
  305. msg[0].buf = msgbuf;
  306. msg[0].len = i;
  307. msg[1].addr = client->addr;
  308. msg[1].flags = I2C_M_RD;
  309. msg[1].buf = buf;
  310. msg[1].len = count;
  311. loop_until_timeout(timeout, read_time) {
  312. status = i2c_transfer(client->adapter, msg, 2);
  313. if (status == 2)
  314. status = count;
  315. dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
  316. count, offset, status, jiffies);
  317. if (status == count)
  318. return count;
  319. }
  320. return -ETIMEDOUT;
  321. }
  322. static ssize_t at24_eeprom_read_serial(struct at24_data *at24, char *buf,
  323. unsigned int offset, size_t count)
  324. {
  325. unsigned long timeout, read_time;
  326. struct i2c_client *client;
  327. struct i2c_msg msg[2];
  328. u8 addrbuf[2];
  329. int status;
  330. client = at24_translate_offset(at24, &offset);
  331. memset(msg, 0, sizeof(msg));
  332. msg[0].addr = client->addr;
  333. msg[0].buf = addrbuf;
  334. /*
  335. * The address pointer of the device is shared between the regular
  336. * EEPROM array and the serial number block. The dummy write (part of
  337. * the sequential read protocol) ensures the address pointer is reset
  338. * to the desired position.
  339. */
  340. if (at24->chip.flags & AT24_FLAG_ADDR16) {
  341. /*
  342. * For 16 bit address pointers, the word address must contain
  343. * a '10' sequence in bits 11 and 10 regardless of the
  344. * intended position of the address pointer.
  345. */
  346. addrbuf[0] = 0x08;
  347. addrbuf[1] = offset;
  348. msg[0].len = 2;
  349. } else {
  350. /*
  351. * Otherwise the word address must begin with a '10' sequence,
  352. * regardless of the intended address.
  353. */
  354. addrbuf[0] = 0x80 + offset;
  355. msg[0].len = 1;
  356. }
  357. msg[1].addr = client->addr;
  358. msg[1].flags = I2C_M_RD;
  359. msg[1].buf = buf;
  360. msg[1].len = count;
  361. loop_until_timeout(timeout, read_time) {
  362. status = i2c_transfer(client->adapter, msg, 2);
  363. if (status == 2)
  364. return count;
  365. }
  366. return -ETIMEDOUT;
  367. }
  368. static ssize_t at24_eeprom_read_mac(struct at24_data *at24, char *buf,
  369. unsigned int offset, size_t count)
  370. {
  371. unsigned long timeout, read_time;
  372. struct i2c_client *client;
  373. struct i2c_msg msg[2];
  374. u8 addrbuf[2];
  375. int status;
  376. client = at24_translate_offset(at24, &offset);
  377. memset(msg, 0, sizeof(msg));
  378. msg[0].addr = client->addr;
  379. msg[0].buf = addrbuf;
  380. addrbuf[0] = 0x90 + offset;
  381. msg[0].len = 1;
  382. msg[1].addr = client->addr;
  383. msg[1].flags = I2C_M_RD;
  384. msg[1].buf = buf;
  385. msg[1].len = count;
  386. loop_until_timeout(timeout, read_time) {
  387. status = i2c_transfer(client->adapter, msg, 2);
  388. if (status == 2)
  389. return count;
  390. }
  391. return -ETIMEDOUT;
  392. }
  393. /*
  394. * Note that if the hardware write-protect pin is pulled high, the whole
  395. * chip is normally write protected. But there are plenty of product
  396. * variants here, including OTP fuses and partial chip protect.
  397. *
  398. * We only use page mode writes; the alternative is sloooow. These routines
  399. * write at most one page.
  400. */
  401. static size_t at24_adjust_write_count(struct at24_data *at24,
  402. unsigned int offset, size_t count)
  403. {
  404. unsigned next_page;
  405. /* write_max is at most a page */
  406. if (count > at24->write_max)
  407. count = at24->write_max;
  408. /* Never roll over backwards, to the start of this page */
  409. next_page = roundup(offset + 1, at24->chip.page_size);
  410. if (offset + count > next_page)
  411. count = next_page - offset;
  412. return count;
  413. }
  414. static ssize_t at24_eeprom_write_smbus_block(struct at24_data *at24,
  415. const char *buf,
  416. unsigned int offset, size_t count)
  417. {
  418. unsigned long timeout, write_time;
  419. struct i2c_client *client;
  420. ssize_t status = 0;
  421. client = at24_translate_offset(at24, &offset);
  422. count = at24_adjust_write_count(at24, offset, count);
  423. loop_until_timeout(timeout, write_time) {
  424. status = i2c_smbus_write_i2c_block_data(client,
  425. offset, count, buf);
  426. if (status == 0)
  427. status = count;
  428. dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
  429. count, offset, status, jiffies);
  430. if (status == count)
  431. return count;
  432. }
  433. return -ETIMEDOUT;
  434. }
  435. static ssize_t at24_eeprom_write_smbus_byte(struct at24_data *at24,
  436. const char *buf,
  437. unsigned int offset, size_t count)
  438. {
  439. unsigned long timeout, write_time;
  440. struct i2c_client *client;
  441. ssize_t status = 0;
  442. client = at24_translate_offset(at24, &offset);
  443. loop_until_timeout(timeout, write_time) {
  444. status = i2c_smbus_write_byte_data(client, offset, buf[0]);
  445. if (status == 0)
  446. status = count;
  447. dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
  448. count, offset, status, jiffies);
  449. if (status == count)
  450. return count;
  451. }
  452. return -ETIMEDOUT;
  453. }
  454. static ssize_t at24_eeprom_write_i2c(struct at24_data *at24, const char *buf,
  455. unsigned int offset, size_t count)
  456. {
  457. unsigned long timeout, write_time;
  458. struct i2c_client *client;
  459. struct i2c_msg msg;
  460. ssize_t status = 0;
  461. int i = 0;
  462. client = at24_translate_offset(at24, &offset);
  463. count = at24_adjust_write_count(at24, offset, count);
  464. msg.addr = client->addr;
  465. msg.flags = 0;
  466. /* msg.buf is u8 and casts will mask the values */
  467. msg.buf = at24->writebuf;
  468. if (at24->chip.flags & AT24_FLAG_ADDR16)
  469. msg.buf[i++] = offset >> 8;
  470. msg.buf[i++] = offset;
  471. memcpy(&msg.buf[i], buf, count);
  472. msg.len = i + count;
  473. loop_until_timeout(timeout, write_time) {
  474. status = i2c_transfer(client->adapter, &msg, 1);
  475. if (status == 1)
  476. status = count;
  477. dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
  478. count, offset, status, jiffies);
  479. if (status == count)
  480. return count;
  481. }
  482. return -ETIMEDOUT;
  483. }
  484. static int at24_read(void *priv, unsigned int off, void *val, size_t count)
  485. {
  486. struct at24_data *at24 = priv;
  487. struct i2c_client *client;
  488. char *buf = val;
  489. int ret;
  490. if (unlikely(!count))
  491. return count;
  492. client = at24_translate_offset(at24, &off);
  493. ret = pm_runtime_get_sync(&client->dev);
  494. if (ret < 0) {
  495. pm_runtime_put_noidle(&client->dev);
  496. return ret;
  497. }
  498. /*
  499. * Read data from chip, protecting against concurrent updates
  500. * from this host, but not from other I2C masters.
  501. */
  502. mutex_lock(&at24->lock);
  503. while (count) {
  504. int status;
  505. status = at24->read_func(at24, buf, off, count);
  506. if (status < 0) {
  507. mutex_unlock(&at24->lock);
  508. pm_runtime_put(&client->dev);
  509. return status;
  510. }
  511. buf += status;
  512. off += status;
  513. count -= status;
  514. }
  515. mutex_unlock(&at24->lock);
  516. pm_runtime_put(&client->dev);
  517. return 0;
  518. }
  519. static int at24_write(void *priv, unsigned int off, void *val, size_t count)
  520. {
  521. struct at24_data *at24 = priv;
  522. struct i2c_client *client;
  523. char *buf = val;
  524. int ret;
  525. if (unlikely(!count))
  526. return -EINVAL;
  527. client = at24_translate_offset(at24, &off);
  528. ret = pm_runtime_get_sync(&client->dev);
  529. if (ret < 0) {
  530. pm_runtime_put_noidle(&client->dev);
  531. return ret;
  532. }
  533. /*
  534. * Write data to chip, protecting against concurrent updates
  535. * from this host, but not from other I2C masters.
  536. */
  537. mutex_lock(&at24->lock);
  538. while (count) {
  539. int status;
  540. status = at24->write_func(at24, buf, off, count);
  541. if (status < 0) {
  542. mutex_unlock(&at24->lock);
  543. pm_runtime_put(&client->dev);
  544. return status;
  545. }
  546. buf += status;
  547. off += status;
  548. count -= status;
  549. }
  550. mutex_unlock(&at24->lock);
  551. pm_runtime_put(&client->dev);
  552. return 0;
  553. }
  554. static void at24_get_pdata(struct device *dev, struct at24_platform_data *chip)
  555. {
  556. int err;
  557. u32 val;
  558. if (device_property_present(dev, "read-only"))
  559. chip->flags |= AT24_FLAG_READONLY;
  560. err = device_property_read_u32(dev, "size", &val);
  561. if (!err)
  562. chip->byte_len = val;
  563. err = device_property_read_u32(dev, "pagesize", &val);
  564. if (!err) {
  565. chip->page_size = val;
  566. } else {
  567. /*
  568. * This is slow, but we can't know all eeproms, so we better
  569. * play safe. Specifying custom eeprom-types via platform_data
  570. * is recommended anyhow.
  571. */
  572. chip->page_size = 1;
  573. }
  574. }
  575. static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
  576. {
  577. struct at24_platform_data chip;
  578. kernel_ulong_t magic = 0;
  579. bool writable;
  580. int use_smbus = 0;
  581. int use_smbus_write = 0;
  582. struct at24_data *at24;
  583. int err;
  584. unsigned i, num_addresses;
  585. u8 test_byte;
  586. if (client->dev.platform_data) {
  587. chip = *(struct at24_platform_data *)client->dev.platform_data;
  588. } else {
  589. /*
  590. * The I2C core allows OF nodes compatibles to match against the
  591. * I2C device ID table as a fallback, so check not only if an OF
  592. * node is present but also if it matches an OF device ID entry.
  593. */
  594. if (client->dev.of_node &&
  595. of_match_device(at24_of_match, &client->dev)) {
  596. magic = (kernel_ulong_t)
  597. of_device_get_match_data(&client->dev);
  598. } else if (id) {
  599. magic = id->driver_data;
  600. } else {
  601. const struct acpi_device_id *aid;
  602. aid = acpi_match_device(at24_acpi_ids, &client->dev);
  603. if (aid)
  604. magic = aid->driver_data;
  605. }
  606. if (!magic)
  607. return -ENODEV;
  608. chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN));
  609. magic >>= AT24_SIZE_BYTELEN;
  610. chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS);
  611. at24_get_pdata(&client->dev, &chip);
  612. chip.setup = NULL;
  613. chip.context = NULL;
  614. }
  615. if (!is_power_of_2(chip.byte_len))
  616. dev_warn(&client->dev,
  617. "byte_len looks suspicious (no power of 2)!\n");
  618. if (!chip.page_size) {
  619. dev_err(&client->dev, "page_size must not be 0!\n");
  620. return -EINVAL;
  621. }
  622. if (!is_power_of_2(chip.page_size))
  623. dev_warn(&client->dev,
  624. "page_size looks suspicious (no power of 2)!\n");
  625. /* Use I2C operations unless we're stuck with SMBus extensions. */
  626. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  627. if (chip.flags & AT24_FLAG_ADDR16)
  628. return -EPFNOSUPPORT;
  629. if (i2c_check_functionality(client->adapter,
  630. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  631. use_smbus = I2C_SMBUS_I2C_BLOCK_DATA;
  632. } else if (i2c_check_functionality(client->adapter,
  633. I2C_FUNC_SMBUS_READ_WORD_DATA)) {
  634. use_smbus = I2C_SMBUS_WORD_DATA;
  635. } else if (i2c_check_functionality(client->adapter,
  636. I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
  637. use_smbus = I2C_SMBUS_BYTE_DATA;
  638. } else {
  639. return -EPFNOSUPPORT;
  640. }
  641. if (i2c_check_functionality(client->adapter,
  642. I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
  643. use_smbus_write = I2C_SMBUS_I2C_BLOCK_DATA;
  644. } else if (i2c_check_functionality(client->adapter,
  645. I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {
  646. use_smbus_write = I2C_SMBUS_BYTE_DATA;
  647. chip.page_size = 1;
  648. }
  649. }
  650. if (chip.flags & AT24_FLAG_TAKE8ADDR)
  651. num_addresses = 8;
  652. else
  653. num_addresses = DIV_ROUND_UP(chip.byte_len,
  654. (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
  655. at24 = devm_kzalloc(&client->dev, sizeof(struct at24_data) +
  656. num_addresses * sizeof(struct i2c_client *), GFP_KERNEL);
  657. if (!at24)
  658. return -ENOMEM;
  659. mutex_init(&at24->lock);
  660. at24->use_smbus = use_smbus;
  661. at24->use_smbus_write = use_smbus_write;
  662. at24->chip = chip;
  663. at24->num_addresses = num_addresses;
  664. if ((chip.flags & AT24_FLAG_SERIAL) && (chip.flags & AT24_FLAG_MAC)) {
  665. dev_err(&client->dev,
  666. "invalid device data - cannot have both AT24_FLAG_SERIAL & AT24_FLAG_MAC.");
  667. return -EINVAL;
  668. }
  669. if (chip.flags & AT24_FLAG_SERIAL) {
  670. at24->read_func = at24_eeprom_read_serial;
  671. } else if (chip.flags & AT24_FLAG_MAC) {
  672. at24->read_func = at24_eeprom_read_mac;
  673. } else {
  674. at24->read_func = at24->use_smbus ? at24_eeprom_read_smbus
  675. : at24_eeprom_read_i2c;
  676. }
  677. if (at24->use_smbus) {
  678. if (at24->use_smbus_write == I2C_SMBUS_I2C_BLOCK_DATA)
  679. at24->write_func = at24_eeprom_write_smbus_block;
  680. else
  681. at24->write_func = at24_eeprom_write_smbus_byte;
  682. } else {
  683. at24->write_func = at24_eeprom_write_i2c;
  684. }
  685. writable = !(chip.flags & AT24_FLAG_READONLY);
  686. if (writable) {
  687. if (!use_smbus || use_smbus_write) {
  688. unsigned write_max = chip.page_size;
  689. if (write_max > io_limit)
  690. write_max = io_limit;
  691. if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX)
  692. write_max = I2C_SMBUS_BLOCK_MAX;
  693. at24->write_max = write_max;
  694. /* buffer (data + address at the beginning) */
  695. at24->writebuf = devm_kzalloc(&client->dev,
  696. write_max + 2, GFP_KERNEL);
  697. if (!at24->writebuf)
  698. return -ENOMEM;
  699. } else {
  700. dev_warn(&client->dev,
  701. "cannot write due to controller restrictions.");
  702. }
  703. }
  704. at24->client[0] = client;
  705. /* use dummy devices for multiple-address chips */
  706. for (i = 1; i < num_addresses; i++) {
  707. at24->client[i] = i2c_new_dummy(client->adapter,
  708. client->addr + i);
  709. if (!at24->client[i]) {
  710. dev_err(&client->dev, "address 0x%02x unavailable\n",
  711. client->addr + i);
  712. err = -EADDRINUSE;
  713. goto err_clients;
  714. }
  715. }
  716. i2c_set_clientdata(client, at24);
  717. /* enable runtime pm */
  718. pm_runtime_set_active(&client->dev);
  719. pm_runtime_enable(&client->dev);
  720. /*
  721. * Perform a one-byte test read to verify that the
  722. * chip is functional.
  723. */
  724. err = at24_read(at24, 0, &test_byte, 1);
  725. pm_runtime_idle(&client->dev);
  726. if (err) {
  727. err = -ENODEV;
  728. goto err_clients;
  729. }
  730. at24->nvmem_config.name = dev_name(&client->dev);
  731. at24->nvmem_config.dev = &client->dev;
  732. at24->nvmem_config.read_only = !writable;
  733. at24->nvmem_config.root_only = true;
  734. at24->nvmem_config.owner = THIS_MODULE;
  735. at24->nvmem_config.compat = true;
  736. at24->nvmem_config.base_dev = &client->dev;
  737. at24->nvmem_config.reg_read = at24_read;
  738. at24->nvmem_config.reg_write = at24_write;
  739. at24->nvmem_config.priv = at24;
  740. at24->nvmem_config.stride = 4;
  741. at24->nvmem_config.word_size = 1;
  742. at24->nvmem_config.size = chip.byte_len;
  743. at24->nvmem = nvmem_register(&at24->nvmem_config);
  744. if (IS_ERR(at24->nvmem)) {
  745. err = PTR_ERR(at24->nvmem);
  746. goto err_clients;
  747. }
  748. dev_info(&client->dev, "%u byte %s EEPROM, %s, %u bytes/write\n",
  749. chip.byte_len, client->name,
  750. writable ? "writable" : "read-only", at24->write_max);
  751. if (use_smbus == I2C_SMBUS_WORD_DATA ||
  752. use_smbus == I2C_SMBUS_BYTE_DATA) {
  753. dev_notice(&client->dev, "Falling back to %s reads, "
  754. "performance will suffer\n", use_smbus ==
  755. I2C_SMBUS_WORD_DATA ? "word" : "byte");
  756. }
  757. /* export data to kernel code */
  758. if (chip.setup)
  759. chip.setup(at24->nvmem, chip.context);
  760. return 0;
  761. err_clients:
  762. for (i = 1; i < num_addresses; i++)
  763. if (at24->client[i])
  764. i2c_unregister_device(at24->client[i]);
  765. pm_runtime_disable(&client->dev);
  766. return err;
  767. }
  768. static int at24_remove(struct i2c_client *client)
  769. {
  770. struct at24_data *at24;
  771. int i;
  772. at24 = i2c_get_clientdata(client);
  773. nvmem_unregister(at24->nvmem);
  774. for (i = 1; i < at24->num_addresses; i++)
  775. i2c_unregister_device(at24->client[i]);
  776. pm_runtime_disable(&client->dev);
  777. pm_runtime_set_suspended(&client->dev);
  778. return 0;
  779. }
  780. /*-------------------------------------------------------------------------*/
  781. static struct i2c_driver at24_driver = {
  782. .driver = {
  783. .name = "at24",
  784. .of_match_table = at24_of_match,
  785. .acpi_match_table = ACPI_PTR(at24_acpi_ids),
  786. },
  787. .probe = at24_probe,
  788. .remove = at24_remove,
  789. .id_table = at24_ids,
  790. };
  791. static int __init at24_init(void)
  792. {
  793. if (!io_limit) {
  794. pr_err("at24: io_limit must not be 0!\n");
  795. return -EINVAL;
  796. }
  797. io_limit = rounddown_pow_of_two(io_limit);
  798. return i2c_add_driver(&at24_driver);
  799. }
  800. module_init(at24_init);
  801. static void __exit at24_exit(void)
  802. {
  803. i2c_del_driver(&at24_driver);
  804. }
  805. module_exit(at24_exit);
  806. MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
  807. MODULE_AUTHOR("David Brownell and Wolfram Sang");
  808. MODULE_LICENSE("GPL");