change prefix for kernelpatchbase 2.6.26
[openwrt/staging/dedeckeh.git] / target / linux / s3c24xx / patches-2.6.26 / 1188-touchscreen-meddling.patch.patch
1 From 76d6ab9228efca87d3e6db46e918c7bbf19f6540 Mon Sep 17 00:00:00 2001
2 From: Andy Green <andy@openmoko.com>
3 Date: Fri, 25 Jul 2008 23:06:16 +0100
4 Subject: [PATCH] touchscreen-meddling.patch
5
6 Touchscreen on GTA01-02 experiences noise on the channel that serves the
7 "tall axis" of the LCM. The sample quality of the other axis is good.
8 The bad samples have a characteristic of one shot excursions that can
9 reach +/- 20% or more of the sample average.
10
11 Previously, we had a simple averaging scheme going in the touchscreen
12 driver that summed up 32 x and ys and then divided it by 32. This patch
13 first tidies up the existing code for style, then adds a new "running
14 average" concept with a FIFO. The running average is separate from the
15 summing average mentioned above, and is accurate for the last n samples
16 sample-by-sample, where n is set by 1 << excursion_filter_len_bits in the
17 machine / platform stuff.
18
19 The heuristic the patch implements for the filtering is to accept all
20 samples, but tag the *previous* sample with a flag if it differed from
21 the running average by more than reject_threshold_vs_avg in either
22 axis. The next sample time, a beauty contest is held if the flag was
23 set to decide if we think the previous sample was a one-shot excursion
24 (detected by the new sample being closer to the average than to the
25 flagged previous sample), or if we believe we are moving (detected by
26 the new sample being closer to the flagged previous sample than the
27 average. In the case that we believe the previous sample was an
28 excursion, we simply overwrite it with the new data and adjust the
29 summing average to use the new data instead of the excursion data.
30
31 I only tested this by eyeballing the output of ts_print_raw, but it
32 seemed to be quite a bit better. Gross movement appeared to be
33 tracked fine too. If folks want to try different heuristics on top
34 of this patch, be my guest; either way feedback on what it looks like
35 with a graphical app would be good.
36
37 Signed-off-by: Andy Green <andy@openmoko.com>
38 ---
39 arch/arm/mach-s3c2440/mach-gta02.c | 10 +-
40 drivers/input/touchscreen/s3c2410_ts.c | 256 ++++++++++++++++++++++++--------
41 include/asm-arm/arch-s3c2410/ts.h | 8 +-
42 3 files changed, 205 insertions(+), 69 deletions(-)
43
44 diff --git a/arch/arm/mach-s3c2440/mach-gta02.c b/arch/arm/mach-s3c2440/mach-gta02.c
45 index c32bb2a..afe8039 100644
46 --- a/arch/arm/mach-s3c2440/mach-gta02.c
47 +++ b/arch/arm/mach-s3c2440/mach-gta02.c
48 @@ -897,10 +897,18 @@ static struct s3c2410_udc_mach_info gta02_udc_cfg = {
49
50 static struct s3c2410_ts_mach_info gta02_ts_cfg = {
51 .delay = 10000,
52 - .presc = 65,
53 + .presc = 50000000 / 1000000, /* 50 MHz PCLK / 1MHz */
54 + /* simple averaging, 2^n samples */
55 .oversampling_shift = 5,
56 + /* averaging filter length, 2^n */
57 + .excursion_filter_len_bits = 5,
58 + /* flagged for beauty contest on next sample if differs from
59 + * average more than this
60 + */
61 + .reject_threshold_vs_avg = 2,
62 };
63
64 +
65 /* SPI: LCM control interface attached to Glamo3362 */
66
67 static void gta02_jbt6k74_reset(int devidx, int level)
68 diff --git a/drivers/input/touchscreen/s3c2410_ts.c b/drivers/input/touchscreen/s3c2410_ts.c
69 index 1251454..b1ba73d 100644
70 --- a/drivers/input/touchscreen/s3c2410_ts.c
71 +++ b/drivers/input/touchscreen/s3c2410_ts.c
72 @@ -36,6 +36,9 @@
73 *
74 * 2007-05-23: Harald Welte <laforge@openmoko.org>
75 * - Add proper support for S32440
76 + *
77 + * 2008-06-18: Andy Green <andy@openmoko.com>
78 + * - Outlier removal
79 */
80
81 #include <linux/errno.h>
82 @@ -62,11 +65,16 @@
83 #define TSC_SLEEP (S3C2410_ADCTSC_PULL_UP_DISABLE | S3C2410_ADCTSC_XY_PST(0))
84
85 #define WAIT4INT(x) (((x)<<8) | \
86 - S3C2410_ADCTSC_YM_SEN | S3C2410_ADCTSC_YP_SEN | S3C2410_ADCTSC_XP_SEN | \
87 + S3C2410_ADCTSC_YM_SEN | \
88 + S3C2410_ADCTSC_YP_SEN | \
89 + S3C2410_ADCTSC_XP_SEN | \
90 S3C2410_ADCTSC_XY_PST(3))
91
92 -#define AUTOPST (S3C2410_ADCTSC_YM_SEN | S3C2410_ADCTSC_YP_SEN | S3C2410_ADCTSC_XP_SEN | \
93 - S3C2410_ADCTSC_AUTO_PST | S3C2410_ADCTSC_XY_PST(0))
94 +#define AUTOPST (S3C2410_ADCTSC_YM_SEN | \
95 + S3C2410_ADCTSC_YP_SEN | \
96 + S3C2410_ADCTSC_XP_SEN | \
97 + S3C2410_ADCTSC_AUTO_PST | \
98 + S3C2410_ADCTSC_XY_PST(0))
99
100 #define DEBUG_LVL KERN_DEBUG
101
102 @@ -85,17 +93,46 @@ static char *s3c2410ts_name = "s3c2410 TouchScreen";
103 * Per-touchscreen data.
104 */
105
106 +struct s3c2410ts_sample {
107 + int x;
108 + int y;
109 +};
110 +
111 struct s3c2410ts {
112 struct input_dev *dev;
113 long xp;
114 long yp;
115 int count;
116 int shift;
117 + int extent; /* 1 << shift */
118 +
119 + /* the raw sample fifo is a lightweight way to track a running average
120 + * of all taken samples. "running average" here means that it gives
121 + * correct average for each sample, not only at the end of block of
122 + * samples
123 + */
124 + int excursion_filter_len;
125 + struct s3c2410ts_sample *raw_sample_fifo;
126 + int head_raw_fifo;
127 + int tail_raw_fifo;
128 + struct s3c2410ts_sample raw_running_avg;
129 + int reject_threshold_vs_avg;
130 + int flag_previous_exceeded_threshold;
131 };
132
133 static struct s3c2410ts ts;
134 static void __iomem *base_addr;
135
136 +static void clear_raw_fifo(void)
137 +{
138 + ts.head_raw_fifo = 0;
139 + ts.tail_raw_fifo = 0;
140 + ts.raw_running_avg.x = 0;
141 + ts.raw_running_avg.y = 0;
142 + ts.flag_previous_exceeded_threshold = 0;
143 +}
144 +
145 +
146 static inline void s3c2410_ts_connect(void)
147 {
148 s3c2410_gpio_cfgpin(S3C2410_GPG12, S3C2410_GPG12_XMON);
149 @@ -110,47 +147,52 @@ static void touch_timer_fire(unsigned long data)
150 unsigned long data1;
151 int updown;
152
153 - data0 = readl(base_addr+S3C2410_ADCDAT0);
154 - data1 = readl(base_addr+S3C2410_ADCDAT1);
155 + data0 = readl(base_addr + S3C2410_ADCDAT0);
156 + data1 = readl(base_addr + S3C2410_ADCDAT1);
157
158 - updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
159 + updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) &&
160 + (!(data1 & S3C2410_ADCDAT0_UPDOWN));
161
162 - if (updown) {
163 - if (ts.count != 0) {
164 - ts.xp >>= ts.shift;
165 - ts.yp >>= ts.shift;
166 + if (updown) {
167 + if (ts.count != 0) {
168 + ts.xp >>= ts.shift;
169 + ts.yp >>= ts.shift;
170
171 #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
172 - {
173 - struct timeval tv;
174 - do_gettimeofday(&tv);
175 - printk(DEBUG_LVL "T: %06d, X: %03ld, Y: %03ld\n", (int)tv.tv_usec, ts.xp, ts.yp);
176 - }
177 + {
178 + struct timeval tv;
179 +
180 + do_gettimeofday(&tv);
181 + printk(DEBUG_LVL "T:%06d, X:%03ld, Y:%03ld\n",
182 + (int)tv.tv_usec, ts.xp, ts.yp);
183 + }
184 #endif
185
186 - input_report_abs(ts.dev, ABS_X, ts.xp);
187 - input_report_abs(ts.dev, ABS_Y, ts.yp);
188 + input_report_abs(ts.dev, ABS_X, ts.xp);
189 + input_report_abs(ts.dev, ABS_Y, ts.yp);
190
191 - input_report_key(ts.dev, BTN_TOUCH, 1);
192 - input_report_abs(ts.dev, ABS_PRESSURE, 1);
193 - input_sync(ts.dev);
194 - }
195 + input_report_key(ts.dev, BTN_TOUCH, 1);
196 + input_report_abs(ts.dev, ABS_PRESSURE, 1);
197 + input_sync(ts.dev);
198 + }
199
200 - ts.xp = 0;
201 - ts.yp = 0;
202 - ts.count = 0;
203 + ts.xp = 0;
204 + ts.yp = 0;
205 + ts.count = 0;
206
207 - writel(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST, base_addr+S3C2410_ADCTSC);
208 - writel(readl(base_addr+S3C2410_ADCCON) | S3C2410_ADCCON_ENABLE_START, base_addr+S3C2410_ADCCON);
209 - } else {
210 - ts.count = 0;
211 + writel(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST,
212 + base_addr+S3C2410_ADCTSC);
213 + writel(readl(base_addr+S3C2410_ADCCON) |
214 + S3C2410_ADCCON_ENABLE_START, base_addr+S3C2410_ADCCON);
215 + } else {
216 + ts.count = 0;
217
218 - input_report_key(ts.dev, BTN_TOUCH, 0);
219 - input_report_abs(ts.dev, ABS_PRESSURE, 0);
220 - input_sync(ts.dev);
221 + input_report_key(ts.dev, BTN_TOUCH, 0);
222 + input_report_abs(ts.dev, ABS_PRESSURE, 0);
223 + input_sync(ts.dev);
224
225 - writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
226 - }
227 + writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
228 + }
229 }
230
231 static struct timer_list touch_timer =
232 @@ -165,7 +207,8 @@ static irqreturn_t stylus_updown(int irq, void *dev_id)
233 data0 = readl(base_addr+S3C2410_ADCDAT0);
234 data1 = readl(base_addr+S3C2410_ADCDAT1);
235
236 - updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
237 + updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) &&
238 + (!(data1 & S3C2410_ADCDAT0_UPDOWN));
239
240 /* TODO we should never get an interrupt with updown set while
241 * the timer is running, but maybe we ought to verify that the
242 @@ -180,24 +223,94 @@ static irqreturn_t stylus_updown(int irq, void *dev_id)
243
244 static irqreturn_t stylus_action(int irq, void *dev_id)
245 {
246 - unsigned long data0;
247 - unsigned long data1;
248 -
249 - data0 = readl(base_addr+S3C2410_ADCDAT0);
250 - data1 = readl(base_addr+S3C2410_ADCDAT1);
251 + unsigned long x;
252 + unsigned long y;
253 + int length = (ts.head_raw_fifo - ts.tail_raw_fifo) & (ts.extent - 1);
254 + int scaled_avg_x = ts.raw_running_avg.x / length;
255 + int scaled_avg_y = ts.raw_running_avg.y / length;
256 +
257 + x = readl(base_addr + S3C2410_ADCDAT0) & S3C2410_ADCDAT0_XPDATA_MASK;
258 + y = readl(base_addr + S3C2410_ADCDAT1) & S3C2410_ADCDAT1_YPDATA_MASK;
259 +
260 + /* we appear to accept every sample into both the running average FIFO
261 + * and the summing average. BUT, if the last sample crossed a
262 + * machine-set threshold, each time we do a beauty contest
263 + * on the new sample comparing if it is closer to the running
264 + * average and the previous sample. If it is closer to the previous
265 + * suspicious sample, we assume the change is real and accept both
266 + * if the new sample has returned to being closer to the average than
267 + * the previous sample, we take the previous sample as an excursion
268 + * and overwrite it in both the running average and summing average.
269 + */
270 +
271 + if (ts.flag_previous_exceeded_threshold)
272 + /* new one closer to "nonconformist" previous, or average?
273 + * Pythagoras? Who? Don't need it because large excursion
274 + * will be accounted for correctly this way
275 + */
276 + if ((abs(x - scaled_avg_x) + abs(y - scaled_avg_y)) <
277 + (abs(x - ts.raw_sample_fifo[(ts.head_raw_fifo - 1) &
278 + (ts.extent - 1)].x) +
279 + abs(y - ts.raw_sample_fifo[(ts.head_raw_fifo - 1) &
280 + (ts.extent - 1)].y))) {
281 + /* it's closer to average, reject previous as a one-
282 + * shot excursion, by overwriting it
283 + */
284 + ts.xp += x - ts.raw_sample_fifo[(ts.head_raw_fifo - 1) &
285 + (ts.extent - 1)].x;
286 + ts.yp += y - ts.raw_sample_fifo[(ts.head_raw_fifo - 1) &
287 + (ts.extent - 1)].y;
288 + ts.raw_sample_fifo[(ts.head_raw_fifo - 1) &
289 + (ts.extent - 1)].x = x;
290 + ts.raw_sample_fifo[(ts.head_raw_fifo - 1) &
291 + (ts.extent - 1)].y = y;
292 + /* no new sample: replaced previous, so we are done */
293 + goto completed;
294 + }
295 + /* else it was closer to nonconformist previous: it's likely
296 + * a genuine consistent move then.
297 + * Keep previous and add new guy.
298 + */
299 +
300 + if ((x >= scaled_avg_x - ts.reject_threshold_vs_avg) &&
301 + (x <= scaled_avg_x + ts.reject_threshold_vs_avg) &&
302 + (y >= scaled_avg_y - ts.reject_threshold_vs_avg) &&
303 + (y <= scaled_avg_y + ts.reject_threshold_vs_avg))
304 + ts.flag_previous_exceeded_threshold = 0;
305 + else
306 + ts.flag_previous_exceeded_threshold = 1;
307
308 - ts.xp += data0 & S3C2410_ADCDAT0_XPDATA_MASK;
309 - ts.yp += data1 & S3C2410_ADCDAT1_YPDATA_MASK;
310 + /* accepted */
311 + ts.xp += x;
312 + ts.yp += y;
313 ts.count++;
314
315 - if (ts.count < (1<<ts.shift)) {
316 - writel(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST, base_addr+S3C2410_ADCTSC);
317 - writel(readl(base_addr+S3C2410_ADCCON) | S3C2410_ADCCON_ENABLE_START, base_addr+S3C2410_ADCCON);
318 - } else {
319 - mod_timer(&touch_timer, jiffies+1);
320 + /* remove oldest sample from avg when we have full pipeline */
321 + if (((ts.head_raw_fifo + 1) & (ts.extent - 1)) == ts.tail_raw_fifo) {
322 + ts.raw_running_avg.x -= ts.raw_sample_fifo[ts.tail_raw_fifo].x;
323 + ts.raw_running_avg.y -= ts.raw_sample_fifo[ts.tail_raw_fifo].y;
324 + ts.tail_raw_fifo = (ts.tail_raw_fifo + 1) & (ts.extent - 1);
325 + }
326 + /* always add current sample to fifo and average */
327 + ts.raw_sample_fifo[ts.head_raw_fifo].x = x;
328 + ts.raw_sample_fifo[ts.head_raw_fifo].y = y;
329 + ts.raw_running_avg.x += x;
330 + ts.raw_running_avg.y += y;
331 + ts.head_raw_fifo = (ts.head_raw_fifo + 1) & (ts.extent - 1);
332 +
333 +completed:
334 + if (ts.count >= (1 << ts.shift)) {
335 + mod_timer(&touch_timer, jiffies + 1);
336 writel(WAIT4INT(1), base_addr+S3C2410_ADCTSC);
337 + goto bail;
338 }
339
340 + writel(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST,
341 + base_addr+S3C2410_ADCTSC);
342 + writel(readl(base_addr+S3C2410_ADCCON) |
343 + S3C2410_ADCCON_ENABLE_START, base_addr+S3C2410_ADCCON);
344 +
345 +bail:
346 return IRQ_HANDLED;
347 }
348
349 @@ -213,11 +326,11 @@ static int __init s3c2410ts_probe(struct platform_device *pdev)
350 struct s3c2410_ts_mach_info *info;
351 struct input_dev *input_dev;
352
353 - info = ( struct s3c2410_ts_mach_info *)pdev->dev.platform_data;
354 + info = (struct s3c2410_ts_mach_info *)pdev->dev.platform_data;
355
356 if (!info)
357 {
358 - printk(KERN_ERR "Hm... too bad : no platform data for ts\n");
359 + dev_err(&pdev->dev, "Hm... too bad: no platform data for ts\n");
360 return -EINVAL;
361 }
362
363 @@ -227,7 +340,7 @@ static int __init s3c2410ts_probe(struct platform_device *pdev)
364
365 adc_clock = clk_get(NULL, "adc");
366 if (!adc_clock) {
367 - printk(KERN_ERR "failed to get adc clock source\n");
368 + dev_err(&pdev->dev, "failed to get adc clock source\n");
369 return -ENOENT;
370 }
371 clk_enable(adc_clock);
372 @@ -238,7 +351,7 @@ static int __init s3c2410ts_probe(struct platform_device *pdev)
373
374 base_addr = ioremap(S3C2410_PA_ADC,0x20);
375 if (base_addr == NULL) {
376 - printk(KERN_ERR "Failed to remap register block\n");
377 + dev_err(&pdev->dev, "Failed to remap register block\n");
378 return -ENOMEM;
379 }
380
381 @@ -247,25 +360,26 @@ static int __init s3c2410ts_probe(struct platform_device *pdev)
382 if (!strcmp(pdev->name, "s3c2410-ts"))
383 s3c2410_ts_connect();
384
385 - if ((info->presc&0xff) > 0)
386 - writel(S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(info->presc&0xFF),\
387 - base_addr+S3C2410_ADCCON);
388 + if ((info->presc & 0xff) > 0)
389 + writel(S3C2410_ADCCON_PRSCEN |
390 + S3C2410_ADCCON_PRSCVL(info->presc&0xFF),
391 + base_addr + S3C2410_ADCCON);
392 else
393 - writel(0,base_addr+S3C2410_ADCCON);
394 + writel(0, base_addr+S3C2410_ADCCON);
395
396
397 /* Initialise registers */
398 - if ((info->delay&0xffff) > 0)
399 - writel(info->delay & 0xffff, base_addr+S3C2410_ADCDLY);
400 + if ((info->delay & 0xffff) > 0)
401 + writel(info->delay & 0xffff, base_addr + S3C2410_ADCDLY);
402
403 - writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
404 + writel(WAIT4INT(0), base_addr + S3C2410_ADCTSC);
405
406 /* Initialise input stuff */
407 memset(&ts, 0, sizeof(struct s3c2410ts));
408 input_dev = input_allocate_device();
409
410 if (!input_dev) {
411 - printk(KERN_ERR "Unable to allocate the input device !!\n");
412 + dev_err(&pdev->dev, "Unable to allocate the input device\n");
413 return -ENOMEM;
414 }
415
416 @@ -284,23 +398,30 @@ static int __init s3c2410ts_probe(struct platform_device *pdev)
417 ts.dev->id.version = S3C2410TSVERSION;
418
419 ts.shift = info->oversampling_shift;
420 + ts.extent = 1 << info->oversampling_shift;
421 + ts.reject_threshold_vs_avg = info->reject_threshold_vs_avg;
422 + ts.excursion_filter_len = 1 << info->excursion_filter_len_bits;
423 +
424 + ts.raw_sample_fifo = kmalloc(sizeof(struct s3c2410ts_sample) *
425 + ts.excursion_filter_len, GFP_KERNEL);
426 + clear_raw_fifo();
427
428 /* Get irqs */
429 if (request_irq(IRQ_ADC, stylus_action, IRQF_SAMPLE_RANDOM,
430 - "s3c2410_action", ts.dev)) {
431 - printk(KERN_ERR "s3c2410_ts.c: Could not allocate ts IRQ_ADC !\n");
432 + "s3c2410_action", ts.dev)) {
433 + dev_err(&pdev->dev, "Could not allocate ts IRQ_ADC !\n");
434 iounmap(base_addr);
435 return -EIO;
436 }
437 if (request_irq(IRQ_TC, stylus_updown, IRQF_SAMPLE_RANDOM,
438 "s3c2410_action", ts.dev)) {
439 - printk(KERN_ERR "s3c2410_ts.c: Could not allocate ts IRQ_TC !\n");
440 + dev_err(&pdev->dev, "Could not allocate ts IRQ_TC !\n");
441 free_irq(IRQ_ADC, ts.dev);
442 iounmap(base_addr);
443 return -EIO;
444 }
445
446 - printk(KERN_INFO "%s successfully loaded\n", s3c2410ts_name);
447 + dev_info(&pdev->dev, "successfully loaded\n");
448
449 /* All went ok, so register to the input system */
450 rc = input_register_device(ts.dev);
451 @@ -328,6 +449,8 @@ static int s3c2410ts_remove(struct platform_device *pdev)
452 adc_clock = NULL;
453 }
454
455 + kfree(ts.raw_sample_fifo);
456 +
457 input_unregister_device(ts.dev);
458 iounmap(base_addr);
459
460 @@ -357,17 +480,20 @@ static int s3c2410ts_resume(struct platform_device *pdev)
461 clk_enable(adc_clock);
462 mdelay(1);
463
464 + clear_raw_fifo();
465 +
466 enable_irq(IRQ_ADC);
467 enable_irq(IRQ_TC);
468
469 if ((info->presc&0xff) > 0)
470 - writel(S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(info->presc&0xFF),\
471 - base_addr+S3C2410_ADCCON);
472 + writel(S3C2410_ADCCON_PRSCEN |
473 + S3C2410_ADCCON_PRSCVL(info->presc&0xFF),
474 + base_addr+S3C2410_ADCCON);
475 else
476 writel(0,base_addr+S3C2410_ADCCON);
477
478 /* Initialise registers */
479 - if ((info->delay&0xffff) > 0)
480 + if ((info->delay & 0xffff) > 0)
481 writel(info->delay & 0xffff, base_addr+S3C2410_ADCDLY);
482
483 writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
484 diff --git a/include/asm-arm/arch-s3c2410/ts.h b/include/asm-arm/arch-s3c2410/ts.h
485 index 593632a..44c1e4b 100644
486 --- a/include/asm-arm/arch-s3c2410/ts.h
487 +++ b/include/asm-arm/arch-s3c2410/ts.h
488 @@ -17,9 +17,11 @@
489 #define __ASM_ARM_TS_H
490
491 struct s3c2410_ts_mach_info {
492 - int delay;
493 - int presc;
494 - int oversampling_shift;
495 + int delay;
496 + int presc;
497 + int oversampling_shift;
498 + int excursion_filter_len_bits;
499 + int reject_threshold_vs_avg;
500 };
501
502 void set_s3c2410ts_info(struct s3c2410_ts_mach_info *hard_s3c2410ts_info);
503 --
504 1.5.6.3
505