Skip to content

Commit 82a943e

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 4e1421f commit 82a943e

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
@@ -26,29 +26,14 @@ int spi_mcux_release(const struct device *dev, const struct spi_config *spi_cfg)
2626
return 0;
2727
}
2828

29-
int spi_mcux_configure(const struct device *dev, const struct spi_config *spi_cfg)
29+
static inline int lpspi_validate_xfer_args(const struct spi_config *spi_cfg)
3030
{
31-
const struct spi_mcux_config *config = dev->config;
32-
struct spi_mcux_data *data = dev->data;
33-
struct spi_context *ctx = &data->ctx;
34-
LPSPI_Type *base = (LPSPI_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
3531
uint32_t word_size = SPI_WORD_SIZE_GET(spi_cfg->operation);
36-
bool configured = ctx->config != NULL;
37-
lpspi_master_config_t master_config;
38-
uint32_t clock_freq;
39-
int ret;
40-
41-
/* fast path to avoid reconfigure */
42-
/* TODO: S32K3 errata ERR050456 requiring module reset before every transfer,
43-
* investigate alternative workaround so we don't have this latency for S32.
44-
*/
45-
if (spi_context_configured(ctx, spi_cfg) && !IS_ENABLED(CONFIG_SOC_FAMILY_NXP_S32)) {
46-
return 0;
47-
}
32+
uint32_t pcs = spi_cfg->slave;
4833

4934
if (spi_cfg->operation & SPI_HALF_DUPLEX) {
5035
/* the IP DOES support half duplex, need to implement driver support */
51-
LOG_ERR("Half-duplex not supported");
36+
LOG_WRN("Half-duplex not supported");
5237
return -ENOTSUP;
5338
}
5439

@@ -60,22 +45,49 @@ int spi_mcux_configure(const struct device *dev, const struct spi_config *spi_cf
6045
* Minimum hardware word size is 2. Since this driver is intended to work
6146
* for 32 bit platforms, and 64 bits is max size, then only 33 and 1 are invalid.
6247
*/
63-
LOG_ERR("Word size %d not allowed", word_size);
48+
LOG_WRN("Word size %d not allowed", word_size);
6449
return -EINVAL;
6550
}
6651

67-
if (spi_cfg->slave > (LPSPI_CHIP_SELECT_COUNT - 1)) {
68-
LOG_ERR("Peripheral %d select exceeds max %d", spi_cfg->slave,
69-
LPSPI_CHIP_SELECT_COUNT - 1);
52+
if (pcs > LPSPI_CHIP_SELECT_COUNT - 1) {
53+
LOG_WRN("Peripheral %d select exceeds max %d", pcs, LPSPI_CHIP_SELECT_COUNT - 1);
7054
return -EINVAL;
7155
}
7256

57+
return 0;
58+
}
59+
60+
int spi_mcux_configure(const struct device *dev, const struct spi_config *spi_cfg)
61+
{
62+
const struct spi_mcux_config *config = dev->config;
63+
struct spi_mcux_data *data = dev->data;
64+
struct spi_context *ctx = &data->ctx;
65+
bool already_configured = spi_context_configured(ctx, spi_cfg);
66+
LPSPI_Type *base = (LPSPI_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
67+
uint32_t word_size = SPI_WORD_SIZE_GET(spi_cfg->operation);
68+
lpspi_master_config_t master_config;
69+
uint32_t clock_freq;
70+
int ret;
71+
72+
/* fast path to avoid reconfigure */
73+
/* TODO: S32K3 errata ERR050456 requiring module reset before every transfer,
74+
* investigate alternative workaround so we don't have this latency for S32.
75+
*/
76+
if (already_configured && !IS_ENABLED(CONFIG_SOC_FAMILY_NXP_S32)) {
77+
return 0;
78+
}
79+
80+
ret = lpspi_validate_xfer_args(spi_cfg);
81+
if (ret) {
82+
return ret;
83+
}
84+
7385
ret = clock_control_get_rate(config->clock_dev, config->clock_subsys, &clock_freq);
7486
if (ret) {
7587
return ret;
7688
}
7789

78-
if (configured) {
90+
if (already_configured) {
7991
/* Setting the baud rate in LPSPI_MasterInit requires module to be disabled. Only
8092
* disable if already configured, otherwise the clock is not enabled and the
8193
* CR register cannot be written.

0 commit comments

Comments
 (0)