Skip to content

Commit c0017ed

Browse files
Markus Pargmannlinusw
authored andcommitted
gpio: Introduce gpio descriptor 'name'
The latest gpio hogging mechanism assigns each gpio a 'line-name' in the devicetree. The 'name' field is different from the 'label' field. 'label' is only used for requested GPIOs to describe its current use by driver or userspace. The 'name' field describes the GPIO itself, not the use. This is most likely identical to the label in the schematic on the GPIO line and should help to find this particular GPIO. This is equivalent to the gpiochip->names array. However names should be stored in the GPIO descriptor. We will use gpiochip->names in the future only as initializer for the GPIO descriptors for drivers that assign GPIO names hardcoded. All other GPIO names will be parsed from DT and directly assigned to the GPIO descriptor. This patch adds a helper function to find gpio descriptors by name instead of gpio number. Signed-off-by: Markus Pargmann <[email protected]> Signed-off-by: Linus Walleij <[email protected]>
1 parent fd7337f commit c0017ed

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

drivers/gpio/gpiolib.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,38 @@ struct gpio_desc *gpio_to_desc(unsigned gpio)
8989
}
9090
EXPORT_SYMBOL_GPL(gpio_to_desc);
9191

92+
/**
93+
* Convert a GPIO name to its descriptor
94+
*/
95+
struct gpio_desc *gpio_name_to_desc(const char * const name)
96+
{
97+
struct gpio_chip *chip;
98+
unsigned long flags;
99+
100+
spin_lock_irqsave(&gpio_lock, flags);
101+
102+
list_for_each_entry(chip, &gpio_chips, list) {
103+
int i;
104+
105+
for (i = 0; i != chip->ngpio; ++i) {
106+
struct gpio_desc *gpio = &chip->desc[i];
107+
108+
if (!gpio->name)
109+
continue;
110+
111+
if (!strcmp(gpio->name, name)) {
112+
spin_unlock_irqrestore(&gpio_lock, flags);
113+
return gpio;
114+
}
115+
}
116+
}
117+
118+
spin_unlock_irqrestore(&gpio_lock, flags);
119+
120+
return NULL;
121+
}
122+
EXPORT_SYMBOL_GPL(gpio_name_to_desc);
123+
92124
/**
93125
* Get the GPIO descriptor corresponding to the given hw number for this chip.
94126
*/

drivers/gpio/gpiolib.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ struct gpio_desc {
8989
#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
9090
#define FLAG_IS_HOGGED 11 /* GPIO is hogged */
9191

92+
/* Connection label */
9293
const char *label;
94+
/* Name of the GPIO */
95+
const char *name;
9396
};
9497

9598
int gpiod_request(struct gpio_desc *desc, const char *label);

include/linux/gpio/consumer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ int gpiod_to_irq(const struct gpio_desc *desc);
130130
/* Convert between the old gpio_ and new gpiod_ interfaces */
131131
struct gpio_desc *gpio_to_desc(unsigned gpio);
132132
int desc_to_gpio(const struct gpio_desc *desc);
133+
struct gpio_desc *gpio_name_to_desc(const char *name);
133134

134135
/* Child properties interface */
135136
struct fwnode_handle;
@@ -400,6 +401,12 @@ static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
400401
{
401402
return ERR_PTR(-EINVAL);
402403
}
404+
405+
static inline struct gpio_desc *gpio_name_to_desc(const char *name)
406+
{
407+
return ERR_PTR(-EINVAL);
408+
}
409+
403410
static inline int desc_to_gpio(const struct gpio_desc *desc)
404411
{
405412
/* GPIO can never have been requested */

0 commit comments

Comments
 (0)