[PATCH 05/11] gpiolib: don't use GPIO number API in of_hog_gpio

Ahmad Fatoum a.fatoum at pengutronix.de
Fri Aug 9 07:23:59 PDT 2024


The GPIO number API is legacy that's meant to be a layer on top
of GPIO descriptors, which should be used directly for new code.

of_hog_gpio which is called after registering adding GPIO chips also
uses the number API, which is quite unnecessary: gpio_get_num iterates
over all GPIO chips to find out the number only to convert it to a
descriptor inside of gpio_request_one.

Instead of that, let's use gpiochip_get_desc, which just does some
arithmetic to get the descriptor as we know the gpiochip must be there.

This prepares us for propagating OF flags in a later commit.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 drivers/gpio/gpiolib.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 70c78a9ad4af..0c6c0fcc1aeb 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -90,6 +90,11 @@ static unsigned gpiodesc_chip_offset(const struct gpio_desc *desc)
 	return (desc - gpio_desc) - desc->chip->base + desc->chip->gpio_offset;
 }
 
+static __maybe_unused struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, int gpio)
+{
+	return gpio_desc + chip->base + gpio - chip->gpio_offset;
+}
+
 static int gpiod_is_active_low(const struct gpio_desc *desc)
 {
 	return desc->flags & OF_GPIO_ACTIVE_LOW;
@@ -643,6 +648,7 @@ static int gpiochip_find_base(int ngpio)
 	return base;
 }
 
+
 #ifdef CONFIG_OF_GPIO
 
 static int of_hog_gpio(struct device_node *np, struct gpio_chip *chip,
@@ -651,7 +657,7 @@ static int of_hog_gpio(struct device_node *np, struct gpio_chip *chip,
 	struct device_node *chip_np = chip->dev->of_node;
 	unsigned long flags = 0;
 	u32 gpio_cells, gpio_num, gpio_flags;
-	int ret, gpio;
+	int ret;
 	const char *name = NULL;
 
 	ret = of_property_read_u32(chip_np, "#gpio-cells", &gpio_cells);
@@ -678,16 +684,6 @@ static int of_hog_gpio(struct device_node *np, struct gpio_chip *chip,
 	if (gpio_flags & OF_GPIO_ACTIVE_LOW)
 		flags |= GPIOF_ACTIVE_LOW;
 
-	gpio = gpio_get_num(chip->dev, gpio_num);
-	if (gpio == -EPROBE_DEFER)
-		return gpio;
-
-	if (gpio < 0) {
-		dev_err(chip->dev, "unable to get gpio %u\n", gpio_num);
-		return gpio;
-	}
-
-
 	/*
 	 * Note that, in order to be compatible with Linux, the code
 	 * below interprets 'output-high' as to mean 'output-active'.
@@ -714,7 +710,8 @@ static int of_hog_gpio(struct device_node *np, struct gpio_chip *chip,
 	if (ret < 0)
 		name = np->name;
 
-	return gpio_request_one(gpio, flags, name);
+	return gpiodesc_request_one(gpiochip_get_desc(chip, gpio_num),
+				    flags, name);
 }
 
 static int of_gpiochip_scan_hogs(struct gpio_chip *chip)
-- 
2.39.2




More information about the barebox mailing list