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 <power/regulator.h>
13 #define LIMIT_DEVNAME 20
14 #define LIMIT_OFNAME 32
17 static struct udevice *currdev;
19 static int failure(int ret)
21 printf("Error: %d (%s)\n", ret, errno_str(ret));
23 return CMD_RET_FAILURE;
26 static int do_dev(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
28 struct dm_regulator_uclass_plat *uc_pdata;
35 ret = regulator_get_by_platname(name, &currdev);
37 printf("Can't get the regulator: %s!\n", name);
42 printf("Regulator device is not set!\n\n");
46 uc_pdata = dev_get_uclass_plat(currdev);
48 printf("%s: no regulator platform data!\n", currdev->name);
52 printf("dev: %s @ %s\n", uc_pdata->name, currdev->name);
55 return CMD_RET_SUCCESS;
58 static int curr_dev_and_plat(struct udevice **devp,
59 struct dm_regulator_uclass_plat **uc_pdata,
60 bool allow_type_fixed)
66 printf("First, set the regulator device!\n");
67 return CMD_RET_FAILURE;
72 *uc_pdata = dev_get_uclass_plat(*devp);
74 pr_err("Regulator: %s - missing platform data!\n", currdev->name);
75 return CMD_RET_FAILURE;
78 if (!allow_type_fixed && (*uc_pdata)->type == REGULATOR_TYPE_FIXED) {
79 printf("Operation not allowed for fixed regulator!\n");
80 return CMD_RET_FAILURE;
83 return CMD_RET_SUCCESS;
86 static int do_list(struct cmd_tbl *cmdtp, int flag, int argc,
89 struct dm_regulator_uclass_plat *uc_pdata;
93 printf("| %-*.*s| %-*.*s| %s\n",
94 LIMIT_DEVNAME, LIMIT_DEVNAME, "Device",
95 LIMIT_OFNAME, LIMIT_OFNAME, "regulator-name",
98 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
99 ret = uclass_find_next_device(&dev)) {
103 uc_pdata = dev_get_uclass_plat(dev);
104 printf("| %-*.*s| %-*.*s| %s\n",
105 LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name,
106 LIMIT_OFNAME, LIMIT_OFNAME, uc_pdata->name,
113 static int constraint(const char *name, int val, const char *val_name)
115 printf("%-*s", LIMIT_INFO, name);
117 printf(" %s (err: %d)\n", errno_str(val), val);
122 printf(" %d (%s)\n", val, val_name);
124 printf(" %d\n", val);
129 static const char *get_mode_name(struct dm_regulator_mode *mode,
133 while (mode_count--) {
134 if (mode->id == mode_id)
142 static int do_info(struct cmd_tbl *cmdtp, int flag, int argc,
146 struct dm_regulator_uclass_plat *uc_pdata;
147 struct dm_regulator_mode *modes;
148 const char *parent_uc;
153 ret = curr_dev_and_plat(&dev, &uc_pdata, true);
157 parent_uc = dev_get_uclass_name(dev->parent);
159 printf("%s\n%-*s %s\n%-*s %s\n%-*s %s\n%-*s %s\n%-*s\n",
161 LIMIT_INFO, "* regulator-name:", uc_pdata->name,
162 LIMIT_INFO, "* device name:", dev->name,
163 LIMIT_INFO, "* parent name:", dev->parent->name,
164 LIMIT_INFO, "* parent uclass:", parent_uc,
165 LIMIT_INFO, "* constraints:");
167 constraint(" - min uV:", uc_pdata->min_uV, NULL);
168 constraint(" - max uV:", uc_pdata->max_uV, NULL);
169 constraint(" - min uA:", uc_pdata->min_uA, NULL);
170 constraint(" - max uA:", uc_pdata->max_uA, NULL);
171 constraint(" - always on:", uc_pdata->always_on,
172 uc_pdata->always_on ? "true" : "false");
173 constraint(" - boot on:", uc_pdata->boot_on,
174 uc_pdata->boot_on ? "true" : "false");
176 mode_count = regulator_mode(dev, &modes);
177 constraint("* op modes:", mode_count, NULL);
179 for (i = 0; i < mode_count; i++, modes++)
180 constraint(" - mode id:", modes->id, modes->name);
182 return CMD_RET_SUCCESS;
185 static void do_status_detail(struct udevice *dev,
186 struct dm_regulator_uclass_plat *uc_pdata)
188 int current, value, mode;
189 const char *mode_name;
192 printf("Regulator %s status:\n", uc_pdata->name);
194 enabled = regulator_get_enable(dev);
195 constraint(" * enable:", enabled, enabled ? "true" : "false");
197 value = regulator_get_value(dev);
198 constraint(" * value uV:", value, NULL);
200 current = regulator_get_current(dev);
201 constraint(" * current uA:", current, NULL);
203 mode = regulator_get_mode(dev);
204 mode_name = get_mode_name(uc_pdata->mode, uc_pdata->mode_count, mode);
205 constraint(" * mode id:", mode, mode_name);
208 static void do_status_line(struct udevice *dev)
210 struct dm_regulator_uclass_plat *pdata;
211 int current, value, mode;
212 const char *mode_name;
215 pdata = dev_get_uclass_plat(dev);
216 enabled = regulator_get_enable(dev);
217 value = regulator_get_value(dev);
218 current = regulator_get_current(dev);
219 mode = regulator_get_mode(dev);
220 mode_name = get_mode_name(pdata->mode, pdata->mode_count, mode);
221 printf("%-20s %-10s ", pdata->name, enabled ? "enabled" : "disabled");
223 printf("%10d ", value);
225 printf("%10s ", "-");
227 printf("%10d ", current);
229 printf("%10s ", "-");
231 printf("%-10s", mode_name);
233 printf("%-10s", "-");
237 static int do_status(struct cmd_tbl *cmdtp, int flag, int argc,
240 struct dm_regulator_uclass_plat *uc_pdata;
244 if (currdev && (argc < 2 || strcmp(argv[1], "-a"))) {
245 ret = curr_dev_and_plat(&dev, &uc_pdata, true);
247 return CMD_RET_FAILURE;
248 do_status_detail(dev, uc_pdata);
252 /* Show all of them in a list, probing them as needed */
253 printf("%-20s %-10s %10s %10s %-10s\n", "Name", "Enabled", "uV", "mA",
255 for (ret = uclass_first_device(UCLASS_REGULATOR, &dev); dev;
256 ret = uclass_next_device(&dev))
259 return CMD_RET_SUCCESS;
262 static int do_value(struct cmd_tbl *cmdtp, int flag, int argc,
266 struct dm_regulator_uclass_plat *uc_pdata;
271 ret = curr_dev_and_plat(&dev, &uc_pdata, argc == 1);
276 ret = regulator_get_value(dev);
278 printf("Regulator: %s - can't get the Voltage!\n",
283 printf("%d uV\n", ret);
284 return CMD_RET_SUCCESS;
288 force = !strcmp("-f", argv[2]);
292 value = simple_strtoul(argv[1], NULL, 0);
293 if ((value < uc_pdata->min_uV || value > uc_pdata->max_uV) && !force) {
294 printf("Value exceeds regulator constraint limits %d..%d uV\n",
295 uc_pdata->min_uV, uc_pdata->max_uV);
296 return CMD_RET_FAILURE;
300 ret = regulator_set_value(dev, value);
302 ret = regulator_set_value_force(dev, value);
304 printf("Regulator: %s - can't set the Voltage!\n",
309 return CMD_RET_SUCCESS;
312 static int do_current(struct cmd_tbl *cmdtp, int flag, int argc,
316 struct dm_regulator_uclass_plat *uc_pdata;
320 ret = curr_dev_and_plat(&dev, &uc_pdata, argc == 1);
325 ret = regulator_get_current(dev);
327 printf("Regulator: %s - can't get the Current!\n",
332 printf("%d uA\n", ret);
333 return CMD_RET_SUCCESS;
336 current = simple_strtoul(argv[1], NULL, 0);
337 if (current < uc_pdata->min_uA || current > uc_pdata->max_uA) {
338 printf("Current exceeds regulator constraint limits\n");
339 return CMD_RET_FAILURE;
342 ret = regulator_set_current(dev, current);
344 printf("Regulator: %s - can't set the Current!\n",
349 return CMD_RET_SUCCESS;
352 static int do_mode(struct cmd_tbl *cmdtp, int flag, int argc,
356 struct dm_regulator_uclass_plat *uc_pdata;
360 ret = curr_dev_and_plat(&dev, &uc_pdata, false);
365 ret = regulator_get_mode(dev);
367 printf("Regulator: %s - can't get the operation mode!\n",
372 printf("mode id: %d\n", ret);
373 return CMD_RET_SUCCESS;
376 mode = simple_strtoul(argv[1], NULL, 0);
378 ret = regulator_set_mode(dev, mode);
380 printf("Regulator: %s - can't set the operation mode!\n",
385 return CMD_RET_SUCCESS;
388 static int do_enable(struct cmd_tbl *cmdtp, int flag, int argc,
392 struct dm_regulator_uclass_plat *uc_pdata;
395 ret = curr_dev_and_plat(&dev, &uc_pdata, true);
399 ret = regulator_set_enable(dev, true);
401 printf("Regulator: %s - can't enable!\n", uc_pdata->name);
405 return CMD_RET_SUCCESS;
408 static int do_disable(struct cmd_tbl *cmdtp, int flag, int argc,
412 struct dm_regulator_uclass_plat *uc_pdata;
415 ret = curr_dev_and_plat(&dev, &uc_pdata, true);
419 ret = regulator_set_enable(dev, false);
421 printf("Regulator: %s - can't disable!\n", uc_pdata->name);
425 return CMD_RET_SUCCESS;
428 static struct cmd_tbl subcmd[] = {
429 U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""),
430 U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""),
431 U_BOOT_CMD_MKENT(info, 2, 1, do_info, "", ""),
432 U_BOOT_CMD_MKENT(status, 2, 1, do_status, "", ""),
433 U_BOOT_CMD_MKENT(value, 3, 1, do_value, "", ""),
434 U_BOOT_CMD_MKENT(current, 3, 1, do_current, "", ""),
435 U_BOOT_CMD_MKENT(mode, 2, 1, do_mode, "", ""),
436 U_BOOT_CMD_MKENT(enable, 1, 1, do_enable, "", ""),
437 U_BOOT_CMD_MKENT(disable, 1, 1, do_disable, "", ""),
440 static int do_regulator(struct cmd_tbl *cmdtp, int flag, int argc,
448 cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd));
449 if (cmd == NULL || argc > cmd->maxargs)
450 return CMD_RET_USAGE;
452 return cmd->cmd(cmdtp, flag, argc, argv);
455 U_BOOT_CMD(regulator, CONFIG_SYS_MAXARGS, 1, do_regulator,
457 "list - list UCLASS regulator devices\n"
458 "regulator dev [regulator-name] - show/[set] operating regulator device\n"
459 "regulator info - print constraints info\n"
460 "regulator status [-a] - print operating status [for all]\n"
461 "regulator value [val] [-f] - print/[set] voltage value [uV] (force)\n"
462 "regulator current [val] - print/[set] current value [uA]\n"
463 "regulator mode [id] - print/[set] operating mode id\n"
464 "regulator enable - enable the regulator output\n"
465 "regulator disable - disable the regulator output\n"