Skip to contentSkip to conversation

pinctrl: mcp23s08: delete regmap reg_defaults to avoid cache sync issues

PATCH In review [email protected] 4 participants 5 messages Oct 9, 2025 – Oct 24, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 pinctrl: mcp23s08: delete regmap reg_defaults to avoid cache sync issues

From: Maksim Kiselev <[email protected]>

The probe function does not guarantee that chip registers are in their default state. Thus using reg_defaults for regmap is incorrect.

For example, the chip may have already been configured by the bootloader before the Linux driver loads, or the mcp might not have a reset at all and not reset a state between reboots.

In such cases, using reg_defaults leads to the cache values diverging from the actual registers values in the chip.

Previous attempts to fix consequences of this issue were made in 'commit 3ede3f8b4b4b ("pinctrl: mcp23s08: Reset all pins to input at probe")', but this is insufficient. The OLAT register reset is also required. And there's still potential for new issues arising due to cache desynchronization of other registers.

Therefore, remove reg_defaults entirely to eliminate the root cause of these problems.

Also remove the force reset all pins to input at probe as it is no longer required.

---
 drivers/pinctrl/pinctrl-mcp23s08.c | 34 ------------------------------
 1 file changed, 34 deletions(-)
drivers/pinctrl/pinctrl-mcp23s08.c +0 -34
index 78ff7930649d2..0b329661b5978 100644
@@ -44,17 +44,6 @@
 #define MCP_GPIO	0x09
 #define MCP_OLAT	0x0a
 
-static const struct reg_default mcp23x08_defaults[] = {
-	{.reg = MCP_IODIR,		.def = 0xff},
-	{.reg = MCP_IPOL,		.def = 0x00},
-	{.reg = MCP_GPINTEN,		.def = 0x00},
-	{.reg = MCP_DEFVAL,		.def = 0x00},
-	{.reg = MCP_INTCON,		.def = 0x00},
-	{.reg = MCP_IOCON,		.def = 0x00},
-	{.reg = MCP_GPPU,		.def = 0x00},
-	{.reg = MCP_OLAT,		.def = 0x00},
-};
-
 static const struct regmap_range mcp23x08_volatile_range = {
 	.range_min = MCP_INTF,
 	.range_max = MCP_GPIO,
@@ -82,25 +71,12 @@ const struct regmap_config mcp23x08_regmap = {
 	.reg_stride = 1,
 	.volatile_table = &mcp23x08_volatile_table,
 	.precious_table = &mcp23x08_precious_table,
-	.reg_defaults = mcp23x08_defaults,
-	.num_reg_defaults = ARRAY_SIZE(mcp23x08_defaults),
 	.cache_type = REGCACHE_FLAT,
 	.max_register = MCP_OLAT,
 	.disable_locking = true, /* mcp->lock protects the regmap */
 };
 EXPORT_SYMBOL_GPL(mcp23x08_regmap);
 
-static const struct reg_default mcp23x17_defaults[] = {
-	{.reg = MCP_IODIR << 1,		.def = 0xffff},
-	{.reg = MCP_IPOL << 1,		.def = 0x0000},
-	{.reg = MCP_GPINTEN << 1,	.def = 0x0000},
-	{.reg = MCP_DEFVAL << 1,	.def = 0x0000},
-	{.reg = MCP_INTCON << 1,	.def = 0x0000},
-	{.reg = MCP_IOCON << 1,		.def = 0x0000},
-	{.reg = MCP_GPPU << 1,		.def = 0x0000},
-	{.reg = MCP_OLAT << 1,		.def = 0x0000},
-};
-
 static const struct regmap_range mcp23x17_volatile_range = {
 	.range_min = MCP_INTF << 1,
 	.range_max = MCP_GPIO << 1,
@@ -129,8 +105,6 @@ const struct regmap_config mcp23x17_regmap = {
 	.max_register = MCP_OLAT << 1,
 	.volatile_table = &mcp23x17_volatile_table,
 	.precious_table = &mcp23x17_precious_table,
-	.reg_defaults = mcp23x17_defaults,
-	.num_reg_defaults = ARRAY_SIZE(mcp23x17_defaults),
 	.cache_type = REGCACHE_FLAT,
 	.val_format_endian = REGMAP_ENDIAN_LITTLE,
 	.disable_locking = true, /* mcp->lock protects the regmap */
@@ -614,14 +588,6 @@ int mcp23s08_probe_one(struct mcp23s08 *mcp, struct device *dev,
 
 	mcp->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
 
-	/*
-	 * Reset the chip - we don't really know what state it's in, so reset
-	 * all pins to input first to prevent surprises.
-	 */
-	ret = mcp_write(mcp, MCP_IODIR, mcp->chip.ngpio == 16 ? 0xFFFF : 0xFF);
-	if (ret < 0)
-		return ret;
-
 	/* verify MCP_IOCON.SEQOP = 0, so sequential reads work,
 	 * and MCP_IOCON.HAEN = 1, so we work with all chips.
 	 */
Signature · 1 line
2.48.1
#1 lore raw

Hi Maksim,

thanks for your patch!

Quoted 22 lines · On Thu, Oct 9, 2025 at 3:29 PM <[email protected]> wrote:

From: Maksim Kiselev <[email protected]>

The probe function does not guarantee that chip registers are in their default state. Thus using reg_defaults for regmap is incorrect.

For example, the chip may have already been configured by the bootloader before the Linux driver loads, or the mcp might not have a reset at all and not reset a state between reboots.

In such cases, using reg_defaults leads to the cache values diverging from the actual registers values in the chip.

Previous attempts to fix consequences of this issue were made in 'commit 3ede3f8b4b4b ("pinctrl: mcp23s08: Reset all pins to input at probe")', but this is insufficient. The OLAT register reset is also required. And there's still potential for new issues arising due to cache desynchronization of other registers.

Therefore, remove reg_defaults entirely to eliminate the root cause of these problems.

Quoted 6 lines

Also remove the force reset all pins to input at probe as it is no longer required.

I would surely like to see some Tested-by on this patch because this driver has *many* users.

I added some people to the To: line who recently made changes to this driver, maybe they can test.

Yours,
Linus Walleij

#2 lore raw
In reply to [email protected]

Hi,

Quoted 15 lines · On Thu, 2025-10-09 at 16:26 +0300, [email protected] wrote:

From: Maksim Kiselev <[email protected]>

The probe function does not guarantee that chip registers are in their default state. Thus using reg_defaults for regmap is incorrect.

---

drivers/pinctrl/pinctrl-mcp23s08.c +0 -2
@@ -82,25 +71,12 @@ const struct regmap_config mcp23x08_regmap = {
   .reg_stride = 1,
   .volatile_table = &mcp23x08_volatile_table,
   .precious_table = &mcp23x08_precious_table,
- .reg_defaults = mcp23x08_defaults,
- .num_reg_defaults = ARRAY_SIZE(mcp23x08_defaults),
   .cache_type = REGCACHE_FLAT,
   .max_register = MCP_OLAT,
Quoted 1 line

  .disable_locking = true, /* mcp->lock protects the regmap */

As Andy mentioned, the problem you will now have to deal with is that your cache is not initialized at all. Unlike the other cache types, REGCACHE_FLAT will zero-initialize its cache, perhaps making your cache sync issues worse.

You have two options to initialize the cache properly:
* Provide .num_reg_defaults_raw (= MCP_OLAT + 1). This will give you a warning on probe about the cache defaults being initialized from hardware.
* Switch to another cache type (REGCACHE_MAPLE), which is aware of (in)valid cache entries. regmap will then init the cache on the first access to a register.

You could also combine the two, like the Cypress driver Andy referred to (pinctrl-cy8c95x0.c). In that case you get cache loading at init, instead of at first use, but without the risk of missing something.

Best,
Sander

#3 lore raw
Quoted 10 lines · Sander Vanheule

Hi,

Quoted 7 lines · On Thu, 2025-10-09 at 16:26 +0300, [email protected] wrote:

From: Maksim Kiselev <[email protected]>

The probe function does not guarantee that chip registers are in their default state. Thus using reg_defaults for regmap is incorrect.

---

Quoted 12 lines

drivers/pinctrl/pinctrl-mcp23s08.c const struct regmap_config mcp23x08_regmap = { · line 76

Quoted 9 lines
drivers/pinctrl/pinctrl-mcp23s08.c +0 -2
@@ -82,25 +71,12 @@ const struct regmap_config mcp23x08_regmap = {
   .reg_stride = 1,
   .volatile_table = &mcp23x08_volatile_table,
   .precious_table = &mcp23x08_precious_table,
- .reg_defaults = mcp23x08_defaults,
- .num_reg_defaults = ARRAY_SIZE(mcp23x08_defaults),
   .cache_type = REGCACHE_FLAT,
   .max_register = MCP_OLAT,
   .disable_locking = true, /* mcp->lock protects the regmap */

As Andy mentioned, the problem you will now have to deal with is that your cache is not initialized at all. Unlike the other cache types, REGCACHE_FLAT will zero-initialize its cache, perhaps making your cache sync issues worse.

Ouch...

I have access to hardware this week (boards with 2 and 3 of the I2C chips), I'll be able to do some hands-on testing, and report back.

Quoted 6 lines

You have two options to initialize the cache properly:
* Provide .num_reg_defaults_raw (= MCP_OLAT + 1). This will give you a warning on probe about the cache defaults being initialized from hardware.
* Switch to another cache type (REGCACHE_MAPLE), which is aware of (in)valid cache entries. regmap will then init the cache on the first access to a register.

Using REGCACHE_MAPLE sounds like the obvious solution to me. That's what most other drivers use.

Quoted 3 lines

You could also combine the two, like the Cypress driver Andy referred to (pinctrl-cy8c95x0.c). In that case you get cache loading at init, instead of at first use, but without the risk of missing something.

Signature · 1 line
Mike Looijmans
Tested-by on v2 1/1
#4 lore raw
In reply to [email protected]
Quoted 4 lines · On 10/9/25 15:26, [email protected] wrote:

From: Maksim Kiselev <[email protected]>

The probe function does not guarantee that chip registers are in their default state. Thus using reg_defaults for regmap is incorrect.

Did some testing on actual hardware with this patch "as is". As Sander mentioned, the REGCACHE_FLAT caching mode assumes all registers are "0" initially, which causes similar issues as what we're trying to solve.

I've verified that by setting a bit to "1" in the OLAT register (using i2cset) for an unused GPIO line. After a reboot, the kernel resets it to "0" because of the caching issue.

I then changed the cache_type to REGCACHE_MAPLE as indicated below, and then the kernel doesn't touch that bit any longer (as it should).

So once you've amended the patch with REGCACHE_MAPLE, you have my:

drivers/pinctrl/pinctrl-mcp23s08.c const struct regmap_config mcp23x08_regmap = { · line 74

Quoted 11 lines

...
static const struct regmap_range mcp23x08_volatile_range = {
.range_min = MCP_INTF,
.range_max = MCP_GPIO,

drivers/pinctrl/pinctrl-mcp23s08.c +0 -2
@@ -82,25 +71,12 @@ const struct regmap_config mcp23x08_regmap = {
  	.reg_stride = 1,
  	.volatile_table = &mcp23x08_volatile_table,
  	.precious_table = &mcp23x08_precious_table,
-	.reg_defaults = mcp23x08_defaults,
-	.num_reg_defaults = ARRAY_SIZE(mcp23x08_defaults),
  	.cache_type = REGCACHE_FLAT,

Must be REGCACHE_MAPLE

Quoted 20 lines
drivers/pinctrl/pinctrl-mcp23s08.c +0 -11
  	.max_register = MCP_OLAT,
  	.disable_locking = true, /* mcp->lock protects the regmap */
  };
  EXPORT_SYMBOL_GPL(mcp23x08_regmap);
 
-static const struct reg_default mcp23x17_defaults[] = {
-	{.reg = MCP_IODIR << 1,		.def = 0xffff},
-	{.reg = MCP_IPOL << 1,		.def = 0x0000},
-	{.reg = MCP_GPINTEN << 1,	.def = 0x0000},
-	{.reg = MCP_DEFVAL << 1,	.def = 0x0000},
-	{.reg = MCP_INTCON << 1,	.def = 0x0000},
-	{.reg = MCP_IOCON << 1,		.def = 0x0000},
-	{.reg = MCP_GPPU << 1,		.def = 0x0000},
-	{.reg = MCP_OLAT << 1,		.def = 0x0000},
-};
-
  static const struct regmap_range mcp23x17_volatile_range = {
  	.range_min = MCP_INTF << 1,
  	.range_max = MCP_GPIO << 1,

drivers/pinctrl/pinctrl-mcp23s08.c const struct regmap_config mcp23x17_regmap = { · line 108

Quoted 6 lines
drivers/pinctrl/pinctrl-mcp23s08.c +0 -2
@@ -129,8 +105,6 @@ const struct regmap_config mcp23x17_regmap = {
  	.max_register = MCP_OLAT << 1,
  	.volatile_table = &mcp23x17_volatile_table,
  	.precious_table = &mcp23x17_precious_table,
-	.reg_defaults = mcp23x17_defaults,
-	.num_reg_defaults = ARRAY_SIZE(mcp23x17_defaults),
  	.cache_type = REGCACHE_FLAT,

Must be REGCACHE_MAPLE

Quoted 4 lines
drivers/pinctrl/pinctrl-mcp23s08.c +0 -0
  	.val_format_endian = REGMAP_ENDIAN_LITTLE,
  	.disable_locking = true, /* mcp->lock protects the regmap */
@@ -614,14 +588,6 @@ int mcp23s08_probe_one(struct mcp23s08 *mcp, struct device *dev,
...
Signature · 10 lines
Mike Looijmans
System Expert

TOPIC Embedded Products B.V.
Materiaalweg 4, 5681 RJ Best
The Netherlands

T: +31 (0) 499 33 69 69
E: [email protected]
W: www.topic.nl