Commit | Line | Data |
---|---|---|
37bd4469 LL |
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> | |
3ff8ff53 | 30 | #include <linux/pm.h> |
37bd4469 LL |
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 | ||
132 | struct timer_list penup_timer; | |
133 | struct work_struct penup_work; | |
134 | ||
135 | unsigned int esd_timeout; | |
136 | struct timer_list esd_timer; | |
137 | struct work_struct esd_work; | |
138 | ||
139 | unsigned int x_plate_ohm; | |
140 | ||
141 | bool disabled; | |
142 | unsigned int disable_depth; | |
c8b6846a DT |
143 | |
144 | bool pen_down; | |
37bd4469 LL |
145 | |
146 | void (*set_reset)(bool enable); | |
147 | }; | |
148 | ||
149 | static void tsc2005_cmd(struct tsc2005 *ts, u8 cmd) | |
150 | { | |
151 | u8 tx; | |
152 | struct spi_message msg; | |
153 | struct spi_transfer xfer = { 0 }; | |
154 | ||
155 | tx = TSC2005_CMD | TSC2005_CMD_12BIT | cmd; | |
156 | ||
157 | xfer.tx_buf = &tx; | |
158 | xfer.rx_buf = NULL; | |
159 | xfer.len = 1; | |
160 | xfer.bits_per_word = 8; | |
161 | ||
162 | spi_message_init(&msg); | |
163 | spi_message_add_tail(&xfer, &msg); | |
164 | spi_sync(ts->spi, &msg); | |
165 | } | |
166 | ||
167 | static void tsc2005_write(struct tsc2005 *ts, u8 reg, u16 value) | |
168 | { | |
169 | u32 tx; | |
170 | struct spi_message msg; | |
171 | struct spi_transfer xfer = { 0 }; | |
172 | ||
173 | tx = (reg | TSC2005_REG_PND0) << 16; | |
174 | tx |= value; | |
175 | ||
176 | xfer.tx_buf = &tx; | |
177 | xfer.rx_buf = NULL; | |
178 | xfer.len = 4; | |
179 | xfer.bits_per_word = 24; | |
180 | ||
181 | spi_message_init(&msg); | |
182 | spi_message_add_tail(&xfer, &msg); | |
183 | spi_sync(ts->spi, &msg); | |
184 | } | |
185 | ||
186 | static void tsc2005_setup_read(struct tsc2005_spi_rd *rd, u8 reg, bool last) | |
187 | { | |
188 | rd->spi_tx = (reg | TSC2005_REG_READ) << 16; | |
189 | rd->spi_xfer.tx_buf = &rd->spi_tx; | |
190 | rd->spi_xfer.rx_buf = &rd->spi_rx; | |
191 | rd->spi_xfer.len = 4; | |
192 | rd->spi_xfer.bits_per_word = 24; | |
193 | rd->spi_xfer.cs_change = !last; | |
194 | } | |
195 | ||
196 | static void tsc2005_read(struct tsc2005 *ts, u8 reg, u16 *value) | |
197 | { | |
198 | struct spi_message msg; | |
199 | struct tsc2005_spi_rd spi_rd = { { 0 }, 0, 0 }; | |
200 | ||
c8b6846a | 201 | tsc2005_setup_read(&spi_rd, reg, true); |
37bd4469 LL |
202 | |
203 | spi_message_init(&msg); | |
204 | spi_message_add_tail(&spi_rd.spi_xfer, &msg); | |
205 | spi_sync(ts->spi, &msg); | |
206 | *value = spi_rd.spi_rx; | |
207 | } | |
208 | ||
209 | static void tsc2005_update_pen_state(struct tsc2005 *ts, | |
210 | int x, int y, int pressure) | |
211 | { | |
212 | if (pressure) { | |
213 | input_report_abs(ts->idev, ABS_X, x); | |
214 | input_report_abs(ts->idev, ABS_Y, y); | |
215 | input_report_abs(ts->idev, ABS_PRESSURE, pressure); | |
216 | if (!ts->pen_down) { | |
217 | input_report_key(ts->idev, BTN_TOUCH, !!pressure); | |
c8b6846a | 218 | ts->pen_down = true; |
37bd4469 LL |
219 | } |
220 | } else { | |
221 | input_report_abs(ts->idev, ABS_PRESSURE, 0); | |
222 | if (ts->pen_down) { | |
223 | input_report_key(ts->idev, BTN_TOUCH, 0); | |
c8b6846a | 224 | ts->pen_down = false; |
37bd4469 LL |
225 | } |
226 | } | |
227 | input_sync(ts->idev); | |
228 | dev_dbg(&ts->spi->dev, "point(%4d,%4d), pressure (%4d)\n", x, y, | |
229 | pressure); | |
230 | } | |
231 | ||
232 | static irqreturn_t tsc2005_irq_handler(int irq, void *dev_id) | |
233 | { | |
234 | struct tsc2005 *ts = dev_id; | |
235 | ||
236 | /* update the penup timer only if it's pending */ | |
237 | mod_timer_pending(&ts->penup_timer, | |
238 | jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS)); | |
239 | ||
240 | return IRQ_WAKE_THREAD; | |
241 | } | |
242 | ||
243 | static irqreturn_t tsc2005_irq_thread(int irq, void *_ts) | |
244 | { | |
245 | struct tsc2005 *ts = _ts; | |
246 | unsigned int pressure; | |
247 | u32 x; | |
248 | u32 y; | |
249 | u32 z1; | |
250 | u32 z2; | |
251 | ||
252 | mutex_lock(&ts->mutex); | |
253 | ||
254 | if (unlikely(ts->disable_depth)) | |
255 | goto out; | |
256 | ||
257 | /* read the coordinates */ | |
258 | spi_sync(ts->spi, &ts->spi_read_msg); | |
259 | x = ts->spi_x.spi_rx; | |
260 | y = ts->spi_y.spi_rx; | |
261 | z1 = ts->spi_z1.spi_rx; | |
262 | z2 = ts->spi_z2.spi_rx; | |
263 | ||
264 | /* validate position */ | |
265 | if (unlikely(x > MAX_12BIT || y > MAX_12BIT)) | |
266 | goto out; | |
267 | ||
268 | /* skip coords if the pressure components are out of range */ | |
269 | if (unlikely(z1 == 0 || z2 > MAX_12BIT || z1 >= z2)) | |
270 | goto out; | |
271 | ||
272 | /* skip point if this is a pen down with the exact same values as | |
273 | * the value before pen-up - that implies SPI fed us stale data | |
274 | */ | |
275 | if (!ts->pen_down && | |
276 | ts->in_x == x && | |
277 | ts->in_y == y && | |
278 | ts->in_z1 == z1 && | |
279 | ts->in_z2 == z2) | |
280 | goto out; | |
281 | ||
282 | /* At this point we are happy we have a valid and useful reading. | |
283 | * Remember it for later comparisons. We may now begin downsampling | |
284 | */ | |
285 | ts->in_x = x; | |
286 | ts->in_y = y; | |
287 | ts->in_z1 = z1; | |
288 | ts->in_z2 = z2; | |
289 | ||
290 | /* compute touch pressure resistance using equation #1 */ | |
291 | pressure = x * (z2 - z1) / z1; | |
292 | pressure = pressure * ts->x_plate_ohm / 4096; | |
293 | if (unlikely(pressure > MAX_12BIT)) | |
294 | goto out; | |
295 | ||
296 | tsc2005_update_pen_state(ts, x, y, pressure); | |
297 | ||
298 | /* set the penup timer */ | |
299 | mod_timer(&ts->penup_timer, | |
300 | jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS)); | |
301 | ||
302 | if (!ts->esd_timeout) | |
303 | goto out; | |
304 | ||
305 | /* update the watchdog timer */ | |
306 | mod_timer(&ts->esd_timer, | |
307 | round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout))); | |
308 | ||
309 | out: | |
310 | mutex_unlock(&ts->mutex); | |
311 | return IRQ_HANDLED; | |
312 | } | |
313 | ||
314 | static void tsc2005_penup_timer(unsigned long data) | |
315 | { | |
316 | struct tsc2005 *ts = (struct tsc2005 *)data; | |
317 | ||
318 | schedule_work(&ts->penup_work); | |
319 | } | |
320 | ||
321 | static void tsc2005_penup_work(struct work_struct *work) | |
322 | { | |
323 | struct tsc2005 *ts = container_of(work, struct tsc2005, penup_work); | |
324 | ||
325 | mutex_lock(&ts->mutex); | |
326 | tsc2005_update_pen_state(ts, 0, 0, 0); | |
327 | mutex_unlock(&ts->mutex); | |
328 | } | |
329 | ||
330 | static void tsc2005_start_scan(struct tsc2005 *ts) | |
331 | { | |
332 | tsc2005_write(ts, TSC2005_REG_CFR0, TSC2005_CFR0_INITVALUE); | |
333 | tsc2005_write(ts, TSC2005_REG_CFR1, TSC2005_CFR1_INITVALUE); | |
334 | tsc2005_write(ts, TSC2005_REG_CFR2, TSC2005_CFR2_INITVALUE); | |
335 | tsc2005_cmd(ts, TSC2005_CMD_NORMAL); | |
336 | } | |
337 | ||
338 | static void tsc2005_stop_scan(struct tsc2005 *ts) | |
339 | { | |
340 | tsc2005_cmd(ts, TSC2005_CMD_STOP); | |
341 | } | |
342 | ||
343 | /* must be called with mutex held */ | |
344 | static void tsc2005_disable(struct tsc2005 *ts) | |
345 | { | |
346 | if (ts->disable_depth++ != 0) | |
347 | return; | |
348 | disable_irq(ts->spi->irq); | |
349 | if (ts->esd_timeout) | |
350 | del_timer_sync(&ts->esd_timer); | |
351 | del_timer_sync(&ts->penup_timer); | |
352 | tsc2005_stop_scan(ts); | |
353 | } | |
354 | ||
355 | /* must be called with mutex held */ | |
356 | static void tsc2005_enable(struct tsc2005 *ts) | |
357 | { | |
358 | if (--ts->disable_depth != 0) | |
359 | return; | |
360 | tsc2005_start_scan(ts); | |
361 | enable_irq(ts->spi->irq); | |
362 | if (!ts->esd_timeout) | |
363 | return; | |
364 | mod_timer(&ts->esd_timer, | |
365 | round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout))); | |
366 | } | |
367 | ||
368 | static ssize_t tsc2005_disable_show(struct device *dev, | |
369 | struct device_attribute *attr, char *buf) | |
370 | { | |
6b007d62 DT |
371 | struct spi_device *spi = to_spi_device(dev); |
372 | struct tsc2005 *ts = spi_get_drvdata(spi); | |
37bd4469 LL |
373 | |
374 | return sprintf(buf, "%u\n", ts->disabled); | |
375 | } | |
376 | ||
377 | static ssize_t tsc2005_disable_store(struct device *dev, | |
378 | struct device_attribute *attr, | |
379 | const char *buf, size_t count) | |
380 | { | |
6b007d62 DT |
381 | struct spi_device *spi = to_spi_device(dev); |
382 | struct tsc2005 *ts = spi_get_drvdata(spi); | |
37bd4469 LL |
383 | unsigned long res; |
384 | int i; | |
385 | ||
386 | if (strict_strtoul(buf, 10, &res) < 0) | |
387 | return -EINVAL; | |
388 | i = res ? 1 : 0; | |
389 | ||
390 | mutex_lock(&ts->mutex); | |
391 | if (i == ts->disabled) | |
392 | goto out; | |
393 | ts->disabled = i; | |
394 | if (i) | |
395 | tsc2005_disable(ts); | |
396 | else | |
397 | tsc2005_enable(ts); | |
398 | out: | |
399 | mutex_unlock(&ts->mutex); | |
400 | return count; | |
401 | } | |
402 | static DEVICE_ATTR(disable, 0664, tsc2005_disable_show, tsc2005_disable_store); | |
403 | ||
404 | static ssize_t tsc2005_selftest_show(struct device *dev, | |
405 | struct device_attribute *attr, | |
406 | char *buf) | |
407 | { | |
6b007d62 DT |
408 | struct spi_device *spi = to_spi_device(dev); |
409 | struct tsc2005 *ts = spi_get_drvdata(spi); | |
37bd4469 LL |
410 | u16 temp_high; |
411 | u16 temp_high_orig; | |
412 | u16 temp_high_test; | |
413 | unsigned int result; | |
414 | ||
37bd4469 LL |
415 | mutex_lock(&ts->mutex); |
416 | ||
417 | /* | |
418 | * Test TSC2005 communications via temp high register. | |
419 | */ | |
420 | tsc2005_disable(ts); | |
421 | result = 1; | |
422 | tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high_orig); | |
423 | temp_high_test = (temp_high_orig - 1) & MAX_12BIT; | |
424 | tsc2005_write(ts, TSC2005_REG_TEMP_HIGH, temp_high_test); | |
425 | tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high); | |
426 | if (temp_high != temp_high_test) { | |
427 | dev_warn(dev, "selftest failed: %d != %d\n", | |
428 | temp_high, temp_high_test); | |
429 | result = 0; | |
430 | } | |
431 | ||
432 | /* hardware reset */ | |
c8b6846a | 433 | ts->set_reset(false); |
37bd4469 | 434 | usleep_range(100, 500); /* only 10us required */ |
c8b6846a | 435 | ts->set_reset(true); |
37bd4469 LL |
436 | tsc2005_enable(ts); |
437 | ||
438 | /* test that the reset really happened */ | |
439 | tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high); | |
440 | if (temp_high != temp_high_orig) { | |
441 | dev_warn(dev, "selftest failed after reset: %d != %d\n", | |
442 | temp_high, temp_high_orig); | |
443 | result = 0; | |
444 | } | |
445 | ||
446 | mutex_unlock(&ts->mutex); | |
447 | ||
37bd4469 LL |
448 | return sprintf(buf, "%u\n", result); |
449 | } | |
8dbcc432 | 450 | |
37bd4469 LL |
451 | static DEVICE_ATTR(selftest, S_IRUGO, tsc2005_selftest_show, NULL); |
452 | ||
8dbcc432 DT |
453 | static struct attribute *tsc2005_attrs[] = { |
454 | &dev_attr_disable.attr, | |
455 | &dev_attr_selftest.attr, | |
456 | NULL | |
457 | }; | |
458 | ||
459 | static mode_t tsc2005_attr_is_visible(struct kobject *kobj, | |
460 | struct attribute *attr, int n) | |
461 | { | |
462 | struct device *dev = container_of(kobj, struct device, kobj); | |
463 | struct spi_device *spi = to_spi_device(dev); | |
464 | struct tsc2005 *ts = spi_get_drvdata(spi); | |
465 | mode_t mode = attr->mode; | |
466 | ||
467 | if (attr == &dev_attr_selftest.attr) { | |
468 | if (!ts->set_reset) | |
469 | mode = 0; | |
470 | } | |
471 | ||
472 | return mode; | |
473 | } | |
474 | ||
475 | static const struct attribute_group tsc2005_attr_group = { | |
476 | .is_visible = tsc2005_attr_is_visible, | |
477 | .attrs = tsc2005_attrs, | |
478 | }; | |
479 | ||
37bd4469 LL |
480 | static void tsc2005_esd_timer(unsigned long data) |
481 | { | |
482 | struct tsc2005 *ts = (struct tsc2005 *)data; | |
483 | ||
484 | schedule_work(&ts->esd_work); | |
485 | } | |
486 | ||
487 | static void tsc2005_esd_work(struct work_struct *work) | |
488 | { | |
489 | struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work); | |
490 | u16 r; | |
491 | ||
492 | mutex_lock(&ts->mutex); | |
493 | ||
494 | if (ts->disable_depth) | |
495 | goto out; | |
496 | ||
497 | /* | |
498 | * If we cannot read our known value from configuration register 0 then | |
499 | * reset the controller as if from power-up and start scanning again. | |
500 | */ | |
501 | tsc2005_read(ts, TSC2005_REG_CFR0, &r); | |
502 | if ((r ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK) { | |
503 | dev_info(&ts->spi->dev, "TSC2005 not responding - resetting\n"); | |
c8b6846a | 504 | ts->set_reset(false); |
37bd4469 LL |
505 | tsc2005_update_pen_state(ts, 0, 0, 0); |
506 | usleep_range(100, 500); /* only 10us required */ | |
c8b6846a | 507 | ts->set_reset(true); |
37bd4469 LL |
508 | tsc2005_start_scan(ts); |
509 | } | |
510 | ||
511 | /* re-arm the watchdog */ | |
512 | mod_timer(&ts->esd_timer, | |
513 | round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout))); | |
514 | ||
515 | out: | |
516 | mutex_unlock(&ts->mutex); | |
517 | } | |
518 | ||
519 | static void __devinit tsc2005_setup_spi_xfer(struct tsc2005 *ts) | |
520 | { | |
c8b6846a DT |
521 | tsc2005_setup_read(&ts->spi_x, TSC2005_REG_X, false); |
522 | tsc2005_setup_read(&ts->spi_y, TSC2005_REG_Y, false); | |
523 | tsc2005_setup_read(&ts->spi_z1, TSC2005_REG_Z1, false); | |
524 | tsc2005_setup_read(&ts->spi_z2, TSC2005_REG_Z2, true); | |
37bd4469 LL |
525 | |
526 | spi_message_init(&ts->spi_read_msg); | |
527 | spi_message_add_tail(&ts->spi_x.spi_xfer, &ts->spi_read_msg); | |
528 | spi_message_add_tail(&ts->spi_y.spi_xfer, &ts->spi_read_msg); | |
529 | spi_message_add_tail(&ts->spi_z1.spi_xfer, &ts->spi_read_msg); | |
530 | spi_message_add_tail(&ts->spi_z2.spi_xfer, &ts->spi_read_msg); | |
531 | } | |
532 | ||
99bb892d | 533 | static int __devinit tsc2005_probe(struct spi_device *spi) |
37bd4469 | 534 | { |
99bb892d DT |
535 | const struct tsc2005_platform_data *pdata = spi->dev.platform_data; |
536 | struct tsc2005 *ts; | |
537 | struct input_dev *input_dev; | |
538 | unsigned int max_x, max_y, max_p; | |
539 | unsigned int fudge_x, fudge_y, fudge_p; | |
540 | int error; | |
37bd4469 | 541 | |
99bb892d DT |
542 | if (!pdata) { |
543 | dev_dbg(&spi->dev, "no platform data\n"); | |
544 | return -ENODEV; | |
545 | } | |
37bd4469 | 546 | |
99bb892d DT |
547 | fudge_x = pdata->ts_x_fudge ? : 4; |
548 | fudge_y = pdata->ts_y_fudge ? : 8; | |
549 | fudge_p = pdata->ts_pressure_fudge ? : 2; | |
550 | max_x = pdata->ts_x_max ? : MAX_12BIT; | |
551 | max_y = pdata->ts_y_max ? : MAX_12BIT; | |
552 | max_p = pdata->ts_pressure_max ? : MAX_12BIT; | |
37bd4469 | 553 | |
99bb892d DT |
554 | if (spi->irq <= 0) { |
555 | dev_dbg(&spi->dev, "no irq\n"); | |
556 | return -ENODEV; | |
557 | } | |
37bd4469 | 558 | |
99bb892d DT |
559 | spi->mode = SPI_MODE_0; |
560 | spi->bits_per_word = 8; | |
561 | if (!spi->max_speed_hz) | |
562 | spi->max_speed_hz = TSC2005_SPI_MAX_SPEED_HZ; | |
37bd4469 | 563 | |
99bb892d DT |
564 | error = spi_setup(spi); |
565 | if (error) | |
566 | return error; | |
37bd4469 | 567 | |
99bb892d DT |
568 | ts = kzalloc(sizeof(*ts), GFP_KERNEL); |
569 | input_dev = input_allocate_device(); | |
570 | if (!ts || !input_dev) { | |
571 | error = -ENOMEM; | |
572 | goto err_free_mem; | |
37bd4469 LL |
573 | } |
574 | ||
99bb892d DT |
575 | ts->spi = spi; |
576 | ts->idev = input_dev; | |
577 | ||
578 | ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280; | |
579 | ts->esd_timeout = pdata->esd_timeout_ms; | |
580 | ts->set_reset = pdata->set_reset; | |
37bd4469 | 581 | |
99bb892d | 582 | mutex_init(&ts->mutex); |
37bd4469 | 583 | |
99bb892d DT |
584 | setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts); |
585 | INIT_WORK(&ts->penup_work, tsc2005_penup_work); | |
37bd4469 | 586 | |
37bd4469 LL |
587 | setup_timer(&ts->esd_timer, tsc2005_esd_timer, (unsigned long)ts); |
588 | INIT_WORK(&ts->esd_work, tsc2005_esd_work); | |
37bd4469 | 589 | |
99bb892d | 590 | tsc2005_setup_spi_xfer(ts); |
37bd4469 | 591 | |
99bb892d DT |
592 | snprintf(ts->phys, sizeof(ts->phys), |
593 | "%s/input-ts", dev_name(&spi->dev)); | |
594 | ||
595 | input_dev->name = "TSC2005 touchscreen"; | |
596 | input_dev->phys = ts->phys; | |
597 | input_dev->id.bustype = BUS_SPI; | |
598 | input_dev->dev.parent = &spi->dev; | |
599 | input_dev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY); | |
600 | input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); | |
601 | ||
602 | input_set_abs_params(input_dev, ABS_X, 0, max_x, fudge_x, 0); | |
603 | input_set_abs_params(input_dev, ABS_Y, 0, max_y, fudge_y, 0); | |
604 | input_set_abs_params(input_dev, ABS_PRESSURE, 0, max_p, fudge_p, 0); | |
605 | ||
606 | error = request_threaded_irq(spi->irq, | |
607 | tsc2005_irq_handler, tsc2005_irq_thread, | |
608 | IRQF_TRIGGER_RISING, "tsc2005", ts); | |
609 | if (error) { | |
610 | dev_err(&spi->dev, "Failed to request irq, err: %d\n", error); | |
611 | goto err_free_mem; | |
37bd4469 LL |
612 | } |
613 | ||
99bb892d DT |
614 | spi_set_drvdata(spi, ts); |
615 | error = sysfs_create_group(&spi->dev.kobj, &tsc2005_attr_group); | |
616 | if (error) { | |
617 | dev_err(&spi->dev, | |
618 | "Failed to create sysfs attributes, err: %d\n", error); | |
619 | goto err_clear_drvdata; | |
37bd4469 LL |
620 | } |
621 | ||
99bb892d DT |
622 | error = input_register_device(ts->idev); |
623 | if (error) { | |
624 | dev_err(&spi->dev, | |
625 | "Failed to register input device, err: %d\n", error); | |
626 | goto err_remove_sysfs; | |
627 | } | |
37bd4469 | 628 | |
99bb892d | 629 | tsc2005_start_scan(ts); |
37bd4469 | 630 | |
99bb892d DT |
631 | if (ts->esd_timeout && ts->set_reset) { |
632 | /* start the optional ESD watchdog */ | |
633 | mod_timer(&ts->esd_timer, round_jiffies(jiffies + | |
634 | msecs_to_jiffies(ts->esd_timeout))); | |
2721a89a | 635 | } |
99bb892d DT |
636 | |
637 | set_irq_wake(spi->irq, 1); | |
638 | return 0; | |
639 | ||
640 | err_remove_sysfs: | |
641 | sysfs_remove_group(&spi->dev.kobj, &tsc2005_attr_group); | |
642 | err_clear_drvdata: | |
643 | spi_set_drvdata(spi, NULL); | |
644 | free_irq(spi->irq, ts); | |
645 | err_free_mem: | |
646 | input_free_device(input_dev); | |
647 | kfree(ts); | |
648 | return error; | |
37bd4469 LL |
649 | } |
650 | ||
651 | static int __devexit tsc2005_remove(struct spi_device *spi) | |
652 | { | |
6b007d62 | 653 | struct tsc2005 *ts = spi_get_drvdata(spi); |
37bd4469 | 654 | |
99bb892d DT |
655 | sysfs_remove_group(&ts->spi->dev.kobj, &tsc2005_attr_group); |
656 | ||
37bd4469 LL |
657 | mutex_lock(&ts->mutex); |
658 | tsc2005_disable(ts); | |
659 | mutex_unlock(&ts->mutex); | |
660 | ||
661 | if (ts->esd_timeout) | |
662 | del_timer_sync(&ts->esd_timer); | |
663 | del_timer_sync(&ts->penup_timer); | |
664 | ||
665 | flush_work(&ts->esd_work); | |
666 | flush_work(&ts->penup_work); | |
667 | ||
37bd4469 LL |
668 | free_irq(ts->spi->irq, ts); |
669 | input_unregister_device(ts->idev); | |
670 | kfree(ts); | |
671 | ||
2721a89a | 672 | spi_set_drvdata(spi, NULL); |
37bd4469 LL |
673 | return 0; |
674 | } | |
675 | ||
3ff8ff53 DT |
676 | #ifdef CONFIG_PM_SLEEP |
677 | static int tsc2005_suspend(struct device *dev) | |
37bd4469 | 678 | { |
6b007d62 DT |
679 | struct spi_device *spi = to_spi_device(dev); |
680 | struct tsc2005 *ts = spi_get_drvdata(spi); | |
37bd4469 LL |
681 | |
682 | mutex_lock(&ts->mutex); | |
683 | tsc2005_disable(ts); | |
684 | mutex_unlock(&ts->mutex); | |
685 | ||
686 | return 0; | |
687 | } | |
688 | ||
3ff8ff53 | 689 | static int tsc2005_resume(struct device *dev) |
37bd4469 | 690 | { |
6b007d62 DT |
691 | struct spi_device *spi = to_spi_device(dev); |
692 | struct tsc2005 *ts = spi_get_drvdata(spi); | |
37bd4469 LL |
693 | |
694 | mutex_lock(&ts->mutex); | |
695 | tsc2005_enable(ts); | |
696 | mutex_unlock(&ts->mutex); | |
697 | ||
698 | return 0; | |
699 | } | |
700 | #endif | |
701 | ||
3ff8ff53 DT |
702 | static SIMPLE_DEV_PM_OPS(tsc2005_pm_ops, tsc2005_suspend, tsc2005_resume); |
703 | ||
37bd4469 | 704 | static struct spi_driver tsc2005_driver = { |
3ff8ff53 DT |
705 | .driver = { |
706 | .name = "tsc2005", | |
707 | .owner = THIS_MODULE, | |
708 | .pm = &tsc2005_pm_ops, | |
37bd4469 | 709 | }, |
3ff8ff53 DT |
710 | .probe = tsc2005_probe, |
711 | .remove = __devexit_p(tsc2005_remove), | |
37bd4469 LL |
712 | }; |
713 | ||
714 | static int __init tsc2005_init(void) | |
715 | { | |
37bd4469 LL |
716 | return spi_register_driver(&tsc2005_driver); |
717 | } | |
718 | module_init(tsc2005_init); | |
719 | ||
720 | static void __exit tsc2005_exit(void) | |
721 | { | |
722 | spi_unregister_driver(&tsc2005_driver); | |
723 | } | |
724 | module_exit(tsc2005_exit); | |
725 | ||
726 | MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>"); | |
b88aa494 | 727 | MODULE_DESCRIPTION("TSC2005 Touchscreen Driver"); |
37bd4469 | 728 | MODULE_LICENSE("GPL"); |