Commit bfb9bcdb authored by Michael Buesch's avatar Michael Buesch Committed by Linus Torvalds

spi-gpio: allow operation without CS signal

Change spi-gpio so that it is possible to drive SPI communications over
GPIO without the need for a chipselect signal.

This is useful in very small setups where there's only one slave device
on the bus.

This patch does not affect existing setups.

I use this for a tiny communication channel between an embedded device and
a microcontroller.  There are not enough GPIOs available for chipselect
and it's not needed anyway in this case.
Signed-off-by: default avatarMichael Buesch <mb@bu3sch.de>
Cc: David Brownell <david-b@pacbell.net>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 8a0cecff
...@@ -178,8 +178,10 @@ static void spi_gpio_chipselect(struct spi_device *spi, int is_active) ...@@ -178,8 +178,10 @@ static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
if (is_active) if (is_active)
setsck(spi, spi->mode & SPI_CPOL); setsck(spi, spi->mode & SPI_CPOL);
/* SPI is normally active-low */ if (cs != SPI_GPIO_NO_CHIPSELECT) {
gpio_set_value(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active); /* SPI is normally active-low */
gpio_set_value(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
}
} }
static int spi_gpio_setup(struct spi_device *spi) static int spi_gpio_setup(struct spi_device *spi)
...@@ -191,15 +193,17 @@ static int spi_gpio_setup(struct spi_device *spi) ...@@ -191,15 +193,17 @@ static int spi_gpio_setup(struct spi_device *spi)
return -EINVAL; return -EINVAL;
if (!spi->controller_state) { if (!spi->controller_state) {
status = gpio_request(cs, dev_name(&spi->dev)); if (cs != SPI_GPIO_NO_CHIPSELECT) {
if (status) status = gpio_request(cs, dev_name(&spi->dev));
return status; if (status)
status = gpio_direction_output(cs, spi->mode & SPI_CS_HIGH); return status;
status = gpio_direction_output(cs, spi->mode & SPI_CS_HIGH);
}
} }
if (!status) if (!status)
status = spi_bitbang_setup(spi); status = spi_bitbang_setup(spi);
if (status) { if (status) {
if (!spi->controller_state) if (!spi->controller_state && cs != SPI_GPIO_NO_CHIPSELECT)
gpio_free(cs); gpio_free(cs);
} }
return status; return status;
...@@ -209,7 +213,8 @@ static void spi_gpio_cleanup(struct spi_device *spi) ...@@ -209,7 +213,8 @@ static void spi_gpio_cleanup(struct spi_device *spi)
{ {
unsigned long cs = (unsigned long) spi->controller_data; unsigned long cs = (unsigned long) spi->controller_data;
gpio_free(cs); if (cs != SPI_GPIO_NO_CHIPSELECT)
gpio_free(cs);
spi_bitbang_cleanup(spi); spi_bitbang_cleanup(spi);
} }
......
...@@ -25,10 +25,16 @@ ...@@ -25,10 +25,16 @@
* ... * ...
* }; * };
* *
* If chipselect is not used (there's only one device on the bus), assign
* SPI_GPIO_NO_CHIPSELECT to the controller_data:
* .controller_data = (void *) SPI_GPIO_NO_CHIPSELECT;
*
* If the bitbanged bus is later switched to a "native" controller, * If the bitbanged bus is later switched to a "native" controller,
* that platform_device and controller_data should be removed. * that platform_device and controller_data should be removed.
*/ */
#define SPI_GPIO_NO_CHIPSELECT ((unsigned long)-1l)
/** /**
* struct spi_gpio_platform_data - parameter for bitbanged SPI master * struct spi_gpio_platform_data - parameter for bitbanged SPI master
* @sck: number of the GPIO used for clock output * @sck: number of the GPIO used for clock output
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment