Skip to content

Commit 83f765b

Browse files
matthijskooijmanpopcornmix
authored andcommitted
overlays: Add gpio-shutdown overlay (#2103)
This overlay facilitates the addition of a powerbutton by converting GPIO edges into KEY_POWER keypresses, which can be handled by systemd-logind to shut down the system. Signed-off-by: Matthijs Kooijman <[email protected]>
1 parent a92b44a commit 83f765b

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

arch/arm/boot/dts/overlays/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dtbo-$(CONFIG_ARCH_BCM2835) += \
3030
googlevoicehat-soundcard.dtbo \
3131
gpio-ir.dtbo \
3232
gpio-poweroff.dtbo \
33+
gpio-shutdown.dtbo \
3334
hifiberry-amp.dtbo \
3435
hifiberry-dac.dtbo \
3536
hifiberry-dacplus.dtbo \

arch/arm/boot/dts/overlays/README

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,38 @@ Params: gpiopin GPIO for signalling (default 26)
508508
will also cause the pin to go low.
509509

510510

511+
Name: gpio-shutdown
512+
Info: Initiates a shutdown when GPIO pin changes. The given GPIO pin
513+
is configured as an input key that generates KEY_POWER events.
514+
This event is handled by systemd-logind by initiating a
515+
shutdown. Systemd versions older than 225 need an udev rule
516+
enable listening to the input device:
517+
518+
ACTION!="REMOVE", SUBSYSTEM=="input", KERNEL=="event*", \
519+
SUBSYSTEMS=="platform", DRIVERS=="gpio-keys", \
520+
ATTRS{keys}=="116", TAG+="power-switch"
521+
522+
This overlay only handles shutdown. After shutdown, the system
523+
can be powered up again by driving GPIO3 low. The default
524+
configuration uses GPIO3 with a pullup, so if you connect a
525+
button between GPIO3 and GND (pin 5 and 6 on the 40-pin header),
526+
you get a shutdown and power-up button.
527+
Load: dtoverlay=gpio-shutdown,<param>=<val>
528+
Params: gpio_pin GPIO pin to trigger on (default 3)
529+
530+
active_low When this is 1 (active low), a falling
531+
edge generates a key down event and a
532+
rising edge generates a key up event.
533+
When this is 0 (active high), this is
534+
reversed. The default is 1 (active low).
535+
536+
gpio_pull Desired pull-up/down state (off, down, up)
537+
Default is "up".
538+
539+
Note that the default pin (GPIO3) has an
540+
external pullup.
541+
542+
511543
Name: hifiberry-amp
512544
Info: Configures the HifiBerry Amp and Amp+ audio cards
513545
Load: dtoverlay=hifiberry-amp
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Definitions for gpio-poweroff module
2+
/dts-v1/;
3+
/plugin/;
4+
5+
// This overlay sets up an input device that generates KEY_POWER events
6+
// when a given GPIO pin changes. It defaults to using GPIO3, which can
7+
// also be used to wake up (start) the Rpi again after shutdown. Since
8+
// wakeup is active-low, this defaults to active-low with a pullup
9+
// enabled, but all of this can be changed using overlay parameters (but
10+
// note that GPIO3 has an external pullup on at least some boards).
11+
12+
/ {
13+
compatible = "brcm,bcm2708";
14+
15+
fragment@0 {
16+
// Configure the gpio pin controller
17+
target = <&gpio>;
18+
__overlay__ {
19+
// Define a pinctrl state, that sets up the gpio
20+
// as an input with a pullup enabled. This does
21+
// not take effect by itself, only when referenced
22+
// by a "pinctrl client", as is done below. See:
23+
// https://www.kernel.org/doc/Documentation/devicetree/bindings/pinctrl/pinctrl-bindings.txt
24+
// https://www.kernel.org/doc/Documentation/devicetree/bindings/pinctrl/brcm,bcm2835-gpio.txt
25+
pin_state: shutdown_button_pins {
26+
brcm,pins = <3>; // gpio number
27+
brcm,function = <0>; // 0 = input, 1 = output
28+
brcm,pull = <2>; // 0 = none, 1 = pull down, 2 = pull up
29+
};
30+
};
31+
};
32+
fragment@1 {
33+
// Add a new device to the /soc devicetree node
34+
target-path = "/soc";
35+
__overlay__ {
36+
shutdown_button {
37+
// Let the gpio-keys driver handle this device. See:
38+
// https://www.kernel.org/doc/Documentation/devicetree/bindings/input/gpio-keys.txt
39+
compatible = "gpio-keys";
40+
41+
// Declare a single pinctrl state (referencing the one declared above) and name it
42+
// default, so it is activated automatically.
43+
pinctrl-names = "default";
44+
pinctrl-0 = <&pin_state>;
45+
46+
// Enable this device
47+
status = "okay";
48+
49+
// Define a single key, called "shutdown" that monitors the gpio and sends KEY_POWER
50+
// (keycode 116, see
51+
// https://github.com/torvalds/linux/blob/v4.12/include/uapi/linux/input-event-codes.h#L190)
52+
button: shutdown {
53+
label = "shutdown";
54+
linux,code = <116>; // KEY_POWER
55+
gpios = <&gpio 3 1>;
56+
};
57+
};
58+
};
59+
};
60+
61+
// This defines parameters that can be specified when loading
62+
// the overlay. Each foo = line specifies one parameter, named
63+
// foo. The rest of the specification gives properties where the
64+
// parameter value is inserted into (changing the values above
65+
// or adding new ones).
66+
__overrides__ {
67+
// Allow overriding the GPIO number.
68+
gpio_pin = <&button>,"gpios:4",
69+
<&pin_state>,"brcm,pins:0";
70+
71+
// Allow changing the internal pullup/down state. 0 = none, 1 = pulldown, 2 = pullup
72+
// Note that GPIO3 and GPIO2 are the I2c pins and have an external pullup (at least
73+
// on some boards).
74+
gpio_pull = <&pin_state>,"brcm,pull:0";
75+
76+
// Allow setting the active_low flag. 0 = active high, 1 = active low
77+
active_low = <&button>,"gpios:8";
78+
};
79+
80+
};

0 commit comments

Comments
 (0)