at24.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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/property.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. static const struct i2c_device_id at24_ids[] = {
  104. /* needs 8 addresses as A0-A2 are ignored */
  105. { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) },
  106. /* old variants can't be handled with this generic entry! */
  107. { "24c01", AT24_DEVICE_MAGIC(1024 / 8, 0) },
  108. { "24cs01", AT24_DEVICE_MAGIC(16,
  109. AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
  110. { "24c02", AT24_DEVICE_MAGIC(2048 / 8, 0) },
  111. { "24cs02", AT24_DEVICE_MAGIC(16,
  112. AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
  113. { "24mac402", AT24_DEVICE_MAGIC(48 / 8,
  114. AT24_FLAG_MAC | AT24_FLAG_READONLY) },
  115. { "24mac602", AT24_DEVICE_MAGIC(64 / 8,
  116. AT24_FLAG_MAC | AT24_FLAG_READONLY) },
  117. /* spd is a 24c02 in memory DIMMs */
  118. { "spd", AT24_DEVICE_MAGIC(2048 / 8,
  119. AT24_FLAG_READONLY | AT24_FLAG_IRUGO) },
  120. { "24c04", AT24_DEVICE_MAGIC(4096 / 8, 0) },
  121. { "24cs04", AT24_DEVICE_MAGIC(16,
  122. AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
  123. /* 24rf08 quirk is handled at i2c-core */
  124. { "24c08", AT24_DEVICE_MAGIC(8192 / 8, 0) },
  125. { "24cs08", AT24_DEVICE_MAGIC(16,
  126. AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
  127. { "24c16", AT24_DEVICE_MAGIC(16384 / 8, 0) },
  128. { "24cs16", AT24_DEVICE_MAGIC(16,
  129. AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
  130. { "24c32", AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) },
  131. { "24cs32", AT24_DEVICE_MAGIC(16,
  132. AT24_FLAG_ADDR16 |
  133. AT24_FLAG_SERIAL |
  134. AT24_FLAG_READONLY) },
  135. { "24c64", AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) },
  136. { "24cs64", AT24_DEVICE_MAGIC(16,
  137. AT24_FLAG_ADDR16 |
  138. AT24_FLAG_SERIAL |
  139. AT24_FLAG_READONLY) },
  140. { "24c128", AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16) },
  141. { "24c256", AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16) },
  142. { "24c512", AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16) },
  143. { "24c1024", AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16) },
  144. { "24c2048", AT24_DEVICE_MAGIC(2097152 / 8, AT24_FLAG_ADDR16) },
  145. { "at24", 0 },
  146. { /* END OF LIST */ }
  147. };
  148. MODULE_DEVICE_TABLE(i2c, at24_ids);
  149. static const struct acpi_device_id at24_acpi_ids[] = {
  150. { "INT3499", AT24_DEVICE_MAGIC(8192 / 8, 0) },
  151. { }
  152. };
  153. MODULE_DEVICE_TABLE(acpi, at24_acpi_ids);
  154. /*-------------------------------------------------------------------------*/
  155. /*
  156. * This routine supports chips which consume multiple I2C addresses. It
  157. * computes the addressing information to be used for a given r/w request.
  158. * Assumes that sanity checks for offset happened at sysfs-layer.
  159. *
  160. * Slave address and byte offset derive from the offset. Always
  161. * set the byte address; on a multi-master board, another master
  162. * may have changed the chip's "current" address pointer.
  163. *
  164. * REVISIT some multi-address chips don't rollover page reads to
  165. * the next slave address, so we may need to truncate the count.
  166. * Those chips might need another quirk flag.
  167. *
  168. * If the real hardware used four adjacent 24c02 chips and that
  169. * were misconfigured as one 24c08, that would be a similar effect:
  170. * one "eeprom" file not four, but larger reads would fail when
  171. * they crossed certain pages.
  172. */
  173. static struct i2c_client *at24_translate_offset(struct at24_data *at24,
  174. unsigned int *offset)
  175. {
  176. unsigned i;
  177. if (at24->chip.flags & AT24_FLAG_ADDR16) {
  178. i = *offset >> 16;
  179. *offset &= 0xffff;
  180. } else {
  181. i = *offset >> 8;
  182. *offset &= 0xff;
  183. }
  184. return at24->client[i];
  185. }
  186. static ssize_t at24_eeprom_read_smbus(struct at24_data *at24, char *buf,
  187. unsigned int offset, size_t count)
  188. {
  189. unsigned long timeout, read_time;
  190. struct i2c_client *client;
  191. int status;
  192. client = at24_translate_offset(at24, &offset);
  193. if (count > io_limit)
  194. count = io_limit;
  195. /* Smaller eeproms can work given some SMBus extension calls */
  196. if (count > I2C_SMBUS_BLOCK_MAX)
  197. count = I2C_SMBUS_BLOCK_MAX;
  198. timeout = jiffies + msecs_to_jiffies(write_timeout);
  199. do {
  200. /*
  201. * The timestamp shall be taken before the actual operation
  202. * to avoid a premature timeout in case of high CPU load.
  203. */
  204. read_time = jiffies;
  205. status = i2c_smbus_read_i2c_block_data_or_emulated(client,
  206. offset,
  207. count, buf);
  208. dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
  209. count, offset, status, jiffies);
  210. if (status == count)
  211. return count;
  212. usleep_range(1000, 1500);
  213. } while (time_before(read_time, timeout));
  214. return -ETIMEDOUT;
  215. }
  216. static ssize_t at24_eeprom_read_i2c(struct at24_data *at24, char *buf,
  217. unsigned int offset, size_t count)
  218. {
  219. unsigned long timeout, read_time;
  220. struct i2c_client *client;
  221. struct i2c_msg msg[2];
  222. int status, i;
  223. u8 msgbuf[2];
  224. memset(msg, 0, sizeof(msg));
  225. client = at24_translate_offset(at24, &offset);
  226. if (count > io_limit)
  227. count = io_limit;
  228. /*
  229. * When we have a better choice than SMBus calls, use a combined I2C
  230. * message. Write address; then read up to io_limit data bytes. Note
  231. * that read page rollover helps us here (unlike writes). msgbuf is
  232. * u8 and will cast to our needs.
  233. */
  234. i = 0;
  235. if (at24->chip.flags & AT24_FLAG_ADDR16)
  236. msgbuf[i++] = offset >> 8;
  237. msgbuf[i++] = offset;
  238. msg[0].addr = client->addr;
  239. msg[0].buf = msgbuf;
  240. msg[0].len = i;
  241. msg[1].addr = client->addr;
  242. msg[1].flags = I2C_M_RD;
  243. msg[1].buf = buf;
  244. msg[1].len = count;
  245. timeout = jiffies + msecs_to_jiffies(write_timeout);
  246. do {
  247. /*
  248. * The timestamp shall be taken before the actual operation
  249. * to avoid a premature timeout in case of high CPU load.
  250. */
  251. read_time = jiffies;
  252. status = i2c_transfer(client->adapter, msg, 2);
  253. if (status == 2)
  254. status = count;
  255. dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
  256. count, offset, status, jiffies);
  257. if (status == count)
  258. return count;
  259. usleep_range(1000, 1500);
  260. } while (time_before(read_time, timeout));
  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. timeout = jiffies + msecs_to_jiffies(write_timeout);
  303. do {
  304. /*
  305. * The timestamp shall be taken before the actual operation
  306. * to avoid a premature timeout in case of high CPU load.
  307. */
  308. read_time = jiffies;
  309. status = i2c_transfer(client->adapter, msg, 2);
  310. if (status == 2)
  311. return count;
  312. usleep_range(1000, 1500);
  313. } while (time_before(read_time, timeout));
  314. return -ETIMEDOUT;
  315. }
  316. static ssize_t at24_eeprom_read_mac(struct at24_data *at24, char *buf,
  317. unsigned int offset, size_t count)
  318. {
  319. unsigned long timeout, read_time;
  320. struct i2c_client *client;
  321. struct i2c_msg msg[2];
  322. u8 addrbuf[2];
  323. int status;
  324. client = at24_translate_offset(at24, &offset);
  325. memset(msg, 0, sizeof(msg));
  326. msg[0].addr = client->addr;
  327. msg[0].buf = addrbuf;
  328. /* EUI-48 starts from 0x9a, EUI-64 from 0x98 */
  329. addrbuf[0] = 0xa0 - at24->chip.byte_len + offset;
  330. msg[0].len = 1;
  331. msg[1].addr = client->addr;
  332. msg[1].flags = I2C_M_RD;
  333. msg[1].buf = buf;
  334. msg[1].len = count;
  335. timeout = jiffies + msecs_to_jiffies(write_timeout);
  336. do {
  337. /*
  338. * The timestamp shall be taken before the actual operation
  339. * to avoid a premature timeout in case of high CPU load.
  340. */
  341. read_time = jiffies;
  342. status = i2c_transfer(client->adapter, msg, 2);
  343. if (status == 2)
  344. return count;
  345. usleep_range(1000, 1500);
  346. } while (time_before(read_time, timeout));
  347. return -ETIMEDOUT;
  348. }
  349. /*
  350. * Note that if the hardware write-protect pin is pulled high, the whole
  351. * chip is normally write protected. But there are plenty of product
  352. * variants here, including OTP fuses and partial chip protect.
  353. *
  354. * We only use page mode writes; the alternative is sloooow. These routines
  355. * write at most one page.
  356. */
  357. static size_t at24_adjust_write_count(struct at24_data *at24,
  358. unsigned int offset, size_t count)
  359. {
  360. unsigned next_page;
  361. /* write_max is at most a page */
  362. if (count > at24->write_max)
  363. count = at24->write_max;
  364. /* Never roll over backwards, to the start of this page */
  365. next_page = roundup(offset + 1, at24->chip.page_size);
  366. if (offset + count > next_page)
  367. count = next_page - offset;
  368. return count;
  369. }
  370. static ssize_t at24_eeprom_write_smbus_block(struct at24_data *at24,
  371. const char *buf,
  372. unsigned int offset, size_t count)
  373. {
  374. unsigned long timeout, write_time;
  375. struct i2c_client *client;
  376. ssize_t status = 0;
  377. client = at24_translate_offset(at24, &offset);
  378. count = at24_adjust_write_count(at24, offset, count);
  379. timeout = jiffies + msecs_to_jiffies(write_timeout);
  380. do {
  381. /*
  382. * The timestamp shall be taken before the actual operation
  383. * to avoid a premature timeout in case of high CPU load.
  384. */
  385. write_time = jiffies;
  386. status = i2c_smbus_write_i2c_block_data(client,
  387. offset, count, buf);
  388. if (status == 0)
  389. status = count;
  390. dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
  391. count, offset, status, jiffies);
  392. if (status == count)
  393. return count;
  394. usleep_range(1000, 1500);
  395. } while (time_before(write_time, timeout));
  396. return -ETIMEDOUT;
  397. }
  398. static ssize_t at24_eeprom_write_smbus_byte(struct at24_data *at24,
  399. const char *buf,
  400. unsigned int offset, size_t count)
  401. {
  402. unsigned long timeout, write_time;
  403. struct i2c_client *client;
  404. ssize_t status = 0;
  405. client = at24_translate_offset(at24, &offset);
  406. timeout = jiffies + msecs_to_jiffies(write_timeout);
  407. do {
  408. /*
  409. * The timestamp shall be taken before the actual operation
  410. * to avoid a premature timeout in case of high CPU load.
  411. */
  412. write_time = jiffies;
  413. status = i2c_smbus_write_byte_data(client, offset, buf[0]);
  414. if (status == 0)
  415. status = count;
  416. dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
  417. count, offset, status, jiffies);
  418. if (status == count)
  419. return count;
  420. usleep_range(1000, 1500);
  421. } while (time_before(write_time, timeout));
  422. return -ETIMEDOUT;
  423. }
  424. static ssize_t at24_eeprom_write_i2c(struct at24_data *at24, const char *buf,
  425. unsigned int offset, size_t count)
  426. {
  427. unsigned long timeout, write_time;
  428. struct i2c_client *client;
  429. struct i2c_msg msg;
  430. ssize_t status = 0;
  431. int i = 0;
  432. client = at24_translate_offset(at24, &offset);
  433. count = at24_adjust_write_count(at24, offset, count);
  434. msg.addr = client->addr;
  435. msg.flags = 0;
  436. /* msg.buf is u8 and casts will mask the values */
  437. msg.buf = at24->writebuf;
  438. if (at24->chip.flags & AT24_FLAG_ADDR16)
  439. msg.buf[i++] = offset >> 8;
  440. msg.buf[i++] = offset;
  441. memcpy(&msg.buf[i], buf, count);
  442. msg.len = i + count;
  443. timeout = jiffies + msecs_to_jiffies(write_timeout);
  444. do {
  445. /*
  446. * The timestamp shall be taken before the actual operation
  447. * to avoid a premature timeout in case of high CPU load.
  448. */
  449. write_time = jiffies;
  450. status = i2c_transfer(client->adapter, &msg, 1);
  451. if (status == 1)
  452. status = count;
  453. dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
  454. count, offset, status, jiffies);
  455. if (status == count)
  456. return count;
  457. usleep_range(1000, 1500);
  458. } while (time_before(write_time, timeout));
  459. return -ETIMEDOUT;
  460. }
  461. static int at24_read(void *priv, unsigned int off, void *val, size_t count)
  462. {
  463. struct at24_data *at24 = priv;
  464. char *buf = val;
  465. if (unlikely(!count))
  466. return count;
  467. if (off + count > at24->chip.byte_len)
  468. return -EINVAL;
  469. /*
  470. * Read data from chip, protecting against concurrent updates
  471. * from this host, but not from other I2C masters.
  472. */
  473. mutex_lock(&at24->lock);
  474. while (count) {
  475. int status;
  476. status = at24->read_func(at24, buf, off, count);
  477. if (status < 0) {
  478. mutex_unlock(&at24->lock);
  479. return status;
  480. }
  481. buf += status;
  482. off += status;
  483. count -= status;
  484. }
  485. mutex_unlock(&at24->lock);
  486. return 0;
  487. }
  488. static int at24_write(void *priv, unsigned int off, void *val, size_t count)
  489. {
  490. struct at24_data *at24 = priv;
  491. char *buf = val;
  492. if (unlikely(!count))
  493. return -EINVAL;
  494. if (off + count > at24->chip.byte_len)
  495. return -EINVAL;
  496. /*
  497. * Write data to chip, protecting against concurrent updates
  498. * from this host, but not from other I2C masters.
  499. */
  500. mutex_lock(&at24->lock);
  501. while (count) {
  502. int status;
  503. status = at24->write_func(at24, buf, off, count);
  504. if (status < 0) {
  505. mutex_unlock(&at24->lock);
  506. return status;
  507. }
  508. buf += status;
  509. off += status;
  510. count -= status;
  511. }
  512. mutex_unlock(&at24->lock);
  513. return 0;
  514. }
  515. static void at24_get_pdata(struct device *dev, struct at24_platform_data *chip)
  516. {
  517. int err;
  518. u32 val;
  519. if (device_property_present(dev, "read-only"))
  520. chip->flags |= AT24_FLAG_READONLY;
  521. err = device_property_read_u32(dev, "address-width", &val);
  522. if (!err) {
  523. switch (val) {
  524. case 8:
  525. if (chip->flags & AT24_FLAG_ADDR16)
  526. dev_warn(dev, "Override address width to be 8, while default is 16\n");
  527. chip->flags &= ~AT24_FLAG_ADDR16;
  528. break;
  529. case 16:
  530. chip->flags |= AT24_FLAG_ADDR16;
  531. break;
  532. default:
  533. dev_warn(dev, "Bad \"address-width\" property: %u\n",
  534. val);
  535. }
  536. }
  537. err = device_property_read_u32(dev, "pagesize", &val);
  538. if (!err) {
  539. chip->page_size = val;
  540. } else {
  541. /*
  542. * This is slow, but we can't know all eeproms, so we better
  543. * play safe. Specifying custom eeprom-types via platform_data
  544. * is recommended anyhow.
  545. */
  546. chip->page_size = 1;
  547. }
  548. }
  549. static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
  550. {
  551. struct at24_platform_data chip;
  552. kernel_ulong_t magic = 0;
  553. bool writable;
  554. int use_smbus = 0;
  555. int use_smbus_write = 0;
  556. struct at24_data *at24;
  557. int err;
  558. unsigned i, num_addresses;
  559. u8 test_byte;
  560. if (client->dev.platform_data) {
  561. chip = *(struct at24_platform_data *)client->dev.platform_data;
  562. } else {
  563. if (id) {
  564. magic = id->driver_data;
  565. } else {
  566. const struct acpi_device_id *aid;
  567. aid = acpi_match_device(at24_acpi_ids, &client->dev);
  568. if (aid)
  569. magic = aid->driver_data;
  570. }
  571. if (!magic)
  572. return -ENODEV;
  573. chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN));
  574. magic >>= AT24_SIZE_BYTELEN;
  575. chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS);
  576. at24_get_pdata(&client->dev, &chip);
  577. chip.setup = NULL;
  578. chip.context = NULL;
  579. }
  580. if (!is_power_of_2(chip.byte_len))
  581. dev_warn(&client->dev,
  582. "byte_len looks suspicious (no power of 2)!\n");
  583. if (!chip.page_size) {
  584. dev_err(&client->dev, "page_size must not be 0!\n");
  585. return -EINVAL;
  586. }
  587. if (!is_power_of_2(chip.page_size))
  588. dev_warn(&client->dev,
  589. "page_size looks suspicious (no power of 2)!\n");
  590. /*
  591. * REVISIT: the size of the EUI-48 byte array is 6 in at24mac402, while
  592. * the call to ilog2() in AT24_DEVICE_MAGIC() rounds it down to 4.
  593. *
  594. * Eventually we'll get rid of the magic values altoghether in favor of
  595. * real structs, but for now just manually set the right size.
  596. */
  597. if (chip.flags & AT24_FLAG_MAC && chip.byte_len == 4)
  598. chip.byte_len = 6;
  599. /* Use I2C operations unless we're stuck with SMBus extensions. */
  600. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  601. if (chip.flags & AT24_FLAG_ADDR16)
  602. return -EPFNOSUPPORT;
  603. if (i2c_check_functionality(client->adapter,
  604. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  605. use_smbus = I2C_SMBUS_I2C_BLOCK_DATA;
  606. } else if (i2c_check_functionality(client->adapter,
  607. I2C_FUNC_SMBUS_READ_WORD_DATA)) {
  608. use_smbus = I2C_SMBUS_WORD_DATA;
  609. } else if (i2c_check_functionality(client->adapter,
  610. I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
  611. use_smbus = I2C_SMBUS_BYTE_DATA;
  612. } else {
  613. return -EPFNOSUPPORT;
  614. }
  615. if (i2c_check_functionality(client->adapter,
  616. I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
  617. use_smbus_write = I2C_SMBUS_I2C_BLOCK_DATA;
  618. } else if (i2c_check_functionality(client->adapter,
  619. I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {
  620. use_smbus_write = I2C_SMBUS_BYTE_DATA;
  621. chip.page_size = 1;
  622. }
  623. }
  624. if (chip.flags & AT24_FLAG_TAKE8ADDR)
  625. num_addresses = 8;
  626. else
  627. num_addresses = DIV_ROUND_UP(chip.byte_len,
  628. (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
  629. at24 = devm_kzalloc(&client->dev, sizeof(struct at24_data) +
  630. num_addresses * sizeof(struct i2c_client *), GFP_KERNEL);
  631. if (!at24)
  632. return -ENOMEM;
  633. mutex_init(&at24->lock);
  634. at24->use_smbus = use_smbus;
  635. at24->use_smbus_write = use_smbus_write;
  636. at24->chip = chip;
  637. at24->num_addresses = num_addresses;
  638. if ((chip.flags & AT24_FLAG_SERIAL) && (chip.flags & AT24_FLAG_MAC)) {
  639. dev_err(&client->dev,
  640. "invalid device data - cannot have both AT24_FLAG_SERIAL & AT24_FLAG_MAC.");
  641. return -EINVAL;
  642. }
  643. if (chip.flags & AT24_FLAG_SERIAL) {
  644. at24->read_func = at24_eeprom_read_serial;
  645. } else if (chip.flags & AT24_FLAG_MAC) {
  646. at24->read_func = at24_eeprom_read_mac;
  647. } else {
  648. at24->read_func = at24->use_smbus ? at24_eeprom_read_smbus
  649. : at24_eeprom_read_i2c;
  650. }
  651. if (at24->use_smbus) {
  652. if (at24->use_smbus_write == I2C_SMBUS_I2C_BLOCK_DATA)
  653. at24->write_func = at24_eeprom_write_smbus_block;
  654. else
  655. at24->write_func = at24_eeprom_write_smbus_byte;
  656. } else {
  657. at24->write_func = at24_eeprom_write_i2c;
  658. }
  659. writable = !(chip.flags & AT24_FLAG_READONLY);
  660. if (writable) {
  661. if (!use_smbus || use_smbus_write) {
  662. unsigned write_max = chip.page_size;
  663. if (write_max > io_limit)
  664. write_max = io_limit;
  665. if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX)
  666. write_max = I2C_SMBUS_BLOCK_MAX;
  667. at24->write_max = write_max;
  668. /* buffer (data + address at the beginning) */
  669. at24->writebuf = devm_kzalloc(&client->dev,
  670. write_max + 2, GFP_KERNEL);
  671. if (!at24->writebuf)
  672. return -ENOMEM;
  673. } else {
  674. dev_warn(&client->dev,
  675. "cannot write due to controller restrictions.");
  676. }
  677. }
  678. at24->client[0] = client;
  679. /* use dummy devices for multiple-address chips */
  680. for (i = 1; i < num_addresses; i++) {
  681. at24->client[i] = i2c_new_dummy(client->adapter,
  682. client->addr + i);
  683. if (!at24->client[i]) {
  684. dev_err(&client->dev, "address 0x%02x unavailable\n",
  685. client->addr + i);
  686. err = -EADDRINUSE;
  687. goto err_clients;
  688. }
  689. }
  690. i2c_set_clientdata(client, at24);
  691. /*
  692. * Perform a one-byte test read to verify that the
  693. * chip is functional.
  694. */
  695. err = at24_read(at24, 0, &test_byte, 1);
  696. if (err) {
  697. err = -ENODEV;
  698. goto err_clients;
  699. }
  700. at24->nvmem_config.name = dev_name(&client->dev);
  701. at24->nvmem_config.dev = &client->dev;
  702. at24->nvmem_config.read_only = !writable;
  703. at24->nvmem_config.root_only = !(chip.flags & AT24_FLAG_IRUGO);
  704. at24->nvmem_config.owner = THIS_MODULE;
  705. at24->nvmem_config.compat = true;
  706. at24->nvmem_config.base_dev = &client->dev;
  707. at24->nvmem_config.reg_read = at24_read;
  708. at24->nvmem_config.reg_write = at24_write;
  709. at24->nvmem_config.priv = at24;
  710. at24->nvmem_config.stride = 1;
  711. at24->nvmem_config.word_size = 1;
  712. at24->nvmem_config.size = chip.byte_len;
  713. at24->nvmem = nvmem_register(&at24->nvmem_config);
  714. if (IS_ERR(at24->nvmem)) {
  715. err = PTR_ERR(at24->nvmem);
  716. goto err_clients;
  717. }
  718. dev_info(&client->dev, "%u byte %s EEPROM, %s, %u bytes/write\n",
  719. chip.byte_len, client->name,
  720. writable ? "writable" : "read-only", at24->write_max);
  721. if (use_smbus == I2C_SMBUS_WORD_DATA ||
  722. use_smbus == I2C_SMBUS_BYTE_DATA) {
  723. dev_notice(&client->dev, "Falling back to %s reads, "
  724. "performance will suffer\n", use_smbus ==
  725. I2C_SMBUS_WORD_DATA ? "word" : "byte");
  726. }
  727. /* export data to kernel code */
  728. if (chip.setup)
  729. chip.setup(at24->nvmem, chip.context);
  730. return 0;
  731. err_clients:
  732. for (i = 1; i < num_addresses; i++)
  733. if (at24->client[i])
  734. i2c_unregister_device(at24->client[i]);
  735. return err;
  736. }
  737. static int at24_remove(struct i2c_client *client)
  738. {
  739. struct at24_data *at24;
  740. int i;
  741. at24 = i2c_get_clientdata(client);
  742. nvmem_unregister(at24->nvmem);
  743. for (i = 1; i < at24->num_addresses; i++)
  744. i2c_unregister_device(at24->client[i]);
  745. return 0;
  746. }
  747. /*-------------------------------------------------------------------------*/
  748. static struct i2c_driver at24_driver = {
  749. .driver = {
  750. .name = "at24",
  751. .acpi_match_table = ACPI_PTR(at24_acpi_ids),
  752. },
  753. .probe = at24_probe,
  754. .remove = at24_remove,
  755. .id_table = at24_ids,
  756. };
  757. static int __init at24_init(void)
  758. {
  759. if (!io_limit) {
  760. pr_err("at24: io_limit must not be 0!\n");
  761. return -EINVAL;
  762. }
  763. io_limit = rounddown_pow_of_two(io_limit);
  764. return i2c_add_driver(&at24_driver);
  765. }
  766. module_init(at24_init);
  767. static void __exit at24_exit(void)
  768. {
  769. i2c_del_driver(&at24_driver);
  770. }
  771. module_exit(at24_exit);
  772. MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
  773. MODULE_AUTHOR("David Brownell and Wolfram Sang");
  774. MODULE_LICENSE("GPL");