diff mbox series

usb: dwc3: Add support for VBUS power control

Message ID 20200603085932.31746-1-mike.looijmans@topic.nl (mailing list archive)
State Superseded
Headers show
Series usb: dwc3: Add support for VBUS power control | expand

Commit Message

Mike Looijmans June 3, 2020, 8:59 a.m. UTC
Support VBUS power control using regulator framework. Enables the regulator
while the port is in host mode.

Signed-off-by: Mike Looijmans <mike.looijmans@topic.nl>
---
 .../devicetree/bindings/usb/dwc3.txt          |  1 +
 drivers/usb/dwc3/core.c                       | 30 ++++++++++++++-----
 drivers/usb/dwc3/core.h                       |  4 +++
 drivers/usb/dwc3/drd.c                        |  6 ++--
 4 files changed, 29 insertions(+), 12 deletions(-)

Comments

Mike Looijmans June 3, 2020, 11:34 a.m. UTC | #1
Oh darn, the "devm_get_regulator..." call in probe got lost in the 
rebase+merge. I'll add that in v2.



Met vriendelijke groet / kind regards,

Mike Looijmans
System Expert


TOPIC Embedded Products B.V.
Materiaalweg 4, 5681 RJ Best
The Netherlands

T: +31 (0) 499 33 69 69
E: mike.looijmans@topicproducts.com
W: www.topicproducts.com

Please consider the environment before printing this e-mail
On 03-06-2020 10:59, Mike Looijmans wrote:
> Support VBUS power control using regulator framework. Enables the regulator
> while the port is in host mode.
>
> Signed-off-by: Mike Looijmans <mike.looijmans@topic.nl>
> ---
>   .../devicetree/bindings/usb/dwc3.txt          |  1 +
>   drivers/usb/dwc3/core.c                       | 30 ++++++++++++++-----
>   drivers/usb/dwc3/core.h                       |  4 +++
>   drivers/usb/dwc3/drd.c                        |  6 ++--
>   4 files changed, 29 insertions(+), 12 deletions(-)
>
> diff --git a/Documentation/devicetree/bindings/usb/dwc3.txt b/Documentation/devicetree/bindings/usb/dwc3.txt
> index 9946ff9ba735..56bc3f238e2d 100644
> --- a/Documentation/devicetree/bindings/usb/dwc3.txt
> +++ b/Documentation/devicetree/bindings/usb/dwc3.txt
> @@ -37,6 +37,7 @@ Optional properties:
>    - phys: from the *Generic PHY* bindings
>    - phy-names: from the *Generic PHY* bindings; supported names are "usb2-phy"
>   	or "usb3-phy".
> + - vbus-supply: Regulator handle that provides the VBUS power.
>    - resets: set of phandle and reset specifier pairs
>    - snps,usb2-lpm-disable: indicate if we don't want to enable USB2 HW LPM
>    - snps,usb3_lpm_capable: determines if platform is USB3 LPM capable
> diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
> index edc17155cb2b..a9e58a301446 100644
> --- a/drivers/usb/dwc3/core.c
> +++ b/drivers/usb/dwc3/core.c
> @@ -25,6 +25,7 @@
>   #include <linux/of.h>
>   #include <linux/acpi.h>
>   #include <linux/pinctrl/consumer.h>
> +#include <linux/regulator/consumer.h>
>   #include <linux/reset.h>
>   
>   #include <linux/usb/ch9.h>
> @@ -112,6 +113,23 @@ void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
>   	dwc->current_dr_role = mode;
>   }
>   
> +void dwc3_set_vbus(struct dwc3 *dwc, bool enable)
> +{
> +	int ret;
> +
> +	if (enable != dwc->vbus_reg_enabled) {
> +		if (enable)
> +			ret = regulator_enable(dwc->vbus_reg);
> +		else
> +			ret = regulator_disable(dwc->vbus_reg);
> +		if (!ret)
> +			dwc->vbus_reg_enabled = enable;
> +	}
> +
> +	if (dwc->usb2_phy)
> +		otg_set_vbus(dwc->usb2_phy->otg, enable);
> +}
> +
>   static void __dwc3_set_mode(struct work_struct *work)
>   {
>   	struct dwc3 *dwc = work_to_dwc(work);
> @@ -164,8 +182,7 @@ static void __dwc3_set_mode(struct work_struct *work)
>   		if (ret) {
>   			dev_err(dwc->dev, "failed to initialize host\n");
>   		} else {
> -			if (dwc->usb2_phy)
> -				otg_set_vbus(dwc->usb2_phy->otg, true);
> +			dwc3_set_vbus(dwc, true);
>   			phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
>   			phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
>   		}
> @@ -173,8 +190,7 @@ static void __dwc3_set_mode(struct work_struct *work)
>   	case DWC3_GCTL_PRTCAP_DEVICE:
>   		dwc3_event_buffers_setup(dwc);
>   
> -		if (dwc->usb2_phy)
> -			otg_set_vbus(dwc->usb2_phy->otg, false);
> +		dwc3_set_vbus(dwc, false);
>   		phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
>   		phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
>   
> @@ -1183,8 +1199,7 @@ static int dwc3_core_init_mode(struct dwc3 *dwc)
>   	case USB_DR_MODE_PERIPHERAL:
>   		dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
>   
> -		if (dwc->usb2_phy)
> -			otg_set_vbus(dwc->usb2_phy->otg, false);
> +		dwc3_set_vbus(dwc, false);
>   		phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
>   		phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
>   
> @@ -1198,8 +1213,7 @@ static int dwc3_core_init_mode(struct dwc3 *dwc)
>   	case USB_DR_MODE_HOST:
>   		dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
>   
> -		if (dwc->usb2_phy)
> -			otg_set_vbus(dwc->usb2_phy->otg, true);
> +		dwc3_set_vbus(dwc, true);
>   		phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
>   		phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
>   
> diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
> index 4c171a8e215f..cee2574d7bf4 100644
> --- a/drivers/usb/dwc3/core.h
> +++ b/drivers/usb/dwc3/core.h
> @@ -1085,6 +1085,9 @@ struct dwc3 {
>   
>   	bool			phys_ready;
>   
> +	struct regulator	*vbus_reg;
> +	bool			vbus_reg_enabled;
> +
>   	struct ulpi		*ulpi;
>   	bool			ulpi_ready;
>   
> @@ -1397,6 +1400,7 @@ struct dwc3_gadget_ep_cmd_params {
>   
>   /* prototypes */
>   void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode);
> +void dwc3_set_vbus(struct dwc3 *dwc, bool enable);
>   void dwc3_set_mode(struct dwc3 *dwc, u32 mode);
>   u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type);
>   
> diff --git a/drivers/usb/dwc3/drd.c b/drivers/usb/dwc3/drd.c
> index 7db1ffc92bbd..45fdec2d128d 100644
> --- a/drivers/usb/dwc3/drd.c
> +++ b/drivers/usb/dwc3/drd.c
> @@ -384,8 +384,7 @@ void dwc3_otg_update(struct dwc3 *dwc, bool ignore_idstatus)
>   		if (ret) {
>   			dev_err(dwc->dev, "failed to initialize host\n");
>   		} else {
> -			if (dwc->usb2_phy)
> -				otg_set_vbus(dwc->usb2_phy->otg, true);
> +			dwc3_set_vbus(dwc, true);
>   			if (dwc->usb2_generic_phy)
>   				phy_set_mode(dwc->usb2_generic_phy,
>   					     PHY_MODE_USB_HOST);
> @@ -398,8 +397,7 @@ void dwc3_otg_update(struct dwc3 *dwc, bool ignore_idstatus)
>   		dwc3_event_buffers_setup(dwc);
>   		spin_unlock_irqrestore(&dwc->lock, flags);
>   
> -		if (dwc->usb2_phy)
> -			otg_set_vbus(dwc->usb2_phy->otg, false);
> +		dwc3_set_vbus(dwc, false);
>   		if (dwc->usb2_generic_phy)
>   			phy_set_mode(dwc->usb2_generic_phy,
>   				     PHY_MODE_USB_DEVICE);
diff mbox series

Patch

diff --git a/Documentation/devicetree/bindings/usb/dwc3.txt b/Documentation/devicetree/bindings/usb/dwc3.txt
index 9946ff9ba735..56bc3f238e2d 100644
--- a/Documentation/devicetree/bindings/usb/dwc3.txt
+++ b/Documentation/devicetree/bindings/usb/dwc3.txt
@@ -37,6 +37,7 @@  Optional properties:
  - phys: from the *Generic PHY* bindings
  - phy-names: from the *Generic PHY* bindings; supported names are "usb2-phy"
 	or "usb3-phy".
+ - vbus-supply: Regulator handle that provides the VBUS power.
  - resets: set of phandle and reset specifier pairs
  - snps,usb2-lpm-disable: indicate if we don't want to enable USB2 HW LPM
  - snps,usb3_lpm_capable: determines if platform is USB3 LPM capable
diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
index edc17155cb2b..a9e58a301446 100644
--- a/drivers/usb/dwc3/core.c
+++ b/drivers/usb/dwc3/core.c
@@ -25,6 +25,7 @@ 
 #include <linux/of.h>
 #include <linux/acpi.h>
 #include <linux/pinctrl/consumer.h>
+#include <linux/regulator/consumer.h>
 #include <linux/reset.h>
 
 #include <linux/usb/ch9.h>
@@ -112,6 +113,23 @@  void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
 	dwc->current_dr_role = mode;
 }
 
+void dwc3_set_vbus(struct dwc3 *dwc, bool enable)
+{
+	int ret;
+
+	if (enable != dwc->vbus_reg_enabled) {
+		if (enable)
+			ret = regulator_enable(dwc->vbus_reg);
+		else
+			ret = regulator_disable(dwc->vbus_reg);
+		if (!ret)
+			dwc->vbus_reg_enabled = enable;
+	}
+
+	if (dwc->usb2_phy)
+		otg_set_vbus(dwc->usb2_phy->otg, enable);
+}
+
 static void __dwc3_set_mode(struct work_struct *work)
 {
 	struct dwc3 *dwc = work_to_dwc(work);
@@ -164,8 +182,7 @@  static void __dwc3_set_mode(struct work_struct *work)
 		if (ret) {
 			dev_err(dwc->dev, "failed to initialize host\n");
 		} else {
-			if (dwc->usb2_phy)
-				otg_set_vbus(dwc->usb2_phy->otg, true);
+			dwc3_set_vbus(dwc, true);
 			phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
 			phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
 		}
@@ -173,8 +190,7 @@  static void __dwc3_set_mode(struct work_struct *work)
 	case DWC3_GCTL_PRTCAP_DEVICE:
 		dwc3_event_buffers_setup(dwc);
 
-		if (dwc->usb2_phy)
-			otg_set_vbus(dwc->usb2_phy->otg, false);
+		dwc3_set_vbus(dwc, false);
 		phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
 		phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
 
@@ -1183,8 +1199,7 @@  static int dwc3_core_init_mode(struct dwc3 *dwc)
 	case USB_DR_MODE_PERIPHERAL:
 		dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
 
-		if (dwc->usb2_phy)
-			otg_set_vbus(dwc->usb2_phy->otg, false);
+		dwc3_set_vbus(dwc, false);
 		phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
 		phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
 
@@ -1198,8 +1213,7 @@  static int dwc3_core_init_mode(struct dwc3 *dwc)
 	case USB_DR_MODE_HOST:
 		dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
 
-		if (dwc->usb2_phy)
-			otg_set_vbus(dwc->usb2_phy->otg, true);
+		dwc3_set_vbus(dwc, true);
 		phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
 		phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
 
diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
index 4c171a8e215f..cee2574d7bf4 100644
--- a/drivers/usb/dwc3/core.h
+++ b/drivers/usb/dwc3/core.h
@@ -1085,6 +1085,9 @@  struct dwc3 {
 
 	bool			phys_ready;
 
+	struct regulator	*vbus_reg;
+	bool			vbus_reg_enabled;
+
 	struct ulpi		*ulpi;
 	bool			ulpi_ready;
 
@@ -1397,6 +1400,7 @@  struct dwc3_gadget_ep_cmd_params {
 
 /* prototypes */
 void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode);
+void dwc3_set_vbus(struct dwc3 *dwc, bool enable);
 void dwc3_set_mode(struct dwc3 *dwc, u32 mode);
 u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type);
 
diff --git a/drivers/usb/dwc3/drd.c b/drivers/usb/dwc3/drd.c
index 7db1ffc92bbd..45fdec2d128d 100644
--- a/drivers/usb/dwc3/drd.c
+++ b/drivers/usb/dwc3/drd.c
@@ -384,8 +384,7 @@  void dwc3_otg_update(struct dwc3 *dwc, bool ignore_idstatus)
 		if (ret) {
 			dev_err(dwc->dev, "failed to initialize host\n");
 		} else {
-			if (dwc->usb2_phy)
-				otg_set_vbus(dwc->usb2_phy->otg, true);
+			dwc3_set_vbus(dwc, true);
 			if (dwc->usb2_generic_phy)
 				phy_set_mode(dwc->usb2_generic_phy,
 					     PHY_MODE_USB_HOST);
@@ -398,8 +397,7 @@  void dwc3_otg_update(struct dwc3 *dwc, bool ignore_idstatus)
 		dwc3_event_buffers_setup(dwc);
 		spin_unlock_irqrestore(&dwc->lock, flags);
 
-		if (dwc->usb2_phy)
-			otg_set_vbus(dwc->usb2_phy->otg, false);
+		dwc3_set_vbus(dwc, false);
 		if (dwc->usb2_generic_phy)
 			phy_set_mode(dwc->usb2_generic_phy,
 				     PHY_MODE_USB_DEVICE);