diff mbox series

[08/12] poweroff: Add poweroff-gpio driver

Message ID 20200701172625.121978-9-sebastian.reichel@collabora.com
State Superseded
Delegated to: Stefano Babic
Headers show
Series Introduce B1x5v2 support | expand

Commit Message

Sebastian Reichel July 1, 2020, 5:26 p.m. UTC
Add GPIO poweroff driver, which is based on the Linux
driver and uses the same DT binding.

Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
---
 drivers/power/Kconfig         |  8 ++++
 drivers/power/Makefile        |  1 +
 drivers/power/poweroff-gpio.c | 87 +++++++++++++++++++++++++++++++++++
 3 files changed, 96 insertions(+)
 create mode 100644 drivers/power/poweroff-gpio.c

Comments

Jaehoon Chung July 8, 2020, 9:58 p.m. UTC | #1
On 7/2/20 2:26 AM, Sebastian Reichel wrote:
> Add GPIO poweroff driver, which is based on the Linux
> driver and uses the same DT binding.
> 
> Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
> ---
>  drivers/power/Kconfig         |  8 ++++
>  drivers/power/Makefile        |  1 +
>  drivers/power/poweroff-gpio.c | 87 +++++++++++++++++++++++++++++++++++
>  3 files changed, 96 insertions(+)
>  create mode 100644 drivers/power/poweroff-gpio.c
> 
> diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig
> index 5a76e23b5fb0..d1eaa0895ddd 100644
> --- a/drivers/power/Kconfig
> +++ b/drivers/power/Kconfig
> @@ -382,4 +382,12 @@ config POWEROFF
>  	  provide access to board-specific implementations. Using device
>  	  tree for configuration is recommended.
>  
> +config POWEROFF_GPIO
> +	bool "GPIO power-off driver"
> +	depends on POWEROFF
> +	help
> +	  This driver supports turning off your board via a GPIO line.
> +	  If your board needs a GPIO high/low to power down, say Y and
> +	  create a binding in your devicetree.
> +
>  endmenu
> diff --git a/drivers/power/Makefile b/drivers/power/Makefile
> index 924e99114ccd..34a2c2c48195 100644
> --- a/drivers/power/Makefile
> +++ b/drivers/power/Makefile
> @@ -23,3 +23,4 @@ obj-$(CONFIG_POWER_SPI) += power_spi.o
>  obj-$(CONFIG_POWER_MT6323) += mt6323.o
>  
>  obj-$(CONFIG_POWEROFF) += poweroff-uclass.o
> +obj-$(CONFIG_POWEROFF_GPIO) += poweroff-gpio.o
> diff --git a/drivers/power/poweroff-gpio.c b/drivers/power/poweroff-gpio.c
> new file mode 100644
> index 000000000000..cf2bdab9e551
> --- /dev/null
> +++ b/drivers/power/poweroff-gpio.c
> @@ -0,0 +1,87 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Toggles a GPIO pin to power down a device
> + *
> + * Created using the Linux driver as reference, which
> + * has been written by:
> + *
> + * Jamie Lentin <jm@lentin.co.uk>
> + * Andrew Lunn <andrew@lunn.ch>
> + *
> + * Copyright (C) 2012 Jamie Lentin
> + */
> +
> +#include <common.h>
> +#include <asm/gpio.h>
> +#include <dm.h>
> +#include <errno.h>
> +#include <linux/delay.h>
> +#include <power/poweroff.h>
> +
> +struct poweroff_gpio_info {
> +	struct gpio_desc gpio;
> +	u32 active_delay;
> +	u32 inactive_delay;
> +	u32 timeout;
> +};
> +
> +static int poweroff_gpio_do_poweroff(struct udevice *dev)
> +{
> +	struct poweroff_gpio_info *priv = dev_get_priv(dev);
> +
> +	/* drive it active, also inactive->active edge */
> +	dm_gpio_set_value(&priv->gpio, 1);
> +	mdelay(priv->active_delay);
> +
> +	/* drive inactive, also active->inactive edge */
> +	dm_gpio_set_value(&priv->gpio, 0);
> +	mdelay(priv->inactive_delay);
> +
> +	/* drive it active, also inactive->active edge */
> +	dm_gpio_set_value(&priv->gpio, 1);
> +
> +	/* give it some time */
> +	mdelay(priv->timeout);
> +
> +	return -ETIMEDOUT;
> +}
> +
> +static int poweroff_gpio_probe(struct udevice *dev)
> +{
> +	struct poweroff_gpio_info *priv = dev_get_priv(dev);
> +	int flags;
> +
> +	if (dev_read_bool(dev, "input"))
> +		flags = GPIOD_IS_IN;
> +	else
> +		flags = GPIOD_IS_OUT;
> +
> +	priv->active_delay = 100;
> +	dev_read_u32(dev, "active-delay-ms", &priv->active_delay);

How about using dev_read_u32_default() instead of dev_read_u32()?
priv->activate_delay = dev_read_u32_default(dev, "active-delay-ms", 100);

> +
> +	priv->inactive_delay = 100;
> +	dev_read_u32(dev, "inactive-delay-ms", &priv->inactive_delay);

ditto

> +
> +	priv->timeout = 3000;
> +	dev_read_u32(dev, "timeout-ms", &priv->timeout);

ditto

Best Regards,
Jaehoon Chung

> +
> +	return gpio_request_by_name(dev, "gpios", 0, &priv->gpio, flags);
> +}
> +
> +static const struct poweroff_ops poweroff_gpio_ops = {
> +	.poweroff = poweroff_gpio_do_poweroff,
> +};
> +
> +static const struct udevice_id poweroff_gpio_ids[] = {
> +	{ .compatible = "gpio-poweroff", },
> +	{},
> +};
> +
> +U_BOOT_DRIVER(poweroff_gpio) = {
> +	.name		= "poweroff-gpio",
> +	.id		= UCLASS_POWEROFF,
> +	.ops		= &poweroff_gpio_ops,
> +	.probe		= poweroff_gpio_probe,
> +	.priv_auto_alloc_size = sizeof(struct poweroff_gpio_info),
> +	.of_match	= poweroff_gpio_ids,
> +};
>
Simon Glass July 8, 2020, 10:06 p.m. UTC | #2
Hi Sebastian,

On Wed, 1 Jul 2020 at 11:27, Sebastian Reichel
<sebastian.reichel@collabora.com> wrote:
>
> Add GPIO poweroff driver, which is based on the Linux
> driver and uses the same DT binding.
>
> Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
> ---
>  drivers/power/Kconfig         |  8 ++++
>  drivers/power/Makefile        |  1 +
>  drivers/power/poweroff-gpio.c | 87 +++++++++++++++++++++++++++++++++++
>  3 files changed, 96 insertions(+)
>  create mode 100644 drivers/power/poweroff-gpio.c

Can you use the SYSRESET uclass instead of creating a new one?

Regards,
SImon
diff mbox series

Patch

diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig
index 5a76e23b5fb0..d1eaa0895ddd 100644
--- a/drivers/power/Kconfig
+++ b/drivers/power/Kconfig
@@ -382,4 +382,12 @@  config POWEROFF
 	  provide access to board-specific implementations. Using device
 	  tree for configuration is recommended.
 
+config POWEROFF_GPIO
+	bool "GPIO power-off driver"
+	depends on POWEROFF
+	help
+	  This driver supports turning off your board via a GPIO line.
+	  If your board needs a GPIO high/low to power down, say Y and
+	  create a binding in your devicetree.
+
 endmenu
diff --git a/drivers/power/Makefile b/drivers/power/Makefile
index 924e99114ccd..34a2c2c48195 100644
--- a/drivers/power/Makefile
+++ b/drivers/power/Makefile
@@ -23,3 +23,4 @@  obj-$(CONFIG_POWER_SPI) += power_spi.o
 obj-$(CONFIG_POWER_MT6323) += mt6323.o
 
 obj-$(CONFIG_POWEROFF) += poweroff-uclass.o
+obj-$(CONFIG_POWEROFF_GPIO) += poweroff-gpio.o
diff --git a/drivers/power/poweroff-gpio.c b/drivers/power/poweroff-gpio.c
new file mode 100644
index 000000000000..cf2bdab9e551
--- /dev/null
+++ b/drivers/power/poweroff-gpio.c
@@ -0,0 +1,87 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Toggles a GPIO pin to power down a device
+ *
+ * Created using the Linux driver as reference, which
+ * has been written by:
+ *
+ * Jamie Lentin <jm@lentin.co.uk>
+ * Andrew Lunn <andrew@lunn.ch>
+ *
+ * Copyright (C) 2012 Jamie Lentin
+ */
+
+#include <common.h>
+#include <asm/gpio.h>
+#include <dm.h>
+#include <errno.h>
+#include <linux/delay.h>
+#include <power/poweroff.h>
+
+struct poweroff_gpio_info {
+	struct gpio_desc gpio;
+	u32 active_delay;
+	u32 inactive_delay;
+	u32 timeout;
+};
+
+static int poweroff_gpio_do_poweroff(struct udevice *dev)
+{
+	struct poweroff_gpio_info *priv = dev_get_priv(dev);
+
+	/* drive it active, also inactive->active edge */
+	dm_gpio_set_value(&priv->gpio, 1);
+	mdelay(priv->active_delay);
+
+	/* drive inactive, also active->inactive edge */
+	dm_gpio_set_value(&priv->gpio, 0);
+	mdelay(priv->inactive_delay);
+
+	/* drive it active, also inactive->active edge */
+	dm_gpio_set_value(&priv->gpio, 1);
+
+	/* give it some time */
+	mdelay(priv->timeout);
+
+	return -ETIMEDOUT;
+}
+
+static int poweroff_gpio_probe(struct udevice *dev)
+{
+	struct poweroff_gpio_info *priv = dev_get_priv(dev);
+	int flags;
+
+	if (dev_read_bool(dev, "input"))
+		flags = GPIOD_IS_IN;
+	else
+		flags = GPIOD_IS_OUT;
+
+	priv->active_delay = 100;
+	dev_read_u32(dev, "active-delay-ms", &priv->active_delay);
+
+	priv->inactive_delay = 100;
+	dev_read_u32(dev, "inactive-delay-ms", &priv->inactive_delay);
+
+	priv->timeout = 3000;
+	dev_read_u32(dev, "timeout-ms", &priv->timeout);
+
+	return gpio_request_by_name(dev, "gpios", 0, &priv->gpio, flags);
+}
+
+static const struct poweroff_ops poweroff_gpio_ops = {
+	.poweroff = poweroff_gpio_do_poweroff,
+};
+
+static const struct udevice_id poweroff_gpio_ids[] = {
+	{ .compatible = "gpio-poweroff", },
+	{},
+};
+
+U_BOOT_DRIVER(poweroff_gpio) = {
+	.name		= "poweroff-gpio",
+	.id		= UCLASS_POWEROFF,
+	.ops		= &poweroff_gpio_ops,
+	.probe		= poweroff_gpio_probe,
+	.priv_auto_alloc_size = sizeof(struct poweroff_gpio_info),
+	.of_match	= poweroff_gpio_ids,
+};