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 cmd_addr
= OP_READ
<< edev
->addrlen
;
53 if (edev
->addrlen
== 7) {
54 cmd_addr
|= off
& 0x7f;
57 cmd_addr
|= off
& 0x3f;
61 dev_dbg(&edev
->spi
->dev
, "read cmd 0x%x, %d Hz\n",
62 cmd_addr
, edev
->spi
->max_speed_hz
);
65 memset(t
, 0, sizeof(t
));
67 t
[0].tx_buf
= (char *)&cmd_addr
;
69 t
[0].bits_per_word
= bits
;
70 spi_message_add_tail(&t
[0], &m
);
74 t
[1].bits_per_word
= 8;
75 spi_message_add_tail(&t
[1], &m
);
77 mutex_lock(&edev
->lock
);
79 if (edev
->pdata
->prepare
)
80 edev
->pdata
->prepare(edev
);
82 ret
= spi_sync(edev
->spi
, &m
);
83 /* have to wait at least Tcsl ns */
86 dev_err(&edev
->spi
->dev
, "read %zu bytes at %d: err. %d\n",
87 count
, (int)off
, ret
);
90 if (edev
->pdata
->finish
)
91 edev
->pdata
->finish(edev
);
93 mutex_unlock(&edev
->lock
);
97 static int eeprom_93xx46_ew(struct eeprom_93xx46_dev
*edev
, int is_on
)
100 struct spi_transfer t
;
104 cmd_addr
= OP_START
<< edev
->addrlen
;
105 if (edev
->addrlen
== 7) {
106 cmd_addr
|= (is_on
? ADDR_EWEN
: ADDR_EWDS
) << 1;
109 cmd_addr
|= (is_on
? ADDR_EWEN
: ADDR_EWDS
);
113 dev_dbg(&edev
->spi
->dev
, "ew cmd 0x%04x\n", cmd_addr
);
115 spi_message_init(&m
);
116 memset(&t
, 0, sizeof(t
));
118 t
.tx_buf
= &cmd_addr
;
120 t
.bits_per_word
= bits
;
121 spi_message_add_tail(&t
, &m
);
123 mutex_lock(&edev
->lock
);
125 if (edev
->pdata
->prepare
)
126 edev
->pdata
->prepare(edev
);
128 ret
= spi_sync(edev
->spi
, &m
);
129 /* have to wait at least Tcsl ns */
132 dev_err(&edev
->spi
->dev
, "erase/write %sable error %d\n",
133 is_on
? "en" : "dis", ret
);
135 if (edev
->pdata
->finish
)
136 edev
->pdata
->finish(edev
);
138 mutex_unlock(&edev
->lock
);
143 eeprom_93xx46_write_word(struct eeprom_93xx46_dev
*edev
,
144 const char *buf
, unsigned off
)
146 struct spi_message m
;
147 struct spi_transfer t
[2];
148 int bits
, data_len
, ret
;
151 cmd_addr
= OP_WRITE
<< edev
->addrlen
;
153 if (edev
->addrlen
== 7) {
154 cmd_addr
|= off
& 0x7f;
158 cmd_addr
|= off
& 0x3f;
163 dev_dbg(&edev
->spi
->dev
, "write cmd 0x%x\n", cmd_addr
);
165 spi_message_init(&m
);
166 memset(t
, 0, sizeof(t
));
168 t
[0].tx_buf
= (char *)&cmd_addr
;
170 t
[0].bits_per_word
= bits
;
171 spi_message_add_tail(&t
[0], &m
);
175 t
[1].bits_per_word
= 8;
176 spi_message_add_tail(&t
[1], &m
);
178 ret
= spi_sync(edev
->spi
, &m
);
179 /* have to wait program cycle time Twc ms */
185 eeprom_93xx46_bin_write(struct file
*filp
, struct kobject
*kobj
,
186 struct bin_attribute
*bin_attr
,
187 char *buf
, loff_t off
, size_t count
)
189 struct eeprom_93xx46_dev
*edev
;
191 int i
, ret
, step
= 1;
193 dev
= container_of(kobj
, struct device
, kobj
);
194 edev
= dev_get_drvdata(dev
);
196 /* only write even number of bytes on 16-bit devices */
197 if (edev
->addrlen
== 6) {
202 /* erase/write enable */
203 ret
= eeprom_93xx46_ew(edev
, 1);
207 mutex_lock(&edev
->lock
);
209 if (edev
->pdata
->prepare
)
210 edev
->pdata
->prepare(edev
);
212 for (i
= 0; i
< count
; i
+= step
) {
213 ret
= eeprom_93xx46_write_word(edev
, &buf
[i
], off
+ i
);
215 dev_err(&edev
->spi
->dev
, "write failed at %d: %d\n",
221 if (edev
->pdata
->finish
)
222 edev
->pdata
->finish(edev
);
224 mutex_unlock(&edev
->lock
);
226 /* erase/write disable */
227 eeprom_93xx46_ew(edev
, 0);
228 return ret
? : count
;
231 static int eeprom_93xx46_eral(struct eeprom_93xx46_dev
*edev
)
233 struct eeprom_93xx46_platform_data
*pd
= edev
->pdata
;
234 struct spi_message m
;
235 struct spi_transfer t
;
239 cmd_addr
= OP_START
<< edev
->addrlen
;
240 if (edev
->addrlen
== 7) {
241 cmd_addr
|= ADDR_ERAL
<< 1;
244 cmd_addr
|= ADDR_ERAL
;
248 spi_message_init(&m
);
249 memset(&t
, 0, sizeof(t
));
251 t
.tx_buf
= &cmd_addr
;
253 t
.bits_per_word
= bits
;
254 spi_message_add_tail(&t
, &m
);
256 mutex_lock(&edev
->lock
);
258 if (edev
->pdata
->prepare
)
259 edev
->pdata
->prepare(edev
);
261 ret
= spi_sync(edev
->spi
, &m
);
263 dev_err(&edev
->spi
->dev
, "erase error %d\n", ret
);
264 /* have to wait erase cycle time Tec ms */
270 mutex_unlock(&edev
->lock
);
274 static ssize_t
eeprom_93xx46_store_erase(struct device
*dev
,
275 struct device_attribute
*attr
,
276 const char *buf
, size_t count
)
278 struct eeprom_93xx46_dev
*edev
= dev_get_drvdata(dev
);
281 sscanf(buf
, "%d", &erase
);
283 ret
= eeprom_93xx46_ew(edev
, 1);
286 ret
= eeprom_93xx46_eral(edev
);
289 ret
= eeprom_93xx46_ew(edev
, 0);
295 static DEVICE_ATTR(erase
, S_IWUSR
, NULL
, eeprom_93xx46_store_erase
);
297 static int eeprom_93xx46_probe(struct spi_device
*spi
)
299 struct eeprom_93xx46_platform_data
*pd
;
300 struct eeprom_93xx46_dev
*edev
;
303 pd
= spi
->dev
.platform_data
;
305 dev_err(&spi
->dev
, "missing platform data\n");
309 edev
= kzalloc(sizeof(*edev
), GFP_KERNEL
);
313 if (pd
->flags
& EE_ADDR8
)
315 else if (pd
->flags
& EE_ADDR16
)
318 dev_err(&spi
->dev
, "unspecified address type\n");
323 mutex_init(&edev
->lock
);
325 edev
->spi
= spi_dev_get(spi
);
328 sysfs_bin_attr_init(&edev
->bin
);
329 edev
->bin
.attr
.name
= "eeprom";
330 edev
->bin
.attr
.mode
= S_IRUSR
;
331 edev
->bin
.read
= eeprom_93xx46_bin_read
;
332 edev
->bin
.size
= 128;
333 if (!(pd
->flags
& EE_READONLY
)) {
334 edev
->bin
.write
= eeprom_93xx46_bin_write
;
335 edev
->bin
.attr
.mode
|= S_IWUSR
;
338 err
= sysfs_create_bin_file(&spi
->dev
.kobj
, &edev
->bin
);
342 dev_info(&spi
->dev
, "%d-bit eeprom %s\n",
343 (pd
->flags
& EE_ADDR8
) ? 8 : 16,
344 (pd
->flags
& EE_READONLY
) ? "(readonly)" : "");
346 if (!(pd
->flags
& EE_READONLY
)) {
347 if (device_create_file(&spi
->dev
, &dev_attr_erase
))
348 dev_err(&spi
->dev
, "can't create erase interface\n");
351 spi_set_drvdata(spi
, edev
);
358 static int eeprom_93xx46_remove(struct spi_device
*spi
)
360 struct eeprom_93xx46_dev
*edev
= spi_get_drvdata(spi
);
362 if (!(edev
->pdata
->flags
& EE_READONLY
))
363 device_remove_file(&spi
->dev
, &dev_attr_erase
);
365 sysfs_remove_bin_file(&spi
->dev
.kobj
, &edev
->bin
);
370 static struct spi_driver eeprom_93xx46_driver
= {
374 .probe
= eeprom_93xx46_probe
,
375 .remove
= eeprom_93xx46_remove
,
378 module_spi_driver(eeprom_93xx46_driver
);
380 MODULE_LICENSE("GPL");
381 MODULE_DESCRIPTION("Driver for 93xx46 EEPROMs");
382 MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
383 MODULE_ALIAS("spi:93xx46");