Skip to content

Commit dcd5934

Browse files
[nrf fromlist] drivers: spi: spi_nrfx_spim: use standard instantiation
Used API for standard instantiation and replaced nrfx_err_t error values with errno. Upstream PR #: 96792 Signed-off-by: Michał Stasiak <[email protected]>
1 parent c475e9f commit dcd5934

File tree

1 file changed

+82
-101
lines changed

1 file changed

+82
-101
lines changed

drivers/spi/spi_nrfx_spim.c

Lines changed: 82 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#define DT_DRV_COMPAT nordic_nrf_spim
8+
79
#include <zephyr/drivers/spi.h>
810
#include <zephyr/drivers/spi/rtio.h>
911
#include <zephyr/cache.h>
@@ -41,36 +43,21 @@ LOG_MODULE_REGISTER(spi_nrfx_spim, CONFIG_SPI_LOG_LEVEL);
4143
#define SPI_BUFFER_IN_RAM 1
4244
#endif
4345

44-
/*
45-
* We use NODELABEL here because the nrfx API requires us to call
46-
* functions which are named according to SoC peripheral instance
47-
* being operated on. Since DT_INST() makes no guarantees about that,
48-
* it won't work.
49-
*/
50-
#define SPIM(idx) DT_NODELABEL(spi##idx)
51-
#define SPIM_PROP(idx, prop) DT_PROP(SPIM(idx), prop)
52-
#define SPIM_HAS_PROP(idx, prop) DT_NODE_HAS_PROP(SPIM(idx), prop)
53-
54-
/* Execute macro f(x) for all instances. */
55-
#define SPIM_FOR_EACH_INSTANCE(f, sep, off_code, ...) \
56-
NRFX_FOREACH_PRESENT(SPIM, f, sep, off_code, __VA_ARGS__)
57-
5846
/* Only CPUAPP and CPURAD can control clocks and power domains, so if a fast instance is
5947
* used by other cores, treat the SPIM like a normal one. This presumes the CPUAPP or CPURAD
6048
* have requested the clocks and power domains needed by the fast instance to be ACTIVE before
6149
* other cores use the fast instance.
6250
*/
6351
#if CONFIG_SOC_NRF54H20_CPUAPP || CONFIG_SOC_NRF54H20_CPURAD
64-
#define INSTANCE_IS_FAST(unused, prefix, idx, _) \
65-
UTIL_AND( \
66-
UTIL_AND( \
67-
IS_ENABLED(CONFIG_HAS_HW_NRF_SPIM##prefix##idx), \
68-
NRF_DT_IS_FAST(SPIM(idx)) \
69-
), \
70-
IS_ENABLED(CONFIG_CLOCK_CONTROL) \
52+
#define INSTANCE_IS_FAST(inst) \
53+
UTIL_AND( \
54+
NRF_DT_INST_IS_FAST(inst), \
55+
IS_ENABLED(CONFIG_CLOCK_CONTROL) \
7156
)
7257

73-
#if SPIM_FOR_EACH_INSTANCE(INSTANCE_IS_FAST, (||), (0))
58+
#define INSTANCE_IS_FAST_OR(inst) INSTANCE_IS_FAST(inst) ||
59+
60+
#if (DT_INST_FOREACH_STATUS_OKAY(INSTANCE_IS_FAST_OR) 0)
7461
#define SPIM_ANY_FAST 1
7562
/* If fast instances are used then system managed device PM cannot be used because
7663
* it may call PM actions from locked context and fast SPIM PM actions can only be
@@ -80,12 +67,12 @@ BUILD_ASSERT(!IS_ENABLED(CONFIG_PM_DEVICE_SYSTEM_MANAGED));
8067
#endif
8168
#endif
8269

83-
#define SPIM_PINS_CROSS_DOMAIN(unused, prefix, idx, _) \
84-
COND_CODE_1(DT_NODE_HAS_STATUS_OKAY(SPIM(prefix##idx)), \
85-
(SPIM_PROP(idx, cross_domain_pins_supported)), \
86-
(0))
70+
#define SPIM_PINS_CROSS_DOMAIN(inst) \
71+
DT_INST_PROP(inst, cross_domain_pins_supported)
8772

88-
#if NRFX_FOREACH_PRESENT(SPIM, SPIM_PINS_CROSS_DOMAIN, (||), (0))
73+
#define SPIM_PINS_CROSS_DOMAIN_OR(inst) SPIM_PINS_CROSS_DOMAIN(inst) ||
74+
75+
#if (SPIM_PINS_CROSS_DOMAIN_OR(INSTANCE_IS_FAST_OR) 0)
8976
#include <hal/nrf_gpio.h>
9077
/* Certain SPIM instances support usage of cross domain pins in form of dedicated pins on
9178
* a port different from the default one.
@@ -103,6 +90,7 @@ BUILD_ASSERT(!IS_ENABLED(CONFIG_PM_DEVICE_SYSTEM_MANAGED));
10390

10491

10592
struct spi_nrfx_data {
93+
nrfx_spim_t spim;
10694
struct spi_context ctx;
10795
const struct device *dev;
10896
size_t chunk_len;
@@ -123,7 +111,6 @@ struct spi_nrfx_data {
123111
};
124112

125113
struct spi_nrfx_config {
126-
nrfx_spim_t spim;
127114
uint32_t max_freq;
128115
nrfx_spim_config_t def_config;
129116
void (*irq_connect)(void);
@@ -221,8 +208,7 @@ static bool spim_has_cross_domain_connection(const struct spi_nrfx_config *confi
221208
static inline void finalize_spi_transaction(const struct device *dev, bool deactivate_cs)
222209
{
223210
struct spi_nrfx_data *dev_data = dev->data;
224-
const struct spi_nrfx_config *dev_config = dev->config;
225-
void *reg = dev_config->spim.p_reg;
211+
void *reg = dev_data->spim.p_reg;
226212

227213
if (deactivate_cs) {
228214
spi_context_cs_control(&dev_data->ctx, false);
@@ -301,7 +287,7 @@ static int configure(const struct device *dev,
301287
struct spi_context *ctx = &dev_data->ctx;
302288
uint32_t max_freq = dev_config->max_freq;
303289
nrfx_spim_config_t config;
304-
nrfx_err_t result;
290+
int result;
305291
uint32_t sck_pin;
306292

307293
if (dev_data->initialized && spi_context_configured(ctx, spi_cfg)) {
@@ -359,22 +345,22 @@ static int configure(const struct device *dev,
359345
config.mode = get_nrf_spim_mode(spi_cfg->operation);
360346
config.bit_order = get_nrf_spim_bit_order(spi_cfg->operation);
361347

362-
sck_pin = nrfy_spim_sck_pin_get(dev_config->spim.p_reg);
348+
sck_pin = nrfy_spim_sck_pin_get(dev_data->spim.p_reg);
363349

364350
if (sck_pin != NRF_SPIM_PIN_NOT_CONNECTED) {
365351
nrfy_gpio_pin_write(sck_pin, spi_cfg->operation & SPI_MODE_CPOL ? 1 : 0);
366352
}
367353

368354
if (dev_data->initialized) {
369-
nrfx_spim_uninit(&dev_config->spim);
355+
nrfx_spim_uninit(&dev_data->spim);
370356
dev_data->initialized = false;
371357
}
372358

373-
result = nrfx_spim_init(&dev_config->spim, &config,
359+
result = nrfx_spim_init(&dev_data->spim, &config,
374360
event_handler, (void *)dev);
375-
if (result != NRFX_SUCCESS) {
376-
LOG_ERR("Failed to initialize nrfx driver: %08x", result);
377-
return -EIO;
361+
if (result < 0) {
362+
LOG_ERR("Failed to initialize nrfx driver: %d", result);
363+
return result;
378364
}
379365

380366
dev_data->initialized = true;
@@ -496,7 +482,6 @@ static void transfer_next_chunk(const struct device *dev)
496482

497483
if (chunk_len > 0) {
498484
nrfx_spim_xfer_desc_t xfer;
499-
nrfx_err_t result;
500485
const uint8_t *tx_buf = ctx->tx_buf;
501486
uint8_t *rx_buf = ctx->rx_buf;
502487

@@ -506,7 +491,7 @@ static void transfer_next_chunk(const struct device *dev)
506491

507492
#ifdef SPI_BUFFER_IN_RAM
508493
if (spi_context_tx_buf_on(ctx) &&
509-
!nrf_dma_accessible_check(&dev_config->spim.p_reg, tx_buf)) {
494+
!nrf_dma_accessible_check(&dev_data->spim.p_reg, tx_buf)) {
510495

511496
if (chunk_len > CONFIG_SPI_NRFX_RAM_BUFFER_SIZE) {
512497
chunk_len = CONFIG_SPI_NRFX_RAM_BUFFER_SIZE;
@@ -517,7 +502,7 @@ static void transfer_next_chunk(const struct device *dev)
517502
}
518503

519504
if (spi_context_rx_buf_on(ctx) &&
520-
!nrf_dma_accessible_check(&dev_config->spim.p_reg, rx_buf)) {
505+
!nrf_dma_accessible_check(&dev_data->spim.p_reg, rx_buf)) {
521506

522507
if (chunk_len > CONFIG_SPI_NRFX_RAM_BUFFER_SIZE) {
523508
chunk_len = CONFIG_SPI_NRFX_RAM_BUFFER_SIZE;
@@ -556,11 +541,10 @@ static void transfer_next_chunk(const struct device *dev)
556541
}
557542
#endif
558543
if (error == 0) {
559-
result = nrfx_spim_xfer(&dev_config->spim, &xfer, 0);
560-
if (result == NRFX_SUCCESS) {
544+
error = nrfx_spim_xfer(&dev_data->spim, &xfer, 0);
545+
if (error == 0) {
561546
return;
562547
}
563-
error = -EIO;
564548
#ifdef CONFIG_SOC_NRF52832_ALLOW_SPIM_DESPITE_PAN_58
565549
anomaly_58_workaround_clear(dev_data);
566550
#endif
@@ -632,7 +616,7 @@ static int transceive(const struct device *dev,
632616
{
633617
struct spi_nrfx_data *dev_data = dev->data;
634618
const struct spi_nrfx_config *dev_config = dev->config;
635-
void *reg = dev_config->spim.p_reg;
619+
void *reg = dev_data->spim.p_reg;
636620
int error;
637621

638622
pm_device_runtime_get(dev);
@@ -678,7 +662,7 @@ static int transceive(const struct device *dev,
678662
/* Abort the current transfer by deinitializing
679663
* the nrfx driver.
680664
*/
681-
nrfx_spim_uninit(&dev_config->spim);
665+
nrfx_spim_uninit(&dev_data->spim);
682666
dev_data->initialized = false;
683667

684668
/* Make sure the transaction is finished (it may be
@@ -795,7 +779,7 @@ static void spim_suspend(const struct device *dev)
795779
struct spi_nrfx_data *dev_data = dev->data;
796780

797781
if (dev_data->initialized) {
798-
nrfx_spim_uninit(&dev_config->spim);
782+
nrfx_spim_uninit(&dev_data->spim);
799783
dev_data->initialized = false;
800784
}
801785

@@ -897,100 +881,97 @@ static int spi_nrfx_deinit(const struct device *dev)
897881
return 0;
898882
}
899883

900-
#define SPI_NRFX_SPIM_EXTENDED_CONFIG(idx) \
884+
#define SPI_NRFX_SPIM_EXTENDED_CONFIG(inst) \
901885
IF_ENABLED(NRFX_SPIM_EXTENDED_ENABLED, \
902886
(.dcx_pin = NRF_SPIM_PIN_NOT_CONNECTED, \
903-
COND_CODE_1(SPIM_PROP(idx, rx_delay_supported), \
904-
(.rx_delay = SPIM_PROP(idx, rx_delay),), \
887+
COND_CODE_1(DT_INST_PROP(inst, rx_delay_supported), \
888+
(.rx_delay = DT_INST_PROP(inst, rx_delay),), \
905889
()) \
906890
))
907891

908892
/* Get initialization priority of an instance. Instances that requires clock control
909893
* which is using nrfs (IPC) are initialized later.
910894
*/
911-
#define SPIM_INIT_PRIORITY(idx) \
912-
COND_CODE_1(INSTANCE_IS_FAST(_, /*empty*/, idx, _), \
895+
#define SPIM_INIT_PRIORITY(inst) \
896+
COND_CODE_1(INSTANCE_IS_FAST(inst), \
913897
(UTIL_INC(CONFIG_CLOCK_CONTROL_NRF_HSFLL_GLOBAL_INIT_PRIORITY)), \
914898
(CONFIG_SPI_INIT_PRIORITY))
915899

916-
#define SPI_NRFX_SPIM_DEFINE(idx) \
917-
NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(SPIM(idx)); \
918-
NRF_DT_CHECK_NODE_HAS_REQUIRED_MEMORY_REGIONS(SPIM(idx)); \
919-
static void irq_connect##idx(void) \
920-
{ \
921-
IRQ_CONNECT(DT_IRQN(SPIM(idx)), DT_IRQ(SPIM(idx), priority), \
922-
nrfx_isr, nrfx_spim_##idx##_irq_handler, 0); \
923-
} \
900+
#define SPI_NRFX_SPIM_DEFINE(inst) \
901+
NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(DT_DRV_INST(inst)); \
902+
NRF_DT_CHECK_NODE_HAS_REQUIRED_MEMORY_REGIONS(DT_DRV_INST(inst)); \
924903
IF_ENABLED(SPI_BUFFER_IN_RAM, \
925-
(static uint8_t spim_##idx##_tx_buffer \
904+
(static uint8_t spim_##inst##_tx_buffer \
926905
[CONFIG_SPI_NRFX_RAM_BUFFER_SIZE] \
927-
DMM_MEMORY_SECTION(SPIM(idx)); \
928-
static uint8_t spim_##idx##_rx_buffer \
906+
DMM_MEMORY_SECTION(DT_DRV_INST(inst)); \
907+
static uint8_t spim_##inst##_rx_buffer \
929908
[CONFIG_SPI_NRFX_RAM_BUFFER_SIZE] \
930-
DMM_MEMORY_SECTION(SPIM(idx));)) \
931-
static struct spi_nrfx_data spi_##idx##_data = { \
909+
DMM_MEMORY_SECTION(DT_DRV_INST(inst));)) \
910+
static struct spi_nrfx_data spi_##inst##_data = { \
911+
.spim = NRFX_SPIM_INSTANCE(DT_INST_REG_ADDR(inst)), \
932912
IF_ENABLED(CONFIG_MULTITHREADING, \
933-
(SPI_CONTEXT_INIT_LOCK(spi_##idx##_data, ctx),)) \
913+
(SPI_CONTEXT_INIT_LOCK(spi_##inst##_data, ctx),)) \
934914
IF_ENABLED(CONFIG_MULTITHREADING, \
935-
(SPI_CONTEXT_INIT_SYNC(spi_##idx##_data, ctx),)) \
936-
SPI_CONTEXT_CS_GPIOS_INITIALIZE(SPIM(idx), ctx) \
915+
(SPI_CONTEXT_INIT_SYNC(spi_##inst##_data, ctx),)) \
916+
SPI_CONTEXT_CS_GPIOS_INITIALIZE(DT_DRV_INST(inst), ctx) \
937917
IF_ENABLED(SPI_BUFFER_IN_RAM, \
938-
(.tx_buffer = spim_##idx##_tx_buffer, \
939-
.rx_buffer = spim_##idx##_rx_buffer,)) \
940-
.dev = DEVICE_DT_GET(SPIM(idx)), \
918+
(.tx_buffer = spim_##inst##_tx_buffer, \
919+
.rx_buffer = spim_##inst##_rx_buffer,)) \
920+
.dev = DEVICE_DT_GET(DT_DRV_INST(inst)), \
941921
.busy = false, \
942922
}; \
943-
PINCTRL_DT_DEFINE(SPIM(idx)); \
944-
static const struct spi_nrfx_config spi_##idx##z_config = { \
945-
.spim = { \
946-
.p_reg = (NRF_SPIM_Type *)DT_REG_ADDR(SPIM(idx)), \
947-
.drv_inst_idx = NRFX_SPIM##idx##_INST_IDX, \
948-
}, \
949-
.max_freq = SPIM_PROP(idx, max_frequency), \
923+
static void irq_connect##inst(void) \
924+
{ \
925+
IRQ_CONNECT(DT_INST_IRQN(inst), DT_INST_IRQ(inst, priority), \
926+
nrfx_spim_irq_handler, &spi_##inst##_data.spim, 0); \
927+
} \
928+
PINCTRL_DT_INST_DEFINE(inst); \
929+
static const struct spi_nrfx_config spi_##inst##z_config = { \
930+
.max_freq = DT_INST_PROP(inst, max_frequency), \
950931
.def_config = { \
951932
.skip_gpio_cfg = true, \
952933
.skip_psel_cfg = true, \
953934
.ss_pin = NRF_SPIM_PIN_NOT_CONNECTED, \
954-
.orc = SPIM_PROP(idx, overrun_character), \
955-
SPI_NRFX_SPIM_EXTENDED_CONFIG(idx) \
935+
.orc = DT_INST_PROP(inst, overrun_character), \
936+
SPI_NRFX_SPIM_EXTENDED_CONFIG(inst) \
956937
}, \
957-
.irq_connect = irq_connect##idx, \
958-
.pcfg = PINCTRL_DT_DEV_CONFIG_GET(SPIM(idx)), \
959-
.max_chunk_len = BIT_MASK(SPIM_PROP(idx, easydma_maxcnt_bits)),\
938+
.irq_connect = irq_connect##inst, \
939+
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \
940+
.max_chunk_len = BIT_MASK( \
941+
DT_INST_PROP(inst, easydma_maxcnt_bits)), \
960942
COND_CODE_1(CONFIG_SOC_NRF52832_ALLOW_SPIM_DESPITE_PAN_58, \
961943
(.anomaly_58_workaround = \
962-
SPIM_PROP(idx, anomaly_58_workaround),), \
944+
DT_INST_PROP(inst, anomaly_58_workaround),), \
963945
()) \
964-
.wake_pin = NRF_DT_GPIOS_TO_PSEL_OR(SPIM(idx), wake_gpios, \
946+
.wake_pin = NRF_DT_GPIOS_TO_PSEL_OR(DT_DRV_INST(inst), \
947+
wake_gpios, \
965948
WAKE_PIN_NOT_USED), \
966-
.wake_gpiote = WAKE_GPIOTE_INSTANCE(SPIM(idx)), \
949+
.wake_gpiote = WAKE_GPIOTE_INSTANCE(DT_DRV_INST(inst)), \
967950
IF_ENABLED(SPIM_ANY_FAST, \
968951
(.clk_dev = DEVICE_DT_GET_OR_NULL( \
969-
DT_CLOCKS_CTLR(SPIM(idx))), \
952+
DT_CLOCKS_CTLR(DT_DRV_INST(inst))), \
970953
.clk_spec = { \
971954
.frequency = NRF_CLOCK_CONTROL_FREQUENCY_MAX, \
972955
},)) \
973-
IF_ENABLED(SPIM_PINS_CROSS_DOMAIN(_, /*empty*/, idx, _), \
956+
IF_ENABLED(SPIM_PINS_CROSS_DOMAIN(inst), \
974957
(.cross_domain = true, \
975958
.default_port = \
976-
DT_PROP_OR(DT_PHANDLE(SPIM(idx), \
959+
DT_PROP_OR(DT_INST_PHANDLE(inst, \
977960
default_gpio_port), port, -1),)) \
978-
.mem_reg = DMM_DEV_TO_REG(SPIM(idx)), \
961+
.mem_reg = DMM_DEV_TO_REG(DT_DRV_INST(inst)), \
979962
}; \
980-
BUILD_ASSERT(!SPIM_HAS_PROP(idx, wake_gpios) || \
981-
!(DT_GPIO_FLAGS(SPIM(idx), wake_gpios) & GPIO_ACTIVE_LOW),\
963+
BUILD_ASSERT(!DT_INST_NODE_HAS_PROP(inst, wake_gpios) || \
964+
!(DT_GPIO_FLAGS(DT_DRV_INST(inst), wake_gpios) & \
965+
GPIO_ACTIVE_LOW), \
982966
"WAKE line must be configured as active high"); \
983-
PM_DEVICE_DT_DEFINE(SPIM(idx), spim_nrfx_pm_action); \
984-
SPI_DEVICE_DT_DEINIT_DEFINE(SPIM(idx), \
967+
PM_DEVICE_DT_INST_DEFINE(inst, spim_nrfx_pm_action); \
968+
SPI_DEVICE_DT_INST_DEINIT_DEFINE(inst, \
985969
spi_nrfx_init, \
986970
spi_nrfx_deinit, \
987-
PM_DEVICE_DT_GET(SPIM(idx)), \
988-
&spi_##idx##_data, \
989-
&spi_##idx##z_config, \
990-
POST_KERNEL, SPIM_INIT_PRIORITY(idx), \
971+
PM_DEVICE_DT_INST_GET(inst), \
972+
&spi_##inst##_data, \
973+
&spi_##inst##z_config, \
974+
POST_KERNEL, SPIM_INIT_PRIORITY(inst), \
991975
&spi_nrfx_driver_api)
992976

993-
#define COND_NRF_SPIM_DEVICE(unused, prefix, i, _) \
994-
IF_ENABLED(CONFIG_HAS_HW_NRF_SPIM##prefix##i, (SPI_NRFX_SPIM_DEFINE(prefix##i);))
995-
996-
SPIM_FOR_EACH_INSTANCE(COND_NRF_SPIM_DEVICE, (), (), _)
977+
DT_INST_FOREACH_STATUS_OKAY(SPI_NRFX_SPIM_DEFINE)

0 commit comments

Comments
 (0)