Write an Input Driver for Buttons and Keys
Write a Linux 6.12 input driver for a GPIO button: devm_input_allocate_device, capability bits, a threaded IRQ, debounce, and events visible to evtest.

A button on a board, a key on a custom keypad, a rotary encoder, a lid switch: all of them are input devices, and the Linux input subsystem is the layer that gives them a common face. A driver that reports key events through it works without any special support in applications, because the desktop, the console and evdev programs already know how to read events from /dev/input/eventN. This post writes a driver for one GPIO button on Linux 6.12: it allocates an input device, declares what it can report, turns each edge into an event and handles debounce. It builds on the GPIO descriptor post.
The subsystem is documented in the kernel's input programming guide and event codes.
When not to write this
Linux already has gpio-keys, a generic driver that turns any GPIO into a key from the device tree, with debounce, long-press and wake-up support. For one button on a board, the right answer is almost always to describe it in the device tree and use that driver:
buttons {
compatible = "gpio-keys";
button-enter {
label = "Enter";
linux,code = <28>;
gpios = <&gpio1 4 GPIO_ACTIVE_LOW>;
debounce-interval = <20>;
};
};Writing a driver is justified when the button belongs to a larger device, such as a chip that also does something else, or when the event logic goes beyond a plain press and release. The example below does the work by hand so you can see how the pieces fit, and what gpio-keys is doing for you.
The pieces of an input driver
An input driver does four things:
Allocate a
struct input_dev.Declare which events it can produce, with
input_set_capability()or by setting bits.Register it with
input_register_device().Report events with
input_report_key()and friends, and mark the end of a group withinput_sync().
The capability declaration is not optional. User space reads it to decide what the device is, and the core drops any event of a type or code you did not declare.
The driver
#include <linux/gpio/consumer.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/platform_device.h>
struct sample_button {
struct input_dev *input;
struct gpio_desc *gpio;
};
static irqreturn_t sample_button_irq(int irq, void *data)
{
struct sample_button *b = data;
int pressed = gpiod_get_value_cansleep(b->gpio);
input_report_key(b->input, KEY_ENTER, pressed);
input_sync(b->input);
return IRQ_HANDLED;
}
static int sample_button_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct sample_button *b;
int irq, ret;
b = devm_kzalloc(dev, sizeof(*b), GFP_KERNEL);
if (!b)
return -ENOMEM;
b->gpio = devm_gpiod_get(dev, "button", GPIOD_IN);
if (IS_ERR(b->gpio))
return dev_err_probe(dev, PTR_ERR(b->gpio), "no button line\n");
gpiod_set_debounce(b->gpio, 20000); /* microseconds, if supported */
b->input = devm_input_allocate_device(dev);
if (!b->input)
return -ENOMEM;
b->input->name = "sample-button";
b->input->phys = "sample-button/input0";
b->input->id.bustype = BUS_HOST;
input_set_capability(b->input, EV_KEY, KEY_ENTER);
ret = input_register_device(b->input);
if (ret)
return ret;
irq = gpiod_to_irq(b->gpio);
if (irq < 0)
return irq;
ret = devm_request_threaded_irq(dev, irq, NULL, sample_button_irq,
IRQF_TRIGGER_RISING |
IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
"sample-button", b);
if (ret)
return ret;
dev_info(dev, "registered as %s\n", dev_name(&b->input->dev));
return 0;
}
static const struct of_device_id sample_button_of_match[] = {
{ .compatible = "example,sample-button" },
{ }
};
MODULE_DEVICE_TABLE(of, sample_button_of_match);
static struct platform_driver sample_button_driver = {
.probe = sample_button_probe,
.driver = {
.name = "sample-button",
.of_match_table = sample_button_of_match,
},
};
module_platform_driver(sample_button_driver);
MODULE_DESCRIPTION("GPIO button input driver");
MODULE_LICENSE("GPL");Because the GPIO is declared with GPIO_ACTIVE_LOW in the device tree, gpiod_get_value_cansleep() returns the logical level: 1 while the button is held, 0 after release. That is why the handler passes it straight to input_report_key(), which wants 1 for pressed and 0 for released. The polarity is firmware's concern, not the driver's.
Why a threaded interrupt
The handler reads the line with gpiod_get_value_cansleep(), which may sleep when the GPIO controller sits behind a slow bus. A normal interrupt handler cannot sleep. A threaded handler runs in process context, so the call is legal. The registration passes NULL as the hard handler and the function as the thread function, and IRQF_ONESHOT keeps the interrupt masked until the thread finishes, which a level-triggered or threaded-only registration requires.
For a button wired to a fast memory-mapped controller you could read in a hard handler, but the threaded form is correct in every case and costs little for something as slow as a person's finger.
Debounce
A mechanical switch bounces: the contact makes and breaks several times over a few milliseconds before settling. Without handling it, one press produces a burst of events. There are three layers of defense. gpiod_set_debounce() asks the GPIO controller to filter, which works only on hardware that supports it and returns an error otherwise. The same value can come from a debounce-interval in the device tree. And a driver can do it in software: on each interrupt, schedule a delayed work item a few milliseconds later and read the settled state there, ignoring the interrupts in between.
The gpio-keys driver uses the software approach with a timer when the hardware cannot filter. A hand-written driver that reads the pin on every edge, with no debounce at all, reports the bounces as extra presses.
What user space sees
Once registered, the driver creates a file under /dev/input/. The standard tool for watching it is evtest:
sudo evtest /dev/input/event3Input driver version is 1.0.1
Input device ID: bus 0x19 vendor 0x0 product 0x0 version 0x0
Input device name: "sample-button"
Supported events:
Event type 0 (EV_SYN)
Event type 1 (EV_KEY)
Event code 28 (KEY_ENTER)
Testing ... (interrupt to exit)
Event: time 1735891200.412345, type 1 (EV_KEY), code 28 (KEY_ENTER), value 1
Event: time 1735891200.412345, -------------- SYN_REPORT ------------
Event: time 1735891200.563210, type 1 (EV_KEY), code 28 (KEY_ENTER), value 0
Event: time 1735891200.563210, -------------- SYN_REPORT ------------The Supported events list is the capability declaration. Each press is a pair of events, value 1 for press and 0 for release, each followed by a SYN_REPORT from input_sync(). Applications act on the report, so omitting input_sync() makes events appear to hang until the next report arrives.
Key codes come from linux/input-event-codes.h. KEY_ENTER is 28, which is the number gpio-keys takes in linux,code. Choose a code that matches the meaning of the button: KEY_POWER for a power button, KEY_VOLUMEUP for volume, or BTN_0 for a button with no standard meaning. Desktop environments bind actions to these codes, so a wrong choice triggers the wrong behavior.
Wake-up and other flags
A button that should wake the system from suspend needs two more things: the interrupt marked as a wake source, and the device allowed to wake. In a driver that is device_init_wakeup(dev, true) in probe and enable_irq_wake(irq) in the suspend callback. The gpio-keys driver handles both from a wakeup-source device tree property, which is one more reason to use it where it fits.
For auto-repeat of a held key, set the EV_REP bit, and the input core generates repeat events for you with input_set_capability(input, EV_REP, ...) or set_bit(EV_REP, input->evbit). Most hardware buttons should not repeat, so the example leaves it off.
Testing without the button
Two checks catch most mistakes before any wiring is involved. First, read /proc/bus/input/devices and confirm your device appears with the name and the EV= capability mask you expect: a value of 3 there means synchronization and key events were declared, and a mask missing the key bit means input_set_capability() was never reached. Second, watch the interrupt counter in /proc/interrupts while pressing: a counter that rises with a press but produces no event in evtest points at the handler or the capability, and a counter that never moves points at the trigger flags, the pin muxing or a controller that does not support edges on that line. Each result isolates one layer, so you never debug the whole chain at once.


