From patchwork Tue Sep 18 07:10:28 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [v4, 1/5] dt: introduce of_get_child_by_name to get child node by name Date: Mon, 17 Sep 2012 21:10:28 -0000 From: Srinivas KANDAGATLA X-Patchwork-Id: 184636 Message-Id: <1347952228-24568-1-git-send-email-srinivas.kandagatla@st.com> To: robherring2@gmail.com, bergner@us.ibm.com, devicetree-discuss@lists.ozlabs.org Cc: kgene.kim@samsung.com, srinivas.kandagatla@st.com, broonie@opensource.wolfsonmicro.com, afleming@freescale.com, ben-linux@fluff.org, linuxppc-dev@lists.ozlabs.org From: Srinivas Kandagatla This patch introduces of_get_child_by_name function to get a child node by its name in a given parent node. Without this patch each driver code has to iterate the parent and do a string compare, However having of_get_child_by_name libary function would avoid code duplication, errors and is more convenient. Signed-off-by: Srinivas Kandagatla --- drivers/of/base.c | 23 +++++++++++++++++++++++ include/linux/of.h | 2 ++ 2 files changed, 25 insertions(+), 0 deletions(-) diff --git a/drivers/of/base.c b/drivers/of/base.c index d4a1c9a..af3b22a 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -391,6 +391,29 @@ struct device_node *of_get_next_available_child(const struct device_node *node, EXPORT_SYMBOL(of_get_next_available_child); /** + * of_get_child_by_name - Find the child node by name for a given parent + * @node: parent node + * @name: child name to look for. + * + * This function looks for child node for given matching name + * + * Returns a node pointer if found, with refcount incremented, use + * of_node_put() on it when done. + * Returns NULL if node is not found. + */ +struct device_node *of_get_child_by_name(const struct device_node *node, + const char *name) +{ + struct device_node *child; + + for_each_child_of_node(node, child) + if (child->name && (of_node_cmp(child->name, name) == 0)) + break; + return child; +} +EXPORT_SYMBOL(of_get_child_by_name); + +/** * of_find_node_by_path - Find a node matching a full OF path * @path: The full path to match * diff --git a/include/linux/of.h b/include/linux/of.h index 1b11632..7b8e3cd 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -192,6 +192,8 @@ extern struct device_node *of_get_next_child(const struct device_node *node, struct device_node *prev); extern struct device_node *of_get_next_available_child( const struct device_node *node, struct device_node *prev); +extern struct device_node *of_get_child_by_name(const struct device_node *node, + const char *name); #define for_each_child_of_node(parent, child) \ for (child = of_get_next_child(parent, NULL); child != NULL; \