From patchwork Sun Aug 6 12:35:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 798379 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-i2c-owner@vger.kernel.org; receiver=) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3xQKy22MDPz9sRq for ; Sun, 6 Aug 2017 22:40:26 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751398AbdHFMgH (ORCPT ); Sun, 6 Aug 2017 08:36:07 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38078 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751256AbdHFMgF (ORCPT ); Sun, 6 Aug 2017 08:36:05 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 539798123A; Sun, 6 Aug 2017 12:36:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 539798123A Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=hdegoede@redhat.com Received: from shalem.localdomain.com (ovpn-116-85.ams2.redhat.com [10.36.116.85]) by smtp.corp.redhat.com (Postfix) with ESMTP id A88FE6FE50; Sun, 6 Aug 2017 12:36:02 +0000 (UTC) From: Hans de Goede To: Darren Hart , Andy Shevchenko , Wolfram Sang , Sebastian Reichel , Greg Kroah-Hartman , Guenter Roeck , Heikki Krogerus Cc: Hans de Goede , platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, linux-i2c@vger.kernel.org, Liam Breck , Tony Lindgren , linux-pm@vger.kernel.org, devel@driverdev.osuosl.org Subject: [PATCH 02/18] staging: typec: tcpm: Add extcon helper functions for USB2 current limit detect Date: Sun, 6 Aug 2017 14:35:39 +0200 Message-Id: <20170806123555.5124-3-hdegoede@redhat.com> In-Reply-To: <20170806123555.5124-1-hdegoede@redhat.com> References: <20170806123555.5124-1-hdegoede@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Sun, 06 Aug 2017 12:36:05 +0000 (UTC) Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org Some type-c port-controllers, such as the fusb302 port-controller, rely on an external device doing USB2 charger-type detection. Existing PMIC (and charger) drivers already use extcon to communicate the detected charger-type from the PMIC (extcon) driver to the charger driver. Rather then inventing a new API for USB2 charger-type detection specifically for use with the tcpm code, lets simply re-use the existing support. This will also allow re-using existing PMIC extcon drivers such as the axp288 and Intel Whiskey Cove drivers as is on devices where these are combined with a fusb302 (or in the future another port-controller which relies on external USB2 charger-type detection). This commit adds a helper function which tcpc drivers can use to easily hook into existing PMIC extcon drivers for USB2 charger-type detection: int tcpm_get_usb2_current_limit_extcon(struct tcpc_dev *tcpc); Signed-off-by: Hans de Goede --- drivers/staging/typec/tcpm.c | 40 ++++++++++++++++++++++++++++++++++++++++ drivers/staging/typec/tcpm.h | 6 ++++++ 2 files changed, 46 insertions(+) diff --git a/drivers/staging/typec/tcpm.c b/drivers/staging/typec/tcpm.c index 9f5adace4309..06bb0e640bcf 100644 --- a/drivers/staging/typec/tcpm.c +++ b/drivers/staging/typec/tcpm.c @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -3532,6 +3533,45 @@ void tcpm_unregister_port(struct tcpm_port *port) } EXPORT_SYMBOL_GPL(tcpm_unregister_port); +/* Generic (helper) implementations for some tcpc_dev callbacks */ +int tcpm_get_usb2_current_limit_extcon(struct tcpc_dev *tcpc) +{ + struct extcon_dev *extcon = tcpc->usb2_extcon; + int current_limit = 0; + unsigned long timeout; + + if (!extcon) + return 0; + + /* + * USB2 Charger detection may still be in progress when we get here, + * this can take upto 600ms, wait 800ms max. + */ + timeout = jiffies + msecs_to_jiffies(800); + do { + if (extcon_get_state(extcon, EXTCON_CHG_USB_SDP) == 1) { + current_limit = 500; + break; + } + + if (extcon_get_state(extcon, EXTCON_CHG_USB_CDP) == 1 || + extcon_get_state(extcon, EXTCON_CHG_USB_ACA) == 1) { + current_limit = 1500; + break; + } + + if (extcon_get_state(extcon, EXTCON_CHG_USB_DCP) == 1) { + current_limit = 2000; + break; + } + + msleep(50); + } while (time_before(jiffies, timeout)); + + return current_limit; +} +EXPORT_SYMBOL_GPL(tcpm_get_usb2_current_limit_extcon); + MODULE_AUTHOR("Guenter Roeck "); MODULE_DESCRIPTION("USB Type-C Port Manager"); MODULE_LICENSE("GPL"); diff --git a/drivers/staging/typec/tcpm.h b/drivers/staging/typec/tcpm.h index 01b7d89379a3..35e8c1e7dba0 100644 --- a/drivers/staging/typec/tcpm.h +++ b/drivers/staging/typec/tcpm.h @@ -16,6 +16,7 @@ #define __LINUX_USB_TCPM_H #include +#include #include #include "pd.h" @@ -126,6 +127,8 @@ struct tcpc_dev { int (*pd_transmit)(struct tcpc_dev *dev, enum tcpm_transmit_type type, const struct pd_message *msg); struct tcpc_mux_dev *mux; + /* Used by tcpm_get_usb2_current_limit_extcon helpers */ + struct extcon_dev *usb2_extcon; }; struct tcpm_port; @@ -151,4 +154,7 @@ void tcpm_pd_transmit_complete(struct tcpm_port *port, void tcpm_pd_hard_reset(struct tcpm_port *port); void tcpm_tcpc_reset(struct tcpm_port *port); +/* Generic (helper) implementations for some tcpc_dev callbacks */ +int tcpm_get_usb2_current_limit_extcon(struct tcpc_dev *tcpc); + #endif /* __LINUX_USB_TCPM_H */