1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ADS7846 based touchscreen and sensor driver 4 * 5 * Copyright (c) 2005 David Brownell 6 * Copyright (c) 2006 Nokia Corporation 7 * Various changes: Imre Deak <imre.deak@nokia.com> 8 * 9 * Using code from: 10 * - corgi_ts.c 11 * Copyright (C) 2004-2005 Richard Purdie 12 * - omap_ts.[hc], ads7846.h, ts_osk.c 13 * Copyright (C) 2002 MontaVista Software 14 * Copyright (C) 2004 Texas Instruments 15 * Copyright (C) 2005 Dirk Behme 16 */ 17 #include <linux/types.h> 18 #include <linux/hwmon.h> 19 #include <linux/err.h> 20 #include <linux/sched.h> 21 #include <linux/delay.h> 22 #include <linux/input.h> 23 #include <linux/input/touchscreen.h> 24 #include <linux/interrupt.h> 25 #include <linux/slab.h> 26 #include <linux/pm.h> 27 #include <linux/property.h> 28 #include <linux/gpio/consumer.h> 29 #include <linux/spi/spi.h> 30 #include <linux/spi/ads7846.h> 31 #include <linux/regulator/consumer.h> 32 #include <linux/module.h> 33 #include <linux/unaligned.h> 34 35 /* 36 * This code has been heavily tested on a Nokia 770, and lightly 37 * tested on other ads7846 devices (OSK/Mistral, Lubbock, Spitz). 38 * TSC2046 is just newer ads7846 silicon. 39 * Support for ads7843 tested on Atmel at91sam926x-EK. 40 * Support for ads7845 has only been stubbed in. 41 * Support for Analog Devices AD7873 and AD7843 tested. 42 * 43 * IRQ handling needs a workaround because of a shortcoming in handling 44 * edge triggered IRQs on some platforms like the OMAP1/2. These 45 * platforms don't handle the ARM lazy IRQ disabling properly, thus we 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. 48 * 49 * App note sbaa036 talks in more detail about accurate sampling... 50 * that ought to help in situations like LCDs inducing noise (which 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. 55 */ 56 57 #define TS_POLL_DELAY 1 /* ms delay before the first sample */ 58 #define TS_POLL_PERIOD 5 /* ms delay between samples */ 59 60 /* this driver doesn't aim at the peak continuous sample rate */ 61 #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */) 62 63 struct ads7846_buf { 64 u8 cmd; 65 __be16 data; 66 } __packed; 67 68 struct ads7846_buf_layout { 69 unsigned int offset; 70 unsigned int count; 71 unsigned int skip; 72 }; 73 74 /* 75 * We allocate this separately to avoid cache line sharing issues when 76 * driver is used with DMA-based SPI controllers (like atmel_spi) on 77 * systems where main memory is not DMA-coherent (most non-x86 boards). 78 */ 79 struct ads7846_packet { 80 unsigned int count; 81 unsigned int count_skip; 82 unsigned int cmds; 83 unsigned int last_cmd_idx; 84 struct ads7846_buf_layout l[5]; 85 struct ads7846_buf *rx; 86 struct ads7846_buf *tx; 87 88 struct ads7846_buf pwrdown_cmd; 89 90 bool ignore; 91 u16 x, y, z1, z2; 92 }; 93 94 struct ads7846 { 95 struct input_dev *input; 96 char phys[32]; 97 char name[32]; 98 99 struct spi_device *spi; 100 struct regulator *reg; 101 102 u16 model; 103 u16 vref_mv; 104 u16 vref_delay_usecs; 105 u16 x_plate_ohms; 106 u16 pressure_max; 107 108 bool swap_xy; 109 bool use_internal; 110 111 struct ads7846_packet *packet; 112 113 struct spi_transfer xfer[18]; 114 struct spi_message msg[5]; 115 int msg_count; 116 wait_queue_head_t wait; 117 118 bool pendown; 119 120 int read_cnt; 121 int read_rep; 122 int last_read; 123 124 u16 debounce_max; 125 u16 debounce_tol; 126 u16 debounce_rep; 127 128 u16 penirq_recheck_delay_usecs; 129 130 struct touchscreen_properties core_prop; 131 132 struct mutex lock; 133 bool stopped; /* P: lock */ 134 bool disabled; /* P: lock */ 135 bool suspended; /* P: lock */ 136 137 int (*setup_spi_msg)(struct ads7846 *ts, 138 const struct ads7846_platform_data *pdata); 139 void (*read_state)(struct ads7846 *ts); 140 int (*filter)(void *data, int data_idx, int *val); 141 void *filter_data; 142 int (*get_pendown_state)(void); 143 struct gpio_desc *gpio_pendown; 144 struct gpio_desc *gpio_hsync; 145 146 void (*wait_for_sync)(void); 147 }; 148 149 enum ads7846_filter { 150 ADS7846_FILTER_OK, 151 ADS7846_FILTER_REPEAT, 152 ADS7846_FILTER_IGNORE, 153 }; 154 155 /* leave chip selected when we're done, for quicker re-select? */ 156 #if 0 157 #define CS_CHANGE(xfer) ((xfer).cs_change = 1) 158 #else 159 #define CS_CHANGE(xfer) ((xfer).cs_change = 0) 160 #endif 161 162 /*--------------------------------------------------------------------------*/ 163 164 /* The ADS7846 has touchscreen and other sensors. 165 * Earlier ads784x chips are somewhat compatible. 166 */ 167 #define ADS_START (1 << 7) 168 #define ADS_A2A1A0_d_y (1 << 4) /* differential */ 169 #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */ 170 #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */ 171 #define ADS_A2A1A0_d_x (5 << 4) /* differential */ 172 #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */ 173 #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */ 174 #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */ 175 #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */ 176 #define ADS_8_BIT (1 << 3) 177 #define ADS_12_BIT (0 << 3) 178 #define ADS_SER (1 << 2) /* non-differential */ 179 #define ADS_DFR (0 << 2) /* differential */ 180 #define ADS_PD10_PDOWN (0 << 0) /* low power mode + penirq */ 181 #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */ 182 #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */ 183 #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */ 184 185 #define MAX_12BIT ((1<<12)-1) 186 187 /* leave ADC powered up (disables penirq) between differential samples */ 188 #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \ 189 | ADS_12_BIT | ADS_DFR | \ 190 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0)) 191 192 #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref)) 193 #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref)) 194 #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref)) 195 #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref)) 196 #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */ 197 198 /* single-ended samples need to first power up reference voltage; 199 * we leave both ADC and VREF powered 200 */ 201 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \ 202 | ADS_12_BIT | ADS_SER) 203 204 #define REF_ON (READ_12BIT_DFR(x, 1, 1)) 205 #define REF_OFF (READ_12BIT_DFR(y, 0, 0)) 206 207 /* Order commands in the most optimal way to reduce Vref switching and 208 * settling time: 209 * Measure: X; Vref: X+, X-; IN: Y+ 210 * Measure: Y; Vref: Y+, Y-; IN: X+ 211 * Measure: Z1; Vref: Y+, X-; IN: X+ 212 * Measure: Z2; Vref: Y+, X-; IN: Y- 213 */ 214 enum ads7846_cmds { 215 ADS7846_X, 216 ADS7846_Y, 217 ADS7846_Z1, 218 ADS7846_Z2, 219 ADS7846_PWDOWN, 220 }; 221 222 static int get_pendown_state(struct ads7846 *ts) 223 { 224 if (ts->get_pendown_state) 225 return ts->get_pendown_state(); 226 227 return gpiod_get_value(ts->gpio_pendown); 228 } 229 230 static void ads7846_report_pen_up(struct ads7846 *ts) 231 { 232 struct input_dev *input = ts->input; 233 234 input_report_key(input, BTN_TOUCH, 0); 235 input_report_abs(input, ABS_PRESSURE, 0); 236 input_sync(input); 237 238 ts->pendown = false; 239 dev_vdbg(&ts->spi->dev, "UP\n"); 240 } 241 242 /* Must be called with ts->lock held */ 243 static void ads7846_stop(struct ads7846 *ts) 244 { 245 if (!ts->disabled && !ts->suspended) { 246 /* Signal IRQ thread to stop polling and disable the handler. */ 247 ts->stopped = true; 248 mb(); 249 wake_up(&ts->wait); 250 disable_irq(ts->spi->irq); 251 } 252 } 253 254 /* Must be called with ts->lock held */ 255 static void ads7846_restart(struct ads7846 *ts) 256 { 257 if (!ts->disabled && !ts->suspended) { 258 /* Check if pen was released since last stop */ 259 if (ts->pendown && !get_pendown_state(ts)) 260 ads7846_report_pen_up(ts); 261 262 /* Tell IRQ thread that it may poll the device. */ 263 ts->stopped = false; 264 mb(); 265 enable_irq(ts->spi->irq); 266 } 267 } 268 269 /* Must be called with ts->lock held */ 270 static void __ads7846_disable(struct ads7846 *ts) 271 { 272 ads7846_stop(ts); 273 regulator_disable(ts->reg); 274 275 /* 276 * We know the chip's in low power mode since we always 277 * leave it that way after every request 278 */ 279 } 280 281 /* Must be called with ts->lock held */ 282 static void __ads7846_enable(struct ads7846 *ts) 283 { 284 int error; 285 286 error = regulator_enable(ts->reg); 287 if (error != 0) 288 dev_err(&ts->spi->dev, "Failed to enable supply: %d\n", error); 289 290 ads7846_restart(ts); 291 } 292 293 static void ads7846_disable(struct ads7846 *ts) 294 { 295 guard(mutex)(&ts->lock); 296 297 if (!ts->disabled) { 298 299 if (!ts->suspended) 300 __ads7846_disable(ts); 301 302 ts->disabled = true; 303 } 304 } 305 306 static void ads7846_enable(struct ads7846 *ts) 307 { 308 guard(mutex)(&ts->lock); 309 310 if (ts->disabled) { 311 312 ts->disabled = false; 313 314 if (!ts->suspended) 315 __ads7846_enable(ts); 316 } 317 } 318 319 /*--------------------------------------------------------------------------*/ 320 321 /* 322 * Non-touchscreen sensors only use single-ended conversions. 323 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF; 324 * ads7846 lets that pin be unconnected, to use internal vREF. 325 */ 326 327 struct ser_req { 328 u8 ref_on; 329 u8 command; 330 u8 ref_off; 331 struct spi_message msg; 332 struct spi_transfer xfer[8]; 333 /* 334 * DMA (thus cache coherency maintenance) requires the 335 * transfer buffers to live in their own cache lines. 336 */ 337 __be16 sample ____cacheline_aligned; 338 u16 scratch; 339 }; 340 341 struct ads7845_ser_req { 342 u8 command[3]; 343 struct spi_message msg; 344 struct spi_transfer xfer[2]; 345 /* 346 * DMA (thus cache coherency maintenance) requires the 347 * transfer buffers to live in their own cache lines. 348 */ 349 u8 sample[3] ____cacheline_aligned; 350 }; 351 352 static int ads7846_read12_ser(struct device *dev, unsigned command) 353 { 354 struct spi_device *spi = to_spi_device(dev); 355 struct ads7846 *ts = dev_get_drvdata(dev); 356 int status; 357 358 struct ser_req *req __free(kfree) = kzalloc_obj(*req); 359 if (!req) 360 return -ENOMEM; 361 362 spi_message_init(&req->msg); 363 364 /* maybe turn on internal vREF, and let it settle */ 365 if (ts->use_internal) { 366 req->ref_on = REF_ON; 367 req->xfer[0].tx_buf = &req->ref_on; 368 req->xfer[0].len = 1; 369 spi_message_add_tail(&req->xfer[0], &req->msg); 370 371 req->xfer[1].rx_buf = &req->scratch; 372 req->xfer[1].len = 2; 373 374 /* for 1uF, settle for 800 usec; no cap, 100 usec. */ 375 req->xfer[1].delay.value = ts->vref_delay_usecs; 376 req->xfer[1].delay.unit = SPI_DELAY_UNIT_USECS; 377 spi_message_add_tail(&req->xfer[1], &req->msg); 378 379 /* Enable reference voltage */ 380 command |= ADS_PD10_REF_ON; 381 } 382 383 /* Enable ADC in every case */ 384 command |= ADS_PD10_ADC_ON; 385 386 /* take sample */ 387 req->command = (u8) command; 388 req->xfer[2].tx_buf = &req->command; 389 req->xfer[2].len = 1; 390 spi_message_add_tail(&req->xfer[2], &req->msg); 391 392 req->xfer[3].rx_buf = &req->sample; 393 req->xfer[3].len = 2; 394 spi_message_add_tail(&req->xfer[3], &req->msg); 395 396 /* REVISIT: take a few more samples, and compare ... */ 397 398 /* converter in low power mode & enable PENIRQ */ 399 req->ref_off = PWRDOWN; 400 req->xfer[4].tx_buf = &req->ref_off; 401 req->xfer[4].len = 1; 402 spi_message_add_tail(&req->xfer[4], &req->msg); 403 404 req->xfer[5].rx_buf = &req->scratch; 405 req->xfer[5].len = 2; 406 spi_message_add_tail(&req->xfer[5], &req->msg); 407 408 /* clear the command register */ 409 req->xfer[6].rx_buf = &req->scratch; 410 req->xfer[6].len = 1; 411 spi_message_add_tail(&req->xfer[6], &req->msg); 412 413 req->xfer[7].rx_buf = &req->scratch; 414 req->xfer[7].len = 2; 415 CS_CHANGE(req->xfer[7]); 416 spi_message_add_tail(&req->xfer[7], &req->msg); 417 418 scoped_guard(mutex, &ts->lock) { 419 ads7846_stop(ts); 420 status = spi_sync(spi, &req->msg); 421 ads7846_restart(ts); 422 } 423 424 if (status == 0) { 425 /* on-wire is a must-ignore bit, a BE12 value, then padding */ 426 status = be16_to_cpu(req->sample); 427 status = status >> 3; 428 status &= 0x0fff; 429 } 430 431 return status; 432 } 433 434 static int ads7845_read12_ser(struct device *dev, unsigned command) 435 { 436 struct spi_device *spi = to_spi_device(dev); 437 struct ads7846 *ts = dev_get_drvdata(dev); 438 int status; 439 440 struct ads7845_ser_req *req __free(kfree) = kzalloc_obj(*req); 441 if (!req) 442 return -ENOMEM; 443 444 spi_message_init(&req->msg); 445 446 req->command[0] = (u8) command; 447 req->xfer[0].tx_buf = req->command; 448 req->xfer[0].rx_buf = req->sample; 449 req->xfer[0].len = 3; 450 spi_message_add_tail(&req->xfer[0], &req->msg); 451 452 scoped_guard(mutex, &ts->lock) { 453 ads7846_stop(ts); 454 status = spi_sync(spi, &req->msg); 455 ads7846_restart(ts); 456 } 457 458 if (status == 0) { 459 /* BE12 value, then padding */ 460 status = get_unaligned_be16(&req->sample[1]); 461 status = status >> 3; 462 status &= 0x0fff; 463 } 464 465 return status; 466 } 467 468 #if IS_ENABLED(CONFIG_HWMON) 469 470 #define SHOW(name, var, adjust) static ssize_t \ 471 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \ 472 { \ 473 struct ads7846 *ts = dev_get_drvdata(dev); \ 474 ssize_t v = ads7846_read12_ser(&ts->spi->dev, \ 475 READ_12BIT_SER(var)); \ 476 if (v < 0) \ 477 return v; \ 478 return sprintf(buf, "%u\n", adjust(ts, v)); \ 479 } \ 480 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL); 481 482 483 /* Sysfs conventions report temperatures in millidegrees Celsius. 484 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high 485 * accuracy scheme without calibration data. For now we won't try either; 486 * userspace sees raw sensor values, and must scale/calibrate appropriately. 487 */ 488 static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v) 489 { 490 return v; 491 } 492 493 SHOW(temp0, temp0, null_adjust) /* temp1_input */ 494 SHOW(temp1, temp1, null_adjust) /* temp2_input */ 495 496 497 /* sysfs conventions report voltages in millivolts. We can convert voltages 498 * if we know vREF. userspace may need to scale vAUX to match the board's 499 * external resistors; we assume that vBATT only uses the internal ones. 500 */ 501 static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v) 502 { 503 unsigned retval = v; 504 505 /* external resistors may scale vAUX into 0..vREF */ 506 retval *= ts->vref_mv; 507 retval = retval >> 12; 508 509 return retval; 510 } 511 512 static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v) 513 { 514 unsigned retval = vaux_adjust(ts, v); 515 516 /* ads7846 has a resistor ladder to scale this signal down */ 517 if (ts->model == 7846) 518 retval *= 4; 519 520 return retval; 521 } 522 523 SHOW(in0_input, vaux, vaux_adjust) 524 SHOW(in1_input, vbatt, vbatt_adjust) 525 526 static umode_t ads7846_is_visible(struct kobject *kobj, struct attribute *attr, 527 int index) 528 { 529 struct device *dev = kobj_to_dev(kobj); 530 struct ads7846 *ts = dev_get_drvdata(dev); 531 532 if (ts->model == 7843 && index < 2) /* in0, in1 */ 533 return 0; 534 if (ts->model == 7845 && index != 2) /* in0 */ 535 return 0; 536 537 return attr->mode; 538 } 539 540 static struct attribute *ads7846_attributes[] = { 541 &dev_attr_temp0.attr, /* 0 */ 542 &dev_attr_temp1.attr, /* 1 */ 543 &dev_attr_in0_input.attr, /* 2 */ 544 &dev_attr_in1_input.attr, /* 3 */ 545 NULL, 546 }; 547 548 static const struct attribute_group ads7846_attr_group = { 549 .attrs = ads7846_attributes, 550 .is_visible = ads7846_is_visible, 551 }; 552 __ATTRIBUTE_GROUPS(ads7846_attr); 553 554 static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts) 555 { 556 struct device *hwmon; 557 558 /* hwmon sensors need a reference voltage */ 559 switch (ts->model) { 560 case 7846: 561 if (!ts->vref_mv) { 562 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n"); 563 ts->vref_mv = 2500; 564 ts->use_internal = true; 565 } 566 break; 567 case 7845: 568 case 7843: 569 if (!ts->vref_mv) { 570 dev_warn(&spi->dev, 571 "external vREF for ADS%d not specified\n", 572 ts->model); 573 return 0; 574 } 575 break; 576 } 577 578 hwmon = devm_hwmon_device_register_with_groups(&spi->dev, 579 spi->modalias, ts, 580 ads7846_attr_groups); 581 582 return PTR_ERR_OR_ZERO(hwmon); 583 } 584 585 #else 586 static inline int ads784x_hwmon_register(struct spi_device *spi, 587 struct ads7846 *ts) 588 { 589 return 0; 590 } 591 #endif 592 593 static ssize_t ads7846_pen_down_show(struct device *dev, 594 struct device_attribute *attr, char *buf) 595 { 596 struct ads7846 *ts = dev_get_drvdata(dev); 597 598 return sprintf(buf, "%u\n", ts->pendown); 599 } 600 601 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL); 602 603 static ssize_t ads7846_disable_show(struct device *dev, 604 struct device_attribute *attr, char *buf) 605 { 606 struct ads7846 *ts = dev_get_drvdata(dev); 607 608 return sprintf(buf, "%u\n", ts->disabled); 609 } 610 611 static ssize_t ads7846_disable_store(struct device *dev, 612 struct device_attribute *attr, 613 const char *buf, size_t count) 614 { 615 struct ads7846 *ts = dev_get_drvdata(dev); 616 unsigned int i; 617 int err; 618 619 err = kstrtouint(buf, 10, &i); 620 if (err) 621 return err; 622 623 if (i) 624 ads7846_disable(ts); 625 else 626 ads7846_enable(ts); 627 628 return count; 629 } 630 631 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store); 632 633 static struct attribute *ads784x_attrs[] = { 634 &dev_attr_pen_down.attr, 635 &dev_attr_disable.attr, 636 NULL, 637 }; 638 ATTRIBUTE_GROUPS(ads784x); 639 640 /*--------------------------------------------------------------------------*/ 641 642 static int ads7846_debounce_filter(void *ads, int data_idx, int *val) 643 { 644 struct ads7846 *ts = ads; 645 646 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) { 647 /* Start over collecting consistent readings. */ 648 ts->read_rep = 0; 649 /* 650 * Repeat it, if this was the first read or the read 651 * wasn't consistent enough. 652 */ 653 if (ts->read_cnt < ts->debounce_max) { 654 ts->last_read = *val; 655 ts->read_cnt++; 656 return ADS7846_FILTER_REPEAT; 657 } else { 658 /* 659 * Maximum number of debouncing reached and still 660 * not enough number of consistent readings. Abort 661 * the whole sample, repeat it in the next sampling 662 * period. 663 */ 664 ts->read_cnt = 0; 665 return ADS7846_FILTER_IGNORE; 666 } 667 } else { 668 if (++ts->read_rep > ts->debounce_rep) { 669 /* 670 * Got a good reading for this coordinate, 671 * go for the next one. 672 */ 673 ts->read_cnt = 0; 674 ts->read_rep = 0; 675 return ADS7846_FILTER_OK; 676 } else { 677 /* Read more values that are consistent. */ 678 ts->read_cnt++; 679 return ADS7846_FILTER_REPEAT; 680 } 681 } 682 } 683 684 static int ads7846_no_filter(void *ads, int data_idx, int *val) 685 { 686 return ADS7846_FILTER_OK; 687 } 688 689 static int ads7846_get_value(struct ads7846_buf *buf) 690 { 691 int value; 692 693 value = be16_to_cpup(&buf->data); 694 695 /* enforce ADC output is 12 bits width */ 696 return (value >> 3) & 0xfff; 697 } 698 699 static void ads7846_set_cmd_val(struct ads7846 *ts, enum ads7846_cmds cmd_idx, 700 u16 val) 701 { 702 struct ads7846_packet *packet = ts->packet; 703 704 switch (cmd_idx) { 705 case ADS7846_Y: 706 packet->y = val; 707 break; 708 case ADS7846_X: 709 packet->x = val; 710 break; 711 case ADS7846_Z1: 712 packet->z1 = val; 713 break; 714 case ADS7846_Z2: 715 packet->z2 = val; 716 break; 717 default: 718 WARN_ON_ONCE(1); 719 } 720 } 721 722 static u8 ads7846_get_cmd(enum ads7846_cmds cmd_idx, int vref) 723 { 724 switch (cmd_idx) { 725 case ADS7846_Y: 726 return READ_Y(vref); 727 case ADS7846_X: 728 return READ_X(vref); 729 730 /* 7846 specific commands */ 731 case ADS7846_Z1: 732 return READ_Z1(vref); 733 case ADS7846_Z2: 734 return READ_Z2(vref); 735 case ADS7846_PWDOWN: 736 return PWRDOWN; 737 default: 738 WARN_ON_ONCE(1); 739 } 740 741 return 0; 742 } 743 744 static bool ads7846_cmd_need_settle(enum ads7846_cmds cmd_idx) 745 { 746 switch (cmd_idx) { 747 case ADS7846_X: 748 case ADS7846_Y: 749 case ADS7846_Z1: 750 case ADS7846_Z2: 751 return true; 752 case ADS7846_PWDOWN: 753 return false; 754 default: 755 WARN_ON_ONCE(1); 756 } 757 758 return false; 759 } 760 761 static int ads7846_filter(struct ads7846 *ts) 762 { 763 struct ads7846_packet *packet = ts->packet; 764 int action; 765 int val; 766 unsigned int cmd_idx, b; 767 768 packet->ignore = false; 769 for (cmd_idx = packet->last_cmd_idx; cmd_idx < packet->cmds - 1; cmd_idx++) { 770 struct ads7846_buf_layout *l = &packet->l[cmd_idx]; 771 772 packet->last_cmd_idx = cmd_idx; 773 774 for (b = l->skip; b < l->count; b++) { 775 val = ads7846_get_value(&packet->rx[l->offset + b]); 776 777 action = ts->filter(ts->filter_data, cmd_idx, &val); 778 if (action == ADS7846_FILTER_REPEAT) { 779 if (b == l->count - 1) 780 return -EAGAIN; 781 } else if (action == ADS7846_FILTER_OK) { 782 ads7846_set_cmd_val(ts, cmd_idx, val); 783 break; 784 } else { 785 packet->ignore = true; 786 return 0; 787 } 788 } 789 } 790 791 return 0; 792 } 793 794 static int ads7846_filter_one(struct ads7846 *ts, unsigned int cmd_idx) 795 { 796 struct ads7846_packet *packet = ts->packet; 797 struct ads7846_buf_layout *l = &packet->l[cmd_idx]; 798 int action, val; 799 800 val = ads7846_get_value(&packet->rx[l->offset + l->count - 1]); 801 action = ts->filter(ts->filter_data, cmd_idx, &val); 802 if (action == ADS7846_FILTER_REPEAT) 803 return -EAGAIN; 804 else if (action != ADS7846_FILTER_OK) 805 return -EIO; 806 ads7846_set_cmd_val(ts, cmd_idx, val); 807 return 0; 808 } 809 810 static void ads7846_wait_for_hsync(struct ads7846 *ts) 811 { 812 if (ts->wait_for_sync) { 813 ts->wait_for_sync(); 814 return; 815 } 816 817 if (!ts->gpio_hsync) 818 return; 819 820 /* 821 * Wait for HSYNC to assert the line should be flagged 822 * as active low so here we are waiting for it to assert 823 */ 824 while (!gpiod_get_value(ts->gpio_hsync)) 825 cpu_relax(); 826 827 /* Then we wait for it do de-assert */ 828 while (gpiod_get_value(ts->gpio_hsync)) 829 cpu_relax(); 830 } 831 832 static void ads7846_halfd_read_state(struct ads7846 *ts) 833 { 834 struct ads7846_packet *packet = ts->packet; 835 int msg_idx = 0; 836 837 packet->ignore = false; 838 839 while (msg_idx < ts->msg_count) { 840 int error; 841 842 ads7846_wait_for_hsync(ts); 843 844 error = spi_sync(ts->spi, &ts->msg[msg_idx]); 845 if (error) { 846 dev_err_ratelimited(&ts->spi->dev, "spi_sync --> %d\n", 847 error); 848 packet->ignore = true; 849 return; 850 } 851 852 /* 853 * Last message is power down request, no need to convert 854 * or filter the value. 855 */ 856 if (msg_idx == ts->msg_count - 1) 857 break; 858 859 error = ads7846_filter_one(ts, msg_idx); 860 if (error == -EAGAIN) { 861 continue; 862 } else if (error) { 863 packet->ignore = true; 864 msg_idx = ts->msg_count - 1; 865 } else { 866 msg_idx++; 867 } 868 } 869 } 870 871 static void ads7846_read_state(struct ads7846 *ts) 872 { 873 struct ads7846_packet *packet = ts->packet; 874 struct spi_message *m; 875 int msg_idx = 0; 876 int error; 877 878 packet->last_cmd_idx = 0; 879 880 while (true) { 881 ads7846_wait_for_hsync(ts); 882 883 m = &ts->msg[msg_idx]; 884 error = spi_sync(ts->spi, m); 885 if (error) { 886 dev_err_ratelimited(&ts->spi->dev, "spi_sync --> %d\n", error); 887 packet->ignore = true; 888 return; 889 } 890 891 error = ads7846_filter(ts); 892 if (error) 893 continue; 894 895 return; 896 } 897 } 898 899 static void ads7846_report_state(struct ads7846 *ts) 900 { 901 struct ads7846_packet *packet = ts->packet; 902 unsigned int Rt; 903 u16 x, y, z1, z2; 904 905 x = packet->x; 906 y = packet->y; 907 if (ts->model == 7845) { 908 z1 = 0; 909 z2 = 0; 910 } else { 911 z1 = packet->z1; 912 z2 = packet->z2; 913 } 914 915 /* range filtering */ 916 if (x == MAX_12BIT) 917 x = 0; 918 919 if (ts->model == 7843 || ts->model == 7845) { 920 Rt = ts->pressure_max / 2; 921 } else if (likely(x && z1)) { 922 /* compute touch pressure resistance using equation #2 */ 923 Rt = z2; 924 Rt -= z1; 925 Rt *= ts->x_plate_ohms; 926 Rt = DIV_ROUND_CLOSEST(Rt, 16); 927 Rt *= x; 928 Rt /= z1; 929 Rt = DIV_ROUND_CLOSEST(Rt, 256); 930 } else { 931 Rt = 0; 932 } 933 934 /* 935 * Sample found inconsistent by debouncing or pressure is beyond 936 * the maximum. Don't report it to user space, repeat at least 937 * once more the measurement 938 */ 939 if (packet->ignore || Rt > ts->pressure_max) { 940 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n", 941 packet->ignore, Rt); 942 return; 943 } 944 945 /* 946 * Maybe check the pendown state before reporting. This discards 947 * false readings when the pen is lifted. 948 */ 949 if (ts->penirq_recheck_delay_usecs) { 950 udelay(ts->penirq_recheck_delay_usecs); 951 if (!get_pendown_state(ts)) 952 Rt = 0; 953 } 954 955 /* 956 * NOTE: We can't rely on the pressure to determine the pen down 957 * state, even this controller has a pressure sensor. The pressure 958 * value can fluctuate for quite a while after lifting the pen and 959 * in some cases may not even settle at the expected value. 960 * 961 * The only safe way to check for the pen up condition is in the 962 * timer by reading the pen signal state (it's a GPIO _and_ IRQ). 963 */ 964 if (Rt) { 965 struct input_dev *input = ts->input; 966 967 if (!ts->pendown) { 968 input_report_key(input, BTN_TOUCH, 1); 969 ts->pendown = true; 970 dev_vdbg(&ts->spi->dev, "DOWN\n"); 971 } 972 973 touchscreen_report_pos(input, &ts->core_prop, x, y, false); 974 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt); 975 976 input_sync(input); 977 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt); 978 } 979 } 980 981 static irqreturn_t ads7846_hard_irq(int irq, void *handle) 982 { 983 struct ads7846 *ts = handle; 984 985 return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED; 986 } 987 988 989 static irqreturn_t ads7846_irq(int irq, void *handle) 990 { 991 struct ads7846 *ts = handle; 992 993 /* Start with a small delay before checking pendown state */ 994 msleep(TS_POLL_DELAY); 995 996 while (!ts->stopped && get_pendown_state(ts)) { 997 998 /* pen is down, continue with the measurement */ 999 ts->read_state(ts); 1000 1001 if (!ts->stopped) 1002 ads7846_report_state(ts); 1003 1004 wait_event_timeout(ts->wait, ts->stopped, 1005 msecs_to_jiffies(TS_POLL_PERIOD)); 1006 } 1007 1008 if (ts->pendown && !ts->stopped) 1009 ads7846_report_pen_up(ts); 1010 1011 return IRQ_HANDLED; 1012 } 1013 1014 static int ads7846_suspend(struct device *dev) 1015 { 1016 struct ads7846 *ts = dev_get_drvdata(dev); 1017 1018 guard(mutex)(&ts->lock); 1019 1020 if (!ts->suspended) { 1021 1022 if (!ts->disabled) 1023 __ads7846_disable(ts); 1024 1025 if (device_may_wakeup(&ts->spi->dev)) 1026 enable_irq_wake(ts->spi->irq); 1027 1028 ts->suspended = true; 1029 } 1030 1031 return 0; 1032 } 1033 1034 static int ads7846_resume(struct device *dev) 1035 { 1036 struct ads7846 *ts = dev_get_drvdata(dev); 1037 1038 guard(mutex)(&ts->lock); 1039 1040 if (ts->suspended) { 1041 1042 ts->suspended = false; 1043 1044 if (device_may_wakeup(&ts->spi->dev)) 1045 disable_irq_wake(ts->spi->irq); 1046 1047 if (!ts->disabled) 1048 __ads7846_enable(ts); 1049 } 1050 1051 return 0; 1052 } 1053 1054 static DEFINE_SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume); 1055 1056 static int ads7846_setup_pendown(struct spi_device *spi, 1057 struct ads7846 *ts, 1058 const struct ads7846_platform_data *pdata) 1059 { 1060 /* 1061 * REVISIT when the irq can be triggered active-low, or if for some 1062 * reason the touchscreen isn't hooked up, we don't need to access 1063 * the pendown state. 1064 */ 1065 1066 if (pdata->get_pendown_state) { 1067 ts->get_pendown_state = pdata->get_pendown_state; 1068 } else { 1069 ts->gpio_pendown = devm_gpiod_get(&spi->dev, "pendown", GPIOD_IN); 1070 if (IS_ERR(ts->gpio_pendown)) { 1071 dev_err(&spi->dev, "failed to request pendown GPIO\n"); 1072 return PTR_ERR(ts->gpio_pendown); 1073 } 1074 if (pdata->gpio_pendown_debounce) 1075 gpiod_set_debounce(ts->gpio_pendown, 1076 pdata->gpio_pendown_debounce); 1077 } 1078 1079 return 0; 1080 } 1081 1082 /* 1083 * Set up the transfers to read touchscreen state; this assumes we 1084 * use formula #2 for pressure, not #3. 1085 */ 1086 static int ads7846_halfd_spi_msg(struct ads7846 *ts, 1087 const struct ads7846_platform_data *pdata) 1088 { 1089 struct spi_message *m = ts->msg; 1090 struct spi_transfer *x = ts->xfer; 1091 struct ads7846_packet *packet = ts->packet; 1092 int vref = pdata->keep_vref_on; 1093 unsigned int offset = 0; 1094 unsigned int cmd_idx, b; 1095 size_t size = 0; 1096 1097 if (pdata->settle_delay_usecs) 1098 packet->count = 2; 1099 else 1100 packet->count = 1; 1101 1102 if (ts->model == 7846) 1103 packet->cmds = 5; /* x, y, z1, z2, pwdown */ 1104 else 1105 packet->cmds = 3; /* x, y, pwdown */ 1106 1107 for (cmd_idx = 0; cmd_idx < packet->cmds; cmd_idx++) { 1108 struct ads7846_buf_layout *l = &packet->l[cmd_idx]; 1109 unsigned int max_count; 1110 1111 if (cmd_idx == packet->cmds - 1) { 1112 cmd_idx = ADS7846_PWDOWN; 1113 max_count = 1; 1114 } else { 1115 max_count = packet->count; 1116 } 1117 1118 l->offset = offset; 1119 offset += max_count; 1120 l->count = max_count; 1121 l->skip = 0; 1122 size += sizeof(*packet->rx) * max_count; 1123 } 1124 1125 /* We use two transfers per command. */ 1126 if (ARRAY_SIZE(ts->xfer) < offset * 2) 1127 return -ENOMEM; 1128 1129 packet->rx = devm_kzalloc(&ts->spi->dev, size, GFP_KERNEL); 1130 if (!packet->rx) 1131 return -ENOMEM; 1132 1133 if (ts->model == 7873) { 1134 /* 1135 * The AD7873 is almost identical to the ADS7846 1136 * keep VREF off during differential/ratiometric 1137 * conversion modes. 1138 */ 1139 ts->model = 7846; 1140 vref = 0; 1141 } 1142 1143 ts->msg_count = 0; 1144 1145 for (cmd_idx = 0; cmd_idx < packet->cmds; cmd_idx++) { 1146 struct ads7846_buf_layout *l = &packet->l[cmd_idx]; 1147 u8 cmd; 1148 1149 ts->msg_count++; 1150 spi_message_init(m); 1151 m->context = ts; 1152 1153 if (cmd_idx == packet->cmds - 1) 1154 cmd_idx = ADS7846_PWDOWN; 1155 1156 cmd = ads7846_get_cmd(cmd_idx, vref); 1157 1158 for (b = 0; b < l->count; b++) { 1159 packet->rx[l->offset + b].cmd = cmd; 1160 x->tx_buf = &packet->rx[l->offset + b].cmd; 1161 x->len = 1; 1162 spi_message_add_tail(x, m); 1163 x++; 1164 x->rx_buf = &packet->rx[l->offset + b].data; 1165 x->len = 2; 1166 if (b < l->count - 1 && l->count > 1) { 1167 x->delay.value = pdata->settle_delay_usecs; 1168 x->delay.unit = SPI_DELAY_UNIT_USECS; 1169 } 1170 spi_message_add_tail(x, m); 1171 x++; 1172 } 1173 m++; 1174 } 1175 return 0; 1176 } 1177 1178 /* 1179 * Set up the transfers to read touchscreen state; this assumes we 1180 * use formula #2 for pressure, not #3. 1181 */ 1182 static int ads7846_setup_spi_msg(struct ads7846 *ts, 1183 const struct ads7846_platform_data *pdata) 1184 { 1185 struct spi_message *m = &ts->msg[0]; 1186 struct spi_transfer *x = ts->xfer; 1187 struct ads7846_packet *packet = ts->packet; 1188 int vref = pdata->keep_vref_on; 1189 unsigned int count, offset = 0; 1190 unsigned int cmd_idx, b; 1191 unsigned long time; 1192 size_t size = 0; 1193 1194 /* time per bit */ 1195 time = NSEC_PER_SEC / ts->spi->max_speed_hz; 1196 1197 count = pdata->settle_delay_usecs * NSEC_PER_USEC / time; 1198 packet->count_skip = DIV_ROUND_UP(count, 24); 1199 1200 if (ts->debounce_max && ts->debounce_rep) 1201 /* ads7846_debounce_filter() is making ts->debounce_rep + 2 1202 * reads. So we need to get all samples for normal case. */ 1203 packet->count = ts->debounce_rep + 2; 1204 else 1205 packet->count = 1; 1206 1207 if (ts->model == 7846) 1208 packet->cmds = 5; /* x, y, z1, z2, pwdown */ 1209 else 1210 packet->cmds = 3; /* x, y, pwdown */ 1211 1212 for (cmd_idx = 0; cmd_idx < packet->cmds; cmd_idx++) { 1213 struct ads7846_buf_layout *l = &packet->l[cmd_idx]; 1214 unsigned int max_count; 1215 1216 if (cmd_idx == packet->cmds - 1) 1217 cmd_idx = ADS7846_PWDOWN; 1218 1219 if (ads7846_cmd_need_settle(cmd_idx)) 1220 max_count = packet->count + packet->count_skip; 1221 else 1222 max_count = packet->count; 1223 1224 l->offset = offset; 1225 offset += max_count; 1226 l->count = max_count; 1227 l->skip = packet->count_skip; 1228 size += sizeof(*packet->tx) * max_count; 1229 } 1230 1231 packet->tx = devm_kzalloc(&ts->spi->dev, size, GFP_KERNEL); 1232 if (!packet->tx) 1233 return -ENOMEM; 1234 1235 packet->rx = devm_kzalloc(&ts->spi->dev, size, GFP_KERNEL); 1236 if (!packet->rx) 1237 return -ENOMEM; 1238 1239 if (ts->model == 7873) { 1240 /* 1241 * The AD7873 is almost identical to the ADS7846 1242 * keep VREF off during differential/ratiometric 1243 * conversion modes. 1244 */ 1245 ts->model = 7846; 1246 vref = 0; 1247 } 1248 1249 ts->msg_count = 1; 1250 spi_message_init(m); 1251 m->context = ts; 1252 1253 for (cmd_idx = 0; cmd_idx < packet->cmds; cmd_idx++) { 1254 struct ads7846_buf_layout *l = &packet->l[cmd_idx]; 1255 u8 cmd; 1256 1257 if (cmd_idx == packet->cmds - 1) 1258 cmd_idx = ADS7846_PWDOWN; 1259 1260 cmd = ads7846_get_cmd(cmd_idx, vref); 1261 1262 for (b = 0; b < l->count; b++) 1263 packet->tx[l->offset + b].cmd = cmd; 1264 } 1265 1266 x->tx_buf = packet->tx; 1267 x->rx_buf = packet->rx; 1268 x->len = size; 1269 spi_message_add_tail(x, m); 1270 1271 return 0; 1272 } 1273 1274 static const struct of_device_id ads7846_dt_ids[] = { 1275 { .compatible = "ti,tsc2046", .data = (void *) 7846 }, 1276 { .compatible = "ti,ads7843", .data = (void *) 7843 }, 1277 { .compatible = "ti,ads7845", .data = (void *) 7845 }, 1278 { .compatible = "ti,ads7846", .data = (void *) 7846 }, 1279 { .compatible = "ti,ads7873", .data = (void *) 7873 }, 1280 { } 1281 }; 1282 MODULE_DEVICE_TABLE(of, ads7846_dt_ids); 1283 1284 static const struct spi_device_id ads7846_spi_ids[] = { 1285 { "tsc2046", 7846 }, 1286 { "ads7843", 7843 }, 1287 { "ads7845", 7845 }, 1288 { "ads7846", 7846 }, 1289 { "ads7873", 7873 }, 1290 { }, 1291 }; 1292 MODULE_DEVICE_TABLE(spi, ads7846_spi_ids); 1293 1294 static const struct ads7846_platform_data *ads7846_get_props(struct device *dev) 1295 { 1296 struct ads7846_platform_data *pdata; 1297 u32 value; 1298 1299 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 1300 if (!pdata) 1301 return ERR_PTR(-ENOMEM); 1302 1303 pdata->model = (uintptr_t)device_get_match_data(dev); 1304 1305 device_property_read_u16(dev, "ti,vref-delay-usecs", 1306 &pdata->vref_delay_usecs); 1307 device_property_read_u16(dev, "ti,vref-mv", &pdata->vref_mv); 1308 pdata->keep_vref_on = device_property_read_bool(dev, "ti,keep-vref-on"); 1309 1310 pdata->swap_xy = device_property_read_bool(dev, "ti,swap-xy"); 1311 1312 device_property_read_u16(dev, "ti,settle-delay-usec", 1313 &pdata->settle_delay_usecs); 1314 device_property_read_u16(dev, "ti,penirq-recheck-delay-usecs", 1315 &pdata->penirq_recheck_delay_usecs); 1316 1317 device_property_read_u16(dev, "ti,x-plate-ohms", &pdata->x_plate_ohms); 1318 device_property_read_u16(dev, "ti,y-plate-ohms", &pdata->y_plate_ohms); 1319 1320 device_property_read_u16(dev, "ti,x-min", &pdata->x_min); 1321 device_property_read_u16(dev, "ti,y-min", &pdata->y_min); 1322 device_property_read_u16(dev, "ti,x-max", &pdata->x_max); 1323 device_property_read_u16(dev, "ti,y-max", &pdata->y_max); 1324 1325 /* 1326 * touchscreen-max-pressure gets parsed during 1327 * touchscreen_parse_properties() 1328 */ 1329 device_property_read_u16(dev, "ti,pressure-min", &pdata->pressure_min); 1330 if (!device_property_read_u32(dev, "touchscreen-min-pressure", &value)) 1331 pdata->pressure_min = (u16) value; 1332 device_property_read_u16(dev, "ti,pressure-max", &pdata->pressure_max); 1333 1334 device_property_read_u16(dev, "ti,debounce-max", &pdata->debounce_max); 1335 if (!device_property_read_u32(dev, "touchscreen-average-samples", &value)) 1336 pdata->debounce_max = (u16) value; 1337 device_property_read_u16(dev, "ti,debounce-tol", &pdata->debounce_tol); 1338 device_property_read_u16(dev, "ti,debounce-rep", &pdata->debounce_rep); 1339 1340 device_property_read_u32(dev, "ti,pendown-gpio-debounce", 1341 &pdata->gpio_pendown_debounce); 1342 1343 pdata->wakeup = device_property_read_bool(dev, "wakeup-source") || 1344 device_property_read_bool(dev, "linux,wakeup"); 1345 1346 return pdata; 1347 } 1348 1349 static void ads7846_regulator_disable(void *regulator) 1350 { 1351 regulator_disable(regulator); 1352 } 1353 1354 static int ads7846_probe(struct spi_device *spi) 1355 { 1356 const struct ads7846_platform_data *pdata; 1357 struct ads7846 *ts; 1358 struct device *dev = &spi->dev; 1359 struct ads7846_packet *packet; 1360 struct input_dev *input_dev; 1361 unsigned long irq_flags; 1362 int err; 1363 1364 if (!spi->irq) { 1365 dev_dbg(dev, "no IRQ?\n"); 1366 return -EINVAL; 1367 } 1368 1369 /* don't exceed max specified sample rate */ 1370 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) { 1371 dev_err(dev, "f(sample) %d KHz?\n", 1372 (spi->max_speed_hz/SAMPLE_BITS)/1000); 1373 return -EINVAL; 1374 } 1375 1376 /* 1377 * We'd set TX word size 8 bits and RX word size to 13 bits ... except 1378 * that even if the hardware can do that, the SPI controller driver 1379 * may not. So we stick to very-portable 8 bit words, both RX and TX. 1380 */ 1381 spi->bits_per_word = 8; 1382 spi->mode &= ~SPI_MODE_X_MASK; 1383 spi->mode |= SPI_MODE_0; 1384 err = spi_setup(spi); 1385 if (err < 0) 1386 return err; 1387 1388 ts = devm_kzalloc(dev, sizeof(struct ads7846), GFP_KERNEL); 1389 if (!ts) 1390 return -ENOMEM; 1391 1392 if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) { 1393 ts->setup_spi_msg = ads7846_halfd_spi_msg; 1394 ts->read_state = ads7846_halfd_read_state; 1395 } else { 1396 ts->setup_spi_msg = ads7846_setup_spi_msg; 1397 ts->read_state = ads7846_read_state; 1398 } 1399 1400 packet = devm_kzalloc(dev, sizeof(struct ads7846_packet), GFP_KERNEL); 1401 if (!packet) 1402 return -ENOMEM; 1403 1404 input_dev = devm_input_allocate_device(dev); 1405 if (!input_dev) 1406 return -ENOMEM; 1407 1408 spi_set_drvdata(spi, ts); 1409 1410 ts->packet = packet; 1411 ts->spi = spi; 1412 ts->input = input_dev; 1413 1414 mutex_init(&ts->lock); 1415 init_waitqueue_head(&ts->wait); 1416 1417 pdata = dev_get_platdata(dev); 1418 if (!pdata) { 1419 pdata = ads7846_get_props(dev); 1420 if (IS_ERR(pdata)) 1421 return PTR_ERR(pdata); 1422 } 1423 1424 ts->model = pdata->model ? : 7846; 1425 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100; 1426 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400; 1427 ts->vref_mv = pdata->vref_mv; 1428 1429 if (pdata->debounce_max) { 1430 ts->debounce_max = pdata->debounce_max; 1431 if (ts->debounce_max < 2) 1432 ts->debounce_max = 2; 1433 ts->debounce_tol = pdata->debounce_tol; 1434 ts->debounce_rep = pdata->debounce_rep; 1435 ts->filter = ads7846_debounce_filter; 1436 ts->filter_data = ts; 1437 } else { 1438 ts->filter = ads7846_no_filter; 1439 } 1440 1441 err = ads7846_setup_pendown(spi, ts, pdata); 1442 if (err) 1443 return err; 1444 1445 if (pdata->penirq_recheck_delay_usecs) 1446 ts->penirq_recheck_delay_usecs = 1447 pdata->penirq_recheck_delay_usecs; 1448 1449 ts->wait_for_sync = pdata->wait_for_sync; 1450 1451 ts->gpio_hsync = devm_gpiod_get_optional(dev, "ti,hsync", GPIOD_IN); 1452 if (IS_ERR(ts->gpio_hsync)) 1453 return PTR_ERR(ts->gpio_hsync); 1454 1455 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev)); 1456 snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model); 1457 1458 input_dev->name = ts->name; 1459 input_dev->phys = ts->phys; 1460 1461 input_dev->id.bustype = BUS_SPI; 1462 input_dev->id.product = pdata->model; 1463 1464 input_set_capability(input_dev, EV_KEY, BTN_TOUCH); 1465 input_set_abs_params(input_dev, ABS_X, 1466 pdata->x_min ? : 0, 1467 pdata->x_max ? : MAX_12BIT, 1468 0, 0); 1469 input_set_abs_params(input_dev, ABS_Y, 1470 pdata->y_min ? : 0, 1471 pdata->y_max ? : MAX_12BIT, 1472 0, 0); 1473 if (ts->model != 7845) 1474 input_set_abs_params(input_dev, ABS_PRESSURE, 1475 pdata->pressure_min, pdata->pressure_max, 0, 0); 1476 1477 /* 1478 * Parse common framework properties. Must be done here to ensure the 1479 * correct behaviour in case of using the legacy vendor bindings. The 1480 * general binding value overrides the vendor specific one. 1481 */ 1482 touchscreen_parse_properties(ts->input, false, &ts->core_prop); 1483 ts->pressure_max = input_abs_get_max(input_dev, ABS_PRESSURE) ? : ~0; 1484 1485 /* 1486 * Check if legacy ti,swap-xy binding is used instead of 1487 * touchscreen-swapped-x-y 1488 */ 1489 if (!ts->core_prop.swap_x_y && pdata->swap_xy) { 1490 swap(input_dev->absinfo[ABS_X], input_dev->absinfo[ABS_Y]); 1491 ts->core_prop.swap_x_y = true; 1492 } 1493 1494 err = ts->setup_spi_msg(ts, pdata); 1495 if (err) 1496 return err; 1497 1498 ts->reg = devm_regulator_get(dev, "vcc"); 1499 if (IS_ERR(ts->reg)) { 1500 err = PTR_ERR(ts->reg); 1501 dev_err(dev, "unable to get regulator: %d\n", err); 1502 return err; 1503 } 1504 1505 err = regulator_enable(ts->reg); 1506 if (err) { 1507 dev_err(dev, "unable to enable regulator: %d\n", err); 1508 return err; 1509 } 1510 1511 err = devm_add_action_or_reset(dev, ads7846_regulator_disable, ts->reg); 1512 if (err) 1513 return err; 1514 1515 irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING; 1516 irq_flags |= IRQF_ONESHOT; 1517 1518 err = devm_request_threaded_irq(dev, spi->irq, 1519 ads7846_hard_irq, ads7846_irq, 1520 irq_flags, dev->driver->name, ts); 1521 if (err && err != -EPROBE_DEFER && !pdata->irq_flags) { 1522 dev_info(dev, 1523 "trying pin change workaround on irq %d\n", spi->irq); 1524 irq_flags |= IRQF_TRIGGER_RISING; 1525 err = devm_request_threaded_irq(dev, spi->irq, 1526 ads7846_hard_irq, ads7846_irq, 1527 irq_flags, dev->driver->name, 1528 ts); 1529 } 1530 1531 if (err) { 1532 dev_dbg(dev, "irq %d busy?\n", spi->irq); 1533 return err; 1534 } 1535 1536 err = ads784x_hwmon_register(spi, ts); 1537 if (err) 1538 return err; 1539 1540 dev_info(dev, "touchscreen, irq %d\n", spi->irq); 1541 1542 /* 1543 * Take a first sample, leaving nPENIRQ active and vREF off; avoid 1544 * the touchscreen, in case it's not connected. 1545 */ 1546 if (ts->model == 7845) 1547 ads7845_read12_ser(dev, PWRDOWN); 1548 else 1549 (void) ads7846_read12_ser(dev, READ_12BIT_SER(vaux)); 1550 1551 err = input_register_device(input_dev); 1552 if (err) 1553 return err; 1554 1555 device_init_wakeup(dev, pdata->wakeup); 1556 1557 /* 1558 * If device does not carry platform data we must have allocated it 1559 * when parsing DT data. 1560 */ 1561 if (!dev_get_platdata(dev)) 1562 devm_kfree(dev, (void *)pdata); 1563 1564 return 0; 1565 } 1566 1567 static void ads7846_remove(struct spi_device *spi) 1568 { 1569 struct ads7846 *ts = spi_get_drvdata(spi); 1570 1571 ads7846_stop(ts); 1572 } 1573 1574 static struct spi_driver ads7846_driver = { 1575 .driver = { 1576 .name = "ads7846", 1577 .dev_groups = ads784x_groups, 1578 .pm = pm_sleep_ptr(&ads7846_pm), 1579 .of_match_table = ads7846_dt_ids, 1580 }, 1581 .probe = ads7846_probe, 1582 .remove = ads7846_remove, 1583 .id_table = ads7846_spi_ids, 1584 }; 1585 1586 module_spi_driver(ads7846_driver); 1587 1588 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver"); 1589 MODULE_LICENSE("GPL"); 1590