summaryrefslogtreecommitdiff
path: root/components/drivers/regulator
diff options
context:
space:
mode:
authorEvlers <[email protected]>2026-04-07 11:50:11 +0800
committerGitHub <[email protected]>2026-04-07 11:50:11 +0800
commit8be4b05af6413ef47fc802377d0addd09df85d3b (patch)
tree211f74d0c0f1c5457937ed4c28d4fae58ed0eba2 /components/drivers/regulator
parent0861ee1e513e40c3144aa44ad335acea015b2ddd (diff)
[drivers][power] add CLI helpers and current control support#11301
- 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
Diffstat (limited to 'components/drivers/regulator')
-rw-r--r--components/drivers/regulator/Kconfig5
-rwxr-xr-xcomponents/drivers/regulator/SConscript2
-rw-r--r--components/drivers/regulator/regulator.c281
-rw-r--r--components/drivers/regulator/regulator_cmd.c548
-rw-r--r--components/drivers/regulator/regulator_dm.h5
5 files changed, 829 insertions, 12 deletions
diff --git a/components/drivers/regulator/Kconfig b/components/drivers/regulator/Kconfig
index 9137bc4e58..17d2f5b17a 100644
--- a/components/drivers/regulator/Kconfig
+++ b/components/drivers/regulator/Kconfig
@@ -2,12 +2,13 @@ menuconfig RT_USING_REGULATOR
bool "Using Voltage and Current Regulator"
select RT_USING_ADT
select RT_USING_ADT_REF
- depends on RT_USING_DM
default n
config RT_REGULATOR_FIXED
bool "Fixed regulator support"
depends on RT_USING_REGULATOR
+ depends on RT_USING_DM
+ depends on RT_USING_OFW
depends on RT_USING_PIN
depends on RT_USING_PINCTRL
default y
@@ -15,6 +16,8 @@ config RT_REGULATOR_FIXED
config RT_REGULATOR_GPIO
bool "GPIO regulator support"
depends on RT_USING_REGULATOR
+ depends on RT_USING_DM
+ depends on RT_USING_OFW
depends on RT_USING_PIN
default y
diff --git a/components/drivers/regulator/SConscript b/components/drivers/regulator/SConscript
index f95f49b196..2069f50869 100755
--- a/components/drivers/regulator/SConscript
+++ b/components/drivers/regulator/SConscript
@@ -8,7 +8,7 @@ if not GetDepend(['RT_USING_REGULATOR']):
cwd = GetCurrentDir()
CPPPATH = [cwd + '/../include']
-src = ['regulator.c', 'regulator_dm.c']
+src = ['regulator.c', 'regulator_dm.c', 'regulator_cmd.c']
if GetDepend(['RT_REGULATOR_FIXED']):
src += ['regulator-fixed.c']
diff --git a/components/drivers/regulator/regulator.c b/components/drivers/regulator/regulator.c
index d335c3b680..3a852a0590 100644
--- a/components/drivers/regulator/regulator.c
+++ b/components/drivers/regulator/regulator.c
@@ -6,32 +6,70 @@
* Change Logs:
* Date Author Notes
* 2023-09-23 GuEe-GUI first version
+ * 2026-03-27 Evlers add current support and break away from reliance on DM,
+ * and solve the problem of enabling counting.
*/
#include <rtthread.h>
#include <rtservice.h>
+#include <rtdevice.h>
#define DBG_TAG "rtdm.regulator"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
-#include <drivers/ofw.h>
-#include <drivers/platform.h>
-#include <drivers/regulator.h>
struct rt_regulator
{
struct rt_regulator_node *reg_np;
};
+struct rt_regulator_record
+{
+ rt_list_t list;
+ struct rt_regulator_node *reg_np;
+};
+
static RT_DEFINE_SPINLOCK(_regulator_lock);
+static rt_list_t _regulator_records = RT_LIST_OBJECT_INIT(_regulator_records);
static rt_err_t regulator_enable(struct rt_regulator_node *reg_np);
static rt_err_t regulator_disable(struct rt_regulator_node *reg_np);
+static struct rt_regulator_record *regulator_find_record_by_name(const char *name)
+{
+ struct rt_regulator_record *record = RT_NULL;
+
+ rt_list_for_each_entry(record, &_regulator_records, list)
+ {
+ if (!rt_strcmp(record->reg_np->supply_name, name))
+ {
+ return record;
+ }
+ }
+
+ return RT_NULL;
+}
+
+static struct rt_regulator_record *regulator_find_record_by_node(struct rt_regulator_node *reg_np)
+{
+ struct rt_regulator_record *record = RT_NULL;
+
+ rt_list_for_each_entry(record, &_regulator_records, list)
+ {
+ if (record->reg_np == reg_np)
+ {
+ return record;
+ }
+ }
+
+ return RT_NULL;
+}
+
rt_err_t rt_regulator_register(struct rt_regulator_node *reg_np)
{
rt_err_t err;
+ struct rt_regulator_record *record;
const struct rt_regulator_param *param;
if (!reg_np || !reg_np->dev || !reg_np->param || !reg_np->ops)
@@ -59,6 +97,28 @@ rt_err_t rt_regulator_register(struct rt_regulator_node *reg_np)
}
}
+ rt_hw_spin_lock(&_regulator_lock.lock);
+
+ if (regulator_find_record_by_name(reg_np->supply_name))
+ {
+ rt_hw_spin_unlock(&_regulator_lock.lock);
+ return -RT_EBUSY;
+ }
+
+ record = rt_calloc(1, sizeof(*record));
+
+ if (!record)
+ {
+ rt_hw_spin_unlock(&_regulator_lock.lock);
+ return -RT_ENOMEM;
+ }
+
+ record->reg_np = reg_np;
+ rt_list_init(&record->list);
+ rt_list_insert_before(&_regulator_records, &record->list);
+
+ rt_hw_spin_unlock(&_regulator_lock.lock);
+
#ifdef RT_USING_OFW
if (reg_np->dev->ofw_node)
{
@@ -77,6 +137,7 @@ rt_err_t rt_regulator_register(struct rt_regulator_node *reg_np)
rt_err_t rt_regulator_unregister(struct rt_regulator_node *reg_np)
{
rt_err_t err = RT_EOK;
+ struct rt_regulator_record *record;
if (!reg_np)
{
@@ -109,9 +170,21 @@ rt_err_t rt_regulator_unregister(struct rt_regulator_node *reg_np)
reg_np->parent = RT_NULL;
rt_list_remove(&reg_np->list);
+ record = regulator_find_record_by_node(reg_np);
+
+ if (record)
+ {
+ rt_list_remove(&record->list);
+ }
+
_unlock:
rt_hw_spin_unlock(&_regulator_lock.lock);
+ if (!err && record)
+ {
+ rt_free(record);
+ }
+
return err;
}
@@ -302,6 +375,7 @@ static rt_err_t regulator_enable(struct rt_regulator_node *reg_np)
rt_err_t rt_regulator_enable(struct rt_regulator *reg)
{
rt_err_t err;
+ int enabled_cnt;
if (!reg)
{
@@ -310,11 +384,20 @@ rt_err_t rt_regulator_enable(struct rt_regulator *reg)
if (rt_regulator_is_enabled(reg))
{
+ rt_atomic_add(&reg->reg_np->enabled_count, 1);
return RT_EOK;
}
rt_hw_spin_lock(&_regulator_lock.lock);
+ enabled_cnt = rt_atomic_load(&reg->reg_np->enabled_count);
+ if (enabled_cnt > 0)
+ {
+ rt_atomic_add(&reg->reg_np->enabled_count, 1);
+ rt_hw_spin_unlock(&_regulator_lock.lock);
+ return RT_EOK;
+ }
+
err = regulator_enable(reg->reg_np);
rt_hw_spin_unlock(&_regulator_lock.lock);
@@ -352,6 +435,7 @@ static rt_err_t regulator_disable(struct rt_regulator_node *reg_np)
rt_err_t rt_regulator_disable(struct rt_regulator *reg)
{
rt_err_t err;
+ int enabled_cnt;
if (!reg)
{
@@ -363,15 +447,21 @@ rt_err_t rt_regulator_disable(struct rt_regulator *reg)
return RT_EOK;
}
- if (rt_atomic_load(&reg->reg_np->enabled_count) != 0)
+ rt_hw_spin_lock(&_regulator_lock.lock);
+
+ enabled_cnt = rt_atomic_load(&reg->reg_np->enabled_count);
+ if (enabled_cnt > 0)
{
rt_atomic_sub(&reg->reg_np->enabled_count, 1);
+ enabled_cnt--;
- return RT_EOK;
+ if (enabled_cnt > 0)
+ {
+ rt_hw_spin_unlock(&_regulator_lock.lock);
+ return RT_EOK;
+ }
}
- rt_hw_spin_lock(&_regulator_lock.lock);
-
err = regulator_disable(reg->reg_np);
rt_hw_spin_unlock(&_regulator_lock.lock);
@@ -440,6 +530,41 @@ static rt_err_t regulator_set_voltage(struct rt_regulator_node *reg_np, int min_
return err;
}
+static rt_err_t regulator_set_current(struct rt_regulator_node *reg_np, int min_uamp, int max_uamp)
+{
+ rt_err_t err = RT_EOK;
+
+ if (reg_np->ops->set_current)
+ {
+ union rt_regulator_notifier_args args;
+
+ RT_ASSERT(reg_np->ops->get_current != RT_NULL);
+
+ args.old_uamp = reg_np->ops->get_current(reg_np);
+ args.min_uamp = min_uamp;
+ args.max_uamp = max_uamp;
+
+ err = regulator_notifier_call_chain(reg_np, RT_REGULATOR_MSG_CURRENT_CHANGE, &args);
+
+ if (!err)
+ {
+ err = reg_np->ops->set_current(reg_np, min_uamp, max_uamp);
+ }
+
+ if (err)
+ {
+ regulator_notifier_call_chain(reg_np, RT_REGULATOR_MSG_CURRENT_CHANGE_ERR,
+ (void *)(rt_base_t)args.old_uamp);
+ }
+ }
+ else
+ {
+ err = -RT_ENOSYS;
+ }
+
+ return err;
+}
+
rt_bool_t rt_regulator_is_supported_voltage(struct rt_regulator *reg, int min_uvolt, int max_uvolt)
{
const struct rt_regulator_param *param;
@@ -502,6 +627,71 @@ int rt_regulator_get_voltage(struct rt_regulator *reg)
return uvolt;
}
+rt_bool_t rt_regulator_is_supported_current(struct rt_regulator *reg, int min_uamp, int max_uamp)
+{
+ const struct rt_regulator_param *param;
+
+ if (!reg)
+ {
+ return RT_FALSE;
+ }
+
+ param = reg->reg_np->param;
+
+ if (!param || param->max_uamp <= 0)
+ {
+ return RT_FALSE;
+ }
+
+ return param->min_uamp <= min_uamp && param->max_uamp >= max_uamp;
+}
+
+rt_err_t rt_regulator_set_current(struct rt_regulator *reg, int min_uamp, int max_uamp)
+{
+ rt_err_t err;
+
+ if (!reg)
+ {
+ return -RT_EINVAL;
+ }
+
+ rt_hw_spin_lock(&_regulator_lock.lock);
+
+ err = regulator_set_current(reg->reg_np, min_uamp, max_uamp);
+
+ rt_hw_spin_unlock(&_regulator_lock.lock);
+
+ return err;
+}
+
+int rt_regulator_get_current(struct rt_regulator *reg)
+{
+ int uamp = RT_REGULATOR_UAMP_INVALID;
+ struct rt_regulator_node *reg_np;
+
+ if (!reg)
+ {
+ return -RT_EINVAL;
+ }
+
+ rt_hw_spin_lock(&_regulator_lock.lock);
+
+ reg_np = reg->reg_np;
+
+ if (reg_np->ops->get_current)
+ {
+ uamp = reg_np->ops->get_current(reg_np);
+ }
+ else
+ {
+ uamp = -RT_ENOSYS;
+ }
+
+ rt_hw_spin_unlock(&_regulator_lock.lock);
+
+ return uamp;
+}
+
rt_err_t rt_regulator_set_mode(struct rt_regulator *reg, rt_uint32_t mode)
{
rt_err_t err;
@@ -603,14 +793,14 @@ struct rt_regulator *rt_regulator_get(struct rt_device *dev, const char *id)
struct rt_regulator *reg = RT_NULL;
struct rt_regulator_node *reg_np = RT_NULL;
- if (!dev || !id)
+ if (!id)
{
reg = rt_err_ptr(-RT_EINVAL);
goto _end;
}
#ifdef RT_USING_OFW
- if (dev->ofw_node)
+ if (dev && dev->ofw_node)
{
rt_phandle supply_phandle;
struct rt_ofw_node *np = dev->ofw_node;
@@ -641,6 +831,19 @@ struct rt_regulator *rt_regulator_get(struct rt_device *dev, const char *id)
if (!reg_np)
{
+ struct rt_regulator_record *record;
+
+ rt_hw_spin_lock(&_regulator_lock.lock);
+ record = regulator_find_record_by_name(id);
+ if (record)
+ {
+ reg_np = record->reg_np;
+ }
+ rt_hw_spin_unlock(&_regulator_lock.lock);
+ }
+
+ if (!reg_np)
+ {
goto _end;
}
@@ -682,3 +885,63 @@ void rt_regulator_put(struct rt_regulator *reg)
rt_ref_put(&reg->reg_np->ref, &regulator_release);
rt_free(reg);
}
+
+struct rt_regulator_node **rt_regulator_nodes_snapshot(rt_size_t *count)
+{
+ struct rt_regulator_record *record;
+ struct rt_regulator_node **nodes;
+ rt_size_t total = 0;
+ rt_size_t idx = 0;
+
+ if (!count)
+ {
+ return RT_NULL;
+ }
+
+ *count = 0;
+
+ rt_hw_spin_lock(&_regulator_lock.lock);
+ rt_list_for_each_entry(record, &_regulator_records, list)
+ {
+ total++;
+ }
+ rt_hw_spin_unlock(&_regulator_lock.lock);
+
+ if (!total)
+ {
+ return RT_NULL;
+ }
+
+ nodes = rt_calloc(total, sizeof(*nodes));
+ if (!nodes)
+ {
+ return RT_NULL;
+ }
+
+ rt_hw_spin_lock(&_regulator_lock.lock);
+ rt_list_for_each_entry(record, &_regulator_records, list)
+ {
+ nodes[idx] = record->reg_np;
+ rt_ref_get(&record->reg_np->ref);
+ idx++;
+ }
+ rt_hw_spin_unlock(&_regulator_lock.lock);
+
+ *count = total;
+ return nodes;
+}
+
+void rt_regulator_nodes_snapshot_free(struct rt_regulator_node **nodes, rt_size_t count)
+{
+ if (!nodes)
+ {
+ return;
+ }
+
+ while (count--)
+ {
+ rt_ref_put(&nodes[count]->ref, &regulator_release);
+ }
+
+ rt_free(nodes);
+}
diff --git a/components/drivers/regulator/regulator_cmd.c b/components/drivers/regulator/regulator_cmd.c
new file mode 100644
index 0000000000..c3438b2498
--- /dev/null
+++ b/components/drivers/regulator/regulator_cmd.c
@@ -0,0 +1,548 @@
+/*
+ * Copyright (c) 2006-2026, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2026-03-27 Evlers first version
+ */
+
+#include <rtdevice.h>
+
+#ifdef RT_USING_FINSH
+
+#include <limits.h>
+#include <stdlib.h>
+
+static void regulator_msh_usage(void)
+{
+ rt_kprintf("Usage:\n");
+ rt_kprintf(" regulator list\n");
+ rt_kprintf(" regulator info <name>\n");
+ rt_kprintf(" regulator <name> on|off|status\n");
+ rt_kprintf(" regulator <name> getv\n");
+ rt_kprintf(" regulator <name> setv <min_uV> [max_uV]\n");
+ rt_kprintf(" regulator <name> getc\n");
+ rt_kprintf(" regulator <name> setc <min_uA> [max_uA]\n");
+}
+
+static rt_bool_t regulator_node_is_enabled(struct rt_regulator_node *node)
+{
+ if (!node)
+ {
+ return RT_FALSE;
+ }
+
+ if (node->ops->is_enabled)
+ {
+ return node->ops->is_enabled(node);
+ }
+
+ return rt_atomic_load(&node->enabled_count) > 0;
+}
+
+static rt_err_t regulator_msh_parse_value(const char *str, int *out)
+{
+ char *endptr;
+ long value;
+
+ if (!str || !out)
+ {
+ return -RT_EINVAL;
+ }
+
+ value = strtol(str, &endptr, 10);
+ if (endptr == str || *endptr != '\0')
+ {
+ return -RT_EINVAL;
+ }
+
+ if (value <= 0 || value > INT_MAX)
+ {
+ return -RT_EINVAL;
+ }
+
+ *out = (int)value;
+ return RT_EOK;
+}
+
+static rt_err_t regulator_msh_get_reg(const char *name, struct rt_regulator **out)
+{
+ struct rt_regulator *reg;
+
+ if (!name || !out)
+ {
+ return -RT_EINVAL;
+ }
+
+ reg = rt_regulator_get(RT_NULL, name);
+ if (!reg)
+ {
+ rt_kprintf("regulator '%s' not found\n", name);
+ return -RT_ERROR;
+ }
+
+ if (rt_is_err(reg))
+ {
+ rt_err_t err = rt_ptr_err(reg);
+
+ rt_kprintf("regulator '%s' get failed: %d\n", name, err);
+ return err;
+ }
+
+ *out = reg;
+ return RT_EOK;
+}
+
+static struct rt_regulator_node *regulator_find_node_by_name(const char *name)
+{
+ struct rt_regulator_node **nodes;
+ struct rt_regulator_node *result = RT_NULL;
+ rt_size_t count = 0;
+ rt_size_t i;
+
+ if (!name)
+ {
+ return RT_NULL;
+ }
+
+ nodes = rt_regulator_nodes_snapshot(&count);
+ if (!nodes)
+ {
+ return RT_NULL;
+ }
+
+ for (i = 0; i < count; ++i)
+ {
+ if (nodes[i]->supply_name && !rt_strcmp(nodes[i]->supply_name, name))
+ {
+ result = nodes[i];
+ break;
+ }
+ }
+
+ rt_regulator_nodes_snapshot_free(nodes, count);
+ return result;
+}
+
+static rt_err_t regulator_msh_info(const char *name)
+{
+ struct rt_regulator *reg;
+ struct rt_regulator_node *node;
+ const struct rt_regulator_param *param;
+ rt_err_t err;
+ int value;
+
+ node = regulator_find_node_by_name(name);
+ if (!node)
+ {
+ rt_kprintf("regulator '%s' not found\n", name ? name : "(null)");
+ return -RT_ERROR;
+ }
+
+ err = regulator_msh_get_reg(name, &reg);
+ if (err)
+ {
+ return err;
+ }
+
+ param = node->param;
+
+ rt_kprintf("Regulator info:\n");
+ rt_kprintf(" name : %s\n", node->supply_name ? node->supply_name : "(null)");
+ rt_kprintf(" state : %s\n", regulator_node_is_enabled(node) ? "on" : "off");
+ rt_kprintf(" enabled_cnt : %d\n", rt_atomic_load(&node->enabled_count));
+
+ if (param)
+ {
+ rt_kprintf(" boot_on : %d\n", param->boot_on);
+ rt_kprintf(" always_on : %d\n", param->always_on);
+ if (param->min_uvolt || param->max_uvolt)
+ {
+ rt_kprintf(" volt_range : [%d, %d] uV\n", param->min_uvolt, param->max_uvolt);
+ }
+ if (param->min_uamp || param->max_uamp)
+ {
+ rt_kprintf(" curr_range : [%d, %d] uA\n", param->min_uamp, param->max_uamp);
+ }
+ if (param->enable_delay)
+ {
+ rt_kprintf(" enable_delay: %d us\n", param->enable_delay);
+ }
+ if (param->off_on_delay)
+ {
+ rt_kprintf(" off_on_delay: %d us\n", param->off_on_delay);
+ }
+ }
+
+ value = rt_regulator_get_voltage(reg);
+ if (value >= 0)
+ {
+ rt_kprintf(" voltage_now : %d uV\n", value);
+ }
+
+ value = rt_regulator_get_current(reg);
+ if (value >= 0)
+ {
+ rt_kprintf(" current_now : %d uA\n", value);
+ }
+
+ value = rt_regulator_get_mode(reg);
+ if (value >= 0)
+ {
+ rt_kprintf(" mode : 0x%X\n", value);
+ }
+
+ rt_regulator_put(reg);
+
+ return RT_EOK;
+}
+
+static void regulator_msh_list(void)
+{
+ struct rt_regulator_node **nodes;
+ rt_size_t count = 0;
+ rt_size_t idx;
+
+ nodes = rt_regulator_nodes_snapshot(&count);
+
+ rt_kprintf("Regulator list:\n");
+
+ if (!nodes || !count)
+ {
+ rt_kprintf(" (none)\n");
+ return;
+ }
+
+ for (idx = 0; idx < count; idx++)
+ {
+ rt_kprintf("%2d) %-20s state=%-3s boot_on=%d always_on=%d\n",
+ (int)(idx + 1),
+ nodes[idx]->supply_name ? nodes[idx]->supply_name : "(null)",
+ regulator_node_is_enabled(nodes[idx]) ? "on" : "off",
+ nodes[idx]->param ? nodes[idx]->param->boot_on : 0,
+ nodes[idx]->param ? nodes[idx]->param->always_on : 0);
+ }
+
+ rt_kprintf("Total: %d\n", (int)count);
+ rt_regulator_nodes_snapshot_free(nodes, count);
+}
+
+static rt_err_t regulator_msh_switch(const char *name, rt_bool_t enable)
+{
+ struct rt_regulator *reg;
+ struct rt_regulator_node *node;
+ rt_err_t err;
+ int enabled_cnt = -1;
+
+ err = regulator_msh_get_reg(name, &reg);
+ if (err)
+ {
+ return err;
+ }
+
+ node = regulator_find_node_by_name(name);
+
+ if (enable)
+ {
+ err = rt_regulator_enable(reg);
+ }
+ else
+ {
+ err = rt_regulator_disable(reg);
+ }
+
+ if (err)
+ {
+ rt_kprintf("%s '%s' failed: %d\n", enable ? "enable" : "disable", name, err);
+ }
+ else
+ {
+ if (node)
+ {
+ enabled_cnt = (int)rt_atomic_load(&node->enabled_count);
+ }
+
+ if (enabled_cnt >= 0)
+ {
+ rt_kprintf("regulator '%s' %s (enabled_cnt=%d)\n", name,
+ enable ? "enabled" : "disabled", enabled_cnt);
+ }
+ else
+ {
+ rt_kprintf("regulator '%s' %s\n", name, enable ? "enabled" : "disabled");
+ }
+ }
+
+ rt_regulator_put(reg);
+ return err;
+}
+
+static rt_err_t regulator_msh_status(const char *name)
+{
+ struct rt_regulator *reg;
+ rt_err_t err;
+
+ err = regulator_msh_get_reg(name, &reg);
+ if (err)
+ {
+ return err;
+ }
+
+ rt_kprintf("regulator '%s' is %s\n", name, rt_regulator_is_enabled(reg) ? "on" : "off");
+ rt_regulator_put(reg);
+
+ return RT_EOK;
+}
+
+static rt_err_t regulator_msh_get_voltage_cmd(const char *name)
+{
+ struct rt_regulator *reg;
+ rt_err_t err;
+ int value;
+
+ err = regulator_msh_get_reg(name, &reg);
+ if (err)
+ {
+ return err;
+ }
+
+ value = rt_regulator_get_voltage(reg);
+ if (value < 0)
+ {
+ rt_kprintf("get voltage of '%s' failed: %d\n", name, value);
+ err = value;
+ }
+ else
+ {
+ rt_kprintf("regulator '%s' voltage: %d uV\n", name, value);
+ }
+
+ rt_regulator_put(reg);
+ return err;
+}
+
+static rt_err_t regulator_msh_set_voltage_cmd(const char *name, const char *min_str, const char *max_str)
+{
+ struct rt_regulator *reg;
+ int min_uvolt;
+ int max_uvolt;
+ rt_err_t err;
+
+ if ((err = regulator_msh_parse_value(min_str, &min_uvolt)))
+ {
+ rt_kprintf("invalid min_uV: %s\n", min_str ? min_str : "(null)");
+ return err;
+ }
+
+ if (max_str)
+ {
+ if ((err = regulator_msh_parse_value(max_str, &max_uvolt)))
+ {
+ rt_kprintf("invalid max_uV: %s\n", max_str);
+ return err;
+ }
+ }
+ else
+ {
+ max_uvolt = min_uvolt;
+ }
+
+ if (max_uvolt < min_uvolt)
+ {
+ rt_kprintf("max_uV must be >= min_uV\n");
+ return -RT_EINVAL;
+ }
+
+ err = regulator_msh_get_reg(name, &reg);
+ if (err)
+ {
+ return err;
+ }
+
+ err = rt_regulator_set_voltage(reg, min_uvolt, max_uvolt);
+ if (err)
+ {
+ rt_kprintf("set voltage of '%s' failed: %d\n", name, err);
+ }
+ else
+ {
+ rt_kprintf("regulator '%s' voltage set to [%d, %d] uV\n", name, min_uvolt, max_uvolt);
+ }
+
+ rt_regulator_put(reg);
+ return err;
+}
+
+static rt_err_t regulator_msh_get_current_cmd(const char *name)
+{
+ struct rt_regulator *reg;
+ rt_err_t err;
+ int value;
+
+ err = regulator_msh_get_reg(name, &reg);
+ if (err)
+ {
+ return err;
+ }
+
+ value = rt_regulator_get_current(reg);
+ if (value < 0)
+ {
+ rt_kprintf("get current of '%s' failed: %d\n", name, value);
+ err = value;
+ }
+ else
+ {
+ rt_kprintf("regulator '%s' current: %d uA\n", name, value);
+ }
+
+ rt_regulator_put(reg);
+ return err;
+}
+
+static rt_err_t regulator_msh_set_current_cmd(const char *name, const char *min_str, const char *max_str)
+{
+ struct rt_regulator *reg;
+ int min_uamp;
+ int max_uamp;
+ rt_err_t err;
+
+ if ((err = regulator_msh_parse_value(min_str, &min_uamp)))
+ {
+ rt_kprintf("invalid min_uA: %s\n", min_str ? min_str : "(null)");
+ return err;
+ }
+
+ if (max_str)
+ {
+ if ((err = regulator_msh_parse_value(max_str, &max_uamp)))
+ {
+ rt_kprintf("invalid max_uA: %s\n", max_str);
+ return err;
+ }
+ }
+ else
+ {
+ max_uamp = min_uamp;
+ }
+
+ if (max_uamp < min_uamp)
+ {
+ rt_kprintf("max_uA must be >= min_uA\n");
+ return -RT_EINVAL;
+ }
+
+ err = regulator_msh_get_reg(name, &reg);
+ if (err)
+ {
+ return err;
+ }
+
+ if (!rt_regulator_is_supported_current(reg, min_uamp, max_uamp))
+ {
+ rt_kprintf("regulator '%s' does not support current setting in [%d, %d] uA\n",
+ name, min_uamp, max_uamp);
+ rt_regulator_put(reg);
+ return -RT_ENOSYS;
+ }
+
+ err = rt_regulator_set_current(reg, min_uamp, max_uamp);
+ if (err)
+ {
+ rt_kprintf("set current of '%s' failed: %d\n", name, err);
+ }
+ else
+ {
+ rt_kprintf("regulator '%s' current set to [%d, %d] uA\n", name, min_uamp, max_uamp);
+ }
+
+ rt_regulator_put(reg);
+ return err;
+}
+
+static int regulator_msh(int argc, char **argv)
+{
+ if (argc < 2)
+ {
+ regulator_msh_usage();
+ return RT_EOK;
+ }
+
+ if (!rt_strcmp(argv[1], "list"))
+ {
+ regulator_msh_list();
+ return RT_EOK;
+ }
+
+ if (!rt_strcmp(argv[1], "info"))
+ {
+ if (argc < 3)
+ {
+ regulator_msh_usage();
+ return -RT_EINVAL;
+ }
+
+ return regulator_msh_info(argv[2]);
+ }
+
+ if (argc < 3)
+ {
+ regulator_msh_usage();
+ return -RT_EINVAL;
+ }
+
+ if (!rt_strcmp(argv[2], "on"))
+ {
+ return regulator_msh_switch(argv[1], RT_TRUE);
+ }
+
+ if (!rt_strcmp(argv[2], "off"))
+ {
+ return regulator_msh_switch(argv[1], RT_FALSE);
+ }
+
+ if (!rt_strcmp(argv[2], "status"))
+ {
+ return regulator_msh_status(argv[1]);
+ }
+
+ if (!rt_strcmp(argv[2], "getv"))
+ {
+ return regulator_msh_get_voltage_cmd(argv[1]);
+ }
+
+ if (!rt_strcmp(argv[2], "setv"))
+ {
+ if (argc < 4)
+ {
+ regulator_msh_usage();
+ return -RT_EINVAL;
+ }
+
+ return regulator_msh_set_voltage_cmd(argv[1], argv[3], argc >= 5 ? argv[4] : RT_NULL);
+ }
+
+ if (!rt_strcmp(argv[2], "getc"))
+ {
+ return regulator_msh_get_current_cmd(argv[1]);
+ }
+
+ if (!rt_strcmp(argv[2], "setc"))
+ {
+ if (argc < 4)
+ {
+ regulator_msh_usage();
+ return -RT_EINVAL;
+ }
+
+ return regulator_msh_set_current_cmd(argv[1], argv[3], argc >= 5 ? argv[4] : RT_NULL);
+ }
+
+ regulator_msh_usage();
+ return -RT_EINVAL;
+}
+MSH_CMD_EXPORT_ALIAS(regulator_msh, regulator, regulator command);
+
+#endif /* RT_USING_FINSH */
diff --git a/components/drivers/regulator/regulator_dm.h b/components/drivers/regulator/regulator_dm.h
index 207bb12794..eabe4912f9 100644
--- a/components/drivers/regulator/regulator_dm.h
+++ b/components/drivers/regulator/regulator_dm.h
@@ -6,6 +6,7 @@
* Change Logs:
* Date Author Notes
* 2023-09-23 GuEe-GUI first version
+ * 2026-03-27 Evlers stub out regulator_ofw_parse when OFW disabled
*/
#ifndef __REGULATOR_DM_H__
@@ -17,8 +18,10 @@
#ifdef RT_USING_OFW
rt_err_t regulator_ofw_parse(struct rt_ofw_node *np, struct rt_regulator_param *param);
#else
-rt_inline rt_err_t regulator_ofw_parse(struct rt_ofw_node *np, struct rt_regulator_param *param);
+rt_inline rt_err_t regulator_ofw_parse(void *np, struct rt_regulator_param *param)
{
+ RT_UNUSED(np);
+ RT_UNUSED(param);
return RT_EOK;
}
#endif /* RT_USING_OFW */