input: touchscreen: add touch screen of gslx680 for rk3399-firefly-edp
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / ts_lib / dejitter.c
1 /*
2  *  tslib/dejitter.c
3  *
4  *  Copyright (C) 2001 Russell King.
5  *
6  * This file is placed under the LGPL.  Please see the file
7  * COPYING for more details.
8  *
9  *
10  * Problem: some touchscreens read the X/Y values from ADC with a
11  * great level of noise in their lowest bits. This produces "jitter"
12  * in touchscreen output, e.g. even if we hold the stylus still,
13  * we get a great deal of X/Y coordinate pairs that are close enough
14  * but not equal. Also if we try to draw a straight line in a painter
15  * program, we'll get a line full of spikes.
16  *
17  * Solution: we apply a smoothing filter on the last several values
18  * thus excluding spikes from output. If we detect a substantial change
19  * in coordinates, we reset the backlog of pen positions, thus avoiding
20  * smoothing coordinates that are not supposed to be smoothed. This
21  * supposes all noise has been filtered by the lower-level filter,
22  * e.g. by the "variance" module.
23  */
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/fcntl.h>
30 #include <linux/delay.h>
31 #include <linux/device.h>
32 //#include <asm/typedef.h>
33 #include <mach/iomux.h>
34 #include <asm/uaccess.h>
35 #include <asm/types.h>
36 #include <asm/io.h>
37 #include <asm/delay.h>
38 #include "tslib.h"
39
40 //#define DEBUG
41 /**
42  * This filter works as follows: we keep track of latest N samples,
43  * and average them with certain weights. The oldest samples have the
44  * least weight and the most recent samples have the most weight.
45  * This helps remove the jitter and at the same time doesn't influence
46  * responsivity because for each input sample we generate one output
47  * sample; pen movement becomes just somehow more smooth.
48  */
49
50 /* To keep things simple (avoiding division) we ensure that
51  * SUM(weight) = power-of-two. Also we must know how to approximate
52  * measurements when we have less than NR_SAMPHISTLEN samples.
53  */
54 static const unsigned char weight [NR_SAMPHISTLEN - 1][NR_SAMPHISTLEN + 1] =
55 {
56         /* The last element is pow2(SUM(0..3)) */
57         { 5, 3, 0, 0, 3 },      /* When we have 2 samples ... */
58         { 8, 5, 3, 0, 4 },      /* When we have 3 samples ... */
59         { 6, 4, 3, 3, 4 },      /* When we have 4 samples ... */
60 };
61
62 static void average (struct tslib_dejitter *djt, struct ts_sample *samp)
63 {
64         const unsigned char *w;
65         int sn = djt->head;
66         int i, x = 0, y = 0;
67         //unsigned int p = 0;
68
69         w = weight [djt->nr - 2];
70
71         for (i = 0; i < djt->nr; i++) {
72                 x += djt->hist [sn].x * w [i];
73                 y += djt->hist [sn].y * w [i];
74                 //p += djt->hist [sn].p * w [i];
75                 sn = (sn - 1) & (NR_SAMPHISTLEN - 1);
76         }
77
78         samp->x = x >> w [NR_SAMPHISTLEN];
79         samp->y = y >> w [NR_SAMPHISTLEN];
80         //samp->pressure = p >> w [NR_SAMPHISTLEN];
81 #ifdef DEBUG
82         printk("DEJITTER----------------> %d %d %d\n",
83                 samp->x, samp->y, samp->pressure);
84 #endif
85 }
86
87 int dejitter_read(struct tslib_info *info, struct ts_sample *samp, int nr)
88 {
89         struct tslib_dejitter *djt = info->djt;
90         struct ts_sample *s;
91         int count = 0, ret;
92
93         ret = variance_read(info, samp, nr);
94         for (s = samp; ret > 0; s++, ret--) {
95                 if (s->pressure == 0) {
96                         /*
97                          * Pen was released. Reset the state and
98                          * forget all history events.
99                          */
100                         djt->nr = 0;
101                         samp [count++] = *s;
102                         continue;
103                 }
104
105         /* If the pen moves too fast, reset the backlog. */
106                 if (djt->nr) {
107                         int prev = (djt->head - 1) & (NR_SAMPHISTLEN - 1);
108                         if (sqr(s->x - djt->hist [prev].x) +
109                             sqr(s->y - djt->hist [prev].y) > djt->delta) {
110 #ifdef DEBUG
111                                 printk("DEJITTER: pen movement exceeds threshold\n");
112 #endif
113                                 djt->nr = 0;
114                         }
115                 }
116
117                 djt->hist[djt->head].x = s->x;
118                 djt->hist[djt->head].y = s->y;
119                 djt->hist[djt->head].p = s->pressure;
120                 if (djt->nr < NR_SAMPHISTLEN)
121                         djt->nr++;
122
123                 /* We'll pass through the very first sample since
124                  * we can't average it (no history yet).
125                  */
126                 if (djt->nr == 1)
127                         samp [count] = *s;
128                 else {
129                         average (djt, samp + count);
130                 }
131                 count++;
132
133                 djt->head = (djt->head + 1) & (NR_SAMPHISTLEN - 1);
134         }
135
136         return count;
137 }