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/gpio/consumer.h>
9 #include <linux/kernel.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/property.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;
40 /*----------------------------------------------------------------------*/
42 #define DRIVER_NAME "spi_gpio"
44 /*----------------------------------------------------------------------*/
46 static inline struct spi_gpio *__pure
47 spi_to_spi_gpio(const struct spi_device *spi)
49 struct spi_bitbang *bang;
50 struct spi_gpio *spi_gpio;
52 bang = spi_controller_get_devdata(spi->controller);
53 spi_gpio = container_of(bang, struct spi_gpio, bitbang);
57 /* These helpers are in turn called by the bitbang inlines */
58 static inline void setsck(const struct spi_device *spi, int is_on)
60 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
62 gpiod_set_value_cansleep(spi_gpio->sck, is_on);
65 static inline void setmosi(const struct spi_device *spi, int is_on)
67 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
69 gpiod_set_value_cansleep(spi_gpio->mosi, is_on);
72 static inline int getmiso(const struct spi_device *spi)
74 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
76 if (spi->mode & SPI_3WIRE)
77 return !!gpiod_get_value_cansleep(spi_gpio->mosi);
79 return !!gpiod_get_value_cansleep(spi_gpio->miso);
83 * NOTE: this clocks "as fast as we can". It "should" be a function of the
84 * requested device clock. Software overhead means we usually have trouble
85 * reaching even one Mbit/sec (except when we can inline bitops), so for now
86 * we'll just assume we never need additional per-bit slowdowns.
88 #define spidelay(nsecs) do {} while (0)
90 #include "spi-bitbang-txrx.h"
93 * These functions can leverage inline expansion of GPIO calls to shrink
94 * costs for a txrx bit, often by factors of around ten (by instruction
95 * count). That is particularly visible for larger word sizes, but helps
96 * even with default 8-bit words.
98 * REVISIT overheads calling these functions for each word also have
99 * significant performance costs. Having txrx_bufs() calls that inline
100 * the txrx_word() logic would help performance, e.g. on larger blocks
101 * used with flash storage or MMC/SD. There should also be ways to make
102 * GCC be less stupid about reloading registers inside the I/O loops,
103 * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
106 static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
107 unsigned nsecs, u32 word, u8 bits, unsigned flags)
109 if (unlikely(spi->mode & SPI_LSB_FIRST))
110 return bitbang_txrx_le_cpha0(spi, nsecs, 0, flags, word, bits);
112 return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
115 static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
116 unsigned nsecs, u32 word, u8 bits, unsigned flags)
118 if (unlikely(spi->mode & SPI_LSB_FIRST))
119 return bitbang_txrx_le_cpha1(spi, nsecs, 0, flags, word, bits);
121 return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
124 static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
125 unsigned nsecs, u32 word, u8 bits, unsigned flags)
127 if (unlikely(spi->mode & SPI_LSB_FIRST))
128 return bitbang_txrx_le_cpha0(spi, nsecs, 1, flags, word, bits);
130 return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
133 static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
134 unsigned nsecs, u32 word, u8 bits, unsigned flags)
136 if (unlikely(spi->mode & SPI_LSB_FIRST))
137 return bitbang_txrx_le_cpha1(spi, nsecs, 1, flags, word, bits);
139 return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
143 * These functions do not call setmosi or getmiso if respective flag
144 * (SPI_CONTROLLER_NO_RX or SPI_CONTROLLER_NO_TX) is set, so they are safe to
145 * call when such pin is not present or defined in the controller.
146 * A separate set of callbacks is defined to get highest possible
147 * speed in the generic case (when both MISO and MOSI lines are
148 * available), as optimiser will remove the checks when argument is
152 static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi,
153 unsigned nsecs, u32 word, u8 bits, unsigned flags)
155 flags = spi->controller->flags;
156 if (unlikely(spi->mode & SPI_LSB_FIRST))
157 return bitbang_txrx_le_cpha0(spi, nsecs, 0, flags, word, bits);
159 return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
162 static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi,
163 unsigned nsecs, u32 word, u8 bits, unsigned flags)
165 flags = spi->controller->flags;
166 if (unlikely(spi->mode & SPI_LSB_FIRST))
167 return bitbang_txrx_le_cpha1(spi, nsecs, 0, flags, word, bits);
169 return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
172 static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi,
173 unsigned nsecs, u32 word, u8 bits, unsigned flags)
175 flags = spi->controller->flags;
176 if (unlikely(spi->mode & SPI_LSB_FIRST))
177 return bitbang_txrx_le_cpha0(spi, nsecs, 1, flags, word, bits);
179 return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
182 static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi,
183 unsigned nsecs, u32 word, u8 bits, unsigned flags)
185 flags = spi->controller->flags;
186 if (unlikely(spi->mode & SPI_LSB_FIRST))
187 return bitbang_txrx_le_cpha1(spi, nsecs, 1, flags, word, bits);
189 return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
192 /*----------------------------------------------------------------------*/
194 static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
196 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
198 /* set initial clock line level */
200 gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL);
202 /* Drive chip select line, if we have one */
203 if (spi_gpio->cs_gpios) {
204 struct gpio_desc *cs = spi_gpio->cs_gpios[spi_get_chipselect(spi, 0)];
206 /* SPI chip selects are normally active-low */
207 gpiod_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
211 static void spi_gpio_set_mosi_idle(struct spi_device *spi)
213 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
215 gpiod_set_value_cansleep(spi_gpio->mosi,
216 !!(spi->mode & SPI_MOSI_IDLE_HIGH));
219 static int spi_gpio_setup(struct spi_device *spi)
221 struct gpio_desc *cs;
222 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
226 * The CS GPIOs have already been
227 * initialized from the descriptor lookup.
229 if (spi_gpio->cs_gpios) {
230 cs = spi_gpio->cs_gpios[spi_get_chipselect(spi, 0)];
231 if (!spi->controller_state && cs) {
232 ret = gpiod_direction_output(cs, !(spi->mode & SPI_CS_HIGH));
238 return spi_bitbang_setup(spi);
241 static int spi_gpio_set_direction(struct spi_device *spi, bool output)
243 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
247 return gpiod_direction_output(spi_gpio->mosi, 1);
250 * Only change MOSI to an input if using 3WIRE mode.
251 * Otherwise, MOSI could be left floating if there is
252 * no pull resistor connected to the I/O pin, or could
253 * be left logic high if there is a pull-up. Transmitting
254 * logic high when only clocking MISO data in can put some
255 * SPI devices in to a bad state.
257 if (spi->mode & SPI_3WIRE) {
258 ret = gpiod_direction_input(spi_gpio->mosi);
263 * Send a turnaround high impedance cycle when switching
264 * from output to input. Theoretically there should be
265 * a clock delay here, but as has been noted above, the
266 * nsec delay function for bit-banged GPIO is simply
267 * {} because bit-banging just doesn't get fast enough
270 if (spi->mode & SPI_3WIRE_HIZ) {
271 gpiod_set_value_cansleep(spi_gpio->sck,
272 !(spi->mode & SPI_CPOL));
273 gpiod_set_value_cansleep(spi_gpio->sck,
274 !!(spi->mode & SPI_CPOL));
279 static void spi_gpio_cleanup(struct spi_device *spi)
281 spi_bitbang_cleanup(spi);
285 * It can be convenient to use this driver with pins that have alternate
286 * functions associated with a "native" SPI controller if a driver for that
287 * controller is not available, or is missing important functionality.
289 * On platforms which can do so, configure MISO with a weak pullup unless
290 * there's an external pullup on that signal. That saves power by avoiding
291 * floating signals. (A weak pulldown would save power too, but many
292 * drivers expect to see all-ones data as the no target "response".)
294 static int spi_gpio_request(struct device *dev, struct spi_gpio *spi_gpio)
296 spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW);
297 if (IS_ERR(spi_gpio->mosi))
298 return PTR_ERR(spi_gpio->mosi);
300 spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN);
301 if (IS_ERR(spi_gpio->miso))
302 return PTR_ERR(spi_gpio->miso);
304 spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
305 return PTR_ERR_OR_ZERO(spi_gpio->sck);
308 static int spi_gpio_probe_pdata(struct platform_device *pdev,
309 struct spi_controller *host)
311 struct device *dev = &pdev->dev;
312 struct spi_gpio_platform_data *pdata = dev_get_platdata(dev);
313 struct spi_gpio *spi_gpio = spi_controller_get_devdata(host);
319 /* It's just one always-selected device, fine to continue */
320 if (!pdata->num_chipselect)
323 host->num_chipselect = pdata->num_chipselect;
324 spi_gpio->cs_gpios = devm_kcalloc(dev, host->num_chipselect,
325 sizeof(*spi_gpio->cs_gpios),
327 if (!spi_gpio->cs_gpios)
330 for (i = 0; i < host->num_chipselect; i++) {
331 spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", i,
333 if (IS_ERR(spi_gpio->cs_gpios[i]))
334 return PTR_ERR(spi_gpio->cs_gpios[i]);
340 static int spi_gpio_probe(struct platform_device *pdev)
343 struct spi_controller *host;
344 struct spi_gpio *spi_gpio;
345 struct device *dev = &pdev->dev;
346 struct fwnode_handle *fwnode = dev_fwnode(dev);
347 struct spi_bitbang *bb;
349 host = devm_spi_alloc_host(dev, sizeof(*spi_gpio));
354 device_set_node(&host->dev, fwnode);
355 host->use_gpio_descriptors = true;
357 status = spi_gpio_probe_pdata(pdev, host);
362 spi_gpio = spi_controller_get_devdata(host);
364 status = spi_gpio_request(dev, spi_gpio);
368 host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
369 host->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL |
370 SPI_CS_HIGH | SPI_LSB_FIRST | SPI_MOSI_IDLE_LOW |
372 if (!spi_gpio->mosi) {
373 /* HW configuration without MOSI pin
375 * No setting SPI_CONTROLLER_NO_RX here - if there is only
376 * a MOSI pin connected the host can still do RX by
377 * changing the direction of the line.
379 host->flags = SPI_CONTROLLER_NO_TX;
382 host->bus_num = pdev->id;
383 host->setup = spi_gpio_setup;
384 host->cleanup = spi_gpio_cleanup;
386 bb = &spi_gpio->bitbang;
389 * There is some additional business, apart from driving the CS GPIO
390 * line, that we need to do on selection. This makes the local
391 * callback for chipselect always get called.
393 host->flags |= SPI_CONTROLLER_GPIO_SS;
394 bb->chipselect = spi_gpio_chipselect;
395 bb->set_line_direction = spi_gpio_set_direction;
396 bb->set_mosi_idle = spi_gpio_set_mosi_idle;
398 if (host->flags & SPI_CONTROLLER_NO_TX) {
399 bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
400 bb->txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
401 bb->txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
402 bb->txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
404 bb->txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
405 bb->txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
406 bb->txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
407 bb->txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
409 bb->setup_transfer = spi_bitbang_setup_transfer;
411 status = spi_bitbang_init(&spi_gpio->bitbang);
415 return devm_spi_register_controller(&pdev->dev, host);
418 static const struct of_device_id spi_gpio_dt_ids[] = {
419 { .compatible = "spi-gpio" },
422 MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids);
424 static struct platform_driver spi_gpio_driver = {
427 .of_match_table = spi_gpio_dt_ids,
429 .probe = spi_gpio_probe,
431 module_platform_driver(spi_gpio_driver);
433 MODULE_DESCRIPTION("SPI host driver using generic bitbanged GPIO ");
434 MODULE_AUTHOR("David Brownell");
435 MODULE_LICENSE("GPL");
436 MODULE_ALIAS("platform:" DRIVER_NAME);