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/kernel.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/slab.h>
17 #include <linux/spi/spi.h>
18 #include <linux/sysfs.h>
19 #include <linux/eeprom_93xx46.h>
22 #define OP_WRITE (OP_START | 0x1)
23 #define OP_READ (OP_START | 0x2)
24 #define ADDR_EWDS 0x00
25 #define ADDR_ERAL 0x20
26 #define ADDR_EWEN 0x30
28 struct eeprom_93xx46_dev
{
29 struct spi_device
*spi
;
30 struct eeprom_93xx46_platform_data
*pdata
;
31 struct bin_attribute bin
;
37 eeprom_93xx46_bin_read(struct file
*filp
, struct kobject
*kobj
,
38 struct bin_attribute
*bin_attr
,
39 char *buf
, loff_t off
, size_t count
)
41 struct eeprom_93xx46_dev
*edev
;
44 struct spi_transfer t
[2];
48 dev
= container_of(kobj
, struct device
, kobj
);
49 edev
= dev_get_drvdata(dev
);
51 if (unlikely(off
>= edev
->bin
.size
))
53 if ((off
+ count
) > edev
->bin
.size
)
54 count
= edev
->bin
.size
- off
;
58 cmd_addr
= OP_READ
<< edev
->addrlen
;
60 if (edev
->addrlen
== 7) {
61 cmd_addr
|= off
& 0x7f;
64 cmd_addr
|= off
& 0x3f;
68 dev_dbg(&edev
->spi
->dev
, "read cmd 0x%x, %d Hz\n",
69 cmd_addr
, edev
->spi
->max_speed_hz
);
72 memset(t
, 0, sizeof(t
));
74 t
[0].tx_buf
= (char *)&cmd_addr
;
76 t
[0].bits_per_word
= bits
;
77 spi_message_add_tail(&t
[0], &m
);
81 t
[1].bits_per_word
= 8;
82 spi_message_add_tail(&t
[1], &m
);
84 mutex_lock(&edev
->lock
);
86 if (edev
->pdata
->prepare
)
87 edev
->pdata
->prepare(edev
);
89 ret
= spi_sync(edev
->spi
, &m
);
90 /* have to wait at least Tcsl ns */
93 dev_err(&edev
->spi
->dev
, "read %zu bytes at %d: err. %d\n",
94 count
, (int)off
, ret
);
97 if (edev
->pdata
->finish
)
98 edev
->pdata
->finish(edev
);
100 mutex_unlock(&edev
->lock
);
101 return ret
? : count
;
104 static int eeprom_93xx46_ew(struct eeprom_93xx46_dev
*edev
, int is_on
)
106 struct spi_message m
;
107 struct spi_transfer t
;
111 cmd_addr
= OP_START
<< edev
->addrlen
;
112 if (edev
->addrlen
== 7) {
113 cmd_addr
|= (is_on
? ADDR_EWEN
: ADDR_EWDS
) << 1;
116 cmd_addr
|= (is_on
? ADDR_EWEN
: ADDR_EWDS
);
120 dev_dbg(&edev
->spi
->dev
, "ew cmd 0x%04x\n", cmd_addr
);
122 spi_message_init(&m
);
123 memset(&t
, 0, sizeof(t
));
125 t
.tx_buf
= &cmd_addr
;
127 t
.bits_per_word
= bits
;
128 spi_message_add_tail(&t
, &m
);
130 mutex_lock(&edev
->lock
);
132 if (edev
->pdata
->prepare
)
133 edev
->pdata
->prepare(edev
);
135 ret
= spi_sync(edev
->spi
, &m
);
136 /* have to wait at least Tcsl ns */
139 dev_err(&edev
->spi
->dev
, "erase/write %sable error %d\n",
140 is_on
? "en" : "dis", ret
);
142 if (edev
->pdata
->finish
)
143 edev
->pdata
->finish(edev
);
145 mutex_unlock(&edev
->lock
);
150 eeprom_93xx46_write_word(struct eeprom_93xx46_dev
*edev
,
151 const char *buf
, unsigned off
)
153 struct spi_message m
;
154 struct spi_transfer t
[2];
155 int bits
, data_len
, ret
;
158 cmd_addr
= OP_WRITE
<< edev
->addrlen
;
160 if (edev
->addrlen
== 7) {
161 cmd_addr
|= off
& 0x7f;
165 cmd_addr
|= off
& 0x3f;
170 dev_dbg(&edev
->spi
->dev
, "write cmd 0x%x\n", cmd_addr
);
172 spi_message_init(&m
);
173 memset(t
, 0, sizeof(t
));
175 t
[0].tx_buf
= (char *)&cmd_addr
;
177 t
[0].bits_per_word
= bits
;
178 spi_message_add_tail(&t
[0], &m
);
182 t
[1].bits_per_word
= 8;
183 spi_message_add_tail(&t
[1], &m
);
185 ret
= spi_sync(edev
->spi
, &m
);
186 /* have to wait program cycle time Twc ms */
192 eeprom_93xx46_bin_write(struct file
*filp
, struct kobject
*kobj
,
193 struct bin_attribute
*bin_attr
,
194 char *buf
, loff_t off
, size_t count
)
196 struct eeprom_93xx46_dev
*edev
;
198 int i
, ret
, step
= 1;
200 dev
= container_of(kobj
, struct device
, kobj
);
201 edev
= dev_get_drvdata(dev
);
203 if (unlikely(off
>= edev
->bin
.size
))
205 if ((off
+ count
) > edev
->bin
.size
)
206 count
= edev
->bin
.size
- off
;
207 if (unlikely(!count
))
210 /* only write even number of bytes on 16-bit devices */
211 if (edev
->addrlen
== 6) {
216 /* erase/write enable */
217 ret
= eeprom_93xx46_ew(edev
, 1);
221 mutex_lock(&edev
->lock
);
223 if (edev
->pdata
->prepare
)
224 edev
->pdata
->prepare(edev
);
226 for (i
= 0; i
< count
; i
+= step
) {
227 ret
= eeprom_93xx46_write_word(edev
, &buf
[i
], off
+ i
);
229 dev_err(&edev
->spi
->dev
, "write failed at %d: %d\n",
235 if (edev
->pdata
->finish
)
236 edev
->pdata
->finish(edev
);
238 mutex_unlock(&edev
->lock
);
240 /* erase/write disable */
241 eeprom_93xx46_ew(edev
, 0);
242 return ret
? : count
;
245 static int eeprom_93xx46_eral(struct eeprom_93xx46_dev
*edev
)
247 struct eeprom_93xx46_platform_data
*pd
= edev
->pdata
;
248 struct spi_message m
;
249 struct spi_transfer t
;
253 cmd_addr
= OP_START
<< edev
->addrlen
;
254 if (edev
->addrlen
== 7) {
255 cmd_addr
|= ADDR_ERAL
<< 1;
258 cmd_addr
|= ADDR_ERAL
;
262 spi_message_init(&m
);
263 memset(&t
, 0, sizeof(t
));
265 t
.tx_buf
= &cmd_addr
;
267 t
.bits_per_word
= bits
;
268 spi_message_add_tail(&t
, &m
);
270 mutex_lock(&edev
->lock
);
272 if (edev
->pdata
->prepare
)
273 edev
->pdata
->prepare(edev
);
275 ret
= spi_sync(edev
->spi
, &m
);
277 dev_err(&edev
->spi
->dev
, "erase error %d\n", ret
);
278 /* have to wait erase cycle time Tec ms */
284 mutex_unlock(&edev
->lock
);
288 static ssize_t
eeprom_93xx46_store_erase(struct device
*dev
,
289 struct device_attribute
*attr
,
290 const char *buf
, size_t count
)
292 struct eeprom_93xx46_dev
*edev
= dev_get_drvdata(dev
);
295 sscanf(buf
, "%d", &erase
);
297 ret
= eeprom_93xx46_ew(edev
, 1);
300 ret
= eeprom_93xx46_eral(edev
);
303 ret
= eeprom_93xx46_ew(edev
, 0);
309 static DEVICE_ATTR(erase
, S_IWUSR
, NULL
, eeprom_93xx46_store_erase
);
311 static int eeprom_93xx46_probe(struct spi_device
*spi
)
313 struct eeprom_93xx46_platform_data
*pd
;
314 struct eeprom_93xx46_dev
*edev
;
317 pd
= spi
->dev
.platform_data
;
319 dev_err(&spi
->dev
, "missing platform data\n");
323 edev
= kzalloc(sizeof(*edev
), GFP_KERNEL
);
327 if (pd
->flags
& EE_ADDR8
)
329 else if (pd
->flags
& EE_ADDR16
)
332 dev_err(&spi
->dev
, "unspecified address type\n");
337 mutex_init(&edev
->lock
);
339 edev
->spi
= spi_dev_get(spi
);
342 sysfs_bin_attr_init(&edev
->bin
);
343 edev
->bin
.attr
.name
= "eeprom";
344 edev
->bin
.attr
.mode
= S_IRUSR
;
345 edev
->bin
.read
= eeprom_93xx46_bin_read
;
346 edev
->bin
.size
= 128;
347 if (!(pd
->flags
& EE_READONLY
)) {
348 edev
->bin
.write
= eeprom_93xx46_bin_write
;
349 edev
->bin
.attr
.mode
|= S_IWUSR
;
352 err
= sysfs_create_bin_file(&spi
->dev
.kobj
, &edev
->bin
);
356 dev_info(&spi
->dev
, "%d-bit eeprom %s\n",
357 (pd
->flags
& EE_ADDR8
) ? 8 : 16,
358 (pd
->flags
& EE_READONLY
) ? "(readonly)" : "");
360 if (!(pd
->flags
& EE_READONLY
)) {
361 if (device_create_file(&spi
->dev
, &dev_attr_erase
))
362 dev_err(&spi
->dev
, "can't create erase interface\n");
365 spi_set_drvdata(spi
, edev
);
372 static int eeprom_93xx46_remove(struct spi_device
*spi
)
374 struct eeprom_93xx46_dev
*edev
= spi_get_drvdata(spi
);
376 if (!(edev
->pdata
->flags
& EE_READONLY
))
377 device_remove_file(&spi
->dev
, &dev_attr_erase
);
379 sysfs_remove_bin_file(&spi
->dev
.kobj
, &edev
->bin
);
384 static struct spi_driver eeprom_93xx46_driver
= {
387 .owner
= THIS_MODULE
,
389 .probe
= eeprom_93xx46_probe
,
390 .remove
= eeprom_93xx46_remove
,
393 module_spi_driver(eeprom_93xx46_driver
);
395 MODULE_LICENSE("GPL");
396 MODULE_DESCRIPTION("Driver for 93xx46 EEPROMs");
397 MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
398 MODULE_ALIAS("spi:93xx46");