Skip to content

Commit 3f4f896

Browse files
maass-hamburgkartben
authored andcommitted
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ß <[email protected]>
1 parent 0598b2c commit 3f4f896

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

drivers/spi/spi_context.h

Lines changed: 19 additions & 1 deletion
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 1
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
@@ -292,6 +302,13 @@ static inline void spi_context_complete(struct spi_context *ctx,
292302
#endif /* CONFIG_SPI_ASYNC */
293303
}
294304

305+
#ifdef DT_SPI_CTX_HAS_NO_CS_GPIOS
306+
#define spi_context_cs_configure_all(...) 0
307+
#define spi_context_cs_get_all(...) 0
308+
#define spi_context_cs_put_all(...) 0
309+
#define _spi_context_cs_control(...) (void) 0
310+
#define spi_context_cs_control(...) (void) 0
311+
#else /* DT_SPI_CTX_HAS_NO_CS_GPIOS */
295312
/*
296313
* This function initializes all the chip select GPIOs associated with a spi controller.
297314
* The context first must be initialized using the SPI_CONTEXT_CS_GPIOS_INITIALIZE macro.
@@ -391,12 +408,13 @@ static inline void spi_context_cs_control(struct spi_context *ctx, bool on)
391408
{
392409
_spi_context_cs_control(ctx, on, false);
393410
}
411+
#endif /* DT_SPI_CTX_HAS_NO_CS_GPIOS */
394412

395413
/* Forcefully releases the spi context and removes the owner, allowing taking the lock
396414
* with spi_context_lock without the previous owner releasing the lock.
397415
* This is usually used to aid in implementation of the spi_release driver API.
398416
*/
399-
static inline void spi_context_unlock_unconditionally(struct spi_context *ctx)
417+
static inline void spi_context_unlock_unconditionally(struct spi_context *ctx __maybe_unused)
400418
{
401419
/* Forcing CS to go to inactive status */
402420
_spi_context_cs_control(ctx, false, true);

drivers/spi/spi_esp32_spim.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static bool spi_esp32_transfer_ongoing(struct spi_esp32_data *data)
3737
}
3838

3939
static inline void spi_esp32_complete(const struct device *dev,
40-
struct spi_esp32_data *data,
40+
struct spi_esp32_data *data __maybe_unused,
4141
spi_dev_t *spi, int status)
4242
{
4343
#ifdef CONFIG_SPI_ESP32_INTERRUPT
@@ -177,7 +177,7 @@ static int IRAM_ATTR spi_esp32_transfer(const struct device *dev)
177177

178178
/* keep cs line active until last transmission */
179179
hal_trans->cs_keep_active =
180-
(!ctx->num_cs_gpios &&
180+
(UTIL_OR(IS_ENABLED(DT_SPI_CTX_HAS_NO_CS_GPIOS), (ctx->num_cs_gpios == 0)) &&
181181
(ctx->rx_count > 1 || ctx->tx_count > 1 || ctx->rx_len > transfer_len_frames ||
182182
ctx->tx_len > transfer_len_frames));
183183

@@ -488,9 +488,10 @@ static int IRAM_ATTR spi_esp32_configure(const struct device *dev,
488488
* Workaround for ESP32S3 and ESP32Cx SoC's. This dummy transaction is needed
489489
* to sync CLK and software controlled CS when SPI is in mode 3
490490
*/
491-
#if defined(CONFIG_SOC_SERIES_ESP32S3) || defined(CONFIG_SOC_SERIES_ESP32C2) || \
492-
defined(CONFIG_SOC_SERIES_ESP32C3) || defined(CONFIG_SOC_SERIES_ESP32C6)
493-
if (ctx->num_cs_gpios && (hal_dev->mode & (SPI_MODE_CPOL | SPI_MODE_CPHA))) {
491+
#if (defined(CONFIG_SOC_SERIES_ESP32S3) || defined(CONFIG_SOC_SERIES_ESP32C2) || \
492+
defined(CONFIG_SOC_SERIES_ESP32C3) || defined(CONFIG_SOC_SERIES_ESP32C6)) && \
493+
!defined(DT_SPI_CTX_HAS_NO_CS_GPIOS)
494+
if ((ctx->num_cs_gpios != 0) && (hal_dev->mode & (SPI_MODE_CPOL | SPI_MODE_CPHA))) {
494495
spi_esp32_transfer(dev);
495496
}
496497
#endif

drivers/spi/spi_ll_stm32.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,9 @@ static int spi_stm32_shift_frames(const struct spi_stm32_config *cfg,
468468
return spi_stm32_get_err(cfg->spi);
469469
}
470470

471-
static void spi_stm32_cs_control(const struct device *dev, bool on)
471+
static void spi_stm32_cs_control(const struct device *dev, bool on __maybe_unused)
472472
{
473-
struct spi_stm32_data *data = dev->data;
473+
__maybe_unused struct spi_stm32_data *data = dev->data;
474474

475475
spi_context_cs_control(&data->ctx, on);
476476

drivers/spi/spi_numaker.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ 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) {
132+
if (UTIL_OR(IS_ENABLED(DT_SPI_CTX_HAS_NO_CS_GPIOS), (data->ctx.num_cs_gpios == 0))) {
133133
SPI_EnableAutoSS(dev_cfg->spi, SPI_SS, SPI_SS_ACTIVE_LOW);
134134
} else {
135135
SPI_DisableAutoSS(dev_cfg->spi);

0 commit comments

Comments
 (0)