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> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/mutex.h> |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 18 | #include <linux/mod_devicetable.h> |
| 19 | #include <linux/log2.h> |
| 20 | #include <linux/bitops.h> |
| 21 | #include <linux/jiffies.h> |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 22 | #include <linux/property.h> |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 23 | #include <linux/acpi.h> |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 24 | #include <linux/i2c.h> |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 25 | #include <linux/nvmem-provider.h> |
Vivien Didelot | 25f73ed | 2013-09-27 15:06:28 -0400 | [diff] [blame] | 26 | #include <linux/platform_data/at24.h> |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame^] | 27 | #include <linux/pm_runtime.h> |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * I2C EEPROMs from most vendors are inexpensive and mostly interchangeable. |
| 31 | * Differences between different vendor product lines (like Atmel AT24C or |
| 32 | * MicroChip 24LC, etc) won't much matter for typical read/write access. |
| 33 | * There are also I2C RAM chips, likewise interchangeable. One example |
| 34 | * would be the PCF8570, which acts like a 24c02 EEPROM (256 bytes). |
| 35 | * |
| 36 | * However, misconfiguration can lose data. "Set 16-bit memory address" |
| 37 | * to a part with 8-bit addressing will overwrite data. Writing with too |
| 38 | * big a page size also loses data. And it's not safe to assume that the |
| 39 | * conventional addresses 0x50..0x57 only hold eeproms; a PCF8563 RTC |
| 40 | * uses 0x51, for just one example. |
| 41 | * |
| 42 | * Accordingly, explicit board-specific configuration data should be used |
| 43 | * in almost all cases. (One partial exception is an SMBus used to access |
| 44 | * "SPD" data for DRAM sticks. Those only use 24c02 EEPROMs.) |
| 45 | * |
| 46 | * So this driver uses "new style" I2C driver binding, expecting to be |
| 47 | * told what devices exist. That may be in arch/X/mach-Y/board-Z.c or |
| 48 | * similar kernel-resident tables; or, configuration data coming from |
| 49 | * a bootloader. |
| 50 | * |
| 51 | * Other than binding model, current differences from "eeprom" driver are |
| 52 | * that this one handles write access and isn't restricted to 24c02 devices. |
| 53 | * It also handles larger devices (32 kbit and up) with two-byte addresses, |
| 54 | * which won't work on pure SMBus systems. |
| 55 | */ |
| 56 | |
| 57 | struct at24_data { |
| 58 | struct at24_platform_data chip; |
Jean Delvare | 7aeb966 | 2010-05-21 18:40:57 +0200 | [diff] [blame] | 59 | int use_smbus; |
Christian Gmeiner | a839ce6 | 2014-10-09 11:07:58 +0200 | [diff] [blame] | 60 | int use_smbus_write; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 61 | |
Bartosz Golaszewski | 318aa9c | 2016-06-06 10:48:46 +0200 | [diff] [blame] | 62 | ssize_t (*read_func)(struct at24_data *, char *, unsigned int, size_t); |
| 63 | ssize_t (*write_func)(struct at24_data *, |
| 64 | const char *, unsigned int, size_t); |
| 65 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 66 | /* |
| 67 | * Lock protects against activities from other Linux tasks, |
| 68 | * but not from changes by other I2C masters. |
| 69 | */ |
| 70 | struct mutex lock; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 71 | |
| 72 | u8 *writebuf; |
| 73 | unsigned write_max; |
| 74 | unsigned num_addresses; |
| 75 | |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 76 | struct nvmem_config nvmem_config; |
| 77 | struct nvmem_device *nvmem; |
| 78 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 79 | /* |
| 80 | * Some chips tie up multiple I2C addresses; dummy devices reserve |
| 81 | * them for us, and we'll use them with SMBus calls. |
| 82 | */ |
| 83 | struct i2c_client *client[]; |
| 84 | }; |
| 85 | |
| 86 | /* |
| 87 | * This parameter is to help this driver avoid blocking other drivers out |
| 88 | * of I2C for potentially troublesome amounts of time. With a 100 kHz I2C |
| 89 | * clock, one 256 byte read takes about 1/43 second which is excessive; |
| 90 | * but the 1/170 second it takes at 400 kHz may be quite reasonable; and |
| 91 | * at 1 MHz (Fm+) a 1/430 second delay could easily be invisible. |
| 92 | * |
| 93 | * This value is forced to be a power of two so that writes align on pages. |
| 94 | */ |
| 95 | static unsigned io_limit = 128; |
| 96 | module_param(io_limit, uint, 0); |
| 97 | MODULE_PARM_DESC(io_limit, "Maximum bytes per I/O (default 128)"); |
| 98 | |
| 99 | /* |
| 100 | * Specs often allow 5 msec for a page write, sometimes 20 msec; |
| 101 | * it's important to recover from write timeouts. |
| 102 | */ |
| 103 | static unsigned write_timeout = 25; |
| 104 | module_param(write_timeout, uint, 0); |
| 105 | MODULE_PARM_DESC(write_timeout, "Time (in ms) to try writes (default 25)"); |
| 106 | |
| 107 | #define AT24_SIZE_BYTELEN 5 |
| 108 | #define AT24_SIZE_FLAGS 8 |
| 109 | |
| 110 | #define AT24_BITMASK(x) (BIT(x) - 1) |
| 111 | |
| 112 | /* create non-zero magic value for given eeprom parameters */ |
| 113 | #define AT24_DEVICE_MAGIC(_len, _flags) \ |
| 114 | ((1 << AT24_SIZE_FLAGS | (_flags)) \ |
| 115 | << AT24_SIZE_BYTELEN | ilog2(_len)) |
| 116 | |
Bartosz Golaszewski | 9344a81 | 2016-06-06 10:48:47 +0200 | [diff] [blame] | 117 | /* |
| 118 | * Both reads and writes fail if the previous write didn't complete yet. This |
| 119 | * 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] | 120 | * write to work while making sure that at least one iteration is run before |
| 121 | * checking the break condition. |
Bartosz Golaszewski | 9344a81 | 2016-06-06 10:48:47 +0200 | [diff] [blame] | 122 | * |
| 123 | * It takes two parameters: a variable in which the future timeout in jiffies |
| 124 | * will be stored and a temporary variable holding the time of the last |
| 125 | * iteration of processing the request. Both should be unsigned integers |
| 126 | * holding at least 32 bits. |
| 127 | */ |
| 128 | #define loop_until_timeout(tout, op_time) \ |
Bartosz Golaszewski | 24da3cc | 2016-07-17 20:40:06 +0200 | [diff] [blame] | 129 | for (tout = jiffies + msecs_to_jiffies(write_timeout), op_time = 0; \ |
| 130 | op_time ? time_before(op_time, tout) : true; \ |
Bartosz Golaszewski | 9344a81 | 2016-06-06 10:48:47 +0200 | [diff] [blame] | 131 | usleep_range(1000, 1500), op_time = jiffies) |
| 132 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 133 | static const struct i2c_device_id at24_ids[] = { |
| 134 | /* needs 8 addresses as A0-A2 are ignored */ |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 135 | { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) }, |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 136 | /* old variants can't be handled with this generic entry! */ |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 137 | { "24c01", AT24_DEVICE_MAGIC(1024 / 8, 0) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 138 | { "24cs01", AT24_DEVICE_MAGIC(16, |
| 139 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY) }, |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 140 | { "24c02", AT24_DEVICE_MAGIC(2048 / 8, 0) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 141 | { "24cs02", AT24_DEVICE_MAGIC(16, |
| 142 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY) }, |
Bartosz Golaszewski | 0b81365 | 2016-06-06 10:48:54 +0200 | [diff] [blame] | 143 | { "24mac402", AT24_DEVICE_MAGIC(48 / 8, |
| 144 | AT24_FLAG_MAC | AT24_FLAG_READONLY) }, |
| 145 | { "24mac602", AT24_DEVICE_MAGIC(64 / 8, |
| 146 | AT24_FLAG_MAC | AT24_FLAG_READONLY) }, |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 147 | /* spd is a 24c02 in memory DIMMs */ |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 148 | { "spd", AT24_DEVICE_MAGIC(2048 / 8, |
| 149 | AT24_FLAG_READONLY | AT24_FLAG_IRUGO) }, |
| 150 | { "24c04", AT24_DEVICE_MAGIC(4096 / 8, 0) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 151 | { "24cs04", AT24_DEVICE_MAGIC(16, |
| 152 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY) }, |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 153 | /* 24rf08 quirk is handled at i2c-core */ |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 154 | { "24c08", AT24_DEVICE_MAGIC(8192 / 8, 0) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 155 | { "24cs08", AT24_DEVICE_MAGIC(16, |
| 156 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY) }, |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 157 | { "24c16", AT24_DEVICE_MAGIC(16384 / 8, 0) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 158 | { "24cs16", AT24_DEVICE_MAGIC(16, |
| 159 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY) }, |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 160 | { "24c32", AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 161 | { "24cs32", AT24_DEVICE_MAGIC(16, |
| 162 | AT24_FLAG_ADDR16 | |
| 163 | AT24_FLAG_SERIAL | |
| 164 | AT24_FLAG_READONLY) }, |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 165 | { "24c64", AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) }, |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 166 | { "24cs64", AT24_DEVICE_MAGIC(16, |
| 167 | AT24_FLAG_ADDR16 | |
| 168 | AT24_FLAG_SERIAL | |
| 169 | AT24_FLAG_READONLY) }, |
Bartosz Golaszewski | b0b9f7b | 2016-06-06 10:48:43 +0200 | [diff] [blame] | 170 | { "24c128", AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16) }, |
| 171 | { "24c256", AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16) }, |
| 172 | { "24c512", AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16) }, |
| 173 | { "24c1024", AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16) }, |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 174 | { "at24", 0 }, |
| 175 | { /* END OF LIST */ } |
| 176 | }; |
| 177 | MODULE_DEVICE_TABLE(i2c, at24_ids); |
| 178 | |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 179 | static const struct acpi_device_id at24_acpi_ids[] = { |
| 180 | { "INT3499", AT24_DEVICE_MAGIC(8192 / 8, 0) }, |
| 181 | { } |
| 182 | }; |
| 183 | MODULE_DEVICE_TABLE(acpi, at24_acpi_ids); |
| 184 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 185 | /*-------------------------------------------------------------------------*/ |
| 186 | |
| 187 | /* |
| 188 | * This routine supports chips which consume multiple I2C addresses. It |
| 189 | * computes the addressing information to be used for a given r/w request. |
| 190 | * Assumes that sanity checks for offset happened at sysfs-layer. |
Bartosz Golaszewski | 9afd6866 | 2016-06-06 10:48:48 +0200 | [diff] [blame] | 191 | * |
| 192 | * Slave address and byte offset derive from the offset. Always |
| 193 | * set the byte address; on a multi-master board, another master |
| 194 | * may have changed the chip's "current" address pointer. |
| 195 | * |
| 196 | * REVISIT some multi-address chips don't rollover page reads to |
| 197 | * the next slave address, so we may need to truncate the count. |
| 198 | * Those chips might need another quirk flag. |
| 199 | * |
| 200 | * If the real hardware used four adjacent 24c02 chips and that |
| 201 | * were misconfigured as one 24c08, that would be a similar effect: |
| 202 | * one "eeprom" file not four, but larger reads would fail when |
| 203 | * they crossed certain pages. |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 204 | */ |
| 205 | static struct i2c_client *at24_translate_offset(struct at24_data *at24, |
Bartosz Golaszewski | 2da78ac | 2016-06-06 10:48:45 +0200 | [diff] [blame] | 206 | unsigned int *offset) |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 207 | { |
| 208 | unsigned i; |
| 209 | |
| 210 | if (at24->chip.flags & AT24_FLAG_ADDR16) { |
| 211 | i = *offset >> 16; |
| 212 | *offset &= 0xffff; |
| 213 | } else { |
| 214 | i = *offset >> 8; |
| 215 | *offset &= 0xff; |
| 216 | } |
| 217 | |
| 218 | return at24->client[i]; |
| 219 | } |
| 220 | |
Bartosz Golaszewski | 9afd6866 | 2016-06-06 10:48:48 +0200 | [diff] [blame] | 221 | static ssize_t at24_eeprom_read_smbus(struct at24_data *at24, char *buf, |
| 222 | unsigned int offset, size_t count) |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 223 | { |
Wolfram Sang | 4d29196 | 2009-11-26 09:22:33 +0100 | [diff] [blame] | 224 | unsigned long timeout, read_time; |
Bartosz Golaszewski | 9afd6866 | 2016-06-06 10:48:48 +0200 | [diff] [blame] | 225 | struct i2c_client *client; |
| 226 | int status; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 227 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 228 | client = at24_translate_offset(at24, &offset); |
| 229 | |
| 230 | if (count > io_limit) |
| 231 | count = io_limit; |
| 232 | |
Bartosz Golaszewski | 9afd6866 | 2016-06-06 10:48:48 +0200 | [diff] [blame] | 233 | /* Smaller eeproms can work given some SMBus extension calls */ |
| 234 | if (count > I2C_SMBUS_BLOCK_MAX) |
| 235 | count = I2C_SMBUS_BLOCK_MAX; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 236 | |
Bartosz Golaszewski | 9344a81 | 2016-06-06 10:48:47 +0200 | [diff] [blame] | 237 | loop_until_timeout(timeout, read_time) { |
Bartosz Golaszewski | 9afd6866 | 2016-06-06 10:48:48 +0200 | [diff] [blame] | 238 | status = i2c_smbus_read_i2c_block_data_or_emulated(client, |
| 239 | offset, |
| 240 | count, buf); |
| 241 | |
| 242 | dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n", |
| 243 | count, offset, status, jiffies); |
| 244 | |
| 245 | if (status == count) |
| 246 | return count; |
| 247 | } |
| 248 | |
| 249 | return -ETIMEDOUT; |
| 250 | } |
| 251 | |
| 252 | static ssize_t at24_eeprom_read_i2c(struct at24_data *at24, char *buf, |
| 253 | unsigned int offset, size_t count) |
| 254 | { |
| 255 | unsigned long timeout, read_time; |
| 256 | struct i2c_client *client; |
| 257 | struct i2c_msg msg[2]; |
| 258 | int status, i; |
| 259 | u8 msgbuf[2]; |
| 260 | |
| 261 | memset(msg, 0, sizeof(msg)); |
| 262 | client = at24_translate_offset(at24, &offset); |
| 263 | |
| 264 | if (count > io_limit) |
| 265 | count = io_limit; |
| 266 | |
| 267 | /* |
| 268 | * When we have a better choice than SMBus calls, use a combined I2C |
| 269 | * message. Write address; then read up to io_limit data bytes. Note |
| 270 | * that read page rollover helps us here (unlike writes). msgbuf is |
| 271 | * u8 and will cast to our needs. |
| 272 | */ |
| 273 | i = 0; |
| 274 | if (at24->chip.flags & AT24_FLAG_ADDR16) |
| 275 | msgbuf[i++] = offset >> 8; |
| 276 | msgbuf[i++] = offset; |
| 277 | |
| 278 | msg[0].addr = client->addr; |
| 279 | msg[0].buf = msgbuf; |
| 280 | msg[0].len = i; |
| 281 | |
| 282 | msg[1].addr = client->addr; |
| 283 | msg[1].flags = I2C_M_RD; |
| 284 | msg[1].buf = buf; |
| 285 | msg[1].len = count; |
| 286 | |
| 287 | loop_until_timeout(timeout, read_time) { |
| 288 | status = i2c_transfer(client->adapter, msg, 2); |
| 289 | if (status == 2) |
| 290 | status = count; |
| 291 | |
Wolfram Sang | 4d29196 | 2009-11-26 09:22:33 +0100 | [diff] [blame] | 292 | dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n", |
| 293 | count, offset, status, jiffies); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 294 | |
Wolfram Sang | 4d29196 | 2009-11-26 09:22:33 +0100 | [diff] [blame] | 295 | if (status == count) |
| 296 | return count; |
Bartosz Golaszewski | 9344a81 | 2016-06-06 10:48:47 +0200 | [diff] [blame] | 297 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 298 | |
Wolfram Sang | 4d29196 | 2009-11-26 09:22:33 +0100 | [diff] [blame] | 299 | return -ETIMEDOUT; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 302 | static ssize_t at24_eeprom_read_serial(struct at24_data *at24, char *buf, |
| 303 | unsigned int offset, size_t count) |
| 304 | { |
| 305 | unsigned long timeout, read_time; |
| 306 | struct i2c_client *client; |
| 307 | struct i2c_msg msg[2]; |
| 308 | u8 addrbuf[2]; |
| 309 | int status; |
| 310 | |
| 311 | client = at24_translate_offset(at24, &offset); |
| 312 | |
| 313 | memset(msg, 0, sizeof(msg)); |
| 314 | msg[0].addr = client->addr; |
| 315 | msg[0].buf = addrbuf; |
| 316 | |
| 317 | /* |
| 318 | * The address pointer of the device is shared between the regular |
| 319 | * EEPROM array and the serial number block. The dummy write (part of |
| 320 | * the sequential read protocol) ensures the address pointer is reset |
| 321 | * to the desired position. |
| 322 | */ |
| 323 | if (at24->chip.flags & AT24_FLAG_ADDR16) { |
| 324 | /* |
| 325 | * For 16 bit address pointers, the word address must contain |
| 326 | * a '10' sequence in bits 11 and 10 regardless of the |
| 327 | * intended position of the address pointer. |
| 328 | */ |
| 329 | addrbuf[0] = 0x08; |
| 330 | addrbuf[1] = offset; |
| 331 | msg[0].len = 2; |
| 332 | } else { |
| 333 | /* |
| 334 | * Otherwise the word address must begin with a '10' sequence, |
| 335 | * regardless of the intended address. |
| 336 | */ |
| 337 | addrbuf[0] = 0x80 + offset; |
| 338 | msg[0].len = 1; |
| 339 | } |
| 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 | return count; |
| 350 | } |
| 351 | |
| 352 | return -ETIMEDOUT; |
| 353 | } |
| 354 | |
Bartosz Golaszewski | 0b81365 | 2016-06-06 10:48:54 +0200 | [diff] [blame] | 355 | static ssize_t at24_eeprom_read_mac(struct at24_data *at24, char *buf, |
| 356 | unsigned int offset, size_t count) |
| 357 | { |
| 358 | unsigned long timeout, read_time; |
| 359 | struct i2c_client *client; |
| 360 | struct i2c_msg msg[2]; |
| 361 | u8 addrbuf[2]; |
| 362 | int status; |
| 363 | |
| 364 | client = at24_translate_offset(at24, &offset); |
| 365 | |
| 366 | memset(msg, 0, sizeof(msg)); |
| 367 | msg[0].addr = client->addr; |
| 368 | msg[0].buf = addrbuf; |
| 369 | addrbuf[0] = 0x90 + offset; |
| 370 | msg[0].len = 1; |
| 371 | msg[1].addr = client->addr; |
| 372 | msg[1].flags = I2C_M_RD; |
| 373 | msg[1].buf = buf; |
| 374 | msg[1].len = count; |
| 375 | |
| 376 | loop_until_timeout(timeout, read_time) { |
| 377 | status = i2c_transfer(client->adapter, msg, 2); |
| 378 | if (status == 2) |
| 379 | return count; |
| 380 | } |
| 381 | |
| 382 | return -ETIMEDOUT; |
| 383 | } |
| 384 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 385 | /* |
| 386 | * Note that if the hardware write-protect pin is pulled high, the whole |
| 387 | * chip is normally write protected. But there are plenty of product |
| 388 | * variants here, including OTP fuses and partial chip protect. |
| 389 | * |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 390 | * We only use page mode writes; the alternative is sloooow. These routines |
| 391 | * write at most one page. |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 392 | */ |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 393 | |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 394 | static size_t at24_adjust_write_count(struct at24_data *at24, |
| 395 | unsigned int offset, size_t count) |
| 396 | { |
| 397 | unsigned next_page; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 398 | |
| 399 | /* write_max is at most a page */ |
| 400 | if (count > at24->write_max) |
| 401 | count = at24->write_max; |
| 402 | |
| 403 | /* Never roll over backwards, to the start of this page */ |
| 404 | next_page = roundup(offset + 1, at24->chip.page_size); |
| 405 | if (offset + count > next_page) |
| 406 | count = next_page - offset; |
| 407 | |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 408 | return count; |
| 409 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 410 | |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 411 | static ssize_t at24_eeprom_write_smbus_block(struct at24_data *at24, |
| 412 | const char *buf, |
| 413 | unsigned int offset, size_t count) |
| 414 | { |
| 415 | unsigned long timeout, write_time; |
| 416 | struct i2c_client *client; |
| 417 | ssize_t status = 0; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 418 | |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 419 | client = at24_translate_offset(at24, &offset); |
| 420 | count = at24_adjust_write_count(at24, offset, count); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 421 | |
Bartosz Golaszewski | 9344a81 | 2016-06-06 10:48:47 +0200 | [diff] [blame] | 422 | loop_until_timeout(timeout, write_time) { |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 423 | status = i2c_smbus_write_i2c_block_data(client, |
| 424 | offset, count, buf); |
| 425 | if (status == 0) |
| 426 | status = count; |
Christian Gmeiner | a839ce6 | 2014-10-09 11:07:58 +0200 | [diff] [blame] | 427 | |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 428 | dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n", |
| 429 | count, offset, status, jiffies); |
| 430 | |
| 431 | if (status == count) |
| 432 | return count; |
| 433 | } |
| 434 | |
| 435 | return -ETIMEDOUT; |
| 436 | } |
| 437 | |
| 438 | static ssize_t at24_eeprom_write_smbus_byte(struct at24_data *at24, |
| 439 | const char *buf, |
| 440 | unsigned int offset, size_t count) |
| 441 | { |
| 442 | unsigned long timeout, write_time; |
| 443 | struct i2c_client *client; |
| 444 | ssize_t status = 0; |
| 445 | |
| 446 | client = at24_translate_offset(at24, &offset); |
| 447 | |
| 448 | loop_until_timeout(timeout, write_time) { |
| 449 | status = i2c_smbus_write_byte_data(client, offset, buf[0]); |
| 450 | if (status == 0) |
| 451 | status = count; |
| 452 | |
| 453 | dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n", |
| 454 | count, offset, status, jiffies); |
| 455 | |
| 456 | if (status == count) |
| 457 | return count; |
| 458 | } |
| 459 | |
| 460 | return -ETIMEDOUT; |
| 461 | } |
| 462 | |
| 463 | static ssize_t at24_eeprom_write_i2c(struct at24_data *at24, const char *buf, |
| 464 | unsigned int offset, size_t count) |
| 465 | { |
| 466 | unsigned long timeout, write_time; |
| 467 | struct i2c_client *client; |
| 468 | struct i2c_msg msg; |
| 469 | ssize_t status = 0; |
| 470 | int i = 0; |
| 471 | |
| 472 | client = at24_translate_offset(at24, &offset); |
| 473 | count = at24_adjust_write_count(at24, offset, count); |
| 474 | |
| 475 | msg.addr = client->addr; |
| 476 | msg.flags = 0; |
| 477 | |
| 478 | /* msg.buf is u8 and casts will mask the values */ |
| 479 | msg.buf = at24->writebuf; |
| 480 | if (at24->chip.flags & AT24_FLAG_ADDR16) |
| 481 | msg.buf[i++] = offset >> 8; |
| 482 | |
| 483 | msg.buf[i++] = offset; |
| 484 | memcpy(&msg.buf[i], buf, count); |
| 485 | msg.len = i + count; |
| 486 | |
| 487 | loop_until_timeout(timeout, write_time) { |
| 488 | status = i2c_transfer(client->adapter, &msg, 1); |
| 489 | if (status == 1) |
| 490 | status = count; |
| 491 | |
David Brownell | 2ce5b34 | 2008-08-10 22:56:16 +0200 | [diff] [blame] | 492 | dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n", |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 493 | count, offset, status, jiffies); |
| 494 | |
| 495 | if (status == count) |
| 496 | return count; |
Bartosz Golaszewski | 9344a81 | 2016-06-06 10:48:47 +0200 | [diff] [blame] | 497 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 498 | |
| 499 | return -ETIMEDOUT; |
| 500 | } |
| 501 | |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 502 | static int at24_read(void *priv, unsigned int off, void *val, size_t count) |
| 503 | { |
| 504 | struct at24_data *at24 = priv; |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame^] | 505 | struct i2c_client *client; |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 506 | char *buf = val; |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame^] | 507 | int ret; |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 508 | |
| 509 | if (unlikely(!count)) |
| 510 | return count; |
| 511 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame^] | 512 | client = at24_translate_offset(at24, &off); |
| 513 | |
| 514 | ret = pm_runtime_get_sync(&client->dev); |
| 515 | if (ret < 0) { |
| 516 | pm_runtime_put_noidle(&client->dev); |
| 517 | return ret; |
| 518 | } |
| 519 | |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 520 | /* |
| 521 | * Read data from chip, protecting against concurrent updates |
| 522 | * from this host, but not from other I2C masters. |
| 523 | */ |
| 524 | mutex_lock(&at24->lock); |
| 525 | |
| 526 | while (count) { |
| 527 | int status; |
| 528 | |
Bartosz Golaszewski | 318aa9c | 2016-06-06 10:48:46 +0200 | [diff] [blame] | 529 | status = at24->read_func(at24, buf, off, count); |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 530 | if (status < 0) { |
| 531 | mutex_unlock(&at24->lock); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame^] | 532 | pm_runtime_put(&client->dev); |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 533 | return status; |
| 534 | } |
| 535 | buf += status; |
| 536 | off += status; |
| 537 | count -= status; |
| 538 | } |
| 539 | |
| 540 | mutex_unlock(&at24->lock); |
| 541 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame^] | 542 | pm_runtime_put(&client->dev); |
| 543 | |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 544 | return 0; |
| 545 | } |
| 546 | |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 547 | 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] | 548 | { |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 549 | struct at24_data *at24 = priv; |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame^] | 550 | struct i2c_client *client; |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 551 | char *buf = val; |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame^] | 552 | int ret; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 553 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 554 | if (unlikely(!count)) |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 555 | return -EINVAL; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 556 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame^] | 557 | client = at24_translate_offset(at24, &off); |
| 558 | |
| 559 | ret = pm_runtime_get_sync(&client->dev); |
| 560 | if (ret < 0) { |
| 561 | pm_runtime_put_noidle(&client->dev); |
| 562 | return ret; |
| 563 | } |
| 564 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 565 | /* |
| 566 | * Write data to chip, protecting against concurrent updates |
| 567 | * from this host, but not from other I2C masters. |
| 568 | */ |
| 569 | mutex_lock(&at24->lock); |
| 570 | |
| 571 | while (count) { |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 572 | int status; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 573 | |
Bartosz Golaszewski | 318aa9c | 2016-06-06 10:48:46 +0200 | [diff] [blame] | 574 | status = at24->write_func(at24, buf, off, count); |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 575 | if (status < 0) { |
| 576 | mutex_unlock(&at24->lock); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame^] | 577 | pm_runtime_put(&client->dev); |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 578 | return status; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 579 | } |
| 580 | buf += status; |
| 581 | off += status; |
| 582 | count -= status; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | mutex_unlock(&at24->lock); |
| 586 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame^] | 587 | pm_runtime_put(&client->dev); |
| 588 | |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 589 | return 0; |
| 590 | } |
| 591 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 592 | 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] | 593 | { |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 594 | int err; |
| 595 | u32 val; |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 596 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 597 | if (device_property_present(dev, "read-only")) |
| 598 | chip->flags |= AT24_FLAG_READONLY; |
| 599 | |
Divagar Mohandass | dbc1ab9 | 2017-10-10 11:30:36 +0530 | [diff] [blame] | 600 | err = device_property_read_u32(dev, "size", &val); |
| 601 | if (!err) |
| 602 | chip->byte_len = val; |
| 603 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 604 | err = device_property_read_u32(dev, "pagesize", &val); |
| 605 | if (!err) { |
| 606 | chip->page_size = val; |
| 607 | } else { |
| 608 | /* |
| 609 | * This is slow, but we can't know all eeproms, so we better |
| 610 | * play safe. Specifying custom eeprom-types via platform_data |
| 611 | * is recommended anyhow. |
| 612 | */ |
| 613 | chip->page_size = 1; |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 614 | } |
| 615 | } |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 616 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 617 | static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) |
| 618 | { |
| 619 | struct at24_platform_data chip; |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 620 | kernel_ulong_t magic = 0; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 621 | bool writable; |
Jean Delvare | 7aeb966 | 2010-05-21 18:40:57 +0200 | [diff] [blame] | 622 | int use_smbus = 0; |
Christian Gmeiner | a839ce6 | 2014-10-09 11:07:58 +0200 | [diff] [blame] | 623 | int use_smbus_write = 0; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 624 | struct at24_data *at24; |
| 625 | int err; |
| 626 | unsigned i, num_addresses; |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 627 | u8 test_byte; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 628 | |
| 629 | if (client->dev.platform_data) { |
| 630 | chip = *(struct at24_platform_data *)client->dev.platform_data; |
| 631 | } else { |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 632 | if (id) { |
| 633 | magic = id->driver_data; |
| 634 | } else { |
| 635 | const struct acpi_device_id *aid; |
| 636 | |
| 637 | aid = acpi_match_device(at24_acpi_ids, &client->dev); |
| 638 | if (aid) |
| 639 | magic = aid->driver_data; |
| 640 | } |
| 641 | if (!magic) |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 642 | return -ENODEV; |
| 643 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 644 | chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN)); |
| 645 | magic >>= AT24_SIZE_BYTELEN; |
| 646 | chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS); |
Kevin Hilman | 7274ec8 | 2009-04-02 16:56:57 -0700 | [diff] [blame] | 647 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 648 | at24_get_pdata(&client->dev, &chip); |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 649 | |
Kevin Hilman | 7274ec8 | 2009-04-02 16:56:57 -0700 | [diff] [blame] | 650 | chip.setup = NULL; |
| 651 | chip.context = NULL; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | if (!is_power_of_2(chip.byte_len)) |
| 655 | dev_warn(&client->dev, |
| 656 | "byte_len looks suspicious (no power of 2)!\n"); |
Wolfram Sang | 45efe84 | 2010-11-17 13:00:49 +0100 | [diff] [blame] | 657 | if (!chip.page_size) { |
| 658 | dev_err(&client->dev, "page_size must not be 0!\n"); |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 659 | return -EINVAL; |
Wolfram Sang | 45efe84 | 2010-11-17 13:00:49 +0100 | [diff] [blame] | 660 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 661 | if (!is_power_of_2(chip.page_size)) |
| 662 | dev_warn(&client->dev, |
| 663 | "page_size looks suspicious (no power of 2)!\n"); |
| 664 | |
| 665 | /* Use I2C operations unless we're stuck with SMBus extensions. */ |
| 666 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 667 | if (chip.flags & AT24_FLAG_ADDR16) |
| 668 | return -EPFNOSUPPORT; |
| 669 | |
Jean Delvare | 7aeb966 | 2010-05-21 18:40:57 +0200 | [diff] [blame] | 670 | if (i2c_check_functionality(client->adapter, |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 671 | I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { |
Jean Delvare | 7aeb966 | 2010-05-21 18:40:57 +0200 | [diff] [blame] | 672 | use_smbus = I2C_SMBUS_I2C_BLOCK_DATA; |
| 673 | } else if (i2c_check_functionality(client->adapter, |
| 674 | I2C_FUNC_SMBUS_READ_WORD_DATA)) { |
| 675 | use_smbus = I2C_SMBUS_WORD_DATA; |
| 676 | } else if (i2c_check_functionality(client->adapter, |
| 677 | I2C_FUNC_SMBUS_READ_BYTE_DATA)) { |
| 678 | use_smbus = I2C_SMBUS_BYTE_DATA; |
| 679 | } else { |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 680 | return -EPFNOSUPPORT; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 681 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 682 | |
Christian Gmeiner | a839ce6 | 2014-10-09 11:07:58 +0200 | [diff] [blame] | 683 | if (i2c_check_functionality(client->adapter, |
| 684 | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) { |
| 685 | use_smbus_write = I2C_SMBUS_I2C_BLOCK_DATA; |
| 686 | } else if (i2c_check_functionality(client->adapter, |
| 687 | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) { |
| 688 | use_smbus_write = I2C_SMBUS_BYTE_DATA; |
| 689 | chip.page_size = 1; |
| 690 | } |
| 691 | } |
| 692 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 693 | if (chip.flags & AT24_FLAG_TAKE8ADDR) |
| 694 | num_addresses = 8; |
| 695 | else |
| 696 | num_addresses = DIV_ROUND_UP(chip.byte_len, |
| 697 | (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256); |
| 698 | |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 699 | at24 = devm_kzalloc(&client->dev, sizeof(struct at24_data) + |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 700 | num_addresses * sizeof(struct i2c_client *), GFP_KERNEL); |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 701 | if (!at24) |
| 702 | return -ENOMEM; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 703 | |
| 704 | mutex_init(&at24->lock); |
| 705 | at24->use_smbus = use_smbus; |
Christian Gmeiner | a839ce6 | 2014-10-09 11:07:58 +0200 | [diff] [blame] | 706 | at24->use_smbus_write = use_smbus_write; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 707 | at24->chip = chip; |
| 708 | at24->num_addresses = num_addresses; |
| 709 | |
Bartosz Golaszewski | 0b81365 | 2016-06-06 10:48:54 +0200 | [diff] [blame] | 710 | if ((chip.flags & AT24_FLAG_SERIAL) && (chip.flags & AT24_FLAG_MAC)) { |
| 711 | dev_err(&client->dev, |
| 712 | "invalid device data - cannot have both AT24_FLAG_SERIAL & AT24_FLAG_MAC."); |
| 713 | return -EINVAL; |
| 714 | } |
| 715 | |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 716 | if (chip.flags & AT24_FLAG_SERIAL) { |
| 717 | at24->read_func = at24_eeprom_read_serial; |
Bartosz Golaszewski | 0b81365 | 2016-06-06 10:48:54 +0200 | [diff] [blame] | 718 | } else if (chip.flags & AT24_FLAG_MAC) { |
| 719 | at24->read_func = at24_eeprom_read_mac; |
Bartosz Golaszewski | 818d022 | 2016-06-06 10:48:51 +0200 | [diff] [blame] | 720 | } else { |
| 721 | at24->read_func = at24->use_smbus ? at24_eeprom_read_smbus |
| 722 | : at24_eeprom_read_i2c; |
| 723 | } |
| 724 | |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 725 | if (at24->use_smbus) { |
| 726 | if (at24->use_smbus_write == I2C_SMBUS_I2C_BLOCK_DATA) |
| 727 | at24->write_func = at24_eeprom_write_smbus_block; |
| 728 | else |
| 729 | at24->write_func = at24_eeprom_write_smbus_byte; |
| 730 | } else { |
| 731 | at24->write_func = at24_eeprom_write_i2c; |
| 732 | } |
Bartosz Golaszewski | 318aa9c | 2016-06-06 10:48:46 +0200 | [diff] [blame] | 733 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 734 | writable = !(chip.flags & AT24_FLAG_READONLY); |
| 735 | if (writable) { |
Christian Gmeiner | a839ce6 | 2014-10-09 11:07:58 +0200 | [diff] [blame] | 736 | if (!use_smbus || use_smbus_write) { |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 737 | |
| 738 | unsigned write_max = chip.page_size; |
| 739 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 740 | if (write_max > io_limit) |
| 741 | write_max = io_limit; |
| 742 | if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX) |
| 743 | write_max = I2C_SMBUS_BLOCK_MAX; |
| 744 | at24->write_max = write_max; |
| 745 | |
| 746 | /* buffer (data + address at the beginning) */ |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 747 | at24->writebuf = devm_kzalloc(&client->dev, |
| 748 | write_max + 2, GFP_KERNEL); |
| 749 | if (!at24->writebuf) |
| 750 | return -ENOMEM; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 751 | } else { |
| 752 | dev_warn(&client->dev, |
| 753 | "cannot write due to controller restrictions."); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | at24->client[0] = client; |
| 758 | |
| 759 | /* use dummy devices for multiple-address chips */ |
| 760 | for (i = 1; i < num_addresses; i++) { |
| 761 | at24->client[i] = i2c_new_dummy(client->adapter, |
| 762 | client->addr + i); |
| 763 | if (!at24->client[i]) { |
| 764 | dev_err(&client->dev, "address 0x%02x unavailable\n", |
| 765 | client->addr + i); |
| 766 | err = -EADDRINUSE; |
| 767 | goto err_clients; |
| 768 | } |
| 769 | } |
| 770 | |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 771 | i2c_set_clientdata(client, at24); |
| 772 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame^] | 773 | /* enable runtime pm */ |
| 774 | pm_runtime_set_active(&client->dev); |
| 775 | pm_runtime_enable(&client->dev); |
| 776 | |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 777 | /* |
| 778 | * Perform a one-byte test read to verify that the |
| 779 | * chip is functional. |
| 780 | */ |
| 781 | err = at24_read(at24, 0, &test_byte, 1); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame^] | 782 | pm_runtime_idle(&client->dev); |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 783 | if (err) { |
| 784 | err = -ENODEV; |
| 785 | goto err_clients; |
| 786 | } |
| 787 | |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 788 | at24->nvmem_config.name = dev_name(&client->dev); |
| 789 | at24->nvmem_config.dev = &client->dev; |
| 790 | at24->nvmem_config.read_only = !writable; |
| 791 | at24->nvmem_config.root_only = true; |
| 792 | at24->nvmem_config.owner = THIS_MODULE; |
| 793 | at24->nvmem_config.compat = true; |
| 794 | at24->nvmem_config.base_dev = &client->dev; |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 795 | at24->nvmem_config.reg_read = at24_read; |
| 796 | at24->nvmem_config.reg_write = at24_write; |
| 797 | at24->nvmem_config.priv = at24; |
| 798 | at24->nvmem_config.stride = 4; |
| 799 | at24->nvmem_config.word_size = 1; |
| 800 | at24->nvmem_config.size = chip.byte_len; |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 801 | |
| 802 | at24->nvmem = nvmem_register(&at24->nvmem_config); |
| 803 | |
| 804 | if (IS_ERR(at24->nvmem)) { |
| 805 | err = PTR_ERR(at24->nvmem); |
| 806 | goto err_clients; |
| 807 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 808 | |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 809 | dev_info(&client->dev, "%u byte %s EEPROM, %s, %u bytes/write\n", |
| 810 | chip.byte_len, client->name, |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 811 | writable ? "writable" : "read-only", at24->write_max); |
Jean Delvare | 7aeb966 | 2010-05-21 18:40:57 +0200 | [diff] [blame] | 812 | if (use_smbus == I2C_SMBUS_WORD_DATA || |
| 813 | use_smbus == I2C_SMBUS_BYTE_DATA) { |
| 814 | dev_notice(&client->dev, "Falling back to %s reads, " |
| 815 | "performance will suffer\n", use_smbus == |
| 816 | I2C_SMBUS_WORD_DATA ? "word" : "byte"); |
| 817 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 818 | |
Kevin Hilman | 7274ec8 | 2009-04-02 16:56:57 -0700 | [diff] [blame] | 819 | /* export data to kernel code */ |
| 820 | if (chip.setup) |
Andrew Lunn | bec3c11 | 2016-02-26 20:59:24 +0100 | [diff] [blame] | 821 | chip.setup(at24->nvmem, chip.context); |
Kevin Hilman | 7274ec8 | 2009-04-02 16:56:57 -0700 | [diff] [blame] | 822 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 823 | return 0; |
| 824 | |
| 825 | err_clients: |
| 826 | for (i = 1; i < num_addresses; i++) |
| 827 | if (at24->client[i]) |
| 828 | i2c_unregister_device(at24->client[i]); |
| 829 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame^] | 830 | pm_runtime_disable(&client->dev); |
| 831 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 832 | return err; |
| 833 | } |
| 834 | |
Bill Pemberton | 486a5c2 | 2012-11-19 13:26:02 -0500 | [diff] [blame] | 835 | static int at24_remove(struct i2c_client *client) |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 836 | { |
| 837 | struct at24_data *at24; |
| 838 | int i; |
| 839 | |
| 840 | at24 = i2c_get_clientdata(client); |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 841 | |
| 842 | nvmem_unregister(at24->nvmem); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 843 | |
| 844 | for (i = 1; i < at24->num_addresses; i++) |
| 845 | i2c_unregister_device(at24->client[i]); |
| 846 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame^] | 847 | pm_runtime_disable(&client->dev); |
| 848 | pm_runtime_set_suspended(&client->dev); |
| 849 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | /*-------------------------------------------------------------------------*/ |
| 854 | |
| 855 | static struct i2c_driver at24_driver = { |
| 856 | .driver = { |
| 857 | .name = "at24", |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 858 | .acpi_match_table = ACPI_PTR(at24_acpi_ids), |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 859 | }, |
| 860 | .probe = at24_probe, |
Bill Pemberton | 2d6bed9 | 2012-11-19 13:21:23 -0500 | [diff] [blame] | 861 | .remove = at24_remove, |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 862 | .id_table = at24_ids, |
| 863 | }; |
| 864 | |
| 865 | static int __init at24_init(void) |
| 866 | { |
Wolfram Sang | 45efe84 | 2010-11-17 13:00:49 +0100 | [diff] [blame] | 867 | if (!io_limit) { |
| 868 | pr_err("at24: io_limit must not be 0!\n"); |
| 869 | return -EINVAL; |
| 870 | } |
| 871 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 872 | io_limit = rounddown_pow_of_two(io_limit); |
| 873 | return i2c_add_driver(&at24_driver); |
| 874 | } |
| 875 | module_init(at24_init); |
| 876 | |
| 877 | static void __exit at24_exit(void) |
| 878 | { |
| 879 | i2c_del_driver(&at24_driver); |
| 880 | } |
| 881 | module_exit(at24_exit); |
| 882 | |
| 883 | MODULE_DESCRIPTION("Driver for most I2C EEPROMs"); |
| 884 | MODULE_AUTHOR("David Brownell and Wolfram Sang"); |
| 885 | MODULE_LICENSE("GPL"); |