----------------------------------------------------------------------------------------------------------------------------
内核版本:linux 5.2.8
根文件系统:busybox 1.25.0
u-boot:2016.05
----------------------------------------------------------------------------------------------------------------------------
在Linux内核采用设备树之后,驱动程序需要获取设备树的属性。Linux内核为驱动程序提供了一系列API函数,用于获取设备树的属性值。在Linux内核中,以“of_”开头的函数是设备树API函数。
一、获取设备节点API
在内核中,设备以节点的形式附加到设备树上,因此要获得设备信息,必须先获取设备节点。
1.1 设备节点
Linux内核使用device_node结构体来描述一个设备节点,此结构体定义在文件 include/linux/of.h 中,代码如下:
struct device_node { const char *name; /*节点的名字*/ phandle phandle; const char *full_name; /*节点的全名,node-name[@unit-address]*/ struct fwnode_handle fwnode; struct property *properties; /*节点的属性*/ struct property *deadprops; /* removed properties */ struct device_node *parent; /*父节点*/ struct device_node *child; /*子节点*/ struct device_node *sibling; /*节点的兄弟,即同级节点*/ #if defined(CONFIG_OF_KOBJ) struct kobject kobj; #endif unsigned long _flags; void *data; #if defined(CONFIG_SPARC) unsigned int unique_id; struct of_irq_controller *irq_trans; #endif };
上述数据结构是设备节点结构。让我们来看一下获取设备节点的几个常见函数。
二、
三、
四、
参考文章
[1]Linux设备树学习笔记(四、设备树常用 OF 操作函数)
[3]DeviceTree Kernel API — The Linux Kernel documentation
[4]Linux device tree API (programs.wiki)
[5]of.h - include/linux/of.h - Linux source code (v5.2.8) - Bootlin