Skip to contentSkip to conversation

iio: magnetometer: bmc150: use FIELD_PREP and FIELD_GET helpers

PATCH Superseded Hungyu Lin 3 participants 8 messages Jun 10 IIO

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

v4 cover 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.

v4:
- Split include sorting into a separate prerequisite patch
- Remove the extra blank line in bmc150_magn_get_odr()

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 v4 1/2 iio: magnetometer: bmc150: sort includes

Sort the include list alphabetically.

---
 drivers/iio/magnetometer/bmc150_magn.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/iio/magnetometer/bmc150_magn.c +8 -8
index bf2551988008..6f2a4a863fbe 100644
@@ -9,23 +9,23 @@
  * (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/pm.h>
-#include <linux/pm_runtime.h>
-#include <linux/iio/iio.h>
-#include <linux/iio/sysfs.h>
+#include <linux/i2c.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/interrupt.h>
+#include <linux/module.h>
+#include <linux/pm.h>
+#include <linux/pm_runtime.h>
 #include <linux/regmap.h>
 #include <linux/regulator/consumer.h>
+#include <linux/slab.h>
 
 #include "bmc150_magn.h"
 
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] iio: magnetometer: bmc150: sort includes

Quoted 1 line · Hungyu Lin

Sort the include list alphabetically.

...

Quoted 9 lines
drivers/iio/magnetometer/bmc150_magn.c +0 -7
-#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/pm.h>
-#include <linux/pm_runtime.h>
-#include <linux/iio/iio.h>

drivers/iio/magnetometer/bmc150_magn.c

Quoted 10 lines
drivers/iio/magnetometer/bmc150_magn.c +3 -1
-#include <linux/iio/sysfs.h>
+#include <linux/i2c.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>

Oh, I didn't noticed this one is mixed with the other linux/*.h.

drivers/iio/magnetometer/bmc150_magn.c line 28

Quoted 7 lines
drivers/iio/magnetometer/bmc150_magn.c +5 -0
+#include <linux/interrupt.h>
+#include <linux/module.h>
+#include <linux/pm.h>
+#include <linux/pm_runtime.h>
 #include <linux/regmap.h>
 #include <linux/regulator/consumer.h>
+#include <linux/slab.h>

Please, make it in two groups

linux/*.h
...blank line...
linux/iio/*.h
...blank line...
"...internal header..."

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

PATCH v4 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 | 34 ++++++++++++--------------
 1 file changed, 16 insertions(+), 18 deletions(-)
drivers/iio/magnetometer/bmc150_magn.c +16 -18
index 6f2a4a863fbe..c402ac852efb 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>
@@ -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
author
#4 lore raw

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

Quoted 3 lines
drivers/iio/magnetometer/bmc150_magn.c +2 -1
-       odr_val = (reg_val & BMC150_MAGN_MASK_ODR) >> BMC150_MAGN_SHIFT_ODR;
+
+       odr_val = FIELD_GET(BMC150_MAGN_MASK_ODR, reg_val);

Thanks Andy.

The blank line issue was inadvertently retained in v4 and has been fixed in v5.

Thanks,
Hungyu

#5 lore raw
In reply to Hungyu Lin
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.

...

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

Quoted 3 lines
drivers/iio/magnetometer/bmc150_magn.c +2 -1
-	odr_val = (reg_val & BMC150_MAGN_MASK_ODR) >> BMC150_MAGN_SHIFT_ODR;
+
+	odr_val = FIELD_GET(BMC150_MAGN_MASK_ODR, reg_val);

Same comment as per previous few versions. I don't understand why you haven't addressed it. Did I miss your reply on this?

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

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

Quoted 3 lines
drivers/iio/magnetometer/bmc150_magn.c +2 -1
-     odr_val = (reg_val & BMC150_MAGN_MASK_ODR) >> BMC150_MAGN_SHIFT_ODR;
+
+     odr_val = FIELD_GET(BMC150_MAGN_MASK_ODR, reg_val);

Same comment as per previous few versions. I don't understand why you haven't addressed it. Did I miss your reply on this?

You didn't miss it.

After sending v4, I noticed that the blank line issue had not actually been fixed and sent v5 shortly afterwards.

The include grouping issue you pointed out was then addressed in v6.

Thanks,
Hungyu

#7 lore raw
In reply to Hungyu Lin
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.

Thanks, v4 is almost good. A couple of nit-picks, and if get addressed, I will give my tag for that new version (presumably v5).

Signature · 2 lines
With Best Regards,
Andy Shevchenko