aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/touchscreen/ads7846.c
diff options
context:
space:
mode:
authorPaul Mackerras <paulus@samba.org>2006-02-23 22:05:47 -0500
committerPaul Mackerras <paulus@samba.org>2006-02-23 22:05:47 -0500
commita00428f5b149e36b8225b2a0812742a6dfb07b8c (patch)
treea78869cd67cf78a0eb091fb0ea5d397734bd6738 /drivers/input/touchscreen/ads7846.c
parent774fee58c465ea1c7e9775e347ec307bcf2deeb3 (diff)
parentfb5c594c2acc441f0d2d8f457484a0e0e9285db3 (diff)
Merge ../powerpc-merge
Diffstat (limited to 'drivers/input/touchscreen/ads7846.c')
-rw-r--r--drivers/input/touchscreen/ads7846.c147
1 files changed, 91 insertions, 56 deletions
diff --git a/drivers/input/touchscreen/ads7846.c b/drivers/input/touchscreen/ads7846.c
index b45a45ca7cc9..8c12a974b411 100644
--- a/drivers/input/touchscreen/ads7846.c
+++ b/drivers/input/touchscreen/ads7846.c
@@ -48,10 +48,13 @@
48 48
49#define TS_POLL_PERIOD msecs_to_jiffies(10) 49#define TS_POLL_PERIOD msecs_to_jiffies(10)
50 50
51/* this driver doesn't aim at the peak continuous sample rate */
52#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
53
51struct ts_event { 54struct ts_event {
52 /* For portability, we can't read 12 bit values using SPI (which 55 /* For portability, we can't read 12 bit values using SPI (which
53 * would make the controller deliver them as native byteorder u16 56 * would make the controller deliver them as native byteorder u16
54 * with msbs zeroed). Instead, we read them as two 8-byte values, 57 * with msbs zeroed). Instead, we read them as two 8-bit values,
55 * which need byteswapping then range adjustment. 58 * which need byteswapping then range adjustment.
56 */ 59 */
57 __be16 x; 60 __be16 x;
@@ -60,7 +63,7 @@ struct ts_event {
60}; 63};
61 64
62struct ads7846 { 65struct ads7846 {
63 struct input_dev input; 66 struct input_dev *input;
64 char phys[32]; 67 char phys[32];
65 68
66 struct spi_device *spi; 69 struct spi_device *spi;
@@ -68,6 +71,7 @@ struct ads7846 {
68 u16 vref_delay_usecs; 71 u16 vref_delay_usecs;
69 u16 x_plate_ohms; 72 u16 x_plate_ohms;
70 73
74 u8 read_x, read_y, read_z1, read_z2;
71 struct ts_event tc; 75 struct ts_event tc;
72 76
73 struct spi_transfer xfer[8]; 77 struct spi_transfer xfer[8];
@@ -117,10 +121,10 @@ struct ads7846 {
117#define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \ 121#define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \
118 | ADS_12_BIT | ADS_DFR) 122 | ADS_12_BIT | ADS_DFR)
119 123
120static const u8 read_y = READ_12BIT_DFR(y) | ADS_PD10_ADC_ON; 124#define READ_Y (READ_12BIT_DFR(y) | ADS_PD10_ADC_ON)
121static const u8 read_z1 = READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON; 125#define READ_Z1 (READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON)
122static const u8 read_z2 = READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON; 126#define READ_Z2 (READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON)
123static const u8 read_x = READ_12BIT_DFR(x) | ADS_PD10_PDOWN; /* LAST */ 127#define READ_X (READ_12BIT_DFR(x) | ADS_PD10_PDOWN) /* LAST */
124 128
125/* single-ended samples need to first power up reference voltage; 129/* single-ended samples need to first power up reference voltage;
126 * we leave both ADC and VREF powered 130 * we leave both ADC and VREF powered
@@ -128,8 +132,8 @@ static const u8 read_x = READ_12BIT_DFR(x) | ADS_PD10_PDOWN; /* LAST */
128#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \ 132#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
129 | ADS_12_BIT | ADS_SER) 133 | ADS_12_BIT | ADS_SER)
130 134
131static const u8 ref_on = READ_12BIT_DFR(x) | ADS_PD10_ALL_ON; 135#define REF_ON (READ_12BIT_DFR(x) | ADS_PD10_ALL_ON)
132static const u8 ref_off = READ_12BIT_DFR(y) | ADS_PD10_PDOWN; 136#define REF_OFF (READ_12BIT_DFR(y) | ADS_PD10_PDOWN)
133 137
134/*--------------------------------------------------------------------------*/ 138/*--------------------------------------------------------------------------*/
135 139
@@ -138,7 +142,9 @@ static const u8 ref_off = READ_12BIT_DFR(y) | ADS_PD10_PDOWN;
138 */ 142 */
139 143
140struct ser_req { 144struct ser_req {
145 u8 ref_on;
141 u8 command; 146 u8 command;
147 u8 ref_off;
142 u16 scratch; 148 u16 scratch;
143 __be16 sample; 149 __be16 sample;
144 struct spi_message msg; 150 struct spi_message msg;
@@ -152,7 +158,7 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)
152 struct ser_req *req = kzalloc(sizeof *req, SLAB_KERNEL); 158 struct ser_req *req = kzalloc(sizeof *req, SLAB_KERNEL);
153 int status; 159 int status;
154 int sample; 160 int sample;
155 int i; 161 int i;
156 162
157 if (!req) 163 if (!req)
158 return -ENOMEM; 164 return -ENOMEM;
@@ -160,7 +166,8 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)
160 INIT_LIST_HEAD(&req->msg.transfers); 166 INIT_LIST_HEAD(&req->msg.transfers);
161 167
162 /* activate reference, so it has time to settle; */ 168 /* activate reference, so it has time to settle; */
163 req->xfer[0].tx_buf = &ref_on; 169 req->ref_on = REF_ON;
170 req->xfer[0].tx_buf = &req->ref_on;
164 req->xfer[0].len = 1; 171 req->xfer[0].len = 1;
165 req->xfer[1].rx_buf = &req->scratch; 172 req->xfer[1].rx_buf = &req->scratch;
166 req->xfer[1].len = 2; 173 req->xfer[1].len = 2;
@@ -182,7 +189,8 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)
182 /* REVISIT: take a few more samples, and compare ... */ 189 /* REVISIT: take a few more samples, and compare ... */
183 190
184 /* turn off reference */ 191 /* turn off reference */
185 req->xfer[4].tx_buf = &ref_off; 192 req->ref_off = REF_OFF;
193 req->xfer[4].tx_buf = &req->ref_off;
186 req->xfer[4].len = 1; 194 req->xfer[4].len = 1;
187 req->xfer[5].rx_buf = &req->scratch; 195 req->xfer[5].rx_buf = &req->scratch;
188 req->xfer[5].len = 2; 196 req->xfer[5].len = 2;
@@ -236,11 +244,12 @@ SHOW(vbatt)
236 244
237static void ads7846_rx(void *ads) 245static void ads7846_rx(void *ads)
238{ 246{
239 struct ads7846 *ts = ads; 247 struct ads7846 *ts = ads;
240 unsigned Rt; 248 struct input_dev *input_dev = ts->input;
241 unsigned sync = 0; 249 unsigned Rt;
242 u16 x, y, z1, z2; 250 unsigned sync = 0;
243 unsigned long flags; 251 u16 x, y, z1, z2;
252 unsigned long flags;
244 253
245 /* adjust: 12 bit samples (left aligned), built from 254 /* adjust: 12 bit samples (left aligned), built from
246 * two 8 bit values writen msb-first. 255 * two 8 bit values writen msb-first.
@@ -276,21 +285,21 @@ static void ads7846_rx(void *ads)
276 * won't notice that, even if nPENIRQ never fires ... 285 * won't notice that, even if nPENIRQ never fires ...
277 */ 286 */
278 if (!ts->pendown && Rt != 0) { 287 if (!ts->pendown && Rt != 0) {
279 input_report_key(&ts->input, BTN_TOUCH, 1); 288 input_report_key(input_dev, BTN_TOUCH, 1);
280 sync = 1; 289 sync = 1;
281 } else if (ts->pendown && Rt == 0) { 290 } else if (ts->pendown && Rt == 0) {
282 input_report_key(&ts->input, BTN_TOUCH, 0); 291 input_report_key(input_dev, BTN_TOUCH, 0);
283 sync = 1; 292 sync = 1;
284 } 293 }
285 294
286 if (Rt) { 295 if (Rt) {
287 input_report_abs(&ts->input, ABS_X, x); 296 input_report_abs(input_dev, ABS_X, x);
288 input_report_abs(&ts->input, ABS_Y, y); 297 input_report_abs(input_dev, ABS_Y, y);
289 input_report_abs(&ts->input, ABS_PRESSURE, Rt); 298 input_report_abs(input_dev, ABS_PRESSURE, Rt);
290 sync = 1; 299 sync = 1;
291 } 300 }
292 if (sync) 301 if (sync)
293 input_sync(&ts->input); 302 input_sync(input_dev);
294 303
295#ifdef VERBOSE 304#ifdef VERBOSE
296 if (Rt || ts->pendown) 305 if (Rt || ts->pendown)
@@ -396,9 +405,10 @@ static int ads7846_resume(struct spi_device *spi)
396static int __devinit ads7846_probe(struct spi_device *spi) 405static int __devinit ads7846_probe(struct spi_device *spi)
397{ 406{
398 struct ads7846 *ts; 407 struct ads7846 *ts;
408 struct input_dev *input_dev;
399 struct ads7846_platform_data *pdata = spi->dev.platform_data; 409 struct ads7846_platform_data *pdata = spi->dev.platform_data;
400 struct spi_transfer *x; 410 struct spi_transfer *x;
401 int i; 411 int err;
402 412
403 if (!spi->irq) { 413 if (!spi->irq) {
404 dev_dbg(&spi->dev, "no IRQ?\n"); 414 dev_dbg(&spi->dev, "no IRQ?\n");
@@ -411,9 +421,9 @@ static int __devinit ads7846_probe(struct spi_device *spi)
411 } 421 }
412 422
413 /* don't exceed max specified sample rate */ 423 /* don't exceed max specified sample rate */
414 if (spi->max_speed_hz > (125000 * 16)) { 424 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
415 dev_dbg(&spi->dev, "f(sample) %d KHz?\n", 425 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
416 (spi->max_speed_hz/16)/1000); 426 (spi->max_speed_hz/SAMPLE_BITS)/1000);
417 return -EINVAL; 427 return -EINVAL;
418 } 428 }
419 429
@@ -423,13 +433,18 @@ static int __devinit ads7846_probe(struct spi_device *spi)
423 * to discard the four garbage LSBs. 433 * to discard the four garbage LSBs.
424 */ 434 */
425 435
426 if (!(ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL))) 436 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
427 return -ENOMEM; 437 input_dev = input_allocate_device();
438 if (!ts || !input_dev) {
439 err = -ENOMEM;
440 goto err_free_mem;
441 }
428 442
429 dev_set_drvdata(&spi->dev, ts); 443 dev_set_drvdata(&spi->dev, ts);
444 spi->dev.power.power_state = PMSG_ON;
430 445
431 ts->spi = spi; 446 ts->spi = spi;
432 spi->dev.power.power_state = PMSG_ON; 447 ts->input = input_dev;
433 448
434 init_timer(&ts->timer); 449 init_timer(&ts->timer);
435 ts->timer.data = (unsigned long) ts; 450 ts->timer.data = (unsigned long) ts;
@@ -439,70 +454,80 @@ static int __devinit ads7846_probe(struct spi_device *spi)
439 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100; 454 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
440 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400; 455 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
441 456
442 init_input_dev(&ts->input); 457 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
443 458
444 ts->input.dev = &spi->dev; 459 input_dev->name = "ADS784x Touchscreen";
445 ts->input.name = "ADS784x Touchscreen"; 460 input_dev->phys = ts->phys;
446 snprintf(ts->phys, sizeof ts->phys, "%s/input0", spi->dev.bus_id); 461 input_dev->cdev.dev = &spi->dev;
447 ts->input.phys = ts->phys;
448 462
449 ts->input.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); 463 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
450 ts->input.keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH); 464 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
451 input_set_abs_params(&ts->input, ABS_X, 465 input_set_abs_params(input_dev, ABS_X,
452 pdata->x_min ? : 0, 466 pdata->x_min ? : 0,
453 pdata->x_max ? : MAX_12BIT, 467 pdata->x_max ? : MAX_12BIT,
454 0, 0); 468 0, 0);
455 input_set_abs_params(&ts->input, ABS_Y, 469 input_set_abs_params(input_dev, ABS_Y,
456 pdata->y_min ? : 0, 470 pdata->y_min ? : 0,
457 pdata->y_max ? : MAX_12BIT, 471 pdata->y_max ? : MAX_12BIT,
458 0, 0); 472 0, 0);
459 input_set_abs_params(&ts->input, ABS_PRESSURE, 473 input_set_abs_params(input_dev, ABS_PRESSURE,
460 pdata->pressure_min, pdata->pressure_max, 0, 0); 474 pdata->pressure_min, pdata->pressure_max, 0, 0);
461 475
462 input_register_device(&ts->input);
463
464 /* set up the transfers to read touchscreen state; this assumes we 476 /* set up the transfers to read touchscreen state; this assumes we
465 * use formula #2 for pressure, not #3. 477 * use formula #2 for pressure, not #3.
466 */ 478 */
479 INIT_LIST_HEAD(&ts->msg.transfers);
467 x = ts->xfer; 480 x = ts->xfer;
468 481
469 /* y- still on; turn on only y+ (and ADC) */ 482 /* y- still on; turn on only y+ (and ADC) */
470 x->tx_buf = &read_y; 483 ts->read_y = READ_Y;
484 x->tx_buf = &ts->read_y;
471 x->len = 1; 485 x->len = 1;
486 spi_message_add_tail(x, &ts->msg);
487
472 x++; 488 x++;
473 x->rx_buf = &ts->tc.y; 489 x->rx_buf = &ts->tc.y;
474 x->len = 2; 490 x->len = 2;
475 x++; 491 spi_message_add_tail(x, &ts->msg);
476 492
477 /* turn y+ off, x- on; we'll use formula #2 */ 493 /* turn y+ off, x- on; we'll use formula #2 */
478 if (ts->model == 7846) { 494 if (ts->model == 7846) {
479 x->tx_buf = &read_z1; 495 x++;
496 ts->read_z1 = READ_Z1;
497 x->tx_buf = &ts->read_z1;
480 x->len = 1; 498 x->len = 1;
499 spi_message_add_tail(x, &ts->msg);
500
481 x++; 501 x++;
482 x->rx_buf = &ts->tc.z1; 502 x->rx_buf = &ts->tc.z1;
483 x->len = 2; 503 x->len = 2;
484 x++; 504 spi_message_add_tail(x, &ts->msg);
485 505
486 x->tx_buf = &read_z2; 506 x++;
507 ts->read_z2 = READ_Z2;
508 x->tx_buf = &ts->read_z2;
487 x->len = 1; 509 x->len = 1;
510 spi_message_add_tail(x, &ts->msg);
511
488 x++; 512 x++;
489 x->rx_buf = &ts->tc.z2; 513 x->rx_buf = &ts->tc.z2;
490 x->len = 2; 514 x->len = 2;
491 x++; 515 spi_message_add_tail(x, &ts->msg);
492 } 516 }
493 517
494 /* turn y- off, x+ on, then leave in lowpower */ 518 /* turn y- off, x+ on, then leave in lowpower */
495 x->tx_buf = &read_x; 519 x++;
520 ts->read_x = READ_X;
521 x->tx_buf = &ts->read_x;
496 x->len = 1; 522 x->len = 1;
523 spi_message_add_tail(x, &ts->msg);
524
497 x++; 525 x++;
498 x->rx_buf = &ts->tc.x; 526 x->rx_buf = &ts->tc.x;
499 x->len = 2; 527 x->len = 2;
500 x++; 528 CS_CHANGE(*x);
501 529 spi_message_add_tail(x, &ts->msg);
502 CS_CHANGE(x[-1]);
503 530
504 for (i = 0; i < x - ts->xfer; i++)
505 spi_message_add_tail(&ts->xfer[i], &ts->msg);
506 ts->msg.complete = ads7846_rx; 531 ts->msg.complete = ads7846_rx;
507 ts->msg.context = ts; 532 ts->msg.context = ts;
508 533
@@ -510,9 +535,8 @@ static int __devinit ads7846_probe(struct spi_device *spi)
510 SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING, 535 SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,
511 spi->dev.bus_id, ts)) { 536 spi->dev.bus_id, ts)) {
512 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq); 537 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
513 input_unregister_device(&ts->input); 538 err = -EBUSY;
514 kfree(ts); 539 goto err_free_mem;
515 return -EBUSY;
516 } 540 }
517 541
518 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq); 542 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
@@ -534,7 +558,18 @@ static int __devinit ads7846_probe(struct spi_device *spi)
534 device_create_file(&spi->dev, &dev_attr_vbatt); 558 device_create_file(&spi->dev, &dev_attr_vbatt);
535 device_create_file(&spi->dev, &dev_attr_vaux); 559 device_create_file(&spi->dev, &dev_attr_vaux);
536 560
561 err = input_register_device(input_dev);
562 if (err)
563 goto err_free_irq;
564
537 return 0; 565 return 0;
566
567 err_free_irq:
568 free_irq(spi->irq, ts);
569 err_free_mem:
570 input_free_device(input_dev);
571 kfree(ts);
572 return err;
538} 573}
539 574
540static int __devexit ads7846_remove(struct spi_device *spi) 575static int __devexit ads7846_remove(struct spi_device *spi)
@@ -554,7 +589,7 @@ static int __devexit ads7846_remove(struct spi_device *spi)
554 device_remove_file(&spi->dev, &dev_attr_vbatt); 589 device_remove_file(&spi->dev, &dev_attr_vbatt);
555 device_remove_file(&spi->dev, &dev_attr_vaux); 590 device_remove_file(&spi->dev, &dev_attr_vaux);
556 591
557 input_unregister_device(&ts->input); 592 input_unregister_device(ts->input);
558 kfree(ts); 593 kfree(ts);
559 594
560 dev_dbg(&spi->dev, "unregistered touchscreen\n"); 595 dev_dbg(&spi->dev, "unregistered touchscreen\n");