Merge tag 'dm-pull-15nov20' of git://git.denx.de/u-boot-dm
[platform/kernel/u-boot.git] / cmd / pinmux.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4  */
5
6 #include <common.h>
7 #include <command.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <dm/pinctrl.h>
11 #include <dm/uclass-internal.h>
12
13 #define LIMIT_DEVNAME   30
14
15 static struct udevice *currdev;
16
17 static int do_dev(struct cmd_tbl *cmdtp, int flag, int argc,
18                   char *const argv[])
19 {
20         const char *name;
21         int ret;
22
23         switch (argc) {
24         case 2:
25                 name = argv[1];
26                 ret = uclass_get_device_by_name(UCLASS_PINCTRL, name, &currdev);
27                 if (ret) {
28                         printf("Can't get the pin-controller: %s!\n", name);
29                         return CMD_RET_FAILURE;
30                 }
31                 /* fall through */
32         case 1:
33                 if (!currdev) {
34                         printf("Pin-controller device is not set!\n");
35                         return CMD_RET_USAGE;
36                 }
37
38                 printf("dev: %s\n", currdev->name);
39         }
40
41         return CMD_RET_SUCCESS;
42 }
43
44 static int show_pinmux(struct udevice *dev)
45 {
46         char pin_name[PINNAME_SIZE];
47         char pin_mux[PINMUX_SIZE];
48         int pins_count;
49         int i;
50         int ret;
51
52         pins_count = pinctrl_get_pins_count(dev);
53
54         if (pins_count == -ENOSYS) {
55                 printf("Ops get_pins_count not supported\n");
56                 return pins_count;
57         }
58
59         for (i = 0; i < pins_count; i++) {
60                 ret = pinctrl_get_pin_name(dev, i, pin_name, PINNAME_SIZE);
61                 if (ret == -ENOSYS) {
62                         printf("Ops get_pin_name not supported\n");
63                         return ret;
64                 }
65
66                 ret = pinctrl_get_pin_muxing(dev, i, pin_mux, PINMUX_SIZE);
67                 if (ret) {
68                         printf("Ops get_pin_muxing error (%d)\n", ret);
69                         return ret;
70                 }
71
72                 printf("%-*s: %-*s\n", PINNAME_SIZE, pin_name,
73                        PINMUX_SIZE, pin_mux);
74         }
75
76         return 0;
77 }
78
79 static int do_status(struct cmd_tbl *cmdtp, int flag, int argc,
80                      char *const argv[])
81 {
82         struct udevice *dev;
83         int ret = CMD_RET_USAGE;
84
85         if (currdev && (argc < 2 || strcmp(argv[1], "-a")))
86                 return show_pinmux(currdev);
87
88         if (argc < 2 || strcmp(argv[1], "-a"))
89                 return ret;
90
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);
96                 if (ret < 0)
97                         printf("Can't display pin muxing for %s\n",
98                                dev->name);
99         }
100
101         return ret;
102 }
103
104 static int do_list(struct cmd_tbl *cmdtp, int flag, int argc,
105                    char *const argv[])
106 {
107         struct udevice *dev;
108
109         printf("| %-*.*s| %-*.*s| %s\n",
110                LIMIT_DEVNAME, LIMIT_DEVNAME, "Device",
111                LIMIT_DEVNAME, LIMIT_DEVNAME, "Driver",
112                "Parent");
113
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,
118                        dev->parent->name);
119         }
120
121         return CMD_RET_SUCCESS;
122 }
123
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, "", ""),
128 };
129
130 static int do_pinmux(struct cmd_tbl *cmdtp, int flag, int argc,
131                      char *const argv[])
132 {
133         struct cmd_tbl *cmd;
134
135         argc--;
136         argv++;
137
138         cmd = find_cmd_tbl(argv[0], pinmux_subcmd, ARRAY_SIZE(pinmux_subcmd));
139         if (!cmd || argc > cmd->maxargs)
140                 return CMD_RET_USAGE;
141
142         return cmd->cmd(cmdtp, flag, argc, argv);
143 }
144
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"
150 )