Skip to content

Commit a264d10

Browse files
andy-shevLinus Walleij
authored andcommitted
gpiolib: Convert fwnode_get_named_gpiod() to configure GPIO
Make fwnode_get_named_gpiod() consistent with the rest of gpiod_get() like API, i.e. configure GPIO pin immediately after request. Besides obvious clean up it will help to configure pins based on firmware provided resources. Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 7ce7d89 commit a264d10

7 files changed

Lines changed: 45 additions & 40 deletions

File tree

‎drivers/gpio/devres.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,18 @@ EXPORT_SYMBOL(devm_gpiod_get_index);
127127
* @dev: GPIO consumer
128128
* @con_id: function within the GPIO consumer
129129
* @child: firmware node (child of @dev)
130+
* @flags: GPIO initialization flags
130131
*
131132
* GPIO descriptors returned from this function are automatically disposed on
132133
* driver detach.
134+
*
135+
* On successfull request the GPIO pin is configured in accordance with
136+
* provided @flags.
133137
*/
134138
struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
135139
const char *con_id,
136-
struct fwnode_handle *child)
140+
struct fwnode_handle *child,
141+
enum gpiod_flags flags)
137142
{
138143
static const char * const suffixes[] = { "gpios", "gpio" };
139144
char prop_name[32]; /* 32 is max size of property name */
@@ -154,7 +159,7 @@ struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
154159
snprintf(prop_name, sizeof(prop_name), "%s",
155160
suffixes[i]);
156161

157-
desc = fwnode_get_named_gpiod(child, prop_name);
162+
desc = fwnode_get_named_gpiod(child, prop_name, flags);
158163
if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
159164
break;
160165
}

‎drivers/gpio/gpiolib.c‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3309,6 +3309,7 @@ EXPORT_SYMBOL_GPL(gpiod_get_index);
33093309
* fwnode_get_named_gpiod - obtain a GPIO from firmware node
33103310
* @fwnode: handle of the firmware node
33113311
* @propname: name of the firmware property representing the GPIO
3312+
* @dflags: GPIO initialization flags
33123313
*
33133314
* This function can be used for drivers that get their configuration
33143315
* from firmware.
@@ -3317,12 +3318,17 @@ EXPORT_SYMBOL_GPL(gpiod_get_index);
33173318
* underlying firmware interface and then makes sure that the GPIO
33183319
* descriptor is requested before it is returned to the caller.
33193320
*
3321+
* On successfull request the GPIO pin is configured in accordance with
3322+
* provided @dflags.
3323+
*
33203324
* In case of error an ERR_PTR() is returned.
33213325
*/
33223326
struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3323-
const char *propname)
3327+
const char *propname,
3328+
enum gpiod_flags dflags)
33243329
{
33253330
struct gpio_desc *desc = ERR_PTR(-ENODEV);
3331+
unsigned long lflags = 0;
33263332
bool active_low = false;
33273333
bool single_ended = false;
33283334
int ret;
@@ -3355,13 +3361,19 @@ struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
33553361
return ERR_PTR(ret);
33563362

33573363
if (active_low)
3358-
set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3364+
lflags |= GPIO_ACTIVE_LOW;
33593365

33603366
if (single_ended) {
33613367
if (active_low)
3362-
set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3368+
lflags |= GPIO_OPEN_DRAIN;
33633369
else
3364-
set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3370+
lflags |= GPIO_OPEN_SOURCE;
3371+
}
3372+
3373+
ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3374+
if (ret < 0) {
3375+
gpiod_put(desc);
3376+
return ERR_PTR(ret);
33653377
}
33663378

33673379
return desc;

‎drivers/input/keyboard/gpio_keys.c‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ static int gpio_keys_setup_key(struct platform_device *pdev,
481481
spin_lock_init(&bdata->lock);
482482

483483
if (child) {
484-
bdata->gpiod = devm_get_gpiod_from_child(dev, NULL, child);
484+
bdata->gpiod = devm_get_gpiod_from_child(dev, NULL, child, GPIOD_IN);
485485
if (IS_ERR(bdata->gpiod)) {
486486
error = PTR_ERR(bdata->gpiod);
487487
if (error == -ENOENT) {
@@ -496,13 +496,6 @@ static int gpio_keys_setup_key(struct platform_device *pdev,
496496
error);
497497
return error;
498498
}
499-
} else {
500-
error = gpiod_direction_input(bdata->gpiod);
501-
if (error) {
502-
dev_err(dev, "Failed to configure GPIO %d as input: %d\n",
503-
desc_to_gpio(bdata->gpiod), error);
504-
return error;
505-
}
506499
}
507500
} else if (gpio_is_valid(button->gpio)) {
508501
/*

‎drivers/input/keyboard/gpio_keys_polled.c‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
304304
}
305305

306306
bdata->gpiod = devm_get_gpiod_from_child(dev, NULL,
307-
child);
307+
child,
308+
GPIOD_IN);
308309
if (IS_ERR(bdata->gpiod)) {
309310
error = PTR_ERR(bdata->gpiod);
310311
if (error != -EPROBE_DEFER)
@@ -314,14 +315,6 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
314315
fwnode_handle_put(child);
315316
return error;
316317
}
317-
318-
error = gpiod_direction_input(bdata->gpiod);
319-
if (error) {
320-
dev_err(dev, "Failed to configure GPIO %d as input: %d\n",
321-
desc_to_gpio(bdata->gpiod), error);
322-
fwnode_handle_put(child);
323-
return error;
324-
}
325318
} else if (gpio_is_valid(button->gpio)) {
326319
/*
327320
* Legacy GPIO number so request the GPIO here and

‎drivers/leds/leds-gpio.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
174174
const char *state = NULL;
175175
struct device_node *np = to_of_node(child);
176176

177-
led.gpiod = devm_get_gpiod_from_child(dev, NULL, child);
177+
led.gpiod = devm_get_gpiod_from_child(dev, NULL, child, GPIOD_ASIS);
178178
if (IS_ERR(led.gpiod)) {
179179
fwnode_handle_put(child);
180180
return ERR_CAST(led.gpiod);

‎drivers/video/fbdev/amba-clcd-nomadik.c‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,32 +184,27 @@ static void tpg110_init(struct device *dev, struct device_node *np,
184184
{
185185
dev_info(dev, "TPG110 display init\n");
186186

187-
grestb = devm_get_gpiod_from_child(dev, "grestb", &np->fwnode);
187+
/* This asserts the GRESTB signal, putting the display into reset */
188+
grestb = devm_get_gpiod_from_child(dev, "grestb", &np->fwnode, GPIOD_OUT_HIGH);
188189
if (IS_ERR(grestb)) {
189190
dev_err(dev, "no GRESTB GPIO\n");
190191
return;
191192
}
192-
/* This asserts the GRESTB signal, putting the display into reset */
193-
gpiod_direction_output(grestb, 1);
194-
195-
scen = devm_get_gpiod_from_child(dev, "scen", &np->fwnode);
193+
scen = devm_get_gpiod_from_child(dev, "scen", &np->fwnode, GPIOD_OUT_LOW);
196194
if (IS_ERR(scen)) {
197195
dev_err(dev, "no SCEN GPIO\n");
198196
return;
199197
}
200-
gpiod_direction_output(scen, 0);
201-
scl = devm_get_gpiod_from_child(dev, "scl", &np->fwnode);
198+
scl = devm_get_gpiod_from_child(dev, "scl", &np->fwnode, GPIOD_OUT_LOW);
202199
if (IS_ERR(scl)) {
203200
dev_err(dev, "no SCL GPIO\n");
204201
return;
205202
}
206-
gpiod_direction_output(scl, 0);
207-
sda = devm_get_gpiod_from_child(dev, "sda", &np->fwnode);
203+
sda = devm_get_gpiod_from_child(dev, "sda", &np->fwnode, GPIOD_OUT_LOW);
208204
if (IS_ERR(sda)) {
209205
dev_err(dev, "no SDA GPIO\n");
210206
return;
211207
}
212-
gpiod_direction_output(sda, 0);
213208
board->enable = tpg110_enable;
214209
board->disable = tpg110_disable;
215210
}

‎include/linux/gpio/consumer.h‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,12 @@ int desc_to_gpio(const struct gpio_desc *desc);
135135
struct fwnode_handle;
136136

137137
struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
138-
const char *propname);
138+
const char *propname,
139+
enum gpiod_flags dflags);
139140
struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
140141
const char *con_id,
141-
struct fwnode_handle *child);
142+
struct fwnode_handle *child,
143+
enum gpiod_flags flags);
142144
#else /* CONFIG_GPIOLIB */
143145

144146
static inline int gpiod_count(struct device *dev, const char *con_id)
@@ -411,14 +413,19 @@ static inline int desc_to_gpio(const struct gpio_desc *desc)
411413
/* Child properties interface */
412414
struct fwnode_handle;
413415

414-
static inline struct gpio_desc *fwnode_get_named_gpiod(
415-
struct fwnode_handle *fwnode, const char *propname)
416+
static inline
417+
struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
418+
const char *propname,
419+
enum gpiod_flags dflags)
416420
{
417421
return ERR_PTR(-ENOSYS);
418422
}
419423

420-
static inline struct gpio_desc *devm_get_gpiod_from_child(
421-
struct device *dev, const char *con_id, struct fwnode_handle *child)
424+
static inline
425+
struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
426+
const char *con_id,
427+
struct fwnode_handle *child,
428+
enum gpiod_flags flags)
422429
{
423430
return ERR_PTR(-ENOSYS);
424431
}

0 commit comments

Comments
 (0)