Skip to contentSkip to conversation

gpiolib: add Linux-like gpiod_get() helper

PATCH Applied Ahmad Fatoum 3 participants 7 messages Apr 10, 2021 – Apr 13, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/5 gpiolib: add Linux-like gpiod_get() helper

Many Linux drivers use [devm_]gpiod_get to get appropriately configured GPIO descriptors out with little code. Make porting such Linux code easier by providing a semi-compatible gpiod_get function. Main differences:

- It returns a gpio index, so it can be passed to any gpio_ function
- It's device-tree only, so it should only be used from drivers that themselves probe from device tree.

---
 drivers/gpio/gpiolib.c | 43 ++++++++++++++++++++++++++++++++++++++++++
 include/gpiod.h        | 26 +++++++++++++++++++++++++
 2 files changed, 69 insertions(+)
 create mode 100644 include/gpiod.h
drivers/gpio/gpiolib.c +43 -0
index 6088cadd8a18..7b7261d01f24 100644
@@ -6,6 +6,7 @@
 #include <complete.h>
 #include <gpio.h>
 #include <of_gpio.h>
+#include <gpiod.h>
 #include <errno.h>
 #include <malloc.h>
 
@@ -554,6 +555,48 @@ static int of_gpiochip_scan_hogs(struct gpio_chip *chip)
 	return 0;
 }
 
+/* Linux compatibility helper: Get a GPIO descriptor from device tree */
+int gpiod_get(struct device_d *dev, const char *_con_id, enum gpiod_flags flags)
+{
+	struct device_node *np = dev->device_node;
+	enum of_gpio_flags of_flags;
+	const char *con_id = "gpios", *label = dev_name(dev);
+	char *buf = NULL;
+	int gpio;
+	int ret;
+
+	if (!IS_ENABLED(CONFIG_OFDEVICE) || !dev->device_node)
+		return -ENODEV;
+
+	if (_con_id) {
+		con_id = buf = basprintf("%s-gpios", _con_id);
+		if (!buf)
+			return -ENOMEM;
+	}
+
+	gpio = of_get_named_gpio_flags(np, con_id, 0, &of_flags);
+	free(buf);
+
+	if (!gpio_is_valid(gpio))
+		return gpio < 0 ? gpio : -EINVAL;
+
+	if (of_flags & OF_GPIO_ACTIVE_LOW)
+		flags |= GPIOF_ACTIVE_LOW;
+
+	buf = NULL;
+
+	if (_con_id) {
+		label = buf = basprintf("%s-%s", dev_name(dev), _con_id);
+		if (!label)
+			return -ENOMEM;
+	}
+
+	ret = gpio_request_one(gpio, flags, label);
+	free(buf);
+
+	return ret ?: gpio;
+}
+
 int gpiochip_add(struct gpio_chip *chip)
 {
 	int base, i;
include/gpiod.h new file +26 -0
new file mode 100644
index 000000000000..c8b2cd47a3cb
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __GPIOD_H_
+#define __GPIOD_H_
+
+#include <gpio.h>
+#include <of_gpio.h>
+
+/**
+ * Optional flags that can be passed to one of gpiod_* to configure direction
+ * and output value. These values cannot be OR'd.
+ */
+enum gpiod_flags {
+	GPIOD_ASIS	= 0,
+	GPIOD_IN	= GPIOF_IN,
+	/*
+	 * To change this later to a different logic level (i.e. taking
+	 * active low into account), use gpio_direction_active()
+	 */
+	GPIOD_OUT_LOW	= GPIOF_OUT_INIT_INACTIVE,
+	GPIOD_OUT_HIGH	= GPIOF_OUT_INIT_ACTIVE,
+};
+
+/* returned gpio descriptor can be passed to any normal gpio_* function */
+int gpiod_get(struct device_d *dev, const char *_con_id, enum gpiod_flags flags);
+
+#endif
Signature · 7 lines
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

PATCH 2/5 sound: gpio-beeper: simplify using new gpiod_get helper

the new gpid_get helper allows us to remove the manual active state setting. Make use of this.

This also fixes an issue where the initial state of the beeper was on, not off.

---
 drivers/sound/gpio-beeper.c | 20 +++++---------------
 1 file changed, 5 insertions(+), 15 deletions(-)
drivers/sound/gpio-beeper.c +5 -15
index 86fd4a4ee67c..300998d6079b 100644
@@ -7,8 +7,7 @@
 #include <regulator.h>
 #include <sound.h>
 #include <of.h>
-#include <gpio.h>
-#include <of_gpio.h>
+#include <gpiod.h>
 
 struct gpio_beeper {
 	int gpio;
@@ -28,21 +27,12 @@ static int gpio_beeper_probe(struct device_d *dev)
 	struct device_node *np = dev->device_node;
 	struct gpio_beeper *beeper;
 	struct sound_card *card;
-	enum of_gpio_flags of_flags;
-	unsigned long gpio_flags = GPIOF_OUT_INIT_ACTIVE;
-	int ret, gpio;
+	int gpio;
 
-	gpio = of_get_named_gpio_flags(np, "gpios", 0, &of_flags);
-	if (!gpio_is_valid(gpio))
+	gpio = gpiod_get(dev, NULL, GPIOD_OUT_LOW);
+	if (gpio < 0) {
+		dev_err(dev, "failed to request gpio: %pe\n", ERR_PTR(gpio));
 		return gpio;
-
-	if (of_flags & OF_GPIO_ACTIVE_LOW)
-		gpio_flags |= GPIOF_ACTIVE_LOW;
-
-	ret = gpio_request_one(gpio, gpio_flags, "gpio-beeper");
-	if (ret) {
-		dev_err(dev, "failed to request gpio %d: %d\n", gpio, ret);
-		return ret;
 	}
 
 	beeper = xzalloc(sizeof(*beeper));
Signature · 7 lines
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

PATCH 3/5 power: reset: add GPIO poweroff driver

In reply to Ahmad Fatoum

This is a straight port from Linux v5.11.

---
 drivers/power/reset/Kconfig         |  8 +++
 drivers/power/reset/Makefile        |  1 +
 drivers/power/reset/gpio-poweroff.c | 93 +++++++++++++++++++++++++++++
 3 files changed, 102 insertions(+)
 create mode 100644 drivers/power/reset/gpio-poweroff.c
drivers/power/reset/Kconfig +8 -0
index e60037a6e637..cadcc0b13f2f 100644
@@ -28,3 +28,11 @@ config POWER_RESET_SYSCON_POWEROFF
 	select MFD_SYSCON
 	help
 	  Poweroff support for generic SYSCON mapped register poweroff.
+
+config POWER_RESET_GPIO
+	bool "GPIO power-off driver"
+	depends on OF_GPIO
+	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.
drivers/power/reset/Makefile +1 -0
index a490dce87333..438c85970028 100644
@@ -3,3 +3,4 @@ obj-$(CONFIG_REBOOT_MODE) += reboot-mode.o
 obj-$(CONFIG_SYSCON_REBOOT_MODE) += syscon-reboot-mode.o
 obj-$(CONFIG_POWER_RESET_SYSCON) += syscon-reboot.o
 obj-$(CONFIG_POWER_RESET_SYSCON_POWEROFF) += syscon-poweroff.o
+obj-$(CONFIG_POWER_RESET_GPIO) += gpio-poweroff.o
drivers/power/reset/gpio-poweroff.c new file +93 -0

93 lines not on this page · raw message

Signature · 7 lines
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw

PATCH 4/5 power: reset: add GPIO restart driver

In reply to Ahmad Fatoum

This is a straight port from Linux v5.11.

---
 drivers/power/reset/Kconfig        |  8 +++
 drivers/power/reset/Makefile       |  1 +
 drivers/power/reset/gpio-restart.c | 99 ++++++++++++++++++++++++++++++
 3 files changed, 108 insertions(+)
 create mode 100644 drivers/power/reset/gpio-restart.c
drivers/power/reset/Kconfig +8 -0
index cadcc0b13f2f..dec1482ccd0c 100644
@@ -36,3 +36,11 @@ config POWER_RESET_GPIO
 	  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.
+
+config POWER_RESET_GPIO_RESTART
+	bool "GPIO restart driver"
+	depends on OF_GPIO
+	help
+	  This driver supports restarting your board via a GPIO line.
+	  If your board needs a GPIO high/low to restart, say Y and
+	  create a binding in your devicetree.
drivers/power/reset/Makefile +1 -0
index 438c85970028..33d29d2d9546 100644
@@ -4,3 +4,4 @@ obj-$(CONFIG_SYSCON_REBOOT_MODE) += syscon-reboot-mode.o
 obj-$(CONFIG_POWER_RESET_SYSCON) += syscon-reboot.o
 obj-$(CONFIG_POWER_RESET_SYSCON_POWEROFF) += syscon-poweroff.o
 obj-$(CONFIG_POWER_RESET_GPIO) += gpio-poweroff.o
+obj-$(CONFIG_POWER_RESET_GPIO_RESTART) += gpio-restart.o
drivers/power/reset/gpio-restart.c new file +99 -0

99 lines not on this page · raw message

Signature · 7 lines
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Tested-by on v1 4/5
#4 lore raw

Re: [PATCH 4/5] power: reset: add GPIO restart driver

Quoted 4 lines · Ahmad Fatoum

This is a straight port from Linux v5.11.

Tested on imx7-tqma7 platform.

Quoted 141 lines
---
 drivers/power/reset/Kconfig        |  8 +++
 drivers/power/reset/Makefile       |  1 +
 drivers/power/reset/gpio-restart.c | 99 ++++++++++++++++++++++++++++++
 3 files changed, 108 insertions(+)
 create mode 100644 drivers/power/reset/gpio-restart.c
drivers/power/reset/Kconfig +8 -0
index cadcc0b13f2f..dec1482ccd0c 100644
@@ -36,3 +36,11 @@ config POWER_RESET_GPIO
          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.
+
+config POWER_RESET_GPIO_RESTART
+       bool "GPIO restart driver"
+       depends on OF_GPIO
+       help
+         This driver supports restarting your board via a GPIO line.
+         If your board needs a GPIO high/low to restart, say Y and
+         create a binding in your devicetree.
drivers/power/reset/Makefile +1 -0
index 438c85970028..33d29d2d9546 100644
@@ -4,3 +4,4 @@ obj-$(CONFIG_SYSCON_REBOOT_MODE) += syscon-reboot-mode.o
 obj-$(CONFIG_POWER_RESET_SYSCON) += syscon-reboot.o
 obj-$(CONFIG_POWER_RESET_SYSCON_POWEROFF) += syscon-poweroff.o
 obj-$(CONFIG_POWER_RESET_GPIO) += gpio-poweroff.o
+obj-$(CONFIG_POWER_RESET_GPIO_RESTART) += gpio-restart.o
drivers/power/reset/gpio-restart.c new file +99 -1
new file mode 100644
index 000000000000..72c690a3cf4c
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Toggles a GPIO pin to restart a device
+ *
+ * Copyright (C) 2014 Google, Inc.
+ *
+ * Based on the gpio-poweroff driver.
+ */
+#include <common.h>
+#include <driver.h>
+#include <restart.h>
+#include <gpiod.h>
+
+struct gpio_restart {
+       int reset_gpio;
+       struct restart_handler restart_handler;
+       u32 active_delay_ms;
+       u32 inactive_delay_ms;
+       u32 wait_delay_ms;
+};
+
+static void __noreturn gpio_restart_handle(struct restart_handler *this)
+{
+       struct gpio_restart *gpio_restart =
+               container_of(this, struct gpio_restart, restart_handler);
+
+       /* drive it active, also inactive->active edge */
+       gpio_direction_active(gpio_restart->reset_gpio, true);
+       mdelay(gpio_restart->active_delay_ms);
+
+       /* drive inactive, also active->inactive edge */
+       gpio_set_active(gpio_restart->reset_gpio, false);
+       mdelay(gpio_restart->inactive_delay_ms);
+
+       /* drive it active, also inactive->active edge */
+       gpio_set_active(gpio_restart->reset_gpio, true);
+
+       /* give it some time */
+       mdelay(gpio_restart->wait_delay_ms);
+
+       mdelay(1000);
+
+       panic("Unable to restart system\n");
+}
+
+static int gpio_restart_probe(struct device_d *dev)
+{
+       struct device_node *np = dev->device_node;
+       struct gpio_restart *gpio_restart;
+       bool open_source = false;
+       u32 property;
+       int ret;
+
+       gpio_restart = xzalloc(sizeof(*gpio_restart));
+
+       open_source = of_property_read_bool(np, "open-source");
+
+       gpio_restart->reset_gpio = gpiod_get(dev, NULL,
+                       open_source ? GPIOD_IN : GPIOD_OUT_LOW);
+       ret = gpio_restart->reset_gpio;
+       if (ret < 0) {
+               if (ret != -EPROBE_DEFER)
+                       dev_err(dev, "Could not get reset GPIO\n");
+               return ret;
+       }
+
+       gpio_restart->restart_handler.restart = gpio_restart_handle;
+       gpio_restart->restart_handler.name = "gpio-restart";
+       gpio_restart->restart_handler.priority = 129;
+       gpio_restart->active_delay_ms = 100;
+       gpio_restart->inactive_delay_ms = 100;
+       gpio_restart->wait_delay_ms = 3000;
+
+       ret = of_property_read_u32(dev->device_node, "priority", &property);
+       if (!ret)
+               gpio_restart->restart_handler.priority = property;
+
+       of_property_read_u32(np, "active-delay", &gpio_restart->active_delay_ms);
+       of_property_read_u32(np, "inactive-delay", &gpio_restart->inactive_delay_ms);
+       of_property_read_u32(np, "wait-delay", &gpio_restart->wait_delay_ms);
+
+       return restart_handler_register(&gpio_restart->restart_handler);
+}
+
+static const struct of_device_id of_gpio_restart_match[] = {
+       { .compatible = "gpio-restart", },
+       {},
+};
+
+static struct driver_d gpio_restart_driver = {
+       .name = "restart-gpio",
+       .of_compatible = of_gpio_restart_match,
+       .probe = gpio_restart_probe,
+};
+device_platform_driver(gpio_restart_driver);
+
+MODULE_AUTHOR("David Riley <[email protected]>");
+MODULE_DESCRIPTION("GPIO restart driver");
+MODULE_LICENSE("GPL");
--

2.30.0

Quoted 4 lines

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#5 lore raw

PATCH 5/5 watchdog: add GPIO watchdog driver

In reply to Ahmad Fatoum

This is a straight port from Linux v5.11.

---
 drivers/watchdog/Kconfig    |   7 ++
 drivers/watchdog/Makefile   |   1 +
 drivers/watchdog/gpio_wdt.c | 142 ++++++++++++++++++++++++++++++++++++
 3 files changed, 150 insertions(+)
 create mode 100644 drivers/watchdog/gpio_wdt.c
drivers/watchdog/Kconfig +7 -0
index 8750a97d8c50..fb4ea7ffac3c 100644
@@ -117,6 +117,13 @@ config F71808E_WDT
 	  F71862FG, F71868, F71869, F71882FG, F71889FG, F81865 and F81866
 	  Super I/O controllers.
 
+config GPIO_WATCHDOG
+	tristate "Watchdog device controlled through GPIO-line"
+	depends on OF_GPIO
+	help
+	  If you say yes here you get support for watchdog device
+	  controlled through GPIO-line.
+
 config ITCO_WDT
 	bool "Intel TCO Timer/Watchdog"
 	depends on X86
drivers/watchdog/Makefile +1 -0
index b55c58cf3cb3..9edb73512903 100644
@@ -17,3 +17,4 @@ obj-$(CONFIG_STM32_IWDG_WATCHDOG) += stm32_iwdg.o
 obj-$(CONFIG_STPMIC1_WATCHDOG) += stpmic1_wdt.o
 obj-$(CONFIG_F71808E_WDT) += f71808e_wdt.o
 obj-$(CONFIG_ITCO_WDT) += itco_wdt.o
+obj-$(CONFIG_GPIO_WATCHDOG)	+= gpio_wdt.o
drivers/watchdog/gpio_wdt.c new file +142 -0

142 lines not on this page · raw message

Signature · 7 lines
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Applied
#6 lore raw
In reply to Ahmad Fatoum
Quoted 9 lines · Ahmad Fatoum

Many Linux drivers use [devm_]gpiod_get to get appropriately configured GPIO descriptors out with little code. Make porting such Linux code easier by providing a semi-compatible gpiod_get function. Main differences:

- It returns a gpio index, so it can be passed to any gpio_ function
- It's device-tree only, so it should only be used from drivers that themselves probe from device tree.

Quoted 6 lines
---
 drivers/gpio/gpiolib.c | 43 ++++++++++++++++++++++++++++++++++++++++++
 include/gpiod.h        | 26 +++++++++++++++++++++++++
 2 files changed, 69 insertions(+)
 create mode 100644 include/gpiod.h

Applied, thanks

Sascha

Quoted 103 lines
drivers/gpio/gpiolib.c +43 -0
index 6088cadd8a18..7b7261d01f24 100644
@@ -6,6 +6,7 @@
 #include <complete.h>
 #include <gpio.h>
 #include <of_gpio.h>
+#include <gpiod.h>
 #include <errno.h>
 #include <malloc.h>
 
@@ -554,6 +555,48 @@ static int of_gpiochip_scan_hogs(struct gpio_chip *chip)
 	return 0;
 }
 
+/* Linux compatibility helper: Get a GPIO descriptor from device tree */
+int gpiod_get(struct device_d *dev, const char *_con_id, enum gpiod_flags flags)
+{
+	struct device_node *np = dev->device_node;
+	enum of_gpio_flags of_flags;
+	const char *con_id = "gpios", *label = dev_name(dev);
+	char *buf = NULL;
+	int gpio;
+	int ret;
+
+	if (!IS_ENABLED(CONFIG_OFDEVICE) || !dev->device_node)
+		return -ENODEV;
+
+	if (_con_id) {
+		con_id = buf = basprintf("%s-gpios", _con_id);
+		if (!buf)
+			return -ENOMEM;
+	}
+
+	gpio = of_get_named_gpio_flags(np, con_id, 0, &of_flags);
+	free(buf);
+
+	if (!gpio_is_valid(gpio))
+		return gpio < 0 ? gpio : -EINVAL;
+
+	if (of_flags & OF_GPIO_ACTIVE_LOW)
+		flags |= GPIOF_ACTIVE_LOW;
+
+	buf = NULL;
+
+	if (_con_id) {
+		label = buf = basprintf("%s-%s", dev_name(dev), _con_id);
+		if (!label)
+			return -ENOMEM;
+	}
+
+	ret = gpio_request_one(gpio, flags, label);
+	free(buf);
+
+	return ret ?: gpio;
+}
+
 int gpiochip_add(struct gpio_chip *chip)
 {
 	int base, i;
include/gpiod.h new file +26 -0
new file mode 100644
index 000000000000..c8b2cd47a3cb
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __GPIOD_H_
+#define __GPIOD_H_
+
+#include <gpio.h>
+#include <of_gpio.h>
+
+/**
+ * Optional flags that can be passed to one of gpiod_* to configure direction
+ * and output value. These values cannot be OR'd.
+ */
+enum gpiod_flags {
+	GPIOD_ASIS	= 0,
+	GPIOD_IN	= GPIOF_IN,
+	/*
+	 * To change this later to a different logic level (i.e. taking
+	 * active low into account), use gpio_direction_active()
+	 */
+	GPIOD_OUT_LOW	= GPIOF_OUT_INIT_INACTIVE,
+	GPIOD_OUT_HIGH	= GPIOF_OUT_INIT_ACTIVE,
+};
+
+/* returned gpio descriptor can be passed to any normal gpio_* function */
+int gpiod_get(struct device_d *dev, const char *_con_id, enum gpiod_flags flags);
+
+#endif
Signature · 7 lines
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox