1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
10 #include <dm/pinctrl.h>
11 #include <dm/uclass-internal.h>
13 #define LIMIT_DEVNAME 30
15 static struct udevice *currdev;
17 static int do_dev(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
25 ret = uclass_get_device_by_name(UCLASS_PINCTRL, name, &currdev);
27 printf("Can't get the pin-controller: %s!\n", name);
28 return CMD_RET_FAILURE;
32 printf("Pin-controller device is not set!\n");
36 printf("dev: %s\n", currdev->name);
39 return CMD_RET_SUCCESS;
42 static int show_pinmux(struct udevice *dev)
44 char pin_name[PINNAME_SIZE];
45 char pin_mux[PINMUX_SIZE];
50 pins_count = pinctrl_get_pins_count(dev);
52 if (pins_count == -ENOSYS) {
53 printf("Ops get_pins_count not supported\n");
57 for (i = 0; i < pins_count; i++) {
58 ret = pinctrl_get_pin_name(dev, i, pin_name, PINNAME_SIZE);
60 printf("Ops get_pin_name not supported\n");
64 ret = pinctrl_get_pin_muxing(dev, i, pin_mux, PINMUX_SIZE);
66 printf("Ops get_pin_muxing error (%d)\n", ret);
70 printf("%-*s: %-*s\n", PINNAME_SIZE, pin_name,
71 PINMUX_SIZE, pin_mux);
77 static int do_status(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
80 int ret = CMD_RET_USAGE;
82 if (currdev && (argc < 2 || strcmp(argv[1], "-a")))
83 return show_pinmux(currdev);
85 if (argc < 2 || strcmp(argv[1], "-a"))
88 uclass_foreach_dev_probe(UCLASS_PINCTRL, dev) {
89 /* insert a separator between each pin-controller display */
90 printf("--------------------------\n");
91 printf("%s:\n", dev->name);
92 ret = show_pinmux(dev);
94 printf("Can't display pin muxing for %s\n",
101 static int do_list(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
105 printf("| %-*.*s| %-*.*s| %s\n",
106 LIMIT_DEVNAME, LIMIT_DEVNAME, "Device",
107 LIMIT_DEVNAME, LIMIT_DEVNAME, "Driver",
110 uclass_foreach_dev_probe(UCLASS_PINCTRL, dev) {
111 printf("| %-*.*s| %-*.*s| %s\n",
112 LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name,
113 LIMIT_DEVNAME, LIMIT_DEVNAME, dev->driver->name,
117 return CMD_RET_SUCCESS;
120 static cmd_tbl_t pinmux_subcmd[] = {
121 U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""),
122 U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""),
123 U_BOOT_CMD_MKENT(status, 2, 1, do_status, "", ""),
126 static int do_pinmux(cmd_tbl_t *cmdtp, int flag, int argc,
134 cmd = find_cmd_tbl(argv[0], pinmux_subcmd, ARRAY_SIZE(pinmux_subcmd));
135 if (!cmd || argc > cmd->maxargs)
136 return CMD_RET_USAGE;
138 return cmd->cmd(cmdtp, flag, argc, argv);
141 U_BOOT_CMD(pinmux, CONFIG_SYS_MAXARGS, 1, do_pinmux,
142 "show pin-controller muxing",
143 "list - list UCLASS_PINCTRL devices\n"
144 "pinmux dev [pincontroller-name] - select pin-controller device\n"
145 "pinmux status [-a] - print pin-controller muxing [for all]\n"