blob: ff7263ce12c7e6a791359f6d1ff8b643f41067bd [file] [log] [blame]
David Brownelld29389d2009-01-06 14:41:41 -08001/*
Grant Likelyca632f52011-06-06 01:16:30 -06002 * SPI master driver using generic bitbanged GPIO
David Brownelld29389d2009-01-06 14:41:41 -08003 *
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>
Paul Gortmakerd7614de2011-07-03 15:44:29 -040021#include <linux/module.h>
David Brownelld29389d2009-01-06 14:41:41 -080022#include <linux/init.h>
23#include <linux/platform_device.h>
24#include <linux/gpio.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
45struct spi_gpio {
46 struct spi_bitbang bitbang;
47 struct spi_gpio_platform_data pdata;
48 struct platform_device *pdev;
Daniel Mack161c2dd2012-09-05 11:04:35 +020049 int cs_gpios[0];
David Brownelld29389d2009-01-06 14:41:41 -080050};
51
52/*----------------------------------------------------------------------*/
53
54/*
55 * Because the overhead of going through four GPIO procedure calls
56 * per transferred bit can make performance a problem, this code
57 * is set up so that you can use it in either of two ways:
58 *
59 * - The slow generic way: set up platform_data to hold the GPIO
60 * numbers used for MISO/MOSI/SCK, and issue procedure calls for
61 * each of them. This driver can handle several such busses.
62 *
63 * - The quicker inlined way: only helps with platform GPIO code
64 * that inlines operations for constant GPIOs. This can give
65 * you tight (fast!) inner loops, but each such bus needs a
66 * new driver. You'll define a new C file, with Makefile and
67 * Kconfig support; the C code can be a total of six lines:
68 *
69 * #define DRIVER_NAME "myboard_spi2"
70 * #define SPI_MISO_GPIO 119
71 * #define SPI_MOSI_GPIO 120
72 * #define SPI_SCK_GPIO 121
73 * #define SPI_N_CHIPSEL 4
Grant Likelyca632f52011-06-06 01:16:30 -060074 * #include "spi-gpio.c"
David Brownelld29389d2009-01-06 14:41:41 -080075 */
76
77#ifndef DRIVER_NAME
78#define DRIVER_NAME "spi_gpio"
79
80#define GENERIC_BITBANG /* vs tight inlines */
81
82/* all functions referencing these symbols must define pdata */
83#define SPI_MISO_GPIO ((pdata)->miso)
84#define SPI_MOSI_GPIO ((pdata)->mosi)
85#define SPI_SCK_GPIO ((pdata)->sck)
86
87#define SPI_N_CHIPSEL ((pdata)->num_chipselect)
88
89#endif
90
91/*----------------------------------------------------------------------*/
92
Daniel Mack161c2dd2012-09-05 11:04:35 +020093static inline struct spi_gpio * __pure
94spi_to_spi_gpio(const struct spi_device *spi)
David Brownelld29389d2009-01-06 14:41:41 -080095{
96 const struct spi_bitbang *bang;
Daniel Mack161c2dd2012-09-05 11:04:35 +020097 struct spi_gpio *spi_gpio;
David Brownelld29389d2009-01-06 14:41:41 -080098
99 bang = spi_master_get_devdata(spi->master);
100 spi_gpio = container_of(bang, struct spi_gpio, bitbang);
Daniel Mack161c2dd2012-09-05 11:04:35 +0200101 return spi_gpio;
102}
103
104static inline struct spi_gpio_platform_data * __pure
105spi_to_pdata(const struct spi_device *spi)
106{
107 return &spi_to_spi_gpio(spi)->pdata;
David Brownelld29389d2009-01-06 14:41:41 -0800108}
109
110/* this is #defined to avoid unused-variable warnings when inlining */
111#define pdata spi_to_pdata(spi)
112
113static inline void setsck(const struct spi_device *spi, int is_on)
114{
115 gpio_set_value(SPI_SCK_GPIO, is_on);
116}
117
118static inline void setmosi(const struct spi_device *spi, int is_on)
119{
120 gpio_set_value(SPI_MOSI_GPIO, is_on);
121}
122
123static inline int getmiso(const struct spi_device *spi)
124{
Michael Bueschbe503442009-02-18 14:48:41 -0800125 return !!gpio_get_value(SPI_MISO_GPIO);
David Brownelld29389d2009-01-06 14:41:41 -0800126}
127
128#undef pdata
129
130/*
131 * NOTE: this clocks "as fast as we can". It "should" be a function of the
132 * requested device clock. Software overhead means we usually have trouble
133 * reaching even one Mbit/sec (except when we can inline bitops), so for now
134 * we'll just assume we never need additional per-bit slowdowns.
135 */
136#define spidelay(nsecs) do {} while (0)
137
Grant Likelyca632f52011-06-06 01:16:30 -0600138#include "spi-bitbang-txrx.h"
David Brownelld29389d2009-01-06 14:41:41 -0800139
140/*
141 * These functions can leverage inline expansion of GPIO calls to shrink
142 * costs for a txrx bit, often by factors of around ten (by instruction
143 * count). That is particularly visible for larger word sizes, but helps
144 * even with default 8-bit words.
145 *
146 * REVISIT overheads calling these functions for each word also have
147 * significant performance costs. Having txrx_bufs() calls that inline
148 * the txrx_word() logic would help performance, e.g. on larger blocks
149 * used with flash storage or MMC/SD. There should also be ways to make
150 * GCC be less stupid about reloading registers inside the I/O loops,
151 * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
152 */
153
154static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
155 unsigned nsecs, u32 word, u8 bits)
156{
Marek Szyprowski04bb2a02010-06-30 14:27:32 -0600157 return bitbang_txrx_be_cpha0(spi, nsecs, 0, 0, word, bits);
David Brownelld29389d2009-01-06 14:41:41 -0800158}
159
160static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
161 unsigned nsecs, u32 word, u8 bits)
162{
Marek Szyprowski04bb2a02010-06-30 14:27:32 -0600163 return bitbang_txrx_be_cpha1(spi, nsecs, 0, 0, word, bits);
David Brownelld29389d2009-01-06 14:41:41 -0800164}
165
166static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
167 unsigned nsecs, u32 word, u8 bits)
168{
Marek Szyprowski04bb2a02010-06-30 14:27:32 -0600169 return bitbang_txrx_be_cpha0(spi, nsecs, 1, 0, word, bits);
David Brownelld29389d2009-01-06 14:41:41 -0800170}
171
172static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
173 unsigned nsecs, u32 word, u8 bits)
174{
Marek Szyprowski04bb2a02010-06-30 14:27:32 -0600175 return bitbang_txrx_be_cpha1(spi, nsecs, 1, 0, word, bits);
David Brownelld29389d2009-01-06 14:41:41 -0800176}
177
Marek Szyprowski3c8e1a82010-06-30 14:27:37 -0600178/*
179 * These functions do not call setmosi or getmiso if respective flag
180 * (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to
181 * call when such pin is not present or defined in the controller.
182 * A separate set of callbacks is defined to get highest possible
183 * speed in the generic case (when both MISO and MOSI lines are
184 * available), as optimiser will remove the checks when argument is
185 * constant.
186 */
187
188static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi,
189 unsigned nsecs, u32 word, u8 bits)
190{
191 unsigned flags = spi->master->flags;
192 return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
193}
194
195static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi,
196 unsigned nsecs, u32 word, u8 bits)
197{
198 unsigned flags = spi->master->flags;
199 return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
200}
201
202static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi,
203 unsigned nsecs, u32 word, u8 bits)
204{
205 unsigned flags = spi->master->flags;
206 return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
207}
208
209static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi,
210 unsigned nsecs, u32 word, u8 bits)
211{
212 unsigned flags = spi->master->flags;
213 return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
214}
215
David Brownelld29389d2009-01-06 14:41:41 -0800216/*----------------------------------------------------------------------*/
217
218static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
219{
Daniel Mack161c2dd2012-09-05 11:04:35 +0200220 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
221 unsigned int cs = spi_gpio->cs_gpios[spi->chip_select];
David Brownelld29389d2009-01-06 14:41:41 -0800222
223 /* set initial clock polarity */
224 if (is_active)
225 setsck(spi, spi->mode & SPI_CPOL);
226
Michael Bueschbfb9bcd2009-04-02 16:57:07 -0700227 if (cs != SPI_GPIO_NO_CHIPSELECT) {
228 /* SPI is normally active-low */
229 gpio_set_value(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
230 }
David Brownelld29389d2009-01-06 14:41:41 -0800231}
232
233static int spi_gpio_setup(struct spi_device *spi)
234{
Daniel Mack161c2dd2012-09-05 11:04:35 +0200235 unsigned int cs = (unsigned int) spi->controller_data;
236 int status = 0;
237 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
David Brownelld29389d2009-01-06 14:41:41 -0800238
239 if (spi->bits_per_word > 32)
240 return -EINVAL;
241
242 if (!spi->controller_state) {
Michael Bueschbfb9bcd2009-04-02 16:57:07 -0700243 if (cs != SPI_GPIO_NO_CHIPSELECT) {
244 status = gpio_request(cs, dev_name(&spi->dev));
245 if (status)
246 return status;
Uwe Kleine-König05644142012-02-09 22:21:45 +0100247 status = gpio_direction_output(cs,
248 !(spi->mode & SPI_CS_HIGH));
Michael Bueschbfb9bcd2009-04-02 16:57:07 -0700249 }
David Brownelld29389d2009-01-06 14:41:41 -0800250 }
Daniel Mack161c2dd2012-09-05 11:04:35 +0200251 if (!status) {
David Brownelld29389d2009-01-06 14:41:41 -0800252 status = spi_bitbang_setup(spi);
Daniel Mack161c2dd2012-09-05 11:04:35 +0200253 spi_gpio->cs_gpios[spi->chip_select] = cs;
254 }
255
David Brownelld29389d2009-01-06 14:41:41 -0800256 if (status) {
Michael Bueschbfb9bcd2009-04-02 16:57:07 -0700257 if (!spi->controller_state && cs != SPI_GPIO_NO_CHIPSELECT)
David Brownelld29389d2009-01-06 14:41:41 -0800258 gpio_free(cs);
259 }
260 return status;
261}
262
263static void spi_gpio_cleanup(struct spi_device *spi)
264{
Daniel Mack161c2dd2012-09-05 11:04:35 +0200265 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
266 unsigned int cs = spi_gpio->cs_gpios[spi->chip_select];
David Brownelld29389d2009-01-06 14:41:41 -0800267
Michael Bueschbfb9bcd2009-04-02 16:57:07 -0700268 if (cs != SPI_GPIO_NO_CHIPSELECT)
269 gpio_free(cs);
David Brownelld29389d2009-01-06 14:41:41 -0800270 spi_bitbang_cleanup(spi);
271}
272
Manuel Laussc65b53b2011-11-19 13:08:10 +0100273static int __devinit spi_gpio_alloc(unsigned pin, const char *label, bool is_in)
David Brownelld29389d2009-01-06 14:41:41 -0800274{
275 int value;
276
277 value = gpio_request(pin, label);
278 if (value == 0) {
279 if (is_in)
280 value = gpio_direction_input(pin);
281 else
282 value = gpio_direction_output(pin, 0);
283 }
284 return value;
285}
286
Manuel Laussc65b53b2011-11-19 13:08:10 +0100287static int __devinit
Marek Szyprowski3c8e1a82010-06-30 14:27:37 -0600288spi_gpio_request(struct spi_gpio_platform_data *pdata, const char *label,
289 u16 *res_flags)
David Brownelld29389d2009-01-06 14:41:41 -0800290{
291 int value;
292
293 /* NOTE: SPI_*_GPIO symbols may reference "pdata" */
294
Marek Szyprowski3c8e1a82010-06-30 14:27:37 -0600295 if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) {
296 value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false);
297 if (value)
298 goto done;
299 } else {
300 /* HW configuration without MOSI pin */
301 *res_flags |= SPI_MASTER_NO_TX;
302 }
David Brownelld29389d2009-01-06 14:41:41 -0800303
Marek Szyprowski3c8e1a82010-06-30 14:27:37 -0600304 if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) {
305 value = spi_gpio_alloc(SPI_MISO_GPIO, label, true);
306 if (value)
307 goto free_mosi;
308 } else {
309 /* HW configuration without MISO pin */
310 *res_flags |= SPI_MASTER_NO_RX;
311 }
David Brownelld29389d2009-01-06 14:41:41 -0800312
313 value = spi_gpio_alloc(SPI_SCK_GPIO, label, false);
314 if (value)
315 goto free_miso;
316
317 goto done;
318
319free_miso:
Marek Szyprowski3c8e1a82010-06-30 14:27:37 -0600320 if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
321 gpio_free(SPI_MISO_GPIO);
David Brownelld29389d2009-01-06 14:41:41 -0800322free_mosi:
Marek Szyprowski3c8e1a82010-06-30 14:27:37 -0600323 if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
324 gpio_free(SPI_MOSI_GPIO);
David Brownelld29389d2009-01-06 14:41:41 -0800325done:
326 return value;
327}
328
Grant Likely940ab882011-10-05 11:29:49 -0600329static int __devinit spi_gpio_probe(struct platform_device *pdev)
David Brownelld29389d2009-01-06 14:41:41 -0800330{
331 int status;
332 struct spi_master *master;
333 struct spi_gpio *spi_gpio;
334 struct spi_gpio_platform_data *pdata;
Marek Szyprowski3c8e1a82010-06-30 14:27:37 -0600335 u16 master_flags = 0;
David Brownelld29389d2009-01-06 14:41:41 -0800336
337 pdata = pdev->dev.platform_data;
338#ifdef GENERIC_BITBANG
339 if (!pdata || !pdata->num_chipselect)
340 return -ENODEV;
341#endif
342
Marek Szyprowski3c8e1a82010-06-30 14:27:37 -0600343 status = spi_gpio_request(pdata, dev_name(&pdev->dev), &master_flags);
David Brownelld29389d2009-01-06 14:41:41 -0800344 if (status < 0)
345 return status;
346
Daniel Mack161c2dd2012-09-05 11:04:35 +0200347 master = spi_alloc_master(&pdev->dev, sizeof(*spi_gpio) +
348 (sizeof(int) * SPI_N_CHIPSEL));
David Brownelld29389d2009-01-06 14:41:41 -0800349 if (!master) {
350 status = -ENOMEM;
351 goto gpio_free;
352 }
353 spi_gpio = spi_master_get_devdata(master);
354 platform_set_drvdata(pdev, spi_gpio);
355
356 spi_gpio->pdev = pdev;
357 if (pdata)
358 spi_gpio->pdata = *pdata;
359
Marek Szyprowski3c8e1a82010-06-30 14:27:37 -0600360 master->flags = master_flags;
David Brownelld29389d2009-01-06 14:41:41 -0800361 master->bus_num = pdev->id;
362 master->num_chipselect = SPI_N_CHIPSEL;
363 master->setup = spi_gpio_setup;
364 master->cleanup = spi_gpio_cleanup;
365
366 spi_gpio->bitbang.master = spi_master_get(master);
367 spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
Marek Szyprowski3c8e1a82010-06-30 14:27:37 -0600368
Roel Kluin23699f92010-10-02 14:03:32 +0200369 if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) {
Marek Szyprowski3c8e1a82010-06-30 14:27:37 -0600370 spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
371 spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
372 spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
373 spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
374 } else {
375 spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
376 spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
377 spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
378 spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
379 }
David Brownelld29389d2009-01-06 14:41:41 -0800380 spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
381 spi_gpio->bitbang.flags = SPI_CS_HIGH;
382
383 status = spi_bitbang_start(&spi_gpio->bitbang);
384 if (status < 0) {
385 spi_master_put(spi_gpio->bitbang.master);
386gpio_free:
Marek Szyprowski3c8e1a82010-06-30 14:27:37 -0600387 if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
388 gpio_free(SPI_MISO_GPIO);
389 if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
390 gpio_free(SPI_MOSI_GPIO);
David Brownelld29389d2009-01-06 14:41:41 -0800391 gpio_free(SPI_SCK_GPIO);
392 spi_master_put(master);
393 }
394
395 return status;
396}
397
Grant Likely940ab882011-10-05 11:29:49 -0600398static int __devexit spi_gpio_remove(struct platform_device *pdev)
David Brownelld29389d2009-01-06 14:41:41 -0800399{
400 struct spi_gpio *spi_gpio;
401 struct spi_gpio_platform_data *pdata;
402 int status;
403
404 spi_gpio = platform_get_drvdata(pdev);
405 pdata = pdev->dev.platform_data;
406
407 /* stop() unregisters child devices too */
408 status = spi_bitbang_stop(&spi_gpio->bitbang);
409 spi_master_put(spi_gpio->bitbang.master);
410
411 platform_set_drvdata(pdev, NULL);
412
Marek Szyprowski3c8e1a82010-06-30 14:27:37 -0600413 if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
414 gpio_free(SPI_MISO_GPIO);
415 if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
416 gpio_free(SPI_MOSI_GPIO);
David Brownelld29389d2009-01-06 14:41:41 -0800417 gpio_free(SPI_SCK_GPIO);
418
419 return status;
420}
421
422MODULE_ALIAS("platform:" DRIVER_NAME);
423
424static struct platform_driver spi_gpio_driver = {
425 .driver.name = DRIVER_NAME,
426 .driver.owner = THIS_MODULE,
Grant Likely940ab882011-10-05 11:29:49 -0600427 .probe = spi_gpio_probe,
428 .remove = __devexit_p(spi_gpio_remove),
David Brownelld29389d2009-01-06 14:41:41 -0800429};
Grant Likely940ab882011-10-05 11:29:49 -0600430module_platform_driver(spi_gpio_driver);
David Brownelld29389d2009-01-06 14:41:41 -0800431
432MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
433MODULE_AUTHOR("David Brownell");
434MODULE_LICENSE("GPL");