mbox series

[0/2] leds: gpio: Add devlink between the leds-gpio device and the gpio used.

Message ID 20240220111111.133826-1-herve.codina@bootlin.com
Headers show
Series leds: gpio: Add devlink between the leds-gpio device and the gpio used. | expand

Message

Herve Codina Feb. 20, 2024, 11:11 a.m. UTC
Hi,

When a gpio used by the leds-gpio device is removed, the leds-gpio
device continues to use this gpio. Also, when the gpio is back, the
leds-gpio still uses the old removed gpio.

A consumer/supplier relationship is missing between the leds-gpio device
(consumer) and the gpio used (supplier).

This series adds an addionnal devlink between this two device.
With this link when the gpio is removed, the leds-gpio device is also
removed.

Best regards,
Hervé Codina

Herve Codina (2):
  gpiolib: Introduce gpiod_device_add_link()
  leds: gpio: Add devlinks between the gpio consumed and the gpio leds
    device

 drivers/gpio/gpiolib.c        | 32 ++++++++++++++++++++++++++++++++
 drivers/leds/leds-gpio.c      | 15 +++++++++++++++
 include/linux/gpio/consumer.h |  5 +++++
 3 files changed, 52 insertions(+)

Comments

Bartosz Golaszewski Feb. 20, 2024, 11:40 a.m. UTC | #1
On Tue, Feb 20, 2024 at 12:11 PM Herve Codina <herve.codina@bootlin.com> wrote:
>
> Hi,
>
> When a gpio used by the leds-gpio device is removed, the leds-gpio
> device continues to use this gpio. Also, when the gpio is back, the
> leds-gpio still uses the old removed gpio.
>
> A consumer/supplier relationship is missing between the leds-gpio device
> (consumer) and the gpio used (supplier).
>
> This series adds an addionnal devlink between this two device.
> With this link when the gpio is removed, the leds-gpio device is also
> removed.
>
> Best regards,
> Hervé Codina
>
> Herve Codina (2):
>   gpiolib: Introduce gpiod_device_add_link()
>   leds: gpio: Add devlinks between the gpio consumed and the gpio leds
>     device
>
>  drivers/gpio/gpiolib.c        | 32 ++++++++++++++++++++++++++++++++
>  drivers/leds/leds-gpio.c      | 15 +++++++++++++++
>  include/linux/gpio/consumer.h |  5 +++++
>  3 files changed, 52 insertions(+)
>
> --
> 2.43.0
>

You should probably Cc Saravana on anything devlink related.

Bart
Linus Walleij Feb. 21, 2024, 10:45 p.m. UTC | #2
Saravana needs to look at this patch too, so I am top-posting.

Yours,
Linus Walleij

On Tue, Feb 20, 2024 at 12:11 PM Herve Codina <herve.codina@bootlin.com> wrote:

> When a gpio is removed, the gpio leds consumer need to be removed first.
> This dependency consumer/supplier can be described by devlink links.
> In case of device-tree, even if some devlinks are created due to the
> presence of gpio phandles in the gpio leds children, these links do not
> help in removing the gpio leds device (i.e. the real consumer) before
> the consumed gpio.
> We can reach cases where the gpio are no more present and the gpio leds
> driver continue to have leds using these gpio.
> Further more, when the gpio come back, the gpio leds still use the old
> removed one.
>
> Indeed, the gpio are consumed by the parent of the consumer used in the
> devlink creation due to phandles. A link is missing between the gpio and
> the real gpio consumer, the gpio leds device itself.
>
> Use the newly introduced gpiod_device_add_link() to create this
> missing link between the gpio leds devices and the gpios.
> With that done, if a gpio is removed, the gpio leds is removed and the
> resources are correctly released.
>
> Signed-off-by: Herve Codina <herve.codina@bootlin.com>
> ---
>  drivers/leds/leds-gpio.c | 15 +++++++++++++++
>  1 file changed, 15 insertions(+)
>
> diff --git a/drivers/leds/leds-gpio.c b/drivers/leds/leds-gpio.c
> index 83fcd7b6afff..b3ec8ecbe5da 100644
> --- a/drivers/leds/leds-gpio.c
> +++ b/drivers/leds/leds-gpio.c
> @@ -150,6 +150,7 @@ static struct gpio_leds_priv *gpio_leds_create(struct device *dev)
>  {
>         struct fwnode_handle *child;
>         struct gpio_leds_priv *priv;
> +       struct device_link *link;
>         int count, ret;
>
>         count = device_get_child_node_count(dev);
> @@ -197,6 +198,20 @@ static struct gpio_leds_priv *gpio_leds_create(struct device *dev)
>                 /* Set gpiod label to match the corresponding LED name. */
>                 gpiod_set_consumer_name(led_dat->gpiod,
>                                         led_dat->cdev.dev->kobj.name);
> +
> +               /*
> +                * Create a link between the GPIO and the gpio-leds device.
> +                * This allow to have a relationship between the gpio used and
> +                * the gpio-leds device in order to automatically remove the
> +                * gpio-leds device (consumer) when a GPIO (supplier) is removed.
> +                */
> +               link = gpiod_device_add_link(dev, led_dat->gpiod,
> +                                            DL_FLAG_AUTOREMOVE_CONSUMER);
> +               if (IS_ERR(link)) {
> +                       fwnode_handle_put(child);
> +                       return ERR_CAST(link);
> +               }
> +
>                 priv->num_leds++;
>         }
>
> --
> 2.43.0
>