linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: mark.rutland@arm.com (Mark Rutland)
To: linux-arm-kernel@lists.infradead.org
Subject: [RFCv2 1/2] of: add of_property_read_u32_index
Date: Thu, 13 Dec 2012 16:49:27 +0000	[thread overview]
Message-ID: <1355417368-6861-2-git-send-email-mark.rutland@arm.com> (raw)
In-Reply-To: <1355417368-6861-1-git-send-email-mark.rutland@arm.com>

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 <mark.rutland@arm.com>
---
 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

  reply	other threads:[~2012-12-13 16:49 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-13 16:49 [RFCv2 0/2] Representing interrupt affinity in devicetree Mark Rutland
2012-12-13 16:49 ` Mark Rutland [this message]
2012-12-13 16:49 ` [RFCv2 2/2] ARM: add functions to parse irq affinity from dt Mark Rutland
2013-03-04  2:51 ` [RFCv2 0/2] Representing interrupt affinity in devicetree Grant Likely
2013-03-05  9:28   ` Mark Rutland
2013-04-13 21:33     ` Grant Likely
2013-04-15  9:09       ` Mark Rutland

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1355417368-6861-2-git-send-email-mark.rutland@arm.com \
    --to=mark.rutland@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).