Skip to contentSkip to conversation

iio: magnetometer: bmc150: use FIELD_PREP and FIELD_GET helpers

PATCH In review Hungyu Lin 3 participants 6 messages Jun 10 IIO

lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

v6 cover iio: magnetometer: bmc150: use FIELD_PREP and FIELD_GET helpers

v6:
- Group linux/*.h and linux/iio/*.h includes separately as suggested by Andy Shevchenko

v5:
- Remove the extra blank line in bmc150_magn_get_odr() that was inadvertently retained in v4

v4:
- Split include sorting into a separate prerequisite patch

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

Hungyu Lin (2):
iio: magnetometer: bmc150: sort includes
iio: magnetometer: bmc150: use FIELD_PREP and FIELD_GET helpers

 drivers/iio/magnetometer/bmc150_magn.c | 50 +++++++++++++-------------
 1 file changed, 24 insertions(+), 26 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v6 1/2 iio: magnetometer: bmc150: sort includes

Sort the include list alphabetically.

---
 drivers/iio/magnetometer/bmc150_magn.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
drivers/iio/magnetometer/bmc150_magn.c +9 -8
index bf2551988008..f6639d7fab04 100644
@@ -9,23 +9,24 @@
  * (C) Copyright 2011~2014 Bosch Sensortec GmbH All Rights Reserved
  */
 
-#include <linux/module.h>
-#include <linux/i2c.h>
-#include <linux/interrupt.h>
 #include <linux/cleanup.h>
 #include <linux/delay.h>
-#include <linux/slab.h>
+#include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/module.h>
 #include <linux/pm.h>
 #include <linux/pm_runtime.h>
-#include <linux/iio/iio.h>
-#include <linux/iio/sysfs.h>
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
+#include <linux/slab.h>
+
 #include <linux/iio/buffer.h>
 #include <linux/iio/events.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
 #include <linux/iio/trigger.h>
 #include <linux/iio/trigger_consumer.h>
 #include <linux/iio/triggered_buffer.h>
-#include <linux/regmap.h>
-#include <linux/regulator/consumer.h>
 
 #include "bmc150_magn.h"
 
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v6 2/2 iio: magnetometer: bmc150: use FIELD_PREP and FIELD_GET helpers

In reply to 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.

---
 drivers/iio/magnetometer/bmc150_magn.c | 33 ++++++++++++--------------
 1 file changed, 15 insertions(+), 18 deletions(-)
drivers/iio/magnetometer/bmc150_magn.c +15 -18
index f6639d7fab04..ee8092d4006c 100644
@@ -9,6 +9,7 @@
  * (C) Copyright 2011~2014 Bosch Sensortec GmbH All Rights Reserved
  */
 
+#include <linux/bitfield.h>
 #include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/i2c.h>
@@ -246,14 +247,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;
@@ -291,7 +292,7 @@ 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) {
@@ -304,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;
@@ -801,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
maintainer
#3 lore raw
Quoted 7 lines · On Wed, 10 Jun 2026 13:58:31 +0000 Hungyu Lin <[email protected]> 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.

Andy mentioned in previous patch - slow down! More than one version a day is very rarely appropriate. That creates a lot of noise on the list and means that you aren't gathering up multiple bits of feedback before posting a new verison.

Anything I apply for IIO is now 7.3 material so this isn't going upstream for about 3 months anyway so take your time!

The alignment is not the preferred amount for pretty much all cases. Rather than go to v7, applied but with this fix up:
Note that it is on the testing branch of iio.git which will get rebased on rc1 once available in about 3 weeks time.

drivers/iio/magnetometer/bmc150_magn.c +6 -6
index ee8092d4006c..32dff9cdd0f7 100644
@@ -248,13 +248,13 @@ static int bmc150_magn_set_power_mode(struct bmc150_magn_data *data,
                                          BMC150_MAGN_REG_OPMODE_ODR,
                                          BMC150_MAGN_MASK_OPMODE,
                                          FIELD_PREP(BMC150_MAGN_MASK_OPMODE,
-                                                 BMC150_MAGN_MODE_SLEEP));
+                                                    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,
                                          FIELD_PREP(BMC150_MAGN_MASK_OPMODE,
-                                                 BMC150_MAGN_MODE_NORMAL));
+                                                    BMC150_MAGN_MODE_NORMAL));
        }
 
        return -EINVAL;
@@ -312,10 +312,10 @@ static int bmc150_magn_set_odr(struct bmc150_magn_data *data, int 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));
+                                         BMC150_MAGN_REG_OPMODE_ODR,
+                                         BMC150_MAGN_MASK_ODR,
+                                         FIELD_PREP(BMC150_MAGN_MASK_ODR,
+                                                    bmc150_magn_samp_freq_table[i].reg_val));
        }
 
Quoted 17 lines
---
 drivers/iio/magnetometer/bmc150_magn.c | 33 ++++++++++++--------------
 1 file changed, 15 insertions(+), 18 deletions(-)
drivers/iio/magnetometer/bmc150_magn.c +1 -0
index f6639d7fab04..ee8092d4006c 100644
@@ -9,6 +9,7 @@
  * (C) Copyright 2011~2014 Bosch Sensortec GmbH All Rights Reserved
  */
 
+#include <linux/bitfield.h>
 #include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/i2c.h>

drivers/iio/magnetometer/bmc150_magn.c static int bmc150_magn_set_power_mode(struct bmc150_magn_data *data, · line 251

Quoted 7 lines
drivers/iio/magnetometer/bmc150_magn.c +2 -2
@@ -246,14 +247,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));

Should be aligned as:

					  FIELD_PREP(BMC150_MAGN_MASK_OPMODE,
						     BMC150_MAGN_MODE_SLEEP));

drivers/iio/magnetometer/bmc150_magn.c static int bmc150_magn_set_power_mode(struct bmc150_magn_data *data, · line 257

Quoted 8 lines
drivers/iio/magnetometer/bmc150_magn.c +2 -2
 	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));

Same here.

Quoted 29 lines
drivers/iio/magnetometer/bmc150_magn.c +1 -11
 	}
 
 	return -EINVAL;
@@ -291,7 +292,7 @@ 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) {
@@ -304,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;

drivers/iio/magnetometer/bmc150_magn.c static int bmc150_magn_get_odr(struct bmc150_magn_data *data, int *val) · line 318

Quoted 10 lines
drivers/iio/magnetometer/bmc150_magn.c +8 -2
-			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));

and here should be

		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));
Quoted 12 lines
drivers/iio/magnetometer/bmc150_magn.c +1 -1
 	}
 
 	return -EINVAL;
@@ -801,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;
 
author
#4 lore raw
Quoted 4 lines

The alignment is not the preferred amount for pretty much all cases. Rather than go to v7, applied but with this fix up:
Note that it is on the testing branch of iio.git which will get rebased on rc1 once available in about 3 weeks time.

Thanks for the review and for applying the series.

I appreciate the fixup and will be more careful about posting multiple revisions in quick succession in the future.

Thanks,
Hungyu