diff options
author | Linus Walleij <linus.walleij@linaro.org> | 2013-09-24 05:54:38 -0400 |
---|---|---|
committer | Linus Walleij <linus.walleij@linaro.org> | 2013-10-16 03:59:54 -0400 |
commit | d468bf9ecaabd3bf3a6134e5a369ced82b1d1ca1 (patch) | |
tree | 452eaf82c5c50a4673ccd5fccdc1407b1243d03d /drivers/gpio | |
parent | d27e06ac5dcf60d5502269e1875bcb0f05f1b1c1 (diff) |
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>
Diffstat (limited to 'drivers/gpio')
-rw-r--r-- | drivers/gpio/gpiolib.c | 88 |
1 files changed, 86 insertions, 2 deletions
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 4fc28603a742..1014cb5e10b0 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c | |||
@@ -60,6 +60,7 @@ struct gpio_desc { | |||
60 | #define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */ | 60 | #define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */ |
61 | #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ | 61 | #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ |
62 | #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ | 62 | #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ |
63 | #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ | ||
63 | 64 | ||
64 | #define ID_SHIFT 16 /* add new flags before this one */ | 65 | #define ID_SHIFT 16 /* add new flags before this one */ |
65 | 66 | ||
@@ -96,6 +97,8 @@ static int gpiod_get_value(const struct gpio_desc *desc); | |||
96 | static void gpiod_set_value(struct gpio_desc *desc, int value); | 97 | static void gpiod_set_value(struct gpio_desc *desc, int value); |
97 | static int gpiod_cansleep(const struct gpio_desc *desc); | 98 | static int gpiod_cansleep(const struct gpio_desc *desc); |
98 | static int gpiod_to_irq(const struct gpio_desc *desc); | 99 | 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); | ||
99 | static int gpiod_export(struct gpio_desc *desc, bool direction_may_change); | 102 | static int gpiod_export(struct gpio_desc *desc, bool direction_may_change); |
100 | static int gpiod_export_link(struct device *dev, const char *name, | 103 | static int gpiod_export_link(struct device *dev, const char *name, |
101 | struct gpio_desc *desc); | 104 | struct gpio_desc *desc); |
@@ -163,6 +166,17 @@ static struct gpio_desc *gpio_to_desc(unsigned gpio) | |||
163 | } | 166 | } |
164 | 167 | ||
165 | /** | 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 | |||
179 | /** | ||
166 | * Convert a GPIO descriptor to the integer namespace. | 180 | * Convert a GPIO descriptor to the integer namespace. |
167 | * This should disappear in the future but is needed since we still | 181 | * This should disappear in the future but is needed since we still |
168 | * use GPIO numbers for error messages and sysfs nodes | 182 | * use GPIO numbers for error messages and sysfs nodes |
@@ -428,6 +442,7 @@ static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, | |||
428 | desc->flags &= ~GPIO_TRIGGER_MASK; | 442 | desc->flags &= ~GPIO_TRIGGER_MASK; |
429 | 443 | ||
430 | if (!gpio_flags) { | 444 | if (!gpio_flags) { |
445 | gpiod_unlock_as_irq(desc); | ||
431 | ret = 0; | 446 | ret = 0; |
432 | goto free_id; | 447 | goto free_id; |
433 | } | 448 | } |
@@ -466,6 +481,12 @@ static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, | |||
466 | if (ret < 0) | 481 | if (ret < 0) |
467 | goto free_id; | 482 | goto free_id; |
468 | 483 | ||
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 | |||
469 | desc->flags |= gpio_flags; | 490 | desc->flags |= gpio_flags; |
470 | return 0; | 491 | return 0; |
471 | 492 | ||
@@ -1730,6 +1751,14 @@ static int gpiod_direction_output(struct gpio_desc *desc, int value) | |||
1730 | return -EINVAL; | 1751 | return -EINVAL; |
1731 | } | 1752 | } |
1732 | 1753 | ||
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 | |||
1733 | /* Open drain pin should not be driven to 1 */ | 1762 | /* Open drain pin should not be driven to 1 */ |
1734 | if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | 1763 | if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
1735 | return gpiod_direction_input(desc); | 1764 | return gpiod_direction_input(desc); |
@@ -2050,6 +2079,58 @@ int __gpio_to_irq(unsigned gpio) | |||
2050 | } | 2079 | } |
2051 | EXPORT_SYMBOL_GPL(__gpio_to_irq); | 2080 | EXPORT_SYMBOL_GPL(__gpio_to_irq); |
2052 | 2081 | ||
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); | ||
2053 | 2134 | ||
2054 | /* There's no value in making it easy to inline GPIO calls that may sleep. | 2135 | /* There's no value in making it easy to inline GPIO calls that may sleep. |
2055 | * Common examples include ones connected to I2C or SPI chips. | 2136 | * Common examples include ones connected to I2C or SPI chips. |
@@ -2091,6 +2172,7 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) | |||
2091 | unsigned gpio = chip->base; | 2172 | unsigned gpio = chip->base; |
2092 | struct gpio_desc *gdesc = &chip->desc[0]; | 2173 | struct gpio_desc *gdesc = &chip->desc[0]; |
2093 | int is_out; | 2174 | int is_out; |
2175 | int is_irq; | ||
2094 | 2176 | ||
2095 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { | 2177 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { |
2096 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) | 2178 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) |
@@ -2098,12 +2180,14 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) | |||
2098 | 2180 | ||
2099 | gpiod_get_direction(gdesc); | 2181 | gpiod_get_direction(gdesc); |
2100 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); | 2182 | 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", | ||
2102 | gpio, gdesc->label, | 2185 | gpio, gdesc->label, |
2103 | is_out ? "out" : "in ", | 2186 | is_out ? "out" : "in ", |
2104 | chip->get | 2187 | chip->get |
2105 | ? (chip->get(chip, i) ? "hi" : "lo") | 2188 | ? (chip->get(chip, i) ? "hi" : "lo") |
2106 | : "? "); | 2189 | : "? ", |
2190 | is_irq ? "IRQ" : " "); | ||
2107 | seq_printf(s, "\n"); | 2191 | seq_printf(s, "\n"); |
2108 | } | 2192 | } |
2109 | } | 2193 | } |