Skip to contentSkip to conversation

usb: phy: generic: Support enabling/disabling VBUS

PATCH In review Sean Anderson 2 participants 6 messages Apr 25, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover usb: phy: generic: Support enabling/disabling VBUS

The generic USB phy has had VBUS-related code for a long time, but it has always been broken, since the regulator was never gotten from the device tree. However, the support itself seems not very useful, since e.g. usb_phy_vbus_on/off has no users and usb_phy_set_power is only used by gadgets to make sure they don't draw too much current. Instead, use the VBUS regulator to implement otg_set_vbus, which is called from several drivers. This results in a change in semantics of VBUS, but since support was always broken I don't think this will have any affect.

This got no (non-automated) feedback for an entire release cycle. I'm sending v2 with a wider CC list so hopefully someone can review it in time for 5.19.

Changes in v2:
- Fix dt_binding_check errors

Sean Anderson (4):
dt-bindings: usb: usb-nop-xceiv: Repurpose vbus-regulator
usb: phy: generic: Get the vbus supply
usb: phy: generic: Implement otg->set_vbus
usb: phy: generic: Disable vbus on removal

 .../bindings/usb/usb-nop-xceiv.yaml           |  9 ++-
 drivers/usb/phy/phy-generic.c                 | 55 +++++++++----------
 2 files changed, 31 insertions(+), 33 deletions(-)
Signature · 1 line
2.35.1.1320.gc452695387.dirty
#1 lore raw

PATCH v2 1/4 dt-bindings: usb: usb-nop-xceiv: Repurpose vbus-regulator

The vbus-regulator property was never actually read from the device tree. Introduce a new property vbus-supply to represent the regulator powering the VBUS when acting as an A-Device. This supply will be enabled and disabled as necessary. Note that this is different from vbus-regulator, which represented the available current available to draw from VBUS in B-Device mode. Because no one was using vbus-regulator, remove it.

---

Changes in v2:
- Fix dt_binding_check errors

 Documentation/devicetree/bindings/usb/usb-nop-xceiv.yaml | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
Documentation/devicetree/bindings/usb/usb-nop-xceiv.yaml +4 -5
index 2824c17285ee..b4517b01616f 100644
@@ -34,10 +34,9 @@ properties:
     description: Should specify the GPIO detecting a VBus insertion
     maxItems: 1
 
-  vbus-regulator:
-    description: Should specifiy the regulator supplying current drawn from
-      the VBus line.
-    $ref: /schemas/types.yaml#/definitions/phandle
+  vbus-supply:
+    description: regulator supplying VBUS. It will be enabled and disabled
+                 dynamically in OTG mode.
 
 required:
   - compatible
@@ -57,7 +56,7 @@ examples:
         vcc-supply = <&hsusb1_vcc_regulator>;
         reset-gpios = <&gpio1 7 GPIO_ACTIVE_LOW>;
         vbus-detect-gpio = <&gpio2 13 GPIO_ACTIVE_HIGH>;
-        vbus-regulator = <&vbus_regulator>;
+        vbus-supply = <&vbus_regulator>;
         #phy-cells = <0>;
     };
 
Signature · 1 line
2.35.1.1320.gc452695387.dirty
Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] dt-bindings: usb: usb-nop-xceiv: Repurpose vbus-regulator

Quoted 8 lines · Sean Anderson

The vbus-regulator property was never actually read from the device tree. Introduce a new property vbus-supply to represent the regulator powering the VBUS when acting as an A-Device. This supply will be enabled and disabled as necessary. Note that this is different from vbus-regulator, which represented the available current available to draw from VBUS in B-Device mode. Because no one was using vbus-regulator, remove it.

(...)

Documentation/devicetree/bindings/usb/usb-nop-xceiv.yaml properties: · line 39

Quoted 3 lines
Documentation/devicetree/bindings/usb/usb-nop-xceiv.yaml +3 -0
+  vbus-supply:
+    description: regulator supplying VBUS. It will be enabled and disabled
+                 dynamically in OTG mode.

I would add a small text snippet like this: "If the regulator is controlled by a GPIO line, this should be modeled as a regulator-fixed and referenced by this supply" - it's helpful because otherwise people tend to start to submit weird stuff like adding yet another GPIO for this.

Either way:

Yours,
Linus Walleij

#3 lore raw

PATCH v2 2/4 usb: phy: generic: Get the vbus supply

In reply to Sean Anderson

While support for working with a vbus was added, the regulator was never actually gotten (despite what was documented). Fix this by actually getting the supply from the device tree.

Fixes 7acc9973e3c4 ("usb: phy: generic: add vbus support")

---

(no changes since v1)

 drivers/usb/phy/phy-generic.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/usb/phy/phy-generic.c +7 -0
index 661a229c105d..34b9f8140187 100644
@@ -268,6 +268,13 @@ int usb_phy_gen_create_phy(struct device *dev, struct usb_phy_generic *nop)
 			return -EPROBE_DEFER;
 	}
 
+	nop->vbus_draw = devm_regulator_get_exclusive(dev, "vbus");
+	if (PTR_ERR(nop->vbus_draw) == -ENODEV)
+		nop->vbus_draw = NULL;
+	if (IS_ERR(nop->vbus_draw))
+		return dev_err_probe(dev, PTR_ERR(nop->vbus_draw),
+				     "could not get vbus regulator\n");
+
 	nop->dev		= dev;
 	nop->phy.dev		= nop->dev;
 	nop->phy.label		= "nop-xceiv";
Signature · 1 line
2.35.1.1320.gc452695387.dirty
#4 lore raw

PATCH v2 3/4 usb: phy: generic: Implement otg->set_vbus

In reply to Sean Anderson

Some USB controller drivers call otg_set_vbus when entering host or device mode. Implement this callback so that VBUS can be turned on and off automatically. This is especially useful when there is no property for a VBUS supply in the controller's binding.

This results in a change in semantics of the vbus_draw regulator. Whereas before it represented the VBUS supplied by an A-Device when we acted as a B-Device, now it represents an internal VBUS source. Accordingly, we no longer set the current limit or enable/disable the bus from nop_gpio_vbus_thread. Because this supply was never initialized before the previous commit, there should be no change in behavior.

---

(no changes since v1)

 drivers/usb/phy/phy-generic.c | 45 +++++++++++++----------------------
 1 file changed, 17 insertions(+), 28 deletions(-)
drivers/usb/phy/phy-generic.c +17 -28
index 34b9f8140187..2c2553bc9b54 100644
@@ -68,33 +68,26 @@ static void nop_reset(struct usb_phy_generic *nop)
 }
 
 /* interface to regulator framework */
-static void nop_set_vbus_draw(struct usb_phy_generic *nop, unsigned mA)
+static int nop_set_vbus(struct usb_otg *otg, bool enable)
 {
-	struct regulator *vbus_draw = nop->vbus_draw;
-	int enabled;
-	int ret;
+	int ret = 0;
+	struct usb_phy_generic *nop = dev_get_drvdata(otg->usb_phy->dev);
 
-	if (!vbus_draw)
-		return;
+	if (!nop->vbus_draw)
+		return 0;
 
-	enabled = nop->vbus_draw_enabled;
-	if (mA) {
-		regulator_set_current_limit(vbus_draw, 0, 1000 * mA);
-		if (!enabled) {
-			ret = regulator_enable(vbus_draw);
-			if (ret < 0)
-				return;
-			nop->vbus_draw_enabled = 1;
-		}
-	} else {
-		if (enabled) {
-			ret = regulator_disable(vbus_draw);
-			if (ret < 0)
-				return;
-			nop->vbus_draw_enabled = 0;
-		}
+	if (enable && !nop->vbus_draw_enabled) {
+		ret = regulator_enable(nop->vbus_draw);
+		if (ret)
+			nop->vbus_draw_enabled = false;
+		else
+			nop->vbus_draw_enabled = true;
+
+	} else if (!enable && nop->vbus_draw_enabled) {
+		ret = regulator_disable(nop->vbus_draw);
+		nop->vbus_draw_enabled = false;
 	}
-	nop->mA = mA;
+	return ret;
 }
 
 
@@ -114,14 +107,9 @@ static irqreturn_t nop_gpio_vbus_thread(int irq, void *data)
 		otg->state = OTG_STATE_B_PERIPHERAL;
 		nop->phy.last_event = status;
 
-		/* drawing a "unit load" is *always* OK, except for OTG */
-		nop_set_vbus_draw(nop, 100);
-
 		atomic_notifier_call_chain(&nop->phy.notifier, status,
 					   otg->gadget);
 	} else {
-		nop_set_vbus_draw(nop, 0);
-
 		status = USB_EVENT_NONE;
 		otg->state = OTG_STATE_B_IDLE;
 		nop->phy.last_event = status;
@@ -285,6 +273,7 @@ int usb_phy_gen_create_phy(struct device *dev, struct usb_phy_generic *nop)
 	nop->phy.otg->usb_phy		= &nop->phy;
 	nop->phy.otg->set_host		= nop_set_host;
 	nop->phy.otg->set_peripheral	= nop_set_peripheral;
+	nop->phy.otg->set_vbus          = nop_set_vbus;
 
 	return 0;
 }
Signature · 1 line
2.35.1.1320.gc452695387.dirty
#5 lore raw

PATCH v2 4/4 usb: phy: generic: Disable vbus on removal

In reply to Sean Anderson

If we enabled vbus, we need to balance that with a disable.

---

(no changes since v1)

 drivers/usb/phy/phy-generic.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/usb/phy/phy-generic.c +3 -0
index 2c2553bc9b54..9fc3312d614a 100644
@@ -328,6 +328,9 @@ static int usb_phy_generic_remove(struct platform_device *pdev)
 
 	usb_remove_phy(&nop->phy);
 
+	if (nop->vbus_draw && nop->vbus_draw_enabled)
+		regulator_disable(nop->vbus_draw);
+
 	return 0;
 }
 
Signature · 1 line
2.35.1.1320.gc452695387.dirty