]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - cmd/regulator.c
1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014-2015 Samsung Electronics
4 * Przemyslaw Marczak <p.marczak@samsung.com>
10 #include <dm/uclass-internal.h>
11 #include <linux/printk.h>
12 #include <power/regulator.h>
14 #define LIMIT_DEVNAME 20
15 #define LIMIT_OFNAME 32
18 static struct udevice
*currdev
;
20 static int failure(int ret
)
22 printf("Error: %d (%s)\n", ret
, errno_str(ret
));
24 return CMD_RET_FAILURE
;
27 static int do_dev(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[])
29 struct dm_regulator_uclass_plat
*uc_pdata
;
36 ret
= regulator_get_by_platname(name
, &currdev
);
38 printf("Can't get the regulator: %s!\n", name
);
44 printf("Regulator device is not set!\n\n");
48 uc_pdata
= dev_get_uclass_plat(currdev
);
50 printf("%s: no regulator platform data!\n", currdev
->name
);
54 printf("dev: %s @ %s\n", uc_pdata
->name
, currdev
->name
);
57 return CMD_RET_SUCCESS
;
60 static int curr_dev_and_plat(struct udevice
**devp
,
61 struct dm_regulator_uclass_plat
**uc_pdata
,
62 bool allow_type_fixed
)
68 printf("First, set the regulator device!\n");
69 return CMD_RET_FAILURE
;
74 *uc_pdata
= dev_get_uclass_plat(*devp
);
76 pr_err("Regulator: %s - missing platform data!\n", currdev
->name
);
77 return CMD_RET_FAILURE
;
80 if (!allow_type_fixed
&& (*uc_pdata
)->type
== REGULATOR_TYPE_FIXED
) {
81 printf("Operation not allowed for fixed regulator!\n");
82 return CMD_RET_FAILURE
;
85 return CMD_RET_SUCCESS
;
88 static int do_list(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
91 struct dm_regulator_uclass_plat
*uc_pdata
;
95 printf("| %-*.*s| %-*.*s| %s\n",
96 LIMIT_DEVNAME
, LIMIT_DEVNAME
, "Device",
97 LIMIT_OFNAME
, LIMIT_OFNAME
, "regulator-name",
100 for (ret
= uclass_find_first_device(UCLASS_REGULATOR
, &dev
); dev
;
101 ret
= uclass_find_next_device(&dev
)) {
105 uc_pdata
= dev_get_uclass_plat(dev
);
106 printf("| %-*.*s| %-*.*s| %s\n",
107 LIMIT_DEVNAME
, LIMIT_DEVNAME
, dev
->name
,
108 LIMIT_OFNAME
, LIMIT_OFNAME
, uc_pdata
->name
,
115 static int constraint(const char *name
, int val
, const char *val_name
)
117 printf("%-*s", LIMIT_INFO
, name
);
119 printf(" %s (err: %d)\n", errno_str(val
), val
);
124 printf(" %d (%s)\n", val
, val_name
);
126 printf(" %d\n", val
);
131 static const char *get_mode_name(struct dm_regulator_mode
*mode
,
135 while (mode_count
--) {
136 if (mode
->id
== mode_id
)
144 static int do_info(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
148 struct dm_regulator_uclass_plat
*uc_pdata
;
149 struct dm_regulator_mode
*modes
;
150 const char *parent_uc
;
155 ret
= curr_dev_and_plat(&dev
, &uc_pdata
, true);
159 parent_uc
= dev_get_uclass_name(dev
->parent
);
161 printf("%s\n%-*s %s\n%-*s %s\n%-*s %s\n%-*s %s\n%-*s\n",
163 LIMIT_INFO
, "* regulator-name:", uc_pdata
->name
,
164 LIMIT_INFO
, "* device name:", dev
->name
,
165 LIMIT_INFO
, "* parent name:", dev
->parent
->name
,
166 LIMIT_INFO
, "* parent uclass:", parent_uc
,
167 LIMIT_INFO
, "* constraints:");
169 constraint(" - min uV:", uc_pdata
->min_uV
, NULL
);
170 constraint(" - max uV:", uc_pdata
->max_uV
, NULL
);
171 constraint(" - min uA:", uc_pdata
->min_uA
, NULL
);
172 constraint(" - max uA:", uc_pdata
->max_uA
, NULL
);
173 constraint(" - always on:", uc_pdata
->always_on
,
174 uc_pdata
->always_on
? "true" : "false");
175 constraint(" - boot on:", uc_pdata
->boot_on
,
176 uc_pdata
->boot_on
? "true" : "false");
178 mode_count
= regulator_mode(dev
, &modes
);
179 constraint("* op modes:", mode_count
, NULL
);
181 for (i
= 0; i
< mode_count
; i
++, modes
++)
182 constraint(" - mode id:", modes
->id
, modes
->name
);
184 return CMD_RET_SUCCESS
;
187 static void do_status_detail(struct udevice
*dev
,
188 struct dm_regulator_uclass_plat
*uc_pdata
)
190 int current
, value
, mode
;
191 const char *mode_name
;
194 printf("Regulator %s status:\n", uc_pdata
->name
);
196 enabled
= regulator_get_enable(dev
);
197 constraint(" * enable:", enabled
, enabled
? "true" : "false");
199 value
= regulator_get_value(dev
);
200 constraint(" * value uV:", value
, NULL
);
202 current
= regulator_get_current(dev
);
203 constraint(" * current uA:", current
, NULL
);
205 mode
= regulator_get_mode(dev
);
206 mode_name
= get_mode_name(uc_pdata
->mode
, uc_pdata
->mode_count
, mode
);
207 constraint(" * mode id:", mode
, mode_name
);
210 static void do_status_line(struct udevice
*dev
, int status
)
212 struct dm_regulator_uclass_plat
*pdata
;
213 int current
, value
, mode
;
214 const char *mode_name
;
217 pdata
= dev_get_uclass_plat(dev
);
218 enabled
= regulator_get_enable(dev
);
219 value
= regulator_get_value(dev
);
220 current
= regulator_get_current(dev
);
221 mode
= regulator_get_mode(dev
);
222 mode_name
= get_mode_name(pdata
->mode
, pdata
->mode_count
, mode
);
223 printf("%-20s %-10s ", pdata
->name
, enabled
? "enabled" : "disabled");
225 printf("%10d ", value
);
227 printf("%10s ", "-");
229 printf("%10d ", current
);
231 printf("%10s ", "-");
233 printf("%-10s", mode_name
);
235 printf("%-10s", "-");
236 printf(" %i", status
);
240 static int do_status(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
243 struct dm_regulator_uclass_plat
*uc_pdata
;
247 if (currdev
&& (argc
< 2 || strcmp(argv
[1], "-a"))) {
248 ret
= curr_dev_and_plat(&dev
, &uc_pdata
, true);
250 return CMD_RET_FAILURE
;
251 do_status_detail(dev
, uc_pdata
);
255 /* Show all of them in a list, probing them as needed */
256 printf("%-20s %-10s %10s %10s %-10s %s\n", "Name", "Enabled", "uV", "mA",
258 for (ret
= uclass_first_device_check(UCLASS_REGULATOR
, &dev
); dev
;
259 ret
= uclass_next_device_check(&dev
))
260 do_status_line(dev
, ret
);
262 return CMD_RET_SUCCESS
;
265 static int do_value(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
269 struct dm_regulator_uclass_plat
*uc_pdata
;
274 ret
= curr_dev_and_plat(&dev
, &uc_pdata
, argc
== 1);
279 ret
= regulator_get_value(dev
);
281 printf("Regulator: %s - can't get the Voltage!\n",
286 printf("%d uV\n", ret
);
287 return CMD_RET_SUCCESS
;
291 force
= !strcmp("-f", argv
[2]);
295 value
= simple_strtoul(argv
[1], NULL
, 0);
296 if ((value
< uc_pdata
->min_uV
|| value
> uc_pdata
->max_uV
) && !force
) {
297 printf("Value exceeds regulator constraint limits %d..%d uV\n",
298 uc_pdata
->min_uV
, uc_pdata
->max_uV
);
299 return CMD_RET_FAILURE
;
303 ret
= regulator_set_value(dev
, value
);
305 ret
= regulator_set_value_force(dev
, value
);
307 printf("Regulator: %s - can't set the Voltage!\n",
312 return CMD_RET_SUCCESS
;
315 static int do_current(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
319 struct dm_regulator_uclass_plat
*uc_pdata
;
323 ret
= curr_dev_and_plat(&dev
, &uc_pdata
, argc
== 1);
328 ret
= regulator_get_current(dev
);
330 printf("Regulator: %s - can't get the Current!\n",
335 printf("%d uA\n", ret
);
336 return CMD_RET_SUCCESS
;
339 current
= simple_strtoul(argv
[1], NULL
, 0);
340 if (current
< uc_pdata
->min_uA
|| current
> uc_pdata
->max_uA
) {
341 printf("Current exceeds regulator constraint limits\n");
342 return CMD_RET_FAILURE
;
345 ret
= regulator_set_current(dev
, current
);
347 printf("Regulator: %s - can't set the Current!\n",
352 return CMD_RET_SUCCESS
;
355 static int do_mode(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
359 struct dm_regulator_uclass_plat
*uc_pdata
;
363 ret
= curr_dev_and_plat(&dev
, &uc_pdata
, false);
368 ret
= regulator_get_mode(dev
);
370 printf("Regulator: %s - can't get the operation mode!\n",
375 printf("mode id: %d\n", ret
);
376 return CMD_RET_SUCCESS
;
379 mode
= simple_strtoul(argv
[1], NULL
, 0);
381 ret
= regulator_set_mode(dev
, mode
);
383 printf("Regulator: %s - can't set the operation mode!\n",
388 return CMD_RET_SUCCESS
;
391 static int do_enable(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
395 struct dm_regulator_uclass_plat
*uc_pdata
;
398 ret
= curr_dev_and_plat(&dev
, &uc_pdata
, true);
402 ret
= regulator_set_enable(dev
, true);
404 printf("Regulator: %s - can't enable!\n", uc_pdata
->name
);
408 return CMD_RET_SUCCESS
;
411 static int do_disable(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
415 struct dm_regulator_uclass_plat
*uc_pdata
;
418 ret
= curr_dev_and_plat(&dev
, &uc_pdata
, true);
422 ret
= regulator_set_enable(dev
, false);
424 printf("Regulator: %s - can't disable!\n", uc_pdata
->name
);
428 return CMD_RET_SUCCESS
;
431 static struct cmd_tbl subcmd
[] = {
432 U_BOOT_CMD_MKENT(dev
, 2, 1, do_dev
, "", ""),
433 U_BOOT_CMD_MKENT(list
, 1, 1, do_list
, "", ""),
434 U_BOOT_CMD_MKENT(info
, 2, 1, do_info
, "", ""),
435 U_BOOT_CMD_MKENT(status
, 2, 1, do_status
, "", ""),
436 U_BOOT_CMD_MKENT(value
, 3, 1, do_value
, "", ""),
437 U_BOOT_CMD_MKENT(current
, 3, 1, do_current
, "", ""),
438 U_BOOT_CMD_MKENT(mode
, 2, 1, do_mode
, "", ""),
439 U_BOOT_CMD_MKENT(enable
, 1, 1, do_enable
, "", ""),
440 U_BOOT_CMD_MKENT(disable
, 1, 1, do_disable
, "", ""),
443 static int do_regulator(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
451 cmd
= find_cmd_tbl(argv
[0], subcmd
, ARRAY_SIZE(subcmd
));
452 if (cmd
== NULL
|| argc
> cmd
->maxargs
)
453 return CMD_RET_USAGE
;
455 return cmd
->cmd(cmdtp
, flag
, argc
, argv
);
458 U_BOOT_CMD(regulator
, CONFIG_SYS_MAXARGS
, 1, do_regulator
,
460 "list - list UCLASS regulator devices\n"
461 "regulator dev [regulator-name] - show/[set] operating regulator device\n"
462 "regulator info - print constraints info\n"
463 "regulator status [-a] - print operating status [for all]\n"
464 "regulator value [val] [-f] - print/[set] voltage value [uV] (force)\n"
465 "regulator current [val] - print/[set] current value [uA]\n"
466 "regulator mode [id] - print/[set] operating mode id\n"
467 "regulator enable - enable the regulator output\n"
468 "regulator disable - disable the regulator output\n"