From patchwork Fri Jul 15 09:13:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\(Exiting\) Baolin Wang" X-Patchwork-Id: 72078 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp510336qga; Fri, 15 Jul 2016 02:15:53 -0700 (PDT) X-Received: by 10.66.41.133 with SMTP id f5mr30595686pal.70.1468574153297; Fri, 15 Jul 2016 02:15:53 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g87si5122486pfd.62.2016.07.15.02.15.53; Fri, 15 Jul 2016 02:15:53 -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=@linaro.org; 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; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932530AbcGOJPv (ORCPT + 29 others); Fri, 15 Jul 2016 05:15:51 -0400 Received: from mail-pf0-f176.google.com ([209.85.192.176]:34720 "EHLO mail-pf0-f176.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932312AbcGOJOm (ORCPT ); Fri, 15 Jul 2016 05:14:42 -0400 Received: by mail-pf0-f176.google.com with SMTP id p64so5931118pfb.1 for ; Fri, 15 Jul 2016 02:14:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=rf4bDQvRLtkkiALbgGm958LXGwWtGn1zGDBwFxGa9JE=; b=hO83qQEsQpiMF//HnDBJgIKDZq5ZD/XhjXBb+oJze3aUKU0feZBuFExwhQvXB4LeiR HM3SUChTmb0iYSvJDkCoswUelS0Lmdl3FKP9CYzMFOQi8XWc0C6tknprRqNBrJDIDGrm oNnu2sywT/z6gzDD2ITSjvmqz6YzyvKBYGG2o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=rf4bDQvRLtkkiALbgGm958LXGwWtGn1zGDBwFxGa9JE=; b=l36Y4GRfgilyaowFoNk+rjaPw/tt7gCrU4EiDtpVmFwrFEu35lu2b766YvWRpYVshN YUBSiaURndBnPssCwL0W3SHr3mqFPdS2ddD3RMeLp3CUkIXK/+rcwU0Gm2JeM0j0mTmd LD51eYVKwDNUEl+4UTfIL/bs6ccDWvpzGFeyLMbvUMv8aWwhqWfR/UACMqAWrxaH5f3d PROb02kqRtpH7fd+P9NvudubpYJimjznXEokE8fF3Rm3Zhzld0TJFTzQuPc9jmcfyYel +4P/vhq2Qlo6jEfwnbe3IyfEEplCqwdnqtoibxxwW6/M7owscjdELUY8IoSYidkbJfZc GMMg== X-Gm-Message-State: ALyK8tKmLa16SnWI3DtVwLzRXWUC6BK/idpJ+OuLx7A6aGxCu/kBB4X4cQxAotA0dOZNd/bk X-Received: by 10.98.72.201 with SMTP id q70mr19820289pfi.159.1468574081835; Fri, 15 Jul 2016 02:14:41 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([175.111.195.49]) by smtp.gmail.com with ESMTPSA id da2sm1820199pad.3.2016.07.15.02.14.38 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 15 Jul 2016 02:14:41 -0700 (PDT) From: Baolin Wang To: balbi@kernel.org, gregkh@linuxfoundation.org Cc: mathias.nyman@intel.com, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, broonie@kernel.org, baolin.wang@linaro.org Subject: [PATCH 4/4] usb: dwc3: core: Support the dwc3 host suspend/resume Date: Fri, 15 Jul 2016 17:13:28 +0800 Message-Id: <9cca1b5a0487676a4fa912e957a03642a330c20a.1468571634.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org For some mobile devices with strict power management, we also want to suspend the host when the slave is detached for power saving. Thus we add the host suspend/resume functions to support this requirement, and we also should enable the 'XHCI_SLOW_SUSPEND' quirk for extraordinary delay when suspending the xhci. Signed-off-by: Baolin Wang --- drivers/usb/dwc3/Kconfig | 7 +++++++ drivers/usb/dwc3/core.c | 25 ++++++++++++++++++++++++- drivers/usb/dwc3/core.h | 15 +++++++++++++++ drivers/usb/dwc3/host.c | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) -- 1.7.9.5 diff --git a/drivers/usb/dwc3/Kconfig b/drivers/usb/dwc3/Kconfig index a64ce1c..725d2bd 100644 --- a/drivers/usb/dwc3/Kconfig +++ b/drivers/usb/dwc3/Kconfig @@ -45,6 +45,13 @@ config USB_DWC3_DUAL_ROLE This is the default mode of working of DWC3 controller where both host and gadget features are enabled. +config USB_DWC3_HOST_SUSPEND + bool "Choose if the host (xhci) can be suspend/resume" + depends on USB_DWC3_HOST=y || USB_DWC3_DUAL_ROLE=y + help + We can suspend the host when the slave is detached for power saving, + and resume the host when one slave is attached. + endchoice comment "Platform Glue Driver Support" diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c index 1485480..5140b4d 100644 --- a/drivers/usb/dwc3/core.c +++ b/drivers/usb/dwc3/core.c @@ -1103,15 +1103,27 @@ static int dwc3_remove(struct platform_device *pdev) static int dwc3_suspend_common(struct dwc3 *dwc) { unsigned long flags; + int ret; switch (dwc->dr_mode) { case USB_DR_MODE_PERIPHERAL: + spin_lock_irqsave(&dwc->lock, flags); + dwc3_gadget_suspend(dwc); + spin_unlock_irqrestore(&dwc->lock, flags); + break; case USB_DR_MODE_OTG: + ret = dwc3_host_suspend(dwc); + if (ret) + return ret; + spin_lock_irqsave(&dwc->lock, flags); dwc3_gadget_suspend(dwc); spin_unlock_irqrestore(&dwc->lock, flags); break; case USB_DR_MODE_HOST: + ret = dwc3_host_suspend(dwc); + if (ret) + return ret; default: /* do nothing */ break; @@ -1133,12 +1145,23 @@ static int dwc3_resume_common(struct dwc3 *dwc) switch (dwc->dr_mode) { case USB_DR_MODE_PERIPHERAL: + spin_lock_irqsave(&dwc->lock, flags); + dwc3_gadget_resume(dwc); + spin_unlock_irqrestore(&dwc->lock, flags); + break; case USB_DR_MODE_OTG: + ret = dwc3_host_resume(dwc); + if (ret) + return ret; + spin_lock_irqsave(&dwc->lock, flags); dwc3_gadget_resume(dwc); spin_unlock_irqrestore(&dwc->lock, flags); - /* FALLTHROUGH */ + break; case USB_DR_MODE_HOST: + ret = dwc3_host_resume(dwc); + if (ret) + return ret; default: /* do nothing */ break; diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h index 45d6de5..0ba203e 100644 --- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -1177,4 +1177,19 @@ static inline void dwc3_ulpi_exit(struct dwc3 *dwc) { } #endif +#if IS_ENABLED(USB_DWC3_HOST_SUSPEND) +int dwc3_host_suspend(struct dwc3 *dwc); +int dwc3_host_resume(struct dwc3 *dwc); +#else +static inline int dwc3_host_suspend(struct dwc3 *dwc) +{ + return 0; +} + +static inline int dwc3_host_resume(struct dwc3 *dwc) +{ + return 0; +} +#endif + #endif /* __DRIVERS_USB_DWC3_CORE_H */ diff --git a/drivers/usb/dwc3/host.c b/drivers/usb/dwc3/host.c index 2e960ed..2ec3eff 100644 --- a/drivers/usb/dwc3/host.c +++ b/drivers/usb/dwc3/host.c @@ -17,8 +17,11 @@ #include #include +#include +#include #include "core.h" +#include "../host/xhci.h" int dwc3_host_init(struct dwc3 *dwc) { @@ -91,6 +94,8 @@ int dwc3_host_init(struct dwc3 *dwc) memset(&pdata, 0, sizeof(pdata)); pdata.usb3_lpm_capable = dwc->usb3_lpm_capable; + /* dwc3 controller need an extraordinary delay when suspending xhci. */ + pdata.usb3_slow_suspend = 1; ret = platform_device_add_data(xhci, &pdata, sizeof(pdata)); if (ret) { @@ -128,3 +133,30 @@ void dwc3_host_exit(struct dwc3 *dwc) dev_name(&dwc->xhci->dev)); platform_device_unregister(dwc->xhci); } + +int dwc3_host_suspend(struct dwc3 *dwc) +{ + struct usb_hcd *hcd = dev_get_drvdata(&dwc->xhci->dev); + struct xhci_hcd *xhci = hcd_to_xhci(hcd); + int ret, cnt = 20; + +try_again: + /* We should wait for xhci bus has been into suspend mode firstly. */ + ret = xhci_suspend(xhci, device_may_wakeup(&dwc->xhci->dev)); + if (ret && --cnt > 0) { + dev_warn(dwc->dev, "xhci suspend failed %d, try again...\n", + ret); + msleep(200); + goto try_again; + } + + return ret; +} + +int dwc3_host_resume(struct dwc3 *dwc) +{ + struct usb_hcd *hcd = dev_get_drvdata(&dwc->xhci->dev); + struct xhci_hcd *xhci = hcd_to_xhci(hcd); + + return xhci_resume(xhci, 0); +}