blob: 0df78c71da7d2f14df9297e9f634e0b8bfb98da0 [file] [log] [blame]
Patrice Chotardd5a83132018-10-24 14:10:17 +02001// 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
15static struct udevice *currdev;
16
Simon Glass09140112020-05-10 11:40:03 -060017static int do_dev(struct cmd_tbl *cmdtp, int flag, int argc,
18 char *const argv[])
Patrice Chotardd5a83132018-10-24 14:10:17 +020019{
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 }
Patrick Delaunayd23aef12019-06-21 15:26:56 +020031 /* fall through */
Patrice Chotardd5a83132018-10-24 14:10:17 +020032 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
44static 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) {
Patrick Delaunay4c60fd92021-05-21 09:47:31 +020055 printf("Ops get_pins_count not supported by %s\n", dev->name);
Patrice Chotardd5a83132018-10-24 14:10:17 +020056 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);
Patrick Delaunay4c60fd92021-05-21 09:47:31 +020061 if (ret) {
62 printf("Ops get_pin_name error (%d) by %s\n", ret, dev->name);
Patrice Chotardd5a83132018-10-24 14:10:17 +020063 return ret;
64 }
65
66 ret = pinctrl_get_pin_muxing(dev, i, pin_mux, PINMUX_SIZE);
67 if (ret) {
Patrick Delaunay4c60fd92021-05-21 09:47:31 +020068 printf("Ops get_pin_muxing error (%d) by %s in %s\n",
69 ret, pin_name, dev->name);
Patrice Chotardd5a83132018-10-24 14:10:17 +020070 return ret;
71 }
72
73 printf("%-*s: %-*s\n", PINNAME_SIZE, pin_name,
74 PINMUX_SIZE, pin_mux);
75 }
76
77 return 0;
78}
79
Simon Glass09140112020-05-10 11:40:03 -060080static int do_status(struct cmd_tbl *cmdtp, int flag, int argc,
81 char *const argv[])
Patrice Chotardd5a83132018-10-24 14:10:17 +020082{
83 struct udevice *dev;
Patrice Chotardd5a83132018-10-24 14:10:17 +020084
Patrick Delaunay4c60fd92021-05-21 09:47:31 +020085 if (argc < 2) {
86 if (!currdev) {
87 printf("pin-controller device not selected\n");
88 return CMD_RET_FAILURE;
89 }
90 show_pinmux(currdev);
91 return CMD_RET_SUCCESS;
92 }
Patrice Chotardd5a83132018-10-24 14:10:17 +020093
Patrick Delaunay4c60fd92021-05-21 09:47:31 +020094 if (strcmp(argv[1], "-a"))
95 return CMD_RET_USAGE;
Patrice Chotardd5a83132018-10-24 14:10:17 +020096
97 uclass_foreach_dev_probe(UCLASS_PINCTRL, dev) {
98 /* insert a separator between each pin-controller display */
99 printf("--------------------------\n");
100 printf("%s:\n", dev->name);
Patrick Delaunay4c60fd92021-05-21 09:47:31 +0200101 show_pinmux(dev);
Patrice Chotardd5a83132018-10-24 14:10:17 +0200102 }
103
Patrick Delaunay4c60fd92021-05-21 09:47:31 +0200104 return CMD_RET_SUCCESS;
Patrice Chotardd5a83132018-10-24 14:10:17 +0200105}
106
Simon Glass09140112020-05-10 11:40:03 -0600107static int do_list(struct cmd_tbl *cmdtp, int flag, int argc,
108 char *const argv[])
Patrice Chotardd5a83132018-10-24 14:10:17 +0200109{
110 struct udevice *dev;
111
112 printf("| %-*.*s| %-*.*s| %s\n",
113 LIMIT_DEVNAME, LIMIT_DEVNAME, "Device",
114 LIMIT_DEVNAME, LIMIT_DEVNAME, "Driver",
115 "Parent");
116
117 uclass_foreach_dev_probe(UCLASS_PINCTRL, dev) {
118 printf("| %-*.*s| %-*.*s| %s\n",
119 LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name,
120 LIMIT_DEVNAME, LIMIT_DEVNAME, dev->driver->name,
121 dev->parent->name);
122 }
123
124 return CMD_RET_SUCCESS;
125}
126
Simon Glass09140112020-05-10 11:40:03 -0600127static struct cmd_tbl pinmux_subcmd[] = {
Patrice Chotardd5a83132018-10-24 14:10:17 +0200128 U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""),
129 U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""),
130 U_BOOT_CMD_MKENT(status, 2, 1, do_status, "", ""),
131};
132
Simon Glass09140112020-05-10 11:40:03 -0600133static int do_pinmux(struct cmd_tbl *cmdtp, int flag, int argc,
134 char *const argv[])
Patrice Chotardd5a83132018-10-24 14:10:17 +0200135{
Simon Glass09140112020-05-10 11:40:03 -0600136 struct cmd_tbl *cmd;
Patrice Chotardd5a83132018-10-24 14:10:17 +0200137
138 argc--;
139 argv++;
140
141 cmd = find_cmd_tbl(argv[0], pinmux_subcmd, ARRAY_SIZE(pinmux_subcmd));
142 if (!cmd || argc > cmd->maxargs)
143 return CMD_RET_USAGE;
144
145 return cmd->cmd(cmdtp, flag, argc, argv);
146}
147
148U_BOOT_CMD(pinmux, CONFIG_SYS_MAXARGS, 1, do_pinmux,
149 "show pin-controller muxing",
150 "list - list UCLASS_PINCTRL devices\n"
151 "pinmux dev [pincontroller-name] - select pin-controller device\n"
152 "pinmux status [-a] - print pin-controller muxing [for all]\n"
153)