Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2014-2015 Samsung Electronics |
| 4 | * Przemyslaw Marczak <p.marczak@samsung.com> |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 5 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 6 | #include <command.h> |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 7 | #include <errno.h> |
| 8 | #include <dm.h> |
| 9 | #include <dm/uclass-internal.h> |
| 10 | #include <power/pmic.h> |
| 11 | |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 12 | #define LIMIT_DEV 32 |
| 13 | #define LIMIT_PARENT 20 |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 14 | |
| 15 | static struct udevice *currdev; |
| 16 | |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 17 | static int failure(int ret) |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 18 | { |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 19 | printf("Error: %d (%s)\n", ret, errno_str(ret)); |
| 20 | |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 21 | return CMD_RET_FAILURE; |
| 22 | } |
| 23 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 24 | static int do_dev(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 25 | { |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 26 | char *name; |
| 27 | int ret = -ENODEV; |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 28 | |
| 29 | switch (argc) { |
| 30 | case 2: |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 31 | name = argv[1]; |
| 32 | ret = pmic_get(name, &currdev); |
| 33 | if (ret) { |
| 34 | printf("Can't get PMIC: %s!\n", name); |
| 35 | return failure(ret); |
| 36 | } |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 37 | case 1: |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 38 | if (!currdev) { |
| 39 | printf("PMIC device is not set!\n\n"); |
| 40 | return CMD_RET_USAGE; |
| 41 | } |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 42 | |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 43 | printf("dev: %d @ %s\n", dev_seq(currdev), currdev->name); |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | return CMD_RET_SUCCESS; |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 49 | static int do_list(struct cmd_tbl *cmdtp, int flag, int argc, |
| 50 | char *const argv[]) |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 51 | { |
| 52 | struct udevice *dev; |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 53 | int ret, err = 0; |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 54 | |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 55 | printf("| %-*.*s| %-*.*s| %s @ %s\n", |
| 56 | LIMIT_DEV, LIMIT_DEV, "Name", |
| 57 | LIMIT_PARENT, LIMIT_PARENT, "Parent name", |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 58 | "Parent uclass", "seq"); |
| 59 | |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 60 | for (ret = uclass_first_device_check(UCLASS_PMIC, &dev); dev; |
| 61 | ret = uclass_next_device_check(&dev)) { |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 62 | if (ret) |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 63 | err = ret; |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 64 | |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 65 | printf("| %-*.*s| %-*.*s| %s @ %d | status: %i\n", |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 66 | LIMIT_DEV, LIMIT_DEV, dev->name, |
| 67 | LIMIT_PARENT, LIMIT_PARENT, dev->parent->name, |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 68 | dev_get_uclass_name(dev->parent), dev_seq(dev->parent), |
| 69 | ret); |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 72 | if (err) |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 73 | return CMD_RET_FAILURE; |
| 74 | |
| 75 | return CMD_RET_SUCCESS; |
| 76 | } |
| 77 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 78 | static int do_dump(struct cmd_tbl *cmdtp, int flag, int argc, |
| 79 | char *const argv[]) |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 80 | { |
Lukasz Majewski | d4505c8 | 2018-05-15 16:26:38 +0200 | [diff] [blame] | 81 | struct uc_pmic_priv *priv; |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 82 | struct udevice *dev; |
Lukasz Majewski | d4505c8 | 2018-05-15 16:26:38 +0200 | [diff] [blame] | 83 | char fmt[16]; |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 84 | uint reg; |
| 85 | int ret; |
| 86 | |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 87 | if (!currdev) { |
| 88 | printf("First, set the PMIC device!\n"); |
| 89 | return CMD_RET_USAGE; |
| 90 | } |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 91 | |
| 92 | dev = currdev; |
Lukasz Majewski | d4505c8 | 2018-05-15 16:26:38 +0200 | [diff] [blame] | 93 | priv = dev_get_uclass_priv(dev); |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 94 | printf("Dump pmic: %s registers\n", dev->name); |
| 95 | |
Lukasz Majewski | d4505c8 | 2018-05-15 16:26:38 +0200 | [diff] [blame] | 96 | sprintf(fmt, "%%%d.%dx ", priv->trans_len * 2, |
| 97 | priv->trans_len * 2); |
| 98 | |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 99 | for (reg = 0; reg < pmic_reg_count(dev); reg++) { |
Lukasz Majewski | d4505c8 | 2018-05-15 16:26:38 +0200 | [diff] [blame] | 100 | ret = pmic_reg_read(dev, reg); |
Martin Fuzzey | b852cca | 2020-01-14 15:56:18 +0000 | [diff] [blame] | 101 | if (ret < 0 && ret != -ENODATA) { |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 102 | printf("Can't read register: %d\n", reg); |
| 103 | return failure(ret); |
| 104 | } |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 105 | |
| 106 | if (!(reg % 16)) |
| 107 | printf("\n0x%02x: ", reg); |
| 108 | |
Martin Fuzzey | b852cca | 2020-01-14 15:56:18 +0000 | [diff] [blame] | 109 | if (ret == -ENODATA) { |
| 110 | int i; |
| 111 | |
| 112 | for (i = 0; i < priv->trans_len; i++) |
| 113 | puts("--"); |
| 114 | puts(" "); |
| 115 | } else { |
| 116 | printf(fmt, ret); |
| 117 | } |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 118 | } |
| 119 | printf("\n"); |
| 120 | |
| 121 | return CMD_RET_SUCCESS; |
| 122 | } |
| 123 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 124 | static int do_read(struct cmd_tbl *cmdtp, int flag, int argc, |
| 125 | char *const argv[]) |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 126 | { |
Lukasz Majewski | d4505c8 | 2018-05-15 16:26:38 +0200 | [diff] [blame] | 127 | struct uc_pmic_priv *priv; |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 128 | struct udevice *dev; |
| 129 | int regs, ret; |
Lukasz Majewski | d4505c8 | 2018-05-15 16:26:38 +0200 | [diff] [blame] | 130 | char fmt[24]; |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 131 | uint reg; |
| 132 | |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 133 | if (!currdev) { |
| 134 | printf("First, set the PMIC device!\n"); |
| 135 | return CMD_RET_USAGE; |
| 136 | } |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 137 | |
| 138 | dev = currdev; |
Lukasz Majewski | d4505c8 | 2018-05-15 16:26:38 +0200 | [diff] [blame] | 139 | priv = dev_get_uclass_priv(dev); |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 140 | |
| 141 | if (argc != 2) |
| 142 | return CMD_RET_USAGE; |
| 143 | |
| 144 | reg = simple_strtoul(argv[1], NULL, 0); |
| 145 | regs = pmic_reg_count(dev); |
| 146 | if (reg > regs) { |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 147 | printf("PMIC max reg: %d\n", regs); |
| 148 | return failure(-EFAULT); |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Lukasz Majewski | d4505c8 | 2018-05-15 16:26:38 +0200 | [diff] [blame] | 151 | ret = pmic_reg_read(dev, reg); |
| 152 | if (ret < 0) { |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 153 | printf("Can't read PMIC register: %d!\n", reg); |
| 154 | return failure(ret); |
| 155 | } |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 156 | |
Lukasz Majewski | d4505c8 | 2018-05-15 16:26:38 +0200 | [diff] [blame] | 157 | sprintf(fmt, "0x%%02x: 0x%%%d.%dx\n", priv->trans_len * 2, |
| 158 | priv->trans_len * 2); |
| 159 | printf(fmt, reg, ret); |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 160 | |
| 161 | return CMD_RET_SUCCESS; |
| 162 | } |
| 163 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 164 | static int do_write(struct cmd_tbl *cmdtp, int flag, int argc, |
| 165 | char *const argv[]) |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 166 | { |
| 167 | struct udevice *dev; |
Lukasz Majewski | d4505c8 | 2018-05-15 16:26:38 +0200 | [diff] [blame] | 168 | uint reg, value; |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 169 | int regs, ret; |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 170 | |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 171 | if (!currdev) { |
| 172 | printf("First, set the PMIC device!\n"); |
| 173 | return CMD_RET_USAGE; |
| 174 | } |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 175 | |
| 176 | dev = currdev; |
| 177 | |
| 178 | if (argc != 3) |
| 179 | return CMD_RET_USAGE; |
| 180 | |
| 181 | reg = simple_strtoul(argv[1], NULL, 0); |
| 182 | regs = pmic_reg_count(dev); |
| 183 | if (reg > regs) { |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 184 | printf("PMIC max reg: %d\n", regs); |
| 185 | return failure(-EFAULT); |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | value = simple_strtoul(argv[2], NULL, 0); |
| 189 | |
Lukasz Majewski | d4505c8 | 2018-05-15 16:26:38 +0200 | [diff] [blame] | 190 | ret = pmic_reg_write(dev, reg, value); |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 191 | if (ret) { |
| 192 | printf("Can't write PMIC register: %d!\n", reg); |
| 193 | return failure(ret); |
| 194 | } |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 195 | |
| 196 | return CMD_RET_SUCCESS; |
| 197 | } |
| 198 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 199 | static struct cmd_tbl subcmd[] = { |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 200 | U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""), |
| 201 | U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""), |
| 202 | U_BOOT_CMD_MKENT(dump, 1, 1, do_dump, "", ""), |
| 203 | U_BOOT_CMD_MKENT(read, 2, 1, do_read, "", ""), |
| 204 | U_BOOT_CMD_MKENT(write, 3, 1, do_write, "", ""), |
| 205 | }; |
| 206 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 207 | static int do_pmic(struct cmd_tbl *cmdtp, int flag, int argc, |
| 208 | char *const argv[]) |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 209 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 210 | struct cmd_tbl *cmd; |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 211 | |
| 212 | argc--; |
| 213 | argv++; |
| 214 | |
| 215 | cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd)); |
| 216 | if (cmd == NULL || argc > cmd->maxargs) |
| 217 | return CMD_RET_USAGE; |
| 218 | |
| 219 | return cmd->cmd(cmdtp, flag, argc, argv); |
| 220 | } |
| 221 | |
| 222 | U_BOOT_CMD(pmic, CONFIG_SYS_MAXARGS, 1, do_pmic, |
Klaus Goger | 7e37a88 | 2018-01-03 09:10:34 +0100 | [diff] [blame] | 223 | "PMIC sub-system", |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 224 | "list - list pmic devices\n" |
Przemyslaw Marczak | 493cdec | 2015-05-13 13:38:28 +0200 | [diff] [blame] | 225 | "pmic dev [name] - show or [set] operating PMIC device\n" |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 226 | "pmic dump - dump registers\n" |
Quentin Schulz | 8081561 | 2024-03-14 10:36:19 +0100 | [diff] [blame] | 227 | "pmic read <reg> - read byte of 'reg' register\n" |
| 228 | "pmic write <reg> <byte> - write 'byte' byte to 'reg' register\n" |
Przemyslaw Marczak | d89fdcf | 2015-04-20 20:07:43 +0200 | [diff] [blame] | 229 | ); |