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 int error; 250 251 error = regmap_bulk_read(ts->regmap, AD7879_REG_XPLUS, 252 ts->conversion_data, AD7879_NR_SENSE); 253 if (error) 254 dev_err_ratelimited(ts->dev, "failed to read %#02x: %d\n", 255 AD7879_REG_XPLUS, error); 256 else if (!ad7879_report(ts)) 257 mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT); 258 259 return IRQ_HANDLED; 260 } 261 262 static void __ad7879_enable(struct ad7879 *ts) 263 { 264 ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2); 265 ad7879_write(ts, AD7879_REG_CTRL3, ts->cmd_crtl3); 266 ad7879_write(ts, AD7879_REG_CTRL1, ts->cmd_crtl1); 267 268 enable_irq(ts->irq); 269 } 270 271 static void __ad7879_disable(struct ad7879 *ts) 272 { 273 u16 reg = (ts->cmd_crtl2 & ~AD7879_PM(-1)) | 274 AD7879_PM(AD7879_PM_SHUTDOWN); 275 disable_irq(ts->irq); 276 277 if (del_timer_sync(&ts->timer)) 278 ad7879_ts_event_release(ts); 279 280 ad7879_write(ts, AD7879_REG_CTRL2, reg); 281 } 282 283 284 static int ad7879_open(struct input_dev *input) 285 { 286 struct ad7879 *ts = input_get_drvdata(input); 287 288 /* protected by input->mutex */ 289 if (!ts->disabled && !ts->suspended) 290 __ad7879_enable(ts); 291 292 return 0; 293 } 294 295 static void ad7879_close(struct input_dev *input) 296 { 297 struct ad7879 *ts = input_get_drvdata(input); 298 299 /* protected by input->mutex */ 300 if (!ts->disabled && !ts->suspended) 301 __ad7879_disable(ts); 302 } 303 304 static int __maybe_unused ad7879_suspend(struct device *dev) 305 { 306 struct ad7879 *ts = dev_get_drvdata(dev); 307 308 mutex_lock(&ts->input->mutex); 309 310 if (!ts->suspended && !ts->disabled && ts->input->users) 311 __ad7879_disable(ts); 312 313 ts->suspended = true; 314 315 mutex_unlock(&ts->input->mutex); 316 317 return 0; 318 } 319 320 static int __maybe_unused ad7879_resume(struct device *dev) 321 { 322 struct ad7879 *ts = dev_get_drvdata(dev); 323 324 mutex_lock(&ts->input->mutex); 325 326 if (ts->suspended && !ts->disabled && ts->input->users) 327 __ad7879_enable(ts); 328 329 ts->suspended = false; 330 331 mutex_unlock(&ts->input->mutex); 332 333 return 0; 334 } 335 336 SIMPLE_DEV_PM_OPS(ad7879_pm_ops, ad7879_suspend, ad7879_resume); 337 EXPORT_SYMBOL(ad7879_pm_ops); 338 339 static void ad7879_toggle(struct ad7879 *ts, bool disable) 340 { 341 mutex_lock(&ts->input->mutex); 342 343 if (!ts->suspended && ts->input->users != 0) { 344 345 if (disable) { 346 if (ts->disabled) 347 __ad7879_enable(ts); 348 } else { 349 if (!ts->disabled) 350 __ad7879_disable(ts); 351 } 352 } 353 354 ts->disabled = disable; 355 356 mutex_unlock(&ts->input->mutex); 357 } 358 359 static ssize_t ad7879_disable_show(struct device *dev, 360 struct device_attribute *attr, char *buf) 361 { 362 struct ad7879 *ts = dev_get_drvdata(dev); 363 364 return sprintf(buf, "%u\n", ts->disabled); 365 } 366 367 static ssize_t ad7879_disable_store(struct device *dev, 368 struct device_attribute *attr, 369 const char *buf, size_t count) 370 { 371 struct ad7879 *ts = dev_get_drvdata(dev); 372 unsigned int val; 373 int error; 374 375 error = kstrtouint(buf, 10, &val); 376 if (error) 377 return error; 378 379 ad7879_toggle(ts, val); 380 381 return count; 382 } 383 384 static DEVICE_ATTR(disable, 0664, ad7879_disable_show, ad7879_disable_store); 385 386 static struct attribute *ad7879_attributes[] = { 387 &dev_attr_disable.attr, 388 NULL 389 }; 390 391 static const struct attribute_group ad7879_attr_group = { 392 .attrs = ad7879_attributes, 393 }; 394 395 #ifdef CONFIG_GPIOLIB 396 static int ad7879_gpio_direction_input(struct gpio_chip *chip, 397 unsigned gpio) 398 { 399 struct ad7879 *ts = gpiochip_get_data(chip); 400 int err; 401 402 mutex_lock(&ts->mutex); 403 ts->cmd_crtl2 |= AD7879_GPIO_EN | AD7879_GPIODIR | AD7879_GPIOPOL; 404 err = ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2); 405 mutex_unlock(&ts->mutex); 406 407 return err; 408 } 409 410 static int ad7879_gpio_direction_output(struct gpio_chip *chip, 411 unsigned gpio, int level) 412 { 413 struct ad7879 *ts = gpiochip_get_data(chip); 414 int err; 415 416 mutex_lock(&ts->mutex); 417 ts->cmd_crtl2 &= ~AD7879_GPIODIR; 418 ts->cmd_crtl2 |= AD7879_GPIO_EN | AD7879_GPIOPOL; 419 if (level) 420 ts->cmd_crtl2 |= AD7879_GPIO_DATA; 421 else 422 ts->cmd_crtl2 &= ~AD7879_GPIO_DATA; 423 424 err = ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2); 425 mutex_unlock(&ts->mutex); 426 427 return err; 428 } 429 430 static int ad7879_gpio_get_value(struct gpio_chip *chip, unsigned gpio) 431 { 432 struct ad7879 *ts = gpiochip_get_data(chip); 433 u16 val; 434 435 mutex_lock(&ts->mutex); 436 val = ad7879_read(ts, AD7879_REG_CTRL2); 437 mutex_unlock(&ts->mutex); 438 439 return !!(val & AD7879_GPIO_DATA); 440 } 441 442 static void ad7879_gpio_set_value(struct gpio_chip *chip, 443 unsigned gpio, int value) 444 { 445 struct ad7879 *ts = gpiochip_get_data(chip); 446 447 mutex_lock(&ts->mutex); 448 if (value) 449 ts->cmd_crtl2 |= AD7879_GPIO_DATA; 450 else 451 ts->cmd_crtl2 &= ~AD7879_GPIO_DATA; 452 453 ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2); 454 mutex_unlock(&ts->mutex); 455 } 456 457 static int ad7879_gpio_add(struct ad7879 *ts) 458 { 459 int ret = 0; 460 461 mutex_init(&ts->mutex); 462 463 /* Do not create a chip unless flagged for it */ 464 if (!device_property_read_bool(ts->dev, "gpio-controller")) 465 return 0; 466 467 ts->gc.direction_input = ad7879_gpio_direction_input; 468 ts->gc.direction_output = ad7879_gpio_direction_output; 469 ts->gc.get = ad7879_gpio_get_value; 470 ts->gc.set = ad7879_gpio_set_value; 471 ts->gc.can_sleep = 1; 472 ts->gc.base = -1; 473 ts->gc.ngpio = 1; 474 ts->gc.label = "AD7879-GPIO"; 475 ts->gc.owner = THIS_MODULE; 476 ts->gc.parent = ts->dev; 477 478 ret = devm_gpiochip_add_data(ts->dev, &ts->gc, ts); 479 if (ret) 480 dev_err(ts->dev, "failed to register gpio %d\n", 481 ts->gc.base); 482 483 return ret; 484 } 485 #else 486 static int ad7879_gpio_add(struct ad7879 *ts) 487 { 488 return 0; 489 } 490 #endif 491 492 static int ad7879_parse_dt(struct device *dev, struct ad7879 *ts) 493 { 494 int err; 495 u32 tmp; 496 497 err = device_property_read_u32(dev, "adi,resistance-plate-x", &tmp); 498 if (err) { 499 dev_err(dev, "failed to get resistance-plate-x property\n"); 500 return err; 501 } 502 ts->x_plate_ohms = (u16)tmp; 503 504 device_property_read_u8(dev, "adi,first-conversion-delay", 505 &ts->first_conversion_delay); 506 device_property_read_u8(dev, "adi,acquisition-time", 507 &ts->acquisition_time); 508 device_property_read_u8(dev, "adi,median-filter-size", &ts->median); 509 device_property_read_u8(dev, "adi,averaging", &ts->averaging); 510 device_property_read_u8(dev, "adi,conversion-interval", 511 &ts->pen_down_acc_interval); 512 513 ts->swap_xy = device_property_read_bool(dev, "touchscreen-swapped-x-y"); 514 515 return 0; 516 } 517 518 int ad7879_probe(struct device *dev, struct regmap *regmap, 519 int irq, u16 bustype, u8 devid) 520 { 521 struct ad7879 *ts; 522 struct input_dev *input_dev; 523 int err; 524 u16 revid; 525 526 if (irq <= 0) { 527 dev_err(dev, "No IRQ specified\n"); 528 return -EINVAL; 529 } 530 531 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL); 532 if (!ts) 533 return -ENOMEM; 534 535 err = ad7879_parse_dt(dev, ts); 536 if (err) 537 return err; 538 539 input_dev = devm_input_allocate_device(dev); 540 if (!input_dev) { 541 dev_err(dev, "Failed to allocate input device\n"); 542 return -ENOMEM; 543 } 544 545 ts->dev = dev; 546 ts->input = input_dev; 547 ts->irq = irq; 548 ts->regmap = regmap; 549 550 timer_setup(&ts->timer, ad7879_timer, 0); 551 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev)); 552 553 input_dev->name = "AD7879 Touchscreen"; 554 input_dev->phys = ts->phys; 555 input_dev->dev.parent = dev; 556 input_dev->id.bustype = bustype; 557 558 input_dev->open = ad7879_open; 559 input_dev->close = ad7879_close; 560 561 input_set_drvdata(input_dev, ts); 562 563 input_set_capability(input_dev, EV_KEY, BTN_TOUCH); 564 565 input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0); 566 input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0); 567 input_set_capability(input_dev, EV_ABS, ABS_PRESSURE); 568 touchscreen_parse_properties(input_dev, false, NULL); 569 if (!input_abs_get_max(input_dev, ABS_PRESSURE)) { 570 dev_err(dev, "Touchscreen pressure is not specified\n"); 571 return -EINVAL; 572 } 573 574 err = ad7879_write(ts, AD7879_REG_CTRL2, AD7879_RESET); 575 if (err < 0) { 576 dev_err(dev, "Failed to write %s\n", input_dev->name); 577 return err; 578 } 579 580 revid = ad7879_read(ts, AD7879_REG_REVID); 581 input_dev->id.product = (revid & 0xff); 582 input_dev->id.version = revid >> 8; 583 if (input_dev->id.product != devid) { 584 dev_err(dev, "Failed to probe %s (%x vs %x)\n", 585 input_dev->name, devid, revid); 586 return -ENODEV; 587 } 588 589 ts->cmd_crtl3 = AD7879_YPLUS_BIT | 590 AD7879_XPLUS_BIT | 591 AD7879_Z2_BIT | 592 AD7879_Z1_BIT | 593 AD7879_TEMPMASK_BIT | 594 AD7879_AUXVBATMASK_BIT | 595 AD7879_GPIOALERTMASK_BIT; 596 597 ts->cmd_crtl2 = AD7879_PM(AD7879_PM_DYN) | AD7879_DFR | 598 AD7879_AVG(ts->averaging) | 599 AD7879_MFS(ts->median) | 600 AD7879_FCD(ts->first_conversion_delay); 601 602 ts->cmd_crtl1 = AD7879_MODE_INT | AD7879_MODE_SEQ1 | 603 AD7879_ACQ(ts->acquisition_time) | 604 AD7879_TMR(ts->pen_down_acc_interval); 605 606 err = devm_request_threaded_irq(dev, ts->irq, NULL, ad7879_irq, 607 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 608 dev_name(dev), ts); 609 if (err) { 610 dev_err(dev, "Failed to request IRQ: %d\n", err); 611 return err; 612 } 613 614 __ad7879_disable(ts); 615 616 err = devm_device_add_group(dev, &ad7879_attr_group); 617 if (err) 618 return err; 619 620 err = ad7879_gpio_add(ts); 621 if (err) 622 return err; 623 624 err = input_register_device(input_dev); 625 if (err) 626 return err; 627 628 dev_set_drvdata(dev, ts); 629 630 return 0; 631 } 632 EXPORT_SYMBOL(ad7879_probe); 633 634 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); 635 MODULE_DESCRIPTION("AD7879(-1) touchscreen Driver"); 636 MODULE_LICENSE("GPL"); 637