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