-
Linux 3.1版本开始使用设备树。
-
Uboot设备从v1.1.3开始支持设备树,需要在编译Uboot的时候在config文件中加入
#define CONFIG_OF_LIBFDT
。 -
设备树路径:
arch/arm/boot/dts
-
DTS: 是一种ASCII文本格式的设备树描述,此文本非常人性化,是和人类的阅读习惯。
-
DTSI: Linux内核为了简化,把SoC公用的部分或者多个设备共同的部分一般提炼为
.dtsi
,类似于C语言的头文件。 -
DTC(Device Tree Compiler): 是将
.dts
编译为.dtb
的工具。DTC的源代码位于scripts/dtc
目录中。 -
DTB(Device Tree Blob): 是被
dts
被DTC编译之后的二进制格式的设备树描述,可由内核解析,U-Boot这样的bootloader也是可以识别.dtb
的。 -
绑定(Binding): 描述对应节点的兼容性、必须的属性和可选的属性的TXT文件位于
Documentation/devicetree/bindings
目录下。该文档又叫DT Binding文档。 -
DTS的编译过程是支持C语言的预处理的,可以使用
GPIO_ACTIVE_LOW
这样的宏。但是需要包含#include <dt-bindings/gpio/gpio.h>
头文件,dt-bindings
头文件位于内核的arch/arm/boot/dts/include/dt-bindings
目录中。 -
设备树结构模版:
/{
node1 {
a-string-property = "A string";
a-string-list-property = "first string", "second string";
a-byte-data-property = [0x01 0x23 0x34 0x56];
child-node1 {
first-child-property;
second-child-property = <1>;
a-string-property = "Hello, world";
};//child-node1-end
child-node2 {
};//child-node2-end
};//node1-end
node2 {
an-empty-property;
a-cell-property = <1 2 3 4>; /* each number (cell) is a uint32 */
child-node1 {
};//child-node1-end
};//node2-end
};//root-node-end
- 驱动中用到的结构体和函数:
/* 使用设备树后,驱动需要与.dts中描述的设备节点进行匹配,从而使驱动的probe()函数执行。
对与platform_driver而言,需要添加一个OF匹配表。*/
static const struct of_device_id a1234_i2c_of_match[] = {
{.compatible = "acme, a1234-i2c-bus"},
{},
};
MODULE_DEVICE_TABLE(of, a1234_i2c_of_match);
/* 平台驱动 platform_driver */
static struct platform_driver i2c_a1234_driver = {
.driver = {
.name = "a1234-i2c-bus",
.owner = THIS_MODULE,
.of_match_table = a1234_i2c_of_match,
},
.probe = i2c_a1234_probe,
.remove = i2c_a1234_remove,
};
module_platform_driver(of_a1234_driver);
/* probe 函数
参数是平台设备*/
static int xxx_probe(struct platform_device *pdev)
{
xxx = kzalloc(sizeof(xxx), GFP_KERNEL);
...
}
/*remove 函数
参数是平台设备*/
static int xxx_remove(struct platform_device *pdev)
- 相关命令:
apt-get install device-tree-compiler //安装DTC工具
./scripts/dtc/dtc -I dtb -O dts -o xxx.dts arch/arm/boot/dts/xxx.dtb //反编译dtb为dts
/*UBoot下的命令*/
UBoot> fdt addr 0x71000000 //设置.dtb的地址为0x71000000
UBoot> fdt resize
UBoot> fdt print
UBoot> bootz kernel_addr initrd_address dtb_address //启动内核