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/init.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18 #include <linux/spi/spi.h>
19 #include <linux/sysfs.h>
20 #include <linux/eeprom_93xx46.h>
23 #define OP_WRITE (OP_START | 0x1)
24 #define OP_READ (OP_START | 0x2)
25 #define ADDR_EWDS 0x00
26 #define ADDR_ERAL 0x20
27 #define ADDR_EWEN 0x30
29 struct eeprom_93xx46_dev
{
30 struct spi_device
*spi
;
31 struct eeprom_93xx46_platform_data
*pdata
;
32 struct bin_attribute bin
;
38 eeprom_93xx46_bin_read(struct file
*filp
, struct kobject
*kobj
,
39 struct bin_attribute
*bin_attr
,
40 char *buf
, loff_t off
, size_t count
)
42 struct eeprom_93xx46_dev
*edev
;
45 struct spi_transfer t
[2];
49 dev
= container_of(kobj
, struct device
, kobj
);
50 edev
= dev_get_drvdata(dev
);
52 if (unlikely(off
>= edev
->bin
.size
))
54 if ((off
+ count
) > edev
->bin
.size
)
55 count
= edev
->bin
.size
- off
;
59 cmd_addr
= OP_READ
<< edev
->addrlen
;
61 if (edev
->addrlen
== 7) {
62 cmd_addr
|= off
& 0x7f;
65 cmd_addr
|= off
& 0x3f;
69 dev_dbg(&edev
->spi
->dev
, "read cmd 0x%x, %d Hz\n",
70 cmd_addr
, edev
->spi
->max_speed_hz
);
73 memset(t
, 0, sizeof(t
));
75 t
[0].tx_buf
= (char *)&cmd_addr
;
77 t
[0].bits_per_word
= bits
;
78 spi_message_add_tail(&t
[0], &m
);
82 t
[1].bits_per_word
= 8;
83 spi_message_add_tail(&t
[1], &m
);
85 mutex_lock(&edev
->lock
);
87 if (edev
->pdata
->prepare
)
88 edev
->pdata
->prepare(edev
);
90 ret
= spi_sync(edev
->spi
, &m
);
91 /* have to wait at least Tcsl ns */
94 dev_err(&edev
->spi
->dev
, "read %zu bytes at %d: err. %d\n",
95 count
, (int)off
, ret
);
98 if (edev
->pdata
->finish
)
99 edev
->pdata
->finish(edev
);
101 mutex_unlock(&edev
->lock
);
102 return ret
? : count
;
105 static int eeprom_93xx46_ew(struct eeprom_93xx46_dev
*edev
, int is_on
)
107 struct spi_message m
;
108 struct spi_transfer t
;
112 cmd_addr
= OP_START
<< edev
->addrlen
;
113 if (edev
->addrlen
== 7) {
114 cmd_addr
|= (is_on
? ADDR_EWEN
: ADDR_EWDS
) << 1;
117 cmd_addr
|= (is_on
? ADDR_EWEN
: ADDR_EWDS
);
121 dev_dbg(&edev
->spi
->dev
, "ew cmd 0x%04x\n", cmd_addr
);
123 spi_message_init(&m
);
124 memset(&t
, 0, sizeof(t
));
126 t
.tx_buf
= &cmd_addr
;
128 t
.bits_per_word
= bits
;
129 spi_message_add_tail(&t
, &m
);
131 mutex_lock(&edev
->lock
);
133 if (edev
->pdata
->prepare
)
134 edev
->pdata
->prepare(edev
);
136 ret
= spi_sync(edev
->spi
, &m
);
137 /* have to wait at least Tcsl ns */
140 dev_err(&edev
->spi
->dev
, "erase/write %sable error %d\n",
141 is_on
? "en" : "dis", ret
);
143 if (edev
->pdata
->finish
)
144 edev
->pdata
->finish(edev
);
146 mutex_unlock(&edev
->lock
);
151 eeprom_93xx46_write_word(struct eeprom_93xx46_dev
*edev
,
152 const char *buf
, unsigned off
)
154 struct spi_message m
;
155 struct spi_transfer t
[2];
156 int bits
, data_len
, ret
;
159 cmd_addr
= OP_WRITE
<< edev
->addrlen
;
161 if (edev
->addrlen
== 7) {
162 cmd_addr
|= off
& 0x7f;
166 cmd_addr
|= off
& 0x3f;
171 dev_dbg(&edev
->spi
->dev
, "write cmd 0x%x\n", cmd_addr
);
173 spi_message_init(&m
);
174 memset(t
, 0, sizeof(t
));
176 t
[0].tx_buf
= (char *)&cmd_addr
;
178 t
[0].bits_per_word
= bits
;
179 spi_message_add_tail(&t
[0], &m
);
183 t
[1].bits_per_word
= 8;
184 spi_message_add_tail(&t
[1], &m
);
186 ret
= spi_sync(edev
->spi
, &m
);
187 /* have to wait program cycle time Twc ms */
193 eeprom_93xx46_bin_write(struct file
*filp
, struct kobject
*kobj
,
194 struct bin_attribute
*bin_attr
,
195 char *buf
, loff_t off
, size_t count
)
197 struct eeprom_93xx46_dev
*edev
;
199 int i
, ret
, step
= 1;
201 dev
= container_of(kobj
, struct device
, kobj
);
202 edev
= dev_get_drvdata(dev
);
204 if (unlikely(off
>= edev
->bin
.size
))
206 if ((off
+ count
) > edev
->bin
.size
)
207 count
= edev
->bin
.size
- off
;
208 if (unlikely(!count
))
211 /* only write even number of bytes on 16-bit devices */
212 if (edev
->addrlen
== 6) {
217 /* erase/write enable */
218 ret
= eeprom_93xx46_ew(edev
, 1);
222 mutex_lock(&edev
->lock
);
224 if (edev
->pdata
->prepare
)
225 edev
->pdata
->prepare(edev
);
227 for (i
= 0; i
< count
; i
+= step
) {
228 ret
= eeprom_93xx46_write_word(edev
, &buf
[i
], off
+ i
);
230 dev_err(&edev
->spi
->dev
, "write failed at %d: %d\n",
236 if (edev
->pdata
->finish
)
237 edev
->pdata
->finish(edev
);
239 mutex_unlock(&edev
->lock
);
241 /* erase/write disable */
242 eeprom_93xx46_ew(edev
, 0);
243 return ret
? : count
;
246 static int eeprom_93xx46_eral(struct eeprom_93xx46_dev
*edev
)
248 struct eeprom_93xx46_platform_data
*pd
= edev
->pdata
;
249 struct spi_message m
;
250 struct spi_transfer t
;
254 cmd_addr
= OP_START
<< edev
->addrlen
;
255 if (edev
->addrlen
== 7) {
256 cmd_addr
|= ADDR_ERAL
<< 1;
259 cmd_addr
|= ADDR_ERAL
;
263 spi_message_init(&m
);
264 memset(&t
, 0, sizeof(t
));
266 t
.tx_buf
= &cmd_addr
;
268 t
.bits_per_word
= bits
;
269 spi_message_add_tail(&t
, &m
);
271 mutex_lock(&edev
->lock
);
273 if (edev
->pdata
->prepare
)
274 edev
->pdata
->prepare(edev
);
276 ret
= spi_sync(edev
->spi
, &m
);
278 dev_err(&edev
->spi
->dev
, "erase error %d\n", ret
);
279 /* have to wait erase cycle time Tec ms */
285 mutex_unlock(&edev
->lock
);
289 static ssize_t
eeprom_93xx46_store_erase(struct device
*dev
,
290 struct device_attribute
*attr
,
291 const char *buf
, size_t count
)
293 struct eeprom_93xx46_dev
*edev
= dev_get_drvdata(dev
);
296 sscanf(buf
, "%d", &erase
);
298 ret
= eeprom_93xx46_ew(edev
, 1);
301 ret
= eeprom_93xx46_eral(edev
);
304 ret
= eeprom_93xx46_ew(edev
, 0);
310 static DEVICE_ATTR(erase
, S_IWUSR
, NULL
, eeprom_93xx46_store_erase
);
312 static int eeprom_93xx46_probe(struct spi_device
*spi
)
314 struct eeprom_93xx46_platform_data
*pd
;
315 struct eeprom_93xx46_dev
*edev
;
318 pd
= spi
->dev
.platform_data
;
320 dev_err(&spi
->dev
, "missing platform data\n");
324 edev
= kzalloc(sizeof(*edev
), GFP_KERNEL
);
328 if (pd
->flags
& EE_ADDR8
)
330 else if (pd
->flags
& EE_ADDR16
)
333 dev_err(&spi
->dev
, "unspecified address type\n");
338 mutex_init(&edev
->lock
);
340 edev
->spi
= spi_dev_get(spi
);
343 sysfs_bin_attr_init(&edev
->bin
);
344 edev
->bin
.attr
.name
= "eeprom";
345 edev
->bin
.attr
.mode
= S_IRUSR
;
346 edev
->bin
.read
= eeprom_93xx46_bin_read
;
347 edev
->bin
.size
= 128;
348 if (!(pd
->flags
& EE_READONLY
)) {
349 edev
->bin
.write
= eeprom_93xx46_bin_write
;
350 edev
->bin
.attr
.mode
|= S_IWUSR
;
353 err
= sysfs_create_bin_file(&spi
->dev
.kobj
, &edev
->bin
);
357 dev_info(&spi
->dev
, "%d-bit eeprom %s\n",
358 (pd
->flags
& EE_ADDR8
) ? 8 : 16,
359 (pd
->flags
& EE_READONLY
) ? "(readonly)" : "");
361 if (!(pd
->flags
& EE_READONLY
)) {
362 if (device_create_file(&spi
->dev
, &dev_attr_erase
))
363 dev_err(&spi
->dev
, "can't create erase interface\n");
366 spi_set_drvdata(spi
, edev
);
373 static int eeprom_93xx46_remove(struct spi_device
*spi
)
375 struct eeprom_93xx46_dev
*edev
= spi_get_drvdata(spi
);
377 if (!(edev
->pdata
->flags
& EE_READONLY
))
378 device_remove_file(&spi
->dev
, &dev_attr_erase
);
380 sysfs_remove_bin_file(&spi
->dev
.kobj
, &edev
->bin
);
385 static struct spi_driver eeprom_93xx46_driver
= {
388 .owner
= THIS_MODULE
,
390 .probe
= eeprom_93xx46_probe
,
391 .remove
= eeprom_93xx46_remove
,
394 module_spi_driver(eeprom_93xx46_driver
);
396 MODULE_LICENSE("GPL");
397 MODULE_DESCRIPTION("Driver for 93xx46 EEPROMs");
398 MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
399 MODULE_ALIAS("spi:93xx46");