summaryrefslogtreecommitdiff
path: root/src/ec/google/chromeec/ec_acpi.c
blob: 0244fd15e8f9c2206760bb0155d6c762b73b4ac2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include <acpi/acpi.h>
#include <acpi/acpi_device.h>
#include <acpi/acpi_pld.h>
#include <acpi/acpigen.h>
#include <acpi/acpigen_ps2_keybd.h>
#include <acpi/acpigen_usb.h>
#include <console/console.h>
#include <drivers/usb/acpi/chip.h>
#include <drivers/intel/usb4/retimer/retimer.h>
#include <ec/google/common/dptf.h>

#include "chip.h"
#include "ec.h"
#include "ec_commands.h"

#define GOOGLE_CHROMEEC_USBC_DEVICE_HID		"GOOG0014"
#define GOOGLE_CHROMEEC_USBC_DEVICE_NAME	"USBC"

const char *google_chromeec_acpi_name(const struct device *dev)
{
	/*
	 * Chrome EC device (CREC - GOOG0004) is really a child of EC device (EC - PNP0C09) in
	 * ACPI tables. However, in coreboot device tree, there is no separate chip/device for
	 * EC0. Thus, Chrome EC device needs to return "EC0.CREC" as the ACPI name so that the
	 * callers can get the correct acpi device path/scope for this device.
	 *
	 * If we ever enable a separate driver for generating AML for EC0 device, then this
	 * function needs to be updated to return "CREC".
	 */
	return "EC0.CREC";
}

/*
 * Helper for fill_ssdt_generator. This adds references to the USB
 * port objects so that the consumer of this information can know
 * whether the port supports USB2 and/or USB3.
 */
static void get_usb_port_references(int port_number, struct device **usb2_port,
				    struct device **usb3_port, struct device **usb4_port)
{
	struct drivers_usb_acpi_config *config;
	struct device *port = NULL;

	/* Search through the devicetree for matching USB Type-C ports */
	while ((port = dev_find_path(port, DEVICE_PATH_USB)) != NULL) {
		if (!port->enabled || port->path.type != DEVICE_PATH_USB)
			continue;

		config = port->chip_info;

		/* Look at only USB Type-C ports */
		if ((config->type != UPC_TYPE_C_USB2_ONLY) &&
		    (config->type != UPC_TYPE_C_USB2_SS_SWITCH) &&
		    (config->type != UPC_TYPE_C_USB2_SS))
			continue;

		/*
		 * Check for a matching port number (the 'token' field in 'group').  Note that
		 * 'port_number' is 0-based, whereas the 'token' field is 1-based.
		 */
		int group_token;
		if (config->use_custom_pld)
			group_token = config->custom_pld.group.token;
		else
			group_token = config->group.token;
		if (group_token != (port_number + 1))
			continue;

		switch (port->path.usb.port_type) {
		case 2:
			*usb2_port = port;
			break;
		case 3:
			*usb3_port = port;
			break;
		case 4:
			*usb4_port = port;
			break;
		default:
			break;
		}
	}
}

/*
 * Apparently these are supposed to be uppercase, in contrast to the other
 * lowercase fields.
 */
static const char *port_location_to_str(enum ec_pd_port_location port_location)
{
	switch (port_location) {
	case EC_PD_PORT_LOCATION_LEFT:
		return "LEFT";
	case EC_PD_PORT_LOCATION_RIGHT:
		return "RIGHT";
	case EC_PD_PORT_LOCATION_BACK:
		return "BACK";
	case EC_PD_PORT_LOCATION_FRONT:
		return "FRONT";
	case EC_PD_PORT_LOCATION_LEFT_FRONT:
		return "LEFT_FRONT";
	case EC_PD_PORT_LOCATION_LEFT_BACK:
		return "LEFT_BACK";
	case EC_PD_PORT_LOCATION_RIGHT_FRONT:
		return "RIGHT_FRONT";
	case EC_PD_PORT_LOCATION_RIGHT_BACK:
		return "RIGHT_BACK";
	case EC_PD_PORT_LOCATION_BACK_LEFT:
		return "BACK_LEFT";
	case EC_PD_PORT_LOCATION_BACK_RIGHT:
		return "BACK_RIGHT";
	case EC_PD_PORT_LOCATION_UNKNOWN:
		__fallthrough;
	default:
		return "UNKNOWN";
	}
}

static struct usb_pd_port_caps port_caps;
static void add_port_location(struct acpi_dp *dsd, int port_number)
{
	acpi_dp_add_string(dsd, "port-location", port_location_to_str(port_caps.port_location));
}

static void get_pld_from_usb_ports(struct acpi_pld *pld,
	struct device *usb2_port, struct device *usb3_port,
	struct device *usb4_port)
{
	struct drivers_usb_acpi_config *config = NULL;

	if (usb4_port)
		config = usb4_port->chip_info;
	else if (usb3_port)
		config = usb3_port->chip_info;
	else if (usb2_port)
		config = usb2_port->chip_info;

	if (config) {
		if (config->use_custom_pld)
			*pld = config->custom_pld;
		else
			acpi_pld_fill_usb(pld, config->type, &config->group);
	}
}

static void fill_ssdt_typec_device(const struct device *dev)
{
	struct ec_google_chromeec_config *config = dev->chip_info;
	int rv;
	int i;
	unsigned int num_ports = 0;
	struct device *usb2_port;
	struct device *usb3_port;
	struct device *usb4_port;
	struct acpi_pld pld = {0};
	uint32_t pcap_mask = 0;

	rv = google_chromeec_get_num_pd_ports(&num_ports);
	if (rv || num_ports == 0)
		return;

	/* If we can't get port caps, we shouldn't bother creating a device. */
	rv = google_chromeec_get_cmd_versions(EC_CMD_GET_PD_PORT_CAPS, &pcap_mask);
	if (rv || pcap_mask == 0)
		return;

	acpigen_write_scope(acpi_device_path(dev));
	acpigen_write_device(GOOGLE_CHROMEEC_USBC_DEVICE_NAME);
	acpigen_write_name_string("_HID", GOOGLE_CHROMEEC_USBC_DEVICE_HID);
	acpigen_write_name_string("_DDN", "ChromeOS EC Embedded Controller "
				"USB Type-C Control");

	for (i = 0; i < num_ports; ++i) {
		rv = google_chromeec_get_pd_port_caps(i, &port_caps);
		if (rv)
			continue;

		usb2_port = NULL;
		usb3_port = NULL;
		usb4_port = NULL;
		get_usb_port_references(i, &usb2_port, &usb3_port, &usb4_port);

		get_pld_from_usb_ports(&pld, usb2_port, usb3_port, usb4_port);

		struct typec_connector_class_config typec_config = {
			.power_role = port_caps.power_role_cap,
			.try_power_role = port_caps.try_power_role_cap,
			.data_role = port_caps.data_role_cap,
			.usb2_port = usb2_port,
			.usb3_port = usb3_port,
			.usb4_port = usb4_port,
			.orientation_switch = config->mux_conn[i],
			.usb_role_switch = config->mux_conn[i],
			.mode_switch = config->mux_conn[i],
			.pld = &pld,
		};

		acpigen_write_typec_connector(&typec_config, i, add_port_location);
	}

	acpigen_pop_len(); /* Device GOOGLE_CHROMEEC_USBC_DEVICE_NAME */
	acpigen_pop_len(); /* Scope */
}

static const enum ps2_action_key ps2_enum_val[] = {
	[TK_ABSENT] = PS2_KEY_ABSENT,
	[TK_BACK] = PS2_KEY_BACK,
	[TK_FORWARD] = PS2_KEY_FORWARD,
	[TK_REFRESH] = PS2_KEY_REFRESH,
	[TK_FULLSCREEN] = PS2_KEY_FULLSCREEN,
	[TK_OVERVIEW] = PS2_KEY_OVERVIEW,
	[TK_BRIGHTNESS_DOWN] = PS2_KEY_BRIGHTNESS_DOWN,
	[TK_BRIGHTNESS_UP] = PS2_KEY_BRIGHTNESS_UP,
	[TK_VOL_MUTE] = PS2_KEY_VOL_MUTE,
	[TK_VOL_DOWN] = PS2_KEY_VOL_DOWN,
	[TK_VOL_UP] = PS2_KEY_VOL_UP,
	[TK_SNAPSHOT] = PS2_KEY_SNAPSHOT,
	[TK_PRIVACY_SCRN_TOGGLE] = PS2_KEY_PRIVACY_SCRN_TOGGLE,
	[TK_KBD_BKLIGHT_DOWN] = PS2_KEY_KBD_BKLIGHT_DOWN,
	[TK_KBD_BKLIGHT_UP] = PS2_KEY_KBD_BKLIGHT_UP,
	[TK_PLAY_PAUSE] = PS2_KEY_PLAY_PAUSE,
	[TK_NEXT_TRACK] = PS2_KEY_NEXT_TRACK,
	[TK_PREV_TRACK] = PS2_KEY_PREV_TRACK,
	[TK_KBD_BKLIGHT_TOGGLE] = PS2_KEY_KBD_BKLIGHT_TOGGLE,
	[TK_MICMUTE] = PS2_KEY_MICMUTE,
	[TK_MENU] = PS2_KEY_MENU,
};

static void fill_ssdt_ps2_keyboard(const struct device *dev)
{
	uint8_t i;
	struct ec_response_keybd_config keybd = {};
	enum ps2_action_key ps2_action_keys[MAX_TOP_ROW_KEYS] = {};

	if (google_chromeec_get_keybd_config(&keybd) ||
	    !keybd.num_top_row_keys ||
	    keybd.num_top_row_keys > MAX_TOP_ROW_KEYS) {
		printk(BIOS_ERR, "PS2K: Bad resp from EC. Vivaldi disabled!\n");
		return;
	}

	/* Convert enum action_key values to enum ps2_action_key values */
	for (i = 0; i < keybd.num_top_row_keys; i++)
		ps2_action_keys[i] = ps2_enum_val[keybd.action_keys[i]];

	acpigen_ps2_keyboard_dsd("_SB.PCI0.PS2K", keybd.num_top_row_keys,
				 ps2_action_keys,
				 !!(keybd.capabilities & KEYBD_CAP_FUNCTION_KEYS),
				 !!(keybd.capabilities & KEYBD_CAP_NUMERIC_KEYPAD),
				 !!(keybd.capabilities & KEYBD_CAP_SCRNLOCK_KEY));
}

static const char *ec_acpi_name(const struct device *dev)
{
	return "EC0";
}

static struct device_operations ec_ops = {
	.acpi_name	= ec_acpi_name,
};

void google_chromeec_fill_ssdt_generator(const struct device *dev)
{
	struct device_path path;
	struct device *ec;

	/* Set up a minimal EC0 device to pass to the DPTF helpers */
	path.type = DEVICE_PATH_GENERIC;
	path.generic.id = 0;
	ec = alloc_find_dev(dev->bus, &path);
	ec->ops = &ec_ops;

	if (CONFIG(DRIVERS_INTEL_DPTF))
		ec_fill_dptf_helpers(ec);

	fill_ssdt_typec_device(dev);
	fill_ssdt_ps2_keyboard(dev);
}

const char *ec_retimer_fw_update_path(void)
{
	return "\\_SB_.PCI0.LPCB.EC0_.RFWU";
}

void ec_retimer_fw_update(uint8_t data)
{
	const char *RFWU = ec_retimer_fw_update_path();

	/*
	 * Write the EC RAM for Retimer Upgrade
	 * RFWU = data
	 */
	acpigen_write_store();
	acpigen_write_byte(data);
	acpigen_emit_namestring(RFWU);
}