diff --git a/drivers/spi/spi_nrfx_spi.c b/drivers/spi/spi_nrfx_spi.c index e33b4a6a648..7f1263b8787 100644 --- a/drivers/spi/spi_nrfx_spi.c +++ b/drivers/spi/spi_nrfx_spi.c @@ -26,6 +26,7 @@ struct spi_nrfx_data { struct spi_nrfx_config { nrfx_spi_t spi; nrfx_spi_config_t def_config; + void (*irq_connect)(void); #ifdef CONFIG_PINCTRL const struct pinctrl_dev_config *pcfg; #endif @@ -316,6 +317,31 @@ static int spi_nrfx_pm_action(const struct device *dev, } #endif /* CONFIG_PM_DEVICE */ +static int spi_nrfx_init(const struct device *dev) +{ + const struct spi_nrfx_config *dev_config = dev->config; + struct spi_nrfx_data *dev_data = dev->data; + int err; + +#ifdef CONFIG_PINCTRL + err = pinctrl_apply_state(dev_config->pcfg, PINCTRL_STATE_DEFAULT); + if (err < 0) { + return err; + } +#endif + + dev_config->irq_connect(); + + err = spi_context_cs_configure_all(&dev_data->ctx); + if (err < 0) { + return err; + } + + spi_context_unlock_unconditionally(&dev_data->ctx); + + return 0; +} + /* * Current factors requiring use of DT_NODELABEL: * @@ -354,26 +380,10 @@ static int spi_nrfx_pm_action(const struct device *dev, SPI_PROP(idx, miso_pull_down)), \ "SPI"#idx \ ": cannot enable both pull-up and pull-down on MISO line"); \ - static int spi_##idx##_init(const struct device *dev) \ + static void irq_connect##idx(void) \ { \ - struct spi_nrfx_data *dev_data = dev->data; \ - int err; \ IRQ_CONNECT(DT_IRQN(SPI(idx)), DT_IRQ(SPI(idx), priority), \ nrfx_isr, nrfx_spi_##idx##_irq_handler, 0); \ - IF_ENABLED(CONFIG_PINCTRL, ( \ - const struct spi_nrfx_config *dev_config = dev->config;\ - err = pinctrl_apply_state(dev_config->pcfg, \ - PINCTRL_STATE_DEFAULT); \ - if (err < 0) { \ - return err; \ - } \ - )) \ - err = spi_context_cs_configure_all(&dev_data->ctx); \ - if (err < 0) { \ - return err; \ - } \ - spi_context_unlock_unconditionally(&dev_data->ctx); \ - return 0; \ } \ static struct spi_nrfx_data spi_##idx##_data = { \ SPI_CONTEXT_INIT_LOCK(spi_##idx##_data, ctx), \ @@ -393,12 +403,13 @@ static int spi_nrfx_pm_action(const struct device *dev, .ss_pin = NRFX_SPI_PIN_NOT_USED, \ .orc = SPI_PROP(idx, overrun_character), \ }, \ + .irq_connect = irq_connect##idx, \ IF_ENABLED(CONFIG_PINCTRL, \ (.pcfg = PINCTRL_DT_DEV_CONFIG_GET(SPI(idx)),)) \ }; \ PM_DEVICE_DT_DEFINE(SPI(idx), spi_nrfx_pm_action); \ DEVICE_DT_DEFINE(SPI(idx), \ - spi_##idx##_init, \ + spi_nrfx_init, \ PM_DEVICE_DT_GET(SPI(idx)), \ &spi_##idx##_data, \ &spi_##idx##z_config, \ diff --git a/drivers/spi/spi_nrfx_spim.c b/drivers/spi/spi_nrfx_spim.c index 51b3c03b6bd..11d8df9c854 100644 --- a/drivers/spi/spi_nrfx_spim.c +++ b/drivers/spi/spi_nrfx_spim.c @@ -49,6 +49,7 @@ struct spi_nrfx_config { nrfx_spim_t spim; uint32_t max_freq; nrfx_spim_config_t def_config; + void (*irq_connect)(void); #ifdef CONFIG_PINCTRL const struct pinctrl_dev_config *pcfg; #endif @@ -482,6 +483,35 @@ static int spim_nrfx_pm_action(const struct device *dev, } #endif /* CONFIG_PM_DEVICE */ + +static int spi_nrfx_init(const struct device *dev) +{ + const struct spi_nrfx_config *dev_config = dev->config; + struct spi_nrfx_data *dev_data = dev->data; + int err; + +#ifdef CONFIG_PINCTRL + err = pinctrl_apply_state(dev_config->pcfg, PINCTRL_STATE_DEFAULT); + if (err < 0) { + return err; + } +#endif + + dev_config->irq_connect(); + + err = spi_context_cs_configure_all(&dev_data->ctx); + if (err < 0) { + return err; + } + + spi_context_unlock_unconditionally(&dev_data->ctx); + +#ifdef CONFIG_SOC_NRF52832_ALLOW_SPIM_DESPITE_PAN_58 + return anomaly_58_workaround_init(dev); +#else + return 0; +#endif +} /* * We use NODELABEL here because the nrfx API requires us to call * functions which are named according to SoC peripheral instance @@ -528,28 +558,10 @@ static int spim_nrfx_pm_action(const struct device *dev, SPIM_PROP(idx, miso_pull_down)), \ "SPIM"#idx \ ": cannot enable both pull-up and pull-down on MISO line"); \ - static int spi_##idx##_init(const struct device *dev) \ + static void irq_connect##idx(void) \ { \ - struct spi_nrfx_data *dev_data = dev->data; \ - int err; \ IRQ_CONNECT(DT_IRQN(SPIM(idx)), DT_IRQ(SPIM(idx), priority), \ nrfx_isr, nrfx_spim_##idx##_irq_handler, 0); \ - IF_ENABLED(CONFIG_PINCTRL, ( \ - const struct spi_nrfx_config *dev_config = dev->config;\ - err = pinctrl_apply_state(dev_config->pcfg, \ - PINCTRL_STATE_DEFAULT); \ - if (err < 0) { \ - return err; \ - } \ - )) \ - err = spi_context_cs_configure_all(&dev_data->ctx); \ - if (err < 0) { \ - return err; \ - } \ - spi_context_unlock_unconditionally(&dev_data->ctx); \ - COND_CODE_1(CONFIG_SOC_NRF52832_ALLOW_SPIM_DESPITE_PAN_58, \ - (return anomaly_58_workaround_init(dev);), \ - (return 0;)) \ } \ IF_ENABLED(SPI_BUFFER_IN_RAM, \ (static uint8_t spim_##idx##_buffer \ @@ -577,6 +589,7 @@ static int spim_nrfx_pm_action(const struct device *dev, .orc = SPIM_PROP(idx, overrun_character), \ SPI_NRFX_SPIM_EXTENDED_CONFIG(idx) \ }, \ + .irq_connect = irq_connect##idx, \ COND_CODE_1(CONFIG_SOC_NRF52832_ALLOW_SPIM_DESPITE_PAN_58, \ (.anomaly_58_workaround = \ SPIM_PROP(idx, anomaly_58_workaround),), \ @@ -586,7 +599,7 @@ static int spim_nrfx_pm_action(const struct device *dev, }; \ PM_DEVICE_DT_DEFINE(SPIM(idx), spim_nrfx_pm_action); \ DEVICE_DT_DEFINE(SPIM(idx), \ - spi_##idx##_init, \ + spi_nrfx_init, \ PM_DEVICE_DT_GET(SPIM(idx)), \ &spi_##idx##_data, \ &spi_##idx##z_config, \ diff --git a/drivers/spi/spi_nrfx_spis.c b/drivers/spi/spi_nrfx_spis.c index 943e8157cea..d739f20f31a 100644 --- a/drivers/spi/spi_nrfx_spis.c +++ b/drivers/spi/spi_nrfx_spis.c @@ -20,6 +20,8 @@ struct spi_nrfx_data { struct spi_nrfx_config { nrfx_spis_t spis; + nrfx_spis_config_t config; + void (*irq_connect)(void); #ifdef CONFIG_PINCTRL const struct pinctrl_dev_config *pcfg; #endif @@ -227,18 +229,27 @@ static void event_handler(const nrfx_spis_evt_t *p_event, void *p_context) } } -static int init_spis(const struct device *dev, - const nrfx_spis_config_t *config) +static int spi_nrfx_init(const struct device *dev) { const struct spi_nrfx_config *dev_config = dev->config; struct spi_nrfx_data *dev_data = dev->data; + nrfx_err_t result; + +#ifdef CONFIG_PINCTRL + int err; + + err = pinctrl_apply_state(dev_config->pcfg, PINCTRL_STATE_DEFAULT); + if (err < 0) { + return err; + } +#endif + /* This sets only default values of mode and bit order. The ones to be * actually used are set in configure() when a transfer is prepared. */ - nrfx_err_t result = nrfx_spis_init(&dev_config->spis, - config, - event_handler, - dev_data); + result = nrfx_spis_init(&dev_config->spis, &dev_config->config, + event_handler, dev_data); + if (result != NRFX_SUCCESS) { LOG_ERR("Failed to initialize device: %s", dev->name); return -EBUSY; @@ -275,26 +286,10 @@ static int init_spis(const struct device *dev, #define SPI_NRFX_SPIS_DEFINE(idx) \ NRF_DT_CHECK_PIN_ASSIGNMENTS(SPIS(idx), 0, \ sck_pin, mosi_pin, miso_pin, csn_pin); \ - static int spi_##idx##_init(const struct device *dev) \ + static void irq_connect##idx(void) \ { \ IRQ_CONNECT(DT_IRQN(SPIS(idx)), DT_IRQ(SPIS(idx), priority), \ nrfx_isr, nrfx_spis_##idx##_irq_handler, 0); \ - const nrfx_spis_config_t config = { \ - SPI_NRFX_SPIS_PIN_CFG(idx) \ - .mode = NRF_SPIS_MODE_0, \ - .bit_order = NRF_SPIS_BIT_ORDER_MSB_FIRST, \ - .orc = SPIS_PROP(idx, overrun_character), \ - .def = SPIS_PROP(idx, def_char), \ - }; \ - IF_ENABLED(CONFIG_PINCTRL, ( \ - const struct spi_nrfx_config *dev_config = dev->config;\ - int err = pinctrl_apply_state(dev_config->pcfg, \ - PINCTRL_STATE_DEFAULT); \ - if (err < 0) { \ - return err; \ - } \ - )) \ - return init_spis(dev, &config); \ } \ static struct spi_nrfx_data spi_##idx##_data = { \ SPI_CONTEXT_INIT_LOCK(spi_##idx##_data, ctx), \ @@ -306,11 +301,19 @@ static int init_spis(const struct device *dev, .p_reg = (NRF_SPIS_Type *)DT_REG_ADDR(SPIS(idx)), \ .drv_inst_idx = NRFX_SPIS##idx##_INST_IDX, \ }, \ + .config = { \ + SPI_NRFX_SPIS_PIN_CFG(idx) \ + .mode = NRF_SPIS_MODE_0, \ + .bit_order = NRF_SPIS_BIT_ORDER_MSB_FIRST, \ + .orc = SPIS_PROP(idx, overrun_character), \ + .def = SPIS_PROP(idx, def_char), \ + }, \ + .irq_connect = irq_connect##idx, \ IF_ENABLED(CONFIG_PINCTRL, \ (.pcfg = PINCTRL_DT_DEV_CONFIG_GET(SPIS(idx)),)) \ }; \ DEVICE_DT_DEFINE(SPIS(idx), \ - spi_##idx##_init, \ + spi_nrfx_init, \ NULL, \ &spi_##idx##_data, \ &spi_##idx##z_config, \