Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

property: Add functions to iterate named child

There are a few use-cases where child nodes with a specific name need to
be parsed. Code like:

fwnode_for_each_child_node()
if (fwnode_name_eq())
...

can be found from a various drivers/subsystems. Adding a macro for this
can simplify things a bit.

In a few cases the data from the found nodes is later added to an array,
which is allocated based on the number of found nodes. One example of
such use is the IIO subsystem's ADC channel nodes, where the relevant
nodes are named as channel[@N].

Add helpers for iterating and counting device's sub-nodes with certain
name instead of open-coding this in every user.

Suggested-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Marcelo Schmitt <marcelo.schmitt1@gmail.com>
Link: https://patch.msgid.link/2767173b7b18e974c0bac244688214bd3863ff06.1742560649.git.mazziesaccount@gmail.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Matti Vaittinen and committed by
Jonathan Cameron
f2430363 4a135e92

+47
+27
drivers/base/property.c
··· 945 945 } 946 946 EXPORT_SYMBOL_GPL(device_get_child_node_count); 947 947 948 + /** 949 + * fwnode_get_named_child_node_count - number of child nodes with given name 950 + * @fwnode: Node which child nodes are counted. 951 + * @name: String to match child node name against. 952 + * 953 + * Scan child nodes and count all the nodes with a specific name. Potential 954 + * 'number' -ending after the 'at sign' for scanned names is ignored. 955 + * E.g.:: 956 + * fwnode_get_named_child_node_count(fwnode, "channel"); 957 + * would match all the nodes:: 958 + * channel { }, channel@0 {}, channel@0xabba {}... 959 + * 960 + * Return: the number of child nodes with a matching name for a given device. 961 + */ 962 + unsigned int fwnode_get_named_child_node_count(const struct fwnode_handle *fwnode, 963 + const char *name) 964 + { 965 + struct fwnode_handle *child; 966 + unsigned int count = 0; 967 + 968 + fwnode_for_each_named_child_node(fwnode, child, name) 969 + count++; 970 + 971 + return count; 972 + } 973 + EXPORT_SYMBOL_GPL(fwnode_get_named_child_node_count); 974 + 948 975 bool device_dma_supported(const struct device *dev) 949 976 { 950 977 return fwnode_call_bool_op(dev_fwnode(dev), device_dma_supported);
+20
include/linux/property.h
··· 167 167 for (child = fwnode_get_next_child_node(fwnode, NULL); child; \ 168 168 child = fwnode_get_next_child_node(fwnode, child)) 169 169 170 + #define fwnode_for_each_named_child_node(fwnode, child, name) \ 171 + fwnode_for_each_child_node(fwnode, child) \ 172 + if (!fwnode_name_eq(child, name)) { } else 173 + 170 174 #define fwnode_for_each_available_child_node(fwnode, child) \ 171 175 for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\ 172 176 child = fwnode_get_next_available_child_node(fwnode, child)) ··· 182 178 for (child = device_get_next_child_node(dev, NULL); child; \ 183 179 child = device_get_next_child_node(dev, child)) 184 180 181 + #define device_for_each_named_child_node(dev, child, name) \ 182 + device_for_each_child_node(dev, child) \ 183 + if (!fwnode_name_eq(child, name)) { } else 184 + 185 185 #define device_for_each_child_node_scoped(dev, child) \ 186 186 for (struct fwnode_handle *child __free(fwnode_handle) = \ 187 187 device_get_next_child_node(dev, NULL); \ 188 188 child; child = device_get_next_child_node(dev, child)) 189 + 190 + #define device_for_each_named_child_node_scoped(dev, child, name) \ 191 + device_for_each_child_node_scoped(dev, child) \ 192 + if (!fwnode_name_eq(child, name)) { } else 189 193 190 194 struct fwnode_handle *fwnode_get_named_child_node(const struct fwnode_handle *fwnode, 191 195 const char *childname); ··· 221 209 int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name); 222 210 223 211 unsigned int device_get_child_node_count(const struct device *dev); 212 + 213 + unsigned int fwnode_get_named_child_node_count(const struct fwnode_handle *fwnode, 214 + const char *name); 215 + static inline unsigned int device_get_named_child_node_count(const struct device *dev, 216 + const char *name) 217 + { 218 + return fwnode_get_named_child_node_count(dev_fwnode(dev), name); 219 + } 224 220 225 221 static inline int device_property_read_u8(const struct device *dev, 226 222 const char *propname, u8 *val)