mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Giandomenico Rossi <rossi@amelchem.com>
To: barebox@lists.infradead.org
Cc: Giandomenico Rossi <rossi@amelchem.com>, Nicola Fontana <ntd@entidi.it>
Subject: [PATCH 4/4] commands: stusb4500: add USB-C PD control command
Date: Sat, 14 Nov 2026 17:19:53 +0100	[thread overview]
Message-ID: <20261114161953.1457428-5-rossi@amelchem.com> (raw)
In-Reply-To: <20261114161953.1457428-1-rossi@amelchem.com>

Add a Barebox shell command to inspect and control the STUSB4500
USB-C Power Delivery sink controller.

The command supports listing and dumping configured PDOs, updating
sink PDOs, forcing PD renegotiation, and displaying the negotiated
contract. The target STUSB4500 device can be selected with the -d
option.

The command is intended primarily for board bring-up.

Reviewed-by: Nicola Fontana <ntd@entidi.it>
Signed-off-by: Giandomenico Rossi <rossi@amelchem.com>
---
 commands/Kconfig     |  23 ++++
 commands/Makefile    |   1 +
 commands/stusb4500.c | 274 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 298 insertions(+)
 create mode 100644 commands/stusb4500.c

diff --git a/commands/Kconfig b/commands/Kconfig
index a43f0fbd4d..1164c29b96 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -2762,6 +2762,29 @@ config CMD_STACKSMASH
 	  This commands trashes the stack to test stackprotector and
 	  guard page. This command does not return.
 
+config STUSB4500_COMMANDS
+	bool "STUSB4500 shell command"
+	depends on STUSB4500 && !SHELL_NONE
+	help
+	  Adds the 'stusb4500' shell command to control and inspect an
+	  STUSB4500 USB-C Power Delivery sink controller at runtime.
+
+	  Usage:
+		stusb4500 [-d <device>] <action> [args...]
+
+	  Actions:
+		set_active_pdos                set currently active PDOs
+		dump_pdos                      dump all configured sink PDOs
+		update_pdo <id> <mv> <ma>      update a sink PDO
+		renegotiate                    force PD renegotiation
+		contract                       show the negotiated contract
+
+	  If -d is omitted, the command targets the only STUSB4500 in the
+	  system; specify -d <device> to disambiguate when several are present.
+
+	  This command is mainly intended for bring-up and debugging. It is
+	  safe to leave disabled in production builds.
+
 # end Miscellaneous commands
 endmenu
 
diff --git a/commands/Makefile b/commands/Makefile
index 7dae582225..d43d8521d5 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -175,4 +175,5 @@ obj-$(CONFIG_CMD_VERITYSETUP)	+= veritysetup.o
 obj-$(CONFIG_CMD_LVM)		+= lvm.o
 obj-$(CONFIG_CMD_SCONFIG)	+= sconfig.o
 obj-$(CONFIG_CMD_RKSECURE)	+= rksecure.o
+obj-$(CONFIG_STUSB4500_COMMANDS) += stusb4500.o
 UBSAN_SANITIZE_ubsan.o := y
diff --git a/commands/stusb4500.c b/commands/stusb4500.c
new file mode 100644
index 0000000000..1931ab7464
--- /dev/null
+++ b/commands/stusb4500.c
@@ -0,0 +1,274 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// SPDX-FileCopyrightText: 2026 Giandomenico Rossi <rossi@amelchem.com>, Amel s.r.l.
+
+/*
+ * stusb4500 - command to control and inspect an STUSB4500 USB-C PD sink.
+ *
+ * The STUSB4500 is a standalone USB Power Delivery sink controller that
+ * autonomously negotiates a power contract from a stored set of sink PDOs.
+ * This command exposes the controller's state and configuration at the
+ * barebox prompt: it reads back the negotiated contract and the advertised
+ * source PDOs, edits the locally stored sink PDOs, selects how many of them
+ * are active, and forces a PD renegotiation.
+ *
+ * Actions are dispatched through a small table (struct stusb4500_action);
+ * each entry declares the minimum number of arguments it expects, which is
+ * validated before the handler runs.
+ */
+
+#include <common.h>
+#include <command.h>
+#include <complete.h>
+#include <getopt.h>
+#include <linux/kstrtox.h>
+#include <usb/stusb4500.h>
+
+
+/**
+ * struct stusb4500_action - one subcommand of the stusb4500 command
+ * @name:     action keyword typed by the user
+ * @fn:       handler, receiving the arguments that follow the action name
+ * @min_args: number of arguments @fn requires (checked before dispatch)
+ * @help:     one-line usage string, also printed on a missing-argument error
+ */
+struct stusb4500_action {
+	const char *name;
+	int (*fn)(struct stusb4500 *stusb, int argc, char *argv[]);
+	int min_args;
+	const char *help;
+};
+
+/* Read and print the currently negotiated PD power contract. */
+static int action_contract(struct stusb4500 *stusb, int argc, char *argv[])
+{
+	struct stusb4500_contract contract;
+	int ret;
+
+	ret = stusb4500_read_contract(stusb, &contract);
+	if (ret) {
+		pr_err("read_contract() failed with %d\n", ret);
+		return COMMAND_ERROR;
+	}
+
+	stusb4500_dump_contract(stusb, &contract);
+
+	return ret;
+}
+
+/* Trigger a new PD negotiation, then show the resulting contract. */
+static int action_renegotiate(struct stusb4500 *stusb, int argc, char *argv[])
+{
+	int ret = stusb4500_renegotiate(stusb);
+
+	if (ret < 0) {
+		pr_err("renegotiate() failed with %d\n", ret);
+		return COMMAND_ERROR;
+	}
+
+	return action_contract(stusb, argc, argv);
+}
+
+/* Dump the sink PDOs stored in the controller. */
+static int action_dump_sink_pdos(struct stusb4500 *stusb, int argc, char *argv[])
+{
+	int ret = stusb4500_dump_sink_pdos(stusb);
+
+	if (ret)
+		pr_err("dump_sink_pdos() failed with %d\n", ret);
+
+	return ret;
+}
+
+/* Scan and dump the source PDOs advertised by the attached port partner. */
+static int action_dump_source_pdos(struct stusb4500 *stusb, int argc, char *argv[])
+{
+	int ret;
+
+	ret = stusb4500_scan_src_pdo(stusb);
+	if (ret < 0)
+		return ret;
+
+	stusb4500_dump_source_pdos(stusb);
+
+	return 0;
+}
+
+/* Edit a stored sink PDO: update_pdo <id> <voltage_mv> <current_ma>. */
+static int action_update_pdo(struct stusb4500 *stusb, int argc, char *argv[])
+{
+	unsigned int id, voltage_mv, current_ma;
+	int ret;
+
+	ret = kstrtouint(argv[0], 10, &id);
+	if (ret < 0)
+		return COMMAND_ERROR_USAGE;
+
+	ret = kstrtouint(argv[1], 10, &voltage_mv);
+	if (ret < 0)
+		return COMMAND_ERROR_USAGE;
+
+	ret = kstrtouint(argv[2], 10, &current_ma);
+	if (ret < 0)
+		return COMMAND_ERROR_USAGE;
+
+	if (id < 1 || id > STUSB4500_MAX_PDO) {
+		pr_err("invalid PDO index: %u is outside of 1..%d\n", id, STUSB4500_MAX_PDO);
+		return COMMAND_ERROR_USAGE;
+	}
+
+	ret = stusb4500_update_sink_pdo(stusb, id, voltage_mv, current_ma);
+	if (ret) {
+		pr_err("update_sink_pdo(%u) failed with %d\n", id, ret);
+		return ret;
+	}
+
+	printf("PDO %u set to %u mV / %u mA\n", id, voltage_mv, current_ma);
+	return 0;
+}
+
+/* Select how many of the stored sink PDOs are advertised during negotiation. */
+static int action_set_active_pdos(struct stusb4500 *stusb, int argc, char *argv[])
+{
+	unsigned int nr_pdo;
+	int ret;
+
+	ret = kstrtouint(argv[0], 0, &nr_pdo);
+	if (ret < 0)
+		return COMMAND_ERROR_USAGE;
+
+	if (nr_pdo < 1 || nr_pdo > STUSB4500_MAX_PDO) {
+		pr_err("invalid PDO number: %u is outside of 1..%d\n", nr_pdo, STUSB4500_MAX_PDO);
+		return COMMAND_ERROR_USAGE;
+	}
+
+	ret = stusb4500_set_active_pdos(stusb, nr_pdo);
+	if (ret)
+		pr_err("set_active_pdos() failed with %d\n", ret);
+
+	return ret;
+}
+
+static const struct stusb4500_action actions[] = {
+	{ "set_active_pdos",  action_set_active_pdos,  1, "<npdo> set # of active PDOs" },
+	{ "dump_sink_pdos",   action_dump_sink_pdos,   0, "dump all sink PDOs" },
+	{ "dump_source_pdos", action_dump_source_pdos, 0, "dump all source PDOs" },
+	{ "update_pdo",       action_update_pdo,       3, "<id> <mv> <ma> update a sink PDO" },
+	{ "renegotiate",      action_renegotiate,      0, "force PD renegotiation" },
+	{ "contract",         action_contract,         0, "show negotiated contract" },
+};
+
+/* Tab-completion for action keywords, falling back to device names. */
+static int stusb4500_complete(struct string_list *sl, char *instr)
+{
+	int len = strlen(instr);
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(actions); i++) {
+		if (strncmp(actions[i].name, instr, len) == 0)
+			string_list_add_asprintf(sl, "%s ", actions[i].name);
+	}
+
+	device_complete(sl, instr);
+
+	return COMPLETE_CONTINUE;
+}
+
+/*
+ * Parse the leading options (currently only -d <device>), default the device
+ * name when none is given, and return the index of the first non-option
+ * argument (the action name), or -1 if none is present.
+ */
+static int cmd_opt(int argc, char *argv[], const char **name)
+{
+	int opt;
+
+	optind = 1;
+	*name = NULL;
+	while ((opt = getopt(argc, argv, "d:")) > 0) {
+		switch (opt) {
+		case 'd':
+			*name = optarg;
+			break;
+		}
+	}
+
+	if (argc - optind <= 0)
+		return -1;
+
+	if (*name == NULL)
+		*name = "typec0";
+
+	return optind;
+}
+
+static const struct stusb4500_action *find_action_by_name(const char *name)
+{
+	int i;
+	const struct stusb4500_action *action = NULL;
+
+	for (i = 0; i < ARRAY_SIZE(actions); i++) {
+		if (!strcmp(name, actions[i].name)) {
+			action = &actions[i];
+			break;
+		}
+	}
+
+	return action;
+}
+
+static int do_stusb4500(int argc, char *argv[])
+{
+	struct stusb4500 *stusb;
+	const char *devname = NULL;
+	const char *action_name;
+	const struct stusb4500_action *action = NULL;
+	int i, nargs;
+
+	i = cmd_opt(argc, argv, &devname);
+	if (i < 0)
+		return COMMAND_ERROR_USAGE;
+
+	action_name = argv[i++];
+	action = find_action_by_name(action_name);
+	if (!action) {
+		printf("unknown action '%s'\n", action_name);
+		return COMMAND_ERROR_USAGE;
+	}
+
+	nargs = argc - i;
+	if (nargs < action->min_args) {
+		printf("action '%s' requires: %s\n",
+		       action->name, action->help);
+		return COMMAND_ERROR_USAGE;
+	}
+
+	stusb = stusb4500_get(devname);
+	if (IS_ERR(stusb))
+		return PTR_ERR(stusb);
+
+	return action->fn(stusb, nargs, &argv[i]);
+}
+
+BAREBOX_CMD_HELP_START(stusb4500)
+BAREBOX_CMD_HELP_TEXT("Control and inspect an STUSB4500 USB-C PD sink controller")
+BAREBOX_CMD_HELP_TEXT("")
+BAREBOX_CMD_HELP_TEXT("Options:")
+BAREBOX_CMD_HELP_OPT("-d <device>", "target device (default: typec0)")
+BAREBOX_CMD_HELP_TEXT("")
+BAREBOX_CMD_HELP_TEXT("Actions:")
+BAREBOX_CMD_HELP_OPT("set_active_pdos",  "\t<npdo> set # of active PDOs")
+BAREBOX_CMD_HELP_OPT("dump_sink_pdos",   "\tdump all configured sink PDOs")
+BAREBOX_CMD_HELP_OPT("dump_source_pdos", "\tdump all configured source PDOs")
+BAREBOX_CMD_HELP_OPT("update_pdo",       "\t<id> <mv> <ma> update a sink PDO")
+BAREBOX_CMD_HELP_OPT("renegotiate",      "\tforce PD renegotiation")
+BAREBOX_CMD_HELP_OPT("contract",         "\tshow the negotiated contract")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(stusb4500)
+	.cmd		= do_stusb4500,
+	BAREBOX_CMD_DESC("STUSB4500 USB-C PD sink control")
+	BAREBOX_CMD_OPTS("[-d <device>] <action> [args...]")
+	BAREBOX_CMD_GROUP(CMD_GRP_MISC)
+	BAREBOX_CMD_COMPLETE(stusb4500_complete)
+	BAREBOX_CMD_HELP(cmd_stusb4500_help)
+BAREBOX_CMD_END
-- 
2.34.1




  parent reply	other threads:[~2026-11-14 16:21 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-14 16:19 [PATCH 0/4] usb: typec: add STUSB4500 USB-C PD sink support Giandomenico Rossi
2026-11-14 16:19 ` [PATCH 1/4] usb: typec: add typec_port_from_device() Giandomenico Rossi
2026-11-14 16:19 ` [PATCH 2/4] usb: typec: stusb4500: add USB PD fixed supply driver Giandomenico Rossi
2026-11-17 11:27   ` Sascha Hauer
2026-11-14 16:19 ` [PATCH 3/4] dt-bindings: usb: add STUSB4500 USB PD sink controller binding Giandomenico Rossi
2026-11-14 16:19 ` Giandomenico Rossi [this message]
2026-11-17 11:46   ` [PATCH 4/4] commands: stusb4500: add USB-C PD control command Sascha Hauer
2026-11-17 13:12     ` Giandomenico Rossi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261114161953.1457428-5-rossi@amelchem.com \
    --to=rossi@amelchem.com \
    --cc=barebox@lists.infradead.org \
    --cc=ntd@entidi.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox