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