2 +++ b/include/linux/spi/spi_gpio.h
5 + * spi_gpio interface to platform code
7 + * Copyright (c) 2008 Piotr Skamruk
9 + * This program is free software; you can redistribute it and/or modify
10 + * it under the terms of the GNU General Public License version 2 as
11 + * published by the Free Software Foundation.
13 +#ifndef _LINUX_SPI_SPI_GPIO
14 +#define _LINUX_SPI_SPI_GPIO
16 +#include <linux/types.h>
17 +#include <linux/spi/spi.h>
21 + * struct spi_gpio_platform_data - Data definitions for a SPI-GPIO device.
23 + * This structure holds information about a GPIO-based SPI device.
25 + * @pin_clk: The GPIO pin number of the CLOCK pin.
27 + * @pin_miso: The GPIO pin number of the MISO pin.
29 + * @pin_mosi: The GPIO pin number of the MOSI pin.
31 + * @pin_cs: The GPIO pin number of the CHIPSELECT pin.
33 + * @cs_activelow: If true, the chip is selected when the CS line is low.
35 + * @no_spi_delay: If true, no delay is done in the lowlevel bitbanging.
36 + * Note that doing no delay is not standards compliant,
37 + * but it might be needed to speed up transfers on some
38 + * slow embedded machines.
40 + * @boardinfo_setup: This callback is called after the
41 + * SPI master device was registered, but before the
42 + * device is registered.
43 + * @boardinfo_setup_data: Data argument passed to boardinfo_setup().
45 +struct spi_gpio_platform_data {
46 + unsigned int pin_clk;
47 + unsigned int pin_miso;
48 + unsigned int pin_mosi;
49 + unsigned int pin_cs;
52 + int (*boardinfo_setup)(struct spi_board_info *bi,
53 + struct spi_master *master,
55 + void *boardinfo_setup_data;
59 + * SPI_GPIO_PLATDEV_NAME - The platform device name string.
61 + * The name string that has to be used for platform_device_alloc
62 + * when allocating a spi-gpio device.
64 +#define SPI_GPIO_PLATDEV_NAME "spi-gpio"
67 + * spi_gpio_next_id - Get another platform device ID number.
69 + * This returns the next platform device ID number that has to be used
70 + * for platform_device_alloc. The ID is opaque and should not be used for
73 +int spi_gpio_next_id(void);
75 +#endif /* _LINUX_SPI_SPI_GPIO */
77 +++ b/drivers/spi/spi_gpio.c
80 + * Bitbanging SPI bus driver using GPIO API
82 + * Copyright (c) 2008 Piotr Skamruk
84 + * based on spi_s3c2410_gpio.c
85 + * Copyright (c) 2006 Ben Dooks
86 + * Copyright (c) 2006 Simtec Electronics
88 + * Copyright (C) 2007 Atmel Corporation
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.
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>
110 + struct spi_bitbang bitbang;
111 + struct spi_gpio_platform_data *info;
112 + struct platform_device *pdev;
113 + struct spi_board_info bi;
117 +static inline struct spi_gpio *spidev_to_sg(struct spi_device *dev)
119 + return dev->controller_data;
122 +static inline void setsck(struct spi_device *dev, int val)
124 + struct spi_gpio *sp = spidev_to_sg(dev);
125 + gpio_set_value(sp->info->pin_clk, val ? 1 : 0);
128 +static inline void setmosi(struct spi_device *dev, int val)
130 + struct spi_gpio *sp = spidev_to_sg(dev);
131 + gpio_set_value(sp->info->pin_mosi, val ? 1 : 0);
134 +static inline u32 getmiso(struct spi_device *dev)
136 + struct spi_gpio *sp = spidev_to_sg(dev);
137 + return gpio_get_value(sp->info->pin_miso) ? 1 : 0;
140 +static inline void do_spidelay(struct spi_device *dev, unsigned nsecs)
142 + struct spi_gpio *sp = spidev_to_sg(dev);
144 + if (!sp->info->no_spi_delay)
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); \
154 +#define EXPAND_BITBANG_TXRX
155 +#include <linux/spi/spi_bitbang.h>
157 +static u32 spi_gpio_txrx_mode0(struct spi_device *spi,
158 + unsigned nsecs, u32 word, u8 bits)
160 + return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
163 +static u32 spi_gpio_txrx_mode1(struct spi_device *spi,
164 + unsigned nsecs, u32 word, u8 bits)
166 + return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
169 +static u32 spi_gpio_txrx_mode2(struct spi_device *spi,
170 + unsigned nsecs, u32 word, u8 bits)
172 + return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits);
175 +static u32 spi_gpio_txrx_mode3(struct spi_device *spi,
176 + unsigned nsecs, u32 word, u8 bits)
178 + return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits);
181 +static void spi_gpio_chipselect(struct spi_device *dev, int on)
183 + struct spi_gpio *sp = spidev_to_sg(dev);
185 + if (sp->info->cs_activelow)
187 + gpio_set_value(sp->info->pin_cs, on ? 1 : 0);
190 +static int spi_gpio_probe(struct platform_device *pdev)
192 + struct spi_master *master;
193 + struct spi_gpio_platform_data *pdata;
194 + struct spi_gpio *sp;
195 + struct spi_device *spidev;
198 + pdata = pdev->dev.platform_data;
203 + master = spi_alloc_master(&pdev->dev, sizeof(struct spi_gpio));
205 + goto err_alloc_master;
207 + sp = spi_master_get_devdata(master);
208 + platform_set_drvdata(pdev, sp);
211 + err = gpio_request(pdata->pin_clk, "spi_clock");
213 + goto err_request_clk;
214 + err = gpio_request(pdata->pin_mosi, "spi_mosi");
216 + goto err_request_mosi;
217 + err = gpio_request(pdata->pin_miso, "spi_miso");
219 + goto err_request_miso;
220 + err = gpio_request(pdata->pin_cs, "spi_cs");
222 + goto err_request_cs;
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;
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);
239 + err = spi_bitbang_start(&sp->bitbang);
241 + goto err_no_bitbang;
242 + err = pdata->boardinfo_setup(&sp->bi, master,
243 + pdata->boardinfo_setup_data);
246 + sp->bi.controller_data = sp;
247 + spidev = spi_new_device(master, &sp->bi);
255 + spi_bitbang_stop(&sp->bitbang);
257 + spi_master_put(sp->bitbang.master);
258 + gpio_free(pdata->pin_cs);
260 + gpio_free(pdata->pin_miso);
262 + gpio_free(pdata->pin_mosi);
264 + gpio_free(pdata->pin_clk);
272 +static int __devexit spi_gpio_remove(struct platform_device *pdev)
274 + struct spi_gpio *sp;
275 + struct spi_gpio_platform_data *pdata;
277 + pdata = pdev->dev.platform_data;
278 + sp = platform_get_drvdata(pdev);
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);
290 +static struct platform_driver spi_gpio_driver = {
292 + .name = SPI_GPIO_PLATDEV_NAME,
293 + .owner = THIS_MODULE,
295 + .probe = spi_gpio_probe,
296 + .remove = __devexit_p(spi_gpio_remove),
299 +int spi_gpio_next_id(void)
301 + static atomic_t counter = ATOMIC_INIT(-1);
303 + return atomic_inc_return(&counter);
305 +EXPORT_SYMBOL(spi_gpio_next_id);
307 +static int __init spi_gpio_init(void)
311 + err = platform_driver_register(&spi_gpio_driver);
313 + printk(KERN_ERR "spi-gpio: register failed: %d\n", err);
317 +module_init(spi_gpio_init);
319 +static void __exit spi_gpio_exit(void)
321 + platform_driver_unregister(&spi_gpio_driver);
323 +module_exit(spi_gpio_exit);
325 +MODULE_AUTHOR("Piot Skamruk <piotr.skamruk at gmail.com>");
326 +MODULE_DESCRIPTION("Platform independent GPIO bitbanging SPI driver");
327 +MODULE_LICENSE("GPL v2");
328 --- a/drivers/spi/Kconfig
329 +++ b/drivers/spi/Kconfig
330 @@ -100,6 +100,19 @@ config SPI_BUTTERFLY
331 inexpensive battery powered microcontroller evaluation board.
332 This same cable can be used to flash new firmware.
335 + tristate "GPIO API based bitbanging SPI controller"
336 + depends on SPI_MASTER && GENERIC_GPIO
339 + This is a platform driver that can be used for bitbanging
340 + an SPI bus over GPIO pins.
341 + Select this if you have any SPI device that is connected via
343 + The module will be called spi_gpio.
348 tristate "Freescale iMX SPI controller"
349 depends on SPI_MASTER && ARCH_IMX && EXPERIMENTAL
350 --- a/drivers/spi/Makefile
351 +++ b/drivers/spi/Makefile
352 @@ -16,6 +16,7 @@ obj-$(CONFIG_SPI_BFIN) += spi_bfin5xx.
353 obj-$(CONFIG_SPI_BITBANG) += spi_bitbang.o
354 obj-$(CONFIG_SPI_AU1550) += au1550_spi.o
355 obj-$(CONFIG_SPI_BUTTERFLY) += spi_butterfly.o
356 +obj-$(CONFIG_SPI_GPIO) += spi_gpio.o
357 obj-$(CONFIG_SPI_IMX) += spi_imx.o
358 obj-$(CONFIG_SPI_LM70_LLP) += spi_lm70llp.o
359 obj-$(CONFIG_SPI_PXA2XX) += pxa2xx_spi.o