From mboxrd@z Thu Jan 1 00:00:00 1970 Return-path: Received: from relay2-d.mail.gandi.net ([217.70.183.194]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1jIvr1-0000tl-Qi for barebox@lists.infradead.org; Mon, 30 Mar 2020 14:57:33 +0000 From: Ahmad Fatoum Date: Mon, 30 Mar 2020 16:57:16 +0200 Message-Id: <20200330145717.667403-12-ahmad@a3f.at> In-Reply-To: <20200330145717.667403-1-ahmad@a3f.at> References: <20200330145717.667403-1-ahmad@a3f.at> MIME-Version: 1.0 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "barebox" Errors-To: barebox-bounces+u.kleine-koenig=pengutronix.de@lists.infradead.org Subject: [PATCH 11/12] of: introduce of_property_count_elems_of_size To: barebox@lists.infradead.org Cc: Ahmad Fatoum From: Ahmad Fatoum Import the Linux helper, so code using it may be more easily ported. Signed-off-by: Ahmad Fatoum --- 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@lists.infradead.org http://lists.infradead.org/mailman/listinfo/barebox