All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2 v2] iio: magnetometer: ak8974: Break out measurement
@ 2020-04-16 14:09 Linus Walleij
  2020-04-16 14:09 ` [PATCH 2/2 v2] iio: magnetometer: ak8974: Provide scaling Linus Walleij
  2020-04-16 17:25 ` [PATCH 1/2 v2] iio: magnetometer: ak8974: Break out measurement Michał Mirosław
  0 siblings, 2 replies; 7+ messages in thread
From: Linus Walleij @ 2020-04-16 14:09 UTC (permalink / raw)
  To: Jonathan Cameron, linux-iio
  Cc: Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-input, Linus Walleij, Nick Reitemeyer, Stephan Gerhold,
	Michał Mirosław

This breaks out the measurement code to its own function
so we can handle this without swirling it up with the
bis switch() statement inside ak8974_read_raw().

Use an intermediary s16* variable since we read s16 but
the external API required an int* so this way we get
explicit casting.

Cc: Nick Reitemeyer <nick.reitemeyer@web.de>
Cc: Stephan Gerhold <stephan@gerhold.net>
Cc: Michał Mirosław <mirq-linux@rere.qmqm.pl>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
ChangeLog v1->v2:
- Break out as a separate patch.
---
 drivers/iio/magnetometer/ak8974.c | 51 +++++++++++++++++++------------
 1 file changed, 31 insertions(+), 20 deletions(-)

diff --git a/drivers/iio/magnetometer/ak8974.c b/drivers/iio/magnetometer/ak8974.c
index ade4ed8f67d2..5361647b9054 100644
--- a/drivers/iio/magnetometer/ak8974.c
+++ b/drivers/iio/magnetometer/ak8974.c
@@ -554,46 +554,57 @@ static int ak8974_detect(struct ak8974 *ak8974)
 	return 0;
 }
 
+static int ak8974_measure(struct ak8974 *ak8974, unsigned long address, s16 *val)
+{
+	__le16 hw_values[3];
+	int ret;
+
+	pm_runtime_get_sync(&ak8974->i2c->dev);
+	mutex_lock(&ak8974->lock);
+
+	ret = ak8974_trigmeas(ak8974);
+	if (ret)
+		goto out_unlock;
+	ret = ak8974_getresult(ak8974, hw_values);
+	if (ret)
+		goto out_unlock;
+	*val = (s16)le16_to_cpu(hw_values[address]);
+out_unlock:
+	mutex_unlock(&ak8974->lock);
+	pm_runtime_mark_last_busy(&ak8974->i2c->dev);
+	pm_runtime_put_autosuspend(&ak8974->i2c->dev);
+
+	return ret;
+}
+
 static int ak8974_read_raw(struct iio_dev *indio_dev,
 			   struct iio_chan_spec const *chan,
 			   int *val, int *val2,
 			   long mask)
 {
 	struct ak8974 *ak8974 = iio_priv(indio_dev);
-	__le16 hw_values[3];
 	int ret = -EINVAL;
-
-	pm_runtime_get_sync(&ak8974->i2c->dev);
-	mutex_lock(&ak8974->lock);
+	s16 outval;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 		if (chan->address > 2) {
 			dev_err(&ak8974->i2c->dev, "faulty channel address\n");
 			ret = -EIO;
-			goto out_unlock;
+			goto out_err_read;
 		}
-		ret = ak8974_trigmeas(ak8974);
-		if (ret)
-			goto out_unlock;
-		ret = ak8974_getresult(ak8974, hw_values);
-		if (ret)
-			goto out_unlock;
-
 		/*
 		 * We read all axes and discard all but one, for optimized
 		 * reading, use the triggered buffer.
 		 */
-		*val = (s16)le16_to_cpu(hw_values[chan->address]);
-
+		ret = ak8974_measure(ak8974, chan->address, &outval);
+		if (ret)
+			goto out_err_read;
+		*val = outval;
 		ret = IIO_VAL_INT;
+		break;
 	}
-
- out_unlock:
-	mutex_unlock(&ak8974->lock);
-	pm_runtime_mark_last_busy(&ak8974->i2c->dev);
-	pm_runtime_put_autosuspend(&ak8974->i2c->dev);
-
+out_err_read:
 	return ret;
 }
 
-- 
2.21.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/2 v2] iio: magnetometer: ak8974: Provide scaling
  2020-04-16 14:09 [PATCH 1/2 v2] iio: magnetometer: ak8974: Break out measurement Linus Walleij
@ 2020-04-16 14:09 ` Linus Walleij
  2020-04-16 15:32   ` Stephan Gerhold
  2020-04-16 18:17   ` Michał Mirosław
  2020-04-16 17:25 ` [PATCH 1/2 v2] iio: magnetometer: ak8974: Break out measurement Michał Mirosław
  1 sibling, 2 replies; 7+ messages in thread
From: Linus Walleij @ 2020-04-16 14:09 UTC (permalink / raw)
  To: Jonathan Cameron, linux-iio
  Cc: Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler,
	linux-input, Linus Walleij, Nick Reitemeyer, Stephan Gerhold,
	Michał Mirosław

The manual for the HSCDTD008A gives us a scaling for the
three axis as +/- 2.4mT per axis.

When I implement this the biggest axis indicates 0.59 Gauss
which is a reasonable measurement for the earths magnetic
which is in the range of 0.25 to 0.65 Gauss on the surface
according to Wikipedia.

Cc: Nick Reitemeyer <nick.reitemeyer@web.de>
Cc: Stephan Gerhold <stephan@gerhold.net>
Cc: Michał Mirosław <mirq-linux@rere.qmqm.pl>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
ChangeLog v1->v2:
- Split out the measurement refactoring.
---
 drivers/iio/magnetometer/ak8974.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/magnetometer/ak8974.c b/drivers/iio/magnetometer/ak8974.c
index 5361647b9054..effcdd93e650 100644
--- a/drivers/iio/magnetometer/ak8974.c
+++ b/drivers/iio/magnetometer/ak8974.c
@@ -603,6 +603,18 @@ static int ak8974_read_raw(struct iio_dev *indio_dev,
 		*val = outval;
 		ret = IIO_VAL_INT;
 		break;
+	case IIO_CHAN_INFO_SCALE:
+		/*
+		 * The datasheet for HSCDTF008A, page 3 specifies the
+		 * range of the sensor as +/- 2.4 mT per axis, which corresponds
+		 * to +/- 2400 uT = +/- 24 Gauss. So 0x7fff is 24 Gauss and
+		 * 0xffff is -24 Gauss. To account for the one missing value if
+		 * we multiply by 1/S16_MAX, instead multiply with 2/U16_MAX.
+		 */
+		*val = 24 * 2;
+		*val2 = U16_MAX;
+		ret = IIO_VAL_FRACTIONAL;
+		break;
 	}
 out_err_read:
 	return ret;
@@ -667,7 +679,8 @@ static const struct iio_chan_spec_ext_info ak8974_ext_info[] = {
 		.type = IIO_MAGN,					\
 		.modified = 1,						\
 		.channel2 = IIO_MOD_##axis,				\
-		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
+			BIT(IIO_CHAN_INFO_SCALE),			\
 		.ext_info = ak8974_ext_info,				\
 		.address = index,					\
 		.scan_index = index,					\
-- 
2.21.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2 v2] iio: magnetometer: ak8974: Provide scaling
  2020-04-16 14:09 ` [PATCH 2/2 v2] iio: magnetometer: ak8974: Provide scaling Linus Walleij
@ 2020-04-16 15:32   ` Stephan Gerhold
  2020-04-16 17:19     ` Linus Walleij
  2020-04-16 18:17   ` Michał Mirosław
  1 sibling, 1 reply; 7+ messages in thread
From: Stephan Gerhold @ 2020-04-16 15:32 UTC (permalink / raw)
  To: Linus Walleij
  Cc: Jonathan Cameron, linux-iio, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-input, Nick Reitemeyer,
	Michał Mirosław

Hi Linus,

On Thu, Apr 16, 2020 at 04:09:17PM +0200, Linus Walleij wrote:
> The manual for the HSCDTD008A gives us a scaling for the
> three axis as +/- 2.4mT per axis.
> 

I wonder if we can really assume that this applies to
the other models (e.g. AK8974) as well?

> When I implement this the biggest axis indicates 0.59 Gauss
> which is a reasonable measurement for the earths magnetic
> which is in the range of 0.25 to 0.65 Gauss on the surface
> according to Wikipedia.
> 
> Cc: Nick Reitemeyer <nick.reitemeyer@web.de>
> Cc: Stephan Gerhold <stephan@gerhold.net>
> Cc: Michał Mirosław <mirq-linux@rere.qmqm.pl>
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---
> ChangeLog v1->v2:
> - Split out the measurement refactoring.
> ---
>  drivers/iio/magnetometer/ak8974.c | 15 ++++++++++++++-
>  1 file changed, 14 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/magnetometer/ak8974.c b/drivers/iio/magnetometer/ak8974.c
> index 5361647b9054..effcdd93e650 100644
> --- a/drivers/iio/magnetometer/ak8974.c
> +++ b/drivers/iio/magnetometer/ak8974.c
> @@ -603,6 +603,18 @@ static int ak8974_read_raw(struct iio_dev *indio_dev,
>  		*val = outval;
>  		ret = IIO_VAL_INT;
>  		break;
> +	case IIO_CHAN_INFO_SCALE:
> +		/*
> +		 * The datasheet for HSCDTF008A, page 3 specifies the
> +		 * range of the sensor as +/- 2.4 mT per axis, which corresponds
> +		 * to +/- 2400 uT = +/- 24 Gauss. So 0x7fff is 24 Gauss and
> +		 * 0xffff is -24 Gauss. To account for the one missing value if
> +		 * we multiply by 1/S16_MAX, instead multiply with 2/U16_MAX.
> +		 */

I just want to note that (according to the datasheet), HSCDTD008A
produces either 14-bit or 15-bit measurements (depending on
the HSCDTD008A_CTRL4_RANGE bit that we set by default).

I think this isn't exposed correctly in the AK8974_AXIS_CHANNEL() macro
(realbits is 16 instead of 15), so this might need special casing for
hscdt008a?

The reason I mention this is because I think it would also affect the
scaling that you implement here. With 15-bit output it produces values
from +16383 (0x3fff) (= 2.4 mT?) to -16384 (0xc000) (= -2.4 mT?).

So it would never reach the 0x7fff and 0xffff you mention
in your comment.

> +		*val = 24 * 2;
> +		*val2 = U16_MAX;
> +		ret = IIO_VAL_FRACTIONAL;
> +		break;
>  	}
>  out_err_read:
>  	return ret;
> @@ -667,7 +679,8 @@ static const struct iio_chan_spec_ext_info ak8974_ext_info[] = {
>  		.type = IIO_MAGN,					\
>  		.modified = 1,						\
>  		.channel2 = IIO_MOD_##axis,				\
> -		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
> +			BIT(IIO_CHAN_INFO_SCALE),			\
>  		.ext_info = ak8974_ext_info,				\
>  		.address = index,					\
>  		.scan_index = index,					\
> -- 
> 2.21.1
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2 v2] iio: magnetometer: ak8974: Provide scaling
  2020-04-16 15:32   ` Stephan Gerhold
@ 2020-04-16 17:19     ` Linus Walleij
  0 siblings, 0 replies; 7+ messages in thread
From: Linus Walleij @ 2020-04-16 17:19 UTC (permalink / raw)
  To: Stephan Gerhold
  Cc: Jonathan Cameron, linux-iio, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, Linux Input, Nick Reitemeyer,
	Michał Mirosław

On Thu, Apr 16, 2020 at 5:32 PM Stephan Gerhold <stephan@gerhold.net> wrote:
> On Thu, Apr 16, 2020 at 04:09:17PM +0200, Linus Walleij wrote:

> > The manual for the HSCDTD008A gives us a scaling for the
> > three axis as +/- 2.4mT per axis.
>
> I wonder if we can really assume that this applies to
> the other models (e.g. AK8974) as well?

Patches are for testing :D

I have a Ux500 reference board with the AK8974 vanilla
variant mounted so I will check with that one.

> > +     case IIO_CHAN_INFO_SCALE:
> > +             /*
> > +              * The datasheet for HSCDTF008A, page 3 specifies the
> > +              * range of the sensor as +/- 2.4 mT per axis, which corresponds
> > +              * to +/- 2400 uT = +/- 24 Gauss. So 0x7fff is 24 Gauss and
> > +              * 0xffff is -24 Gauss. To account for the one missing value if
> > +              * we multiply by 1/S16_MAX, instead multiply with 2/U16_MAX.
> > +              */
>
> I just want to note that (according to the datasheet), HSCDTD008A
> produces either 14-bit or 15-bit measurements (depending on
> the HSCDTD008A_CTRL4_RANGE bit that we set by default).

Argh OK I will fix this. I try to get an AMI datasheet as well.

> I think this isn't exposed correctly in the AK8974_AXIS_CHANNEL() macro
> (realbits is 16 instead of 15), so this might need special casing for
> hscdt008a?

Yes definately. It's a bug. I'll make a separate patch for this.

> The reason I mention this is because I think it would also affect the
> scaling that you implement here. With 15-bit output it produces values
> from +16383 (0x3fff) (= 2.4 mT?) to -16384 (0xc000) (= -2.4 mT?).
>
> So it would never reach the 0x7fff and 0xffff you mention
> in your comment.

You're right. What I need to do is put the HSCDTD008A and
AK8974 side by side and compare.

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2 v2] iio: magnetometer: ak8974: Break out measurement
  2020-04-16 14:09 [PATCH 1/2 v2] iio: magnetometer: ak8974: Break out measurement Linus Walleij
  2020-04-16 14:09 ` [PATCH 2/2 v2] iio: magnetometer: ak8974: Provide scaling Linus Walleij
@ 2020-04-16 17:25 ` Michał Mirosław
  1 sibling, 0 replies; 7+ messages in thread
From: Michał Mirosław @ 2020-04-16 17:25 UTC (permalink / raw)
  To: Linus Walleij
  Cc: Jonathan Cameron, linux-iio, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-input, Nick Reitemeyer,
	Stephan Gerhold

On Thu, Apr 16, 2020 at 04:09:16PM +0200, Linus Walleij wrote:
> This breaks out the measurement code to its own function
> so we can handle this without swirling it up with the
> bis switch() statement inside ak8974_read_raw().
> 
> Use an intermediary s16* variable since we read s16 but
> the external API required an int* so this way we get
> explicit casting.
> 
> Cc: Nick Reitemeyer <nick.reitemeyer@web.de>
> Cc: Stephan Gerhold <stephan@gerhold.net>
> Cc: Michał Mirosław <mirq-linux@rere.qmqm.pl>
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---
> ChangeLog v1->v2:
> - Break out as a separate patch.
> ---
>  drivers/iio/magnetometer/ak8974.c | 51 +++++++++++++++++++------------
>  1 file changed, 31 insertions(+), 20 deletions(-)
> 
> diff --git a/drivers/iio/magnetometer/ak8974.c b/drivers/iio/magnetometer/ak8974.c
> index ade4ed8f67d2..5361647b9054 100644
> --- a/drivers/iio/magnetometer/ak8974.c
> +++ b/drivers/iio/magnetometer/ak8974.c
> @@ -554,46 +554,57 @@ static int ak8974_detect(struct ak8974 *ak8974)
>  	return 0;
>  }
>  
> +static int ak8974_measure(struct ak8974 *ak8974, unsigned long address, s16 *val)
> +{
> +	__le16 hw_values[3];
> +	int ret;
> +
> +	pm_runtime_get_sync(&ak8974->i2c->dev);
> +	mutex_lock(&ak8974->lock);
> +
> +	ret = ak8974_trigmeas(ak8974);
> +	if (ret)
> +		goto out_unlock;
> +	ret = ak8974_getresult(ak8974, hw_values);
> +	if (ret)
> +		goto out_unlock;
> +	*val = (s16)le16_to_cpu(hw_values[address]);

This will still work if the 'val' argument was 'int *'. And it would make
'outval' in the caller redundant.

> +out_unlock:
> +	mutex_unlock(&ak8974->lock);
> +	pm_runtime_mark_last_busy(&ak8974->i2c->dev);
> +	pm_runtime_put_autosuspend(&ak8974->i2c->dev);
> +
> +	return ret;
> +}
> +
>  static int ak8974_read_raw(struct iio_dev *indio_dev,
>  			   struct iio_chan_spec const *chan,
>  			   int *val, int *val2,
>  			   long mask)
>  {
>  	struct ak8974 *ak8974 = iio_priv(indio_dev);
> -	__le16 hw_values[3];
>  	int ret = -EINVAL;
> -
> -	pm_runtime_get_sync(&ak8974->i2c->dev);
> -	mutex_lock(&ak8974->lock);
> +	s16 outval;
>  
>  	switch (mask) {
>  	case IIO_CHAN_INFO_RAW:
>  		if (chan->address > 2) {
>  			dev_err(&ak8974->i2c->dev, "faulty channel address\n");
>  			ret = -EIO;
> -			goto out_unlock;
> +			goto out_err_read;

You could get rid of the gotos to shorten and clear up the code.

>  		}
> -		ret = ak8974_trigmeas(ak8974);
> -		if (ret)
> -			goto out_unlock;
> -		ret = ak8974_getresult(ak8974, hw_values);
> -		if (ret)
> -			goto out_unlock;
> -
>  		/*
>  		 * We read all axes and discard all but one, for optimized
>  		 * reading, use the triggered buffer.
>  		 */
> -		*val = (s16)le16_to_cpu(hw_values[chan->address]);
> -
> +		ret = ak8974_measure(ak8974, chan->address, &outval);
> +		if (ret)
> +			goto out_err_read;
> +		*val = outval;
>  		ret = IIO_VAL_INT;
> +		break;
>  	}
> -
> - out_unlock:
> -	mutex_unlock(&ak8974->lock);
> -	pm_runtime_mark_last_busy(&ak8974->i2c->dev);
> -	pm_runtime_put_autosuspend(&ak8974->i2c->dev);
> -
> +out_err_read:
>  	return ret;
>  }
[...]

Best Regards,
Michał Mirosław

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2 v2] iio: magnetometer: ak8974: Provide scaling
  2020-04-16 14:09 ` [PATCH 2/2 v2] iio: magnetometer: ak8974: Provide scaling Linus Walleij
  2020-04-16 15:32   ` Stephan Gerhold
@ 2020-04-16 18:17   ` Michał Mirosław
  2020-04-16 18:29     ` Michał Mirosław
  1 sibling, 1 reply; 7+ messages in thread
From: Michał Mirosław @ 2020-04-16 18:17 UTC (permalink / raw)
  To: Linus Walleij
  Cc: Jonathan Cameron, linux-iio, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-input, Nick Reitemeyer,
	Stephan Gerhold

On Thu, Apr 16, 2020 at 04:09:17PM +0200, Linus Walleij wrote:
> The manual for the HSCDTD008A gives us a scaling for the
> three axis as +/- 2.4mT per axis.
> 
> When I implement this the biggest axis indicates 0.59 Gauss
> which is a reasonable measurement for the earths magnetic
> which is in the range of 0.25 to 0.65 Gauss on the surface
> according to Wikipedia.
> 
> Cc: Nick Reitemeyer <nick.reitemeyer@web.de>
> Cc: Stephan Gerhold <stephan@gerhold.net>
> Cc: Michał Mirosław <mirq-linux@rere.qmqm.pl>
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---
> ChangeLog v1->v2:
> - Split out the measurement refactoring.
> ---
>  drivers/iio/magnetometer/ak8974.c | 15 ++++++++++++++-
>  1 file changed, 14 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/magnetometer/ak8974.c b/drivers/iio/magnetometer/ak8974.c
> index 5361647b9054..effcdd93e650 100644
> --- a/drivers/iio/magnetometer/ak8974.c
> +++ b/drivers/iio/magnetometer/ak8974.c
> @@ -603,6 +603,18 @@ static int ak8974_read_raw(struct iio_dev *indio_dev,
>  		*val = outval;
>  		ret = IIO_VAL_INT;
>  		break;
> +	case IIO_CHAN_INFO_SCALE:
> +		/*
> +		 * The datasheet for HSCDTF008A, page 3 specifies the
> +		 * range of the sensor as +/- 2.4 mT per axis, which corresponds
> +		 * to +/- 2400 uT = +/- 24 Gauss. So 0x7fff is 24 Gauss and
> +		 * 0xffff is -24 Gauss. To account for the one missing value if
> +		 * we multiply by 1/S16_MAX, instead multiply with 2/U16_MAX.
> +		 */
> +		*val = 24 * 2;
> +		*val2 = U16_MAX;
> +		ret = IIO_VAL_FRACTIONAL;
> +		break;

Hi,

The comment seems wrong. DS mentions that the measurement values are
S16, but limited in range (-k to +k, with k = 2^13 or 2^14). So the
denominator should be 2^13 or 2^14, and numerator 2.4mT.

Best Regards,
Michał Mirosław

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2 v2] iio: magnetometer: ak8974: Provide scaling
  2020-04-16 18:17   ` Michał Mirosław
@ 2020-04-16 18:29     ` Michał Mirosław
  0 siblings, 0 replies; 7+ messages in thread
From: Michał Mirosław @ 2020-04-16 18:29 UTC (permalink / raw)
  To: Linus Walleij
  Cc: Jonathan Cameron, linux-iio, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-input, Nick Reitemeyer,
	Stephan Gerhold

On Thu, Apr 16, 2020 at 08:17:46PM +0200, Michał Mirosław wrote:
> On Thu, Apr 16, 2020 at 04:09:17PM +0200, Linus Walleij wrote:
> > The manual for the HSCDTD008A gives us a scaling for the
> > three axis as +/- 2.4mT per axis.
> > 
> > When I implement this the biggest axis indicates 0.59 Gauss
> > which is a reasonable measurement for the earths magnetic
> > which is in the range of 0.25 to 0.65 Gauss on the surface
> > according to Wikipedia.
> > 
> > Cc: Nick Reitemeyer <nick.reitemeyer@web.de>
> > Cc: Stephan Gerhold <stephan@gerhold.net>
> > Cc: Michał Mirosław <mirq-linux@rere.qmqm.pl>
> > Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> > ---
> > ChangeLog v1->v2:
> > - Split out the measurement refactoring.
> > ---
> >  drivers/iio/magnetometer/ak8974.c | 15 ++++++++++++++-
> >  1 file changed, 14 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/iio/magnetometer/ak8974.c b/drivers/iio/magnetometer/ak8974.c
> > index 5361647b9054..effcdd93e650 100644
> > --- a/drivers/iio/magnetometer/ak8974.c
> > +++ b/drivers/iio/magnetometer/ak8974.c
> > @@ -603,6 +603,18 @@ static int ak8974_read_raw(struct iio_dev *indio_dev,
> >  		*val = outval;
> >  		ret = IIO_VAL_INT;
> >  		break;
> > +	case IIO_CHAN_INFO_SCALE:
> > +		/*
> > +		 * The datasheet for HSCDTF008A, page 3 specifies the
> > +		 * range of the sensor as +/- 2.4 mT per axis, which corresponds
> > +		 * to +/- 2400 uT = +/- 24 Gauss. So 0x7fff is 24 Gauss and
> > +		 * 0xffff is -24 Gauss. To account for the one missing value if
> > +		 * we multiply by 1/S16_MAX, instead multiply with 2/U16_MAX.
> > +		 */
> > +		*val = 24 * 2;
> > +		*val2 = U16_MAX;
> > +		ret = IIO_VAL_FRACTIONAL;
> > +		break;
> 
> Hi,
> 
> The comment seems wrong. DS mentions that the measurement values are
> S16, but limited in range (-k to +k, with k = 2^13 or 2^14). So the
> denominator should be 2^13 or 2^14, and numerator 2.4mT.

Ok, this is what you actually did. :-) The error is in 0xffff in the
comment: it should be 0x8000 (== -0x8000). After applying the limit
in range it would be 0x3fff to -0x4000 (== 0xC000).

I would actually use 2^14 as denominator because that's a "round" number,
the difference is insignificant, and we don't really know which one is
correct. (BTW, the DS mentions 0.15uT/LSB resolution, so it would not be
exact 2.4mT for the max value.)

Best Regards,
Michał Mirosław

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-04-16 18:29 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-16 14:09 [PATCH 1/2 v2] iio: magnetometer: ak8974: Break out measurement Linus Walleij
2020-04-16 14:09 ` [PATCH 2/2 v2] iio: magnetometer: ak8974: Provide scaling Linus Walleij
2020-04-16 15:32   ` Stephan Gerhold
2020-04-16 17:19     ` Linus Walleij
2020-04-16 18:17   ` Michał Mirosław
2020-04-16 18:29     ` Michał Mirosław
2020-04-16 17:25 ` [PATCH 1/2 v2] iio: magnetometer: ak8974: Break out measurement Michał Mirosław

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.