1 /* 2 * ADS7846 based touchscreen and sensor driver 3 * 4 * Copyright (c) 2005 David Brownell 5 * Copyright (c) 2006 Nokia Corporation 6 * Various changes: Imre Deak <imre.deak@nokia.com> 7 * 8 * Using code from: 9 * - corgi_ts.c 10 * Copyright (C) 2004-2005 Richard Purdie 11 * - omap_ts.[hc], ads7846.h, ts_osk.c 12 * Copyright (C) 2002 MontaVista Software 13 * Copyright (C) 2004 Texas Instruments 14 * Copyright (C) 2005 Dirk Behme 15 * 16 * This program is free software; you can redistribute it and/or modify 17 * it under the terms of the GNU General Public License version 2 as 18 * published by the Free Software Foundation. 19 */ 20 #include <linux/hwmon.h> 21 #include <linux/init.h> 22 #include <linux/err.h> 23 #include <linux/delay.h> 24 #include <linux/input.h> 25 #include <linux/interrupt.h> 26 #include <linux/slab.h> 27 #include <linux/gpio.h> 28 #include <linux/spi/spi.h> 29 #include <linux/spi/ads7846.h> 30 #include <asm/irq.h> 31 32 /* 33 * This code has been heavily tested on a Nokia 770, and lightly 34 * tested on other ads7846 devices (OSK/Mistral, Lubbock, Spitz). 35 * TSC2046 is just newer ads7846 silicon. 36 * Support for ads7843 tested on Atmel at91sam926x-EK. 37 * Support for ads7845 has only been stubbed in. 38 * 39 * IRQ handling needs a workaround because of a shortcoming in handling 40 * edge triggered IRQs on some platforms like the OMAP1/2. These 41 * platforms don't handle the ARM lazy IRQ disabling properly, thus we 42 * have to maintain our own SW IRQ disabled status. This should be 43 * removed as soon as the affected platform's IRQ handling is fixed. 44 * 45 * App note sbaa036 talks in more detail about accurate sampling... 46 * that ought to help in situations like LCDs inducing noise (which 47 * can also be helped by using synch signals) and more generally. 48 * This driver tries to utilize the measures described in the app 49 * note. The strength of filtering can be set in the board-* specific 50 * files. 51 */ 52 53 #define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */ 54 #define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */ 55 56 /* this driver doesn't aim at the peak continuous sample rate */ 57 #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */) 58 59 struct ts_event { 60 /* For portability, we can't read 12 bit values using SPI (which 61 * would make the controller deliver them as native byteorder u16 62 * with msbs zeroed). Instead, we read them as two 8-bit values, 63 * *** WHICH NEED BYTESWAPPING *** and range adjustment. 64 */ 65 u16 x; 66 u16 y; 67 u16 z1, z2; 68 int ignore; 69 }; 70 71 /* 72 * We allocate this separately to avoid cache line sharing issues when 73 * driver is used with DMA-based SPI controllers (like atmel_spi) on 74 * systems where main memory is not DMA-coherent (most non-x86 boards). 75 */ 76 struct ads7846_packet { 77 u8 read_x, read_y, read_z1, read_z2, pwrdown; 78 u16 dummy; /* for the pwrdown read */ 79 struct ts_event tc; 80 }; 81 82 struct ads7846 { 83 struct input_dev *input; 84 char phys[32]; 85 char name[32]; 86 87 struct spi_device *spi; 88 89 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE) 90 struct attribute_group *attr_group; 91 struct device *hwmon; 92 #endif 93 94 u16 model; 95 u16 vref_mv; 96 u16 vref_delay_usecs; 97 u16 x_plate_ohms; 98 u16 pressure_max; 99 100 bool swap_xy; 101 102 struct ads7846_packet *packet; 103 104 struct spi_transfer xfer[18]; 105 struct spi_message msg[5]; 106 struct spi_message *last_msg; 107 int msg_idx; 108 int read_cnt; 109 int read_rep; 110 int last_read; 111 112 u16 debounce_max; 113 u16 debounce_tol; 114 u16 debounce_rep; 115 116 u16 penirq_recheck_delay_usecs; 117 118 spinlock_t lock; 119 struct hrtimer timer; 120 unsigned pendown:1; /* P: lock */ 121 unsigned pending:1; /* P: lock */ 122 // FIXME remove "irq_disabled" 123 unsigned irq_disabled:1; /* P: lock */ 124 unsigned disabled:1; 125 unsigned is_suspended:1; 126 127 int (*filter)(void *data, int data_idx, int *val); 128 void *filter_data; 129 void (*filter_cleanup)(void *data); 130 int (*get_pendown_state)(void); 131 int gpio_pendown; 132 133 void (*wait_for_sync)(void); 134 }; 135 136 /* leave chip selected when we're done, for quicker re-select? */ 137 #if 0 138 #define CS_CHANGE(xfer) ((xfer).cs_change = 1) 139 #else 140 #define CS_CHANGE(xfer) ((xfer).cs_change = 0) 141 #endif 142 143 /*--------------------------------------------------------------------------*/ 144 145 /* The ADS7846 has touchscreen and other sensors. 146 * Earlier ads784x chips are somewhat compatible. 147 */ 148 #define ADS_START (1 << 7) 149 #define ADS_A2A1A0_d_y (1 << 4) /* differential */ 150 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */ 151 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */ 152 #define ADS_A2A1A0_d_x (5 << 4) /* differential */ 153 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */ 154 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */ 155 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */ 156 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */ 157 #define ADS_8_BIT (1 << 3) 158 #define ADS_12_BIT (0 << 3) 159 #define ADS_SER (1 << 2) /* non-differential */ 160 #define ADS_DFR (0 << 2) /* differential */ 161 #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */ 162 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */ 163 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */ 164 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */ 165 166 #define MAX_12BIT ((1<<12)-1) 167 168 /* leave ADC powered up (disables penirq) between differential samples */ 169 #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \ 170 | ADS_12_BIT | ADS_DFR | \ 171 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0)) 172 173 #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref)) 174 #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref)) 175 #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref)) 176 177 #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref)) 178 #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */ 179 180 /* single-ended samples need to first power up reference voltage; 181 * we leave both ADC and VREF powered 182 */ 183 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \ 184 | ADS_12_BIT | ADS_SER) 185 186 #define REF_ON (READ_12BIT_DFR(x, 1, 1)) 187 #define REF_OFF (READ_12BIT_DFR(y, 0, 0)) 188 189 /*--------------------------------------------------------------------------*/ 190 191 /* 192 * Non-touchscreen sensors only use single-ended conversions. 193 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF; 194 * ads7846 lets that pin be unconnected, to use internal vREF. 195 */ 196 197 struct ser_req { 198 u8 ref_on; 199 u8 command; 200 u8 ref_off; 201 u16 scratch; 202 __be16 sample; 203 struct spi_message msg; 204 struct spi_transfer xfer[6]; 205 }; 206 207 static void ads7846_enable(struct ads7846 *ts); 208 static void ads7846_disable(struct ads7846 *ts); 209 210 static int device_suspended(struct device *dev) 211 { 212 struct ads7846 *ts = dev_get_drvdata(dev); 213 return ts->is_suspended || ts->disabled; 214 } 215 216 static int ads7846_read12_ser(struct device *dev, unsigned command) 217 { 218 struct spi_device *spi = to_spi_device(dev); 219 struct ads7846 *ts = dev_get_drvdata(dev); 220 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL); 221 int status; 222 int use_internal; 223 224 if (!req) 225 return -ENOMEM; 226 227 spi_message_init(&req->msg); 228 229 /* FIXME boards with ads7846 might use external vref instead ... */ 230 use_internal = (ts->model == 7846); 231 232 /* maybe turn on internal vREF, and let it settle */ 233 if (use_internal) { 234 req->ref_on = REF_ON; 235 req->xfer[0].tx_buf = &req->ref_on; 236 req->xfer[0].len = 1; 237 spi_message_add_tail(&req->xfer[0], &req->msg); 238 239 req->xfer[1].rx_buf = &req->scratch; 240 req->xfer[1].len = 2; 241 242 /* for 1uF, settle for 800 usec; no cap, 100 usec. */ 243 req->xfer[1].delay_usecs = ts->vref_delay_usecs; 244 spi_message_add_tail(&req->xfer[1], &req->msg); 245 } 246 247 /* take sample */ 248 req->command = (u8) command; 249 req->xfer[2].tx_buf = &req->command; 250 req->xfer[2].len = 1; 251 spi_message_add_tail(&req->xfer[2], &req->msg); 252 253 req->xfer[3].rx_buf = &req->sample; 254 req->xfer[3].len = 2; 255 spi_message_add_tail(&req->xfer[3], &req->msg); 256 257 /* REVISIT: take a few more samples, and compare ... */ 258 259 /* converter in low power mode & enable PENIRQ */ 260 req->ref_off = PWRDOWN; 261 req->xfer[4].tx_buf = &req->ref_off; 262 req->xfer[4].len = 1; 263 spi_message_add_tail(&req->xfer[4], &req->msg); 264 265 req->xfer[5].rx_buf = &req->scratch; 266 req->xfer[5].len = 2; 267 CS_CHANGE(req->xfer[5]); 268 spi_message_add_tail(&req->xfer[5], &req->msg); 269 270 ts->irq_disabled = 1; 271 disable_irq(spi->irq); 272 status = spi_sync(spi, &req->msg); 273 ts->irq_disabled = 0; 274 enable_irq(spi->irq); 275 276 if (status == 0) { 277 /* on-wire is a must-ignore bit, a BE12 value, then padding */ 278 status = be16_to_cpu(req->sample); 279 status = status >> 3; 280 status &= 0x0fff; 281 } 282 283 kfree(req); 284 return status; 285 } 286 287 #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE) 288 289 #define SHOW(name, var, adjust) static ssize_t \ 290 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \ 291 { \ 292 struct ads7846 *ts = dev_get_drvdata(dev); \ 293 ssize_t v = ads7846_read12_ser(dev, \ 294 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \ 295 if (v < 0) \ 296 return v; \ 297 return sprintf(buf, "%u\n", adjust(ts, v)); \ 298 } \ 299 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL); 300 301 302 /* Sysfs conventions report temperatures in millidegrees Celsius. 303 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high 304 * accuracy scheme without calibration data. For now we won't try either; 305 * userspace sees raw sensor values, and must scale/calibrate appropriately. 306 */ 307 static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v) 308 { 309 return v; 310 } 311 312 SHOW(temp0, temp0, null_adjust) /* temp1_input */ 313 SHOW(temp1, temp1, null_adjust) /* temp2_input */ 314 315 316 /* sysfs conventions report voltages in millivolts. We can convert voltages 317 * if we know vREF. userspace may need to scale vAUX to match the board's 318 * external resistors; we assume that vBATT only uses the internal ones. 319 */ 320 static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v) 321 { 322 unsigned retval = v; 323 324 /* external resistors may scale vAUX into 0..vREF */ 325 retval *= ts->vref_mv; 326 retval = retval >> 12; 327 return retval; 328 } 329 330 static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v) 331 { 332 unsigned retval = vaux_adjust(ts, v); 333 334 /* ads7846 has a resistor ladder to scale this signal down */ 335 if (ts->model == 7846) 336 retval *= 4; 337 return retval; 338 } 339 340 SHOW(in0_input, vaux, vaux_adjust) 341 SHOW(in1_input, vbatt, vbatt_adjust) 342 343 344 static struct attribute *ads7846_attributes[] = { 345 &dev_attr_temp0.attr, 346 &dev_attr_temp1.attr, 347 &dev_attr_in0_input.attr, 348 &dev_attr_in1_input.attr, 349 NULL, 350 }; 351 352 static struct attribute_group ads7846_attr_group = { 353 .attrs = ads7846_attributes, 354 }; 355 356 static struct attribute *ads7843_attributes[] = { 357 &dev_attr_in0_input.attr, 358 &dev_attr_in1_input.attr, 359 NULL, 360 }; 361 362 static struct attribute_group ads7843_attr_group = { 363 .attrs = ads7843_attributes, 364 }; 365 366 static struct attribute *ads7845_attributes[] = { 367 &dev_attr_in0_input.attr, 368 NULL, 369 }; 370 371 static struct attribute_group ads7845_attr_group = { 372 .attrs = ads7845_attributes, 373 }; 374 375 static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts) 376 { 377 struct device *hwmon; 378 int err; 379 380 /* hwmon sensors need a reference voltage */ 381 switch (ts->model) { 382 case 7846: 383 if (!ts->vref_mv) { 384 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n"); 385 ts->vref_mv = 2500; 386 } 387 break; 388 case 7845: 389 case 7843: 390 if (!ts->vref_mv) { 391 dev_warn(&spi->dev, 392 "external vREF for ADS%d not specified\n", 393 ts->model); 394 return 0; 395 } 396 break; 397 } 398 399 /* different chips have different sensor groups */ 400 switch (ts->model) { 401 case 7846: 402 ts->attr_group = &ads7846_attr_group; 403 break; 404 case 7845: 405 ts->attr_group = &ads7845_attr_group; 406 break; 407 case 7843: 408 ts->attr_group = &ads7843_attr_group; 409 break; 410 default: 411 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model); 412 return 0; 413 } 414 415 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group); 416 if (err) 417 return err; 418 419 hwmon = hwmon_device_register(&spi->dev); 420 if (IS_ERR(hwmon)) { 421 sysfs_remove_group(&spi->dev.kobj, ts->attr_group); 422 return PTR_ERR(hwmon); 423 } 424 425 ts->hwmon = hwmon; 426 return 0; 427 } 428 429 static void ads784x_hwmon_unregister(struct spi_device *spi, 430 struct ads7846 *ts) 431 { 432 if (ts->hwmon) { 433 sysfs_remove_group(&spi->dev.kobj, ts->attr_group); 434 hwmon_device_unregister(ts->hwmon); 435 } 436 } 437 438 #else 439 static inline int ads784x_hwmon_register(struct spi_device *spi, 440 struct ads7846 *ts) 441 { 442 return 0; 443 } 444 445 static inline void ads784x_hwmon_unregister(struct spi_device *spi, 446 struct ads7846 *ts) 447 { 448 } 449 #endif 450 451 static int is_pen_down(struct device *dev) 452 { 453 struct ads7846 *ts = dev_get_drvdata(dev); 454 455 return ts->pendown; 456 } 457 458 static ssize_t ads7846_pen_down_show(struct device *dev, 459 struct device_attribute *attr, char *buf) 460 { 461 return sprintf(buf, "%u\n", is_pen_down(dev)); 462 } 463 464 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL); 465 466 static ssize_t ads7846_disable_show(struct device *dev, 467 struct device_attribute *attr, char *buf) 468 { 469 struct ads7846 *ts = dev_get_drvdata(dev); 470 471 return sprintf(buf, "%u\n", ts->disabled); 472 } 473 474 static ssize_t ads7846_disable_store(struct device *dev, 475 struct device_attribute *attr, 476 const char *buf, size_t count) 477 { 478 struct ads7846 *ts = dev_get_drvdata(dev); 479 unsigned long i; 480 481 if (strict_strtoul(buf, 10, &i)) 482 return -EINVAL; 483 484 spin_lock_irq(&ts->lock); 485 486 if (i) 487 ads7846_disable(ts); 488 else 489 ads7846_enable(ts); 490 491 spin_unlock_irq(&ts->lock); 492 493 return count; 494 } 495 496 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store); 497 498 static struct attribute *ads784x_attributes[] = { 499 &dev_attr_pen_down.attr, 500 &dev_attr_disable.attr, 501 NULL, 502 }; 503 504 static struct attribute_group ads784x_attr_group = { 505 .attrs = ads784x_attributes, 506 }; 507 508 /*--------------------------------------------------------------------------*/ 509 510 static int get_pendown_state(struct ads7846 *ts) 511 { 512 if (ts->get_pendown_state) 513 return ts->get_pendown_state(); 514 515 return !gpio_get_value(ts->gpio_pendown); 516 } 517 518 static void null_wait_for_sync(void) 519 { 520 } 521 522 /* 523 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer, 524 * to retrieve touchscreen status. 525 * 526 * The SPI transfer completion callback does the real work. It reports 527 * touchscreen events and reactivates the timer (or IRQ) as appropriate. 528 */ 529 530 static void ads7846_rx(void *ads) 531 { 532 struct ads7846 *ts = ads; 533 struct ads7846_packet *packet = ts->packet; 534 unsigned Rt; 535 u16 x, y, z1, z2; 536 537 /* ads7846_rx_val() did in-place conversion (including byteswap) from 538 * on-the-wire format as part of debouncing to get stable readings. 539 */ 540 x = packet->tc.x; 541 y = packet->tc.y; 542 z1 = packet->tc.z1; 543 z2 = packet->tc.z2; 544 545 /* range filtering */ 546 if (x == MAX_12BIT) 547 x = 0; 548 549 if (ts->model == 7843) { 550 Rt = ts->pressure_max / 2; 551 } else if (likely(x && z1)) { 552 /* compute touch pressure resistance using equation #2 */ 553 Rt = z2; 554 Rt -= z1; 555 Rt *= x; 556 Rt *= ts->x_plate_ohms; 557 Rt /= z1; 558 Rt = (Rt + 2047) >> 12; 559 } else { 560 Rt = 0; 561 } 562 563 /* Sample found inconsistent by debouncing or pressure is beyond 564 * the maximum. Don't report it to user space, repeat at least 565 * once more the measurement 566 */ 567 if (packet->tc.ignore || Rt > ts->pressure_max) { 568 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n", 569 packet->tc.ignore, Rt); 570 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD), 571 HRTIMER_MODE_REL); 572 return; 573 } 574 575 /* Maybe check the pendown state before reporting. This discards 576 * false readings when the pen is lifted. 577 */ 578 if (ts->penirq_recheck_delay_usecs) { 579 udelay(ts->penirq_recheck_delay_usecs); 580 if (!get_pendown_state(ts)) 581 Rt = 0; 582 } 583 584 /* NOTE: We can't rely on the pressure to determine the pen down 585 * state, even this controller has a pressure sensor. The pressure 586 * value can fluctuate for quite a while after lifting the pen and 587 * in some cases may not even settle at the expected value. 588 * 589 * The only safe way to check for the pen up condition is in the 590 * timer by reading the pen signal state (it's a GPIO _and_ IRQ). 591 */ 592 if (Rt) { 593 struct input_dev *input = ts->input; 594 595 if (!ts->pendown) { 596 input_report_key(input, BTN_TOUCH, 1); 597 ts->pendown = 1; 598 dev_vdbg(&ts->spi->dev, "DOWN\n"); 599 } 600 601 if (ts->swap_xy) 602 swap(x, y); 603 604 input_report_abs(input, ABS_X, x); 605 input_report_abs(input, ABS_Y, y); 606 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt); 607 608 input_sync(input); 609 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt); 610 } 611 612 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD), 613 HRTIMER_MODE_REL); 614 } 615 616 static int ads7846_debounce(void *ads, int data_idx, int *val) 617 { 618 struct ads7846 *ts = ads; 619 620 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) { 621 /* Start over collecting consistent readings. */ 622 ts->read_rep = 0; 623 /* Repeat it, if this was the first read or the read 624 * wasn't consistent enough. */ 625 if (ts->read_cnt < ts->debounce_max) { 626 ts->last_read = *val; 627 ts->read_cnt++; 628 return ADS7846_FILTER_REPEAT; 629 } else { 630 /* Maximum number of debouncing reached and still 631 * not enough number of consistent readings. Abort 632 * the whole sample, repeat it in the next sampling 633 * period. 634 */ 635 ts->read_cnt = 0; 636 return ADS7846_FILTER_IGNORE; 637 } 638 } else { 639 if (++ts->read_rep > ts->debounce_rep) { 640 /* Got a good reading for this coordinate, 641 * go for the next one. */ 642 ts->read_cnt = 0; 643 ts->read_rep = 0; 644 return ADS7846_FILTER_OK; 645 } else { 646 /* Read more values that are consistent. */ 647 ts->read_cnt++; 648 return ADS7846_FILTER_REPEAT; 649 } 650 } 651 } 652 653 static int ads7846_no_filter(void *ads, int data_idx, int *val) 654 { 655 return ADS7846_FILTER_OK; 656 } 657 658 static void ads7846_rx_val(void *ads) 659 { 660 struct ads7846 *ts = ads; 661 struct ads7846_packet *packet = ts->packet; 662 struct spi_message *m; 663 struct spi_transfer *t; 664 int val; 665 int action; 666 int status; 667 668 m = &ts->msg[ts->msg_idx]; 669 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list); 670 671 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding; 672 * built from two 8 bit values written msb-first. 673 */ 674 val = be16_to_cpup((__be16 *)t->rx_buf) >> 3; 675 676 action = ts->filter(ts->filter_data, ts->msg_idx, &val); 677 switch (action) { 678 case ADS7846_FILTER_REPEAT: 679 break; 680 case ADS7846_FILTER_IGNORE: 681 packet->tc.ignore = 1; 682 /* Last message will contain ads7846_rx() as the 683 * completion function. 684 */ 685 m = ts->last_msg; 686 break; 687 case ADS7846_FILTER_OK: 688 *(u16 *)t->rx_buf = val; 689 packet->tc.ignore = 0; 690 m = &ts->msg[++ts->msg_idx]; 691 break; 692 default: 693 BUG(); 694 } 695 ts->wait_for_sync(); 696 status = spi_async(ts->spi, m); 697 if (status) 698 dev_err(&ts->spi->dev, "spi_async --> %d\n", 699 status); 700 } 701 702 static enum hrtimer_restart ads7846_timer(struct hrtimer *handle) 703 { 704 struct ads7846 *ts = container_of(handle, struct ads7846, timer); 705 int status = 0; 706 707 spin_lock(&ts->lock); 708 709 if (unlikely(!get_pendown_state(ts) || 710 device_suspended(&ts->spi->dev))) { 711 if (ts->pendown) { 712 struct input_dev *input = ts->input; 713 714 input_report_key(input, BTN_TOUCH, 0); 715 input_report_abs(input, ABS_PRESSURE, 0); 716 input_sync(input); 717 718 ts->pendown = 0; 719 dev_vdbg(&ts->spi->dev, "UP\n"); 720 } 721 722 /* measurement cycle ended */ 723 if (!device_suspended(&ts->spi->dev)) { 724 ts->irq_disabled = 0; 725 enable_irq(ts->spi->irq); 726 } 727 ts->pending = 0; 728 } else { 729 /* pen is still down, continue with the measurement */ 730 ts->msg_idx = 0; 731 ts->wait_for_sync(); 732 status = spi_async(ts->spi, &ts->msg[0]); 733 if (status) 734 dev_err(&ts->spi->dev, "spi_async --> %d\n", status); 735 } 736 737 spin_unlock(&ts->lock); 738 return HRTIMER_NORESTART; 739 } 740 741 static irqreturn_t ads7846_irq(int irq, void *handle) 742 { 743 struct ads7846 *ts = handle; 744 unsigned long flags; 745 746 spin_lock_irqsave(&ts->lock, flags); 747 if (likely(get_pendown_state(ts))) { 748 if (!ts->irq_disabled) { 749 /* The ARM do_simple_IRQ() dispatcher doesn't act 750 * like the other dispatchers: it will report IRQs 751 * even after they've been disabled. We work around 752 * that here. (The "generic irq" framework may help...) 753 */ 754 ts->irq_disabled = 1; 755 disable_irq_nosync(ts->spi->irq); 756 ts->pending = 1; 757 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY), 758 HRTIMER_MODE_REL); 759 } 760 } 761 spin_unlock_irqrestore(&ts->lock, flags); 762 763 return IRQ_HANDLED; 764 } 765 766 /*--------------------------------------------------------------------------*/ 767 768 /* Must be called with ts->lock held */ 769 static void ads7846_disable(struct ads7846 *ts) 770 { 771 if (ts->disabled) 772 return; 773 774 ts->disabled = 1; 775 776 /* are we waiting for IRQ, or polling? */ 777 if (!ts->pending) { 778 ts->irq_disabled = 1; 779 disable_irq(ts->spi->irq); 780 } else { 781 /* the timer will run at least once more, and 782 * leave everything in a clean state, IRQ disabled 783 */ 784 while (ts->pending) { 785 spin_unlock_irq(&ts->lock); 786 msleep(1); 787 spin_lock_irq(&ts->lock); 788 } 789 } 790 791 /* we know the chip's in lowpower mode since we always 792 * leave it that way after every request 793 */ 794 } 795 796 /* Must be called with ts->lock held */ 797 static void ads7846_enable(struct ads7846 *ts) 798 { 799 if (!ts->disabled) 800 return; 801 802 ts->disabled = 0; 803 ts->irq_disabled = 0; 804 enable_irq(ts->spi->irq); 805 } 806 807 static int ads7846_suspend(struct spi_device *spi, pm_message_t message) 808 { 809 struct ads7846 *ts = dev_get_drvdata(&spi->dev); 810 811 spin_lock_irq(&ts->lock); 812 813 ts->is_suspended = 1; 814 ads7846_disable(ts); 815 816 spin_unlock_irq(&ts->lock); 817 818 return 0; 819 820 } 821 822 static int ads7846_resume(struct spi_device *spi) 823 { 824 struct ads7846 *ts = dev_get_drvdata(&spi->dev); 825 826 spin_lock_irq(&ts->lock); 827 828 ts->is_suspended = 0; 829 ads7846_enable(ts); 830 831 spin_unlock_irq(&ts->lock); 832 833 return 0; 834 } 835 836 static int __devinit setup_pendown(struct spi_device *spi, struct ads7846 *ts) 837 { 838 struct ads7846_platform_data *pdata = spi->dev.platform_data; 839 int err; 840 841 /* REVISIT when the irq can be triggered active-low, or if for some 842 * reason the touchscreen isn't hooked up, we don't need to access 843 * the pendown state. 844 */ 845 if (!pdata->get_pendown_state && !gpio_is_valid(pdata->gpio_pendown)) { 846 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n"); 847 return -EINVAL; 848 } 849 850 if (pdata->get_pendown_state) { 851 ts->get_pendown_state = pdata->get_pendown_state; 852 return 0; 853 } 854 855 err = gpio_request(pdata->gpio_pendown, "ads7846_pendown"); 856 if (err) { 857 dev_err(&spi->dev, "failed to request pendown GPIO%d\n", 858 pdata->gpio_pendown); 859 return err; 860 } 861 862 ts->gpio_pendown = pdata->gpio_pendown; 863 return 0; 864 } 865 866 static int __devinit ads7846_probe(struct spi_device *spi) 867 { 868 struct ads7846 *ts; 869 struct ads7846_packet *packet; 870 struct input_dev *input_dev; 871 struct ads7846_platform_data *pdata = spi->dev.platform_data; 872 struct spi_message *m; 873 struct spi_transfer *x; 874 int vref; 875 int err; 876 877 if (!spi->irq) { 878 dev_dbg(&spi->dev, "no IRQ?\n"); 879 return -ENODEV; 880 } 881 882 if (!pdata) { 883 dev_dbg(&spi->dev, "no platform data?\n"); 884 return -ENODEV; 885 } 886 887 /* don't exceed max specified sample rate */ 888 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) { 889 dev_dbg(&spi->dev, "f(sample) %d KHz?\n", 890 (spi->max_speed_hz/SAMPLE_BITS)/1000); 891 return -EINVAL; 892 } 893 894 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except 895 * that even if the hardware can do that, the SPI controller driver 896 * may not. So we stick to very-portable 8 bit words, both RX and TX. 897 */ 898 spi->bits_per_word = 8; 899 spi->mode = SPI_MODE_0; 900 err = spi_setup(spi); 901 if (err < 0) 902 return err; 903 904 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL); 905 packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL); 906 input_dev = input_allocate_device(); 907 if (!ts || !packet || !input_dev) { 908 err = -ENOMEM; 909 goto err_free_mem; 910 } 911 912 dev_set_drvdata(&spi->dev, ts); 913 914 ts->packet = packet; 915 ts->spi = spi; 916 ts->input = input_dev; 917 ts->vref_mv = pdata->vref_mv; 918 ts->swap_xy = pdata->swap_xy; 919 920 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 921 ts->timer.function = ads7846_timer; 922 923 spin_lock_init(&ts->lock); 924 925 ts->model = pdata->model ? : 7846; 926 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100; 927 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400; 928 ts->pressure_max = pdata->pressure_max ? : ~0; 929 930 if (pdata->filter != NULL) { 931 if (pdata->filter_init != NULL) { 932 err = pdata->filter_init(pdata, &ts->filter_data); 933 if (err < 0) 934 goto err_free_mem; 935 } 936 ts->filter = pdata->filter; 937 ts->filter_cleanup = pdata->filter_cleanup; 938 } else if (pdata->debounce_max) { 939 ts->debounce_max = pdata->debounce_max; 940 if (ts->debounce_max < 2) 941 ts->debounce_max = 2; 942 ts->debounce_tol = pdata->debounce_tol; 943 ts->debounce_rep = pdata->debounce_rep; 944 ts->filter = ads7846_debounce; 945 ts->filter_data = ts; 946 } else 947 ts->filter = ads7846_no_filter; 948 949 err = setup_pendown(spi, ts); 950 if (err) 951 goto err_cleanup_filter; 952 953 if (pdata->penirq_recheck_delay_usecs) 954 ts->penirq_recheck_delay_usecs = 955 pdata->penirq_recheck_delay_usecs; 956 957 ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync; 958 959 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev)); 960 snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model); 961 962 input_dev->name = ts->name; 963 input_dev->phys = ts->phys; 964 input_dev->dev.parent = &spi->dev; 965 966 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); 967 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); 968 input_set_abs_params(input_dev, ABS_X, 969 pdata->x_min ? : 0, 970 pdata->x_max ? : MAX_12BIT, 971 0, 0); 972 input_set_abs_params(input_dev, ABS_Y, 973 pdata->y_min ? : 0, 974 pdata->y_max ? : MAX_12BIT, 975 0, 0); 976 input_set_abs_params(input_dev, ABS_PRESSURE, 977 pdata->pressure_min, pdata->pressure_max, 0, 0); 978 979 vref = pdata->keep_vref_on; 980 981 /* set up the transfers to read touchscreen state; this assumes we 982 * use formula #2 for pressure, not #3. 983 */ 984 m = &ts->msg[0]; 985 x = ts->xfer; 986 987 spi_message_init(m); 988 989 /* y- still on; turn on only y+ (and ADC) */ 990 packet->read_y = READ_Y(vref); 991 x->tx_buf = &packet->read_y; 992 x->len = 1; 993 spi_message_add_tail(x, m); 994 995 x++; 996 x->rx_buf = &packet->tc.y; 997 x->len = 2; 998 spi_message_add_tail(x, m); 999 1000 /* the first sample after switching drivers can be low quality; 1001 * optionally discard it, using a second one after the signals 1002 * have had enough time to stabilize. 1003 */ 1004 if (pdata->settle_delay_usecs) { 1005 x->delay_usecs = pdata->settle_delay_usecs; 1006 1007 x++; 1008 x->tx_buf = &packet->read_y; 1009 x->len = 1; 1010 spi_message_add_tail(x, m); 1011 1012 x++; 1013 x->rx_buf = &packet->tc.y; 1014 x->len = 2; 1015 spi_message_add_tail(x, m); 1016 } 1017 1018 m->complete = ads7846_rx_val; 1019 m->context = ts; 1020 1021 m++; 1022 spi_message_init(m); 1023 1024 /* turn y- off, x+ on, then leave in lowpower */ 1025 x++; 1026 packet->read_x = READ_X(vref); 1027 x->tx_buf = &packet->read_x; 1028 x->len = 1; 1029 spi_message_add_tail(x, m); 1030 1031 x++; 1032 x->rx_buf = &packet->tc.x; 1033 x->len = 2; 1034 spi_message_add_tail(x, m); 1035 1036 /* ... maybe discard first sample ... */ 1037 if (pdata->settle_delay_usecs) { 1038 x->delay_usecs = pdata->settle_delay_usecs; 1039 1040 x++; 1041 x->tx_buf = &packet->read_x; 1042 x->len = 1; 1043 spi_message_add_tail(x, m); 1044 1045 x++; 1046 x->rx_buf = &packet->tc.x; 1047 x->len = 2; 1048 spi_message_add_tail(x, m); 1049 } 1050 1051 m->complete = ads7846_rx_val; 1052 m->context = ts; 1053 1054 /* turn y+ off, x- on; we'll use formula #2 */ 1055 if (ts->model == 7846) { 1056 m++; 1057 spi_message_init(m); 1058 1059 x++; 1060 packet->read_z1 = READ_Z1(vref); 1061 x->tx_buf = &packet->read_z1; 1062 x->len = 1; 1063 spi_message_add_tail(x, m); 1064 1065 x++; 1066 x->rx_buf = &packet->tc.z1; 1067 x->len = 2; 1068 spi_message_add_tail(x, m); 1069 1070 /* ... maybe discard first sample ... */ 1071 if (pdata->settle_delay_usecs) { 1072 x->delay_usecs = pdata->settle_delay_usecs; 1073 1074 x++; 1075 x->tx_buf = &packet->read_z1; 1076 x->len = 1; 1077 spi_message_add_tail(x, m); 1078 1079 x++; 1080 x->rx_buf = &packet->tc.z1; 1081 x->len = 2; 1082 spi_message_add_tail(x, m); 1083 } 1084 1085 m->complete = ads7846_rx_val; 1086 m->context = ts; 1087 1088 m++; 1089 spi_message_init(m); 1090 1091 x++; 1092 packet->read_z2 = READ_Z2(vref); 1093 x->tx_buf = &packet->read_z2; 1094 x->len = 1; 1095 spi_message_add_tail(x, m); 1096 1097 x++; 1098 x->rx_buf = &packet->tc.z2; 1099 x->len = 2; 1100 spi_message_add_tail(x, m); 1101 1102 /* ... maybe discard first sample ... */ 1103 if (pdata->settle_delay_usecs) { 1104 x->delay_usecs = pdata->settle_delay_usecs; 1105 1106 x++; 1107 x->tx_buf = &packet->read_z2; 1108 x->len = 1; 1109 spi_message_add_tail(x, m); 1110 1111 x++; 1112 x->rx_buf = &packet->tc.z2; 1113 x->len = 2; 1114 spi_message_add_tail(x, m); 1115 } 1116 1117 m->complete = ads7846_rx_val; 1118 m->context = ts; 1119 } 1120 1121 /* power down */ 1122 m++; 1123 spi_message_init(m); 1124 1125 x++; 1126 packet->pwrdown = PWRDOWN; 1127 x->tx_buf = &packet->pwrdown; 1128 x->len = 1; 1129 spi_message_add_tail(x, m); 1130 1131 x++; 1132 x->rx_buf = &packet->dummy; 1133 x->len = 2; 1134 CS_CHANGE(*x); 1135 spi_message_add_tail(x, m); 1136 1137 m->complete = ads7846_rx; 1138 m->context = ts; 1139 1140 ts->last_msg = m; 1141 1142 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING, 1143 spi->dev.driver->name, ts)) { 1144 dev_info(&spi->dev, 1145 "trying pin change workaround on irq %d\n", spi->irq); 1146 err = request_irq(spi->irq, ads7846_irq, 1147 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, 1148 spi->dev.driver->name, ts); 1149 if (err) { 1150 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq); 1151 goto err_free_gpio; 1152 } 1153 } 1154 1155 err = ads784x_hwmon_register(spi, ts); 1156 if (err) 1157 goto err_free_irq; 1158 1159 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq); 1160 1161 /* take a first sample, leaving nPENIRQ active and vREF off; avoid 1162 * the touchscreen, in case it's not connected. 1163 */ 1164 (void) ads7846_read12_ser(&spi->dev, 1165 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON); 1166 1167 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group); 1168 if (err) 1169 goto err_remove_hwmon; 1170 1171 err = input_register_device(input_dev); 1172 if (err) 1173 goto err_remove_attr_group; 1174 1175 return 0; 1176 1177 err_remove_attr_group: 1178 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group); 1179 err_remove_hwmon: 1180 ads784x_hwmon_unregister(spi, ts); 1181 err_free_irq: 1182 free_irq(spi->irq, ts); 1183 err_free_gpio: 1184 if (ts->gpio_pendown != -1) 1185 gpio_free(ts->gpio_pendown); 1186 err_cleanup_filter: 1187 if (ts->filter_cleanup) 1188 ts->filter_cleanup(ts->filter_data); 1189 err_free_mem: 1190 input_free_device(input_dev); 1191 kfree(packet); 1192 kfree(ts); 1193 return err; 1194 } 1195 1196 static int __devexit ads7846_remove(struct spi_device *spi) 1197 { 1198 struct ads7846 *ts = dev_get_drvdata(&spi->dev); 1199 1200 ads784x_hwmon_unregister(spi, ts); 1201 input_unregister_device(ts->input); 1202 1203 ads7846_suspend(spi, PMSG_SUSPEND); 1204 1205 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group); 1206 1207 free_irq(ts->spi->irq, ts); 1208 /* suspend left the IRQ disabled */ 1209 enable_irq(ts->spi->irq); 1210 1211 if (ts->gpio_pendown != -1) 1212 gpio_free(ts->gpio_pendown); 1213 1214 if (ts->filter_cleanup) 1215 ts->filter_cleanup(ts->filter_data); 1216 1217 kfree(ts->packet); 1218 kfree(ts); 1219 1220 dev_dbg(&spi->dev, "unregistered touchscreen\n"); 1221 return 0; 1222 } 1223 1224 static struct spi_driver ads7846_driver = { 1225 .driver = { 1226 .name = "ads7846", 1227 .bus = &spi_bus_type, 1228 .owner = THIS_MODULE, 1229 }, 1230 .probe = ads7846_probe, 1231 .remove = __devexit_p(ads7846_remove), 1232 .suspend = ads7846_suspend, 1233 .resume = ads7846_resume, 1234 }; 1235 1236 static int __init ads7846_init(void) 1237 { 1238 return spi_register_driver(&ads7846_driver); 1239 } 1240 module_init(ads7846_init); 1241 1242 static void __exit ads7846_exit(void) 1243 { 1244 spi_unregister_driver(&ads7846_driver); 1245 } 1246 module_exit(ads7846_exit); 1247 1248 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver"); 1249 MODULE_LICENSE("GPL"); 1250 MODULE_ALIAS("spi:ads7846"); 1251