From: Patrick Delaunay Date: Wed, 23 Oct 2019 15:44:38 +0200 Subject: [U-Boot] [PATCH 03/13] dm: core: add ofnode function to iterate on node property In-Reply-To: <20191023134448.20149-1-patrick.delaunay@st.com> References: <20191023134448.20149-1-patrick.delaunay@st.com> Message-ID: <20191023134448.20149-4-patrick.delaunay@st.com> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de Add functions to iterate on all property with livetree - ofnode_get_first_property - ofnode_get_next_property - ofnode_get_property_by_prop For example: for (prop = ofnode_get_first_property(dev_ofnode(dev)); prop; prop = ofnode_get_next_property(dev_ofnode(dev),prop)) { value = ofnode_get_property_by_prop(dev_ofnode(dev), prop, &propname, &len); .... } Signed-off-by: Patrick Delaunay --- drivers/core/of_access.c | 32 ++++++++++++++++++++++++++++ drivers/core/ofnode.c | 45 ++++++++++++++++++++++++++++++++++++++++ include/dm/of_access.h | 40 +++++++++++++++++++++++++++++++++++ include/dm/ofnode.h | 39 +++++++++++++++++++++++++++++++++- 4 files changed, 155 insertions(+), 1 deletion(-) diff --git a/drivers/core/of_access.c b/drivers/core/of_access.c index 945b81448c..86fe42ad14 100644 --- a/drivers/core/of_access.c +++ b/drivers/core/of_access.c @@ -170,6 +170,38 @@ const void *of_get_property(const struct device_node *np, const char *name, return pp ? pp->value : NULL; } +const struct property *of_get_first_property(const struct device_node *np) +{ + if (!np) + return NULL; + + return np->properties; +} + +const struct property *of_get_next_property(const struct device_node *np, + const struct property *property) +{ + if (!np) + return NULL; + + return property->next; +} + +const void *of_get_property_by_prop(const struct device_node *np, + const struct property *property, + const char **name, + int *lenp) +{ + if (!np || !property) + return NULL; + if (name) + *name = property->name; + if (lenp) + *lenp = property->length; + + return property->value; +} + static const char *of_prop_next_string(struct property *prop, const char *cur) { const void *curv = cur; diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c index 297f0a0c7c..4169befd92 100644 --- a/drivers/core/ofnode.c +++ b/drivers/core/ofnode.c @@ -536,6 +536,51 @@ const void *ofnode_get_property(ofnode node, const char *propname, int *lenp) propname, lenp); } +const void *ofnode_get_first_property(ofnode node) +{ + int prop_offset; + + if (ofnode_is_np(node)) { + return of_get_first_property(ofnode_to_np(node)); + } else { + prop_offset = fdt_first_property_offset(gd->fdt_blob, + ofnode_to_offset(node)); + if (prop_offset < 0) + return NULL; + + return (void *)(uintptr_t)prop_offset; + } +} + +const void *ofnode_get_next_property(ofnode node, const void *property) +{ + int prop_offset; + + if (ofnode_is_np(node)) { + return of_get_next_property(ofnode_to_np(node), property); + } else { + prop_offset = (uintptr_t)property; + prop_offset = fdt_next_property_offset(gd->fdt_blob, + prop_offset); + if (prop_offset < 0) + return NULL; + + return (void *)(uintptr_t)prop_offset; + } +} + +const void *ofnode_get_property_by_prop(ofnode node, const void *property, + const char **propname, int *lenp) +{ + if (ofnode_is_np(node)) + return of_get_property_by_prop(ofnode_to_np(node), + property, propname, lenp); + else + return fdt_getprop_by_offset(gd->fdt_blob, + (uintptr_t)property, + propname, lenp); +} + bool ofnode_is_available(ofnode node) { if (ofnode_is_np(node)) diff --git a/include/dm/of_access.h b/include/dm/of_access.h index 13fedb7cf5..0418782aa2 100644 --- a/include/dm/of_access.h +++ b/include/dm/of_access.h @@ -103,6 +103,46 @@ struct property *of_find_property(const struct device_node *np, const void *of_get_property(const struct device_node *np, const char *name, int *lenp); +/** + * of_get_first_property()- get to the pointer of the first property + * + * Get pointer to the first property of the node, it is used to iterate + * and read all the property with of_get_next_property_by_prop(). + * + * @p: Pointer to device node + * @return pointer to property or NULL if not found + */ +const struct property *of_get_first_property(const struct device_node *np); + +/** + * of_get_next_property() - get to the pointer of the next property + * + * Get pointer to the next property of the node, it is used to iterate + * and read all the property with of_get_property_by_prop(). + * + * @p: Pointer to device node + * @property: pointer of the current property + * @return pointer to next property or NULL if not found + */ +const struct property *of_get_next_property(const struct device_node *np, + const struct property *property); + +/** + * of_get_property_by_prop() - get a property value of a node property + * + * Get value for the property identified by node and property pointer. + * + * @node: node to read + * @property: pointer of the property to read + * @propname: place to property name on success + * @lenp: place to put length on success + * @return pointer to property value or NULL if error + */ +const void *of_get_property_by_prop(const struct device_node *np, + const struct property *property, + const char **name, + int *lenp); + /** * of_device_is_compatible() - Check if the node matches given constraints * @device: pointer to node diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h index 5c4cbf0998..08d684cea0 100644 --- a/include/dm/ofnode.h +++ b/include/dm/ofnode.h @@ -543,7 +543,7 @@ int ofnode_decode_display_timing(ofnode node, int index, struct display_timing *config); /** - * ofnode_get_property()- - get a pointer to the value of a node property + * ofnode_get_property() - get a pointer to the value of a node property * * @node: node to read * @propname: property to read @@ -552,6 +552,43 @@ int ofnode_decode_display_timing(ofnode node, int index, */ const void *ofnode_get_property(ofnode node, const char *propname, int *lenp); +/** + * ofnode_get_first_property()- get to the pointer of the first property + * + * Get pointer to the first property of the node, it is used to iterate + * and read all the property with ofnode_get_property_by_prop(). + * + * @node: node to read + * @return pointer or offset to property, used to iterate, or NULL + */ +const void *ofnode_get_first_property(ofnode node); + +/** + * ofnode_get_next_property() - get to the pointer of the next property + * + * Get pointer to the next property of the node, it is used to iterate + * and read all the property with ofnode_get_property_by_prop(). + * + * @node: node to read + * @property: pointer or offset of the current property + * @return pointer or offset to next property or NULL + */ +const void *ofnode_get_next_property(ofnode node, const void *property); + +/** + * ofnode_get_property_by_prop() - get a pointer to the value of a node property + * + * Get value for the property identified by node and property. + * + * @node: node to read + * @property: pointer or offset of the property to read + * @propname: place to property name on success + * @lenp: place to put length on success + * @return pointer to property or NULL if error + */ +const void *ofnode_get_property_by_prop(ofnode node, const void *property, + const char **propname, int *lenp); + /** * ofnode_is_available() - check if a node is marked available * -- 2.17.1