Skip to content

Commit 4a07b8b

Browse files
Linus Walleijbroonie
authored andcommitted
spi: bitbang: Make chipselect callback optional
The ->chipselect() callback on the bit-banged SPI library master is optional if using GPIO descriptors: when using descriptors exclusively without any native chipselects, the core does not even call out the the native ->set_cs() and therefore ->chipselect() on a bit-banged SPI master will not even be called in this case. Make sure to respect the SPI_MASTER_GPIO_SS as used by e.g. spi-gpio.c though: this setting will make the core handle the chip select using GPIO descriptors *AND* call the local chipselect handler. Signed-off-by: Linus Walleij <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 8a6553e commit 4a07b8b

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

drivers/spi/spi-bitbang.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,20 @@ static void spi_bitbang_set_cs(struct spi_device *spi, bool enable)
329329
int spi_bitbang_init(struct spi_bitbang *bitbang)
330330
{
331331
struct spi_master *master = bitbang->master;
332+
bool custom_cs;
332333

333-
if (!master || !bitbang->chipselect)
334+
if (!master)
335+
return -EINVAL;
336+
/*
337+
* We only need the chipselect callback if we are actually using it.
338+
* If we just use GPIO descriptors, it is surplus. If the
339+
* SPI_MASTER_GPIO_SS flag is set, we always need to call the
340+
* driver-specific chipselect routine.
341+
*/
342+
custom_cs = (!master->use_gpio_descriptors ||
343+
(master->flags & SPI_MASTER_GPIO_SS));
344+
345+
if (custom_cs && !bitbang->chipselect)
334346
return -EINVAL;
335347

336348
mutex_init(&bitbang->lock);
@@ -344,7 +356,12 @@ int spi_bitbang_init(struct spi_bitbang *bitbang)
344356
master->prepare_transfer_hardware = spi_bitbang_prepare_hardware;
345357
master->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;
346358
master->transfer_one = spi_bitbang_transfer_one;
347-
master->set_cs = spi_bitbang_set_cs;
359+
/*
360+
* When using GPIO descriptors, the ->set_cs() callback doesn't even
361+
* get called unless SPI_MASTER_GPIO_SS is set.
362+
*/
363+
if (custom_cs)
364+
master->set_cs = spi_bitbang_set_cs;
348365

349366
if (!bitbang->txrx_bufs) {
350367
bitbang->use_dma = 0;

0 commit comments

Comments
 (0)