Skip to content

Commit 6b169e1

Browse files
jerome-pouillerasmellby
authored andcommitted
drivers: spi: siwx91x: Simplify error management
In functions requiring to lock/release resources, it is less error prone to have only one exit point and user goto to manage errors. The behavior of the new code is exactly identical to the initial one. Signed-off-by: Jérôme Pouiller <[email protected]> Upstream-status: pr <zephyrproject-rtos#100762>
1 parent 8253921 commit 6b169e1

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

drivers/spi/spi_silabs_siwx91x_gspi.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ static int gspi_siwx91x_transceive(const struct device *dev, const struct spi_co
626626
spi_callback_t cb, void *userdata)
627627
{
628628
struct gspi_siwx91x_data *data = dev->data;
629-
int ret = 0;
629+
int ret;
630630

631631
ret = pm_device_runtime_get(dev);
632632
if (ret < 0) {
@@ -635,40 +635,38 @@ static int gspi_siwx91x_transceive(const struct device *dev, const struct spi_co
635635

636636
if (!spi_siwx91x_is_dma_enabled_instance(dev) && asynchronous) {
637637
ret = -ENOTSUP;
638-
pm_device_runtime_put(dev);
639-
return ret;
638+
goto pm;
640639
}
641640

642641
spi_context_lock(&data->ctx, asynchronous, cb, userdata, config);
643-
/* Configure the device if it is not already configured */
644642
if (!spi_context_configured(&data->ctx, config)) {
645643
ret = gspi_siwx91x_config(dev, config, cb, userdata);
646644
if (ret) {
647-
spi_context_release(&data->ctx, ret);
648-
pm_device_runtime_put(dev);
649-
return ret;
645+
goto context;
650646
}
651647
}
652648

653649
spi_context_buffers_setup(&data->ctx, tx_bufs, rx_bufs,
654650
SPI_WORD_SIZE_GET(config->operation) / 8);
655651

656-
/* Check if DMA is enabled */
657652
if (spi_siwx91x_is_dma_enabled_instance(dev)) {
658-
/* Perform DMA transceive */
659653
ret = gspi_siwx91x_transceive_dma(dev, config);
660-
if (ret < 0) {
661-
pm_device_runtime_put(dev);
662-
}
663-
spi_context_release(&data->ctx, ret);
664654
} else {
665-
/* Perform synchronous polling transceive */
666655
ret = gspi_siwx91x_transceive_polling_sync(dev, &data->ctx);
667-
spi_context_unlock_unconditionally(&data->ctx);
668-
pm_device_runtime_put(dev);
669656
}
670657

658+
if (spi_siwx91x_is_dma_enabled_instance(dev) && !ret) {
659+
/* pm_device_runtime_put(dev) is called from the dma callback */
660+
spi_context_release(&data->ctx, ret);
661+
return ret;
662+
}
663+
664+
context:
665+
spi_context_release(&data->ctx, ret);
666+
pm:
667+
pm_device_runtime_put(dev);
671668
return ret;
669+
672670
}
673671

674672
static int gspi_siwx91x_transceive_sync(const struct device *dev, const struct spi_config *config,

0 commit comments

Comments
 (0)