[PATCH v2 06/10] gpio: rockchip: Get pinctrl device from gpio-ranges prop

Jonas Karlman jonas at kwiboo.se
Sat Aug 3 00:56:48 CEST 2024


Get pinctrl device from gpio-ranges phandle when the property exists,
fallback to get the first pinctrl device.

Signed-off-by: Jonas Karlman <jonas at kwiboo.se>
Reviewed-by: Kever Yang <kever.yang at rock-chips.com>
---
v2: Collect r-b tag
---
 drivers/gpio/rk_gpio.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/drivers/gpio/rk_gpio.c b/drivers/gpio/rk_gpio.c
index 24ba12dd820e..abece6409ae0 100644
--- a/drivers/gpio/rk_gpio.c
+++ b/drivers/gpio/rk_gpio.c
@@ -181,12 +181,6 @@ static int rockchip_gpio_probe(struct udevice *dev)
 
 	priv->regs = dev_read_addr_ptr(dev);
 
-	if (CONFIG_IS_ENABLED(PINCTRL)) {
-		ret = uclass_first_device_err(UCLASS_PINCTRL, &priv->pinctrl);
-		if (ret)
-			return ret;
-	}
-
 	/*
 	 * If "gpio-ranges" is present in the devicetree use it to parse
 	 * the GPIO bank ID, otherwise use the legacy method.
@@ -194,16 +188,33 @@ static int rockchip_gpio_probe(struct udevice *dev)
 	ret = ofnode_parse_phandle_with_args(dev_ofnode(dev),
 					     "gpio-ranges", NULL, 3,
 					     0, &args);
-	if (!ret || ret != -ENOENT) {
+	if (!ret) {
 		uc_priv->gpio_count = args.args[2];
 		priv->bank = args.args[1] / ROCKCHIP_GPIOS_PER_BANK;
-	} else {
+
+		if (CONFIG_IS_ENABLED(PINCTRL)) {
+			ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
+							  args.node,
+							  &priv->pinctrl);
+			if (ret)
+				return ret;
+		}
+	} else if (ret == -ENOENT || !CONFIG_IS_ENABLED(PINCTRL)) {
 		uc_priv->gpio_count = ROCKCHIP_GPIOS_PER_BANK;
 		ret = dev_read_alias_seq(dev, &priv->bank);
 		if (ret) {
 			end = strrchr(dev->name, '@');
 			priv->bank = trailing_strtoln(dev->name, end);
 		}
+
+		if (CONFIG_IS_ENABLED(PINCTRL)) {
+			ret = uclass_first_device_err(UCLASS_PINCTRL,
+						      &priv->pinctrl);
+			if (ret)
+				return ret;
+		}
+	} else {
+		return ret;
 	}
 
 	priv->name[0] = 'A' + priv->bank;
-- 
2.45.2



More information about the U-Boot mailing list