1 /* 2 * Copyright (C) 2006-2008 Michael Hennerich, Analog Devices Inc. 3 * 4 * Description: AD7877 based touchscreen, sensor (ADCs), DAC and GPIO driver 5 * Based on: ads7846.c 6 * 7 * Bugs: Enter bugs at http://blackfin.uclinux.org/ 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, see the file COPYING, or write 21 * to the Free Software Foundation, Inc., 22 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23 * 24 * History: 25 * Copyright (c) 2005 David Brownell 26 * Copyright (c) 2006 Nokia Corporation 27 * Various changes: Imre Deak <imre.deak@nokia.com> 28 * 29 * Using code from: 30 * - corgi_ts.c 31 * Copyright (C) 2004-2005 Richard Purdie 32 * - omap_ts.[hc], ads7846.h, ts_osk.c 33 * Copyright (C) 2002 MontaVista Software 34 * Copyright (C) 2004 Texas Instruments 35 * Copyright (C) 2005 Dirk Behme 36 */ 37 38 39 #include <linux/device.h> 40 #include <linux/init.h> 41 #include <linux/delay.h> 42 #include <linux/input.h> 43 #include <linux/interrupt.h> 44 #include <linux/slab.h> 45 #include <linux/spi/spi.h> 46 #include <linux/spi/ad7877.h> 47 #include <asm/irq.h> 48 49 #define TS_PEN_UP_TIMEOUT msecs_to_jiffies(50) 50 51 #define MAX_SPI_FREQ_HZ 20000000 52 #define MAX_12BIT ((1<<12)-1) 53 54 #define AD7877_REG_ZEROS 0 55 #define AD7877_REG_CTRL1 1 56 #define AD7877_REG_CTRL2 2 57 #define AD7877_REG_ALERT 3 58 #define AD7877_REG_AUX1HIGH 4 59 #define AD7877_REG_AUX1LOW 5 60 #define AD7877_REG_BAT1HIGH 6 61 #define AD7877_REG_BAT1LOW 7 62 #define AD7877_REG_BAT2HIGH 8 63 #define AD7877_REG_BAT2LOW 9 64 #define AD7877_REG_TEMP1HIGH 10 65 #define AD7877_REG_TEMP1LOW 11 66 #define AD7877_REG_SEQ0 12 67 #define AD7877_REG_SEQ1 13 68 #define AD7877_REG_DAC 14 69 #define AD7877_REG_NONE1 15 70 #define AD7877_REG_EXTWRITE 15 71 #define AD7877_REG_XPLUS 16 72 #define AD7877_REG_YPLUS 17 73 #define AD7877_REG_Z2 18 74 #define AD7877_REG_aux1 19 75 #define AD7877_REG_aux2 20 76 #define AD7877_REG_aux3 21 77 #define AD7877_REG_bat1 22 78 #define AD7877_REG_bat2 23 79 #define AD7877_REG_temp1 24 80 #define AD7877_REG_temp2 25 81 #define AD7877_REG_Z1 26 82 #define AD7877_REG_GPIOCTRL1 27 83 #define AD7877_REG_GPIOCTRL2 28 84 #define AD7877_REG_GPIODATA 29 85 #define AD7877_REG_NONE2 30 86 #define AD7877_REG_NONE3 31 87 88 #define AD7877_SEQ_YPLUS_BIT (1<<11) 89 #define AD7877_SEQ_XPLUS_BIT (1<<10) 90 #define AD7877_SEQ_Z2_BIT (1<<9) 91 #define AD7877_SEQ_AUX1_BIT (1<<8) 92 #define AD7877_SEQ_AUX2_BIT (1<<7) 93 #define AD7877_SEQ_AUX3_BIT (1<<6) 94 #define AD7877_SEQ_BAT1_BIT (1<<5) 95 #define AD7877_SEQ_BAT2_BIT (1<<4) 96 #define AD7877_SEQ_TEMP1_BIT (1<<3) 97 #define AD7877_SEQ_TEMP2_BIT (1<<2) 98 #define AD7877_SEQ_Z1_BIT (1<<1) 99 100 enum { 101 AD7877_SEQ_YPOS = 0, 102 AD7877_SEQ_XPOS = 1, 103 AD7877_SEQ_Z2 = 2, 104 AD7877_SEQ_AUX1 = 3, 105 AD7877_SEQ_AUX2 = 4, 106 AD7877_SEQ_AUX3 = 5, 107 AD7877_SEQ_BAT1 = 6, 108 AD7877_SEQ_BAT2 = 7, 109 AD7877_SEQ_TEMP1 = 8, 110 AD7877_SEQ_TEMP2 = 9, 111 AD7877_SEQ_Z1 = 10, 112 AD7877_NR_SENSE = 11, 113 }; 114 115 /* DAC Register Default RANGE 0 to Vcc, Volatge Mode, DAC On */ 116 #define AD7877_DAC_CONF 0x1 117 118 /* If gpio3 is set AUX3/GPIO3 acts as GPIO Output */ 119 #define AD7877_EXTW_GPIO_3_CONF 0x1C4 120 #define AD7877_EXTW_GPIO_DATA 0x200 121 122 /* Control REG 2 */ 123 #define AD7877_TMR(x) ((x & 0x3) << 0) 124 #define AD7877_REF(x) ((x & 0x1) << 2) 125 #define AD7877_POL(x) ((x & 0x1) << 3) 126 #define AD7877_FCD(x) ((x & 0x3) << 4) 127 #define AD7877_PM(x) ((x & 0x3) << 6) 128 #define AD7877_ACQ(x) ((x & 0x3) << 8) 129 #define AD7877_AVG(x) ((x & 0x3) << 10) 130 131 /* Control REG 1 */ 132 #define AD7877_SER (1 << 11) /* non-differential */ 133 #define AD7877_DFR (0 << 11) /* differential */ 134 135 #define AD7877_MODE_NOC (0) /* Do not convert */ 136 #define AD7877_MODE_SCC (1) /* Single channel conversion */ 137 #define AD7877_MODE_SEQ0 (2) /* Sequence 0 in Slave Mode */ 138 #define AD7877_MODE_SEQ1 (3) /* Sequence 1 in Master Mode */ 139 140 #define AD7877_CHANADD(x) ((x&0xF)<<7) 141 #define AD7877_READADD(x) ((x)<<2) 142 #define AD7877_WRITEADD(x) ((x)<<12) 143 144 #define AD7877_READ_CHAN(x) (AD7877_WRITEADD(AD7877_REG_CTRL1) | AD7877_SER | \ 145 AD7877_MODE_SCC | AD7877_CHANADD(AD7877_REG_ ## x) | \ 146 AD7877_READADD(AD7877_REG_ ## x)) 147 148 #define AD7877_MM_SEQUENCE (AD7877_SEQ_YPLUS_BIT | AD7877_SEQ_XPLUS_BIT | \ 149 AD7877_SEQ_Z2_BIT | AD7877_SEQ_Z1_BIT) 150 151 /* 152 * Non-touchscreen sensors only use single-ended conversions. 153 */ 154 155 struct ser_req { 156 u16 reset; 157 u16 ref_on; 158 u16 command; 159 u16 sample; 160 struct spi_message msg; 161 struct spi_transfer xfer[6]; 162 }; 163 164 struct ad7877 { 165 struct input_dev *input; 166 char phys[32]; 167 168 struct spi_device *spi; 169 u16 model; 170 u16 vref_delay_usecs; 171 u16 x_plate_ohms; 172 u16 pressure_max; 173 174 u16 cmd_crtl1; 175 u16 cmd_crtl2; 176 u16 cmd_dummy; 177 u16 dac; 178 179 u8 stopacq_polarity; 180 u8 first_conversion_delay; 181 u8 acquisition_time; 182 u8 averaging; 183 u8 pen_down_acc_interval; 184 185 u16 conversion_data[AD7877_NR_SENSE]; 186 187 struct spi_transfer xfer[AD7877_NR_SENSE + 2]; 188 struct spi_message msg; 189 190 struct mutex mutex; 191 unsigned disabled:1; /* P: mutex */ 192 unsigned gpio3:1; /* P: mutex */ 193 unsigned gpio4:1; /* P: mutex */ 194 195 spinlock_t lock; 196 struct timer_list timer; /* P: lock */ 197 unsigned pending:1; /* P: lock */ 198 }; 199 200 static int gpio3; 201 module_param(gpio3, int, 0); 202 MODULE_PARM_DESC(gpio3, "If gpio3 is set to 1 AUX3 acts as GPIO3"); 203 204 /* 205 * ad7877_read/write are only used for initial setup and for sysfs controls. 206 * The main traffic is done using spi_async() in the interrupt handler. 207 */ 208 209 static int ad7877_read(struct spi_device *spi, u16 reg) 210 { 211 struct ser_req *req; 212 int status, ret; 213 214 req = kzalloc(sizeof *req, GFP_KERNEL); 215 if (!req) 216 return -ENOMEM; 217 218 spi_message_init(&req->msg); 219 220 req->command = (u16) (AD7877_WRITEADD(AD7877_REG_CTRL1) | 221 AD7877_READADD(reg)); 222 req->xfer[0].tx_buf = &req->command; 223 req->xfer[0].len = 2; 224 225 req->xfer[1].rx_buf = &req->sample; 226 req->xfer[1].len = 2; 227 228 spi_message_add_tail(&req->xfer[0], &req->msg); 229 spi_message_add_tail(&req->xfer[1], &req->msg); 230 231 status = spi_sync(spi, &req->msg); 232 ret = status ? : req->sample; 233 234 kfree(req); 235 236 return ret; 237 } 238 239 static int ad7877_write(struct spi_device *spi, u16 reg, u16 val) 240 { 241 struct ser_req *req; 242 int status; 243 244 req = kzalloc(sizeof *req, GFP_KERNEL); 245 if (!req) 246 return -ENOMEM; 247 248 spi_message_init(&req->msg); 249 250 req->command = (u16) (AD7877_WRITEADD(reg) | (val & MAX_12BIT)); 251 req->xfer[0].tx_buf = &req->command; 252 req->xfer[0].len = 2; 253 254 spi_message_add_tail(&req->xfer[0], &req->msg); 255 256 status = spi_sync(spi, &req->msg); 257 258 kfree(req); 259 260 return status; 261 } 262 263 static int ad7877_read_adc(struct spi_device *spi, unsigned command) 264 { 265 struct ad7877 *ts = dev_get_drvdata(&spi->dev); 266 struct ser_req *req; 267 int status; 268 int sample; 269 int i; 270 271 req = kzalloc(sizeof *req, GFP_KERNEL); 272 if (!req) 273 return -ENOMEM; 274 275 spi_message_init(&req->msg); 276 277 /* activate reference, so it has time to settle; */ 278 req->ref_on = AD7877_WRITEADD(AD7877_REG_CTRL2) | 279 AD7877_POL(ts->stopacq_polarity) | 280 AD7877_AVG(0) | AD7877_PM(2) | AD7877_TMR(0) | 281 AD7877_ACQ(ts->acquisition_time) | AD7877_FCD(0); 282 283 req->reset = AD7877_WRITEADD(AD7877_REG_CTRL1) | AD7877_MODE_NOC; 284 285 req->command = (u16) command; 286 287 req->xfer[0].tx_buf = &req->reset; 288 req->xfer[0].len = 2; 289 290 req->xfer[1].tx_buf = &req->ref_on; 291 req->xfer[1].len = 2; 292 req->xfer[1].delay_usecs = ts->vref_delay_usecs; 293 294 req->xfer[2].tx_buf = &req->command; 295 req->xfer[2].len = 2; 296 req->xfer[2].delay_usecs = ts->vref_delay_usecs; 297 298 req->xfer[3].rx_buf = &req->sample; 299 req->xfer[3].len = 2; 300 301 req->xfer[4].tx_buf = &ts->cmd_crtl2; /*REF OFF*/ 302 req->xfer[4].len = 2; 303 304 req->xfer[5].tx_buf = &ts->cmd_crtl1; /*DEFAULT*/ 305 req->xfer[5].len = 2; 306 307 /* group all the transfers together, so we can't interfere with 308 * reading touchscreen state; disable penirq while sampling 309 */ 310 for (i = 0; i < 6; i++) 311 spi_message_add_tail(&req->xfer[i], &req->msg); 312 313 status = spi_sync(spi, &req->msg); 314 sample = req->sample; 315 316 kfree(req); 317 318 return status ? : sample; 319 } 320 321 static void ad7877_rx(struct ad7877 *ts) 322 { 323 struct input_dev *input_dev = ts->input; 324 unsigned Rt; 325 u16 x, y, z1, z2; 326 327 x = ts->conversion_data[AD7877_SEQ_XPOS] & MAX_12BIT; 328 y = ts->conversion_data[AD7877_SEQ_YPOS] & MAX_12BIT; 329 z1 = ts->conversion_data[AD7877_SEQ_Z1] & MAX_12BIT; 330 z2 = ts->conversion_data[AD7877_SEQ_Z2] & MAX_12BIT; 331 332 /* 333 * The samples processed here are already preprocessed by the AD7877. 334 * The preprocessing function consists of an averaging filter. 335 * The combination of 'first conversion delay' and averaging provides a robust solution, 336 * discarding the spurious noise in the signal and keeping only the data of interest. 337 * The size of the averaging filter is programmable. (dev.platform_data, see linux/spi/ad7877.h) 338 * Other user-programmable conversion controls include variable acquisition time, 339 * and first conversion delay. Up to 16 averages can be taken per conversion. 340 */ 341 342 if (likely(x && z1)) { 343 /* compute touch pressure resistance using equation #1 */ 344 Rt = (z2 - z1) * x * ts->x_plate_ohms; 345 Rt /= z1; 346 Rt = (Rt + 2047) >> 12; 347 348 input_report_abs(input_dev, ABS_X, x); 349 input_report_abs(input_dev, ABS_Y, y); 350 input_report_abs(input_dev, ABS_PRESSURE, Rt); 351 input_sync(input_dev); 352 } 353 } 354 355 static inline void ad7877_ts_event_release(struct ad7877 *ts) 356 { 357 struct input_dev *input_dev = ts->input; 358 359 input_report_abs(input_dev, ABS_PRESSURE, 0); 360 input_sync(input_dev); 361 } 362 363 static void ad7877_timer(unsigned long handle) 364 { 365 struct ad7877 *ts = (void *)handle; 366 367 ad7877_ts_event_release(ts); 368 } 369 370 static irqreturn_t ad7877_irq(int irq, void *handle) 371 { 372 struct ad7877 *ts = handle; 373 unsigned long flags; 374 int status; 375 376 /* 377 * The repeated conversion sequencer controlled by TMR kicked off 378 * too fast. We ignore the last and process the sample sequence 379 * currently in the queue. It can't be older than 9.4ms, and we 380 * need to avoid that ts->msg doesn't get issued twice while in work. 381 */ 382 383 spin_lock_irqsave(&ts->lock, flags); 384 if (!ts->pending) { 385 ts->pending = 1; 386 387 status = spi_async(ts->spi, &ts->msg); 388 if (status) 389 dev_err(&ts->spi->dev, "spi_sync --> %d\n", status); 390 } 391 spin_unlock_irqrestore(&ts->lock, flags); 392 393 return IRQ_HANDLED; 394 } 395 396 static void ad7877_callback(void *_ts) 397 { 398 struct ad7877 *ts = _ts; 399 400 spin_lock_irq(&ts->lock); 401 402 ad7877_rx(ts); 403 ts->pending = 0; 404 mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT); 405 406 spin_unlock_irq(&ts->lock); 407 } 408 409 static void ad7877_disable(struct ad7877 *ts) 410 { 411 mutex_lock(&ts->mutex); 412 413 if (!ts->disabled) { 414 ts->disabled = 1; 415 disable_irq(ts->spi->irq); 416 417 /* Wait for spi_async callback */ 418 while (ts->pending) 419 msleep(1); 420 421 if (del_timer_sync(&ts->timer)) 422 ad7877_ts_event_release(ts); 423 } 424 425 /* we know the chip's in lowpower mode since we always 426 * leave it that way after every request 427 */ 428 429 mutex_unlock(&ts->mutex); 430 } 431 432 static void ad7877_enable(struct ad7877 *ts) 433 { 434 mutex_lock(&ts->mutex); 435 436 if (ts->disabled) { 437 ts->disabled = 0; 438 enable_irq(ts->spi->irq); 439 } 440 441 mutex_unlock(&ts->mutex); 442 } 443 444 #define SHOW(name) static ssize_t \ 445 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \ 446 { \ 447 struct ad7877 *ts = dev_get_drvdata(dev); \ 448 ssize_t v = ad7877_read_adc(ts->spi, \ 449 AD7877_READ_CHAN(name)); \ 450 if (v < 0) \ 451 return v; \ 452 return sprintf(buf, "%u\n", (unsigned) v); \ 453 } \ 454 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL); 455 456 SHOW(aux1) 457 SHOW(aux2) 458 SHOW(aux3) 459 SHOW(bat1) 460 SHOW(bat2) 461 SHOW(temp1) 462 SHOW(temp2) 463 464 static ssize_t ad7877_disable_show(struct device *dev, 465 struct device_attribute *attr, char *buf) 466 { 467 struct ad7877 *ts = dev_get_drvdata(dev); 468 469 return sprintf(buf, "%u\n", ts->disabled); 470 } 471 472 static ssize_t ad7877_disable_store(struct device *dev, 473 struct device_attribute *attr, 474 const char *buf, size_t count) 475 { 476 struct ad7877 *ts = dev_get_drvdata(dev); 477 unsigned long val; 478 int error; 479 480 error = strict_strtoul(buf, 10, &val); 481 if (error) 482 return error; 483 484 if (val) 485 ad7877_disable(ts); 486 else 487 ad7877_enable(ts); 488 489 return count; 490 } 491 492 static DEVICE_ATTR(disable, 0664, ad7877_disable_show, ad7877_disable_store); 493 494 static ssize_t ad7877_dac_show(struct device *dev, 495 struct device_attribute *attr, char *buf) 496 { 497 struct ad7877 *ts = dev_get_drvdata(dev); 498 499 return sprintf(buf, "%u\n", ts->dac); 500 } 501 502 static ssize_t ad7877_dac_store(struct device *dev, 503 struct device_attribute *attr, 504 const char *buf, size_t count) 505 { 506 struct ad7877 *ts = dev_get_drvdata(dev); 507 unsigned long val; 508 int error; 509 510 error = strict_strtoul(buf, 10, &val); 511 if (error) 512 return error; 513 514 mutex_lock(&ts->mutex); 515 ts->dac = val & 0xFF; 516 ad7877_write(ts->spi, AD7877_REG_DAC, (ts->dac << 4) | AD7877_DAC_CONF); 517 mutex_unlock(&ts->mutex); 518 519 return count; 520 } 521 522 static DEVICE_ATTR(dac, 0664, ad7877_dac_show, ad7877_dac_store); 523 524 static ssize_t ad7877_gpio3_show(struct device *dev, 525 struct device_attribute *attr, char *buf) 526 { 527 struct ad7877 *ts = dev_get_drvdata(dev); 528 529 return sprintf(buf, "%u\n", ts->gpio3); 530 } 531 532 static ssize_t ad7877_gpio3_store(struct device *dev, 533 struct device_attribute *attr, 534 const char *buf, size_t count) 535 { 536 struct ad7877 *ts = dev_get_drvdata(dev); 537 unsigned long val; 538 int error; 539 540 error = strict_strtoul(buf, 10, &val); 541 if (error) 542 return error; 543 544 mutex_lock(&ts->mutex); 545 ts->gpio3 = !!val; 546 ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA | 547 (ts->gpio4 << 4) | (ts->gpio3 << 5)); 548 mutex_unlock(&ts->mutex); 549 550 return count; 551 } 552 553 static DEVICE_ATTR(gpio3, 0664, ad7877_gpio3_show, ad7877_gpio3_store); 554 555 static ssize_t ad7877_gpio4_show(struct device *dev, 556 struct device_attribute *attr, char *buf) 557 { 558 struct ad7877 *ts = dev_get_drvdata(dev); 559 560 return sprintf(buf, "%u\n", ts->gpio4); 561 } 562 563 static ssize_t ad7877_gpio4_store(struct device *dev, 564 struct device_attribute *attr, 565 const char *buf, size_t count) 566 { 567 struct ad7877 *ts = dev_get_drvdata(dev); 568 unsigned long val; 569 int error; 570 571 error = strict_strtoul(buf, 10, &val); 572 if (error) 573 return error; 574 575 mutex_lock(&ts->mutex); 576 ts->gpio4 = !!val; 577 ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA | 578 (ts->gpio4 << 4) | (ts->gpio3 << 5)); 579 mutex_unlock(&ts->mutex); 580 581 return count; 582 } 583 584 static DEVICE_ATTR(gpio4, 0664, ad7877_gpio4_show, ad7877_gpio4_store); 585 586 static struct attribute *ad7877_attributes[] = { 587 &dev_attr_temp1.attr, 588 &dev_attr_temp2.attr, 589 &dev_attr_aux1.attr, 590 &dev_attr_aux2.attr, 591 &dev_attr_bat1.attr, 592 &dev_attr_bat2.attr, 593 &dev_attr_disable.attr, 594 &dev_attr_dac.attr, 595 &dev_attr_gpio4.attr, 596 NULL 597 }; 598 599 static const struct attribute_group ad7877_attr_group = { 600 .attrs = ad7877_attributes, 601 }; 602 603 static void ad7877_setup_ts_def_msg(struct spi_device *spi, struct ad7877 *ts) 604 { 605 struct spi_message *m; 606 int i; 607 608 ts->cmd_crtl2 = AD7877_WRITEADD(AD7877_REG_CTRL2) | 609 AD7877_POL(ts->stopacq_polarity) | 610 AD7877_AVG(ts->averaging) | AD7877_PM(1) | 611 AD7877_TMR(ts->pen_down_acc_interval) | 612 AD7877_ACQ(ts->acquisition_time) | 613 AD7877_FCD(ts->first_conversion_delay); 614 615 ad7877_write(spi, AD7877_REG_CTRL2, ts->cmd_crtl2); 616 617 ts->cmd_crtl1 = AD7877_WRITEADD(AD7877_REG_CTRL1) | 618 AD7877_READADD(AD7877_REG_XPLUS-1) | 619 AD7877_MODE_SEQ1 | AD7877_DFR; 620 621 ad7877_write(spi, AD7877_REG_CTRL1, ts->cmd_crtl1); 622 623 ts->cmd_dummy = 0; 624 625 m = &ts->msg; 626 627 spi_message_init(m); 628 629 m->complete = ad7877_callback; 630 m->context = ts; 631 632 ts->xfer[0].tx_buf = &ts->cmd_crtl1; 633 ts->xfer[0].len = 2; 634 635 spi_message_add_tail(&ts->xfer[0], m); 636 637 ts->xfer[1].tx_buf = &ts->cmd_dummy; /* Send ZERO */ 638 ts->xfer[1].len = 2; 639 640 spi_message_add_tail(&ts->xfer[1], m); 641 642 for (i = 0; i < 11; i++) { 643 ts->xfer[i + 2].rx_buf = &ts->conversion_data[AD7877_SEQ_YPOS + i]; 644 ts->xfer[i + 2].len = 2; 645 spi_message_add_tail(&ts->xfer[i + 2], m); 646 } 647 } 648 649 static int __devinit ad7877_probe(struct spi_device *spi) 650 { 651 struct ad7877 *ts; 652 struct input_dev *input_dev; 653 struct ad7877_platform_data *pdata = spi->dev.platform_data; 654 int err; 655 u16 verify; 656 657 if (!spi->irq) { 658 dev_dbg(&spi->dev, "no IRQ?\n"); 659 return -ENODEV; 660 } 661 662 if (!pdata) { 663 dev_dbg(&spi->dev, "no platform data?\n"); 664 return -ENODEV; 665 } 666 667 /* don't exceed max specified SPI CLK frequency */ 668 if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) { 669 dev_dbg(&spi->dev, "SPI CLK %d Hz?\n",spi->max_speed_hz); 670 return -EINVAL; 671 } 672 673 ts = kzalloc(sizeof(struct ad7877), GFP_KERNEL); 674 input_dev = input_allocate_device(); 675 if (!ts || !input_dev) { 676 err = -ENOMEM; 677 goto err_free_mem; 678 } 679 680 dev_set_drvdata(&spi->dev, ts); 681 ts->spi = spi; 682 ts->input = input_dev; 683 684 setup_timer(&ts->timer, ad7877_timer, (unsigned long) ts); 685 mutex_init(&ts->mutex); 686 spin_lock_init(&ts->lock); 687 688 ts->model = pdata->model ? : 7877; 689 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100; 690 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400; 691 ts->pressure_max = pdata->pressure_max ? : ~0; 692 693 ts->stopacq_polarity = pdata->stopacq_polarity; 694 ts->first_conversion_delay = pdata->first_conversion_delay; 695 ts->acquisition_time = pdata->acquisition_time; 696 ts->averaging = pdata->averaging; 697 ts->pen_down_acc_interval = pdata->pen_down_acc_interval; 698 699 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev)); 700 701 input_dev->name = "AD7877 Touchscreen"; 702 input_dev->phys = ts->phys; 703 input_dev->dev.parent = &spi->dev; 704 705 __set_bit(EV_ABS, input_dev->evbit); 706 __set_bit(ABS_X, input_dev->absbit); 707 __set_bit(ABS_Y, input_dev->absbit); 708 __set_bit(ABS_PRESSURE, input_dev->absbit); 709 710 input_set_abs_params(input_dev, ABS_X, 711 pdata->x_min ? : 0, 712 pdata->x_max ? : MAX_12BIT, 713 0, 0); 714 input_set_abs_params(input_dev, ABS_Y, 715 pdata->y_min ? : 0, 716 pdata->y_max ? : MAX_12BIT, 717 0, 0); 718 input_set_abs_params(input_dev, ABS_PRESSURE, 719 pdata->pressure_min, pdata->pressure_max, 0, 0); 720 721 ad7877_write(spi, AD7877_REG_SEQ1, AD7877_MM_SEQUENCE); 722 723 verify = ad7877_read(spi, AD7877_REG_SEQ1); 724 725 if (verify != AD7877_MM_SEQUENCE){ 726 dev_err(&spi->dev, "%s: Failed to probe %s\n", 727 dev_name(&spi->dev), input_dev->name); 728 err = -ENODEV; 729 goto err_free_mem; 730 } 731 732 if (gpio3) 733 ad7877_write(spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_3_CONF); 734 735 ad7877_setup_ts_def_msg(spi, ts); 736 737 /* Request AD7877 /DAV GPIO interrupt */ 738 739 err = request_irq(spi->irq, ad7877_irq, IRQF_TRIGGER_FALLING, 740 spi->dev.driver->name, ts); 741 if (err) { 742 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq); 743 goto err_free_mem; 744 } 745 746 err = sysfs_create_group(&spi->dev.kobj, &ad7877_attr_group); 747 if (err) 748 goto err_free_irq; 749 750 err = device_create_file(&spi->dev, 751 gpio3 ? &dev_attr_gpio3 : &dev_attr_aux3); 752 if (err) 753 goto err_remove_attr_group; 754 755 err = input_register_device(input_dev); 756 if (err) 757 goto err_remove_attr; 758 759 return 0; 760 761 err_remove_attr: 762 device_remove_file(&spi->dev, 763 gpio3 ? &dev_attr_gpio3 : &dev_attr_aux3); 764 err_remove_attr_group: 765 sysfs_remove_group(&spi->dev.kobj, &ad7877_attr_group); 766 err_free_irq: 767 free_irq(spi->irq, ts); 768 err_free_mem: 769 input_free_device(input_dev); 770 kfree(ts); 771 dev_set_drvdata(&spi->dev, NULL); 772 return err; 773 } 774 775 static int __devexit ad7877_remove(struct spi_device *spi) 776 { 777 struct ad7877 *ts = dev_get_drvdata(&spi->dev); 778 779 sysfs_remove_group(&spi->dev.kobj, &ad7877_attr_group); 780 device_remove_file(&spi->dev, 781 gpio3 ? &dev_attr_gpio3 : &dev_attr_aux3); 782 783 ad7877_disable(ts); 784 free_irq(ts->spi->irq, ts); 785 786 input_unregister_device(ts->input); 787 kfree(ts); 788 789 dev_dbg(&spi->dev, "unregistered touchscreen\n"); 790 dev_set_drvdata(&spi->dev, NULL); 791 792 return 0; 793 } 794 795 #ifdef CONFIG_PM 796 static int ad7877_suspend(struct spi_device *spi, pm_message_t message) 797 { 798 struct ad7877 *ts = dev_get_drvdata(&spi->dev); 799 800 ad7877_disable(ts); 801 802 return 0; 803 } 804 805 static int ad7877_resume(struct spi_device *spi) 806 { 807 struct ad7877 *ts = dev_get_drvdata(&spi->dev); 808 809 ad7877_enable(ts); 810 811 return 0; 812 } 813 #else 814 #define ad7877_suspend NULL 815 #define ad7877_resume NULL 816 #endif 817 818 static struct spi_driver ad7877_driver = { 819 .driver = { 820 .name = "ad7877", 821 .bus = &spi_bus_type, 822 .owner = THIS_MODULE, 823 }, 824 .probe = ad7877_probe, 825 .remove = __devexit_p(ad7877_remove), 826 .suspend = ad7877_suspend, 827 .resume = ad7877_resume, 828 }; 829 830 static int __init ad7877_init(void) 831 { 832 return spi_register_driver(&ad7877_driver); 833 } 834 module_init(ad7877_init); 835 836 static void __exit ad7877_exit(void) 837 { 838 spi_unregister_driver(&ad7877_driver); 839 } 840 module_exit(ad7877_exit); 841 842 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); 843 MODULE_DESCRIPTION("AD7877 touchscreen Driver"); 844 MODULE_LICENSE("GPL"); 845 MODULE_ALIAS("spi:ad7877"); 846