Skip to contentSkip to conversation

iio: magnetometer: bmc150: use FIELD_PREP and FIELD_GET helpers

PATCH Superseded Hungyu Lin 2 participants 4 messages Jun 9 IIO

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH v3 1/1 iio: magnetometer: bmc150: use FIELD_PREP and FIELD_GET helpers

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.

---
v3:
- Add missing include <linux/bitfield.h>

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 | 34 ++++++++++++--------------
 1 file changed, 16 insertions(+), 18 deletions(-)
drivers/iio/magnetometer/bmc150_magn.c +16 -18
index bf2551988008..fff7e88ef6a8 100644
@@ -13,6 +13,7 @@
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
 #include <linux/cleanup.h>
+#include <linux/bitfield.h>
 #include <linux/delay.h>
 #include <linux/slab.h>
 #include <linux/pm.h>
@@ -245,14 +246,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 +291,8 @@ static int bmc150_magn_get_odr(struct bmc150_magn_data *data, int *val)
 	ret = regmap_read(data->regmap, BMC150_MAGN_REG_OPMODE_ODR, &reg_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 +305,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 +798,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;
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 5 lines · 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.

...

Quoted 2 lines

v3:
- Add missing include <linux/bitfield.h>

You need to slow down. The comment I gave from the previous round still applies. Now even more, id est the location of new header is chosen in suboptimal way. Always try to keep as much as possible headers sorted.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#2 lore raw
Quoted 3 lines

You need to slow down. The comment I gave from the previous round still applies. Now even more, id est the location of new header is chosen in suboptimal way. Always try to keep as much as possible headers sorted.

Thanks for the review.

I'll remove the blank line.

Could you suggest the preferred placement
for <linux/bitfield.h> in this include list?

Thanks,
Hungyu

#3 lore raw
Quoted 10 lines · Hungyu Lin
Quoted 3 lines

You need to slow down. The comment I gave from the previous round still applies. Now even more, id est the location of new header is chosen in suboptimal way. Always try to keep as much as possible headers sorted.

Thanks for the review.

I'll remove the blank line.

Could you suggest the preferred placement
for <linux/bitfield.h> in this include list?

As I said, create an additional (prerequisite) patch that sorts them first.

Signature · 2 lines
With Best Regards,
Andy Shevchenko