at24.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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/sysfs.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/i2c.h>
  24. #include <linux/i2c/at24.h>
  25. /*
  26. * I2C EEPROMs from most vendors are inexpensive and mostly interchangeable.
  27. * Differences between different vendor product lines (like Atmel AT24C or
  28. * MicroChip 24LC, etc) won't much matter for typical read/write access.
  29. * There are also I2C RAM chips, likewise interchangeable. One example
  30. * would be the PCF8570, which acts like a 24c02 EEPROM (256 bytes).
  31. *
  32. * However, misconfiguration can lose data. "Set 16-bit memory address"
  33. * to a part with 8-bit addressing will overwrite data. Writing with too
  34. * big a page size also loses data. And it's not safe to assume that the
  35. * conventional addresses 0x50..0x57 only hold eeproms; a PCF8563 RTC
  36. * uses 0x51, for just one example.
  37. *
  38. * Accordingly, explicit board-specific configuration data should be used
  39. * in almost all cases. (One partial exception is an SMBus used to access
  40. * "SPD" data for DRAM sticks. Those only use 24c02 EEPROMs.)
  41. *
  42. * So this driver uses "new style" I2C driver binding, expecting to be
  43. * told what devices exist. That may be in arch/X/mach-Y/board-Z.c or
  44. * similar kernel-resident tables; or, configuration data coming from
  45. * a bootloader.
  46. *
  47. * Other than binding model, current differences from "eeprom" driver are
  48. * that this one handles write access and isn't restricted to 24c02 devices.
  49. * It also handles larger devices (32 kbit and up) with two-byte addresses,
  50. * which won't work on pure SMBus systems.
  51. */
  52. struct at24_data {
  53. struct at24_platform_data chip;
  54. bool use_smbus;
  55. /*
  56. * Lock protects against activities from other Linux tasks,
  57. * but not from changes by other I2C masters.
  58. */
  59. struct mutex lock;
  60. struct bin_attribute bin;
  61. u8 *writebuf;
  62. unsigned write_max;
  63. unsigned num_addresses;
  64. /*
  65. * Some chips tie up multiple I2C addresses; dummy devices reserve
  66. * them for us, and we'll use them with SMBus calls.
  67. */
  68. struct i2c_client *client[];
  69. };
  70. /*
  71. * This parameter is to help this driver avoid blocking other drivers out
  72. * of I2C for potentially troublesome amounts of time. With a 100 kHz I2C
  73. * clock, one 256 byte read takes about 1/43 second which is excessive;
  74. * but the 1/170 second it takes at 400 kHz may be quite reasonable; and
  75. * at 1 MHz (Fm+) a 1/430 second delay could easily be invisible.
  76. *
  77. * This value is forced to be a power of two so that writes align on pages.
  78. */
  79. static unsigned io_limit = 128;
  80. module_param(io_limit, uint, 0);
  81. MODULE_PARM_DESC(io_limit, "Maximum bytes per I/O (default 128)");
  82. /*
  83. * Specs often allow 5 msec for a page write, sometimes 20 msec;
  84. * it's important to recover from write timeouts.
  85. */
  86. static unsigned write_timeout = 25;
  87. module_param(write_timeout, uint, 0);
  88. MODULE_PARM_DESC(write_timeout, "Time (in ms) to try writes (default 25)");
  89. #define AT24_SIZE_BYTELEN 5
  90. #define AT24_SIZE_FLAGS 8
  91. #define AT24_BITMASK(x) (BIT(x) - 1)
  92. /* create non-zero magic value for given eeprom parameters */
  93. #define AT24_DEVICE_MAGIC(_len, _flags) \
  94. ((1 << AT24_SIZE_FLAGS | (_flags)) \
  95. << AT24_SIZE_BYTELEN | ilog2(_len))
  96. static const struct i2c_device_id at24_ids[] = {
  97. /* needs 8 addresses as A0-A2 are ignored */
  98. { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) },
  99. /* old variants can't be handled with this generic entry! */
  100. { "24c01", AT24_DEVICE_MAGIC(1024 / 8, 0) },
  101. { "24c02", AT24_DEVICE_MAGIC(2048 / 8, 0) },
  102. /* spd is a 24c02 in memory DIMMs */
  103. { "spd", AT24_DEVICE_MAGIC(2048 / 8,
  104. AT24_FLAG_READONLY | AT24_FLAG_IRUGO) },
  105. { "24c04", AT24_DEVICE_MAGIC(4096 / 8, 0) },
  106. /* 24rf08 quirk is handled at i2c-core */
  107. { "24c08", AT24_DEVICE_MAGIC(8192 / 8, 0) },
  108. { "24c16", AT24_DEVICE_MAGIC(16384 / 8, 0) },
  109. { "24c32", AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) },
  110. { "24c64", AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) },
  111. { "24c128", AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16) },
  112. { "24c256", AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16) },
  113. { "24c512", AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16) },
  114. { "24c1024", AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16) },
  115. { "at24", 0 },
  116. { /* END OF LIST */ }
  117. };
  118. MODULE_DEVICE_TABLE(i2c, at24_ids);
  119. /*-------------------------------------------------------------------------*/
  120. /*
  121. * This routine supports chips which consume multiple I2C addresses. It
  122. * computes the addressing information to be used for a given r/w request.
  123. * Assumes that sanity checks for offset happened at sysfs-layer.
  124. */
  125. static struct i2c_client *at24_translate_offset(struct at24_data *at24,
  126. unsigned *offset)
  127. {
  128. unsigned i;
  129. if (at24->chip.flags & AT24_FLAG_ADDR16) {
  130. i = *offset >> 16;
  131. *offset &= 0xffff;
  132. } else {
  133. i = *offset >> 8;
  134. *offset &= 0xff;
  135. }
  136. return at24->client[i];
  137. }
  138. static ssize_t at24_eeprom_read(struct at24_data *at24, char *buf,
  139. unsigned offset, size_t count)
  140. {
  141. struct i2c_msg msg[2];
  142. u8 msgbuf[2];
  143. struct i2c_client *client;
  144. int status, i;
  145. memset(msg, 0, sizeof(msg));
  146. /*
  147. * REVISIT some multi-address chips don't rollover page reads to
  148. * the next slave address, so we may need to truncate the count.
  149. * Those chips might need another quirk flag.
  150. *
  151. * If the real hardware used four adjacent 24c02 chips and that
  152. * were misconfigured as one 24c08, that would be a similar effect:
  153. * one "eeprom" file not four, but larger reads would fail when
  154. * they crossed certain pages.
  155. */
  156. /*
  157. * Slave address and byte offset derive from the offset. Always
  158. * set the byte address; on a multi-master board, another master
  159. * may have changed the chip's "current" address pointer.
  160. */
  161. client = at24_translate_offset(at24, &offset);
  162. if (count > io_limit)
  163. count = io_limit;
  164. /* Smaller eeproms can work given some SMBus extension calls */
  165. if (at24->use_smbus) {
  166. if (count > I2C_SMBUS_BLOCK_MAX)
  167. count = I2C_SMBUS_BLOCK_MAX;
  168. status = i2c_smbus_read_i2c_block_data(client, offset,
  169. count, buf);
  170. dev_dbg(&client->dev, "smbus read %zu@%d --> %d\n",
  171. count, offset, status);
  172. return (status < 0) ? -EIO : status;
  173. }
  174. /*
  175. * When we have a better choice than SMBus calls, use a combined
  176. * I2C message. Write address; then read up to io_limit data bytes.
  177. * Note that read page rollover helps us here (unlike writes).
  178. * msgbuf is u8 and will cast to our needs.
  179. */
  180. i = 0;
  181. if (at24->chip.flags & AT24_FLAG_ADDR16)
  182. msgbuf[i++] = offset >> 8;
  183. msgbuf[i++] = offset;
  184. msg[0].addr = client->addr;
  185. msg[0].buf = msgbuf;
  186. msg[0].len = i;
  187. msg[1].addr = client->addr;
  188. msg[1].flags = I2C_M_RD;
  189. msg[1].buf = buf;
  190. msg[1].len = count;
  191. status = i2c_transfer(client->adapter, msg, 2);
  192. dev_dbg(&client->dev, "i2c read %zu@%d --> %d\n",
  193. count, offset, status);
  194. if (status == 2)
  195. return count;
  196. else if (status >= 0)
  197. return -EIO;
  198. else
  199. return status;
  200. }
  201. static ssize_t at24_bin_read(struct kobject *kobj, struct bin_attribute *attr,
  202. char *buf, loff_t off, size_t count)
  203. {
  204. struct at24_data *at24;
  205. ssize_t retval = 0;
  206. at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
  207. if (unlikely(!count))
  208. return count;
  209. /*
  210. * Read data from chip, protecting against concurrent updates
  211. * from this host, but not from other I2C masters.
  212. */
  213. mutex_lock(&at24->lock);
  214. while (count) {
  215. ssize_t status;
  216. status = at24_eeprom_read(at24, buf, off, count);
  217. if (status <= 0) {
  218. if (retval == 0)
  219. retval = status;
  220. break;
  221. }
  222. buf += status;
  223. off += status;
  224. count -= status;
  225. retval += status;
  226. }
  227. mutex_unlock(&at24->lock);
  228. return retval;
  229. }
  230. /*
  231. * REVISIT: export at24_bin{read,write}() to let other kernel code use
  232. * eeprom data. For example, it might hold a board's Ethernet address, or
  233. * board-specific calibration data generated on the manufacturing floor.
  234. */
  235. /*
  236. * Note that if the hardware write-protect pin is pulled high, the whole
  237. * chip is normally write protected. But there are plenty of product
  238. * variants here, including OTP fuses and partial chip protect.
  239. *
  240. * We only use page mode writes; the alternative is sloooow. This routine
  241. * writes at most one page.
  242. */
  243. static ssize_t at24_eeprom_write(struct at24_data *at24, char *buf,
  244. unsigned offset, size_t count)
  245. {
  246. struct i2c_client *client;
  247. struct i2c_msg msg;
  248. ssize_t status;
  249. unsigned long timeout, write_time;
  250. unsigned next_page;
  251. /* Get corresponding I2C address and adjust offset */
  252. client = at24_translate_offset(at24, &offset);
  253. /* write_max is at most a page */
  254. if (count > at24->write_max)
  255. count = at24->write_max;
  256. /* Never roll over backwards, to the start of this page */
  257. next_page = roundup(offset + 1, at24->chip.page_size);
  258. if (offset + count > next_page)
  259. count = next_page - offset;
  260. /* If we'll use I2C calls for I/O, set up the message */
  261. if (!at24->use_smbus) {
  262. int i = 0;
  263. msg.addr = client->addr;
  264. msg.flags = 0;
  265. /* msg.buf is u8 and casts will mask the values */
  266. msg.buf = at24->writebuf;
  267. if (at24->chip.flags & AT24_FLAG_ADDR16)
  268. msg.buf[i++] = offset >> 8;
  269. msg.buf[i++] = offset;
  270. memcpy(&msg.buf[i], buf, count);
  271. msg.len = i + count;
  272. }
  273. /*
  274. * Writes fail if the previous one didn't complete yet. We may
  275. * loop a few times until this one succeeds, waiting at least
  276. * long enough for one entire page write to work.
  277. */
  278. timeout = jiffies + msecs_to_jiffies(write_timeout);
  279. do {
  280. write_time = jiffies;
  281. if (at24->use_smbus) {
  282. status = i2c_smbus_write_i2c_block_data(client,
  283. offset, count, buf);
  284. if (status == 0)
  285. status = count;
  286. } else {
  287. status = i2c_transfer(client->adapter, &msg, 1);
  288. if (status == 1)
  289. status = count;
  290. }
  291. dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
  292. count, offset, status, jiffies);
  293. if (status == count)
  294. return count;
  295. /* REVISIT: at HZ=100, this is sloooow */
  296. msleep(1);
  297. } while (time_before(write_time, timeout));
  298. return -ETIMEDOUT;
  299. }
  300. static ssize_t at24_bin_write(struct kobject *kobj, struct bin_attribute *attr,
  301. char *buf, loff_t off, size_t count)
  302. {
  303. struct at24_data *at24;
  304. ssize_t retval = 0;
  305. at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
  306. if (unlikely(!count))
  307. return count;
  308. /*
  309. * Write data to chip, protecting against concurrent updates
  310. * from this host, but not from other I2C masters.
  311. */
  312. mutex_lock(&at24->lock);
  313. while (count) {
  314. ssize_t status;
  315. status = at24_eeprom_write(at24, buf, off, count);
  316. if (status <= 0) {
  317. if (retval == 0)
  318. retval = status;
  319. break;
  320. }
  321. buf += status;
  322. off += status;
  323. count -= status;
  324. retval += status;
  325. }
  326. mutex_unlock(&at24->lock);
  327. return retval;
  328. }
  329. /*-------------------------------------------------------------------------*/
  330. static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
  331. {
  332. struct at24_platform_data chip;
  333. bool writable;
  334. bool use_smbus = false;
  335. struct at24_data *at24;
  336. int err;
  337. unsigned i, num_addresses;
  338. kernel_ulong_t magic;
  339. if (client->dev.platform_data) {
  340. chip = *(struct at24_platform_data *)client->dev.platform_data;
  341. } else {
  342. if (!id->driver_data) {
  343. err = -ENODEV;
  344. goto err_out;
  345. }
  346. magic = id->driver_data;
  347. chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN));
  348. magic >>= AT24_SIZE_BYTELEN;
  349. chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS);
  350. /*
  351. * This is slow, but we can't know all eeproms, so we better
  352. * play safe. Specifying custom eeprom-types via platform_data
  353. * is recommended anyhow.
  354. */
  355. chip.page_size = 1;
  356. }
  357. if (!is_power_of_2(chip.byte_len))
  358. dev_warn(&client->dev,
  359. "byte_len looks suspicious (no power of 2)!\n");
  360. if (!is_power_of_2(chip.page_size))
  361. dev_warn(&client->dev,
  362. "page_size looks suspicious (no power of 2)!\n");
  363. /* Use I2C operations unless we're stuck with SMBus extensions. */
  364. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  365. if (chip.flags & AT24_FLAG_ADDR16) {
  366. err = -EPFNOSUPPORT;
  367. goto err_out;
  368. }
  369. if (!i2c_check_functionality(client->adapter,
  370. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  371. err = -EPFNOSUPPORT;
  372. goto err_out;
  373. }
  374. use_smbus = true;
  375. }
  376. if (chip.flags & AT24_FLAG_TAKE8ADDR)
  377. num_addresses = 8;
  378. else
  379. num_addresses = DIV_ROUND_UP(chip.byte_len,
  380. (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
  381. at24 = kzalloc(sizeof(struct at24_data) +
  382. num_addresses * sizeof(struct i2c_client *), GFP_KERNEL);
  383. if (!at24) {
  384. err = -ENOMEM;
  385. goto err_out;
  386. }
  387. mutex_init(&at24->lock);
  388. at24->use_smbus = use_smbus;
  389. at24->chip = chip;
  390. at24->num_addresses = num_addresses;
  391. /*
  392. * Export the EEPROM bytes through sysfs, since that's convenient.
  393. * By default, only root should see the data (maybe passwords etc)
  394. */
  395. at24->bin.attr.name = "eeprom";
  396. at24->bin.attr.mode = chip.flags & AT24_FLAG_IRUGO ? S_IRUGO : S_IRUSR;
  397. at24->bin.attr.owner = THIS_MODULE;
  398. at24->bin.read = at24_bin_read;
  399. at24->bin.size = chip.byte_len;
  400. writable = !(chip.flags & AT24_FLAG_READONLY);
  401. if (writable) {
  402. if (!use_smbus || i2c_check_functionality(client->adapter,
  403. I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
  404. unsigned write_max = chip.page_size;
  405. at24->bin.write = at24_bin_write;
  406. at24->bin.attr.mode |= S_IWUSR;
  407. if (write_max > io_limit)
  408. write_max = io_limit;
  409. if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX)
  410. write_max = I2C_SMBUS_BLOCK_MAX;
  411. at24->write_max = write_max;
  412. /* buffer (data + address at the beginning) */
  413. at24->writebuf = kmalloc(write_max + 2, GFP_KERNEL);
  414. if (!at24->writebuf) {
  415. err = -ENOMEM;
  416. goto err_struct;
  417. }
  418. } else {
  419. dev_warn(&client->dev,
  420. "cannot write due to controller restrictions.");
  421. }
  422. }
  423. at24->client[0] = client;
  424. /* use dummy devices for multiple-address chips */
  425. for (i = 1; i < num_addresses; i++) {
  426. at24->client[i] = i2c_new_dummy(client->adapter,
  427. client->addr + i);
  428. if (!at24->client[i]) {
  429. dev_err(&client->dev, "address 0x%02x unavailable\n",
  430. client->addr + i);
  431. err = -EADDRINUSE;
  432. goto err_clients;
  433. }
  434. }
  435. err = sysfs_create_bin_file(&client->dev.kobj, &at24->bin);
  436. if (err)
  437. goto err_clients;
  438. i2c_set_clientdata(client, at24);
  439. dev_info(&client->dev, "%zu byte %s EEPROM %s\n",
  440. at24->bin.size, client->name,
  441. writable ? "(writable)" : "(read-only)");
  442. dev_dbg(&client->dev,
  443. "page_size %d, num_addresses %d, write_max %d%s\n",
  444. chip.page_size, num_addresses,
  445. at24->write_max,
  446. use_smbus ? ", use_smbus" : "");
  447. return 0;
  448. err_clients:
  449. for (i = 1; i < num_addresses; i++)
  450. if (at24->client[i])
  451. i2c_unregister_device(at24->client[i]);
  452. kfree(at24->writebuf);
  453. err_struct:
  454. kfree(at24);
  455. err_out:
  456. dev_dbg(&client->dev, "probe error %d\n", err);
  457. return err;
  458. }
  459. static int __devexit at24_remove(struct i2c_client *client)
  460. {
  461. struct at24_data *at24;
  462. int i;
  463. at24 = i2c_get_clientdata(client);
  464. sysfs_remove_bin_file(&client->dev.kobj, &at24->bin);
  465. for (i = 1; i < at24->num_addresses; i++)
  466. i2c_unregister_device(at24->client[i]);
  467. kfree(at24->writebuf);
  468. kfree(at24);
  469. i2c_set_clientdata(client, NULL);
  470. return 0;
  471. }
  472. /*-------------------------------------------------------------------------*/
  473. static struct i2c_driver at24_driver = {
  474. .driver = {
  475. .name = "at24",
  476. .owner = THIS_MODULE,
  477. },
  478. .probe = at24_probe,
  479. .remove = __devexit_p(at24_remove),
  480. .id_table = at24_ids,
  481. };
  482. static int __init at24_init(void)
  483. {
  484. io_limit = rounddown_pow_of_two(io_limit);
  485. return i2c_add_driver(&at24_driver);
  486. }
  487. module_init(at24_init);
  488. static void __exit at24_exit(void)
  489. {
  490. i2c_del_driver(&at24_driver);
  491. }
  492. module_exit(at24_exit);
  493. MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
  494. MODULE_AUTHOR("David Brownell and Wolfram Sang");
  495. MODULE_LICENSE("GPL");