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.
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>
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/sysfs.h>
23 #include <linux/eeprom_93xx46.h>
26 #define OP_WRITE (OP_START | 0x1)
27 #define OP_READ (OP_START | 0x2)
28 #define ADDR_EWDS 0x00
29 #define ADDR_ERAL 0x20
30 #define ADDR_EWEN 0x30
32 struct eeprom_93xx46_devtype_data {
36 static const struct eeprom_93xx46_devtype_data atmel_at93c46d_data = {
37 .quirks = EEPROM_93XX46_QUIRK_SINGLE_WORD_READ |
38 EEPROM_93XX46_QUIRK_INSTRUCTION_LENGTH,
41 struct eeprom_93xx46_dev {
42 struct spi_device *spi;
43 struct eeprom_93xx46_platform_data *pdata;
44 struct bin_attribute bin;
49 static inline bool has_quirk_single_word_read(struct eeprom_93xx46_dev *edev)
51 return edev->pdata->quirks & EEPROM_93XX46_QUIRK_SINGLE_WORD_READ;
54 static inline bool has_quirk_instruction_length(struct eeprom_93xx46_dev *edev)
56 return edev->pdata->quirks & EEPROM_93XX46_QUIRK_INSTRUCTION_LENGTH;
60 eeprom_93xx46_bin_read(struct file *filp, struct kobject *kobj,
61 struct bin_attribute *bin_attr,
62 char *buf, loff_t off, size_t count)
64 struct eeprom_93xx46_dev *edev;
68 dev = kobj_to_dev(kobj);
69 edev = dev_get_drvdata(dev);
71 mutex_lock(&edev->lock);
73 if (edev->pdata->prepare)
74 edev->pdata->prepare(edev);
78 struct spi_transfer t[2] = { { 0 } };
79 u16 cmd_addr = OP_READ << edev->addrlen;
80 size_t nbytes = count;
84 if (edev->addrlen == 7) {
85 cmd_addr |= off & 0x7f;
87 if (has_quirk_single_word_read(edev))
90 cmd_addr |= (off >> 1) & 0x3f;
92 if (has_quirk_single_word_read(edev))
96 dev_dbg(&edev->spi->dev, "read cmd 0x%x, %d Hz\n",
97 cmd_addr, edev->spi->max_speed_hz);
101 t[0].tx_buf = (char *)&cmd_addr;
103 t[0].bits_per_word = bits;
104 spi_message_add_tail(&t[0], &m);
108 t[1].bits_per_word = 8;
109 spi_message_add_tail(&t[1], &m);
111 err = spi_sync(edev->spi, &m);
112 /* have to wait at least Tcsl ns */
116 dev_err(&edev->spi->dev, "read %zu bytes at %d: err. %d\n",
117 nbytes, (int)off, err);
128 if (edev->pdata->finish)
129 edev->pdata->finish(edev);
131 mutex_unlock(&edev->lock);
135 static int eeprom_93xx46_ew(struct eeprom_93xx46_dev *edev, int is_on)
137 struct spi_message m;
138 struct spi_transfer t;
142 cmd_addr = OP_START << edev->addrlen;
143 if (edev->addrlen == 7) {
144 cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS) << 1;
147 cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS);
151 if (has_quirk_instruction_length(edev)) {
156 dev_dbg(&edev->spi->dev, "ew%s cmd 0x%04x, %d bits\n",
157 is_on ? "en" : "ds", cmd_addr, bits);
159 spi_message_init(&m);
160 memset(&t, 0, sizeof(t));
162 t.tx_buf = &cmd_addr;
164 t.bits_per_word = bits;
165 spi_message_add_tail(&t, &m);
167 mutex_lock(&edev->lock);
169 if (edev->pdata->prepare)
170 edev->pdata->prepare(edev);
172 ret = spi_sync(edev->spi, &m);
173 /* have to wait at least Tcsl ns */
176 dev_err(&edev->spi->dev, "erase/write %sable error %d\n",
177 is_on ? "en" : "dis", ret);
179 if (edev->pdata->finish)
180 edev->pdata->finish(edev);
182 mutex_unlock(&edev->lock);
187 eeprom_93xx46_write_word(struct eeprom_93xx46_dev *edev,
188 const char *buf, unsigned off)
190 struct spi_message m;
191 struct spi_transfer t[2];
192 int bits, data_len, ret;
195 cmd_addr = OP_WRITE << edev->addrlen;
197 if (edev->addrlen == 7) {
198 cmd_addr |= off & 0x7f;
202 cmd_addr |= (off >> 1) & 0x3f;
207 dev_dbg(&edev->spi->dev, "write cmd 0x%x\n", cmd_addr);
209 spi_message_init(&m);
210 memset(t, 0, sizeof(t));
212 t[0].tx_buf = (char *)&cmd_addr;
214 t[0].bits_per_word = bits;
215 spi_message_add_tail(&t[0], &m);
219 t[1].bits_per_word = 8;
220 spi_message_add_tail(&t[1], &m);
222 ret = spi_sync(edev->spi, &m);
223 /* have to wait program cycle time Twc ms */
229 eeprom_93xx46_bin_write(struct file *filp, struct kobject *kobj,
230 struct bin_attribute *bin_attr,
231 char *buf, loff_t off, size_t count)
233 struct eeprom_93xx46_dev *edev;
235 int i, ret, step = 1;
237 dev = kobj_to_dev(kobj);
238 edev = dev_get_drvdata(dev);
240 /* only write even number of bytes on 16-bit devices */
241 if (edev->addrlen == 6) {
246 /* erase/write enable */
247 ret = eeprom_93xx46_ew(edev, 1);
251 mutex_lock(&edev->lock);
253 if (edev->pdata->prepare)
254 edev->pdata->prepare(edev);
256 for (i = 0; i < count; i += step) {
257 ret = eeprom_93xx46_write_word(edev, &buf[i], off + i);
259 dev_err(&edev->spi->dev, "write failed at %d: %d\n",
265 if (edev->pdata->finish)
266 edev->pdata->finish(edev);
268 mutex_unlock(&edev->lock);
270 /* erase/write disable */
271 eeprom_93xx46_ew(edev, 0);
272 return ret ? : count;
275 static int eeprom_93xx46_eral(struct eeprom_93xx46_dev *edev)
277 struct eeprom_93xx46_platform_data *pd = edev->pdata;
278 struct spi_message m;
279 struct spi_transfer t;
283 cmd_addr = OP_START << edev->addrlen;
284 if (edev->addrlen == 7) {
285 cmd_addr |= ADDR_ERAL << 1;
288 cmd_addr |= ADDR_ERAL;
292 if (has_quirk_instruction_length(edev)) {
297 dev_dbg(&edev->spi->dev, "eral cmd 0x%04x, %d bits\n", cmd_addr, bits);
299 spi_message_init(&m);
300 memset(&t, 0, sizeof(t));
302 t.tx_buf = &cmd_addr;
304 t.bits_per_word = bits;
305 spi_message_add_tail(&t, &m);
307 mutex_lock(&edev->lock);
309 if (edev->pdata->prepare)
310 edev->pdata->prepare(edev);
312 ret = spi_sync(edev->spi, &m);
314 dev_err(&edev->spi->dev, "erase error %d\n", ret);
315 /* have to wait erase cycle time Tec ms */
321 mutex_unlock(&edev->lock);
325 static ssize_t eeprom_93xx46_store_erase(struct device *dev,
326 struct device_attribute *attr,
327 const char *buf, size_t count)
329 struct eeprom_93xx46_dev *edev = dev_get_drvdata(dev);
332 sscanf(buf, "%d", &erase);
334 ret = eeprom_93xx46_ew(edev, 1);
337 ret = eeprom_93xx46_eral(edev);
340 ret = eeprom_93xx46_ew(edev, 0);
346 static DEVICE_ATTR(erase, S_IWUSR, NULL, eeprom_93xx46_store_erase);
348 static void select_assert(void *context)
350 struct eeprom_93xx46_dev *edev = context;
352 gpiod_set_value_cansleep(edev->pdata->select, 1);
355 static void select_deassert(void *context)
357 struct eeprom_93xx46_dev *edev = context;
359 gpiod_set_value_cansleep(edev->pdata->select, 0);
362 static const struct of_device_id eeprom_93xx46_of_table[] = {
363 { .compatible = "eeprom-93xx46", },
364 { .compatible = "atmel,at93c46d", .data = &atmel_at93c46d_data, },
367 MODULE_DEVICE_TABLE(of, eeprom_93xx46_of_table);
369 static int eeprom_93xx46_probe_dt(struct spi_device *spi)
371 const struct of_device_id *of_id =
372 of_match_device(eeprom_93xx46_of_table, &spi->dev);
373 struct device_node *np = spi->dev.of_node;
374 struct eeprom_93xx46_platform_data *pd;
377 enum of_gpio_flags of_flags;
380 pd = devm_kzalloc(&spi->dev, sizeof(*pd), GFP_KERNEL);
384 ret = of_property_read_u32(np, "data-size", &tmp);
386 dev_err(&spi->dev, "data-size property not found\n");
391 pd->flags |= EE_ADDR8;
392 } else if (tmp == 16) {
393 pd->flags |= EE_ADDR16;
395 dev_err(&spi->dev, "invalid data-size (%d)\n", tmp);
399 if (of_property_read_bool(np, "read-only"))
400 pd->flags |= EE_READONLY;
402 gpio = of_get_named_gpio_flags(np, "select-gpios", 0, &of_flags);
403 if (gpio_is_valid(gpio)) {
404 unsigned long flags =
405 of_flags == OF_GPIO_ACTIVE_LOW ? GPIOF_ACTIVE_LOW : 0;
407 ret = devm_gpio_request_one(&spi->dev, gpio, flags,
408 "eeprom_93xx46_select");
412 pd->select = gpio_to_desc(gpio);
413 pd->prepare = select_assert;
414 pd->finish = select_deassert;
416 gpiod_direction_output(pd->select, 0);
420 const struct eeprom_93xx46_devtype_data *data = of_id->data;
422 pd->quirks = data->quirks;
425 spi->dev.platform_data = pd;
430 static int eeprom_93xx46_probe(struct spi_device *spi)
432 struct eeprom_93xx46_platform_data *pd;
433 struct eeprom_93xx46_dev *edev;
436 if (spi->dev.of_node) {
437 err = eeprom_93xx46_probe_dt(spi);
442 pd = spi->dev.platform_data;
444 dev_err(&spi->dev, "missing platform data\n");
448 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
452 if (pd->flags & EE_ADDR8)
454 else if (pd->flags & EE_ADDR16)
457 dev_err(&spi->dev, "unspecified address type\n");
462 mutex_init(&edev->lock);
464 edev->spi = spi_dev_get(spi);
467 sysfs_bin_attr_init(&edev->bin);
468 edev->bin.attr.name = "eeprom";
469 edev->bin.attr.mode = S_IRUSR;
470 edev->bin.read = eeprom_93xx46_bin_read;
471 edev->bin.size = 128;
472 if (!(pd->flags & EE_READONLY)) {
473 edev->bin.write = eeprom_93xx46_bin_write;
474 edev->bin.attr.mode |= S_IWUSR;
477 err = sysfs_create_bin_file(&spi->dev.kobj, &edev->bin);
481 dev_info(&spi->dev, "%d-bit eeprom %s\n",
482 (pd->flags & EE_ADDR8) ? 8 : 16,
483 (pd->flags & EE_READONLY) ? "(readonly)" : "");
485 if (!(pd->flags & EE_READONLY)) {
486 if (device_create_file(&spi->dev, &dev_attr_erase))
487 dev_err(&spi->dev, "can't create erase interface\n");
490 spi_set_drvdata(spi, edev);
497 static int eeprom_93xx46_remove(struct spi_device *spi)
499 struct eeprom_93xx46_dev *edev = spi_get_drvdata(spi);
501 if (!(edev->pdata->flags & EE_READONLY))
502 device_remove_file(&spi->dev, &dev_attr_erase);
504 sysfs_remove_bin_file(&spi->dev.kobj, &edev->bin);
509 static struct spi_driver eeprom_93xx46_driver = {
512 .of_match_table = of_match_ptr(eeprom_93xx46_of_table),
514 .probe = eeprom_93xx46_probe,
515 .remove = eeprom_93xx46_remove,
518 module_spi_driver(eeprom_93xx46_driver);
520 MODULE_LICENSE("GPL");
521 MODULE_DESCRIPTION("Driver for 93xx46 EEPROMs");
522 MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
523 MODULE_ALIAS("spi:93xx46");