Re: [PATCH v3 4/5] of: Create function for counting number of phandles in a property

2013-02-13 Thread Andreas Larsson

On 2013-02-13 00:06, Grant Likely wrote:

This patch creates of_count_phandle_with_args(), a new function for
counting the number of phandle+argument tuples in a given property. This
is better than the existing method of parsing each phandle individually
until parsing fails which is a horribly slow way to do the count.

Tested on ARM using the selftest code.

v3: - Rebased on top of selftest code cleanup patch
v2: - fix bug where of_parse_phandle_with_args() could behave like _count_.
 - made of_gpio_named_count() into a static inline regardless of 
CONFIG_OF_GPIO

Signed-off-by: Grant Likely 
Cc: Linus Walleij 
Cc: Rob Herring 
Cc: Andreas Larsson 


Tested-by: Andreas Larsson 

Cheers,
Andreas Larsson

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH v3 4/5] of: Create function for counting number of phandles in a property

2013-02-13 Thread Andreas Larsson

On 2013-02-13 00:06, Grant Likely wrote:

This patch creates of_count_phandle_with_args(), a new function for
counting the number of phandle+argument tuples in a given property. This
is better than the existing method of parsing each phandle individually
until parsing fails which is a horribly slow way to do the count.

Tested on ARM using the selftest code.

v3: - Rebased on top of selftest code cleanup patch
v2: - fix bug where of_parse_phandle_with_args() could behave like _count_.
 - made of_gpio_named_count() into a static inline regardless of 
CONFIG_OF_GPIO

Signed-off-by: Grant Likely grant.lik...@secretlab.ca
Cc: Linus Walleij linus.wall...@linaro.org
Cc: Rob Herring rob.herr...@calxeda.com
Cc: Andreas Larsson andr...@gaisler.com


Tested-by: Andreas Larsson andr...@gaisler.com

Cheers,
Andreas Larsson

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH v3 4/5] of: Create function for counting number of phandles in a property

2013-02-12 Thread Grant Likely
This patch creates of_count_phandle_with_args(), a new function for
counting the number of phandle+argument tuples in a given property. This
is better than the existing method of parsing each phandle individually
until parsing fails which is a horribly slow way to do the count.

Tested on ARM using the selftest code.

v3: - Rebased on top of selftest code cleanup patch
v2: - fix bug where of_parse_phandle_with_args() could behave like _count_.
- made of_gpio_named_count() into a static inline regardless of 
CONFIG_OF_GPIO

Signed-off-by: Grant Likely 
Cc: Linus Walleij 
Cc: Rob Herring 
Cc: Andreas Larsson 
---
 drivers/of/base.c |   41 +
 drivers/of/selftest.c |   15 +++
 include/linux/of.h|9 +
 3 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index c4538ab..f21794c 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -1082,9 +1082,10 @@ EXPORT_SYMBOL(of_parse_phandle);
  * To get a device_node of the `node2' node you may call this:
  * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, );
  */
-int of_parse_phandle_with_args(const struct device_node *np, const char 
*list_name,
-   const char *cells_name, int index,
-   struct of_phandle_args *out_args)
+static int __of_parse_phandle_with_args(const struct device_node *np,
+   const char *list_name,
+   const char *cells_name, int index,
+   struct of_phandle_args *out_args)
 {
const __be32 *list, *list_end;
int rc = 0, size, cur_index = 0;
@@ -1174,15 +1175,47 @@ int of_parse_phandle_with_args(const struct device_node 
*np, const char *list_na
 * Unlock node before returning result; will be one of:
 * -ENOENT : index is for empty phandle
 * -EINVAL : parsing error on data
+* [1..n]  : Number of phandle (count mode; when index = -1)
 */
-   rc = -ENOENT;
+   rc = index < 0 ? cur_index : -ENOENT;
  err:
if (node)
of_node_put(node);
return rc;
 }
+
+int of_parse_phandle_with_args(const struct device_node *np, const char 
*list_name,
+   const char *cells_name, int index,
+   struct of_phandle_args *out_args)
+{
+   if (index < 0)
+   return -EINVAL;
+   return __of_parse_phandle_with_args(np, list_name, cells_name, index, 
out_args);
+}
 EXPORT_SYMBOL(of_parse_phandle_with_args);
 
+/**
+ * of_count_phandle_with_args() - Find the number of phandles references in a 
property
+ * @np:pointer to a device tree node containing a list
+ * @list_name: property name that contains a list
+ * @cells_name:property name that specifies phandles' arguments count
+ *
+ * Returns the number of phandle + argument tuples within a property. It
+ * is a typical pattern to encode a list of phandle and variable
+ * arguments into a single property. The number of arguments is encoded
+ * by a property in the phandle-target node. For example, a gpios
+ * property would contain a list of GPIO specifies consisting of a
+ * phandle and 1 or more arguments. The number of arguments are
+ * determined by the #gpio-cells property in the node pointed to by the
+ * phandle.
+ */
+int of_count_phandle_with_args(const struct device_node *np, const char 
*list_name,
+   const char *cells_name)
+{
+   return __of_parse_phandle_with_args(np, list_name, cells_name, -1, 
NULL);
+}
+EXPORT_SYMBOL(of_count_phandle_with_args);
+
 #if defined(CONFIG_OF_DYNAMIC)
 static int of_property_notify(int action, struct device_node *np,
  struct property *prop)
diff --git a/drivers/of/selftest.c b/drivers/of/selftest.c
index 01b4174..0eb5c38 100644
--- a/drivers/of/selftest.c
+++ b/drivers/of/selftest.c
@@ -36,6 +36,9 @@ static void __init of_selftest_parse_phandle_with_args(void)
return;
}
 
+   rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
+   selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 
7\n", rc);
+
for (i = 0; i < 8; i++) {
bool passed = true;
rc = of_parse_phandle_with_args(np, "phandle-list",
@@ -94,21 +97,33 @@ static void __init of_selftest_parse_phandle_with_args(void)
rc = of_parse_phandle_with_args(np, "phandle-list-missing",
"#phandle-cells", 0, );
selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
+   rc = of_count_phandle_with_args(np, "phandle-list-missing",
+   "#phandle-cells");
+   selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
 
/* Check for missing cells property */
rc = 

[PATCH v3 4/5] of: Create function for counting number of phandles in a property

2013-02-12 Thread Grant Likely
This patch creates of_count_phandle_with_args(), a new function for
counting the number of phandle+argument tuples in a given property. This
is better than the existing method of parsing each phandle individually
until parsing fails which is a horribly slow way to do the count.

Tested on ARM using the selftest code.

v3: - Rebased on top of selftest code cleanup patch
v2: - fix bug where of_parse_phandle_with_args() could behave like _count_.
- made of_gpio_named_count() into a static inline regardless of 
CONFIG_OF_GPIO

Signed-off-by: Grant Likely grant.lik...@secretlab.ca
Cc: Linus Walleij linus.wall...@linaro.org
Cc: Rob Herring rob.herr...@calxeda.com
Cc: Andreas Larsson andr...@gaisler.com
---
 drivers/of/base.c |   41 +
 drivers/of/selftest.c |   15 +++
 include/linux/of.h|9 +
 3 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index c4538ab..f21794c 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -1082,9 +1082,10 @@ EXPORT_SYMBOL(of_parse_phandle);
  * To get a device_node of the `node2' node you may call this:
  * of_parse_phandle_with_args(node3, list, #list-cells, 1, args);
  */
-int of_parse_phandle_with_args(const struct device_node *np, const char 
*list_name,
-   const char *cells_name, int index,
-   struct of_phandle_args *out_args)
+static int __of_parse_phandle_with_args(const struct device_node *np,
+   const char *list_name,
+   const char *cells_name, int index,
+   struct of_phandle_args *out_args)
 {
const __be32 *list, *list_end;
int rc = 0, size, cur_index = 0;
@@ -1174,15 +1175,47 @@ int of_parse_phandle_with_args(const struct device_node 
*np, const char *list_na
 * Unlock node before returning result; will be one of:
 * -ENOENT : index is for empty phandle
 * -EINVAL : parsing error on data
+* [1..n]  : Number of phandle (count mode; when index = -1)
 */
-   rc = -ENOENT;
+   rc = index  0 ? cur_index : -ENOENT;
  err:
if (node)
of_node_put(node);
return rc;
 }
+
+int of_parse_phandle_with_args(const struct device_node *np, const char 
*list_name,
+   const char *cells_name, int index,
+   struct of_phandle_args *out_args)
+{
+   if (index  0)
+   return -EINVAL;
+   return __of_parse_phandle_with_args(np, list_name, cells_name, index, 
out_args);
+}
 EXPORT_SYMBOL(of_parse_phandle_with_args);
 
+/**
+ * of_count_phandle_with_args() - Find the number of phandles references in a 
property
+ * @np:pointer to a device tree node containing a list
+ * @list_name: property name that contains a list
+ * @cells_name:property name that specifies phandles' arguments count
+ *
+ * Returns the number of phandle + argument tuples within a property. It
+ * is a typical pattern to encode a list of phandle and variable
+ * arguments into a single property. The number of arguments is encoded
+ * by a property in the phandle-target node. For example, a gpios
+ * property would contain a list of GPIO specifies consisting of a
+ * phandle and 1 or more arguments. The number of arguments are
+ * determined by the #gpio-cells property in the node pointed to by the
+ * phandle.
+ */
+int of_count_phandle_with_args(const struct device_node *np, const char 
*list_name,
+   const char *cells_name)
+{
+   return __of_parse_phandle_with_args(np, list_name, cells_name, -1, 
NULL);
+}
+EXPORT_SYMBOL(of_count_phandle_with_args);
+
 #if defined(CONFIG_OF_DYNAMIC)
 static int of_property_notify(int action, struct device_node *np,
  struct property *prop)
diff --git a/drivers/of/selftest.c b/drivers/of/selftest.c
index 01b4174..0eb5c38 100644
--- a/drivers/of/selftest.c
+++ b/drivers/of/selftest.c
@@ -36,6 +36,9 @@ static void __init of_selftest_parse_phandle_with_args(void)
return;
}
 
+   rc = of_count_phandle_with_args(np, phandle-list, #phandle-cells);
+   selftest(rc == 7, of_count_phandle_with_args() returned %i, expected 
7\n, rc);
+
for (i = 0; i  8; i++) {
bool passed = true;
rc = of_parse_phandle_with_args(np, phandle-list,
@@ -94,21 +97,33 @@ static void __init of_selftest_parse_phandle_with_args(void)
rc = of_parse_phandle_with_args(np, phandle-list-missing,
#phandle-cells, 0, args);
selftest(rc == -ENOENT, expected:%i got:%i\n, -ENOENT, rc);
+   rc = of_count_phandle_with_args(np, phandle-list-missing,
+   #phandle-cells);
+   selftest(rc == -ENOENT, expected:%i got:%i\n, -ENOENT,