blob: 8c80382b18d998465c2c825e9a4085774d0cacf3 [file] [log] [blame]
Tim Wawrzynczak6da82132020-05-19 12:46:04 -06001/* SPDX-License-Identifier: GPL-2.0-or-later */
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -07002
Furquan Shaikh76cedd22020-05-02 10:24:23 -07003#include <acpi/acpi.h>
4#include <acpi/acpi_device.h>
5#include <acpi/acpigen.h>
6#include <acpi/acpigen_ps2_keybd.h>
Tim Wawrzynczak6da82132020-05-19 12:46:04 -06007#include <acpi/acpigen_usb.h>
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -07008#include <console/console.h>
9#include <drivers/usb/acpi/chip.h>
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070010
11#include "chip.h"
12#include "ec.h"
13#include "ec_commands.h"
14
Karthikeyan Ramasubramaniana95907b2020-04-13 18:03:29 -060015#define GOOGLE_CHROMEEC_USBC_DEVICE_HID "GOOG0014"
16#define GOOGLE_CHROMEEC_USBC_DEVICE_NAME "USBC"
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070017
Tim Wawrzynczak60467392020-05-18 13:45:43 -060018/* Avoid adding a false dependency on an SoC or intel/common */
19extern const struct device *soc_get_pmc_mux_device(int port_number);
20
21__weak const struct device *soc_get_pmc_mux_device(int port_number)
22{
23 return NULL;
24}
25
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070026const char *google_chromeec_acpi_name(const struct device *dev)
27{
Furquan Shaikheec30f72020-04-20 16:38:21 -070028 /*
29 * Chrome EC device (CREC - GOOG0004) is really a child of EC device (EC - PNP0C09) in
30 * ACPI tables. However, in coreboot device tree, there is no separate chip/device for
31 * EC0. Thus, Chrome EC device needs to return "EC0.CREC" as the ACPI name so that the
32 * callers can get the correct acpi device path/scope for this device.
33 *
34 * If we ever enable a separate driver for generating AML for EC0 device, then this
35 * function needs to be updated to return "CREC".
36 */
37 return "EC0.CREC";
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070038}
39
Tim Wawrzynczak6da82132020-05-19 12:46:04 -060040/*
41 * Helper for fill_ssdt_generator. This adds references to the USB
42 * port objects so that the consumer of this information can know
43 * whether the port supports USB2 and/or USB3.
44 */
45static void get_usb_port_references(int port_number, struct device **usb2_port,
46 struct device **usb3_port, struct device **usb4_port)
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070047{
Tim Wawrzynczak6da82132020-05-19 12:46:04 -060048 struct drivers_usb_acpi_config *config;
49 struct device *port = NULL;
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070050
Tim Wawrzynczak6da82132020-05-19 12:46:04 -060051 /* Search through the devicetree for matching USB Type-C ports */
52 while ((port = dev_find_path(port, DEVICE_PATH_USB)) != NULL) {
53 if (!port->enabled || port->path.type != DEVICE_PATH_USB)
54 continue;
55
56 config = port->chip_info;
57
58 /* Look at only USB Type-C ports */
59 if ((config->type != UPC_TYPE_C_USB2_ONLY) &&
60 (config->type != UPC_TYPE_C_USB2_SS_SWITCH) &&
61 (config->type != UPC_TYPE_C_USB2_SS))
62 continue;
63
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070064 /*
Tim Wawrzynczak6da82132020-05-19 12:46:04 -060065 * Check for a matching port number (the 'token' field in 'group'). Note that
66 * 'port_number' is 0-based, whereas the 'token' field is 1-based.
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070067 */
Tim Wawrzynczak6da82132020-05-19 12:46:04 -060068 if (config->group.token != (port_number + 1))
69 continue;
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070070
Tim Wawrzynczak6da82132020-05-19 12:46:04 -060071 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 Wawrzynczakeb3cd852020-01-22 16:52:13 -070084 }
85}
86
87/*
88 * Apparently these are supposed to be uppercase, in contrast to the other
89 * lowercase fields.
90 */
91static 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 Wawrzynczak6da82132020-05-19 12:46:04 -0600120static struct usb_pd_port_caps port_caps;
121static void add_port_location(struct acpi_dp *dsd, int port_number)
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700122{
Tim Wawrzynczak6da82132020-05-19 12:46:04 -0600123 acpi_dp_add_string(dsd, "port-location",
124 port_location_to_str(port_caps.port_location));
Tim Wawrzynczak60467392020-05-18 13:45:43 -0600125}
126
Tim Wawrzynczak16ef59e2020-06-01 20:07:45 -0600127static int con_id_to_match;
128
129/* A callback to match a port's connector for dev_find_matching_device_on_bus */
130static bool match_connector(DEVTREE_CONST struct device *dev)
131{
132 if (CONFIG(DRIVERS_INTEL_PMC)) {
133 extern struct chip_operations drivers_intel_pmc_mux_con_ops;
134
135 return (dev->chip_ops == &drivers_intel_pmc_mux_con_ops &&
136 dev->path.type == DEVICE_PATH_GENERIC &&
137 dev->path.generic.id == con_id_to_match);
138 }
139
140 return false;
141}
142
Furquan Shaikh7536a392020-04-24 21:59:21 -0700143static void fill_ssdt_typec_device(const struct device *dev)
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700144{
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700145 int rv;
Rajat Jain962c7882020-04-01 17:24:28 -0700146 int i, num_ports;
Tim Wawrzynczak6da82132020-05-19 12:46:04 -0600147 struct device *usb2_port;
148 struct device *usb3_port;
149 struct device *usb4_port;
150 const struct device *mux;
Tim Wawrzynczak16ef59e2020-06-01 20:07:45 -0600151 const struct device *con;
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700152
Rajat Jain962c7882020-04-01 17:24:28 -0700153 if (google_chromeec_get_num_pd_ports(&num_ports))
154 return;
155
Furquan Shaikheec30f72020-04-20 16:38:21 -0700156 acpigen_write_scope(acpi_device_path(dev));
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700157 acpigen_write_device(GOOGLE_CHROMEEC_USBC_DEVICE_NAME);
158 acpigen_write_name_string("_HID", GOOGLE_CHROMEEC_USBC_DEVICE_HID);
159 acpigen_write_name_string("_DDN", "ChromeOS EC Embedded Controller "
160 "USB Type-C Control");
161
162 for (i = 0; i < num_ports; ++i) {
163 rv = google_chromeec_get_pd_port_caps(i, &port_caps);
164 if (rv)
165 continue;
166
Tim Wawrzynczak16ef59e2020-06-01 20:07:45 -0600167 /* Get the MUX device, and find the matching connector on its bus */
168 con = NULL;
Tim Wawrzynczak6da82132020-05-19 12:46:04 -0600169 mux = soc_get_pmc_mux_device(i);
Tim Wawrzynczak16ef59e2020-06-01 20:07:45 -0600170 if (mux) {
171 con_id_to_match = i;
172 con = dev_find_matching_device_on_bus(mux->link_list, match_connector);
173 }
174
Tim Wawrzynczak6da82132020-05-19 12:46:04 -0600175 usb2_port = NULL;
176 usb3_port = NULL;
177 usb4_port = NULL;
178 get_usb_port_references(i, &usb2_port, &usb3_port, &usb4_port);
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700179
Tim Wawrzynczak6da82132020-05-19 12:46:04 -0600180 struct typec_connector_class_config config = {
181 .power_role = port_caps.power_role_cap,
182 .try_power_role = port_caps.try_power_role_cap,
183 .data_role = port_caps.data_role_cap,
184 .usb2_port = usb2_port,
185 .usb3_port = usb3_port,
186 .usb4_port = usb4_port,
Tim Wawrzynczak16ef59e2020-06-01 20:07:45 -0600187 .orientation_switch = con,
188 .usb_role_switch = con,
189 .mode_switch = con,
Tim Wawrzynczak6da82132020-05-19 12:46:04 -0600190 };
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700191
Tim Wawrzynczak6da82132020-05-19 12:46:04 -0600192 acpigen_write_typec_connector(&config, i, add_port_location);
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700193 }
194
195 acpigen_pop_len(); /* Device GOOGLE_CHROMEEC_USBC_DEVICE_NAME */
Rajat Jain962c7882020-04-01 17:24:28 -0700196 acpigen_pop_len(); /* Scope */
197}
198
199static const enum ps2_action_key ps2_enum_val[] = {
200 [TK_ABSENT] = PS2_KEY_ABSENT,
201 [TK_BACK] = PS2_KEY_BACK,
202 [TK_FORWARD] = PS2_KEY_FORWARD,
203 [TK_REFRESH] = PS2_KEY_REFRESH,
204 [TK_FULLSCREEN] = PS2_KEY_FULLSCREEN,
205 [TK_OVERVIEW] = PS2_KEY_OVERVIEW,
206 [TK_BRIGHTNESS_DOWN] = PS2_KEY_BRIGHTNESS_DOWN,
207 [TK_BRIGHTNESS_UP] = PS2_KEY_BRIGHTNESS_UP,
208 [TK_VOL_MUTE] = PS2_KEY_VOL_MUTE,
209 [TK_VOL_DOWN] = PS2_KEY_VOL_DOWN,
210 [TK_VOL_UP] = PS2_KEY_VOL_UP,
211 [TK_SNAPSHOT] = PS2_KEY_SNAPSHOT,
212 [TK_PRIVACY_SCRN_TOGGLE] = PS2_KEY_PRIVACY_SCRN_TOGGLE,
213 [TK_KBD_BKLIGHT_DOWN] = PS2_KEY_KBD_BKLIGHT_DOWN,
214 [TK_KBD_BKLIGHT_UP] = PS2_KEY_KBD_BKLIGHT_UP,
215 [TK_PLAY_PAUSE] = PS2_KEY_PLAY_PAUSE,
216 [TK_NEXT_TRACK] = PS2_KEY_NEXT_TRACK,
217 [TK_PREV_TRACK] = PS2_KEY_PREV_TRACK,
218};
219
Furquan Shaikh7536a392020-04-24 21:59:21 -0700220static void fill_ssdt_ps2_keyboard(const struct device *dev)
Rajat Jain962c7882020-04-01 17:24:28 -0700221{
222 uint8_t i;
223 struct ec_response_keybd_config keybd = {};
224 enum ps2_action_key ps2_action_keys[MAX_TOP_ROW_KEYS] = {};
225
226 if (google_chromeec_get_keybd_config(&keybd) ||
227 !keybd.num_top_row_keys ||
228 keybd.num_top_row_keys > MAX_TOP_ROW_KEYS) {
229 printk(BIOS_ERR, "PS2K: Bad resp from EC. Vivaldi disabled!\n");
230 return;
231 }
232
233 /* Convert enum action_key values to enum ps2_action_key values */
234 for (i = 0; i < keybd.num_top_row_keys; i++)
235 ps2_action_keys[i] = ps2_enum_val[keybd.action_keys[i]];
236
237 acpigen_ps2_keyboard_dsd("_SB.PCI0.PS2K", keybd.num_top_row_keys,
238 ps2_action_keys,
239 !!(keybd.capabilities & KEYBD_CAP_FUNCTION_KEYS),
240 !!(keybd.capabilities & KEYBD_CAP_NUMERIC_KEYPAD),
241 !!(keybd.capabilities & KEYBD_CAP_SCRNLOCK_KEY));
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700242}
243
Furquan Shaikh7536a392020-04-24 21:59:21 -0700244void google_chromeec_fill_ssdt_generator(const struct device *dev)
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700245{
Rajat Jain962c7882020-04-01 17:24:28 -0700246 if (!dev->enabled)
Matt DeVillier70ea3b92020-03-18 23:45:32 -0500247 return;
248
Rajat Jain962c7882020-04-01 17:24:28 -0700249 fill_ssdt_typec_device(dev);
250 fill_ssdt_ps2_keyboard(dev);
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700251}