2 +++ b/include/linux/spi/spi_gpio.h
5 + * spi_gpio interface to platform code
7 + * Copyright (c) 2008 Piotr Skamruk
8 + * Copyright (c) 2008 Michael Buesch
10 + * This program is free software; you can redistribute it and/or modify
11 + * it under the terms of the GNU General Public License version 2 as
12 + * published by the Free Software Foundation.
14 +#ifndef _LINUX_SPI_SPI_GPIO
15 +#define _LINUX_SPI_SPI_GPIO
17 +#include <linux/types.h>
18 +#include <linux/spi/spi.h>
22 + * struct spi_gpio_platform_data - Data definitions for a SPI-GPIO device.
24 + * This structure holds information about a GPIO-based SPI device.
26 + * @pin_clk: The GPIO pin number of the CLOCK pin.
28 + * @pin_miso: The GPIO pin number of the MISO pin.
30 + * @pin_mosi: The GPIO pin number of the MOSI pin.
32 + * @pin_cs: The GPIO pin number of the CHIPSELECT pin.
34 + * @cs_activelow: If true, the chip is selected when the CS line is low.
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.
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().
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;
53 + int (*boardinfo_setup)(struct spi_board_info *bi,
54 + struct spi_master *master,
56 + void *boardinfo_setup_data;
60 + * SPI_GPIO_PLATDEV_NAME - The platform device name string.
62 + * The name string that has to be used for platform_device_alloc
63 + * when allocating a spi-gpio device.
65 +#define SPI_GPIO_PLATDEV_NAME "spi-gpio"
68 + * spi_gpio_next_id - Get another platform device ID number.
70 + * This returns the next platform device ID number that has to be used
71 + * for platform_device_alloc. The ID is opaque and should not be used for
74 +int spi_gpio_next_id(void);
76 +#endif /* _LINUX_SPI_SPI_GPIO */
78 +++ b/drivers/spi/spi_gpio.c
81 + * Bitbanging SPI bus driver using GPIO API
83 + * Copyright (c) 2008 Piotr Skamruk
84 + * Copyright (c) 2008 Michael Buesch
86 + * based on spi_s3c2410_gpio.c
87 + * Copyright (c) 2006 Ben Dooks
88 + * Copyright (c) 2006 Simtec Electronics
90 + * Copyright (C) 2007 Atmel Corporation
92 + * This program is free software; you can redistribute it and/or modify
93 + * it under the terms of the GNU General Public License version 2 as
94 + * published by the Free Software Foundation.
97 +#include <linux/kernel.h>
98 +#include <linux/init.h>
99 +#include <linux/delay.h>
100 +#include <linux/spinlock.h>
101 +#include <linux/workqueue.h>
102 +#include <linux/module.h>
103 +#include <linux/platform_device.h>
104 +#include <linux/spi/spi.h>
105 +#include <linux/spi/spi_bitbang.h>
106 +#include <linux/spi/spi_gpio.h>
107 +#include <linux/gpio.h>
108 +#include <asm/atomic.h>
112 + struct spi_bitbang bitbang;
113 + struct spi_gpio_platform_data *info;
114 + struct platform_device *pdev;
115 + struct spi_board_info bi;
119 +static inline struct spi_gpio *spidev_to_sg(struct spi_device *dev)
121 + return dev->controller_data;
124 +static inline void setsck(struct spi_device *dev, int val)
126 + struct spi_gpio *sp = spidev_to_sg(dev);
127 + gpio_set_value(sp->info->pin_clk, val ? 1 : 0);
130 +static inline void setmosi(struct spi_device *dev, int val)
132 + struct spi_gpio *sp = spidev_to_sg(dev);
133 + gpio_set_value(sp->info->pin_mosi, val ? 1 : 0);
136 +static inline u32 getmiso(struct spi_device *dev)
138 + struct spi_gpio *sp = spidev_to_sg(dev);
139 + return gpio_get_value(sp->info->pin_miso) ? 1 : 0;
142 +static inline void do_spidelay(struct spi_device *dev, unsigned nsecs)
144 + struct spi_gpio *sp = spidev_to_sg(dev);
146 + if (!sp->info->no_spi_delay)
150 +#define spidelay(nsecs) do { \
151 + /* Steal the spi_device pointer from our caller. \
152 + * The bitbang-API should probably get fixed here... */ \
153 + do_spidelay(spi, nsecs); \
156 +#define EXPAND_BITBANG_TXRX
157 +#include <linux/spi/spi_bitbang.h>
159 +static u32 spi_gpio_txrx_mode0(struct spi_device *spi,
160 + unsigned nsecs, u32 word, u8 bits)
162 + return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
165 +static u32 spi_gpio_txrx_mode1(struct spi_device *spi,
166 + unsigned nsecs, u32 word, u8 bits)
168 + return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
171 +static u32 spi_gpio_txrx_mode2(struct spi_device *spi,
172 + unsigned nsecs, u32 word, u8 bits)
174 + return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits);
177 +static u32 spi_gpio_txrx_mode3(struct spi_device *spi,
178 + unsigned nsecs, u32 word, u8 bits)
180 + return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits);
183 +static void spi_gpio_chipselect(struct spi_device *dev, int on)
185 + struct spi_gpio *sp = spidev_to_sg(dev);
187 + if (sp->info->cs_activelow)
189 + gpio_set_value(sp->info->pin_cs, on ? 1 : 0);
192 +static int spi_gpio_probe(struct platform_device *pdev)
194 + struct spi_master *master;
195 + struct spi_gpio_platform_data *pdata;
196 + struct spi_gpio *sp;
197 + struct spi_device *spidev;
200 + pdata = pdev->dev.platform_data;
205 + master = spi_alloc_master(&pdev->dev, sizeof(struct spi_gpio));
207 + goto err_alloc_master;
209 + sp = spi_master_get_devdata(master);
210 + platform_set_drvdata(pdev, sp);
213 + err = gpio_request(pdata->pin_clk, "spi_clock");
215 + goto err_request_clk;
216 + err = gpio_request(pdata->pin_mosi, "spi_mosi");
218 + goto err_request_mosi;
219 + err = gpio_request(pdata->pin_miso, "spi_miso");
221 + goto err_request_miso;
222 + err = gpio_request(pdata->pin_cs, "spi_cs");
224 + goto err_request_cs;
226 + sp->bitbang.master = spi_master_get(master);
227 + sp->bitbang.master->bus_num = -1;
228 + sp->bitbang.master->num_chipselect = 1;
229 + sp->bitbang.chipselect = spi_gpio_chipselect;
230 + sp->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_mode0;
231 + sp->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_mode1;
232 + sp->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_mode2;
233 + sp->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_mode3;
235 + gpio_direction_output(pdata->pin_clk, 0);
236 + gpio_direction_output(pdata->pin_mosi, 0);
237 + gpio_direction_output(pdata->pin_cs,
238 + pdata->cs_activelow ? 1 : 0);
239 + gpio_direction_input(pdata->pin_miso);
241 + err = spi_bitbang_start(&sp->bitbang);
243 + goto err_no_bitbang;
244 + err = pdata->boardinfo_setup(&sp->bi, master,
245 + pdata->boardinfo_setup_data);
248 + sp->bi.controller_data = sp;
249 + spidev = spi_new_device(master, &sp->bi);
257 + spi_bitbang_stop(&sp->bitbang);
259 + spi_master_put(sp->bitbang.master);
260 + gpio_free(pdata->pin_cs);
262 + gpio_free(pdata->pin_miso);
264 + gpio_free(pdata->pin_mosi);
266 + gpio_free(pdata->pin_clk);
274 +static int __devexit spi_gpio_remove(struct platform_device *pdev)
276 + struct spi_gpio *sp;
277 + struct spi_gpio_platform_data *pdata;
279 + pdata = pdev->dev.platform_data;
280 + sp = platform_get_drvdata(pdev);
282 + gpio_free(pdata->pin_clk);
283 + gpio_free(pdata->pin_mosi);
284 + gpio_free(pdata->pin_miso);
285 + gpio_free(pdata->pin_cs);
286 + spi_bitbang_stop(&sp->bitbang);
287 + spi_master_put(sp->bitbang.master);
292 +static struct platform_driver spi_gpio_driver = {
294 + .name = SPI_GPIO_PLATDEV_NAME,
295 + .owner = THIS_MODULE,
297 + .probe = spi_gpio_probe,
298 + .remove = __devexit_p(spi_gpio_remove),
301 +int spi_gpio_next_id(void)
303 + static atomic_t counter = ATOMIC_INIT(-1);
305 + return atomic_inc_return(&counter);
307 +EXPORT_SYMBOL(spi_gpio_next_id);
309 +static int __init spi_gpio_init(void)
313 + err = platform_driver_register(&spi_gpio_driver);
315 + printk(KERN_ERR "spi-gpio: register failed: %d\n", err);
319 +module_init(spi_gpio_init);
321 +static void __exit spi_gpio_exit(void)
323 + platform_driver_unregister(&spi_gpio_driver);
325 +module_exit(spi_gpio_exit);
327 +MODULE_AUTHOR("Piot Skamruk <piotr.skamruk at gmail.com>");
328 +MODULE_AUTHOR("Michael Buesch");
329 +MODULE_DESCRIPTION("Platform independent GPIO bitbanging SPI driver");
330 +MODULE_LICENSE("GPL v2");
331 --- a/drivers/spi/Kconfig
332 +++ b/drivers/spi/Kconfig
334 inexpensive battery powered microcontroller evaluation board.
335 This same cable can be used to flash new firmware.
338 + tristate "GPIO API based bitbanging SPI controller"
339 + depends on SPI_MASTER && GENERIC_GPIO
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
346 + The module will be called spi_gpio.
351 tristate "Freescale iMX SPI controller"
352 depends on SPI_MASTER && ARCH_IMX && EXPERIMENTAL
353 --- a/drivers/spi/Makefile
354 +++ b/drivers/spi/Makefile
356 obj-$(CONFIG_SPI_BITBANG) += spi_bitbang.o
357 obj-$(CONFIG_SPI_AU1550) += au1550_spi.o
358 obj-$(CONFIG_SPI_BUTTERFLY) += spi_butterfly.o
359 +obj-$(CONFIG_SPI_GPIO) += spi_gpio.o
360 obj-$(CONFIG_SPI_IMX) += spi_imx.o
361 obj-$(CONFIG_SPI_LM70_LLP) += spi_lm70llp.o
362 obj-$(CONFIG_SPI_PXA2XX) += pxa2xx_spi.o
365 @@ -3795,6 +3795,11 @@
366 W: http://www.ibm.com/developerworks/power/cell/
369 +SPI GPIO MASTER DRIVER
375 P: Greg Kroah-Hartman