本系列均为正点原子 Linux 驱动的学习笔记, 以便加深笔者记忆。如读者想进一步学习,可以到正点原子官网中下载资料进行学习。
Linux 内核中使用 device_node 结构体来描述一个节点,此结构体定义在文件 include/linux/of.h
中,定义如下:
struct device_node {
const char *name; /* 节点名字 */
const char *type; /* 设备类型 */
phandle phandle;
const char *full_name; /* 节点全名 */
struct fwnode_handle fnode;
struct property *properties; /* 属性 */
struct property *deadprops; /* removes 属性 */
struct device_node *parend; /* 父节点 */
struct device_node *child; /* 子节点 */
struct device_node *sibling;
struct kobject kobj;
...
}
与查找节点有关的 OF 函数有 5 个
of_find_node_by_name 函数通过节点名字查找指定的节点,函数原型如下
struct device_node *of_find_node_by_name(struct device_node *from, const char *name);
参数 | 含义 |
---|---|
from | 开始查找的节点,如果为 NULL 表示从根节点开始查找整个设备树 |
name | 要查找的节点名字 |
返回值 | 含义 |
NULL | 查找失败 |
成功 | 找到的节点 |
of_find_node_by_type 函数通过 device_type 属性查找指定的节点,函数原型如下:
struct device_node *of_find_node_by_type(struct device_node *from, const char *type);
参数 | 含义 |
---|---|
from | 开始查找的节点,如果为 NULL 表示从根节点开始查找整个设备树 |
type | 要查找的节点对应的字符串,也就是 device_type 属性值 |
返回值 | 含义 |
NULL | 查找失败 |
成功 | 找到的节点 |
of_find_compatible_node 根据 device_type 和 compatible 这两个属性查找指定的节点,函数原型如下
struct device_node *of_find_compatible_node(struct device_node *from, const struct of_device_id *matches, const struct of_device_id **match);
参数 | 含义 |
---|---|
from | 开始查找的节点,如果为 NULL 表示从根节点开始查找整个设备树 |
matches | of_device_id 匹配表,也就是在此匹配表里面查找节点 |
match | 找到的匹配的 of_device_id |
返回值 | 含义 |
NULL | 查找失败 |
成功 | 找到的节点 |
of_find_node_by_path通过路径来查找指定的节点,函数原型如下
inline struct device_node *of_find_node_by_path(const char *path);
参数 | 含义 |
---|---|
path | 带有全路径的节点名 |
返回值 | 含义 |
NULL | 查找失败 |
成功 | 找到的节点 |
用于获取指定节点的父节点(如果有父节点的话)
struct device_node *of_get_parent(const struct device_node *node);
函数原型如下:
struct device_node *of_get_next_child(const struct device_node *node, struct device_node *prev);
node :父节点
prev:前一个子节点,也就是从哪一个子节点开始的下一个子节点,如果为 NULL 表示从第一个子节点开始
返回值:找到的下一个子节点
Linux 内核中使用 struct property 结构体来表示属性,原型如下
struct property {
char *name; //属性名字
int length; //属性长度
void *value; //属性值
struct property *next;//下一个属性
unsigned long _flags;
unsigned int unique_id;
struct bin_attribute attr;
}
用于查找指定的属性
struct property *of_find_property(const struct device_node *np, const char *name, int *lenp);
np:设备节点
name :属性名字
lenp:属性值的字节数
返回值:找到的属性
of_property_count_elems_of_size 函数用于获取属性中元素的数量,比如 reg 属性值是一个
数组,那么使用此函数可以获取到这个数组的大小,此函数原型如下:
int of_property_count_elems_of_size(const struct device_node *np, const char *propname int elem_size)
of_property_read_u32_index 函数用于从属性中获取指定标号的 u32 类型数据值(无符号 32 位),比如某个属性有多个 u32 类型的值,那么就可以使用此函数来获取指定标号的数据值,此
函数原型如下:
int of_property_read_u32_index(const struct device_node *np,
const char *propname,
u32 index,
u32 *out_value)
of_property_read_u16_array of_property_read_u32_array of_property_read_u64_array 函数原型如下
int of_property_read_u8_array(const struct device_node *np,
const char *propname,
u8 *out_values,
size_t sz)
int of_property_read_u16_array(const struct device_node *np,
const char *propname,
u16 *out_values,
size_t sz)
int of_property_read_u32_array(const struct device_node *np,
const char *propname,
u32 *out_values,
size_t sz)
int of_property_read_u64_array(const struct device_node *np,
const char *propname,
u64 *out_values,
size_t sz)
有些属性只有一个整形值,这四个函数就是用于读取这种只有一个整形值的属性,分别用
于读取 u8、u16、u32 和 u64 类型属性值,函数原型如下:
int of_property_read_u8(const struct device_node *np,
const char *propname,
u8 *out_value)
int of_property_read_u16(const struct device_node *np,
const char *propname,
u16 *out_value)
int of_property_read_u32(const struct device_node *np,
const char *propname,
u32 *out_value)
int of_property_read_u64(const struct device_node *np,
const char *propname,
u64 *out_value)
of_property_read_string 函数用于读取属性中字符串值,函数原型如下:
int of_property_read_string(struct device_node *np,
const char *propname,
const char **out_string)
of_n_addr_cells 函数用于获取#address-cells 属性值,函数原型如下:
int of_n_addr_cells(struct device_node *np)
of_size_cells 函数用于获取#size-cells 属性值,函数原型如下:
int of_n_size_cells(struct device_node *np)
of_device_is_compatible 函数用于查看节点的 compatible 属性是否有包含 compat 指定的字
符串,也就是检查设备节点的兼容性,函数原型如下:
int of_device_is_compatible(const struct device_node *device,
const char *compat)
of_get_address 函数用于获取地址相关属性,主要是“reg”或者“assigned-addresses”属性
值,函数属性如下:
const __be32 *of_get_address(struct device_node *dev,
int index,
u64 *size,
unsigned int *flags)
of_translate_address 函数负责将从设备树读取到的地址转换为物理地址,函数原型如下:
u64 of_translate_address(struct device_node *dev,const __be32 *in_addr)
int of_address_to_resource(struct device_node *dev,
int index,
struct resource *r)
void __iomem *of_iomap(struct device_node *np, int index)