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