Lines Matching +full:bit +full:- +full:banging
1 // SPDX-License-Identifier: GPL-2.0-or-later
26 * platform_device->driver_data ... points to spi_gpio
28 * spi->controller_state ... reserved for bitbang framework code
30 * spi->master->dev.driver_data ... points to spi_gpio->bitbang
41 /*----------------------------------------------------------------------*/
45 * per transferred bit can make performance a problem, this code
48 * - The slow generic way: set up platform_data to hold the GPIO
52 * - The quicker inlined way: only helps with platform GPIO code
63 * #include "spi-gpio.c"
73 /*----------------------------------------------------------------------*/
81 bang = spi_master_get_devdata(spi->master); in spi_to_spi_gpio()
91 gpiod_set_value_cansleep(spi_gpio->sck, is_on); in setsck()
98 gpiod_set_value_cansleep(spi_gpio->mosi, is_on); in setmosi()
105 if (spi->mode & SPI_3WIRE) in getmiso()
106 return !!gpiod_get_value_cansleep(spi_gpio->mosi); in getmiso()
108 return !!gpiod_get_value_cansleep(spi_gpio->miso); in getmiso()
115 * we'll just assume we never need additional per-bit slowdowns.
119 #include "spi-bitbang-txrx.h"
123 * costs for a txrx bit, often by factors of around ten (by instruction
125 * even with default 8-bit words.
172 flags = spi->master->flags; in spi_gpio_spec_txrx_word_mode0()
179 flags = spi->master->flags; in spi_gpio_spec_txrx_word_mode1()
186 flags = spi->master->flags; in spi_gpio_spec_txrx_word_mode2()
193 flags = spi->master->flags; in spi_gpio_spec_txrx_word_mode3()
197 /*----------------------------------------------------------------------*/
205 gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL); in spi_gpio_chipselect()
208 if (spi_gpio->cs_gpios) { in spi_gpio_chipselect()
209 struct gpio_desc *cs = spi_gpio->cs_gpios[spi->chip_select]; in spi_gpio_chipselect()
211 /* SPI chip selects are normally active-low */ in spi_gpio_chipselect()
212 gpiod_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active); in spi_gpio_chipselect()
226 if (spi_gpio->cs_gpios) { in spi_gpio_setup()
227 cs = spi_gpio->cs_gpios[spi->chip_select]; in spi_gpio_setup()
228 if (!spi->controller_state && cs) in spi_gpio_setup()
230 !(spi->mode & SPI_CS_HIGH)); in spi_gpio_setup()
245 return gpiod_direction_output(spi_gpio->mosi, 1); in spi_gpio_set_direction()
251 * be left logic high if there is a pull-up. Transmitting in spi_gpio_set_direction()
255 if (spi->mode & SPI_3WIRE) { in spi_gpio_set_direction()
256 ret = gpiod_direction_input(spi_gpio->mosi); in spi_gpio_set_direction()
264 * nsec delay function for bit-banged GPIO is simply in spi_gpio_set_direction()
265 * {} because bit-banging just doesn't get fast enough in spi_gpio_set_direction()
268 if (spi->mode & SPI_3WIRE_HIZ) { in spi_gpio_set_direction()
269 gpiod_set_value_cansleep(spi_gpio->sck, in spi_gpio_set_direction()
270 !(spi->mode & SPI_CPOL)); in spi_gpio_set_direction()
271 gpiod_set_value_cansleep(spi_gpio->sck, in spi_gpio_set_direction()
272 !!(spi->mode & SPI_CPOL)); in spi_gpio_set_direction()
290 * drivers expect to see all-ones data as the no slave "response".)
294 spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW); in spi_gpio_request()
295 if (IS_ERR(spi_gpio->mosi)) in spi_gpio_request()
296 return PTR_ERR(spi_gpio->mosi); in spi_gpio_request()
298 spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN); in spi_gpio_request()
299 if (IS_ERR(spi_gpio->miso)) in spi_gpio_request()
300 return PTR_ERR(spi_gpio->miso); in spi_gpio_request()
302 spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW); in spi_gpio_request()
303 return PTR_ERR_OR_ZERO(spi_gpio->sck); in spi_gpio_request()
308 { .compatible = "spi-gpio" },
316 master->dev.of_node = pdev->dev.of_node; in spi_gpio_probe_dt()
317 master->use_gpio_descriptors = true; in spi_gpio_probe_dt()
332 struct device *dev = &pdev->dev; in spi_gpio_probe_pdata()
338 if (!pdata || !pdata->num_chipselect) in spi_gpio_probe_pdata()
339 return -ENODEV; in spi_gpio_probe_pdata()
345 master->num_chipselect = pdata->num_chipselect ?: 1; in spi_gpio_probe_pdata()
347 spi_gpio->cs_gpios = devm_kcalloc(dev, master->num_chipselect, in spi_gpio_probe_pdata()
348 sizeof(*spi_gpio->cs_gpios), in spi_gpio_probe_pdata()
350 if (!spi_gpio->cs_gpios) in spi_gpio_probe_pdata()
351 return -ENOMEM; in spi_gpio_probe_pdata()
353 for (i = 0; i < master->num_chipselect; i++) { in spi_gpio_probe_pdata()
354 spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", i, in spi_gpio_probe_pdata()
356 if (IS_ERR(spi_gpio->cs_gpios[i])) in spi_gpio_probe_pdata()
357 return PTR_ERR(spi_gpio->cs_gpios[i]); in spi_gpio_probe_pdata()
368 struct device *dev = &pdev->dev; in spi_gpio_probe()
373 return -ENOMEM; in spi_gpio_probe()
375 if (pdev->dev.of_node) in spi_gpio_probe()
389 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32); in spi_gpio_probe()
390 master->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL | in spi_gpio_probe()
392 if (!spi_gpio->mosi) { in spi_gpio_probe()
395 * No setting SPI_MASTER_NO_RX here - if there is only in spi_gpio_probe()
399 master->flags = SPI_MASTER_NO_TX; in spi_gpio_probe()
402 master->bus_num = pdev->id; in spi_gpio_probe()
403 master->setup = spi_gpio_setup; in spi_gpio_probe()
404 master->cleanup = spi_gpio_cleanup; in spi_gpio_probe()
406 bb = &spi_gpio->bitbang; in spi_gpio_probe()
407 bb->master = master; in spi_gpio_probe()
413 master->flags |= SPI_MASTER_GPIO_SS; in spi_gpio_probe()
414 bb->chipselect = spi_gpio_chipselect; in spi_gpio_probe()
415 bb->set_line_direction = spi_gpio_set_direction; in spi_gpio_probe()
417 if (master->flags & SPI_MASTER_NO_TX) { in spi_gpio_probe()
418 bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0; in spi_gpio_probe()
419 bb->txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1; in spi_gpio_probe()
420 bb->txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2; in spi_gpio_probe()
421 bb->txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3; in spi_gpio_probe()
423 bb->txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0; in spi_gpio_probe()
424 bb->txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1; in spi_gpio_probe()
425 bb->txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2; in spi_gpio_probe()
426 bb->txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3; in spi_gpio_probe()
428 bb->setup_transfer = spi_bitbang_setup_transfer; in spi_gpio_probe()
430 status = spi_bitbang_init(&spi_gpio->bitbang); in spi_gpio_probe()
434 return devm_spi_register_master(&pdev->dev, master); in spi_gpio_probe()