blob: b7683167af6bed0f854767d782ba5da363f27544 [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 Wawrzynczak93d7bcb2020-05-29 15:44:25 -060010#include <ec/google/common/dptf.h>
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070011
12#include "chip.h"
13#include "ec.h"
14#include "ec_commands.h"
15
Karthikeyan Ramasubramaniana95907b2020-04-13 18:03:29 -060016#define GOOGLE_CHROMEEC_USBC_DEVICE_HID "GOOG0014"
17#define GOOGLE_CHROMEEC_USBC_DEVICE_NAME "USBC"
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070018
Tim Wawrzynczak60467392020-05-18 13:45:43 -060019/* Avoid adding a false dependency on an SoC or intel/common */
20extern 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 Wawrzynczakeb3cd852020-01-22 16:52:13 -070027const char *google_chromeec_acpi_name(const struct device *dev)
28{
Furquan Shaikheec30f72020-04-20 16:38:21 -070029 /*
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 Wawrzynczakeb3cd852020-01-22 16:52:13 -070039}
40
Tim Wawrzynczak6da82132020-05-19 12:46:04 -060041/*
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 */
46static void get_usb_port_references(int port_number, struct device **usb2_port,
47 struct device **usb3_port, struct device **usb4_port)
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070048{
Tim Wawrzynczak6da82132020-05-19 12:46:04 -060049 struct drivers_usb_acpi_config *config;
50 struct device *port = NULL;
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070051
Tim Wawrzynczak6da82132020-05-19 12:46:04 -060052 /* 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 Wawrzynczakeb3cd852020-01-22 16:52:13 -070065 /*
Tim Wawrzynczak6da82132020-05-19 12:46:04 -060066 * 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 Wawrzynczakeb3cd852020-01-22 16:52:13 -070068 */
Tim Wawrzynczak6da82132020-05-19 12:46:04 -060069 if (config->group.token != (port_number + 1))
70 continue;
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070071
Tim Wawrzynczak6da82132020-05-19 12:46:04 -060072 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 Wawrzynczakeb3cd852020-01-22 16:52:13 -070085 }
86}
87
88/*
89 * Apparently these are supposed to be uppercase, in contrast to the other
90 * lowercase fields.
91 */
92static 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 Wawrzynczak6da82132020-05-19 12:46:04 -0600121static struct usb_pd_port_caps port_caps;
122static void add_port_location(struct acpi_dp *dsd, int port_number)
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700123{
Tim Wawrzynczak6da82132020-05-19 12:46:04 -0600124 acpi_dp_add_string(dsd, "port-location",
125 port_location_to_str(port_caps.port_location));
Tim Wawrzynczak60467392020-05-18 13:45:43 -0600126}
127
Tim Wawrzynczake414ce42020-07-03 09:36:11 -0600128static int conn_id_to_match;
Tim Wawrzynczak16ef59e2020-06-01 20:07:45 -0600129
130/* A callback to match a port's connector for dev_find_matching_device_on_bus */
131static bool match_connector(DEVTREE_CONST struct device *dev)
132{
133 if (CONFIG(DRIVERS_INTEL_PMC)) {
Tim Wawrzynczake414ce42020-07-03 09:36:11 -0600134 extern struct chip_operations drivers_intel_pmc_mux_conn_ops;
Tim Wawrzynczak16ef59e2020-06-01 20:07:45 -0600135
Tim Wawrzynczake414ce42020-07-03 09:36:11 -0600136 return (dev->chip_ops == &drivers_intel_pmc_mux_conn_ops &&
Tim Wawrzynczak16ef59e2020-06-01 20:07:45 -0600137 dev->path.type == DEVICE_PATH_GENERIC &&
Tim Wawrzynczake414ce42020-07-03 09:36:11 -0600138 dev->path.generic.id == conn_id_to_match);
Tim Wawrzynczak16ef59e2020-06-01 20:07:45 -0600139 }
140
141 return false;
142}
143
Furquan Shaikh7536a392020-04-24 21:59:21 -0700144static void fill_ssdt_typec_device(const struct device *dev)
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700145{
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700146 int rv;
Divya Sasidharan49d74de2019-10-22 13:41:15 -0700147 int i;
148 unsigned int num_ports;
Tim Wawrzynczak6da82132020-05-19 12:46:04 -0600149 struct device *usb2_port;
150 struct device *usb3_port;
151 struct device *usb4_port;
152 const struct device *mux;
Tim Wawrzynczake414ce42020-07-03 09:36:11 -0600153 const struct device *conn;
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700154
Rajat Jain962c7882020-04-01 17:24:28 -0700155 if (google_chromeec_get_num_pd_ports(&num_ports))
156 return;
157
Furquan Shaikheec30f72020-04-20 16:38:21 -0700158 acpigen_write_scope(acpi_device_path(dev));
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700159 acpigen_write_device(GOOGLE_CHROMEEC_USBC_DEVICE_NAME);
160 acpigen_write_name_string("_HID", GOOGLE_CHROMEEC_USBC_DEVICE_HID);
161 acpigen_write_name_string("_DDN", "ChromeOS EC Embedded Controller "
162 "USB Type-C Control");
163
164 for (i = 0; i < num_ports; ++i) {
165 rv = google_chromeec_get_pd_port_caps(i, &port_caps);
166 if (rv)
167 continue;
168
Tim Wawrzynczak16ef59e2020-06-01 20:07:45 -0600169 /* Get the MUX device, and find the matching connector on its bus */
Tim Wawrzynczake414ce42020-07-03 09:36:11 -0600170 conn = NULL;
Tim Wawrzynczak6da82132020-05-19 12:46:04 -0600171 mux = soc_get_pmc_mux_device(i);
Tim Wawrzynczak16ef59e2020-06-01 20:07:45 -0600172 if (mux) {
Tim Wawrzynczake414ce42020-07-03 09:36:11 -0600173 conn_id_to_match = i;
174 conn = dev_find_matching_device_on_bus(mux->link_list, match_connector);
Tim Wawrzynczak16ef59e2020-06-01 20:07:45 -0600175 }
176
Tim Wawrzynczak6da82132020-05-19 12:46:04 -0600177 usb2_port = NULL;
178 usb3_port = NULL;
179 usb4_port = NULL;
180 get_usb_port_references(i, &usb2_port, &usb3_port, &usb4_port);
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700181
Tim Wawrzynczak6da82132020-05-19 12:46:04 -0600182 struct typec_connector_class_config config = {
183 .power_role = port_caps.power_role_cap,
184 .try_power_role = port_caps.try_power_role_cap,
185 .data_role = port_caps.data_role_cap,
186 .usb2_port = usb2_port,
187 .usb3_port = usb3_port,
188 .usb4_port = usb4_port,
Tim Wawrzynczake414ce42020-07-03 09:36:11 -0600189 .orientation_switch = conn,
190 .usb_role_switch = conn,
191 .mode_switch = conn,
Tim Wawrzynczak6da82132020-05-19 12:46:04 -0600192 };
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700193
Tim Wawrzynczak6da82132020-05-19 12:46:04 -0600194 acpigen_write_typec_connector(&config, i, add_port_location);
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700195 }
196
197 acpigen_pop_len(); /* Device GOOGLE_CHROMEEC_USBC_DEVICE_NAME */
Rajat Jain962c7882020-04-01 17:24:28 -0700198 acpigen_pop_len(); /* Scope */
199}
200
201static const enum ps2_action_key ps2_enum_val[] = {
202 [TK_ABSENT] = PS2_KEY_ABSENT,
203 [TK_BACK] = PS2_KEY_BACK,
204 [TK_FORWARD] = PS2_KEY_FORWARD,
205 [TK_REFRESH] = PS2_KEY_REFRESH,
206 [TK_FULLSCREEN] = PS2_KEY_FULLSCREEN,
207 [TK_OVERVIEW] = PS2_KEY_OVERVIEW,
208 [TK_BRIGHTNESS_DOWN] = PS2_KEY_BRIGHTNESS_DOWN,
209 [TK_BRIGHTNESS_UP] = PS2_KEY_BRIGHTNESS_UP,
210 [TK_VOL_MUTE] = PS2_KEY_VOL_MUTE,
211 [TK_VOL_DOWN] = PS2_KEY_VOL_DOWN,
212 [TK_VOL_UP] = PS2_KEY_VOL_UP,
213 [TK_SNAPSHOT] = PS2_KEY_SNAPSHOT,
214 [TK_PRIVACY_SCRN_TOGGLE] = PS2_KEY_PRIVACY_SCRN_TOGGLE,
215 [TK_KBD_BKLIGHT_DOWN] = PS2_KEY_KBD_BKLIGHT_DOWN,
216 [TK_KBD_BKLIGHT_UP] = PS2_KEY_KBD_BKLIGHT_UP,
217 [TK_PLAY_PAUSE] = PS2_KEY_PLAY_PAUSE,
218 [TK_NEXT_TRACK] = PS2_KEY_NEXT_TRACK,
219 [TK_PREV_TRACK] = PS2_KEY_PREV_TRACK,
220};
221
Furquan Shaikh7536a392020-04-24 21:59:21 -0700222static void fill_ssdt_ps2_keyboard(const struct device *dev)
Rajat Jain962c7882020-04-01 17:24:28 -0700223{
224 uint8_t i;
225 struct ec_response_keybd_config keybd = {};
226 enum ps2_action_key ps2_action_keys[MAX_TOP_ROW_KEYS] = {};
227
228 if (google_chromeec_get_keybd_config(&keybd) ||
229 !keybd.num_top_row_keys ||
230 keybd.num_top_row_keys > MAX_TOP_ROW_KEYS) {
231 printk(BIOS_ERR, "PS2K: Bad resp from EC. Vivaldi disabled!\n");
232 return;
233 }
234
235 /* Convert enum action_key values to enum ps2_action_key values */
236 for (i = 0; i < keybd.num_top_row_keys; i++)
237 ps2_action_keys[i] = ps2_enum_val[keybd.action_keys[i]];
238
239 acpigen_ps2_keyboard_dsd("_SB.PCI0.PS2K", keybd.num_top_row_keys,
240 ps2_action_keys,
241 !!(keybd.capabilities & KEYBD_CAP_FUNCTION_KEYS),
242 !!(keybd.capabilities & KEYBD_CAP_NUMERIC_KEYPAD),
243 !!(keybd.capabilities & KEYBD_CAP_SCRNLOCK_KEY));
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700244}
245
Tim Wawrzynczak93d7bcb2020-05-29 15:44:25 -0600246static const char *ec_acpi_name(const struct device *dev)
247{
248 return "EC0";
249}
250
251static struct device_operations ec_ops = {
252 .acpi_name = ec_acpi_name,
253};
254
Furquan Shaikh7536a392020-04-24 21:59:21 -0700255void google_chromeec_fill_ssdt_generator(const struct device *dev)
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700256{
Tim Wawrzynczak93d7bcb2020-05-29 15:44:25 -0600257 struct device_path path;
258 struct device *ec;
259
Rajat Jain962c7882020-04-01 17:24:28 -0700260 if (!dev->enabled)
Matt DeVillier70ea3b92020-03-18 23:45:32 -0500261 return;
262
Tim Wawrzynczak93d7bcb2020-05-29 15:44:25 -0600263 /* Set up a minimal EC0 device to pass to the DPTF helpers */
264 path.type = DEVICE_PATH_GENERIC;
265 path.generic.id = 0;
266 ec = alloc_find_dev(dev->bus, &path);
267 ec->ops = &ec_ops;
268
269 if (CONFIG(DRIVERS_INTEL_DPTF))
270 ec_fill_dptf_helpers(ec);
271
Rajat Jain962c7882020-04-01 17:24:28 -0700272 fill_ssdt_typec_device(dev);
273 fill_ssdt_ps2_keyboard(dev);
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700274}