Sebastian Reichel | 3dd2332 | 2024-10-15 17:26:43 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2024 Collabora |
| 4 | */ |
| 5 | |
| 6 | #include <command.h> |
| 7 | #include <errno.h> |
| 8 | #include <dm.h> |
| 9 | #include <dm/uclass-internal.h> |
| 10 | #include <usb/tcpm.h> |
| 11 | |
| 12 | #define LIMIT_DEV 32 |
| 13 | #define LIMIT_PARENT 20 |
| 14 | |
| 15 | static struct udevice *currdev; |
| 16 | |
| 17 | static int do_dev(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
| 18 | { |
| 19 | int devnum, ret; |
| 20 | |
| 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; |
| 28 | } |
| 29 | case 1: |
| 30 | if (!currdev) { |
| 31 | log_err("TCPM device is not set!\n\n"); |
| 32 | return CMD_RET_USAGE; |
| 33 | } |
| 34 | |
| 35 | printf("dev: %d @ %s\n", dev_seq(currdev), currdev->name); |
| 36 | } |
| 37 | |
| 38 | return CMD_RET_SUCCESS; |
| 39 | } |
| 40 | |
| 41 | static int do_list(struct cmd_tbl *cmdtp, int flag, int argc, |
| 42 | char *const argv[]) |
| 43 | { |
| 44 | struct udevice *dev; |
| 45 | int ret, err = 0; |
| 46 | |
| 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"); |
| 51 | |
| 52 | for (ret = uclass_first_device_check(UCLASS_TCPM, &dev); dev; |
| 53 | ret = uclass_next_device_check(&dev)) { |
| 54 | if (ret) |
| 55 | err = ret; |
| 56 | |
| 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); |
| 63 | } |
| 64 | |
| 65 | if (err) |
| 66 | return CMD_RET_FAILURE; |
| 67 | |
| 68 | return CMD_RET_SUCCESS; |
| 69 | } |
| 70 | |
| 71 | int do_print_info(struct udevice *dev) |
| 72 | { |
| 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); |
| 81 | |
| 82 | if (!connected) { |
| 83 | printf("TCPM State: %s\n", state); |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 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); |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static int do_info(struct cmd_tbl *cmdtp, int flag, int argc, |
| 98 | char *const argv[]) |
| 99 | { |
| 100 | if (!currdev) { |
| 101 | printf("First, set the TCPM device!\n"); |
| 102 | return CMD_RET_USAGE; |
| 103 | } |
| 104 | |
| 105 | return do_print_info(currdev); |
| 106 | } |
| 107 | |
| 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, "", ""), |
| 112 | }; |
| 113 | |
| 114 | static int do_tcpm(struct cmd_tbl *cmdtp, int flag, int argc, |
| 115 | char *const argv[]) |
| 116 | { |
| 117 | struct cmd_tbl *cmd; |
| 118 | |
| 119 | argc--; |
| 120 | argv++; |
| 121 | |
| 122 | cmd = find_cmd_tbl(argv[0], subcmd, ARRAY_SIZE(subcmd)); |
| 123 | if (!cmd || argc > cmd->maxargs) |
| 124 | return CMD_RET_USAGE; |
| 125 | |
| 126 | return cmd->cmd(cmdtp, flag, argc, argv); |
| 127 | } |
| 128 | |
| 129 | /**************************************************/ |
| 130 | |
| 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" |
| 136 | ); |