Skip to contentSkip to conversation

iio: magnetometer: fix the register restore on resume in mmc35240 and mmc5633

PATCH Superseded Shashank Mohan Jain 2 participants 6 messages Oct 11 – Oct 12 IIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover iio: magnetometer: fix the register restore on resume in mmc35240 and mmc5633

The resume callbacks of mmc35240 and mmc5633 call regcache_sync_region() before they take the regmap out of cache-only mode. Since v7.3-rc1 regmap refuses a sync in cache-only mode with a WARN and -EINVAL (86fc3483a4d6 and afc8e3ee8307), so both drivers now warn on every resume and no longer restore their control registers.

The kernel-doc added by 86fc3483a4d6 says that the caller has to disable cache-only mode before the sync, so the two patches change the order in the drivers.

The Fixes tags name the regcache commit, because that is where the behaviour changed: the drivers have used this order since their resume callbacks were added, and it worked until v7.2. Please tell me if you would rather have the driver commits there.

I have no hardware for either chip. The patches were tested in qemu with the drivers bound to an i2c-stub device and a suspend/resume cycle through /sys/power/pm_test; the details are in the changelogs. A scan of drivers/ and sound/ for other resume paths which sync before leaving cache-only mode in the same function found only these two.

Based on mainline (a5ebb76233b7, v7.3-rc6+); the patches also apply to next-20261009.

Shashank Mohan Jain (2):
iio: magnetometer: mmc35240: leave cache-only mode before the cache
sync
iio: magnetometer: mmc5633: leave cache-only mode before the cache
sync

 drivers/iio/magnetometer/mmc35240.c | 3 +--
 drivers/iio/magnetometer/mmc5633.c  | 3 +--
 2 files changed, 2 insertions(+), 4 deletions(-)

base-commit: a5ebb76233b79db01e82e061173cb73c6d2b5c6b

Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 iio: magnetometer: mmc35240: leave cache-only mode before the cache sync

mmc35240_resume() marks the register cache dirty and calls regcache_sync_region() while the regmap is still in cache-only mode. It calls regcache_cache_only(false) only after the sync.

Since commit afc8e3ee8307 ("regcache: Validate cache_only state in regcache_sync_region()") a sync in cache-only mode is refused with a WARN and -EINVAL. Every resume now gives

  WARNING: drivers/base/regmap/regcache.c:536 at regcache_sync_region+0x1a4/0x1c0
  ...
   mmc35240_resume+0x30/0x70
   dpm_run_callback+0x7d/0x1c0
   device_resume+0x175/0x270
  ...
  mmc35240 1-0030: Failed to restore control registers

and the control registers are not written. If the chip lost its state during suspend, it runs with the reset value of CTRL1 while the cache, and with it the sampling frequency attribute, still has the configured value. Writing the same frequency again does not repair this, because regmap_update_bits() finds no difference to the cache.

Leave cache-only mode first, then mark the cache dirty and sync it.

Reproduced in qemu with the driver bound to an i2c-stub device: set in_magn_sampling_frequency to 25 (CTRL1 = 0x02), clear CTRL0 and CTRL1 of the stub behind the driver's back to emulate the power loss, and run a suspend/resume cycle with "devices" in /sys/power/pm_test. Without this change CTRL1 is still 0x00 after resume and the warning is printed. With it, and also with the regcache commit reverted, CTRL1 is 0x02 again and there is no warning. Not tested on real hardware.

Fixes afc8e3ee8307 ("regcache: Validate cache_only state in regcache_sync_region()")
Assisted-by LLM

---
Prepared with Claude Code (Anthropic), model Claude Opus 5.5 (claude-opus-5-5).

 drivers/iio/magnetometer/mmc35240.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iio/magnetometer/mmc35240.c +1 -2
index 6f40cba4b1..7f028d7f1f 100644
@@ -532,14 +532,13 @@ static int mmc35240_resume(struct device *dev)
 	struct mmc35240_data *data = iio_priv(indio_dev);
 	int ret;
 
+	regcache_cache_only(data->regmap, false);
 	regcache_mark_dirty(data->regmap);
 	ret = regcache_sync_region(data->regmap, MMC35240_REG_CTRL0,
 				   MMC35240_REG_CTRL1);
 	if (ret < 0)
 		dev_err(dev, "Failed to restore control registers\n");
 
-	regcache_cache_only(data->regmap, false);
-
 	return 0;
 }
 
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/2] iio: magnetometer: mmc35240: leave cache-only mode before the cache sync

Hi Shashank,

Thanks for the fix.

Quoted 8 lines
Fixes afc8e3ee8307 ("regcache: Validate cache_only state in regcache_sync_region()")
Assisted-by LLM

---
Prepared with Claude Code (Anthropic), model Claude Opus 5.5 (claude-opus-5-5).

Note that the wrong ordering in mmc35240_resume() predates afc8e3ee8307; the sync in cache-only mode just happened to work before, and afc8e3ee8307 only exposed it.
So the Fixes tag might better point to the commit that introduced the resume handler:

553a776b7908 ("iio: magnetometer: mmc35240: Add PM sleep support")

Best regards,
Phuc

#3 lore raw

PATCH 2/2 iio: magnetometer: mmc5633: leave cache-only mode before the cache sync

In reply to Shashank Mohan Jain

mmc5633_resume() marks the register cache dirty and calls regcache_sync_region() while the regmap is still in cache-only mode. It calls regcache_cache_only(false) only after the sync.

Since commit afc8e3ee8307 ("regcache: Validate cache_only state in regcache_sync_region()") a sync in cache-only mode is refused with a WARN and -EINVAL. Every resume now gives

  WARNING: drivers/base/regmap/regcache.c:536 at regcache_sync_region+0x1a4/0x1c0
  ...
   mmc5633_resume+0x2d/0x70
   dpm_run_callback+0x7d/0x1c0
   device_resume+0x175/0x270
  ...
  mmc5633_i2c 1-0030: Failed to restore control registers

and the control registers are not written. If the chip lost its state during suspend, it runs with the reset value of CTRL1 while the cache, and with it the sampling frequency attribute, still has the configured value. Writing the same frequency again does not repair this, because regmap_update_bits() finds no difference to the cache.

Leave cache-only mode first, then mark the cache dirty and sync it.

Reproduced in qemu with the I2C driver bound to an i2c-stub device: set in_magn_sampling_frequency to 3.5 (CTRL1 = 0x02), clear CTRL0 and CTRL1 of the stub behind the driver's back to emulate the power loss, and run a suspend/resume cycle with "devices" in /sys/power/pm_test. Without this change CTRL1 is still 0x00 after resume and the warning is printed. With it, and also with the regcache commit reverted, CTRL1 is 0x02 again and there is no warning. Not tested on real hardware.

Fixes afc8e3ee8307 ("regcache: Validate cache_only state in regcache_sync_region()")
Assisted-by LLM

---
Prepared with Claude Code (Anthropic), model Claude Opus 5.5 (claude-opus-5-5).

 drivers/iio/magnetometer/mmc5633.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iio/magnetometer/mmc5633.c +1 -2
index f4dae7ba13..b651f01e1b 100644
@@ -499,13 +499,12 @@ static int mmc5633_resume(struct device *dev)
 	struct regmap *regmap = dev_get_regmap(dev, NULL);
 	int ret;
 
+	regcache_cache_only(regmap, false);
 	regcache_mark_dirty(regmap);
 	ret = regcache_sync_region(regmap, MMC5633_REG_CTRL0, MMC5633_REG_CTRL1);
 	if (ret)
 		dev_err(dev, "Failed to restore control registers\n");
 
-	regcache_cache_only(regmap, false);
-
 	return 0;
 }
 
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH 2/2] iio: magnetometer: mmc5633: leave cache-only mode before the cache sync

Hi Shashank,

Thanks for the fix.

Quoted 7 lines
Fixes afc8e3ee8307 ("regcache: Validate cache_only state in regcache_sync_region()")
Assisted-by LLM

---
Prepared with Claude Code (Anthropic), model Claude Opus 5.5 (claude-opus-5-5).

Note that the wrong ordering in mmc5633_resume() predates afc8e3ee8307; the sync in cache-only mode just happened to work before, and afc8e3ee8307 only exposed it.
So the Fixes tag might better point to the commit that introduced the resume handler:

6e5f6bf2e3f0 ("iio: magnetometer: Add mmc5633 sensor")

Best regards,
Phuc

#5 lore raw
In reply to Shashank Mohan Jain

Hi Shashank,

Thanks for finding this.

Quoted 6 lines

The Fixes tags name the regcache commit, because that is where the behaviour changed: the drivers have used this order since their resume callbacks were added, and it worked until v7.2. Please tell me if you would rather have the driver commits there.

Cache-only mode has to be disabled before the sync, otherwise the sync makes no sense. mmc35240 and mmc5633 have been misusing
the API since their resume callbacks were added, it only worked by accident. 86fc3483a4d6 and afc8e3ee8307 just catch this misuse.

So please point the Fixes tags at the commits that added the resume callbacks, so the fixes get backported correctly. I've commented on each patch.

Best regards,
Phuc