Lines Matching +full:spi +full:- +full:gpio

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SPI host driver using generic bitbanged GPIO
11 #include <linux/gpio/consumer.h>
14 #include <linux/spi/spi.h>
15 #include <linux/spi/spi_bitbang.h>
16 #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
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
40 /*----------------------------------------------------------------------*/
43 * Because the overhead of going through four GPIO procedure calls
47 * - The slow generic way: set up platform_data to hold the GPIO
51 * - The quicker inlined way: only helps with platform GPIO code
62 * #include "spi-gpio.c"
72 /*----------------------------------------------------------------------*/
75 spi_to_spi_gpio(const struct spi_device *spi) in spi_to_spi_gpio() argument
80 bang = spi_controller_get_devdata(spi->controller); in spi_to_spi_gpio()
86 static inline void setsck(const struct spi_device *spi, int is_on) in setsck() argument
88 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); in setsck()
90 gpiod_set_value_cansleep(spi_gpio->sck, is_on); in setsck()
93 static inline void setmosi(const struct spi_device *spi, int is_on) in setmosi() argument
95 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); in setmosi()
97 gpiod_set_value_cansleep(spi_gpio->mosi, is_on); in setmosi()
100 static inline int getmiso(const struct spi_device *spi) in getmiso() argument
102 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); in getmiso()
104 if (spi->mode & SPI_3WIRE) in getmiso()
105 return !!gpiod_get_value_cansleep(spi_gpio->mosi); in getmiso()
107 return !!gpiod_get_value_cansleep(spi_gpio->miso); in getmiso()
114 * we'll just assume we never need additional per-bit slowdowns.
118 #include "spi-bitbang-txrx.h"
121 * These functions can leverage inline expansion of GPIO calls to shrink
124 * even with default 8-bit words.
131 * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
134 static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi, in spi_gpio_txrx_word_mode0() argument
137 if (unlikely(spi->mode & SPI_LSB_FIRST)) in spi_gpio_txrx_word_mode0()
138 return bitbang_txrx_le_cpha0(spi, nsecs, 0, flags, word, bits); in spi_gpio_txrx_word_mode0()
140 return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits); in spi_gpio_txrx_word_mode0()
143 static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi, in spi_gpio_txrx_word_mode1() argument
146 if (unlikely(spi->mode & SPI_LSB_FIRST)) in spi_gpio_txrx_word_mode1()
147 return bitbang_txrx_le_cpha1(spi, nsecs, 0, flags, word, bits); in spi_gpio_txrx_word_mode1()
149 return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits); in spi_gpio_txrx_word_mode1()
152 static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi, in spi_gpio_txrx_word_mode2() argument
155 if (unlikely(spi->mode & SPI_LSB_FIRST)) in spi_gpio_txrx_word_mode2()
156 return bitbang_txrx_le_cpha0(spi, nsecs, 1, flags, word, bits); in spi_gpio_txrx_word_mode2()
158 return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits); in spi_gpio_txrx_word_mode2()
161 static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi, in spi_gpio_txrx_word_mode3() argument
164 if (unlikely(spi->mode & SPI_LSB_FIRST)) in spi_gpio_txrx_word_mode3()
165 return bitbang_txrx_le_cpha1(spi, nsecs, 1, flags, word, bits); in spi_gpio_txrx_word_mode3()
167 return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits); in spi_gpio_txrx_word_mode3()
180 static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi, in spi_gpio_spec_txrx_word_mode0() argument
183 flags = spi->controller->flags; in spi_gpio_spec_txrx_word_mode0()
184 if (unlikely(spi->mode & SPI_LSB_FIRST)) in spi_gpio_spec_txrx_word_mode0()
185 return bitbang_txrx_le_cpha0(spi, nsecs, 0, flags, word, bits); in spi_gpio_spec_txrx_word_mode0()
187 return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits); in spi_gpio_spec_txrx_word_mode0()
190 static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi, in spi_gpio_spec_txrx_word_mode1() argument
193 flags = spi->controller->flags; in spi_gpio_spec_txrx_word_mode1()
194 if (unlikely(spi->mode & SPI_LSB_FIRST)) in spi_gpio_spec_txrx_word_mode1()
195 return bitbang_txrx_le_cpha1(spi, nsecs, 0, flags, word, bits); in spi_gpio_spec_txrx_word_mode1()
197 return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits); in spi_gpio_spec_txrx_word_mode1()
200 static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi, in spi_gpio_spec_txrx_word_mode2() argument
203 flags = spi->controller->flags; in spi_gpio_spec_txrx_word_mode2()
204 if (unlikely(spi->mode & SPI_LSB_FIRST)) in spi_gpio_spec_txrx_word_mode2()
205 return bitbang_txrx_le_cpha0(spi, nsecs, 1, flags, word, bits); in spi_gpio_spec_txrx_word_mode2()
207 return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits); in spi_gpio_spec_txrx_word_mode2()
210 static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi, in spi_gpio_spec_txrx_word_mode3() argument
213 flags = spi->controller->flags; in spi_gpio_spec_txrx_word_mode3()
214 if (unlikely(spi->mode & SPI_LSB_FIRST)) in spi_gpio_spec_txrx_word_mode3()
215 return bitbang_txrx_le_cpha1(spi, nsecs, 1, flags, word, bits); in spi_gpio_spec_txrx_word_mode3()
217 return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits); in spi_gpio_spec_txrx_word_mode3()
220 /*----------------------------------------------------------------------*/
222 static void spi_gpio_chipselect(struct spi_device *spi, int is_active) in spi_gpio_chipselect() argument
224 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); in spi_gpio_chipselect()
228 gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL); in spi_gpio_chipselect()
231 if (spi_gpio->cs_gpios) { in spi_gpio_chipselect()
232 struct gpio_desc *cs = spi_gpio->cs_gpios[spi_get_chipselect(spi, 0)]; in spi_gpio_chipselect()
234 /* SPI chip selects are normally active-low */ in spi_gpio_chipselect()
235 gpiod_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active); in spi_gpio_chipselect()
239 static int spi_gpio_setup(struct spi_device *spi) in spi_gpio_setup() argument
243 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); in spi_gpio_setup()
249 if (spi_gpio->cs_gpios) { in spi_gpio_setup()
250 cs = spi_gpio->cs_gpios[spi_get_chipselect(spi, 0)]; in spi_gpio_setup()
251 if (!spi->controller_state && cs) in spi_gpio_setup()
253 !(spi->mode & SPI_CS_HIGH)); in spi_gpio_setup()
257 status = spi_bitbang_setup(spi); in spi_gpio_setup()
262 static int spi_gpio_set_direction(struct spi_device *spi, bool output) in spi_gpio_set_direction() argument
264 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); in spi_gpio_set_direction()
268 return gpiod_direction_output(spi_gpio->mosi, 1); in spi_gpio_set_direction()
274 * be left logic high if there is a pull-up. Transmitting in spi_gpio_set_direction()
276 * SPI devices in to a bad state. in spi_gpio_set_direction()
278 if (spi->mode & SPI_3WIRE) { in spi_gpio_set_direction()
279 ret = gpiod_direction_input(spi_gpio->mosi); in spi_gpio_set_direction()
287 * nsec delay function for bit-banged GPIO is simply in spi_gpio_set_direction()
288 * {} because bit-banging just doesn't get fast enough in spi_gpio_set_direction()
291 if (spi->mode & SPI_3WIRE_HIZ) { in spi_gpio_set_direction()
292 gpiod_set_value_cansleep(spi_gpio->sck, in spi_gpio_set_direction()
293 !(spi->mode & SPI_CPOL)); in spi_gpio_set_direction()
294 gpiod_set_value_cansleep(spi_gpio->sck, in spi_gpio_set_direction()
295 !!(spi->mode & SPI_CPOL)); in spi_gpio_set_direction()
300 static void spi_gpio_cleanup(struct spi_device *spi) in spi_gpio_cleanup() argument
302 spi_bitbang_cleanup(spi); in spi_gpio_cleanup()
307 * functions associated with a "native" SPI controller if a driver for that
313 * drivers expect to see all-ones data as the no target "response".)
317 spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW); in spi_gpio_request()
318 if (IS_ERR(spi_gpio->mosi)) in spi_gpio_request()
319 return PTR_ERR(spi_gpio->mosi); in spi_gpio_request()
321 spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN); in spi_gpio_request()
322 if (IS_ERR(spi_gpio->miso)) in spi_gpio_request()
323 return PTR_ERR(spi_gpio->miso); in spi_gpio_request()
325 spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW); in spi_gpio_request()
326 return PTR_ERR_OR_ZERO(spi_gpio->sck); in spi_gpio_request()
331 { .compatible = "spi-gpio" },
339 host->dev.of_node = pdev->dev.of_node; in spi_gpio_probe_dt()
340 host->use_gpio_descriptors = true; in spi_gpio_probe_dt()
355 struct device *dev = &pdev->dev; in spi_gpio_probe_pdata()
361 if (!pdata || !pdata->num_chipselect) in spi_gpio_probe_pdata()
362 return -ENODEV; in spi_gpio_probe_pdata()
368 host->num_chipselect = pdata->num_chipselect ?: 1; in spi_gpio_probe_pdata()
370 spi_gpio->cs_gpios = devm_kcalloc(dev, host->num_chipselect, in spi_gpio_probe_pdata()
371 sizeof(*spi_gpio->cs_gpios), in spi_gpio_probe_pdata()
373 if (!spi_gpio->cs_gpios) in spi_gpio_probe_pdata()
374 return -ENOMEM; in spi_gpio_probe_pdata()
376 for (i = 0; i < host->num_chipselect; i++) { in spi_gpio_probe_pdata()
377 spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", i, in spi_gpio_probe_pdata()
379 if (IS_ERR(spi_gpio->cs_gpios[i])) in spi_gpio_probe_pdata()
380 return PTR_ERR(spi_gpio->cs_gpios[i]); in spi_gpio_probe_pdata()
391 struct device *dev = &pdev->dev; in spi_gpio_probe()
396 return -ENOMEM; in spi_gpio_probe()
398 if (pdev->dev.of_node) in spi_gpio_probe()
412 host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32); in spi_gpio_probe()
413 host->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL | in spi_gpio_probe()
415 if (!spi_gpio->mosi) { in spi_gpio_probe()
418 * No setting SPI_CONTROLLER_NO_RX here - if there is only in spi_gpio_probe()
422 host->flags = SPI_CONTROLLER_NO_TX; in spi_gpio_probe()
425 host->bus_num = pdev->id; in spi_gpio_probe()
426 host->setup = spi_gpio_setup; in spi_gpio_probe()
427 host->cleanup = spi_gpio_cleanup; in spi_gpio_probe()
429 bb = &spi_gpio->bitbang; in spi_gpio_probe()
430 bb->master = host; in spi_gpio_probe()
432 * There is some additional business, apart from driving the CS GPIO in spi_gpio_probe()
436 host->flags |= SPI_CONTROLLER_GPIO_SS; in spi_gpio_probe()
437 bb->chipselect = spi_gpio_chipselect; in spi_gpio_probe()
438 bb->set_line_direction = spi_gpio_set_direction; in spi_gpio_probe()
440 if (host->flags & SPI_CONTROLLER_NO_TX) { in spi_gpio_probe()
441 bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0; in spi_gpio_probe()
442 bb->txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1; in spi_gpio_probe()
443 bb->txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2; in spi_gpio_probe()
444 bb->txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3; in spi_gpio_probe()
446 bb->txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0; in spi_gpio_probe()
447 bb->txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1; in spi_gpio_probe()
448 bb->txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2; in spi_gpio_probe()
449 bb->txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3; in spi_gpio_probe()
451 bb->setup_transfer = spi_bitbang_setup_transfer; in spi_gpio_probe()
453 status = spi_bitbang_init(&spi_gpio->bitbang); in spi_gpio_probe()
457 return devm_spi_register_controller(&pdev->dev, host); in spi_gpio_probe()
471 MODULE_DESCRIPTION("SPI host driver using generic bitbanged GPIO ");