1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2020 Google LLC
5 * This driver provides the ability to view and manage Type C ports through the
9 #include <linux/acpi.h>
10 #include <linux/list.h>
11 #include <linux/module.h>
13 #include <linux/platform_data/cros_ec_commands.h>
14 #include <linux/platform_data/cros_ec_proto.h>
15 #include <linux/platform_data/cros_usbpd_notify.h>
16 #include <linux/platform_device.h>
17 #include <linux/usb/pd.h>
18 #include <linux/usb/pd_vdo.h>
19 #include <linux/usb/typec.h>
20 #include <linux/usb/typec_altmode.h>
21 #include <linux/usb/typec_dp.h>
22 #include <linux/usb/typec_mux.h>
23 #include <linux/usb/typec_tbt.h>
24 #include <linux/usb/role.h>
26 #define DRV_NAME "cros-ec-typec"
28 /* Supported alt modes. */
30 CROS_EC_ALTMODE_DP = 0,
35 /* Container for altmode pointer nodes. */
36 struct cros_typec_altmode_node {
37 struct typec_altmode *amode;
38 struct list_head list;
42 struct cros_typec_port {
43 struct typec_port *port;
44 /* Initial capabilities for the port. */
45 struct typec_capability caps;
46 struct typec_partner *partner;
47 struct typec_cable *cable;
49 struct typec_plug *plug;
50 /* Port partner PD identity info. */
51 struct usb_pd_identity p_identity;
52 /* Port cable PD identity info. */
53 struct usb_pd_identity c_identity;
54 struct typec_switch *ori_sw;
55 struct typec_mux *mux;
56 struct usb_role_switch *role_sw;
58 /* Variables keeping track of switch state. */
59 struct typec_mux_state state;
63 struct typec_altmode p_altmode[CROS_EC_ALTMODE_MAX];
65 /* Flag indicating that PD partner discovery data parsing is completed. */
67 bool sop_prime_disc_done;
68 struct ec_response_typec_discovery *disc_data;
69 struct list_head partner_mode_list;
70 struct list_head plug_mode_list;
73 /* Platform-specific data for the Chrome OS EC Type C controller. */
74 struct cros_typec_data {
76 struct cros_ec_device *ec;
78 unsigned int pd_ctrl_ver;
79 /* Array of ports, indexed by port number. */
80 struct cros_typec_port *ports[EC_USB_PD_MAX_PORTS];
81 struct notifier_block nb;
82 struct work_struct port_work;
83 bool typec_cmd_supported;
87 static int cros_typec_parse_port_props(struct typec_capability *cap,
88 struct fwnode_handle *fwnode,
94 memset(cap, 0, sizeof(*cap));
95 ret = fwnode_property_read_string(fwnode, "power-role", &buf);
97 dev_err(dev, "power-role not found: %d\n", ret);
101 ret = typec_find_port_power_role(buf);
106 ret = fwnode_property_read_string(fwnode, "data-role", &buf);
108 dev_err(dev, "data-role not found: %d\n", ret);
112 ret = typec_find_port_data_role(buf);
117 ret = fwnode_property_read_string(fwnode, "try-power-role", &buf);
119 dev_err(dev, "try-power-role not found: %d\n", ret);
123 ret = typec_find_power_role(buf);
126 cap->prefer_role = ret;
128 cap->fwnode = fwnode;
133 static int cros_typec_get_switch_handles(struct cros_typec_port *port,
134 struct fwnode_handle *fwnode,
137 port->mux = fwnode_typec_mux_get(fwnode, NULL);
138 if (IS_ERR(port->mux)) {
139 dev_dbg(dev, "Mux handle not found.\n");
143 port->ori_sw = fwnode_typec_switch_get(fwnode);
144 if (IS_ERR(port->ori_sw)) {
145 dev_dbg(dev, "Orientation switch handle not found.\n");
149 port->role_sw = fwnode_usb_role_switch_get(fwnode);
150 if (IS_ERR(port->role_sw)) {
151 dev_dbg(dev, "USB role switch handle not found.\n");
158 usb_role_switch_put(port->role_sw);
160 typec_switch_put(port->ori_sw);
162 typec_mux_put(port->mux);
167 static int cros_typec_add_partner(struct cros_typec_data *typec, int port_num,
170 struct cros_typec_port *port = typec->ports[port_num];
171 struct typec_partner_desc p_desc = {
177 * Fill an initial PD identity, which will then be updated with info
180 p_desc.identity = &port->p_identity;
182 port->partner = typec_register_partner(port->port, &p_desc);
183 if (IS_ERR(port->partner)) {
184 ret = PTR_ERR(port->partner);
185 port->partner = NULL;
191 static void cros_typec_unregister_altmodes(struct cros_typec_data *typec, int port_num,
194 struct cros_typec_port *port = typec->ports[port_num];
195 struct cros_typec_altmode_node *node, *tmp;
196 struct list_head *head;
198 head = is_partner ? &port->partner_mode_list : &port->plug_mode_list;
199 list_for_each_entry_safe(node, tmp, head, list) {
200 list_del(&node->list);
201 typec_unregister_altmode(node->amode);
202 devm_kfree(typec->dev, node);
206 static int cros_typec_usb_disconnect_state(struct cros_typec_port *port)
208 port->state.alt = NULL;
209 port->state.mode = TYPEC_STATE_USB;
210 port->state.data = NULL;
212 usb_role_switch_set_role(port->role_sw, USB_ROLE_NONE);
213 typec_switch_set(port->ori_sw, TYPEC_ORIENTATION_NONE);
215 return typec_mux_set(port->mux, &port->state);
218 static void cros_typec_remove_partner(struct cros_typec_data *typec,
221 struct cros_typec_port *port = typec->ports[port_num];
223 cros_typec_unregister_altmodes(typec, port_num, true);
225 cros_typec_usb_disconnect_state(port);
227 typec_unregister_partner(port->partner);
228 port->partner = NULL;
229 memset(&port->p_identity, 0, sizeof(port->p_identity));
230 port->sop_disc_done = false;
233 static void cros_typec_remove_cable(struct cros_typec_data *typec,
236 struct cros_typec_port *port = typec->ports[port_num];
238 cros_typec_unregister_altmodes(typec, port_num, false);
240 typec_unregister_plug(port->plug);
242 typec_unregister_cable(port->cable);
244 memset(&port->c_identity, 0, sizeof(port->c_identity));
245 port->sop_prime_disc_done = false;
248 static void cros_unregister_ports(struct cros_typec_data *typec)
252 for (i = 0; i < typec->num_ports; i++) {
253 if (!typec->ports[i])
256 if (typec->ports[i]->partner)
257 cros_typec_remove_partner(typec, i);
259 if (typec->ports[i]->cable)
260 cros_typec_remove_cable(typec, i);
262 usb_role_switch_put(typec->ports[i]->role_sw);
263 typec_switch_put(typec->ports[i]->ori_sw);
264 typec_mux_put(typec->ports[i]->mux);
265 typec_unregister_port(typec->ports[i]->port);
270 * Fake the alt mode structs until we actually start registering Type C port
271 * and partner alt modes.
273 static void cros_typec_register_port_altmodes(struct cros_typec_data *typec,
276 struct cros_typec_port *port = typec->ports[port_num];
278 /* All PD capable CrOS devices are assumed to support DP altmode. */
279 port->p_altmode[CROS_EC_ALTMODE_DP].svid = USB_TYPEC_DP_SID;
280 port->p_altmode[CROS_EC_ALTMODE_DP].mode = USB_TYPEC_DP_MODE;
283 * Register TBT compatibility alt mode. The EC will not enter the mode
284 * if it doesn't support it, so it's safe to register it unconditionally
287 port->p_altmode[CROS_EC_ALTMODE_TBT].svid = USB_TYPEC_TBT_SID;
288 port->p_altmode[CROS_EC_ALTMODE_TBT].mode = TYPEC_ANY_MODE;
290 port->state.alt = NULL;
291 port->state.mode = TYPEC_STATE_USB;
292 port->state.data = NULL;
295 static int cros_typec_init_ports(struct cros_typec_data *typec)
297 struct device *dev = typec->dev;
298 struct typec_capability *cap;
299 struct fwnode_handle *fwnode;
300 struct cros_typec_port *cros_port;
301 const char *port_prop;
306 nports = device_get_child_node_count(dev);
308 dev_err(dev, "No port entries found.\n");
312 if (nports > typec->num_ports) {
313 dev_err(dev, "More ports listed than can be supported.\n");
317 /* DT uses "reg" to specify port number. */
318 port_prop = dev->of_node ? "reg" : "port-number";
319 device_for_each_child_node(dev, fwnode) {
320 if (fwnode_property_read_u32(fwnode, port_prop, &port_num)) {
322 dev_err(dev, "No port-number for port, aborting.\n");
323 goto unregister_ports;
326 if (port_num >= typec->num_ports) {
327 dev_err(dev, "Invalid port number.\n");
329 goto unregister_ports;
332 dev_dbg(dev, "Registering port %d\n", port_num);
334 cros_port = devm_kzalloc(dev, sizeof(*cros_port), GFP_KERNEL);
337 goto unregister_ports;
340 typec->ports[port_num] = cros_port;
341 cap = &cros_port->caps;
343 ret = cros_typec_parse_port_props(cap, fwnode, dev);
345 goto unregister_ports;
347 cros_port->port = typec_register_port(dev, cap);
348 if (IS_ERR(cros_port->port)) {
349 dev_err(dev, "Failed to register port %d\n", port_num);
350 ret = PTR_ERR(cros_port->port);
351 goto unregister_ports;
354 ret = cros_typec_get_switch_handles(cros_port, fwnode, dev);
356 dev_dbg(dev, "No switch control for port %d\n",
359 cros_typec_register_port_altmodes(typec, port_num);
361 cros_port->disc_data = devm_kzalloc(dev, EC_PROTO2_MAX_RESPONSE_SIZE, GFP_KERNEL);
362 if (!cros_port->disc_data) {
364 goto unregister_ports;
367 INIT_LIST_HEAD(&cros_port->partner_mode_list);
368 INIT_LIST_HEAD(&cros_port->plug_mode_list);
374 cros_unregister_ports(typec);
378 static int cros_typec_ec_command(struct cros_typec_data *typec,
379 unsigned int version,
380 unsigned int command,
382 unsigned int outsize,
386 struct cros_ec_command *msg;
389 msg = kzalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL);
393 msg->version = version;
394 msg->command = command;
395 msg->outsize = outsize;
396 msg->insize = insize;
399 memcpy(msg->data, outdata, outsize);
401 ret = cros_ec_cmd_xfer_status(typec->ec, msg);
402 if (ret >= 0 && insize)
403 memcpy(indata, msg->data, insize);
409 static int cros_typec_usb_safe_state(struct cros_typec_port *port)
411 port->state.mode = TYPEC_STATE_SAFE;
413 return typec_mux_set(port->mux, &port->state);
417 * Spoof the VDOs that were likely communicated by the partner for TBT alt
420 static int cros_typec_enable_tbt(struct cros_typec_data *typec,
422 struct ec_response_usb_pd_control_v2 *pd_ctrl)
424 struct cros_typec_port *port = typec->ports[port_num];
425 struct typec_thunderbolt_data data;
428 if (typec->pd_ctrl_ver < 2) {
430 "PD_CTRL version too old: %d\n", typec->pd_ctrl_ver);
434 /* Device Discover Mode VDO */
435 data.device_mode = TBT_MODE;
437 if (pd_ctrl->control_flags & USB_PD_CTRL_TBT_LEGACY_ADAPTER)
438 data.device_mode = TBT_SET_ADAPTER(TBT_ADAPTER_TBT3);
440 /* Cable Discover Mode VDO */
441 data.cable_mode = TBT_MODE;
442 data.cable_mode |= TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed);
444 if (pd_ctrl->control_flags & USB_PD_CTRL_OPTICAL_CABLE)
445 data.cable_mode |= TBT_CABLE_OPTICAL;
447 if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_LINK_UNIDIR)
448 data.cable_mode |= TBT_CABLE_LINK_TRAINING;
450 data.cable_mode |= TBT_SET_CABLE_ROUNDED(pd_ctrl->cable_gen);
453 data.enter_vdo = TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed);
455 if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE)
456 data.enter_vdo |= TBT_ENTER_MODE_ACTIVE_CABLE;
458 if (!port->state.alt) {
459 port->state.alt = &port->p_altmode[CROS_EC_ALTMODE_TBT];
460 ret = cros_typec_usb_safe_state(port);
465 port->state.data = &data;
466 port->state.mode = TYPEC_TBT_MODE;
468 return typec_mux_set(port->mux, &port->state);
471 /* Spoof the VDOs that were likely communicated by the partner. */
472 static int cros_typec_enable_dp(struct cros_typec_data *typec,
474 struct ec_response_usb_pd_control_v2 *pd_ctrl)
476 struct cros_typec_port *port = typec->ports[port_num];
477 struct typec_displayport_data dp_data;
480 if (typec->pd_ctrl_ver < 2) {
482 "PD_CTRL version too old: %d\n", typec->pd_ctrl_ver);
487 dp_data.status = DP_STATUS_ENABLED;
488 if (port->mux_flags & USB_PD_MUX_HPD_IRQ)
489 dp_data.status |= DP_STATUS_IRQ_HPD;
490 if (port->mux_flags & USB_PD_MUX_HPD_LVL)
491 dp_data.status |= DP_STATUS_HPD_STATE;
493 /* Configuration VDO. */
494 dp_data.conf = DP_CONF_SET_PIN_ASSIGN(pd_ctrl->dp_mode);
495 if (!port->state.alt) {
496 port->state.alt = &port->p_altmode[CROS_EC_ALTMODE_DP];
497 ret = cros_typec_usb_safe_state(port);
502 port->state.data = &dp_data;
503 port->state.mode = TYPEC_MODAL_STATE(ffs(pd_ctrl->dp_mode));
505 return typec_mux_set(port->mux, &port->state);
508 static int cros_typec_enable_usb4(struct cros_typec_data *typec,
510 struct ec_response_usb_pd_control_v2 *pd_ctrl)
512 struct cros_typec_port *port = typec->ports[port_num];
513 struct enter_usb_data data;
515 data.eudo = EUDO_USB_MODE_USB4 << EUDO_USB_MODE_SHIFT;
518 data.eudo |= pd_ctrl->cable_speed << EUDO_CABLE_SPEED_SHIFT;
521 if (pd_ctrl->control_flags & USB_PD_CTRL_OPTICAL_CABLE)
522 data.eudo |= EUDO_CABLE_TYPE_OPTICAL << EUDO_CABLE_TYPE_SHIFT;
523 else if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE)
524 data.eudo |= EUDO_CABLE_TYPE_RE_TIMER << EUDO_CABLE_TYPE_SHIFT;
526 data.active_link_training = !!(pd_ctrl->control_flags &
527 USB_PD_CTRL_ACTIVE_LINK_UNIDIR);
529 port->state.alt = NULL;
530 port->state.data = &data;
531 port->state.mode = TYPEC_MODE_USB4;
533 return typec_mux_set(port->mux, &port->state);
536 static int cros_typec_configure_mux(struct cros_typec_data *typec, int port_num,
538 struct ec_response_usb_pd_control_v2 *pd_ctrl)
540 struct cros_typec_port *port = typec->ports[port_num];
541 struct ec_params_usb_pd_mux_ack mux_ack;
542 enum typec_orientation orientation;
545 if (mux_flags == USB_PD_MUX_NONE) {
546 ret = cros_typec_usb_disconnect_state(port);
550 if (mux_flags & USB_PD_MUX_POLARITY_INVERTED)
551 orientation = TYPEC_ORIENTATION_REVERSE;
553 orientation = TYPEC_ORIENTATION_NORMAL;
555 ret = typec_switch_set(port->ori_sw, orientation);
559 ret = usb_role_switch_set_role(typec->ports[port_num]->role_sw,
560 pd_ctrl->role & PD_CTRL_RESP_ROLE_DATA
561 ? USB_ROLE_HOST : USB_ROLE_DEVICE);
565 if (mux_flags & USB_PD_MUX_USB4_ENABLED) {
566 ret = cros_typec_enable_usb4(typec, port_num, pd_ctrl);
567 } else if (mux_flags & USB_PD_MUX_TBT_COMPAT_ENABLED) {
568 ret = cros_typec_enable_tbt(typec, port_num, pd_ctrl);
569 } else if (mux_flags & USB_PD_MUX_DP_ENABLED) {
570 ret = cros_typec_enable_dp(typec, port_num, pd_ctrl);
571 } else if (mux_flags & USB_PD_MUX_SAFE_MODE) {
572 ret = cros_typec_usb_safe_state(port);
573 } else if (mux_flags & USB_PD_MUX_USB_ENABLED) {
574 port->state.alt = NULL;
575 port->state.mode = TYPEC_STATE_USB;
576 ret = typec_mux_set(port->mux, &port->state);
579 "Unrecognized mode requested, mux flags: %x\n",
584 if (!typec->needs_mux_ack)
587 /* Sending Acknowledgment to EC */
588 mux_ack.port = port_num;
590 if (cros_typec_ec_command(typec, 0, EC_CMD_USB_PD_MUX_ACK, &mux_ack,
591 sizeof(mux_ack), NULL, 0) < 0)
593 "Failed to send Mux ACK to EC for port: %d\n",
599 static void cros_typec_set_port_params_v0(struct cros_typec_data *typec,
600 int port_num, struct ec_response_usb_pd_control *resp)
602 struct typec_port *port = typec->ports[port_num]->port;
603 enum typec_orientation polarity;
606 polarity = TYPEC_ORIENTATION_NONE;
607 else if (!resp->polarity)
608 polarity = TYPEC_ORIENTATION_NORMAL;
610 polarity = TYPEC_ORIENTATION_REVERSE;
612 typec_set_pwr_role(port, resp->role ? TYPEC_SOURCE : TYPEC_SINK);
613 typec_set_orientation(port, polarity);
616 static void cros_typec_set_port_params_v1(struct cros_typec_data *typec,
617 int port_num, struct ec_response_usb_pd_control_v1 *resp)
619 struct typec_port *port = typec->ports[port_num]->port;
620 enum typec_orientation polarity;
624 if (!(resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED))
625 polarity = TYPEC_ORIENTATION_NONE;
626 else if (!resp->polarity)
627 polarity = TYPEC_ORIENTATION_NORMAL;
629 polarity = TYPEC_ORIENTATION_REVERSE;
630 typec_set_orientation(port, polarity);
631 typec_set_data_role(port, resp->role & PD_CTRL_RESP_ROLE_DATA ?
632 TYPEC_HOST : TYPEC_DEVICE);
633 typec_set_pwr_role(port, resp->role & PD_CTRL_RESP_ROLE_POWER ?
634 TYPEC_SOURCE : TYPEC_SINK);
635 typec_set_vconn_role(port, resp->role & PD_CTRL_RESP_ROLE_VCONN ?
636 TYPEC_SOURCE : TYPEC_SINK);
638 /* Register/remove partners when a connect/disconnect occurs. */
639 if (resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED) {
640 if (typec->ports[port_num]->partner)
643 pd_en = resp->enabled & PD_CTRL_RESP_ENABLED_PD_CAPABLE;
644 ret = cros_typec_add_partner(typec, port_num, pd_en);
647 "Failed to register partner on port: %d\n",
650 if (typec->ports[port_num]->partner)
651 cros_typec_remove_partner(typec, port_num);
653 if (typec->ports[port_num]->cable)
654 cros_typec_remove_cable(typec, port_num);
658 static int cros_typec_get_mux_info(struct cros_typec_data *typec, int port_num,
659 struct ec_response_usb_pd_mux_info *resp)
661 struct ec_params_usb_pd_mux_info req = {
665 return cros_typec_ec_command(typec, 0, EC_CMD_USB_PD_MUX_INFO, &req,
666 sizeof(req), resp, sizeof(*resp));
670 * Helper function to register partner/plug altmodes.
672 static int cros_typec_register_altmodes(struct cros_typec_data *typec, int port_num,
675 struct cros_typec_port *port = typec->ports[port_num];
676 struct ec_response_typec_discovery *sop_disc = port->disc_data;
677 struct cros_typec_altmode_node *node;
678 struct typec_altmode_desc desc;
679 struct typec_altmode *amode;
680 int num_altmodes = 0;
684 for (i = 0; i < sop_disc->svid_count; i++) {
685 for (j = 0; j < sop_disc->svids[i].mode_count; j++) {
686 memset(&desc, 0, sizeof(desc));
687 desc.svid = sop_disc->svids[i].svid;
689 desc.vdo = sop_disc->svids[i].mode_vdo[j];
692 amode = typec_partner_register_altmode(port->partner, &desc);
694 amode = typec_plug_register_altmode(port->plug, &desc);
697 ret = PTR_ERR(amode);
701 /* If no memory is available we should unregister and exit. */
702 node = devm_kzalloc(typec->dev, sizeof(*node), GFP_KERNEL);
705 typec_unregister_altmode(amode);
712 list_add_tail(&node->list, &port->partner_mode_list);
714 list_add_tail(&node->list, &port->plug_mode_list);
720 ret = typec_partner_set_num_altmodes(port->partner, num_altmodes);
722 ret = typec_plug_set_num_altmodes(port->plug, num_altmodes);
725 dev_err(typec->dev, "Unable to set %s num_altmodes for port: %d\n",
726 is_partner ? "partner" : "plug", port_num);
733 cros_typec_unregister_altmodes(typec, port_num, is_partner);
738 * Parse the PD identity data from the EC PD discovery responses and copy that to the supplied
739 * PD identity struct.
741 static void cros_typec_parse_pd_identity(struct usb_pd_identity *id,
742 struct ec_response_typec_discovery *disc)
746 /* First, update the PD identity VDOs for the partner. */
747 if (disc->identity_count > 0)
748 id->id_header = disc->discovery_vdo[0];
749 if (disc->identity_count > 1)
750 id->cert_stat = disc->discovery_vdo[1];
751 if (disc->identity_count > 2)
752 id->product = disc->discovery_vdo[2];
754 /* Copy the remaining identity VDOs till a maximum of 6. */
755 for (i = 3; i < disc->identity_count && i < VDO_MAX_OBJECTS; i++)
756 id->vdo[i - 3] = disc->discovery_vdo[i];
759 static int cros_typec_handle_sop_prime_disc(struct cros_typec_data *typec, int port_num, u16 pd_revision)
761 struct cros_typec_port *port = typec->ports[port_num];
762 struct ec_response_typec_discovery *disc = port->disc_data;
763 struct typec_cable_desc c_desc = {};
764 struct typec_plug_desc p_desc;
765 struct ec_params_typec_discovery req = {
767 .partner_type = TYPEC_PARTNER_SOP_PRIME,
772 memset(disc, 0, EC_PROTO2_MAX_RESPONSE_SIZE);
773 ret = cros_typec_ec_command(typec, 0, EC_CMD_TYPEC_DISCOVERY, &req, sizeof(req),
774 disc, EC_PROTO2_MAX_RESPONSE_SIZE);
776 dev_err(typec->dev, "Failed to get SOP' discovery data for port: %d\n", port_num);
777 goto sop_prime_disc_exit;
780 /* Parse the PD identity data, even if only 0s were returned. */
781 cros_typec_parse_pd_identity(&port->c_identity, disc);
783 if (disc->identity_count != 0) {
784 cable_plug_type = VDO_TYPEC_CABLE_TYPE(port->c_identity.vdo[0]);
785 switch (cable_plug_type) {
787 c_desc.type = USB_PLUG_TYPE_A;
790 c_desc.type = USB_PLUG_TYPE_B;
793 c_desc.type = USB_PLUG_TYPE_C;
796 c_desc.type = USB_PLUG_CAPTIVE;
799 c_desc.type = USB_PLUG_NONE;
801 c_desc.active = PD_IDH_PTYPE(port->c_identity.id_header) == IDH_PTYPE_ACABLE;
804 c_desc.identity = &port->c_identity;
805 c_desc.pd_revision = pd_revision;
807 port->cable = typec_register_cable(port->port, &c_desc);
808 if (IS_ERR(port->cable)) {
809 ret = PTR_ERR(port->cable);
811 goto sop_prime_disc_exit;
814 p_desc.index = TYPEC_PLUG_SOP_P;
815 port->plug = typec_register_plug(port->cable, &p_desc);
816 if (IS_ERR(port->plug)) {
817 ret = PTR_ERR(port->plug);
819 goto sop_prime_disc_exit;
822 ret = cros_typec_register_altmodes(typec, port_num, false);
824 dev_err(typec->dev, "Failed to register plug altmodes, port: %d\n", port_num);
825 goto sop_prime_disc_exit;
831 cros_typec_remove_cable(typec, port_num);
835 static int cros_typec_handle_sop_disc(struct cros_typec_data *typec, int port_num, u16 pd_revision)
837 struct cros_typec_port *port = typec->ports[port_num];
838 struct ec_response_typec_discovery *sop_disc = port->disc_data;
839 struct ec_params_typec_discovery req = {
841 .partner_type = TYPEC_PARTNER_SOP,
845 if (!port->partner) {
847 "SOP Discovery received without partner registered, port: %d\n",
853 typec_partner_set_pd_revision(port->partner, pd_revision);
855 memset(sop_disc, 0, EC_PROTO2_MAX_RESPONSE_SIZE);
856 ret = cros_typec_ec_command(typec, 0, EC_CMD_TYPEC_DISCOVERY, &req, sizeof(req),
857 sop_disc, EC_PROTO2_MAX_RESPONSE_SIZE);
859 dev_err(typec->dev, "Failed to get SOP discovery data for port: %d\n", port_num);
863 cros_typec_parse_pd_identity(&port->p_identity, sop_disc);
865 ret = typec_partner_set_identity(port->partner);
867 dev_err(typec->dev, "Failed to update partner PD identity, port: %d\n", port_num);
871 ret = cros_typec_register_altmodes(typec, port_num, true);
873 dev_err(typec->dev, "Failed to register partner altmodes, port: %d\n", port_num);
881 static int cros_typec_send_clear_event(struct cros_typec_data *typec, int port_num, u32 events_mask)
883 struct ec_params_typec_control req = {
885 .command = TYPEC_CONTROL_COMMAND_CLEAR_EVENTS,
886 .clear_events_mask = events_mask,
889 return cros_typec_ec_command(typec, 0, EC_CMD_TYPEC_CONTROL, &req,
890 sizeof(req), NULL, 0);
893 static void cros_typec_handle_status(struct cros_typec_data *typec, int port_num)
895 struct ec_response_typec_status resp;
896 struct ec_params_typec_status req = {
901 ret = cros_typec_ec_command(typec, 0, EC_CMD_TYPEC_STATUS, &req, sizeof(req),
902 &resp, sizeof(resp));
904 dev_warn(typec->dev, "EC_CMD_TYPEC_STATUS failed for port: %d\n", port_num);
908 /* Handle any events appropriately. */
909 if (resp.events & PD_STATUS_EVENT_SOP_DISC_DONE && !typec->ports[port_num]->sop_disc_done) {
912 /* Convert BCD to the format preferred by the TypeC framework */
913 sop_revision = (le16_to_cpu(resp.sop_revision) & 0xff00) >> 4;
914 ret = cros_typec_handle_sop_disc(typec, port_num, sop_revision);
916 dev_err(typec->dev, "Couldn't parse SOP Disc data, port: %d\n", port_num);
918 typec->ports[port_num]->sop_disc_done = true;
919 ret = cros_typec_send_clear_event(typec, port_num,
920 PD_STATUS_EVENT_SOP_DISC_DONE);
923 "Failed SOP Disc event clear, port: %d\n", port_num);
925 if (resp.sop_connected)
926 typec_set_pwr_opmode(typec->ports[port_num]->port, TYPEC_PWR_MODE_PD);
929 if (resp.events & PD_STATUS_EVENT_SOP_PRIME_DISC_DONE &&
930 !typec->ports[port_num]->sop_prime_disc_done) {
931 u16 sop_prime_revision;
933 /* Convert BCD to the format preferred by the TypeC framework */
934 sop_prime_revision = (le16_to_cpu(resp.sop_prime_revision) & 0xff00) >> 4;
935 ret = cros_typec_handle_sop_prime_disc(typec, port_num, sop_prime_revision);
937 dev_err(typec->dev, "Couldn't parse SOP' Disc data, port: %d\n", port_num);
939 typec->ports[port_num]->sop_prime_disc_done = true;
940 ret = cros_typec_send_clear_event(typec, port_num,
941 PD_STATUS_EVENT_SOP_PRIME_DISC_DONE);
944 "Failed SOP Disc event clear, port: %d\n", port_num);
949 static int cros_typec_port_update(struct cros_typec_data *typec, int port_num)
951 struct ec_params_usb_pd_control req;
952 struct ec_response_usb_pd_control_v2 resp;
953 struct ec_response_usb_pd_mux_info mux_resp;
956 if (port_num < 0 || port_num >= typec->num_ports) {
957 dev_err(typec->dev, "cannot get status for invalid port %d\n",
963 req.role = USB_PD_CTRL_ROLE_NO_CHANGE;
964 req.mux = USB_PD_CTRL_MUX_NO_CHANGE;
965 req.swap = USB_PD_CTRL_SWAP_NONE;
967 ret = cros_typec_ec_command(typec, typec->pd_ctrl_ver,
968 EC_CMD_USB_PD_CONTROL, &req, sizeof(req),
969 &resp, sizeof(resp));
973 dev_dbg(typec->dev, "Enabled %d: 0x%hhx\n", port_num, resp.enabled);
974 dev_dbg(typec->dev, "Role %d: 0x%hhx\n", port_num, resp.role);
975 dev_dbg(typec->dev, "Polarity %d: 0x%hhx\n", port_num, resp.polarity);
976 dev_dbg(typec->dev, "State %d: %s\n", port_num, resp.state);
978 if (typec->pd_ctrl_ver != 0)
979 cros_typec_set_port_params_v1(typec, port_num,
980 (struct ec_response_usb_pd_control_v1 *)&resp);
982 cros_typec_set_port_params_v0(typec, port_num,
983 (struct ec_response_usb_pd_control *) &resp);
985 if (typec->typec_cmd_supported)
986 cros_typec_handle_status(typec, port_num);
988 /* Update the switches if they exist, according to requested state */
989 ret = cros_typec_get_mux_info(typec, port_num, &mux_resp);
992 "Failed to get mux info for port: %d, err = %d\n",
997 /* No change needs to be made, let's exit early. */
998 if (typec->ports[port_num]->mux_flags == mux_resp.flags)
1001 typec->ports[port_num]->mux_flags = mux_resp.flags;
1002 ret = cros_typec_configure_mux(typec, port_num, mux_resp.flags, &resp);
1004 dev_warn(typec->dev, "Configure muxes failed, err = %d\n", ret);
1009 static int cros_typec_get_cmd_version(struct cros_typec_data *typec)
1011 struct ec_params_get_cmd_versions_v1 req_v1;
1012 struct ec_response_get_cmd_versions resp;
1015 /* We're interested in the PD control command version. */
1016 req_v1.cmd = EC_CMD_USB_PD_CONTROL;
1017 ret = cros_typec_ec_command(typec, 1, EC_CMD_GET_CMD_VERSIONS,
1018 &req_v1, sizeof(req_v1), &resp,
1023 if (resp.version_mask & EC_VER_MASK(2))
1024 typec->pd_ctrl_ver = 2;
1025 else if (resp.version_mask & EC_VER_MASK(1))
1026 typec->pd_ctrl_ver = 1;
1028 typec->pd_ctrl_ver = 0;
1030 dev_dbg(typec->dev, "PD Control has version mask 0x%hhx\n",
1031 typec->pd_ctrl_ver);
1036 /* Check the EC feature flags to see if TYPEC_* features are supported. */
1037 static int cros_typec_feature_supported(struct cros_typec_data *typec, enum ec_feature_code feature)
1039 struct ec_response_get_features resp = {};
1042 ret = cros_typec_ec_command(typec, 0, EC_CMD_GET_FEATURES, NULL, 0,
1043 &resp, sizeof(resp));
1045 dev_warn(typec->dev,
1046 "Failed to get features, assuming typec feature=%d unsupported.\n",
1051 return resp.flags[feature / 32] & EC_FEATURE_MASK_1(feature);
1054 static void cros_typec_port_work(struct work_struct *work)
1056 struct cros_typec_data *typec = container_of(work, struct cros_typec_data, port_work);
1059 for (i = 0; i < typec->num_ports; i++) {
1060 ret = cros_typec_port_update(typec, i);
1062 dev_warn(typec->dev, "Update failed for port: %d\n", i);
1066 static int cros_ec_typec_event(struct notifier_block *nb,
1067 unsigned long host_event, void *_notify)
1069 struct cros_typec_data *typec = container_of(nb, struct cros_typec_data, nb);
1071 flush_work(&typec->port_work);
1072 schedule_work(&typec->port_work);
1078 static const struct acpi_device_id cros_typec_acpi_id[] = {
1082 MODULE_DEVICE_TABLE(acpi, cros_typec_acpi_id);
1086 static const struct of_device_id cros_typec_of_match[] = {
1087 { .compatible = "google,cros-ec-typec", },
1090 MODULE_DEVICE_TABLE(of, cros_typec_of_match);
1093 static int cros_typec_probe(struct platform_device *pdev)
1095 struct device *dev = &pdev->dev;
1096 struct cros_typec_data *typec;
1097 struct ec_response_usb_pd_ports resp;
1100 typec = devm_kzalloc(dev, sizeof(*typec), GFP_KERNEL);
1105 typec->ec = dev_get_drvdata(pdev->dev.parent);
1106 platform_set_drvdata(pdev, typec);
1108 ret = cros_typec_get_cmd_version(typec);
1110 dev_err(dev, "failed to get PD command version info\n");
1114 typec->typec_cmd_supported = !!cros_typec_feature_supported(typec,
1115 EC_FEATURE_TYPEC_CMD);
1116 typec->needs_mux_ack = !!cros_typec_feature_supported(typec,
1117 EC_FEATURE_TYPEC_MUX_REQUIRE_AP_ACK);
1119 ret = cros_typec_ec_command(typec, 0, EC_CMD_USB_PD_PORTS, NULL, 0,
1120 &resp, sizeof(resp));
1124 typec->num_ports = resp.num_ports;
1125 if (typec->num_ports > EC_USB_PD_MAX_PORTS) {
1126 dev_warn(typec->dev,
1127 "Too many ports reported: %d, limiting to max: %d\n",
1128 typec->num_ports, EC_USB_PD_MAX_PORTS);
1129 typec->num_ports = EC_USB_PD_MAX_PORTS;
1132 ret = cros_typec_init_ports(typec);
1136 INIT_WORK(&typec->port_work, cros_typec_port_work);
1139 * Safe to call port update here, since we haven't registered the
1142 for (i = 0; i < typec->num_ports; i++) {
1143 ret = cros_typec_port_update(typec, i);
1145 goto unregister_ports;
1148 typec->nb.notifier_call = cros_ec_typec_event;
1149 ret = cros_usbpd_register_notify(&typec->nb);
1151 goto unregister_ports;
1156 cros_unregister_ports(typec);
1160 static int __maybe_unused cros_typec_suspend(struct device *dev)
1162 struct cros_typec_data *typec = dev_get_drvdata(dev);
1164 cancel_work_sync(&typec->port_work);
1169 static int __maybe_unused cros_typec_resume(struct device *dev)
1171 struct cros_typec_data *typec = dev_get_drvdata(dev);
1173 /* Refresh port state. */
1174 schedule_work(&typec->port_work);
1179 static const struct dev_pm_ops cros_typec_pm_ops = {
1180 SET_SYSTEM_SLEEP_PM_OPS(cros_typec_suspend, cros_typec_resume)
1183 static struct platform_driver cros_typec_driver = {
1186 .acpi_match_table = ACPI_PTR(cros_typec_acpi_id),
1187 .of_match_table = of_match_ptr(cros_typec_of_match),
1188 .pm = &cros_typec_pm_ops,
1190 .probe = cros_typec_probe,
1193 module_platform_driver(cros_typec_driver);
1195 MODULE_AUTHOR("Prashant Malani <pmalani@chromium.org>");
1196 MODULE_DESCRIPTION("Chrome OS EC Type C control");
1197 MODULE_LICENSE("GPL");