Root/
Source at commit 8110220d2cd9c0a591243052b1eec00900de3e14 created 14 years 2 months ago. By Lars-Peter Clausen, From df07ed6a52d9f6027ff1753c00b3128fa18dde31 Mon Sep 17 00:00:00 2001 Subject: [PATCH] /opt/Projects/openwrt/target/linux/xburst/patches-2.6.31/400-spi-gpio-3wire.patch | |
---|---|
1 | /* |
2 | * spi_gpio.c - SPI master driver using generic bitbanged GPIO |
3 | * |
4 | * Copyright (C) 2006,2008 David Brownell |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with this program; if not, write to the Free Software |
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | */ |
20 | #include <linux/kernel.h> |
21 | #include <linux/init.h> |
22 | #include <linux/platform_device.h> |
23 | #include <linux/gpio.h> |
24 | #include <linux/delay.h> |
25 | |
26 | #include <linux/spi/spi.h> |
27 | #include <linux/spi/spi_bitbang.h> |
28 | #include <linux/spi/spi_gpio.h> |
29 | |
30 | |
31 | /* |
32 | * This bitbanging SPI master driver should help make systems usable |
33 | * when a native hardware SPI engine is not available, perhaps because |
34 | * its driver isn't yet working or because the I/O pins it requires |
35 | * are used for other purposes. |
36 | * |
37 | * platform_device->driver_data ... points to spi_gpio |
38 | * |
39 | * spi->controller_state ... reserved for bitbang framework code |
40 | * spi->controller_data ... holds chipselect GPIO |
41 | * |
42 | * spi->master->dev.driver_data ... points to spi_gpio->bitbang |
43 | */ |
44 | |
45 | struct spi_gpio { |
46 | struct spi_bitbang bitbang; |
47 | struct spi_gpio_platform_data pdata; |
48 | struct platform_device *pdev; |
49 | }; |
50 | |
51 | /*----------------------------------------------------------------------*/ |
52 | |
53 | /* |
54 | * Because the overhead of going through four GPIO procedure calls |
55 | * per transferred bit can make performance a problem, this code |
56 | * is set up so that you can use it in either of two ways: |
57 | * |
58 | * - The slow generic way: set up platform_data to hold the GPIO |
59 | * numbers used for MISO/MOSI/SCK, and issue procedure calls for |
60 | * each of them. This driver can handle several such busses. |
61 | * |
62 | * - The quicker inlined way: only helps with platform GPIO code |
63 | * that inlines operations for constant GPIOs. This can give |
64 | * you tight (fast!) inner loops, but each such bus needs a |
65 | * new driver. You'll define a new C file, with Makefile and |
66 | * Kconfig support; the C code can be a total of six lines: |
67 | * |
68 | * #define DRIVER_NAME "myboard_spi2" |
69 | * #define SPI_MISO_GPIO 119 |
70 | * #define SPI_MOSI_GPIO 120 |
71 | * #define SPI_SCK_GPIO 121 |
72 | * #define SPI_N_CHIPSEL 4 |
73 | * #undef NEED_SPIDELAY |
74 | * #include "spi_gpio.c" |
75 | */ |
76 | |
77 | #ifndef DRIVER_NAME |
78 | #define DRIVER_NAME "spi_gpio" |
79 | |
80 | #define GENERIC_BITBANG /* vs tight inlines */ |
81 | #define NEED_SPIDELAY 1 |
82 | |
83 | /* all functions referencing these symbols must define pdata */ |
84 | #define SPI_MISO_GPIO ((pdata)->miso) |
85 | #define SPI_MOSI_GPIO ((pdata)->mosi) |
86 | #define SPI_SCK_GPIO ((pdata)->sck) |
87 | |
88 | #define SPI_N_CHIPSEL ((pdata)->num_chipselect) |
89 | |
90 | #endif |
91 | |
92 | /*----------------------------------------------------------------------*/ |
93 | |
94 | static inline const struct spi_gpio_platform_data * __pure |
95 | spi_to_pdata(const struct spi_device *spi) |
96 | { |
97 | const struct spi_bitbang *bang; |
98 | const struct spi_gpio *spi_gpio; |
99 | |
100 | bang = spi_master_get_devdata(spi->master); |
101 | spi_gpio = container_of(bang, struct spi_gpio, bitbang); |
102 | return &spi_gpio->pdata; |
103 | } |
104 | |
105 | /* this is #defined to avoid unused-variable warnings when inlining */ |
106 | #define pdata spi_to_pdata(spi) |
107 | |
108 | static inline void setsck(const struct spi_device *spi, int is_on) |
109 | { |
110 | gpio_set_value(SPI_SCK_GPIO, is_on); |
111 | } |
112 | |
113 | static inline void setmosi(const struct spi_device *spi, int is_on) |
114 | { |
115 | gpio_set_value(SPI_MOSI_GPIO, is_on); |
116 | } |
117 | |
118 | static inline int getmiso(const struct spi_device *spi) |
119 | { |
120 | return !!gpio_get_value(SPI_MISO_GPIO); |
121 | } |
122 | |
123 | #undef pdata |
124 | |
125 | /* |
126 | * NOTE: to clock "as fast as we can", set spi_device.max_speed_hz |
127 | * and spi_transfer.speed_hz to 0. |
128 | * Otherwise this is a function of the requested device clock. |
129 | * Software overhead means we usually have trouble |
130 | * reaching even one Mbit/sec (except when we can inline bitops). So on small |
131 | * embedded devices with fast SPI slaves you usually don't need a delay. |
132 | */ |
133 | static inline void spidelay(unsigned nsecs) |
134 | { |
135 | #ifdef NEED_SPIDELAY |
136 | if (unlikely(nsecs)) |
137 | ndelay(nsecs); |
138 | #endif /* NEED_SPIDELAY */ |
139 | } |
140 | |
141 | #define EXPAND_BITBANG_TXRX |
142 | #include <linux/spi/spi_bitbang.h> |
143 | |
144 | /* |
145 | * These functions can leverage inline expansion of GPIO calls to shrink |
146 | * costs for a txrx bit, often by factors of around ten (by instruction |
147 | * count). That is particularly visible for larger word sizes, but helps |
148 | * even with default 8-bit words. |
149 | * |
150 | * REVISIT overheads calling these functions for each word also have |
151 | * significant performance costs. Having txrx_bufs() calls that inline |
152 | * the txrx_word() logic would help performance, e.g. on larger blocks |
153 | * used with flash storage or MMC/SD. There should also be ways to make |
154 | * GCC be less stupid about reloading registers inside the I/O loops, |
155 | * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3? |
156 | */ |
157 | |
158 | static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi, |
159 | unsigned nsecs, u32 word, u8 bits) |
160 | { |
161 | return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits); |
162 | } |
163 | |
164 | static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi, |
165 | unsigned nsecs, u32 word, u8 bits) |
166 | { |
167 | return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits); |
168 | } |
169 | |
170 | static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi, |
171 | unsigned nsecs, u32 word, u8 bits) |
172 | { |
173 | return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits); |
174 | } |
175 | |
176 | static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi, |
177 | unsigned nsecs, u32 word, u8 bits) |
178 | { |
179 | return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits); |
180 | } |
181 | |
182 | /*----------------------------------------------------------------------*/ |
183 | |
184 | static void spi_gpio_chipselect(struct spi_device *spi, int is_active) |
185 | { |
186 | unsigned long cs = (unsigned long) spi->controller_data; |
187 | |
188 | /* set initial clock polarity */ |
189 | if (is_active) |
190 | setsck(spi, spi->mode & SPI_CPOL); |
191 | |
192 | if (cs != SPI_GPIO_NO_CHIPSELECT) { |
193 | /* SPI is normally active-low */ |
194 | gpio_set_value(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active); |
195 | } |
196 | } |
197 | |
198 | static int spi_gpio_setup(struct spi_device *spi) |
199 | { |
200 | unsigned long cs = (unsigned long) spi->controller_data; |
201 | int status = 0; |
202 | |
203 | if (spi->bits_per_word > 32) |
204 | return -EINVAL; |
205 | |
206 | if (!spi->controller_state) { |
207 | if (cs != SPI_GPIO_NO_CHIPSELECT) { |
208 | status = gpio_request(cs, dev_name(&spi->dev)); |
209 | if (status) |
210 | return status; |
211 | status = gpio_direction_output(cs, spi->mode & SPI_CS_HIGH); |
212 | } |
213 | } |
214 | if (!status) |
215 | status = spi_bitbang_setup(spi); |
216 | if (status) { |
217 | if (!spi->controller_state && cs != SPI_GPIO_NO_CHIPSELECT) |
218 | gpio_free(cs); |
219 | } |
220 | return status; |
221 | } |
222 | |
223 | static void spi_gpio_cleanup(struct spi_device *spi) |
224 | { |
225 | unsigned long cs = (unsigned long) spi->controller_data; |
226 | |
227 | if (cs != SPI_GPIO_NO_CHIPSELECT) |
228 | gpio_free(cs); |
229 | spi_bitbang_cleanup(spi); |
230 | } |
231 | |
232 | static int __devinit spi_gpio_alloc(unsigned pin, const char *label, bool is_in) |
233 | { |
234 | int value; |
235 | |
236 | value = gpio_request(pin, label); |
237 | if (value == 0) { |
238 | if (is_in) |
239 | value = gpio_direction_input(pin); |
240 | else |
241 | value = gpio_direction_output(pin, 0); |
242 | } |
243 | return value; |
244 | } |
245 | |
246 | static int __devinit |
247 | spi_gpio_request(struct spi_gpio_platform_data *pdata, const char *label) |
248 | { |
249 | int value; |
250 | |
251 | /* NOTE: SPI_*_GPIO symbols may reference "pdata" */ |
252 | |
253 | value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false); |
254 | if (value) |
255 | goto done; |
256 | |
257 | if (SPI_MISO_GPIO != SPI_MOSI_GPIO) { |
258 | value = spi_gpio_alloc(SPI_MISO_GPIO, label, true); |
259 | if (value) |
260 | goto free_mosi; |
261 | } |
262 | |
263 | value = spi_gpio_alloc(SPI_SCK_GPIO, label, false); |
264 | if (value) |
265 | goto free_miso; |
266 | |
267 | goto done; |
268 | |
269 | free_miso: |
270 | gpio_free(SPI_MISO_GPIO); |
271 | free_mosi: |
272 | gpio_free(SPI_MOSI_GPIO); |
273 | done: |
274 | return value; |
275 | } |
276 | |
277 | static int __devinit spi_gpio_probe(struct platform_device *pdev) |
278 | { |
279 | int status; |
280 | struct spi_master *master; |
281 | struct spi_gpio *spi_gpio; |
282 | struct spi_gpio_platform_data *pdata; |
283 | |
284 | pdata = pdev->dev.platform_data; |
285 | #ifdef GENERIC_BITBANG |
286 | if (!pdata || !pdata->num_chipselect) |
287 | return -ENODEV; |
288 | #endif |
289 | |
290 | status = spi_gpio_request(pdata, dev_name(&pdev->dev)); |
291 | if (status < 0) |
292 | return status; |
293 | |
294 | master = spi_alloc_master(&pdev->dev, sizeof *spi_gpio); |
295 | if (!master) { |
296 | status = -ENOMEM; |
297 | goto gpio_free; |
298 | } |
299 | spi_gpio = spi_master_get_devdata(master); |
300 | platform_set_drvdata(pdev, spi_gpio); |
301 | |
302 | spi_gpio->pdev = pdev; |
303 | if (pdata) |
304 | spi_gpio->pdata = *pdata; |
305 | |
306 | master->bus_num = pdev->id; |
307 | master->num_chipselect = SPI_N_CHIPSEL; |
308 | master->setup = spi_gpio_setup; |
309 | master->cleanup = spi_gpio_cleanup; |
310 | |
311 | spi_gpio->bitbang.master = spi_master_get(master); |
312 | spi_gpio->bitbang.chipselect = spi_gpio_chipselect; |
313 | spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0; |
314 | spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1; |
315 | spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2; |
316 | spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3; |
317 | spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer; |
318 | spi_gpio->bitbang.flags = SPI_CS_HIGH; |
319 | |
320 | status = spi_bitbang_start(&spi_gpio->bitbang); |
321 | if (status < 0) { |
322 | spi_master_put(spi_gpio->bitbang.master); |
323 | gpio_free: |
324 | if (SPI_MISO_GPIO != SPI_MOSI_GPIO) |
325 | gpio_free(SPI_MISO_GPIO); |
326 | gpio_free(SPI_MOSI_GPIO); |
327 | gpio_free(SPI_SCK_GPIO); |
328 | spi_master_put(master); |
329 | } |
330 | |
331 | return status; |
332 | } |
333 | |
334 | static int __devexit spi_gpio_remove(struct platform_device *pdev) |
335 | { |
336 | struct spi_gpio *spi_gpio; |
337 | struct spi_gpio_platform_data *pdata; |
338 | int status; |
339 | |
340 | spi_gpio = platform_get_drvdata(pdev); |
341 | pdata = pdev->dev.platform_data; |
342 | |
343 | /* stop() unregisters child devices too */ |
344 | status = spi_bitbang_stop(&spi_gpio->bitbang); |
345 | spi_master_put(spi_gpio->bitbang.master); |
346 | |
347 | platform_set_drvdata(pdev, NULL); |
348 | |
349 | if (SPI_MISO_GPIO != SPI_MOSI_GPIO) |
350 | gpio_free(SPI_MISO_GPIO); |
351 | gpio_free(SPI_MOSI_GPIO); |
352 | gpio_free(SPI_SCK_GPIO); |
353 | |
354 | return status; |
355 | } |
356 | |
357 | MODULE_ALIAS("platform:" DRIVER_NAME); |
358 | |
359 | static struct platform_driver spi_gpio_driver = { |
360 | .driver.name = DRIVER_NAME, |
361 | .driver.owner = THIS_MODULE, |
362 | .probe = spi_gpio_probe, |
363 | .remove = __devexit_p(spi_gpio_remove), |
364 | }; |
365 | |
366 | static int __init spi_gpio_init(void) |
367 | { |
368 | return platform_driver_register(&spi_gpio_driver); |
369 | } |
370 | module_init(spi_gpio_init); |
371 | |
372 | static void __exit spi_gpio_exit(void) |
373 | { |
374 | platform_driver_unregister(&spi_gpio_driver); |
375 | } |
376 | module_exit(spi_gpio_exit); |
377 | |
378 | |
379 | MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO "); |
380 | MODULE_AUTHOR("David Brownell"); |
381 | MODULE_LICENSE("GPL"); |
382 |
Branches:
ben-wpan
ben-wpan-stefan
javiroman/ks7010
jz-2.6.34
jz-2.6.34-rc5
jz-2.6.34-rc6
jz-2.6.34-rc7
jz-2.6.35
jz-2.6.36
jz-2.6.37
jz-2.6.38
jz-2.6.39
jz-3.0
jz-3.1
jz-3.11
jz-3.12
jz-3.13
jz-3.15
jz-3.16
jz-3.18-dt
jz-3.2
jz-3.3
jz-3.4
jz-3.5
jz-3.6
jz-3.6-rc2-pwm
jz-3.9
jz-3.9-clk
jz-3.9-rc8
jz47xx
jz47xx-2.6.38
master
Tags:
od-2011-09-04
od-2011-09-18
v2.6.34-rc5
v2.6.34-rc6
v2.6.34-rc7
v3.9