blob: cf29636e343fcc6459066b6e47b8ed8d28a6c212 [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>
Won Chung667471b2021-11-15 21:19:51 +00005#include <acpi/acpi_pld.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07006#include <acpi/acpigen.h>
7#include <acpi/acpigen_ps2_keybd.h>
Tim Wawrzynczak6da82132020-05-19 12:46:04 -06008#include <acpi/acpigen_usb.h>
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -07009#include <console/console.h>
10#include <drivers/usb/acpi/chip.h>
John Zhaoeec3e3b2021-01-08 22:33:04 -080011#include <drivers/intel/usb4/retimer/retimer.h>
Tim Wawrzynczak93d7bcb2020-05-29 15:44:25 -060012#include <ec/google/common/dptf.h>
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070013
14#include "chip.h"
15#include "ec.h"
16#include "ec_commands.h"
17
Karthikeyan Ramasubramaniana95907b2020-04-13 18:03:29 -060018#define GOOGLE_CHROMEEC_USBC_DEVICE_HID "GOOG0014"
19#define GOOGLE_CHROMEEC_USBC_DEVICE_NAME "USBC"
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070020
21const char *google_chromeec_acpi_name(const struct device *dev)
22{
Furquan Shaikheec30f72020-04-20 16:38:21 -070023 /*
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 Wawrzynczakeb3cd852020-01-22 16:52:13 -070033}
34
Tim Wawrzynczak6da82132020-05-19 12:46:04 -060035/*
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 */
40static void get_usb_port_references(int port_number, struct device **usb2_port,
41 struct device **usb3_port, struct device **usb4_port)
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070042{
Tim Wawrzynczak6da82132020-05-19 12:46:04 -060043 struct drivers_usb_acpi_config *config;
44 struct device *port = NULL;
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -070045
Tim Wawrzynczak6da82132020-05-19 12:46:04 -060046 /* 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 Wawrzynczakeb3cd852020-01-22 16:52:13 -070059 /*
Tim Wawrzynczak6da82132020-05-19 12:46:04 -060060 * 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 Wawrzynczakeb3cd852020-01-22 16:52:13 -070062 */
Won Chungf6bd2782022-01-25 22:22:31 +000063 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 Wawrzynczak6da82132020-05-19 12:46:04 -060069 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";
Arthur Heymansfff20212021-03-15 14:56:16 +0100114 case EC_PD_PORT_LOCATION_UNKNOWN:
115 __fallthrough;
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700116 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 Wawrzynczake7881ed2020-09-30 13:12:26 -0600124 acpi_dp_add_string(dsd, "port-location", port_location_to_str(port_caps.port_location));
Tim Wawrzynczak16ef59e2020-06-01 20:07:45 -0600125}
126
Won Chung667471b2021-11-15 21:19:51 +0000127static void get_pld_from_usb_ports(struct acpi_pld *pld,
128 struct device *usb2_port, struct device *usb3_port,
129 struct device *usb4_port)
130{
131 struct drivers_usb_acpi_config *config = NULL;
132
133 if (usb4_port)
134 config = usb4_port->chip_info;
135 else if (usb3_port)
136 config = usb3_port->chip_info;
137 else if (usb2_port)
138 config = usb2_port->chip_info;
139
140 if (config) {
141 if (config->use_custom_pld)
142 *pld = config->custom_pld;
143 else
144 acpi_pld_fill_usb(pld, config->type, &config->group);
145 }
146}
147
Furquan Shaikh7536a392020-04-24 21:59:21 -0700148static void fill_ssdt_typec_device(const struct device *dev)
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700149{
Tim Wawrzynczake7881ed2020-09-30 13:12:26 -0600150 struct ec_google_chromeec_config *config = dev->chip_info;
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700151 int rv;
Divya Sasidharan49d74de2019-10-22 13:41:15 -0700152 int i;
Prashant Malani20f836b2022-01-19 18:58:44 +0000153 unsigned int num_ports = 0;
Tim Wawrzynczak6da82132020-05-19 12:46:04 -0600154 struct device *usb2_port;
155 struct device *usb3_port;
156 struct device *usb4_port;
Won Chung667471b2021-11-15 21:19:51 +0000157 struct acpi_pld pld = {0};
Prashant Malani20f836b2022-01-19 18:58:44 +0000158 uint32_t pcap_mask = 0;
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700159
Prashant Malani20f836b2022-01-19 18:58:44 +0000160 rv = google_chromeec_get_num_pd_ports(&num_ports);
161 if (rv || num_ports == 0)
162 return;
163
164 /* If we can't get port caps, we shouldn't bother creating a device. */
165 rv = google_chromeec_get_cmd_versions(EC_CMD_GET_PD_PORT_CAPS, &pcap_mask);
166 if (rv || pcap_mask == 0)
Rajat Jain962c7882020-04-01 17:24:28 -0700167 return;
168
Furquan Shaikheec30f72020-04-20 16:38:21 -0700169 acpigen_write_scope(acpi_device_path(dev));
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700170 acpigen_write_device(GOOGLE_CHROMEEC_USBC_DEVICE_NAME);
171 acpigen_write_name_string("_HID", GOOGLE_CHROMEEC_USBC_DEVICE_HID);
172 acpigen_write_name_string("_DDN", "ChromeOS EC Embedded Controller "
173 "USB Type-C Control");
174
175 for (i = 0; i < num_ports; ++i) {
176 rv = google_chromeec_get_pd_port_caps(i, &port_caps);
177 if (rv)
178 continue;
179
Tim Wawrzynczak6da82132020-05-19 12:46:04 -0600180 usb2_port = NULL;
181 usb3_port = NULL;
182 usb4_port = NULL;
183 get_usb_port_references(i, &usb2_port, &usb3_port, &usb4_port);
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700184
Won Chung667471b2021-11-15 21:19:51 +0000185 get_pld_from_usb_ports(&pld, usb2_port, usb3_port, usb4_port);
186
Tim Wawrzynczake7881ed2020-09-30 13:12:26 -0600187 struct typec_connector_class_config typec_config = {
Arthur Heymans4998aae2022-02-18 12:54:05 +0100188 .power_role = (enum usb_typec_power_role)port_caps.power_role_cap,
189 .try_power_role =
190 (enum usb_typec_try_power_role)port_caps.try_power_role_cap,
191 .data_role = (enum usb_typec_data_role)port_caps.data_role_cap,
Tim Wawrzynczak6da82132020-05-19 12:46:04 -0600192 .usb2_port = usb2_port,
193 .usb3_port = usb3_port,
194 .usb4_port = usb4_port,
Tim Wawrzynczake7881ed2020-09-30 13:12:26 -0600195 .orientation_switch = config->mux_conn[i],
196 .usb_role_switch = config->mux_conn[i],
197 .mode_switch = config->mux_conn[i],
Prashant Malanida6e9a02022-04-21 18:01:40 +0000198 .retimer_switch = config->retimer_conn[i],
Won Chung667471b2021-11-15 21:19:51 +0000199 .pld = &pld,
Tim Wawrzynczak6da82132020-05-19 12:46:04 -0600200 };
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700201
Tim Wawrzynczake7881ed2020-09-30 13:12:26 -0600202 acpigen_write_typec_connector(&typec_config, i, add_port_location);
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700203 }
204
205 acpigen_pop_len(); /* Device GOOGLE_CHROMEEC_USBC_DEVICE_NAME */
Rajat Jain962c7882020-04-01 17:24:28 -0700206 acpigen_pop_len(); /* Scope */
207}
208
209static const enum ps2_action_key ps2_enum_val[] = {
210 [TK_ABSENT] = PS2_KEY_ABSENT,
211 [TK_BACK] = PS2_KEY_BACK,
212 [TK_FORWARD] = PS2_KEY_FORWARD,
213 [TK_REFRESH] = PS2_KEY_REFRESH,
214 [TK_FULLSCREEN] = PS2_KEY_FULLSCREEN,
215 [TK_OVERVIEW] = PS2_KEY_OVERVIEW,
216 [TK_BRIGHTNESS_DOWN] = PS2_KEY_BRIGHTNESS_DOWN,
217 [TK_BRIGHTNESS_UP] = PS2_KEY_BRIGHTNESS_UP,
218 [TK_VOL_MUTE] = PS2_KEY_VOL_MUTE,
219 [TK_VOL_DOWN] = PS2_KEY_VOL_DOWN,
220 [TK_VOL_UP] = PS2_KEY_VOL_UP,
221 [TK_SNAPSHOT] = PS2_KEY_SNAPSHOT,
222 [TK_PRIVACY_SCRN_TOGGLE] = PS2_KEY_PRIVACY_SCRN_TOGGLE,
223 [TK_KBD_BKLIGHT_DOWN] = PS2_KEY_KBD_BKLIGHT_DOWN,
224 [TK_KBD_BKLIGHT_UP] = PS2_KEY_KBD_BKLIGHT_UP,
225 [TK_PLAY_PAUSE] = PS2_KEY_PLAY_PAUSE,
226 [TK_NEXT_TRACK] = PS2_KEY_NEXT_TRACK,
227 [TK_PREV_TRACK] = PS2_KEY_PREV_TRACK,
Scott Chao18141d8c2021-07-30 10:40:57 +0800228 [TK_KBD_BKLIGHT_TOGGLE] = PS2_KEY_KBD_BKLIGHT_TOGGLE,
229 [TK_MICMUTE] = PS2_KEY_MICMUTE,
Boris Mittelberg130de14a2022-02-10 16:30:40 -0800230 [TK_MENU] = PS2_KEY_MENU,
Rajat Jain962c7882020-04-01 17:24:28 -0700231};
232
Furquan Shaikh7536a392020-04-24 21:59:21 -0700233static void fill_ssdt_ps2_keyboard(const struct device *dev)
Rajat Jain962c7882020-04-01 17:24:28 -0700234{
235 uint8_t i;
236 struct ec_response_keybd_config keybd = {};
237 enum ps2_action_key ps2_action_keys[MAX_TOP_ROW_KEYS] = {};
238
239 if (google_chromeec_get_keybd_config(&keybd) ||
240 !keybd.num_top_row_keys ||
241 keybd.num_top_row_keys > MAX_TOP_ROW_KEYS) {
Matt DeVillier05be8c62023-01-26 14:33:54 -0600242 printk(BIOS_INFO, "PS2K: Unsupported or bad resp from EC. Vivaldi disabled!\n");
Rajat Jain962c7882020-04-01 17:24:28 -0700243 return;
244 }
245
246 /* Convert enum action_key values to enum ps2_action_key values */
247 for (i = 0; i < keybd.num_top_row_keys; i++)
248 ps2_action_keys[i] = ps2_enum_val[keybd.action_keys[i]];
249
250 acpigen_ps2_keyboard_dsd("_SB.PCI0.PS2K", keybd.num_top_row_keys,
251 ps2_action_keys,
252 !!(keybd.capabilities & KEYBD_CAP_FUNCTION_KEYS),
253 !!(keybd.capabilities & KEYBD_CAP_NUMERIC_KEYPAD),
Jonathon Hallb63017f2023-07-13 16:54:41 -0400254 !!(keybd.capabilities & KEYBD_CAP_SCRNLOCK_KEY),
255 true);
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700256}
257
Tim Wawrzynczak93d7bcb2020-05-29 15:44:25 -0600258static const char *ec_acpi_name(const struct device *dev)
259{
260 return "EC0";
261}
262
263static struct device_operations ec_ops = {
264 .acpi_name = ec_acpi_name,
265};
266
Furquan Shaikh7536a392020-04-24 21:59:21 -0700267void google_chromeec_fill_ssdt_generator(const struct device *dev)
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700268{
Tim Wawrzynczak93d7bcb2020-05-29 15:44:25 -0600269 struct device_path path;
270 struct device *ec;
271
Tim Wawrzynczak93d7bcb2020-05-29 15:44:25 -0600272 /* Set up a minimal EC0 device to pass to the DPTF helpers */
273 path.type = DEVICE_PATH_GENERIC;
274 path.generic.id = 0;
Akihiko Odakif0be9e32022-04-06 15:37:59 +0900275 path.generic.subid = 0;
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200276 ec = alloc_find_dev(dev->upstream, &path);
Tim Wawrzynczak93d7bcb2020-05-29 15:44:25 -0600277 ec->ops = &ec_ops;
278
279 if (CONFIG(DRIVERS_INTEL_DPTF))
Sumeet Pawnikar2f7fa552022-06-08 17:43:36 +0530280 ec_fill_dptf_helpers(ec, dev);
Tim Wawrzynczak93d7bcb2020-05-29 15:44:25 -0600281
Rajat Jain962c7882020-04-01 17:24:28 -0700282 fill_ssdt_typec_device(dev);
283 fill_ssdt_ps2_keyboard(dev);
Tim Wawrzynczakeb3cd852020-01-22 16:52:13 -0700284}
John Zhaoeec3e3b2021-01-08 22:33:04 -0800285
286const char *ec_retimer_fw_update_path(void)
287{
288 return "\\_SB_.PCI0.LPCB.EC0_.RFWU";
289}
John Zhao8bb83a32021-04-27 11:19:41 -0700290
291void ec_retimer_fw_update(uint8_t data)
292{
293 const char *RFWU = ec_retimer_fw_update_path();
294
295 /*
296 * Write the EC RAM for Retimer Upgrade
297 * RFWU = data
298 */
299 acpigen_write_store();
300 acpigen_write_byte(data);
301 acpigen_emit_namestring(RFWU);
302}