From patchwork Mon Sep 21 19:55:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Badhri Jagan Sridharan X-Patchwork-Id: 297557 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.4 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A3801C4363C for ; Mon, 21 Sep 2020 19:56:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6C66C22574 for ; Mon, 21 Sep 2020 19:56:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Vww2+/C0" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728347AbgIUT4Y (ORCPT ); Mon, 21 Sep 2020 15:56:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35592 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728334AbgIUT4X (ORCPT ); Mon, 21 Sep 2020 15:56:23 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D961C0613DA for ; Mon, 21 Sep 2020 12:56:23 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id i23so492864pju.7 for ; Mon, 21 Sep 2020 12:56:23 -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=Od9yElV1P9nOtF+n66V3yXmKIpojTRtnzHLfBefsfwo=; b=Vww2+/C0IkDqNnyGXH1GAHujyibguHDsm4lLK8VjE+c7UnUjUtzOq5UB30F70tFphG swKndRU0R/ZbhLJHAckgcicxC+lZAANZ8JZXwsxBity7k6JIQInaWDzKnqmgTZnjuBKU 1ySuxz9fNDJ+PPq5NuaQMvmyL0zBeXXDV/JnS1nd+a3TJli3bA6N/sdrH/rJAsdrlzLo YryjCvvn/B5WXJ8HXaPfvpjlImSzkjYiV8DtZRlXbz0L4fZ57taDPn4VZmWV8LE36uyz zhJpPPo7/tm5womydtGBB0XQ5tWQxu7WYOJSLoKxaNvAt9d2SkUV+U6eNEzYT0pN9j3P VVFg== 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=Od9yElV1P9nOtF+n66V3yXmKIpojTRtnzHLfBefsfwo=; b=ph/60ZAtuW4+fRbJTa5XQbDNo37FuFyQIm8HuxXxTWcCrszmUhOLYxPAt4A+bGfF7j 6tWv4hMggZe0gDvJcw9mSmteudNISsd8jJ9YVi8/KnQd+ikDB06m+Eed8iwc/1YKfSvN XAR/bata0hIfl/dlTlosJ0KK0KC2ojU9mi/rM46zHOZdHK/PjxZmfMQchoekOl8ezLNh tn2PKrujETYEz4vRvXLhZ+Jt28n1c2YUeLLhezFu3sBZyiw1sSihqhbfyXBg6oqds4+n yKxzQUEkr4nf9JM9HwBwxWaAimTp8QxLaxYIRkJ2Q7ePL2zTA3NjpowtJJ6CI9+gzBtE DEyQ== X-Gm-Message-State: AOAM530eMfJ383Dn+gLsbD5IdzNnvPLBpmCwc7wEEtXjFp4ljbkumcNP eNmzEykG/Pscp8y2bEr+KU6/pzQXeRg= X-Google-Smtp-Source: ABdhPJyRKWIhRYoXSiKYNl0C/5q5JrF4gGPkiq+w/miU7Llsu5J9c/kNjGVyXw2U2OY39CC+V4PdLBwJf1I= 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:a17:902:6b05:b029:d0:a100:8365 with SMTP id o5-20020a1709026b05b02900d0a1008365mr1457888plk.11.1600718182548; Mon, 21 Sep 2020 12:56:22 -0700 (PDT) Date: Mon, 21 Sep 2020 12:55:54 -0700 In-Reply-To: <20200921195555.1050731-1-badhri@google.com> Message-Id: <20200921195555.1050731-10-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 10/11] usb: typec: tcpci: Implement Auto discharge disconnect callbacks 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 Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org Implement callbacks for enabling/disabling POWER_CONTROL.AutoDischargeDisconnect. 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: - Rebase on usb-next. Changes since v7: Heikki's suggestion: - Moved the actual write to TCPC_VBUS_SINK_DISCONNECT_THRESH as it's common to all chip drivers. - Renaming the tcpci_data callback as get_auto_vbus_discharge_threshold --- drivers/usb/typec/tcpm/tcpci.c | 48 ++++++++++++++++++++++++++++++++++ drivers/usb/typec/tcpm/tcpci.h | 22 ++++++++++++++-- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/drivers/usb/typec/tcpm/tcpci.c b/drivers/usb/typec/tcpm/tcpci.c index f9f0af64da5f..dca21cd5873e 100644 --- a/drivers/usb/typec/tcpm/tcpci.c +++ b/drivers/usb/typec/tcpm/tcpci.c @@ -268,6 +268,48 @@ static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool enable) enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0); } +static int tcpci_enable_auto_vbus_discharge(struct tcpc_dev *dev, bool enable) +{ + struct tcpci *tcpci = tcpc_to_tcpci(dev); + int ret; + + ret = regmap_update_bits(tcpci->regmap, TCPC_POWER_CTRL, TCPC_POWER_CTRL_AUTO_DISCHARGE, + enable ? TCPC_POWER_CTRL_AUTO_DISCHARGE : 0); + return ret; +} + +static int tcpci_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_mv) +{ + struct tcpci *tcpci = tcpc_to_tcpci(dev); + u32 (*get_auto_vbus_threshold)(struct tcpci *tcpci, struct tcpci_data *data, + enum typec_role port_role, enum typec_pwr_opmode mode, + bool pps_active, u32 requested_vbus_voltage_mv); + + get_auto_vbus_threshold = tcpci->data->get_auto_vbus_discharge_threshold; + if (get_auto_vbus_threshold) { + u32 threshold; + u16 val; + + /* + * Indicates that vbus is going to go away due PR_SWAP, hard reset etc. + * Do not discharge vbus here. + */ + threshold = requested_vbus_voltage_mv ? + get_auto_vbus_threshold(tcpci, tcpci->data, port_role, mode, pps_active, + requested_vbus_voltage_mv) : 0; + + val = threshold / TCPC_VBUS_SINK_DISCONNECT_THRESH_LSB_MV; + if (val > TCPC_VBUS_SINK_DISCONNECT_THRESH_MAX) + return -EINVAL; + + return tcpci_write16(tcpci, TCPC_VBUS_SINK_DISCONNECT_THRESH, val); + } + + return 0; +} + static int tcpci_enable_frs(struct tcpc_dev *dev, bool enable) { struct tcpci *tcpci = tcpc_to_tcpci(dev); @@ -629,6 +671,12 @@ struct tcpci *tcpci_register_port(struct device *dev, struct tcpci_data *data) tcpci->tcpc.set_bist_data = tcpci_set_bist_data; tcpci->tcpc.enable_frs = tcpci_enable_frs; + if (tcpci->data->auto_discharge_disconnect) { + tcpci->tcpc.enable_auto_vbus_discharge = tcpci_enable_auto_vbus_discharge; + tcpci->tcpc.set_auto_vbus_discharge_threshold = + tcpci_set_auto_vbus_discharge_threshold; + } + err = tcpci_parse_config(tcpci); if (err < 0) return ERR_PTR(err); diff --git a/drivers/usb/typec/tcpm/tcpci.h b/drivers/usb/typec/tcpm/tcpci.h index 5ef07a56d67a..406dfb53f492 100644 --- a/drivers/usb/typec/tcpm/tcpci.h +++ b/drivers/usb/typec/tcpm/tcpci.h @@ -8,6 +8,8 @@ #ifndef __LINUX_USB_TCPCI_H #define __LINUX_USB_TCPCI_H +#include + #define TCPC_VENDOR_ID 0x0 #define TCPC_PRODUCT_ID 0x2 #define TCPC_BCD_DEV 0x4 @@ -67,6 +69,7 @@ #define TCPC_POWER_CTRL 0x1c #define TCPC_POWER_CTRL_VCONN_ENABLE BIT(0) +#define TCPC_POWER_CTRL_AUTO_DISCHARGE BIT(4) #define TCPC_FAST_ROLE_SWAP_EN BIT(7) #define TCPC_CC_STATUS 0x1d @@ -133,6 +136,8 @@ #define TCPC_VBUS_VOLTAGE 0x70 #define TCPC_VBUS_SINK_DISCONNECT_THRESH 0x72 +#define TCPC_VBUS_SINK_DISCONNECT_THRESH_LSB_MV 25 +#define TCPC_VBUS_SINK_DISCONNECT_THRESH_MAX 0x3ff #define TCPC_VBUS_STOP_DISCHARGE_THRESH 0x74 #define TCPC_VBUS_VOLTAGE_ALARM_HI_CFG 0x76 #define TCPC_VBUS_VOLTAGE_ALARM_LO_CFG 0x78 @@ -140,20 +145,33 @@ /* I2C_WRITE_BYTE_COUNT + 1 when TX_BUF_BYTE_x is only accessible I2C_WRITE_BYTE_COUNT */ #define TCPC_TRANSMIT_BUFFER_MAX_LEN 31 +struct tcpci; + /* - * @TX_BUF_BYTE_x_hidden + * @TX_BUF_BYTE_x_hidden: * optional; Set when TX_BUF_BYTE_x can only be accessed through I2C_WRITE_BYTE_COUNT. + * @auto_discharge_disconnect: + * Optional; Enables TCPC to autonously discharge vbus on disconnect. + * @set_auto_vbus_discharge_threshold: + * Mandatory when @auto_discharge_disconnect is sets. Allows + * programming the voltage threshold of vbus below which TCPC + * enables the vbus discharge circuit. */ -struct tcpci; struct tcpci_data { struct regmap *regmap; unsigned char TX_BUF_BYTE_x_hidden:1; + unsigned char auto_discharge_disconnect:1; + int (*init)(struct tcpci *tcpci, struct tcpci_data *data); int (*set_vconn)(struct tcpci *tcpci, struct tcpci_data *data, bool enable); int (*start_drp_toggling)(struct tcpci *tcpci, struct tcpci_data *data, enum typec_cc_status cc); int (*set_vbus)(struct tcpci *tcpci, struct tcpci_data *data, bool source, bool sink); + u32 (*get_auto_vbus_discharge_threshold)(struct tcpci *tcpci, struct tcpci_data *data, + enum typec_role port_role, + enum typec_pwr_opmode mode, bool pps_active, + u32 requested_vbus_voltage); }; struct tcpci *tcpci_register_port(struct device *dev, struct tcpci_data *data);