[PATCH] iio: accel: fxls8962af: Fix temperature calculation

Sean Nyekjaer posted 1 patch 9 months, 1 week ago
drivers/iio/accel/fxls8962af-core.c | 17 ++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)
[PATCH] iio: accel: fxls8962af: Fix temperature calculation
Posted by Sean Nyekjaer 9 months, 1 week ago
According to spec temperature should be returned in milli degrees Celsius.
Add in_temp_scale to calculate from Celsius to milli Celsius.

Fixes: a3e0b51884ee ("iio: accel: add support for FXLS8962AF/FXLS8964AF accelerometers")
Cc: stable@vger.kernel.org
Signed-off-by: Sean Nyekjaer <sean@geanix.com>
---
 drivers/iio/accel/fxls8962af-core.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/accel/fxls8962af-core.c b/drivers/iio/accel/fxls8962af-core.c
index bf1d3923a181798a1c884ee08b62d86ab5aed26f..30f08160eecdd1f9574c86baeeb17cc0ef1e7c60 100644
--- a/drivers/iio/accel/fxls8962af-core.c
+++ b/drivers/iio/accel/fxls8962af-core.c
@@ -133,7 +133,9 @@
 #define FXLS8967AF_DEVICE_ID			0x87
 
 /* Raw temp channel offset */
-#define FXLS8962AF_TEMP_CENTER_VAL		25
+#define FXLS8962AF_TEMP_CENTER_VAL		25000
+/* Raw temp channel scale */
+#define FXLS8962AF_TEMP_SCALE			1000
 
 #define FXLS8962AF_AUTO_SUSPEND_DELAY_MS	2000
 
@@ -439,8 +441,16 @@ static int fxls8962af_read_raw(struct iio_dev *indio_dev,
 		*val = FXLS8962AF_TEMP_CENTER_VAL;
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
-		*val = 0;
-		return fxls8962af_read_full_scale(data, val2);
+		switch (chan->type) {
+		case IIO_TEMP:
+			*val = FXLS8962AF_TEMP_SCALE;
+			return IIO_VAL_INT;
+		case IIO_ACCEL:
+			*val = 0;
+			return fxls8962af_read_full_scale(data, val2);
+		default:
+			return -EINVAL;
+		}
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		return fxls8962af_read_samp_freq(data, val, val2);
 	default:
@@ -736,6 +746,7 @@ static const struct iio_event_spec fxls8962af_event[] = {
 	.type = IIO_TEMP, \
 	.address = FXLS8962AF_TEMP_OUT, \
 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
+			      BIT(IIO_CHAN_INFO_SCALE) | \
 			      BIT(IIO_CHAN_INFO_OFFSET),\
 	.scan_index = -1, \
 	.scan_type = { \

---
base-commit: 609bc31eca06c7408e6860d8b46311ebe45c1fef
change-id: 20250501-fxls-307ef3d6d065

Best regards,
-- 
Sean Nyekjaer <sean@geanix.com>
Re: [PATCH] iio: accel: fxls8962af: Fix temperature calculation
Posted by Marcelo Schmitt 9 months, 1 week ago
Hello Sean,

Overall, this looks good to me.
Only thing I think need to (not) change is the offset (see comment below).

Regards,
Marcelo

On 05/01, Sean Nyekjaer wrote:
> According to spec temperature should be returned in milli degrees Celsius.
> Add in_temp_scale to calculate from Celsius to milli Celsius.
> 
> Fixes: a3e0b51884ee ("iio: accel: add support for FXLS8962AF/FXLS8964AF accelerometers")
> Cc: stable@vger.kernel.org
> Signed-off-by: Sean Nyekjaer <sean@geanix.com>
> ---
>  drivers/iio/accel/fxls8962af-core.c | 17 ++++++++++++++---
...
>  
>  /* Raw temp channel offset */
> -#define FXLS8962AF_TEMP_CENTER_VAL		25
> +#define FXLS8962AF_TEMP_CENTER_VAL		25000
I think the offset was correct the way it was before.
For example, if the sensor at 37°C, the output code will be 0b00001100, 12 (decimal).
Then (_raw + _offset) * _scale = (12 + 25) * 1000 = 37000 milli °C

> +/* Raw temp channel scale */
> +#define FXLS8962AF_TEMP_SCALE			1000
>  
...
> @@ -736,6 +746,7 @@ static const struct iio_event_spec fxls8962af_event[] = {
>  	.type = IIO_TEMP, \
>  	.address = FXLS8962AF_TEMP_OUT, \
>  	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
> +			      BIT(IIO_CHAN_INFO_SCALE) | \
>  			      BIT(IIO_CHAN_INFO_OFFSET),\
>  	.scan_index = -1, \
>  	.scan_type = { \
Datasheet page 39 says 'the TEMP_OUT register contains the 8-bit, 2's complement
temperature value.'
So I think we can make the temperature scan element signed.
		.sign = 's',	\

Though, I guess that would be a thing for a separate patch.
Re: [PATCH] iio: accel: fxls8962af: Fix temperature calculation
Posted by Sean Nyekjaer 9 months, 1 week ago
Hi Marcelo,

On Thu, May 01, 2025 at 04:13:29PM +0100, Marcelo Schmitt wrote:
> Hello Sean,
> 
> Overall, this looks good to me.
> Only thing I think need to (not) change is the offset (see comment below).
> 
> Regards,
> Marcelo
> 
> On 05/01, Sean Nyekjaer wrote:
> > According to spec temperature should be returned in milli degrees Celsius.
> > Add in_temp_scale to calculate from Celsius to milli Celsius.
> >
> > Fixes: a3e0b51884ee ("iio: accel: add support for FXLS8962AF/FXLS8964AF accelerometers")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Sean Nyekjaer <sean@geanix.com>
> > ---
> >  drivers/iio/accel/fxls8962af-core.c | 17 ++++++++++++++---
> ...
> >
> >  /* Raw temp channel offset */
> > -#define FXLS8962AF_TEMP_CENTER_VAL		25
> > +#define FXLS8962AF_TEMP_CENTER_VAL		25000
> I think the offset was correct the way it was before.
> For example, if the sensor at 37°C, the output code will be 0b00001100, 12 (decimal).
> Then (_raw + _offset) * _scale = (12 + 25) * 1000 = 37000 milli °C

Yes you are right. Just checked the documentation.
"If known for a device, offset to be added to <type>[Y]_raw prior to
scaling by <type>[Y]_scale"

> 
> > +/* Raw temp channel scale */
> > +#define FXLS8962AF_TEMP_SCALE			1000
> >
> ...
> > @@ -736,6 +746,7 @@ static const struct iio_event_spec fxls8962af_event[] = {
> >  	.type = IIO_TEMP, \
> >  	.address = FXLS8962AF_TEMP_OUT, \
> >  	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
> > +			      BIT(IIO_CHAN_INFO_SCALE) | \
> >  			      BIT(IIO_CHAN_INFO_OFFSET),\
> >  	.scan_index = -1, \
> >  	.scan_type = { \
> Datasheet page 39 says 'the TEMP_OUT register contains the 8-bit, 2's complement
> temperature value.'
> So I think we can make the temperature scan element signed.
> 		.sign = 's',	\
> 
> Though, I guess that would be a thing for a separate patch.

Good catch will do some testing :)

Thanks
/Sean