Skip to contentSkip to conversation

iio: accel: adxl380: fix FIFO watermark bit 8 always written as 0

PATCH In review Antoniu Miclaus 4 participants 7 messages Feb 27 – Mar 2

Subsystems (3)
lore
Share & tools
lore

Antoniu Miclaus

author maintainer
#0 lore raw

PATCH 1/1 iio: accel: adxl380: fix FIFO watermark bit 8 always written as 0

FIELD_PREP(BIT(0), fifo_samples & BIT(8)) produces either 0 or 256, and since FIELD_PREP masks to bit 0, 256 & 1 evaluates to 0. Use !! to convert the result to a proper 0-or-1 value.

Fixes df36de13677a ("iio: accel: add ADXL380 driver")
---
 drivers/iio/accel/adxl380.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/accel/adxl380.c +1 -1
index 8fab2fdbe147..a51d1d61c412 100644
@@ -877,7 +877,7 @@ static int adxl380_set_fifo_samples(struct adxl380_state *st)
 	ret = regmap_update_bits(st->regmap, ADXL380_FIFO_CONFIG_0_REG,
 				 ADXL380_FIFO_SAMPLES_8_MSK,
 				 FIELD_PREP(ADXL380_FIFO_SAMPLES_8_MSK,
-					    (fifo_samples & BIT(8))));
+					    !!(fifo_samples & BIT(8))));
 	if (ret)
 		return ret;
 
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 6 lines · On Fri, 27 Feb 2026 14:43:05 +0200 Antoniu Miclaus…

FIELD_PREP(BIT(0), fifo_samples & BIT(8)) produces either 0 or 256, and since FIELD_PREP masks to bit 0, 256 & 1 evaluates to 0. Use !! to convert the result to a proper 0-or-1 value.

Fixes df36de13677a ("iio: accel: add ADXL380 driver")

Applied and marked for stable.

Thanks,

Jonathan

Quoted 17 lines
---
 drivers/iio/accel/adxl380.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/accel/adxl380.c +1 -1
index 8fab2fdbe147..a51d1d61c412 100644
@@ -877,7 +877,7 @@ static int adxl380_set_fifo_samples(struct adxl380_state *st)
 	ret = regmap_update_bits(st->regmap, ADXL380_FIFO_CONFIG_0_REG,
 				 ADXL380_FIFO_SAMPLES_8_MSK,
 				 FIELD_PREP(ADXL380_FIFO_SAMPLES_8_MSK,
-					    (fifo_samples & BIT(8))));
+					    !!(fifo_samples & BIT(8))));
 	if (ret)
 		return ret;
 
reviewer
#2 lore raw
In reply to Antoniu Miclaus

drivers/iio/accel/adxl380.c static int adxl380_set_fifo_samples(struct adxl380_state *st)

Quoted 15 lines · Antoniu Miclaus

FIELD_PREP(BIT(0), fifo_samples & BIT(8)) produces either 0 or 256, and since FIELD_PREP masks to bit 0, 256 & 1 evaluates to 0. Use !! to convert the result to a proper 0-or-1 value.

Fixes df36de13677a ("iio: accel: add ADXL380 driver")
---
 drivers/iio/accel/adxl380.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/accel/adxl380.c +0 -0
index 8fab2fdbe147..a51d1d61c412 100644
@@ -877,7 +877,7 @@ static int adxl380_set_fifo_samples(struct adxl380_state *st)

Some relevant context...

	u16 fifo_samples = st->watermark * st->fifo_set_size;

drivers/iio/accel/adxl380.c static int adxl380_set_fifo_samples(struct adxl380_state *st) · line 880

Quoted 5 lines
drivers/iio/accel/adxl380.c +1 -1
 	ret = regmap_update_bits(st->regmap, ADXL380_FIFO_CONFIG_0_REG,
 				 ADXL380_FIFO_SAMPLES_8_MSK,
 				 FIELD_PREP(ADXL380_FIFO_SAMPLES_8_MSK,
-					    (fifo_samples & BIT(8))));
+					    !!(fifo_samples & BIT(8))));

Technically, this works, but in terms of understanding the code I think fifo_samples >= BIT(8) would make more sense.

fifo_samples is a count, not bit flags.

Quoted 3 lines
drivers/iio/accel/adxl380.c +0 -0
 	if (ret)
 		return ret;
 
maintainer
#3 lore raw
Quoted 30 lines · On Sat, 28 Feb 2026 10:50:00 -0600 David Lechner <[email protected]> wrote:

drivers/iio/accel/adxl380.c static int adxl380_set_fifo_samples(struct adxl380_state *st)

Quoted 15 lines · Antoniu Miclaus

FIELD_PREP(BIT(0), fifo_samples & BIT(8)) produces either 0 or 256, and since FIELD_PREP masks to bit 0, 256 & 1 evaluates to 0. Use !! to convert the result to a proper 0-or-1 value.

Fixes df36de13677a ("iio: accel: add ADXL380 driver")
---
 drivers/iio/accel/adxl380.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/accel/adxl380.c +0 -0
index 8fab2fdbe147..a51d1d61c412 100644
@@ -877,7 +877,7 @@ static int adxl380_set_fifo_samples(struct adxl380_state *st)  

Some relevant context...

	u16 fifo_samples = st->watermark * st->fifo_set_size;

drivers/iio/accel/adxl380.c static int adxl380_set_fifo_samples(struct adxl380_state *st) · line 880

Quoted 5 lines
drivers/iio/accel/adxl380.c +1 -1
 	ret = regmap_update_bits(st->regmap, ADXL380_FIFO_CONFIG_0_REG,
 				 ADXL380_FIFO_SAMPLES_8_MSK,
 				 FIELD_PREP(ADXL380_FIFO_SAMPLES_8_MSK,
-					    (fifo_samples & BIT(8))));
+					    !!(fifo_samples & BIT(8))));  

Technically, this works, but in terms of understanding the code I think fifo_samples >= BIT(8) would make more sense.

Quoted 1 line

fifo_samples is a count, not bit flags.

It's an odd bit of code, but then it's unusual hardware too.
To me it is about bit 8 not the value being big enough to have a bit 8 because if there were yet another register that was for BIT(9) then

Quoted 1 line

= BIT(8) would give the wrong value if BIT(9) was set and BIT(8) was not.

We could express this what is going on in a bit more explicit detail maybe?

	FIELD_PREP(ADXL380_FIFO_SAMPLES_8_MSK,
		   FIELD_GET(BIT(8), fifo_samples));

So there we are extracting the 8th bit and writing to the location for the 8th bit.

Quoted 6 lines
Quoted 5 lines
drivers/iio/accel/adxl380.c +0 -0
 	if (ret)
 		return ret;
 
#4 lore raw
In reply to David Lechner
Quoted 1 line · David Lechner

On 2/27/26 6:43 AM, Antoniu Miclaus wrote:

...

Quoted 6 lines

drivers/iio/accel/adxl380.c static int adxl380_set_fifo_samples(struct adxl380_state *st) · line 880

Quoted 1 line
drivers/iio/accel/adxl380.c +1 -0
+					    !!(fifo_samples & BIT(8))));

Technically, this works, but in terms of understanding the code I think fifo_samples >= BIT(8) would make more sense.

fifo_samples is a count, not bit flags.

I even would prefer to see in such a case

	fifo_samples > (BIT(8) - 1)

that it will define the maximum that fits the HW, or plain number

	fifo_samples > 127
Signature · 2 lines
With Best Regards,
Andy Shevchenko
reviewer
#5 lore raw
Quoted 12 lines · Andy Shevchenko
Quoted 1 line · David Lechner

On 2/27/26 6:43 AM, Antoniu Miclaus wrote:

...

Quoted 6 lines

drivers/iio/accel/adxl380.c static int adxl380_set_fifo_samples(struct adxl380_state *st) · line 880

Quoted 1 line
drivers/iio/accel/adxl380.c +1 -0
+					    !!(fifo_samples & BIT(8))));

Technically, this works, but in terms of understanding the code I think fifo_samples >= BIT(8) would make more sense.

fifo_samples is a count, not bit flags.

Quoted 8 lines

I even would prefer to see in such a case

	fifo_samples > (BIT(8) - 1)

that it will define the maximum that fits the HW, or plain number

	fifo_samples > 127

Now that I looked at the dataheet, I get Jonathan's point.

The reason this is here is that we are filling in a 9-bit value using 8-bit registers, so the MSB goes in a different register.

So actually, it would probably make the most sense to write it as fifo_samples >> 8.

#6 lore raw
Quoted 3 lines · David Lechner
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · David Lechner

On 2/27/26 6:43 AM, Antoniu Miclaus wrote:

...

Quoted 15 lines
Quoted 7 lines
Quoted 6 lines

drivers/iio/accel/adxl380.c static int adxl380_set_fifo_samples(struct adxl380_state *st) · line 880

Quoted 1 line
drivers/iio/accel/adxl380.c +1 -0
+					    !!(fifo_samples & BIT(8))));

Technically, this works, but in terms of understanding the code I think fifo_samples >= BIT(8) would make more sense.

fifo_samples is a count, not bit flags.

Quoted 7 lines

I even would prefer to see in such a case

	fifo_samples > (BIT(8) - 1)

that it will define the maximum that fits the HW, or plain number

	fifo_samples > 127
Quoted 7 lines

Now that I looked at the dataheet, I get Jonathan's point.

The reason this is here is that we are filling in a 9-bit value using 8-bit registers, so the MSB goes in a different register.

So actually, it would probably make the most sense to write it as fifo_samples >> 8.

I agree based on the above justification.

Signature · 2 lines
With Best Regards,
Andy Shevchenko