Skip to content

Commit 9d7039f

Browse files
danieldegrassemmahadevan108
authored andcommitted
drivers: regulator: add regulator shell
Add shell for interacting with regulator devices, to enable users to test power management settings quickly and verify that their regulator is functioning as expected. Also allows users to interact with regulator PMIC devices Signed-off-by: Daniel DeGrasse <[email protected]>
1 parent 5c484e2 commit 9d7039f

File tree

4 files changed

+152
-1
lines changed

4 files changed

+152
-1
lines changed

drivers/regulator/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ zephyr_library()
55

66
zephyr_library_sources_ifdef(CONFIG_REGULATOR_FIXED regulator_fixed.c)
77
zephyr_library_sources_ifdef(CONFIG_REGULATOR_PMIC regulator_pmic.c)
8+
zephyr_library_sources_ifdef(CONFIG_REGULATOR_SHELL regulator_shell.c)

drivers/regulator/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ menuconfig REGULATOR
99

1010
if REGULATOR
1111

12+
config REGULATOR_SHELL
13+
bool "Regulator shell"
14+
default y
15+
depends on SHELL
16+
help
17+
Enable regulator shell framework, for interacting with regulators via
18+
the shell interface
19+
1220
module = REGULATOR
1321
module-str = regulator
1422
source "subsys/logging/Kconfig.template.log_config"

drivers/regulator/Kconfig.pmic

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ config REGULATOR_PMIC
1111

1212
config PMIC_REGULATOR_INIT_PRIORITY
1313
int "Regulator Init priority"
14-
depends on REGULATOR_PMIC
1514
default 75
15+
depends on REGULATOR_PMIC
1616
help
1717
I2C based regulator init priority.
1818
Must be greater than KERNEL_INIT_PRIORITY_DEVICE so I2C is initialized,
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Copyright 2022 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/shell/shell.h>
9+
#include <zephyr/drivers/uart.h>
10+
#include <zephyr/drivers/regulator.h>
11+
#if CONFIG_REGULATOR_PMIC
12+
#include <zephyr/drivers/regulator/consumer.h>
13+
#endif
14+
15+
LOG_MODULE_REGISTER(regulator_shell, CONFIG_REGULATOR_LOG_LEVEL);
16+
17+
static int cmd_reg_en(const struct shell *sh, size_t argc, char **argv)
18+
{
19+
struct onoff_client cli;
20+
const struct device *reg_dev;
21+
int ret;
22+
23+
reg_dev = device_get_binding(argv[1]);
24+
if (!device_is_ready(reg_dev)) {
25+
shell_error(sh, "regulator device %s not available", argv[1]);
26+
return -ENODEV;
27+
}
28+
29+
ret = regulator_enable(reg_dev, &cli);
30+
if (ret < 0) {
31+
shell_error(sh, "failed to enable regulator, error %d", ret);
32+
return ret;
33+
}
34+
shell_print(sh, "enabled regulator");
35+
return 0;
36+
}
37+
38+
39+
static int cmd_reg_dis(const struct shell *sh, size_t argc, char **argv)
40+
{
41+
const struct device *reg_dev;
42+
int ret;
43+
44+
reg_dev = device_get_binding(argv[1]);
45+
if (!device_is_ready(reg_dev)) {
46+
shell_error(sh, "regulator device %s not available", argv[1]);
47+
return -ENODEV;
48+
}
49+
50+
ret = regulator_disable(reg_dev);
51+
if (ret < 0) {
52+
shell_error(sh, "failed to disable regulator, error %d", ret);
53+
return ret;
54+
}
55+
shell_print(sh, "disabled regulator");
56+
return 0;
57+
}
58+
59+
60+
#if CONFIG_REGULATOR_PMIC
61+
static int cmd_pmic_set_vol(const struct shell *sh, size_t argc, char **argv)
62+
{
63+
int lvol, uvol, ret;
64+
const struct device *reg_dev;
65+
66+
reg_dev = device_get_binding(argv[1]);
67+
if (!device_is_ready(reg_dev)) {
68+
shell_error(sh, "regulator device %s not available", argv[1]);
69+
return -ENODEV;
70+
}
71+
72+
lvol = atoi(argv[2]) * 1000;
73+
uvol = atoi(argv[3]) * 1000;
74+
shell_print(sh, "Setting range to %d-%d uV", lvol, uvol);
75+
ret = regulator_set_voltage(reg_dev, lvol, uvol);
76+
if (ret < 0) {
77+
shell_error(sh, "failed to set voltage, error %d", ret);
78+
return ret;
79+
}
80+
ret = regulator_get_voltage(reg_dev);
81+
if (ret < 0) {
82+
shell_error(sh, "failed to read voltage, error %d", ret);
83+
return ret;
84+
}
85+
shell_print(sh, "set voltage to %d uV", ret);
86+
return 0;
87+
}
88+
89+
static int cmd_pmic_set_ilim(const struct shell *sh, size_t argc, char **argv)
90+
{
91+
int lcur, ucur, ret;
92+
const struct device *reg_dev;
93+
94+
reg_dev = device_get_binding(argv[1]);
95+
if (!device_is_ready(reg_dev)) {
96+
shell_error(sh, "regulator device %s not available", argv[1]);
97+
return -ENODEV;
98+
}
99+
100+
lcur = atoi(argv[2]) * 1000;
101+
ucur = atoi(argv[3]) * 1000;
102+
shell_print(sh, "Setting range to %d-%d uA", lcur, ucur);
103+
ret = regulator_set_current_limit(reg_dev, lcur, ucur);
104+
if (ret < 0) {
105+
shell_error(sh, "failed to set current, error %d", ret);
106+
return ret;
107+
}
108+
ret = regulator_get_current_limit(reg_dev);
109+
if (ret < 0) {
110+
shell_error(sh, "failed to read current, error %d", ret);
111+
return ret;
112+
}
113+
shell_print(sh, "set current limit to %d uA", ret);
114+
return 0;
115+
}
116+
117+
SHELL_STATIC_SUBCMD_SET_CREATE(pmic_set,
118+
SHELL_CMD_ARG(set_vol, NULL, "Set voltage (in mV)\n"
119+
"Usage: set_vol <device> <low limit (uV)> <high limit (uV)>",
120+
cmd_pmic_set_vol, 4, 0),
121+
SHELL_CMD_ARG(set_current, NULL, "Set current limit( in mA)\n"
122+
"Usage: set_current <device> <low limit (uA)> <high limit (uA)>",
123+
cmd_pmic_set_ilim, 4, 0),
124+
SHELL_SUBCMD_SET_END
125+
);
126+
127+
#endif /* CONFIG_REGULATOR_PMIC */
128+
129+
SHELL_STATIC_SUBCMD_SET_CREATE(regulator_set,
130+
SHELL_CMD_ARG(enable, NULL,
131+
"Enable regulator\n"
132+
"Usage: enable <device>", cmd_reg_en, 2, 0),
133+
SHELL_CMD_ARG(disable, NULL,
134+
"Disable regulator\n"
135+
"Usage: disable <device>", cmd_reg_dis, 2, 0),
136+
#if CONFIG_REGULATOR_PMIC
137+
SHELL_CMD(pmic, &pmic_set, "PMIC management", NULL),
138+
#endif
139+
SHELL_SUBCMD_SET_END
140+
);
141+
142+
SHELL_CMD_REGISTER(regulator, &regulator_set, "Regulator Management", NULL);

0 commit comments

Comments
 (0)