Merge tag 'xilinx-for-v2025.01-rc3-v2' of https://source.denx.de/u-boot/custodians...
[u-boot.git] / cmd / tcpm.c
blob39578f6ccc0bfc9a1c1451a619fcafa555a42832
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2024 Collabora
4 */
6 #include <command.h>
7 #include <errno.h>
8 #include <dm.h>
9 #include <dm/uclass-internal.h>
10 #include <usb/tcpm.h>
12 #define LIMIT_DEV 32
13 #define LIMIT_PARENT 20
15 static struct udevice *currdev;
17 static int do_dev(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
19 int devnum, ret;
21 switch (argc) {
22 case 2:
23 devnum = (int)dectoul(argv[1], NULL);
24 ret = tcpm_get(devnum, &currdev);
25 if (ret) {
26 log_err("Can't get TCPM %d: %d (%s)!\n", devnum, ret, errno_str(ret));
27 return CMD_RET_FAILURE;
29 case 1:
30 if (!currdev) {
31 log_err("TCPM device is not set!\n\n");
32 return CMD_RET_USAGE;
35 printf("dev: %d @ %s\n", dev_seq(currdev), currdev->name);
38 return CMD_RET_SUCCESS;
41 static int do_list(struct cmd_tbl *cmdtp, int flag, int argc,
42 char *const argv[])
44 struct udevice *dev;
45 int ret, err = 0;
47 printf("| ID | %-*.*s| %-*.*s| %s @ %s\n",
48 LIMIT_DEV, LIMIT_DEV, "Name",
49 LIMIT_PARENT, LIMIT_PARENT, "Parent name",
50 "Parent uclass", "seq");
52 for (ret = uclass_first_device_check(UCLASS_TCPM, &dev); dev;
53 ret = uclass_next_device_check(&dev)) {
54 if (ret)
55 err = ret;
57 printf("| %2d | %-*.*s| %-*.*s| %s @ %d | status: %i\n",
58 dev_seq(dev),
59 LIMIT_DEV, LIMIT_DEV, dev->name,
60 LIMIT_PARENT, LIMIT_PARENT, dev->parent->name,
61 dev_get_uclass_name(dev->parent), dev_seq(dev->parent),
62 ret);
65 if (err)
66 return CMD_RET_FAILURE;
68 return CMD_RET_SUCCESS;
71 int do_print_info(struct udevice *dev)
73 enum typec_orientation orientation = tcpm_get_orientation(dev);
74 const char *state = tcpm_get_state(dev);
75 int pd_rev = tcpm_get_pd_rev(dev);
76 int mv = tcpm_get_voltage(dev);
77 int ma = tcpm_get_current(dev);
78 enum typec_role pwr_role = tcpm_get_pwr_role(dev);
79 enum typec_data_role data_role = tcpm_get_data_role(dev);
80 bool connected = tcpm_is_connected(dev);
82 if (!connected) {
83 printf("TCPM State: %s\n", state);
84 return 0;
87 printf("Orientation: %s\n", typec_orientation_name[orientation]);
88 printf("PD Revision: %s\n", typec_pd_rev_name[pd_rev]);
89 printf("Power Role: %s\n", typec_role_name[pwr_role]);
90 printf("Data Role: %s\n", typec_data_role_name[data_role]);
91 printf("Voltage: %2d.%03d V\n", mv / 1000, mv % 1000);
92 printf("Current: %2d.%03d A\n", ma / 1000, ma % 1000);
94 return 0;
97 static int do_info(struct cmd_tbl *cmdtp, int flag, int argc,
98 char *const argv[])
100 if (!currdev) {
101 printf("First, set the TCPM device!\n");
102 return CMD_RET_USAGE;
105 return do_print_info(currdev);
108 static struct cmd_tbl subcmd[] = {
109 U_BOOT_CMD_MKENT(dev, 2, 1, do_dev, "", ""),
110 U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""),
111 U_BOOT_CMD_MKENT(info, 1, 1, do_info, "", ""),
114 static int do_tcpm(struct cmd_tbl *cmdtp, int flag, int argc,
115 char *const argv[])
117 struct cmd_tbl *cmd;
119 argc--;
120 argv++;
122 cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd));
123 if (!cmd || argc > cmd->maxargs)
124 return CMD_RET_USAGE;
126 return cmd->cmd(cmdtp, flag, argc, argv);
129 /**************************************************/
131 U_BOOT_CMD(tcpm, CONFIG_SYS_MAXARGS, 1, do_tcpm,
132 "TCPM sub-system",
133 "list - list TCPM devices\n"
134 "tcpm dev [ID] - show or [set] operating TCPM device\n"
135 "tcpm info - dump information\n"