linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Bryan Freed <bfreed@chromium.org>
Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org,
	grundler@chromium.org, ldewangan@nvidia.com, lars@metafoo.de,
	gregkh@linuxfoundation.org, jic23@cam.ac.uk
Subject: Re: [PATCH] iio: isl29018: Support fractional ALS scaling.
Date: Sat, 08 Sep 2012 10:29:03 +0100	[thread overview]
Message-ID: <504B0FDF.70004@kernel.org> (raw)
In-Reply-To: <1346874953-22353-1-git-send-email-bfreed@chromium.org>

On 09/05/2012 08:55 PM, Bryan Freed wrote:
> The Industrial IO framework supports scaling ADC values by fractions,
> but most drivers default to using whole numbers.
> This change turns on fractional scaling in the isl29018 driver.
> 
> Signed-off-by: Bryan Freed <bfreed@chromium.org>

add to togreg branch of
git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git

Thanks

Technically doing integer + micro values is actually pretty common
but having calibration scale is less so. Doesn't seem any harm in doing
it here, though as calibscale is of an arbitary scaling itself, if it
wasn't an abi change (which this is not) then I'd suggest just doing it
as a 1000 * the original value.

Still this does the job nicely so thanks!

Jonathan
> ---
>  drivers/staging/iio/light/isl29018.c |   17 +++++++++++++++--
>  1 files changed, 15 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/staging/iio/light/isl29018.c b/drivers/staging/iio/light/isl29018.c
> index 31d22f5..6ee5567 100644
> --- a/drivers/staging/iio/light/isl29018.c
> +++ b/drivers/staging/iio/light/isl29018.c
> @@ -63,6 +63,7 @@ struct isl29018_chip {
>  	struct regmap		*regmap;
>  	struct mutex		lock;
>  	unsigned int		lux_scale;
> +	unsigned int		lux_uscale;
>  	unsigned int		range;
>  	unsigned int		adc_bit;
>  	int			prox_scheme;
> @@ -145,13 +146,22 @@ static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
>  static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
>  {
>  	int lux_data;
> +	unsigned int data_x_range, lux_unshifted;
>  
>  	lux_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_ALS_ONCE);
>  
>  	if (lux_data < 0)
>  		return lux_data;
>  
> -	*lux = (lux_data * chip->range * chip->lux_scale) >> chip->adc_bit;
> +	/* To support fractional scaling, separate the unshifted lux
> +	 * into two calculations: int scaling and micro-scaling.
> +	 * lux_uscale ranges from 0-999999, so about 20 bits.  Split
> +	 * the /1,000,000 in two to reduce the risk of over/underflow.
> +	 */
> +	data_x_range = lux_data * chip->range;
> +	lux_unshifted = data_x_range * chip->lux_scale;
> +	lux_unshifted += data_x_range / 1000 * chip->lux_uscale / 1000;
> +	*lux = lux_unshifted >> chip->adc_bit;
>  
>  	return 0;
>  }
> @@ -339,6 +349,8 @@ static int isl29018_write_raw(struct iio_dev *indio_dev,
>  	mutex_lock(&chip->lock);
>  	if (mask == IIO_CHAN_INFO_CALIBSCALE && chan->type == IIO_LIGHT) {
>  		chip->lux_scale = val;
> +		/* With no write_raw_get_fmt(), val2 is a MICRO fraction. */
> +		chip->lux_uscale = val2;
>  		ret = 0;
>  	}
>  	mutex_unlock(&chip->lock);
> @@ -379,7 +391,8 @@ static int isl29018_read_raw(struct iio_dev *indio_dev,
>  	case IIO_CHAN_INFO_CALIBSCALE:
>  		if (chan->type == IIO_LIGHT) {
>  			*val = chip->lux_scale;
> -			ret = IIO_VAL_INT;
> +			*val2 = chip->lux_uscale;
> +			ret = IIO_VAL_INT_PLUS_MICRO;
>  		}
>  		break;
>  	default:
> 

      reply	other threads:[~2012-09-08  9:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-05 19:55 [PATCH] iio: isl29018: Support fractional ALS scaling Bryan Freed
2012-09-08  9:29 ` Jonathan Cameron [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=504B0FDF.70004@kernel.org \
    --to=jic23@kernel.org \
    --cc=bfreed@chromium.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=grundler@chromium.org \
    --cc=jic23@cam.ac.uk \
    --cc=lars@metafoo.de \
    --cc=ldewangan@nvidia.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).