Skip to content

Commit 6da3d18

Browse files
committed
spi_nxp_lpspi: Refactor validation args to func
Minor refactor to make a separate function to validate configuration arguments. Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
1 parent 6429816 commit 6da3d18

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

drivers/spi/spi_nxp_lpspi/spi_nxp_lpspi_common.c

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,14 @@ int spi_mcux_release(const struct device *dev, const struct spi_config *spi_cfg)
4444
return 0;
4545
}
4646

47-
int spi_mcux_configure(const struct device *dev, const struct spi_config *spi_cfg)
47+
static inline int lpspi_validate_xfer_args(const struct spi_config *spi_cfg)
4848
{
49-
const struct spi_mcux_config *config = dev->config;
50-
struct spi_mcux_data *data = dev->data;
51-
struct spi_context *ctx = &data->ctx;
52-
LPSPI_Type *base = (LPSPI_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
5349
uint32_t word_size = SPI_WORD_SIZE_GET(spi_cfg->operation);
54-
bool configured = ctx->config != NULL;
55-
lpspi_master_config_t master_config;
56-
uint32_t clock_freq;
57-
int ret;
58-
59-
/* fast path to avoid reconfigure */
60-
/* TODO: S32K3 errata ERR050456 requiring module reset before every transfer,
61-
* investigate alternative workaround so we don't have this latency for S32.
62-
*/
63-
if (spi_context_configured(ctx, spi_cfg) && !IS_ENABLED(CONFIG_SOC_FAMILY_NXP_S32)) {
64-
return 0;
65-
}
50+
uint32_t pcs = spi_cfg->slave;
6651

6752
if (spi_cfg->operation & SPI_HALF_DUPLEX) {
6853
/* the IP DOES support half duplex, need to implement driver support */
69-
LOG_ERR("Half-duplex not supported");
54+
LOG_WRN("Half-duplex not supported");
7055
return -ENOTSUP;
7156
}
7257

@@ -78,22 +63,49 @@ int spi_mcux_configure(const struct device *dev, const struct spi_config *spi_cf
7863
* Minimum hardware word size is 2. Since this driver is intended to work
7964
* for 32 bit platforms, and 64 bits is max size, then only 33 and 1 are invalid.
8065
*/
81-
LOG_ERR("Word size %d not allowed", word_size);
66+
LOG_WRN("Word size %d not allowed", word_size);
8267
return -EINVAL;
8368
}
8469

85-
if (spi_cfg->slave > (LPSPI_CHIP_SELECT_COUNT - 1)) {
86-
LOG_ERR("Peripheral %d select exceeds max %d", spi_cfg->slave,
87-
LPSPI_CHIP_SELECT_COUNT - 1);
70+
if (pcs > LPSPI_CHIP_SELECT_COUNT - 1) {
71+
LOG_WRN("Peripheral %d select exceeds max %d", pcs, LPSPI_CHIP_SELECT_COUNT - 1);
8872
return -EINVAL;
8973
}
9074

75+
return 0;
76+
}
77+
78+
int spi_mcux_configure(const struct device *dev, const struct spi_config *spi_cfg)
79+
{
80+
const struct spi_mcux_config *config = dev->config;
81+
struct spi_mcux_data *data = dev->data;
82+
struct spi_context *ctx = &data->ctx;
83+
bool already_configured = spi_context_configured(ctx, spi_cfg);
84+
LPSPI_Type *base = (LPSPI_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
85+
uint32_t word_size = SPI_WORD_SIZE_GET(spi_cfg->operation);
86+
lpspi_master_config_t master_config;
87+
uint32_t clock_freq;
88+
int ret;
89+
90+
/* fast path to avoid reconfigure */
91+
/* TODO: S32K3 errata ERR050456 requiring module reset before every transfer,
92+
* investigate alternative workaround so we don't have this latency for S32.
93+
*/
94+
if (already_configured && !IS_ENABLED(CONFIG_SOC_FAMILY_NXP_S32)) {
95+
return 0;
96+
}
97+
98+
ret = lpspi_validate_xfer_args(spi_cfg);
99+
if (ret) {
100+
return ret;
101+
}
102+
91103
ret = clock_control_get_rate(config->clock_dev, config->clock_subsys, &clock_freq);
92104
if (ret) {
93105
return ret;
94106
}
95107

96-
if (configured) {
108+
if (already_configured) {
97109
/* Setting the baud rate in LPSPI_MasterInit requires module to be disabled. Only
98110
* disable if already configured, otherwise the clock is not enabled and the
99111
* CR register cannot be written.

0 commit comments

Comments
 (0)