Skip to content

Commit 32b9e65

Browse files
ananglcarlescufi
authored andcommitted
drivers: spi: nrfx: Add support for pinctrl
Add support for the new pinctrl API to the SPI drivers that handle the nRF SPI, SPIM, and SPIS peripherals. Update code of the drivers and related devicetree bindings. Signed-off-by: Andrzej Głąbek <[email protected]>
1 parent 2a87e04 commit 32b9e65

File tree

6 files changed

+164
-45
lines changed

6 files changed

+164
-45
lines changed

drivers/pinctrl/pinctrl_nrf.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,13 @@ int pinctrl_configure_pins(const pinctrl_soc_pin_t *pins, uint8_t pin_cnt,
106106
#if defined(NRF_PSEL_SPIM)
107107
case NRF_FUN_SPIM_SCK:
108108
NRF_PSEL_SPIM(reg, SCK) = NRF_GET_PIN(pins[i]);
109+
nrf_gpio_pin_write(NRF_GET_PIN(pins[i]), 0);
109110
nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_OUTPUT,
110111
NRF_GPIO_PIN_INPUT_CONNECT);
111112
break;
112113
case NRF_FUN_SPIM_MOSI:
113114
NRF_PSEL_SPIM(reg, MOSI) = NRF_GET_PIN(pins[i]);
115+
nrf_gpio_pin_write(NRF_GET_PIN(pins[i]), 0);
114116
nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_OUTPUT,
115117
NRF_GPIO_PIN_INPUT_DISCONNECT);
116118
break;

drivers/spi/spi_nrfx_spi.c

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <drivers/spi.h>
88
#include <pm/device.h>
9+
#include <drivers/pinctrl.h>
10+
#include <soc.h>
911
#include <nrfx_spi.h>
1012

1113
#include <logging/log.h>
@@ -24,6 +26,9 @@ struct spi_nrfx_data {
2426
struct spi_nrfx_config {
2527
nrfx_spi_t spi;
2628
nrfx_spi_config_t def_config;
29+
#ifdef CONFIG_PINCTRL
30+
const struct pinctrl_dev_config *pcfg;
31+
#endif
2732
};
2833

2934
static void event_handler(const nrfx_spi_evt_t *p_event, void *p_context);
@@ -266,7 +271,6 @@ static const struct spi_driver_api spi_nrfx_driver_api = {
266271
.release = spi_nrfx_release,
267272
};
268273

269-
270274
#ifdef CONFIG_PM_DEVICE
271275
static int spi_nrfx_pm_action(const struct device *dev,
272276
enum pm_device_action action)
@@ -277,8 +281,15 @@ static int spi_nrfx_pm_action(const struct device *dev,
277281

278282
switch (action) {
279283
case PM_DEVICE_ACTION_RESUME:
280-
/* No action needed at this point, nrfx_spi_init() will be
281-
* called at configuration before the next transfer.
284+
#ifdef CONFIG_PINCTRL
285+
ret = pinctrl_apply_state(dev_config->pcfg,
286+
PINCTRL_STATE_DEFAULT);
287+
if (ret < 0) {
288+
return ret;
289+
}
290+
#endif
291+
/* nrfx_spi_init() will be called at configuration before
292+
* the next transfer.
282293
*/
283294
break;
284295

@@ -287,6 +298,14 @@ static int spi_nrfx_pm_action(const struct device *dev,
287298
nrfx_spi_uninit(&dev_config->spi);
288299
dev_data->initialized = false;
289300
}
301+
302+
#ifdef CONFIG_PINCTRL
303+
ret = pinctrl_apply_state(dev_config->pcfg,
304+
PINCTRL_STATE_SLEEP);
305+
if (ret < 0) {
306+
return ret;
307+
}
308+
#endif
290309
break;
291310

292311
default:
@@ -317,9 +336,22 @@ static int spi_nrfx_pm_action(const struct device *dev,
317336
? NRF_GPIO_PIN_PULLDOWN \
318337
: NRF_GPIO_PIN_NOPULL)
319338

339+
#define SPI_NRFX_SPI_PIN_CFG(idx) \
340+
COND_CODE_1(CONFIG_PINCTRL, \
341+
(.skip_gpio_cfg = true, \
342+
.skip_psel_cfg = true,), \
343+
(.sck_pin = SPI_PROP(idx, sck_pin), \
344+
.mosi_pin = DT_PROP_OR(SPI(idx), mosi_pin, \
345+
NRFX_SPI_PIN_NOT_USED), \
346+
.miso_pin = DT_PROP_OR(SPI(idx), miso_pin, \
347+
NRFX_SPI_PIN_NOT_USED), \
348+
.miso_pull = SPI_NRFX_MISO_PULL(idx),))
349+
320350
#define SPI_NRFX_SPI_DEVICE(idx) \
321-
BUILD_ASSERT( \
322-
!SPI_PROP(idx, miso_pull_up) || !SPI_PROP(idx, miso_pull_down),\
351+
NRF_DT_ENSURE_PINS_ASSIGNED(SPI(idx), sck_pin); \
352+
BUILD_ASSERT(IS_ENABLED(CONFIG_PINCTRL) || \
353+
!(SPI_PROP(idx, miso_pull_up) && \
354+
SPI_PROP(idx, miso_pull_down)), \
323355
"SPI"#idx \
324356
": cannot enable both pull-up and pull-down on MISO line"); \
325357
static int spi_##idx##_init(const struct device *dev) \
@@ -328,6 +360,14 @@ static int spi_nrfx_pm_action(const struct device *dev,
328360
int err; \
329361
IRQ_CONNECT(DT_IRQN(SPI(idx)), DT_IRQ(SPI(idx), priority), \
330362
nrfx_isr, nrfx_spi_##idx##_irq_handler, 0); \
363+
IF_ENABLED(CONFIG_PINCTRL, ( \
364+
const struct spi_nrfx_config *dev_config = dev->config;\
365+
err = pinctrl_apply_state(dev_config->pcfg, \
366+
PINCTRL_STATE_DEFAULT); \
367+
if (err < 0) { \
368+
return err; \
369+
} \
370+
)) \
331371
err = spi_context_cs_configure_all(&dev_data->ctx); \
332372
if (err < 0) { \
333373
return err; \
@@ -342,16 +382,16 @@ static int spi_nrfx_pm_action(const struct device *dev,
342382
.dev = DEVICE_DT_GET(SPI(idx)), \
343383
.busy = false, \
344384
}; \
385+
IF_ENABLED(CONFIG_PINCTRL, (PINCTRL_DT_DEFINE(SPI(idx)))); \
345386
static const struct spi_nrfx_config spi_##idx##z_config = { \
346387
.spi = NRFX_SPI_INSTANCE(idx), \
347388
.def_config = { \
348-
.sck_pin = SPI_PROP(idx, sck_pin), \
349-
.mosi_pin = SPI_PROP(idx, mosi_pin), \
350-
.miso_pin = SPI_PROP(idx, miso_pin), \
351-
.ss_pin = NRFX_SPI_PIN_NOT_USED, \
352-
.orc = CONFIG_SPI_##idx##_NRF_ORC, \
353-
.miso_pull = SPI_NRFX_MISO_PULL(idx), \
354-
} \
389+
SPI_NRFX_SPI_PIN_CFG(idx) \
390+
.ss_pin = NRFX_SPI_PIN_NOT_USED, \
391+
.orc = CONFIG_SPI_##idx##_NRF_ORC, \
392+
}, \
393+
IF_ENABLED(CONFIG_PINCTRL, \
394+
(.pcfg = PINCTRL_DT_DEV_CONFIG_GET(SPI(idx)),)) \
355395
}; \
356396
PM_DEVICE_DT_DEFINE(SPI(idx), spi_nrfx_pm_action); \
357397
DEVICE_DT_DEFINE(SPI(idx), \

drivers/spi/spi_nrfx_spim.c

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <drivers/spi.h>
88
#include <pm/device.h>
9+
#include <drivers/pinctrl.h>
10+
#include <soc.h>
911
#ifdef CONFIG_SOC_NRF52832_ALLOW_SPIM_DESPITE_PAN_58
1012
#include <nrfx_gpiote.h>
1113
#include <nrfx_ppi.h>
@@ -40,8 +42,11 @@ struct spi_nrfx_config {
4042
size_t max_chunk_len;
4143
uint32_t max_freq;
4244
nrfx_spim_config_t def_config;
45+
#ifdef CONFIG_PINCTRL
46+
const struct pinctrl_dev_config *pcfg;
47+
#endif
4348
#ifdef CONFIG_SOC_NRF52832_ALLOW_SPIM_DESPITE_PAN_58
44-
bool anomaly_58_workaround;
49+
bool anomaly_58_workaround;
4550
#endif
4651
};
4752

@@ -435,8 +440,15 @@ static int spim_nrfx_pm_action(const struct device *dev,
435440

436441
switch (action) {
437442
case PM_DEVICE_ACTION_RESUME:
438-
/* No action needed at this point, nrfx_spim_init() will be
439-
* called at configuration before the next transfer.
443+
#ifdef CONFIG_PINCTRL
444+
ret = pinctrl_apply_state(dev_config->pcfg,
445+
PINCTRL_STATE_DEFAULT);
446+
if (ret < 0) {
447+
return ret;
448+
}
449+
#endif
450+
/* nrfx_spim_init() will be called at configuration before
451+
* the next transfer.
440452
*/
441453
break;
442454

@@ -445,6 +457,14 @@ static int spim_nrfx_pm_action(const struct device *dev,
445457
nrfx_spim_uninit(&dev_config->spim);
446458
dev_data->initialized = false;
447459
}
460+
461+
#ifdef CONFIG_PINCTRL
462+
ret = pinctrl_apply_state(dev_config->pcfg,
463+
PINCTRL_STATE_SLEEP);
464+
if (ret < 0) {
465+
return ret;
466+
}
467+
#endif
448468
break;
449469

450470
default:
@@ -468,11 +488,11 @@ static int spim_nrfx_pm_action(const struct device *dev,
468488
#define SPIM_NRFX_MISO_PULL_UP(idx) DT_PROP(SPIM(idx), miso_pull_up)
469489

470490
#define SPIM_NRFX_MISO_PULL(idx) \
471-
(SPIM_NRFX_MISO_PULL_UP(idx) \
472-
? SPIM_NRFX_MISO_PULL_DOWN(idx) \
491+
(SPIM_PROP(idx, miso_pull_up) \
492+
? SPIM_PROP(idx, miso_pull_down) \
473493
? -1 /* invalid configuration */\
474494
: NRF_GPIO_PIN_PULLUP \
475-
: SPIM_NRFX_MISO_PULL_DOWN(idx) \
495+
: SPIM_PROP(idx, miso_pull_down) \
476496
? NRF_GPIO_PIN_PULLDOWN \
477497
: NRF_GPIO_PIN_NOPULL)
478498

@@ -483,9 +503,22 @@ static int spim_nrfx_pm_action(const struct device *dev,
483503
(.rx_delay = CONFIG_SPI_##idx##_NRF_RX_DELAY,)) \
484504
))
485505

506+
#define SPI_NRFX_SPIM_PIN_CFG(idx) \
507+
COND_CODE_1(CONFIG_PINCTRL, \
508+
(.skip_gpio_cfg = true, \
509+
.skip_psel_cfg = true,), \
510+
(.sck_pin = SPIM_PROP(idx, sck_pin), \
511+
.mosi_pin = DT_PROP_OR(SPIM(idx), mosi_pin, \
512+
NRFX_SPIM_PIN_NOT_USED), \
513+
.miso_pin = DT_PROP_OR(SPIM(idx), miso_pin, \
514+
NRFX_SPIM_PIN_NOT_USED), \
515+
.miso_pull = SPIM_NRFX_MISO_PULL(idx),))
516+
486517
#define SPI_NRFX_SPIM_DEVICE(idx) \
487-
BUILD_ASSERT( \
488-
!SPIM_NRFX_MISO_PULL_UP(idx) || !SPIM_NRFX_MISO_PULL_DOWN(idx),\
518+
NRF_DT_ENSURE_PINS_ASSIGNED(SPIM(idx), sck_pin); \
519+
BUILD_ASSERT(IS_ENABLED(CONFIG_PINCTRL) || \
520+
!(SPIM_PROP(idx, miso_pull_up) && \
521+
SPIM_PROP(idx, miso_pull_down)), \
489522
"SPIM"#idx \
490523
": cannot enable both pull-up and pull-down on MISO line"); \
491524
static int spi_##idx##_init(const struct device *dev) \
@@ -495,6 +528,14 @@ static int spim_nrfx_pm_action(const struct device *dev,
495528
IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_SPIM##idx), \
496529
DT_IRQ(SPIM(idx), priority), \
497530
nrfx_isr, nrfx_spim_##idx##_irq_handler, 0); \
531+
IF_ENABLED(CONFIG_PINCTRL, ( \
532+
const struct spi_nrfx_config *dev_config = dev->config;\
533+
err = pinctrl_apply_state(dev_config->pcfg, \
534+
PINCTRL_STATE_DEFAULT); \
535+
if (err < 0) { \
536+
return err; \
537+
} \
538+
)) \
498539
err = spi_context_cs_configure_all(&dev_data->ctx); \
499540
if (err < 0) { \
500541
return err; \
@@ -511,23 +552,23 @@ static int spim_nrfx_pm_action(const struct device *dev,
511552
.dev = DEVICE_DT_GET(SPIM(idx)), \
512553
.busy = false, \
513554
}; \
555+
IF_ENABLED(CONFIG_PINCTRL, (PINCTRL_DT_DEFINE(SPIM(idx)))); \
514556
static const struct spi_nrfx_config spi_##idx##z_config = { \
515557
.spim = NRFX_SPIM_INSTANCE(idx), \
516558
.max_chunk_len = (1 << SPIM##idx##_EASYDMA_MAXCNT_SIZE) - 1, \
517559
.max_freq = SPIM##idx##_MAX_DATARATE * 1000000, \
518560
.def_config = { \
519-
.sck_pin = SPIM_PROP(idx, sck_pin), \
520-
.mosi_pin = SPIM_PROP(idx, mosi_pin), \
521-
.miso_pin = SPIM_PROP(idx, miso_pin), \
522-
.ss_pin = NRFX_SPIM_PIN_NOT_USED, \
523-
.orc = CONFIG_SPI_##idx##_NRF_ORC, \
524-
.miso_pull = SPIM_NRFX_MISO_PULL(idx), \
561+
SPI_NRFX_SPIM_PIN_CFG(idx) \
562+
.ss_pin = NRFX_SPIM_PIN_NOT_USED, \
563+
.orc = CONFIG_SPI_##idx##_NRF_ORC, \
525564
SPI_NRFX_SPIM_EXTENDED_CONFIG(idx) \
526565
}, \
527566
COND_CODE_1(CONFIG_SOC_NRF52832_ALLOW_SPIM_DESPITE_PAN_58, \
528567
(.anomaly_58_workaround = \
529568
SPIM_PROP(idx, anomaly_58_workaround),), \
530569
()) \
570+
IF_ENABLED(CONFIG_PINCTRL, \
571+
(.pcfg = PINCTRL_DT_DEV_CONFIG_GET(SPIM(idx)),)) \
531572
}; \
532573
PM_DEVICE_DT_DEFINE(SPIM(idx), spim_nrfx_pm_action); \
533574
DEVICE_DT_DEFINE(SPIM(idx), \

drivers/spi/spi_nrfx_spis.c

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66

77
#include <drivers/spi.h>
8+
#include <drivers/pinctrl.h>
9+
#include <soc.h>
810
#include <nrfx_spis.h>
911

1012
#include <logging/log.h>
@@ -19,6 +21,9 @@ struct spi_nrfx_data {
1921
struct spi_nrfx_config {
2022
nrfx_spis_t spis;
2123
size_t max_buf_len;
24+
#ifdef CONFIG_PINCTRL
25+
const struct pinctrl_dev_config *pcfg;
26+
#endif
2227
};
2328

2429
static inline nrf_spis_mode_t get_nrf_spis_mode(uint16_t operation)
@@ -208,7 +213,6 @@ static const struct spi_driver_api spi_nrfx_driver_api = {
208213
.release = spi_nrfx_release,
209214
};
210215

211-
212216
static void event_handler(const nrfx_spis_evt_t *p_event, void *p_context)
213217
{
214218
struct spi_nrfx_data *dev_data = p_context;
@@ -251,32 +255,52 @@ static int init_spis(const struct device *dev,
251255
#define SPIS(idx) DT_NODELABEL(spi##idx)
252256
#define SPIS_PROP(idx, prop) DT_PROP(SPIS(idx), prop)
253257

258+
#define SPI_NRFX_SPIS_PIN_CFG(idx) \
259+
COND_CODE_1(CONFIG_PINCTRL, \
260+
(.skip_gpio_cfg = true, \
261+
.skip_psel_cfg = true,), \
262+
(.sck_pin = SPIS_PROP(idx, sck_pin), \
263+
.mosi_pin = DT_PROP_OR(SPIS(idx), mosi_pin, \
264+
NRFX_SPIS_PIN_NOT_USED), \
265+
.miso_pin = DT_PROP_OR(SPIS(idx), miso_pin, \
266+
NRFX_SPIS_PIN_NOT_USED), \
267+
.csn_pin = SPIS_PROP(idx, csn_pin), \
268+
.csn_pullup = NRF_GPIO_PIN_NOPULL, \
269+
.miso_drive = NRF_GPIO_PIN_S0S1,))
270+
254271
#define SPI_NRFX_SPIS_DEVICE(idx) \
272+
NRF_DT_ENSURE_PINS_ASSIGNED(SPIS(idx), sck_pin); \
255273
static int spi_##idx##_init(const struct device *dev) \
256274
{ \
257275
IRQ_CONNECT(DT_IRQN(SPIS(idx)), DT_IRQ(SPIS(idx), priority), \
258276
nrfx_isr, nrfx_spis_##idx##_irq_handler, 0); \
259277
const nrfx_spis_config_t config = { \
260-
.sck_pin = SPIS_PROP(idx, sck_pin), \
261-
.mosi_pin = SPIS_PROP(idx, mosi_pin), \
262-
.miso_pin = SPIS_PROP(idx, miso_pin), \
263-
.csn_pin = SPIS_PROP(idx, csn_pin), \
264-
.mode = NRF_SPIS_MODE_0, \
265-
.bit_order = NRF_SPIS_BIT_ORDER_MSB_FIRST, \
266-
.csn_pullup = NRF_GPIO_PIN_NOPULL, \
267-
.miso_drive = NRF_GPIO_PIN_S0S1, \
268-
.orc = CONFIG_SPI_##idx##_NRF_ORC, \
269-
.def = SPIS_PROP(idx, def_char), \
278+
SPI_NRFX_SPIS_PIN_CFG(idx) \
279+
.mode = NRF_SPIS_MODE_0, \
280+
.bit_order = NRF_SPIS_BIT_ORDER_MSB_FIRST, \
281+
.orc = CONFIG_SPI_##idx##_NRF_ORC, \
282+
.def = SPIS_PROP(idx, def_char), \
270283
}; \
284+
IF_ENABLED(CONFIG_PINCTRL, ( \
285+
const struct spi_nrfx_config *dev_config = dev->config;\
286+
int err = pinctrl_apply_state(dev_config->pcfg, \
287+
PINCTRL_STATE_DEFAULT); \
288+
if (err < 0) { \
289+
return err; \
290+
} \
291+
)) \
271292
return init_spis(dev, &config); \
272293
} \
273294
static struct spi_nrfx_data spi_##idx##_data = { \
274295
SPI_CONTEXT_INIT_LOCK(spi_##idx##_data, ctx), \
275296
SPI_CONTEXT_INIT_SYNC(spi_##idx##_data, ctx), \
276297
}; \
298+
IF_ENABLED(CONFIG_PINCTRL, (PINCTRL_DT_DEFINE(SPIS(idx)))); \
277299
static const struct spi_nrfx_config spi_##idx##z_config = { \
278300
.spis = NRFX_SPIS_INSTANCE(idx), \
279301
.max_buf_len = BIT_MASK(SPIS##idx##_EASYDMA_MAXCNT_SIZE), \
302+
IF_ENABLED(CONFIG_PINCTRL, \
303+
(.pcfg = PINCTRL_DT_DEV_CONFIG_GET(SPIS(idx)),)) \
280304
}; \
281305
DEVICE_DT_DEFINE(SPIS(idx), \
282306
spi_##idx##_init, \

0 commit comments

Comments
 (0)