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;
33 printf("Pin-controller device is not set!\n");
37 printf("dev: %s\n", currdev->name);
40 return CMD_RET_SUCCESS;
43 static int show_pinmux(struct udevice *dev)
45 char pin_name[PINNAME_SIZE];
46 char pin_mux[PINMUX_SIZE];
51 pins_count = pinctrl_get_pins_count(dev);
53 if (pins_count == -ENOSYS) {
54 printf("Ops get_pins_count not supported\n");
58 for (i = 0; i < pins_count; i++) {
59 ret = pinctrl_get_pin_name(dev, i, pin_name, PINNAME_SIZE);
61 printf("Ops get_pin_name not supported\n");
65 ret = pinctrl_get_pin_muxing(dev, i, pin_mux, PINMUX_SIZE);
67 printf("Ops get_pin_muxing error (%d)\n", ret);
71 printf("%-*s: %-*s\n", PINNAME_SIZE, pin_name,
72 PINMUX_SIZE, pin_mux);
78 static int do_status(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
81 int ret = CMD_RET_USAGE;
83 if (currdev && (argc < 2 || strcmp(argv[1], "-a")))
84 return show_pinmux(currdev);
86 if (argc < 2 || strcmp(argv[1], "-a"))
89 uclass_foreach_dev_probe(UCLASS_PINCTRL, dev) {
90 /* insert a separator between each pin-controller display */
91 printf("--------------------------\n");
92 printf("%s:\n", dev->name);
93 ret = show_pinmux(dev);
95 printf("Can't display pin muxing for %s\n",
102 static int do_list(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
106 printf("| %-*.*s| %-*.*s| %s\n",
107 LIMIT_DEVNAME, LIMIT_DEVNAME, "Device",
108 LIMIT_DEVNAME, LIMIT_DEVNAME, "Driver",
111 uclass_foreach_dev_probe(UCLASS_PINCTRL, dev) {
112 printf("| %-*.*s| %-*.*s| %s\n",
113 LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name,
114 LIMIT_DEVNAME, LIMIT_DEVNAME, dev->driver->name,
118 return CMD_RET_SUCCESS;
121 static cmd_tbl_t pinmux_subcmd[] = {
122 U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""),
123 U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""),
124 U_BOOT_CMD_MKENT(status, 2, 1, do_status, "", ""),
127 static int do_pinmux(cmd_tbl_t *cmdtp, int flag, int argc,
135 cmd = find_cmd_tbl(argv[0], pinmux_subcmd, ARRAY_SIZE(pinmux_subcmd));
136 if (!cmd || argc > cmd->maxargs)
137 return CMD_RET_USAGE;
139 return cmd->cmd(cmdtp, flag, argc, argv);
142 U_BOOT_CMD(pinmux, CONFIG_SYS_MAXARGS, 1, do_pinmux,
143 "show pin-controller muxing",
144 "list - list UCLASS_PINCTRL devices\n"
145 "pinmux dev [pincontroller-name] - select pin-controller device\n"
146 "pinmux status [-a] - print pin-controller muxing [for all]\n"