xref: /linux/drivers/input/touchscreen/cyttsp5.c (revision 29e31a8ee811f5d85274f0381f13cd6fe650aea4)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Parade TrueTouch(TM) Standard Product V5 Module.
4  *
5  * Copyright (C) 2015 Parade Technologies
6  * Copyright (C) 2012-2015 Cypress Semiconductor
7  * Copyright (C) 2018 Bootlin
8  *
9  * Authors: Mylène Josserand <mylene.josserand@bootlin.com>
10  *                Alistair Francis <alistair@alistair23.me>
11  */
12 
13 #include <linux/crc-itu-t.h>
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/input/mt.h>
18 #include <linux/input/touchscreen.h>
19 #include <linux/interrupt.h>
20 #include <linux/i2c.h>
21 #include <linux/module.h>
22 #include <linux/of_device.h>
23 #include <linux/regmap.h>
24 #include <asm/unaligned.h>
25 
26 #define CYTTSP5_NAME				"cyttsp5"
27 #define CY_I2C_DATA_SIZE			(2 * 256)
28 #define HID_VERSION				0x0100
29 #define CY_MAX_INPUT				512
30 #define CYTTSP5_PREALLOCATED_CMD_BUFFER		32
31 #define CY_BITS_PER_BTN				1
32 #define CY_NUM_BTN_EVENT_ID			GENMASK(CY_BITS_PER_BTN - 1, 0)
33 
34 #define MAX_AREA				255
35 #define HID_OUTPUT_BL_SOP			0x1
36 #define HID_OUTPUT_BL_EOP			0x17
37 #define HID_OUTPUT_BL_LAUNCH_APP		0x3B
38 #define HID_OUTPUT_BL_LAUNCH_APP_SIZE		11
39 #define HID_OUTPUT_GET_SYSINFO			0x2
40 #define HID_OUTPUT_GET_SYSINFO_SIZE		5
41 #define HID_OUTPUT_MAX_CMD_SIZE			12
42 
43 #define HID_DESC_REG				0x1
44 #define HID_INPUT_REG				0x3
45 #define HID_OUTPUT_REG				0x4
46 
47 #define REPORT_ID_TOUCH				0x1
48 #define REPORT_ID_BTN				0x3
49 #define REPORT_SIZE_5				5
50 #define REPORT_SIZE_8				8
51 #define REPORT_SIZE_16				16
52 
53 /* Touch reports offsets */
54 /* Header offsets */
55 #define TOUCH_REPORT_DESC_HDR_CONTACTCOUNT	16
56 /* Record offsets */
57 #define TOUCH_REPORT_DESC_CONTACTID		8
58 #define TOUCH_REPORT_DESC_X			16
59 #define TOUCH_REPORT_DESC_Y			32
60 #define TOUCH_REPORT_DESC_P			48
61 #define TOUCH_REPORT_DESC_MAJ			56
62 #define TOUCH_REPORT_DESC_MIN			64
63 
64 /* HID */
65 #define HID_TOUCH_REPORT_ID			0x1
66 #define HID_BTN_REPORT_ID			0x3
67 #define HID_APP_RESPONSE_REPORT_ID		0x1F
68 #define HID_APP_OUTPUT_REPORT_ID		0x2F
69 #define HID_BL_RESPONSE_REPORT_ID		0x30
70 #define HID_BL_OUTPUT_REPORT_ID			0x40
71 
72 #define HID_OUTPUT_RESPONSE_REPORT_OFFSET	2
73 #define HID_OUTPUT_RESPONSE_CMD_OFFSET		4
74 #define HID_OUTPUT_RESPONSE_CMD_MASK		GENMASK(6, 0)
75 
76 #define HID_SYSINFO_SENSING_OFFSET		33
77 #define HID_SYSINFO_BTN_OFFSET			48
78 #define HID_SYSINFO_BTN_MASK			GENMASK(7, 0)
79 #define HID_SYSINFO_MAX_BTN			8
80 
81 #define CY_HID_OUTPUT_TIMEOUT_MS		200
82 #define CY_HID_OUTPUT_GET_SYSINFO_TIMEOUT_MS	3000
83 #define CY_HID_GET_HID_DESCRIPTOR_TIMEOUT_MS	4000
84 
85 /* maximum number of concurrent tracks */
86 #define TOUCH_REPORT_SIZE			10
87 #define TOUCH_INPUT_HEADER_SIZE			7
88 #define BTN_REPORT_SIZE				9
89 #define BTN_INPUT_HEADER_SIZE			5
90 
91 #define MAX_CY_TCH_T_IDS			32
92 
93 /* All usage pages for Touch Report */
94 #define TOUCH_REPORT_USAGE_PG_X			0x00010030
95 #define TOUCH_REPORT_USAGE_PG_Y			0x00010031
96 #define TOUCH_REPORT_USAGE_PG_P			0x000D0030
97 #define TOUCH_REPORT_USAGE_PG_CONTACTID		0x000D0051
98 #define TOUCH_REPORT_USAGE_PG_CONTACTCOUNT	0x000D0054
99 #define TOUCH_REPORT_USAGE_PG_MAJ		0xFF010062
100 #define TOUCH_REPORT_USAGE_PG_MIN		0xFF010063
101 #define TOUCH_COL_USAGE_PG			0x000D0022
102 
103 /* System Information interface definitions */
104 struct cyttsp5_sensing_conf_data_dev {
105 	u8 electrodes_x;
106 	u8 electrodes_y;
107 	__le16 len_x;
108 	__le16 len_y;
109 	__le16 res_x;
110 	__le16 res_y;
111 	__le16 max_z;
112 	u8 origin_x;
113 	u8 origin_y;
114 	u8 panel_id;
115 	u8 btn;
116 	u8 scan_mode;
117 	u8 max_num_of_tch_per_refresh_cycle;
118 } __packed;
119 
120 struct cyttsp5_sensing_conf_data {
121 	u16 res_x;
122 	u16 res_y;
123 	u16 max_z;
124 	u16 len_x;
125 	u16 len_y;
126 	u8 origin_x;
127 	u8 origin_y;
128 	u8 max_tch;
129 };
130 
131 enum cyttsp5_tch_abs {	/* for ordering within the extracted touch data array */
132 	CY_TCH_X,	/* X */
133 	CY_TCH_Y,	/* Y */
134 	CY_TCH_P,	/* P (Z) */
135 	CY_TCH_T,	/* TOUCH ID */
136 	CY_TCH_MAJ,	/* TOUCH_MAJOR */
137 	CY_TCH_MIN,	/* TOUCH_MINOR */
138 	CY_TCH_NUM_ABS
139 };
140 
141 struct cyttsp5_tch_abs_params {
142 	size_t ofs;	/* abs byte offset */
143 	size_t size;	/* size in bits */
144 	size_t min;	/* min value */
145 	size_t max;	/* max value */
146 	size_t bofs;	/* bit offset */
147 };
148 
149 struct cyttsp5_touch {
150 	int abs[CY_TCH_NUM_ABS];
151 };
152 
153 struct cyttsp5_sysinfo {
154 	struct cyttsp5_sensing_conf_data sensing_conf_data;
155 	int num_btns;
156 	struct cyttsp5_tch_abs_params tch_hdr;
157 	struct cyttsp5_tch_abs_params tch_abs[CY_TCH_NUM_ABS];
158 	u32 key_code[HID_SYSINFO_MAX_BTN];
159 };
160 
161 struct cyttsp5_hid_desc {
162 	__le16 hid_desc_len;
163 	u8 packet_id;
164 	u8 reserved_byte;
165 	__le16 bcd_version;
166 	__le16 report_desc_len;
167 	__le16 report_desc_register;
168 	__le16 input_register;
169 	__le16 max_input_len;
170 	__le16 output_register;
171 	__le16 max_output_len;
172 	__le16 command_register;
173 	__le16 data_register;
174 	__le16 vendor_id;
175 	__le16 product_id;
176 	__le16 version_id;
177 	u8 reserved[4];
178 } __packed;
179 
180 struct cyttsp5 {
181 	struct device *dev;
182 	struct completion cmd_done;
183 	struct cyttsp5_sysinfo sysinfo;
184 	struct cyttsp5_hid_desc hid_desc;
185 	u8 cmd_buf[CYTTSP5_PREALLOCATED_CMD_BUFFER];
186 	u8 input_buf[CY_MAX_INPUT];
187 	u8 response_buf[CY_MAX_INPUT];
188 	struct gpio_desc *reset_gpio;
189 	struct input_dev *input;
190 	char phys[NAME_MAX];
191 	int num_prv_rec;
192 	struct regmap *regmap;
193 	struct touchscreen_properties prop;
194 	struct regulator *vdd;
195 };
196 
197 /*
198  * For what is understood in the datasheet, the register does not
199  * matter. For consistency, use the Input Register address
200  * but it does mean anything to the device. The important data
201  * to send is the I2C address
202  */
203 static int cyttsp5_read(struct cyttsp5 *ts, u8 *buf, u32 max)
204 {
205 	int error;
206 	u32 size;
207 	u8 temp[2];
208 
209 	/* Read the frame to retrieve the size */
210 	error = regmap_bulk_read(ts->regmap, HID_INPUT_REG, temp, sizeof(temp));
211 	if (error)
212 		return error;
213 
214 	size = get_unaligned_le16(temp);
215 	if (!size || size == 2)
216 		return 0;
217 
218 	if (size > max)
219 		return -EINVAL;
220 
221 	/* Get the real value */
222 	return regmap_bulk_read(ts->regmap, HID_INPUT_REG, buf, size);
223 }
224 
225 static int cyttsp5_write(struct cyttsp5 *ts, unsigned int reg, u8 *data,
226 			 size_t size)
227 {
228 	u8 cmd[HID_OUTPUT_MAX_CMD_SIZE];
229 
230 	if (size + 1 > HID_OUTPUT_MAX_CMD_SIZE)
231 		return -E2BIG;
232 
233 	/* High bytes of register address needed as first byte of cmd */
234 	cmd[0] = (reg >> 8) & 0xFF;
235 
236 	/* Copy the rest of the data */
237 	if (data)
238 		memcpy(&cmd[1], data, size);
239 
240 	/*
241 	 * The hardware wants to receive a frame with the address register
242 	 * contained in the first two bytes. As the regmap_write function
243 	 * add the register adresse in the frame, we use the low byte as
244 	 * first frame byte for the address register and the first
245 	 * data byte is the high register + left of the cmd to send
246 	 */
247 	return regmap_bulk_write(ts->regmap, reg & 0xFF, cmd, size + 1);
248 }
249 
250 static void cyttsp5_get_touch_axis(int *axis, int size, int max, u8 *xy_data,
251 				   int bofs)
252 {
253 	int nbyte;
254 
255 	for (nbyte = 0, *axis = 0; nbyte < size; nbyte++)
256 		*axis += ((xy_data[nbyte] >> bofs) << (nbyte * 8));
257 
258 	*axis &= max - 1;
259 }
260 
261 static void cyttsp5_get_touch_record(struct cyttsp5 *ts,
262 				     struct cyttsp5_touch *touch, u8 *xy_data)
263 {
264 	struct cyttsp5_sysinfo *si = &ts->sysinfo;
265 	enum cyttsp5_tch_abs abs;
266 
267 	for (abs = CY_TCH_X; abs < CY_TCH_NUM_ABS; abs++)
268 		cyttsp5_get_touch_axis(&touch->abs[abs],
269 				       si->tch_abs[abs].size,
270 				       si->tch_abs[abs].max,
271 				       xy_data + si->tch_abs[abs].ofs,
272 				       si->tch_abs[abs].bofs);
273 }
274 
275 static void cyttsp5_get_mt_touches(struct cyttsp5 *ts,
276 				   struct cyttsp5_touch *tch, int num_cur_tch)
277 {
278 	struct cyttsp5_sysinfo *si = &ts->sysinfo;
279 	int i, t = 0, offset = 0;
280 	DECLARE_BITMAP(ids, MAX_CY_TCH_T_IDS);
281 	u8 *tch_addr;
282 	int tmp;
283 
284 	bitmap_zero(ids, MAX_CY_TCH_T_IDS);
285 	memset(tch->abs, 0, sizeof(tch->abs));
286 
287 	switch (ts->input_buf[2]) {
288 	case HID_TOUCH_REPORT_ID:
289 		offset = TOUCH_INPUT_HEADER_SIZE;
290 		break;
291 	case HID_BTN_REPORT_ID:
292 		offset = BTN_INPUT_HEADER_SIZE;
293 		break;
294 	}
295 
296 	for (i = 0; i < num_cur_tch; i++) {
297 		tch_addr = ts->input_buf + offset + (i * TOUCH_REPORT_SIZE);
298 		cyttsp5_get_touch_record(ts, tch, tch_addr);
299 
300 		/* Convert MAJOR/MINOR from mm to resolution */
301 		tmp = tch->abs[CY_TCH_MAJ] * 100 * si->sensing_conf_data.res_x;
302 		tch->abs[CY_TCH_MAJ] = tmp / si->sensing_conf_data.len_x;
303 		tmp = tch->abs[CY_TCH_MIN] * 100 * si->sensing_conf_data.res_x;
304 		tch->abs[CY_TCH_MIN] = tmp / si->sensing_conf_data.len_x;
305 
306 		t = tch->abs[CY_TCH_T];
307 		input_mt_slot(ts->input, t);
308 		input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
309 		__set_bit(t, ids);
310 
311 		/* position and pressure fields */
312 		touchscreen_report_pos(ts->input, &ts->prop,
313 				       tch->abs[CY_TCH_X], tch->abs[CY_TCH_Y],
314 				       true);
315 		input_report_abs(ts->input, ABS_MT_PRESSURE,
316 				 tch->abs[CY_TCH_P]);
317 
318 		/* Get the extended touch fields */
319 		input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR,
320 				 tch->abs[CY_TCH_MAJ]);
321 		input_report_abs(ts->input, ABS_MT_TOUCH_MINOR,
322 				 tch->abs[CY_TCH_MIN]);
323 	}
324 
325 	ts->num_prv_rec = num_cur_tch;
326 }
327 
328 static int cyttsp5_mt_attention(struct device *dev)
329 {
330 	struct cyttsp5 *ts = dev_get_drvdata(dev);
331 	struct cyttsp5_sysinfo *si = &ts->sysinfo;
332 	int max_tch = si->sensing_conf_data.max_tch;
333 	struct cyttsp5_touch tch;
334 	int num_cur_tch;
335 
336 	cyttsp5_get_touch_axis(&num_cur_tch, si->tch_hdr.size,
337 			       si->tch_hdr.max,
338 			       ts->input_buf + 3 + si->tch_hdr.ofs,
339 			       si->tch_hdr.bofs);
340 
341 	if (num_cur_tch > max_tch) {
342 		dev_err(dev, "Num touch err detected (n=%d)\n", num_cur_tch);
343 		num_cur_tch = max_tch;
344 	}
345 
346 	if (num_cur_tch == 0 && ts->num_prv_rec == 0)
347 		return 0;
348 
349 	/* extract xy_data for all currently reported touches */
350 	if (num_cur_tch)
351 		cyttsp5_get_mt_touches(ts, &tch, num_cur_tch);
352 
353 	input_mt_sync_frame(ts->input);
354 	input_sync(ts->input);
355 
356 	return 0;
357 }
358 
359 static int cyttsp5_setup_input_device(struct device *dev)
360 {
361 	struct cyttsp5 *ts = dev_get_drvdata(dev);
362 	struct cyttsp5_sysinfo *si = &ts->sysinfo;
363 	int max_x, max_y, max_p;
364 	int max_x_tmp, max_y_tmp;
365 	int error;
366 
367 	max_x_tmp = si->sensing_conf_data.res_x;
368 	max_y_tmp = si->sensing_conf_data.res_y;
369 	max_x = max_x_tmp - 1;
370 	max_y = max_y_tmp - 1;
371 	max_p = si->sensing_conf_data.max_z;
372 
373 	input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, max_x, 0, 0);
374 	input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, max_y, 0, 0);
375 	input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, max_p, 0, 0);
376 
377 	input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, MAX_AREA, 0, 0);
378 	input_set_abs_params(ts->input, ABS_MT_TOUCH_MINOR, 0, MAX_AREA, 0, 0);
379 
380 	error = input_mt_init_slots(ts->input, si->tch_abs[CY_TCH_T].max,
381 				    INPUT_MT_DROP_UNUSED | INPUT_MT_DIRECT);
382 	if (error)
383 		return error;
384 
385 	error = input_register_device(ts->input);
386 	if (error) {
387 		dev_err(dev, "failed to register input device: %d\n", error);
388 		return error;
389 	}
390 
391 	return error;
392 }
393 
394 static int cyttsp5_parse_dt_key_code(struct device *dev)
395 {
396 	struct cyttsp5 *ts = dev_get_drvdata(dev);
397 	struct cyttsp5_sysinfo *si = &ts->sysinfo;
398 
399 	if (!si->num_btns)
400 		return 0;
401 
402 	/* Initialize the button to RESERVED */
403 	memset32(si->key_code, KEY_RESERVED,  si->num_btns);
404 
405 	return device_property_read_u32_array(dev, "linux,keycodes",
406 					      si->key_code, si->num_btns);
407 }
408 
409 static int cyttsp5_btn_attention(struct device *dev)
410 {
411 	struct cyttsp5 *ts = dev_get_drvdata(dev);
412 	struct cyttsp5_sysinfo *si = &ts->sysinfo;
413 	int cur_btn, offset = 0;
414 	int cur_btn_state;
415 
416 	switch (ts->input_buf[2]) {
417 	case HID_TOUCH_REPORT_ID:
418 		offset = TOUCH_INPUT_HEADER_SIZE;
419 		break;
420 	case HID_BTN_REPORT_ID:
421 		offset = BTN_INPUT_HEADER_SIZE;
422 		break;
423 	}
424 
425 	if (ts->input_buf[2] != HID_BTN_REPORT_ID)
426 		return 0;
427 
428 	/* extract button press/release touch information */
429 	for (cur_btn = 0; cur_btn < si->num_btns; cur_btn++) {
430 		/* Get current button state */
431 		cur_btn_state = (ts->input_buf[offset] >> (cur_btn * CY_BITS_PER_BTN))
432 				& CY_NUM_BTN_EVENT_ID;
433 
434 		input_report_key(ts->input, si->key_code[cur_btn],
435 				 cur_btn_state);
436 		input_sync(ts->input);
437 	}
438 
439 	return 0;
440 }
441 
442 static int cyttsp5_validate_cmd_response(struct cyttsp5 *ts, u8 code)
443 {
444 	u16 size, crc;
445 	u8 status, report_id;
446 	int command_code;
447 
448 	size = get_unaligned_le16(&ts->response_buf[0]);
449 	if (!size)
450 		return 0;
451 
452 	report_id = ts->response_buf[HID_OUTPUT_RESPONSE_REPORT_OFFSET];
453 
454 	switch (report_id) {
455 	case HID_BL_RESPONSE_REPORT_ID:
456 		if (ts->response_buf[4] != HID_OUTPUT_BL_SOP) {
457 			dev_err(ts->dev, "HID output response, wrong SOP\n");
458 			return -EPROTO;
459 		}
460 
461 		if (ts->response_buf[size - 1] != HID_OUTPUT_BL_EOP) {
462 			dev_err(ts->dev, "HID output response, wrong EOP\n");
463 			return -EPROTO;
464 		}
465 
466 		crc = crc_itu_t(0xFFFF, &ts->response_buf[4], size - 7);
467 		if (get_unaligned_le16(&ts->response_buf[size - 3]) != crc) {
468 			dev_err(ts->dev,
469 				"HID output response, wrong CRC 0x%X\n",
470 				crc);
471 			return -EPROTO;
472 		}
473 
474 		status = ts->response_buf[5];
475 		if (status) {
476 			dev_err(ts->dev, "HID output response, ERROR:%d\n",
477 				status);
478 			return -EPROTO;
479 		}
480 		break;
481 
482 	case HID_APP_RESPONSE_REPORT_ID:
483 		command_code = ts->response_buf[HID_OUTPUT_RESPONSE_CMD_OFFSET]
484 			& HID_OUTPUT_RESPONSE_CMD_MASK;
485 		if (command_code != code) {
486 			dev_err(ts->dev,
487 				"HID output response, wrong command_code:%X\n",
488 				command_code);
489 			return -EPROTO;
490 		}
491 		break;
492 	}
493 
494 	return 0;
495 }
496 
497 static void cyttsp5_si_get_btn_data(struct cyttsp5 *ts)
498 {
499 	struct cyttsp5_sysinfo *si = &ts->sysinfo;
500 	unsigned int btns = ts->response_buf[HID_SYSINFO_BTN_OFFSET] &
501 				HID_SYSINFO_BTN_MASK;
502 
503 	si->num_btns = hweight8(btns);
504 }
505 
506 static int cyttsp5_get_sysinfo_regs(struct cyttsp5 *ts)
507 {
508 	struct cyttsp5_sensing_conf_data *scd = &ts->sysinfo.sensing_conf_data;
509 	struct cyttsp5_sensing_conf_data_dev *scd_dev =
510 		(struct cyttsp5_sensing_conf_data_dev *)
511 		&ts->response_buf[HID_SYSINFO_SENSING_OFFSET];
512 
513 	cyttsp5_si_get_btn_data(ts);
514 
515 	scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle;
516 	scd->res_x = get_unaligned_le16(&scd_dev->res_x);
517 	scd->res_y = get_unaligned_le16(&scd_dev->res_y);
518 	scd->max_z = get_unaligned_le16(&scd_dev->max_z);
519 	scd->len_x = get_unaligned_le16(&scd_dev->len_x);
520 	scd->len_y = get_unaligned_le16(&scd_dev->len_y);
521 
522 	return 0;
523 }
524 
525 static int cyttsp5_hid_output_get_sysinfo(struct cyttsp5 *ts)
526 {
527 	int rc;
528 	u8 cmd[HID_OUTPUT_GET_SYSINFO_SIZE];
529 
530 	/* HI bytes of Output register address */
531 	put_unaligned_le16(HID_OUTPUT_GET_SYSINFO_SIZE, cmd);
532 	cmd[2] = HID_APP_OUTPUT_REPORT_ID;
533 	cmd[3] = 0x0; /* Reserved */
534 	cmd[4] = HID_OUTPUT_GET_SYSINFO;
535 
536 	rc = cyttsp5_write(ts, HID_OUTPUT_REG, cmd,
537 			   HID_OUTPUT_GET_SYSINFO_SIZE);
538 	if (rc) {
539 		dev_err(ts->dev, "Failed to write command %d", rc);
540 		return rc;
541 	}
542 
543 	rc = wait_for_completion_interruptible_timeout(&ts->cmd_done,
544 						msecs_to_jiffies(CY_HID_OUTPUT_GET_SYSINFO_TIMEOUT_MS));
545 	if (rc <= 0) {
546 		dev_err(ts->dev, "HID output cmd execution timed out\n");
547 		rc = -ETIMEDOUT;
548 		return rc;
549 	}
550 
551 	rc = cyttsp5_validate_cmd_response(ts, HID_OUTPUT_GET_SYSINFO);
552 	if (rc) {
553 		dev_err(ts->dev, "Validation of the response failed\n");
554 		return rc;
555 	}
556 
557 	return cyttsp5_get_sysinfo_regs(ts);
558 }
559 
560 static int cyttsp5_hid_output_bl_launch_app(struct cyttsp5 *ts)
561 {
562 	int rc;
563 	u8 cmd[HID_OUTPUT_BL_LAUNCH_APP];
564 	u16 crc;
565 
566 	put_unaligned_le16(HID_OUTPUT_BL_LAUNCH_APP_SIZE, cmd);
567 	cmd[2] = HID_BL_OUTPUT_REPORT_ID;
568 	cmd[3] = 0x0; /* Reserved */
569 	cmd[4] = HID_OUTPUT_BL_SOP;
570 	cmd[5] = HID_OUTPUT_BL_LAUNCH_APP;
571 	put_unaligned_le16(0x00, &cmd[6]);
572 	crc = crc_itu_t(0xFFFF, &cmd[4], 4);
573 	put_unaligned_le16(crc, &cmd[8]);
574 	cmd[10] = HID_OUTPUT_BL_EOP;
575 
576 	rc = cyttsp5_write(ts, HID_OUTPUT_REG, cmd,
577 			   HID_OUTPUT_BL_LAUNCH_APP_SIZE);
578 	if (rc) {
579 		dev_err(ts->dev, "Failed to write command %d", rc);
580 		return rc;
581 	}
582 
583 	rc = wait_for_completion_interruptible_timeout(&ts->cmd_done,
584 				msecs_to_jiffies(CY_HID_OUTPUT_TIMEOUT_MS));
585 	if (rc <= 0) {
586 		dev_err(ts->dev, "HID output cmd execution timed out\n");
587 		rc = -ETIMEDOUT;
588 		return rc;
589 	}
590 
591 	rc = cyttsp5_validate_cmd_response(ts, HID_OUTPUT_BL_LAUNCH_APP);
592 	if (rc) {
593 		dev_err(ts->dev, "Validation of the response failed\n");
594 		return rc;
595 	}
596 
597 	return 0;
598 }
599 
600 static int cyttsp5_get_hid_descriptor(struct cyttsp5 *ts,
601 				      struct cyttsp5_hid_desc *desc)
602 {
603 	struct device *dev = ts->dev;
604 	__le16 hid_desc_register = cpu_to_le16(HID_DESC_REG);
605 	int rc;
606 	u8 cmd[2];
607 
608 	/* Set HID descriptor register */
609 	memcpy(cmd, &hid_desc_register, sizeof(hid_desc_register));
610 
611 	rc = cyttsp5_write(ts, HID_DESC_REG, NULL, 0);
612 	if (rc) {
613 		dev_err(dev, "Failed to get HID descriptor, rc=%d\n", rc);
614 		return rc;
615 	}
616 
617 	rc = wait_for_completion_interruptible_timeout(&ts->cmd_done,
618 			msecs_to_jiffies(CY_HID_GET_HID_DESCRIPTOR_TIMEOUT_MS));
619 	if (rc <= 0) {
620 		dev_err(ts->dev, "HID get descriptor timed out\n");
621 		rc = -ETIMEDOUT;
622 		return rc;
623 	}
624 
625 	memcpy(desc, ts->response_buf, sizeof(*desc));
626 
627 	/* Check HID descriptor length and version */
628 	if (le16_to_cpu(desc->hid_desc_len) != sizeof(*desc) ||
629 	    le16_to_cpu(desc->bcd_version) != HID_VERSION) {
630 		dev_err(dev, "Unsupported HID version\n");
631 		return -ENODEV;
632 	}
633 
634 	return 0;
635 }
636 
637 static int fill_tch_abs(struct cyttsp5_tch_abs_params *tch_abs, int report_size,
638 			int offset)
639 {
640 	tch_abs->ofs = offset / 8;
641 	tch_abs->size = report_size / 8;
642 	if (report_size % 8)
643 		tch_abs->size += 1;
644 	tch_abs->min = 0;
645 	tch_abs->max = 1 << report_size;
646 	tch_abs->bofs = offset - (tch_abs->ofs << 3);
647 
648 	return 0;
649 }
650 
651 static irqreturn_t cyttsp5_handle_irq(int irq, void *handle)
652 {
653 	struct cyttsp5 *ts = handle;
654 	int report_id;
655 	int size;
656 	int error;
657 
658 	error = cyttsp5_read(ts, ts->input_buf, CY_MAX_INPUT);
659 	if (error)
660 		return IRQ_HANDLED;
661 
662 	size = get_unaligned_le16(&ts->input_buf[0]);
663 	if (size == 0) {
664 		/* reset */
665 		report_id = 0;
666 		size = 2;
667 	} else {
668 		report_id = ts->input_buf[2];
669 	}
670 
671 	switch (report_id) {
672 	case HID_TOUCH_REPORT_ID:
673 		cyttsp5_mt_attention(ts->dev);
674 		break;
675 	case HID_BTN_REPORT_ID:
676 		cyttsp5_btn_attention(ts->dev);
677 		break;
678 	default:
679 		/* It is not an input but a command response */
680 		memcpy(ts->response_buf, ts->input_buf, size);
681 		complete(&ts->cmd_done);
682 	}
683 
684 	return IRQ_HANDLED;
685 }
686 
687 static int cyttsp5_deassert_int(struct cyttsp5 *ts)
688 {
689 	u16 size;
690 	u8 buf[2];
691 	int error;
692 
693 	error = regmap_bulk_read(ts->regmap, HID_INPUT_REG, buf, sizeof(buf));
694 	if (error < 0)
695 		return error;
696 
697 	size = get_unaligned_le16(&buf[0]);
698 	if (size == 2 || size == 0)
699 		return 0;
700 
701 	return -EINVAL;
702 }
703 
704 static int cyttsp5_fill_all_touch(struct cyttsp5 *ts)
705 {
706 	struct cyttsp5_sysinfo *si = &ts->sysinfo;
707 
708 	fill_tch_abs(&si->tch_abs[CY_TCH_X], REPORT_SIZE_16,
709 		     TOUCH_REPORT_DESC_X);
710 	fill_tch_abs(&si->tch_abs[CY_TCH_Y], REPORT_SIZE_16,
711 		     TOUCH_REPORT_DESC_Y);
712 	fill_tch_abs(&si->tch_abs[CY_TCH_P], REPORT_SIZE_8,
713 		     TOUCH_REPORT_DESC_P);
714 	fill_tch_abs(&si->tch_abs[CY_TCH_T], REPORT_SIZE_5,
715 		     TOUCH_REPORT_DESC_CONTACTID);
716 	fill_tch_abs(&si->tch_hdr, REPORT_SIZE_5,
717 		     TOUCH_REPORT_DESC_HDR_CONTACTCOUNT);
718 	fill_tch_abs(&si->tch_abs[CY_TCH_MAJ], REPORT_SIZE_8,
719 		     TOUCH_REPORT_DESC_MAJ);
720 	fill_tch_abs(&si->tch_abs[CY_TCH_MIN], REPORT_SIZE_8,
721 		     TOUCH_REPORT_DESC_MIN);
722 
723 	return 0;
724 }
725 
726 static int cyttsp5_startup(struct cyttsp5 *ts)
727 {
728 	int error;
729 
730 	error = cyttsp5_deassert_int(ts);
731 	if (error) {
732 		dev_err(ts->dev, "Error on deassert int r=%d\n", error);
733 		return -ENODEV;
734 	}
735 
736 	/*
737 	 * Launch the application as the device starts in bootloader mode
738 	 * because of a power-on-reset
739 	 */
740 	error = cyttsp5_hid_output_bl_launch_app(ts);
741 	if (error < 0) {
742 		dev_err(ts->dev, "Error on launch app r=%d\n", error);
743 		return error;
744 	}
745 
746 	error = cyttsp5_get_hid_descriptor(ts, &ts->hid_desc);
747 	if (error < 0) {
748 		dev_err(ts->dev, "Error on getting HID descriptor r=%d\n", error);
749 		return error;
750 	}
751 
752 	error = cyttsp5_fill_all_touch(ts);
753 	if (error < 0) {
754 		dev_err(ts->dev, "Error on report descriptor r=%d\n", error);
755 		return error;
756 	}
757 
758 	error = cyttsp5_hid_output_get_sysinfo(ts);
759 	if (error) {
760 		dev_err(ts->dev, "Error on getting sysinfo r=%d\n", error);
761 		return error;
762 	}
763 
764 	return error;
765 }
766 
767 static void cyttsp5_cleanup(void *data)
768 {
769 	struct cyttsp5 *ts = data;
770 
771 	regulator_disable(ts->vdd);
772 }
773 
774 static int cyttsp5_probe(struct device *dev, struct regmap *regmap, int irq,
775 			 const char *name)
776 {
777 	struct cyttsp5 *ts;
778 	struct cyttsp5_sysinfo *si;
779 	int error, i;
780 
781 	ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
782 	if (!ts)
783 		return -ENOMEM;
784 
785 	/* Initialize device info */
786 	ts->regmap = regmap;
787 	ts->dev = dev;
788 	si = &ts->sysinfo;
789 	dev_set_drvdata(dev, ts);
790 
791 	init_completion(&ts->cmd_done);
792 
793 	/* Power up the device */
794 	ts->vdd = devm_regulator_get(dev, "vdd");
795 	if (IS_ERR(ts->vdd)) {
796 		error = PTR_ERR(ts->vdd);
797 		return error;
798 	}
799 
800 	error = devm_add_action_or_reset(dev, cyttsp5_cleanup, ts);
801 	if (error)
802 		return error;
803 
804 	error = regulator_enable(ts->vdd);
805 	if (error)
806 		return error;
807 
808 	ts->input = devm_input_allocate_device(dev);
809 	if (!ts->input) {
810 		dev_err(dev, "Error, failed to allocate input device\n");
811 		return -ENODEV;
812 	}
813 
814 	ts->input->name = "cyttsp5";
815 	scnprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev));
816 	ts->input->phys = ts->phys;
817 	input_set_drvdata(ts->input, ts);
818 
819 	/* Reset the gpio to be in a reset state */
820 	ts->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
821 	if (IS_ERR(ts->reset_gpio)) {
822 		error = PTR_ERR(ts->reset_gpio);
823 		dev_err(dev, "Failed to request reset gpio, error %d\n", error);
824 		return error;
825 	}
826 	gpiod_set_value_cansleep(ts->reset_gpio, 0);
827 
828 	/* Need a delay to have device up */
829 	msleep(20);
830 
831 	error = devm_request_threaded_irq(dev, irq, NULL, cyttsp5_handle_irq,
832 					  IRQF_ONESHOT, name, ts);
833 	if (error) {
834 		dev_err(dev, "unable to request IRQ\n");
835 		return error;
836 	}
837 
838 	error = cyttsp5_startup(ts);
839 	if (error) {
840 		dev_err(ts->dev, "Fail initial startup r=%d\n", error);
841 		return error;
842 	}
843 
844 	error = cyttsp5_parse_dt_key_code(dev);
845 	if (error < 0) {
846 		dev_err(ts->dev, "Error while parsing dts %d\n", error);
847 		return error;
848 	}
849 
850 	touchscreen_parse_properties(ts->input, true, &ts->prop);
851 
852 	__set_bit(EV_KEY, ts->input->evbit);
853 	for (i = 0; i < si->num_btns; i++)
854 		__set_bit(si->key_code[i], ts->input->keybit);
855 
856 	return cyttsp5_setup_input_device(dev);
857 }
858 
859 static int cyttsp5_i2c_probe(struct i2c_client *client)
860 {
861 	struct regmap *regmap;
862 	static const struct regmap_config config = {
863 		.reg_bits = 8,
864 		.val_bits = 8,
865 	};
866 
867 	regmap = devm_regmap_init_i2c(client, &config);
868 	if (IS_ERR(regmap)) {
869 		dev_err(&client->dev, "regmap allocation failed: %ld\n",
870 			PTR_ERR(regmap));
871 		return PTR_ERR(regmap);
872 	}
873 
874 	return cyttsp5_probe(&client->dev, regmap, client->irq, client->name);
875 }
876 
877 static const struct of_device_id cyttsp5_of_match[] = {
878 	{ .compatible = "cypress,tt21000", },
879 	{ }
880 };
881 MODULE_DEVICE_TABLE(of, cyttsp5_of_match);
882 
883 static const struct i2c_device_id cyttsp5_i2c_id[] = {
884 	{ CYTTSP5_NAME, 0, },
885 	{ }
886 };
887 MODULE_DEVICE_TABLE(i2c, cyttsp5_i2c_id);
888 
889 static struct i2c_driver cyttsp5_i2c_driver = {
890 	.driver = {
891 		.name = CYTTSP5_NAME,
892 		.of_match_table = cyttsp5_of_match,
893 	},
894 	.probe_new = cyttsp5_i2c_probe,
895 	.id_table = cyttsp5_i2c_id,
896 };
897 module_i2c_driver(cyttsp5_i2c_driver);
898 
899 MODULE_LICENSE("GPL");
900 MODULE_DESCRIPTION("Touchscreen driver for Cypress TrueTouch Gen 5 Product");
901 MODULE_AUTHOR("Mylène Josserand <mylene.josserand@bootlin.com>");
902