Skip to content

Commit 0b7018a

Browse files
Imre DeakDmitry Torokhov
authored andcommitted
Input: ads7846 - debouncing and rudimentary sample filtering
Some touchscreens seem to oscillate heavily for a while after touching the screen.  Implement support for sampling the screen until we get two consecutive values that are close enough. Signed-off-by: Imre Deak <imre.deak@nokia.com> Signed-off-by: Juha Yrjola <juha.yrjola@nokia.com> Signed-off-by: David Brownell <dbrownell@users.sourceforge.net> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
1 parent 53a0ef8 commit 0b7018a

2 files changed

Lines changed: 121 additions & 38 deletions

File tree

‎drivers/input/touchscreen/ads7846.c‎

Lines changed: 118 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,13 @@ struct ads7846 {
7777
struct ts_event tc;
7878

7979
struct spi_transfer xfer[10];
80-
struct spi_message msg;
80+
struct spi_message msg[5];
81+
int msg_idx;
82+
int read_cnt;
83+
int last_read;
84+
85+
u16 debounce_max;
86+
u16 debounce_tol;
8187

8288
spinlock_t lock;
8389
struct timer_list timer; /* P: lock */
@@ -167,7 +173,7 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)
167173
if (!req)
168174
return -ENOMEM;
169175

170-
INIT_LIST_HEAD(&req->msg.transfers);
176+
spi_message_init(&req->msg);
171177

172178
/* activate reference, so it has time to settle; */
173179
req->ref_on = REF_ON;
@@ -344,31 +350,76 @@ static void ads7846_rx(void *ads)
344350
spin_unlock_irqrestore(&ts->lock, flags);
345351
}
346352

353+
static void ads7846_debounce(void *ads)
354+
{
355+
struct ads7846 *ts = ads;
356+
struct spi_message *m;
357+
struct spi_transfer *t;
358+
u16 val;
359+
int status;
360+
361+
m = &ts->msg[ts->msg_idx];
362+
t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
363+
val = (*(u16 *)t->rx_buf) >> 3;
364+
365+
if (!ts->read_cnt || (abs(ts->last_read - val) > ts->debounce_tol
366+
&& ts->read_cnt < ts->debounce_max)) {
367+
/* Repeat it, if this was the first read or the read wasn't
368+
* consistent enough
369+
*/
370+
ts->read_cnt++;
371+
ts->last_read = val;
372+
} else {
373+
/* Go for the next read */
374+
ts->msg_idx++;
375+
ts->read_cnt = 0;
376+
m++;
377+
}
378+
status = spi_async(ts->spi, m);
379+
if (status)
380+
dev_err(&ts->spi->dev, "spi_async --> %d\n",
381+
status);
382+
}
383+
347384
static void ads7846_timer(unsigned long handle)
348385
{
349386
struct ads7846 *ts = (void *)handle;
350387
int status = 0;
351-
unsigned long flags;
388+
389+
ts->msg_idx = 0;
390+
status = spi_async(ts->spi, &ts->msg[0]);
391+
if (status)
392+
dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
393+
}
394+
395+
static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
396+
{
397+
struct ads7846 *ts = handle;
398+
unsigned long flags;
399+
int r = IRQ_HANDLED;
352400

353401
spin_lock_irqsave(&ts->lock, flags);
354-
if (!ts->pending) {
355-
ts->pending = 1;
402+
if (ts->irq_disabled)
403+
r = IRQ_HANDLED;
404+
else {
356405
if (!ts->irq_disabled) {
406+
/* REVISIT irq logic for many ARM chips has cloned a
407+
* bug wherein disabling an irq in its handler won't
408+
* work;(it's disabled lazily, and too late to work.
409+
* until all their irq logic is fixed, we must shadow
410+
* that state here.
411+
*/
357412
ts->irq_disabled = 1;
413+
358414
disable_irq(ts->spi->irq);
359415
}
360-
status = spi_async(ts->spi, &ts->msg);
361-
if (status)
362-
dev_err(&ts->spi->dev, "spi_async --> %d\n",
363-
status);
416+
if (!ts->pending) {
417+
ts->pending = 1;
418+
mod_timer(&ts->timer, jiffies);
419+
}
364420
}
365421
spin_unlock_irqrestore(&ts->lock, flags);
366-
}
367-
368-
static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
369-
{
370-
ads7846_timer((unsigned long) handle);
371-
return IRQ_HANDLED;
422+
return r;
372423
}
373424

374425
/*--------------------------------------------------------------------------*/
@@ -426,6 +477,7 @@ static int __devinit ads7846_probe(struct spi_device *spi)
426477
struct ads7846 *ts;
427478
struct input_dev *input_dev;
428479
struct ads7846_platform_data *pdata = spi->dev.platform_data;
480+
struct spi_message *m;
429481
struct spi_transfer *x;
430482
int err;
431483

@@ -472,6 +524,8 @@ static int __devinit ads7846_probe(struct spi_device *spi)
472524
ts->model = pdata->model ? : 7846;
473525
ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
474526
ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
527+
ts->debounce_max = pdata->debounce_max ? : 1;
528+
ts->debounce_tol = pdata->debounce_tol ? : 10;
475529

476530
snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
477531

@@ -495,72 +549,98 @@ static int __devinit ads7846_probe(struct spi_device *spi)
495549
/* set up the transfers to read touchscreen state; this assumes we
496550
* use formula #2 for pressure, not #3.
497551
*/
498-
INIT_LIST_HEAD(&ts->msg.transfers);
552+
m = &ts->msg[0];
499553
x = ts->xfer;
500554

555+
spi_message_init(m);
556+
501557
/* y- still on; turn on only y+ (and ADC) */
502558
ts->read_y = READ_Y;
503559
x->tx_buf = &ts->read_y;
504560
x->len = 1;
505-
spi_message_add_tail(x, &ts->msg);
561+
spi_message_add_tail(x, m);
506562

507563
x++;
508564
x->rx_buf = &ts->tc.y;
509565
x->len = 2;
510-
spi_message_add_tail(x, &ts->msg);
566+
spi_message_add_tail(x, m);
567+
568+
m->complete = ads7846_debounce;
569+
m->context = ts;
570+
571+
m++;
572+
spi_message_init(m);
573+
574+
/* turn y- off, x+ on, then leave in lowpower */
575+
x++;
576+
ts->read_x = READ_X;
577+
x->tx_buf = &ts->read_x;
578+
x->len = 1;
579+
spi_message_add_tail(x, m);
580+
581+
x++;
582+
x->rx_buf = &ts->tc.x;
583+
x->len = 2;
584+
spi_message_add_tail(x, m);
585+
586+
m->complete = ads7846_debounce;
587+
m->context = ts;
511588

512589
/* turn y+ off, x- on; we'll use formula #2 */
513590
if (ts->model == 7846) {
591+
m++;
592+
spi_message_init(m);
593+
514594
x++;
515595
ts->read_z1 = READ_Z1;
516596
x->tx_buf = &ts->read_z1;
517597
x->len = 1;
518-
spi_message_add_tail(x, &ts->msg);
598+
spi_message_add_tail(x, m);
519599

520600
x++;
521601
x->rx_buf = &ts->tc.z1;
522602
x->len = 2;
523-
spi_message_add_tail(x, &ts->msg);
603+
spi_message_add_tail(x, m);
604+
605+
m->complete = ads7846_debounce;
606+
m->context = ts;
607+
608+
m++;
609+
spi_message_init(m);
524610

525611
x++;
526612
ts->read_z2 = READ_Z2;
527613
x->tx_buf = &ts->read_z2;
528614
x->len = 1;
529-
spi_message_add_tail(x, &ts->msg);
615+
spi_message_add_tail(x, m);
530616

531617
x++;
532618
x->rx_buf = &ts->tc.z2;
533619
x->len = 2;
534-
spi_message_add_tail(x, &ts->msg);
535-
}
620+
spi_message_add_tail(x, m);
536621

537-
/* turn y- off, x+ on, then leave in lowpower */
538-
x++;
539-
ts->read_x = READ_X;
540-
x->tx_buf = &ts->read_x;
541-
x->len = 1;
542-
spi_message_add_tail(x, &ts->msg);
543-
544-
x++;
545-
x->rx_buf = &ts->tc.x;
546-
x->len = 2;
547-
spi_message_add_tail(x, &ts->msg);
622+
m->complete = ads7846_debounce;
623+
m->context = ts;
624+
}
548625

549626
/* power down */
627+
m++;
628+
spi_message_init(m);
629+
550630
x++;
551631
ts->pwrdown = PWRDOWN;
552632
x->tx_buf = &ts->pwrdown;
553633
x->len = 1;
554-
spi_message_add_tail(x, &ts->msg);
634+
spi_message_add_tail(x, m);
555635

556636
x++;
557637
x->rx_buf = &ts->dummy;
558638
x->len = 2;
559639
CS_CHANGE(*x);
560-
spi_message_add_tail(x, &ts->msg);
640+
spi_message_add_tail(x, m);
561641

562-
ts->msg.complete = ads7846_rx;
563-
ts->msg.context = ts;
642+
m->complete = ads7846_rx;
643+
m->context = ts;
564644

565645
if (request_irq(spi->irq, ads7846_irq,
566646
SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,

‎include/linux/spi/ads7846.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ struct ads7846_platform_data {
1414
u16 x_min, x_max;
1515
u16 y_min, y_max;
1616
u16 pressure_min, pressure_max;
17+
18+
u16 debounce_max; /* max number of readings per sample */
19+
u16 debounce_tol; /* tolerance used for filtering */
1720
};
1821

0 commit comments

Comments
 (0)