[U-Boot] [PATCH usb RFC] usb: Make vbus-supply code common

Marek BehĂșn marek.behun at nic.cz
Thu Nov 8 13:13:43 UTC 2018


The code for vbus-supply regulator enabling/disabling is repeated in
several drivers. Create a header file with static inline definitions
of the funtions and use them in some of those drivers.

Signed-off-by: Marek BehĂșn <marek.behun at nic.cz>
---
 drivers/usb/host/dwc2.c         | 61 ++++-----------------------------
 drivers/usb/host/ehci-generic.c | 51 +++------------------------
 drivers/usb/host/vbus-supply.h  | 49 ++++++++++++++++++++++++++
 drivers/usb/host/xhci-mvebu.c   | 28 +++++++++------
 4 files changed, 76 insertions(+), 113 deletions(-)
 create mode 100644 drivers/usb/host/vbus-supply.h

diff --git a/drivers/usb/host/dwc2.c b/drivers/usb/host/dwc2.c
index b6f008a400..51a2e21a6b 100644
--- a/drivers/usb/host/dwc2.c
+++ b/drivers/usb/host/dwc2.c
@@ -14,9 +14,9 @@
 #include <usbroothubdes.h>
 #include <wait_bit.h>
 #include <asm/io.h>
-#include <power/regulator.h>
 #include <reset.h>
 
+#include "vbus-supply.h"
 #include "dwc2.h"
 
 /* Use only HC channel 0. */
@@ -168,57 +168,6 @@ static void dwc_otg_core_reset(struct dwc2_core_regs *regs)
 	mdelay(100);
 }
 
-#if defined(CONFIG_DM_USB) && defined(CONFIG_DM_REGULATOR)
-static int dwc_vbus_supply_init(struct udevice *dev)
-{
-	struct dwc2_priv *priv = dev_get_priv(dev);
-	int ret;
-
-	ret = device_get_supply_regulator(dev, "vbus-supply",
-					  &priv->vbus_supply);
-	if (ret) {
-		debug("%s: No vbus supply\n", dev->name);
-		return 0;
-	}
-
-	ret = regulator_set_enable(priv->vbus_supply, true);
-	if (ret) {
-		dev_err(dev, "Error enabling vbus supply\n");
-		return ret;
-	}
-
-	return 0;
-}
-
-static int dwc_vbus_supply_exit(struct udevice *dev)
-{
-	struct dwc2_priv *priv = dev_get_priv(dev);
-	int ret;
-
-	if (priv->vbus_supply) {
-		ret = regulator_set_enable(priv->vbus_supply, false);
-		if (ret) {
-			dev_err(dev, "Error disabling vbus supply\n");
-			return ret;
-		}
-	}
-
-	return 0;
-}
-#else
-static int dwc_vbus_supply_init(struct udevice *dev)
-{
-	return 0;
-}
-
-#if defined(CONFIG_DM_USB)
-static int dwc_vbus_supply_exit(struct udevice *dev)
-{
-	return 0;
-}
-#endif
-#endif
-
 /*
  * This function initializes the DWC_otg controller registers for
  * host mode.
@@ -311,8 +260,10 @@ static void dwc_otg_core_host_init(struct udevice *dev,
 		}
 	}
 
-	if (dev)
-		dwc_vbus_supply_init(dev);
+	if (dev) {
+		struct dwc2_priv *priv = dev_get_priv(dev);
+		enable_vbus_supply(dev, &priv->vbus_supply);
+	}
 }
 
 /*
@@ -1332,7 +1283,7 @@ static int dwc2_usb_remove(struct udevice *dev)
 	struct dwc2_priv *priv = dev_get_priv(dev);
 	int ret;
 
-	ret = dwc_vbus_supply_exit(dev);
+	ret = disable_vbus_supply(priv->vbus_supply);
 	if (ret)
 		return ret;
 
diff --git a/drivers/usb/host/ehci-generic.c b/drivers/usb/host/ehci-generic.c
index 0270f3bc95..e47263ba58 100644
--- a/drivers/usb/host/ehci-generic.c
+++ b/drivers/usb/host/ehci-generic.c
@@ -11,7 +11,7 @@
 #include <asm/io.h>
 #include <dm.h>
 #include "ehci.h"
-#include <power/regulator.h>
+#include "vbus-supply.h"
 
 /*
  * Even though here we don't explicitly use "struct ehci_ctrl"
@@ -30,49 +30,6 @@ struct generic_ehci {
 	int reset_count;
 };
 
-#ifdef CONFIG_DM_REGULATOR
-static int ehci_enable_vbus_supply(struct udevice *dev)
-{
-	struct generic_ehci *priv = dev_get_priv(dev);
-	int ret;
-
-	ret = device_get_supply_regulator(dev, "vbus-supply",
-					  &priv->vbus_supply);
-	if (ret && ret != -ENOENT)
-		return ret;
-
-	if (priv->vbus_supply) {
-		ret = regulator_set_enable(priv->vbus_supply, true);
-		if (ret) {
-			dev_err(dev, "Error enabling VBUS supply\n");
-			return ret;
-		}
-	} else {
-		dev_dbg(dev, "No vbus supply\n");
-	}
-
-	return 0;
-}
-
-static int ehci_disable_vbus_supply(struct generic_ehci *priv)
-{
-	if (priv->vbus_supply)
-		return regulator_set_enable(priv->vbus_supply, false);
-	else
-		return 0;
-}
-#else
-static int ehci_enable_vbus_supply(struct udevice *dev)
-{
-	return 0;
-}
-
-static int ehci_disable_vbus_supply(struct generic_ehci *priv)
-{
-	return 0;
-}
-#endif
-
 static int ehci_usb_probe(struct udevice *dev)
 {
 	struct generic_ehci *priv = dev_get_priv(dev);
@@ -142,7 +99,7 @@ static int ehci_usb_probe(struct udevice *dev)
 		}
 	}
 
-	err = ehci_enable_vbus_supply(dev);
+	err = enable_vbus_supply(dev, &priv->vbus_supply);
 	if (err)
 		goto reset_err;
 
@@ -166,7 +123,7 @@ phy_err:
 		dev_err(dev, "failed to shutdown usb phy\n");
 
 regulator_err:
-	ret = ehci_disable_vbus_supply(priv);
+	ret = disable_vbus_supply(priv->vbus_supply);
 	if (ret)
 		dev_err(dev, "failed to disable VBUS supply\n");
 
@@ -195,7 +152,7 @@ static int ehci_usb_remove(struct udevice *dev)
 	if (ret)
 		return ret;
 
-	ret = ehci_disable_vbus_supply(priv);
+	ret = disable_vbus_supply(priv->vbus_supply);
 	if (ret)
 		return ret;
 
diff --git a/drivers/usb/host/vbus-supply.h b/drivers/usb/host/vbus-supply.h
new file mode 100644
index 0000000000..dcd48ab86d
--- /dev/null
+++ b/drivers/usb/host/vbus-supply.h
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2015 Alexey Brodkin <abrodkin at synopsys.com>
+ * Copyright (C) 2018 Marek Behun <marek.behun at nic.cz>
+ */
+
+#ifndef _VBUS_SUPPLY_H_
+#define _VBUS_SUPPLY_H_
+
+#include <common.h>
+#include <dm.h>
+#include <power/regulator.h>
+
+#ifdef CONFIG_DM_REGULATOR
+static inline int enable_vbus_supply(struct udevice *dev,
+				     struct udevice **vbus_supply)
+{
+	int ret;
+
+	ret = device_get_supply_regulator(dev, "vbus-supply", vbus_supply);
+	if (ret && ret != -ENOENT)
+		return ret;
+
+	if (*vbus_supply) {
+		ret = regulator_set_enable(*vbus_supply, true);
+		if (ret) {
+			dev_err(dev, "Error enabling VBUS supply\n");
+			return ret;
+		}
+	} else {
+		dev_dbg(dev, "No vbus supply\n");
+	}
+
+	return 0;
+}
+
+static inline int disable_vbus_supply(struct udevice *vbus_supply)
+{
+	if (vbus_supply)
+		return regulator_set_enable(vbus_supply, false);
+	else
+		return 0;
+}
+#else
+#define enable_vbus_supply(dev,supply)		0
+#define disable_vbus_supply(supply)		0
+#endif
+
+#endif /* _VBUS_SUPPLY_H_ */
diff --git a/drivers/usb/host/xhci-mvebu.c b/drivers/usb/host/xhci-mvebu.c
index b6c6aaf78e..11aea269d0 100644
--- a/drivers/usb/host/xhci-mvebu.c
+++ b/drivers/usb/host/xhci-mvebu.c
@@ -9,9 +9,9 @@
 #include <dm.h>
 #include <fdtdec.h>
 #include <usb.h>
-#include <power/regulator.h>
 #include <asm/gpio.h>
 
+#include "vbus-supply.h"
 #include "xhci.h"
 
 struct mvebu_xhci_platdata {
@@ -26,6 +26,9 @@ struct mvebu_xhci {
 	struct xhci_ctrl ctrl;	/* Needs to come first in this struct! */
 	struct usb_platdata usb_plat;
 	struct xhci_hccr *hcd;
+#ifdef CONFIG_DM_REGULATOR
+	struct udevice *vbus_supply;
+#endif
 };
 
 /*
@@ -43,20 +46,14 @@ static int xhci_usb_probe(struct udevice *dev)
 	struct mvebu_xhci *ctx = dev_get_priv(dev);
 	struct xhci_hcor *hcor;
 	int len, ret;
-	struct udevice *regulator;
 
 	ctx->hcd = (struct xhci_hccr *)plat->hcd_base;
 	len = HC_LENGTH(xhci_readl(&ctx->hcd->cr_capbase));
 	hcor = (struct xhci_hcor *)((uintptr_t)ctx->hcd + len);
 
-	ret = device_get_supply_regulator(dev, "vbus-supply", &regulator);
-	if (!ret) {
-		ret = regulator_set_enable(regulator, true);
-		if (ret) {
-			printf("Failed to turn ON the VBUS regulator\n");
-			return ret;
-		}
-	}
+	ret = enable_vbus_supply(dev, &ctx->vbus_supply);
+	if (ret < 0)
+		return ret;
 
 	/* Enable USB xHCI (VBUS, reset etc) in board specific code */
 	board_xhci_enable(devfdt_get_addr_index(dev, 1));
@@ -64,6 +61,15 @@ static int xhci_usb_probe(struct udevice *dev)
 	return xhci_register(dev, ctx->hcd, hcor);
 }
 
+static int xhci_usb_remove(struct udevice *dev)
+{
+	struct mvebu_xhci *ctx = dev_get_priv(dev);
+
+	disable_vbus_supply(ctx->vbus_supply);
+
+	return xhci_deregister(dev);
+}
+
 static int xhci_usb_ofdata_to_platdata(struct udevice *dev)
 {
 	struct mvebu_xhci_platdata *plat = dev_get_platdata(dev);
@@ -93,7 +99,7 @@ U_BOOT_DRIVER(usb_xhci) = {
 	.of_match = xhci_usb_ids,
 	.ofdata_to_platdata = xhci_usb_ofdata_to_platdata,
 	.probe = xhci_usb_probe,
-	.remove = xhci_deregister,
+	.remove = xhci_usb_remove,
 	.ops	= &xhci_usb_ops,
 	.platdata_auto_alloc_size = sizeof(struct mvebu_xhci_platdata),
 	.priv_auto_alloc_size = sizeof(struct mvebu_xhci),
-- 
2.18.1



More information about the U-Boot mailing list