]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_regulator.c
dm: regulator: add regulator command
[karo-tx-uboot.git] / common / cmd_regulator.c
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
13 #define LIMIT_SEQ       3
14 #define LIMIT_DEVNAME   20
15 #define LIMIT_OFNAME    20
16 #define LIMIT_INFO      16
17
18 static struct udevice *currdev;
19
20 static int failed(const char *getset, const char *thing,
21                   const char *for_dev, int ret)
22 {
23         printf("Can't %s %s %s.\nError: %d (%s)\n", getset, thing, for_dev,
24                                                     ret, errno_str(ret));
25         return CMD_RET_FAILURE;
26 }
27
28 static int regulator_get(bool list_only, int get_seq, struct udevice **devp)
29 {
30         struct dm_regulator_uclass_platdata *uc_pdata;
31         struct udevice *dev;
32         int ret;
33
34         if (devp)
35                 *devp = NULL;
36
37         for (ret = uclass_first_device(UCLASS_REGULATOR, &dev); dev;
38              ret = uclass_next_device(&dev)) {
39                 if (list_only) {
40                         uc_pdata = dev_get_uclass_platdata(dev);
41                         printf("|%*d | %*.*s @ %-*.*s| %s @ %s\n",
42                                LIMIT_SEQ, dev->seq,
43                                LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name,
44                                LIMIT_OFNAME, LIMIT_OFNAME, uc_pdata->name,
45                                dev->parent->name,
46                                dev_get_uclass_name(dev->parent));
47                         continue;
48                 }
49
50                 if (dev->seq == get_seq) {
51                         if (devp)
52                                 *devp = dev;
53                         else
54                                 return -EINVAL;
55
56                         return 0;
57                 }
58         }
59
60         if (list_only)
61                 return ret;
62
63         return -ENODEV;
64 }
65
66 static int do_dev(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
67 {
68         struct dm_regulator_uclass_platdata *uc_pdata;
69         int seq, ret = -ENXIO;
70
71         switch (argc) {
72         case 2:
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)))
76                         goto failed;
77         case 1:
78                 uc_pdata = dev_get_uclass_platdata(currdev);
79                 if (!uc_pdata)
80                         goto failed;
81
82                 printf("dev: %d @ %s\n", currdev->seq, uc_pdata->name);
83         }
84
85         return CMD_RET_SUCCESS;
86 failed:
87         return failed("get", "the", "device", ret);
88 }
89
90 static int get_curr_dev_and_pl(struct udevice **devp,
91                                struct dm_regulator_uclass_platdata **uc_pdata,
92                                bool allow_type_fixed)
93 {
94         *devp = NULL;
95         *uc_pdata = NULL;
96
97         if (!currdev)
98                 return failed("get", "current", "device", -ENODEV);
99
100         *devp = currdev;
101
102         *uc_pdata = dev_get_uclass_platdata(*devp);
103         if (!*uc_pdata)
104                 return failed("get", "regulator", "platdata", -ENXIO);
105
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;
109         }
110
111         return CMD_RET_SUCCESS;
112 }
113
114 static int do_list(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
115 {
116         int ret;
117
118         printf("|%*s | %*.*s @ %-*.*s| %s @ %s\n",
119                LIMIT_SEQ, "Seq",
120                LIMIT_DEVNAME, LIMIT_DEVNAME, "Name",
121                LIMIT_OFNAME, LIMIT_OFNAME, "fdtname",
122                "Parent", "uclass");
123
124         ret = regulator_get(true, 0, NULL);
125         if (ret)
126                 return CMD_RET_FAILURE;
127
128         return CMD_RET_SUCCESS;
129 }
130
131 static int constraint(const char *name, int val, const char *val_name)
132 {
133         printf("%-*s", LIMIT_INFO, name);
134         if (val < 0) {
135                 printf(" %s (err: %d)\n", errno_str(val), val);
136                 return val;
137         }
138
139         if (val_name)
140                 printf(" %d (%s)\n", val, val_name);
141         else
142                 printf(" %d\n", val);
143
144         return 0;
145 }
146
147 static const char *get_mode_name(struct dm_regulator_mode *mode,
148                                  int mode_count,
149                                  int mode_id)
150 {
151         while (mode_count--) {
152                 if (mode->id == mode_id)
153                         return mode->name;
154                 mode++;
155         }
156
157         return NULL;
158 }
159
160 static int do_info(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
161 {
162         struct udevice *dev;
163         struct dm_regulator_uclass_platdata *uc_pdata;
164         struct dm_regulator_mode *modes;
165         const char *parent_uc;
166         int mode_count;
167         int ret;
168         int i;
169
170         ret = get_curr_dev_and_pl(&dev, &uc_pdata, true);
171         if (ret)
172                 return ret;
173
174         parent_uc = dev_get_uclass_name(dev->parent);
175
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:");
182
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");
191
192         mode_count = regulator_mode(dev, &modes);
193         constraint("* op modes:", mode_count, NULL);
194
195         for (i = 0; i < mode_count; i++, modes++)
196                 constraint("  - mode id:", modes->id, modes->name);
197
198         return CMD_RET_SUCCESS;
199 }
200
201 static int do_status(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
202 {
203         struct dm_regulator_uclass_platdata *uc_pdata;
204         int current, value, mode, ret;
205         const char *mode_name = NULL;
206         struct udevice *dev;
207         bool enabled;
208
209         ret = get_curr_dev_and_pl(&dev, &uc_pdata, true);
210         if (ret)
211                 return ret;
212
213         enabled = regulator_get_enable(dev);
214         constraint(" * enable:", enabled, enabled ? "true" : "false");
215
216         value = regulator_get_value(dev);
217         constraint(" * value uV:", value, NULL);
218
219         current = regulator_get_current(dev);
220         constraint(" * current uA:", current, NULL);
221
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);
225
226         return CMD_RET_SUCCESS;
227 }
228
229 static int do_value(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
230 {
231         struct udevice *dev;
232         struct dm_regulator_uclass_platdata *uc_pdata;
233         int value;
234         int force;
235         int ret;
236
237         ret = get_curr_dev_and_pl(&dev, &uc_pdata, argc == 1);
238         if (ret)
239                 return ret;
240
241         if (argc == 1) {
242                 value = regulator_get_value(dev);
243                 if (value < 0)
244                         return failed("get", uc_pdata->name, "voltage", value);
245
246                 printf("%d uV\n", value);
247                 return CMD_RET_SUCCESS;
248         }
249
250         if (argc == 3)
251                 force = !strcmp("-f", argv[2]);
252         else
253                 force = 0;
254
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;
259         }
260
261         ret = regulator_set_value(dev, value);
262         if (ret)
263                 return failed("set", uc_pdata->name, "voltage value", ret);
264
265         return CMD_RET_SUCCESS;
266 }
267
268 static int do_current(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
269 {
270         struct udevice *dev;
271         struct dm_regulator_uclass_platdata *uc_pdata;
272         int current;
273         int ret;
274
275         ret = get_curr_dev_and_pl(&dev, &uc_pdata, argc == 1);
276         if (ret)
277                 return ret;
278
279         if (argc == 1) {
280                 current = regulator_get_current(dev);
281                 if (current < 0)
282                         return failed("get", uc_pdata->name, "current", current);
283
284                 printf("%d uA\n", current);
285                 return CMD_RET_SUCCESS;
286         }
287
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;
292         }
293
294         ret = regulator_set_current(dev, current);
295         if (ret)
296                 return failed("set", uc_pdata->name, "current value", ret);
297
298         return CMD_RET_SUCCESS;
299 }
300
301 static int do_mode(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
302 {
303         struct udevice *dev;
304         struct dm_regulator_uclass_platdata *uc_pdata;
305         int new_mode;
306         int mode;
307         int ret;
308
309         ret = get_curr_dev_and_pl(&dev, &uc_pdata, false);
310         if (ret)
311                 return ret;
312
313         if (argc == 1) {
314                 mode = regulator_get_mode(dev);
315                 if (mode < 0)
316                         return failed("get", uc_pdata->name, "mode", mode);
317
318                 printf("mode id: %d\n", mode);
319                 return CMD_RET_SUCCESS;
320         }
321
322         new_mode = simple_strtoul(argv[1], NULL, 0);
323
324         ret = regulator_set_mode(dev, new_mode);
325         if (ret)
326                 return failed("set", uc_pdata->name, "mode", ret);
327
328         return CMD_RET_SUCCESS;
329 }
330
331 static int do_enable(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
332 {
333         struct udevice *dev;
334         struct dm_regulator_uclass_platdata *uc_pdata;
335         int ret;
336
337         ret = get_curr_dev_and_pl(&dev, &uc_pdata, true);
338         if (ret)
339                 return ret;
340
341         ret = regulator_set_enable(dev, true);
342         if (ret)
343                 return failed("enable", "regulator", uc_pdata->name, ret);
344
345         return CMD_RET_SUCCESS;
346 }
347
348 static int do_disable(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
349 {
350         struct udevice *dev;
351         struct dm_regulator_uclass_platdata *uc_pdata;
352         int ret;
353
354         ret = get_curr_dev_and_pl(&dev, &uc_pdata, true);
355         if (ret)
356                 return ret;
357
358         ret = regulator_set_enable(dev, false);
359         if (ret)
360                 return failed("disable", "regulator", uc_pdata->name, ret);
361
362         return CMD_RET_SUCCESS;
363 }
364
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, "", ""),
375 };
376
377 static int do_regulator(cmd_tbl_t *cmdtp, int flag, int argc,
378                         char * const argv[])
379 {
380         cmd_tbl_t *cmd;
381
382         argc--;
383         argv++;
384
385         cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd));
386         if (cmd == NULL || argc > cmd->maxargs)
387                 return CMD_RET_USAGE;
388
389         return cmd->cmd(cmdtp, flag, argc, argv);
390 }
391
392 U_BOOT_CMD(regulator, CONFIG_SYS_MAXARGS, 1, do_regulator,
393         "uclass operations",
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"
403 );