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(struct cmd_tbl *cmdtp, int flag, int argc,
26 ret = uclass_get_device_by_name(UCLASS_PINCTRL, name, &currdev);
28 printf("Can't get the pin-controller: %s!\n", name);
29 return CMD_RET_FAILURE;
34 printf("Pin-controller device is not set!\n");
38 printf("dev: %s\n", currdev->name);
41 return CMD_RET_SUCCESS;
45 * Print the muxing information for one or all pins of one pinctrl device
47 * @param dev pinctrl device
48 * @param name NULL to display all the pins
49 * or name of the pin to display
50 * @return 0 on success, non-0 on error
52 static int show_pinmux(struct udevice *dev, char *name)
54 char pin_name[PINNAME_SIZE];
55 char pin_mux[PINMUX_SIZE];
61 pins_count = pinctrl_get_pins_count(dev);
63 if (pins_count == -ENOSYS) {
64 printf("Ops get_pins_count not supported by %s\n", dev->name);
68 for (i = 0; i < pins_count; i++) {
69 ret = pinctrl_get_pin_name(dev, i, pin_name, PINNAME_SIZE);
71 printf("Ops get_pin_name error (%d) by %s\n", ret, dev->name);
74 if (name && strcmp(name, pin_name))
77 ret = pinctrl_get_pin_muxing(dev, i, pin_mux, PINMUX_SIZE);
79 printf("Ops get_pin_muxing error (%d) by %s in %s\n",
80 ret, pin_name, dev->name);
84 printf("%-*s: %-*s\n", PINNAME_SIZE, pin_name,
85 PINMUX_SIZE, pin_mux);
94 static int do_status(struct cmd_tbl *cmdtp, int flag, int argc,
103 printf("pin-controller device not selected\n");
104 return CMD_RET_FAILURE;
106 show_pinmux(currdev, NULL);
107 return CMD_RET_SUCCESS;
110 if (strcmp(argv[1], "-a"))
115 uclass_foreach_dev_probe(UCLASS_PINCTRL, dev) {
117 /* insert a separator between each pin-controller display */
118 printf("--------------------------\n");
119 printf("%s:\n", dev->name);
121 ret = show_pinmux(dev, name);
122 /* stop when the status of requested pin is displayed */
124 return CMD_RET_SUCCESS;
128 printf("%s not found\n", name);
129 return CMD_RET_FAILURE;
132 return CMD_RET_SUCCESS;
135 static int do_list(struct cmd_tbl *cmdtp, int flag, int argc,
140 printf("| %-*.*s| %-*.*s| %s\n",
141 LIMIT_DEVNAME, LIMIT_DEVNAME, "Device",
142 LIMIT_DEVNAME, LIMIT_DEVNAME, "Driver",
145 uclass_foreach_dev_probe(UCLASS_PINCTRL, dev) {
146 printf("| %-*.*s| %-*.*s| %s\n",
147 LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name,
148 LIMIT_DEVNAME, LIMIT_DEVNAME, dev->driver->name,
152 return CMD_RET_SUCCESS;
155 static struct cmd_tbl pinmux_subcmd[] = {
156 U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""),
157 U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""),
158 U_BOOT_CMD_MKENT(status, 2, 1, do_status, "", ""),
161 static int do_pinmux(struct cmd_tbl *cmdtp, int flag, int argc,
169 cmd = find_cmd_tbl(argv[0], pinmux_subcmd, ARRAY_SIZE(pinmux_subcmd));
170 if (!cmd || argc > cmd->maxargs)
171 return CMD_RET_USAGE;
173 return cmd->cmd(cmdtp, flag, argc, argv);
176 U_BOOT_CMD(pinmux, CONFIG_SYS_MAXARGS, 1, do_pinmux,
177 "show pin-controller muxing",
178 "list - list UCLASS_PINCTRL devices\n"
179 "pinmux dev [pincontroller-name] - select pin-controller device\n"
180 "pinmux status [-a | pin-name] - print pin-controller muxing [for all | for pin-name]\n"