diff options
author | Heiko Stuebner <heiko.stuebner@bqreaders.com> | 2014-02-11 19:00:34 -0500 |
---|---|---|
committer | Mark Brown <broonie@linaro.org> | 2014-02-12 10:48:48 -0500 |
commit | ad54a0cfbeb4bd4033d09017557ccbc423f9d5ff (patch) | |
tree | 673298cf99ab63d9bc0ab19b705537966d65e2e3 /drivers/of | |
parent | 38dbfb59d1175ef458d006556061adeaa8751b72 (diff) |
of: add functions to count number of elements in a property
The need to know the number of array elements in a property is
a common pattern. To prevent duplication of open-coded implementations
add a helper static function that also centralises strict sanity
checking and DTB format details, as well as a set of wrapper functions
for u8, u16, u32 and u64.
Suggested-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Heiko Stuebner <heiko.stuebner@bqreaders.com>
Acked-by: Rob Herring <robh@kernel.org>
Acked-by: Grant Likely <grant.likely@linaro.org>
Acked-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Mark Brown <broonie@linaro.org>
Diffstat (limited to 'drivers/of')
-rw-r--r-- | drivers/of/base.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/drivers/of/base.c b/drivers/of/base.c index ff85450d5683..8a2a55c65e5d 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c | |||
@@ -886,6 +886,38 @@ struct device_node *of_find_node_by_phandle(phandle handle) | |||
886 | EXPORT_SYMBOL(of_find_node_by_phandle); | 886 | EXPORT_SYMBOL(of_find_node_by_phandle); |
887 | 887 | ||
888 | /** | 888 | /** |
889 | * of_property_count_elems_of_size - Count the number of elements in a property | ||
890 | * | ||
891 | * @np: device node from which the property value is to be read. | ||
892 | * @propname: name of the property to be searched. | ||
893 | * @elem_size: size of the individual element | ||
894 | * | ||
895 | * Search for a property in a device node and count the number of elements of | ||
896 | * size elem_size in it. Returns number of elements on sucess, -EINVAL if the | ||
897 | * property does not exist or its length does not match a multiple of elem_size | ||
898 | * and -ENODATA if the property does not have a value. | ||
899 | */ | ||
900 | int of_property_count_elems_of_size(const struct device_node *np, | ||
901 | const char *propname, int elem_size) | ||
902 | { | ||
903 | struct property *prop = of_find_property(np, propname, NULL); | ||
904 | |||
905 | if (!prop) | ||
906 | return -EINVAL; | ||
907 | if (!prop->value) | ||
908 | return -ENODATA; | ||
909 | |||
910 | if (prop->length % elem_size != 0) { | ||
911 | pr_err("size of %s in node %s is not a multiple of %d\n", | ||
912 | propname, np->full_name, elem_size); | ||
913 | return -EINVAL; | ||
914 | } | ||
915 | |||
916 | return prop->length / elem_size; | ||
917 | } | ||
918 | EXPORT_SYMBOL_GPL(of_property_count_elems_of_size); | ||
919 | |||
920 | /** | ||
889 | * of_find_property_value_of_size | 921 | * of_find_property_value_of_size |
890 | * | 922 | * |
891 | * @np: device node from which the property value is to be read. | 923 | * @np: device node from which the property value is to be read. |