Skip to content

Commit 5ee9160

Browse files
andy-shevbroonie
authored andcommitted
spi: Exctract spi_set_all_cs_unused() helper
It seems a few functions implement the similar for-loop to mark all chip select pins unused. 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 7ad2882 commit 5ee9160

File tree

1 file changed

+24
-50
lines changed

1 file changed

+24
-50
lines changed

drivers/spi/spi.c

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,23 @@ int spi_add_device(struct spi_device *spi)
745745
}
746746
EXPORT_SYMBOL_GPL(spi_add_device);
747747

748+
static void spi_set_all_cs_unused(struct spi_device *spi)
749+
{
750+
u8 idx;
751+
752+
/*
753+
* Zero(0) is a valid physical CS value and can be located at any
754+
* logical CS in the spi->chip_select[]. If all the physical CS
755+
* are initialized to 0 then It would be difficult to differentiate
756+
* between a valid physical CS 0 & an unused logical CS whose physical
757+
* CS can be 0. As a solution to this issue initialize all the CS to 0xFF.
758+
* Now all the unused logical CS will have 0xFF physical CS value & can be
759+
* ignore while performing physical CS validity checks.
760+
*/
761+
for (idx = 0; idx < SPI_CS_CNT_MAX; idx++)
762+
spi_set_chipselect(spi, idx, 0xFF);
763+
}
764+
748765
/**
749766
* spi_new_device - instantiate one new SPI device
750767
* @ctlr: Controller to which device is connected
@@ -764,7 +781,6 @@ struct spi_device *spi_new_device(struct spi_controller *ctlr,
764781
{
765782
struct spi_device *proxy;
766783
int status;
767-
u8 idx;
768784

769785
/*
770786
* NOTE: caller did any chip->bus_num checks necessary.
@@ -780,19 +796,10 @@ struct spi_device *spi_new_device(struct spi_controller *ctlr,
780796

781797
WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
782798

783-
/*
784-
* Zero(0) is a valid physical CS value and can be located at any
785-
* logical CS in the spi->chip_select[]. If all the physical CS
786-
* are initialized to 0 then It would be difficult to differentiate
787-
* between a valid physical CS 0 & an unused logical CS whose physical
788-
* CS can be 0. As a solution to this issue initialize all the CS to 0xFF.
789-
* Now all the unused logical CS will have 0xFF physical CS value & can be
790-
* ignore while performing physical CS validity checks.
791-
*/
792-
for (idx = 0; idx < SPI_CS_CNT_MAX; idx++)
793-
spi_set_chipselect(proxy, idx, 0xFF);
794-
799+
/* Use provided chip-select for proxy device */
800+
spi_set_all_cs_unused(proxy);
795801
spi_set_chipselect(proxy, 0, chip->chip_select);
802+
796803
proxy->max_speed_hz = chip->max_speed_hz;
797804
proxy->mode = chip->mode;
798805
proxy->irq = chip->irq;
@@ -2418,17 +2425,7 @@ static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
24182425
return -EINVAL;
24192426
}
24202427

2421-
/*
2422-
* Zero(0) is a valid physical CS value and can be located at any
2423-
* logical CS in the spi->chip_select[]. If all the physical CS
2424-
* are initialized to 0 then It would be difficult to differentiate
2425-
* between a valid physical CS 0 & an unused logical CS whose physical
2426-
* CS can be 0. As a solution to this issue initialize all the CS to 0xFF.
2427-
* Now all the unused logical CS will have 0xFF physical CS value & can be
2428-
* ignore while performing physical CS validity checks.
2429-
*/
2430-
for (idx = 0; idx < SPI_CS_CNT_MAX; idx++)
2431-
spi_set_chipselect(spi, idx, 0xFF);
2428+
spi_set_all_cs_unused(spi);
24322429

24332430
/* Device address */
24342431
rc = of_property_read_variable_u32_array(nc, "reg", &cs[0], 1,
@@ -2565,7 +2562,6 @@ struct spi_device *spi_new_ancillary_device(struct spi_device *spi,
25652562
struct spi_controller *ctlr = spi->controller;
25662563
struct spi_device *ancillary;
25672564
int rc = 0;
2568-
u8 idx;
25692565

25702566
/* Alloc an spi_device */
25712567
ancillary = spi_alloc_device(ctlr);
@@ -2576,19 +2572,8 @@ struct spi_device *spi_new_ancillary_device(struct spi_device *spi,
25762572

25772573
strscpy(ancillary->modalias, "dummy", sizeof(ancillary->modalias));
25782574

2579-
/*
2580-
* Zero(0) is a valid physical CS value and can be located at any
2581-
* logical CS in the spi->chip_select[]. If all the physical CS
2582-
* are initialized to 0 then It would be difficult to differentiate
2583-
* between a valid physical CS 0 & an unused logical CS whose physical
2584-
* CS can be 0. As a solution to this issue initialize all the CS to 0xFF.
2585-
* Now all the unused logical CS will have 0xFF physical CS value & can be
2586-
* ignore while performing physical CS validity checks.
2587-
*/
2588-
for (idx = 0; idx < SPI_CS_CNT_MAX; idx++)
2589-
spi_set_chipselect(ancillary, idx, 0xFF);
2590-
25912575
/* Use provided chip-select for ancillary device */
2576+
spi_set_all_cs_unused(ancillary);
25922577
spi_set_chipselect(ancillary, 0, chip_select);
25932578

25942579
/* Take over SPI mode/speed from SPI main device */
@@ -2805,7 +2790,6 @@ struct spi_device *acpi_spi_device_alloc(struct spi_controller *ctlr,
28052790
struct acpi_spi_lookup lookup = {};
28062791
struct spi_device *spi;
28072792
int ret;
2808-
u8 idx;
28092793

28102794
if (!ctlr && index == -1)
28112795
return ERR_PTR(-EINVAL);
@@ -2841,24 +2825,14 @@ struct spi_device *acpi_spi_device_alloc(struct spi_controller *ctlr,
28412825
return ERR_PTR(-ENOMEM);
28422826
}
28432827

2844-
/*
2845-
* Zero(0) is a valid physical CS value and can be located at any
2846-
* logical CS in the spi->chip_select[]. If all the physical CS
2847-
* are initialized to 0 then It would be difficult to differentiate
2848-
* between a valid physical CS 0 & an unused logical CS whose physical
2849-
* CS can be 0. As a solution to this issue initialize all the CS to 0xFF.
2850-
* Now all the unused logical CS will have 0xFF physical CS value & can be
2851-
* ignore while performing physical CS validity checks.
2852-
*/
2853-
for (idx = 0; idx < SPI_CS_CNT_MAX; idx++)
2854-
spi_set_chipselect(spi, idx, 0xFF);
2828+
spi_set_all_cs_unused(spi);
2829+
spi_set_chipselect(spi, 0, lookup.chip_select);
28552830

28562831
ACPI_COMPANION_SET(&spi->dev, adev);
28572832
spi->max_speed_hz = lookup.max_speed_hz;
28582833
spi->mode |= lookup.mode;
28592834
spi->irq = lookup.irq;
28602835
spi->bits_per_word = lookup.bits_per_word;
2861-
spi_set_chipselect(spi, 0, lookup.chip_select);
28622836
/*
28632837
* spi->chip_select[i] gives the corresponding physical CS for logical CS i
28642838
* logical CS number is represented by setting the ith bit in spi->cs_index_mask

0 commit comments

Comments
 (0)