Skip to content

Commit a1cfd81

Browse files
[nrf fromtree] drivers: adc: nrf_saadc: add pm device support
Extend nrf saadc device driver with pm device runtime support. To preserve previous behavior: * if pm device is disabled, saadc is resumed on sampling start and suspended when sampling done. * if pm device is enabled only, saadc does nothing on sampling start/stop. its resumed on init. * if pm device runtime is enabled, saadc is got on sampling start, and put on sampling stop. Signed-off-by: Bjarki Arge Andreasen <[email protected]> (cherry picked from commit 96c3cd6)
1 parent 097b37e commit a1cfd81

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

drivers/adc/adc_nrfx_saadc.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <zephyr/dt-bindings/adc/nrf-saadc-v3.h>
1111
#include <zephyr/dt-bindings/adc/nrf-saadc-nrf54l.h>
1212
#include <zephyr/linker/devicetree_regions.h>
13+
#include <zephyr/pm/device.h>
14+
#include <zephyr/pm/device_runtime.h>
1315

1416
#define LOG_LEVEL CONFIG_ADC_LOG_LEVEL
1517
#include <zephyr/logging/log.h>
@@ -168,6 +170,26 @@ static int adc_convert_acq_time(uint16_t acquisition_time, nrf_saadc_acqtime_t *
168170
return result;
169171
}
170172

173+
static int saadc_pm_hook(const struct device *dev, enum pm_device_action action)
174+
{
175+
ARG_UNUSED(dev);
176+
177+
switch (action) {
178+
case PM_DEVICE_ACTION_SUSPEND:
179+
nrf_saadc_disable(NRF_SAADC);
180+
return 0;
181+
182+
case PM_DEVICE_ACTION_RESUME:
183+
nrf_saadc_enable(NRF_SAADC);
184+
return 0;
185+
186+
default:
187+
break;
188+
}
189+
190+
return -ENOTSUP;
191+
}
192+
171193
/* Implementation of the ADC driver API function: adc_channel_setup. */
172194
static int adc_nrfx_channel_setup(const struct device *dev,
173195
const struct adc_channel_cfg *channel_cfg)
@@ -320,7 +342,11 @@ static int adc_nrfx_channel_setup(const struct device *dev,
320342

321343
static void adc_context_start_sampling(struct adc_context *ctx)
322344
{
345+
#if defined(CONFIG_PM_DEVICE_RUNTIME)
346+
pm_device_runtime_get(DEVICE_DT_INST_GET(0));
347+
#else
323348
nrf_saadc_enable(NRF_SAADC);
349+
#endif
324350

325351
if (ctx->sequence.calibrate) {
326352
nrf_saadc_task_trigger(NRF_SAADC,
@@ -623,7 +649,12 @@ static void saadc_irq_handler(const struct device *dev)
623649
nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_END);
624650

625651
nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_STOP);
652+
653+
#if defined(CONFIG_PM_DEVICE_RUNTIME)
654+
pm_device_runtime_put(DEVICE_DT_INST_GET(0));
655+
#else
626656
nrf_saadc_disable(NRF_SAADC);
657+
#endif
627658

628659
if (has_single_ended(&m_data.ctx.sequence)) {
629660
correct_single_ended(&m_data.ctx.sequence);
@@ -663,7 +694,7 @@ static int init_saadc(const struct device *dev)
663694

664695
adc_context_unlock_unconditionally(&m_data.ctx);
665696

666-
return 0;
697+
return pm_device_driver_init(dev, saadc_pm_hook);
667698
}
668699

669700
static DEVICE_API(adc, adc_nrfx_driver_api) = {
@@ -693,9 +724,10 @@ static DEVICE_API(adc, adc_nrfx_driver_api) = {
693724
#define SAADC_INIT(inst) \
694725
BUILD_ASSERT((inst) == 0, \
695726
"multiple instances not supported"); \
727+
PM_DEVICE_DT_INST_DEFINE(0, saadc_pm_hook, 1); \
696728
DEVICE_DT_INST_DEFINE(0, \
697729
init_saadc, \
698-
NULL, \
730+
PM_DEVICE_DT_INST_GET(0), \
699731
NULL, \
700732
NULL, \
701733
POST_KERNEL, \

0 commit comments

Comments
 (0)