1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014-2015 Samsung Electronics
4 * Przemyslaw Marczak <p.marczak@samsung.com>
10 #include <dm/uclass-internal.h>
11 #include <power/pmic.h>
14 #define LIMIT_PARENT 20
16 static struct udevice *currdev;
18 static int failure(int ret)
20 printf("Error: %d (%s)\n", ret, errno_str(ret));
22 return CMD_RET_FAILURE;
25 static int do_dev(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
33 ret = pmic_get(name, &currdev);
35 printf("Can't get PMIC: %s!\n", name);
40 printf("PMIC device is not set!\n\n");
44 printf("dev: %d @ %s\n", dev_seq(currdev), currdev->name);
47 return CMD_RET_SUCCESS;
50 static int do_list(struct cmd_tbl *cmdtp, int flag, int argc,
56 printf("| %-*.*s| %-*.*s| %s @ %s\n",
57 LIMIT_DEV, LIMIT_DEV, "Name",
58 LIMIT_PARENT, LIMIT_PARENT, "Parent name",
59 "Parent uclass", "seq");
61 for (ret = uclass_first_device(UCLASS_PMIC, &dev); dev;
62 ret = uclass_next_device(&dev)) {
66 printf("| %-*.*s| %-*.*s| %s @ %d\n",
67 LIMIT_DEV, LIMIT_DEV, dev->name,
68 LIMIT_PARENT, LIMIT_PARENT, dev->parent->name,
69 dev_get_uclass_name(dev->parent), dev_seq(dev->parent));
73 return CMD_RET_FAILURE;
75 return CMD_RET_SUCCESS;
78 static int do_dump(struct cmd_tbl *cmdtp, int flag, int argc,
81 struct uc_pmic_priv *priv;
88 printf("First, set the PMIC device!\n");
93 priv = dev_get_uclass_priv(dev);
94 printf("Dump pmic: %s registers\n", dev->name);
96 sprintf(fmt, "%%%d.%dx ", priv->trans_len * 2,
99 for (reg = 0; reg < pmic_reg_count(dev); reg++) {
100 ret = pmic_reg_read(dev, reg);
101 if (ret < 0 && ret != -ENODATA) {
102 printf("Can't read register: %d\n", reg);
107 printf("\n0x%02x: ", reg);
109 if (ret == -ENODATA) {
112 for (i = 0; i < priv->trans_len; i++)
121 return CMD_RET_SUCCESS;
124 static int do_read(struct cmd_tbl *cmdtp, int flag, int argc,
127 struct uc_pmic_priv *priv;
134 printf("First, set the PMIC device!\n");
135 return CMD_RET_USAGE;
139 priv = dev_get_uclass_priv(dev);
142 return CMD_RET_USAGE;
144 reg = simple_strtoul(argv[1], NULL, 0);
145 regs = pmic_reg_count(dev);
147 printf("PMIC max reg: %d\n", regs);
148 return failure(-EFAULT);
151 ret = pmic_reg_read(dev, reg);
153 printf("Can't read PMIC register: %d!\n", reg);
157 sprintf(fmt, "0x%%02x: 0x%%%d.%dx\n", priv->trans_len * 2,
158 priv->trans_len * 2);
159 printf(fmt, reg, ret);
161 return CMD_RET_SUCCESS;
164 static int do_write(struct cmd_tbl *cmdtp, int flag, int argc,
172 printf("First, set the PMIC device!\n");
173 return CMD_RET_USAGE;
179 return CMD_RET_USAGE;
181 reg = simple_strtoul(argv[1], NULL, 0);
182 regs = pmic_reg_count(dev);
184 printf("PMIC max reg: %d\n", regs);
185 return failure(-EFAULT);
188 value = simple_strtoul(argv[2], NULL, 0);
190 ret = pmic_reg_write(dev, reg, value);
192 printf("Can't write PMIC register: %d!\n", reg);
196 return CMD_RET_SUCCESS;
199 static struct cmd_tbl subcmd[] = {
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, "", ""),
207 static int do_pmic(struct cmd_tbl *cmdtp, int flag, int argc,
215 cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd));
216 if (cmd == NULL || argc > cmd->maxargs)
217 return CMD_RET_USAGE;
219 return cmd->cmd(cmdtp, flag, argc, argv);
222 U_BOOT_CMD(pmic, CONFIG_SYS_MAXARGS, 1, do_pmic,
224 "list - list pmic devices\n"
225 "pmic dev [name] - show or [set] operating PMIC device\n"
226 "pmic dump - dump registers\n"
227 "pmic read address - read byte of register at address\n"
228 "pmic write address - write byte to register at address\n"