[PATCH v1 2/2] usb: typec: ucsi: gaokun: Set orientation before switching to DP
From: Lee Youngjoon
Date: Thu Nov 26 2026 - 02:53:44 EST
The connector status callback can sample the old CC orientation before
the EC delivers the port update that selects DP mode. The mode handler
then configures the mux and asserts HPD with the transmitter still using
the previous orientation. On the MateBook E Go this was observed with a
reverse EC orientation, a normal PHY orientation and failed link training.
Use the orientation switch described by the connector firmware node to
apply the fresh CC value before setting the DP mux. Serialize switch
writes with the existing Type-C status callback, including its sampling
of CC. Keep references to the switch and mux for the lifetime of the
Gaokun device so the EC worker need not dereference a UCSI connector
during port registration or unwind.
If orientation or mux setup fails, withhold HPD high for that update.
Continue to acknowledge the EC frame and deliver HPD low on detach.
A subsequent valid DP update can announce the display again.
Fixes: 1c2b66a7d725 ("usb: ucsi: huawei_gaokun: support mode switching")
Signed-off-by: Lee Youngjoon <getfeus@xxxxxxxxx>
---
drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c | 66 ++++++++++++++++++---
1 file changed, 58 insertions(+), 8 deletions(-)
diff --git a/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
index 9cb7c9bc1..606928662 100644
--- a/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
+++ b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
@@ -88,6 +88,8 @@ struct gaokun_ucsi_port {
struct gaokun_ucsi *ucsi;
struct auxiliary_device *bridge;
+ struct mutex orientation_lock;
+ struct typec_switch *sw;
struct typec_mux *typec_mux;
struct typec_mux_state state;
struct typec_altmode dp_alt;
@@ -185,6 +187,7 @@ static void gaokun_set_orientation(struct
ucsi_connector *con,
enum gaokun_ucsi_ccx ccx;
unsigned long flags;
+ guard(mutex)(&port->orientation_lock);
spin_lock_irqsave(&port->lock, flags);
ccx = port->ccx;
spin_unlock_irqrestore(&port->lock, flags);
@@ -305,9 +308,25 @@ static int gaokun_ucsi_refresh(struct gaokun_ucsi
*uec, int expected)
return idx + 1;
}
+static int gaokun_ucsi_set_dp_orientation(struct gaokun_ucsi_port *port)
+{
+ enum gaokun_ucsi_ccx ccx = port->ccx;
+
+ lockdep_assert_held(&port->ucsi->usb_lock);
+ if (!port->sw)
+ return -ENODEV;
+ if (ccx != USBC_CCX_NORMAL && ccx != USBC_CCX_REVERSE)
+ return -EINVAL;
+
+ /* Finish any connector callback that sampled the previous CC value. */
+ guard(mutex)(&port->orientation_lock);
+ return typec_switch_set(port->sw, CCX_TO_ORI(ccx));
+}
+
static void gaokun_ucsi_handle_usb_mode(struct gaokun_ucsi_port *port)
{
struct gaokun_ucsi *uec = port->ucsi;
+ bool dp = port->bridge && port->svid == USB_TYPEC_DP_SID;
int idx = port->idx, ret;
/*
@@ -323,15 +342,24 @@ static void gaokun_ucsi_handle_usb_mode(struct
gaokun_ucsi_port *port)
return;
}
+ if (dp) {
+ ret = gaokun_ucsi_set_dp_orientation(port);
+ if (ret && port->hpd_state) {
+ dev_err_ratelimited(uec->dev, "failed to set DP orientation: %d\n", ret);
+ goto ack;
+ }
+ }
+
ret = typec_mux_set(port->typec_mux, &port->state);
- if (ret)
+ if (ret) {
dev_err(uec->dev, "failed to set mux %d\n", ret);
+ if (dp && port->hpd_state)
+ goto ack;
+ }
/* A physical detach can clear the DP SVID together with HPD. */
- if (port->bridge &&
- (port->svid == USB_TYPEC_DP_SID ||
- (port->hpd_connected && port->ccx == USBC_CCX_NONE &&
- port->mux == USBC_MUX_NONE && !port->hpd_state))) {
+ if (dp || (port->hpd_connected && port->ccx == USBC_CCX_NONE &&
+ port->mux == USBC_MUX_NONE && !port->hpd_state)) {
port->hpd_connected = port->hpd_state;
drm_aux_hpd_bridge_notify(&port->bridge->dev,
port->hpd_state ?
@@ -339,6 +367,7 @@ static void gaokun_ucsi_handle_usb_mode(struct
gaokun_ucsi_port *port)
connector_status_disconnected);
}
+ack:
gaokun_ec_ucsi_pan_ack(uec->ec, port->idx);
}
@@ -429,6 +458,17 @@ static int gaokun_ucsi_notify(struct notifier_block *nb,
}
}
+static void gaokun_ucsi_put_switches(void *data)
+{
+ struct gaokun_ucsi *uec = data;
+ int i;
+
+ for (i = 0; i < uec->num_ports; i++) {
+ typec_switch_put(uec->ports[i].sw);
+ typec_mux_put(uec->ports[i].typec_mux);
+ }
+}
+
static int gaokun_ucsi_ports_init(struct gaokun_ucsi *uec)
{
struct gaokun_ucsi_port *ucsi_port;
@@ -450,6 +490,10 @@ static int gaokun_ucsi_ports_init(struct gaokun_ucsi *uec)
if (!uec->ports)
return -ENOMEM;
+ ret = devm_add_action_or_reset(dev, gaokun_ucsi_put_switches, uec);
+ if (ret)
+ return ret;
+
for (i = 0; i < num_ports; ++i) {
ucsi_port = &uec->ports[i];
ucsi_port->ccx = USBC_CCX_NONE;
@@ -457,6 +501,7 @@ static int gaokun_ucsi_ports_init(struct gaokun_ucsi *uec)
ucsi_port->ucsi = uec;
INIT_DELAYED_WORK(&ucsi_port->usb_fallback, gaokun_ucsi_usb_fallback);
spin_lock_init(&ucsi_port->lock);
+ mutex_init(&ucsi_port->orientation_lock);
}
device_for_each_child_node(dev, fwnode) {
@@ -479,6 +524,14 @@ static int gaokun_ucsi_ports_init(struct gaokun_ucsi *uec)
return PTR_ERR(ucsi_port->bridge);
}
+ ucsi_port->sw = fwnode_typec_switch_get(fwnode);
+ if (IS_ERR(ucsi_port->sw)) {
+ fwnode_handle_put(fwnode);
+ return dev_err_probe(dev, PTR_ERR(ucsi_port->sw),
+ "failed to get orientation switch for port %u\n",
+ port);
+ }
+
ucsi_port->typec_mux = fwnode_typec_mux_get(fwnode);
if (IS_ERR(ucsi_port->typec_mux)) {
fwnode_handle_put(fwnode);
@@ -576,9 +629,6 @@ static void gaokun_ucsi_remove(struct
auxiliary_device *adev)
if (uec->registered)
ucsi_unregister(uec->ucsi);
- for (i = 0; i < uec->num_ports; ++i)
- typec_mux_put(uec->ports[i].typec_mux);
-
ucsi_destroy(uec->ucsi);
}