1
0
mirror of https://xff.cz/git/u-boot/ synced 2025-09-03 17:52:07 +02:00

common: cmd pmic: command cleanup

This commit cleanups the use of function: failed().
The new function name is: failure(), and it is used
for print errno and the errno-related message only.

The second change is choosing PMIC device by it's name,
instead of seq number. Thanks to this change, for set
the current device, call of pmic_get() is enough.

Signed-off-by: Przemyslaw Marczak <p.marczak@samsung.com>
Acked-by: Simon Glass <sjg@chromium.org>
Tested on sandbox:
Tested-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Przemyslaw Marczak
2015-05-13 13:38:28 +02:00
committed by Simon Glass
parent 3b880757ab
commit 493cdec7ac

View File

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