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 | { |
Dmitry Torokhov | 6b007d6 | 2011-03-16 22:08:08 -0700 | [diff] [blame^] | 369 | struct spi_device *spi = to_spi_device(dev); |
| 370 | struct tsc2005 *ts = spi_get_drvdata(spi); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 371 | |
| 372 | return sprintf(buf, "%u\n", ts->disabled); |
| 373 | } |
| 374 | |
| 375 | static ssize_t tsc2005_disable_store(struct device *dev, |
| 376 | struct device_attribute *attr, |
| 377 | const char *buf, size_t count) |
| 378 | { |
Dmitry Torokhov | 6b007d6 | 2011-03-16 22:08:08 -0700 | [diff] [blame^] | 379 | struct spi_device *spi = to_spi_device(dev); |
| 380 | struct tsc2005 *ts = spi_get_drvdata(spi); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 381 | unsigned long res; |
| 382 | int i; |
| 383 | |
| 384 | if (strict_strtoul(buf, 10, &res) < 0) |
| 385 | return -EINVAL; |
| 386 | i = res ? 1 : 0; |
| 387 | |
| 388 | mutex_lock(&ts->mutex); |
| 389 | if (i == ts->disabled) |
| 390 | goto out; |
| 391 | ts->disabled = i; |
| 392 | if (i) |
| 393 | tsc2005_disable(ts); |
| 394 | else |
| 395 | tsc2005_enable(ts); |
| 396 | out: |
| 397 | mutex_unlock(&ts->mutex); |
| 398 | return count; |
| 399 | } |
| 400 | static DEVICE_ATTR(disable, 0664, tsc2005_disable_show, tsc2005_disable_store); |
| 401 | |
| 402 | static ssize_t tsc2005_selftest_show(struct device *dev, |
| 403 | struct device_attribute *attr, |
| 404 | char *buf) |
| 405 | { |
Dmitry Torokhov | 6b007d6 | 2011-03-16 22:08:08 -0700 | [diff] [blame^] | 406 | struct spi_device *spi = to_spi_device(dev); |
| 407 | struct tsc2005 *ts = spi_get_drvdata(spi); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 408 | u16 temp_high; |
| 409 | u16 temp_high_orig; |
| 410 | u16 temp_high_test; |
| 411 | unsigned int result; |
| 412 | |
| 413 | if (!ts->set_reset) { |
| 414 | dev_warn(&ts->spi->dev, |
| 415 | "unable to selftest: no reset function\n"); |
| 416 | result = 0; |
| 417 | goto out; |
| 418 | } |
| 419 | |
| 420 | mutex_lock(&ts->mutex); |
| 421 | |
| 422 | /* |
| 423 | * Test TSC2005 communications via temp high register. |
| 424 | */ |
| 425 | tsc2005_disable(ts); |
| 426 | result = 1; |
| 427 | tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high_orig); |
| 428 | temp_high_test = (temp_high_orig - 1) & MAX_12BIT; |
| 429 | tsc2005_write(ts, TSC2005_REG_TEMP_HIGH, temp_high_test); |
| 430 | tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high); |
| 431 | if (temp_high != temp_high_test) { |
| 432 | dev_warn(dev, "selftest failed: %d != %d\n", |
| 433 | temp_high, temp_high_test); |
| 434 | result = 0; |
| 435 | } |
| 436 | |
| 437 | /* hardware reset */ |
| 438 | ts->set_reset(0); |
| 439 | usleep_range(100, 500); /* only 10us required */ |
| 440 | ts->set_reset(1); |
| 441 | tsc2005_enable(ts); |
| 442 | |
| 443 | /* test that the reset really happened */ |
| 444 | tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high); |
| 445 | if (temp_high != temp_high_orig) { |
| 446 | dev_warn(dev, "selftest failed after reset: %d != %d\n", |
| 447 | temp_high, temp_high_orig); |
| 448 | result = 0; |
| 449 | } |
| 450 | |
| 451 | mutex_unlock(&ts->mutex); |
| 452 | |
| 453 | out: |
| 454 | return sprintf(buf, "%u\n", result); |
| 455 | } |
| 456 | static DEVICE_ATTR(selftest, S_IRUGO, tsc2005_selftest_show, NULL); |
| 457 | |
| 458 | static void tsc2005_esd_timer(unsigned long data) |
| 459 | { |
| 460 | struct tsc2005 *ts = (struct tsc2005 *)data; |
| 461 | |
| 462 | schedule_work(&ts->esd_work); |
| 463 | } |
| 464 | |
| 465 | static void tsc2005_esd_work(struct work_struct *work) |
| 466 | { |
| 467 | struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work); |
| 468 | u16 r; |
| 469 | |
| 470 | mutex_lock(&ts->mutex); |
| 471 | |
| 472 | if (ts->disable_depth) |
| 473 | goto out; |
| 474 | |
| 475 | /* |
| 476 | * If we cannot read our known value from configuration register 0 then |
| 477 | * reset the controller as if from power-up and start scanning again. |
| 478 | */ |
| 479 | tsc2005_read(ts, TSC2005_REG_CFR0, &r); |
| 480 | if ((r ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK) { |
| 481 | dev_info(&ts->spi->dev, "TSC2005 not responding - resetting\n"); |
| 482 | ts->set_reset(0); |
| 483 | tsc2005_update_pen_state(ts, 0, 0, 0); |
| 484 | usleep_range(100, 500); /* only 10us required */ |
| 485 | ts->set_reset(1); |
| 486 | tsc2005_start_scan(ts); |
| 487 | } |
| 488 | |
| 489 | /* re-arm the watchdog */ |
| 490 | mod_timer(&ts->esd_timer, |
| 491 | round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout))); |
| 492 | |
| 493 | out: |
| 494 | mutex_unlock(&ts->mutex); |
| 495 | } |
| 496 | |
| 497 | static void __devinit tsc2005_setup_spi_xfer(struct tsc2005 *ts) |
| 498 | { |
| 499 | tsc2005_setup_read(&ts->spi_x, TSC2005_REG_X, 0); |
| 500 | tsc2005_setup_read(&ts->spi_y, TSC2005_REG_Y, 0); |
| 501 | tsc2005_setup_read(&ts->spi_z1, TSC2005_REG_Z1, 0); |
| 502 | tsc2005_setup_read(&ts->spi_z2, TSC2005_REG_Z2, 1); |
| 503 | |
| 504 | spi_message_init(&ts->spi_read_msg); |
| 505 | spi_message_add_tail(&ts->spi_x.spi_xfer, &ts->spi_read_msg); |
| 506 | spi_message_add_tail(&ts->spi_y.spi_xfer, &ts->spi_read_msg); |
| 507 | spi_message_add_tail(&ts->spi_z1.spi_xfer, &ts->spi_read_msg); |
| 508 | spi_message_add_tail(&ts->spi_z2.spi_xfer, &ts->spi_read_msg); |
| 509 | } |
| 510 | |
| 511 | static struct attribute *tsc2005_attrs[] = { |
| 512 | &dev_attr_disable.attr, |
| 513 | &dev_attr_selftest.attr, |
| 514 | NULL |
| 515 | }; |
| 516 | |
| 517 | static struct attribute_group tsc2005_attr_group = { |
| 518 | .attrs = tsc2005_attrs, |
| 519 | }; |
| 520 | |
| 521 | static int __devinit tsc2005_setup(struct tsc2005 *ts, |
| 522 | struct tsc2005_platform_data *pdata) |
| 523 | { |
| 524 | int r; |
| 525 | int fudge_x; |
| 526 | int fudge_y; |
| 527 | int fudge_p; |
| 528 | int p_max; |
| 529 | int x_max; |
| 530 | int y_max; |
| 531 | |
| 532 | mutex_init(&ts->mutex); |
| 533 | |
| 534 | tsc2005_setup_spi_xfer(ts); |
| 535 | |
| 536 | init_timer(&ts->penup_timer); |
| 537 | setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts); |
| 538 | INIT_WORK(&ts->penup_work, tsc2005_penup_work); |
| 539 | |
| 540 | fudge_x = pdata->ts_x_fudge ? : 4; |
| 541 | fudge_y = pdata->ts_y_fudge ? : 8; |
| 542 | fudge_p = pdata->ts_pressure_fudge ? : 2; |
| 543 | x_max = pdata->ts_x_max ? : MAX_12BIT; |
| 544 | y_max = pdata->ts_y_max ? : MAX_12BIT; |
| 545 | p_max = pdata->ts_pressure_max ? : MAX_12BIT; |
| 546 | ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280; |
| 547 | ts->esd_timeout = pdata->esd_timeout_ms; |
| 548 | ts->set_reset = pdata->set_reset; |
| 549 | |
| 550 | ts->idev = input_allocate_device(); |
| 551 | if (ts->idev == NULL) |
| 552 | return -ENOMEM; |
| 553 | ts->idev->name = "TSC2005 touchscreen"; |
| 554 | snprintf(ts->phys, sizeof(ts->phys), "%s/input-ts", |
| 555 | dev_name(&ts->spi->dev)); |
| 556 | ts->idev->phys = ts->phys; |
| 557 | ts->idev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY); |
| 558 | ts->idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE); |
| 559 | ts->idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); |
| 560 | |
| 561 | input_set_abs_params(ts->idev, ABS_X, 0, x_max, fudge_x, 0); |
| 562 | input_set_abs_params(ts->idev, ABS_Y, 0, y_max, fudge_y, 0); |
| 563 | input_set_abs_params(ts->idev, ABS_PRESSURE, 0, p_max, fudge_p, 0); |
| 564 | |
| 565 | r = request_threaded_irq(ts->spi->irq, tsc2005_irq_handler, |
| 566 | tsc2005_irq_thread, IRQF_TRIGGER_RISING, |
| 567 | "tsc2005", ts); |
| 568 | if (r) { |
| 569 | dev_err(&ts->spi->dev, "request_threaded_irq(): %d\n", r); |
| 570 | goto err1; |
| 571 | } |
| 572 | set_irq_wake(ts->spi->irq, 1); |
| 573 | |
| 574 | r = input_register_device(ts->idev); |
| 575 | if (r) { |
| 576 | dev_err(&ts->spi->dev, "input_register_device(): %d\n", r); |
| 577 | goto err2; |
| 578 | } |
| 579 | |
| 580 | r = sysfs_create_group(&ts->spi->dev.kobj, &tsc2005_attr_group); |
| 581 | if (r) |
| 582 | dev_warn(&ts->spi->dev, "sysfs entry creation failed: %d\n", r); |
| 583 | |
| 584 | tsc2005_start_scan(ts); |
| 585 | |
| 586 | if (!ts->esd_timeout || !ts->set_reset) |
| 587 | goto done; |
| 588 | |
| 589 | /* start the optional ESD watchdog */ |
| 590 | setup_timer(&ts->esd_timer, tsc2005_esd_timer, (unsigned long)ts); |
| 591 | INIT_WORK(&ts->esd_work, tsc2005_esd_work); |
| 592 | mod_timer(&ts->esd_timer, |
| 593 | round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout))); |
| 594 | |
| 595 | done: |
| 596 | return 0; |
| 597 | |
| 598 | err2: |
| 599 | free_irq(ts->spi->irq, ts); |
| 600 | |
| 601 | err1: |
| 602 | input_free_device(ts->idev); |
| 603 | return r; |
| 604 | } |
| 605 | |
| 606 | static int __devinit tsc2005_probe(struct spi_device *spi) |
| 607 | { |
| 608 | struct tsc2005_platform_data *pdata = spi->dev.platform_data; |
| 609 | struct tsc2005 *ts; |
| 610 | int r; |
| 611 | |
| 612 | if (spi->irq < 0) { |
| 613 | dev_dbg(&spi->dev, "no irq\n"); |
| 614 | return -ENODEV; |
| 615 | } |
| 616 | |
| 617 | if (!pdata) { |
| 618 | dev_dbg(&spi->dev, "no platform data\n"); |
| 619 | return -ENODEV; |
| 620 | } |
| 621 | |
| 622 | ts = kzalloc(sizeof(*ts), GFP_KERNEL); |
| 623 | if (ts == NULL) |
| 624 | return -ENOMEM; |
| 625 | |
Dmitry Torokhov | 6b007d6 | 2011-03-16 22:08:08 -0700 | [diff] [blame^] | 626 | spi_set_drvdata(spi, ts); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 627 | ts->spi = spi; |
| 628 | spi->dev.power.power_state = PMSG_ON; |
| 629 | spi->mode = SPI_MODE_0; |
| 630 | spi->bits_per_word = 8; |
| 631 | if (!spi->max_speed_hz) |
| 632 | spi->max_speed_hz = TSC2005_SPI_MAX_SPEED_HZ; |
| 633 | spi_setup(spi); |
| 634 | |
| 635 | r = tsc2005_setup(ts, pdata); |
| 636 | if (r) |
| 637 | kfree(ts); |
| 638 | return r; |
| 639 | } |
| 640 | |
| 641 | static int __devexit tsc2005_remove(struct spi_device *spi) |
| 642 | { |
Dmitry Torokhov | 6b007d6 | 2011-03-16 22:08:08 -0700 | [diff] [blame^] | 643 | struct tsc2005 *ts = spi_get_drvdata(spi); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 644 | |
| 645 | mutex_lock(&ts->mutex); |
| 646 | tsc2005_disable(ts); |
| 647 | mutex_unlock(&ts->mutex); |
| 648 | |
| 649 | if (ts->esd_timeout) |
| 650 | del_timer_sync(&ts->esd_timer); |
| 651 | del_timer_sync(&ts->penup_timer); |
| 652 | |
| 653 | flush_work(&ts->esd_work); |
| 654 | flush_work(&ts->penup_work); |
| 655 | |
| 656 | sysfs_remove_group(&ts->spi->dev.kobj, &tsc2005_attr_group); |
| 657 | free_irq(ts->spi->irq, ts); |
| 658 | input_unregister_device(ts->idev); |
| 659 | kfree(ts); |
| 660 | |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | #ifdef CONFIG_PM |
| 665 | static int tsc2005_suspend(struct spi_device *spi, pm_message_t mesg) |
| 666 | { |
Dmitry Torokhov | 6b007d6 | 2011-03-16 22:08:08 -0700 | [diff] [blame^] | 667 | struct spi_device *spi = to_spi_device(dev); |
| 668 | struct tsc2005 *ts = spi_get_drvdata(spi); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 669 | |
| 670 | mutex_lock(&ts->mutex); |
| 671 | tsc2005_disable(ts); |
| 672 | mutex_unlock(&ts->mutex); |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | static int tsc2005_resume(struct spi_device *spi) |
| 678 | { |
Dmitry Torokhov | 6b007d6 | 2011-03-16 22:08:08 -0700 | [diff] [blame^] | 679 | struct spi_device *spi = to_spi_device(dev); |
| 680 | struct tsc2005 *ts = spi_get_drvdata(spi); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 681 | |
| 682 | mutex_lock(&ts->mutex); |
| 683 | tsc2005_enable(ts); |
| 684 | mutex_unlock(&ts->mutex); |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | #endif |
| 689 | |
| 690 | static struct spi_driver tsc2005_driver = { |
| 691 | .driver = { |
| 692 | .name = "tsc2005", |
| 693 | .owner = THIS_MODULE, |
| 694 | }, |
| 695 | #ifdef CONFIG_PM |
| 696 | .suspend = tsc2005_suspend, |
| 697 | .resume = tsc2005_resume, |
| 698 | #endif |
| 699 | .probe = tsc2005_probe, |
| 700 | .remove = __devexit_p(tsc2005_remove), |
| 701 | }; |
| 702 | |
| 703 | static int __init tsc2005_init(void) |
| 704 | { |
| 705 | printk(KERN_INFO "TSC2005 driver initializing\n"); |
| 706 | return spi_register_driver(&tsc2005_driver); |
| 707 | } |
| 708 | module_init(tsc2005_init); |
| 709 | |
| 710 | static void __exit tsc2005_exit(void) |
| 711 | { |
| 712 | spi_unregister_driver(&tsc2005_driver); |
| 713 | } |
| 714 | module_exit(tsc2005_exit); |
| 715 | |
| 716 | MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>"); |
| 717 | MODULE_LICENSE("GPL"); |
| 718 | MODULE_ALIAS("platform:tsc2005"); |