1 // SPDX-License-Identifier: GPL-2.0
3 * List, select, and deselect mux controllers on the fly.
5 * Copyright (c) 2020 Texas Instruments Inc.
6 * Author: Pratyush Yadav <p.yadav@ti.com>
13 #include <dm/device_compat.h>
15 #include <mux-internal.h>
16 #include <linux/err.h>
17 #include <dt-bindings/mux/mux.h>
19 #define COLUMN_SIZE 16
22 * Print a member of a column. The total size of the text printed, including
23 * trailing whitespace, will always be COLUMN_SIZE.
25 #define PRINT_COLUMN(fmt, args...) do { \
26 char buf[COLUMN_SIZE + 1]; \
27 snprintf(buf, COLUMN_SIZE + 1, fmt, ##args); \
28 printf("%-*s", COLUMN_SIZE, buf); \
32 * Find a mux based on its device name in argv[1] and index in the chip in
35 static struct mux_control *cmd_mux_find(char *const argv[])
38 struct mux_chip *chip;
42 ret = strict_strtoul(argv[2], 10, &id);
46 ret = uclass_get_device_by_name(UCLASS_MUX, argv[1], &dev);
50 chip = dev_get_uclass_priv(dev);
54 if (id >= chip->controllers)
55 return ERR_PTR(-EINVAL);
57 return &chip->mux[id];
61 * Print the details of a mux. The columns printed correspond to: "Selected",
62 * "Current State", "Idle State", and "Num States".
64 static void print_mux(struct mux_control *mux)
66 PRINT_COLUMN("%s", mux->in_use ? "yes" : "no");
68 if (mux->cached_state == MUX_IDLE_AS_IS)
69 PRINT_COLUMN("%s", "unknown");
71 PRINT_COLUMN("0x%x", mux->cached_state);
73 if (mux->idle_state == MUX_IDLE_AS_IS)
74 PRINT_COLUMN("%s", "as-is");
75 else if (mux->idle_state == MUX_IDLE_DISCONNECT)
76 PRINT_COLUMN("%s", "disconnect");
78 PRINT_COLUMN("0x%x", mux->idle_state);
80 PRINT_COLUMN("0x%x", mux->states);
85 static int do_mux_list(struct cmd_tbl *cmdtp, int flag, int argc,
89 struct mux_chip *chip;
92 for (uclass_first_device(UCLASS_MUX, &dev);
94 uclass_next_device(&dev)) {
95 chip = dev_get_uclass_priv(dev);
97 dev_err(dev, "can't find mux chip\n");
101 printf("%s:\n", dev->name);
105 PRINT_COLUMN("Selected");
106 PRINT_COLUMN("Current State");
107 PRINT_COLUMN("Idle State");
108 PRINT_COLUMN("Num States");
110 for (j = 0; j < chip->controllers; j++) {
112 PRINT_COLUMN("%d", j);
113 print_mux(&chip->mux[j]);
121 static int do_mux_select(struct cmd_tbl *cmdtp, int flag, int argc,
124 struct mux_control *mux;
129 return CMD_RET_USAGE;
131 mux = cmd_mux_find(argv);
132 if (IS_ERR_OR_NULL(mux)) {
133 printf("Failed to find the specified mux\n");
134 return CMD_RET_FAILURE;
137 ret = strict_strtoul(argv[3], 16, &state);
139 printf("Invalid state\n");
140 return CMD_RET_FAILURE;
143 ret = mux_control_select(mux, state);
145 printf("Failed to select requested state\n");
146 return CMD_RET_FAILURE;
149 return CMD_RET_SUCCESS;
152 static int do_mux_deselect(struct cmd_tbl *cmdtp, int flag, int argc,
155 struct mux_control *mux;
159 return CMD_RET_USAGE;
161 mux = cmd_mux_find(argv);
162 if (IS_ERR_OR_NULL(mux)) {
163 printf("Failed to find the specified mux\n");
164 return CMD_RET_FAILURE;
167 ret = mux_control_deselect(mux);
169 printf("Failed to deselect mux\n");
170 return CMD_RET_FAILURE;
173 return CMD_RET_SUCCESS;
176 static char mux_help_text[] =
177 "list - List all Muxes and their states\n"
178 "select <chip> <id> <state> - Select the given mux state\n"
179 "deselect <chip> <id> - Deselect the given mux and reset it to its idle state";
181 U_BOOT_CMD_WITH_SUBCMDS(mux, "List, select, and deselect muxes", mux_help_text,
182 U_BOOT_SUBCMD_MKENT(list, 1, 1, do_mux_list),
183 U_BOOT_SUBCMD_MKENT(select, 4, 0, do_mux_select),
184 U_BOOT_SUBCMD_MKENT(deselect, 3, 0, do_mux_deselect));