mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH v2 02/12] of: implement of_property_read_u64_array
Date: Mon, 12 Oct 2020 08:26:09 +0200	[thread overview]
Message-ID: <20201012062619.20400-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20201012062619.20400-1-a.fatoum@pengutronix.de>

For reading reg with #address-cells and #size-cells of 2,
an of_property_read_u64_array can be quite convenient.
Add one.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> v2: no changes
---
 drivers/of/base.c | 37 +++++++++++++++++++++++++++++++++++++
 include/of.h      | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index f5aad268b2d0..5b45c2023f3b 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -893,6 +893,43 @@ int of_property_read_u64(const struct device_node *np, const char *propname,
 }
 EXPORT_SYMBOL_GPL(of_property_read_u64);
 
+/**
+ * of_property_read_u64_array - Find and read an array of 64 bit integers
+ * from a 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.
+ * @sz:		number of array elements to read
+ *
+ * Search for a property in a device node and read 64-bit value(s) 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
+ * property data isn't large enough.
+ *
+ * The out_value is modified only if a valid u64 value can be decoded.
+ */
+int of_property_read_variable_u64_array(const struct device_node *np,
+			       const char *propname, u64 *out_values,
+			       size_t sz)
+{
+	size_t count;
+	const __be32 *val = of_find_property_value_of_size(np, propname,
+						(sz * sizeof(*out_values)));
+
+	if (IS_ERR(val))
+		return PTR_ERR(val);
+
+	count = sz;
+	while (count--) {
+		*out_values++ = of_read_number(val, 2);
+		val += 2;
+	}
+
+	return sz;
+}
+EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
+
 /**
  * of_property_read_string - Find and read a string from a property
  * @np:		device node from which the property value is to be read.
diff --git a/include/of.h b/include/of.h
index d5947fbaab56..d87eeff738e8 100644
--- a/include/of.h
+++ b/include/of.h
@@ -201,6 +201,11 @@ extern int of_property_read_u32_array(const struct device_node *np,
 extern int of_property_read_u64(const struct device_node *np,
 				const char *propname, u64 *out_value);
 
+extern int of_property_read_variable_u64_array(const struct device_node *np,
+					const char *propname,
+					u64 *out_values,
+					size_t sz);
+
 extern int of_property_read_string(struct device_node *np,
 				   const char *propname,
 				   const char **out_string);
@@ -469,6 +474,15 @@ static inline int of_property_read_u64(const struct device_node *np,
 	return -ENOSYS;
 }
 
+static inline int of_property_read_variable_u64_array(const struct device_node *np,
+					const char *propname,
+					u64 *out_values,
+					size_t sz_min,
+					size_t sz_max)
+{
+	return -ENOSYS;
+}
+
 static inline int of_property_read_string(struct device_node *np,
 				const char *propname, const char **out_string)
 {
@@ -881,6 +895,34 @@ static inline int of_property_read_u32(const struct device_node *np,
 	return of_property_read_u32_array(np, propname, out_value, 1);
 }
 
+/**
+ * of_property_read_u64_array - Find and read an array of 64 bit integers
+ * from a property.
+ *
+ * @np:		device node from which the property value is to be read.
+ * @propname:	name of the property to be searched.
+ * @out_values:	pointer to return value, modified only if return value is 0.
+ * @sz:		number of array elements to read
+ *
+ * Search for a property in a device node and read 64-bit value(s) 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
+ * property data isn't large enough.
+ *
+ * The out_values is modified only if a valid u64 value can be decoded.
+ */
+static inline int of_property_read_u64_array(const struct device_node *np,
+					     const char *propname,
+					     u64 *out_values, size_t sz)
+{
+	int ret = of_property_read_variable_u64_array(np, propname, out_values,
+						      sz);
+	if (ret >= 0)
+		return 0;
+	else
+		return ret;
+}
+
 /*
  * struct property *prop;
  * const __be32 *p;
-- 
2.28.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2020-10-12  6:26 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-12  6:26 [PATCH v2 01/12] sandbox: dts: retire skeleton.dtsi Ahmad Fatoum
2020-10-12  6:26 ` Ahmad Fatoum [this message]
2020-10-12  6:26 ` [PATCH v2 03/12] sandbox: hostfile: unify --image and direct device tree probe Ahmad Fatoum
2020-10-12  6:26 ` [PATCH v2 04/12] sandbox: hostfile: support anonymous hostfiles in device tree Ahmad Fatoum
2020-10-12  6:26 ` [PATCH v2 05/12] sandbox: hostfile: maintain created temp files over reset Ahmad Fatoum
2020-10-12  6:26 ` [PATCH v2 06/12] sandbox: dts: define default environment node Ahmad Fatoum
2020-10-12  6:26 ` [PATCH v2 07/12] sandbox: poweroff: migrate to driver probed from device tree Ahmad Fatoum
2020-10-12  6:26 ` [PATCH v2 08/12] sandbox: power: implement reset source support Ahmad Fatoum
2020-10-12  6:26 ` [PATCH v2 09/12] sandbox: dts: implement reboot mode Ahmad Fatoum
2020-10-12  6:26 ` [PATCH v2 10/12] sandbox: add watchdog driver Ahmad Fatoum
2020-10-12  6:26 ` [PATCH v2 11/12] sandbox: dts: include state node by default Ahmad Fatoum
2020-10-12  6:26 ` [PATCH v2 12/12] sandbox: defconfig: enable new generic features Ahmad Fatoum
2020-10-12 14:36 ` [PATCH v2 01/12] sandbox: dts: retire skeleton.dtsi Sascha Hauer

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=20201012062619.20400-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@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