• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include <acpi/acpi.h>
4 #include <acpi/acpi_device.h>
5 #include <acpi/acpi_pld.h>
6 #include <acpi/acpigen.h>
7 #include <acpi/acpigen_ps2_keybd.h>
8 #include <acpi/acpigen_usb.h>
9 #include <console/console.h>
10 #include <drivers/usb/acpi/chip.h>
11 #include <drivers/intel/usb4/retimer/retimer.h>
12 #include <ec/google/common/dptf.h>
13 
14 #include "chip.h"
15 #include "ec.h"
16 #include "ec_commands.h"
17 
18 #define GOOGLE_CHROMEEC_USBC_DEVICE_HID		"GOOG0014"
19 #define GOOGLE_CHROMEEC_USBC_DEVICE_NAME	"USBC"
20 
google_chromeec_acpi_name(const struct device * dev)21 const char *google_chromeec_acpi_name(const struct device *dev)
22 {
23 	/*
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";
33 }
34 
35 /*
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  */
get_usb_port_references(int port_number,struct device ** usb2_port,struct device ** usb3_port,struct device ** usb4_port)40 static void get_usb_port_references(int port_number, struct device **usb2_port,
41 				    struct device **usb3_port, struct device **usb4_port)
42 {
43 	struct drivers_usb_acpi_config *config;
44 	struct device *port = NULL;
45 
46 	/* 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 
59 		/*
60 		 * 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.
62 		 */
63 		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))
69 			continue;
70 
71 		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 		}
84 	}
85 }
86 
87 /*
88  * Apparently these are supposed to be uppercase, in contrast to the other
89  * lowercase fields.
90  */
port_location_to_str(enum ec_pd_port_location port_location)91 static 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:
115 		__fallthrough;
116 	default:
117 		return "UNKNOWN";
118 	}
119 }
120 
121 static struct usb_pd_port_caps port_caps;
add_port_location(struct acpi_dp * dsd,int port_number)122 static void add_port_location(struct acpi_dp *dsd, int port_number)
123 {
124 	acpi_dp_add_string(dsd, "port-location", port_location_to_str(port_caps.port_location));
125 }
126 
get_pld_from_usb_ports(struct acpi_pld * pld,struct device * usb2_port,struct device * usb3_port,struct device * usb4_port)127 static 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 
fill_ssdt_typec_device(const struct device * dev)148 static void fill_ssdt_typec_device(const struct device *dev)
149 {
150 	struct ec_google_chromeec_config *config = dev->chip_info;
151 	int rv;
152 	int i;
153 	unsigned int num_ports = 0;
154 	struct device *usb2_port;
155 	struct device *usb3_port;
156 	struct device *usb4_port;
157 	struct acpi_pld pld = {0};
158 	uint32_t pcap_mask = 0;
159 
160 	/* UCSI implementations do not require an ACPI device with mux info since the
161 	   linux kernel doesn't set the muxes. */
162 	if (google_chromeec_get_ucsi_enabled())
163 		return;
164 
165 	rv = google_chromeec_get_num_pd_ports(&num_ports);
166 	if (rv || num_ports == 0)
167 		return;
168 
169 	/* If we can't get port caps, we shouldn't bother creating a device. */
170 	rv = google_chromeec_get_cmd_versions(EC_CMD_GET_PD_PORT_CAPS, &pcap_mask);
171 	if (rv || pcap_mask == 0)
172 		return;
173 
174 	acpigen_write_scope(acpi_device_path(dev));
175 	acpigen_write_device(GOOGLE_CHROMEEC_USBC_DEVICE_NAME);
176 	acpigen_write_name_string("_HID", GOOGLE_CHROMEEC_USBC_DEVICE_HID);
177 	acpigen_write_name_string("_DDN", "ChromeOS EC Embedded Controller "
178 				"USB Type-C Control");
179 
180 	for (i = 0; i < num_ports; ++i) {
181 		rv = google_chromeec_get_pd_port_caps(i, &port_caps);
182 		if (rv)
183 			continue;
184 
185 		usb2_port = NULL;
186 		usb3_port = NULL;
187 		usb4_port = NULL;
188 		get_usb_port_references(i, &usb2_port, &usb3_port, &usb4_port);
189 
190 		get_pld_from_usb_ports(&pld, usb2_port, usb3_port, usb4_port);
191 
192 		struct typec_connector_class_config typec_config = {
193 			.power_role = (enum usb_typec_power_role)port_caps.power_role_cap,
194 			.try_power_role =
195 				(enum usb_typec_try_power_role)port_caps.try_power_role_cap,
196 			.data_role = (enum usb_typec_data_role)port_caps.data_role_cap,
197 			.usb2_port = usb2_port,
198 			.usb3_port = usb3_port,
199 			.usb4_port = usb4_port,
200 			.orientation_switch = config->mux_conn[i],
201 			.usb_role_switch = config->mux_conn[i],
202 			.mode_switch = config->mux_conn[i],
203 			.retimer_switch = config->retimer_conn[i],
204 			.pld = &pld,
205 		};
206 
207 		acpigen_write_typec_connector(&typec_config, i, add_port_location);
208 	}
209 
210 	acpigen_pop_len(); /* Device GOOGLE_CHROMEEC_USBC_DEVICE_NAME */
211 	acpigen_pop_len(); /* Scope */
212 }
213 
214 static const enum ps2_action_key ps2_enum_val[] = {
215 	[TK_ABSENT] = PS2_KEY_ABSENT,
216 	[TK_BACK] = PS2_KEY_BACK,
217 	[TK_FORWARD] = PS2_KEY_FORWARD,
218 	[TK_REFRESH] = PS2_KEY_REFRESH,
219 	[TK_FULLSCREEN] = PS2_KEY_FULLSCREEN,
220 	[TK_OVERVIEW] = PS2_KEY_OVERVIEW,
221 	[TK_BRIGHTNESS_DOWN] = PS2_KEY_BRIGHTNESS_DOWN,
222 	[TK_BRIGHTNESS_UP] = PS2_KEY_BRIGHTNESS_UP,
223 	[TK_VOL_MUTE] = PS2_KEY_VOL_MUTE,
224 	[TK_VOL_DOWN] = PS2_KEY_VOL_DOWN,
225 	[TK_VOL_UP] = PS2_KEY_VOL_UP,
226 	[TK_SNAPSHOT] = PS2_KEY_SNAPSHOT,
227 	[TK_PRIVACY_SCRN_TOGGLE] = PS2_KEY_PRIVACY_SCRN_TOGGLE,
228 	[TK_KBD_BKLIGHT_DOWN] = PS2_KEY_KBD_BKLIGHT_DOWN,
229 	[TK_KBD_BKLIGHT_UP] = PS2_KEY_KBD_BKLIGHT_UP,
230 	[TK_PLAY_PAUSE] = PS2_KEY_PLAY_PAUSE,
231 	[TK_NEXT_TRACK] = PS2_KEY_NEXT_TRACK,
232 	[TK_PREV_TRACK] = PS2_KEY_PREV_TRACK,
233 	[TK_KBD_BKLIGHT_TOGGLE] = PS2_KEY_KBD_BKLIGHT_TOGGLE,
234 	[TK_MICMUTE] = PS2_KEY_MICMUTE,
235 	[TK_MENU] = PS2_KEY_MENU,
236 	[TK_DICTATE] = PS2_KEY_DICTATE,
237 	[TK_ACCESSIBILITY] = PS2_KEY_ACCESSIBILITY,
238 	[TK_DONOTDISTURB] = PS2_KEY_DO_NOT_DISTURB,
239 };
240 
fill_ssdt_ps2_keyboard(const struct device * dev)241 static void fill_ssdt_ps2_keyboard(const struct device *dev)
242 {
243 	uint8_t i;
244 	struct ec_response_keybd_config keybd = {};
245 	enum ps2_action_key ps2_action_keys[MAX_TOP_ROW_KEYS] = {};
246 
247 	if (google_chromeec_get_keybd_config(&keybd) ||
248 	    !keybd.num_top_row_keys ||
249 	    keybd.num_top_row_keys > MAX_TOP_ROW_KEYS) {
250 		printk(BIOS_INFO, "PS2K: Unsupported or bad resp from EC. Vivaldi disabled!\n");
251 		return;
252 	}
253 
254 	/* Convert enum action_key values to enum ps2_action_key values */
255 	for (i = 0; i < keybd.num_top_row_keys; i++)
256 		ps2_action_keys[i] = ps2_enum_val[keybd.action_keys[i]];
257 
258 	acpigen_ps2_keyboard_dsd("_SB.PCI0.PS2K", keybd.num_top_row_keys,
259 				 ps2_action_keys,
260 				 !!(keybd.capabilities & KEYBD_CAP_FUNCTION_KEYS),
261 				 !!(keybd.capabilities & KEYBD_CAP_NUMERIC_KEYPAD),
262 				 !!(keybd.capabilities & KEYBD_CAP_SCRNLOCK_KEY),
263 				 !!(keybd.capabilities & KEYBD_CAP_ASSISTANT_KEY),
264 				 true);
265 }
266 
ec_acpi_name(const struct device * dev)267 static const char *ec_acpi_name(const struct device *dev)
268 {
269 	return "EC0";
270 }
271 
272 static struct device_operations ec_ops = {
273 	.acpi_name	= ec_acpi_name,
274 };
275 
google_chromeec_fill_ssdt_generator(const struct device * dev)276 void google_chromeec_fill_ssdt_generator(const struct device *dev)
277 {
278 	struct device_path path;
279 	struct device *ec;
280 
281 	/* Set up a minimal EC0 device to pass to the DPTF helpers */
282 	path.type = DEVICE_PATH_GENERIC;
283 	path.generic.id = 0;
284 	path.generic.subid = 0;
285 	ec = alloc_find_dev(dev->upstream, &path);
286 	ec->ops = &ec_ops;
287 
288 	if (CONFIG(DRIVERS_INTEL_DPTF))
289 		ec_fill_dptf_helpers(ec, dev);
290 
291 	fill_ssdt_typec_device(dev);
292 	fill_ssdt_ps2_keyboard(dev);
293 }
294 
ec_retimer_fw_update_path(void)295 const char *ec_retimer_fw_update_path(void)
296 {
297 	return "\\_SB_.PCI0.LPCB.EC0_.RFWU";
298 }
299 
ec_retimer_fw_update(uint8_t data)300 void ec_retimer_fw_update(uint8_t data)
301 {
302 	const char *RFWU = ec_retimer_fw_update_path();
303 
304 	/*
305 	 * Write the EC RAM for Retimer Upgrade
306 	 * RFWU = data
307 	 */
308 	acpigen_write_store();
309 	acpigen_write_byte(data);
310 	acpigen_emit_namestring(RFWU);
311 }
312