diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 87e37864bd..a2a6dc9ef6 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -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" diff --git a/components/drivers/include/drivers/power_supply.h b/components/drivers/include/drivers/power_supply.h new file mode 100644 index 0000000000..15ee64e90b --- /dev/null +++ b/components/drivers/include/drivers/power_supply.h @@ -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 +#include +#include + +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__ */ diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index cd4ef4f444..7cda82c235 100644 --- a/components/drivers/include/rtdevice.h +++ b/components/drivers/include/rtdevice.h @@ -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 */ diff --git a/components/drivers/power/reset/Kconfig b/components/drivers/power/reset/Kconfig new file mode 100644 index 0000000000..78378fe541 --- /dev/null +++ b/components/drivers/power/reset/Kconfig @@ -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 diff --git a/components/drivers/power/reset/SConscript b/components/drivers/power/reset/SConscript new file mode 100644 index 0000000000..9824338614 --- /dev/null +++ b/components/drivers/power/reset/SConscript @@ -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') diff --git a/components/drivers/power/supply/Kconfig b/components/drivers/power/supply/Kconfig new file mode 100644 index 0000000000..be78289018 --- /dev/null +++ b/components/drivers/power/supply/Kconfig @@ -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 diff --git a/components/drivers/power/supply/SConscript b/components/drivers/power/supply/SConscript new file mode 100644 index 0000000000..a8c082c0db --- /dev/null +++ b/components/drivers/power/supply/SConscript @@ -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') diff --git a/components/drivers/power/supply/supply-daemon.c b/components/drivers/power/supply/supply-daemon.c new file mode 100644 index 0000000000..8f0286e496 --- /dev/null +++ b/components/drivers/power/supply/supply-daemon.c @@ -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 + +#define DBG_TAG "power_supply.daemon" +#define DBG_LVL DBG_INFO +#include + +#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); diff --git a/components/drivers/power/supply/supply.c b/components/drivers/power/supply/supply.c new file mode 100644 index 0000000000..bce91ad997 --- /dev/null +++ b/components/drivers/power/supply/supply.c @@ -0,0 +1,694 @@ +/* + * 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 + +#define DBG_TAG "rtdm.power_supply" +#define DBG_LVL DBG_INFO +#include + +#ifndef INT_MAX +#define INT_MAX (RT_UINT32_MAX >> 1) +#endif + +#ifndef INT_MIN +#define INT_MIN (-INT_MAX - 1) +#endif + +static RT_DEFINE_SPINLOCK(nodes_lock); +static rt_list_t power_supply_nodes = RT_LIST_OBJECT_INIT(power_supply_nodes); +static rt_list_t power_supply_notifier_nodes = RT_LIST_OBJECT_INIT(power_supply_notifier_nodes); + +static rt_bool_t power_supply_have_property(struct rt_power_supply *psy, + enum rt_power_supply_property prop); + +#ifdef RT_USING_THERMAL +static rt_err_t power_supply_thermal_zone_get_temp(struct rt_thermal_zone_device *zdev, + int *out_temp) +{ + rt_err_t err; + union rt_power_supply_property_val val; + struct rt_power_supply *psy = zdev->priv; + + if ((err = rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_TEMP, &val))) + { + return err; + } + + *out_temp = val.intval; + + return RT_EOK; +} + +const static struct rt_thermal_zone_ops power_supply_thermal_zone_ops = +{ + .get_temp = power_supply_thermal_zone_get_temp, +}; + +rt_err_t power_supply_thermal_register(struct rt_power_supply *psy) +{ + if (psy->thermal_dev) + { + return RT_EOK; + } + + if (power_supply_have_property(psy, RT_POWER_SUPPLY_PROP_TEMP)) + { + rt_err_t err; + + if (!(psy->thermal_dev = rt_calloc(1, sizeof(*psy->thermal_dev)))) + { + return -RT_ENOMEM; + } + + rt_dm_dev_set_name(&psy->thermal_dev->parent, rt_dm_dev_get_name(psy->dev)); + psy->thermal_dev->zone_id = 0; + psy->thermal_dev->ops = &power_supply_thermal_zone_ops; + psy->thermal_dev->parent.ofw_node = psy->dev->ofw_node; + psy->thermal_dev->priv = psy; + + if ((err = rt_thermal_zone_device_register(psy->thermal_dev))) + { + rt_free(psy->thermal_dev); + psy->thermal_dev = RT_NULL; + + return err; + } + } + + return RT_EOK; +} + +rt_err_t power_supply_thermal_unregister(struct rt_power_supply *psy) +{ + rt_err_t err = RT_EOK; + + if (psy->thermal_dev) + { + if (!(err = rt_thermal_zone_device_unregister(psy->thermal_dev))) + { + rt_free(psy->thermal_dev); + psy->thermal_dev = RT_NULL; + } + } + + return err; +} +#else +rt_err_t power_supply_thermal_register(struct rt_power_supply *psy) +{ + return RT_EOK; +} + +rt_err_t power_supply_thermal_unregister(struct rt_power_supply *psy) +{ + return RT_EOK; +} +#endif /* RT_USING_THERMAL */ + +#ifdef RT_USING_LED +static void power_supply_update_battery_led(struct rt_power_supply *psy) +{ + union rt_power_supply_property_val status; + + if (rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_STATUS, &status)) + { + return; + } + + switch (status.intval) + { + case RT_POWER_SUPPLY_STATUS_FULL: + rt_led_set_state(psy->led_dev, RT_LED_S_ON); + rt_led_set_brightness(psy->led_dev, 255); + break; + + case RT_POWER_SUPPLY_STATUS_CHARGING: + rt_led_set_state(psy->led_dev, RT_LED_S_ON); + rt_led_set_brightness(psy->led_dev, 255 >> 1); + break; + + default: + rt_led_set_state(psy->led_dev, RT_LED_S_OFF); + break; + } +} +static void power_supply_update_online_led(struct rt_power_supply *psy) +{ + union rt_power_supply_property_val online; + + if (rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_ONLINE, &online)) + { + return; + } + + if (online.intval) + { + rt_led_set_state(psy->led_dev, RT_LED_S_ON); + } + else + { + rt_led_set_state(psy->led_dev, RT_LED_S_OFF); + } +} + +static void power_supply_update_led(struct rt_power_supply *psy) +{ + if (!psy->led_dev) + { + return; + } + + if (psy->type == RT_POWER_SUPPLY_TYPE_BATTERY) + { + power_supply_update_battery_led(psy); + } + else + { + power_supply_update_online_led(psy); + } +} +#else +static void power_supply_update_led(struct rt_power_supply *psy) +{ +} +#endif /* RT_USING_LED */ + +static void power_supply_changed_work(struct rt_work *work, void *work_data) +{ + struct rt_power_supply *psy = work_data; + struct rt_power_supply_notifier *notifier, *next_notifier; + + power_supply_update_led(psy); + + rt_spin_lock(&nodes_lock); + + rt_list_for_each_entry_safe(notifier, next_notifier, &power_supply_notifier_nodes, list) + { + rt_spin_unlock(&nodes_lock); + + notifier->callback(notifier, psy); + + rt_spin_lock(&nodes_lock); + } + + rt_spin_unlock(&nodes_lock); +} + +rt_err_t rt_power_supply_register(struct rt_power_supply *psy) +{ + rt_err_t err; + + if (!psy || !psy->dev) + { + return -RT_EINVAL; + } + + if (!psy->battery_info && (!psy->properties_nr || !psy->properties || !psy->ops)) + { + return -RT_EINVAL; + } + + if ((err = power_supply_thermal_register(psy))) + { + return err; + } + + rt_ref_init(&psy->ref); + rt_list_init(&psy->list); + rt_work_init(&psy->changed_work, power_supply_changed_work, psy); + + rt_spin_lock(&nodes_lock); + rt_list_insert_before(&power_supply_nodes, &psy->list); + rt_spin_unlock(&nodes_lock); + + if (psy->dev->ofw_node) + { + rt_dm_dev_bind_fwdata(psy->dev, RT_NULL, psy); + } + + return RT_EOK; +} + +rt_err_t rt_power_supply_unregister(struct rt_power_supply *psy) +{ + rt_err_t err; + + if (!psy) + { + return -RT_EINVAL; + } + + rt_spin_lock(&nodes_lock); + + if (rt_ref_read(&psy->ref) > 1) + { + err = -RT_EBUSY; + goto _unlock; + } + + rt_list_remove(&psy->list); + + if (psy->dev->ofw_node) + { + rt_dm_dev_unbind_fwdata(psy->dev, RT_NULL); + } + +_unlock: + rt_spin_unlock(&nodes_lock); + + if (!err) + { + rt_work_cancel(&psy->changed_work); + + err = power_supply_thermal_unregister(psy); + } + + return err; +} + +rt_err_t rt_power_supply_notifier_register(struct rt_power_supply_notifier *notifier) +{ + if (!notifier || !notifier->callback) + { + return -RT_EINVAL; + } + + rt_list_init(¬ifier->list); + + rt_spin_lock(&nodes_lock); + rt_list_insert_before(&power_supply_notifier_nodes, ¬ifier->list); + rt_spin_unlock(&nodes_lock); + + return RT_EOK; +} + +rt_err_t rt_power_supply_notifier_unregister(struct rt_power_supply_notifier *notifier) +{ + if (!notifier) + { + return -RT_EINVAL; + } + + rt_spin_lock(&nodes_lock); + rt_list_remove(¬ifier->list); + rt_spin_unlock(&nodes_lock); + + return RT_EOK; +} + +static rt_bool_t power_supply_have_property(struct rt_power_supply *psy, + enum rt_power_supply_property prop) +{ + if (!psy->ops->get_property) + { + return RT_FALSE; + } + + for (int i = 0; i < psy->properties_nr; ++i) + { + if (psy->properties[i] == prop) + { + return RT_TRUE; + } + } + + return RT_FALSE; +} + +static rt_bool_t power_supply_battery_info_have_property( + struct rt_power_supply_battery_info *info, enum rt_power_supply_property prop) +{ + if (!info) + { + return RT_FALSE; + } + + switch (prop) + { + case RT_POWER_SUPPLY_PROP_TECHNOLOGY: + return info->technology != RT_POWER_SUPPLY_TECHNOLOGY_UNKNOWN; + + case RT_POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: + return info->energy_full_design_uwh >= 0; + + case RT_POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: + return info->charge_full_design_uah >= 0; + + case RT_POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: + return info->voltage_min_design_uv >= 0; + + case RT_POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: + return info->voltage_max_design_uv >= 0; + + case RT_POWER_SUPPLY_PROP_PRECHARGE_CURRENT: + return info->precharge_current_ua >= 0; + + case RT_POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: + return info->charge_term_current_ua >= 0; + + case RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: + return info->constant_charge_current_max_ua >= 0; + + case RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: + return info->constant_charge_voltage_max_uv >= 0; + + case RT_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN: + return info->temp_ambient_alert_min > INT_MIN; + + case RT_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX: + return info->temp_ambient_alert_max < INT_MAX; + + case RT_POWER_SUPPLY_PROP_TEMP_ALERT_MIN: + return info->temp_alert_min > INT_MIN; + + case RT_POWER_SUPPLY_PROP_TEMP_ALERT_MAX: + return info->temp_alert_max < INT_MAX; + + case RT_POWER_SUPPLY_PROP_TEMP_MIN: + return info->temp_min > INT_MIN; + + case RT_POWER_SUPPLY_PROP_TEMP_MAX: + return info->temp_max < INT_MAX; + + default: + return RT_FALSE; + } +} + +static rt_err_t power_supply_battery_info_get_property( + struct rt_power_supply_battery_info *info, enum rt_power_supply_property prop, + union rt_power_supply_property_val *val) +{ + switch (prop) + { + case RT_POWER_SUPPLY_PROP_TECHNOLOGY: + val->intval = info->technology; + break; + + case RT_POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: + val->intval = info->energy_full_design_uwh; + break; + + case RT_POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: + val->intval = info->charge_full_design_uah; + break; + + case RT_POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: + val->intval = info->voltage_min_design_uv; + break; + + case RT_POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: + val->intval = info->voltage_max_design_uv; + break; + + case RT_POWER_SUPPLY_PROP_PRECHARGE_CURRENT: + val->intval = info->precharge_current_ua; + break; + + case RT_POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: + val->intval = info->charge_term_current_ua; + break; + + case RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: + val->intval = info->constant_charge_current_max_ua; + break; + + case RT_POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: + val->intval = info->constant_charge_voltage_max_uv; + break; + + case RT_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN: + val->intval = info->temp_ambient_alert_min; + break; + + case RT_POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX: + val->intval = info->temp_ambient_alert_max; + break; + + case RT_POWER_SUPPLY_PROP_TEMP_ALERT_MIN: + val->intval = info->temp_alert_min; + break; + + case RT_POWER_SUPPLY_PROP_TEMP_ALERT_MAX: + val->intval = info->temp_alert_max; + break; + + case RT_POWER_SUPPLY_PROP_TEMP_MIN: + val->intval = info->temp_min; + break; + + case RT_POWER_SUPPLY_PROP_TEMP_MAX: + val->intval = info->temp_max; + break; + + default: + return -RT_EINVAL; + } + + return RT_EOK; +} + +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) +{ + if (!psy || !val) + { + return -RT_EINVAL; + } + + if (power_supply_have_property(psy, prop)) + { + return psy->ops->get_property(psy, prop, val); + } + else if (power_supply_battery_info_have_property(psy->battery_info, prop)) + { + return power_supply_battery_info_get_property(psy->battery_info, prop, val); + } + + return -RT_ENOSYS; +} + +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) +{ + if (!psy || !val) + { + return -RT_EINVAL; + } + + if (!psy->ops->set_property) + { + return -RT_ENOSYS; + } + + return psy->ops->set_property(psy, prop, val); +} + +void rt_power_supply_changed(struct rt_power_supply *psy) +{ + RT_ASSERT(psy != RT_NULL); + + rt_work_submit(&psy->changed_work, 0); +} + +struct rt_power_supply *rt_power_supply_get(struct rt_device *dev, const char *id) +{ + struct rt_power_supply *psy = RT_NULL; + + if (!dev || !id) + { + return rt_err_ptr(-RT_EINVAL); + } + +#ifdef RT_USING_OFW + if (dev->ofw_node) + { + struct rt_ofw_node *psy_np = rt_ofw_parse_phandle(dev->ofw_node, id, 0); + + if (!psy_np) + { + return psy; + } + + psy = rt_ofw_data(psy_np); + } +#endif /* RT_USING_OFW */ + + if (!psy) + { + struct rt_power_supply *psy_target, *psy_next; + + rt_spin_lock(&nodes_lock); + + rt_list_for_each_entry_safe(psy_target, psy_next, &power_supply_nodes, list) + { + if (!rt_strcmp(psy_target->dev->parent.name, id)) + { + psy = psy_target; + break; + } + } + + rt_spin_unlock(&nodes_lock); + } + + return psy; +} + +static void power_supply_release(struct rt_ref *r) +{ + struct rt_power_supply *psy = rt_container_of(r, struct rt_power_supply, ref); + + rt_power_supply_unregister(psy); +} + +void rt_power_supply_put(struct rt_power_supply *psy) +{ + if (!psy) + { + return; + } + + rt_ref_put(&psy->ref, power_supply_release); +} + +#if defined(RT_USING_CONSOLE) && defined(RT_USING_MSH) +const char * const type_str[] = +{ + [RT_POWER_SUPPLY_TYPE_UNKNOWN] = "UnKnown", + [RT_POWER_SUPPLY_TYPE_BATTERY] = "Battery", + [RT_POWER_SUPPLY_TYPE_UPS] = "UPS", + [RT_POWER_SUPPLY_TYPE_MAINS] = "Mains", + [RT_POWER_SUPPLY_TYPE_USB_SDP] = "USB SDP", + [RT_POWER_SUPPLY_TYPE_USB_DCP] = "USB DCP", + [RT_POWER_SUPPLY_TYPE_USB_CDP] = "USB CDP", + [RT_POWER_SUPPLY_TYPE_USB_ACA] = "USB ACA", + [RT_POWER_SUPPLY_TYPE_USB_TYPE_C] = "USB TypeC", + [RT_POWER_SUPPLY_TYPE_USB_PD] = "USB PD", + [RT_POWER_SUPPLY_TYPE_USB_PD_DRP] = "USB PD DRP", + [RT_POWER_SUPPLY_TYPE_USB_PD_PPS] = "USB PD PPS", + [RT_POWER_SUPPLY_TYPE_WIRELESS] = "Wireless", +}; + +const char * const status_str[] = +{ + [RT_POWER_SUPPLY_STATUS_UNKNOWN] = "UnKnown", + [RT_POWER_SUPPLY_STATUS_CHARGING] = "Charging", + [RT_POWER_SUPPLY_STATUS_DISCHARGING] = "Discharging", + [RT_POWER_SUPPLY_STATUS_NOT_CHARGING] = "Not Charging", + [RT_POWER_SUPPLY_STATUS_FULL] = "Full", +}; + +const char * const charge_type_str[] = +{ + [RT_POWER_SUPPLY_CHARGE_TYPE_UNKNOWN] = "Unknown", + [RT_POWER_SUPPLY_CHARGE_TYPE_NONE] = "None", + [RT_POWER_SUPPLY_CHARGE_TYPE_TRICKLE] = "Trickle", + [RT_POWER_SUPPLY_CHARGE_TYPE_FAST] = "Fast", + [RT_POWER_SUPPLY_CHARGE_TYPE_STANDARD] = "Standard", + [RT_POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE] = "Adaptive", + [RT_POWER_SUPPLY_CHARGE_TYPE_CUSTOM] = "Custom", + [RT_POWER_SUPPLY_CHARGE_TYPE_LONGLIFE] = "Longlife", + [RT_POWER_SUPPLY_CHARGE_TYPE_BYPASS] = "Bypass", +}; + +const char * const health_str[] = +{ + [RT_POWER_SUPPLY_HEALTH_UNKNOWN] = "Unknown", + [RT_POWER_SUPPLY_HEALTH_GOOD] = "Good", + [RT_POWER_SUPPLY_HEALTH_OVERHEAT] = "Overheat", + [RT_POWER_SUPPLY_HEALTH_DEAD] = "Dead", + [RT_POWER_SUPPLY_HEALTH_OVERVOLTAGE] = "Overvoltage", + [RT_POWER_SUPPLY_HEALTH_UNSPEC_FAILURE] = "Unspec Failure", + [RT_POWER_SUPPLY_HEALTH_COLD] = "Cold", + [RT_POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE] = "Watchdog Timer Expire", + [RT_POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE] = "Safety Timer Expire", + [RT_POWER_SUPPLY_HEALTH_OVERCURRENT] = "Overcurrent", + [RT_POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED] = "Calibration Required", + [RT_POWER_SUPPLY_HEALTH_WARM] = "Warm", + [RT_POWER_SUPPLY_HEALTH_COOL] = "Cool", + [RT_POWER_SUPPLY_HEALTH_HOT] = "Hot", + [RT_POWER_SUPPLY_HEALTH_NO_BATTERY] = "No Battery", +}; + +const char * const tech_str[] = +{ + [RT_POWER_SUPPLY_TECHNOLOGY_UNKNOWN] = "UnKnown", + [RT_POWER_SUPPLY_TECHNOLOGY_NiMH] = "NiMH", + [RT_POWER_SUPPLY_TECHNOLOGY_LION] = "LION", + [RT_POWER_SUPPLY_TECHNOLOGY_LIPO] = "LIPO", + [RT_POWER_SUPPLY_TECHNOLOGY_LiFe] = "LiFe", + [RT_POWER_SUPPLY_TECHNOLOGY_NiCd] = "NiCd", + [RT_POWER_SUPPLY_TECHNOLOGY_LiMn] = "LiMn", +}; + +static int list_power_supply(int argc, char**argv) +{ + struct rt_power_supply *psy, *psy_next; + union rt_power_supply_property_val propval = {}; + + rt_spin_lock(&nodes_lock); + + rt_list_for_each_entry_safe(psy, psy_next, &power_supply_nodes, list) + { + rt_spin_unlock(&nodes_lock); + + rt_kprintf("%s %s\n", rt_dm_dev_get_name(psy->dev), type_str[psy->type]); + + rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_STATUS, &propval); + rt_kprintf("status: %s\n", status_str[propval.intval]), propval.intval = 0; + + rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_CHARGE_TYPE, &propval); + rt_kprintf("charge type: %s\n", charge_type_str[propval.intval]), propval.intval = 0; + + rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_HEALTH, &propval); + rt_kprintf("health: %s\n", health_str[propval.intval]), propval.intval = 0; + + if (psy->battery_info) + { + struct rt_power_supply_battery_info *info = psy->battery_info; + + rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_CAPACITY, &propval); + rt_kprintf("capacity: %d%%\n", propval.intval), propval.intval = 0; + + rt_kprintf("technology: %s\n", tech_str[info->technology]); + rt_kprintf("energy full design: %u uWh\n", info->energy_full_design_uwh); + rt_kprintf("charge full design: %u uAh\n", info->charge_full_design_uah); + rt_kprintf("voltage design range: %u~%u uV\n", info->voltage_min_design_uv, info->voltage_max_design_uv); + rt_kprintf("precharge current: %u uA\n", info->precharge_current_ua); + rt_kprintf("charge term current: %u uA\n", info->charge_term_current_ua); + rt_kprintf("charge restart voltage: %u uV\n", info->charge_restart_voltage_uv); + rt_kprintf("constant charge current max: %u uA\n", info->constant_charge_current_max_ua); + rt_kprintf("constant charge voltage max: %u uV\n", info->constant_charge_voltage_max_uv); + rt_kprintf("temp ambient alert range: %+d.%u~%+d.%u C\n", + info->temp_ambient_alert_min / 1000, rt_abs(info->temp_ambient_alert_min) % 1000, + info->temp_ambient_alert_max / 1000, rt_abs(info->temp_ambient_alert_max) % 1000); + rt_kprintf("temp alert range: %+d.%u~%+d.%u C\n", + info->temp_alert_min / 1000, rt_abs(info->temp_alert_min) % 1000, + info->temp_alert_max / 1000, rt_abs(info->temp_alert_max) % 1000); + rt_kprintf("temp range: %+d.%u~%+d.%u C\n", + info->temp_min / 1000, rt_abs(info->temp_min) % 1000, + info->temp_max / 1000, rt_abs(info->temp_max) % 1000); + } + + rt_kputs("\n"); + + rt_spin_lock(&nodes_lock); + } + + rt_spin_unlock(&nodes_lock); + + return 0; +} +MSH_CMD_EXPORT(list_power_supply, dump all of power supply information); +#endif /* RT_USING_CONSOLE && RT_USING_MSH */