[PATCH 2/7] dm: button: add a driver for button driven by gpio

Simon Glass sjg at chromium.org
Wed Jul 15 03:05:39 CEST 2020


On Mon, 13 Jul 2020 at 06:56, Philippe Reynes
<philippe.reynes at softathome.com> wrote:
>
> Add a simple driver which allows use of buttons attached to GPIOs.
>
> Signed-off-by: Philippe Reynes <philippe.reynes at softathome.com>
> ---
>  drivers/button/Kconfig       |  10 ++++
>  drivers/button/Makefile      |   1 +
>  drivers/button/button-gpio.c | 111 +++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 122 insertions(+)
>  create mode 100644 drivers/button/button-gpio.c

Reviewed-by: Simon Glass <sjg at chromium.org>


>
> diff --git a/drivers/button/Kconfig b/drivers/button/Kconfig
> index 8301858..7de1a97 100644
> --- a/drivers/button/Kconfig
> +++ b/drivers/button/Kconfig
> @@ -9,4 +9,14 @@ config BUTTON
>           can provide access to board-specific buttons. Use of the device tree
>           for configuration is encouraged.
>
> +config BUTTON_GPIO
> +       bool "Button gpio"
> +       depends on BUTTON
> +       default n

not needed

> +       help
> +         Enable support for buttons which are connected to GPIO lines. These
> +         GPIOs may be on the SoC or some other device which provides GPIOs.
> +         The GPIO driver must used driver model. Buttons are configured using
> +         the device tree.
> +
>  endmenu
> diff --git a/drivers/button/Makefile b/drivers/button/Makefile
> index 0b4c128..fcc10eb 100644
> --- a/drivers/button/Makefile
> +++ b/drivers/button/Makefile
> @@ -3,3 +3,4 @@
>  # Copyright (C) 2020 Philippe Reynes <philippe.reynes at softathome.com>
>
>  obj-$(CONFIG_BUTTON) += button-uclass.o
> +obj-$(CONFIG_BUTTON_GPIO) += button-gpio.o
> diff --git a/drivers/button/button-gpio.c b/drivers/button/button-gpio.c
> new file mode 100644
> index 0000000..6ad0094
> --- /dev/null
> +++ b/drivers/button/button-gpio.c
> @@ -0,0 +1,111 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Copyright (C) 2020 Philippe Reynes <philippe.reynes at softathome.com>
> + */
> +
> +#include <common.h>
> +#include <dm.h>
> +#include <dm/lists.h>
> +#include <dm/uclass-internal.h>
> +#include <log.h>
> +#include <asm/gpio.h>
> +#include <button.h>

Please fix order
https://www.denx.de/wiki/U-Boot/CodingStyle

> +
> +struct button_gpio_priv {
> +       struct gpio_desc gpio;
> +};
> +
> +static enum button_state_t button_gpio_get_state(struct udevice *dev)
> +{
> +       struct button_gpio_priv *priv = dev_get_priv(dev);
> +       int ret;
> +
> +       if (!dm_gpio_is_valid(&priv->gpio))
> +               return -EREMOTEIO;
> +       ret = dm_gpio_get_value(&priv->gpio);
> +       if (ret < 0)
> +               return ret;
> +
> +       return ret ? BUTTON_ON : BUTTON_OFF;
> +}
> +
> +static int button_gpio_probe(struct udevice *dev)
> +{
> +       struct button_uc_plat *uc_plat = dev_get_uclass_platdata(dev);
> +       struct button_gpio_priv *priv = dev_get_priv(dev);
> +       int ret;
> +
> +       /* Ignore the top-level button node */
> +       if (!uc_plat->label)
> +               return 0;
> +
> +       ret = gpio_request_by_name(dev, "gpios", 0, &priv->gpio, GPIOD_IS_IN);
> +       if (ret)
> +               return ret;
> +
> +       return 0;
> +}
> +
> +static int button_gpio_remove(struct udevice *dev)
> +{
> +       /*
> +        * The GPIO driver may have already been removed. We will need to
> +        * address this more generally.

What is needed here?

> +        */
> +       if (IS_ENABLED(CONFIG_SANDBOX)) {
> +               struct button_gpio_priv *priv = dev_get_priv(dev);
> +
> +               if (dm_gpio_is_valid(&priv->gpio))
> +                       dm_gpio_free(dev, &priv->gpio);
> +       }
> +
> +       return 0;
> +}
> +

[..]


More information about the U-Boot mailing list