Skip to contentSkip to conversation

usb: common: usb-conn-gpio: Make VBUS supply optional

PATCH In review Thierry Reding 2 participants 3 messages Aug 6, 2020 – Aug 8, 2020

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

PATCH 1/2 usb: common: usb-conn-gpio: Make VBUS supply optional

From: Thierry Reding <[email protected]>

If the connector is the child of a USB port and that USB port already has a VBUS supply attached to it, it would be redundant to require the connector to have a VBUS supply. In this case, allow the VBUS supply to be optional.

---
 drivers/usb/common/usb-conn-gpio.c | 30 ++++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)
drivers/usb/common/usb-conn-gpio.c +24 -6
index 7b3a21360d7c..c5b516d327c7 100644
@@ -91,14 +91,14 @@ static void usb_conn_detect_cable(struct work_struct *work)
 		return;
 	}
 
-	if (info->last_role == USB_ROLE_HOST)
+	if (info->last_role == USB_ROLE_HOST && info->vbus)
 		regulator_disable(info->vbus);
 
 	ret = usb_role_switch_set_role(info->role_sw, role);
 	if (ret)
 		dev_err(info->dev, "failed to set role: %d\n", ret);
 
-	if (role == USB_ROLE_HOST) {
+	if (role == USB_ROLE_HOST && info->vbus) {
 		ret = regulator_enable(info->vbus);
 		if (ret)
 			dev_err(info->dev, "enable vbus regulator failed\n");
@@ -106,8 +106,9 @@ static void usb_conn_detect_cable(struct work_struct *work)
 
 	info->last_role = role;
 
-	dev_dbg(info->dev, "vbus regulator is %s\n",
-		regulator_is_enabled(info->vbus) ? "enabled" : "disabled");
+	if (info->vbus)
+		dev_dbg(info->dev, "vbus regulator is %s\n",
+			regulator_is_enabled(info->vbus) ? "enabled" : "disabled");
 
 	power_supply_changed(info->charger);
 }
@@ -156,6 +157,7 @@ static int usb_conn_probe(struct platform_device *pdev)
 	struct power_supply_config cfg = {
 		.of_node = dev->of_node,
 	};
+	bool need_vbus = true;
 	int ret = 0;
 
 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
@@ -185,7 +187,23 @@ static int usb_conn_probe(struct platform_device *pdev)
 
 	INIT_DELAYED_WORK(&info->dw_det, usb_conn_detect_cable);
 
-	info->vbus = devm_regulator_get(dev, "vbus");
+	/*
+	 * If the USB connector is a child of a USB port and that port already provides the VBUS
+	 * supply, there's no need for the USB connector to provide it again.
+	 */
+	if (dev->parent && dev->parent->of_node) {
+		if (of_find_property(dev->parent->of_node, "vbus-supply", NULL))
+			need_vbus = false;
+	}
+
+	if (!need_vbus) {
+		info->vbus = devm_regulator_get_optional(dev, "vbus");
+		if (PTR_ERR(info->vbus) == -ENODEV)
+			info->vbus = NULL;
+	} else {
+		info->vbus = devm_regulator_get(dev, "vbus");
+	}
+
 	if (IS_ERR(info->vbus)) {
 		if (PTR_ERR(info->vbus) != -EPROBE_DEFER)
 			dev_err(dev, "failed to get vbus\n");
@@ -266,7 +284,7 @@ static int usb_conn_remove(struct platform_device *pdev)
 
 	cancel_delayed_work_sync(&info->dw_det);
 
-	if (info->last_role == USB_ROLE_HOST)
+	if (info->last_role == USB_ROLE_HOST && info->vbus)
 		regulator_disable(info->vbus);
 
 	usb_role_switch_put(info->role_sw);
Signature · 1 line
2.27.0
#1 lore raw

PATCH 2/2 usb: common: usb-conn-gpio: Print error on failure to get VBUS

From: Thierry Reding <[email protected]>

The exact error that happened trying to get the VBUS supply can be useful to troubleshoot what's going on.

---
 drivers/usb/common/usb-conn-gpio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/common/usb-conn-gpio.c +1 -1
index c5b516d327c7..6c4e3a19f42c 100644
@@ -206,7 +206,7 @@ static int usb_conn_probe(struct platform_device *pdev)
 
 	if (IS_ERR(info->vbus)) {
 		if (PTR_ERR(info->vbus) != -EPROBE_DEFER)
-			dev_err(dev, "failed to get vbus\n");
+			dev_err(dev, "failed to get vbus: %ld\n", PTR_ERR(info->vbus));
 		return PTR_ERR(info->vbus);
 	}
 
Signature · 1 line
2.27.0
#2 lore raw

Re: [PATCH 2/2] usb: common: usb-conn-gpio: Print error on failure to get VBUS

Quoted 2 lines · Thierry Reding

The exact error that happened trying to get the VBUS supply can be useful to troubleshoot what's going on.

[...]

drivers/usb/common/usb-conn-gpio.c static int usb_conn_probe(struct platform_device *pdev) · line 209

Quoted 8 lines
drivers/usb/common/usb-conn-gpio.c +1 -1
@@ -206,7 +206,7 @@ static int usb_conn_probe(struct platform_device *pdev)
 
 	if (IS_ERR(info->vbus)) {
 		if (PTR_ERR(info->vbus) != -EPROBE_DEFER)
-			dev_err(dev, "failed to get vbus\n");
+			dev_err(dev, "failed to get vbus: %ld\n", PTR_ERR(info->vbus));

There is now a nice "%pe" format, that can make this even better.

Best Regards,
Michał Mirosław