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/nvmem-provider.h>
23 #include <linux/regmap.h>
24 #include <linux/eeprom_93xx46.h>
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
{
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
;
46 struct regmap_config regmap_config
;
47 struct nvmem_config nvmem_config
;
48 struct nvmem_device
*nvmem
;
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
;
64 eeprom_93xx46_read(struct eeprom_93xx46_dev
*edev
, char *buf
,
65 unsigned off
, size_t count
)
69 if (unlikely(off
>= edev
->size
))
71 if ((off
+ count
) > edev
->size
)
72 count
= edev
->size
- off
;
76 mutex_lock(&edev
->lock
);
78 if (edev
->pdata
->prepare
)
79 edev
->pdata
->prepare(edev
);
83 struct spi_transfer t
[2] = { { 0 } };
84 u16 cmd_addr
= OP_READ
<< edev
->addrlen
;
85 size_t nbytes
= count
;
89 if (edev
->addrlen
== 7) {
90 cmd_addr
|= off
& 0x7f;
92 if (has_quirk_single_word_read(edev
))
95 cmd_addr
|= (off
>> 1) & 0x3f;
97 if (has_quirk_single_word_read(edev
))
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
;
108 t
[0].bits_per_word
= bits
;
109 spi_message_add_tail(&t
[0], &m
);
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 */
121 dev_err(&edev
->spi
->dev
, "read %zu bytes at %d: err. %d\n",
122 nbytes
, (int)off
, err
);
133 if (edev
->pdata
->finish
)
134 edev
->pdata
->finish(edev
);
136 mutex_unlock(&edev
->lock
);
140 static int eeprom_93xx46_ew(struct eeprom_93xx46_dev
*edev
, int is_on
)
142 struct spi_message m
;
143 struct spi_transfer t
;
147 cmd_addr
= OP_START
<< edev
->addrlen
;
148 if (edev
->addrlen
== 7) {
149 cmd_addr
|= (is_on
? ADDR_EWEN
: ADDR_EWDS
) << 1;
152 cmd_addr
|= (is_on
? ADDR_EWEN
: ADDR_EWDS
);
156 if (has_quirk_instruction_length(edev
)) {
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
;
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 */
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
);
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
;
200 cmd_addr
= OP_WRITE
<< edev
->addrlen
;
202 if (edev
->addrlen
== 7) {
203 cmd_addr
|= off
& 0x7f;
207 cmd_addr
|= (off
>> 1) & 0x3f;
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
;
219 t
[0].bits_per_word
= bits
;
220 spi_message_add_tail(&t
[0], &m
);
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 */
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
))
241 if ((off
+ count
) > edev
->size
)
242 count
= edev
->size
- off
;
243 if (unlikely(!count
))
246 /* only write even number of bytes on 16-bit devices */
247 if (edev
->addrlen
== 6) {
252 /* erase/write enable */
253 ret
= eeprom_93xx46_ew(edev
, 1);
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
);
265 dev_err(&edev
->spi
->dev
, "write failed at %d: %d\n",
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
285 static int eeprom_93xx46_regmap_read(void *context
, const void *reg
,
286 size_t reg_size
, void *val
,
289 struct eeprom_93xx46_dev
*eeprom_93xx46
= context
;
290 off_t offset
= *(u32
*)reg
;
293 err
= eeprom_93xx46_read(eeprom_93xx46
, val
, offset
, val_size
);
299 static int eeprom_93xx46_regmap_write(void *context
, const void *data
,
302 struct eeprom_93xx46_dev
*eeprom_93xx46
= context
;
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
);
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
;
332 cmd_addr
= OP_START
<< edev
->addrlen
;
333 if (edev
->addrlen
== 7) {
334 cmd_addr
|= ADDR_ERAL
<< 1;
337 cmd_addr
|= ADDR_ERAL
;
341 if (has_quirk_instruction_length(edev
)) {
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
;
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
);
363 dev_err(&edev
->spi
->dev
, "erase error %d\n", ret
);
364 /* have to wait erase cycle time Tec ms */
370 mutex_unlock(&edev
->lock
);
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
);
381 sscanf(buf
, "%d", &erase
);
383 ret
= eeprom_93xx46_ew(edev
, 1);
386 ret
= eeprom_93xx46_eral(edev
);
389 ret
= eeprom_93xx46_ew(edev
, 0);
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
;
426 enum of_gpio_flags of_flags
;
429 pd
= devm_kzalloc(&spi
->dev
, sizeof(*pd
), GFP_KERNEL
);
433 ret
= of_property_read_u32(np
, "data-size", &tmp
);
435 dev_err(&spi
->dev
, "data-size property not found\n");
440 pd
->flags
|= EE_ADDR8
;
441 } else if (tmp
== 16) {
442 pd
->flags
|= EE_ADDR16
;
444 dev_err(&spi
->dev
, "invalid data-size (%d)\n", tmp
);
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");
461 pd
->select
= gpio_to_desc(gpio
);
462 pd
->prepare
= select_assert
;
463 pd
->finish
= select_deassert
;
465 gpiod_direction_output(pd
->select
, 0);
469 const struct eeprom_93xx46_devtype_data
*data
= of_id
->data
;
471 pd
->quirks
= data
->quirks
;
474 spi
->dev
.platform_data
= pd
;
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
;
486 if (spi
->dev
.of_node
) {
487 err
= eeprom_93xx46_probe_dt(spi
);
492 pd
= spi
->dev
.platform_data
;
494 dev_err(&spi
->dev
, "missing platform data\n");
498 edev
= kzalloc(sizeof(*edev
), GFP_KERNEL
);
502 if (pd
->flags
& EE_ADDR8
)
504 else if (pd
->flags
& EE_ADDR16
)
507 dev_err(&spi
->dev
, "unspecified address type\n");
512 mutex_init(&edev
->lock
);
514 edev
->spi
= spi_dev_get(spi
);
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
);
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
);
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
);
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
);
575 static struct spi_driver eeprom_93xx46_driver
= {
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");