[RFC 05/15] gpio: move DT parsing code to separate functions

2015-01-13 Thread Linus Walleij
On Wed, Dec 10, 2014 at 4:48 PM, Andrzej Hajda  wrote:

> The patch moves Device Tree parsing logic to separate
> function.
>
> Signed-off-by: Andrzej Hajda 

I think this patch need to be rebased on the GPIO devel branch, but
looks like it makes things cleaner so can stand on its own merits.
We moves stuff around a bit in the OF code though.

Please remember to send this patch to the linux-gpio list and comaintainer
Alexandre Courbot on reposts. (Cc-tags in the patch are good
for this.)

Yours,
Linus Walleij


[RFC 05/15] gpio: move DT parsing code to separate functions

2014-12-10 Thread Andrzej Hajda
The patch moves Device Tree parsing logic to separate
function.

Signed-off-by: Andrzej Hajda 
---
 drivers/gpio/gpiolib-of.c | 59 +--
 drivers/gpio/gpiolib.c| 33 +++---
 drivers/gpio/gpiolib.h|  4 ++--
 3 files changed, 53 insertions(+), 43 deletions(-)

diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
index 604dbe6..4707727 100644
--- a/drivers/gpio/gpiolib-of.c
+++ b/drivers/gpio/gpiolib-of.c
@@ -28,7 +28,7 @@
 /* Private data structure for of_gpiochip_find_and_xlate */
 struct gg_data {
enum of_gpio_flags *flags;
-   struct of_phandle_args gpiospec;
+   struct of_phandle_args *gpiospec;

struct gpio_desc *out_gpio;
 };
@@ -39,12 +39,12 @@ static int of_gpiochip_find_and_xlate(struct gpio_chip *gc, 
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) ||
+   if ((gc->of_node != gg_data->gpiospec->np) ||
+   (gc->of_gpio_n_cells != gg_data->gpiospec->args_count) ||
(!gc->of_xlate))
return false;

-   ret = gc->of_xlate(gc, _data->gpiospec, gg_data->flags);
+   ret = gc->of_xlate(gc, gg_data->gpiospec, gg_data->flags);
if (ret < 0)
return false;

@@ -52,61 +52,54 @@ static int of_gpiochip_find_and_xlate(struct gpio_chip *gc, 
void *data)
return true;
 }

-/**
- * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
- * @np:device node to get GPIO from
- * @propname:  property name containing gpio specifier(s)
- * @index: index of the GPIO
- * @flags: a flags pointer to fill in
- *
- * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
- * value on the error condition. If @flags is not NULL the function also fills
- * in flags for the GPIO.
- */
-struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
-const char *propname, int index, enum of_gpio_flags *flags)
+struct gpio_desc *of_get_gpiod_by_spec(struct of_phandle_args *spec,
+  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)
+   .out_gpio = ERR_PTR(-EPROBE_DEFER),
+   .gpiospec = spec
};
-   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,
-_data.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(_data, of_gpiochip_find_and_xlate);

-   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,
+   pr_debug("%s: parsed property of node '%s[%d]' - status (%d)\n",
+__func__, spec->np->full_name, spec->args[0],
 PTR_ERR_OR_ZERO(gg_data.out_gpio));
+
return gg_data.out_gpio;
 }

 int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
int index, enum of_gpio_flags *flags)
 {
+   struct of_phandle_args spec;
struct gpio_desc *desc;
+   int ret;
+
+   ret = of_parse_phandle_with_args(np, list_name, "#gpio-cells", index,
+);
+   if (ret) {
+   pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n",
+   __func__, list_name, np->full_name, index);
+   return ret;
+   }

-   desc = of_get_named_gpiod_flags(np, list_name, index, flags);
+   desc = of_get_gpiod_by_spec(, flags);

if (IS_ERR(desc))
return PTR_ERR(desc);
-   else
-   return desc_to_gpio(desc);
+
+   of_node_put(spec.np);
+
+   return desc_to_gpio(desc);
 }
 EXPORT_SYMBOL(of_get_named_gpio_flags);

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index e8e98ca..78fcec9 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -1470,15 +1470,13 @@ void gpiod_add_lookup_table(struct gpiod_lookup_table 
*table)
mutex_unlock(_lookup_lock);
 }

-static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
- unsigned int idx,
- enum gpio_lookup_flags *flags)
+static int of_get_gpiod_spec(struct