Skip to content

Commit da970e6

Browse files
Imre DeakDmitry Torokhov
authored andcommitted
Input: ads7846 - pluggable filtering logic
Some LCDs like the LS041Y3 require a customized filtering logic for reliable readings, so make the filtering function replacable through platform specific hooks. Signed-off-by: Imre Deak <imre.deak@solidboot.com> Signed-off-by: Juha Yrjola <juha.yrjola@solidboot.com> Signed-off-by: David Brownell <dbrownell@users.sourceforge.net> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
1 parent 78a56aa commit da970e6

2 files changed

Lines changed: 105 additions & 40 deletions

File tree

‎drivers/input/touchscreen/ads7846.c‎

Lines changed: 95 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ struct ts_event {
6363
/* For portability, we can't read 12 bit values using SPI (which
6464
* would make the controller deliver them as native byteorder u16
6565
* with msbs zeroed). Instead, we read them as two 8-bit values,
66-
* which need byteswapping then range adjustment.
66+
* *** WHICH NEED BYTESWAPPING *** and range adjustment.
6767
*/
68-
__be16 x;
69-
__be16 y;
70-
__be16 z1, z2;
68+
u16 x;
69+
u16 y;
70+
u16 z1, z2;
7171
int ignore;
7272
};
7373

@@ -106,6 +106,9 @@ struct ads7846 {
106106
unsigned irq_disabled:1; /* P: lock */
107107
unsigned disabled:1;
108108

109+
int (*filter)(void *data, int data_idx, int *val);
110+
void *filter_data;
111+
void (*filter_cleanup)(void *data);
109112
int (*get_pendown_state)(void);
110113
};
111114

@@ -379,13 +382,13 @@ static void ads7846_rx(void *ads)
379382
u16 x, y, z1, z2;
380383
unsigned long flags;
381384

382-
/* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
383-
* built from two 8 bit values written msb-first.
385+
/* ads7846_rx_val() did in-place conversion (including byteswap) from
386+
* on-the-wire format as part of debouncing to get stable readings.
384387
*/
385-
x = (be16_to_cpu(ts->tc.x) >> 3) & 0x0fff;
386-
y = (be16_to_cpu(ts->tc.y) >> 3) & 0x0fff;
387-
z1 = (be16_to_cpu(ts->tc.z1) >> 3) & 0x0fff;
388-
z2 = (be16_to_cpu(ts->tc.z2) >> 3) & 0x0fff;
388+
x = ts->tc.x;
389+
y = ts->tc.y;
390+
z1 = ts->tc.z1;
391+
z2 = ts->tc.z2;
389392

390393
/* range filtering */
391394
if (x == MAX_12BIT)
@@ -453,50 +456,85 @@ static void ads7846_rx(void *ads)
453456
spin_unlock_irqrestore(&ts->lock, flags);
454457
}
455458

456-
static void ads7846_debounce(void *ads)
459+
static int ads7846_debounce(void *ads, int data_idx, int *val)
457460
{
458461
struct ads7846 *ts = ads;
459-
struct spi_message *m;
460-
struct spi_transfer *t;
461-
int val;
462-
int status;
463462

464-
m = &ts->msg[ts->msg_idx];
465-
t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
466-
val = (be16_to_cpu(*(__be16 *)t->rx_buf) >> 3) & 0x0fff;
467-
if (!ts->read_cnt || (abs(ts->last_read - val) > ts->debounce_tol)) {
463+
if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
464+
/* Start over collecting consistent readings. */
465+
ts->read_rep = 0;
468466
/* Repeat it, if this was the first read or the read
469467
* wasn't consistent enough. */
470468
if (ts->read_cnt < ts->debounce_max) {
471-
ts->last_read = val;
469+
ts->last_read = *val;
472470
ts->read_cnt++;
471+
return ADS7846_FILTER_REPEAT;
473472
} else {
474473
/* Maximum number of debouncing reached and still
475474
* not enough number of consistent readings. Abort
476475
* the whole sample, repeat it in the next sampling
477476
* period.
478477
*/
479-
ts->tc.ignore = 1;
480478
ts->read_cnt = 0;
481-
/* Last message will contain ads7846_rx() as the
482-
* completion function.
483-
*/
484-
m = ts->last_msg;
479+
return ADS7846_FILTER_IGNORE;
485480
}
486-
/* Start over collecting consistent readings. */
487-
ts->read_rep = 0;
488481
} else {
489482
if (++ts->read_rep > ts->debounce_rep) {
490483
/* Got a good reading for this coordinate,
491484
* go for the next one. */
492-
ts->tc.ignore = 0;
493-
ts->msg_idx++;
494485
ts->read_cnt = 0;
495486
ts->read_rep = 0;
496-
m++;
497-
} else
487+
return ADS7846_FILTER_OK;
488+
} else {
498489
/* Read more values that are consistent. */
499490
ts->read_cnt++;
491+
return ADS7846_FILTER_REPEAT;
492+
}
493+
}
494+
}
495+
496+
static int ads7846_no_filter(void *ads, int data_idx, int *val)
497+
{
498+
return ADS7846_FILTER_OK;
499+
}
500+
501+
static void ads7846_rx_val(void *ads)
502+
{
503+
struct ads7846 *ts = ads;
504+
struct spi_message *m;
505+
struct spi_transfer *t;
506+
u16 *rx_val;
507+
int val;
508+
int action;
509+
int status;
510+
511+
m = &ts->msg[ts->msg_idx];
512+
t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
513+
rx_val = t->rx_buf;
514+
515+
/* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
516+
* built from two 8 bit values written msb-first.
517+
*/
518+
val = be16_to_cpu(*rx_val) >> 3;
519+
520+
action = ts->filter(ts->filter_data, ts->msg_idx, &val);
521+
switch (action) {
522+
case ADS7846_FILTER_REPEAT:
523+
break;
524+
case ADS7846_FILTER_IGNORE:
525+
ts->tc.ignore = 1;
526+
/* Last message will contain ads7846_rx() as the
527+
* completion function.
528+
*/
529+
m = ts->last_msg;
530+
break;
531+
case ADS7846_FILTER_OK:
532+
*rx_val = val;
533+
ts->tc.ignore = 0;
534+
m = &ts->msg[++ts->msg_idx];
535+
break;
536+
default:
537+
BUG();
500538
}
501539
status = spi_async(ts->spi, m);
502540
if (status)
@@ -689,14 +727,25 @@ static int __devinit ads7846_probe(struct spi_device *spi)
689727
ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
690728
ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
691729
ts->pressure_max = pdata->pressure_max ? : ~0;
692-
if (pdata->debounce_max) {
730+
731+
if (pdata->filter != NULL) {
732+
if (pdata->filter_init != NULL) {
733+
err = pdata->filter_init(pdata, &ts->filter_data);
734+
if (err < 0)
735+
goto err_free_mem;
736+
}
737+
ts->filter = pdata->filter;
738+
ts->filter_cleanup = pdata->filter_cleanup;
739+
} else if (pdata->debounce_max) {
693740
ts->debounce_max = pdata->debounce_max;
741+
if (ts->debounce_max < 2)
742+
ts->debounce_max = 2;
694743
ts->debounce_tol = pdata->debounce_tol;
695744
ts->debounce_rep = pdata->debounce_rep;
696-
if (ts->debounce_rep > ts->debounce_max + 1)
697-
ts->debounce_rep = ts->debounce_max - 1;
745+
ts->filter = ads7846_debounce;
746+
ts->filter_data = ts;
698747
} else
699-
ts->debounce_tol = ~0;
748+
ts->filter = ads7846_no_filter;
700749
ts->get_pendown_state = pdata->get_pendown_state;
701750

702751
snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
@@ -737,7 +786,7 @@ static int __devinit ads7846_probe(struct spi_device *spi)
737786
x->len = 2;
738787
spi_message_add_tail(x, m);
739788

740-
m->complete = ads7846_debounce;
789+
m->complete = ads7846_rx_val;
741790
m->context = ts;
742791

743792
m++;
@@ -755,7 +804,7 @@ static int __devinit ads7846_probe(struct spi_device *spi)
755804
x->len = 2;
756805
spi_message_add_tail(x, m);
757806

758-
m->complete = ads7846_debounce;
807+
m->complete = ads7846_rx_val;
759808
m->context = ts;
760809

761810
/* turn y+ off, x- on; we'll use formula #2 */
@@ -774,7 +823,7 @@ static int __devinit ads7846_probe(struct spi_device *spi)
774823
x->len = 2;
775824
spi_message_add_tail(x, m);
776825

777-
m->complete = ads7846_debounce;
826+
m->complete = ads7846_rx_val;
778827
m->context = ts;
779828

780829
m++;
@@ -791,7 +840,7 @@ static int __devinit ads7846_probe(struct spi_device *spi)
791840
x->len = 2;
792841
spi_message_add_tail(x, m);
793842

794-
m->complete = ads7846_debounce;
843+
m->complete = ads7846_rx_val;
795844
m->context = ts;
796845
}
797846

@@ -820,7 +869,7 @@ static int __devinit ads7846_probe(struct spi_device *spi)
820869
spi->dev.driver->name, ts)) {
821870
dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
822871
err = -EBUSY;
823-
goto err_free_mem;
872+
goto err_cleanup_filter;
824873
}
825874

826875
dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
@@ -856,6 +905,9 @@ static int __devinit ads7846_probe(struct spi_device *spi)
856905
sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
857906
err_free_irq:
858907
free_irq(spi->irq, ts);
908+
err_cleanup_filter:
909+
if (ts->filter_cleanup)
910+
ts->filter_cleanup(ts->filter_data);
859911
err_free_mem:
860912
input_free_device(input_dev);
861913
kfree(ts);
@@ -876,6 +928,9 @@ static int __devexit ads7846_remove(struct spi_device *spi)
876928
/* suspend left the IRQ disabled */
877929
enable_irq(ts->spi->irq);
878930

931+
if (ts->filter_cleanup)
932+
ts->filter_cleanup(ts->filter_data);
933+
879934
kfree(ts);
880935

881936
dev_dbg(&spi->dev, "unregistered touchscreen\n");

‎include/linux/spi/ads7846.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
*
66
* It's OK if the min/max values are zero.
77
*/
8+
enum ads7846_filter {
9+
ADS7846_FILTER_OK,
10+
ADS7846_FILTER_REPEAT,
11+
ADS7846_FILTER_IGNORE,
12+
};
13+
814
struct ads7846_platform_data {
915
u16 model; /* 7843, 7845, 7846. */
1016
u16 vref_delay_usecs; /* 0 for external vref; etc */
@@ -21,5 +27,9 @@ struct ads7846_platform_data {
2127
u16 debounce_rep; /* additional consecutive good readings
2228
* required after the first two */
2329
int (*get_pendown_state)(void);
30+
int (*filter_init) (struct ads7846_platform_data *pdata,
31+
void **filter_data);
32+
int (*filter) (void *filter_data, int data_idx, int *val);
33+
void (*filter_cleanup)(void *filter_data);
2434
};
2535

0 commit comments

Comments
 (0)