diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index af126053213f..2350463bfb8f 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -2630,7 +2630,7 @@ static int spi_get_gpio_descs(struct spi_controller *ctlr) native_cs_mask |= BIT(i); } - ctlr->unused_native_cs = ffz(native_cs_mask); + ctlr->unused_native_cs = ffs(~native_cs_mask) - 1; if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios && ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) {