Skip to contentSkip to conversation

staging: greybus: spilib: Handle SPI device types with a switch statement

PATCH Superseded [email protected] 3 participants 8 messages Aug 5 – Sep 1

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: greybus: spilib: Handle SPI device types with a switch statement

From: Supreet <[email protected]>

Replaced the if/else chain with switch statement. makes it easier to extend support for additional device types.

Remove the temporary spidev variable since it is only used to test the return value of spi_new_device(). Call spi_new_device() directly in the conditional instead.

checkpatch was run, had no warnings and errors.

---
 drivers/staging/greybus/spilib.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
drivers/staging/greybus/spilib.c +11 -8
index e4d1ae8308aa..53fc0c124754 100644
@@ -444,7 +444,6 @@ static int gb_spi_setup_device(struct gb_spilib *spi, u8 cs)
 	struct gb_spi_device_config_request request;
 	struct gb_spi_device_config_response response;
 	struct spi_board_info spi_board = { {0} };
-	struct spi_device *spidev;
 	int ret;
 	u8 dev_type;
 
@@ -458,25 +457,29 @@ static int gb_spi_setup_device(struct gb_spilib *spi, u8 cs)
 
 	dev_type = response.device_type;
 
-	if (dev_type == GB_SPI_SPI_DEV)
+	switch (dev_type) {
+	case GB_SPI_SPI_DEV:
 		strscpy(spi_board.modalias, "spidev",
 			sizeof(spi_board.modalias));
-	else if (dev_type == GB_SPI_SPI_NOR)
+		break;
+	case GB_SPI_SPI_NOR:
 		strscpy(spi_board.modalias, "spi-nor",
 			sizeof(spi_board.modalias));
-	else if (dev_type == GB_SPI_SPI_MODALIAS)
+		break;
+	case GB_SPI_SPI_MODALIAS:
 		memcpy(spi_board.modalias, response.name,
-		       sizeof(spi_board.modalias));
-	else
+		sizeof(spi_board.modalias));
+		break;
+	default:
 		return -EINVAL;
+	}
 
 	spi_board.mode		= le16_to_cpu(response.mode);
 	spi_board.bus_num	= ctlr->bus_num;
 	spi_board.chip_select	= cs;
 	spi_board.max_speed_hz	= le32_to_cpu(response.max_speed_hz);
 
-	spidev = spi_new_device(ctlr, &spi_board);
-	if (!spidev)
+	if (!spi_new_device(ctlr, &spi_board))
 		return -EINVAL;
 
 	return 0;
Signature · 1 line
2.55.0
#1 lore raw
Quoted 1 line · On Wed, Aug 05, 2026 at 04:49:57PM +0530, [email protected] wrote:

From: Supreet <[email protected]>

We need a full name please.

Quoted 51 lines

Replaced the if/else chain with switch statement. makes it easier to extend support for additional device types.

Remove the temporary spidev variable since it is only used to test the return value of spi_new_device(). Call spi_new_device() directly in the conditional instead.

checkpatch was run, had no warnings and errors.

---
 drivers/staging/greybus/spilib.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
drivers/staging/greybus/spilib.c +9 -6
index e4d1ae8308aa..53fc0c124754 100644
@@ -444,7 +444,6 @@ static int gb_spi_setup_device(struct gb_spilib *spi, u8 cs)
 	struct gb_spi_device_config_request request;
 	struct gb_spi_device_config_response response;
 	struct spi_board_info spi_board = { {0} };
-	struct spi_device *spidev;
 	int ret;
 	u8 dev_type;
 
@@ -458,25 +457,29 @@ static int gb_spi_setup_device(struct gb_spilib *spi, u8 cs)
 
 	dev_type = response.device_type;
 
-	if (dev_type == GB_SPI_SPI_DEV)
+	switch (dev_type) {
+	case GB_SPI_SPI_DEV:
 		strscpy(spi_board.modalias, "spidev",
 			sizeof(spi_board.modalias));
-	else if (dev_type == GB_SPI_SPI_NOR)
+		break;
+	case GB_SPI_SPI_NOR:
 		strscpy(spi_board.modalias, "spi-nor",
 			sizeof(spi_board.modalias));
-	else if (dev_type == GB_SPI_SPI_MODALIAS)
+		break;
+	case GB_SPI_SPI_MODALIAS:
 		memcpy(spi_board.modalias, response.name,
-		       sizeof(spi_board.modalias));
-	else
+		sizeof(spi_board.modalias));
+		break;
+	default:
 		return -EINVAL;

drivers/staging/greybus/spilib.c static int gb_spi_setup_device(struct gb_spilib *spi, u8 cs) · line 482

Quoted 10 lines
drivers/staging/greybus/spilib.c +2 -2
+	}
 
 	spi_board.mode		= le16_to_cpu(response.mode);
 	spi_board.bus_num	= ctlr->bus_num;
 	spi_board.chip_select	= cs;
 	spi_board.max_speed_hz	= le32_to_cpu(response.max_speed_hz);
 
-	spidev = spi_new_device(ctlr, &spi_board);
-	if (!spidev)
+	if (!spi_new_device(ctlr, &spi_board))

Why was this change made? It's a different one than the switch statement, right?

thanks,

greg k-h

#2 lore raw
Quoted 71 lines · Greg Kroah-Hartman
Quoted 1 line · On Wed, Aug 05, 2026 at 04:49:57PM +0530, [email protected] wrote:

From: Supreet <[email protected]>

We need a full name please.

Quoted 51 lines

Replaced the if/else chain with switch statement. makes it easier to extend support for additional device types.

Remove the temporary spidev variable since it is only used to test the return value of spi_new_device(). Call spi_new_device() directly in the conditional instead.

checkpatch was run, had no warnings and errors.

---
 drivers/staging/greybus/spilib.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
drivers/staging/greybus/spilib.c +9 -6
index e4d1ae8308aa..53fc0c124754 100644
@@ -444,7 +444,6 @@ static int gb_spi_setup_device(struct gb_spilib *spi, u8 cs)
 	struct gb_spi_device_config_request request;
 	struct gb_spi_device_config_response response;
 	struct spi_board_info spi_board = { {0} };
-	struct spi_device *spidev;
 	int ret;
 	u8 dev_type;
 
@@ -458,25 +457,29 @@ static int gb_spi_setup_device(struct gb_spilib *spi, u8 cs)
 
 	dev_type = response.device_type;
 
-	if (dev_type == GB_SPI_SPI_DEV)
+	switch (dev_type) {
+	case GB_SPI_SPI_DEV:
 		strscpy(spi_board.modalias, "spidev",
 			sizeof(spi_board.modalias));
-	else if (dev_type == GB_SPI_SPI_NOR)
+		break;
+	case GB_SPI_SPI_NOR:
 		strscpy(spi_board.modalias, "spi-nor",
 			sizeof(spi_board.modalias));
-	else if (dev_type == GB_SPI_SPI_MODALIAS)
+		break;
+	case GB_SPI_SPI_MODALIAS:
 		memcpy(spi_board.modalias, response.name,
-		       sizeof(spi_board.modalias));
-	else
+		sizeof(spi_board.modalias));
+		break;
+	default:
 		return -EINVAL;

drivers/staging/greybus/spilib.c static int gb_spi_setup_device(struct gb_spilib *spi, u8 cs) · line 482

Quoted 10 lines
drivers/staging/greybus/spilib.c +2 -2
+	}
 
 	spi_board.mode		= le16_to_cpu(response.mode);
 	spi_board.bus_num	= ctlr->bus_num;
 	spi_board.chip_select	= cs;
 	spi_board.max_speed_hz	= le32_to_cpu(response.max_speed_hz);
 
-	spidev = spi_new_device(ctlr, &spi_board);
-	if (!spidev)
+	if (!spi_new_device(ctlr, &spi_board))

Why was this change made? It's a different one than the switch statement, right?

Quoted 3 lines

thanks,

greg k-h

Hi Greg

Yes, you are right, the spidev change is independent of the switch statement. My intention was to remove the temporary variable as it was only used to check the return value of the spi_new_device().

I will drop that change, and send a v2 focused only on the switch statement. If appropriate I can send that cleanup as a separate patch.

Apologies for the last email, first time using mutt.

Thanks
Supreet Singh

#3 lore raw

PATCH v2 1/1 staging: greybus: spilib: Handle SPI device types with a switch statement

In reply to [email protected]

From: Supreet Singh <[email protected]>

Replace the if/else chain that selects the SPI device type with a switch statement. This makes the code easier to extend when additional device types are introduced and improves readability.

---
Changes in v2:
- Restore the temporary variable spidev
- Use the contributor's full name in the From and Signed-off-by lines.

 drivers/staging/greybus/spilib.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/staging/greybus/spilib.c +10 -5
index e4d1ae8308aa..d1b7ca9c4ee3 100644
@@ -458,17 +458,22 @@ static int gb_spi_setup_device(struct gb_spilib *spi, u8 cs)
 
 	dev_type = response.device_type;
 
-	if (dev_type == GB_SPI_SPI_DEV)
+	switch (dev_type) {
+	case GB_SPI_SPI_DEV:
 		strscpy(spi_board.modalias, "spidev",
 			sizeof(spi_board.modalias));
-	else if (dev_type == GB_SPI_SPI_NOR)
+		break;
+	case GB_SPI_SPI_NOR:
 		strscpy(spi_board.modalias, "spi-nor",
 			sizeof(spi_board.modalias));
-	else if (dev_type == GB_SPI_SPI_MODALIAS)
+		break;
+	case GB_SPI_SPI_MODALIAS:
 		memcpy(spi_board.modalias, response.name,
-		       sizeof(spi_board.modalias));
-	else
+		sizeof(spi_board.modalias));
+		break;
+	default:
 		return -EINVAL;
+	}
 
 	spi_board.mode		= le16_to_cpu(response.mode);
 	spi_board.bus_num	= ctlr->bus_num;
Signature · 1 line
2.55.0
#4 lore raw
Quoted 7 lines · On Thu, Aug 06, 2026 at 03:41:18PM +0530, [email protected] wrote:

From: Supreet Singh <[email protected]>

Replace the if/else chain that selects the SPI device type with a switch statement. This makes the code easier to extend when additional device types are introduced and improves readability.

Delete the extra blank line.

Quoted 24 lines

---
Changes in v2:
- Restore the temporary variable spidev
- Use the contributor's full name in the From and Signed-off-by lines.

 drivers/staging/greybus/spilib.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/staging/greybus/spilib.c +3 -2
index e4d1ae8308aa..d1b7ca9c4ee3 100644
@@ -458,17 +458,22 @@ static int gb_spi_setup_device(struct gb_spilib *spi, u8 cs)
 
 	dev_type = response.device_type;
 
-	if (dev_type == GB_SPI_SPI_DEV)
+	switch (dev_type) {
+	case GB_SPI_SPI_DEV:
 		strscpy(spi_board.modalias, "spidev",
 			sizeof(spi_board.modalias));
-	else if (dev_type == GB_SPI_SPI_NOR)
+		break;

drivers/staging/greybus/spilib.c static int gb_spi_setup_device(struct gb_spilib *spi, u8 cs) · line 472

Quoted 10 lines
drivers/staging/greybus/spilib.c +4 -3
+	case GB_SPI_SPI_NOR:
 		strscpy(spi_board.modalias, "spi-nor",
 			sizeof(spi_board.modalias));
-	else if (dev_type == GB_SPI_SPI_MODALIAS)
+		break;
+	case GB_SPI_SPI_MODALIAS:
 		memcpy(spi_board.modalias, response.name,
-		       sizeof(spi_board.modalias));
-	else
+		sizeof(spi_board.modalias));

This line is indented badly now.

regards,
dan carpenter

Quoted 4 lines
drivers/staging/greybus/spilib.c +3 -0
+		break;
+	default:
 		return -EINVAL;
+	}
#5 lore raw

PATCH v3 1/1 staging: greybus: spilib: Handle SPI device types with a switch statement

In reply to [email protected]

From: Supreet Singh <[email protected]>

Replace the if/else chain that selects the SPI device type with a switch statement. This makes the code easier to extend when additional device types are introduced and improves readability.

---
Changes in v3:
- Fixed the memcpy indentation issue
- Removed the extra blank line in the commit message

 drivers/staging/greybus/spilib.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/staging/greybus/spilib.c +9 -4
index e4d1ae8308aa..39d5bf234c61 100644
@@ -458,17 +458,22 @@ static int gb_spi_setup_device(struct gb_spilib *spi, u8 cs)
 
 	dev_type = response.device_type;
 
-	if (dev_type == GB_SPI_SPI_DEV)
+	switch (dev_type) {
+	case GB_SPI_SPI_DEV:
 		strscpy(spi_board.modalias, "spidev",
 			sizeof(spi_board.modalias));
-	else if (dev_type == GB_SPI_SPI_NOR)
+		break;
+	case GB_SPI_SPI_NOR:
 		strscpy(spi_board.modalias, "spi-nor",
 			sizeof(spi_board.modalias));
-	else if (dev_type == GB_SPI_SPI_MODALIAS)
+		break;
+	case GB_SPI_SPI_MODALIAS:
 		memcpy(spi_board.modalias, response.name,
 		       sizeof(spi_board.modalias));
-	else
+		break;
+	default:
 		return -EINVAL;
+	}
 
 	spi_board.mode		= le16_to_cpu(response.mode);
 	spi_board.bus_num	= ctlr->bus_num;
Signature · 1 line
2.55.0
#6 lore raw
Quoted 5 lines · On Fri, Aug 07, 2026 at 12:45:42PM +0530, [email protected] wrote:

From: Supreet Singh <[email protected]>

Replace the if/else chain that selects the SPI device type with a switch statement. This makes the code easier to extend when additional device types are introduced and improves readability.

But let's deal with that when it happens, you are adding more code here than removing:

Quoted 9 lines

---
Changes in v3:
- Fixed the memcpy indentation issue
- Removed the extra blank line in the commit message

 drivers/staging/greybus/spilib.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

So it's not really a benefit overall, right?

Quoted 19 lines
drivers/staging/greybus/spilib.c +4 -2
index e4d1ae8308aa..39d5bf234c61 100644
@@ -458,17 +458,22 @@ static int gb_spi_setup_device(struct gb_spilib *spi, u8 cs)
 
 	dev_type = response.device_type;
 
-	if (dev_type == GB_SPI_SPI_DEV)
+	switch (dev_type) {
+	case GB_SPI_SPI_DEV:
 		strscpy(spi_board.modalias, "spidev",
 			sizeof(spi_board.modalias));
-	else if (dev_type == GB_SPI_SPI_NOR)
+		break;
+	case GB_SPI_SPI_NOR:
 		strscpy(spi_board.modalias, "spi-nor",
 			sizeof(spi_board.modalias));

drivers/staging/greybus/spilib.c static int gb_spi_setup_device(struct gb_spilib *spi, u8 cs) · line 476

Quoted 10 lines
drivers/staging/greybus/spilib.c +5 -2
-	else if (dev_type == GB_SPI_SPI_MODALIAS)
+		break;
+	case GB_SPI_SPI_MODALIAS:
 		memcpy(spi_board.modalias, response.name,
 		       sizeof(spi_board.modalias));
-	else
+		break;
+	default:
 		return -EINVAL;
+	}

thanks,

greg k-h

#7 lore raw

Understandable. Thanks for your time and review.

Thanks
Supreet

Quoted 55 lines · Greg Kroah-Hartman
Quoted 5 lines · On Fri, Aug 07, 2026 at 12:45:42PM +0530, [email protected] wrote:

From: Supreet Singh <[email protected]>

Replace the if/else chain that selects the SPI device type with a switch statement. This makes the code easier to extend when additional device types are introduced and improves readability.

But let's deal with that when it happens, you are adding more code here than removing:

Quoted 9 lines

---
Changes in v3:
- Fixed the memcpy indentation issue
- Removed the extra blank line in the commit message

 drivers/staging/greybus/spilib.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

So it's not really a benefit overall, right?

Quoted 19 lines
drivers/staging/greybus/spilib.c +4 -2
index e4d1ae8308aa..39d5bf234c61 100644
@@ -458,17 +458,22 @@ static int gb_spi_setup_device(struct gb_spilib *spi, u8 cs)
 
 	dev_type = response.device_type;
 
-	if (dev_type == GB_SPI_SPI_DEV)
+	switch (dev_type) {
+	case GB_SPI_SPI_DEV:
 		strscpy(spi_board.modalias, "spidev",
 			sizeof(spi_board.modalias));
-	else if (dev_type == GB_SPI_SPI_NOR)
+		break;
+	case GB_SPI_SPI_NOR:
 		strscpy(spi_board.modalias, "spi-nor",
 			sizeof(spi_board.modalias));

drivers/staging/greybus/spilib.c static int gb_spi_setup_device(struct gb_spilib *spi, u8 cs) · line 476

Quoted 10 lines
drivers/staging/greybus/spilib.c +5 -2
-	else if (dev_type == GB_SPI_SPI_MODALIAS)
+		break;
+	case GB_SPI_SPI_MODALIAS:
 		memcpy(spi_board.modalias, response.name,
 		       sizeof(spi_board.modalias));
-	else
+		break;
+	default:
 		return -EINVAL;
+	}

thanks,

greg k-h