How to read child node property in a device tree

2019-09-18 01:46发布

I been trying to read child node property in a device tree.. Could not figured it out, can any one help here.

I have a dts

  AA{
    child 1: {
                property 1 : XXX
                property 2 : XXX
       }
   child 2 :{
                property 1 : XXX
                property 2 : XXX
       }

 BB{
    child 1: {
                property 1 : XXX
                property 2 : XXX
       }
   child 2 :{
                property 1 : XXX
                property 2 : XXX
       }

Is there any way of reading properies of child 2 in AA node of given dts ?

2条回答
\"骚年 ilove
2楼-- · 2019-09-18 02:37

If I understood correctly you have to use something like for_each_child_of_node(). Check for example drivers/input/keyboard/gpio_keys.c and Documentation/devicetree/bindings/input/gpio-keys.txt.

查看更多
狗以群分
3楼-- · 2019-09-18 02:49

Yes, you can do it. Just write a similar function as below and call it in AA with the path of the child node of BB.

For example, From AA if you need to access BB/child_2 property then pass the absolute path to of_find_node_by_path() function.

Also, check of_* family of function in the kernel that might be useful.

static void access_dt(void)                                                      
{                                                                                
    /* device node path - check it from /proc/device-tree/ */                    
    char *path = "/path/to/BB/child_2";                                            
    struct device_node *dt_node;                                                 
    const u8 *prop = NULL;                                                       
    int ret;                                                                     

    dt_node = of_find_node_by_path(path);                                        
    if (!dt_node) {                                                              
        printk(KERN_ERR "Failed to find node by path: %s.\n");                   
    } else {                                                                     
        printk(KERN_INFO "Found the node for %s.\n", path);                      
        prop = of_get_property(dt_node, "property 2", &ret);                      
        if(!prop) {
             //You are still in trouble!
        } else {
            //You have got property 2 of BB!
        }                                                                        
    }                                                                            
}
查看更多
登录 后发表回答