common: cmd pmic: command cleanup
authorPrzemyslaw Marczak <p.marczak@samsung.com>
Wed, 13 May 2015 11:38:28 +0000 (13:38 +0200)
committerSimon Glass <sjg@chromium.org>
Fri, 15 May 2015 01:58:34 +0000 (19:58 -0600)
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>
common/cmd_pmic.c

index bd88d687cbed65889d3908e6509904d980e0f5ef..970767cdfa768d581f5b2c42e172ee86fd000c1e 100644 (file)
 #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[])
@@ -86,18 +52,19 @@ 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);
        }
 
@@ -114,8 +81,10 @@ static int do_dump(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
        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;
 
@@ -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++) {
                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);
@@ -143,8 +114,10 @@ static int do_read(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
        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;
 
@@ -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);
        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);
 
@@ -174,8 +149,10 @@ static int do_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
        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;
 
@@ -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);
        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;
 }
@@ -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,
        " 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"