From 276e164cec9a78150383a5d622700cd9ffbd1a13 Mon Sep 17 00:00:00 2001
From: Ondrej Jirman <megi@xff.cz>
Date: Sat, 2 Mar 2024 14:33:21 +0100
Subject: [PATCH 348/463] usb: dwc3: Extend reset quirk support to include
 role-switch

Originally my reset quirk patch only supported extcon mode changes
via extcon interface. Support role-switch, too.

Signed-off-by: Ondrej Jirman <megi@xff.cz>
---
 drivers/usb/dwc3/drd.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/dwc3/drd.c b/drivers/usb/dwc3/drd.c
index 338eba5855da1..491766bed9b5f 100644
--- a/drivers/usb/dwc3/drd.c
+++ b/drivers/usb/dwc3/drd.c
@@ -458,7 +458,7 @@ static int dwc3_usb_role_switch_set(struct usb_role_switch *sw,
 				    enum usb_role role)
 {
 	struct dwc3 *dwc = usb_role_switch_get_drvdata(sw);
-	u32 mode;
+	u32 mode = DWC3_GCTL_PRTCAP_DEVICE_DISCONNECTED;
 
 	switch (role) {
 	case USB_ROLE_HOST:
@@ -468,6 +468,8 @@ static int dwc3_usb_role_switch_set(struct usb_role_switch *sw,
 		mode = DWC3_GCTL_PRTCAP_DEVICE;
 		break;
 	default:
+		if (dwc->usb3_phy_reset_quirk)
+			break;
 		if (dwc->role_switch_default_mode == USB_DR_MODE_HOST)
 			mode = DWC3_GCTL_PRTCAP_HOST;
 		else
@@ -487,6 +489,9 @@ static enum usb_role dwc3_usb_role_switch_get(struct usb_role_switch *sw)
 
 	spin_lock_irqsave(&dwc->lock, flags);
 	switch (dwc->current_dr_role) {
+	case DWC3_GCTL_PRTCAP_DEVICE_DISCONNECTED:
+		role = USB_ROLE_NONE;
+		break;
 	case DWC3_GCTL_PRTCAP_HOST:
 		role = USB_ROLE_HOST;
 		break;
@@ -518,6 +523,8 @@ static int dwc3_setup_role_switch(struct dwc3 *dwc)
 	} else {
 		dwc->role_switch_default_mode = USB_DR_MODE_PERIPHERAL;
 		mode = DWC3_GCTL_PRTCAP_DEVICE;
+		if (dwc->usb3_phy_reset_quirk)
+			mode = DWC3_GCTL_PRTCAP_DEVICE_DISCONNECTED;
 	}
 	dwc3_set_mode(dwc, mode);
 
-- 
2.46.0

