* [PATCH 1/5] gpio: of: optimize "gpios" property parsing of of_parse_own_gpio()
2016-06-14 10:07 [PATCH 0/5] gpio: refactor of_parse_own_gpio() and of_get_named_gpiod_flags() Masahiro Yamada
@ 2016-06-14 10:07 ` Masahiro Yamada
2016-06-23 7:23 ` Linus Walleij
2016-06-14 10:07 ` [PATCH 2/5] gpio: of: drop needless gpio_chip look-up in of_parse_own_gpio() Masahiro Yamada
` (4 subsequent siblings)
5 siblings, 1 reply; 12+ messages in thread
From: Masahiro Yamada @ 2016-06-14 10:07 UTC (permalink / raw)
To: linux-gpio
Cc: Masahiro Yamada, Linus Walleij, Alexandre Courbot, linux-kernel
Call of_property_read_u32_array() only once rather than iterating
of_property_read_u32_index().
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
---
drivers/gpio/gpiolib-of.c | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
index 0e963ef..99fc40e 100644
--- a/drivers/gpio/gpiolib-of.c
+++ b/drivers/gpio/gpiolib-of.c
@@ -141,7 +141,7 @@ static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
.flags = &xlate_flags,
};
u32 tmp;
- int i, ret;
+ int ret;
chip_np = np->parent;
if (!chip_np)
@@ -160,12 +160,10 @@ static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
gg_data.gpiospec.args_count = tmp;
gg_data.gpiospec.np = chip_np;
- for (i = 0; i < tmp; i++) {
- ret = of_property_read_u32_index(np, "gpios", i,
- &gg_data.gpiospec.args[i]);
- if (ret)
- return ERR_PTR(ret);
- }
+ ret = of_property_read_u32_array(np, "gpios", gg_data.gpiospec.args,
+ tmp);
+ if (ret)
+ return ERR_PTR(ret);
gpiochip_find(&gg_data, of_gpiochip_find_and_xlate);
if (!gg_data.out_gpio) {
--
1.9.1
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH 2/5] gpio: of: drop needless gpio_chip look-up in of_parse_own_gpio()
2016-06-14 10:07 [PATCH 0/5] gpio: refactor of_parse_own_gpio() and of_get_named_gpiod_flags() Masahiro Yamada
2016-06-14 10:07 ` [PATCH 1/5] gpio: of: optimize "gpios" property parsing of of_parse_own_gpio() Masahiro Yamada
@ 2016-06-14 10:07 ` Masahiro Yamada
2016-06-23 7:34 ` Linus Walleij
2016-06-14 10:07 ` [PATCH 3/5] gpio: of: move chip->of_gpio_n_cells checking to of_gpiochip_add() Masahiro Yamada
` (3 subsequent siblings)
5 siblings, 1 reply; 12+ messages in thread
From: Masahiro Yamada @ 2016-06-14 10:07 UTC (permalink / raw)
To: linux-gpio
Cc: Masahiro Yamada, Linus Walleij, Alexandre Courbot, linux-kernel
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
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH 2/5] gpio: of: drop needless gpio_chip look-up in of_parse_own_gpio()
2016-06-14 10:07 ` [PATCH 2/5] gpio: of: drop needless gpio_chip look-up in of_parse_own_gpio() Masahiro Yamada
@ 2016-06-23 7:34 ` Linus Walleij
0 siblings, 0 replies; 12+ messages in thread
From: Linus Walleij @ 2016-06-23 7:34 UTC (permalink / raw)
To: Masahiro Yamada; +Cc: linux-gpio, Alexandre Courbot, linux-kernel
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
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH 3/5] gpio: of: move chip->of_gpio_n_cells checking to of_gpiochip_add()
2016-06-14 10:07 [PATCH 0/5] gpio: refactor of_parse_own_gpio() and of_get_named_gpiod_flags() Masahiro Yamada
2016-06-14 10:07 ` [PATCH 1/5] gpio: of: optimize "gpios" property parsing of of_parse_own_gpio() Masahiro Yamada
2016-06-14 10:07 ` [PATCH 2/5] gpio: of: drop needless gpio_chip look-up in of_parse_own_gpio() Masahiro Yamada
@ 2016-06-14 10:07 ` Masahiro Yamada
2016-06-23 7:36 ` Linus Walleij
2016-06-14 10:07 ` [PATCH 4/5] gpio: of: remove of_gpiochip_and_xlate() and struct gg_data Masahiro Yamada
` (2 subsequent siblings)
5 siblings, 1 reply; 12+ messages in thread
From: Masahiro Yamada @ 2016-06-14 10:07 UTC (permalink / raw)
To: linux-gpio
Cc: Masahiro Yamada, Linus Walleij, Alexandre Courbot, linux-kernel
Do this sanity check only once when the gpio_chip is added
rather than every time gpio-hog is handled.
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
---
drivers/gpio/gpiolib-of.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
index 27b1729..a5a5bc7 100644
--- a/drivers/gpio/gpiolib-of.c
+++ b/drivers/gpio/gpiolib-of.c
@@ -156,7 +156,7 @@ static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
if (ret)
return ERR_PTR(ret);
- if (tmp > MAX_PHANDLE_ARGS || tmp != chip->of_gpio_n_cells)
+ if (tmp != chip->of_gpio_n_cells)
return ERR_PTR(-EINVAL);
gpiospec.np = chip_np;
@@ -487,6 +487,9 @@ int of_gpiochip_add(struct gpio_chip *chip)
chip->of_xlate = of_gpio_simple_xlate;
}
+ if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
+ return -EINVAL;
+
status = of_gpiochip_add_pin_range(chip);
if (status)
return status;
--
1.9.1
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH 4/5] gpio: of: remove of_gpiochip_and_xlate() and struct gg_data
2016-06-14 10:07 [PATCH 0/5] gpio: refactor of_parse_own_gpio() and of_get_named_gpiod_flags() Masahiro Yamada
` (2 preceding siblings ...)
2016-06-14 10:07 ` [PATCH 3/5] gpio: of: move chip->of_gpio_n_cells checking to of_gpiochip_add() Masahiro Yamada
@ 2016-06-14 10:07 ` Masahiro Yamada
2016-06-23 7:41 ` Linus Walleij
2016-06-14 10:07 ` [PATCH 5/5] gpio: of: factor out common code to a new helper function Masahiro Yamada
2016-06-23 7:44 ` [PATCH 0/5] gpio: refactor of_parse_own_gpio() and of_get_named_gpiod_flags() Linus Walleij
5 siblings, 1 reply; 12+ messages in thread
From: Masahiro Yamada @ 2016-06-14 10:07 UTC (permalink / raw)
To: linux-gpio
Cc: Masahiro Yamada, Linus Walleij, Alexandre Courbot, linux-kernel
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
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH 4/5] gpio: of: remove of_gpiochip_and_xlate() and struct gg_data
2016-06-14 10:07 ` [PATCH 4/5] gpio: of: remove of_gpiochip_and_xlate() and struct gg_data Masahiro Yamada
@ 2016-06-23 7:41 ` Linus Walleij
0 siblings, 0 replies; 12+ messages in thread
From: Linus Walleij @ 2016-06-23 7:41 UTC (permalink / raw)
To: Masahiro Yamada; +Cc: linux-gpio, Alexandre Courbot, linux-kernel
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
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH 5/5] gpio: of: factor out common code to a new helper function
2016-06-14 10:07 [PATCH 0/5] gpio: refactor of_parse_own_gpio() and of_get_named_gpiod_flags() Masahiro Yamada
` (3 preceding siblings ...)
2016-06-14 10:07 ` [PATCH 4/5] gpio: of: remove of_gpiochip_and_xlate() and struct gg_data Masahiro Yamada
@ 2016-06-14 10:07 ` Masahiro Yamada
2016-06-23 7:42 ` Linus Walleij
2016-06-23 7:44 ` [PATCH 0/5] gpio: refactor of_parse_own_gpio() and of_get_named_gpiod_flags() Linus Walleij
5 siblings, 1 reply; 12+ messages in thread
From: Masahiro Yamada @ 2016-06-14 10:07 UTC (permalink / raw)
To: linux-gpio
Cc: Masahiro Yamada, Linus Walleij, Alexandre Courbot, linux-kernel
The conversion from a DT spec to struct gpio_desc is common between
of_get_named_gpiod_flags() and of_parse_own_gpio(). Factor out the
common code to a new helper, of_xlate_and_get_gpiod_flags().
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
---
drivers/gpio/gpiolib-of.c | 37 ++++++++++++++++++-------------------
1 file changed, 18 insertions(+), 19 deletions(-)
diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
index d5e19f6..75e7b39 100644
--- a/drivers/gpio/gpiolib-of.c
+++ b/drivers/gpio/gpiolib-of.c
@@ -37,6 +37,22 @@ static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
return gpiochip_find(np, of_gpiochip_match_node);
}
+static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
+ struct of_phandle_args *gpiospec,
+ enum of_gpio_flags *flags)
+{
+ int ret;
+
+ if (chip->of_gpio_n_cells != gpiospec->args_count)
+ return ERR_PTR(-EINVAL);
+
+ ret = chip->of_xlate(chip, gpiospec, flags);
+ if (ret < 0)
+ return ERR_PTR(ret);
+
+ return gpiochip_get_desc(chip, ret);
+}
+
/**
* of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
* @np: device node to get GPIO from
@@ -69,18 +85,8 @@ struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
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);
+ desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
if (IS_ERR(desc))
goto out;
@@ -145,9 +151,6 @@ static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
if (ret)
return ERR_PTR(ret);
- if (tmp != chip->of_gpio_n_cells)
- return ERR_PTR(-EINVAL);
-
gpiospec.np = chip_np;
gpiospec.args_count = tmp;
@@ -155,11 +158,7 @@ static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
if (ret)
return ERR_PTR(ret);
- ret = chip->of_xlate(chip, &gpiospec, &xlate_flags);
- if (ret < 0)
- return ERR_PTR(ret);
-
- desc = gpiochip_get_desc(chip, ret);
+ desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
if (IS_ERR(desc))
return desc;
--
1.9.1
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH 0/5] gpio: refactor of_parse_own_gpio() and of_get_named_gpiod_flags()
2016-06-14 10:07 [PATCH 0/5] gpio: refactor of_parse_own_gpio() and of_get_named_gpiod_flags() Masahiro Yamada
` (4 preceding siblings ...)
2016-06-14 10:07 ` [PATCH 5/5] gpio: of: factor out common code to a new helper function Masahiro Yamada
@ 2016-06-23 7:44 ` Linus Walleij
5 siblings, 0 replies; 12+ messages in thread
From: Linus Walleij @ 2016-06-23 7:44 UTC (permalink / raw)
To: Masahiro Yamada; +Cc: linux-gpio, Alexandre Courbot, linux-kernel
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
^ permalink raw reply [flat|nested] 12+ messages in thread