Lines Matching +full:spi +full:- +full:mode
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SPI host driver using generic bitbanged GPIO
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
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 /*----------------------------------------------------------------------*/
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"
124 * even with default 8-bit words.
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 void spi_gpio_set_mosi_idle(struct spi_device *spi) in spi_gpio_set_mosi_idle() argument
241 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); in spi_gpio_set_mosi_idle()
243 gpiod_set_value_cansleep(spi_gpio->mosi, in spi_gpio_set_mosi_idle()
244 !!(spi->mode & SPI_MOSI_IDLE_HIGH)); in spi_gpio_set_mosi_idle()
247 static int spi_gpio_setup(struct spi_device *spi) in spi_gpio_setup() argument
250 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); in spi_gpio_setup()
257 if (spi_gpio->cs_gpios) { in spi_gpio_setup()
258 cs = spi_gpio->cs_gpios[spi_get_chipselect(spi, 0)]; in spi_gpio_setup()
259 if (!spi->controller_state && cs) { in spi_gpio_setup()
260 ret = gpiod_direction_output(cs, !(spi->mode & SPI_CS_HIGH)); in spi_gpio_setup()
266 return spi_bitbang_setup(spi); in spi_gpio_setup()
269 static int spi_gpio_set_direction(struct spi_device *spi, bool output) in spi_gpio_set_direction() argument
271 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); in spi_gpio_set_direction()
275 return gpiod_direction_output(spi_gpio->mosi, 1); in spi_gpio_set_direction()
278 * Only change MOSI to an input if using 3WIRE mode. in spi_gpio_set_direction()
281 * be left logic high if there is a pull-up. Transmitting in spi_gpio_set_direction()
283 * SPI devices in to a bad state. in spi_gpio_set_direction()
285 if (spi->mode & SPI_3WIRE) { in spi_gpio_set_direction()
286 ret = gpiod_direction_input(spi_gpio->mosi); in spi_gpio_set_direction()
294 * nsec delay function for bit-banged GPIO is simply in spi_gpio_set_direction()
295 * {} because bit-banging just doesn't get fast enough in spi_gpio_set_direction()
298 if (spi->mode & SPI_3WIRE_HIZ) { in spi_gpio_set_direction()
299 gpiod_set_value_cansleep(spi_gpio->sck, in spi_gpio_set_direction()
300 !(spi->mode & SPI_CPOL)); in spi_gpio_set_direction()
301 gpiod_set_value_cansleep(spi_gpio->sck, in spi_gpio_set_direction()
302 !!(spi->mode & SPI_CPOL)); in spi_gpio_set_direction()
307 static void spi_gpio_cleanup(struct spi_device *spi) in spi_gpio_cleanup() argument
309 spi_bitbang_cleanup(spi); in spi_gpio_cleanup()
314 * functions associated with a "native" SPI controller if a driver for that
320 * drivers expect to see all-ones data as the no target "response".)
324 spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW); in spi_gpio_request()
325 if (IS_ERR(spi_gpio->mosi)) in spi_gpio_request()
326 return PTR_ERR(spi_gpio->mosi); in spi_gpio_request()
328 spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN); in spi_gpio_request()
329 if (IS_ERR(spi_gpio->miso)) in spi_gpio_request()
330 return PTR_ERR(spi_gpio->miso); in spi_gpio_request()
332 spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW); in spi_gpio_request()
333 return PTR_ERR_OR_ZERO(spi_gpio->sck); in spi_gpio_request()
339 struct device *dev = &pdev->dev; in spi_gpio_probe_pdata()
345 if (!pdata || !pdata->num_chipselect) in spi_gpio_probe_pdata()
346 return -ENODEV; in spi_gpio_probe_pdata()
352 host->num_chipselect = pdata->num_chipselect ?: 1; in spi_gpio_probe_pdata()
354 spi_gpio->cs_gpios = devm_kcalloc(dev, host->num_chipselect, in spi_gpio_probe_pdata()
355 sizeof(*spi_gpio->cs_gpios), in spi_gpio_probe_pdata()
357 if (!spi_gpio->cs_gpios) in spi_gpio_probe_pdata()
358 return -ENOMEM; in spi_gpio_probe_pdata()
360 for (i = 0; i < host->num_chipselect; i++) { in spi_gpio_probe_pdata()
361 spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", i, in spi_gpio_probe_pdata()
363 if (IS_ERR(spi_gpio->cs_gpios[i])) in spi_gpio_probe_pdata()
364 return PTR_ERR(spi_gpio->cs_gpios[i]); in spi_gpio_probe_pdata()
375 struct device *dev = &pdev->dev; in spi_gpio_probe()
381 return -ENOMEM; in spi_gpio_probe()
384 device_set_node(&host->dev, fwnode); in spi_gpio_probe()
385 host->use_gpio_descriptors = true; in spi_gpio_probe()
398 host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32); in spi_gpio_probe()
399 host->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL | in spi_gpio_probe()
402 if (!spi_gpio->mosi) { in spi_gpio_probe()
405 * No setting SPI_CONTROLLER_NO_RX here - if there is only in spi_gpio_probe()
409 host->flags = SPI_CONTROLLER_NO_TX; in spi_gpio_probe()
412 host->bus_num = pdev->id; in spi_gpio_probe()
413 host->setup = spi_gpio_setup; in spi_gpio_probe()
414 host->cleanup = spi_gpio_cleanup; in spi_gpio_probe()
416 bb = &spi_gpio->bitbang; in spi_gpio_probe()
417 bb->ctlr = host; in spi_gpio_probe()
423 host->flags |= SPI_CONTROLLER_GPIO_SS; in spi_gpio_probe()
424 bb->chipselect = spi_gpio_chipselect; in spi_gpio_probe()
425 bb->set_line_direction = spi_gpio_set_direction; in spi_gpio_probe()
426 bb->set_mosi_idle = spi_gpio_set_mosi_idle; in spi_gpio_probe()
428 if (host->flags & SPI_CONTROLLER_NO_TX) { in spi_gpio_probe()
429 bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0; in spi_gpio_probe()
430 bb->txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1; in spi_gpio_probe()
431 bb->txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2; in spi_gpio_probe()
432 bb->txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3; in spi_gpio_probe()
434 bb->txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0; in spi_gpio_probe()
435 bb->txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1; in spi_gpio_probe()
436 bb->txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2; in spi_gpio_probe()
437 bb->txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3; in spi_gpio_probe()
439 bb->setup_transfer = spi_bitbang_setup_transfer; in spi_gpio_probe()
441 status = spi_bitbang_init(&spi_gpio->bitbang); in spi_gpio_probe()
445 return devm_spi_register_controller(&pdev->dev, host); in spi_gpio_probe()
451 { .compatible = "spi-gpio" },
465 MODULE_DESCRIPTION("SPI host driver using generic bitbanged GPIO ");