Skip to contentSkip to conversation

usb: common: allow dr_mode to come from gpio-id based usb-connector

PATCH Superseded Tim Harvey 2 participants 4 messages Jul 9, 2025 – Jul 25, 2025

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Tim Harvey

author
#0 lore raw

PATCH v2 1/2 usb: common: allow dr_mode to come from gpio-id based usb-connector

If a usb host with dr_mode of "otg" has a usb-connector using a GPIO ID pin use this to determine host vs peripheral.

---
v2
- do not display error if 'port' node not found
- change print to debug

---
 drivers/usb/common/common.c | 94 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 93 insertions(+), 1 deletion(-)
drivers/usb/common/common.c +93 -1
index 13e9a61072a9..85865712bfcc 100644
@@ -8,6 +8,7 @@
 
 #include <dm.h>
 #include <asm/global_data.h>
+#include <asm/gpio.h>
 #include <linux/printk.h>
 #include <linux/usb/otg.h>
 #include <linux/usb/ch9.h>
@@ -22,6 +23,97 @@ static const char *const usb_dr_modes[] = {
 	[USB_DR_MODE_OTG]		= "otg",
 };
 
+/**
+ * get_remote_node_from_graph - Resolve the remote node from a graph binding
+ * @node: Starting ofnode (e.g., connector)
+ * @port_id: Port unit address (e.g., 0 for port@0, or -1 for first port)
+ * @endpoint_id: Endpoint unit address (e.g., 0 for endpoint@0, or -1 for first endpoint)
+ * Return: ofnode of the remote node, or ofnode_null() on failure
+ */
+static ofnode get_remote_node_from_graph(ofnode node, int port_id, int endpoint_id)
+{
+	ofnode port_node, endpoint_node, remote_node;
+	u32 phandle_value;
+	char port_name[16];
+	char endpoint_name[16];
+
+	/* Validate the starting node */
+	if (!ofnode_valid(node)) {
+		debug("Invalid starting node\n");
+		return ofnode_null();
+	}
+
+	/* Construct port name (e.g., "port" or "port@0") */
+	if (port_id == -1)
+		strcpy(port_name, "port");
+	else
+		snprintf(port_name, sizeof(port_name), "port@%d", port_id);
+
+	/* look for a 'port' node */
+	port_node = ofnode_find_subnode(node, port_name);
+	if (!ofnode_valid(port_node))
+		return ofnode_null();
+
+	/* Construct endpoint name (e.g., "endpoint" or "endpoint@0") */
+	if (endpoint_id == -1)
+		strcpy(endpoint_name, "endpoint");
+	else
+		snprintf(endpoint_name, sizeof(endpoint_name), "endpoint@%d", endpoint_id);
+
+	/* Find the 'endpoint' node */
+	endpoint_node = ofnode_find_subnode(port_node, endpoint_name);
+	if (!ofnode_valid(endpoint_node)) {
+		printf("No '%s' node found under '%s'\n", endpoint_name, port_name);
+		return ofnode_null();
+	}
+
+	/* Read the remote-endpoint phandle */
+	phandle_value = ofnode_read_u32_default(endpoint_node, "remote-endpoint", 0);
+	if (phandle_value == 0) {
+		printf("No valid 'remote-endpoint' phandle in '%s'\n", endpoint_name);
+		return ofnode_null();
+	}
+
+	/* Resolve the phandle to the remote node */
+	remote_node = ofnode_get_by_phandle(phandle_value);
+	if (!ofnode_valid(remote_node)) {
+		printf("Failed to resolve phandle %u\n", phandle_value);
+		return ofnode_null();
+	}
+
+	return remote_node;
+}
+
+static enum usb_dr_mode get_connector_drmode(ofnode node)
+{
+	struct gpio_desc id;
+	enum usb_dr_mode dr_mode = USB_DR_MODE_OTG;
+	ofnode conn;
+
+	/* get remote endpoint */
+	conn = get_remote_node_from_graph(node, -1, -1);
+	/* get port endpoint */
+	if (ofnode_valid(conn))
+		conn = ofnode_get_parent(conn);
+	/* get connector */
+	if (ofnode_valid(conn))
+		conn = ofnode_get_parent(conn);
+	if (ofnode_valid(conn) &&
+	    ofnode_device_is_compatible(conn, "gpio-usb-b-connector") &&
+	    !gpio_request_by_name_nodev(conn, "id-gpios", 0, &id, GPIOD_IS_IN)) {
+		if (dm_gpio_get_value(&id))
+			dr_mode = USB_DR_MODE_PERIPHERAL;
+		else
+			dr_mode = USB_DR_MODE_HOST;
+		gpio_free_list_nodev(&id, 1);
+		pr_debug("%s got dr_mode from connector %s dr_mode=%s\n", __func__,
+			 ofnode_get_name(node),
+			 dr_mode == USB_DR_MODE_HOST ? "host" : "peripheral");
+	}
+
+	return dr_mode;
+}
+
 enum usb_dr_mode usb_get_dr_mode(ofnode node)
 {
 	const char *dr_mode;
@@ -35,7 +127,7 @@ enum usb_dr_mode usb_get_dr_mode(ofnode node)
 
 	for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
 		if (!strcmp(dr_mode, usb_dr_modes[i]))
-			return i;
+			return (i == USB_DR_MODE_OTG) ?  get_connector_drmode(node) : i;
 
 	return USB_DR_MODE_UNKNOWN;
 }
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v2 2/2 imx8mp-venice: enable USB device mode

Now that we can determine device role via a usb-connector ID pin, remove the host-only mode dt override and enable additional USB device mode support.

---
v2: no changes

---
 arch/arm/dts/imx8mp-venice-gw71xx-2x-u-boot.dtsi | 5 -----
 arch/arm/dts/imx8mp-venice-gw72xx-2x-u-boot.dtsi | 5 -----
 arch/arm/dts/imx8mp-venice-gw73xx-2x-u-boot.dtsi | 5 -----
 arch/arm/dts/imx8mp-venice-gw74xx-u-boot.dtsi    | 5 -----
 configs/imx8mp_venice_defconfig                  | 7 +++++++
 5 files changed, 7 insertions(+), 20 deletions(-)
arch/arm/dts/imx8mp-venice-gw71xx-2x-u-boot.dtsi +0 -5
index a291b7abab62..c1d21e51c4e6 100644
@@ -54,8 +54,3 @@
 		line-name = "pci_wdis#";
 	};
 };
-
-/* gpio-usb-con not supported yet in U-Boot so make this a host for now */
-&usb_dwc3_0 {
-	dr_mode = "host";
-};
arch/arm/dts/imx8mp-venice-gw72xx-2x-u-boot.dtsi +0 -5
index bdf5370fcdf6..9f6d05fdae0c 100644
@@ -89,8 +89,3 @@
 		line-name = "pci_wdis#";
 	};
 };
-
-/* gpio-usb-con not supported yet in U-Boot so make this a host for now */
-&usb_dwc3_0 {
-	dr_mode = "host";
-};
arch/arm/dts/imx8mp-venice-gw73xx-2x-u-boot.dtsi +0 -5
index 7e6f66bd9dd9..3b23802e9a7d 100644
@@ -95,8 +95,3 @@
 		line-name = "pci_wdis#";
 	};
 };
-
-/* gpio-usb-con not supported yet in U-Boot so make this a host for now */
-&usb_dwc3_0 {
-	dr_mode = "host";
-};
arch/arm/dts/imx8mp-venice-gw74xx-u-boot.dtsi +0 -5
index 95f5f15e742a..a82ce5a19788 100644
@@ -250,8 +250,3 @@
 &wdog1 {
 	bootph-pre-ram;
 };
-
-/* gpio-usb-con not supported yet in U-Boot so make this a host for now */
-&usb_dwc3_0 {
-	dr_mode = "host";
-};
configs/imx8mp_venice_defconfig +7 -0
index 158ce4327488..2c9c6e5564fc 100644
@@ -70,6 +70,7 @@ CONFIG_CMD_I2C=y
 CONFIG_CMD_MMC=y
 CONFIG_CMD_PCI=y
 CONFIG_CMD_USB=y
+CONFIG_CMD_USB_MASS_STORAGE=y
 CONFIG_CMD_CAT=y
 CONFIG_CMD_SETEXPR_FMT=y
 CONFIG_CMD_XXD=y
@@ -163,6 +164,7 @@ CONFIG_IMX_TMU=y
 # CONFIG_TPM_V1 is not set
 CONFIG_TPM2_TIS_SPI=y
 CONFIG_USB=y
+CONFIG_DM_USB_GADGET=y
 CONFIG_USB_XHCI_HCD=y
 CONFIG_USB_XHCI_DWC3=y
 CONFIG_USB_XHCI_DWC3_OF_SIMPLE=y
@@ -178,6 +180,11 @@ CONFIG_USB_ETHER_LAN78XX=y
 CONFIG_USB_ETHER_MCS7830=y
 CONFIG_USB_ETHER_RTL8152=y
 CONFIG_USB_ETHER_SMSC95XX=y
+CONFIG_USB_GADGET=y
+CONFIG_USB_GADGET_MANUFACTURER="Gateworks"
+CONFIG_USB_GADGET_VENDOR_NUM=0x0525
+CONFIG_USB_GADGET_PRODUCT_NUM=0xa4a5
+CONFIG_USB_GADGET_DOWNLOAD=y
 CONFIG_IMX_WATCHDOG=y
 CONFIG_TPM=y
 # CONFIG_SPL_SHA512 is not set
Signature · 1 line
2.25.1
reviewer
#2 lore raw

Re: [v2 PATCH 2/2] imx8mp-venice: enable USB device mode

Hi Tim,

Quoted 6 lines · Tim Harvey

Now that we can determine device role via a usb-connector ID pin, remove the host-only mode dt override and enable additional USB device mode support.

Please resend it after the driver part has been applied.

Thanks

author
#3 lore raw
In reply to Tim Harvey
Quoted 133 lines · Tim Harvey

If a usb host with dr_mode of "otg" has a usb-connector using a GPIO ID pin use this to determine host vs peripheral.

---
v2
- do not display error if 'port' node not found
- change print to debug

---
 drivers/usb/common/common.c | 94 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 93 insertions(+), 1 deletion(-)
drivers/usb/common/common.c +93 -2
index 13e9a61072a9..85865712bfcc 100644
@@ -8,6 +8,7 @@
 
 #include <dm.h>
 #include <asm/global_data.h>
+#include <asm/gpio.h>
 #include <linux/printk.h>
 #include <linux/usb/otg.h>
 #include <linux/usb/ch9.h>
@@ -22,6 +23,97 @@ static const char *const usb_dr_modes[] = {
        [USB_DR_MODE_OTG]               = "otg",
 };
 
+/**
+ * get_remote_node_from_graph - Resolve the remote node from a graph binding
+ * @node: Starting ofnode (e.g., connector)
+ * @port_id: Port unit address (e.g., 0 for port@0, or -1 for first port)
+ * @endpoint_id: Endpoint unit address (e.g., 0 for endpoint@0, or -1 for first endpoint)
+ * Return: ofnode of the remote node, or ofnode_null() on failure
+ */
+static ofnode get_remote_node_from_graph(ofnode node, int port_id, int endpoint_id)
+{
+       ofnode port_node, endpoint_node, remote_node;
+       u32 phandle_value;
+       char port_name[16];
+       char endpoint_name[16];
+
+       /* Validate the starting node */
+       if (!ofnode_valid(node)) {
+               debug("Invalid starting node\n");
+               return ofnode_null();
+       }
+
+       /* Construct port name (e.g., "port" or "port@0") */
+       if (port_id == -1)
+               strcpy(port_name, "port");
+       else
+               snprintf(port_name, sizeof(port_name), "port@%d", port_id);
+
+       /* look for a 'port' node */
+       port_node = ofnode_find_subnode(node, port_name);
+       if (!ofnode_valid(port_node))
+               return ofnode_null();
+
+       /* Construct endpoint name (e.g., "endpoint" or "endpoint@0") */
+       if (endpoint_id == -1)
+               strcpy(endpoint_name, "endpoint");
+       else
+               snprintf(endpoint_name, sizeof(endpoint_name), "endpoint@%d", endpoint_id);
+
+       /* Find the 'endpoint' node */
+       endpoint_node = ofnode_find_subnode(port_node, endpoint_name);
+       if (!ofnode_valid(endpoint_node)) {
+               printf("No '%s' node found under '%s'\n", endpoint_name, port_name);
+               return ofnode_null();
+       }
+
+       /* Read the remote-endpoint phandle */
+       phandle_value = ofnode_read_u32_default(endpoint_node, "remote-endpoint", 0);
+       if (phandle_value == 0) {
+               printf("No valid 'remote-endpoint' phandle in '%s'\n", endpoint_name);
+               return ofnode_null();
+       }
+
+       /* Resolve the phandle to the remote node */
+       remote_node = ofnode_get_by_phandle(phandle_value);
+       if (!ofnode_valid(remote_node)) {
+               printf("Failed to resolve phandle %u\n", phandle_value);
+               return ofnode_null();
+       }
+
+       return remote_node;
+}
+
+static enum usb_dr_mode get_connector_drmode(ofnode node)
+{
+       struct gpio_desc id;
+       enum usb_dr_mode dr_mode = USB_DR_MODE_OTG;
+       ofnode conn;
+
+       /* get remote endpoint */
+       conn = get_remote_node_from_graph(node, -1, -1);
+       /* get port endpoint */
+       if (ofnode_valid(conn))
+               conn = ofnode_get_parent(conn);
+       /* get connector */
+       if (ofnode_valid(conn))
+               conn = ofnode_get_parent(conn);
+       if (ofnode_valid(conn) &&
+           ofnode_device_is_compatible(conn, "gpio-usb-b-connector") &&
+           !gpio_request_by_name_nodev(conn, "id-gpios", 0, &id, GPIOD_IS_IN)) {
+               if (dm_gpio_get_value(&id))
+                       dr_mode = USB_DR_MODE_PERIPHERAL;
+               else
+                       dr_mode = USB_DR_MODE_HOST;
+               gpio_free_list_nodev(&id, 1);
+               pr_debug("%s got dr_mode from connector %s dr_mode=%s\n", __func__,
+                        ofnode_get_name(node),
+                        dr_mode == USB_DR_MODE_HOST ? "host" : "peripheral");
+       }
+
+       return dr_mode;
+}
+
 enum usb_dr_mode usb_get_dr_mode(ofnode node)
 {
        const char *dr_mode;
@@ -35,7 +127,7 @@ enum usb_dr_mode usb_get_dr_mode(ofnode node)
 
        for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
                if (!strcmp(dr_mode, usb_dr_modes[i]))
-                       return i;
+                       return (i == USB_DR_MODE_OTG) ?  get_connector_drmode(node) : i;
 
        return USB_DR_MODE_UNKNOWN;
 }
--
Quoted 2 lines

2.25.1

Hi Marek,

Have you had a chance to review this yet?

Best Regards,

Tim