Skip to content

Commit c9e617a

Browse files
Imre DeakDmitry Torokhov
authored andcommitted
Input: ads7846 - handle IRQs that were latched during disabled IRQs
The pen down IRQ will toggle during each X,Y,Z measurement cycle. Even though the IRQ is disabled it will be latched and delivered when after enable_irq. Thus in the IRQ handler we must avoid starting a new measurement cycle when such an "unwanted" IRQ happens. Add a get_pendown_state platform function, which will probably determine this by reading the current GPIO level of the pen IRQ pin. Move the IRQ reenabling from the SPI RX function to the timer. After the last power down message the pen IRQ pin is still active for a while and get_pendown_state would report incorrectly a pen down state. When suspending we should check the ts->pending flag instead of ts->pendown, since the timer can be pending regardless of ts->pendown. Also if ts->pending is set we can be sure that the timer is running, so no need to rearm it. Similarly if ts->pending is not set we can be sure that the IRQ is enabled (and the timer is not). Signed-off-by: Imre Deak <imre.deak@nokia.com> Signed-off-by: David Brownell <dbrownell@users.sourceforge.net> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
1 parent 7de90a8 commit c9e617a

2 files changed

Lines changed: 50 additions & 34 deletions

File tree

‎drivers/input/touchscreen/ads7846.c‎

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ struct ads7846 {
102102
// FIXME remove "irq_disabled"
103103
unsigned irq_disabled:1; /* P: lock */
104104
unsigned disabled:1;
105+
106+
int (*get_pendown_state)(void);
105107
};
106108

107109
/* leave chip selected when we're done, for quicker re-select? */
@@ -175,6 +177,12 @@ struct ser_req {
175177
static void ads7846_enable(struct ads7846 *ts);
176178
static void ads7846_disable(struct ads7846 *ts);
177179

180+
static int device_suspended(struct device *dev)
181+
{
182+
struct ads7846 *ts = dev_get_drvdata(dev);
183+
return dev->power.power_state.event != PM_EVENT_ON || ts->disabled;
184+
}
185+
178186
static int ads7846_read12_ser(struct device *dev, unsigned command)
179187
{
180188
struct spi_device *spi = to_spi_device(dev);
@@ -227,8 +235,10 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)
227235
for (i = 0; i < 6; i++)
228236
spi_message_add_tail(&req->xfer[i], &req->msg);
229237

238+
ts->irq_disabled = 1;
230239
disable_irq(spi->irq);
231240
status = spi_sync(spi, &req->msg);
241+
ts->irq_disabled = 0;
232242
enable_irq(spi->irq);
233243

234244
if (req->msg.status)
@@ -333,7 +343,7 @@ static void ads7846_rx(void *ads)
333343
if (x == MAX_12BIT)
334344
x = 0;
335345

336-
if (x && z1 && ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
346+
if (likely(x && z1 && !device_suspended(&ts->spi->dev))) {
337347
/* compute touch pressure resistance using equation #2 */
338348
Rt = z2;
339349
Rt -= z1;
@@ -377,20 +387,10 @@ static void ads7846_rx(void *ads)
377387
x, y, Rt, Rt ? "" : " UP");
378388
#endif
379389

380-
/* don't retrigger while we're suspended */
381390
spin_lock_irqsave(&ts->lock, flags);
382391

383392
ts->pendown = (Rt != 0);
384-
ts->pending = 0;
385-
386-
if (ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
387-
if (ts->pendown)
388-
mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
389-
else if (ts->irq_disabled) {
390-
ts->irq_disabled = 0;
391-
enable_irq(ts->spi->irq);
392-
}
393-
}
393+
mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
394394

395395
spin_unlock_irqrestore(&ts->lock, flags);
396396
}
@@ -431,10 +431,25 @@ static void ads7846_timer(unsigned long handle)
431431
struct ads7846 *ts = (void *)handle;
432432
int status = 0;
433433

434-
ts->msg_idx = 0;
435-
status = spi_async(ts->spi, &ts->msg[0]);
436-
if (status)
437-
dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
434+
spin_lock_irq(&ts->lock);
435+
436+
if (unlikely(ts->msg_idx && !ts->pendown)) {
437+
/* measurment cycle ended */
438+
if (!device_suspended(&ts->spi->dev)) {
439+
ts->irq_disabled = 0;
440+
enable_irq(ts->spi->irq);
441+
}
442+
ts->pending = 0;
443+
ts->msg_idx = 0;
444+
} else {
445+
/* pen is still down, continue with the measurement */
446+
ts->msg_idx = 0;
447+
status = spi_async(ts->spi, &ts->msg[0]);
448+
if (status)
449+
dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
450+
}
451+
452+
spin_unlock_irq(&ts->lock);
438453
}
439454

440455
static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
@@ -443,7 +458,7 @@ static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
443458
unsigned long flags;
444459

445460
spin_lock_irqsave(&ts->lock, flags);
446-
if (likely(!ts->irq_disabled && !ts->disabled)) {
461+
if (likely(ts->get_pendown_state())) {
447462
if (!ts->irq_disabled) {
448463
/* REVISIT irq logic for many ARM chips has cloned a
449464
* bug wherein disabling an irq in its handler won't
@@ -452,10 +467,7 @@ static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
452467
* that state here.
453468
*/
454469
ts->irq_disabled = 1;
455-
456470
disable_irq(ts->spi->irq);
457-
}
458-
if (!ts->pending) {
459471
ts->pending = 1;
460472
mod_timer(&ts->timer, jiffies);
461473
}
@@ -473,20 +485,17 @@ static void ads7846_disable(struct ads7846 *ts)
473485
if (ts->disabled)
474486
return;
475487

488+
ts->disabled = 1;
489+
476490
/* are we waiting for IRQ, or polling? */
477-
if (!ts->pendown) {
478-
if (!ts->irq_disabled) {
479-
ts->irq_disabled = 1;
480-
disable_irq(ts->spi->irq);
481-
}
491+
if (!ts->pending) {
492+
ts->irq_disabled = 1;
493+
disable_irq(ts->spi->irq);
482494
} else {
483-
/* polling; force a final SPI completion;
484-
* that will clean things up neatly
495+
/* the timer will run at least once more, and
496+
* leave everything in a clean state, IRQ disabled
485497
*/
486-
if (!ts->pending)
487-
mod_timer(&ts->timer, jiffies);
488-
489-
while (ts->pendown || ts->pending) {
498+
while (ts->pending) {
490499
spin_unlock_irq(&ts->lock);
491500
msleep(1);
492501
spin_lock_irq(&ts->lock);
@@ -497,7 +506,6 @@ static void ads7846_disable(struct ads7846 *ts)
497506
* leave it that way after every request
498507
*/
499508

500-
ts->disabled = 1;
501509
}
502510

503511
/* Must be called with ts->lock held */
@@ -566,6 +574,11 @@ static int __devinit ads7846_probe(struct spi_device *spi)
566574
return -EINVAL;
567575
}
568576

577+
if (pdata->get_pendown_state == NULL) {
578+
dev_dbg(&spi->dev, "no get_pendown_state function?\n");
579+
return -EINVAL;
580+
}
581+
569582
/* We'd set the wordsize to 12 bits ... except that some controllers
570583
* will then treat the 8 bit command words as 12 bits (and drop the
571584
* four MSBs of the 12 bit result). Result: inputs must be shifted
@@ -596,6 +609,7 @@ static int __devinit ads7846_probe(struct spi_device *spi)
596609
ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
597610
ts->debounce_max = pdata->debounce_max ? : 1;
598611
ts->debounce_tol = pdata->debounce_tol ? : 10;
612+
ts->get_pendown_state = pdata->get_pendown_state;
599613

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

@@ -786,8 +800,8 @@ static int __devexit ads7846_remove(struct spi_device *spi)
786800
device_remove_file(&spi->dev, &dev_attr_vaux);
787801

788802
free_irq(ts->spi->irq, ts);
789-
if (ts->irq_disabled)
790-
enable_irq(ts->spi->irq);
803+
/* suspend left the IRQ disabled */
804+
enable_irq(ts->spi->irq);
791805

792806
kfree(ts);
793807

‎include/linux/spi/ads7846.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ struct ads7846_platform_data {
1717

1818
u16 debounce_max; /* max number of readings per sample */
1919
u16 debounce_tol; /* tolerance used for filtering */
20+
21+
int (*get_pendown_state)(void);
2022
};
2123

0 commit comments

Comments
 (0)