Update ext_power_generic.c
This commit is contained in:
parent
c3f8ad4636
commit
67576fc994
1 changed files with 42 additions and 19 deletions
|
@ -8,7 +8,6 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <device.h>
|
#include <device.h>
|
||||||
#include <pm/device.h>
|
|
||||||
#include <init.h>
|
#include <init.h>
|
||||||
#include <kernel.h>
|
#include <kernel.h>
|
||||||
#include <settings/settings.h>
|
#include <settings/settings.h>
|
||||||
|
@ -36,6 +35,9 @@ struct ext_power_generic_data {
|
||||||
#if IS_ENABLED(CONFIG_SETTINGS)
|
#if IS_ENABLED(CONFIG_SETTINGS)
|
||||||
bool settings_init;
|
bool settings_init;
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef CONFIG_PM_DEVICE
|
||||||
|
uint32_t pm_state;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
#if IS_ENABLED(CONFIG_SETTINGS)
|
#if IS_ENABLED(CONFIG_SETTINGS)
|
||||||
|
@ -48,13 +50,13 @@ static void ext_power_save_state_work(struct k_work *work) {
|
||||||
settings_save_one(setting_path, &data->status, sizeof(data->status));
|
settings_save_one(setting_path, &data->status, sizeof(data->status));
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct k_work_delayable ext_power_save_work;
|
static struct k_delayed_work ext_power_save_work;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int ext_power_save_state() {
|
int ext_power_save_state() {
|
||||||
#if IS_ENABLED(CONFIG_SETTINGS)
|
#if IS_ENABLED(CONFIG_SETTINGS)
|
||||||
int ret = k_work_reschedule(&ext_power_save_work, K_MSEC(CONFIG_ZMK_SETTINGS_SAVE_DEBOUNCE));
|
k_delayed_work_cancel(&ext_power_save_work);
|
||||||
return MIN(ret, 0);
|
return k_delayed_work_submit(&ext_power_save_work, K_MSEC(CONFIG_ZMK_SETTINGS_SAVE_DEBOUNCE));
|
||||||
#else
|
#else
|
||||||
return 0;
|
return 0;
|
||||||
#endif
|
#endif
|
||||||
|
@ -109,7 +111,7 @@ static int ext_power_settings_set(const char *name, size_t len, settings_read_cb
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
if (settings_name_steq(name, DT_INST_LABEL(0), &next) && !next) {
|
if (settings_name_steq(name, DT_INST_LABEL(0), &next) && !next) {
|
||||||
const struct device *ext_power = DEVICE_DT_GET(DT_DRV_INST(0));
|
const struct device *ext_power = device_get_binding(DT_INST_LABEL(0));
|
||||||
struct ext_power_generic_data *data = ext_power->data;
|
struct ext_power_generic_data *data = ext_power->data;
|
||||||
|
|
||||||
if (len != sizeof(data->status)) {
|
if (len != sizeof(data->status)) {
|
||||||
|
@ -157,6 +159,10 @@ static int ext_power_generic_init(const struct device *dev) {
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef CONFIG_PM_DEVICE
|
||||||
|
data->pm_state = DEVICE_PM_ACTIVE_STATE;
|
||||||
|
#endif
|
||||||
|
|
||||||
#if IS_ENABLED(CONFIG_SETTINGS)
|
#if IS_ENABLED(CONFIG_SETTINGS)
|
||||||
settings_subsys_init();
|
settings_subsys_init();
|
||||||
|
|
||||||
|
@ -166,14 +172,14 @@ static int ext_power_generic_init(const struct device *dev) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
k_work_init_delayable(&ext_power_save_work, ext_power_save_state_work);
|
k_delayed_work_init(&ext_power_save_work, ext_power_save_state_work);
|
||||||
|
|
||||||
// Set default value (on) if settings isn't set
|
// Set default value (on) if settings isn't set
|
||||||
settings_load_subtree("ext_power");
|
settings_load_subtree("ext_power");
|
||||||
if (!data->settings_init) {
|
if (!data->settings_init) {
|
||||||
|
|
||||||
data->status = true;
|
data->status = true;
|
||||||
k_work_schedule(&ext_power_save_work, K_NO_WAIT);
|
k_delayed_work_submit(&ext_power_save_work, K_NO_WAIT);
|
||||||
|
|
||||||
ext_power_enable(dev);
|
ext_power_enable(dev);
|
||||||
}
|
}
|
||||||
|
@ -190,17 +196,35 @@ static int ext_power_generic_init(const struct device *dev) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CONFIG_PM_DEVICE
|
#ifdef CONFIG_PM_DEVICE
|
||||||
static int ext_power_generic_pm_action(const struct device *dev, enum pm_device_action action) {
|
static int ext_power_generic_pm_control(const struct device *dev, uint32_t ctrl_command,
|
||||||
switch (action) {
|
void *context, device_pm_cb cb, void *arg) {
|
||||||
case PM_DEVICE_ACTION_RESUME:
|
int rc;
|
||||||
ext_power_generic_enable(dev);
|
struct ext_power_generic_data *data = dev->data;
|
||||||
return 0;
|
|
||||||
case PM_DEVICE_ACTION_SUSPEND:
|
switch (ctrl_command) {
|
||||||
ext_power_generic_disable(dev);
|
case DEVICE_PM_SET_POWER_STATE:
|
||||||
return 0;
|
if (*((uint32_t *)context) == DEVICE_PM_ACTIVE_STATE) {
|
||||||
|
data->pm_state = DEVICE_PM_ACTIVE_STATE;
|
||||||
|
rc = 0;
|
||||||
|
} else {
|
||||||
|
ext_power_generic_disable(dev);
|
||||||
|
data->pm_state = DEVICE_PM_LOW_POWER_STATE;
|
||||||
|
rc = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case DEVICE_PM_GET_POWER_STATE:
|
||||||
|
*((uint32_t *)context) = data->pm_state;
|
||||||
|
rc = 0;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return -ENOTSUP;
|
rc = -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (cb != NULL) {
|
||||||
|
cb(dev, rc, context, arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
}
|
}
|
||||||
#endif /* CONFIG_PM_DEVICE */
|
#endif /* CONFIG_PM_DEVICE */
|
||||||
|
|
||||||
|
@ -223,8 +247,7 @@ static const struct ext_power_api api = {.enable = ext_power_generic_enable,
|
||||||
|
|
||||||
#define ZMK_EXT_POWER_INIT_PRIORITY 81
|
#define ZMK_EXT_POWER_INIT_PRIORITY 81
|
||||||
|
|
||||||
PM_DEVICE_DT_INST_DEFINE(0, ext_power_generic_pm_action);
|
DEVICE_DT_INST_DEFINE(0, ext_power_generic_init, &ext_power_generic_pm_control, &data, &config,
|
||||||
DEVICE_DT_INST_DEFINE(0, ext_power_generic_init, PM_DEVICE_DT_INST_GET(0), &data, &config,
|
|
||||||
POST_KERNEL, ZMK_EXT_POWER_INIT_PRIORITY, &api);
|
POST_KERNEL, ZMK_EXT_POWER_INIT_PRIORITY, &api);
|
||||||
|
|
||||||
#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */
|
#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */
|
Loading…
Add table
Reference in a new issue