blob: 793f08e81a92fef5c996b9518c7f00f7ed81c6e3 [file] [log] [blame]
Przemyslaw Marczak6262b722015-04-20 20:07:44 +02001/*
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
Przemyslaw Marczak6262b722015-04-20 20:07:44 +020013#define LIMIT_DEVNAME 20
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +020014#define LIMIT_OFNAME 32
15#define LIMIT_INFO 18
Przemyslaw Marczak6262b722015-04-20 20:07:44 +020016
17static struct udevice *currdev;
18
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +020019static int failure(int ret)
Przemyslaw Marczak6262b722015-04-20 20:07:44 +020020{
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +020021 printf("Error: %d (%s)\n", ret, errno_str(ret));
22
Przemyslaw Marczak6262b722015-04-20 20:07:44 +020023 return CMD_RET_FAILURE;
24}
25
Przemyslaw Marczak6262b722015-04-20 20:07:44 +020026static int do_dev(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
27{
28 struct dm_regulator_uclass_platdata *uc_pdata;
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +020029 const char *name;
30 int ret = -ENXIO;
Przemyslaw Marczak6262b722015-04-20 20:07:44 +020031
32 switch (argc) {
33 case 2:
Przemyslaw Marczake09b2a02015-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 Marczak6262b722015-04-20 20:07:44 +020040 case 1:
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +020041 if (!currdev) {
42 printf("Regulator device is not set!\n\n");
43 return CMD_RET_USAGE;
44 }
Przemyslaw Marczak6262b722015-04-20 20:07:44 +020045
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +020046 uc_pdata = dev_get_uclass_platdata(currdev);
47 if (!uc_pdata) {
48 printf("%s: no regulator platform data!\n", currdev->name);
49 return failure(ret);
50 }
51
52 printf("dev: %s @ %s\n", uc_pdata->name, currdev->name);
Przemyslaw Marczak6262b722015-04-20 20:07:44 +020053 }
54
55 return CMD_RET_SUCCESS;
Przemyslaw Marczak6262b722015-04-20 20:07:44 +020056}
57
Przemyslaw Marczake09b2a02015-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 Marczak6262b722015-04-20 20:07:44 +020061{
62 *devp = NULL;
63 *uc_pdata = NULL;
64
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +020065 if (!currdev) {
66 printf("First, set the regulator device!\n");
67 return CMD_RET_FAILURE;
68 }
Przemyslaw Marczak6262b722015-04-20 20:07:44 +020069
70 *devp = currdev;
71
72 *uc_pdata = dev_get_uclass_platdata(*devp);
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +020073 if (!*uc_pdata) {
74 error("Regulator: %s - missing platform data!", currdev->name);
75 return CMD_RET_FAILURE;
76 }
Przemyslaw Marczak6262b722015-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
86static int do_list(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
87{
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +020088 struct dm_regulator_uclass_platdata *uc_pdata;
89 struct udevice *dev;
Przemyslaw Marczak6262b722015-04-20 20:07:44 +020090 int ret;
91
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +020092 printf("| %-*.*s| %-*.*s| %s\n",
93 LIMIT_DEVNAME, LIMIT_DEVNAME, "Device",
94 LIMIT_OFNAME, LIMIT_OFNAME, "regulator-name",
95 "Parent");
Przemyslaw Marczak6262b722015-04-20 20:07:44 +020096
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +020097 for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
98 ret = uclass_find_next_device(&dev)) {
99 if (ret)
100 continue;
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200101
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200102 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;
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200110}
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
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200151 ret = curr_dev_and_platdata(&dev, &uc_pdata, true);
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200152 if (ret)
153 return ret;
154
155 parent_uc = dev_get_uclass_name(dev->parent);
156
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200157 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,
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200163 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
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200191 ret = curr_dev_and_platdata(&dev, &uc_pdata, true);
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200192 if (ret)
193 return ret;
194
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200195 printf("Regulator %s status:\n", uc_pdata->name);
196
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200197 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
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200221 ret = curr_dev_and_platdata(&dev, &uc_pdata, argc == 1);
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200222 if (ret)
223 return ret;
224
225 if (argc == 1) {
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200226 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 }
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200232
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200233 printf("%d uV\n", ret);
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200234 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) {
Simon Glass224d1dd2015-07-02 18:16:07 -0600244 printf("Value exceeds regulator constraint limits %d..%d uV\n",
245 uc_pdata->min_uV, uc_pdata->max_uV);
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200246 return CMD_RET_FAILURE;
247 }
248
249 ret = regulator_set_value(dev, value);
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200250 if (ret) {
251 printf("Regulator: %s - can't set the Voltage!\n",
252 uc_pdata->name);
253 return failure(ret);
254 }
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200255
256 return CMD_RET_SUCCESS;
257}
258
259static int do_current(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
260{
261 struct udevice *dev;
262 struct dm_regulator_uclass_platdata *uc_pdata;
263 int current;
264 int ret;
265
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200266 ret = curr_dev_and_platdata(&dev, &uc_pdata, argc == 1);
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200267 if (ret)
268 return ret;
269
270 if (argc == 1) {
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200271 ret = regulator_get_current(dev);
272 if (ret < 0) {
273 printf("Regulator: %s - can't get the Current!\n",
274 uc_pdata->name);
275 return failure(ret);
276 }
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200277
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200278 printf("%d uA\n", ret);
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200279 return CMD_RET_SUCCESS;
280 }
281
282 current = simple_strtoul(argv[1], NULL, 0);
283 if (current < uc_pdata->min_uA || current > uc_pdata->max_uA) {
284 printf("Current exceeds regulator constraint limits\n");
285 return CMD_RET_FAILURE;
286 }
287
288 ret = regulator_set_current(dev, current);
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200289 if (ret) {
290 printf("Regulator: %s - can't set the Current!\n",
291 uc_pdata->name);
292 return failure(ret);
293 }
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200294
295 return CMD_RET_SUCCESS;
296}
297
298static int do_mode(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
299{
300 struct udevice *dev;
301 struct dm_regulator_uclass_platdata *uc_pdata;
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200302 int mode;
303 int ret;
304
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200305 ret = curr_dev_and_platdata(&dev, &uc_pdata, false);
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200306 if (ret)
307 return ret;
308
309 if (argc == 1) {
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200310 ret = regulator_get_mode(dev);
311 if (ret < 0) {
312 printf("Regulator: %s - can't get the operation mode!\n",
313 uc_pdata->name);
314 return failure(ret);
315 }
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200316
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200317 printf("mode id: %d\n", ret);
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200318 return CMD_RET_SUCCESS;
319 }
320
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200321 mode = simple_strtoul(argv[1], NULL, 0);
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200322
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200323 ret = regulator_set_mode(dev, mode);
324 if (ret) {
325 printf("Regulator: %s - can't set the operation mode!\n",
326 uc_pdata->name);
327 return failure(ret);
328 }
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200329
330 return CMD_RET_SUCCESS;
331}
332
333static int do_enable(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
334{
335 struct udevice *dev;
336 struct dm_regulator_uclass_platdata *uc_pdata;
337 int ret;
338
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200339 ret = curr_dev_and_platdata(&dev, &uc_pdata, true);
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200340 if (ret)
341 return ret;
342
343 ret = regulator_set_enable(dev, true);
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200344 if (ret) {
345 printf("Regulator: %s - can't enable!\n", uc_pdata->name);
346 return failure(ret);
347 }
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200348
349 return CMD_RET_SUCCESS;
350}
351
352static int do_disable(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
353{
354 struct udevice *dev;
355 struct dm_regulator_uclass_platdata *uc_pdata;
356 int ret;
357
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200358 ret = curr_dev_and_platdata(&dev, &uc_pdata, true);
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200359 if (ret)
360 return ret;
361
362 ret = regulator_set_enable(dev, false);
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200363 if (ret) {
364 printf("Regulator: %s - can't disable!\n", uc_pdata->name);
365 return failure(ret);
366 }
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200367
368 return CMD_RET_SUCCESS;
369}
370
371static cmd_tbl_t subcmd[] = {
372 U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""),
373 U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""),
374 U_BOOT_CMD_MKENT(info, 2, 1, do_info, "", ""),
375 U_BOOT_CMD_MKENT(status, 2, 1, do_status, "", ""),
376 U_BOOT_CMD_MKENT(value, 3, 1, do_value, "", ""),
377 U_BOOT_CMD_MKENT(current, 3, 1, do_current, "", ""),
378 U_BOOT_CMD_MKENT(mode, 2, 1, do_mode, "", ""),
379 U_BOOT_CMD_MKENT(enable, 1, 1, do_enable, "", ""),
380 U_BOOT_CMD_MKENT(disable, 1, 1, do_disable, "", ""),
381};
382
383static int do_regulator(cmd_tbl_t *cmdtp, int flag, int argc,
384 char * const argv[])
385{
386 cmd_tbl_t *cmd;
387
388 argc--;
389 argv++;
390
391 cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd));
392 if (cmd == NULL || argc > cmd->maxargs)
393 return CMD_RET_USAGE;
394
395 return cmd->cmd(cmdtp, flag, argc, argv);
396}
397
398U_BOOT_CMD(regulator, CONFIG_SYS_MAXARGS, 1, do_regulator,
399 "uclass operations",
Przemyslaw Marczake09b2a02015-05-13 13:38:29 +0200400 "list - list UCLASS regulator devices\n"
401 "regulator dev [regulator-name] - show/[set] operating regulator device\n"
402 "regulator info - print constraints info\n"
403 "regulator status - print operating status\n"
404 "regulator value [val] [-f] - print/[set] voltage value [uV] (force)\n"
405 "regulator current [val] - print/[set] current value [uA]\n"
406 "regulator mode [id] - print/[set] operating mode id\n"
407 "regulator enable - enable the regulator output\n"
408 "regulator disable - disable the regulator output\n"
Przemyslaw Marczak6262b722015-04-20 20:07:44 +0200409);