1 From cf7096e6f6e24d8a362ad05f768d609a2921f479 Mon Sep 17 00:00:00 2001
2 From: Andy Green <andy@openmoko.com>
3 Date: Wed, 2 Jul 2008 22:43:31 +0100
4 Subject: [PATCH] touchscreen-meddling.patch
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.
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.
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.
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.
37 Signed-off-by: Andy Green <andy@openmoko.com>
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(-)
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 = {
50 static struct s3c2410_ts_mach_info gta02_ts_cfg = {
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
61 + .reject_threshold_vs_avg = 2,
65 /* SPI: LCM control interface attached to Glamo3362 */
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 6d395ca..83e7aff 100644
70 --- a/drivers/input/touchscreen/s3c2410_ts.c
71 +++ b/drivers/input/touchscreen/s3c2410_ts.c
74 * 2007-05-23: Harald Welte <laforge@openmoko.org>
75 * - Add proper support for S32440
77 + * 2008-06-18: Andy Green <andy@openmoko.com>
81 #include <linux/errno.h>
83 #define TSC_SLEEP (S3C2410_ADCTSC_PULL_UP_DISABLE | S3C2410_ADCTSC_XY_PST(0))
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))
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))
100 #define DEBUG_LVL KERN_DEBUG
102 @@ -85,17 +93,46 @@ static char *s3c2410ts_name = "s3c2410 TouchScreen";
103 * Per-touchscreen data.
106 +struct s3c2410ts_sample {
112 struct input_dev *dev;
117 + int extent; /* 1 << shift */
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
124 + int excursion_filter_len;
125 + struct s3c2410ts_sample *raw_sample_fifo;
128 + struct s3c2410ts_sample raw_running_avg;
129 + int reject_threshold_vs_avg;
130 + int flag_previous_exceeded_threshold;
133 static struct s3c2410ts ts;
134 static void __iomem *base_addr;
136 +static void clear_raw_fifo(void)
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;
146 static inline void s3c2410_ts_connect(void)
148 s3c2410_gpio_cfgpin(S3C2410_GPG12, S3C2410_GPG12_XMON);
149 @@ -110,47 +147,52 @@ static void touch_timer_fire(unsigned long data)
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);
158 - updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
159 + updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) &&
160 + (!(data1 & S3C2410_ADCDAT0_UPDOWN));
163 - if (ts.count != 0) {
164 - ts.xp >>= ts.shift;
165 - ts.yp >>= ts.shift;
167 + if (ts.count != 0) {
168 + ts.xp >>= ts.shift;
169 + ts.yp >>= ts.shift;
171 #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
174 - do_gettimeofday(&tv);
175 - printk(DEBUG_LVL "T: %06d, X: %03ld, Y: %03ld\n", (int)tv.tv_usec, ts.xp, ts.yp);
180 + do_gettimeofday(&tv);
181 + printk(DEBUG_LVL "T:%06d, X:%03ld, Y:%03ld\n",
182 + (int)tv.tv_usec, ts.xp, ts.yp);
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);
191 - input_report_key(ts.dev, BTN_TOUCH, 1);
192 - input_report_abs(ts.dev, ABS_PRESSURE, 1);
193 - input_sync(ts.dev);
195 + input_report_key(ts.dev, BTN_TOUCH, 1);
196 + input_report_abs(ts.dev, ABS_PRESSURE, 1);
197 + input_sync(ts.dev);
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);
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);
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);
225 - writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
227 + writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
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);
236 - updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
237 + updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) &&
238 + (!(data1 & S3C2410_ADCDAT0_UPDOWN));
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)
244 static irqreturn_t stylus_action(int irq, void *dev_id)
246 - unsigned long data0;
247 - unsigned long data1;
249 - data0 = readl(base_addr+S3C2410_ADCDAT0);
250 - data1 = readl(base_addr+S3C2410_ADCDAT1);
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;
257 + x = readl(base_addr + S3C2410_ADCDAT0) & S3C2410_ADCDAT0_XPDATA_MASK;
258 + y = readl(base_addr + S3C2410_ADCDAT1) & S3C2410_ADCDAT1_YPDATA_MASK;
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.
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
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
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 */
295 + /* else it was closer to nonconformist previous: it's likely
296 + * a genuine consistent move then.
297 + * Keep previous and add new guy.
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;
306 + ts.flag_previous_exceeded_threshold = 1;
308 - ts.xp += data0 & S3C2410_ADCDAT0_XPDATA_MASK;
309 - ts.yp += data1 & S3C2410_ADCDAT1_YPDATA_MASK;
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);
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);
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);
334 + if (ts.count >= (1 << ts.shift)) {
335 + mod_timer(&touch_timer, jiffies + 1);
336 writel(WAIT4INT(1), base_addr+S3C2410_ADCTSC);
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);
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;
353 - info = ( struct s3c2410_ts_mach_info *)pdev->dev.platform_data;
354 + info = (struct s3c2410_ts_mach_info *)pdev->dev.platform_data;
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");
363 @@ -227,7 +340,7 @@ static int __init s3c2410ts_probe(struct platform_device *pdev)
365 adc_clock = clk_get(NULL, "adc");
367 - printk(KERN_ERR "failed to get adc clock source\n");
368 + dev_err(&pdev->dev, "failed to get adc clock source\n");
371 clk_enable(adc_clock);
372 @@ -238,7 +351,7 @@ static int __init s3c2410ts_probe(struct platform_device *pdev)
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");
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();
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);
393 - writel(0,base_addr+S3C2410_ADCCON);
394 + writel(0, base_addr+S3C2410_ADCCON);
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);
403 - writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
404 + writel(WAIT4INT(0), base_addr + S3C2410_ADCTSC);
406 /* Initialise input stuff */
407 memset(&ts, 0, sizeof(struct s3c2410ts));
408 input_dev = input_allocate_device();
411 - printk(KERN_ERR "Unable to allocate the input device !!\n");
412 + dev_err(&pdev->dev, "Unable to allocate the input device\n");
416 @@ -285,23 +399,30 @@ static int __init s3c2410ts_probe(struct platform_device *pdev)
417 ts.dev->id.version = S3C2410TSVERSION;
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;
424 + ts.raw_sample_fifo = kmalloc(sizeof(struct s3c2410ts_sample) *
425 + ts.excursion_filter_len, GFP_KERNEL);
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");
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);
446 - printk(KERN_INFO "%s successfully loaded\n", s3c2410ts_name);
447 + dev_info(&pdev->dev, "successfully loaded\n");
449 /* All went ok, so register to the input system */
450 rc = input_register_device(ts.dev);
451 @@ -329,6 +450,8 @@ static int s3c2410ts_remove(struct platform_device *pdev)
455 + kfree(ts.raw_sample_fifo);
457 input_unregister_device(ts.dev);
460 @@ -358,17 +481,20 @@ static int s3c2410ts_resume(struct platform_device *pdev)
461 clk_enable(adc_clock);
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);
476 writel(0,base_addr+S3C2410_ADCCON);
478 /* Initialise registers */
479 - if ((info->delay&0xffff) > 0)
480 + if ((info->delay & 0xffff) > 0)
481 writel(info->delay & 0xffff, base_addr+S3C2410_ADCDLY);
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
489 #define __ASM_ARM_TS_H
491 struct s3c2410_ts_mach_info {
494 - int oversampling_shift;
497 + int oversampling_shift;
498 + int excursion_filter_len_bits;
499 + int reject_threshold_vs_avg;
502 void set_s3c2410ts_info(struct s3c2410_ts_mach_info *hard_s3c2410ts_info);