[ar71xx] parse the board parameter from the command line first
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.28 / 920-spi-gpio.patch
1 Port of the SPI-GPIO driver from 2.6.29-rc4.
2
3 --mb
4
5
6
7 Index: linux-2.6.28.2/drivers/spi/spi_gpio.c
8 ===================================================================
9 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
10 +++ linux-2.6.28.2/drivers/spi/spi_gpio.c       2009-02-10 17:56:59.000000000 +0100
11 @@ -0,0 +1,360 @@
12 +/*
13 + * spi_gpio.c - SPI master driver using generic bitbanged GPIO
14 + *
15 + * Copyright (C) 2006,2008 David Brownell
16 + *
17 + * This program is free software; you can redistribute it and/or modify
18 + * it under the terms of the GNU General Public License as published by
19 + * the Free Software Foundation; either version 2 of the License, or
20 + * (at your option) any later version.
21 + *
22 + * This program is distributed in the hope that it will be useful,
23 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 + * GNU General Public License for more details.
26 + *
27 + * You should have received a copy of the GNU General Public License
28 + * along with this program; if not, write to the Free Software
29 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 + */
31 +#include <linux/kernel.h>
32 +#include <linux/init.h>
33 +#include <linux/platform_device.h>
34 +#include <linux/gpio.h>
35 +
36 +#include <linux/spi/spi.h>
37 +#include <linux/spi/spi_bitbang.h>
38 +#include <linux/spi/spi_gpio.h>
39 +
40 +
41 +/*
42 + * This bitbanging SPI master driver should help make systems usable
43 + * when a native hardware SPI engine is not available, perhaps because
44 + * its driver isn't yet working or because the I/O pins it requires
45 + * are used for other purposes.
46 + *
47 + * platform_device->driver_data ... points to spi_gpio
48 + *
49 + * spi->controller_state ... reserved for bitbang framework code
50 + * spi->controller_data ... holds chipselect GPIO
51 + *
52 + * spi->master->dev.driver_data ... points to spi_gpio->bitbang
53 + */
54 +
55 +struct spi_gpio {
56 +       struct spi_bitbang              bitbang;
57 +       struct spi_gpio_platform_data   pdata;
58 +       struct platform_device          *pdev;
59 +};
60 +
61 +/*----------------------------------------------------------------------*/
62 +
63 +/*
64 + * Because the overhead of going through four GPIO procedure calls
65 + * per transferred bit can make performance a problem, this code
66 + * is set up so that you can use it in either of two ways:
67 + *
68 + *   - The slow generic way:  set up platform_data to hold the GPIO
69 + *     numbers used for MISO/MOSI/SCK, and issue procedure calls for
70 + *     each of them.  This driver can handle several such busses.
71 + *
72 + *   - The quicker inlined way:  only helps with platform GPIO code
73 + *     that inlines operations for constant GPIOs.  This can give
74 + *     you tight (fast!) inner loops, but each such bus needs a
75 + *     new driver.  You'll define a new C file, with Makefile and
76 + *     Kconfig support; the C code can be a total of six lines:
77 + *
78 + *             #define DRIVER_NAME     "myboard_spi2"
79 + *             #define SPI_MISO_GPIO   119
80 + *             #define SPI_MOSI_GPIO   120
81 + *             #define SPI_SCK_GPIO    121
82 + *             #define SPI_N_CHIPSEL   4
83 + *             #include "spi_gpio.c"
84 + */
85 +
86 +#ifndef DRIVER_NAME
87 +#define DRIVER_NAME    "spi_gpio"
88 +
89 +#define GENERIC_BITBANG        /* vs tight inlines */
90 +
91 +/* all functions referencing these symbols must define pdata */
92 +#define SPI_MISO_GPIO  ((pdata)->miso)
93 +#define SPI_MOSI_GPIO  ((pdata)->mosi)
94 +#define SPI_SCK_GPIO   ((pdata)->sck)
95 +
96 +#define SPI_N_CHIPSEL  ((pdata)->num_chipselect)
97 +
98 +#endif
99 +
100 +/*----------------------------------------------------------------------*/
101 +
102 +static inline const struct spi_gpio_platform_data * __pure
103 +spi_to_pdata(const struct spi_device *spi)
104 +{
105 +       const struct spi_bitbang        *bang;
106 +       const struct spi_gpio           *spi_gpio;
107 +
108 +       bang = spi_master_get_devdata(spi->master);
109 +       spi_gpio = container_of(bang, struct spi_gpio, bitbang);
110 +       return &spi_gpio->pdata;
111 +}
112 +
113 +/* this is #defined to avoid unused-variable warnings when inlining */
114 +#define pdata          spi_to_pdata(spi)
115 +
116 +static inline void setsck(const struct spi_device *spi, int is_on)
117 +{
118 +       gpio_set_value(SPI_SCK_GPIO, is_on);
119 +}
120 +
121 +static inline void setmosi(const struct spi_device *spi, int is_on)
122 +{
123 +       gpio_set_value(SPI_MOSI_GPIO, is_on);
124 +}
125 +
126 +static inline int getmiso(const struct spi_device *spi)
127 +{
128 +       return gpio_get_value(SPI_MISO_GPIO);
129 +}
130 +
131 +#undef pdata
132 +
133 +/*
134 + * NOTE:  this clocks "as fast as we can".  It "should" be a function of the
135 + * requested device clock.  Software overhead means we usually have trouble
136 + * reaching even one Mbit/sec (except when we can inline bitops), so for now
137 + * we'll just assume we never need additional per-bit slowdowns.
138 + */
139 +#define spidelay(nsecs)        do {} while (0)
140 +
141 +#define        EXPAND_BITBANG_TXRX
142 +#include <linux/spi/spi_bitbang.h>
143 +
144 +/*
145 + * These functions can leverage inline expansion of GPIO calls to shrink
146 + * costs for a txrx bit, often by factors of around ten (by instruction
147 + * count).  That is particularly visible for larger word sizes, but helps
148 + * even with default 8-bit words.
149 + *
150 + * REVISIT overheads calling these functions for each word also have
151 + * significant performance costs.  Having txrx_bufs() calls that inline
152 + * the txrx_word() logic would help performance, e.g. on larger blocks
153 + * used with flash storage or MMC/SD.  There should also be ways to make
154 + * GCC be less stupid about reloading registers inside the I/O loops,
155 + * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
156 + */
157 +
158 +static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
159 +               unsigned nsecs, u32 word, u8 bits)
160 +{
161 +       return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
162 +}
163 +
164 +static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
165 +               unsigned nsecs, u32 word, u8 bits)
166 +{
167 +       return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
168 +}
169 +
170 +static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
171 +               unsigned nsecs, u32 word, u8 bits)
172 +{
173 +       return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits);
174 +}
175 +
176 +static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
177 +               unsigned nsecs, u32 word, u8 bits)
178 +{
179 +       return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits);
180 +}
181 +
182 +/*----------------------------------------------------------------------*/
183 +
184 +static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
185 +{
186 +       unsigned long cs = (unsigned long) spi->controller_data;
187 +
188 +       /* set initial clock polarity */
189 +       if (is_active)
190 +               setsck(spi, spi->mode & SPI_CPOL);
191 +
192 +       /* SPI is normally active-low */
193 +       gpio_set_value(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
194 +}
195 +
196 +static int spi_gpio_setup(struct spi_device *spi)
197 +{
198 +       unsigned long   cs = (unsigned long) spi->controller_data;
199 +       int             status = 0;
200 +
201 +       if (spi->bits_per_word > 32)
202 +               return -EINVAL;
203 +
204 +       if (!spi->controller_state) {
205 +               status = gpio_request(cs, spi->dev.bus_id);
206 +               if (status)
207 +                       return status;
208 +               status = gpio_direction_output(cs, spi->mode & SPI_CS_HIGH);
209 +       }
210 +       if (!status)
211 +               status = spi_bitbang_setup(spi);
212 +       if (status) {
213 +               if (!spi->controller_state)
214 +                       gpio_free(cs);
215 +       }
216 +       return status;
217 +}
218 +
219 +static void spi_gpio_cleanup(struct spi_device *spi)
220 +{
221 +       unsigned long   cs = (unsigned long) spi->controller_data;
222 +
223 +       gpio_free(cs);
224 +       spi_bitbang_cleanup(spi);
225 +}
226 +
227 +static int __init spi_gpio_alloc(unsigned pin, const char *label, bool is_in)
228 +{
229 +       int value;
230 +
231 +       value = gpio_request(pin, label);
232 +       if (value == 0) {
233 +               if (is_in)
234 +                       value = gpio_direction_input(pin);
235 +               else
236 +                       value = gpio_direction_output(pin, 0);
237 +       }
238 +       return value;
239 +}
240 +
241 +static int __init
242 +spi_gpio_request(struct spi_gpio_platform_data *pdata, const char *label)
243 +{
244 +       int value;
245 +
246 +       /* NOTE:  SPI_*_GPIO symbols may reference "pdata" */
247 +
248 +       value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false);
249 +       if (value)
250 +               goto done;
251 +
252 +       value = spi_gpio_alloc(SPI_MISO_GPIO, label, true);
253 +       if (value)
254 +               goto free_mosi;
255 +
256 +       value = spi_gpio_alloc(SPI_SCK_GPIO, label, false);
257 +       if (value)
258 +               goto free_miso;
259 +
260 +       goto done;
261 +
262 +free_miso:
263 +       gpio_free(SPI_MISO_GPIO);
264 +free_mosi:
265 +       gpio_free(SPI_MOSI_GPIO);
266 +done:
267 +       return value;
268 +}
269 +
270 +static int __init spi_gpio_probe(struct platform_device *pdev)
271 +{
272 +       int                             status;
273 +       struct spi_master               *master;
274 +       struct spi_gpio                 *spi_gpio;
275 +       struct spi_gpio_platform_data   *pdata;
276 +
277 +       pdata = pdev->dev.platform_data;
278 +#ifdef GENERIC_BITBANG
279 +       if (!pdata || !pdata->num_chipselect)
280 +               return -ENODEV;
281 +#endif
282 +
283 +       status = spi_gpio_request(pdata, dev_name(&pdev->dev));
284 +       if (status < 0)
285 +               return status;
286 +
287 +       master = spi_alloc_master(&pdev->dev, sizeof *spi_gpio);
288 +       if (!master) {
289 +               status = -ENOMEM;
290 +               goto gpio_free;
291 +       }
292 +       spi_gpio = spi_master_get_devdata(master);
293 +       platform_set_drvdata(pdev, spi_gpio);
294 +
295 +       spi_gpio->pdev = pdev;
296 +       if (pdata)
297 +               spi_gpio->pdata = *pdata;
298 +
299 +       master->bus_num = pdev->id;
300 +       master->num_chipselect = SPI_N_CHIPSEL;
301 +       master->setup = spi_gpio_setup;
302 +       master->cleanup = spi_gpio_cleanup;
303 +
304 +       spi_gpio->bitbang.master = spi_master_get(master);
305 +       spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
306 +       spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
307 +       spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
308 +       spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
309 +       spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
310 +       spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
311 +       spi_gpio->bitbang.flags = SPI_CS_HIGH;
312 +
313 +       status = spi_bitbang_start(&spi_gpio->bitbang);
314 +       if (status < 0) {
315 +               spi_master_put(spi_gpio->bitbang.master);
316 +gpio_free:
317 +               gpio_free(SPI_MISO_GPIO);
318 +               gpio_free(SPI_MOSI_GPIO);
319 +               gpio_free(SPI_SCK_GPIO);
320 +               spi_master_put(master);
321 +       }
322 +
323 +       return status;
324 +}
325 +
326 +static int __exit spi_gpio_remove(struct platform_device *pdev)
327 +{
328 +       struct spi_gpio                 *spi_gpio;
329 +       struct spi_gpio_platform_data   *pdata;
330 +       int                             status;
331 +
332 +       spi_gpio = platform_get_drvdata(pdev);
333 +       pdata = pdev->dev.platform_data;
334 +
335 +       /* stop() unregisters child devices too */
336 +       status = spi_bitbang_stop(&spi_gpio->bitbang);
337 +       spi_master_put(spi_gpio->bitbang.master);
338 +
339 +       platform_set_drvdata(pdev, NULL);
340 +
341 +       gpio_free(SPI_MISO_GPIO);
342 +       gpio_free(SPI_MOSI_GPIO);
343 +       gpio_free(SPI_SCK_GPIO);
344 +
345 +       return status;
346 +}
347 +
348 +MODULE_ALIAS("platform:" DRIVER_NAME);
349 +
350 +static struct platform_driver spi_gpio_driver = {
351 +       .driver.name    = DRIVER_NAME,
352 +       .driver.owner   = THIS_MODULE,
353 +       .remove         = __exit_p(spi_gpio_remove),
354 +};
355 +
356 +static int __init spi_gpio_init(void)
357 +{
358 +       return platform_driver_probe(&spi_gpio_driver, spi_gpio_probe);
359 +}
360 +module_init(spi_gpio_init);
361 +
362 +static void __exit spi_gpio_exit(void)
363 +{
364 +       platform_driver_unregister(&spi_gpio_driver);
365 +}
366 +module_exit(spi_gpio_exit);
367 +
368 +
369 +MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
370 +MODULE_AUTHOR("David Brownell");
371 +MODULE_LICENSE("GPL");
372 Index: linux-2.6.28.2/include/linux/spi/spi_gpio.h
373 ===================================================================
374 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
375 +++ linux-2.6.28.2/include/linux/spi/spi_gpio.h 2009-02-10 17:56:49.000000000 +0100
376 @@ -0,0 +1,60 @@
377 +#ifndef __LINUX_SPI_GPIO_H
378 +#define __LINUX_SPI_GPIO_H
379 +
380 +/*
381 + * For each bitbanged SPI bus, set up a platform_device node with:
382 + *   - name "spi_gpio"
383 + *   - id the same as the SPI bus number it implements
384 + *   - dev.platform data pointing to a struct spi_gpio_platform_data
385 + *
386 + * Or, see the driver code for information about speedups that are
387 + * possible on platforms that support inlined access for GPIOs (no
388 + * spi_gpio_platform_data is used).
389 + *
390 + * Use spi_board_info with these busses in the usual way, being sure
391 + * that the controller_data being the GPIO used for each device's
392 + * chipselect:
393 + *
394 + *     static struct spi_board_info ... [] = {
395 + *     ...
396 + *             // this slave uses GPIO 42 for its chipselect
397 + *             .controller_data = (void *) 42,
398 + *     ...
399 + *             // this one uses GPIO 86 for its chipselect
400 + *             .controller_data = (void *) 86,
401 + *     ...
402 + *     };
403 + *
404 + * If the bitbanged bus is later switched to a "native" controller,
405 + * that platform_device and controller_data should be removed.
406 + */
407 +
408 +/**
409 + * struct spi_gpio_platform_data - parameter for bitbanged SPI master
410 + * @sck: number of the GPIO used for clock output
411 + * @mosi: number of the GPIO used for Master Output, Slave In (MOSI) data
412 + * @miso: number of the GPIO used for Master Input, Slave Output (MISO) data
413 + * @num_chipselect: how many slaves to allow
414 + *
415 + * All GPIO signals used with the SPI bus managed through this driver
416 + * (chipselects, MOSI, MISO, SCK) must be configured as GPIOs, instead
417 + * of some alternate function.
418 + *
419 + * It can be convenient to use this driver with pins that have alternate
420 + * functions associated with a "native" SPI controller if a driver for that
421 + * controller is not available, or is missing important functionality.
422 + *
423 + * On platforms which can do so, configure MISO with a weak pullup unless
424 + * there's an external pullup on that signal.  That saves power by avoiding
425 + * floating signals.  (A weak pulldown would save power too, but many
426 + * drivers expect to see all-ones data as the no slave "response".)
427 + */
428 +struct spi_gpio_platform_data {
429 +       unsigned        sck;
430 +       unsigned        mosi;
431 +       unsigned        miso;
432 +
433 +       u16             num_chipselect;
434 +};
435 +
436 +#endif /* __LINUX_SPI_GPIO_H */
437 Index: linux-2.6.28.2/drivers/spi/Kconfig
438 ===================================================================
439 --- linux-2.6.28.2.orig/drivers/spi/Kconfig     2009-02-10 17:57:10.000000000 +0100
440 +++ linux-2.6.28.2/drivers/spi/Kconfig  2009-02-10 18:08:31.000000000 +0100
441 @@ -100,6 +100,22 @@ config SPI_BUTTERFLY
442           inexpensive battery powered microcontroller evaluation board.
443           This same cable can be used to flash new firmware.
444  
445 +config SPI_GPIO
446 +       tristate "GPIO-based bitbanging SPI Master"
447 +       depends on GENERIC_GPIO
448 +       select SPI_BITBANG
449 +       help
450 +         This simple GPIO bitbanging SPI master uses the arch-neutral GPIO
451 +         interface to manage MOSI, MISO, SCK, and chipselect signals.  SPI
452 +         slaves connected to a bus using this driver are configured as usual,
453 +         except that the spi_board_info.controller_data holds the GPIO number
454 +         for the chipselect used by this controller driver.
455 +
456 +         Note that this driver often won't achieve even 1 Mbit/sec speeds,
457 +         making it unusually slow for SPI.  If your platform can inline
458 +         GPIO operations, you should be able to leverage that for better
459 +         speed with a custom version of this driver; see the source code.
460 +
461  config SPI_IMX
462         tristate "Freescale iMX SPI controller"
463         depends on ARCH_IMX && EXPERIMENTAL
464 Index: linux-2.6.28.2/drivers/spi/Makefile
465 ===================================================================
466 --- linux-2.6.28.2.orig/drivers/spi/Makefile    2009-02-10 17:58:46.000000000 +0100
467 +++ linux-2.6.28.2/drivers/spi/Makefile 2009-02-10 18:08:31.000000000 +0100
468 @@ -16,6 +16,7 @@ obj-$(CONFIG_SPI_BFIN)                        += spi_bfin5xx.
469  obj-$(CONFIG_SPI_BITBANG)              += spi_bitbang.o
470  obj-$(CONFIG_SPI_AU1550)               += au1550_spi.o
471  obj-$(CONFIG_SPI_BUTTERFLY)            += spi_butterfly.o
472 +obj-$(CONFIG_SPI_GPIO)                 += spi_gpio.o
473  obj-$(CONFIG_SPI_IMX)                  += spi_imx.o
474  obj-$(CONFIG_SPI_LM70_LLP)             += spi_lm70llp.o
475  obj-$(CONFIG_SPI_PXA2XX)               += pxa2xx_spi.o