blob: e79833d62284b3b000281d8c436b274192df8292 [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_config nvmem_config;
79 struct nvmem_device *nvmem;
80
Bartosz Golaszewski6ce261e2017-12-19 11:28:54 +010081 struct gpio_desc *wp_gpio;
82
Wolfram Sang2b7a5052008-07-14 22:38:35 +020083 /*
84 * Some chips tie up multiple I2C addresses; dummy devices reserve
85 * them for us, and we'll use them with SMBus calls.
86 */
Heiner Kallweit5c015252017-11-28 21:51:40 +010087 struct at24_client client[];
Wolfram Sang2b7a5052008-07-14 22:38:35 +020088};
89
90/*
91 * This parameter is to help this driver avoid blocking other drivers out
92 * of I2C for potentially troublesome amounts of time. With a 100 kHz I2C
93 * clock, one 256 byte read takes about 1/43 second which is excessive;
94 * but the 1/170 second it takes at 400 kHz may be quite reasonable; and
95 * at 1 MHz (Fm+) a 1/430 second delay could easily be invisible.
96 *
97 * This value is forced to be a power of two so that writes align on pages.
98 */
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +010099static unsigned int at24_io_limit = 128;
100module_param_named(io_limit, at24_io_limit, uint, 0);
101MODULE_PARM_DESC(at24_io_limit, "Maximum bytes per I/O (default 128)");
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200102
103/*
104 * Specs often allow 5 msec for a page write, sometimes 20 msec;
105 * it's important to recover from write timeouts.
106 */
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100107static unsigned int at24_write_timeout = 25;
108module_param_named(write_timeout, at24_write_timeout, uint, 0);
109MODULE_PARM_DESC(at24_write_timeout, "Time (in ms) to try writes (default 25)");
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200110
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200111/*
112 * Both reads and writes fail if the previous write didn't complete yet. This
113 * macro loops a few times waiting at least long enough for one entire page
Bartosz Golaszewski24da3cc2016-07-17 20:40:06 +0200114 * write to work while making sure that at least one iteration is run before
115 * checking the break condition.
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200116 *
117 * It takes two parameters: a variable in which the future timeout in jiffies
118 * will be stored and a temporary variable holding the time of the last
119 * iteration of processing the request. Both should be unsigned integers
120 * holding at least 32 bits.
121 */
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100122#define at24_loop_until_timeout(tout, op_time) \
123 for (tout = jiffies + msecs_to_jiffies(at24_write_timeout), \
124 op_time = 0; \
Bartosz Golaszewski24da3cc2016-07-17 20:40:06 +0200125 op_time ? time_before(op_time, tout) : true; \
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200126 usleep_range(1000, 1500), op_time = jiffies)
127
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500128struct at24_chip_data {
129 /*
130 * these fields mirror their equivalents in
131 * struct at24_platform_data
132 */
133 u32 byte_len;
134 u8 flags;
135};
136
137#define AT24_CHIP_DATA(_name, _len, _flags) \
138 static const struct at24_chip_data _name = { \
139 .byte_len = _len, .flags = _flags, \
140 }
141
142/* needs 8 addresses as A0-A2 are ignored */
143AT24_CHIP_DATA(at24_data_24c00, 128 / 8, AT24_FLAG_TAKE8ADDR);
144/* old variants can't be handled with this generic entry! */
145AT24_CHIP_DATA(at24_data_24c01, 1024 / 8, 0);
146AT24_CHIP_DATA(at24_data_24cs01, 16,
147 AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
148AT24_CHIP_DATA(at24_data_24c02, 2048 / 8, 0);
149AT24_CHIP_DATA(at24_data_24cs02, 16,
150 AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
151AT24_CHIP_DATA(at24_data_24mac402, 48 / 8,
152 AT24_FLAG_MAC | AT24_FLAG_READONLY);
153AT24_CHIP_DATA(at24_data_24mac602, 64 / 8,
154 AT24_FLAG_MAC | AT24_FLAG_READONLY);
155/* spd is a 24c02 in memory DIMMs */
156AT24_CHIP_DATA(at24_data_spd, 2048 / 8,
157 AT24_FLAG_READONLY | AT24_FLAG_IRUGO);
158AT24_CHIP_DATA(at24_data_24c04, 4096 / 8, 0);
159AT24_CHIP_DATA(at24_data_24cs04, 16,
160 AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
161/* 24rf08 quirk is handled at i2c-core */
162AT24_CHIP_DATA(at24_data_24c08, 8192 / 8, 0);
163AT24_CHIP_DATA(at24_data_24cs08, 16,
164 AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
165AT24_CHIP_DATA(at24_data_24c16, 16384 / 8, 0);
166AT24_CHIP_DATA(at24_data_24cs16, 16,
167 AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
168AT24_CHIP_DATA(at24_data_24c32, 32768 / 8, AT24_FLAG_ADDR16);
169AT24_CHIP_DATA(at24_data_24cs32, 16,
170 AT24_FLAG_ADDR16 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
171AT24_CHIP_DATA(at24_data_24c64, 65536 / 8, AT24_FLAG_ADDR16);
172AT24_CHIP_DATA(at24_data_24cs64, 16,
173 AT24_FLAG_ADDR16 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY);
174AT24_CHIP_DATA(at24_data_24c128, 131072 / 8, AT24_FLAG_ADDR16);
175AT24_CHIP_DATA(at24_data_24c256, 262144 / 8, AT24_FLAG_ADDR16);
176AT24_CHIP_DATA(at24_data_24c512, 524288 / 8, AT24_FLAG_ADDR16);
177AT24_CHIP_DATA(at24_data_24c1024, 1048576 / 8, AT24_FLAG_ADDR16);
178/* identical to 24c08 ? */
179AT24_CHIP_DATA(at24_data_INT3499, 8192 / 8, 0);
180
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200181static const struct i2c_device_id at24_ids[] = {
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500182 { "24c00", (kernel_ulong_t)&at24_data_24c00 },
183 { "24c01", (kernel_ulong_t)&at24_data_24c01 },
184 { "24cs01", (kernel_ulong_t)&at24_data_24cs01 },
185 { "24c02", (kernel_ulong_t)&at24_data_24c02 },
186 { "24cs02", (kernel_ulong_t)&at24_data_24cs02 },
187 { "24mac402", (kernel_ulong_t)&at24_data_24mac402 },
188 { "24mac602", (kernel_ulong_t)&at24_data_24mac602 },
189 { "spd", (kernel_ulong_t)&at24_data_spd },
190 { "24c04", (kernel_ulong_t)&at24_data_24c04 },
191 { "24cs04", (kernel_ulong_t)&at24_data_24cs04 },
192 { "24c08", (kernel_ulong_t)&at24_data_24c08 },
193 { "24cs08", (kernel_ulong_t)&at24_data_24cs08 },
194 { "24c16", (kernel_ulong_t)&at24_data_24c16 },
195 { "24cs16", (kernel_ulong_t)&at24_data_24cs16 },
196 { "24c32", (kernel_ulong_t)&at24_data_24c32 },
197 { "24cs32", (kernel_ulong_t)&at24_data_24cs32 },
198 { "24c64", (kernel_ulong_t)&at24_data_24c64 },
199 { "24cs64", (kernel_ulong_t)&at24_data_24cs64 },
200 { "24c128", (kernel_ulong_t)&at24_data_24c128 },
201 { "24c256", (kernel_ulong_t)&at24_data_24c256 },
202 { "24c512", (kernel_ulong_t)&at24_data_24c512 },
203 { "24c1024", (kernel_ulong_t)&at24_data_24c1024 },
204 { "at24", 0 },
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200205 { /* END OF LIST */ }
206};
207MODULE_DEVICE_TABLE(i2c, at24_ids);
208
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200209static const struct of_device_id at24_of_match[] = {
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500210 { .compatible = "atmel,24c00", .data = &at24_data_24c00 },
211 { .compatible = "atmel,24c01", .data = &at24_data_24c01 },
212 { .compatible = "atmel,24c02", .data = &at24_data_24c02 },
213 { .compatible = "atmel,spd", .data = &at24_data_spd },
214 { .compatible = "atmel,24c04", .data = &at24_data_24c04 },
215 { .compatible = "atmel,24c08", .data = &at24_data_24c08 },
216 { .compatible = "atmel,24c16", .data = &at24_data_24c16 },
217 { .compatible = "atmel,24c32", .data = &at24_data_24c32 },
218 { .compatible = "atmel,24c64", .data = &at24_data_24c64 },
219 { .compatible = "atmel,24c128", .data = &at24_data_24c128 },
220 { .compatible = "atmel,24c256", .data = &at24_data_24c256 },
221 { .compatible = "atmel,24c512", .data = &at24_data_24c512 },
222 { .compatible = "atmel,24c1024", .data = &at24_data_24c1024 },
223 { /* END OF LIST */ },
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200224};
225MODULE_DEVICE_TABLE(of, at24_of_match);
226
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300227static const struct acpi_device_id at24_acpi_ids[] = {
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500228 { "INT3499", (kernel_ulong_t)&at24_data_INT3499 },
229 { /* END OF LIST */ }
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300230};
231MODULE_DEVICE_TABLE(acpi, at24_acpi_ids);
232
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200233/*-------------------------------------------------------------------------*/
234
235/*
236 * This routine supports chips which consume multiple I2C addresses. It
237 * computes the addressing information to be used for a given r/w request.
238 * Assumes that sanity checks for offset happened at sysfs-layer.
Bartosz Golaszewski9afd68662016-06-06 10:48:48 +0200239 *
240 * Slave address and byte offset derive from the offset. Always
241 * set the byte address; on a multi-master board, another master
242 * may have changed the chip's "current" address pointer.
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200243 */
Heiner Kallweit46049482017-11-28 21:51:42 +0100244static struct at24_client *at24_translate_offset(struct at24_data *at24,
245 unsigned int *offset)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200246{
Bartosz Golaszewskiaa4ce222017-12-13 11:56:23 +0100247 unsigned int i;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200248
249 if (at24->chip.flags & AT24_FLAG_ADDR16) {
250 i = *offset >> 16;
251 *offset &= 0xffff;
252 } else {
253 i = *offset >> 8;
254 *offset &= 0xff;
255 }
256
Heiner Kallweit46049482017-11-28 21:51:42 +0100257 return &at24->client[i];
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200258}
259
Sven Van Asbroecke32213f2017-12-08 11:28:30 -0500260static size_t at24_adjust_read_count(struct at24_data *at24,
261 unsigned int offset, size_t count)
262{
263 unsigned int bits;
264 size_t remainder;
265
266 /*
267 * In case of multi-address chips that don't rollover reads to
268 * the next slave address: truncate the count to the slave boundary,
269 * so that the read never straddles slaves.
270 */
271 if (at24->chip.flags & AT24_FLAG_NO_RDROL) {
272 bits = (at24->chip.flags & AT24_FLAG_ADDR16) ? 16 : 8;
273 remainder = BIT(bits) - offset;
274 if (count > remainder)
275 count = remainder;
276 }
277
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100278 if (count > at24_io_limit)
279 count = at24_io_limit;
Sven Van Asbroecke32213f2017-12-08 11:28:30 -0500280
281 return count;
282}
283
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100284static ssize_t at24_regmap_read(struct at24_data *at24, char *buf,
285 unsigned int offset, size_t count)
286{
287 unsigned long timeout, read_time;
288 struct at24_client *at24_client;
289 struct i2c_client *client;
290 struct regmap *regmap;
291 int ret;
292
293 at24_client = at24_translate_offset(at24, &offset);
294 regmap = at24_client->regmap;
295 client = at24_client->client;
Sven Van Asbroecke32213f2017-12-08 11:28:30 -0500296 count = at24_adjust_read_count(at24, offset, count);
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100297
298 /* adjust offset for mac and serial read ops */
299 offset += at24->offset_adj;
300
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100301 at24_loop_until_timeout(timeout, read_time) {
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100302 ret = regmap_bulk_read(regmap, offset, buf, count);
303 dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
304 count, offset, ret, jiffies);
305 if (!ret)
306 return count;
307 }
308
309 return -ETIMEDOUT;
310}
311
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200312/*
313 * Note that if the hardware write-protect pin is pulled high, the whole
314 * chip is normally write protected. But there are plenty of product
315 * variants here, including OTP fuses and partial chip protect.
316 *
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200317 * We only use page mode writes; the alternative is sloooow. These routines
318 * write at most one page.
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200319 */
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200320
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200321static size_t at24_adjust_write_count(struct at24_data *at24,
322 unsigned int offset, size_t count)
323{
Bartosz Golaszewskiaa4ce222017-12-13 11:56:23 +0100324 unsigned int next_page;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200325
326 /* write_max is at most a page */
327 if (count > at24->write_max)
328 count = at24->write_max;
329
330 /* Never roll over backwards, to the start of this page */
331 next_page = roundup(offset + 1, at24->chip.page_size);
332 if (offset + count > next_page)
333 count = next_page - offset;
334
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200335 return count;
336}
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200337
Heiner Kallweit8e5888e2017-11-28 21:51:45 +0100338static ssize_t at24_regmap_write(struct at24_data *at24, const char *buf,
339 unsigned int offset, size_t count)
340{
341 unsigned long timeout, write_time;
342 struct at24_client *at24_client;
343 struct i2c_client *client;
344 struct regmap *regmap;
345 int ret;
346
347 at24_client = at24_translate_offset(at24, &offset);
348 regmap = at24_client->regmap;
349 client = at24_client->client;
350 count = at24_adjust_write_count(at24, offset, count);
351
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100352 at24_loop_until_timeout(timeout, write_time) {
Heiner Kallweit8e5888e2017-11-28 21:51:45 +0100353 ret = regmap_bulk_write(regmap, offset, buf, count);
354 dev_dbg(&client->dev, "write %zu@%d --> %d (%ld)\n",
355 count, offset, ret, jiffies);
356 if (!ret)
357 return count;
358 }
359
360 return -ETIMEDOUT;
361}
362
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200363static int at24_read(void *priv, unsigned int off, void *val, size_t count)
364{
365 struct at24_data *at24 = priv;
Heiner Kallweit5c015252017-11-28 21:51:40 +0100366 struct device *dev = &at24->client[0].client->dev;
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200367 char *buf = val;
Divagar Mohandass98e82012017-10-10 11:30:37 +0530368 int ret;
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200369
370 if (unlikely(!count))
371 return count;
372
Heiner Kallweitd9bcd462017-11-24 07:47:50 +0100373 if (off + count > at24->chip.byte_len)
374 return -EINVAL;
375
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500376 ret = pm_runtime_get_sync(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530377 if (ret < 0) {
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500378 pm_runtime_put_noidle(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530379 return ret;
380 }
381
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200382 /*
383 * Read data from chip, protecting against concurrent updates
384 * from this host, but not from other I2C masters.
385 */
386 mutex_lock(&at24->lock);
387
388 while (count) {
389 int status;
390
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100391 status = at24_regmap_read(at24, buf, off, count);
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200392 if (status < 0) {
393 mutex_unlock(&at24->lock);
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500394 pm_runtime_put(dev);
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200395 return status;
396 }
397 buf += status;
398 off += status;
399 count -= status;
400 }
401
402 mutex_unlock(&at24->lock);
403
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500404 pm_runtime_put(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530405
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200406 return 0;
407}
408
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100409static int at24_write(void *priv, unsigned int off, void *val, size_t count)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200410{
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100411 struct at24_data *at24 = priv;
Heiner Kallweit5c015252017-11-28 21:51:40 +0100412 struct device *dev = &at24->client[0].client->dev;
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100413 char *buf = val;
Divagar Mohandass98e82012017-10-10 11:30:37 +0530414 int ret;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200415
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200416 if (unlikely(!count))
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100417 return -EINVAL;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200418
Heiner Kallweitd9bcd462017-11-24 07:47:50 +0100419 if (off + count > at24->chip.byte_len)
420 return -EINVAL;
421
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500422 ret = pm_runtime_get_sync(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530423 if (ret < 0) {
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500424 pm_runtime_put_noidle(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530425 return ret;
426 }
427
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200428 /*
429 * Write data to chip, protecting against concurrent updates
430 * from this host, but not from other I2C masters.
431 */
432 mutex_lock(&at24->lock);
Bartosz Golaszewski6ce261e2017-12-19 11:28:54 +0100433 gpiod_set_value_cansleep(at24->wp_gpio, 0);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200434
435 while (count) {
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100436 int status;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200437
Heiner Kallweit8e5888e2017-11-28 21:51:45 +0100438 status = at24_regmap_write(at24, buf, off, count);
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100439 if (status < 0) {
Bartosz Golaszewski6ce261e2017-12-19 11:28:54 +0100440 gpiod_set_value_cansleep(at24->wp_gpio, 1);
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100441 mutex_unlock(&at24->lock);
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500442 pm_runtime_put(dev);
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100443 return status;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200444 }
445 buf += status;
446 off += status;
447 count -= status;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200448 }
449
Bartosz Golaszewski6ce261e2017-12-19 11:28:54 +0100450 gpiod_set_value_cansleep(at24->wp_gpio, 1);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200451 mutex_unlock(&at24->lock);
452
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500453 pm_runtime_put(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530454
Andrew Lunn57d15552016-02-26 20:59:20 +0100455 return 0;
456}
457
Ben Gardnerdd905a62017-02-09 11:36:08 -0600458static void at24_get_pdata(struct device *dev, struct at24_platform_data *chip)
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100459{
Ben Gardnerdd905a62017-02-09 11:36:08 -0600460 int err;
461 u32 val;
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100462
Ben Gardnerdd905a62017-02-09 11:36:08 -0600463 if (device_property_present(dev, "read-only"))
464 chip->flags |= AT24_FLAG_READONLY;
Sven Van Asbroecke32213f2017-12-08 11:28:30 -0500465 if (device_property_present(dev, "no-read-rollover"))
466 chip->flags |= AT24_FLAG_NO_RDROL;
Ben Gardnerdd905a62017-02-09 11:36:08 -0600467
Divagar Mohandassdbc1ab92017-10-10 11:30:36 +0530468 err = device_property_read_u32(dev, "size", &val);
469 if (!err)
470 chip->byte_len = val;
471
Ben Gardnerdd905a62017-02-09 11:36:08 -0600472 err = device_property_read_u32(dev, "pagesize", &val);
473 if (!err) {
474 chip->page_size = val;
475 } else {
476 /*
477 * This is slow, but we can't know all eeproms, so we better
478 * play safe. Specifying custom eeprom-types via platform_data
479 * is recommended anyhow.
480 */
481 chip->page_size = 1;
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100482 }
483}
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100484
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100485static unsigned int at24_get_offset_adj(u8 flags, unsigned int byte_len)
486{
487 if (flags & AT24_FLAG_MAC) {
488 /* EUI-48 starts from 0x9a, EUI-64 from 0x98 */
489 return 0xa0 - byte_len;
490 } else if (flags & AT24_FLAG_SERIAL && flags & AT24_FLAG_ADDR16) {
491 /*
492 * For 16 bit address pointers, the word address must contain
493 * a '10' sequence in bits 11 and 10 regardless of the
494 * intended position of the address pointer.
495 */
496 return 0x0800;
497 } else if (flags & AT24_FLAG_SERIAL) {
498 /*
499 * Otherwise the word address must begin with a '10' sequence,
500 * regardless of the intended address.
501 */
502 return 0x0080;
503 } else {
504 return 0;
505 }
506}
507
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200508static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
509{
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500510 struct at24_platform_data chip = { 0 };
511 const struct at24_chip_data *cd = NULL;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200512 bool writable;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200513 struct at24_data *at24;
514 int err;
Bartosz Golaszewskiaa4ce222017-12-13 11:56:23 +0100515 unsigned int i, num_addresses;
Bartosz Golaszewskieef69392017-12-18 18:24:43 +0100516 struct regmap_config regmap_config = { };
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200517 u8 test_byte;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200518
519 if (client->dev.platform_data) {
520 chip = *(struct at24_platform_data *)client->dev.platform_data;
521 } else {
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200522 /*
523 * The I2C core allows OF nodes compatibles to match against the
524 * I2C device ID table as a fallback, so check not only if an OF
525 * node is present but also if it matches an OF device ID entry.
526 */
527 if (client->dev.of_node &&
528 of_match_device(at24_of_match, &client->dev)) {
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500529 cd = of_device_get_match_data(&client->dev);
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200530 } else if (id) {
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500531 cd = (void *)id->driver_data;
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300532 } else {
533 const struct acpi_device_id *aid;
534
535 aid = acpi_match_device(at24_acpi_ids, &client->dev);
536 if (aid)
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500537 cd = (void *)aid->driver_data;
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300538 }
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500539 if (!cd)
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700540 return -ENODEV;
541
Sven Van Asbroeckb680f4f2017-12-20 11:48:56 -0500542 chip.byte_len = cd->byte_len;
543 chip.flags = cd->flags;
Ben Gardnerdd905a62017-02-09 11:36:08 -0600544 at24_get_pdata(&client->dev, &chip);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200545 }
546
547 if (!is_power_of_2(chip.byte_len))
548 dev_warn(&client->dev,
549 "byte_len looks suspicious (no power of 2)!\n");
Wolfram Sang45efe842010-11-17 13:00:49 +0100550 if (!chip.page_size) {
551 dev_err(&client->dev, "page_size must not be 0!\n");
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700552 return -EINVAL;
Wolfram Sang45efe842010-11-17 13:00:49 +0100553 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200554 if (!is_power_of_2(chip.page_size))
555 dev_warn(&client->dev,
556 "page_size looks suspicious (no power of 2)!\n");
557
Heiner Kallweita23727c2017-11-28 21:51:54 +0100558 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C) &&
559 !i2c_check_functionality(client->adapter,
560 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK))
561 chip.page_size = 1;
Christian Gmeinera839ce62014-10-09 11:07:58 +0200562
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200563 if (chip.flags & AT24_FLAG_TAKE8ADDR)
564 num_addresses = 8;
565 else
566 num_addresses = DIV_ROUND_UP(chip.byte_len,
567 (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
568
Bartosz Golaszewskieef69392017-12-18 18:24:43 +0100569 regmap_config.val_bits = 8;
570 regmap_config.reg_bits = (chip.flags & AT24_FLAG_ADDR16) ? 16 : 8;
Heiner Kallweit5c015252017-11-28 21:51:40 +0100571
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700572 at24 = devm_kzalloc(&client->dev, sizeof(struct at24_data) +
Heiner Kallweit5c015252017-11-28 21:51:40 +0100573 num_addresses * sizeof(struct at24_client), GFP_KERNEL);
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700574 if (!at24)
575 return -ENOMEM;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200576
577 mutex_init(&at24->lock);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200578 at24->chip = chip;
579 at24->num_addresses = num_addresses;
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100580 at24->offset_adj = at24_get_offset_adj(chip.flags, chip.byte_len);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200581
Bartosz Golaszewski6ce261e2017-12-19 11:28:54 +0100582 at24->wp_gpio = devm_gpiod_get_optional(&client->dev,
583 "wp", GPIOD_OUT_HIGH);
584 if (IS_ERR(at24->wp_gpio))
585 return PTR_ERR(at24->wp_gpio);
586
Heiner Kallweit5c015252017-11-28 21:51:40 +0100587 at24->client[0].client = client;
Bartosz Golaszewskieef69392017-12-18 18:24:43 +0100588 at24->client[0].regmap = devm_regmap_init_i2c(client, &regmap_config);
Heiner Kallweit5c015252017-11-28 21:51:40 +0100589 if (IS_ERR(at24->client[0].regmap))
590 return PTR_ERR(at24->client[0].regmap);
591
Bartosz Golaszewski0b813652016-06-06 10:48:54 +0200592 if ((chip.flags & AT24_FLAG_SERIAL) && (chip.flags & AT24_FLAG_MAC)) {
593 dev_err(&client->dev,
594 "invalid device data - cannot have both AT24_FLAG_SERIAL & AT24_FLAG_MAC.");
595 return -EINVAL;
596 }
597
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200598 writable = !(chip.flags & AT24_FLAG_READONLY);
599 if (writable) {
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100600 at24->write_max = min_t(unsigned int,
601 chip.page_size, at24_io_limit);
Heiner Kallweita23727c2017-11-28 21:51:54 +0100602 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C) &&
603 at24->write_max > I2C_SMBUS_BLOCK_MAX)
604 at24->write_max = I2C_SMBUS_BLOCK_MAX;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200605 }
606
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200607 /* use dummy devices for multiple-address chips */
608 for (i = 1; i < num_addresses; i++) {
Heiner Kallweit5c015252017-11-28 21:51:40 +0100609 at24->client[i].client = i2c_new_dummy(client->adapter,
610 client->addr + i);
611 if (!at24->client[i].client) {
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200612 dev_err(&client->dev, "address 0x%02x unavailable\n",
613 client->addr + i);
614 err = -EADDRINUSE;
615 goto err_clients;
616 }
Heiner Kallweit5c015252017-11-28 21:51:40 +0100617 at24->client[i].regmap = devm_regmap_init_i2c(
Bartosz Golaszewskieef69392017-12-18 18:24:43 +0100618 at24->client[i].client,
619 &regmap_config);
Heiner Kallweit5c015252017-11-28 21:51:40 +0100620 if (IS_ERR(at24->client[i].regmap)) {
621 err = PTR_ERR(at24->client[i].regmap);
622 goto err_clients;
623 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200624 }
625
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200626 i2c_set_clientdata(client, at24);
627
Divagar Mohandass98e82012017-10-10 11:30:37 +0530628 /* enable runtime pm */
629 pm_runtime_set_active(&client->dev);
630 pm_runtime_enable(&client->dev);
631
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200632 /*
633 * Perform a one-byte test read to verify that the
634 * chip is functional.
635 */
636 err = at24_read(at24, 0, &test_byte, 1);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530637 pm_runtime_idle(&client->dev);
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200638 if (err) {
639 err = -ENODEV;
640 goto err_clients;
641 }
642
Andrew Lunn57d15552016-02-26 20:59:20 +0100643 at24->nvmem_config.name = dev_name(&client->dev);
644 at24->nvmem_config.dev = &client->dev;
645 at24->nvmem_config.read_only = !writable;
646 at24->nvmem_config.root_only = true;
647 at24->nvmem_config.owner = THIS_MODULE;
648 at24->nvmem_config.compat = true;
649 at24->nvmem_config.base_dev = &client->dev;
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100650 at24->nvmem_config.reg_read = at24_read;
651 at24->nvmem_config.reg_write = at24_write;
652 at24->nvmem_config.priv = at24;
David Lechner7f6d2ec2017-12-03 19:54:41 -0600653 at24->nvmem_config.stride = 1;
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100654 at24->nvmem_config.word_size = 1;
655 at24->nvmem_config.size = chip.byte_len;
Andrew Lunn57d15552016-02-26 20:59:20 +0100656
657 at24->nvmem = nvmem_register(&at24->nvmem_config);
658
659 if (IS_ERR(at24->nvmem)) {
660 err = PTR_ERR(at24->nvmem);
661 goto err_clients;
662 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200663
Andrew Lunn57d15552016-02-26 20:59:20 +0100664 dev_info(&client->dev, "%u byte %s EEPROM, %s, %u bytes/write\n",
665 chip.byte_len, client->name,
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100666 writable ? "writable" : "read-only", at24->write_max);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200667
Kevin Hilman7274ec82009-04-02 16:56:57 -0700668 /* export data to kernel code */
669 if (chip.setup)
Andrew Lunnbec3c112016-02-26 20:59:24 +0100670 chip.setup(at24->nvmem, chip.context);
Kevin Hilman7274ec82009-04-02 16:56:57 -0700671
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200672 return 0;
673
674err_clients:
675 for (i = 1; i < num_addresses; i++)
Heiner Kallweit5c015252017-11-28 21:51:40 +0100676 if (at24->client[i].client)
677 i2c_unregister_device(at24->client[i].client);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200678
Divagar Mohandass98e82012017-10-10 11:30:37 +0530679 pm_runtime_disable(&client->dev);
680
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200681 return err;
682}
683
Bill Pemberton486a5c22012-11-19 13:26:02 -0500684static int at24_remove(struct i2c_client *client)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200685{
686 struct at24_data *at24;
687 int i;
688
689 at24 = i2c_get_clientdata(client);
Andrew Lunn57d15552016-02-26 20:59:20 +0100690
691 nvmem_unregister(at24->nvmem);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200692
693 for (i = 1; i < at24->num_addresses; i++)
Heiner Kallweit5c015252017-11-28 21:51:40 +0100694 i2c_unregister_device(at24->client[i].client);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200695
Divagar Mohandass98e82012017-10-10 11:30:37 +0530696 pm_runtime_disable(&client->dev);
697 pm_runtime_set_suspended(&client->dev);
698
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200699 return 0;
700}
701
702/*-------------------------------------------------------------------------*/
703
704static struct i2c_driver at24_driver = {
705 .driver = {
706 .name = "at24",
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200707 .of_match_table = at24_of_match,
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300708 .acpi_match_table = ACPI_PTR(at24_acpi_ids),
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200709 },
710 .probe = at24_probe,
Bill Pemberton2d6bed92012-11-19 13:21:23 -0500711 .remove = at24_remove,
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200712 .id_table = at24_ids,
713};
714
715static int __init at24_init(void)
716{
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100717 if (!at24_io_limit) {
718 pr_err("at24: at24_io_limit must not be 0!\n");
Wolfram Sang45efe842010-11-17 13:00:49 +0100719 return -EINVAL;
720 }
721
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100722 at24_io_limit = rounddown_pow_of_two(at24_io_limit);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200723 return i2c_add_driver(&at24_driver);
724}
725module_init(at24_init);
726
727static void __exit at24_exit(void)
728{
729 i2c_del_driver(&at24_driver);
730}
731module_exit(at24_exit);
732
733MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
734MODULE_AUTHOR("David Brownell and Wolfram Sang");
735MODULE_LICENSE("GPL");