1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2024 Collabora
9 #include <dm/uclass-internal.h>
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
[])
23 devnum
= (int)dectoul(argv
[1], NULL
);
24 ret
= tcpm_get(devnum
, &currdev
);
26 log_err("Can't get TCPM %d: %d (%s)!\n", devnum
, ret
, errno_str(ret
));
27 return CMD_RET_FAILURE
;
31 log_err("TCPM device is not set!\n\n");
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
,
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
)) {
57 printf("| %2d | %-*.*s| %-*.*s| %s @ %d | status: %i\n",
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
),
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
);
83 printf("TCPM State: %s\n", state
);
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);
97 static int do_info(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
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
,
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
,
133 "list - list TCPM devices\n"
134 "tcpm dev [ID] - show or [set] operating TCPM device\n"
135 "tcpm info - dump information\n"