Learn and record common attributes and functions of the device tree

1. Basic structure of equipment tree

1.1 node structure

[label:] node-name[@unit-address] {
	[properties definitions]
	[child nodes]
};

Label represents the node label, which is convenient for other nodes to reference or modify the node later. It is implemented with & label (if there is no label, only the path can be referenced);

&Unit address is used to label the node address.

1.2 common properties

Attribute namefunction
#address-cellsHow many 32 digits does address occupy
#size-cellsHow many 32 digits does size occupy
compatibleThe compatible in the corresponding driver is used to bind the device and driver
modelSimilar to compatible, you can specify compatible devices
statusDefine the device status, "okay" - normal operation, "disabled" - temporary shutdown, "fail" - serious error occurs and needs to be repaired, "fail SSS" - follow the error message
regUsed to describe a register or a section of space. Format RET = < address, size >

1.3 common nodes

chosen node

You can pass some parameters to the kernel through dts, and set the bootargs attribute in the node

chosen {
	bootargs = "noinitrd root=/dev/mtdblock4 rw init=/linuxrc console=ttySAC0,115200";
};

memory node

Define the memory base address and size used by a specific board

memory {
	reg = <0x80000000 0x20000000>;
};

2 related common functions

2.1 finding nodes

a. of_find_node_by_path / / find the node according to the path

struct device_node *of_find_node_by_path(const char *path)

The parameter path value "/" corresponds to the root node, "/ memory" corresponds to the memory node, and so on.

b. of_find_compatible_node / / find the node according to the compatible

struct device_node *of_find_compatible_node(struct device_node *from, const char *type, const char *compat)

The parameter from indicates which node to start from. If NULL is passed in, it is the root node; The parameter type is generally null (generally there is no type attribute)

c. of_get_parent / / find the parent node of the node

struct device_node *of_get_parent(const struct device_node *node)

Note: if you call this function to get a parent, you must call of_node_get(parent) to reduce the number of references to the parent node.

d. of_get_next_parent / / find the parent node of the node

struct device_node *of_get_next_parent(struct device_node *node)

Note: unlike c., there is no need to call of to call this function_ node_ get(parent)

e. of_get_next_child / / retrieve the previous child node

struct device_node *of_get_next_child(const struct device_node *node, struct device_node *prev)

The parameter prev represents the previous child node. If NULL is passed in, the first child node will be the default.

f. of_get_next_available_child / / fetch the last available child node

struct device_node *of_get_next_available_child(const struct device_node *node, struct device_node *prev)

Compared with e., the "disabled" status node is skipped.

2.2 find an attribute

a. of_find_property / / find the property in the node

struct property *of_find_property(const struct device_node *np, const char *name, int *lenp)

Parameter name represents attribute name; Parameter lenp is the length of the saved attribute value

2.3 get the value of the attribute

a. of_get_property / / find the node's property by name and return its value

const void *of_get_property(const struct device_node *np, const char *name, int *lenp)

Parameter name represents attribute name; Parameter lenp is the length of the saved attribute value

b. of_property_count_elems_of_size / / get the number of elements in the node

int of_property_count_elems_of_size(const struct device_node *np, const char *propname, int elem_size)

The parameter propname indicates the attribute name to be counted; Parameter else_size represents the length of the element

c. of_property_read_u32/u64 / / read u32/u64 integers

int of_property_read_u32(const struct device_node *np, const char *propname, u32 *out_value)
int of_property_read_u64(const struct device_node *np, const char *propname, u64 *out_value)

The parameter propname indicates the attribute name to be read; Parameter out_value saves the read value

d. of_property_read_u32 / / read the specified location

int of_property_read_u32_index(const struct device_node *np, const char *propname, u32 index, 
                               u32 *out_value);

The parameter propname indicates the attribute name to be read; Parameter out_value saves the read value; The parameter index represents the read position

e. of_property_read_variable_u8/u16/u32/u64_array / / read array

int of_property_read_variable_u8_array(const struct device_node *np,
                    const char *propname, u8 *out_values, size_t sz_min, size_t sz_max);
int of_property_read_variable_u16_array(const struct device_node *np,
                    const char *propname, u16 *out_values, size_t sz_min, size_t sz_max);
int of_property_read_variable_u32_array(const struct device_node *np,
                    const char *propname, u32 *out_values, size_t sz_min, size_t sz_max);
int of_property_read_variable_u64_array(const struct device_node *np,
                    const char *propname, u64 *out_values, size_t sz_min, size_t sz_max);

Parameter sz_min and sz_max is used to limit the length of the value. If both are not met, neither number can be read

f. of_property_read_string / / read string

int of_property_read_string(const struct device_node *np, const char *propname, const char **out_string);

The parameter propname indicates the attribute name to be read; Parameter out_string saves the read string

3 how to obtain device nodes in the driver

struct device_node *node = pdev->dev.of_node;

Of in dev member of pdev_ The node member represents the device node

Keywords: Linux

Added by validangina on Thu, 13 Jan 2022 23:37:45 +0200