[ar71xx] fix GPIO function select bit definitions (based on a patch by Jonas <jmajau...
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.27 / 921-gpio_spi_driver.patch
1 --- /dev/null
2 +++ b/include/linux/spi/spi_gpio.h
3 @@ -0,0 +1,73 @@
4 +/*
5 + * spi_gpio interface to platform code
6 + *
7 + * Copyright (c) 2008 Piotr Skamruk
8 + * Copyright (c) 2008 Michael Buesch
9 + *
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.
13 + */
14 +#ifndef _LINUX_SPI_SPI_GPIO
15 +#define _LINUX_SPI_SPI_GPIO
16 +
17 +#include <linux/types.h>
18 +#include <linux/spi/spi.h>
19 +
20 +
21 +/**
22 + * struct spi_gpio_platform_data - Data definitions for a SPI-GPIO device.
23 + *
24 + * This structure holds information about a GPIO-based SPI device.
25 + *
26 + * @pin_clk: The GPIO pin number of the CLOCK pin.
27 + *
28 + * @pin_miso: The GPIO pin number of the MISO pin.
29 + *
30 + * @pin_mosi: The GPIO pin number of the MOSI pin.
31 + *
32 + * @pin_cs: The GPIO pin number of the CHIPSELECT pin.
33 + *
34 + * @cs_activelow: If true, the chip is selected when the CS line is low.
35 + *
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.
40 + *
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().
45 + */
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;
51 + bool cs_activelow;
52 + bool no_spi_delay;
53 + int (*boardinfo_setup)(struct spi_board_info *bi,
54 + struct spi_master *master,
55 + void *data);
56 + void *boardinfo_setup_data;
57 +};
58 +
59 +/**
60 + * SPI_GPIO_PLATDEV_NAME - The platform device name string.
61 + *
62 + * The name string that has to be used for platform_device_alloc
63 + * when allocating a spi-gpio device.
64 + */
65 +#define SPI_GPIO_PLATDEV_NAME "spi-gpio"
66 +
67 +/**
68 + * spi_gpio_next_id - Get another platform device ID number.
69 + *
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
72 + * anything else.
73 + */
74 +int spi_gpio_next_id(void);
75 +
76 +#endif /* _LINUX_SPI_SPI_GPIO */
77 --- /dev/null
78 +++ b/drivers/spi/spi_gpio.c
79 @@ -0,0 +1,251 @@
80 +/*
81 + * Bitbanging SPI bus driver using GPIO API
82 + *
83 + * Copyright (c) 2008 Piotr Skamruk
84 + * Copyright (c) 2008 Michael Buesch
85 + *
86 + * based on spi_s3c2410_gpio.c
87 + * Copyright (c) 2006 Ben Dooks
88 + * Copyright (c) 2006 Simtec Electronics
89 + * and on i2c-gpio.c
90 + * Copyright (C) 2007 Atmel Corporation
91 + *
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.
95 + */
96 +
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>
109 +
110 +
111 +struct spi_gpio {
112 + struct spi_bitbang bitbang;
113 + struct spi_gpio_platform_data *info;
114 + struct platform_device *pdev;
115 + struct spi_board_info bi;
116 +};
117 +
118 +
119 +static inline struct spi_gpio *spidev_to_sg(struct spi_device *dev)
120 +{
121 + return dev->controller_data;
122 +}
123 +
124 +static inline void setsck(struct spi_device *dev, int val)
125 +{
126 + struct spi_gpio *sp = spidev_to_sg(dev);
127 + gpio_set_value(sp->info->pin_clk, val ? 1 : 0);
128 +}
129 +
130 +static inline void setmosi(struct spi_device *dev, int val)
131 +{
132 + struct spi_gpio *sp = spidev_to_sg(dev);
133 + gpio_set_value(sp->info->pin_mosi, val ? 1 : 0);
134 +}
135 +
136 +static inline u32 getmiso(struct spi_device *dev)
137 +{
138 + struct spi_gpio *sp = spidev_to_sg(dev);
139 + return gpio_get_value(sp->info->pin_miso) ? 1 : 0;
140 +}
141 +
142 +static inline void do_spidelay(struct spi_device *dev, unsigned nsecs)
143 +{
144 + struct spi_gpio *sp = spidev_to_sg(dev);
145 +
146 + if (!sp->info->no_spi_delay)
147 + ndelay(nsecs);
148 +}
149 +
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); \
154 + } while (0)
155 +
156 +#define EXPAND_BITBANG_TXRX
157 +#include <linux/spi/spi_bitbang.h>
158 +
159 +static u32 spi_gpio_txrx_mode0(struct spi_device *spi,
160 + unsigned nsecs, u32 word, u8 bits)
161 +{
162 + return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
163 +}
164 +
165 +static u32 spi_gpio_txrx_mode1(struct spi_device *spi,
166 + unsigned nsecs, u32 word, u8 bits)
167 +{
168 + return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
169 +}
170 +
171 +static u32 spi_gpio_txrx_mode2(struct spi_device *spi,
172 + unsigned nsecs, u32 word, u8 bits)
173 +{
174 + return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits);
175 +}
176 +
177 +static u32 spi_gpio_txrx_mode3(struct spi_device *spi,
178 + unsigned nsecs, u32 word, u8 bits)
179 +{
180 + return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits);
181 +}
182 +
183 +static void spi_gpio_chipselect(struct spi_device *dev, int on)
184 +{
185 + struct spi_gpio *sp = spidev_to_sg(dev);
186 +
187 + if (sp->info->cs_activelow)
188 + on = !on;
189 + gpio_set_value(sp->info->pin_cs, on ? 1 : 0);
190 +}
191 +
192 +static int spi_gpio_probe(struct platform_device *pdev)
193 +{
194 + struct spi_master *master;
195 + struct spi_gpio_platform_data *pdata;
196 + struct spi_gpio *sp;
197 + struct spi_device *spidev;
198 + int err;
199 +
200 + pdata = pdev->dev.platform_data;
201 + if (!pdata)
202 + return -ENXIO;
203 +
204 + err = -ENOMEM;
205 + master = spi_alloc_master(&pdev->dev, sizeof(struct spi_gpio));
206 + if (!master)
207 + goto err_alloc_master;
208 +
209 + sp = spi_master_get_devdata(master);
210 + platform_set_drvdata(pdev, sp);
211 + sp->info = pdata;
212 +
213 + err = gpio_request(pdata->pin_clk, "spi_clock");
214 + if (err)
215 + goto err_request_clk;
216 + err = gpio_request(pdata->pin_mosi, "spi_mosi");
217 + if (err)
218 + goto err_request_mosi;
219 + err = gpio_request(pdata->pin_miso, "spi_miso");
220 + if (err)
221 + goto err_request_miso;
222 + err = gpio_request(pdata->pin_cs, "spi_cs");
223 + if (err)
224 + goto err_request_cs;
225 +
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;
234 +
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);
240 +
241 + err = spi_bitbang_start(&sp->bitbang);
242 + if (err)
243 + goto err_no_bitbang;
244 + err = pdata->boardinfo_setup(&sp->bi, master,
245 + pdata->boardinfo_setup_data);
246 + if (err)
247 + goto err_bi_setup;
248 + sp->bi.controller_data = sp;
249 + spidev = spi_new_device(master, &sp->bi);
250 + if (!spidev)
251 + goto err_new_dev;
252 +
253 + return 0;
254 +
255 +err_new_dev:
256 +err_bi_setup:
257 + spi_bitbang_stop(&sp->bitbang);
258 +err_no_bitbang:
259 + spi_master_put(sp->bitbang.master);
260 + gpio_free(pdata->pin_cs);
261 +err_request_cs:
262 + gpio_free(pdata->pin_miso);
263 +err_request_miso:
264 + gpio_free(pdata->pin_mosi);
265 +err_request_mosi:
266 + gpio_free(pdata->pin_clk);
267 +err_request_clk:
268 + kfree(master);
269 +
270 +err_alloc_master:
271 + return err;
272 +}
273 +
274 +static int __devexit spi_gpio_remove(struct platform_device *pdev)
275 +{
276 + struct spi_gpio *sp;
277 + struct spi_gpio_platform_data *pdata;
278 +
279 + pdata = pdev->dev.platform_data;
280 + sp = platform_get_drvdata(pdev);
281 +
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);
288 +
289 + return 0;
290 +}
291 +
292 +static struct platform_driver spi_gpio_driver = {
293 + .driver = {
294 + .name = SPI_GPIO_PLATDEV_NAME,
295 + .owner = THIS_MODULE,
296 + },
297 + .probe = spi_gpio_probe,
298 + .remove = __devexit_p(spi_gpio_remove),
299 +};
300 +
301 +int spi_gpio_next_id(void)
302 +{
303 + static atomic_t counter = ATOMIC_INIT(-1);
304 +
305 + return atomic_inc_return(&counter);
306 +}
307 +EXPORT_SYMBOL(spi_gpio_next_id);
308 +
309 +static int __init spi_gpio_init(void)
310 +{
311 + int err;
312 +
313 + err = platform_driver_register(&spi_gpio_driver);
314 + if (err)
315 + printk(KERN_ERR "spi-gpio: register failed: %d\n", err);
316 +
317 + return err;
318 +}
319 +module_init(spi_gpio_init);
320 +
321 +static void __exit spi_gpio_exit(void)
322 +{
323 + platform_driver_unregister(&spi_gpio_driver);
324 +}
325 +module_exit(spi_gpio_exit);
326 +
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
333 @@ -100,6 +100,19 @@ config SPI_BUTTERFLY
334 inexpensive battery powered microcontroller evaluation board.
335 This same cable can be used to flash new firmware.
336
337 +config SPI_GPIO
338 + tristate "GPIO API based bitbanging SPI controller"
339 + depends on SPI_MASTER && GENERIC_GPIO
340 + select SPI_BITBANG
341 + help
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
345 + GPIO pins.
346 + The module will be called spi_gpio.
347 +
348 + If unsure, say N.
349 +
350 config SPI_IMX
351 tristate "Freescale iMX SPI controller"
352 depends on ARCH_IMX && EXPERIMENTAL
353 --- a/drivers/spi/Makefile
354 +++ b/drivers/spi/Makefile
355 @@ -16,6 +16,7 @@ obj-$(CONFIG_SPI_BFIN) += spi_bfin5xx.
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
363 --- a/MAINTAINERS
364 +++ b/MAINTAINERS
365 @@ -3854,6 +3854,11 @@ L: cbe-oss-dev@ozlabs.org
366 W: http://www.ibm.com/developerworks/power/cell/
367 S: Supported
368
369 +SPI GPIO MASTER DRIVER
370 +P: Michael Buesch
371 +M: mb@bu3sch.de
372 +S: Maintained
373 +
374 STABLE BRANCH:
375 P: Greg Kroah-Hartman
376 M: greg@kroah.com
This page took 0.057125 seconds and 5 git commands to generate.