Device Tree信息获取:
添加节点:
/* 添加 led 节点 */
led {
#address-cells = <1>;
#size-cells = <1>;
/* 蓝灯子节点 */
blue_led@0xe0200240 {
compatible = "s5pv210, blue_led";
reg = <0xe0200240 0x8>;
status = "okay";
};
};
设备树信息获取代码:
#include <linux/module.h>
#include <linux/init.h>
/* dts */
#include <linux/of.h>
struct dts_info
{
struct device_node * led_parent;
struct device_node * led_child_blue;
struct property * led_child_blue_property;
};
static struct dts_info g_dts_info = {0};
// 模块安装函数
static int __init chrdev_init(void)
{
int blue_compatible_size = 0;
int ret = -1;
int out_values[2] = {0};
const char * status = NULL;
/*
of_find_node_by_path: 根据节点路径寻找节点函数
*/
g_dts_info.led_parent = of_find_node_by_path("/led");
if (NULL == g_dts_info.led_parent)
{
printk(KERN_ERR "led_test is NULL\n");
return -EINVAL;
}
printk(KERN_INFO "led_parent name:%s\n", g_dts_info.led_parent->name);
printk(KERN_INFO "child name:%s\n", g_dts_info.led_parent->child->name);
/*
of_get_next_child: 寻找子节点函数
*/
g_dts_info.led_child_blue = of_get_next_child(g_dts_info.led_parent, NULL);
if (NULL == g_dts_info.led_child_blue)
{
printk(KERN_ERR "cof_get_next_child is NULL\n");
return -EINVAL;
}
printk(KERN_INFO "current child name:%s\n", g_dts_info.led_child_blue->name);
printk(KERN_INFO "parent name:%s\n", g_dts_info.led_child_blue->parent->name);
/*
of_find_property: 查找节点属性函数
*/
g_dts_info.led_child_blue_property = of_find_property(g_dts_info.led_child_blue, "compatible", &blue_compatible_size);
if (NULL == g_dts_info.led_child_blue_property)
{
printk(KERN_ERR "of_find_property is NULL\n");
return -EINVAL;
}
printk(KERN_INFO "size:%d\n", blue_compatible_size);
printk(KERN_INFO "name:%s\n", g_dts_info.led_child_blue_property->name);
printk(KERN_INFO "value:%s\n", (char *)g_dts_info.led_child_blue_property->value);
printk(KERN_INFO "length:%d\n", g_dts_info.led_child_blue_property->length);
/*
of_property_read_u32_array: 读取整型属性函数
*/
ret = of_property_read_u32_array(g_dts_info.led_child_blue, "reg", out_values, 2);
if (ret)
{
printk(KERN_ERR "of_property_read_u32_array error\n");
return -EINVAL;
}
printk(KERN_INFO "0x%08X\n", out_values[0]);
printk(KERN_INFO "0x%08X\n", out_values[1]);
/*
of_property_read_string: 读取字符串属性函数
*/
ret = of_property_read_string(g_dts_info.led_child_blue, "status", &status);
if (ret)
{
printk(KERN_ERR "of_property_read_string error\n");
return -EINVAL;
}
printk(KERN_INFO "status=%s\n", status);
return 0;
}
// 模块卸载函数
static void __exit chrdev_exit(void)
{
printk(KERN_INFO "chrdev_exit helloworld exit\n");
}
module_init(chrdev_init);
module_exit(chrdev_exit);
// MODULE_xxx这种宏作用是用来添加模块描述信息
MODULE_LICENSE("GPL"); // 描述模块的许可证
MODULE_AUTHOR("Mark"); // 描述模块的作者
MODULE_DESCRIPTION("test for driver"); // 描述模块的介绍信息
MODULE_ALIAS("alias test"); // 描述模块的别名信息
部分资源来源于网络,若有侵权,请联系作者删除,谢谢