GPIO Driver with IRQ (Platform Driver)¶
This note summarizes how to implement a GPIO driver with interrupt support using the Linux platform driver model and Device Tree.
1. Architecture Overview¶
User Space ↓ /dev/mygpio (char device) ↓ Platform Driver ↓ GPIO subsystem (gpiod) ↓ Hardware (LED / Button)
2. Device Tree Mapping¶
Example:
0= active high1= active low
Driver mapping:
devm_gpiod_get(dev, "led1", GPIOD_OUT_LOW);
devm_gpiod_get(dev, "led2", GPIOD_OUT_LOW);
devm_gpiod_get(dev, "btn", GPIOD_IN);
3. devm_ Managed Resource¶
Manual (traditional)¶
devm_ version¶
- Automatically released when driver is removed
- Cleaner and safer
4. IRQ Flow¶
Step 1: Convert GPIO → IRQ¶
Step 2: Register IRQ¶
devm_request_irq(dev,
irq,
handler,
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
"mygpio_btn",
data);
Step 3: ISR (Interrupt Service Routine)¶
static irqreturn_t mygpio_btn_isr(int irq, void *data)
{
int value = gpiod_get_value(...);
// Update state
mydev->btn_state = value;
return IRQ_HANDLED;
}
5. IRQ Design Principles¶
DO:¶
- Keep ISR short
- Update state
- Trigger event
DON'T:¶
- sleep
- use mutex
- heavy computation
- excessive logging
6. Logging in IRQ Context¶
Allowed:
But NOT recommended for production: - increases latency - may flood logs (bounce) - may cause contention
Better:
7. Character Device Interface¶
Write¶
Read¶
8. Permission Issue¶
Problem:
Solution:
or:
9. Common Pitfalls¶
Missing header¶
Wrong format specifier¶
Shell redirect issue¶
10. Button Bounce¶
Observed: - Multiple IRQ triggers per press
Reason: - Mechanical switch bounce
Solutions (later): - jiffies filter - timer - workqueue
11. Summary¶
This driver demonstrates:
- Platform driver + Device Tree
- Multiple GPIO handling
- Interrupt-based input
- Character device interface
- Basic driver design principles
This is a complete minimal Linux driver architecture.