Skip to contentSkip to conversation

usb: typec: tcpci: don't touch CC line if it's Vconn source

PATCH Superseded Xu Yang 3 participants 3 messages Jan 12, 2022

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Xu Yang

author
#0 lore raw

PATCH v2 1/1 usb: typec: tcpci: don't touch CC line if it's Vconn source

With the AMS and Collision Avoidance, tcpm often needs to change the CC's termination. When one CC line is souring Vconn, if we still change its termination, the voltage of the another CC line is likely to be fluctuant and unstable.

Therefore, we should verify whether a CC line is soucing Vconn before changing its termination. And only changing the termination that is not a Vconn line. This can be done by reading the VCONN Present bit of POWER_ STATUS register. To determinate the polarity, we can read the Plug Orientation bit of TCPC_CONTROL register. Since only if Plug Orientation is set, Vconn can be sourced.

Fixes 0908c5aca31e ("usb: typec: tcpm: AMS and Collision Avoidance")

---
v2: changed subject line

---
 drivers/usb/typec/tcpm/tcpci.c | 27 +++++++++++++++++++++++++++
 drivers/usb/typec/tcpm/tcpci.h |  1 +
 2 files changed, 28 insertions(+)
drivers/usb/typec/tcpm/tcpci.c +27 -0
index 35a1307349a2..0bf4cbfaa21c 100644
@@ -75,9 +75,26 @@ static int tcpci_write16(struct tcpci *tcpci, unsigned int reg, u16 val)
 static int tcpci_set_cc(struct tcpc_dev *tcpc, enum typec_cc_status cc)
 {
 	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+	bool vconn_pres = false;
+	enum typec_cc_polarity polarity = TYPEC_POLARITY_CC1;
 	unsigned int reg;
 	int ret;
 
+	ret = regmap_read(tcpci->regmap, TCPC_POWER_STATUS, &reg);
+	if (ret < 0)
+		return ret;
+
+	if (reg & TCPC_POWER_STATUS_VCONN_PRES) {
+		vconn_pres = true;
+
+		ret = regmap_read(tcpci->regmap, TCPC_TCPC_CTRL, &reg);
+		if (ret < 0)
+			return ret;
+
+		if (reg & TCPC_TCPC_CTRL_ORIENTATION)
+			polarity = TYPEC_POLARITY_CC2;
+	}
+
 	switch (cc) {
 	case TYPEC_CC_RA:
 		reg = (TCPC_ROLE_CTRL_CC_RA << TCPC_ROLE_CTRL_CC1_SHIFT) |
@@ -112,6 +129,16 @@ static int tcpci_set_cc(struct tcpc_dev *tcpc, enum typec_cc_status cc)
 		break;
 	}
 
+	if (vconn_pres) {
+		if (polarity == TYPEC_POLARITY_CC2) {
+			reg &= ~(TCPC_ROLE_CTRL_CC1_MASK << TCPC_ROLE_CTRL_CC1_SHIFT);
+			reg |= (TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC1_SHIFT);
+		} else {
+			reg &= ~(TCPC_ROLE_CTRL_CC2_MASK << TCPC_ROLE_CTRL_CC2_SHIFT);
+			reg |= (TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC2_SHIFT);
+		}
+	}
+
 	ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg);
 	if (ret < 0)
 		return ret;
drivers/usb/typec/tcpm/tcpci.h +1 -0
index 2be7a77d400e..b2edd45f13c6 100644
@@ -98,6 +98,7 @@
 #define TCPC_POWER_STATUS_SOURCING_VBUS	BIT(4)
 #define TCPC_POWER_STATUS_VBUS_DET	BIT(3)
 #define TCPC_POWER_STATUS_VBUS_PRES	BIT(2)
+#define TCPC_POWER_STATUS_VCONN_PRES	BIT(1)
 #define TCPC_POWER_STATUS_SINKING_VBUS	BIT(0)
 
 #define TCPC_FAULT_STATUS		0x1f
Signature · 1 line
2.25.1
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 12 lines · Xu Yang

With the AMS and Collision Avoidance, tcpm often needs to change the CC's termination. When one CC line is souring Vconn, if we still change its termination, the voltage of the another CC line is likely to be fluctuant and unstable.

Therefore, we should verify whether a CC line is soucing Vconn before changing its termination. And only changing the termination that is not a Vconn line. This can be done by reading the VCONN Present bit of POWER_ STATUS register. To determinate the polarity, we can read the Plug Orientation bit of TCPC_CONTROL register. Since only if Plug Orientation is set, Vconn can be sourced.

Quoted 3 lines
Fixes 0908c5aca31e ("usb: typec: tcpm: AMS and Collision Avoidance")
Quoted 69 lines

---
v2: changed subject line

---
 drivers/usb/typec/tcpm/tcpci.c | 27 +++++++++++++++++++++++++++
 drivers/usb/typec/tcpm/tcpci.h |  1 +
 2 files changed, 28 insertions(+)
drivers/usb/typec/tcpm/tcpci.c +27 -0
index 35a1307349a2..0bf4cbfaa21c 100644
@@ -75,9 +75,26 @@ static int tcpci_write16(struct tcpci *tcpci, unsigned int reg, u16 val)
 static int tcpci_set_cc(struct tcpc_dev *tcpc, enum typec_cc_status cc)
 {
 	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+	bool vconn_pres = false;
+	enum typec_cc_polarity polarity = TYPEC_POLARITY_CC1;
 	unsigned int reg;
 	int ret;
 
+	ret = regmap_read(tcpci->regmap, TCPC_POWER_STATUS, &reg);
+	if (ret < 0)
+		return ret;
+
+	if (reg & TCPC_POWER_STATUS_VCONN_PRES) {
+		vconn_pres = true;
+
+		ret = regmap_read(tcpci->regmap, TCPC_TCPC_CTRL, &reg);
+		if (ret < 0)
+			return ret;
+
+		if (reg & TCPC_TCPC_CTRL_ORIENTATION)
+			polarity = TYPEC_POLARITY_CC2;
+	}
+
 	switch (cc) {
 	case TYPEC_CC_RA:
 		reg = (TCPC_ROLE_CTRL_CC_RA << TCPC_ROLE_CTRL_CC1_SHIFT) |
@@ -112,6 +129,16 @@ static int tcpci_set_cc(struct tcpc_dev *tcpc, enum typec_cc_status cc)
 		break;
 	}
 
+	if (vconn_pres) {
+		if (polarity == TYPEC_POLARITY_CC2) {
+			reg &= ~(TCPC_ROLE_CTRL_CC1_MASK << TCPC_ROLE_CTRL_CC1_SHIFT);
+			reg |= (TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC1_SHIFT);
+		} else {
+			reg &= ~(TCPC_ROLE_CTRL_CC2_MASK << TCPC_ROLE_CTRL_CC2_SHIFT);
+			reg |= (TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC2_SHIFT);
+		}
+	}
+
 	ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg);
 	if (ret < 0)
 		return ret;
drivers/usb/typec/tcpm/tcpci.h +1 -0
index 2be7a77d400e..b2edd45f13c6 100644
@@ -98,6 +98,7 @@
 #define TCPC_POWER_STATUS_SOURCING_VBUS	BIT(4)
 #define TCPC_POWER_STATUS_VBUS_DET	BIT(3)
 #define TCPC_POWER_STATUS_VBUS_PRES	BIT(2)
+#define TCPC_POWER_STATUS_VCONN_PRES	BIT(1)
 #define TCPC_POWER_STATUS_SINKING_VBUS	BIT(0)
 
 #define TCPC_FAULT_STATUS		0x1f
Signature · 1 line
2.25.1
Signature · 1 line
heikki
#2 lore raw
In reply to Xu Yang
Quoted 2 lines · Xu Yang

With the AMS and Collision Avoidance, tcpm often needs to change the CC's termination. When one CC line is souring Vconn, if we still change its

s/souring/sourcing/

Quoted 4 lines

termination, the voltage of the another CC line is likely to be fluctuant and unstable.

Therefore, we should verify whether a CC line is soucing Vconn before

s/soucing/sourcing/

Quoted 2 lines

changing its termination. And only changing the termination that is not a Vconn line. This can be done by reading the VCONN Present bit of

... and only change ...

maybe to make it a full sentence.

Quoted 1 line

POWER_ STATUS register. To determinate the polarity, we can read the

determine

Quoted 2 lines

Plug Orientation bit of TCPC_CONTROL register. Since only if Plug Orientation is set, Vconn can be sourced.

Maybe something like

Vconn can only be be sourced if Plug Orientation is set.

Quoted 21 lines
Fixes 0908c5aca31e ("usb: typec: tcpm: AMS and Collision Avoidance")

---
v2: changed subject line

---
 drivers/usb/typec/tcpm/tcpci.c | 27 +++++++++++++++++++++++++++
 drivers/usb/typec/tcpm/tcpci.h |  1 +
 2 files changed, 28 insertions(+)
drivers/usb/typec/tcpm/tcpci.c +1 -0
index 35a1307349a2..0bf4cbfaa21c 100644
@@ -75,9 +75,26 @@ static int tcpci_write16(struct tcpci *tcpci, unsigned int reg, u16 val)
 static int tcpci_set_cc(struct tcpc_dev *tcpc, enum typec_cc_status cc)
 {
 	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
+	bool vconn_pres = false;

drivers/usb/typec/tcpm/tcpci.c static int tcpci_write16(struct tcpci *tcpci, unsigned int reg, u16 val) · line 88

Quoted 10 lines
drivers/usb/typec/tcpm/tcpci.c +7 -0
+	enum typec_cc_polarity polarity = TYPEC_POLARITY_CC1;
 	unsigned int reg;
 	int ret;
 
+	ret = regmap_read(tcpci->regmap, TCPC_POWER_STATUS, &reg);
+	if (ret < 0)
+		return ret;
+
+	if (reg & TCPC_POWER_STATUS_VCONN_PRES) {
+		vconn_pres = true;

How about

	vconn_pres = !!(reg & TCPC_POWER_STATUS_VCONN_PRES);

	if (vconn_pres) {

...

Quoted 44 lines
drivers/usb/typec/tcpm/tcpci.c +19 -0
+
+		ret = regmap_read(tcpci->regmap, TCPC_TCPC_CTRL, &reg);
+		if (ret < 0)
+			return ret;
+
+		if (reg & TCPC_TCPC_CTRL_ORIENTATION)
+			polarity = TYPEC_POLARITY_CC2;
+	}
+
 	switch (cc) {
 	case TYPEC_CC_RA:
 		reg = (TCPC_ROLE_CTRL_CC_RA << TCPC_ROLE_CTRL_CC1_SHIFT) |
@@ -112,6 +129,16 @@ static int tcpci_set_cc(struct tcpc_dev *tcpc, enum typec_cc_status cc)
 		break;
 	}
 
+	if (vconn_pres) {
+		if (polarity == TYPEC_POLARITY_CC2) {
+			reg &= ~(TCPC_ROLE_CTRL_CC1_MASK << TCPC_ROLE_CTRL_CC1_SHIFT);
+			reg |= (TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC1_SHIFT);
+		} else {
+			reg &= ~(TCPC_ROLE_CTRL_CC2_MASK << TCPC_ROLE_CTRL_CC2_SHIFT);
+			reg |= (TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC2_SHIFT);
+		}
+	}
+
 	ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg);
 	if (ret < 0)
 		return ret;
drivers/usb/typec/tcpm/tcpci.h +1 -0
index 2be7a77d400e..b2edd45f13c6 100644
@@ -98,6 +98,7 @@
 #define TCPC_POWER_STATUS_SOURCING_VBUS	BIT(4)
 #define TCPC_POWER_STATUS_VBUS_DET	BIT(3)
 #define TCPC_POWER_STATUS_VBUS_PRES	BIT(2)
+#define TCPC_POWER_STATUS_VCONN_PRES	BIT(1)
 #define TCPC_POWER_STATUS_SINKING_VBUS	BIT(0)
 
 #define TCPC_FAULT_STATUS		0x1f
Signature · 1 line
2.25.1