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> |
Rajat Jain | 962c788 | 2020-04-01 17:24:28 -0700 | [diff] [blame] | 11 | #include <arch/acpigen_ps2_keybd.h> |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 12 | #include <console/console.h> |
| 13 | #include <drivers/usb/acpi/chip.h> |
| 14 | #include <stdlib.h> |
| 15 | |
| 16 | #include "chip.h" |
| 17 | #include "ec.h" |
| 18 | #include "ec_commands.h" |
| 19 | |
Karthikeyan Ramasubramanian | a95907b | 2020-04-13 18:03:29 -0600 | [diff] [blame] | 20 | #define GOOGLE_CHROMEEC_USBC_DEVICE_PARENT "CREC" |
| 21 | #define GOOGLE_CHROMEEC_USBC_DEVICE_HID "GOOG0014" |
| 22 | #define GOOGLE_CHROMEEC_USBC_DEVICE_NAME "USBC" |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 23 | |
| 24 | const char *google_chromeec_acpi_name(const struct device *dev) |
| 25 | { |
| 26 | return "EC0"; |
| 27 | } |
| 28 | |
| 29 | static const char *power_role_to_str(enum ec_pd_power_role_caps power_role) |
| 30 | { |
| 31 | switch (power_role) { |
| 32 | case EC_PD_POWER_ROLE_SOURCE: |
| 33 | return "source"; |
| 34 | case EC_PD_POWER_ROLE_SINK: |
| 35 | return "sink"; |
| 36 | case EC_PD_POWER_ROLE_DUAL: |
| 37 | return "dual"; |
| 38 | default: |
| 39 | return "unknown"; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | static const char *try_power_role_to_str(enum ec_pd_try_power_role_caps try_power_role) |
| 44 | { |
| 45 | switch (try_power_role) { |
| 46 | case EC_PD_TRY_POWER_ROLE_NONE: |
| 47 | /* |
| 48 | * This should never get returned; if there is no try-power role for a device, |
| 49 | * then the try-power-role field is not added to the DSD. Thus, this is just |
| 50 | * for completeness. |
| 51 | */ |
| 52 | return "none"; |
| 53 | case EC_PD_TRY_POWER_ROLE_SINK: |
| 54 | return "sink"; |
| 55 | case EC_PD_TRY_POWER_ROLE_SOURCE: |
| 56 | return "source"; |
| 57 | default: |
| 58 | return "unknown"; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | static const char *data_role_to_str(enum ec_pd_data_role_caps data_role) |
| 63 | { |
| 64 | switch (data_role) { |
| 65 | case EC_PD_DATA_ROLE_DFP: |
| 66 | return "host"; |
| 67 | case EC_PD_DATA_ROLE_UFP: |
| 68 | return "device"; |
| 69 | case EC_PD_DATA_ROLE_DUAL: |
| 70 | return "dual"; |
| 71 | default: |
| 72 | return "unknown"; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Apparently these are supposed to be uppercase, in contrast to the other |
| 78 | * lowercase fields. |
| 79 | */ |
| 80 | static const char *port_location_to_str(enum ec_pd_port_location port_location) |
| 81 | { |
| 82 | switch (port_location) { |
| 83 | case EC_PD_PORT_LOCATION_LEFT: |
| 84 | return "LEFT"; |
| 85 | case EC_PD_PORT_LOCATION_RIGHT: |
| 86 | return "RIGHT"; |
| 87 | case EC_PD_PORT_LOCATION_BACK: |
| 88 | return "BACK"; |
| 89 | case EC_PD_PORT_LOCATION_FRONT: |
| 90 | return "FRONT"; |
| 91 | case EC_PD_PORT_LOCATION_LEFT_FRONT: |
| 92 | return "LEFT_FRONT"; |
| 93 | case EC_PD_PORT_LOCATION_LEFT_BACK: |
| 94 | return "LEFT_BACK"; |
| 95 | case EC_PD_PORT_LOCATION_RIGHT_FRONT: |
| 96 | return "RIGHT_FRONT"; |
| 97 | case EC_PD_PORT_LOCATION_RIGHT_BACK: |
| 98 | return "RIGHT_BACK"; |
| 99 | case EC_PD_PORT_LOCATION_BACK_LEFT: |
| 100 | return "BACK_LEFT"; |
| 101 | case EC_PD_PORT_LOCATION_BACK_RIGHT: |
| 102 | return "BACK_RIGHT"; |
| 103 | case EC_PD_PORT_LOCATION_UNKNOWN: /* intentional fallthrough */ |
| 104 | default: |
| 105 | return "UNKNOWN"; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /* Add port capabilities as DP properties */ |
| 110 | static void add_port_caps(struct acpi_dp *dsd, const struct usb_pd_port_caps *port_caps) |
| 111 | { |
| 112 | acpi_dp_add_string(dsd, "power-role", power_role_to_str(port_caps->power_role_cap)); |
| 113 | |
| 114 | if (port_caps->try_power_role_cap != EC_PD_TRY_POWER_ROLE_NONE) |
| 115 | acpi_dp_add_string(dsd, "try-power-role", |
| 116 | try_power_role_to_str(port_caps->try_power_role_cap)); |
| 117 | |
| 118 | acpi_dp_add_string(dsd, "data-role", data_role_to_str(port_caps->data_role_cap)); |
| 119 | acpi_dp_add_string(dsd, "port-location", port_location_to_str( |
| 120 | port_caps->port_location)); |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Helper for fill_ssdt_generator. This adds references to the USB |
| 125 | * port objects so that the consumer of this information can know |
| 126 | * whether the port supports USB2 and/or USB3. |
| 127 | */ |
| 128 | static void add_usb_port_references(struct acpi_dp *dsd, int port_number) |
| 129 | { |
| 130 | static const char usb2_port[] = "usb2-port"; |
| 131 | static const char usb3_port[] = "usb3-port"; |
| 132 | struct device *port = NULL; |
| 133 | const char *path; |
| 134 | const char *usb_port_type; |
| 135 | struct drivers_usb_acpi_config *config; |
| 136 | |
| 137 | /* |
| 138 | * Unfortunately, the acpi_dp_* API doesn't write out the data immediately, thus we need |
| 139 | * different storage areas for all of the strings, so strdup() is used for that. It is |
| 140 | * safe to use strdup() here, because the strings are generated at build-time and are |
| 141 | * guaranteed to be NUL-terminated (they come from the devicetree). |
| 142 | */ |
| 143 | while ((port = dev_find_path(port, DEVICE_PATH_USB)) != NULL) { |
| 144 | if (!port->enabled || port->path.type != DEVICE_PATH_USB) |
| 145 | continue; |
| 146 | |
| 147 | /* Looking for USB 2 & 3 port devices only */ |
| 148 | if (port->path.usb.port_type == 2) |
| 149 | usb_port_type = usb2_port; |
| 150 | else if (port->path.usb.port_type == 3) |
| 151 | usb_port_type = usb3_port; |
| 152 | else |
| 153 | continue; |
| 154 | |
| 155 | config = port->chip_info; |
| 156 | |
| 157 | /* |
| 158 | * Look at only USB Type-C ports, making sure they match the |
| 159 | * port number we're looking for (the 'token' field in 'group'). |
| 160 | * Also note that 'port_number' is 0-based, whereas the 'token' |
| 161 | * field is 1-based. |
| 162 | */ |
| 163 | if ((config->type != UPC_TYPE_C_USB2_ONLY) && |
| 164 | (config->type != UPC_TYPE_C_USB2_SS_SWITCH) && |
| 165 | (config->type != UPC_TYPE_C_USB2_SS)) |
| 166 | continue; |
| 167 | |
| 168 | if (config->group.token != (port_number + 1)) |
| 169 | continue; |
| 170 | |
| 171 | path = acpi_device_path(port); |
| 172 | if (path) { |
| 173 | path = strdup(path); |
| 174 | if (!path) |
| 175 | continue; |
| 176 | |
| 177 | acpi_dp_add_reference(dsd, usb_port_type, path); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
Rajat Jain | 962c788 | 2020-04-01 17:24:28 -0700 | [diff] [blame] | 182 | static void fill_ssdt_typec_device(struct device *dev) |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 183 | { |
| 184 | struct usb_pd_port_caps port_caps; |
| 185 | char con_name[] = "CONx"; |
| 186 | struct acpi_dp *dsd; |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 187 | int rv; |
Rajat Jain | 962c788 | 2020-04-01 17:24:28 -0700 | [diff] [blame] | 188 | int i, num_ports; |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 189 | |
Rajat Jain | 962c788 | 2020-04-01 17:24:28 -0700 | [diff] [blame] | 190 | if (google_chromeec_get_num_pd_ports(&num_ports)) |
| 191 | return; |
| 192 | |
| 193 | /* Add TypeC device under the existing device + ".CREC" scope */ |
| 194 | acpigen_write_scope(acpi_device_path_join(dev, GOOGLE_CHROMEEC_USBC_DEVICE_PARENT)); |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 195 | acpigen_write_device(GOOGLE_CHROMEEC_USBC_DEVICE_NAME); |
| 196 | acpigen_write_name_string("_HID", GOOGLE_CHROMEEC_USBC_DEVICE_HID); |
| 197 | acpigen_write_name_string("_DDN", "ChromeOS EC Embedded Controller " |
| 198 | "USB Type-C Control"); |
| 199 | |
| 200 | for (i = 0; i < num_ports; ++i) { |
| 201 | rv = google_chromeec_get_pd_port_caps(i, &port_caps); |
| 202 | if (rv) |
| 203 | continue; |
| 204 | |
| 205 | con_name[3] = (char)i + '0'; |
| 206 | acpigen_write_device(con_name); |
| 207 | acpigen_write_name_integer("_ADR", i); |
| 208 | |
| 209 | /* _DSD, Device-Specific Data */ |
| 210 | dsd = acpi_dp_new_table("_DSD"); |
| 211 | |
| 212 | acpi_dp_add_integer(dsd, "port-number", i); |
| 213 | add_port_caps(dsd, &port_caps); |
| 214 | add_usb_port_references(dsd, i); |
| 215 | |
| 216 | acpi_dp_write(dsd); |
| 217 | acpigen_pop_len(); /* Device CONx */ |
| 218 | } |
| 219 | |
| 220 | acpigen_pop_len(); /* Device GOOGLE_CHROMEEC_USBC_DEVICE_NAME */ |
Rajat Jain | 962c788 | 2020-04-01 17:24:28 -0700 | [diff] [blame] | 221 | acpigen_pop_len(); /* Scope */ |
| 222 | } |
| 223 | |
| 224 | static const enum ps2_action_key ps2_enum_val[] = { |
| 225 | [TK_ABSENT] = PS2_KEY_ABSENT, |
| 226 | [TK_BACK] = PS2_KEY_BACK, |
| 227 | [TK_FORWARD] = PS2_KEY_FORWARD, |
| 228 | [TK_REFRESH] = PS2_KEY_REFRESH, |
| 229 | [TK_FULLSCREEN] = PS2_KEY_FULLSCREEN, |
| 230 | [TK_OVERVIEW] = PS2_KEY_OVERVIEW, |
| 231 | [TK_BRIGHTNESS_DOWN] = PS2_KEY_BRIGHTNESS_DOWN, |
| 232 | [TK_BRIGHTNESS_UP] = PS2_KEY_BRIGHTNESS_UP, |
| 233 | [TK_VOL_MUTE] = PS2_KEY_VOL_MUTE, |
| 234 | [TK_VOL_DOWN] = PS2_KEY_VOL_DOWN, |
| 235 | [TK_VOL_UP] = PS2_KEY_VOL_UP, |
| 236 | [TK_SNAPSHOT] = PS2_KEY_SNAPSHOT, |
| 237 | [TK_PRIVACY_SCRN_TOGGLE] = PS2_KEY_PRIVACY_SCRN_TOGGLE, |
| 238 | [TK_KBD_BKLIGHT_DOWN] = PS2_KEY_KBD_BKLIGHT_DOWN, |
| 239 | [TK_KBD_BKLIGHT_UP] = PS2_KEY_KBD_BKLIGHT_UP, |
| 240 | [TK_PLAY_PAUSE] = PS2_KEY_PLAY_PAUSE, |
| 241 | [TK_NEXT_TRACK] = PS2_KEY_NEXT_TRACK, |
| 242 | [TK_PREV_TRACK] = PS2_KEY_PREV_TRACK, |
| 243 | }; |
| 244 | |
| 245 | static void fill_ssdt_ps2_keyboard(struct device *dev) |
| 246 | { |
| 247 | uint8_t i; |
| 248 | struct ec_response_keybd_config keybd = {}; |
| 249 | enum ps2_action_key ps2_action_keys[MAX_TOP_ROW_KEYS] = {}; |
| 250 | |
| 251 | if (google_chromeec_get_keybd_config(&keybd) || |
| 252 | !keybd.num_top_row_keys || |
| 253 | keybd.num_top_row_keys > MAX_TOP_ROW_KEYS) { |
| 254 | printk(BIOS_ERR, "PS2K: Bad resp from EC. Vivaldi disabled!\n"); |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | /* Convert enum action_key values to enum ps2_action_key values */ |
| 259 | for (i = 0; i < keybd.num_top_row_keys; i++) |
| 260 | ps2_action_keys[i] = ps2_enum_val[keybd.action_keys[i]]; |
| 261 | |
| 262 | acpigen_ps2_keyboard_dsd("_SB.PCI0.PS2K", keybd.num_top_row_keys, |
| 263 | ps2_action_keys, |
| 264 | !!(keybd.capabilities & KEYBD_CAP_FUNCTION_KEYS), |
| 265 | !!(keybd.capabilities & KEYBD_CAP_NUMERIC_KEYPAD), |
| 266 | !!(keybd.capabilities & KEYBD_CAP_SCRNLOCK_KEY)); |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void google_chromeec_fill_ssdt_generator(struct device *dev) |
| 270 | { |
Rajat Jain | 962c788 | 2020-04-01 17:24:28 -0700 | [diff] [blame] | 271 | if (!dev->enabled) |
Matt DeVillier | 70ea3b9 | 2020-03-18 23:45:32 -0500 | [diff] [blame] | 272 | return; |
| 273 | |
Rajat Jain | 962c788 | 2020-04-01 17:24:28 -0700 | [diff] [blame] | 274 | fill_ssdt_typec_device(dev); |
| 275 | fill_ssdt_ps2_keyboard(dev); |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 276 | } |