Skip to content

Commit 5d081c9

Browse files
decsnynashif
authored andcommitted
drivers: spi_mcux_lpspi: Create DMA dev helpers
Create helper functions for duplicated code related to checking for DMA devices, with readable names. Also remove unneeded function prototype. Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
1 parent d76ccdb commit 5d081c9

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

drivers/spi/spi_mcux_lpspi.c

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,10 @@ static int spi_mcux_configure(const struct device *dev, const struct spi_config
272272
}
273273

274274
#ifdef CONFIG_SPI_MCUX_LPSPI_DMA
275-
static int spi_mcux_dma_rxtx_load(const struct device *dev, size_t *dma_size);
275+
static bool lpspi_inst_has_dma(const struct spi_mcux_data *data)
276+
{
277+
return (data->dma_tx.dma_dev && data->dma_rx.dma_dev);
278+
}
276279

277280
/* This function is executed in the interrupt context */
278281
static void spi_mcux_dma_callback(const struct device *dev, void *arg, uint32_t channel, int status)
@@ -550,7 +553,9 @@ static int transceive_dma(const struct device *dev, const struct spi_config *spi
550553

551554
return ret;
552555
}
553-
#endif
556+
#else
557+
#define lpspi_inst_has_dma(arg) arg != arg
558+
#endif /* CONFIG_SPI_MCUX_LPSPI_DMA */
554559

555560
#ifdef CONFIG_SPI_RTIO
556561

@@ -613,7 +618,7 @@ static int spi_mcux_transceive(const struct device *dev, const struct spi_config
613618
#ifdef CONFIG_SPI_MCUX_LPSPI_DMA
614619
const struct spi_mcux_data *data = dev->data;
615620

616-
if (data->dma_rx.dma_dev && data->dma_tx.dma_dev) {
621+
if (lpspi_inst_has_dma(data)) {
617622
return transceive_dma(dev, spi_cfg, tx_bufs, rx_bufs, false, NULL, NULL);
618623
}
619624
#endif /* CONFIG_SPI_MCUX_LPSPI_DMA */
@@ -630,7 +635,7 @@ static int spi_mcux_transceive_async(const struct device *dev, const struct spi_
630635
#ifdef CONFIG_SPI_MCUX_LPSPI_DMA
631636
struct spi_mcux_data *data = dev->data;
632637

633-
if (data->dma_rx.dma_dev && data->dma_tx.dma_dev) {
638+
if (lpspi_inst_has_dma(data)) {
634639
spi_context_buffers_setup(&data->ctx, tx_bufs, rx_bufs, 1);
635640
}
636641

@@ -749,29 +754,42 @@ static void spi_mcux_iodev_complete(const struct device *dev, int status)
749754

750755
#endif
751756

757+
#if defined(CONFIG_SPI_MCUX_LPSPI_DMA)
758+
static int lpspi_dma_dev_ready(const struct device *dma_dev)
759+
{
760+
if (!device_is_ready(dma_dev)) {
761+
LOG_ERR("%s device is not ready", dma_dev->name);
762+
return -ENODEV;
763+
}
764+
765+
return 0;
766+
}
767+
768+
static int lpspi_dma_devs_ready(struct spi_mcux_data *data)
769+
{
770+
return lpspi_dma_dev_ready(data->dma_tx.dma_dev) |
771+
lpspi_dma_dev_ready(data->dma_rx.dma_dev);
772+
}
773+
#else
774+
#define lpspi_dma_devs_ready(...) 0
775+
#endif /* CONFIG_SPI_MCUX_LPSPI_DMA */
776+
752777
static int spi_mcux_init(const struct device *dev)
753778
{
754779
const struct spi_mcux_config *config = dev->config;
755780
struct spi_mcux_data *data = dev->data;
756-
int err;
781+
int err = 0;
757782

758783
DEVICE_MMIO_NAMED_MAP(dev, reg_base, K_MEM_CACHE_NONE | K_MEM_DIRECT_MAP);
759784

760785
data->dev = dev;
761786

762-
#ifdef CONFIG_SPI_MCUX_LPSPI_DMA
763-
if (data->dma_tx.dma_dev && data->dma_rx.dma_dev) {
764-
if (!device_is_ready(data->dma_tx.dma_dev)) {
765-
LOG_ERR("%s device is not ready", data->dma_tx.dma_dev->name);
766-
return -ENODEV;
767-
}
768-
769-
if (!device_is_ready(data->dma_rx.dma_dev)) {
770-
LOG_ERR("%s device is not ready", data->dma_rx.dma_dev->name);
771-
return -ENODEV;
772-
}
787+
if (IS_ENABLED(CONFIG_SPI_MCUX_LPSPI_DMA) && lpspi_inst_has_dma(data)) {
788+
err = lpspi_dma_devs_ready(data);
789+
}
790+
if (err < 0) {
791+
return err;
773792
}
774-
#endif /* CONFIG_SPI_MCUX_LPSPI_DMA */
775793

776794
err = spi_context_cs_configure_all(&data->ctx);
777795
if (err < 0) {

0 commit comments

Comments
 (0)