1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014-2015 Samsung Electronics
4 * Przemyslaw Marczak <p.marczak@samsung.com>
9 #include <dm/uclass-internal.h>
10 #include <power/pmic.h>
13 #define LIMIT_PARENT 20
15 static struct udevice *currdev;
17 static int failure(int ret)
19 printf("Error: %d (%s)\n", ret, errno_str(ret));
21 return CMD_RET_FAILURE;
24 static int do_dev(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
32 ret = pmic_get(name, &currdev);
34 printf("Can't get PMIC: %s!\n", name);
39 printf("PMIC device is not set!\n\n");
43 printf("dev: %d @ %s\n", currdev->seq, currdev->name);
46 return CMD_RET_SUCCESS;
49 static int do_list(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
54 printf("| %-*.*s| %-*.*s| %s @ %s\n",
55 LIMIT_DEV, LIMIT_DEV, "Name",
56 LIMIT_PARENT, LIMIT_PARENT, "Parent name",
57 "Parent uclass", "seq");
59 for (ret = uclass_first_device(UCLASS_PMIC, &dev); dev;
60 ret = uclass_next_device(&dev)) {
64 printf("| %-*.*s| %-*.*s| %s @ %d\n",
65 LIMIT_DEV, LIMIT_DEV, dev->name,
66 LIMIT_PARENT, LIMIT_PARENT, dev->parent->name,
67 dev_get_uclass_name(dev->parent), dev->parent->seq);
71 return CMD_RET_FAILURE;
73 return CMD_RET_SUCCESS;
76 static int do_dump(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
78 struct uc_pmic_priv *priv;
85 printf("First, set the PMIC device!\n");
90 priv = dev_get_uclass_priv(dev);
91 printf("Dump pmic: %s registers\n", dev->name);
93 sprintf(fmt, "%%%d.%dx ", priv->trans_len * 2,
96 for (reg = 0; reg < pmic_reg_count(dev); reg++) {
97 ret = pmic_reg_read(dev, reg);
98 if (ret < 0 && ret != -ENODATA) {
99 printf("Can't read register: %d\n", reg);
104 printf("\n0x%02x: ", reg);
106 if (ret == -ENODATA) {
109 for (i = 0; i < priv->trans_len; i++)
118 return CMD_RET_SUCCESS;
121 static int do_read(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
123 struct uc_pmic_priv *priv;
130 printf("First, set the PMIC device!\n");
131 return CMD_RET_USAGE;
135 priv = dev_get_uclass_priv(dev);
138 return CMD_RET_USAGE;
140 reg = simple_strtoul(argv[1], NULL, 0);
141 regs = pmic_reg_count(dev);
143 printf("PMIC max reg: %d\n", regs);
144 return failure(-EFAULT);
147 ret = pmic_reg_read(dev, reg);
149 printf("Can't read PMIC register: %d!\n", reg);
153 sprintf(fmt, "0x%%02x: 0x%%%d.%dx\n", priv->trans_len * 2,
154 priv->trans_len * 2);
155 printf(fmt, reg, ret);
157 return CMD_RET_SUCCESS;
160 static int do_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
167 printf("First, set the PMIC device!\n");
168 return CMD_RET_USAGE;
174 return CMD_RET_USAGE;
176 reg = simple_strtoul(argv[1], NULL, 0);
177 regs = pmic_reg_count(dev);
179 printf("PMIC max reg: %d\n", regs);
180 return failure(-EFAULT);
183 value = simple_strtoul(argv[2], NULL, 0);
185 ret = pmic_reg_write(dev, reg, value);
187 printf("Can't write PMIC register: %d!\n", reg);
191 return CMD_RET_SUCCESS;
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, "", ""),
202 static int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc,
210 cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd));
211 if (cmd == NULL || argc > cmd->maxargs)
212 return CMD_RET_USAGE;
214 return cmd->cmd(cmdtp, flag, argc, argv);
217 U_BOOT_CMD(pmic, CONFIG_SYS_MAXARGS, 1, do_pmic,
219 "list - list pmic devices\n"
220 "pmic dev [name] - show or [set] operating PMIC device\n"
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"