Skip to content

Commit 4e9439d

Browse files
Hans VerkuilLinus Walleij
authored andcommitted
gpiolib: add flag to indicate if the irq is disabled
GPIO drivers call gpiochip_(un)lock_as_irq whenever they want to use a gpio as an interrupt. This is done when the irq is requested and it marks the gpio as in use by an interrupt. This is problematic for cases where a gpio pin is used as an interrupt pin, then, after the irq is disabled, is used as a regular gpio pin. Currently it is not possible to do this other than by first freeing the interrupt so gpiochip_unlock_as_irq is called, since an attempt to switch the gpio direction for output will fail since gpiolib believes that the gpio is in use for an interrupt and it does not know that it the irq is actually disabled. There are currently two drivers that would like to be able to do this: the tda998x_drv.c driver where a regular gpio pin needs to be temporarily reconfigured as an interrupt pin during CEC calibration, and the cec-gpio driver where you want to configure the gpio pin as an interrupt while waiting for traffic over the CEC bus, or as a regular pin when receiving or transmitting a CEC message. The solution is to add a new flag that is set when the irq is enabled, and have gpiod_direction_output check for that flag. We also add functions that drivers that do not use GPIOLIB_IRQCHIP can call when they enable/disable the irq. Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent ca620f2 commit 4e9439d

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎drivers/gpio/gpiolib.c‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2588,8 +2588,9 @@ int gpiod_direction_output(struct gpio_desc *desc, int value)
25882588
else
25892589
value = !!value;
25902590

2591-
/* GPIOs used for IRQs shall not be set as output */
2592-
if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2591+
/* GPIOs used for enabled IRQs shall not be set as output */
2592+
if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2593+
test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
25932594
gpiod_err(desc,
25942595
"%s: tried to set a GPIO tied to an IRQ as output\n",
25952596
__func__);
@@ -3276,6 +3277,7 @@ int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
32763277
}
32773278

32783279
set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3280+
set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
32793281

32803282
/*
32813283
* If the consumer has not set up a label (such as when the
@@ -3306,13 +3308,36 @@ void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
33063308
return;
33073309

33083310
clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3311+
clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
33093312

33103313
/* If we only had this marking, erase it */
33113314
if (desc->label && !strcmp(desc->label, "interrupt"))
33123315
desc_set_label(desc, NULL);
33133316
}
33143317
EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
33153318

3319+
void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset)
3320+
{
3321+
struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
3322+
3323+
if (!IS_ERR(desc) &&
3324+
!WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3325+
clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3326+
}
3327+
EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3328+
3329+
void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset)
3330+
{
3331+
struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
3332+
3333+
if (!IS_ERR(desc) &&
3334+
!WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3335+
WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags));
3336+
set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3337+
}
3338+
}
3339+
EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3340+
33163341
bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
33173342
{
33183343
if (offset >= chip->ngpio)

‎drivers/gpio/gpiolib.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ struct gpio_desc {
214214
#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
215215
#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
216216
#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
217+
#define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */
217218
#define FLAG_IS_HOGGED 11 /* GPIO is hogged */
218219
#define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */
219220

‎include/linux/gpio/driver.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset);
403403
bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset);
404404
int gpiochip_reqres_irq(struct gpio_chip *chip, unsigned int offset);
405405
void gpiochip_relres_irq(struct gpio_chip *chip, unsigned int offset);
406+
void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset);
407+
void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset);
406408

407409
/* Line status inquiry for drivers */
408410
bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset);

0 commit comments

Comments
 (0)