1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
9 #include <dm/pinctrl.h>
10 #include <dm/uclass-internal.h>
12 #define LIMIT_DEVNAME 30
14 static struct udevice *currdev;
16 static int do_dev(struct cmd_tbl *cmdtp, int flag, int argc,
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;
44 * Print the muxing information for one or all pins of one pinctrl device
46 * @param dev pinctrl device
47 * @param name NULL to display all the pins
48 * or name of the pin to display
49 * Return: 0 on success, non-0 on error
51 static int show_pinmux(struct udevice *dev, char *name)
53 char pin_name[PINNAME_SIZE];
54 char pin_mux[PINMUX_SIZE];
60 pins_count = pinctrl_get_pins_count(dev);
62 if (pins_count == -ENOSYS) {
63 printf("Ops get_pins_count not supported by %s\n", dev->name);
67 for (i = 0; i < pins_count; i++) {
68 ret = pinctrl_get_pin_name(dev, i, pin_name, PINNAME_SIZE);
70 printf("Ops get_pin_name error (%d) by %s\n", ret, dev->name);
73 if (name && strcmp(name, pin_name))
76 ret = pinctrl_get_pin_muxing(dev, i, pin_mux, PINMUX_SIZE);
78 printf("Ops get_pin_muxing error (%d) by %s in %s\n",
79 ret, pin_name, dev->name);
83 printf("%-*s: %-*s\n", PINNAME_SIZE, pin_name,
84 PINMUX_SIZE, pin_mux);
93 static int do_status(struct cmd_tbl *cmdtp, int flag, int argc,
102 printf("pin-controller device not selected\n");
103 return CMD_RET_FAILURE;
105 show_pinmux(currdev, NULL);
106 return CMD_RET_SUCCESS;
109 if (strcmp(argv[1], "-a"))
114 uclass_foreach_dev_probe(UCLASS_PINCTRL, dev) {
116 /* insert a separator between each pin-controller display */
117 printf("--------------------------\n");
118 printf("%s:\n", dev->name);
120 ret = show_pinmux(dev, name);
121 /* stop when the status of requested pin is displayed */
123 return CMD_RET_SUCCESS;
127 printf("%s not found\n", name);
128 return CMD_RET_FAILURE;
131 return CMD_RET_SUCCESS;
134 static int do_list(struct cmd_tbl *cmdtp, int flag, int argc,
139 printf("| %-*.*s| %-*.*s| %s\n",
140 LIMIT_DEVNAME, LIMIT_DEVNAME, "Device",
141 LIMIT_DEVNAME, LIMIT_DEVNAME, "Driver",
144 uclass_foreach_dev_probe(UCLASS_PINCTRL, dev) {
145 printf("| %-*.*s| %-*.*s| %s\n",
146 LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name,
147 LIMIT_DEVNAME, LIMIT_DEVNAME, dev->driver->name,
151 return CMD_RET_SUCCESS;
154 static struct cmd_tbl pinmux_subcmd[] = {
155 U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""),
156 U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""),
157 U_BOOT_CMD_MKENT(status, 2, 1, do_status, "", ""),
160 static int do_pinmux(struct cmd_tbl *cmdtp, int flag, int argc,
168 cmd = find_cmd_tbl(argv[0], pinmux_subcmd, ARRAY_SIZE(pinmux_subcmd));
169 if (!cmd || argc > cmd->maxargs)
170 return CMD_RET_USAGE;
172 return cmd->cmd(cmdtp, flag, argc, argv);
175 U_BOOT_CMD(pinmux, CONFIG_SYS_MAXARGS, 1, do_pinmux,
176 "show pin-controller muxing",
177 "list - list UCLASS_PINCTRL devices\n"
178 "pinmux dev [pincontroller-name] - select pin-controller device\n"
179 "pinmux status [-a | pin-name] - print pin-controller muxing [for all | for pin-name]\n"