cmd: pinmux: Add pinmux command
authorPatrice Chotard <patrice.chotard@st.com>
Wed, 24 Oct 2018 12:10:17 +0000 (14:10 +0200)
committerTom Rini <trini@konsulko.com>
Fri, 16 Nov 2018 21:51:56 +0000 (16:51 -0500)
pinmux command allows to :
 - list all pin-controllers available on platforms
 - select a pin-controller
 - display the muxing of all pins of the current pin-controller
   or all pin-controllers depending of given options

Signed-off-by: Patrice Chotard <patrice.chotard@st.com>
cmd: pinmux: Fix pinmux command

if "pinmux status" command is used without having
set dev using "pinmux dev", print pinmux usage
Reviewed-by: Simon Glass <sjg@chromium.org>
cmd/Kconfig
cmd/Makefile
cmd/pinmux.c [new file with mode: 0644]
include/dm/pinctrl.h

index d609f9d1c96078de90a195253751a63c0b1853b0..e2973b3c514e48707936260f2bbe506568d042e0 100644 (file)
@@ -953,6 +953,14 @@ config CMD_PCMCIA
          about 1990. These devices are typically removable memory or network
          cards using a standard 68-pin connector.
 
+config CMD_PINMUX
+       bool "pinmux - show pins muxing"
+       default y if PINCTRL
+       help
+         Parse all available pin-controllers and show pins muxing. This
+         is useful for debug purpoer to check the pin muxing and to know if
+         a pin is configured as a GPIO or as an alternate function.
+
 config CMD_POWEROFF
        bool "poweroff"
        help
index 12a1330b0629fd9e151510340101e2b5f363bf72..0534ddc67975d40afcccd2c148b87858ac94ec59 100644 (file)
@@ -103,6 +103,7 @@ ifdef CONFIG_PCI
 obj-$(CONFIG_CMD_PCI) += pci.o
 endif
 obj-y += pcmcia.o
+obj-$(CONFIG_CMD_PINMUX) += pinmux.o
 obj-$(CONFIG_CMD_PXE) += pxe.o
 obj-$(CONFIG_CMD_WOL) += wol.o
 obj-$(CONFIG_CMD_QFW) += qfw.o
diff --git a/cmd/pinmux.c b/cmd/pinmux.c
new file mode 100644 (file)
index 0000000..6c8ec51
--- /dev/null
@@ -0,0 +1,146 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
+ */
+
+#include <common.h>
+#include <command.h>
+#include <dm.h>
+#include <errno.h>
+#include <dm/pinctrl.h>
+#include <dm/uclass-internal.h>
+
+#define LIMIT_DEVNAME  30
+
+static struct udevice *currdev;
+
+static int do_dev(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+       const char *name;
+       int ret;
+
+       switch (argc) {
+       case 2:
+               name = argv[1];
+               ret = uclass_get_device_by_name(UCLASS_PINCTRL, name, &currdev);
+               if (ret) {
+                       printf("Can't get the pin-controller: %s!\n", name);
+                       return CMD_RET_FAILURE;
+               }
+       case 1:
+               if (!currdev) {
+                       printf("Pin-controller device is not set!\n");
+                       return CMD_RET_USAGE;
+               }
+
+               printf("dev: %s\n", currdev->name);
+       }
+
+       return CMD_RET_SUCCESS;
+}
+
+static int show_pinmux(struct udevice *dev)
+{
+       char pin_name[PINNAME_SIZE];
+       char pin_mux[PINMUX_SIZE];
+       int pins_count;
+       int i;
+       int ret;
+
+       pins_count = pinctrl_get_pins_count(dev);
+
+       if (pins_count == -ENOSYS) {
+               printf("Ops get_pins_count not supported\n");
+               return pins_count;
+       }
+
+       for (i = 0; i < pins_count; i++) {
+               ret = pinctrl_get_pin_name(dev, i, pin_name, PINNAME_SIZE);
+               if (ret == -ENOSYS) {
+                       printf("Ops get_pin_name not supported\n");
+                       return ret;
+               }
+
+               ret = pinctrl_get_pin_muxing(dev, i, pin_mux, PINMUX_SIZE);
+               if (ret) {
+                       printf("Ops get_pin_muxing error (%d)\n", ret);
+                       return ret;
+               }
+
+               printf("%-*s: %-*s\n", PINNAME_SIZE, pin_name,
+                      PINMUX_SIZE, pin_mux);
+       }
+
+       return 0;
+}
+
+static int do_status(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+       struct udevice *dev;
+       int ret = CMD_RET_USAGE;
+
+       if (currdev && (argc < 2 || strcmp(argv[1], "-a")))
+               return show_pinmux(currdev);
+
+       if (argc < 2 || strcmp(argv[1], "-a"))
+               return ret;
+
+       uclass_foreach_dev_probe(UCLASS_PINCTRL, dev) {
+               /* insert a separator between each pin-controller display */
+               printf("--------------------------\n");
+               printf("%s:\n", dev->name);
+               ret = show_pinmux(dev);
+               if (ret < 0)
+                       printf("Can't display pin muxing for %s\n",
+                              dev->name);
+       }
+
+       return ret;
+}
+
+static int do_list(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+       struct udevice *dev;
+
+       printf("| %-*.*s| %-*.*s| %s\n",
+              LIMIT_DEVNAME, LIMIT_DEVNAME, "Device",
+              LIMIT_DEVNAME, LIMIT_DEVNAME, "Driver",
+              "Parent");
+
+       uclass_foreach_dev_probe(UCLASS_PINCTRL, dev) {
+               printf("| %-*.*s| %-*.*s| %s\n",
+                      LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name,
+                      LIMIT_DEVNAME, LIMIT_DEVNAME, dev->driver->name,
+                      dev->parent->name);
+       }
+
+       return CMD_RET_SUCCESS;
+}
+
+static cmd_tbl_t pinmux_subcmd[] = {
+       U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""),
+       U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""),
+       U_BOOT_CMD_MKENT(status, 2, 1, do_status, "", ""),
+};
+
+static int do_pinmux(cmd_tbl_t *cmdtp, int flag, int argc,
+                    char * const argv[])
+{
+       cmd_tbl_t *cmd;
+
+       argc--;
+       argv++;
+
+       cmd = find_cmd_tbl(argv[0], pinmux_subcmd, ARRAY_SIZE(pinmux_subcmd));
+       if (!cmd || argc > cmd->maxargs)
+               return CMD_RET_USAGE;
+
+       return cmd->cmd(cmdtp, flag, argc, argv);
+}
+
+U_BOOT_CMD(pinmux, CONFIG_SYS_MAXARGS, 1, do_pinmux,
+          "show pin-controller muxing",
+          "list                     - list UCLASS_PINCTRL devices\n"
+          "pinmux dev [pincontroller-name] - select pin-controller device\n"
+          "pinmux status [-a]              - print pin-controller muxing [for all]\n"
+)
index 5a7c5f102e67e2ff166d506cc4cd04e3eb7d123e..63a7d55b88808c428c71ff243f5850262af30121 100644 (file)
@@ -6,6 +6,9 @@
 #ifndef __PINCTRL_H
 #define __PINCTRL_H
 
+#define PINNAME_SIZE   10
+#define PINMUX_SIZE    40
+
 /**
  * struct pinconf_param - pin config parameters
  *