From patchwork Tue Jun 13 14:55:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Al Cooper X-Patchwork-Id: 9784215 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3E1D860212 for ; Tue, 13 Jun 2017 14:59:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1E9B227F97 for ; Tue, 13 Jun 2017 14:59:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1D0132870D; Tue, 13 Jun 2017 14:59:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 733AA28724 for ; Tue, 13 Jun 2017 14:59:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=QJlocq2Kf9za1M9vh/Uy+8tUsNri2LqDBLfhyALo9uA=; b=Of/oF95XlBTwCH9KD5af9PTTi7 7q5G5v83xw7DzKi+iPdk2q/etEzvPCovrvVf8i2PRMdyMA5tHT5bleDELD95AHD/sGWM0vWcDpmcp naFtI6xBr63n80Pl7m+NiZwrh7j/3JEBzNCKAEe0OnzTDWBuVr86VVS2CEitepSPLHpek+a+HgMPH Pmnx0CapKcR7Z5ltmQdmtiiYqvepHLzJPJCMTUbDr9nTZmyAGfihKzzFbZ1hnKaJJ4c1gW9mlzV6k 963o/KHfCmV8FaXkvmWDjmYsGg2ajs8BAsd7ejULYlqleV5E9wYXwusNabyvfzTeW/TM5hvd6hJdr 7Y498mbw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dKnHX-0005yX-3o; Tue, 13 Jun 2017 14:58:59 +0000 Received: from lpdvrndsmtp01.broadcom.com ([192.19.229.170] helo=rnd-relay.smtp.broadcom.com) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dKnGo-0004sC-Cj for linux-arm-kernel@lists.infradead.org; Tue, 13 Jun 2017 14:58:17 +0000 Received: from mail-irv-17.broadcom.com (mail-irv-17.lvn.broadcom.net [10.75.224.233]) by rnd-relay.smtp.broadcom.com (Postfix) with ESMTP id C678830C038; Tue, 13 Jun 2017 07:57:53 -0700 (PDT) Received: from stbsrv-and-3.and.broadcom.com (stbsrv-and-3.and.broadcom.com [10.28.16.21]) by mail-irv-17.broadcom.com (Postfix) with ESMTP id 2428081EA4; Tue, 13 Jun 2017 07:57:52 -0700 (PDT) From: Al Cooper To: linux-kernel@vger.kernel.org Subject: [PATCH v2 4/4] phy: usb: phy-brcm-usb: Add ability to force DRD mode to host or device Date: Tue, 13 Jun 2017 10:55:21 -0400 Message-Id: <1497365721-31188-5-git-send-email-al.cooper@broadcom.com> X-Mailer: git-send-email 1.9.0.138.g2de3478 In-Reply-To: <1497365721-31188-1-git-send-email-al.cooper@broadcom.com> References: <1497365721-31188-1-git-send-email-al.cooper@broadcom.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170613_075814_543278_B28A5A8C X-CRM114-Status: GOOD ( 14.16 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , devicetree@vger.kernel.org, Florian Fainelli , Greg Kroah-Hartman , Al Cooper , Rob Herring , Kishon Vijay Abraham I , Al Cooper , bcm-kernel-feedback-list@broadcom.com, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP When the usb phy device mode is set to "drd", the USB port will switch between device and host modes depending on what's plugged into the port. Customers have asked for the ability to force host or device mode from software. This commit adds sysfs entries to the phy device that allow this. The sysfs for the phy device can be found at: /sys/bus/platform/drivers/brcmstb-usb-phy/*.usb-phy The following sysfs entries were added: - "dr_mode" (RO) - The current phy "dr_mode" setting. It will be set to one of the following values: - "host" - host mode - "peripheral " - device mode - "drd" - switch between device and host mode based on installed device - "typec-pd" - device/host mode is controller by the USB Type-C PD protocol. If "dr_mode" is "drd" - "drd_select" (RW) - It will be set to one of the following values: - "host" - force host mode - "device" - force device mode - "auto" - allow normal auto selection of host/device based on inserted USB device Signed-off-by: Al Cooper --- drivers/phy/broadcom/phy-brcm-usb.c | 85 +++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/drivers/phy/broadcom/phy-brcm-usb.c b/drivers/phy/broadcom/phy-brcm-usb.c index cd18616..e30fde8 100644 --- a/drivers/phy/broadcom/phy-brcm-usb.c +++ b/drivers/phy/broadcom/phy-brcm-usb.c @@ -27,6 +27,8 @@ #include "phy-brcm-usb-init.h" +static DEFINE_MUTEX(sysfs_lock); + enum brcm_usb_phy_id { BRCM_USB_PHY_2_0 = 0, BRCM_USB_PHY_3_0, @@ -45,6 +47,12 @@ struct value_to_name_map { { USB_CTLR_MODE_TYPEC_PD, "typec-pd" } }; +static struct value_to_name_map brcm_dual_mode_to_name[] = { + { 0, "host" }, + { 1, "device" }, + { 2, "auto" }, +}; + struct brcm_usb_phy { struct phy *phy; unsigned int id; @@ -156,6 +164,73 @@ static int name_to_value(struct value_to_name_map *table, int count, return -EINVAL; } +static const char *value_to_name(struct value_to_name_map *table, int count, + int value) +{ + if (value >= count) + return "unknown"; + return table[value].name; +} + +static ssize_t dr_mode_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct brcm_usb_phy_data *priv = dev_get_drvdata(dev); + + return sprintf(buf, "%s\n", + value_to_name(&brcm_dr_mode_to_name[0], + ARRAY_SIZE(brcm_dr_mode_to_name), + priv->ini.mode)); +} +static DEVICE_ATTR_RO(dr_mode); + +static ssize_t dual_select_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t len) +{ + struct brcm_usb_phy_data *priv = dev_get_drvdata(dev); + int value; + int res; + + mutex_lock(&sysfs_lock); + res = name_to_value(&brcm_dual_mode_to_name[0], + ARRAY_SIZE(brcm_dual_mode_to_name), buf, &value); + if (!res) { + brcm_usb_init_set_dual_select(&priv->ini, value); + res = len; + } + mutex_unlock(&sysfs_lock); + return res; +} + +static ssize_t dual_select_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct brcm_usb_phy_data *priv = dev_get_drvdata(dev); + int value; + + mutex_lock(&sysfs_lock); + value = brcm_usb_init_get_dual_select(&priv->ini); + mutex_unlock(&sysfs_lock); + return sprintf(buf, "%s\n", + value_to_name(&brcm_dual_mode_to_name[0], + ARRAY_SIZE(brcm_dual_mode_to_name), + value)); +} +static DEVICE_ATTR_RW(dual_select); + +static struct attribute *brcm_usb_phy_attrs[] = { + &dev_attr_dr_mode.attr, + &dev_attr_dual_select.attr, + NULL +}; + +static const struct attribute_group brcm_usb_phy_group = { + .attrs = brcm_usb_phy_attrs, +}; + static int brcm_usb_phy_probe(struct platform_device *pdev) { struct resource *res; @@ -262,6 +337,16 @@ static int brcm_usb_phy_probe(struct platform_device *pdev) brcm_usb_init_ipp(&priv->ini); + /* + * Create sysfs entries for mode. + * Remove "dual_select" attribute if not in dual mode + */ + if (priv->ini.mode != USB_CTLR_MODE_DRD) + brcm_usb_phy_attrs[1] = NULL; + err = sysfs_create_group(&dev->kobj, &brcm_usb_phy_group); + if (err) + dev_warn(&pdev->dev, "Error creating sysfs attributes\n"); + /* start with everything off */ if (priv->has_xhci) brcm_usb_uninit_xhci(&priv->ini);