From patchwork Wed Jul 1 17:26:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Reichel X-Patchwork-Id: 1321302 X-Patchwork-Delegate: sbabic@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=collabora.com Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49yFxP2yGHz9sPF for ; Thu, 2 Jul 2020 21:19:53 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8EF1281BD8; Thu, 2 Jul 2020 13:14:21 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=collabora.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id E9DA781991; Wed, 1 Jul 2020 19:27:10 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, UNPARSEABLE_RELAY,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 3842581B57 for ; Wed, 1 Jul 2020 19:27:01 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sebastian.reichel@collabora.com Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: sre) with ESMTPSA id D1CCB2A54D2 Received: by jupiter.universe (Postfix, from userid 1000) id 28EAB48010B; Wed, 1 Jul 2020 19:26:57 +0200 (CEST) From: Sebastian Reichel To: Sebastian Reichel , Stefano Babic , Fabio Estevam , "NXP i.MX U-Boot Team" Cc: Jaehoon Chung , Simon Glass , u-boot@lists.denx.de Subject: [PATCH 08/12] poweroff: Add poweroff-gpio driver Date: Wed, 1 Jul 2020 19:26:21 +0200 Message-Id: <20200701172625.121978-9-sebastian.reichel@collabora.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200701172625.121978-1-sebastian.reichel@collabora.com> References: <20200701172625.121978-1-sebastian.reichel@collabora.com> MIME-Version: 1.0 X-Mailman-Approved-At: Thu, 02 Jul 2020 13:12:53 +0200 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.3 at phobos.denx.de X-Virus-Status: Clean Add GPIO poweroff driver, which is based on the Linux driver and uses the same DT binding. Signed-off-by: Sebastian Reichel --- 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 + * Andrew Lunn + * + * Copyright (C) 2012 Jamie Lentin + */ + +#include +#include +#include +#include +#include +#include + +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, +};