spi: convert CS usage to gpio_dt_spec

Convert all CS control logic to be based on the `gpio_dt_spec` member
instead of the standalone `port`, `pin` and `flags` members.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Co-authored-by: Jordan Yates <jordan.yates@data61.csiro.au>
Signed-off-by: Bartosz Bilas <bartosz.bilas@hotmail.com>
Co-authored-by: Bartosz Bilas <bartosz.bilas@hotmail.com>
This commit is contained in:
Jordan Yates 2021-08-01 15:30:40 +10:00 committed by Christopher Friedt
commit 29773391c7
2 changed files with 21 additions and 20 deletions

View file

@ -183,29 +183,34 @@ gpio_dt_flags_t spi_context_cs_active_level(struct spi_context *ctx)
return GPIO_ACTIVE_LOW;
}
static inline void spi_context_cs_configure(struct spi_context *ctx)
static inline int spi_context_cs_configure(struct spi_context *ctx)
{
if (ctx->config->cs && ctx->config->cs->gpio_dev) {
int ret;
if (ctx->config->cs && ctx->config->cs->gpio.port) {
/* Validate CS active levels are equivalent */
__ASSERT(spi_context_cs_active_level(ctx) ==
(ctx->config->cs->gpio_dt_flags & GPIO_ACTIVE_LOW),
(ctx->config->cs->gpio.dt_flags & GPIO_ACTIVE_LOW),
"Devicetree and spi_context CS levels are not equal");
gpio_pin_configure(ctx->config->cs->gpio_dev,
ctx->config->cs->gpio_pin,
ctx->config->cs->gpio_dt_flags |
GPIO_OUTPUT_INACTIVE);
ret = gpio_pin_configure_dt(&ctx->config->cs->gpio,
GPIO_OUTPUT_INACTIVE);
if (ret < 0) {
LOG_ERR("Failed to configure 'cs' gpio: %d", ret);
return ret;
}
} else {
LOG_INF("CS control inhibited (no GPIO device)");
}
return 0;
}
static inline void _spi_context_cs_control(struct spi_context *ctx,
bool on, bool force_off)
{
if (ctx->config && ctx->config->cs && ctx->config->cs->gpio_dev) {
if (ctx->config && ctx->config->cs && ctx->config->cs->gpio.port) {
if (on) {
gpio_pin_set(ctx->config->cs->gpio_dev,
ctx->config->cs->gpio_pin, 1);
gpio_pin_set_dt(&ctx->config->cs->gpio, 1);
k_busy_wait(ctx->config->cs->delay);
} else {
if (!force_off &&
@ -214,8 +219,7 @@ static inline void _spi_context_cs_control(struct spi_context *ctx,
}
k_busy_wait(ctx->config->cs->delay);
gpio_pin_set(ctx->config->cs->gpio_dev,
ctx->config->cs->gpio_pin, 0);
gpio_pin_set_dt(&ctx->config->cs->gpio, 0);
}
}
}

View file

@ -193,13 +193,10 @@ struct spi_cs_control {
* @param delay_ The @p delay field to set in the @p spi_cs_control
* @return a pointer to the @p spi_cs_control structure
*/
#define SPI_CS_CONTROL_PTR_DT(node_id, delay_) \
(&(struct spi_cs_control) { \
.gpio_dev = DEVICE_DT_GET( \
DT_SPI_DEV_CS_GPIOS_CTLR(node_id)), \
.delay = (delay_), \
.gpio_pin = DT_SPI_DEV_CS_GPIOS_PIN(node_id), \
.gpio_dt_flags = DT_SPI_DEV_CS_GPIOS_FLAGS(node_id), \
#define SPI_CS_CONTROL_PTR_DT(node_id, delay_) \
(&(struct spi_cs_control) { \
.gpio = DT_SPI_DEV_CS_GPIOS_DT_SPEC_GET(node_id), \
.delay = (delay_), \
})
/**
@ -439,7 +436,7 @@ static inline bool spi_is_ready(const struct spi_dt_spec *spec)
}
/* Validate CS gpio port is ready, if it is used */
if (spec->config.cs &&
!device_is_ready(spec->config.cs->gpio_dev)) {
!device_is_ready(spec->config.cs->gpio.port)) {
return false;
}
return true;