1249a63188f45f0cbc88540871a0ba4a81df7df0
[openwrt/openwrt.git] / target / linux / generic-2.6 / patches-2.6.25 / 921-gpio_spi_driver.patch
1 Index: linux-2.6.25.10/include/linux/spi/spi_gpio.h
2 ===================================================================
3 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4 +++ linux-2.6.25.10/include/linux/spi/spi_gpio.h 2008-07-20 20:10:21.000000000 +0200
5 @@ -0,0 +1,73 @@
6 +/*
7 + * spi_gpio interface to platform code
8 + *
9 + * Copyright (c) 2008 Piotr Skamruk
10 + * Copyright (c) 2008 Michael Buesch
11 + *
12 + * This program is free software; you can redistribute it and/or modify
13 + * it under the terms of the GNU General Public License version 2 as
14 + * published by the Free Software Foundation.
15 + */
16 +#ifndef _LINUX_SPI_SPI_GPIO
17 +#define _LINUX_SPI_SPI_GPIO
18 +
19 +#include <linux/types.h>
20 +#include <linux/spi/spi.h>
21 +
22 +
23 +/**
24 + * struct spi_gpio_platform_data - Data definitions for a SPI-GPIO device.
25 + *
26 + * This structure holds information about a GPIO-based SPI device.
27 + *
28 + * @pin_clk: The GPIO pin number of the CLOCK pin.
29 + *
30 + * @pin_miso: The GPIO pin number of the MISO pin.
31 + *
32 + * @pin_mosi: The GPIO pin number of the MOSI pin.
33 + *
34 + * @pin_cs: The GPIO pin number of the CHIPSELECT pin.
35 + *
36 + * @cs_activelow: If true, the chip is selected when the CS line is low.
37 + *
38 + * @no_spi_delay: If true, no delay is done in the lowlevel bitbanging.
39 + * Note that doing no delay is not standards compliant,
40 + * but it might be needed to speed up transfers on some
41 + * slow embedded machines.
42 + *
43 + * @boardinfo_setup: This callback is called after the
44 + * SPI master device was registered, but before the
45 + * device is registered.
46 + * @boardinfo_setup_data: Data argument passed to boardinfo_setup().
47 + */
48 +struct spi_gpio_platform_data {
49 + unsigned int pin_clk;
50 + unsigned int pin_miso;
51 + unsigned int pin_mosi;
52 + unsigned int pin_cs;
53 + bool cs_activelow;
54 + bool no_spi_delay;
55 + int (*boardinfo_setup)(struct spi_board_info *bi,
56 + struct spi_master *master,
57 + void *data);
58 + void *boardinfo_setup_data;
59 +};
60 +
61 +/**
62 + * SPI_GPIO_PLATDEV_NAME - The platform device name string.
63 + *
64 + * The name string that has to be used for platform_device_alloc
65 + * when allocating a spi-gpio device.
66 + */
67 +#define SPI_GPIO_PLATDEV_NAME "spi-gpio"
68 +
69 +/**
70 + * spi_gpio_next_id - Get another platform device ID number.
71 + *
72 + * This returns the next platform device ID number that has to be used
73 + * for platform_device_alloc. The ID is opaque and should not be used for
74 + * anything else.
75 + */
76 +int spi_gpio_next_id(void);
77 +
78 +#endif /* _LINUX_SPI_SPI_GPIO */
79 Index: linux-2.6.25.10/drivers/spi/spi_gpio.c
80 ===================================================================
81 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
82 +++ linux-2.6.25.10/drivers/spi/spi_gpio.c 2008-07-20 20:10:21.000000000 +0200
83 @@ -0,0 +1,251 @@
84 +/*
85 + * Bitbanging SPI bus driver using GPIO API
86 + *
87 + * Copyright (c) 2008 Piotr Skamruk
88 + * Copyright (c) 2008 Michael Buesch
89 + *
90 + * based on spi_s3c2410_gpio.c
91 + * Copyright (c) 2006 Ben Dooks
92 + * Copyright (c) 2006 Simtec Electronics
93 + * and on i2c-gpio.c
94 + * Copyright (C) 2007 Atmel Corporation
95 + *
96 + * This program is free software; you can redistribute it and/or modify
97 + * it under the terms of the GNU General Public License version 2 as
98 + * published by the Free Software Foundation.
99 + */
100 +
101 +#include <linux/kernel.h>
102 +#include <linux/init.h>
103 +#include <linux/delay.h>
104 +#include <linux/spinlock.h>
105 +#include <linux/workqueue.h>
106 +#include <linux/module.h>
107 +#include <linux/platform_device.h>
108 +#include <linux/spi/spi.h>
109 +#include <linux/spi/spi_bitbang.h>
110 +#include <linux/spi/spi_gpio.h>
111 +#include <linux/gpio.h>
112 +#include <asm/atomic.h>
113 +
114 +
115 +struct spi_gpio {
116 + struct spi_bitbang bitbang;
117 + struct spi_gpio_platform_data *info;
118 + struct platform_device *pdev;
119 + struct spi_board_info bi;
120 +};
121 +
122 +
123 +static inline struct spi_gpio *spidev_to_sg(struct spi_device *dev)
124 +{
125 + return dev->controller_data;
126 +}
127 +
128 +static inline void setsck(struct spi_device *dev, int val)
129 +{
130 + struct spi_gpio *sp = spidev_to_sg(dev);
131 + gpio_set_value(sp->info->pin_clk, val ? 1 : 0);
132 +}
133 +
134 +static inline void setmosi(struct spi_device *dev, int val)
135 +{
136 + struct spi_gpio *sp = spidev_to_sg(dev);
137 + gpio_set_value(sp->info->pin_mosi, val ? 1 : 0);
138 +}
139 +
140 +static inline u32 getmiso(struct spi_device *dev)
141 +{
142 + struct spi_gpio *sp = spidev_to_sg(dev);
143 + return gpio_get_value(sp->info->pin_miso) ? 1 : 0;
144 +}
145 +
146 +static inline void do_spidelay(struct spi_device *dev, unsigned nsecs)
147 +{
148 + struct spi_gpio *sp = spidev_to_sg(dev);
149 +
150 + if (!sp->info->no_spi_delay)
151 + ndelay(nsecs);
152 +}
153 +
154 +#define spidelay(nsecs) do { \
155 + /* Steal the spi_device pointer from our caller. \
156 + * The bitbang-API should probably get fixed here... */ \
157 + do_spidelay(spi, nsecs); \
158 + } while (0)
159 +
160 +#define EXPAND_BITBANG_TXRX
161 +#include <linux/spi/spi_bitbang.h>
162 +
163 +static u32 spi_gpio_txrx_mode0(struct spi_device *spi,
164 + unsigned nsecs, u32 word, u8 bits)
165 +{
166 + return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
167 +}
168 +
169 +static u32 spi_gpio_txrx_mode1(struct spi_device *spi,
170 + unsigned nsecs, u32 word, u8 bits)
171 +{
172 + return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
173 +}
174 +
175 +static u32 spi_gpio_txrx_mode2(struct spi_device *spi,
176 + unsigned nsecs, u32 word, u8 bits)
177 +{
178 + return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits);
179 +}
180 +
181 +static u32 spi_gpio_txrx_mode3(struct spi_device *spi,
182 + unsigned nsecs, u32 word, u8 bits)
183 +{
184 + return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits);
185 +}
186 +
187 +static void spi_gpio_chipselect(struct spi_device *dev, int on)
188 +{
189 + struct spi_gpio *sp = spidev_to_sg(dev);
190 +
191 + if (sp->info->cs_activelow)
192 + on = !on;
193 + gpio_set_value(sp->info->pin_cs, on ? 1 : 0);
194 +}
195 +
196 +static int spi_gpio_probe(struct platform_device *pdev)
197 +{
198 + struct spi_master *master;
199 + struct spi_gpio_platform_data *pdata;
200 + struct spi_gpio *sp;
201 + struct spi_device *spidev;
202 + int err;
203 +
204 + pdata = pdev->dev.platform_data;
205 + if (!pdata)
206 + return -ENXIO;
207 +
208 + err = -ENOMEM;
209 + master = spi_alloc_master(&pdev->dev, sizeof(struct spi_gpio));
210 + if (!master)
211 + goto err_alloc_master;
212 +
213 + sp = spi_master_get_devdata(master);
214 + platform_set_drvdata(pdev, sp);
215 + sp->info = pdata;
216 +
217 + err = gpio_request(pdata->pin_clk, "spi_clock");
218 + if (err)
219 + goto err_request_clk;
220 + err = gpio_request(pdata->pin_mosi, "spi_mosi");
221 + if (err)
222 + goto err_request_mosi;
223 + err = gpio_request(pdata->pin_miso, "spi_miso");
224 + if (err)
225 + goto err_request_miso;
226 + err = gpio_request(pdata->pin_cs, "spi_cs");
227 + if (err)
228 + goto err_request_cs;
229 +
230 + sp->bitbang.master = spi_master_get(master);
231 + sp->bitbang.master->bus_num = -1;
232 + sp->bitbang.master->num_chipselect = 1;
233 + sp->bitbang.chipselect = spi_gpio_chipselect;
234 + sp->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_mode0;
235 + sp->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_mode1;
236 + sp->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_mode2;
237 + sp->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_mode3;
238 +
239 + gpio_direction_output(pdata->pin_clk, 0);
240 + gpio_direction_output(pdata->pin_mosi, 0);
241 + gpio_direction_output(pdata->pin_cs,
242 + pdata->cs_activelow ? 1 : 0);
243 + gpio_direction_input(pdata->pin_miso);
244 +
245 + err = spi_bitbang_start(&sp->bitbang);
246 + if (err)
247 + goto err_no_bitbang;
248 + err = pdata->boardinfo_setup(&sp->bi, master,
249 + pdata->boardinfo_setup_data);
250 + if (err)
251 + goto err_bi_setup;
252 + sp->bi.controller_data = sp;
253 + spidev = spi_new_device(master, &sp->bi);
254 + if (!spidev)
255 + goto err_new_dev;
256 +
257 + return 0;
258 +
259 +err_new_dev:
260 +err_bi_setup:
261 + spi_bitbang_stop(&sp->bitbang);
262 +err_no_bitbang:
263 + spi_master_put(sp->bitbang.master);
264 + gpio_free(pdata->pin_cs);
265 +err_request_cs:
266 + gpio_free(pdata->pin_miso);
267 +err_request_miso:
268 + gpio_free(pdata->pin_mosi);
269 +err_request_mosi:
270 + gpio_free(pdata->pin_clk);
271 +err_request_clk:
272 + kfree(master);
273 +
274 +err_alloc_master:
275 + return err;
276 +}
277 +
278 +static int __devexit spi_gpio_remove(struct platform_device *pdev)
279 +{
280 + struct spi_gpio *sp;
281 + struct spi_gpio_platform_data *pdata;
282 +
283 + pdata = pdev->dev.platform_data;
284 + sp = platform_get_drvdata(pdev);
285 +
286 + gpio_free(pdata->pin_clk);
287 + gpio_free(pdata->pin_mosi);
288 + gpio_free(pdata->pin_miso);
289 + gpio_free(pdata->pin_cs);
290 + spi_bitbang_stop(&sp->bitbang);
291 + spi_master_put(sp->bitbang.master);
292 +
293 + return 0;
294 +}
295 +
296 +static struct platform_driver spi_gpio_driver = {
297 + .driver = {
298 + .name = SPI_GPIO_PLATDEV_NAME,
299 + .owner = THIS_MODULE,
300 + },
301 + .probe = spi_gpio_probe,
302 + .remove = __devexit_p(spi_gpio_remove),
303 +};
304 +
305 +int spi_gpio_next_id(void)
306 +{
307 + static atomic_t counter = ATOMIC_INIT(-1);
308 +
309 + return atomic_inc_return(&counter);
310 +}
311 +EXPORT_SYMBOL(spi_gpio_next_id);
312 +
313 +static int __init spi_gpio_init(void)
314 +{
315 + int err;
316 +
317 + err = platform_driver_register(&spi_gpio_driver);
318 + if (err)
319 + printk(KERN_ERR "spi-gpio: register failed: %d\n", err);
320 +
321 + return err;
322 +}
323 +module_init(spi_gpio_init);
324 +
325 +static void __exit spi_gpio_exit(void)
326 +{
327 + platform_driver_unregister(&spi_gpio_driver);
328 +}
329 +module_exit(spi_gpio_exit);
330 +
331 +MODULE_AUTHOR("Piot Skamruk <piotr.skamruk at gmail.com>");
332 +MODULE_AUTHOR("Michael Buesch");
333 +MODULE_DESCRIPTION("Platform independent GPIO bitbanging SPI driver");
334 +MODULE_LICENSE("GPL v2");
335 Index: linux-2.6.25.10/drivers/spi/Kconfig
336 ===================================================================
337 --- linux-2.6.25.10.orig/drivers/spi/Kconfig 2008-07-20 20:09:48.000000000 +0200
338 +++ linux-2.6.25.10/drivers/spi/Kconfig 2008-07-20 20:11:48.000000000 +0200
339 @@ -100,6 +100,19 @@ config SPI_BUTTERFLY
340 inexpensive battery powered microcontroller evaluation board.
341 This same cable can be used to flash new firmware.
342
343 +config SPI_GPIO
344 + tristate "GPIO API based bitbanging SPI controller"
345 + depends on SPI_MASTER && GENERIC_GPIO
346 + select SPI_BITBANG
347 + help
348 + This is a platform driver that can be used for bitbanging
349 + an SPI bus over GPIO pins.
350 + Select this if you have any SPI device that is connected via
351 + GPIO pins.
352 + The module will be called spi_gpio.
353 +
354 + If unsure, say N.
355 +
356 config SPI_IMX
357 tristate "Freescale iMX SPI controller"
358 depends on SPI_MASTER && ARCH_IMX && EXPERIMENTAL
359 Index: linux-2.6.25.10/drivers/spi/Makefile
360 ===================================================================
361 --- linux-2.6.25.10.orig/drivers/spi/Makefile 2008-07-20 20:09:48.000000000 +0200
362 +++ linux-2.6.25.10/drivers/spi/Makefile 2008-07-20 20:10:21.000000000 +0200
363 @@ -16,6 +16,7 @@ obj-$(CONFIG_SPI_BFIN) += spi_bfin5xx.
364 obj-$(CONFIG_SPI_BITBANG) += spi_bitbang.o
365 obj-$(CONFIG_SPI_AU1550) += au1550_spi.o
366 obj-$(CONFIG_SPI_BUTTERFLY) += spi_butterfly.o
367 +obj-$(CONFIG_SPI_GPIO) += spi_gpio.o
368 obj-$(CONFIG_SPI_IMX) += spi_imx.o
369 obj-$(CONFIG_SPI_LM70_LLP) += spi_lm70llp.o
370 obj-$(CONFIG_SPI_PXA2XX) += pxa2xx_spi.o
371 Index: linux-2.6.25.10/MAINTAINERS
372 ===================================================================
373 --- linux-2.6.25.10.orig/MAINTAINERS 2008-07-20 20:09:48.000000000 +0200
374 +++ linux-2.6.25.10/MAINTAINERS 2008-07-20 20:10:59.000000000 +0200
375 @@ -3685,6 +3685,11 @@ M: dbrownell@users.sourceforge.net
376 L: spi-devel-general@lists.sourceforge.net
377 S: Maintained
378
379 +SPI GPIO MASTER DRIVER
380 +P: Michael Buesch
381 +M: mb@bu3sch.de
382 +S: Maintained
383 +
384 STABLE BRANCH:
385 P: Greg Kroah-Hartman
386 M: greg@kroah.com