Skip to contentSkip to conversation

usb: dwc3: Add support for VBUS power control

PATCH Superseded Mike Looijmans 4 participants 10 messages Jun 19, 2020 – Sep 7, 2020 1 message not in the archive

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 usb: dwc3: Add support for VBUS power control

Support VBUS power control using regulator framework. Enables the regulator while the port is in host mode.

The "vbus-supply" property can be provided using a usb-connector child node and standard devicetree bindings.

---
v2: Add missing devm_regulator_get call which got lost during rebase
v3: Remove devicetree binding, use standard usb-connector
Fix probe fail when vbus-supply is not present

 drivers/usb/dwc3/core.c | 38 ++++++++++++++++++++++++++++++--------
 drivers/usb/dwc3/core.h |  4 ++++
 drivers/usb/dwc3/drd.c  |  6 ++----
 3 files changed, 36 insertions(+), 12 deletions(-)
drivers/usb/dwc3/core.c +30 -8
index edc17155cb2b..42e804ede1b3 100644
@@ -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);
 
@@ -1470,6 +1484,14 @@ static int dwc3_probe(struct platform_device *pdev)
 
 	dwc3_get_properties(dwc);
 
+	dwc->vbus_reg = devm_regulator_get_optional(dev, "vbus");
+	if (IS_ERR(dwc->vbus_reg)) {
+		if (PTR_ERR(dwc->vbus_reg) == -EPROBE_DEFER)
+			return -EPROBE_DEFER;
+
+		dwc->vbus_reg = NULL;
+	}
+
 	dwc->reset = devm_reset_control_array_get(dev, true, true);
 	if (IS_ERR(dwc->reset))
 		return PTR_ERR(dwc->reset);
drivers/usb/dwc3/core.h +4 -0
index 4c171a8e215f..cee2574d7bf4 100644
@@ -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);
 
drivers/usb/dwc3/drd.c +2 -4
index 7db1ffc92bbd..45fdec2d128d 100644
@@ -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);
Signature · 1 line
2.17.1
#1 lore raw

drivers/usb/dwc3/core.c void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode) · line 124

Quoted 9 lines · Mike Looijmans
drivers/usb/dwc3/core.c +9 -0
+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);

dwc->vbus_reg is set to NULL when the regulator is not present. These regulator_* functions expect a non-NULL pointer so a NULL check is required before calling them.

Quoted 52 lines
drivers/usb/dwc3/core.c +12 -8
+		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);
 

drivers/usb/dwc3/core.c static int dwc3_probe(struct platform_device *pdev) · line 1490

Quoted 7 lines
drivers/usb/dwc3/core.c +4 -0
@@ -1470,6 +1484,14 @@ static int dwc3_probe(struct platform_device *pdev)
 
 	dwc3_get_properties(dwc);
 
+	dwc->vbus_reg = devm_regulator_get_optional(dev, "vbus");
+	if (IS_ERR(dwc->vbus_reg)) {
+		if (PTR_ERR(dwc->vbus_reg) == -EPROBE_DEFER)
+			return -EPROBE_DEFER;

Some drivers seem to do it this way, but I think it would be more correct to return all errors that aren't ENODEV, like drivers/gpu/drm/exynos/exynos_hdmi.c does. That way you would allow the regulator to not be present, but you also wouldn't silently ignore other errors such as ENOMEM.

Quoted 4 lines
drivers/usb/dwc3/core.c +4 -0
+
+		dwc->vbus_reg = NULL;
+	}
+
#2 lore raw
Quoted 14 lines · Vincent Whitchurch

drivers/usb/dwc3/core.c void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode) · line 124

Quoted 9 lines · Mike Looijmans
drivers/usb/dwc3/core.c +9 -0
+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);

dwc->vbus_reg is set to NULL when the regulator is not present. These regulator_* functions expect a non-NULL pointer so a NULL check is required before calling them.

Does the device actually support running without power so that's a thing that can happen? _get_optional() should only ever be used for supplies that may be physically absent.

#3 lore raw

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: [email protected]
W: www.topicproducts.com

Please consider the environment before printing this e-mail

Quoted 15 lines · Mark Brown
Quoted 14 lines · Vincent Whitchurch

drivers/usb/dwc3/core.c void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode) · line 124

Quoted 9 lines · Mike Looijmans
drivers/usb/dwc3/core.c +9 -0
+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);

dwc->vbus_reg is set to NULL when the regulator is not present. These regulator_* functions expect a non-NULL pointer so a NULL check is required before calling them.

Quoted 3 lines

Does the device actually support running without power so that's a thing that can happen? _get_optional() should only ever be used for supplies that may be physically absent.

It's the 5V VBUS power for the USB "plug" that's being controlled here. It must turned on when the controller is in "host" mode. Some boards arrange this in hardware through the PHY, and some just don't have any control at all and have it permanently on or off. On a board where the 5V is controlled using a GPIO line or an I2C chip, this patch is required to make it work.

Signature · 1 line
Mike Looijmans
#4 lore raw
Quoted 11 lines · Mike Looijmans
Quoted 3 lines · Mark Brown

Does the device actually support running without power so that's a thing that can happen? _get_optional() should only ever be used for supplies that may be physically absent.

It's the 5V VBUS power for the USB "plug" that's being controlled here. It must turned on when the controller is in "host" mode. Some boards arrange this in hardware through the PHY, and some just don't have any control at all and have it permanently on or off. On a board where the 5V is controlled using a GPIO line or an I2C chip, this patch is required to make it work.

That sounds like the driver should not be using _get_optional() then.

#5 lore raw

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: [email protected]
W: www.topicproducts.com

Please consider the environment before printing this e-mail

Quoted 7 lines · Mark Brown
Quoted 5 lines · Mike Looijmans
Quoted 3 lines · Mark Brown

Does the device actually support running without power so that's a thing that can happen? _get_optional() should only ever be used for supplies that may be physically absent.

Quoted 8 lines
Quoted 5 lines

It's the 5V VBUS power for the USB "plug" that's being controlled here. It must turned on when the controller is in "host" mode. Some boards arrange this in hardware through the PHY, and some just don't have any control at all and have it permanently on or off. On a board where the 5V is controlled using a GPIO line or an I2C chip, this patch is required to make it work.

That sounds like the driver should not be using _get_optional() then.

Making it mandatory would break most (read: all except Topic's) existing boards as they won't have it in their devicetree. I'm perfectly okay with that, but others might disagree.

#6 lore raw
Quoted 5 lines · Mike Looijmans

Met vriendelijke groet / kind regards,

Mike Looijmans
System Expert

You probably want to remove your signature when replying to the list...

Quoted 9 lines · Mark Brown
Quoted 7 lines · Mike Looijmans
Quoted 6 lines

It's the 5V VBUS power for the USB "plug" that's being controlled here. It must turned on when the controller is in "host" mode. Some boards arrange this in hardware through the PHY, and some just don't have any control at all and have it permanently on or off. On a board where the 5V is controlled using a GPIO line or an I2C chip, this patch is required to make it work.

Quoted 5 lines
Quoted 1 line

That sounds like the driver should not be using _get_optional() then.

Making it mandatory would break most (read: all except Topic's) existing boards as they won't have it in their devicetree. I'm perfectly okay with that, but others might disagree.

No, it wouldn't break them at all - they'd get a dummy regulator provided.

#7 lore raw

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: [email protected]
W: www.topicproducts.com

Please consider the environment before printing this e-mail

Quoted 18 lines · Mark Brown
Quoted 5 lines · Mike Looijmans

Met vriendelijke groet / kind regards,

Mike Looijmans
System Expert

You probably want to remove your signature when replying to the list...

Quoted 8 lines · Mark Brown
Quoted 7 lines · Mike Looijmans
Quoted 6 lines

It's the 5V VBUS power for the USB "plug" that's being controlled here. It must turned on when the controller is in "host" mode. Some boards arrange this in hardware through the PHY, and some just don't have any control at all and have it permanently on or off. On a board where the 5V is controlled using a GPIO line or an I2C chip, this patch is required to make it work.

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

That sounds like the driver should not be using _get_optional() then.

Making it mandatory would break most (read: all except Topic's) existing boards as they won't have it in their devicetree. I'm perfectly okay with that, but others might disagree.

No, it wouldn't break them at all - they'd get a dummy regulator provided.

Ah, so *not* using _get_optional will yield the behaviour that I'd want. I'll test and make a v4 patch.

#8 lore raw
In reply to Mike Looijmans

Hi,

Quoted 29 lines · Mike Looijmans

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: [email protected]
W: www.topicproducts.com

Please consider the environment before printing this e-mail

Quoted 13 lines · Mark Brown
Quoted 11 lines · Mike Looijmans
Quoted 3 lines · Mark Brown

Does the device actually support running without power so that's a thing that can happen? _get_optional() should only ever be used for supplies that may be physically absent.

It's the 5V VBUS power for the USB "plug" that's being controlled here. It must turned on when the controller is in "host" mode. Some boards arrange this in hardware through the PHY, and some just don't have any control at all and have it permanently on or off. On a board where the 5V is controlled using a GPIO line or an I2C chip, this patch is required to make it work.

Quoted 6 lines
Quoted 2 lines

That sounds like the driver should not be using _get_optional() then.

Making it mandatory would break most (read: all except Topic's) existing boards as they won't have it in their devicetree. I'm perfectly okay with that, but others might disagree.

you're perfectly okay with break all existing users of the driver? That's a bit harsh

Signature · 1 line
balbi
#9 lore raw

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: [email protected]
W: www.topicproducts.com

Please consider the environment before printing this e-mail

Quoted 29 lines · Felipe Balbi

Hi,

Quoted 26 lines · Mike Looijmans

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: [email protected]
W: www.topicproducts.com

Please consider the environment before printing this e-mail

Quoted 10 lines · Mark Brown
Quoted 9 lines · Mike Looijmans
Quoted 3 lines · Mark Brown

Does the device actually support running without power so that's a thing that can happen? _get_optional() should only ever be used for supplies that may be physically absent.

It's the 5V VBUS power for the USB "plug" that's being controlled here. It must turned on when the controller is in "host" mode. Some boards arrange this in hardware through the PHY, and some just don't have any control at all and have it permanently on or off. On a board where the 5V is controlled using a GPIO line or an I2C chip, this patch is required to make it work.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

That sounds like the driver should not be using _get_optional() then.

Making it mandatory would break most (read: all except Topic's) existing boards as they won't have it in their devicetree. I'm perfectly okay with that, but others might disagree.

you're perfectly okay with break all existing users of the driver? That's a bit harsh

It turned out that "optional" when used for regulators means the opposite of when used in clk context. For regulators, "optional" means "don't supply a dummy regulator if there's none provided". So get_optional will just fail when the supply isn't defined, while get_regulator will just return a dummy in that case.

So the v4 patch which doesn't use "_get_optional" works for both cases and doesn't break existing use(r)s.

Signature · 1 line
Mike Looijmans