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