Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 1 | /* |
| 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 Canillas | 7f2a2f0 | 2017-10-01 12:49:48 +0200 | [diff] [blame] | 15 | #include <linux/of_device.h> |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/mutex.h> |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 19 | #include <linux/mod_devicetable.h> |
| 20 | #include <linux/log2.h> |
| 21 | #include <linux/bitops.h> |
| 22 | #include <linux/jiffies.h> |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 23 | #include <linux/property.h> |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 24 | #include <linux/acpi.h> |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 25 | #include <linux/i2c.h> |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 26 | #include <linux/nvmem-provider.h> |
Vivien Didelot | 25f73ed | 2013-09-27 15:06:28 -0400 | [diff] [blame] | 27 | #include <linux/platform_data/at24.h> |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 28 | #include <linux/pm_runtime.h> |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * I2C EEPROMs from most vendors are inexpensive and mostly interchangeable. |
| 32 | * Differences between different vendor product lines (like Atmel AT24C or |
| 33 | * MicroChip 24LC, etc) won't much matter for typical read/write access. |
| 34 | * There are also I2C RAM chips, likewise interchangeable. One example |
| 35 | * would be the PCF8570, which acts like a 24c02 EEPROM (256 bytes). |
| 36 | * |
| 37 | * However, misconfiguration can lose data. "Set 16-bit memory address" |
| 38 | * to a part with 8-bit addressing will overwrite data. Writing with too |
| 39 | * big a page size also loses data. And it's not safe to assume that the |
| 40 | * conventional addresses 0x50..0x57 only hold eeproms; a PCF8563 RTC |
| 41 | * uses 0x51, for just one example. |
| 42 | * |
| 43 | * Accordingly, explicit board-specific configuration data should be used |
| 44 | * in almost all cases. (One partial exception is an SMBus used to access |
| 45 | * "SPD" data for DRAM sticks. Those only use 24c02 EEPROMs.) |
| 46 | * |
| 47 | * So this driver uses "new style" I2C driver binding, expecting to be |
| 48 | * told what devices exist. That may be in arch/X/mach-Y/board-Z.c or |
| 49 | * similar kernel-resident tables; or, configuration data coming from |
| 50 | * a bootloader. |
| 51 | * |
| 52 | * Other than binding model, current differences from "eeprom" driver are |
| 53 | * that this one handles write access and isn't restricted to 24c02 devices. |
| 54 | * It also handles larger devices (32 kbit and up) with two-byte addresses, |
| 55 | * which won't work on pure SMBus systems. |
| 56 | */ |
| 57 | |
| 58 | struct at24_data { |
| 59 | struct at24_platform_data chip; |
Jean Delvare | 7aeb966 | 2010-05-21 18:40:57 +0200 | [diff] [blame] | 60 | int use_smbus; |
Christian Gmeiner | a839ce6 | 2014-10-09 11:07:58 +0200 | [diff] [blame] | 61 | int use_smbus_write; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 62 | |
Bartosz Golaszewski | 318aa9c | 2016-06-06 10:48:46 +0200 | [diff] [blame] | 63 | ssize_t (*read_func)(struct at24_data *, char *, unsigned int, size_t); |
| 64 | ssize_t (*write_func)(struct at24_data *, |
| 65 | const char *, unsigned int, size_t); |
| 66 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 67 | /* |
| 68 | * Lock protects against activities from other Linux tasks, |
| 69 | * but not from changes by other I2C masters. |
| 70 | */ |
| 71 | struct mutex lock; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 72 | |
| 73 | u8 *writebuf; |
| 74 | unsigned write_max; |
| 75 | unsigned num_addresses; |
| 76 | |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 77 | struct nvmem_config nvmem_config; |
| 78 | struct nvmem_device *nvmem; |
| 79 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 80 | /* |
| 81 | * Some chips tie up multiple I2C addresses; dummy devices reserve |
| 82 | * them for us, and we'll use them with SMBus calls. |
| 83 | */ |
| 84 | struct i2c_client *client[]; |
| 85 | }; |
| 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 | */ |
| 96 | static unsigned io_limit = 128; |
| 97 | module_param(io_limit, uint, 0); |
| 98 | MODULE_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 | */ |
| 104 | static unsigned write_timeout = 25; |
| 105 | module_param(write_timeout, uint, 0); |
| 106 | MODULE_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 Golaszewski | 9344a81 | 2016-06-06 10:48:47 +0200 | [diff] [blame] | 118 | /* |
| 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 Golaszewski | 24da3cc | 2016-07-17 20:40:06 +0200 | [diff] [blame] | 121 | * write to work while making sure that at least one iteration is run before |
| 122 | * checking the break condition. |
Bartosz Golaszewski | 9344a81 | 2016-06-06 10:48:47 +0200 | [diff] [blame] | 123 | * |
| 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 Golaszewski | 24da3cc | 2016-07-17 20:40:06 +0200 | [diff] [blame] | 130 | for (tout = jiffies + msecs_to_jiffies(write_timeout), op_time = 0; \ |
| 131 | op_time ? time_before(op_time, tout) : true; \ |
Bartosz Golaszewski | 9344a81 | 2016-06-06 10:48:47 +0200 | [diff] [blame] | 132 | usleep_range(1000, 1500), op_time = jiffies) |
| 133 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 134 | static const struct i2c_device_id at24_ids[] = { |
| 135 | /* needs 8 addresses as A0-A2 are ignored */ |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 136 | { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) }, |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 137 | /* old variants can't be handled with this generic entry! */ |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 138 | { "24c01", AT24_DEVICE_MAGIC(1024 / 8, 0) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 139 | { "24cs01", AT24_DEVICE_MAGIC(16, |
| 140 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY) }, |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 141 | { "24c02", AT24_DEVICE_MAGIC(2048 / 8, 0) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 142 | { "24cs02", AT24_DEVICE_MAGIC(16, |
| 143 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY) }, |
Bartosz Golaszewski | 0b81365 | 2016-06-06 10:48:54 +0200 | [diff] [blame] | 144 | { "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 Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 148 | /* spd is a 24c02 in memory DIMMs */ |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 149 | { "spd", AT24_DEVICE_MAGIC(2048 / 8, |
| 150 | AT24_FLAG_READONLY | AT24_FLAG_IRUGO) }, |
| 151 | { "24c04", AT24_DEVICE_MAGIC(4096 / 8, 0) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 152 | { "24cs04", AT24_DEVICE_MAGIC(16, |
| 153 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY) }, |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 154 | /* 24rf08 quirk is handled at i2c-core */ |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 155 | { "24c08", AT24_DEVICE_MAGIC(8192 / 8, 0) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 156 | { "24cs08", AT24_DEVICE_MAGIC(16, |
| 157 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY) }, |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 158 | { "24c16", AT24_DEVICE_MAGIC(16384 / 8, 0) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 159 | { "24cs16", AT24_DEVICE_MAGIC(16, |
| 160 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY) }, |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 161 | { "24c32", AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 162 | { "24cs32", AT24_DEVICE_MAGIC(16, |
| 163 | AT24_FLAG_ADDR16 | |
| 164 | AT24_FLAG_SERIAL | |
| 165 | AT24_FLAG_READONLY) }, |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 166 | { "24c64", AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 167 | { "24cs64", AT24_DEVICE_MAGIC(16, |
| 168 | AT24_FLAG_ADDR16 | |
| 169 | AT24_FLAG_SERIAL | |
| 170 | AT24_FLAG_READONLY) }, |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 171 | { "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 Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 175 | { "at24", 0 }, |
| 176 | { /* END OF LIST */ } |
| 177 | }; |
| 178 | MODULE_DEVICE_TABLE(i2c, at24_ids); |
| 179 | |
Javier Martinez Canillas | 7f2a2f0 | 2017-10-01 12:49:48 +0200 | [diff] [blame] | 180 | static 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 | }; |
| 236 | MODULE_DEVICE_TABLE(of, at24_of_match); |
| 237 | |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 238 | static const struct acpi_device_id at24_acpi_ids[] = { |
| 239 | { "INT3499", AT24_DEVICE_MAGIC(8192 / 8, 0) }, |
| 240 | { } |
| 241 | }; |
| 242 | MODULE_DEVICE_TABLE(acpi, at24_acpi_ids); |
| 243 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 244 | /*-------------------------------------------------------------------------*/ |
| 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 Golaszewski | 9afd6866 | 2016-06-06 10:48:48 +0200 | [diff] [blame] | 250 | * |
| 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 Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 263 | */ |
| 264 | static struct i2c_client *at24_translate_offset(struct at24_data *at24, |
Bartosz Golaszewski | 2da78ac | 2016-06-06 10:48:45 +0200 | [diff] [blame] | 265 | unsigned int *offset) |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 266 | { |
| 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 | |
| 277 | return at24->client[i]; |
| 278 | } |
| 279 | |
Bartosz Golaszewski | 9afd6866 | 2016-06-06 10:48:48 +0200 | [diff] [blame] | 280 | static ssize_t at24_eeprom_read_smbus(struct at24_data *at24, char *buf, |
| 281 | unsigned int offset, size_t count) |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 282 | { |
Wolfram Sang | 4d29196 | 2009-11-26 09:22:33 +0100 | [diff] [blame] | 283 | unsigned long timeout, read_time; |
Bartosz Golaszewski | 9afd6866 | 2016-06-06 10:48:48 +0200 | [diff] [blame] | 284 | struct i2c_client *client; |
| 285 | int status; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 286 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 287 | client = at24_translate_offset(at24, &offset); |
| 288 | |
| 289 | if (count > io_limit) |
| 290 | count = io_limit; |
| 291 | |
Bartosz Golaszewski | 9afd6866 | 2016-06-06 10:48:48 +0200 | [diff] [blame] | 292 | /* Smaller eeproms can work given some SMBus extension calls */ |
| 293 | if (count > I2C_SMBUS_BLOCK_MAX) |
| 294 | count = I2C_SMBUS_BLOCK_MAX; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 295 | |
Bartosz Golaszewski | 9344a81 | 2016-06-06 10:48:47 +0200 | [diff] [blame] | 296 | loop_until_timeout(timeout, read_time) { |
Bartosz Golaszewski | 9afd6866 | 2016-06-06 10:48:48 +0200 | [diff] [blame] | 297 | status = i2c_smbus_read_i2c_block_data_or_emulated(client, |
| 298 | offset, |
| 299 | count, buf); |
| 300 | |
| 301 | dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n", |
| 302 | count, offset, status, jiffies); |
| 303 | |
| 304 | if (status == count) |
| 305 | return count; |
| 306 | } |
| 307 | |
| 308 | return -ETIMEDOUT; |
| 309 | } |
| 310 | |
| 311 | static ssize_t at24_eeprom_read_i2c(struct at24_data *at24, char *buf, |
| 312 | unsigned int offset, size_t count) |
| 313 | { |
| 314 | unsigned long timeout, read_time; |
| 315 | struct i2c_client *client; |
| 316 | struct i2c_msg msg[2]; |
| 317 | int status, i; |
| 318 | u8 msgbuf[2]; |
| 319 | |
| 320 | memset(msg, 0, sizeof(msg)); |
| 321 | client = at24_translate_offset(at24, &offset); |
| 322 | |
| 323 | if (count > io_limit) |
| 324 | count = io_limit; |
| 325 | |
| 326 | /* |
| 327 | * When we have a better choice than SMBus calls, use a combined I2C |
| 328 | * message. Write address; then read up to io_limit data bytes. Note |
| 329 | * that read page rollover helps us here (unlike writes). msgbuf is |
| 330 | * u8 and will cast to our needs. |
| 331 | */ |
| 332 | i = 0; |
| 333 | if (at24->chip.flags & AT24_FLAG_ADDR16) |
| 334 | msgbuf[i++] = offset >> 8; |
| 335 | msgbuf[i++] = offset; |
| 336 | |
| 337 | msg[0].addr = client->addr; |
| 338 | msg[0].buf = msgbuf; |
| 339 | msg[0].len = i; |
| 340 | |
| 341 | msg[1].addr = client->addr; |
| 342 | msg[1].flags = I2C_M_RD; |
| 343 | msg[1].buf = buf; |
| 344 | msg[1].len = count; |
| 345 | |
| 346 | loop_until_timeout(timeout, read_time) { |
| 347 | status = i2c_transfer(client->adapter, msg, 2); |
| 348 | if (status == 2) |
| 349 | status = count; |
| 350 | |
Wolfram Sang | 4d29196 | 2009-11-26 09:22:33 +0100 | [diff] [blame] | 351 | dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n", |
| 352 | count, offset, status, jiffies); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 353 | |
Wolfram Sang | 4d29196 | 2009-11-26 09:22:33 +0100 | [diff] [blame] | 354 | if (status == count) |
| 355 | return count; |
Bartosz Golaszewski | 9344a81 | 2016-06-06 10:48:47 +0200 | [diff] [blame] | 356 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 357 | |
Wolfram Sang | 4d29196 | 2009-11-26 09:22:33 +0100 | [diff] [blame] | 358 | return -ETIMEDOUT; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 359 | } |
| 360 | |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 361 | static ssize_t at24_eeprom_read_serial(struct at24_data *at24, char *buf, |
| 362 | unsigned int offset, size_t count) |
| 363 | { |
| 364 | unsigned long timeout, read_time; |
| 365 | struct i2c_client *client; |
| 366 | struct i2c_msg msg[2]; |
| 367 | u8 addrbuf[2]; |
| 368 | int status; |
| 369 | |
| 370 | client = at24_translate_offset(at24, &offset); |
| 371 | |
| 372 | memset(msg, 0, sizeof(msg)); |
| 373 | msg[0].addr = client->addr; |
| 374 | msg[0].buf = addrbuf; |
| 375 | |
| 376 | /* |
| 377 | * The address pointer of the device is shared between the regular |
| 378 | * EEPROM array and the serial number block. The dummy write (part of |
| 379 | * the sequential read protocol) ensures the address pointer is reset |
| 380 | * to the desired position. |
| 381 | */ |
| 382 | if (at24->chip.flags & AT24_FLAG_ADDR16) { |
| 383 | /* |
| 384 | * For 16 bit address pointers, the word address must contain |
| 385 | * a '10' sequence in bits 11 and 10 regardless of the |
| 386 | * intended position of the address pointer. |
| 387 | */ |
| 388 | addrbuf[0] = 0x08; |
| 389 | addrbuf[1] = offset; |
| 390 | msg[0].len = 2; |
| 391 | } else { |
| 392 | /* |
| 393 | * Otherwise the word address must begin with a '10' sequence, |
| 394 | * regardless of the intended address. |
| 395 | */ |
| 396 | addrbuf[0] = 0x80 + offset; |
| 397 | msg[0].len = 1; |
| 398 | } |
| 399 | |
| 400 | msg[1].addr = client->addr; |
| 401 | msg[1].flags = I2C_M_RD; |
| 402 | msg[1].buf = buf; |
| 403 | msg[1].len = count; |
| 404 | |
| 405 | loop_until_timeout(timeout, read_time) { |
| 406 | status = i2c_transfer(client->adapter, msg, 2); |
| 407 | if (status == 2) |
| 408 | return count; |
| 409 | } |
| 410 | |
| 411 | return -ETIMEDOUT; |
| 412 | } |
| 413 | |
Bartosz Golaszewski | 0b81365 | 2016-06-06 10:48:54 +0200 | [diff] [blame] | 414 | static ssize_t at24_eeprom_read_mac(struct at24_data *at24, char *buf, |
| 415 | unsigned int offset, size_t count) |
| 416 | { |
| 417 | unsigned long timeout, read_time; |
| 418 | struct i2c_client *client; |
| 419 | struct i2c_msg msg[2]; |
| 420 | u8 addrbuf[2]; |
| 421 | int status; |
| 422 | |
| 423 | client = at24_translate_offset(at24, &offset); |
| 424 | |
| 425 | memset(msg, 0, sizeof(msg)); |
| 426 | msg[0].addr = client->addr; |
| 427 | msg[0].buf = addrbuf; |
Heiner Kallweit | 644a1f1 | 2017-11-27 20:46:22 +0100 | [diff] [blame] | 428 | /* EUI-48 starts from 0x9a, EUI-64 from 0x98 */ |
| 429 | addrbuf[0] = 0xa0 - at24->chip.byte_len + offset; |
Bartosz Golaszewski | 0b81365 | 2016-06-06 10:48:54 +0200 | [diff] [blame] | 430 | msg[0].len = 1; |
| 431 | msg[1].addr = client->addr; |
| 432 | msg[1].flags = I2C_M_RD; |
| 433 | msg[1].buf = buf; |
| 434 | msg[1].len = count; |
| 435 | |
| 436 | loop_until_timeout(timeout, read_time) { |
| 437 | status = i2c_transfer(client->adapter, msg, 2); |
| 438 | if (status == 2) |
| 439 | return count; |
| 440 | } |
| 441 | |
| 442 | return -ETIMEDOUT; |
| 443 | } |
| 444 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 445 | /* |
| 446 | * Note that if the hardware write-protect pin is pulled high, the whole |
| 447 | * chip is normally write protected. But there are plenty of product |
| 448 | * variants here, including OTP fuses and partial chip protect. |
| 449 | * |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 450 | * We only use page mode writes; the alternative is sloooow. These routines |
| 451 | * write at most one page. |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 452 | */ |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 453 | |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 454 | static size_t at24_adjust_write_count(struct at24_data *at24, |
| 455 | unsigned int offset, size_t count) |
| 456 | { |
| 457 | unsigned next_page; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 458 | |
| 459 | /* write_max is at most a page */ |
| 460 | if (count > at24->write_max) |
| 461 | count = at24->write_max; |
| 462 | |
| 463 | /* Never roll over backwards, to the start of this page */ |
| 464 | next_page = roundup(offset + 1, at24->chip.page_size); |
| 465 | if (offset + count > next_page) |
| 466 | count = next_page - offset; |
| 467 | |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 468 | return count; |
| 469 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 470 | |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 471 | static ssize_t at24_eeprom_write_smbus_block(struct at24_data *at24, |
| 472 | const char *buf, |
| 473 | unsigned int offset, size_t count) |
| 474 | { |
| 475 | unsigned long timeout, write_time; |
| 476 | struct i2c_client *client; |
| 477 | ssize_t status = 0; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 478 | |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 479 | client = at24_translate_offset(at24, &offset); |
| 480 | count = at24_adjust_write_count(at24, offset, count); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 481 | |
Bartosz Golaszewski | 9344a81 | 2016-06-06 10:48:47 +0200 | [diff] [blame] | 482 | loop_until_timeout(timeout, write_time) { |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 483 | status = i2c_smbus_write_i2c_block_data(client, |
| 484 | offset, count, buf); |
| 485 | if (status == 0) |
| 486 | status = count; |
Christian Gmeiner | a839ce6 | 2014-10-09 11:07:58 +0200 | [diff] [blame] | 487 | |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 488 | dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n", |
| 489 | count, offset, status, jiffies); |
| 490 | |
| 491 | if (status == count) |
| 492 | return count; |
| 493 | } |
| 494 | |
| 495 | return -ETIMEDOUT; |
| 496 | } |
| 497 | |
| 498 | static ssize_t at24_eeprom_write_smbus_byte(struct at24_data *at24, |
| 499 | const char *buf, |
| 500 | unsigned int offset, size_t count) |
| 501 | { |
| 502 | unsigned long timeout, write_time; |
| 503 | struct i2c_client *client; |
| 504 | ssize_t status = 0; |
| 505 | |
| 506 | client = at24_translate_offset(at24, &offset); |
| 507 | |
| 508 | loop_until_timeout(timeout, write_time) { |
| 509 | status = i2c_smbus_write_byte_data(client, offset, buf[0]); |
| 510 | if (status == 0) |
| 511 | status = count; |
| 512 | |
| 513 | dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n", |
| 514 | count, offset, status, jiffies); |
| 515 | |
| 516 | if (status == count) |
| 517 | return count; |
| 518 | } |
| 519 | |
| 520 | return -ETIMEDOUT; |
| 521 | } |
| 522 | |
| 523 | static ssize_t at24_eeprom_write_i2c(struct at24_data *at24, const char *buf, |
| 524 | unsigned int offset, size_t count) |
| 525 | { |
| 526 | unsigned long timeout, write_time; |
| 527 | struct i2c_client *client; |
| 528 | struct i2c_msg msg; |
| 529 | ssize_t status = 0; |
| 530 | int i = 0; |
| 531 | |
| 532 | client = at24_translate_offset(at24, &offset); |
| 533 | count = at24_adjust_write_count(at24, offset, count); |
| 534 | |
| 535 | msg.addr = client->addr; |
| 536 | msg.flags = 0; |
| 537 | |
| 538 | /* msg.buf is u8 and casts will mask the values */ |
| 539 | msg.buf = at24->writebuf; |
| 540 | if (at24->chip.flags & AT24_FLAG_ADDR16) |
| 541 | msg.buf[i++] = offset >> 8; |
| 542 | |
| 543 | msg.buf[i++] = offset; |
| 544 | memcpy(&msg.buf[i], buf, count); |
| 545 | msg.len = i + count; |
| 546 | |
| 547 | loop_until_timeout(timeout, write_time) { |
| 548 | status = i2c_transfer(client->adapter, &msg, 1); |
| 549 | if (status == 1) |
| 550 | status = count; |
| 551 | |
David Brownell | 2ce5b34 | 2008-08-10 22:56:16 +0200 | [diff] [blame] | 552 | dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n", |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 553 | count, offset, status, jiffies); |
| 554 | |
| 555 | if (status == count) |
| 556 | return count; |
Bartosz Golaszewski | 9344a81 | 2016-06-06 10:48:47 +0200 | [diff] [blame] | 557 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 558 | |
| 559 | return -ETIMEDOUT; |
| 560 | } |
| 561 | |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 562 | static int at24_read(void *priv, unsigned int off, void *val, size_t count) |
| 563 | { |
| 564 | struct at24_data *at24 = priv; |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 565 | struct device *dev = &at24->client[0]->dev; |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 566 | char *buf = val; |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 567 | int ret; |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 568 | |
| 569 | if (unlikely(!count)) |
| 570 | return count; |
| 571 | |
Heiner Kallweit | d9bcd46 | 2017-11-24 07:47:50 +0100 | [diff] [blame] | 572 | if (off + count > at24->chip.byte_len) |
| 573 | return -EINVAL; |
| 574 | |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 575 | ret = pm_runtime_get_sync(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 576 | if (ret < 0) { |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 577 | pm_runtime_put_noidle(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 578 | return ret; |
| 579 | } |
| 580 | |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 581 | /* |
| 582 | * Read data from chip, protecting against concurrent updates |
| 583 | * from this host, but not from other I2C masters. |
| 584 | */ |
| 585 | mutex_lock(&at24->lock); |
| 586 | |
| 587 | while (count) { |
| 588 | int status; |
| 589 | |
Bartosz Golaszewski | 318aa9c | 2016-06-06 10:48:46 +0200 | [diff] [blame] | 590 | status = at24->read_func(at24, buf, off, count); |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 591 | if (status < 0) { |
| 592 | mutex_unlock(&at24->lock); |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 593 | pm_runtime_put(dev); |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 594 | return status; |
| 595 | } |
| 596 | buf += status; |
| 597 | off += status; |
| 598 | count -= status; |
| 599 | } |
| 600 | |
| 601 | mutex_unlock(&at24->lock); |
| 602 | |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 603 | pm_runtime_put(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 604 | |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 605 | return 0; |
| 606 | } |
| 607 | |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 608 | static int at24_write(void *priv, unsigned int off, void *val, size_t count) |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 609 | { |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 610 | struct at24_data *at24 = priv; |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 611 | struct device *dev = &at24->client[0]->dev; |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 612 | char *buf = val; |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 613 | int ret; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 614 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 615 | if (unlikely(!count)) |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 616 | return -EINVAL; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 617 | |
Heiner Kallweit | d9bcd46 | 2017-11-24 07:47:50 +0100 | [diff] [blame] | 618 | if (off + count > at24->chip.byte_len) |
| 619 | return -EINVAL; |
| 620 | |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 621 | ret = pm_runtime_get_sync(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 622 | if (ret < 0) { |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 623 | pm_runtime_put_noidle(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 624 | return ret; |
| 625 | } |
| 626 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 627 | /* |
| 628 | * Write data to chip, protecting against concurrent updates |
| 629 | * from this host, but not from other I2C masters. |
| 630 | */ |
| 631 | mutex_lock(&at24->lock); |
| 632 | |
| 633 | while (count) { |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 634 | int status; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 635 | |
Bartosz Golaszewski | 318aa9c | 2016-06-06 10:48:46 +0200 | [diff] [blame] | 636 | status = at24->write_func(at24, buf, off, count); |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 637 | if (status < 0) { |
| 638 | mutex_unlock(&at24->lock); |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 639 | pm_runtime_put(dev); |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 640 | return status; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 641 | } |
| 642 | buf += status; |
| 643 | off += status; |
| 644 | count -= status; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | mutex_unlock(&at24->lock); |
| 648 | |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 649 | pm_runtime_put(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 650 | |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 651 | return 0; |
| 652 | } |
| 653 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 654 | static void at24_get_pdata(struct device *dev, struct at24_platform_data *chip) |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 655 | { |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 656 | int err; |
| 657 | u32 val; |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 658 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 659 | if (device_property_present(dev, "read-only")) |
| 660 | chip->flags |= AT24_FLAG_READONLY; |
| 661 | |
Divagar Mohandass | dbc1ab9 | 2017-10-10 11:30:36 +0530 | [diff] [blame] | 662 | err = device_property_read_u32(dev, "size", &val); |
| 663 | if (!err) |
| 664 | chip->byte_len = val; |
| 665 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 666 | err = device_property_read_u32(dev, "pagesize", &val); |
| 667 | if (!err) { |
| 668 | chip->page_size = val; |
| 669 | } else { |
| 670 | /* |
| 671 | * This is slow, but we can't know all eeproms, so we better |
| 672 | * play safe. Specifying custom eeprom-types via platform_data |
| 673 | * is recommended anyhow. |
| 674 | */ |
| 675 | chip->page_size = 1; |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 676 | } |
| 677 | } |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 678 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 679 | static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) |
| 680 | { |
| 681 | struct at24_platform_data chip; |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 682 | kernel_ulong_t magic = 0; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 683 | bool writable; |
Jean Delvare | 7aeb966 | 2010-05-21 18:40:57 +0200 | [diff] [blame] | 684 | int use_smbus = 0; |
Christian Gmeiner | a839ce6 | 2014-10-09 11:07:58 +0200 | [diff] [blame] | 685 | int use_smbus_write = 0; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 686 | struct at24_data *at24; |
| 687 | int err; |
| 688 | unsigned i, num_addresses; |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 689 | u8 test_byte; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 690 | |
| 691 | if (client->dev.platform_data) { |
| 692 | chip = *(struct at24_platform_data *)client->dev.platform_data; |
| 693 | } else { |
Javier Martinez Canillas | 7f2a2f0 | 2017-10-01 12:49:48 +0200 | [diff] [blame] | 694 | /* |
| 695 | * The I2C core allows OF nodes compatibles to match against the |
| 696 | * I2C device ID table as a fallback, so check not only if an OF |
| 697 | * node is present but also if it matches an OF device ID entry. |
| 698 | */ |
| 699 | if (client->dev.of_node && |
| 700 | of_match_device(at24_of_match, &client->dev)) { |
| 701 | magic = (kernel_ulong_t) |
| 702 | of_device_get_match_data(&client->dev); |
| 703 | } else if (id) { |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 704 | magic = id->driver_data; |
| 705 | } else { |
| 706 | const struct acpi_device_id *aid; |
| 707 | |
| 708 | aid = acpi_match_device(at24_acpi_ids, &client->dev); |
| 709 | if (aid) |
| 710 | magic = aid->driver_data; |
| 711 | } |
| 712 | if (!magic) |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 713 | return -ENODEV; |
| 714 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 715 | chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN)); |
| 716 | magic >>= AT24_SIZE_BYTELEN; |
| 717 | chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS); |
Kevin Hilman | 7274ec8 | 2009-04-02 16:56:57 -0700 | [diff] [blame] | 718 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 719 | at24_get_pdata(&client->dev, &chip); |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 720 | |
Kevin Hilman | 7274ec8 | 2009-04-02 16:56:57 -0700 | [diff] [blame] | 721 | chip.setup = NULL; |
| 722 | chip.context = NULL; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | if (!is_power_of_2(chip.byte_len)) |
| 726 | dev_warn(&client->dev, |
| 727 | "byte_len looks suspicious (no power of 2)!\n"); |
Wolfram Sang | 45efe84 | 2010-11-17 13:00:49 +0100 | [diff] [blame] | 728 | if (!chip.page_size) { |
| 729 | dev_err(&client->dev, "page_size must not be 0!\n"); |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 730 | return -EINVAL; |
Wolfram Sang | 45efe84 | 2010-11-17 13:00:49 +0100 | [diff] [blame] | 731 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 732 | if (!is_power_of_2(chip.page_size)) |
| 733 | dev_warn(&client->dev, |
| 734 | "page_size looks suspicious (no power of 2)!\n"); |
| 735 | |
Bartosz Golaszewski | 5478e47 | 2017-11-27 22:06:13 +0100 | [diff] [blame] | 736 | /* |
| 737 | * REVISIT: the size of the EUI-48 byte array is 6 in at24mac402, while |
| 738 | * the call to ilog2() in AT24_DEVICE_MAGIC() rounds it down to 4. |
| 739 | * |
| 740 | * Eventually we'll get rid of the magic values altoghether in favor of |
| 741 | * real structs, but for now just manually set the right size. |
| 742 | */ |
| 743 | if (chip.flags & AT24_FLAG_MAC && chip.byte_len == 4) |
| 744 | chip.byte_len = 6; |
| 745 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 746 | /* Use I2C operations unless we're stuck with SMBus extensions. */ |
| 747 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 748 | if (chip.flags & AT24_FLAG_ADDR16) |
| 749 | return -EPFNOSUPPORT; |
| 750 | |
Jean Delvare | 7aeb966 | 2010-05-21 18:40:57 +0200 | [diff] [blame] | 751 | if (i2c_check_functionality(client->adapter, |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 752 | I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { |
Jean Delvare | 7aeb966 | 2010-05-21 18:40:57 +0200 | [diff] [blame] | 753 | use_smbus = I2C_SMBUS_I2C_BLOCK_DATA; |
| 754 | } else if (i2c_check_functionality(client->adapter, |
| 755 | I2C_FUNC_SMBUS_READ_WORD_DATA)) { |
| 756 | use_smbus = I2C_SMBUS_WORD_DATA; |
| 757 | } else if (i2c_check_functionality(client->adapter, |
| 758 | I2C_FUNC_SMBUS_READ_BYTE_DATA)) { |
| 759 | use_smbus = I2C_SMBUS_BYTE_DATA; |
| 760 | } else { |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 761 | return -EPFNOSUPPORT; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 762 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 763 | |
Christian Gmeiner | a839ce6 | 2014-10-09 11:07:58 +0200 | [diff] [blame] | 764 | if (i2c_check_functionality(client->adapter, |
| 765 | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) { |
| 766 | use_smbus_write = I2C_SMBUS_I2C_BLOCK_DATA; |
| 767 | } else if (i2c_check_functionality(client->adapter, |
| 768 | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) { |
| 769 | use_smbus_write = I2C_SMBUS_BYTE_DATA; |
| 770 | chip.page_size = 1; |
| 771 | } |
| 772 | } |
| 773 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 774 | if (chip.flags & AT24_FLAG_TAKE8ADDR) |
| 775 | num_addresses = 8; |
| 776 | else |
| 777 | num_addresses = DIV_ROUND_UP(chip.byte_len, |
| 778 | (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256); |
| 779 | |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 780 | at24 = devm_kzalloc(&client->dev, sizeof(struct at24_data) + |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 781 | num_addresses * sizeof(struct i2c_client *), GFP_KERNEL); |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 782 | if (!at24) |
| 783 | return -ENOMEM; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 784 | |
| 785 | mutex_init(&at24->lock); |
| 786 | at24->use_smbus = use_smbus; |
Christian Gmeiner | a839ce6 | 2014-10-09 11:07:58 +0200 | [diff] [blame] | 787 | at24->use_smbus_write = use_smbus_write; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 788 | at24->chip = chip; |
| 789 | at24->num_addresses = num_addresses; |
| 790 | |
Bartosz Golaszewski | 0b81365 | 2016-06-06 10:48:54 +0200 | [diff] [blame] | 791 | if ((chip.flags & AT24_FLAG_SERIAL) && (chip.flags & AT24_FLAG_MAC)) { |
| 792 | dev_err(&client->dev, |
| 793 | "invalid device data - cannot have both AT24_FLAG_SERIAL & AT24_FLAG_MAC."); |
| 794 | return -EINVAL; |
| 795 | } |
| 796 | |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 797 | if (chip.flags & AT24_FLAG_SERIAL) { |
| 798 | at24->read_func = at24_eeprom_read_serial; |
Bartosz Golaszewski | 0b81365 | 2016-06-06 10:48:54 +0200 | [diff] [blame] | 799 | } else if (chip.flags & AT24_FLAG_MAC) { |
| 800 | at24->read_func = at24_eeprom_read_mac; |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 801 | } else { |
| 802 | at24->read_func = at24->use_smbus ? at24_eeprom_read_smbus |
| 803 | : at24_eeprom_read_i2c; |
| 804 | } |
| 805 | |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 806 | if (at24->use_smbus) { |
| 807 | if (at24->use_smbus_write == I2C_SMBUS_I2C_BLOCK_DATA) |
| 808 | at24->write_func = at24_eeprom_write_smbus_block; |
| 809 | else |
| 810 | at24->write_func = at24_eeprom_write_smbus_byte; |
| 811 | } else { |
| 812 | at24->write_func = at24_eeprom_write_i2c; |
| 813 | } |
Bartosz Golaszewski | 318aa9c | 2016-06-06 10:48:46 +0200 | [diff] [blame] | 814 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 815 | writable = !(chip.flags & AT24_FLAG_READONLY); |
| 816 | if (writable) { |
Christian Gmeiner | a839ce6 | 2014-10-09 11:07:58 +0200 | [diff] [blame] | 817 | if (!use_smbus || use_smbus_write) { |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 818 | |
| 819 | unsigned write_max = chip.page_size; |
| 820 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 821 | if (write_max > io_limit) |
| 822 | write_max = io_limit; |
| 823 | if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX) |
| 824 | write_max = I2C_SMBUS_BLOCK_MAX; |
| 825 | at24->write_max = write_max; |
| 826 | |
| 827 | /* buffer (data + address at the beginning) */ |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 828 | at24->writebuf = devm_kzalloc(&client->dev, |
| 829 | write_max + 2, GFP_KERNEL); |
| 830 | if (!at24->writebuf) |
| 831 | return -ENOMEM; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 832 | } else { |
| 833 | dev_warn(&client->dev, |
| 834 | "cannot write due to controller restrictions."); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | at24->client[0] = client; |
| 839 | |
| 840 | /* use dummy devices for multiple-address chips */ |
| 841 | for (i = 1; i < num_addresses; i++) { |
| 842 | at24->client[i] = i2c_new_dummy(client->adapter, |
| 843 | client->addr + i); |
| 844 | if (!at24->client[i]) { |
| 845 | dev_err(&client->dev, "address 0x%02x unavailable\n", |
| 846 | client->addr + i); |
| 847 | err = -EADDRINUSE; |
| 848 | goto err_clients; |
| 849 | } |
| 850 | } |
| 851 | |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 852 | i2c_set_clientdata(client, at24); |
| 853 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 854 | /* enable runtime pm */ |
| 855 | pm_runtime_set_active(&client->dev); |
| 856 | pm_runtime_enable(&client->dev); |
| 857 | |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 858 | /* |
| 859 | * Perform a one-byte test read to verify that the |
| 860 | * chip is functional. |
| 861 | */ |
| 862 | err = at24_read(at24, 0, &test_byte, 1); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 863 | pm_runtime_idle(&client->dev); |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 864 | if (err) { |
| 865 | err = -ENODEV; |
| 866 | goto err_clients; |
| 867 | } |
| 868 | |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 869 | at24->nvmem_config.name = dev_name(&client->dev); |
| 870 | at24->nvmem_config.dev = &client->dev; |
| 871 | at24->nvmem_config.read_only = !writable; |
| 872 | at24->nvmem_config.root_only = true; |
| 873 | at24->nvmem_config.owner = THIS_MODULE; |
| 874 | at24->nvmem_config.compat = true; |
| 875 | at24->nvmem_config.base_dev = &client->dev; |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 876 | at24->nvmem_config.reg_read = at24_read; |
| 877 | at24->nvmem_config.reg_write = at24_write; |
| 878 | at24->nvmem_config.priv = at24; |
David Lechner | 7f6d2ec | 2017-12-03 19:54:41 -0600 | [diff] [blame^] | 879 | at24->nvmem_config.stride = 1; |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 880 | at24->nvmem_config.word_size = 1; |
| 881 | at24->nvmem_config.size = chip.byte_len; |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 882 | |
| 883 | at24->nvmem = nvmem_register(&at24->nvmem_config); |
| 884 | |
| 885 | if (IS_ERR(at24->nvmem)) { |
| 886 | err = PTR_ERR(at24->nvmem); |
| 887 | goto err_clients; |
| 888 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 889 | |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 890 | dev_info(&client->dev, "%u byte %s EEPROM, %s, %u bytes/write\n", |
| 891 | chip.byte_len, client->name, |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 892 | writable ? "writable" : "read-only", at24->write_max); |
Jean Delvare | 7aeb966 | 2010-05-21 18:40:57 +0200 | [diff] [blame] | 893 | if (use_smbus == I2C_SMBUS_WORD_DATA || |
| 894 | use_smbus == I2C_SMBUS_BYTE_DATA) { |
| 895 | dev_notice(&client->dev, "Falling back to %s reads, " |
| 896 | "performance will suffer\n", use_smbus == |
| 897 | I2C_SMBUS_WORD_DATA ? "word" : "byte"); |
| 898 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 899 | |
Kevin Hilman | 7274ec8 | 2009-04-02 16:56:57 -0700 | [diff] [blame] | 900 | /* export data to kernel code */ |
| 901 | if (chip.setup) |
Andrew Lunn | bec3c11 | 2016-02-26 20:59:24 +0100 | [diff] [blame] | 902 | chip.setup(at24->nvmem, chip.context); |
Kevin Hilman | 7274ec8 | 2009-04-02 16:56:57 -0700 | [diff] [blame] | 903 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 904 | return 0; |
| 905 | |
| 906 | err_clients: |
| 907 | for (i = 1; i < num_addresses; i++) |
| 908 | if (at24->client[i]) |
| 909 | i2c_unregister_device(at24->client[i]); |
| 910 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 911 | pm_runtime_disable(&client->dev); |
| 912 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 913 | return err; |
| 914 | } |
| 915 | |
Bill Pemberton | 486a5c2 | 2012-11-19 13:26:02 -0500 | [diff] [blame] | 916 | static int at24_remove(struct i2c_client *client) |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 917 | { |
| 918 | struct at24_data *at24; |
| 919 | int i; |
| 920 | |
| 921 | at24 = i2c_get_clientdata(client); |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 922 | |
| 923 | nvmem_unregister(at24->nvmem); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 924 | |
| 925 | for (i = 1; i < at24->num_addresses; i++) |
| 926 | i2c_unregister_device(at24->client[i]); |
| 927 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 928 | pm_runtime_disable(&client->dev); |
| 929 | pm_runtime_set_suspended(&client->dev); |
| 930 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | /*-------------------------------------------------------------------------*/ |
| 935 | |
| 936 | static struct i2c_driver at24_driver = { |
| 937 | .driver = { |
| 938 | .name = "at24", |
Javier Martinez Canillas | 7f2a2f0 | 2017-10-01 12:49:48 +0200 | [diff] [blame] | 939 | .of_match_table = at24_of_match, |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 940 | .acpi_match_table = ACPI_PTR(at24_acpi_ids), |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 941 | }, |
| 942 | .probe = at24_probe, |
Bill Pemberton | 2d6bed9 | 2012-11-19 13:21:23 -0500 | [diff] [blame] | 943 | .remove = at24_remove, |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 944 | .id_table = at24_ids, |
| 945 | }; |
| 946 | |
| 947 | static int __init at24_init(void) |
| 948 | { |
Wolfram Sang | 45efe84 | 2010-11-17 13:00:49 +0100 | [diff] [blame] | 949 | if (!io_limit) { |
| 950 | pr_err("at24: io_limit must not be 0!\n"); |
| 951 | return -EINVAL; |
| 952 | } |
| 953 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 954 | io_limit = rounddown_pow_of_two(io_limit); |
| 955 | return i2c_add_driver(&at24_driver); |
| 956 | } |
| 957 | module_init(at24_init); |
| 958 | |
| 959 | static void __exit at24_exit(void) |
| 960 | { |
| 961 | i2c_del_driver(&at24_driver); |
| 962 | } |
| 963 | module_exit(at24_exit); |
| 964 | |
| 965 | MODULE_DESCRIPTION("Driver for most I2C EEPROMs"); |
| 966 | MODULE_AUTHOR("David Brownell and Wolfram Sang"); |
| 967 | MODULE_LICENSE("GPL"); |