Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Staging/iio/adc/touchscreen/MXS: add interrupt driven touch detection

For battery driven systems it is a very bad idea to collect the touchscreen
data within a kernel busy loop.

This change uses the features of the hardware to delay and accumulate samples in
hardware to avoid a high interrupt and CPU load.

Signed-off-by: Juergen Beisert <jbe@pengutronix.de>
Tested-by: Marek Vasut <marex@denx.de>
Acked-by: Marek Vasut <marex@denx.de>
Tested-by: Lothar Waßmann <LW@KARO-electronics.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>

CC: linux-arm-kernel@lists.infradead.org
CC: linux-input@vger.kernel.org
CC: devel@driverdev.osuosl.org
CC: Marek Vasut <marex@denx.de>
CC: Fabio Estevam <fabio.estevam@freescale.com>
CC: Dmitry Torokhov <dmitry.torokhov@gmail.com>
CC: linux-input@vger.kernel.org

authored by

Juergen Beisert and committed by
Jonathan Cameron
dee05308 8c06f714

+476 -54
+476 -54
drivers/staging/iio/adc/mxs-lradc.c
··· 130 130 MXS_LRADC_TOUCHSCREEN_5WIRE, 131 131 }; 132 132 133 + /* 134 + * Touchscreen handling 135 + */ 136 + enum lradc_ts_plate { 137 + LRADC_TOUCH = 0, 138 + LRADC_SAMPLE_X, 139 + LRADC_SAMPLE_Y, 140 + LRADC_SAMPLE_PRESSURE, 141 + LRADC_SAMPLE_VALID, 142 + }; 143 + 133 144 struct mxs_lradc { 134 145 struct device *dev; 135 146 void __iomem *base; ··· 183 172 #define CHAN_MASK_TOUCHSCREEN_4WIRE (0xf << 2) 184 173 #define CHAN_MASK_TOUCHSCREEN_5WIRE (0x1f << 2) 185 174 enum mxs_lradc_ts use_touchscreen; 186 - bool stop_touchscreen; 187 175 bool use_touchbutton; 188 176 189 177 struct input_dev *ts_input; 190 178 struct work_struct ts_work; 191 179 192 180 enum mxs_lradc_id soc; 181 + enum lradc_ts_plate cur_plate; /* statemachine */ 182 + bool ts_valid; 183 + unsigned ts_x_pos; 184 + unsigned ts_y_pos; 185 + unsigned ts_pressure; 186 + 187 + /* handle touchscreen's physical behaviour */ 188 + /* samples per coordinate */ 189 + unsigned over_sample_cnt; 190 + /* time clocks between samples */ 191 + unsigned over_sample_delay; 192 + /* time in clocks to wait after the plates where switched */ 193 + unsigned settling_delay; 193 194 }; 194 195 195 196 #define LRADC_CTRL0 0x00 ··· 253 230 #define LRADC_CH_ACCUMULATE (1 << 29) 254 231 #define LRADC_CH_NUM_SAMPLES_MASK (0x1f << 24) 255 232 #define LRADC_CH_NUM_SAMPLES_OFFSET 24 233 + #define LRADC_CH_NUM_SAMPLES(x) \ 234 + ((x) << LRADC_CH_NUM_SAMPLES_OFFSET) 256 235 #define LRADC_CH_VALUE_MASK 0x3ffff 257 236 #define LRADC_CH_VALUE_OFFSET 0 258 237 259 238 #define LRADC_DELAY(n) (0xd0 + (0x10 * (n))) 260 239 #define LRADC_DELAY_TRIGGER_LRADCS_MASK (0xff << 24) 261 240 #define LRADC_DELAY_TRIGGER_LRADCS_OFFSET 24 241 + #define LRADC_DELAY_TRIGGER(x) \ 242 + (((x) << LRADC_DELAY_TRIGGER_LRADCS_OFFSET) & \ 243 + LRADC_DELAY_TRIGGER_LRADCS_MASK) 262 244 #define LRADC_DELAY_KICK (1 << 20) 263 245 #define LRADC_DELAY_TRIGGER_DELAYS_MASK (0xf << 16) 264 246 #define LRADC_DELAY_TRIGGER_DELAYS_OFFSET 16 247 + #define LRADC_DELAY_TRIGGER_DELAYS(x) \ 248 + (((x) << LRADC_DELAY_TRIGGER_DELAYS_OFFSET) & \ 249 + LRADC_DELAY_TRIGGER_DELAYS_MASK) 265 250 #define LRADC_DELAY_LOOP_COUNT_MASK (0x1f << 11) 266 251 #define LRADC_DELAY_LOOP_COUNT_OFFSET 11 252 + #define LRADC_DELAY_LOOP(x) \ 253 + (((x) << LRADC_DELAY_LOOP_COUNT_OFFSET) & \ 254 + LRADC_DELAY_LOOP_COUNT_MASK) 267 255 #define LRADC_DELAY_DELAY_MASK 0x7ff 268 256 #define LRADC_DELAY_DELAY_OFFSET 0 257 + #define LRADC_DELAY_DELAY(x) \ 258 + (((x) << LRADC_DELAY_DELAY_OFFSET) & \ 259 + LRADC_DELAY_DELAY_MASK) 269 260 270 261 #define LRADC_CTRL4 0x140 271 262 #define LRADC_CTRL4_LRADCSELECT_MASK(n) (0xf << ((n) * 4)) ··· 359 322 return LRADC_CTRL0_MX28_YPPSW | LRADC_CTRL0_MX28_XNNSW; 360 323 } 361 324 325 + static bool mxs_lradc_check_touch_event(struct mxs_lradc *lradc) 326 + { 327 + return !!(readl(lradc->base + LRADC_STATUS) & 328 + LRADC_STATUS_TOUCH_DETECT_RAW); 329 + } 330 + 331 + static void mxs_lradc_setup_ts_channel(struct mxs_lradc *lradc, unsigned ch) 332 + { 333 + /* 334 + * prepare for oversampling conversion 335 + * 336 + * from the datasheet: 337 + * "The ACCUMULATE bit in the appropriate channel register 338 + * HW_LRADC_CHn must be set to 1 if NUM_SAMPLES is greater then 0; 339 + * otherwise, the IRQs will not fire." 340 + */ 341 + mxs_lradc_reg_wrt(lradc, LRADC_CH_ACCUMULATE | 342 + LRADC_CH_NUM_SAMPLES(lradc->over_sample_cnt - 1), 343 + LRADC_CH(ch)); 344 + 345 + /* from the datasheet: 346 + * "Software must clear this register in preparation for a 347 + * multi-cycle accumulation. 348 + */ 349 + mxs_lradc_reg_clear(lradc, LRADC_CH_VALUE_MASK, LRADC_CH(ch)); 350 + 351 + /* prepare the delay/loop unit according to the oversampling count */ 352 + mxs_lradc_reg_wrt(lradc, LRADC_DELAY_TRIGGER(1 << ch) | 353 + LRADC_DELAY_TRIGGER_DELAYS(0) | 354 + LRADC_DELAY_LOOP(lradc->over_sample_cnt - 1) | 355 + LRADC_DELAY_DELAY(lradc->over_sample_delay - 1), 356 + LRADC_DELAY(3)); 357 + 358 + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ(2) | 359 + LRADC_CTRL1_LRADC_IRQ(3) | LRADC_CTRL1_LRADC_IRQ(4) | 360 + LRADC_CTRL1_LRADC_IRQ(5), LRADC_CTRL1); 361 + 362 + /* wake us again, when the complete conversion is done */ 363 + mxs_lradc_reg_set(lradc, LRADC_CTRL1_LRADC_IRQ_EN(ch), LRADC_CTRL1); 364 + /* 365 + * after changing the touchscreen plates setting 366 + * the signals need some initial time to settle. Start the 367 + * SoC's delay unit and start the conversion later 368 + * and automatically. 369 + */ 370 + mxs_lradc_reg_wrt(lradc, LRADC_DELAY_TRIGGER(0) | /* don't trigger ADC */ 371 + LRADC_DELAY_TRIGGER_DELAYS(1 << 3) | /* trigger DELAY unit#3 */ 372 + LRADC_DELAY_KICK | 373 + LRADC_DELAY_DELAY(lradc->settling_delay), 374 + LRADC_DELAY(2)); 375 + } 376 + 377 + /* 378 + * Pressure detection is special: 379 + * We want to do both required measurements for the pressure detection in 380 + * one turn. Use the hardware features to chain both conversions and let the 381 + * hardware report one interrupt if both conversions are done 382 + */ 383 + static void mxs_lradc_setup_ts_pressure(struct mxs_lradc *lradc, unsigned ch1, 384 + unsigned ch2) 385 + { 386 + u32 reg; 387 + 388 + /* 389 + * prepare for oversampling conversion 390 + * 391 + * from the datasheet: 392 + * "The ACCUMULATE bit in the appropriate channel register 393 + * HW_LRADC_CHn must be set to 1 if NUM_SAMPLES is greater then 0; 394 + * otherwise, the IRQs will not fire." 395 + */ 396 + reg = LRADC_CH_ACCUMULATE | 397 + LRADC_CH_NUM_SAMPLES(lradc->over_sample_cnt - 1); 398 + mxs_lradc_reg_wrt(lradc, reg, LRADC_CH(ch1)); 399 + mxs_lradc_reg_wrt(lradc, reg, LRADC_CH(ch2)); 400 + 401 + /* from the datasheet: 402 + * "Software must clear this register in preparation for a 403 + * multi-cycle accumulation. 404 + */ 405 + mxs_lradc_reg_clear(lradc, LRADC_CH_VALUE_MASK, LRADC_CH(ch1)); 406 + mxs_lradc_reg_clear(lradc, LRADC_CH_VALUE_MASK, LRADC_CH(ch2)); 407 + 408 + /* prepare the delay/loop unit according to the oversampling count */ 409 + mxs_lradc_reg_wrt(lradc, LRADC_DELAY_TRIGGER(1 << ch1) | 410 + LRADC_DELAY_TRIGGER(1 << ch2) | /* start both channels */ 411 + LRADC_DELAY_TRIGGER_DELAYS(0) | 412 + LRADC_DELAY_LOOP(lradc->over_sample_cnt - 1) | 413 + LRADC_DELAY_DELAY(lradc->over_sample_delay - 1), 414 + LRADC_DELAY(3)); 415 + 416 + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ(2) | 417 + LRADC_CTRL1_LRADC_IRQ(3) | LRADC_CTRL1_LRADC_IRQ(4) | 418 + LRADC_CTRL1_LRADC_IRQ(5), LRADC_CTRL1); 419 + 420 + /* wake us again, when the conversions are done */ 421 + mxs_lradc_reg_set(lradc, LRADC_CTRL1_LRADC_IRQ_EN(ch2), LRADC_CTRL1); 422 + /* 423 + * after changing the touchscreen plates setting 424 + * the signals need some initial time to settle. Start the 425 + * SoC's delay unit and start the conversion later 426 + * and automatically. 427 + */ 428 + mxs_lradc_reg_wrt(lradc, LRADC_DELAY_TRIGGER(0) | /* don't trigger ADC */ 429 + LRADC_DELAY_TRIGGER_DELAYS(1 << 3) | /* trigger DELAY unit#3 */ 430 + LRADC_DELAY_KICK | 431 + LRADC_DELAY_DELAY(lradc->settling_delay), LRADC_DELAY(2)); 432 + } 433 + 434 + static unsigned mxs_lradc_read_raw_channel(struct mxs_lradc *lradc, 435 + unsigned channel) 436 + { 437 + u32 reg; 438 + unsigned num_samples, val; 439 + 440 + reg = readl(lradc->base + LRADC_CH(channel)); 441 + if (reg & LRADC_CH_ACCUMULATE) 442 + num_samples = lradc->over_sample_cnt; 443 + else 444 + num_samples = 1; 445 + 446 + val = (reg & LRADC_CH_VALUE_MASK) >> LRADC_CH_VALUE_OFFSET; 447 + return val / num_samples; 448 + } 449 + 450 + static unsigned mxs_lradc_read_ts_pressure(struct mxs_lradc *lradc, 451 + unsigned ch1, unsigned ch2) 452 + { 453 + u32 reg, mask; 454 + unsigned pressure, m1, m2; 455 + 456 + mask = LRADC_CTRL1_LRADC_IRQ(ch1) | LRADC_CTRL1_LRADC_IRQ(ch2); 457 + reg = readl(lradc->base + LRADC_CTRL1) & mask; 458 + 459 + while (reg != mask) { 460 + reg = readl(lradc->base + LRADC_CTRL1) & mask; 461 + dev_dbg(lradc->dev, "One channel is still busy: %X\n", reg); 462 + } 463 + 464 + m1 = mxs_lradc_read_raw_channel(lradc, ch1); 465 + m2 = mxs_lradc_read_raw_channel(lradc, ch2); 466 + 467 + if (m2 == 0) { 468 + dev_warn(lradc->dev, "Cannot calculate pressure\n"); 469 + return 1 << (LRADC_RESOLUTION - 1); 470 + } 471 + 472 + /* simply scale the value from 0 ... max ADC resolution */ 473 + pressure = m1; 474 + pressure *= (1 << LRADC_RESOLUTION); 475 + pressure /= m2; 476 + 477 + dev_dbg(lradc->dev, "Pressure = %u\n", pressure); 478 + return pressure; 479 + } 480 + 481 + #define TS_CH_XP 2 482 + #define TS_CH_YP 3 483 + #define TS_CH_XM 4 484 + #define TS_CH_YM 5 485 + 486 + static int mxs_lradc_read_ts_channel(struct mxs_lradc *lradc) 487 + { 488 + u32 reg; 489 + int val; 490 + 491 + reg = readl(lradc->base + LRADC_CTRL1); 492 + 493 + /* only channels 3 to 5 are of interest here */ 494 + if (reg & LRADC_CTRL1_LRADC_IRQ(TS_CH_YP)) { 495 + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ_EN(TS_CH_YP) | 496 + LRADC_CTRL1_LRADC_IRQ(TS_CH_YP), LRADC_CTRL1); 497 + val = mxs_lradc_read_raw_channel(lradc, TS_CH_YP); 498 + } else if (reg & LRADC_CTRL1_LRADC_IRQ(TS_CH_XM)) { 499 + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ_EN(TS_CH_XM) | 500 + LRADC_CTRL1_LRADC_IRQ(TS_CH_XM), LRADC_CTRL1); 501 + val = mxs_lradc_read_raw_channel(lradc, TS_CH_XM); 502 + } else if (reg & LRADC_CTRL1_LRADC_IRQ(TS_CH_YM)) { 503 + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ_EN(TS_CH_YM) | 504 + LRADC_CTRL1_LRADC_IRQ(TS_CH_YM), LRADC_CTRL1); 505 + val = mxs_lradc_read_raw_channel(lradc, TS_CH_YM); 506 + } else { 507 + return -EIO; 508 + } 509 + 510 + mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(2)); 511 + mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(3)); 512 + 513 + return val; 514 + } 515 + 516 + /* 517 + * YP(open)--+-------------+ 518 + * | |--+ 519 + * | | | 520 + * YM(-)--+-------------+ | 521 + * +--------------+ 522 + * | | 523 + * XP(weak+) XM(open) 524 + * 525 + * "weak+" means 200k Ohm VDDIO 526 + * (-) means GND 527 + */ 528 + static void mxs_lradc_setup_touch_detection(struct mxs_lradc *lradc) 529 + { 530 + /* 531 + * In order to detect a touch event the 'touch detect enable' bit 532 + * enables: 533 + * - a weak pullup to the X+ connector 534 + * - a strong ground at the Y- connector 535 + */ 536 + mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0); 537 + mxs_lradc_reg_set(lradc, mxs_lradc_touch_detect_bit(lradc), 538 + LRADC_CTRL0); 539 + } 540 + 541 + /* 542 + * YP(meas)--+-------------+ 543 + * | |--+ 544 + * | | | 545 + * YM(open)--+-------------+ | 546 + * +--------------+ 547 + * | | 548 + * XP(+) XM(-) 549 + * 550 + * (+) means here 1.85 V 551 + * (-) means here GND 552 + */ 553 + static void mxs_lradc_prepare_x_pos(struct mxs_lradc *lradc) 554 + { 555 + mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0); 556 + mxs_lradc_reg_set(lradc, mxs_lradc_drive_x_plate(lradc), LRADC_CTRL0); 557 + 558 + lradc->cur_plate = LRADC_SAMPLE_X; 559 + mxs_lradc_setup_ts_channel(lradc, TS_CH_YP); 560 + } 561 + 562 + /* 563 + * YP(+)--+-------------+ 564 + * | |--+ 565 + * | | | 566 + * YM(-)--+-------------+ | 567 + * +--------------+ 568 + * | | 569 + * XP(open) XM(meas) 570 + * 571 + * (+) means here 1.85 V 572 + * (-) means here GND 573 + */ 574 + static void mxs_lradc_prepare_y_pos(struct mxs_lradc *lradc) 575 + { 576 + mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0); 577 + mxs_lradc_reg_set(lradc, mxs_lradc_drive_y_plate(lradc), LRADC_CTRL0); 578 + 579 + lradc->cur_plate = LRADC_SAMPLE_Y; 580 + mxs_lradc_setup_ts_channel(lradc, TS_CH_XM); 581 + } 582 + 583 + /* 584 + * YP(+)--+-------------+ 585 + * | |--+ 586 + * | | | 587 + * YM(meas)--+-------------+ | 588 + * +--------------+ 589 + * | | 590 + * XP(meas) XM(-) 591 + * 592 + * (+) means here 1.85 V 593 + * (-) means here GND 594 + */ 595 + static void mxs_lradc_prepare_pressure(struct mxs_lradc *lradc) 596 + { 597 + mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0); 598 + mxs_lradc_reg_set(lradc, mxs_lradc_drive_pressure(lradc), LRADC_CTRL0); 599 + 600 + lradc->cur_plate = LRADC_SAMPLE_PRESSURE; 601 + mxs_lradc_setup_ts_pressure(lradc, TS_CH_XP, TS_CH_YM); 602 + } 603 + 604 + static void mxs_lradc_enable_touch_detection(struct mxs_lradc *lradc) 605 + { 606 + mxs_lradc_setup_touch_detection(lradc); 607 + 608 + lradc->cur_plate = LRADC_TOUCH; 609 + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ | 610 + LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, LRADC_CTRL1); 611 + mxs_lradc_reg_set(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, LRADC_CTRL1); 612 + } 613 + 614 + static void mxs_lradc_report_ts_event(struct mxs_lradc *lradc) 615 + { 616 + input_report_abs(lradc->ts_input, ABS_X, lradc->ts_x_pos); 617 + input_report_abs(lradc->ts_input, ABS_Y, lradc->ts_y_pos); 618 + input_report_abs(lradc->ts_input, ABS_PRESSURE, lradc->ts_pressure); 619 + input_report_key(lradc->ts_input, BTN_TOUCH, 1); 620 + input_sync(lradc->ts_input); 621 + } 622 + 623 + static void mxs_lradc_complete_touch_event(struct mxs_lradc *lradc) 624 + { 625 + mxs_lradc_setup_touch_detection(lradc); 626 + lradc->cur_plate = LRADC_SAMPLE_VALID; 627 + /* 628 + * start a dummy conversion to burn time to settle the signals 629 + * note: we are not interested in the conversion's value 630 + */ 631 + mxs_lradc_reg_wrt(lradc, 0, LRADC_CH(5)); 632 + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ(5), LRADC_CTRL1); 633 + mxs_lradc_reg_set(lradc, LRADC_CTRL1_LRADC_IRQ_EN(5), LRADC_CTRL1); 634 + mxs_lradc_reg_wrt(lradc, LRADC_DELAY_TRIGGER(1 << 5) | 635 + LRADC_DELAY_KICK | LRADC_DELAY_DELAY(10), /* waste 5 ms */ 636 + LRADC_DELAY(2)); 637 + } 638 + 639 + /* 640 + * in order to avoid false measurements, report only samples where 641 + * the surface is still touched after the position measurement 642 + */ 643 + static void mxs_lradc_finish_touch_event(struct mxs_lradc *lradc, bool valid) 644 + { 645 + /* if it is still touched, report the sample */ 646 + if (valid && mxs_lradc_check_touch_event(lradc)) { 647 + lradc->ts_valid = true; 648 + mxs_lradc_report_ts_event(lradc); 649 + } 650 + 651 + /* if it is even still touched, continue with the next measurement */ 652 + if (mxs_lradc_check_touch_event(lradc)) { 653 + mxs_lradc_prepare_y_pos(lradc); 654 + return; 655 + } 656 + 657 + if (lradc->ts_valid) { 658 + /* signal the release */ 659 + lradc->ts_valid = false; 660 + input_report_key(lradc->ts_input, BTN_TOUCH, 0); 661 + input_sync(lradc->ts_input); 662 + } 663 + 664 + /* if it is released, wait for the next touch via IRQ */ 665 + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ, LRADC_CTRL1); 666 + mxs_lradc_reg_set(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, LRADC_CTRL1); 667 + } 668 + 669 + /* touchscreen's state machine */ 670 + static void mxs_lradc_handle_touch(struct mxs_lradc *lradc) 671 + { 672 + int val; 673 + 674 + switch (lradc->cur_plate) { 675 + case LRADC_TOUCH: 676 + /* 677 + * start with the Y-pos, because it uses nearly the same plate 678 + * settings like the touch detection 679 + */ 680 + if (mxs_lradc_check_touch_event(lradc)) { 681 + mxs_lradc_reg_clear(lradc, 682 + LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, 683 + LRADC_CTRL1); 684 + mxs_lradc_prepare_y_pos(lradc); 685 + } 686 + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ, 687 + LRADC_CTRL1); 688 + return; 689 + 690 + case LRADC_SAMPLE_Y: 691 + val = mxs_lradc_read_ts_channel(lradc); 692 + if (val < 0) { 693 + mxs_lradc_enable_touch_detection(lradc); /* re-start */ 694 + return; 695 + } 696 + lradc->ts_y_pos = val; 697 + mxs_lradc_prepare_x_pos(lradc); 698 + return; 699 + 700 + case LRADC_SAMPLE_X: 701 + val = mxs_lradc_read_ts_channel(lradc); 702 + if (val < 0) { 703 + mxs_lradc_enable_touch_detection(lradc); /* re-start */ 704 + return; 705 + } 706 + lradc->ts_x_pos = val; 707 + mxs_lradc_prepare_pressure(lradc); 708 + return; 709 + 710 + case LRADC_SAMPLE_PRESSURE: 711 + lradc->ts_pressure = 712 + mxs_lradc_read_ts_pressure(lradc, TS_CH_XP, TS_CH_YM); 713 + mxs_lradc_complete_touch_event(lradc); 714 + return; 715 + 716 + case LRADC_SAMPLE_VALID: 717 + val = mxs_lradc_read_ts_channel(lradc); /* ignore the value */ 718 + mxs_lradc_finish_touch_event(lradc, 1); 719 + break; 720 + } 721 + } 722 + 362 723 /* 363 724 * Raw I/O operations 364 725 */ ··· 828 393 static const struct iio_info mxs_lradc_iio_info = { 829 394 .driver_module = THIS_MODULE, 830 395 .read_raw = mxs_lradc_read_raw, 831 - }; 832 - 833 - /* 834 - * Touchscreen handling 835 - */ 836 - enum lradc_ts_plate { 837 - LRADC_SAMPLE_X, 838 - LRADC_SAMPLE_Y, 839 - LRADC_SAMPLE_PRESSURE, 840 396 }; 841 397 842 398 static int mxs_lradc_ts_touched(struct mxs_lradc *lradc) ··· 987 561 input_report_key(lradc->ts_input, BTN_TOUCH, 0); 988 562 input_sync(lradc->ts_input); 989 563 990 - /* Do not restart the TS IRQ if the driver is shutting down. */ 991 - if (lradc->stop_touchscreen) 992 - return; 993 - 994 564 /* Restart the touchscreen interrupts. */ 995 565 mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ, LRADC_CTRL1); 996 566 mxs_lradc_reg_set(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, LRADC_CTRL1); ··· 996 574 { 997 575 struct mxs_lradc *lradc = input_get_drvdata(dev); 998 576 999 - /* The touchscreen is starting. */ 1000 - lradc->stop_touchscreen = false; 1001 - 1002 577 /* Enable the touch-detect circuitry. */ 1003 - mxs_lradc_reg_set(lradc, mxs_lradc_touch_detect_bit(lradc), 1004 - LRADC_CTRL0); 1005 - 1006 - /* Enable the touch-detect IRQ. */ 1007 - mxs_lradc_reg_set(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, LRADC_CTRL1); 578 + mxs_lradc_enable_touch_detection(lradc); 1008 579 1009 580 return 0; 581 + } 582 + 583 + static void mxs_lradc_disable_ts(struct mxs_lradc *lradc) 584 + { 585 + /* stop all interrupts from firing */ 586 + mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN | 587 + LRADC_CTRL1_LRADC_IRQ_EN(2) | LRADC_CTRL1_LRADC_IRQ_EN(3) | 588 + LRADC_CTRL1_LRADC_IRQ_EN(4) | LRADC_CTRL1_LRADC_IRQ_EN(5), 589 + LRADC_CTRL1); 590 + 591 + /* Power-down touchscreen touch-detect circuitry. */ 592 + mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0); 1010 593 } 1011 594 1012 595 static void mxs_lradc_ts_close(struct input_dev *dev) 1013 596 { 1014 597 struct mxs_lradc *lradc = input_get_drvdata(dev); 1015 598 1016 - /* Indicate the touchscreen is stopping. */ 1017 - lradc->stop_touchscreen = true; 1018 - mb(); 1019 - 1020 - /* Wait until touchscreen thread finishes any possible remnants. */ 1021 - cancel_work_sync(&lradc->ts_work); 1022 - 1023 - /* Disable touchscreen touch-detect IRQ. */ 1024 - mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, 1025 - LRADC_CTRL1); 1026 - 1027 - /* Power-down touchscreen touch-detect circuitry. */ 1028 - mxs_lradc_reg_clear(lradc, mxs_lradc_touch_detect_bit(lradc), LRADC_CTRL0); 599 + mxs_lradc_disable_ts(lradc); 1029 600 } 1030 601 1031 602 static int mxs_lradc_ts_register(struct mxs_lradc *lradc) ··· 1064 649 if (!lradc->use_touchscreen) 1065 650 return; 1066 651 1067 - cancel_work_sync(&lradc->ts_work); 1068 - 652 + mxs_lradc_disable_ts(lradc); 1069 653 input_unregister_device(lradc->ts_input); 1070 654 } 1071 655 ··· 1077 663 struct mxs_lradc *lradc = iio_priv(iio); 1078 664 unsigned long reg = readl(lradc->base + LRADC_CTRL1); 1079 665 const uint32_t ts_irq_mask = 1080 - LRADC_CTRL1_TOUCH_DETECT_IRQ_EN | 1081 - LRADC_CTRL1_TOUCH_DETECT_IRQ; 666 + LRADC_CTRL1_TOUCH_DETECT_IRQ | 667 + LRADC_CTRL1_LRADC_IRQ(2) | 668 + LRADC_CTRL1_LRADC_IRQ(3) | 669 + LRADC_CTRL1_LRADC_IRQ(4) | 670 + LRADC_CTRL1_LRADC_IRQ(5); 1082 671 1083 672 if (!(reg & mxs_lradc_irq_mask(lradc))) 1084 673 return IRQ_NONE; 1085 674 1086 - /* 1087 - * Touchscreen IRQ handling code has priority and therefore 1088 - * is placed here. In case touchscreen IRQ arrives, disable 1089 - * it ASAP 1090 - */ 1091 - if (reg & LRADC_CTRL1_TOUCH_DETECT_IRQ) { 1092 - mxs_lradc_reg_clear(lradc, ts_irq_mask, LRADC_CTRL1); 1093 - if (!lradc->stop_touchscreen) 1094 - schedule_work(&lradc->ts_work); 1095 - } 675 + if (lradc->use_touchscreen && (reg & ts_irq_mask)) 676 + mxs_lradc_handle_touch(lradc); 1096 677 1097 678 if (iio_buffer_enabled(iio)) 1098 679 iio_trigger_poll(iio->trig, iio_get_time_ns()); ··· 1384 975 }; 1385 976 MODULE_DEVICE_TABLE(of, mxs_lradc_dt_ids); 1386 977 978 + static int mxs_lradc_probe_touchscreen(struct mxs_lradc *lradc, 979 + struct device_node *lradc_node) 980 + { 981 + /* TODO retrieve from device tree */ 982 + lradc->over_sample_cnt = 4; 983 + lradc->over_sample_delay = 2; 984 + lradc->settling_delay = 10; 985 + 986 + return 0; 987 + } 988 + 1387 989 static int mxs_lradc_probe(struct platform_device *pdev) 1388 990 { 1389 991 const struct of_device_id *of_id = ··· 1407 987 struct iio_dev *iio; 1408 988 struct resource *iores; 1409 989 uint32_t ts_wires = 0; 1410 - int ret = 0; 990 + int ret = 0, touch_ret; 1411 991 int i; 1412 992 1413 993 /* Allocate the IIO device. */ ··· 1438 1018 return ret; 1439 1019 } 1440 1020 1441 - INIT_WORK(&lradc->ts_work, mxs_lradc_ts_work); 1021 + touch_ret = mxs_lradc_probe_touchscreen(lradc, node); 1442 1022 1443 1023 /* Check if touchscreen is enabled in DT. */ 1444 1024 ret = of_property_read_u32(node, "fsl,lradc-touchscreen-wires", ··· 1501 1081 goto err_dev; 1502 1082 1503 1083 /* Register the touchscreen input device. */ 1504 - ret = mxs_lradc_ts_register(lradc); 1505 - if (ret) 1506 - goto err_ts_register; 1084 + if (touch_ret == 0) { 1085 + ret = mxs_lradc_ts_register(lradc); 1086 + if (ret) 1087 + goto err_ts_register; 1088 + } 1507 1089 1508 1090 /* Register IIO device. */ 1509 1091 ret = iio_device_register(iio);