Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Driver for 93xx46 EEPROMs |
| 4 | * |
| 5 | * (C) 2011 DENX Software Engineering, Anatolij Gustschin <agust@denx.de> |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/delay.h> |
| 9 | #include <linux/device.h> |
Cory Tusar | 3ca9b1a | 2016-02-10 14:32:08 -0500 | [diff] [blame] | 10 | #include <linux/gpio/consumer.h> |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Emmanuel Gil Peyrot | 4a5ff99 | 2021-05-11 23:07:24 +0200 | [diff] [blame] | 12 | #include <linux/log2.h> |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/mutex.h> |
Cory Tusar | c074abe | 2016-01-06 22:55:02 -0500 | [diff] [blame] | 15 | #include <linux/of.h> |
| 16 | #include <linux/of_device.h> |
Cory Tusar | 3ca9b1a | 2016-02-10 14:32:08 -0500 | [diff] [blame] | 17 | #include <linux/of_gpio.h> |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 18 | #include <linux/slab.h> |
| 19 | #include <linux/spi/spi.h> |
Andrew Lunn | 1c4b6e2 | 2016-02-26 20:59:23 +0100 | [diff] [blame] | 20 | #include <linux/nvmem-provider.h> |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 21 | #include <linux/eeprom_93xx46.h> |
| 22 | |
| 23 | #define OP_START 0x4 |
| 24 | #define OP_WRITE (OP_START | 0x1) |
| 25 | #define OP_READ (OP_START | 0x2) |
| 26 | #define ADDR_EWDS 0x00 |
| 27 | #define ADDR_ERAL 0x20 |
| 28 | #define ADDR_EWEN 0x30 |
| 29 | |
Cory Tusar | e1379b5 | 2016-02-10 14:32:07 -0500 | [diff] [blame] | 30 | struct eeprom_93xx46_devtype_data { |
| 31 | unsigned int quirks; |
Emmanuel Gil Peyrot | 14374fb | 2021-05-11 23:07:25 +0200 | [diff] [blame] | 32 | unsigned char flags; |
| 33 | }; |
| 34 | |
| 35 | static const struct eeprom_93xx46_devtype_data at93c46_data = { |
| 36 | .flags = EE_SIZE1K, |
| 37 | }; |
| 38 | |
| 39 | static const struct eeprom_93xx46_devtype_data at93c56_data = { |
| 40 | .flags = EE_SIZE2K, |
| 41 | }; |
| 42 | |
| 43 | static const struct eeprom_93xx46_devtype_data at93c66_data = { |
| 44 | .flags = EE_SIZE4K, |
Cory Tusar | e1379b5 | 2016-02-10 14:32:07 -0500 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static const struct eeprom_93xx46_devtype_data atmel_at93c46d_data = { |
Emmanuel Gil Peyrot | 14374fb | 2021-05-11 23:07:25 +0200 | [diff] [blame] | 48 | .flags = EE_SIZE1K, |
Cory Tusar | e1379b5 | 2016-02-10 14:32:07 -0500 | [diff] [blame] | 49 | .quirks = EEPROM_93XX46_QUIRK_SINGLE_WORD_READ | |
| 50 | EEPROM_93XX46_QUIRK_INSTRUCTION_LENGTH, |
| 51 | }; |
| 52 | |
Aswath Govindraju | f6f1f8e | 2021-01-05 16:28:12 +0530 | [diff] [blame] | 53 | static const struct eeprom_93xx46_devtype_data microchip_93lc46b_data = { |
Emmanuel Gil Peyrot | 14374fb | 2021-05-11 23:07:25 +0200 | [diff] [blame] | 54 | .flags = EE_SIZE1K, |
Aswath Govindraju | f6f1f8e | 2021-01-05 16:28:12 +0530 | [diff] [blame] | 55 | .quirks = EEPROM_93XX46_QUIRK_EXTRA_READ_CYCLE, |
| 56 | }; |
| 57 | |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 58 | struct eeprom_93xx46_dev { |
| 59 | struct spi_device *spi; |
| 60 | struct eeprom_93xx46_platform_data *pdata; |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 61 | struct mutex lock; |
Andrew Lunn | 1c4b6e2 | 2016-02-26 20:59:23 +0100 | [diff] [blame] | 62 | struct nvmem_config nvmem_config; |
| 63 | struct nvmem_device *nvmem; |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 64 | int addrlen; |
Andrew Lunn | 1c4b6e2 | 2016-02-26 20:59:23 +0100 | [diff] [blame] | 65 | int size; |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Cory Tusar | e1379b5 | 2016-02-10 14:32:07 -0500 | [diff] [blame] | 68 | static inline bool has_quirk_single_word_read(struct eeprom_93xx46_dev *edev) |
| 69 | { |
| 70 | return edev->pdata->quirks & EEPROM_93XX46_QUIRK_SINGLE_WORD_READ; |
| 71 | } |
| 72 | |
| 73 | static inline bool has_quirk_instruction_length(struct eeprom_93xx46_dev *edev) |
| 74 | { |
| 75 | return edev->pdata->quirks & EEPROM_93XX46_QUIRK_INSTRUCTION_LENGTH; |
| 76 | } |
| 77 | |
Aswath Govindraju | f6f1f8e | 2021-01-05 16:28:12 +0530 | [diff] [blame] | 78 | static inline bool has_quirk_extra_read_cycle(struct eeprom_93xx46_dev *edev) |
| 79 | { |
| 80 | return edev->pdata->quirks & EEPROM_93XX46_QUIRK_EXTRA_READ_CYCLE; |
| 81 | } |
| 82 | |
Srinivas Kandagatla | a8ab316 | 2016-04-24 20:28:16 +0100 | [diff] [blame] | 83 | static int eeprom_93xx46_read(void *priv, unsigned int off, |
| 84 | void *val, size_t count) |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 85 | { |
Srinivas Kandagatla | a8ab316 | 2016-04-24 20:28:16 +0100 | [diff] [blame] | 86 | struct eeprom_93xx46_dev *edev = priv; |
| 87 | char *buf = val; |
| 88 | int err = 0; |
Emmanuel Gil Peyrot | 4a5ff99 | 2021-05-11 23:07:24 +0200 | [diff] [blame] | 89 | int bits; |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 90 | |
Andrew Lunn | 1c4b6e2 | 2016-02-26 20:59:23 +0100 | [diff] [blame] | 91 | if (unlikely(off >= edev->size)) |
| 92 | return 0; |
| 93 | if ((off + count) > edev->size) |
| 94 | count = edev->size - off; |
| 95 | if (unlikely(!count)) |
| 96 | return count; |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 97 | |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 98 | mutex_lock(&edev->lock); |
| 99 | |
| 100 | if (edev->pdata->prepare) |
| 101 | edev->pdata->prepare(edev); |
| 102 | |
Emmanuel Gil Peyrot | 4a5ff99 | 2021-05-11 23:07:24 +0200 | [diff] [blame] | 103 | /* The opcode in front of the address is three bits. */ |
| 104 | bits = edev->addrlen + 3; |
| 105 | |
Cory Tusar | e1379b5 | 2016-02-10 14:32:07 -0500 | [diff] [blame] | 106 | while (count) { |
| 107 | struct spi_message m; |
| 108 | struct spi_transfer t[2] = { { 0 } }; |
| 109 | u16 cmd_addr = OP_READ << edev->addrlen; |
| 110 | size_t nbytes = count; |
Cory Tusar | e1379b5 | 2016-02-10 14:32:07 -0500 | [diff] [blame] | 111 | |
Emmanuel Gil Peyrot | 4a5ff99 | 2021-05-11 23:07:24 +0200 | [diff] [blame] | 112 | if (edev->pdata->flags & EE_ADDR8) { |
| 113 | cmd_addr |= off; |
Cory Tusar | e1379b5 | 2016-02-10 14:32:07 -0500 | [diff] [blame] | 114 | if (has_quirk_single_word_read(edev)) |
| 115 | nbytes = 1; |
| 116 | } else { |
Emmanuel Gil Peyrot | 4a5ff99 | 2021-05-11 23:07:24 +0200 | [diff] [blame] | 117 | cmd_addr |= (off >> 1); |
Cory Tusar | e1379b5 | 2016-02-10 14:32:07 -0500 | [diff] [blame] | 118 | if (has_quirk_single_word_read(edev)) |
| 119 | nbytes = 2; |
| 120 | } |
| 121 | |
| 122 | dev_dbg(&edev->spi->dev, "read cmd 0x%x, %d Hz\n", |
| 123 | cmd_addr, edev->spi->max_speed_hz); |
| 124 | |
Aswath Govindraju | f6f1f8e | 2021-01-05 16:28:12 +0530 | [diff] [blame] | 125 | if (has_quirk_extra_read_cycle(edev)) { |
| 126 | cmd_addr <<= 1; |
| 127 | bits += 1; |
| 128 | } |
| 129 | |
Cory Tusar | e1379b5 | 2016-02-10 14:32:07 -0500 | [diff] [blame] | 130 | spi_message_init(&m); |
| 131 | |
| 132 | t[0].tx_buf = (char *)&cmd_addr; |
| 133 | t[0].len = 2; |
| 134 | t[0].bits_per_word = bits; |
| 135 | spi_message_add_tail(&t[0], &m); |
| 136 | |
| 137 | t[1].rx_buf = buf; |
| 138 | t[1].len = count; |
| 139 | t[1].bits_per_word = 8; |
| 140 | spi_message_add_tail(&t[1], &m); |
| 141 | |
| 142 | err = spi_sync(edev->spi, &m); |
| 143 | /* have to wait at least Tcsl ns */ |
| 144 | ndelay(250); |
| 145 | |
| 146 | if (err) { |
| 147 | dev_err(&edev->spi->dev, "read %zu bytes at %d: err. %d\n", |
| 148 | nbytes, (int)off, err); |
Cory Tusar | e1379b5 | 2016-02-10 14:32:07 -0500 | [diff] [blame] | 149 | break; |
| 150 | } |
| 151 | |
| 152 | buf += nbytes; |
| 153 | off += nbytes; |
| 154 | count -= nbytes; |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | if (edev->pdata->finish) |
| 158 | edev->pdata->finish(edev); |
| 159 | |
| 160 | mutex_unlock(&edev->lock); |
Srinivas Kandagatla | a8ab316 | 2016-04-24 20:28:16 +0100 | [diff] [blame] | 161 | |
| 162 | return err; |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static int eeprom_93xx46_ew(struct eeprom_93xx46_dev *edev, int is_on) |
| 166 | { |
| 167 | struct spi_message m; |
| 168 | struct spi_transfer t; |
| 169 | int bits, ret; |
| 170 | u16 cmd_addr; |
| 171 | |
Emmanuel Gil Peyrot | 4a5ff99 | 2021-05-11 23:07:24 +0200 | [diff] [blame] | 172 | /* The opcode in front of the address is three bits. */ |
| 173 | bits = edev->addrlen + 3; |
| 174 | |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 175 | cmd_addr = OP_START << edev->addrlen; |
Emmanuel Gil Peyrot | 4a5ff99 | 2021-05-11 23:07:24 +0200 | [diff] [blame] | 176 | if (edev->pdata->flags & EE_ADDR8) |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 177 | cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS) << 1; |
Emmanuel Gil Peyrot | 4a5ff99 | 2021-05-11 23:07:24 +0200 | [diff] [blame] | 178 | else |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 179 | cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS); |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 180 | |
Cory Tusar | e1379b5 | 2016-02-10 14:32:07 -0500 | [diff] [blame] | 181 | if (has_quirk_instruction_length(edev)) { |
| 182 | cmd_addr <<= 2; |
| 183 | bits += 2; |
| 184 | } |
| 185 | |
| 186 | dev_dbg(&edev->spi->dev, "ew%s cmd 0x%04x, %d bits\n", |
| 187 | is_on ? "en" : "ds", cmd_addr, bits); |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 188 | |
| 189 | spi_message_init(&m); |
| 190 | memset(&t, 0, sizeof(t)); |
| 191 | |
| 192 | t.tx_buf = &cmd_addr; |
| 193 | t.len = 2; |
| 194 | t.bits_per_word = bits; |
| 195 | spi_message_add_tail(&t, &m); |
| 196 | |
| 197 | mutex_lock(&edev->lock); |
| 198 | |
| 199 | if (edev->pdata->prepare) |
| 200 | edev->pdata->prepare(edev); |
| 201 | |
| 202 | ret = spi_sync(edev->spi, &m); |
| 203 | /* have to wait at least Tcsl ns */ |
| 204 | ndelay(250); |
| 205 | if (ret) |
| 206 | dev_err(&edev->spi->dev, "erase/write %sable error %d\n", |
| 207 | is_on ? "en" : "dis", ret); |
| 208 | |
| 209 | if (edev->pdata->finish) |
| 210 | edev->pdata->finish(edev); |
| 211 | |
| 212 | mutex_unlock(&edev->lock); |
| 213 | return ret; |
| 214 | } |
| 215 | |
| 216 | static ssize_t |
| 217 | eeprom_93xx46_write_word(struct eeprom_93xx46_dev *edev, |
| 218 | const char *buf, unsigned off) |
| 219 | { |
| 220 | struct spi_message m; |
| 221 | struct spi_transfer t[2]; |
| 222 | int bits, data_len, ret; |
| 223 | u16 cmd_addr; |
| 224 | |
Emmanuel Gil Peyrot | 4a5ff99 | 2021-05-11 23:07:24 +0200 | [diff] [blame] | 225 | if (unlikely(off >= edev->size)) |
| 226 | return -EINVAL; |
| 227 | |
| 228 | /* The opcode in front of the address is three bits. */ |
| 229 | bits = edev->addrlen + 3; |
| 230 | |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 231 | cmd_addr = OP_WRITE << edev->addrlen; |
| 232 | |
Emmanuel Gil Peyrot | 4a5ff99 | 2021-05-11 23:07:24 +0200 | [diff] [blame] | 233 | if (edev->pdata->flags & EE_ADDR8) { |
| 234 | cmd_addr |= off; |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 235 | data_len = 1; |
| 236 | } else { |
Emmanuel Gil Peyrot | 4a5ff99 | 2021-05-11 23:07:24 +0200 | [diff] [blame] | 237 | cmd_addr |= (off >> 1); |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 238 | data_len = 2; |
| 239 | } |
| 240 | |
| 241 | dev_dbg(&edev->spi->dev, "write cmd 0x%x\n", cmd_addr); |
| 242 | |
| 243 | spi_message_init(&m); |
| 244 | memset(t, 0, sizeof(t)); |
| 245 | |
| 246 | t[0].tx_buf = (char *)&cmd_addr; |
| 247 | t[0].len = 2; |
| 248 | t[0].bits_per_word = bits; |
| 249 | spi_message_add_tail(&t[0], &m); |
| 250 | |
| 251 | t[1].tx_buf = buf; |
| 252 | t[1].len = data_len; |
| 253 | t[1].bits_per_word = 8; |
| 254 | spi_message_add_tail(&t[1], &m); |
| 255 | |
| 256 | ret = spi_sync(edev->spi, &m); |
| 257 | /* have to wait program cycle time Twc ms */ |
| 258 | mdelay(6); |
| 259 | return ret; |
| 260 | } |
| 261 | |
Srinivas Kandagatla | a8ab316 | 2016-04-24 20:28:16 +0100 | [diff] [blame] | 262 | static int eeprom_93xx46_write(void *priv, unsigned int off, |
| 263 | void *val, size_t count) |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 264 | { |
Srinivas Kandagatla | a8ab316 | 2016-04-24 20:28:16 +0100 | [diff] [blame] | 265 | struct eeprom_93xx46_dev *edev = priv; |
| 266 | char *buf = val; |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 267 | int i, ret, step = 1; |
| 268 | |
Andrew Lunn | 1c4b6e2 | 2016-02-26 20:59:23 +0100 | [diff] [blame] | 269 | if (unlikely(off >= edev->size)) |
| 270 | return -EFBIG; |
| 271 | if ((off + count) > edev->size) |
| 272 | count = edev->size - off; |
| 273 | if (unlikely(!count)) |
| 274 | return count; |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 275 | |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 276 | /* only write even number of bytes on 16-bit devices */ |
Emmanuel Gil Peyrot | 4a5ff99 | 2021-05-11 23:07:24 +0200 | [diff] [blame] | 277 | if (edev->pdata->flags & EE_ADDR16) { |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 278 | step = 2; |
| 279 | count &= ~1; |
| 280 | } |
| 281 | |
| 282 | /* erase/write enable */ |
| 283 | ret = eeprom_93xx46_ew(edev, 1); |
| 284 | if (ret) |
| 285 | return ret; |
| 286 | |
| 287 | mutex_lock(&edev->lock); |
| 288 | |
| 289 | if (edev->pdata->prepare) |
| 290 | edev->pdata->prepare(edev); |
| 291 | |
| 292 | for (i = 0; i < count; i += step) { |
| 293 | ret = eeprom_93xx46_write_word(edev, &buf[i], off + i); |
| 294 | if (ret) { |
| 295 | dev_err(&edev->spi->dev, "write failed at %d: %d\n", |
| 296 | (int)off + i, ret); |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | if (edev->pdata->finish) |
| 302 | edev->pdata->finish(edev); |
| 303 | |
| 304 | mutex_unlock(&edev->lock); |
| 305 | |
| 306 | /* erase/write disable */ |
| 307 | eeprom_93xx46_ew(edev, 0); |
Srinivas Kandagatla | a8ab316 | 2016-04-24 20:28:16 +0100 | [diff] [blame] | 308 | return ret; |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static int eeprom_93xx46_eral(struct eeprom_93xx46_dev *edev) |
| 312 | { |
| 313 | struct eeprom_93xx46_platform_data *pd = edev->pdata; |
| 314 | struct spi_message m; |
| 315 | struct spi_transfer t; |
| 316 | int bits, ret; |
| 317 | u16 cmd_addr; |
| 318 | |
Emmanuel Gil Peyrot | 4a5ff99 | 2021-05-11 23:07:24 +0200 | [diff] [blame] | 319 | /* The opcode in front of the address is three bits. */ |
| 320 | bits = edev->addrlen + 3; |
| 321 | |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 322 | cmd_addr = OP_START << edev->addrlen; |
Emmanuel Gil Peyrot | 4a5ff99 | 2021-05-11 23:07:24 +0200 | [diff] [blame] | 323 | if (edev->pdata->flags & EE_ADDR8) |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 324 | cmd_addr |= ADDR_ERAL << 1; |
Emmanuel Gil Peyrot | 4a5ff99 | 2021-05-11 23:07:24 +0200 | [diff] [blame] | 325 | else |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 326 | cmd_addr |= ADDR_ERAL; |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 327 | |
Cory Tusar | e1379b5 | 2016-02-10 14:32:07 -0500 | [diff] [blame] | 328 | if (has_quirk_instruction_length(edev)) { |
| 329 | cmd_addr <<= 2; |
| 330 | bits += 2; |
| 331 | } |
| 332 | |
| 333 | dev_dbg(&edev->spi->dev, "eral cmd 0x%04x, %d bits\n", cmd_addr, bits); |
| 334 | |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 335 | spi_message_init(&m); |
| 336 | memset(&t, 0, sizeof(t)); |
| 337 | |
| 338 | t.tx_buf = &cmd_addr; |
| 339 | t.len = 2; |
| 340 | t.bits_per_word = bits; |
| 341 | spi_message_add_tail(&t, &m); |
| 342 | |
| 343 | mutex_lock(&edev->lock); |
| 344 | |
| 345 | if (edev->pdata->prepare) |
| 346 | edev->pdata->prepare(edev); |
| 347 | |
| 348 | ret = spi_sync(edev->spi, &m); |
| 349 | if (ret) |
| 350 | dev_err(&edev->spi->dev, "erase error %d\n", ret); |
| 351 | /* have to wait erase cycle time Tec ms */ |
| 352 | mdelay(6); |
| 353 | |
| 354 | if (pd->finish) |
| 355 | pd->finish(edev); |
| 356 | |
| 357 | mutex_unlock(&edev->lock); |
| 358 | return ret; |
| 359 | } |
| 360 | |
| 361 | static ssize_t eeprom_93xx46_store_erase(struct device *dev, |
| 362 | struct device_attribute *attr, |
| 363 | const char *buf, size_t count) |
| 364 | { |
| 365 | struct eeprom_93xx46_dev *edev = dev_get_drvdata(dev); |
| 366 | int erase = 0, ret; |
| 367 | |
| 368 | sscanf(buf, "%d", &erase); |
| 369 | if (erase) { |
| 370 | ret = eeprom_93xx46_ew(edev, 1); |
| 371 | if (ret) |
| 372 | return ret; |
| 373 | ret = eeprom_93xx46_eral(edev); |
| 374 | if (ret) |
| 375 | return ret; |
| 376 | ret = eeprom_93xx46_ew(edev, 0); |
| 377 | if (ret) |
| 378 | return ret; |
| 379 | } |
| 380 | return count; |
| 381 | } |
| 382 | static DEVICE_ATTR(erase, S_IWUSR, NULL, eeprom_93xx46_store_erase); |
| 383 | |
Cory Tusar | 3ca9b1a | 2016-02-10 14:32:08 -0500 | [diff] [blame] | 384 | static void select_assert(void *context) |
| 385 | { |
| 386 | struct eeprom_93xx46_dev *edev = context; |
| 387 | |
| 388 | gpiod_set_value_cansleep(edev->pdata->select, 1); |
| 389 | } |
| 390 | |
| 391 | static void select_deassert(void *context) |
| 392 | { |
| 393 | struct eeprom_93xx46_dev *edev = context; |
| 394 | |
| 395 | gpiod_set_value_cansleep(edev->pdata->select, 0); |
| 396 | } |
| 397 | |
Cory Tusar | c074abe | 2016-01-06 22:55:02 -0500 | [diff] [blame] | 398 | static const struct of_device_id eeprom_93xx46_of_table[] = { |
Emmanuel Gil Peyrot | 14374fb | 2021-05-11 23:07:25 +0200 | [diff] [blame] | 399 | { .compatible = "eeprom-93xx46", .data = &at93c46_data, }, |
| 400 | { .compatible = "atmel,at93c46", .data = &at93c46_data, }, |
Cory Tusar | e1379b5 | 2016-02-10 14:32:07 -0500 | [diff] [blame] | 401 | { .compatible = "atmel,at93c46d", .data = &atmel_at93c46d_data, }, |
Emmanuel Gil Peyrot | 14374fb | 2021-05-11 23:07:25 +0200 | [diff] [blame] | 402 | { .compatible = "atmel,at93c56", .data = &at93c56_data, }, |
| 403 | { .compatible = "atmel,at93c66", .data = &at93c66_data, }, |
Aswath Govindraju | f6f1f8e | 2021-01-05 16:28:12 +0530 | [diff] [blame] | 404 | { .compatible = "microchip,93lc46b", .data = µchip_93lc46b_data, }, |
Cory Tusar | c074abe | 2016-01-06 22:55:02 -0500 | [diff] [blame] | 405 | {} |
| 406 | }; |
| 407 | MODULE_DEVICE_TABLE(of, eeprom_93xx46_of_table); |
| 408 | |
Mark Brown | 137879f | 2021-09-22 19:40:48 +0100 | [diff] [blame] | 409 | static const struct spi_device_id eeprom_93xx46_spi_ids[] = { |
| 410 | { .name = "eeprom-93xx46", |
| 411 | .driver_data = (kernel_ulong_t)&at93c46_data, }, |
| 412 | { .name = "at93c46", |
| 413 | .driver_data = (kernel_ulong_t)&at93c46_data, }, |
| 414 | { .name = "at93c46d", |
| 415 | .driver_data = (kernel_ulong_t)&atmel_at93c46d_data, }, |
| 416 | { .name = "at93c56", |
| 417 | .driver_data = (kernel_ulong_t)&at93c56_data, }, |
| 418 | { .name = "at93c66", |
| 419 | .driver_data = (kernel_ulong_t)&at93c66_data, }, |
| 420 | { .name = "93lc46b", |
| 421 | .driver_data = (kernel_ulong_t)µchip_93lc46b_data, }, |
| 422 | {} |
| 423 | }; |
Arnd Bergmann | f427527 | 2021-10-14 17:37:18 +0200 | [diff] [blame] | 424 | MODULE_DEVICE_TABLE(spi, eeprom_93xx46_spi_ids); |
Mark Brown | 137879f | 2021-09-22 19:40:48 +0100 | [diff] [blame] | 425 | |
Cory Tusar | c074abe | 2016-01-06 22:55:02 -0500 | [diff] [blame] | 426 | static int eeprom_93xx46_probe_dt(struct spi_device *spi) |
| 427 | { |
Cory Tusar | e1379b5 | 2016-02-10 14:32:07 -0500 | [diff] [blame] | 428 | const struct of_device_id *of_id = |
| 429 | of_match_device(eeprom_93xx46_of_table, &spi->dev); |
Cory Tusar | c074abe | 2016-01-06 22:55:02 -0500 | [diff] [blame] | 430 | struct device_node *np = spi->dev.of_node; |
| 431 | struct eeprom_93xx46_platform_data *pd; |
| 432 | u32 tmp; |
| 433 | int ret; |
| 434 | |
| 435 | pd = devm_kzalloc(&spi->dev, sizeof(*pd), GFP_KERNEL); |
| 436 | if (!pd) |
| 437 | return -ENOMEM; |
| 438 | |
| 439 | ret = of_property_read_u32(np, "data-size", &tmp); |
| 440 | if (ret < 0) { |
| 441 | dev_err(&spi->dev, "data-size property not found\n"); |
| 442 | return ret; |
| 443 | } |
| 444 | |
| 445 | if (tmp == 8) { |
| 446 | pd->flags |= EE_ADDR8; |
| 447 | } else if (tmp == 16) { |
| 448 | pd->flags |= EE_ADDR16; |
| 449 | } else { |
| 450 | dev_err(&spi->dev, "invalid data-size (%d)\n", tmp); |
| 451 | return -EINVAL; |
| 452 | } |
| 453 | |
| 454 | if (of_property_read_bool(np, "read-only")) |
| 455 | pd->flags |= EE_READONLY; |
| 456 | |
Fabio Estevam | 54ec602 | 2017-07-19 23:35:28 -0300 | [diff] [blame] | 457 | pd->select = devm_gpiod_get_optional(&spi->dev, "select", |
| 458 | GPIOD_OUT_LOW); |
| 459 | if (IS_ERR(pd->select)) |
| 460 | return PTR_ERR(pd->select); |
Cory Tusar | 3ca9b1a | 2016-02-10 14:32:08 -0500 | [diff] [blame] | 461 | |
Fabio Estevam | 54ec602 | 2017-07-19 23:35:28 -0300 | [diff] [blame] | 462 | pd->prepare = select_assert; |
| 463 | pd->finish = select_deassert; |
| 464 | gpiod_direction_output(pd->select, 0); |
Cory Tusar | 3ca9b1a | 2016-02-10 14:32:08 -0500 | [diff] [blame] | 465 | |
Cory Tusar | e1379b5 | 2016-02-10 14:32:07 -0500 | [diff] [blame] | 466 | if (of_id->data) { |
| 467 | const struct eeprom_93xx46_devtype_data *data = of_id->data; |
| 468 | |
| 469 | pd->quirks = data->quirks; |
Emmanuel Gil Peyrot | 14374fb | 2021-05-11 23:07:25 +0200 | [diff] [blame] | 470 | pd->flags |= data->flags; |
Cory Tusar | e1379b5 | 2016-02-10 14:32:07 -0500 | [diff] [blame] | 471 | } |
| 472 | |
Cory Tusar | c074abe | 2016-01-06 22:55:02 -0500 | [diff] [blame] | 473 | spi->dev.platform_data = pd; |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
Bill Pemberton | 80c8ae2 | 2012-11-19 13:23:05 -0500 | [diff] [blame] | 478 | static int eeprom_93xx46_probe(struct spi_device *spi) |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 479 | { |
| 480 | struct eeprom_93xx46_platform_data *pd; |
| 481 | struct eeprom_93xx46_dev *edev; |
| 482 | int err; |
| 483 | |
Cory Tusar | c074abe | 2016-01-06 22:55:02 -0500 | [diff] [blame] | 484 | if (spi->dev.of_node) { |
| 485 | err = eeprom_93xx46_probe_dt(spi); |
| 486 | if (err < 0) |
| 487 | return err; |
| 488 | } |
| 489 | |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 490 | pd = spi->dev.platform_data; |
| 491 | if (!pd) { |
| 492 | dev_err(&spi->dev, "missing platform data\n"); |
| 493 | return -ENODEV; |
| 494 | } |
| 495 | |
Bartosz Golaszewski | c853d69 | 2018-09-21 06:40:01 -0700 | [diff] [blame] | 496 | edev = devm_kzalloc(&spi->dev, sizeof(*edev), GFP_KERNEL); |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 497 | if (!edev) |
| 498 | return -ENOMEM; |
| 499 | |
Emmanuel Gil Peyrot | 14374fb | 2021-05-11 23:07:25 +0200 | [diff] [blame] | 500 | if (pd->flags & EE_SIZE1K) |
| 501 | edev->size = 128; |
| 502 | else if (pd->flags & EE_SIZE2K) |
| 503 | edev->size = 256; |
| 504 | else if (pd->flags & EE_SIZE4K) |
| 505 | edev->size = 512; |
| 506 | else { |
| 507 | dev_err(&spi->dev, "unspecified size\n"); |
| 508 | return -EINVAL; |
| 509 | } |
Emmanuel Gil Peyrot | 4a5ff99 | 2021-05-11 23:07:24 +0200 | [diff] [blame] | 510 | |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 511 | if (pd->flags & EE_ADDR8) |
Emmanuel Gil Peyrot | 4a5ff99 | 2021-05-11 23:07:24 +0200 | [diff] [blame] | 512 | edev->addrlen = ilog2(edev->size); |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 513 | else if (pd->flags & EE_ADDR16) |
Emmanuel Gil Peyrot | 4a5ff99 | 2021-05-11 23:07:24 +0200 | [diff] [blame] | 514 | edev->addrlen = ilog2(edev->size) - 1; |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 515 | else { |
| 516 | dev_err(&spi->dev, "unspecified address type\n"); |
Bartosz Golaszewski | c853d69 | 2018-09-21 06:40:01 -0700 | [diff] [blame] | 517 | return -EINVAL; |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | mutex_init(&edev->lock); |
| 521 | |
Mark Brown | dd69a18 | 2016-04-20 10:16:36 +0100 | [diff] [blame] | 522 | edev->spi = spi; |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 523 | edev->pdata = pd; |
| 524 | |
Vadym Kochan | 1d62a2c | 2020-09-16 20:09:33 +0300 | [diff] [blame] | 525 | edev->nvmem_config.type = NVMEM_TYPE_EEPROM; |
Andrew Lunn | 1c4b6e2 | 2016-02-26 20:59:23 +0100 | [diff] [blame] | 526 | edev->nvmem_config.name = dev_name(&spi->dev); |
| 527 | edev->nvmem_config.dev = &spi->dev; |
| 528 | edev->nvmem_config.read_only = pd->flags & EE_READONLY; |
| 529 | edev->nvmem_config.root_only = true; |
| 530 | edev->nvmem_config.owner = THIS_MODULE; |
| 531 | edev->nvmem_config.compat = true; |
| 532 | edev->nvmem_config.base_dev = &spi->dev; |
Srinivas Kandagatla | a8ab316 | 2016-04-24 20:28:16 +0100 | [diff] [blame] | 533 | edev->nvmem_config.reg_read = eeprom_93xx46_read; |
| 534 | edev->nvmem_config.reg_write = eeprom_93xx46_write; |
| 535 | edev->nvmem_config.priv = edev; |
| 536 | edev->nvmem_config.stride = 4; |
| 537 | edev->nvmem_config.word_size = 1; |
| 538 | edev->nvmem_config.size = edev->size; |
Andrew Lunn | 1c4b6e2 | 2016-02-26 20:59:23 +0100 | [diff] [blame] | 539 | |
Bartosz Golaszewski | c853d69 | 2018-09-21 06:40:01 -0700 | [diff] [blame] | 540 | edev->nvmem = devm_nvmem_register(&spi->dev, &edev->nvmem_config); |
| 541 | if (IS_ERR(edev->nvmem)) |
| 542 | return PTR_ERR(edev->nvmem); |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 543 | |
Emmanuel Gil Peyrot | 14374fb | 2021-05-11 23:07:25 +0200 | [diff] [blame] | 544 | dev_info(&spi->dev, "%d-bit eeprom containing %d bytes %s\n", |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 545 | (pd->flags & EE_ADDR8) ? 8 : 16, |
Emmanuel Gil Peyrot | 14374fb | 2021-05-11 23:07:25 +0200 | [diff] [blame] | 546 | edev->size, |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 547 | (pd->flags & EE_READONLY) ? "(readonly)" : ""); |
| 548 | |
| 549 | if (!(pd->flags & EE_READONLY)) { |
| 550 | if (device_create_file(&spi->dev, &dev_attr_erase)) |
| 551 | dev_err(&spi->dev, "can't create erase interface\n"); |
| 552 | } |
| 553 | |
Jingoo Han | 5ba75b5 | 2013-04-05 10:56:04 +0900 | [diff] [blame] | 554 | spi_set_drvdata(spi, edev); |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 555 | return 0; |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Bill Pemberton | 486a5c2 | 2012-11-19 13:26:02 -0500 | [diff] [blame] | 558 | static int eeprom_93xx46_remove(struct spi_device *spi) |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 559 | { |
Jingoo Han | 5ba75b5 | 2013-04-05 10:56:04 +0900 | [diff] [blame] | 560 | struct eeprom_93xx46_dev *edev = spi_get_drvdata(spi); |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 561 | |
| 562 | if (!(edev->pdata->flags & EE_READONLY)) |
| 563 | device_remove_file(&spi->dev, &dev_attr_erase); |
| 564 | |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static struct spi_driver eeprom_93xx46_driver = { |
| 569 | .driver = { |
| 570 | .name = "93xx46", |
Cory Tusar | c074abe | 2016-01-06 22:55:02 -0500 | [diff] [blame] | 571 | .of_match_table = of_match_ptr(eeprom_93xx46_of_table), |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 572 | }, |
| 573 | .probe = eeprom_93xx46_probe, |
Bill Pemberton | 2d6bed9 | 2012-11-19 13:21:23 -0500 | [diff] [blame] | 574 | .remove = eeprom_93xx46_remove, |
Mark Brown | 137879f | 2021-09-22 19:40:48 +0100 | [diff] [blame] | 575 | .id_table = eeprom_93xx46_spi_ids, |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 576 | }; |
| 577 | |
Axel Lin | a3dc3c9 | 2012-01-22 15:38:22 +0800 | [diff] [blame] | 578 | module_spi_driver(eeprom_93xx46_driver); |
Anatolij Gustschin | 06b4501 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 579 | |
| 580 | MODULE_LICENSE("GPL"); |
| 581 | MODULE_DESCRIPTION("Driver for 93xx46 EEPROMs"); |
| 582 | MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>"); |
Aswath Govindraju | 4540b9f | 2021-01-13 10:42:52 +0530 | [diff] [blame] | 583 | MODULE_ALIAS("spi:93xx46"); |
Aswath Govindraju | 13613a2 | 2021-01-07 22:09:53 +0530 | [diff] [blame] | 584 | MODULE_ALIAS("spi:eeprom-93xx46"); |
Aswath Govindraju | 47771f1 | 2021-01-07 22:09:54 +0530 | [diff] [blame] | 585 | MODULE_ALIAS("spi:93lc46b"); |