1 /* 2 * AD7879/AD7889 based touchscreen and GPIO driver 3 * 4 * Copyright (C) 2008-2010 Michael Hennerich, Analog Devices Inc. 5 * 6 * Licensed under the GPL-2 or later. 7 * 8 * History: 9 * Copyright (c) 2005 David Brownell 10 * Copyright (c) 2006 Nokia Corporation 11 * Various changes: Imre Deak <imre.deak@nokia.com> 12 * 13 * Using code from: 14 * - corgi_ts.c 15 * Copyright (C) 2004-2005 Richard Purdie 16 * - omap_ts.[hc], ads7846.h, ts_osk.c 17 * Copyright (C) 2002 MontaVista Software 18 * Copyright (C) 2004 Texas Instruments 19 * Copyright (C) 2005 Dirk Behme 20 * - ad7877.c 21 * Copyright (C) 2006-2008 Analog Devices Inc. 22 */ 23 24 #include <linux/device.h> 25 #include <linux/delay.h> 26 #include <linux/input.h> 27 #include <linux/interrupt.h> 28 #include <linux/irq.h> 29 #include <linux/property.h> 30 #include <linux/regmap.h> 31 #include <linux/slab.h> 32 #include <linux/gpio/driver.h> 33 34 #include <linux/input/touchscreen.h> 35 #include <linux/module.h> 36 #include "ad7879.h" 37 38 #define AD7879_REG_ZEROS 0 39 #define AD7879_REG_CTRL1 1 40 #define AD7879_REG_CTRL2 2 41 #define AD7879_REG_CTRL3 3 42 #define AD7879_REG_AUX1HIGH 4 43 #define AD7879_REG_AUX1LOW 5 44 #define AD7879_REG_TEMP1HIGH 6 45 #define AD7879_REG_TEMP1LOW 7 46 #define AD7879_REG_XPLUS 8 47 #define AD7879_REG_YPLUS 9 48 #define AD7879_REG_Z1 10 49 #define AD7879_REG_Z2 11 50 #define AD7879_REG_AUXVBAT 12 51 #define AD7879_REG_TEMP 13 52 #define AD7879_REG_REVID 14 53 54 /* Control REG 1 */ 55 #define AD7879_TMR(x) ((x & 0xFF) << 0) 56 #define AD7879_ACQ(x) ((x & 0x3) << 8) 57 #define AD7879_MODE_NOC (0 << 10) /* Do not convert */ 58 #define AD7879_MODE_SCC (1 << 10) /* Single channel conversion */ 59 #define AD7879_MODE_SEQ0 (2 << 10) /* Sequence 0 in Slave Mode */ 60 #define AD7879_MODE_SEQ1 (3 << 10) /* Sequence 1 in Master Mode */ 61 #define AD7879_MODE_INT (1 << 15) /* PENIRQ disabled INT enabled */ 62 63 /* Control REG 2 */ 64 #define AD7879_FCD(x) ((x & 0x3) << 0) 65 #define AD7879_RESET (1 << 4) 66 #define AD7879_MFS(x) ((x & 0x3) << 5) 67 #define AD7879_AVG(x) ((x & 0x3) << 7) 68 #define AD7879_SER (1 << 9) /* non-differential */ 69 #define AD7879_DFR (0 << 9) /* differential */ 70 #define AD7879_GPIOPOL (1 << 10) 71 #define AD7879_GPIODIR (1 << 11) 72 #define AD7879_GPIO_DATA (1 << 12) 73 #define AD7879_GPIO_EN (1 << 13) 74 #define AD7879_PM(x) ((x & 0x3) << 14) 75 #define AD7879_PM_SHUTDOWN (0) 76 #define AD7879_PM_DYN (1) 77 #define AD7879_PM_FULLON (2) 78 79 /* Control REG 3 */ 80 #define AD7879_TEMPMASK_BIT (1<<15) 81 #define AD7879_AUXVBATMASK_BIT (1<<14) 82 #define AD7879_INTMODE_BIT (1<<13) 83 #define AD7879_GPIOALERTMASK_BIT (1<<12) 84 #define AD7879_AUXLOW_BIT (1<<11) 85 #define AD7879_AUXHIGH_BIT (1<<10) 86 #define AD7879_TEMPLOW_BIT (1<<9) 87 #define AD7879_TEMPHIGH_BIT (1<<8) 88 #define AD7879_YPLUS_BIT (1<<7) 89 #define AD7879_XPLUS_BIT (1<<6) 90 #define AD7879_Z1_BIT (1<<5) 91 #define AD7879_Z2_BIT (1<<4) 92 #define AD7879_AUX_BIT (1<<3) 93 #define AD7879_VBAT_BIT (1<<2) 94 #define AD7879_TEMP_BIT (1<<1) 95 96 enum { 97 AD7879_SEQ_YPOS = 0, 98 AD7879_SEQ_XPOS = 1, 99 AD7879_SEQ_Z1 = 2, 100 AD7879_SEQ_Z2 = 3, 101 AD7879_NR_SENSE = 4, 102 }; 103 104 #define MAX_12BIT ((1<<12)-1) 105 #define TS_PEN_UP_TIMEOUT msecs_to_jiffies(50) 106 107 struct ad7879 { 108 struct regmap *regmap; 109 struct device *dev; 110 struct input_dev *input; 111 struct timer_list timer; 112 #ifdef CONFIG_GPIOLIB 113 struct gpio_chip gc; 114 struct mutex mutex; 115 #endif 116 unsigned int irq; 117 bool disabled; /* P: input->mutex */ 118 bool suspended; /* P: input->mutex */ 119 bool swap_xy; 120 u16 conversion_data[AD7879_NR_SENSE]; 121 char phys[32]; 122 u8 first_conversion_delay; 123 u8 acquisition_time; 124 u8 averaging; 125 u8 pen_down_acc_interval; 126 u8 median; 127 u16 x_plate_ohms; 128 u16 cmd_crtl1; 129 u16 cmd_crtl2; 130 u16 cmd_crtl3; 131 int x; 132 int y; 133 int Rt; 134 }; 135 136 static int ad7879_read(struct ad7879 *ts, u8 reg) 137 { 138 unsigned int val; 139 int error; 140 141 error = regmap_read(ts->regmap, reg, &val); 142 if (error) { 143 dev_err(ts->dev, "failed to read register %#02x: %d\n", 144 reg, error); 145 return error; 146 } 147 148 return val; 149 } 150 151 static int ad7879_write(struct ad7879 *ts, u8 reg, u16 val) 152 { 153 int error; 154 155 error = regmap_write(ts->regmap, reg, val); 156 if (error) { 157 dev_err(ts->dev, 158 "failed to write %#04x to register %#02x: %d\n", 159 val, reg, error); 160 return error; 161 } 162 163 return 0; 164 } 165 166 static int ad7879_report(struct ad7879 *ts) 167 { 168 struct input_dev *input_dev = ts->input; 169 unsigned Rt; 170 u16 x, y, z1, z2; 171 172 x = ts->conversion_data[AD7879_SEQ_XPOS] & MAX_12BIT; 173 y = ts->conversion_data[AD7879_SEQ_YPOS] & MAX_12BIT; 174 z1 = ts->conversion_data[AD7879_SEQ_Z1] & MAX_12BIT; 175 z2 = ts->conversion_data[AD7879_SEQ_Z2] & MAX_12BIT; 176 177 if (ts->swap_xy) 178 swap(x, y); 179 180 /* 181 * The samples processed here are already preprocessed by the AD7879. 182 * The preprocessing function consists of a median and an averaging 183 * filter. The combination of these two techniques provides a robust 184 * solution, discarding the spurious noise in the signal and keeping 185 * only the data of interest. The size of both filters is 186 * programmable. (dev.platform_data, see linux/platform_data/ad7879.h) 187 * Other user-programmable conversion controls include variable 188 * acquisition time, and first conversion delay. Up to 16 averages can 189 * be taken per conversion. 190 */ 191 192 if (likely(x && z1)) { 193 /* compute touch pressure resistance using equation #1 */ 194 Rt = (z2 - z1) * x * ts->x_plate_ohms; 195 Rt /= z1; 196 Rt = (Rt + 2047) >> 12; 197 198 /* 199 * Sample found inconsistent, pressure is beyond 200 * the maximum. Don't report it to user space. 201 */ 202 if (Rt > input_abs_get_max(input_dev, ABS_PRESSURE)) 203 return -EINVAL; 204 205 /* 206 * Note that we delay reporting events by one sample. 207 * This is done to avoid reporting last sample of the 208 * touch sequence, which may be incomplete if finger 209 * leaves the surface before last reading is taken. 210 */ 211 if (timer_pending(&ts->timer)) { 212 /* Touch continues */ 213 input_report_key(input_dev, BTN_TOUCH, 1); 214 input_report_abs(input_dev, ABS_X, ts->x); 215 input_report_abs(input_dev, ABS_Y, ts->y); 216 input_report_abs(input_dev, ABS_PRESSURE, ts->Rt); 217 input_sync(input_dev); 218 } 219 220 ts->x = x; 221 ts->y = y; 222 ts->Rt = Rt; 223 224 return 0; 225 } 226 227 return -EINVAL; 228 } 229 230 static void ad7879_ts_event_release(struct ad7879 *ts) 231 { 232 struct input_dev *input_dev = ts->input; 233 234 input_report_abs(input_dev, ABS_PRESSURE, 0); 235 input_report_key(input_dev, BTN_TOUCH, 0); 236 input_sync(input_dev); 237 } 238 239 static void ad7879_timer(struct timer_list *t) 240 { 241 struct ad7879 *ts = from_timer(ts, t, timer); 242 243 ad7879_ts_event_release(ts); 244 } 245 246 static irqreturn_t ad7879_irq(int irq, void *handle) 247 { 248 struct ad7879 *ts = handle; 249 250 regmap_bulk_read(ts->regmap, AD7879_REG_XPLUS, 251 ts->conversion_data, AD7879_NR_SENSE); 252 253 if (!ad7879_report(ts)) 254 mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT); 255 256 return IRQ_HANDLED; 257 } 258 259 static void __ad7879_enable(struct ad7879 *ts) 260 { 261 ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2); 262 ad7879_write(ts, AD7879_REG_CTRL3, ts->cmd_crtl3); 263 ad7879_write(ts, AD7879_REG_CTRL1, ts->cmd_crtl1); 264 265 enable_irq(ts->irq); 266 } 267 268 static void __ad7879_disable(struct ad7879 *ts) 269 { 270 u16 reg = (ts->cmd_crtl2 & ~AD7879_PM(-1)) | 271 AD7879_PM(AD7879_PM_SHUTDOWN); 272 disable_irq(ts->irq); 273 274 if (del_timer_sync(&ts->timer)) 275 ad7879_ts_event_release(ts); 276 277 ad7879_write(ts, AD7879_REG_CTRL2, reg); 278 } 279 280 281 static int ad7879_open(struct input_dev *input) 282 { 283 struct ad7879 *ts = input_get_drvdata(input); 284 285 /* protected by input->mutex */ 286 if (!ts->disabled && !ts->suspended) 287 __ad7879_enable(ts); 288 289 return 0; 290 } 291 292 static void ad7879_close(struct input_dev *input) 293 { 294 struct ad7879 *ts = input_get_drvdata(input); 295 296 /* protected by input->mutex */ 297 if (!ts->disabled && !ts->suspended) 298 __ad7879_disable(ts); 299 } 300 301 static int __maybe_unused ad7879_suspend(struct device *dev) 302 { 303 struct ad7879 *ts = dev_get_drvdata(dev); 304 305 mutex_lock(&ts->input->mutex); 306 307 if (!ts->suspended && !ts->disabled && ts->input->users) 308 __ad7879_disable(ts); 309 310 ts->suspended = true; 311 312 mutex_unlock(&ts->input->mutex); 313 314 return 0; 315 } 316 317 static int __maybe_unused ad7879_resume(struct device *dev) 318 { 319 struct ad7879 *ts = dev_get_drvdata(dev); 320 321 mutex_lock(&ts->input->mutex); 322 323 if (ts->suspended && !ts->disabled && ts->input->users) 324 __ad7879_enable(ts); 325 326 ts->suspended = false; 327 328 mutex_unlock(&ts->input->mutex); 329 330 return 0; 331 } 332 333 SIMPLE_DEV_PM_OPS(ad7879_pm_ops, ad7879_suspend, ad7879_resume); 334 EXPORT_SYMBOL(ad7879_pm_ops); 335 336 static void ad7879_toggle(struct ad7879 *ts, bool disable) 337 { 338 mutex_lock(&ts->input->mutex); 339 340 if (!ts->suspended && ts->input->users != 0) { 341 342 if (disable) { 343 if (ts->disabled) 344 __ad7879_enable(ts); 345 } else { 346 if (!ts->disabled) 347 __ad7879_disable(ts); 348 } 349 } 350 351 ts->disabled = disable; 352 353 mutex_unlock(&ts->input->mutex); 354 } 355 356 static ssize_t ad7879_disable_show(struct device *dev, 357 struct device_attribute *attr, char *buf) 358 { 359 struct ad7879 *ts = dev_get_drvdata(dev); 360 361 return sprintf(buf, "%u\n", ts->disabled); 362 } 363 364 static ssize_t ad7879_disable_store(struct device *dev, 365 struct device_attribute *attr, 366 const char *buf, size_t count) 367 { 368 struct ad7879 *ts = dev_get_drvdata(dev); 369 unsigned int val; 370 int error; 371 372 error = kstrtouint(buf, 10, &val); 373 if (error) 374 return error; 375 376 ad7879_toggle(ts, val); 377 378 return count; 379 } 380 381 static DEVICE_ATTR(disable, 0664, ad7879_disable_show, ad7879_disable_store); 382 383 static struct attribute *ad7879_attributes[] = { 384 &dev_attr_disable.attr, 385 NULL 386 }; 387 388 static const struct attribute_group ad7879_attr_group = { 389 .attrs = ad7879_attributes, 390 }; 391 392 #ifdef CONFIG_GPIOLIB 393 static int ad7879_gpio_direction_input(struct gpio_chip *chip, 394 unsigned gpio) 395 { 396 struct ad7879 *ts = gpiochip_get_data(chip); 397 int err; 398 399 mutex_lock(&ts->mutex); 400 ts->cmd_crtl2 |= AD7879_GPIO_EN | AD7879_GPIODIR | AD7879_GPIOPOL; 401 err = ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2); 402 mutex_unlock(&ts->mutex); 403 404 return err; 405 } 406 407 static int ad7879_gpio_direction_output(struct gpio_chip *chip, 408 unsigned gpio, int level) 409 { 410 struct ad7879 *ts = gpiochip_get_data(chip); 411 int err; 412 413 mutex_lock(&ts->mutex); 414 ts->cmd_crtl2 &= ~AD7879_GPIODIR; 415 ts->cmd_crtl2 |= AD7879_GPIO_EN | AD7879_GPIOPOL; 416 if (level) 417 ts->cmd_crtl2 |= AD7879_GPIO_DATA; 418 else 419 ts->cmd_crtl2 &= ~AD7879_GPIO_DATA; 420 421 err = ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2); 422 mutex_unlock(&ts->mutex); 423 424 return err; 425 } 426 427 static int ad7879_gpio_get_value(struct gpio_chip *chip, unsigned gpio) 428 { 429 struct ad7879 *ts = gpiochip_get_data(chip); 430 u16 val; 431 432 mutex_lock(&ts->mutex); 433 val = ad7879_read(ts, AD7879_REG_CTRL2); 434 mutex_unlock(&ts->mutex); 435 436 return !!(val & AD7879_GPIO_DATA); 437 } 438 439 static void ad7879_gpio_set_value(struct gpio_chip *chip, 440 unsigned gpio, int value) 441 { 442 struct ad7879 *ts = gpiochip_get_data(chip); 443 444 mutex_lock(&ts->mutex); 445 if (value) 446 ts->cmd_crtl2 |= AD7879_GPIO_DATA; 447 else 448 ts->cmd_crtl2 &= ~AD7879_GPIO_DATA; 449 450 ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2); 451 mutex_unlock(&ts->mutex); 452 } 453 454 static int ad7879_gpio_add(struct ad7879 *ts) 455 { 456 int ret = 0; 457 458 mutex_init(&ts->mutex); 459 460 /* Do not create a chip unless flagged for it */ 461 if (!device_property_read_bool(ts->dev, "gpio-controller")) 462 return 0; 463 464 ts->gc.direction_input = ad7879_gpio_direction_input; 465 ts->gc.direction_output = ad7879_gpio_direction_output; 466 ts->gc.get = ad7879_gpio_get_value; 467 ts->gc.set = ad7879_gpio_set_value; 468 ts->gc.can_sleep = 1; 469 ts->gc.base = -1; 470 ts->gc.ngpio = 1; 471 ts->gc.label = "AD7879-GPIO"; 472 ts->gc.owner = THIS_MODULE; 473 ts->gc.parent = ts->dev; 474 475 ret = devm_gpiochip_add_data(ts->dev, &ts->gc, ts); 476 if (ret) 477 dev_err(ts->dev, "failed to register gpio %d\n", 478 ts->gc.base); 479 480 return ret; 481 } 482 #else 483 static int ad7879_gpio_add(struct ad7879 *ts) 484 { 485 return 0; 486 } 487 #endif 488 489 static int ad7879_parse_dt(struct device *dev, struct ad7879 *ts) 490 { 491 int err; 492 u32 tmp; 493 494 err = device_property_read_u32(dev, "adi,resistance-plate-x", &tmp); 495 if (err) { 496 dev_err(dev, "failed to get resistance-plate-x property\n"); 497 return err; 498 } 499 ts->x_plate_ohms = (u16)tmp; 500 501 device_property_read_u8(dev, "adi,first-conversion-delay", 502 &ts->first_conversion_delay); 503 device_property_read_u8(dev, "adi,acquisition-time", 504 &ts->acquisition_time); 505 device_property_read_u8(dev, "adi,median-filter-size", &ts->median); 506 device_property_read_u8(dev, "adi,averaging", &ts->averaging); 507 device_property_read_u8(dev, "adi,conversion-interval", 508 &ts->pen_down_acc_interval); 509 510 ts->swap_xy = device_property_read_bool(dev, "touchscreen-swapped-x-y"); 511 512 return 0; 513 } 514 515 int ad7879_probe(struct device *dev, struct regmap *regmap, 516 int irq, u16 bustype, u8 devid) 517 { 518 struct ad7879 *ts; 519 struct input_dev *input_dev; 520 int err; 521 u16 revid; 522 523 if (irq <= 0) { 524 dev_err(dev, "No IRQ specified\n"); 525 return -EINVAL; 526 } 527 528 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL); 529 if (!ts) 530 return -ENOMEM; 531 532 err = ad7879_parse_dt(dev, ts); 533 if (err) 534 return err; 535 536 input_dev = devm_input_allocate_device(dev); 537 if (!input_dev) { 538 dev_err(dev, "Failed to allocate input device\n"); 539 return -ENOMEM; 540 } 541 542 ts->dev = dev; 543 ts->input = input_dev; 544 ts->irq = irq; 545 ts->regmap = regmap; 546 547 timer_setup(&ts->timer, ad7879_timer, 0); 548 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev)); 549 550 input_dev->name = "AD7879 Touchscreen"; 551 input_dev->phys = ts->phys; 552 input_dev->dev.parent = dev; 553 input_dev->id.bustype = bustype; 554 555 input_dev->open = ad7879_open; 556 input_dev->close = ad7879_close; 557 558 input_set_drvdata(input_dev, ts); 559 560 input_set_capability(input_dev, EV_KEY, BTN_TOUCH); 561 562 input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0); 563 input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0); 564 input_set_capability(input_dev, EV_ABS, ABS_PRESSURE); 565 touchscreen_parse_properties(input_dev, false, NULL); 566 if (!input_abs_get_max(input_dev, ABS_PRESSURE)) { 567 dev_err(dev, "Touchscreen pressure is not specified\n"); 568 return -EINVAL; 569 } 570 571 err = ad7879_write(ts, AD7879_REG_CTRL2, AD7879_RESET); 572 if (err < 0) { 573 dev_err(dev, "Failed to write %s\n", input_dev->name); 574 return err; 575 } 576 577 revid = ad7879_read(ts, AD7879_REG_REVID); 578 input_dev->id.product = (revid & 0xff); 579 input_dev->id.version = revid >> 8; 580 if (input_dev->id.product != devid) { 581 dev_err(dev, "Failed to probe %s (%x vs %x)\n", 582 input_dev->name, devid, revid); 583 return -ENODEV; 584 } 585 586 ts->cmd_crtl3 = AD7879_YPLUS_BIT | 587 AD7879_XPLUS_BIT | 588 AD7879_Z2_BIT | 589 AD7879_Z1_BIT | 590 AD7879_TEMPMASK_BIT | 591 AD7879_AUXVBATMASK_BIT | 592 AD7879_GPIOALERTMASK_BIT; 593 594 ts->cmd_crtl2 = AD7879_PM(AD7879_PM_DYN) | AD7879_DFR | 595 AD7879_AVG(ts->averaging) | 596 AD7879_MFS(ts->median) | 597 AD7879_FCD(ts->first_conversion_delay); 598 599 ts->cmd_crtl1 = AD7879_MODE_INT | AD7879_MODE_SEQ1 | 600 AD7879_ACQ(ts->acquisition_time) | 601 AD7879_TMR(ts->pen_down_acc_interval); 602 603 err = devm_request_threaded_irq(dev, ts->irq, NULL, ad7879_irq, 604 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 605 dev_name(dev), ts); 606 if (err) { 607 dev_err(dev, "Failed to request IRQ: %d\n", err); 608 return err; 609 } 610 611 __ad7879_disable(ts); 612 613 err = devm_device_add_group(dev, &ad7879_attr_group); 614 if (err) 615 return err; 616 617 err = ad7879_gpio_add(ts); 618 if (err) 619 return err; 620 621 err = input_register_device(input_dev); 622 if (err) 623 return err; 624 625 dev_set_drvdata(dev, ts); 626 627 return 0; 628 } 629 EXPORT_SYMBOL(ad7879_probe); 630 631 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); 632 MODULE_DESCRIPTION("AD7879(-1) touchscreen Driver"); 633 MODULE_LICENSE("GPL"); 634