[dm][power] add power reset and supply framework

1. Board level reset or poweroff
2. Power supply class

Signed-off-by: GuEe-GUI <2991707448@qq.com>
This commit is contained in:
GuEe-GUI
2025-12-15 12:20:35 +08:00
committed by R b b666
parent 99a25459cd
commit b4d332706e
9 changed files with 1215 additions and 0 deletions

View File

@@ -34,6 +34,7 @@ rsource "hwcache/Kconfig"
rsource "regulator/Kconfig"
rsource "reset/Kconfig"
rsource "pmdomain/Kconfig"
rsource "power/Kconfig"
rsource "thermal/Kconfig"
rsource "virtio/Kconfig"
rsource "nvmem/Kconfig"

View File

@@ -0,0 +1,277 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-11-21 GuEe-GUI first version
*/
#ifndef __POWER_SUPPLY_H__
#define __POWER_SUPPLY_H__
#include <rtthread.h>
#include <drivers/led.h>
#include <drivers/thermal.h>
enum rt_power_supply_type
{
RT_POWER_SUPPLY_TYPE_UNKNOWN = 0,
RT_POWER_SUPPLY_TYPE_BATTERY,
RT_POWER_SUPPLY_TYPE_UPS,
RT_POWER_SUPPLY_TYPE_MAINS,
RT_POWER_SUPPLY_TYPE_USB_SDP, /* Standard Downstream Port */
RT_POWER_SUPPLY_TYPE_USB_DCP, /* Dedicated Charging Port */
RT_POWER_SUPPLY_TYPE_USB_CDP, /* Charging Downstream Port */
RT_POWER_SUPPLY_TYPE_USB_ACA, /* Accessory Charger Adapters */
RT_POWER_SUPPLY_TYPE_USB_TYPE_C, /* Type C Port */
RT_POWER_SUPPLY_TYPE_USB_PD, /* Power Delivery Port */
RT_POWER_SUPPLY_TYPE_USB_PD_DRP, /* PD Dual Role Port */
RT_POWER_SUPPLY_TYPE_USB_PD_PPS, /* PD Programmable Power Supply */
RT_POWER_SUPPLY_TYPE_WIRELESS, /* Wireless */
};
enum rt_power_supply_status
{
RT_POWER_SUPPLY_STATUS_UNKNOWN = 0,
RT_POWER_SUPPLY_STATUS_CHARGING,
RT_POWER_SUPPLY_STATUS_DISCHARGING,
RT_POWER_SUPPLY_STATUS_NOT_CHARGING,
RT_POWER_SUPPLY_STATUS_FULL,
};
enum rt_power_supply_charge_type
{
RT_POWER_SUPPLY_CHARGE_TYPE_UNKNOWN = 0,
RT_POWER_SUPPLY_CHARGE_TYPE_NONE,
RT_POWER_SUPPLY_CHARGE_TYPE_TRICKLE, /* Slow speed */
RT_POWER_SUPPLY_CHARGE_TYPE_FAST, /* Fast speed */
RT_POWER_SUPPLY_CHARGE_TYPE_STANDARD, /* Normal speed */
RT_POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE, /* Dynamically adjusted speed */
RT_POWER_SUPPLY_CHARGE_TYPE_CUSTOM, /* Use CHARGE_CONTROL_* props */
RT_POWER_SUPPLY_CHARGE_TYPE_LONGLIFE, /* Slow speed, longer life */
RT_POWER_SUPPLY_CHARGE_TYPE_BYPASS, /* Bypassing the charger */
};
enum rt_power_supply_health
{
RT_POWER_SUPPLY_HEALTH_UNKNOWN = 0,
RT_POWER_SUPPLY_HEALTH_GOOD,
RT_POWER_SUPPLY_HEALTH_OVERHEAT,
RT_POWER_SUPPLY_HEALTH_DEAD,
RT_POWER_SUPPLY_HEALTH_OVERVOLTAGE,
RT_POWER_SUPPLY_HEALTH_UNSPEC_FAILURE,
RT_POWER_SUPPLY_HEALTH_COLD,
RT_POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE,
RT_POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE,
RT_POWER_SUPPLY_HEALTH_OVERCURRENT,
RT_POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED,
RT_POWER_SUPPLY_HEALTH_WARM,
RT_POWER_SUPPLY_HEALTH_COOL,
RT_POWER_SUPPLY_HEALTH_HOT,
RT_POWER_SUPPLY_HEALTH_NO_BATTERY,
};
enum rt_power_supply_technology
{
RT_POWER_SUPPLY_TECHNOLOGY_UNKNOWN = 0,
RT_POWER_SUPPLY_TECHNOLOGY_NiMH,
RT_POWER_SUPPLY_TECHNOLOGY_LION,
RT_POWER_SUPPLY_TECHNOLOGY_LIPO,
RT_POWER_SUPPLY_TECHNOLOGY_LiFe,
RT_POWER_SUPPLY_TECHNOLOGY_NiCd,
RT_POWER_SUPPLY_TECHNOLOGY_LiMn,
};
enum rt_power_supply_capacity_level
{
RT_POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN = 0,
RT_POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL,
RT_POWER_SUPPLY_CAPACITY_LEVEL_LOW,
RT_POWER_SUPPLY_CAPACITY_LEVEL_NORMAL,
RT_POWER_SUPPLY_CAPACITY_LEVEL_HIGH,
RT_POWER_SUPPLY_CAPACITY_LEVEL_FULL,
};
enum rt_power_supply_scope
{
RT_POWER_SUPPLY_SCOPE_UNKNOWN = 0,
RT_POWER_SUPPLY_SCOPE_SYSTEM,
RT_POWER_SUPPLY_SCOPE_DEVICE,
};
enum rt_power_supply_property
{
/* Properties of type `int' */
RT_POWER_SUPPLY_PROP_STATUS = 0,
RT_POWER_SUPPLY_PROP_CHARGE_TYPE,
RT_POWER_SUPPLY_PROP_HEALTH,
RT_POWER_SUPPLY_PROP_PRESENT,
RT_POWER_SUPPLY_PROP_ONLINE,
RT_POWER_SUPPLY_PROP_AUTHENTIC,
RT_POWER_SUPPLY_PROP_TECHNOLOGY,
RT_POWER_SUPPLY_PROP_CYCLE_COUNT,
RT_POWER_SUPPLY_PROP_VOLTAGE_MAX,
RT_POWER_SUPPLY_PROP_VOLTAGE_MIN,
RT_POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
RT_POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
RT_POWER_SUPPLY_PROP_VOLTAGE_NOW,
RT_POWER_SUPPLY_PROP_VOLTAGE_AVG,
RT_POWER_SUPPLY_PROP_VOLTAGE_OCV,
RT_POWER_SUPPLY_PROP_VOLTAGE_BOOT,
RT_POWER_SUPPLY_PROP_CURRENT_MAX,
RT_POWER_SUPPLY_PROP_CURRENT_NOW,
RT_POWER_SUPPLY_PROP_CURRENT_AVG,
RT_POWER_SUPPLY_PROP_CURRENT_BOOT,
RT_POWER_SUPPLY_PROP_POWER_NOW,
RT_POWER_SUPPLY_PROP_POWER_AVG,
RT_POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
RT_POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
RT_POWER_SUPPLY_PROP_CHARGE_FULL,
RT_POWER_SUPPLY_PROP_CHARGE_EMPTY,
RT_POWER_SUPPLY_PROP_CHARGE_NOW,
RT_POWER_SUPPLY_PROP_CHARGE_AVG,
RT_POWER_SUPPLY_PROP_CHARGE_COUNTER,
RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
RT_POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
RT_POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
RT_POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD,
RT_POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD,
RT_POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR,
RT_POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
RT_POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT,
RT_POWER_SUPPLY_PROP_INPUT_POWER_LIMIT,
RT_POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
RT_POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN,
RT_POWER_SUPPLY_PROP_ENERGY_FULL,
RT_POWER_SUPPLY_PROP_ENERGY_EMPTY,
RT_POWER_SUPPLY_PROP_ENERGY_NOW,
RT_POWER_SUPPLY_PROP_ENERGY_AVG,
RT_POWER_SUPPLY_PROP_CAPACITY,
RT_POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
RT_POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX,
RT_POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN,
RT_POWER_SUPPLY_PROP_CAPACITY_LEVEL,
RT_POWER_SUPPLY_PROP_TEMP,
RT_POWER_SUPPLY_PROP_TEMP_MAX,
RT_POWER_SUPPLY_PROP_TEMP_MIN,
RT_POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
RT_POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
RT_POWER_SUPPLY_PROP_TEMP_AMBIENT,
RT_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN,
RT_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX,
RT_POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
RT_POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
RT_POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
RT_POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
RT_POWER_SUPPLY_PROP_SCOPE,
RT_POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
RT_POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
RT_POWER_SUPPLY_PROP_CALIBRATE,
RT_POWER_SUPPLY_PROP_MANUFACTURE_YEAR,
RT_POWER_SUPPLY_PROP_MANUFACTURE_MONTH,
RT_POWER_SUPPLY_PROP_MANUFACTURE_DAY,
/* Properties of type `const char *' */
RT_POWER_SUPPLY_PROP_MODEL_NAME,
RT_POWER_SUPPLY_PROP_MANUFACTURER,
RT_POWER_SUPPLY_PROP_SERIAL_NUMBER,
};
union rt_power_supply_property_val
{
int intval;
const char *strval;
};
struct rt_power_supply_battery_info
{
enum rt_power_supply_technology technology;
int energy_full_design_uwh;
int charge_full_design_uah;
int voltage_min_design_uv;
int voltage_max_design_uv;
int precharge_current_ua;
int charge_term_current_ua;
int charge_restart_voltage_uv;
int constant_charge_current_max_ua;
int constant_charge_voltage_max_uv;
int temp_ambient_alert_min;
int temp_ambient_alert_max;
int temp_alert_min;
int temp_alert_max;
int temp_min;
int temp_max;
};
struct rt_power_supply_ops;
struct rt_power_supply_notifier;
struct rt_power_supply
{
rt_list_t list;
struct rt_device *dev;
enum rt_power_supply_type type;
rt_size_t properties_nr;
enum rt_power_supply_property *properties;
struct rt_power_supply_battery_info *battery_info;
const struct rt_power_supply_ops *ops;
struct rt_ref ref;
#ifdef RT_USING_THERMAL
struct rt_thermal_zone_device *thermal_dev;
#endif
#ifdef RT_USING_LED
struct rt_led_device *led_dev;
#endif
struct rt_work changed_work;
};
struct rt_power_supply_ops
{
rt_err_t (*get_property)(struct rt_power_supply *psy,
enum rt_power_supply_property prop, union rt_power_supply_property_val *val);
rt_err_t (*set_property)(struct rt_power_supply *psy,
enum rt_power_supply_property prop, const union rt_power_supply_property_val *val);
};
typedef rt_err_t (*rt_power_supply_notifier_callback)(struct rt_power_supply_notifier *notifier,
struct rt_power_supply *psy);
struct rt_power_supply_notifier
{
rt_list_t list;
rt_power_supply_notifier_callback callback;
void *priv;
};
rt_err_t rt_power_supply_register(struct rt_power_supply *psy);
rt_err_t rt_power_supply_unregister(struct rt_power_supply *psy);
rt_err_t rt_power_supply_notifier_register(struct rt_power_supply_notifier *notifier);
rt_err_t rt_power_supply_notifier_unregister(struct rt_power_supply_notifier *notifier);
rt_err_t rt_power_supply_get_property(struct rt_power_supply *psy,
enum rt_power_supply_property prop,
union rt_power_supply_property_val *val);
rt_err_t rt_power_supply_set_property(struct rt_power_supply *psy,
enum rt_power_supply_property prop,
const union rt_power_supply_property_val *val);
void rt_power_supply_changed(struct rt_power_supply *psy);
struct rt_power_supply *rt_power_supply_get(struct rt_device *dev, const char *id);
void rt_power_supply_put(struct rt_power_supply *psy);
#endif /* __POWER_SUPPLY_H__ */

View File

@@ -135,6 +135,10 @@ extern "C" {
#include "drivers/hwcache.h"
#endif /* RT_USING_HWCACHE */
#ifdef RT_USING_POWER_SUPPLY
#include "drivers/power_supply.h"
#endif /* RT_USING_POWER_SUPPLY */
#ifdef RT_USING_NVMEM
#include "drivers/nvmem.h"
#endif /* RT_USING_NVMEM */

View File

@@ -0,0 +1,7 @@
menuconfig RT_USING_POWER_RESET
bool "Using Board level reset or poweroff"
depends on RT_USING_DM
if RT_USING_POWER_RESET
osource "$(SOC_DM_POWER_RESET_DIR)/Kconfig"
endif

View File

@@ -0,0 +1,15 @@
from building import *
group = []
if not GetDepend(['RT_USING_POWER_RESET']):
Return('group')
cwd = GetCurrentDir()
CPPPATH = [cwd + '/../../include']
src = []
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
Return('group')

View File

@@ -0,0 +1,20 @@
menuconfig RT_USING_POWER_SUPPLY
bool "Using Power supply class support"
depends on RT_USING_DM
select RT_USING_ADT
select RT_USING_ADT_REF
select RT_USING_SYSTEM_WORKQUEUE
default n
config RT_POWER_SUPPLY_DAEMON
bool "System supply daemon"
depends on RT_USING_POWER_SUPPLY
default y
if RT_USING_POWER_SUPPLY
comment "Power Supply Device Drivers"
endif
if RT_USING_POWER_SUPPLY
osource "$(SOC_DM_POWER_SUPPLY_DIR)/Kconfig"
endif

View File

@@ -0,0 +1,18 @@
from building import *
group = []
if not GetDepend(['RT_USING_POWER_SUPPLY']):
Return('group')
cwd = GetCurrentDir()
CPPPATH = [cwd + '/../../include']
src = ['supply.c']
if GetDepend(['RT_POWER_SUPPLY_DAEMON']):
src += ['supply-daemon.c']
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
Return('group')

View File

@@ -0,0 +1,179 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-02-25 GuEe-GUI the first version
*/
#include <rtdevice.h>
#define DBG_TAG "power_supply.daemon"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#ifdef RT_USING_PM
static rt_bool_t low_power_mode = RT_FALSE;
static rt_uint8_t raw_pm_run_mode = PM_RUN_MODE_MAX;
static rt_uint8_t raw_pm_sleep_mode = PM_RUN_MODE_MAX;
static rt_uint8_t last_pm_sleep_mode;
#endif
static rt_err_t daemon_power_supply_notify(struct rt_power_supply_notifier *notifier,
struct rt_power_supply *psy)
{
union rt_power_supply_property_val propval;
rt_uint32_t voltage_now, voltage_min, voltage_max;
if (!rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_CAPACITY, &propval))
{
goto _capacity_check;
}
if (rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_VOLTAGE_NOW, &propval))
{
return -RT_ENOSYS;
}
voltage_now = propval.intval / 1000000;
if (rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_VOLTAGE_MIN, &propval))
{
return -RT_ENOSYS;
}
voltage_min = propval.intval / 1000000;
if (rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_VOLTAGE_MAX, &propval))
{
return -RT_ENOSYS;
}
voltage_max = propval.intval / 1000000;
propval.intval = (voltage_now - voltage_min) * 100 / (voltage_max - voltage_min);
_capacity_check:
if (propval.intval >= 80)
{
rt_bool_t full_power = propval.intval == 100;
if (rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_STATUS, &propval))
{
return -RT_ENOSYS;
}
if (propval.intval == RT_POWER_SUPPLY_STATUS_CHARGING)
{
if (full_power)
{
LOG_I("%s: Power is full", rt_dm_dev_get_name(psy->dev));
}
else
{
LOG_I("%s: Power is sufficient", rt_dm_dev_get_name(psy->dev));
}
}
}
else if (propval.intval <= 20)
{
#ifdef RT_USING_PM
rt_uint8_t pm_sleep_mode;
struct rt_pm *pm = rt_pm_get_handle();
RT_ASSERT(pm != RT_NULL);
low_power_mode = RT_TRUE;
if (raw_pm_run_mode == PM_RUN_MODE_MAX)
{
raw_pm_run_mode = pm->run_mode;
}
if (raw_pm_sleep_mode == PM_SLEEP_MODE_MAX)
{
last_pm_sleep_mode = raw_pm_sleep_mode = pm->sleep_mode;
}
pm_sleep_mode = pm->sleep_mode;
#endif /* RT_USING_PM */
if (propval.intval <= 5)
{
do {
#ifdef RT_USING_PM
if (pm_sleep_mode != PM_SLEEP_MODE_SHUTDOWN && propval.intval > 1)
{
pm_sleep_mode = PM_SLEEP_MODE_SHUTDOWN;
break;
}
#endif
if (!rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_SCOPE, &propval) &&
propval.intval == RT_POWER_SUPPLY_SCOPE_SYSTEM)
{
LOG_E("%s: Power is critical, poweroff now", rt_dm_dev_get_name(psy->dev));
rt_hw_cpu_shutdown();
}
} while (0);
LOG_E("%s: Power is critical", rt_dm_dev_get_name(psy->dev));
}
else if (propval.intval <= 10)
{
#ifdef RT_USING_PM
pm_sleep_mode = PM_SLEEP_MODE_STANDBY;
rt_pm_run_enter(PM_RUN_MODE_LOW_SPEED);
#endif
}
else if (propval.intval <= 15)
{
#ifdef RT_USING_PM
pm_sleep_mode = PM_SLEEP_MODE_DEEP;
rt_pm_run_enter(PM_RUN_MODE_MEDIUM_SPEED);
#endif
}
else if (propval.intval <= 20)
{
#ifdef RT_USING_PM
pm_sleep_mode = PM_SLEEP_MODE_LIGHT;
rt_pm_run_enter(PM_RUN_MODE_NORMAL_SPEED);
#endif
LOG_W("%s: Power is low", rt_dm_dev_get_name(psy->dev));
}
#ifdef RT_USING_PM
if (pm_sleep_mode != last_pm_sleep_mode)
{
rt_pm_release(last_pm_sleep_mode);
rt_pm_request(pm_sleep_mode);
last_pm_sleep_mode = pm_sleep_mode;
}
#endif /* RT_USING_PM */
}
else
{
#ifdef RT_USING_PM
if (low_power_mode)
{
rt_pm_release(last_pm_sleep_mode);
rt_pm_request(raw_pm_sleep_mode);
rt_pm_run_enter(raw_pm_run_mode);
low_power_mode = RT_FALSE;
}
#endif /* RT_USING_PM */
}
return RT_EOK;
}
static int power_supply_daemon_init(void)
{
static struct rt_power_supply_notifier daemon_notifier;
daemon_notifier.callback = daemon_power_supply_notify;
rt_power_supply_notifier_register(&daemon_notifier);
return 0;
}
INIT_ENV_EXPORT(power_supply_daemon_init);

File diff suppressed because it is too large Load Diff