Skip to content

Commit 9545ce0

Browse files
committed
drivers: adc: cc23x0: Add power management
Add PM support to cc23x0 ADC module. Signed-off-by: Julien Panis <jpanis@baylibre.com>
1 parent 179045e commit 9545ce0

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

drivers/adc/adc_cc23x0.c

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ LOG_MODULE_REGISTER(adc_cc23x0, CONFIG_ADC_LOG_LEVEL);
1414
#include <zephyr/drivers/dma.h>
1515
#include <zephyr/drivers/pinctrl.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/adc.h>
@@ -72,8 +74,27 @@ struct adc_cc23x0_data {
7274
uint8_t ch_count;
7375
uint8_t mem_index;
7476
uint16_t *buffer;
77+
#ifdef CONFIG_PM_DEVICE
78+
bool configured;
79+
#endif
7580
};
7681

82+
static inline void adc_cc23x0_pm_policy_state_lock_get(void)
83+
{
84+
#ifdef CONFIG_PM_DEVICE
85+
pm_policy_state_lock_get(PM_STATE_RUNTIME_IDLE, PM_ALL_SUBSTATES);
86+
pm_policy_state_lock_get(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
87+
#endif
88+
}
89+
90+
static inline void adc_cc23x0_pm_policy_state_lock_put(void)
91+
{
92+
#ifdef CONFIG_PM_DEVICE
93+
pm_policy_state_lock_put(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
94+
pm_policy_state_lock_put(PM_STATE_RUNTIME_IDLE, PM_ALL_SUBSTATES);
95+
#endif
96+
}
97+
7798
static void adc_context_start_sampling(struct adc_context *ctx)
7899
{
79100
struct adc_cc23x0_data *data = CONTAINER_OF(ctx, struct adc_cc23x0_data, ctx);
@@ -115,6 +136,8 @@ static void adc_context_start_sampling(struct adc_context *ctx)
115136
data->mem_index = 0;
116137
#endif
117138

139+
adc_cc23x0_pm_policy_state_lock_get();
140+
118141
ADCManualTrigger();
119142
}
120143

@@ -144,6 +167,7 @@ static void adc_cc23x0_isr(const struct device *dev)
144167
*/
145168
ADCClearInterrupt(ADC_INT_DMADONE);
146169
LOG_DBG("DMA done");
170+
adc_cc23x0_pm_policy_state_lock_put();
147171
adc_context_on_sampling_done(&data->ctx, dev);
148172
#else
149173
/*
@@ -174,6 +198,7 @@ static void adc_cc23x0_isr(const struct device *dev)
174198
/* Trigger next conversion */
175199
ADCManualTrigger();
176200
} else {
201+
adc_cc23x0_pm_policy_state_lock_put();
177202
adc_context_on_sampling_done(&data->ctx, dev);
178203
}
179204
#endif
@@ -462,6 +487,10 @@ static int adc_cc23x0_channel_setup(const struct device *dev,
462487
return -EINVAL;
463488
}
464489

490+
#ifdef CONFIG_PM_DEVICE
491+
data->configured = true;
492+
#endif
493+
465494
return 0;
466495
}
467496

@@ -497,6 +526,34 @@ static int adc_cc23x0_init(const struct device *dev)
497526
return 0;
498527
}
499528

529+
#ifdef CONFIG_PM_DEVICE
530+
531+
static int adc_cc23x0_pm_action(const struct device *dev, enum pm_device_action action)
532+
{
533+
struct adc_cc23x0_data *data = dev->data;
534+
535+
switch (action) {
536+
case PM_DEVICE_ACTION_SUSPEND:
537+
CLKCTLDisable(CLKCTL_BASE, CLKCTL_ADC0);
538+
return 0;
539+
case PM_DEVICE_ACTION_RESUME:
540+
CLKCTLEnable(CLKCTL_BASE, CLKCTL_ADC0);
541+
ADCEnableInterrupt(ADC_CC23X0_INT_MASK);
542+
543+
/* Restore context if needed */
544+
if (data->configured) {
545+
ADCSetSampleDuration(adc_cc23x0_clkdiv_to_field(data->clk_div),
546+
data->clk_cycles);
547+
}
548+
549+
return 0;
550+
default:
551+
return -ENOTSUP;
552+
}
553+
}
554+
555+
#endif /* CONFIG_PM_DEVICE */
556+
500557
static const struct adc_driver_api adc_cc23x0_driver_api = {
501558
.channel_setup = adc_cc23x0_channel_setup,
502559
.read = adc_cc23x0_read,
@@ -517,6 +574,7 @@ static const struct adc_driver_api adc_cc23x0_driver_api = {
517574

518575
#define CC23X0_ADC_INIT(n) \
519576
PINCTRL_DT_INST_DEFINE(n); \
577+
PM_DEVICE_DT_INST_DEFINE(n, adc_cc23x0_pm_action); \
520578
\
521579
static void adc_cc23x0_cfg_func_##n(void) \
522580
{ \
@@ -542,7 +600,7 @@ static const struct adc_driver_api adc_cc23x0_driver_api = {
542600
\
543601
DEVICE_DT_INST_DEFINE(n, \
544602
&adc_cc23x0_init, \
545-
NULL, \
603+
PM_DEVICE_DT_INST_GET(n), \
546604
&adc_cc23x0_data_##n, \
547605
&adc_cc23x0_config_##n, \
548606
POST_KERNEL, \

0 commit comments

Comments
 (0)