Skip to content

Commit f3ea332

Browse files
committed
drivers: spi: cc23x0: Add power management
Add PM support to cc23x0 SPI module. Signed-off-by: Julien Panis <jpanis@baylibre.com>
1 parent ba905d0 commit f3ea332

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

drivers/spi/spi_cc23x0.c

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ LOG_MODULE_REGISTER(spi_cc23x0, CONFIG_SPI_LOG_LEVEL);
1414
#include <zephyr/drivers/pinctrl.h>
1515
#include <zephyr/drivers/spi.h>
1616
#include <zephyr/irq.h>
17+
#include <zephyr/pm/device.h>
18+
#include <zephyr/pm/policy.h>
1719
#include <zephyr/sys/util.h>
1820

1921
#include <driverlib/clkctl.h>
@@ -63,6 +65,22 @@ struct spi_cc23x0_data {
6365
#endif
6466
};
6567

68+
static inline void spi_cc23x0_pm_policy_state_lock_get(void)
69+
{
70+
#ifdef CONFIG_PM_DEVICE
71+
pm_policy_state_lock_get(PM_STATE_RUNTIME_IDLE, PM_ALL_SUBSTATES);
72+
pm_policy_state_lock_get(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
73+
#endif
74+
}
75+
76+
static inline void spi_cc23x0_pm_policy_state_lock_put(void)
77+
{
78+
#ifdef CONFIG_PM_DEVICE
79+
pm_policy_state_lock_put(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
80+
pm_policy_state_lock_put(PM_STATE_RUNTIME_IDLE, PM_ALL_SUBSTATES);
81+
#endif
82+
}
83+
6684
static void spi_cc23x0_isr(const struct device *dev)
6785
{
6886
const struct spi_cc23x0_config *cfg = dev->config;
@@ -307,6 +325,8 @@ static int spi_cc23x0_transceive(const struct device *dev,
307325
};
308326
#endif
309327

328+
spi_cc23x0_pm_policy_state_lock_get();
329+
310330
spi_context_lock(ctx, false, NULL, NULL, config);
311331

312332
ret = spi_cc23x0_configure(dev, config);
@@ -388,6 +408,7 @@ static int spi_cc23x0_transceive(const struct device *dev,
388408

389409
ctx_release:
390410
spi_context_release(ctx, ret);
411+
spi_cc23x0_pm_policy_state_lock_put();
391412
return ret;
392413
}
393414

@@ -446,6 +467,29 @@ static int spi_cc23x0_init(const struct device *dev)
446467
return 0;
447468
}
448469

470+
#ifdef CONFIG_PM_DEVICE
471+
472+
static int spi_cc23x0_pm_action(const struct device *dev, enum pm_device_action action)
473+
{
474+
const struct spi_cc23x0_config *cfg = dev->config;
475+
struct spi_cc23x0_data *data = dev->data;
476+
477+
switch (action) {
478+
case PM_DEVICE_ACTION_SUSPEND:
479+
SPIDisable(cfg->base);
480+
CLKCTLDisable(CLKCTL_BASE, CLKCTL_SPI0);
481+
return 0;
482+
case PM_DEVICE_ACTION_RESUME:
483+
/* Force SPI to be reconfigured at next transfer */
484+
data->ctx.config = NULL;
485+
return 0;
486+
default:
487+
return -ENOTSUP;
488+
}
489+
}
490+
491+
#endif /* CONFIG_PM_DEVICE */
492+
449493
#ifdef CONFIG_SPI_CC23X0_DMA_DRIVEN
450494
#define SPI_CC23X0_DMA_INIT(n) \
451495
.dma_dev = DEVICE_DT_GET(TI_CC23X0_DT_INST_DMA_CTLR(n, tx)), \
@@ -459,6 +503,7 @@ static int spi_cc23x0_init(const struct device *dev)
459503

460504
#define SPI_CC23X0_INIT(n) \
461505
PINCTRL_DT_INST_DEFINE(n); \
506+
PM_DEVICE_DT_INST_DEFINE(n, spi_cc23x0_pm_action); \
462507
\
463508
static void spi_irq_config_func_##n(void) \
464509
{ \
@@ -484,7 +529,7 @@ static int spi_cc23x0_init(const struct device *dev)
484529
\
485530
DEVICE_DT_INST_DEFINE(n, \
486531
spi_cc23x0_init, \
487-
NULL, \
532+
PM_DEVICE_DT_INST_GET(n), \
488533
&spi_cc23x0_data_##n, \
489534
&spi_cc23x0_config_##n, \
490535
POST_KERNEL, \

0 commit comments

Comments
 (0)