blob: 1f15399e5cb49199f5f4b467f8ff8ef8fb6771f8 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Anatolij Gustschin06b45012011-07-25 17:13:27 -07002/*
3 * Driver for 93xx46 EEPROMs
4 *
5 * (C) 2011 DENX Software Engineering, Anatolij Gustschin <agust@denx.de>
Anatolij Gustschin06b45012011-07-25 17:13:27 -07006 */
7
8#include <linux/delay.h>
9#include <linux/device.h>
Cory Tusar3ca9b1a2016-02-10 14:32:08 -050010#include <linux/gpio/consumer.h>
Anatolij Gustschin06b45012011-07-25 17:13:27 -070011#include <linux/kernel.h>
Emmanuel Gil Peyrot4a5ff992021-05-11 23:07:24 +020012#include <linux/log2.h>
Anatolij Gustschin06b45012011-07-25 17:13:27 -070013#include <linux/module.h>
14#include <linux/mutex.h>
Cory Tusarc074abe2016-01-06 22:55:02 -050015#include <linux/of.h>
16#include <linux/of_device.h>
Cory Tusar3ca9b1a2016-02-10 14:32:08 -050017#include <linux/of_gpio.h>
Anatolij Gustschin06b45012011-07-25 17:13:27 -070018#include <linux/slab.h>
19#include <linux/spi/spi.h>
Andrew Lunn1c4b6e22016-02-26 20:59:23 +010020#include <linux/nvmem-provider.h>
Anatolij Gustschin06b45012011-07-25 17:13:27 -070021#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 Tusare1379b52016-02-10 14:32:07 -050030struct eeprom_93xx46_devtype_data {
31 unsigned int quirks;
Emmanuel Gil Peyrot14374fb2021-05-11 23:07:25 +020032 unsigned char flags;
33};
34
35static const struct eeprom_93xx46_devtype_data at93c46_data = {
36 .flags = EE_SIZE1K,
37};
38
39static const struct eeprom_93xx46_devtype_data at93c56_data = {
40 .flags = EE_SIZE2K,
41};
42
43static const struct eeprom_93xx46_devtype_data at93c66_data = {
44 .flags = EE_SIZE4K,
Cory Tusare1379b52016-02-10 14:32:07 -050045};
46
47static const struct eeprom_93xx46_devtype_data atmel_at93c46d_data = {
Emmanuel Gil Peyrot14374fb2021-05-11 23:07:25 +020048 .flags = EE_SIZE1K,
Cory Tusare1379b52016-02-10 14:32:07 -050049 .quirks = EEPROM_93XX46_QUIRK_SINGLE_WORD_READ |
50 EEPROM_93XX46_QUIRK_INSTRUCTION_LENGTH,
51};
52
Aswath Govindrajuf6f1f8e2021-01-05 16:28:12 +053053static const struct eeprom_93xx46_devtype_data microchip_93lc46b_data = {
Emmanuel Gil Peyrot14374fb2021-05-11 23:07:25 +020054 .flags = EE_SIZE1K,
Aswath Govindrajuf6f1f8e2021-01-05 16:28:12 +053055 .quirks = EEPROM_93XX46_QUIRK_EXTRA_READ_CYCLE,
56};
57
Anatolij Gustschin06b45012011-07-25 17:13:27 -070058struct eeprom_93xx46_dev {
59 struct spi_device *spi;
60 struct eeprom_93xx46_platform_data *pdata;
Anatolij Gustschin06b45012011-07-25 17:13:27 -070061 struct mutex lock;
Andrew Lunn1c4b6e22016-02-26 20:59:23 +010062 struct nvmem_config nvmem_config;
63 struct nvmem_device *nvmem;
Anatolij Gustschin06b45012011-07-25 17:13:27 -070064 int addrlen;
Andrew Lunn1c4b6e22016-02-26 20:59:23 +010065 int size;
Anatolij Gustschin06b45012011-07-25 17:13:27 -070066};
67
Cory Tusare1379b52016-02-10 14:32:07 -050068static 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
73static 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 Govindrajuf6f1f8e2021-01-05 16:28:12 +053078static 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 Kandagatlaa8ab3162016-04-24 20:28:16 +010083static int eeprom_93xx46_read(void *priv, unsigned int off,
84 void *val, size_t count)
Anatolij Gustschin06b45012011-07-25 17:13:27 -070085{
Srinivas Kandagatlaa8ab3162016-04-24 20:28:16 +010086 struct eeprom_93xx46_dev *edev = priv;
87 char *buf = val;
88 int err = 0;
Emmanuel Gil Peyrot4a5ff992021-05-11 23:07:24 +020089 int bits;
Anatolij Gustschin06b45012011-07-25 17:13:27 -070090
Andrew Lunn1c4b6e22016-02-26 20:59:23 +010091 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 Gustschin06b45012011-07-25 17:13:27 -070097
Anatolij Gustschin06b45012011-07-25 17:13:27 -070098 mutex_lock(&edev->lock);
99
100 if (edev->pdata->prepare)
101 edev->pdata->prepare(edev);
102
Emmanuel Gil Peyrot4a5ff992021-05-11 23:07:24 +0200103 /* The opcode in front of the address is three bits. */
104 bits = edev->addrlen + 3;
105
Cory Tusare1379b52016-02-10 14:32:07 -0500106 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 Tusare1379b52016-02-10 14:32:07 -0500111
Emmanuel Gil Peyrot4a5ff992021-05-11 23:07:24 +0200112 if (edev->pdata->flags & EE_ADDR8) {
113 cmd_addr |= off;
Cory Tusare1379b52016-02-10 14:32:07 -0500114 if (has_quirk_single_word_read(edev))
115 nbytes = 1;
116 } else {
Emmanuel Gil Peyrot4a5ff992021-05-11 23:07:24 +0200117 cmd_addr |= (off >> 1);
Cory Tusare1379b52016-02-10 14:32:07 -0500118 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 Govindrajuf6f1f8e2021-01-05 16:28:12 +0530125 if (has_quirk_extra_read_cycle(edev)) {
126 cmd_addr <<= 1;
127 bits += 1;
128 }
129
Cory Tusare1379b52016-02-10 14:32:07 -0500130 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 Tusare1379b52016-02-10 14:32:07 -0500149 break;
150 }
151
152 buf += nbytes;
153 off += nbytes;
154 count -= nbytes;
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700155 }
156
157 if (edev->pdata->finish)
158 edev->pdata->finish(edev);
159
160 mutex_unlock(&edev->lock);
Srinivas Kandagatlaa8ab3162016-04-24 20:28:16 +0100161
162 return err;
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700163}
164
165static 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 Peyrot4a5ff992021-05-11 23:07:24 +0200172 /* The opcode in front of the address is three bits. */
173 bits = edev->addrlen + 3;
174
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700175 cmd_addr = OP_START << edev->addrlen;
Emmanuel Gil Peyrot4a5ff992021-05-11 23:07:24 +0200176 if (edev->pdata->flags & EE_ADDR8)
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700177 cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS) << 1;
Emmanuel Gil Peyrot4a5ff992021-05-11 23:07:24 +0200178 else
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700179 cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS);
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700180
Cory Tusare1379b52016-02-10 14:32:07 -0500181 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 Gustschin06b45012011-07-25 17:13:27 -0700188
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
216static ssize_t
217eeprom_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 Peyrot4a5ff992021-05-11 23:07:24 +0200225 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 Gustschin06b45012011-07-25 17:13:27 -0700231 cmd_addr = OP_WRITE << edev->addrlen;
232
Emmanuel Gil Peyrot4a5ff992021-05-11 23:07:24 +0200233 if (edev->pdata->flags & EE_ADDR8) {
234 cmd_addr |= off;
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700235 data_len = 1;
236 } else {
Emmanuel Gil Peyrot4a5ff992021-05-11 23:07:24 +0200237 cmd_addr |= (off >> 1);
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700238 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 Kandagatlaa8ab3162016-04-24 20:28:16 +0100262static int eeprom_93xx46_write(void *priv, unsigned int off,
263 void *val, size_t count)
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700264{
Srinivas Kandagatlaa8ab3162016-04-24 20:28:16 +0100265 struct eeprom_93xx46_dev *edev = priv;
266 char *buf = val;
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700267 int i, ret, step = 1;
268
Andrew Lunn1c4b6e22016-02-26 20:59:23 +0100269 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 Gustschin06b45012011-07-25 17:13:27 -0700275
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700276 /* only write even number of bytes on 16-bit devices */
Emmanuel Gil Peyrot4a5ff992021-05-11 23:07:24 +0200277 if (edev->pdata->flags & EE_ADDR16) {
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700278 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 Kandagatlaa8ab3162016-04-24 20:28:16 +0100308 return ret;
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700309}
310
311static 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 Peyrot4a5ff992021-05-11 23:07:24 +0200319 /* The opcode in front of the address is three bits. */
320 bits = edev->addrlen + 3;
321
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700322 cmd_addr = OP_START << edev->addrlen;
Emmanuel Gil Peyrot4a5ff992021-05-11 23:07:24 +0200323 if (edev->pdata->flags & EE_ADDR8)
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700324 cmd_addr |= ADDR_ERAL << 1;
Emmanuel Gil Peyrot4a5ff992021-05-11 23:07:24 +0200325 else
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700326 cmd_addr |= ADDR_ERAL;
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700327
Cory Tusare1379b52016-02-10 14:32:07 -0500328 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 Gustschin06b45012011-07-25 17:13:27 -0700335 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
361static 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}
382static DEVICE_ATTR(erase, S_IWUSR, NULL, eeprom_93xx46_store_erase);
383
Cory Tusar3ca9b1a2016-02-10 14:32:08 -0500384static 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
391static 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 Tusarc074abe2016-01-06 22:55:02 -0500398static const struct of_device_id eeprom_93xx46_of_table[] = {
Emmanuel Gil Peyrot14374fb2021-05-11 23:07:25 +0200399 { .compatible = "eeprom-93xx46", .data = &at93c46_data, },
400 { .compatible = "atmel,at93c46", .data = &at93c46_data, },
Cory Tusare1379b52016-02-10 14:32:07 -0500401 { .compatible = "atmel,at93c46d", .data = &atmel_at93c46d_data, },
Emmanuel Gil Peyrot14374fb2021-05-11 23:07:25 +0200402 { .compatible = "atmel,at93c56", .data = &at93c56_data, },
403 { .compatible = "atmel,at93c66", .data = &at93c66_data, },
Aswath Govindrajuf6f1f8e2021-01-05 16:28:12 +0530404 { .compatible = "microchip,93lc46b", .data = &microchip_93lc46b_data, },
Cory Tusarc074abe2016-01-06 22:55:02 -0500405 {}
406};
407MODULE_DEVICE_TABLE(of, eeprom_93xx46_of_table);
408
Mark Brown137879f2021-09-22 19:40:48 +0100409static 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)&microchip_93lc46b_data, },
422 {}
423};
Arnd Bergmannf4275272021-10-14 17:37:18 +0200424MODULE_DEVICE_TABLE(spi, eeprom_93xx46_spi_ids);
Mark Brown137879f2021-09-22 19:40:48 +0100425
Cory Tusarc074abe2016-01-06 22:55:02 -0500426static int eeprom_93xx46_probe_dt(struct spi_device *spi)
427{
Cory Tusare1379b52016-02-10 14:32:07 -0500428 const struct of_device_id *of_id =
429 of_match_device(eeprom_93xx46_of_table, &spi->dev);
Cory Tusarc074abe2016-01-06 22:55:02 -0500430 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 Estevam54ec6022017-07-19 23:35:28 -0300457 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 Tusar3ca9b1a2016-02-10 14:32:08 -0500461
Fabio Estevam54ec6022017-07-19 23:35:28 -0300462 pd->prepare = select_assert;
463 pd->finish = select_deassert;
464 gpiod_direction_output(pd->select, 0);
Cory Tusar3ca9b1a2016-02-10 14:32:08 -0500465
Cory Tusare1379b52016-02-10 14:32:07 -0500466 if (of_id->data) {
467 const struct eeprom_93xx46_devtype_data *data = of_id->data;
468
469 pd->quirks = data->quirks;
Emmanuel Gil Peyrot14374fb2021-05-11 23:07:25 +0200470 pd->flags |= data->flags;
Cory Tusare1379b52016-02-10 14:32:07 -0500471 }
472
Cory Tusarc074abe2016-01-06 22:55:02 -0500473 spi->dev.platform_data = pd;
474
475 return 0;
476}
477
Bill Pemberton80c8ae22012-11-19 13:23:05 -0500478static int eeprom_93xx46_probe(struct spi_device *spi)
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700479{
480 struct eeprom_93xx46_platform_data *pd;
481 struct eeprom_93xx46_dev *edev;
482 int err;
483
Cory Tusarc074abe2016-01-06 22:55:02 -0500484 if (spi->dev.of_node) {
485 err = eeprom_93xx46_probe_dt(spi);
486 if (err < 0)
487 return err;
488 }
489
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700490 pd = spi->dev.platform_data;
491 if (!pd) {
492 dev_err(&spi->dev, "missing platform data\n");
493 return -ENODEV;
494 }
495
Bartosz Golaszewskic853d692018-09-21 06:40:01 -0700496 edev = devm_kzalloc(&spi->dev, sizeof(*edev), GFP_KERNEL);
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700497 if (!edev)
498 return -ENOMEM;
499
Emmanuel Gil Peyrot14374fb2021-05-11 23:07:25 +0200500 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 Peyrot4a5ff992021-05-11 23:07:24 +0200510
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700511 if (pd->flags & EE_ADDR8)
Emmanuel Gil Peyrot4a5ff992021-05-11 23:07:24 +0200512 edev->addrlen = ilog2(edev->size);
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700513 else if (pd->flags & EE_ADDR16)
Emmanuel Gil Peyrot4a5ff992021-05-11 23:07:24 +0200514 edev->addrlen = ilog2(edev->size) - 1;
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700515 else {
516 dev_err(&spi->dev, "unspecified address type\n");
Bartosz Golaszewskic853d692018-09-21 06:40:01 -0700517 return -EINVAL;
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700518 }
519
520 mutex_init(&edev->lock);
521
Mark Browndd69a182016-04-20 10:16:36 +0100522 edev->spi = spi;
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700523 edev->pdata = pd;
524
Vadym Kochan1d62a2c2020-09-16 20:09:33 +0300525 edev->nvmem_config.type = NVMEM_TYPE_EEPROM;
Andrew Lunn1c4b6e22016-02-26 20:59:23 +0100526 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 Kandagatlaa8ab3162016-04-24 20:28:16 +0100533 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 Lunn1c4b6e22016-02-26 20:59:23 +0100539
Bartosz Golaszewskic853d692018-09-21 06:40:01 -0700540 edev->nvmem = devm_nvmem_register(&spi->dev, &edev->nvmem_config);
541 if (IS_ERR(edev->nvmem))
542 return PTR_ERR(edev->nvmem);
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700543
Emmanuel Gil Peyrot14374fb2021-05-11 23:07:25 +0200544 dev_info(&spi->dev, "%d-bit eeprom containing %d bytes %s\n",
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700545 (pd->flags & EE_ADDR8) ? 8 : 16,
Emmanuel Gil Peyrot14374fb2021-05-11 23:07:25 +0200546 edev->size,
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700547 (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 Han5ba75b52013-04-05 10:56:04 +0900554 spi_set_drvdata(spi, edev);
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700555 return 0;
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700556}
557
Bill Pemberton486a5c22012-11-19 13:26:02 -0500558static int eeprom_93xx46_remove(struct spi_device *spi)
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700559{
Jingoo Han5ba75b52013-04-05 10:56:04 +0900560 struct eeprom_93xx46_dev *edev = spi_get_drvdata(spi);
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700561
562 if (!(edev->pdata->flags & EE_READONLY))
563 device_remove_file(&spi->dev, &dev_attr_erase);
564
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700565 return 0;
566}
567
568static struct spi_driver eeprom_93xx46_driver = {
569 .driver = {
570 .name = "93xx46",
Cory Tusarc074abe2016-01-06 22:55:02 -0500571 .of_match_table = of_match_ptr(eeprom_93xx46_of_table),
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700572 },
573 .probe = eeprom_93xx46_probe,
Bill Pemberton2d6bed92012-11-19 13:21:23 -0500574 .remove = eeprom_93xx46_remove,
Mark Brown137879f2021-09-22 19:40:48 +0100575 .id_table = eeprom_93xx46_spi_ids,
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700576};
577
Axel Lina3dc3c92012-01-22 15:38:22 +0800578module_spi_driver(eeprom_93xx46_driver);
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700579
580MODULE_LICENSE("GPL");
581MODULE_DESCRIPTION("Driver for 93xx46 EEPROMs");
582MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
Aswath Govindraju4540b9f2021-01-13 10:42:52 +0530583MODULE_ALIAS("spi:93xx46");
Aswath Govindraju13613a22021-01-07 22:09:53 +0530584MODULE_ALIAS("spi:eeprom-93xx46");
Aswath Govindraju47771f12021-01-07 22:09:54 +0530585MODULE_ALIAS("spi:93lc46b");