From patchwork Thu Mar 15 11:39:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 131766 Delivered-To: patch@linaro.org Received: by 10.46.84.17 with SMTP id i17csp997493ljb; Thu, 15 Mar 2018 04:41:00 -0700 (PDT) X-Google-Smtp-Source: AG47ELtT33Bkwh1+sDaHbKhwNSOeuovVCj7jmtctZGC0azO+BzlWosDkNox93Ni8MmQmPxl0w5JN X-Received: by 2002:a17:902:1c5:: with SMTP id b63-v6mr7637105plb.311.1521114060798; Thu, 15 Mar 2018 04:41:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521114060; cv=none; d=google.com; s=arc-20160816; b=aNaRFDmBFXBEsD46Z9ynorMQ5GN7nQstz04ral614+ArQ8xc8dv14NANDr9/W3D6X6 erKffZvkCAehQVPzScFjgc0O8NE3gF8zgxYD3VamdayP37ptL527qAswmthh26xUu6vL HGPobFq/3LPQNWTuu2Pke93AvOUP3gALCO+5Ni0ClxOPk/OwbOFGrjxYw98RiQz4dFG0 9FUwPjDiEGX7Y2DG5HSGtwNRynDPMoMfFdA5/j0iwQQ6+u+XpdlHiUyGWpnBCI7ynGo7 73JLjTIvcyUmu4CdvYDMvj11JS91RN9LAChgqgJHUZi4nve0E3ElGkOkpZTGwv7FjDZZ JgUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-filter :arc-authentication-results; bh=fX7pU0AKxMEf9WhcCbO70BVl9X5MiHf6nQo44tRP7DA=; b=A/Qz0NUyT226C3YW4IAKmp50Q5ObY9E82h3cWdK/gf2S8OMifeJVzohSNG7AN+WDoU 79ddEjps+cWlvSLUPNdCU2x0i3cuDiyTXxx1RbPFhZWJc1lB4DbyPkN7R/zZAQ+h/0Ny nDX9vlmT4mmpDgUWbjmzY1jVXxUt4oIUgcXx+Fxnmhvy8O7FlI14rZKZCIDPBON5Kk5N z9gNKOGUW3DLk9W0A+4khuzmKM6ah7CKwMbmtGJSY6fM/HO2WptRso3EGyiFXGWn/TXO +0KiGTuKyaEiExm+HP5mvwMItaohx+21BF8l4rzk96+1U+LIQ6xu9iYbgU+voOZkmkMW RVXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=oxnQ0VWu; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y12-v6si1770630plr.42.2018.03.15.04.41.00; Thu, 15 Mar 2018 04:41:00 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=oxnQ0VWu; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751924AbeCOLky (ORCPT + 28 others); Thu, 15 Mar 2018 07:40:54 -0400 Received: from conuserg-10.nifty.com ([210.131.2.77]:32521 "EHLO conuserg-10.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751467AbeCOLku (ORCPT ); Thu, 15 Mar 2018 07:40:50 -0400 Received: from pug.e01.socionext.com (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-10.nifty.com with ESMTP id w2FBe0AV027015; Thu, 15 Mar 2018 20:40:03 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-10.nifty.com w2FBe0AV027015 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1521114005; bh=fX7pU0AKxMEf9WhcCbO70BVl9X5MiHf6nQo44tRP7DA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oxnQ0VWuRBs3zqrOLgiI+S0xSbL4rVwGyAUNAypNOZFab6sghtws1OuNOusN0RoJ/ JzWELZjoMKu7BVMDWEoCXdZ8QGn4cWPPXWWUH5VO8HzOpnlHaYqGjpMA/ReOp7kdHr 3DxTffW6TJLXD3ByCag2xs5OfMnk+/1LG0xZJtTds5fz4ItEDbn/rnDvNAcs4VqWD6 Q5/njD6D+JniTtOKnuJVJQ68ICRD8QTsvIpLYIiefOsEKHbOmWLW6R9ebgl4FVhJRt JVdmbe0+KsjNY1T2da3b8YAm2lOOjC4Va0YnM3tQeabrDdydb2xDAbzZwpu1fFfa2F iNyX0XDPbO4vQ== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: Felipe Balbi , linux-usb@vger.kernel.org Cc: Masami Hiramatsu , Jassi Brar , Kunihiko Hayashi , Masahiro Yamada , devicetree@vger.kernel.org, Felipe Balbi , linux-kernel@vger.kernel.org, Rob Herring , Greg Kroah-Hartman , Mark Rutland Subject: [PATCH 2/2] usb: dwc3: add clock and resets Date: Thu, 15 Mar 2018 20:39:58 +0900 Message-Id: <1521113998-25052-2-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1521113998-25052-1-git-send-email-yamada.masahiro@socionext.com> References: <1521113998-25052-1-git-send-email-yamada.masahiro@socionext.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org dwc3-of-simple.c only handles arbitrary number of clocks and resets. They are both generic enough to be put into the dwc3 core. For simple cases, a nested node structure like follows: dwc3-glue { compatible = "foo,dwc3"; clocks = ...; resets = ...; ... dwc3 { compatible = "snps,dwc3"; ... }; } would be turned into a single node: dwc3 { compatible = "foo,dwc3", "snps,dwc3"; clocks = ...; resets = ...; ... } I inserted reset_control_deassert() and clk_enable() before the first register access, i.e. dwc3_cache_hwparams(). Signed-off-by: Masahiro Yamada --- Documentation/devicetree/bindings/usb/dwc3.txt | 2 + drivers/usb/dwc3/core.c | 127 ++++++++++++++++++++++++- drivers/usb/dwc3/core.h | 5 + 3 files changed, 132 insertions(+), 2 deletions(-) -- 2.7.4 diff --git a/Documentation/devicetree/bindings/usb/dwc3.txt b/Documentation/devicetree/bindings/usb/dwc3.txt index 44e8bab..67e9cfb 100644 --- a/Documentation/devicetree/bindings/usb/dwc3.txt +++ b/Documentation/devicetree/bindings/usb/dwc3.txt @@ -9,12 +9,14 @@ Required properties: - interrupts: Interrupts used by the dwc3 controller. Optional properties: + - clocks: list of phandle and clock specifier pairs - usb-phy : array of phandle for the PHY device. The first element in the array is expected to be a handle to the USB2/HS PHY and the second element is expected to be a handle to the USB3/SS PHY - phys: from the *Generic PHY* bindings - phy-names: from the *Generic PHY* bindings; supported names are "usb2-phy" or "usb3-phy". + - resets: list of phandle and reset specifier pairs - snps,usb3_lpm_capable: determines if platform is USB3 LPM capable - snps,disable_scramble_quirk: true when SW should disable data scrambling. Only really useful for FPGA builds. diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c index e9083a3..f17e4a9 100644 --- a/drivers/usb/dwc3/core.c +++ b/drivers/usb/dwc3/core.c @@ -8,6 +8,7 @@ * Sebastian Andrzej Siewior */ +#include #include #include #include @@ -24,6 +25,7 @@ #include #include #include +#include #include #include @@ -240,6 +242,74 @@ static int dwc3_core_soft_reset(struct dwc3 *dwc) return -ETIMEDOUT; } +static int dwc3_core_get_clks(struct dwc3 *dwc) +{ + struct device *dev = dwc->dev; + struct device_node *node = dev->of_node; + struct clk *clk; + int num_clks, i; + + num_clks = of_count_phandle_with_args(node, "clocks", "#clock-cells"); + if (num_clks <= 0) + return 0; + + dwc->num_clks = num_clks; + + dwc->clks = devm_kcalloc(dev, num_clks, sizeof(*dwc->clks), GFP_KERNEL); + if (!dwc->clks) + return -ENOMEM; + + for (i = 0; i < num_clks; i++) { + clk = of_clk_get(node, i); + if (IS_ERR(clk)) + goto put_clks; + dwc->clks[i] = clk; + } + + return 0; + +put_clks: + while (--i >= 0) + clk_put(dwc->clks[i]); + + return PTR_ERR(clk); +} + +static void dwc3_core_put_clks(struct dwc3 *dwc) +{ + int i; + + for (i = dwc->num_clks - 1; i >= 0; i--) + clk_put(dwc->clks[i]); +} + +static int dwc3_core_enable_clks(struct dwc3 *dwc) +{ + int ret, i; + + for (i = 0; i < dwc->num_clks; i++) { + ret = clk_prepare_enable(dwc->clks[i]); + if (ret) + goto disable_clks; + } + + return 0; + +disable_clks: + while (--i >= 0) + clk_disable_unprepare(dwc->clks[i]); + + return ret; +} + +static void dwc3_core_disable_clks(struct dwc3 *dwc) +{ + int i; + + for (i = dwc->num_clks - 1; i >= 0; i--) + clk_disable_unprepare(dwc->clks[i]); +} + /* * dwc3_frame_length_adjustment - Adjusts frame length if required * @dwc3: Pointer to our controller context structure @@ -641,6 +711,8 @@ static void dwc3_core_exit(struct dwc3 *dwc) usb_phy_set_suspend(dwc->usb3_phy, 1); phy_power_off(dwc->usb2_generic_phy); phy_power_off(dwc->usb3_generic_phy); + dwc3_core_disable_clks(dwc); + reset_control_assert(dwc->resets); } static bool dwc3_core_is_valid(struct dwc3 *dwc) @@ -1205,6 +1277,22 @@ static int dwc3_probe(struct platform_device *pdev) dwc3_get_properties(dwc); + dwc->resets = devm_reset_control_array_get_optional_shared(dev); + if (IS_ERR(dwc->resets)) + return PTR_ERR(dwc->resets); + + ret = dwc3_core_get_clks(dwc); + if (ret) + return ret; + + ret = reset_control_deassert(dwc->resets); + if (ret) + goto put_clks; + + ret = dwc3_core_enable_clks(dwc); + if (ret) + goto assert_resets; + platform_set_drvdata(pdev, dwc); dwc3_cache_hwparams(dwc); @@ -1268,6 +1356,14 @@ static int dwc3_probe(struct platform_device *pdev) pm_runtime_put_sync(&pdev->dev); pm_runtime_disable(&pdev->dev); + dwc3_core_disable_clks(dwc); + +assert_resets: + reset_control_assert(dwc->resets); + +put_clks: + dwc3_core_put_clks(dwc); + return ret; } @@ -1289,11 +1385,38 @@ static int dwc3_remove(struct platform_device *pdev) dwc3_free_event_buffers(dwc); dwc3_free_scratch_buffers(dwc); + dwc3_core_put_clks(dwc); return 0; } #ifdef CONFIG_PM +static int dwc3_core_init_for_resume(struct dwc3 *dwc) +{ + int ret; + + ret = reset_control_deassert(dwc->resets); + if (ret) + return ret; + + ret = dwc3_core_enable_clks(dwc); + if (ret) + goto assert_resets; + + ret = dwc3_core_init(dwc); + if (ret) + goto disable_clks; + + return 0; + +disable_clks: + dwc3_core_disable_clks(dwc); +assert_resets: + reset_control_assert(dwc->resets); + + return ret; +} + static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg) { unsigned long flags; @@ -1325,7 +1448,7 @@ static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg) switch (dwc->current_dr_role) { case DWC3_GCTL_PRTCAP_DEVICE: - ret = dwc3_core_init(dwc); + ret = dwc3_core_init_for_resume(dwc); if (ret) return ret; @@ -1336,7 +1459,7 @@ static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg) case DWC3_GCTL_PRTCAP_HOST: /* nothing to do on host runtime_resume */ if (!PMSG_IS_AUTO(msg)) { - ret = dwc3_core_init(dwc); + ret = dwc3_core_init_for_resume(dwc); if (ret) return ret; } diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h index 860d2bc..14cd335 100644 --- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -891,6 +891,11 @@ struct dwc3 { struct usb_gadget gadget; struct usb_gadget_driver *gadget_driver; + struct clk **clks; + int num_clks; + + struct reset_control *resets; + struct usb_phy *usb2_phy; struct usb_phy *usb3_phy;