Day 13 - Debounce + Bottom Half (Workqueue / Timer)¶
Overview¶
In this chapter, we improve the GPIO interrupt driver by introducing:
- Software debounce using delayed work
- Proper top half / bottom half design
- Event-driven architecture with stable input
Why Debounce is Needed¶
Mechanical buttons do not produce clean transitions.
Instead of:
We often get:
This is called bounce.
Design Approach¶
We implement debounce using:
IRQ (top half)
↓
schedule delayed work
↓
bottom half (process context)
↓
read stable GPIO value
↓
generate event (if changed)
Top Half vs Bottom Half¶
Top Half (ISR)¶
Responsibilities:
- Triggered by hardware interrupt
- Must be fast and non-blocking
- Cannot sleep
Implementation:
Bottom Half (Delayed Work)¶
Responsibilities:
- Runs in process context
- Can sleep
- Performs debounce and event generation
Key API:
Debounce Logic¶
Core idea:
This ensures:
- No duplicate events
- Only stable transitions are reported
Event Flow¶
Complete pipeline:
GPIO IRQ
→ ISR (top half)
→ delayed work (bottom half)
→ update event state
→ wake_up_interruptible()
→ read() / poll()
→ user space
Logical vs Raw GPIO¶
With Device Tree:
Kernel automatically converts:
| Physical Level | Logical Value |
|---|---|
| LOW | 1 (pressed) |
| HIGH | 0 (released) |
Driver should always expose:
Key Kernel APIs¶
Workqueue¶
Wait Queue¶
Poll¶
Design Guidelines¶
- Keep ISR minimal
- Use bottom half for heavy work
- Always debounce GPIO input
- Expose logical state to user space
- Avoid duplicate events
Summary¶
Day13 transforms the driver into a more realistic Linux design:
- Interrupt-safe
- Debounced
- Event-driven
- User-space friendly
This is a foundation for real input drivers.