All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 1/1] iio: ltr501: Add light channel support
@ 2015-05-16  1:23 ` Kuppuswamy Sathyanarayanan
  0 siblings, 0 replies; 4+ messages in thread
From: Kuppuswamy Sathyanarayanan @ 2015-05-16  1:23 UTC (permalink / raw)
  To: jic23, pmeerw
  Cc: srinivas.pandruvada, sathyanarayanan.kuppuswamy, linux-kernel, linux-iio

Added support to calculate lux value from visible
and IR spectrum adc count values. Also added IIO_LIGHT
channel to enable user read the lux value directly
from device using illuminance input ABI.

Signed-off-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
---
 drivers/iio/light/ltr501.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

v2: Changed scan index of light channel to -1
v3: Removed scan type info from light channel

diff --git a/drivers/iio/light/ltr501.c b/drivers/iio/light/ltr501.c
index ca4bf47..d7245c6 100644
--- a/drivers/iio/light/ltr501.c
+++ b/drivers/iio/light/ltr501.c
@@ -66,6 +66,9 @@
 
 #define LTR501_REGMAP_NAME "ltr501_regmap"
 
+#define LTR501_LUX_CONV(vis_coeff, vis_data, ir_coeff, ir_data) \
+			((vis_coeff * vis_data) - (ir_coeff * ir_data))
+
 static const int int_time_mapping[] = {100000, 50000, 200000, 400000};
 
 static const struct reg_field reg_field_it =
@@ -298,6 +301,29 @@ static int ltr501_ps_read_samp_period(struct ltr501_data *data, int *val)
 	return IIO_VAL_INT;
 }
 
+/* IR and visible spectrum coeff's are given in data sheet */
+static unsigned long ltr501_calculate_lux(u16 vis_data, u16 ir_data)
+{
+	unsigned long ratio, lux;
+
+	if (vis_data == 0)
+		return 0;
+
+	/* multiply numerator by 100 to avoid handling ratio < 1 */
+	ratio = DIV_ROUND_UP(ir_data * 100, ir_data + vis_data);
+
+	if (ratio < 45)
+		lux = LTR501_LUX_CONV(1774, vis_data, -1105, ir_data);
+	else if (ratio >= 45 && ratio < 64)
+		lux = LTR501_LUX_CONV(3772, vis_data, 1336, ir_data);
+	else if (ratio >= 64 && ratio < 85)
+		lux = LTR501_LUX_CONV(1690, vis_data, 169, ir_data);
+	else
+		lux = 0;
+
+	return lux / 1000;
+}
+
 static int ltr501_drdy(struct ltr501_data *data, u8 drdy_mask)
 {
 	int tries = 100;
@@ -548,7 +574,14 @@ static const struct iio_event_spec ltr501_pxs_event_spec[] = {
 	.num_event_specs = _evsize,\
 }
 
+#define LTR501_LIGHT_CHANNEL() { \
+	.type = IIO_LIGHT, \
+	.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
+	.scan_index = -1, \
+}
+
 static const struct iio_chan_spec ltr501_channels[] = {
+	LTR501_LIGHT_CHANNEL(),
 	LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0,
 				 ltr501_als_event_spec,
 				 ARRAY_SIZE(ltr501_als_event_spec)),
@@ -576,6 +609,7 @@ static const struct iio_chan_spec ltr501_channels[] = {
 };
 
 static const struct iio_chan_spec ltr301_channels[] = {
+	LTR501_LIGHT_CHANNEL(),
 	LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0,
 				 ltr501_als_event_spec,
 				 ARRAY_SIZE(ltr501_als_event_spec)),
@@ -596,6 +630,23 @@ static int ltr501_read_raw(struct iio_dev *indio_dev,
 	int ret, i;
 
 	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		if (iio_buffer_enabled(indio_dev))
+			return -EBUSY;
+
+		switch (chan->type) {
+		case IIO_LIGHT:
+			mutex_lock(&data->lock_als);
+			ret = ltr501_read_als(data, buf);
+			mutex_unlock(&data->lock_als);
+			if (ret < 0)
+				return ret;
+			*val = ltr501_calculate_lux(le16_to_cpu(buf[1]),
+						    le16_to_cpu(buf[0]));
+			return IIO_VAL_INT;
+		default:
+			return -EINVAL;
+		}
 	case IIO_CHAN_INFO_RAW:
 		if (iio_buffer_enabled(indio_dev))
 			return -EBUSY;
-- 
1.9.1


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

* [PATCH v3 1/1] iio: ltr501: Add light channel support
@ 2015-05-16  1:23 ` Kuppuswamy Sathyanarayanan
  0 siblings, 0 replies; 4+ messages in thread
From: Kuppuswamy Sathyanarayanan @ 2015-05-16  1:23 UTC (permalink / raw)
  To: jic23, pmeerw
  Cc: srinivas.pandruvada, sathyanarayanan.kuppuswamy, linux-kernel, linux-iio

Added support to calculate lux value from visible
and IR spectrum adc count values. Also added IIO_LIGHT
channel to enable user read the lux value directly
from device using illuminance input ABI.

Signed-off-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
---
 drivers/iio/light/ltr501.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

v2: Changed scan index of light channel to -1
v3: Removed scan type info from light channel

diff --git a/drivers/iio/light/ltr501.c b/drivers/iio/light/ltr501.c
index ca4bf47..d7245c6 100644
--- a/drivers/iio/light/ltr501.c
+++ b/drivers/iio/light/ltr501.c
@@ -66,6 +66,9 @@
 
 #define LTR501_REGMAP_NAME "ltr501_regmap"
 
+#define LTR501_LUX_CONV(vis_coeff, vis_data, ir_coeff, ir_data) \
+			((vis_coeff * vis_data) - (ir_coeff * ir_data))
+
 static const int int_time_mapping[] = {100000, 50000, 200000, 400000};
 
 static const struct reg_field reg_field_it =
@@ -298,6 +301,29 @@ static int ltr501_ps_read_samp_period(struct ltr501_data *data, int *val)
 	return IIO_VAL_INT;
 }
 
+/* IR and visible spectrum coeff's are given in data sheet */
+static unsigned long ltr501_calculate_lux(u16 vis_data, u16 ir_data)
+{
+	unsigned long ratio, lux;
+
+	if (vis_data == 0)
+		return 0;
+
+	/* multiply numerator by 100 to avoid handling ratio < 1 */
+	ratio = DIV_ROUND_UP(ir_data * 100, ir_data + vis_data);
+
+	if (ratio < 45)
+		lux = LTR501_LUX_CONV(1774, vis_data, -1105, ir_data);
+	else if (ratio >= 45 && ratio < 64)
+		lux = LTR501_LUX_CONV(3772, vis_data, 1336, ir_data);
+	else if (ratio >= 64 && ratio < 85)
+		lux = LTR501_LUX_CONV(1690, vis_data, 169, ir_data);
+	else
+		lux = 0;
+
+	return lux / 1000;
+}
+
 static int ltr501_drdy(struct ltr501_data *data, u8 drdy_mask)
 {
 	int tries = 100;
@@ -548,7 +574,14 @@ static const struct iio_event_spec ltr501_pxs_event_spec[] = {
 	.num_event_specs = _evsize,\
 }
 
+#define LTR501_LIGHT_CHANNEL() { \
+	.type = IIO_LIGHT, \
+	.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
+	.scan_index = -1, \
+}
+
 static const struct iio_chan_spec ltr501_channels[] = {
+	LTR501_LIGHT_CHANNEL(),
 	LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0,
 				 ltr501_als_event_spec,
 				 ARRAY_SIZE(ltr501_als_event_spec)),
@@ -576,6 +609,7 @@ static const struct iio_chan_spec ltr501_channels[] = {
 };
 
 static const struct iio_chan_spec ltr301_channels[] = {
+	LTR501_LIGHT_CHANNEL(),
 	LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0,
 				 ltr501_als_event_spec,
 				 ARRAY_SIZE(ltr501_als_event_spec)),
@@ -596,6 +630,23 @@ static int ltr501_read_raw(struct iio_dev *indio_dev,
 	int ret, i;
 
 	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		if (iio_buffer_enabled(indio_dev))
+			return -EBUSY;
+
+		switch (chan->type) {
+		case IIO_LIGHT:
+			mutex_lock(&data->lock_als);
+			ret = ltr501_read_als(data, buf);
+			mutex_unlock(&data->lock_als);
+			if (ret < 0)
+				return ret;
+			*val = ltr501_calculate_lux(le16_to_cpu(buf[1]),
+						    le16_to_cpu(buf[0]));
+			return IIO_VAL_INT;
+		default:
+			return -EINVAL;
+		}
 	case IIO_CHAN_INFO_RAW:
 		if (iio_buffer_enabled(indio_dev))
 			return -EBUSY;
-- 
1.9.1


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

* Re: [PATCH v3 1/1] iio: ltr501: Add light channel support
  2015-05-16  1:23 ` Kuppuswamy Sathyanarayanan
@ 2015-05-17  8:14   ` Jonathan Cameron
  -1 siblings, 0 replies; 4+ messages in thread
From: Jonathan Cameron @ 2015-05-17  8:14 UTC (permalink / raw)
  To: Kuppuswamy Sathyanarayanan, pmeerw
  Cc: srinivas.pandruvada, linux-kernel, linux-iio

On 16/05/15 02:23, Kuppuswamy Sathyanarayanan wrote:
> Added support to calculate lux value from visible
> and IR spectrum adc count values. Also added IIO_LIGHT
> channel to enable user read the lux value directly
> from device using illuminance input ABI.
> 
> Signed-off-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
Applied to the togreg branch of iio.git - initially pushed out as testing for
the autobuilders to play with it.

Jonathan
> ---
>  drivers/iio/light/ltr501.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 51 insertions(+)
> 
> v2: Changed scan index of light channel to -1
> v3: Removed scan type info from light channel
> 
> diff --git a/drivers/iio/light/ltr501.c b/drivers/iio/light/ltr501.c
> index ca4bf47..d7245c6 100644
> --- a/drivers/iio/light/ltr501.c
> +++ b/drivers/iio/light/ltr501.c
> @@ -66,6 +66,9 @@
>  
>  #define LTR501_REGMAP_NAME "ltr501_regmap"
>  
> +#define LTR501_LUX_CONV(vis_coeff, vis_data, ir_coeff, ir_data) \
> +			((vis_coeff * vis_data) - (ir_coeff * ir_data))
> +
>  static const int int_time_mapping[] = {100000, 50000, 200000, 400000};
>  
>  static const struct reg_field reg_field_it =
> @@ -298,6 +301,29 @@ static int ltr501_ps_read_samp_period(struct ltr501_data *data, int *val)
>  	return IIO_VAL_INT;
>  }
>  
> +/* IR and visible spectrum coeff's are given in data sheet */
> +static unsigned long ltr501_calculate_lux(u16 vis_data, u16 ir_data)
> +{
> +	unsigned long ratio, lux;
> +
> +	if (vis_data == 0)
> +		return 0;
> +
> +	/* multiply numerator by 100 to avoid handling ratio < 1 */
> +	ratio = DIV_ROUND_UP(ir_data * 100, ir_data + vis_data);
> +
> +	if (ratio < 45)
> +		lux = LTR501_LUX_CONV(1774, vis_data, -1105, ir_data);
> +	else if (ratio >= 45 && ratio < 64)
> +		lux = LTR501_LUX_CONV(3772, vis_data, 1336, ir_data);
> +	else if (ratio >= 64 && ratio < 85)
> +		lux = LTR501_LUX_CONV(1690, vis_data, 169, ir_data);
> +	else
> +		lux = 0;
> +
> +	return lux / 1000;
> +}
> +
>  static int ltr501_drdy(struct ltr501_data *data, u8 drdy_mask)
>  {
>  	int tries = 100;
> @@ -548,7 +574,14 @@ static const struct iio_event_spec ltr501_pxs_event_spec[] = {
>  	.num_event_specs = _evsize,\
>  }
>  
> +#define LTR501_LIGHT_CHANNEL() { \
> +	.type = IIO_LIGHT, \
> +	.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
> +	.scan_index = -1, \
> +}
> +
>  static const struct iio_chan_spec ltr501_channels[] = {
> +	LTR501_LIGHT_CHANNEL(),
>  	LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0,
>  				 ltr501_als_event_spec,
>  				 ARRAY_SIZE(ltr501_als_event_spec)),
> @@ -576,6 +609,7 @@ static const struct iio_chan_spec ltr501_channels[] = {
>  };
>  
>  static const struct iio_chan_spec ltr301_channels[] = {
> +	LTR501_LIGHT_CHANNEL(),
>  	LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0,
>  				 ltr501_als_event_spec,
>  				 ARRAY_SIZE(ltr501_als_event_spec)),
> @@ -596,6 +630,23 @@ static int ltr501_read_raw(struct iio_dev *indio_dev,
>  	int ret, i;
>  
>  	switch (mask) {
> +	case IIO_CHAN_INFO_PROCESSED:
> +		if (iio_buffer_enabled(indio_dev))
> +			return -EBUSY;
> +
> +		switch (chan->type) {
> +		case IIO_LIGHT:
> +			mutex_lock(&data->lock_als);
> +			ret = ltr501_read_als(data, buf);
> +			mutex_unlock(&data->lock_als);
> +			if (ret < 0)
> +				return ret;
> +			*val = ltr501_calculate_lux(le16_to_cpu(buf[1]),
> +						    le16_to_cpu(buf[0]));
> +			return IIO_VAL_INT;
> +		default:
> +			return -EINVAL;
> +		}
>  	case IIO_CHAN_INFO_RAW:
>  		if (iio_buffer_enabled(indio_dev))
>  			return -EBUSY;
> 


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

* Re: [PATCH v3 1/1] iio: ltr501: Add light channel support
@ 2015-05-17  8:14   ` Jonathan Cameron
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Cameron @ 2015-05-17  8:14 UTC (permalink / raw)
  To: Kuppuswamy Sathyanarayanan, pmeerw
  Cc: srinivas.pandruvada, linux-kernel, linux-iio

On 16/05/15 02:23, Kuppuswamy Sathyanarayanan wrote:
> Added support to calculate lux value from visible
> and IR spectrum adc count values. Also added IIO_LIGHT
> channel to enable user read the lux value directly
> from device using illuminance input ABI.
> 
> Signed-off-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
Applied to the togreg branch of iio.git - initially pushed out as testing for
the autobuilders to play with it.

Jonathan
> ---
>  drivers/iio/light/ltr501.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 51 insertions(+)
> 
> v2: Changed scan index of light channel to -1
> v3: Removed scan type info from light channel
> 
> diff --git a/drivers/iio/light/ltr501.c b/drivers/iio/light/ltr501.c
> index ca4bf47..d7245c6 100644
> --- a/drivers/iio/light/ltr501.c
> +++ b/drivers/iio/light/ltr501.c
> @@ -66,6 +66,9 @@
>  
>  #define LTR501_REGMAP_NAME "ltr501_regmap"
>  
> +#define LTR501_LUX_CONV(vis_coeff, vis_data, ir_coeff, ir_data) \
> +			((vis_coeff * vis_data) - (ir_coeff * ir_data))
> +
>  static const int int_time_mapping[] = {100000, 50000, 200000, 400000};
>  
>  static const struct reg_field reg_field_it =
> @@ -298,6 +301,29 @@ static int ltr501_ps_read_samp_period(struct ltr501_data *data, int *val)
>  	return IIO_VAL_INT;
>  }
>  
> +/* IR and visible spectrum coeff's are given in data sheet */
> +static unsigned long ltr501_calculate_lux(u16 vis_data, u16 ir_data)
> +{
> +	unsigned long ratio, lux;
> +
> +	if (vis_data == 0)
> +		return 0;
> +
> +	/* multiply numerator by 100 to avoid handling ratio < 1 */
> +	ratio = DIV_ROUND_UP(ir_data * 100, ir_data + vis_data);
> +
> +	if (ratio < 45)
> +		lux = LTR501_LUX_CONV(1774, vis_data, -1105, ir_data);
> +	else if (ratio >= 45 && ratio < 64)
> +		lux = LTR501_LUX_CONV(3772, vis_data, 1336, ir_data);
> +	else if (ratio >= 64 && ratio < 85)
> +		lux = LTR501_LUX_CONV(1690, vis_data, 169, ir_data);
> +	else
> +		lux = 0;
> +
> +	return lux / 1000;
> +}
> +
>  static int ltr501_drdy(struct ltr501_data *data, u8 drdy_mask)
>  {
>  	int tries = 100;
> @@ -548,7 +574,14 @@ static const struct iio_event_spec ltr501_pxs_event_spec[] = {
>  	.num_event_specs = _evsize,\
>  }
>  
> +#define LTR501_LIGHT_CHANNEL() { \
> +	.type = IIO_LIGHT, \
> +	.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
> +	.scan_index = -1, \
> +}
> +
>  static const struct iio_chan_spec ltr501_channels[] = {
> +	LTR501_LIGHT_CHANNEL(),
>  	LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0,
>  				 ltr501_als_event_spec,
>  				 ARRAY_SIZE(ltr501_als_event_spec)),
> @@ -576,6 +609,7 @@ static const struct iio_chan_spec ltr501_channels[] = {
>  };
>  
>  static const struct iio_chan_spec ltr301_channels[] = {
> +	LTR501_LIGHT_CHANNEL(),
>  	LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0,
>  				 ltr501_als_event_spec,
>  				 ARRAY_SIZE(ltr501_als_event_spec)),
> @@ -596,6 +630,23 @@ static int ltr501_read_raw(struct iio_dev *indio_dev,
>  	int ret, i;
>  
>  	switch (mask) {
> +	case IIO_CHAN_INFO_PROCESSED:
> +		if (iio_buffer_enabled(indio_dev))
> +			return -EBUSY;
> +
> +		switch (chan->type) {
> +		case IIO_LIGHT:
> +			mutex_lock(&data->lock_als);
> +			ret = ltr501_read_als(data, buf);
> +			mutex_unlock(&data->lock_als);
> +			if (ret < 0)
> +				return ret;
> +			*val = ltr501_calculate_lux(le16_to_cpu(buf[1]),
> +						    le16_to_cpu(buf[0]));
> +			return IIO_VAL_INT;
> +		default:
> +			return -EINVAL;
> +		}
>  	case IIO_CHAN_INFO_RAW:
>  		if (iio_buffer_enabled(indio_dev))
>  			return -EBUSY;
> 


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

end of thread, other threads:[~2015-05-17  8:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-16  1:23 [PATCH v3 1/1] iio: ltr501: Add light channel support Kuppuswamy Sathyanarayanan
2015-05-16  1:23 ` Kuppuswamy Sathyanarayanan
2015-05-17  8:14 ` Jonathan Cameron
2015-05-17  8:14   ` Jonathan Cameron

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.