Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * TSC2005 touchscreen driver |
| 3 | * |
| 4 | * Copyright (C) 2006-2010 Nokia Corporation |
| 5 | * |
| 6 | * Author: Lauri Leukkunen <lauri.leukkunen@nokia.com> |
| 7 | * based on TSC2301 driver by Klaus K. Pedersen <klaus.k.pedersen@nokia.com> |
| 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, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/input.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/spi/spi.h> |
| 31 | #include <linux/spi/tsc2005.h> |
| 32 | |
| 33 | /* |
| 34 | * The touchscreen interface operates as follows: |
| 35 | * |
| 36 | * 1) Pen is pressed against the touchscreen. |
| 37 | * 2) TSC2005 performs AD conversion. |
| 38 | * 3) After the conversion is done TSC2005 drives DAV line down. |
| 39 | * 4) GPIO IRQ is received and tsc2005_irq_thread() is scheduled. |
| 40 | * 5) tsc2005_irq_thread() queues up an spi transfer to fetch the x, y, z1, z2 |
| 41 | * values. |
| 42 | * 6) tsc2005_irq_thread() reports coordinates to input layer and sets up |
| 43 | * tsc2005_penup_timer() to be called after TSC2005_PENUP_TIME_MS (40ms). |
| 44 | * 7) When the penup timer expires, there have not been touch or DAV interrupts |
| 45 | * during the last 40ms which means the pen has been lifted. |
| 46 | * |
| 47 | * ESD recovery via a hardware reset is done if the TSC2005 doesn't respond |
| 48 | * after a configurable period (in ms) of activity. If esd_timeout is 0, the |
| 49 | * watchdog is disabled. |
| 50 | */ |
| 51 | |
| 52 | /* control byte 1 */ |
| 53 | #define TSC2005_CMD 0x80 |
| 54 | #define TSC2005_CMD_NORMAL 0x00 |
| 55 | #define TSC2005_CMD_STOP 0x01 |
| 56 | #define TSC2005_CMD_12BIT 0x04 |
| 57 | |
| 58 | /* control byte 0 */ |
| 59 | #define TSC2005_REG_READ 0x0001 |
| 60 | #define TSC2005_REG_PND0 0x0002 |
| 61 | #define TSC2005_REG_X 0x0000 |
| 62 | #define TSC2005_REG_Y 0x0008 |
| 63 | #define TSC2005_REG_Z1 0x0010 |
| 64 | #define TSC2005_REG_Z2 0x0018 |
| 65 | #define TSC2005_REG_TEMP_HIGH 0x0050 |
| 66 | #define TSC2005_REG_CFR0 0x0060 |
| 67 | #define TSC2005_REG_CFR1 0x0068 |
| 68 | #define TSC2005_REG_CFR2 0x0070 |
| 69 | |
| 70 | /* configuration register 0 */ |
| 71 | #define TSC2005_CFR0_PRECHARGE_276US 0x0040 |
| 72 | #define TSC2005_CFR0_STABTIME_1MS 0x0300 |
| 73 | #define TSC2005_CFR0_CLOCK_1MHZ 0x1000 |
| 74 | #define TSC2005_CFR0_RESOLUTION12 0x2000 |
| 75 | #define TSC2005_CFR0_PENMODE 0x8000 |
| 76 | #define TSC2005_CFR0_INITVALUE (TSC2005_CFR0_STABTIME_1MS | \ |
| 77 | TSC2005_CFR0_CLOCK_1MHZ | \ |
| 78 | TSC2005_CFR0_RESOLUTION12 | \ |
| 79 | TSC2005_CFR0_PRECHARGE_276US | \ |
| 80 | TSC2005_CFR0_PENMODE) |
| 81 | |
| 82 | /* bits common to both read and write of configuration register 0 */ |
| 83 | #define TSC2005_CFR0_RW_MASK 0x3fff |
| 84 | |
| 85 | /* configuration register 1 */ |
| 86 | #define TSC2005_CFR1_BATCHDELAY_4MS 0x0003 |
| 87 | #define TSC2005_CFR1_INITVALUE TSC2005_CFR1_BATCHDELAY_4MS |
| 88 | |
| 89 | /* configuration register 2 */ |
| 90 | #define TSC2005_CFR2_MAVE_Z 0x0004 |
| 91 | #define TSC2005_CFR2_MAVE_Y 0x0008 |
| 92 | #define TSC2005_CFR2_MAVE_X 0x0010 |
| 93 | #define TSC2005_CFR2_AVG_7 0x0800 |
| 94 | #define TSC2005_CFR2_MEDIUM_15 0x3000 |
| 95 | #define TSC2005_CFR2_INITVALUE (TSC2005_CFR2_MAVE_X | \ |
| 96 | TSC2005_CFR2_MAVE_Y | \ |
| 97 | TSC2005_CFR2_MAVE_Z | \ |
| 98 | TSC2005_CFR2_MEDIUM_15 | \ |
| 99 | TSC2005_CFR2_AVG_7) |
| 100 | |
| 101 | #define MAX_12BIT 0xfff |
| 102 | #define TSC2005_SPI_MAX_SPEED_HZ 10000000 |
| 103 | #define TSC2005_PENUP_TIME_MS 40 |
| 104 | |
| 105 | struct tsc2005_spi_rd { |
| 106 | struct spi_transfer spi_xfer; |
| 107 | u32 spi_tx; |
| 108 | u32 spi_rx; |
| 109 | }; |
| 110 | |
| 111 | struct tsc2005 { |
| 112 | struct spi_device *spi; |
| 113 | |
| 114 | struct spi_message spi_read_msg; |
| 115 | struct tsc2005_spi_rd spi_x; |
| 116 | struct tsc2005_spi_rd spi_y; |
| 117 | struct tsc2005_spi_rd spi_z1; |
| 118 | struct tsc2005_spi_rd spi_z2; |
| 119 | |
| 120 | struct input_dev *idev; |
| 121 | char phys[32]; |
| 122 | |
| 123 | struct mutex mutex; |
| 124 | |
| 125 | /* raw copy of previous x,y,z */ |
| 126 | int in_x; |
| 127 | int in_y; |
| 128 | int in_z1; |
| 129 | int in_z2; |
| 130 | |
| 131 | struct timer_list penup_timer; |
| 132 | struct work_struct penup_work; |
| 133 | |
| 134 | unsigned int esd_timeout; |
| 135 | struct timer_list esd_timer; |
| 136 | struct work_struct esd_work; |
| 137 | |
| 138 | unsigned int x_plate_ohm; |
| 139 | |
| 140 | bool disabled; |
| 141 | unsigned int disable_depth; |
| 142 | unsigned int pen_down; |
| 143 | |
| 144 | void (*set_reset)(bool enable); |
| 145 | }; |
| 146 | |
| 147 | static void tsc2005_cmd(struct tsc2005 *ts, u8 cmd) |
| 148 | { |
| 149 | u8 tx; |
| 150 | struct spi_message msg; |
| 151 | struct spi_transfer xfer = { 0 }; |
| 152 | |
| 153 | tx = TSC2005_CMD | TSC2005_CMD_12BIT | cmd; |
| 154 | |
| 155 | xfer.tx_buf = &tx; |
| 156 | xfer.rx_buf = NULL; |
| 157 | xfer.len = 1; |
| 158 | xfer.bits_per_word = 8; |
| 159 | |
| 160 | spi_message_init(&msg); |
| 161 | spi_message_add_tail(&xfer, &msg); |
| 162 | spi_sync(ts->spi, &msg); |
| 163 | } |
| 164 | |
| 165 | static void tsc2005_write(struct tsc2005 *ts, u8 reg, u16 value) |
| 166 | { |
| 167 | u32 tx; |
| 168 | struct spi_message msg; |
| 169 | struct spi_transfer xfer = { 0 }; |
| 170 | |
| 171 | tx = (reg | TSC2005_REG_PND0) << 16; |
| 172 | tx |= value; |
| 173 | |
| 174 | xfer.tx_buf = &tx; |
| 175 | xfer.rx_buf = NULL; |
| 176 | xfer.len = 4; |
| 177 | xfer.bits_per_word = 24; |
| 178 | |
| 179 | spi_message_init(&msg); |
| 180 | spi_message_add_tail(&xfer, &msg); |
| 181 | spi_sync(ts->spi, &msg); |
| 182 | } |
| 183 | |
| 184 | static void tsc2005_setup_read(struct tsc2005_spi_rd *rd, u8 reg, bool last) |
| 185 | { |
| 186 | rd->spi_tx = (reg | TSC2005_REG_READ) << 16; |
| 187 | rd->spi_xfer.tx_buf = &rd->spi_tx; |
| 188 | rd->spi_xfer.rx_buf = &rd->spi_rx; |
| 189 | rd->spi_xfer.len = 4; |
| 190 | rd->spi_xfer.bits_per_word = 24; |
| 191 | rd->spi_xfer.cs_change = !last; |
| 192 | } |
| 193 | |
| 194 | static void tsc2005_read(struct tsc2005 *ts, u8 reg, u16 *value) |
| 195 | { |
| 196 | struct spi_message msg; |
| 197 | struct tsc2005_spi_rd spi_rd = { { 0 }, 0, 0 }; |
| 198 | |
| 199 | tsc2005_setup_read(&spi_rd, reg, 1); |
| 200 | |
| 201 | spi_message_init(&msg); |
| 202 | spi_message_add_tail(&spi_rd.spi_xfer, &msg); |
| 203 | spi_sync(ts->spi, &msg); |
| 204 | *value = spi_rd.spi_rx; |
| 205 | } |
| 206 | |
| 207 | static void tsc2005_update_pen_state(struct tsc2005 *ts, |
| 208 | int x, int y, int pressure) |
| 209 | { |
| 210 | if (pressure) { |
| 211 | input_report_abs(ts->idev, ABS_X, x); |
| 212 | input_report_abs(ts->idev, ABS_Y, y); |
| 213 | input_report_abs(ts->idev, ABS_PRESSURE, pressure); |
| 214 | if (!ts->pen_down) { |
| 215 | input_report_key(ts->idev, BTN_TOUCH, !!pressure); |
| 216 | ts->pen_down = 1; |
| 217 | } |
| 218 | } else { |
| 219 | input_report_abs(ts->idev, ABS_PRESSURE, 0); |
| 220 | if (ts->pen_down) { |
| 221 | input_report_key(ts->idev, BTN_TOUCH, 0); |
| 222 | ts->pen_down = 0; |
| 223 | } |
| 224 | } |
| 225 | input_sync(ts->idev); |
| 226 | dev_dbg(&ts->spi->dev, "point(%4d,%4d), pressure (%4d)\n", x, y, |
| 227 | pressure); |
| 228 | } |
| 229 | |
| 230 | static irqreturn_t tsc2005_irq_handler(int irq, void *dev_id) |
| 231 | { |
| 232 | struct tsc2005 *ts = dev_id; |
| 233 | |
| 234 | /* update the penup timer only if it's pending */ |
| 235 | mod_timer_pending(&ts->penup_timer, |
| 236 | jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS)); |
| 237 | |
| 238 | return IRQ_WAKE_THREAD; |
| 239 | } |
| 240 | |
| 241 | static irqreturn_t tsc2005_irq_thread(int irq, void *_ts) |
| 242 | { |
| 243 | struct tsc2005 *ts = _ts; |
| 244 | unsigned int pressure; |
| 245 | u32 x; |
| 246 | u32 y; |
| 247 | u32 z1; |
| 248 | u32 z2; |
| 249 | |
| 250 | mutex_lock(&ts->mutex); |
| 251 | |
| 252 | if (unlikely(ts->disable_depth)) |
| 253 | goto out; |
| 254 | |
| 255 | /* read the coordinates */ |
| 256 | spi_sync(ts->spi, &ts->spi_read_msg); |
| 257 | x = ts->spi_x.spi_rx; |
| 258 | y = ts->spi_y.spi_rx; |
| 259 | z1 = ts->spi_z1.spi_rx; |
| 260 | z2 = ts->spi_z2.spi_rx; |
| 261 | |
| 262 | /* validate position */ |
| 263 | if (unlikely(x > MAX_12BIT || y > MAX_12BIT)) |
| 264 | goto out; |
| 265 | |
| 266 | /* skip coords if the pressure components are out of range */ |
| 267 | if (unlikely(z1 == 0 || z2 > MAX_12BIT || z1 >= z2)) |
| 268 | goto out; |
| 269 | |
| 270 | /* skip point if this is a pen down with the exact same values as |
| 271 | * the value before pen-up - that implies SPI fed us stale data |
| 272 | */ |
| 273 | if (!ts->pen_down && |
| 274 | ts->in_x == x && |
| 275 | ts->in_y == y && |
| 276 | ts->in_z1 == z1 && |
| 277 | ts->in_z2 == z2) |
| 278 | goto out; |
| 279 | |
| 280 | /* At this point we are happy we have a valid and useful reading. |
| 281 | * Remember it for later comparisons. We may now begin downsampling |
| 282 | */ |
| 283 | ts->in_x = x; |
| 284 | ts->in_y = y; |
| 285 | ts->in_z1 = z1; |
| 286 | ts->in_z2 = z2; |
| 287 | |
| 288 | /* compute touch pressure resistance using equation #1 */ |
| 289 | pressure = x * (z2 - z1) / z1; |
| 290 | pressure = pressure * ts->x_plate_ohm / 4096; |
| 291 | if (unlikely(pressure > MAX_12BIT)) |
| 292 | goto out; |
| 293 | |
| 294 | tsc2005_update_pen_state(ts, x, y, pressure); |
| 295 | |
| 296 | /* set the penup timer */ |
| 297 | mod_timer(&ts->penup_timer, |
| 298 | jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS)); |
| 299 | |
| 300 | if (!ts->esd_timeout) |
| 301 | goto out; |
| 302 | |
| 303 | /* update the watchdog timer */ |
| 304 | mod_timer(&ts->esd_timer, |
| 305 | round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout))); |
| 306 | |
| 307 | out: |
| 308 | mutex_unlock(&ts->mutex); |
| 309 | return IRQ_HANDLED; |
| 310 | } |
| 311 | |
| 312 | static void tsc2005_penup_timer(unsigned long data) |
| 313 | { |
| 314 | struct tsc2005 *ts = (struct tsc2005 *)data; |
| 315 | |
| 316 | schedule_work(&ts->penup_work); |
| 317 | } |
| 318 | |
| 319 | static void tsc2005_penup_work(struct work_struct *work) |
| 320 | { |
| 321 | struct tsc2005 *ts = container_of(work, struct tsc2005, penup_work); |
| 322 | |
| 323 | mutex_lock(&ts->mutex); |
| 324 | tsc2005_update_pen_state(ts, 0, 0, 0); |
| 325 | mutex_unlock(&ts->mutex); |
| 326 | } |
| 327 | |
| 328 | static void tsc2005_start_scan(struct tsc2005 *ts) |
| 329 | { |
| 330 | tsc2005_write(ts, TSC2005_REG_CFR0, TSC2005_CFR0_INITVALUE); |
| 331 | tsc2005_write(ts, TSC2005_REG_CFR1, TSC2005_CFR1_INITVALUE); |
| 332 | tsc2005_write(ts, TSC2005_REG_CFR2, TSC2005_CFR2_INITVALUE); |
| 333 | tsc2005_cmd(ts, TSC2005_CMD_NORMAL); |
| 334 | } |
| 335 | |
| 336 | static void tsc2005_stop_scan(struct tsc2005 *ts) |
| 337 | { |
| 338 | tsc2005_cmd(ts, TSC2005_CMD_STOP); |
| 339 | } |
| 340 | |
| 341 | /* must be called with mutex held */ |
| 342 | static void tsc2005_disable(struct tsc2005 *ts) |
| 343 | { |
| 344 | if (ts->disable_depth++ != 0) |
| 345 | return; |
| 346 | disable_irq(ts->spi->irq); |
| 347 | if (ts->esd_timeout) |
| 348 | del_timer_sync(&ts->esd_timer); |
| 349 | del_timer_sync(&ts->penup_timer); |
| 350 | tsc2005_stop_scan(ts); |
| 351 | } |
| 352 | |
| 353 | /* must be called with mutex held */ |
| 354 | static void tsc2005_enable(struct tsc2005 *ts) |
| 355 | { |
| 356 | if (--ts->disable_depth != 0) |
| 357 | return; |
| 358 | tsc2005_start_scan(ts); |
| 359 | enable_irq(ts->spi->irq); |
| 360 | if (!ts->esd_timeout) |
| 361 | return; |
| 362 | mod_timer(&ts->esd_timer, |
| 363 | round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout))); |
| 364 | } |
| 365 | |
| 366 | static ssize_t tsc2005_disable_show(struct device *dev, |
| 367 | struct device_attribute *attr, char *buf) |
| 368 | { |
| 369 | struct tsc2005 *ts = dev_get_drvdata(dev); |
| 370 | |
| 371 | return sprintf(buf, "%u\n", ts->disabled); |
| 372 | } |
| 373 | |
| 374 | static ssize_t tsc2005_disable_store(struct device *dev, |
| 375 | struct device_attribute *attr, |
| 376 | const char *buf, size_t count) |
| 377 | { |
| 378 | struct tsc2005 *ts = dev_get_drvdata(dev); |
| 379 | unsigned long res; |
| 380 | int i; |
| 381 | |
| 382 | if (strict_strtoul(buf, 10, &res) < 0) |
| 383 | return -EINVAL; |
| 384 | i = res ? 1 : 0; |
| 385 | |
| 386 | mutex_lock(&ts->mutex); |
| 387 | if (i == ts->disabled) |
| 388 | goto out; |
| 389 | ts->disabled = i; |
| 390 | if (i) |
| 391 | tsc2005_disable(ts); |
| 392 | else |
| 393 | tsc2005_enable(ts); |
| 394 | out: |
| 395 | mutex_unlock(&ts->mutex); |
| 396 | return count; |
| 397 | } |
| 398 | static DEVICE_ATTR(disable, 0664, tsc2005_disable_show, tsc2005_disable_store); |
| 399 | |
| 400 | static ssize_t tsc2005_selftest_show(struct device *dev, |
| 401 | struct device_attribute *attr, |
| 402 | char *buf) |
| 403 | { |
| 404 | struct tsc2005 *ts = dev_get_drvdata(dev); |
| 405 | u16 temp_high; |
| 406 | u16 temp_high_orig; |
| 407 | u16 temp_high_test; |
| 408 | unsigned int result; |
| 409 | |
| 410 | if (!ts->set_reset) { |
| 411 | dev_warn(&ts->spi->dev, |
| 412 | "unable to selftest: no reset function\n"); |
| 413 | result = 0; |
| 414 | goto out; |
| 415 | } |
| 416 | |
| 417 | mutex_lock(&ts->mutex); |
| 418 | |
| 419 | /* |
| 420 | * Test TSC2005 communications via temp high register. |
| 421 | */ |
| 422 | tsc2005_disable(ts); |
| 423 | result = 1; |
| 424 | tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high_orig); |
| 425 | temp_high_test = (temp_high_orig - 1) & MAX_12BIT; |
| 426 | tsc2005_write(ts, TSC2005_REG_TEMP_HIGH, temp_high_test); |
| 427 | tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high); |
| 428 | if (temp_high != temp_high_test) { |
| 429 | dev_warn(dev, "selftest failed: %d != %d\n", |
| 430 | temp_high, temp_high_test); |
| 431 | result = 0; |
| 432 | } |
| 433 | |
| 434 | /* hardware reset */ |
| 435 | ts->set_reset(0); |
| 436 | usleep_range(100, 500); /* only 10us required */ |
| 437 | ts->set_reset(1); |
| 438 | tsc2005_enable(ts); |
| 439 | |
| 440 | /* test that the reset really happened */ |
| 441 | tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high); |
| 442 | if (temp_high != temp_high_orig) { |
| 443 | dev_warn(dev, "selftest failed after reset: %d != %d\n", |
| 444 | temp_high, temp_high_orig); |
| 445 | result = 0; |
| 446 | } |
| 447 | |
| 448 | mutex_unlock(&ts->mutex); |
| 449 | |
| 450 | out: |
| 451 | return sprintf(buf, "%u\n", result); |
| 452 | } |
| 453 | static DEVICE_ATTR(selftest, S_IRUGO, tsc2005_selftest_show, NULL); |
| 454 | |
| 455 | static void tsc2005_esd_timer(unsigned long data) |
| 456 | { |
| 457 | struct tsc2005 *ts = (struct tsc2005 *)data; |
| 458 | |
| 459 | schedule_work(&ts->esd_work); |
| 460 | } |
| 461 | |
| 462 | static void tsc2005_esd_work(struct work_struct *work) |
| 463 | { |
| 464 | struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work); |
| 465 | u16 r; |
| 466 | |
| 467 | mutex_lock(&ts->mutex); |
| 468 | |
| 469 | if (ts->disable_depth) |
| 470 | goto out; |
| 471 | |
| 472 | /* |
| 473 | * If we cannot read our known value from configuration register 0 then |
| 474 | * reset the controller as if from power-up and start scanning again. |
| 475 | */ |
| 476 | tsc2005_read(ts, TSC2005_REG_CFR0, &r); |
| 477 | if ((r ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK) { |
| 478 | dev_info(&ts->spi->dev, "TSC2005 not responding - resetting\n"); |
| 479 | ts->set_reset(0); |
| 480 | tsc2005_update_pen_state(ts, 0, 0, 0); |
| 481 | usleep_range(100, 500); /* only 10us required */ |
| 482 | ts->set_reset(1); |
| 483 | tsc2005_start_scan(ts); |
| 484 | } |
| 485 | |
| 486 | /* re-arm the watchdog */ |
| 487 | mod_timer(&ts->esd_timer, |
| 488 | round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout))); |
| 489 | |
| 490 | out: |
| 491 | mutex_unlock(&ts->mutex); |
| 492 | } |
| 493 | |
| 494 | static void __devinit tsc2005_setup_spi_xfer(struct tsc2005 *ts) |
| 495 | { |
| 496 | tsc2005_setup_read(&ts->spi_x, TSC2005_REG_X, 0); |
| 497 | tsc2005_setup_read(&ts->spi_y, TSC2005_REG_Y, 0); |
| 498 | tsc2005_setup_read(&ts->spi_z1, TSC2005_REG_Z1, 0); |
| 499 | tsc2005_setup_read(&ts->spi_z2, TSC2005_REG_Z2, 1); |
| 500 | |
| 501 | spi_message_init(&ts->spi_read_msg); |
| 502 | spi_message_add_tail(&ts->spi_x.spi_xfer, &ts->spi_read_msg); |
| 503 | spi_message_add_tail(&ts->spi_y.spi_xfer, &ts->spi_read_msg); |
| 504 | spi_message_add_tail(&ts->spi_z1.spi_xfer, &ts->spi_read_msg); |
| 505 | spi_message_add_tail(&ts->spi_z2.spi_xfer, &ts->spi_read_msg); |
| 506 | } |
| 507 | |
| 508 | static struct attribute *tsc2005_attrs[] = { |
| 509 | &dev_attr_disable.attr, |
| 510 | &dev_attr_selftest.attr, |
| 511 | NULL |
| 512 | }; |
| 513 | |
| 514 | static struct attribute_group tsc2005_attr_group = { |
| 515 | .attrs = tsc2005_attrs, |
| 516 | }; |
| 517 | |
| 518 | static int __devinit tsc2005_setup(struct tsc2005 *ts, |
| 519 | struct tsc2005_platform_data *pdata) |
| 520 | { |
| 521 | int r; |
| 522 | int fudge_x; |
| 523 | int fudge_y; |
| 524 | int fudge_p; |
| 525 | int p_max; |
| 526 | int x_max; |
| 527 | int y_max; |
| 528 | |
| 529 | mutex_init(&ts->mutex); |
| 530 | |
| 531 | tsc2005_setup_spi_xfer(ts); |
| 532 | |
| 533 | init_timer(&ts->penup_timer); |
| 534 | setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts); |
| 535 | INIT_WORK(&ts->penup_work, tsc2005_penup_work); |
| 536 | |
| 537 | fudge_x = pdata->ts_x_fudge ? : 4; |
| 538 | fudge_y = pdata->ts_y_fudge ? : 8; |
| 539 | fudge_p = pdata->ts_pressure_fudge ? : 2; |
| 540 | x_max = pdata->ts_x_max ? : MAX_12BIT; |
| 541 | y_max = pdata->ts_y_max ? : MAX_12BIT; |
| 542 | p_max = pdata->ts_pressure_max ? : MAX_12BIT; |
| 543 | ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280; |
| 544 | ts->esd_timeout = pdata->esd_timeout_ms; |
| 545 | ts->set_reset = pdata->set_reset; |
| 546 | |
| 547 | ts->idev = input_allocate_device(); |
| 548 | if (ts->idev == NULL) |
| 549 | return -ENOMEM; |
| 550 | ts->idev->name = "TSC2005 touchscreen"; |
| 551 | snprintf(ts->phys, sizeof(ts->phys), "%s/input-ts", |
| 552 | dev_name(&ts->spi->dev)); |
| 553 | ts->idev->phys = ts->phys; |
| 554 | ts->idev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY); |
| 555 | ts->idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE); |
| 556 | ts->idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); |
| 557 | |
| 558 | input_set_abs_params(ts->idev, ABS_X, 0, x_max, fudge_x, 0); |
| 559 | input_set_abs_params(ts->idev, ABS_Y, 0, y_max, fudge_y, 0); |
| 560 | input_set_abs_params(ts->idev, ABS_PRESSURE, 0, p_max, fudge_p, 0); |
| 561 | |
| 562 | r = request_threaded_irq(ts->spi->irq, tsc2005_irq_handler, |
| 563 | tsc2005_irq_thread, IRQF_TRIGGER_RISING, |
| 564 | "tsc2005", ts); |
| 565 | if (r) { |
| 566 | dev_err(&ts->spi->dev, "request_threaded_irq(): %d\n", r); |
| 567 | goto err1; |
| 568 | } |
| 569 | set_irq_wake(ts->spi->irq, 1); |
| 570 | |
| 571 | r = input_register_device(ts->idev); |
| 572 | if (r) { |
| 573 | dev_err(&ts->spi->dev, "input_register_device(): %d\n", r); |
| 574 | goto err2; |
| 575 | } |
| 576 | |
| 577 | r = sysfs_create_group(&ts->spi->dev.kobj, &tsc2005_attr_group); |
| 578 | if (r) |
| 579 | dev_warn(&ts->spi->dev, "sysfs entry creation failed: %d\n", r); |
| 580 | |
| 581 | tsc2005_start_scan(ts); |
| 582 | |
| 583 | if (!ts->esd_timeout || !ts->set_reset) |
| 584 | goto done; |
| 585 | |
| 586 | /* start the optional ESD watchdog */ |
| 587 | setup_timer(&ts->esd_timer, tsc2005_esd_timer, (unsigned long)ts); |
| 588 | INIT_WORK(&ts->esd_work, tsc2005_esd_work); |
| 589 | mod_timer(&ts->esd_timer, |
| 590 | round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout))); |
| 591 | |
| 592 | done: |
| 593 | return 0; |
| 594 | |
| 595 | err2: |
| 596 | free_irq(ts->spi->irq, ts); |
| 597 | |
| 598 | err1: |
| 599 | input_free_device(ts->idev); |
| 600 | return r; |
| 601 | } |
| 602 | |
| 603 | static int __devinit tsc2005_probe(struct spi_device *spi) |
| 604 | { |
| 605 | struct tsc2005_platform_data *pdata = spi->dev.platform_data; |
| 606 | struct tsc2005 *ts; |
| 607 | int r; |
| 608 | |
| 609 | if (spi->irq < 0) { |
| 610 | dev_dbg(&spi->dev, "no irq\n"); |
| 611 | return -ENODEV; |
| 612 | } |
| 613 | |
| 614 | if (!pdata) { |
| 615 | dev_dbg(&spi->dev, "no platform data\n"); |
| 616 | return -ENODEV; |
| 617 | } |
| 618 | |
| 619 | ts = kzalloc(sizeof(*ts), GFP_KERNEL); |
| 620 | if (ts == NULL) |
| 621 | return -ENOMEM; |
| 622 | |
| 623 | dev_set_drvdata(&spi->dev, ts); |
| 624 | ts->spi = spi; |
| 625 | spi->dev.power.power_state = PMSG_ON; |
| 626 | spi->mode = SPI_MODE_0; |
| 627 | spi->bits_per_word = 8; |
| 628 | if (!spi->max_speed_hz) |
| 629 | spi->max_speed_hz = TSC2005_SPI_MAX_SPEED_HZ; |
| 630 | spi_setup(spi); |
| 631 | |
| 632 | r = tsc2005_setup(ts, pdata); |
| 633 | if (r) |
| 634 | kfree(ts); |
| 635 | return r; |
| 636 | } |
| 637 | |
| 638 | static int __devexit tsc2005_remove(struct spi_device *spi) |
| 639 | { |
| 640 | struct tsc2005 *ts = dev_get_drvdata(&spi->dev); |
| 641 | |
| 642 | mutex_lock(&ts->mutex); |
| 643 | tsc2005_disable(ts); |
| 644 | mutex_unlock(&ts->mutex); |
| 645 | |
| 646 | if (ts->esd_timeout) |
| 647 | del_timer_sync(&ts->esd_timer); |
| 648 | del_timer_sync(&ts->penup_timer); |
| 649 | |
| 650 | flush_work(&ts->esd_work); |
| 651 | flush_work(&ts->penup_work); |
| 652 | |
| 653 | sysfs_remove_group(&ts->spi->dev.kobj, &tsc2005_attr_group); |
| 654 | free_irq(ts->spi->irq, ts); |
| 655 | input_unregister_device(ts->idev); |
| 656 | kfree(ts); |
| 657 | |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | #ifdef CONFIG_PM |
| 662 | static int tsc2005_suspend(struct spi_device *spi, pm_message_t mesg) |
| 663 | { |
| 664 | struct tsc2005 *ts = dev_get_drvdata(&spi->dev); |
| 665 | |
| 666 | mutex_lock(&ts->mutex); |
| 667 | tsc2005_disable(ts); |
| 668 | mutex_unlock(&ts->mutex); |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static int tsc2005_resume(struct spi_device *spi) |
| 674 | { |
| 675 | struct tsc2005 *ts = dev_get_drvdata(&spi->dev); |
| 676 | |
| 677 | mutex_lock(&ts->mutex); |
| 678 | tsc2005_enable(ts); |
| 679 | mutex_unlock(&ts->mutex); |
| 680 | |
| 681 | return 0; |
| 682 | } |
| 683 | #endif |
| 684 | |
| 685 | static struct spi_driver tsc2005_driver = { |
| 686 | .driver = { |
| 687 | .name = "tsc2005", |
| 688 | .owner = THIS_MODULE, |
| 689 | }, |
| 690 | #ifdef CONFIG_PM |
| 691 | .suspend = tsc2005_suspend, |
| 692 | .resume = tsc2005_resume, |
| 693 | #endif |
| 694 | .probe = tsc2005_probe, |
| 695 | .remove = __devexit_p(tsc2005_remove), |
| 696 | }; |
| 697 | |
| 698 | static int __init tsc2005_init(void) |
| 699 | { |
| 700 | printk(KERN_INFO "TSC2005 driver initializing\n"); |
| 701 | return spi_register_driver(&tsc2005_driver); |
| 702 | } |
| 703 | module_init(tsc2005_init); |
| 704 | |
| 705 | static void __exit tsc2005_exit(void) |
| 706 | { |
| 707 | spi_unregister_driver(&tsc2005_driver); |
| 708 | } |
| 709 | module_exit(tsc2005_exit); |
| 710 | |
| 711 | MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>"); |
| 712 | MODULE_LICENSE("GPL"); |
| 713 | MODULE_ALIAS("platform:tsc2005"); |