Skip to content

Day 73 - Platform Driver Development

Today's Goal

Understand how a platform driver acquires hardware resources, stores private driver state, and manages resource lifetime using devm APIs.

Topics:

  • struct resource
  • platform_get_resource()
  • IORESOURCE_MEM
  • IORESOURCE_IRQ
  • platform_get_irq()
  • Driver private data
  • platform_set_drvdata()
  • platform_get_drvdata()
  • devm resource management
  • devm_request_irq()
  • delayed_work debounce

What I Learned

Platform Resources

A platform driver does not access hardware addresses directly.

Hardware resources are described by Device Tree and converted into platform resources during device creation.

The driver acquires these resources through:

  • platform_get_resource()
  • platform_get_irq()

Common resource types:

  • IORESOURCE_MEM
  • IORESOURCE_IRQ

Driver Private Data

Each platform device instance owns its own private driver state.

Instead of using global variables, Linux drivers allocate instance-specific data:

struct demo_priv {
    struct resource *mem_res;
    int irq;
};

The private data is attached to the platform device:

platform_set_drvdata()

and retrieved later through:

platform_get_drvdata()

IRQ Resource Acquisition

Interrupt information is obtained from Device Tree.

The platform driver retrieves the mapped Linux IRQ number using:

platform_get_irq()

The returned IRQ number may differ from the interrupt specifier defined in Device Tree because irqdomain mapping occurs inside the kernel.


Managed Resource Allocation

The devm framework simplifies resource lifetime management.

Examples:

  • devm_kzalloc()
  • devm_request_irq()

Resources allocated through devm APIs are automatically released when the device is removed.


Delayed Work Debounce

A GPIO interrupt may generate multiple triggers due to signal bouncing.

A common Linux driver solution is:

IRQ
mod_delayed_work()
Debounce timeout
Worker execution

Using mod_delayed_work() automatically refreshes the debounce timeout when additional interrupts arrive.


Lab Summary

Implemented a platform driver using a Device Tree Overlay.

Verified:

  • platform_get_resource()
  • platform_get_irq()
  • platform_set_drvdata()
  • platform_get_drvdata()
  • devm_request_irq()

Implemented:

  • GPIO23 interrupt source
  • delayed_work debounce
  • interrupt event counter

Key Observations

Device Tree Resource Flow

Device Tree
platform_device
resource[]
platform_get_resource()

IRQ Flow

GPIO23
rp1_gpio irqdomain
platform_get_irq()
Linux IRQ number

Driver State Flow

probe()
devm_kzalloc()
platform_set_drvdata()
platform_get_drvdata()

Result

Successfully verified:

  • Platform resource acquisition
  • Platform IRQ acquisition
  • Driver private data management
  • Managed IRQ registration
  • Delayed work debounce mechanism

This completes the fundamental platform driver initialization flow.


Next Step

Day 74:

  • devm_ioremap_resource()
  • readl()
  • writel()
  • Memory-mapped I/O
  • Platform Driver Register Access