Writing a Linux Device Driver: Platform Drivers, Probe and the Driver Model
New Linux driver authors often start with a character device and a hand-written ioctl interface. Most real drivers look quite different. The kernel driver model matches devices, described by the device tree, ACPI or the bus itself, to drivers that declare what they support, and then calls the driver probe function. The driver claims its resources, configures the hardware and registers with a subsystem that already defines the user interface.
At a glance
- Bus
- Each device sits on a bus, such as platform, I2C or USB, that matches it to a driver.
- compatible
- The device tree string that binds a node to a driver through its match table.
- devm
- Managed resource helpers that release everything automatically when the driver detaches.
- Deferral
- Returning EPROBE_DEFER asks the kernel to retry probe once a clock or regulator is ready.
The problem
Drivers written outside this model tend to hard-code addresses and interrupt numbers, invent their own sysfs files and ioctl commands, and clean up by hand on every error path. They work on one board, leak resources when probe fails, and expose interfaces no standard tool understands.
Probe ordering is another trap. A driver that assumes its clock, regulator or GPIO controller is already available will fail intermittently, depending on module load order, unless it uses deferred probing.
Let the driver model and the subsystem do the generic work; the code you write should be about your hardware and nothing else.
Anatomy of probe
A platform driver declares an of_device_id table of compatible strings and a probe function, then registers with a single module_platform_driver line. In probe it obtains its registers, interrupts, clocks and regulators from the device description using managed devm helpers, so every resource is released if probe fails or the device is removed. Errors from resource lookups go through dev_err_probe, which handles deferral cleanly.
The driver then registers with the subsystem for its device class, such as IIO, input or hwmon. The subsystem provides the user interface, so applications and standard tools work without custom ioctls. Callback signatures such as remove change between kernel versions, so follow the current kernel rather than an old example.
What good looks like
Write or find the device tree binding first, with its compatible string and properties, and validate it against the binding schema.
Pick the matching subsystem and model the driver on a recent, well-maintained one there.
Unbind and rebind the driver through sysfs to prove teardown is clean.
From device description to user space
- Description: Device tree or ACPI node
- Match: compatible string meets the match table
- Probe: Claim resources with devm helpers
- Register: Subsystem such as IIO or input
- User space: Standard sysfs and device nodes
| Device type | Subsystem | What users see |
|---|---|---|
| ADC, DAC, IMU, light sensor | IIO | sysfs channels and buffered data |
| Buttons, touch, keypads | input | Event devices read by standard input stacks |
| Temperature, voltage and fan monitors | hwmon | sysfs attributes used by monitoring tools |
| GPIO expanders | GPIO | Lines on the GPIO character device |
| Backlights and LEDs | backlight, LED | sysfs brightness controls |
| Register maps on I2C or SPI | regmap (helper) | Nothing directly; simpler driver code |
What it changes
Board Independence
Resources come from the device description, so one driver serves many boards.
Robust Error Handling
Managed resources remove whole classes of leak and cleanup bugs.
Standard Interfaces
Subsystem registration gives users the tools they already know.
Reviewable Code
Drivers that follow the model are short and easy to review.
Where to go next
Put register access behind regmap where the device sits on I2C or SPI. It adds caching, locking and debugfs access with little code.
If the driver may go upstream, follow kernel coding style from the start. Retrofitting style and structure later is slow.
Problems this prevents
- Hard-coded addresses that tie a driver to one board
- Resource leaks when probe fails halfway
- Intermittent failures from probe ordering
- Custom ioctls where a subsystem already exists
Discuss your project
Email support@aavico.com or use Get in Touch when the website finishes loading.