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