设备树 DTS 描述硬件:节点(node)表示设备,属性(property)描述参数;根节点 / 下有 cpu、memory、bus 等;compatible 属性匹配驱动(如 “vendor,chip”);reg 描述地址空间,interrupts 描述中断,pinctrl 描述引脚;编译为 DTB 由 bootloader 传给内核。
本文概述
设备树是 Linux 驱动基础,面试常考:节点/属性、compatible、reg、interrupts、pinctrl。回答要给 DTS 示例和匹配过程。
核心要点
设备树 DTS 描述硬件拓扑,编译为 DTB,u-boot 传给内核
节点表示设备,用节点名@地址格式,属性是 key=value
compatible = "vendor,chip" 匹配驱动 of_match_table
reg = <地址 长度> 描述寄存器地址空间,#address-cells/#size-cells 决定格式
interrupts-parent/interrupts 描述中断,pinctrl 描述引脚复用

面试官考察点
设备树基础题。面试官看你是否写过 DTS。重点听:节点结构、compatible、reg、匹配。
回答思路
给 I2C 设备 DTS 示例,讲 compatible 匹配驱动过程。
参考回答
I2C 设备节点示例。
&i2c1 {
clock-frequency = <400000>;
pinctrl-names = "default";
pinctrl-0 = <&i2c1_pins>;
status = "okay";
sensor@48 {
compatible = "ti,tmp102";
reg = <0x48>;
interrupt-parent = <&gpio1>;
interrupts = <5 IRQ_TYPE_EDGE_FALLING>;
vcc-supply = <®_3v3>;
};
};
关键字段。
| 属性 | 含义 | 示例 |
|---|---|---|
| compatible | 匹配驱动,"厂商,型号" | "ti,tmp102" |
| reg | I2C 地址/寄存器地址 | <0x48> |
| interrupt-parent | 中断控制器 | &gpio1 |
| interrupts | 中断号+触发方式 | <5 2> |
| pinctrl-0 | 引脚复用配置 | &i2c1_pins |
| status | okay/disabled | "okay" |
| *-supply | 电源 regulator | ®_3v3 |
compatible 匹配过程。
驱动定义 of_match_table:
static const struct of_device_id tmp102_of_match[] = {
{ .compatible = "ti,tmp102" },
{ /* Sentinel */ }
};
MODULE_DEVICE_TABLE(of, tmp102_of_match);
内核解析 DTS,找到 compatible="ti,tmp102" 的节点;
和驱动 of_match_table 比较,匹配成功调用 probe;
probe 里用 of_property_read_u32 等读属性。
设备树结构。
/ {
model = "My Board";
compatible = "myboard,evk";
#address-cells = <1>;
#size-cells = <1>;
memory@80000000 {
device_type = "memory";
reg = <0x80000000 0x10000000>; /* 256MB */
};
cpus { ... };
soc { ... };
};
注意。
DTS 包含 DTSI(公共部分),板级 DTS 只改差异;
reg 格式由父节点 #address-cells/#size-cells 决定;
pinctrl 配置在 pincontroller 节点里,设备引用;
设备树不改驱动代码就能适配不同板级,这是核心优势。

扫码关注






























