内核里操作设备树的常用函数

内核里操作设备树的常用函数

内核源码中 include/linux 目录下有很多 of 开头的头文件,of 表示 open firmware 即开放固件。

内核中设备树相关的头文件介绍

设备树的处理过程是:dtb->device_node->platform_device

  1. 处理 DTB
of_fdt.h   // dtb 文件的相关操作函数, 我们一般用不到, 
			// 因为 dtb 文件在内核中已经被转换为 device_node 树(它更易于使用)
  1. 处理 device_node
of.h 		// 提供设备树的一般处理函数
			// 比如 of_property_read_u32(读取某个属性的 u32 值),
			// of_get_child_count(获取某个 device_node 的子节点数

of_address.h   // 地址相关的函数, 
				// 比如 of_get_address(获得 reg 属性中的 addr, size 值)
				// of_match_device (从 matches 数组中取出与当前设备最匹配的一项)

of_dma.h	 // 设备树中 DMA 相关属性的函数
of_gpio.h	 // GPIO 相关的函数
of_graph.h	 // GPU 相关驱动中用到的函数, 从设备树中获得 GPU 信息
of_iommu.h	 // 很少用到
of_irq.h	 // 中断相关的函数
of_mdio.h	 // MDIO (Ethernet PHY) API
of_net.h	 // OF helpers for network devices.
of_pci.h	 // PCI 相关函数
of_pdt.h	 // 很少用到
of_reserved_mem.h	 // reserved_mem 的相关函数
  1. 处理 platform_device
of_platform.h		 // 把 device_node 转换为 platform_device 时用到的函数,
					 // 比如 of_device_alloc(根据 device_node 分配设置 platform_device),
					 // of_find_device_by_node (根据 device_node 查找到 platform_device),
					 // of_platform_bus_probe (处理 device_node 及它的子节点)
of_device.h			 // 设备相关的函数, 比如 of_match_device

platform_device 相关的函数

of_platform.h 中声明了很多函数,但是作为驱动开发者,我们只使用其中的 1、2 个。其他的都是给内核自己使用的,内核使用它们来处理设备树,转换得到 platform_device

  1. of_find_device_by_node
    函数原型:

extern struct platform_device *of_find_device_by_node(struct device_node *np);

设备树中的每一个节点,在内核里都有一个 device_node;你可以使用 device_node去找到对应的 platform_device

  1. platform_get_resource

这个函数跟设备树没什么关系,但是设备树中的节点被转换为platform_device 后,设备树中的 reg 属性,interrupts 属性也会被转换为 resource
这时,你可以使用这个函数取出这些资源。
函数原型:

/**
* platform_get_resource - get a resource for a device
* @dev: platform device
* @type: resource type // 取哪类资源?IORESOURCE_MEM、IORESOURCE_REG
* // IORESOURCE_IRQ 等
* @num: resource index // 这类资源中的哪一个?
*/
struct resource *platform_get_resource(struct platform_device *dev,
 unsigned int type, unsigned int num);

对于设备树节点中的 reg 属性,它属性 IORESOURCE_MEM 类型的资源;

对于设备树节点中的 interrupts 属性,它属性 IORESOURCE_IRQ 类型的资源。

有些节点不会生成 platform_device,怎么访问它们

内核会把 dtb 文件解析出一系列的 device_node 结构体,我们可以直接访问这些 device_node
内核源码 include/linux/of.h 中声明了 device_node 和属性 property 的操作函数,device_nodeproperty 的结构体定义如下:

在这里插入图片描述

  1. 找到节点 of_find_node_by_path
    根据路径找到节点,比如 / 就对应跟节点,/memory 对应 /memory 节点。
    函数原型:

static inline struct device_node *of_find_node_by_path(const char *path);

  1. of_find_node_by_name
    根据名字找到节点,节点如果定义了 name 属性,那我们可以根据名字找到它。
    函数原型:

extern struct device_node *of_find_node_by_name(struct device_node *from,
const char *name);

参数 from 表示从哪一个节点开始寻找,传入 NULL 表示从根节点开始寻找。但是在设备树的官方规范中不建议使用“name”属性,所以这函数也不建议使用。

  1. of_find_node_by_type
    根据类型找到节点,节点如果定义了 device_type 属性,那我们可以根据类型找到它。
    函数原型:

extern struct device_node *of_find_node_by_type(struct device_node *from,
const char *type);

参数 from 表示从哪一个节点开始寻找,传入 NULL 表示从根节点开始寻找。
但是在设备树的官方规范中不建议使用“device_type”属性,所以这函数也不建议使用。

  1. of_find_compatible_node
    根据 compatible 找到节点,节点如果定义了 compatible 属性,那我们可以根据
    compatible 属性找到它。
    函数原型:

extern struct device_node *of_find_compatible_node(struct device_node *from,
const char *type, const char *compat);

参数 from 表示从哪一个节点开始寻找,传入 NULL 表示从根节点开始寻找。
参数 compat 是一个字符串,用来指定 compatible 属性的值;
参数 type 是一个字符串,用来指定 device_type 属性的值,可以传入 NULL。

  1. of_find_node_by_phandle
    根据 phandle 找到节点。
    dts 文件被编译为 dtb 文件时,每一个节点都有一个数字 ID,这些数字 ID 彼此不同。可以使用数字 ID 来找到 device_node。这些数字 ID 就是 phandle。
    函数原型:

extern struct device_node *of_find_node_by_phandle(phandle handle);

参数 from 表示从哪一个节点开始寻找,传入 NULL 表示从根节点开始寻找。

  1. of_get_parent
    找到 device_node 的父节点。
    函数原型:

extern struct device_node *of_get_parent(const struct device_node *node);

参数 from 表示从哪一个节点开始寻找,传入 NULL 表示从根节点开始寻找。

  1. of_get_next_parent
    这个函数名比较奇怪,怎么可能有“next parent”?
    它实际上也是找到 device_node 的父节点,跟 of_get_parent 的返回结果是一样的。
    差别在于它多调用下列函数,把 node 节点的引用计数减少了 1。这意味着调用 of_get_next_parent 之后,你不再需要调用 of_node_put 释放 node 节点。

of_node_put(node);

函数原型:

extern struct device_node *of_get_next_parent(struct device_node *node);

参数 from 表示从哪一个节点开始寻找,传入 NULL 表示从根节点开始寻找。

  1. of_get_next_child
    取出下一个子节点。
    函数原型:

extern struct device_node *of_get_next_child(const struct device_node *node,
struct device_node *prev);

参数 node 表示父节点;
prev 表示上一个子节点,设为 NULL 时表示想找到第 1 个子节点。
不断调用 of_get_next_child 时,不断更新 pre 参数,就可以得到所有的子节点。

  1. of_get_next_available_child
    取出下一个“可用”的子节点,有些节点的 status 是“disabled”,那就会跳过这些节点。
    函数原型:

struct device_node *of_get_next_available_child(const struct device_node *node,
struct device_node *prev);

参数 node 表示父节点;
prev 表示上一个子节点,设为 NULL 时表示想找到第 1 个子节点。

  1. of_get_child_by_name
    根据名字取出子节点。
    函数原型:

extern struct device_node *of_get_child_by_name(const struct device_node *node,
const char *name);

参数 node 表示父节点;
name 表示子节点的名字。

找到属性

核源码 incldue/linux/of.h 中声明了 device_node 的操作函数,当然也包括属性的操作函数

  1. of_find_property
    找到节点中的属性。
    函数原型:
extern struct property *of_find_property(const struct device_node *np, 
										const char *name, 
										int *lenp);

参数 np 表示节点,我们要在这个节点中找到名为 name 的属性。
lenp 用来保存这个属性的长度,即它的值的长度。

在设备树中,节点大概是这样:

xxx_node {
 	xxx_pp_name = “hello”;
};

上述节点中,“xxx_pp_name” 就是属性的名字,值的长度是 6。

获取属性的值

  1. of_get_property
    根据名字找到节点的属性,并且返回它的值。
    函数原型:
/*
* Find a property with a given name for a given node
* and return the value.
*/
const void *of_get_property(const struct device_node *np, 
							const char *name,
							int *lenp)

参数 np 表示节点,我们要在这个节点中找到名为 name 的属性,然后返回它的值。
lenp 用来保存这个属性的长度,即它的值的长度。

  1. of_property_count_elems_of_size
    根据名字找到节点的属性,确定它的值有多少个元素(elem)。
    函数原型:
* of_property_count_elems_of_size - Count the number of elements in a property
*
* @np: device node from which the property value is to be read.
* @propname: name of the property to be searched.
* @elem_size: size of the individual element
*
* Search for a property in a device node and count the number of elements of
* size elem_size in it. Returns number of elements on sucess, -EINVAL if the
* property does not exist or its length does not match a multiple of elem_size
* and -ENODATA if the property does not have a value.
*/
int of_property_count_elems_of_size(const struct device_node *np,
const char *propname, int elem_size)

参数 np 表示节点,我们要在这个节点中找到名为 propname 的属性,然后返回下列结果:

return prop->length / elem_size;

在设备树中,节点大概是这样:

xxx_node {
	 xxx_pp_name = <0x50000000 1024> <0x60000000 2048>;
};

调用 of_property_count_elems_of_size(np, “xxx_pp_name”, 8)时,返回值是 2;
调用 of_property_count_elems_of_size(np, “xxx_pp_name”, 4)时,返回值是 4。

  1. 读整数 u32/u64
    函数原型为:
static inline int of_property_read_u32(const struct device_node *np,
									 const char *propname,
									 u32 *out_value);
extern int of_property_read_u64(const struct device_node *np,
								const char *propname, 
								u64 *out_value);

在设备树中,节点大概是这样:

xxx_node {
	 name1 = <0x50000000>;
	 name2 = <0x50000000 0x60000000>;
};

调用 of_property_read_u32 (np, “name1”, &val)时,val 将得到值 0x50000000;
调用 of_property_read_u64 (np, “name2”, &val)时,val 将得到值0x0x6000000050000000。

  1. 读某个整数 u32/u64
    函数原型为:
extern int of_property_read_u32_index(const struct device_node *np,
									 const char *propname,
									 u32 index, u32 *out_value);

在设备树中,节点大概是这样

xxx_node {
 	name2 = <0x50000000 0x60000000>;
};

调用 of_property_read_u32 (np, “name2”, 1, &val)时,val 将得到值 0x0x60000000。

  1. 读数组
    函数原型为:
int of_property_read_variable_u8_array(const struct device_node *np,
										const char *propname, u8 *out_values,
										size_t sz_min, size_t sz_max);
										
int of_property_read_variable_u16_array(const struct device_node *np,
										const char *propname, u16 *out_values,
										size_t sz_min, size_t sz_max);
										
int of_property_read_variable_u32_array(const struct device_node *np,
										const char *propname, u32 *out_values,
 										size_t sz_min, size_t sz_max);

int of_property_read_variable_u64_array(const struct device_node *np,
 										const char *propname, u64 *out_values,
										 size_t sz_min, size_t sz_max);

在设备树中,节点大概是这样:

xxx_node {
	 name2 = <0x50000012 0x60000034>;
};

上述例子中属性 name2 的值,长度为 8
调用 of_property_read_variable_u8_array (np, “name2”, out_values, 1, 10)时,out_values中将会保存这 8 个字节: 0x12,0x00,0x00,0x50,0x34,0x00,0x00,0x60。

调用 of_property_read_variable_u16_array (np, “name2”, out_values, 1, 10)时,out_values
中将会保存这 4 个 16 位数值: 0x0012, 0x5000,0x0034,0x6000。

总之,这些函数要么能取到全部的数值,要么一个数值都取不到;
如果值的长度在 sz_min 和 sz_max 之间,就返回全部的数值;否则一个数值都不返回。

  1. 读字符串
    函数原型为:
int of_property_read_string(const struct device_node *np, const char *propname,
							const char **out_string);

返回节点 np 的属性(名为 propname)的值,(*out_string)指向这个值,把它当作字符串。

怎么修改设备树文件

一个写得好的驱动程序, 它会尽量确定所用资源。
只把不能确定的资源留给设备树, 让设备树来指定。

根据原理图确定"驱动程序无法确定的硬件资源", 再在设备树文件中填写对应内容。
那么, 所填写内容的格式是什么?

  1. 看绑定文档
    • 内核文档 Documentation/devicetree/bindings/
    • 做得好的厂家也会提供设备树的说明文档
  2. 参考同类型单板的设备树文件
  3. 网上搜索
  4. 实在没办法时, 只能去研究驱动源码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值