Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions components/drivers/include/drivers/power_supply.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Change Logs:
* Date Author Notes
* 2022-11-21 GuEe-GUI first version
* 2026-03-27 Evlers add snapshot helpers and public name getter
*/

#ifndef __POWER_SUPPLY_H__
Expand Down Expand Up @@ -274,4 +275,8 @@ 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);

const char *rt_power_supply_name(struct rt_power_supply *psy);
struct rt_power_supply **rt_power_supply_snapshot(rt_size_t *count);
void rt_power_supply_snapshot_free(struct rt_power_supply **nodes, rt_size_t count);

#endif /* __POWER_SUPPLY_H__ */
30 changes: 30 additions & 0 deletions components/drivers/include/drivers/regulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Change Logs:
* Date Author Notes
* 2023-09-23 GuEe-GUI first version
* 2026-03-27 Evlers add current control API and snapshot helpers
*/

#ifndef __REGULATOR_H__
Expand All @@ -18,6 +19,7 @@
#include <drivers/misc.h>

#define RT_REGULATOR_UVOLT_INVALID (((int)(RT_UINT32_MAX >> 1)))
#define RT_REGULATOR_UAMP_INVALID (((int)(RT_UINT32_MAX >> 1)))

struct rt_regulator_param
{
Expand Down Expand Up @@ -86,6 +88,8 @@ struct rt_regulator_ops
rt_bool_t (*is_enabled)(struct rt_regulator_node *reg);
rt_err_t (*set_voltage)(struct rt_regulator_node *reg, int min_uvolt, int max_uvolt);
int (*get_voltage)(struct rt_regulator_node *reg);
rt_err_t (*set_current)(struct rt_regulator_node *reg, int min_uamp, int max_uamp);
int (*get_current)(struct rt_regulator_node *reg);
rt_err_t (*set_mode)(struct rt_regulator_node *reg, rt_uint32_t mode);
rt_int32_t (*get_mode)(struct rt_regulator_node *reg);
rt_err_t (*set_ramp_delay)(struct rt_regulator_node *reg, int ramp);
Expand All @@ -98,6 +102,8 @@ struct rt_regulator_notifier;
#define RT_REGULATOR_MSG_DISABLE RT_BIT(1)
#define RT_REGULATOR_MSG_VOLTAGE_CHANGE RT_BIT(2)
#define RT_REGULATOR_MSG_VOLTAGE_CHANGE_ERR RT_BIT(3)
#define RT_REGULATOR_MSG_CURRENT_CHANGE RT_BIT(4)
#define RT_REGULATOR_MSG_CURRENT_CHANGE_ERR RT_BIT(5)

union rt_regulator_notifier_args
{
Expand All @@ -107,6 +113,12 @@ union rt_regulator_notifier_args
int min_uvolt;
int max_uvolt;
};
struct
{
int old_uamp;
int min_uamp;
int max_uamp;
};
};

typedef rt_err_t (*rt_regulator_notifier_callback)(struct rt_regulator_notifier *notifier,
Expand Down Expand Up @@ -140,9 +152,16 @@ rt_bool_t rt_regulator_is_supported_voltage(struct rt_regulator *reg, int min_uv
rt_err_t rt_regulator_set_voltage(struct rt_regulator *reg, int min_uvolt, int max_uvolt);
int rt_regulator_get_voltage(struct rt_regulator *reg);

rt_bool_t rt_regulator_is_supported_current(struct rt_regulator *reg, int min_uamp, int max_uamp);
rt_err_t rt_regulator_set_current(struct rt_regulator *reg, int min_uamp, int max_uamp);
int rt_regulator_get_current(struct rt_regulator *reg);

rt_err_t rt_regulator_set_mode(struct rt_regulator *reg, rt_uint32_t mode);
rt_int32_t rt_regulator_get_mode(struct rt_regulator *reg);

struct rt_regulator_node **rt_regulator_nodes_snapshot(rt_size_t *count);
void rt_regulator_nodes_snapshot_free(struct rt_regulator_node **nodes, rt_size_t count);

rt_inline rt_err_t rt_regulator_set_voltage_triplet(struct rt_regulator *reg,
int min_uvolt, int target_uvolt, int max_uvolt)
{
Expand All @@ -154,4 +173,15 @@ rt_inline rt_err_t rt_regulator_set_voltage_triplet(struct rt_regulator *reg,
return rt_regulator_set_voltage(reg, min_uvolt, max_uvolt);
}

rt_inline rt_err_t rt_regulator_set_current_triplet(struct rt_regulator *reg,
int min_uamp, int target_uamp, int max_uamp)
{
if (!rt_regulator_set_current(reg, target_uamp, max_uamp))
{
return RT_EOK;
}

return rt_regulator_set_current(reg, min_uamp, max_uamp);
}

#endif /* __REGULATOR_H__ */
17 changes: 9 additions & 8 deletions components/drivers/include/rtdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Date Author Notes
* 2012-01-08 bernard first version.
* 2014-07-12 bernard Add workqueue implementation.
* 2026-03-27 Evlers reorder regulator/power supply headers after DM deps
*/

#ifndef __RT_DEVICE_H__
Expand Down Expand Up @@ -118,10 +119,6 @@ extern "C" {
#endif /* RT_PCI_ENDPOINT */
#endif /* RT_USING_PCI */

#ifdef RT_USING_REGULATOR
#include "drivers/regulator.h"
#endif /* RT_USING_REGULATOR */

#ifdef RT_USING_RESET
#include "drivers/reset.h"
#endif /* RT_USING_RESET */
Expand All @@ -148,15 +145,19 @@ 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 */
#endif /* RT_USING_DM */

#ifdef RT_USING_REGULATOR
#include "drivers/regulator.h"
#endif /* RT_USING_REGULATOR */

#ifdef RT_USING_POWER_SUPPLY
#include "drivers/power_supply.h"
#endif /* RT_USING_POWER_SUPPLY */

#ifdef RT_USING_RTC
#include "drivers/dev_rtc.h"
#ifdef RT_USING_ALARM
Expand Down
3 changes: 2 additions & 1 deletion components/drivers/power/supply/Kconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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
Expand All @@ -27,6 +26,8 @@ config RT_POWER_SUPPLY_EMU
config RT_POWER_SUPPLY_CHARGER_GPIO
bool "GPIO charger"
depends on RT_USING_POWER_SUPPLY
depends on RT_USING_DM
depends on RT_USING_OFW
depends on RT_USING_PIN
default y

Expand Down
2 changes: 1 addition & 1 deletion components/drivers/power/supply/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if not GetDepend(['RT_USING_POWER_SUPPLY']):
cwd = GetCurrentDir()
CPPPATH = [cwd + '/../../include']

src = ['supply.c']
src = ['supply.c', 'supply_cmd.c']

if GetDepend(['RT_POWER_SUPPLY_DAEMON']):
src += ['supply-daemon.c']
Expand Down
5 changes: 5 additions & 0 deletions components/drivers/power/supply/emu-power.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Change Logs:
* Date Author Notes
* 2023-02-25 GuEe-GUI the first version
* 2026-03-27 Evlers allow building without DM by naming parent directly
*/

#include <rtthread.h>
Expand Down Expand Up @@ -299,7 +300,11 @@ static int emu_power_init(void)

rt_memset(ep, 0, sizeof(*ep));

#ifdef RT_USING_DM
rt_dm_dev_set_name(&ep->parent, "emu-power");
#else
ep->parent.parent.name = "emu-power";
#endif

ep->battery.dev = &ep->parent,
ep->battery.type = RT_POWER_SUPPLY_TYPE_BATTERY,
Expand Down
20 changes: 15 additions & 5 deletions components/drivers/power/supply/supply-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Change Logs:
* Date Author Notes
* 2023-02-25 GuEe-GUI the first version
* 2026-03-27 Evlers support builds without DM names and improve logging
*/

#include <rtdevice.h>
Expand All @@ -21,6 +22,15 @@ static rt_uint8_t raw_pm_sleep_mode = PM_RUN_MODE_MAX;
static rt_uint8_t last_pm_sleep_mode;
#endif

rt_inline const char *power_supply_dev_name(struct rt_device *dev)
{
#ifdef RT_USING_DM
return rt_dm_dev_get_name(dev);
#else
return dev ? dev->parent.name : "<no-dev>";
#endif
}

static rt_err_t daemon_power_supply_notify(struct rt_power_supply_notifier *notifier,
struct rt_power_supply *psy)
{
Expand Down Expand Up @@ -66,11 +76,11 @@ static rt_err_t daemon_power_supply_notify(struct rt_power_supply_notifier *noti
{
if (full_power)
{
LOG_I("%s: Power is full", rt_dm_dev_get_name(psy->dev));
LOG_I("%s: Power is full", power_supply_dev_name(psy->dev));
}
else
{
LOG_I("%s: Power is sufficient", rt_dm_dev_get_name(psy->dev));
LOG_I("%s: Power is sufficient", power_supply_dev_name(psy->dev));
}
}
}
Expand Down Expand Up @@ -109,12 +119,12 @@ static rt_err_t daemon_power_supply_notify(struct rt_power_supply_notifier *noti
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));
LOG_E("%s: Power is critical, poweroff now", power_supply_dev_name(psy->dev));
rt_hw_cpu_shutdown();
}
} while (0);

LOG_E("%s: Power is critical", rt_dm_dev_get_name(psy->dev));
LOG_E("%s: Power is critical", power_supply_dev_name(psy->dev));
}
else if (propval.intval <= 10)
{
Expand All @@ -136,7 +146,7 @@ static rt_err_t daemon_power_supply_notify(struct rt_power_supply_notifier *noti
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));
LOG_W("%s: Power is low", power_supply_dev_name(psy->dev));
}

#ifdef RT_USING_PM
Expand Down
Loading
Loading