Mastering Linux Device Tree: Syntax, Nodes, and OF Functions Explained
This guide introduces Linux Device Tree concepts, explains why it was added to the kernel, details its syntax and standard properties, describes common nodes, and provides practical examples of OF functions for accessing nodes and properties in kernel code.
What Is a Device Tree?
A Device Tree is a hierarchical description of hardware resources used by the Linux kernel. It is written in .dts / .dtsi source files, which are compiled with dtc into binary .dtb files that the kernel loads at boot (typically found under /boot).
Why Linux Introduced Device Tree?
Historically, hardware was described directly in C code via platform_device structures. As the number of supported boards grew, this approach made the kernel bloated and hard to maintain, prompting Linus Torvalds to criticize the situation. Device Tree abstracts hardware description into separate files, reducing kernel size and simplifying the build process.
Device Tree Syntax
Device Tree source follows a C‑like syntax with two main constructs: nodes and properties.
Node format: label: node-name@unit-address { … } (the label is optional and used for later references).
Property format: name = value; where value can be a 32‑bit integer, a 64‑bit pair, a string, a byte array, or a list of mixed values.
Examples:
interrupts = <17 0xc>; clock-frequency = <0x00000001 0x00000000>; compatible = "simple-bus"; local-mac-address = [00 00 12 34 56 78]; compatible = "ns16550", "ns8250"; example = <0xf00f0000 19>, "a strange property format";Standard Properties
compatible: Lists driver strings the hardware can match.
model: Gives a human‑readable board name.
status: Indicates whether the device is okay, disabled, etc.
#address-cells / #size-cells: Define how many 32‑bit cells represent addresses and sizes in child reg properties.
reg: Describes address‑size pairs for resources, interpreted according to the parent’s cell settings.
name / device_type: Legacy fields; compatible takes precedence for driver matching.
Common Nodes
Root node (/): Top of the tree, contains model, compatible, and cell definitions.
CPU node: Usually defined in a separate .dtsi file; contains address/size cells and individual CPU entries.
Memory node: Specifies physical RAM with a reg property (e.g., reg = <0x80000000 0x20000000>;).
Chosen node: Passes boot arguments to the kernel via bootargs.
Operating Device Tree Functions (OF Functions)
The kernel provides a set of functions prefixed with of_ (Open Firmware) to locate nodes and read properties.
Node‑related Functions
struct device_node *of_find_node_by_name(struct device_node *from, const char *name); struct device_node *of_find_node_by_type(struct device_node *from, const char *type); struct device_node *of_find_compatible_node(struct device_node *from, const char *type, const char *compatible); struct device_node *of_find_matching_node_and_match(struct device_node *from, const struct of_device_id *matches, const struct of_device_id **match); struct device_node *of_find_node_by_path(const char *path);Property‑extraction Functions
property *of_find_property(const struct device_node *np, const char *name, int *lenp); int of_property_count_elems_of_size(const struct device_node *np, const char *propname, int elem_size); int of_property_read_string(const struct device_node *np, const char *propname, const char **out_string);These functions enable driver developers to query hardware descriptions without hard‑coding platform data, making drivers portable across different boards.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
