* [PATCH v2] iio: magnetometer: bmc150: use FIELD_PREP and FIELD_GET helpers
@ 2026-06-09 1:21 Hungyu Lin
2026-06-09 14:29 ` Andy Shevchenko
0 siblings, 1 reply; 2+ messages in thread
From: Hungyu Lin @ 2026-06-09 1:21 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, linux-iio, linux-kernel, Hungyu Lin
Replace open-coded bitfield operations with FIELD_PREP() and
FIELD_GET() helpers where appropriate.
Also simplify bmc150_magn_set_odr() by returning directly from
the matching table entry.
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
v2:
- Use FIELD_PREP() and FIELD_GET() helpers as suggested by
Jonathan Cameron
- Simplify bmc150_magn_set_odr() by returning directly from
the matching table entry
drivers/iio/magnetometer/bmc150_magn.c | 33 ++++++++++++--------------
1 file changed, 15 insertions(+), 18 deletions(-)
diff --git a/drivers/iio/magnetometer/bmc150_magn.c b/drivers/iio/magnetometer/bmc150_magn.c
index bf2551988008..716b6a088c33 100644
--- a/drivers/iio/magnetometer/bmc150_magn.c
+++ b/drivers/iio/magnetometer/bmc150_magn.c
@@ -245,14 +245,14 @@ static int bmc150_magn_set_power_mode(struct bmc150_magn_data *data,
return regmap_update_bits(data->regmap,
BMC150_MAGN_REG_OPMODE_ODR,
BMC150_MAGN_MASK_OPMODE,
- BMC150_MAGN_MODE_SLEEP <<
- BMC150_MAGN_SHIFT_OPMODE);
+ FIELD_PREP(BMC150_MAGN_MASK_OPMODE,
+ BMC150_MAGN_MODE_SLEEP));
case BMC150_MAGN_POWER_MODE_NORMAL:
return regmap_update_bits(data->regmap,
BMC150_MAGN_REG_OPMODE_ODR,
BMC150_MAGN_MASK_OPMODE,
- BMC150_MAGN_MODE_NORMAL <<
- BMC150_MAGN_SHIFT_OPMODE);
+ FIELD_PREP(BMC150_MAGN_MASK_OPMODE,
+ BMC150_MAGN_MODE_NORMAL));
}
return -EINVAL;
@@ -290,7 +290,8 @@ static int bmc150_magn_get_odr(struct bmc150_magn_data *data, int *val)
ret = regmap_read(data->regmap, BMC150_MAGN_REG_OPMODE_ODR, ®_val);
if (ret < 0)
return ret;
- odr_val = (reg_val & BMC150_MAGN_MASK_ODR) >> BMC150_MAGN_SHIFT_ODR;
+
+ odr_val = FIELD_GET(BMC150_MAGN_MASK_ODR, reg_val);
for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++)
if (bmc150_magn_samp_freq_table[i].reg_val == odr_val) {
@@ -303,21 +304,17 @@ static int bmc150_magn_get_odr(struct bmc150_magn_data *data, int *val)
static int bmc150_magn_set_odr(struct bmc150_magn_data *data, int val)
{
- int ret;
u8 i;
for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++) {
- if (bmc150_magn_samp_freq_table[i].freq == val) {
- ret = regmap_update_bits(data->regmap,
- BMC150_MAGN_REG_OPMODE_ODR,
- BMC150_MAGN_MASK_ODR,
- bmc150_magn_samp_freq_table[i].
- reg_val <<
- BMC150_MAGN_SHIFT_ODR);
- if (ret < 0)
- return ret;
- return 0;
- }
+ if (bmc150_magn_samp_freq_table[i].freq != val)
+ continue;
+
+ return regmap_update_bits(data->regmap,
+ BMC150_MAGN_REG_OPMODE_ODR,
+ BMC150_MAGN_MASK_ODR,
+ FIELD_PREP(BMC150_MAGN_MASK_ODR,
+ bmc150_magn_samp_freq_table[i].reg_val));
}
return -EINVAL;
@@ -800,7 +797,7 @@ static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger *trig,
ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_INT_DRDY,
BMC150_MAGN_MASK_DRDY_EN,
- state << BMC150_MAGN_SHIFT_DRDY_EN);
+ FIELD_PREP(BMC150_MAGN_MASK_DRDY_EN, state));
if (ret < 0)
return ret;
--
2.34.1
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH v2] iio: magnetometer: bmc150: use FIELD_PREP and FIELD_GET helpers
2026-06-09 1:21 [PATCH v2] iio: magnetometer: bmc150: use FIELD_PREP and FIELD_GET helpers Hungyu Lin
@ 2026-06-09 14:29 ` Andy Shevchenko
0 siblings, 0 replies; 2+ messages in thread
From: Andy Shevchenko @ 2026-06-09 14:29 UTC (permalink / raw)
To: Hungyu Lin
Cc: Jonathan Cameron, David Lechner, Nuno Sá,
Andy Shevchenko, linux-iio, linux-kernel
On Tue, Jun 09, 2026 at 01:21:45AM +0000, Hungyu Lin wrote:
> Replace open-coded bitfield operations with FIELD_PREP() and
> FIELD_GET() helpers where appropriate.
>
> Also simplify bmc150_magn_set_odr() by returning directly from
> the matching table entry.
...
> ret = regmap_read(data->regmap, BMC150_MAGN_REG_OPMODE_ODR, ®_val);
> if (ret < 0)
> return ret;
> - odr_val = (reg_val & BMC150_MAGN_MASK_ODR) >> BMC150_MAGN_SHIFT_ODR;
> +
Stray addition. This calculation is coupled with the above regmap, so I see
a value of having no blank line there (as in the original code).
> + odr_val = FIELD_GET(BMC150_MAGN_MASK_ODR, reg_val);
>
> for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++)
...
The rest looks good to me, I dunno if Jonathan wants a new version (probably
he does as IIO closed for this cycle anyway).
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-06-09 14:29 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-06-09 1:21 [PATCH v2] iio: magnetometer: bmc150: use FIELD_PREP and FIELD_GET helpers Hungyu Lin
2026-06-09 14:29 ` 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®