Commit b90233658424cb89da201e84ca3ef19fa3552e5d

Authored by Vignesh R
Committed by Jacob Stiffler
1 parent 88f7cc4b04

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>
Signed-off-by: Franklin S Cooper Jr <fcooper@ti.com>

Showing 1 changed file with 52 additions and 40 deletions Side-by-side Diff

drivers/input/touchscreen/ti_am335x_tsc.c
... ... @@ -26,6 +26,7 @@
26 26 #include <linux/delay.h>
27 27 #include <linux/of.h>
28 28 #include <linux/of_device.h>
  29 +#include <linux/sort.h>
29 30  
30 31 #include <linux/mfd/ti_am335x_tscadc.h>
31 32  
32 33  
33 34  
34 35  
35 36  
36 37  
37 38  
38 39  
39 40  
... ... @@ -204,56 +205,61 @@
204 205 am335x_tsc_se_set_cache(ts_dev->mfd_tscadc, ts_dev->step_mask);
205 206 }
206 207  
  208 +static int titsc_cmp_coord(const void *a, const void *b)
  209 +{
  210 + return *(int *)a - *(int *)b;
  211 +}
  212 +
207 213 static void titsc_read_coordinates(struct titsc *ts_dev,
208 214 u32 *x, u32 *y, u32 *z1, u32 *z2)
209 215 {
210   - unsigned int fifocount = titsc_readl(ts_dev, REG_FIFO0CNT);
211   - unsigned int prev_val_x = ~0, prev_val_y = ~0;
212   - unsigned int prev_diff_x = ~0, prev_diff_y = ~0;
213   - unsigned int read, diff;
214   - unsigned int i, channel;
  216 + unsigned int yvals[7], xvals[7];
  217 + unsigned int i, xsum = 0, ysum = 0;
215 218 unsigned int creads = ts_dev->coordinate_readouts;
216   - unsigned int first_step = TOTAL_STEPS - (creads * 2 + 2);
217 219  
218   - *z1 = *z2 = 0;
219   - if (fifocount % (creads * 2 + 2))
220   - fifocount -= fifocount % (creads * 2 + 2);
221   - /*
222   - * Delta filter is used to remove large variations in sampled
223   - * values from ADC. The filter tries to predict where the next
224   - * coordinate could be. This is done by taking a previous
225   - * coordinate and subtracting it form current one. Further the
226   - * algorithm compares the difference with that of a present value,
227   - * if true the value is reported to the sub system.
228   - */
229   - for (i = 0; i < fifocount; i++) {
230   - read = titsc_readl(ts_dev, REG_FIFO0);
  220 + for (i = 0; i < creads; i++) {
  221 + yvals[i] = titsc_readl(ts_dev, REG_FIFO0);
  222 + yvals[i] &= 0xfff;
  223 + }
231 224  
232   - channel = (read & 0xf0000) >> 16;
233   - read &= 0xfff;
234   - if (channel > first_step + creads + 2) {
235   - diff = abs(read - prev_val_x);
236   - if (diff < prev_diff_x) {
237   - prev_diff_x = diff;
238   - *x = read;
239   - }
240   - prev_val_x = read;
  225 + *z1 = titsc_readl(ts_dev, REG_FIFO0);
  226 + *z1 &= 0xfff;
  227 + *z2 = titsc_readl(ts_dev, REG_FIFO0);
  228 + *z2 &= 0xfff;
241 229  
242   - } else if (channel == first_step + creads + 1) {
243   - *z1 = read;
  230 + for (i = 0; i < creads; i++) {
  231 + xvals[i] = titsc_readl(ts_dev, REG_FIFO0);
  232 + xvals[i] &= 0xfff;
  233 + }
244 234  
245   - } else if (channel == first_step + creads + 2) {
246   - *z2 = read;
247   -
248   - } else if (channel > first_step) {
249   - diff = abs(read - prev_val_y);
250   - if (diff < prev_diff_y) {
251   - prev_diff_y = diff;
252   - *y = read;
253   - }
254   - prev_val_y = read;
  235 + /*
  236 + * If co-ordinates readouts is less than 4 then
  237 + * report the average. In case of 4 or more
  238 + * readouts, sort the co-ordinate samples, drop
  239 + * min and max values and report the average of
  240 + * remaining values.
  241 + */
  242 + if (creads <= 3) {
  243 + for (i = 0; i < creads; i++) {
  244 + ysum += yvals[i];
  245 + xsum += xvals[i];
255 246 }
  247 + ysum /= creads;
  248 + xsum /= creads;
  249 + } else {
  250 + sort(yvals, creads, sizeof(unsigned int),
  251 + titsc_cmp_coord, NULL);
  252 + sort(xvals, creads, sizeof(unsigned int),
  253 + titsc_cmp_coord, NULL);
  254 + for (i = 1; i < creads - 1; i++) {
  255 + ysum += yvals[i];
  256 + xsum += xvals[i];
  257 + }
  258 + ysum /= creads - 2;
  259 + xsum /= creads - 2;
256 260 }
  261 + *y = ysum;
  262 + *x = xsum;
257 263 }
258 264  
259 265 static irqreturn_t titsc_irq(int irq, void *dev)
... ... @@ -363,6 +369,12 @@
363 369 &ts_dev->coordinate_readouts);
364 370 if (err < 0)
365 371 return err;
  372 +
  373 + if (ts_dev->coordinate_readouts <= 0) {
  374 + dev_warn(&pdev->dev,
  375 + "invalid co-ordinate readouts, resetting it to 5\n");
  376 + ts_dev->coordinate_readouts = 5;
  377 + }
366 378  
367 379 err = of_property_read_u32(node, "ti,charge-delay",
368 380 &ts_dev->charge_delay);