mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alexey Charkov <alchark@flipper.net>
To: Linus Walleij <linusw@kernel.org>,
	 Bartosz Golaszewski <brgl@kernel.org>,
	Potin Lai <potin.lai.pt@gmail.com>
Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Alexey Charkov <alchark@flipper.net>
Subject: [PATCH] gpio: pca953x: Re-check level-triggered lines that stay active
Date: Mon, 30 Nov 2026 14:10:41 +0400	[thread overview]
Message-ID: <20261130-pca953x-level-reenable-v1-1-a989d2505da4@flipper.net> (raw)

The expanders only interrupt when an input changes from the value last
read, so they cannot express a level that is still active once its
handler has run, or once its consumer re-enables the interrupt. That is
the case whenever a new event comes in before the handler, or the
deferred work of a consumer that disables its interrupt meanwhile, has
cleared the last one. No input changes, the expander stays quiet, and the
event sits unnoticed until an unrelated line on the same expander happens
to change.

On a board with a FUSB302 behind a TCA6416 this loses the source's Accept
in most USB PD negotiations, so the sink keeps timing out and issuing hard
resets for tens of seconds before it gets a contract. A FUSB302 driver
that handles its interrupt in the IRQ thread rather than in deferred work
stops getting interrupts within seconds of a burst of USB PD messages
instead: its RX FIFO fills up and the chip stops acknowledging them.

Wake the expander's interrupt thread, so that it reads the inputs and
handles the line if it is still active, as a level-sensitive interrupt
controller would, once a level-triggered interrupt has been handled, and
when one is enabled, or switched to level triggering while enabled. Skip
an interrupt the consumer keeps disabled after its handler, which would
only spin until enabling it re-checks anyway. Do the scan in that thread
rather than in the caller, so nested handlers stay serialized and never
run under the locks of whoever enabled the interrupt. Hook .irq_enable
rather than .irq_unmask: the core disables interrupts lazily without
masking them, so re-enabling one does not unmask it.

Fixes: 417b0f8d08f8 ("gpio: pca953x: Add support for level-triggered interrupts")
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
 drivers/gpio/gpio-pca953x.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/drivers/gpio/gpio-pca953x.c b/drivers/gpio/gpio-pca953x.c
index 270cea8b4b49..e8ff948f392c 100644
--- a/drivers/gpio/gpio-pca953x.c
+++ b/drivers/gpio/gpio-pca953x.c
@@ -232,6 +232,7 @@ struct pca953x_chip {
 	DECLARE_BITMAP(irq_trig_fall, MAX_LINE);
 	DECLARE_BITMAP(irq_trig_level_high, MAX_LINE);
 	DECLARE_BITMAP(irq_trig_level_low, MAX_LINE);
+	bool irq_level_recheck;
 #endif
 	atomic_t wakeup_path;
 
@@ -834,6 +835,19 @@ static void pca953x_irq_unmask(struct irq_data *d)
 	set_bit(hwirq, chip->irq_mask);
 }
 
+static void pca953x_irq_enable(struct irq_data *d)
+{
+	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
+	struct pca953x_chip *chip = gpiochip_get_data(gc);
+	irq_hw_number_t hwirq = irqd_to_hwirq(d);
+
+	pca953x_irq_unmask(d);
+
+	if (test_bit(hwirq, chip->irq_trig_level_low) ||
+	    test_bit(hwirq, chip->irq_trig_level_high))
+		chip->irq_level_recheck = true;
+}
+
 static int pca953x_irq_set_wake(struct irq_data *d, unsigned int on)
 {
 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
@@ -899,6 +913,15 @@ static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
 		pca953x_gpio_direction_input_unlocked(&chip->gpio_chip, level);
 
 out:
+	/*
+	 * The chip only interrupts on input changes, so a level-triggered line
+	 * that is already active when enabled would go unnoticed.
+	 */
+	if (chip->irq_level_recheck) {
+		chip->irq_level_recheck = false;
+		irq_wake_thread(chip->client->irq, chip);
+	}
+
 	mutex_unlock(&chip->irq_lock);
 }
 
@@ -919,6 +942,9 @@ static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
 	assign_bit(hwirq, chip->irq_trig_level_low, type & IRQ_TYPE_LEVEL_LOW);
 	assign_bit(hwirq, chip->irq_trig_level_high, type & IRQ_TYPE_LEVEL_HIGH);
 
+	if ((type & IRQ_TYPE_LEVEL_MASK) && test_bit(hwirq, chip->irq_mask))
+		chip->irq_level_recheck = true;
+
 	return 0;
 }
 
@@ -946,6 +972,7 @@ static void pca953x_irq_print_chip(struct irq_data *data, struct seq_file *p)
 static const struct irq_chip pca953x_irq_chip = {
 	.irq_mask		= pca953x_irq_mask,
 	.irq_unmask		= pca953x_irq_unmask,
+	.irq_enable		= pca953x_irq_enable,
 	.irq_set_wake		= pca953x_irq_set_wake,
 	.irq_bus_lock		= pca953x_irq_bus_lock,
 	.irq_bus_sync_unlock	= pca953x_irq_bus_sync_unlock,
@@ -1035,6 +1062,7 @@ static irqreturn_t pca953x_irq_handler(int irq, void *devid)
 	struct pca953x_chip *chip = devid;
 	struct gpio_chip *gc = &chip->gpio_chip;
 	DECLARE_BITMAP(pending, MAX_LINE);
+	bool recheck = false;
 	int level;
 	bool ret;
 
@@ -1055,9 +1083,25 @@ static irqreturn_t pca953x_irq_handler(int irq, void *devid)
 
 			handle_nested_irq(nested_irq);
 			ret = 1;
+
+			/*
+			 * The chip interrupts again only once an input changes,
+			 * so a level still active after its handler, as when a
+			 * new event came in while the handler cleared the last
+			 * one, would go unnoticed: read the inputs again. Not
+			 * while the consumer keeps the interrupt disabled, which
+			 * would only spin, as enabling it re-checks anyway.
+			 */
+			if ((test_bit(level, chip->irq_trig_level_low) ||
+			     test_bit(level, chip->irq_trig_level_high)) &&
+			    !irqd_irq_disabled(irq_get_irq_data(nested_irq)))
+				recheck = true;
 		}
 	}
 
+	if (recheck)
+		irq_wake_thread(chip->client->irq, chip);
+
 	return IRQ_RETVAL(ret);
 }
 

---
base-commit: e5aad2bdbbee783611d32599f89cea3f12f9593d
change-id: 20261015-pca953x-level-reenable-aeaa863e255b

Best regards,
--  
Alexey Charkov <alchark@flipper.net>


                 reply	other threads:[~2026-11-30 10:10 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130-pca953x-level-reenable-v1-1-a989d2505da4@flipper.net \
    --to=alchark@flipper.net \
    --cc=brgl@kernel.org \
    --cc=linusw@kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=potin.lai.pt@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®