1、 修改linuxDTS设备树文件,/arch/arm/boot/dts/imx6ull-alientek-emmc.dts
ddd是“/”根节点的子节点,定义了5条属性,这5条属性意义如下:
a)、 compatible属性也叫做“兼容性”属性!compatible属性的值是一个字符串列表, compatible属性用于将设备和驱动绑定起来。
b)、status属性
status属性值也是字符串,字符串是设备的状态信息,可选的状态如表
c)、#address-cells和 #size-cells属性
#address-cells属性值决定了子节点 reg属性中地址信息所占用的字长 (32位 ),
#size-cells属性值决定了子节点 reg属性中长度信息所占的字长 (32位 )
d)、reg = <address1 length1 address2 length2 address3 length3…………>
每个“ address length”组合表示一个地址范围,其中 address是起始地址, length是地址长度。
2、修改驱动程序:
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
struct device_node *nd;
static int __init dtsled_init(void)
{
int ret;
u32 regdata[14];
const char *str;
struct property *proper;
nd = of_find_node_by_path("/ddd");
if(nd == NULL)
{
printk("ddd node can not found!\r\n");
return -EINVAL;
}
else
{
printk("ddd node has been found!\r\n");
}
proper = of_find_property(nd, "compatible", NULL);
if(proper == NULL)
{
printk("compatible property find failed\r\n");
}
else
{
printk("compatible = %s\r\n", (char*)proper->value);
}
ret = of_property_read_string(nd, "status", &str);
if(ret < 0)
{
printk("status read failed!\r\n");
}
else
{
printk("status = %s\r\n",str);
}
ret = of_property_read_u32_array(nd, "reg", regdata, 10);
if(ret < 0)
{
printk("reg property read failed!\r\n");
}
else
{
u8 i = 0;
printk("reg data:\r\n");
for(i = 0; i < 10; i++)
printk("%#X ", regdata[i]);
printk("\r\n");
}
return 0;
}
static void __exit dtsled_exit(void)
{
}
module_init(dtsled_init);
module_exit(dtsled_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ddd");
主要涉及到两个结构体和四个函数
struct device_node *nd;
struct property *proper;
a)、Linux内核使用 device_node结构体来描述一个节点。节点的属性信息里面保存了驱动所需要的内容,因此对于属性值的提取非常重要。
b)返回找到的节点
inline struct device_node *of_find_node_by_path(const char *path)
c)函数用于查找指定的属性
property *of_find_property(const struct device_node *np, const char *name, int *lenp)
d)函数用于读取属性中字符串值,函数原型如下:
int of_property_read_string(struct device_node *np, const char *propname, const char **out_string)
e)个函数分别是读取属性中 u32类型的数组数据
int of_property_read_u32_array(const struct device_node *np, const char *propname, u32 *out_values, size_t sz)
最终可以获取/arch/arm/boot/dts/imx6ull-alientek-emmc.dts设备树的ddd节点信息