diff options
-rw-r--r-- | drivers/gpio/gpiolib.c | 88 | ||||
-rw-r--r-- | include/asm-generic/gpio.h | 3 | ||||
-rw-r--r-- | include/linux/gpio.h | 12 |
3 files changed, 101 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 | } |
diff --git a/include/asm-generic/gpio.h b/include/asm-generic/gpio.h index bde646995d10..b309a5c0019e 100644 --- a/include/asm-generic/gpio.h +++ b/include/asm-generic/gpio.h | |||
@@ -192,6 +192,9 @@ extern int __gpio_cansleep(unsigned gpio); | |||
192 | 192 | ||
193 | extern int __gpio_to_irq(unsigned gpio); | 193 | extern int __gpio_to_irq(unsigned gpio); |
194 | 194 | ||
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 | |||
195 | extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label); | 198 | extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label); |
196 | extern int gpio_request_array(const struct gpio *array, size_t num); | 199 | extern int gpio_request_array(const struct gpio *array, size_t num); |
197 | extern void gpio_free_array(const struct gpio *array, size_t num); | 200 | extern void gpio_free_array(const struct gpio *array, size_t num); |
diff --git a/include/linux/gpio.h b/include/linux/gpio.h index 552e3f46e4a3..a06ec3e85ba3 100644 --- a/include/linux/gpio.h +++ b/include/linux/gpio.h | |||
@@ -204,6 +204,18 @@ static inline int gpio_to_irq(unsigned gpio) | |||
204 | return -EINVAL; | 204 | return -EINVAL; |
205 | } | 205 | } |
206 | 206 | ||
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 | |||
207 | static inline int irq_to_gpio(unsigned irq) | 219 | static inline int irq_to_gpio(unsigned irq) |
208 | { | 220 | { |
209 | /* irq can never have been returned from gpio_to_irq() */ | 221 | /* irq can never have been returned from gpio_to_irq() */ |