Skip to content

Commit

Permalink
gpiolib: add gpiochip_get_desc() driver function
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Alexandre Courbot authored and Linus Walleij committed Feb 12, 2014
1 parent 90df4fe commit bb1e88c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
17 changes: 9 additions & 8 deletions drivers/gpio/gpiolib.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,17 @@ struct gpio_desc *gpio_to_desc(unsigned gpio)
EXPORT_SYMBOL_GPL(gpio_to_desc);

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

return &chip->desc[offset];
return &chip->desc[hwnum];
}
EXPORT_SYMBOL_GPL(gpiochip_get_desc);

/**
* Convert a GPIO descriptor to the integer namespace.
Expand Down Expand Up @@ -2190,7 +2191,7 @@ EXPORT_SYMBOL_GPL(gpiod_lock_as_irq);

int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
{
return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset));
return gpiod_lock_as_irq(gpiochip_get_desc(chip, offset));
}
EXPORT_SYMBOL_GPL(gpio_lock_as_irq);

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

void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
{
return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset));
return gpiod_unlock_as_irq(gpiochip_get_desc(chip, offset));
}
EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);

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

desc = gpiochip_offset_to_desc(chip, p->chip_hwnum);
desc = gpiochip_get_desc(chip, p->chip_hwnum);
*flags = p->flags;

return desc;
Expand Down
8 changes: 0 additions & 8 deletions include/linux/gpio/consumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#ifdef CONFIG_GPIOLIB

struct device;
struct gpio_chip;

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

#else /* CONFIG_GPIOLIB */

Expand Down Expand Up @@ -214,12 +212,6 @@ static inline int desc_to_gpio(const struct gpio_desc *desc)
WARN_ON(1);
return -EINVAL;
}
static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
{
/* GPIO can never have been requested */
WARN_ON(1);
return ERR_PTR(-ENODEV);
}


#endif /* CONFIG_GPIOLIB */
Expand Down
18 changes: 18 additions & 0 deletions include/linux/gpio/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ struct of_phandle_args;
struct device_node;
struct seq_file;

#ifdef CONFIG_GPIOLIB

/**
* struct gpio_chip - abstract a GPIO controller
* @label: for diagnostics
Expand Down Expand Up @@ -129,6 +131,11 @@ extern struct gpio_chip *gpiochip_find(void *data,
int gpiod_lock_as_irq(struct gpio_desc *desc);
void gpiod_unlock_as_irq(struct gpio_desc *desc);

struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);

struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
u16 hwnum);

enum gpio_lookup_flags {
GPIO_ACTIVE_HIGH = (0 << 0),
GPIO_ACTIVE_LOW = (1 << 0),
Expand Down Expand Up @@ -183,4 +190,15 @@ struct gpiod_lookup_table {

void gpiod_add_lookup_table(struct gpiod_lookup_table *table);

#else /* CONFIG_GPIOLIB */

static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
{
/* GPIO can never have been requested */
WARN_ON(1);
return ERR_PTR(-ENODEV);
}

#endif /* CONFIG_GPIOLIB */

#endif

0 comments on commit bb1e88c

Please sign in to comment.