Skip to content

Commit be84be4

Browse files
andy-shevbroonie
authored andcommitted
spi: Introduce SPI_INVALID_CS and is_valid_cs()
The SPI core inconsistently uses the marker value for unused chip select pin. Define a constant (with appropriate type) and introduce is_valid_cs() helper function to avoid spreading this inconsistency in the future. Signed-off-by: Andy Shevchenko <[email protected]> Link: https://msgid.link/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 14fe5a9 commit be84be4

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

drivers/spi/spi.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,22 @@ static void spi_dev_set_name(struct spi_device *spi)
608608
spi_get_chipselect(spi, 0));
609609
}
610610

611+
/*
612+
* Zero(0) is a valid physical CS value and can be located at any
613+
* logical CS in the spi->chip_select[]. If all the physical CS
614+
* are initialized to 0 then It would be difficult to differentiate
615+
* between a valid physical CS 0 & an unused logical CS whose physical
616+
* CS can be 0. As a solution to this issue initialize all the CS to -1.
617+
* Now all the unused logical CS will have -1 physical CS value & can be
618+
* ignored while performing physical CS validity checks.
619+
*/
620+
#define SPI_INVALID_CS ((s8)-1)
621+
622+
static inline bool is_valid_cs(s8 chip_select)
623+
{
624+
return chip_select != SPI_INVALID_CS;
625+
}
626+
611627
static inline int spi_dev_check_cs(struct device *dev,
612628
struct spi_device *spi, u8 idx,
613629
struct spi_device *new_spi, u8 new_idx)
@@ -618,7 +634,7 @@ static inline int spi_dev_check_cs(struct device *dev,
618634
cs = spi_get_chipselect(spi, idx);
619635
for (idx_new = new_idx; idx_new < SPI_CS_CNT_MAX; idx_new++) {
620636
cs_new = spi_get_chipselect(new_spi, idx_new);
621-
if (cs != 0xFF && cs_new != 0xFF && cs == cs_new) {
637+
if (is_valid_cs(cs) && is_valid_cs(cs_new) && cs == cs_new) {
622638
dev_err(dev, "chipselect %u already in use\n", cs_new);
623639
return -EBUSY;
624640
}
@@ -658,7 +674,7 @@ static int __spi_add_device(struct spi_device *spi)
658674
for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
659675
/* Chipselects are numbered 0..max; validate. */
660676
cs = spi_get_chipselect(spi, idx);
661-
if (cs != 0xFF && cs >= ctlr->num_chipselect) {
677+
if (is_valid_cs(cs) && cs >= ctlr->num_chipselect) {
662678
dev_err(dev, "cs%d >= max %d\n", spi_get_chipselect(spi, idx),
663679
ctlr->num_chipselect);
664680
return -EINVAL;
@@ -698,7 +714,7 @@ static int __spi_add_device(struct spi_device *spi)
698714

699715
for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
700716
cs = spi_get_chipselect(spi, idx);
701-
if (cs != 0xFF)
717+
if (is_valid_cs(cs))
702718
spi_set_csgpiod(spi, idx, ctlr->cs_gpiods[cs]);
703719
}
704720
}
@@ -756,17 +772,8 @@ static void spi_set_all_cs_unused(struct spi_device *spi)
756772
{
757773
u8 idx;
758774

759-
/*
760-
* Zero(0) is a valid physical CS value and can be located at any
761-
* logical CS in the spi->chip_select[]. If all the physical CS
762-
* are initialized to 0 then It would be difficult to differentiate
763-
* between a valid physical CS 0 & an unused logical CS whose physical
764-
* CS can be 0. As a solution to this issue initialize all the CS to 0xFF.
765-
* Now all the unused logical CS will have 0xFF physical CS value & can be
766-
* ignore while performing physical CS validity checks.
767-
*/
768775
for (idx = 0; idx < SPI_CS_CNT_MAX; idx++)
769-
spi_set_chipselect(spi, idx, 0xFF);
776+
spi_set_chipselect(spi, idx, SPI_INVALID_CS);
770777
}
771778

772779
/**
@@ -1050,7 +1057,7 @@ static void spi_set_cs(struct spi_device *spi, bool enable, bool force)
10501057

10511058
spi->controller->last_cs_index_mask = spi->cs_index_mask;
10521059
for (idx = 0; idx < SPI_CS_CNT_MAX; idx++)
1053-
spi->controller->last_cs[idx] = enable ? spi_get_chipselect(spi, 0) : -1;
1060+
spi->controller->last_cs[idx] = enable ? spi_get_chipselect(spi, 0) : SPI_INVALID_CS;
10541061
spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH;
10551062

10561063
if (spi->mode & SPI_CS_HIGH)
@@ -3333,9 +3340,9 @@ int spi_register_controller(struct spi_controller *ctlr)
33333340
goto free_bus_id;
33343341
}
33353342

3336-
/* Setting last_cs to -1 means no chip selected */
3343+
/* Setting last_cs to SPI_INVALID_CS means no chip selected */
33373344
for (idx = 0; idx < SPI_CS_CNT_MAX; idx++)
3338-
ctlr->last_cs[idx] = -1;
3345+
ctlr->last_cs[idx] = SPI_INVALID_CS;
33393346

33403347
status = device_add(&ctlr->dev);
33413348
if (status < 0)

0 commit comments

Comments
 (0)