aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>2015-05-15 21:23:21 -0400
committerJonathan Cameron <jic23@kernel.org>2015-05-17 04:14:10 -0400
commitc06cee8d0865b7478484d9472155d8df83a10c06 (patch)
treee42f2d274e434175c19b21e00929b578b315aeae
parentc24e7daf823256c83ce3efe6fa9b9b8ab5b78480 (diff)
iio: ltr501: Add light channel support
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> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
-rw-r--r--drivers/iio/light/ltr501.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/drivers/iio/light/ltr501.c b/drivers/iio/light/ltr501.c
index 64e235281cef..1ef7d3773ab9 100644
--- a/drivers/iio/light/ltr501.c
+++ b/drivers/iio/light/ltr501.c
@@ -66,6 +66,9 @@
66 66
67#define LTR501_REGMAP_NAME "ltr501_regmap" 67#define LTR501_REGMAP_NAME "ltr501_regmap"
68 68
69#define LTR501_LUX_CONV(vis_coeff, vis_data, ir_coeff, ir_data) \
70 ((vis_coeff * vis_data) - (ir_coeff * ir_data))
71
69static const int int_time_mapping[] = {100000, 50000, 200000, 400000}; 72static const int int_time_mapping[] = {100000, 50000, 200000, 400000};
70 73
71static const struct reg_field reg_field_it = 74static const struct reg_field reg_field_it =
@@ -298,6 +301,29 @@ static int ltr501_ps_read_samp_period(struct ltr501_data *data, int *val)
298 return IIO_VAL_INT; 301 return IIO_VAL_INT;
299} 302}
300 303
304/* IR and visible spectrum coeff's are given in data sheet */
305static unsigned long ltr501_calculate_lux(u16 vis_data, u16 ir_data)
306{
307 unsigned long ratio, lux;
308
309 if (vis_data == 0)
310 return 0;
311
312 /* multiply numerator by 100 to avoid handling ratio < 1 */
313 ratio = DIV_ROUND_UP(ir_data * 100, ir_data + vis_data);
314
315 if (ratio < 45)
316 lux = LTR501_LUX_CONV(1774, vis_data, -1105, ir_data);
317 else if (ratio >= 45 && ratio < 64)
318 lux = LTR501_LUX_CONV(3772, vis_data, 1336, ir_data);
319 else if (ratio >= 64 && ratio < 85)
320 lux = LTR501_LUX_CONV(1690, vis_data, 169, ir_data);
321 else
322 lux = 0;
323
324 return lux / 1000;
325}
326
301static int ltr501_drdy(struct ltr501_data *data, u8 drdy_mask) 327static int ltr501_drdy(struct ltr501_data *data, u8 drdy_mask)
302{ 328{
303 int tries = 100; 329 int tries = 100;
@@ -548,7 +574,14 @@ static const struct iio_event_spec ltr501_pxs_event_spec[] = {
548 .num_event_specs = _evsize,\ 574 .num_event_specs = _evsize,\
549} 575}
550 576
577#define LTR501_LIGHT_CHANNEL() { \
578 .type = IIO_LIGHT, \
579 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
580 .scan_index = -1, \
581}
582
551static const struct iio_chan_spec ltr501_channels[] = { 583static const struct iio_chan_spec ltr501_channels[] = {
584 LTR501_LIGHT_CHANNEL(),
552 LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0, 585 LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0,
553 ltr501_als_event_spec, 586 ltr501_als_event_spec,
554 ARRAY_SIZE(ltr501_als_event_spec)), 587 ARRAY_SIZE(ltr501_als_event_spec)),
@@ -576,6 +609,7 @@ static const struct iio_chan_spec ltr501_channels[] = {
576}; 609};
577 610
578static const struct iio_chan_spec ltr301_channels[] = { 611static const struct iio_chan_spec ltr301_channels[] = {
612 LTR501_LIGHT_CHANNEL(),
579 LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0, 613 LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0,
580 ltr501_als_event_spec, 614 ltr501_als_event_spec,
581 ARRAY_SIZE(ltr501_als_event_spec)), 615 ARRAY_SIZE(ltr501_als_event_spec)),
@@ -596,6 +630,23 @@ static int ltr501_read_raw(struct iio_dev *indio_dev,
596 int ret, i; 630 int ret, i;
597 631
598 switch (mask) { 632 switch (mask) {
633 case IIO_CHAN_INFO_PROCESSED:
634 if (iio_buffer_enabled(indio_dev))
635 return -EBUSY;
636
637 switch (chan->type) {
638 case IIO_LIGHT:
639 mutex_lock(&data->lock_als);
640 ret = ltr501_read_als(data, buf);
641 mutex_unlock(&data->lock_als);
642 if (ret < 0)
643 return ret;
644 *val = ltr501_calculate_lux(le16_to_cpu(buf[1]),
645 le16_to_cpu(buf[0]));
646 return IIO_VAL_INT;
647 default:
648 return -EINVAL;
649 }
599 case IIO_CHAN_INFO_RAW: 650 case IIO_CHAN_INFO_RAW:
600 if (iio_buffer_enabled(indio_dev)) 651 if (iio_buffer_enabled(indio_dev))
601 return -EBUSY; 652 return -EBUSY;