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