• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2020 Google LLC
4  *
5  * This driver provides the ability to view and manage Type C ports through the
6  * Chrome OS EC.
7  */
8 
9 #include <linux/acpi.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/platform_data/cros_ec_commands.h>
13 #include <linux/platform_data/cros_ec_proto.h>
14 #include <linux/platform_data/cros_usbpd_notify.h>
15 #include <linux/platform_device.h>
16 #include <linux/usb/pd.h>
17 #include <linux/usb/typec.h>
18 #include <linux/usb/typec_altmode.h>
19 #include <linux/usb/typec_dp.h>
20 #include <linux/usb/typec_mux.h>
21 #include <linux/usb/typec_tbt.h>
22 #include <linux/usb/role.h>
23 
24 #define DRV_NAME "cros-ec-typec"
25 
26 /* Supported alt modes. */
27 enum {
28 	CROS_EC_ALTMODE_DP = 0,
29 	CROS_EC_ALTMODE_TBT,
30 	CROS_EC_ALTMODE_MAX,
31 };
32 
33 /* Per port data. */
34 struct cros_typec_port {
35 	struct typec_port *port;
36 	/* Initial capabilities for the port. */
37 	struct typec_capability caps;
38 	struct typec_partner *partner;
39 	/* Port partner PD identity info. */
40 	struct usb_pd_identity p_identity;
41 	struct typec_switch *ori_sw;
42 	struct typec_mux *mux;
43 	struct usb_role_switch *role_sw;
44 
45 	/* Variables keeping track of switch state. */
46 	struct typec_mux_state state;
47 	uint8_t mux_flags;
48 
49 	/* Port alt modes. */
50 	struct typec_altmode p_altmode[CROS_EC_ALTMODE_MAX];
51 };
52 
53 /* Platform-specific data for the Chrome OS EC Type C controller. */
54 struct cros_typec_data {
55 	struct device *dev;
56 	struct cros_ec_device *ec;
57 	int num_ports;
58 	unsigned int pd_ctrl_ver;
59 	/* Array of ports, indexed by port number. */
60 	struct cros_typec_port *ports[EC_USB_PD_MAX_PORTS];
61 	struct notifier_block nb;
62 	struct work_struct port_work;
63 };
64 
cros_typec_parse_port_props(struct typec_capability * cap,struct fwnode_handle * fwnode,struct device * dev)65 static int cros_typec_parse_port_props(struct typec_capability *cap,
66 				       struct fwnode_handle *fwnode,
67 				       struct device *dev)
68 {
69 	const char *buf;
70 	int ret;
71 
72 	memset(cap, 0, sizeof(*cap));
73 	ret = fwnode_property_read_string(fwnode, "power-role", &buf);
74 	if (ret) {
75 		dev_err(dev, "power-role not found: %d\n", ret);
76 		return ret;
77 	}
78 
79 	ret = typec_find_port_power_role(buf);
80 	if (ret < 0)
81 		return ret;
82 	cap->type = ret;
83 
84 	ret = fwnode_property_read_string(fwnode, "data-role", &buf);
85 	if (ret) {
86 		dev_err(dev, "data-role not found: %d\n", ret);
87 		return ret;
88 	}
89 
90 	ret = typec_find_port_data_role(buf);
91 	if (ret < 0)
92 		return ret;
93 	cap->data = ret;
94 
95 	ret = fwnode_property_read_string(fwnode, "try-power-role", &buf);
96 	if (ret) {
97 		dev_err(dev, "try-power-role not found: %d\n", ret);
98 		return ret;
99 	}
100 
101 	ret = typec_find_power_role(buf);
102 	if (ret < 0)
103 		return ret;
104 	cap->prefer_role = ret;
105 
106 	cap->fwnode = fwnode;
107 
108 	return 0;
109 }
110 
cros_typec_get_switch_handles(struct cros_typec_port * port,struct fwnode_handle * fwnode,struct device * dev)111 static int cros_typec_get_switch_handles(struct cros_typec_port *port,
112 					 struct fwnode_handle *fwnode,
113 					 struct device *dev)
114 {
115 	port->mux = fwnode_typec_mux_get(fwnode, NULL);
116 	if (IS_ERR(port->mux)) {
117 		dev_dbg(dev, "Mux handle not found.\n");
118 		goto mux_err;
119 	}
120 
121 	port->ori_sw = fwnode_typec_switch_get(fwnode);
122 	if (IS_ERR(port->ori_sw)) {
123 		dev_dbg(dev, "Orientation switch handle not found.\n");
124 		goto ori_sw_err;
125 	}
126 
127 	port->role_sw = fwnode_usb_role_switch_get(fwnode);
128 	if (IS_ERR(port->role_sw)) {
129 		dev_dbg(dev, "USB role switch handle not found.\n");
130 		goto role_sw_err;
131 	}
132 
133 	return 0;
134 
135 role_sw_err:
136 	usb_role_switch_put(port->role_sw);
137 ori_sw_err:
138 	typec_switch_put(port->ori_sw);
139 mux_err:
140 	typec_mux_put(port->mux);
141 
142 	return -ENODEV;
143 }
144 
cros_typec_add_partner(struct cros_typec_data * typec,int port_num,bool pd_en)145 static int cros_typec_add_partner(struct cros_typec_data *typec, int port_num,
146 				  bool pd_en)
147 {
148 	struct cros_typec_port *port = typec->ports[port_num];
149 	struct typec_partner_desc p_desc = {
150 		.usb_pd = pd_en,
151 	};
152 	int ret = 0;
153 
154 	/*
155 	 * Fill an initial PD identity, which will then be updated with info
156 	 * from the EC.
157 	 */
158 	p_desc.identity = &port->p_identity;
159 
160 	port->partner = typec_register_partner(port->port, &p_desc);
161 	if (IS_ERR(port->partner)) {
162 		ret = PTR_ERR(port->partner);
163 		port->partner = NULL;
164 	}
165 
166 	return ret;
167 }
168 
cros_typec_remove_partner(struct cros_typec_data * typec,int port_num)169 static void cros_typec_remove_partner(struct cros_typec_data *typec,
170 				     int port_num)
171 {
172 	struct cros_typec_port *port = typec->ports[port_num];
173 
174 	port->state.alt = NULL;
175 	port->state.mode = TYPEC_STATE_USB;
176 	port->state.data = NULL;
177 
178 	usb_role_switch_set_role(port->role_sw, USB_ROLE_NONE);
179 	typec_switch_set(port->ori_sw, TYPEC_ORIENTATION_NONE);
180 	typec_mux_set(port->mux, &port->state);
181 
182 	typec_unregister_partner(port->partner);
183 	port->partner = NULL;
184 }
185 
cros_unregister_ports(struct cros_typec_data * typec)186 static void cros_unregister_ports(struct cros_typec_data *typec)
187 {
188 	int i;
189 
190 	for (i = 0; i < typec->num_ports; i++) {
191 		if (!typec->ports[i])
192 			continue;
193 		cros_typec_remove_partner(typec, i);
194 		usb_role_switch_put(typec->ports[i]->role_sw);
195 		typec_switch_put(typec->ports[i]->ori_sw);
196 		typec_mux_put(typec->ports[i]->mux);
197 		typec_unregister_port(typec->ports[i]->port);
198 	}
199 }
200 
201 /*
202  * Fake the alt mode structs until we actually start registering Type C port
203  * and partner alt modes.
204  */
cros_typec_register_port_altmodes(struct cros_typec_data * typec,int port_num)205 static void cros_typec_register_port_altmodes(struct cros_typec_data *typec,
206 					      int port_num)
207 {
208 	struct cros_typec_port *port = typec->ports[port_num];
209 
210 	/* All PD capable CrOS devices are assumed to support DP altmode. */
211 	port->p_altmode[CROS_EC_ALTMODE_DP].svid = USB_TYPEC_DP_SID;
212 	port->p_altmode[CROS_EC_ALTMODE_DP].mode = USB_TYPEC_DP_MODE;
213 
214 	/*
215 	 * Register TBT compatibility alt mode. The EC will not enter the mode
216 	 * if it doesn't support it, so it's safe to register it unconditionally
217 	 * here for now.
218 	 */
219 	port->p_altmode[CROS_EC_ALTMODE_TBT].svid = USB_TYPEC_TBT_SID;
220 	port->p_altmode[CROS_EC_ALTMODE_TBT].mode = TYPEC_ANY_MODE;
221 
222 	port->state.alt = NULL;
223 	port->state.mode = TYPEC_STATE_USB;
224 	port->state.data = NULL;
225 }
226 
cros_typec_init_ports(struct cros_typec_data * typec)227 static int cros_typec_init_ports(struct cros_typec_data *typec)
228 {
229 	struct device *dev = typec->dev;
230 	struct typec_capability *cap;
231 	struct fwnode_handle *fwnode;
232 	struct cros_typec_port *cros_port;
233 	const char *port_prop;
234 	int ret;
235 	int nports;
236 	u32 port_num = 0;
237 
238 	nports = device_get_child_node_count(dev);
239 	if (nports == 0) {
240 		dev_err(dev, "No port entries found.\n");
241 		return -ENODEV;
242 	}
243 
244 	if (nports > typec->num_ports) {
245 		dev_err(dev, "More ports listed than can be supported.\n");
246 		return -EINVAL;
247 	}
248 
249 	/* DT uses "reg" to specify port number. */
250 	port_prop = dev->of_node ? "reg" : "port-number";
251 	device_for_each_child_node(dev, fwnode) {
252 		if (fwnode_property_read_u32(fwnode, port_prop, &port_num)) {
253 			ret = -EINVAL;
254 			dev_err(dev, "No port-number for port, aborting.\n");
255 			goto unregister_ports;
256 		}
257 
258 		if (port_num >= typec->num_ports) {
259 			dev_err(dev, "Invalid port number.\n");
260 			ret = -EINVAL;
261 			goto unregister_ports;
262 		}
263 
264 		dev_dbg(dev, "Registering port %d\n", port_num);
265 
266 		cros_port = devm_kzalloc(dev, sizeof(*cros_port), GFP_KERNEL);
267 		if (!cros_port) {
268 			ret = -ENOMEM;
269 			goto unregister_ports;
270 		}
271 
272 		typec->ports[port_num] = cros_port;
273 		cap = &cros_port->caps;
274 
275 		ret = cros_typec_parse_port_props(cap, fwnode, dev);
276 		if (ret < 0)
277 			goto unregister_ports;
278 
279 		cros_port->port = typec_register_port(dev, cap);
280 		if (IS_ERR(cros_port->port)) {
281 			dev_err(dev, "Failed to register port %d\n", port_num);
282 			ret = PTR_ERR(cros_port->port);
283 			goto unregister_ports;
284 		}
285 
286 		ret = cros_typec_get_switch_handles(cros_port, fwnode, dev);
287 		if (ret)
288 			dev_dbg(dev, "No switch control for port %d\n",
289 				port_num);
290 
291 		cros_typec_register_port_altmodes(typec, port_num);
292 	}
293 
294 	return 0;
295 
296 unregister_ports:
297 	cros_unregister_ports(typec);
298 	return ret;
299 }
300 
cros_typec_ec_command(struct cros_typec_data * typec,unsigned int version,unsigned int command,void * outdata,unsigned int outsize,void * indata,unsigned int insize)301 static int cros_typec_ec_command(struct cros_typec_data *typec,
302 				 unsigned int version,
303 				 unsigned int command,
304 				 void *outdata,
305 				 unsigned int outsize,
306 				 void *indata,
307 				 unsigned int insize)
308 {
309 	struct cros_ec_command *msg;
310 	int ret;
311 
312 	msg = kzalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL);
313 	if (!msg)
314 		return -ENOMEM;
315 
316 	msg->version = version;
317 	msg->command = command;
318 	msg->outsize = outsize;
319 	msg->insize = insize;
320 
321 	if (outsize)
322 		memcpy(msg->data, outdata, outsize);
323 
324 	ret = cros_ec_cmd_xfer_status(typec->ec, msg);
325 	if (ret >= 0 && insize)
326 		memcpy(indata, msg->data, insize);
327 
328 	kfree(msg);
329 	return ret;
330 }
331 
cros_typec_set_port_params_v0(struct cros_typec_data * typec,int port_num,struct ec_response_usb_pd_control * resp)332 static void cros_typec_set_port_params_v0(struct cros_typec_data *typec,
333 		int port_num, struct ec_response_usb_pd_control *resp)
334 {
335 	struct typec_port *port = typec->ports[port_num]->port;
336 	enum typec_orientation polarity;
337 
338 	if (!resp->enabled)
339 		polarity = TYPEC_ORIENTATION_NONE;
340 	else if (!resp->polarity)
341 		polarity = TYPEC_ORIENTATION_NORMAL;
342 	else
343 		polarity = TYPEC_ORIENTATION_REVERSE;
344 
345 	typec_set_pwr_role(port, resp->role ? TYPEC_SOURCE : TYPEC_SINK);
346 	typec_set_orientation(port, polarity);
347 }
348 
cros_typec_set_port_params_v1(struct cros_typec_data * typec,int port_num,struct ec_response_usb_pd_control_v1 * resp)349 static void cros_typec_set_port_params_v1(struct cros_typec_data *typec,
350 		int port_num, struct ec_response_usb_pd_control_v1 *resp)
351 {
352 	struct typec_port *port = typec->ports[port_num]->port;
353 	enum typec_orientation polarity;
354 	bool pd_en;
355 	int ret;
356 
357 	if (!(resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED))
358 		polarity = TYPEC_ORIENTATION_NONE;
359 	else if (!resp->polarity)
360 		polarity = TYPEC_ORIENTATION_NORMAL;
361 	else
362 		polarity = TYPEC_ORIENTATION_REVERSE;
363 	typec_set_orientation(port, polarity);
364 	typec_set_data_role(port, resp->role & PD_CTRL_RESP_ROLE_DATA ?
365 			TYPEC_HOST : TYPEC_DEVICE);
366 	typec_set_pwr_role(port, resp->role & PD_CTRL_RESP_ROLE_POWER ?
367 			TYPEC_SOURCE : TYPEC_SINK);
368 	typec_set_vconn_role(port, resp->role & PD_CTRL_RESP_ROLE_VCONN ?
369 			TYPEC_SOURCE : TYPEC_SINK);
370 
371 	/* Register/remove partners when a connect/disconnect occurs. */
372 	if (resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED) {
373 		if (typec->ports[port_num]->partner)
374 			return;
375 
376 		pd_en = resp->enabled & PD_CTRL_RESP_ENABLED_PD_CAPABLE;
377 		ret = cros_typec_add_partner(typec, port_num, pd_en);
378 		if (ret)
379 			dev_warn(typec->dev,
380 				 "Failed to register partner on port: %d\n",
381 				 port_num);
382 	} else {
383 		if (!typec->ports[port_num]->partner)
384 			return;
385 		cros_typec_remove_partner(typec, port_num);
386 	}
387 }
388 
cros_typec_get_mux_info(struct cros_typec_data * typec,int port_num,struct ec_response_usb_pd_mux_info * resp)389 static int cros_typec_get_mux_info(struct cros_typec_data *typec, int port_num,
390 				   struct ec_response_usb_pd_mux_info *resp)
391 {
392 	struct ec_params_usb_pd_mux_info req = {
393 		.port = port_num,
394 	};
395 
396 	return cros_typec_ec_command(typec, 0, EC_CMD_USB_PD_MUX_INFO, &req,
397 				     sizeof(req), resp, sizeof(*resp));
398 }
399 
cros_typec_usb_safe_state(struct cros_typec_port * port)400 static int cros_typec_usb_safe_state(struct cros_typec_port *port)
401 {
402 	port->state.mode = TYPEC_STATE_SAFE;
403 
404 	return typec_mux_set(port->mux, &port->state);
405 }
406 
407 /*
408  * Spoof the VDOs that were likely communicated by the partner for TBT alt
409  * mode.
410  */
cros_typec_enable_tbt(struct cros_typec_data * typec,int port_num,struct ec_response_usb_pd_control_v2 * pd_ctrl)411 static int cros_typec_enable_tbt(struct cros_typec_data *typec,
412 				 int port_num,
413 				 struct ec_response_usb_pd_control_v2 *pd_ctrl)
414 {
415 	struct cros_typec_port *port = typec->ports[port_num];
416 	struct typec_thunderbolt_data data;
417 	int ret;
418 
419 	if (typec->pd_ctrl_ver < 2) {
420 		dev_err(typec->dev,
421 			"PD_CTRL version too old: %d\n", typec->pd_ctrl_ver);
422 		return -ENOTSUPP;
423 	}
424 
425 	/* Device Discover Mode VDO */
426 	data.device_mode = TBT_MODE;
427 
428 	if (pd_ctrl->control_flags & USB_PD_CTRL_TBT_LEGACY_ADAPTER)
429 		data.device_mode = TBT_SET_ADAPTER(TBT_ADAPTER_TBT3);
430 
431 	/* Cable Discover Mode VDO */
432 	data.cable_mode = TBT_MODE;
433 	data.cable_mode |= TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed);
434 
435 	if (pd_ctrl->control_flags & USB_PD_CTRL_OPTICAL_CABLE)
436 		data.cable_mode |= TBT_CABLE_OPTICAL;
437 
438 	if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_LINK_UNIDIR)
439 		data.cable_mode |= TBT_CABLE_LINK_TRAINING;
440 
441 	if (pd_ctrl->cable_gen)
442 		data.cable_mode |= TBT_CABLE_ROUNDED;
443 
444 	/* Enter Mode VDO */
445 	data.enter_vdo = TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed);
446 
447 	if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE)
448 		data.enter_vdo |= TBT_ENTER_MODE_ACTIVE_CABLE;
449 
450 	if (!port->state.alt) {
451 		port->state.alt = &port->p_altmode[CROS_EC_ALTMODE_TBT];
452 		ret = cros_typec_usb_safe_state(port);
453 		if (ret)
454 			return ret;
455 	}
456 
457 	port->state.data = &data;
458 	port->state.mode = TYPEC_TBT_MODE;
459 
460 	return typec_mux_set(port->mux, &port->state);
461 }
462 
463 /* Spoof the VDOs that were likely communicated by the partner. */
cros_typec_enable_dp(struct cros_typec_data * typec,int port_num,struct ec_response_usb_pd_control_v2 * pd_ctrl)464 static int cros_typec_enable_dp(struct cros_typec_data *typec,
465 				int port_num,
466 				struct ec_response_usb_pd_control_v2 *pd_ctrl)
467 {
468 	struct cros_typec_port *port = typec->ports[port_num];
469 	struct typec_displayport_data dp_data;
470 	int ret;
471 
472 	if (typec->pd_ctrl_ver < 2) {
473 		dev_err(typec->dev,
474 			"PD_CTRL version too old: %d\n", typec->pd_ctrl_ver);
475 		return -ENOTSUPP;
476 	}
477 
478 	if (!pd_ctrl->dp_mode) {
479 		dev_err(typec->dev, "No valid DP mode provided.\n");
480 		return -EINVAL;
481 	}
482 
483 	/* Status VDO. */
484 	dp_data.status = DP_STATUS_ENABLED;
485 	if (port->mux_flags & USB_PD_MUX_HPD_IRQ)
486 		dp_data.status |= DP_STATUS_IRQ_HPD;
487 	if (port->mux_flags & USB_PD_MUX_HPD_LVL)
488 		dp_data.status |= DP_STATUS_HPD_STATE;
489 
490 	/* Configuration VDO. */
491 	dp_data.conf = DP_CONF_SET_PIN_ASSIGN(pd_ctrl->dp_mode);
492 	if (!port->state.alt) {
493 		port->state.alt = &port->p_altmode[CROS_EC_ALTMODE_DP];
494 		ret = cros_typec_usb_safe_state(port);
495 		if (ret)
496 			return ret;
497 	}
498 
499 	port->state.data = &dp_data;
500 	port->state.mode = TYPEC_MODAL_STATE(ffs(pd_ctrl->dp_mode));
501 
502 	return typec_mux_set(port->mux, &port->state);
503 }
504 
cros_typec_enable_usb4(struct cros_typec_data * typec,int port_num,struct ec_response_usb_pd_control_v2 * pd_ctrl)505 static int cros_typec_enable_usb4(struct cros_typec_data *typec,
506 				  int port_num,
507 				  struct ec_response_usb_pd_control_v2 *pd_ctrl)
508 {
509 	struct cros_typec_port *port = typec->ports[port_num];
510 	struct enter_usb_data data;
511 
512 	data.eudo = EUDO_USB_MODE_USB4 << EUDO_USB_MODE_SHIFT;
513 
514 	/* Cable Speed */
515 	data.eudo |= pd_ctrl->cable_speed << EUDO_CABLE_SPEED_SHIFT;
516 
517 	/* Cable Type */
518 	if (pd_ctrl->control_flags & USB_PD_CTRL_OPTICAL_CABLE)
519 		data.eudo |= EUDO_CABLE_TYPE_OPTICAL << EUDO_CABLE_TYPE_SHIFT;
520 	else if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE)
521 		data.eudo |= EUDO_CABLE_TYPE_RE_TIMER << EUDO_CABLE_TYPE_SHIFT;
522 
523 	data.active_link_training = !!(pd_ctrl->control_flags &
524 				       USB_PD_CTRL_ACTIVE_LINK_UNIDIR);
525 
526 	port->state.alt = NULL;
527 	port->state.data = &data;
528 	port->state.mode = TYPEC_MODE_USB4;
529 
530 	return typec_mux_set(port->mux, &port->state);
531 }
532 
cros_typec_configure_mux(struct cros_typec_data * typec,int port_num,uint8_t mux_flags,struct ec_response_usb_pd_control_v2 * pd_ctrl)533 static int cros_typec_configure_mux(struct cros_typec_data *typec, int port_num,
534 				uint8_t mux_flags,
535 				struct ec_response_usb_pd_control_v2 *pd_ctrl)
536 {
537 	struct cros_typec_port *port = typec->ports[port_num];
538 	enum typec_orientation orientation;
539 	int ret;
540 
541 	if (!port->partner)
542 		return 0;
543 
544 	if (mux_flags & USB_PD_MUX_POLARITY_INVERTED)
545 		orientation = TYPEC_ORIENTATION_REVERSE;
546 	else
547 		orientation = TYPEC_ORIENTATION_NORMAL;
548 
549 	ret = typec_switch_set(port->ori_sw, orientation);
550 	if (ret)
551 		return ret;
552 
553 	ret = usb_role_switch_set_role(typec->ports[port_num]->role_sw,
554 					pd_ctrl->role & PD_CTRL_RESP_ROLE_DATA
555 					? USB_ROLE_HOST : USB_ROLE_DEVICE);
556 	if (ret)
557 		return ret;
558 
559 	if (mux_flags & USB_PD_MUX_USB4_ENABLED) {
560 		ret = cros_typec_enable_usb4(typec, port_num, pd_ctrl);
561 	} else if (mux_flags & USB_PD_MUX_TBT_COMPAT_ENABLED) {
562 		ret = cros_typec_enable_tbt(typec, port_num, pd_ctrl);
563 	} else if (mux_flags & USB_PD_MUX_DP_ENABLED) {
564 		ret = cros_typec_enable_dp(typec, port_num, pd_ctrl);
565 	} else if (mux_flags & USB_PD_MUX_SAFE_MODE) {
566 		ret = cros_typec_usb_safe_state(port);
567 	} else if (mux_flags & USB_PD_MUX_USB_ENABLED) {
568 		port->state.alt = NULL;
569 		port->state.mode = TYPEC_STATE_USB;
570 		ret = typec_mux_set(port->mux, &port->state);
571 	} else {
572 		dev_info(typec->dev,
573 			 "Unsupported mode requested, mux flags: %x\n",
574 			 mux_flags);
575 		ret = -ENOTSUPP;
576 	}
577 
578 	return ret;
579 }
580 
cros_typec_port_update(struct cros_typec_data * typec,int port_num)581 static int cros_typec_port_update(struct cros_typec_data *typec, int port_num)
582 {
583 	struct ec_params_usb_pd_control req;
584 	struct ec_response_usb_pd_control_v2 resp;
585 	struct ec_response_usb_pd_mux_info mux_resp;
586 	int ret;
587 
588 	if (port_num < 0 || port_num >= typec->num_ports) {
589 		dev_err(typec->dev, "cannot get status for invalid port %d\n",
590 			port_num);
591 		return -EINVAL;
592 	}
593 
594 	req.port = port_num;
595 	req.role = USB_PD_CTRL_ROLE_NO_CHANGE;
596 	req.mux = USB_PD_CTRL_MUX_NO_CHANGE;
597 	req.swap = USB_PD_CTRL_SWAP_NONE;
598 
599 	ret = cros_typec_ec_command(typec, typec->pd_ctrl_ver,
600 				    EC_CMD_USB_PD_CONTROL, &req, sizeof(req),
601 				    &resp, sizeof(resp));
602 	if (ret < 0)
603 		return ret;
604 
605 	dev_dbg(typec->dev, "Enabled %d: 0x%hhx\n", port_num, resp.enabled);
606 	dev_dbg(typec->dev, "Role %d: 0x%hhx\n", port_num, resp.role);
607 	dev_dbg(typec->dev, "Polarity %d: 0x%hhx\n", port_num, resp.polarity);
608 	dev_dbg(typec->dev, "State %d: %s\n", port_num, resp.state);
609 
610 	if (typec->pd_ctrl_ver != 0)
611 		cros_typec_set_port_params_v1(typec, port_num,
612 			(struct ec_response_usb_pd_control_v1 *)&resp);
613 	else
614 		cros_typec_set_port_params_v0(typec, port_num,
615 			(struct ec_response_usb_pd_control *) &resp);
616 
617 	/* Update the switches if they exist, according to requested state */
618 	ret = cros_typec_get_mux_info(typec, port_num, &mux_resp);
619 	if (ret < 0) {
620 		dev_warn(typec->dev,
621 			 "Failed to get mux info for port: %d, err = %d\n",
622 			 port_num, ret);
623 		return 0;
624 	}
625 
626 	/* No change needs to be made, let's exit early. */
627 	if (typec->ports[port_num]->mux_flags == mux_resp.flags)
628 		return 0;
629 
630 	typec->ports[port_num]->mux_flags = mux_resp.flags;
631 	ret = cros_typec_configure_mux(typec, port_num, mux_resp.flags, &resp);
632 	if (ret)
633 		dev_warn(typec->dev, "Configure muxes failed, err = %d\n", ret);
634 
635 	return ret;
636 }
637 
cros_typec_get_cmd_version(struct cros_typec_data * typec)638 static int cros_typec_get_cmd_version(struct cros_typec_data *typec)
639 {
640 	struct ec_params_get_cmd_versions_v1 req_v1;
641 	struct ec_response_get_cmd_versions resp;
642 	int ret;
643 
644 	/* We're interested in the PD control command version. */
645 	req_v1.cmd = EC_CMD_USB_PD_CONTROL;
646 	ret = cros_typec_ec_command(typec, 1, EC_CMD_GET_CMD_VERSIONS,
647 				    &req_v1, sizeof(req_v1), &resp,
648 				    sizeof(resp));
649 	if (ret < 0)
650 		return ret;
651 
652 	if (resp.version_mask & EC_VER_MASK(2))
653 		typec->pd_ctrl_ver = 2;
654 	else if (resp.version_mask & EC_VER_MASK(1))
655 		typec->pd_ctrl_ver = 1;
656 	else
657 		typec->pd_ctrl_ver = 0;
658 
659 	dev_dbg(typec->dev, "PD Control has version mask 0x%hhx\n",
660 		typec->pd_ctrl_ver);
661 
662 	return 0;
663 }
664 
cros_typec_port_work(struct work_struct * work)665 static void cros_typec_port_work(struct work_struct *work)
666 {
667 	struct cros_typec_data *typec = container_of(work, struct cros_typec_data, port_work);
668 	int ret, i;
669 
670 	for (i = 0; i < typec->num_ports; i++) {
671 		ret = cros_typec_port_update(typec, i);
672 		if (ret < 0)
673 			dev_warn(typec->dev, "Update failed for port: %d\n", i);
674 	}
675 }
676 
cros_ec_typec_event(struct notifier_block * nb,unsigned long host_event,void * _notify)677 static int cros_ec_typec_event(struct notifier_block *nb,
678 			       unsigned long host_event, void *_notify)
679 {
680 	struct cros_typec_data *typec = container_of(nb, struct cros_typec_data, nb);
681 
682 	schedule_work(&typec->port_work);
683 
684 	return NOTIFY_OK;
685 }
686 
687 #ifdef CONFIG_ACPI
688 static const struct acpi_device_id cros_typec_acpi_id[] = {
689 	{ "GOOG0014", 0 },
690 	{}
691 };
692 MODULE_DEVICE_TABLE(acpi, cros_typec_acpi_id);
693 #endif
694 
695 #ifdef CONFIG_OF
696 static const struct of_device_id cros_typec_of_match[] = {
697 	{ .compatible = "google,cros-ec-typec", },
698 	{}
699 };
700 MODULE_DEVICE_TABLE(of, cros_typec_of_match);
701 #endif
702 
cros_typec_probe(struct platform_device * pdev)703 static int cros_typec_probe(struct platform_device *pdev)
704 {
705 	struct device *dev = &pdev->dev;
706 	struct cros_typec_data *typec;
707 	struct ec_response_usb_pd_ports resp;
708 	int ret, i;
709 
710 	typec = devm_kzalloc(dev, sizeof(*typec), GFP_KERNEL);
711 	if (!typec)
712 		return -ENOMEM;
713 
714 	typec->dev = dev;
715 	typec->ec = dev_get_drvdata(pdev->dev.parent);
716 	platform_set_drvdata(pdev, typec);
717 
718 	ret = cros_typec_get_cmd_version(typec);
719 	if (ret < 0) {
720 		dev_err(dev, "failed to get PD command version info\n");
721 		return ret;
722 	}
723 
724 	ret = cros_typec_ec_command(typec, 0, EC_CMD_USB_PD_PORTS, NULL, 0,
725 				    &resp, sizeof(resp));
726 	if (ret < 0)
727 		return ret;
728 
729 	typec->num_ports = resp.num_ports;
730 	if (typec->num_ports > EC_USB_PD_MAX_PORTS) {
731 		dev_warn(typec->dev,
732 			 "Too many ports reported: %d, limiting to max: %d\n",
733 			 typec->num_ports, EC_USB_PD_MAX_PORTS);
734 		typec->num_ports = EC_USB_PD_MAX_PORTS;
735 	}
736 
737 	ret = cros_typec_init_ports(typec);
738 	if (ret < 0)
739 		return ret;
740 
741 	INIT_WORK(&typec->port_work, cros_typec_port_work);
742 
743 	/*
744 	 * Safe to call port update here, since we haven't registered the
745 	 * PD notifier yet.
746 	 */
747 	for (i = 0; i < typec->num_ports; i++) {
748 		ret = cros_typec_port_update(typec, i);
749 		if (ret < 0)
750 			goto unregister_ports;
751 	}
752 
753 	typec->nb.notifier_call = cros_ec_typec_event;
754 	ret = cros_usbpd_register_notify(&typec->nb);
755 	if (ret < 0)
756 		goto unregister_ports;
757 
758 	return 0;
759 
760 unregister_ports:
761 	cros_unregister_ports(typec);
762 	return ret;
763 }
764 
cros_typec_suspend(struct device * dev)765 static int __maybe_unused cros_typec_suspend(struct device *dev)
766 {
767 	struct cros_typec_data *typec = dev_get_drvdata(dev);
768 
769 	cancel_work_sync(&typec->port_work);
770 
771 	return 0;
772 }
773 
cros_typec_resume(struct device * dev)774 static int __maybe_unused cros_typec_resume(struct device *dev)
775 {
776 	struct cros_typec_data *typec = dev_get_drvdata(dev);
777 
778 	/* Refresh port state. */
779 	schedule_work(&typec->port_work);
780 
781 	return 0;
782 }
783 
784 static const struct dev_pm_ops cros_typec_pm_ops = {
785 	SET_SYSTEM_SLEEP_PM_OPS(cros_typec_suspend, cros_typec_resume)
786 };
787 
788 static struct platform_driver cros_typec_driver = {
789 	.driver	= {
790 		.name = DRV_NAME,
791 		.acpi_match_table = ACPI_PTR(cros_typec_acpi_id),
792 		.of_match_table = of_match_ptr(cros_typec_of_match),
793 		.pm = &cros_typec_pm_ops,
794 	},
795 	.probe = cros_typec_probe,
796 };
797 
798 module_platform_driver(cros_typec_driver);
799 
800 MODULE_AUTHOR("Prashant Malani <pmalani@chromium.org>");
801 MODULE_DESCRIPTION("Chrome OS EC Type C control");
802 MODULE_LICENSE("GPL");
803