Chrome platform driver development
 help / color / mirror / Atom feed
* [PATCH v3 0/2] USB Type-C mode selection
@ 2026-10-28  9:09 Andrei Kuchynski
  2026-10-28  9:09 ` [PATCH v3 1/2] platform/chrome: cros_ec_typec: Enforce priority-based " Andrei Kuchynski
  2026-10-28  9:09 ` [PATCH v3 2/2] platform/chrome: cros_ec_typec: Reorder altmode registration Andrei Kuchynski
  0 siblings, 2 replies; 4+ messages in thread
From: Andrei Kuchynski @ 2026-10-28  9:09 UTC (permalink / raw)
  To: Tzung-Bi Shih
  Cc: Jameson Thies, Benson Leung, Abhishek Pandit-Subedi,
	Guenter Roeck, chrome-platform, linux-kernel, Andrei Kuchynski

This patch series introduces priority-based alternate mode selection
support for cros_ec_typec. 
The base patch series and UCSI support was submitted here:
https://lore.kernel.org/all/20260119131824.2529334-1-akuchynski@chromium.org/

When AP-driven alternate mode selection is active, the driver enforces the
mode_selection bit for each registered Alternate Mode. This blocks
individual altmode drivers from self-activating. Once registration
completes, the driver evaluates USB4 capability. If USB4 is unsupported,
it falls back to priority-based negotiation via typec_mode_selection_start.

Additionally, we swap the registration sequence of the port's alternate
modes. Registering DisplayPort after Thunderbolt correctly ensures DP is
prioritized.

Changes in v3:
- Replaced delay/timeout consts with macros.
- Downgraded USB4/mode selection error logs to dev_warn().
- Fixed comment formatting and added comment for DP/TBT order.
Changes in v2:
- Added this cover letter.
- Rebased on top of the latest platform-chrome tree.
- Tested on Redrix with Linux Fedora 7.3.0-rc5+.
v1 Link:
https://lore.kernel.org/all/20260129131928.794768-1-akuchynski@chromium.org/

Andrei Kuchynski (2):
  platform/chrome: cros_ec_typec: Enforce priority-based mode selection
  platform/chrome: cros_ec_typec: Reorder altmode registration

 drivers/platform/chrome/cros_ec_typec.c      | 65 ++++++++++++++++----
 drivers/platform/chrome/cros_typec_altmode.c |  8 ++-
 2 files changed, 60 insertions(+), 13 deletions(-)

-- 
2.56.0.385.gac671c819a-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v3 1/2] platform/chrome: cros_ec_typec: Enforce priority-based mode selection
  2026-10-28  9:09 [PATCH v3 0/2] USB Type-C mode selection Andrei Kuchynski
@ 2026-10-28  9:09 ` Andrei Kuchynski
  2026-10-29  4:19   ` Tzung-Bi Shih
  2026-10-28  9:09 ` [PATCH v3 2/2] platform/chrome: cros_ec_typec: Reorder altmode registration Andrei Kuchynski
  1 sibling, 1 reply; 4+ messages in thread
From: Andrei Kuchynski @ 2026-10-28  9:09 UTC (permalink / raw)
  To: Tzung-Bi Shih
  Cc: Jameson Thies, Benson Leung, Abhishek Pandit-Subedi,
	Guenter Roeck, chrome-platform, linux-kernel, Andrei Kuchynski

The driver sets mode_selection bit for each Alternate mode, thereby
preventing individual altmode drivers from activating their respective
modes.
Once the registration of all Alternate Modes is complete, the driver
verifies if USB4 mode is supported by both the port and the partner and
activates USB4 mode. In cases where USB4 is not supported, the driver
invokes typec_mode_selection_start to initiate the mode selection process
based on mode priorities.
The driver communicates the current Type-C mode to the mode selection
process via typec_altmode_state_update.

Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
---
 drivers/platform/chrome/cros_ec_typec.c      | 40 +++++++++++++++++++-
 drivers/platform/chrome/cros_typec_altmode.c |  8 +++-
 2 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec_typec.c b/drivers/platform/chrome/cros_ec_typec.c
index 50a68819ceb7b..89d29ef040879 100644
--- a/drivers/platform/chrome/cros_ec_typec.c
+++ b/drivers/platform/chrome/cros_ec_typec.c
@@ -27,6 +27,11 @@
 						BIT(DP_PIN_ASSIGN_D) | \
 						BIT(DP_PIN_ASSIGN_E)))
 
+/* Delay between mode entry/exit attempts, ms */
+#define MODE_SELECTION_DELAY	1000
+/* Timeout for a mode entry attempt, ms */
+#define MODE_SELECTION_TIMEOUT	4000
+
 static void cros_typec_role_switch_quirk(struct fwnode_handle *fwnode)
 {
 #ifdef CONFIG_ACPI
@@ -325,6 +330,7 @@ static void cros_typec_remove_partner(struct cros_typec_data *typec,
 	if (!port->partner)
 		return;
 
+	typec_mode_selection_delete(port->partner);
 	cros_typec_unregister_altmodes(typec, port_num, true);
 
 	typec_partner_set_usb_power_delivery(port->partner, NULL);
@@ -421,7 +427,6 @@ static int cros_typec_register_port_altmodes(struct cros_typec_data *typec,
 		memset(&desc, 0, sizeof(desc));
 		desc.svid = USB_TYPEC_TBT_SID;
 		desc.mode = TBT_MODE;
-		desc.inactive = true;
 		amode = cros_typec_register_thunderbolt(port, &desc);
 		if (IS_ERR(amode))
 			return PTR_ERR(amode);
@@ -742,6 +747,7 @@ static int cros_typec_configure_mux(struct cros_typec_data *typec, int port_num,
 	enum typec_orientation orientation;
 	struct cros_typec_altmode_node *node;
 	int ret;
+	u16 active_svid = 0;
 
 	ret = cros_ec_cmd(typec->ec, 0, EC_CMD_USB_PD_MUX_INFO,
 			  &req, sizeof(req), &resp, sizeof(resp));
@@ -782,8 +788,10 @@ static int cros_typec_configure_mux(struct cros_typec_data *typec, int port_num,
 		ret = cros_typec_enable_usb4(typec, port_num, pd_ctrl);
 	} else if (port->mux_flags & USB_PD_MUX_TBT_COMPAT_ENABLED) {
 		ret = cros_typec_enable_tbt(typec, port_num, pd_ctrl);
+		active_svid = USB_TYPEC_TBT_SID;
 	} else if (port->mux_flags & USB_PD_MUX_DP_ENABLED) {
 		ret = cros_typec_enable_dp(typec, port_num, pd_ctrl);
+		active_svid = USB_TYPEC_DP_SID;
 	} else if (port->mux_flags & USB_PD_MUX_SAFE_MODE) {
 		ret = cros_typec_usb_safe_state(port);
 	} else if (port->mux_flags & USB_PD_MUX_USB_ENABLED) {
@@ -799,6 +807,9 @@ static int cros_typec_configure_mux(struct cros_typec_data *typec, int port_num,
 			port->mux_flags);
 	}
 
+	if (port->partner)
+		typec_altmode_state_update(port->partner, active_svid, ret);
+
 	/* Iterate all partner alt-modes and set the active alternate mode. */
 	list_for_each_entry(node, &port->partner_mode_list, list) {
 		typec_altmode_update_active(
@@ -900,6 +911,7 @@ static int cros_typec_register_altmodes(struct cros_typec_data *typec, int port_
 			desc.svid = sop_disc->svids[i].svid;
 			desc.mode = j + 1;
 			desc.vdo = sop_disc->svids[i].mode_vdo[j];
+			desc.mode_selection = typec->ap_driven_altmode;
 
 			if (is_partner)
 				amode = typec_partner_register_altmode(port->partner, &desc);
@@ -940,6 +952,32 @@ static int cros_typec_register_altmodes(struct cros_typec_data *typec, int port_
 		goto err_cleanup;
 	}
 
+	/*
+	 * Once all partner alt-modes are added, we should also trigger
+	 * mode selection.
+	 */
+	if (is_partner && typec->ap_driven_altmode) {
+		bool usb4_mode = (port->caps.usb_capability & USB_CAPABILITY_USB4) &&
+			(PD_VDO_UFP_DEVCAP(port->p_identity.vdo[0]) & DEV_USB4_CAPABLE);
+
+		if (usb4_mode) {
+			ret = cros_typec_enter_usb_mode(port->port, USB_MODE_USB4);
+			if (ret < 0) {
+				usb4_mode = false;
+				dev_warn(typec->dev,
+					 "Unable to activate USB4 mode, port: %d\n", port_num);
+			}
+		}
+		if (!usb4_mode) {
+			ret = typec_mode_selection_start(port->partner,
+							 MODE_SELECTION_DELAY,
+							 MODE_SELECTION_TIMEOUT);
+			if (ret < 0)
+				dev_warn(typec->dev,
+					 "Unable to run mode selection, port: %d\n", port_num);
+		}
+	}
+
 	return 0;
 
 err_cleanup:
diff --git a/drivers/platform/chrome/cros_typec_altmode.c b/drivers/platform/chrome/cros_typec_altmode.c
index 66c546bf89b53..116329dd113f8 100644
--- a/drivers/platform/chrome/cros_typec_altmode.c
+++ b/drivers/platform/chrome/cros_typec_altmode.c
@@ -41,12 +41,16 @@ static void cros_typec_altmode_work(struct work_struct *work)
 {
 	struct cros_typec_altmode_data *data =
 		container_of(work, struct cros_typec_altmode_data, work);
+	int ret;
 
 	mutex_lock(&data->lock);
 
-	if (typec_altmode_vdm(data->alt, data->header, data->vdo_data,
-			      data->vdo_size))
+	ret = typec_altmode_vdm(data->alt, data->header, data->vdo_data,
+				data->vdo_size);
+	if (ret) {
 		dev_err(&data->alt->dev, "VDM 0x%x failed\n", data->header);
+		typec_altmode_state_update(data->port->partner, data->sid, ret);
+	}
 
 	data->header = 0;
 	data->vdo_data = NULL;
-- 
2.56.0.385.gac671c819a-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v3 2/2] platform/chrome: cros_ec_typec: Reorder altmode registration
  2026-10-28  9:09 [PATCH v3 0/2] USB Type-C mode selection Andrei Kuchynski
  2026-10-28  9:09 ` [PATCH v3 1/2] platform/chrome: cros_ec_typec: Enforce priority-based " Andrei Kuchynski
@ 2026-10-28  9:09 ` Andrei Kuchynski
  1 sibling, 0 replies; 4+ messages in thread
From: Andrei Kuchynski @ 2026-10-28  9:09 UTC (permalink / raw)
  To: Tzung-Bi Shih
  Cc: Jameson Thies, Benson Leung, Abhishek Pandit-Subedi,
	Guenter Roeck, chrome-platform, linux-kernel, Andrei Kuchynski

By default, alternate mode priorities are set in registration order, with
the last mode registered taking precedence. Changing the registration order
of alternate modes prioritizes DisplayPort over Thunderbolt.

Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
---
 drivers/platform/chrome/cros_ec_typec.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec_typec.c b/drivers/platform/chrome/cros_ec_typec.c
index 89d29ef040879..11d9589422eeb 100644
--- a/drivers/platform/chrome/cros_ec_typec.c
+++ b/drivers/platform/chrome/cros_ec_typec.c
@@ -406,16 +406,10 @@ static int cros_typec_register_port_altmodes(struct cros_typec_data *typec,
 	struct typec_altmode_desc desc;
 	struct typec_altmode *amode;
 
-	/* All PD capable CrOS devices are assumed to support DP altmode. */
-	memset(&desc, 0, sizeof(desc));
-	desc.svid = USB_TYPEC_DP_SID;
-	desc.mode = USB_TYPEC_DP_MODE;
-	desc.vdo = DP_PORT_VDO;
-	amode = cros_typec_register_displayport(port, &desc,
-						typec->ap_driven_altmode);
-	if (IS_ERR(amode))
-		return PTR_ERR(amode);
-	port->port_altmode[CROS_EC_ALTMODE_DP] = amode;
+	/*
+	 * Alternate modes registered later take precedence. Register DisplayPort
+	 * after Thunderbolt to prioritize DisplayPort over Thunderbolt.
+	 */
 
 	/*
 	 * Register TBT compatibility alt mode. The EC will not enter the mode
@@ -433,6 +427,17 @@ static int cros_typec_register_port_altmodes(struct cros_typec_data *typec,
 		port->port_altmode[CROS_EC_ALTMODE_TBT] = amode;
 	}
 
+	/* All PD capable CrOS devices are assumed to support DP altmode. */
+	memset(&desc, 0, sizeof(desc));
+	desc.svid = USB_TYPEC_DP_SID;
+	desc.mode = USB_TYPEC_DP_MODE;
+	desc.vdo = DP_PORT_VDO;
+	amode = cros_typec_register_displayport(port, &desc,
+						typec->ap_driven_altmode);
+	if (IS_ERR(amode))
+		return PTR_ERR(amode);
+	port->port_altmode[CROS_EC_ALTMODE_DP] = amode;
+
 	port->state.alt = NULL;
 	port->state.mode = TYPEC_STATE_USB;
 	port->state.data = NULL;
-- 
2.56.0.385.gac671c819a-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v3 1/2] platform/chrome: cros_ec_typec: Enforce priority-based mode selection
  2026-10-28  9:09 ` [PATCH v3 1/2] platform/chrome: cros_ec_typec: Enforce priority-based " Andrei Kuchynski
@ 2026-10-29  4:19   ` Tzung-Bi Shih
  0 siblings, 0 replies; 4+ messages in thread
From: Tzung-Bi Shih @ 2026-10-29  4:19 UTC (permalink / raw)
  To: Andrei Kuchynski
  Cc: Jameson Thies, Benson Leung, Abhishek Pandit-Subedi,
	Guenter Roeck, chrome-platform, linux-kernel

On Wed, Oct 28, 2026 at 09:09:31AM +0000, Andrei Kuchynski wrote:
> The driver sets mode_selection bit for each Alternate mode, thereby
> preventing individual altmode drivers from activating their respective
> modes.
> Once the registration of all Alternate Modes is complete, the driver
> verifies if USB4 mode is supported by both the port and the partner and
> activates USB4 mode. In cases where USB4 is not supported, the driver
> invokes typec_mode_selection_start to initiate the mode selection process
> based on mode priorities.
> The driver communicates the current Type-C mode to the mode selection
> process via typec_altmode_state_update.

Hi Andrei,

I'm less familiar with the details of cros_ec_typec and its friend drivers.
Could you double-check the Sashiko review [1] to see if any of the findings
need to be addressed as they look real?

[1] https://sashiko.dev/#/patchset/20261028090932.27053-1-akuchynski%40chromium.org

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-10-29  4:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-28  9:09 [PATCH v3 0/2] USB Type-C mode selection Andrei Kuchynski
2026-10-28  9:09 ` [PATCH v3 1/2] platform/chrome: cros_ec_typec: Enforce priority-based " Andrei Kuchynski
2026-10-29  4:19   ` Tzung-Bi Shih
2026-10-28  9:09 ` [PATCH v3 2/2] platform/chrome: cros_ec_typec: Reorder altmode registration Andrei Kuchynski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox