Implementing Platform Drivers in the Kernel
In this lesson, you will learn how the Linux kernel uses platform drivers to bridge the gap between static Device Tree hardware descriptions and functional kernel software.
In the world of embedded Linux, the kernel doesn't "know" your hardware exists until you tell it. While the Device Tree (DT) provides the blueprint (describing memory addresses, IRQs, and compatible strings), it is the platform driver that provides the logic to drive that hardware.
The Matching Mechanism
The kernel maintains a list of registered drivers. When the system boots, it parses the Device Tree and creates "platform devices" for every node. The kernel then performs a "marriage" process: it compares the compatible string defined in your DT node against the of_match_table inside your driver code. If they match, the kernel calls your driver's probe function.
A Minimal Implementation
A platform driver is essentially a structure that defines how to bind to hardware and what to do once connected.
static const struct of_device_id my_device_dt_ids[] = {
{ .compatible = "vendor,my-peripheral" },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, my_device_dt_ids);
static int my_driver_probe(struct platform_device *pdev) {
dev_info(&pdev->dev, "Device probed successfully!\n");
return 0;
}
static struct platform_driver my_driver = {
.probe = my_driver_probe,
.driver = {
.name = "my-driver",
.of_match_table = my_device_dt_ids,
},
};
module_platform_driver(my_driver);
The Role of the Probe Function
When your probe function executes, the hardware is ready to be initialized. This is where you would map registers, request GPIOs, or register character device interfaces. By offloading these tasks to the driver, you keep your system modular—you can swap hardware versions simply by updating the Device Tree, while your core driver logic remains untouched.
If the probe function returns a non-zero value, the kernel assumes the device could not be initialized, effectively ignoring that hardware node. This is a critical debugging point when your hardware doesn't appear to be active.
Key Takeaway: The platform driver acts as the bridge between the Device Tree’s hardware description and the kernel’s functional logic, triggering the probe function only when a compatible hardware node is identified at runtime.