blob: a466e40acb2c01d507eae6fdec909624a315415b [file] [log] [blame]
Wolfram Sang2b7a5052008-07-14 22:38:35 +02001/*
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>
Wolfram Sang2b7a5052008-07-14 22:38:35 +020018#include <linux/mod_devicetable.h>
19#include <linux/log2.h>
20#include <linux/bitops.h>
21#include <linux/jiffies.h>
Ben Gardnerdd905a62017-02-09 11:36:08 -060022#include <linux/property.h>
Andy Shevchenko40d8edc2015-10-23 12:16:44 +030023#include <linux/acpi.h>
Wolfram Sang2b7a5052008-07-14 22:38:35 +020024#include <linux/i2c.h>
Andrew Lunn57d15552016-02-26 20:59:20 +010025#include <linux/nvmem-provider.h>
Vivien Didelot25f73ed2013-09-27 15:06:28 -040026#include <linux/platform_data/at24.h>
Divagar Mohandass98e82012017-10-10 11:30:37 +053027#include <linux/pm_runtime.h>
Wolfram Sang2b7a5052008-07-14 22:38:35 +020028
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
57struct at24_data {
58 struct at24_platform_data chip;
Jean Delvare7aeb9662010-05-21 18:40:57 +020059 int use_smbus;
Christian Gmeinera839ce62014-10-09 11:07:58 +020060 int use_smbus_write;
Wolfram Sang2b7a5052008-07-14 22:38:35 +020061
Bartosz Golaszewski318aa9c2016-06-06 10:48:46 +020062 ssize_t (*read_func)(struct at24_data *, char *, unsigned int, size_t);
63 ssize_t (*write_func)(struct at24_data *,
64 const char *, unsigned int, size_t);
65
Wolfram Sang2b7a5052008-07-14 22:38:35 +020066 /*
67 * Lock protects against activities from other Linux tasks,
68 * but not from changes by other I2C masters.
69 */
70 struct mutex lock;
Wolfram Sang2b7a5052008-07-14 22:38:35 +020071
72 u8 *writebuf;
73 unsigned write_max;
74 unsigned num_addresses;
75
Andrew Lunn57d15552016-02-26 20:59:20 +010076 struct nvmem_config nvmem_config;
77 struct nvmem_device *nvmem;
78
Wolfram Sang2b7a5052008-07-14 22:38:35 +020079 /*
80 * Some chips tie up multiple I2C addresses; dummy devices reserve
81 * them for us, and we'll use them with SMBus calls.
82 */
83 struct i2c_client *client[];
84};
85
86/*
87 * This parameter is to help this driver avoid blocking other drivers out
88 * of I2C for potentially troublesome amounts of time. With a 100 kHz I2C
89 * clock, one 256 byte read takes about 1/43 second which is excessive;
90 * but the 1/170 second it takes at 400 kHz may be quite reasonable; and
91 * at 1 MHz (Fm+) a 1/430 second delay could easily be invisible.
92 *
93 * This value is forced to be a power of two so that writes align on pages.
94 */
95static unsigned io_limit = 128;
96module_param(io_limit, uint, 0);
97MODULE_PARM_DESC(io_limit, "Maximum bytes per I/O (default 128)");
98
99/*
100 * Specs often allow 5 msec for a page write, sometimes 20 msec;
101 * it's important to recover from write timeouts.
102 */
103static unsigned write_timeout = 25;
104module_param(write_timeout, uint, 0);
105MODULE_PARM_DESC(write_timeout, "Time (in ms) to try writes (default 25)");
106
107#define AT24_SIZE_BYTELEN 5
108#define AT24_SIZE_FLAGS 8
109
110#define AT24_BITMASK(x) (BIT(x) - 1)
111
112/* create non-zero magic value for given eeprom parameters */
113#define AT24_DEVICE_MAGIC(_len, _flags) \
114 ((1 << AT24_SIZE_FLAGS | (_flags)) \
115 << AT24_SIZE_BYTELEN | ilog2(_len))
116
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200117/*
118 * Both reads and writes fail if the previous write didn't complete yet. This
119 * macro loops a few times waiting at least long enough for one entire page
Bartosz Golaszewski24da3cc2016-07-17 20:40:06 +0200120 * write to work while making sure that at least one iteration is run before
121 * checking the break condition.
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200122 *
123 * It takes two parameters: a variable in which the future timeout in jiffies
124 * will be stored and a temporary variable holding the time of the last
125 * iteration of processing the request. Both should be unsigned integers
126 * holding at least 32 bits.
127 */
128#define loop_until_timeout(tout, op_time) \
Bartosz Golaszewski24da3cc2016-07-17 20:40:06 +0200129 for (tout = jiffies + msecs_to_jiffies(write_timeout), op_time = 0; \
130 op_time ? time_before(op_time, tout) : true; \
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200131 usleep_range(1000, 1500), op_time = jiffies)
132
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200133static const struct i2c_device_id at24_ids[] = {
134 /* needs 8 addresses as A0-A2 are ignored */
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200135 { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) },
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200136 /* old variants can't be handled with this generic entry! */
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200137 { "24c01", AT24_DEVICE_MAGIC(1024 / 8, 0) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200138 { "24cs01", AT24_DEVICE_MAGIC(16,
139 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200140 { "24c02", AT24_DEVICE_MAGIC(2048 / 8, 0) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200141 { "24cs02", AT24_DEVICE_MAGIC(16,
142 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
Bartosz Golaszewski0b813652016-06-06 10:48:54 +0200143 { "24mac402", AT24_DEVICE_MAGIC(48 / 8,
144 AT24_FLAG_MAC | AT24_FLAG_READONLY) },
145 { "24mac602", AT24_DEVICE_MAGIC(64 / 8,
146 AT24_FLAG_MAC | AT24_FLAG_READONLY) },
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200147 /* spd is a 24c02 in memory DIMMs */
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200148 { "spd", AT24_DEVICE_MAGIC(2048 / 8,
149 AT24_FLAG_READONLY | AT24_FLAG_IRUGO) },
150 { "24c04", AT24_DEVICE_MAGIC(4096 / 8, 0) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200151 { "24cs04", AT24_DEVICE_MAGIC(16,
152 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200153 /* 24rf08 quirk is handled at i2c-core */
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200154 { "24c08", AT24_DEVICE_MAGIC(8192 / 8, 0) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200155 { "24cs08", AT24_DEVICE_MAGIC(16,
156 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200157 { "24c16", AT24_DEVICE_MAGIC(16384 / 8, 0) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200158 { "24cs16", AT24_DEVICE_MAGIC(16,
159 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200160 { "24c32", AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200161 { "24cs32", AT24_DEVICE_MAGIC(16,
162 AT24_FLAG_ADDR16 |
163 AT24_FLAG_SERIAL |
164 AT24_FLAG_READONLY) },
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200165 { "24c64", AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200166 { "24cs64", AT24_DEVICE_MAGIC(16,
167 AT24_FLAG_ADDR16 |
168 AT24_FLAG_SERIAL |
169 AT24_FLAG_READONLY) },
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200170 { "24c128", AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16) },
171 { "24c256", AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16) },
172 { "24c512", AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16) },
173 { "24c1024", AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16) },
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200174 { "at24", 0 },
175 { /* END OF LIST */ }
176};
177MODULE_DEVICE_TABLE(i2c, at24_ids);
178
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300179static const struct acpi_device_id at24_acpi_ids[] = {
180 { "INT3499", AT24_DEVICE_MAGIC(8192 / 8, 0) },
181 { }
182};
183MODULE_DEVICE_TABLE(acpi, at24_acpi_ids);
184
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200185/*-------------------------------------------------------------------------*/
186
187/*
188 * This routine supports chips which consume multiple I2C addresses. It
189 * computes the addressing information to be used for a given r/w request.
190 * Assumes that sanity checks for offset happened at sysfs-layer.
Bartosz Golaszewski9afd68662016-06-06 10:48:48 +0200191 *
192 * Slave address and byte offset derive from the offset. Always
193 * set the byte address; on a multi-master board, another master
194 * may have changed the chip's "current" address pointer.
195 *
196 * REVISIT some multi-address chips don't rollover page reads to
197 * the next slave address, so we may need to truncate the count.
198 * Those chips might need another quirk flag.
199 *
200 * If the real hardware used four adjacent 24c02 chips and that
201 * were misconfigured as one 24c08, that would be a similar effect:
202 * one "eeprom" file not four, but larger reads would fail when
203 * they crossed certain pages.
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200204 */
205static struct i2c_client *at24_translate_offset(struct at24_data *at24,
Bartosz Golaszewski2da78ac2016-06-06 10:48:45 +0200206 unsigned int *offset)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200207{
208 unsigned i;
209
210 if (at24->chip.flags & AT24_FLAG_ADDR16) {
211 i = *offset >> 16;
212 *offset &= 0xffff;
213 } else {
214 i = *offset >> 8;
215 *offset &= 0xff;
216 }
217
218 return at24->client[i];
219}
220
Bartosz Golaszewski9afd68662016-06-06 10:48:48 +0200221static ssize_t at24_eeprom_read_smbus(struct at24_data *at24, char *buf,
222 unsigned int offset, size_t count)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200223{
Wolfram Sang4d291962009-11-26 09:22:33 +0100224 unsigned long timeout, read_time;
Bartosz Golaszewski9afd68662016-06-06 10:48:48 +0200225 struct i2c_client *client;
226 int status;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200227
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200228 client = at24_translate_offset(at24, &offset);
229
230 if (count > io_limit)
231 count = io_limit;
232
Bartosz Golaszewski9afd68662016-06-06 10:48:48 +0200233 /* Smaller eeproms can work given some SMBus extension calls */
234 if (count > I2C_SMBUS_BLOCK_MAX)
235 count = I2C_SMBUS_BLOCK_MAX;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200236
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200237 loop_until_timeout(timeout, read_time) {
Bartosz Golaszewski9afd68662016-06-06 10:48:48 +0200238 status = i2c_smbus_read_i2c_block_data_or_emulated(client,
239 offset,
240 count, buf);
241
242 dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
243 count, offset, status, jiffies);
244
245 if (status == count)
246 return count;
247 }
248
249 return -ETIMEDOUT;
250}
251
252static ssize_t at24_eeprom_read_i2c(struct at24_data *at24, char *buf,
253 unsigned int offset, size_t count)
254{
255 unsigned long timeout, read_time;
256 struct i2c_client *client;
257 struct i2c_msg msg[2];
258 int status, i;
259 u8 msgbuf[2];
260
261 memset(msg, 0, sizeof(msg));
262 client = at24_translate_offset(at24, &offset);
263
264 if (count > io_limit)
265 count = io_limit;
266
267 /*
268 * When we have a better choice than SMBus calls, use a combined I2C
269 * message. Write address; then read up to io_limit data bytes. Note
270 * that read page rollover helps us here (unlike writes). msgbuf is
271 * u8 and will cast to our needs.
272 */
273 i = 0;
274 if (at24->chip.flags & AT24_FLAG_ADDR16)
275 msgbuf[i++] = offset >> 8;
276 msgbuf[i++] = offset;
277
278 msg[0].addr = client->addr;
279 msg[0].buf = msgbuf;
280 msg[0].len = i;
281
282 msg[1].addr = client->addr;
283 msg[1].flags = I2C_M_RD;
284 msg[1].buf = buf;
285 msg[1].len = count;
286
287 loop_until_timeout(timeout, read_time) {
288 status = i2c_transfer(client->adapter, msg, 2);
289 if (status == 2)
290 status = count;
291
Wolfram Sang4d291962009-11-26 09:22:33 +0100292 dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
293 count, offset, status, jiffies);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200294
Wolfram Sang4d291962009-11-26 09:22:33 +0100295 if (status == count)
296 return count;
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200297 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200298
Wolfram Sang4d291962009-11-26 09:22:33 +0100299 return -ETIMEDOUT;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200300}
301
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200302static ssize_t at24_eeprom_read_serial(struct at24_data *at24, char *buf,
303 unsigned int offset, size_t count)
304{
305 unsigned long timeout, read_time;
306 struct i2c_client *client;
307 struct i2c_msg msg[2];
308 u8 addrbuf[2];
309 int status;
310
311 client = at24_translate_offset(at24, &offset);
312
313 memset(msg, 0, sizeof(msg));
314 msg[0].addr = client->addr;
315 msg[0].buf = addrbuf;
316
317 /*
318 * The address pointer of the device is shared between the regular
319 * EEPROM array and the serial number block. The dummy write (part of
320 * the sequential read protocol) ensures the address pointer is reset
321 * to the desired position.
322 */
323 if (at24->chip.flags & AT24_FLAG_ADDR16) {
324 /*
325 * For 16 bit address pointers, the word address must contain
326 * a '10' sequence in bits 11 and 10 regardless of the
327 * intended position of the address pointer.
328 */
329 addrbuf[0] = 0x08;
330 addrbuf[1] = offset;
331 msg[0].len = 2;
332 } else {
333 /*
334 * Otherwise the word address must begin with a '10' sequence,
335 * regardless of the intended address.
336 */
337 addrbuf[0] = 0x80 + offset;
338 msg[0].len = 1;
339 }
340
341 msg[1].addr = client->addr;
342 msg[1].flags = I2C_M_RD;
343 msg[1].buf = buf;
344 msg[1].len = count;
345
346 loop_until_timeout(timeout, read_time) {
347 status = i2c_transfer(client->adapter, msg, 2);
348 if (status == 2)
349 return count;
350 }
351
352 return -ETIMEDOUT;
353}
354
Bartosz Golaszewski0b813652016-06-06 10:48:54 +0200355static ssize_t at24_eeprom_read_mac(struct at24_data *at24, char *buf,
356 unsigned int offset, size_t count)
357{
358 unsigned long timeout, read_time;
359 struct i2c_client *client;
360 struct i2c_msg msg[2];
361 u8 addrbuf[2];
362 int status;
363
364 client = at24_translate_offset(at24, &offset);
365
366 memset(msg, 0, sizeof(msg));
367 msg[0].addr = client->addr;
368 msg[0].buf = addrbuf;
369 addrbuf[0] = 0x90 + offset;
370 msg[0].len = 1;
371 msg[1].addr = client->addr;
372 msg[1].flags = I2C_M_RD;
373 msg[1].buf = buf;
374 msg[1].len = count;
375
376 loop_until_timeout(timeout, read_time) {
377 status = i2c_transfer(client->adapter, msg, 2);
378 if (status == 2)
379 return count;
380 }
381
382 return -ETIMEDOUT;
383}
384
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200385/*
386 * Note that if the hardware write-protect pin is pulled high, the whole
387 * chip is normally write protected. But there are plenty of product
388 * variants here, including OTP fuses and partial chip protect.
389 *
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200390 * We only use page mode writes; the alternative is sloooow. These routines
391 * write at most one page.
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200392 */
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200393
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200394static size_t at24_adjust_write_count(struct at24_data *at24,
395 unsigned int offset, size_t count)
396{
397 unsigned next_page;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200398
399 /* write_max is at most a page */
400 if (count > at24->write_max)
401 count = at24->write_max;
402
403 /* Never roll over backwards, to the start of this page */
404 next_page = roundup(offset + 1, at24->chip.page_size);
405 if (offset + count > next_page)
406 count = next_page - offset;
407
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200408 return count;
409}
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200410
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200411static ssize_t at24_eeprom_write_smbus_block(struct at24_data *at24,
412 const char *buf,
413 unsigned int offset, size_t count)
414{
415 unsigned long timeout, write_time;
416 struct i2c_client *client;
417 ssize_t status = 0;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200418
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200419 client = at24_translate_offset(at24, &offset);
420 count = at24_adjust_write_count(at24, offset, count);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200421
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200422 loop_until_timeout(timeout, write_time) {
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200423 status = i2c_smbus_write_i2c_block_data(client,
424 offset, count, buf);
425 if (status == 0)
426 status = count;
Christian Gmeinera839ce62014-10-09 11:07:58 +0200427
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200428 dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
429 count, offset, status, jiffies);
430
431 if (status == count)
432 return count;
433 }
434
435 return -ETIMEDOUT;
436}
437
438static ssize_t at24_eeprom_write_smbus_byte(struct at24_data *at24,
439 const char *buf,
440 unsigned int offset, size_t count)
441{
442 unsigned long timeout, write_time;
443 struct i2c_client *client;
444 ssize_t status = 0;
445
446 client = at24_translate_offset(at24, &offset);
447
448 loop_until_timeout(timeout, write_time) {
449 status = i2c_smbus_write_byte_data(client, offset, buf[0]);
450 if (status == 0)
451 status = count;
452
453 dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
454 count, offset, status, jiffies);
455
456 if (status == count)
457 return count;
458 }
459
460 return -ETIMEDOUT;
461}
462
463static ssize_t at24_eeprom_write_i2c(struct at24_data *at24, const char *buf,
464 unsigned int offset, size_t count)
465{
466 unsigned long timeout, write_time;
467 struct i2c_client *client;
468 struct i2c_msg msg;
469 ssize_t status = 0;
470 int i = 0;
471
472 client = at24_translate_offset(at24, &offset);
473 count = at24_adjust_write_count(at24, offset, count);
474
475 msg.addr = client->addr;
476 msg.flags = 0;
477
478 /* msg.buf is u8 and casts will mask the values */
479 msg.buf = at24->writebuf;
480 if (at24->chip.flags & AT24_FLAG_ADDR16)
481 msg.buf[i++] = offset >> 8;
482
483 msg.buf[i++] = offset;
484 memcpy(&msg.buf[i], buf, count);
485 msg.len = i + count;
486
487 loop_until_timeout(timeout, write_time) {
488 status = i2c_transfer(client->adapter, &msg, 1);
489 if (status == 1)
490 status = count;
491
David Brownell2ce5b342008-08-10 22:56:16 +0200492 dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200493 count, offset, status, jiffies);
494
495 if (status == count)
496 return count;
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200497 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200498
499 return -ETIMEDOUT;
500}
501
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200502static int at24_read(void *priv, unsigned int off, void *val, size_t count)
503{
504 struct at24_data *at24 = priv;
Divagar Mohandass98e82012017-10-10 11:30:37 +0530505 struct i2c_client *client;
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200506 char *buf = val;
Divagar Mohandass98e82012017-10-10 11:30:37 +0530507 int ret;
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200508
509 if (unlikely(!count))
510 return count;
511
Divagar Mohandass98e82012017-10-10 11:30:37 +0530512 client = at24_translate_offset(at24, &off);
513
514 ret = pm_runtime_get_sync(&client->dev);
515 if (ret < 0) {
516 pm_runtime_put_noidle(&client->dev);
517 return ret;
518 }
519
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200520 /*
521 * Read data from chip, protecting against concurrent updates
522 * from this host, but not from other I2C masters.
523 */
524 mutex_lock(&at24->lock);
525
526 while (count) {
527 int status;
528
Bartosz Golaszewski318aa9c2016-06-06 10:48:46 +0200529 status = at24->read_func(at24, buf, off, count);
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200530 if (status < 0) {
531 mutex_unlock(&at24->lock);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530532 pm_runtime_put(&client->dev);
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200533 return status;
534 }
535 buf += status;
536 off += status;
537 count -= status;
538 }
539
540 mutex_unlock(&at24->lock);
541
Divagar Mohandass98e82012017-10-10 11:30:37 +0530542 pm_runtime_put(&client->dev);
543
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200544 return 0;
545}
546
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100547static int at24_write(void *priv, unsigned int off, void *val, size_t count)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200548{
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100549 struct at24_data *at24 = priv;
Divagar Mohandass98e82012017-10-10 11:30:37 +0530550 struct i2c_client *client;
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100551 char *buf = val;
Divagar Mohandass98e82012017-10-10 11:30:37 +0530552 int ret;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200553
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200554 if (unlikely(!count))
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100555 return -EINVAL;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200556
Divagar Mohandass98e82012017-10-10 11:30:37 +0530557 client = at24_translate_offset(at24, &off);
558
559 ret = pm_runtime_get_sync(&client->dev);
560 if (ret < 0) {
561 pm_runtime_put_noidle(&client->dev);
562 return ret;
563 }
564
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200565 /*
566 * Write data to chip, protecting against concurrent updates
567 * from this host, but not from other I2C masters.
568 */
569 mutex_lock(&at24->lock);
570
571 while (count) {
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100572 int status;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200573
Bartosz Golaszewski318aa9c2016-06-06 10:48:46 +0200574 status = at24->write_func(at24, buf, off, count);
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100575 if (status < 0) {
576 mutex_unlock(&at24->lock);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530577 pm_runtime_put(&client->dev);
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100578 return status;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200579 }
580 buf += status;
581 off += status;
582 count -= status;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200583 }
584
585 mutex_unlock(&at24->lock);
586
Divagar Mohandass98e82012017-10-10 11:30:37 +0530587 pm_runtime_put(&client->dev);
588
Andrew Lunn57d15552016-02-26 20:59:20 +0100589 return 0;
590}
591
Ben Gardnerdd905a62017-02-09 11:36:08 -0600592static void at24_get_pdata(struct device *dev, struct at24_platform_data *chip)
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100593{
Ben Gardnerdd905a62017-02-09 11:36:08 -0600594 int err;
595 u32 val;
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100596
Ben Gardnerdd905a62017-02-09 11:36:08 -0600597 if (device_property_present(dev, "read-only"))
598 chip->flags |= AT24_FLAG_READONLY;
599
Divagar Mohandassdbc1ab92017-10-10 11:30:36 +0530600 err = device_property_read_u32(dev, "size", &val);
601 if (!err)
602 chip->byte_len = val;
603
Ben Gardnerdd905a62017-02-09 11:36:08 -0600604 err = device_property_read_u32(dev, "pagesize", &val);
605 if (!err) {
606 chip->page_size = val;
607 } else {
608 /*
609 * This is slow, but we can't know all eeproms, so we better
610 * play safe. Specifying custom eeprom-types via platform_data
611 * is recommended anyhow.
612 */
613 chip->page_size = 1;
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100614 }
615}
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100616
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200617static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
618{
619 struct at24_platform_data chip;
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300620 kernel_ulong_t magic = 0;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200621 bool writable;
Jean Delvare7aeb9662010-05-21 18:40:57 +0200622 int use_smbus = 0;
Christian Gmeinera839ce62014-10-09 11:07:58 +0200623 int use_smbus_write = 0;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200624 struct at24_data *at24;
625 int err;
626 unsigned i, num_addresses;
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200627 u8 test_byte;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200628
629 if (client->dev.platform_data) {
630 chip = *(struct at24_platform_data *)client->dev.platform_data;
631 } else {
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300632 if (id) {
633 magic = id->driver_data;
634 } else {
635 const struct acpi_device_id *aid;
636
637 aid = acpi_match_device(at24_acpi_ids, &client->dev);
638 if (aid)
639 magic = aid->driver_data;
640 }
641 if (!magic)
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700642 return -ENODEV;
643
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200644 chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN));
645 magic >>= AT24_SIZE_BYTELEN;
646 chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS);
Kevin Hilman7274ec82009-04-02 16:56:57 -0700647
Ben Gardnerdd905a62017-02-09 11:36:08 -0600648 at24_get_pdata(&client->dev, &chip);
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100649
Kevin Hilman7274ec82009-04-02 16:56:57 -0700650 chip.setup = NULL;
651 chip.context = NULL;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200652 }
653
654 if (!is_power_of_2(chip.byte_len))
655 dev_warn(&client->dev,
656 "byte_len looks suspicious (no power of 2)!\n");
Wolfram Sang45efe842010-11-17 13:00:49 +0100657 if (!chip.page_size) {
658 dev_err(&client->dev, "page_size must not be 0!\n");
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700659 return -EINVAL;
Wolfram Sang45efe842010-11-17 13:00:49 +0100660 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200661 if (!is_power_of_2(chip.page_size))
662 dev_warn(&client->dev,
663 "page_size looks suspicious (no power of 2)!\n");
664
665 /* Use I2C operations unless we're stuck with SMBus extensions. */
666 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700667 if (chip.flags & AT24_FLAG_ADDR16)
668 return -EPFNOSUPPORT;
669
Jean Delvare7aeb9662010-05-21 18:40:57 +0200670 if (i2c_check_functionality(client->adapter,
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200671 I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
Jean Delvare7aeb9662010-05-21 18:40:57 +0200672 use_smbus = I2C_SMBUS_I2C_BLOCK_DATA;
673 } else if (i2c_check_functionality(client->adapter,
674 I2C_FUNC_SMBUS_READ_WORD_DATA)) {
675 use_smbus = I2C_SMBUS_WORD_DATA;
676 } else if (i2c_check_functionality(client->adapter,
677 I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
678 use_smbus = I2C_SMBUS_BYTE_DATA;
679 } else {
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700680 return -EPFNOSUPPORT;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200681 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200682
Christian Gmeinera839ce62014-10-09 11:07:58 +0200683 if (i2c_check_functionality(client->adapter,
684 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
685 use_smbus_write = I2C_SMBUS_I2C_BLOCK_DATA;
686 } else if (i2c_check_functionality(client->adapter,
687 I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {
688 use_smbus_write = I2C_SMBUS_BYTE_DATA;
689 chip.page_size = 1;
690 }
691 }
692
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200693 if (chip.flags & AT24_FLAG_TAKE8ADDR)
694 num_addresses = 8;
695 else
696 num_addresses = DIV_ROUND_UP(chip.byte_len,
697 (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
698
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700699 at24 = devm_kzalloc(&client->dev, sizeof(struct at24_data) +
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200700 num_addresses * sizeof(struct i2c_client *), GFP_KERNEL);
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700701 if (!at24)
702 return -ENOMEM;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200703
704 mutex_init(&at24->lock);
705 at24->use_smbus = use_smbus;
Christian Gmeinera839ce62014-10-09 11:07:58 +0200706 at24->use_smbus_write = use_smbus_write;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200707 at24->chip = chip;
708 at24->num_addresses = num_addresses;
709
Bartosz Golaszewski0b813652016-06-06 10:48:54 +0200710 if ((chip.flags & AT24_FLAG_SERIAL) && (chip.flags & AT24_FLAG_MAC)) {
711 dev_err(&client->dev,
712 "invalid device data - cannot have both AT24_FLAG_SERIAL & AT24_FLAG_MAC.");
713 return -EINVAL;
714 }
715
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200716 if (chip.flags & AT24_FLAG_SERIAL) {
717 at24->read_func = at24_eeprom_read_serial;
Bartosz Golaszewski0b813652016-06-06 10:48:54 +0200718 } else if (chip.flags & AT24_FLAG_MAC) {
719 at24->read_func = at24_eeprom_read_mac;
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200720 } else {
721 at24->read_func = at24->use_smbus ? at24_eeprom_read_smbus
722 : at24_eeprom_read_i2c;
723 }
724
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200725 if (at24->use_smbus) {
726 if (at24->use_smbus_write == I2C_SMBUS_I2C_BLOCK_DATA)
727 at24->write_func = at24_eeprom_write_smbus_block;
728 else
729 at24->write_func = at24_eeprom_write_smbus_byte;
730 } else {
731 at24->write_func = at24_eeprom_write_i2c;
732 }
Bartosz Golaszewski318aa9c2016-06-06 10:48:46 +0200733
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200734 writable = !(chip.flags & AT24_FLAG_READONLY);
735 if (writable) {
Christian Gmeinera839ce62014-10-09 11:07:58 +0200736 if (!use_smbus || use_smbus_write) {
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200737
738 unsigned write_max = chip.page_size;
739
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200740 if (write_max > io_limit)
741 write_max = io_limit;
742 if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX)
743 write_max = I2C_SMBUS_BLOCK_MAX;
744 at24->write_max = write_max;
745
746 /* buffer (data + address at the beginning) */
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700747 at24->writebuf = devm_kzalloc(&client->dev,
748 write_max + 2, GFP_KERNEL);
749 if (!at24->writebuf)
750 return -ENOMEM;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200751 } else {
752 dev_warn(&client->dev,
753 "cannot write due to controller restrictions.");
754 }
755 }
756
757 at24->client[0] = client;
758
759 /* use dummy devices for multiple-address chips */
760 for (i = 1; i < num_addresses; i++) {
761 at24->client[i] = i2c_new_dummy(client->adapter,
762 client->addr + i);
763 if (!at24->client[i]) {
764 dev_err(&client->dev, "address 0x%02x unavailable\n",
765 client->addr + i);
766 err = -EADDRINUSE;
767 goto err_clients;
768 }
769 }
770
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200771 i2c_set_clientdata(client, at24);
772
Divagar Mohandass98e82012017-10-10 11:30:37 +0530773 /* enable runtime pm */
774 pm_runtime_set_active(&client->dev);
775 pm_runtime_enable(&client->dev);
776
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200777 /*
778 * Perform a one-byte test read to verify that the
779 * chip is functional.
780 */
781 err = at24_read(at24, 0, &test_byte, 1);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530782 pm_runtime_idle(&client->dev);
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200783 if (err) {
784 err = -ENODEV;
785 goto err_clients;
786 }
787
Andrew Lunn57d15552016-02-26 20:59:20 +0100788 at24->nvmem_config.name = dev_name(&client->dev);
789 at24->nvmem_config.dev = &client->dev;
790 at24->nvmem_config.read_only = !writable;
791 at24->nvmem_config.root_only = true;
792 at24->nvmem_config.owner = THIS_MODULE;
793 at24->nvmem_config.compat = true;
794 at24->nvmem_config.base_dev = &client->dev;
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100795 at24->nvmem_config.reg_read = at24_read;
796 at24->nvmem_config.reg_write = at24_write;
797 at24->nvmem_config.priv = at24;
798 at24->nvmem_config.stride = 4;
799 at24->nvmem_config.word_size = 1;
800 at24->nvmem_config.size = chip.byte_len;
Andrew Lunn57d15552016-02-26 20:59:20 +0100801
802 at24->nvmem = nvmem_register(&at24->nvmem_config);
803
804 if (IS_ERR(at24->nvmem)) {
805 err = PTR_ERR(at24->nvmem);
806 goto err_clients;
807 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200808
Andrew Lunn57d15552016-02-26 20:59:20 +0100809 dev_info(&client->dev, "%u byte %s EEPROM, %s, %u bytes/write\n",
810 chip.byte_len, client->name,
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100811 writable ? "writable" : "read-only", at24->write_max);
Jean Delvare7aeb9662010-05-21 18:40:57 +0200812 if (use_smbus == I2C_SMBUS_WORD_DATA ||
813 use_smbus == I2C_SMBUS_BYTE_DATA) {
814 dev_notice(&client->dev, "Falling back to %s reads, "
815 "performance will suffer\n", use_smbus ==
816 I2C_SMBUS_WORD_DATA ? "word" : "byte");
817 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200818
Kevin Hilman7274ec82009-04-02 16:56:57 -0700819 /* export data to kernel code */
820 if (chip.setup)
Andrew Lunnbec3c112016-02-26 20:59:24 +0100821 chip.setup(at24->nvmem, chip.context);
Kevin Hilman7274ec82009-04-02 16:56:57 -0700822
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200823 return 0;
824
825err_clients:
826 for (i = 1; i < num_addresses; i++)
827 if (at24->client[i])
828 i2c_unregister_device(at24->client[i]);
829
Divagar Mohandass98e82012017-10-10 11:30:37 +0530830 pm_runtime_disable(&client->dev);
831
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200832 return err;
833}
834
Bill Pemberton486a5c22012-11-19 13:26:02 -0500835static int at24_remove(struct i2c_client *client)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200836{
837 struct at24_data *at24;
838 int i;
839
840 at24 = i2c_get_clientdata(client);
Andrew Lunn57d15552016-02-26 20:59:20 +0100841
842 nvmem_unregister(at24->nvmem);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200843
844 for (i = 1; i < at24->num_addresses; i++)
845 i2c_unregister_device(at24->client[i]);
846
Divagar Mohandass98e82012017-10-10 11:30:37 +0530847 pm_runtime_disable(&client->dev);
848 pm_runtime_set_suspended(&client->dev);
849
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200850 return 0;
851}
852
853/*-------------------------------------------------------------------------*/
854
855static struct i2c_driver at24_driver = {
856 .driver = {
857 .name = "at24",
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300858 .acpi_match_table = ACPI_PTR(at24_acpi_ids),
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200859 },
860 .probe = at24_probe,
Bill Pemberton2d6bed92012-11-19 13:21:23 -0500861 .remove = at24_remove,
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200862 .id_table = at24_ids,
863};
864
865static int __init at24_init(void)
866{
Wolfram Sang45efe842010-11-17 13:00:49 +0100867 if (!io_limit) {
868 pr_err("at24: io_limit must not be 0!\n");
869 return -EINVAL;
870 }
871
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200872 io_limit = rounddown_pow_of_two(io_limit);
873 return i2c_add_driver(&at24_driver);
874}
875module_init(at24_init);
876
877static void __exit at24_exit(void)
878{
879 i2c_del_driver(&at24_driver);
880}
881module_exit(at24_exit);
882
883MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
884MODULE_AUTHOR("David Brownell and Wolfram Sang");
885MODULE_LICENSE("GPL");