Skip to content

Commit d468bf9

Browse files
author
Linus Walleij
committed
gpio: add API to be strict about GPIO IRQ usage
It is currently often possible in many GPIO drivers to request a GPIO line to be used as IRQ after calling gpio_to_irq() and, as the gpiolib is not aware of this, set the same line to output and start driving it, with undesired side effects. As it is a bogus usage scenario to request a line flagged as output to used as IRQ, we introduce APIs to let gpiolib track the use of a line as IRQ, and also set this flag from the userspace ABI. The API is symmetric so that lines can also be flagged from .irq_enable() and unflagged from IRQ by .irq_disable(). The debugfs file is altered so that we see if a line is reserved for IRQ. Cc: Enric Balletbo i Serra <eballetbo@gmail.com> Cc: Grant Likely <grant.likely@linaro.org> Cc: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> Cc: Santosh Shilimkar <santosh.shilimkar@ti.com> Acked-by: Alexandre Courbot <acourbot@nvidia.com> Reviewed-by: Stephen Warren <swarren@nvidia.com> Reviewed-by: Javier Martinez Canillas <javier.martinez@collabora.co.uk> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent d27e06a commit d468bf9

3 files changed

Lines changed: 101 additions & 2 deletions

File tree

drivers/gpio/gpiolib.c

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ struct gpio_desc {
6060
#define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */
6161
#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
6262
#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
63+
#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
6364

6465
#define ID_SHIFT 16 /* add new flags before this one */
6566

@@ -96,6 +97,8 @@ static int gpiod_get_value(const struct gpio_desc *desc);
9697
static void gpiod_set_value(struct gpio_desc *desc, int value);
9798
static int gpiod_cansleep(const struct gpio_desc *desc);
9899
static int gpiod_to_irq(const struct gpio_desc *desc);
100+
static int gpiod_lock_as_irq(struct gpio_desc *desc);
101+
static void gpiod_unlock_as_irq(struct gpio_desc *desc);
99102
static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
100103
static int gpiod_export_link(struct device *dev, const char *name,
101104
struct gpio_desc *desc);
@@ -162,6 +165,17 @@ static struct gpio_desc *gpio_to_desc(unsigned gpio)
162165
return &gpio_desc[gpio];
163166
}
164167

168+
/**
169+
* Convert an offset on a certain chip to a corresponding descriptor
170+
*/
171+
static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip,
172+
unsigned int offset)
173+
{
174+
unsigned int gpio = chip->base + offset;
175+
176+
return gpio_to_desc(gpio);
177+
}
178+
165179
/**
166180
* Convert a GPIO descriptor to the integer namespace.
167181
* This should disappear in the future but is needed since we still
@@ -428,6 +442,7 @@ static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
428442
desc->flags &= ~GPIO_TRIGGER_MASK;
429443

430444
if (!gpio_flags) {
445+
gpiod_unlock_as_irq(desc);
431446
ret = 0;
432447
goto free_id;
433448
}
@@ -466,6 +481,12 @@ static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
466481
if (ret < 0)
467482
goto free_id;
468483

484+
ret = gpiod_lock_as_irq(desc);
485+
if (ret < 0) {
486+
gpiod_warn(desc, "failed to flag the GPIO for IRQ\n");
487+
goto free_id;
488+
}
489+
469490
desc->flags |= gpio_flags;
470491
return 0;
471492

@@ -1730,6 +1751,14 @@ static int gpiod_direction_output(struct gpio_desc *desc, int value)
17301751
return -EINVAL;
17311752
}
17321753

1754+
/* GPIOs used for IRQs shall not be set as output */
1755+
if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1756+
gpiod_err(desc,
1757+
"%s: tried to set a GPIO tied to an IRQ as output\n",
1758+
__func__);
1759+
return -EIO;
1760+
}
1761+
17331762
/* Open drain pin should not be driven to 1 */
17341763
if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
17351764
return gpiod_direction_input(desc);
@@ -2050,6 +2079,58 @@ int __gpio_to_irq(unsigned gpio)
20502079
}
20512080
EXPORT_SYMBOL_GPL(__gpio_to_irq);
20522081

2082+
/**
2083+
* gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
2084+
* @gpio: the GPIO line to lock as used for IRQ
2085+
*
2086+
* This is used directly by GPIO drivers that want to lock down
2087+
* a certain GPIO line to be used as IRQs, for example in the
2088+
* .to_irq() callback of their gpio_chip, or in the .irq_enable()
2089+
* of its irq_chip implementation if the GPIO is known from that
2090+
* code.
2091+
*/
2092+
static int gpiod_lock_as_irq(struct gpio_desc *desc)
2093+
{
2094+
if (!desc)
2095+
return -EINVAL;
2096+
2097+
if (test_bit(FLAG_IS_OUT, &desc->flags)) {
2098+
gpiod_err(desc,
2099+
"%s: tried to flag a GPIO set as output for IRQ\n",
2100+
__func__);
2101+
return -EIO;
2102+
}
2103+
2104+
set_bit(FLAG_USED_AS_IRQ, &desc->flags);
2105+
return 0;
2106+
}
2107+
2108+
int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
2109+
{
2110+
return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset));
2111+
}
2112+
EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
2113+
2114+
/**
2115+
* gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
2116+
* @gpio: the GPIO line to unlock from IRQ usage
2117+
*
2118+
* This is used directly by GPIO drivers that want to indicate
2119+
* that a certain GPIO is no longer used exclusively for IRQ.
2120+
*/
2121+
static void gpiod_unlock_as_irq(struct gpio_desc *desc)
2122+
{
2123+
if (!desc)
2124+
return;
2125+
2126+
clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
2127+
}
2128+
2129+
void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
2130+
{
2131+
return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset));
2132+
}
2133+
EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
20532134

20542135
/* There's no value in making it easy to inline GPIO calls that may sleep.
20552136
* Common examples include ones connected to I2C or SPI chips.
@@ -2091,19 +2172,22 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
20912172
unsigned gpio = chip->base;
20922173
struct gpio_desc *gdesc = &chip->desc[0];
20932174
int is_out;
2175+
int is_irq;
20942176

20952177
for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
20962178
if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
20972179
continue;
20982180

20992181
gpiod_get_direction(gdesc);
21002182
is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
2101-
seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
2183+
is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2184+
seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
21022185
gpio, gdesc->label,
21032186
is_out ? "out" : "in ",
21042187
chip->get
21052188
? (chip->get(chip, i) ? "hi" : "lo")
2106-
: "? ");
2189+
: "? ",
2190+
is_irq ? "IRQ" : " ");
21072191
seq_printf(s, "\n");
21082192
}
21092193
}

include/asm-generic/gpio.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ extern int __gpio_cansleep(unsigned gpio);
192192

193193
extern int __gpio_to_irq(unsigned gpio);
194194

195+
extern int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset);
196+
extern void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset);
197+
195198
extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
196199
extern int gpio_request_array(const struct gpio *array, size_t num);
197200
extern void gpio_free_array(const struct gpio *array, size_t num);

include/linux/gpio.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,18 @@ static inline int gpio_to_irq(unsigned gpio)
204204
return -EINVAL;
205205
}
206206

207+
static inline int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
208+
{
209+
WARN_ON(1);
210+
return -EINVAL;
211+
}
212+
213+
static inline void gpio_unlock_as_irq(struct gpio_chip *chip,
214+
unsigned int offset)
215+
{
216+
WARN_ON(1);
217+
}
218+
207219
static inline int irq_to_gpio(unsigned irq)
208220
{
209221
/* irq can never have been returned from gpio_to_irq() */

0 commit comments

Comments
 (0)