blob: d87a49c88d7bcb0bd75c9bc23df940ee96bb8da5 [file] [log] [blame]
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -07001/*
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -07002 *
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -07003 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
Furquan Shaikh76cedd22020-05-02 10:24:23 -07007#include <acpi/acpi.h>
8#include <acpi/acpi_device.h>
9#include <acpi/acpigen.h>
10#include <acpi/acpigen_ps2_keybd.h>
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070011#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 Ramasubramaniana95907b2020-04-13 18:03:29 -060019#define GOOGLE_CHROMEEC_USBC_DEVICE_HID "GOOG0014"
20#define GOOGLE_CHROMEEC_USBC_DEVICE_NAME "USBC"
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070021
Tim Wawrzynczak60467392020-05-18 13:45:43 -060022/* Avoid adding a false dependency on an SoC or intel/common */
23extern const struct device *soc_get_pmc_mux_device(int port_number);
24
25__weak const struct device *soc_get_pmc_mux_device(int port_number)
26{
27 return NULL;
28}
29
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070030const char *google_chromeec_acpi_name(const struct device *dev)
31{
Furquan Shaikheec30f72020-04-20 16:38:21 -070032 /*
33 * Chrome EC device (CREC - GOOG0004) is really a child of EC device (EC - PNP0C09) in
34 * ACPI tables. However, in coreboot device tree, there is no separate chip/device for
35 * EC0. Thus, Chrome EC device needs to return "EC0.CREC" as the ACPI name so that the
36 * callers can get the correct acpi device path/scope for this device.
37 *
38 * If we ever enable a separate driver for generating AML for EC0 device, then this
39 * function needs to be updated to return "CREC".
40 */
41 return "EC0.CREC";
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070042}
43
44static const char *power_role_to_str(enum ec_pd_power_role_caps power_role)
45{
46 switch (power_role) {
47 case EC_PD_POWER_ROLE_SOURCE:
48 return "source";
49 case EC_PD_POWER_ROLE_SINK:
50 return "sink";
51 case EC_PD_POWER_ROLE_DUAL:
52 return "dual";
53 default:
54 return "unknown";
55 }
56}
57
58static const char *try_power_role_to_str(enum ec_pd_try_power_role_caps try_power_role)
59{
60 switch (try_power_role) {
61 case EC_PD_TRY_POWER_ROLE_NONE:
62 /*
63 * This should never get returned; if there is no try-power role for a device,
64 * then the try-power-role field is not added to the DSD. Thus, this is just
65 * for completeness.
66 */
67 return "none";
68 case EC_PD_TRY_POWER_ROLE_SINK:
69 return "sink";
70 case EC_PD_TRY_POWER_ROLE_SOURCE:
71 return "source";
72 default:
73 return "unknown";
74 }
75}
76
77static const char *data_role_to_str(enum ec_pd_data_role_caps data_role)
78{
79 switch (data_role) {
80 case EC_PD_DATA_ROLE_DFP:
81 return "host";
82 case EC_PD_DATA_ROLE_UFP:
83 return "device";
84 case EC_PD_DATA_ROLE_DUAL:
85 return "dual";
86 default:
87 return "unknown";
88 }
89}
90
91/*
92 * Apparently these are supposed to be uppercase, in contrast to the other
93 * lowercase fields.
94 */
95static const char *port_location_to_str(enum ec_pd_port_location port_location)
96{
97 switch (port_location) {
98 case EC_PD_PORT_LOCATION_LEFT:
99 return "LEFT";
100 case EC_PD_PORT_LOCATION_RIGHT:
101 return "RIGHT";
102 case EC_PD_PORT_LOCATION_BACK:
103 return "BACK";
104 case EC_PD_PORT_LOCATION_FRONT:
105 return "FRONT";
106 case EC_PD_PORT_LOCATION_LEFT_FRONT:
107 return "LEFT_FRONT";
108 case EC_PD_PORT_LOCATION_LEFT_BACK:
109 return "LEFT_BACK";
110 case EC_PD_PORT_LOCATION_RIGHT_FRONT:
111 return "RIGHT_FRONT";
112 case EC_PD_PORT_LOCATION_RIGHT_BACK:
113 return "RIGHT_BACK";
114 case EC_PD_PORT_LOCATION_BACK_LEFT:
115 return "BACK_LEFT";
116 case EC_PD_PORT_LOCATION_BACK_RIGHT:
117 return "BACK_RIGHT";
118 case EC_PD_PORT_LOCATION_UNKNOWN: /* intentional fallthrough */
119 default:
120 return "UNKNOWN";
121 }
122}
123
124/* Add port capabilities as DP properties */
125static void add_port_caps(struct acpi_dp *dsd, const struct usb_pd_port_caps *port_caps)
126{
127 acpi_dp_add_string(dsd, "power-role", power_role_to_str(port_caps->power_role_cap));
128
129 if (port_caps->try_power_role_cap != EC_PD_TRY_POWER_ROLE_NONE)
130 acpi_dp_add_string(dsd, "try-power-role",
131 try_power_role_to_str(port_caps->try_power_role_cap));
132
133 acpi_dp_add_string(dsd, "data-role", data_role_to_str(port_caps->data_role_cap));
134 acpi_dp_add_string(dsd, "port-location", port_location_to_str(
135 port_caps->port_location));
136}
137
138/*
139 * Helper for fill_ssdt_generator. This adds references to the USB
140 * port objects so that the consumer of this information can know
141 * whether the port supports USB2 and/or USB3.
142 */
143static void add_usb_port_references(struct acpi_dp *dsd, int port_number)
144{
145 static const char usb2_port[] = "usb2-port";
146 static const char usb3_port[] = "usb3-port";
147 struct device *port = NULL;
148 const char *path;
149 const char *usb_port_type;
150 struct drivers_usb_acpi_config *config;
151
152 /*
153 * Unfortunately, the acpi_dp_* API doesn't write out the data immediately, thus we need
154 * different storage areas for all of the strings, so strdup() is used for that. It is
155 * safe to use strdup() here, because the strings are generated at build-time and are
156 * guaranteed to be NUL-terminated (they come from the devicetree).
157 */
158 while ((port = dev_find_path(port, DEVICE_PATH_USB)) != NULL) {
159 if (!port->enabled || port->path.type != DEVICE_PATH_USB)
160 continue;
161
162 /* Looking for USB 2 & 3 port devices only */
163 if (port->path.usb.port_type == 2)
164 usb_port_type = usb2_port;
165 else if (port->path.usb.port_type == 3)
166 usb_port_type = usb3_port;
167 else
168 continue;
169
170 config = port->chip_info;
171
172 /*
173 * Look at only USB Type-C ports, making sure they match the
174 * port number we're looking for (the 'token' field in 'group').
175 * Also note that 'port_number' is 0-based, whereas the 'token'
176 * field is 1-based.
177 */
178 if ((config->type != UPC_TYPE_C_USB2_ONLY) &&
179 (config->type != UPC_TYPE_C_USB2_SS_SWITCH) &&
180 (config->type != UPC_TYPE_C_USB2_SS))
181 continue;
182
183 if (config->group.token != (port_number + 1))
184 continue;
185
186 path = acpi_device_path(port);
187 if (path) {
188 path = strdup(path);
189 if (!path)
190 continue;
191
192 acpi_dp_add_reference(dsd, usb_port_type, path);
193 }
194 }
195}
196
Tim Wawrzynczak60467392020-05-18 13:45:43 -0600197/*
198 * Another helper for fill_ssdt_typec_device(). For each port, this one adds references to the
199 * ACPI device which control the orientation, USB data role and data muxing.
200 */
201static void add_switch_references(struct acpi_dp *dsd, int port_number)
202{
203 const struct device *dev;
204 const char *path;
205
206 dev = soc_get_pmc_mux_device(port_number);
207 if (!dev) {
208 printk(BIOS_ERR, "ERROR: %s: No SOC PMC MUX device found", __func__);
209 return;
210 }
211
212 path = acpi_device_path(dev);
213 if (!path)
214 return;
215
216 acpi_dp_add_reference(dsd, "orientation-switch", path);
217 acpi_dp_add_reference(dsd, "usb-role-switch", path);
218 acpi_dp_add_reference(dsd, "mode-switch", path);
219}
220
Furquan Shaikh7536a392020-04-24 21:59:21 -0700221static void fill_ssdt_typec_device(const struct device *dev)
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700222{
223 struct usb_pd_port_caps port_caps;
224 char con_name[] = "CONx";
225 struct acpi_dp *dsd;
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700226 int rv;
Rajat Jain962c7882020-04-01 17:24:28 -0700227 int i, num_ports;
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700228
Rajat Jain962c7882020-04-01 17:24:28 -0700229 if (google_chromeec_get_num_pd_ports(&num_ports))
230 return;
231
Furquan Shaikheec30f72020-04-20 16:38:21 -0700232 acpigen_write_scope(acpi_device_path(dev));
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700233 acpigen_write_device(GOOGLE_CHROMEEC_USBC_DEVICE_NAME);
234 acpigen_write_name_string("_HID", GOOGLE_CHROMEEC_USBC_DEVICE_HID);
235 acpigen_write_name_string("_DDN", "ChromeOS EC Embedded Controller "
236 "USB Type-C Control");
237
238 for (i = 0; i < num_ports; ++i) {
239 rv = google_chromeec_get_pd_port_caps(i, &port_caps);
240 if (rv)
241 continue;
242
243 con_name[3] = (char)i + '0';
244 acpigen_write_device(con_name);
245 acpigen_write_name_integer("_ADR", i);
246
247 /* _DSD, Device-Specific Data */
248 dsd = acpi_dp_new_table("_DSD");
249
250 acpi_dp_add_integer(dsd, "port-number", i);
251 add_port_caps(dsd, &port_caps);
252 add_usb_port_references(dsd, i);
Tim Wawrzynczak60467392020-05-18 13:45:43 -0600253 add_switch_references(dsd, i);
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700254
255 acpi_dp_write(dsd);
256 acpigen_pop_len(); /* Device CONx */
257 }
258
259 acpigen_pop_len(); /* Device GOOGLE_CHROMEEC_USBC_DEVICE_NAME */
Rajat Jain962c7882020-04-01 17:24:28 -0700260 acpigen_pop_len(); /* Scope */
261}
262
263static const enum ps2_action_key ps2_enum_val[] = {
264 [TK_ABSENT] = PS2_KEY_ABSENT,
265 [TK_BACK] = PS2_KEY_BACK,
266 [TK_FORWARD] = PS2_KEY_FORWARD,
267 [TK_REFRESH] = PS2_KEY_REFRESH,
268 [TK_FULLSCREEN] = PS2_KEY_FULLSCREEN,
269 [TK_OVERVIEW] = PS2_KEY_OVERVIEW,
270 [TK_BRIGHTNESS_DOWN] = PS2_KEY_BRIGHTNESS_DOWN,
271 [TK_BRIGHTNESS_UP] = PS2_KEY_BRIGHTNESS_UP,
272 [TK_VOL_MUTE] = PS2_KEY_VOL_MUTE,
273 [TK_VOL_DOWN] = PS2_KEY_VOL_DOWN,
274 [TK_VOL_UP] = PS2_KEY_VOL_UP,
275 [TK_SNAPSHOT] = PS2_KEY_SNAPSHOT,
276 [TK_PRIVACY_SCRN_TOGGLE] = PS2_KEY_PRIVACY_SCRN_TOGGLE,
277 [TK_KBD_BKLIGHT_DOWN] = PS2_KEY_KBD_BKLIGHT_DOWN,
278 [TK_KBD_BKLIGHT_UP] = PS2_KEY_KBD_BKLIGHT_UP,
279 [TK_PLAY_PAUSE] = PS2_KEY_PLAY_PAUSE,
280 [TK_NEXT_TRACK] = PS2_KEY_NEXT_TRACK,
281 [TK_PREV_TRACK] = PS2_KEY_PREV_TRACK,
282};
283
Furquan Shaikh7536a392020-04-24 21:59:21 -0700284static void fill_ssdt_ps2_keyboard(const struct device *dev)
Rajat Jain962c7882020-04-01 17:24:28 -0700285{
286 uint8_t i;
287 struct ec_response_keybd_config keybd = {};
288 enum ps2_action_key ps2_action_keys[MAX_TOP_ROW_KEYS] = {};
289
290 if (google_chromeec_get_keybd_config(&keybd) ||
291 !keybd.num_top_row_keys ||
292 keybd.num_top_row_keys > MAX_TOP_ROW_KEYS) {
293 printk(BIOS_ERR, "PS2K: Bad resp from EC. Vivaldi disabled!\n");
294 return;
295 }
296
297 /* Convert enum action_key values to enum ps2_action_key values */
298 for (i = 0; i < keybd.num_top_row_keys; i++)
299 ps2_action_keys[i] = ps2_enum_val[keybd.action_keys[i]];
300
301 acpigen_ps2_keyboard_dsd("_SB.PCI0.PS2K", keybd.num_top_row_keys,
302 ps2_action_keys,
303 !!(keybd.capabilities & KEYBD_CAP_FUNCTION_KEYS),
304 !!(keybd.capabilities & KEYBD_CAP_NUMERIC_KEYPAD),
305 !!(keybd.capabilities & KEYBD_CAP_SCRNLOCK_KEY));
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700306}
307
Furquan Shaikh7536a392020-04-24 21:59:21 -0700308void google_chromeec_fill_ssdt_generator(const struct device *dev)
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700309{
Rajat Jain962c7882020-04-01 17:24:28 -0700310 if (!dev->enabled)
Matt DeVillier70ea3b92020-03-18 23:45:32 -0500311 return;
312
Rajat Jain962c7882020-04-01 17:24:28 -0700313 fill_ssdt_typec_device(dev);
314 fill_ssdt_ps2_keyboard(dev);
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700315}