1 Port of the SPI-GPIO driver from 2.6.29-rc4.
8 +++ b/drivers/spi/spi_gpio.c
11 + * spi_gpio.c - SPI master driver using generic bitbanged GPIO
13 + * Copyright (C) 2006,2008 David Brownell
15 + * This program is free software; you can redistribute it and/or modify
16 + * it under the terms of the GNU General Public License as published by
17 + * the Free Software Foundation; either version 2 of the License, or
18 + * (at your option) any later version.
20 + * This program is distributed in the hope that it will be useful,
21 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 + * GNU General Public License for more details.
25 + * You should have received a copy of the GNU General Public License
26 + * along with this program; if not, write to the Free Software
27 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 +#include <linux/kernel.h>
30 +#include <linux/init.h>
31 +#include <linux/platform_device.h>
32 +#include <linux/gpio.h>
34 +#include <linux/spi/spi.h>
35 +#include <linux/spi/spi_bitbang.h>
36 +#include <linux/spi/spi_gpio.h>
40 + * This bitbanging SPI master driver should help make systems usable
41 + * when a native hardware SPI engine is not available, perhaps because
42 + * its driver isn't yet working or because the I/O pins it requires
43 + * are used for other purposes.
45 + * platform_device->driver_data ... points to spi_gpio
47 + * spi->controller_state ... reserved for bitbang framework code
48 + * spi->controller_data ... holds chipselect GPIO
50 + * spi->master->dev.driver_data ... points to spi_gpio->bitbang
54 + struct spi_bitbang bitbang;
55 + struct spi_gpio_platform_data pdata;
56 + struct platform_device *pdev;
59 +/*----------------------------------------------------------------------*/
62 + * Because the overhead of going through four GPIO procedure calls
63 + * per transferred bit can make performance a problem, this code
64 + * is set up so that you can use it in either of two ways:
66 + * - The slow generic way: set up platform_data to hold the GPIO
67 + * numbers used for MISO/MOSI/SCK, and issue procedure calls for
68 + * each of them. This driver can handle several such busses.
70 + * - The quicker inlined way: only helps with platform GPIO code
71 + * that inlines operations for constant GPIOs. This can give
72 + * you tight (fast!) inner loops, but each such bus needs a
73 + * new driver. You'll define a new C file, with Makefile and
74 + * Kconfig support; the C code can be a total of six lines:
76 + * #define DRIVER_NAME "myboard_spi2"
77 + * #define SPI_MISO_GPIO 119
78 + * #define SPI_MOSI_GPIO 120
79 + * #define SPI_SCK_GPIO 121
80 + * #define SPI_N_CHIPSEL 4
81 + * #include "spi_gpio.c"
85 +#define DRIVER_NAME "spi_gpio"
87 +#define GENERIC_BITBANG /* vs tight inlines */
89 +/* all functions referencing these symbols must define pdata */
90 +#define SPI_MISO_GPIO ((pdata)->miso)
91 +#define SPI_MOSI_GPIO ((pdata)->mosi)
92 +#define SPI_SCK_GPIO ((pdata)->sck)
94 +#define SPI_N_CHIPSEL ((pdata)->num_chipselect)
98 +/*----------------------------------------------------------------------*/
100 +static inline const struct spi_gpio_platform_data * __pure
101 +spi_to_pdata(const struct spi_device *spi)
103 + const struct spi_bitbang *bang;
104 + const struct spi_gpio *spi_gpio;
106 + bang = spi_master_get_devdata(spi->master);
107 + spi_gpio = container_of(bang, struct spi_gpio, bitbang);
108 + return &spi_gpio->pdata;
111 +/* this is #defined to avoid unused-variable warnings when inlining */
112 +#define pdata spi_to_pdata(spi)
114 +static inline void setsck(const struct spi_device *spi, int is_on)
116 + gpio_set_value(SPI_SCK_GPIO, is_on);
119 +static inline void setmosi(const struct spi_device *spi, int is_on)
121 + gpio_set_value(SPI_MOSI_GPIO, is_on);
124 +static inline int getmiso(const struct spi_device *spi)
126 + return gpio_get_value(SPI_MISO_GPIO);
132 + * NOTE: this clocks "as fast as we can". It "should" be a function of the
133 + * requested device clock. Software overhead means we usually have trouble
134 + * reaching even one Mbit/sec (except when we can inline bitops), so for now
135 + * we'll just assume we never need additional per-bit slowdowns.
137 +#define spidelay(nsecs) do {} while (0)
139 +#define EXPAND_BITBANG_TXRX
140 +#include <linux/spi/spi_bitbang.h>
143 + * These functions can leverage inline expansion of GPIO calls to shrink
144 + * costs for a txrx bit, often by factors of around ten (by instruction
145 + * count). That is particularly visible for larger word sizes, but helps
146 + * even with default 8-bit words.
148 + * REVISIT overheads calling these functions for each word also have
149 + * significant performance costs. Having txrx_bufs() calls that inline
150 + * the txrx_word() logic would help performance, e.g. on larger blocks
151 + * used with flash storage or MMC/SD. There should also be ways to make
152 + * GCC be less stupid about reloading registers inside the I/O loops,
153 + * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
156 +static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
157 + unsigned nsecs, u32 word, u8 bits)
159 + return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
162 +static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
163 + unsigned nsecs, u32 word, u8 bits)
165 + return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
168 +static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
169 + unsigned nsecs, u32 word, u8 bits)
171 + return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits);
174 +static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
175 + unsigned nsecs, u32 word, u8 bits)
177 + return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits);
180 +/*----------------------------------------------------------------------*/
182 +static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
184 + unsigned long cs = (unsigned long) spi->controller_data;
186 + /* set initial clock polarity */
188 + setsck(spi, spi->mode & SPI_CPOL);
190 + /* SPI is normally active-low */
191 + gpio_set_value(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
194 +static int spi_gpio_setup(struct spi_device *spi)
196 + unsigned long cs = (unsigned long) spi->controller_data;
199 + if (spi->bits_per_word > 32)
202 + if (!spi->controller_state) {
203 + status = gpio_request(cs, spi->dev.bus_id);
206 + status = gpio_direction_output(cs, spi->mode & SPI_CS_HIGH);
209 + status = spi_bitbang_setup(spi);
211 + if (!spi->controller_state)
217 +static void spi_gpio_cleanup(struct spi_device *spi)
219 + unsigned long cs = (unsigned long) spi->controller_data;
222 + spi_bitbang_cleanup(spi);
225 +static int __init spi_gpio_alloc(unsigned pin, const char *label, bool is_in)
229 + value = gpio_request(pin, label);
232 + value = gpio_direction_input(pin);
234 + value = gpio_direction_output(pin, 0);
240 +spi_gpio_request(struct spi_gpio_platform_data *pdata, const char *label)
244 + /* NOTE: SPI_*_GPIO symbols may reference "pdata" */
246 + value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false);
250 + value = spi_gpio_alloc(SPI_MISO_GPIO, label, true);
254 + value = spi_gpio_alloc(SPI_SCK_GPIO, label, false);
261 + gpio_free(SPI_MISO_GPIO);
263 + gpio_free(SPI_MOSI_GPIO);
268 +static int __init spi_gpio_probe(struct platform_device *pdev)
271 + struct spi_master *master;
272 + struct spi_gpio *spi_gpio;
273 + struct spi_gpio_platform_data *pdata;
275 + pdata = pdev->dev.platform_data;
276 +#ifdef GENERIC_BITBANG
277 + if (!pdata || !pdata->num_chipselect)
281 + status = spi_gpio_request(pdata, dev_name(&pdev->dev));
285 + master = spi_alloc_master(&pdev->dev, sizeof *spi_gpio);
290 + spi_gpio = spi_master_get_devdata(master);
291 + platform_set_drvdata(pdev, spi_gpio);
293 + spi_gpio->pdev = pdev;
295 + spi_gpio->pdata = *pdata;
297 + master->bus_num = pdev->id;
298 + master->num_chipselect = SPI_N_CHIPSEL;
299 + master->setup = spi_gpio_setup;
300 + master->cleanup = spi_gpio_cleanup;
302 + spi_gpio->bitbang.master = spi_master_get(master);
303 + spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
304 + spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
305 + spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
306 + spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
307 + spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
308 + spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
309 + spi_gpio->bitbang.flags = SPI_CS_HIGH;
311 + status = spi_bitbang_start(&spi_gpio->bitbang);
313 + spi_master_put(spi_gpio->bitbang.master);
315 + gpio_free(SPI_MISO_GPIO);
316 + gpio_free(SPI_MOSI_GPIO);
317 + gpio_free(SPI_SCK_GPIO);
318 + spi_master_put(master);
324 +static int __exit spi_gpio_remove(struct platform_device *pdev)
326 + struct spi_gpio *spi_gpio;
327 + struct spi_gpio_platform_data *pdata;
330 + spi_gpio = platform_get_drvdata(pdev);
331 + pdata = pdev->dev.platform_data;
333 + /* stop() unregisters child devices too */
334 + status = spi_bitbang_stop(&spi_gpio->bitbang);
335 + spi_master_put(spi_gpio->bitbang.master);
337 + platform_set_drvdata(pdev, NULL);
339 + gpio_free(SPI_MISO_GPIO);
340 + gpio_free(SPI_MOSI_GPIO);
341 + gpio_free(SPI_SCK_GPIO);
346 +MODULE_ALIAS("platform:" DRIVER_NAME);
348 +static struct platform_driver spi_gpio_driver = {
349 + .driver.name = DRIVER_NAME,
350 + .driver.owner = THIS_MODULE,
351 + .remove = __exit_p(spi_gpio_remove),
354 +static int __init spi_gpio_init(void)
356 + return platform_driver_probe(&spi_gpio_driver, spi_gpio_probe);
358 +module_init(spi_gpio_init);
360 +static void __exit spi_gpio_exit(void)
362 + platform_driver_unregister(&spi_gpio_driver);
364 +module_exit(spi_gpio_exit);
367 +MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
368 +MODULE_AUTHOR("David Brownell");
369 +MODULE_LICENSE("GPL");
371 +++ b/include/linux/spi/spi_gpio.h
373 +#ifndef __LINUX_SPI_GPIO_H
374 +#define __LINUX_SPI_GPIO_H
377 + * For each bitbanged SPI bus, set up a platform_device node with:
378 + * - name "spi_gpio"
379 + * - id the same as the SPI bus number it implements
380 + * - dev.platform data pointing to a struct spi_gpio_platform_data
382 + * Or, see the driver code for information about speedups that are
383 + * possible on platforms that support inlined access for GPIOs (no
384 + * spi_gpio_platform_data is used).
386 + * Use spi_board_info with these busses in the usual way, being sure
387 + * that the controller_data being the GPIO used for each device's
390 + * static struct spi_board_info ... [] = {
392 + * // this slave uses GPIO 42 for its chipselect
393 + * .controller_data = (void *) 42,
395 + * // this one uses GPIO 86 for its chipselect
396 + * .controller_data = (void *) 86,
400 + * If the bitbanged bus is later switched to a "native" controller,
401 + * that platform_device and controller_data should be removed.
405 + * struct spi_gpio_platform_data - parameter for bitbanged SPI master
406 + * @sck: number of the GPIO used for clock output
407 + * @mosi: number of the GPIO used for Master Output, Slave In (MOSI) data
408 + * @miso: number of the GPIO used for Master Input, Slave Output (MISO) data
409 + * @num_chipselect: how many slaves to allow
411 + * All GPIO signals used with the SPI bus managed through this driver
412 + * (chipselects, MOSI, MISO, SCK) must be configured as GPIOs, instead
413 + * of some alternate function.
415 + * It can be convenient to use this driver with pins that have alternate
416 + * functions associated with a "native" SPI controller if a driver for that
417 + * controller is not available, or is missing important functionality.
419 + * On platforms which can do so, configure MISO with a weak pullup unless
420 + * there's an external pullup on that signal. That saves power by avoiding
421 + * floating signals. (A weak pulldown would save power too, but many
422 + * drivers expect to see all-ones data as the no slave "response".)
424 +struct spi_gpio_platform_data {
429 + u16 num_chipselect;
432 +#endif /* __LINUX_SPI_GPIO_H */
433 --- a/drivers/spi/Kconfig
434 +++ b/drivers/spi/Kconfig
435 @@ -100,6 +100,22 @@ config SPI_BUTTERFLY
436 inexpensive battery powered microcontroller evaluation board.
437 This same cable can be used to flash new firmware.
440 + tristate "GPIO-based bitbanging SPI Master"
441 + depends on GENERIC_GPIO
444 + This simple GPIO bitbanging SPI master uses the arch-neutral GPIO
445 + interface to manage MOSI, MISO, SCK, and chipselect signals. SPI
446 + slaves connected to a bus using this driver are configured as usual,
447 + except that the spi_board_info.controller_data holds the GPIO number
448 + for the chipselect used by this controller driver.
450 + Note that this driver often won't achieve even 1 Mbit/sec speeds,
451 + making it unusually slow for SPI. If your platform can inline
452 + GPIO operations, you should be able to leverage that for better
453 + speed with a custom version of this driver; see the source code.
456 tristate "Freescale iMX SPI controller"
457 depends on ARCH_IMX && EXPERIMENTAL
458 --- a/drivers/spi/Makefile
459 +++ b/drivers/spi/Makefile
460 @@ -16,6 +16,7 @@ obj-$(CONFIG_SPI_BFIN) += spi_bfin5xx.
461 obj-$(CONFIG_SPI_BITBANG) += spi_bitbang.o
462 obj-$(CONFIG_SPI_AU1550) += au1550_spi.o
463 obj-$(CONFIG_SPI_BUTTERFLY) += spi_butterfly.o
464 +obj-$(CONFIG_SPI_GPIO) += spi_gpio.o
465 obj-$(CONFIG_SPI_IMX) += spi_imx.o
466 obj-$(CONFIG_SPI_LM70_LLP) += spi_lm70llp.o
467 obj-$(CONFIG_SPI_PXA2XX) += pxa2xx_spi.o