Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include <arch/acpi.h> |
| 9 | #include <arch/acpi_device.h> |
| 10 | #include <arch/acpigen.h> |
| 11 | #include <console/console.h> |
| 12 | #include <drivers/usb/acpi/chip.h> |
| 13 | #include <stdlib.h> |
| 14 | |
| 15 | #include "chip.h" |
| 16 | #include "ec.h" |
| 17 | #include "ec_commands.h" |
| 18 | |
Karthikeyan Ramasubramanian | a95907b | 2020-04-13 18:03:29 -0600 | [diff] [blame] | 19 | #define GOOGLE_CHROMEEC_USBC_DEVICE_PARENT "CREC" |
| 20 | #define GOOGLE_CHROMEEC_USBC_DEVICE_HID "GOOG0014" |
| 21 | #define GOOGLE_CHROMEEC_USBC_DEVICE_NAME "USBC" |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 22 | |
| 23 | const char *google_chromeec_acpi_name(const struct device *dev) |
| 24 | { |
| 25 | return "EC0"; |
| 26 | } |
| 27 | |
| 28 | static const char *power_role_to_str(enum ec_pd_power_role_caps power_role) |
| 29 | { |
| 30 | switch (power_role) { |
| 31 | case EC_PD_POWER_ROLE_SOURCE: |
| 32 | return "source"; |
| 33 | case EC_PD_POWER_ROLE_SINK: |
| 34 | return "sink"; |
| 35 | case EC_PD_POWER_ROLE_DUAL: |
| 36 | return "dual"; |
| 37 | default: |
| 38 | return "unknown"; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | static const char *try_power_role_to_str(enum ec_pd_try_power_role_caps try_power_role) |
| 43 | { |
| 44 | switch (try_power_role) { |
| 45 | case EC_PD_TRY_POWER_ROLE_NONE: |
| 46 | /* |
| 47 | * This should never get returned; if there is no try-power role for a device, |
| 48 | * then the try-power-role field is not added to the DSD. Thus, this is just |
| 49 | * for completeness. |
| 50 | */ |
| 51 | return "none"; |
| 52 | case EC_PD_TRY_POWER_ROLE_SINK: |
| 53 | return "sink"; |
| 54 | case EC_PD_TRY_POWER_ROLE_SOURCE: |
| 55 | return "source"; |
| 56 | default: |
| 57 | return "unknown"; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static const char *data_role_to_str(enum ec_pd_data_role_caps data_role) |
| 62 | { |
| 63 | switch (data_role) { |
| 64 | case EC_PD_DATA_ROLE_DFP: |
| 65 | return "host"; |
| 66 | case EC_PD_DATA_ROLE_UFP: |
| 67 | return "device"; |
| 68 | case EC_PD_DATA_ROLE_DUAL: |
| 69 | return "dual"; |
| 70 | default: |
| 71 | return "unknown"; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Apparently these are supposed to be uppercase, in contrast to the other |
| 77 | * lowercase fields. |
| 78 | */ |
| 79 | static const char *port_location_to_str(enum ec_pd_port_location port_location) |
| 80 | { |
| 81 | switch (port_location) { |
| 82 | case EC_PD_PORT_LOCATION_LEFT: |
| 83 | return "LEFT"; |
| 84 | case EC_PD_PORT_LOCATION_RIGHT: |
| 85 | return "RIGHT"; |
| 86 | case EC_PD_PORT_LOCATION_BACK: |
| 87 | return "BACK"; |
| 88 | case EC_PD_PORT_LOCATION_FRONT: |
| 89 | return "FRONT"; |
| 90 | case EC_PD_PORT_LOCATION_LEFT_FRONT: |
| 91 | return "LEFT_FRONT"; |
| 92 | case EC_PD_PORT_LOCATION_LEFT_BACK: |
| 93 | return "LEFT_BACK"; |
| 94 | case EC_PD_PORT_LOCATION_RIGHT_FRONT: |
| 95 | return "RIGHT_FRONT"; |
| 96 | case EC_PD_PORT_LOCATION_RIGHT_BACK: |
| 97 | return "RIGHT_BACK"; |
| 98 | case EC_PD_PORT_LOCATION_BACK_LEFT: |
| 99 | return "BACK_LEFT"; |
| 100 | case EC_PD_PORT_LOCATION_BACK_RIGHT: |
| 101 | return "BACK_RIGHT"; |
| 102 | case EC_PD_PORT_LOCATION_UNKNOWN: /* intentional fallthrough */ |
| 103 | default: |
| 104 | return "UNKNOWN"; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /* Add port capabilities as DP properties */ |
| 109 | static void add_port_caps(struct acpi_dp *dsd, const struct usb_pd_port_caps *port_caps) |
| 110 | { |
| 111 | acpi_dp_add_string(dsd, "power-role", power_role_to_str(port_caps->power_role_cap)); |
| 112 | |
| 113 | if (port_caps->try_power_role_cap != EC_PD_TRY_POWER_ROLE_NONE) |
| 114 | acpi_dp_add_string(dsd, "try-power-role", |
| 115 | try_power_role_to_str(port_caps->try_power_role_cap)); |
| 116 | |
| 117 | acpi_dp_add_string(dsd, "data-role", data_role_to_str(port_caps->data_role_cap)); |
| 118 | acpi_dp_add_string(dsd, "port-location", port_location_to_str( |
| 119 | port_caps->port_location)); |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Helper for fill_ssdt_generator. This adds references to the USB |
| 124 | * port objects so that the consumer of this information can know |
| 125 | * whether the port supports USB2 and/or USB3. |
| 126 | */ |
| 127 | static void add_usb_port_references(struct acpi_dp *dsd, int port_number) |
| 128 | { |
| 129 | static const char usb2_port[] = "usb2-port"; |
| 130 | static const char usb3_port[] = "usb3-port"; |
| 131 | struct device *port = NULL; |
| 132 | const char *path; |
| 133 | const char *usb_port_type; |
| 134 | struct drivers_usb_acpi_config *config; |
| 135 | |
| 136 | /* |
| 137 | * Unfortunately, the acpi_dp_* API doesn't write out the data immediately, thus we need |
| 138 | * different storage areas for all of the strings, so strdup() is used for that. It is |
| 139 | * safe to use strdup() here, because the strings are generated at build-time and are |
| 140 | * guaranteed to be NUL-terminated (they come from the devicetree). |
| 141 | */ |
| 142 | while ((port = dev_find_path(port, DEVICE_PATH_USB)) != NULL) { |
| 143 | if (!port->enabled || port->path.type != DEVICE_PATH_USB) |
| 144 | continue; |
| 145 | |
| 146 | /* Looking for USB 2 & 3 port devices only */ |
| 147 | if (port->path.usb.port_type == 2) |
| 148 | usb_port_type = usb2_port; |
| 149 | else if (port->path.usb.port_type == 3) |
| 150 | usb_port_type = usb3_port; |
| 151 | else |
| 152 | continue; |
| 153 | |
| 154 | config = port->chip_info; |
| 155 | |
| 156 | /* |
| 157 | * Look at only USB Type-C ports, making sure they match the |
| 158 | * port number we're looking for (the 'token' field in 'group'). |
| 159 | * Also note that 'port_number' is 0-based, whereas the 'token' |
| 160 | * field is 1-based. |
| 161 | */ |
| 162 | if ((config->type != UPC_TYPE_C_USB2_ONLY) && |
| 163 | (config->type != UPC_TYPE_C_USB2_SS_SWITCH) && |
| 164 | (config->type != UPC_TYPE_C_USB2_SS)) |
| 165 | continue; |
| 166 | |
| 167 | if (config->group.token != (port_number + 1)) |
| 168 | continue; |
| 169 | |
| 170 | path = acpi_device_path(port); |
| 171 | if (path) { |
| 172 | path = strdup(path); |
| 173 | if (!path) |
| 174 | continue; |
| 175 | |
| 176 | acpi_dp_add_reference(dsd, usb_port_type, path); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
Matt DeVillier | 70ea3b9 | 2020-03-18 23:45:32 -0500 | [diff] [blame] | 181 | static void fill_ssdt_typec_device(int num_ports) |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 182 | { |
| 183 | struct usb_pd_port_caps port_caps; |
| 184 | char con_name[] = "CONx"; |
| 185 | struct acpi_dp *dsd; |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 186 | int rv; |
| 187 | int i; |
| 188 | |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 189 | acpigen_write_device(GOOGLE_CHROMEEC_USBC_DEVICE_NAME); |
| 190 | acpigen_write_name_string("_HID", GOOGLE_CHROMEEC_USBC_DEVICE_HID); |
| 191 | acpigen_write_name_string("_DDN", "ChromeOS EC Embedded Controller " |
| 192 | "USB Type-C Control"); |
| 193 | |
| 194 | for (i = 0; i < num_ports; ++i) { |
| 195 | rv = google_chromeec_get_pd_port_caps(i, &port_caps); |
| 196 | if (rv) |
| 197 | continue; |
| 198 | |
| 199 | con_name[3] = (char)i + '0'; |
| 200 | acpigen_write_device(con_name); |
| 201 | acpigen_write_name_integer("_ADR", i); |
| 202 | |
| 203 | /* _DSD, Device-Specific Data */ |
| 204 | dsd = acpi_dp_new_table("_DSD"); |
| 205 | |
| 206 | acpi_dp_add_integer(dsd, "port-number", i); |
| 207 | add_port_caps(dsd, &port_caps); |
| 208 | add_usb_port_references(dsd, i); |
| 209 | |
| 210 | acpi_dp_write(dsd); |
| 211 | acpigen_pop_len(); /* Device CONx */ |
| 212 | } |
| 213 | |
| 214 | acpigen_pop_len(); /* Device GOOGLE_CHROMEEC_USBC_DEVICE_NAME */ |
| 215 | } |
| 216 | |
| 217 | void google_chromeec_fill_ssdt_generator(struct device *dev) |
| 218 | { |
Matt DeVillier | 70ea3b9 | 2020-03-18 23:45:32 -0500 | [diff] [blame] | 219 | int num_ports; |
| 220 | if (google_chromeec_get_num_pd_ports(&num_ports)) |
| 221 | return; |
| 222 | |
Karthikeyan Ramasubramanian | a95907b | 2020-04-13 18:03:29 -0600 | [diff] [blame] | 223 | /* Add TypeC device under the existing device + ".CREC" scope */ |
| 224 | acpigen_write_scope(acpi_device_path_join(dev, GOOGLE_CHROMEEC_USBC_DEVICE_PARENT)); |
Matt DeVillier | 70ea3b9 | 2020-03-18 23:45:32 -0500 | [diff] [blame] | 225 | fill_ssdt_typec_device(num_ports); |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 226 | acpigen_pop_len(); /* Scope */ |
| 227 | } |