diff mbox

[v5,1/2] usb: host: plat: Enable xhci plat runtime PM

Message ID d7a3bb017581435bdb1e726c2b7260d85e1f5611.1481532028.git.baolin.wang@linaro.org
State New
Headers show

Commit Message

(Exiting) Baolin Wang Dec. 13, 2016, 7:49 a.m. UTC
Enable the xhci plat runtime PM for parent device to suspend/resume xhci.
Also call pm_runtime_get_noresume() in probe() function in case the parent
device doesn't call suspend/resume callback by runtime PM now.

Signed-off-by: Baolin Wang <baolin.wang@linaro.org>

---
Changes since v4:
 - No updates.

Changes since v3:
 - Fix kbuild error.

Changes since v2:
 - Add pm_runtime_get_noresume() in probe() function.
 - Add pm_runtime_set_suspended()/pm_runtime_put_noidle() in remove() function.

Changes since v1:
 - No updates.
---
 drivers/usb/host/xhci-plat.c |   41 ++++++++++++++++++++++++++++++++++++-----
 1 file changed, 36 insertions(+), 5 deletions(-)

-- 
1.7.9.5

Comments

Felipe Balbi Jan. 16, 2017, 10:28 a.m. UTC | #1
Hi,

Baolin Wang <baolin.wang@linaro.org> writes:
> 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@linaro.org>

> ---

> Changes since v4:

>  - Remove Kconfig and just enable host suspend/resume.

>  - Simplify the dwc3_host_suspend/resume() function.

>

> Changes since v3:

>  - No updates.

>

> Changes since v2:

>  - Remove pm_children_suspended() and other unused macros.

>

> Changes since v1:

>  - Add pm_runtime.h head file to avoid kbuild error.

> ---

>  drivers/usb/dwc3/core.c |   26 +++++++++++++++++++++++++-

>  drivers/usb/dwc3/core.h |    7 +++++++

>  drivers/usb/dwc3/host.c |   21 +++++++++++++++++++++

>  3 files changed, 53 insertions(+), 1 deletion(-)

>

> 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..1099168 100644

> --- a/drivers/usb/dwc3/core.h

> +++ b/drivers/usb/dwc3/core.h

> @@ -1158,11 +1158,18 @@ static inline bool dwc3_is_usb31(struct dwc3 *dwc)

>  #if IS_ENABLED(CONFIG_USB_DWC3_HOST) || IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)

>  int dwc3_host_init(struct dwc3 *dwc);

>  void dwc3_host_exit(struct dwc3 *dwc);

> +int dwc3_host_suspend(struct dwc3 *dwc);

> +int dwc3_host_resume(struct dwc3 *dwc);

>  #else

>  static inline int dwc3_host_init(struct dwc3 *dwc)

>  { return 0; }

>  static inline void dwc3_host_exit(struct dwc3 *dwc)

>  { }

> +

> +static inline int dwc3_host_suspend(struct dwc3 *dwc)

> +{ return 0; }

> +static inline int dwc3_host_resume(struct dwc3 *dwc)

> +{ return 0; }

>  #endif

>  

>  #if IS_ENABLED(CONFIG_USB_DWC3_GADGET) || IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)

> diff --git a/drivers/usb/dwc3/host.c b/drivers/usb/dwc3/host.c

> index ed82464..7959ef0 100644

> --- a/drivers/usb/dwc3/host.c

> +++ b/drivers/usb/dwc3/host.c

> @@ -16,6 +16,7 @@

>   */

>  

>  #include <linux/platform_device.h>

> +#include <linux/pm_runtime.h>

>  

>  #include "core.h"

>  

> @@ -130,3 +131,23 @@ 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 device *xhci = &dwc->xhci->dev;

> +

> +	/*

> +	 * Note: if we get the -EBUSY, which means the xHCI children devices are

> +	 * not in suspend state yet, the glue layer need to wait for a while and

> +	 * try to suspend xHCI device again.

> +	 */

> +	return pm_runtime_put_sync(xhci);

> +}

> +

> +int dwc3_host_resume(struct dwc3 *dwc)

> +{

> +	struct device *xhci = &dwc->xhci->dev;

> +

> +	/* Resume the xHCI device synchronously. */

> +	return pm_runtime_get_sync(xhci);

> +}

> -- 

> 1.7.9.5


I can't take this without Mathias taking xhci side of things.

-- 
balbi
(Exiting) Baolin Wang Jan. 16, 2017, 10:53 a.m. UTC | #2
Hi,

On 16 January 2017 at 18:28, Felipe Balbi <balbi@kernel.org> wrote:
>

> Hi,

>

> Baolin Wang <baolin.wang@linaro.org> writes:

>> 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@linaro.org>

>> ---

>> Changes since v4:

>>  - Remove Kconfig and just enable host suspend/resume.

>>  - Simplify the dwc3_host_suspend/resume() function.

>>

>> Changes since v3:

>>  - No updates.

>>

>> Changes since v2:

>>  - Remove pm_children_suspended() and other unused macros.

>>

>> Changes since v1:

>>  - Add pm_runtime.h head file to avoid kbuild error.

>> ---

>>  drivers/usb/dwc3/core.c |   26 +++++++++++++++++++++++++-

>>  drivers/usb/dwc3/core.h |    7 +++++++

>>  drivers/usb/dwc3/host.c |   21 +++++++++++++++++++++

>>  3 files changed, 53 insertions(+), 1 deletion(-)

>>

>> 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..1099168 100644

>> --- a/drivers/usb/dwc3/core.h

>> +++ b/drivers/usb/dwc3/core.h

>> @@ -1158,11 +1158,18 @@ static inline bool dwc3_is_usb31(struct dwc3 *dwc)

>>  #if IS_ENABLED(CONFIG_USB_DWC3_HOST) || IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)

>>  int dwc3_host_init(struct dwc3 *dwc);

>>  void dwc3_host_exit(struct dwc3 *dwc);

>> +int dwc3_host_suspend(struct dwc3 *dwc);

>> +int dwc3_host_resume(struct dwc3 *dwc);

>>  #else

>>  static inline int dwc3_host_init(struct dwc3 *dwc)

>>  { return 0; }

>>  static inline void dwc3_host_exit(struct dwc3 *dwc)

>>  { }

>> +

>> +static inline int dwc3_host_suspend(struct dwc3 *dwc)

>> +{ return 0; }

>> +static inline int dwc3_host_resume(struct dwc3 *dwc)

>> +{ return 0; }

>>  #endif

>>

>>  #if IS_ENABLED(CONFIG_USB_DWC3_GADGET) || IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)

>> diff --git a/drivers/usb/dwc3/host.c b/drivers/usb/dwc3/host.c

>> index ed82464..7959ef0 100644

>> --- a/drivers/usb/dwc3/host.c

>> +++ b/drivers/usb/dwc3/host.c

>> @@ -16,6 +16,7 @@

>>   */

>>

>>  #include <linux/platform_device.h>

>> +#include <linux/pm_runtime.h>

>>

>>  #include "core.h"

>>

>> @@ -130,3 +131,23 @@ 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 device *xhci = &dwc->xhci->dev;

>> +

>> +     /*

>> +      * Note: if we get the -EBUSY, which means the xHCI children devices are

>> +      * not in suspend state yet, the glue layer need to wait for a while and

>> +      * try to suspend xHCI device again.

>> +      */

>> +     return pm_runtime_put_sync(xhci);

>> +}

>> +

>> +int dwc3_host_resume(struct dwc3 *dwc)

>> +{

>> +     struct device *xhci = &dwc->xhci->dev;

>> +

>> +     /* Resume the xHCI device synchronously. */

>> +     return pm_runtime_get_sync(xhci);

>> +}

>> --

>> 1.7.9.5

>

> I can't take this without Mathias taking xhci side of things.


Okay, I will wait for Mathias' comments. Thanks.

-- 
Baolin.wang
Best Regards
(Exiting) Baolin Wang Jan. 16, 2017, 10:56 a.m. UTC | #3
Hi Mathias,

On 13 December 2016 at 15:49, Baolin Wang <baolin.wang@linaro.org> wrote:
> Enable the xhci plat runtime PM for parent device to suspend/resume xhci.

> Also call pm_runtime_get_noresume() in probe() function in case the parent

> device doesn't call suspend/resume callback by runtime PM now.

>

> Signed-off-by: Baolin Wang <baolin.wang@linaro.org>

> ---

> Changes since v4:

>  - No updates.

>

> Changes since v3:

>  - Fix kbuild error.

>

> Changes since v2:

>  - Add pm_runtime_get_noresume() in probe() function.

>  - Add pm_runtime_set_suspended()/pm_runtime_put_noidle() in remove() function.

>

> Changes since v1:

>  - No updates.

> ---


Do you have any comments about this patch? Thanks.

>  drivers/usb/host/xhci-plat.c |   41 ++++++++++++++++++++++++++++++++++++-----

>  1 file changed, 36 insertions(+), 5 deletions(-)

>

> diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c

> index ed56bf9..5805c6a 100644

> --- a/drivers/usb/host/xhci-plat.c

> +++ b/drivers/usb/host/xhci-plat.c

> @@ -246,6 +246,10 @@ static int xhci_plat_probe(struct platform_device *pdev)

>         if (ret)

>                 goto dealloc_usb2_hcd;

>

> +       pm_runtime_get_noresume(&pdev->dev);

> +       pm_runtime_set_active(&pdev->dev);

> +       pm_runtime_enable(&pdev->dev);

> +

>         return 0;

>

>

> @@ -274,6 +278,10 @@ static int xhci_plat_remove(struct platform_device *dev)

>         struct xhci_hcd *xhci = hcd_to_xhci(hcd);

>         struct clk *clk = xhci->clk;

>

> +       pm_runtime_set_suspended(&dev->dev);

> +       pm_runtime_put_noidle(&dev->dev);

> +       pm_runtime_disable(&dev->dev);

> +

>         usb_remove_hcd(xhci->shared_hcd);

>         usb_phy_shutdown(hcd->usb_phy);

>

> @@ -311,14 +319,37 @@ static int xhci_plat_resume(struct device *dev)

>

>         return xhci_resume(xhci, 0);

>  }

> +#endif /* CONFIG_PM_SLEEP */

> +

> +#ifdef CONFIG_PM

> +static int xhci_plat_runtime_suspend(struct device *dev)

> +{

> +       struct usb_hcd  *hcd = dev_get_drvdata(dev);

> +       struct xhci_hcd *xhci = hcd_to_xhci(hcd);

> +

> +       return xhci_suspend(xhci, device_may_wakeup(dev));

> +}

> +

> +static int xhci_plat_runtime_resume(struct device *dev)

> +{

> +       struct usb_hcd  *hcd = dev_get_drvdata(dev);

> +       struct xhci_hcd *xhci = hcd_to_xhci(hcd);

> +

> +       return xhci_resume(xhci, 0);

> +}

> +

> +static int xhci_plat_runtime_idle(struct device *dev)

> +{

> +       return 0;

> +}

> +#endif /* CONFIG_PM */

>

>  static const struct dev_pm_ops xhci_plat_pm_ops = {

>         SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)

> +

> +       SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend, xhci_plat_runtime_resume,

> +                          xhci_plat_runtime_idle)

>  };

> -#define DEV_PM_OPS     (&xhci_plat_pm_ops)

> -#else

> -#define DEV_PM_OPS     NULL

> -#endif /* CONFIG_PM */

>

>  static const struct acpi_device_id usb_xhci_acpi_match[] = {

>         /* XHCI-compliant USB Controller */

> @@ -332,7 +363,7 @@ static int xhci_plat_resume(struct device *dev)

>         .remove = xhci_plat_remove,

>         .driver = {

>                 .name = "xhci-hcd",

> -               .pm = DEV_PM_OPS,

> +               .pm = &xhci_plat_pm_ops,

>                 .of_match_table = of_match_ptr(usb_xhci_of_match),

>                 .acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),

>         },

> --

> 1.7.9.5

>




-- 
Baolin.wang
Best Regards
Robert Foss Jan. 25, 2017, 5:30 p.m. UTC | #4
Looks good to me.

Feel free to add my r-b.

On 2016-12-13 02:49 AM, Baolin Wang wrote:
> Enable the xhci plat runtime PM for parent device to suspend/resume xhci.

> Also call pm_runtime_get_noresume() in probe() function in case the parent

> device doesn't call suspend/resume callback by runtime PM now.

>

> Signed-off-by: Baolin Wang <baolin.wang@linaro.org>

> ---

> Changes since v4:

>  - No updates.

>

> Changes since v3:

>  - Fix kbuild error.

>

> Changes since v2:

>  - Add pm_runtime_get_noresume() in probe() function.

>  - Add pm_runtime_set_suspended()/pm_runtime_put_noidle() in remove() function.

>

> Changes since v1:

>  - No updates.

> ---

>  drivers/usb/host/xhci-plat.c |   41 ++++++++++++++++++++++++++++++++++++-----

>  1 file changed, 36 insertions(+), 5 deletions(-)

>

> diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c

> index ed56bf9..5805c6a 100644

> --- a/drivers/usb/host/xhci-plat.c

> +++ b/drivers/usb/host/xhci-plat.c

> @@ -246,6 +246,10 @@ static int xhci_plat_probe(struct platform_device *pdev)

>  	if (ret)

>  		goto dealloc_usb2_hcd;

>

> +	pm_runtime_get_noresume(&pdev->dev);

> +	pm_runtime_set_active(&pdev->dev);

> +	pm_runtime_enable(&pdev->dev);

> +

>  	return 0;

>

>

> @@ -274,6 +278,10 @@ static int xhci_plat_remove(struct platform_device *dev)

>  	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);

>  	struct clk *clk = xhci->clk;

>

> +	pm_runtime_set_suspended(&dev->dev);

> +	pm_runtime_put_noidle(&dev->dev);

> +	pm_runtime_disable(&dev->dev);

> +

>  	usb_remove_hcd(xhci->shared_hcd);

>  	usb_phy_shutdown(hcd->usb_phy);

>

> @@ -311,14 +319,37 @@ static int xhci_plat_resume(struct device *dev)

>

>  	return xhci_resume(xhci, 0);

>  }

> +#endif /* CONFIG_PM_SLEEP */

> +

> +#ifdef CONFIG_PM

> +static int xhci_plat_runtime_suspend(struct device *dev)

> +{

> +	struct usb_hcd  *hcd = dev_get_drvdata(dev);

> +	struct xhci_hcd *xhci = hcd_to_xhci(hcd);

> +

> +	return xhci_suspend(xhci, device_may_wakeup(dev));

> +}

> +

> +static int xhci_plat_runtime_resume(struct device *dev)

> +{

> +	struct usb_hcd  *hcd = dev_get_drvdata(dev);

> +	struct xhci_hcd *xhci = hcd_to_xhci(hcd);

> +

> +	return xhci_resume(xhci, 0);

> +}

> +

> +static int xhci_plat_runtime_idle(struct device *dev)

> +{

> +	return 0;

> +}

> +#endif /* CONFIG_PM */

>

>  static const struct dev_pm_ops xhci_plat_pm_ops = {

>  	SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)

> +

> +	SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend, xhci_plat_runtime_resume,

> +			   xhci_plat_runtime_idle)

>  };

> -#define DEV_PM_OPS	(&xhci_plat_pm_ops)

> -#else

> -#define DEV_PM_OPS	NULL

> -#endif /* CONFIG_PM */

>

>  static const struct acpi_device_id usb_xhci_acpi_match[] = {

>  	/* XHCI-compliant USB Controller */

> @@ -332,7 +363,7 @@ static int xhci_plat_resume(struct device *dev)

>  	.remove	= xhci_plat_remove,

>  	.driver	= {

>  		.name = "xhci-hcd",

> -		.pm = DEV_PM_OPS,

> +		.pm = &xhci_plat_pm_ops,

>  		.of_match_table = of_match_ptr(usb_xhci_of_match),

>  		.acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),

>  	},

>
Robert Foss Jan. 25, 2017, 5:31 p.m. UTC | #5
Looks good to me.

Feel free to add my r-b.


Rob.

On 2016-12-13 02:49 AM, Baolin Wang wrote:
> 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@linaro.org>

> ---

> Changes since v4:

>  - Remove Kconfig and just enable host suspend/resume.

>  - Simplify the dwc3_host_suspend/resume() function.

>

> Changes since v3:

>  - No updates.

>

> Changes since v2:

>  - Remove pm_children_suspended() and other unused macros.

>

> Changes since v1:

>  - Add pm_runtime.h head file to avoid kbuild error.

> ---

>  drivers/usb/dwc3/core.c |   26 +++++++++++++++++++++++++-

>  drivers/usb/dwc3/core.h |    7 +++++++

>  drivers/usb/dwc3/host.c |   21 +++++++++++++++++++++

>  3 files changed, 53 insertions(+), 1 deletion(-)

>

> 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..1099168 100644

> --- a/drivers/usb/dwc3/core.h

> +++ b/drivers/usb/dwc3/core.h

> @@ -1158,11 +1158,18 @@ static inline bool dwc3_is_usb31(struct dwc3 *dwc)

>  #if IS_ENABLED(CONFIG_USB_DWC3_HOST) || IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)

>  int dwc3_host_init(struct dwc3 *dwc);

>  void dwc3_host_exit(struct dwc3 *dwc);

> +int dwc3_host_suspend(struct dwc3 *dwc);

> +int dwc3_host_resume(struct dwc3 *dwc);

>  #else

>  static inline int dwc3_host_init(struct dwc3 *dwc)

>  { return 0; }

>  static inline void dwc3_host_exit(struct dwc3 *dwc)

>  { }

> +

> +static inline int dwc3_host_suspend(struct dwc3 *dwc)

> +{ return 0; }

> +static inline int dwc3_host_resume(struct dwc3 *dwc)

> +{ return 0; }

>  #endif

>

>  #if IS_ENABLED(CONFIG_USB_DWC3_GADGET) || IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)

> diff --git a/drivers/usb/dwc3/host.c b/drivers/usb/dwc3/host.c

> index ed82464..7959ef0 100644

> --- a/drivers/usb/dwc3/host.c

> +++ b/drivers/usb/dwc3/host.c

> @@ -16,6 +16,7 @@

>   */

>

>  #include <linux/platform_device.h>

> +#include <linux/pm_runtime.h>

>

>  #include "core.h"

>

> @@ -130,3 +131,23 @@ 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 device *xhci = &dwc->xhci->dev;

> +

> +	/*

> +	 * Note: if we get the -EBUSY, which means the xHCI children devices are

> +	 * not in suspend state yet, the glue layer need to wait for a while and

> +	 * try to suspend xHCI device again.

> +	 */

> +	return pm_runtime_put_sync(xhci);

> +}

> +

> +int dwc3_host_resume(struct dwc3 *dwc)

> +{

> +	struct device *xhci = &dwc->xhci->dev;

> +

> +	/* Resume the xHCI device synchronously. */

> +	return pm_runtime_get_sync(xhci);

> +}

>
Mathias Nyman Jan. 31, 2017, 1:14 p.m. UTC | #6
On 16.01.2017 12:56, Baolin Wang wrote:
> Hi Mathias,


Hi

Sorry about the long review delay
CC Alan in case my pm assumptions need to be corrected

>

> On 13 December 2016 at 15:49, Baolin Wang <baolin.wang@linaro.org> wrote:

>> Enable the xhci plat runtime PM for parent device to suspend/resume xhci.

>> Also call pm_runtime_get_noresume() in probe() function in case the parent

>> device doesn't call suspend/resume callback by runtime PM now.

>>

>> Signed-off-by: Baolin Wang <baolin.wang@linaro.org>

>> ---

>> Changes since v4:

>>   - No updates.

>>

>> Changes since v3:

>>   - Fix kbuild error.

>>

>> Changes since v2:

>>   - Add pm_runtime_get_noresume() in probe() function.

>>   - Add pm_runtime_set_suspended()/pm_runtime_put_noidle() in remove() function.

>>

>> Changes since v1:

>>   - No updates.

>> ---

>

> Do you have any comments about this patch? Thanks.

>

>>   drivers/usb/host/xhci-plat.c |   41 ++++++++++++++++++++++++++++++++++++-----

>>   1 file changed, 36 insertions(+), 5 deletions(-)

>>

>> diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c

>> index ed56bf9..5805c6a 100644

>> --- a/drivers/usb/host/xhci-plat.c

>> +++ b/drivers/usb/host/xhci-plat.c

>> @@ -246,6 +246,10 @@ static int xhci_plat_probe(struct platform_device *pdev)

>>          if (ret)

>>                  goto dealloc_usb2_hcd;

>>

>> +       pm_runtime_get_noresume(&pdev->dev);

>> +       pm_runtime_set_active(&pdev->dev);

>> +       pm_runtime_enable(&pdev->dev);

>> +

>>          return 0;

>>

>>


To me this looks like we are not really enabling runtime pm for xhci, we increment the
usage count in probe, and keep it until remove is called.

This would normally prevent parent dwc3 from runtime suspending, but I see that in patch 2/2 adds
pm_suspend_ignore_children(dev, true) to dwc3, and, that dwc3 actually controls xhci runtime
pm by calling pm_runtime_get/put for xhci in its own dwc3 runtime_suspend/resume callbacks.

Looks a bit upside down to me, what's the reason for this?

what prevents calling pm_runtime_put_* before leaving probe in xhci and let pm code handle
the runtime suspend and parent/child relationships?

-Mathias
(Exiting) Baolin Wang Feb. 20, 2017, 2:47 a.m. UTC | #7
Hi Mathias,

On 6 February 2017 at 13:26, Baolin Wang <baolin.wang@linaro.org> wrote:
> Hi Mathias,

>

> On 31 January 2017 at 21:14, Mathias Nyman

> <mathias.nyman@linux.intel.com> wrote:

>> On 16.01.2017 12:56, Baolin Wang wrote:

>>>

>>> Hi Mathias,

>>

>>

>> Hi

>>

>> Sorry about the long review delay

>> CC Alan in case my pm assumptions need to be corrected

>>

>>

>>>

>>> On 13 December 2016 at 15:49, Baolin Wang <baolin.wang@linaro.org> wrote:

>>>>

>>>> Enable the xhci plat runtime PM for parent device to suspend/resume xhci.

>>>> Also call pm_runtime_get_noresume() in probe() function in case the

>>>> parent

>>>> device doesn't call suspend/resume callback by runtime PM now.

>>>>

>>>> Signed-off-by: Baolin Wang <baolin.wang@linaro.org>

>>>> ---

>>>> Changes since v4:

>>>>   - No updates.

>>>>

>>>> Changes since v3:

>>>>   - Fix kbuild error.

>>>>

>>>> Changes since v2:

>>>>   - Add pm_runtime_get_noresume() in probe() function.

>>>>   - Add pm_runtime_set_suspended()/pm_runtime_put_noidle() in remove()

>>>> function.

>>>>

>>>> Changes since v1:

>>>>   - No updates.

>>>> ---

>>>

>>>

>>> Do you have any comments about this patch? Thanks.

>>>

>>>>   drivers/usb/host/xhci-plat.c |   41

>>>> ++++++++++++++++++++++++++++++++++++-----

>>>>   1 file changed, 36 insertions(+), 5 deletions(-)

>>>>

>>>> diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c

>>>> index ed56bf9..5805c6a 100644

>>>> --- a/drivers/usb/host/xhci-plat.c

>>>> +++ b/drivers/usb/host/xhci-plat.c

>>>> @@ -246,6 +246,10 @@ static int xhci_plat_probe(struct platform_device

>>>> *pdev)

>>>>          if (ret)

>>>>                  goto dealloc_usb2_hcd;

>>>>

>>>> +       pm_runtime_get_noresume(&pdev->dev);

>>>> +       pm_runtime_set_active(&pdev->dev);

>>>> +       pm_runtime_enable(&pdev->dev);

>>>> +

>>>>          return 0;

>>>>

>>>>

>>

>> To me this looks like we are not really enabling runtime pm for xhci, we

>> increment the

>> usage count in probe, and keep it until remove is called.

>>

>> This would normally prevent parent dwc3 from runtime suspending, but I see

>> that in patch 2/2 adds

>> pm_suspend_ignore_children(dev, true) to dwc3, and, that dwc3 actually

>> controls xhci runtime

>> pm by calling pm_runtime_get/put for xhci in its own dwc3

>> runtime_suspend/resume callbacks.

>>

>> Looks a bit upside down to me, what's the reason for this?

>>

>> what prevents calling pm_runtime_put_* before leaving probe in xhci and let

>> pm code handle

>> the runtime suspend and parent/child relationships?

>

> When dwc3 controller is working on host mode, which will create and

> add the USB hcd for host side. Then if we want to suspend dwc3

> controller as host mode, the USB device (bus) of host side will

> runtime suspend automatically if there are no slave attached (by

> usb_runtime_suspend()--->usb_suspend_both()--->usb_suspend_interface()--->usb_suspend_device()--->generic_suspend()--->hcd_bus_suspend()--->xhci_bus_suspend()),

> but we should not suspend xHCI device (issuing xhci_suspend()) now,

> since some other  controllers did not implement the runtime PM to

> control xHCI device's runtime state, which will cause other

> controllers can not resume xHCI device (issuing xhci_resume()) if the

> xHCI device suspend automatically by its child device. Thus we should

> get the runtime count for xHCI device in xhci_plat_probe() in case

> xHCI device will also suspend automatically by its child device.

> According to that, for xHCI device's parent: dwc3 device, we should

> put the xHCI device's runtime count to suspend xHCI device manually.

>


Any more comments?

-- 
Baolin.wang
Best Regards
Mathias Nyman Feb. 20, 2017, 3:10 p.m. UTC | #8
On 20.02.2017 04:47, Baolin Wang wrote:
> Hi Mathias,

>

> On 6 February 2017 at 13:26, Baolin Wang <baolin.wang@linaro.org> wrote:

>> Hi Mathias,

>>

>> On 31 January 2017 at 21:14, Mathias Nyman

>> <mathias.nyman@linux.intel.com> wrote:

>>> On 16.01.2017 12:56, Baolin Wang wrote:

>>>>

>>>> Hi Mathias,

>>>

>>>

>>> Hi

>>>

>>> Sorry about the long review delay

>>> CC Alan in case my pm assumptions need to be corrected

>>>

>>>

>>>>

>>>> On 13 December 2016 at 15:49, Baolin Wang <baolin.wang@linaro.org> wrote:

>>>>>

>>>>> Enable the xhci plat runtime PM for parent device to suspend/resume xhci.

>>>>> Also call pm_runtime_get_noresume() in probe() function in case the

>>>>> parent

>>>>> device doesn't call suspend/resume callback by runtime PM now.

>>>>>

>>>>> Signed-off-by: Baolin Wang <baolin.wang@linaro.org>

>>>>> ---

>>>>> Changes since v4:

>>>>>    - No updates.

>>>>>

>>>>> Changes since v3:

>>>>>    - Fix kbuild error.

>>>>>

>>>>> Changes since v2:

>>>>>    - Add pm_runtime_get_noresume() in probe() function.

>>>>>    - Add pm_runtime_set_suspended()/pm_runtime_put_noidle() in remove()

>>>>> function.

>>>>>

>>>>> Changes since v1:

>>>>>    - No updates.

>>>>> ---

>>>>

>>>>

>>>> Do you have any comments about this patch? Thanks.

>>>>

>>>>>    drivers/usb/host/xhci-plat.c |   41

>>>>> ++++++++++++++++++++++++++++++++++++-----

>>>>>    1 file changed, 36 insertions(+), 5 deletions(-)

>>>>>

>>>>> diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c

>>>>> index ed56bf9..5805c6a 100644

>>>>> --- a/drivers/usb/host/xhci-plat.c

>>>>> +++ b/drivers/usb/host/xhci-plat.c

>>>>> @@ -246,6 +246,10 @@ static int xhci_plat_probe(struct platform_device

>>>>> *pdev)

>>>>>           if (ret)

>>>>>                   goto dealloc_usb2_hcd;

>>>>>

>>>>> +       pm_runtime_get_noresume(&pdev->dev);

>>>>> +       pm_runtime_set_active(&pdev->dev);

>>>>> +       pm_runtime_enable(&pdev->dev);

>>>>> +

>>>>>           return 0;

>>>>>

>>>>>

>>>

>>> To me this looks like we are not really enabling runtime pm for xhci, we

>>> increment the

>>> usage count in probe, and keep it until remove is called.

>>>

>>> This would normally prevent parent dwc3 from runtime suspending, but I see

>>> that in patch 2/2 adds

>>> pm_suspend_ignore_children(dev, true) to dwc3, and, that dwc3 actually

>>> controls xhci runtime

>>> pm by calling pm_runtime_get/put for xhci in its own dwc3

>>> runtime_suspend/resume callbacks.

>>>

>>> Looks a bit upside down to me, what's the reason for this?

>>>

>>> what prevents calling pm_runtime_put_* before leaving probe in xhci and let

>>> pm code handle

>>> the runtime suspend and parent/child relationships?

>>

>> When dwc3 controller is working on host mode, which will create and

>> add the USB hcd for host side. Then if we want to suspend dwc3

>> controller as host mode, the USB device (bus) of host side will

>> runtime suspend automatically if there are no slave attached (by

>> usb_runtime_suspend()--->usb_suspend_both()--->usb_suspend_interface()--->usb_suspend_device()--->generic_suspend()--->hcd_bus_suspend()--->xhci_bus_suspend()),

>> but we should not suspend xHCI device (issuing xhci_suspend()) now,

>> since some other  controllers did not implement the runtime PM to

>> control xHCI device's runtime state, which will cause other

>> controllers can not resume xHCI device (issuing xhci_resume()) if the

>> xHCI device suspend automatically by its child device. Thus we should

>> get the runtime count for xHCI device in xhci_plat_probe() in case

>> xHCI device will also suspend automatically by its child device.

>> According to that, for xHCI device's parent: dwc3 device, we should

>> put the xHCI device's runtime count to suspend xHCI device manually.

>>

>

> Any more comments?

>


I think I at least partially understand your point. You don't want to enable
runtime suspend for all xhci platform devices by default, but only for the ones
that are part of dwc3.

The implementation you suggest is that xhci platform driver always increase the xhci platform
device usage counter during probe with pm_runtime_get_noresume(), and never decrement it,
preventing xhci platform devices from runtime suspending by themselves.

You then control xhci runtime suspend from dwc3 driver runtime suspend, allowing
xhci platfrom controller to runtime suspend only when dwc3 runtime suspends by decrementing xhci
platform device usage in dwc3 runtime suspend callback.
As xhci is a child of dwc3 in this case,  dwc3 would never runtime suspend as long as xhci is running, so
dwc3 needed to be detached from xhci by setting dwc3 to ignore its children to get it to work.

I don't yet understand why we can't just keep runtime pm disabled as a default for xhci platform devices.
It could be enabled by whatever creates the platform device by setting some device property
(or equivalent), which would be checked in xhci_plat_probe() before enabling runtime pm. It
could then optionally be set in sysfs via power/control entry.

We would keep the usage counting intact, and only enable xhci platform device runtime pm
if the platform supports it, and dwc3 and xhci would keep their parent-child relationship
intact and make sure dwc3 can't runtime suspend before xhci.

To the concerns about xhci runtime suspending too early, the codepath you describet only applies
for roothub devices, which sohuld be called only after other devices have suspended.

To make sure xhci platform device does not runtime suspend during probe after first
hcd is added (without children), but before second one, make sure to increase the usage count before creating and adding
both hcd's, and only decrease it after both are  ready.

This way it will only runtime suspend after both buses are created.

xhci_plat_probe(pdev)
   /* prevent platform device runtime suspend between adding main and shared_hcd */	
   pm_runtime_get_noresume(&pdev->dev)
   usb_create_hcd()
   usb_create_shared_hcd()
   usb_add_hcd(hcd)
   usb_add_hcd(shared_hcd)		
   /* both hcs's added, allow platform device to runtime suspend */
   pm_runtime_put_noidle(&dev->dev);

-Mathias
(Exiting) Baolin Wang March 21, 2017, 7:55 a.m. UTC | #9
Hi Mathias,

On 21 February 2017 at 10:09, Baolin Wang <baolin.wang@linaro.org> wrote:
>>>>>> Do you have any comments about this patch? Thanks.

>>>>>>

>>>>>>>    drivers/usb/host/xhci-plat.c |   41

>>>>>>> ++++++++++++++++++++++++++++++++++++-----

>>>>>>>    1 file changed, 36 insertions(+), 5 deletions(-)

>>>>>>>

>>>>>>> diff --git a/drivers/usb/host/xhci-plat.c

>>>>>>> b/drivers/usb/host/xhci-plat.c

>>>>>>> index ed56bf9..5805c6a 100644

>>>>>>> --- a/drivers/usb/host/xhci-plat.c

>>>>>>> +++ b/drivers/usb/host/xhci-plat.c

>>>>>>> @@ -246,6 +246,10 @@ static int xhci_plat_probe(struct platform_device

>>>>>>> *pdev)

>>>>>>>           if (ret)

>>>>>>>                   goto dealloc_usb2_hcd;

>>>>>>>

>>>>>>> +       pm_runtime_get_noresume(&pdev->dev);

>>>>>>> +       pm_runtime_set_active(&pdev->dev);

>>>>>>> +       pm_runtime_enable(&pdev->dev);

>>>>>>> +

>>>>>>>           return 0;

>>>>>>>

>>>>>>>

>>>>>

>>>>> To me this looks like we are not really enabling runtime pm for xhci, we

>>>>> increment the

>>>>> usage count in probe, and keep it until remove is called.

>>>>>

>>>>> This would normally prevent parent dwc3 from runtime suspending, but I

>>>>> see

>>>>> that in patch 2/2 adds

>>>>> pm_suspend_ignore_children(dev, true) to dwc3, and, that dwc3 actually

>>>>> controls xhci runtime

>>>>> pm by calling pm_runtime_get/put for xhci in its own dwc3

>>>>> runtime_suspend/resume callbacks.

>>>>>

>>>>> Looks a bit upside down to me, what's the reason for this?

>>>>>

>>>>> what prevents calling pm_runtime_put_* before leaving probe in xhci and

>>>>> let

>>>>> pm code handle

>>>>> the runtime suspend and parent/child relationships?

>>>>

>>>>

>>>> When dwc3 controller is working on host mode, which will create and

>>>> add the USB hcd for host side. Then if we want to suspend dwc3

>>>> controller as host mode, the USB device (bus) of host side will

>>>> runtime suspend automatically if there are no slave attached (by

>>>>

>>>> usb_runtime_suspend()--->usb_suspend_both()--->usb_suspend_interface()--->usb_suspend_device()--->generic_suspend()--->hcd_bus_suspend()--->xhci_bus_suspend()),

>>>> but we should not suspend xHCI device (issuing xhci_suspend()) now,

>>>> since some other  controllers did not implement the runtime PM to

>>>> control xHCI device's runtime state, which will cause other

>>>> controllers can not resume xHCI device (issuing xhci_resume()) if the

>>>> xHCI device suspend automatically by its child device. Thus we should

>>>> get the runtime count for xHCI device in xhci_plat_probe() in case

>>>> xHCI device will also suspend automatically by its child device.

>>>> According to that, for xHCI device's parent: dwc3 device, we should

>>>> put the xHCI device's runtime count to suspend xHCI device manually.

>>>>

>>>

>>> Any more comments?

>>>

>>

>> I think I at least partially understand your point. You don't want to enable

>> runtime suspend for all xhci platform devices by default, but only for the

>> ones

>> that are part of dwc3.

>>

>> The implementation you suggest is that xhci platform driver always increase

>> the xhci platform

>> device usage counter during probe with pm_runtime_get_noresume(), and never

>> decrement it,

>> preventing xhci platform devices from runtime suspending by themselves.

>>

>> You then control xhci runtime suspend from dwc3 driver runtime suspend,

>> allowing

>> xhci platfrom controller to runtime suspend only when dwc3 runtime suspends

>> by decrementing xhci

>> platform device usage in dwc3 runtime suspend callback.

>> As xhci is a child of dwc3 in this case,  dwc3 would never runtime suspend

>> as long as xhci is running, so

>> dwc3 needed to be detached from xhci by setting dwc3 to ignore its children

>> to get it to work.

>

> Yes, that's my point.

>

>>

>> I don't yet understand why we can't just keep runtime pm disabled as a

>> default for xhci platform devices.

>> It could be enabled by whatever creates the platform device by setting some

>> device property

>> (or equivalent), which would be checked in xhci_plat_probe() before enabling

>> runtime pm. It

>> could then optionally be set in sysfs via power/control entry.

>

> I think runtime pm is not one hardware property, is it suitable if we

> introduce one device property to enable/disable runtime pm?


As I said, runtime pm is not one hardware property, I think it is not
suitable if we introduce one device property to enable/disable runtime
pm.

Secondly, we only can resume the xhci platform device by getting the
xhci usage counter from gadget driver, since the cable plug in/out
events only can be notified to glue layer of gadget driver(like dwc3
glue layer). That means if we want to suspend xhci platform device, we
must put xhci usage counter (so we can not keep their parent-child
relationship intact). That is why we need
pm_suspend_ignore_children(dev, true).

Mathias, could you re-check this patch? Thanks.

>>

>> We would keep the usage counting intact, and only enable xhci platform

>> device runtime pm

>> if the platform supports it, and dwc3 and xhci would keep their parent-child

>> relationship

>> intact and make sure dwc3 can't runtime suspend before xhci.

>

> Yes, that is the benefits.

>

>>

>> To the concerns about xhci runtime suspending too early, the codepath you

>> describet only applies

>> for roothub devices, which sohuld be called only after other devices have

>> suspended.

>>

>> To make sure xhci platform device does not runtime suspend during probe

>> after first

>> hcd is added (without children), but before second one, make sure to

>> increase the usage count before creating and adding

>> both hcd's, and only decrease it after both are  ready.

>>

>> This way it will only runtime suspend after both buses are created.

>>

>> xhci_plat_probe(pdev)

>>   /* prevent platform device runtime suspend between adding main and

>> shared_hcd */

>>   pm_runtime_get_noresume(&pdev->dev)

>>   usb_create_hcd()

>>   usb_create_shared_hcd()

>>   usb_add_hcd(hcd)

>>   usb_add_hcd(shared_hcd)

>>   /* both hcs's added, allow platform device to runtime suspend */

>>   pm_runtime_put_noidle(&dev->dev);

>>

>

> Make sense.

>

> --

> Baolin.wang

> Best Regards




-- 
Baolin.wang
Best Regards
Felipe Balbi March 21, 2017, 8:07 a.m. UTC | #10
Hi,

Baolin Wang <baolin.wang@linaro.org> writes:
>>> I don't yet understand why we can't just keep runtime pm disabled as a

>>> default for xhci platform devices.

>>> It could be enabled by whatever creates the platform device by setting some

>>> device property

>>> (or equivalent), which would be checked in xhci_plat_probe() before enabling

>>> runtime pm. It

>>> could then optionally be set in sysfs via power/control entry.

>>

>> I think runtime pm is not one hardware property, is it suitable if we

>> introduce one device property to enable/disable runtime pm?

>

> As I said, runtime pm is not one hardware property, I think it is not

> suitable if we introduce one device property to enable/disable runtime

> pm.


we already this functionality exposed on sysfs.

> Secondly, we only can resume the xhci platform device by getting the

> xhci usage counter from gadget driver, since the cable plug in/out

> events only can be notified to glue layer of gadget driver(like dwc3

> glue layer). That means if we want to suspend xhci platform device, we


this is a problem with the glue layer, IMO. It should be something like
so:

static irqreturn_t dwc3_foobar_wakeup(int irq, void *_glue)
{
	struct dwc3_foobar_glue *glue = _glue;
	u32 reg;

	reg = real(glue->base, OFFSET);
        if (reg & CONNECT)
		pm_runtime_resume(&glue->dwc3);

	return IRQ_HANDLED;
}

then dwc3's ->runtime_resume() should check if the event is supposed to
be handled by host or peripheral by checking which mode it was before
suspend and making the assumption that we don't change modes while
suspended. Something like:

static int dwc3_runtime_resume(struct device *dev)
{
	struct dwc3 *dwc = dev_get_drvdata(dev);

	[...]

	if (dwc->is_host)
        	pm_runtime_resume(dwc->xhci.dev);

	[...]

	return 0;
}

> must put xhci usage counter (so we can not keep their parent-child

> relationship intact). That is why we need

> pm_suspend_ignore_children(dev, true).


you really shouldn't need that and it's still unclear why you think you
do.

-- 
balbi
diff mbox

Patch

diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c
index ed56bf9..5805c6a 100644
--- a/drivers/usb/host/xhci-plat.c
+++ b/drivers/usb/host/xhci-plat.c
@@ -246,6 +246,10 @@  static int xhci_plat_probe(struct platform_device *pdev)
 	if (ret)
 		goto dealloc_usb2_hcd;
 
+	pm_runtime_get_noresume(&pdev->dev);
+	pm_runtime_set_active(&pdev->dev);
+	pm_runtime_enable(&pdev->dev);
+
 	return 0;
 
 
@@ -274,6 +278,10 @@  static int xhci_plat_remove(struct platform_device *dev)
 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
 	struct clk *clk = xhci->clk;
 
+	pm_runtime_set_suspended(&dev->dev);
+	pm_runtime_put_noidle(&dev->dev);
+	pm_runtime_disable(&dev->dev);
+
 	usb_remove_hcd(xhci->shared_hcd);
 	usb_phy_shutdown(hcd->usb_phy);
 
@@ -311,14 +319,37 @@  static int xhci_plat_resume(struct device *dev)
 
 	return xhci_resume(xhci, 0);
 }
+#endif /* CONFIG_PM_SLEEP */
+
+#ifdef CONFIG_PM
+static int xhci_plat_runtime_suspend(struct device *dev)
+{
+	struct usb_hcd  *hcd = dev_get_drvdata(dev);
+	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
+
+	return xhci_suspend(xhci, device_may_wakeup(dev));
+}
+
+static int xhci_plat_runtime_resume(struct device *dev)
+{
+	struct usb_hcd  *hcd = dev_get_drvdata(dev);
+	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
+
+	return xhci_resume(xhci, 0);
+}
+
+static int xhci_plat_runtime_idle(struct device *dev)
+{
+	return 0;
+}
+#endif /* CONFIG_PM */
 
 static const struct dev_pm_ops xhci_plat_pm_ops = {
 	SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
+
+	SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend, xhci_plat_runtime_resume,
+			   xhci_plat_runtime_idle)
 };
-#define DEV_PM_OPS	(&xhci_plat_pm_ops)
-#else
-#define DEV_PM_OPS	NULL
-#endif /* CONFIG_PM */
 
 static const struct acpi_device_id usb_xhci_acpi_match[] = {
 	/* XHCI-compliant USB Controller */
@@ -332,7 +363,7 @@  static int xhci_plat_resume(struct device *dev)
 	.remove	= xhci_plat_remove,
 	.driver	= {
 		.name = "xhci-hcd",
-		.pm = DEV_PM_OPS,
+		.pm = &xhci_plat_pm_ops,
 		.of_match_table = of_match_ptr(usb_xhci_of_match),
 		.acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
 	},