1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014-2015 Samsung Electronics
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_check(UCLASS_PMIC, &dev); dev;
62 ret = uclass_next_device_check(&dev)) {
66 printf("| %-*.*s| %-*.*s| %s @ %d | status: %i\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),
74 return CMD_RET_FAILURE;
76 return CMD_RET_SUCCESS;
79 static int do_dump(struct cmd_tbl *cmdtp, int flag, int argc,
82 struct uc_pmic_priv *priv;
89 printf("First, set the PMIC device!\n");
94 priv = dev_get_uclass_priv(dev);
95 printf("Dump pmic: %s registers\n", dev->name);
97 sprintf(fmt, "%%%d.%dx ", priv->trans_len * 2,
100 for (reg = 0; reg < pmic_reg_count(dev); reg++) {
101 ret = pmic_reg_read(dev, reg);
102 if (ret < 0 && ret != -ENODATA) {
103 printf("Can't read register: %d\n", reg);
108 printf("\n0x%02x: ", reg);
110 if (ret == -ENODATA) {
113 for (i = 0; i < priv->trans_len; i++)
122 return CMD_RET_SUCCESS;
125 static int do_read(struct cmd_tbl *cmdtp, int flag, int argc,
128 struct uc_pmic_priv *priv;
135 printf("First, set the PMIC device!\n");
136 return CMD_RET_USAGE;
140 priv = dev_get_uclass_priv(dev);
143 return CMD_RET_USAGE;
145 reg = simple_strtoul(argv[1], NULL, 0);
146 regs = pmic_reg_count(dev);
148 printf("PMIC max reg: %d\n", regs);
149 return failure(-EFAULT);
152 ret = pmic_reg_read(dev, reg);
154 printf("Can't read PMIC register: %d!\n", reg);
158 sprintf(fmt, "0x%%02x: 0x%%%d.%dx\n", priv->trans_len * 2,
159 priv->trans_len * 2);
160 printf(fmt, reg, ret);
162 return CMD_RET_SUCCESS;
165 static int do_write(struct cmd_tbl *cmdtp, int flag, int argc,
173 printf("First, set the PMIC device!\n");
174 return CMD_RET_USAGE;
180 return CMD_RET_USAGE;
182 reg = simple_strtoul(argv[1], NULL, 0);
183 regs = pmic_reg_count(dev);
185 printf("PMIC max reg: %d\n", regs);
186 return failure(-EFAULT);
189 value = simple_strtoul(argv[2], NULL, 0);
191 ret = pmic_reg_write(dev, reg, value);
193 printf("Can't write PMIC register: %d!\n", reg);
197 return CMD_RET_SUCCESS;
200 static struct cmd_tbl subcmd[] = {
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, "", ""),
208 static int do_pmic(struct cmd_tbl *cmdtp, int flag, int argc,
216 cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd));
217 if (cmd == NULL || argc > cmd->maxargs)
218 return CMD_RET_USAGE;
220 return cmd->cmd(cmdtp, flag, argc, argv);
223 U_BOOT_CMD(pmic, CONFIG_SYS_MAXARGS, 1, do_pmic,
225 "list - list pmic devices\n"
226 "pmic dev [name] - show or [set] operating PMIC device\n"
227 "pmic dump - dump registers\n"
228 "pmic read <reg> - read byte of 'reg' register\n"
229 "pmic write <reg> <byte> - write 'byte' byte to 'reg' register\n"