Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / misc / eeprom / eeprom_93xx46.c
blob9cae6f530679b3e5ee6ed721f952cb6d8f4e216f
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Driver for 93xx46 EEPROMs
5 * (C) 2011 DENX Software Engineering, Anatolij Gustschin <agust@denx.de>
6 */
8 #include <linux/array_size.h>
9 #include <linux/bits.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/kstrtox.h>
14 #include <linux/log2.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/property.h>
19 #include <linux/slab.h>
20 #include <linux/spi/spi.h>
21 #include <linux/string_choices.h>
23 #include <linux/nvmem-provider.h>
25 struct eeprom_93xx46_platform_data {
26 unsigned char flags;
27 #define EE_ADDR8 0x01 /* 8 bit addr. cfg */
28 #define EE_ADDR16 0x02 /* 16 bit addr. cfg */
29 #define EE_READONLY 0x08 /* forbid writing */
30 #define EE_SIZE1K 0x10 /* 1 kb of data, that is a 93xx46 */
31 #define EE_SIZE2K 0x20 /* 2 kb of data, that is a 93xx56 */
32 #define EE_SIZE4K 0x40 /* 4 kb of data, that is a 93xx66 */
34 unsigned int quirks;
35 /* Single word read transfers only; no sequential read. */
36 #define EEPROM_93XX46_QUIRK_SINGLE_WORD_READ (1 << 0)
37 /* Instructions such as EWEN are (addrlen + 2) in length. */
38 #define EEPROM_93XX46_QUIRK_INSTRUCTION_LENGTH (1 << 1)
39 /* Add extra cycle after address during a read */
40 #define EEPROM_93XX46_QUIRK_EXTRA_READ_CYCLE BIT(2)
42 struct gpio_desc *select;
45 #define OP_START 0x4
46 #define OP_WRITE (OP_START | 0x1)
47 #define OP_READ (OP_START | 0x2)
48 #define ADDR_EWDS 0x00
49 #define ADDR_ERAL 0x20
50 #define ADDR_EWEN 0x30
52 struct eeprom_93xx46_devtype_data {
53 unsigned int quirks;
54 unsigned char flags;
57 static const struct eeprom_93xx46_devtype_data at93c46_data = {
58 .flags = EE_SIZE1K,
61 static const struct eeprom_93xx46_devtype_data at93c56_data = {
62 .flags = EE_SIZE2K,
65 static const struct eeprom_93xx46_devtype_data at93c66_data = {
66 .flags = EE_SIZE4K,
69 static const struct eeprom_93xx46_devtype_data atmel_at93c46d_data = {
70 .flags = EE_SIZE1K,
71 .quirks = EEPROM_93XX46_QUIRK_SINGLE_WORD_READ |
72 EEPROM_93XX46_QUIRK_INSTRUCTION_LENGTH,
75 static const struct eeprom_93xx46_devtype_data microchip_93lc46b_data = {
76 .flags = EE_SIZE1K,
77 .quirks = EEPROM_93XX46_QUIRK_EXTRA_READ_CYCLE,
80 struct eeprom_93xx46_dev {
81 struct spi_device *spi;
82 struct eeprom_93xx46_platform_data *pdata;
83 struct mutex lock;
84 struct nvmem_config nvmem_config;
85 struct nvmem_device *nvmem;
86 int addrlen;
87 int size;
90 static inline bool has_quirk_single_word_read(struct eeprom_93xx46_dev *edev)
92 return edev->pdata->quirks & EEPROM_93XX46_QUIRK_SINGLE_WORD_READ;
95 static inline bool has_quirk_instruction_length(struct eeprom_93xx46_dev *edev)
97 return edev->pdata->quirks & EEPROM_93XX46_QUIRK_INSTRUCTION_LENGTH;
100 static inline bool has_quirk_extra_read_cycle(struct eeprom_93xx46_dev *edev)
102 return edev->pdata->quirks & EEPROM_93XX46_QUIRK_EXTRA_READ_CYCLE;
105 static int eeprom_93xx46_read(void *priv, unsigned int off,
106 void *val, size_t count)
108 struct eeprom_93xx46_dev *edev = priv;
109 char *buf = val;
110 int err = 0;
111 int bits;
113 if (unlikely(off >= edev->size))
114 return 0;
115 if ((off + count) > edev->size)
116 count = edev->size - off;
117 if (unlikely(!count))
118 return count;
120 mutex_lock(&edev->lock);
122 gpiod_set_value_cansleep(edev->pdata->select, 1);
124 /* The opcode in front of the address is three bits. */
125 bits = edev->addrlen + 3;
127 while (count) {
128 struct spi_message m;
129 struct spi_transfer t[2] = {};
130 u16 cmd_addr = OP_READ << edev->addrlen;
131 size_t nbytes = count;
133 if (edev->pdata->flags & EE_ADDR8) {
134 cmd_addr |= off;
135 if (has_quirk_single_word_read(edev))
136 nbytes = 1;
137 } else {
138 cmd_addr |= (off >> 1);
139 if (has_quirk_single_word_read(edev))
140 nbytes = 2;
143 dev_dbg(&edev->spi->dev, "read cmd 0x%x, %d Hz\n",
144 cmd_addr, edev->spi->max_speed_hz);
146 if (has_quirk_extra_read_cycle(edev)) {
147 cmd_addr <<= 1;
148 bits += 1;
151 t[0].tx_buf = (char *)&cmd_addr;
152 t[0].len = 2;
153 t[0].bits_per_word = bits;
155 t[1].rx_buf = buf;
156 t[1].len = count;
157 t[1].bits_per_word = 8;
159 spi_message_init_with_transfers(&m, t, ARRAY_SIZE(t));
161 err = spi_sync(edev->spi, &m);
162 /* have to wait at least Tcsl ns */
163 ndelay(250);
165 if (err) {
166 dev_err(&edev->spi->dev, "read %zu bytes at %u: err. %d\n",
167 nbytes, off, err);
168 break;
171 buf += nbytes;
172 off += nbytes;
173 count -= nbytes;
176 gpiod_set_value_cansleep(edev->pdata->select, 0);
178 mutex_unlock(&edev->lock);
180 return err;
183 static int eeprom_93xx46_ew(struct eeprom_93xx46_dev *edev, int is_on)
185 struct spi_message m;
186 struct spi_transfer t = {};
187 int bits, ret;
188 u16 cmd_addr;
190 /* The opcode in front of the address is three bits. */
191 bits = edev->addrlen + 3;
193 cmd_addr = OP_START << edev->addrlen;
194 if (edev->pdata->flags & EE_ADDR8)
195 cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS) << 1;
196 else
197 cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS);
199 if (has_quirk_instruction_length(edev)) {
200 cmd_addr <<= 2;
201 bits += 2;
204 dev_dbg(&edev->spi->dev, "ew %s cmd 0x%04x, %d bits\n",
205 str_enable_disable(is_on), cmd_addr, bits);
207 t.tx_buf = &cmd_addr;
208 t.len = 2;
209 t.bits_per_word = bits;
211 spi_message_init_with_transfers(&m, &t, 1);
213 mutex_lock(&edev->lock);
215 gpiod_set_value_cansleep(edev->pdata->select, 1);
217 ret = spi_sync(edev->spi, &m);
218 /* have to wait at least Tcsl ns */
219 ndelay(250);
220 if (ret)
221 dev_err(&edev->spi->dev, "erase/write %s error %d\n",
222 str_enable_disable(is_on), ret);
224 gpiod_set_value_cansleep(edev->pdata->select, 0);
226 mutex_unlock(&edev->lock);
227 return ret;
230 static ssize_t
231 eeprom_93xx46_write_word(struct eeprom_93xx46_dev *edev,
232 const char *buf, unsigned int off)
234 struct spi_message m;
235 struct spi_transfer t[2] = {};
236 int bits, data_len, ret;
237 u16 cmd_addr;
239 if (unlikely(off >= edev->size))
240 return -EINVAL;
242 /* The opcode in front of the address is three bits. */
243 bits = edev->addrlen + 3;
245 cmd_addr = OP_WRITE << edev->addrlen;
247 if (edev->pdata->flags & EE_ADDR8) {
248 cmd_addr |= off;
249 data_len = 1;
250 } else {
251 cmd_addr |= (off >> 1);
252 data_len = 2;
255 dev_dbg(&edev->spi->dev, "write cmd 0x%x\n", cmd_addr);
257 t[0].tx_buf = (char *)&cmd_addr;
258 t[0].len = 2;
259 t[0].bits_per_word = bits;
261 t[1].tx_buf = buf;
262 t[1].len = data_len;
263 t[1].bits_per_word = 8;
265 spi_message_init_with_transfers(&m, t, ARRAY_SIZE(t));
267 ret = spi_sync(edev->spi, &m);
268 /* have to wait program cycle time Twc ms */
269 mdelay(6);
270 return ret;
273 static int eeprom_93xx46_write(void *priv, unsigned int off,
274 void *val, size_t count)
276 struct eeprom_93xx46_dev *edev = priv;
277 char *buf = val;
278 int ret, step = 1;
279 unsigned int i;
281 if (unlikely(off >= edev->size))
282 return -EFBIG;
283 if ((off + count) > edev->size)
284 count = edev->size - off;
285 if (unlikely(!count))
286 return count;
288 /* only write even number of bytes on 16-bit devices */
289 if (edev->pdata->flags & EE_ADDR16) {
290 step = 2;
291 count &= ~1;
294 /* erase/write enable */
295 ret = eeprom_93xx46_ew(edev, 1);
296 if (ret)
297 return ret;
299 mutex_lock(&edev->lock);
301 gpiod_set_value_cansleep(edev->pdata->select, 1);
303 for (i = 0; i < count; i += step) {
304 ret = eeprom_93xx46_write_word(edev, &buf[i], off + i);
305 if (ret) {
306 dev_err(&edev->spi->dev, "write failed at %u: %d\n", off + i, ret);
307 break;
311 gpiod_set_value_cansleep(edev->pdata->select, 0);
313 mutex_unlock(&edev->lock);
315 /* erase/write disable */
316 eeprom_93xx46_ew(edev, 0);
317 return ret;
320 static int eeprom_93xx46_eral(struct eeprom_93xx46_dev *edev)
322 struct spi_message m;
323 struct spi_transfer t = {};
324 int bits, ret;
325 u16 cmd_addr;
327 /* The opcode in front of the address is three bits. */
328 bits = edev->addrlen + 3;
330 cmd_addr = OP_START << edev->addrlen;
331 if (edev->pdata->flags & EE_ADDR8)
332 cmd_addr |= ADDR_ERAL << 1;
333 else
334 cmd_addr |= ADDR_ERAL;
336 if (has_quirk_instruction_length(edev)) {
337 cmd_addr <<= 2;
338 bits += 2;
341 dev_dbg(&edev->spi->dev, "eral cmd 0x%04x, %d bits\n", cmd_addr, bits);
343 t.tx_buf = &cmd_addr;
344 t.len = 2;
345 t.bits_per_word = bits;
347 spi_message_init_with_transfers(&m, &t, 1);
349 mutex_lock(&edev->lock);
351 gpiod_set_value_cansleep(edev->pdata->select, 1);
353 ret = spi_sync(edev->spi, &m);
354 if (ret)
355 dev_err(&edev->spi->dev, "erase error %d\n", ret);
356 /* have to wait erase cycle time Tec ms */
357 mdelay(6);
359 gpiod_set_value_cansleep(edev->pdata->select, 0);
361 mutex_unlock(&edev->lock);
362 return ret;
365 static ssize_t erase_store(struct device *dev, struct device_attribute *attr,
366 const char *buf, size_t count)
368 struct eeprom_93xx46_dev *edev = dev_get_drvdata(dev);
369 bool erase;
370 int ret;
372 ret = kstrtobool(buf, &erase);
373 if (ret)
374 return ret;
376 if (erase) {
377 ret = eeprom_93xx46_ew(edev, 1);
378 if (ret)
379 return ret;
380 ret = eeprom_93xx46_eral(edev);
381 if (ret)
382 return ret;
383 ret = eeprom_93xx46_ew(edev, 0);
384 if (ret)
385 return ret;
387 return count;
389 static DEVICE_ATTR_WO(erase);
391 static const struct of_device_id eeprom_93xx46_of_table[] = {
392 { .compatible = "eeprom-93xx46", .data = &at93c46_data, },
393 { .compatible = "atmel,at93c46", .data = &at93c46_data, },
394 { .compatible = "atmel,at93c46d", .data = &atmel_at93c46d_data, },
395 { .compatible = "atmel,at93c56", .data = &at93c56_data, },
396 { .compatible = "atmel,at93c66", .data = &at93c66_data, },
397 { .compatible = "microchip,93lc46b", .data = &microchip_93lc46b_data, },
400 MODULE_DEVICE_TABLE(of, eeprom_93xx46_of_table);
402 static const struct spi_device_id eeprom_93xx46_spi_ids[] = {
403 { .name = "eeprom-93xx46",
404 .driver_data = (kernel_ulong_t)&at93c46_data, },
405 { .name = "at93c46",
406 .driver_data = (kernel_ulong_t)&at93c46_data, },
407 { .name = "at93c46d",
408 .driver_data = (kernel_ulong_t)&atmel_at93c46d_data, },
409 { .name = "at93c56",
410 .driver_data = (kernel_ulong_t)&at93c56_data, },
411 { .name = "at93c66",
412 .driver_data = (kernel_ulong_t)&at93c66_data, },
413 { .name = "93lc46b",
414 .driver_data = (kernel_ulong_t)&microchip_93lc46b_data, },
417 MODULE_DEVICE_TABLE(spi, eeprom_93xx46_spi_ids);
419 static int eeprom_93xx46_probe_fw(struct device *dev)
421 const struct eeprom_93xx46_devtype_data *data;
422 struct eeprom_93xx46_platform_data *pd;
423 u32 tmp;
424 int ret;
426 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
427 if (!pd)
428 return -ENOMEM;
430 ret = device_property_read_u32(dev, "data-size", &tmp);
431 if (ret < 0) {
432 dev_err(dev, "data-size property not found\n");
433 return ret;
436 if (tmp == 8) {
437 pd->flags |= EE_ADDR8;
438 } else if (tmp == 16) {
439 pd->flags |= EE_ADDR16;
440 } else {
441 dev_err(dev, "invalid data-size (%d)\n", tmp);
442 return -EINVAL;
445 if (device_property_read_bool(dev, "read-only"))
446 pd->flags |= EE_READONLY;
448 pd->select = devm_gpiod_get_optional(dev, "select", GPIOD_OUT_LOW);
449 if (IS_ERR(pd->select))
450 return PTR_ERR(pd->select);
451 gpiod_set_consumer_name(pd->select, "93xx46 EEPROMs OE");
453 data = spi_get_device_match_data(to_spi_device(dev));
454 if (data) {
455 pd->quirks = data->quirks;
456 pd->flags |= data->flags;
459 dev->platform_data = pd;
461 return 0;
464 static int eeprom_93xx46_probe(struct spi_device *spi)
466 struct eeprom_93xx46_platform_data *pd;
467 struct eeprom_93xx46_dev *edev;
468 struct device *dev = &spi->dev;
469 int err;
471 err = eeprom_93xx46_probe_fw(dev);
472 if (err < 0)
473 return err;
475 pd = spi->dev.platform_data;
476 if (!pd) {
477 dev_err(&spi->dev, "missing platform data\n");
478 return -ENODEV;
481 edev = devm_kzalloc(&spi->dev, sizeof(*edev), GFP_KERNEL);
482 if (!edev)
483 return -ENOMEM;
485 if (pd->flags & EE_SIZE1K)
486 edev->size = 128;
487 else if (pd->flags & EE_SIZE2K)
488 edev->size = 256;
489 else if (pd->flags & EE_SIZE4K)
490 edev->size = 512;
491 else {
492 dev_err(&spi->dev, "unspecified size\n");
493 return -EINVAL;
496 if (pd->flags & EE_ADDR8)
497 edev->addrlen = ilog2(edev->size);
498 else if (pd->flags & EE_ADDR16)
499 edev->addrlen = ilog2(edev->size) - 1;
500 else {
501 dev_err(&spi->dev, "unspecified address type\n");
502 return -EINVAL;
505 mutex_init(&edev->lock);
507 edev->spi = spi;
508 edev->pdata = pd;
510 edev->nvmem_config.type = NVMEM_TYPE_EEPROM;
511 edev->nvmem_config.name = dev_name(&spi->dev);
512 edev->nvmem_config.dev = &spi->dev;
513 edev->nvmem_config.read_only = pd->flags & EE_READONLY;
514 edev->nvmem_config.root_only = true;
515 edev->nvmem_config.owner = THIS_MODULE;
516 edev->nvmem_config.compat = true;
517 edev->nvmem_config.base_dev = &spi->dev;
518 edev->nvmem_config.reg_read = eeprom_93xx46_read;
519 edev->nvmem_config.reg_write = eeprom_93xx46_write;
520 edev->nvmem_config.priv = edev;
521 edev->nvmem_config.stride = 4;
522 edev->nvmem_config.word_size = 1;
523 edev->nvmem_config.size = edev->size;
525 edev->nvmem = devm_nvmem_register(&spi->dev, &edev->nvmem_config);
526 if (IS_ERR(edev->nvmem))
527 return PTR_ERR(edev->nvmem);
529 dev_info(&spi->dev, "%d-bit eeprom containing %d bytes %s\n",
530 (pd->flags & EE_ADDR8) ? 8 : 16,
531 edev->size,
532 (pd->flags & EE_READONLY) ? "(readonly)" : "");
534 if (!(pd->flags & EE_READONLY)) {
535 if (device_create_file(&spi->dev, &dev_attr_erase))
536 dev_err(&spi->dev, "can't create erase interface\n");
539 spi_set_drvdata(spi, edev);
540 return 0;
543 static void eeprom_93xx46_remove(struct spi_device *spi)
545 struct eeprom_93xx46_dev *edev = spi_get_drvdata(spi);
547 if (!(edev->pdata->flags & EE_READONLY))
548 device_remove_file(&spi->dev, &dev_attr_erase);
551 static struct spi_driver eeprom_93xx46_driver = {
552 .driver = {
553 .name = "93xx46",
554 .of_match_table = eeprom_93xx46_of_table,
556 .probe = eeprom_93xx46_probe,
557 .remove = eeprom_93xx46_remove,
558 .id_table = eeprom_93xx46_spi_ids,
561 module_spi_driver(eeprom_93xx46_driver);
563 MODULE_LICENSE("GPL");
564 MODULE_DESCRIPTION("Driver for 93xx46 EEPROMs");
565 MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
566 MODULE_ALIAS("spi:93xx46");