xref: /linux/drivers/input/touchscreen/tsc2005.c (revision e0bf6c5ca2d3281f231c5f0c9bf145e9513644de)
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/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>
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
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 
112 #define TSC2005_SPI_MAX_SPEED_HZ	10000000
113 #define TSC2005_PENUP_TIME_MS		40
114 
115 struct tsc2005_spi_rd {
116 	struct spi_transfer	spi_xfer;
117 	u32			spi_tx;
118 	u32			spi_rx;
119 };
120 
121 struct 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 
141 	spinlock_t		lock;
142 	struct timer_list	penup_timer;
143 
144 	unsigned int		esd_timeout;
145 	struct delayed_work	esd_work;
146 	unsigned long		last_valid_interrupt;
147 
148 	unsigned int		x_plate_ohm;
149 
150 	bool			opened;
151 	bool			suspended;
152 
153 	bool			pen_down;
154 
155 	struct regulator	*vio;
156 
157 	int			reset_gpio;
158 	void			(*set_reset)(bool enable);
159 };
160 
161 static int tsc2005_cmd(struct tsc2005 *ts, u8 cmd)
162 {
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 	};
169 	struct spi_message msg;
170 	int error;
171 
172 	spi_message_init(&msg);
173 	spi_message_add_tail(&xfer, &msg);
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;
183 }
184 
185 static int tsc2005_write(struct tsc2005 *ts, u8 reg, u16 value)
186 {
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 	};
193 	struct spi_message msg;
194 	int error;
195 
196 	spi_message_init(&msg);
197 	spi_message_add_tail(&xfer, &msg);
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;
208 }
209 
210 static void tsc2005_setup_read(struct tsc2005_spi_rd *rd, u8 reg, bool last)
211 {
212 	memset(rd, 0, sizeof(*rd));
213 
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 
222 static int tsc2005_read(struct tsc2005 *ts, u8 reg, u16 *value)
223 {
224 	struct tsc2005_spi_rd spi_rd;
225 	struct spi_message msg;
226 	int error;
227 
228 	tsc2005_setup_read(&spi_rd, reg, true);
229 
230 	spi_message_init(&msg);
231 	spi_message_add_tail(&spi_rd.spi_xfer, &msg);
232 
233 	error = spi_sync(ts->spi, &msg);
234 	if (error)
235 		return error;
236 
237 	*value = spi_rd.spi_rx;
238 	return 0;
239 }
240 
241 static 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);
250 			ts->pen_down = true;
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);
256 			ts->pen_down = false;
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 
264 static irqreturn_t tsc2005_irq_thread(int irq, void *_ts)
265 {
266 	struct tsc2005 *ts = _ts;
267 	unsigned long flags;
268 	unsigned int pressure;
269 	u32 x, y;
270 	u32 z1, z2;
271 	int error;
272 
273 	/* read the coordinates */
274 	error = spi_sync(ts->spi, &ts->spi_read_msg);
275 	if (unlikely(error))
276 		goto out;
277 
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 
287 	/* Skip reading if the pressure components are out of range */
288 	if (unlikely(z1 == 0 || z2 > MAX_12BIT || z1 >= z2))
289 		goto out;
290 
291        /*
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
294 	*/
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;
299 	}
300 
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 	 */
305 	ts->in_x = x;
306 	ts->in_y = y;
307 	ts->in_z1 = z1;
308 	ts->in_z2 = z2;
309 
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;
315 
316 	spin_lock_irqsave(&ts->lock, flags);
317 
318 	tsc2005_update_pen_state(ts, x, y, pressure);
319 	mod_timer(&ts->penup_timer,
320 		  jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS));
321 
322 	spin_unlock_irqrestore(&ts->lock, flags);
323 
324 	ts->last_valid_interrupt = jiffies;
325 out:
326 	return IRQ_HANDLED;
327 }
328 
329 static void tsc2005_penup_timer(unsigned long data)
330 {
331 	struct tsc2005 *ts = (struct tsc2005 *)data;
332 	unsigned long flags;
333 
334 	spin_lock_irqsave(&ts->lock, flags);
335 	tsc2005_update_pen_state(ts, 0, 0, 0);
336 	spin_unlock_irqrestore(&ts->lock, flags);
337 }
338 
339 static 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 
347 static void tsc2005_stop_scan(struct tsc2005 *ts)
348 {
349 	tsc2005_cmd(ts, TSC2005_CMD_STOP);
350 }
351 
352 static 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 
360 /* must be called with ts->mutex held */
361 static void __tsc2005_disable(struct tsc2005 *ts)
362 {
363 	tsc2005_stop_scan(ts);
364 
365 	disable_irq(ts->spi->irq);
366 	del_timer_sync(&ts->penup_timer);
367 
368 	cancel_delayed_work_sync(&ts->esd_work);
369 
370 	enable_irq(ts->spi->irq);
371 }
372 
373 /* must be called with ts->mutex held */
374 static void __tsc2005_enable(struct tsc2005 *ts)
375 {
376 	tsc2005_start_scan(ts);
377 
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)));
383 	}
384 
385 }
386 
387 static ssize_t tsc2005_selftest_show(struct device *dev,
388 				     struct device_attribute *attr,
389 				     char *buf)
390 {
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;
398 
399 	mutex_lock(&ts->mutex);
400 
401 	/*
402 	 * Test TSC2005 communications via temp high register.
403 	 */
404 	__tsc2005_disable(ts);
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 
413 	temp_high_test = (temp_high_orig - 1) & MAX_12BIT;
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 
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;
434 	}
435 
436 	/* hardware reset */
437 	tsc2005_set_reset(ts, false);
438 	usleep_range(100, 500); /* only 10us required */
439 	tsc2005_set_reset(ts, true);
440 
441 	if (!success)
442 		goto out;
443 
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;
451 	}
452 
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;
457 	}
458 
459 out:
460 	__tsc2005_enable(ts);
461 	mutex_unlock(&ts->mutex);
462 
463 	return sprintf(buf, "%d\n", success);
464 }
465 
466 static DEVICE_ATTR(selftest, S_IRUGO, tsc2005_selftest_show, NULL);
467 
468 static struct attribute *tsc2005_attrs[] = {
469 	&dev_attr_selftest.attr,
470 	NULL
471 };
472 
473 static umode_t tsc2005_attr_is_visible(struct kobject *kobj,
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);
479 	umode_t mode = attr->mode;
480 
481 	if (attr == &dev_attr_selftest.attr) {
482 		if (!ts->set_reset && !ts->reset_gpio)
483 			mode = 0;
484 	}
485 
486 	return mode;
487 }
488 
489 static const struct attribute_group tsc2005_attr_group = {
490 	.is_visible	= tsc2005_attr_is_visible,
491 	.attrs		= tsc2005_attrs,
492 };
493 
494 static void tsc2005_esd_work(struct work_struct *work)
495 {
496 	struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work.work);
497 	int error;
498 	u16 r;
499 
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 	}
508 
509 	if (time_is_after_jiffies(ts->last_valid_interrupt +
510 				  msecs_to_jiffies(ts->esd_timeout)))
511 		goto out;
512 
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;
518 	}
519 
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 
532 	tsc2005_set_reset(ts, false);
533 	usleep_range(100, 500); /* only 10us required */
534 	tsc2005_set_reset(ts, true);
535 
536 	enable_irq(ts->spi->irq);
537 	tsc2005_start_scan(ts);
538 
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)));
546 }
547 
548 static int tsc2005_open(struct input_dev *input)
549 {
550 	struct tsc2005 *ts = input_get_drvdata(input);
551 
552 	mutex_lock(&ts->mutex);
553 
554 	if (!ts->suspended)
555 		__tsc2005_enable(ts);
556 
557 	ts->opened = true;
558 
559 	mutex_unlock(&ts->mutex);
560 
561 	return 0;
562 }
563 
564 static void tsc2005_close(struct input_dev *input)
565 {
566 	struct tsc2005 *ts = input_get_drvdata(input);
567 
568 	mutex_lock(&ts->mutex);
569 
570 	if (!ts->suspended)
571 		__tsc2005_disable(ts);
572 
573 	ts->opened = false;
574 
575 	mutex_unlock(&ts->mutex);
576 }
577 
578 static void tsc2005_setup_spi_xfer(struct tsc2005 *ts)
579 {
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);
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 
592 static int tsc2005_probe(struct spi_device *spi)
593 {
594 	const struct tsc2005_platform_data *pdata = dev_get_platdata(&spi->dev);
595 	struct device_node *np = spi->dev.of_node;
596 
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;
608 
609 	if (!np && !pdata) {
610 		dev_err(&spi->dev, "no platform data\n");
611 		return -ENODEV;
612 	}
613 
614 	if (spi->irq <= 0) {
615 		dev_err(&spi->dev, "no irq\n");
616 		return -ENODEV;
617 	}
618 
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 
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;
640 
641 	error = spi_setup(spi);
642 	if (error)
643 		return error;
644 
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;
652 
653 	ts->spi = spi;
654 	ts->idev = input_dev;
655 
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 	}
687 
688 	mutex_init(&ts->mutex);
689 
690 	spin_lock_init(&ts->lock);
691 	setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts);
692 
693 	INIT_DELAYED_WORK(&ts->esd_work, tsc2005_esd_work);
694 
695 	tsc2005_setup_spi_xfer(ts);
696 
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 
711 	if (np)
712 		touchscreen_parse_of_params(input_dev);
713 
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 
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;
729 	}
730 
731 	/* enable regulator for DT */
732 	if (ts->vio) {
733 		error = regulator_enable(ts->vio);
734 		if (error)
735 			return error;
736 	}
737 
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;
744 	}
745 
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 	}
752 
753 	irq_set_irq_wake(spi->irq, 1);
754 	return 0;
755 
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;
762 }
763 
764 static int tsc2005_remove(struct spi_device *spi)
765 {
766 	struct tsc2005 *ts = spi_get_drvdata(spi);
767 
768 	sysfs_remove_group(&spi->dev.kobj, &tsc2005_attr_group);
769 
770 	if (ts->vio)
771 		regulator_disable(ts->vio);
772 
773 	return 0;
774 }
775 
776 static int __maybe_unused tsc2005_suspend(struct device *dev)
777 {
778 	struct spi_device *spi = to_spi_device(dev);
779 	struct tsc2005 *ts = spi_get_drvdata(spi);
780 
781 	mutex_lock(&ts->mutex);
782 
783 	if (!ts->suspended && ts->opened)
784 		__tsc2005_disable(ts);
785 
786 	ts->suspended = true;
787 
788 	mutex_unlock(&ts->mutex);
789 
790 	return 0;
791 }
792 
793 static int __maybe_unused tsc2005_resume(struct device *dev)
794 {
795 	struct spi_device *spi = to_spi_device(dev);
796 	struct tsc2005 *ts = spi_get_drvdata(spi);
797 
798 	mutex_lock(&ts->mutex);
799 
800 	if (ts->suspended && ts->opened)
801 		__tsc2005_enable(ts);
802 
803 	ts->suspended = false;
804 
805 	mutex_unlock(&ts->mutex);
806 
807 	return 0;
808 }
809 
810 static SIMPLE_DEV_PM_OPS(tsc2005_pm_ops, tsc2005_suspend, tsc2005_resume);
811 
812 static struct spi_driver tsc2005_driver = {
813 	.driver	= {
814 		.name	= "tsc2005",
815 		.owner	= THIS_MODULE,
816 		.pm	= &tsc2005_pm_ops,
817 	},
818 	.probe	= tsc2005_probe,
819 	.remove	= tsc2005_remove,
820 };
821 
822 module_spi_driver(tsc2005_driver);
823 
824 MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
825 MODULE_DESCRIPTION("TSC2005 Touchscreen Driver");
826 MODULE_LICENSE("GPL");
827 MODULE_ALIAS("spi:tsc2005");
828