Skip to content
systemcoding.dev
Kernels

Register a platform driver with module_platform_driver

Register a minimal platform driver with module_platform_driver on Linux 6.12, whose remove callback returns void since 6.11.

1 min read
Summarize in AI

A device described in a device tree or by board code becomes a platform_device, and a platform driver binds to it. The macro module_platform_driver writes the module init and exit functions for you.

demo.cc
#include <linux/module.h>
#include <linux/platform_device.h>

static int demo_probe(struct platform_device *pdev)
{
	dev_info(&pdev->dev, "probed\n");
	return 0;
}

static void demo_remove(struct platform_device *pdev)
{
	dev_info(&pdev->dev, "removed\n");
}

static struct platform_driver demo_driver = {
	.probe  = demo_probe,
	.remove = demo_remove,
	.driver = { .name = "demo" },
};
module_platform_driver(demo_driver);
MODULE_LICENSE("GPL");
Text
demo 12340000.demo: probed

The device name prefix is the example from a device-tree node at address 12340000. Matching here is by the .driver.name string, so use a device tree table, as the next rows show, for real hardware.

Since Linux 6.11 the remove callback returns void. Before that it returned int, and a transitional remove_new member existed; code that must build on both needs a version guard. A returned error never stopped the removal anyway. Keep probe free of side effects that remove cannot undo. See the platform device documentation.

Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.