Skip to contentSkip to conversation

gpio: pca953x: Fix TCA6418 multi-bank I/O, caching, and IRQs

PATCH In review Maria Garcia 2 participants 6 messages Nov 23 GPIO

lore
Share & tools
lore
#0 lore raw

cover gpio: pca953x: Fix TCA6418 multi-bank I/O, caching, and IRQs

This series fixes multi-bank register access, input register caching, and interrupt handling for the TI TCA6418E GPIO expander in gpio-pca953x:

1. Disable REG_ADDR_AI (0x80) for TCA6418_TYPE so multi-byte reads and writes advance sequentially across all three banks, reverse Bank 0 bit order (bitrev8) in bulk read/write paths to match the datasheet's MSB-first layout, and mark the input status registers (0x14..0x16) as volatile so reads do not return stale cached values.

2. Support TCA6418 interrupt setup, edge configuration (GPIO_INT_LVL), masking (GPIO_INT_EN), status decoding (GPIO_INT_STAT), and W1C acknowledgment (INT_STAT) by reusing the existing pca953x paths, while rejecting unsupported IRQ_TYPE_EDGE_BOTH triggers.

Tested on TI TCA6418E hardware across all three banks (single-pin and multi-pin reads/writes, input volatility, interrupts). Also, independently tested by Guy Morand on his hardware.

Maria Garcia (2):
gpio: pca953x: Fix TCA6418 bulk transfers and input caching
gpio: pca953x: Fix TCA6418 interrupt handling

 drivers/gpio/gpio-pca953x.c | 120 ++++++++++++++++++++++++++++++------
 1 file changed, 101 insertions(+), 19 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH 1/2 gpio: pca953x: Fix TCA6418 bulk transfers and input caching

The TCA6418E does not support PCA-style 0x80 auto-increment on the command byte, uses MSB-first bit ordering in Bank 0, and requires input registers to be marked volatile so regmap does not return stale cached values.

Disable REG_ADDR_AI for TCA6418_TYPE, apply bitrev8() to Bank 0 in bulk read/write helpers, and mark TCA6418_INPUT volatile.

Also, add IRQ-related registers to the tca6418_check_register function for the sake of completion: GPIO_INT_EN, GPIO_INT_LVL, GPIO_INT_STAT, INT_STAT

---
 drivers/gpio/gpio-pca953x.c | 41 +++++++++++++++++++++++++++++++------
 1 file changed, 35 insertions(+), 6 deletions(-)
drivers/gpio/gpio-pca953x.c +35 -6
index 3b2407690281..10a6fbdcb64d 100644
@@ -10,6 +10,7 @@
 
 #include <linux/atomic.h>
 #include <linux/bitmap.h>
+#include <linux/bitrev.h>
 #include <linux/cleanup.h>
 #include <linux/device.h>
 #include <linux/errno.h>
@@ -37,9 +38,14 @@
 #define PCA953X_INVERT		0x02
 #define PCA953X_DIRECTION	0x03
 
+#define TCA6418_INT_STAT	0x02
+#define TCA6418_INT_STAT_GPI	BIT(1)
+#define TCA6418_GPIO_INT_STAT	0x11
 #define TCA6418_INPUT		0x14
 #define TCA6418_OUTPUT		0x17
+#define TCA6418_GPIO_INT_EN	0x1a
 #define TCA6418_DIRECTION	0x23
+#define TCA6418_GPIO_INT_LVL	0x26
 
 #define REG_ADDR_MASK		GENMASK(5, 0)
 #define REG_ADDR_EXT		BIT(6)
@@ -384,22 +390,37 @@ static bool pcal6534_check_register(struct pca953x_chip *chip, unsigned int reg,
 	return true;
 }
 
-/* TCA6418 breaks the PCA953x register order rule */
+/*
+ * TCA6418 breaks the PCA953x register order rule
+ * Access masks: BIT(0) readable, BIT(1) writeable, BIT(2) volatile
+ */
 static bool tca6418_check_register(struct pca953x_chip *chip, unsigned int reg,
 				   u32 access_type_mask)
 {
-	/*  Valid Input Registers - BIT(0) for readable access */
+	if (reg == TCA6418_INT_STAT)
+		return access_type_mask & (BIT(0) | BIT(1) | BIT(2));
+
+	if (reg >= TCA6418_GPIO_INT_STAT &&
+	    reg < (TCA6418_GPIO_INT_STAT + NBANK(chip)))
+		return access_type_mask & (BIT(0) | BIT(2));
+
 	if (reg >= TCA6418_INPUT && reg < (TCA6418_INPUT + NBANK(chip)))
-		return (access_type_mask & BIT(0));
+		return (access_type_mask & (BIT(0) | BIT(2)));
 
-	/*  Valid Output Registers - BIT(1) for writeable access */
 	if (reg >= TCA6418_OUTPUT && reg < (TCA6418_OUTPUT + NBANK(chip)))
 		return (access_type_mask & (BIT(0) | BIT(1)));
 
-	/*  Valid Direction Registers - BIT(2) for volatile access */
+	if (reg >= TCA6418_GPIO_INT_EN &&
+	    reg < (TCA6418_GPIO_INT_EN + NBANK(chip)))
+		return access_type_mask & (BIT(0) | BIT(1));
+
 	if (reg >= TCA6418_DIRECTION && reg < (TCA6418_DIRECTION + NBANK(chip)))
 		return (access_type_mask & (BIT(0) | BIT(1)));
 
+	if (reg >= TCA6418_GPIO_INT_LVL &&
+	    reg < (TCA6418_GPIO_INT_LVL + NBANK(chip)))
+		return access_type_mask & (BIT(0) | BIT(1));
+
 	return false;
 }
 
@@ -577,6 +598,9 @@ static int pca953x_write_regs(struct pca953x_chip *chip, int reg, unsigned long
 	for (i = 0; i < NBANK(chip); i++)
 		value[i] = bitmap_get_value8(val, i * BANK_SZ);
 
+	if (PCA_CHIP_TYPE(chip->driver_data) == TCA6418_TYPE)
+		value[0] = bitrev8(value[0]);
+
 	ret = regmap_bulk_write(chip->regmap, regaddr, value, NBANK(chip));
 	if (ret < 0) {
 		dev_err(&chip->client->dev, "failed writing register: %d\n", ret);
@@ -598,6 +622,9 @@ static int pca953x_read_regs(struct pca953x_chip *chip, int reg, unsigned long *
 		return ret;
 	}
 
+	if (PCA_CHIP_TYPE(chip->driver_data) == TCA6418_TYPE)
+		value[0] = bitrev8(value[0]);
+
 	for (i = 0; i < NBANK(chip); i++)
 		bitmap_set_value8(val, value[i], i * BANK_SZ);
 
@@ -1251,7 +1278,9 @@ static int pca953x_probe(struct i2c_client *client)
 
 	pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
 
-	if (NBANK(chip) > 2 || PCA_CHIP_TYPE(chip->driver_data) == PCA957X_TYPE) {
+	if (PCA_CHIP_TYPE(chip->driver_data) != TCA6418_TYPE &&
+	    (NBANK(chip) > 2 ||
+	     PCA_CHIP_TYPE(chip->driver_data) == PCA957X_TYPE)) {
 		dev_info(dev, "using auto increment\n");
 		regmap_config = &pca953x_ai_i2c_regmap;
 	} else {
Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH 1/2] gpio: pca953x: Fix TCA6418 bulk transfers and input caching

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] gpio: pca953x: missing select BITREVERSE in Kconfig for bitrev8() calls --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] gpio: pca953x: missing select BITREVERSE in Kconfig for bitrev8() calls
--

commit bfc14bf3ac2754cdca4700090ff5ce99abc64218
Author: Maria Garcia <[email protected]>

gpio: pca953x: Fix TCA6418 bulk transfers and input caching

This commit disables PCA-style register auto-increment for TCA6418, reverses Bank 0 bits during bulk reads and writes, marks TCA6418 input registers volatile, and adds TCA6418 IRQ registers to tca6418_check_register().

Quoted 4 lines
drivers/gpio/gpio-pca953x.c +0 -0
index afec5ec236968..8d9f5563e222a 100644

[ ... ]

Quoted 11 lines
drivers/gpio/gpio-pca953x.c +3 -0
@@ -577,6 +598,9 @@ static int pca953x_write_regs(struct pca953x_chip *chip, int reg, unsigned long
 	for (i = 0; i < NBANK(chip); i++)
 		value[i] = bitmap_get_value8(val, i * BANK_SZ);
 
+	if (PCA_CHIP_TYPE(chip->driver_data) == TCA6418_TYPE)
+		value[0] = bitrev8(value[0]);
+
 	ret = regmap_bulk_write(chip->regmap, regaddr, value, NBANK(chip));
 	if (ret < 0) {
 		dev_err(&chip->client->dev, "failed writing register: %d\n", ret);

drivers/gpio/gpio-pca953x.c static int pca953x_read_regs(struct pca953x_chip *chip, int reg, unsigned long * · line 626

Quoted 5 lines
drivers/gpio/gpio-pca953x.c +2 -0
@@ -598,6 +622,9 @@ static int pca953x_read_regs(struct pca953x_chip *chip, int reg, unsigned long *
 		return ret;
 	}
 
+	if (PCA_CHIP_TYPE(chip->driver_data) == TCA6418_TYPE)
+		value[0] = bitrev8(value[0]);

[Severity: Medium]
Does config GPIO_PCA953X in drivers/gpio/Kconfig need to select BITREVERSE for these bitrev8() calls?

On architectures without CONFIG_HAVE_ARCH_BITREVERSE, runtime bitrev8() calls reference byte_rev_table from lib/bitrev.c, which is only built when CONFIG_BITREVERSE is enabled. Without selecting BITREVERSE, building with CONFIG_GPIO_PCA953X enabled and CONFIG_BITREVERSE disabled (or =m with CONFIG_GPIO_PCA953X=y) fails at link time with an undefined reference to byte_rev_table.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH 2/2 gpio: pca953x: Fix TCA6418 interrupt handling

In reply to Maria Garcia

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.

---
 drivers/gpio/gpio-pca953x.c | 79 +++++++++++++++++++++++++++++++------
 1 file changed, 66 insertions(+), 13 deletions(-)
drivers/gpio/gpio-pca953x.c +66 -13
index 10a6fbdcb64d..c771ecf65071 100644
@@ -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;
Signature · 1 line
2.52.0
#4 lore raw

Re: [PATCH 2/2] gpio: pca953x: Fix TCA6418 interrupt handling

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] gpio: pca953x: wrong TCA6418 direction check in pca953x_irq_bus_sync_unlock() It also noticed 1…

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] gpio: pca953x: wrong TCA6418 direction check in pca953x_irq_bus_sync_unlock()
It also noticed 1 related pre-existing issue this patch does not introduce:
- [High] gpio: pca953x: broken TCA6418 volatility in tca6418_check_register() and IRQs [1]
--

commit 5ed3e002b749b9b8aa9dbd3081f6ed840b5b291d
Author: Maria Garcia <[email protected]>

gpio: pca953x: Fix TCA6418 interrupt handling

This commit adds TCA6418-specific interrupt register programming and status handling to pca953x_irq_bus_sync_unlock(), pca953x_irq_pending(), and pca953x_irq_setup(), and restricts TCA6418 to rising or falling edge triggers.

Quoted 7 lines
drivers/gpio/gpio-pca953x.c +0 -0
index 8d9f5563e222a..5a39c5a5c8a36 100644
@@ -910,6 +910,12 @@ static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
 
 		/* Unmask enabled interrupts */

drivers/gpio/gpio-pca953x.c static void pca953x_irq_bus_sync_unlock(struct irq_data *d) · line 921

Quoted 10 lines
drivers/gpio/gpio-pca953x.c +6 -0
 		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 */

[Severity: Medium]
Does the direction switch below work on TCA6418?

pca953x_irq_bus_sync_unlock() falls through to:

drivers/gpio/gpio-pca953x.c:pca953x_irq_bus_sync_unlock() {
	...
	/* Look for any newly setup interrupt */
	for_each_andnot_bit(level, irq_mask, reg_direction, gc->ngpio)
		pca953x_gpio_direction_input_unlocked(&chip->gpio_chip, level);
}

On TCA6418, a bit in TCA6418_DIRECTION is 0 for input and 1 for output (unlike standard PCA953x where 1 is input and 0 is output). Because for_each_andnot_bit() iterates over pins where reg_direction is 0, any interrupt pin that was previously configured as an output (reg_direction bit 1) is skipped and remains driving as an output, while pins already in input mode (bit 0) are reconfigured on every IRQ bus unlock.

[Severity: High]
This problem wasn't introduced by this patch, but as part of [1], pca953x_irq_bus_sync_unlock() still treats set bits in reg_direction as inputs on TCA6418 instead of outputs, leaving newly requested interrupt pins configured as outputs when switching direction to input.

[1] https://sashiko.dev/#/bug/linux-a8794f43-5e5f-405d-ba31-cf360dc2e1af

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw
In reply to Maria Garcia
Quoted 29 lines · Maria Garcia

This series fixes multi-bank register access, input register caching, and interrupt handling for the TI TCA6418E GPIO expander in gpio-pca953x:

1. Disable REG_ADDR_AI (0x80) for TCA6418_TYPE so multi-byte reads and writes advance sequentially across all three banks, reverse Bank 0 bit order (bitrev8) in bulk read/write paths to match the datasheet's MSB-first layout, and mark the input status registers (0x14..0x16) as volatile so reads do not return stale cached values.

2. Support TCA6418 interrupt setup, edge configuration (GPIO_INT_LVL), masking (GPIO_INT_EN), status decoding (GPIO_INT_STAT), and W1C acknowledgment (INT_STAT) by reusing the existing pca953x paths, while rejecting unsupported IRQ_TYPE_EDGE_BOTH triggers.

Tested on TI TCA6418E hardware across all three banks (single-pin and multi-pin reads/writes, input volatility, interrupts). Also, independently tested by Guy Morand on his hardware.

Maria Garcia (2):
gpio: pca953x: Fix TCA6418 bulk transfers and input caching
gpio: pca953x: Fix TCA6418 interrupt handling

 drivers/gpio/gpio-pca953x.c | 120 ++++++++++++++++++++++++++++++------
 1 file changed, 101 insertions(+), 19 deletions(-)

--
2.52.0