1 Port of the SPI-GPIO driver from 2.6.29-rc4.
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
13 + * spi_gpio.c - SPI master driver using generic bitbanged GPIO
15 + * Copyright (C) 2006,2008 David Brownell
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.
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.
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.
31 +#include <linux/kernel.h>
32 +#include <linux/init.h>
33 +#include <linux/platform_device.h>
34 +#include <linux/gpio.h>
36 +#include <linux/spi/spi.h>
37 +#include <linux/spi/spi_bitbang.h>
38 +#include <linux/spi/spi_gpio.h>
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.
47 + * platform_device->driver_data ... points to spi_gpio
49 + * spi->controller_state ... reserved for bitbang framework code
50 + * spi->controller_data ... holds chipselect GPIO
52 + * spi->master->dev.driver_data ... points to spi_gpio->bitbang
56 + struct spi_bitbang bitbang;
57 + struct spi_gpio_platform_data pdata;
58 + struct platform_device *pdev;
61 +/*----------------------------------------------------------------------*/
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:
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.
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:
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"
87 +#define DRIVER_NAME "spi_gpio"
89 +#define GENERIC_BITBANG /* vs tight inlines */
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)
96 +#define SPI_N_CHIPSEL ((pdata)->num_chipselect)
100 +/*----------------------------------------------------------------------*/
102 +static inline const struct spi_gpio_platform_data * __pure
103 +spi_to_pdata(const struct spi_device *spi)
105 + const struct spi_bitbang *bang;
106 + const struct spi_gpio *spi_gpio;
108 + bang = spi_master_get_devdata(spi->master);
109 + spi_gpio = container_of(bang, struct spi_gpio, bitbang);
110 + return &spi_gpio->pdata;
113 +/* this is #defined to avoid unused-variable warnings when inlining */
114 +#define pdata spi_to_pdata(spi)
116 +static inline void setsck(const struct spi_device *spi, int is_on)
118 + gpio_set_value(SPI_SCK_GPIO, is_on);
121 +static inline void setmosi(const struct spi_device *spi, int is_on)
123 + gpio_set_value(SPI_MOSI_GPIO, is_on);
126 +static inline int getmiso(const struct spi_device *spi)
128 + return gpio_get_value(SPI_MISO_GPIO);
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.
139 +#define spidelay(nsecs) do {} while (0)
141 +#define EXPAND_BITBANG_TXRX
142 +#include <linux/spi/spi_bitbang.h>
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.
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?
158 +static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
159 + unsigned nsecs, u32 word, u8 bits)
161 + return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
164 +static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
165 + unsigned nsecs, u32 word, u8 bits)
167 + return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
170 +static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
171 + unsigned nsecs, u32 word, u8 bits)
173 + return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits);
176 +static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
177 + unsigned nsecs, u32 word, u8 bits)
179 + return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits);
182 +/*----------------------------------------------------------------------*/
184 +static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
186 + unsigned long cs = (unsigned long) spi->controller_data;
188 + /* set initial clock polarity */
190 + setsck(spi, spi->mode & SPI_CPOL);
192 + /* SPI is normally active-low */
193 + gpio_set_value(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
196 +static int spi_gpio_setup(struct spi_device *spi)
198 + unsigned long cs = (unsigned long) spi->controller_data;
201 + if (spi->bits_per_word > 32)
204 + if (!spi->controller_state) {
205 + status = gpio_request(cs, spi->dev.bus_id);
208 + status = gpio_direction_output(cs, spi->mode & SPI_CS_HIGH);
211 + status = spi_bitbang_setup(spi);
213 + if (!spi->controller_state)
219 +static void spi_gpio_cleanup(struct spi_device *spi)
221 + unsigned long cs = (unsigned long) spi->controller_data;
224 + spi_bitbang_cleanup(spi);
227 +static int __init spi_gpio_alloc(unsigned pin, const char *label, bool is_in)
231 + value = gpio_request(pin, label);
234 + value = gpio_direction_input(pin);
236 + value = gpio_direction_output(pin, 0);
242 +spi_gpio_request(struct spi_gpio_platform_data *pdata, const char *label)
246 + /* NOTE: SPI_*_GPIO symbols may reference "pdata" */
248 + value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false);
252 + value = spi_gpio_alloc(SPI_MISO_GPIO, label, true);
256 + value = spi_gpio_alloc(SPI_SCK_GPIO, label, false);
263 + gpio_free(SPI_MISO_GPIO);
265 + gpio_free(SPI_MOSI_GPIO);
270 +static int __init spi_gpio_probe(struct platform_device *pdev)
273 + struct spi_master *master;
274 + struct spi_gpio *spi_gpio;
275 + struct spi_gpio_platform_data *pdata;
277 + pdata = pdev->dev.platform_data;
278 +#ifdef GENERIC_BITBANG
279 + if (!pdata || !pdata->num_chipselect)
283 + status = spi_gpio_request(pdata, dev_name(&pdev->dev));
287 + master = spi_alloc_master(&pdev->dev, sizeof *spi_gpio);
292 + spi_gpio = spi_master_get_devdata(master);
293 + platform_set_drvdata(pdev, spi_gpio);
295 + spi_gpio->pdev = pdev;
297 + spi_gpio->pdata = *pdata;
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;
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;
313 + status = spi_bitbang_start(&spi_gpio->bitbang);
315 + spi_master_put(spi_gpio->bitbang.master);
317 + gpio_free(SPI_MISO_GPIO);
318 + gpio_free(SPI_MOSI_GPIO);
319 + gpio_free(SPI_SCK_GPIO);
320 + spi_master_put(master);
326 +static int __exit spi_gpio_remove(struct platform_device *pdev)
328 + struct spi_gpio *spi_gpio;
329 + struct spi_gpio_platform_data *pdata;
332 + spi_gpio = platform_get_drvdata(pdev);
333 + pdata = pdev->dev.platform_data;
335 + /* stop() unregisters child devices too */
336 + status = spi_bitbang_stop(&spi_gpio->bitbang);
337 + spi_master_put(spi_gpio->bitbang.master);
339 + platform_set_drvdata(pdev, NULL);
341 + gpio_free(SPI_MISO_GPIO);
342 + gpio_free(SPI_MOSI_GPIO);
343 + gpio_free(SPI_SCK_GPIO);
348 +MODULE_ALIAS("platform:" DRIVER_NAME);
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),
356 +static int __init spi_gpio_init(void)
358 + return platform_driver_probe(&spi_gpio_driver, spi_gpio_probe);
360 +module_init(spi_gpio_init);
362 +static void __exit spi_gpio_exit(void)
364 + platform_driver_unregister(&spi_gpio_driver);
366 +module_exit(spi_gpio_exit);
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
377 +#ifndef __LINUX_SPI_GPIO_H
378 +#define __LINUX_SPI_GPIO_H
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
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).
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
394 + * static struct spi_board_info ... [] = {
396 + * // this slave uses GPIO 42 for its chipselect
397 + * .controller_data = (void *) 42,
399 + * // this one uses GPIO 86 for its chipselect
400 + * .controller_data = (void *) 86,
404 + * If the bitbanged bus is later switched to a "native" controller,
405 + * that platform_device and controller_data should be removed.
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
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.
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.
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".)
428 +struct spi_gpio_platform_data {
433 + u16 num_chipselect;
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.
446 + tristate "GPIO-based bitbanging SPI Master"
447 + depends on GENERIC_GPIO
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.
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.
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