@@ -608,23 +608,35 @@ static void spi_dev_set_name(struct spi_device *spi)
spi_get_chipselect(spi, 0));
}
+static inline int spi_dev_check_cs(struct device *dev,
+ struct spi_device *spi, u8 idx,
+ struct spi_device *new_spi, u8 new_idx)
+{
+ u8 cs, cs_new;
+ u8 idx_new;
+
+ cs = spi_get_chipselect(spi, idx);
+ for (idx_new = new_idx; idx_new < SPI_CS_CNT_MAX; idx_new++) {
+ cs_new = spi_get_chipselect(new_spi, idx_new);
+ if (cs != 0xFF && cs_new != 0xFF && cs == cs_new) {
+ dev_err(dev, "chipselect %u already in use\n", cs_new);
+ return -EBUSY;
+ }
+ }
+ return 0;
+}
+
static int spi_dev_check(struct device *dev, void *data)
{
struct spi_device *spi = to_spi_device(dev);
struct spi_device *new_spi = data;
- int idx, nw_idx;
- u8 cs, cs_nw;
+ int status, idx;
if (spi->controller == new_spi->controller) {
for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
- cs = spi_get_chipselect(spi, idx);
- for (nw_idx = 0; nw_idx < SPI_CS_CNT_MAX; nw_idx++) {
- cs_nw = spi_get_chipselect(new_spi, nw_idx);
- if (cs != 0xFF && cs_nw != 0xFF && cs == cs_nw) {
- dev_err(dev, "chipselect %d already in use\n", cs_nw);
- return -EBUSY;
- }
- }
+ status = spi_dev_check_cs(dev, spi, idx, new_spi, 0);
+ if (status)
+ return status;
}
}
return 0;
@@ -640,8 +652,8 @@ static int __spi_add_device(struct spi_device *spi)
{
struct spi_controller *ctlr = spi->controller;
struct device *dev = ctlr->dev.parent;
- int status, idx, nw_idx;
- u8 cs, nw_cs;
+ int status, idx;
+ u8 cs;
for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
/* Chipselects are numbered 0..max; validate. */
@@ -658,14 +670,9 @@ static int __spi_add_device(struct spi_device *spi)
* For example, spi->chip_select[0] != spi->chip_select[1] and so on.
*/
for (idx = 0; idx < SPI_CS_CNT_MAX; idx++) {
- cs = spi_get_chipselect(spi, idx);
- for (nw_idx = idx + 1; nw_idx < SPI_CS_CNT_MAX; nw_idx++) {
- nw_cs = spi_get_chipselect(spi, nw_idx);
- if (cs != 0xFF && nw_cs != 0xFF && cs == nw_cs) {
- dev_err(dev, "chipselect %d already in use\n", nw_cs);
- return -EBUSY;
- }
- }
+ status = spi_dev_check_cs(dev, spi, idx, spi, idx + 1);
+ if (status)
+ return status;
}
/* Set the bus ID string */
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 <andriy.shevchenko@linux.intel.com> --- drivers/spi/spi.c | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-)