Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bb1e88c

Browse files
Gnuroulinusw
authored andcommittedFeb 12, 2014
gpiolib: add gpiochip_get_desc() driver function
Some drivers dealing with a gpio_chip might need to act on its descriptors directly; one example is pinctrl drivers that need to lock a GPIO for being used as IRQ using gpiod_lock_as_irq(). This patch exports a gpiochip_get_desc() function that returns the GPIO descriptor at the requested index. It also sweeps the gpio_to_chip() function out of the consumer interface since any holder of a gpio_chip reference can manipulate its GPIOs way beyond what a consumer should be allowed to do. As a result, gpio_chip is not visible anymore to simple GPIO consumers. Signed-off-by: Alexandre Courbot <acourbot@nvidia.com> Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com> Reviewed-by: Jean-Jacques Hiblot <jjhiblot@traphandler.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 90df4fe commit bb1e88c

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed
 

‎drivers/gpio/gpiolib.c‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,17 @@ struct gpio_desc *gpio_to_desc(unsigned gpio)
164164
EXPORT_SYMBOL_GPL(gpio_to_desc);
165165

166166
/**
167-
* Convert an offset on a certain chip to a corresponding descriptor
167+
* Get the GPIO descriptor corresponding to the given hw number for this chip.
168168
*/
169-
static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip,
170-
unsigned int offset)
169+
struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
170+
u16 hwnum)
171171
{
172-
if (offset >= chip->ngpio)
172+
if (hwnum >= chip->ngpio)
173173
return ERR_PTR(-EINVAL);
174174

175-
return &chip->desc[offset];
175+
return &chip->desc[hwnum];
176176
}
177+
EXPORT_SYMBOL_GPL(gpiochip_get_desc);
177178

178179
/**
179180
* Convert a GPIO descriptor to the integer namespace.
@@ -2190,7 +2191,7 @@ EXPORT_SYMBOL_GPL(gpiod_lock_as_irq);
21902191

21912192
int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
21922193
{
2193-
return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset));
2194+
return gpiod_lock_as_irq(gpiochip_get_desc(chip, offset));
21942195
}
21952196
EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
21962197

@@ -2212,7 +2213,7 @@ EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq);
22122213

22132214
void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
22142215
{
2215-
return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset));
2216+
return gpiod_unlock_as_irq(gpiochip_get_desc(chip, offset));
22162217
}
22172218
EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
22182219

@@ -2433,7 +2434,7 @@ static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
24332434
return ERR_PTR(-EINVAL);
24342435
}
24352436

2436-
desc = gpiochip_offset_to_desc(chip, p->chip_hwnum);
2437+
desc = gpiochip_get_desc(chip, p->chip_hwnum);
24372438
*flags = p->flags;
24382439

24392440
return desc;

‎include/linux/gpio/consumer.h‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#ifdef CONFIG_GPIOLIB
88

99
struct device;
10-
struct gpio_chip;
1110

1211
/**
1312
* Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are
@@ -60,7 +59,6 @@ int gpiod_to_irq(const struct gpio_desc *desc);
6059
/* Convert between the old gpio_ and new gpiod_ interfaces */
6160
struct gpio_desc *gpio_to_desc(unsigned gpio);
6261
int desc_to_gpio(const struct gpio_desc *desc);
63-
struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
6462

6563
#else /* CONFIG_GPIOLIB */
6664

@@ -214,12 +212,6 @@ static inline int desc_to_gpio(const struct gpio_desc *desc)
214212
WARN_ON(1);
215213
return -EINVAL;
216214
}
217-
static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
218-
{
219-
/* GPIO can never have been requested */
220-
WARN_ON(1);
221-
return ERR_PTR(-ENODEV);
222-
}
223215

224216

225217
#endif /* CONFIG_GPIOLIB */

‎include/linux/gpio/driver.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ struct of_phandle_args;
1010
struct device_node;
1111
struct seq_file;
1212

13+
#ifdef CONFIG_GPIOLIB
14+
1315
/**
1416
* struct gpio_chip - abstract a GPIO controller
1517
* @label: for diagnostics
@@ -129,6 +131,11 @@ extern struct gpio_chip *gpiochip_find(void *data,
129131
int gpiod_lock_as_irq(struct gpio_desc *desc);
130132
void gpiod_unlock_as_irq(struct gpio_desc *desc);
131133

134+
struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
135+
136+
struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
137+
u16 hwnum);
138+
132139
enum gpio_lookup_flags {
133140
GPIO_ACTIVE_HIGH = (0 << 0),
134141
GPIO_ACTIVE_LOW = (1 << 0),
@@ -183,4 +190,15 @@ struct gpiod_lookup_table {
183190

184191
void gpiod_add_lookup_table(struct gpiod_lookup_table *table);
185192

193+
#else /* CONFIG_GPIOLIB */
194+
195+
static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
196+
{
197+
/* GPIO can never have been requested */
198+
WARN_ON(1);
199+
return ERR_PTR(-ENODEV);
200+
}
201+
202+
#endif /* CONFIG_GPIOLIB */
203+
186204
#endif

0 commit comments

Comments
 (0)
Failed to load comments.