From d3bef81bd427caf4cbf7ecef64f0268a6ac8ce52 Mon Sep 17 00:00:00 2001
From: Emil Renner Berthing <emil.renner.berthing@canonical.com>
Date: Sat, 22 Jul 2023 15:59:02 +0200
Subject: [PATCH 1016/1021] usb: cdns3: starfive: Simplify mode init

The syscon regmap and offset to the USB mode register is only used at
probe time, so there is no need to store it in the device data. Just get
the regmap pointer in the cdns_mode_init() function where it is needed.
Also this function never uses the platform device, so just pass the
device pointer directly.

Signed-off-by: Emil Renner Berthing <emil.renner.berthing@canonical.com>
---
 drivers/usb/cdns3/cdns3-starfive.c | 51 ++++++++++++------------------
 1 file changed, 21 insertions(+), 30 deletions(-)

--- a/drivers/usb/cdns3/cdns3-starfive.c
+++ b/drivers/usb/cdns3/cdns3-starfive.c
@@ -34,46 +34,45 @@
 
 struct cdns_starfive {
 	struct device *dev;
-	struct regmap *stg_syscon;
 	struct reset_control *resets;
 	struct clk_bulk_data *clks;
 	int num_clks;
-	u32 stg_usb_mode;
 };
 
-static void cdns_mode_init(struct platform_device *pdev,
-			   struct cdns_starfive *data)
+static int cdns_mode_init(struct device *dev, struct cdns_starfive *data)
 {
+	struct regmap *syscon;
+	unsigned int usb_mode;
 	enum usb_dr_mode mode;
 
-	regmap_update_bits(data->stg_syscon, data->stg_usb_mode,
+	syscon = syscon_regmap_lookup_by_phandle_args(dev->of_node,
+						      "starfive,stg-syscon", 1, &usb_mode);
+	if (IS_ERR(syscon))
+		return dev_err_probe(dev, PTR_ERR(syscon),
+				     "Failed to parse starfive,stg-syscon\n");
+
+	regmap_update_bits(syscon, usb_mode,
 			   USB_MISC_CFG_MASK,
 			   USB_SUSPENDM_BYPS | USB_PLL_EN | USB_REFCLK_MODE);
 
 	/* dr mode setting */
-	mode = usb_get_dr_mode(&pdev->dev);
+	mode = usb_get_dr_mode(dev);
 
 	switch (mode) {
 	case USB_DR_MODE_HOST:
-		regmap_update_bits(data->stg_syscon,
-				   data->stg_usb_mode,
-				   USB_STRAP_MASK,
-				   USB_STRAP_HOST);
-		regmap_update_bits(data->stg_syscon,
-				   data->stg_usb_mode,
-				   USB_SUSPENDM_MASK,
-				   USB_SUSPENDM_HOST);
+		regmap_update_bits(syscon, usb_mode, USB_STRAP_MASK, USB_STRAP_HOST);
+		regmap_update_bits(syscon, usb_mode, USB_SUSPENDM_MASK, USB_SUSPENDM_HOST);
 		break;
 
 	case USB_DR_MODE_PERIPHERAL:
-		regmap_update_bits(data->stg_syscon, data->stg_usb_mode,
-				   USB_STRAP_MASK, USB_STRAP_DEVICE);
-		regmap_update_bits(data->stg_syscon, data->stg_usb_mode,
-				   USB_SUSPENDM_MASK, 0);
+		regmap_update_bits(syscon, usb_mode, USB_STRAP_MASK, USB_STRAP_DEVICE);
+		regmap_update_bits(syscon, usb_mode, USB_SUSPENDM_MASK, 0);
 		break;
 	default:
 		break;
 	}
+
+	return 0;
 }
 
 static int cdns_clk_rst_init(struct cdns_starfive *data)
@@ -108,7 +107,6 @@ static int cdns_starfive_probe(struct pl
 {
 	struct device *dev = &pdev->dev;
 	struct cdns_starfive *data;
-	unsigned int args;
 	int ret;
 
 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
@@ -117,16 +115,6 @@ static int cdns_starfive_probe(struct pl
 
 	data->dev = dev;
 
-	data->stg_syscon =
-		syscon_regmap_lookup_by_phandle_args(pdev->dev.of_node,
-						     "starfive,stg-syscon", 1, &args);
-
-	if (IS_ERR(data->stg_syscon))
-		return dev_err_probe(dev, PTR_ERR(data->stg_syscon),
-				     "Failed to parse starfive,stg-syscon\n");
-
-	data->stg_usb_mode = args;
-
 	data->num_clks = devm_clk_bulk_get_all(data->dev, &data->clks);
 	if (data->num_clks < 0)
 		return dev_err_probe(data->dev, -ENODEV,
@@ -137,7 +125,10 @@ static int cdns_starfive_probe(struct pl
 		return dev_err_probe(data->dev, PTR_ERR(data->resets),
 				     "Failed to get resets");
 
-	cdns_mode_init(pdev, data);
+	ret = cdns_mode_init(dev, data);
+	if (ret)
+		return ret;
+
 	ret = cdns_clk_rst_init(data);
 	if (ret)
 		return ret;
