diff mbox series

[v4,04/11] gpiolib: provide gpio_device_find_by_label()

Message ID 20230927142931.19798-5-brgl@bgdev.pl
State New
Headers show
Series gpiolib: work towards removing gpiochip_find() | expand

Commit Message

Bartosz Golaszewski Sept. 27, 2023, 2:29 p.m. UTC
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

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>
---
 drivers/gpio/gpiolib.c      | 21 +++++++++++++++++++++
 include/linux/gpio/driver.h |  1 +
 2 files changed, 22 insertions(+)

Comments

Andy Shevchenko Oct. 2, 2023, 9:44 a.m. UTC | #1
On Wed, Sep 27, 2023 at 04:29:24PM +0200, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> 
> 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.

...

> +struct gpio_device *gpio_device_find_by_label(const char *label)
> +{
> +	return gpio_device_find((void *)label, gpio_chip_match_by_label);
> +}

Are we expecting that data referenced by the first parameter to the
gpio_device_find() can be altered? If not, why not using const void *
there and here as well?
Bartosz Golaszewski Oct. 2, 2023, 9:53 a.m. UTC | #2
On Mon, Oct 2, 2023 at 11:44 AM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> On Wed, Sep 27, 2023 at 04:29:24PM +0200, Bartosz Golaszewski wrote:
> > From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> >
> > 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.
>
> ...
>
> > +struct gpio_device *gpio_device_find_by_label(const char *label)
> > +{
> > +     return gpio_device_find((void *)label, gpio_chip_match_by_label);
> > +}
>
> Are we expecting that data referenced by the first parameter to the
> gpio_device_find() can be altered? If not, why not using const void *
> there and here as well?
>

I guess it's a good idea.

Bart
Bartosz Golaszewski Oct. 3, 2023, 12:44 p.m. UTC | #3
On Mon, Oct 2, 2023 at 11:53 AM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>
> On Mon, Oct 2, 2023 at 11:44 AM Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:
> >
> > On Wed, Sep 27, 2023 at 04:29:24PM +0200, Bartosz Golaszewski wrote:
> > > From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> > >
> > > 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.
> >
> > ...
> >
> > > +struct gpio_device *gpio_device_find_by_label(const char *label)
> > > +{
> > > +     return gpio_device_find((void *)label, gpio_chip_match_by_label);
> > > +}
> >
> > Are we expecting that data referenced by the first parameter to the
> > gpio_device_find() can be altered? If not, why not using const void *
> > there and here as well?
> >
>
> I guess it's a good idea.
>
> Bart

Scratch that. When we know that we're passing a label, it's fine to
have it const but almost treewide, various matching functions take
"void *data" as argument. I'll leave it this way.

Bart
diff mbox series

Patch

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 0371d23f0a46..9f20311e4c1a 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -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>
@@ -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;
diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h
index 6ad1f1a8ef2e..24996cba6465 100644
--- a/include/linux/gpio/driver.h
+++ b/include/linux/gpio/driver.h
@@ -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);