]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
6262b72b PM |
2 | /* |
3 | * Copyright (C) 2014-2015 Samsung Electronics | |
4 | * Przemyslaw Marczak <p.marczak@samsung.com> | |
6262b72b PM |
5 | */ |
6 | #include <common.h> | |
09140113 | 7 | #include <command.h> |
6262b72b PM |
8 | #include <errno.h> |
9 | #include <dm.h> | |
10 | #include <dm/uclass-internal.h> | |
1e94b46f | 11 | #include <linux/printk.h> |
6262b72b PM |
12 | #include <power/regulator.h> |
13 | ||
6262b72b | 14 | #define LIMIT_DEVNAME 20 |
e09b2a02 PM |
15 | #define LIMIT_OFNAME 32 |
16 | #define LIMIT_INFO 18 | |
6262b72b PM |
17 | |
18 | static struct udevice *currdev; | |
19 | ||
e09b2a02 | 20 | static int failure(int ret) |
6262b72b | 21 | { |
e09b2a02 | 22 | printf("Error: %d (%s)\n", ret, errno_str(ret)); |
6262b72b | 23 | |
e09b2a02 | 24 | return CMD_RET_FAILURE; |
6262b72b PM |
25 | } |
26 | ||
09140113 | 27 | static int do_dev(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
6262b72b | 28 | { |
caa4daa2 | 29 | struct dm_regulator_uclass_plat *uc_pdata; |
e09b2a02 PM |
30 | const char *name; |
31 | int ret = -ENXIO; | |
6262b72b PM |
32 | |
33 | switch (argc) { | |
34 | case 2: | |
e09b2a02 PM |
35 | name = argv[1]; |
36 | ret = regulator_get_by_platname(name, &currdev); | |
37 | if (ret) { | |
38 | printf("Can't get the regulator: %s!\n", name); | |
39 | return failure(ret); | |
40 | } | |
16abdd42 | 41 | fallthrough; |
6262b72b | 42 | case 1: |
e09b2a02 PM |
43 | if (!currdev) { |
44 | printf("Regulator device is not set!\n\n"); | |
45 | return CMD_RET_USAGE; | |
46 | } | |
47 | ||
caa4daa2 | 48 | uc_pdata = dev_get_uclass_plat(currdev); |
e09b2a02 PM |
49 | if (!uc_pdata) { |
50 | printf("%s: no regulator platform data!\n", currdev->name); | |
51 | return failure(ret); | |
52 | } | |
6262b72b | 53 | |
e09b2a02 | 54 | printf("dev: %s @ %s\n", uc_pdata->name, currdev->name); |
6262b72b PM |
55 | } |
56 | ||
57 | return CMD_RET_SUCCESS; | |
6262b72b PM |
58 | } |
59 | ||
8a8d24bd SG |
60 | static int curr_dev_and_plat(struct udevice **devp, |
61 | struct dm_regulator_uclass_plat **uc_pdata, | |
62 | bool allow_type_fixed) | |
6262b72b PM |
63 | { |
64 | *devp = NULL; | |
65 | *uc_pdata = NULL; | |
66 | ||
e09b2a02 PM |
67 | if (!currdev) { |
68 | printf("First, set the regulator device!\n"); | |
69 | return CMD_RET_FAILURE; | |
70 | } | |
6262b72b PM |
71 | |
72 | *devp = currdev; | |
73 | ||
caa4daa2 | 74 | *uc_pdata = dev_get_uclass_plat(*devp); |
e09b2a02 | 75 | if (!*uc_pdata) { |
71002b50 | 76 | pr_err("Regulator: %s - missing platform data!\n", currdev->name); |
e09b2a02 PM |
77 | return CMD_RET_FAILURE; |
78 | } | |
6262b72b PM |
79 | |
80 | if (!allow_type_fixed && (*uc_pdata)->type == REGULATOR_TYPE_FIXED) { | |
81 | printf("Operation not allowed for fixed regulator!\n"); | |
82 | return CMD_RET_FAILURE; | |
83 | } | |
84 | ||
85 | return CMD_RET_SUCCESS; | |
86 | } | |
87 | ||
09140113 SG |
88 | static int do_list(struct cmd_tbl *cmdtp, int flag, int argc, |
89 | char *const argv[]) | |
6262b72b | 90 | { |
caa4daa2 | 91 | struct dm_regulator_uclass_plat *uc_pdata; |
e09b2a02 | 92 | struct udevice *dev; |
6262b72b PM |
93 | int ret; |
94 | ||
e09b2a02 PM |
95 | printf("| %-*.*s| %-*.*s| %s\n", |
96 | LIMIT_DEVNAME, LIMIT_DEVNAME, "Device", | |
97 | LIMIT_OFNAME, LIMIT_OFNAME, "regulator-name", | |
98 | "Parent"); | |
6262b72b | 99 | |
e09b2a02 PM |
100 | for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev; |
101 | ret = uclass_find_next_device(&dev)) { | |
102 | if (ret) | |
103 | continue; | |
6262b72b | 104 | |
caa4daa2 | 105 | uc_pdata = dev_get_uclass_plat(dev); |
e09b2a02 PM |
106 | printf("| %-*.*s| %-*.*s| %s\n", |
107 | LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name, | |
108 | LIMIT_OFNAME, LIMIT_OFNAME, uc_pdata->name, | |
109 | dev->parent->name); | |
110 | } | |
111 | ||
112 | return ret; | |
6262b72b PM |
113 | } |
114 | ||
115 | static int constraint(const char *name, int val, const char *val_name) | |
116 | { | |
117 | printf("%-*s", LIMIT_INFO, name); | |
118 | if (val < 0) { | |
119 | printf(" %s (err: %d)\n", errno_str(val), val); | |
120 | return val; | |
121 | } | |
122 | ||
123 | if (val_name) | |
124 | printf(" %d (%s)\n", val, val_name); | |
125 | else | |
126 | printf(" %d\n", val); | |
127 | ||
128 | return 0; | |
129 | } | |
130 | ||
131 | static const char *get_mode_name(struct dm_regulator_mode *mode, | |
132 | int mode_count, | |
133 | int mode_id) | |
134 | { | |
135 | while (mode_count--) { | |
136 | if (mode->id == mode_id) | |
137 | return mode->name; | |
138 | mode++; | |
139 | } | |
140 | ||
141 | return NULL; | |
142 | } | |
143 | ||
09140113 SG |
144 | static int do_info(struct cmd_tbl *cmdtp, int flag, int argc, |
145 | char *const argv[]) | |
6262b72b PM |
146 | { |
147 | struct udevice *dev; | |
caa4daa2 | 148 | struct dm_regulator_uclass_plat *uc_pdata; |
6262b72b PM |
149 | struct dm_regulator_mode *modes; |
150 | const char *parent_uc; | |
151 | int mode_count; | |
152 | int ret; | |
153 | int i; | |
154 | ||
8a8d24bd | 155 | ret = curr_dev_and_plat(&dev, &uc_pdata, true); |
6262b72b PM |
156 | if (ret) |
157 | return ret; | |
158 | ||
159 | parent_uc = dev_get_uclass_name(dev->parent); | |
160 | ||
e09b2a02 PM |
161 | printf("%s\n%-*s %s\n%-*s %s\n%-*s %s\n%-*s %s\n%-*s\n", |
162 | "Regulator info:", | |
163 | LIMIT_INFO, "* regulator-name:", uc_pdata->name, | |
164 | LIMIT_INFO, "* device name:", dev->name, | |
165 | LIMIT_INFO, "* parent name:", dev->parent->name, | |
166 | LIMIT_INFO, "* parent uclass:", parent_uc, | |
6262b72b PM |
167 | LIMIT_INFO, "* constraints:"); |
168 | ||
169 | constraint(" - min uV:", uc_pdata->min_uV, NULL); | |
170 | constraint(" - max uV:", uc_pdata->max_uV, NULL); | |
171 | constraint(" - min uA:", uc_pdata->min_uA, NULL); | |
172 | constraint(" - max uA:", uc_pdata->max_uA, NULL); | |
173 | constraint(" - always on:", uc_pdata->always_on, | |
174 | uc_pdata->always_on ? "true" : "false"); | |
175 | constraint(" - boot on:", uc_pdata->boot_on, | |
176 | uc_pdata->boot_on ? "true" : "false"); | |
177 | ||
178 | mode_count = regulator_mode(dev, &modes); | |
179 | constraint("* op modes:", mode_count, NULL); | |
180 | ||
181 | for (i = 0; i < mode_count; i++, modes++) | |
182 | constraint(" - mode id:", modes->id, modes->name); | |
183 | ||
184 | return CMD_RET_SUCCESS; | |
185 | } | |
186 | ||
931b24c5 | 187 | static void do_status_detail(struct udevice *dev, |
caa4daa2 | 188 | struct dm_regulator_uclass_plat *uc_pdata) |
6262b72b | 189 | { |
931b24c5 SG |
190 | int current, value, mode; |
191 | const char *mode_name; | |
6262b72b PM |
192 | bool enabled; |
193 | ||
e09b2a02 PM |
194 | printf("Regulator %s status:\n", uc_pdata->name); |
195 | ||
6262b72b PM |
196 | enabled = regulator_get_enable(dev); |
197 | constraint(" * enable:", enabled, enabled ? "true" : "false"); | |
198 | ||
199 | value = regulator_get_value(dev); | |
200 | constraint(" * value uV:", value, NULL); | |
201 | ||
202 | current = regulator_get_current(dev); | |
203 | constraint(" * current uA:", current, NULL); | |
204 | ||
205 | mode = regulator_get_mode(dev); | |
206 | mode_name = get_mode_name(uc_pdata->mode, uc_pdata->mode_count, mode); | |
207 | constraint(" * mode id:", mode, mode_name); | |
931b24c5 SG |
208 | } |
209 | ||
8676ae36 | 210 | static void do_status_line(struct udevice *dev, int status) |
931b24c5 | 211 | { |
caa4daa2 | 212 | struct dm_regulator_uclass_plat *pdata; |
931b24c5 SG |
213 | int current, value, mode; |
214 | const char *mode_name; | |
215 | bool enabled; | |
216 | ||
caa4daa2 | 217 | pdata = dev_get_uclass_plat(dev); |
931b24c5 SG |
218 | enabled = regulator_get_enable(dev); |
219 | value = regulator_get_value(dev); | |
220 | current = regulator_get_current(dev); | |
221 | mode = regulator_get_mode(dev); | |
222 | mode_name = get_mode_name(pdata->mode, pdata->mode_count, mode); | |
223 | printf("%-20s %-10s ", pdata->name, enabled ? "enabled" : "disabled"); | |
224 | if (value >= 0) | |
225 | printf("%10d ", value); | |
226 | else | |
227 | printf("%10s ", "-"); | |
228 | if (current >= 0) | |
229 | printf("%10d ", current); | |
230 | else | |
231 | printf("%10s ", "-"); | |
232 | if (mode >= 0) | |
233 | printf("%-10s", mode_name); | |
234 | else | |
235 | printf("%-10s", "-"); | |
8676ae36 | 236 | printf(" %i", status); |
931b24c5 SG |
237 | printf("\n"); |
238 | } | |
239 | ||
09140113 SG |
240 | static int do_status(struct cmd_tbl *cmdtp, int flag, int argc, |
241 | char *const argv[]) | |
931b24c5 | 242 | { |
caa4daa2 | 243 | struct dm_regulator_uclass_plat *uc_pdata; |
931b24c5 SG |
244 | struct udevice *dev; |
245 | int ret; | |
246 | ||
247 | if (currdev && (argc < 2 || strcmp(argv[1], "-a"))) { | |
8a8d24bd | 248 | ret = curr_dev_and_plat(&dev, &uc_pdata, true); |
931b24c5 SG |
249 | if (ret) |
250 | return CMD_RET_FAILURE; | |
251 | do_status_detail(dev, uc_pdata); | |
252 | return 0; | |
253 | } | |
254 | ||
255 | /* Show all of them in a list, probing them as needed */ | |
8676ae36 MS |
256 | printf("%-20s %-10s %10s %10s %-10s %s\n", "Name", "Enabled", "uV", "mA", |
257 | "Mode", "Status"); | |
258 | for (ret = uclass_first_device_check(UCLASS_REGULATOR, &dev); dev; | |
259 | ret = uclass_next_device_check(&dev)) | |
260 | do_status_line(dev, ret); | |
6262b72b PM |
261 | |
262 | return CMD_RET_SUCCESS; | |
263 | } | |
264 | ||
09140113 SG |
265 | static int do_value(struct cmd_tbl *cmdtp, int flag, int argc, |
266 | char *const argv[]) | |
6262b72b PM |
267 | { |
268 | struct udevice *dev; | |
caa4daa2 | 269 | struct dm_regulator_uclass_plat *uc_pdata; |
6262b72b PM |
270 | int value; |
271 | int force; | |
272 | int ret; | |
273 | ||
8a8d24bd | 274 | ret = curr_dev_and_plat(&dev, &uc_pdata, argc == 1); |
6262b72b PM |
275 | if (ret) |
276 | return ret; | |
277 | ||
278 | if (argc == 1) { | |
e09b2a02 PM |
279 | ret = regulator_get_value(dev); |
280 | if (ret < 0) { | |
281 | printf("Regulator: %s - can't get the Voltage!\n", | |
282 | uc_pdata->name); | |
283 | return failure(ret); | |
284 | } | |
6262b72b | 285 | |
e09b2a02 | 286 | printf("%d uV\n", ret); |
6262b72b PM |
287 | return CMD_RET_SUCCESS; |
288 | } | |
289 | ||
290 | if (argc == 3) | |
291 | force = !strcmp("-f", argv[2]); | |
292 | else | |
293 | force = 0; | |
294 | ||
295 | value = simple_strtoul(argv[1], NULL, 0); | |
296 | if ((value < uc_pdata->min_uV || value > uc_pdata->max_uV) && !force) { | |
224d1ddc SG |
297 | printf("Value exceeds regulator constraint limits %d..%d uV\n", |
298 | uc_pdata->min_uV, uc_pdata->max_uV); | |
6262b72b PM |
299 | return CMD_RET_FAILURE; |
300 | } | |
301 | ||
2f5d532f K |
302 | if (!force) |
303 | ret = regulator_set_value(dev, value); | |
304 | else | |
305 | ret = regulator_set_value_force(dev, value); | |
e09b2a02 PM |
306 | if (ret) { |
307 | printf("Regulator: %s - can't set the Voltage!\n", | |
308 | uc_pdata->name); | |
309 | return failure(ret); | |
310 | } | |
6262b72b PM |
311 | |
312 | return CMD_RET_SUCCESS; | |
313 | } | |
314 | ||
09140113 SG |
315 | static int do_current(struct cmd_tbl *cmdtp, int flag, int argc, |
316 | char *const argv[]) | |
6262b72b PM |
317 | { |
318 | struct udevice *dev; | |
caa4daa2 | 319 | struct dm_regulator_uclass_plat *uc_pdata; |
6262b72b PM |
320 | int current; |
321 | int ret; | |
322 | ||
8a8d24bd | 323 | ret = curr_dev_and_plat(&dev, &uc_pdata, argc == 1); |
6262b72b PM |
324 | if (ret) |
325 | return ret; | |
326 | ||
327 | if (argc == 1) { | |
e09b2a02 PM |
328 | ret = regulator_get_current(dev); |
329 | if (ret < 0) { | |
330 | printf("Regulator: %s - can't get the Current!\n", | |
331 | uc_pdata->name); | |
332 | return failure(ret); | |
333 | } | |
6262b72b | 334 | |
e09b2a02 | 335 | printf("%d uA\n", ret); |
6262b72b PM |
336 | return CMD_RET_SUCCESS; |
337 | } | |
338 | ||
339 | current = simple_strtoul(argv[1], NULL, 0); | |
340 | if (current < uc_pdata->min_uA || current > uc_pdata->max_uA) { | |
341 | printf("Current exceeds regulator constraint limits\n"); | |
342 | return CMD_RET_FAILURE; | |
343 | } | |
344 | ||
345 | ret = regulator_set_current(dev, current); | |
e09b2a02 PM |
346 | if (ret) { |
347 | printf("Regulator: %s - can't set the Current!\n", | |
348 | uc_pdata->name); | |
349 | return failure(ret); | |
350 | } | |
6262b72b PM |
351 | |
352 | return CMD_RET_SUCCESS; | |
353 | } | |
354 | ||
09140113 SG |
355 | static int do_mode(struct cmd_tbl *cmdtp, int flag, int argc, |
356 | char *const argv[]) | |
6262b72b PM |
357 | { |
358 | struct udevice *dev; | |
caa4daa2 | 359 | struct dm_regulator_uclass_plat *uc_pdata; |
6262b72b PM |
360 | int mode; |
361 | int ret; | |
362 | ||
8a8d24bd | 363 | ret = curr_dev_and_plat(&dev, &uc_pdata, false); |
6262b72b PM |
364 | if (ret) |
365 | return ret; | |
366 | ||
367 | if (argc == 1) { | |
e09b2a02 PM |
368 | ret = regulator_get_mode(dev); |
369 | if (ret < 0) { | |
370 | printf("Regulator: %s - can't get the operation mode!\n", | |
371 | uc_pdata->name); | |
372 | return failure(ret); | |
373 | } | |
6262b72b | 374 | |
e09b2a02 | 375 | printf("mode id: %d\n", ret); |
6262b72b PM |
376 | return CMD_RET_SUCCESS; |
377 | } | |
378 | ||
e09b2a02 | 379 | mode = simple_strtoul(argv[1], NULL, 0); |
6262b72b | 380 | |
e09b2a02 PM |
381 | ret = regulator_set_mode(dev, mode); |
382 | if (ret) { | |
383 | printf("Regulator: %s - can't set the operation mode!\n", | |
384 | uc_pdata->name); | |
385 | return failure(ret); | |
386 | } | |
6262b72b PM |
387 | |
388 | return CMD_RET_SUCCESS; | |
389 | } | |
390 | ||
09140113 SG |
391 | static int do_enable(struct cmd_tbl *cmdtp, int flag, int argc, |
392 | char *const argv[]) | |
6262b72b PM |
393 | { |
394 | struct udevice *dev; | |
caa4daa2 | 395 | struct dm_regulator_uclass_plat *uc_pdata; |
6262b72b PM |
396 | int ret; |
397 | ||
8a8d24bd | 398 | ret = curr_dev_and_plat(&dev, &uc_pdata, true); |
6262b72b PM |
399 | if (ret) |
400 | return ret; | |
401 | ||
402 | ret = regulator_set_enable(dev, true); | |
e09b2a02 PM |
403 | if (ret) { |
404 | printf("Regulator: %s - can't enable!\n", uc_pdata->name); | |
405 | return failure(ret); | |
406 | } | |
6262b72b PM |
407 | |
408 | return CMD_RET_SUCCESS; | |
409 | } | |
410 | ||
09140113 SG |
411 | static int do_disable(struct cmd_tbl *cmdtp, int flag, int argc, |
412 | char *const argv[]) | |
6262b72b PM |
413 | { |
414 | struct udevice *dev; | |
caa4daa2 | 415 | struct dm_regulator_uclass_plat *uc_pdata; |
6262b72b PM |
416 | int ret; |
417 | ||
8a8d24bd | 418 | ret = curr_dev_and_plat(&dev, &uc_pdata, true); |
6262b72b PM |
419 | if (ret) |
420 | return ret; | |
421 | ||
422 | ret = regulator_set_enable(dev, false); | |
e09b2a02 PM |
423 | if (ret) { |
424 | printf("Regulator: %s - can't disable!\n", uc_pdata->name); | |
425 | return failure(ret); | |
426 | } | |
6262b72b PM |
427 | |
428 | return CMD_RET_SUCCESS; | |
429 | } | |
430 | ||
09140113 | 431 | static struct cmd_tbl subcmd[] = { |
6262b72b PM |
432 | U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""), |
433 | U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""), | |
434 | U_BOOT_CMD_MKENT(info, 2, 1, do_info, "", ""), | |
435 | U_BOOT_CMD_MKENT(status, 2, 1, do_status, "", ""), | |
436 | U_BOOT_CMD_MKENT(value, 3, 1, do_value, "", ""), | |
437 | U_BOOT_CMD_MKENT(current, 3, 1, do_current, "", ""), | |
438 | U_BOOT_CMD_MKENT(mode, 2, 1, do_mode, "", ""), | |
439 | U_BOOT_CMD_MKENT(enable, 1, 1, do_enable, "", ""), | |
440 | U_BOOT_CMD_MKENT(disable, 1, 1, do_disable, "", ""), | |
441 | }; | |
442 | ||
09140113 SG |
443 | static int do_regulator(struct cmd_tbl *cmdtp, int flag, int argc, |
444 | char *const argv[]) | |
6262b72b | 445 | { |
09140113 | 446 | struct cmd_tbl *cmd; |
6262b72b PM |
447 | |
448 | argc--; | |
449 | argv++; | |
450 | ||
451 | cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd)); | |
452 | if (cmd == NULL || argc > cmd->maxargs) | |
453 | return CMD_RET_USAGE; | |
454 | ||
455 | return cmd->cmd(cmdtp, flag, argc, argv); | |
456 | } | |
457 | ||
458 | U_BOOT_CMD(regulator, CONFIG_SYS_MAXARGS, 1, do_regulator, | |
459 | "uclass operations", | |
e09b2a02 PM |
460 | "list - list UCLASS regulator devices\n" |
461 | "regulator dev [regulator-name] - show/[set] operating regulator device\n" | |
462 | "regulator info - print constraints info\n" | |
931b24c5 | 463 | "regulator status [-a] - print operating status [for all]\n" |
e09b2a02 PM |
464 | "regulator value [val] [-f] - print/[set] voltage value [uV] (force)\n" |
465 | "regulator current [val] - print/[set] current value [uA]\n" | |
466 | "regulator mode [id] - print/[set] operating mode id\n" | |
467 | "regulator enable - enable the regulator output\n" | |
468 | "regulator disable - disable the regulator output\n" | |
6262b72b | 469 | ); |