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