Skip to content

Commit d5b415c

Browse files
Imre DeakDmitry Torokhov
authored andcommitted
Input: ads7846 - improve filtering for thumb press accuracy
Providing more accurate coordinates for thumb press requires additional steps in the filtering logic: - Ignore samples found invalid by the debouncing logic, or the ones that have out of bound pressure value. - Add a parameter to repeat debouncing, so that more then two consecutive good readings are required for a valid sample. Signed-off-by: Imre Deak <imre.deak@nokia.com> Acked-by: Juha Yrjola <juha.yrjola@nokia.com> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
1 parent ae82d5a commit d5b415c

2 files changed

Lines changed: 61 additions & 17 deletions

File tree

‎drivers/input/touchscreen/ads7846.c‎

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ struct ts_event {
7171
__be16 x;
7272
__be16 y;
7373
__be16 z1, z2;
74+
int ignore;
7475
};
7576

7677
struct ads7846 {
@@ -81,19 +82,23 @@ struct ads7846 {
8182
u16 model;
8283
u16 vref_delay_usecs;
8384
u16 x_plate_ohms;
85+
u16 pressure_max;
8486

8587
u8 read_x, read_y, read_z1, read_z2, pwrdown;
8688
u16 dummy; /* for the pwrdown read */
8789
struct ts_event tc;
8890

8991
struct spi_transfer xfer[10];
9092
struct spi_message msg[5];
93+
struct spi_message *last_msg;
9194
int msg_idx;
9295
int read_cnt;
96+
int read_rep;
9397
int last_read;
9498

9599
u16 debounce_max;
96100
u16 debounce_tol;
101+
u16 debounce_rep;
97102

98103
spinlock_t lock;
99104
struct timer_list timer; /* P: lock */
@@ -354,6 +359,14 @@ static void ads7846_rx(void *ads)
354359
} else
355360
Rt = 0;
356361

362+
/* Sample found inconsistent by debouncing or pressure is beyond
363+
* the maximum. Don't report it to user space, repeat at least
364+
* once more the measurement */
365+
if (ts->tc.ignore || Rt > ts->pressure_max) {
366+
mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
367+
return;
368+
}
369+
357370
/* NOTE: "pendown" is inferred from pressure; we don't rely on
358371
* being able to check nPENIRQ status, or "friendly" trigger modes
359372
* (both-edges is much better than just-falling or low-level).
@@ -402,25 +415,45 @@ static void ads7846_debounce(void *ads)
402415
struct ads7846 *ts = ads;
403416
struct spi_message *m;
404417
struct spi_transfer *t;
405-
u16 val;
418+
int val;
406419
int status;
407420

408421
m = &ts->msg[ts->msg_idx];
409422
t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
410423
val = (*(u16 *)t->rx_buf) >> 3;
411-
412-
if (!ts->read_cnt || (abs(ts->last_read - val) > ts->debounce_tol
413-
&& ts->read_cnt < ts->debounce_max)) {
414-
/* Repeat it, if this was the first read or the read wasn't
415-
* consistent enough
416-
*/
417-
ts->read_cnt++;
418-
ts->last_read = val;
424+
if (!ts->read_cnt || (abs(ts->last_read - val) > ts->debounce_tol)) {
425+
/* Repeat it, if this was the first read or the read
426+
* wasn't consistent enough. */
427+
if (ts->read_cnt < ts->debounce_max) {
428+
ts->last_read = val;
429+
ts->read_cnt++;
430+
} else {
431+
/* Maximum number of debouncing reached and still
432+
* not enough number of consistent readings. Abort
433+
* the whole sample, repeat it in the next sampling
434+
* period.
435+
*/
436+
ts->tc.ignore = 1;
437+
ts->read_cnt = 0;
438+
/* Last message will contain ads7846_rx() as the
439+
* completion function.
440+
*/
441+
m = ts->last_msg;
442+
}
443+
/* Start over collecting consistent readings. */
444+
ts->read_rep = 0;
419445
} else {
420-
/* Go for the next read */
421-
ts->msg_idx++;
422-
ts->read_cnt = 0;
423-
m++;
446+
if (++ts->read_rep > ts->debounce_rep) {
447+
/* Got a good reading for this coordinate,
448+
* go for the next one. */
449+
ts->tc.ignore = 0;
450+
ts->msg_idx++;
451+
ts->read_cnt = 0;
452+
ts->read_rep = 0;
453+
m++;
454+
} else
455+
/* Read more values that are consistent. */
456+
ts->read_cnt++;
424457
}
425458
status = spi_async(ts->spi, m);
426459
if (status)
@@ -609,8 +642,15 @@ static int __devinit ads7846_probe(struct spi_device *spi)
609642
ts->model = pdata->model ? : 7846;
610643
ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
611644
ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
612-
ts->debounce_max = pdata->debounce_max ? : 1;
613-
ts->debounce_tol = pdata->debounce_tol ? : 10;
645+
ts->pressure_max = pdata->pressure_max ? : ~0;
646+
if (pdata->debounce_max) {
647+
ts->debounce_max = pdata->debounce_max;
648+
ts->debounce_tol = pdata->debounce_tol;
649+
ts->debounce_rep = pdata->debounce_rep;
650+
if (ts->debounce_rep > ts->debounce_max + 1)
651+
ts->debounce_rep = ts->debounce_max - 1;
652+
} else
653+
ts->debounce_tol = ~0;
614654
ts->get_pendown_state = pdata->get_pendown_state;
615655

616656
snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
@@ -728,6 +768,8 @@ static int __devinit ads7846_probe(struct spi_device *spi)
728768
m->complete = ads7846_rx;
729769
m->context = ts;
730770

771+
ts->last_msg = m;
772+
731773
if (request_irq(spi->irq, ads7846_irq,
732774
SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,
733775
spi->dev.bus_id, ts)) {

‎include/linux/spi/ads7846.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ struct ads7846_platform_data {
1515
u16 y_min, y_max;
1616
u16 pressure_min, pressure_max;
1717

18-
u16 debounce_max; /* max number of readings per sample */
18+
u16 debounce_max; /* max number of additional readings
19+
* per sample */
1920
u16 debounce_tol; /* tolerance used for filtering */
20-
21+
u16 debounce_rep; /* additional consecutive good readings
22+
* required after the first two */
2123
int (*get_pendown_state)(void);
2224
};
2325

0 commit comments

Comments
 (0)