[U-Boot] [PATCH v2 5/7] sunxi: common VBUS detection logic in usbc

Paul Kocialkowski contact at paulk.fr
Sun Mar 22 18:07:12 CET 2015


VBUS detection could be needed not only by the musb code (to prevent host mode),
but also by e.g. gadget drivers to start only when a cable is connected.

In addition, this allows more flexibility in vbus detection, as it could easily
be extended to other USBC indexes. Eventually, this would help making musb
support independent from a hardcoded USB controller index (0).

Signed-off-by: Paul Kocialkowski <contact at paulk.fr>
---
 arch/arm/cpu/armv7/sunxi/usbc.c        | 47 +++++++++++++++++++++++++++++++---
 arch/arm/include/asm/arch-sunxi/usbc.h |  1 +
 board/sunxi/Kconfig                    |  1 -
 drivers/usb/musb-new/sunxi.c           | 35 +++++--------------------
 4 files changed, 50 insertions(+), 34 deletions(-)

diff --git a/arch/arm/cpu/armv7/sunxi/usbc.c b/arch/arm/cpu/armv7/sunxi/usbc.c
index 4d8a9a1..d2c6f23 100644
--- a/arch/arm/cpu/armv7/sunxi/usbc.c
+++ b/arch/arm/cpu/armv7/sunxi/usbc.c
@@ -41,6 +41,7 @@ static struct sunxi_usbc_hcd {
 	int usb_rst_mask;
 	int ahb_clk_mask;
 	int gpio_vbus;
+	int gpio_vbus_det;
 	int irq;
 	int id;
 } sunxi_usbc_hcd[] = {
@@ -104,6 +105,14 @@ static int get_vbus_gpio(int index)
 	return -1;
 }
 
+static int get_vbus_detect_gpio(int index)
+{
+	switch (index) {
+	case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_DET);
+	}
+	return -1;
+}
+
 static void usb_phy_write(struct sunxi_usbc_hcd *sunxi_usbc, int addr,
 			  int data, int len)
 {
@@ -176,22 +185,31 @@ static void sunxi_usb_passby(struct sunxi_usbc_hcd *sunxi_usbc, int enable)
 int sunxi_usbc_request_resources(int index)
 {
 	struct sunxi_usbc_hcd *sunxi_usbc = &sunxi_usbc_hcd[index];
+	int ret = 0;
 
 	sunxi_usbc->gpio_vbus = get_vbus_gpio(index);
 	if (sunxi_usbc->gpio_vbus != -1)
-		return gpio_request(sunxi_usbc->gpio_vbus, "usbc_vbus");
+		ret |= gpio_request(sunxi_usbc->gpio_vbus, "usbc_vbus");
+
+	sunxi_usbc->gpio_vbus_det = get_vbus_detect_gpio(index);
+	if (sunxi_usbc->gpio_vbus_det != -1)
+		ret |= gpio_request(sunxi_usbc->gpio_vbus_det, "usbc_vbus_det");
 
-	return 0;
+	return ret;
 }
 
 int sunxi_usbc_free_resources(int index)
 {
 	struct sunxi_usbc_hcd *sunxi_usbc = &sunxi_usbc_hcd[index];
+	int ret = 0;
 
 	if (sunxi_usbc->gpio_vbus != -1)
-		return gpio_free(sunxi_usbc->gpio_vbus);
+		ret |= gpio_free(sunxi_usbc->gpio_vbus);
 
-	return 0;
+	if (sunxi_usbc->gpio_vbus_det != -1)
+		ret |= gpio_free(sunxi_usbc->gpio_vbus_det);
+
+	return ret;
 }
 
 void sunxi_usbc_enable(int index)
@@ -253,3 +271,24 @@ void sunxi_usbc_vbus_disable(int index)
 	if (sunxi_usbc->gpio_vbus != -1)
 		gpio_direction_output(sunxi_usbc->gpio_vbus, 0);
 }
+
+int sunxi_usbc_vbus_detect(int index)
+{
+	struct sunxi_usbc_hcd *sunxi_usbc = &sunxi_usbc_hcd[index];
+	int err;
+
+	if (sunxi_usbc->gpio_vbus_det == -1) {
+		eprintf("Error: invalid vbus detection pin\n");
+		return -1;
+	}
+
+	err = gpio_direction_input(sunxi_usbc->gpio_vbus_det);
+	if (err)
+		return err;
+
+	err = gpio_get_value(sunxi_usbc->gpio_vbus_det);
+	if (err < 0)
+		return err;
+
+	return err;
+}
diff --git a/arch/arm/include/asm/arch-sunxi/usbc.h b/arch/arm/include/asm/arch-sunxi/usbc.h
index cb538cd..67281ec 100644
--- a/arch/arm/include/asm/arch-sunxi/usbc.h
+++ b/arch/arm/include/asm/arch-sunxi/usbc.h
@@ -20,3 +20,4 @@ void sunxi_usbc_enable(int index);
 void sunxi_usbc_disable(int index);
 void sunxi_usbc_vbus_enable(int index);
 void sunxi_usbc_vbus_disable(int index);
+int sunxi_usbc_vbus_detect(int index);
diff --git a/board/sunxi/Kconfig b/board/sunxi/Kconfig
index 9d0eb91..5639321 100644
--- a/board/sunxi/Kconfig
+++ b/board/sunxi/Kconfig
@@ -225,7 +225,6 @@ config USB0_VBUS_PIN
 
 config USB0_VBUS_DET
 	string "Vbus detect pin for usb0 (otg)"
-	depends on USB_MUSB_SUNXI
 	default ""
 	---help---
 	Set the Vbus detect pin for usb0 (otg). This takes a string in the
diff --git a/drivers/usb/musb-new/sunxi.c b/drivers/usb/musb-new/sunxi.c
index 8049944..c9a6a16 100644
--- a/drivers/usb/musb-new/sunxi.c
+++ b/drivers/usb/musb-new/sunxi.c
@@ -235,42 +235,19 @@ static int sunxi_musb_init(struct musb *musb)
 
 	pr_debug("%s():\n", __func__);
 
-	if (is_host_enabled(musb)) {
-		int vbus_det = sunxi_name_to_gpio(CONFIG_USB0_VBUS_DET);
-
-		if (vbus_det == -1) {
-			eprintf("Error invalid Vusb-det pin\n");
-			return -EINVAL;
-		}
-
-		err = gpio_request(vbus_det, "vbus0_det");
-		if (err)
-			return err;
-
-		err = gpio_direction_input(vbus_det);
-		if (err) {
-			gpio_free(vbus_det);
-			return err;
-		}
-
-		err = gpio_get_value(vbus_det);
-		if (err < 0) {
-			gpio_free(vbus_det);
-			return -EIO;
-		}
-
-		gpio_free(vbus_det);
+	err = sunxi_usbc_request_resources(0);
+	if (err)
+		return err;
 
+	if (is_host_enabled(musb)) {
+		err = sunxi_usbc_vbus_detect(0);
 		if (err) {
 			eprintf("Error: A charger is plugged into the OTG\n");
+			sunxi_usbc_free_resources(0);
 			return -EIO;
 		}
 	}
 
-	err = sunxi_usbc_request_resources(0);
-	if (err)
-		return err;
-
 	musb->isr = sunxi_musb_interrupt;
 	sunxi_usbc_enable(0);
 
-- 
1.9.1



More information about the U-Boot mailing list