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> |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 27 | #include <linux/regmap.h> |
Vivien Didelot | 25f73ed | 2013-09-27 15:06:28 -0400 | [diff] [blame] | 28 | #include <linux/platform_data/at24.h> |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 29 | #include <linux/pm_runtime.h> |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 30 | |
| 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 Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 59 | struct at24_client { |
| 60 | struct i2c_client *client; |
| 61 | struct regmap *regmap; |
| 62 | }; |
| 63 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 64 | struct at24_data { |
| 65 | struct at24_platform_data chip; |
Bartosz Golaszewski | 318aa9c | 2016-06-06 10:48:46 +0200 | [diff] [blame] | 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 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 73 | unsigned write_max; |
| 74 | unsigned num_addresses; |
Heiner Kallweit | 4bb5c13c | 2017-11-28 21:51:50 +0100 | [diff] [blame] | 75 | unsigned int offset_adj; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 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 | */ |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 84 | struct at24_client client[]; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 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 | */ |
Heiner Kallweit | 4604948 | 2017-11-28 21:51:42 +0100 | [diff] [blame] | 264 | static struct at24_client *at24_translate_offset(struct at24_data *at24, |
| 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 | |
Heiner Kallweit | 4604948 | 2017-11-28 21:51:42 +0100 | [diff] [blame] | 277 | return &at24->client[i]; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Heiner Kallweit | 4bb5c13c | 2017-11-28 21:51:50 +0100 | [diff] [blame] | 280 | static 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 Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 310 | /* |
| 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 Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 315 | * We only use page mode writes; the alternative is sloooow. These routines |
| 316 | * write at most one page. |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 317 | */ |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 318 | |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 319 | static size_t at24_adjust_write_count(struct at24_data *at24, |
| 320 | unsigned int offset, size_t count) |
| 321 | { |
| 322 | unsigned next_page; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 323 | |
| 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 Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 333 | return count; |
| 334 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 335 | |
Heiner Kallweit | 8e5888e | 2017-11-28 21:51:45 +0100 | [diff] [blame] | 336 | static 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 Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 361 | static int at24_read(void *priv, unsigned int off, void *val, size_t count) |
| 362 | { |
| 363 | struct at24_data *at24 = priv; |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 364 | struct device *dev = &at24->client[0].client->dev; |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 365 | char *buf = val; |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 366 | int ret; |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 367 | |
| 368 | if (unlikely(!count)) |
| 369 | return count; |
| 370 | |
Heiner Kallweit | d9bcd46 | 2017-11-24 07:47:50 +0100 | [diff] [blame] | 371 | if (off + count > at24->chip.byte_len) |
| 372 | return -EINVAL; |
| 373 | |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 374 | ret = pm_runtime_get_sync(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 375 | if (ret < 0) { |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 376 | pm_runtime_put_noidle(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 377 | return ret; |
| 378 | } |
| 379 | |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 380 | /* |
| 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 Kallweit | 4bb5c13c | 2017-11-28 21:51:50 +0100 | [diff] [blame] | 389 | status = at24_regmap_read(at24, buf, off, count); |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 390 | if (status < 0) { |
| 391 | mutex_unlock(&at24->lock); |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 392 | pm_runtime_put(dev); |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 393 | return status; |
| 394 | } |
| 395 | buf += status; |
| 396 | off += status; |
| 397 | count -= status; |
| 398 | } |
| 399 | |
| 400 | mutex_unlock(&at24->lock); |
| 401 | |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 402 | pm_runtime_put(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 403 | |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 404 | return 0; |
| 405 | } |
| 406 | |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 407 | 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] | 408 | { |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 409 | struct at24_data *at24 = priv; |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 410 | struct device *dev = &at24->client[0].client->dev; |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 411 | char *buf = val; |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 412 | int ret; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 413 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 414 | if (unlikely(!count)) |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 415 | return -EINVAL; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 416 | |
Heiner Kallweit | d9bcd46 | 2017-11-24 07:47:50 +0100 | [diff] [blame] | 417 | if (off + count > at24->chip.byte_len) |
| 418 | return -EINVAL; |
| 419 | |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 420 | ret = pm_runtime_get_sync(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 421 | if (ret < 0) { |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 422 | pm_runtime_put_noidle(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 423 | return ret; |
| 424 | } |
| 425 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 426 | /* |
| 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 Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 433 | int status; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 434 | |
Heiner Kallweit | 8e5888e | 2017-11-28 21:51:45 +0100 | [diff] [blame] | 435 | status = at24_regmap_write(at24, buf, off, count); |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 436 | if (status < 0) { |
| 437 | mutex_unlock(&at24->lock); |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 438 | pm_runtime_put(dev); |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 439 | return status; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 440 | } |
| 441 | buf += status; |
| 442 | off += status; |
| 443 | count -= status; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | mutex_unlock(&at24->lock); |
| 447 | |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 448 | pm_runtime_put(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 449 | |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 450 | return 0; |
| 451 | } |
| 452 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 453 | 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] | 454 | { |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 455 | int err; |
| 456 | u32 val; |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 457 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 458 | if (device_property_present(dev, "read-only")) |
| 459 | chip->flags |= AT24_FLAG_READONLY; |
| 460 | |
Divagar Mohandass | dbc1ab9 | 2017-10-10 11:30:36 +0530 | [diff] [blame] | 461 | err = device_property_read_u32(dev, "size", &val); |
| 462 | if (!err) |
| 463 | chip->byte_len = val; |
| 464 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 465 | 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 Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 475 | } |
| 476 | } |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 477 | |
Heiner Kallweit | 4bb5c13c | 2017-11-28 21:51:50 +0100 | [diff] [blame] | 478 | static 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 Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 501 | static const struct regmap_config regmap_config_8 = { |
| 502 | .reg_bits = 8, |
| 503 | .val_bits = 8, |
| 504 | }; |
| 505 | |
| 506 | static const struct regmap_config regmap_config_16 = { |
| 507 | .reg_bits = 16, |
| 508 | .val_bits = 8, |
| 509 | }; |
| 510 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 511 | static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) |
| 512 | { |
| 513 | struct at24_platform_data chip; |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 514 | kernel_ulong_t magic = 0; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 515 | bool writable; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 516 | struct at24_data *at24; |
| 517 | int err; |
| 518 | unsigned i, num_addresses; |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 519 | const struct regmap_config *config; |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 520 | u8 test_byte; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 521 | |
| 522 | if (client->dev.platform_data) { |
| 523 | chip = *(struct at24_platform_data *)client->dev.platform_data; |
| 524 | } else { |
Javier Martinez Canillas | 7f2a2f0 | 2017-10-01 12:49:48 +0200 | [diff] [blame] | 525 | /* |
| 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 Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 535 | 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 Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 544 | return -ENODEV; |
| 545 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 546 | 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 Hilman | 7274ec8 | 2009-04-02 16:56:57 -0700 | [diff] [blame] | 549 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 550 | at24_get_pdata(&client->dev, &chip); |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 551 | |
Kevin Hilman | 7274ec8 | 2009-04-02 16:56:57 -0700 | [diff] [blame] | 552 | chip.setup = NULL; |
| 553 | chip.context = NULL; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 554 | } |
| 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 Sang | 45efe84 | 2010-11-17 13:00:49 +0100 | [diff] [blame] | 559 | if (!chip.page_size) { |
| 560 | dev_err(&client->dev, "page_size must not be 0!\n"); |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 561 | return -EINVAL; |
Wolfram Sang | 45efe84 | 2010-11-17 13:00:49 +0100 | [diff] [blame] | 562 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 563 | 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 Golaszewski | 5478e47 | 2017-11-27 22:06:13 +0100 | [diff] [blame] | 567 | /* |
| 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 Kallweit | a23727c | 2017-11-28 21:51:54 +0100 | [diff] [blame^] | 577 | 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 Gmeiner | a839ce6 | 2014-10-09 11:07:58 +0200 | [diff] [blame] | 581 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 582 | 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 Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 588 | if (chip.flags & AT24_FLAG_ADDR16) |
| 589 | config = ®map_config_16; |
| 590 | else |
| 591 | config = ®map_config_8; |
| 592 | |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 593 | at24 = devm_kzalloc(&client->dev, sizeof(struct at24_data) + |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 594 | num_addresses * sizeof(struct at24_client), GFP_KERNEL); |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 595 | if (!at24) |
| 596 | return -ENOMEM; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 597 | |
| 598 | mutex_init(&at24->lock); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 599 | at24->chip = chip; |
| 600 | at24->num_addresses = num_addresses; |
Heiner Kallweit | 4bb5c13c | 2017-11-28 21:51:50 +0100 | [diff] [blame] | 601 | at24->offset_adj = at24_get_offset_adj(chip.flags, chip.byte_len); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 602 | |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 603 | 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 Golaszewski | 0b81365 | 2016-06-06 10:48:54 +0200 | [diff] [blame] | 608 | 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 Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 614 | writable = !(chip.flags & AT24_FLAG_READONLY); |
| 615 | if (writable) { |
Heiner Kallweit | a23727c | 2017-11-28 21:51:54 +0100 | [diff] [blame^] | 616 | 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 Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 620 | } |
| 621 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 622 | /* use dummy devices for multiple-address chips */ |
| 623 | for (i = 1; i < num_addresses; i++) { |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 624 | at24->client[i].client = i2c_new_dummy(client->adapter, |
| 625 | client->addr + i); |
| 626 | if (!at24->client[i].client) { |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 627 | dev_err(&client->dev, "address 0x%02x unavailable\n", |
| 628 | client->addr + i); |
| 629 | err = -EADDRINUSE; |
| 630 | goto err_clients; |
| 631 | } |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 632 | 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 Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 638 | } |
| 639 | |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 640 | i2c_set_clientdata(client, at24); |
| 641 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 642 | /* enable runtime pm */ |
| 643 | pm_runtime_set_active(&client->dev); |
| 644 | pm_runtime_enable(&client->dev); |
| 645 | |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 646 | /* |
| 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 Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 651 | pm_runtime_idle(&client->dev); |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 652 | if (err) { |
| 653 | err = -ENODEV; |
| 654 | goto err_clients; |
| 655 | } |
| 656 | |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 657 | 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 Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 664 | at24->nvmem_config.reg_read = at24_read; |
| 665 | at24->nvmem_config.reg_write = at24_write; |
| 666 | at24->nvmem_config.priv = at24; |
David Lechner | 7f6d2ec | 2017-12-03 19:54:41 -0600 | [diff] [blame] | 667 | at24->nvmem_config.stride = 1; |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 668 | at24->nvmem_config.word_size = 1; |
| 669 | at24->nvmem_config.size = chip.byte_len; |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 670 | |
| 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 Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 677 | |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 678 | dev_info(&client->dev, "%u byte %s EEPROM, %s, %u bytes/write\n", |
| 679 | chip.byte_len, client->name, |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 680 | writable ? "writable" : "read-only", at24->write_max); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 681 | |
Kevin Hilman | 7274ec8 | 2009-04-02 16:56:57 -0700 | [diff] [blame] | 682 | /* export data to kernel code */ |
| 683 | if (chip.setup) |
Andrew Lunn | bec3c11 | 2016-02-26 20:59:24 +0100 | [diff] [blame] | 684 | chip.setup(at24->nvmem, chip.context); |
Kevin Hilman | 7274ec8 | 2009-04-02 16:56:57 -0700 | [diff] [blame] | 685 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 686 | return 0; |
| 687 | |
| 688 | err_clients: |
| 689 | for (i = 1; i < num_addresses; i++) |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 690 | if (at24->client[i].client) |
| 691 | i2c_unregister_device(at24->client[i].client); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 692 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 693 | pm_runtime_disable(&client->dev); |
| 694 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 695 | return err; |
| 696 | } |
| 697 | |
Bill Pemberton | 486a5c2 | 2012-11-19 13:26:02 -0500 | [diff] [blame] | 698 | static int at24_remove(struct i2c_client *client) |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 699 | { |
| 700 | struct at24_data *at24; |
| 701 | int i; |
| 702 | |
| 703 | at24 = i2c_get_clientdata(client); |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 704 | |
| 705 | nvmem_unregister(at24->nvmem); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 706 | |
| 707 | for (i = 1; i < at24->num_addresses; i++) |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 708 | i2c_unregister_device(at24->client[i].client); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 709 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 710 | pm_runtime_disable(&client->dev); |
| 711 | pm_runtime_set_suspended(&client->dev); |
| 712 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | /*-------------------------------------------------------------------------*/ |
| 717 | |
| 718 | static struct i2c_driver at24_driver = { |
| 719 | .driver = { |
| 720 | .name = "at24", |
Javier Martinez Canillas | 7f2a2f0 | 2017-10-01 12:49:48 +0200 | [diff] [blame] | 721 | .of_match_table = at24_of_match, |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 722 | .acpi_match_table = ACPI_PTR(at24_acpi_ids), |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 723 | }, |
| 724 | .probe = at24_probe, |
Bill Pemberton | 2d6bed9 | 2012-11-19 13:21:23 -0500 | [diff] [blame] | 725 | .remove = at24_remove, |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 726 | .id_table = at24_ids, |
| 727 | }; |
| 728 | |
| 729 | static int __init at24_init(void) |
| 730 | { |
Wolfram Sang | 45efe84 | 2010-11-17 13:00:49 +0100 | [diff] [blame] | 731 | if (!io_limit) { |
| 732 | pr_err("at24: io_limit must not be 0!\n"); |
| 733 | return -EINVAL; |
| 734 | } |
| 735 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 736 | io_limit = rounddown_pow_of_two(io_limit); |
| 737 | return i2c_add_driver(&at24_driver); |
| 738 | } |
| 739 | module_init(at24_init); |
| 740 | |
| 741 | static void __exit at24_exit(void) |
| 742 | { |
| 743 | i2c_del_driver(&at24_driver); |
| 744 | } |
| 745 | module_exit(at24_exit); |
| 746 | |
| 747 | MODULE_DESCRIPTION("Driver for most I2C EEPROMs"); |
| 748 | MODULE_AUTHOR("David Brownell and Wolfram Sang"); |
| 749 | MODULE_LICENSE("GPL"); |