Skip to content

Commit af8b637

Browse files
GnurouLinus Walleij
authored andcommitted
gpiolib: port of_ functions to use gpiod
Refactor the of_ functions of gpiolib to use the now public gpiod interface, and export of_get_named_gpiod_flags() and of_get_gpiod_flags() functions. Signed-off-by: Alexandre Courbot <acourbot@nvidia.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 79a9bec commit af8b637

2 files changed

Lines changed: 41 additions & 16 deletions

File tree

‎drivers/gpio/gpiolib-of.c‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,21 @@
1515
#include <linux/errno.h>
1616
#include <linux/module.h>
1717
#include <linux/io.h>
18-
#include <linux/gpio.h>
18+
#include <linux/gpio/consumer.h>
1919
#include <linux/of.h>
2020
#include <linux/of_address.h>
2121
#include <linux/of_gpio.h>
2222
#include <linux/pinctrl/pinctrl.h>
2323
#include <linux/slab.h>
2424

25+
struct gpio_desc;
26+
2527
/* Private data structure for of_gpiochip_find_and_xlate */
2628
struct gg_data {
2729
enum of_gpio_flags *flags;
2830
struct of_phandle_args gpiospec;
2931

30-
int out_gpio;
32+
struct gpio_desc *out_gpio;
3133
};
3234

3335
/* Private function for resolving node pointer to gpio_chip */
@@ -45,28 +47,31 @@ static int of_gpiochip_find_and_xlate(struct gpio_chip *gc, void *data)
4547
if (ret < 0)
4648
return false;
4749

48-
gg_data->out_gpio = ret + gc->base;
50+
gg_data->out_gpio = gpio_to_desc(ret + gc->base);
4951
return true;
5052
}
5153

5254
/**
53-
* of_get_named_gpio_flags() - Get a GPIO number and flags to use with GPIO API
55+
* of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
5456
* @np: device node to get GPIO from
5557
* @propname: property name containing gpio specifier(s)
5658
* @index: index of the GPIO
5759
* @flags: a flags pointer to fill in
5860
*
59-
* Returns GPIO number to use with Linux generic GPIO API, or one of the errno
61+
* Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
6062
* value on the error condition. If @flags is not NULL the function also fills
6163
* in flags for the GPIO.
6264
*/
63-
int of_get_named_gpio_flags(struct device_node *np, const char *propname,
64-
int index, enum of_gpio_flags *flags)
65+
struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
66+
const char *propname, int index, enum of_gpio_flags *flags)
6567
{
6668
/* Return -EPROBE_DEFER to support probe() functions to be called
6769
* later when the GPIO actually becomes available
6870
*/
69-
struct gg_data gg_data = { .flags = flags, .out_gpio = -EPROBE_DEFER };
71+
struct gg_data gg_data = {
72+
.flags = flags,
73+
.out_gpio = ERR_PTR(-EPROBE_DEFER)
74+
};
7075
int ret;
7176

7277
/* .of_xlate might decide to not fill in the flags, so clear it. */
@@ -78,16 +83,17 @@ int of_get_named_gpio_flags(struct device_node *np, const char *propname,
7883
if (ret) {
7984
pr_debug("%s: can't parse gpios property of node '%s[%d]'\n",
8085
__func__, np->full_name, index);
81-
return ret;
86+
return ERR_PTR(ret);
8287
}
8388

8489
gpiochip_find(&gg_data, of_gpiochip_find_and_xlate);
8590

8691
of_node_put(gg_data.gpiospec.np);
87-
pr_debug("%s exited with status %d\n", __func__, gg_data.out_gpio);
92+
pr_debug("%s exited with status %d\n", __func__,
93+
PTR_RET(gg_data.out_gpio));
8894
return gg_data.out_gpio;
8995
}
90-
EXPORT_SYMBOL(of_get_named_gpio_flags);
96+
EXPORT_SYMBOL(of_get_named_gpiod_flags);
9197

9298
/**
9399
* of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags

‎include/linux/of_gpio.h‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/of.h>
2222

2323
struct device_node;
24+
struct gpio_desc;
2425

2526
/*
2627
* This is Linux-specific flags. By default controllers' and Linux' mapping
@@ -47,7 +48,7 @@ static inline struct of_mm_gpio_chip *to_of_mm_gpio_chip(struct gpio_chip *gc)
4748
return container_of(gc, struct of_mm_gpio_chip, gc);
4849
}
4950

50-
extern int of_get_named_gpio_flags(struct device_node *np,
51+
extern struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
5152
const char *list_name, int index, enum of_gpio_flags *flags);
5253

5354
extern int of_mm_gpiochip_add(struct device_node *np,
@@ -62,10 +63,10 @@ extern int of_gpio_simple_xlate(struct gpio_chip *gc,
6263
#else /* CONFIG_OF_GPIO */
6364

6465
/* Drivers may not strictly depend on the GPIO support, so let them link. */
65-
static inline int of_get_named_gpio_flags(struct device_node *np,
66+
static inline struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
6667
const char *list_name, int index, enum of_gpio_flags *flags)
6768
{
68-
return -ENOSYS;
69+
return ERR_PTR(-ENOSYS);
6970
}
7071

7172
static inline int of_gpio_simple_xlate(struct gpio_chip *gc,
@@ -80,6 +81,18 @@ static inline void of_gpiochip_remove(struct gpio_chip *gc) { }
8081

8182
#endif /* CONFIG_OF_GPIO */
8283

84+
static inline int of_get_named_gpio_flags(struct device_node *np,
85+
const char *list_name, int index, enum of_gpio_flags *flags)
86+
{
87+
struct gpio_desc *desc;
88+
desc = of_get_named_gpiod_flags(np, list_name, index, flags);
89+
90+
if (IS_ERR(desc))
91+
return PTR_ERR(desc);
92+
else
93+
return desc_to_gpio(desc);
94+
}
95+
8396
/**
8497
* of_gpio_named_count() - Count GPIOs for a device
8598
* @np: device node to count GPIOs for
@@ -117,15 +130,21 @@ static inline int of_gpio_count(struct device_node *np)
117130
}
118131

119132
/**
120-
* of_get_gpio_flags() - Get a GPIO number and flags to use with GPIO API
133+
* of_get_gpiod_flags() - Get a GPIO descriptor and flags to use with GPIO API
121134
* @np: device node to get GPIO from
122135
* @index: index of the GPIO
123136
* @flags: a flags pointer to fill in
124137
*
125-
* Returns GPIO number to use with Linux generic GPIO API, or one of the errno
138+
* Returns GPIO descriptor to use with Linux generic GPIO API, or a errno
126139
* value on the error condition. If @flags is not NULL the function also fills
127140
* in flags for the GPIO.
128141
*/
142+
static inline struct gpio_desc *of_get_gpiod_flags(struct device_node *np,
143+
int index, enum of_gpio_flags *flags)
144+
{
145+
return of_get_named_gpiod_flags(np, "gpios", index, flags);
146+
}
147+
129148
static inline int of_get_gpio_flags(struct device_node *np, int index,
130149
enum of_gpio_flags *flags)
131150
{

0 commit comments

Comments
 (0)