Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3409819pxk; Mon, 21 Sep 2020 12:58:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwyGYG6aSa/AyaOLr84k3hDO1rQqgs0dCghaTl+a/uTs9oa3Lc6fKppWTY7zdojFozbHZ0O X-Received: by 2002:a17:906:2301:: with SMTP id l1mr1131554eja.488.1600718294550; Mon, 21 Sep 2020 12:58:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600718294; cv=none; d=google.com; s=arc-20160816; b=IhHq2IJ1A02nsRPpmvNd72sbEWorPAYatrBz2A83mD9k4CK1tOdItJRwI4Euo5LhCn aVOUIXZgKldS0K8L2LCA6ArjsPJkeMB4h9DIKknRuj3/cpg7pDmBNxfUhaO3/P+PAHjB NNQ9aDNrpLlHFGDaVf4JWu+fnzAAbe7xSv5IndHy3J9VmQeyAA01TCoEvAaTaj9RKyA4 4872D4YtftQOh5XKqgokM9qgbo9EoZNZiuCXr68Mz3ZR8maFnjhyY+jl8CWlD9zGu3lE 44lp4LhWrFalL7tf7/Lc0AVGuk5gKHqSKbRpn0ISrVz1UcDCzKYJIK4e3BzxID1fOUKi 5LWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=PpfXbwP5h561OnIXDrzldax9t/sUHIL5wMhDCaaaIt8=; b=RK/9h9RCgPUmb5X50KUPKll2yTcMnP8FRewRMUNHSFyFoJ24Gb0ap+NbcdHJGeemfX Ji4NNkVin4ireLCg8t7HbOtE4WaZsXH6xRJf8b0GI5rpwkesBcVFFD76zWtRbIoNXyfY gt4obfmu7kdPU2Kq/axT8F0b7kfOTsgKCnXeWKCvJ325PNGmeI5U8qF0cRgvs8QLrpxM NmSKAFVp6KEAmTt0fVNwwEmZOZhgzBrzJfjE1w6XbODfwL4FBwgdgVkg00Qec9wp8+Lf Zw/C9OTSot1ugUIx3f5mso729NB6uoj9nUz9RHWOTX71LwUQWuJ1lqOnb687HVErP7jV Ggjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=UpV3Hicb; 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 oq13si9057217ejb.308.2020.09.21.12.57.51; Mon, 21 Sep 2020 12:58:14 -0700 (PDT) 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=UpV3Hicb; 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 S1728346AbgIUT4Y (ORCPT + 99 others); Mon, 21 Sep 2020 15:56:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728281AbgIUT4X (ORCPT ); Mon, 21 Sep 2020 15:56:23 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D79BC0613D7 for ; Mon, 21 Sep 2020 12:56:21 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id v106so13958977ybi.6 for ; Mon, 21 Sep 2020 12:56:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=PpfXbwP5h561OnIXDrzldax9t/sUHIL5wMhDCaaaIt8=; b=UpV3Hicb3hTUuqUhuf+XEZX4SNcRpCJLpT0XnZc6eMzGRt6lOz8zyAPmKCeye2TOOn t7mDyGPGlBkjWpQSTz81abNXTim4uFIDf9SetTi9i39jnuCR14bWaBBzxO01/6hQH297 kBqRlxGWPi0CDmpi8nbrcjBcQcJDOzS0B3+tpYYqx070dKnb9D8TDYOXGyrD96Xz6yQp Yzt4cPn9KgSnsOXYv13hKIyprBBPkZ8v+D2/JPmXLWlDOm3uGwzkMZbqBNcbnk2a8r6t LKu2y58wMRG44TtHyYuBdeDMT/vJCyW8mFkZJx81DfF7sK2EeFhBkQLkupFbumP7I/6D 4Kfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=PpfXbwP5h561OnIXDrzldax9t/sUHIL5wMhDCaaaIt8=; b=HUB+EqPStNPUPE/Suik/rKX1NraRhyrT6heSaYg3jQTbMAno1a2q/ohQREXe58IXtP Cy6A9OqHoEI0+x4+LUk/ERkaBlAE2s+simA77lpbeEI7nOAai+Xx6WIRc/ZFFpvAEXSD I7Ydl8TtBlTmeE9a0oCdnDxDB3ZB2fgtHmrNIHEEvERLSIF93AdVHOtymEzg6tOAGNHq 7VfM+MrMY0q5wmKAL6x6DTp4vrzs6QaydjMmwmdrxb48pQJEKA6HTWoWEZEVPNEQ2wxO KJwAYBKEW121O07NO9iFIndzQoHlIOtBzCJNRht3c2SBayM5Jf5x/7qWjsXWG69MEMTN BK7A== X-Gm-Message-State: AOAM532NPO0QBGevk1vyNn/U1OivHsHemoeHesXnO7/C2dpRWZPyKpwk m+6c8P1Ph5CjVIXvwfqixkbfV8H9G/A= Sender: "badhri via sendgmr" X-Received: from badhri.mtv.corp.google.com ([2620:15c:211:1:f292:1cff:fee0:66cf]) (user=badhri job=sendgmr) by 2002:a25:d950:: with SMTP id q77mr2161784ybg.402.1600718180389; Mon, 21 Sep 2020 12:56:20 -0700 (PDT) Date: Mon, 21 Sep 2020 12:55:53 -0700 In-Reply-To: <20200921195555.1050731-1-badhri@google.com> Message-Id: <20200921195555.1050731-9-badhri@google.com> Mime-Version: 1.0 References: <20200921195555.1050731-1-badhri@google.com> X-Mailer: git-send-email 2.28.0.681.g6f77f65b4e-goog Subject: [PATCH v8 09/11] usb: typec: tcpm: Implement enabling Auto Discharge disconnect support From: Badhri Jagan Sridharan To: Guenter Roeck , Heikki Krogerus , Greg Kroah-Hartman , Rob Herring , Lee Jones , Mark Brown , Maxime Ripard , Alexandre Belloni , Thierry Reding , Prashant Malani , Badhri Jagan Sridharan Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- Changes since v1: - Changing patch version to v6 to fix version number confusion. Changes since v6: - Fixed incorrect data_role error that I introduced by mistake in the previous version. Changes since v7: - Rebase on usb-next --- drivers/usb/typec/tcpm/tcpm.c | 74 +++++++++++++++++++++++++++++++++-- include/linux/usb/tcpm.h | 16 ++++++++ 2 files changed, 86 insertions(+), 4 deletions(-) diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c index 55535c4f66bf..d6b2a757a564 100644 --- a/drivers/usb/typec/tcpm/tcpm.c +++ b/drivers/usb/typec/tcpm/tcpm.c @@ -1706,6 +1706,25 @@ static void tcpm_handle_alert(struct tcpm_port *port, const __le32 *payload, } } +static int tcpm_set_auto_vbus_discharge_threshold(struct tcpm_port *port, enum typec_role port_role, + enum typec_pwr_opmode mode, bool pps_active, + u32 requested_vbus_voltage) +{ + int ret; + + if (!port->tcpc->set_auto_vbus_discharge_threshold) + return 0; + + ret = port->tcpc->set_auto_vbus_discharge_threshold(port->tcpc, port_role, mode, pps_active, + requested_vbus_voltage); + tcpm_log_force(port, + "set_auto_vbus_discharge_threshold pwr_role:%s mode:%d pps_active:%c vbus:%u ret:%d", + port_role == TYPEC_SINK ? "sink" : "source", mode, pps_active ? 'y' : 'n', + requested_vbus_voltage, ret); + + return ret; +} + static void tcpm_pd_data_request(struct tcpm_port *port, const struct pd_message *msg) { @@ -1875,6 +1894,10 @@ static void tcpm_pd_ctrl_request(struct tcpm_port *port, port->current_limit, port->supply_voltage); port->explicit_contract = true; + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_SINK, + TYPEC_PWR_MODE_PD, + port->pps_data.active, + port->supply_voltage); tcpm_set_state(port, SNK_READY, 0); } else { /* @@ -2789,8 +2812,14 @@ static int tcpm_src_attach(struct tcpm_port *port) if (ret < 0) return ret; - ret = tcpm_set_roles(port, true, TYPEC_SOURCE, - tcpm_data_role_for_source(port)); + if (port->tcpc->enable_auto_vbus_discharge) { + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_SOURCE, 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); + } + + ret = tcpm_set_roles(port, true, TYPEC_SOURCE, tcpm_data_role_for_source(port)); if (ret < 0) return ret; @@ -2857,6 +2886,12 @@ static void tcpm_unregister_altmodes(struct tcpm_port *port) static void tcpm_reset_port(struct tcpm_port *port) { + int ret; + + 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); + } tcpm_unregister_altmodes(port); tcpm_typec_disconnect(port); port->attached = false; @@ -2921,8 +2956,14 @@ static int tcpm_snk_attach(struct tcpm_port *port) if (ret < 0) return ret; - ret = tcpm_set_roles(port, true, TYPEC_SINK, - tcpm_data_role_for_sink(port)); + if (port->tcpc->enable_auto_vbus_discharge) { + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_SINK, 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); + } + + ret = tcpm_set_roles(port, true, TYPEC_SINK, tcpm_data_role_for_sink(port)); if (ret < 0) return ret; @@ -3482,6 +3523,8 @@ static void run_state_machine(struct tcpm_port *port) * drive VBUS to vSafe0V as shown in Figure 7-9. */ tcpm_set_vconn(port, false); + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_SOURCE, TYPEC_PWR_MODE_USB, + false, 0); tcpm_set_vbus(port, false); tcpm_set_roles(port, port->self_powered, TYPEC_SOURCE, tcpm_data_role_for_source(port)); @@ -3503,9 +3546,13 @@ static void run_state_machine(struct tcpm_port *port) tcpm_set_vbus(port, true); port->tcpc->set_pd_rx(port->tcpc, true); tcpm_set_attached_state(port, true); + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_SOURCE, TYPEC_PWR_MODE_USB, + false, VSAFE5V); tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON); break; case SNK_HARD_RESET_SINK_OFF: + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_SINK, TYPEC_PWR_MODE_USB, false, + 0); memset(&port->pps_data, 0, sizeof(port->pps_data)); tcpm_set_vconn(port, false); if (port->pd_capable) @@ -3548,6 +3595,8 @@ static void run_state_machine(struct tcpm_port *port) tcpm_set_charge(port, true); } tcpm_set_attached_state(port, true); + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_SINK, TYPEC_PWR_MODE_USB, false, + VSAFE5V); tcpm_set_state(port, SNK_STARTUP, 0); break; @@ -3649,6 +3698,12 @@ static void run_state_machine(struct tcpm_port *port) tcpm_set_state(port, PR_SWAP_SNK_SRC_SINK_OFF, 0); break; case PR_SWAP_SRC_SNK_TRANSITION_OFF: + /* + * Prevent vbus discharge circuit from turning on during PR_SWAP + * as this is not a disconnect. + */ + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_SOURCE, TYPEC_PWR_MODE_USB, + port->pps_data.active, 0); tcpm_set_vbus(port, false); port->explicit_contract = false; /* allow time for Vbus discharge, must be < tSrcSwapStdby */ @@ -3677,9 +3732,18 @@ static void run_state_machine(struct tcpm_port *port) tcpm_set_state_cond(port, SNK_UNATTACHED, PD_T_PS_SOURCE_ON); break; case PR_SWAP_SRC_SNK_SINK_ON: + /* Set the vbus disconnect threshold for implicit contract */ + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_SINK, TYPEC_PWR_MODE_USB, false, + VSAFE5V); tcpm_set_state(port, SNK_STARTUP, 0); break; case PR_SWAP_SNK_SRC_SINK_OFF: + /* + * Prevent vbus discharge circuit from turning on during PR_SWAP + * as this is not a disconnect. + */ + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_SINK, TYPEC_PWR_MODE_USB, + port->pps_data.active, 0); tcpm_set_charge(port, false); tcpm_set_state(port, hard_reset_state(port), PD_T_PS_SOURCE_OFF); @@ -3705,6 +3769,8 @@ static void run_state_machine(struct tcpm_port *port) */ tcpm_set_pwr_role(port, TYPEC_SOURCE); tcpm_pd_send_control(port, PD_CTRL_PS_RDY); + tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_SOURCE, TYPEC_PWR_MODE_USB, + false, VSAFE5V); tcpm_set_state(port, SRC_STARTUP, PD_T_SWAP_SRC_START); break; diff --git a/include/linux/usb/tcpm.h b/include/linux/usb/tcpm.h index 09762d26fa0c..a1707def51c3 100644 --- a/include/linux/usb/tcpm.h +++ b/include/linux/usb/tcpm.h @@ -83,6 +83,18 @@ enum tcpm_transmit_type { * Optional; Called to enable/disable PD 3.0 fast role swap. * Enabling frs is accessory dependent as not all PD3.0 * accessories support fast role swap. + * @enable_auto_vbus_discharge: + * Optional; TCPCI spec based TCPC implementations can optionally + * support hardware to autonomously dischrge vbus upon disconnecting + * as sink or source. TCPM signals TCPC to enable the mechanism upon + * entering connected state and signals disabling upon disconnect. + * @set_auto_vbus_discharge_threshold: + * Mandatory when enable_auto_vbus_discharge is implemented. TCPM + * calls this function to allow lower levels drivers to program the + * vbus threshold voltage below which the vbus discharge circuit + * will be turned on. requested_vbus_voltage is set to 0 when vbus + * is going to disappear knowingly i.e. during PR_SWAP and + * HARD_RESET etc. */ struct tcpc_dev { struct fwnode_handle *fwnode; @@ -109,6 +121,10 @@ struct tcpc_dev { const struct pd_message *msg); int (*set_bist_data)(struct tcpc_dev *dev, bool on); int (*enable_frs)(struct tcpc_dev *dev, bool enable); + int (*enable_auto_vbus_discharge)(struct tcpc_dev *dev, bool enable); + int (*set_auto_vbus_discharge_threshold)(struct tcpc_dev *dev, enum typec_role port_role, + enum typec_pwr_opmode mode, bool pps_active, + u32 requested_vbus_voltage); }; struct tcpm_port; -- 2.28.0.681.g6f77f65b4e-goog