Skip to content

Commit

Permalink
Add support for gpio-poweroff (torvalds#29)
Browse files Browse the repository at this point in the history
Cherry picked from Raspberry Pi kernel source.

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
Signed-off-by: Nick Bulleid <nedbulleid@fastmail.com>
  • Loading branch information
theophile authored and 0x011011110 committed Nov 23, 2024
1 parent a7c603b commit e80c9d3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Optional properties:
- inactive-delay-ms: Delay (default 100) to wait after driving gpio inactive
- timeout-ms: Time to wait before asserting a WARN_ON(1). If nothing is
specified, 3000 ms is used.
- export : Export the GPIO line to the sysfs system

Examples:

Expand Down
3 changes: 2 additions & 1 deletion arch/arm64/boot/dts/amlogic/overlay/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ dtbo-$(CONFIG_ARCH_MESON) += \
meson-g12b-spi-b-gpioh-4-5-6-7.dtbo \
meson-g12b-uart-ao-a-gpioao-0-1.dtbo \
meson-g12b-uart-ao-b-gpioao-8-9.dtbo \
meson-g12b-uart-ee-c-gpioh-4-5-6-7.dtbo
meson-g12b-uart-ee-c-gpioh-4-5-6-7.dtbo \
meson-g12a-gpio-poweroff.dtbo

scr-$(CONFIG_ARCH_MESON) += \
meson-fixup.scr
Expand Down
22 changes: 22 additions & 0 deletions arch/arm64/boot/dts/amlogic/overlay/meson-g12a-gpio-poweroff.dts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/dts-v1/;
/plugin/;

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/gpio/meson-g12a-gpio.h>

/ {
compatible = "radxa,zero", "amlogic,g12a";

fragment@0 {
target-path = "/";
__overlay__ {
gpio-poweroff {
compatible = "gpio-poweroff";
/* Sets poweroff pin to GPIOAO_9 as an example. Modify as needed. */
gpios = <&gpio_ao GPIOAO_9 GPIO_ACTIVE_LOW>;
timeout-ms = <20000>;
force;
};
};
};
};
20 changes: 18 additions & 2 deletions drivers/power/reset/gpio-poweroff.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ static struct gpio_desc *reset_gpio;
static u32 timeout = DEFAULT_TIMEOUT_MS;
static u32 active_delay = 100;
static u32 inactive_delay = 100;
static void (*old_power_off)(void);

static void gpio_poweroff_do_poweroff(void)
{
Expand All @@ -43,16 +44,22 @@ static void gpio_poweroff_do_poweroff(void)
/* give it some time */
mdelay(timeout);

if (old_power_off)
old_power_off();

WARN_ON(1);
}

static int gpio_poweroff_probe(struct platform_device *pdev)
{
bool input = false;
enum gpiod_flags flags;
bool force = false;
bool export = false;

/* If a pm_power_off function has already been added, leave it alone */
if (pm_power_off != NULL) {
force = of_property_read_bool(pdev->dev.of_node, "force");
if (!force && (pm_power_off != NULL)) {
dev_err(&pdev->dev,
"%s: pm_power_off function already registered\n",
__func__);
Expand All @@ -74,14 +81,23 @@ static int gpio_poweroff_probe(struct platform_device *pdev)
if (IS_ERR(reset_gpio))
return PTR_ERR(reset_gpio);

export = of_property_read_bool(pdev->dev.of_node, "export");
if (export) {
gpiod_export(reset_gpio, false);
gpiod_export_link(&pdev->dev, "poweroff-gpio", reset_gpio);
}

old_power_off = pm_power_off;
pm_power_off = &gpio_poweroff_do_poweroff;
return 0;
}

static int gpio_poweroff_remove(struct platform_device *pdev)
{
if (pm_power_off == &gpio_poweroff_do_poweroff)
pm_power_off = NULL;
pm_power_off = old_power_off;

gpiod_unexport(reset_gpio);

return 0;
}
Expand Down

0 comments on commit e80c9d3

Please sign in to comment.