Skip to content

Commit 13949fa

Browse files
dtorLinus Walleij
authored andcommitted
gpiolib: introduce fwnode_gpiod_get_index()
This introduces fwnode_gpiod_get_index() that iterates through common gpio suffixes when trying to locate a GPIO within a given firmware node. We also switch devm_fwnode_gpiod_get_index() to call fwnode_gpiod_get_index() instead of iterating through GPIO suffixes on its own. Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com> Link: https://lore.kernel.org/r/20190913032240.50333-3-dmitry.torokhov@gmail.com Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 2d2f116 commit 13949fa

3 files changed

Lines changed: 62 additions & 15 deletions

File tree

‎drivers/gpio/gpiolib-devres.c‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -205,29 +205,15 @@ struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
205205
enum gpiod_flags flags,
206206
const char *label)
207207
{
208-
char prop_name[32]; /* 32 is max size of property name */
209208
struct gpio_desc **dr;
210209
struct gpio_desc *desc;
211-
unsigned int i;
212210

213211
dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
214212
GFP_KERNEL);
215213
if (!dr)
216214
return ERR_PTR(-ENOMEM);
217215

218-
for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
219-
if (con_id)
220-
snprintf(prop_name, sizeof(prop_name), "%s-%s",
221-
con_id, gpio_suffixes[i]);
222-
else
223-
snprintf(prop_name, sizeof(prop_name), "%s",
224-
gpio_suffixes[i]);
225-
226-
desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
227-
label);
228-
if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
229-
break;
230-
}
216+
desc = fwnode_gpiod_get_index(fwnode, con_id, index, flags, label);
231217
if (IS_ERR(desc)) {
232218
devres_free(dr);
233219
return desc;

‎drivers/gpio/gpiolib.c‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4324,6 +4324,54 @@ static int platform_gpio_count(struct device *dev, const char *con_id)
43244324
return count;
43254325
}
43264326

4327+
/**
4328+
* fwnode_gpiod_get_index - obtain a GPIO from firmware node
4329+
* @fwnode: handle of the firmware node
4330+
* @con_id: function within the GPIO consumer
4331+
* @index: index of the GPIO to obtain for the consumer
4332+
* @flags: GPIO initialization flags
4333+
* @label: label to attach to the requested GPIO
4334+
*
4335+
* This function can be used for drivers that get their configuration
4336+
* from opaque firmware.
4337+
*
4338+
* The function properly finds the corresponding GPIO using whatever is the
4339+
* underlying firmware interface and then makes sure that the GPIO
4340+
* descriptor is requested before it is returned to the caller.
4341+
*
4342+
* Returns:
4343+
* On successful request the GPIO pin is configured in accordance with
4344+
* provided @flags.
4345+
*
4346+
* In case of error an ERR_PTR() is returned.
4347+
*/
4348+
struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
4349+
const char *con_id, int index,
4350+
enum gpiod_flags flags,
4351+
const char *label)
4352+
{
4353+
struct gpio_desc *desc;
4354+
char prop_name[32]; /* 32 is max size of property name */
4355+
unsigned int i;
4356+
4357+
for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
4358+
if (con_id)
4359+
snprintf(prop_name, sizeof(prop_name), "%s-%s",
4360+
con_id, gpio_suffixes[i]);
4361+
else
4362+
snprintf(prop_name, sizeof(prop_name), "%s",
4363+
gpio_suffixes[i]);
4364+
4365+
desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
4366+
label);
4367+
if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
4368+
break;
4369+
}
4370+
4371+
return desc;
4372+
}
4373+
EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
4374+
43274375
/**
43284376
* gpiod_count - return the number of GPIOs associated with a device / function
43294377
* or -ENOENT if no GPIO has been assigned to the requested function

‎include/linux/gpio/consumer.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
176176
const char *propname, int index,
177177
enum gpiod_flags dflags,
178178
const char *label);
179+
struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
180+
const char *con_id, int index,
181+
enum gpiod_flags flags,
182+
const char *label);
179183
struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
180184
struct fwnode_handle *child,
181185
const char *con_id, int index,
@@ -531,6 +535,15 @@ struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
531535
return ERR_PTR(-ENOSYS);
532536
}
533537

538+
static inline
539+
struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
540+
const char *con_id, int index,
541+
enum gpiod_flags flags,
542+
const char *label)
543+
{
544+
return ERR_PTR(-ENOSYS);
545+
}
546+
534547
static inline
535548
struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
536549
struct fwnode_handle *fwnode,

0 commit comments

Comments
 (0)