一.of 操作函数获取属性
of_find_property 函数,用于在设备树中查找节点 下具有指定名称的属性。
struct property *of_find_property(const struct device_node *np, const char *name, int*lenp)
np: 要查找的节点。
name: 要查找的属性的属性名。
lenp: 一个指向整数的指针,用于接收属性值的字节数。
返回值: 如果成功找到了指定名称的属性,则返回对应的属性结构体指针struct property*;如果未找到,则返回.。
of_property_count_elems_of_size 函数,用于获取属性中指定元素的数量。
int of_property_count_elems_of_size(const struct device_node *np, const char *propname, int elem_size)
np: 设备节点。
propname: 需要获取元素数量的属性名。
elem_size: 单个元素的尺寸。
返回值: 如果成功获取了指定属性中元素的数量,则返回该数量;如果未找到属性或属性中没有元素,则返回 0。
of_property_read_u32_index 函数,用于从指定属性中获取指定索引位置的 u32 类型的数据值。
int of_property_read_u32_index(const struct device_node *np, const char *propname, u32 index, u32 *out_value)
np: 设备节点。
propname: 要读取的属性名。
index: 要读取的属性值在属性中的索引,索引从 0 开始。
out_value: 用于存储读取到的值的指针。
返回值: 如果成功读取到了指定属性指定索引位置的 u32 类型的数据值,则返回0;如果未找到属性或读取失败,则返回相应的错误码。
of_property_read_u64_index 函数,用于从指定属性中获取指定索引位置的 u64 类型的数据值。
static inline int of_property_read_u64_index(const struct device_node *np, const char*propname, u32 index, u64 *out_value)
np: 设备节点。
propname: 要读取的属性名。
index: 要读取的属性值在属性中的索引,索引从 0 开始。
out_value: 用于存储读取到的值的指针。
返回值: 如果成功读取到了指定属性指定索引位置的 u64 类型的数据值,则返回0;如果未找到属性或读取失败,则返回相应的错误码。
of_property_read_string 函数,用于从指定属性中读取字符串。
static inline int of_property_read_string(const struct device_node *np, const char *propname, const char **out_string)
np: 设备节点。
propname: 要读取的属性名。
out_string: 用于存储读取到的字符串的指针。
返回值: 如果成功读取到了指定属性的字符串,则返回 0;如果未找到属性或读取失败,则返回相应的错误码。
二.of函数获取属性实验
dts:
/{topeet{#address-cells = <1>;#size-cells = <1>;compatible = "simple-bus";myLed{compatible = "my devicetree";reg = <0xFDD60000 0x00000004>;};};
};
驱动程序:
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/of.h>struct device_node *mydevice_node;
int num;
u32 value_u32;
u64 value_u64;
u32 out_value[2];
const char *value_compatible;
struct property *my_property;// 平台设备的探测函数
static int my_platform_probe(struct platform_device *pdev)
{printk(KERN_INFO "my_platform_probe: Probing platform device\n");// 通过节点名称查找设备树节点mydevice_node = of_find_node_by_name(NULL, "myLed");printk("mydevice node is %s\n", mydevice_node->name);// 查找compatible属性my_property = of_find_property(mydevice_node, "compatible", NULL);printk("my_property name is %s\n", my_property->name);// 获取reg属性的元素数量num = of_property_count_elems_of_size(mydevice_node, "reg", 4);printk("reg num is %d\n", num);// 读取reg属性的值of_property_read_u32_index(mydevice_node, "reg", 0, &value_u32);of_property_read_u64_index(mydevice_node, "reg", 0, &value_u64);printk("value u32 is 0x%X\n", value_u32);printk("value u64 is 0x%llx\n", value_u64);// 读取reg属性的变长数组of_property_read_variable_u32_array(mydevice_node, "reg", out_value, 1, 2);printk("out_value[0] is 0x%X\n", out_value[0]);printk("out_value[1] is 0x%X\n", out_value[1]);// 读取compatible属性的字符串值of_property_read_string(mydevice_node, "compatible", &value_compatible);printk("compatible value is %s\n", value_compatible);return 0;
}// 平台设备的移除函数
static int my_platform_remove(struct platform_device *pdev)
{printk(KERN_INFO "my_platform_remove: Removing platform device\n");// 清理设备特定的操作// ...return 0;
}const struct of_device_id of_match_table_id[] = {{.compatible="my devicetree"},
};// 定义平台驱动结构体
static struct platform_driver my_platform_driver = {.probe = my_platform_probe,.remove = my_platform_remove,.driver = {.name = "my_platform_device",.owner = THIS_MODULE,.of_match_table = of_match_table_id,},
};// 模块初始化函数
static int __init my_platform_driver_init(void)
{int ret;// 注册平台驱动ret = platform_driver_register(&my_platform_driver);if (ret) {printk(KERN_ERR "Failed to register platform driver\n");return ret;}printk(KERN_INFO "my_platform_driver: Platform driver initialized\n");return 0;
}// 模块退出函数
static void __exit my_platform_driver_exit(void)
{// 注销平台驱动platform_driver_unregister(&my_platform_driver);printk(KERN_INFO "my_platform_driver: Platform driver exited\n");
}module_init(my_platform_driver_init);
module_exit(my_platform_driver_exit);MODULE_LICENSE("GPL");
MODULE_AUTHOR("topeet");