Skip to content

Commit 79b364f

Browse files
committed
[drivers][power] add CLI helpers and current control support
- add msh commands to inspect/operate power supplies and regulators - expose snapshot/name helpers so shells and daemons can enumerate nodes safely - add current support for regulator - relax DM-only constraints in power/regulator stacks so basic builds work without DM/OFW - solve the problem of enabling counting for regulator
1 parent e41c7cc commit 79b364f

14 files changed

Lines changed: 1367 additions & 141 deletions

File tree

components/drivers/include/drivers/power_supply.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2022-11-21 GuEe-GUI first version
9+
* 2026-03-27 Evlers add snapshot helpers and public name getter
910
*/
1011

1112
#ifndef __POWER_SUPPLY_H__
@@ -274,4 +275,8 @@ void rt_power_supply_changed(struct rt_power_supply *psy);
274275
struct rt_power_supply *rt_power_supply_get(struct rt_device *dev, const char *id);
275276
void rt_power_supply_put(struct rt_power_supply *psy);
276277

278+
const char *rt_power_supply_name(struct rt_power_supply *psy);
279+
struct rt_power_supply **rt_power_supply_snapshot(rt_size_t *count);
280+
void rt_power_supply_snapshot_free(struct rt_power_supply **nodes, rt_size_t count);
281+
277282
#endif /* __POWER_SUPPLY_H__ */

components/drivers/include/drivers/regulator.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2023-09-23 GuEe-GUI first version
9+
* 2026-03-27 Evlers add current control API and snapshot helpers
910
*/
1011

1112
#ifndef __REGULATOR_H__
@@ -18,6 +19,7 @@
1819
#include <drivers/misc.h>
1920

2021
#define RT_REGULATOR_UVOLT_INVALID (((int)(RT_UINT32_MAX >> 1)))
22+
#define RT_REGULATOR_UAMP_INVALID (((int)(RT_UINT32_MAX >> 1)))
2123

2224
struct rt_regulator_param
2325
{
@@ -86,6 +88,8 @@ struct rt_regulator_ops
8688
rt_bool_t (*is_enabled)(struct rt_regulator_node *reg);
8789
rt_err_t (*set_voltage)(struct rt_regulator_node *reg, int min_uvolt, int max_uvolt);
8890
int (*get_voltage)(struct rt_regulator_node *reg);
91+
rt_err_t (*set_current)(struct rt_regulator_node *reg, int min_uamp, int max_uamp);
92+
int (*get_current)(struct rt_regulator_node *reg);
8993
rt_err_t (*set_mode)(struct rt_regulator_node *reg, rt_uint32_t mode);
9094
rt_int32_t (*get_mode)(struct rt_regulator_node *reg);
9195
rt_err_t (*set_ramp_delay)(struct rt_regulator_node *reg, int ramp);
@@ -98,6 +102,8 @@ struct rt_regulator_notifier;
98102
#define RT_REGULATOR_MSG_DISABLE RT_BIT(1)
99103
#define RT_REGULATOR_MSG_VOLTAGE_CHANGE RT_BIT(2)
100104
#define RT_REGULATOR_MSG_VOLTAGE_CHANGE_ERR RT_BIT(3)
105+
#define RT_REGULATOR_MSG_CURRENT_CHANGE RT_BIT(4)
106+
#define RT_REGULATOR_MSG_CURRENT_CHANGE_ERR RT_BIT(5)
101107

102108
union rt_regulator_notifier_args
103109
{
@@ -107,6 +113,12 @@ union rt_regulator_notifier_args
107113
int min_uvolt;
108114
int max_uvolt;
109115
};
116+
struct
117+
{
118+
int old_uamp;
119+
int min_uamp;
120+
int max_uamp;
121+
};
110122
};
111123

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

155+
rt_bool_t rt_regulator_is_supported_current(struct rt_regulator *reg, int min_uamp, int max_uamp);
156+
rt_err_t rt_regulator_set_current(struct rt_regulator *reg, int min_uamp, int max_uamp);
157+
int rt_regulator_get_current(struct rt_regulator *reg);
158+
143159
rt_err_t rt_regulator_set_mode(struct rt_regulator *reg, rt_uint32_t mode);
144160
rt_int32_t rt_regulator_get_mode(struct rt_regulator *reg);
145161

162+
struct rt_regulator_node **rt_regulator_nodes_snapshot(rt_size_t *count);
163+
void rt_regulator_nodes_snapshot_free(struct rt_regulator_node **nodes, rt_size_t count);
164+
146165
rt_inline rt_err_t rt_regulator_set_voltage_triplet(struct rt_regulator *reg,
147166
int min_uvolt, int target_uvolt, int max_uvolt)
148167
{
@@ -154,4 +173,15 @@ rt_inline rt_err_t rt_regulator_set_voltage_triplet(struct rt_regulator *reg,
154173
return rt_regulator_set_voltage(reg, min_uvolt, max_uvolt);
155174
}
156175

176+
rt_inline rt_err_t rt_regulator_set_current_triplet(struct rt_regulator *reg,
177+
int min_uamp, int target_uamp, int max_uamp)
178+
{
179+
if (!rt_regulator_set_current(reg, target_uamp, max_uamp))
180+
{
181+
return RT_EOK;
182+
}
183+
184+
return rt_regulator_set_current(reg, min_uamp, max_uamp);
185+
}
186+
157187
#endif /* __REGULATOR_H__ */

components/drivers/include/rtdevice.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Date Author Notes
88
* 2012-01-08 bernard first version.
99
* 2014-07-12 bernard Add workqueue implementation.
10+
* 2026-03-27 Evlers reorder regulator/power supply headers after DM deps
1011
*/
1112

1213
#ifndef __RT_DEVICE_H__
@@ -118,10 +119,6 @@ extern "C" {
118119
#endif /* RT_PCI_ENDPOINT */
119120
#endif /* RT_USING_PCI */
120121

121-
#ifdef RT_USING_REGULATOR
122-
#include "drivers/regulator.h"
123-
#endif /* RT_USING_REGULATOR */
124-
125122
#ifdef RT_USING_RESET
126123
#include "drivers/reset.h"
127124
#endif /* RT_USING_RESET */
@@ -148,15 +145,19 @@ extern "C" {
148145
#include "drivers/hwcache.h"
149146
#endif /* RT_USING_HWCACHE */
150147

151-
#ifdef RT_USING_POWER_SUPPLY
152-
#include "drivers/power_supply.h"
153-
#endif /* RT_USING_POWER_SUPPLY */
154-
155148
#ifdef RT_USING_NVMEM
156149
#include "drivers/nvmem.h"
157150
#endif /* RT_USING_NVMEM */
158151
#endif /* RT_USING_DM */
159152

153+
#ifdef RT_USING_REGULATOR
154+
#include "drivers/regulator.h"
155+
#endif /* RT_USING_REGULATOR */
156+
157+
#ifdef RT_USING_POWER_SUPPLY
158+
#include "drivers/power_supply.h"
159+
#endif /* RT_USING_POWER_SUPPLY */
160+
160161
#ifdef RT_USING_RTC
161162
#include "drivers/dev_rtc.h"
162163
#ifdef RT_USING_ALARM

components/drivers/power/supply/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
menuconfig RT_USING_POWER_SUPPLY
22
bool "Using Power supply class support"
3-
depends on RT_USING_DM
43
select RT_USING_ADT
54
select RT_USING_ADT_REF
65
select RT_USING_SYSTEM_WORKQUEUE
@@ -27,6 +26,8 @@ config RT_POWER_SUPPLY_EMU
2726
config RT_POWER_SUPPLY_CHARGER_GPIO
2827
bool "GPIO charger"
2928
depends on RT_USING_POWER_SUPPLY
29+
depends on RT_USING_DM
30+
depends on RT_USING_OFW
3031
depends on RT_USING_PIN
3132
default y
3233

components/drivers/power/supply/SConscript

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if not GetDepend(['RT_USING_POWER_SUPPLY']):
88
cwd = GetCurrentDir()
99
CPPPATH = [cwd + '/../../include']
1010

11-
src = ['supply.c']
11+
src = ['supply.c', 'supply_cmd.c']
1212

1313
if GetDepend(['RT_POWER_SUPPLY_DAEMON']):
1414
src += ['supply-daemon.c']

components/drivers/power/supply/emu-power.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2023-02-25 GuEe-GUI the first version
9+
* 2026-03-27 Evlers allow building without DM by naming parent directly
910
*/
1011

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

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

303+
#ifdef RT_USING_DM
302304
rt_dm_dev_set_name(&ep->parent, "emu-power");
305+
#else
306+
ep->parent.parent.name = "emu-power";
307+
#endif
303308

304309
ep->battery.dev = &ep->parent,
305310
ep->battery.type = RT_POWER_SUPPLY_TYPE_BATTERY,

components/drivers/power/supply/supply-daemon.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2023-02-25 GuEe-GUI the first version
9+
* 2026-03-27 Evlers support builds without DM names and improve logging
910
*/
1011

1112
#include <rtdevice.h>
@@ -21,6 +22,15 @@ static rt_uint8_t raw_pm_sleep_mode = PM_RUN_MODE_MAX;
2122
static rt_uint8_t last_pm_sleep_mode;
2223
#endif
2324

25+
static rt_inline const char *power_supply_dev_name(struct rt_device *dev)
26+
{
27+
#ifdef RT_USING_DM
28+
return rt_dm_dev_get_name(dev);
29+
#else
30+
return dev ? dev->parent.name : "<no-dev>";
31+
#endif
32+
}
33+
2434
static rt_err_t daemon_power_supply_notify(struct rt_power_supply_notifier *notifier,
2535
struct rt_power_supply *psy)
2636
{
@@ -66,11 +76,11 @@ static rt_err_t daemon_power_supply_notify(struct rt_power_supply_notifier *noti
6676
{
6777
if (full_power)
6878
{
69-
LOG_I("%s: Power is full", rt_dm_dev_get_name(psy->dev));
79+
LOG_I("%s: Power is full", power_supply_dev_name(psy->dev));
7080
}
7181
else
7282
{
73-
LOG_I("%s: Power is sufficient", rt_dm_dev_get_name(psy->dev));
83+
LOG_I("%s: Power is sufficient", power_supply_dev_name(psy->dev));
7484
}
7585
}
7686
}
@@ -109,12 +119,12 @@ static rt_err_t daemon_power_supply_notify(struct rt_power_supply_notifier *noti
109119
if (!rt_power_supply_get_property(psy, RT_POWER_SUPPLY_PROP_SCOPE, &propval) &&
110120
propval.intval == RT_POWER_SUPPLY_SCOPE_SYSTEM)
111121
{
112-
LOG_E("%s: Power is critical, poweroff now", rt_dm_dev_get_name(psy->dev));
122+
LOG_E("%s: Power is critical, poweroff now", power_supply_dev_name(psy->dev));
113123
rt_hw_cpu_shutdown();
114124
}
115125
} while (0);
116126

117-
LOG_E("%s: Power is critical", rt_dm_dev_get_name(psy->dev));
127+
LOG_E("%s: Power is critical", power_supply_dev_name(psy->dev));
118128
}
119129
else if (propval.intval <= 10)
120130
{
@@ -136,7 +146,7 @@ static rt_err_t daemon_power_supply_notify(struct rt_power_supply_notifier *noti
136146
pm_sleep_mode = PM_SLEEP_MODE_LIGHT;
137147
rt_pm_run_enter(PM_RUN_MODE_NORMAL_SPEED);
138148
#endif
139-
LOG_W("%s: Power is low", rt_dm_dev_get_name(psy->dev));
149+
LOG_W("%s: Power is low", power_supply_dev_name(psy->dev));
140150
}
141151

142152
#ifdef RT_USING_PM

0 commit comments

Comments
 (0)