One of the most powerful and often misunderstood concepts in Embedded Linux is the Device Tree. It serves as a bridge between the hardware and the software, allowing a single kernel binary to run on multiple different hardware platforms without recompilation. But what exactly is a Device Tree, and how does it function in a real-world system?
The Problem: Hardware Explosion
Before Device Trees were widely adopted (prior to Linux 3.x), hardware information was hard-coded in C files within the kernel's arch/arm/mach-* directories. As the number of ARM-based boards exploded, the kernel became bloated with repetitive platform-specific code. Linus Torvalds famously demanded a change, leading to the adoption of the Device Tree format.
1. Anatomy of a Device Tree
A Device Tree consists of nodes and properties in a tree-like hierarchy. The structure typically includes:
- Nodes: Represent physical or virtual devices (e.g., CPU, I2C bus, LED).
- Properties: Key-value pairs defining device parameters (e.g., memory addresses, interrupt lines, clock frequencies).
- Compatible String: The most important property, used by the kernel to match the device with the correct driver.
/dts-v1/;
/ {
compatible = "bcm2835";
i2c@7e205000 {
compatible = "brcm,bcm2835-i2c";
reg = <0x7e205000 0x1000>;
interrupts = <2 21>;
status = "okay";
sensor@48 {
compatible = "ti,tmp102";
reg = <0x48>;
};
};
};
2. DTS, DTSI, and DTB: What's the Difference?
- DTS (Device Tree Source): The human-readable source file for a specific board.
- DTSI (Device Tree Source Include): Reusable source files containing common hardware definitions (e.g., for a specific SoC).
- DTB (Device Tree Blob): The compiled, binary version of the DTS that is loaded by the bootloader at startup.
- DTC (Device Tree Compiler): The tool used to convert DTS to DTB.
3. Device Tree Overlays: Dynamic Hardware Tuning
Device Tree Overlays (DTO) allow you to modify the system's hardware description without re-compiling the main DTB. This is essential for modular systems like Raspberry Pi or BeagleBone, where HATs or capes can be added at runtime.
/dts-v1/;
/plugin/;
/ {
fragment@0 {
target = <&i2c1>;
__overlay__ {
status = "okay";
};
};
};
4. Debugging and Inspection
When hardware isn't working as expected, the first step is often to inspect the live Device Tree. On a running Linux system, you can explore the tree at /proc/device-tree or use the fdtdump tool to decompile the active blob.
Conclusion
Mastering Device Trees is non-negotiable for any serious Embedded Linux developer. It transforms hardware management from a "black box" into a structured, manageable data format, significantly reducing kernel bloat and development time. At BM Embedded, we leverage complex Device Tree hierarchies to support diverse industrial hardware profiles with minimal software overhead.