From: Ahmad Fatoum <a.fatoum@xxxxxxxxxxxxxx> Import the Linux helper, so code using it may be more easily ported. Signed-off-by: Ahmad Fatoum <a.fatoum@xxxxxxxxxxxxxx> --- drivers/of/base.c | 32 ++++++++++++++++++++++++++++++++ include/of.h | 8 ++++++++ 2 files changed, 40 insertions(+) diff --git a/drivers/of/base.c b/drivers/of/base.c index 6d13b66a0e3e..346d72e1e09e 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -738,6 +738,38 @@ int of_property_read_u32_index(const struct device_node *np, } EXPORT_SYMBOL_GPL(of_property_read_u32_index); +/** + * of_property_count_elems_of_size - Count the number of elements in a property + * + * @np: device node from which the property value is to be read. + * @propname: name of the property to be searched. + * @elem_size: size of the individual element + * + * Search for a property in a device node and count the number of elements of + * size elem_size in it. Returns number of elements on sucess, -EINVAL if the + * property does not exist or its length does not match a multiple of elem_size + * and -ENODATA if the property does not have a value. + */ +int of_property_count_elems_of_size(const struct device_node *np, + const char *propname, int elem_size) +{ + struct property *prop = of_find_property(np, propname, NULL); + + if (!prop) + return -EINVAL; + if (!prop->value) + return -ENODATA; + + if (prop->length % elem_size != 0) { + pr_err("size of %s in node %pOF is not a multiple of %d\n", + propname, np, elem_size); + return -EINVAL; + } + + return prop->length / elem_size; +} +EXPORT_SYMBOL_GPL(of_property_count_elems_of_size); + /** * of_property_read_u8_array - Find and read an array of u8 from a property. * diff --git a/include/of.h b/include/of.h index 8d6c018b73ec..eea5abae459b 100644 --- a/include/of.h +++ b/include/of.h @@ -186,6 +186,8 @@ extern struct device_node *of_find_node_by_reproducible_name(struct device_node extern int of_property_read_u32_index(const struct device_node *np, const char *propname, u32 index, u32 *out_value); +extern int of_property_count_elems_of_size(const struct device_node *np, + const char *propname, int elem_size); extern int of_property_read_u8_array(const struct device_node *np, const char *propname, u8 *out_values, size_t sz); extern int of_property_read_u16_array(const struct device_node *np, @@ -425,6 +427,12 @@ static inline int of_property_read_u32_index(const struct device_node *np, return -ENOSYS; } +static inline int of_property_count_elems_of_size(const struct device_node *np, + const char *propname, int elem_size) +{ + return -ENOSYS; +} + static inline int of_property_read_u8_array(const struct device_node *np, const char *propname, u8 *out_values, size_t sz) { -- 2.20.1 _______________________________________________ barebox mailing list barebox@xxxxxxxxxxxxxxxxxxx http://lists.infradead.org/mailman/listinfo/barebox