Re: [PATCH v4 7/9] spi: add KUnit tests for the multi-CS framework
From: Mark Brown
Date: Sat Nov 07 2026 - 10:59:03 EST
> Add a KUnit suite for the multi-CS feature in the SPI core, covering:
> the CS suffix of the device name, considering the contiguous values
> restriction, the last-asserted CS tracking for single and multi-CS devices,
> and CS array validation rules (overflow, out of range, duplicates,
> non-contiguous and descending sequences) and filling of unused slots with
> SPI_INVALID_CS, implemented by a test-local spi_validate_cs_config()
> helper.
> +/*
> + * cs_index_mask only has bit 0 set even though num_chipselect=2:
> + * slot 1 is skipped by spi_for_each_valid_cs, only slot 0 is checked.
> + * last_cs[0] matches.
> + */
> +static void last_cs_index_mask_filters_slots(struct kunit *test)
> +{
> + const u8 cs[] = { 0, 1 };
> + struct spi_device *spi = alloc_spi(test, cs, ARRAY_SIZE(cs), 8);
> +
> + /* override mask: only slot 0 active. */
> + spi->cs_index_mask = BIT(0);
> + spi->controller->last_cs[0] = 0;
> + spi->controller->last_cs[1] = SPI_INVALID_CS;
> +
> + KUNIT_EXPECT_TRUE(test, spi_is_last_cs(spi));
Should this be:
spi->cs_index_mask = BIT(0);
spi->controller->last_cs[0] = SPI_INVALID_CS;
spi->controller->last_cs[1] = 1;
KUNIT_EXPECT_FALSE(test, spi_is_last_cs(spi));
since we're not checking that slot 1 was untouched.
> +/*
> + * Validate and normalise the CS configuration of @spi against the controller's
> + * @num_chipselect limit. Arithmetic Analysis only, no device-model calls or bus
> + * scan.
> + *
> + * On success the unused chip_select[] slots are filled with SPI_INVALID_CS.
> + */
> +static int spi_validate_cs_config(struct spi_device *spi,
> + u16 ctlr_num_chipselect)
> +{
> + int idx;
Should we expose the relevant core code with a VISIBLE_IF_KUNIT helper
instead of duplicating it?
--
Mark Brown <broonie@xxxxxxxxxx>