diff mbox series

[v1,1/3] gpiolib: Split out for_each_gpio_desc() macro

Message ID 20220330145910.1946-1-andriy.shevchenko@linux.intel.com
State New
Headers show
Series [v1,1/3] gpiolib: Split out for_each_gpio_desc() macro | expand

Commit Message

Andy Shevchenko March 30, 2022, 2:59 p.m. UTC
In some cases we want to traverse all GPIO descriptors for given
chip, let's split out for_each_gpio_desc() macro for such cases.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/gpio/gpiolib.c | 10 +++-------
 drivers/gpio/gpiolib.h |  7 +++++--
 2 files changed, 8 insertions(+), 9 deletions(-)

Comments

Bartosz Golaszewski April 5, 2022, 12:21 p.m. UTC | #1
On Wed, Mar 30, 2022 at 4:59 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> In some cases we want to traverse all GPIO descriptors for given
> chip, let's split out for_each_gpio_desc() macro for such cases.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>  drivers/gpio/gpiolib.c | 10 +++-------
>  drivers/gpio/gpiolib.h |  7 +++++--
>  2 files changed, 8 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> index e59884cc12a7..723411c13f1c 100644
> --- a/drivers/gpio/gpiolib.c
> +++ b/drivers/gpio/gpiolib.c
> @@ -310,15 +310,11 @@ static struct gpio_desc *gpio_name_to_desc(const char * const name)
>         spin_lock_irqsave(&gpio_lock, flags);
>
>         list_for_each_entry(gdev, &gpio_devices, list) {
> +               struct gpio_desc *desc;
>                 int i;
>
> -               for (i = 0; i != gdev->ngpio; ++i) {
> -                       struct gpio_desc *desc = &gdev->descs[i];
> -
> -                       if (!desc->name)
> -                               continue;
> -
> -                       if (!strcmp(desc->name, name)) {
> +               for_each_gpio_desc(i, gdev->chip, desc) {
> +                       if (desc->name && !strcmp(desc->name, name)) {
>                                 spin_unlock_irqrestore(&gpio_lock, flags);
>                                 return desc;
>                         }
> diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h
> index 06f3faa9fbef..450fb2fabe43 100644
> --- a/drivers/gpio/gpiolib.h
> +++ b/drivers/gpio/gpiolib.h
> @@ -100,10 +100,13 @@ struct gpio_array {
>
>  struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum);
>
> -#define for_each_gpio_desc_with_flag(i, gc, desc, flag)                \
> +#define for_each_gpio_desc(i, gc, desc)                                \

While at it: how about declaring the i variable in the loop definition?

Bart

>         for (i = 0, desc = gpiochip_get_desc(gc, i);            \
>              i < gc->ngpio;                                     \
> -            i++, desc = gpiochip_get_desc(gc, i))              \
> +            i++, desc = gpiochip_get_desc(gc, i))
> +
> +#define for_each_gpio_desc_with_flag(i, gc, desc, flag)                \
> +       for_each_gpio_desc(i, gc, desc)                         \
>                 if (!test_bit(flag, &desc->flags)) {} else
>
>  int gpiod_get_array_value_complex(bool raw, bool can_sleep,
> --
> 2.35.1
>
Andy Shevchenko April 5, 2022, 2:55 p.m. UTC | #2
On Tue, Apr 05, 2022 at 02:21:42PM +0200, Bartosz Golaszewski wrote:
> On Wed, Mar 30, 2022 at 4:59 PM Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:

...

> > -#define for_each_gpio_desc_with_flag(i, gc, desc, flag)                \
> > +#define for_each_gpio_desc(i, gc, desc)                                \
> 
> While at it: how about declaring the i variable in the loop definition?

It will require changes in the users, so, I will do it as a prerequisite
separate change.

> >         for (i = 0, desc = gpiochip_get_desc(gc, i);            \
> >              i < gc->ngpio;                                     \
> > -            i++, desc = gpiochip_get_desc(gc, i))              \
> > +            i++, desc = gpiochip_get_desc(gc, i))
diff mbox series

Patch

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index e59884cc12a7..723411c13f1c 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -310,15 +310,11 @@  static struct gpio_desc *gpio_name_to_desc(const char * const name)
 	spin_lock_irqsave(&gpio_lock, flags);
 
 	list_for_each_entry(gdev, &gpio_devices, list) {
+		struct gpio_desc *desc;
 		int i;
 
-		for (i = 0; i != gdev->ngpio; ++i) {
-			struct gpio_desc *desc = &gdev->descs[i];
-
-			if (!desc->name)
-				continue;
-
-			if (!strcmp(desc->name, name)) {
+		for_each_gpio_desc(i, gdev->chip, desc) {
+			if (desc->name && !strcmp(desc->name, name)) {
 				spin_unlock_irqrestore(&gpio_lock, flags);
 				return desc;
 			}
diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h
index 06f3faa9fbef..450fb2fabe43 100644
--- a/drivers/gpio/gpiolib.h
+++ b/drivers/gpio/gpiolib.h
@@ -100,10 +100,13 @@  struct gpio_array {
 
 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum);
 
-#define for_each_gpio_desc_with_flag(i, gc, desc, flag)		\
+#define for_each_gpio_desc(i, gc, desc)				\
 	for (i = 0, desc = gpiochip_get_desc(gc, i);		\
 	     i < gc->ngpio;					\
-	     i++, desc = gpiochip_get_desc(gc, i))		\
+	     i++, desc = gpiochip_get_desc(gc, i))
+
+#define for_each_gpio_desc_with_flag(i, gc, desc, flag)		\
+	for_each_gpio_desc(i, gc, desc)				\
 		if (!test_bit(flag, &desc->flags)) {} else
 
 int gpiod_get_array_value_complex(bool raw, bool can_sleep,