diff mbox series

[6/6] nvmem: increase the reference count of a gpio passed over config

Message ID 20200217195435.9309-7-brgl@bgdev.pl
State New
Headers show
Series nvmem/gpio: fix resource management | expand

Commit Message

Bartosz Golaszewski Feb. 17, 2020, 7:54 p.m. UTC
From: Bartosz Golaszewski <bgolaszewski@baylibre.com>

We can obtain the write-protect GPIO in nvmem_register() by requesting
it ourselves or by storing the gpio_desc passed in nvmem_config. In the
latter case we need to increase the reference count so that it gets
freed correctly.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
 drivers/nvmem/core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Geert Uytterhoeven Feb. 17, 2020, 9:13 p.m. UTC | #1
Hi Bartosz,

On Mon, Feb 17, 2020 at 8:56 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
> From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
>
> We can obtain the write-protect GPIO in nvmem_register() by requesting
> it ourselves or by storing the gpio_desc passed in nvmem_config. In the
> latter case we need to increase the reference count so that it gets
> freed correctly.
>
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>

Thanks for your patch!

> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
> @@ -349,11 +349,13 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
>                 return ERR_PTR(rval);
>         }
>
> -       if (config->wp_gpio)
> +       if (config->wp_gpio) {
>                 nvmem->wp_gpio = config->wp_gpio;
> -       else
> +               gpiod_ref(config->wp_gpio);

If gpiod_ref() would return the passed GPIO, or an error code, you could write

        nvmem->wp_gpio = gpiod_ref(config->wp_gpio);

> +       } else {
>                 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
>                                                     GPIOD_OUT_HIGH);
> +       }
>         if (IS_ERR(nvmem->wp_gpio))
>                 goto err_ida_remove;
>

Gr{oetje,eeting}s,

                        Geert
Bartosz Golaszewski Feb. 17, 2020, 10:09 p.m. UTC | #2
pon., 17 lut 2020 o 22:13 Geert Uytterhoeven <geert@linux-m68k.org> napisaƂ(a):
>
> Hi Bartosz,
>
> On Mon, Feb 17, 2020 at 8:56 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
> > From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
> >
> > We can obtain the write-protect GPIO in nvmem_register() by requesting
> > it ourselves or by storing the gpio_desc passed in nvmem_config. In the
> > latter case we need to increase the reference count so that it gets
> > freed correctly.
> >
> > Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
>
> Thanks for your patch!
>
> > --- a/drivers/nvmem/core.c
> > +++ b/drivers/nvmem/core.c
> > @@ -349,11 +349,13 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
> >                 return ERR_PTR(rval);
> >         }
> >
> > -       if (config->wp_gpio)
> > +       if (config->wp_gpio) {
> >                 nvmem->wp_gpio = config->wp_gpio;
> > -       else
> > +               gpiod_ref(config->wp_gpio);
>
> If gpiod_ref() would return the passed GPIO, or an error code, you could write
>
>         nvmem->wp_gpio = gpiod_ref(config->wp_gpio);
>

Yes, makes perfect sense - v2 coming up tomorrow.

Bart
diff mbox series

Patch

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 096c7bae9e74..b7b1e3194453 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -349,11 +349,13 @@  struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 		return ERR_PTR(rval);
 	}
 
-	if (config->wp_gpio)
+	if (config->wp_gpio) {
 		nvmem->wp_gpio = config->wp_gpio;
-	else
+		gpiod_ref(config->wp_gpio);
+	} else {
 		nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
 						    GPIOD_OUT_HIGH);
+	}
 	if (IS_ERR(nvmem->wp_gpio))
 		goto err_ida_remove;