blob: aa06c9a9fc831c36014d252a8150af5fb62b1d43 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Przemyslaw Marczak6a923122015-04-20 20:07:44 +02002/*
3 * Copyright (C) 2014-2015 Samsung Electronics
4 * Przemyslaw Marczak <p.marczak@samsung.com>
Przemyslaw Marczak6a923122015-04-20 20:07:44 +02005 */
6#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -06007#include <command.h>
Przemyslaw Marczak6a923122015-04-20 20:07:44 +02008#include <errno.h>
9#include <dm.h>
10#include <dm/uclass-internal.h>
11#include <power/regulator.h>
12
Przemyslaw Marczak6a923122015-04-20 20:07:44 +020013#define LIMIT_DEVNAME 20
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +020014#define LIMIT_OFNAME 32
15#define LIMIT_INFO 18
Przemyslaw Marczak6a923122015-04-20 20:07:44 +020016
17static struct udevice *currdev;
18
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +020019static int failure(int ret)
Przemyslaw Marczak6a923122015-04-20 20:07:44 +020020{
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +020021 printf("Error: %d (%s)\n", ret, errno_str(ret));
Przemyslaw Marczak6a923122015-04-20 20:07:44 +020022
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +020023 return CMD_RET_FAILURE;
Przemyslaw Marczak6a923122015-04-20 20:07:44 +020024}
25
Simon Glassed38aef2020-05-10 11:40:03 -060026static int do_dev(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Przemyslaw Marczak6a923122015-04-20 20:07:44 +020027{
28 struct dm_regulator_uclass_platdata *uc_pdata;
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +020029 const char *name;
30 int ret = -ENXIO;
Przemyslaw Marczak6a923122015-04-20 20:07:44 +020031
32 switch (argc) {
33 case 2:
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +020034 name = argv[1];
35 ret = regulator_get_by_platname(name, &currdev);
36 if (ret) {
37 printf("Can't get the regulator: %s!\n", name);
38 return failure(ret);
39 }
Przemyslaw Marczak6a923122015-04-20 20:07:44 +020040 case 1:
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +020041 if (!currdev) {
42 printf("Regulator device is not set!\n\n");
43 return CMD_RET_USAGE;
44 }
45
Przemyslaw Marczak6a923122015-04-20 20:07:44 +020046 uc_pdata = dev_get_uclass_platdata(currdev);
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +020047 if (!uc_pdata) {
48 printf("%s: no regulator platform data!\n", currdev->name);
49 return failure(ret);
50 }
Przemyslaw Marczak6a923122015-04-20 20:07:44 +020051
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +020052 printf("dev: %s @ %s\n", uc_pdata->name, currdev->name);
Przemyslaw Marczak6a923122015-04-20 20:07:44 +020053 }
54
55 return CMD_RET_SUCCESS;
Przemyslaw Marczak6a923122015-04-20 20:07:44 +020056}
57
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +020058static int curr_dev_and_platdata(struct udevice **devp,
59 struct dm_regulator_uclass_platdata **uc_pdata,
60 bool allow_type_fixed)
Przemyslaw Marczak6a923122015-04-20 20:07:44 +020061{
62 *devp = NULL;
63 *uc_pdata = NULL;
64
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +020065 if (!currdev) {
66 printf("First, set the regulator device!\n");
67 return CMD_RET_FAILURE;
68 }
Przemyslaw Marczak6a923122015-04-20 20:07:44 +020069
70 *devp = currdev;
71
72 *uc_pdata = dev_get_uclass_platdata(*devp);
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +020073 if (!*uc_pdata) {
Seung-Woo Kim591188e2018-06-04 16:04:51 +090074 pr_err("Regulator: %s - missing platform data!\n", currdev->name);
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +020075 return CMD_RET_FAILURE;
76 }
Przemyslaw Marczak6a923122015-04-20 20:07:44 +020077
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;
81 }
82
83 return CMD_RET_SUCCESS;
84}
85
Simon Glassed38aef2020-05-10 11:40:03 -060086static int do_list(struct cmd_tbl *cmdtp, int flag, int argc,
87 char *const argv[])
Przemyslaw Marczak6a923122015-04-20 20:07:44 +020088{
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +020089 struct dm_regulator_uclass_platdata *uc_pdata;
90 struct udevice *dev;
Przemyslaw Marczak6a923122015-04-20 20:07:44 +020091 int ret;
92
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +020093 printf("| %-*.*s| %-*.*s| %s\n",
94 LIMIT_DEVNAME, LIMIT_DEVNAME, "Device",
95 LIMIT_OFNAME, LIMIT_OFNAME, "regulator-name",
96 "Parent");
Przemyslaw Marczak6a923122015-04-20 20:07:44 +020097
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +020098 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
99 ret = uclass_find_next_device(&dev)) {
100 if (ret)
101 continue;
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200102
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200103 uc_pdata = dev_get_uclass_platdata(dev);
104 printf("| %-*.*s| %-*.*s| %s\n",
105 LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name,
106 LIMIT_OFNAME, LIMIT_OFNAME, uc_pdata->name,
107 dev->parent->name);
108 }
109
110 return ret;
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200111}
112
113static int constraint(const char *name, int val, const char *val_name)
114{
115 printf("%-*s", LIMIT_INFO, name);
116 if (val < 0) {
117 printf(" %s (err: %d)\n", errno_str(val), val);
118 return val;
119 }
120
121 if (val_name)
122 printf(" %d (%s)\n", val, val_name);
123 else
124 printf(" %d\n", val);
125
126 return 0;
127}
128
129static const char *get_mode_name(struct dm_regulator_mode *mode,
130 int mode_count,
131 int mode_id)
132{
133 while (mode_count--) {
134 if (mode->id == mode_id)
135 return mode->name;
136 mode++;
137 }
138
139 return NULL;
140}
141
Simon Glassed38aef2020-05-10 11:40:03 -0600142static int do_info(struct cmd_tbl *cmdtp, int flag, int argc,
143 char *const argv[])
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200144{
145 struct udevice *dev;
146 struct dm_regulator_uclass_platdata *uc_pdata;
147 struct dm_regulator_mode *modes;
148 const char *parent_uc;
149 int mode_count;
150 int ret;
151 int i;
152
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200153 ret = curr_dev_and_platdata(&dev, &uc_pdata, true);
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200154 if (ret)
155 return ret;
156
157 parent_uc = dev_get_uclass_name(dev->parent);
158
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200159 printf("%s\n%-*s %s\n%-*s %s\n%-*s %s\n%-*s %s\n%-*s\n",
160 "Regulator info:",
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,
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200165 LIMIT_INFO, "* constraints:");
166
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");
175
176 mode_count = regulator_mode(dev, &modes);
177 constraint("* op modes:", mode_count, NULL);
178
179 for (i = 0; i < mode_count; i++, modes++)
180 constraint(" - mode id:", modes->id, modes->name);
181
182 return CMD_RET_SUCCESS;
183}
184
Simon Glass0875f242016-01-21 19:43:27 -0700185static void do_status_detail(struct udevice *dev,
186 struct dm_regulator_uclass_platdata *uc_pdata)
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200187{
Simon Glass0875f242016-01-21 19:43:27 -0700188 int current, value, mode;
189 const char *mode_name;
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200190 bool enabled;
191
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200192 printf("Regulator %s status:\n", uc_pdata->name);
193
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200194 enabled = regulator_get_enable(dev);
195 constraint(" * enable:", enabled, enabled ? "true" : "false");
196
197 value = regulator_get_value(dev);
198 constraint(" * value uV:", value, NULL);
199
200 current = regulator_get_current(dev);
201 constraint(" * current uA:", current, NULL);
202
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);
Simon Glass0875f242016-01-21 19:43:27 -0700206}
207
208static void do_status_line(struct udevice *dev)
209{
210 struct dm_regulator_uclass_platdata *pdata;
211 int current, value, mode;
212 const char *mode_name;
213 bool enabled;
214
215 pdata = dev_get_uclass_platdata(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");
222 if (value >= 0)
223 printf("%10d ", value);
224 else
225 printf("%10s ", "-");
226 if (current >= 0)
227 printf("%10d ", current);
228 else
229 printf("%10s ", "-");
230 if (mode >= 0)
231 printf("%-10s", mode_name);
232 else
233 printf("%-10s", "-");
234 printf("\n");
235}
236
Simon Glassed38aef2020-05-10 11:40:03 -0600237static int do_status(struct cmd_tbl *cmdtp, int flag, int argc,
238 char *const argv[])
Simon Glass0875f242016-01-21 19:43:27 -0700239{
240 struct dm_regulator_uclass_platdata *uc_pdata;
241 struct udevice *dev;
242 int ret;
243
244 if (currdev && (argc < 2 || strcmp(argv[1], "-a"))) {
245 ret = curr_dev_and_platdata(&dev, &uc_pdata, true);
246 if (ret)
247 return CMD_RET_FAILURE;
248 do_status_detail(dev, uc_pdata);
249 return 0;
250 }
251
252 /* Show all of them in a list, probing them as needed */
253 printf("%-20s %-10s %10s %10s %-10s\n", "Name", "Enabled", "uV", "mA",
254 "Mode");
255 for (ret = uclass_first_device(UCLASS_REGULATOR, &dev); dev;
256 ret = uclass_next_device(&dev))
257 do_status_line(dev);
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200258
259 return CMD_RET_SUCCESS;
260}
261
Simon Glassed38aef2020-05-10 11:40:03 -0600262static int do_value(struct cmd_tbl *cmdtp, int flag, int argc,
263 char *const argv[])
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200264{
265 struct udevice *dev;
266 struct dm_regulator_uclass_platdata *uc_pdata;
267 int value;
268 int force;
269 int ret;
270
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200271 ret = curr_dev_and_platdata(&dev, &uc_pdata, argc == 1);
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200272 if (ret)
273 return ret;
274
275 if (argc == 1) {
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200276 ret = regulator_get_value(dev);
277 if (ret < 0) {
278 printf("Regulator: %s - can't get the Voltage!\n",
279 uc_pdata->name);
280 return failure(ret);
281 }
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200282
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200283 printf("%d uV\n", ret);
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200284 return CMD_RET_SUCCESS;
285 }
286
287 if (argc == 3)
288 force = !strcmp("-f", argv[2]);
289 else
290 force = 0;
291
292 value = simple_strtoul(argv[1], NULL, 0);
293 if ((value < uc_pdata->min_uV || value > uc_pdata->max_uV) && !force) {
Simon Glasse5c88ea2015-07-02 18:16:07 -0600294 printf("Value exceeds regulator constraint limits %d..%d uV\n",
295 uc_pdata->min_uV, uc_pdata->max_uV);
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200296 return CMD_RET_FAILURE;
297 }
298
Keerthy162c02e2016-10-26 13:42:30 +0530299 if (!force)
300 ret = regulator_set_value(dev, value);
301 else
302 ret = regulator_set_value_force(dev, value);
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200303 if (ret) {
304 printf("Regulator: %s - can't set the Voltage!\n",
305 uc_pdata->name);
306 return failure(ret);
307 }
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200308
309 return CMD_RET_SUCCESS;
310}
311
Simon Glassed38aef2020-05-10 11:40:03 -0600312static int do_current(struct cmd_tbl *cmdtp, int flag, int argc,
313 char *const argv[])
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200314{
315 struct udevice *dev;
316 struct dm_regulator_uclass_platdata *uc_pdata;
317 int current;
318 int ret;
319
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200320 ret = curr_dev_and_platdata(&dev, &uc_pdata, argc == 1);
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200321 if (ret)
322 return ret;
323
324 if (argc == 1) {
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200325 ret = regulator_get_current(dev);
326 if (ret < 0) {
327 printf("Regulator: %s - can't get the Current!\n",
328 uc_pdata->name);
329 return failure(ret);
330 }
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200331
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200332 printf("%d uA\n", ret);
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200333 return CMD_RET_SUCCESS;
334 }
335
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;
340 }
341
342 ret = regulator_set_current(dev, current);
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200343 if (ret) {
344 printf("Regulator: %s - can't set the Current!\n",
345 uc_pdata->name);
346 return failure(ret);
347 }
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200348
349 return CMD_RET_SUCCESS;
350}
351
Simon Glassed38aef2020-05-10 11:40:03 -0600352static int do_mode(struct cmd_tbl *cmdtp, int flag, int argc,
353 char *const argv[])
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200354{
355 struct udevice *dev;
356 struct dm_regulator_uclass_platdata *uc_pdata;
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200357 int mode;
358 int ret;
359
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200360 ret = curr_dev_and_platdata(&dev, &uc_pdata, false);
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200361 if (ret)
362 return ret;
363
364 if (argc == 1) {
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200365 ret = regulator_get_mode(dev);
366 if (ret < 0) {
367 printf("Regulator: %s - can't get the operation mode!\n",
368 uc_pdata->name);
369 return failure(ret);
370 }
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200371
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200372 printf("mode id: %d\n", ret);
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200373 return CMD_RET_SUCCESS;
374 }
375
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200376 mode = simple_strtoul(argv[1], NULL, 0);
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200377
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200378 ret = regulator_set_mode(dev, mode);
379 if (ret) {
380 printf("Regulator: %s - can't set the operation mode!\n",
381 uc_pdata->name);
382 return failure(ret);
383 }
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200384
385 return CMD_RET_SUCCESS;
386}
387
Simon Glassed38aef2020-05-10 11:40:03 -0600388static int do_enable(struct cmd_tbl *cmdtp, int flag, int argc,
389 char *const argv[])
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200390{
391 struct udevice *dev;
392 struct dm_regulator_uclass_platdata *uc_pdata;
393 int ret;
394
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200395 ret = curr_dev_and_platdata(&dev, &uc_pdata, true);
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200396 if (ret)
397 return ret;
398
399 ret = regulator_set_enable(dev, true);
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200400 if (ret) {
401 printf("Regulator: %s - can't enable!\n", uc_pdata->name);
402 return failure(ret);
403 }
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200404
405 return CMD_RET_SUCCESS;
406}
407
Simon Glassed38aef2020-05-10 11:40:03 -0600408static int do_disable(struct cmd_tbl *cmdtp, int flag, int argc,
409 char *const argv[])
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200410{
411 struct udevice *dev;
412 struct dm_regulator_uclass_platdata *uc_pdata;
413 int ret;
414
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200415 ret = curr_dev_and_platdata(&dev, &uc_pdata, true);
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200416 if (ret)
417 return ret;
418
419 ret = regulator_set_enable(dev, false);
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200420 if (ret) {
421 printf("Regulator: %s - can't disable!\n", uc_pdata->name);
422 return failure(ret);
423 }
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200424
425 return CMD_RET_SUCCESS;
426}
427
Simon Glassed38aef2020-05-10 11:40:03 -0600428static struct cmd_tbl subcmd[] = {
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200429 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, "", ""),
438};
439
Simon Glassed38aef2020-05-10 11:40:03 -0600440static int do_regulator(struct cmd_tbl *cmdtp, int flag, int argc,
441 char *const argv[])
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200442{
Simon Glassed38aef2020-05-10 11:40:03 -0600443 struct cmd_tbl *cmd;
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200444
445 argc--;
446 argv++;
447
448 cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd));
449 if (cmd == NULL || argc > cmd->maxargs)
450 return CMD_RET_USAGE;
451
452 return cmd->cmd(cmdtp, flag, argc, argv);
453}
454
455U_BOOT_CMD(regulator, CONFIG_SYS_MAXARGS, 1, do_regulator,
456 "uclass operations",
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200457 "list - list UCLASS regulator devices\n"
458 "regulator dev [regulator-name] - show/[set] operating regulator device\n"
459 "regulator info - print constraints info\n"
Simon Glass0875f242016-01-21 19:43:27 -0700460 "regulator status [-a] - print operating status [for all]\n"
Przemyslaw Marczak6df3ba72015-05-13 13:38:29 +0200461 "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"
Przemyslaw Marczak6a923122015-04-20 20:07:44 +0200466);