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_retimer.h>
24 #include <linux/usb/typec_tbt.h>
25 #include <linux/usb/role.h>
27 #define DRV_NAME "cros-ec-typec"
29 #define DP_PORT_VDO (DP_CONF_SET_PIN_ASSIGN(BIT(DP_PIN_ASSIGN_C) | BIT(DP_PIN_ASSIGN_D)) | \
32 /* Supported alt modes. */
34 CROS_EC_ALTMODE_DP = 0,
39 /* Container for altmode pointer nodes. */
40 struct cros_typec_altmode_node {
41 struct typec_altmode *amode;
42 struct list_head list;
46 struct cros_typec_port {
47 struct typec_port *port;
48 /* Initial capabilities for the port. */
49 struct typec_capability caps;
50 struct typec_partner *partner;
51 struct typec_cable *cable;
53 struct typec_plug *plug;
54 /* Port partner PD identity info. */
55 struct usb_pd_identity p_identity;
56 /* Port cable PD identity info. */
57 struct usb_pd_identity c_identity;
58 struct typec_switch *ori_sw;
59 struct typec_mux *mux;
60 struct typec_retimer *retimer;
61 struct usb_role_switch *role_sw;
63 /* Variables keeping track of switch state. */
64 struct typec_mux_state state;
68 struct typec_altmode *port_altmode[CROS_EC_ALTMODE_MAX];
70 /* Flag indicating that PD partner discovery data parsing is completed. */
72 bool sop_prime_disc_done;
73 struct ec_response_typec_discovery *disc_data;
74 struct list_head partner_mode_list;
75 struct list_head plug_mode_list;
77 /* PDO-related structs */
78 struct usb_power_delivery *partner_pd;
79 struct usb_power_delivery_capabilities *partner_src_caps;
80 struct usb_power_delivery_capabilities *partner_sink_caps;
83 /* Platform-specific data for the Chrome OS EC Type C controller. */
84 struct cros_typec_data {
86 struct cros_ec_device *ec;
88 unsigned int pd_ctrl_ver;
89 /* Array of ports, indexed by port number. */
90 struct cros_typec_port *ports[EC_USB_PD_MAX_PORTS];
91 struct notifier_block nb;
92 struct work_struct port_work;
93 bool typec_cmd_supported;
97 static int cros_typec_parse_port_props(struct typec_capability *cap,
98 struct fwnode_handle *fwnode,
104 memset(cap, 0, sizeof(*cap));
105 ret = fwnode_property_read_string(fwnode, "power-role", &buf);
107 dev_err(dev, "power-role not found: %d\n", ret);
111 ret = typec_find_port_power_role(buf);
116 ret = fwnode_property_read_string(fwnode, "data-role", &buf);
118 dev_err(dev, "data-role not found: %d\n", ret);
122 ret = typec_find_port_data_role(buf);
127 /* Try-power-role is optional. */
128 ret = fwnode_property_read_string(fwnode, "try-power-role", &buf);
130 dev_warn(dev, "try-power-role not found: %d\n", ret);
131 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
133 ret = typec_find_power_role(buf);
136 cap->prefer_role = ret;
139 cap->fwnode = fwnode;
144 static int cros_typec_get_switch_handles(struct cros_typec_port *port,
145 struct fwnode_handle *fwnode,
148 port->mux = fwnode_typec_mux_get(fwnode, NULL);
149 if (IS_ERR(port->mux)) {
150 dev_dbg(dev, "Mux handle not found.\n");
154 port->retimer = fwnode_typec_retimer_get(fwnode);
155 if (IS_ERR(port->retimer)) {
156 dev_dbg(dev, "Retimer handle not found.\n");
160 port->ori_sw = fwnode_typec_switch_get(fwnode);
161 if (IS_ERR(port->ori_sw)) {
162 dev_dbg(dev, "Orientation switch handle not found.\n");
166 port->role_sw = fwnode_usb_role_switch_get(fwnode);
167 if (IS_ERR(port->role_sw)) {
168 dev_dbg(dev, "USB role switch handle not found.\n");
175 typec_switch_put(port->ori_sw);
178 typec_retimer_put(port->retimer);
179 port->retimer = NULL;
181 typec_mux_put(port->mux);
187 static int cros_typec_add_partner(struct cros_typec_data *typec, int port_num,
190 struct cros_typec_port *port = typec->ports[port_num];
191 struct typec_partner_desc p_desc = {
197 * Fill an initial PD identity, which will then be updated with info
200 p_desc.identity = &port->p_identity;
202 port->partner = typec_register_partner(port->port, &p_desc);
203 if (IS_ERR(port->partner)) {
204 ret = PTR_ERR(port->partner);
205 port->partner = NULL;
211 static void cros_typec_unregister_altmodes(struct cros_typec_data *typec, int port_num,
214 struct cros_typec_port *port = typec->ports[port_num];
215 struct cros_typec_altmode_node *node, *tmp;
216 struct list_head *head;
218 head = is_partner ? &port->partner_mode_list : &port->plug_mode_list;
219 list_for_each_entry_safe(node, tmp, head, list) {
220 list_del(&node->list);
221 typec_unregister_altmode(node->amode);
222 devm_kfree(typec->dev, node);
227 * Map the Type-C Mux state to retimer state and call the retimer set function. We need this
228 * because we re-use the Type-C mux state for retimers.
230 static int cros_typec_retimer_set(struct typec_retimer *retimer, struct typec_mux_state state)
232 struct typec_retimer_state rstate = {
238 return typec_retimer_set(retimer, &rstate);
241 static int cros_typec_usb_disconnect_state(struct cros_typec_port *port)
243 port->state.alt = NULL;
244 port->state.mode = TYPEC_STATE_USB;
245 port->state.data = NULL;
247 usb_role_switch_set_role(port->role_sw, USB_ROLE_NONE);
248 typec_switch_set(port->ori_sw, TYPEC_ORIENTATION_NONE);
249 cros_typec_retimer_set(port->retimer, port->state);
251 return typec_mux_set(port->mux, &port->state);
254 static void cros_typec_remove_partner(struct cros_typec_data *typec,
257 struct cros_typec_port *port = typec->ports[port_num];
262 cros_typec_unregister_altmodes(typec, port_num, true);
264 typec_partner_set_usb_power_delivery(port->partner, NULL);
265 usb_power_delivery_unregister_capabilities(port->partner_sink_caps);
266 port->partner_sink_caps = NULL;
267 usb_power_delivery_unregister_capabilities(port->partner_src_caps);
268 port->partner_src_caps = NULL;
269 usb_power_delivery_unregister(port->partner_pd);
270 port->partner_pd = NULL;
272 cros_typec_usb_disconnect_state(port);
273 port->mux_flags = USB_PD_MUX_NONE;
275 typec_unregister_partner(port->partner);
276 port->partner = NULL;
277 memset(&port->p_identity, 0, sizeof(port->p_identity));
278 port->sop_disc_done = false;
281 static void cros_typec_remove_cable(struct cros_typec_data *typec,
284 struct cros_typec_port *port = typec->ports[port_num];
289 cros_typec_unregister_altmodes(typec, port_num, false);
291 typec_unregister_plug(port->plug);
293 typec_unregister_cable(port->cable);
295 memset(&port->c_identity, 0, sizeof(port->c_identity));
296 port->sop_prime_disc_done = false;
299 static void cros_typec_unregister_port_altmodes(struct cros_typec_port *port)
303 for (i = 0; i < CROS_EC_ALTMODE_MAX; i++)
304 typec_unregister_altmode(port->port_altmode[i]);
307 static void cros_unregister_ports(struct cros_typec_data *typec)
311 for (i = 0; i < typec->num_ports; i++) {
312 if (!typec->ports[i])
315 cros_typec_remove_partner(typec, i);
316 cros_typec_remove_cable(typec, i);
318 usb_role_switch_put(typec->ports[i]->role_sw);
319 typec_switch_put(typec->ports[i]->ori_sw);
320 typec_mux_put(typec->ports[i]->mux);
321 cros_typec_unregister_port_altmodes(typec->ports[i]);
322 typec_unregister_port(typec->ports[i]->port);
327 * Register port alt modes with known values till we start retrieving
328 * port capabilities from the EC.
330 static int cros_typec_register_port_altmodes(struct cros_typec_data *typec,
333 struct cros_typec_port *port = typec->ports[port_num];
334 struct typec_altmode_desc desc;
335 struct typec_altmode *amode;
337 /* All PD capable CrOS devices are assumed to support DP altmode. */
338 desc.svid = USB_TYPEC_DP_SID,
339 desc.mode = USB_TYPEC_DP_MODE,
340 desc.vdo = DP_PORT_VDO,
341 amode = typec_port_register_altmode(port->port, &desc);
343 return PTR_ERR(amode);
344 port->port_altmode[CROS_EC_ALTMODE_DP] = amode;
347 * Register TBT compatibility alt mode. The EC will not enter the mode
348 * if it doesn't support it, so it's safe to register it unconditionally
351 memset(&desc, 0, sizeof(desc));
352 desc.svid = USB_TYPEC_TBT_SID,
353 desc.mode = TYPEC_ANY_MODE,
354 amode = typec_port_register_altmode(port->port, &desc);
356 return PTR_ERR(amode);
357 port->port_altmode[CROS_EC_ALTMODE_TBT] = amode;
359 port->state.alt = NULL;
360 port->state.mode = TYPEC_STATE_USB;
361 port->state.data = NULL;
366 static int cros_typec_init_ports(struct cros_typec_data *typec)
368 struct device *dev = typec->dev;
369 struct typec_capability *cap;
370 struct fwnode_handle *fwnode;
371 struct cros_typec_port *cros_port;
372 const char *port_prop;
377 nports = device_get_child_node_count(dev);
379 dev_err(dev, "No port entries found.\n");
383 if (nports > typec->num_ports) {
384 dev_err(dev, "More ports listed than can be supported.\n");
388 /* DT uses "reg" to specify port number. */
389 port_prop = dev->of_node ? "reg" : "port-number";
390 device_for_each_child_node(dev, fwnode) {
391 if (fwnode_property_read_u32(fwnode, port_prop, &port_num)) {
393 dev_err(dev, "No port-number for port, aborting.\n");
394 goto unregister_ports;
397 if (port_num >= typec->num_ports) {
398 dev_err(dev, "Invalid port number.\n");
400 goto unregister_ports;
403 dev_dbg(dev, "Registering port %d\n", port_num);
405 cros_port = devm_kzalloc(dev, sizeof(*cros_port), GFP_KERNEL);
408 goto unregister_ports;
411 typec->ports[port_num] = cros_port;
412 cap = &cros_port->caps;
414 ret = cros_typec_parse_port_props(cap, fwnode, dev);
416 goto unregister_ports;
418 cros_port->port = typec_register_port(dev, cap);
419 if (IS_ERR(cros_port->port)) {
420 ret = PTR_ERR(cros_port->port);
421 dev_err_probe(dev, ret, "Failed to register port %d\n", port_num);
422 goto unregister_ports;
425 ret = cros_typec_get_switch_handles(cros_port, fwnode, dev);
427 dev_dbg(dev, "No switch control for port %d\n",
430 ret = cros_typec_register_port_altmodes(typec, port_num);
432 dev_err(dev, "Failed to register port altmodes\n");
433 goto unregister_ports;
436 cros_port->disc_data = devm_kzalloc(dev, EC_PROTO2_MAX_RESPONSE_SIZE, GFP_KERNEL);
437 if (!cros_port->disc_data) {
439 goto unregister_ports;
442 INIT_LIST_HEAD(&cros_port->partner_mode_list);
443 INIT_LIST_HEAD(&cros_port->plug_mode_list);
449 cros_unregister_ports(typec);
453 static int cros_typec_usb_safe_state(struct cros_typec_port *port)
456 port->state.mode = TYPEC_STATE_SAFE;
458 ret = cros_typec_retimer_set(port->retimer, port->state);
460 ret = typec_mux_set(port->mux, &port->state);
466 * Spoof the VDOs that were likely communicated by the partner for TBT alt
469 static int cros_typec_enable_tbt(struct cros_typec_data *typec,
471 struct ec_response_usb_pd_control_v2 *pd_ctrl)
473 struct cros_typec_port *port = typec->ports[port_num];
474 struct typec_thunderbolt_data data;
477 if (typec->pd_ctrl_ver < 2) {
479 "PD_CTRL version too old: %d\n", typec->pd_ctrl_ver);
483 /* Device Discover Mode VDO */
484 data.device_mode = TBT_MODE;
486 if (pd_ctrl->control_flags & USB_PD_CTRL_TBT_LEGACY_ADAPTER)
487 data.device_mode = TBT_SET_ADAPTER(TBT_ADAPTER_TBT3);
489 /* Cable Discover Mode VDO */
490 data.cable_mode = TBT_MODE;
491 data.cable_mode |= TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed);
493 if (pd_ctrl->control_flags & USB_PD_CTRL_OPTICAL_CABLE)
494 data.cable_mode |= TBT_CABLE_OPTICAL;
496 if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_LINK_UNIDIR)
497 data.cable_mode |= TBT_CABLE_LINK_TRAINING;
499 data.cable_mode |= TBT_SET_CABLE_ROUNDED(pd_ctrl->cable_gen);
502 data.enter_vdo = TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed);
504 if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE)
505 data.enter_vdo |= TBT_ENTER_MODE_ACTIVE_CABLE;
507 if (!port->state.alt) {
508 port->state.alt = port->port_altmode[CROS_EC_ALTMODE_TBT];
509 ret = cros_typec_usb_safe_state(port);
514 port->state.data = &data;
515 port->state.mode = TYPEC_TBT_MODE;
517 return typec_mux_set(port->mux, &port->state);
520 /* Spoof the VDOs that were likely communicated by the partner. */
521 static int cros_typec_enable_dp(struct cros_typec_data *typec,
523 struct ec_response_usb_pd_control_v2 *pd_ctrl)
525 struct cros_typec_port *port = typec->ports[port_num];
526 struct typec_displayport_data dp_data;
529 if (typec->pd_ctrl_ver < 2) {
531 "PD_CTRL version too old: %d\n", typec->pd_ctrl_ver);
535 if (!pd_ctrl->dp_mode) {
536 dev_err(typec->dev, "No valid DP mode provided.\n");
541 dp_data.status = DP_STATUS_ENABLED;
542 if (port->mux_flags & USB_PD_MUX_HPD_IRQ)
543 dp_data.status |= DP_STATUS_IRQ_HPD;
544 if (port->mux_flags & USB_PD_MUX_HPD_LVL)
545 dp_data.status |= DP_STATUS_HPD_STATE;
547 /* Configuration VDO. */
548 dp_data.conf = DP_CONF_SET_PIN_ASSIGN(pd_ctrl->dp_mode);
549 if (!port->state.alt) {
550 port->state.alt = port->port_altmode[CROS_EC_ALTMODE_DP];
551 ret = cros_typec_usb_safe_state(port);
556 port->state.data = &dp_data;
557 port->state.mode = TYPEC_MODAL_STATE(ffs(pd_ctrl->dp_mode));
559 ret = cros_typec_retimer_set(port->retimer, port->state);
561 ret = typec_mux_set(port->mux, &port->state);
566 static int cros_typec_enable_usb4(struct cros_typec_data *typec,
568 struct ec_response_usb_pd_control_v2 *pd_ctrl)
570 struct cros_typec_port *port = typec->ports[port_num];
571 struct enter_usb_data data;
573 data.eudo = EUDO_USB_MODE_USB4 << EUDO_USB_MODE_SHIFT;
576 data.eudo |= pd_ctrl->cable_speed << EUDO_CABLE_SPEED_SHIFT;
579 if (pd_ctrl->control_flags & USB_PD_CTRL_OPTICAL_CABLE)
580 data.eudo |= EUDO_CABLE_TYPE_OPTICAL << EUDO_CABLE_TYPE_SHIFT;
581 else if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE)
582 data.eudo |= EUDO_CABLE_TYPE_RE_TIMER << EUDO_CABLE_TYPE_SHIFT;
584 data.active_link_training = !!(pd_ctrl->control_flags &
585 USB_PD_CTRL_ACTIVE_LINK_UNIDIR);
587 port->state.alt = NULL;
588 port->state.data = &data;
589 port->state.mode = TYPEC_MODE_USB4;
591 return typec_mux_set(port->mux, &port->state);
594 static int cros_typec_configure_mux(struct cros_typec_data *typec, int port_num,
595 struct ec_response_usb_pd_control_v2 *pd_ctrl)
597 struct cros_typec_port *port = typec->ports[port_num];
598 struct ec_response_usb_pd_mux_info resp;
599 struct ec_params_usb_pd_mux_info req = {
602 struct ec_params_usb_pd_mux_ack mux_ack;
603 enum typec_orientation orientation;
606 ret = cros_ec_cmd(typec->ec, 0, EC_CMD_USB_PD_MUX_INFO,
607 &req, sizeof(req), &resp, sizeof(resp));
609 dev_warn(typec->dev, "Failed to get mux info for port: %d, err = %d\n",
614 /* No change needs to be made, let's exit early. */
615 if (port->mux_flags == resp.flags && port->role == pd_ctrl->role)
618 port->mux_flags = resp.flags;
619 port->role = pd_ctrl->role;
621 if (port->mux_flags == USB_PD_MUX_NONE) {
622 ret = cros_typec_usb_disconnect_state(port);
626 if (port->mux_flags & USB_PD_MUX_POLARITY_INVERTED)
627 orientation = TYPEC_ORIENTATION_REVERSE;
629 orientation = TYPEC_ORIENTATION_NORMAL;
631 ret = typec_switch_set(port->ori_sw, orientation);
635 ret = usb_role_switch_set_role(typec->ports[port_num]->role_sw,
636 pd_ctrl->role & PD_CTRL_RESP_ROLE_DATA
637 ? USB_ROLE_HOST : USB_ROLE_DEVICE);
641 if (port->mux_flags & USB_PD_MUX_USB4_ENABLED) {
642 ret = cros_typec_enable_usb4(typec, port_num, pd_ctrl);
643 } else if (port->mux_flags & USB_PD_MUX_TBT_COMPAT_ENABLED) {
644 ret = cros_typec_enable_tbt(typec, port_num, pd_ctrl);
645 } else if (port->mux_flags & USB_PD_MUX_DP_ENABLED) {
646 ret = cros_typec_enable_dp(typec, port_num, pd_ctrl);
647 } else if (port->mux_flags & USB_PD_MUX_SAFE_MODE) {
648 ret = cros_typec_usb_safe_state(port);
649 } else if (port->mux_flags & USB_PD_MUX_USB_ENABLED) {
650 port->state.alt = NULL;
651 port->state.mode = TYPEC_STATE_USB;
653 ret = cros_typec_retimer_set(port->retimer, port->state);
655 ret = typec_mux_set(port->mux, &port->state);
658 "Unrecognized mode requested, mux flags: %x\n",
663 if (!typec->needs_mux_ack)
666 /* Sending Acknowledgment to EC */
667 mux_ack.port = port_num;
669 if (cros_ec_cmd(typec->ec, 0, EC_CMD_USB_PD_MUX_ACK, &mux_ack,
670 sizeof(mux_ack), NULL, 0) < 0)
672 "Failed to send Mux ACK to EC for port: %d\n",
678 static void cros_typec_set_port_params_v0(struct cros_typec_data *typec,
679 int port_num, struct ec_response_usb_pd_control *resp)
681 struct typec_port *port = typec->ports[port_num]->port;
682 enum typec_orientation polarity;
685 polarity = TYPEC_ORIENTATION_NONE;
686 else if (!resp->polarity)
687 polarity = TYPEC_ORIENTATION_NORMAL;
689 polarity = TYPEC_ORIENTATION_REVERSE;
691 typec_set_pwr_role(port, resp->role ? TYPEC_SOURCE : TYPEC_SINK);
692 typec_set_orientation(port, polarity);
695 static void cros_typec_set_port_params_v1(struct cros_typec_data *typec,
696 int port_num, struct ec_response_usb_pd_control_v1 *resp)
698 struct typec_port *port = typec->ports[port_num]->port;
699 enum typec_orientation polarity;
703 if (!(resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED))
704 polarity = TYPEC_ORIENTATION_NONE;
705 else if (!resp->polarity)
706 polarity = TYPEC_ORIENTATION_NORMAL;
708 polarity = TYPEC_ORIENTATION_REVERSE;
709 typec_set_orientation(port, polarity);
710 typec_set_data_role(port, resp->role & PD_CTRL_RESP_ROLE_DATA ?
711 TYPEC_HOST : TYPEC_DEVICE);
712 typec_set_pwr_role(port, resp->role & PD_CTRL_RESP_ROLE_POWER ?
713 TYPEC_SOURCE : TYPEC_SINK);
714 typec_set_vconn_role(port, resp->role & PD_CTRL_RESP_ROLE_VCONN ?
715 TYPEC_SOURCE : TYPEC_SINK);
717 /* Register/remove partners when a connect/disconnect occurs. */
718 if (resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED) {
719 if (typec->ports[port_num]->partner)
722 pd_en = resp->enabled & PD_CTRL_RESP_ENABLED_PD_CAPABLE;
723 ret = cros_typec_add_partner(typec, port_num, pd_en);
726 "Failed to register partner on port: %d\n",
729 cros_typec_remove_partner(typec, port_num);
730 cros_typec_remove_cable(typec, port_num);
735 * Helper function to register partner/plug altmodes.
737 static int cros_typec_register_altmodes(struct cros_typec_data *typec, int port_num,
740 struct cros_typec_port *port = typec->ports[port_num];
741 struct ec_response_typec_discovery *sop_disc = port->disc_data;
742 struct cros_typec_altmode_node *node;
743 struct typec_altmode_desc desc;
744 struct typec_altmode *amode;
745 int num_altmodes = 0;
749 for (i = 0; i < sop_disc->svid_count; i++) {
750 for (j = 0; j < sop_disc->svids[i].mode_count; j++) {
751 memset(&desc, 0, sizeof(desc));
752 desc.svid = sop_disc->svids[i].svid;
754 desc.vdo = sop_disc->svids[i].mode_vdo[j];
757 amode = typec_partner_register_altmode(port->partner, &desc);
759 amode = typec_plug_register_altmode(port->plug, &desc);
762 ret = PTR_ERR(amode);
766 /* If no memory is available we should unregister and exit. */
767 node = devm_kzalloc(typec->dev, sizeof(*node), GFP_KERNEL);
770 typec_unregister_altmode(amode);
777 list_add_tail(&node->list, &port->partner_mode_list);
779 list_add_tail(&node->list, &port->plug_mode_list);
785 ret = typec_partner_set_num_altmodes(port->partner, num_altmodes);
787 ret = typec_plug_set_num_altmodes(port->plug, num_altmodes);
790 dev_err(typec->dev, "Unable to set %s num_altmodes for port: %d\n",
791 is_partner ? "partner" : "plug", port_num);
798 cros_typec_unregister_altmodes(typec, port_num, is_partner);
803 * Parse the PD identity data from the EC PD discovery responses and copy that to the supplied
804 * PD identity struct.
806 static void cros_typec_parse_pd_identity(struct usb_pd_identity *id,
807 struct ec_response_typec_discovery *disc)
811 /* First, update the PD identity VDOs for the partner. */
812 if (disc->identity_count > 0)
813 id->id_header = disc->discovery_vdo[0];
814 if (disc->identity_count > 1)
815 id->cert_stat = disc->discovery_vdo[1];
816 if (disc->identity_count > 2)
817 id->product = disc->discovery_vdo[2];
819 /* Copy the remaining identity VDOs till a maximum of 6. */
820 for (i = 3; i < disc->identity_count && i < VDO_MAX_OBJECTS; i++)
821 id->vdo[i - 3] = disc->discovery_vdo[i];
824 static int cros_typec_handle_sop_prime_disc(struct cros_typec_data *typec, int port_num, u16 pd_revision)
826 struct cros_typec_port *port = typec->ports[port_num];
827 struct ec_response_typec_discovery *disc = port->disc_data;
828 struct typec_cable_desc c_desc = {};
829 struct typec_plug_desc p_desc;
830 struct ec_params_typec_discovery req = {
832 .partner_type = TYPEC_PARTNER_SOP_PRIME,
837 memset(disc, 0, EC_PROTO2_MAX_RESPONSE_SIZE);
838 ret = cros_ec_cmd(typec->ec, 0, EC_CMD_TYPEC_DISCOVERY, &req, sizeof(req),
839 disc, EC_PROTO2_MAX_RESPONSE_SIZE);
841 dev_err(typec->dev, "Failed to get SOP' discovery data for port: %d\n", port_num);
842 goto sop_prime_disc_exit;
845 /* Parse the PD identity data, even if only 0s were returned. */
846 cros_typec_parse_pd_identity(&port->c_identity, disc);
848 if (disc->identity_count != 0) {
849 cable_plug_type = VDO_TYPEC_CABLE_TYPE(port->c_identity.vdo[0]);
850 switch (cable_plug_type) {
852 c_desc.type = USB_PLUG_TYPE_A;
855 c_desc.type = USB_PLUG_TYPE_B;
858 c_desc.type = USB_PLUG_TYPE_C;
861 c_desc.type = USB_PLUG_CAPTIVE;
864 c_desc.type = USB_PLUG_NONE;
866 c_desc.active = PD_IDH_PTYPE(port->c_identity.id_header) == IDH_PTYPE_ACABLE;
869 c_desc.identity = &port->c_identity;
870 c_desc.pd_revision = pd_revision;
872 port->cable = typec_register_cable(port->port, &c_desc);
873 if (IS_ERR(port->cable)) {
874 ret = PTR_ERR(port->cable);
876 goto sop_prime_disc_exit;
879 p_desc.index = TYPEC_PLUG_SOP_P;
880 port->plug = typec_register_plug(port->cable, &p_desc);
881 if (IS_ERR(port->plug)) {
882 ret = PTR_ERR(port->plug);
884 goto sop_prime_disc_exit;
887 ret = cros_typec_register_altmodes(typec, port_num, false);
889 dev_err(typec->dev, "Failed to register plug altmodes, port: %d\n", port_num);
890 goto sop_prime_disc_exit;
896 cros_typec_remove_cable(typec, port_num);
900 static int cros_typec_handle_sop_disc(struct cros_typec_data *typec, int port_num, u16 pd_revision)
902 struct cros_typec_port *port = typec->ports[port_num];
903 struct ec_response_typec_discovery *sop_disc = port->disc_data;
904 struct ec_params_typec_discovery req = {
906 .partner_type = TYPEC_PARTNER_SOP,
910 if (!port->partner) {
912 "SOP Discovery received without partner registered, port: %d\n",
918 typec_partner_set_pd_revision(port->partner, pd_revision);
920 memset(sop_disc, 0, EC_PROTO2_MAX_RESPONSE_SIZE);
921 ret = cros_ec_cmd(typec->ec, 0, EC_CMD_TYPEC_DISCOVERY, &req, sizeof(req),
922 sop_disc, EC_PROTO2_MAX_RESPONSE_SIZE);
924 dev_err(typec->dev, "Failed to get SOP discovery data for port: %d\n", port_num);
928 cros_typec_parse_pd_identity(&port->p_identity, sop_disc);
930 ret = typec_partner_set_identity(port->partner);
932 dev_err(typec->dev, "Failed to update partner PD identity, port: %d\n", port_num);
936 ret = cros_typec_register_altmodes(typec, port_num, true);
938 dev_err(typec->dev, "Failed to register partner altmodes, port: %d\n", port_num);
946 static int cros_typec_send_clear_event(struct cros_typec_data *typec, int port_num, u32 events_mask)
948 struct ec_params_typec_control req = {
950 .command = TYPEC_CONTROL_COMMAND_CLEAR_EVENTS,
951 .clear_events_mask = events_mask,
954 return cros_ec_cmd(typec->ec, 0, EC_CMD_TYPEC_CONTROL, &req,
955 sizeof(req), NULL, 0);
958 static void cros_typec_register_partner_pdos(struct cros_typec_data *typec,
959 struct ec_response_typec_status *resp, int port_num)
961 struct usb_power_delivery_capabilities_desc caps_desc = {};
962 struct usb_power_delivery_desc desc = {
963 .revision = (le16_to_cpu(resp->sop_revision) & 0xff00) >> 4,
965 struct cros_typec_port *port = typec->ports[port_num];
967 if (!port->partner || port->partner_pd)
970 /* If no caps are available, don't bother creating a device. */
971 if (!resp->source_cap_count && !resp->sink_cap_count)
974 port->partner_pd = typec_partner_usb_power_delivery_register(port->partner, &desc);
975 if (IS_ERR(port->partner_pd)) {
976 dev_warn(typec->dev, "Failed to register partner PD device, port: %d\n", port_num);
980 typec_partner_set_usb_power_delivery(port->partner, port->partner_pd);
982 memcpy(caps_desc.pdo, resp->source_cap_pdos, sizeof(u32) * resp->source_cap_count);
983 caps_desc.role = TYPEC_SOURCE;
984 port->partner_src_caps = usb_power_delivery_register_capabilities(port->partner_pd,
986 if (IS_ERR(port->partner_src_caps))
987 dev_warn(typec->dev, "Failed to register source caps, port: %d\n", port_num);
989 memset(&caps_desc, 0, sizeof(caps_desc));
990 memcpy(caps_desc.pdo, resp->sink_cap_pdos, sizeof(u32) * resp->sink_cap_count);
991 caps_desc.role = TYPEC_SINK;
992 port->partner_sink_caps = usb_power_delivery_register_capabilities(port->partner_pd,
994 if (IS_ERR(port->partner_sink_caps))
995 dev_warn(typec->dev, "Failed to register sink caps, port: %d\n", port_num);
998 static void cros_typec_handle_status(struct cros_typec_data *typec, int port_num)
1000 struct ec_response_typec_status resp;
1001 struct ec_params_typec_status req = {
1006 ret = cros_ec_cmd(typec->ec, 0, EC_CMD_TYPEC_STATUS, &req, sizeof(req),
1007 &resp, sizeof(resp));
1009 dev_warn(typec->dev, "EC_CMD_TYPEC_STATUS failed for port: %d\n", port_num);
1013 /* If we got a hard reset, unregister everything and return. */
1014 if (resp.events & PD_STATUS_EVENT_HARD_RESET) {
1015 cros_typec_remove_partner(typec, port_num);
1016 cros_typec_remove_cable(typec, port_num);
1018 ret = cros_typec_send_clear_event(typec, port_num,
1019 PD_STATUS_EVENT_HARD_RESET);
1021 dev_warn(typec->dev,
1022 "Failed hard reset event clear, port: %d\n", port_num);
1026 /* Handle any events appropriately. */
1027 if (resp.events & PD_STATUS_EVENT_SOP_DISC_DONE && !typec->ports[port_num]->sop_disc_done) {
1030 /* Convert BCD to the format preferred by the TypeC framework */
1031 sop_revision = (le16_to_cpu(resp.sop_revision) & 0xff00) >> 4;
1032 ret = cros_typec_handle_sop_disc(typec, port_num, sop_revision);
1034 dev_err(typec->dev, "Couldn't parse SOP Disc data, port: %d\n", port_num);
1036 typec->ports[port_num]->sop_disc_done = true;
1037 ret = cros_typec_send_clear_event(typec, port_num,
1038 PD_STATUS_EVENT_SOP_DISC_DONE);
1040 dev_warn(typec->dev,
1041 "Failed SOP Disc event clear, port: %d\n", port_num);
1043 if (resp.sop_connected)
1044 typec_set_pwr_opmode(typec->ports[port_num]->port, TYPEC_PWR_MODE_PD);
1046 cros_typec_register_partner_pdos(typec, &resp, port_num);
1049 if (resp.events & PD_STATUS_EVENT_SOP_PRIME_DISC_DONE &&
1050 !typec->ports[port_num]->sop_prime_disc_done) {
1051 u16 sop_prime_revision;
1053 /* Convert BCD to the format preferred by the TypeC framework */
1054 sop_prime_revision = (le16_to_cpu(resp.sop_prime_revision) & 0xff00) >> 4;
1055 ret = cros_typec_handle_sop_prime_disc(typec, port_num, sop_prime_revision);
1057 dev_err(typec->dev, "Couldn't parse SOP' Disc data, port: %d\n", port_num);
1059 typec->ports[port_num]->sop_prime_disc_done = true;
1060 ret = cros_typec_send_clear_event(typec, port_num,
1061 PD_STATUS_EVENT_SOP_PRIME_DISC_DONE);
1063 dev_warn(typec->dev,
1064 "Failed SOP Disc event clear, port: %d\n", port_num);
1069 static int cros_typec_port_update(struct cros_typec_data *typec, int port_num)
1071 struct ec_params_usb_pd_control req;
1072 struct ec_response_usb_pd_control_v2 resp;
1075 if (port_num < 0 || port_num >= typec->num_ports) {
1076 dev_err(typec->dev, "cannot get status for invalid port %d\n",
1081 req.port = port_num;
1082 req.role = USB_PD_CTRL_ROLE_NO_CHANGE;
1083 req.mux = USB_PD_CTRL_MUX_NO_CHANGE;
1084 req.swap = USB_PD_CTRL_SWAP_NONE;
1086 ret = cros_ec_cmd(typec->ec, typec->pd_ctrl_ver,
1087 EC_CMD_USB_PD_CONTROL, &req, sizeof(req),
1088 &resp, sizeof(resp));
1092 /* Update the switches if they exist, according to requested state */
1093 ret = cros_typec_configure_mux(typec, port_num, &resp);
1095 dev_warn(typec->dev, "Configure muxes failed, err = %d\n", ret);
1097 dev_dbg(typec->dev, "Enabled %d: 0x%hhx\n", port_num, resp.enabled);
1098 dev_dbg(typec->dev, "Role %d: 0x%hhx\n", port_num, resp.role);
1099 dev_dbg(typec->dev, "Polarity %d: 0x%hhx\n", port_num, resp.polarity);
1100 dev_dbg(typec->dev, "State %d: %s\n", port_num, resp.state);
1102 if (typec->pd_ctrl_ver != 0)
1103 cros_typec_set_port_params_v1(typec, port_num,
1104 (struct ec_response_usb_pd_control_v1 *)&resp);
1106 cros_typec_set_port_params_v0(typec, port_num,
1107 (struct ec_response_usb_pd_control *) &resp);
1109 if (typec->typec_cmd_supported)
1110 cros_typec_handle_status(typec, port_num);
1115 static int cros_typec_get_cmd_version(struct cros_typec_data *typec)
1117 struct ec_params_get_cmd_versions_v1 req_v1;
1118 struct ec_response_get_cmd_versions resp;
1121 /* We're interested in the PD control command version. */
1122 req_v1.cmd = EC_CMD_USB_PD_CONTROL;
1123 ret = cros_ec_cmd(typec->ec, 1, EC_CMD_GET_CMD_VERSIONS,
1124 &req_v1, sizeof(req_v1), &resp, sizeof(resp));
1128 if (resp.version_mask & EC_VER_MASK(2))
1129 typec->pd_ctrl_ver = 2;
1130 else if (resp.version_mask & EC_VER_MASK(1))
1131 typec->pd_ctrl_ver = 1;
1133 typec->pd_ctrl_ver = 0;
1135 dev_dbg(typec->dev, "PD Control has version mask 0x%02x\n",
1136 typec->pd_ctrl_ver & 0xff);
1141 static void cros_typec_port_work(struct work_struct *work)
1143 struct cros_typec_data *typec = container_of(work, struct cros_typec_data, port_work);
1146 for (i = 0; i < typec->num_ports; i++) {
1147 ret = cros_typec_port_update(typec, i);
1149 dev_warn(typec->dev, "Update failed for port: %d\n", i);
1153 static int cros_ec_typec_event(struct notifier_block *nb,
1154 unsigned long host_event, void *_notify)
1156 struct cros_typec_data *typec = container_of(nb, struct cros_typec_data, nb);
1158 flush_work(&typec->port_work);
1159 schedule_work(&typec->port_work);
1165 static const struct acpi_device_id cros_typec_acpi_id[] = {
1169 MODULE_DEVICE_TABLE(acpi, cros_typec_acpi_id);
1173 static const struct of_device_id cros_typec_of_match[] = {
1174 { .compatible = "google,cros-ec-typec", },
1177 MODULE_DEVICE_TABLE(of, cros_typec_of_match);
1180 static int cros_typec_probe(struct platform_device *pdev)
1182 struct cros_ec_dev *ec_dev = NULL;
1183 struct device *dev = &pdev->dev;
1184 struct cros_typec_data *typec;
1185 struct ec_response_usb_pd_ports resp;
1188 typec = devm_kzalloc(dev, sizeof(*typec), GFP_KERNEL);
1194 typec->ec = dev_get_drvdata(pdev->dev.parent);
1196 dev_err(dev, "couldn't find parent EC device\n");
1200 platform_set_drvdata(pdev, typec);
1202 ret = cros_typec_get_cmd_version(typec);
1204 dev_err(dev, "failed to get PD command version info\n");
1208 ec_dev = dev_get_drvdata(&typec->ec->ec->dev);
1210 return -EPROBE_DEFER;
1212 typec->typec_cmd_supported = cros_ec_check_features(ec_dev, EC_FEATURE_TYPEC_CMD);
1213 typec->needs_mux_ack = cros_ec_check_features(ec_dev, EC_FEATURE_TYPEC_MUX_REQUIRE_AP_ACK);
1215 ret = cros_ec_cmd(typec->ec, 0, EC_CMD_USB_PD_PORTS, NULL, 0,
1216 &resp, sizeof(resp));
1220 typec->num_ports = resp.num_ports;
1221 if (typec->num_ports > EC_USB_PD_MAX_PORTS) {
1222 dev_warn(typec->dev,
1223 "Too many ports reported: %d, limiting to max: %d\n",
1224 typec->num_ports, EC_USB_PD_MAX_PORTS);
1225 typec->num_ports = EC_USB_PD_MAX_PORTS;
1228 ret = cros_typec_init_ports(typec);
1232 INIT_WORK(&typec->port_work, cros_typec_port_work);
1235 * Safe to call port update here, since we haven't registered the
1238 for (i = 0; i < typec->num_ports; i++) {
1239 ret = cros_typec_port_update(typec, i);
1241 goto unregister_ports;
1244 typec->nb.notifier_call = cros_ec_typec_event;
1245 ret = cros_usbpd_register_notify(&typec->nb);
1247 goto unregister_ports;
1252 cros_unregister_ports(typec);
1256 static int __maybe_unused cros_typec_suspend(struct device *dev)
1258 struct cros_typec_data *typec = dev_get_drvdata(dev);
1260 cancel_work_sync(&typec->port_work);
1265 static int __maybe_unused cros_typec_resume(struct device *dev)
1267 struct cros_typec_data *typec = dev_get_drvdata(dev);
1269 /* Refresh port state. */
1270 schedule_work(&typec->port_work);
1275 static const struct dev_pm_ops cros_typec_pm_ops = {
1276 SET_SYSTEM_SLEEP_PM_OPS(cros_typec_suspend, cros_typec_resume)
1279 static struct platform_driver cros_typec_driver = {
1282 .acpi_match_table = ACPI_PTR(cros_typec_acpi_id),
1283 .of_match_table = of_match_ptr(cros_typec_of_match),
1284 .pm = &cros_typec_pm_ops,
1286 .probe = cros_typec_probe,
1289 module_platform_driver(cros_typec_driver);
1291 MODULE_AUTHOR("Prashant Malani <pmalani@chromium.org>");
1292 MODULE_DESCRIPTION("Chrome OS EC Type C control");
1293 MODULE_LICENSE("GPL");