Skip to content

Commit b983847

Browse files
Tomasz BursztykaAnas Nashif
authored andcommitted
drivers/spi: Handle synchronous calls in a generic manner in DW driver
All SPI drivers have this same way to handle synchronous call, thus let's generalize it in struct spi_context, with a relevant API and apply the change into SPI DW driver. spi_context API will prove to be useful once asynchronous call will be handled as well, through the same completion functions used now only for synchronous call. It will be transparent for the driver. Signed-off-by: Tomasz Bursztyka <[email protected]>
1 parent 0bd83d2 commit b983847

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

drivers/spi/spi_context.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct spi_context {
2323
struct spi_config *config;
2424

2525
struct k_sem lock;
26+
struct k_sem sync;
2627

2728
const struct spi_buf **current_tx;
2829
struct spi_buf **current_rx;
@@ -36,6 +37,9 @@ struct spi_context {
3637
#define SPI_CONTEXT_INIT_LOCK(_data, _ctx_name) \
3738
._ctx_name.lock = K_SEM_INITIALIZER(_data._ctx_name.lock, 0, 1)
3839

40+
#define SPI_CONTEXT_INIT_SYNC(_data, _ctx_name) \
41+
._ctx_name.sync = K_SEM_INITIALIZER(_data._ctx_name.sync, 0, UINT_MAX)
42+
3943
static inline bool spi_context_configured(struct spi_context *ctx,
4044
struct spi_config *config)
4145
{
@@ -52,6 +56,16 @@ static inline void spi_context_release(struct spi_context *ctx)
5256
k_sem_give(&ctx->lock);
5357
}
5458

59+
static inline void spi_context_wait_for_completion(struct spi_context *ctx)
60+
{
61+
k_sem_take(&ctx->sync, K_FOREVER);
62+
}
63+
64+
static inline void spi_context_complete(struct spi_context *ctx)
65+
{
66+
k_sem_give(&ctx->sync);
67+
}
68+
5569
static inline void spi_context_cs_configure(struct spi_context *ctx)
5670
{
5771
if (ctx->config->cs) {

drivers/spi/spi_dw.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static void completed(struct device *dev, uint8_t error)
7575
SYS_LOG_DBG("SPI transaction completed %s error",
7676
error ? "with" : "without");
7777

78-
k_sem_give(&spi->device_sync_sem);
78+
spi_context_complete(&spi->ctx);
7979
}
8080

8181
static void push_data(struct device *dev)
@@ -306,7 +306,7 @@ static int spi_dw_transceive(struct device *dev,
306306
/* Enable the controller */
307307
set_bit_ssienr(info->regs);
308308

309-
k_sem_take(&spi->device_sync_sem, K_FOREVER);
309+
spi_context_wait_for_completion(&spi->ctx);
310310

311311
if (spi->error) {
312312
ret = -EIO;
@@ -362,8 +362,6 @@ int spi_dw_init(struct device *dev)
362362

363363
info->config_func();
364364

365-
k_sem_init(&spi->device_sync_sem, 0, UINT_MAX);
366-
367365
/* Masking interrupt and making sure controller is disabled */
368366
write_imr(DW_SPI_IMR_MASK, info->regs);
369367
clear_bit_ssienr(info->regs);
@@ -381,6 +379,7 @@ void spi_config_0_irq(void);
381379

382380
struct spi_dw_data spi_dw_data_port_0 = {
383381
SPI_CONTEXT_INIT_LOCK(spi_dw_data_port_0, ctx),
382+
SPI_CONTEXT_INIT_SYNC(spi_dw_data_port_0, ctx),
384383
};
385384

386385
const struct spi_dw_config spi_dw_config_0 = {
@@ -426,6 +425,7 @@ void spi_config_1_irq(void);
426425

427426
struct spi_dw_data spi_dw_data_port_1 = {
428427
SPI_CONTEXT_INIT_LOCK(spi_dw_data_port_1, ctx),
428+
SPI_CONTEXT_INIT_SYNC(spi_dw_data_port_1, ctx),
429429
};
430430

431431
static const struct spi_dw_config spi_dw_config_1 = {

drivers/spi/spi_dw.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ struct spi_dw_data {
5151
#ifdef CONFIG_SPI_DW_CLOCK_GATE
5252
struct device *clock;
5353
#endif /* CONFIG_SPI_DW_CLOCK_GATE */
54-
struct k_sem device_sync_sem;
5554
struct spi_context ctx;
5655
u8_t error;
5756
u8_t dfs; /* dfs in bytes: 1,2 or 4 */

0 commit comments

Comments
 (0)