regmap: Add debugfs file for forcing field writes
Subsystems (2)
PATCH v2 1/1 regmap: Add debugfs file for forcing field writes
_regmap_update_bits() checks if the current register value differs from the new value, and only writes to the register if they differ. When testing hardware drivers, it might be desirable to always force a register write, for example when writing to a regmap_field. This enables and simplifies testing and verification of the hardware interaction. For example, when using a hardware mock/simulation model, one can then more easily verify that the driver makes the correct expected register writes during certain events.
Add a bool variable force_write_field and a corresponding debugfs entry to enable this. Since this feature could interfere with driver operation, guard it with a macro.
---
Changes in v2:
* Add macro to guard the debugfs entry.
* Fix Signed-off-by in commit message to match actual email address.
* Link to v1: https://lore.kernel.org/all/[email protected]/
drivers/base/regmap/internal.h | 3 +++ drivers/base/regmap/regmap-debugfs.c | 11 +++++++++++ drivers/base/regmap/regmap.c | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-)
drivers/base/regmap/internal.h +3 -0
@@ -125,6 +125,9 @@ struct regmap {int reg_stride;int reg_stride_order; +/* If set, will always write field to HW. */ +bool force_write_field; +/* regcache specific members */ const struct regcache_ops *cache_ops;enum regcache_type cache_type;
drivers/base/regmap/regmap-debugfs.c +11 -0
@@ -636,6 +636,17 @@ void regmap_debugfs_init(struct regmap *map) ®map_cache_bypass_fops); } +/* +* This could interfere with driver operation. Therefore, don't provide +* any real compile time configuration option for this feature. One will +* have to modify the source code directly in order to use it. +*/ +#undef REGMAP_ALLOW_FORCE_WRITE_FIELD_DEBUGFS +#ifdef REGMAP_ALLOW_FORCE_WRITE_FIELD_DEBUGFS +debugfs_create_bool ("force_write_field" ,0600 , map->debugfs, + &map->force_write_field); +#endif + next =rb_first (&map->range_tree);while (next) { range_node = rb_entry(next, struct regmap_range_node, node);
drivers/base/regmap/regmap.c +1 -1
@@ -3273,7 +3273,7 @@ static int _regmap_update_bits(struct regmap *map, unsigned int reg, tmp = orig & ~mask; tmp |= val & mask; -if (force_write || (tmp != orig)) { +if (force_write || (tmp != orig) || map->force_write_field) { ret =_regmap_write (map, reg, tmp);if (ret ==0 && change) *change = true;
base-commit: 858fd168a95c5b9669aac8db6c14a9aeab446375
Signature · 1 line
2.30.2
Greg Kroah-Hartman
maintainerQuoted 47 lines · Waqar Hameed
_regmap_update_bits()checks if the current register value differs from the new value, and only writes to the register if they differ. When testing hardware drivers, it might be desirable to always force a register write, for example when writing to aregmap_field. This enables and simplifies testing and verification of the hardware interaction. For example, when using a hardware mock/simulation model, one can then more easily verify that the driver makes the correct expected register writes during certain events.Add a bool variable
force_write_fieldand a corresponding debugfs entry to enable this. Since this feature could interfere with driver operation, guard it with a macro.Signed-off-by Waqar Hameed <[email protected]>---
Changes in v2:
* Add macro to guard the debugfs entry.
* FixSigned-off-byin commit message to match actual email address.
* Link to v1: https://lore.kernel.org/all/[email protected]/drivers/base/regmap/internal.h | 3 +++ drivers/base/regmap/regmap-debugfs.c | 11 +++++++++++ drivers/base/regmap/regmap.c | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-)drivers/base/regmap/internal.h +3
-0@@ -125,6 +125,9 @@ struct regmap {int reg_stride;int reg_stride_order; +/* If set, will always write field to HW. */ +bool force_write_field; +/* regcache specific members */ const struct regcache_ops *cache_ops;enum regcache_type cache_type;drivers/base/regmap/regmap-debugfs.c +0
-0@@ -636,6 +636,17 @@ void regmap_debugfs_init(struct regmap *map) ®map_cache_bypass_fops); }
drivers/base/regmap/regmap-debugfs.c void regmap_debugfs_init(struct regmap *map) · line 648
Quoted 10 lines
drivers/base/regmap/regmap-debugfs.c +10
-0+/* +* This could interfere with driver operation. Therefore, don't provide +* any real compile time configuration option for this feature. One will +* have to modify the source code directly in order to use it. +*/ +#undef REGMAP_ALLOW_FORCE_WRITE_FIELD_DEBUGFS +#ifdef REGMAP_ALLOW_FORCE_WRITE_FIELD_DEBUGFS +debugfs_create_bool ("force_write_field" ,0600 , map->debugfs, + &map->force_write_field); +#endif
Please no, that means this will never ever ever get used, and if it happens to break the build or runtime, no one will ever notice it.
If you need this for your device/tree/distro, great, just keep it as an out-of-tree patch with the huge header "NEVER ENABLE THIS IN A REAL SYSTEM" or something like that.
But as-is, we can't take this, sorry.
greg k-h
Quoted 18 lines · Greg Kroah-Hartman
drivers/base/regmap/regmap-debugfs.c void regmap_debugfs_init(struct regmap *map) · line 648
Quoted 13 lines · Waqar Hameed
_regmap_update_bits()checks if the current register value differs from the new value, and only writes to the register if they differ. Whendrivers/base/regmap/regmap-debugfs.c +10
-0+/* +* This could interfere with driver operation. Therefore, don't provide +* any real compile time configuration option for this feature. One will +* have to modify the source code directly in order to use it. +*/ +#undef REGMAP_ALLOW_FORCE_WRITE_FIELD_DEBUGFS +#ifdef REGMAP_ALLOW_FORCE_WRITE_FIELD_DEBUGFS +debugfs_create_bool ("force_write_field" ,0600 , map->debugfs, + &map->force_write_field); +#endif Please no, that means this will never ever ever get used, and if it happens to break the build or runtime, no one will ever notice it.
Quoted 3 lines
If you need this for your device/tree/distro, great, just keep it as an out-of-tree patch with the huge header "NEVER ENABLE THIS IN A REAL SYSTEM" or something like that.
The ordinary only-write-if-bits-have-changed behavior of _regmap_update_bits would seem to be mostly an optimization to minimize the number of writes to a device. The way it is normally used in the code, it's not easy to predict when it in fact will result in a write, because the whole idea of a function like this is that a driver doesn't have to keep track of which bits are actually set or not, or else the function would not be useful in the first place.
I can understand that enabling a write-always behavior results in a different behavior on the associated bus (e.g. I2C), but in the end it shouldn't affect the functionality of the peripheral device any more than rearranging driver code to perform regmap writes in a different order, which might also lead to fewer or more bus writes.
It seems I'm clearly in the wrong here, so there must be some scenario I've missed. It just doesn't seem that dangerous; it's a debug functionality after all.
/Ricard
Signature · 3 lines
Ricard Wolf Wanderlof ricardw(at)axis.com Axis Communications AB, Lund, Sweden www.axis.com Phone +46 46 272 2016 Fax +46 46 13 61 30
Mark Brown
maintainerQuoted 9 lines · Ricard Wanderlof
I can understand that enabling a write-always behavior results in a different behavior on the associated bus (e.g. I2C), but in the end it shouldn't affect the functionality of the peripheral device any more than rearranging driver code to perform regmap writes in a different order, which might also lead to fewer or more bus writes.
It seems I'm clearly in the wrong here, so there must be some scenario I've missed. It just doesn't seem that dangerous; it's a debug functionality after all.
There are devices where the act of writing to a register can have side effects even if the values in the register are the same (for example triggering some kind of recalibration based on the "new" value). It's rare, and it's probably more likely to be annoying or disruptive than actually dangerous, but it is something drivers might be relying on.
Mark Brown
maintainerQuoted 11 lines · Greg Kroah-Hartman
drivers/base/regmap/regmap-debugfs.c void regmap_debugfs_init(struct regmap *map) · line 648
Quoted 5 lines · Waqar Hameed
drivers/base/regmap/regmap-debugfs.c +5
-0+#undef REGMAP_ALLOW_FORCE_WRITE_FIELD_DEBUGFS +#ifdef REGMAP_ALLOW_FORCE_WRITE_FIELD_DEBUGFS +debugfs_create_bool ("force_write_field" ,0600 , map->debugfs, + &map->force_write_field); +#endif Please no, that means this will never ever ever get used, and if it happens to break the build or runtime, no one will ever notice it.
Quoted 3 lines
If you need this for your device/tree/distro, great, just keep it as an out-of-tree patch with the huge header "NEVER ENABLE THIS IN A REAL SYSTEM" or something like that.
We have an existing setting that parallels this which enables writes via debugfs - there *is* demand for this sort of thing in development where this is a viable configuration mechanism, having something in tree in a way that requires an out of tree patch like you're suggesting to enable it documents why the feature isn't something you can enable without code changes whereas just not having anything means people send patches adding the feature.
This one is very much safer than the general write thing, we probably *could* have a Kconfig option but changing the guarantees underneath drivers feels like it's going to explode if we just let it happen.
Mark Brown
maintainerQuoted 10 lines · Waqar Hameed
_regmap_update_bits()checks if the current register value differs from the new value, and only writes to the register if they differ. When testing hardware drivers, it might be desirable to always force a register write, for example when writing to aregmap_field. This enables and simplifies testing and verification of the hardware interaction. For example, when using a hardware mock/simulation model, one can then more easily verify that the driver makes the correct expected register writes during certain events.[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap.git for-next
Thanks!
[1/1] regmap: Add debugfs file for forcing field writes
commit: b629c698eae745eb51b6d92395e2eee44bbf5f49
All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark