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