blob: b1f78b99f8a152bba007b58d31cf26c0db2debfd [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>
Wolfram Sang2b7a5052008-07-14 22:38:35 +020030
31/*
32 * I2C EEPROMs from most vendors are inexpensive and mostly interchangeable.
33 * Differences between different vendor product lines (like Atmel AT24C or
34 * MicroChip 24LC, etc) won't much matter for typical read/write access.
35 * There are also I2C RAM chips, likewise interchangeable. One example
36 * would be the PCF8570, which acts like a 24c02 EEPROM (256 bytes).
37 *
38 * However, misconfiguration can lose data. "Set 16-bit memory address"
39 * to a part with 8-bit addressing will overwrite data. Writing with too
40 * big a page size also loses data. And it's not safe to assume that the
41 * conventional addresses 0x50..0x57 only hold eeproms; a PCF8563 RTC
42 * uses 0x51, for just one example.
43 *
44 * Accordingly, explicit board-specific configuration data should be used
45 * in almost all cases. (One partial exception is an SMBus used to access
46 * "SPD" data for DRAM sticks. Those only use 24c02 EEPROMs.)
47 *
48 * So this driver uses "new style" I2C driver binding, expecting to be
49 * told what devices exist. That may be in arch/X/mach-Y/board-Z.c or
50 * similar kernel-resident tables; or, configuration data coming from
51 * a bootloader.
52 *
53 * Other than binding model, current differences from "eeprom" driver are
54 * that this one handles write access and isn't restricted to 24c02 devices.
55 * It also handles larger devices (32 kbit and up) with two-byte addresses,
56 * which won't work on pure SMBus systems.
57 */
58
Heiner Kallweit5c015252017-11-28 21:51:40 +010059struct at24_client {
60 struct i2c_client *client;
61 struct regmap *regmap;
62};
63
Wolfram Sang2b7a5052008-07-14 22:38:35 +020064struct at24_data {
65 struct at24_platform_data chip;
Bartosz Golaszewski318aa9c2016-06-06 10:48:46 +020066
Wolfram Sang2b7a5052008-07-14 22:38:35 +020067 /*
68 * Lock protects against activities from other Linux tasks,
69 * but not from changes by other I2C masters.
70 */
71 struct mutex lock;
Wolfram Sang2b7a5052008-07-14 22:38:35 +020072
Bartosz Golaszewskiaa4ce222017-12-13 11:56:23 +010073 unsigned int write_max;
74 unsigned int num_addresses;
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +010075 unsigned int offset_adj;
Wolfram Sang2b7a5052008-07-14 22:38:35 +020076
Andrew Lunn57d15552016-02-26 20:59:20 +010077 struct nvmem_config nvmem_config;
78 struct nvmem_device *nvmem;
79
Wolfram Sang2b7a5052008-07-14 22:38:35 +020080 /*
81 * Some chips tie up multiple I2C addresses; dummy devices reserve
82 * them for us, and we'll use them with SMBus calls.
83 */
Heiner Kallweit5c015252017-11-28 21:51:40 +010084 struct at24_client client[];
Wolfram Sang2b7a5052008-07-14 22:38:35 +020085};
86
87/*
88 * This parameter is to help this driver avoid blocking other drivers out
89 * of I2C for potentially troublesome amounts of time. With a 100 kHz I2C
90 * clock, one 256 byte read takes about 1/43 second which is excessive;
91 * but the 1/170 second it takes at 400 kHz may be quite reasonable; and
92 * at 1 MHz (Fm+) a 1/430 second delay could easily be invisible.
93 *
94 * This value is forced to be a power of two so that writes align on pages.
95 */
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +010096static unsigned int at24_io_limit = 128;
97module_param_named(io_limit, at24_io_limit, uint, 0);
98MODULE_PARM_DESC(at24_io_limit, "Maximum bytes per I/O (default 128)");
Wolfram Sang2b7a5052008-07-14 22:38:35 +020099
100/*
101 * Specs often allow 5 msec for a page write, sometimes 20 msec;
102 * it's important to recover from write timeouts.
103 */
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100104static unsigned int at24_write_timeout = 25;
105module_param_named(write_timeout, at24_write_timeout, uint, 0);
106MODULE_PARM_DESC(at24_write_timeout, "Time (in ms) to try writes (default 25)");
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200107
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200108/*
109 * Both reads and writes fail if the previous write didn't complete yet. This
110 * macro loops a few times waiting at least long enough for one entire page
Bartosz Golaszewski24da3cc2016-07-17 20:40:06 +0200111 * write to work while making sure that at least one iteration is run before
112 * checking the break condition.
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200113 *
114 * It takes two parameters: a variable in which the future timeout in jiffies
115 * will be stored and a temporary variable holding the time of the last
116 * iteration of processing the request. Both should be unsigned integers
117 * holding at least 32 bits.
118 */
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100119#define at24_loop_until_timeout(tout, op_time) \
120 for (tout = jiffies + msecs_to_jiffies(at24_write_timeout), \
121 op_time = 0; \
Bartosz Golaszewski24da3cc2016-07-17 20:40:06 +0200122 op_time ? time_before(op_time, tout) : true; \
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200123 usleep_range(1000, 1500), op_time = jiffies)
124
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500125struct at24_chip_data {
126 /*
127 * these fields mirror their equivalents in
128 * struct at24_platform_data
129 */
130 u32 byte_len;
131 u8 flags;
132};
133
134#define AT24_CHIP_DATA(_name, _len, _flags) \
135 static const struct at24_chip_data _name = { \
136 .byte_len = _len, .flags = _flags, \
137 }
138
139/* needs 8 addresses as A0-A2 are ignored */
140AT24_CHIP_DATA(at24_data_24c00, 128 / 8, AT24_FLAG_TAKE8ADDR);
141/* old variants can't be handled with this generic entry! */
142AT24_CHIP_DATA(at24_data_24c01, 1024 / 8, 0);
143AT24_CHIP_DATA(at24_data_24cs01, 16,
144 AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
145AT24_CHIP_DATA(at24_data_24c02, 2048 / 8, 0);
146AT24_CHIP_DATA(at24_data_24cs02, 16,
147 AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
148AT24_CHIP_DATA(at24_data_24mac402, 48 / 8,
149 AT24_FLAG_MAC | AT24_FLAG_READONLY);
150AT24_CHIP_DATA(at24_data_24mac602, 64 / 8,
151 AT24_FLAG_MAC | AT24_FLAG_READONLY);
152/* spd is a 24c02 in memory DIMMs */
153AT24_CHIP_DATA(at24_data_spd, 2048 / 8,
154 AT24_FLAG_READONLY | AT24_FLAG_IRUGO);
155AT24_CHIP_DATA(at24_data_24c04, 4096 / 8, 0);
156AT24_CHIP_DATA(at24_data_24cs04, 16,
157 AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
158/* 24rf08 quirk is handled at i2c-core */
159AT24_CHIP_DATA(at24_data_24c08, 8192 / 8, 0);
160AT24_CHIP_DATA(at24_data_24cs08, 16,
161 AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
162AT24_CHIP_DATA(at24_data_24c16, 16384 / 8, 0);
163AT24_CHIP_DATA(at24_data_24cs16, 16,
164 AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
165AT24_CHIP_DATA(at24_data_24c32, 32768 / 8, AT24_FLAG_ADDR16);
166AT24_CHIP_DATA(at24_data_24cs32, 16,
167 AT24_FLAG_ADDR16 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
168AT24_CHIP_DATA(at24_data_24c64, 65536 / 8, AT24_FLAG_ADDR16);
169AT24_CHIP_DATA(at24_data_24cs64, 16,
170 AT24_FLAG_ADDR16 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
171AT24_CHIP_DATA(at24_data_24c128, 131072 / 8, AT24_FLAG_ADDR16);
172AT24_CHIP_DATA(at24_data_24c256, 262144 / 8, AT24_FLAG_ADDR16);
173AT24_CHIP_DATA(at24_data_24c512, 524288 / 8, AT24_FLAG_ADDR16);
174AT24_CHIP_DATA(at24_data_24c1024, 1048576 / 8, AT24_FLAG_ADDR16);
175/* identical to 24c08 ? */
176AT24_CHIP_DATA(at24_data_INT3499, 8192 / 8, 0);
177
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200178static const struct i2c_device_id at24_ids[] = {
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500179 { "24c00", (kernel_ulong_t)&at24_data_24c00 },
180 { "24c01", (kernel_ulong_t)&at24_data_24c01 },
181 { "24cs01", (kernel_ulong_t)&at24_data_24cs01 },
182 { "24c02", (kernel_ulong_t)&at24_data_24c02 },
183 { "24cs02", (kernel_ulong_t)&at24_data_24cs02 },
184 { "24mac402", (kernel_ulong_t)&at24_data_24mac402 },
185 { "24mac602", (kernel_ulong_t)&at24_data_24mac602 },
186 { "spd", (kernel_ulong_t)&at24_data_spd },
187 { "24c04", (kernel_ulong_t)&at24_data_24c04 },
188 { "24cs04", (kernel_ulong_t)&at24_data_24cs04 },
189 { "24c08", (kernel_ulong_t)&at24_data_24c08 },
190 { "24cs08", (kernel_ulong_t)&at24_data_24cs08 },
191 { "24c16", (kernel_ulong_t)&at24_data_24c16 },
192 { "24cs16", (kernel_ulong_t)&at24_data_24cs16 },
193 { "24c32", (kernel_ulong_t)&at24_data_24c32 },
194 { "24cs32", (kernel_ulong_t)&at24_data_24cs32 },
195 { "24c64", (kernel_ulong_t)&at24_data_24c64 },
196 { "24cs64", (kernel_ulong_t)&at24_data_24cs64 },
197 { "24c128", (kernel_ulong_t)&at24_data_24c128 },
198 { "24c256", (kernel_ulong_t)&at24_data_24c256 },
199 { "24c512", (kernel_ulong_t)&at24_data_24c512 },
200 { "24c1024", (kernel_ulong_t)&at24_data_24c1024 },
201 { "at24", 0 },
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200202 { /* END OF LIST */ }
203};
204MODULE_DEVICE_TABLE(i2c, at24_ids);
205
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200206static const struct of_device_id at24_of_match[] = {
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500207 { .compatible = "atmel,24c00", .data = &at24_data_24c00 },
208 { .compatible = "atmel,24c01", .data = &at24_data_24c01 },
209 { .compatible = "atmel,24c02", .data = &at24_data_24c02 },
210 { .compatible = "atmel,spd", .data = &at24_data_spd },
211 { .compatible = "atmel,24c04", .data = &at24_data_24c04 },
212 { .compatible = "atmel,24c08", .data = &at24_data_24c08 },
213 { .compatible = "atmel,24c16", .data = &at24_data_24c16 },
214 { .compatible = "atmel,24c32", .data = &at24_data_24c32 },
215 { .compatible = "atmel,24c64", .data = &at24_data_24c64 },
216 { .compatible = "atmel,24c128", .data = &at24_data_24c128 },
217 { .compatible = "atmel,24c256", .data = &at24_data_24c256 },
218 { .compatible = "atmel,24c512", .data = &at24_data_24c512 },
219 { .compatible = "atmel,24c1024", .data = &at24_data_24c1024 },
220 { /* END OF LIST */ },
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200221};
222MODULE_DEVICE_TABLE(of, at24_of_match);
223
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300224static const struct acpi_device_id at24_acpi_ids[] = {
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500225 { "INT3499", (kernel_ulong_t)&at24_data_INT3499 },
226 { /* END OF LIST */ }
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300227};
228MODULE_DEVICE_TABLE(acpi, at24_acpi_ids);
229
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200230/*-------------------------------------------------------------------------*/
231
232/*
233 * This routine supports chips which consume multiple I2C addresses. It
234 * computes the addressing information to be used for a given r/w request.
235 * Assumes that sanity checks for offset happened at sysfs-layer.
Bartosz Golaszewski9afd68662016-06-06 10:48:48 +0200236 *
237 * Slave address and byte offset derive from the offset. Always
238 * set the byte address; on a multi-master board, another master
239 * may have changed the chip's "current" address pointer.
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200240 */
Heiner Kallweit46049482017-11-28 21:51:42 +0100241static struct at24_client *at24_translate_offset(struct at24_data *at24,
242 unsigned int *offset)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200243{
Bartosz Golaszewskiaa4ce222017-12-13 11:56:23 +0100244 unsigned int i;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200245
246 if (at24->chip.flags & AT24_FLAG_ADDR16) {
247 i = *offset >> 16;
248 *offset &= 0xffff;
249 } else {
250 i = *offset >> 8;
251 *offset &= 0xff;
252 }
253
Heiner Kallweit46049482017-11-28 21:51:42 +0100254 return &at24->client[i];
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200255}
256
Sven Van Asbroecke32213f2017-12-08 11:28:30 -0500257static size_t at24_adjust_read_count(struct at24_data *at24,
258 unsigned int offset, size_t count)
259{
260 unsigned int bits;
261 size_t remainder;
262
263 /*
264 * In case of multi-address chips that don't rollover reads to
265 * the next slave address: truncate the count to the slave boundary,
266 * so that the read never straddles slaves.
267 */
268 if (at24->chip.flags & AT24_FLAG_NO_RDROL) {
269 bits = (at24->chip.flags & AT24_FLAG_ADDR16) ? 16 : 8;
270 remainder = BIT(bits) - offset;
271 if (count > remainder)
272 count = remainder;
273 }
274
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100275 if (count > at24_io_limit)
276 count = at24_io_limit;
Sven Van Asbroecke32213f2017-12-08 11:28:30 -0500277
278 return count;
279}
280
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100281static ssize_t at24_regmap_read(struct at24_data *at24, char *buf,
282 unsigned int offset, size_t count)
283{
284 unsigned long timeout, read_time;
285 struct at24_client *at24_client;
286 struct i2c_client *client;
287 struct regmap *regmap;
288 int ret;
289
290 at24_client = at24_translate_offset(at24, &offset);
291 regmap = at24_client->regmap;
292 client = at24_client->client;
Sven Van Asbroecke32213f2017-12-08 11:28:30 -0500293 count = at24_adjust_read_count(at24, offset, count);
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100294
295 /* adjust offset for mac and serial read ops */
296 offset += at24->offset_adj;
297
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100298 at24_loop_until_timeout(timeout, read_time) {
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100299 ret = regmap_bulk_read(regmap, offset, buf, count);
300 dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
301 count, offset, ret, jiffies);
302 if (!ret)
303 return count;
304 }
305
306 return -ETIMEDOUT;
307}
308
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200309/*
310 * Note that if the hardware write-protect pin is pulled high, the whole
311 * chip is normally write protected. But there are plenty of product
312 * variants here, including OTP fuses and partial chip protect.
313 *
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200314 * We only use page mode writes; the alternative is sloooow. These routines
315 * write at most one page.
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200316 */
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200317
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200318static size_t at24_adjust_write_count(struct at24_data *at24,
319 unsigned int offset, size_t count)
320{
Bartosz Golaszewskiaa4ce222017-12-13 11:56:23 +0100321 unsigned int next_page;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200322
323 /* write_max is at most a page */
324 if (count > at24->write_max)
325 count = at24->write_max;
326
327 /* Never roll over backwards, to the start of this page */
328 next_page = roundup(offset + 1, at24->chip.page_size);
329 if (offset + count > next_page)
330 count = next_page - offset;
331
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200332 return count;
333}
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200334
Heiner Kallweit8e5888e2017-11-28 21:51:45 +0100335static ssize_t at24_regmap_write(struct at24_data *at24, const char *buf,
336 unsigned int offset, size_t count)
337{
338 unsigned long timeout, write_time;
339 struct at24_client *at24_client;
340 struct i2c_client *client;
341 struct regmap *regmap;
342 int ret;
343
344 at24_client = at24_translate_offset(at24, &offset);
345 regmap = at24_client->regmap;
346 client = at24_client->client;
347 count = at24_adjust_write_count(at24, offset, count);
348
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100349 at24_loop_until_timeout(timeout, write_time) {
Heiner Kallweit8e5888e2017-11-28 21:51:45 +0100350 ret = regmap_bulk_write(regmap, offset, buf, count);
351 dev_dbg(&client->dev, "write %zu@%d --> %d (%ld)\n",
352 count, offset, ret, jiffies);
353 if (!ret)
354 return count;
355 }
356
357 return -ETIMEDOUT;
358}
359
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200360static int at24_read(void *priv, unsigned int off, void *val, size_t count)
361{
362 struct at24_data *at24 = priv;
Heiner Kallweit5c015252017-11-28 21:51:40 +0100363 struct device *dev = &at24->client[0].client->dev;
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200364 char *buf = val;
Divagar Mohandass98e82012017-10-10 11:30:37 +0530365 int ret;
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200366
367 if (unlikely(!count))
368 return count;
369
Heiner Kallweitd9bcd462017-11-24 07:47:50 +0100370 if (off + count > at24->chip.byte_len)
371 return -EINVAL;
372
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500373 ret = pm_runtime_get_sync(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530374 if (ret < 0) {
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500375 pm_runtime_put_noidle(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530376 return ret;
377 }
378
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200379 /*
380 * Read data from chip, protecting against concurrent updates
381 * from this host, but not from other I2C masters.
382 */
383 mutex_lock(&at24->lock);
384
385 while (count) {
386 int status;
387
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100388 status = at24_regmap_read(at24, buf, off, count);
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200389 if (status < 0) {
390 mutex_unlock(&at24->lock);
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500391 pm_runtime_put(dev);
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200392 return status;
393 }
394 buf += status;
395 off += status;
396 count -= status;
397 }
398
399 mutex_unlock(&at24->lock);
400
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500401 pm_runtime_put(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530402
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200403 return 0;
404}
405
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100406static int at24_write(void *priv, unsigned int off, void *val, size_t count)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200407{
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100408 struct at24_data *at24 = priv;
Heiner Kallweit5c015252017-11-28 21:51:40 +0100409 struct device *dev = &at24->client[0].client->dev;
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100410 char *buf = val;
Divagar Mohandass98e82012017-10-10 11:30:37 +0530411 int ret;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200412
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200413 if (unlikely(!count))
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100414 return -EINVAL;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200415
Heiner Kallweitd9bcd462017-11-24 07:47:50 +0100416 if (off + count > at24->chip.byte_len)
417 return -EINVAL;
418
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500419 ret = pm_runtime_get_sync(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530420 if (ret < 0) {
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500421 pm_runtime_put_noidle(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530422 return ret;
423 }
424
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200425 /*
426 * Write data to chip, protecting against concurrent updates
427 * from this host, but not from other I2C masters.
428 */
429 mutex_lock(&at24->lock);
430
431 while (count) {
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100432 int status;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200433
Heiner Kallweit8e5888e2017-11-28 21:51:45 +0100434 status = at24_regmap_write(at24, buf, off, count);
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100435 if (status < 0) {
436 mutex_unlock(&at24->lock);
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500437 pm_runtime_put(dev);
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100438 return status;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200439 }
440 buf += status;
441 off += status;
442 count -= status;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200443 }
444
445 mutex_unlock(&at24->lock);
446
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500447 pm_runtime_put(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530448
Andrew Lunn57d15552016-02-26 20:59:20 +0100449 return 0;
450}
451
Ben Gardnerdd905a62017-02-09 11:36:08 -0600452static void at24_get_pdata(struct device *dev, struct at24_platform_data *chip)
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100453{
Ben Gardnerdd905a62017-02-09 11:36:08 -0600454 int err;
455 u32 val;
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100456
Ben Gardnerdd905a62017-02-09 11:36:08 -0600457 if (device_property_present(dev, "read-only"))
458 chip->flags |= AT24_FLAG_READONLY;
Sven Van Asbroecke32213f2017-12-08 11:28:30 -0500459 if (device_property_present(dev, "no-read-rollover"))
460 chip->flags |= AT24_FLAG_NO_RDROL;
Ben Gardnerdd905a62017-02-09 11:36:08 -0600461
Divagar Mohandassdbc1ab92017-10-10 11:30:36 +0530462 err = device_property_read_u32(dev, "size", &val);
463 if (!err)
464 chip->byte_len = val;
465
Ben Gardnerdd905a62017-02-09 11:36:08 -0600466 err = device_property_read_u32(dev, "pagesize", &val);
467 if (!err) {
468 chip->page_size = val;
469 } else {
470 /*
471 * This is slow, but we can't know all eeproms, so we better
472 * play safe. Specifying custom eeprom-types via platform_data
473 * is recommended anyhow.
474 */
475 chip->page_size = 1;
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100476 }
477}
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100478
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100479static unsigned int at24_get_offset_adj(u8 flags, unsigned int byte_len)
480{
481 if (flags & AT24_FLAG_MAC) {
482 /* EUI-48 starts from 0x9a, EUI-64 from 0x98 */
483 return 0xa0 - byte_len;
484 } else if (flags & AT24_FLAG_SERIAL && flags & AT24_FLAG_ADDR16) {
485 /*
486 * For 16 bit address pointers, the word address must contain
487 * a '10' sequence in bits 11 and 10 regardless of the
488 * intended position of the address pointer.
489 */
490 return 0x0800;
491 } else if (flags & AT24_FLAG_SERIAL) {
492 /*
493 * Otherwise the word address must begin with a '10' sequence,
494 * regardless of the intended address.
495 */
496 return 0x0080;
497 } else {
498 return 0;
499 }
500}
501
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200502static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
503{
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500504 struct at24_platform_data chip = { 0 };
505 const struct at24_chip_data *cd = NULL;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200506 bool writable;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200507 struct at24_data *at24;
508 int err;
Bartosz Golaszewskiaa4ce222017-12-13 11:56:23 +0100509 unsigned int i, num_addresses;
Bartosz Golaszewskieef69392017-12-18 18:24:43 +0100510 struct regmap_config regmap_config = { };
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200511 u8 test_byte;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200512
513 if (client->dev.platform_data) {
514 chip = *(struct at24_platform_data *)client->dev.platform_data;
515 } else {
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200516 /*
517 * The I2C core allows OF nodes compatibles to match against the
518 * I2C device ID table as a fallback, so check not only if an OF
519 * node is present but also if it matches an OF device ID entry.
520 */
521 if (client->dev.of_node &&
522 of_match_device(at24_of_match, &client->dev)) {
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500523 cd = of_device_get_match_data(&client->dev);
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200524 } else if (id) {
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500525 cd = (void *)id->driver_data;
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300526 } else {
527 const struct acpi_device_id *aid;
528
529 aid = acpi_match_device(at24_acpi_ids, &client->dev);
530 if (aid)
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500531 cd = (void *)aid->driver_data;
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300532 }
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500533 if (!cd)
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700534 return -ENODEV;
535
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500536 chip.byte_len = cd->byte_len;
537 chip.flags = cd->flags;
Ben Gardnerdd905a62017-02-09 11:36:08 -0600538 at24_get_pdata(&client->dev, &chip);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200539 }
540
541 if (!is_power_of_2(chip.byte_len))
542 dev_warn(&client->dev,
543 "byte_len looks suspicious (no power of 2)!\n");
Wolfram Sang45efe842010-11-17 13:00:49 +0100544 if (!chip.page_size) {
545 dev_err(&client->dev, "page_size must not be 0!\n");
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700546 return -EINVAL;
Wolfram Sang45efe842010-11-17 13:00:49 +0100547 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200548 if (!is_power_of_2(chip.page_size))
549 dev_warn(&client->dev,
550 "page_size looks suspicious (no power of 2)!\n");
551
Bartosz Golaszewski5478e472017-11-27 22:06:13 +0100552 /*
553 * REVISIT: the size of the EUI-48 byte array is 6 in at24mac402, while
554 * the call to ilog2() in AT24_DEVICE_MAGIC() rounds it down to 4.
555 *
556 * Eventually we'll get rid of the magic values altoghether in favor of
557 * real structs, but for now just manually set the right size.
558 */
559 if (chip.flags & AT24_FLAG_MAC && chip.byte_len == 4)
560 chip.byte_len = 6;
561
Heiner Kallweita23727c2017-11-28 21:51:54 +0100562 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C) &&
563 !i2c_check_functionality(client->adapter,
564 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK))
565 chip.page_size = 1;
Christian Gmeinera839ce62014-10-09 11:07:58 +0200566
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200567 if (chip.flags & AT24_FLAG_TAKE8ADDR)
568 num_addresses = 8;
569 else
570 num_addresses = DIV_ROUND_UP(chip.byte_len,
571 (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
572
Bartosz Golaszewskieef69392017-12-18 18:24:43 +0100573 regmap_config.val_bits = 8;
574 regmap_config.reg_bits = (chip.flags & AT24_FLAG_ADDR16) ? 16 : 8;
Heiner Kallweit5c015252017-11-28 21:51:40 +0100575
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700576 at24 = devm_kzalloc(&client->dev, sizeof(struct at24_data) +
Heiner Kallweit5c015252017-11-28 21:51:40 +0100577 num_addresses * sizeof(struct at24_client), GFP_KERNEL);
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700578 if (!at24)
579 return -ENOMEM;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200580
581 mutex_init(&at24->lock);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200582 at24->chip = chip;
583 at24->num_addresses = num_addresses;
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100584 at24->offset_adj = at24_get_offset_adj(chip.flags, chip.byte_len);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200585
Heiner Kallweit5c015252017-11-28 21:51:40 +0100586 at24->client[0].client = client;
Bartosz Golaszewskieef69392017-12-18 18:24:43 +0100587 at24->client[0].regmap = devm_regmap_init_i2c(client, &regmap_config);
Heiner Kallweit5c015252017-11-28 21:51:40 +0100588 if (IS_ERR(at24->client[0].regmap))
589 return PTR_ERR(at24->client[0].regmap);
590
Bartosz Golaszewski0b813652016-06-06 10:48:54 +0200591 if ((chip.flags & AT24_FLAG_SERIAL) && (chip.flags & AT24_FLAG_MAC)) {
592 dev_err(&client->dev,
593 "invalid device data - cannot have both AT24_FLAG_SERIAL & AT24_FLAG_MAC.");
594 return -EINVAL;
595 }
596
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200597 writable = !(chip.flags & AT24_FLAG_READONLY);
598 if (writable) {
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100599 at24->write_max = min_t(unsigned int,
600 chip.page_size, at24_io_limit);
Heiner Kallweita23727c2017-11-28 21:51:54 +0100601 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C) &&
602 at24->write_max > I2C_SMBUS_BLOCK_MAX)
603 at24->write_max = I2C_SMBUS_BLOCK_MAX;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200604 }
605
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200606 /* use dummy devices for multiple-address chips */
607 for (i = 1; i < num_addresses; i++) {
Heiner Kallweit5c015252017-11-28 21:51:40 +0100608 at24->client[i].client = i2c_new_dummy(client->adapter,
609 client->addr + i);
610 if (!at24->client[i].client) {
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200611 dev_err(&client->dev, "address 0x%02x unavailable\n",
612 client->addr + i);
613 err = -EADDRINUSE;
614 goto err_clients;
615 }
Heiner Kallweit5c015252017-11-28 21:51:40 +0100616 at24->client[i].regmap = devm_regmap_init_i2c(
Bartosz Golaszewskieef69392017-12-18 18:24:43 +0100617 at24->client[i].client,
618 &regmap_config);
Heiner Kallweit5c015252017-11-28 21:51:40 +0100619 if (IS_ERR(at24->client[i].regmap)) {
620 err = PTR_ERR(at24->client[i].regmap);
621 goto err_clients;
622 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200623 }
624
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200625 i2c_set_clientdata(client, at24);
626
Divagar Mohandass98e82012017-10-10 11:30:37 +0530627 /* enable runtime pm */
628 pm_runtime_set_active(&client->dev);
629 pm_runtime_enable(&client->dev);
630
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200631 /*
632 * Perform a one-byte test read to verify that the
633 * chip is functional.
634 */
635 err = at24_read(at24, 0, &test_byte, 1);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530636 pm_runtime_idle(&client->dev);
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200637 if (err) {
638 err = -ENODEV;
639 goto err_clients;
640 }
641
Andrew Lunn57d15552016-02-26 20:59:20 +0100642 at24->nvmem_config.name = dev_name(&client->dev);
643 at24->nvmem_config.dev = &client->dev;
644 at24->nvmem_config.read_only = !writable;
645 at24->nvmem_config.root_only = true;
646 at24->nvmem_config.owner = THIS_MODULE;
647 at24->nvmem_config.compat = true;
648 at24->nvmem_config.base_dev = &client->dev;
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100649 at24->nvmem_config.reg_read = at24_read;
650 at24->nvmem_config.reg_write = at24_write;
651 at24->nvmem_config.priv = at24;
David Lechner7f6d2ec2017-12-03 19:54:41 -0600652 at24->nvmem_config.stride = 1;
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100653 at24->nvmem_config.word_size = 1;
654 at24->nvmem_config.size = chip.byte_len;
Andrew Lunn57d15552016-02-26 20:59:20 +0100655
656 at24->nvmem = nvmem_register(&at24->nvmem_config);
657
658 if (IS_ERR(at24->nvmem)) {
659 err = PTR_ERR(at24->nvmem);
660 goto err_clients;
661 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200662
Andrew Lunn57d15552016-02-26 20:59:20 +0100663 dev_info(&client->dev, "%u byte %s EEPROM, %s, %u bytes/write\n",
664 chip.byte_len, client->name,
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100665 writable ? "writable" : "read-only", at24->write_max);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200666
Kevin Hilman7274ec82009-04-02 16:56:57 -0700667 /* export data to kernel code */
668 if (chip.setup)
Andrew Lunnbec3c112016-02-26 20:59:24 +0100669 chip.setup(at24->nvmem, chip.context);
Kevin Hilman7274ec82009-04-02 16:56:57 -0700670
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200671 return 0;
672
673err_clients:
674 for (i = 1; i < num_addresses; i++)
Heiner Kallweit5c015252017-11-28 21:51:40 +0100675 if (at24->client[i].client)
676 i2c_unregister_device(at24->client[i].client);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200677
Divagar Mohandass98e82012017-10-10 11:30:37 +0530678 pm_runtime_disable(&client->dev);
679
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200680 return err;
681}
682
Bill Pemberton486a5c22012-11-19 13:26:02 -0500683static int at24_remove(struct i2c_client *client)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200684{
685 struct at24_data *at24;
686 int i;
687
688 at24 = i2c_get_clientdata(client);
Andrew Lunn57d15552016-02-26 20:59:20 +0100689
690 nvmem_unregister(at24->nvmem);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200691
692 for (i = 1; i < at24->num_addresses; i++)
Heiner Kallweit5c015252017-11-28 21:51:40 +0100693 i2c_unregister_device(at24->client[i].client);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200694
Divagar Mohandass98e82012017-10-10 11:30:37 +0530695 pm_runtime_disable(&client->dev);
696 pm_runtime_set_suspended(&client->dev);
697
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200698 return 0;
699}
700
701/*-------------------------------------------------------------------------*/
702
703static struct i2c_driver at24_driver = {
704 .driver = {
705 .name = "at24",
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200706 .of_match_table = at24_of_match,
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300707 .acpi_match_table = ACPI_PTR(at24_acpi_ids),
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200708 },
709 .probe = at24_probe,
Bill Pemberton2d6bed92012-11-19 13:21:23 -0500710 .remove = at24_remove,
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200711 .id_table = at24_ids,
712};
713
714static int __init at24_init(void)
715{
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100716 if (!at24_io_limit) {
717 pr_err("at24: at24_io_limit must not be 0!\n");
Wolfram Sang45efe842010-11-17 13:00:49 +0100718 return -EINVAL;
719 }
720
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100721 at24_io_limit = rounddown_pow_of_two(at24_io_limit);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200722 return i2c_add_driver(&at24_driver);
723}
724module_init(at24_init);
725
726static void __exit at24_exit(void)
727{
728 i2c_del_driver(&at24_driver);
729}
730module_exit(at24_exit);
731
732MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
733MODULE_AUTHOR("David Brownell and Wolfram Sang");
734MODULE_LICENSE("GPL");