Skip to content

Commit

Permalink
feat(ext power): Support multiple ext_power GPIOS
Browse files Browse the repository at this point in the history
  • Loading branch information
ReFil committed Sep 24, 2024
1 parent 11f600d commit 03e0c80
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions app/src/ext_power_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

struct ext_power_generic_config {
const struct gpio_dt_spec control;
const struct gpio_dt_spec *control;
const size_t control_gpios_count;
const uint16_t init_delay_ms;
};

Expand Down Expand Up @@ -59,9 +60,12 @@ static int ext_power_generic_enable(const struct device *dev) {
struct ext_power_generic_data *data = dev->data;
const struct ext_power_generic_config *config = dev->config;

if (gpio_pin_set_dt(&config->control, 1)) {
LOG_WRN("Failed to set ext-power control pin");
return -EIO;
for (int i = 0; i < config->control_gpios_count; i++) {
const struct gpio_dt_spec *gpio = &config->control[i];
if (gpio_pin_set_dt(gpio, 1)) {
LOG_WRN("Failed to set ext-power control pin %d", i);
return -EIO;
}
}
data->status = true;
return ext_power_save_state();
Expand All @@ -71,10 +75,12 @@ static int ext_power_generic_disable(const struct device *dev) {
struct ext_power_generic_data *data = dev->data;
const struct ext_power_generic_config *config = dev->config;

if (gpio_pin_set_dt(&config->control, 0)) {
LOG_WRN("Failed to set ext-power control pin");
LOG_WRN("Failed to clear ext-power control pin");
return -EIO;
for (int i = 0; i < config->control_gpios_count; i++) {
const struct gpio_dt_spec *gpio = &config->control[i];
if (gpio_pin_set_dt(gpio, 0)) {
LOG_WRN("Failed to clear ext-power control pin %d", i);
return -EIO;
}
}
data->status = false;
return ext_power_save_state();
Expand Down Expand Up @@ -144,9 +150,12 @@ SETTINGS_STATIC_HANDLER_DEFINE(ext_power, "ext_power/state", NULL, ext_power_set
static int ext_power_generic_init(const struct device *dev) {
const struct ext_power_generic_config *config = dev->config;

if (gpio_pin_configure_dt(&config->control, GPIO_OUTPUT_INACTIVE)) {
LOG_ERR("Failed to configure ext-power control pin");
return -EIO;
for (int i = 0; i < config->control_gpios_count; i++) {
const struct gpio_dt_spec *gpio = &config->control[i];
if (gpio_pin_configure_dt(gpio, GPIO_OUTPUT_INACTIVE)) {
LOG_ERR("Failed to configure ext-power control pin %d", i);
return -EIO;
}
}

#if IS_ENABLED(CONFIG_SETTINGS)
Expand Down Expand Up @@ -178,8 +187,12 @@ static int ext_power_generic_pm_action(const struct device *dev, enum pm_device_
}
#endif /* CONFIG_PM_DEVICE */

static const struct gpio_dt_spec ext_power_control_gpios[DT_INST_PROP_LEN(0, control_gpios)] = {
DT_INST_FOREACH_PROP_ELEM_SEP(0, control_gpios, GPIO_DT_SPEC_GET_BY_IDX, (, ))};

static const struct ext_power_generic_config config = {
.control = GPIO_DT_SPEC_INST_GET(0, control_gpios),
.control = ext_power_control_gpios,
.control_gpios_count = DT_INST_PROP_LEN(0, control_gpios),
.init_delay_ms = DT_INST_PROP_OR(0, init_delay_ms, 0)};

static struct ext_power_generic_data data = {
Expand Down

0 comments on commit 03e0c80

Please sign in to comment.