From: Guy Morand <guy.morand@bytesatwork.ch>
To: "María García Fernández" <mariagarcia7293@gmail.com>,
"Bartosz Golaszewski" <brgl@kernel.org>
Cc: linus.walleij@linaro.org, linux-gpio@vger.kernel.org
Subject: Re: gpio: pca953x: TCA6418 support and driver design
Date: Thu, 19 Nov 2026 09:55:23 +0100 [thread overview]
Message-ID: <0a4623a1-0c3e-4802-a7fd-cfceb84de40f@bytesatwork.ch> (raw)
In-Reply-To: <CAFOBSkSHt5nwdqsf=V0esBh23oTkOzy--9-vBQFPBoroGK3-HA@mail.gmail.com>
Hi Bartosz, María,
Thanks for the quick feedback.
Maria, my intention is not to assign blame. These issues can look like
userspace bugs, so I would like to help prevent others from spending
time debugging the wrong layer. I would be happy to work together on
more specific test scenarios.
Bartosz, I have implemented a preliminary set of changes to the TCA6418
support in gpio-pca953x and tested input, output and interrupts on our
board. I’ve included the patch below as a starting point for discussion,
it is not yet a formal submission.
My preference would be to move the TCA6418 support to a dedicated
driver, since it requires a number of chip-specific operations. Our
existing driver could serve as a starting point:
https://github.com/bytesatwork/linux-imx/blob/baw-lf-6.18.20-2.0.0/drivers/gpio/gpio-tca6418.c
By keeping the existing ti,tca6418 compatible, it will remain compatible
with existing device trees.
Maria, if you have time to test the dedicated driver with your hardware
and use case, I would highly appreciate your feedback.
Would you prefer that I prepare the dedicated driver as an RFC, or clean
up the gpio-pca953x changes instead?
Best regards,
Guy Morand
--
bytesatwork
Technoparkstrasse 7
CH-8406 Winterthur
Switzerland
From 5e4be90be3d240893bb1125b5b763493be6d2605 Mon Sep 17 00:00:00 2001
From: Guy Morand <guy.morand@bytesatwork.ch>
Date: Wed, 18 Nov 2026 09:33:49 +0100
Subject: [PATCH] gpio: gpio-pca953x: Fix tca6418 support
The original support was only working when configuring a single line,
without changing its value at runtime (gpioset -c xxx 0=1). When
toggling the line at runtime, multiple write was generated on the i2c
bus which lead to corrupted register due to auto increment not being
supported by the chip (gpioset -c xxx -t 10ms,10ms,0 0=0).
Moreover, gpioget was not working due to regmap INPUT register not
declared as volatile, reading a staled value.
Interrupt handling was missing.
To make it work, the following changes were done:
* Do not use auto increment
* Declare INT_STATUS and INPUT registers volatile
* Add configuring interrupt handling registers
* Add tca6418 specific interrupt handler
* Revert the first bank bit order in set multiple line
Signed-off-by: Guy Morand <guy.morand@bytesatwork.ch>
---
drivers/gpio/gpio-pca953x.c | 252 +++++++++++++++++++++++++++++++++---
1 file changed, 234 insertions(+), 18 deletions(-)
diff --git a/drivers/gpio/gpio-pca953x.c b/drivers/gpio/gpio-pca953x.c
index ead20c357418..bbedbd35e97c 100644
--- a/drivers/gpio/gpio-pca953x.c
+++ b/drivers/gpio/gpio-pca953x.c
@@ -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>
@@ -38,9 +39,14 @@
#define PCA953X_INVERT 0x02
#define PCA953X_DIRECTION 0x03
+#define TCA6418_INT_STATUS 0x02
+#define TCA6418_GPIO_INT_STATUS 0x11
#define TCA6418_INPUT 0x14
#define TCA6418_OUTPUT 0x17
+#define TCA6418_GPIO_INT_ENABLE 0x1a
#define TCA6418_DIRECTION 0x23
+#define TCA6418_GPIO_INT_LEVEL 0x26
+#define TCA6418_GPI_INT BIT(1)
#define REG_ADDR_MASK GENMASK(5, 0)
#define REG_ADDR_EXT BIT(6)
@@ -385,22 +391,45 @@ static bool pcal6534_check_register(struct
pca953x_chip *chip, unsigned int reg,
return true;
}
+static void tca6418_revert_low_byte(unsigned long *reg_val)
+{
+ bitmap_set_value8(reg_val, bitrev8(bitmap_get_value8(reg_val, 0)),
+ 0);
+}
+
/* TCA6418 breaks the PCA953x register order rule */
static bool tca6418_check_register(struct pca953x_chip *chip, unsigned
int reg,
u32 access_type_mask)
{
- /* Valid Input Registers - BIT(0) for readable access */
+ /*
+ * BIT(0) readable
+ * BIT(1) writable
+ * BIT(2) volatile
+ */
+ if (reg == TCA6418_INT_STATUS)
+ return access_type_mask & (BIT(0) | BIT(1) | BIT(2));
+
+ if (reg >= TCA6418_GPIO_INT_STATUS &&
+ reg < TCA6418_GPIO_INT_STATUS + 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_ENABLE &&
+ reg < TCA6418_GPIO_INT_ENABLE + 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_LEVEL &&
+ reg < TCA6418_GPIO_INT_LEVEL + NBANK(chip))
+ return access_type_mask & (BIT(0) | BIT(1));
+
return false;
}
@@ -711,14 +740,18 @@ static int pca953x_gpio_get_multiple(struct
gpio_chip *gc,
if (ret)
return ret;
+ if (PCA_CHIP_TYPE(chip->driver_data) == TCA6418_TYPE)
+ tca6418_revert_low_byte(reg_val);
+
bitmap_replace(bits, bits, reg_val, mask, gc->ngpio);
return 0;
}
-static int pca953x_gpio_set_multiple(struct gpio_chip *gc,
- unsigned long *mask, unsigned long *bits)
+static int pca953x_gpio_set_multiple(struct gpio_chip *gc, unsigned
long *mask,
+ unsigned long *bits)
{
struct pca953x_chip *chip = gpiochip_get_data(gc);
+ bool is_tca6418 = PCA_CHIP_TYPE(chip->driver_data) == TCA6418_TYPE;
DECLARE_BITMAP(reg_val, MAX_LINE);
int ret;
@@ -728,8 +761,15 @@ static int pca953x_gpio_set_multiple(struct
gpio_chip *gc,
if (ret)
return ret;
+ /* TCA6418's first bank is reversed relative to GPIO offsets. */
+ if (is_tca6418)
+ tca6418_revert_low_byte(reg_val);
+
bitmap_replace(reg_val, reg_val, bits, mask, gc->ngpio);
+ if (is_tca6418)
+ tca6418_revert_low_byte(reg_val);
+
return pca953x_write_regs(chip, chip->regs->output, reg_val);
}
@@ -925,6 +965,74 @@ static void pca953x_irq_print_chip(struct irq_data
*data, struct seq_file *p)
seq_puts(p, dev_name(gc->parent));
}
+static int tca6418_irq_set_type(struct irq_data *d, unsigned int type)
+{
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
+ struct pca953x_chip *pca953x = gpiochip_get_data(gc);
+
+ if (type != IRQ_TYPE_EDGE_RISING && type != IRQ_TYPE_EDGE_FALLING) {
+ dev_err(&pca953x->client->dev, "Only rising or falling edge are
supported\n");
+ return -EINVAL;
+ }
+
+ return pca953x_irq_set_type(d, type);
+}
+
+static void tca6418_irq_bus_sync_unlock(struct irq_data *d)
+{
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
+ struct pca953x_chip *chip = gpiochip_get_data(gc);
+ struct device *dev = &chip->client->dev;
+ DECLARE_BITMAP(irq_enabled, MAX_LINE);
+ DECLARE_BITMAP(irq_level, MAX_LINE);
+ DECLARE_BITMAP(irq_inputs, MAX_LINE);
+ unsigned int offset;
+ int ret;
+
+ bitmap_or(irq_inputs, chip->irq_trig_raise, chip->irq_trig_fall,
+ gc->ngpio);
+ bitmap_copy(irq_enabled, chip->irq_mask, gc->ngpio);
+ for_each_set_bit(offset, irq_inputs, gc->ngpio) {
+ ret = pca953x_gpio_direction_input(gc, offset);
+ if (ret) {
+ dev_err(dev, "failed to set IRQ line %u as input: %d\n",
+ offset, ret);
+ clear_bit(offset, irq_enabled);
+ }
+ }
+
+ bitmap_copy(irq_level, chip->irq_trig_raise, gc->ngpio);
+ tca6418_revert_low_byte(irq_level);
+ tca6418_revert_low_byte(irq_enabled);
+
+ {
+ guard(mutex)(&chip->i2c_lock);
+ ret = pca953x_write_regs(chip, TCA6418_GPIO_INT_LEVEL,
+ irq_level);
+ if (!ret)
+ ret = pca953x_write_regs(chip, TCA6418_GPIO_INT_ENABLE,
+ irq_enabled);
+ }
+ if (ret)
+ dev_err(dev, "failed to configure TCA6418 interrupts: %d\n",
+ ret);
+
+ mutex_unlock(&chip->irq_lock);
+}
+
+static const struct irq_chip tca6418_irq_chip = {
+ .irq_mask = pca953x_irq_mask,
+ .irq_unmask = pca953x_irq_unmask,
+ .irq_set_wake = pca953x_irq_set_wake,
+ .irq_bus_lock = pca953x_irq_bus_lock,
+ .irq_bus_sync_unlock = tca6418_irq_bus_sync_unlock,
+ .irq_set_type = tca6418_irq_set_type,
+ .irq_shutdown = pca953x_irq_shutdown,
+ .irq_print_chip = pca953x_irq_print_chip,
+ .flags = IRQCHIP_IMMUTABLE,
+ GPIOCHIP_IRQ_RESOURCE_HELPERS,
+};
+
static const struct irq_chip pca953x_irq_chip = {
.irq_mask = pca953x_irq_mask,
.irq_unmask = pca953x_irq_unmask,
@@ -1011,6 +1119,80 @@ static bool pca953x_irq_pending(struct
pca953x_chip *chip, unsigned long *pendin
return !bitmap_empty(pending, gc->ngpio);
}
+static int tca6418_irq_pending(struct pca953x_chip *chip,
+ unsigned long *pending, bool *asserted)
+{
+ struct gpio_chip *gc = &chip->gpio_chip;
+ struct device *dev = &chip->client->dev;
+ u8 int_status[MAX_BANK];
+ unsigned int status;
+ int i, ret;
+
+ lockdep_assert_held(&chip->irq_lock);
+ lockdep_assert_held(&chip->i2c_lock);
+
+ *asserted = false;
+ ret = regmap_read(chip->regmap, TCA6418_INT_STATUS, &status);
+ if (ret || !(status & TCA6418_GPI_INT))
+ return ret;
+
+ *asserted = true;
+ ret = regmap_bulk_read(chip->regmap, TCA6418_GPIO_INT_STATUS,
+ int_status, NBANK(chip));
+ if (ret) {
+ dev_err_ratelimited(dev, "failed read int status: %d\n", ret);
+ return ret;
+ }
+
+ for (i = 0; i < NBANK(chip); i++)
+ bitmap_set_value8(pending, int_status[i], i * BANK_SZ);
+ tca6418_revert_low_byte(pending);
+ bitmap_and(pending, pending, chip->irq_mask, gc->ngpio);
+
+ ret = regmap_write(chip->regmap, TCA6418_INT_STATUS, TCA6418_GPI_INT);
+ if (ret)
+ dev_err_ratelimited(dev, "failed to handle TCA6418 IRQ: %d\n", ret);
+
+ return ret;
+}
+
+static irqreturn_t tca6418_irq_handler(struct pca953x_chip *chip)
+{
+ struct gpio_chip *gc = &chip->gpio_chip;
+ struct device *dev = &chip->client->dev;
+ DECLARE_BITMAP(pending, MAX_LINE);
+ bool asserted = false;
+ int level, ret;
+
+ for (;;) {
+ bitmap_zero(pending, MAX_LINE);
+ {
+ guard(mutex)(&chip->irq_lock);
+ guard(mutex)(&chip->i2c_lock);
+ ret = tca6418_irq_pending(chip, pending, &asserted);
+ }
+
+ if (ret || !asserted)
+ break;
+
+ for_each_set_bit(level, pending, gc->ngpio) {
+ int nested_irq =
+ irq_find_mapping(gc->irq.domain, level);
+
+ if (unlikely(nested_irq <= 0)) {
+ dev_warn_ratelimited(
+ dev, "unmapped interrupt %d\n", level);
+ continue;
+ }
+ handle_nested_irq(nested_irq);
+ }
+ if (ret)
+ break;
+ }
+
+ return IRQ_RETVAL(asserted);
+}
+
static irqreturn_t pca953x_irq_handler(int irq, void *devid)
{
struct pca953x_chip *chip = devid;
@@ -1019,6 +1201,9 @@ static irqreturn_t pca953x_irq_handler(int irq,
void *devid)
int level;
bool ret;
+ if (PCA_CHIP_TYPE(chip->driver_data) == TCA6418_TYPE)
+ return tca6418_irq_handler(chip);
+
bitmap_zero(pending, MAX_LINE);
scoped_guard(mutex, &chip->i2c_lock)
@@ -1042,6 +1227,27 @@ static irqreturn_t pca953x_irq_handler(int irq,
void *devid)
return IRQ_RETVAL(ret);
}
+static int tca6418_irq_init(struct pca953x_chip *chip)
+{
+ DECLARE_BITMAP(disabled, MAX_LINE);
+ u8 int_status[MAX_BANK];
+ int ret;
+
+ bitmap_zero(disabled, MAX_LINE);
+
+ guard(mutex)(&chip->i2c_lock);
+ ret = pca953x_write_regs(chip, TCA6418_GPIO_INT_ENABLE, disabled);
+ if (ret)
+ return ret;
+
+ ret = regmap_bulk_read(chip->regmap, TCA6418_GPIO_INT_STATUS,
+ int_status, NBANK(chip));
+ if (ret)
+ return ret;
+
+ return regmap_write(chip->regmap, TCA6418_INT_STATUS, TCA6418_GPI_INT);
+}
+
static int pca953x_irq_setup(struct pca953x_chip *chip, int irq_base)
{
struct i2c_client *client = chip->client;
@@ -1067,21 +1273,30 @@ 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) {
+ ret = tca6418_irq_init(chip);
+ 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.
- */
- pca953x_read_regs(chip, chip->regs->direction, reg_direction);
- bitmap_and(chip->irq_stat, irq_stat, reg_direction, gc->ngpio);
+ /*
+ * There is no way to know which GPIO line generated the
+ * interrupt. We have to rely on the previous read for
+ * this purpose.
+ */
+ pca953x_read_regs(chip, chip->regs->direction, reg_direction);
+ bitmap_and(chip->irq_stat, irq_stat, reg_direction, gc->ngpio);
+ }
mutex_init(&chip->irq_lock);
girq = &chip->gpio_chip.irq;
- gpio_irq_chip_set_chip(girq, &pca953x_irq_chip);
+ if (PCA_CHIP_TYPE(chip->driver_data) == TCA6418_TYPE)
+ gpio_irq_chip_set_chip(girq, &tca6418_irq_chip);
+ else
+ gpio_irq_chip_set_chip(girq, &pca953x_irq_chip);
/* This will let us handle the parent IRQ in the driver */
girq->parent_handler = NULL;
girq->num_parents = 0;
@@ -1236,7 +1451,8 @@ 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 ((NBANK(chip) > 2 || PCA_CHIP_TYPE(chip->driver_data) ==
PCA957X_TYPE) &&
+ PCA_CHIP_TYPE(chip->driver_data) != TCA6418_TYPE) {
dev_info(dev, "using AI\n");
regmap_config = &pca953x_ai_i2c_regmap;
} else {
--
2.47.3
On 18/11/2026 16:16, María García Fernández wrote:
> (Resending in plain text mode, sorry)
>
> Hi Bartosz, Guy,
>
> Very sorry about these bugs you encountered. The testing I did back
> then did not reveal any of these problems, and the teams that use this
> chip in one of the products at the company where I work have never
> complained about the chip not working properly. I will raise it ASAP
> and have a look. We can try to work together on this and define more
> specific testing scenarios other than the few tests I did by hand
> myself. I do think at this point we may be better off moving the
> TCA6418 to its own driver if the differences are so many.
>
> Thank you.
>
> Kind regards,
>
> María García.
>
>
> El mié, 18 nov 2026 a las 16:04, María García Fernández
> (<mariagarcia7293@gmail.com>) escribió:
>>
>> Hi Bartosz, Guy,
>>
>> Very sorry about these bugs you encountered. The testing I did back then did not reveal any of these problems, and the teams that use this chip in one of the products at the company where I work have never complained about the chip not working properly. I will raise it ASAP and have a look. We can try to work together on this and define more specific testing scenarios other than the few tests I did by hand myself. I do think at this point we may be better off moving the TCA6418 to its own driver if the differences are so many.
>>
>> Thank you.
>>
>> Kind regards,
>>
>> María García.
>>
>> El mié, 18 nov 2026 a las 15:48, Bartosz Golaszewski (<brgl@kernel.org>) escribió:
>>>
>>> On Wed, Nov 18, 2026 at 01:25:35PM +0100, Guy Morand wrote:
>>>> Hi Linus, Bartosz, Maria,
>>>>
>>>> We have been testing the TCA6418 support added to gpio-pca953x by the
>>>> following patch:
>>>> https://lore.kernel.org/all/20250703205740.45385-3-mariagarcia7293@gmail.com/
>>>>
>>>> On our board, using a 6.18-based NXP linux-imx kernel, the following command
>>>> does not toggle the requested GPIO as expected:
>>>> gpioset -c /dev/gpiochip4 -t 10ms,10ms,0 0=0
>>>>
>>>> While investigating, we found several issues in the TCA6418 support:
>>>> * The driver uses the PCA-style i2c auto-increment which is not documented
>>>> in the TCA6418 datasheet, leading to corrupted registers.
>>>> * Input-status registers are not marked volatile, so reads can return stale
>>>> cached values.
>>>> * The bulk get/set paths do not reverse the bit order of the first GPIO
>>>> bank, so they can read or update the wrong line.
>>>> * Interrupt handling is incomplete: the generic IRQ path does not configure
>>>> the TCA6418’s interrupt-enable and edge-selection registers or handle its
>>>> status and acknowledgement registers.
>>>>
>>>> I have spent some time trying to fix the integration, but the TCA6418 seems
>>>> to require several chip-specific operations.
>>>> Before this support was added, we used an out-of-tree TCA6418 driver that
>>>> worked well for our use case.
>>>>
>>>> I think the current support needs to be fixed, but I’m unsure whether it
>>>> would be preferable to add the necessary chip-specific operations to
>>>> gpio-pca953x or replace its TCA6418 support with a dedicated driver based on
>>>> our existing one.
>>>>
>>>> I’m not familiar with the subsystem’s design preferences. Which approach
>>>> would you prefer?
>>>>
>>>
>>> It really depends on how much model-specific operations there are. If
>>> every callback needs a completely different implementation for TCA6418
>>> then clearly, a separate driver is the way to go. Just make sure it
>>> doesn't break the ABI for OF systems.
>>>
>>> Bart
--
bytesatwork
Technoparkstrasse 7
CH-8406 Winterthur
Switzerland
phone: +41 52 213 79 79
prev parent reply other threads:[~2026-11-19 8:55 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 12:25 gpio: pca953x: TCA6418 support and driver design Guy Morand
2026-11-18 14:48 ` Bartosz Golaszewski
[not found] ` <CAFOBSkR9eHu_2UZkyu-v8saQ2YrKNcQxbLbyKNkFWtZw3XvMMQ@mail.gmail.com>
2026-11-18 15:16 ` María García Fernández
2026-11-19 8:55 ` Guy Morand [this message]
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=0a4623a1-0c3e-4802-a7fd-cfceb84de40f@bytesatwork.ch \
--to=guy.morand@bytesatwork.ch \
--cc=brgl@kernel.org \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=mariagarcia7293@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