linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] usb: typec: tcpci: Check ROLE_CONTROL while interpreting CC_STATUS
@ 2021-03-04  7:09 Badhri Jagan Sridharan
  2021-03-08 11:02 ` Heikki Krogerus
  2021-03-08 18:28 ` Guenter Roeck
  0 siblings, 2 replies; 3+ messages in thread
From: Badhri Jagan Sridharan @ 2021-03-04  7:09 UTC (permalink / raw)
  To: Guenter Roeck, Heikki Krogerus, Greg Kroah-Hartman
  Cc: linux-usb, linux-kernel, Kyle Tso, stable, Badhri Jagan Sridharan

While interpreting CC_STATUS, ROLE_CONTROL has to be read to make
sure that CC1/CC2 is not forced presenting Rp/Rd.

From the TCPCI spec:

4.4.5.2 ROLE_CONTROL (Normative):
The TCPM shall write B6 (DRP) = 0b and B3..0 (CC1/CC2) if it wishes
to control the Rp/Rd directly instead of having the TCPC perform
DRP toggling autonomously. When controlling Rp/Rd directly, the
TCPM writes to B3..0 (CC1/CC2) each time it wishes to change the
CC1/CC2 values. This control is used for TCPM-TCPC implementing
Source or Sink only as well as when a connection has been detected
via DRP toggling but the TCPM wishes to attempt Try.Src or Try.Snk.

Table 4-22. CC_STATUS Register Definition:
If (ROLE_CONTROL.CC1 = Rd) or ConnectResult=1)
00b: SNK.Open (Below maximum vRa)
01b: SNK.Default (Above minimum vRd-Connect)
10b: SNK.Power1.5 (Above minimum vRd-Connect) Detects Rp-1.5A
11b: SNK.Power3.0 (Above minimum vRd-Connect) Detects Rp-3.0A

If (ROLE_CONTROL.CC2=Rd) or (ConnectResult=1)
00b: SNK.Open (Below maximum vRa)
01b: SNK.Default (Above minimum vRd-Connect)
10b: SNK.Power1.5 (Above minimum vRd-Connect) Detects Rp 1.5A
11b: SNK.Power3.0 (Above minimum vRd-Connect) Detects Rp 3.0A

Fixes: 74e656d6b0551 ("staging: typec: Type-C Port Controller
Interface driver (tcpci)")
Signed-off-by: Badhri Jagan Sridharan <badhri@google.com>
---
 drivers/usb/typec/tcpm/tcpci.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/typec/tcpm/tcpci.c b/drivers/usb/typec/tcpm/tcpci.c
index a27deb0b5f03..027afd7dfdce 100644
--- a/drivers/usb/typec/tcpm/tcpci.c
+++ b/drivers/usb/typec/tcpm/tcpci.c
@@ -24,6 +24,15 @@
 #define	AUTO_DISCHARGE_PD_HEADROOM_MV		850
 #define	AUTO_DISCHARGE_PPS_HEADROOM_MV		1250
 
+#define tcpc_presenting_cc1_rd(reg) \
+	(!(TCPC_ROLE_CTRL_DRP & (reg)) && \
+	 (((reg) & (TCPC_ROLE_CTRL_CC1_MASK << TCPC_ROLE_CTRL_CC1_SHIFT)) == \
+	  (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT)))
+#define tcpc_presenting_cc2_rd(reg) \
+	(!(TCPC_ROLE_CTRL_DRP & (reg)) && \
+	 (((reg) & (TCPC_ROLE_CTRL_CC2_MASK << TCPC_ROLE_CTRL_CC2_SHIFT)) == \
+	  (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT)))
+
 struct tcpci {
 	struct device *dev;
 
@@ -178,19 +187,25 @@ static int tcpci_get_cc(struct tcpc_dev *tcpc,
 			enum typec_cc_status *cc1, enum typec_cc_status *cc2)
 {
 	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
-	unsigned int reg;
+	unsigned int reg, role_control;
 	int ret;
 
+	ret = regmap_read(tcpci->regmap, TCPC_ROLE_CTRL, &role_control);
+	if (ret < 0)
+		return ret;
+
 	ret = regmap_read(tcpci->regmap, TCPC_CC_STATUS, &reg);
 	if (ret < 0)
 		return ret;
 
 	*cc1 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC1_SHIFT) &
 				 TCPC_CC_STATUS_CC1_MASK,
-				 reg & TCPC_CC_STATUS_TERM);
+				 reg & TCPC_CC_STATUS_TERM ||
+				 tcpc_presenting_cc1_rd(role_control));
 	*cc2 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC2_SHIFT) &
 				 TCPC_CC_STATUS_CC2_MASK,
-				 reg & TCPC_CC_STATUS_TERM);
+				 reg & TCPC_CC_STATUS_TERM ||
+				 tcpc_presenting_cc2_rd(role_control));
 
 	return 0;
 }
-- 
2.30.1.766.gb4fecdf3b7-goog


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

* Re: [PATCH v1] usb: typec: tcpci: Check ROLE_CONTROL while interpreting CC_STATUS
  2021-03-04  7:09 [PATCH v1] usb: typec: tcpci: Check ROLE_CONTROL while interpreting CC_STATUS Badhri Jagan Sridharan
@ 2021-03-08 11:02 ` Heikki Krogerus
  2021-03-08 18:28 ` Guenter Roeck
  1 sibling, 0 replies; 3+ messages in thread
From: Heikki Krogerus @ 2021-03-08 11:02 UTC (permalink / raw)
  To: Badhri Jagan Sridharan
  Cc: Guenter Roeck, Greg Kroah-Hartman, linux-usb, linux-kernel,
	Kyle Tso, stable

On Wed, Mar 03, 2021 at 11:09:31PM -0800, Badhri Jagan Sridharan wrote:
> While interpreting CC_STATUS, ROLE_CONTROL has to be read to make
> sure that CC1/CC2 is not forced presenting Rp/Rd.
> 
> >From the TCPCI spec:
> 
> 4.4.5.2 ROLE_CONTROL (Normative):
> The TCPM shall write B6 (DRP) = 0b and B3..0 (CC1/CC2) if it wishes
> to control the Rp/Rd directly instead of having the TCPC perform
> DRP toggling autonomously. When controlling Rp/Rd directly, the
> TCPM writes to B3..0 (CC1/CC2) each time it wishes to change the
> CC1/CC2 values. This control is used for TCPM-TCPC implementing
> Source or Sink only as well as when a connection has been detected
> via DRP toggling but the TCPM wishes to attempt Try.Src or Try.Snk.
> 
> Table 4-22. CC_STATUS Register Definition:
> If (ROLE_CONTROL.CC1 = Rd) or ConnectResult=1)
> 00b: SNK.Open (Below maximum vRa)
> 01b: SNK.Default (Above minimum vRd-Connect)
> 10b: SNK.Power1.5 (Above minimum vRd-Connect) Detects Rp-1.5A
> 11b: SNK.Power3.0 (Above minimum vRd-Connect) Detects Rp-3.0A
> 
> If (ROLE_CONTROL.CC2=Rd) or (ConnectResult=1)
> 00b: SNK.Open (Below maximum vRa)
> 01b: SNK.Default (Above minimum vRd-Connect)
> 10b: SNK.Power1.5 (Above minimum vRd-Connect) Detects Rp 1.5A
> 11b: SNK.Power3.0 (Above minimum vRd-Connect) Detects Rp 3.0A
> 
> Fixes: 74e656d6b0551 ("staging: typec: Type-C Port Controller
> Interface driver (tcpci)")
> Signed-off-by: Badhri Jagan Sridharan <badhri@google.com>

Acked-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>

> ---
>  drivers/usb/typec/tcpm/tcpci.c | 21 ++++++++++++++++++---
>  1 file changed, 18 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/usb/typec/tcpm/tcpci.c b/drivers/usb/typec/tcpm/tcpci.c
> index a27deb0b5f03..027afd7dfdce 100644
> --- a/drivers/usb/typec/tcpm/tcpci.c
> +++ b/drivers/usb/typec/tcpm/tcpci.c
> @@ -24,6 +24,15 @@
>  #define	AUTO_DISCHARGE_PD_HEADROOM_MV		850
>  #define	AUTO_DISCHARGE_PPS_HEADROOM_MV		1250
>  
> +#define tcpc_presenting_cc1_rd(reg) \
> +	(!(TCPC_ROLE_CTRL_DRP & (reg)) && \
> +	 (((reg) & (TCPC_ROLE_CTRL_CC1_MASK << TCPC_ROLE_CTRL_CC1_SHIFT)) == \
> +	  (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT)))
> +#define tcpc_presenting_cc2_rd(reg) \
> +	(!(TCPC_ROLE_CTRL_DRP & (reg)) && \
> +	 (((reg) & (TCPC_ROLE_CTRL_CC2_MASK << TCPC_ROLE_CTRL_CC2_SHIFT)) == \
> +	  (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT)))
> +

Couldn't you handle that with a single macro by concatenating the CC
line numbers?

>  struct tcpci {
>  	struct device *dev;
>  
> @@ -178,19 +187,25 @@ static int tcpci_get_cc(struct tcpc_dev *tcpc,
>  			enum typec_cc_status *cc1, enum typec_cc_status *cc2)
>  {
>  	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
> -	unsigned int reg;
> +	unsigned int reg, role_control;
>  	int ret;
>  
> +	ret = regmap_read(tcpci->regmap, TCPC_ROLE_CTRL, &role_control);
> +	if (ret < 0)
> +		return ret;
> +
>  	ret = regmap_read(tcpci->regmap, TCPC_CC_STATUS, &reg);
>  	if (ret < 0)
>  		return ret;
>  
>  	*cc1 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC1_SHIFT) &
>  				 TCPC_CC_STATUS_CC1_MASK,
> -				 reg & TCPC_CC_STATUS_TERM);
> +				 reg & TCPC_CC_STATUS_TERM ||
> +				 tcpc_presenting_cc1_rd(role_control));
>  	*cc2 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC2_SHIFT) &
>  				 TCPC_CC_STATUS_CC2_MASK,
> -				 reg & TCPC_CC_STATUS_TERM);
> +				 reg & TCPC_CC_STATUS_TERM ||
> +				 tcpc_presenting_cc2_rd(role_control));
>  
>  	return 0;
>  }
> -- 
> 2.30.1.766.gb4fecdf3b7-goog

thanks,

-- 
heikki

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

* Re: [PATCH v1] usb: typec: tcpci: Check ROLE_CONTROL while interpreting CC_STATUS
  2021-03-04  7:09 [PATCH v1] usb: typec: tcpci: Check ROLE_CONTROL while interpreting CC_STATUS Badhri Jagan Sridharan
  2021-03-08 11:02 ` Heikki Krogerus
@ 2021-03-08 18:28 ` Guenter Roeck
  1 sibling, 0 replies; 3+ messages in thread
From: Guenter Roeck @ 2021-03-08 18:28 UTC (permalink / raw)
  To: Badhri Jagan Sridharan
  Cc: Heikki Krogerus, Greg Kroah-Hartman, linux-usb, linux-kernel,
	Kyle Tso, stable

On Wed, Mar 03, 2021 at 11:09:31PM -0800, Badhri Jagan Sridharan wrote:
> While interpreting CC_STATUS, ROLE_CONTROL has to be read to make
> sure that CC1/CC2 is not forced presenting Rp/Rd.
> 
> From the TCPCI spec:
> 
> 4.4.5.2 ROLE_CONTROL (Normative):
> The TCPM shall write B6 (DRP) = 0b and B3..0 (CC1/CC2) if it wishes
> to control the Rp/Rd directly instead of having the TCPC perform
> DRP toggling autonomously. When controlling Rp/Rd directly, the
> TCPM writes to B3..0 (CC1/CC2) each time it wishes to change the
> CC1/CC2 values. This control is used for TCPM-TCPC implementing
> Source or Sink only as well as when a connection has been detected
> via DRP toggling but the TCPM wishes to attempt Try.Src or Try.Snk.
> 
> Table 4-22. CC_STATUS Register Definition:
> If (ROLE_CONTROL.CC1 = Rd) or ConnectResult=1)
> 00b: SNK.Open (Below maximum vRa)
> 01b: SNK.Default (Above minimum vRd-Connect)
> 10b: SNK.Power1.5 (Above minimum vRd-Connect) Detects Rp-1.5A
> 11b: SNK.Power3.0 (Above minimum vRd-Connect) Detects Rp-3.0A
> 
> If (ROLE_CONTROL.CC2=Rd) or (ConnectResult=1)
> 00b: SNK.Open (Below maximum vRa)
> 01b: SNK.Default (Above minimum vRd-Connect)
> 10b: SNK.Power1.5 (Above minimum vRd-Connect) Detects Rp 1.5A
> 11b: SNK.Power3.0 (Above minimum vRd-Connect) Detects Rp 3.0A
> 
> Fixes: 74e656d6b0551 ("staging: typec: Type-C Port Controller
> Interface driver (tcpci)")
> Signed-off-by: Badhri Jagan Sridharan <badhri@google.com>

Reviewed-by: Guenter Roeck <linux@roeck-us.net>

> ---
>  drivers/usb/typec/tcpm/tcpci.c | 21 ++++++++++++++++++---
>  1 file changed, 18 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/usb/typec/tcpm/tcpci.c b/drivers/usb/typec/tcpm/tcpci.c
> index a27deb0b5f03..027afd7dfdce 100644
> --- a/drivers/usb/typec/tcpm/tcpci.c
> +++ b/drivers/usb/typec/tcpm/tcpci.c
> @@ -24,6 +24,15 @@
>  #define	AUTO_DISCHARGE_PD_HEADROOM_MV		850
>  #define	AUTO_DISCHARGE_PPS_HEADROOM_MV		1250
>  
> +#define tcpc_presenting_cc1_rd(reg) \
> +	(!(TCPC_ROLE_CTRL_DRP & (reg)) && \
> +	 (((reg) & (TCPC_ROLE_CTRL_CC1_MASK << TCPC_ROLE_CTRL_CC1_SHIFT)) == \
> +	  (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT)))
> +#define tcpc_presenting_cc2_rd(reg) \
> +	(!(TCPC_ROLE_CTRL_DRP & (reg)) && \
> +	 (((reg) & (TCPC_ROLE_CTRL_CC2_MASK << TCPC_ROLE_CTRL_CC2_SHIFT)) == \
> +	  (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT)))
> +
>  struct tcpci {
>  	struct device *dev;
>  
> @@ -178,19 +187,25 @@ static int tcpci_get_cc(struct tcpc_dev *tcpc,
>  			enum typec_cc_status *cc1, enum typec_cc_status *cc2)
>  {
>  	struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
> -	unsigned int reg;
> +	unsigned int reg, role_control;
>  	int ret;
>  
> +	ret = regmap_read(tcpci->regmap, TCPC_ROLE_CTRL, &role_control);
> +	if (ret < 0)
> +		return ret;
> +
>  	ret = regmap_read(tcpci->regmap, TCPC_CC_STATUS, &reg);
>  	if (ret < 0)
>  		return ret;
>  
>  	*cc1 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC1_SHIFT) &
>  				 TCPC_CC_STATUS_CC1_MASK,
> -				 reg & TCPC_CC_STATUS_TERM);
> +				 reg & TCPC_CC_STATUS_TERM ||
> +				 tcpc_presenting_cc1_rd(role_control));
>  	*cc2 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC2_SHIFT) &
>  				 TCPC_CC_STATUS_CC2_MASK,
> -				 reg & TCPC_CC_STATUS_TERM);
> +				 reg & TCPC_CC_STATUS_TERM ||
> +				 tcpc_presenting_cc2_rd(role_control));
>  
>  	return 0;
>  }
> -- 
> 2.30.1.766.gb4fecdf3b7-goog
> 

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

end of thread, other threads:[~2021-03-08 18:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-04  7:09 [PATCH v1] usb: typec: tcpci: Check ROLE_CONTROL while interpreting CC_STATUS Badhri Jagan Sridharan
2021-03-08 11:02 ` Heikki Krogerus
2021-03-08 18:28 ` Guenter Roeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).