mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v1 1/1] regmap: Add REG_FIELD_GENMASK() helper macro
@ 2026-11-05 10:34 Andy Shevchenko
  2026-11-05 15:39 ` Victor Chernyakin
  0 siblings, 1 reply; 3+ messages in thread
From: Andy Shevchenko @ 2026-11-05 10:34 UTC (permalink / raw)
  To: Mark Brown, Peng Fan, linux-kernel, driver-core
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Johan Hovold, Victor Chernyakin, Andy Shevchenko

One time in regmap and already a few times over the whole kernel
the users want to extract a mask from the regmap field data type.
Provide a helper and convert regmap core to use it. Others may
follow later on.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---

Inspired by a patch SN1PR12MB25282EC93A27F3577BB9B454A5772@SN1PR12MB2528.namprd12.prod.outlook.com
that leaves the driver to handle the custom macro for that.

 drivers/base/regmap/regmap.c | 2 +-
 include/linux/regmap.h       | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index 83f1f5942135..48e96bc9e7b6 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -1215,7 +1215,7 @@ static void regmap_field_init(struct regmap_field *rm_field,
 	rm_field->regmap = regmap;
 	rm_field->reg = reg_field.reg;
 	rm_field->shift = reg_field.lsb;
-	rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
+	rm_field->mask = REG_FIELD_GENMASK(reg_field);
 
 	WARN_ONCE(rm_field->mask == 0, "invalid empty mask defined\n");
 
diff --git a/include/linux/regmap.h b/include/linux/regmap.h
index a72f17ab6264..8f3034881f8a 100644
--- a/include/linux/regmap.h
+++ b/include/linux/regmap.h
@@ -10,6 +10,7 @@
  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  */
 
+#include <linux/bits.h>
 #include <linux/bug.h>
 #include <linux/cleanup.h>
 #include <linux/delay.h>
@@ -1477,6 +1478,8 @@ struct reg_field {
 				.id_offset = _offset,		\
 				}
 
+#define REG_FIELD_GENMASK(_reg_field)	GENMASK(_reg_field.msb, _reg_field.lsb)
+
 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
 		struct reg_field reg_field);
 void regmap_field_free(struct regmap_field *field);
-- 
2.50.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v1 1/1] regmap: Add REG_FIELD_GENMASK() helper macro
  2026-11-05 10:34 [PATCH v1 1/1] regmap: Add REG_FIELD_GENMASK() helper macro Andy Shevchenko
@ 2026-11-05 15:39 ` Victor Chernyakin
  2026-11-05 17:54   ` Andy Shevchenko
  0 siblings, 1 reply; 3+ messages in thread
From: Victor Chernyakin @ 2026-11-05 15:39 UTC (permalink / raw)
  To: andriy.shevchenko
  Cc: broonie, chernyakin.victor.j, dakr, driver-core, gregkh, johan,
	linux-kernel, peng.fan, rafael

> #define REG_FIELD_GENMASK(_reg_field)	GENMASK(_reg_field.msb, _reg_field.lsb)

Macro parameter should have parens around it:

+ #define REG_FIELD_GENMASK(_reg_field)	GENMASK((_reg_field).msb, (_reg_field).lsb)

(In the patch you link to, I wrote it without parens at first too,
then had to figure out why I was getting "too many arguments to macro"
error messages)




BTW, I've been thinking about making reg_field store the mask directly,
by putting GENMASK(lsb, msb) in the REG_FIELD macro, but that's probably
a more involved change.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v1 1/1] regmap: Add REG_FIELD_GENMASK() helper macro
  2026-11-05 15:39 ` Victor Chernyakin
@ 2026-11-05 17:54   ` Andy Shevchenko
  0 siblings, 0 replies; 3+ messages in thread
From: Andy Shevchenko @ 2026-11-05 17:54 UTC (permalink / raw)
  To: Victor Chernyakin
  Cc: broonie, dakr, driver-core, gregkh, johan, linux-kernel,
	peng.fan, rafael

On Thu, Nov 05, 2026 at 09:39:45AM -0600, Victor Chernyakin wrote:
> > #define REG_FIELD_GENMASK(_reg_field)	GENMASK(_reg_field.msb, _reg_field.lsb)
> 
> Macro parameter should have parens around it:

In this case no, it shouldn't. It a shortcut which assumes that _reg_field is
not an expression.

> + #define REG_FIELD_GENMASK(_reg_field)	GENMASK((_reg_field).msb, (_reg_field).lsb)

This doesn't look good as it implies that _reg_field can be an expression that
will be evaluated twice.

> (In the patch you link to, I wrote it without parens at first too,
> then had to figure out why I was getting "too many arguments to macro"
> error messages)

This is strange.

I have compiled it with GCC-15 and clang-22 (in both cases sparse enabled),
no such a problem (It's also -Werror build). Can you elaborate?

> BTW, I've been thinking about making reg_field store the mask directly,
> by putting GENMASK(lsb, msb) in the REG_FIELD macro, but that's probably
> a more involved change.

But it's already there as you see in the regmap core. What you want is to make
it at the beginning. Not sure if it's feasible and needed change.

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-05 17:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-05 10:34 [PATCH v1 1/1] regmap: Add REG_FIELD_GENMASK() helper macro Andy Shevchenko
2026-11-05 15:39 ` Victor Chernyakin
2026-11-05 17:54   ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®