blob: 3fd26d7cb50e79808282f11ee76e77d9760fd7d3 [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
Wolfram Sang2b7a5052008-07-14 22:38:35 +020073 unsigned write_max;
74 unsigned 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 */
96static unsigned io_limit = 128;
97module_param(io_limit, uint, 0);
98MODULE_PARM_DESC(io_limit, "Maximum bytes per I/O (default 128)");
99
100/*
101 * Specs often allow 5 msec for a page write, sometimes 20 msec;
102 * it's important to recover from write timeouts.
103 */
104static unsigned write_timeout = 25;
105module_param(write_timeout, uint, 0);
106MODULE_PARM_DESC(write_timeout, "Time (in ms) to try writes (default 25)");
107
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 */
114#define AT24_DEVICE_MAGIC(_len, _flags) \
115 ((1 << AT24_SIZE_FLAGS | (_flags)) \
116 << 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 */
129#define loop_until_timeout(tout, op_time) \
Bartosz Golaszewski24da3cc2016-07-17 20:40:06 +0200130 for (tout = jiffies + msecs_to_jiffies(write_timeout), op_time = 0; \
131 op_time ? time_before(op_time, tout) : true; \
Bartosz Golaszewski9344a812016-06-06 10:48:47 +0200132 usleep_range(1000, 1500), op_time = jiffies)
133
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200134static const struct i2c_device_id at24_ids[] = {
135 /* needs 8 addresses as A0-A2 are ignored */
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200136 { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) },
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200137 /* old variants can't be handled with this generic entry! */
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200138 { "24c01", AT24_DEVICE_MAGIC(1024 / 8, 0) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200139 { "24cs01", AT24_DEVICE_MAGIC(16,
140 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200141 { "24c02", AT24_DEVICE_MAGIC(2048 / 8, 0) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200142 { "24cs02", AT24_DEVICE_MAGIC(16,
143 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
Bartosz Golaszewski0b813652016-06-06 10:48:54 +0200144 { "24mac402", AT24_DEVICE_MAGIC(48 / 8,
145 AT24_FLAG_MAC | AT24_FLAG_READONLY) },
146 { "24mac602", AT24_DEVICE_MAGIC(64 / 8,
147 AT24_FLAG_MAC | AT24_FLAG_READONLY) },
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200148 /* spd is a 24c02 in memory DIMMs */
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200149 { "spd", AT24_DEVICE_MAGIC(2048 / 8,
150 AT24_FLAG_READONLY | AT24_FLAG_IRUGO) },
151 { "24c04", AT24_DEVICE_MAGIC(4096 / 8, 0) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200152 { "24cs04", AT24_DEVICE_MAGIC(16,
153 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200154 /* 24rf08 quirk is handled at i2c-core */
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200155 { "24c08", AT24_DEVICE_MAGIC(8192 / 8, 0) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200156 { "24cs08", AT24_DEVICE_MAGIC(16,
157 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200158 { "24c16", AT24_DEVICE_MAGIC(16384 / 8, 0) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200159 { "24cs16", AT24_DEVICE_MAGIC(16,
160 AT24_FLAG_SERIAL | AT24_FLAG_READONLY) },
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200161 { "24c32", AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200162 { "24cs32", AT24_DEVICE_MAGIC(16,
163 AT24_FLAG_ADDR16 |
164 AT24_FLAG_SERIAL |
165 AT24_FLAG_READONLY) },
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200166 { "24c64", AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) },
Bartosz Golaszewski818d0222016-06-06 10:48:51 +0200167 { "24cs64", AT24_DEVICE_MAGIC(16,
168 AT24_FLAG_ADDR16 |
169 AT24_FLAG_SERIAL |
170 AT24_FLAG_READONLY) },
Bartosz Golaszewskib0b9f7b2016-06-06 10:48:43 +0200171 { "24c128", AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16) },
172 { "24c256", AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16) },
173 { "24c512", AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16) },
174 { "24c1024", AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16) },
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200175 { "at24", 0 },
176 { /* END OF LIST */ }
177};
178MODULE_DEVICE_TABLE(i2c, at24_ids);
179
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200180static const struct of_device_id at24_of_match[] = {
181 {
182 .compatible = "atmel,24c00",
183 .data = (void *)AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR)
184 },
185 {
186 .compatible = "atmel,24c01",
187 .data = (void *)AT24_DEVICE_MAGIC(1024 / 8, 0)
188 },
189 {
190 .compatible = "atmel,24c02",
191 .data = (void *)AT24_DEVICE_MAGIC(2048 / 8, 0)
192 },
193 {
194 .compatible = "atmel,spd",
195 .data = (void *)AT24_DEVICE_MAGIC(2048 / 8,
196 AT24_FLAG_READONLY | AT24_FLAG_IRUGO)
197 },
198 {
199 .compatible = "atmel,24c04",
200 .data = (void *)AT24_DEVICE_MAGIC(4096 / 8, 0)
201 },
202 {
203 .compatible = "atmel,24c08",
204 .data = (void *)AT24_DEVICE_MAGIC(8192 / 8, 0)
205 },
206 {
207 .compatible = "atmel,24c16",
208 .data = (void *)AT24_DEVICE_MAGIC(16384 / 8, 0)
209 },
210 {
211 .compatible = "atmel,24c32",
212 .data = (void *)AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16)
213 },
214 {
215 .compatible = "atmel,24c64",
216 .data = (void *)AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16)
217 },
218 {
219 .compatible = "atmel,24c128",
220 .data = (void *)AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16)
221 },
222 {
223 .compatible = "atmel,24c256",
224 .data = (void *)AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16)
225 },
226 {
227 .compatible = "atmel,24c512",
228 .data = (void *)AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16)
229 },
230 {
231 .compatible = "atmel,24c1024",
232 .data = (void *)AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16)
233 },
234 { },
235};
236MODULE_DEVICE_TABLE(of, at24_of_match);
237
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300238static const struct acpi_device_id at24_acpi_ids[] = {
239 { "INT3499", AT24_DEVICE_MAGIC(8192 / 8, 0) },
240 { }
241};
242MODULE_DEVICE_TABLE(acpi, at24_acpi_ids);
243
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200244/*-------------------------------------------------------------------------*/
245
246/*
247 * This routine supports chips which consume multiple I2C addresses. It
248 * computes the addressing information to be used for a given r/w request.
249 * Assumes that sanity checks for offset happened at sysfs-layer.
Bartosz Golaszewski9afd68662016-06-06 10:48:48 +0200250 *
251 * Slave address and byte offset derive from the offset. Always
252 * set the byte address; on a multi-master board, another master
253 * may have changed the chip's "current" address pointer.
254 *
255 * REVISIT some multi-address chips don't rollover page reads to
256 * the next slave address, so we may need to truncate the count.
257 * Those chips might need another quirk flag.
258 *
259 * If the real hardware used four adjacent 24c02 chips and that
260 * were misconfigured as one 24c08, that would be a similar effect:
261 * one "eeprom" file not four, but larger reads would fail when
262 * they crossed certain pages.
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200263 */
Heiner Kallweit46049482017-11-28 21:51:42 +0100264static struct at24_client *at24_translate_offset(struct at24_data *at24,
265 unsigned int *offset)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200266{
267 unsigned i;
268
269 if (at24->chip.flags & AT24_FLAG_ADDR16) {
270 i = *offset >> 16;
271 *offset &= 0xffff;
272 } else {
273 i = *offset >> 8;
274 *offset &= 0xff;
275 }
276
Heiner Kallweit46049482017-11-28 21:51:42 +0100277 return &at24->client[i];
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200278}
279
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100280static ssize_t at24_regmap_read(struct at24_data *at24, char *buf,
281 unsigned int offset, size_t count)
282{
283 unsigned long timeout, read_time;
284 struct at24_client *at24_client;
285 struct i2c_client *client;
286 struct regmap *regmap;
287 int ret;
288
289 at24_client = at24_translate_offset(at24, &offset);
290 regmap = at24_client->regmap;
291 client = at24_client->client;
292
293 if (count > io_limit)
294 count = io_limit;
295
296 /* adjust offset for mac and serial read ops */
297 offset += at24->offset_adj;
298
299 loop_until_timeout(timeout, read_time) {
300 ret = regmap_bulk_read(regmap, offset, buf, count);
301 dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
302 count, offset, ret, jiffies);
303 if (!ret)
304 return count;
305 }
306
307 return -ETIMEDOUT;
308}
309
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200310/*
311 * Note that if the hardware write-protect pin is pulled high, the whole
312 * chip is normally write protected. But there are plenty of product
313 * variants here, including OTP fuses and partial chip protect.
314 *
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200315 * We only use page mode writes; the alternative is sloooow. These routines
316 * write at most one page.
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200317 */
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200318
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200319static size_t at24_adjust_write_count(struct at24_data *at24,
320 unsigned int offset, size_t count)
321{
322 unsigned next_page;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200323
324 /* write_max is at most a page */
325 if (count > at24->write_max)
326 count = at24->write_max;
327
328 /* Never roll over backwards, to the start of this page */
329 next_page = roundup(offset + 1, at24->chip.page_size);
330 if (offset + count > next_page)
331 count = next_page - offset;
332
Bartosz Golaszewskicd0c8612016-06-06 10:48:49 +0200333 return count;
334}
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200335
Heiner Kallweit8e5888e2017-11-28 21:51:45 +0100336static ssize_t at24_regmap_write(struct at24_data *at24, const char *buf,
337 unsigned int offset, size_t count)
338{
339 unsigned long timeout, write_time;
340 struct at24_client *at24_client;
341 struct i2c_client *client;
342 struct regmap *regmap;
343 int ret;
344
345 at24_client = at24_translate_offset(at24, &offset);
346 regmap = at24_client->regmap;
347 client = at24_client->client;
348 count = at24_adjust_write_count(at24, offset, count);
349
350 loop_until_timeout(timeout, write_time) {
351 ret = regmap_bulk_write(regmap, offset, buf, count);
352 dev_dbg(&client->dev, "write %zu@%d --> %d (%ld)\n",
353 count, offset, ret, jiffies);
354 if (!ret)
355 return count;
356 }
357
358 return -ETIMEDOUT;
359}
360
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200361static int at24_read(void *priv, unsigned int off, void *val, size_t count)
362{
363 struct at24_data *at24 = priv;
Heiner Kallweit5c015252017-11-28 21:51:40 +0100364 struct device *dev = &at24->client[0].client->dev;
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200365 char *buf = val;
Divagar Mohandass98e82012017-10-10 11:30:37 +0530366 int ret;
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200367
368 if (unlikely(!count))
369 return count;
370
Heiner Kallweitd9bcd462017-11-24 07:47:50 +0100371 if (off + count > at24->chip.byte_len)
372 return -EINVAL;
373
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500374 ret = pm_runtime_get_sync(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530375 if (ret < 0) {
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500376 pm_runtime_put_noidle(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530377 return ret;
378 }
379
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200380 /*
381 * Read data from chip, protecting against concurrent updates
382 * from this host, but not from other I2C masters.
383 */
384 mutex_lock(&at24->lock);
385
386 while (count) {
387 int status;
388
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100389 status = at24_regmap_read(at24, buf, off, count);
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200390 if (status < 0) {
391 mutex_unlock(&at24->lock);
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500392 pm_runtime_put(dev);
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200393 return status;
394 }
395 buf += status;
396 off += status;
397 count -= status;
398 }
399
400 mutex_unlock(&at24->lock);
401
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500402 pm_runtime_put(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530403
Bartosz Golaszewskid5bc0042016-06-06 10:48:44 +0200404 return 0;
405}
406
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100407static int at24_write(void *priv, unsigned int off, void *val, size_t count)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200408{
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100409 struct at24_data *at24 = priv;
Heiner Kallweit5c015252017-11-28 21:51:40 +0100410 struct device *dev = &at24->client[0].client->dev;
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100411 char *buf = val;
Divagar Mohandass98e82012017-10-10 11:30:37 +0530412 int ret;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200413
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200414 if (unlikely(!count))
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100415 return -EINVAL;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200416
Heiner Kallweitd9bcd462017-11-24 07:47:50 +0100417 if (off + count > at24->chip.byte_len)
418 return -EINVAL;
419
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500420 ret = pm_runtime_get_sync(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530421 if (ret < 0) {
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500422 pm_runtime_put_noidle(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530423 return ret;
424 }
425
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200426 /*
427 * Write data to chip, protecting against concurrent updates
428 * from this host, but not from other I2C masters.
429 */
430 mutex_lock(&at24->lock);
431
432 while (count) {
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100433 int status;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200434
Heiner Kallweit8e5888e2017-11-28 21:51:45 +0100435 status = at24_regmap_write(at24, buf, off, count);
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100436 if (status < 0) {
437 mutex_unlock(&at24->lock);
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500438 pm_runtime_put(dev);
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100439 return status;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200440 }
441 buf += status;
442 off += status;
443 count -= status;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200444 }
445
446 mutex_unlock(&at24->lock);
447
Sakari Ailusf9ecc832017-12-01 13:37:12 -0500448 pm_runtime_put(dev);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530449
Andrew Lunn57d15552016-02-26 20:59:20 +0100450 return 0;
451}
452
Ben Gardnerdd905a62017-02-09 11:36:08 -0600453static void at24_get_pdata(struct device *dev, struct at24_platform_data *chip)
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100454{
Ben Gardnerdd905a62017-02-09 11:36:08 -0600455 int err;
456 u32 val;
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100457
Ben Gardnerdd905a62017-02-09 11:36:08 -0600458 if (device_property_present(dev, "read-only"))
459 chip->flags |= AT24_FLAG_READONLY;
460
Divagar Mohandassdbc1ab92017-10-10 11:30:36 +0530461 err = device_property_read_u32(dev, "size", &val);
462 if (!err)
463 chip->byte_len = val;
464
Ben Gardnerdd905a62017-02-09 11:36:08 -0600465 err = device_property_read_u32(dev, "pagesize", &val);
466 if (!err) {
467 chip->page_size = val;
468 } else {
469 /*
470 * This is slow, but we can't know all eeproms, so we better
471 * play safe. Specifying custom eeprom-types via platform_data
472 * is recommended anyhow.
473 */
474 chip->page_size = 1;
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100475 }
476}
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100477
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100478static unsigned int at24_get_offset_adj(u8 flags, unsigned int byte_len)
479{
480 if (flags & AT24_FLAG_MAC) {
481 /* EUI-48 starts from 0x9a, EUI-64 from 0x98 */
482 return 0xa0 - byte_len;
483 } else if (flags & AT24_FLAG_SERIAL && flags & AT24_FLAG_ADDR16) {
484 /*
485 * For 16 bit address pointers, the word address must contain
486 * a '10' sequence in bits 11 and 10 regardless of the
487 * intended position of the address pointer.
488 */
489 return 0x0800;
490 } else if (flags & AT24_FLAG_SERIAL) {
491 /*
492 * Otherwise the word address must begin with a '10' sequence,
493 * regardless of the intended address.
494 */
495 return 0x0080;
496 } else {
497 return 0;
498 }
499}
500
Heiner Kallweit5c015252017-11-28 21:51:40 +0100501static const struct regmap_config regmap_config_8 = {
502 .reg_bits = 8,
503 .val_bits = 8,
504};
505
506static const struct regmap_config regmap_config_16 = {
507 .reg_bits = 16,
508 .val_bits = 8,
509};
510
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200511static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
512{
513 struct at24_platform_data chip;
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300514 kernel_ulong_t magic = 0;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200515 bool writable;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200516 struct at24_data *at24;
517 int err;
518 unsigned i, num_addresses;
Heiner Kallweit5c015252017-11-28 21:51:40 +0100519 const struct regmap_config *config;
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200520 u8 test_byte;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200521
522 if (client->dev.platform_data) {
523 chip = *(struct at24_platform_data *)client->dev.platform_data;
524 } else {
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200525 /*
526 * The I2C core allows OF nodes compatibles to match against the
527 * I2C device ID table as a fallback, so check not only if an OF
528 * node is present but also if it matches an OF device ID entry.
529 */
530 if (client->dev.of_node &&
531 of_match_device(at24_of_match, &client->dev)) {
532 magic = (kernel_ulong_t)
533 of_device_get_match_data(&client->dev);
534 } else if (id) {
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300535 magic = id->driver_data;
536 } else {
537 const struct acpi_device_id *aid;
538
539 aid = acpi_match_device(at24_acpi_ids, &client->dev);
540 if (aid)
541 magic = aid->driver_data;
542 }
543 if (!magic)
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700544 return -ENODEV;
545
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200546 chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN));
547 magic >>= AT24_SIZE_BYTELEN;
548 chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS);
Kevin Hilman7274ec82009-04-02 16:56:57 -0700549
Ben Gardnerdd905a62017-02-09 11:36:08 -0600550 at24_get_pdata(&client->dev, &chip);
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100551
Kevin Hilman7274ec82009-04-02 16:56:57 -0700552 chip.setup = NULL;
553 chip.context = NULL;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200554 }
555
556 if (!is_power_of_2(chip.byte_len))
557 dev_warn(&client->dev,
558 "byte_len looks suspicious (no power of 2)!\n");
Wolfram Sang45efe842010-11-17 13:00:49 +0100559 if (!chip.page_size) {
560 dev_err(&client->dev, "page_size must not be 0!\n");
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700561 return -EINVAL;
Wolfram Sang45efe842010-11-17 13:00:49 +0100562 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200563 if (!is_power_of_2(chip.page_size))
564 dev_warn(&client->dev,
565 "page_size looks suspicious (no power of 2)!\n");
566
Bartosz Golaszewski5478e472017-11-27 22:06:13 +0100567 /*
568 * REVISIT: the size of the EUI-48 byte array is 6 in at24mac402, while
569 * the call to ilog2() in AT24_DEVICE_MAGIC() rounds it down to 4.
570 *
571 * Eventually we'll get rid of the magic values altoghether in favor of
572 * real structs, but for now just manually set the right size.
573 */
574 if (chip.flags & AT24_FLAG_MAC && chip.byte_len == 4)
575 chip.byte_len = 6;
576
Heiner Kallweita23727c2017-11-28 21:51:54 +0100577 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C) &&
578 !i2c_check_functionality(client->adapter,
579 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK))
580 chip.page_size = 1;
Christian Gmeinera839ce62014-10-09 11:07:58 +0200581
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200582 if (chip.flags & AT24_FLAG_TAKE8ADDR)
583 num_addresses = 8;
584 else
585 num_addresses = DIV_ROUND_UP(chip.byte_len,
586 (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
587
Heiner Kallweit5c015252017-11-28 21:51:40 +0100588 if (chip.flags & AT24_FLAG_ADDR16)
589 config = &regmap_config_16;
590 else
591 config = &regmap_config_8;
592
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700593 at24 = devm_kzalloc(&client->dev, sizeof(struct at24_data) +
Heiner Kallweit5c015252017-11-28 21:51:40 +0100594 num_addresses * sizeof(struct at24_client), GFP_KERNEL);
Nikolay Balandinf0ac2362013-05-28 13:00:20 -0700595 if (!at24)
596 return -ENOMEM;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200597
598 mutex_init(&at24->lock);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200599 at24->chip = chip;
600 at24->num_addresses = num_addresses;
Heiner Kallweit4bb5c13c2017-11-28 21:51:50 +0100601 at24->offset_adj = at24_get_offset_adj(chip.flags, chip.byte_len);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200602
Heiner Kallweit5c015252017-11-28 21:51:40 +0100603 at24->client[0].client = client;
604 at24->client[0].regmap = devm_regmap_init_i2c(client, config);
605 if (IS_ERR(at24->client[0].regmap))
606 return PTR_ERR(at24->client[0].regmap);
607
Bartosz Golaszewski0b813652016-06-06 10:48:54 +0200608 if ((chip.flags & AT24_FLAG_SERIAL) && (chip.flags & AT24_FLAG_MAC)) {
609 dev_err(&client->dev,
610 "invalid device data - cannot have both AT24_FLAG_SERIAL & AT24_FLAG_MAC.");
611 return -EINVAL;
612 }
613
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200614 writable = !(chip.flags & AT24_FLAG_READONLY);
615 if (writable) {
Heiner Kallweita23727c2017-11-28 21:51:54 +0100616 at24->write_max = min_t(unsigned int, chip.page_size, io_limit);
617 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C) &&
618 at24->write_max > I2C_SMBUS_BLOCK_MAX)
619 at24->write_max = I2C_SMBUS_BLOCK_MAX;
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200620 }
621
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200622 /* use dummy devices for multiple-address chips */
623 for (i = 1; i < num_addresses; i++) {
Heiner Kallweit5c015252017-11-28 21:51:40 +0100624 at24->client[i].client = i2c_new_dummy(client->adapter,
625 client->addr + i);
626 if (!at24->client[i].client) {
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200627 dev_err(&client->dev, "address 0x%02x unavailable\n",
628 client->addr + i);
629 err = -EADDRINUSE;
630 goto err_clients;
631 }
Heiner Kallweit5c015252017-11-28 21:51:40 +0100632 at24->client[i].regmap = devm_regmap_init_i2c(
633 at24->client[i].client, config);
634 if (IS_ERR(at24->client[i].regmap)) {
635 err = PTR_ERR(at24->client[i].regmap);
636 goto err_clients;
637 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200638 }
639
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200640 i2c_set_clientdata(client, at24);
641
Divagar Mohandass98e82012017-10-10 11:30:37 +0530642 /* enable runtime pm */
643 pm_runtime_set_active(&client->dev);
644 pm_runtime_enable(&client->dev);
645
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200646 /*
647 * Perform a one-byte test read to verify that the
648 * chip is functional.
649 */
650 err = at24_read(at24, 0, &test_byte, 1);
Divagar Mohandass98e82012017-10-10 11:30:37 +0530651 pm_runtime_idle(&client->dev);
Bartosz Golaszewski00f0ea72016-08-12 13:32:57 +0200652 if (err) {
653 err = -ENODEV;
654 goto err_clients;
655 }
656
Andrew Lunn57d15552016-02-26 20:59:20 +0100657 at24->nvmem_config.name = dev_name(&client->dev);
658 at24->nvmem_config.dev = &client->dev;
659 at24->nvmem_config.read_only = !writable;
660 at24->nvmem_config.root_only = true;
661 at24->nvmem_config.owner = THIS_MODULE;
662 at24->nvmem_config.compat = true;
663 at24->nvmem_config.base_dev = &client->dev;
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100664 at24->nvmem_config.reg_read = at24_read;
665 at24->nvmem_config.reg_write = at24_write;
666 at24->nvmem_config.priv = at24;
David Lechner7f6d2ec2017-12-03 19:54:41 -0600667 at24->nvmem_config.stride = 1;
Srinivas Kandagatlacf0361a2016-04-24 20:28:06 +0100668 at24->nvmem_config.word_size = 1;
669 at24->nvmem_config.size = chip.byte_len;
Andrew Lunn57d15552016-02-26 20:59:20 +0100670
671 at24->nvmem = nvmem_register(&at24->nvmem_config);
672
673 if (IS_ERR(at24->nvmem)) {
674 err = PTR_ERR(at24->nvmem);
675 goto err_clients;
676 }
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200677
Andrew Lunn57d15552016-02-26 20:59:20 +0100678 dev_info(&client->dev, "%u byte %s EEPROM, %s, %u bytes/write\n",
679 chip.byte_len, client->name,
Wolfram Sang9ed030d2010-11-17 13:00:48 +0100680 writable ? "writable" : "read-only", at24->write_max);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200681
Kevin Hilman7274ec82009-04-02 16:56:57 -0700682 /* export data to kernel code */
683 if (chip.setup)
Andrew Lunnbec3c112016-02-26 20:59:24 +0100684 chip.setup(at24->nvmem, chip.context);
Kevin Hilman7274ec82009-04-02 16:56:57 -0700685
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200686 return 0;
687
688err_clients:
689 for (i = 1; i < num_addresses; i++)
Heiner Kallweit5c015252017-11-28 21:51:40 +0100690 if (at24->client[i].client)
691 i2c_unregister_device(at24->client[i].client);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200692
Divagar Mohandass98e82012017-10-10 11:30:37 +0530693 pm_runtime_disable(&client->dev);
694
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200695 return err;
696}
697
Bill Pemberton486a5c22012-11-19 13:26:02 -0500698static int at24_remove(struct i2c_client *client)
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200699{
700 struct at24_data *at24;
701 int i;
702
703 at24 = i2c_get_clientdata(client);
Andrew Lunn57d15552016-02-26 20:59:20 +0100704
705 nvmem_unregister(at24->nvmem);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200706
707 for (i = 1; i < at24->num_addresses; i++)
Heiner Kallweit5c015252017-11-28 21:51:40 +0100708 i2c_unregister_device(at24->client[i].client);
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200709
Divagar Mohandass98e82012017-10-10 11:30:37 +0530710 pm_runtime_disable(&client->dev);
711 pm_runtime_set_suspended(&client->dev);
712
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200713 return 0;
714}
715
716/*-------------------------------------------------------------------------*/
717
718static struct i2c_driver at24_driver = {
719 .driver = {
720 .name = "at24",
Javier Martinez Canillas7f2a2f02017-10-01 12:49:48 +0200721 .of_match_table = at24_of_match,
Andy Shevchenko40d8edc2015-10-23 12:16:44 +0300722 .acpi_match_table = ACPI_PTR(at24_acpi_ids),
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200723 },
724 .probe = at24_probe,
Bill Pemberton2d6bed92012-11-19 13:21:23 -0500725 .remove = at24_remove,
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200726 .id_table = at24_ids,
727};
728
729static int __init at24_init(void)
730{
Wolfram Sang45efe842010-11-17 13:00:49 +0100731 if (!io_limit) {
732 pr_err("at24: io_limit must not be 0!\n");
733 return -EINVAL;
734 }
735
Wolfram Sang2b7a5052008-07-14 22:38:35 +0200736 io_limit = rounddown_pow_of_two(io_limit);
737 return i2c_add_driver(&at24_driver);
738}
739module_init(at24_init);
740
741static void __exit at24_exit(void)
742{
743 i2c_del_driver(&at24_driver);
744}
745module_exit(at24_exit);
746
747MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
748MODULE_AUTHOR("David Brownell and Wolfram Sang");
749MODULE_LICENSE("GPL");