Menu Close

Linux Device Tree of Functions Used to Find Nodes

This article talks about the binding information documents in device trees.

For related subjects, please refer to the SOC Table of Contents.

 

Device trees provide detailed information on devices, which can take on various forms such as numerical and strings. This information needs to be obtained when writing drivers. For example, when the device tree uses the reg property to describe a peripheral register’s address as 0x12345678, with a length of 0x100, we need to know the value of the reg property 0x12345678 as well as 0x100 when writing drivers, and then initialize peripherals. Thus, Linux utilizes a series of functions that start with “of_” to obtain device tree node and property information. These of_ functions are all defined under the directory include/linux/of.h.

Devices are mounted to device trees as nodes, thus in order to obtain the device’s properties it is necessary to obtain the device node first. Linux kernels use the device_node structure to describe nodes, and the structure is defined in include/linux/of.h between lines 49 and 69:

struct device_node{
        const char *name;                     // Name of node
        const char *type;                     // Device type
        phandle phandle;
        const char *full_name;                // Full name of node
        struct fwnode_handle fwnode;

        struct property *properties;          // Properties
        struct property *deadprops;           // Removed property
        struct device_node *parent;           // Parent node
        struct device_node *child;            // Child node
        struct device_node *sibling;
        struct kobject kobj;
        unsigned long _flags;
        void *data;
#if defined(CONFIG_SPARC)
        const char *path_component_name;
        unsigned int unique_id;
        struct of_irq_controller *irq_trans;
#endif
};

There are 5 of functions, each would return the node if it is found and NULL otherwise.

 

of_find_node_by_name

This function searches for nodes via the node’s name, with the following format:

struct device_node *of_find_node_by_name(struct device_node *from,
                                         const char         *name);

from is the node that the search starts from. If set as NULL, it will search the entire device tree starting from the root node.

name is the name of the node to be searched.

 

of_find_node_by_type

This function uses the device_type property to search for the specified node, with the following format:

struct device_node *of_find_node_by_type(struct device_node *from,
                                         const char         *type);

from is the node that the search starts from. If set as NULL, it will search the entire device tree starting from the root node.

type is the type of the node to be searched, which is also the property value of device_type.

 

of_find_compatible_node

This function is used to find a specified node based off device_type and compatible properties.

struct device_node *of_find_compatible_node(struct device_node *from,
                                            const char         *type,
                                             const char   *compatible);

from is the node that the search starts from. If set as NULL, it will search the entire device treestarting from the root node.

type is the type of the node to be searched, which is also the property value of device_type. This can be set as NULL for it to be ignored.

compatible is the property list that corresponds to the node to be searched.

 

of_find_matching_node_and_match

This function uses the of_device_id match table to search the specified node. Formatting is as follows:

struct device_node *of_find_matching_node_and_match(struct device_node *from,
                                                const struct of_device_id *matches,
                                                const struct of_device_id **match);

from is the node that the search starts from. If set as NULL, it will search the entire device tree starting from the root node.

matches is the of_device_id match table that will be searched.

match finds the corresponding of_device_id.

 

of_find_node_by_path

This function uses node paths to find the specified node, with the following format:

inline struct device_node *of_find_node_by_path(const char *path);

path is the name of the node with the entire directory. Its alias may also be used.

Posted in Textbook and Training Project

Related Articles

Leave a Reply

Your email address will not be published.

Leave the field below empty!