at24.c 23 KB

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