1 /*
2  * Core Source for:
3  * Cypress TrueTouch(TM) Standard Product (TTSP) touchscreen drivers.
4  * For use with Cypress Txx3xx parts.
5  * Supported parts include:
6  * CY8CTST341
7  * CY8CTMA340
8  *
9  * Copyright (C) 2009, 2010, 2011 Cypress Semiconductor, Inc.
10  * Copyright (C) 2012 Javier Martinez Canillas <javier@dowhile0.org>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * version 2, and only version 2, as published by the
15  * Free Software Foundation.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25  *
26  * Contact Cypress Semiconductor at www.cypress.com <kev@cypress.com>
27  *
28  */
29 
30 #include <linux/delay.h>
31 #include <linux/input.h>
32 #include <linux/input/mt.h>
33 #include <linux/input/touchscreen.h>
34 #include <linux/gpio.h>
35 #include <linux/interrupt.h>
36 #include <linux/slab.h>
37 #include <linux/property.h>
38 #include <linux/gpio/consumer.h>
39 
40 #include "cyttsp_core.h"
41 
42 /* Bootloader number of command keys */
43 #define CY_NUM_BL_KEYS		8
44 
45 /* helpers */
46 #define GET_NUM_TOUCHES(x)		((x) & 0x0F)
47 #define IS_LARGE_AREA(x)		(((x) & 0x10) >> 4)
48 #define IS_BAD_PKT(x)			((x) & 0x20)
49 #define IS_VALID_APP(x)			((x) & 0x01)
50 #define IS_OPERATIONAL_ERR(x)		((x) & 0x3F)
51 #define GET_HSTMODE(reg)		(((reg) & 0x70) >> 4)
52 #define GET_BOOTLOADERMODE(reg)		(((reg) & 0x10) >> 4)
53 
54 #define CY_REG_BASE			0x00
55 #define CY_REG_ACT_DIST			0x1E
56 #define CY_REG_ACT_INTRVL		0x1D
57 #define CY_REG_TCH_TMOUT		(CY_REG_ACT_INTRVL + 1)
58 #define CY_REG_LP_INTRVL		(CY_REG_TCH_TMOUT + 1)
59 #define CY_MAXZ				255
60 #define CY_DELAY_DFLT			20 /* ms */
61 #define CY_DELAY_MAX			500
62 #define CY_ACT_DIST_DFLT		0xF8
63 #define CY_ACT_DIST_MASK		0x0F
64 #define CY_HNDSHK_BIT			0x80
65 /* device mode bits */
66 #define CY_OPERATE_MODE			0x00
67 #define CY_SYSINFO_MODE			0x10
68 /* power mode select bits */
69 #define CY_SOFT_RESET_MODE		0x01 /* return to Bootloader mode */
70 #define CY_DEEP_SLEEP_MODE		0x02
71 #define CY_LOW_POWER_MODE		0x04
72 
73 /* Slots management */
74 #define CY_MAX_FINGER			4
75 #define CY_MAX_ID			16
76 
77 static const u8 bl_command[] = {
78 	0x00,			/* file offset */
79 	0xFF,			/* command */
80 	0xA5,			/* exit bootloader command */
81 	0, 1, 2, 3, 4, 5, 6, 7	/* default keys */
82 };
83 
84 static int ttsp_read_block_data(struct cyttsp *ts, u8 command,
85 				u8 length, void *buf)
86 {
87 	int error;
88 	int tries;
89 
90 	for (tries = 0; tries < CY_NUM_RETRY; tries++) {
91 		error = ts->bus_ops->read(ts->dev, ts->xfer_buf, command,
92 				length, buf);
93 		if (!error)
94 			return 0;
95 
96 		msleep(CY_DELAY_DFLT);
97 	}
98 
99 	return -EIO;
100 }
101 
102 static int ttsp_write_block_data(struct cyttsp *ts, u8 command,
103 				 u8 length, void *buf)
104 {
105 	int error;
106 	int tries;
107 
108 	for (tries = 0; tries < CY_NUM_RETRY; tries++) {
109 		error = ts->bus_ops->write(ts->dev, ts->xfer_buf, command,
110 				length, buf);
111 		if (!error)
112 			return 0;
113 
114 		msleep(CY_DELAY_DFLT);
115 	}
116 
117 	return -EIO;
118 }
119 
120 static int ttsp_send_command(struct cyttsp *ts, u8 cmd)
121 {
122 	return ttsp_write_block_data(ts, CY_REG_BASE, sizeof(cmd), &cmd);
123 }
124 
125 static int cyttsp_handshake(struct cyttsp *ts)
126 {
127 	if (ts->use_hndshk)
128 		return ttsp_send_command(ts,
129 				ts->xy_data.hst_mode ^ CY_HNDSHK_BIT);
130 
131 	return 0;
132 }
133 
134 static int cyttsp_load_bl_regs(struct cyttsp *ts)
135 {
136 	memset(&ts->bl_data, 0, sizeof(ts->bl_data));
137 	ts->bl_data.bl_status = 0x10;
138 
139 	return ttsp_read_block_data(ts, CY_REG_BASE,
140 				    sizeof(ts->bl_data), &ts->bl_data);
141 }
142 
143 static int cyttsp_exit_bl_mode(struct cyttsp *ts)
144 {
145 	int error;
146 	u8 bl_cmd[sizeof(bl_command)];
147 
148 	memcpy(bl_cmd, bl_command, sizeof(bl_command));
149 	if (ts->bl_keys)
150 		memcpy(&bl_cmd[sizeof(bl_command) - CY_NUM_BL_KEYS],
151 			ts->bl_keys, CY_NUM_BL_KEYS);
152 
153 	error = ttsp_write_block_data(ts, CY_REG_BASE,
154 				      sizeof(bl_cmd), bl_cmd);
155 	if (error)
156 		return error;
157 
158 	/* wait for TTSP Device to complete the operation */
159 	msleep(CY_DELAY_DFLT);
160 
161 	error = cyttsp_load_bl_regs(ts);
162 	if (error)
163 		return error;
164 
165 	if (GET_BOOTLOADERMODE(ts->bl_data.bl_status))
166 		return -EIO;
167 
168 	return 0;
169 }
170 
171 static int cyttsp_set_operational_mode(struct cyttsp *ts)
172 {
173 	int error;
174 
175 	error = ttsp_send_command(ts, CY_OPERATE_MODE);
176 	if (error)
177 		return error;
178 
179 	/* wait for TTSP Device to complete switch to Operational mode */
180 	error = ttsp_read_block_data(ts, CY_REG_BASE,
181 				     sizeof(ts->xy_data), &ts->xy_data);
182 	if (error)
183 		return error;
184 
185 	error = cyttsp_handshake(ts);
186 	if (error)
187 		return error;
188 
189 	return ts->xy_data.act_dist == CY_ACT_DIST_DFLT ? -EIO : 0;
190 }
191 
192 static int cyttsp_set_sysinfo_mode(struct cyttsp *ts)
193 {
194 	int error;
195 
196 	memset(&ts->sysinfo_data, 0, sizeof(ts->sysinfo_data));
197 
198 	/* switch to sysinfo mode */
199 	error = ttsp_send_command(ts, CY_SYSINFO_MODE);
200 	if (error)
201 		return error;
202 
203 	/* read sysinfo registers */
204 	msleep(CY_DELAY_DFLT);
205 	error = ttsp_read_block_data(ts, CY_REG_BASE, sizeof(ts->sysinfo_data),
206 				      &ts->sysinfo_data);
207 	if (error)
208 		return error;
209 
210 	error = cyttsp_handshake(ts);
211 	if (error)
212 		return error;
213 
214 	if (!ts->sysinfo_data.tts_verh && !ts->sysinfo_data.tts_verl)
215 		return -EIO;
216 
217 	return 0;
218 }
219 
220 static int cyttsp_set_sysinfo_regs(struct cyttsp *ts)
221 {
222 	int retval = 0;
223 
224 	if (ts->act_intrvl != CY_ACT_INTRVL_DFLT ||
225 	    ts->tch_tmout != CY_TCH_TMOUT_DFLT ||
226 	    ts->lp_intrvl != CY_LP_INTRVL_DFLT) {
227 
228 		u8 intrvl_ray[] = {
229 			ts->act_intrvl,
230 			ts->tch_tmout,
231 			ts->lp_intrvl
232 		};
233 
234 		/* set intrvl registers */
235 		retval = ttsp_write_block_data(ts, CY_REG_ACT_INTRVL,
236 					sizeof(intrvl_ray), intrvl_ray);
237 		msleep(CY_DELAY_DFLT);
238 	}
239 
240 	return retval;
241 }
242 
243 static int cyttsp_soft_reset(struct cyttsp *ts)
244 {
245 	unsigned long timeout;
246 	int retval;
247 
248 	/* wait for interrupt to set ready completion */
249 	reinit_completion(&ts->bl_ready);
250 	ts->state = CY_BL_STATE;
251 
252 	enable_irq(ts->irq);
253 
254 	retval = ttsp_send_command(ts, CY_SOFT_RESET_MODE);
255 	if (retval)
256 		goto out;
257 
258 	timeout = wait_for_completion_timeout(&ts->bl_ready,
259 			msecs_to_jiffies(CY_DELAY_DFLT * CY_DELAY_MAX));
260 	retval = timeout ? 0 : -EIO;
261 
262 out:
263 	ts->state = CY_IDLE_STATE;
264 	disable_irq(ts->irq);
265 	return retval;
266 }
267 
268 static int cyttsp_act_dist_setup(struct cyttsp *ts)
269 {
270 	u8 act_dist_setup = ts->act_dist;
271 
272 	/* Init gesture; active distance setup */
273 	return ttsp_write_block_data(ts, CY_REG_ACT_DIST,
274 				sizeof(act_dist_setup), &act_dist_setup);
275 }
276 
277 static void cyttsp_extract_track_ids(struct cyttsp_xydata *xy_data, int *ids)
278 {
279 	ids[0] = xy_data->touch12_id >> 4;
280 	ids[1] = xy_data->touch12_id & 0xF;
281 	ids[2] = xy_data->touch34_id >> 4;
282 	ids[3] = xy_data->touch34_id & 0xF;
283 }
284 
285 static const struct cyttsp_tch *cyttsp_get_tch(struct cyttsp_xydata *xy_data,
286 					       int idx)
287 {
288 	switch (idx) {
289 	case 0:
290 		return &xy_data->tch1;
291 	case 1:
292 		return &xy_data->tch2;
293 	case 2:
294 		return &xy_data->tch3;
295 	case 3:
296 		return &xy_data->tch4;
297 	default:
298 		return NULL;
299 	}
300 }
301 
302 static void cyttsp_report_tchdata(struct cyttsp *ts)
303 {
304 	struct cyttsp_xydata *xy_data = &ts->xy_data;
305 	struct input_dev *input = ts->input;
306 	int num_tch = GET_NUM_TOUCHES(xy_data->tt_stat);
307 	const struct cyttsp_tch *tch;
308 	int ids[CY_MAX_ID];
309 	int i;
310 	DECLARE_BITMAP(used, CY_MAX_ID);
311 
312 	if (IS_LARGE_AREA(xy_data->tt_stat) == 1) {
313 		/* terminate all active tracks */
314 		num_tch = 0;
315 		dev_dbg(ts->dev, "%s: Large area detected\n", __func__);
316 	} else if (num_tch > CY_MAX_FINGER) {
317 		/* terminate all active tracks */
318 		num_tch = 0;
319 		dev_dbg(ts->dev, "%s: Num touch error detected\n", __func__);
320 	} else if (IS_BAD_PKT(xy_data->tt_mode)) {
321 		/* terminate all active tracks */
322 		num_tch = 0;
323 		dev_dbg(ts->dev, "%s: Invalid buffer detected\n", __func__);
324 	}
325 
326 	cyttsp_extract_track_ids(xy_data, ids);
327 
328 	bitmap_zero(used, CY_MAX_ID);
329 
330 	for (i = 0; i < num_tch; i++) {
331 		tch = cyttsp_get_tch(xy_data, i);
332 
333 		input_mt_slot(input, ids[i]);
334 		input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
335 		input_report_abs(input, ABS_MT_POSITION_X, be16_to_cpu(tch->x));
336 		input_report_abs(input, ABS_MT_POSITION_Y, be16_to_cpu(tch->y));
337 		input_report_abs(input, ABS_MT_TOUCH_MAJOR, tch->z);
338 
339 		__set_bit(ids[i], used);
340 	}
341 
342 	for (i = 0; i < CY_MAX_ID; i++) {
343 		if (test_bit(i, used))
344 			continue;
345 
346 		input_mt_slot(input, i);
347 		input_mt_report_slot_state(input, MT_TOOL_FINGER, false);
348 	}
349 
350 	input_sync(input);
351 }
352 
353 static irqreturn_t cyttsp_irq(int irq, void *handle)
354 {
355 	struct cyttsp *ts = handle;
356 	int error;
357 
358 	if (unlikely(ts->state == CY_BL_STATE)) {
359 		complete(&ts->bl_ready);
360 		goto out;
361 	}
362 
363 	/* Get touch data from CYTTSP device */
364 	error = ttsp_read_block_data(ts, CY_REG_BASE,
365 				 sizeof(struct cyttsp_xydata), &ts->xy_data);
366 	if (error)
367 		goto out;
368 
369 	/* provide flow control handshake */
370 	error = cyttsp_handshake(ts);
371 	if (error)
372 		goto out;
373 
374 	if (unlikely(ts->state == CY_IDLE_STATE))
375 		goto out;
376 
377 	if (GET_BOOTLOADERMODE(ts->xy_data.tt_mode)) {
378 		/*
379 		 * TTSP device has reset back to bootloader mode.
380 		 * Restore to operational mode.
381 		 */
382 		error = cyttsp_exit_bl_mode(ts);
383 		if (error) {
384 			dev_err(ts->dev,
385 				"Could not return to operational mode, err: %d\n",
386 				error);
387 			ts->state = CY_IDLE_STATE;
388 		}
389 	} else {
390 		cyttsp_report_tchdata(ts);
391 	}
392 
393 out:
394 	return IRQ_HANDLED;
395 }
396 
397 static int cyttsp_power_on(struct cyttsp *ts)
398 {
399 	int error;
400 
401 	error = cyttsp_soft_reset(ts);
402 	if (error)
403 		return error;
404 
405 	error = cyttsp_load_bl_regs(ts);
406 	if (error)
407 		return error;
408 
409 	if (GET_BOOTLOADERMODE(ts->bl_data.bl_status) &&
410 	    IS_VALID_APP(ts->bl_data.bl_status)) {
411 		error = cyttsp_exit_bl_mode(ts);
412 		if (error)
413 			return error;
414 	}
415 
416 	if (GET_HSTMODE(ts->bl_data.bl_file) != CY_OPERATE_MODE ||
417 	    IS_OPERATIONAL_ERR(ts->bl_data.bl_status)) {
418 		return -ENODEV;
419 	}
420 
421 	error = cyttsp_set_sysinfo_mode(ts);
422 	if (error)
423 		return error;
424 
425 	error = cyttsp_set_sysinfo_regs(ts);
426 	if (error)
427 		return error;
428 
429 	error = cyttsp_set_operational_mode(ts);
430 	if (error)
431 		return error;
432 
433 	/* init active distance */
434 	error = cyttsp_act_dist_setup(ts);
435 	if (error)
436 		return error;
437 
438 	ts->state = CY_ACTIVE_STATE;
439 
440 	return 0;
441 }
442 
443 static int cyttsp_enable(struct cyttsp *ts)
444 {
445 	int error;
446 
447 	/*
448 	 * The device firmware can wake on an I2C or SPI memory slave
449 	 * address match. So just reading a register is sufficient to
450 	 * wake up the device. The first read attempt will fail but it
451 	 * will wake it up making the second read attempt successful.
452 	 */
453 	error = ttsp_read_block_data(ts, CY_REG_BASE,
454 				     sizeof(ts->xy_data), &ts->xy_data);
455 	if (error)
456 		return error;
457 
458 	if (GET_HSTMODE(ts->xy_data.hst_mode))
459 		return -EIO;
460 
461 	enable_irq(ts->irq);
462 
463 	return 0;
464 }
465 
466 static int cyttsp_disable(struct cyttsp *ts)
467 {
468 	int error;
469 
470 	error = ttsp_send_command(ts, CY_LOW_POWER_MODE);
471 	if (error)
472 		return error;
473 
474 	disable_irq(ts->irq);
475 
476 	return 0;
477 }
478 
479 static int __maybe_unused cyttsp_suspend(struct device *dev)
480 {
481 	struct cyttsp *ts = dev_get_drvdata(dev);
482 	int retval = 0;
483 
484 	mutex_lock(&ts->input->mutex);
485 
486 	if (ts->input->users) {
487 		retval = cyttsp_disable(ts);
488 		if (retval == 0)
489 			ts->suspended = true;
490 	}
491 
492 	mutex_unlock(&ts->input->mutex);
493 
494 	return retval;
495 }
496 
497 static int __maybe_unused cyttsp_resume(struct device *dev)
498 {
499 	struct cyttsp *ts = dev_get_drvdata(dev);
500 
501 	mutex_lock(&ts->input->mutex);
502 
503 	if (ts->input->users)
504 		cyttsp_enable(ts);
505 
506 	ts->suspended = false;
507 
508 	mutex_unlock(&ts->input->mutex);
509 
510 	return 0;
511 }
512 
513 SIMPLE_DEV_PM_OPS(cyttsp_pm_ops, cyttsp_suspend, cyttsp_resume);
514 EXPORT_SYMBOL_GPL(cyttsp_pm_ops);
515 
516 static int cyttsp_open(struct input_dev *dev)
517 {
518 	struct cyttsp *ts = input_get_drvdata(dev);
519 	int retval = 0;
520 
521 	if (!ts->suspended)
522 		retval = cyttsp_enable(ts);
523 
524 	return retval;
525 }
526 
527 static void cyttsp_close(struct input_dev *dev)
528 {
529 	struct cyttsp *ts = input_get_drvdata(dev);
530 
531 	if (!ts->suspended)
532 		cyttsp_disable(ts);
533 }
534 
535 static int cyttsp_parse_properties(struct cyttsp *ts)
536 {
537 	struct device *dev = ts->dev;
538 	u32 dt_value;
539 	int ret;
540 
541 	ts->bl_keys = devm_kzalloc(dev, CY_NUM_BL_KEYS, GFP_KERNEL);
542 	if (!ts->bl_keys)
543 		return -ENOMEM;
544 
545 	/* Set some default values */
546 	ts->use_hndshk = false;
547 	ts->act_dist = CY_ACT_DIST_DFLT;
548 	ts->act_intrvl = CY_ACT_INTRVL_DFLT;
549 	ts->tch_tmout = CY_TCH_TMOUT_DFLT;
550 	ts->lp_intrvl = CY_LP_INTRVL_DFLT;
551 
552 	ret = device_property_read_u8_array(dev, "bootloader-key",
553 					    ts->bl_keys, CY_NUM_BL_KEYS);
554 	if (ret) {
555 		dev_err(dev,
556 			"bootloader-key property could not be retrieved\n");
557 		return ret;
558 	}
559 
560 	ts->use_hndshk = device_property_present(dev, "use-handshake");
561 
562 	if (!device_property_read_u32(dev, "active-distance", &dt_value)) {
563 		if (dt_value > 15) {
564 			dev_err(dev, "active-distance (%u) must be [0-15]\n",
565 				dt_value);
566 			return -EINVAL;
567 		}
568 		ts->act_dist &= ~CY_ACT_DIST_MASK;
569 		ts->act_dist |= dt_value;
570 	}
571 
572 	if (!device_property_read_u32(dev, "active-interval-ms", &dt_value)) {
573 		if (dt_value > 255) {
574 			dev_err(dev, "active-interval-ms (%u) must be [0-255]\n",
575 				dt_value);
576 			return -EINVAL;
577 		}
578 		ts->act_intrvl = dt_value;
579 	}
580 
581 	if (!device_property_read_u32(dev, "lowpower-interval-ms", &dt_value)) {
582 		if (dt_value > 2550) {
583 			dev_err(dev, "lowpower-interval-ms (%u) must be [0-2550]\n",
584 				dt_value);
585 			return -EINVAL;
586 		}
587 		/* Register value is expressed in 0.01s / bit */
588 		ts->lp_intrvl = dt_value / 10;
589 	}
590 
591 	if (!device_property_read_u32(dev, "touch-timeout-ms", &dt_value)) {
592 		if (dt_value > 2550) {
593 			dev_err(dev, "touch-timeout-ms (%u) must be [0-2550]\n",
594 				dt_value);
595 			return -EINVAL;
596 		}
597 		/* Register value is expressed in 0.01s / bit */
598 		ts->tch_tmout = dt_value / 10;
599 	}
600 
601 	return 0;
602 }
603 
604 struct cyttsp *cyttsp_probe(const struct cyttsp_bus_ops *bus_ops,
605 			    struct device *dev, int irq, size_t xfer_buf_size)
606 {
607 	struct cyttsp *ts;
608 	struct input_dev *input_dev;
609 	int error;
610 
611 	ts = devm_kzalloc(dev, sizeof(*ts) + xfer_buf_size, GFP_KERNEL);
612 	if (!ts)
613 		return ERR_PTR(-ENOMEM);
614 
615 	input_dev = devm_input_allocate_device(dev);
616 	if (!input_dev)
617 		return ERR_PTR(-ENOMEM);
618 
619 	ts->dev = dev;
620 	ts->input = input_dev;
621 	ts->bus_ops = bus_ops;
622 	ts->irq = irq;
623 
624 	ts->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
625 	if (IS_ERR(ts->reset_gpio)) {
626 		error = PTR_ERR(ts->reset_gpio);
627 		dev_err(dev, "Failed to request reset gpio, error %d\n", error);
628 		return ERR_PTR(error);
629 	}
630 
631 	error = cyttsp_parse_properties(ts);
632 	if (error)
633 		return ERR_PTR(error);
634 
635 	init_completion(&ts->bl_ready);
636 	snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev));
637 
638 	input_dev->name = "Cypress TTSP TouchScreen";
639 	input_dev->phys = ts->phys;
640 	input_dev->id.bustype = bus_ops->bustype;
641 	input_dev->dev.parent = ts->dev;
642 
643 	input_dev->open = cyttsp_open;
644 	input_dev->close = cyttsp_close;
645 
646 	input_set_drvdata(input_dev, ts);
647 
648 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X);
649 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y);
650 	touchscreen_parse_properties(input_dev, true);
651 
652 	error = input_mt_init_slots(input_dev, CY_MAX_ID, 0);
653 	if (error) {
654 		dev_err(dev, "Unable to init MT slots.\n");
655 		return ERR_PTR(error);
656 	}
657 
658 	error = devm_request_threaded_irq(dev, ts->irq, NULL, cyttsp_irq,
659 					  IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
660 					  "cyttsp", ts);
661 	if (error) {
662 		dev_err(ts->dev, "failed to request IRQ %d, err: %d\n",
663 			ts->irq, error);
664 		return ERR_PTR(error);
665 	}
666 
667 	disable_irq(ts->irq);
668 
669 	error = cyttsp_power_on(ts);
670 	if (error)
671 		return ERR_PTR(error);
672 
673 	error = input_register_device(input_dev);
674 	if (error) {
675 		dev_err(ts->dev, "failed to register input device: %d\n",
676 			error);
677 		return ERR_PTR(error);
678 	}
679 
680 	return ts;
681 }
682 EXPORT_SYMBOL_GPL(cyttsp_probe);
683 
684 MODULE_LICENSE("GPL");
685 MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard touchscreen driver core");
686 MODULE_AUTHOR("Cypress");
687