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> |
Bartosz Golaszewski | 6ce261e | 2017-12-19 11:28:54 +0100 | [diff] [blame^] | 30 | #include <linux/gpio/consumer.h> |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * I2C EEPROMs from most vendors are inexpensive and mostly interchangeable. |
| 34 | * Differences between different vendor product lines (like Atmel AT24C or |
| 35 | * MicroChip 24LC, etc) won't much matter for typical read/write access. |
| 36 | * There are also I2C RAM chips, likewise interchangeable. One example |
| 37 | * would be the PCF8570, which acts like a 24c02 EEPROM (256 bytes). |
| 38 | * |
| 39 | * However, misconfiguration can lose data. "Set 16-bit memory address" |
| 40 | * to a part with 8-bit addressing will overwrite data. Writing with too |
| 41 | * big a page size also loses data. And it's not safe to assume that the |
| 42 | * conventional addresses 0x50..0x57 only hold eeproms; a PCF8563 RTC |
| 43 | * uses 0x51, for just one example. |
| 44 | * |
| 45 | * Accordingly, explicit board-specific configuration data should be used |
| 46 | * in almost all cases. (One partial exception is an SMBus used to access |
| 47 | * "SPD" data for DRAM sticks. Those only use 24c02 EEPROMs.) |
| 48 | * |
| 49 | * So this driver uses "new style" I2C driver binding, expecting to be |
| 50 | * told what devices exist. That may be in arch/X/mach-Y/board-Z.c or |
| 51 | * similar kernel-resident tables; or, configuration data coming from |
| 52 | * a bootloader. |
| 53 | * |
| 54 | * Other than binding model, current differences from "eeprom" driver are |
| 55 | * that this one handles write access and isn't restricted to 24c02 devices. |
| 56 | * It also handles larger devices (32 kbit and up) with two-byte addresses, |
| 57 | * which won't work on pure SMBus systems. |
| 58 | */ |
| 59 | |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 60 | struct at24_client { |
| 61 | struct i2c_client *client; |
| 62 | struct regmap *regmap; |
| 63 | }; |
| 64 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 65 | struct at24_data { |
| 66 | struct at24_platform_data chip; |
Bartosz Golaszewski | 318aa9c | 2016-06-06 10:48:46 +0200 | [diff] [blame] | 67 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 68 | /* |
| 69 | * Lock protects against activities from other Linux tasks, |
| 70 | * but not from changes by other I2C masters. |
| 71 | */ |
| 72 | struct mutex lock; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 73 | |
Bartosz Golaszewski | aa4ce22 | 2017-12-13 11:56:23 +0100 | [diff] [blame] | 74 | unsigned int write_max; |
| 75 | unsigned int num_addresses; |
Heiner Kallweit | 4bb5c13c | 2017-11-28 21:51:50 +0100 | [diff] [blame] | 76 | unsigned int offset_adj; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 77 | |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 78 | struct nvmem_config nvmem_config; |
| 79 | struct nvmem_device *nvmem; |
| 80 | |
Bartosz Golaszewski | 6ce261e | 2017-12-19 11:28:54 +0100 | [diff] [blame^] | 81 | struct gpio_desc *wp_gpio; |
| 82 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 83 | /* |
| 84 | * Some chips tie up multiple I2C addresses; dummy devices reserve |
| 85 | * them for us, and we'll use them with SMBus calls. |
| 86 | */ |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 87 | struct at24_client client[]; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | /* |
| 91 | * This parameter is to help this driver avoid blocking other drivers out |
| 92 | * of I2C for potentially troublesome amounts of time. With a 100 kHz I2C |
| 93 | * clock, one 256 byte read takes about 1/43 second which is excessive; |
| 94 | * but the 1/170 second it takes at 400 kHz may be quite reasonable; and |
| 95 | * at 1 MHz (Fm+) a 1/430 second delay could easily be invisible. |
| 96 | * |
| 97 | * This value is forced to be a power of two so that writes align on pages. |
| 98 | */ |
Bartosz Golaszewski | ec3c2d5 | 2017-12-18 18:16:46 +0100 | [diff] [blame] | 99 | static unsigned int at24_io_limit = 128; |
| 100 | module_param_named(io_limit, at24_io_limit, uint, 0); |
| 101 | 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] | 102 | |
| 103 | /* |
| 104 | * Specs often allow 5 msec for a page write, sometimes 20 msec; |
| 105 | * it's important to recover from write timeouts. |
| 106 | */ |
Bartosz Golaszewski | ec3c2d5 | 2017-12-18 18:16:46 +0100 | [diff] [blame] | 107 | static unsigned int at24_write_timeout = 25; |
| 108 | module_param_named(write_timeout, at24_write_timeout, uint, 0); |
| 109 | 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] | 110 | |
Bartosz Golaszewski | 9344a81 | 2016-06-06 10:48:47 +0200 | [diff] [blame] | 111 | /* |
| 112 | * Both reads and writes fail if the previous write didn't complete yet. This |
| 113 | * 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] | 114 | * write to work while making sure that at least one iteration is run before |
| 115 | * checking the break condition. |
Bartosz Golaszewski | 9344a81 | 2016-06-06 10:48:47 +0200 | [diff] [blame] | 116 | * |
| 117 | * It takes two parameters: a variable in which the future timeout in jiffies |
| 118 | * will be stored and a temporary variable holding the time of the last |
| 119 | * iteration of processing the request. Both should be unsigned integers |
| 120 | * holding at least 32 bits. |
| 121 | */ |
Bartosz Golaszewski | ec3c2d5 | 2017-12-18 18:16:46 +0100 | [diff] [blame] | 122 | #define at24_loop_until_timeout(tout, op_time) \ |
| 123 | for (tout = jiffies + msecs_to_jiffies(at24_write_timeout), \ |
| 124 | op_time = 0; \ |
Bartosz Golaszewski | 24da3cc | 2016-07-17 20:40:06 +0200 | [diff] [blame] | 125 | op_time ? time_before(op_time, tout) : true; \ |
Bartosz Golaszewski | 9344a81 | 2016-06-06 10:48:47 +0200 | [diff] [blame] | 126 | usleep_range(1000, 1500), op_time = jiffies) |
| 127 | |
Sven Van Asbroeck | b680f4f | 2017-12-20 11:48:56 -0500 | [diff] [blame] | 128 | struct at24_chip_data { |
| 129 | /* |
| 130 | * these fields mirror their equivalents in |
| 131 | * struct at24_platform_data |
| 132 | */ |
| 133 | u32 byte_len; |
| 134 | u8 flags; |
| 135 | }; |
| 136 | |
| 137 | #define AT24_CHIP_DATA(_name, _len, _flags) \ |
| 138 | static const struct at24_chip_data _name = { \ |
| 139 | .byte_len = _len, .flags = _flags, \ |
| 140 | } |
| 141 | |
| 142 | /* needs 8 addresses as A0-A2 are ignored */ |
| 143 | AT24_CHIP_DATA(at24_data_24c00, 128 / 8, AT24_FLAG_TAKE8ADDR); |
| 144 | /* old variants can't be handled with this generic entry! */ |
| 145 | AT24_CHIP_DATA(at24_data_24c01, 1024 / 8, 0); |
| 146 | AT24_CHIP_DATA(at24_data_24cs01, 16, |
| 147 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY); |
| 148 | AT24_CHIP_DATA(at24_data_24c02, 2048 / 8, 0); |
| 149 | AT24_CHIP_DATA(at24_data_24cs02, 16, |
| 150 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY); |
| 151 | AT24_CHIP_DATA(at24_data_24mac402, 48 / 8, |
| 152 | AT24_FLAG_MAC | AT24_FLAG_READONLY); |
| 153 | AT24_CHIP_DATA(at24_data_24mac602, 64 / 8, |
| 154 | AT24_FLAG_MAC | AT24_FLAG_READONLY); |
| 155 | /* spd is a 24c02 in memory DIMMs */ |
| 156 | AT24_CHIP_DATA(at24_data_spd, 2048 / 8, |
| 157 | AT24_FLAG_READONLY | AT24_FLAG_IRUGO); |
| 158 | AT24_CHIP_DATA(at24_data_24c04, 4096 / 8, 0); |
| 159 | AT24_CHIP_DATA(at24_data_24cs04, 16, |
| 160 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY); |
| 161 | /* 24rf08 quirk is handled at i2c-core */ |
| 162 | AT24_CHIP_DATA(at24_data_24c08, 8192 / 8, 0); |
| 163 | AT24_CHIP_DATA(at24_data_24cs08, 16, |
| 164 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY); |
| 165 | AT24_CHIP_DATA(at24_data_24c16, 16384 / 8, 0); |
| 166 | AT24_CHIP_DATA(at24_data_24cs16, 16, |
| 167 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY); |
| 168 | AT24_CHIP_DATA(at24_data_24c32, 32768 / 8, AT24_FLAG_ADDR16); |
| 169 | AT24_CHIP_DATA(at24_data_24cs32, 16, |
| 170 | AT24_FLAG_ADDR16 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY); |
| 171 | AT24_CHIP_DATA(at24_data_24c64, 65536 / 8, AT24_FLAG_ADDR16); |
| 172 | AT24_CHIP_DATA(at24_data_24cs64, 16, |
| 173 | AT24_FLAG_ADDR16 | AT24_FLAG_SERIAL | AT24_FLAG_READONLY); |
| 174 | AT24_CHIP_DATA(at24_data_24c128, 131072 / 8, AT24_FLAG_ADDR16); |
| 175 | AT24_CHIP_DATA(at24_data_24c256, 262144 / 8, AT24_FLAG_ADDR16); |
| 176 | AT24_CHIP_DATA(at24_data_24c512, 524288 / 8, AT24_FLAG_ADDR16); |
| 177 | AT24_CHIP_DATA(at24_data_24c1024, 1048576 / 8, AT24_FLAG_ADDR16); |
| 178 | /* identical to 24c08 ? */ |
| 179 | AT24_CHIP_DATA(at24_data_INT3499, 8192 / 8, 0); |
| 180 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 181 | static const struct i2c_device_id at24_ids[] = { |
Sven Van Asbroeck | b680f4f | 2017-12-20 11:48:56 -0500 | [diff] [blame] | 182 | { "24c00", (kernel_ulong_t)&at24_data_24c00 }, |
| 183 | { "24c01", (kernel_ulong_t)&at24_data_24c01 }, |
| 184 | { "24cs01", (kernel_ulong_t)&at24_data_24cs01 }, |
| 185 | { "24c02", (kernel_ulong_t)&at24_data_24c02 }, |
| 186 | { "24cs02", (kernel_ulong_t)&at24_data_24cs02 }, |
| 187 | { "24mac402", (kernel_ulong_t)&at24_data_24mac402 }, |
| 188 | { "24mac602", (kernel_ulong_t)&at24_data_24mac602 }, |
| 189 | { "spd", (kernel_ulong_t)&at24_data_spd }, |
| 190 | { "24c04", (kernel_ulong_t)&at24_data_24c04 }, |
| 191 | { "24cs04", (kernel_ulong_t)&at24_data_24cs04 }, |
| 192 | { "24c08", (kernel_ulong_t)&at24_data_24c08 }, |
| 193 | { "24cs08", (kernel_ulong_t)&at24_data_24cs08 }, |
| 194 | { "24c16", (kernel_ulong_t)&at24_data_24c16 }, |
| 195 | { "24cs16", (kernel_ulong_t)&at24_data_24cs16 }, |
| 196 | { "24c32", (kernel_ulong_t)&at24_data_24c32 }, |
| 197 | { "24cs32", (kernel_ulong_t)&at24_data_24cs32 }, |
| 198 | { "24c64", (kernel_ulong_t)&at24_data_24c64 }, |
| 199 | { "24cs64", (kernel_ulong_t)&at24_data_24cs64 }, |
| 200 | { "24c128", (kernel_ulong_t)&at24_data_24c128 }, |
| 201 | { "24c256", (kernel_ulong_t)&at24_data_24c256 }, |
| 202 | { "24c512", (kernel_ulong_t)&at24_data_24c512 }, |
| 203 | { "24c1024", (kernel_ulong_t)&at24_data_24c1024 }, |
| 204 | { "at24", 0 }, |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 205 | { /* END OF LIST */ } |
| 206 | }; |
| 207 | MODULE_DEVICE_TABLE(i2c, at24_ids); |
| 208 | |
Javier Martinez Canillas | 7f2a2f0 | 2017-10-01 12:49:48 +0200 | [diff] [blame] | 209 | static const struct of_device_id at24_of_match[] = { |
Sven Van Asbroeck | b680f4f | 2017-12-20 11:48:56 -0500 | [diff] [blame] | 210 | { .compatible = "atmel,24c00", .data = &at24_data_24c00 }, |
| 211 | { .compatible = "atmel,24c01", .data = &at24_data_24c01 }, |
| 212 | { .compatible = "atmel,24c02", .data = &at24_data_24c02 }, |
| 213 | { .compatible = "atmel,spd", .data = &at24_data_spd }, |
| 214 | { .compatible = "atmel,24c04", .data = &at24_data_24c04 }, |
| 215 | { .compatible = "atmel,24c08", .data = &at24_data_24c08 }, |
| 216 | { .compatible = "atmel,24c16", .data = &at24_data_24c16 }, |
| 217 | { .compatible = "atmel,24c32", .data = &at24_data_24c32 }, |
| 218 | { .compatible = "atmel,24c64", .data = &at24_data_24c64 }, |
| 219 | { .compatible = "atmel,24c128", .data = &at24_data_24c128 }, |
| 220 | { .compatible = "atmel,24c256", .data = &at24_data_24c256 }, |
| 221 | { .compatible = "atmel,24c512", .data = &at24_data_24c512 }, |
| 222 | { .compatible = "atmel,24c1024", .data = &at24_data_24c1024 }, |
| 223 | { /* END OF LIST */ }, |
Javier Martinez Canillas | 7f2a2f0 | 2017-10-01 12:49:48 +0200 | [diff] [blame] | 224 | }; |
| 225 | MODULE_DEVICE_TABLE(of, at24_of_match); |
| 226 | |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 227 | static const struct acpi_device_id at24_acpi_ids[] = { |
Sven Van Asbroeck | b680f4f | 2017-12-20 11:48:56 -0500 | [diff] [blame] | 228 | { "INT3499", (kernel_ulong_t)&at24_data_INT3499 }, |
| 229 | { /* END OF LIST */ } |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 230 | }; |
| 231 | MODULE_DEVICE_TABLE(acpi, at24_acpi_ids); |
| 232 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 233 | /*-------------------------------------------------------------------------*/ |
| 234 | |
| 235 | /* |
| 236 | * This routine supports chips which consume multiple I2C addresses. It |
| 237 | * computes the addressing information to be used for a given r/w request. |
| 238 | * Assumes that sanity checks for offset happened at sysfs-layer. |
Bartosz Golaszewski | 9afd6866 | 2016-06-06 10:48:48 +0200 | [diff] [blame] | 239 | * |
| 240 | * Slave address and byte offset derive from the offset. Always |
| 241 | * set the byte address; on a multi-master board, another master |
| 242 | * may have changed the chip's "current" address pointer. |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 243 | */ |
Heiner Kallweit | 4604948 | 2017-11-28 21:51:42 +0100 | [diff] [blame] | 244 | static struct at24_client *at24_translate_offset(struct at24_data *at24, |
| 245 | unsigned int *offset) |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 246 | { |
Bartosz Golaszewski | aa4ce22 | 2017-12-13 11:56:23 +0100 | [diff] [blame] | 247 | unsigned int i; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 248 | |
| 249 | if (at24->chip.flags & AT24_FLAG_ADDR16) { |
| 250 | i = *offset >> 16; |
| 251 | *offset &= 0xffff; |
| 252 | } else { |
| 253 | i = *offset >> 8; |
| 254 | *offset &= 0xff; |
| 255 | } |
| 256 | |
Heiner Kallweit | 4604948 | 2017-11-28 21:51:42 +0100 | [diff] [blame] | 257 | return &at24->client[i]; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 258 | } |
| 259 | |
Sven Van Asbroeck | e32213f | 2017-12-08 11:28:30 -0500 | [diff] [blame] | 260 | static size_t at24_adjust_read_count(struct at24_data *at24, |
| 261 | unsigned int offset, size_t count) |
| 262 | { |
| 263 | unsigned int bits; |
| 264 | size_t remainder; |
| 265 | |
| 266 | /* |
| 267 | * In case of multi-address chips that don't rollover reads to |
| 268 | * the next slave address: truncate the count to the slave boundary, |
| 269 | * so that the read never straddles slaves. |
| 270 | */ |
| 271 | if (at24->chip.flags & AT24_FLAG_NO_RDROL) { |
| 272 | bits = (at24->chip.flags & AT24_FLAG_ADDR16) ? 16 : 8; |
| 273 | remainder = BIT(bits) - offset; |
| 274 | if (count > remainder) |
| 275 | count = remainder; |
| 276 | } |
| 277 | |
Bartosz Golaszewski | ec3c2d5 | 2017-12-18 18:16:46 +0100 | [diff] [blame] | 278 | if (count > at24_io_limit) |
| 279 | count = at24_io_limit; |
Sven Van Asbroeck | e32213f | 2017-12-08 11:28:30 -0500 | [diff] [blame] | 280 | |
| 281 | return count; |
| 282 | } |
| 283 | |
Heiner Kallweit | 4bb5c13c | 2017-11-28 21:51:50 +0100 | [diff] [blame] | 284 | static ssize_t at24_regmap_read(struct at24_data *at24, char *buf, |
| 285 | unsigned int offset, size_t count) |
| 286 | { |
| 287 | unsigned long timeout, read_time; |
| 288 | struct at24_client *at24_client; |
| 289 | struct i2c_client *client; |
| 290 | struct regmap *regmap; |
| 291 | int ret; |
| 292 | |
| 293 | at24_client = at24_translate_offset(at24, &offset); |
| 294 | regmap = at24_client->regmap; |
| 295 | client = at24_client->client; |
Sven Van Asbroeck | e32213f | 2017-12-08 11:28:30 -0500 | [diff] [blame] | 296 | count = at24_adjust_read_count(at24, offset, count); |
Heiner Kallweit | 4bb5c13c | 2017-11-28 21:51:50 +0100 | [diff] [blame] | 297 | |
| 298 | /* adjust offset for mac and serial read ops */ |
| 299 | offset += at24->offset_adj; |
| 300 | |
Bartosz Golaszewski | ec3c2d5 | 2017-12-18 18:16:46 +0100 | [diff] [blame] | 301 | at24_loop_until_timeout(timeout, read_time) { |
Heiner Kallweit | 4bb5c13c | 2017-11-28 21:51:50 +0100 | [diff] [blame] | 302 | ret = regmap_bulk_read(regmap, offset, buf, count); |
| 303 | dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n", |
| 304 | count, offset, ret, jiffies); |
| 305 | if (!ret) |
| 306 | return count; |
| 307 | } |
| 308 | |
| 309 | return -ETIMEDOUT; |
| 310 | } |
| 311 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 312 | /* |
| 313 | * Note that if the hardware write-protect pin is pulled high, the whole |
| 314 | * chip is normally write protected. But there are plenty of product |
| 315 | * variants here, including OTP fuses and partial chip protect. |
| 316 | * |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 317 | * We only use page mode writes; the alternative is sloooow. These routines |
| 318 | * write at most one page. |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 319 | */ |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 320 | |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 321 | static size_t at24_adjust_write_count(struct at24_data *at24, |
| 322 | unsigned int offset, size_t count) |
| 323 | { |
Bartosz Golaszewski | aa4ce22 | 2017-12-13 11:56:23 +0100 | [diff] [blame] | 324 | unsigned int next_page; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 325 | |
| 326 | /* write_max is at most a page */ |
| 327 | if (count > at24->write_max) |
| 328 | count = at24->write_max; |
| 329 | |
| 330 | /* Never roll over backwards, to the start of this page */ |
| 331 | next_page = roundup(offset + 1, at24->chip.page_size); |
| 332 | if (offset + count > next_page) |
| 333 | count = next_page - offset; |
| 334 | |
Bartosz Golaszewski | cd0c861 | 2016-06-06 10:48:49 +0200 | [diff] [blame] | 335 | return count; |
| 336 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 337 | |
Heiner Kallweit | 8e5888e | 2017-11-28 21:51:45 +0100 | [diff] [blame] | 338 | static ssize_t at24_regmap_write(struct at24_data *at24, const char *buf, |
| 339 | unsigned int offset, size_t count) |
| 340 | { |
| 341 | unsigned long timeout, write_time; |
| 342 | struct at24_client *at24_client; |
| 343 | struct i2c_client *client; |
| 344 | struct regmap *regmap; |
| 345 | int ret; |
| 346 | |
| 347 | at24_client = at24_translate_offset(at24, &offset); |
| 348 | regmap = at24_client->regmap; |
| 349 | client = at24_client->client; |
| 350 | count = at24_adjust_write_count(at24, offset, count); |
| 351 | |
Bartosz Golaszewski | ec3c2d5 | 2017-12-18 18:16:46 +0100 | [diff] [blame] | 352 | at24_loop_until_timeout(timeout, write_time) { |
Heiner Kallweit | 8e5888e | 2017-11-28 21:51:45 +0100 | [diff] [blame] | 353 | ret = regmap_bulk_write(regmap, offset, buf, count); |
| 354 | dev_dbg(&client->dev, "write %zu@%d --> %d (%ld)\n", |
| 355 | count, offset, ret, jiffies); |
| 356 | if (!ret) |
| 357 | return count; |
| 358 | } |
| 359 | |
| 360 | return -ETIMEDOUT; |
| 361 | } |
| 362 | |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 363 | static int at24_read(void *priv, unsigned int off, void *val, size_t count) |
| 364 | { |
| 365 | struct at24_data *at24 = priv; |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 366 | struct device *dev = &at24->client[0].client->dev; |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 367 | char *buf = val; |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 368 | int ret; |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 369 | |
| 370 | if (unlikely(!count)) |
| 371 | return count; |
| 372 | |
Heiner Kallweit | d9bcd46 | 2017-11-24 07:47:50 +0100 | [diff] [blame] | 373 | if (off + count > at24->chip.byte_len) |
| 374 | return -EINVAL; |
| 375 | |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 376 | ret = pm_runtime_get_sync(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 377 | if (ret < 0) { |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 378 | pm_runtime_put_noidle(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 379 | return ret; |
| 380 | } |
| 381 | |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 382 | /* |
| 383 | * Read data from chip, protecting against concurrent updates |
| 384 | * from this host, but not from other I2C masters. |
| 385 | */ |
| 386 | mutex_lock(&at24->lock); |
| 387 | |
| 388 | while (count) { |
| 389 | int status; |
| 390 | |
Heiner Kallweit | 4bb5c13c | 2017-11-28 21:51:50 +0100 | [diff] [blame] | 391 | status = at24_regmap_read(at24, buf, off, count); |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 392 | if (status < 0) { |
| 393 | mutex_unlock(&at24->lock); |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 394 | pm_runtime_put(dev); |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 395 | return status; |
| 396 | } |
| 397 | buf += status; |
| 398 | off += status; |
| 399 | count -= status; |
| 400 | } |
| 401 | |
| 402 | mutex_unlock(&at24->lock); |
| 403 | |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 404 | pm_runtime_put(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 405 | |
Bartosz Golaszewski | d5bc004 | 2016-06-06 10:48:44 +0200 | [diff] [blame] | 406 | return 0; |
| 407 | } |
| 408 | |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 409 | 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] | 410 | { |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 411 | struct at24_data *at24 = priv; |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 412 | struct device *dev = &at24->client[0].client->dev; |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 413 | char *buf = val; |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 414 | int ret; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 415 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 416 | if (unlikely(!count)) |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 417 | return -EINVAL; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 418 | |
Heiner Kallweit | d9bcd46 | 2017-11-24 07:47:50 +0100 | [diff] [blame] | 419 | if (off + count > at24->chip.byte_len) |
| 420 | return -EINVAL; |
| 421 | |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 422 | ret = pm_runtime_get_sync(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 423 | if (ret < 0) { |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 424 | pm_runtime_put_noidle(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 425 | return ret; |
| 426 | } |
| 427 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 428 | /* |
| 429 | * Write data to chip, protecting against concurrent updates |
| 430 | * from this host, but not from other I2C masters. |
| 431 | */ |
| 432 | mutex_lock(&at24->lock); |
Bartosz Golaszewski | 6ce261e | 2017-12-19 11:28:54 +0100 | [diff] [blame^] | 433 | gpiod_set_value_cansleep(at24->wp_gpio, 0); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 434 | |
| 435 | while (count) { |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 436 | int status; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 437 | |
Heiner Kallweit | 8e5888e | 2017-11-28 21:51:45 +0100 | [diff] [blame] | 438 | status = at24_regmap_write(at24, buf, off, count); |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 439 | if (status < 0) { |
Bartosz Golaszewski | 6ce261e | 2017-12-19 11:28:54 +0100 | [diff] [blame^] | 440 | gpiod_set_value_cansleep(at24->wp_gpio, 1); |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 441 | mutex_unlock(&at24->lock); |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 442 | pm_runtime_put(dev); |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 443 | return status; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 444 | } |
| 445 | buf += status; |
| 446 | off += status; |
| 447 | count -= status; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 448 | } |
| 449 | |
Bartosz Golaszewski | 6ce261e | 2017-12-19 11:28:54 +0100 | [diff] [blame^] | 450 | gpiod_set_value_cansleep(at24->wp_gpio, 1); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 451 | mutex_unlock(&at24->lock); |
| 452 | |
Sakari Ailus | f9ecc83 | 2017-12-01 13:37:12 -0500 | [diff] [blame] | 453 | pm_runtime_put(dev); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 454 | |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 455 | return 0; |
| 456 | } |
| 457 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 458 | 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] | 459 | { |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 460 | int err; |
| 461 | u32 val; |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 462 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 463 | if (device_property_present(dev, "read-only")) |
| 464 | chip->flags |= AT24_FLAG_READONLY; |
Sven Van Asbroeck | e32213f | 2017-12-08 11:28:30 -0500 | [diff] [blame] | 465 | if (device_property_present(dev, "no-read-rollover")) |
| 466 | chip->flags |= AT24_FLAG_NO_RDROL; |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 467 | |
Divagar Mohandass | dbc1ab9 | 2017-10-10 11:30:36 +0530 | [diff] [blame] | 468 | err = device_property_read_u32(dev, "size", &val); |
| 469 | if (!err) |
| 470 | chip->byte_len = val; |
| 471 | |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 472 | err = device_property_read_u32(dev, "pagesize", &val); |
| 473 | if (!err) { |
| 474 | chip->page_size = val; |
| 475 | } else { |
| 476 | /* |
| 477 | * This is slow, but we can't know all eeproms, so we better |
| 478 | * play safe. Specifying custom eeprom-types via platform_data |
| 479 | * is recommended anyhow. |
| 480 | */ |
| 481 | chip->page_size = 1; |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 482 | } |
| 483 | } |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 484 | |
Heiner Kallweit | 4bb5c13c | 2017-11-28 21:51:50 +0100 | [diff] [blame] | 485 | static unsigned int at24_get_offset_adj(u8 flags, unsigned int byte_len) |
| 486 | { |
| 487 | if (flags & AT24_FLAG_MAC) { |
| 488 | /* EUI-48 starts from 0x9a, EUI-64 from 0x98 */ |
| 489 | return 0xa0 - byte_len; |
| 490 | } else if (flags & AT24_FLAG_SERIAL && flags & AT24_FLAG_ADDR16) { |
| 491 | /* |
| 492 | * For 16 bit address pointers, the word address must contain |
| 493 | * a '10' sequence in bits 11 and 10 regardless of the |
| 494 | * intended position of the address pointer. |
| 495 | */ |
| 496 | return 0x0800; |
| 497 | } else if (flags & AT24_FLAG_SERIAL) { |
| 498 | /* |
| 499 | * Otherwise the word address must begin with a '10' sequence, |
| 500 | * regardless of the intended address. |
| 501 | */ |
| 502 | return 0x0080; |
| 503 | } else { |
| 504 | return 0; |
| 505 | } |
| 506 | } |
| 507 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 508 | static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) |
| 509 | { |
Sven Van Asbroeck | b680f4f | 2017-12-20 11:48:56 -0500 | [diff] [blame] | 510 | struct at24_platform_data chip = { 0 }; |
| 511 | const struct at24_chip_data *cd = NULL; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 512 | bool writable; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 513 | struct at24_data *at24; |
| 514 | int err; |
Bartosz Golaszewski | aa4ce22 | 2017-12-13 11:56:23 +0100 | [diff] [blame] | 515 | unsigned int i, num_addresses; |
Bartosz Golaszewski | eef6939 | 2017-12-18 18:24:43 +0100 | [diff] [blame] | 516 | struct regmap_config regmap_config = { }; |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 517 | u8 test_byte; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 518 | |
| 519 | if (client->dev.platform_data) { |
| 520 | chip = *(struct at24_platform_data *)client->dev.platform_data; |
| 521 | } else { |
Javier Martinez Canillas | 7f2a2f0 | 2017-10-01 12:49:48 +0200 | [diff] [blame] | 522 | /* |
| 523 | * The I2C core allows OF nodes compatibles to match against the |
| 524 | * I2C device ID table as a fallback, so check not only if an OF |
| 525 | * node is present but also if it matches an OF device ID entry. |
| 526 | */ |
| 527 | if (client->dev.of_node && |
| 528 | of_match_device(at24_of_match, &client->dev)) { |
Sven Van Asbroeck | b680f4f | 2017-12-20 11:48:56 -0500 | [diff] [blame] | 529 | cd = of_device_get_match_data(&client->dev); |
Javier Martinez Canillas | 7f2a2f0 | 2017-10-01 12:49:48 +0200 | [diff] [blame] | 530 | } else if (id) { |
Sven Van Asbroeck | b680f4f | 2017-12-20 11:48:56 -0500 | [diff] [blame] | 531 | cd = (void *)id->driver_data; |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 532 | } else { |
| 533 | const struct acpi_device_id *aid; |
| 534 | |
| 535 | aid = acpi_match_device(at24_acpi_ids, &client->dev); |
| 536 | if (aid) |
Sven Van Asbroeck | b680f4f | 2017-12-20 11:48:56 -0500 | [diff] [blame] | 537 | cd = (void *)aid->driver_data; |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 538 | } |
Sven Van Asbroeck | b680f4f | 2017-12-20 11:48:56 -0500 | [diff] [blame] | 539 | if (!cd) |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 540 | return -ENODEV; |
| 541 | |
Sven Van Asbroeck | b680f4f | 2017-12-20 11:48:56 -0500 | [diff] [blame] | 542 | chip.byte_len = cd->byte_len; |
| 543 | chip.flags = cd->flags; |
Ben Gardner | dd905a6 | 2017-02-09 11:36:08 -0600 | [diff] [blame] | 544 | at24_get_pdata(&client->dev, &chip); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | if (!is_power_of_2(chip.byte_len)) |
| 548 | dev_warn(&client->dev, |
| 549 | "byte_len looks suspicious (no power of 2)!\n"); |
Wolfram Sang | 45efe84 | 2010-11-17 13:00:49 +0100 | [diff] [blame] | 550 | if (!chip.page_size) { |
| 551 | dev_err(&client->dev, "page_size must not be 0!\n"); |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 552 | return -EINVAL; |
Wolfram Sang | 45efe84 | 2010-11-17 13:00:49 +0100 | [diff] [blame] | 553 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 554 | if (!is_power_of_2(chip.page_size)) |
| 555 | dev_warn(&client->dev, |
| 556 | "page_size looks suspicious (no power of 2)!\n"); |
| 557 | |
Heiner Kallweit | a23727c | 2017-11-28 21:51:54 +0100 | [diff] [blame] | 558 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C) && |
| 559 | !i2c_check_functionality(client->adapter, |
| 560 | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) |
| 561 | chip.page_size = 1; |
Christian Gmeiner | a839ce6 | 2014-10-09 11:07:58 +0200 | [diff] [blame] | 562 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 563 | if (chip.flags & AT24_FLAG_TAKE8ADDR) |
| 564 | num_addresses = 8; |
| 565 | else |
| 566 | num_addresses = DIV_ROUND_UP(chip.byte_len, |
| 567 | (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256); |
| 568 | |
Bartosz Golaszewski | eef6939 | 2017-12-18 18:24:43 +0100 | [diff] [blame] | 569 | regmap_config.val_bits = 8; |
| 570 | regmap_config.reg_bits = (chip.flags & AT24_FLAG_ADDR16) ? 16 : 8; |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 571 | |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 572 | at24 = devm_kzalloc(&client->dev, sizeof(struct at24_data) + |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 573 | num_addresses * sizeof(struct at24_client), GFP_KERNEL); |
Nikolay Balandin | f0ac236 | 2013-05-28 13:00:20 -0700 | [diff] [blame] | 574 | if (!at24) |
| 575 | return -ENOMEM; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 576 | |
| 577 | mutex_init(&at24->lock); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 578 | at24->chip = chip; |
| 579 | at24->num_addresses = num_addresses; |
Heiner Kallweit | 4bb5c13c | 2017-11-28 21:51:50 +0100 | [diff] [blame] | 580 | at24->offset_adj = at24_get_offset_adj(chip.flags, chip.byte_len); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 581 | |
Bartosz Golaszewski | 6ce261e | 2017-12-19 11:28:54 +0100 | [diff] [blame^] | 582 | at24->wp_gpio = devm_gpiod_get_optional(&client->dev, |
| 583 | "wp", GPIOD_OUT_HIGH); |
| 584 | if (IS_ERR(at24->wp_gpio)) |
| 585 | return PTR_ERR(at24->wp_gpio); |
| 586 | |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 587 | at24->client[0].client = client; |
Bartosz Golaszewski | eef6939 | 2017-12-18 18:24:43 +0100 | [diff] [blame] | 588 | at24->client[0].regmap = devm_regmap_init_i2c(client, ®map_config); |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 589 | if (IS_ERR(at24->client[0].regmap)) |
| 590 | return PTR_ERR(at24->client[0].regmap); |
| 591 | |
Bartosz Golaszewski | 0b81365 | 2016-06-06 10:48:54 +0200 | [diff] [blame] | 592 | if ((chip.flags & AT24_FLAG_SERIAL) && (chip.flags & AT24_FLAG_MAC)) { |
| 593 | dev_err(&client->dev, |
| 594 | "invalid device data - cannot have both AT24_FLAG_SERIAL & AT24_FLAG_MAC."); |
| 595 | return -EINVAL; |
| 596 | } |
| 597 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 598 | writable = !(chip.flags & AT24_FLAG_READONLY); |
| 599 | if (writable) { |
Bartosz Golaszewski | ec3c2d5 | 2017-12-18 18:16:46 +0100 | [diff] [blame] | 600 | at24->write_max = min_t(unsigned int, |
| 601 | chip.page_size, at24_io_limit); |
Heiner Kallweit | a23727c | 2017-11-28 21:51:54 +0100 | [diff] [blame] | 602 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C) && |
| 603 | at24->write_max > I2C_SMBUS_BLOCK_MAX) |
| 604 | at24->write_max = I2C_SMBUS_BLOCK_MAX; |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 605 | } |
| 606 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 607 | /* use dummy devices for multiple-address chips */ |
| 608 | for (i = 1; i < num_addresses; i++) { |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 609 | at24->client[i].client = i2c_new_dummy(client->adapter, |
| 610 | client->addr + i); |
| 611 | if (!at24->client[i].client) { |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 612 | dev_err(&client->dev, "address 0x%02x unavailable\n", |
| 613 | client->addr + i); |
| 614 | err = -EADDRINUSE; |
| 615 | goto err_clients; |
| 616 | } |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 617 | at24->client[i].regmap = devm_regmap_init_i2c( |
Bartosz Golaszewski | eef6939 | 2017-12-18 18:24:43 +0100 | [diff] [blame] | 618 | at24->client[i].client, |
| 619 | ®map_config); |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 620 | if (IS_ERR(at24->client[i].regmap)) { |
| 621 | err = PTR_ERR(at24->client[i].regmap); |
| 622 | goto err_clients; |
| 623 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 624 | } |
| 625 | |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 626 | i2c_set_clientdata(client, at24); |
| 627 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 628 | /* enable runtime pm */ |
| 629 | pm_runtime_set_active(&client->dev); |
| 630 | pm_runtime_enable(&client->dev); |
| 631 | |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 632 | /* |
| 633 | * Perform a one-byte test read to verify that the |
| 634 | * chip is functional. |
| 635 | */ |
| 636 | err = at24_read(at24, 0, &test_byte, 1); |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 637 | pm_runtime_idle(&client->dev); |
Bartosz Golaszewski | 00f0ea7 | 2016-08-12 13:32:57 +0200 | [diff] [blame] | 638 | if (err) { |
| 639 | err = -ENODEV; |
| 640 | goto err_clients; |
| 641 | } |
| 642 | |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 643 | at24->nvmem_config.name = dev_name(&client->dev); |
| 644 | at24->nvmem_config.dev = &client->dev; |
| 645 | at24->nvmem_config.read_only = !writable; |
| 646 | at24->nvmem_config.root_only = true; |
| 647 | at24->nvmem_config.owner = THIS_MODULE; |
| 648 | at24->nvmem_config.compat = true; |
| 649 | at24->nvmem_config.base_dev = &client->dev; |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 650 | at24->nvmem_config.reg_read = at24_read; |
| 651 | at24->nvmem_config.reg_write = at24_write; |
| 652 | at24->nvmem_config.priv = at24; |
David Lechner | 7f6d2ec | 2017-12-03 19:54:41 -0600 | [diff] [blame] | 653 | at24->nvmem_config.stride = 1; |
Srinivas Kandagatla | cf0361a | 2016-04-24 20:28:06 +0100 | [diff] [blame] | 654 | at24->nvmem_config.word_size = 1; |
| 655 | at24->nvmem_config.size = chip.byte_len; |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 656 | |
| 657 | at24->nvmem = nvmem_register(&at24->nvmem_config); |
| 658 | |
| 659 | if (IS_ERR(at24->nvmem)) { |
| 660 | err = PTR_ERR(at24->nvmem); |
| 661 | goto err_clients; |
| 662 | } |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 663 | |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 664 | dev_info(&client->dev, "%u byte %s EEPROM, %s, %u bytes/write\n", |
| 665 | chip.byte_len, client->name, |
Wolfram Sang | 9ed030d | 2010-11-17 13:00:48 +0100 | [diff] [blame] | 666 | writable ? "writable" : "read-only", at24->write_max); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 667 | |
Kevin Hilman | 7274ec8 | 2009-04-02 16:56:57 -0700 | [diff] [blame] | 668 | /* export data to kernel code */ |
| 669 | if (chip.setup) |
Andrew Lunn | bec3c11 | 2016-02-26 20:59:24 +0100 | [diff] [blame] | 670 | chip.setup(at24->nvmem, chip.context); |
Kevin Hilman | 7274ec8 | 2009-04-02 16:56:57 -0700 | [diff] [blame] | 671 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 672 | return 0; |
| 673 | |
| 674 | err_clients: |
| 675 | for (i = 1; i < num_addresses; i++) |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 676 | if (at24->client[i].client) |
| 677 | i2c_unregister_device(at24->client[i].client); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 678 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 679 | pm_runtime_disable(&client->dev); |
| 680 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 681 | return err; |
| 682 | } |
| 683 | |
Bill Pemberton | 486a5c2 | 2012-11-19 13:26:02 -0500 | [diff] [blame] | 684 | static int at24_remove(struct i2c_client *client) |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 685 | { |
| 686 | struct at24_data *at24; |
| 687 | int i; |
| 688 | |
| 689 | at24 = i2c_get_clientdata(client); |
Andrew Lunn | 57d1555 | 2016-02-26 20:59:20 +0100 | [diff] [blame] | 690 | |
| 691 | nvmem_unregister(at24->nvmem); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 692 | |
| 693 | for (i = 1; i < at24->num_addresses; i++) |
Heiner Kallweit | 5c01525 | 2017-11-28 21:51:40 +0100 | [diff] [blame] | 694 | i2c_unregister_device(at24->client[i].client); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 695 | |
Divagar Mohandass | 98e8201 | 2017-10-10 11:30:37 +0530 | [diff] [blame] | 696 | pm_runtime_disable(&client->dev); |
| 697 | pm_runtime_set_suspended(&client->dev); |
| 698 | |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | /*-------------------------------------------------------------------------*/ |
| 703 | |
| 704 | static struct i2c_driver at24_driver = { |
| 705 | .driver = { |
| 706 | .name = "at24", |
Javier Martinez Canillas | 7f2a2f0 | 2017-10-01 12:49:48 +0200 | [diff] [blame] | 707 | .of_match_table = at24_of_match, |
Andy Shevchenko | 40d8edc | 2015-10-23 12:16:44 +0300 | [diff] [blame] | 708 | .acpi_match_table = ACPI_PTR(at24_acpi_ids), |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 709 | }, |
| 710 | .probe = at24_probe, |
Bill Pemberton | 2d6bed9 | 2012-11-19 13:21:23 -0500 | [diff] [blame] | 711 | .remove = at24_remove, |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 712 | .id_table = at24_ids, |
| 713 | }; |
| 714 | |
| 715 | static int __init at24_init(void) |
| 716 | { |
Bartosz Golaszewski | ec3c2d5 | 2017-12-18 18:16:46 +0100 | [diff] [blame] | 717 | if (!at24_io_limit) { |
| 718 | pr_err("at24: at24_io_limit must not be 0!\n"); |
Wolfram Sang | 45efe84 | 2010-11-17 13:00:49 +0100 | [diff] [blame] | 719 | return -EINVAL; |
| 720 | } |
| 721 | |
Bartosz Golaszewski | ec3c2d5 | 2017-12-18 18:16:46 +0100 | [diff] [blame] | 722 | at24_io_limit = rounddown_pow_of_two(at24_io_limit); |
Wolfram Sang | 2b7a505 | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 723 | return i2c_add_driver(&at24_driver); |
| 724 | } |
| 725 | module_init(at24_init); |
| 726 | |
| 727 | static void __exit at24_exit(void) |
| 728 | { |
| 729 | i2c_del_driver(&at24_driver); |
| 730 | } |
| 731 | module_exit(at24_exit); |
| 732 | |
| 733 | MODULE_DESCRIPTION("Driver for most I2C EEPROMs"); |
| 734 | MODULE_AUTHOR("David Brownell and Wolfram Sang"); |
| 735 | MODULE_LICENSE("GPL"); |