Skip to content

Commit d62fcd9

Browse files
author
Bartosz Golaszewski
committed
gpiolib: provide gpio_device_find_by_label()
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 <[email protected]> Reviewed-by: Linus Walleij <[email protected]>
1 parent cfe102f commit d62fcd9

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

drivers/gpio/gpiolib.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/seq_file.h>
2121
#include <linux/slab.h>
2222
#include <linux/spinlock.h>
23+
#include <linux/string.h>
2324

2425
#include <linux/gpio.h>
2526
#include <linux/gpio/driver.h>
@@ -1081,6 +1082,26 @@ struct gpio_device *gpio_device_find(void *data,
10811082
}
10821083
EXPORT_SYMBOL_GPL(gpio_device_find);
10831084

1085+
static int gpio_chip_match_by_label(struct gpio_chip *gc, void *label)
1086+
{
1087+
return gc->label && !strcmp(gc->label, label);
1088+
}
1089+
1090+
/**
1091+
* gpio_device_find_by_label() - wrapper around gpio_device_find() finding the
1092+
* GPIO device by its backing chip's label
1093+
* @label: Label to lookup
1094+
*
1095+
* Returns:
1096+
* Reference to the GPIO device or NULL. Reference must be released with
1097+
* gpio_device_put().
1098+
*/
1099+
struct gpio_device *gpio_device_find_by_label(const char *label)
1100+
{
1101+
return gpio_device_find((void *)label, gpio_chip_match_by_label);
1102+
}
1103+
EXPORT_SYMBOL_GPL(gpio_device_find_by_label);
1104+
10841105
static int gpiochip_match_name(struct gpio_chip *gc, void *data)
10851106
{
10861107
const char *name = data;

include/linux/gpio/driver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ struct gpio_chip *gpiochip_find(void *data,
610610

611611
struct gpio_device *gpio_device_find(void *data,
612612
int (*match)(struct gpio_chip *gc, void *data));
613+
struct gpio_device *gpio_device_find_by_label(const char *label);
613614

614615
struct gpio_device *gpio_device_get(struct gpio_device *gdev);
615616
void gpio_device_put(struct gpio_device *gdev);

0 commit comments

Comments
 (0)