Commit bce075d0 authored by Brian Masney's avatar Brian Masney Committed by Jonathan Cameron

staging: iio: tsl2x7x: simplify tsl2x7x_prox_cal()

tsl2x7x_prox_cal() would set the interrupt flag, and reset the device to
start doing the calibration routine. However, this did not actually
affect the readings since they are polled. This patch drops the interrupt
code.

This patch also drops the function tsl2x7x_prox_calculate() and removes
support for the standard deviation and min sample since those values
were not used.

Driver was tested using a TSL2772 hooked up to a Raspberry Pi 2. I
performed the following testing at various distances:

- Put hand in front of sensor and keep the sensor and hand stationary.
- Perform calibration routine.
- Run iio_event_monitor.
- Verify that a proximity event is triggered when my hand comes
  anywhere between the sensor and where I performed the calibration
  routine.
Signed-off-by: default avatarBrian Masney <masneyb@onstation.org>
Signed-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 85a7d0f7
...@@ -149,13 +149,6 @@ struct tsl2x7x_als_info { ...@@ -149,13 +149,6 @@ struct tsl2x7x_als_info {
u16 lux; u16 lux;
}; };
struct tsl2x7x_prox_stat {
int min;
int max;
int mean;
unsigned long stddev;
};
struct tsl2x7x_chip_info { struct tsl2x7x_chip_info {
int chan_table_elements; int chan_table_elements;
struct iio_chan_spec channel[4]; struct iio_chan_spec channel[4];
...@@ -771,106 +764,36 @@ static int tsl2x7x_invoke_change(struct iio_dev *indio_dev) ...@@ -771,106 +764,36 @@ static int tsl2x7x_invoke_change(struct iio_dev *indio_dev)
return ret; return ret;
} }
static void tsl2x7x_prox_calculate(int *data, int length,
struct tsl2x7x_prox_stat *stat)
{
int i;
int sample_sum;
int tmp;
if (!length)
length = 1;
sample_sum = 0;
stat->min = INT_MAX;
stat->max = INT_MIN;
for (i = 0; i < length; i++) {
sample_sum += data[i];
stat->min = min(stat->min, data[i]);
stat->max = max(stat->max, data[i]);
}
stat->mean = sample_sum / length;
sample_sum = 0;
for (i = 0; i < length; i++) {
tmp = data[i] - stat->mean;
sample_sum += tmp * tmp;
}
stat->stddev = int_sqrt((long)sample_sum / length);
}
/**
* tsl2x7x_prox_cal() - Calculates std. and sets thresholds.
* @indio_dev: pointer to IIO device
*
* Calculates a standard deviation based on the samples,
* and sets the threshold accordingly.
*/
static int tsl2x7x_prox_cal(struct iio_dev *indio_dev) static int tsl2x7x_prox_cal(struct iio_dev *indio_dev)
{ {
int prox_history[MAX_SAMPLES_CAL + 1];
int i, ret;
struct tsl2x7x_prox_stat prox_stat_data[2];
struct tsl2x7x_prox_stat *cal;
struct tsl2X7X_chip *chip = iio_priv(indio_dev); struct tsl2X7X_chip *chip = iio_priv(indio_dev);
u8 tmp_irq_settings; int prox_history[MAX_SAMPLES_CAL + 1];
u8 current_state = chip->tsl2x7x_chip_status; int i, ret, mean, max, sample_sum;
if (chip->settings.prox_max_samples_cal > MAX_SAMPLES_CAL) {
dev_err(&chip->client->dev,
"max prox samples cal is too big: %d\n",
chip->settings.prox_max_samples_cal);
chip->settings.prox_max_samples_cal = MAX_SAMPLES_CAL;
}
/* have to stop to change settings */
ret = tsl2x7x_chip_off(indio_dev);
if (ret < 0)
return ret;
/* Enable proximity detection save just in case prox not wanted yet*/
tmp_irq_settings = chip->settings.interrupts_en;
chip->settings.interrupts_en |= TSL2X7X_CNTL_PROX_INT_ENBL;
/*turn on device if not already on*/ if (chip->settings.prox_max_samples_cal < 1 ||
ret = tsl2x7x_chip_on(indio_dev); chip->settings.prox_max_samples_cal > MAX_SAMPLES_CAL)
if (ret < 0) return -EINVAL;
return ret;
/*gather the samples*/
for (i = 0; i < chip->settings.prox_max_samples_cal; i++) { for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
usleep_range(15000, 17500); usleep_range(15000, 17500);
ret = tsl2x7x_get_prox(indio_dev); ret = tsl2x7x_get_prox(indio_dev);
if (ret < 0) if (ret < 0)
return ret; return ret;
prox_history[i] = chip->prox_data; prox_history[i] = chip->prox_data;
dev_info(&chip->client->dev, "2 i=%d prox data= %d\n",
i, chip->prox_data);
} }
ret = tsl2x7x_chip_off(indio_dev); sample_sum = 0;
if (ret < 0) max = INT_MIN;
return ret; for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
cal = &prox_stat_data[PROX_STAT_CAL]; sample_sum += prox_history[i];
tsl2x7x_prox_calculate(prox_history, max = max(max, prox_history[i]);
chip->settings.prox_max_samples_cal, cal);
chip->settings.prox_thres_high = (cal->max << 1) - cal->mean;
dev_info(&chip->client->dev, " cal min=%d mean=%d max=%d\n",
cal->min, cal->mean, cal->max);
dev_info(&chip->client->dev,
"%s proximity threshold set to %d\n",
chip->client->name, chip->settings.prox_thres_high);
/* back to the way they were */
chip->settings.interrupts_en = tmp_irq_settings;
if (current_state == TSL2X7X_CHIP_WORKING) {
ret = tsl2x7x_chip_on(indio_dev);
if (ret < 0)
return ret;
} }
mean = sample_sum / chip->settings.prox_max_samples_cal;
return 0; chip->settings.prox_thres_high = (max << 1) - mean;
return tsl2x7x_invoke_change(indio_dev);
} }
static ssize_t static ssize_t
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment