Patrice Chotard | d5a8313 | 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 | 0914011 | 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 | d5a8313 | 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 | d23aef1 | 2019-06-21 15:26:56 +0200 | [diff] [blame] | 31 | /* fall through */ |
Patrice Chotard | d5a8313 | 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 | |
| 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) { |
Patrick Delaunay | 4c60fd9 | 2021-05-21 09:47:31 +0200 | [diff] [blame^] | 55 | printf("Ops get_pins_count not supported by %s\n", dev->name); |
Patrice Chotard | d5a8313 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 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); |
Patrick Delaunay | 4c60fd9 | 2021-05-21 09:47:31 +0200 | [diff] [blame^] | 61 | if (ret) { |
| 62 | printf("Ops get_pin_name error (%d) by %s\n", ret, dev->name); |
Patrice Chotard | d5a8313 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | ret = pinctrl_get_pin_muxing(dev, i, pin_mux, PINMUX_SIZE); |
| 67 | if (ret) { |
Patrick Delaunay | 4c60fd9 | 2021-05-21 09:47:31 +0200 | [diff] [blame^] | 68 | printf("Ops get_pin_muxing error (%d) by %s in %s\n", |
| 69 | ret, pin_name, dev->name); |
Patrice Chotard | d5a8313 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 70 | 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 Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 80 | static int do_status(struct cmd_tbl *cmdtp, int flag, int argc, |
| 81 | char *const argv[]) |
Patrice Chotard | d5a8313 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 82 | { |
| 83 | struct udevice *dev; |
Patrice Chotard | d5a8313 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 84 | |
Patrick Delaunay | 4c60fd9 | 2021-05-21 09:47:31 +0200 | [diff] [blame^] | 85 | 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 Chotard | d5a8313 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 93 | |
Patrick Delaunay | 4c60fd9 | 2021-05-21 09:47:31 +0200 | [diff] [blame^] | 94 | if (strcmp(argv[1], "-a")) |
| 95 | return CMD_RET_USAGE; |
Patrice Chotard | d5a8313 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 96 | |
| 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 Delaunay | 4c60fd9 | 2021-05-21 09:47:31 +0200 | [diff] [blame^] | 101 | show_pinmux(dev); |
Patrice Chotard | d5a8313 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Patrick Delaunay | 4c60fd9 | 2021-05-21 09:47:31 +0200 | [diff] [blame^] | 104 | return CMD_RET_SUCCESS; |
Patrice Chotard | d5a8313 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 107 | static int do_list(struct cmd_tbl *cmdtp, int flag, int argc, |
| 108 | char *const argv[]) |
Patrice Chotard | d5a8313 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 109 | { |
| 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 Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 127 | static struct cmd_tbl pinmux_subcmd[] = { |
Patrice Chotard | d5a8313 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 128 | 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 Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 133 | static int do_pinmux(struct cmd_tbl *cmdtp, int flag, int argc, |
| 134 | char *const argv[]) |
Patrice Chotard | d5a8313 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 135 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 136 | struct cmd_tbl *cmd; |
Patrice Chotard | d5a8313 | 2018-10-24 14:10:17 +0200 | [diff] [blame] | 137 | |
| 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 | |
| 148 | U_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 | ) |