1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SPI host driver using generic bitbanged GPIO
5 * Copyright (C) 2006,2008 David Brownell
6 * Copyright (C) 2017 Linus Walleij
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/gpio/consumer.h>
13 #include <linux/delay.h>
15 #include <linux/spi/spi.h>
16 #include <linux/spi/spi_bitbang.h>
17 #include <linux/spi/spi_gpio.h>
20 * This bitbanging SPI host driver should help make systems usable
21 * when a native hardware SPI engine is not available, perhaps because
22 * its driver isn't yet working or because the I/O pins it requires
23 * are used for other purposes.
25 * platform_device->driver_data ... points to spi_gpio
27 * spi->controller_state ... reserved for bitbang framework code
29 * spi->controller->dev.driver_data ... points to spi_gpio->bitbang
33 struct spi_bitbang bitbang;
34 struct gpio_desc *sck;
35 struct gpio_desc *miso;
36 struct gpio_desc *mosi;
37 struct gpio_desc **cs_gpios;
42 /*----------------------------------------------------------------------*/
45 * Because the overhead of going through four GPIO procedure calls
46 * per transferred bit can make performance a problem, this code
47 * is set up so that you can use it in either of two ways:
49 * - The slow generic way: set up platform_data to hold the GPIO
50 * numbers used for MISO/MOSI/SCK, and issue procedure calls for
51 * each of them. This driver can handle several such busses.
53 * - The quicker inlined way: only helps with platform GPIO code
54 * that inlines operations for constant GPIOs. This can give
55 * you tight (fast!) inner loops, but each such bus needs a
56 * new driver. You'll define a new C file, with Makefile and
57 * Kconfig support; the C code can be a total of six lines:
59 * #define DRIVER_NAME "myboard_spi2"
60 * #define SPI_MISO_GPIO 119
61 * #define SPI_MOSI_GPIO 120
62 * #define SPI_SCK_GPIO 121
63 * #define SPI_N_CHIPSEL 4
64 * #include "spi-gpio.c"
68 #define DRIVER_NAME "spi_gpio"
70 #define GENERIC_BITBANG /* vs tight inlines */
74 /*----------------------------------------------------------------------*/
76 static inline struct spi_gpio *__pure
77 spi_to_spi_gpio(const struct spi_device *spi)
79 const struct spi_bitbang *bang;
80 struct spi_gpio *spi_gpio;
82 bang = spi_controller_get_devdata(spi->controller);
83 spi_gpio = container_of(bang, struct spi_gpio, bitbang);
87 /* These helpers are in turn called by the bitbang inlines */
88 static inline void setsck(const struct spi_device *spi, int is_on)
90 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
92 gpiod_set_value_cansleep(spi_gpio->sck, is_on);
95 static inline void setmosi(const struct spi_device *spi, int is_on)
97 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
99 gpiod_set_value_cansleep(spi_gpio->mosi, is_on);
102 static inline int getmiso(const struct spi_device *spi)
104 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
106 if (spi->mode & SPI_3WIRE)
107 return !!gpiod_get_value_cansleep(spi_gpio->mosi);
109 return !!gpiod_get_value_cansleep(spi_gpio->miso);
113 * Generic bit-banged GPIO SPI might free-run at something in the range
114 * 1Mbps ~ 10Mbps (depending on the platform), and some SPI devices may
115 * need to be clocked at a lower rate. ndelay() is often implemented by
116 * udelay() with rounding up, so do the delay only for nsecs >= 500
117 * (<= 1Mbps). The conditional test adds a small overhead.
120 static inline void spidelay(unsigned long nsecs)
126 #include "spi-bitbang-txrx.h"
129 * These functions can leverage inline expansion of GPIO calls to shrink
130 * costs for a txrx bit, often by factors of around ten (by instruction
131 * count). That is particularly visible for larger word sizes, but helps
132 * even with default 8-bit words.
134 * REVISIT overheads calling these functions for each word also have
135 * significant performance costs. Having txrx_bufs() calls that inline
136 * the txrx_word() logic would help performance, e.g. on larger blocks
137 * used with flash storage or MMC/SD. There should also be ways to make
138 * GCC be less stupid about reloading registers inside the I/O loops,
139 * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
142 static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
143 unsigned nsecs, u32 word, u8 bits, unsigned flags)
145 if (unlikely(spi->mode & SPI_LSB_FIRST))
146 return bitbang_txrx_le_cpha0(spi, nsecs, 0, flags, word, bits);
148 return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
151 static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
152 unsigned nsecs, u32 word, u8 bits, unsigned flags)
154 if (unlikely(spi->mode & SPI_LSB_FIRST))
155 return bitbang_txrx_le_cpha1(spi, nsecs, 0, flags, word, bits);
157 return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
160 static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
161 unsigned nsecs, u32 word, u8 bits, unsigned flags)
163 if (unlikely(spi->mode & SPI_LSB_FIRST))
164 return bitbang_txrx_le_cpha0(spi, nsecs, 1, flags, word, bits);
166 return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
169 static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
170 unsigned nsecs, u32 word, u8 bits, unsigned flags)
172 if (unlikely(spi->mode & SPI_LSB_FIRST))
173 return bitbang_txrx_le_cpha1(spi, nsecs, 1, flags, word, bits);
175 return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
179 * These functions do not call setmosi or getmiso if respective flag
180 * (SPI_CONTROLLER_NO_RX or SPI_CONTROLLER_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
188 static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi,
189 unsigned nsecs, u32 word, u8 bits, unsigned flags)
191 flags = spi->controller->flags;
192 if (unlikely(spi->mode & SPI_LSB_FIRST))
193 return bitbang_txrx_le_cpha0(spi, nsecs, 0, flags, word, bits);
195 return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
198 static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi,
199 unsigned nsecs, u32 word, u8 bits, unsigned flags)
201 flags = spi->controller->flags;
202 if (unlikely(spi->mode & SPI_LSB_FIRST))
203 return bitbang_txrx_le_cpha1(spi, nsecs, 0, flags, word, bits);
205 return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
208 static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi,
209 unsigned nsecs, u32 word, u8 bits, unsigned flags)
211 flags = spi->controller->flags;
212 if (unlikely(spi->mode & SPI_LSB_FIRST))
213 return bitbang_txrx_le_cpha0(spi, nsecs, 1, flags, word, bits);
215 return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
218 static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi,
219 unsigned nsecs, u32 word, u8 bits, unsigned flags)
221 flags = spi->controller->flags;
222 if (unlikely(spi->mode & SPI_LSB_FIRST))
223 return bitbang_txrx_le_cpha1(spi, nsecs, 1, flags, word, bits);
225 return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
228 /*----------------------------------------------------------------------*/
230 static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
232 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
234 /* set initial clock line level */
236 if (spi_gpio->sck_idle_input)
237 gpiod_direction_output(spi_gpio->sck, spi->mode & SPI_CPOL);
239 gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL);
243 * Drive chip select line, if we have one.
244 * SPI chip selects are normally active-low, but when
245 * cs_dont_invert is set, we assume their polarity is
246 * controlled by the GPIO, and write '1' to assert.
248 if (spi_gpio->cs_gpios) {
249 struct gpio_desc *cs = spi_gpio->cs_gpios[spi_get_chipselect(spi, 0)];
250 int val = ((spi->mode & SPI_CS_HIGH) || spi_gpio->cs_dont_invert) ?
251 is_active : !is_active;
253 gpiod_set_value_cansleep(cs, val);
256 if (spi_gpio->sck_idle_input && !is_active)
257 gpiod_direction_input(spi_gpio->sck);
260 static int spi_gpio_setup(struct spi_device *spi)
262 struct gpio_desc *cs;
264 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
267 * The CS GPIOs have already been
268 * initialized from the descriptor lookup.
269 * Here we set them to the non-asserted state.
271 if (spi_gpio->cs_gpios) {
272 cs = spi_gpio->cs_gpios[spi_get_chipselect(spi, 0)];
273 if (!spi->controller_state && cs)
274 status = gpiod_direction_output(cs,
275 !((spi->mode & SPI_CS_HIGH) ||
276 spi_gpio->cs_dont_invert));
280 status = spi_bitbang_setup(spi);
285 static int spi_gpio_set_direction(struct spi_device *spi, bool output)
287 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
291 return gpiod_direction_output(spi_gpio->mosi, 1);
294 * Only change MOSI to an input if using 3WIRE mode.
295 * Otherwise, MOSI could be left floating if there is
296 * no pull resistor connected to the I/O pin, or could
297 * be left logic high if there is a pull-up. Transmitting
298 * logic high when only clocking MISO data in can put some
299 * SPI devices in to a bad state.
301 if (spi->mode & SPI_3WIRE) {
302 ret = gpiod_direction_input(spi_gpio->mosi);
307 * Send a turnaround high impedance cycle when switching
308 * from output to input. Theoretically there should be
309 * a clock delay here, but as has been noted above, the
310 * nsec delay function for bit-banged GPIO is simply
311 * {} because bit-banging just doesn't get fast enough
314 if (spi->mode & SPI_3WIRE_HIZ) {
315 gpiod_set_value_cansleep(spi_gpio->sck,
316 !(spi->mode & SPI_CPOL));
317 gpiod_set_value_cansleep(spi_gpio->sck,
318 !!(spi->mode & SPI_CPOL));
323 static void spi_gpio_cleanup(struct spi_device *spi)
325 spi_bitbang_cleanup(spi);
329 * It can be convenient to use this driver with pins that have alternate
330 * functions associated with a "native" SPI controller if a driver for that
331 * controller is not available, or is missing important functionality.
333 * On platforms which can do so, configure MISO with a weak pullup unless
334 * there's an external pullup on that signal. That saves power by avoiding
335 * floating signals. (A weak pulldown would save power too, but many
336 * drivers expect to see all-ones data as the no target "response".)
338 static int spi_gpio_request(struct device *dev, struct spi_gpio *spi_gpio)
340 spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW);
341 if (IS_ERR(spi_gpio->mosi))
342 return PTR_ERR(spi_gpio->mosi);
344 spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN);
345 if (IS_ERR(spi_gpio->miso))
346 return PTR_ERR(spi_gpio->miso);
348 spi_gpio->sck_idle_input = device_property_read_bool(dev, "sck-idle-input");
349 spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
350 return PTR_ERR_OR_ZERO(spi_gpio->sck);
354 * In order to implement "sck-idle-input" (which requires SCK
355 * direction and CS level to be switched in a particular order),
356 * we need to control GPIO chip selects from within this driver.
359 static int spi_gpio_probe_get_cs_gpios(struct device *dev,
360 struct spi_master *master,
361 bool gpio_defines_polarity)
364 struct spi_gpio *spi_gpio = spi_master_get_devdata(master);
366 spi_gpio->cs_dont_invert = gpio_defines_polarity;
367 spi_gpio->cs_gpios = devm_kcalloc(dev, master->num_chipselect,
368 sizeof(*spi_gpio->cs_gpios),
370 if (!spi_gpio->cs_gpios)
373 for (i = 0; i < master->num_chipselect; i++) {
374 spi_gpio->cs_gpios[i] =
375 devm_gpiod_get_index(dev, "cs", i,
376 gpio_defines_polarity ?
377 GPIOD_OUT_LOW : GPIOD_OUT_HIGH);
378 if (IS_ERR(spi_gpio->cs_gpios[i]))
379 return PTR_ERR(spi_gpio->cs_gpios[i]);
386 static const struct of_device_id spi_gpio_dt_ids[] = {
387 { .compatible = "spi-gpio" },
390 MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids);
392 static int spi_gpio_probe_dt(struct platform_device *pdev,
393 struct spi_controller *host)
395 struct device *dev = &pdev->dev;
397 host->dev.of_node = dev->of_node;
398 host->num_chipselect = gpiod_count(dev, "cs");
400 return spi_gpio_probe_get_cs_gpios(dev, host, true);
403 static inline int spi_gpio_probe_dt(struct platform_device *pdev,
404 struct spi_controller *host)
410 static int spi_gpio_probe_pdata(struct platform_device *pdev,
411 struct spi_controller *host)
413 struct device *dev = &pdev->dev;
414 struct spi_gpio_platform_data *pdata = dev_get_platdata(dev);
416 #ifdef GENERIC_BITBANG
417 if (!pdata || !pdata->num_chipselect)
421 * The host needs to think there is a chipselect even if not
424 host->num_chipselect = pdata->num_chipselect ?: 1;
426 return spi_gpio_probe_get_cs_gpios(dev, host, false);
429 static int spi_gpio_probe(struct platform_device *pdev)
432 struct spi_controller *host;
433 struct spi_gpio *spi_gpio;
434 struct device *dev = &pdev->dev;
435 struct spi_bitbang *bb;
437 host = devm_spi_alloc_host(dev, sizeof(*spi_gpio));
441 if (pdev->dev.of_node)
442 status = spi_gpio_probe_dt(pdev, host);
444 status = spi_gpio_probe_pdata(pdev, host);
449 spi_gpio = spi_controller_get_devdata(host);
451 status = spi_gpio_request(dev, spi_gpio);
455 host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
456 host->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL |
457 SPI_CS_HIGH | SPI_LSB_FIRST;
458 if (!spi_gpio->mosi) {
459 /* HW configuration without MOSI pin
461 * No setting SPI_CONTROLLER_NO_RX here - if there is only
462 * a MOSI pin connected the host can still do RX by
463 * changing the direction of the line.
465 host->flags = SPI_CONTROLLER_NO_TX;
468 host->bus_num = pdev->id;
469 host->setup = spi_gpio_setup;
470 host->cleanup = spi_gpio_cleanup;
472 bb = &spi_gpio->bitbang;
475 * There is some additional business, apart from driving the CS GPIO
476 * line, that we need to do on selection. This makes the local
477 * callback for chipselect always get called.
479 host->flags |= SPI_CONTROLLER_GPIO_SS;
480 bb->chipselect = spi_gpio_chipselect;
481 bb->set_line_direction = spi_gpio_set_direction;
483 if (host->flags & SPI_CONTROLLER_NO_TX) {
484 bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
485 bb->txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
486 bb->txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
487 bb->txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
489 bb->txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
490 bb->txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
491 bb->txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
492 bb->txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
494 bb->setup_transfer = spi_bitbang_setup_transfer;
496 status = spi_bitbang_init(&spi_gpio->bitbang);
500 return devm_spi_register_controller(&pdev->dev, host);
503 MODULE_ALIAS("platform:" DRIVER_NAME);
505 static struct platform_driver spi_gpio_driver = {
508 .of_match_table = of_match_ptr(spi_gpio_dt_ids),
510 .probe = spi_gpio_probe,
512 module_platform_driver(spi_gpio_driver);
514 MODULE_DESCRIPTION("SPI host driver using generic bitbanged GPIO ");
515 MODULE_AUTHOR("David Brownell");
516 MODULE_LICENSE("GPL");