drivers: current sense amp: add pm

Enable power management for current sense amp.

Signed-off-by: Nick Ward <nix.ward@gmail.com>
This commit is contained in:
Nick Ward 2023-09-23 01:34:09 +10:00 committed by Maureen Helm
commit 3dba54b92d
3 changed files with 67 additions and 3 deletions

View file

@ -8,7 +8,9 @@
#include <zephyr/drivers/adc.h> #include <zephyr/drivers/adc.h>
#include <zephyr/drivers/adc/current_sense_amplifier.h> #include <zephyr/drivers/adc/current_sense_amplifier.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/sensor.h> #include <zephyr/drivers/sensor.h>
#include <zephyr/pm/device.h>
#include <zephyr/logging/log.h> #include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(current_amp, CONFIG_SENSOR_LOG_LEVEL); LOG_MODULE_REGISTER(current_amp, CONFIG_SENSOR_LOG_LEVEL);
@ -73,6 +75,40 @@ static const struct sensor_driver_api current_api = {
.channel_get = get, .channel_get = get,
}; };
#ifdef CONFIG_PM_DEVICE
static int pm_action(const struct device *dev, enum pm_device_action action)
{
const struct current_sense_amplifier_dt_spec *config = dev->config;
int ret;
if (config->power_gpio.port == NULL) {
LOG_ERR("PM not supported");
return -ENOTSUP;
}
switch (action) {
case PM_DEVICE_ACTION_RESUME:
ret = gpio_pin_set_dt(&config->power_gpio, 1);
if (ret != 0) {
LOG_ERR("failed to set GPIO for PM resume");
return ret;
}
break;
case PM_DEVICE_ACTION_SUSPEND:
ret = gpio_pin_set_dt(&config->power_gpio, 0);
if (ret != 0) {
LOG_ERR("failed to set GPIO for PM suspend");
return ret;
}
break;
default:
return -ENOTSUP;
}
return 0;
}
#endif
static int current_init(const struct device *dev) static int current_init(const struct device *dev)
{ {
const struct current_sense_amplifier_dt_spec *config = dev->config; const struct current_sense_amplifier_dt_spec *config = dev->config;
@ -84,6 +120,21 @@ static int current_init(const struct device *dev)
return -ENODEV; return -ENODEV;
} }
#ifdef CONFIG_PM_DEVICE
if (config->power_gpio.port != NULL) {
if (!gpio_is_ready_dt(&config->power_gpio)) {
LOG_ERR("Power GPIO is not ready");
return -ENODEV;
}
ret = gpio_pin_configure_dt(&config->power_gpio, GPIO_OUTPUT_ACTIVE);
if (ret != 0) {
LOG_ERR("failed to config GPIO: %d", ret);
return ret;
}
}
#endif
ret = adc_channel_setup_dt(&config->port); ret = adc_channel_setup_dt(&config->port);
if (ret != 0) { if (ret != 0) {
LOG_ERR("setup: %d", ret); LOG_ERR("setup: %d", ret);
@ -108,8 +159,10 @@ static int current_init(const struct device *dev)
static const struct current_sense_amplifier_dt_spec current_amp_##inst##_config = \ static const struct current_sense_amplifier_dt_spec current_amp_##inst##_config = \
CURRENT_SENSE_AMPLIFIER_DT_SPEC_GET(DT_DRV_INST(inst)); \ CURRENT_SENSE_AMPLIFIER_DT_SPEC_GET(DT_DRV_INST(inst)); \
\ \
SENSOR_DEVICE_DT_INST_DEFINE(inst, &current_init, NULL, &current_amp_##inst##_data, \ PM_DEVICE_DT_INST_DEFINE(inst, pm_action); \
&current_amp_##inst##_config, POST_KERNEL, \ \
CONFIG_SENSOR_INIT_PRIORITY, &current_api); SENSOR_DEVICE_DT_INST_DEFINE(inst, &current_init, PM_DEVICE_DT_INST_GET(inst), \
&current_amp_##inst##_data, &current_amp_##inst##_config, \
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &current_api);
DT_INST_FOREACH_STATUS_OKAY(CURRENT_SENSE_AMPLIFIER_INIT) DT_INST_FOREACH_STATUS_OKAY(CURRENT_SENSE_AMPLIFIER_INIT)

View file

@ -37,3 +37,11 @@ properties:
default: 1 default: 1
description: | description: |
Amplifier gain divider. The default is <1>. Amplifier gain divider. The default is <1>.
power-gpios:
type: phandle-array
description: |
Control power to the current amplifier.
If present the corresponding GPIO must be set to an active level
to enable the current amplifier.

View file

@ -8,12 +8,14 @@
#define ZEPHYR_INCLUDE_DRIVERS_ADC_CURRENT_SENSE_AMPLIFIER_H_ #define ZEPHYR_INCLUDE_DRIVERS_ADC_CURRENT_SENSE_AMPLIFIER_H_
#include <zephyr/drivers/adc.h> #include <zephyr/drivers/adc.h>
#include <zephyr/drivers/gpio.h>
struct current_sense_amplifier_dt_spec { struct current_sense_amplifier_dt_spec {
const struct adc_dt_spec port; const struct adc_dt_spec port;
uint32_t sense_micro_ohms; uint32_t sense_micro_ohms;
uint32_t sense_gain_mult; uint32_t sense_gain_mult;
uint32_t sense_gain_div; uint32_t sense_gain_div;
struct gpio_dt_spec power_gpio;
}; };
/** /**
@ -32,6 +34,7 @@ struct current_sense_amplifier_dt_spec {
.sense_micro_ohms = DT_PROP(node_id, sense_resistor_micro_ohms), \ .sense_micro_ohms = DT_PROP(node_id, sense_resistor_micro_ohms), \
.sense_gain_mult = DT_PROP(node_id, sense_gain_mult), \ .sense_gain_mult = DT_PROP(node_id, sense_gain_mult), \
.sense_gain_div = DT_PROP(node_id, sense_gain_div), \ .sense_gain_div = DT_PROP(node_id, sense_gain_div), \
.power_gpio = GPIO_DT_SPEC_GET_OR(node_id, power_gpios, {0}), \
} }
/** /**