Skip to content

Commit 6607976

Browse files
decsnynashif
authored andcommitted
drivers: spi_mcux_lpspi: Organize top of file
Organize #includes and #defines for less redundancy and more readability. Shorten some macros to avoid multi line statements. Add some comments. Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
1 parent 69e8152 commit 6607976

File tree

1 file changed

+42
-43
lines changed

1 file changed

+42
-43
lines changed

drivers/spi/spi_mcux_lpspi.c

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,53 @@
66

77
#define DT_DRV_COMPAT nxp_imx_lpspi
88

9-
#include <errno.h>
109
#include <zephyr/drivers/spi.h>
10+
#include <zephyr/drivers/pinctrl.h>
1111
#include <zephyr/drivers/clock_control.h>
12-
#include <fsl_lpspi.h>
13-
#if CONFIG_NXP_LP_FLEXCOMM
14-
#include <zephyr/drivers/mfd/nxp_lp_flexcomm.h>
15-
#endif
16-
#include <zephyr/logging/log.h>
1712
#include <zephyr/irq.h>
18-
#ifdef CONFIG_SPI_MCUX_LPSPI_DMA
19-
#include <zephyr/drivers/dma.h>
20-
#endif
21-
#include <zephyr/drivers/pinctrl.h>
13+
14+
#include <zephyr/logging/log.h>
15+
LOG_MODULE_REGISTER(spi_mcux_lpspi, CONFIG_SPI_LOG_LEVEL);
16+
2217
#ifdef CONFIG_SPI_RTIO
23-
#include <zephyr/rtio/rtio.h>
2418
#include <zephyr/drivers/spi/rtio.h>
25-
#include <zephyr/spinlock.h>
2619
#endif
2720

28-
LOG_MODULE_REGISTER(spi_mcux_lpspi, CONFIG_SPI_LOG_LEVEL);
29-
3021
#include "spi_context.h"
3122

23+
#if CONFIG_NXP_LP_FLEXCOMM
24+
#include <zephyr/drivers/mfd/nxp_lp_flexcomm.h>
25+
#endif
26+
27+
#include <fsl_lpspi.h>
28+
29+
/* If any hardware revisions change this, make it into a DT property.
30+
* DONT'T make #ifdefs here by platform.
31+
*/
3232
#define CHIP_SELECT_COUNT 4
3333
#define MAX_DATA_WIDTH 4096
3434

3535
/* Required by DEVICE_MMIO_NAMED_* macros */
3636
#define DEV_CFG(_dev) ((const struct spi_mcux_config *)(_dev)->config)
3737
#define DEV_DATA(_dev) ((struct spi_mcux_data *)(_dev)->data)
3838

39+
#ifdef CONFIG_SPI_MCUX_LPSPI_DMA
40+
#include <zephyr/drivers/dma.h>
41+
42+
/* These flags are arbitrary */
43+
#define LPSPI_DMA_ERROR_FLAG BIT(0)
44+
#define LPSPI_DMA_RX_DONE_FLAG BIT(1)
45+
#define LPSPI_DMA_TX_DONE_FLAG BIT(2)
46+
#define LPSPI_DMA_DONE_FLAG (LPSPI_DMA_RX_DONE_FLAG | LPSPI_DMA_TX_DONE_FLAG)
47+
48+
struct spi_dma_stream {
49+
const struct device *dma_dev;
50+
uint32_t channel; /* stores the channel for dma */
51+
struct dma_config dma_cfg;
52+
struct dma_block_config dma_blk_cfg;
53+
};
54+
#endif /* CONFIG_SPI_MCUX_LPSPI_DMA */
55+
3956
struct spi_mcux_config {
4057
DEVICE_MMIO_NAMED_ROM(reg_base);
4158
#ifdef CONFIG_NXP_LP_FLEXCOMM
@@ -51,36 +68,19 @@ struct spi_mcux_config {
5168
lpspi_pin_config_t data_pin_config;
5269
};
5370

54-
#ifdef CONFIG_SPI_MCUX_LPSPI_DMA
55-
#define SPI_MCUX_LPSPI_DMA_ERROR_FLAG 0x01
56-
#define SPI_MCUX_LPSPI_DMA_RX_DONE_FLAG 0x02
57-
#define SPI_MCUX_LPSPI_DMA_TX_DONE_FLAG 0x04
58-
#define SPI_MCUX_LPSPI_DMA_DONE_FLAG \
59-
(SPI_MCUX_LPSPI_DMA_RX_DONE_FLAG | SPI_MCUX_LPSPI_DMA_TX_DONE_FLAG)
60-
61-
struct stream {
62-
const struct device *dma_dev;
63-
uint32_t channel; /* stores the channel for dma */
64-
struct dma_config dma_cfg;
65-
struct dma_block_config dma_blk_cfg;
66-
};
67-
#endif
68-
6971
struct spi_mcux_data {
7072
DEVICE_MMIO_NAMED_RAM(reg_base);
7173
const struct device *dev;
7274
lpspi_master_handle_t handle;
7375
struct spi_context ctx;
7476
size_t transfer_len;
75-
7677
#ifdef CONFIG_SPI_RTIO
7778
struct spi_rtio *rtio_ctx;
7879
#endif
79-
8080
#ifdef CONFIG_SPI_MCUX_LPSPI_DMA
8181
volatile uint32_t status_flags;
82-
struct stream dma_rx;
83-
struct stream dma_tx;
82+
struct spi_dma_stream dma_rx;
83+
struct spi_dma_stream dma_tx;
8484
/* dummy value used for transferring NOP when tx buf is null */
8585
uint32_t dummy_tx_buffer;
8686
/* dummy value used to read RX data into when rx buf is null */
@@ -286,25 +286,25 @@ static void spi_mcux_dma_callback(const struct device *dev, void *arg, uint32_t
286286

287287
if (status < 0) {
288288
LOG_ERR("DMA callback error with channel %d.", channel);
289-
data->status_flags |= SPI_MCUX_LPSPI_DMA_ERROR_FLAG;
289+
data->status_flags |= LPSPI_DMA_ERROR_FLAG;
290290
} else {
291291
/* identify the origin of this callback */
292292
if (channel == data->dma_tx.channel) {
293293
/* this part of the transfer ends */
294-
data->status_flags |= SPI_MCUX_LPSPI_DMA_TX_DONE_FLAG;
294+
data->status_flags |= LPSPI_DMA_TX_DONE_FLAG;
295295
LOG_DBG("DMA TX Block Complete");
296296
} else if (channel == data->dma_rx.channel) {
297297
/* this part of the transfer ends */
298-
data->status_flags |= SPI_MCUX_LPSPI_DMA_RX_DONE_FLAG;
298+
data->status_flags |= LPSPI_DMA_RX_DONE_FLAG;
299299
LOG_DBG("DMA RX Block Complete");
300300
} else {
301301
LOG_ERR("DMA callback channel %d is not valid.", channel);
302-
data->status_flags |= SPI_MCUX_LPSPI_DMA_ERROR_FLAG;
302+
data->status_flags |= LPSPI_DMA_ERROR_FLAG;
303303
}
304304
}
305305
#if CONFIG_SPI_ASYNC
306306
if (data->ctx.asynchronous &&
307-
((data->status_flags & SPI_MCUX_LPSPI_DMA_DONE_FLAG) == SPI_MCUX_LPSPI_DMA_DONE_FLAG)) {
307+
((data->status_flags & LPSPI_DMA_DONE_FLAG) == LPSPI_DMA_DONE_FLAG)) {
308308
/* Load dma blocks of equal length */
309309
size_t dma_size = MIN(data->ctx.tx_len, data->ctx.rx_len);
310310

@@ -332,7 +332,7 @@ static int spi_mcux_dma_tx_load(const struct device *dev, const uint8_t *buf, si
332332
LPSPI_Type *base = (LPSPI_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
333333

334334
/* remember active TX DMA channel (used in callback) */
335-
struct stream *stream = &data->dma_tx;
335+
struct spi_dma_stream *stream = &data->dma_tx;
336336

337337
blk_cfg = &stream->dma_blk_cfg;
338338

@@ -373,7 +373,7 @@ static int spi_mcux_dma_rx_load(const struct device *dev, uint8_t *buf, size_t l
373373
LPSPI_Type *base = (LPSPI_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
374374

375375
/* retrieve active RX DMA channel (used in callback) */
376-
struct stream *stream = &data->dma_rx;
376+
struct spi_dma_stream *stream = &data->dma_rx;
377377

378378
blk_cfg = &stream->dma_blk_cfg;
379379

@@ -416,12 +416,11 @@ static int wait_dma_rx_tx_done(const struct device *dev)
416416
LOG_DBG("Timed out waiting for SPI context to complete");
417417
return ret;
418418
}
419-
if (data->status_flags & SPI_MCUX_LPSPI_DMA_ERROR_FLAG) {
419+
if (data->status_flags & LPSPI_DMA_ERROR_FLAG) {
420420
return -EIO;
421421
}
422422

423-
if ((data->status_flags & SPI_MCUX_LPSPI_DMA_DONE_FLAG) ==
424-
SPI_MCUX_LPSPI_DMA_DONE_FLAG) {
423+
if ((data->status_flags & LPSPI_DMA_DONE_FLAG) == LPSPI_DMA_DONE_FLAG) {
425424
LOG_DBG("DMA block completed");
426425
return 0;
427426
}

0 commit comments

Comments
 (0)