Linux 4.1.16
[linux/fpc-iii.git] / drivers / input / touchscreen / tsc2005.c
blob72657c5794303d6c7a2a7b251e848046ec674210
1 /*
2 * TSC2005 touchscreen driver
4 * Copyright (C) 2006-2010 Nokia Corporation
6 * Author: Lauri Leukkunen <lauri.leukkunen@nokia.com>
7 * based on TSC2301 driver by Klaus K. Pedersen <klaus.k.pedersen@nokia.com>
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.
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.
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
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/input.h>
28 #include <linux/input/touchscreen.h>
29 #include <linux/interrupt.h>
30 #include <linux/delay.h>
31 #include <linux/pm.h>
32 #include <linux/of.h>
33 #include <linux/of_gpio.h>
34 #include <linux/spi/spi.h>
35 #include <linux/spi/tsc2005.h>
36 #include <linux/regulator/consumer.h>
39 * The touchscreen interface operates as follows:
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.
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.
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
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
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)
87 /* bits common to both read and write of configuration register 0 */
88 #define TSC2005_CFR0_RW_MASK 0x3fff
90 /* configuration register 1 */
91 #define TSC2005_CFR1_BATCHDELAY_4MS 0x0003
92 #define TSC2005_CFR1_INITVALUE TSC2005_CFR1_BATCHDELAY_4MS
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)
106 #define MAX_12BIT 0xfff
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
112 #define TSC2005_SPI_MAX_SPEED_HZ 10000000
113 #define TSC2005_PENUP_TIME_MS 40
115 struct tsc2005_spi_rd {
116 struct spi_transfer spi_xfer;
117 u32 spi_tx;
118 u32 spi_rx;
121 struct tsc2005 {
122 struct spi_device *spi;
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;
130 struct input_dev *idev;
131 char phys[32];
133 struct mutex mutex;
135 /* raw copy of previous x,y,z */
136 int in_x;
137 int in_y;
138 int in_z1;
139 int in_z2;
141 spinlock_t lock;
142 struct timer_list penup_timer;
144 unsigned int esd_timeout;
145 struct delayed_work esd_work;
146 unsigned long last_valid_interrupt;
148 unsigned int x_plate_ohm;
150 bool opened;
151 bool suspended;
153 bool pen_down;
155 struct regulator *vio;
157 int reset_gpio;
158 void (*set_reset)(bool enable);
161 static int tsc2005_cmd(struct tsc2005 *ts, u8 cmd)
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,
169 struct spi_message msg;
170 int error;
172 spi_message_init(&msg);
173 spi_message_add_tail(&xfer, &msg);
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;
182 return 0;
185 static int tsc2005_write(struct tsc2005 *ts, u8 reg, u16 value)
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,
193 struct spi_message msg;
194 int error;
196 spi_message_init(&msg);
197 spi_message_add_tail(&xfer, &msg);
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;
207 return 0;
210 static void tsc2005_setup_read(struct tsc2005_spi_rd *rd, u8 reg, bool last)
212 memset(rd, 0, sizeof(*rd));
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;
222 static int tsc2005_read(struct tsc2005 *ts, u8 reg, u16 *value)
224 struct tsc2005_spi_rd spi_rd;
225 struct spi_message msg;
226 int error;
228 tsc2005_setup_read(&spi_rd, reg, true);
230 spi_message_init(&msg);
231 spi_message_add_tail(&spi_rd.spi_xfer, &msg);
233 error = spi_sync(ts->spi, &msg);
234 if (error)
235 return error;
237 *value = spi_rd.spi_rx;
238 return 0;
241 static void tsc2005_update_pen_state(struct tsc2005 *ts,
242 int x, int y, int pressure)
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);
250 ts->pen_down = true;
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);
256 ts->pen_down = false;
259 input_sync(ts->idev);
260 dev_dbg(&ts->spi->dev, "point(%4d,%4d), pressure (%4d)\n", x, y,
261 pressure);
264 static irqreturn_t tsc2005_irq_thread(int irq, void *_ts)
266 struct tsc2005 *ts = _ts;
267 unsigned long flags;
268 unsigned int pressure;
269 u32 x, y;
270 u32 z1, z2;
271 int error;
273 /* read the coordinates */
274 error = spi_sync(ts->spi, &ts->spi_read_msg);
275 if (unlikely(error))
276 goto out;
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;
283 /* validate position */
284 if (unlikely(x > MAX_12BIT || y > MAX_12BIT))
285 goto out;
287 /* Skip reading if the pressure components are out of range */
288 if (unlikely(z1 == 0 || z2 > MAX_12BIT || z1 >= z2))
289 goto out;
292 * Skip point if this is a pen down with the exact same values as
293 * the value before pen-up - that implies SPI fed us stale data
295 if (!ts->pen_down &&
296 ts->in_x == x && ts->in_y == y &&
297 ts->in_z1 == z1 && ts->in_z2 == z2) {
298 goto out;
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.
305 ts->in_x = x;
306 ts->in_y = y;
307 ts->in_z1 = z1;
308 ts->in_z2 = z2;
310 /* Compute touch pressure resistance using equation #1 */
311 pressure = x * (z2 - z1) / z1;
312 pressure = pressure * ts->x_plate_ohm / 4096;
313 if (unlikely(pressure > MAX_12BIT))
314 goto out;
316 spin_lock_irqsave(&ts->lock, flags);
318 tsc2005_update_pen_state(ts, x, y, pressure);
319 mod_timer(&ts->penup_timer,
320 jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS));
322 spin_unlock_irqrestore(&ts->lock, flags);
324 ts->last_valid_interrupt = jiffies;
325 out:
326 return IRQ_HANDLED;
329 static void tsc2005_penup_timer(unsigned long data)
331 struct tsc2005 *ts = (struct tsc2005 *)data;
332 unsigned long flags;
334 spin_lock_irqsave(&ts->lock, flags);
335 tsc2005_update_pen_state(ts, 0, 0, 0);
336 spin_unlock_irqrestore(&ts->lock, flags);
339 static void tsc2005_start_scan(struct tsc2005 *ts)
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);
347 static void tsc2005_stop_scan(struct tsc2005 *ts)
349 tsc2005_cmd(ts, TSC2005_CMD_STOP);
352 static void tsc2005_set_reset(struct tsc2005 *ts, bool enable)
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);
360 /* must be called with ts->mutex held */
361 static void __tsc2005_disable(struct tsc2005 *ts)
363 tsc2005_stop_scan(ts);
365 disable_irq(ts->spi->irq);
366 del_timer_sync(&ts->penup_timer);
368 cancel_delayed_work_sync(&ts->esd_work);
370 enable_irq(ts->spi->irq);
373 /* must be called with ts->mutex held */
374 static void __tsc2005_enable(struct tsc2005 *ts)
376 tsc2005_start_scan(ts);
378 if (ts->esd_timeout && (ts->set_reset || ts->reset_gpio)) {
379 ts->last_valid_interrupt = jiffies;
380 schedule_delayed_work(&ts->esd_work,
381 round_jiffies_relative(
382 msecs_to_jiffies(ts->esd_timeout)));
387 static ssize_t tsc2005_selftest_show(struct device *dev,
388 struct device_attribute *attr,
389 char *buf)
391 struct spi_device *spi = to_spi_device(dev);
392 struct tsc2005 *ts = spi_get_drvdata(spi);
393 u16 temp_high;
394 u16 temp_high_orig;
395 u16 temp_high_test;
396 bool success = true;
397 int error;
399 mutex_lock(&ts->mutex);
402 * Test TSC2005 communications via temp high register.
404 __tsc2005_disable(ts);
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;
413 temp_high_test = (temp_high_orig - 1) & MAX_12BIT;
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;
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;
430 if (temp_high != temp_high_test) {
431 dev_warn(dev, "selftest failed: %d != %d\n",
432 temp_high, temp_high_test);
433 success = false;
436 /* hardware reset */
437 tsc2005_set_reset(ts, false);
438 usleep_range(100, 500); /* only 10us required */
439 tsc2005_set_reset(ts, true);
441 if (!success)
442 goto out;
444 /* test that the reset really happened */
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;
453 if (temp_high != temp_high_orig) {
454 dev_warn(dev, "selftest failed after reset: %d != %d\n",
455 temp_high, temp_high_orig);
456 success = false;
459 out:
460 __tsc2005_enable(ts);
461 mutex_unlock(&ts->mutex);
463 return sprintf(buf, "%d\n", success);
466 static DEVICE_ATTR(selftest, S_IRUGO, tsc2005_selftest_show, NULL);
468 static struct attribute *tsc2005_attrs[] = {
469 &dev_attr_selftest.attr,
470 NULL
473 static umode_t tsc2005_attr_is_visible(struct kobject *kobj,
474 struct attribute *attr, int n)
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);
479 umode_t mode = attr->mode;
481 if (attr == &dev_attr_selftest.attr) {
482 if (!ts->set_reset && !ts->reset_gpio)
483 mode = 0;
486 return mode;
489 static const struct attribute_group tsc2005_attr_group = {
490 .is_visible = tsc2005_attr_is_visible,
491 .attrs = tsc2005_attrs,
494 static void tsc2005_esd_work(struct work_struct *work)
496 struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work.work);
497 int error;
498 u16 r;
500 if (!mutex_trylock(&ts->mutex)) {
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.
506 goto reschedule;
509 if (time_is_after_jiffies(ts->last_valid_interrupt +
510 msecs_to_jiffies(ts->esd_timeout)))
511 goto out;
513 /* We should be able to read register without disabling interrupts. */
514 error = tsc2005_read(ts, TSC2005_REG_CFR0, &r);
515 if (!error &&
516 !((r ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK)) {
517 goto out;
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.
525 dev_info(&ts->spi->dev, "TSC2005 not responding - resetting\n");
527 disable_irq(ts->spi->irq);
528 del_timer_sync(&ts->penup_timer);
530 tsc2005_update_pen_state(ts, 0, 0, 0);
532 tsc2005_set_reset(ts, false);
533 usleep_range(100, 500); /* only 10us required */
534 tsc2005_set_reset(ts, true);
536 enable_irq(ts->spi->irq);
537 tsc2005_start_scan(ts);
539 out:
540 mutex_unlock(&ts->mutex);
541 reschedule:
542 /* re-arm the watchdog */
543 schedule_delayed_work(&ts->esd_work,
544 round_jiffies_relative(
545 msecs_to_jiffies(ts->esd_timeout)));
548 static int tsc2005_open(struct input_dev *input)
550 struct tsc2005 *ts = input_get_drvdata(input);
552 mutex_lock(&ts->mutex);
554 if (!ts->suspended)
555 __tsc2005_enable(ts);
557 ts->opened = true;
559 mutex_unlock(&ts->mutex);
561 return 0;
564 static void tsc2005_close(struct input_dev *input)
566 struct tsc2005 *ts = input_get_drvdata(input);
568 mutex_lock(&ts->mutex);
570 if (!ts->suspended)
571 __tsc2005_disable(ts);
573 ts->opened = false;
575 mutex_unlock(&ts->mutex);
578 static void tsc2005_setup_spi_xfer(struct tsc2005 *ts)
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);
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);
592 static int tsc2005_probe(struct spi_device *spi)
594 const struct tsc2005_platform_data *pdata = dev_get_platdata(&spi->dev);
595 struct device_node *np = spi->dev.of_node;
597 struct tsc2005 *ts;
598 struct input_dev *input_dev;
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;
607 int error;
609 if (!np && !pdata) {
610 dev_err(&spi->dev, "no platform data\n");
611 return -ENODEV;
614 if (spi->irq <= 0) {
615 dev_err(&spi->dev, "no irq\n");
616 return -ENODEV;
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);
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;
641 error = spi_setup(spi);
642 if (error)
643 return error;
645 ts = devm_kzalloc(&spi->dev, sizeof(*ts), GFP_KERNEL);
646 if (!ts)
647 return -ENOMEM;
649 input_dev = devm_input_allocate_device(&spi->dev);
650 if (!input_dev)
651 return -ENOMEM;
653 ts->spi = spi;
654 ts->idev = input_dev;
656 ts->x_plate_ohm = x_plate_ohm;
657 ts->esd_timeout = esd_timeout;
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;
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;
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;
683 } else {
684 ts->reset_gpio = -1;
685 ts->set_reset = pdata->set_reset;
688 mutex_init(&ts->mutex);
690 spin_lock_init(&ts->lock);
691 setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts);
693 INIT_DELAYED_WORK(&ts->esd_work, tsc2005_esd_work);
695 tsc2005_setup_spi_xfer(ts);
697 snprintf(ts->phys, sizeof(ts->phys),
698 "%s/input-ts", dev_name(&spi->dev));
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);
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);
711 if (np)
712 touchscreen_parse_of_params(input_dev);
714 input_dev->open = tsc2005_open;
715 input_dev->close = tsc2005_close;
717 input_set_drvdata(input_dev, ts);
719 /* Ensure the touchscreen is off */
720 tsc2005_stop_scan(ts);
722 error = devm_request_threaded_irq(&spi->dev, spi->irq, NULL,
723 tsc2005_irq_thread,
724 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
725 "tsc2005", ts);
726 if (error) {
727 dev_err(&spi->dev, "Failed to request irq, err: %d\n", error);
728 return error;
731 /* enable regulator for DT */
732 if (ts->vio) {
733 error = regulator_enable(ts->vio);
734 if (error)
735 return error;
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);
743 goto disable_regulator;
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;
753 irq_set_irq_wake(spi->irq, 1);
754 return 0;
756 err_remove_sysfs:
757 sysfs_remove_group(&spi->dev.kobj, &tsc2005_attr_group);
758 disable_regulator:
759 if (ts->vio)
760 regulator_disable(ts->vio);
761 return error;
764 static int tsc2005_remove(struct spi_device *spi)
766 struct tsc2005 *ts = spi_get_drvdata(spi);
768 sysfs_remove_group(&spi->dev.kobj, &tsc2005_attr_group);
770 if (ts->vio)
771 regulator_disable(ts->vio);
773 return 0;
776 static int __maybe_unused tsc2005_suspend(struct device *dev)
778 struct spi_device *spi = to_spi_device(dev);
779 struct tsc2005 *ts = spi_get_drvdata(spi);
781 mutex_lock(&ts->mutex);
783 if (!ts->suspended && ts->opened)
784 __tsc2005_disable(ts);
786 ts->suspended = true;
788 mutex_unlock(&ts->mutex);
790 return 0;
793 static int __maybe_unused tsc2005_resume(struct device *dev)
795 struct spi_device *spi = to_spi_device(dev);
796 struct tsc2005 *ts = spi_get_drvdata(spi);
798 mutex_lock(&ts->mutex);
800 if (ts->suspended && ts->opened)
801 __tsc2005_enable(ts);
803 ts->suspended = false;
805 mutex_unlock(&ts->mutex);
807 return 0;
810 static SIMPLE_DEV_PM_OPS(tsc2005_pm_ops, tsc2005_suspend, tsc2005_resume);
812 static struct spi_driver tsc2005_driver = {
813 .driver = {
814 .name = "tsc2005",
815 .owner = THIS_MODULE,
816 .pm = &tsc2005_pm_ops,
818 .probe = tsc2005_probe,
819 .remove = tsc2005_remove,
822 module_spi_driver(tsc2005_driver);
824 MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
825 MODULE_DESCRIPTION("TSC2005 Touchscreen Driver");
826 MODULE_LICENSE("GPL");
827 MODULE_ALIAS("spi:tsc2005");