From e61c5f796e143ff2f183f09a4ef440e4cdf77370 Mon Sep 17 00:00:00 2001
From: Ondrej Jirman <megi@xff.cz>
Date: Sun, 30 Apr 2023 18:54:51 +0200
Subject: [PATCH 344/463] usb: dwc3: Track the power state of usb3_generic_phy

We will need to manage power state of this phy inisde set_mode work,
without any ability to perform recovery if power on fails, so we'll
need to track result of power on separately, to be able to balance
the phy on/off calls.

Signed-off-by: Ondrej Jirman <megi@xff.cz>
---
 drivers/usb/dwc3/core.c | 7 +++++--
 drivers/usb/dwc3/core.h | 2 ++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
index 9eb085f359ce3..09c3affe7019c 100644
--- a/drivers/usb/dwc3/core.c
+++ b/drivers/usb/dwc3/core.c
@@ -875,6 +875,7 @@ static int dwc3_phy_power_on(struct dwc3 *dwc)
 			goto err_power_off_usb3_phy;
 	}
 
+	dwc->usb3_phy_powered = true;
 	return 0;
 
 err_power_off_usb3_phy:
@@ -895,8 +896,10 @@ static void dwc3_phy_power_off(struct dwc3 *dwc)
 {
 	int i;
 
-	for (i = 0; i < dwc->num_usb3_ports; i++)
-		phy_power_off(dwc->usb3_generic_phy[i]);
+	if (dwc->usb3_phy_powered)
+		for (i = 0; i < dwc->num_usb3_ports; i++)
+			phy_power_off(dwc->usb3_generic_phy[i]);
+	dwc->usb3_phy_powered = false;
 
 	for (i = 0; i < dwc->num_usb2_ports; i++)
 		phy_power_off(dwc->usb2_generic_phy[i]);
diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
index c71240e8f7c7d..02d986e3aacb1 100644
--- a/drivers/usb/dwc3/core.h
+++ b/drivers/usb/dwc3/core.h
@@ -1207,6 +1207,8 @@ struct dwc3 {
 	u8			num_usb2_ports;
 	u8			num_usb3_ports;
 
+	bool			usb3_phy_powered;
+
 	bool			phys_ready;
 
 	struct ulpi		*ulpi;
-- 
2.46.0

