public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] iio: adc: spmi-vadc: Fix overflow in output value normalization
@ 2015-04-17 14:51 Ivan T. Ivanov
  2015-04-18 16:41 ` Jonathan Cameron
  0 siblings, 1 reply; 2+ messages in thread
From: Ivan T. Ivanov @ 2015-04-17 14:51 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Stanimir Varbanov, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald, linux-iio, linux-kernel, linux-arm-msm

With 'dx' equal to 0.625V and 15 bit ADC, calculations overflow
when difference against GND is ~20% of the ADC range. Fix this.

Signed-off-by: Ivan T. Ivanov <ivan.ivanov@linaro.org>
---
 drivers/iio/adc/qcom-spmi-vadc.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/adc/qcom-spmi-vadc.c b/drivers/iio/adc/qcom-spmi-vadc.c
index 3211729..0c4618b 100644
--- a/drivers/iio/adc/qcom-spmi-vadc.c
+++ b/drivers/iio/adc/qcom-spmi-vadc.c
@@ -18,6 +18,7 @@
 #include <linux/iio/iio.h>
 #include <linux/interrupt.h>
 #include <linux/kernel.h>
+#include <linux/math64.h>
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/platform_device.h>
@@ -471,11 +472,11 @@ static s32 vadc_calibrate(struct vadc_priv *vadc,
 			  const struct vadc_channel_prop *prop, u16 adc_code)
 {
 	const struct vadc_prescale_ratio *prescale;
-	s32 voltage;
+	s64 voltage;

 	voltage = adc_code - vadc->graph[prop->calibration].gnd;
 	voltage *= vadc->graph[prop->calibration].dx;
-	voltage = voltage / vadc->graph[prop->calibration].dy;
+	voltage = div64_s64(voltage, vadc->graph[prop->calibration].dy);

 	if (prop->calibration == VADC_CALIB_ABSOLUTE)
 		voltage += vadc->graph[prop->calibration].dx;
@@ -487,7 +488,7 @@ static s32 vadc_calibrate(struct vadc_priv *vadc,

 	voltage = voltage * prescale->den;

-	return voltage / prescale->num;
+	return div64_s64(voltage, prescale->num);
 }

 static int vadc_decimation_from_dt(u32 value)
--
1.9.1


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

* Re: [PATCH] iio: adc: spmi-vadc: Fix overflow in output value normalization
  2015-04-17 14:51 [PATCH] iio: adc: spmi-vadc: Fix overflow in output value normalization Ivan T. Ivanov
@ 2015-04-18 16:41 ` Jonathan Cameron
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2015-04-18 16:41 UTC (permalink / raw)
  To: Ivan T. Ivanov
  Cc: Stanimir Varbanov, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald, linux-iio, linux-kernel, linux-arm-msm

On 17/04/15 15:51, Ivan T. Ivanov wrote:
> With 'dx' equal to 0.625V and 15 bit ADC, calculations overflow
> when difference against GND is ~20% of the ADC range. Fix this.
> 
> Signed-off-by: Ivan T. Ivanov <ivan.ivanov@linaro.org>
Applied to the fixes-togreg branch of iio.git and marked for stable.

Thanks,

Jonathan
>
> ---
>  drivers/iio/adc/qcom-spmi-vadc.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iio/adc/qcom-spmi-vadc.c b/drivers/iio/adc/qcom-spmi-vadc.c
> index 3211729..0c4618b 100644
> --- a/drivers/iio/adc/qcom-spmi-vadc.c
> +++ b/drivers/iio/adc/qcom-spmi-vadc.c
> @@ -18,6 +18,7 @@
>  #include <linux/iio/iio.h>
>  #include <linux/interrupt.h>
>  #include <linux/kernel.h>
> +#include <linux/math64.h>
>  #include <linux/module.h>
>  #include <linux/of.h>
>  #include <linux/platform_device.h>
> @@ -471,11 +472,11 @@ static s32 vadc_calibrate(struct vadc_priv *vadc,
>  			  const struct vadc_channel_prop *prop, u16 adc_code)
>  {
>  	const struct vadc_prescale_ratio *prescale;
> -	s32 voltage;
> +	s64 voltage;
> 
>  	voltage = adc_code - vadc->graph[prop->calibration].gnd;
>  	voltage *= vadc->graph[prop->calibration].dx;
> -	voltage = voltage / vadc->graph[prop->calibration].dy;
> +	voltage = div64_s64(voltage, vadc->graph[prop->calibration].dy);
> 
>  	if (prop->calibration == VADC_CALIB_ABSOLUTE)
>  		voltage += vadc->graph[prop->calibration].dx;
> @@ -487,7 +488,7 @@ static s32 vadc_calibrate(struct vadc_priv *vadc,
> 
>  	voltage = voltage * prescale->den;
> 
> -	return voltage / prescale->num;
> +	return div64_s64(voltage, prescale->num);
>  }
> 
>  static int vadc_decimation_from_dt(u32 value)
> --
> 1.9.1
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-iio" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 


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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-04-17 14:51 [PATCH] iio: adc: spmi-vadc: Fix overflow in output value normalization Ivan T. Ivanov
2015-04-18 16:41 ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox