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/module.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>
24 #define DRV_NAME "cros-ec-typec"
26 /* Supported alt modes. */
28 CROS_EC_ALTMODE_DP = 0,
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;
45 /* Variables keeping track of switch state. */
46 struct typec_mux_state state;
50 struct typec_altmode p_altmode[CROS_EC_ALTMODE_MAX];
53 /* Platform-specific data for the Chrome OS EC Type C controller. */
54 struct cros_typec_data {
56 struct cros_ec_device *ec;
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;
65 static int cros_typec_parse_port_props(struct typec_capability *cap,
66 struct fwnode_handle *fwnode,
72 memset(cap, 0, sizeof(*cap));
73 ret = fwnode_property_read_string(fwnode, "power-role", &buf);
75 dev_err(dev, "power-role not found: %d\n", ret);
79 ret = typec_find_port_power_role(buf);
84 ret = fwnode_property_read_string(fwnode, "data-role", &buf);
86 dev_err(dev, "data-role not found: %d\n", ret);
90 ret = typec_find_port_data_role(buf);
95 ret = fwnode_property_read_string(fwnode, "try-power-role", &buf);
97 dev_err(dev, "try-power-role not found: %d\n", ret);
101 ret = typec_find_power_role(buf);
104 cap->prefer_role = ret;
106 cap->fwnode = fwnode;
111 static int cros_typec_get_switch_handles(struct cros_typec_port *port,
112 struct fwnode_handle *fwnode,
115 port->mux = fwnode_typec_mux_get(fwnode, NULL);
116 if (IS_ERR(port->mux)) {
117 dev_dbg(dev, "Mux handle not found.\n");
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");
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");
136 usb_role_switch_put(port->role_sw);
138 typec_switch_put(port->ori_sw);
140 typec_mux_put(port->mux);
145 static int cros_typec_add_partner(struct cros_typec_data *typec, int port_num,
148 struct cros_typec_port *port = typec->ports[port_num];
149 struct typec_partner_desc p_desc = {
155 * Fill an initial PD identity, which will then be updated with info
158 p_desc.identity = &port->p_identity;
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;
169 static void cros_typec_remove_partner(struct cros_typec_data *typec,
172 struct cros_typec_port *port = typec->ports[port_num];
174 port->state.alt = NULL;
175 port->state.mode = TYPEC_STATE_USB;
176 port->state.data = NULL;
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);
182 typec_unregister_partner(port->partner);
183 port->partner = NULL;
186 static void cros_unregister_ports(struct cros_typec_data *typec)
190 for (i = 0; i < typec->num_ports; i++) {
191 if (!typec->ports[i])
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);
202 * Fake the alt mode structs until we actually start registering Type C port
203 * and partner alt modes.
205 static void cros_typec_register_port_altmodes(struct cros_typec_data *typec,
208 struct cros_typec_port *port = typec->ports[port_num];
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;
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
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;
222 port->state.alt = NULL;
223 port->state.mode = TYPEC_STATE_USB;
224 port->state.data = NULL;
227 static int cros_typec_init_ports(struct cros_typec_data *typec)
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;
238 nports = device_get_child_node_count(dev);
240 dev_err(dev, "No port entries found.\n");
244 if (nports > typec->num_ports) {
245 dev_err(dev, "More ports listed than can be supported.\n");
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)) {
254 dev_err(dev, "No port-number for port, aborting.\n");
255 goto unregister_ports;
258 if (port_num >= typec->num_ports) {
259 dev_err(dev, "Invalid port number.\n");
261 goto unregister_ports;
264 dev_dbg(dev, "Registering port %d\n", port_num);
266 cros_port = devm_kzalloc(dev, sizeof(*cros_port), GFP_KERNEL);
269 goto unregister_ports;
272 typec->ports[port_num] = cros_port;
273 cap = &cros_port->caps;
275 ret = cros_typec_parse_port_props(cap, fwnode, dev);
277 goto unregister_ports;
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;
286 ret = cros_typec_get_switch_handles(cros_port, fwnode, dev);
288 dev_dbg(dev, "No switch control for port %d\n",
291 cros_typec_register_port_altmodes(typec, port_num);
297 cros_unregister_ports(typec);
301 static int cros_typec_ec_command(struct cros_typec_data *typec,
302 unsigned int version,
303 unsigned int command,
305 unsigned int outsize,
309 struct cros_ec_command *msg;
312 msg = kzalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL);
316 msg->version = version;
317 msg->command = command;
318 msg->outsize = outsize;
319 msg->insize = insize;
322 memcpy(msg->data, outdata, outsize);
324 ret = cros_ec_cmd_xfer_status(typec->ec, msg);
325 if (ret >= 0 && insize)
326 memcpy(indata, msg->data, insize);
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)
335 struct typec_port *port = typec->ports[port_num]->port;
336 enum typec_orientation polarity;
339 polarity = TYPEC_ORIENTATION_NONE;
340 else if (!resp->polarity)
341 polarity = TYPEC_ORIENTATION_NORMAL;
343 polarity = TYPEC_ORIENTATION_REVERSE;
345 typec_set_pwr_role(port, resp->role ? TYPEC_SOURCE : TYPEC_SINK);
346 typec_set_orientation(port, polarity);
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)
352 struct typec_port *port = typec->ports[port_num]->port;
353 enum typec_orientation polarity;
357 if (!(resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED))
358 polarity = TYPEC_ORIENTATION_NONE;
359 else if (!resp->polarity)
360 polarity = TYPEC_ORIENTATION_NORMAL;
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);
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)
376 pd_en = resp->enabled & PD_CTRL_RESP_ENABLED_PD_CAPABLE;
377 ret = cros_typec_add_partner(typec, port_num, pd_en);
380 "Failed to register partner on port: %d\n",
383 if (!typec->ports[port_num]->partner)
385 cros_typec_remove_partner(typec, port_num);
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)
392 struct ec_params_usb_pd_mux_info req = {
396 return cros_typec_ec_command(typec, 0, EC_CMD_USB_PD_MUX_INFO, &req,
397 sizeof(req), resp, sizeof(*resp));
400 static int cros_typec_usb_safe_state(struct cros_typec_port *port)
402 port->state.mode = TYPEC_STATE_SAFE;
404 return typec_mux_set(port->mux, &port->state);
408 * Spoof the VDOs that were likely communicated by the partner for TBT alt
411 static int cros_typec_enable_tbt(struct cros_typec_data *typec,
413 struct ec_response_usb_pd_control_v2 *pd_ctrl)
415 struct cros_typec_port *port = typec->ports[port_num];
416 struct typec_thunderbolt_data data;
419 if (typec->pd_ctrl_ver < 2) {
421 "PD_CTRL version too old: %d\n", typec->pd_ctrl_ver);
425 /* Device Discover Mode VDO */
426 data.device_mode = TBT_MODE;
428 if (pd_ctrl->control_flags & USB_PD_CTRL_TBT_LEGACY_ADAPTER)
429 data.device_mode = TBT_SET_ADAPTER(TBT_ADAPTER_TBT3);
431 /* Cable Discover Mode VDO */
432 data.cable_mode = TBT_MODE;
433 data.cable_mode |= TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed);
435 if (pd_ctrl->control_flags & USB_PD_CTRL_OPTICAL_CABLE)
436 data.cable_mode |= TBT_CABLE_OPTICAL;
438 if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_LINK_UNIDIR)
439 data.cable_mode |= TBT_CABLE_LINK_TRAINING;
441 data.cable_mode |= TBT_SET_CABLE_ROUNDED(pd_ctrl->cable_gen);
444 data.enter_vdo = TBT_SET_CABLE_SPEED(pd_ctrl->cable_speed);
446 if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE)
447 data.enter_vdo |= TBT_ENTER_MODE_ACTIVE_CABLE;
449 if (!port->state.alt) {
450 port->state.alt = &port->p_altmode[CROS_EC_ALTMODE_TBT];
451 ret = cros_typec_usb_safe_state(port);
456 port->state.data = &data;
457 port->state.mode = TYPEC_TBT_MODE;
459 return typec_mux_set(port->mux, &port->state);
462 /* Spoof the VDOs that were likely communicated by the partner. */
463 static int cros_typec_enable_dp(struct cros_typec_data *typec,
465 struct ec_response_usb_pd_control_v2 *pd_ctrl)
467 struct cros_typec_port *port = typec->ports[port_num];
468 struct typec_displayport_data dp_data;
471 if (typec->pd_ctrl_ver < 2) {
473 "PD_CTRL version too old: %d\n", typec->pd_ctrl_ver);
478 dp_data.status = DP_STATUS_ENABLED;
479 if (port->mux_flags & USB_PD_MUX_HPD_IRQ)
480 dp_data.status |= DP_STATUS_IRQ_HPD;
481 if (port->mux_flags & USB_PD_MUX_HPD_LVL)
482 dp_data.status |= DP_STATUS_HPD_STATE;
484 /* Configuration VDO. */
485 dp_data.conf = DP_CONF_SET_PIN_ASSIGN(pd_ctrl->dp_mode);
486 if (!port->state.alt) {
487 port->state.alt = &port->p_altmode[CROS_EC_ALTMODE_DP];
488 ret = cros_typec_usb_safe_state(port);
493 port->state.data = &dp_data;
494 port->state.mode = TYPEC_MODAL_STATE(ffs(pd_ctrl->dp_mode));
496 return typec_mux_set(port->mux, &port->state);
499 static int cros_typec_enable_usb4(struct cros_typec_data *typec,
501 struct ec_response_usb_pd_control_v2 *pd_ctrl)
503 struct cros_typec_port *port = typec->ports[port_num];
504 struct enter_usb_data data;
506 data.eudo = EUDO_USB_MODE_USB4 << EUDO_USB_MODE_SHIFT;
509 data.eudo |= pd_ctrl->cable_speed << EUDO_CABLE_SPEED_SHIFT;
512 if (pd_ctrl->control_flags & USB_PD_CTRL_OPTICAL_CABLE)
513 data.eudo |= EUDO_CABLE_TYPE_OPTICAL << EUDO_CABLE_TYPE_SHIFT;
514 else if (pd_ctrl->control_flags & USB_PD_CTRL_ACTIVE_CABLE)
515 data.eudo |= EUDO_CABLE_TYPE_RE_TIMER << EUDO_CABLE_TYPE_SHIFT;
517 data.active_link_training = !!(pd_ctrl->control_flags &
518 USB_PD_CTRL_ACTIVE_LINK_UNIDIR);
520 port->state.alt = NULL;
521 port->state.data = &data;
522 port->state.mode = TYPEC_MODE_USB4;
524 return typec_mux_set(port->mux, &port->state);
527 static int cros_typec_configure_mux(struct cros_typec_data *typec, int port_num,
529 struct ec_response_usb_pd_control_v2 *pd_ctrl)
531 struct cros_typec_port *port = typec->ports[port_num];
532 enum typec_orientation orientation;
538 if (mux_flags & USB_PD_MUX_POLARITY_INVERTED)
539 orientation = TYPEC_ORIENTATION_REVERSE;
541 orientation = TYPEC_ORIENTATION_NORMAL;
543 ret = typec_switch_set(port->ori_sw, orientation);
547 ret = usb_role_switch_set_role(typec->ports[port_num]->role_sw,
548 pd_ctrl->role & PD_CTRL_RESP_ROLE_DATA
549 ? USB_ROLE_HOST : USB_ROLE_DEVICE);
553 if (mux_flags & USB_PD_MUX_USB4_ENABLED) {
554 ret = cros_typec_enable_usb4(typec, port_num, pd_ctrl);
555 } else if (mux_flags & USB_PD_MUX_TBT_COMPAT_ENABLED) {
556 ret = cros_typec_enable_tbt(typec, port_num, pd_ctrl);
557 } else if (mux_flags & USB_PD_MUX_DP_ENABLED) {
558 ret = cros_typec_enable_dp(typec, port_num, pd_ctrl);
559 } else if (mux_flags & USB_PD_MUX_SAFE_MODE) {
560 ret = cros_typec_usb_safe_state(port);
561 } else if (mux_flags & USB_PD_MUX_USB_ENABLED) {
562 port->state.alt = NULL;
563 port->state.mode = TYPEC_STATE_USB;
564 ret = typec_mux_set(port->mux, &port->state);
567 "Unsupported mode requested, mux flags: %x\n",
575 static int cros_typec_port_update(struct cros_typec_data *typec, int port_num)
577 struct ec_params_usb_pd_control req;
578 struct ec_response_usb_pd_control_v2 resp;
579 struct ec_response_usb_pd_mux_info mux_resp;
582 if (port_num < 0 || port_num >= typec->num_ports) {
583 dev_err(typec->dev, "cannot get status for invalid port %d\n",
589 req.role = USB_PD_CTRL_ROLE_NO_CHANGE;
590 req.mux = USB_PD_CTRL_MUX_NO_CHANGE;
591 req.swap = USB_PD_CTRL_SWAP_NONE;
593 ret = cros_typec_ec_command(typec, typec->pd_ctrl_ver,
594 EC_CMD_USB_PD_CONTROL, &req, sizeof(req),
595 &resp, sizeof(resp));
599 dev_dbg(typec->dev, "Enabled %d: 0x%hhx\n", port_num, resp.enabled);
600 dev_dbg(typec->dev, "Role %d: 0x%hhx\n", port_num, resp.role);
601 dev_dbg(typec->dev, "Polarity %d: 0x%hhx\n", port_num, resp.polarity);
602 dev_dbg(typec->dev, "State %d: %s\n", port_num, resp.state);
604 if (typec->pd_ctrl_ver != 0)
605 cros_typec_set_port_params_v1(typec, port_num,
606 (struct ec_response_usb_pd_control_v1 *)&resp);
608 cros_typec_set_port_params_v0(typec, port_num,
609 (struct ec_response_usb_pd_control *) &resp);
611 /* Update the switches if they exist, according to requested state */
612 ret = cros_typec_get_mux_info(typec, port_num, &mux_resp);
615 "Failed to get mux info for port: %d, err = %d\n",
620 /* No change needs to be made, let's exit early. */
621 if (typec->ports[port_num]->mux_flags == mux_resp.flags)
624 typec->ports[port_num]->mux_flags = mux_resp.flags;
625 ret = cros_typec_configure_mux(typec, port_num, mux_resp.flags, &resp);
627 dev_warn(typec->dev, "Configure muxes failed, err = %d\n", ret);
632 static int cros_typec_get_cmd_version(struct cros_typec_data *typec)
634 struct ec_params_get_cmd_versions_v1 req_v1;
635 struct ec_response_get_cmd_versions resp;
638 /* We're interested in the PD control command version. */
639 req_v1.cmd = EC_CMD_USB_PD_CONTROL;
640 ret = cros_typec_ec_command(typec, 1, EC_CMD_GET_CMD_VERSIONS,
641 &req_v1, sizeof(req_v1), &resp,
646 if (resp.version_mask & EC_VER_MASK(2))
647 typec->pd_ctrl_ver = 2;
648 else if (resp.version_mask & EC_VER_MASK(1))
649 typec->pd_ctrl_ver = 1;
651 typec->pd_ctrl_ver = 0;
653 dev_dbg(typec->dev, "PD Control has version mask 0x%hhx\n",
659 static void cros_typec_port_work(struct work_struct *work)
661 struct cros_typec_data *typec = container_of(work, struct cros_typec_data, port_work);
664 for (i = 0; i < typec->num_ports; i++) {
665 ret = cros_typec_port_update(typec, i);
667 dev_warn(typec->dev, "Update failed for port: %d\n", i);
671 static int cros_ec_typec_event(struct notifier_block *nb,
672 unsigned long host_event, void *_notify)
674 struct cros_typec_data *typec = container_of(nb, struct cros_typec_data, nb);
676 schedule_work(&typec->port_work);
682 static const struct acpi_device_id cros_typec_acpi_id[] = {
686 MODULE_DEVICE_TABLE(acpi, cros_typec_acpi_id);
690 static const struct of_device_id cros_typec_of_match[] = {
691 { .compatible = "google,cros-ec-typec", },
694 MODULE_DEVICE_TABLE(of, cros_typec_of_match);
697 static int cros_typec_probe(struct platform_device *pdev)
699 struct device *dev = &pdev->dev;
700 struct cros_typec_data *typec;
701 struct ec_response_usb_pd_ports resp;
704 typec = devm_kzalloc(dev, sizeof(*typec), GFP_KERNEL);
709 typec->ec = dev_get_drvdata(pdev->dev.parent);
710 platform_set_drvdata(pdev, typec);
712 ret = cros_typec_get_cmd_version(typec);
714 dev_err(dev, "failed to get PD command version info\n");
718 ret = cros_typec_ec_command(typec, 0, EC_CMD_USB_PD_PORTS, NULL, 0,
719 &resp, sizeof(resp));
723 typec->num_ports = resp.num_ports;
724 if (typec->num_ports > EC_USB_PD_MAX_PORTS) {
726 "Too many ports reported: %d, limiting to max: %d\n",
727 typec->num_ports, EC_USB_PD_MAX_PORTS);
728 typec->num_ports = EC_USB_PD_MAX_PORTS;
731 ret = cros_typec_init_ports(typec);
735 INIT_WORK(&typec->port_work, cros_typec_port_work);
738 * Safe to call port update here, since we haven't registered the
741 for (i = 0; i < typec->num_ports; i++) {
742 ret = cros_typec_port_update(typec, i);
744 goto unregister_ports;
747 typec->nb.notifier_call = cros_ec_typec_event;
748 ret = cros_usbpd_register_notify(&typec->nb);
750 goto unregister_ports;
755 cros_unregister_ports(typec);
759 static int __maybe_unused cros_typec_suspend(struct device *dev)
761 struct cros_typec_data *typec = dev_get_drvdata(dev);
763 cancel_work_sync(&typec->port_work);
768 static int __maybe_unused cros_typec_resume(struct device *dev)
770 struct cros_typec_data *typec = dev_get_drvdata(dev);
772 /* Refresh port state. */
773 schedule_work(&typec->port_work);
778 static const struct dev_pm_ops cros_typec_pm_ops = {
779 SET_SYSTEM_SLEEP_PM_OPS(cros_typec_suspend, cros_typec_resume)
782 static struct platform_driver cros_typec_driver = {
785 .acpi_match_table = ACPI_PTR(cros_typec_acpi_id),
786 .of_match_table = of_match_ptr(cros_typec_of_match),
787 .pm = &cros_typec_pm_ops,
789 .probe = cros_typec_probe,
792 module_platform_driver(cros_typec_driver);
794 MODULE_AUTHOR("Prashant Malani <pmalani@chromium.org>");
795 MODULE_DESCRIPTION("Chrome OS EC Type C control");
796 MODULE_LICENSE("GPL");