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