2 * Copyright (C) 2014-2015 Samsung Electronics
3 * Przemyslaw Marczak <p.marczak@samsung.com>
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <dm/uclass-internal.h>
11 #include <power/regulator.h>
14 #define LIMIT_DEVNAME 20
15 #define LIMIT_OFNAME 20
18 static struct udevice *currdev;
20 static int failed(const char *getset, const char *thing,
21 const char *for_dev, int ret)
23 printf("Can't %s %s %s.\nError: %d (%s)\n", getset, thing, for_dev,
25 return CMD_RET_FAILURE;
28 static int regulator_get(bool list_only, int get_seq, struct udevice **devp)
30 struct dm_regulator_uclass_platdata *uc_pdata;
37 for (ret = uclass_first_device(UCLASS_REGULATOR, &dev); dev;
38 ret = uclass_next_device(&dev)) {
40 uc_pdata = dev_get_uclass_platdata(dev);
41 printf("|%*d | %*.*s @ %-*.*s| %s @ %s\n",
43 LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name,
44 LIMIT_OFNAME, LIMIT_OFNAME, uc_pdata->name,
46 dev_get_uclass_name(dev->parent));
50 if (dev->seq == get_seq) {
66 static int do_dev(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
68 struct dm_regulator_uclass_platdata *uc_pdata;
69 int seq, ret = -ENXIO;
73 seq = simple_strtoul(argv[1], NULL, 0);
74 ret = uclass_get_device_by_seq(UCLASS_REGULATOR, seq, &currdev);
75 if (ret && (ret = regulator_get(false, seq, &currdev)))
78 uc_pdata = dev_get_uclass_platdata(currdev);
82 printf("dev: %d @ %s\n", currdev->seq, uc_pdata->name);
85 return CMD_RET_SUCCESS;
87 return failed("get", "the", "device", ret);
90 static int get_curr_dev_and_pl(struct udevice **devp,
91 struct dm_regulator_uclass_platdata **uc_pdata,
92 bool allow_type_fixed)
98 return failed("get", "current", "device", -ENODEV);
102 *uc_pdata = dev_get_uclass_platdata(*devp);
104 return failed("get", "regulator", "platdata", -ENXIO);
106 if (!allow_type_fixed && (*uc_pdata)->type == REGULATOR_TYPE_FIXED) {
107 printf("Operation not allowed for fixed regulator!\n");
108 return CMD_RET_FAILURE;
111 return CMD_RET_SUCCESS;
114 static int do_list(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
118 printf("|%*s | %*.*s @ %-*.*s| %s @ %s\n",
120 LIMIT_DEVNAME, LIMIT_DEVNAME, "Name",
121 LIMIT_OFNAME, LIMIT_OFNAME, "fdtname",
124 ret = regulator_get(true, 0, NULL);
126 return CMD_RET_FAILURE;
128 return CMD_RET_SUCCESS;
131 static int constraint(const char *name, int val, const char *val_name)
133 printf("%-*s", LIMIT_INFO, name);
135 printf(" %s (err: %d)\n", errno_str(val), val);
140 printf(" %d (%s)\n", val, val_name);
142 printf(" %d\n", val);
147 static const char *get_mode_name(struct dm_regulator_mode *mode,
151 while (mode_count--) {
152 if (mode->id == mode_id)
160 static int do_info(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
163 struct dm_regulator_uclass_platdata *uc_pdata;
164 struct dm_regulator_mode *modes;
165 const char *parent_uc;
170 ret = get_curr_dev_and_pl(&dev, &uc_pdata, true);
174 parent_uc = dev_get_uclass_name(dev->parent);
176 printf("Uclass regulator dev %d info:\n", dev->seq);
177 printf("%-*s %s @ %s\n%-*s %s\n%-*s %s\n%-*s\n",
178 LIMIT_INFO, "* parent:", dev->parent->name, parent_uc,
179 LIMIT_INFO, "* dev name:", dev->name,
180 LIMIT_INFO, "* fdt name:", uc_pdata->name,
181 LIMIT_INFO, "* constraints:");
183 constraint(" - min uV:", uc_pdata->min_uV, NULL);
184 constraint(" - max uV:", uc_pdata->max_uV, NULL);
185 constraint(" - min uA:", uc_pdata->min_uA, NULL);
186 constraint(" - max uA:", uc_pdata->max_uA, NULL);
187 constraint(" - always on:", uc_pdata->always_on,
188 uc_pdata->always_on ? "true" : "false");
189 constraint(" - boot on:", uc_pdata->boot_on,
190 uc_pdata->boot_on ? "true" : "false");
192 mode_count = regulator_mode(dev, &modes);
193 constraint("* op modes:", mode_count, NULL);
195 for (i = 0; i < mode_count; i++, modes++)
196 constraint(" - mode id:", modes->id, modes->name);
198 return CMD_RET_SUCCESS;
201 static int do_status(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
203 struct dm_regulator_uclass_platdata *uc_pdata;
204 int current, value, mode, ret;
205 const char *mode_name = NULL;
209 ret = get_curr_dev_and_pl(&dev, &uc_pdata, true);
213 enabled = regulator_get_enable(dev);
214 constraint(" * enable:", enabled, enabled ? "true" : "false");
216 value = regulator_get_value(dev);
217 constraint(" * value uV:", value, NULL);
219 current = regulator_get_current(dev);
220 constraint(" * current uA:", current, NULL);
222 mode = regulator_get_mode(dev);
223 mode_name = get_mode_name(uc_pdata->mode, uc_pdata->mode_count, mode);
224 constraint(" * mode id:", mode, mode_name);
226 return CMD_RET_SUCCESS;
229 static int do_value(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
232 struct dm_regulator_uclass_platdata *uc_pdata;
237 ret = get_curr_dev_and_pl(&dev, &uc_pdata, argc == 1);
242 value = regulator_get_value(dev);
244 return failed("get", uc_pdata->name, "voltage", value);
246 printf("%d uV\n", value);
247 return CMD_RET_SUCCESS;
251 force = !strcmp("-f", argv[2]);
255 value = simple_strtoul(argv[1], NULL, 0);
256 if ((value < uc_pdata->min_uV || value > uc_pdata->max_uV) && !force) {
257 printf("Value exceeds regulator constraint limits\n");
258 return CMD_RET_FAILURE;
261 ret = regulator_set_value(dev, value);
263 return failed("set", uc_pdata->name, "voltage value", ret);
265 return CMD_RET_SUCCESS;
268 static int do_current(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
271 struct dm_regulator_uclass_platdata *uc_pdata;
275 ret = get_curr_dev_and_pl(&dev, &uc_pdata, argc == 1);
280 current = regulator_get_current(dev);
282 return failed("get", uc_pdata->name, "current", current);
284 printf("%d uA\n", current);
285 return CMD_RET_SUCCESS;
288 current = simple_strtoul(argv[1], NULL, 0);
289 if (current < uc_pdata->min_uA || current > uc_pdata->max_uA) {
290 printf("Current exceeds regulator constraint limits\n");
291 return CMD_RET_FAILURE;
294 ret = regulator_set_current(dev, current);
296 return failed("set", uc_pdata->name, "current value", ret);
298 return CMD_RET_SUCCESS;
301 static int do_mode(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
304 struct dm_regulator_uclass_platdata *uc_pdata;
309 ret = get_curr_dev_and_pl(&dev, &uc_pdata, false);
314 mode = regulator_get_mode(dev);
316 return failed("get", uc_pdata->name, "mode", mode);
318 printf("mode id: %d\n", mode);
319 return CMD_RET_SUCCESS;
322 new_mode = simple_strtoul(argv[1], NULL, 0);
324 ret = regulator_set_mode(dev, new_mode);
326 return failed("set", uc_pdata->name, "mode", ret);
328 return CMD_RET_SUCCESS;
331 static int do_enable(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
334 struct dm_regulator_uclass_platdata *uc_pdata;
337 ret = get_curr_dev_and_pl(&dev, &uc_pdata, true);
341 ret = regulator_set_enable(dev, true);
343 return failed("enable", "regulator", uc_pdata->name, ret);
345 return CMD_RET_SUCCESS;
348 static int do_disable(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
351 struct dm_regulator_uclass_platdata *uc_pdata;
354 ret = get_curr_dev_and_pl(&dev, &uc_pdata, true);
358 ret = regulator_set_enable(dev, false);
360 return failed("disable", "regulator", uc_pdata->name, ret);
362 return CMD_RET_SUCCESS;
365 static cmd_tbl_t subcmd[] = {
366 U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""),
367 U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""),
368 U_BOOT_CMD_MKENT(info, 2, 1, do_info, "", ""),
369 U_BOOT_CMD_MKENT(status, 2, 1, do_status, "", ""),
370 U_BOOT_CMD_MKENT(value, 3, 1, do_value, "", ""),
371 U_BOOT_CMD_MKENT(current, 3, 1, do_current, "", ""),
372 U_BOOT_CMD_MKENT(mode, 2, 1, do_mode, "", ""),
373 U_BOOT_CMD_MKENT(enable, 1, 1, do_enable, "", ""),
374 U_BOOT_CMD_MKENT(disable, 1, 1, do_disable, "", ""),
377 static int do_regulator(cmd_tbl_t *cmdtp, int flag, int argc,
385 cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd));
386 if (cmd == NULL || argc > cmd->maxargs)
387 return CMD_RET_USAGE;
389 return cmd->cmd(cmdtp, flag, argc, argv);
392 U_BOOT_CMD(regulator, CONFIG_SYS_MAXARGS, 1, do_regulator,
394 "list - list UCLASS regulator devices\n"
395 "regulator dev [id] - show or [set] operating regulator device\n"
396 "regulator [info] - print constraints info\n"
397 "regulator [status] - print operating status\n"
398 "regulator [value] [-f] - print/[set] voltage value [uV] (force)\n"
399 "regulator [current] - print/[set] current value [uA]\n"
400 "regulator [mode_id] - print/[set] operating mode id\n"
401 "regulator [enable] - enable the regulator output\n"
402 "regulator [disable] - disable the regulator output\n"