All of lore.kernel.org
 help / color / mirror / Atom feed
From: Maria Garcia <mariagarcia7293@gmail.com>
To: linux-gpio@vger.kernel.org
Cc: "Linus Walleij" <linus.walleij@linaro.org>,
	"Bartosz Golaszewski" <brgl@bgdev.pl>,
	"Guy Morand" <guy.morand@bytesatwork.ch>,
	"Theo Lébrun" <theo.lebrun@bootlin.com>,
	"Maria Garcia" <mariagarcia7293@gmail.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH 2/2] gpio: pca953x: Fix TCA6418 interrupt handling
Date: Mon, 23 Nov 2026 12:32:34 +0100	[thread overview]
Message-ID: <20261123113234.197852-3-mariagarcia7293@gmail.com> (raw)
In-Reply-To: <20261123113234.197852-1-mariagarcia7293@gmail.com>

Unlike standard PCA953x expanders, the TCA6418E has dedicated interrupt
control and status registers:
- GPIO_INT_EN (0x1a..0x1c) to enable or mask per-pin interrupts
- GPIO_INT_LVL (0x26..0x28) to select rising (1) or falling (0) edge
- GPIO_INT_STAT (0x11..0x13) for clear-on-read per-pin status
- INT_STAT (0x02), where GPI_INT (BIT(1)) must be cleared via W1C

Update pca953x_irq_bus_sync_unlock(), pca953x_irq_pending(), and
pca953x_irq_setup() to program and acknowledge these registers. Also
reject IRQ_TYPE_EDGE_BOTH as the hardware only supports a single edge
polarity per pin at a time.

Fixes: 6c99a046edfa ("gpio: pca953x: Add support for TI TCA6418")
Reported-by: Guy Morand <guy.morand@bytesatwork.ch>
Closes: https://lore.kernel.org/linux-gpio/CAFOBSkSHt5nwdqsf=V0esBh23oTkOzy--9-vBQFPBoroGK3-HA@mail.gmail.com/
Tested-by: Guy Morand <guy.morand@bytesatwork.ch>
Signed-off-by: Maria Garcia <mariagarcia7293@gmail.com>
---
 drivers/gpio/gpio-pca953x.c | 79 +++++++++++++++++++++++++++++++------
 1 file changed, 66 insertions(+), 13 deletions(-)

diff --git a/drivers/gpio/gpio-pca953x.c b/drivers/gpio/gpio-pca953x.c
index 10a6fbdcb64d..c771ecf65071 100644
--- a/drivers/gpio/gpio-pca953x.c
+++ b/drivers/gpio/gpio-pca953x.c
@@ -910,6 +910,12 @@ static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
 
 		/* Unmask enabled interrupts */
 		pca953x_write_regs(chip, PCAL953X_INT_MASK, irq_mask);
+	} else if (PCA_CHIP_TYPE(chip->driver_data) == TCA6418_TYPE) {
+		/* 0 = falling edge, 1 = rising edge */
+		pca953x_write_regs(chip, TCA6418_GPIO_INT_LVL, chip->irq_trig_raise);
+
+		/* Unmask enabled interrupts */
+		pca953x_write_regs(chip, TCA6418_GPIO_INT_EN, chip->irq_mask);
 	}
 
 	/* Switch direction to input if needed */
@@ -936,7 +942,15 @@ static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
 	struct device *dev = &chip->client->dev;
 	irq_hw_number_t hwirq = irqd_to_hwirq(d);
 
-	if (!(type & IRQ_TYPE_SENSE_MASK)) {
+	/* TCA6418 only supports either rising or falling edge at a time (per pin) */
+	if (PCA_CHIP_TYPE(chip->driver_data) == TCA6418_TYPE) {
+		if (type != IRQ_TYPE_EDGE_RISING &&
+		    type != IRQ_TYPE_EDGE_FALLING) {
+			dev_err(dev, "irq %d: unsupported type %d\n", d->irq,
+				type);
+			return -EINVAL;
+		}
+	} else if (!(type & IRQ_TYPE_SENSE_MASK)) {
 		dev_err(dev, "irq %d: unsupported type %d\n", d->irq, type);
 		return -EINVAL;
 	}
@@ -1002,6 +1016,28 @@ static bool pca953x_irq_pending(struct pca953x_chip *chip, unsigned long *pendin
 			return false;
 	}
 
+	if (PCA_CHIP_TYPE(chip->driver_data) == TCA6418_TYPE) {
+		unsigned int stat;
+
+		ret = regmap_read(chip->regmap, TCA6418_INT_STAT, &stat);
+		if (ret || !(stat & TCA6418_INT_STAT_GPI))
+			return false;
+
+		ret = pca953x_read_regs(chip, TCA6418_GPIO_INT_STAT, pending);
+		if (ret)
+			return false;
+
+		/* Clear GPI_INT in INT_STAT */
+		ret = regmap_write(chip->regmap, TCA6418_INT_STAT,
+				   TCA6418_INT_STAT_GPI);
+
+		if (ret)
+			return false;
+
+		bitmap_and(pending, pending, chip->irq_mask, gc->ngpio);
+		return !bitmap_empty(pending, gc->ngpio);
+	}
+
 	ret = pca953x_read_regs(chip, chip->regs->input, cur_stat);
 	if (ret)
 		return false;
@@ -1113,19 +1149,36 @@ static int pca953x_irq_setup(struct pca953x_chip *chip, int irq_base)
 	if (!(chip->driver_data & PCA_INT))
 		return 0;
 
-	ret = pca953x_read_regs(chip, chip->regs->input, irq_stat);
-	if (ret)
-		return ret;
+	if (PCA_CHIP_TYPE(chip->driver_data) == TCA6418_TYPE) {
+		/* Ensure all interrupts are masked and stale flags cleared */
+		bitmap_zero(irq_stat, gc->ngpio);
+		ret = pca953x_write_regs(chip, TCA6418_GPIO_INT_EN, irq_stat);
+		if (ret)
+			return ret;
+		ret = pca953x_read_regs(chip, TCA6418_GPIO_INT_STAT, irq_stat);
+		if (ret)
+			return ret;
 
-	/*
-	 * There is no way to know which GPIO line generated the
-	 * interrupt.  We have to rely on the previous read for
-	 * this purpose.
-	 */
-	ret = pca953x_read_regs(chip, chip->regs->direction, reg_direction);
-	if (ret)
-		return ret;
-	bitmap_and(chip->irq_stat, irq_stat, reg_direction, gc->ngpio);
+		/* Clear TCA6418_INT_STAT */
+		ret = regmap_write(chip->regmap, TCA6418_INT_STAT,
+				   TCA6418_INT_STAT_GPI);
+		if (ret)
+			return ret;
+	} else {
+		ret = pca953x_read_regs(chip, chip->regs->input, irq_stat);
+		if (ret)
+			return ret;
+
+		/*
+		 * There is no way to know which GPIO line generated the
+		 * interrupt.  We have to rely on the previous read for
+		 * this purpose.
+		 */
+		ret = pca953x_read_regs(chip, chip->regs->direction, reg_direction);
+		if (ret)
+			return ret;
+		bitmap_and(chip->irq_stat, irq_stat, reg_direction, gc->ngpio);
+	}
 	mutex_init(&chip->irq_lock);
 
 	girq = &chip->gpio_chip.irq;
-- 
2.52.0


  parent reply	other threads:[~2026-11-23 11:33 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 11:32 [PATCH 0/2] gpio: pca953x: Fix TCA6418 multi-bank I/O, caching, and IRQs Maria Garcia
2026-11-23 11:32 ` [PATCH 1/2] gpio: pca953x: Fix TCA6418 bulk transfers and input caching Maria Garcia
2026-11-23 11:48   ` sashiko-bot
2026-11-23 11:32 ` Maria Garcia [this message]
2026-11-23 11:47   ` [PATCH 2/2] gpio: pca953x: Fix TCA6418 interrupt handling sashiko-bot
2026-11-23 11:42 ` [PATCH 0/2] gpio: pca953x: Fix TCA6418 multi-bank I/O, caching, and IRQs María García Fernández

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=20261123113234.197852-3-mariagarcia7293@gmail.com \
    --to=mariagarcia7293@gmail.com \
    --cc=brgl@bgdev.pl \
    --cc=guy.morand@bytesatwork.ch \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=theo.lebrun@bootlin.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.