blob: b44a3d2b2b20cd56aef6d25f5494217f9325a204 [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
108#define AT24_SIZE_BYTELEN 5
109#define AT24_SIZE_FLAGS 8
110
111#define AT24_BITMASK(x) (BIT(x) - 1)
112
113/* create non-zero magic value for given eeprom parameters */
Bartosz Golaszewskiaa4ce222017-12-13 11:56:23 +0100114#define AT24_DEVICE_MAGIC(_len, _flags) \
115 ((1 << AT24_SIZE_FLAGS | (_flags)) \
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200116 << AT24_SIZE_BYTELEN | ilog2(_len))
117
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200118/*
119 * Both reads and writes fail if the previous write didn't complete yet. This
120 * macro loops a few times waiting at least long enough for one entire page
Bartosz Golaszewski24da3cc2016-07-17 20:40:06 +0200121 * write to work while making sure that at least one iteration is run before
122 * checking the break condition.
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200123 *
124 * It takes two parameters: a variable in which the future timeout in jiffies
125 * will be stored and a temporary variable holding the time of the last
126 * iteration of processing the request. Both should be unsigned integers
127 * holding at least 32 bits.
128 */
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100129#define at24_loop_until_timeout(tout, op_time) \
130 for (tout = jiffies + msecs_to_jiffies(at24_write_timeout), \
131 op_time = 0; \
Bartosz Golaszewski24da3cc2016-07-17 20:40:06 +0200132 op_time ? time_before(op_time, tout) : true; \
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200133 usleep_range(1000, 1500), op_time = jiffies)
134
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200135static const struct i2c_device_id at24_ids[] = {
136 /* needs 8 addresses as A0-A2 are ignored */
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200137 { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) },
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200138 /* old variants can't be handled with this generic entry! */
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200139 { "24c01", AT24_DEVICE_MAGIC(1024 / 8, 0) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200140 { "24cs01", AT24_DEVICE_MAGIC(16,
141 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200142 { "24c02", AT24_DEVICE_MAGIC(2048 / 8, 0) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200143 { "24cs02", AT24_DEVICE_MAGIC(16,
144 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
Bartosz Golaszewski0b813652016-06-06 10:48:54 +0200145 { "24mac402", AT24_DEVICE_MAGIC(48 / 8,
146 AT24_FLAG_MAC | AT24_FLAG_READONLY) },
147 { "24mac602", AT24_DEVICE_MAGIC(64 / 8,
148 AT24_FLAG_MAC | AT24_FLAG_READONLY) },
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200149 /* spd is a 24c02 in memory DIMMs */
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200150 { "spd", AT24_DEVICE_MAGIC(2048 / 8,
151 AT24_FLAG_READONLY | AT24_FLAG_IRUGO) },
152 { "24c04", AT24_DEVICE_MAGIC(4096 / 8, 0) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200153 { "24cs04", AT24_DEVICE_MAGIC(16,
154 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200155 /* 24rf08 quirk is handled at i2c-core */
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200156 { "24c08", AT24_DEVICE_MAGIC(8192 / 8, 0) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200157 { "24cs08", AT24_DEVICE_MAGIC(16,
158 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200159 { "24c16", AT24_DEVICE_MAGIC(16384 / 8, 0) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200160 { "24cs16", AT24_DEVICE_MAGIC(16,
161 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200162 { "24c32", AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200163 { "24cs32", AT24_DEVICE_MAGIC(16,
164 AT24_FLAG_ADDR16 |
165 AT24_FLAG_SERIAL |
166 AT24_FLAG_READONLY) },
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200167 { "24c64", AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200168 { "24cs64", AT24_DEVICE_MAGIC(16,
169 AT24_FLAG_ADDR16 |
170 AT24_FLAG_SERIAL |
171 AT24_FLAG_READONLY) },
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200172 { "24c128", AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16) },
173 { "24c256", AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16) },
174 { "24c512", AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16) },
175 { "24c1024", AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16) },
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200176 { "at24", 0 },
177 { /* END OF LIST */ }
178};
179MODULE_DEVICE_TABLE(i2c, at24_ids);
180
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200181static const struct of_device_id at24_of_match[] = {
182 {
183 .compatible = "atmel,24c00",
184 .data = (void *)AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR)
185 },
186 {
187 .compatible = "atmel,24c01",
188 .data = (void *)AT24_DEVICE_MAGIC(1024 / 8, 0)
189 },
190 {
191 .compatible = "atmel,24c02",
192 .data = (void *)AT24_DEVICE_MAGIC(2048 / 8, 0)
193 },
194 {
195 .compatible = "atmel,spd",
196 .data = (void *)AT24_DEVICE_MAGIC(2048 / 8,
197 AT24_FLAG_READONLY | AT24_FLAG_IRUGO)
198 },
199 {
200 .compatible = "atmel,24c04",
201 .data = (void *)AT24_DEVICE_MAGIC(4096 / 8, 0)
202 },
203 {
204 .compatible = "atmel,24c08",
205 .data = (void *)AT24_DEVICE_MAGIC(8192 / 8, 0)
206 },
207 {
208 .compatible = "atmel,24c16",
209 .data = (void *)AT24_DEVICE_MAGIC(16384 / 8, 0)
210 },
211 {
212 .compatible = "atmel,24c32",
213 .data = (void *)AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16)
214 },
215 {
216 .compatible = "atmel,24c64",
217 .data = (void *)AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16)
218 },
219 {
220 .compatible = "atmel,24c128",
221 .data = (void *)AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16)
222 },
223 {
224 .compatible = "atmel,24c256",
225 .data = (void *)AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16)
226 },
227 {
228 .compatible = "atmel,24c512",
229 .data = (void *)AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16)
230 },
231 {
232 .compatible = "atmel,24c1024",
233 .data = (void *)AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16)
234 },
235 { },
236};
237MODULE_DEVICE_TABLE(of, at24_of_match);
238
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300239static const struct acpi_device_id at24_acpi_ids[] = {
240 { "INT3499", AT24_DEVICE_MAGIC(8192 / 8, 0) },
241 { }
242};
243MODULE_DEVICE_TABLE(acpi, at24_acpi_ids);
244
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200245/*-------------------------------------------------------------------------*/
246
247/*
248 * This routine supports chips which consume multiple I2C addresses. It
249 * computes the addressing information to be used for a given r/w request.
250 * Assumes that sanity checks for offset happened at sysfs-layer.
Bartosz Golaszewski9afd68662016-06-06 10:48:48 +0200251 *
252 * Slave address and byte offset derive from the offset. Always
253 * set the byte address; on a multi-master board, another master
254 * may have changed the chip's "current" address pointer.
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200255 */
Heiner Kallweit46049482017-11-28 21:51:42 +0100256static struct at24_client *at24_translate_offset(struct at24_data *at24,
257 unsigned int *offset)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200258{
Bartosz Golaszewskiaa4ce222017-12-13 11:56:23 +0100259 unsigned int i;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200260
261 if (at24->chip.flags & AT24_FLAG_ADDR16) {
262 i = *offset >> 16;
263 *offset &= 0xffff;
264 } else {
265 i = *offset >> 8;
266 *offset &= 0xff;
267 }
268
Heiner Kallweit46049482017-11-28 21:51:42 +0100269 return &at24->client[i];
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200270}
271
Sven Van Asbroecke32213f2017-12-08 11:28:30 -0500272static size_t at24_adjust_read_count(struct at24_data *at24,
273 unsigned int offset, size_t count)
274{
275 unsigned int bits;
276 size_t remainder;
277
278 /*
279 * In case of multi-address chips that don't rollover reads to
280 * the next slave address: truncate the count to the slave boundary,
281 * so that the read never straddles slaves.
282 */
283 if (at24->chip.flags & AT24_FLAG_NO_RDROL) {
284 bits = (at24->chip.flags & AT24_FLAG_ADDR16) ? 16 : 8;
285 remainder = BIT(bits) - offset;
286 if (count > remainder)
287 count = remainder;
288 }
289
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100290 if (count > at24_io_limit)
291 count = at24_io_limit;
Sven Van Asbroecke32213f2017-12-08 11:28:30 -0500292
293 return count;
294}
295
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100296static ssize_t at24_regmap_read(struct at24_data *at24, char *buf,
297 unsigned int offset, size_t count)
298{
299 unsigned long timeout, read_time;
300 struct at24_client *at24_client;
301 struct i2c_client *client;
302 struct regmap *regmap;
303 int ret;
304
305 at24_client = at24_translate_offset(at24, &offset);
306 regmap = at24_client->regmap;
307 client = at24_client->client;
Sven Van Asbroecke32213f2017-12-08 11:28:30 -0500308 count = at24_adjust_read_count(at24, offset, count);
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100309
310 /* adjust offset for mac and serial read ops */
311 offset += at24->offset_adj;
312
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100313 at24_loop_until_timeout(timeout, read_time) {
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100314 ret = regmap_bulk_read(regmap, offset, buf, count);
315 dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
316 count, offset, ret, jiffies);
317 if (!ret)
318 return count;
319 }
320
321 return -ETIMEDOUT;
322}
323
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200324/*
325 * Note that if the hardware write-protect pin is pulled high, the whole
326 * chip is normally write protected. But there are plenty of product
327 * variants here, including OTP fuses and partial chip protect.
328 *
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200329 * We only use page mode writes; the alternative is sloooow. These routines
330 * write at most one page.
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200331 */
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200332
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200333static size_t at24_adjust_write_count(struct at24_data *at24,
334 unsigned int offset, size_t count)
335{
Bartosz Golaszewskiaa4ce222017-12-13 11:56:23 +0100336 unsigned int next_page;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200337
338 /* write_max is at most a page */
339 if (count > at24->write_max)
340 count = at24->write_max;
341
342 /* Never roll over backwards, to the start of this page */
343 next_page = roundup(offset + 1, at24->chip.page_size);
344 if (offset + count > next_page)
345 count = next_page - offset;
346
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200347 return count;
348}
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200349
Heiner Kallweit8e5888e2017-11-28 21:51:45 +0100350static ssize_t at24_regmap_write(struct at24_data *at24, const char *buf,
351 unsigned int offset, size_t count)
352{
353 unsigned long timeout, write_time;
354 struct at24_client *at24_client;
355 struct i2c_client *client;
356 struct regmap *regmap;
357 int ret;
358
359 at24_client = at24_translate_offset(at24, &offset);
360 regmap = at24_client->regmap;
361 client = at24_client->client;
362 count = at24_adjust_write_count(at24, offset, count);
363
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100364 at24_loop_until_timeout(timeout, write_time) {
Heiner Kallweit8e5888e2017-11-28 21:51:45 +0100365 ret = regmap_bulk_write(regmap, offset, buf, count);
366 dev_dbg(&client->dev, "write %zu@%d --> %d (%ld)\n",
367 count, offset, ret, jiffies);
368 if (!ret)
369 return count;
370 }
371
372 return -ETIMEDOUT;
373}
374
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200375static int at24_read(void *priv, unsigned int off, void *val, size_t count)
376{
377 struct at24_data *at24 = priv;
Heiner Kallweit5c015252017-11-28 21:51:40 +0100378 struct device *dev = &at24->client[0].client->dev;
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200379 char *buf = val;
Divagar Mohandass98e82012017-10-10 11:30:37 +0530380 int ret;
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200381
382 if (unlikely(!count))
383 return count;
384
Heiner Kallweitd9bcd462017-11-24 07:47:50 +0100385 if (off + count > at24->chip.byte_len)
386 return -EINVAL;
387
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500388 ret = pm_runtime_get_sync(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530389 if (ret < 0) {
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500390 pm_runtime_put_noidle(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530391 return ret;
392 }
393
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200394 /*
395 * Read data from chip, protecting against concurrent updates
396 * from this host, but not from other I2C masters.
397 */
398 mutex_lock(&at24->lock);
399
400 while (count) {
401 int status;
402
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100403 status = at24_regmap_read(at24, buf, off, count);
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200404 if (status < 0) {
405 mutex_unlock(&at24->lock);
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500406 pm_runtime_put(dev);
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200407 return status;
408 }
409 buf += status;
410 off += status;
411 count -= status;
412 }
413
414 mutex_unlock(&at24->lock);
415
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500416 pm_runtime_put(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530417
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200418 return 0;
419}
420
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100421static int at24_write(void *priv, unsigned int off, void *val, size_t count)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200422{
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100423 struct at24_data *at24 = priv;
Heiner Kallweit5c015252017-11-28 21:51:40 +0100424 struct device *dev = &at24->client[0].client->dev;
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100425 char *buf = val;
Divagar Mohandass98e82012017-10-10 11:30:37 +0530426 int ret;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200427
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200428 if (unlikely(!count))
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100429 return -EINVAL;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200430
Heiner Kallweitd9bcd462017-11-24 07:47:50 +0100431 if (off + count > at24->chip.byte_len)
432 return -EINVAL;
433
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500434 ret = pm_runtime_get_sync(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530435 if (ret < 0) {
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500436 pm_runtime_put_noidle(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530437 return ret;
438 }
439
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200440 /*
441 * Write data to chip, protecting against concurrent updates
442 * from this host, but not from other I2C masters.
443 */
444 mutex_lock(&at24->lock);
445
446 while (count) {
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100447 int status;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200448
Heiner Kallweit8e5888e2017-11-28 21:51:45 +0100449 status = at24_regmap_write(at24, buf, off, count);
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100450 if (status < 0) {
451 mutex_unlock(&at24->lock);
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500452 pm_runtime_put(dev);
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100453 return status;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200454 }
455 buf += status;
456 off += status;
457 count -= status;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200458 }
459
460 mutex_unlock(&at24->lock);
461
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500462 pm_runtime_put(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530463
Andrew Lunn57d15552016-02-26 20:59:20 +0100464 return 0;
465}
466
Ben Gardnerdd905a62017-02-09 11:36:08 -0600467static void at24_get_pdata(struct device *dev, 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{
519 struct at24_platform_data chip;
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300520 kernel_ulong_t magic = 0;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200521 bool writable;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200522 struct at24_data *at24;
523 int err;
Bartosz Golaszewskiaa4ce222017-12-13 11:56:23 +0100524 unsigned int i, num_addresses;
Bartosz Golaszewskieef69392017-12-18 18:24:43 +0100525 struct regmap_config regmap_config = { };
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200526 u8 test_byte;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200527
528 if (client->dev.platform_data) {
529 chip = *(struct at24_platform_data *)client->dev.platform_data;
530 } else {
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200531 /*
532 * The I2C core allows OF nodes compatibles to match against the
533 * I2C device ID table as a fallback, so check not only if an OF
534 * node is present but also if it matches an OF device ID entry.
535 */
536 if (client->dev.of_node &&
537 of_match_device(at24_of_match, &client->dev)) {
538 magic = (kernel_ulong_t)
539 of_device_get_match_data(&client->dev);
540 } else if (id) {
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300541 magic = id->driver_data;
542 } else {
543 const struct acpi_device_id *aid;
544
545 aid = acpi_match_device(at24_acpi_ids, &client->dev);
546 if (aid)
547 magic = aid->driver_data;
548 }
549 if (!magic)
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700550 return -ENODEV;
551
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200552 chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN));
553 magic >>= AT24_SIZE_BYTELEN;
554 chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS);
Kevin Hilman7274ec82009-04-02 16:56:57 -0700555
Ben Gardnerdd905a62017-02-09 11:36:08 -0600556 at24_get_pdata(&client->dev, &chip);
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100557
Kevin Hilman7274ec82009-04-02 16:56:57 -0700558 chip.setup = NULL;
559 chip.context = NULL;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200560 }
561
562 if (!is_power_of_2(chip.byte_len))
563 dev_warn(&client->dev,
564 "byte_len looks suspicious (no power of 2)!\n");
Wolfram Sang45efe842010-11-17 13:00:49 +0100565 if (!chip.page_size) {
566 dev_err(&client->dev, "page_size must not be 0!\n");
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700567 return -EINVAL;
Wolfram Sang45efe842010-11-17 13:00:49 +0100568 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200569 if (!is_power_of_2(chip.page_size))
570 dev_warn(&client->dev,
571 "page_size looks suspicious (no power of 2)!\n");
572
Bartosz Golaszewski5478e472017-11-27 22:06:13 +0100573 /*
574 * REVISIT: the size of the EUI-48 byte array is 6 in at24mac402, while
575 * the call to ilog2() in AT24_DEVICE_MAGIC() rounds it down to 4.
576 *
577 * Eventually we'll get rid of the magic values altoghether in favor of
578 * real structs, but for now just manually set the right size.
579 */
580 if (chip.flags & AT24_FLAG_MAC && chip.byte_len == 4)
581 chip.byte_len = 6;
582
Heiner Kallweita23727c2017-11-28 21:51:54 +0100583 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C) &&
584 !i2c_check_functionality(client->adapter,
585 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK))
586 chip.page_size = 1;
Christian Gmeinera839ce62014-10-09 11:07:58 +0200587
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200588 if (chip.flags & AT24_FLAG_TAKE8ADDR)
589 num_addresses = 8;
590 else
591 num_addresses = DIV_ROUND_UP(chip.byte_len,
592 (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
593
Bartosz Golaszewskieef69392017-12-18 18:24:43 +0100594 regmap_config.val_bits = 8;
595 regmap_config.reg_bits = (chip.flags & AT24_FLAG_ADDR16) ? 16 : 8;
Heiner Kallweit5c015252017-11-28 21:51:40 +0100596
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700597 at24 = devm_kzalloc(&client->dev, sizeof(struct at24_data) +
Heiner Kallweit5c015252017-11-28 21:51:40 +0100598 num_addresses * sizeof(struct at24_client), GFP_KERNEL);
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700599 if (!at24)
600 return -ENOMEM;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200601
602 mutex_init(&at24->lock);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200603 at24->chip = chip;
604 at24->num_addresses = num_addresses;
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100605 at24->offset_adj = at24_get_offset_adj(chip.flags, chip.byte_len);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200606
Heiner Kallweit5c015252017-11-28 21:51:40 +0100607 at24->client[0].client = client;
Bartosz Golaszewskieef69392017-12-18 18:24:43 +0100608 at24->client[0].regmap = devm_regmap_init_i2c(client, &regmap_config);
Heiner Kallweit5c015252017-11-28 21:51:40 +0100609 if (IS_ERR(at24->client[0].regmap))
610 return PTR_ERR(at24->client[0].regmap);
611
Bartosz Golaszewski0b813652016-06-06 10:48:54 +0200612 if ((chip.flags & AT24_FLAG_SERIAL) && (chip.flags & AT24_FLAG_MAC)) {
613 dev_err(&client->dev,
614 "invalid device data - cannot have both AT24_FLAG_SERIAL & AT24_FLAG_MAC.");
615 return -EINVAL;
616 }
617
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200618 writable = !(chip.flags & AT24_FLAG_READONLY);
619 if (writable) {
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100620 at24->write_max = min_t(unsigned int,
621 chip.page_size, at24_io_limit);
Heiner Kallweita23727c2017-11-28 21:51:54 +0100622 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C) &&
623 at24->write_max > I2C_SMBUS_BLOCK_MAX)
624 at24->write_max = I2C_SMBUS_BLOCK_MAX;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200625 }
626
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200627 /* use dummy devices for multiple-address chips */
628 for (i = 1; i < num_addresses; i++) {
Heiner Kallweit5c015252017-11-28 21:51:40 +0100629 at24->client[i].client = i2c_new_dummy(client->adapter,
630 client->addr + i);
631 if (!at24->client[i].client) {
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200632 dev_err(&client->dev, "address 0x%02x unavailable\n",
633 client->addr + i);
634 err = -EADDRINUSE;
635 goto err_clients;
636 }
Heiner Kallweit5c015252017-11-28 21:51:40 +0100637 at24->client[i].regmap = devm_regmap_init_i2c(
Bartosz Golaszewskieef69392017-12-18 18:24:43 +0100638 at24->client[i].client,
639 &regmap_config);
Heiner Kallweit5c015252017-11-28 21:51:40 +0100640 if (IS_ERR(at24->client[i].regmap)) {
641 err = PTR_ERR(at24->client[i].regmap);
642 goto err_clients;
643 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200644 }
645
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200646 i2c_set_clientdata(client, at24);
647
Divagar Mohandass98e82012017-10-10 11:30:37 +0530648 /* enable runtime pm */
649 pm_runtime_set_active(&client->dev);
650 pm_runtime_enable(&client->dev);
651
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200652 /*
653 * Perform a one-byte test read to verify that the
654 * chip is functional.
655 */
656 err = at24_read(at24, 0, &test_byte, 1);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530657 pm_runtime_idle(&client->dev);
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200658 if (err) {
659 err = -ENODEV;
660 goto err_clients;
661 }
662
Andrew Lunn57d15552016-02-26 20:59:20 +0100663 at24->nvmem_config.name = dev_name(&client->dev);
664 at24->nvmem_config.dev = &client->dev;
665 at24->nvmem_config.read_only = !writable;
666 at24->nvmem_config.root_only = true;
667 at24->nvmem_config.owner = THIS_MODULE;
668 at24->nvmem_config.compat = true;
669 at24->nvmem_config.base_dev = &client->dev;
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100670 at24->nvmem_config.reg_read = at24_read;
671 at24->nvmem_config.reg_write = at24_write;
672 at24->nvmem_config.priv = at24;
David Lechner7f6d2ec2017-12-03 19:54:41 -0600673 at24->nvmem_config.stride = 1;
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100674 at24->nvmem_config.word_size = 1;
675 at24->nvmem_config.size = chip.byte_len;
Andrew Lunn57d15552016-02-26 20:59:20 +0100676
677 at24->nvmem = nvmem_register(&at24->nvmem_config);
678
679 if (IS_ERR(at24->nvmem)) {
680 err = PTR_ERR(at24->nvmem);
681 goto err_clients;
682 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200683
Andrew Lunn57d15552016-02-26 20:59:20 +0100684 dev_info(&client->dev, "%u byte %s EEPROM, %s, %u bytes/write\n",
685 chip.byte_len, client->name,
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100686 writable ? "writable" : "read-only", at24->write_max);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200687
Kevin Hilman7274ec82009-04-02 16:56:57 -0700688 /* export data to kernel code */
689 if (chip.setup)
Andrew Lunnbec3c112016-02-26 20:59:24 +0100690 chip.setup(at24->nvmem, chip.context);
Kevin Hilman7274ec82009-04-02 16:56:57 -0700691
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200692 return 0;
693
694err_clients:
695 for (i = 1; i < num_addresses; i++)
Heiner Kallweit5c015252017-11-28 21:51:40 +0100696 if (at24->client[i].client)
697 i2c_unregister_device(at24->client[i].client);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200698
Divagar Mohandass98e82012017-10-10 11:30:37 +0530699 pm_runtime_disable(&client->dev);
700
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200701 return err;
702}
703
Bill Pemberton486a5c22012-11-19 13:26:02 -0500704static int at24_remove(struct i2c_client *client)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200705{
706 struct at24_data *at24;
707 int i;
708
709 at24 = i2c_get_clientdata(client);
Andrew Lunn57d15552016-02-26 20:59:20 +0100710
711 nvmem_unregister(at24->nvmem);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200712
713 for (i = 1; i < at24->num_addresses; i++)
Heiner Kallweit5c015252017-11-28 21:51:40 +0100714 i2c_unregister_device(at24->client[i].client);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200715
Divagar Mohandass98e82012017-10-10 11:30:37 +0530716 pm_runtime_disable(&client->dev);
717 pm_runtime_set_suspended(&client->dev);
718
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200719 return 0;
720}
721
722/*-------------------------------------------------------------------------*/
723
724static struct i2c_driver at24_driver = {
725 .driver = {
726 .name = "at24",
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200727 .of_match_table = at24_of_match,
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300728 .acpi_match_table = ACPI_PTR(at24_acpi_ids),
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200729 },
730 .probe = at24_probe,
Bill Pemberton2d6bed92012-11-19 13:21:23 -0500731 .remove = at24_remove,
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200732 .id_table = at24_ids,
733};
734
735static int __init at24_init(void)
736{
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100737 if (!at24_io_limit) {
738 pr_err("at24: at24_io_limit must not be 0!\n");
Wolfram Sang45efe842010-11-17 13:00:49 +0100739 return -EINVAL;
740 }
741
Bartosz Golaszewskiec3c2d52017-12-18 18:16:46 +0100742 at24_io_limit = rounddown_pow_of_two(at24_io_limit);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200743 return i2c_add_driver(&at24_driver);
744}
745module_init(at24_init);
746
747static void __exit at24_exit(void)
748{
749 i2c_del_driver(&at24_driver);
750}
751module_exit(at24_exit);
752
753MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
754MODULE_AUTHOR("David Brownell and Wolfram Sang");
755MODULE_LICENSE("GPL");