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