[v2,1/2] gpiolib: Provide to_gpio_device() helper

Message ID 20221128190055.6350-1-andriy.shevchenko@linux.intel.com
State New
Headers
Series [v2,1/2] gpiolib: Provide to_gpio_device() helper |

Commit Message

Andy Shevchenko Nov. 28, 2022, 7 p.m. UTC
  Provide to_gpio_device() helper which can be utilized in the existing
and future code.

While at it, make sure it becomes no-op at compilation time.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
v2: made helper static inline (Bart)
 drivers/gpio/gpiolib.c | 2 +-
 drivers/gpio/gpiolib.h | 9 +++++++--
 2 files changed, 8 insertions(+), 3 deletions(-)
  

Comments

Linus Walleij Nov. 28, 2022, 8:40 p.m. UTC | #1
On Mon, Nov 28, 2022 at 8:00 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:

> Provide to_gpio_device() helper which can be utilized in the existing
> and future code.
>
> While at it, make sure it becomes no-op at compilation time.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Looks helpful.
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij
  
Bartosz Golaszewski Nov. 29, 2022, 12:50 p.m. UTC | #2
On Mon, Nov 28, 2022 at 8:00 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> Provide to_gpio_device() helper which can be utilized in the existing
> and future code.
>
> While at it, make sure it becomes no-op at compilation time.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
> v2: made helper static inline (Bart)
>  drivers/gpio/gpiolib.c | 2 +-
>  drivers/gpio/gpiolib.h | 9 +++++++--
>  2 files changed, 8 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> index a11480634213..ccd47da4ec66 100644
> --- a/drivers/gpio/gpiolib.c
> +++ b/drivers/gpio/gpiolib.c
> @@ -548,7 +548,7 @@ EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
>
>  static void gpiodevice_release(struct device *dev)
>  {
> -       struct gpio_device *gdev = container_of(dev, struct gpio_device, dev);
> +       struct gpio_device *gdev = to_gpio_device(dev);
>         unsigned long flags;
>
>         spin_lock_irqsave(&gpio_lock, flags);
> diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h
> index d900ecdbac46..e443c1023a37 100644
> --- a/drivers/gpio/gpiolib.h
> +++ b/drivers/gpio/gpiolib.h
> @@ -20,9 +20,9 @@
>
>  /**
>   * struct gpio_device - internal state container for GPIO devices
> - * @id: numerical ID number for the GPIO chip
>   * @dev: the GPIO device struct
>   * @chrdev: character device for the GPIO device
> + * @id: numerical ID number for the GPIO chip
>   * @mockdev: class device used by the deprecated sysfs interface (may be
>   * NULL)
>   * @owner: helps prevent removal of modules exporting active GPIOs
> @@ -47,9 +47,9 @@
>   * userspace.
>   */
>  struct gpio_device {
> -       int                     id;

Sorry I didn't notice it before but why the churn? This is not needed
for container_of to work.

Bart

>         struct device           dev;
>         struct cdev             chrdev;
> +       int                     id;
>         struct device           *mockdev;
>         struct module           *owner;
>         struct gpio_chip        *chip;
> @@ -72,6 +72,11 @@ struct gpio_device {
>  #endif
>  };
>
> +static inline struct gpio_device *to_gpio_device(struct device *dev)
> +{
> +       return container_of(dev, struct gpio_device, dev);
> +}
> +
>  /* gpio suffixes used for ACPI and device tree lookup */
>  static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" };
>
> --
> 2.35.1
>
  
Andy Shevchenko Nov. 29, 2022, 1:46 p.m. UTC | #3
On Tue, Nov 29, 2022 at 01:50:51PM +0100, Bartosz Golaszewski wrote:
> On Mon, Nov 28, 2022 at 8:00 PM Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:

...

> > While at it, make sure it becomes no-op at compilation time.

^^^

> > -       int                     id;
> 
> Sorry I didn't notice it before but why the churn? This is not needed
> for container_of to work.

> > +       int                     id;

I hope this explains and can be accepted.

> >  };
  
Bartosz Golaszewski Nov. 29, 2022, 1:52 p.m. UTC | #4
On Tue, Nov 29, 2022 at 2:46 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> On Tue, Nov 29, 2022 at 01:50:51PM +0100, Bartosz Golaszewski wrote:
> > On Mon, Nov 28, 2022 at 8:00 PM Andy Shevchenko
> > <andriy.shevchenko@linux.intel.com> wrote:
>
> ...
>
> > > While at it, make sure it becomes no-op at compilation time.
>
> ^^^
>
> > > -       int                     id;
> >
> > Sorry I didn't notice it before but why the churn? This is not needed
> > for container_of to work.
>
> > > +       int                     id;
>
> I hope this explains and can be accepted.
>

Ah, alright makes sense. Applying.

Bart
  

Patch

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index a11480634213..ccd47da4ec66 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -548,7 +548,7 @@  EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
 
 static void gpiodevice_release(struct device *dev)
 {
-	struct gpio_device *gdev = container_of(dev, struct gpio_device, dev);
+	struct gpio_device *gdev = to_gpio_device(dev);
 	unsigned long flags;
 
 	spin_lock_irqsave(&gpio_lock, flags);
diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h
index d900ecdbac46..e443c1023a37 100644
--- a/drivers/gpio/gpiolib.h
+++ b/drivers/gpio/gpiolib.h
@@ -20,9 +20,9 @@ 
 
 /**
  * struct gpio_device - internal state container for GPIO devices
- * @id: numerical ID number for the GPIO chip
  * @dev: the GPIO device struct
  * @chrdev: character device for the GPIO device
+ * @id: numerical ID number for the GPIO chip
  * @mockdev: class device used by the deprecated sysfs interface (may be
  * NULL)
  * @owner: helps prevent removal of modules exporting active GPIOs
@@ -47,9 +47,9 @@ 
  * userspace.
  */
 struct gpio_device {
-	int			id;
 	struct device		dev;
 	struct cdev		chrdev;
+	int			id;
 	struct device		*mockdev;
 	struct module		*owner;
 	struct gpio_chip	*chip;
@@ -72,6 +72,11 @@  struct gpio_device {
 #endif
 };
 
+static inline struct gpio_device *to_gpio_device(struct device *dev)
+{
+	return container_of(dev, struct gpio_device, dev);
+}
+
 /* gpio suffixes used for ACPI and device tree lookup */
 static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" };