[PATCH v2 2/2] usb: dwc3: core: Support the dwc3 host suspend/resume
From: Baolin Wang
Date: Mon Nov 21 2016 - 01:28:49 EST
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.
Signed-off-by: Baolin Wang <baolin.wang@xxxxxxxxxx>
---
Changes since v1:
- Add pm_runtime.h head file to avoid kbuild error.
---
drivers/usb/dwc3/Kconfig | 7 ++++++
drivers/usb/dwc3/core.c | 26 +++++++++++++++++++++-
drivers/usb/dwc3/core.h | 15 +++++++++++++
drivers/usb/dwc3/host.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 101 insertions(+), 1 deletion(-)
diff --git a/drivers/usb/dwc3/Kconfig b/drivers/usb/dwc3/Kconfig
index a45b4f1..47bb2f3 100644
--- a/drivers/usb/dwc3/Kconfig
+++ b/drivers/usb/dwc3/Kconfig
@@ -47,6 +47,13 @@ config USB_DWC3_DUAL_ROLE
endchoice
+config USB_DWC3_HOST_SUSPEND
+ bool "Choose if the DWC3 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.
+
comment "Platform Glue Driver Support"
config USB_DWC3_OMAP
diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
index 9a4a5e4..7ad4bc3 100644
--- a/drivers/usb/dwc3/core.c
+++ b/drivers/usb/dwc3/core.c
@@ -1091,6 +1091,7 @@ static int dwc3_probe(struct platform_device *pdev)
pm_runtime_use_autosuspend(dev);
pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
pm_runtime_enable(dev);
+ pm_suspend_ignore_children(dev, true);
ret = pm_runtime_get_sync(dev);
if (ret < 0)
goto err1;
@@ -1215,15 +1216,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;
@@ -1245,12 +1258,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 b585a30..db41908 100644
--- a/drivers/usb/dwc3/core.h
+++ b/drivers/usb/dwc3/core.h
@@ -1226,4 +1226,19 @@ static inline void dwc3_ulpi_exit(struct dwc3 *dwc)
{ }
#endif
+#if IS_ENABLED(CONFIG_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 ed82464..20e84fc 100644
--- a/drivers/usb/dwc3/host.c
+++ b/drivers/usb/dwc3/host.c
@@ -16,9 +16,13 @@
*/
#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
#include "core.h"
+#define DWC3_HOST_SUSPEND_COUNT 100
+#define DWC3_HOST_SUSPEND_TIMEOUT 100
+
static int dwc3_host_get_irq(struct dwc3 *dwc)
{
struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
@@ -130,3 +134,53 @@ void dwc3_host_exit(struct dwc3 *dwc)
dev_name(&dwc->xhci->dev));
platform_device_unregister(dwc->xhci);
}
+
+#ifdef CONFIG_USB_DWC3_HOST_SUSPEND
+int dwc3_host_suspend(struct dwc3 *dwc)
+{
+ struct device *xhci = &dwc->xhci->dev;
+ int ret, cnt = DWC3_HOST_SUSPEND_COUNT;
+
+ /*
+ * We need make sure the children of the xHCI device had been into
+ * suspend state, or we will suspend xHCI device failed.
+ */
+ while (!pm_children_suspended(xhci) && --cnt > 0)
+ msleep(DWC3_HOST_SUSPEND_TIMEOUT);
+
+ if (cnt <= 0) {
+ dev_err(xhci, "failed to suspend xHCI children device\n");
+ return -EBUSY;
+ }
+
+ /*
+ * If the xHCI device had been into suspend state, thus just return.
+ */
+ if (pm_runtime_suspended(xhci))
+ return 0;
+
+ /* Suspend the xHCI device synchronously. */
+ ret = pm_runtime_put_sync(xhci);
+ if (ret) {
+ dev_err(xhci, "failed to suspend xHCI device\n");
+ return ret;
+ }
+
+ return 0;
+}
+
+int dwc3_host_resume(struct dwc3 *dwc)
+{
+ struct device *xhci = &dwc->xhci->dev;
+ int ret;
+
+ /* Resume the xHCI device synchronously. */
+ ret = pm_runtime_get_sync(xhci);
+ if (ret) {
+ dev_err(xhci, "failed to resume xHCI device\n");
+ return ret;
+ }
+
+ return 0;
+}
+#endif
--
1.7.9.5