linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iio: light: acpi-als: Add a tuning parameter interface.
@ 2016-09-06 10:25 Enric Balletbo i Serra
  2016-09-06 10:31 ` Lars-Peter Clausen
  0 siblings, 1 reply; 4+ messages in thread
From: Enric Balletbo i Serra @ 2016-09-06 10:25 UTC (permalink / raw)
  To: linux-kernel, linux-iio, Jonathan Cameron; +Cc: Bryan Freed

From: Bryan Freed <bfreed@chromium.org>

Add IIO_CHAN_INFO_CALIBSCALE to the channel to scale up or down
the raw measurements through the IIO framework.

Add IIO_CHAN_INFO_PROCESSED to provide the interface to read the
scaled measurements through the in_illuminance_input file.

Signed-off-by: Bryan Freed <bfreed@chromium.org>
Signed-off-by: Enric Balletbo i Serra <enric.balletbo@collabora.com>
---
 drivers/iio/light/acpi-als.c | 59 +++++++++++++++++++++++++++++++++++++-------
 1 file changed, 50 insertions(+), 9 deletions(-)

diff --git a/drivers/iio/light/acpi-als.c b/drivers/iio/light/acpi-als.c
index f0b47c5..40837bc 100644
--- a/drivers/iio/light/acpi-als.c
+++ b/drivers/iio/light/acpi-als.c
@@ -56,7 +56,8 @@ static const struct iio_chan_spec acpi_als_channels[] = {
 		},
 		/* _RAW is here for backward ABI compatibility */
 		.info_mask_separate	= BIT(IIO_CHAN_INFO_RAW) |
-					  BIT(IIO_CHAN_INFO_PROCESSED),
+					  BIT(IIO_CHAN_INFO_PROCESSED) |
+					  BIT(IIO_CHAN_INFO_CALIBSCALE),
 	},
 };
 
@@ -76,6 +77,9 @@ struct acpi_als {
 	struct mutex		lock;
 
 	s32			evt_buffer[ACPI_ALS_EVT_BUFFER_SIZE];
+
+	uint			als_scale;
+	uint			als_uscale;
 };
 
 /*
@@ -154,25 +158,60 @@ static int acpi_als_read_raw(struct iio_dev *indio_dev,
 	s32 temp_val;
 	int ret;
 
-	if ((mask != IIO_CHAN_INFO_PROCESSED) && (mask != IIO_CHAN_INFO_RAW))
-		return -EINVAL;
-
 	/* we support only illumination (_ALI) so far. */
 	if (chan->type != IIO_LIGHT)
 		return -EINVAL;
 
-	ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &temp_val);
-	if (ret < 0)
-		return ret;
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+	case IIO_CHAN_INFO_PROCESSED:
+		ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &temp_val);
+		if (ret < 0)
+			return ret;
+		if (mask == IIO_CHAN_INFO_PROCESSED) {
+			/* use u64 to avoid overflow */
+			u64 ulux = (u64) temp_val * 1000000;
+
+			mutex_lock(&als->lock);
+			ulux = ulux * als->als_scale +
+			       div_u64(ulux * als->als_uscale, 1000000U);
+			mutex_unlock(&als->lock);
+			*val = div_u64(ulux, 1000000U);
+		} else {
+			*val = temp_val;
+		}
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_CALIBSCALE:
+		mutex_lock(&als->lock);
+		*val = als->als_scale;
+		*val2 = als->als_uscale;
+		mutex_unlock(&als->lock);
+		return IIO_VAL_INT_PLUS_MICRO;
+	default:
+		return -EINVAL;
+	}
+}
 
-	*val = temp_val;
+static int acpi_als_write_raw(struct iio_dev *iio,
+			      struct iio_chan_spec const *chan, int val,
+			      int val2, long mask)
+{
+	struct acpi_als *als = iio_priv(iio);
+
+	if (mask != IIO_CHAN_INFO_CALIBSCALE || chan->type != IIO_LIGHT)
+		return -EINVAL;
 
-	return IIO_VAL_INT;
+	mutex_lock(&als->lock);
+	als->als_scale = val;
+	als->als_uscale = val2;
+	mutex_unlock(&als->lock);
+	return 0;
 }
 
 static const struct iio_info acpi_als_info = {
 	.driver_module		= THIS_MODULE,
 	.read_raw		= acpi_als_read_raw,
+	.write_raw		= acpi_als_write_raw,
 };
 
 static int acpi_als_add(struct acpi_device *device)
@@ -189,6 +228,8 @@ static int acpi_als_add(struct acpi_device *device)
 
 	device->driver_data = indio_dev;
 	als->device = device;
+	als->als_scale = 1;
+	als->als_uscale = 0;
 	mutex_init(&als->lock);
 
 	indio_dev->name = ACPI_ALS_DEVICE_NAME;
-- 
2.1.0

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

* Re: [PATCH] iio: light: acpi-als: Add a tuning parameter interface.
  2016-09-06 10:25 [PATCH] iio: light: acpi-als: Add a tuning parameter interface Enric Balletbo i Serra
@ 2016-09-06 10:31 ` Lars-Peter Clausen
  2016-09-06 16:48   ` Enric Balletbo Serra
  0 siblings, 1 reply; 4+ messages in thread
From: Lars-Peter Clausen @ 2016-09-06 10:31 UTC (permalink / raw)
  To: Enric Balletbo i Serra, linux-kernel, linux-iio, Jonathan Cameron
  Cc: Bryan Freed

On 09/06/2016 12:25 PM, Enric Balletbo i Serra wrote:
> From: Bryan Freed <bfreed@chromium.org>
> 
> Add IIO_CHAN_INFO_CALIBSCALE to the channel to scale up or down
> the raw measurements through the IIO framework.
> 
> Add IIO_CHAN_INFO_PROCESSED to provide the interface to read the
> scaled measurements through the in_illuminance_input file.

What is the use-case for this, how is this interface supposed to be used?

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

* Re: [PATCH] iio: light: acpi-als: Add a tuning parameter interface.
  2016-09-06 10:31 ` Lars-Peter Clausen
@ 2016-09-06 16:48   ` Enric Balletbo Serra
  2016-09-10 15:57     ` Jonathan Cameron
  0 siblings, 1 reply; 4+ messages in thread
From: Enric Balletbo Serra @ 2016-09-06 16:48 UTC (permalink / raw)
  To: Lars-Peter Clausen
  Cc: Enric Balletbo i Serra, linux-kernel, linux-iio, Jonathan Cameron

Hi Lars,

2016-09-06 12:31 GMT+02:00 Lars-Peter Clausen <lars@metafoo.de>:
> On 09/06/2016 12:25 PM, Enric Balletbo i Serra wrote:
>> From: Bryan Freed <bfreed@chromium.org>
>>
>> Add IIO_CHAN_INFO_CALIBSCALE to the channel to scale up or down
>> the raw measurements through the IIO framework.
>>
>> Add IIO_CHAN_INFO_PROCESSED to provide the interface to read the
>> scaled measurements through the in_illuminance_input file.
>
> What is the use-case for this, how is this interface supposed to be used?

The idea behind this is be able to do a per-device calibration. The
use case we're thinking is:

 1. Read raw measurements from sensor in controlled environment (at
30, 500 and 1000 lux)
 2. do some math to calculate the calibration value.
 3. write the value to calibscale (in_illuminance_calibscale)
 4. read the calibrated measurement (in_illuminance_input)

Regards,
  Enric

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

* Re: [PATCH] iio: light: acpi-als: Add a tuning parameter interface.
  2016-09-06 16:48   ` Enric Balletbo Serra
@ 2016-09-10 15:57     ` Jonathan Cameron
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Cameron @ 2016-09-10 15:57 UTC (permalink / raw)
  To: Enric Balletbo Serra, Lars-Peter Clausen
  Cc: Enric Balletbo i Serra, linux-kernel, linux-iio

On 06/09/16 17:48, Enric Balletbo Serra wrote:
> Hi Lars,
> 
> 2016-09-06 12:31 GMT+02:00 Lars-Peter Clausen <lars@metafoo.de>:
>> On 09/06/2016 12:25 PM, Enric Balletbo i Serra wrote:
>>> From: Bryan Freed <bfreed@chromium.org>
>>>
>>> Add IIO_CHAN_INFO_CALIBSCALE to the channel to scale up or down
>>> the raw measurements through the IIO framework.
>>>
>>> Add IIO_CHAN_INFO_PROCESSED to provide the interface to read the
>>> scaled measurements through the in_illuminance_input file.
>>
>> What is the use-case for this, how is this interface supposed to be used?
> 
> The idea behind this is be able to do a per-device calibration. The
> use case we're thinking is:
> 
>  1. Read raw measurements from sensor in controlled environment (at
> 30, 500 and 1000 lux)
>  2. do some math to calculate the calibration value.
>  3. write the value to calibscale (in_illuminance_calibscale)
>  4. read the calibrated measurement (in_illuminance_input)
> 
I can see where you are coming from but its a rather nasty fudge.
Given we have non trivial maths involved I'd be inclined to say this
should really be left for userspace HAL layers.

If we were getting it from ACPI tables (as the manufacturer had
done the measurements for the device) then we might expose it this
way but given userspace has to be involved to apply the tuning anyway
I'm not sure having a means to 'fudge' the kernel returned value
really makes sense.

Jonathan
> Regards,
>   Enric
> --
> 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] 4+ messages in thread

end of thread, other threads:[~2016-09-10 15:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-06 10:25 [PATCH] iio: light: acpi-als: Add a tuning parameter interface Enric Balletbo i Serra
2016-09-06 10:31 ` Lars-Peter Clausen
2016-09-06 16:48   ` Enric Balletbo Serra
2016-09-10 15:57     ` Jonathan Cameron

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).