#include <dm/uclass-internal.h>
#include <power/pmic.h>
-#define LIMIT_SEQ 3
-#define LIMIT_DEVNAME 20
+#define LIMIT_DEV 32
+#define LIMIT_PARENT 20
static struct udevice *currdev;
-static int failed(const char *getset, const char *thing,
- const char *for_dev, int ret)
+static int failure(int ret)
{
- printf("Can't %s %s %s.\nError: %d (%s)\n", getset, thing, for_dev,
- ret, errno_str(ret));
- return CMD_RET_FAILURE;
-}
-
-static int pmic_dev_get(bool list_only, int get_seq, struct udevice **devp)
-{
- struct udevice *dev;
- int ret;
-
- if (devp)
- *devp = NULL;
-
- for (ret = uclass_first_device(UCLASS_PMIC, &dev); dev;
- ret = uclass_next_device(&dev)) {
- if (list_only) {
- printf("|%*d | %-*.*s| %-*.*s| %s @ %d\n",
- LIMIT_SEQ, dev->seq,
- LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name,
- LIMIT_DEVNAME, LIMIT_DEVNAME, dev->parent->name,
- dev_get_uclass_name(dev->parent),
- dev->parent->seq);
- continue;
- }
+ printf("Error: %d (%s)\n", ret, errno_str(ret));
- if (dev->seq == get_seq) {
- if (devp)
- *devp = dev;
- else
- return -EINVAL;
-
- return 0;
- }
- }
-
- if (list_only)
- return ret;
-
- return -ENODEV;
+ return CMD_RET_FAILURE;
}
static int do_dev(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
- int seq, ret = -ENODEV;
+ char *name;
+ int ret = -ENODEV;
switch (argc) {
case 2:
- seq = simple_strtoul(argv[1], NULL, 0);
- ret = uclass_get_device_by_seq(UCLASS_PMIC, seq, &currdev);
- if (ret && (ret = pmic_dev_get(false, seq, &currdev)))
- goto failed;
+ name = argv[1];
+ ret = pmic_get(name, &currdev);
+ if (ret) {
+ printf("Can't get PMIC: %s!\n", name);
+ return failure(ret);
+ }
case 1:
- if (!currdev)
- goto failed;
+ if (!currdev) {
+ printf("PMIC device is not set!\n\n");
+ return CMD_RET_USAGE;
+ }
printf("dev: %d @ %s\n", currdev->seq, currdev->name);
}
return CMD_RET_SUCCESS;
-failed:
- return failed("get", "the", "device", ret);
}
static int do_list(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
struct udevice *dev;
int ret;
- printf("|%*s | %-*.*s| %-*.*s| %s @ %s\n",
- LIMIT_SEQ, "Seq",
- LIMIT_DEVNAME, LIMIT_DEVNAME, "Name",
- LIMIT_DEVNAME, LIMIT_DEVNAME, "Parent name",
+ printf("| %-*.*s| %-*.*s| %s @ %s\n",
+ LIMIT_DEV, LIMIT_DEV, "Name",
+ LIMIT_PARENT, LIMIT_PARENT, "Parent name",
"Parent uclass", "seq");
for (ret = uclass_first_device(UCLASS_PMIC, &dev); dev;
ret = uclass_next_device(&dev)) {
- printf("|%*d | %-*.*s| %-*.*s| %s @ %d\n",
- LIMIT_SEQ, dev->seq,
- LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name,
- LIMIT_DEVNAME, LIMIT_DEVNAME, dev->parent->name,
+ if (ret)
+ continue;
+
+ printf("| %-*.*s| %-*.*s| %s @ %d\n",
+ LIMIT_DEV, LIMIT_DEV, dev->name,
+ LIMIT_PARENT, LIMIT_PARENT, dev->parent->name,
dev_get_uclass_name(dev->parent), dev->parent->seq);
}
uint reg;
int ret;
- if (!currdev)
- return failed("get", "current", "device", -ENODEV);
+ if (!currdev) {
+ printf("First, set the PMIC device!\n");
+ return CMD_RET_USAGE;
+ }
dev = currdev;
for (reg = 0; reg < pmic_reg_count(dev); reg++) {
ret = pmic_read(dev, reg, &value, 1);
- if (ret)
- return failed("read", dev->name, "register", ret);
+ if (ret) {
+ printf("Can't read register: %d\n", reg);
+ return failure(ret);
+ }
if (!(reg % 16))
printf("\n0x%02x: ", reg);
uint8_t value;
uint reg;
- if (!currdev)
- return failed("get", "current", "device", -ENODEV);
+ if (!currdev) {
+ printf("First, set the PMIC device!\n");
+ return CMD_RET_USAGE;
+ }
dev = currdev;
reg = simple_strtoul(argv[1], NULL, 0);
regs = pmic_reg_count(dev);
if (reg > regs) {
- printf("Pmic max reg: %d\n", regs);
- return failed("read", "given", "address", -EFAULT);
+ printf("PMIC max reg: %d\n", regs);
+ return failure(-EFAULT);
}
ret = pmic_read(dev, reg, &value, 1);
- if (ret)
- return failed("read", dev->name, "register", ret);
+ if (ret) {
+ printf("Can't read PMIC register: %d!\n", reg);
+ return failure(ret);
+ }
printf("0x%02x: 0x%2.2x\n", reg, value);
uint8_t value;
uint reg;
- if (!currdev)
- return failed("get", "current", "device", -ENODEV);
+ if (!currdev) {
+ printf("First, set the PMIC device!\n");
+ return CMD_RET_USAGE;
+ }
dev = currdev;
reg = simple_strtoul(argv[1], NULL, 0);
regs = pmic_reg_count(dev);
if (reg > regs) {
- printf("Pmic max reg: %d\n", regs);
- return failed("write", "given", "address", -EFAULT);
+ printf("PMIC max reg: %d\n", regs);
+ return failure(-EFAULT);
}
value = simple_strtoul(argv[2], NULL, 0);
ret = pmic_write(dev, reg, &value, 1);
- if (ret)
- return failed("write", dev->name, "register", ret);
+ if (ret) {
+ printf("Can't write PMIC register: %d!\n", reg);
+ return failure(ret);
+ }
return CMD_RET_SUCCESS;
}
U_BOOT_CMD(pmic, CONFIG_SYS_MAXARGS, 1, do_pmic,
" operations",
"list - list pmic devices\n"
- "pmic dev [id] - show or [set] operating pmic device\n"
+ "pmic dev [name] - show or [set] operating PMIC device\n"
"pmic dump - dump registers\n"
"pmic read address - read byte of register at address\n"
"pmic write address - write byte to register at address\n"