Skip to content

Commit bd52723

Browse files
committed
drivers: spi: spi_context: exclude gpio code if no gpio cs
exclude gpio code if no gpio cs are used for that spi driver. Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
1 parent 732a3a5 commit bd52723

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

drivers/spi/spi_context.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
extern "C" {
2222
#endif
2323

24+
#if defined(DT_DRV_COMPAT) && !DT_ANY_INST_HAS_PROP_STATUS_OKAY(cs_gpios)
25+
#define DT_SPI_CTX_HAS_NO_CS_GPIOS
26+
#endif
27+
2428
enum spi_ctx_runtime_op_mode {
2529
SPI_CTX_RUNTIME_OP_MODE_MASTER = BIT(0),
2630
SPI_CTX_RUNTIME_OP_MODE_SLAVE = BIT(1),
@@ -31,8 +35,10 @@ struct spi_context {
3135
#ifdef CONFIG_MULTITHREADING
3236
const struct spi_config *owner;
3337
#endif
38+
#ifndef DT_SPI_CTX_HAS_NO_CS_GPIOS
3439
const struct gpio_dt_spec *cs_gpios;
3540
size_t num_cs_gpios;
41+
#endif /* !DT_SPI_CTX_HAS_NO_CS_GPIOS */
3642

3743
#ifdef CONFIG_MULTITHREADING
3844
struct k_sem lock;
@@ -71,6 +77,7 @@ struct spi_context {
7177
#define SPI_CONTEXT_INIT_SYNC(_data, _ctx_name) \
7278
._ctx_name.sync = Z_SEM_INITIALIZER(_data._ctx_name.sync, 0, 1)
7379

80+
#ifndef DT_SPI_CTX_HAS_NO_CS_GPIOS
7481
#define SPI_CONTEXT_CS_GPIO_SPEC_ELEM(_node_id, _prop, _idx) \
7582
GPIO_DT_SPEC_GET_BY_IDX(_node_id, _prop, _idx),
7683

@@ -84,6 +91,9 @@ struct spi_context {
8491
(SPI_CONTEXT_CS_GPIOS_FOREACH_ELEM(_node_id)), ({0})) \
8592
}, \
8693
._ctx_name.num_cs_gpios = DT_PROP_LEN_OR(_node_id, cs_gpios, 0),
94+
#else /* DT_SPI_CTX_HAS_NO_CS_GPIOS */
95+
#define SPI_CONTEXT_CS_GPIOS_INITIALIZE(...)
96+
#endif /* DT_SPI_CTX_HAS_NO_CS_GPIOS */
8797

8898
/*
8999
* Checks if a spi config is the same as the one stored in the spi_context
@@ -300,8 +310,9 @@ static inline void spi_context_complete(struct spi_context *ctx,
300310
* Note: If a controller has native CS control in SPI hardware, they should also be initialized
301311
* during device init by the driver with hardware-specific code.
302312
*/
303-
static inline int spi_context_cs_configure_all(struct spi_context *ctx)
313+
static inline int spi_context_cs_configure_all(struct spi_context *ctx __maybe_unused)
304314
{
315+
#ifndef DT_SPI_CTX_HAS_NO_CS_GPIOS
305316
int ret;
306317
const struct gpio_dt_spec *cs_gpio;
307318

@@ -317,13 +328,16 @@ static inline int spi_context_cs_configure_all(struct spi_context *ctx)
317328
return ret;
318329
}
319330
}
331+
#endif /* !DT_SPI_CTX_HAS_NO_CS_GPIOS */
320332

321333
return 0;
322334
}
323335

324336
/* Helper function to power manage the GPIO CS pins, not meant to be used directly by drivers */
325-
static inline int _spi_context_cs_pm_all(struct spi_context *ctx, bool get)
337+
static inline int _spi_context_cs_pm_all(struct spi_context *ctx __maybe_unused,
338+
bool get __maybe_unused)
326339
{
340+
#ifndef DT_SPI_CTX_HAS_NO_CS_GPIOS
327341
const struct gpio_dt_spec *cs_gpio;
328342
int ret;
329343

@@ -338,6 +352,7 @@ static inline int _spi_context_cs_pm_all(struct spi_context *ctx, bool get)
338352
return ret;
339353
}
340354
}
355+
#endif /* !DT_SPI_CTX_HAS_NO_CS_GPIOS */
341356

342357
return 0;
343358
}
@@ -361,9 +376,10 @@ static inline int spi_context_cs_put_all(struct spi_context *ctx)
361376
}
362377

363378
/* Helper function to control the GPIO CS, not meant to be used directly by drivers */
364-
static inline void _spi_context_cs_control(struct spi_context *ctx,
365-
bool on, bool force_off)
379+
static inline void _spi_context_cs_control(struct spi_context *ctx __maybe_unused,
380+
bool on __maybe_unused, bool force_off __maybe_unused)
366381
{
382+
#ifndef DT_SPI_CTX_HAS_NO_CS_GPIOS
367383
if (ctx->config && spi_cs_is_gpio(ctx->config)) {
368384
if (on) {
369385
gpio_pin_set_dt(&ctx->config->cs.gpio, 1);
@@ -378,6 +394,7 @@ static inline void _spi_context_cs_control(struct spi_context *ctx,
378394
gpio_pin_set_dt(&ctx->config->cs.gpio, 0);
379395
}
380396
}
397+
#endif /* !DT_SPI_CTX_HAS_NO_CS_GPIOS */
381398
}
382399

383400
/* This function should be called by drivers to control the chip select line in master mode

drivers/spi/spi_esp32_spim.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static int IRAM_ATTR spi_esp32_transfer(const struct device *dev)
106106

107107
/* keep cs line active until last transmission */
108108
hal_trans->cs_keep_active =
109-
(!ctx->num_cs_gpios &&
109+
(UTIL_OR(IS_ENABLED(DT_SPI_CTX_HAS_NO_CS_GPIOS), (ctx->num_cs_gpios == 0)) &&
110110
(ctx->rx_count > 1 || ctx->tx_count > 1 || ctx->rx_len > transfer_len_frames ||
111111
ctx->tx_len > transfer_len_frames));
112112

@@ -382,9 +382,10 @@ static int IRAM_ATTR spi_esp32_configure(const struct device *dev,
382382
* Workaround for ESP32S3 and ESP32Cx SoC's. This dummy transaction is needed
383383
* to sync CLK and software controlled CS when SPI is in mode 3
384384
*/
385-
#if defined(CONFIG_SOC_SERIES_ESP32S3) || defined(CONFIG_SOC_SERIES_ESP32C2) || \
386-
defined(CONFIG_SOC_SERIES_ESP32C3) || defined(CONFIG_SOC_SERIES_ESP32C6)
387-
if (ctx->num_cs_gpios && (hal_dev->mode & (SPI_MODE_CPOL | SPI_MODE_CPHA))) {
385+
#if (defined(CONFIG_SOC_SERIES_ESP32S3) || defined(CONFIG_SOC_SERIES_ESP32C2) || \
386+
defined(CONFIG_SOC_SERIES_ESP32C3) || defined(CONFIG_SOC_SERIES_ESP32C6)) && \
387+
!defined(DT_SPI_CTX_HAS_NO_CS_GPIOS)
388+
if ((ctx->num_cs_gpios != 0) && (hal_dev->mode & (SPI_MODE_CPOL | SPI_MODE_CPHA))) {
388389
spi_esp32_transfer(dev);
389390
}
390391
#endif

drivers/spi/spi_numaker.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ static int spi_numaker_configure(const struct device *dev, const struct spi_conf
129129
/* Enable the automatic hardware slave select function. Select the SS pin and configure as
130130
* low-active.
131131
*/
132-
if (data->ctx.num_cs_gpios != 0) {
133-
SPI_EnableAutoSS(dev_cfg->spi, SPI_SS, SPI_SS_ACTIVE_LOW);
134-
} else {
132+
if (UTIL_OR(IS_ENABLED(DT_SPI_CTX_HAS_NO_CS_GPIOS), data->ctx.num_cs_gpios == 0)) {
135133
SPI_DisableAutoSS(dev_cfg->spi);
134+
} else {
135+
SPI_EnableAutoSS(dev_cfg->spi, SPI_SS, SPI_SS_ACTIVE_LOW);
136136
}
137137

138138
/* Be able to set TX/RX FIFO threshold, for ex: SPI_SetFIFO(dev_cfg->spi, 2, 2) */

0 commit comments

Comments
 (0)