Skip to content

Commit

Permalink
gpiolib: provide gpio_device_find_by_label()
Browse files Browse the repository at this point in the history
By far the most common way of looking up GPIO devices is using their
label. Provide a helpers for that to avoid every user implementing their
own matching function.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
  • Loading branch information
Bartosz Golaszewski committed Oct 4, 2023
1 parent cfe102f commit d62fcd9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions drivers/gpio/gpiolib.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <linux/seq_file.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/string.h>

#include <linux/gpio.h>
#include <linux/gpio/driver.h>
Expand Down Expand Up @@ -1081,6 +1082,26 @@ struct gpio_device *gpio_device_find(void *data,
}
EXPORT_SYMBOL_GPL(gpio_device_find);

static int gpio_chip_match_by_label(struct gpio_chip *gc, void *label)
{
return gc->label && !strcmp(gc->label, label);
}

/**
* gpio_device_find_by_label() - wrapper around gpio_device_find() finding the
* GPIO device by its backing chip's label
* @label: Label to lookup
*
* Returns:
* Reference to the GPIO device or NULL. Reference must be released with
* gpio_device_put().
*/
struct gpio_device *gpio_device_find_by_label(const char *label)
{
return gpio_device_find((void *)label, gpio_chip_match_by_label);
}
EXPORT_SYMBOL_GPL(gpio_device_find_by_label);

static int gpiochip_match_name(struct gpio_chip *gc, void *data)
{
const char *name = data;
Expand Down
1 change: 1 addition & 0 deletions include/linux/gpio/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ struct gpio_chip *gpiochip_find(void *data,

struct gpio_device *gpio_device_find(void *data,
int (*match)(struct gpio_chip *gc, void *data));
struct gpio_device *gpio_device_find_by_label(const char *label);

struct gpio_device *gpio_device_get(struct gpio_device *gdev);
void gpio_device_put(struct gpio_device *gdev);
Expand Down

0 comments on commit d62fcd9

Please sign in to comment.