[PATCH v5 04/12] gpio: aggregator: move GPIO forwarder allocation in a dedicated function

Thomas Richard posted 12 patches 7 months, 2 weeks ago
There is a newer version of this series
[PATCH v5 04/12] gpio: aggregator: move GPIO forwarder allocation in a dedicated function
Posted by Thomas Richard 7 months, 2 weeks ago
Move the GPIO forwarder allocation and static initialization in a dedicated
function.

Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Thomas Richard <thomas.richard@bootlin.com>
---
 drivers/gpio/gpio-aggregator.c | 47 ++++++++++++++++++++++++++++--------------
 1 file changed, 31 insertions(+), 16 deletions(-)

diff --git a/drivers/gpio/gpio-aggregator.c b/drivers/gpio/gpio-aggregator.c
index d232ea865356..4268ef94914d 100644
--- a/drivers/gpio/gpio-aggregator.c
+++ b/drivers/gpio/gpio-aggregator.c
@@ -498,6 +498,36 @@ static int gpiochip_fwd_setup_delay_line(struct device *dev, struct gpio_chip *c
 }
 #endif	/* !CONFIG_OF_GPIO */
 
+static struct gpiochip_fwd *
+devm_gpiochip_fwd_alloc(struct device *dev, unsigned int ngpios)
+{
+	const char *label = dev_name(dev);
+	struct gpiochip_fwd *fwd;
+	struct gpio_chip *chip;
+
+	fwd = devm_kzalloc(dev, struct_size(fwd, tmp, fwd_tmp_size(ngpios)), GFP_KERNEL);
+	if (!fwd)
+		return ERR_PTR(-ENOMEM);
+
+	chip = &fwd->chip;
+
+	chip->label = label;
+	chip->parent = dev;
+	chip->owner = THIS_MODULE;
+	chip->get_direction = gpio_fwd_get_direction;
+	chip->direction_input = gpio_fwd_direction_input;
+	chip->direction_output = gpio_fwd_direction_output;
+	chip->get = gpio_fwd_get;
+	chip->get_multiple = gpio_fwd_get_multiple_locked;
+	chip->set_rv = gpio_fwd_set;
+	chip->set_multiple_rv = gpio_fwd_set_multiple_locked;
+	chip->to_irq = gpio_fwd_to_irq;
+	chip->base = -1;
+	chip->ngpio = ngpios;
+
+	return fwd;
+}
+
 /**
  * gpiochip_fwd_create() - Create a new GPIO forwarder
  * @dev: Parent device pointer
@@ -518,14 +548,12 @@ static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
 						struct gpio_desc *descs[],
 						unsigned long features)
 {
-	const char *label = dev_name(dev);
 	struct gpiochip_fwd *fwd;
 	struct gpio_chip *chip;
 	unsigned int i;
 	int error;
 
-	fwd = devm_kzalloc(dev, struct_size(fwd, tmp, fwd_tmp_size(ngpios)),
-			   GFP_KERNEL);
+	fwd = devm_gpiochip_fwd_alloc(dev, ngpios);
 	if (!fwd)
 		return ERR_PTR(-ENOMEM);
 
@@ -549,19 +577,6 @@ static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
 			chip->set_config = gpio_fwd_set_config;
 	}
 
-	chip->label = label;
-	chip->parent = dev;
-	chip->owner = THIS_MODULE;
-	chip->get_direction = gpio_fwd_get_direction;
-	chip->direction_input = gpio_fwd_direction_input;
-	chip->direction_output = gpio_fwd_direction_output;
-	chip->get = gpio_fwd_get;
-	chip->get_multiple = gpio_fwd_get_multiple_locked;
-	chip->set_rv = gpio_fwd_set;
-	chip->set_multiple_rv = gpio_fwd_set_multiple_locked;
-	chip->to_irq = gpio_fwd_to_irq;
-	chip->base = -1;
-	chip->ngpio = ngpios;
 	fwd->descs = descs;
 
 	if (chip->can_sleep)

-- 
2.39.5
Re: [PATCH v5 04/12] gpio: aggregator: move GPIO forwarder allocation in a dedicated function
Posted by Geert Uytterhoeven 7 months, 1 week ago
Hi Thomas,

On Tue, 6 May 2025 at 17:21, Thomas Richard <thomas.richard@bootlin.com> wrote:
> Move the GPIO forwarder allocation and static initialization in a dedicated
> function.
>
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Signed-off-by: Thomas Richard <thomas.richard@bootlin.com>

Thanks for your patch!

> --- a/drivers/gpio/gpio-aggregator.c
> +++ b/drivers/gpio/gpio-aggregator.c
> @@ -498,6 +498,36 @@ static int gpiochip_fwd_setup_delay_line(struct device *dev, struct gpio_chip *c
>  }
>  #endif /* !CONFIG_OF_GPIO */
>
> +static struct gpiochip_fwd *
> +devm_gpiochip_fwd_alloc(struct device *dev, unsigned int ngpios)
> +{
> +       const char *label = dev_name(dev);
> +       struct gpiochip_fwd *fwd;
> +       struct gpio_chip *chip;
> +
> +       fwd = devm_kzalloc(dev, struct_size(fwd, tmp, fwd_tmp_size(ngpios)), GFP_KERNEL);
> +       if (!fwd)
> +               return ERR_PTR(-ENOMEM);

The caller expects NULL for failures.
So either this return value has to be changed, or the caller needs
to be updated, too.

> +
> +       chip = &fwd->chip;
> +
> +       chip->label = label;

Since you will have to respin this patch anyway, you may want to
drop the label variable, and use dev_name(dev) directly.

> +       chip->parent = dev;
> +       chip->owner = THIS_MODULE;
> +       chip->get_direction = gpio_fwd_get_direction;
> +       chip->direction_input = gpio_fwd_direction_input;
> +       chip->direction_output = gpio_fwd_direction_output;
> +       chip->get = gpio_fwd_get;
> +       chip->get_multiple = gpio_fwd_get_multiple_locked;
> +       chip->set_rv = gpio_fwd_set;
> +       chip->set_multiple_rv = gpio_fwd_set_multiple_locked;
> +       chip->to_irq = gpio_fwd_to_irq;
> +       chip->base = -1;
> +       chip->ngpio = ngpios;
> +
> +       return fwd;
> +}
> +
>  /**
>   * gpiochip_fwd_create() - Create a new GPIO forwarder
>   * @dev: Parent device pointer
> @@ -518,14 +548,12 @@ static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
>                                                 struct gpio_desc *descs[],
>                                                 unsigned long features)
>  {
> -       const char *label = dev_name(dev);
>         struct gpiochip_fwd *fwd;
>         struct gpio_chip *chip;
>         unsigned int i;
>         int error;
>
> -       fwd = devm_kzalloc(dev, struct_size(fwd, tmp, fwd_tmp_size(ngpios)),
> -                          GFP_KERNEL);
> +       fwd = devm_gpiochip_fwd_alloc(dev, ngpios);
>         if (!fwd)
>                 return ERR_PTR(-ENOMEM);
>

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds