Skip to content

Commit 9086d0f

Browse files
andy-shevbroonie
authored andcommitted
spi: Exctract spi_dev_check_cs() helper
It seems a few functions implement the similar for-loop to validate chip select pins for uniqueness. Let's deduplicate that code in order to have a single place of that for better maintenance. Signed-off-by: Andy Shevchenko <[email protected]> Link: https://msgid.link/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 5ee9160 commit 9086d0f

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

drivers/spi/spi.c

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

611+
static inline int spi_dev_check_cs(struct device *dev,
612+
struct spi_device *spi, u8 idx,
613+
struct spi_device *new_spi, u8 new_idx)
614+
{
615+
u8 cs, cs_new;
616+
u8 idx_new;
617+
618+
cs = spi_get_chipselect(spi, idx);
619+
for (idx_new = new_idx; idx_new < SPI_CS_CNT_MAX; idx_new++) {
620+
cs_new = spi_get_chipselect(new_spi, idx_new);
621+
if (cs != 0xFF && cs_new != 0xFF && cs == cs_new) {
622+
dev_err(dev, "chipselect %u already in use\n", cs_new);
623+
return -EBUSY;
624+
}
625+
}
626+
return 0;
627+
}
628+
611629
static int spi_dev_check(struct device *dev, void *data)
612630
{
613631
struct spi_device *spi = to_spi_device(dev);
614632
struct spi_device *new_spi = data;
615-
int idx, nw_idx;
616-
u8 cs, cs_nw;
633+
int status, idx;
617634

618635
if (spi->controller == new_spi->controller) {
619636
for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
620-
cs = spi_get_chipselect(spi, idx);
621-
for (nw_idx = 0; nw_idx < SPI_CS_CNT_MAX; nw_idx++) {
622-
cs_nw = spi_get_chipselect(new_spi, nw_idx);
623-
if (cs != 0xFF && cs_nw != 0xFF && cs == cs_nw) {
624-
dev_err(dev, "chipselect %d already in use\n", cs_nw);
625-
return -EBUSY;
626-
}
627-
}
637+
status = spi_dev_check_cs(dev, spi, idx, new_spi, 0);
638+
if (status)
639+
return status;
628640
}
629641
}
630642
return 0;
@@ -640,8 +652,8 @@ static int __spi_add_device(struct spi_device *spi)
640652
{
641653
struct spi_controller *ctlr = spi->controller;
642654
struct device *dev = ctlr->dev.parent;
643-
int status, idx, nw_idx;
644-
u8 cs, nw_cs;
655+
int status, idx;
656+
u8 cs;
645657

646658
for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
647659
/* Chipselects are numbered 0..max; validate. */
@@ -658,14 +670,9 @@ static int __spi_add_device(struct spi_device *spi)
658670
* For example, spi->chip_select[0] != spi->chip_select[1] and so on.
659671
*/
660672
for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
661-
cs = spi_get_chipselect(spi, idx);
662-
for (nw_idx = idx + 1; nw_idx < SPI_CS_CNT_MAX; nw_idx++) {
663-
nw_cs = spi_get_chipselect(spi, nw_idx);
664-
if (cs != 0xFF && nw_cs != 0xFF && cs == nw_cs) {
665-
dev_err(dev, "chipselect %d already in use\n", nw_cs);
666-
return -EBUSY;
667-
}
668-
}
673+
status = spi_dev_check_cs(dev, spi, idx, spi, idx + 1);
674+
if (status)
675+
return status;
669676
}
670677

671678
/* Set the bus ID string */

0 commit comments

Comments
 (0)