diff options
Diffstat (limited to 'drivers/input/touchscreen/ads7846.c')
-rw-r--r-- | drivers/input/touchscreen/ads7846.c | 445 |
1 files changed, 352 insertions, 93 deletions
diff --git a/drivers/input/touchscreen/ads7846.c b/drivers/input/touchscreen/ads7846.c index 46d1fec2cfd8..161afddd0f44 100644 --- a/drivers/input/touchscreen/ads7846.c +++ b/drivers/input/touchscreen/ads7846.c | |||
@@ -2,6 +2,8 @@ | |||
2 | * ADS7846 based touchscreen and sensor driver | 2 | * ADS7846 based touchscreen and sensor driver |
3 | * | 3 | * |
4 | * Copyright (c) 2005 David Brownell | 4 | * Copyright (c) 2005 David Brownell |
5 | * Copyright (c) 2006 Nokia Corporation | ||
6 | * Various changes: Imre Deak <imre.deak@nokia.com> | ||
5 | * | 7 | * |
6 | * Using code from: | 8 | * Using code from: |
7 | * - corgi_ts.c | 9 | * - corgi_ts.c |
@@ -34,17 +36,22 @@ | |||
34 | 36 | ||
35 | 37 | ||
36 | /* | 38 | /* |
37 | * This code has been lightly tested on an ads7846. | 39 | * This code has been heavily tested on a Nokia 770, and lightly |
40 | * tested on other ads7846 devices (OSK/Mistral, Lubbock). | ||
38 | * Support for ads7843 and ads7845 has only been stubbed in. | 41 | * Support for ads7843 and ads7845 has only been stubbed in. |
39 | * | 42 | * |
40 | * Not yet done: investigate the values reported. Are x/y/pressure | 43 | * IRQ handling needs a workaround because of a shortcoming in handling |
41 | * event values sane enough for X11? How accurate are the temperature | 44 | * edge triggered IRQs on some platforms like the OMAP1/2. These |
42 | * and voltage readings? (System-specific calibration should support | 45 | * platforms don't handle the ARM lazy IRQ disabling properly, thus we |
43 | * accuracy of 0.3 degrees C; otherwise it's 2.0 degrees.) | 46 | * have to maintain our own SW IRQ disabled status. This should be |
47 | * removed as soon as the affected platform's IRQ handling is fixed. | ||
44 | * | 48 | * |
45 | * app note sbaa036 talks in more detail about accurate sampling... | 49 | * app note sbaa036 talks in more detail about accurate sampling... |
46 | * that ought to help in situations like LCDs inducing noise (which | 50 | * that ought to help in situations like LCDs inducing noise (which |
47 | * can also be helped by using synch signals) and more generally. | 51 | * can also be helped by using synch signals) and more generally. |
52 | * This driver tries to utilize the measures described in the app | ||
53 | * note. The strength of filtering can be set in the board-* specific | ||
54 | * files. | ||
48 | */ | 55 | */ |
49 | 56 | ||
50 | #define TS_POLL_PERIOD msecs_to_jiffies(10) | 57 | #define TS_POLL_PERIOD msecs_to_jiffies(10) |
@@ -61,6 +68,7 @@ struct ts_event { | |||
61 | __be16 x; | 68 | __be16 x; |
62 | __be16 y; | 69 | __be16 y; |
63 | __be16 z1, z2; | 70 | __be16 z1, z2; |
71 | int ignore; | ||
64 | }; | 72 | }; |
65 | 73 | ||
66 | struct ads7846 { | 74 | struct ads7846 { |
@@ -71,12 +79,23 @@ struct ads7846 { | |||
71 | u16 model; | 79 | u16 model; |
72 | u16 vref_delay_usecs; | 80 | u16 vref_delay_usecs; |
73 | u16 x_plate_ohms; | 81 | u16 x_plate_ohms; |
82 | u16 pressure_max; | ||
74 | 83 | ||
75 | u8 read_x, read_y, read_z1, read_z2; | 84 | u8 read_x, read_y, read_z1, read_z2, pwrdown; |
85 | u16 dummy; /* for the pwrdown read */ | ||
76 | struct ts_event tc; | 86 | struct ts_event tc; |
77 | 87 | ||
78 | struct spi_transfer xfer[8]; | 88 | struct spi_transfer xfer[10]; |
79 | struct spi_message msg; | 89 | struct spi_message msg[5]; |
90 | struct spi_message *last_msg; | ||
91 | int msg_idx; | ||
92 | int read_cnt; | ||
93 | int read_rep; | ||
94 | int last_read; | ||
95 | |||
96 | u16 debounce_max; | ||
97 | u16 debounce_tol; | ||
98 | u16 debounce_rep; | ||
80 | 99 | ||
81 | spinlock_t lock; | 100 | spinlock_t lock; |
82 | struct timer_list timer; /* P: lock */ | 101 | struct timer_list timer; /* P: lock */ |
@@ -84,6 +103,9 @@ struct ads7846 { | |||
84 | unsigned pending:1; /* P: lock */ | 103 | unsigned pending:1; /* P: lock */ |
85 | // FIXME remove "irq_disabled" | 104 | // FIXME remove "irq_disabled" |
86 | unsigned irq_disabled:1; /* P: lock */ | 105 | unsigned irq_disabled:1; /* P: lock */ |
106 | unsigned disabled:1; | ||
107 | |||
108 | int (*get_pendown_state)(void); | ||
87 | }; | 109 | }; |
88 | 110 | ||
89 | /* leave chip selected when we're done, for quicker re-select? */ | 111 | /* leave chip selected when we're done, for quicker re-select? */ |
@@ -125,7 +147,9 @@ struct ads7846 { | |||
125 | #define READ_Y (READ_12BIT_DFR(y) | ADS_PD10_ADC_ON) | 147 | #define READ_Y (READ_12BIT_DFR(y) | ADS_PD10_ADC_ON) |
126 | #define READ_Z1 (READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON) | 148 | #define READ_Z1 (READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON) |
127 | #define READ_Z2 (READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON) | 149 | #define READ_Z2 (READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON) |
128 | #define READ_X (READ_12BIT_DFR(x) | ADS_PD10_PDOWN) /* LAST */ | 150 | |
151 | #define READ_X (READ_12BIT_DFR(x) | ADS_PD10_ADC_ON) | ||
152 | #define PWRDOWN (READ_12BIT_DFR(y) | ADS_PD10_PDOWN) /* LAST */ | ||
129 | 153 | ||
130 | /* single-ended samples need to first power up reference voltage; | 154 | /* single-ended samples need to first power up reference voltage; |
131 | * we leave both ADC and VREF powered | 155 | * we leave both ADC and VREF powered |
@@ -152,6 +176,15 @@ struct ser_req { | |||
152 | struct spi_transfer xfer[6]; | 176 | struct spi_transfer xfer[6]; |
153 | }; | 177 | }; |
154 | 178 | ||
179 | static void ads7846_enable(struct ads7846 *ts); | ||
180 | static void ads7846_disable(struct ads7846 *ts); | ||
181 | |||
182 | static int device_suspended(struct device *dev) | ||
183 | { | ||
184 | struct ads7846 *ts = dev_get_drvdata(dev); | ||
185 | return dev->power.power_state.event != PM_EVENT_ON || ts->disabled; | ||
186 | } | ||
187 | |||
155 | static int ads7846_read12_ser(struct device *dev, unsigned command) | 188 | static int ads7846_read12_ser(struct device *dev, unsigned command) |
156 | { | 189 | { |
157 | struct spi_device *spi = to_spi_device(dev); | 190 | struct spi_device *spi = to_spi_device(dev); |
@@ -164,7 +197,7 @@ static int ads7846_read12_ser(struct device *dev, unsigned command) | |||
164 | if (!req) | 197 | if (!req) |
165 | return -ENOMEM; | 198 | return -ENOMEM; |
166 | 199 | ||
167 | INIT_LIST_HEAD(&req->msg.transfers); | 200 | spi_message_init(&req->msg); |
168 | 201 | ||
169 | /* activate reference, so it has time to settle; */ | 202 | /* activate reference, so it has time to settle; */ |
170 | req->ref_on = REF_ON; | 203 | req->ref_on = REF_ON; |
@@ -204,16 +237,21 @@ static int ads7846_read12_ser(struct device *dev, unsigned command) | |||
204 | for (i = 0; i < 6; i++) | 237 | for (i = 0; i < 6; i++) |
205 | spi_message_add_tail(&req->xfer[i], &req->msg); | 238 | spi_message_add_tail(&req->xfer[i], &req->msg); |
206 | 239 | ||
240 | ts->irq_disabled = 1; | ||
207 | disable_irq(spi->irq); | 241 | disable_irq(spi->irq); |
208 | status = spi_sync(spi, &req->msg); | 242 | status = spi_sync(spi, &req->msg); |
243 | ts->irq_disabled = 0; | ||
209 | enable_irq(spi->irq); | 244 | enable_irq(spi->irq); |
210 | 245 | ||
211 | if (req->msg.status) | 246 | if (req->msg.status) |
212 | status = req->msg.status; | 247 | status = req->msg.status; |
248 | |||
249 | /* on-wire is a must-ignore bit, a BE12 value, then padding */ | ||
213 | sample = be16_to_cpu(req->sample); | 250 | sample = be16_to_cpu(req->sample); |
214 | sample = sample >> 4; | 251 | sample = sample >> 3; |
215 | kfree(req); | 252 | sample &= 0x0fff; |
216 | 253 | ||
254 | kfree(req); | ||
217 | return status ? status : sample; | 255 | return status ? status : sample; |
218 | } | 256 | } |
219 | 257 | ||
@@ -233,6 +271,52 @@ SHOW(temp1) | |||
233 | SHOW(vaux) | 271 | SHOW(vaux) |
234 | SHOW(vbatt) | 272 | SHOW(vbatt) |
235 | 273 | ||
274 | static int is_pen_down(struct device *dev) | ||
275 | { | ||
276 | struct ads7846 *ts = dev_get_drvdata(dev); | ||
277 | |||
278 | return ts->pendown; | ||
279 | } | ||
280 | |||
281 | static ssize_t ads7846_pen_down_show(struct device *dev, | ||
282 | struct device_attribute *attr, char *buf) | ||
283 | { | ||
284 | return sprintf(buf, "%u\n", is_pen_down(dev)); | ||
285 | } | ||
286 | |||
287 | static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL); | ||
288 | |||
289 | static ssize_t ads7846_disable_show(struct device *dev, | ||
290 | struct device_attribute *attr, char *buf) | ||
291 | { | ||
292 | struct ads7846 *ts = dev_get_drvdata(dev); | ||
293 | |||
294 | return sprintf(buf, "%u\n", ts->disabled); | ||
295 | } | ||
296 | |||
297 | static ssize_t ads7846_disable_store(struct device *dev, | ||
298 | struct device_attribute *attr, | ||
299 | const char *buf, size_t count) | ||
300 | { | ||
301 | struct ads7846 *ts = dev_get_drvdata(dev); | ||
302 | char *endp; | ||
303 | int i; | ||
304 | |||
305 | i = simple_strtoul(buf, &endp, 10); | ||
306 | spin_lock_irq(&ts->lock); | ||
307 | |||
308 | if (i) | ||
309 | ads7846_disable(ts); | ||
310 | else | ||
311 | ads7846_enable(ts); | ||
312 | |||
313 | spin_unlock_irq(&ts->lock); | ||
314 | |||
315 | return count; | ||
316 | } | ||
317 | |||
318 | static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store); | ||
319 | |||
236 | /*--------------------------------------------------------------------------*/ | 320 | /*--------------------------------------------------------------------------*/ |
237 | 321 | ||
238 | /* | 322 | /* |
@@ -252,19 +336,19 @@ static void ads7846_rx(void *ads) | |||
252 | u16 x, y, z1, z2; | 336 | u16 x, y, z1, z2; |
253 | unsigned long flags; | 337 | unsigned long flags; |
254 | 338 | ||
255 | /* adjust: 12 bit samples (left aligned), built from | 339 | /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding; |
256 | * two 8 bit values writen msb-first. | 340 | * built from two 8 bit values written msb-first. |
257 | */ | 341 | */ |
258 | x = be16_to_cpu(ts->tc.x) >> 4; | 342 | x = (be16_to_cpu(ts->tc.x) >> 3) & 0x0fff; |
259 | y = be16_to_cpu(ts->tc.y) >> 4; | 343 | y = (be16_to_cpu(ts->tc.y) >> 3) & 0x0fff; |
260 | z1 = be16_to_cpu(ts->tc.z1) >> 4; | 344 | z1 = (be16_to_cpu(ts->tc.z1) >> 3) & 0x0fff; |
261 | z2 = be16_to_cpu(ts->tc.z2) >> 4; | 345 | z2 = (be16_to_cpu(ts->tc.z2) >> 3) & 0x0fff; |
262 | 346 | ||
263 | /* range filtering */ | 347 | /* range filtering */ |
264 | if (x == MAX_12BIT) | 348 | if (x == MAX_12BIT) |
265 | x = 0; | 349 | x = 0; |
266 | 350 | ||
267 | if (x && z1 && ts->spi->dev.power.power_state.event == PM_EVENT_ON) { | 351 | if (likely(x && z1 && !device_suspended(&ts->spi->dev))) { |
268 | /* compute touch pressure resistance using equation #2 */ | 352 | /* compute touch pressure resistance using equation #2 */ |
269 | Rt = z2; | 353 | Rt = z2; |
270 | Rt -= z1; | 354 | Rt -= z1; |
@@ -275,6 +359,14 @@ static void ads7846_rx(void *ads) | |||
275 | } else | 359 | } else |
276 | Rt = 0; | 360 | Rt = 0; |
277 | 361 | ||
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 | |||
278 | /* NOTE: "pendown" is inferred from pressure; we don't rely on | 370 | /* NOTE: "pendown" is inferred from pressure; we don't rely on |
279 | * being able to check nPENIRQ status, or "friendly" trigger modes | 371 | * being able to check nPENIRQ status, or "friendly" trigger modes |
280 | * (both-edges is much better than just-falling or low-level). | 372 | * (both-edges is much better than just-falling or low-level). |
@@ -296,11 +388,13 @@ static void ads7846_rx(void *ads) | |||
296 | if (Rt) { | 388 | if (Rt) { |
297 | input_report_abs(input_dev, ABS_X, x); | 389 | input_report_abs(input_dev, ABS_X, x); |
298 | input_report_abs(input_dev, ABS_Y, y); | 390 | input_report_abs(input_dev, ABS_Y, y); |
299 | input_report_abs(input_dev, ABS_PRESSURE, Rt); | ||
300 | sync = 1; | 391 | sync = 1; |
301 | } | 392 | } |
302 | if (sync) | 393 | |
394 | if (sync) { | ||
395 | input_report_abs(input_dev, ABS_PRESSURE, Rt); | ||
303 | input_sync(input_dev); | 396 | input_sync(input_dev); |
397 | } | ||
304 | 398 | ||
305 | #ifdef VERBOSE | 399 | #ifdef VERBOSE |
306 | if (Rt || ts->pendown) | 400 | if (Rt || ts->pendown) |
@@ -308,80 +402,137 @@ static void ads7846_rx(void *ads) | |||
308 | x, y, Rt, Rt ? "" : " UP"); | 402 | x, y, Rt, Rt ? "" : " UP"); |
309 | #endif | 403 | #endif |
310 | 404 | ||
311 | /* don't retrigger while we're suspended */ | ||
312 | spin_lock_irqsave(&ts->lock, flags); | 405 | spin_lock_irqsave(&ts->lock, flags); |
313 | 406 | ||
314 | ts->pendown = (Rt != 0); | 407 | ts->pendown = (Rt != 0); |
315 | ts->pending = 0; | 408 | mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD); |
316 | 409 | ||
317 | if (ts->spi->dev.power.power_state.event == PM_EVENT_ON) { | 410 | spin_unlock_irqrestore(&ts->lock, flags); |
318 | if (ts->pendown) | 411 | } |
319 | mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD); | 412 | |
320 | else if (ts->irq_disabled) { | 413 | static void ads7846_debounce(void *ads) |
321 | ts->irq_disabled = 0; | 414 | { |
322 | enable_irq(ts->spi->irq); | 415 | struct ads7846 *ts = ads; |
416 | struct spi_message *m; | ||
417 | struct spi_transfer *t; | ||
418 | int val; | ||
419 | int status; | ||
420 | |||
421 | m = &ts->msg[ts->msg_idx]; | ||
422 | t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list); | ||
423 | val = (be16_to_cpu(*(__be16 *)t->rx_buf) >> 3) & 0x0fff; | ||
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; | ||
323 | } | 442 | } |
443 | /* Start over collecting consistent readings. */ | ||
444 | ts->read_rep = 0; | ||
445 | } else { | ||
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++; | ||
324 | } | 457 | } |
325 | 458 | status = spi_async(ts->spi, m); | |
326 | spin_unlock_irqrestore(&ts->lock, flags); | 459 | if (status) |
460 | dev_err(&ts->spi->dev, "spi_async --> %d\n", | ||
461 | status); | ||
327 | } | 462 | } |
328 | 463 | ||
329 | static void ads7846_timer(unsigned long handle) | 464 | static void ads7846_timer(unsigned long handle) |
330 | { | 465 | { |
331 | struct ads7846 *ts = (void *)handle; | 466 | struct ads7846 *ts = (void *)handle; |
332 | int status = 0; | 467 | int status = 0; |
333 | unsigned long flags; | 468 | |
469 | spin_lock_irq(&ts->lock); | ||
470 | |||
471 | if (unlikely(ts->msg_idx && !ts->pendown)) { | ||
472 | /* measurement cycle ended */ | ||
473 | if (!device_suspended(&ts->spi->dev)) { | ||
474 | ts->irq_disabled = 0; | ||
475 | enable_irq(ts->spi->irq); | ||
476 | } | ||
477 | ts->pending = 0; | ||
478 | ts->msg_idx = 0; | ||
479 | } else { | ||
480 | /* pen is still down, continue with the measurement */ | ||
481 | ts->msg_idx = 0; | ||
482 | status = spi_async(ts->spi, &ts->msg[0]); | ||
483 | if (status) | ||
484 | dev_err(&ts->spi->dev, "spi_async --> %d\n", status); | ||
485 | } | ||
486 | |||
487 | spin_unlock_irq(&ts->lock); | ||
488 | } | ||
489 | |||
490 | static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs) | ||
491 | { | ||
492 | struct ads7846 *ts = handle; | ||
493 | unsigned long flags; | ||
334 | 494 | ||
335 | spin_lock_irqsave(&ts->lock, flags); | 495 | spin_lock_irqsave(&ts->lock, flags); |
336 | if (!ts->pending) { | 496 | if (likely(ts->get_pendown_state())) { |
337 | ts->pending = 1; | ||
338 | if (!ts->irq_disabled) { | 497 | if (!ts->irq_disabled) { |
498 | /* The ARM do_simple_IRQ() dispatcher doesn't act | ||
499 | * like the other dispatchers: it will report IRQs | ||
500 | * even after they've been disabled. We work around | ||
501 | * that here. (The "generic irq" framework may help...) | ||
502 | */ | ||
339 | ts->irq_disabled = 1; | 503 | ts->irq_disabled = 1; |
340 | disable_irq(ts->spi->irq); | 504 | disable_irq(ts->spi->irq); |
505 | ts->pending = 1; | ||
506 | mod_timer(&ts->timer, jiffies); | ||
341 | } | 507 | } |
342 | status = spi_async(ts->spi, &ts->msg); | ||
343 | if (status) | ||
344 | dev_err(&ts->spi->dev, "spi_async --> %d\n", | ||
345 | status); | ||
346 | } | 508 | } |
347 | spin_unlock_irqrestore(&ts->lock, flags); | 509 | spin_unlock_irqrestore(&ts->lock, flags); |
348 | } | ||
349 | 510 | ||
350 | static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs) | ||
351 | { | ||
352 | ads7846_timer((unsigned long) handle); | ||
353 | return IRQ_HANDLED; | 511 | return IRQ_HANDLED; |
354 | } | 512 | } |
355 | 513 | ||
356 | /*--------------------------------------------------------------------------*/ | 514 | /*--------------------------------------------------------------------------*/ |
357 | 515 | ||
358 | static int | 516 | /* Must be called with ts->lock held */ |
359 | ads7846_suspend(struct spi_device *spi, pm_message_t message) | 517 | static void ads7846_disable(struct ads7846 *ts) |
360 | { | 518 | { |
361 | struct ads7846 *ts = dev_get_drvdata(&spi->dev); | 519 | if (ts->disabled) |
362 | unsigned long flags; | 520 | return; |
363 | 521 | ||
364 | spin_lock_irqsave(&ts->lock, flags); | 522 | ts->disabled = 1; |
365 | |||
366 | spi->dev.power.power_state = message; | ||
367 | 523 | ||
368 | /* are we waiting for IRQ, or polling? */ | 524 | /* are we waiting for IRQ, or polling? */ |
369 | if (!ts->pendown) { | 525 | if (!ts->pending) { |
370 | if (!ts->irq_disabled) { | 526 | ts->irq_disabled = 1; |
371 | ts->irq_disabled = 1; | 527 | disable_irq(ts->spi->irq); |
372 | disable_irq(ts->spi->irq); | ||
373 | } | ||
374 | } else { | 528 | } else { |
375 | /* polling; force a final SPI completion; | 529 | /* the timer will run at least once more, and |
376 | * that will clean things up neatly | 530 | * leave everything in a clean state, IRQ disabled |
377 | */ | 531 | */ |
378 | if (!ts->pending) | 532 | while (ts->pending) { |
379 | mod_timer(&ts->timer, jiffies); | 533 | spin_unlock_irq(&ts->lock); |
380 | 534 | msleep(1); | |
381 | while (ts->pendown || ts->pending) { | 535 | spin_lock_irq(&ts->lock); |
382 | spin_unlock_irqrestore(&ts->lock, flags); | ||
383 | udelay(10); | ||
384 | spin_lock_irqsave(&ts->lock, flags); | ||
385 | } | 536 | } |
386 | } | 537 | } |
387 | 538 | ||
@@ -389,17 +540,45 @@ ads7846_suspend(struct spi_device *spi, pm_message_t message) | |||
389 | * leave it that way after every request | 540 | * leave it that way after every request |
390 | */ | 541 | */ |
391 | 542 | ||
392 | spin_unlock_irqrestore(&ts->lock, flags); | 543 | } |
544 | |||
545 | /* Must be called with ts->lock held */ | ||
546 | static void ads7846_enable(struct ads7846 *ts) | ||
547 | { | ||
548 | if (!ts->disabled) | ||
549 | return; | ||
550 | |||
551 | ts->disabled = 0; | ||
552 | ts->irq_disabled = 0; | ||
553 | enable_irq(ts->spi->irq); | ||
554 | } | ||
555 | |||
556 | static int ads7846_suspend(struct spi_device *spi, pm_message_t message) | ||
557 | { | ||
558 | struct ads7846 *ts = dev_get_drvdata(&spi->dev); | ||
559 | |||
560 | spin_lock_irq(&ts->lock); | ||
561 | |||
562 | spi->dev.power.power_state = message; | ||
563 | ads7846_disable(ts); | ||
564 | |||
565 | spin_unlock_irq(&ts->lock); | ||
566 | |||
393 | return 0; | 567 | return 0; |
568 | |||
394 | } | 569 | } |
395 | 570 | ||
396 | static int ads7846_resume(struct spi_device *spi) | 571 | static int ads7846_resume(struct spi_device *spi) |
397 | { | 572 | { |
398 | struct ads7846 *ts = dev_get_drvdata(&spi->dev); | 573 | struct ads7846 *ts = dev_get_drvdata(&spi->dev); |
399 | 574 | ||
400 | ts->irq_disabled = 0; | 575 | spin_lock_irq(&ts->lock); |
401 | enable_irq(ts->spi->irq); | 576 | |
402 | spi->dev.power.power_state = PMSG_ON; | 577 | spi->dev.power.power_state = PMSG_ON; |
578 | ads7846_enable(ts); | ||
579 | |||
580 | spin_unlock_irq(&ts->lock); | ||
581 | |||
403 | return 0; | 582 | return 0; |
404 | } | 583 | } |
405 | 584 | ||
@@ -408,6 +587,7 @@ static int __devinit ads7846_probe(struct spi_device *spi) | |||
408 | struct ads7846 *ts; | 587 | struct ads7846 *ts; |
409 | struct input_dev *input_dev; | 588 | struct input_dev *input_dev; |
410 | struct ads7846_platform_data *pdata = spi->dev.platform_data; | 589 | struct ads7846_platform_data *pdata = spi->dev.platform_data; |
590 | struct spi_message *m; | ||
411 | struct spi_transfer *x; | 591 | struct spi_transfer *x; |
412 | int err; | 592 | int err; |
413 | 593 | ||
@@ -428,11 +608,20 @@ static int __devinit ads7846_probe(struct spi_device *spi) | |||
428 | return -EINVAL; | 608 | return -EINVAL; |
429 | } | 609 | } |
430 | 610 | ||
431 | /* We'd set the wordsize to 12 bits ... except that some controllers | 611 | /* REVISIT when the irq can be triggered active-low, or if for some |
432 | * will then treat the 8 bit command words as 12 bits (and drop the | 612 | * reason the touchscreen isn't hooked up, we don't need to access |
433 | * four MSBs of the 12 bit result). Result: inputs must be shifted | 613 | * the pendown state. |
434 | * to discard the four garbage LSBs. | ||
435 | */ | 614 | */ |
615 | if (pdata->get_pendown_state == NULL) { | ||
616 | dev_dbg(&spi->dev, "no get_pendown_state function?\n"); | ||
617 | return -EINVAL; | ||
618 | } | ||
619 | |||
620 | /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except | ||
621 | * that even if the hardware can do that, the SPI controller driver | ||
622 | * may not. So we stick to very-portable 8 bit words, both RX and TX. | ||
623 | */ | ||
624 | spi->bits_per_word = 8; | ||
436 | 625 | ||
437 | ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL); | 626 | ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL); |
438 | input_dev = input_allocate_device(); | 627 | input_dev = input_allocate_device(); |
@@ -451,9 +640,21 @@ static int __devinit ads7846_probe(struct spi_device *spi) | |||
451 | ts->timer.data = (unsigned long) ts; | 640 | ts->timer.data = (unsigned long) ts; |
452 | ts->timer.function = ads7846_timer; | 641 | ts->timer.function = ads7846_timer; |
453 | 642 | ||
643 | spin_lock_init(&ts->lock); | ||
644 | |||
454 | ts->model = pdata->model ? : 7846; | 645 | ts->model = pdata->model ? : 7846; |
455 | ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100; | 646 | ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100; |
456 | ts->x_plate_ohms = pdata->x_plate_ohms ? : 400; | 647 | ts->x_plate_ohms = pdata->x_plate_ohms ? : 400; |
648 | ts->pressure_max = pdata->pressure_max ? : ~0; | ||
649 | if (pdata->debounce_max) { | ||
650 | ts->debounce_max = pdata->debounce_max; | ||
651 | ts->debounce_tol = pdata->debounce_tol; | ||
652 | ts->debounce_rep = pdata->debounce_rep; | ||
653 | if (ts->debounce_rep > ts->debounce_max + 1) | ||
654 | ts->debounce_rep = ts->debounce_max - 1; | ||
655 | } else | ||
656 | ts->debounce_tol = ~0; | ||
657 | ts->get_pendown_state = pdata->get_pendown_state; | ||
457 | 658 | ||
458 | snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id); | 659 | snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id); |
459 | 660 | ||
@@ -477,64 +678,104 @@ static int __devinit ads7846_probe(struct spi_device *spi) | |||
477 | /* set up the transfers to read touchscreen state; this assumes we | 678 | /* set up the transfers to read touchscreen state; this assumes we |
478 | * use formula #2 for pressure, not #3. | 679 | * use formula #2 for pressure, not #3. |
479 | */ | 680 | */ |
480 | INIT_LIST_HEAD(&ts->msg.transfers); | 681 | m = &ts->msg[0]; |
481 | x = ts->xfer; | 682 | x = ts->xfer; |
482 | 683 | ||
684 | spi_message_init(m); | ||
685 | |||
483 | /* y- still on; turn on only y+ (and ADC) */ | 686 | /* y- still on; turn on only y+ (and ADC) */ |
484 | ts->read_y = READ_Y; | 687 | ts->read_y = READ_Y; |
485 | x->tx_buf = &ts->read_y; | 688 | x->tx_buf = &ts->read_y; |
486 | x->len = 1; | 689 | x->len = 1; |
487 | spi_message_add_tail(x, &ts->msg); | 690 | spi_message_add_tail(x, m); |
488 | 691 | ||
489 | x++; | 692 | x++; |
490 | x->rx_buf = &ts->tc.y; | 693 | x->rx_buf = &ts->tc.y; |
491 | x->len = 2; | 694 | x->len = 2; |
492 | spi_message_add_tail(x, &ts->msg); | 695 | spi_message_add_tail(x, m); |
696 | |||
697 | m->complete = ads7846_debounce; | ||
698 | m->context = ts; | ||
699 | |||
700 | m++; | ||
701 | spi_message_init(m); | ||
702 | |||
703 | /* turn y- off, x+ on, then leave in lowpower */ | ||
704 | x++; | ||
705 | ts->read_x = READ_X; | ||
706 | x->tx_buf = &ts->read_x; | ||
707 | x->len = 1; | ||
708 | spi_message_add_tail(x, m); | ||
709 | |||
710 | x++; | ||
711 | x->rx_buf = &ts->tc.x; | ||
712 | x->len = 2; | ||
713 | spi_message_add_tail(x, m); | ||
714 | |||
715 | m->complete = ads7846_debounce; | ||
716 | m->context = ts; | ||
493 | 717 | ||
494 | /* turn y+ off, x- on; we'll use formula #2 */ | 718 | /* turn y+ off, x- on; we'll use formula #2 */ |
495 | if (ts->model == 7846) { | 719 | if (ts->model == 7846) { |
720 | m++; | ||
721 | spi_message_init(m); | ||
722 | |||
496 | x++; | 723 | x++; |
497 | ts->read_z1 = READ_Z1; | 724 | ts->read_z1 = READ_Z1; |
498 | x->tx_buf = &ts->read_z1; | 725 | x->tx_buf = &ts->read_z1; |
499 | x->len = 1; | 726 | x->len = 1; |
500 | spi_message_add_tail(x, &ts->msg); | 727 | spi_message_add_tail(x, m); |
501 | 728 | ||
502 | x++; | 729 | x++; |
503 | x->rx_buf = &ts->tc.z1; | 730 | x->rx_buf = &ts->tc.z1; |
504 | x->len = 2; | 731 | x->len = 2; |
505 | spi_message_add_tail(x, &ts->msg); | 732 | spi_message_add_tail(x, m); |
733 | |||
734 | m->complete = ads7846_debounce; | ||
735 | m->context = ts; | ||
736 | |||
737 | m++; | ||
738 | spi_message_init(m); | ||
506 | 739 | ||
507 | x++; | 740 | x++; |
508 | ts->read_z2 = READ_Z2; | 741 | ts->read_z2 = READ_Z2; |
509 | x->tx_buf = &ts->read_z2; | 742 | x->tx_buf = &ts->read_z2; |
510 | x->len = 1; | 743 | x->len = 1; |
511 | spi_message_add_tail(x, &ts->msg); | 744 | spi_message_add_tail(x, m); |
512 | 745 | ||
513 | x++; | 746 | x++; |
514 | x->rx_buf = &ts->tc.z2; | 747 | x->rx_buf = &ts->tc.z2; |
515 | x->len = 2; | 748 | x->len = 2; |
516 | spi_message_add_tail(x, &ts->msg); | 749 | spi_message_add_tail(x, m); |
750 | |||
751 | m->complete = ads7846_debounce; | ||
752 | m->context = ts; | ||
517 | } | 753 | } |
518 | 754 | ||
519 | /* turn y- off, x+ on, then leave in lowpower */ | 755 | /* power down */ |
756 | m++; | ||
757 | spi_message_init(m); | ||
758 | |||
520 | x++; | 759 | x++; |
521 | ts->read_x = READ_X; | 760 | ts->pwrdown = PWRDOWN; |
522 | x->tx_buf = &ts->read_x; | 761 | x->tx_buf = &ts->pwrdown; |
523 | x->len = 1; | 762 | x->len = 1; |
524 | spi_message_add_tail(x, &ts->msg); | 763 | spi_message_add_tail(x, m); |
525 | 764 | ||
526 | x++; | 765 | x++; |
527 | x->rx_buf = &ts->tc.x; | 766 | x->rx_buf = &ts->dummy; |
528 | x->len = 2; | 767 | x->len = 2; |
529 | CS_CHANGE(*x); | 768 | CS_CHANGE(*x); |
530 | spi_message_add_tail(x, &ts->msg); | 769 | spi_message_add_tail(x, m); |
770 | |||
771 | m->complete = ads7846_rx; | ||
772 | m->context = ts; | ||
531 | 773 | ||
532 | ts->msg.complete = ads7846_rx; | 774 | ts->last_msg = m; |
533 | ts->msg.context = ts; | ||
534 | 775 | ||
535 | if (request_irq(spi->irq, ads7846_irq, | 776 | if (request_irq(spi->irq, ads7846_irq, |
536 | SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING, | 777 | SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING, |
537 | spi->dev.bus_id, ts)) { | 778 | spi->dev.driver->name, ts)) { |
538 | dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq); | 779 | dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq); |
539 | err = -EBUSY; | 780 | err = -EBUSY; |
540 | goto err_free_mem; | 781 | goto err_free_mem; |
@@ -559,13 +800,27 @@ static int __devinit ads7846_probe(struct spi_device *spi) | |||
559 | device_create_file(&spi->dev, &dev_attr_vbatt); | 800 | device_create_file(&spi->dev, &dev_attr_vbatt); |
560 | device_create_file(&spi->dev, &dev_attr_vaux); | 801 | device_create_file(&spi->dev, &dev_attr_vaux); |
561 | 802 | ||
803 | device_create_file(&spi->dev, &dev_attr_pen_down); | ||
804 | |||
805 | device_create_file(&spi->dev, &dev_attr_disable); | ||
806 | |||
562 | err = input_register_device(input_dev); | 807 | err = input_register_device(input_dev); |
563 | if (err) | 808 | if (err) |
564 | goto err_free_irq; | 809 | goto err_remove_attr; |
565 | 810 | ||
566 | return 0; | 811 | return 0; |
567 | 812 | ||
568 | err_free_irq: | 813 | err_remove_attr: |
814 | device_remove_file(&spi->dev, &dev_attr_disable); | ||
815 | device_remove_file(&spi->dev, &dev_attr_pen_down); | ||
816 | if (ts->model == 7846) { | ||
817 | device_remove_file(&spi->dev, &dev_attr_temp1); | ||
818 | device_remove_file(&spi->dev, &dev_attr_temp0); | ||
819 | } | ||
820 | if (ts->model != 7845) | ||
821 | device_remove_file(&spi->dev, &dev_attr_vbatt); | ||
822 | device_remove_file(&spi->dev, &dev_attr_vaux); | ||
823 | |||
569 | free_irq(spi->irq, ts); | 824 | free_irq(spi->irq, ts); |
570 | err_free_mem: | 825 | err_free_mem: |
571 | input_free_device(input_dev); | 826 | input_free_device(input_dev); |
@@ -577,20 +832,24 @@ static int __devexit ads7846_remove(struct spi_device *spi) | |||
577 | { | 832 | { |
578 | struct ads7846 *ts = dev_get_drvdata(&spi->dev); | 833 | struct ads7846 *ts = dev_get_drvdata(&spi->dev); |
579 | 834 | ||
835 | input_unregister_device(ts->input); | ||
836 | |||
580 | ads7846_suspend(spi, PMSG_SUSPEND); | 837 | ads7846_suspend(spi, PMSG_SUSPEND); |
581 | free_irq(ts->spi->irq, ts); | ||
582 | if (ts->irq_disabled) | ||
583 | enable_irq(ts->spi->irq); | ||
584 | 838 | ||
839 | device_remove_file(&spi->dev, &dev_attr_disable); | ||
840 | device_remove_file(&spi->dev, &dev_attr_pen_down); | ||
585 | if (ts->model == 7846) { | 841 | if (ts->model == 7846) { |
586 | device_remove_file(&spi->dev, &dev_attr_temp0); | ||
587 | device_remove_file(&spi->dev, &dev_attr_temp1); | 842 | device_remove_file(&spi->dev, &dev_attr_temp1); |
843 | device_remove_file(&spi->dev, &dev_attr_temp0); | ||
588 | } | 844 | } |
589 | if (ts->model != 7845) | 845 | if (ts->model != 7845) |
590 | device_remove_file(&spi->dev, &dev_attr_vbatt); | 846 | device_remove_file(&spi->dev, &dev_attr_vbatt); |
591 | device_remove_file(&spi->dev, &dev_attr_vaux); | 847 | device_remove_file(&spi->dev, &dev_attr_vaux); |
592 | 848 | ||
593 | input_unregister_device(ts->input); | 849 | free_irq(ts->spi->irq, ts); |
850 | /* suspend left the IRQ disabled */ | ||
851 | enable_irq(ts->spi->irq); | ||
852 | |||
594 | kfree(ts); | 853 | kfree(ts); |
595 | 854 | ||
596 | dev_dbg(&spi->dev, "unregistered touchscreen\n"); | 855 | dev_dbg(&spi->dev, "unregistered touchscreen\n"); |