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