Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 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 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 17 | static int do_dev(struct cmd_tbl *cmdtp, int flag, int argc, |
| 18 | char *const argv[]) |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 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 | } |
Patrick Delaunay | 9e2a8bc | 2019-06-21 15:26:56 +0200 | [diff] [blame] | 31 | /* fall through */ |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 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 | |
Patrick Delaunay | b40399c | 2021-05-21 09:47:32 +0200 | [diff] [blame] | 44 | /** |
| 45 | * Print the muxing information for one or all pins of one pinctrl device |
| 46 | * |
| 47 | * @param dev pinctrl device |
| 48 | * @param name NULL to display all the pins |
| 49 | * or name of the pin to display |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 50 | * Return: 0 on success, non-0 on error |
Patrick Delaunay | b40399c | 2021-05-21 09:47:32 +0200 | [diff] [blame] | 51 | */ |
| 52 | static int show_pinmux(struct udevice *dev, char *name) |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 53 | { |
| 54 | char pin_name[PINNAME_SIZE]; |
| 55 | char pin_mux[PINMUX_SIZE]; |
| 56 | int pins_count; |
| 57 | int i; |
| 58 | int ret; |
Patrick Delaunay | b40399c | 2021-05-21 09:47:32 +0200 | [diff] [blame] | 59 | bool found = false; |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 60 | |
| 61 | pins_count = pinctrl_get_pins_count(dev); |
| 62 | |
| 63 | if (pins_count == -ENOSYS) { |
Patrick Delaunay | da45368 | 2021-05-21 09:47:31 +0200 | [diff] [blame] | 64 | printf("Ops get_pins_count not supported by %s\n", dev->name); |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 65 | return pins_count; |
| 66 | } |
| 67 | |
| 68 | for (i = 0; i < pins_count; i++) { |
| 69 | ret = pinctrl_get_pin_name(dev, i, pin_name, PINNAME_SIZE); |
Patrick Delaunay | da45368 | 2021-05-21 09:47:31 +0200 | [diff] [blame] | 70 | if (ret) { |
| 71 | printf("Ops get_pin_name error (%d) by %s\n", ret, dev->name); |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 72 | return ret; |
| 73 | } |
Patrick Delaunay | b40399c | 2021-05-21 09:47:32 +0200 | [diff] [blame] | 74 | if (name && strcmp(name, pin_name)) |
| 75 | continue; |
| 76 | found = true; |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 77 | ret = pinctrl_get_pin_muxing(dev, i, pin_mux, PINMUX_SIZE); |
| 78 | if (ret) { |
Patrick Delaunay | da45368 | 2021-05-21 09:47:31 +0200 | [diff] [blame] | 79 | printf("Ops get_pin_muxing error (%d) by %s in %s\n", |
| 80 | ret, pin_name, dev->name); |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | printf("%-*s: %-*s\n", PINNAME_SIZE, pin_name, |
| 85 | PINMUX_SIZE, pin_mux); |
| 86 | } |
| 87 | |
Patrick Delaunay | b40399c | 2021-05-21 09:47:32 +0200 | [diff] [blame] | 88 | if (!found) |
| 89 | return -ENOENT; |
| 90 | |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 94 | static int do_status(struct cmd_tbl *cmdtp, int flag, int argc, |
| 95 | char *const argv[]) |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 96 | { |
| 97 | struct udevice *dev; |
Patrick Delaunay | b40399c | 2021-05-21 09:47:32 +0200 | [diff] [blame] | 98 | char *name; |
| 99 | int ret; |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 100 | |
Patrick Delaunay | da45368 | 2021-05-21 09:47:31 +0200 | [diff] [blame] | 101 | if (argc < 2) { |
| 102 | if (!currdev) { |
| 103 | printf("pin-controller device not selected\n"); |
| 104 | return CMD_RET_FAILURE; |
| 105 | } |
Patrick Delaunay | b40399c | 2021-05-21 09:47:32 +0200 | [diff] [blame] | 106 | show_pinmux(currdev, NULL); |
Patrick Delaunay | da45368 | 2021-05-21 09:47:31 +0200 | [diff] [blame] | 107 | return CMD_RET_SUCCESS; |
| 108 | } |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 109 | |
Patrick Delaunay | da45368 | 2021-05-21 09:47:31 +0200 | [diff] [blame] | 110 | if (strcmp(argv[1], "-a")) |
Patrick Delaunay | b40399c | 2021-05-21 09:47:32 +0200 | [diff] [blame] | 111 | name = argv[1]; |
| 112 | else |
| 113 | name = NULL; |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 114 | |
| 115 | uclass_foreach_dev_probe(UCLASS_PINCTRL, dev) { |
Patrick Delaunay | b40399c | 2021-05-21 09:47:32 +0200 | [diff] [blame] | 116 | if (!name) { |
| 117 | /* insert a separator between each pin-controller display */ |
| 118 | printf("--------------------------\n"); |
| 119 | printf("%s:\n", dev->name); |
| 120 | } |
| 121 | ret = show_pinmux(dev, name); |
| 122 | /* stop when the status of requested pin is displayed */ |
| 123 | if (name && !ret) |
| 124 | return CMD_RET_SUCCESS; |
| 125 | } |
| 126 | |
| 127 | if (name) { |
| 128 | printf("%s not found\n", name); |
| 129 | return CMD_RET_FAILURE; |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Patrick Delaunay | da45368 | 2021-05-21 09:47:31 +0200 | [diff] [blame] | 132 | return CMD_RET_SUCCESS; |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 135 | static int do_list(struct cmd_tbl *cmdtp, int flag, int argc, |
| 136 | char *const argv[]) |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 137 | { |
| 138 | struct udevice *dev; |
| 139 | |
| 140 | printf("| %-*.*s| %-*.*s| %s\n", |
| 141 | LIMIT_DEVNAME, LIMIT_DEVNAME, "Device", |
| 142 | LIMIT_DEVNAME, LIMIT_DEVNAME, "Driver", |
| 143 | "Parent"); |
| 144 | |
| 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, |
| 149 | dev->parent->name); |
| 150 | } |
| 151 | |
| 152 | return CMD_RET_SUCCESS; |
| 153 | } |
| 154 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 155 | static struct cmd_tbl pinmux_subcmd[] = { |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 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, "", ""), |
| 159 | }; |
| 160 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 161 | static int do_pinmux(struct cmd_tbl *cmdtp, int flag, int argc, |
| 162 | char *const argv[]) |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 163 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 164 | struct cmd_tbl *cmd; |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 165 | |
| 166 | argc--; |
| 167 | argv++; |
| 168 | |
| 169 | cmd = find_cmd_tbl(argv[0], pinmux_subcmd, ARRAY_SIZE(pinmux_subcmd)); |
| 170 | if (!cmd || argc > cmd->maxargs) |
| 171 | return CMD_RET_USAGE; |
| 172 | |
| 173 | return cmd->cmd(cmdtp, flag, argc, argv); |
| 174 | } |
| 175 | |
| 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" |
Patrick Delaunay | b40399c | 2021-05-21 09:47:32 +0200 | [diff] [blame] | 180 | "pinmux status [-a | pin-name] - print pin-controller muxing [for all | for pin-name]\n" |
Patrice Chotard | c4b4ef0 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 181 | ) |