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 | |
Bartosz Golaszewski | aa4ce22 | 2017-12-13 11:56:23 +0100 | [diff] [blame] | 73 | unsigned int write_max; |
| 74 | unsigned int 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 | */ |
Bartosz Golaszewski | ec3c2d5 | 2017-12-18 18:16:46 +0100 | [diff] [blame] | 96 | static unsigned int at24_io_limit = 128; |
| 97 | module_param_named(io_limit, at24_io_limit, uint, 0); |
| 98 | MODULE_PARM_DESC(at24_io_limit, "Maximum bytes per I/O (default 128)"); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 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 | */ |
Bartosz Golaszewski | ec3c2d5 | 2017-12-18 18:16:46 +0100 | [diff] [blame] | 104 | static unsigned int at24_write_timeout = 25; |
| 105 | module_param_named(write_timeout, at24_write_timeout, uint, 0); |
| 106 | MODULE_PARM_DESC(at24_write_timeout, "Time (in ms) to try writes (default 25)"); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 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 */ |
Bartosz Golaszewski | aa4ce22 | 2017-12-13 11:56:23 +0100 | [diff] [blame] | 114 | #define AT24_DEVICE_MAGIC(_len, _flags) \ |
| 115 | ((1 << AT24_SIZE_FLAGS | (_flags)) \ |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 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 | */ |
Bartosz Golaszewski | ec3c2d5 | 2017-12-18 18:16:46 +0100 | [diff] [blame] | 129 | #define at24_loop_until_timeout(tout, op_time) \ |
| 130 | for (tout = jiffies + msecs_to_jiffies(at24_write_timeout), \ |
| 131 | op_time = 0; \ |
Bartosz Golaszewski | 24da3cc | 2016-07-17 20:40:06 +0200 | [diff] [blame] | 132 | op_time ? time_before(op_time, tout) : true; \ |
Bartosz Golaszewski | 9344a81 | 2016-06-06 10:48:47 +0200 | [diff] [blame] | 133 | usleep_range(1000, 1500), op_time = jiffies) |
| 134 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 135 | static const struct i2c_device_id at24_ids[] = { |
| 136 | /* needs 8 addresses as A0-A2 are ignored */ |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 137 | { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) }, |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 138 | /* old variants can't be handled with this generic entry! */ |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 139 | { "24c01", AT24_DEVICE_MAGIC(1024 / 8, 0) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 140 | { "24cs01", AT24_DEVICE_MAGIC(16, |
| 141 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY) }, |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 142 | { "24c02", AT24_DEVICE_MAGIC(2048 / 8, 0) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 143 | { "24cs02", AT24_DEVICE_MAGIC(16, |
| 144 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY) }, |
Bartosz Golaszewski | 0b81365 | 2016-06-06 10:48:54 +0200 | [diff] [blame] | 145 | { "24mac402", AT24_DEVICE_MAGIC(48 / 8, |
| 146 | AT24_FLAG_MAC | AT24_FLAG_READONLY) }, |
| 147 | { "24mac602", AT24_DEVICE_MAGIC(64 / 8, |
| 148 | AT24_FLAG_MAC | AT24_FLAG_READONLY) }, |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 149 | /* spd is a 24c02 in memory DIMMs */ |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 150 | { "spd", AT24_DEVICE_MAGIC(2048 / 8, |
| 151 | AT24_FLAG_READONLY | AT24_FLAG_IRUGO) }, |
| 152 | { "24c04", AT24_DEVICE_MAGIC(4096 / 8, 0) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 153 | { "24cs04", AT24_DEVICE_MAGIC(16, |
| 154 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY) }, |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 155 | /* 24rf08 quirk is handled at i2c-core */ |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 156 | { "24c08", AT24_DEVICE_MAGIC(8192 / 8, 0) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 157 | { "24cs08", AT24_DEVICE_MAGIC(16, |
| 158 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY) }, |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 159 | { "24c16", AT24_DEVICE_MAGIC(16384 / 8, 0) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 160 | { "24cs16", AT24_DEVICE_MAGIC(16, |
| 161 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY) }, |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 162 | { "24c32", AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 163 | { "24cs32", AT24_DEVICE_MAGIC(16, |
| 164 | AT24_FLAG_ADDR16 | |
| 165 | AT24_FLAG_SERIAL | |
| 166 | AT24_FLAG_READONLY) }, |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 167 | { "24c64", AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 168 | { "24cs64", AT24_DEVICE_MAGIC(16, |
| 169 | AT24_FLAG_ADDR16 | |
| 170 | AT24_FLAG_SERIAL | |
| 171 | AT24_FLAG_READONLY) }, |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 172 | { "24c128", AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16) }, |
| 173 | { "24c256", AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16) }, |
| 174 | { "24c512", AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16) }, |
| 175 | { "24c1024", AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16) }, |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 176 | { "at24", 0 }, |
| 177 | { /* END OF LIST */ } |
| 178 | }; |
| 179 | MODULE_DEVICE_TABLE(i2c, at24_ids); |
| 180 | |
Javier Martinez Canillas | 7f2a2f0 | 2017-10-01 12:49:48 +0200 | [diff] [blame] | 181 | static const struct of_device_id at24_of_match[] = { |
| 182 | { |
| 183 | .compatible = "atmel,24c00", |
| 184 | .data = (void *)AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) |
| 185 | }, |
| 186 | { |
| 187 | .compatible = "atmel,24c01", |
| 188 | .data = (void *)AT24_DEVICE_MAGIC(1024 / 8, 0) |
| 189 | }, |
| 190 | { |
| 191 | .compatible = "atmel,24c02", |
| 192 | .data = (void *)AT24_DEVICE_MAGIC(2048 / 8, 0) |
| 193 | }, |
| 194 | { |
| 195 | .compatible = "atmel,spd", |
| 196 | .data = (void *)AT24_DEVICE_MAGIC(2048 / 8, |
| 197 | AT24_FLAG_READONLY | AT24_FLAG_IRUGO) |
| 198 | }, |
| 199 | { |
| 200 | .compatible = "atmel,24c04", |
| 201 | .data = (void *)AT24_DEVICE_MAGIC(4096 / 8, 0) |
| 202 | }, |
| 203 | { |
| 204 | .compatible = "atmel,24c08", |
| 205 | .data = (void *)AT24_DEVICE_MAGIC(8192 / 8, 0) |
| 206 | }, |
| 207 | { |
| 208 | .compatible = "atmel,24c16", |
| 209 | .data = (void *)AT24_DEVICE_MAGIC(16384 / 8, 0) |
| 210 | }, |
| 211 | { |
| 212 | .compatible = "atmel,24c32", |
| 213 | .data = (void *)AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) |
| 214 | }, |
| 215 | { |
| 216 | .compatible = "atmel,24c64", |
| 217 | .data = (void *)AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) |
| 218 | }, |
| 219 | { |
| 220 | .compatible = "atmel,24c128", |
| 221 | .data = (void *)AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16) |
| 222 | }, |
| 223 | { |
| 224 | .compatible = "atmel,24c256", |
| 225 | .data = (void *)AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16) |
| 226 | }, |
| 227 | { |
| 228 | .compatible = "atmel,24c512", |
| 229 | .data = (void *)AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16) |
| 230 | }, |
| 231 | { |
| 232 | .compatible = "atmel,24c1024", |
| 233 | .data = (void *)AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16) |
| 234 | }, |
| 235 | { }, |
| 236 | }; |
| 237 | MODULE_DEVICE_TABLE(of, at24_of_match); |
| 238 | |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 239 | static const struct acpi_device_id at24_acpi_ids[] = { |
| 240 | { "INT3499", AT24_DEVICE_MAGIC(8192 / 8, 0) }, |
| 241 | { } |
| 242 | }; |
| 243 | MODULE_DEVICE_TABLE(acpi, at24_acpi_ids); |
| 244 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 245 | /*-------------------------------------------------------------------------*/ |
| 246 | |
| 247 | /* |
| 248 | * This routine supports chips which consume multiple I2C addresses. It |
| 249 | * computes the addressing information to be used for a given r/w request. |
| 250 | * Assumes that sanity checks for offset happened at sysfs-layer. |
Bartosz Golaszewski | 9afd6866 | 2016-06-06 10:48:48 +0200 | [diff] [blame] | 251 | * |
| 252 | * Slave address and byte offset derive from the offset. Always |
| 253 | * set the byte address; on a multi-master board, another master |
| 254 | * may have changed the chip's "current" address pointer. |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 255 | */ |
Heiner Kallweit | 4604948 | 2017-11-28 21:51:42 +0100 | [diff] [blame] | 256 | static struct at24_client *at24_translate_offset(struct at24_data *at24, |
| 257 | unsigned int *offset) |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 258 | { |
Bartosz Golaszewski | aa4ce22 | 2017-12-13 11:56:23 +0100 | [diff] [blame] | 259 | unsigned int i; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 260 | |
| 261 | if (at24->chip.flags & AT24_FLAG_ADDR16) { |
| 262 | i = *offset >> 16; |
| 263 | *offset &= 0xffff; |
| 264 | } else { |
| 265 | i = *offset >> 8; |
| 266 | *offset &= 0xff; |
| 267 | } |
| 268 | |
Heiner Kallweit | 4604948 | 2017-11-28 21:51:42 +0100 | [diff] [blame] | 269 | return &at24->client[i]; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Sven Van Asbroeck | e32213f | 2017-12-08 11:28:30 -0500 | [diff] [blame] | 272 | static size_t at24_adjust_read_count(struct at24_data *at24, |
| 273 | unsigned int offset, size_t count) |
| 274 | { |
| 275 | unsigned int bits; |
| 276 | size_t remainder; |
| 277 | |
| 278 | /* |
| 279 | * In case of multi-address chips that don't rollover reads to |
| 280 | * the next slave address: truncate the count to the slave boundary, |
| 281 | * so that the read never straddles slaves. |
| 282 | */ |
| 283 | if (at24->chip.flags & AT24_FLAG_NO_RDROL) { |
| 284 | bits = (at24->chip.flags & AT24_FLAG_ADDR16) ? 16 : 8; |
| 285 | remainder = BIT(bits) - offset; |
| 286 | if (count > remainder) |
| 287 | count = remainder; |
| 288 | } |
| 289 | |
Bartosz Golaszewski | ec3c2d5 | 2017-12-18 18:16:46 +0100 | [diff] [blame] | 290 | if (count > at24_io_limit) |
| 291 | count = at24_io_limit; |
Sven Van Asbroeck | e32213f | 2017-12-08 11:28:30 -0500 | [diff] [blame] | 292 | |
| 293 | return count; |
| 294 | } |
| 295 | |
Heiner Kallweit | 4bb5c13c | 2017-11-28 21:51:50 +0100 | [diff] [blame] | 296 | static ssize_t at24_regmap_read(struct at24_data *at24, char *buf, |
| 297 | unsigned int offset, size_t count) |
| 298 | { |
| 299 | unsigned long timeout, read_time; |
| 300 | struct at24_client *at24_client; |
| 301 | struct i2c_client *client; |
| 302 | struct regmap *regmap; |
| 303 | int ret; |
| 304 | |
| 305 | at24_client = at24_translate_offset(at24, &offset); |
| 306 | regmap = at24_client->regmap; |
| 307 | client = at24_client->client; |
Sven Van Asbroeck | e32213f | 2017-12-08 11:28:30 -0500 | [diff] [blame] | 308 | count = at24_adjust_read_count(at24, offset, count); |
Heiner Kallweit | 4bb5c13c | 2017-11-28 21:51:50 +0100 | [diff] [blame] | 309 | |
| 310 | /* adjust offset for mac and serial read ops */ |
| 311 | offset += at24->offset_adj; |
| 312 | |
Bartosz Golaszewski | ec3c2d5 | 2017-12-18 18:16:46 +0100 | [diff] [blame] | 313 | at24_loop_until_timeout(timeout, read_time) { |
Heiner Kallweit | 4bb5c13c | 2017-11-28 21:51:50 +0100 | [diff] [blame] | 314 | ret = regmap_bulk_read(regmap, offset, buf, count); |
| 315 | dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n", |
| 316 | count, offset, ret, jiffies); |
| 317 | if (!ret) |
| 318 | return count; |
| 319 | } |
| 320 | |
| 321 | return -ETIMEDOUT; |
| 322 | } |
| 323 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 324 | /* |
| 325 | * Note that if the hardware write-protect pin is pulled high, the whole |
| 326 | * chip is normally write protected. But there are plenty of product |
| 327 | * variants here, including OTP fuses and partial chip protect. |
| 328 | * |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 329 | * We only use page mode writes; the alternative is sloooow. These routines |
| 330 | * write at most one page. |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 331 | */ |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 332 | |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 333 | static size_t at24_adjust_write_count(struct at24_data *at24, |
| 334 | unsigned int offset, size_t count) |
| 335 | { |
Bartosz Golaszewski | aa4ce22 | 2017-12-13 11:56:23 +0100 | [diff] [blame] | 336 | unsigned int next_page; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 337 | |
| 338 | /* write_max is at most a page */ |
| 339 | if (count > at24->write_max) |
| 340 | count = at24->write_max; |
| 341 | |
| 342 | /* Never roll over backwards, to the start of this page */ |
| 343 | next_page = roundup(offset + 1, at24->chip.page_size); |
| 344 | if (offset + count > next_page) |
| 345 | count = next_page - offset; |
| 346 | |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 347 | return count; |
| 348 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 349 | |
Heiner Kallweit | 8e5888e | 2017-11-28 21:51:45 +0100 | [diff] [blame] | 350 | static ssize_t at24_regmap_write(struct at24_data *at24, const char *buf, |
| 351 | unsigned int offset, size_t count) |
| 352 | { |
| 353 | unsigned long timeout, write_time; |
| 354 | struct at24_client *at24_client; |
| 355 | struct i2c_client *client; |
| 356 | struct regmap *regmap; |
| 357 | int ret; |
| 358 | |
| 359 | at24_client = at24_translate_offset(at24, &offset); |
| 360 | regmap = at24_client->regmap; |
| 361 | client = at24_client->client; |
| 362 | count = at24_adjust_write_count(at24, offset, count); |
| 363 | |
Bartosz Golaszewski | ec3c2d5 | 2017-12-18 18:16:46 +0100 | [diff] [blame] | 364 | at24_loop_until_timeout(timeout, write_time) { |
Heiner Kallweit | 8e5888e | 2017-11-28 21:51:45 +0100 | [diff] [blame] | 365 | ret = regmap_bulk_write(regmap, offset, buf, count); |
| 366 | dev_dbg(&client->dev, "write %zu@%d --> %d (%ld)\n", |
| 367 | count, offset, ret, jiffies); |
| 368 | if (!ret) |
| 369 | return count; |
| 370 | } |
| 371 | |
| 372 | return -ETIMEDOUT; |
| 373 | } |
| 374 | |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 375 | static int at24_read(void *priv, unsigned int off, void *val, size_t count) |
| 376 | { |
| 377 | struct at24_data *at24 = priv; |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 378 | struct device *dev = &at24->client[0].client->dev; |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 379 | char *buf = val; |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 380 | int ret; |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 381 | |
| 382 | if (unlikely(!count)) |
| 383 | return count; |
| 384 | |
Heiner Kallweit | d9bcd46 | 2017-11-24 07:47:50 +0100 | [diff] [blame] | 385 | if (off + count > at24->chip.byte_len) |
| 386 | return -EINVAL; |
| 387 | |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 388 | ret = pm_runtime_get_sync(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 389 | if (ret < 0) { |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 390 | pm_runtime_put_noidle(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 391 | return ret; |
| 392 | } |
| 393 | |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 394 | /* |
| 395 | * Read data from chip, protecting against concurrent updates |
| 396 | * from this host, but not from other I2C masters. |
| 397 | */ |
| 398 | mutex_lock(&at24->lock); |
| 399 | |
| 400 | while (count) { |
| 401 | int status; |
| 402 | |
Heiner Kallweit | 4bb5c13c | 2017-11-28 21:51:50 +0100 | [diff] [blame] | 403 | status = at24_regmap_read(at24, buf, off, count); |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 404 | if (status < 0) { |
| 405 | mutex_unlock(&at24->lock); |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 406 | pm_runtime_put(dev); |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 407 | return status; |
| 408 | } |
| 409 | buf += status; |
| 410 | off += status; |
| 411 | count -= status; |
| 412 | } |
| 413 | |
| 414 | mutex_unlock(&at24->lock); |
| 415 | |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 416 | pm_runtime_put(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 417 | |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 418 | return 0; |
| 419 | } |
| 420 | |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 421 | 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] | 422 | { |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 423 | struct at24_data *at24 = priv; |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 424 | struct device *dev = &at24->client[0].client->dev; |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 425 | char *buf = val; |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 426 | int ret; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 427 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 428 | if (unlikely(!count)) |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 429 | return -EINVAL; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 430 | |
Heiner Kallweit | d9bcd46 | 2017-11-24 07:47:50 +0100 | [diff] [blame] | 431 | if (off + count > at24->chip.byte_len) |
| 432 | return -EINVAL; |
| 433 | |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 434 | ret = pm_runtime_get_sync(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 435 | if (ret < 0) { |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 436 | pm_runtime_put_noidle(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 437 | return ret; |
| 438 | } |
| 439 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 440 | /* |
| 441 | * Write data to chip, protecting against concurrent updates |
| 442 | * from this host, but not from other I2C masters. |
| 443 | */ |
| 444 | mutex_lock(&at24->lock); |
| 445 | |
| 446 | while (count) { |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 447 | int status; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 448 | |
Heiner Kallweit | 8e5888e | 2017-11-28 21:51:45 +0100 | [diff] [blame] | 449 | status = at24_regmap_write(at24, buf, off, count); |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 450 | if (status < 0) { |
| 451 | mutex_unlock(&at24->lock); |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 452 | pm_runtime_put(dev); |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 453 | return status; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 454 | } |
| 455 | buf += status; |
| 456 | off += status; |
| 457 | count -= status; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | mutex_unlock(&at24->lock); |
| 461 | |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 462 | pm_runtime_put(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 463 | |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 464 | return 0; |
| 465 | } |
| 466 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 467 | 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] | 468 | { |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 469 | int err; |
| 470 | u32 val; |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 471 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 472 | if (device_property_present(dev, "read-only")) |
| 473 | chip->flags |= AT24_FLAG_READONLY; |
Sven Van Asbroeck | e32213f | 2017-12-08 11:28:30 -0500 | [diff] [blame] | 474 | if (device_property_present(dev, "no-read-rollover")) |
| 475 | chip->flags |= AT24_FLAG_NO_RDROL; |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 476 | |
Divagar Mohandass | dbc1ab9 | 2017-10-10 11:30:36 +0530 | [diff] [blame] | 477 | err = device_property_read_u32(dev, "size", &val); |
| 478 | if (!err) |
| 479 | chip->byte_len = val; |
| 480 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 481 | err = device_property_read_u32(dev, "pagesize", &val); |
| 482 | if (!err) { |
| 483 | chip->page_size = val; |
| 484 | } else { |
| 485 | /* |
| 486 | * This is slow, but we can't know all eeproms, so we better |
| 487 | * play safe. Specifying custom eeprom-types via platform_data |
| 488 | * is recommended anyhow. |
| 489 | */ |
| 490 | chip->page_size = 1; |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 491 | } |
| 492 | } |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 493 | |
Heiner Kallweit | 4bb5c13c | 2017-11-28 21:51:50 +0100 | [diff] [blame] | 494 | static unsigned int at24_get_offset_adj(u8 flags, unsigned int byte_len) |
| 495 | { |
| 496 | if (flags & AT24_FLAG_MAC) { |
| 497 | /* EUI-48 starts from 0x9a, EUI-64 from 0x98 */ |
| 498 | return 0xa0 - byte_len; |
| 499 | } else if (flags & AT24_FLAG_SERIAL && flags & AT24_FLAG_ADDR16) { |
| 500 | /* |
| 501 | * For 16 bit address pointers, the word address must contain |
| 502 | * a '10' sequence in bits 11 and 10 regardless of the |
| 503 | * intended position of the address pointer. |
| 504 | */ |
| 505 | return 0x0800; |
| 506 | } else if (flags & AT24_FLAG_SERIAL) { |
| 507 | /* |
| 508 | * Otherwise the word address must begin with a '10' sequence, |
| 509 | * regardless of the intended address. |
| 510 | */ |
| 511 | return 0x0080; |
| 512 | } else { |
| 513 | return 0; |
| 514 | } |
| 515 | } |
| 516 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 517 | static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) |
| 518 | { |
| 519 | struct at24_platform_data chip; |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 520 | kernel_ulong_t magic = 0; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 521 | bool writable; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 522 | struct at24_data *at24; |
| 523 | int err; |
Bartosz Golaszewski | aa4ce22 | 2017-12-13 11:56:23 +0100 | [diff] [blame] | 524 | unsigned int i, num_addresses; |
Bartosz Golaszewski | eef6939 | 2017-12-18 18:24:43 +0100 | [diff] [blame^] | 525 | struct regmap_config regmap_config = { }; |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 526 | u8 test_byte; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 527 | |
| 528 | if (client->dev.platform_data) { |
| 529 | chip = *(struct at24_platform_data *)client->dev.platform_data; |
| 530 | } else { |
Javier Martinez Canillas | 7f2a2f0 | 2017-10-01 12:49:48 +0200 | [diff] [blame] | 531 | /* |
| 532 | * The I2C core allows OF nodes compatibles to match against the |
| 533 | * I2C device ID table as a fallback, so check not only if an OF |
| 534 | * node is present but also if it matches an OF device ID entry. |
| 535 | */ |
| 536 | if (client->dev.of_node && |
| 537 | of_match_device(at24_of_match, &client->dev)) { |
| 538 | magic = (kernel_ulong_t) |
| 539 | of_device_get_match_data(&client->dev); |
| 540 | } else if (id) { |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 541 | magic = id->driver_data; |
| 542 | } else { |
| 543 | const struct acpi_device_id *aid; |
| 544 | |
| 545 | aid = acpi_match_device(at24_acpi_ids, &client->dev); |
| 546 | if (aid) |
| 547 | magic = aid->driver_data; |
| 548 | } |
| 549 | if (!magic) |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 550 | return -ENODEV; |
| 551 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 552 | chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN)); |
| 553 | magic >>= AT24_SIZE_BYTELEN; |
| 554 | chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS); |
Kevin Hilman | 7274ec8 | 2009-04-02 16:56:57 -0700 | [diff] [blame] | 555 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 556 | at24_get_pdata(&client->dev, &chip); |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 557 | |
Kevin Hilman | 7274ec8 | 2009-04-02 16:56:57 -0700 | [diff] [blame] | 558 | chip.setup = NULL; |
| 559 | chip.context = NULL; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | if (!is_power_of_2(chip.byte_len)) |
| 563 | dev_warn(&client->dev, |
| 564 | "byte_len looks suspicious (no power of 2)!\n"); |
Wolfram Sang | 45efe84 | 2010-11-17 13:00:49 +0100 | [diff] [blame] | 565 | if (!chip.page_size) { |
| 566 | dev_err(&client->dev, "page_size must not be 0!\n"); |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 567 | return -EINVAL; |
Wolfram Sang | 45efe84 | 2010-11-17 13:00:49 +0100 | [diff] [blame] | 568 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 569 | if (!is_power_of_2(chip.page_size)) |
| 570 | dev_warn(&client->dev, |
| 571 | "page_size looks suspicious (no power of 2)!\n"); |
| 572 | |
Bartosz Golaszewski | 5478e47 | 2017-11-27 22:06:13 +0100 | [diff] [blame] | 573 | /* |
| 574 | * REVISIT: the size of the EUI-48 byte array is 6 in at24mac402, while |
| 575 | * the call to ilog2() in AT24_DEVICE_MAGIC() rounds it down to 4. |
| 576 | * |
| 577 | * Eventually we'll get rid of the magic values altoghether in favor of |
| 578 | * real structs, but for now just manually set the right size. |
| 579 | */ |
| 580 | if (chip.flags & AT24_FLAG_MAC && chip.byte_len == 4) |
| 581 | chip.byte_len = 6; |
| 582 | |
Heiner Kallweit | a23727c | 2017-11-28 21:51:54 +0100 | [diff] [blame] | 583 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C) && |
| 584 | !i2c_check_functionality(client->adapter, |
| 585 | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) |
| 586 | chip.page_size = 1; |
Christian Gmeiner | a839ce6 | 2014-10-09 11:07:58 +0200 | [diff] [blame] | 587 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 588 | if (chip.flags & AT24_FLAG_TAKE8ADDR) |
| 589 | num_addresses = 8; |
| 590 | else |
| 591 | num_addresses = DIV_ROUND_UP(chip.byte_len, |
| 592 | (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256); |
| 593 | |
Bartosz Golaszewski | eef6939 | 2017-12-18 18:24:43 +0100 | [diff] [blame^] | 594 | regmap_config.val_bits = 8; |
| 595 | regmap_config.reg_bits = (chip.flags & AT24_FLAG_ADDR16) ? 16 : 8; |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 596 | |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 597 | at24 = devm_kzalloc(&client->dev, sizeof(struct at24_data) + |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 598 | num_addresses * sizeof(struct at24_client), GFP_KERNEL); |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 599 | if (!at24) |
| 600 | return -ENOMEM; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 601 | |
| 602 | mutex_init(&at24->lock); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 603 | at24->chip = chip; |
| 604 | at24->num_addresses = num_addresses; |
Heiner Kallweit | 4bb5c13c | 2017-11-28 21:51:50 +0100 | [diff] [blame] | 605 | at24->offset_adj = at24_get_offset_adj(chip.flags, chip.byte_len); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 606 | |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 607 | at24->client[0].client = client; |
Bartosz Golaszewski | eef6939 | 2017-12-18 18:24:43 +0100 | [diff] [blame^] | 608 | at24->client[0].regmap = devm_regmap_init_i2c(client, ®map_config); |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 609 | if (IS_ERR(at24->client[0].regmap)) |
| 610 | return PTR_ERR(at24->client[0].regmap); |
| 611 | |
Bartosz Golaszewski | 0b81365 | 2016-06-06 10:48:54 +0200 | [diff] [blame] | 612 | if ((chip.flags & AT24_FLAG_SERIAL) && (chip.flags & AT24_FLAG_MAC)) { |
| 613 | dev_err(&client->dev, |
| 614 | "invalid device data - cannot have both AT24_FLAG_SERIAL & AT24_FLAG_MAC."); |
| 615 | return -EINVAL; |
| 616 | } |
| 617 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 618 | writable = !(chip.flags & AT24_FLAG_READONLY); |
| 619 | if (writable) { |
Bartosz Golaszewski | ec3c2d5 | 2017-12-18 18:16:46 +0100 | [diff] [blame] | 620 | at24->write_max = min_t(unsigned int, |
| 621 | chip.page_size, at24_io_limit); |
Heiner Kallweit | a23727c | 2017-11-28 21:51:54 +0100 | [diff] [blame] | 622 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C) && |
| 623 | at24->write_max > I2C_SMBUS_BLOCK_MAX) |
| 624 | at24->write_max = I2C_SMBUS_BLOCK_MAX; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 625 | } |
| 626 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 627 | /* use dummy devices for multiple-address chips */ |
| 628 | for (i = 1; i < num_addresses; i++) { |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 629 | at24->client[i].client = i2c_new_dummy(client->adapter, |
| 630 | client->addr + i); |
| 631 | if (!at24->client[i].client) { |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 632 | dev_err(&client->dev, "address 0x%02x unavailable\n", |
| 633 | client->addr + i); |
| 634 | err = -EADDRINUSE; |
| 635 | goto err_clients; |
| 636 | } |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 637 | at24->client[i].regmap = devm_regmap_init_i2c( |
Bartosz Golaszewski | eef6939 | 2017-12-18 18:24:43 +0100 | [diff] [blame^] | 638 | at24->client[i].client, |
| 639 | ®map_config); |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 640 | if (IS_ERR(at24->client[i].regmap)) { |
| 641 | err = PTR_ERR(at24->client[i].regmap); |
| 642 | goto err_clients; |
| 643 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 644 | } |
| 645 | |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 646 | i2c_set_clientdata(client, at24); |
| 647 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 648 | /* enable runtime pm */ |
| 649 | pm_runtime_set_active(&client->dev); |
| 650 | pm_runtime_enable(&client->dev); |
| 651 | |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 652 | /* |
| 653 | * Perform a one-byte test read to verify that the |
| 654 | * chip is functional. |
| 655 | */ |
| 656 | err = at24_read(at24, 0, &test_byte, 1); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 657 | pm_runtime_idle(&client->dev); |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 658 | if (err) { |
| 659 | err = -ENODEV; |
| 660 | goto err_clients; |
| 661 | } |
| 662 | |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 663 | at24->nvmem_config.name = dev_name(&client->dev); |
| 664 | at24->nvmem_config.dev = &client->dev; |
| 665 | at24->nvmem_config.read_only = !writable; |
| 666 | at24->nvmem_config.root_only = true; |
| 667 | at24->nvmem_config.owner = THIS_MODULE; |
| 668 | at24->nvmem_config.compat = true; |
| 669 | at24->nvmem_config.base_dev = &client->dev; |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 670 | at24->nvmem_config.reg_read = at24_read; |
| 671 | at24->nvmem_config.reg_write = at24_write; |
| 672 | at24->nvmem_config.priv = at24; |
David Lechner | 7f6d2ec | 2017-12-03 19:54:41 -0600 | [diff] [blame] | 673 | at24->nvmem_config.stride = 1; |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 674 | at24->nvmem_config.word_size = 1; |
| 675 | at24->nvmem_config.size = chip.byte_len; |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 676 | |
| 677 | at24->nvmem = nvmem_register(&at24->nvmem_config); |
| 678 | |
| 679 | if (IS_ERR(at24->nvmem)) { |
| 680 | err = PTR_ERR(at24->nvmem); |
| 681 | goto err_clients; |
| 682 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 683 | |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 684 | dev_info(&client->dev, "%u byte %s EEPROM, %s, %u bytes/write\n", |
| 685 | chip.byte_len, client->name, |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 686 | writable ? "writable" : "read-only", at24->write_max); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 687 | |
Kevin Hilman | 7274ec8 | 2009-04-02 16:56:57 -0700 | [diff] [blame] | 688 | /* export data to kernel code */ |
| 689 | if (chip.setup) |
Andrew Lunn | bec3c11 | 2016-02-26 20:59:24 +0100 | [diff] [blame] | 690 | chip.setup(at24->nvmem, chip.context); |
Kevin Hilman | 7274ec8 | 2009-04-02 16:56:57 -0700 | [diff] [blame] | 691 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 692 | return 0; |
| 693 | |
| 694 | err_clients: |
| 695 | for (i = 1; i < num_addresses; i++) |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 696 | if (at24->client[i].client) |
| 697 | i2c_unregister_device(at24->client[i].client); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 698 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 699 | pm_runtime_disable(&client->dev); |
| 700 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 701 | return err; |
| 702 | } |
| 703 | |
Bill Pemberton | 486a5c2 | 2012-11-19 13:26:02 -0500 | [diff] [blame] | 704 | static int at24_remove(struct i2c_client *client) |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 705 | { |
| 706 | struct at24_data *at24; |
| 707 | int i; |
| 708 | |
| 709 | at24 = i2c_get_clientdata(client); |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 710 | |
| 711 | nvmem_unregister(at24->nvmem); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 712 | |
| 713 | for (i = 1; i < at24->num_addresses; i++) |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 714 | i2c_unregister_device(at24->client[i].client); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 715 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 716 | pm_runtime_disable(&client->dev); |
| 717 | pm_runtime_set_suspended(&client->dev); |
| 718 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | /*-------------------------------------------------------------------------*/ |
| 723 | |
| 724 | static struct i2c_driver at24_driver = { |
| 725 | .driver = { |
| 726 | .name = "at24", |
Javier Martinez Canillas | 7f2a2f0 | 2017-10-01 12:49:48 +0200 | [diff] [blame] | 727 | .of_match_table = at24_of_match, |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 728 | .acpi_match_table = ACPI_PTR(at24_acpi_ids), |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 729 | }, |
| 730 | .probe = at24_probe, |
Bill Pemberton | 2d6bed9 | 2012-11-19 13:21:23 -0500 | [diff] [blame] | 731 | .remove = at24_remove, |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 732 | .id_table = at24_ids, |
| 733 | }; |
| 734 | |
| 735 | static int __init at24_init(void) |
| 736 | { |
Bartosz Golaszewski | ec3c2d5 | 2017-12-18 18:16:46 +0100 | [diff] [blame] | 737 | if (!at24_io_limit) { |
| 738 | pr_err("at24: at24_io_limit must not be 0!\n"); |
Wolfram Sang | 45efe84 | 2010-11-17 13:00:49 +0100 | [diff] [blame] | 739 | return -EINVAL; |
| 740 | } |
| 741 | |
Bartosz Golaszewski | ec3c2d5 | 2017-12-18 18:16:46 +0100 | [diff] [blame] | 742 | at24_io_limit = rounddown_pow_of_two(at24_io_limit); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 743 | return i2c_add_driver(&at24_driver); |
| 744 | } |
| 745 | module_init(at24_init); |
| 746 | |
| 747 | static void __exit at24_exit(void) |
| 748 | { |
| 749 | i2c_del_driver(&at24_driver); |
| 750 | } |
| 751 | module_exit(at24_exit); |
| 752 | |
| 753 | MODULE_DESCRIPTION("Driver for most I2C EEPROMs"); |
| 754 | MODULE_AUTHOR("David Brownell and Wolfram Sang"); |
| 755 | MODULE_LICENSE("GPL"); |