Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

spi: spi-gpio: add SPI_3WIRE support

Add SPI_3WIRE support to spi-gpio controller introducing
set_line_direction function pointer in spi_bitbang data structure.
Spi-gpio controller has been tested using hts221 temp/rh iio sensor
running in 3wire mode and lsm6dsm running in 4wire mode

Signed-off-by: Lorenzo Bianconi <lorenzo.bianconi@redhat.com>
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Lorenzo Bianconi and committed by
Mark Brown
4b859db2 304d3436

+33 -1
+16
drivers/spi/spi-bitbang.c
··· 243 243 { 244 244 struct spi_bitbang_cs *cs = spi->controller_state; 245 245 unsigned nsecs = cs->nsecs; 246 + struct spi_bitbang *bitbang; 246 247 248 + bitbang = spi_master_get_devdata(spi->master); 249 + if (bitbang->set_line_direction) { 250 + int err; 251 + 252 + err = bitbang->set_line_direction(spi, !!(t->tx_buf)); 253 + if (err < 0) 254 + return err; 255 + } 256 + 257 + if (spi->mode & SPI_3WIRE) { 258 + unsigned flags; 259 + 260 + flags = t->tx_buf ? SPI_MASTER_NO_RX : SPI_MASTER_NO_TX; 261 + return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, flags); 262 + } 247 263 return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, 0); 248 264 } 249 265
+16 -1
drivers/spi/spi-gpio.c
··· 121 121 { 122 122 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 123 123 124 - return !!gpiod_get_value_cansleep(spi_gpio->miso); 124 + if (spi->mode & SPI_3WIRE) 125 + return !!gpiod_get_value_cansleep(spi_gpio->mosi); 126 + else 127 + return !!gpiod_get_value_cansleep(spi_gpio->miso); 125 128 } 126 129 127 130 /* ··· 251 248 status = spi_bitbang_setup(spi); 252 249 253 250 return status; 251 + } 252 + 253 + static int spi_gpio_set_direction(struct spi_device *spi, bool output) 254 + { 255 + struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 256 + 257 + if (output) 258 + return gpiod_direction_output(spi_gpio->mosi, 1); 259 + else 260 + return gpiod_direction_input(spi_gpio->mosi); 254 261 } 255 262 256 263 static void spi_gpio_cleanup(struct spi_device *spi) ··· 408 395 return status; 409 396 410 397 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32); 398 + master->mode_bits = SPI_3WIRE | SPI_CPHA | SPI_CPOL; 411 399 master->flags = master_flags; 412 400 master->bus_num = pdev->id; 413 401 /* The master needs to think there is a chipselect even if not connected */ ··· 421 407 422 408 spi_gpio->bitbang.master = master; 423 409 spi_gpio->bitbang.chipselect = spi_gpio_chipselect; 410 + spi_gpio->bitbang.set_line_direction = spi_gpio_set_direction; 424 411 425 412 if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) { 426 413 spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
+1
include/linux/spi/spi_bitbang.h
··· 31 31 u32 (*txrx_word[4])(struct spi_device *spi, 32 32 unsigned nsecs, 33 33 u32 word, u8 bits, unsigned flags); 34 + int (*set_line_direction)(struct spi_device *spi, bool output); 34 35 }; 35 36 36 37 /* you can call these default bitbang->master methods from your custom