1. 代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/* 模块入口 */
static int __init dtsof_init(void)
{
int ret = 0;
struct device_node *bl_nd = NULL; /* 节点 */
struct property *comppro = NULL; /* 属性 */
const char *str; /* 用于保存下面的status, 修饰是按函数来的 */
u32 defValue = 0; /* 用于保存下面的数字变量 */
u32 eleSize; /* 用于保存数组个数 */
u32* briVal; /* 数组,读亮度的,需要malloc */
u8 i; /* 循环用 */
/* 1.找到backlight节点, 路径是/backlight */
bl_nd = of_find_node_by_path("/backlight");
if(bl_nd == NULL) { //代表失败啦
ret = -EINVAL;
goto fail_findnd;
}
/* 2. 找到节点之后,可以获取属性 */
/* 第一个参数为节点,第二个为属性名字, 第三个是长度 */
comppro = of_find_property(bl_nd, "compatible", NULL);
if(comppro == NULL) { //失败
ret = -EINVAL;
goto fail_findpro;
} else { //成功
printk("compatible = %s \r\n", (char*)comppro->value);
}
/* 获取status属性 */
ret = of_property_read_string(bl_nd, "status", &str);
if(ret < 0) {
goto fail_rs;
} else {
printk("status = %s \r\n", str);
}
/* 3.获取数字属性值 */
ret = of_property_read_u32(bl_nd, "default-brightness-level", &defValue);
if(ret < 0) {
goto fail_read_32;
}else {
printk("default-brightness-level = %d \r\n", defValue);
}
/* 4.获取数组类型 */
/* 注意eleSize返回值小于0说明失败,大于0是元素的数量 */
eleSize = of_property_count_elems_of_size(bl_nd, "brightness-levels", sizeof(u32));
if(eleSize < 0) {
goto fail_readele;
}else {
printk("brightness-level size = %d \r\n", eleSize);
}
/* GFP_ATOMIC 分配内存是一个原子过程,分配过程不被中断打断 */
/* GFP_KERNEL 正常分配内存 */
/* GFP_DMA 给DMA控制器分配内存,需要使用标志(DMA要求分配虚拟地址和物理地址连续) */
briVal = kmalloc(eleSize * sizeof(u32), GFP_KERNEL);
if(!eleSize){
ret = -EINVAL;
goto fail_mem;
}
/* 获取数组的值 */
ret = of_property_read_u32_array(bl_nd, "brightness-levels", briVal, eleSize);
if(ret < 0) {
goto fail_read_array;
}else {
for (i = 0; i < eleSize; ++i){
printk("brightness-level[%d]= %d \r\n", i, *(briVal + i));
}
}
kfree(briVal);
return 0;
fail_read_array:
kfree(briVal);
fail_mem:
fail_readele:
fail_read_32:
fail_rs:
fail_findpro:
fail_findnd:
return ret;
}
/* 模块出口 */
static void __exit dtsof_exit(void)
{
}
/* 注册模块入口和出口 */
module_init(dtsof_init);
module_exit(dtsof_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Shao Zheming");
2.效果