blob: 89b610ac22d31f3d3d057254dfb840f9aeb499b3 [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>
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +020015#include <linux/of_device.h>
Wolfram Sang2b7a5052008-07-14 22:38:35 +020016#include <linux/slab.h>
17#include <linux/delay.h>
18#include <linux/mutex.h>
Wolfram Sang2b7a5052008-07-14 22:38:35 +020019#include <linux/mod_devicetable.h>
20#include <linux/log2.h>
21#include <linux/bitops.h>
22#include <linux/jiffies.h>
Ben Gardnerdd905a62017-02-09 11:36:08 -060023#include <linux/property.h>
Andy Shevchenko40d8edc2015-10-23 12:16:44 +030024#include <linux/acpi.h>
Wolfram Sang2b7a5052008-07-14 22:38:35 +020025#include <linux/i2c.h>
Andrew Lunn57d15552016-02-26 20:59:20 +010026#include <linux/nvmem-provider.h>
Heiner Kallweit5c015252017-11-28 21:51:40 +010027#include <linux/regmap.h>
Vivien Didelot25f73ed2013-09-27 15:06:28 -040028#include <linux/platform_data/at24.h>
Divagar Mohandass98e82012017-10-10 11:30:37 +053029#include <linux/pm_runtime.h>
Bartosz Golaszewski6ce261e2017-12-19 11:28:54 +010030#include <linux/gpio/consumer.h>
Wolfram Sang2b7a5052008-07-14 22:38:35 +020031
32/*
33 * I2C EEPROMs from most vendors are inexpensive and mostly interchangeable.
34 * Differences between different vendor product lines (like Atmel AT24C or
35 * MicroChip 24LC, etc) won't much matter for typical read/write access.
36 * There are also I2C RAM chips, likewise interchangeable. One example
37 * would be the PCF8570, which acts like a 24c02 EEPROM (256 bytes).
38 *
39 * However, misconfiguration can lose data. "Set 16-bit memory address"
40 * to a part with 8-bit addressing will overwrite data. Writing with too
41 * big a page size also loses data. And it's not safe to assume that the
42 * conventional addresses 0x50..0x57 only hold eeproms; a PCF8563 RTC
43 * uses 0x51, for just one example.
44 *
45 * Accordingly, explicit board-specific configuration data should be used
46 * in almost all cases. (One partial exception is an SMBus used to access
47 * "SPD" data for DRAM sticks. Those only use 24c02 EEPROMs.)
48 *
49 * So this driver uses "new style" I2C driver binding, expecting to be
50 * told what devices exist. That may be in arch/X/mach-Y/board-Z.c or
51 * similar kernel-resident tables; or, configuration data coming from
52 * a bootloader.
53 *
54 * Other than binding model, current differences from "eeprom" driver are
55 * that this one handles write access and isn't restricted to 24c02 devices.
56 * It also handles larger devices (32 kbit and up) with two-byte addresses,
57 * which won't work on pure SMBus systems.
58 */
59
Heiner Kallweit5c015252017-11-28 21:51:40 +010060struct at24_client {
61 struct i2c_client *client;
62 struct regmap *regmap;
63};
64
Wolfram Sang2b7a5052008-07-14 22:38:35 +020065struct at24_data {
66 struct at24_platform_data chip;
Bartosz Golaszewski318aa9c2016-06-06 10:48:46 +020067
Wolfram Sang2b7a5052008-07-14 22:38:35 +020068 /*
69 * Lock protects against activities from other Linux tasks,
70 * but not from changes by other I2C masters.
71 */
72 struct mutex lock;
Wolfram Sang2b7a5052008-07-14 22:38:35 +020073
Bartosz Golaszewskiaa4ce222017-12-13 11:56:23 +010074 unsigned int write_max;
75 unsigned int num_addresses;
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +010076 unsigned int offset_adj;
Wolfram Sang2b7a5052008-07-14 22:38:35 +020077
Andrew Lunn57d15552016-02-26 20:59:20 +010078 struct nvmem_device *nvmem;
79
Bartosz Golaszewski6ce261e2017-12-19 11:28:54 +010080 struct gpio_desc *wp_gpio;
81
Wolfram Sang2b7a5052008-07-14 22:38:35 +020082 /*
83 * Some chips tie up multiple I2C addresses; dummy devices reserve
84 * them for us, and we'll use them with SMBus calls.
85 */
Heiner Kallweit5c015252017-11-28 21:51:40 +010086 struct at24_client client[];
Wolfram Sang2b7a5052008-07-14 22:38:35 +020087};
88
89/*
90 * This parameter is to help this driver avoid blocking other drivers out
91 * of I2C for potentially troublesome amounts of time. With a 100 kHz I2C
92 * clock, one 256 byte read takes about 1/43 second which is excessive;
93 * but the 1/170 second it takes at 400 kHz may be quite reasonable; and
94 * at 1 MHz (Fm+) a 1/430 second delay could easily be invisible.
95 *
96 * This value is forced to be a power of two so that writes align on pages.
97 */
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +010098static unsigned int at24_io_limit = 128;
99module_param_named(io_limit, at24_io_limit, uint, 0);
100MODULE_PARM_DESC(at24_io_limit, "Maximum bytes per I/O (default 128)");
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200101
102/*
103 * Specs often allow 5 msec for a page write, sometimes 20 msec;
104 * it's important to recover from write timeouts.
105 */
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100106static unsigned int at24_write_timeout = 25;
107module_param_named(write_timeout, at24_write_timeout, uint, 0);
108MODULE_PARM_DESC(at24_write_timeout, "Time (in ms) to try writes (default 25)");
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200109
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200110/*
111 * Both reads and writes fail if the previous write didn't complete yet. This
112 * macro loops a few times waiting at least long enough for one entire page
Bartosz Golaszewski24da3cc2016-07-17 20:40:06 +0200113 * write to work while making sure that at least one iteration is run before
114 * checking the break condition.
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200115 *
116 * It takes two parameters: a variable in which the future timeout in jiffies
117 * will be stored and a temporary variable holding the time of the last
118 * iteration of processing the request. Both should be unsigned integers
119 * holding at least 32 bits.
120 */
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100121#define at24_loop_until_timeout(tout, op_time) \
122 for (tout = jiffies + msecs_to_jiffies(at24_write_timeout), \
123 op_time = 0; \
Bartosz Golaszewski24da3cc2016-07-17 20:40:06 +0200124 op_time ? time_before(op_time, tout) : true; \
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200125 usleep_range(1000, 1500), op_time = jiffies)
126
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500127struct at24_chip_data {
128 /*
129 * these fields mirror their equivalents in
130 * struct at24_platform_data
131 */
132 u32 byte_len;
133 u8 flags;
134};
135
136#define AT24_CHIP_DATA(_name, _len, _flags) \
137 static const struct at24_chip_data _name = { \
138 .byte_len = _len, .flags = _flags, \
139 }
140
141/* needs 8 addresses as A0-A2 are ignored */
142AT24_CHIP_DATA(at24_data_24c00, 128 / 8, AT24_FLAG_TAKE8ADDR);
143/* old variants can't be handled with this generic entry! */
144AT24_CHIP_DATA(at24_data_24c01, 1024 / 8, 0);
145AT24_CHIP_DATA(at24_data_24cs01, 16,
146 AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
147AT24_CHIP_DATA(at24_data_24c02, 2048 / 8, 0);
148AT24_CHIP_DATA(at24_data_24cs02, 16,
149 AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
150AT24_CHIP_DATA(at24_data_24mac402, 48 / 8,
151 AT24_FLAG_MAC | AT24_FLAG_READONLY);
152AT24_CHIP_DATA(at24_data_24mac602, 64 / 8,
153 AT24_FLAG_MAC | AT24_FLAG_READONLY);
154/* spd is a 24c02 in memory DIMMs */
155AT24_CHIP_DATA(at24_data_spd, 2048 / 8,
156 AT24_FLAG_READONLY | AT24_FLAG_IRUGO);
157AT24_CHIP_DATA(at24_data_24c04, 4096 / 8, 0);
158AT24_CHIP_DATA(at24_data_24cs04, 16,
159 AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
160/* 24rf08 quirk is handled at i2c-core */
161AT24_CHIP_DATA(at24_data_24c08, 8192 / 8, 0);
162AT24_CHIP_DATA(at24_data_24cs08, 16,
163 AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
164AT24_CHIP_DATA(at24_data_24c16, 16384 / 8, 0);
165AT24_CHIP_DATA(at24_data_24cs16, 16,
166 AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
167AT24_CHIP_DATA(at24_data_24c32, 32768 / 8, AT24_FLAG_ADDR16);
168AT24_CHIP_DATA(at24_data_24cs32, 16,
169 AT24_FLAG_ADDR16 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
170AT24_CHIP_DATA(at24_data_24c64, 65536 / 8, AT24_FLAG_ADDR16);
171AT24_CHIP_DATA(at24_data_24cs64, 16,
172 AT24_FLAG_ADDR16 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
173AT24_CHIP_DATA(at24_data_24c128, 131072 / 8, AT24_FLAG_ADDR16);
174AT24_CHIP_DATA(at24_data_24c256, 262144 / 8, AT24_FLAG_ADDR16);
175AT24_CHIP_DATA(at24_data_24c512, 524288 / 8, AT24_FLAG_ADDR16);
176AT24_CHIP_DATA(at24_data_24c1024, 1048576 / 8, AT24_FLAG_ADDR16);
177/* identical to 24c08 ? */
178AT24_CHIP_DATA(at24_data_INT3499, 8192 / 8, 0);
179
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200180static const struct i2c_device_id at24_ids[] = {
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500181 { "24c00", (kernel_ulong_t)&at24_data_24c00 },
182 { "24c01", (kernel_ulong_t)&at24_data_24c01 },
183 { "24cs01", (kernel_ulong_t)&at24_data_24cs01 },
184 { "24c02", (kernel_ulong_t)&at24_data_24c02 },
185 { "24cs02", (kernel_ulong_t)&at24_data_24cs02 },
186 { "24mac402", (kernel_ulong_t)&at24_data_24mac402 },
187 { "24mac602", (kernel_ulong_t)&at24_data_24mac602 },
188 { "spd", (kernel_ulong_t)&at24_data_spd },
189 { "24c04", (kernel_ulong_t)&at24_data_24c04 },
190 { "24cs04", (kernel_ulong_t)&at24_data_24cs04 },
191 { "24c08", (kernel_ulong_t)&at24_data_24c08 },
192 { "24cs08", (kernel_ulong_t)&at24_data_24cs08 },
193 { "24c16", (kernel_ulong_t)&at24_data_24c16 },
194 { "24cs16", (kernel_ulong_t)&at24_data_24cs16 },
195 { "24c32", (kernel_ulong_t)&at24_data_24c32 },
196 { "24cs32", (kernel_ulong_t)&at24_data_24cs32 },
197 { "24c64", (kernel_ulong_t)&at24_data_24c64 },
198 { "24cs64", (kernel_ulong_t)&at24_data_24cs64 },
199 { "24c128", (kernel_ulong_t)&at24_data_24c128 },
200 { "24c256", (kernel_ulong_t)&at24_data_24c256 },
201 { "24c512", (kernel_ulong_t)&at24_data_24c512 },
202 { "24c1024", (kernel_ulong_t)&at24_data_24c1024 },
203 { "at24", 0 },
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200204 { /* END OF LIST */ }
205};
206MODULE_DEVICE_TABLE(i2c, at24_ids);
207
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200208static const struct of_device_id at24_of_match[] = {
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500209 { .compatible = "atmel,24c00", .data = &at24_data_24c00 },
210 { .compatible = "atmel,24c01", .data = &at24_data_24c01 },
Bartosz Golaszewski0f30aca2017-12-28 11:49:13 +0100211 { .compatible = "atmel,24cs01", .data = &at24_data_24cs01 },
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500212 { .compatible = "atmel,24c02", .data = &at24_data_24c02 },
Bartosz Golaszewski0f30aca2017-12-28 11:49:13 +0100213 { .compatible = "atmel,24cs02", .data = &at24_data_24cs02 },
214 { .compatible = "atmel,24mac402", .data = &at24_data_24mac402 },
215 { .compatible = "atmel,24mac602", .data = &at24_data_24mac602 },
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500216 { .compatible = "atmel,spd", .data = &at24_data_spd },
217 { .compatible = "atmel,24c04", .data = &at24_data_24c04 },
Bartosz Golaszewski0f30aca2017-12-28 11:49:13 +0100218 { .compatible = "atmel,24cs04", .data = &at24_data_24cs04 },
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500219 { .compatible = "atmel,24c08", .data = &at24_data_24c08 },
Bartosz Golaszewski0f30aca2017-12-28 11:49:13 +0100220 { .compatible = "atmel,24cs08", .data = &at24_data_24cs08 },
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500221 { .compatible = "atmel,24c16", .data = &at24_data_24c16 },
Bartosz Golaszewski0f30aca2017-12-28 11:49:13 +0100222 { .compatible = "atmel,24cs16", .data = &at24_data_24cs16 },
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500223 { .compatible = "atmel,24c32", .data = &at24_data_24c32 },
Bartosz Golaszewski0f30aca2017-12-28 11:49:13 +0100224 { .compatible = "atmel,24cs32", .data = &at24_data_24cs32 },
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500225 { .compatible = "atmel,24c64", .data = &at24_data_24c64 },
Bartosz Golaszewski0f30aca2017-12-28 11:49:13 +0100226 { .compatible = "atmel,24cs64", .data = &at24_data_24cs64 },
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500227 { .compatible = "atmel,24c128", .data = &at24_data_24c128 },
228 { .compatible = "atmel,24c256", .data = &at24_data_24c256 },
229 { .compatible = "atmel,24c512", .data = &at24_data_24c512 },
230 { .compatible = "atmel,24c1024", .data = &at24_data_24c1024 },
231 { /* END OF LIST */ },
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200232};
233MODULE_DEVICE_TABLE(of, at24_of_match);
234
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300235static const struct acpi_device_id at24_acpi_ids[] = {
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500236 { "INT3499", (kernel_ulong_t)&at24_data_INT3499 },
237 { /* END OF LIST */ }
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300238};
239MODULE_DEVICE_TABLE(acpi, at24_acpi_ids);
240
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200241/*
242 * This routine supports chips which consume multiple I2C addresses. It
243 * computes the addressing information to be used for a given r/w request.
244 * Assumes that sanity checks for offset happened at sysfs-layer.
Bartosz Golaszewski9afd68662016-06-06 10:48:48 +0200245 *
246 * Slave address and byte offset derive from the offset. Always
247 * set the byte address; on a multi-master board, another master
248 * may have changed the chip's "current" address pointer.
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200249 */
Heiner Kallweit46049482017-11-28 21:51:42 +0100250static struct at24_client *at24_translate_offset(struct at24_data *at24,
251 unsigned int *offset)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200252{
Bartosz Golaszewskiaa4ce222017-12-13 11:56:23 +0100253 unsigned int i;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200254
255 if (at24->chip.flags & AT24_FLAG_ADDR16) {
256 i = *offset >> 16;
257 *offset &= 0xffff;
258 } else {
259 i = *offset >> 8;
260 *offset &= 0xff;
261 }
262
Heiner Kallweit46049482017-11-28 21:51:42 +0100263 return &at24->client[i];
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200264}
265
Sven Van Asbroecke32213f2017-12-08 11:28:30 -0500266static 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->chip.flags & AT24_FLAG_NO_RDROL) {
278 bits = (at24->chip.flags & AT24_FLAG_ADDR16) ? 16 : 8;
279 remainder = BIT(bits) - offset;
280 if (count > remainder)
281 count = remainder;
282 }
283
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100284 if (count > at24_io_limit)
285 count = at24_io_limit;
Sven Van Asbroecke32213f2017-12-08 11:28:30 -0500286
287 return count;
288}
289
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100290static 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;
Sven Van Asbroecke32213f2017-12-08 11:28:30 -0500302 count = at24_adjust_read_count(at24, offset, count);
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100303
304 /* adjust offset for mac and serial read ops */
305 offset += at24->offset_adj;
306
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100307 at24_loop_until_timeout(timeout, read_time) {
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100308 ret = regmap_bulk_read(regmap, offset, buf, count);
309 dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
310 count, offset, ret, jiffies);
311 if (!ret)
312 return count;
313 }
314
315 return -ETIMEDOUT;
316}
317
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200318/*
319 * Note that if the hardware write-protect pin is pulled high, the whole
320 * chip is normally write protected. But there are plenty of product
321 * variants here, including OTP fuses and partial chip protect.
322 *
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200323 * We only use page mode writes; the alternative is sloooow. These routines
324 * write at most one page.
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200325 */
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200326
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200327static size_t at24_adjust_write_count(struct at24_data *at24,
328 unsigned int offset, size_t count)
329{
Bartosz Golaszewskiaa4ce222017-12-13 11:56:23 +0100330 unsigned int next_page;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200331
332 /* write_max is at most a page */
333 if (count > at24->write_max)
334 count = at24->write_max;
335
336 /* Never roll over backwards, to the start of this page */
337 next_page = roundup(offset + 1, at24->chip.page_size);
338 if (offset + count > next_page)
339 count = next_page - offset;
340
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200341 return count;
342}
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200343
Heiner Kallweit8e5888e2017-11-28 21:51:45 +0100344static ssize_t at24_regmap_write(struct at24_data *at24, const char *buf,
345 unsigned int offset, size_t count)
346{
347 unsigned long timeout, write_time;
348 struct at24_client *at24_client;
349 struct i2c_client *client;
350 struct regmap *regmap;
351 int ret;
352
353 at24_client = at24_translate_offset(at24, &offset);
354 regmap = at24_client->regmap;
355 client = at24_client->client;
356 count = at24_adjust_write_count(at24, offset, count);
357
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100358 at24_loop_until_timeout(timeout, write_time) {
Heiner Kallweit8e5888e2017-11-28 21:51:45 +0100359 ret = regmap_bulk_write(regmap, offset, buf, count);
360 dev_dbg(&client->dev, "write %zu@%d --> %d (%ld)\n",
361 count, offset, ret, jiffies);
362 if (!ret)
363 return count;
364 }
365
366 return -ETIMEDOUT;
367}
368
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200369static int at24_read(void *priv, unsigned int off, void *val, size_t count)
370{
Bartosz Golaszewski5ca2b5b2018-03-19 10:17:03 +0100371 struct at24_data *at24;
372 struct device *dev;
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200373 char *buf = val;
Divagar Mohandass98e82012017-10-10 11:30:37 +0530374 int ret;
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200375
Bartosz Golaszewski5ca2b5b2018-03-19 10:17:03 +0100376 at24 = priv;
377 dev = &at24->client[0].client->dev;
378
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200379 if (unlikely(!count))
380 return count;
381
Heiner Kallweitd9bcd462017-11-24 07:47:50 +0100382 if (off + count > at24->chip.byte_len)
383 return -EINVAL;
384
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500385 ret = pm_runtime_get_sync(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530386 if (ret < 0) {
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500387 pm_runtime_put_noidle(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530388 return ret;
389 }
390
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200391 /*
392 * Read data from chip, protecting against concurrent updates
393 * from this host, but not from other I2C masters.
394 */
395 mutex_lock(&at24->lock);
396
397 while (count) {
Bartosz Golaszewskieb27fde2018-03-19 10:17:06 +0100398 ret = at24_regmap_read(at24, buf, off, count);
399 if (ret < 0) {
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200400 mutex_unlock(&at24->lock);
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500401 pm_runtime_put(dev);
Bartosz Golaszewskieb27fde2018-03-19 10:17:06 +0100402 return ret;
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200403 }
Bartosz Golaszewskieb27fde2018-03-19 10:17:06 +0100404 buf += ret;
405 off += ret;
406 count -= ret;
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200407 }
408
409 mutex_unlock(&at24->lock);
410
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500411 pm_runtime_put(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530412
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200413 return 0;
414}
415
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100416static int at24_write(void *priv, unsigned int off, void *val, size_t count)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200417{
Bartosz Golaszewski5ca2b5b2018-03-19 10:17:03 +0100418 struct at24_data *at24;
419 struct device *dev;
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100420 char *buf = val;
Divagar Mohandass98e82012017-10-10 11:30:37 +0530421 int ret;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200422
Bartosz Golaszewski5ca2b5b2018-03-19 10:17:03 +0100423 at24 = priv;
424 dev = &at24->client[0].client->dev;
425
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200426 if (unlikely(!count))
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100427 return -EINVAL;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200428
Heiner Kallweitd9bcd462017-11-24 07:47:50 +0100429 if (off + count > at24->chip.byte_len)
430 return -EINVAL;
431
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500432 ret = pm_runtime_get_sync(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530433 if (ret < 0) {
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500434 pm_runtime_put_noidle(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530435 return ret;
436 }
437
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200438 /*
439 * Write data to chip, protecting against concurrent updates
440 * from this host, but not from other I2C masters.
441 */
442 mutex_lock(&at24->lock);
Bartosz Golaszewski6ce261e2017-12-19 11:28:54 +0100443 gpiod_set_value_cansleep(at24->wp_gpio, 0);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200444
445 while (count) {
Bartosz Golaszewskic4fee332018-03-19 10:17:07 +0100446 ret = at24_regmap_write(at24, buf, off, count);
447 if (ret < 0) {
Bartosz Golaszewski6ce261e2017-12-19 11:28:54 +0100448 gpiod_set_value_cansleep(at24->wp_gpio, 1);
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100449 mutex_unlock(&at24->lock);
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500450 pm_runtime_put(dev);
Bartosz Golaszewskic4fee332018-03-19 10:17:07 +0100451 return ret;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200452 }
Bartosz Golaszewskic4fee332018-03-19 10:17:07 +0100453 buf += ret;
454 off += ret;
455 count -= ret;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200456 }
457
Bartosz Golaszewski6ce261e2017-12-19 11:28:54 +0100458 gpiod_set_value_cansleep(at24->wp_gpio, 1);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200459 mutex_unlock(&at24->lock);
460
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500461 pm_runtime_put(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530462
Andrew Lunn57d15552016-02-26 20:59:20 +0100463 return 0;
464}
465
Bartosz Golaszewski1f77d182018-03-19 10:17:10 +0100466static void at24_properties_to_pdata(struct device *dev,
467 struct at24_platform_data *chip)
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100468{
Ben Gardnerdd905a62017-02-09 11:36:08 -0600469 int err;
470 u32 val;
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100471
Ben Gardnerdd905a62017-02-09 11:36:08 -0600472 if (device_property_present(dev, "read-only"))
473 chip->flags |= AT24_FLAG_READONLY;
Sven Van Asbroecke32213f2017-12-08 11:28:30 -0500474 if (device_property_present(dev, "no-read-rollover"))
475 chip->flags |= AT24_FLAG_NO_RDROL;
Ben Gardnerdd905a62017-02-09 11:36:08 -0600476
Divagar Mohandassdbc1ab92017-10-10 11:30:36 +0530477 err = device_property_read_u32(dev, "size", &val);
478 if (!err)
479 chip->byte_len = val;
480
Ben Gardnerdd905a62017-02-09 11:36:08 -0600481 err = device_property_read_u32(dev, "pagesize", &val);
482 if (!err) {
483 chip->page_size = val;
484 } else {
485 /*
486 * This is slow, but we can't know all eeproms, so we better
487 * play safe. Specifying custom eeprom-types via platform_data
488 * is recommended anyhow.
489 */
490 chip->page_size = 1;
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100491 }
492}
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100493
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100494static unsigned int at24_get_offset_adj(u8 flags, unsigned int byte_len)
495{
496 if (flags & AT24_FLAG_MAC) {
497 /* EUI-48 starts from 0x9a, EUI-64 from 0x98 */
498 return 0xa0 - byte_len;
499 } else if (flags & AT24_FLAG_SERIAL && flags & AT24_FLAG_ADDR16) {
500 /*
501 * For 16 bit address pointers, the word address must contain
502 * a '10' sequence in bits 11 and 10 regardless of the
503 * intended position of the address pointer.
504 */
505 return 0x0800;
506 } else if (flags & AT24_FLAG_SERIAL) {
507 /*
508 * Otherwise the word address must begin with a '10' sequence,
509 * regardless of the intended address.
510 */
511 return 0x0080;
512 } else {
513 return 0;
514 }
515}
516
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200517static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
518{
Bartosz Golaszewskieef69392017-12-18 18:24:43 +0100519 struct regmap_config regmap_config = { };
Bartosz Golaszewski8cdc4e72018-03-19 10:17:02 +0100520 struct nvmem_config nvmem_config = { };
Bartosz Golaszewski5ca2b5b2018-03-19 10:17:03 +0100521 const struct at24_chip_data *cd = NULL;
Bartosz Golaszewskif2adff62018-03-19 10:17:11 +0100522 struct at24_platform_data pdata = { };
Bartosz Golaszewski021c7d72018-03-19 10:17:12 +0100523 struct device *dev = &client->dev;
Bartosz Golaszewski5ca2b5b2018-03-19 10:17:03 +0100524 unsigned int i, num_addresses;
525 struct at24_data *at24;
526 bool writable;
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200527 u8 test_byte;
Bartosz Golaszewski5ca2b5b2018-03-19 10:17:03 +0100528 int err;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200529
Bartosz Golaszewski021c7d72018-03-19 10:17:12 +0100530 if (dev->platform_data) {
531 pdata = *(struct at24_platform_data *)dev->platform_data;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200532 } else {
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200533 /*
534 * The I2C core allows OF nodes compatibles to match against the
535 * I2C device ID table as a fallback, so check not only if an OF
536 * node is present but also if it matches an OF device ID entry.
537 */
Bartosz Golaszewski021c7d72018-03-19 10:17:12 +0100538 if (dev->of_node && of_match_device(at24_of_match, dev)) {
539 cd = of_device_get_match_data(dev);
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200540 } else if (id) {
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500541 cd = (void *)id->driver_data;
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300542 } else {
543 const struct acpi_device_id *aid;
544
Bartosz Golaszewski021c7d72018-03-19 10:17:12 +0100545 aid = acpi_match_device(at24_acpi_ids, dev);
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300546 if (aid)
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500547 cd = (void *)aid->driver_data;
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300548 }
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500549 if (!cd)
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700550 return -ENODEV;
551
Bartosz Golaszewskif2adff62018-03-19 10:17:11 +0100552 pdata.byte_len = cd->byte_len;
553 pdata.flags = cd->flags;
Bartosz Golaszewski021c7d72018-03-19 10:17:12 +0100554 at24_properties_to_pdata(dev, &pdata);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200555 }
556
Bartosz Golaszewskif2adff62018-03-19 10:17:11 +0100557 if (!pdata.page_size) {
Bartosz Golaszewski021c7d72018-03-19 10:17:12 +0100558 dev_err(dev, "page_size must not be 0!\n");
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700559 return -EINVAL;
Wolfram Sang45efe842010-11-17 13:00:49 +0100560 }
Bartosz Golaszewskif2adff62018-03-19 10:17:11 +0100561 if (!is_power_of_2(pdata.page_size))
Bartosz Golaszewski021c7d72018-03-19 10:17:12 +0100562 dev_warn(dev, "page_size looks suspicious (no power of 2)!\n");
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200563
Heiner Kallweita23727c2017-11-28 21:51:54 +0100564 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C) &&
565 !i2c_check_functionality(client->adapter,
566 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK))
Bartosz Golaszewskif2adff62018-03-19 10:17:11 +0100567 pdata.page_size = 1;
Christian Gmeinera839ce62014-10-09 11:07:58 +0200568
Bartosz Golaszewskif2adff62018-03-19 10:17:11 +0100569 if (pdata.flags & AT24_FLAG_TAKE8ADDR)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200570 num_addresses = 8;
571 else
Bartosz Golaszewskif2adff62018-03-19 10:17:11 +0100572 num_addresses = DIV_ROUND_UP(pdata.byte_len,
573 (pdata.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200574
Bartosz Golaszewskieef69392017-12-18 18:24:43 +0100575 regmap_config.val_bits = 8;
Bartosz Golaszewskif2adff62018-03-19 10:17:11 +0100576 regmap_config.reg_bits = (pdata.flags & AT24_FLAG_ADDR16) ? 16 : 8;
Bartosz Golaszewskid1543162018-03-19 10:17:01 +0100577 regmap_config.disable_locking = true;
Heiner Kallweit5c015252017-11-28 21:51:40 +0100578
Bartosz Golaszewski021c7d72018-03-19 10:17:12 +0100579 at24 = devm_kzalloc(dev, sizeof(struct at24_data) + num_addresses *
580 sizeof(struct at24_client), GFP_KERNEL);
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700581 if (!at24)
582 return -ENOMEM;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200583
584 mutex_init(&at24->lock);
Bartosz Golaszewskif2adff62018-03-19 10:17:11 +0100585 at24->chip = pdata;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200586 at24->num_addresses = num_addresses;
Bartosz Golaszewskif2adff62018-03-19 10:17:11 +0100587 at24->offset_adj = at24_get_offset_adj(pdata.flags, pdata.byte_len);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200588
Bartosz Golaszewski021c7d72018-03-19 10:17:12 +0100589 at24->wp_gpio = devm_gpiod_get_optional(dev, "wp", GPIOD_OUT_HIGH);
Bartosz Golaszewski6ce261e2017-12-19 11:28:54 +0100590 if (IS_ERR(at24->wp_gpio))
591 return PTR_ERR(at24->wp_gpio);
592
Heiner Kallweit5c015252017-11-28 21:51:40 +0100593 at24->client[0].client = client;
Bartosz Golaszewskieef69392017-12-18 18:24:43 +0100594 at24->client[0].regmap = devm_regmap_init_i2c(client, &regmap_config);
Heiner Kallweit5c015252017-11-28 21:51:40 +0100595 if (IS_ERR(at24->client[0].regmap))
596 return PTR_ERR(at24->client[0].regmap);
597
Bartosz Golaszewskif2adff62018-03-19 10:17:11 +0100598 if ((pdata.flags & AT24_FLAG_SERIAL) && (pdata.flags & AT24_FLAG_MAC)) {
Bartosz Golaszewski021c7d72018-03-19 10:17:12 +0100599 dev_err(dev,
Bartosz Golaszewski0b813652016-06-06 10:48:54 +0200600 "invalid device data - cannot have both AT24_FLAG_SERIAL & AT24_FLAG_MAC.");
601 return -EINVAL;
602 }
603
Bartosz Golaszewskif2adff62018-03-19 10:17:11 +0100604 writable = !(pdata.flags & AT24_FLAG_READONLY);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200605 if (writable) {
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100606 at24->write_max = min_t(unsigned int,
Bartosz Golaszewskif2adff62018-03-19 10:17:11 +0100607 pdata.page_size, at24_io_limit);
Heiner Kallweita23727c2017-11-28 21:51:54 +0100608 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C) &&
609 at24->write_max > I2C_SMBUS_BLOCK_MAX)
610 at24->write_max = I2C_SMBUS_BLOCK_MAX;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200611 }
612
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200613 /* use dummy devices for multiple-address chips */
614 for (i = 1; i < num_addresses; i++) {
Heiner Kallweit5c015252017-11-28 21:51:40 +0100615 at24->client[i].client = i2c_new_dummy(client->adapter,
616 client->addr + i);
617 if (!at24->client[i].client) {
Bartosz Golaszewski021c7d72018-03-19 10:17:12 +0100618 dev_err(dev, "address 0x%02x unavailable\n",
619 client->addr + i);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200620 err = -EADDRINUSE;
621 goto err_clients;
622 }
Heiner Kallweit5c015252017-11-28 21:51:40 +0100623 at24->client[i].regmap = devm_regmap_init_i2c(
Bartosz Golaszewskieef69392017-12-18 18:24:43 +0100624 at24->client[i].client,
625 &regmap_config);
Heiner Kallweit5c015252017-11-28 21:51:40 +0100626 if (IS_ERR(at24->client[i].regmap)) {
627 err = PTR_ERR(at24->client[i].regmap);
628 goto err_clients;
629 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200630 }
631
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200632 i2c_set_clientdata(client, at24);
633
Divagar Mohandass98e82012017-10-10 11:30:37 +0530634 /* enable runtime pm */
Bartosz Golaszewski021c7d72018-03-19 10:17:12 +0100635 pm_runtime_set_active(dev);
636 pm_runtime_enable(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530637
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200638 /*
639 * Perform a one-byte test read to verify that the
640 * chip is functional.
641 */
642 err = at24_read(at24, 0, &test_byte, 1);
Bartosz Golaszewski021c7d72018-03-19 10:17:12 +0100643 pm_runtime_idle(dev);
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200644 if (err) {
645 err = -ENODEV;
646 goto err_clients;
647 }
648
Bartosz Golaszewski021c7d72018-03-19 10:17:12 +0100649 nvmem_config.name = dev_name(dev);
650 nvmem_config.dev = dev;
Bartosz Golaszewski8cdc4e72018-03-19 10:17:02 +0100651 nvmem_config.read_only = !writable;
652 nvmem_config.root_only = true;
653 nvmem_config.owner = THIS_MODULE;
654 nvmem_config.compat = true;
Bartosz Golaszewski021c7d72018-03-19 10:17:12 +0100655 nvmem_config.base_dev = dev;
Bartosz Golaszewski8cdc4e72018-03-19 10:17:02 +0100656 nvmem_config.reg_read = at24_read;
657 nvmem_config.reg_write = at24_write;
658 nvmem_config.priv = at24;
659 nvmem_config.stride = 1;
660 nvmem_config.word_size = 1;
Bartosz Golaszewskif2adff62018-03-19 10:17:11 +0100661 nvmem_config.size = pdata.byte_len;
Andrew Lunn57d15552016-02-26 20:59:20 +0100662
Bartosz Golaszewski8cdc4e72018-03-19 10:17:02 +0100663 at24->nvmem = nvmem_register(&nvmem_config);
Andrew Lunn57d15552016-02-26 20:59:20 +0100664
665 if (IS_ERR(at24->nvmem)) {
666 err = PTR_ERR(at24->nvmem);
667 goto err_clients;
668 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200669
Bartosz Golaszewski021c7d72018-03-19 10:17:12 +0100670 dev_info(dev, "%u byte %s EEPROM, %s, %u bytes/write\n",
Bartosz Golaszewskif2adff62018-03-19 10:17:11 +0100671 pdata.byte_len, client->name,
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100672 writable ? "writable" : "read-only", at24->write_max);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200673
Kevin Hilman7274ec82009-04-02 16:56:57 -0700674 /* export data to kernel code */
Bartosz Golaszewskif2adff62018-03-19 10:17:11 +0100675 if (pdata.setup)
676 pdata.setup(at24->nvmem, pdata.context);
Kevin Hilman7274ec82009-04-02 16:56:57 -0700677
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200678 return 0;
679
680err_clients:
681 for (i = 1; i < num_addresses; i++)
Heiner Kallweit5c015252017-11-28 21:51:40 +0100682 if (at24->client[i].client)
683 i2c_unregister_device(at24->client[i].client);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200684
Bartosz Golaszewski021c7d72018-03-19 10:17:12 +0100685 pm_runtime_disable(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530686
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200687 return err;
688}
689
Bill Pemberton486a5c22012-11-19 13:26:02 -0500690static int at24_remove(struct i2c_client *client)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200691{
692 struct at24_data *at24;
693 int i;
694
695 at24 = i2c_get_clientdata(client);
Andrew Lunn57d15552016-02-26 20:59:20 +0100696
697 nvmem_unregister(at24->nvmem);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200698
699 for (i = 1; i < at24->num_addresses; i++)
Heiner Kallweit5c015252017-11-28 21:51:40 +0100700 i2c_unregister_device(at24->client[i].client);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200701
Divagar Mohandass98e82012017-10-10 11:30:37 +0530702 pm_runtime_disable(&client->dev);
703 pm_runtime_set_suspended(&client->dev);
704
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200705 return 0;
706}
707
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200708static struct i2c_driver at24_driver = {
709 .driver = {
710 .name = "at24",
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200711 .of_match_table = at24_of_match,
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300712 .acpi_match_table = ACPI_PTR(at24_acpi_ids),
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200713 },
714 .probe = at24_probe,
Bill Pemberton2d6bed92012-11-19 13:21:23 -0500715 .remove = at24_remove,
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200716 .id_table = at24_ids,
717};
718
719static int __init at24_init(void)
720{
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100721 if (!at24_io_limit) {
722 pr_err("at24: at24_io_limit must not be 0!\n");
Wolfram Sang45efe842010-11-17 13:00:49 +0100723 return -EINVAL;
724 }
725
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100726 at24_io_limit = rounddown_pow_of_two(at24_io_limit);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200727 return i2c_add_driver(&at24_driver);
728}
729module_init(at24_init);
730
731static void __exit at24_exit(void)
732{
733 i2c_del_driver(&at24_driver);
734}
735module_exit(at24_exit);
736
737MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
738MODULE_AUTHOR("David Brownell and Wolfram Sang");
739MODULE_LICENSE("GPL");