EDAC: i7core, sb_edac: Don't return NOTIFY_BAD from mce_decoder callback
[linux/fpc-iii.git] / drivers / misc / eeprom / eeprom_93xx46.c
blob426fe2fd5238c8f529c94f3c7ce0be8289e55def
1 /*
2 * Driver for 93xx46 EEPROMs
4 * (C) 2011 DENX Software Engineering, Anatolij Gustschin <agust@denx.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/of.h>
18 #include <linux/of_device.h>
19 #include <linux/of_gpio.h>
20 #include <linux/slab.h>
21 #include <linux/spi/spi.h>
22 #include <linux/nvmem-provider.h>
23 #include <linux/regmap.h>
24 #include <linux/eeprom_93xx46.h>
26 #define OP_START 0x4
27 #define OP_WRITE (OP_START | 0x1)
28 #define OP_READ (OP_START | 0x2)
29 #define ADDR_EWDS 0x00
30 #define ADDR_ERAL 0x20
31 #define ADDR_EWEN 0x30
33 struct eeprom_93xx46_devtype_data {
34 unsigned int quirks;
37 static const struct eeprom_93xx46_devtype_data atmel_at93c46d_data = {
38 .quirks = EEPROM_93XX46_QUIRK_SINGLE_WORD_READ |
39 EEPROM_93XX46_QUIRK_INSTRUCTION_LENGTH,
42 struct eeprom_93xx46_dev {
43 struct spi_device *spi;
44 struct eeprom_93xx46_platform_data *pdata;
45 struct mutex lock;
46 struct regmap_config regmap_config;
47 struct nvmem_config nvmem_config;
48 struct nvmem_device *nvmem;
49 int addrlen;
50 int size;
53 static inline bool has_quirk_single_word_read(struct eeprom_93xx46_dev *edev)
55 return edev->pdata->quirks & EEPROM_93XX46_QUIRK_SINGLE_WORD_READ;
58 static inline bool has_quirk_instruction_length(struct eeprom_93xx46_dev *edev)
60 return edev->pdata->quirks & EEPROM_93XX46_QUIRK_INSTRUCTION_LENGTH;
63 static ssize_t
64 eeprom_93xx46_read(struct eeprom_93xx46_dev *edev, char *buf,
65 unsigned off, size_t count)
67 ssize_t ret = 0;
69 if (unlikely(off >= edev->size))
70 return 0;
71 if ((off + count) > edev->size)
72 count = edev->size - off;
73 if (unlikely(!count))
74 return count;
76 mutex_lock(&edev->lock);
78 if (edev->pdata->prepare)
79 edev->pdata->prepare(edev);
81 while (count) {
82 struct spi_message m;
83 struct spi_transfer t[2] = { { 0 } };
84 u16 cmd_addr = OP_READ << edev->addrlen;
85 size_t nbytes = count;
86 int bits;
87 int err;
89 if (edev->addrlen == 7) {
90 cmd_addr |= off & 0x7f;
91 bits = 10;
92 if (has_quirk_single_word_read(edev))
93 nbytes = 1;
94 } else {
95 cmd_addr |= (off >> 1) & 0x3f;
96 bits = 9;
97 if (has_quirk_single_word_read(edev))
98 nbytes = 2;
101 dev_dbg(&edev->spi->dev, "read cmd 0x%x, %d Hz\n",
102 cmd_addr, edev->spi->max_speed_hz);
104 spi_message_init(&m);
106 t[0].tx_buf = (char *)&cmd_addr;
107 t[0].len = 2;
108 t[0].bits_per_word = bits;
109 spi_message_add_tail(&t[0], &m);
111 t[1].rx_buf = buf;
112 t[1].len = count;
113 t[1].bits_per_word = 8;
114 spi_message_add_tail(&t[1], &m);
116 err = spi_sync(edev->spi, &m);
117 /* have to wait at least Tcsl ns */
118 ndelay(250);
120 if (err) {
121 dev_err(&edev->spi->dev, "read %zu bytes at %d: err. %d\n",
122 nbytes, (int)off, err);
123 ret = err;
124 break;
127 buf += nbytes;
128 off += nbytes;
129 count -= nbytes;
130 ret += nbytes;
133 if (edev->pdata->finish)
134 edev->pdata->finish(edev);
136 mutex_unlock(&edev->lock);
137 return ret;
140 static int eeprom_93xx46_ew(struct eeprom_93xx46_dev *edev, int is_on)
142 struct spi_message m;
143 struct spi_transfer t;
144 int bits, ret;
145 u16 cmd_addr;
147 cmd_addr = OP_START << edev->addrlen;
148 if (edev->addrlen == 7) {
149 cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS) << 1;
150 bits = 10;
151 } else {
152 cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS);
153 bits = 9;
156 if (has_quirk_instruction_length(edev)) {
157 cmd_addr <<= 2;
158 bits += 2;
161 dev_dbg(&edev->spi->dev, "ew%s cmd 0x%04x, %d bits\n",
162 is_on ? "en" : "ds", cmd_addr, bits);
164 spi_message_init(&m);
165 memset(&t, 0, sizeof(t));
167 t.tx_buf = &cmd_addr;
168 t.len = 2;
169 t.bits_per_word = bits;
170 spi_message_add_tail(&t, &m);
172 mutex_lock(&edev->lock);
174 if (edev->pdata->prepare)
175 edev->pdata->prepare(edev);
177 ret = spi_sync(edev->spi, &m);
178 /* have to wait at least Tcsl ns */
179 ndelay(250);
180 if (ret)
181 dev_err(&edev->spi->dev, "erase/write %sable error %d\n",
182 is_on ? "en" : "dis", ret);
184 if (edev->pdata->finish)
185 edev->pdata->finish(edev);
187 mutex_unlock(&edev->lock);
188 return ret;
191 static ssize_t
192 eeprom_93xx46_write_word(struct eeprom_93xx46_dev *edev,
193 const char *buf, unsigned off)
195 struct spi_message m;
196 struct spi_transfer t[2];
197 int bits, data_len, ret;
198 u16 cmd_addr;
200 cmd_addr = OP_WRITE << edev->addrlen;
202 if (edev->addrlen == 7) {
203 cmd_addr |= off & 0x7f;
204 bits = 10;
205 data_len = 1;
206 } else {
207 cmd_addr |= (off >> 1) & 0x3f;
208 bits = 9;
209 data_len = 2;
212 dev_dbg(&edev->spi->dev, "write cmd 0x%x\n", cmd_addr);
214 spi_message_init(&m);
215 memset(t, 0, sizeof(t));
217 t[0].tx_buf = (char *)&cmd_addr;
218 t[0].len = 2;
219 t[0].bits_per_word = bits;
220 spi_message_add_tail(&t[0], &m);
222 t[1].tx_buf = buf;
223 t[1].len = data_len;
224 t[1].bits_per_word = 8;
225 spi_message_add_tail(&t[1], &m);
227 ret = spi_sync(edev->spi, &m);
228 /* have to wait program cycle time Twc ms */
229 mdelay(6);
230 return ret;
233 static ssize_t
234 eeprom_93xx46_write(struct eeprom_93xx46_dev *edev, const char *buf,
235 loff_t off, size_t count)
237 int i, ret, step = 1;
239 if (unlikely(off >= edev->size))
240 return -EFBIG;
241 if ((off + count) > edev->size)
242 count = edev->size - off;
243 if (unlikely(!count))
244 return count;
246 /* only write even number of bytes on 16-bit devices */
247 if (edev->addrlen == 6) {
248 step = 2;
249 count &= ~1;
252 /* erase/write enable */
253 ret = eeprom_93xx46_ew(edev, 1);
254 if (ret)
255 return ret;
257 mutex_lock(&edev->lock);
259 if (edev->pdata->prepare)
260 edev->pdata->prepare(edev);
262 for (i = 0; i < count; i += step) {
263 ret = eeprom_93xx46_write_word(edev, &buf[i], off + i);
264 if (ret) {
265 dev_err(&edev->spi->dev, "write failed at %d: %d\n",
266 (int)off + i, ret);
267 break;
271 if (edev->pdata->finish)
272 edev->pdata->finish(edev);
274 mutex_unlock(&edev->lock);
276 /* erase/write disable */
277 eeprom_93xx46_ew(edev, 0);
278 return ret ? : count;
282 * Provide a regmap interface, which is registered with the NVMEM
283 * framework
285 static int eeprom_93xx46_regmap_read(void *context, const void *reg,
286 size_t reg_size, void *val,
287 size_t val_size)
289 struct eeprom_93xx46_dev *eeprom_93xx46 = context;
290 off_t offset = *(u32 *)reg;
291 int err;
293 err = eeprom_93xx46_read(eeprom_93xx46, val, offset, val_size);
294 if (err)
295 return err;
296 return 0;
299 static int eeprom_93xx46_regmap_write(void *context, const void *data,
300 size_t count)
302 struct eeprom_93xx46_dev *eeprom_93xx46 = context;
303 const char *buf;
304 u32 offset;
305 size_t len;
306 int err;
308 memcpy(&offset, data, sizeof(offset));
309 buf = (const char *)data + sizeof(offset);
310 len = count - sizeof(offset);
312 err = eeprom_93xx46_write(eeprom_93xx46, buf, offset, len);
313 if (err)
314 return err;
315 return 0;
318 static const struct regmap_bus eeprom_93xx46_regmap_bus = {
319 .read = eeprom_93xx46_regmap_read,
320 .write = eeprom_93xx46_regmap_write,
321 .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
324 static int eeprom_93xx46_eral(struct eeprom_93xx46_dev *edev)
326 struct eeprom_93xx46_platform_data *pd = edev->pdata;
327 struct spi_message m;
328 struct spi_transfer t;
329 int bits, ret;
330 u16 cmd_addr;
332 cmd_addr = OP_START << edev->addrlen;
333 if (edev->addrlen == 7) {
334 cmd_addr |= ADDR_ERAL << 1;
335 bits = 10;
336 } else {
337 cmd_addr |= ADDR_ERAL;
338 bits = 9;
341 if (has_quirk_instruction_length(edev)) {
342 cmd_addr <<= 2;
343 bits += 2;
346 dev_dbg(&edev->spi->dev, "eral cmd 0x%04x, %d bits\n", cmd_addr, bits);
348 spi_message_init(&m);
349 memset(&t, 0, sizeof(t));
351 t.tx_buf = &cmd_addr;
352 t.len = 2;
353 t.bits_per_word = bits;
354 spi_message_add_tail(&t, &m);
356 mutex_lock(&edev->lock);
358 if (edev->pdata->prepare)
359 edev->pdata->prepare(edev);
361 ret = spi_sync(edev->spi, &m);
362 if (ret)
363 dev_err(&edev->spi->dev, "erase error %d\n", ret);
364 /* have to wait erase cycle time Tec ms */
365 mdelay(6);
367 if (pd->finish)
368 pd->finish(edev);
370 mutex_unlock(&edev->lock);
371 return ret;
374 static ssize_t eeprom_93xx46_store_erase(struct device *dev,
375 struct device_attribute *attr,
376 const char *buf, size_t count)
378 struct eeprom_93xx46_dev *edev = dev_get_drvdata(dev);
379 int erase = 0, ret;
381 sscanf(buf, "%d", &erase);
382 if (erase) {
383 ret = eeprom_93xx46_ew(edev, 1);
384 if (ret)
385 return ret;
386 ret = eeprom_93xx46_eral(edev);
387 if (ret)
388 return ret;
389 ret = eeprom_93xx46_ew(edev, 0);
390 if (ret)
391 return ret;
393 return count;
395 static DEVICE_ATTR(erase, S_IWUSR, NULL, eeprom_93xx46_store_erase);
397 static void select_assert(void *context)
399 struct eeprom_93xx46_dev *edev = context;
401 gpiod_set_value_cansleep(edev->pdata->select, 1);
404 static void select_deassert(void *context)
406 struct eeprom_93xx46_dev *edev = context;
408 gpiod_set_value_cansleep(edev->pdata->select, 0);
411 static const struct of_device_id eeprom_93xx46_of_table[] = {
412 { .compatible = "eeprom-93xx46", },
413 { .compatible = "atmel,at93c46d", .data = &atmel_at93c46d_data, },
416 MODULE_DEVICE_TABLE(of, eeprom_93xx46_of_table);
418 static int eeprom_93xx46_probe_dt(struct spi_device *spi)
420 const struct of_device_id *of_id =
421 of_match_device(eeprom_93xx46_of_table, &spi->dev);
422 struct device_node *np = spi->dev.of_node;
423 struct eeprom_93xx46_platform_data *pd;
424 u32 tmp;
425 int gpio;
426 enum of_gpio_flags of_flags;
427 int ret;
429 pd = devm_kzalloc(&spi->dev, sizeof(*pd), GFP_KERNEL);
430 if (!pd)
431 return -ENOMEM;
433 ret = of_property_read_u32(np, "data-size", &tmp);
434 if (ret < 0) {
435 dev_err(&spi->dev, "data-size property not found\n");
436 return ret;
439 if (tmp == 8) {
440 pd->flags |= EE_ADDR8;
441 } else if (tmp == 16) {
442 pd->flags |= EE_ADDR16;
443 } else {
444 dev_err(&spi->dev, "invalid data-size (%d)\n", tmp);
445 return -EINVAL;
448 if (of_property_read_bool(np, "read-only"))
449 pd->flags |= EE_READONLY;
451 gpio = of_get_named_gpio_flags(np, "select-gpios", 0, &of_flags);
452 if (gpio_is_valid(gpio)) {
453 unsigned long flags =
454 of_flags == OF_GPIO_ACTIVE_LOW ? GPIOF_ACTIVE_LOW : 0;
456 ret = devm_gpio_request_one(&spi->dev, gpio, flags,
457 "eeprom_93xx46_select");
458 if (ret)
459 return ret;
461 pd->select = gpio_to_desc(gpio);
462 pd->prepare = select_assert;
463 pd->finish = select_deassert;
465 gpiod_direction_output(pd->select, 0);
468 if (of_id->data) {
469 const struct eeprom_93xx46_devtype_data *data = of_id->data;
471 pd->quirks = data->quirks;
474 spi->dev.platform_data = pd;
476 return 0;
479 static int eeprom_93xx46_probe(struct spi_device *spi)
481 struct eeprom_93xx46_platform_data *pd;
482 struct eeprom_93xx46_dev *edev;
483 struct regmap *regmap;
484 int err;
486 if (spi->dev.of_node) {
487 err = eeprom_93xx46_probe_dt(spi);
488 if (err < 0)
489 return err;
492 pd = spi->dev.platform_data;
493 if (!pd) {
494 dev_err(&spi->dev, "missing platform data\n");
495 return -ENODEV;
498 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
499 if (!edev)
500 return -ENOMEM;
502 if (pd->flags & EE_ADDR8)
503 edev->addrlen = 7;
504 else if (pd->flags & EE_ADDR16)
505 edev->addrlen = 6;
506 else {
507 dev_err(&spi->dev, "unspecified address type\n");
508 err = -EINVAL;
509 goto fail;
512 mutex_init(&edev->lock);
514 edev->spi = spi_dev_get(spi);
515 edev->pdata = pd;
517 edev->size = 128;
519 edev->regmap_config.reg_bits = 32;
520 edev->regmap_config.val_bits = 8;
521 edev->regmap_config.reg_stride = 1;
522 edev->regmap_config.max_register = edev->size - 1;
524 regmap = devm_regmap_init(&spi->dev, &eeprom_93xx46_regmap_bus, edev,
525 &edev->regmap_config);
526 if (IS_ERR(regmap)) {
527 dev_err(&spi->dev, "regmap init failed\n");
528 err = PTR_ERR(regmap);
529 goto fail;
532 edev->nvmem_config.name = dev_name(&spi->dev);
533 edev->nvmem_config.dev = &spi->dev;
534 edev->nvmem_config.read_only = pd->flags & EE_READONLY;
535 edev->nvmem_config.root_only = true;
536 edev->nvmem_config.owner = THIS_MODULE;
537 edev->nvmem_config.compat = true;
538 edev->nvmem_config.base_dev = &spi->dev;
540 edev->nvmem = nvmem_register(&edev->nvmem_config);
541 if (IS_ERR(edev->nvmem)) {
542 err = PTR_ERR(edev->nvmem);
543 goto fail;
546 dev_info(&spi->dev, "%d-bit eeprom %s\n",
547 (pd->flags & EE_ADDR8) ? 8 : 16,
548 (pd->flags & EE_READONLY) ? "(readonly)" : "");
550 if (!(pd->flags & EE_READONLY)) {
551 if (device_create_file(&spi->dev, &dev_attr_erase))
552 dev_err(&spi->dev, "can't create erase interface\n");
555 spi_set_drvdata(spi, edev);
556 return 0;
557 fail:
558 kfree(edev);
559 return err;
562 static int eeprom_93xx46_remove(struct spi_device *spi)
564 struct eeprom_93xx46_dev *edev = spi_get_drvdata(spi);
566 nvmem_unregister(edev->nvmem);
568 if (!(edev->pdata->flags & EE_READONLY))
569 device_remove_file(&spi->dev, &dev_attr_erase);
571 kfree(edev);
572 return 0;
575 static struct spi_driver eeprom_93xx46_driver = {
576 .driver = {
577 .name = "93xx46",
578 .of_match_table = of_match_ptr(eeprom_93xx46_of_table),
580 .probe = eeprom_93xx46_probe,
581 .remove = eeprom_93xx46_remove,
584 module_spi_driver(eeprom_93xx46_driver);
586 MODULE_LICENSE("GPL");
587 MODULE_DESCRIPTION("Driver for 93xx46 EEPROMs");
588 MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
589 MODULE_ALIAS("spi:93xx46");