| 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/types.h> |
| 21 | #include <linux/hwmon.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/err.h> |
| 24 | #include <linux/sched.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/input.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/pm.h> |
| 30 | #include <linux/of.h> |
| 31 | #include <linux/of_gpio.h> |
| 32 | #include <linux/of_device.h> |
| 33 | #include <linux/gpio.h> |
| 34 | #include <linux/spi/spi.h> |
| 35 | #include <linux/spi/ads7846.h> |
| 36 | #include <linux/regulator/consumer.h> |
| 37 | #include <linux/module.h> |
| 38 | #include <asm/irq.h> |
| 39 | |
| 40 | /* |
| 41 | * This code has been heavily tested on a Nokia 770, and lightly |
| 42 | * tested on other ads7846 devices (OSK/Mistral, Lubbock, Spitz). |
| 43 | * TSC2046 is just newer ads7846 silicon. |
| 44 | * Support for ads7843 tested on Atmel at91sam926x-EK. |
| 45 | * Support for ads7845 has only been stubbed in. |
| 46 | * Support for Analog Devices AD7873 and AD7843 tested. |
| 47 | * |
| 48 | * IRQ handling needs a workaround because of a shortcoming in handling |
| 49 | * edge triggered IRQs on some platforms like the OMAP1/2. These |
| 50 | * platforms don't handle the ARM lazy IRQ disabling properly, thus we |
| 51 | * have to maintain our own SW IRQ disabled status. This should be |
| 52 | * removed as soon as the affected platform's IRQ handling is fixed. |
| 53 | * |
| 54 | * App note sbaa036 talks in more detail about accurate sampling... |
| 55 | * that ought to help in situations like LCDs inducing noise (which |
| 56 | * can also be helped by using synch signals) and more generally. |
| 57 | * This driver tries to utilize the measures described in the app |
| 58 | * note. The strength of filtering can be set in the board-* specific |
| 59 | * files. |
| 60 | */ |
| 61 | |
| 62 | #define TS_POLL_DELAY 1 /* ms delay before the first sample */ |
| 63 | #define TS_POLL_PERIOD 5 /* ms delay between samples */ |
| 64 | |
| 65 | /* this driver doesn't aim at the peak continuous sample rate */ |
| 66 | #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */) |
| 67 | |
| 68 | struct ts_event { |
| 69 | /* |
| 70 | * For portability, we can't read 12 bit values using SPI (which |
| 71 | * would make the controller deliver them as native byte order u16 |
| 72 | * with msbs zeroed). Instead, we read them as two 8-bit values, |
| 73 | * *** WHICH NEED BYTESWAPPING *** and range adjustment. |
| 74 | */ |
| 75 | u16 x; |
| 76 | u16 y; |
| 77 | u16 z1, z2; |
| 78 | bool ignore; |
| 79 | u8 x_buf[3]; |
| 80 | u8 y_buf[3]; |
| 81 | }; |
| 82 | |
| 83 | /* |
| 84 | * We allocate this separately to avoid cache line sharing issues when |
| 85 | * driver is used with DMA-based SPI controllers (like atmel_spi) on |
| 86 | * systems where main memory is not DMA-coherent (most non-x86 boards). |
| 87 | */ |
| 88 | struct ads7846_packet { |
| 89 | u8 read_x, read_y, read_z1, read_z2, pwrdown; |
| 90 | u16 dummy; /* for the pwrdown read */ |
| 91 | struct ts_event tc; |
| 92 | /* for ads7845 with mpc5121 psc spi we use 3-byte buffers */ |
| 93 | u8 read_x_cmd[3], read_y_cmd[3], pwrdown_cmd[3]; |
| 94 | }; |
| 95 | |
| 96 | struct ads7846 { |
| 97 | struct input_dev *input; |
| 98 | char phys[32]; |
| 99 | char name[32]; |
| 100 | |
| 101 | struct spi_device *spi; |
| 102 | struct regulator *reg; |
| 103 | |
| 104 | #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE) |
| 105 | struct attribute_group *attr_group; |
| 106 | struct device *hwmon; |
| 107 | #endif |
| 108 | |
| 109 | u16 model; |
| 110 | u16 vref_mv; |
| 111 | u16 vref_delay_usecs; |
| 112 | u16 x_plate_ohms; |
| 113 | u16 pressure_max; |
| 114 | |
| 115 | bool swap_xy; |
| 116 | bool use_internal; |
| 117 | |
| 118 | struct ads7846_packet *packet; |
| 119 | |
| 120 | struct spi_transfer xfer[18]; |
| 121 | struct spi_message msg[5]; |
| 122 | int msg_count; |
| 123 | wait_queue_head_t wait; |
| 124 | |
| 125 | bool pendown; |
| 126 | |
| 127 | int read_cnt; |
| 128 | int read_rep; |
| 129 | int last_read; |
| 130 | |
| 131 | u16 debounce_max; |
| 132 | u16 debounce_tol; |
| 133 | u16 debounce_rep; |
| 134 | |
| 135 | u16 penirq_recheck_delay_usecs; |
| 136 | |
| 137 | struct mutex lock; |
| 138 | bool stopped; /* P: lock */ |
| 139 | bool disabled; /* P: lock */ |
| 140 | bool suspended; /* P: lock */ |
| 141 | |
| 142 | int (*filter)(void *data, int data_idx, int *val); |
| 143 | void *filter_data; |
| 144 | void (*filter_cleanup)(void *data); |
| 145 | int (*get_pendown_state)(void); |
| 146 | int gpio_pendown; |
| 147 | |
| 148 | void (*wait_for_sync)(void); |
| 149 | }; |
| 150 | |
| 151 | /* leave chip selected when we're done, for quicker re-select? */ |
| 152 | #if 0 |
| 153 | #define CS_CHANGE(xfer) ((xfer).cs_change = 1) |
| 154 | #else |
| 155 | #define CS_CHANGE(xfer) ((xfer).cs_change = 0) |
| 156 | #endif |
| 157 | |
| 158 | /*--------------------------------------------------------------------------*/ |
| 159 | |
| 160 | /* The ADS7846 has touchscreen and other sensors. |
| 161 | * Earlier ads784x chips are somewhat compatible. |
| 162 | */ |
| 163 | #define ADS_START (1 << 7) |
| 164 | #define ADS_A2A1A0_d_y (1 << 4) /* differential */ |
| 165 | #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */ |
| 166 | #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */ |
| 167 | #define ADS_A2A1A0_d_x (5 << 4) /* differential */ |
| 168 | #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */ |
| 169 | #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */ |
| 170 | #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */ |
| 171 | #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */ |
| 172 | #define ADS_8_BIT (1 << 3) |
| 173 | #define ADS_12_BIT (0 << 3) |
| 174 | #define ADS_SER (1 << 2) /* non-differential */ |
| 175 | #define ADS_DFR (0 << 2) /* differential */ |
| 176 | #define ADS_PD10_PDOWN (0 << 0) /* low power mode + penirq */ |
| 177 | #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */ |
| 178 | #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */ |
| 179 | #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */ |
| 180 | |
| 181 | #define MAX_12BIT ((1<<12)-1) |
| 182 | |
| 183 | /* leave ADC powered up (disables penirq) between differential samples */ |
| 184 | #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \ |
| 185 | | ADS_12_BIT | ADS_DFR | \ |
| 186 | (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0)) |
| 187 | |
| 188 | #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref)) |
| 189 | #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref)) |
| 190 | #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref)) |
| 191 | |
| 192 | #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref)) |
| 193 | #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */ |
| 194 | |
| 195 | /* single-ended samples need to first power up reference voltage; |
| 196 | * we leave both ADC and VREF powered |
| 197 | */ |
| 198 | #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \ |
| 199 | | ADS_12_BIT | ADS_SER) |
| 200 | |
| 201 | #define REF_ON (READ_12BIT_DFR(x, 1, 1)) |
| 202 | #define REF_OFF (READ_12BIT_DFR(y, 0, 0)) |
| 203 | |
| 204 | /* Must be called with ts->lock held */ |
| 205 | static void ads7846_stop(struct ads7846 *ts) |
| 206 | { |
| 207 | if (!ts->disabled && !ts->suspended) { |
| 208 | /* Signal IRQ thread to stop polling and disable the handler. */ |
| 209 | ts->stopped = true; |
| 210 | mb(); |
| 211 | wake_up(&ts->wait); |
| 212 | disable_irq(ts->spi->irq); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /* Must be called with ts->lock held */ |
| 217 | static void ads7846_restart(struct ads7846 *ts) |
| 218 | { |
| 219 | if (!ts->disabled && !ts->suspended) { |
| 220 | /* Tell IRQ thread that it may poll the device. */ |
| 221 | ts->stopped = false; |
| 222 | mb(); |
| 223 | enable_irq(ts->spi->irq); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /* Must be called with ts->lock held */ |
| 228 | static void __ads7846_disable(struct ads7846 *ts) |
| 229 | { |
| 230 | ads7846_stop(ts); |
| 231 | regulator_disable(ts->reg); |
| 232 | |
| 233 | /* |
| 234 | * We know the chip's in low power mode since we always |
| 235 | * leave it that way after every request |
| 236 | */ |
| 237 | } |
| 238 | |
| 239 | /* Must be called with ts->lock held */ |
| 240 | static void __ads7846_enable(struct ads7846 *ts) |
| 241 | { |
| 242 | int error; |
| 243 | |
| 244 | error = regulator_enable(ts->reg); |
| 245 | if (error != 0) |
| 246 | dev_err(&ts->spi->dev, "Failed to enable supply: %d\n", error); |
| 247 | |
| 248 | ads7846_restart(ts); |
| 249 | } |
| 250 | |
| 251 | static void ads7846_disable(struct ads7846 *ts) |
| 252 | { |
| 253 | mutex_lock(&ts->lock); |
| 254 | |
| 255 | if (!ts->disabled) { |
| 256 | |
| 257 | if (!ts->suspended) |
| 258 | __ads7846_disable(ts); |
| 259 | |
| 260 | ts->disabled = true; |
| 261 | } |
| 262 | |
| 263 | mutex_unlock(&ts->lock); |
| 264 | } |
| 265 | |
| 266 | static void ads7846_enable(struct ads7846 *ts) |
| 267 | { |
| 268 | mutex_lock(&ts->lock); |
| 269 | |
| 270 | if (ts->disabled) { |
| 271 | |
| 272 | ts->disabled = false; |
| 273 | |
| 274 | if (!ts->suspended) |
| 275 | __ads7846_enable(ts); |
| 276 | } |
| 277 | |
| 278 | mutex_unlock(&ts->lock); |
| 279 | } |
| 280 | |
| 281 | /*--------------------------------------------------------------------------*/ |
| 282 | |
| 283 | /* |
| 284 | * Non-touchscreen sensors only use single-ended conversions. |
| 285 | * The range is GND..vREF. The ads7843 and ads7835 must use external vREF; |
| 286 | * ads7846 lets that pin be unconnected, to use internal vREF. |
| 287 | */ |
| 288 | |
| 289 | struct ser_req { |
| 290 | u8 ref_on; |
| 291 | u8 command; |
| 292 | u8 ref_off; |
| 293 | u16 scratch; |
| 294 | struct spi_message msg; |
| 295 | struct spi_transfer xfer[6]; |
| 296 | /* |
| 297 | * DMA (thus cache coherency maintenance) requires the |
| 298 | * transfer buffers to live in their own cache lines. |
| 299 | */ |
| 300 | __be16 sample ____cacheline_aligned; |
| 301 | }; |
| 302 | |
| 303 | struct ads7845_ser_req { |
| 304 | u8 command[3]; |
| 305 | struct spi_message msg; |
| 306 | struct spi_transfer xfer[2]; |
| 307 | /* |
| 308 | * DMA (thus cache coherency maintenance) requires the |
| 309 | * transfer buffers to live in their own cache lines. |
| 310 | */ |
| 311 | u8 sample[3] ____cacheline_aligned; |
| 312 | }; |
| 313 | |
| 314 | static int ads7846_read12_ser(struct device *dev, unsigned command) |
| 315 | { |
| 316 | struct spi_device *spi = to_spi_device(dev); |
| 317 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 318 | struct ser_req *req; |
| 319 | int status; |
| 320 | |
| 321 | req = kzalloc(sizeof *req, GFP_KERNEL); |
| 322 | if (!req) |
| 323 | return -ENOMEM; |
| 324 | |
| 325 | spi_message_init(&req->msg); |
| 326 | |
| 327 | /* maybe turn on internal vREF, and let it settle */ |
| 328 | if (ts->use_internal) { |
| 329 | req->ref_on = REF_ON; |
| 330 | req->xfer[0].tx_buf = &req->ref_on; |
| 331 | req->xfer[0].len = 1; |
| 332 | spi_message_add_tail(&req->xfer[0], &req->msg); |
| 333 | |
| 334 | req->xfer[1].rx_buf = &req->scratch; |
| 335 | req->xfer[1].len = 2; |
| 336 | |
| 337 | /* for 1uF, settle for 800 usec; no cap, 100 usec. */ |
| 338 | req->xfer[1].delay_usecs = ts->vref_delay_usecs; |
| 339 | spi_message_add_tail(&req->xfer[1], &req->msg); |
| 340 | |
| 341 | /* Enable reference voltage */ |
| 342 | command |= ADS_PD10_REF_ON; |
| 343 | } |
| 344 | |
| 345 | /* Enable ADC in every case */ |
| 346 | command |= ADS_PD10_ADC_ON; |
| 347 | |
| 348 | /* take sample */ |
| 349 | req->command = (u8) command; |
| 350 | req->xfer[2].tx_buf = &req->command; |
| 351 | req->xfer[2].len = 1; |
| 352 | spi_message_add_tail(&req->xfer[2], &req->msg); |
| 353 | |
| 354 | req->xfer[3].rx_buf = &req->sample; |
| 355 | req->xfer[3].len = 2; |
| 356 | spi_message_add_tail(&req->xfer[3], &req->msg); |
| 357 | |
| 358 | /* REVISIT: take a few more samples, and compare ... */ |
| 359 | |
| 360 | /* converter in low power mode & enable PENIRQ */ |
| 361 | req->ref_off = PWRDOWN; |
| 362 | req->xfer[4].tx_buf = &req->ref_off; |
| 363 | req->xfer[4].len = 1; |
| 364 | spi_message_add_tail(&req->xfer[4], &req->msg); |
| 365 | |
| 366 | req->xfer[5].rx_buf = &req->scratch; |
| 367 | req->xfer[5].len = 2; |
| 368 | CS_CHANGE(req->xfer[5]); |
| 369 | spi_message_add_tail(&req->xfer[5], &req->msg); |
| 370 | |
| 371 | mutex_lock(&ts->lock); |
| 372 | ads7846_stop(ts); |
| 373 | status = spi_sync(spi, &req->msg); |
| 374 | ads7846_restart(ts); |
| 375 | mutex_unlock(&ts->lock); |
| 376 | |
| 377 | if (status == 0) { |
| 378 | /* on-wire is a must-ignore bit, a BE12 value, then padding */ |
| 379 | status = be16_to_cpu(req->sample); |
| 380 | status = status >> 3; |
| 381 | status &= 0x0fff; |
| 382 | } |
| 383 | |
| 384 | kfree(req); |
| 385 | return status; |
| 386 | } |
| 387 | |
| 388 | static int ads7845_read12_ser(struct device *dev, unsigned command) |
| 389 | { |
| 390 | struct spi_device *spi = to_spi_device(dev); |
| 391 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 392 | struct ads7845_ser_req *req; |
| 393 | int status; |
| 394 | |
| 395 | req = kzalloc(sizeof *req, GFP_KERNEL); |
| 396 | if (!req) |
| 397 | return -ENOMEM; |
| 398 | |
| 399 | spi_message_init(&req->msg); |
| 400 | |
| 401 | req->command[0] = (u8) command; |
| 402 | req->xfer[0].tx_buf = req->command; |
| 403 | req->xfer[0].rx_buf = req->sample; |
| 404 | req->xfer[0].len = 3; |
| 405 | spi_message_add_tail(&req->xfer[0], &req->msg); |
| 406 | |
| 407 | mutex_lock(&ts->lock); |
| 408 | ads7846_stop(ts); |
| 409 | status = spi_sync(spi, &req->msg); |
| 410 | ads7846_restart(ts); |
| 411 | mutex_unlock(&ts->lock); |
| 412 | |
| 413 | if (status == 0) { |
| 414 | /* BE12 value, then padding */ |
| 415 | status = be16_to_cpu(*((u16 *)&req->sample[1])); |
| 416 | status = status >> 3; |
| 417 | status &= 0x0fff; |
| 418 | } |
| 419 | |
| 420 | kfree(req); |
| 421 | return status; |
| 422 | } |
| 423 | |
| 424 | #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE) |
| 425 | |
| 426 | #define SHOW(name, var, adjust) static ssize_t \ |
| 427 | name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \ |
| 428 | { \ |
| 429 | struct ads7846 *ts = dev_get_drvdata(dev); \ |
| 430 | ssize_t v = ads7846_read12_ser(dev, \ |
| 431 | READ_12BIT_SER(var)); \ |
| 432 | if (v < 0) \ |
| 433 | return v; \ |
| 434 | return sprintf(buf, "%u\n", adjust(ts, v)); \ |
| 435 | } \ |
| 436 | static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL); |
| 437 | |
| 438 | |
| 439 | /* Sysfs conventions report temperatures in millidegrees Celsius. |
| 440 | * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high |
| 441 | * accuracy scheme without calibration data. For now we won't try either; |
| 442 | * userspace sees raw sensor values, and must scale/calibrate appropriately. |
| 443 | */ |
| 444 | static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v) |
| 445 | { |
| 446 | return v; |
| 447 | } |
| 448 | |
| 449 | SHOW(temp0, temp0, null_adjust) /* temp1_input */ |
| 450 | SHOW(temp1, temp1, null_adjust) /* temp2_input */ |
| 451 | |
| 452 | |
| 453 | /* sysfs conventions report voltages in millivolts. We can convert voltages |
| 454 | * if we know vREF. userspace may need to scale vAUX to match the board's |
| 455 | * external resistors; we assume that vBATT only uses the internal ones. |
| 456 | */ |
| 457 | static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v) |
| 458 | { |
| 459 | unsigned retval = v; |
| 460 | |
| 461 | /* external resistors may scale vAUX into 0..vREF */ |
| 462 | retval *= ts->vref_mv; |
| 463 | retval = retval >> 12; |
| 464 | |
| 465 | return retval; |
| 466 | } |
| 467 | |
| 468 | static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v) |
| 469 | { |
| 470 | unsigned retval = vaux_adjust(ts, v); |
| 471 | |
| 472 | /* ads7846 has a resistor ladder to scale this signal down */ |
| 473 | if (ts->model == 7846) |
| 474 | retval *= 4; |
| 475 | |
| 476 | return retval; |
| 477 | } |
| 478 | |
| 479 | SHOW(in0_input, vaux, vaux_adjust) |
| 480 | SHOW(in1_input, vbatt, vbatt_adjust) |
| 481 | |
| 482 | static struct attribute *ads7846_attributes[] = { |
| 483 | &dev_attr_temp0.attr, |
| 484 | &dev_attr_temp1.attr, |
| 485 | &dev_attr_in0_input.attr, |
| 486 | &dev_attr_in1_input.attr, |
| 487 | NULL, |
| 488 | }; |
| 489 | |
| 490 | static struct attribute_group ads7846_attr_group = { |
| 491 | .attrs = ads7846_attributes, |
| 492 | }; |
| 493 | |
| 494 | static struct attribute *ads7843_attributes[] = { |
| 495 | &dev_attr_in0_input.attr, |
| 496 | &dev_attr_in1_input.attr, |
| 497 | NULL, |
| 498 | }; |
| 499 | |
| 500 | static struct attribute_group ads7843_attr_group = { |
| 501 | .attrs = ads7843_attributes, |
| 502 | }; |
| 503 | |
| 504 | static struct attribute *ads7845_attributes[] = { |
| 505 | &dev_attr_in0_input.attr, |
| 506 | NULL, |
| 507 | }; |
| 508 | |
| 509 | static struct attribute_group ads7845_attr_group = { |
| 510 | .attrs = ads7845_attributes, |
| 511 | }; |
| 512 | |
| 513 | static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts) |
| 514 | { |
| 515 | struct device *hwmon; |
| 516 | int err; |
| 517 | |
| 518 | /* hwmon sensors need a reference voltage */ |
| 519 | switch (ts->model) { |
| 520 | case 7846: |
| 521 | if (!ts->vref_mv) { |
| 522 | dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n"); |
| 523 | ts->vref_mv = 2500; |
| 524 | ts->use_internal = true; |
| 525 | } |
| 526 | break; |
| 527 | case 7845: |
| 528 | case 7843: |
| 529 | if (!ts->vref_mv) { |
| 530 | dev_warn(&spi->dev, |
| 531 | "external vREF for ADS%d not specified\n", |
| 532 | ts->model); |
| 533 | return 0; |
| 534 | } |
| 535 | break; |
| 536 | } |
| 537 | |
| 538 | /* different chips have different sensor groups */ |
| 539 | switch (ts->model) { |
| 540 | case 7846: |
| 541 | ts->attr_group = &ads7846_attr_group; |
| 542 | break; |
| 543 | case 7845: |
| 544 | ts->attr_group = &ads7845_attr_group; |
| 545 | break; |
| 546 | case 7843: |
| 547 | ts->attr_group = &ads7843_attr_group; |
| 548 | break; |
| 549 | default: |
| 550 | dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model); |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | err = sysfs_create_group(&spi->dev.kobj, ts->attr_group); |
| 555 | if (err) |
| 556 | return err; |
| 557 | |
| 558 | hwmon = hwmon_device_register(&spi->dev); |
| 559 | if (IS_ERR(hwmon)) { |
| 560 | sysfs_remove_group(&spi->dev.kobj, ts->attr_group); |
| 561 | return PTR_ERR(hwmon); |
| 562 | } |
| 563 | |
| 564 | ts->hwmon = hwmon; |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static void ads784x_hwmon_unregister(struct spi_device *spi, |
| 569 | struct ads7846 *ts) |
| 570 | { |
| 571 | if (ts->hwmon) { |
| 572 | sysfs_remove_group(&spi->dev.kobj, ts->attr_group); |
| 573 | hwmon_device_unregister(ts->hwmon); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | #else |
| 578 | static inline int ads784x_hwmon_register(struct spi_device *spi, |
| 579 | struct ads7846 *ts) |
| 580 | { |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | static inline void ads784x_hwmon_unregister(struct spi_device *spi, |
| 585 | struct ads7846 *ts) |
| 586 | { |
| 587 | } |
| 588 | #endif |
| 589 | |
| 590 | static ssize_t ads7846_pen_down_show(struct device *dev, |
| 591 | struct device_attribute *attr, char *buf) |
| 592 | { |
| 593 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 594 | |
| 595 | return sprintf(buf, "%u\n", ts->pendown); |
| 596 | } |
| 597 | |
| 598 | static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL); |
| 599 | |
| 600 | static ssize_t ads7846_disable_show(struct device *dev, |
| 601 | struct device_attribute *attr, char *buf) |
| 602 | { |
| 603 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 604 | |
| 605 | return sprintf(buf, "%u\n", ts->disabled); |
| 606 | } |
| 607 | |
| 608 | static ssize_t ads7846_disable_store(struct device *dev, |
| 609 | struct device_attribute *attr, |
| 610 | const char *buf, size_t count) |
| 611 | { |
| 612 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 613 | unsigned int i; |
| 614 | int err; |
| 615 | |
| 616 | err = kstrtouint(buf, 10, &i); |
| 617 | if (err) |
| 618 | return err; |
| 619 | |
| 620 | if (i) |
| 621 | ads7846_disable(ts); |
| 622 | else |
| 623 | ads7846_enable(ts); |
| 624 | |
| 625 | return count; |
| 626 | } |
| 627 | |
| 628 | static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store); |
| 629 | |
| 630 | static struct attribute *ads784x_attributes[] = { |
| 631 | &dev_attr_pen_down.attr, |
| 632 | &dev_attr_disable.attr, |
| 633 | NULL, |
| 634 | }; |
| 635 | |
| 636 | static struct attribute_group ads784x_attr_group = { |
| 637 | .attrs = ads784x_attributes, |
| 638 | }; |
| 639 | |
| 640 | /*--------------------------------------------------------------------------*/ |
| 641 | |
| 642 | static int get_pendown_state(struct ads7846 *ts) |
| 643 | { |
| 644 | if (ts->get_pendown_state) |
| 645 | return ts->get_pendown_state(); |
| 646 | |
| 647 | return !gpio_get_value(ts->gpio_pendown); |
| 648 | } |
| 649 | |
| 650 | static void null_wait_for_sync(void) |
| 651 | { |
| 652 | } |
| 653 | |
| 654 | static int ads7846_debounce_filter(void *ads, int data_idx, int *val) |
| 655 | { |
| 656 | struct ads7846 *ts = ads; |
| 657 | |
| 658 | if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) { |
| 659 | /* Start over collecting consistent readings. */ |
| 660 | ts->read_rep = 0; |
| 661 | /* |
| 662 | * Repeat it, if this was the first read or the read |
| 663 | * wasn't consistent enough. |
| 664 | */ |
| 665 | if (ts->read_cnt < ts->debounce_max) { |
| 666 | ts->last_read = *val; |
| 667 | ts->read_cnt++; |
| 668 | return ADS7846_FILTER_REPEAT; |
| 669 | } else { |
| 670 | /* |
| 671 | * Maximum number of debouncing reached and still |
| 672 | * not enough number of consistent readings. Abort |
| 673 | * the whole sample, repeat it in the next sampling |
| 674 | * period. |
| 675 | */ |
| 676 | ts->read_cnt = 0; |
| 677 | return ADS7846_FILTER_IGNORE; |
| 678 | } |
| 679 | } else { |
| 680 | if (++ts->read_rep > ts->debounce_rep) { |
| 681 | /* |
| 682 | * Got a good reading for this coordinate, |
| 683 | * go for the next one. |
| 684 | */ |
| 685 | ts->read_cnt = 0; |
| 686 | ts->read_rep = 0; |
| 687 | return ADS7846_FILTER_OK; |
| 688 | } else { |
| 689 | /* Read more values that are consistent. */ |
| 690 | ts->read_cnt++; |
| 691 | return ADS7846_FILTER_REPEAT; |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | static int ads7846_no_filter(void *ads, int data_idx, int *val) |
| 697 | { |
| 698 | return ADS7846_FILTER_OK; |
| 699 | } |
| 700 | |
| 701 | static int ads7846_get_value(struct ads7846 *ts, struct spi_message *m) |
| 702 | { |
| 703 | struct spi_transfer *t = |
| 704 | list_entry(m->transfers.prev, struct spi_transfer, transfer_list); |
| 705 | |
| 706 | if (ts->model == 7845) { |
| 707 | return be16_to_cpup((__be16 *)&(((char*)t->rx_buf)[1])) >> 3; |
| 708 | } else { |
| 709 | /* |
| 710 | * adjust: on-wire is a must-ignore bit, a BE12 value, then |
| 711 | * padding; built from two 8 bit values written msb-first. |
| 712 | */ |
| 713 | return be16_to_cpup((__be16 *)t->rx_buf) >> 3; |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | static void ads7846_update_value(struct spi_message *m, int val) |
| 718 | { |
| 719 | struct spi_transfer *t = |
| 720 | list_entry(m->transfers.prev, struct spi_transfer, transfer_list); |
| 721 | |
| 722 | *(u16 *)t->rx_buf = val; |
| 723 | } |
| 724 | |
| 725 | static void ads7846_read_state(struct ads7846 *ts) |
| 726 | { |
| 727 | struct ads7846_packet *packet = ts->packet; |
| 728 | struct spi_message *m; |
| 729 | int msg_idx = 0; |
| 730 | int val; |
| 731 | int action; |
| 732 | int error; |
| 733 | |
| 734 | while (msg_idx < ts->msg_count) { |
| 735 | |
| 736 | ts->wait_for_sync(); |
| 737 | |
| 738 | m = &ts->msg[msg_idx]; |
| 739 | error = spi_sync(ts->spi, m); |
| 740 | if (error) { |
| 741 | dev_err(&ts->spi->dev, "spi_async --> %d\n", error); |
| 742 | packet->tc.ignore = true; |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * Last message is power down request, no need to convert |
| 748 | * or filter the value. |
| 749 | */ |
| 750 | if (msg_idx < ts->msg_count - 1) { |
| 751 | |
| 752 | val = ads7846_get_value(ts, m); |
| 753 | |
| 754 | action = ts->filter(ts->filter_data, msg_idx, &val); |
| 755 | switch (action) { |
| 756 | case ADS7846_FILTER_REPEAT: |
| 757 | continue; |
| 758 | |
| 759 | case ADS7846_FILTER_IGNORE: |
| 760 | packet->tc.ignore = true; |
| 761 | msg_idx = ts->msg_count - 1; |
| 762 | continue; |
| 763 | |
| 764 | case ADS7846_FILTER_OK: |
| 765 | ads7846_update_value(m, val); |
| 766 | packet->tc.ignore = false; |
| 767 | msg_idx++; |
| 768 | break; |
| 769 | |
| 770 | default: |
| 771 | BUG(); |
| 772 | } |
| 773 | } else { |
| 774 | msg_idx++; |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | static void ads7846_report_state(struct ads7846 *ts) |
| 780 | { |
| 781 | struct ads7846_packet *packet = ts->packet; |
| 782 | unsigned int Rt; |
| 783 | u16 x, y, z1, z2; |
| 784 | |
| 785 | /* |
| 786 | * ads7846_get_value() does in-place conversion (including byte swap) |
| 787 | * from on-the-wire format as part of debouncing to get stable |
| 788 | * readings. |
| 789 | */ |
| 790 | if (ts->model == 7845) { |
| 791 | x = *(u16 *)packet->tc.x_buf; |
| 792 | y = *(u16 *)packet->tc.y_buf; |
| 793 | z1 = 0; |
| 794 | z2 = 0; |
| 795 | } else { |
| 796 | x = packet->tc.x; |
| 797 | y = packet->tc.y; |
| 798 | z1 = packet->tc.z1; |
| 799 | z2 = packet->tc.z2; |
| 800 | } |
| 801 | |
| 802 | /* range filtering */ |
| 803 | if (x == MAX_12BIT) |
| 804 | x = 0; |
| 805 | |
| 806 | if (ts->model == 7843) { |
| 807 | Rt = ts->pressure_max / 2; |
| 808 | } else if (ts->model == 7845) { |
| 809 | if (get_pendown_state(ts)) |
| 810 | Rt = ts->pressure_max / 2; |
| 811 | else |
| 812 | Rt = 0; |
| 813 | dev_vdbg(&ts->spi->dev, "x/y: %d/%d, PD %d\n", x, y, Rt); |
| 814 | } else if (likely(x && z1)) { |
| 815 | /* compute touch pressure resistance using equation #2 */ |
| 816 | Rt = z2; |
| 817 | Rt -= z1; |
| 818 | Rt *= x; |
| 819 | Rt *= ts->x_plate_ohms; |
| 820 | Rt /= z1; |
| 821 | Rt = (Rt + 2047) >> 12; |
| 822 | } else { |
| 823 | Rt = 0; |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * Sample found inconsistent by debouncing or pressure is beyond |
| 828 | * the maximum. Don't report it to user space, repeat at least |
| 829 | * once more the measurement |
| 830 | */ |
| 831 | if (packet->tc.ignore || Rt > ts->pressure_max) { |
| 832 | dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n", |
| 833 | packet->tc.ignore, Rt); |
| 834 | return; |
| 835 | } |
| 836 | |
| 837 | /* |
| 838 | * Maybe check the pendown state before reporting. This discards |
| 839 | * false readings when the pen is lifted. |
| 840 | */ |
| 841 | if (ts->penirq_recheck_delay_usecs) { |
| 842 | udelay(ts->penirq_recheck_delay_usecs); |
| 843 | if (!get_pendown_state(ts)) |
| 844 | Rt = 0; |
| 845 | } |
| 846 | |
| 847 | /* |
| 848 | * NOTE: We can't rely on the pressure to determine the pen down |
| 849 | * state, even this controller has a pressure sensor. The pressure |
| 850 | * value can fluctuate for quite a while after lifting the pen and |
| 851 | * in some cases may not even settle at the expected value. |
| 852 | * |
| 853 | * The only safe way to check for the pen up condition is in the |
| 854 | * timer by reading the pen signal state (it's a GPIO _and_ IRQ). |
| 855 | */ |
| 856 | if (Rt) { |
| 857 | struct input_dev *input = ts->input; |
| 858 | |
| 859 | if (ts->swap_xy) |
| 860 | swap(x, y); |
| 861 | |
| 862 | if (!ts->pendown) { |
| 863 | input_report_key(input, BTN_TOUCH, 1); |
| 864 | ts->pendown = true; |
| 865 | dev_vdbg(&ts->spi->dev, "DOWN\n"); |
| 866 | } |
| 867 | |
| 868 | input_report_abs(input, ABS_X, x); |
| 869 | input_report_abs(input, ABS_Y, y); |
| 870 | input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt); |
| 871 | |
| 872 | input_sync(input); |
| 873 | dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | static irqreturn_t ads7846_hard_irq(int irq, void *handle) |
| 878 | { |
| 879 | struct ads7846 *ts = handle; |
| 880 | |
| 881 | return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED; |
| 882 | } |
| 883 | |
| 884 | |
| 885 | static irqreturn_t ads7846_irq(int irq, void *handle) |
| 886 | { |
| 887 | struct ads7846 *ts = handle; |
| 888 | |
| 889 | /* Start with a small delay before checking pendown state */ |
| 890 | msleep(TS_POLL_DELAY); |
| 891 | |
| 892 | while (!ts->stopped && get_pendown_state(ts)) { |
| 893 | |
| 894 | /* pen is down, continue with the measurement */ |
| 895 | ads7846_read_state(ts); |
| 896 | |
| 897 | if (!ts->stopped) |
| 898 | ads7846_report_state(ts); |
| 899 | |
| 900 | wait_event_timeout(ts->wait, ts->stopped, |
| 901 | msecs_to_jiffies(TS_POLL_PERIOD)); |
| 902 | } |
| 903 | |
| 904 | if (ts->pendown) { |
| 905 | struct input_dev *input = ts->input; |
| 906 | |
| 907 | input_report_key(input, BTN_TOUCH, 0); |
| 908 | input_report_abs(input, ABS_PRESSURE, 0); |
| 909 | input_sync(input); |
| 910 | |
| 911 | ts->pendown = false; |
| 912 | dev_vdbg(&ts->spi->dev, "UP\n"); |
| 913 | } |
| 914 | |
| 915 | return IRQ_HANDLED; |
| 916 | } |
| 917 | |
| 918 | #ifdef CONFIG_PM_SLEEP |
| 919 | static int ads7846_suspend(struct device *dev) |
| 920 | { |
| 921 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 922 | |
| 923 | mutex_lock(&ts->lock); |
| 924 | |
| 925 | if (!ts->suspended) { |
| 926 | |
| 927 | if (!ts->disabled) |
| 928 | __ads7846_disable(ts); |
| 929 | |
| 930 | if (device_may_wakeup(&ts->spi->dev)) |
| 931 | enable_irq_wake(ts->spi->irq); |
| 932 | |
| 933 | ts->suspended = true; |
| 934 | } |
| 935 | |
| 936 | mutex_unlock(&ts->lock); |
| 937 | |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | static int ads7846_resume(struct device *dev) |
| 942 | { |
| 943 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 944 | |
| 945 | mutex_lock(&ts->lock); |
| 946 | |
| 947 | if (ts->suspended) { |
| 948 | |
| 949 | ts->suspended = false; |
| 950 | |
| 951 | if (device_may_wakeup(&ts->spi->dev)) |
| 952 | disable_irq_wake(ts->spi->irq); |
| 953 | |
| 954 | if (!ts->disabled) |
| 955 | __ads7846_enable(ts); |
| 956 | } |
| 957 | |
| 958 | mutex_unlock(&ts->lock); |
| 959 | |
| 960 | return 0; |
| 961 | } |
| 962 | #endif |
| 963 | |
| 964 | static SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume); |
| 965 | |
| 966 | static int ads7846_setup_pendown(struct spi_device *spi, |
| 967 | struct ads7846 *ts, |
| 968 | const struct ads7846_platform_data *pdata) |
| 969 | { |
| 970 | int err; |
| 971 | |
| 972 | /* |
| 973 | * REVISIT when the irq can be triggered active-low, or if for some |
| 974 | * reason the touchscreen isn't hooked up, we don't need to access |
| 975 | * the pendown state. |
| 976 | */ |
| 977 | |
| 978 | if (pdata->get_pendown_state) { |
| 979 | ts->get_pendown_state = pdata->get_pendown_state; |
| 980 | } else if (gpio_is_valid(pdata->gpio_pendown)) { |
| 981 | |
| 982 | err = gpio_request_one(pdata->gpio_pendown, GPIOF_IN, |
| 983 | "ads7846_pendown"); |
| 984 | if (err) { |
| 985 | dev_err(&spi->dev, |
| 986 | "failed to request/setup pendown GPIO%d: %d\n", |
| 987 | pdata->gpio_pendown, err); |
| 988 | return err; |
| 989 | } |
| 990 | |
| 991 | ts->gpio_pendown = pdata->gpio_pendown; |
| 992 | |
| 993 | if (pdata->gpio_pendown_debounce) |
| 994 | gpio_set_debounce(pdata->gpio_pendown, |
| 995 | pdata->gpio_pendown_debounce); |
| 996 | } else { |
| 997 | dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n"); |
| 998 | return -EINVAL; |
| 999 | } |
| 1000 | |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * Set up the transfers to read touchscreen state; this assumes we |
| 1006 | * use formula #2 for pressure, not #3. |
| 1007 | */ |
| 1008 | static void ads7846_setup_spi_msg(struct ads7846 *ts, |
| 1009 | const struct ads7846_platform_data *pdata) |
| 1010 | { |
| 1011 | struct spi_message *m = &ts->msg[0]; |
| 1012 | struct spi_transfer *x = ts->xfer; |
| 1013 | struct ads7846_packet *packet = ts->packet; |
| 1014 | int vref = pdata->keep_vref_on; |
| 1015 | |
| 1016 | if (ts->model == 7873) { |
| 1017 | /* |
| 1018 | * The AD7873 is almost identical to the ADS7846 |
| 1019 | * keep VREF off during differential/ratiometric |
| 1020 | * conversion modes. |
| 1021 | */ |
| 1022 | ts->model = 7846; |
| 1023 | vref = 0; |
| 1024 | } |
| 1025 | |
| 1026 | ts->msg_count = 1; |
| 1027 | spi_message_init(m); |
| 1028 | m->context = ts; |
| 1029 | |
| 1030 | if (ts->model == 7845) { |
| 1031 | packet->read_y_cmd[0] = READ_Y(vref); |
| 1032 | packet->read_y_cmd[1] = 0; |
| 1033 | packet->read_y_cmd[2] = 0; |
| 1034 | x->tx_buf = &packet->read_y_cmd[0]; |
| 1035 | x->rx_buf = &packet->tc.y_buf[0]; |
| 1036 | x->len = 3; |
| 1037 | spi_message_add_tail(x, m); |
| 1038 | } else { |
| 1039 | /* y- still on; turn on only y+ (and ADC) */ |
| 1040 | packet->read_y = READ_Y(vref); |
| 1041 | x->tx_buf = &packet->read_y; |
| 1042 | x->len = 1; |
| 1043 | spi_message_add_tail(x, m); |
| 1044 | |
| 1045 | x++; |
| 1046 | x->rx_buf = &packet->tc.y; |
| 1047 | x->len = 2; |
| 1048 | spi_message_add_tail(x, m); |
| 1049 | } |
| 1050 | |
| 1051 | /* |
| 1052 | * The first sample after switching drivers can be low quality; |
| 1053 | * optionally discard it, using a second one after the signals |
| 1054 | * have had enough time to stabilize. |
| 1055 | */ |
| 1056 | if (pdata->settle_delay_usecs) { |
| 1057 | x->delay_usecs = pdata->settle_delay_usecs; |
| 1058 | |
| 1059 | x++; |
| 1060 | x->tx_buf = &packet->read_y; |
| 1061 | x->len = 1; |
| 1062 | spi_message_add_tail(x, m); |
| 1063 | |
| 1064 | x++; |
| 1065 | x->rx_buf = &packet->tc.y; |
| 1066 | x->len = 2; |
| 1067 | spi_message_add_tail(x, m); |
| 1068 | } |
| 1069 | |
| 1070 | ts->msg_count++; |
| 1071 | m++; |
| 1072 | spi_message_init(m); |
| 1073 | m->context = ts; |
| 1074 | |
| 1075 | if (ts->model == 7845) { |
| 1076 | x++; |
| 1077 | packet->read_x_cmd[0] = READ_X(vref); |
| 1078 | packet->read_x_cmd[1] = 0; |
| 1079 | packet->read_x_cmd[2] = 0; |
| 1080 | x->tx_buf = &packet->read_x_cmd[0]; |
| 1081 | x->rx_buf = &packet->tc.x_buf[0]; |
| 1082 | x->len = 3; |
| 1083 | spi_message_add_tail(x, m); |
| 1084 | } else { |
| 1085 | /* turn y- off, x+ on, then leave in lowpower */ |
| 1086 | x++; |
| 1087 | packet->read_x = READ_X(vref); |
| 1088 | x->tx_buf = &packet->read_x; |
| 1089 | x->len = 1; |
| 1090 | spi_message_add_tail(x, m); |
| 1091 | |
| 1092 | x++; |
| 1093 | x->rx_buf = &packet->tc.x; |
| 1094 | x->len = 2; |
| 1095 | spi_message_add_tail(x, m); |
| 1096 | } |
| 1097 | |
| 1098 | /* ... maybe discard first sample ... */ |
| 1099 | if (pdata->settle_delay_usecs) { |
| 1100 | x->delay_usecs = pdata->settle_delay_usecs; |
| 1101 | |
| 1102 | x++; |
| 1103 | x->tx_buf = &packet->read_x; |
| 1104 | x->len = 1; |
| 1105 | spi_message_add_tail(x, m); |
| 1106 | |
| 1107 | x++; |
| 1108 | x->rx_buf = &packet->tc.x; |
| 1109 | x->len = 2; |
| 1110 | spi_message_add_tail(x, m); |
| 1111 | } |
| 1112 | |
| 1113 | /* turn y+ off, x- on; we'll use formula #2 */ |
| 1114 | if (ts->model == 7846) { |
| 1115 | ts->msg_count++; |
| 1116 | m++; |
| 1117 | spi_message_init(m); |
| 1118 | m->context = ts; |
| 1119 | |
| 1120 | x++; |
| 1121 | packet->read_z1 = READ_Z1(vref); |
| 1122 | x->tx_buf = &packet->read_z1; |
| 1123 | x->len = 1; |
| 1124 | spi_message_add_tail(x, m); |
| 1125 | |
| 1126 | x++; |
| 1127 | x->rx_buf = &packet->tc.z1; |
| 1128 | x->len = 2; |
| 1129 | spi_message_add_tail(x, m); |
| 1130 | |
| 1131 | /* ... maybe discard first sample ... */ |
| 1132 | if (pdata->settle_delay_usecs) { |
| 1133 | x->delay_usecs = pdata->settle_delay_usecs; |
| 1134 | |
| 1135 | x++; |
| 1136 | x->tx_buf = &packet->read_z1; |
| 1137 | x->len = 1; |
| 1138 | spi_message_add_tail(x, m); |
| 1139 | |
| 1140 | x++; |
| 1141 | x->rx_buf = &packet->tc.z1; |
| 1142 | x->len = 2; |
| 1143 | spi_message_add_tail(x, m); |
| 1144 | } |
| 1145 | |
| 1146 | ts->msg_count++; |
| 1147 | m++; |
| 1148 | spi_message_init(m); |
| 1149 | m->context = ts; |
| 1150 | |
| 1151 | x++; |
| 1152 | packet->read_z2 = READ_Z2(vref); |
| 1153 | x->tx_buf = &packet->read_z2; |
| 1154 | x->len = 1; |
| 1155 | spi_message_add_tail(x, m); |
| 1156 | |
| 1157 | x++; |
| 1158 | x->rx_buf = &packet->tc.z2; |
| 1159 | x->len = 2; |
| 1160 | spi_message_add_tail(x, m); |
| 1161 | |
| 1162 | /* ... maybe discard first sample ... */ |
| 1163 | if (pdata->settle_delay_usecs) { |
| 1164 | x->delay_usecs = pdata->settle_delay_usecs; |
| 1165 | |
| 1166 | x++; |
| 1167 | x->tx_buf = &packet->read_z2; |
| 1168 | x->len = 1; |
| 1169 | spi_message_add_tail(x, m); |
| 1170 | |
| 1171 | x++; |
| 1172 | x->rx_buf = &packet->tc.z2; |
| 1173 | x->len = 2; |
| 1174 | spi_message_add_tail(x, m); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | /* power down */ |
| 1179 | ts->msg_count++; |
| 1180 | m++; |
| 1181 | spi_message_init(m); |
| 1182 | m->context = ts; |
| 1183 | |
| 1184 | if (ts->model == 7845) { |
| 1185 | x++; |
| 1186 | packet->pwrdown_cmd[0] = PWRDOWN; |
| 1187 | packet->pwrdown_cmd[1] = 0; |
| 1188 | packet->pwrdown_cmd[2] = 0; |
| 1189 | x->tx_buf = &packet->pwrdown_cmd[0]; |
| 1190 | x->len = 3; |
| 1191 | } else { |
| 1192 | x++; |
| 1193 | packet->pwrdown = PWRDOWN; |
| 1194 | x->tx_buf = &packet->pwrdown; |
| 1195 | x->len = 1; |
| 1196 | spi_message_add_tail(x, m); |
| 1197 | |
| 1198 | x++; |
| 1199 | x->rx_buf = &packet->dummy; |
| 1200 | x->len = 2; |
| 1201 | } |
| 1202 | |
| 1203 | CS_CHANGE(*x); |
| 1204 | spi_message_add_tail(x, m); |
| 1205 | } |
| 1206 | |
| 1207 | #ifdef CONFIG_OF |
| 1208 | static const struct of_device_id ads7846_dt_ids[] = { |
| 1209 | { .compatible = "ti,tsc2046", .data = (void *) 7846 }, |
| 1210 | { .compatible = "ti,ads7843", .data = (void *) 7843 }, |
| 1211 | { .compatible = "ti,ads7845", .data = (void *) 7845 }, |
| 1212 | { .compatible = "ti,ads7846", .data = (void *) 7846 }, |
| 1213 | { .compatible = "ti,ads7873", .data = (void *) 7873 }, |
| 1214 | { } |
| 1215 | }; |
| 1216 | MODULE_DEVICE_TABLE(of, ads7846_dt_ids); |
| 1217 | |
| 1218 | static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev) |
| 1219 | { |
| 1220 | struct ads7846_platform_data *pdata; |
| 1221 | struct device_node *node = dev->of_node; |
| 1222 | const struct of_device_id *match; |
| 1223 | |
| 1224 | if (!node) { |
| 1225 | dev_err(dev, "Device does not have associated DT data\n"); |
| 1226 | return ERR_PTR(-EINVAL); |
| 1227 | } |
| 1228 | |
| 1229 | match = of_match_device(ads7846_dt_ids, dev); |
| 1230 | if (!match) { |
| 1231 | dev_err(dev, "Unknown device model\n"); |
| 1232 | return ERR_PTR(-EINVAL); |
| 1233 | } |
| 1234 | |
| 1235 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 1236 | if (!pdata) |
| 1237 | return ERR_PTR(-ENOMEM); |
| 1238 | |
| 1239 | pdata->model = (unsigned long)match->data; |
| 1240 | |
| 1241 | of_property_read_u16(node, "ti,vref-delay-usecs", |
| 1242 | &pdata->vref_delay_usecs); |
| 1243 | of_property_read_u16(node, "ti,vref-mv", &pdata->vref_mv); |
| 1244 | pdata->keep_vref_on = of_property_read_bool(node, "ti,keep-vref-on"); |
| 1245 | |
| 1246 | pdata->swap_xy = of_property_read_bool(node, "ti,swap-xy"); |
| 1247 | |
| 1248 | of_property_read_u16(node, "ti,settle-delay-usec", |
| 1249 | &pdata->settle_delay_usecs); |
| 1250 | of_property_read_u16(node, "ti,penirq-recheck-delay-usecs", |
| 1251 | &pdata->penirq_recheck_delay_usecs); |
| 1252 | |
| 1253 | of_property_read_u16(node, "ti,x-plate-ohms", &pdata->x_plate_ohms); |
| 1254 | of_property_read_u16(node, "ti,y-plate-ohms", &pdata->y_plate_ohms); |
| 1255 | |
| 1256 | of_property_read_u16(node, "ti,x-min", &pdata->x_min); |
| 1257 | of_property_read_u16(node, "ti,y-min", &pdata->y_min); |
| 1258 | of_property_read_u16(node, "ti,x-max", &pdata->x_max); |
| 1259 | of_property_read_u16(node, "ti,y-max", &pdata->y_max); |
| 1260 | |
| 1261 | of_property_read_u16(node, "ti,pressure-min", &pdata->pressure_min); |
| 1262 | of_property_read_u16(node, "ti,pressure-max", &pdata->pressure_max); |
| 1263 | |
| 1264 | of_property_read_u16(node, "ti,debounce-max", &pdata->debounce_max); |
| 1265 | of_property_read_u16(node, "ti,debounce-tol", &pdata->debounce_tol); |
| 1266 | of_property_read_u16(node, "ti,debounce-rep", &pdata->debounce_rep); |
| 1267 | |
| 1268 | of_property_read_u32(node, "ti,pendown-gpio-debounce", |
| 1269 | &pdata->gpio_pendown_debounce); |
| 1270 | |
| 1271 | pdata->wakeup = of_property_read_bool(node, "linux,wakeup"); |
| 1272 | |
| 1273 | pdata->gpio_pendown = of_get_named_gpio(dev->of_node, "pendown-gpio", 0); |
| 1274 | |
| 1275 | return pdata; |
| 1276 | } |
| 1277 | #else |
| 1278 | static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev) |
| 1279 | { |
| 1280 | dev_err(dev, "no platform data defined\n"); |
| 1281 | return ERR_PTR(-EINVAL); |
| 1282 | } |
| 1283 | #endif |
| 1284 | |
| 1285 | static int ads7846_probe(struct spi_device *spi) |
| 1286 | { |
| 1287 | const struct ads7846_platform_data *pdata; |
| 1288 | struct ads7846 *ts; |
| 1289 | struct ads7846_packet *packet; |
| 1290 | struct input_dev *input_dev; |
| 1291 | unsigned long irq_flags; |
| 1292 | int err; |
| 1293 | |
| 1294 | if (!spi->irq) { |
| 1295 | dev_dbg(&spi->dev, "no IRQ?\n"); |
| 1296 | return -EINVAL; |
| 1297 | } |
| 1298 | |
| 1299 | /* don't exceed max specified sample rate */ |
| 1300 | if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) { |
| 1301 | dev_err(&spi->dev, "f(sample) %d KHz?\n", |
| 1302 | (spi->max_speed_hz/SAMPLE_BITS)/1000); |
| 1303 | return -EINVAL; |
| 1304 | } |
| 1305 | |
| 1306 | /* |
| 1307 | * We'd set TX word size 8 bits and RX word size to 13 bits ... except |
| 1308 | * that even if the hardware can do that, the SPI controller driver |
| 1309 | * may not. So we stick to very-portable 8 bit words, both RX and TX. |
| 1310 | */ |
| 1311 | spi->bits_per_word = 8; |
| 1312 | spi->mode = SPI_MODE_0; |
| 1313 | err = spi_setup(spi); |
| 1314 | if (err < 0) |
| 1315 | return err; |
| 1316 | |
| 1317 | ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL); |
| 1318 | packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL); |
| 1319 | input_dev = input_allocate_device(); |
| 1320 | if (!ts || !packet || !input_dev) { |
| 1321 | err = -ENOMEM; |
| 1322 | goto err_free_mem; |
| 1323 | } |
| 1324 | |
| 1325 | spi_set_drvdata(spi, ts); |
| 1326 | |
| 1327 | ts->packet = packet; |
| 1328 | ts->spi = spi; |
| 1329 | ts->input = input_dev; |
| 1330 | |
| 1331 | mutex_init(&ts->lock); |
| 1332 | init_waitqueue_head(&ts->wait); |
| 1333 | |
| 1334 | pdata = dev_get_platdata(&spi->dev); |
| 1335 | if (!pdata) { |
| 1336 | pdata = ads7846_probe_dt(&spi->dev); |
| 1337 | if (IS_ERR(pdata)) |
| 1338 | return PTR_ERR(pdata); |
| 1339 | } |
| 1340 | |
| 1341 | ts->model = pdata->model ? : 7846; |
| 1342 | ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100; |
| 1343 | ts->x_plate_ohms = pdata->x_plate_ohms ? : 400; |
| 1344 | ts->pressure_max = pdata->pressure_max ? : ~0; |
| 1345 | |
| 1346 | ts->vref_mv = pdata->vref_mv; |
| 1347 | ts->swap_xy = pdata->swap_xy; |
| 1348 | |
| 1349 | if (pdata->filter != NULL) { |
| 1350 | if (pdata->filter_init != NULL) { |
| 1351 | err = pdata->filter_init(pdata, &ts->filter_data); |
| 1352 | if (err < 0) |
| 1353 | goto err_free_mem; |
| 1354 | } |
| 1355 | ts->filter = pdata->filter; |
| 1356 | ts->filter_cleanup = pdata->filter_cleanup; |
| 1357 | } else if (pdata->debounce_max) { |
| 1358 | ts->debounce_max = pdata->debounce_max; |
| 1359 | if (ts->debounce_max < 2) |
| 1360 | ts->debounce_max = 2; |
| 1361 | ts->debounce_tol = pdata->debounce_tol; |
| 1362 | ts->debounce_rep = pdata->debounce_rep; |
| 1363 | ts->filter = ads7846_debounce_filter; |
| 1364 | ts->filter_data = ts; |
| 1365 | } else { |
| 1366 | ts->filter = ads7846_no_filter; |
| 1367 | } |
| 1368 | |
| 1369 | err = ads7846_setup_pendown(spi, ts, pdata); |
| 1370 | if (err) |
| 1371 | goto err_cleanup_filter; |
| 1372 | |
| 1373 | if (pdata->penirq_recheck_delay_usecs) |
| 1374 | ts->penirq_recheck_delay_usecs = |
| 1375 | pdata->penirq_recheck_delay_usecs; |
| 1376 | |
| 1377 | ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync; |
| 1378 | |
| 1379 | snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev)); |
| 1380 | snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model); |
| 1381 | |
| 1382 | input_dev->name = ts->name; |
| 1383 | input_dev->phys = ts->phys; |
| 1384 | input_dev->dev.parent = &spi->dev; |
| 1385 | |
| 1386 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
| 1387 | input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); |
| 1388 | input_set_abs_params(input_dev, ABS_X, |
| 1389 | pdata->x_min ? : 0, |
| 1390 | pdata->x_max ? : MAX_12BIT, |
| 1391 | 0, 0); |
| 1392 | input_set_abs_params(input_dev, ABS_Y, |
| 1393 | pdata->y_min ? : 0, |
| 1394 | pdata->y_max ? : MAX_12BIT, |
| 1395 | 0, 0); |
| 1396 | input_set_abs_params(input_dev, ABS_PRESSURE, |
| 1397 | pdata->pressure_min, pdata->pressure_max, 0, 0); |
| 1398 | |
| 1399 | ads7846_setup_spi_msg(ts, pdata); |
| 1400 | |
| 1401 | ts->reg = regulator_get(&spi->dev, "vcc"); |
| 1402 | if (IS_ERR(ts->reg)) { |
| 1403 | err = PTR_ERR(ts->reg); |
| 1404 | dev_err(&spi->dev, "unable to get regulator: %d\n", err); |
| 1405 | goto err_free_gpio; |
| 1406 | } |
| 1407 | |
| 1408 | err = regulator_enable(ts->reg); |
| 1409 | if (err) { |
| 1410 | dev_err(&spi->dev, "unable to enable regulator: %d\n", err); |
| 1411 | goto err_put_regulator; |
| 1412 | } |
| 1413 | |
| 1414 | irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING; |
| 1415 | irq_flags |= IRQF_ONESHOT; |
| 1416 | |
| 1417 | err = request_threaded_irq(spi->irq, ads7846_hard_irq, ads7846_irq, |
| 1418 | irq_flags, spi->dev.driver->name, ts); |
| 1419 | if (err && !pdata->irq_flags) { |
| 1420 | dev_info(&spi->dev, |
| 1421 | "trying pin change workaround on irq %d\n", spi->irq); |
| 1422 | irq_flags |= IRQF_TRIGGER_RISING; |
| 1423 | err = request_threaded_irq(spi->irq, |
| 1424 | ads7846_hard_irq, ads7846_irq, |
| 1425 | irq_flags, spi->dev.driver->name, ts); |
| 1426 | } |
| 1427 | |
| 1428 | if (err) { |
| 1429 | dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq); |
| 1430 | goto err_disable_regulator; |
| 1431 | } |
| 1432 | |
| 1433 | err = ads784x_hwmon_register(spi, ts); |
| 1434 | if (err) |
| 1435 | goto err_free_irq; |
| 1436 | |
| 1437 | dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq); |
| 1438 | |
| 1439 | /* |
| 1440 | * Take a first sample, leaving nPENIRQ active and vREF off; avoid |
| 1441 | * the touchscreen, in case it's not connected. |
| 1442 | */ |
| 1443 | if (ts->model == 7845) |
| 1444 | ads7845_read12_ser(&spi->dev, PWRDOWN); |
| 1445 | else |
| 1446 | (void) ads7846_read12_ser(&spi->dev, READ_12BIT_SER(vaux)); |
| 1447 | |
| 1448 | err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group); |
| 1449 | if (err) |
| 1450 | goto err_remove_hwmon; |
| 1451 | |
| 1452 | err = input_register_device(input_dev); |
| 1453 | if (err) |
| 1454 | goto err_remove_attr_group; |
| 1455 | |
| 1456 | device_init_wakeup(&spi->dev, pdata->wakeup); |
| 1457 | |
| 1458 | /* |
| 1459 | * If device does not carry platform data we must have allocated it |
| 1460 | * when parsing DT data. |
| 1461 | */ |
| 1462 | if (!dev_get_platdata(&spi->dev)) |
| 1463 | devm_kfree(&spi->dev, (void *)pdata); |
| 1464 | |
| 1465 | return 0; |
| 1466 | |
| 1467 | err_remove_attr_group: |
| 1468 | sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group); |
| 1469 | err_remove_hwmon: |
| 1470 | ads784x_hwmon_unregister(spi, ts); |
| 1471 | err_free_irq: |
| 1472 | free_irq(spi->irq, ts); |
| 1473 | err_disable_regulator: |
| 1474 | regulator_disable(ts->reg); |
| 1475 | err_put_regulator: |
| 1476 | regulator_put(ts->reg); |
| 1477 | err_free_gpio: |
| 1478 | if (!ts->get_pendown_state) |
| 1479 | gpio_free(ts->gpio_pendown); |
| 1480 | err_cleanup_filter: |
| 1481 | if (ts->filter_cleanup) |
| 1482 | ts->filter_cleanup(ts->filter_data); |
| 1483 | err_free_mem: |
| 1484 | input_free_device(input_dev); |
| 1485 | kfree(packet); |
| 1486 | kfree(ts); |
| 1487 | return err; |
| 1488 | } |
| 1489 | |
| 1490 | static int ads7846_remove(struct spi_device *spi) |
| 1491 | { |
| 1492 | struct ads7846 *ts = spi_get_drvdata(spi); |
| 1493 | |
| 1494 | device_init_wakeup(&spi->dev, false); |
| 1495 | |
| 1496 | sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group); |
| 1497 | |
| 1498 | ads7846_disable(ts); |
| 1499 | free_irq(ts->spi->irq, ts); |
| 1500 | |
| 1501 | input_unregister_device(ts->input); |
| 1502 | |
| 1503 | ads784x_hwmon_unregister(spi, ts); |
| 1504 | |
| 1505 | regulator_disable(ts->reg); |
| 1506 | regulator_put(ts->reg); |
| 1507 | |
| 1508 | if (!ts->get_pendown_state) { |
| 1509 | /* |
| 1510 | * If we are not using specialized pendown method we must |
| 1511 | * have been relying on gpio we set up ourselves. |
| 1512 | */ |
| 1513 | gpio_free(ts->gpio_pendown); |
| 1514 | } |
| 1515 | |
| 1516 | if (ts->filter_cleanup) |
| 1517 | ts->filter_cleanup(ts->filter_data); |
| 1518 | |
| 1519 | kfree(ts->packet); |
| 1520 | kfree(ts); |
| 1521 | |
| 1522 | dev_dbg(&spi->dev, "unregistered touchscreen\n"); |
| 1523 | |
| 1524 | return 0; |
| 1525 | } |
| 1526 | |
| 1527 | static struct spi_driver ads7846_driver = { |
| 1528 | .driver = { |
| 1529 | .name = "ads7846", |
| 1530 | .owner = THIS_MODULE, |
| 1531 | .pm = &ads7846_pm, |
| 1532 | .of_match_table = of_match_ptr(ads7846_dt_ids), |
| 1533 | }, |
| 1534 | .probe = ads7846_probe, |
| 1535 | .remove = ads7846_remove, |
| 1536 | }; |
| 1537 | |
| 1538 | module_spi_driver(ads7846_driver); |
| 1539 | |
| 1540 | MODULE_DESCRIPTION("ADS7846 TouchScreen Driver"); |
| 1541 | MODULE_LICENSE("GPL"); |
| 1542 | MODULE_ALIAS("spi:ads7846"); |