</>EmbeddedInterview

What is the device tree and why is it used?

MediumLinux Kernel & Device Drivers

Short answer: The device tree is a data structure that describes hardware that cannot be discovered at run time: CPUs, memory, peripherals, their addresses, interrupts, clocks, GPIOs, regulators and pin configuration. Source files (.dts/.dtsi) are compiled by dtc into a binary blob (.dtb) that the bootloader passes to the kernel. One kernel image can then support many boards, because board details live in data instead of C code.

/ {
    model = "Acme Board";
    compatible = "acme,board", "st,stm32mp157";

    memory@c0000000 { device_type = "memory"; reg = <0xc0000000 0x20000000>; };

    leds {
        compatible = "gpio-leds";
        status_led: led-0 { gpios = <&gpioa 13 GPIO_ACTIVE_LOW>; linux,default-trigger = "heartbeat"; };
    };
};

&i2c1 {
    pinctrl-names = "default";
    pinctrl-0 = <&i2c1_pins>;
    clock-frequency = <400000>;
    status = "okay";

    temp@48 { compatible = "ti,tmp102"; reg = <0x48>; };
};

Key concepts

Concept Meaning
compatible most specific first, e.g. "acme,board-v2", "acme,board"; drivers match on it
reg address and size, interpreted with the parent's #address-cells / #size-cells
interrupts, interrupt-parent which interrupt controller line and trigger type
phandles (&label) references to other nodes: clocks, GPIO controllers, regulators, pinctrl states
status = "okay"/"disabled" enable/disable a node, commonly in board .dts files
.dtsi includes SoC description shared by many boards; the board file overrides nodes with &label { ... }

Bindings: Documentation/devicetree/bindings/ contains YAML schemas that define which properties each compatible requires; make dt_binding_check and make dtbs_check validate them.

At run time: /sys/firmware/devicetree/base/ (or /proc/device-tree) shows the tree the kernel received; dtc -I fs /sys/firmware/devicetree/base decompiles it, which is very useful when debugging which properties actually reached the kernel.

In drivers: of_property_read_u32(), device_property_read_u32() (works for DT and ACPI), devm_gpiod_get(), devm_clk_get(), devm_regulator_get().

ACPI is the equivalent on x86 and ARM servers.