diff mbox series

[v2,1/2] staging: iio: ad7780: update voltage on read

Message ID 1f7b2c6ee0e18b1c43a8b27f482b08313c52d4e9.1540506298.git.renatogeh@gmail.com (mailing list archive)
State New, archived
Headers show
Series staging: iio: ad7780: correct driver read | expand

Commit Message

Renato Lui Geh Oct. 25, 2018, 10:30 p.m. UTC
The ad7780 driver previously did not read the correct device output, as
it read an outdated value set at initialization. It now updates its
voltage on read.

Signed-off-by: Renato Lui Geh <renatogeh@gmail.com>
---
 drivers/staging/iio/adc/ad7780.c | 4 ++++
 1 file changed, 4 insertions(+)

Comments

Jonathan Cameron Oct. 28, 2018, 3:56 p.m. UTC | #1
On Thu, 25 Oct 2018 19:30:04 -0300
Renato Lui Geh <renatogeh@gmail.com> wrote:

> The ad7780 driver previously did not read the correct device output, as
> it read an outdated value set at initialization. It now updates its
> voltage on read.
> 
> Signed-off-by: Renato Lui Geh <renatogeh@gmail.com>
> ---
>  drivers/staging/iio/adc/ad7780.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
> index b67412db0318..27972563bb6a 100644
> --- a/drivers/staging/iio/adc/ad7780.c
> +++ b/drivers/staging/iio/adc/ad7780.c
> @@ -87,11 +87,15 @@ static int ad7780_read_raw(struct iio_dev *indio_dev,
>  			   long m)
>  {
>  	struct ad7780_state *st = iio_priv(indio_dev);
> +	int voltage_uv = 0;
No initialization needed as it is always set in the paths where it is used
(before it is used).
>  
>  	switch (m) {
>  	case IIO_CHAN_INFO_RAW:
>  		return ad_sigma_delta_single_conversion(indio_dev, chan, val);
>  	case IIO_CHAN_INFO_SCALE:
> +		voltage_uv = regulator_get_voltage(st->reg);
> +		if (voltage_uv)
> +			st->int_vref_mv = voltage_uv/1000;
>  		*val = st->int_vref_mv * st->gain;
Is there actually a reason (now) to have the stashed value
of int_vref_mv in the state structure?

Also, I think we are always replacing the value that is retrieved
in probe so we can drop reading it there as well.

Thanks,

Jonathan

>  		*val2 = chan->scan_type.realbits - 1;
>  		return IIO_VAL_FRACTIONAL_LOG2;
Renato Lui Geh Oct. 28, 2018, 4:52 p.m. UTC | #2
Hi Jonathan,

Thank you for the review.

>> +		voltage_uv = regulator_get_voltage(st->reg);
>> +		if (voltage_uv)
>> +			st->int_vref_mv = voltage_uv/1000;
>>  		*val = st->int_vref_mv * st->gain;
>Is there actually a reason (now) to have the stashed value
>of int_vref_mv in the state structure?

From probe:

if (voltage_uv)
	st->int_vref_mv = voltage_uv / 1000;
else
	dev_warn(&spi->dev, "Reference voltage unspecified\n");

So the idea was to, when voltage_uv = 0, return the previous voltage.
Should I instead handle this as an error the same way as in probe?

Thanks,
Renato
Jonathan Cameron Oct. 28, 2018, 6:58 p.m. UTC | #3
On Sun, 28 Oct 2018 13:52:32 -0300
Renato Lui Geh <renatogeh@gmail.com> wrote:

> Hi Jonathan,
> 
> Thank you for the review.
> 
> >> +		voltage_uv = regulator_get_voltage(st->reg);
> >> +		if (voltage_uv)
> >> +			st->int_vref_mv = voltage_uv/1000;
> >>  		*val = st->int_vref_mv * st->gain;  
> >Is there actually a reason (now) to have the stashed value
> >of int_vref_mv in the state structure?  
> 
> From probe:
> 
> if (voltage_uv)
> 	st->int_vref_mv = voltage_uv / 1000;
> else
> 	dev_warn(&spi->dev, "Reference voltage unspecified\n");
> 
> So the idea was to, when voltage_uv = 0, return the previous voltage.
> Should I instead handle this as an error the same way as in probe?
> 
I would return it as an error.  I can't really see how we would get
this to occur if the bindings are all correct and appropriate driver
support is there for the regulator to actually let us use it.

If we wanted to handle the case of no regulator having been provided
cleanly then we should it using an optional regulator get, and
not provide the scale attribute at all if we can't know what it will
read.  This is a weird enough corner case though that I just wouldn't 
bother handling it as anything other than an error.

> Thanks,
> Renato
Thanks,

Jonathan
diff mbox series

Patch

diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
index b67412db0318..27972563bb6a 100644
--- a/drivers/staging/iio/adc/ad7780.c
+++ b/drivers/staging/iio/adc/ad7780.c
@@ -87,11 +87,15 @@  static int ad7780_read_raw(struct iio_dev *indio_dev,
 			   long m)
 {
 	struct ad7780_state *st = iio_priv(indio_dev);
+	int voltage_uv = 0;
 
 	switch (m) {
 	case IIO_CHAN_INFO_RAW:
 		return ad_sigma_delta_single_conversion(indio_dev, chan, val);
 	case IIO_CHAN_INFO_SCALE:
+		voltage_uv = regulator_get_voltage(st->reg);
+		if (voltage_uv)
+			st->int_vref_mv = voltage_uv/1000;
 		*val = st->int_vref_mv * st->gain;
 		*val2 = chan->scan_type.realbits - 1;
 		return IIO_VAL_FRACTIONAL_LOG2;