linux/ixp4xx: sync 2.6.36 patches to 2.6.36-rc6
[openwrt.git] / target / linux / generic / patches-2.6.25 / 921-gpio_spi_driver.patch
1 --- /dev/null
2 +++ b/include/linux/spi/spi_gpio.h
3 @@ -0,0 +1,72 @@
4 +/*
5 + * spi_gpio interface to platform code
6 + *
7 + * Copyright (c) 2008 Piotr Skamruk
8 + *
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.
12 + */
13 +#ifndef _LINUX_SPI_SPI_GPIO
14 +#define _LINUX_SPI_SPI_GPIO
15 +
16 +#include <linux/types.h>
17 +#include <linux/spi/spi.h>
18 +
19 +
20 +/**
21 + * struct spi_gpio_platform_data - Data definitions for a SPI-GPIO device.
22 + *
23 + * This structure holds information about a GPIO-based SPI device.
24 + *
25 + * @pin_clk: The GPIO pin number of the CLOCK pin.
26 + *
27 + * @pin_miso: The GPIO pin number of the MISO pin.
28 + *
29 + * @pin_mosi: The GPIO pin number of the MOSI pin.
30 + *
31 + * @pin_cs: The GPIO pin number of the CHIPSELECT pin.
32 + *
33 + * @cs_activelow: If true, the chip is selected when the CS line is low.
34 + *
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.
39 + *
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().
44 + */
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;
50 + bool cs_activelow;
51 + bool no_spi_delay;
52 + int (*boardinfo_setup)(struct spi_board_info *bi,
53 + struct spi_master *master,
54 + void *data);
55 + void *boardinfo_setup_data;
56 +};
57 +
58 +/**
59 + * SPI_GPIO_PLATDEV_NAME - The platform device name string.
60 + *
61 + * The name string that has to be used for platform_device_alloc
62 + * when allocating a spi-gpio device.
63 + */
64 +#define SPI_GPIO_PLATDEV_NAME "spi-gpio"
65 +
66 +/**
67 + * spi_gpio_next_id - Get another platform device ID number.
68 + *
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
71 + * anything else.
72 + */
73 +int spi_gpio_next_id(void);
74 +
75 +#endif /* _LINUX_SPI_SPI_GPIO */
76 --- /dev/null
77 +++ b/drivers/spi/spi_gpio.c
78 @@ -0,0 +1,249 @@
79 +/*
80 + * Bitbanging SPI bus driver using GPIO API
81 + *
82 + * Copyright (c) 2008 Piotr Skamruk
83 + *
84 + * based on spi_s3c2410_gpio.c
85 + * Copyright (c) 2006 Ben Dooks
86 + * Copyright (c) 2006 Simtec Electronics
87 + * and on i2c-gpio.c
88 + * Copyright (C) 2007 Atmel Corporation
89 + *
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.
93 + */
94 +
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>
107 +
108 +
109 +struct spi_gpio {
110 + struct spi_bitbang bitbang;
111 + struct spi_gpio_platform_data *info;
112 + struct platform_device *pdev;
113 + struct spi_board_info bi;
114 +};
115 +
116 +
117 +static inline struct spi_gpio *spidev_to_sg(struct spi_device *dev)
118 +{
119 + return dev->controller_data;
120 +}
121 +
122 +static inline void setsck(struct spi_device *dev, int val)
123 +{
124 + struct spi_gpio *sp = spidev_to_sg(dev);
125 + gpio_set_value(sp->info->pin_clk, val ? 1 : 0);
126 +}
127 +
128 +static inline void setmosi(struct spi_device *dev, int val)
129 +{
130 + struct spi_gpio *sp = spidev_to_sg(dev);
131 + gpio_set_value(sp->info->pin_mosi, val ? 1 : 0);
132 +}
133 +
134 +static inline u32 getmiso(struct spi_device *dev)
135 +{
136 + struct spi_gpio *sp = spidev_to_sg(dev);
137 + return gpio_get_value(sp->info->pin_miso) ? 1 : 0;
138 +}
139 +
140 +static inline void do_spidelay(struct spi_device *dev, unsigned nsecs)
141 +{
142 + struct spi_gpio *sp = spidev_to_sg(dev);
143 +
144 + if (!sp->info->no_spi_delay)
145 + ndelay(nsecs);
146 +}
147 +
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); \
152 + } while (0)
153 +
154 +#define EXPAND_BITBANG_TXRX
155 +#include <linux/spi/spi_bitbang.h>
156 +
157 +static u32 spi_gpio_txrx_mode0(struct spi_device *spi,
158 + unsigned nsecs, u32 word, u8 bits)
159 +{
160 + return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
161 +}
162 +
163 +static u32 spi_gpio_txrx_mode1(struct spi_device *spi,
164 + unsigned nsecs, u32 word, u8 bits)
165 +{
166 + return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
167 +}
168 +
169 +static u32 spi_gpio_txrx_mode2(struct spi_device *spi,
170 + unsigned nsecs, u32 word, u8 bits)
171 +{
172 + return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits);
173 +}
174 +
175 +static u32 spi_gpio_txrx_mode3(struct spi_device *spi,
176 + unsigned nsecs, u32 word, u8 bits)
177 +{
178 + return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits);
179 +}
180 +
181 +static void spi_gpio_chipselect(struct spi_device *dev, int on)
182 +{
183 + struct spi_gpio *sp = spidev_to_sg(dev);
184 +
185 + if (sp->info->cs_activelow)
186 + on = !on;
187 + gpio_set_value(sp->info->pin_cs, on ? 1 : 0);
188 +}
189 +
190 +static int spi_gpio_probe(struct platform_device *pdev)
191 +{
192 + struct spi_master *master;
193 + struct spi_gpio_platform_data *pdata;
194 + struct spi_gpio *sp;
195 + struct spi_device *spidev;
196 + int err;
197 +
198 + pdata = pdev->dev.platform_data;
199 + if (!pdata)
200 + return -ENXIO;
201 +
202 + err = -ENOMEM;
203 + master = spi_alloc_master(&pdev->dev, sizeof(struct spi_gpio));
204 + if (!master)
205 + goto err_alloc_master;
206 +
207 + sp = spi_master_get_devdata(master);
208 + platform_set_drvdata(pdev, sp);
209 + sp->info = pdata;
210 +
211 + err = gpio_request(pdata->pin_clk, "spi_clock");
212 + if (err)
213 + goto err_request_clk;
214 + err = gpio_request(pdata->pin_mosi, "spi_mosi");
215 + if (err)
216 + goto err_request_mosi;
217 + err = gpio_request(pdata->pin_miso, "spi_miso");
218 + if (err)
219 + goto err_request_miso;
220 + err = gpio_request(pdata->pin_cs, "spi_cs");
221 + if (err)
222 + goto err_request_cs;
223 +
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;
232 +
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);
238 +
239 + err = spi_bitbang_start(&sp->bitbang);
240 + if (err)
241 + goto err_no_bitbang;
242 + err = pdata->boardinfo_setup(&sp->bi, master,
243 + pdata->boardinfo_setup_data);
244 + if (err)
245 + goto err_bi_setup;
246 + sp->bi.controller_data = sp;
247 + spidev = spi_new_device(master, &sp->bi);
248 + if (!spidev)
249 + goto err_new_dev;
250 +
251 + return 0;
252 +
253 +err_new_dev:
254 +err_bi_setup:
255 + spi_bitbang_stop(&sp->bitbang);
256 +err_no_bitbang:
257 + spi_master_put(sp->bitbang.master);
258 + gpio_free(pdata->pin_cs);
259 +err_request_cs:
260 + gpio_free(pdata->pin_miso);
261 +err_request_miso:
262 + gpio_free(pdata->pin_mosi);
263 +err_request_mosi:
264 + gpio_free(pdata->pin_clk);
265 +err_request_clk:
266 + kfree(master);
267 +
268 +err_alloc_master:
269 + return err;
270 +}
271 +
272 +static int __devexit spi_gpio_remove(struct platform_device *pdev)
273 +{
274 + struct spi_gpio *sp;
275 + struct spi_gpio_platform_data *pdata;
276 +
277 + pdata = pdev->dev.platform_data;
278 + sp = platform_get_drvdata(pdev);
279 +
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);
286 +
287 + return 0;
288 +}
289 +
290 +static struct platform_driver spi_gpio_driver = {
291 + .driver = {
292 + .name = SPI_GPIO_PLATDEV_NAME,
293 + .owner = THIS_MODULE,
294 + },
295 + .probe = spi_gpio_probe,
296 + .remove = __devexit_p(spi_gpio_remove),
297 +};
298 +
299 +int spi_gpio_next_id(void)
300 +{
301 + static atomic_t counter = ATOMIC_INIT(-1);
302 +
303 + return atomic_inc_return(&counter);
304 +}
305 +EXPORT_SYMBOL(spi_gpio_next_id);
306 +
307 +static int __init spi_gpio_init(void)
308 +{
309 + int err;
310 +
311 + err = platform_driver_register(&spi_gpio_driver);
312 + if (err)
313 + printk(KERN_ERR "spi-gpio: register failed: %d\n", err);
314 +
315 + return err;
316 +}
317 +module_init(spi_gpio_init);
318 +
319 +static void __exit spi_gpio_exit(void)
320 +{
321 + platform_driver_unregister(&spi_gpio_driver);
322 +}
323 +module_exit(spi_gpio_exit);
324 +
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.
333
334 +config SPI_GPIO
335 + tristate "GPIO API based bitbanging SPI controller"
336 + depends on SPI_MASTER && GENERIC_GPIO
337 + select SPI_BITBANG
338 + help
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
342 + GPIO pins.
343 + The module will be called spi_gpio.
344 +
345 + If unsure, say N.
346 +
347 config SPI_IMX
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
This page took 0.053956 seconds and 5 git commands to generate.