Re: [RFC PATCH v2 1/2] regmap: add regmap_reg_field_* APIs

From: Victor Chernyakin

Date: Sat Nov 14 2026 - 17:20:14 EST


> > > > -#define REG_FIELD(_reg, _lsb, _msb) { \
> > > > - .reg = _reg, \
> > > > - .lsb = _lsb, \
> > > > - .msb = _msb, \
> > > > - }
> > > > +#define REG_FIELD(_reg, _lsb, _msb) ((struct reg_field){ \
> > > > + .reg = _reg, \
> > > > + .lsb = _lsb, \
> > > > + .msb = _msb, \
> > > > + })
> > > >
> > > > -#define REG_FIELD_ID(_reg, _lsb, _msb, _size, _offset) { \
> > > > - .reg = _reg, \
> > > > - .lsb = _lsb, \
> > > > - .msb = _msb, \
> > > > - .id_size = _size, \
> > > > - .id_offset = _offset, \
> > > > - }
> > > > +#define REG_FIELD_ID(_reg, _lsb, _msb, _size, _offset) ((struct reg_field){ \
> > > > + .reg = _reg, \
> > > > + .lsb = _lsb, \
> > > > + .msb = _msb, \
> > > > + .id_size = _size, \
> > > > + .id_offset = _offset, \
> > > > + })
> > >
> > > So, you convert them to compound literals. This change may be good on
> > > its own (see other similar changes in the kernel).
> > > The only problem you have is the outer parentheses. They should be
> > > dropped. If you need them, it's a problem on the user's side of this
> > > macros.
> >
> > In this comment:
> >
> > https://lore.kernel.org/all/auzCxxkUB9p6WKNC@ashevche-desk.local/
> >
> > ...I got the impresssion that you were *against* dealing with it
> > on the user's side (in REG_FIELD_GENMASK), and I agreed:>
>
> Yes, that's true and I confirm that it's not a business to handle
> expressions in REG_FIELD_GENMASK(). We imply that the parameter there
> is a regfield.>
>
> > if it > can be handled in REG_FIELD, why make users worry about it?>
>
> But REG_FIELD is also a macro, it's not their business either.>
>
> > But now it seems you're saying the opposite.>
>
> Nothing is opposite. The statement is the same, dealing with the
> expressions should be done on the caller's side in REG_FIELD_GENMASK()
> and REG_FIELD() (as the latter is also a macro).>
>
> > Can you clarify
> > your stance?>
>
> I assume you are trying to have a use of it like in __uart_port_tx().
> See also uart_port_tx().

I don't necessarily care about putting whole expressions in there.
All I want is for the following code to Just Work:

#define MSA311_F_SOFT_RESET_I2C REG_FIELD(MSA311_SOFT_RESET_REG, 2, 2)
u32 mask = REG_FIELD_GENMASK(MSA311_F_SOFT_RESET_I2C);

For that, there has to be an extra pair of parens *somewhere*.
Now, I hope you'd agree that making users write parens like this:

#define MSA311_F_SOFT_RESET_I2C (REG_FIELD(MSA311_SOFT_RESET_REG, 2, 2))

or this:

u32 mask = REG_FIELD_GENMASK((MSA311_F_SOFT_RESET_I2C));

would look a bit weird. And if we're not putting the parens in the
REG_FIELD_GENMASK definition, that leaves the REG_FIELD definition.

The only alternative I see is making users define REG_FIELDs not as
a macros, but variables, i.e.:

static const struct reg_field msa311_f_soft_reset_i2c = REG_FIELD(MSA311_SOFT_RESET_REG, 2, 2);

But that would lead to a lot of line wrapping, and would mean that
registers and register fields now use different capitalization styles.