shibata
Posts: 4
Joined: Sat Jul 11, 2015 2:04 am

gpio_keys device tree overlay

Sat Jul 11, 2015 2:55 am

I'm trying to setup gpio_keys to trigger keyboard events on GPIO activity. The desired behavior is using a GPIO pin to send a shutdown key code and halt the system. I know there are other ways of doing that and I'm currently using a simple bash script to read the GPIO, but since there is a gpio_keys driver ready to use and with better integration with the kernel I'm willing to move to it.

I can't get the device tree setup to work. My configuration was based on http://lists.infradead.org/pipermail/li ... 58315.html , https://www.kernel.org/doc/Documentatio ... o-keys.txt and http://developer.toradex.com/knowledge- ... O_shutdown . I tried options from all these sources. Finally I decided to abandon overlays and try compiling bt-blob.dts modified to be sure my changes were loaded, but no luck. Trying to generate other keycodes didn't help either. The code I am testing:

Code: Select all

gpio-keys {
    compatible = "gpio-keys";
        newline {
            // also tested with other options
            label = "Newline";
            gpios = <&gpio0 21 0>;
            linux,key-code = <13>;
        };
};
I'm loading the gpio_keys module, but tried with gpio_keys_polled too. I believe the GPIO isn't even being setup, since /sys/kernel/debug/gpio gives me:

Code: Select all

GPIOs 0-53, bcm2708_gpio:
 gpio-47  (led0                ) out lo
zcat /proc/config.gz | grep -i gpio gives me:

Code: Select all

CONFIG_BCM2708_GPIO=y
CONFIG_ARCH_NR_GPIO=0
CONFIG_RFKILL_GPIO=m
# CONFIG_MDIO_GPIO is not set
# CONFIG_MDIO_BUS_MUX_GPIO is not set
CONFIG_KEYBOARD_GPIO=m
CONFIG_KEYBOARD_GPIO_POLLED=m
# CONFIG_INPUT_GPIO_BEEPER is not set
CONFIG_INPUT_GPIO_TILT_POLLED=m
CONFIG_INPUT_GPIO_ROTARY_ENCODER=m
# CONFIG_I2C_ARB_GPIO_CHALLENGE is not set
CONFIG_I2C_MUX_GPIO=m
# CONFIG_I2C_CBUS_GPIO is not set
CONFIG_I2C_GPIO=y
CONFIG_SPI_GPIO=m
CONFIG_PPS_CLIENT_GPIO=m
CONFIG_ARCH_HAVE_CUSTOM_GPIO_H=y
CONFIG_ARCH_REQUIRE_GPIOLIB=y
CONFIG_GPIOLIB=y
CONFIG_GPIO_DEVRES=y
CONFIG_OF_GPIO=y
CONFIG_GPIOLIB_IRQCHIP=y
# CONFIG_DEBUG_GPIO is not set
CONFIG_GPIO_SYSFS=y
# Memory mapped GPIO drivers:
# CONFIG_GPIO_74XX_MMIO is not set
# CONFIG_GPIO_GENERIC_PLATFORM is not set
# CONFIG_GPIO_DWAPB is not set
# CONFIG_GPIO_EM is not set
# CONFIG_GPIO_ZEVIO is not set
# CONFIG_GPIO_PL061 is not set
CONFIG_GPIO_SCH311X=m
# CONFIG_GPIO_GRGPIO is not set
# I2C GPIO expanders:
CONFIG_GPIO_ARIZONA=m
# CONFIG_GPIO_MAX7300 is not set
# CONFIG_GPIO_MAX732X is not set
# CONFIG_GPIO_PCA953X is not set
# CONFIG_GPIO_PCF857X is not set
# CONFIG_GPIO_SX150X is not set
CONFIG_GPIO_STMPE=y
# CONFIG_GPIO_ADP5588 is not set
# CONFIG_GPIO_ADNP is not set
# PCI GPIO expanders:
# SPI GPIO expanders:
# CONFIG_GPIO_MAX7301 is not set
# CONFIG_GPIO_MCP23S08 is not set
# CONFIG_GPIO_MC33880 is not set
# CONFIG_GPIO_74X164 is not set
# AC97 GPIO expanders:
# LPC GPIO expanders:
# MODULbus GPIO expanders:
# USB GPIO expanders:
CONFIG_W1_MASTER_GPIO=m
# CONFIG_CHARGER_GPIO is not set
CONFIG_POWER_RESET_GPIO=y
# CONFIG_POWER_RESET_GPIO_RESTART is not set
# CONFIG_SENSORS_GPIO_FAN is not set
# CONFIG_GPIO_WATCHDOG is not set
# CONFIG_SSB_DRIVER_GPIO is not set
# CONFIG_BCMA_DRIVER_GPIO is not set
# CONFIG_HTC_EGPIO is not set
CONFIG_IR_GPIO_CIR=m
CONFIG_BACKLIGHT_GPIO=m
# CONFIG_USB_GPIO_VBUS is not set
CONFIG_LEDS_GPIO=y
CONFIG_LEDS_TRIGGER_GPIO=y
# CONFIG_EXTCON_GPIO is not set
Also, I'm running Arch here. Am I missing something? Can someone with more device tree experience help?

Thanks in advance

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1273
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: gpio_keys device tree overlay

Sat Jul 11, 2015 12:54 pm

If you look here you will see that the label for the gpio node is called "gpio", so it should be referred to using "&gpio" (not "&gpio0"). If you try:

Code: Select all

sudo vcdbg log msg
you should find some error messages because the loader can't locate the "gpio0" label.

Everything else looks plausible, so you may find that deleting the "0" is all you need to do.

FYI you can examine the active device tree in /proc/device-tree. Navigating through all of the directories and hexdumping binary files is a bit tedious, but there is a better way:

Code: Select all

dtc -I fs /proc/device-tree
will give you a decompiled version. It will have lost the labels and gained some phandles, and some properties may have become an array of hex bytes, but it isn't too hard to read.

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 4568
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: gpio_keys device tree overlay

Sat Jul 11, 2015 1:31 pm

Don't know if this overlay is 100% cromulent, but it works for me:

Code: Select all

/dts-v1/;
/plugin/;
/ {
	compatible = "brcm,bcm2835", "brcm,bcm2708", "brcm,bcm2709";
	
	fragment@0 {
		target-path = "/soc";
		__overlay__ {
			keypad: keypad {
				compatible = "gpio-keys";
				#address-cells = <1>;
				#size-cells = <0>;
				button@13 {
					label = "Test BTN0";
					linux,code = <0x100>;
					gpios = <&gpio 13 1>;
				};
				button@16 {
					label = "Test BTN1";
					linux,code = <0x101>;
					gpios = <&gpio 16 1>;
				};
				button@19 {
					label = "Test BTN2";
					linux,code = <0x102>;
					gpios = <&gpio 19 1>;
				};
				button@20 {
					label = "Test BTN3";
					linux,code = <0x103>;
					gpios = <&gpio 20 1>;
				};
				button@21 {
					label = "Test BTN4";
					linux,code = <0x104>;
					gpios = <&gpio 21 1>;
				};
				button@26 {
					label = "Test BTN5";
					linux,code = <0x105>;
					gpios = <&gpio 26 1>;
				};
			};
		};
	};
};

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1273
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: gpio_keys device tree overlay

Sat Jul 11, 2015 1:56 pm

Totally cromulent.

It's good of you to embiggen the example.

shibata
Posts: 4
Joined: Sat Jul 11, 2015 2:04 am

Re: gpio_keys device tree overlay

Sun Jul 12, 2015 4:29 am

Thanks for the help. At first I though it still wasn't working because I was using showkey to test the buttons, which wasn't giving any output, but I saw gpio_keys automatically loaded and cat /dev/input/event0 gave me data. Any idea why keys won't appear on terminal/applications?

EDIT: Nevermind, I mistakenly chose RIGHTCTRL as the GPIO key.

doubleodoug
Posts: 3
Joined: Sat Sep 05, 2015 1:08 am

Re: gpio_keys device tree overlay

Sat Sep 05, 2015 1:10 am

Did this get figured out? I've got the switch working and it's recognized as XF86PowerOff, same as a power button on a USB keyboard, but linux doesn't actually shutdown when this key is pressed and I have no idea how to turn that on. X11 will if configured to, but this doesn't help unless the X server is running. The udev rule at http://developer.toradex.com/knowledge- ... O_shutdown doesn't seem to work.

doubleodoug
Posts: 3
Joined: Sat Sep 05, 2015 1:08 am

Re: gpio_keys device tree overlay

Sat Sep 05, 2015 6:11 pm

Nevermind, the systemd maintainers added this ability to the latest version. http://lists.freedesktop.org/archives/s ... 33800.html. This should filter down to raspbian eventually.

shibata
Posts: 4
Joined: Sat Jul 11, 2015 2:04 am

Re: gpio_keys device tree overlay

Sun Sep 06, 2015 3:20 am

You will have to edit ID_PATH to match your ID. On mine,

Code: Select all

SUBSYSTEM=="input", KERNEL=="event*", ENV{ID_PATH}=="platform-soc:keypad", ATTRS{keys}=="*116*", TAG+="power-switch"
works.

Return to “Device Tree”

Who is online

Users browsing this forum: tytyty and 2 guests