linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] Some work to implement of_gpio_count()
@ 2008-12-05 18:15 Anton Vorontsov
  2008-12-05 18:15 ` [PATCH 1/3] of: Minor simplification for the of_parse_phandles_with_args() Anton Vorontsov
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Anton Vorontsov @ 2008-12-05 18:15 UTC (permalink / raw)
  To: Paul Mackerras; +Cc: Stefan Roese, linuxppc-dev

Hi all,

Few patches are needed to implement of_gpio_count() helper function.

So far there are two potential users for the new helper:

- PPC4xx SPI driver
  http://www.mail-archive.com/linuxppc-dev@ozlabs.org/msg26847.html
  ^^ The driver implements of_num_gpios() by itself. Though its
  implementation has some minor issues with regard to the empty cells.
  We'll convert it to the generic function later (the driver is
  a half way to the mainline, so I don't want to interfere. And it's
  another reason for the "of_gpio_count" name).

- Some MPC83xx SPI work I'm about to send.


Thanks,

-- 
Anton Vorontsov
email: cbouatmailru@gmail.com
irc://irc.freenode.net/bd2

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] of: Minor simplification for the of_parse_phandles_with_args()
  2008-12-05 18:15 [PATCH 0/3] Some work to implement of_gpio_count() Anton Vorontsov
@ 2008-12-05 18:15 ` Anton Vorontsov
  2008-12-05 18:15 ` [PATCH 2/3] of: of_parse_phandles_with_args() learns to differentiate 'hole' cells Anton Vorontsov
  2008-12-05 18:15 ` [PATCH 3/3] of/gpio: Implement of_gpio_count() Anton Vorontsov
  2 siblings, 0 replies; 4+ messages in thread
From: Anton Vorontsov @ 2008-12-05 18:15 UTC (permalink / raw)
  To: Paul Mackerras; +Cc: Stefan Roese, linuxppc-dev

By using 'list++' in the beginning we can simplify the code a
little bit.

Suggested-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Signed-off-by: Anton Vorontsov <avorontsov@ru.mvista.com>
---
 drivers/of/base.c |   11 ++++-------
 1 files changed, 4 insertions(+), 7 deletions(-)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 4f884a3..cf04d4d 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -547,14 +547,12 @@ int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
 		const u32 *cells;
 		const phandle *phandle;
 
-		phandle = list;
-		args = list + 1;
+		phandle = list++;
+		args = list;
 
 		/* one cell hole in the list = <>; */
-		if (!*phandle) {
-			list++;
+		if (!*phandle)
 			goto next;
-		}
 
 		node = of_find_node_by_phandle(*phandle);
 		if (!node) {
@@ -570,8 +568,7 @@ int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
 			goto err1;
 		}
 
-		/* Next phandle is at offset of one phandle cell + #cells */
-		list += 1 + *cells;
+		list += *cells;
 		if (list > list_end) {
 			pr_debug("%s: insufficient arguments length\n",
 				 np->full_name);
-- 
1.5.6.5

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] of: of_parse_phandles_with_args() learns to differentiate 'hole' cells
  2008-12-05 18:15 [PATCH 0/3] Some work to implement of_gpio_count() Anton Vorontsov
  2008-12-05 18:15 ` [PATCH 1/3] of: Minor simplification for the of_parse_phandles_with_args() Anton Vorontsov
@ 2008-12-05 18:15 ` Anton Vorontsov
  2008-12-05 18:15 ` [PATCH 3/3] of/gpio: Implement of_gpio_count() Anton Vorontsov
  2 siblings, 0 replies; 4+ messages in thread
From: Anton Vorontsov @ 2008-12-05 18:15 UTC (permalink / raw)
  To: Paul Mackerras; +Cc: Stefan Roese, linuxppc-dev

Given this list (contains three gpio specifiers, one of which is a hole):

gpios = <&phandle1 1 2 3
         0 /* a hole */
         &phandle2 4 5 6>;

of_parse_phandles_with_args() would report -ENOENT for the `hole'
specifier item, the same error value is used to report the end of the
list, for example.

Sometimes we want to differentiate holes from real errors -- for
example when we want to count all the [syntax correct] specifiers.

With this patch of_parse_phandles_with_args() will report -EEXITS when
somebody requested to parse a hole.

Also, make the out_{node,args} arguments optional, when counting we
don't really need the out values.

Signed-off-by: Anton Vorontsov <avorontsov@ru.mvista.com>
---
 drivers/of/base.c |   22 ++++++++++++++++------
 1 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index cf04d4d..cd17092 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -499,8 +499,8 @@ EXPORT_SYMBOL_GPL(of_modalias_node);
  * @list_name:	property name that contains a list
  * @cells_name:	property name that specifies phandles' arguments count
  * @index:	index of a phandle to parse out
- * @out_node:	pointer to device_node struct pointer (will be filled)
- * @out_args:	pointer to arguments pointer (will be filled)
+ * @out_node:	optional pointer to device_node struct pointer (will be filled)
+ * @out_args:	optional pointer to arguments pointer (will be filled)
  *
  * This function is useful to parse lists of phandles and their arguments.
  * Returns 0 on success and fills out_node and out_args, on error returns
@@ -534,7 +534,7 @@ int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
 	int size;
 	int cur_index = 0;
 	struct device_node *node = NULL;
-	const void *args;
+	const void *args = NULL;
 
 	list = of_get_property(np, list_name, &size);
 	if (!list) {
@@ -580,16 +580,26 @@ next:
 
 		of_node_put(node);
 		node = NULL;
+		args = NULL;
 		cur_index++;
 	}
 
 	if (!node) {
-		ret = -ENOENT;
+		/*
+		 * args w/o node indicates that the loop above has stopped at
+		 * the 'hole' cell. Report this differently.
+		 */
+		if (args)
+			ret = -EEXIST;
+		else
+			ret = -ENOENT;
 		goto err0;
 	}
 
-	*out_node = node;
-	*out_args = args;
+	if (out_node)
+		*out_node = node;
+	if (out_args)
+		*out_args = args;
 
 	return 0;
 err1:
-- 
1.5.6.5

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] of/gpio: Implement of_gpio_count()
  2008-12-05 18:15 [PATCH 0/3] Some work to implement of_gpio_count() Anton Vorontsov
  2008-12-05 18:15 ` [PATCH 1/3] of: Minor simplification for the of_parse_phandles_with_args() Anton Vorontsov
  2008-12-05 18:15 ` [PATCH 2/3] of: of_parse_phandles_with_args() learns to differentiate 'hole' cells Anton Vorontsov
@ 2008-12-05 18:15 ` Anton Vorontsov
  2 siblings, 0 replies; 4+ messages in thread
From: Anton Vorontsov @ 2008-12-05 18:15 UTC (permalink / raw)
  To: Paul Mackerras; +Cc: Stefan Roese, linuxppc-dev

This function is used to count how many GPIOs are specified for
a device node.

Signed-off-by: Anton Vorontsov <avorontsov@ru.mvista.com>
---
 drivers/of/gpio.c       |   34 ++++++++++++++++++++++++++++++++++
 include/linux/of_gpio.h |    6 ++++++
 2 files changed, 40 insertions(+), 0 deletions(-)

diff --git a/drivers/of/gpio.c b/drivers/of/gpio.c
index a4ba217..6eea601 100644
--- a/drivers/of/gpio.c
+++ b/drivers/of/gpio.c
@@ -80,6 +80,40 @@ err0:
 EXPORT_SYMBOL(of_get_gpio_flags);
 
 /**
+ * of_gpio_count - Count GPIOs for a device
+ * @np:		device node to count GPIOs for
+ *
+ * The function returns the count of GPIOs specified for a node.
+ *
+ * Note that the empty GPIO specifiers counts too. For example,
+ *
+ * gpios = <0
+ *          &pio1 1 2
+ *          0
+ *          &pio2 3 4>;
+ *
+ * defines four GPIOs (so this function will return 4), two of which
+ * are not specified.
+ */
+unsigned int of_gpio_count(struct device_node *np)
+{
+	unsigned int cnt = 0;
+
+	do {
+		int ret;
+
+		ret = of_parse_phandles_with_args(np, "gpios", "#gpio-cells",
+						  cnt, NULL, NULL);
+		/* A hole in the gpios = <> counts anyway. */
+		if (ret < 0 && ret != -EEXIST)
+			break;
+	} while (++cnt);
+
+	return cnt;
+}
+EXPORT_SYMBOL(of_gpio_count);
+
+/**
  * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
  * @of_gc:	pointer to the of_gpio_chip structure
  * @np:		device node of the GPIO chip
diff --git a/include/linux/of_gpio.h b/include/linux/of_gpio.h
index e25abf6..fc2472c 100644
--- a/include/linux/of_gpio.h
+++ b/include/linux/of_gpio.h
@@ -65,6 +65,7 @@ static inline struct of_mm_gpio_chip *to_of_mm_gpio_chip(struct gpio_chip *gc)
 
 extern int of_get_gpio_flags(struct device_node *np, int index,
 			     enum of_gpio_flags *flags);
+extern unsigned int of_gpio_count(struct device_node *np);
 
 extern int of_mm_gpiochip_add(struct device_node *np,
 			      struct of_mm_gpio_chip *mm_gc);
@@ -81,6 +82,11 @@ static inline int of_get_gpio_flags(struct device_node *np, int index,
 	return -ENOSYS;
 }
 
+static inline unsigned int of_gpio_count(struct device_node *np)
+{
+	return 0;
+}
+
 #endif /* CONFIG_OF_GPIO */
 
 /**
-- 
1.5.6.5

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-12-05 18:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-12-05 18:15 [PATCH 0/3] Some work to implement of_gpio_count() Anton Vorontsov
2008-12-05 18:15 ` [PATCH 1/3] of: Minor simplification for the of_parse_phandles_with_args() Anton Vorontsov
2008-12-05 18:15 ` [PATCH 2/3] of: of_parse_phandles_with_args() learns to differentiate 'hole' cells Anton Vorontsov
2008-12-05 18:15 ` [PATCH 3/3] of/gpio: Implement of_gpio_count() Anton Vorontsov

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).