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