Skip to content

Commit 80c78fb

Browse files
andy-shevbrgl
authored andcommitted
gpiolib: Introduce for_each_gpio_desc_with_flag() macro
In a few places we are using a loop against all GPIO descriptors with a given flag for a given device. Replace it with a consolidated for_each type of macro. Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Reviewed-by: Linus Walleij <linus.walleij@linaro.org> Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
1 parent 0868ad3 commit 80c78fb

4 files changed

Lines changed: 16 additions & 15 deletions

File tree

drivers/gpio/gpiolib-of.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -711,14 +711,12 @@ static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
711711
static void of_gpiochip_remove_hog(struct gpio_chip *chip,
712712
struct device_node *hog)
713713
{
714-
struct gpio_desc *descs = chip->gpiodev->descs;
714+
struct gpio_desc *desc;
715715
unsigned int i;
716716

717-
for (i = 0; i < chip->ngpio; i++) {
718-
if (test_bit(FLAG_IS_HOGGED, &descs[i].flags) &&
719-
descs[i].hog == hog)
720-
gpiochip_free_own_desc(&descs[i]);
721-
}
717+
for_each_gpio_desc_with_flag(i, chip, desc, FLAG_IS_HOGGED)
718+
if (desc->hog == hog)
719+
gpiochip_free_own_desc(desc);
722720
}
723721

724722
static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)

drivers/gpio/gpiolib-sysfs.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -793,11 +793,8 @@ void gpiochip_sysfs_unregister(struct gpio_device *gdev)
793793
mutex_unlock(&sysfs_lock);
794794

795795
/* unregister gpiod class devices owned by sysfs */
796-
for (i = 0; i < chip->ngpio; i++) {
797-
desc = &gdev->descs[i];
798-
if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
799-
gpiod_free(desc);
800-
}
796+
for_each_gpio_desc_with_flag(i, chip, desc, FLAG_SYSFS)
797+
gpiod_free(desc);
801798
}
802799

803800
static int __init gpiolib_sysfs_init(void)

drivers/gpio/gpiolib.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4102,12 +4102,11 @@ int gpiod_hog(struct gpio_desc *desc, const char *name,
41024102
*/
41034103
static void gpiochip_free_hogs(struct gpio_chip *gc)
41044104
{
4105+
struct gpio_desc *desc;
41054106
int id;
41064107

4107-
for (id = 0; id < gc->ngpio; id++) {
4108-
if (test_bit(FLAG_IS_HOGGED, &gc->gpiodev->descs[id].flags))
4109-
gpiochip_free_own_desc(&gc->gpiodev->descs[id]);
4110-
}
4108+
for_each_gpio_desc_with_flag(id, gc, desc, FLAG_IS_HOGGED)
4109+
gpiochip_free_own_desc(desc);
41114110
}
41124111

41134112
/**

drivers/gpio/gpiolib.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ struct gpio_array {
8282
};
8383

8484
struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum);
85+
86+
#define for_each_gpio_desc_with_flag(i, gc, desc, flag) \
87+
for (i = 0, desc = gpiochip_get_desc(gc, i); \
88+
i < gc->ngpio; \
89+
i++, desc = gpiochip_get_desc(gc, i)) \
90+
if (!test_bit(flag, &desc->flags)) {} else
91+
8592
int gpiod_get_array_value_complex(bool raw, bool can_sleep,
8693
unsigned int array_size,
8794
struct gpio_desc **desc_array,

0 commit comments

Comments
 (0)