linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] usb: typec: tcpm: Wait for vbus discharge to VSAFE0V before toggling
@ 2021-02-18 10:02 Badhri Jagan Sridharan
  2021-02-18 10:09 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 5+ messages in thread
From: Badhri Jagan Sridharan @ 2021-02-18 10:02 UTC (permalink / raw)
  To: Guenter Roeck, Heikki Krogerus, Greg Kroah-Hartman, Kyle Tso
  Cc: linux-usb, linux-kernel, Badhri Jagan Sridharan

When vbus auto discharge is enabled, TCPM can sometimes be faster than
the TCPC i.e. TCPM can go ahead and move the port to unattached state
(involves disabling vbus auto discharge) before TCPC could effectively
discharge vbus to VSAFE0V. This leaves vbus with residual charge and
increases the decay time which prevents tsafe0v from being met.
This change introduces a new state VBUS_DISCHARGE where the TCPM waits
for a maximum of tSafe0V(max) for vbus to discharge to VSAFE0V before
transitioning to unattached state and re-enable toggling. If vbus
discharges to vsafe0v sooner, then, transition to unattached state
happens right away.

Also, while in SNK_READY, when auto discharge is enabled, drive
disconnect based on vbus turning off instead of Rp disappearing on
CC pins. Rp disappearing on CC pins is almost instanteous compared
to vbus decay.

Signed-off-by: Badhri Jagan Sridharan <badhri@google.com>
---
 drivers/usb/typec/tcpm/tcpm.c | 60 +++++++++++++++++++++++++++++++----
 1 file changed, 53 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index be0b6469dd3d..0ed71725980f 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -62,6 +62,8 @@
 	S(SNK_TRANSITION_SINK_VBUS),		\
 	S(SNK_READY),				\
 						\
+	S(VBUS_DISCHARGE),			\
+						\
 	S(ACC_UNATTACHED),			\
 	S(DEBUG_ACC_ATTACHED),			\
 	S(AUDIO_ACC_ATTACHED),			\
@@ -438,6 +440,9 @@ struct tcpm_port {
 	enum tcpm_ams next_ams;
 	bool in_ams;
 
+	/* Auto vbus discharge state */
+	bool auto_vbus_discharge_enabled;
+
 #ifdef CONFIG_DEBUG_FS
 	struct dentry *dentry;
 	struct mutex logbuffer_lock;	/* log buffer access lock */
@@ -3413,6 +3418,8 @@ static int tcpm_src_attach(struct tcpm_port *port)
 	if (port->tcpc->enable_auto_vbus_discharge) {
 		ret = port->tcpc->enable_auto_vbus_discharge(port->tcpc, true);
 		tcpm_log_force(port, "enable vbus discharge ret:%d", ret);
+		if (!ret)
+			port->auto_vbus_discharge_enabled = true;
 	}
 
 	ret = tcpm_set_roles(port, true, TYPEC_SOURCE, tcpm_data_role_for_source(port));
@@ -3495,6 +3502,8 @@ static void tcpm_reset_port(struct tcpm_port *port)
 	if (port->tcpc->enable_auto_vbus_discharge) {
 		ret = port->tcpc->enable_auto_vbus_discharge(port->tcpc, false);
 		tcpm_log_force(port, "Disable vbus discharge ret:%d", ret);
+		if (!ret)
+			port->auto_vbus_discharge_enabled = false;
 	}
 	port->in_ams = false;
 	port->ams = NONE_AMS;
@@ -3568,6 +3577,8 @@ static int tcpm_snk_attach(struct tcpm_port *port)
 		tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V);
 		ret = port->tcpc->enable_auto_vbus_discharge(port->tcpc, true);
 		tcpm_log_force(port, "enable vbus discharge ret:%d", ret);
+		if (!ret)
+			port->auto_vbus_discharge_enabled = true;
 	}
 
 	ret = tcpm_set_roles(port, true, TYPEC_SINK, tcpm_data_role_for_sink(port));
@@ -3684,6 +3695,12 @@ static void run_state_machine(struct tcpm_port *port)
 	switch (port->state) {
 	case TOGGLING:
 		break;
+	case VBUS_DISCHARGE:
+		if (port->port_type == TYPEC_PORT_SRC)
+			tcpm_set_state(port, SRC_UNATTACHED, PD_T_SAFE_0V);
+		else
+			tcpm_set_state(port, SNK_UNATTACHED, PD_T_SAFE_0V);
+		break;
 	/* SRC states */
 	case SRC_UNATTACHED:
 		if (!port->non_pd_role_swap)
@@ -4669,7 +4686,9 @@ static void _tcpm_cc_change(struct tcpm_port *port, enum typec_cc_status cc1,
 	case SRC_READY:
 		if (tcpm_port_is_disconnected(port) ||
 		    !tcpm_port_is_source(port)) {
-			if (port->port_type == TYPEC_PORT_SRC)
+			if (port->auto_vbus_discharge_enabled && !port->vbus_vsafe0v)
+				tcpm_set_state(port, VBUS_DISCHARGE, 0);
+			else if (port->port_type == TYPEC_PORT_SRC)
 				tcpm_set_state(port, SRC_UNATTACHED, 0);
 			else
 				tcpm_set_state(port, SNK_UNATTACHED, 0);
@@ -4703,7 +4722,18 @@ static void _tcpm_cc_change(struct tcpm_port *port, enum typec_cc_status cc1,
 			tcpm_set_state(port, SNK_DEBOUNCED, 0);
 		break;
 	case SNK_READY:
-		if (tcpm_port_is_disconnected(port))
+		/*
+		 * When set_auto_vbus_discharge_threshold is enabled, CC pins go
+		 * away before vbus decays to disconnect threshold. Allow
+		 * disconnect to be driven by vbus disconnect when auto vbus
+		 * discharge is enabled.
+		 *
+		 * EXIT condition is based primarily on vbus disconnect and CC is secondary.
+		 * "A port that has entered into USB PD communications with the Source and
+		 * has seen the CC voltage exceed vRd-USB may monitor the CC pin to detect
+		 * cable disconnect in addition to monitoring VBUS.
+		 */
+		if (!port->auto_vbus_discharge_enabled && tcpm_port_is_disconnected(port))
 			tcpm_set_state(port, unattached_state(port), 0);
 		else if (!port->pd_capable &&
 			 (cc1 != old_cc1 || cc2 != old_cc2))
@@ -4803,9 +4833,16 @@ static void _tcpm_cc_change(struct tcpm_port *port, enum typec_cc_status cc1,
 		 */
 		break;
 
+	case VBUS_DISCHARGE:
+		/* Do nothing. Waiting for vsafe0v signal */
+		break;
 	default:
-		if (tcpm_port_is_disconnected(port))
-			tcpm_set_state(port, unattached_state(port), 0);
+		if (tcpm_port_is_disconnected(port)) {
+			if (port->auto_vbus_discharge_enabled && !port->vbus_vsafe0v)
+				tcpm_set_state(port, VBUS_DISCHARGE, 0);
+			else
+				tcpm_set_state(port, unattached_state(port), 0);
+		}
 		break;
 	}
 }
@@ -4988,9 +5025,12 @@ static void _tcpm_pd_vbus_off(struct tcpm_port *port)
 		break;
 
 	default:
-		if (port->pwr_role == TYPEC_SINK &&
-		    port->attached)
-			tcpm_set_state(port, SNK_UNATTACHED, 0);
+		if (port->pwr_role == TYPEC_SINK && port->attached) {
+			if (port->auto_vbus_discharge_enabled && !port->vbus_vsafe0v)
+				tcpm_set_state(port, VBUS_DISCHARGE, 0);
+			else
+				tcpm_set_state(port, SNK_UNATTACHED, 0);
+		}
 		break;
 	}
 }
@@ -5012,6 +5052,12 @@ static void _tcpm_pd_vbus_vsafe0v(struct tcpm_port *port)
 			tcpm_set_state(port, tcpm_try_snk(port) ? SNK_TRY : SRC_ATTACHED,
 				       PD_T_CC_DEBOUNCE);
 		break;
+	case VBUS_DISCHARGE:
+		if (port->port_type == TYPEC_PORT_SRC)
+			tcpm_set_state(port, SRC_UNATTACHED, 0);
+		else
+			tcpm_set_state(port, SNK_UNATTACHED, 0);
+		break;
 	default:
 		break;
 	}
-- 
2.30.0.617.g56c4b15f3c-goog


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

* Re: [PATCH v1] usb: typec: tcpm: Wait for vbus discharge to VSAFE0V before toggling
  2021-02-18 10:02 [PATCH v1] usb: typec: tcpm: Wait for vbus discharge to VSAFE0V before toggling Badhri Jagan Sridharan
@ 2021-02-18 10:09 ` Greg Kroah-Hartman
  2021-02-18 10:38   ` Badhri Jagan Sridharan
  0 siblings, 1 reply; 5+ messages in thread
From: Greg Kroah-Hartman @ 2021-02-18 10:09 UTC (permalink / raw)
  To: Badhri Jagan Sridharan
  Cc: Guenter Roeck, Heikki Krogerus, Kyle Tso, linux-usb, linux-kernel

On Thu, Feb 18, 2021 at 02:02:43AM -0800, Badhri Jagan Sridharan wrote:
> When vbus auto discharge is enabled, TCPM can sometimes be faster than
> the TCPC i.e. TCPM can go ahead and move the port to unattached state
> (involves disabling vbus auto discharge) before TCPC could effectively
> discharge vbus to VSAFE0V. This leaves vbus with residual charge and
> increases the decay time which prevents tsafe0v from being met.
> This change introduces a new state VBUS_DISCHARGE where the TCPM waits
> for a maximum of tSafe0V(max) for vbus to discharge to VSAFE0V before
> transitioning to unattached state and re-enable toggling. If vbus
> discharges to vsafe0v sooner, then, transition to unattached state
> happens right away.
> 
> Also, while in SNK_READY, when auto discharge is enabled, drive
> disconnect based on vbus turning off instead of Rp disappearing on
> CC pins. Rp disappearing on CC pins is almost instanteous compared
> to vbus decay.
> 
> Signed-off-by: Badhri Jagan Sridharan <badhri@google.com>
> ---
>  drivers/usb/typec/tcpm/tcpm.c | 60 +++++++++++++++++++++++++++++++----
>  1 file changed, 53 insertions(+), 7 deletions(-)

As this seems to be a bugfix, what commit does it fix?  Should it go to
stable kernels?  If so, how far back?

And as this is the merge window, I can't do anything with this until
5.12-rc1 is out, so be prepared for the delay...

thanks,

greg k-h

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

* Re: [PATCH v1] usb: typec: tcpm: Wait for vbus discharge to VSAFE0V before toggling
  2021-02-18 10:09 ` Greg Kroah-Hartman
@ 2021-02-18 10:38   ` Badhri Jagan Sridharan
  2021-02-18 10:42     ` Greg Kroah-Hartman
  0 siblings, 1 reply; 5+ messages in thread
From: Badhri Jagan Sridharan @ 2021-02-18 10:38 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: Guenter Roeck, Heikki Krogerus, Kyle Tso, USB, LKML

Hi Greg,

This patch is a bug fix for the following patch which was introduced in 5.11.

commit f321a02caebdd0c56e167610cda2fa148cd96e8b
Author: Badhri Jagan Sridharan <badhri@google.com>
Date:   Wed Oct 28 23:31:35 2020 -0700

    usb: typec: tcpm: Implement enabling Auto Discharge disconnect support

    TCPCI spec allows TCPC hardware to autonomously discharge the vbus
    capacitance upon disconnect. The expectation is that the TCPM enables
    AutoDischargeDisconnect while entering SNK/SRC_ATTACHED states. Hardware
    then automously discharges vbus when the vbus falls below a certain
    threshold i.e. VBUS_SINK_DISCONNECT_THRESHOLD.

    Apart from enabling the vbus discharge circuit, AutoDischargeDisconnect
    is also used a flag to move TCPCI based TCPC implementations into
    Attached.Snk/Attached.Src state as mentioned in
    Figure 4-15. TCPC State Diagram before a Connection of the
    USB Type-C Port Controller Interface Specification.
    In such TCPC implementations, setting AutoDischargeDisconnect would
    prevent TCPC into entering "Connection_Invalid" state as well.

    Signed-off-by: Badhri Jagan Sridharan <badhri@google.com>
    Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
    Link: https://lore.kernel.org/r/20201029063138.1429760-8-badhri@google.com
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Thanks,
Badhri

On Thu, Feb 18, 2021 at 2:10 AM Greg Kroah-Hartman
<gregkh@linuxfoundation.org> wrote:
>
> On Thu, Feb 18, 2021 at 02:02:43AM -0800, Badhri Jagan Sridharan wrote:
> > When vbus auto discharge is enabled, TCPM can sometimes be faster than
> > the TCPC i.e. TCPM can go ahead and move the port to unattached state
> > (involves disabling vbus auto discharge) before TCPC could effectively
> > discharge vbus to VSAFE0V. This leaves vbus with residual charge and
> > increases the decay time which prevents tsafe0v from being met.
> > This change introduces a new state VBUS_DISCHARGE where the TCPM waits
> > for a maximum of tSafe0V(max) for vbus to discharge to VSAFE0V before
> > transitioning to unattached state and re-enable toggling. If vbus
> > discharges to vsafe0v sooner, then, transition to unattached state
> > happens right away.
> >
> > Also, while in SNK_READY, when auto discharge is enabled, drive
> > disconnect based on vbus turning off instead of Rp disappearing on
> > CC pins. Rp disappearing on CC pins is almost instanteous compared
> > to vbus decay.
> >
> > Signed-off-by: Badhri Jagan Sridharan <badhri@google.com>
> > ---
> >  drivers/usb/typec/tcpm/tcpm.c | 60 +++++++++++++++++++++++++++++++----
> >  1 file changed, 53 insertions(+), 7 deletions(-)
>
> As this seems to be a bugfix, what commit does it fix?  Should it go to
> stable kernels?  If so, how far back?
>
> And as this is the merge window, I can't do anything with this until
> 5.12-rc1 is out, so be prepared for the delay...
>
> thanks,
>
> greg k-h

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

* Re: [PATCH v1] usb: typec: tcpm: Wait for vbus discharge to VSAFE0V before toggling
  2021-02-18 10:38   ` Badhri Jagan Sridharan
@ 2021-02-18 10:42     ` Greg Kroah-Hartman
  2021-02-19  9:06       ` Badhri Jagan Sridharan
  0 siblings, 1 reply; 5+ messages in thread
From: Greg Kroah-Hartman @ 2021-02-18 10:42 UTC (permalink / raw)
  To: Badhri Jagan Sridharan
  Cc: Guenter Roeck, Heikki Krogerus, Kyle Tso, USB, LKML

On Thu, Feb 18, 2021 at 02:38:45AM -0800, Badhri Jagan Sridharan wrote:
> Hi Greg,
> 
> This patch is a bug fix for the following patch which was introduced in 5.11.
> 
> commit f321a02caebdd0c56e167610cda2fa148cd96e8b
> Author: Badhri Jagan Sridharan <badhri@google.com>
> Date:   Wed Oct 28 23:31:35 2020 -0700
> 
>     usb: typec: tcpm: Implement enabling Auto Discharge disconnect support
> 
>     TCPCI spec allows TCPC hardware to autonomously discharge the vbus
>     capacitance upon disconnect. The expectation is that the TCPM enables
>     AutoDischargeDisconnect while entering SNK/SRC_ATTACHED states. Hardware
>     then automously discharges vbus when the vbus falls below a certain
>     threshold i.e. VBUS_SINK_DISCONNECT_THRESHOLD.
> 
>     Apart from enabling the vbus discharge circuit, AutoDischargeDisconnect
>     is also used a flag to move TCPCI based TCPC implementations into
>     Attached.Snk/Attached.Src state as mentioned in
>     Figure 4-15. TCPC State Diagram before a Connection of the
>     USB Type-C Port Controller Interface Specification.
>     In such TCPC implementations, setting AutoDischargeDisconnect would
>     prevent TCPC into entering "Connection_Invalid" state as well.
> 
>     Signed-off-by: Badhri Jagan Sridharan <badhri@google.com>
>     Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
>     Link: https://lore.kernel.org/r/20201029063138.1429760-8-badhri@google.com
>     Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Great, then can you resend the patch and add a proper Fixes: tag, along
with a cc: stable as well?

thanks,

greg k-h

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

* Re: [PATCH v1] usb: typec: tcpm: Wait for vbus discharge to VSAFE0V before toggling
  2021-02-18 10:42     ` Greg Kroah-Hartman
@ 2021-02-19  9:06       ` Badhri Jagan Sridharan
  0 siblings, 0 replies; 5+ messages in thread
From: Badhri Jagan Sridharan @ 2021-02-19  9:06 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: Guenter Roeck, Heikki Krogerus, Kyle Tso, USB, LKML

Done. Just sent out the following patch and CCed stable@vger.kernel.org as well.

[PATCH v2] usb: typec: tcpm: Wait for vbus discharge to VSAFE0V before toggling

Thanks,
Badhri


On Thu, Feb 18, 2021 at 2:42 AM Greg Kroah-Hartman
<gregkh@linuxfoundation.org> wrote:
>
> On Thu, Feb 18, 2021 at 02:38:45AM -0800, Badhri Jagan Sridharan wrote:
> > Hi Greg,
> >
> > This patch is a bug fix for the following patch which was introduced in 5.11.
> >
> > commit f321a02caebdd0c56e167610cda2fa148cd96e8b
> > Author: Badhri Jagan Sridharan <badhri@google.com>
> > Date:   Wed Oct 28 23:31:35 2020 -0700
> >
> >     usb: typec: tcpm: Implement enabling Auto Discharge disconnect support
> >
> >     TCPCI spec allows TCPC hardware to autonomously discharge the vbus
> >     capacitance upon disconnect. The expectation is that the TCPM enables
> >     AutoDischargeDisconnect while entering SNK/SRC_ATTACHED states. Hardware
> >     then automously discharges vbus when the vbus falls below a certain
> >     threshold i.e. VBUS_SINK_DISCONNECT_THRESHOLD.
> >
> >     Apart from enabling the vbus discharge circuit, AutoDischargeDisconnect
> >     is also used a flag to move TCPCI based TCPC implementations into
> >     Attached.Snk/Attached.Src state as mentioned in
> >     Figure 4-15. TCPC State Diagram before a Connection of the
> >     USB Type-C Port Controller Interface Specification.
> >     In such TCPC implementations, setting AutoDischargeDisconnect would
> >     prevent TCPC into entering "Connection_Invalid" state as well.
> >
> >     Signed-off-by: Badhri Jagan Sridharan <badhri@google.com>
> >     Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> >     Link: https://lore.kernel.org/r/20201029063138.1429760-8-badhri@google.com
> >     Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>
> Great, then can you resend the patch and add a proper Fixes: tag, along
> with a cc: stable as well?
>
> thanks,
>
> greg k-h

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

end of thread, other threads:[~2021-02-19  9:13 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-18 10:02 [PATCH v1] usb: typec: tcpm: Wait for vbus discharge to VSAFE0V before toggling Badhri Jagan Sridharan
2021-02-18 10:09 ` Greg Kroah-Hartman
2021-02-18 10:38   ` Badhri Jagan Sridharan
2021-02-18 10:42     ` Greg Kroah-Hartman
2021-02-19  9:06       ` Badhri Jagan Sridharan

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).