Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp181056pxb; Wed, 24 Feb 2021 22:49:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJzSKeJYrnik1UU7o9SKH/UM1vwpdQNh1rkeEYueRxU71jxcADHLDPi2NyFY2x5yUzS1V7c5 X-Received: by 2002:aa7:cac6:: with SMTP id l6mr1452149edt.357.1614235752324; Wed, 24 Feb 2021 22:49:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614235752; cv=none; d=google.com; s=arc-20160816; b=H93YGjpF8A0Qz4WMfdKZi36JxqYYcKrw5th8CUXVbgIoiBj5anpuUUKtYyAOZXwviR pQBOkSoz+LLlM55POqYGBc4nbmFZgn1l+2ZQZDCTQPhj9ACZn8/vo/I2L4ug8KauGv00 yu7ltE8iWw5V/aYy2X+KXdpfNd88JK/xnOkFzglTujsnMbCKilvPZRgvzrRjVF1nQnwF QLOnC3EkgnIFiZry6e5dhWESbCUQTZNa565/YIqOcQLG8CEk3USBm3mNo2R1W5GbY0h+ LLbzRSvoirIMkd/3CQF8DiYppdNPu+W1Opy5zBdXG3IpYgVFc3HqdvTEqNQT/4JH4ZmH 4gmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=7rjxHnpyYn9L8b5/UDQL6DJ1bU7foo2uFq2+dZ8sa4Y=; b=GirZe3pUyjwW9RgYt3eX4FI9fYRX40OEOs57izNG9OLqxr87Br4sqP3Y15ZoUoeh7w NiImHF2T6Z2WZOOV1nHYAXjbv6GqBDBMyhL4PpNXUR28fwt3i6lbIMIk67rY2ndzP7dQ qkVS3zQ5KSmt0+LlSxf3EpTbHiqOSv7zQiXhm2Ax6QQnIepBOoHACoIyIAkTvbqNd15m XYnTKG+h89gbEOT85PEud+B91WTCuCKHMfc1a/WXd7uRhoX6gtFmct5qVFbwcdYKlhUP HvoHgWhLJpJrnvx/v0qAz8q98KfKaCfeHbB4cze5Ba6lnwn/lQIOBKDHcd1VVKWmlvm4 eDHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=q6SaxgMe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w21si2972522ejn.516.2021.02.24.22.48.49; Wed, 24 Feb 2021 22:49:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=q6SaxgMe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235245AbhBYFra (ORCPT + 99 others); Thu, 25 Feb 2021 00:47:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54492 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231274AbhBYFr2 (ORCPT ); Thu, 25 Feb 2021 00:47:28 -0500 Received: from mail-vs1-xe2d.google.com (mail-vs1-xe2d.google.com [IPv6:2607:f8b0:4864:20::e2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 738F5C061574 for ; Wed, 24 Feb 2021 21:46:42 -0800 (PST) Received: by mail-vs1-xe2d.google.com with SMTP id t23so2371614vsk.2 for ; Wed, 24 Feb 2021 21:46:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=7rjxHnpyYn9L8b5/UDQL6DJ1bU7foo2uFq2+dZ8sa4Y=; b=q6SaxgMehhZKqHr35k4EhsbU7RgRd2N6TlC5zRxFfrx7ADEhd+X5erKoplc/AwXcju Dy0/MHQox2Fke9+SpXgXQ2/nAEa6NvE7VmDkWf2fDE69GMFZMu0AdOURM7Nka+s568NP R048xtZUf2lSOHFED/00TKEXA03aBxSv0iaF0Cyd/mjo/PPKCxuSdD28Ut9Y5622YPLd m/X5K7J2Nmp7qdxNvYPmxcnJNQIaeTWKAQX1Y8rmUB5+H2dn12D1iYdZNDVgLon35/p/ Tvd9TwA9w7bQhkc28Ehf+M5mNFGvZiupmbNNNndLaHa726HpZ6QtSjH2ttPTbtC3My7B 9azQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=7rjxHnpyYn9L8b5/UDQL6DJ1bU7foo2uFq2+dZ8sa4Y=; b=ZIpwBQtDpQ3ZVtY0+cAC+wdaJNO8VXe5hmlpk54Ha22DDVmnnivoTC6Gc50FGV6bl2 2kcxjYg5/+BfzFEKrsdiOPhd4xPMN3KdlAV4FpvF0fnUxIptIND7C7j20BXFaMWoc4oG Xnu2sSNuoTFRWAZw9nc9Uylf6mvf8RfSwCj8coKbxzsQJeBBT7BkKFRVeEUR0qyRBJeq SBW0Ofq2R1wDywQtpKZp87HVNIZ/SUr6RVurQP54BxmYB//p09oRGgMqXoJ14taTli+S BtNqW0VMnvqX8jyxBsq9RKPP5xZrVCniWFVk6H+33maPlzuobgtJPtCrqZZfB5Q3DUCr Vepw== X-Gm-Message-State: AOAM532l7XIi8eSLJJcIldhya/JlU9YrwmC5g2Nwk09Z+mdjcMQq0/mi 6sI5aBV3obHZK866OEI5+ZOdtOJLX2h2AEs5K5FMnA== X-Received: by 2002:a05:6102:1275:: with SMTP id q21mr689951vsg.43.1614232001065; Wed, 24 Feb 2021 21:46:41 -0800 (PST) MIME-Version: 1.0 References: <20210219090409.325492-1-badhri@google.com> In-Reply-To: From: Badhri Jagan Sridharan Date: Wed, 24 Feb 2021 21:46:04 -0800 Message-ID: Subject: Re: [PATCH v2] usb: typec: tcpm: Wait for vbus discharge to VSAFE0V before toggling To: Guenter Roeck Cc: Heikki Krogerus , Greg Kroah-Hartman , Kyle Tso , USB , LKML , stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Feb 19, 2021 at 7:56 AM Guenter Roeck wrote: > > On 2/19/21 1:04 AM, 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. > > > > Fixes: f321a02caebd ("usb: typec: tcpm: Implement enabling Auto > > Discharge disconnect support") > > Signed-off-by: Badhri Jagan Sridharan > > --- > > Changes since V1: > > - Add Fixes tag > > --- > > 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); > > Unless I am missing something, the new state is only used to set the > PD_T_SAFE_0V timeout. Is it really necessary/useful to add a new state > just for that, while keeping the rest of if/else statements ? > Personally I would prefer something like > timeout = (port->auto_vbus_discharge_enabled && !port->vbus_vsafe0v) ? PD_T_SAFE_0V : 0; > if (port->port_type == TYPEC_PORT_SRC) > tcpm_set_state(port, SRC_UNATTACHED, timeout); > else > tcpm_set_state(port, SNK_UNATTACHED, timeout); > Yes this should be OK as well. I was thinking it would be more clearer during debug if there was a separate state altogether, but, looks like we should be fine. Implementing/Validating it now. Will send a follow up version today. > In this context, any idea why port_type==TYPEC_PORT_DRP results in > SNK_UNATTACHED state ? That seems a bit odd. This comes from the patch here: https://lore.kernel.org/r/1582128343-22438-1-git-send-email-jun.li@nxp.com Looks reasonable to me as tcpm_*_detach functions call tcpm_detach so teardown should happen anyways. static void tcpm_snk_detach(struct tcpm_port *port) { tcpm_detach(port); } static void tcpm_src_detach(struct tcpm_port *port) { tcpm_detach(port); } Thanks, Badhri > > Guenter > > > @@ -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; > > } > > >