[GUIDE] Turn LID GPIO 27 into a standard input device sending lid switch events

Hi folks!

I was bothered by the fact that I had to use a proprietary daemon to use a standard device like a lid switch on my One Up, so I have created this overlay for a gpio-based input device.

Now I can simply use standard tools to react to the state of the shell’s lid.

Here’s my overlay’s dts:

[root@argon-oneup overlays]$ cat lid.dts 
/dts-v1/;
/plugin/;

/ {
    compatible = "brcm,bcm2712";

    fragment@0 {
        target = <&rp1_gpio>;
        __overlay__ {
            lid_switch_pins: lid_switch_pins {
                pins = "gpio27";
                function = "gpio";
                bias-pull-up;
                input-enable;
                input-schmitt-enable;
            };
        };
    };

    fragment@1 {
        target-path = "/";
        __overlay__ {
            lid_switch {
                pinctrl-names = "default";
                pinctrl-0 = <&lid_switch_pins>;
                compatible = "gpio-keys";
                status = "okay";

                lid {
                    label = "lid_switch";
                    phandle = <0x61>;
                    linux,input-type = <0x05>;   // EV_SW
                    linux,code = <0x00>;         // SW_LID
                    debounce-interval = <0x32>;
                    gpios = <&rp1_gpio 27 1>;    // 1 = GPIO_ACTIVE_LOW (invert)
                };
            };
        };
    };
};
[root@argon-oneup overlays]$

Just like always simply compile with dtc -@ -I dts -O dtb -o lid.dtbo lid.dts and apply the overlay in your config.txt.

This will result in your lid switch showing up as a separate input device like this:

[root@argon-oneup ~]# libinput list-devices

Device:                  lid_switch
Kernel:                  /dev/input/event0
Id:                      host:0001:0001
Group:                   4
Seat:                    seat0, default
Capabilities:            switch
Tap-to-click:            n/a
Tap-and-drag:            n/a
Tap button map:          n/a
Tap drag lock:           n/a
Left-handed:             n/a
Nat.scrolling:           n/a
Middle emulation:        n/a
Calibration:             n/a
Scroll methods:          none
Scroll button:           n/a
Scroll button lock:      n/a
Click methods:           none
Clickfinger button map:  n/a
Disable-w-typing:        n/a
Disable-w-trackpointing: n/a
Accel profiles:          n/a
Rotation:                0.0
Area rectangle:          n/a

[root@argon-oneup ~]#

and it’ll send lid switch events just like any other standard lid out there does:

[root@argon-oneup ~]# libinput debug-events --device /dev/input/event0

-event0   DEVICE_ADDED                 lid_switch                        seat0 default group1  cap:S
 event0   SWITCH_TOGGLE                +0.000s	switch lid state 1
 event0   SWITCH_TOGGLE                +2.500s	switch lid state 0

[root@argon-oneup ~]# 

Now you can set up systemd-logind or your favourite desktop environment to handle the lid switch as you’re used to.

Hope this’ll help anyone.

3 Likes

Great, thanks for the guide!

Before I try it out, do I need to disable any specific services? I currently have the Argon script installed, but I’ve kept argononeupd.service deactivated to maintain compatibility with Jeff’s battery driver.

No, there’s nothing that you need to disable. The argononeupd.service (that you aren’t using anyway) won’t be able to to grab the gpio pin since the driver has a lock on it already, but that’s all.

I am also waiting for Jeff’s battery driver to mature a bit. Looks promising.

This is awesome, thanks!