Tim Wawrzynczak | 6da8213 | 2020-05-19 12:46:04 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 2 | |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 3 | #include <acpi/acpi.h> |
| 4 | #include <acpi/acpi_device.h> |
Won Chung | 667471b | 2021-11-15 21:19:51 +0000 | [diff] [blame] | 5 | #include <acpi/acpi_pld.h> |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 6 | #include <acpi/acpigen.h> |
| 7 | #include <acpi/acpigen_ps2_keybd.h> |
Tim Wawrzynczak | 6da8213 | 2020-05-19 12:46:04 -0600 | [diff] [blame] | 8 | #include <acpi/acpigen_usb.h> |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 9 | #include <console/console.h> |
| 10 | #include <drivers/usb/acpi/chip.h> |
John Zhao | eec3e3b | 2021-01-08 22:33:04 -0800 | [diff] [blame] | 11 | #include <drivers/intel/usb4/retimer/retimer.h> |
Tim Wawrzynczak | 93d7bcb | 2020-05-29 15:44:25 -0600 | [diff] [blame] | 12 | #include <ec/google/common/dptf.h> |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 13 | |
| 14 | #include "chip.h" |
| 15 | #include "ec.h" |
| 16 | #include "ec_commands.h" |
| 17 | |
Karthikeyan Ramasubramanian | a95907b | 2020-04-13 18:03:29 -0600 | [diff] [blame] | 18 | #define GOOGLE_CHROMEEC_USBC_DEVICE_HID "GOOG0014" |
| 19 | #define GOOGLE_CHROMEEC_USBC_DEVICE_NAME "USBC" |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 20 | |
| 21 | const char *google_chromeec_acpi_name(const struct device *dev) |
| 22 | { |
Furquan Shaikh | eec30f7 | 2020-04-20 16:38:21 -0700 | [diff] [blame] | 23 | /* |
| 24 | * Chrome EC device (CREC - GOOG0004) is really a child of EC device (EC - PNP0C09) in |
| 25 | * ACPI tables. However, in coreboot device tree, there is no separate chip/device for |
| 26 | * EC0. Thus, Chrome EC device needs to return "EC0.CREC" as the ACPI name so that the |
| 27 | * callers can get the correct acpi device path/scope for this device. |
| 28 | * |
| 29 | * If we ever enable a separate driver for generating AML for EC0 device, then this |
| 30 | * function needs to be updated to return "CREC". |
| 31 | */ |
| 32 | return "EC0.CREC"; |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Tim Wawrzynczak | 6da8213 | 2020-05-19 12:46:04 -0600 | [diff] [blame] | 35 | /* |
| 36 | * Helper for fill_ssdt_generator. This adds references to the USB |
| 37 | * port objects so that the consumer of this information can know |
| 38 | * whether the port supports USB2 and/or USB3. |
| 39 | */ |
| 40 | static void get_usb_port_references(int port_number, struct device **usb2_port, |
| 41 | struct device **usb3_port, struct device **usb4_port) |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 42 | { |
Tim Wawrzynczak | 6da8213 | 2020-05-19 12:46:04 -0600 | [diff] [blame] | 43 | struct drivers_usb_acpi_config *config; |
| 44 | struct device *port = NULL; |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 45 | |
Tim Wawrzynczak | 6da8213 | 2020-05-19 12:46:04 -0600 | [diff] [blame] | 46 | /* Search through the devicetree for matching USB Type-C ports */ |
| 47 | while ((port = dev_find_path(port, DEVICE_PATH_USB)) != NULL) { |
| 48 | if (!port->enabled || port->path.type != DEVICE_PATH_USB) |
| 49 | continue; |
| 50 | |
| 51 | config = port->chip_info; |
| 52 | |
| 53 | /* Look at only USB Type-C ports */ |
| 54 | if ((config->type != UPC_TYPE_C_USB2_ONLY) && |
| 55 | (config->type != UPC_TYPE_C_USB2_SS_SWITCH) && |
| 56 | (config->type != UPC_TYPE_C_USB2_SS)) |
| 57 | continue; |
| 58 | |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 59 | /* |
Tim Wawrzynczak | 6da8213 | 2020-05-19 12:46:04 -0600 | [diff] [blame] | 60 | * Check for a matching port number (the 'token' field in 'group'). Note that |
| 61 | * 'port_number' is 0-based, whereas the 'token' field is 1-based. |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 62 | */ |
Won Chung | f6bd278 | 2022-01-25 22:22:31 +0000 | [diff] [blame] | 63 | int group_token; |
| 64 | if (config->use_custom_pld) |
| 65 | group_token = config->custom_pld.group.token; |
| 66 | else |
| 67 | group_token = config->group.token; |
| 68 | if (group_token != (port_number + 1)) |
Tim Wawrzynczak | 6da8213 | 2020-05-19 12:46:04 -0600 | [diff] [blame] | 69 | continue; |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 70 | |
Tim Wawrzynczak | 6da8213 | 2020-05-19 12:46:04 -0600 | [diff] [blame] | 71 | switch (port->path.usb.port_type) { |
| 72 | case 2: |
| 73 | *usb2_port = port; |
| 74 | break; |
| 75 | case 3: |
| 76 | *usb3_port = port; |
| 77 | break; |
| 78 | case 4: |
| 79 | *usb4_port = port; |
| 80 | break; |
| 81 | default: |
| 82 | break; |
| 83 | } |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Apparently these are supposed to be uppercase, in contrast to the other |
| 89 | * lowercase fields. |
| 90 | */ |
| 91 | static const char *port_location_to_str(enum ec_pd_port_location port_location) |
| 92 | { |
| 93 | switch (port_location) { |
| 94 | case EC_PD_PORT_LOCATION_LEFT: |
| 95 | return "LEFT"; |
| 96 | case EC_PD_PORT_LOCATION_RIGHT: |
| 97 | return "RIGHT"; |
| 98 | case EC_PD_PORT_LOCATION_BACK: |
| 99 | return "BACK"; |
| 100 | case EC_PD_PORT_LOCATION_FRONT: |
| 101 | return "FRONT"; |
| 102 | case EC_PD_PORT_LOCATION_LEFT_FRONT: |
| 103 | return "LEFT_FRONT"; |
| 104 | case EC_PD_PORT_LOCATION_LEFT_BACK: |
| 105 | return "LEFT_BACK"; |
| 106 | case EC_PD_PORT_LOCATION_RIGHT_FRONT: |
| 107 | return "RIGHT_FRONT"; |
| 108 | case EC_PD_PORT_LOCATION_RIGHT_BACK: |
| 109 | return "RIGHT_BACK"; |
| 110 | case EC_PD_PORT_LOCATION_BACK_LEFT: |
| 111 | return "BACK_LEFT"; |
| 112 | case EC_PD_PORT_LOCATION_BACK_RIGHT: |
| 113 | return "BACK_RIGHT"; |
| 114 | case EC_PD_PORT_LOCATION_UNKNOWN: /* intentional fallthrough */ |
| 115 | default: |
| 116 | return "UNKNOWN"; |
| 117 | } |
| 118 | } |
| 119 | |
Tim Wawrzynczak | 6da8213 | 2020-05-19 12:46:04 -0600 | [diff] [blame] | 120 | static struct usb_pd_port_caps port_caps; |
| 121 | static void add_port_location(struct acpi_dp *dsd, int port_number) |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 122 | { |
Tim Wawrzynczak | e7881ed | 2020-09-30 13:12:26 -0600 | [diff] [blame] | 123 | acpi_dp_add_string(dsd, "port-location", port_location_to_str(port_caps.port_location)); |
Tim Wawrzynczak | 16ef59e | 2020-06-01 20:07:45 -0600 | [diff] [blame] | 124 | } |
| 125 | |
Won Chung | 667471b | 2021-11-15 21:19:51 +0000 | [diff] [blame] | 126 | static void get_pld_from_usb_ports(struct acpi_pld *pld, |
| 127 | struct device *usb2_port, struct device *usb3_port, |
| 128 | struct device *usb4_port) |
| 129 | { |
| 130 | struct drivers_usb_acpi_config *config = NULL; |
| 131 | |
| 132 | if (usb4_port) |
| 133 | config = usb4_port->chip_info; |
| 134 | else if (usb3_port) |
| 135 | config = usb3_port->chip_info; |
| 136 | else if (usb2_port) |
| 137 | config = usb2_port->chip_info; |
| 138 | |
| 139 | if (config) { |
| 140 | if (config->use_custom_pld) |
| 141 | *pld = config->custom_pld; |
| 142 | else |
| 143 | acpi_pld_fill_usb(pld, config->type, &config->group); |
| 144 | } |
| 145 | } |
| 146 | |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 147 | static void fill_ssdt_typec_device(const struct device *dev) |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 148 | { |
Tim Wawrzynczak | e7881ed | 2020-09-30 13:12:26 -0600 | [diff] [blame] | 149 | struct ec_google_chromeec_config *config = dev->chip_info; |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 150 | int rv; |
Divya Sasidharan | 49d74de | 2019-10-22 13:41:15 -0700 | [diff] [blame] | 151 | int i; |
Prashant Malani | 20f836b | 2022-01-19 18:58:44 +0000 | [diff] [blame] | 152 | unsigned int num_ports = 0; |
Tim Wawrzynczak | 6da8213 | 2020-05-19 12:46:04 -0600 | [diff] [blame] | 153 | struct device *usb2_port; |
| 154 | struct device *usb3_port; |
| 155 | struct device *usb4_port; |
Won Chung | 667471b | 2021-11-15 21:19:51 +0000 | [diff] [blame] | 156 | struct acpi_pld pld = {0}; |
Prashant Malani | 20f836b | 2022-01-19 18:58:44 +0000 | [diff] [blame] | 157 | uint32_t pcap_mask = 0; |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 158 | |
Prashant Malani | 20f836b | 2022-01-19 18:58:44 +0000 | [diff] [blame] | 159 | rv = google_chromeec_get_num_pd_ports(&num_ports); |
| 160 | if (rv || num_ports == 0) |
| 161 | return; |
| 162 | |
| 163 | /* If we can't get port caps, we shouldn't bother creating a device. */ |
| 164 | rv = google_chromeec_get_cmd_versions(EC_CMD_GET_PD_PORT_CAPS, &pcap_mask); |
| 165 | if (rv || pcap_mask == 0) |
Rajat Jain | 962c788 | 2020-04-01 17:24:28 -0700 | [diff] [blame] | 166 | return; |
| 167 | |
Furquan Shaikh | eec30f7 | 2020-04-20 16:38:21 -0700 | [diff] [blame] | 168 | acpigen_write_scope(acpi_device_path(dev)); |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 169 | acpigen_write_device(GOOGLE_CHROMEEC_USBC_DEVICE_NAME); |
| 170 | acpigen_write_name_string("_HID", GOOGLE_CHROMEEC_USBC_DEVICE_HID); |
| 171 | acpigen_write_name_string("_DDN", "ChromeOS EC Embedded Controller " |
| 172 | "USB Type-C Control"); |
| 173 | |
| 174 | for (i = 0; i < num_ports; ++i) { |
| 175 | rv = google_chromeec_get_pd_port_caps(i, &port_caps); |
| 176 | if (rv) |
| 177 | continue; |
| 178 | |
Tim Wawrzynczak | 6da8213 | 2020-05-19 12:46:04 -0600 | [diff] [blame] | 179 | usb2_port = NULL; |
| 180 | usb3_port = NULL; |
| 181 | usb4_port = NULL; |
| 182 | get_usb_port_references(i, &usb2_port, &usb3_port, &usb4_port); |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 183 | |
Won Chung | 667471b | 2021-11-15 21:19:51 +0000 | [diff] [blame] | 184 | get_pld_from_usb_ports(&pld, usb2_port, usb3_port, usb4_port); |
| 185 | |
Tim Wawrzynczak | e7881ed | 2020-09-30 13:12:26 -0600 | [diff] [blame] | 186 | struct typec_connector_class_config typec_config = { |
Tim Wawrzynczak | 6da8213 | 2020-05-19 12:46:04 -0600 | [diff] [blame] | 187 | .power_role = port_caps.power_role_cap, |
| 188 | .try_power_role = port_caps.try_power_role_cap, |
| 189 | .data_role = port_caps.data_role_cap, |
| 190 | .usb2_port = usb2_port, |
| 191 | .usb3_port = usb3_port, |
| 192 | .usb4_port = usb4_port, |
Tim Wawrzynczak | e7881ed | 2020-09-30 13:12:26 -0600 | [diff] [blame] | 193 | .orientation_switch = config->mux_conn[i], |
| 194 | .usb_role_switch = config->mux_conn[i], |
| 195 | .mode_switch = config->mux_conn[i], |
Won Chung | 667471b | 2021-11-15 21:19:51 +0000 | [diff] [blame] | 196 | .pld = &pld, |
Tim Wawrzynczak | 6da8213 | 2020-05-19 12:46:04 -0600 | [diff] [blame] | 197 | }; |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 198 | |
Tim Wawrzynczak | e7881ed | 2020-09-30 13:12:26 -0600 | [diff] [blame] | 199 | acpigen_write_typec_connector(&typec_config, i, add_port_location); |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | acpigen_pop_len(); /* Device GOOGLE_CHROMEEC_USBC_DEVICE_NAME */ |
Rajat Jain | 962c788 | 2020-04-01 17:24:28 -0700 | [diff] [blame] | 203 | acpigen_pop_len(); /* Scope */ |
| 204 | } |
| 205 | |
| 206 | static const enum ps2_action_key ps2_enum_val[] = { |
| 207 | [TK_ABSENT] = PS2_KEY_ABSENT, |
| 208 | [TK_BACK] = PS2_KEY_BACK, |
| 209 | [TK_FORWARD] = PS2_KEY_FORWARD, |
| 210 | [TK_REFRESH] = PS2_KEY_REFRESH, |
| 211 | [TK_FULLSCREEN] = PS2_KEY_FULLSCREEN, |
| 212 | [TK_OVERVIEW] = PS2_KEY_OVERVIEW, |
| 213 | [TK_BRIGHTNESS_DOWN] = PS2_KEY_BRIGHTNESS_DOWN, |
| 214 | [TK_BRIGHTNESS_UP] = PS2_KEY_BRIGHTNESS_UP, |
| 215 | [TK_VOL_MUTE] = PS2_KEY_VOL_MUTE, |
| 216 | [TK_VOL_DOWN] = PS2_KEY_VOL_DOWN, |
| 217 | [TK_VOL_UP] = PS2_KEY_VOL_UP, |
| 218 | [TK_SNAPSHOT] = PS2_KEY_SNAPSHOT, |
| 219 | [TK_PRIVACY_SCRN_TOGGLE] = PS2_KEY_PRIVACY_SCRN_TOGGLE, |
| 220 | [TK_KBD_BKLIGHT_DOWN] = PS2_KEY_KBD_BKLIGHT_DOWN, |
| 221 | [TK_KBD_BKLIGHT_UP] = PS2_KEY_KBD_BKLIGHT_UP, |
| 222 | [TK_PLAY_PAUSE] = PS2_KEY_PLAY_PAUSE, |
| 223 | [TK_NEXT_TRACK] = PS2_KEY_NEXT_TRACK, |
| 224 | [TK_PREV_TRACK] = PS2_KEY_PREV_TRACK, |
Scott Chao | 18141d8c | 2021-07-30 10:40:57 +0800 | [diff] [blame] | 225 | [TK_KBD_BKLIGHT_TOGGLE] = PS2_KEY_KBD_BKLIGHT_TOGGLE, |
| 226 | [TK_MICMUTE] = PS2_KEY_MICMUTE, |
Rajat Jain | 962c788 | 2020-04-01 17:24:28 -0700 | [diff] [blame] | 227 | }; |
| 228 | |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 229 | static void fill_ssdt_ps2_keyboard(const struct device *dev) |
Rajat Jain | 962c788 | 2020-04-01 17:24:28 -0700 | [diff] [blame] | 230 | { |
| 231 | uint8_t i; |
| 232 | struct ec_response_keybd_config keybd = {}; |
| 233 | enum ps2_action_key ps2_action_keys[MAX_TOP_ROW_KEYS] = {}; |
| 234 | |
| 235 | if (google_chromeec_get_keybd_config(&keybd) || |
| 236 | !keybd.num_top_row_keys || |
| 237 | keybd.num_top_row_keys > MAX_TOP_ROW_KEYS) { |
| 238 | printk(BIOS_ERR, "PS2K: Bad resp from EC. Vivaldi disabled!\n"); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | /* Convert enum action_key values to enum ps2_action_key values */ |
| 243 | for (i = 0; i < keybd.num_top_row_keys; i++) |
| 244 | ps2_action_keys[i] = ps2_enum_val[keybd.action_keys[i]]; |
| 245 | |
| 246 | acpigen_ps2_keyboard_dsd("_SB.PCI0.PS2K", keybd.num_top_row_keys, |
| 247 | ps2_action_keys, |
| 248 | !!(keybd.capabilities & KEYBD_CAP_FUNCTION_KEYS), |
| 249 | !!(keybd.capabilities & KEYBD_CAP_NUMERIC_KEYPAD), |
| 250 | !!(keybd.capabilities & KEYBD_CAP_SCRNLOCK_KEY)); |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Tim Wawrzynczak | 93d7bcb | 2020-05-29 15:44:25 -0600 | [diff] [blame] | 253 | static const char *ec_acpi_name(const struct device *dev) |
| 254 | { |
| 255 | return "EC0"; |
| 256 | } |
| 257 | |
| 258 | static struct device_operations ec_ops = { |
| 259 | .acpi_name = ec_acpi_name, |
| 260 | }; |
| 261 | |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 262 | void google_chromeec_fill_ssdt_generator(const struct device *dev) |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 263 | { |
Tim Wawrzynczak | 93d7bcb | 2020-05-29 15:44:25 -0600 | [diff] [blame] | 264 | struct device_path path; |
| 265 | struct device *ec; |
| 266 | |
Tim Wawrzynczak | 93d7bcb | 2020-05-29 15:44:25 -0600 | [diff] [blame] | 267 | /* Set up a minimal EC0 device to pass to the DPTF helpers */ |
| 268 | path.type = DEVICE_PATH_GENERIC; |
| 269 | path.generic.id = 0; |
| 270 | ec = alloc_find_dev(dev->bus, &path); |
| 271 | ec->ops = &ec_ops; |
| 272 | |
| 273 | if (CONFIG(DRIVERS_INTEL_DPTF)) |
| 274 | ec_fill_dptf_helpers(ec); |
| 275 | |
Rajat Jain | 962c788 | 2020-04-01 17:24:28 -0700 | [diff] [blame] | 276 | fill_ssdt_typec_device(dev); |
| 277 | fill_ssdt_ps2_keyboard(dev); |
Tim Wawrzynczak | eb3cd85 | 2020-01-22 16:52:13 -0700 | [diff] [blame] | 278 | } |
John Zhao | eec3e3b | 2021-01-08 22:33:04 -0800 | [diff] [blame] | 279 | |
| 280 | const char *ec_retimer_fw_update_path(void) |
| 281 | { |
| 282 | return "\\_SB_.PCI0.LPCB.EC0_.RFWU"; |
| 283 | } |
John Zhao | 8bb83a3 | 2021-04-27 11:19:41 -0700 | [diff] [blame] | 284 | |
| 285 | void ec_retimer_fw_update(uint8_t data) |
| 286 | { |
| 287 | const char *RFWU = ec_retimer_fw_update_path(); |
| 288 | |
| 289 | /* |
| 290 | * Write the EC RAM for Retimer Upgrade |
| 291 | * RFWU = data |
| 292 | */ |
| 293 | acpigen_write_store(); |
| 294 | acpigen_write_byte(data); |
| 295 | acpigen_emit_namestring(RFWU); |
| 296 | } |