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