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