Skip to content

Commit c2913ad

Browse files
Tomasz BursztykaAnas Nashif
authored andcommitted
drivers/spi: Handle ressource locking and release in DW driver
Again this is made as generic as possible through driver's spi_context API. Signed-off-by: Tomasz Bursztyka <[email protected]>
1 parent 10c1e49 commit c2913ad

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

drivers/spi/spi_context.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ static inline void spi_context_lock(struct spi_context *ctx,
6464

6565
static inline void spi_context_release(struct spi_context *ctx, int status)
6666
{
67+
if (!status && (ctx->config->operation & SPI_LOCK_ON)) {
68+
return;
69+
}
70+
6771
#ifdef CONFIG_POLL
6872
if (!ctx->asynchronous || status) {
6973
k_sem_give(&ctx->lock);
@@ -73,6 +77,13 @@ static inline void spi_context_release(struct spi_context *ctx, int status)
7377
#endif
7478
}
7579

80+
static inline void spi_context_unlock_unconditionally(struct spi_context *ctx)
81+
{
82+
if (!k_sem_count_get(&ctx->lock)) {
83+
k_sem_give(&ctx->lock);
84+
}
85+
}
86+
7687
static inline void spi_context_wait_for_completion(struct spi_context *ctx)
7788
{
7889
#ifdef CONFIG_POLL
@@ -94,7 +105,9 @@ static inline void spi_context_complete(struct spi_context *ctx, int status)
94105
k_poll_signal(ctx->signal, status);
95106
}
96107

97-
k_sem_give(&ctx->lock);
108+
if (!(ctx->config->operation & SPI_LOCK_ON)) {
109+
k_sem_give(&ctx->lock);
110+
}
98111
}
99112
#else
100113
k_sem_give(&ctx->sync);

drivers/spi/spi_dw.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,16 @@ static int spi_dw_transceive_async(struct spi_config *config,
339339

340340
static int spi_dw_release(struct spi_config *config)
341341
{
342+
const struct spi_dw_config *info = config->dev->config->config_info;
343+
struct spi_dw_data *spi = config->dev->driver_data;
344+
345+
if (!spi_context_configured(&spi->ctx, config) ||
346+
test_bit_ssienr(info->regs) || test_bit_sr_busy(info->regs)) {
347+
return -EBUSY;
348+
}
349+
350+
spi_context_unlock_unconditionally(&spi->ctx);
351+
342352
return 0;
343353
}
344354

0 commit comments

Comments
 (0)