From mboxrd@z Thu Jan 1 00:00:00 1970 From: Mark Rutland Subject: [RFCv2 1/2] of: add of_property_read_u32_index Date: Thu, 13 Dec 2012 16:49:27 +0000 Message-ID: <1355417368-6861-2-git-send-email-mark.rutland@arm.com> References: <1355417368-6861-1-git-send-email-mark.rutland@arm.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1355417368-6861-1-git-send-email-mark.rutland-5wv7dgnIgG8@public.gmane.org> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: devicetree-discuss-bounces+gldd-devicetree-discuss=m.gmane.org-uLR06cmDAlY/bJ5BZ2RsiQ@public.gmane.org Sender: "devicetree-discuss" To: linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org Cc: devicetree-discuss-uLR06cmDAlY/bJ5BZ2RsiQ@public.gmane.org, Will.Deacon-5wv7dgnIgG8@public.gmane.org, rob.herring-bsGFqQB8/DxBDgjK7y7TUQ@public.gmane.org, tglx-hfZtesqFncYOwBW4kG4KsQ@public.gmane.org List-Id: devicetree@vger.kernel.org Currently, we have to pre-allocate an array when reading multiple u32 values from a property using of_property_read_u32_array. This isn't very good for times when we want to iterate over elements of a list of arbitrary size. This patch adds a function to read a single u32 value from an arbitrary index in a list, making this case easier. Signed-off-by: Mark Rutland --- drivers/of/base.c | 36 ++++++++++++++++++++++++++++++++++++ include/linux/of.h | 11 +++++++++++ 2 files changed, 47 insertions(+), 0 deletions(-) diff --git a/drivers/of/base.c b/drivers/of/base.c index af3b22a..5ad6830 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -707,6 +707,42 @@ int of_property_read_u32_array(const struct device_node *np, EXPORT_SYMBOL_GPL(of_property_read_u32_array); /** + * of_property_read_u32_index - Find and read a 32 bit integer from a multiple + * integer property. + * + * @np: device node from which the property value is to be read. + * @propname: name of the property to be searched. + * @out_value: pointer to return value, modified only if return value is 0. + * @index: index of the integer in the list of integers. + * + * Search for a property in a device node and read a 32-bit value from + * it. Returns 0 on success, -EINVAL if the property does not exist, + * -ENODATA if property does not have a value, and -EOVERFLOW if the + * index is beyond the range of data. + * + * The out_value is modified only if a valid u32 value can be decoded. + */ +int of_property_read_u32_index(const struct device_node *np, + const char *propname, u32 *out_value, + int idx) +{ + struct property *prop = of_find_property(np, propname, NULL); + const __be32 *val; + + if (!prop) + return -EINVAL; + if (!prop->value) + return -ENODATA; + if ((idx+1) * sizeof(*out_value) > prop->length) + return -EOVERFLOW; + + val = prop->value; + *out_value = be32_to_cpup(val + idx); + return 0; +} +EXPORT_SYMBOL_GPL(of_property_read_u32_index); + +/** * of_property_read_u64 - Find and read a 64 bit integer from a property * @np: device node from which the property value is to be read. * @propname: name of the property to be searched. diff --git a/include/linux/of.h b/include/linux/of.h index b4e50d5..37f6f67 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -227,6 +227,10 @@ extern int of_property_read_u32_array(const struct device_node *np, const char *propname, u32 *out_values, size_t sz); +extern int of_property_read_u32_index(const struct device_node *np, + const char *propname, + u32 *out_value, + int idx); extern int of_property_read_u64(const struct device_node *np, const char *propname, u64 *out_value); @@ -371,6 +375,13 @@ static inline int of_property_read_u32_array(const struct device_node *np, return -ENOSYS; } +static inline int of_property_read_u32_index(const struct device_node *np, + const char *propname, + u32 *out_value, int idx) +{ + return -ENOSYS; +} + static inline int of_property_read_string(struct device_node *np, const char *propname, const char **out_string) -- 1.7.0.4