Skip to content

Day 11 - GPIO Driver with IRQ

๐ŸŽฏ Goal

Extend GPIO driver to support:

  • Multiple LEDs
  • Button input
  • Interrupt handling (IRQ)

๐Ÿ“Œ What I Did

1. Device Tree

Defined GPIO mapping:

LED1 โ†’ GPIO17
LED2 โ†’ GPIO27
BTN  โ†’ GPIO22

Compiled overlay:

dtc -@ -I dts -O dtb -o mygpio-irq.dtbo mygpio-irq.dts

2. Driver Update

Added:

  • led1 / led2 output
  • button input
  • IRQ handling

Key APIs:

devm_gpiod_get()
gpiod_to_irq()
devm_request_irq()

3. ISR Implementation

btn_state = gpiod_get_value(...)

Observed:

  • IRQ triggers on both edges
  • button bounce occurs

4. Debugging

Issue 1: IRQ compile error

Error:

irqreturn_t not found

Fix:

#include <linux/interrupt.h>

Issue 2: DTS compile error

Cause: - GPIO_ACTIVE_LOW not recognized

Fix:

Use numeric flags (0 / 1)


Issue 3: Permission denied

Error:

/dev/mygpio: Permission denied

Fix:

sudo chmod 666 /dev/mygpio

Issue 4: sudo redirect issue

sudo echo ... > /dev/mygpio   โŒ
echo ... | sudo tee /dev/mygpio   โœ…

5. Makefile Improvement

Added:

  • module build
  • overlay build
  • deploy
  • load/unload

๐Ÿงช Test Result

LED Control

echo led1=1 > /dev/mygpio
echo led2=1 > /dev/mygpio
echo all=0 > /dev/mygpio

โœ” OK


Read State

cat /dev/mygpio
โ†’ led1=0,led2=1,btn=0

โœ” OK


IRQ

dmesg -w
  • IRQ triggered on button press
  • Multiple triggers observed (bounce)

โœ” OK


๐Ÿง  Key Learnings

  • Platform driver + Device Tree integration
  • GPIO descriptor API (gpiod)
  • Interrupt handling in kernel
  • ISR design constraints
  • Difference between polling vs interrupt
  • Real-world issue: button bounce

โš ๏ธ Limitations

  • No debounce handling
  • Still polling in userspace
  • IRQ logging not optimized

๐Ÿš€ Next Step

Day 12:

  • wait queue
  • blocking read
  • poll / select support

Goal: ๐Ÿ‘‰ Event-driven driver