[PATCH v4 4/9] spi: restrict CS logical values to a contiguous sequence
From: Jonathan Santos
Date: Tue Oct 20 2026 - 21:59:00 EST
When a device uses more than one chip select, require the logical CS
numbers to be consecutive (for example 2, 3, 4). If they aren't,
__spi_add_device() now fails with -EINVAL and logs which CS breaks
the sequence.
Suggested-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxx>
Signed-off-by: Jonathan Santos <Jonathan.Santos@xxxxxxxxxx>
---
Changes in v4:
* New patch, suggested by Andy. I assume this might have impacts on
existing users. Should we make something else to complement this?
---
drivers/spi/spi.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 7a0406ec200e..2b2eeaa4c15d 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -902,6 +902,16 @@ static int __spi_add_device(struct spi_device *spi, struct spi_device *parent)
for (idx = spi->num_chipselect; idx < SPI_DEVICE_CS_CNT_MAX; idx++)
spi_set_chipselect(spi, idx, SPI_INVALID_CS);
+ /* Restrict CS logical values to a contiguous sequence */
+ for (idx = 1; idx < spi->num_chipselect; idx++) {
+ if (spi_get_chipselect(spi, idx) != spi_get_chipselect(spi, idx - 1) + 1) {
+ dev_err(dev, "cs%d is not consecutive with cs%d\n",
+ spi_get_chipselect(spi, idx),
+ spi_get_chipselect(spi, idx - 1));
+ return -EINVAL;
+ }
+ }
+
/* Set the bus ID string */
spi_dev_set_name(spi);
--
2.34.1