Input: ti_am335x_tsc - replace delta filtering with median filtering

Previously, delta filtering was applied TSC co-ordinate readouts before
reporting a single value to user space. This patch replaces delta filtering
with median filtering. Median filtering sorts co-ordinate readouts, drops
min and max values, and reports the average of remaining values. This
method is more sensible than delta filtering. Median filtering is applied
only if number of readouts is greater than 3 else just average of
co-ordinate readouts is reported.

Signed-off-by: Vignesh R <vigneshr@ti.com>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
This commit is contained in:
Vignesh R 2015-02-03 11:47:05 -08:00 committed by Dmitry Torokhov
parent e6e4a0d198
commit 83edfdf30b

View file

@ -26,6 +26,7 @@
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/of.h> #include <linux/of.h>
#include <linux/of_device.h> #include <linux/of_device.h>
#include <linux/sort.h>
#include <linux/mfd/ti_am335x_tscadc.h> #include <linux/mfd/ti_am335x_tscadc.h>
@ -204,56 +205,61 @@ static void titsc_step_config(struct titsc *ts_dev)
am335x_tsc_se_set_cache(ts_dev->mfd_tscadc, ts_dev->step_mask); am335x_tsc_se_set_cache(ts_dev->mfd_tscadc, ts_dev->step_mask);
} }
static int titsc_cmp_coord(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
static void titsc_read_coordinates(struct titsc *ts_dev, static void titsc_read_coordinates(struct titsc *ts_dev,
u32 *x, u32 *y, u32 *z1, u32 *z2) u32 *x, u32 *y, u32 *z1, u32 *z2)
{ {
unsigned int fifocount = titsc_readl(ts_dev, REG_FIFO0CNT); unsigned int yvals[7], xvals[7];
unsigned int prev_val_x = ~0, prev_val_y = ~0; unsigned int i, xsum = 0, ysum = 0;
unsigned int prev_diff_x = ~0, prev_diff_y = ~0;
unsigned int read, diff;
unsigned int i, channel;
unsigned int creads = ts_dev->coordinate_readouts; unsigned int creads = ts_dev->coordinate_readouts;
unsigned int first_step = TOTAL_STEPS - (creads * 2 + 2);
*z1 = *z2 = 0; for (i = 0; i < creads; i++) {
if (fifocount % (creads * 2 + 2)) yvals[i] = titsc_readl(ts_dev, REG_FIFO0);
fifocount -= fifocount % (creads * 2 + 2); yvals[i] &= 0xfff;
}
*z1 = titsc_readl(ts_dev, REG_FIFO0);
*z1 &= 0xfff;
*z2 = titsc_readl(ts_dev, REG_FIFO0);
*z2 &= 0xfff;
for (i = 0; i < creads; i++) {
xvals[i] = titsc_readl(ts_dev, REG_FIFO0);
xvals[i] &= 0xfff;
}
/* /*
* Delta filter is used to remove large variations in sampled * If co-ordinates readouts is less than 4 then
* values from ADC. The filter tries to predict where the next * report the average. In case of 4 or more
* coordinate could be. This is done by taking a previous * readouts, sort the co-ordinate samples, drop
* coordinate and subtracting it form current one. Further the * min and max values and report the average of
* algorithm compares the difference with that of a present value, * remaining values.
* if true the value is reported to the sub system.
*/ */
for (i = 0; i < fifocount; i++) { if (creads <= 3) {
read = titsc_readl(ts_dev, REG_FIFO0); for (i = 0; i < creads; i++) {
ysum += yvals[i];
channel = (read & 0xf0000) >> 16; xsum += xvals[i];
read &= 0xfff;
if (channel > first_step + creads + 2) {
diff = abs(read - prev_val_x);
if (diff < prev_diff_x) {
prev_diff_x = diff;
*x = read;
} }
prev_val_x = read; ysum /= creads;
xsum /= creads;
} else if (channel == first_step + creads + 1) { } else {
*z1 = read; sort(yvals, creads, sizeof(unsigned int),
titsc_cmp_coord, NULL);
} else if (channel == first_step + creads + 2) { sort(xvals, creads, sizeof(unsigned int),
*z2 = read; titsc_cmp_coord, NULL);
for (i = 1; i < creads - 1; i++) {
} else if (channel > first_step) { ysum += yvals[i];
diff = abs(read - prev_val_y); xsum += xvals[i];
if (diff < prev_diff_y) {
prev_diff_y = diff;
*y = read;
}
prev_val_y = read;
} }
ysum /= creads - 2;
xsum /= creads - 2;
} }
*y = ysum;
*x = xsum;
} }
static irqreturn_t titsc_irq(int irq, void *dev) static irqreturn_t titsc_irq(int irq, void *dev)
@ -369,6 +375,12 @@ static int titsc_parse_dt(struct platform_device *pdev,
if (err < 0) if (err < 0)
return err; return err;
if (ts_dev->coordinate_readouts <= 0) {
dev_warn(&pdev->dev,
"invalid co-ordinate readouts, resetting it to 5\n");
ts_dev->coordinate_readouts = 5;
}
err = of_property_read_u32(node, "ti,charge-delay", err = of_property_read_u32(node, "ti,charge-delay",
&ts_dev->charge_delay); &ts_dev->charge_delay);
/* /*