Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1421732 > unrolled thread

[PATCH 0/5] gpio: refactor of_parse_own_gpio() and of_get_named_gpiod_flags()

Started byMasahiro Yamada <yamada.masahiro@socionext.com>
First post2016-06-14 12:10 +0200
Last post2016-06-23 09:50 +0200
Articles 6 — 2 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH 0/5] gpio: refactor of_parse_own_gpio() and of_get_named_gpiod_flags() Masahiro Yamada <yamada.masahiro@socionext.com> - 2016-06-14 12:10 +0200
    [PATCH 2/5] gpio: of: drop needless gpio_chip look-up in of_parse_own_gpio() Masahiro Yamada <yamada.masahiro@socionext.com> - 2016-06-14 12:10 +0200
      Re: [PATCH 2/5] gpio: of: drop needless gpio_chip look-up in of_parse_own_gpio() Linus Walleij <linus.walleij@linaro.org> - 2016-06-23 09:40 +0200
    [PATCH 4/5] gpio: of: remove of_gpiochip_and_xlate() and struct gg_data Masahiro Yamada <yamada.masahiro@socionext.com> - 2016-06-14 12:10 +0200
      Re: [PATCH 4/5] gpio: of: remove of_gpiochip_and_xlate() and struct gg_data Linus Walleij <linus.walleij@linaro.org> - 2016-06-23 09:50 +0200
    Re: [PATCH 0/5] gpio: refactor of_parse_own_gpio() and of_get_named_gpiod_flags() Linus Walleij <linus.walleij@linaro.org> - 2016-06-23 09:50 +0200

#1421732 — [PATCH 0/5] gpio: refactor of_parse_own_gpio() and of_get_named_gpiod_flags()

FromMasahiro Yamada <yamada.masahiro@socionext.com>
Date2016-06-14 12:10 +0200
Subject[PATCH 0/5] gpio: refactor of_parse_own_gpio() and of_get_named_gpiod_flags()
Message-ID<rJTsd-3r2-3@gated-at.bofh.it>
I found one bug in of_parse_own_gpio(); of_gpiochip_find_and_xlate()
fills gg_data->out_gpio with an error pointer if the .of_xlate()
callback fails, but of_parse_own_gpio() only checks whether the
out_gpio is NULL.  This is a problem because the error pointer might
be passed to the desc_to_gpio() in the failure path.

At first, I was trying to fix it with the following patch:

    @@ -165,6 +165,9 @@ static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
                return ERR_PTR(ret);

        gpiochip_find(&gg_data, of_gpiochip_find_and_xlate);
    +   if (IS_ERR(gg_data.out_gpio)){
    +           return gg_data.out_gpio;
    +   }
        if (!gg_data.out_gpio) {
                if (np->parent == np)
                        return ERR_PTR(-ENXIO);

But, after reading this code around, I thought it was more complex
than needed.  So, I decided to clean-up it instead of fix the bug.

Talking about of_parse_own_gpio(), does it need to call gpiochip_find()
in the first place?

The caller of this function, of_gpiochip_scan_gpios() already knows the
gpio_chip.  I want to make the implementation more straight-forward.



Masahiro Yamada (5):
  gpio: of: optimize "gpios" property parsing of of_parse_own_gpio()
  gpio: of: drop needless gpio_chip look-up in of_parse_own_gpio()
  gpio: of: move chip->of_gpio_n_cells checking to of_gpiochip_add()
  gpio: of: remove of_gpiochip_and_xlate() and struct gg_data
  gpio: of: factor out common code to a new helper function

 drivers/gpio/gpiolib-of.c | 126 +++++++++++++++++++++-------------------------
 1 file changed, 58 insertions(+), 68 deletions(-)

-- 
1.9.1

[toc] | [next] | [standalone]


#1421733 — [PATCH 2/5] gpio: of: drop needless gpio_chip look-up in of_parse_own_gpio()

FromMasahiro Yamada <yamada.masahiro@socionext.com>
Date2016-06-14 12:10 +0200
Subject[PATCH 2/5] gpio: of: drop needless gpio_chip look-up in of_parse_own_gpio()
Message-ID<rJTsd-3r2-5@gated-at.bofh.it>
In reply to#1421732
This function is doing more complicated than needed.  The caller of
this function, of_gpiochip_scan_gpios() already knows the pointer to
the gpio_chip.  It can pass it to of_parse_own_gpio() instead of
looking up the gpio_chip by gpiochip_find().

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
---

 drivers/gpio/gpiolib-of.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)

diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
index 99fc40e..27b1729 100644
--- a/drivers/gpio/gpiolib-of.c
+++ b/drivers/gpio/gpiolib-of.c
@@ -122,6 +122,7 @@ EXPORT_SYMBOL(of_get_named_gpio_flags);
 /**
  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
  * @np:		device node to get GPIO from
+ * @chip:	GPIO chip whose hog is parsed
  * @name:	GPIO line name
  * @lflags:	gpio_lookup_flags - returned from of_find_gpio() or
  *		of_parse_own_gpio()
@@ -131,19 +132,19 @@ EXPORT_SYMBOL(of_get_named_gpio_flags);
  * value on the error condition.
  */
 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
+					   struct gpio_chip *chip,
 					   const char **name,
 					   enum gpio_lookup_flags *lflags,
 					   enum gpiod_flags *dflags)
 {
 	struct device_node *chip_np;
 	enum of_gpio_flags xlate_flags;
-	struct gg_data gg_data = {
-		.flags = &xlate_flags,
-	};
+	struct of_phandle_args gpiospec;
+	struct gpio_desc *desc;
 	u32 tmp;
 	int ret;
 
-	chip_np = np->parent;
+	chip_np = chip->of_node;
 	if (!chip_np)
 		return ERR_PTR(-EINVAL);
 
@@ -155,23 +156,23 @@ static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
 	if (ret)
 		return ERR_PTR(ret);
 
-	if (tmp > MAX_PHANDLE_ARGS)
+	if (tmp > MAX_PHANDLE_ARGS || tmp != chip->of_gpio_n_cells)
 		return ERR_PTR(-EINVAL);
 
-	gg_data.gpiospec.args_count = tmp;
-	gg_data.gpiospec.np = chip_np;
-	ret = of_property_read_u32_array(np, "gpios", gg_data.gpiospec.args,
-					 tmp);
+	gpiospec.np = chip_np;
+	gpiospec.args_count = tmp;
+
+	ret = of_property_read_u32_array(np, "gpios", gpiospec.args, tmp);
 	if (ret)
 		return ERR_PTR(ret);
 
-	gpiochip_find(&gg_data, of_gpiochip_find_and_xlate);
-	if (!gg_data.out_gpio) {
-		if (np->parent == np)
-			return ERR_PTR(-ENXIO);
-		else
-			return ERR_PTR(-EINVAL);
-	}
+	ret = chip->of_xlate(chip, &gpiospec, &xlate_flags);
+	if (ret < 0)
+		return ERR_PTR(ret);
+
+	desc = gpiochip_get_desc(chip, ret);
+	if (IS_ERR(desc))
+		return desc;
 
 	if (xlate_flags & OF_GPIO_ACTIVE_LOW)
 		*lflags |= GPIO_ACTIVE_LOW;
@@ -184,14 +185,14 @@ static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
 		*dflags |= GPIOD_OUT_HIGH;
 	else {
 		pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
-			desc_to_gpio(gg_data.out_gpio), np->name);
+			desc_to_gpio(desc), np->name);
 		return ERR_PTR(-EINVAL);
 	}
 
 	if (name && of_property_read_string(np, "line-name", name))
 		*name = np->name;
 
-	return gg_data.out_gpio;
+	return desc;
 }
 
 /**
@@ -260,7 +261,7 @@ static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
 		if (!of_property_read_bool(np, "gpio-hog"))
 			continue;
 
-		desc = of_parse_own_gpio(np, &name, &lflags, &dflags);
+		desc = of_parse_own_gpio(np, chip, &name, &lflags, &dflags);
 		if (IS_ERR(desc))
 			continue;
 
-- 
1.9.1

[toc] | [prev] | [next] | [standalone]


#1429527 — Re: [PATCH 2/5] gpio: of: drop needless gpio_chip look-up in of_parse_own_gpio()

FromLinus Walleij <linus.walleij@linaro.org>
Date2016-06-23 09:40 +0200
SubjectRe: [PATCH 2/5] gpio: of: drop needless gpio_chip look-up in of_parse_own_gpio()
Message-ID<rN7oZ-6Je-1@gated-at.bofh.it>
In reply to#1421733
On Tue, Jun 14, 2016 at 12:07 PM, Masahiro Yamada
<yamada.masahiro@socionext.com> wrote:

> This function is doing more complicated than needed.  The caller of
> this function, of_gpiochip_scan_gpios() already knows the pointer to
> the gpio_chip.  It can pass it to of_parse_own_gpio() instead of
> looking up the gpio_chip by gpiochip_find().
>
> Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>

Patch applied!

First I was worried about this semantic change:

> -       chip_np = np->parent;
> +       chip_np = chip->of_node;

But then I read up on the code and saw that of_gpiochip_add() sets
it to the parent if not set before.

Yours,
Linus Walleij

[toc] | [prev] | [next] | [standalone]


#1421736 — [PATCH 4/5] gpio: of: remove of_gpiochip_and_xlate() and struct gg_data

FromMasahiro Yamada <yamada.masahiro@socionext.com>
Date2016-06-14 12:10 +0200
Subject[PATCH 4/5] gpio: of: remove of_gpiochip_and_xlate() and struct gg_data
Message-ID<rJTse-3r2-23@gated-at.bofh.it>
In reply to#1421732
The usage of gpiochip_find(&gg_data, of_gpiochip_and_xlate) is odd.

Usually gpiochip_find() is used to find a gpio_chip.  Here, however,
the return value from gpiochip_find() is just discarded.  Instead,
gpiochip_find(&gg_data, of_gpiochip_and_xlate) is called for the
side-effect of the match function.

The match function, of_gpiochip_find_and_xlate(), fills the given
struct gg_data, but a match function should be simply called to
judge the matching.

This commit fixes this distortion and makes the code more readable.
Remove of_gpiochip_find_and_xlate() and struct gg_data.  Instead,
this adds a very simple helper function of_find_gpiochip_by_node().
Now, of_get_named_gpiod_flags() is implemented more straight-forward.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
---

 drivers/gpio/gpiolib-of.c | 81 ++++++++++++++++++++---------------------------
 1 file changed, 35 insertions(+), 46 deletions(-)

diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
index a5a5bc7..d5e19f6 100644
--- a/drivers/gpio/gpiolib-of.c
+++ b/drivers/gpio/gpiolib-of.c
@@ -27,38 +27,14 @@
 
 #include "gpiolib.h"
 
-/* Private data structure for of_gpiochip_find_and_xlate */
-struct gg_data {
-	enum of_gpio_flags *flags;
-	struct of_phandle_args gpiospec;
-
-	struct gpio_desc *out_gpio;
-};
-
-/* Private function for resolving node pointer to gpio_chip */
-static int of_gpiochip_find_and_xlate(struct gpio_chip *gc, void *data)
+static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
 {
-	struct gg_data *gg_data = data;
-	int ret;
-
-	if ((gc->of_node != gg_data->gpiospec.np) ||
-	    (gc->of_gpio_n_cells != gg_data->gpiospec.args_count) ||
-	    (!gc->of_xlate))
-		return false;
+	return chip->gpiodev->dev.of_node == data;
+}
 
-	ret = gc->of_xlate(gc, &gg_data->gpiospec, gg_data->flags);
-	if (ret < 0) {
-		/* We've found a gpio chip, but the translation failed.
-		 * Store translation error in out_gpio.
-		 * Return false to keep looking, as more than one gpio chip
-		 * could be registered per of-node.
-		 */
-		gg_data->out_gpio = ERR_PTR(ret);
-		return false;
-	 }
-
-	gg_data->out_gpio = gpiochip_get_desc(gc, ret);
-	return true;
+static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
+{
+	return gpiochip_find(np, of_gpiochip_match_node);
 }
 
 /**
@@ -75,34 +51,47 @@ static int of_gpiochip_find_and_xlate(struct gpio_chip *gc, void *data)
 struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
 		     const char *propname, int index, enum of_gpio_flags *flags)
 {
-	/* Return -EPROBE_DEFER to support probe() functions to be called
-	 * later when the GPIO actually becomes available
-	 */
-	struct gg_data gg_data = {
-		.flags = flags,
-		.out_gpio = ERR_PTR(-EPROBE_DEFER)
-	};
+	struct of_phandle_args gpiospec;
+	struct gpio_chip *chip;
+	struct gpio_desc *desc;
 	int ret;
 
-	/* .of_xlate might decide to not fill in the flags, so clear it. */
-	if (flags)
-		*flags = 0;
-
 	ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
-					 &gg_data.gpiospec);
+					 &gpiospec);
 	if (ret) {
 		pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n",
 			__func__, propname, np->full_name, index);
 		return ERR_PTR(ret);
 	}
 
-	gpiochip_find(&gg_data, of_gpiochip_find_and_xlate);
+	chip = of_find_gpiochip_by_node(gpiospec.np);
+	if (!chip) {
+		desc = ERR_PTR(-EPROBE_DEFER);
+		goto out;
+	}
+	if (chip->of_gpio_n_cells != gpiospec.args_count) {
+		desc = ERR_PTR(-EINVAL);
+		goto out;
+	}
+
+	ret = chip->of_xlate(chip, &gpiospec, flags);
+	if (ret < 0) {
+		desc = ERR_PTR(ret);
+		goto out;
+	}
+
+	desc = gpiochip_get_desc(chip, ret);
+	if (IS_ERR(desc))
+		goto out;
 
-	of_node_put(gg_data.gpiospec.np);
 	pr_debug("%s: parsed '%s' property of node '%s[%d]' - status (%d)\n",
 		 __func__, propname, np->full_name, index,
-		 PTR_ERR_OR_ZERO(gg_data.out_gpio));
-	return gg_data.out_gpio;
+		 PTR_ERR_OR_ZERO(desc));
+
+out:
+	of_node_put(gpiospec.np);
+
+	return desc;
 }
 
 int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
-- 
1.9.1

[toc] | [prev] | [next] | [standalone]


#1429540 — Re: [PATCH 4/5] gpio: of: remove of_gpiochip_and_xlate() and struct gg_data

FromLinus Walleij <linus.walleij@linaro.org>
Date2016-06-23 09:50 +0200
SubjectRe: [PATCH 4/5] gpio: of: remove of_gpiochip_and_xlate() and struct gg_data
Message-ID<rN7yG-6MX-43@gated-at.bofh.it>
In reply to#1421736
On Tue, Jun 14, 2016 at 12:07 PM, Masahiro Yamada
<yamada.masahiro@socionext.com> wrote:

> The usage of gpiochip_find(&gg_data, of_gpiochip_and_xlate) is odd.
>
> Usually gpiochip_find() is used to find a gpio_chip.  Here, however,
> the return value from gpiochip_find() is just discarded.  Instead,
> gpiochip_find(&gg_data, of_gpiochip_and_xlate) is called for the
> side-effect of the match function.
>
> The match function, of_gpiochip_find_and_xlate(), fills the given
> struct gg_data, but a match function should be simply called to
> judge the matching.
>
> This commit fixes this distortion and makes the code more readable.
> Remove of_gpiochip_find_and_xlate() and struct gg_data.  Instead,
> this adds a very simple helper function of_find_gpiochip_by_node().
> Now, of_get_named_gpiod_flags() is implemented more straight-forward.
>
> Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>

Excellent patch, this always confused me too. Now I understand
the code.

Patch applied.

Yours,
Linus Walleij

[toc] | [prev] | [next] | [standalone]


#1429544

FromLinus Walleij <linus.walleij@linaro.org>
Date2016-06-23 09:50 +0200
Message-ID<rN7yG-6MX-47@gated-at.bofh.it>
In reply to#1421732
On Tue, Jun 14, 2016 at 12:07 PM, Masahiro Yamada
<yamada.masahiro@socionext.com> wrote:

> But, after reading this code around, I thought it was more complex
> than needed.  So, I decided to clean-up it instead of fix the bug.
>
> Talking about of_parse_own_gpio(), does it need to call gpiochip_find()
> in the first place?
>
> The caller of this function, of_gpiochip_scan_gpios() already knows the
> gpio_chip.  I want to make the implementation more straight-forward.

Thanks a lot for this patch series, it eases maintenance of the gpiolib
OF core a lot, and makes the code size smaller too. I bet it also
gives better speed.

This is a very valuable contribution from you and Socionext to anyone
using GPIO with DT.

Yours,
Linus Walleij

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web