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;
64 struct typec_altmode p_altmode[CROS_EC_ALTMODE_MAX];
66 /* Flag indicating that PD partner discovery data parsing is completed. */
68 bool sop_prime_disc_done;
69 struct ec_response_typec_discovery *disc_data;
70 struct list_head partner_mode_list;
71 struct list_head plug_mode_list;
74 /* Platform-specific data for the Chrome OS EC Type C controller. */
75 struct cros_typec_data {
77 struct cros_ec_device *ec;
79 unsigned int pd_ctrl_ver;
80 /* Array of ports, indexed by port number. */
81 struct cros_typec_port *ports[EC_USB_PD_MAX_PORTS];
82 struct notifier_block nb;
83 struct work_struct port_work;
84 bool typec_cmd_supported;
88 static int cros_typec_parse_port_props(struct typec_capability *cap,
89 struct fwnode_handle *fwnode,
95 memset(cap, 0, sizeof(*cap));
96 ret = fwnode_property_read_string(fwnode, "power-role", &buf);
98 dev_err(dev, "power-role not found: %d\n", ret);
102 ret = typec_find_port_power_role(buf);
107 ret = fwnode_property_read_string(fwnode, "data-role", &buf);
109 dev_err(dev, "data-role not found: %d\n", ret);
113 ret = typec_find_port_data_role(buf);
118 /* Try-power-role is optional. */
119 ret = fwnode_property_read_string(fwnode, "try-power-role", &buf);
121 dev_warn(dev, "try-power-role not found: %d\n", ret);
122 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
124 ret = typec_find_power_role(buf);
127 cap->prefer_role = ret;
130 cap->fwnode = fwnode;
135 static int cros_typec_get_switch_handles(struct cros_typec_port *port,
136 struct fwnode_handle *fwnode,
139 port->mux = fwnode_typec_mux_get(fwnode, NULL);
140 if (IS_ERR(port->mux)) {
141 dev_dbg(dev, "Mux handle not found.\n");
145 port->ori_sw = fwnode_typec_switch_get(fwnode);
146 if (IS_ERR(port->ori_sw)) {
147 dev_dbg(dev, "Orientation switch handle not found.\n");
151 port->role_sw = fwnode_usb_role_switch_get(fwnode);
152 if (IS_ERR(port->role_sw)) {
153 dev_dbg(dev, "USB role switch handle not found.\n");
160 usb_role_switch_put(port->role_sw);
162 typec_switch_put(port->ori_sw);
164 typec_mux_put(port->mux);
169 static int cros_typec_add_partner(struct cros_typec_data *typec, int port_num,
172 struct cros_typec_port *port = typec->ports[port_num];
173 struct typec_partner_desc p_desc = {
179 * Fill an initial PD identity, which will then be updated with info
182 p_desc.identity = &port->p_identity;
184 port->partner = typec_register_partner(port->port, &p_desc);
185 if (IS_ERR(port->partner)) {
186 ret = PTR_ERR(port->partner);
187 port->partner = NULL;
193 static void cros_typec_unregister_altmodes(struct cros_typec_data *typec, int port_num,
196 struct cros_typec_port *port = typec->ports[port_num];
197 struct cros_typec_altmode_node *node, *tmp;
198 struct list_head *head;
200 head = is_partner ? &port->partner_mode_list : &port->plug_mode_list;
201 list_for_each_entry_safe(node, tmp, head, list) {
202 list_del(&node->list);
203 typec_unregister_altmode(node->amode);
204 devm_kfree(typec->dev, node);
208 static int cros_typec_usb_disconnect_state(struct cros_typec_port *port)
210 port->state.alt = NULL;
211 port->state.mode = TYPEC_STATE_USB;
212 port->state.data = NULL;
214 usb_role_switch_set_role(port->role_sw, USB_ROLE_NONE);
215 typec_switch_set(port->ori_sw, TYPEC_ORIENTATION_NONE);
217 return typec_mux_set(port->mux, &port->state);
220 static void cros_typec_remove_partner(struct cros_typec_data *typec,
223 struct cros_typec_port *port = typec->ports[port_num];
228 cros_typec_unregister_altmodes(typec, port_num, true);
230 cros_typec_usb_disconnect_state(port);
231 port->mux_flags = USB_PD_MUX_NONE;
233 typec_unregister_partner(port->partner);
234 port->partner = NULL;
235 memset(&port->p_identity, 0, sizeof(port->p_identity));
236 port->sop_disc_done = false;
239 static void cros_typec_remove_cable(struct cros_typec_data *typec,
242 struct cros_typec_port *port = typec->ports[port_num];
247 cros_typec_unregister_altmodes(typec, port_num, false);
249 typec_unregister_plug(port->plug);
251 typec_unregister_cable(port->cable);
253 memset(&port->c_identity, 0, sizeof(port->c_identity));
254 port->sop_prime_disc_done = false;
257 static void cros_unregister_ports(struct cros_typec_data *typec)
261 for (i = 0; i < typec->num_ports; i++) {
262 if (!typec->ports[i])
265 cros_typec_remove_partner(typec, i);
266 cros_typec_remove_cable(typec, i);
268 usb_role_switch_put(typec->ports[i]->role_sw);
269 typec_switch_put(typec->ports[i]->ori_sw);
270 typec_mux_put(typec->ports[i]->mux);
271 typec_unregister_port(typec->ports[i]->port);
276 * Fake the alt mode structs until we actually start registering Type C port
277 * and partner alt modes.
279 static void cros_typec_register_port_altmodes(struct cros_typec_data *typec,
282 struct cros_typec_port *port = typec->ports[port_num];
284 /* All PD capable CrOS devices are assumed to support DP altmode. */
285 port->p_altmode[CROS_EC_ALTMODE_DP].svid = USB_TYPEC_DP_SID;
286 port->p_altmode[CROS_EC_ALTMODE_DP].mode = USB_TYPEC_DP_MODE;
289 * Register TBT compatibility alt mode. The EC will not enter the mode
290 * if it doesn't support it, so it's safe to register it unconditionally
293 port->p_altmode[CROS_EC_ALTMODE_TBT].svid = USB_TYPEC_TBT_SID;
294 port->p_altmode[CROS_EC_ALTMODE_TBT].mode = TYPEC_ANY_MODE;
296 port->state.alt = NULL;
297 port->state.mode = TYPEC_STATE_USB;
298 port->state.data = NULL;
301 static int cros_typec_init_ports(struct cros_typec_data *typec)
303 struct device *dev = typec->dev;
304 struct typec_capability *cap;
305 struct fwnode_handle *fwnode;
306 struct cros_typec_port *cros_port;
307 const char *port_prop;
312 nports = device_get_child_node_count(dev);
314 dev_err(dev, "No port entries found.\n");
318 if (nports > typec->num_ports) {
319 dev_err(dev, "More ports listed than can be supported.\n");
323 /* DT uses "reg" to specify port number. */
324 port_prop = dev->of_node ? "reg" : "port-number";
325 device_for_each_child_node(dev, fwnode) {
326 if (fwnode_property_read_u32(fwnode, port_prop, &port_num)) {
328 dev_err(dev, "No port-number for port, aborting.\n");
329 goto unregister_ports;
332 if (port_num >= typec->num_ports) {
333 dev_err(dev, "Invalid port number.\n");
335 goto unregister_ports;
338 dev_dbg(dev, "Registering port %d\n", port_num);
340 cros_port = devm_kzalloc(dev, sizeof(*cros_port), GFP_KERNEL);
343 goto unregister_ports;
346 typec->ports[port_num] = cros_port;
347 cap = &cros_port->caps;
349 ret = cros_typec_parse_port_props(cap, fwnode, dev);
351 goto unregister_ports;
353 cros_port->port = typec_register_port(dev, cap);
354 if (IS_ERR(cros_port->port)) {
355 dev_err(dev, "Failed to register port %d\n", port_num);
356 ret = PTR_ERR(cros_port->port);
357 goto unregister_ports;
360 ret = cros_typec_get_switch_handles(cros_port, fwnode, dev);
362 dev_dbg(dev, "No switch control for port %d\n",
365 cros_typec_register_port_altmodes(typec, port_num);
367 cros_port->disc_data = devm_kzalloc(dev, EC_PROTO2_MAX_RESPONSE_SIZE, GFP_KERNEL);
368 if (!cros_port->disc_data) {
370 goto unregister_ports;
373 INIT_LIST_HEAD(&cros_port->partner_mode_list);
374 INIT_LIST_HEAD(&cros_port->plug_mode_list);
380 cros_unregister_ports(typec);
384 static int cros_typec_usb_safe_state(struct cros_typec_port *port)
386 port->state.mode = TYPEC_STATE_SAFE;
388 return typec_mux_set(port->mux, &port->state);
392 * Spoof the VDOs that were likely communicated by the partner for TBT alt
395 static int cros_typec_enable_tbt(struct cros_typec_data *typec,
397 struct ec_response_usb_pd_control_v2 *pd_ctrl)
399 struct cros_typec_port *port = typec->ports[port_num];
400 struct typec_thunderbolt_data data;
403 if (typec->pd_ctrl_ver < 2) {
405 "PD_CTRL version too old: %d\n", typec->pd_ctrl_ver);
409 /* Device Discover Mode VDO */
410 data.device_mode = TBT_MODE;
412 if (pd_ctrl->control_flags & USB_PD_CTRL_TBT_LEGACY_ADAPTER)
413 data.device_mode = TBT_SET_ADAPTER(TBT_ADAPTER_TBT3);
415 /* Cable Discover Mode VDO */
416 data.cable_mode = TBT_MODE;
417 data.cable_mode |= TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed);
419 if (pd_ctrl->control_flags & USB_PD_CTRL_OPTICAL_CABLE)
420 data.cable_mode |= TBT_CABLE_OPTICAL;
422 if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_LINK_UNIDIR)
423 data.cable_mode |= TBT_CABLE_LINK_TRAINING;
425 data.cable_mode |= TBT_SET_CABLE_ROUNDED(pd_ctrl->cable_gen);
428 data.enter_vdo = TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed);
430 if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE)
431 data.enter_vdo |= TBT_ENTER_MODE_ACTIVE_CABLE;
433 if (!port->state.alt) {
434 port->state.alt = &port->p_altmode[CROS_EC_ALTMODE_TBT];
435 ret = cros_typec_usb_safe_state(port);
440 port->state.data = &data;
441 port->state.mode = TYPEC_TBT_MODE;
443 return typec_mux_set(port->mux, &port->state);
446 /* Spoof the VDOs that were likely communicated by the partner. */
447 static int cros_typec_enable_dp(struct cros_typec_data *typec,
449 struct ec_response_usb_pd_control_v2 *pd_ctrl)
451 struct cros_typec_port *port = typec->ports[port_num];
452 struct typec_displayport_data dp_data;
455 if (typec->pd_ctrl_ver < 2) {
457 "PD_CTRL version too old: %d\n", typec->pd_ctrl_ver);
461 if (!pd_ctrl->dp_mode) {
462 dev_err(typec->dev, "No valid DP mode provided.\n");
467 dp_data.status = DP_STATUS_ENABLED;
468 if (port->mux_flags & USB_PD_MUX_HPD_IRQ)
469 dp_data.status |= DP_STATUS_IRQ_HPD;
470 if (port->mux_flags & USB_PD_MUX_HPD_LVL)
471 dp_data.status |= DP_STATUS_HPD_STATE;
473 /* Configuration VDO. */
474 dp_data.conf = DP_CONF_SET_PIN_ASSIGN(pd_ctrl->dp_mode);
475 if (!port->state.alt) {
476 port->state.alt = &port->p_altmode[CROS_EC_ALTMODE_DP];
477 ret = cros_typec_usb_safe_state(port);
482 port->state.data = &dp_data;
483 port->state.mode = TYPEC_MODAL_STATE(ffs(pd_ctrl->dp_mode));
485 return typec_mux_set(port->mux, &port->state);
488 static int cros_typec_enable_usb4(struct cros_typec_data *typec,
490 struct ec_response_usb_pd_control_v2 *pd_ctrl)
492 struct cros_typec_port *port = typec->ports[port_num];
493 struct enter_usb_data data;
495 data.eudo = EUDO_USB_MODE_USB4 << EUDO_USB_MODE_SHIFT;
498 data.eudo |= pd_ctrl->cable_speed << EUDO_CABLE_SPEED_SHIFT;
501 if (pd_ctrl->control_flags & USB_PD_CTRL_OPTICAL_CABLE)
502 data.eudo |= EUDO_CABLE_TYPE_OPTICAL << EUDO_CABLE_TYPE_SHIFT;
503 else if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE)
504 data.eudo |= EUDO_CABLE_TYPE_RE_TIMER << EUDO_CABLE_TYPE_SHIFT;
506 data.active_link_training = !!(pd_ctrl->control_flags &
507 USB_PD_CTRL_ACTIVE_LINK_UNIDIR);
509 port->state.alt = NULL;
510 port->state.data = &data;
511 port->state.mode = TYPEC_MODE_USB4;
513 return typec_mux_set(port->mux, &port->state);
516 static int cros_typec_configure_mux(struct cros_typec_data *typec, int port_num,
517 struct ec_response_usb_pd_control_v2 *pd_ctrl)
519 struct cros_typec_port *port = typec->ports[port_num];
520 struct ec_response_usb_pd_mux_info resp;
521 struct ec_params_usb_pd_mux_info req = {
524 struct ec_params_usb_pd_mux_ack mux_ack;
525 enum typec_orientation orientation;
528 ret = cros_ec_command(typec->ec, 0, EC_CMD_USB_PD_MUX_INFO,
529 &req, sizeof(req), &resp, sizeof(resp));
531 dev_warn(typec->dev, "Failed to get mux info for port: %d, err = %d\n",
536 /* No change needs to be made, let's exit early. */
537 if (port->mux_flags == resp.flags && port->role == pd_ctrl->role)
540 port->mux_flags = resp.flags;
541 port->role = pd_ctrl->role;
543 if (port->mux_flags == USB_PD_MUX_NONE) {
544 ret = cros_typec_usb_disconnect_state(port);
548 if (port->mux_flags & USB_PD_MUX_POLARITY_INVERTED)
549 orientation = TYPEC_ORIENTATION_REVERSE;
551 orientation = TYPEC_ORIENTATION_NORMAL;
553 ret = typec_switch_set(port->ori_sw, orientation);
557 ret = usb_role_switch_set_role(typec->ports[port_num]->role_sw,
558 pd_ctrl->role & PD_CTRL_RESP_ROLE_DATA
559 ? USB_ROLE_HOST : USB_ROLE_DEVICE);
563 if (port->mux_flags & USB_PD_MUX_USB4_ENABLED) {
564 ret = cros_typec_enable_usb4(typec, port_num, pd_ctrl);
565 } else if (port->mux_flags & USB_PD_MUX_TBT_COMPAT_ENABLED) {
566 ret = cros_typec_enable_tbt(typec, port_num, pd_ctrl);
567 } else if (port->mux_flags & USB_PD_MUX_DP_ENABLED) {
568 ret = cros_typec_enable_dp(typec, port_num, pd_ctrl);
569 } else if (port->mux_flags & USB_PD_MUX_SAFE_MODE) {
570 ret = cros_typec_usb_safe_state(port);
571 } else if (port->mux_flags & USB_PD_MUX_USB_ENABLED) {
572 port->state.alt = NULL;
573 port->state.mode = TYPEC_STATE_USB;
574 ret = typec_mux_set(port->mux, &port->state);
577 "Unrecognized mode requested, mux flags: %x\n",
582 if (!typec->needs_mux_ack)
585 /* Sending Acknowledgment to EC */
586 mux_ack.port = port_num;
588 if (cros_ec_command(typec->ec, 0, EC_CMD_USB_PD_MUX_ACK, &mux_ack,
589 sizeof(mux_ack), NULL, 0) < 0)
591 "Failed to send Mux ACK to EC for port: %d\n",
597 static void cros_typec_set_port_params_v0(struct cros_typec_data *typec,
598 int port_num, struct ec_response_usb_pd_control *resp)
600 struct typec_port *port = typec->ports[port_num]->port;
601 enum typec_orientation polarity;
604 polarity = TYPEC_ORIENTATION_NONE;
605 else if (!resp->polarity)
606 polarity = TYPEC_ORIENTATION_NORMAL;
608 polarity = TYPEC_ORIENTATION_REVERSE;
610 typec_set_pwr_role(port, resp->role ? TYPEC_SOURCE : TYPEC_SINK);
611 typec_set_orientation(port, polarity);
614 static void cros_typec_set_port_params_v1(struct cros_typec_data *typec,
615 int port_num, struct ec_response_usb_pd_control_v1 *resp)
617 struct typec_port *port = typec->ports[port_num]->port;
618 enum typec_orientation polarity;
622 if (!(resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED))
623 polarity = TYPEC_ORIENTATION_NONE;
624 else if (!resp->polarity)
625 polarity = TYPEC_ORIENTATION_NORMAL;
627 polarity = TYPEC_ORIENTATION_REVERSE;
628 typec_set_orientation(port, polarity);
629 typec_set_data_role(port, resp->role & PD_CTRL_RESP_ROLE_DATA ?
630 TYPEC_HOST : TYPEC_DEVICE);
631 typec_set_pwr_role(port, resp->role & PD_CTRL_RESP_ROLE_POWER ?
632 TYPEC_SOURCE : TYPEC_SINK);
633 typec_set_vconn_role(port, resp->role & PD_CTRL_RESP_ROLE_VCONN ?
634 TYPEC_SOURCE : TYPEC_SINK);
636 /* Register/remove partners when a connect/disconnect occurs. */
637 if (resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED) {
638 if (typec->ports[port_num]->partner)
641 pd_en = resp->enabled & PD_CTRL_RESP_ENABLED_PD_CAPABLE;
642 ret = cros_typec_add_partner(typec, port_num, pd_en);
645 "Failed to register partner on port: %d\n",
648 cros_typec_remove_partner(typec, port_num);
649 cros_typec_remove_cable(typec, port_num);
654 * Helper function to register partner/plug altmodes.
656 static int cros_typec_register_altmodes(struct cros_typec_data *typec, int port_num,
659 struct cros_typec_port *port = typec->ports[port_num];
660 struct ec_response_typec_discovery *sop_disc = port->disc_data;
661 struct cros_typec_altmode_node *node;
662 struct typec_altmode_desc desc;
663 struct typec_altmode *amode;
664 int num_altmodes = 0;
668 for (i = 0; i < sop_disc->svid_count; i++) {
669 for (j = 0; j < sop_disc->svids[i].mode_count; j++) {
670 memset(&desc, 0, sizeof(desc));
671 desc.svid = sop_disc->svids[i].svid;
673 desc.vdo = sop_disc->svids[i].mode_vdo[j];
676 amode = typec_partner_register_altmode(port->partner, &desc);
678 amode = typec_plug_register_altmode(port->plug, &desc);
681 ret = PTR_ERR(amode);
685 /* If no memory is available we should unregister and exit. */
686 node = devm_kzalloc(typec->dev, sizeof(*node), GFP_KERNEL);
689 typec_unregister_altmode(amode);
696 list_add_tail(&node->list, &port->partner_mode_list);
698 list_add_tail(&node->list, &port->plug_mode_list);
704 ret = typec_partner_set_num_altmodes(port->partner, num_altmodes);
706 ret = typec_plug_set_num_altmodes(port->plug, num_altmodes);
709 dev_err(typec->dev, "Unable to set %s num_altmodes for port: %d\n",
710 is_partner ? "partner" : "plug", port_num);
717 cros_typec_unregister_altmodes(typec, port_num, is_partner);
722 * Parse the PD identity data from the EC PD discovery responses and copy that to the supplied
723 * PD identity struct.
725 static void cros_typec_parse_pd_identity(struct usb_pd_identity *id,
726 struct ec_response_typec_discovery *disc)
730 /* First, update the PD identity VDOs for the partner. */
731 if (disc->identity_count > 0)
732 id->id_header = disc->discovery_vdo[0];
733 if (disc->identity_count > 1)
734 id->cert_stat = disc->discovery_vdo[1];
735 if (disc->identity_count > 2)
736 id->product = disc->discovery_vdo[2];
738 /* Copy the remaining identity VDOs till a maximum of 6. */
739 for (i = 3; i < disc->identity_count && i < VDO_MAX_OBJECTS; i++)
740 id->vdo[i - 3] = disc->discovery_vdo[i];
743 static int cros_typec_handle_sop_prime_disc(struct cros_typec_data *typec, int port_num, u16 pd_revision)
745 struct cros_typec_port *port = typec->ports[port_num];
746 struct ec_response_typec_discovery *disc = port->disc_data;
747 struct typec_cable_desc c_desc = {};
748 struct typec_plug_desc p_desc;
749 struct ec_params_typec_discovery req = {
751 .partner_type = TYPEC_PARTNER_SOP_PRIME,
756 memset(disc, 0, EC_PROTO2_MAX_RESPONSE_SIZE);
757 ret = cros_ec_command(typec->ec, 0, EC_CMD_TYPEC_DISCOVERY, &req, sizeof(req),
758 disc, EC_PROTO2_MAX_RESPONSE_SIZE);
760 dev_err(typec->dev, "Failed to get SOP' discovery data for port: %d\n", port_num);
761 goto sop_prime_disc_exit;
764 /* Parse the PD identity data, even if only 0s were returned. */
765 cros_typec_parse_pd_identity(&port->c_identity, disc);
767 if (disc->identity_count != 0) {
768 cable_plug_type = VDO_TYPEC_CABLE_TYPE(port->c_identity.vdo[0]);
769 switch (cable_plug_type) {
771 c_desc.type = USB_PLUG_TYPE_A;
774 c_desc.type = USB_PLUG_TYPE_B;
777 c_desc.type = USB_PLUG_TYPE_C;
780 c_desc.type = USB_PLUG_CAPTIVE;
783 c_desc.type = USB_PLUG_NONE;
785 c_desc.active = PD_IDH_PTYPE(port->c_identity.id_header) == IDH_PTYPE_ACABLE;
788 c_desc.identity = &port->c_identity;
789 c_desc.pd_revision = pd_revision;
791 port->cable = typec_register_cable(port->port, &c_desc);
792 if (IS_ERR(port->cable)) {
793 ret = PTR_ERR(port->cable);
795 goto sop_prime_disc_exit;
798 p_desc.index = TYPEC_PLUG_SOP_P;
799 port->plug = typec_register_plug(port->cable, &p_desc);
800 if (IS_ERR(port->plug)) {
801 ret = PTR_ERR(port->plug);
803 goto sop_prime_disc_exit;
806 ret = cros_typec_register_altmodes(typec, port_num, false);
808 dev_err(typec->dev, "Failed to register plug altmodes, port: %d\n", port_num);
809 goto sop_prime_disc_exit;
815 cros_typec_remove_cable(typec, port_num);
819 static int cros_typec_handle_sop_disc(struct cros_typec_data *typec, int port_num, u16 pd_revision)
821 struct cros_typec_port *port = typec->ports[port_num];
822 struct ec_response_typec_discovery *sop_disc = port->disc_data;
823 struct ec_params_typec_discovery req = {
825 .partner_type = TYPEC_PARTNER_SOP,
829 if (!port->partner) {
831 "SOP Discovery received without partner registered, port: %d\n",
837 typec_partner_set_pd_revision(port->partner, pd_revision);
839 memset(sop_disc, 0, EC_PROTO2_MAX_RESPONSE_SIZE);
840 ret = cros_ec_command(typec->ec, 0, EC_CMD_TYPEC_DISCOVERY, &req, sizeof(req),
841 sop_disc, EC_PROTO2_MAX_RESPONSE_SIZE);
843 dev_err(typec->dev, "Failed to get SOP discovery data for port: %d\n", port_num);
847 cros_typec_parse_pd_identity(&port->p_identity, sop_disc);
849 ret = typec_partner_set_identity(port->partner);
851 dev_err(typec->dev, "Failed to update partner PD identity, port: %d\n", port_num);
855 ret = cros_typec_register_altmodes(typec, port_num, true);
857 dev_err(typec->dev, "Failed to register partner altmodes, port: %d\n", port_num);
865 static int cros_typec_send_clear_event(struct cros_typec_data *typec, int port_num, u32 events_mask)
867 struct ec_params_typec_control req = {
869 .command = TYPEC_CONTROL_COMMAND_CLEAR_EVENTS,
870 .clear_events_mask = events_mask,
873 return cros_ec_command(typec->ec, 0, EC_CMD_TYPEC_CONTROL, &req,
874 sizeof(req), NULL, 0);
877 static void cros_typec_handle_status(struct cros_typec_data *typec, int port_num)
879 struct ec_response_typec_status resp;
880 struct ec_params_typec_status req = {
885 ret = cros_ec_command(typec->ec, 0, EC_CMD_TYPEC_STATUS, &req, sizeof(req),
886 &resp, sizeof(resp));
888 dev_warn(typec->dev, "EC_CMD_TYPEC_STATUS failed for port: %d\n", port_num);
892 /* If we got a hard reset, unregister everything and return. */
893 if (resp.events & PD_STATUS_EVENT_HARD_RESET) {
894 cros_typec_remove_partner(typec, port_num);
895 cros_typec_remove_cable(typec, port_num);
897 ret = cros_typec_send_clear_event(typec, port_num,
898 PD_STATUS_EVENT_HARD_RESET);
901 "Failed hard reset event clear, port: %d\n", port_num);
905 /* Handle any events appropriately. */
906 if (resp.events & PD_STATUS_EVENT_SOP_DISC_DONE && !typec->ports[port_num]->sop_disc_done) {
909 /* Convert BCD to the format preferred by the TypeC framework */
910 sop_revision = (le16_to_cpu(resp.sop_revision) & 0xff00) >> 4;
911 ret = cros_typec_handle_sop_disc(typec, port_num, sop_revision);
913 dev_err(typec->dev, "Couldn't parse SOP Disc data, port: %d\n", port_num);
915 typec->ports[port_num]->sop_disc_done = true;
916 ret = cros_typec_send_clear_event(typec, port_num,
917 PD_STATUS_EVENT_SOP_DISC_DONE);
920 "Failed SOP Disc event clear, port: %d\n", port_num);
922 if (resp.sop_connected)
923 typec_set_pwr_opmode(typec->ports[port_num]->port, TYPEC_PWR_MODE_PD);
926 if (resp.events & PD_STATUS_EVENT_SOP_PRIME_DISC_DONE &&
927 !typec->ports[port_num]->sop_prime_disc_done) {
928 u16 sop_prime_revision;
930 /* Convert BCD to the format preferred by the TypeC framework */
931 sop_prime_revision = (le16_to_cpu(resp.sop_prime_revision) & 0xff00) >> 4;
932 ret = cros_typec_handle_sop_prime_disc(typec, port_num, sop_prime_revision);
934 dev_err(typec->dev, "Couldn't parse SOP' Disc data, port: %d\n", port_num);
936 typec->ports[port_num]->sop_prime_disc_done = true;
937 ret = cros_typec_send_clear_event(typec, port_num,
938 PD_STATUS_EVENT_SOP_PRIME_DISC_DONE);
941 "Failed SOP Disc event clear, port: %d\n", port_num);
946 static int cros_typec_port_update(struct cros_typec_data *typec, int port_num)
948 struct ec_params_usb_pd_control req;
949 struct ec_response_usb_pd_control_v2 resp;
952 if (port_num < 0 || port_num >= typec->num_ports) {
953 dev_err(typec->dev, "cannot get status for invalid port %d\n",
959 req.role = USB_PD_CTRL_ROLE_NO_CHANGE;
960 req.mux = USB_PD_CTRL_MUX_NO_CHANGE;
961 req.swap = USB_PD_CTRL_SWAP_NONE;
963 ret = cros_ec_command(typec->ec, typec->pd_ctrl_ver,
964 EC_CMD_USB_PD_CONTROL, &req, sizeof(req),
965 &resp, sizeof(resp));
969 /* Update the switches if they exist, according to requested state */
970 ret = cros_typec_configure_mux(typec, port_num, &resp);
972 dev_warn(typec->dev, "Configure muxes failed, err = %d\n", ret);
974 dev_dbg(typec->dev, "Enabled %d: 0x%hhx\n", port_num, resp.enabled);
975 dev_dbg(typec->dev, "Role %d: 0x%hhx\n", port_num, resp.role);
976 dev_dbg(typec->dev, "Polarity %d: 0x%hhx\n", port_num, resp.polarity);
977 dev_dbg(typec->dev, "State %d: %s\n", port_num, resp.state);
979 if (typec->pd_ctrl_ver != 0)
980 cros_typec_set_port_params_v1(typec, port_num,
981 (struct ec_response_usb_pd_control_v1 *)&resp);
983 cros_typec_set_port_params_v0(typec, port_num,
984 (struct ec_response_usb_pd_control *) &resp);
986 if (typec->typec_cmd_supported)
987 cros_typec_handle_status(typec, port_num);
992 static int cros_typec_get_cmd_version(struct cros_typec_data *typec)
994 struct ec_params_get_cmd_versions_v1 req_v1;
995 struct ec_response_get_cmd_versions resp;
998 /* We're interested in the PD control command version. */
999 req_v1.cmd = EC_CMD_USB_PD_CONTROL;
1000 ret = cros_ec_command(typec->ec, 1, EC_CMD_GET_CMD_VERSIONS,
1001 &req_v1, sizeof(req_v1), &resp,
1006 if (resp.version_mask & EC_VER_MASK(2))
1007 typec->pd_ctrl_ver = 2;
1008 else if (resp.version_mask & EC_VER_MASK(1))
1009 typec->pd_ctrl_ver = 1;
1011 typec->pd_ctrl_ver = 0;
1013 dev_dbg(typec->dev, "PD Control has version mask 0x%02x\n",
1014 typec->pd_ctrl_ver & 0xff);
1019 static void cros_typec_port_work(struct work_struct *work)
1021 struct cros_typec_data *typec = container_of(work, struct cros_typec_data, port_work);
1024 for (i = 0; i < typec->num_ports; i++) {
1025 ret = cros_typec_port_update(typec, i);
1027 dev_warn(typec->dev, "Update failed for port: %d\n", i);
1031 static int cros_ec_typec_event(struct notifier_block *nb,
1032 unsigned long host_event, void *_notify)
1034 struct cros_typec_data *typec = container_of(nb, struct cros_typec_data, nb);
1036 flush_work(&typec->port_work);
1037 schedule_work(&typec->port_work);
1043 static const struct acpi_device_id cros_typec_acpi_id[] = {
1047 MODULE_DEVICE_TABLE(acpi, cros_typec_acpi_id);
1051 static const struct of_device_id cros_typec_of_match[] = {
1052 { .compatible = "google,cros-ec-typec", },
1055 MODULE_DEVICE_TABLE(of, cros_typec_of_match);
1058 static int cros_typec_probe(struct platform_device *pdev)
1060 struct cros_ec_dev *ec_dev = NULL;
1061 struct device *dev = &pdev->dev;
1062 struct cros_typec_data *typec;
1063 struct ec_response_usb_pd_ports resp;
1066 typec = devm_kzalloc(dev, sizeof(*typec), GFP_KERNEL);
1072 typec->ec = dev_get_drvdata(pdev->dev.parent);
1074 dev_err(dev, "couldn't find parent EC device\n");
1078 platform_set_drvdata(pdev, typec);
1080 ret = cros_typec_get_cmd_version(typec);
1082 dev_err(dev, "failed to get PD command version info\n");
1086 ec_dev = dev_get_drvdata(&typec->ec->ec->dev);
1088 return -EPROBE_DEFER;
1090 typec->typec_cmd_supported = cros_ec_check_features(ec_dev, EC_FEATURE_TYPEC_CMD);
1091 typec->needs_mux_ack = cros_ec_check_features(ec_dev, EC_FEATURE_TYPEC_MUX_REQUIRE_AP_ACK);
1093 ret = cros_ec_command(typec->ec, 0, EC_CMD_USB_PD_PORTS, NULL, 0,
1094 &resp, sizeof(resp));
1098 typec->num_ports = resp.num_ports;
1099 if (typec->num_ports > EC_USB_PD_MAX_PORTS) {
1100 dev_warn(typec->dev,
1101 "Too many ports reported: %d, limiting to max: %d\n",
1102 typec->num_ports, EC_USB_PD_MAX_PORTS);
1103 typec->num_ports = EC_USB_PD_MAX_PORTS;
1106 ret = cros_typec_init_ports(typec);
1110 INIT_WORK(&typec->port_work, cros_typec_port_work);
1113 * Safe to call port update here, since we haven't registered the
1116 for (i = 0; i < typec->num_ports; i++) {
1117 ret = cros_typec_port_update(typec, i);
1119 goto unregister_ports;
1122 typec->nb.notifier_call = cros_ec_typec_event;
1123 ret = cros_usbpd_register_notify(&typec->nb);
1125 goto unregister_ports;
1130 cros_unregister_ports(typec);
1134 static int __maybe_unused cros_typec_suspend(struct device *dev)
1136 struct cros_typec_data *typec = dev_get_drvdata(dev);
1138 cancel_work_sync(&typec->port_work);
1143 static int __maybe_unused cros_typec_resume(struct device *dev)
1145 struct cros_typec_data *typec = dev_get_drvdata(dev);
1147 /* Refresh port state. */
1148 schedule_work(&typec->port_work);
1153 static const struct dev_pm_ops cros_typec_pm_ops = {
1154 SET_SYSTEM_SLEEP_PM_OPS(cros_typec_suspend, cros_typec_resume)
1157 static struct platform_driver cros_typec_driver = {
1160 .acpi_match_table = ACPI_PTR(cros_typec_acpi_id),
1161 .of_match_table = of_match_ptr(cros_typec_of_match),
1162 .pm = &cros_typec_pm_ops,
1164 .probe = cros_typec_probe,
1167 module_platform_driver(cros_typec_driver);
1169 MODULE_AUTHOR("Prashant Malani <pmalani@chromium.org>");
1170 MODULE_DESCRIPTION("Chrome OS EC Type C control");
1171 MODULE_LICENSE("GPL");