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