All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] usb: typec: tcpm: Relax disconnect threshold during power negotiation
@ 2021-06-16  9:01 Kyle Tso
  2021-06-16 13:42 ` Heikki Krogerus
  0 siblings, 1 reply; 3+ messages in thread
From: Kyle Tso @ 2021-06-16  9:01 UTC (permalink / raw)
  To: linux, heikki.krogerus, gregkh; +Cc: badhri, linux-usb, linux-kernel, Kyle Tso

If the voltage is being decreased in power negotiation, the Source will
set the power supply to operate at the new voltage level before sending
PS_RDY. Relax the threshold before sending Request Message so that it
will not race with Source which begins to adjust the voltage right after
it sends Accept Message (PPS) or tSrcTransition (25~35ms) after it sends
Accept Message (non-PPS).

The real threshold will be set after Sink receives PS_RDY Message.

Fixes: f321a02caebd ("usb: typec: tcpm: Implement enabling Auto Discharge disconnect support")
Cc: Badhri Jagan Sridharan <badhri@google.com>
Signed-off-by: Kyle Tso <kyletso@google.com>
---
Changes in v3:
- move the timing of setting threshold for Fixed RDO as I did for PPS in
  v2, i.e. move it to tcpm_pd_send_request.
- add Cc: tag for Badhri
- update the commit message for the above changes

Changes in v2:
- move the timing of setting threshold up to "before sending Request"
  for PPS power negotiation so that it won't race with the Source.
- PPS: if it fails to send the Request, fallback to previous threshold
- PPS: if the Source doesn't respond Accept, fallback to previous
  threshold
- update the commit message for above changes

 drivers/usb/typec/tcpm/tcpm.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index 197556038ba4..b1d310ab84c4 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -2604,6 +2604,11 @@ static void tcpm_pd_ctrl_request(struct tcpm_port *port,
 			} else {
 				next_state = SNK_WAIT_CAPABILITIES;
 			}
+
+			/* Threshold was relaxed before sending Request. Restore it back. */
+			tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
+							       port->pps_data.active,
+							       port->supply_voltage);
 			tcpm_set_state(port, next_state, 0);
 			break;
 		case SNK_NEGOTIATE_PPS_CAPABILITIES:
@@ -2617,6 +2622,11 @@ static void tcpm_pd_ctrl_request(struct tcpm_port *port,
 			    port->send_discover)
 				port->vdm_sm_running = true;
 
+			/* Threshold was relaxed before sending Request. Restore it back. */
+			tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
+							       port->pps_data.active,
+							       port->supply_voltage);
+
 			tcpm_set_state(port, SNK_READY, 0);
 			break;
 		case DR_SWAP_SEND:
@@ -3336,6 +3346,12 @@ static int tcpm_pd_send_request(struct tcpm_port *port)
 	if (ret < 0)
 		return ret;
 
+	/*
+	 * Relax the threshold as voltage will be adjusted after Accept Message plus tSrcTransition.
+	 * It is safer to modify the threshold here.
+	 */
+	tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
+
 	memset(&msg, 0, sizeof(msg));
 	msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
 				  port->pwr_role,
@@ -3433,6 +3449,9 @@ static int tcpm_pd_send_pps_request(struct tcpm_port *port)
 	if (ret < 0)
 		return ret;
 
+	/* Relax the threshold as voltage will be adjusted right after Accept Message. */
+	tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
+
 	memset(&msg, 0, sizeof(msg));
 	msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
 				  port->pwr_role,
@@ -4196,6 +4215,10 @@ static void run_state_machine(struct tcpm_port *port)
 		port->hard_reset_count = 0;
 		ret = tcpm_pd_send_request(port);
 		if (ret < 0) {
+			/* Restore back to the original state */
+			tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
+							       port->pps_data.active,
+							       port->supply_voltage);
 			/* Let the Source send capabilities again. */
 			tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
 		} else {
@@ -4206,6 +4229,10 @@ static void run_state_machine(struct tcpm_port *port)
 	case SNK_NEGOTIATE_PPS_CAPABILITIES:
 		ret = tcpm_pd_send_pps_request(port);
 		if (ret < 0) {
+			/* Restore back to the original state */
+			tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
+							       port->pps_data.active,
+							       port->supply_voltage);
 			port->pps_status = ret;
 			/*
 			 * If this was called due to updates to sink
-- 
2.32.0.272.g935e593368-goog


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

* Re: [PATCH v3] usb: typec: tcpm: Relax disconnect threshold during power negotiation
  2021-06-16  9:01 [PATCH v3] usb: typec: tcpm: Relax disconnect threshold during power negotiation Kyle Tso
@ 2021-06-16 13:42 ` Heikki Krogerus
  2021-06-17  1:50   ` Badhri Jagan Sridharan
  0 siblings, 1 reply; 3+ messages in thread
From: Heikki Krogerus @ 2021-06-16 13:42 UTC (permalink / raw)
  To: Kyle Tso; +Cc: linux, gregkh, badhri, linux-usb, linux-kernel

On Wed, Jun 16, 2021 at 05:01:02PM +0800, Kyle Tso wrote:
> If the voltage is being decreased in power negotiation, the Source will
> set the power supply to operate at the new voltage level before sending
> PS_RDY. Relax the threshold before sending Request Message so that it
> will not race with Source which begins to adjust the voltage right after
> it sends Accept Message (PPS) or tSrcTransition (25~35ms) after it sends
> Accept Message (non-PPS).
> 
> The real threshold will be set after Sink receives PS_RDY Message.
> 
> Fixes: f321a02caebd ("usb: typec: tcpm: Implement enabling Auto Discharge disconnect support")
> Cc: Badhri Jagan Sridharan <badhri@google.com>
> Signed-off-by: Kyle Tso <kyletso@google.com>

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

> ---
> Changes in v3:
> - move the timing of setting threshold for Fixed RDO as I did for PPS in
>   v2, i.e. move it to tcpm_pd_send_request.
> - add Cc: tag for Badhri
> - update the commit message for the above changes
> 
> Changes in v2:
> - move the timing of setting threshold up to "before sending Request"
>   for PPS power negotiation so that it won't race with the Source.
> - PPS: if it fails to send the Request, fallback to previous threshold
> - PPS: if the Source doesn't respond Accept, fallback to previous
>   threshold
> - update the commit message for above changes
> 
>  drivers/usb/typec/tcpm/tcpm.c | 27 +++++++++++++++++++++++++++
>  1 file changed, 27 insertions(+)
> 
> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index 197556038ba4..b1d310ab84c4 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
> @@ -2604,6 +2604,11 @@ static void tcpm_pd_ctrl_request(struct tcpm_port *port,
>  			} else {
>  				next_state = SNK_WAIT_CAPABILITIES;
>  			}
> +
> +			/* Threshold was relaxed before sending Request. Restore it back. */
> +			tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
> +							       port->pps_data.active,
> +							       port->supply_voltage);
>  			tcpm_set_state(port, next_state, 0);
>  			break;
>  		case SNK_NEGOTIATE_PPS_CAPABILITIES:
> @@ -2617,6 +2622,11 @@ static void tcpm_pd_ctrl_request(struct tcpm_port *port,
>  			    port->send_discover)
>  				port->vdm_sm_running = true;
>  
> +			/* Threshold was relaxed before sending Request. Restore it back. */
> +			tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
> +							       port->pps_data.active,
> +							       port->supply_voltage);
> +
>  			tcpm_set_state(port, SNK_READY, 0);
>  			break;
>  		case DR_SWAP_SEND:
> @@ -3336,6 +3346,12 @@ static int tcpm_pd_send_request(struct tcpm_port *port)
>  	if (ret < 0)
>  		return ret;
>  
> +	/*
> +	 * Relax the threshold as voltage will be adjusted after Accept Message plus tSrcTransition.
> +	 * It is safer to modify the threshold here.
> +	 */
> +	tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
> +
>  	memset(&msg, 0, sizeof(msg));
>  	msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
>  				  port->pwr_role,
> @@ -3433,6 +3449,9 @@ static int tcpm_pd_send_pps_request(struct tcpm_port *port)
>  	if (ret < 0)
>  		return ret;
>  
> +	/* Relax the threshold as voltage will be adjusted right after Accept Message. */
> +	tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
> +
>  	memset(&msg, 0, sizeof(msg));
>  	msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
>  				  port->pwr_role,
> @@ -4196,6 +4215,10 @@ static void run_state_machine(struct tcpm_port *port)
>  		port->hard_reset_count = 0;
>  		ret = tcpm_pd_send_request(port);
>  		if (ret < 0) {
> +			/* Restore back to the original state */
> +			tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
> +							       port->pps_data.active,
> +							       port->supply_voltage);
>  			/* Let the Source send capabilities again. */
>  			tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
>  		} else {
> @@ -4206,6 +4229,10 @@ static void run_state_machine(struct tcpm_port *port)
>  	case SNK_NEGOTIATE_PPS_CAPABILITIES:
>  		ret = tcpm_pd_send_pps_request(port);
>  		if (ret < 0) {
> +			/* Restore back to the original state */
> +			tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
> +							       port->pps_data.active,
> +							       port->supply_voltage);
>  			port->pps_status = ret;
>  			/*
>  			 * If this was called due to updates to sink
> -- 
> 2.32.0.272.g935e593368-goog

-- 
heikki

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

* Re: [PATCH v3] usb: typec: tcpm: Relax disconnect threshold during power negotiation
  2021-06-16 13:42 ` Heikki Krogerus
@ 2021-06-17  1:50   ` Badhri Jagan Sridharan
  0 siblings, 0 replies; 3+ messages in thread
From: Badhri Jagan Sridharan @ 2021-06-17  1:50 UTC (permalink / raw)
  To: Heikki Krogerus; +Cc: Kyle Tso, Guenter Roeck, Greg Kroah-Hartman, USB, LKML

On Wed, Jun 16, 2021 at 6:42 AM Heikki Krogerus
<heikki.krogerus@linux.intel.com> wrote:
>
> On Wed, Jun 16, 2021 at 05:01:02PM +0800, Kyle Tso wrote:
> > If the voltage is being decreased in power negotiation, the Source will
> > set the power supply to operate at the new voltage level before sending
> > PS_RDY. Relax the threshold before sending Request Message so that it
> > will not race with Source which begins to adjust the voltage right after
> > it sends Accept Message (PPS) or tSrcTransition (25~35ms) after it sends
> > Accept Message (non-PPS).
> >
> > The real threshold will be set after Sink receives PS_RDY Message.
> >
> > Fixes: f321a02caebd ("usb: typec: tcpm: Implement enabling Auto Discharge disconnect support")
> > Cc: Badhri Jagan Sridharan <badhri@google.com>
> > Signed-off-by: Kyle Tso <kyletso@google.com>
>
> Acked-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Reviewed-by: Badhri Jagan Sridharan <badhri@google.com>
>
> > ---
> > Changes in v3:
> > - move the timing of setting threshold for Fixed RDO as I did for PPS in
> >   v2, i.e. move it to tcpm_pd_send_request.
> > - add Cc: tag for Badhri
> > - update the commit message for the above changes
> >
> > Changes in v2:
> > - move the timing of setting threshold up to "before sending Request"
> >   for PPS power negotiation so that it won't race with the Source.
> > - PPS: if it fails to send the Request, fallback to previous threshold
> > - PPS: if the Source doesn't respond Accept, fallback to previous
> >   threshold
> > - update the commit message for above changes
> >
> >  drivers/usb/typec/tcpm/tcpm.c | 27 +++++++++++++++++++++++++++
> >  1 file changed, 27 insertions(+)
> >
> > diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> > index 197556038ba4..b1d310ab84c4 100644
> > --- a/drivers/usb/typec/tcpm/tcpm.c
> > +++ b/drivers/usb/typec/tcpm/tcpm.c
> > @@ -2604,6 +2604,11 @@ static void tcpm_pd_ctrl_request(struct tcpm_port *port,
> >                       } else {
> >                               next_state = SNK_WAIT_CAPABILITIES;
> >                       }
> > +
> > +                     /* Threshold was relaxed before sending Request. Restore it back. */
> > +                     tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
> > +                                                            port->pps_data.active,
> > +                                                            port->supply_voltage);
> >                       tcpm_set_state(port, next_state, 0);
> >                       break;
> >               case SNK_NEGOTIATE_PPS_CAPABILITIES:
> > @@ -2617,6 +2622,11 @@ static void tcpm_pd_ctrl_request(struct tcpm_port *port,
> >                           port->send_discover)
> >                               port->vdm_sm_running = true;
> >
> > +                     /* Threshold was relaxed before sending Request. Restore it back. */
> > +                     tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
> > +                                                            port->pps_data.active,
> > +                                                            port->supply_voltage);
> > +
> >                       tcpm_set_state(port, SNK_READY, 0);
> >                       break;
> >               case DR_SWAP_SEND:
> > @@ -3336,6 +3346,12 @@ static int tcpm_pd_send_request(struct tcpm_port *port)
> >       if (ret < 0)
> >               return ret;
> >
> > +     /*
> > +      * Relax the threshold as voltage will be adjusted after Accept Message plus tSrcTransition.
> > +      * It is safer to modify the threshold here.
> > +      */
> > +     tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
> > +
> >       memset(&msg, 0, sizeof(msg));
> >       msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
> >                                 port->pwr_role,
> > @@ -3433,6 +3449,9 @@ static int tcpm_pd_send_pps_request(struct tcpm_port *port)
> >       if (ret < 0)
> >               return ret;
> >
> > +     /* Relax the threshold as voltage will be adjusted right after Accept Message. */
> > +     tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
> > +
> >       memset(&msg, 0, sizeof(msg));
> >       msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
> >                                 port->pwr_role,
> > @@ -4196,6 +4215,10 @@ static void run_state_machine(struct tcpm_port *port)
> >               port->hard_reset_count = 0;
> >               ret = tcpm_pd_send_request(port);
> >               if (ret < 0) {
> > +                     /* Restore back to the original state */
> > +                     tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
> > +                                                            port->pps_data.active,
> > +                                                            port->supply_voltage);
> >                       /* Let the Source send capabilities again. */
> >                       tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
> >               } else {
> > @@ -4206,6 +4229,10 @@ static void run_state_machine(struct tcpm_port *port)
> >       case SNK_NEGOTIATE_PPS_CAPABILITIES:
> >               ret = tcpm_pd_send_pps_request(port);
> >               if (ret < 0) {
> > +                     /* Restore back to the original state */
> > +                     tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
> > +                                                            port->pps_data.active,
> > +                                                            port->supply_voltage);
> >                       port->pps_status = ret;
> >                       /*
> >                        * If this was called due to updates to sink
> > --
> > 2.32.0.272.g935e593368-goog
>
> --
> heikki

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

end of thread, other threads:[~2021-06-17  1:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-16  9:01 [PATCH v3] usb: typec: tcpm: Relax disconnect threshold during power negotiation Kyle Tso
2021-06-16 13:42 ` Heikki Krogerus
2021-06-17  1:50   ` Badhri Jagan Sridharan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.