* [PATCH 3/3] lp8788-charger: fix wrong ADC conversion
@ 2012-10-18 6:11 Kim, Milo
2012-10-18 11:20 ` Lars-Peter Clausen
0 siblings, 1 reply; 4+ messages in thread
From: Kim, Milo @ 2012-10-18 6:11 UTC (permalink / raw)
To: cbou@mail.ru
Cc: Anton Vorontsov, Lars-Peter Clausen, Jonathan Cameron,
linux-kernel@vger.kernel.org
To get the battery voltage and temperature, IIO ADC functions are used.
LP8788 ADC driver provides RAW and SCALE channel information.
Both RAW with SCALE values are used for the ADC calculation.
The scale is micro unit, additional conversions are required in each case.
This patch fixes wrong ADC result.
Signed-off-by: Milo(Woogyom) Kim <milo.kim@ti.com>
---
drivers/power/lp8788-charger.c | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/drivers/power/lp8788-charger.c b/drivers/power/lp8788-charger.c
index 02fc9ab..3c739e6 100644
--- a/drivers/power/lp8788-charger.c
+++ b/drivers/power/lp8788-charger.c
@@ -239,19 +239,26 @@ static int lp8788_get_vbatt_adc(struct lp8788_charger *pchg,
unsigned int *result)
{
struct iio_channel *channel = pchg->chan[LP8788_VBATT];
+ int raw;
int scaleint;
int scalepart;
int ret;
+ unsigned long tmp;
if (!channel)
return -EINVAL;
+ ret = iio_read_channel_raw(channel, &raw);
+ if (ret != IIO_VAL_INT)
+ return -EINVAL;
+
ret = iio_read_channel_scale(channel, &scaleint, &scalepart);
if (ret != IIO_VAL_INT_PLUS_MICRO)
return -EINVAL;
/* unit: mV */
- *result = (scaleint + scalepart * 1000000) / 1000;
+ tmp = raw * scaleint + div_u64(raw * scalepart, 1000000L);
+ *result = (unsigned int)tmp;
return 0;
}
@@ -304,19 +311,26 @@ static int lp8788_get_battery_temperature(struct lp8788_charger *pchg,
union power_supply_propval *val)
{
struct iio_channel *channel = pchg->chan[LP8788_BATT_TEMP];
+ int raw;
int scaleint;
int scalepart;
int ret;
+ unsigned long tmp;
if (!channel)
return -EINVAL;
+ ret = iio_read_channel_raw(channel, &raw);
+ if (ret != IIO_VAL_INT)
+ return -EINVAL;
+
ret = iio_read_channel_scale(channel, &scaleint, &scalepart);
if (ret != IIO_VAL_INT_PLUS_MICRO)
return -EINVAL;
/* unit: 0.1 'C */
- val->intval = (scaleint + scalepart * 1000000) / 100;
+ tmp = raw * scaleint + div_u64(raw * scalepart, 1000000L);
+ val->intval = (unsigned int)tmp * 10;
return 0;
}
--
1.7.9.5
Best Regards,
Milo
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH 3/3] lp8788-charger: fix wrong ADC conversion
2012-10-18 6:11 [PATCH 3/3] lp8788-charger: fix wrong ADC conversion Kim, Milo
@ 2012-10-18 11:20 ` Lars-Peter Clausen
2012-10-18 23:03 ` Kim, Milo
2012-10-18 23:39 ` Kim, Milo
0 siblings, 2 replies; 4+ messages in thread
From: Lars-Peter Clausen @ 2012-10-18 11:20 UTC (permalink / raw)
To: Kim, Milo
Cc: cbou@mail.ru, Anton Vorontsov, Jonathan Cameron,
linux-kernel@vger.kernel.org
Hi,
On 10/18/2012 08:11 AM, Kim, Milo wrote:
> To get the battery voltage and temperature, IIO ADC functions are used.
> LP8788 ADC driver provides RAW and SCALE channel information.
> Both RAW with SCALE values are used for the ADC calculation.
> The scale is micro unit, additional conversions are required in each case.
> This patch fixes wrong ADC result.
>
> Signed-off-by: Milo(Woogyom) Kim <milo.kim@ti.com>
> ---
> drivers/power/lp8788-charger.c | 18 ++++++++++++++++--
> 1 file changed, 16 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/power/lp8788-charger.c b/drivers/power/lp8788-charger.c
> index 02fc9ab..3c739e6 100644
> --- a/drivers/power/lp8788-charger.c
> +++ b/drivers/power/lp8788-charger.c
> @@ -239,19 +239,26 @@ static int lp8788_get_vbatt_adc(struct lp8788_charger *pchg,
> unsigned int *result)
> {
> struct iio_channel *channel = pchg->chan[LP8788_VBATT];
> + int raw;
> int scaleint;
> int scalepart;
> int ret;
> + unsigned long tmp;
>
> if (!channel)
> return -EINVAL;
>
> + ret = iio_read_channel_raw(channel, &raw);
> + if (ret != IIO_VAL_INT)
> + return -EINVAL;
> +
> ret = iio_read_channel_scale(channel, &scaleint, &scalepart);
> if (ret != IIO_VAL_INT_PLUS_MICRO)
> return -EINVAL;
>
> /* unit: mV */
> - *result = (scaleint + scalepart * 1000000) / 1000;
> + tmp = raw * scaleint + div_u64(raw * scalepart, 1000000L);
> + *result = (unsigned int)tmp;
We now have a function in the IIO core which does the sampling + conversion
in one step: iio_read_channel_processed. This basically allows you to reduce
this function to
return iio_read_channel_processed(channel, result);
I'd also make the result parameter of lp8788_get_vbatt_adc signed, since
both power_supply_propval.intval and iio_read_channel_processed use a signed
int.
>
> return 0;
> }
> @@ -304,19 +311,26 @@ static int lp8788_get_battery_temperature(struct lp8788_charger *pchg,
> union power_supply_propval *val)
> {
> struct iio_channel *channel = pchg->chan[LP8788_BATT_TEMP];
> + int raw;
> int scaleint;
> int scalepart;
> int ret;
> + unsigned long tmp;
>
> if (!channel)
> return -EINVAL;
>
> + ret = iio_read_channel_raw(channel, &raw);
> + if (ret != IIO_VAL_INT)
> + return -EINVAL;
> +
> ret = iio_read_channel_scale(channel, &scaleint, &scalepart);
> if (ret != IIO_VAL_INT_PLUS_MICRO)
> return -EINVAL;
>
> /* unit: 0.1 'C */
> - val->intval = (scaleint + scalepart * 1000000) / 100;
> + tmp = raw * scaleint + div_u64(raw * scalepart, 1000000L);
> + val->intval = (unsigned int)tmp * 10;
>
Same here
> return 0;
> }
^ permalink raw reply [flat|nested] 4+ messages in thread* RE: [PATCH 3/3] lp8788-charger: fix wrong ADC conversion
2012-10-18 11:20 ` Lars-Peter Clausen
@ 2012-10-18 23:03 ` Kim, Milo
2012-10-18 23:39 ` Kim, Milo
1 sibling, 0 replies; 4+ messages in thread
From: Kim, Milo @ 2012-10-18 23:03 UTC (permalink / raw)
To: Lars-Peter Clausen
Cc: cbou@mail.ru, Anton Vorontsov, Jonathan Cameron,
linux-kernel@vger.kernel.org
> -----Original Message-----
> From: Lars-Peter Clausen [mailto:lars@metafoo.de]
> Sent: Thursday, October 18, 2012 8:21 PM
> To: Kim, Milo
> Cc: cbou@mail.ru; Anton Vorontsov; Jonathan Cameron; linux-
> kernel@vger.kernel.org
> Subject: Re: [PATCH 3/3] lp8788-charger: fix wrong ADC conversion
> >
> > + ret = iio_read_channel_raw(channel, &raw);
> > + if (ret != IIO_VAL_INT)
> > + return -EINVAL;
> > +
> > ret = iio_read_channel_scale(channel, &scaleint, &scalepart);
> > if (ret != IIO_VAL_INT_PLUS_MICRO)
> > return -EINVAL;
> >
> > /* unit: mV */
> > - *result = (scaleint + scalepart * 1000000) / 1000;
> > + tmp = raw * scaleint + div_u64(raw * scalepart, 1000000L);
> > + *result = (unsigned int)tmp;
>
>
> We now have a function in the IIO core which does the sampling +
> conversion
> in one step: iio_read_channel_processed. This basically allows you to
> reduce
> this function to
>
> return iio_read_channel_processed(channel, result);
I was hesitating it before sending this patch. :)
Currently, LP8788 ADC driver has no iio channel info for PROCESSED.
Thus, I'll add it in the LP8788 ADC driver first.
Then I'll send the patch for lp8788-charger again.
Thanks for your comment!
Best Regards,
Milo
^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: [PATCH 3/3] lp8788-charger: fix wrong ADC conversion
2012-10-18 11:20 ` Lars-Peter Clausen
2012-10-18 23:03 ` Kim, Milo
@ 2012-10-18 23:39 ` Kim, Milo
1 sibling, 0 replies; 4+ messages in thread
From: Kim, Milo @ 2012-10-18 23:39 UTC (permalink / raw)
To: Lars-Peter Clausen
Cc: cbou@mail.ru, Anton Vorontsov, Jonathan Cameron,
linux-kernel@vger.kernel.org
> > We now have a function in the IIO core which does the sampling +
> > conversion
> > in one step: iio_read_channel_processed. This basically allows you to
> > reduce
> > this function to
> >
> > return iio_read_channel_processed(channel, result);
>
> I was hesitating it before sending this patch. :)
> Currently, LP8788 ADC driver has no iio channel info for PROCESSED.
> Thus, I'll add it in the LP8788 ADC driver first.
> Then I'll send the patch for lp8788-charger again.
I just found you have already handled in case that IIO driver has no IIO_CHAN_INFO_PROCESSED.
(drivers/iio/inkern.c)
So I don't need to worry about the LP8788 ADC driver.
Only the IIO consumer driver(lp8788-charger) needs to be fixed.
Thank you,
Milo
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2012-10-18 23:39 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-10-18 6:11 [PATCH 3/3] lp8788-charger: fix wrong ADC conversion Kim, Milo
2012-10-18 11:20 ` Lars-Peter Clausen
2012-10-18 23:03 ` Kim, Milo
2012-10-18 23:39 ` Kim, Milo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox