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;
44 static int show_pinmux(struct udevice *dev)
46 char pin_name[PINNAME_SIZE];
47 char pin_mux[PINMUX_SIZE];
52 pins_count = pinctrl_get_pins_count(dev);
54 if (pins_count == -ENOSYS) {
55 printf("Ops get_pins_count not supported\n");
59 for (i = 0; i < pins_count; i++) {
60 ret = pinctrl_get_pin_name(dev, i, pin_name, PINNAME_SIZE);
62 printf("Ops get_pin_name not supported\n");
66 ret = pinctrl_get_pin_muxing(dev, i, pin_mux, PINMUX_SIZE);
68 printf("Ops get_pin_muxing error (%d)\n", ret);
72 printf("%-*s: %-*s\n", PINNAME_SIZE, pin_name,
73 PINMUX_SIZE, pin_mux);
79 static int do_status(struct cmd_tbl *cmdtp, int flag, int argc,
83 int ret = CMD_RET_USAGE;
85 if (currdev && (argc < 2 || strcmp(argv[1], "-a")))
86 return show_pinmux(currdev);
88 if (argc < 2 || strcmp(argv[1], "-a"))
91 uclass_foreach_dev_probe(UCLASS_PINCTRL, dev) {
92 /* insert a separator between each pin-controller display */
93 printf("--------------------------\n");
94 printf("%s:\n", dev->name);
95 ret = show_pinmux(dev);
97 printf("Can't display pin muxing for %s\n",
104 static int do_list(struct cmd_tbl *cmdtp, int flag, int argc,
109 printf("| %-*.*s| %-*.*s| %s\n",
110 LIMIT_DEVNAME, LIMIT_DEVNAME, "Device",
111 LIMIT_DEVNAME, LIMIT_DEVNAME, "Driver",
114 uclass_foreach_dev_probe(UCLASS_PINCTRL, dev) {
115 printf("| %-*.*s| %-*.*s| %s\n",
116 LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name,
117 LIMIT_DEVNAME, LIMIT_DEVNAME, dev->driver->name,
121 return CMD_RET_SUCCESS;
124 static struct cmd_tbl pinmux_subcmd[] = {
125 U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""),
126 U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""),
127 U_BOOT_CMD_MKENT(status, 2, 1, do_status, "", ""),
130 static int do_pinmux(struct cmd_tbl *cmdtp, int flag, int argc,
138 cmd = find_cmd_tbl(argv[0], pinmux_subcmd, ARRAY_SIZE(pinmux_subcmd));
139 if (!cmd || argc > cmd->maxargs)
140 return CMD_RET_USAGE;
142 return cmd->cmd(cmdtp, flag, argc, argv);
145 U_BOOT_CMD(pinmux, CONFIG_SYS_MAXARGS, 1, do_pinmux,
146 "show pin-controller muxing",
147 "list - list UCLASS_PINCTRL devices\n"
148 "pinmux dev [pincontroller-name] - select pin-controller device\n"
149 "pinmux status [-a] - print pin-controller muxing [for all]\n"