Skip to contentSkip to conversation

gpiolib: improve coding style for local variables

PATCH Superseded Bartosz Golaszewski 3 participants 8 messages Nov 11, 2021 – Nov 12, 2021 GPIO

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 gpiolib: improve coding style for local variables

Drop unneeded whitespaces and put the variables of the same type together for consistency with the rest of the code.

---
 drivers/gpio/gpiolib.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib.c +3 -4
index d1b9b721218f..4c34c96ef136 100644
@@ -594,11 +594,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 			       struct lock_class_key *request_key)
 {
 	struct fwnode_handle *fwnode = gc->parent ? dev_fwnode(gc->parent) : NULL;
-	unsigned long	flags;
-	int		ret = 0;
-	unsigned	i;
-	int		base = gc->base;
+	int ret = 0, base = gc->base;
 	struct gpio_device *gdev;
+	unsigned long flags;
+	unsigned int i;
 
 	/*
 	 * First: allocate and populate the internal stat container, and
Signature · 1 line
2.30.1
#1 lore raw

RFC PATCH 2/2 gpiolib: check the 'ngpios' property in core gpiolib code

Several drivers read the 'ngpios' device property on their own, but since it's defined as a standard GPIO property in the device tree bindings anyway, it's a good candidate for generalization. If the driver didn't set its gc->ngpio, try to read the 'ngpios' property from the GPIO device's firmware node before bailing out.

---
 drivers/gpio/gpiolib.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/gpio/gpiolib.c +10 -3
index 4c34c96ef136..23b0478163d4 100644
@@ -598,6 +598,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 	struct gpio_device *gdev;
 	unsigned long flags;
 	unsigned int i;
+	u32 ngpios;
 
 	/*
 	 * First: allocate and populate the internal stat container, and
@@ -646,9 +647,15 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 	}
 
 	if (gc->ngpio == 0) {
-		chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
-		ret = -EINVAL;
-		goto err_free_descs;
+		ret = fwnode_property_read_u32(gdev->dev.fwnode, "ngpios",
+					       &ngpios);
+		if (ret == 0) {
+			gc->ngpio = ngpios;
+		} else {
+			chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
+			ret = -EINVAL;
+			goto err_free_descs;
+		}
 	}
 
 	if (gc->ngpio > FASTPATH_NGPIO)
Signature · 1 line
2.30.1
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH RFC 2/2] gpiolib: check the 'ngpios' property in core gpiolib code

Quoted 8 lines · Bartosz Golaszewski

Several drivers read the 'ngpios' device property on their own, but since it's defined as a standard GPIO property in the device tree bindings anyway, it's a good candidate for generalization. If the driver didn't set its gc->ngpio, try to read the 'ngpios' property from the GPIO device's firmware node before bailing out.

Nice!

Yours,
Linus Walleij

#3 lore raw

Re: [PATCH RFC 2/2] gpiolib: check the 'ngpios' property in core gpiolib code

In reply to Bartosz Golaszewski
Quoted 6 lines · Bartosz Golaszewski

Several drivers read the 'ngpios' device property on their own, but since it's defined as a standard GPIO property in the device tree bindings anyway, it's a good candidate for generalization. If the driver didn't set its gc->ngpio, try to read the 'ngpios' property from the GPIO device's firmware node before bailing out.

Thanks!

...

drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, · line 651

Quoted 2 lines
drivers/gpio/gpiolib.c +2 -0
+               ret = fwnode_property_read_u32(gdev->dev.fwnode, "ngpios",
+                                              &ngpios);

I'm wondering if there is any obstacle to call

               ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);

?

Rationale (the main one) is to avoid direct dereference of fwnode from struct device (it might be changed in the future). I really prefer API calls here.

drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, · line 658

Quoted 7 lines
drivers/gpio/gpiolib.c +7 -0
+               if (ret == 0) {
+                       gc->ngpio = ngpios;
+               } else {
+                       chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
+                       ret = -EINVAL;
+                       goto err_free_descs;
+               }

I would prefer the other way around and without 'else' being involved.

Quoted 1 line
        }
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Re: [PATCH RFC 2/2] gpiolib: check the 'ngpios' property in core gpiolib code

Quoted 34 lines · Andy Shevchenko
Quoted 6 lines · Bartosz Golaszewski

Several drivers read the 'ngpios' device property on their own, but since it's defined as a standard GPIO property in the device tree bindings anyway, it's a good candidate for generalization. If the driver didn't set its gc->ngpio, try to read the 'ngpios' property from the GPIO device's firmware node before bailing out.

Thanks!

...

drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, · line 651

Quoted 2 lines
drivers/gpio/gpiolib.c +2 -0
+               ret = fwnode_property_read_u32(gdev->dev.fwnode, "ngpios",
+                                              &ngpios);

I'm wondering if there is any obstacle to call

               ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);

?

Rationale (the main one) is to avoid direct dereference of fwnode from struct device (it might be changed in the future). I really prefer API calls here.

drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, · line 658

Quoted 7 lines
drivers/gpio/gpiolib.c +7 -0
+               if (ret == 0) {
+                       gc->ngpio = ngpios;
+               } else {
+                       chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
+                       ret = -EINVAL;
+                       goto err_free_descs;
+               }
Quoted 8 lines

I would prefer the other way around and without 'else' being involved.

Quoted 1 line
        }

--
With Best Regards,
Andy Shevchenko

Will do. Just a note: some drivers that parse the ngpios property will need to continue doing it themselves as they have additional requirements from the value (limited max value, needs to be multiple of 8, etc.) on the read value. For those that are obvious, we can start converting them after this patch lands.

Bart

#5 lore raw

Re: [PATCH RFC 2/2] gpiolib: check the 'ngpios' property in core gpiolib code

Quoted 2 lines · Bartosz Golaszewski

On Thu, Nov 11, 2021 at 9:47 PM Andy Shevchenko
<[email protected]> wrote:

...

Quoted 5 lines

Will do. Just a note: some drivers that parse the ngpios property will need to continue doing it themselves as they have additional requirements from the value (limited max value, needs to be multiple of 8, etc.) on the read value. For those that are obvious, we can start converting them after this patch lands.

Sure! No objections from my side.

Signature · 2 lines
With Best Regards,
Andy Shevchenko