]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_regulator.c
common: cmd_part: start and size sub-commands introduction
[people/ms/u-boot.git] / common / cmd_regulator.c
CommitLineData
6262b72b
PM
1/*
2 * Copyright (C) 2014-2015 Samsung Electronics
3 * Przemyslaw Marczak <p.marczak@samsung.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7#include <common.h>
8#include <errno.h>
9#include <dm.h>
10#include <dm/uclass-internal.h>
11#include <power/regulator.h>
12
6262b72b 13#define LIMIT_DEVNAME 20
e09b2a02
PM
14#define LIMIT_OFNAME 32
15#define LIMIT_INFO 18
6262b72b
PM
16
17static struct udevice *currdev;
18
e09b2a02 19static int failure(int ret)
6262b72b 20{
e09b2a02 21 printf("Error: %d (%s)\n", ret, errno_str(ret));
6262b72b 22
e09b2a02 23 return CMD_RET_FAILURE;
6262b72b
PM
24}
25
26static int do_dev(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
27{
28 struct dm_regulator_uclass_platdata *uc_pdata;
e09b2a02
PM
29 const char *name;
30 int ret = -ENXIO;
6262b72b
PM
31
32 switch (argc) {
33 case 2:
e09b2a02
PM
34 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 }
6262b72b 40 case 1:
e09b2a02
PM
41 if (!currdev) {
42 printf("Regulator device is not set!\n\n");
43 return CMD_RET_USAGE;
44 }
45
6262b72b 46 uc_pdata = dev_get_uclass_platdata(currdev);
e09b2a02
PM
47 if (!uc_pdata) {
48 printf("%s: no regulator platform data!\n", currdev->name);
49 return failure(ret);
50 }
6262b72b 51
e09b2a02 52 printf("dev: %s @ %s\n", uc_pdata->name, currdev->name);
6262b72b
PM
53 }
54
55 return CMD_RET_SUCCESS;
6262b72b
PM
56}
57
e09b2a02
PM
58static int curr_dev_and_platdata(struct udevice **devp,
59 struct dm_regulator_uclass_platdata **uc_pdata,
60 bool allow_type_fixed)
6262b72b
PM
61{
62 *devp = NULL;
63 *uc_pdata = NULL;
64
e09b2a02
PM
65 if (!currdev) {
66 printf("First, set the regulator device!\n");
67 return CMD_RET_FAILURE;
68 }
6262b72b
PM
69
70 *devp = currdev;
71
72 *uc_pdata = dev_get_uclass_platdata(*devp);
e09b2a02
PM
73 if (!*uc_pdata) {
74 error("Regulator: %s - missing platform data!", currdev->name);
75 return CMD_RET_FAILURE;
76 }
6262b72b
PM
77
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
86static int do_list(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
87{
e09b2a02
PM
88 struct dm_regulator_uclass_platdata *uc_pdata;
89 struct udevice *dev;
6262b72b
PM
90 int ret;
91
e09b2a02
PM
92 printf("| %-*.*s| %-*.*s| %s\n",
93 LIMIT_DEVNAME, LIMIT_DEVNAME, "Device",
94 LIMIT_OFNAME, LIMIT_OFNAME, "regulator-name",
95 "Parent");
6262b72b 96
e09b2a02
PM
97 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
98 ret = uclass_find_next_device(&dev)) {
99 if (ret)
100 continue;
6262b72b 101
e09b2a02
PM
102 uc_pdata = dev_get_uclass_platdata(dev);
103 printf("| %-*.*s| %-*.*s| %s\n",
104 LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name,
105 LIMIT_OFNAME, LIMIT_OFNAME, uc_pdata->name,
106 dev->parent->name);
107 }
108
109 return ret;
6262b72b
PM
110}
111
112static int constraint(const char *name, int val, const char *val_name)
113{
114 printf("%-*s", LIMIT_INFO, name);
115 if (val < 0) {
116 printf(" %s (err: %d)\n", errno_str(val), val);
117 return val;
118 }
119
120 if (val_name)
121 printf(" %d (%s)\n", val, val_name);
122 else
123 printf(" %d\n", val);
124
125 return 0;
126}
127
128static const char *get_mode_name(struct dm_regulator_mode *mode,
129 int mode_count,
130 int mode_id)
131{
132 while (mode_count--) {
133 if (mode->id == mode_id)
134 return mode->name;
135 mode++;
136 }
137
138 return NULL;
139}
140
141static int do_info(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
142{
143 struct udevice *dev;
144 struct dm_regulator_uclass_platdata *uc_pdata;
145 struct dm_regulator_mode *modes;
146 const char *parent_uc;
147 int mode_count;
148 int ret;
149 int i;
150
e09b2a02 151 ret = curr_dev_and_platdata(&dev, &uc_pdata, true);
6262b72b
PM
152 if (ret)
153 return ret;
154
155 parent_uc = dev_get_uclass_name(dev->parent);
156
e09b2a02
PM
157 printf("%s\n%-*s %s\n%-*s %s\n%-*s %s\n%-*s %s\n%-*s\n",
158 "Regulator info:",
159 LIMIT_INFO, "* regulator-name:", uc_pdata->name,
160 LIMIT_INFO, "* device name:", dev->name,
161 LIMIT_INFO, "* parent name:", dev->parent->name,
162 LIMIT_INFO, "* parent uclass:", parent_uc,
6262b72b
PM
163 LIMIT_INFO, "* constraints:");
164
165 constraint(" - min uV:", uc_pdata->min_uV, NULL);
166 constraint(" - max uV:", uc_pdata->max_uV, NULL);
167 constraint(" - min uA:", uc_pdata->min_uA, NULL);
168 constraint(" - max uA:", uc_pdata->max_uA, NULL);
169 constraint(" - always on:", uc_pdata->always_on,
170 uc_pdata->always_on ? "true" : "false");
171 constraint(" - boot on:", uc_pdata->boot_on,
172 uc_pdata->boot_on ? "true" : "false");
173
174 mode_count = regulator_mode(dev, &modes);
175 constraint("* op modes:", mode_count, NULL);
176
177 for (i = 0; i < mode_count; i++, modes++)
178 constraint(" - mode id:", modes->id, modes->name);
179
180 return CMD_RET_SUCCESS;
181}
182
183static int do_status(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
184{
185 struct dm_regulator_uclass_platdata *uc_pdata;
186 int current, value, mode, ret;
187 const char *mode_name = NULL;
188 struct udevice *dev;
189 bool enabled;
190
e09b2a02 191 ret = curr_dev_and_platdata(&dev, &uc_pdata, true);
6262b72b
PM
192 if (ret)
193 return ret;
194
e09b2a02
PM
195 printf("Regulator %s status:\n", uc_pdata->name);
196
6262b72b
PM
197 enabled = regulator_get_enable(dev);
198 constraint(" * enable:", enabled, enabled ? "true" : "false");
199
200 value = regulator_get_value(dev);
201 constraint(" * value uV:", value, NULL);
202
203 current = regulator_get_current(dev);
204 constraint(" * current uA:", current, NULL);
205
206 mode = regulator_get_mode(dev);
207 mode_name = get_mode_name(uc_pdata->mode, uc_pdata->mode_count, mode);
208 constraint(" * mode id:", mode, mode_name);
209
210 return CMD_RET_SUCCESS;
211}
212
213static int do_value(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
214{
215 struct udevice *dev;
216 struct dm_regulator_uclass_platdata *uc_pdata;
217 int value;
218 int force;
219 int ret;
220
e09b2a02 221 ret = curr_dev_and_platdata(&dev, &uc_pdata, argc == 1);
6262b72b
PM
222 if (ret)
223 return ret;
224
225 if (argc == 1) {
e09b2a02
PM
226 ret = regulator_get_value(dev);
227 if (ret < 0) {
228 printf("Regulator: %s - can't get the Voltage!\n",
229 uc_pdata->name);
230 return failure(ret);
231 }
6262b72b 232
e09b2a02 233 printf("%d uV\n", ret);
6262b72b
PM
234 return CMD_RET_SUCCESS;
235 }
236
237 if (argc == 3)
238 force = !strcmp("-f", argv[2]);
239 else
240 force = 0;
241
242 value = simple_strtoul(argv[1], NULL, 0);
243 if ((value < uc_pdata->min_uV || value > uc_pdata->max_uV) && !force) {
244 printf("Value exceeds regulator constraint limits\n");
245 return CMD_RET_FAILURE;
246 }
247
248 ret = regulator_set_value(dev, value);
e09b2a02
PM
249 if (ret) {
250 printf("Regulator: %s - can't set the Voltage!\n",
251 uc_pdata->name);
252 return failure(ret);
253 }
6262b72b
PM
254
255 return CMD_RET_SUCCESS;
256}
257
258static int do_current(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
259{
260 struct udevice *dev;
261 struct dm_regulator_uclass_platdata *uc_pdata;
262 int current;
263 int ret;
264
e09b2a02 265 ret = curr_dev_and_platdata(&dev, &uc_pdata, argc == 1);
6262b72b
PM
266 if (ret)
267 return ret;
268
269 if (argc == 1) {
e09b2a02
PM
270 ret = regulator_get_current(dev);
271 if (ret < 0) {
272 printf("Regulator: %s - can't get the Current!\n",
273 uc_pdata->name);
274 return failure(ret);
275 }
6262b72b 276
e09b2a02 277 printf("%d uA\n", ret);
6262b72b
PM
278 return CMD_RET_SUCCESS;
279 }
280
281 current = simple_strtoul(argv[1], NULL, 0);
282 if (current < uc_pdata->min_uA || current > uc_pdata->max_uA) {
283 printf("Current exceeds regulator constraint limits\n");
284 return CMD_RET_FAILURE;
285 }
286
287 ret = regulator_set_current(dev, current);
e09b2a02
PM
288 if (ret) {
289 printf("Regulator: %s - can't set the Current!\n",
290 uc_pdata->name);
291 return failure(ret);
292 }
6262b72b
PM
293
294 return CMD_RET_SUCCESS;
295}
296
297static int do_mode(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
298{
299 struct udevice *dev;
300 struct dm_regulator_uclass_platdata *uc_pdata;
6262b72b
PM
301 int mode;
302 int ret;
303
e09b2a02 304 ret = curr_dev_and_platdata(&dev, &uc_pdata, false);
6262b72b
PM
305 if (ret)
306 return ret;
307
308 if (argc == 1) {
e09b2a02
PM
309 ret = regulator_get_mode(dev);
310 if (ret < 0) {
311 printf("Regulator: %s - can't get the operation mode!\n",
312 uc_pdata->name);
313 return failure(ret);
314 }
6262b72b 315
e09b2a02 316 printf("mode id: %d\n", ret);
6262b72b
PM
317 return CMD_RET_SUCCESS;
318 }
319
e09b2a02 320 mode = simple_strtoul(argv[1], NULL, 0);
6262b72b 321
e09b2a02
PM
322 ret = regulator_set_mode(dev, mode);
323 if (ret) {
324 printf("Regulator: %s - can't set the operation mode!\n",
325 uc_pdata->name);
326 return failure(ret);
327 }
6262b72b
PM
328
329 return CMD_RET_SUCCESS;
330}
331
332static int do_enable(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
333{
334 struct udevice *dev;
335 struct dm_regulator_uclass_platdata *uc_pdata;
336 int ret;
337
e09b2a02 338 ret = curr_dev_and_platdata(&dev, &uc_pdata, true);
6262b72b
PM
339 if (ret)
340 return ret;
341
342 ret = regulator_set_enable(dev, true);
e09b2a02
PM
343 if (ret) {
344 printf("Regulator: %s - can't enable!\n", uc_pdata->name);
345 return failure(ret);
346 }
6262b72b
PM
347
348 return CMD_RET_SUCCESS;
349}
350
351static int do_disable(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
352{
353 struct udevice *dev;
354 struct dm_regulator_uclass_platdata *uc_pdata;
355 int ret;
356
e09b2a02 357 ret = curr_dev_and_platdata(&dev, &uc_pdata, true);
6262b72b
PM
358 if (ret)
359 return ret;
360
361 ret = regulator_set_enable(dev, false);
e09b2a02
PM
362 if (ret) {
363 printf("Regulator: %s - can't disable!\n", uc_pdata->name);
364 return failure(ret);
365 }
6262b72b
PM
366
367 return CMD_RET_SUCCESS;
368}
369
370static cmd_tbl_t subcmd[] = {
371 U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""),
372 U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""),
373 U_BOOT_CMD_MKENT(info, 2, 1, do_info, "", ""),
374 U_BOOT_CMD_MKENT(status, 2, 1, do_status, "", ""),
375 U_BOOT_CMD_MKENT(value, 3, 1, do_value, "", ""),
376 U_BOOT_CMD_MKENT(current, 3, 1, do_current, "", ""),
377 U_BOOT_CMD_MKENT(mode, 2, 1, do_mode, "", ""),
378 U_BOOT_CMD_MKENT(enable, 1, 1, do_enable, "", ""),
379 U_BOOT_CMD_MKENT(disable, 1, 1, do_disable, "", ""),
380};
381
382static int do_regulator(cmd_tbl_t *cmdtp, int flag, int argc,
383 char * const argv[])
384{
385 cmd_tbl_t *cmd;
386
387 argc--;
388 argv++;
389
390 cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd));
391 if (cmd == NULL || argc > cmd->maxargs)
392 return CMD_RET_USAGE;
393
394 return cmd->cmd(cmdtp, flag, argc, argv);
395}
396
397U_BOOT_CMD(regulator, CONFIG_SYS_MAXARGS, 1, do_regulator,
398 "uclass operations",
e09b2a02
PM
399 "list - list UCLASS regulator devices\n"
400 "regulator dev [regulator-name] - show/[set] operating regulator device\n"
401 "regulator info - print constraints info\n"
402 "regulator status - print operating status\n"
403 "regulator value [val] [-f] - print/[set] voltage value [uV] (force)\n"
404 "regulator current [val] - print/[set] current value [uA]\n"
405 "regulator mode [id] - print/[set] operating mode id\n"
406 "regulator enable - enable the regulator output\n"
407 "regulator disable - disable the regulator output\n"
6262b72b 408);