Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / misc / eeprom / eeprom_93xx46.c
blob0c7ebb1e19e52199b54616d7ae1fb2300a65b694
1 /*
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.
9 */
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>
22 #define OP_START 0x4
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;
33 struct mutex lock;
34 int addrlen;
37 static ssize_t
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;
43 struct device *dev;
44 struct spi_message m;
45 struct spi_transfer t[2];
46 int bits, ret;
47 u16 cmd_addr;
49 dev = container_of(kobj, struct device, kobj);
50 edev = dev_get_drvdata(dev);
52 if (unlikely(off >= edev->bin.size))
53 return 0;
54 if ((off + count) > edev->bin.size)
55 count = edev->bin.size - off;
56 if (unlikely(!count))
57 return count;
59 cmd_addr = OP_READ << edev->addrlen;
61 if (edev->addrlen == 7) {
62 cmd_addr |= off & 0x7f;
63 bits = 10;
64 } else {
65 cmd_addr |= off & 0x3f;
66 bits = 9;
69 dev_dbg(&edev->spi->dev, "read cmd 0x%x, %d Hz\n",
70 cmd_addr, edev->spi->max_speed_hz);
72 spi_message_init(&m);
73 memset(t, 0, sizeof(t));
75 t[0].tx_buf = (char *)&cmd_addr;
76 t[0].len = 2;
77 t[0].bits_per_word = bits;
78 spi_message_add_tail(&t[0], &m);
80 t[1].rx_buf = buf;
81 t[1].len = count;
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 */
92 ndelay(250);
93 if (ret) {
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;
109 int bits, ret;
110 u16 cmd_addr;
112 cmd_addr = OP_START << edev->addrlen;
113 if (edev->addrlen == 7) {
114 cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS) << 1;
115 bits = 10;
116 } else {
117 cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS);
118 bits = 9;
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;
127 t.len = 2;
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 */
138 ndelay(250);
139 if (ret)
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);
147 return ret;
150 static ssize_t
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;
157 u16 cmd_addr;
159 cmd_addr = OP_WRITE << edev->addrlen;
161 if (edev->addrlen == 7) {
162 cmd_addr |= off & 0x7f;
163 bits = 10;
164 data_len = 1;
165 } else {
166 cmd_addr |= off & 0x3f;
167 bits = 9;
168 data_len = 2;
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;
177 t[0].len = 2;
178 t[0].bits_per_word = bits;
179 spi_message_add_tail(&t[0], &m);
181 t[1].tx_buf = buf;
182 t[1].len = data_len;
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 */
188 mdelay(6);
189 return ret;
192 static ssize_t
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;
198 struct device *dev;
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))
205 return 0;
206 if ((off + count) > edev->bin.size)
207 count = edev->bin.size - off;
208 if (unlikely(!count))
209 return count;
211 /* only write even number of bytes on 16-bit devices */
212 if (edev->addrlen == 6) {
213 step = 2;
214 count &= ~1;
217 /* erase/write enable */
218 ret = eeprom_93xx46_ew(edev, 1);
219 if (ret)
220 return ret;
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);
229 if (ret) {
230 dev_err(&edev->spi->dev, "write failed at %d: %d\n",
231 (int)off + i, ret);
232 break;
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;
251 int bits, ret;
252 u16 cmd_addr;
254 cmd_addr = OP_START << edev->addrlen;
255 if (edev->addrlen == 7) {
256 cmd_addr |= ADDR_ERAL << 1;
257 bits = 10;
258 } else {
259 cmd_addr |= ADDR_ERAL;
260 bits = 9;
263 spi_message_init(&m);
264 memset(&t, 0, sizeof(t));
266 t.tx_buf = &cmd_addr;
267 t.len = 2;
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);
277 if (ret)
278 dev_err(&edev->spi->dev, "erase error %d\n", ret);
279 /* have to wait erase cycle time Tec ms */
280 mdelay(6);
282 if (pd->finish)
283 pd->finish(edev);
285 mutex_unlock(&edev->lock);
286 return ret;
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);
294 int erase = 0, ret;
296 sscanf(buf, "%d", &erase);
297 if (erase) {
298 ret = eeprom_93xx46_ew(edev, 1);
299 if (ret)
300 return ret;
301 ret = eeprom_93xx46_eral(edev);
302 if (ret)
303 return ret;
304 ret = eeprom_93xx46_ew(edev, 0);
305 if (ret)
306 return ret;
308 return count;
310 static DEVICE_ATTR(erase, S_IWUSR, NULL, eeprom_93xx46_store_erase);
312 static int __devinit eeprom_93xx46_probe(struct spi_device *spi)
314 struct eeprom_93xx46_platform_data *pd;
315 struct eeprom_93xx46_dev *edev;
316 int err;
318 pd = spi->dev.platform_data;
319 if (!pd) {
320 dev_err(&spi->dev, "missing platform data\n");
321 return -ENODEV;
324 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
325 if (!edev)
326 return -ENOMEM;
328 if (pd->flags & EE_ADDR8)
329 edev->addrlen = 7;
330 else if (pd->flags & EE_ADDR16)
331 edev->addrlen = 6;
332 else {
333 dev_err(&spi->dev, "unspecified address type\n");
334 err = -EINVAL;
335 goto fail;
338 mutex_init(&edev->lock);
340 edev->spi = spi_dev_get(spi);
341 edev->pdata = pd;
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);
354 if (err)
355 goto fail;
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 dev_set_drvdata(&spi->dev, edev);
367 return 0;
368 fail:
369 kfree(edev);
370 return err;
373 static int __devexit eeprom_93xx46_remove(struct spi_device *spi)
375 struct eeprom_93xx46_dev *edev = dev_get_drvdata(&spi->dev);
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);
381 dev_set_drvdata(&spi->dev, NULL);
382 kfree(edev);
383 return 0;
386 static struct spi_driver eeprom_93xx46_driver = {
387 .driver = {
388 .name = "93xx46",
389 .owner = THIS_MODULE,
391 .probe = eeprom_93xx46_probe,
392 .remove = __devexit_p(eeprom_93xx46_remove),
395 static int __init eeprom_93xx46_init(void)
397 return spi_register_driver(&eeprom_93xx46_driver);
399 module_init(eeprom_93xx46_init);
401 static void __exit eeprom_93xx46_exit(void)
403 spi_unregister_driver(&eeprom_93xx46_driver);
405 module_exit(eeprom_93xx46_exit);
407 MODULE_LICENSE("GPL");
408 MODULE_DESCRIPTION("Driver for 93xx46 EEPROMs");
409 MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
410 MODULE_ALIAS("spi:93xx46");