Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * cyttsp4_core.c
4 * Cypress TrueTouch(TM) Standard Product V4 Core driver module.
5 * For use with Cypress Txx4xx parts.
6 * Supported parts include:
7 * TMA4XX
8 * TMA1036
9 *
10 * Copyright (C) 2012 Cypress Semiconductor
11 *
12 * Contact Cypress Semiconductor at www.cypress.com <ttdrivers@cypress.com>
13 */
14
15#include "cyttsp4_core.h"
16#include <linux/delay.h>
17#include <linux/gpio.h>
18#include <linux/input/mt.h>
19#include <linux/interrupt.h>
20#include <linux/pm_runtime.h>
21#include <linux/sched.h>
22#include <linux/slab.h>
23
24/* Timeout in ms. */
25#define CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT 500
26#define CY_CORE_SLEEP_REQUEST_EXCLUSIVE_TIMEOUT 5000
27#define CY_CORE_MODE_CHANGE_TIMEOUT 1000
28#define CY_CORE_RESET_AND_WAIT_TIMEOUT 500
29#define CY_CORE_WAKEUP_TIMEOUT 500
30
31#define CY_CORE_STARTUP_RETRY_COUNT 3
32
33static const char * const cyttsp4_tch_abs_string[] = {
34 [CY_TCH_X] = "X",
35 [CY_TCH_Y] = "Y",
36 [CY_TCH_P] = "P",
37 [CY_TCH_T] = "T",
38 [CY_TCH_E] = "E",
39 [CY_TCH_O] = "O",
40 [CY_TCH_W] = "W",
41 [CY_TCH_MAJ] = "MAJ",
42 [CY_TCH_MIN] = "MIN",
43 [CY_TCH_OR] = "OR",
44 [CY_TCH_NUM_ABS] = "INVALID"
45};
46
47static const u8 ldr_exit[] = {
48 0xFF, 0x01, 0x3B, 0x00, 0x00, 0x4F, 0x6D, 0x17
49};
50
51static const u8 ldr_err_app[] = {
52 0x01, 0x02, 0x00, 0x00, 0x55, 0xDD, 0x17
53};
54
55static inline size_t merge_bytes(u8 high, u8 low)
56{
57 return (high << 8) + low;
58}
59
60#ifdef VERBOSE_DEBUG
61static void cyttsp4_pr_buf(struct device *dev, u8 *pr_buf, u8 *dptr, int size,
62 const char *data_name)
63{
64 int i, k;
65 const char fmt[] = "%02X ";
66 int max;
67
68 if (!size)
69 return;
70
71 max = (CY_MAX_PRBUF_SIZE - 1) - sizeof(CY_PR_TRUNCATED);
72
73 pr_buf[0] = 0;
74 for (i = k = 0; i < size && k < max; i++, k += 3)
75 scnprintf(pr_buf + k, CY_MAX_PRBUF_SIZE, fmt, dptr[i]);
76
77 dev_vdbg(dev, "%s: %s[0..%d]=%s%s\n", __func__, data_name, size - 1,
78 pr_buf, size <= max ? "" : CY_PR_TRUNCATED);
79}
80#else
81#define cyttsp4_pr_buf(dev, pr_buf, dptr, size, data_name) do { } while (0)
82#endif
83
84static int cyttsp4_load_status_regs(struct cyttsp4 *cd)
85{
86 struct cyttsp4_sysinfo *si = &cd->sysinfo;
87 struct device *dev = cd->dev;
88 int rc;
89
90 rc = cyttsp4_adap_read(cd, CY_REG_BASE, si->si_ofs.mode_size,
91 si->xy_mode);
92 if (rc < 0)
93 dev_err(dev, "%s: fail read mode regs r=%d\n",
94 __func__, rc);
95 else
96 cyttsp4_pr_buf(dev, cd->pr_buf, si->xy_mode,
97 si->si_ofs.mode_size, "xy_mode");
98
99 return rc;
100}
101
102static int cyttsp4_handshake(struct cyttsp4 *cd, u8 mode)
103{
104 u8 cmd = mode ^ CY_HST_TOGGLE;
105 int rc;
106
107 /*
108 * Mode change issued, handshaking now will cause endless mode change
109 * requests, for sync mode modechange will do same with handshake
110 * */
111 if (mode & CY_HST_MODE_CHANGE)
112 return 0;
113
114 rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(cmd), &cmd);
115 if (rc < 0)
116 dev_err(cd->dev, "%s: bus write fail on handshake (ret=%d)\n",
117 __func__, rc);
118
119 return rc;
120}
121
122static int cyttsp4_hw_soft_reset(struct cyttsp4 *cd)
123{
124 u8 cmd = CY_HST_RESET;
125 int rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(cmd), &cmd);
126 if (rc < 0) {
127 dev_err(cd->dev, "%s: FAILED to execute SOFT reset\n",
128 __func__);
129 return rc;
130 }
131 return 0;
132}
133
134static int cyttsp4_hw_hard_reset(struct cyttsp4 *cd)
135{
136 if (cd->cpdata->xres) {
137 cd->cpdata->xres(cd->cpdata, cd->dev);
138 dev_dbg(cd->dev, "%s: execute HARD reset\n", __func__);
139 return 0;
140 }
141 dev_err(cd->dev, "%s: FAILED to execute HARD reset\n", __func__);
142 return -ENOSYS;
143}
144
145static int cyttsp4_hw_reset(struct cyttsp4 *cd)
146{
147 int rc = cyttsp4_hw_hard_reset(cd);
148 if (rc == -ENOSYS)
149 rc = cyttsp4_hw_soft_reset(cd);
150 return rc;
151}
152
153/*
154 * Gets number of bits for a touch filed as parameter,
155 * sets maximum value for field which is used as bit mask
156 * and returns number of bytes required for that field
157 */
158static int cyttsp4_bits_2_bytes(unsigned int nbits, size_t *max)
159{
160 *max = 1UL << nbits;
161 return (nbits + 7) / 8;
162}
163
164static int cyttsp4_si_data_offsets(struct cyttsp4 *cd)
165{
166 struct cyttsp4_sysinfo *si = &cd->sysinfo;
167 int rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(si->si_data),
168 &si->si_data);
169 if (rc < 0) {
170 dev_err(cd->dev, "%s: fail read sysinfo data offsets r=%d\n",
171 __func__, rc);
172 return rc;
173 }
174
175 /* Print sysinfo data offsets */
176 cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)&si->si_data,
177 sizeof(si->si_data), "sysinfo_data_offsets");
178
179 /* convert sysinfo data offset bytes into integers */
180
181 si->si_ofs.map_sz = merge_bytes(si->si_data.map_szh,
182 si->si_data.map_szl);
183 si->si_ofs.map_sz = merge_bytes(si->si_data.map_szh,
184 si->si_data.map_szl);
185 si->si_ofs.cydata_ofs = merge_bytes(si->si_data.cydata_ofsh,
186 si->si_data.cydata_ofsl);
187 si->si_ofs.test_ofs = merge_bytes(si->si_data.test_ofsh,
188 si->si_data.test_ofsl);
189 si->si_ofs.pcfg_ofs = merge_bytes(si->si_data.pcfg_ofsh,
190 si->si_data.pcfg_ofsl);
191 si->si_ofs.opcfg_ofs = merge_bytes(si->si_data.opcfg_ofsh,
192 si->si_data.opcfg_ofsl);
193 si->si_ofs.ddata_ofs = merge_bytes(si->si_data.ddata_ofsh,
194 si->si_data.ddata_ofsl);
195 si->si_ofs.mdata_ofs = merge_bytes(si->si_data.mdata_ofsh,
196 si->si_data.mdata_ofsl);
197 return rc;
198}
199
200static int cyttsp4_si_get_cydata(struct cyttsp4 *cd)
201{
202 struct cyttsp4_sysinfo *si = &cd->sysinfo;
203 int read_offset;
204 int mfgid_sz, calc_mfgid_sz;
205 void *p;
206 int rc;
207
208 if (si->si_ofs.test_ofs <= si->si_ofs.cydata_ofs) {
209 dev_err(cd->dev,
210 "%s: invalid offset test_ofs: %zu, cydata_ofs: %zu\n",
211 __func__, si->si_ofs.test_ofs, si->si_ofs.cydata_ofs);
212 return -EINVAL;
213 }
214
215 si->si_ofs.cydata_size = si->si_ofs.test_ofs - si->si_ofs.cydata_ofs;
216 dev_dbg(cd->dev, "%s: cydata size: %zd\n", __func__,
217 si->si_ofs.cydata_size);
218
219 p = krealloc(si->si_ptrs.cydata, si->si_ofs.cydata_size, GFP_KERNEL);
220 if (p == NULL) {
221 dev_err(cd->dev, "%s: failed to allocate cydata memory\n",
222 __func__);
223 return -ENOMEM;
224 }
225 si->si_ptrs.cydata = p;
226
227 read_offset = si->si_ofs.cydata_ofs;
228
229 /* Read the CYDA registers up to MFGID field */
230 rc = cyttsp4_adap_read(cd, read_offset,
231 offsetof(struct cyttsp4_cydata, mfgid_sz)
232 + sizeof(si->si_ptrs.cydata->mfgid_sz),
233 si->si_ptrs.cydata);
234 if (rc < 0) {
235 dev_err(cd->dev, "%s: fail read cydata r=%d\n",
236 __func__, rc);
237 return rc;
238 }
239
240 /* Check MFGID size */
241 mfgid_sz = si->si_ptrs.cydata->mfgid_sz;
242 calc_mfgid_sz = si->si_ofs.cydata_size - sizeof(struct cyttsp4_cydata);
243 if (mfgid_sz != calc_mfgid_sz) {
244 dev_err(cd->dev, "%s: mismatch in MFGID size, reported:%d calculated:%d\n",
245 __func__, mfgid_sz, calc_mfgid_sz);
246 return -EINVAL;
247 }
248
249 read_offset += offsetof(struct cyttsp4_cydata, mfgid_sz)
250 + sizeof(si->si_ptrs.cydata->mfgid_sz);
251
252 /* Read the CYDA registers for MFGID field */
253 rc = cyttsp4_adap_read(cd, read_offset, si->si_ptrs.cydata->mfgid_sz,
254 si->si_ptrs.cydata->mfg_id);
255 if (rc < 0) {
256 dev_err(cd->dev, "%s: fail read cydata r=%d\n",
257 __func__, rc);
258 return rc;
259 }
260
261 read_offset += si->si_ptrs.cydata->mfgid_sz;
262
263 /* Read the rest of the CYDA registers */
264 rc = cyttsp4_adap_read(cd, read_offset,
265 sizeof(struct cyttsp4_cydata)
266 - offsetof(struct cyttsp4_cydata, cyito_idh),
267 &si->si_ptrs.cydata->cyito_idh);
268 if (rc < 0) {
269 dev_err(cd->dev, "%s: fail read cydata r=%d\n",
270 __func__, rc);
271 return rc;
272 }
273
274 cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)si->si_ptrs.cydata,
275 si->si_ofs.cydata_size, "sysinfo_cydata");
276 return rc;
277}
278
279static int cyttsp4_si_get_test_data(struct cyttsp4 *cd)
280{
281 struct cyttsp4_sysinfo *si = &cd->sysinfo;
282 void *p;
283 int rc;
284
285 if (si->si_ofs.pcfg_ofs <= si->si_ofs.test_ofs) {
286 dev_err(cd->dev,
287 "%s: invalid offset pcfg_ofs: %zu, test_ofs: %zu\n",
288 __func__, si->si_ofs.pcfg_ofs, si->si_ofs.test_ofs);
289 return -EINVAL;
290 }
291
292 si->si_ofs.test_size = si->si_ofs.pcfg_ofs - si->si_ofs.test_ofs;
293
294 p = krealloc(si->si_ptrs.test, si->si_ofs.test_size, GFP_KERNEL);
295 if (p == NULL) {
296 dev_err(cd->dev, "%s: failed to allocate test memory\n",
297 __func__);
298 return -ENOMEM;
299 }
300 si->si_ptrs.test = p;
301
302 rc = cyttsp4_adap_read(cd, si->si_ofs.test_ofs, si->si_ofs.test_size,
303 si->si_ptrs.test);
304 if (rc < 0) {
305 dev_err(cd->dev, "%s: fail read test data r=%d\n",
306 __func__, rc);
307 return rc;
308 }
309
310 cyttsp4_pr_buf(cd->dev, cd->pr_buf,
311 (u8 *)si->si_ptrs.test, si->si_ofs.test_size,
312 "sysinfo_test_data");
313 if (si->si_ptrs.test->post_codel &
314 CY_POST_CODEL_WDG_RST)
315 dev_info(cd->dev, "%s: %s codel=%02X\n",
316 __func__, "Reset was a WATCHDOG RESET",
317 si->si_ptrs.test->post_codel);
318
319 if (!(si->si_ptrs.test->post_codel &
320 CY_POST_CODEL_CFG_DATA_CRC_FAIL))
321 dev_info(cd->dev, "%s: %s codel=%02X\n", __func__,
322 "Config Data CRC FAIL",
323 si->si_ptrs.test->post_codel);
324
325 if (!(si->si_ptrs.test->post_codel &
326 CY_POST_CODEL_PANEL_TEST_FAIL))
327 dev_info(cd->dev, "%s: %s codel=%02X\n",
328 __func__, "PANEL TEST FAIL",
329 si->si_ptrs.test->post_codel);
330
331 dev_info(cd->dev, "%s: SCANNING is %s codel=%02X\n",
332 __func__, si->si_ptrs.test->post_codel & 0x08 ?
333 "ENABLED" : "DISABLED",
334 si->si_ptrs.test->post_codel);
335 return rc;
336}
337
338static int cyttsp4_si_get_pcfg_data(struct cyttsp4 *cd)
339{
340 struct cyttsp4_sysinfo *si = &cd->sysinfo;
341 void *p;
342 int rc;
343
344 if (si->si_ofs.opcfg_ofs <= si->si_ofs.pcfg_ofs) {
345 dev_err(cd->dev,
346 "%s: invalid offset opcfg_ofs: %zu, pcfg_ofs: %zu\n",
347 __func__, si->si_ofs.opcfg_ofs, si->si_ofs.pcfg_ofs);
348 return -EINVAL;
349 }
350
351 si->si_ofs.pcfg_size = si->si_ofs.opcfg_ofs - si->si_ofs.pcfg_ofs;
352
353 p = krealloc(si->si_ptrs.pcfg, si->si_ofs.pcfg_size, GFP_KERNEL);
354 if (p == NULL) {
355 dev_err(cd->dev, "%s: failed to allocate pcfg memory\n",
356 __func__);
357 return -ENOMEM;
358 }
359 si->si_ptrs.pcfg = p;
360
361 rc = cyttsp4_adap_read(cd, si->si_ofs.pcfg_ofs, si->si_ofs.pcfg_size,
362 si->si_ptrs.pcfg);
363 if (rc < 0) {
364 dev_err(cd->dev, "%s: fail read pcfg data r=%d\n",
365 __func__, rc);
366 return rc;
367 }
368
369 si->si_ofs.max_x = merge_bytes((si->si_ptrs.pcfg->res_xh
370 & CY_PCFG_RESOLUTION_X_MASK), si->si_ptrs.pcfg->res_xl);
371 si->si_ofs.x_origin = !!(si->si_ptrs.pcfg->res_xh
372 & CY_PCFG_ORIGIN_X_MASK);
373 si->si_ofs.max_y = merge_bytes((si->si_ptrs.pcfg->res_yh
374 & CY_PCFG_RESOLUTION_Y_MASK), si->si_ptrs.pcfg->res_yl);
375 si->si_ofs.y_origin = !!(si->si_ptrs.pcfg->res_yh
376 & CY_PCFG_ORIGIN_Y_MASK);
377 si->si_ofs.max_p = merge_bytes(si->si_ptrs.pcfg->max_zh,
378 si->si_ptrs.pcfg->max_zl);
379
380 cyttsp4_pr_buf(cd->dev, cd->pr_buf,
381 (u8 *)si->si_ptrs.pcfg,
382 si->si_ofs.pcfg_size, "sysinfo_pcfg_data");
383 return rc;
384}
385
386static int cyttsp4_si_get_opcfg_data(struct cyttsp4 *cd)
387{
388 struct cyttsp4_sysinfo *si = &cd->sysinfo;
389 struct cyttsp4_tch_abs_params *tch;
390 struct cyttsp4_tch_rec_params *tch_old, *tch_new;
391 enum cyttsp4_tch_abs abs;
392 int i;
393 void *p;
394 int rc;
395
396 if (si->si_ofs.ddata_ofs <= si->si_ofs.opcfg_ofs) {
397 dev_err(cd->dev,
398 "%s: invalid offset ddata_ofs: %zu, opcfg_ofs: %zu\n",
399 __func__, si->si_ofs.ddata_ofs, si->si_ofs.opcfg_ofs);
400 return -EINVAL;
401 }
402
403 si->si_ofs.opcfg_size = si->si_ofs.ddata_ofs - si->si_ofs.opcfg_ofs;
404
405 p = krealloc(si->si_ptrs.opcfg, si->si_ofs.opcfg_size, GFP_KERNEL);
406 if (p == NULL) {
407 dev_err(cd->dev, "%s: failed to allocate opcfg memory\n",
408 __func__);
409 return -ENOMEM;
410 }
411 si->si_ptrs.opcfg = p;
412
413 rc = cyttsp4_adap_read(cd, si->si_ofs.opcfg_ofs, si->si_ofs.opcfg_size,
414 si->si_ptrs.opcfg);
415 if (rc < 0) {
416 dev_err(cd->dev, "%s: fail read opcfg data r=%d\n",
417 __func__, rc);
418 return rc;
419 }
420 si->si_ofs.cmd_ofs = si->si_ptrs.opcfg->cmd_ofs;
421 si->si_ofs.rep_ofs = si->si_ptrs.opcfg->rep_ofs;
422 si->si_ofs.rep_sz = (si->si_ptrs.opcfg->rep_szh * 256) +
423 si->si_ptrs.opcfg->rep_szl;
424 si->si_ofs.num_btns = si->si_ptrs.opcfg->num_btns;
425 si->si_ofs.num_btn_regs = (si->si_ofs.num_btns +
426 CY_NUM_BTN_PER_REG - 1) / CY_NUM_BTN_PER_REG;
427 si->si_ofs.tt_stat_ofs = si->si_ptrs.opcfg->tt_stat_ofs;
428 si->si_ofs.obj_cfg0 = si->si_ptrs.opcfg->obj_cfg0;
429 si->si_ofs.max_tchs = si->si_ptrs.opcfg->max_tchs &
430 CY_BYTE_OFS_MASK;
431 si->si_ofs.tch_rec_size = si->si_ptrs.opcfg->tch_rec_size &
432 CY_BYTE_OFS_MASK;
433
434 /* Get the old touch fields */
435 for (abs = CY_TCH_X; abs < CY_NUM_TCH_FIELDS; abs++) {
436 tch = &si->si_ofs.tch_abs[abs];
437 tch_old = &si->si_ptrs.opcfg->tch_rec_old[abs];
438
439 tch->ofs = tch_old->loc & CY_BYTE_OFS_MASK;
440 tch->size = cyttsp4_bits_2_bytes(tch_old->size,
441 &tch->max);
442 tch->bofs = (tch_old->loc & CY_BOFS_MASK) >> CY_BOFS_SHIFT;
443 }
444
445 /* button fields */
446 si->si_ofs.btn_rec_size = si->si_ptrs.opcfg->btn_rec_size;
447 si->si_ofs.btn_diff_ofs = si->si_ptrs.opcfg->btn_diff_ofs;
448 si->si_ofs.btn_diff_size = si->si_ptrs.opcfg->btn_diff_size;
449
450 if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE) {
451 /* Get the extended touch fields */
452 for (i = 0; i < CY_NUM_EXT_TCH_FIELDS; abs++, i++) {
453 tch = &si->si_ofs.tch_abs[abs];
454 tch_new = &si->si_ptrs.opcfg->tch_rec_new[i];
455
456 tch->ofs = tch_new->loc & CY_BYTE_OFS_MASK;
457 tch->size = cyttsp4_bits_2_bytes(tch_new->size,
458 &tch->max);
459 tch->bofs = (tch_new->loc & CY_BOFS_MASK) >> CY_BOFS_SHIFT;
460 }
461 }
462
463 for (abs = 0; abs < CY_TCH_NUM_ABS; abs++) {
464 dev_dbg(cd->dev, "%s: tch_rec_%s\n", __func__,
465 cyttsp4_tch_abs_string[abs]);
466 dev_dbg(cd->dev, "%s: ofs =%2zd\n", __func__,
467 si->si_ofs.tch_abs[abs].ofs);
468 dev_dbg(cd->dev, "%s: siz =%2zd\n", __func__,
469 si->si_ofs.tch_abs[abs].size);
470 dev_dbg(cd->dev, "%s: max =%2zd\n", __func__,
471 si->si_ofs.tch_abs[abs].max);
472 dev_dbg(cd->dev, "%s: bofs=%2zd\n", __func__,
473 si->si_ofs.tch_abs[abs].bofs);
474 }
475
476 si->si_ofs.mode_size = si->si_ofs.tt_stat_ofs + 1;
477 si->si_ofs.data_size = si->si_ofs.max_tchs *
478 si->si_ptrs.opcfg->tch_rec_size;
479
480 cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)si->si_ptrs.opcfg,
481 si->si_ofs.opcfg_size, "sysinfo_opcfg_data");
482
483 return 0;
484}
485
486static int cyttsp4_si_get_ddata(struct cyttsp4 *cd)
487{
488 struct cyttsp4_sysinfo *si = &cd->sysinfo;
489 void *p;
490 int rc;
491
492 si->si_ofs.ddata_size = si->si_ofs.mdata_ofs - si->si_ofs.ddata_ofs;
493
494 p = krealloc(si->si_ptrs.ddata, si->si_ofs.ddata_size, GFP_KERNEL);
495 if (p == NULL) {
496 dev_err(cd->dev, "%s: fail alloc ddata memory\n", __func__);
497 return -ENOMEM;
498 }
499 si->si_ptrs.ddata = p;
500
501 rc = cyttsp4_adap_read(cd, si->si_ofs.ddata_ofs, si->si_ofs.ddata_size,
502 si->si_ptrs.ddata);
503 if (rc < 0)
504 dev_err(cd->dev, "%s: fail read ddata data r=%d\n",
505 __func__, rc);
506 else
507 cyttsp4_pr_buf(cd->dev, cd->pr_buf,
508 (u8 *)si->si_ptrs.ddata,
509 si->si_ofs.ddata_size, "sysinfo_ddata");
510 return rc;
511}
512
513static int cyttsp4_si_get_mdata(struct cyttsp4 *cd)
514{
515 struct cyttsp4_sysinfo *si = &cd->sysinfo;
516 void *p;
517 int rc;
518
519 si->si_ofs.mdata_size = si->si_ofs.map_sz - si->si_ofs.mdata_ofs;
520
521 p = krealloc(si->si_ptrs.mdata, si->si_ofs.mdata_size, GFP_KERNEL);
522 if (p == NULL) {
523 dev_err(cd->dev, "%s: fail alloc mdata memory\n", __func__);
524 return -ENOMEM;
525 }
526 si->si_ptrs.mdata = p;
527
528 rc = cyttsp4_adap_read(cd, si->si_ofs.mdata_ofs, si->si_ofs.mdata_size,
529 si->si_ptrs.mdata);
530 if (rc < 0)
531 dev_err(cd->dev, "%s: fail read mdata data r=%d\n",
532 __func__, rc);
533 else
534 cyttsp4_pr_buf(cd->dev, cd->pr_buf,
535 (u8 *)si->si_ptrs.mdata,
536 si->si_ofs.mdata_size, "sysinfo_mdata");
537 return rc;
538}
539
540static int cyttsp4_si_get_btn_data(struct cyttsp4 *cd)
541{
542 struct cyttsp4_sysinfo *si = &cd->sysinfo;
543 int btn;
544 int num_defined_keys;
545 u16 *key_table;
546 void *p;
547 int rc = 0;
548
549 if (si->si_ofs.num_btns) {
550 si->si_ofs.btn_keys_size = si->si_ofs.num_btns *
551 sizeof(struct cyttsp4_btn);
552
553 p = krealloc(si->btn, si->si_ofs.btn_keys_size,
554 GFP_KERNEL|__GFP_ZERO);
555 if (p == NULL) {
556 dev_err(cd->dev, "%s: %s\n", __func__,
557 "fail alloc btn_keys memory");
558 return -ENOMEM;
559 }
560 si->btn = p;
561
562 if (cd->cpdata->sett[CY_IC_GRPNUM_BTN_KEYS] == NULL)
563 num_defined_keys = 0;
564 else if (cd->cpdata->sett[CY_IC_GRPNUM_BTN_KEYS]->data == NULL)
565 num_defined_keys = 0;
566 else
567 num_defined_keys = cd->cpdata->sett
568 [CY_IC_GRPNUM_BTN_KEYS]->size;
569
570 for (btn = 0; btn < si->si_ofs.num_btns &&
571 btn < num_defined_keys; btn++) {
572 key_table = (u16 *)cd->cpdata->sett
573 [CY_IC_GRPNUM_BTN_KEYS]->data;
574 si->btn[btn].key_code = key_table[btn];
575 si->btn[btn].state = CY_BTN_RELEASED;
576 si->btn[btn].enabled = true;
577 }
578 for (; btn < si->si_ofs.num_btns; btn++) {
579 si->btn[btn].key_code = KEY_RESERVED;
580 si->btn[btn].state = CY_BTN_RELEASED;
581 si->btn[btn].enabled = true;
582 }
583
584 return rc;
585 }
586
587 si->si_ofs.btn_keys_size = 0;
588 kfree(si->btn);
589 si->btn = NULL;
590 return rc;
591}
592
593static int cyttsp4_si_get_op_data_ptrs(struct cyttsp4 *cd)
594{
595 struct cyttsp4_sysinfo *si = &cd->sysinfo;
596 void *p;
597
598 p = krealloc(si->xy_mode, si->si_ofs.mode_size, GFP_KERNEL|__GFP_ZERO);
599 if (p == NULL)
600 return -ENOMEM;
601 si->xy_mode = p;
602
603 p = krealloc(si->xy_data, si->si_ofs.data_size, GFP_KERNEL|__GFP_ZERO);
604 if (p == NULL)
605 return -ENOMEM;
606 si->xy_data = p;
607
608 p = krealloc(si->btn_rec_data,
609 si->si_ofs.btn_rec_size * si->si_ofs.num_btns,
610 GFP_KERNEL|__GFP_ZERO);
611 if (p == NULL)
612 return -ENOMEM;
613 si->btn_rec_data = p;
614
615 return 0;
616}
617
618static void cyttsp4_si_put_log_data(struct cyttsp4 *cd)
619{
620 struct cyttsp4_sysinfo *si = &cd->sysinfo;
621 dev_dbg(cd->dev, "%s: cydata_ofs =%4zd siz=%4zd\n", __func__,
622 si->si_ofs.cydata_ofs, si->si_ofs.cydata_size);
623 dev_dbg(cd->dev, "%s: test_ofs =%4zd siz=%4zd\n", __func__,
624 si->si_ofs.test_ofs, si->si_ofs.test_size);
625 dev_dbg(cd->dev, "%s: pcfg_ofs =%4zd siz=%4zd\n", __func__,
626 si->si_ofs.pcfg_ofs, si->si_ofs.pcfg_size);
627 dev_dbg(cd->dev, "%s: opcfg_ofs =%4zd siz=%4zd\n", __func__,
628 si->si_ofs.opcfg_ofs, si->si_ofs.opcfg_size);
629 dev_dbg(cd->dev, "%s: ddata_ofs =%4zd siz=%4zd\n", __func__,
630 si->si_ofs.ddata_ofs, si->si_ofs.ddata_size);
631 dev_dbg(cd->dev, "%s: mdata_ofs =%4zd siz=%4zd\n", __func__,
632 si->si_ofs.mdata_ofs, si->si_ofs.mdata_size);
633
634 dev_dbg(cd->dev, "%s: cmd_ofs =%4zd\n", __func__,
635 si->si_ofs.cmd_ofs);
636 dev_dbg(cd->dev, "%s: rep_ofs =%4zd\n", __func__,
637 si->si_ofs.rep_ofs);
638 dev_dbg(cd->dev, "%s: rep_sz =%4zd\n", __func__,
639 si->si_ofs.rep_sz);
640 dev_dbg(cd->dev, "%s: num_btns =%4zd\n", __func__,
641 si->si_ofs.num_btns);
642 dev_dbg(cd->dev, "%s: num_btn_regs =%4zd\n", __func__,
643 si->si_ofs.num_btn_regs);
644 dev_dbg(cd->dev, "%s: tt_stat_ofs =%4zd\n", __func__,
645 si->si_ofs.tt_stat_ofs);
646 dev_dbg(cd->dev, "%s: tch_rec_size =%4zd\n", __func__,
647 si->si_ofs.tch_rec_size);
648 dev_dbg(cd->dev, "%s: max_tchs =%4zd\n", __func__,
649 si->si_ofs.max_tchs);
650 dev_dbg(cd->dev, "%s: mode_size =%4zd\n", __func__,
651 si->si_ofs.mode_size);
652 dev_dbg(cd->dev, "%s: data_size =%4zd\n", __func__,
653 si->si_ofs.data_size);
654 dev_dbg(cd->dev, "%s: map_sz =%4zd\n", __func__,
655 si->si_ofs.map_sz);
656
657 dev_dbg(cd->dev, "%s: btn_rec_size =%2zd\n", __func__,
658 si->si_ofs.btn_rec_size);
659 dev_dbg(cd->dev, "%s: btn_diff_ofs =%2zd\n", __func__,
660 si->si_ofs.btn_diff_ofs);
661 dev_dbg(cd->dev, "%s: btn_diff_size =%2zd\n", __func__,
662 si->si_ofs.btn_diff_size);
663
664 dev_dbg(cd->dev, "%s: max_x = 0x%04zX (%zd)\n", __func__,
665 si->si_ofs.max_x, si->si_ofs.max_x);
666 dev_dbg(cd->dev, "%s: x_origin = %zd (%s)\n", __func__,
667 si->si_ofs.x_origin,
668 si->si_ofs.x_origin == CY_NORMAL_ORIGIN ?
669 "left corner" : "right corner");
670 dev_dbg(cd->dev, "%s: max_y = 0x%04zX (%zd)\n", __func__,
671 si->si_ofs.max_y, si->si_ofs.max_y);
672 dev_dbg(cd->dev, "%s: y_origin = %zd (%s)\n", __func__,
673 si->si_ofs.y_origin,
674 si->si_ofs.y_origin == CY_NORMAL_ORIGIN ?
675 "upper corner" : "lower corner");
676 dev_dbg(cd->dev, "%s: max_p = 0x%04zX (%zd)\n", __func__,
677 si->si_ofs.max_p, si->si_ofs.max_p);
678
679 dev_dbg(cd->dev, "%s: xy_mode=%p xy_data=%p\n", __func__,
680 si->xy_mode, si->xy_data);
681}
682
683static int cyttsp4_get_sysinfo_regs(struct cyttsp4 *cd)
684{
685 struct cyttsp4_sysinfo *si = &cd->sysinfo;
686 int rc;
687
688 rc = cyttsp4_si_data_offsets(cd);
689 if (rc < 0)
690 return rc;
691
692 rc = cyttsp4_si_get_cydata(cd);
693 if (rc < 0)
694 return rc;
695
696 rc = cyttsp4_si_get_test_data(cd);
697 if (rc < 0)
698 return rc;
699
700 rc = cyttsp4_si_get_pcfg_data(cd);
701 if (rc < 0)
702 return rc;
703
704 rc = cyttsp4_si_get_opcfg_data(cd);
705 if (rc < 0)
706 return rc;
707
708 rc = cyttsp4_si_get_ddata(cd);
709 if (rc < 0)
710 return rc;
711
712 rc = cyttsp4_si_get_mdata(cd);
713 if (rc < 0)
714 return rc;
715
716 rc = cyttsp4_si_get_btn_data(cd);
717 if (rc < 0)
718 return rc;
719
720 rc = cyttsp4_si_get_op_data_ptrs(cd);
721 if (rc < 0) {
722 dev_err(cd->dev, "%s: failed to get_op_data\n",
723 __func__);
724 return rc;
725 }
726
727 cyttsp4_si_put_log_data(cd);
728
729 /* provide flow control handshake */
730 rc = cyttsp4_handshake(cd, si->si_data.hst_mode);
731 if (rc < 0)
732 dev_err(cd->dev, "%s: handshake fail on sysinfo reg\n",
733 __func__);
734
735 si->ready = true;
736 return rc;
737}
738
739static void cyttsp4_queue_startup_(struct cyttsp4 *cd)
740{
741 if (cd->startup_state == STARTUP_NONE) {
742 cd->startup_state = STARTUP_QUEUED;
743 schedule_work(&cd->startup_work);
744 dev_dbg(cd->dev, "%s: cyttsp4_startup queued\n", __func__);
745 } else {
746 dev_dbg(cd->dev, "%s: startup_state = %d\n", __func__,
747 cd->startup_state);
748 }
749}
750
751static void cyttsp4_report_slot_liftoff(struct cyttsp4_mt_data *md,
752 int max_slots)
753{
754 int t;
755
756 if (md->num_prv_tch == 0)
757 return;
758
759 for (t = 0; t < max_slots; t++) {
760 input_mt_slot(md->input, t);
761 input_mt_report_slot_inactive(md->input);
762 }
763}
764
765static void cyttsp4_lift_all(struct cyttsp4_mt_data *md)
766{
767 if (!md->si)
768 return;
769
770 if (md->num_prv_tch != 0) {
771 cyttsp4_report_slot_liftoff(md,
772 md->si->si_ofs.tch_abs[CY_TCH_T].max);
773 input_sync(md->input);
774 md->num_prv_tch = 0;
775 }
776}
777
778static void cyttsp4_get_touch_axis(struct cyttsp4_mt_data *md,
779 int *axis, int size, int max, u8 *xy_data, int bofs)
780{
781 int nbyte;
782 int next;
783
784 for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
785 dev_vdbg(&md->input->dev,
786 "%s: *axis=%02X(%d) size=%d max=%08X xy_data=%p"
787 " xy_data[%d]=%02X(%d) bofs=%d\n",
788 __func__, *axis, *axis, size, max, xy_data, next,
789 xy_data[next], xy_data[next], bofs);
790 *axis = (*axis * 256) + (xy_data[next] >> bofs);
791 next++;
792 }
793
794 *axis &= max - 1;
795
796 dev_vdbg(&md->input->dev,
797 "%s: *axis=%02X(%d) size=%d max=%08X xy_data=%p"
798 " xy_data[%d]=%02X(%d)\n",
799 __func__, *axis, *axis, size, max, xy_data, next,
800 xy_data[next], xy_data[next]);
801}
802
803static void cyttsp4_get_touch(struct cyttsp4_mt_data *md,
804 struct cyttsp4_touch *touch, u8 *xy_data)
805{
806 struct device *dev = &md->input->dev;
807 struct cyttsp4_sysinfo *si = md->si;
808 enum cyttsp4_tch_abs abs;
809 bool flipped;
810
811 for (abs = CY_TCH_X; abs < CY_TCH_NUM_ABS; abs++) {
812 cyttsp4_get_touch_axis(md, &touch->abs[abs],
813 si->si_ofs.tch_abs[abs].size,
814 si->si_ofs.tch_abs[abs].max,
815 xy_data + si->si_ofs.tch_abs[abs].ofs,
816 si->si_ofs.tch_abs[abs].bofs);
817 dev_vdbg(dev, "%s: get %s=%04X(%d)\n", __func__,
818 cyttsp4_tch_abs_string[abs],
819 touch->abs[abs], touch->abs[abs]);
820 }
821
822 if (md->pdata->flags & CY_FLAG_FLIP) {
823 swap(touch->abs[CY_TCH_X], touch->abs[CY_TCH_Y]);
824 flipped = true;
825 } else
826 flipped = false;
827
828 if (md->pdata->flags & CY_FLAG_INV_X) {
829 if (flipped)
830 touch->abs[CY_TCH_X] = md->si->si_ofs.max_y -
831 touch->abs[CY_TCH_X];
832 else
833 touch->abs[CY_TCH_X] = md->si->si_ofs.max_x -
834 touch->abs[CY_TCH_X];
835 }
836 if (md->pdata->flags & CY_FLAG_INV_Y) {
837 if (flipped)
838 touch->abs[CY_TCH_Y] = md->si->si_ofs.max_x -
839 touch->abs[CY_TCH_Y];
840 else
841 touch->abs[CY_TCH_Y] = md->si->si_ofs.max_y -
842 touch->abs[CY_TCH_Y];
843 }
844
845 dev_vdbg(dev, "%s: flip=%s inv-x=%s inv-y=%s x=%04X(%d) y=%04X(%d)\n",
846 __func__, flipped ? "true" : "false",
847 md->pdata->flags & CY_FLAG_INV_X ? "true" : "false",
848 md->pdata->flags & CY_FLAG_INV_Y ? "true" : "false",
849 touch->abs[CY_TCH_X], touch->abs[CY_TCH_X],
850 touch->abs[CY_TCH_Y], touch->abs[CY_TCH_Y]);
851}
852
853static void cyttsp4_final_sync(struct input_dev *input, int max_slots, int *ids)
854{
855 int t;
856
857 for (t = 0; t < max_slots; t++) {
858 if (ids[t])
859 continue;
860 input_mt_slot(input, t);
861 input_mt_report_slot_inactive(input);
862 }
863
864 input_sync(input);
865}
866
867static void cyttsp4_get_mt_touches(struct cyttsp4_mt_data *md, int num_cur_tch)
868{
869 struct device *dev = &md->input->dev;
870 struct cyttsp4_sysinfo *si = md->si;
871 struct cyttsp4_touch tch;
872 int sig;
873 int i, j, t = 0;
874 int ids[max(CY_TMA1036_MAX_TCH, CY_TMA4XX_MAX_TCH)];
875
876 memset(ids, 0, si->si_ofs.tch_abs[CY_TCH_T].max * sizeof(int));
877 for (i = 0; i < num_cur_tch; i++) {
878 cyttsp4_get_touch(md, &tch, si->xy_data +
879 (i * si->si_ofs.tch_rec_size));
880 if ((tch.abs[CY_TCH_T] < md->pdata->frmwrk->abs
881 [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MIN_OST]) ||
882 (tch.abs[CY_TCH_T] > md->pdata->frmwrk->abs
883 [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MAX_OST])) {
884 dev_err(dev, "%s: tch=%d -> bad trk_id=%d max_id=%d\n",
885 __func__, i, tch.abs[CY_TCH_T],
886 md->pdata->frmwrk->abs[(CY_ABS_ID_OST *
887 CY_NUM_ABS_SET) + CY_MAX_OST]);
888 continue;
889 }
890
891 /* use 0 based track id's */
892 sig = md->pdata->frmwrk->abs
893 [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + 0];
894 if (sig != CY_IGNORE_VALUE) {
895 t = tch.abs[CY_TCH_T] - md->pdata->frmwrk->abs
896 [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MIN_OST];
897 if (tch.abs[CY_TCH_E] == CY_EV_LIFTOFF) {
898 dev_dbg(dev, "%s: t=%d e=%d lift-off\n",
899 __func__, t, tch.abs[CY_TCH_E]);
900 goto cyttsp4_get_mt_touches_pr_tch;
901 }
902 input_mt_slot(md->input, t);
903 input_mt_report_slot_state(md->input, MT_TOOL_FINGER,
904 true);
905 ids[t] = true;
906 }
907
908 /* all devices: position and pressure fields */
909 for (j = 0; j <= CY_ABS_W_OST; j++) {
910 sig = md->pdata->frmwrk->abs[((CY_ABS_X_OST + j) *
911 CY_NUM_ABS_SET) + 0];
912 if (sig != CY_IGNORE_VALUE)
913 input_report_abs(md->input, sig,
914 tch.abs[CY_TCH_X + j]);
915 }
916 if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE) {
917 /*
918 * TMA400 size and orientation fields:
919 * if pressure is non-zero and major touch
920 * signal is zero, then set major and minor touch
921 * signals to minimum non-zero value
922 */
923 if (tch.abs[CY_TCH_P] > 0 && tch.abs[CY_TCH_MAJ] == 0)
924 tch.abs[CY_TCH_MAJ] = tch.abs[CY_TCH_MIN] = 1;
925
926 /* Get the extended touch fields */
927 for (j = 0; j < CY_NUM_EXT_TCH_FIELDS; j++) {
928 sig = md->pdata->frmwrk->abs
929 [((CY_ABS_MAJ_OST + j) *
930 CY_NUM_ABS_SET) + 0];
931 if (sig != CY_IGNORE_VALUE)
932 input_report_abs(md->input, sig,
933 tch.abs[CY_TCH_MAJ + j]);
934 }
935 }
936
937cyttsp4_get_mt_touches_pr_tch:
938 if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE)
939 dev_dbg(dev,
940 "%s: t=%d x=%d y=%d z=%d M=%d m=%d o=%d e=%d\n",
941 __func__, t,
942 tch.abs[CY_TCH_X],
943 tch.abs[CY_TCH_Y],
944 tch.abs[CY_TCH_P],
945 tch.abs[CY_TCH_MAJ],
946 tch.abs[CY_TCH_MIN],
947 tch.abs[CY_TCH_OR],
948 tch.abs[CY_TCH_E]);
949 else
950 dev_dbg(dev,
951 "%s: t=%d x=%d y=%d z=%d e=%d\n", __func__,
952 t,
953 tch.abs[CY_TCH_X],
954 tch.abs[CY_TCH_Y],
955 tch.abs[CY_TCH_P],
956 tch.abs[CY_TCH_E]);
957 }
958
959 cyttsp4_final_sync(md->input, si->si_ofs.tch_abs[CY_TCH_T].max, ids);
960
961 md->num_prv_tch = num_cur_tch;
962
963 return;
964}
965
966/* read xy_data for all current touches */
967static int cyttsp4_xy_worker(struct cyttsp4 *cd)
968{
969 struct cyttsp4_mt_data *md = &cd->md;
970 struct device *dev = &md->input->dev;
971 struct cyttsp4_sysinfo *si = md->si;
972 u8 num_cur_tch;
973 u8 hst_mode;
974 u8 rep_len;
975 u8 rep_stat;
976 u8 tt_stat;
977 int rc = 0;
978
979 /*
980 * Get event data from cyttsp4 device.
981 * The event data includes all data
982 * for all active touches.
983 * Event data also includes button data
984 */
985 /*
986 * Use 2 reads:
987 * 1st read to get mode + button bytes + touch count (core)
988 * 2nd read (optional) to get touch 1 - touch n data
989 */
990 hst_mode = si->xy_mode[CY_REG_BASE];
991 rep_len = si->xy_mode[si->si_ofs.rep_ofs];
992 rep_stat = si->xy_mode[si->si_ofs.rep_ofs + 1];
993 tt_stat = si->xy_mode[si->si_ofs.tt_stat_ofs];
994 dev_vdbg(dev, "%s: %s%02X %s%d %s%02X %s%02X\n", __func__,
995 "hst_mode=", hst_mode, "rep_len=", rep_len,
996 "rep_stat=", rep_stat, "tt_stat=", tt_stat);
997
998 num_cur_tch = GET_NUM_TOUCHES(tt_stat);
999 dev_vdbg(dev, "%s: num_cur_tch=%d\n", __func__, num_cur_tch);
1000
1001 if (rep_len == 0 && num_cur_tch > 0) {
1002 dev_err(dev, "%s: report length error rep_len=%d num_tch=%d\n",
1003 __func__, rep_len, num_cur_tch);
1004 goto cyttsp4_xy_worker_exit;
1005 }
1006
1007 /* read touches */
1008 if (num_cur_tch > 0) {
1009 rc = cyttsp4_adap_read(cd, si->si_ofs.tt_stat_ofs + 1,
1010 num_cur_tch * si->si_ofs.tch_rec_size,
1011 si->xy_data);
1012 if (rc < 0) {
1013 dev_err(dev, "%s: read fail on touch regs r=%d\n",
1014 __func__, rc);
1015 goto cyttsp4_xy_worker_exit;
1016 }
1017 }
1018
1019 /* print xy data */
1020 cyttsp4_pr_buf(dev, cd->pr_buf, si->xy_data, num_cur_tch *
1021 si->si_ofs.tch_rec_size, "xy_data");
1022
1023 /* check any error conditions */
1024 if (IS_BAD_PKT(rep_stat)) {
1025 dev_dbg(dev, "%s: Invalid buffer detected\n", __func__);
1026 rc = 0;
1027 goto cyttsp4_xy_worker_exit;
1028 }
1029
1030 if (IS_LARGE_AREA(tt_stat))
1031 dev_dbg(dev, "%s: Large area detected\n", __func__);
1032
1033 if (num_cur_tch > si->si_ofs.max_tchs) {
1034 dev_err(dev, "%s: too many tch; set to max tch (n=%d c=%zd)\n",
1035 __func__, num_cur_tch, si->si_ofs.max_tchs);
1036 num_cur_tch = si->si_ofs.max_tchs;
1037 }
1038
1039 /* extract xy_data for all currently reported touches */
1040 dev_vdbg(dev, "%s: extract data num_cur_tch=%d\n", __func__,
1041 num_cur_tch);
1042 if (num_cur_tch)
1043 cyttsp4_get_mt_touches(md, num_cur_tch);
1044 else
1045 cyttsp4_lift_all(md);
1046
1047 rc = 0;
1048
1049cyttsp4_xy_worker_exit:
1050 return rc;
1051}
1052
1053static int cyttsp4_mt_attention(struct cyttsp4 *cd)
1054{
1055 struct device *dev = cd->dev;
1056 struct cyttsp4_mt_data *md = &cd->md;
1057 int rc = 0;
1058
1059 if (!md->si)
1060 return 0;
1061
1062 mutex_lock(&md->report_lock);
1063 if (!md->is_suspended) {
1064 /* core handles handshake */
1065 rc = cyttsp4_xy_worker(cd);
1066 } else {
1067 dev_vdbg(dev, "%s: Ignoring report while suspended\n",
1068 __func__);
1069 }
1070 mutex_unlock(&md->report_lock);
1071 if (rc < 0)
1072 dev_err(dev, "%s: xy_worker error r=%d\n", __func__, rc);
1073
1074 return rc;
1075}
1076
1077static irqreturn_t cyttsp4_irq(int irq, void *handle)
1078{
1079 struct cyttsp4 *cd = handle;
1080 struct device *dev = cd->dev;
1081 enum cyttsp4_mode cur_mode;
1082 u8 cmd_ofs = cd->sysinfo.si_ofs.cmd_ofs;
1083 u8 mode[3];
1084 int rc;
1085
1086 /*
1087 * Check whether this IRQ should be ignored (external)
1088 * This should be the very first thing to check since
1089 * ignore_irq may be set for a very short period of time
1090 */
1091 if (atomic_read(&cd->ignore_irq)) {
1092 dev_vdbg(dev, "%s: Ignoring IRQ\n", __func__);
1093 return IRQ_HANDLED;
1094 }
1095
1096 dev_dbg(dev, "%s int:0x%x\n", __func__, cd->int_status);
1097
1098 mutex_lock(&cd->system_lock);
1099
1100 /* Just to debug */
1101 if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEPING)
1102 dev_vdbg(dev, "%s: Received IRQ while in sleep\n", __func__);
1103
1104 rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), mode);
1105 if (rc) {
1106 dev_err(cd->dev, "%s: Fail read adapter r=%d\n", __func__, rc);
1107 goto cyttsp4_irq_exit;
1108 }
1109 dev_vdbg(dev, "%s mode[0-2]:0x%X 0x%X 0x%X\n", __func__,
1110 mode[0], mode[1], mode[2]);
1111
1112 if (IS_BOOTLOADER(mode[0], mode[1])) {
1113 cur_mode = CY_MODE_BOOTLOADER;
1114 dev_vdbg(dev, "%s: bl running\n", __func__);
1115 if (cd->mode == CY_MODE_BOOTLOADER) {
1116 /* Signal bootloader heartbeat heard */
1117 wake_up(&cd->wait_q);
1118 goto cyttsp4_irq_exit;
1119 }
1120
1121 /* switch to bootloader */
1122 dev_dbg(dev, "%s: restart switch to bl m=%d -> m=%d\n",
1123 __func__, cd->mode, cur_mode);
1124
1125 /* catch operation->bl glitch */
1126 if (cd->mode != CY_MODE_UNKNOWN) {
1127 /* Incase startup_state do not let startup_() */
1128 cd->mode = CY_MODE_UNKNOWN;
1129 cyttsp4_queue_startup_(cd);
1130 goto cyttsp4_irq_exit;
1131 }
1132
1133 /*
1134 * do not wake thread on this switch since
1135 * it is possible to get an early heartbeat
1136 * prior to performing the reset
1137 */
1138 cd->mode = cur_mode;
1139
1140 goto cyttsp4_irq_exit;
1141 }
1142
1143 switch (mode[0] & CY_HST_MODE) {
1144 case CY_HST_OPERATE:
1145 cur_mode = CY_MODE_OPERATIONAL;
1146 dev_vdbg(dev, "%s: operational\n", __func__);
1147 break;
1148 case CY_HST_CAT:
1149 cur_mode = CY_MODE_CAT;
1150 dev_vdbg(dev, "%s: CaT\n", __func__);
1151 break;
1152 case CY_HST_SYSINFO:
1153 cur_mode = CY_MODE_SYSINFO;
1154 dev_vdbg(dev, "%s: sysinfo\n", __func__);
1155 break;
1156 default:
1157 cur_mode = CY_MODE_UNKNOWN;
1158 dev_err(dev, "%s: unknown HST mode 0x%02X\n", __func__,
1159 mode[0]);
1160 break;
1161 }
1162
1163 /* Check whether this IRQ should be ignored (internal) */
1164 if (cd->int_status & CY_INT_IGNORE) {
1165 dev_vdbg(dev, "%s: Ignoring IRQ\n", __func__);
1166 goto cyttsp4_irq_exit;
1167 }
1168
1169 /* Check for wake up interrupt */
1170 if (cd->int_status & CY_INT_AWAKE) {
1171 cd->int_status &= ~CY_INT_AWAKE;
1172 wake_up(&cd->wait_q);
1173 dev_vdbg(dev, "%s: Received wake up interrupt\n", __func__);
1174 goto cyttsp4_irq_handshake;
1175 }
1176
1177 /* Expecting mode change interrupt */
1178 if ((cd->int_status & CY_INT_MODE_CHANGE)
1179 && (mode[0] & CY_HST_MODE_CHANGE) == 0) {
1180 cd->int_status &= ~CY_INT_MODE_CHANGE;
1181 dev_dbg(dev, "%s: finish mode switch m=%d -> m=%d\n",
1182 __func__, cd->mode, cur_mode);
1183 cd->mode = cur_mode;
1184 wake_up(&cd->wait_q);
1185 goto cyttsp4_irq_handshake;
1186 }
1187
1188 /* compare current core mode to current device mode */
1189 dev_vdbg(dev, "%s: cd->mode=%d cur_mode=%d\n",
1190 __func__, cd->mode, cur_mode);
1191 if ((mode[0] & CY_HST_MODE_CHANGE) == 0 && cd->mode != cur_mode) {
1192 /* Unexpected mode change occurred */
1193 dev_err(dev, "%s %d->%d 0x%x\n", __func__, cd->mode,
1194 cur_mode, cd->int_status);
1195 dev_dbg(dev, "%s: Unexpected mode change, startup\n",
1196 __func__);
1197 cyttsp4_queue_startup_(cd);
1198 goto cyttsp4_irq_exit;
1199 }
1200
1201 /* Expecting command complete interrupt */
1202 dev_vdbg(dev, "%s: command byte:0x%x\n", __func__, mode[cmd_ofs]);
1203 if ((cd->int_status & CY_INT_EXEC_CMD)
1204 && mode[cmd_ofs] & CY_CMD_COMPLETE) {
1205 cd->int_status &= ~CY_INT_EXEC_CMD;
1206 dev_vdbg(dev, "%s: Received command complete interrupt\n",
1207 __func__);
1208 wake_up(&cd->wait_q);
1209 /*
1210 * It is possible to receive a single interrupt for
1211 * command complete and touch/button status report.
1212 * Continue processing for a possible status report.
1213 */
1214 }
1215
1216 /* This should be status report, read status regs */
1217 if (cd->mode == CY_MODE_OPERATIONAL) {
1218 dev_vdbg(dev, "%s: Read status registers\n", __func__);
1219 rc = cyttsp4_load_status_regs(cd);
1220 if (rc < 0)
1221 dev_err(dev, "%s: fail read mode regs r=%d\n",
1222 __func__, rc);
1223 }
1224
1225 cyttsp4_mt_attention(cd);
1226
1227cyttsp4_irq_handshake:
1228 /* handshake the event */
1229 dev_vdbg(dev, "%s: Handshake mode=0x%02X r=%d\n",
1230 __func__, mode[0], rc);
1231 rc = cyttsp4_handshake(cd, mode[0]);
1232 if (rc < 0)
1233 dev_err(dev, "%s: Fail handshake mode=0x%02X r=%d\n",
1234 __func__, mode[0], rc);
1235
1236 /*
1237 * a non-zero udelay period is required for using
1238 * IRQF_TRIGGER_LOW in order to delay until the
1239 * device completes isr deassert
1240 */
1241 udelay(cd->cpdata->level_irq_udelay);
1242
1243cyttsp4_irq_exit:
1244 mutex_unlock(&cd->system_lock);
1245 return IRQ_HANDLED;
1246}
1247
1248static void cyttsp4_start_wd_timer(struct cyttsp4 *cd)
1249{
1250 if (!CY_WATCHDOG_TIMEOUT)
1251 return;
1252
1253 mod_timer(&cd->watchdog_timer, jiffies +
1254 msecs_to_jiffies(CY_WATCHDOG_TIMEOUT));
1255}
1256
1257static void cyttsp4_stop_wd_timer(struct cyttsp4 *cd)
1258{
1259 if (!CY_WATCHDOG_TIMEOUT)
1260 return;
1261
1262 /*
1263 * Ensure we wait until the watchdog timer
1264 * running on a different CPU finishes
1265 */
1266 timer_shutdown_sync(&cd->watchdog_timer);
1267 cancel_work_sync(&cd->watchdog_work);
1268}
1269
1270static void cyttsp4_watchdog_timer(struct timer_list *t)
1271{
1272 struct cyttsp4 *cd = from_timer(cd, t, watchdog_timer);
1273
1274 dev_vdbg(cd->dev, "%s: Watchdog timer triggered\n", __func__);
1275
1276 schedule_work(&cd->watchdog_work);
1277
1278 return;
1279}
1280
1281static int cyttsp4_request_exclusive(struct cyttsp4 *cd, void *ownptr,
1282 int timeout_ms)
1283{
1284 int t = msecs_to_jiffies(timeout_ms);
1285 bool with_timeout = (timeout_ms != 0);
1286
1287 mutex_lock(&cd->system_lock);
1288 if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
1289 cd->exclusive_dev = ownptr;
1290 goto exit;
1291 }
1292
1293 cd->exclusive_waits++;
1294wait:
1295 mutex_unlock(&cd->system_lock);
1296 if (with_timeout) {
1297 t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
1298 if (IS_TMO(t)) {
1299 dev_err(cd->dev, "%s: tmo waiting exclusive access\n",
1300 __func__);
1301 mutex_lock(&cd->system_lock);
1302 cd->exclusive_waits--;
1303 mutex_unlock(&cd->system_lock);
1304 return -ETIME;
1305 }
1306 } else {
1307 wait_event(cd->wait_q, !cd->exclusive_dev);
1308 }
1309 mutex_lock(&cd->system_lock);
1310 if (cd->exclusive_dev)
1311 goto wait;
1312 cd->exclusive_dev = ownptr;
1313 cd->exclusive_waits--;
1314exit:
1315 mutex_unlock(&cd->system_lock);
1316
1317 return 0;
1318}
1319
1320/*
1321 * returns error if was not owned
1322 */
1323static int cyttsp4_release_exclusive(struct cyttsp4 *cd, void *ownptr)
1324{
1325 mutex_lock(&cd->system_lock);
1326 if (cd->exclusive_dev != ownptr) {
1327 mutex_unlock(&cd->system_lock);
1328 return -EINVAL;
1329 }
1330
1331 dev_vdbg(cd->dev, "%s: exclusive_dev %p freed\n",
1332 __func__, cd->exclusive_dev);
1333 cd->exclusive_dev = NULL;
1334 wake_up(&cd->wait_q);
1335 mutex_unlock(&cd->system_lock);
1336 return 0;
1337}
1338
1339static int cyttsp4_wait_bl_heartbeat(struct cyttsp4 *cd)
1340{
1341 long t;
1342 int rc = 0;
1343
1344 /* wait heartbeat */
1345 dev_vdbg(cd->dev, "%s: wait heartbeat...\n", __func__);
1346 t = wait_event_timeout(cd->wait_q, cd->mode == CY_MODE_BOOTLOADER,
1347 msecs_to_jiffies(CY_CORE_RESET_AND_WAIT_TIMEOUT));
1348 if (IS_TMO(t)) {
1349 dev_err(cd->dev, "%s: tmo waiting bl heartbeat cd->mode=%d\n",
1350 __func__, cd->mode);
1351 rc = -ETIME;
1352 }
1353
1354 return rc;
1355}
1356
1357static int cyttsp4_wait_sysinfo_mode(struct cyttsp4 *cd)
1358{
1359 long t;
1360
1361 dev_vdbg(cd->dev, "%s: wait sysinfo...\n", __func__);
1362
1363 t = wait_event_timeout(cd->wait_q, cd->mode == CY_MODE_SYSINFO,
1364 msecs_to_jiffies(CY_CORE_MODE_CHANGE_TIMEOUT));
1365 if (IS_TMO(t)) {
1366 dev_err(cd->dev, "%s: tmo waiting exit bl cd->mode=%d\n",
1367 __func__, cd->mode);
1368 mutex_lock(&cd->system_lock);
1369 cd->int_status &= ~CY_INT_MODE_CHANGE;
1370 mutex_unlock(&cd->system_lock);
1371 return -ETIME;
1372 }
1373
1374 return 0;
1375}
1376
1377static int cyttsp4_reset_and_wait(struct cyttsp4 *cd)
1378{
1379 int rc;
1380
1381 /* reset hardware */
1382 mutex_lock(&cd->system_lock);
1383 dev_dbg(cd->dev, "%s: reset hw...\n", __func__);
1384 rc = cyttsp4_hw_reset(cd);
1385 cd->mode = CY_MODE_UNKNOWN;
1386 mutex_unlock(&cd->system_lock);
1387 if (rc < 0) {
1388 dev_err(cd->dev, "%s:Fail hw reset r=%d\n", __func__, rc);
1389 return rc;
1390 }
1391
1392 return cyttsp4_wait_bl_heartbeat(cd);
1393}
1394
1395/*
1396 * returns err if refused or timeout; block until mode change complete
1397 * bit is set (mode change interrupt)
1398 */
1399static int cyttsp4_set_mode(struct cyttsp4 *cd, int new_mode)
1400{
1401 u8 new_dev_mode;
1402 u8 mode;
1403 long t;
1404 int rc;
1405
1406 switch (new_mode) {
1407 case CY_MODE_OPERATIONAL:
1408 new_dev_mode = CY_HST_OPERATE;
1409 break;
1410 case CY_MODE_SYSINFO:
1411 new_dev_mode = CY_HST_SYSINFO;
1412 break;
1413 case CY_MODE_CAT:
1414 new_dev_mode = CY_HST_CAT;
1415 break;
1416 default:
1417 dev_err(cd->dev, "%s: invalid mode: %02X(%d)\n",
1418 __func__, new_mode, new_mode);
1419 return -EINVAL;
1420 }
1421
1422 /* change mode */
1423 dev_dbg(cd->dev, "%s: %s=%p new_dev_mode=%02X new_mode=%d\n",
1424 __func__, "have exclusive", cd->exclusive_dev,
1425 new_dev_mode, new_mode);
1426
1427 mutex_lock(&cd->system_lock);
1428 rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), &mode);
1429 if (rc < 0) {
1430 mutex_unlock(&cd->system_lock);
1431 dev_err(cd->dev, "%s: Fail read mode r=%d\n",
1432 __func__, rc);
1433 goto exit;
1434 }
1435
1436 /* Clear device mode bits and set to new mode */
1437 mode &= ~CY_HST_MODE;
1438 mode |= new_dev_mode | CY_HST_MODE_CHANGE;
1439
1440 cd->int_status |= CY_INT_MODE_CHANGE;
1441 rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(mode), &mode);
1442 mutex_unlock(&cd->system_lock);
1443 if (rc < 0) {
1444 dev_err(cd->dev, "%s: Fail write mode change r=%d\n",
1445 __func__, rc);
1446 goto exit;
1447 }
1448
1449 /* wait for mode change done interrupt */
1450 t = wait_event_timeout(cd->wait_q,
1451 (cd->int_status & CY_INT_MODE_CHANGE) == 0,
1452 msecs_to_jiffies(CY_CORE_MODE_CHANGE_TIMEOUT));
1453 dev_dbg(cd->dev, "%s: back from wait t=%ld cd->mode=%d\n",
1454 __func__, t, cd->mode);
1455
1456 if (IS_TMO(t)) {
1457 dev_err(cd->dev, "%s: %s\n", __func__,
1458 "tmo waiting mode change");
1459 mutex_lock(&cd->system_lock);
1460 cd->int_status &= ~CY_INT_MODE_CHANGE;
1461 mutex_unlock(&cd->system_lock);
1462 rc = -EINVAL;
1463 }
1464
1465exit:
1466 return rc;
1467}
1468
1469static void cyttsp4_watchdog_work(struct work_struct *work)
1470{
1471 struct cyttsp4 *cd =
1472 container_of(work, struct cyttsp4, watchdog_work);
1473 u8 *mode;
1474 int retval;
1475
1476 mutex_lock(&cd->system_lock);
1477 retval = cyttsp4_load_status_regs(cd);
1478 if (retval < 0) {
1479 dev_err(cd->dev,
1480 "%s: failed to access device in watchdog timer r=%d\n",
1481 __func__, retval);
1482 cyttsp4_queue_startup_(cd);
1483 goto cyttsp4_timer_watchdog_exit_error;
1484 }
1485 mode = &cd->sysinfo.xy_mode[CY_REG_BASE];
1486 if (IS_BOOTLOADER(mode[0], mode[1])) {
1487 dev_err(cd->dev,
1488 "%s: device found in bootloader mode when operational mode\n",
1489 __func__);
1490 cyttsp4_queue_startup_(cd);
1491 goto cyttsp4_timer_watchdog_exit_error;
1492 }
1493
1494 cyttsp4_start_wd_timer(cd);
1495cyttsp4_timer_watchdog_exit_error:
1496 mutex_unlock(&cd->system_lock);
1497 return;
1498}
1499
1500static int cyttsp4_core_sleep_(struct cyttsp4 *cd)
1501{
1502 enum cyttsp4_sleep_state ss = SS_SLEEP_ON;
1503 enum cyttsp4_int_state int_status = CY_INT_IGNORE;
1504 int rc = 0;
1505 u8 mode[2];
1506
1507 /* Already in sleep mode? */
1508 mutex_lock(&cd->system_lock);
1509 if (cd->sleep_state == SS_SLEEP_ON) {
1510 mutex_unlock(&cd->system_lock);
1511 return 0;
1512 }
1513 cd->sleep_state = SS_SLEEPING;
1514 mutex_unlock(&cd->system_lock);
1515
1516 cyttsp4_stop_wd_timer(cd);
1517
1518 /* Wait until currently running IRQ handler exits and disable IRQ */
1519 disable_irq(cd->irq);
1520
1521 dev_vdbg(cd->dev, "%s: write DEEP SLEEP...\n", __func__);
1522 mutex_lock(&cd->system_lock);
1523 rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), &mode);
1524 if (rc) {
1525 mutex_unlock(&cd->system_lock);
1526 dev_err(cd->dev, "%s: Fail read adapter r=%d\n", __func__, rc);
1527 goto error;
1528 }
1529
1530 if (IS_BOOTLOADER(mode[0], mode[1])) {
1531 mutex_unlock(&cd->system_lock);
1532 dev_err(cd->dev, "%s: Device in BOOTLOADER mode.\n", __func__);
1533 rc = -EINVAL;
1534 goto error;
1535 }
1536
1537 mode[0] |= CY_HST_SLEEP;
1538 rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(mode[0]), &mode[0]);
1539 mutex_unlock(&cd->system_lock);
1540 if (rc) {
1541 dev_err(cd->dev, "%s: Fail write adapter r=%d\n", __func__, rc);
1542 goto error;
1543 }
1544 dev_vdbg(cd->dev, "%s: write DEEP SLEEP succeeded\n", __func__);
1545
1546 if (cd->cpdata->power) {
1547 dev_dbg(cd->dev, "%s: Power down HW\n", __func__);
1548 rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, &cd->ignore_irq);
1549 } else {
1550 dev_dbg(cd->dev, "%s: No power function\n", __func__);
1551 rc = 0;
1552 }
1553 if (rc < 0) {
1554 dev_err(cd->dev, "%s: HW Power down fails r=%d\n",
1555 __func__, rc);
1556 goto error;
1557 }
1558
1559 /* Give time to FW to sleep */
1560 msleep(50);
1561
1562 goto exit;
1563
1564error:
1565 ss = SS_SLEEP_OFF;
1566 int_status = CY_INT_NONE;
1567 cyttsp4_start_wd_timer(cd);
1568
1569exit:
1570 mutex_lock(&cd->system_lock);
1571 cd->sleep_state = ss;
1572 cd->int_status |= int_status;
1573 mutex_unlock(&cd->system_lock);
1574 enable_irq(cd->irq);
1575 return rc;
1576}
1577
1578static int cyttsp4_startup_(struct cyttsp4 *cd)
1579{
1580 int retry = CY_CORE_STARTUP_RETRY_COUNT;
1581 int rc;
1582
1583 cyttsp4_stop_wd_timer(cd);
1584
1585reset:
1586 if (retry != CY_CORE_STARTUP_RETRY_COUNT)
1587 dev_dbg(cd->dev, "%s: Retry %d\n", __func__,
1588 CY_CORE_STARTUP_RETRY_COUNT - retry);
1589
1590 /* reset hardware and wait for heartbeat */
1591 rc = cyttsp4_reset_and_wait(cd);
1592 if (rc < 0) {
1593 dev_err(cd->dev, "%s: Error on h/w reset r=%d\n", __func__, rc);
1594 if (retry--)
1595 goto reset;
1596 goto exit;
1597 }
1598
1599 /* exit bl into sysinfo mode */
1600 dev_vdbg(cd->dev, "%s: write exit ldr...\n", __func__);
1601 mutex_lock(&cd->system_lock);
1602 cd->int_status &= ~CY_INT_IGNORE;
1603 cd->int_status |= CY_INT_MODE_CHANGE;
1604
1605 rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(ldr_exit),
1606 (u8 *)ldr_exit);
1607 mutex_unlock(&cd->system_lock);
1608 if (rc < 0) {
1609 dev_err(cd->dev, "%s: Fail write r=%d\n", __func__, rc);
1610 if (retry--)
1611 goto reset;
1612 goto exit;
1613 }
1614
1615 rc = cyttsp4_wait_sysinfo_mode(cd);
1616 if (rc < 0) {
1617 u8 buf[sizeof(ldr_err_app)];
1618 int rc1;
1619
1620 /* Check for invalid/corrupted touch application */
1621 rc1 = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(ldr_err_app),
1622 buf);
1623 if (rc1) {
1624 dev_err(cd->dev, "%s: Fail read r=%d\n", __func__, rc1);
1625 } else if (!memcmp(buf, ldr_err_app, sizeof(ldr_err_app))) {
1626 dev_err(cd->dev, "%s: Error launching touch application\n",
1627 __func__);
1628 mutex_lock(&cd->system_lock);
1629 cd->invalid_touch_app = true;
1630 mutex_unlock(&cd->system_lock);
1631 goto exit_no_wd;
1632 }
1633
1634 if (retry--)
1635 goto reset;
1636 goto exit;
1637 }
1638
1639 mutex_lock(&cd->system_lock);
1640 cd->invalid_touch_app = false;
1641 mutex_unlock(&cd->system_lock);
1642
1643 /* read sysinfo data */
1644 dev_vdbg(cd->dev, "%s: get sysinfo regs..\n", __func__);
1645 rc = cyttsp4_get_sysinfo_regs(cd);
1646 if (rc < 0) {
1647 dev_err(cd->dev, "%s: failed to get sysinfo regs rc=%d\n",
1648 __func__, rc);
1649 if (retry--)
1650 goto reset;
1651 goto exit;
1652 }
1653
1654 rc = cyttsp4_set_mode(cd, CY_MODE_OPERATIONAL);
1655 if (rc < 0) {
1656 dev_err(cd->dev, "%s: failed to set mode to operational rc=%d\n",
1657 __func__, rc);
1658 if (retry--)
1659 goto reset;
1660 goto exit;
1661 }
1662
1663 cyttsp4_lift_all(&cd->md);
1664
1665 /* restore to sleep if was suspended */
1666 mutex_lock(&cd->system_lock);
1667 if (cd->sleep_state == SS_SLEEP_ON) {
1668 cd->sleep_state = SS_SLEEP_OFF;
1669 mutex_unlock(&cd->system_lock);
1670 cyttsp4_core_sleep_(cd);
1671 goto exit_no_wd;
1672 }
1673 mutex_unlock(&cd->system_lock);
1674
1675exit:
1676 cyttsp4_start_wd_timer(cd);
1677exit_no_wd:
1678 return rc;
1679}
1680
1681static int cyttsp4_startup(struct cyttsp4 *cd)
1682{
1683 int rc;
1684
1685 mutex_lock(&cd->system_lock);
1686 cd->startup_state = STARTUP_RUNNING;
1687 mutex_unlock(&cd->system_lock);
1688
1689 rc = cyttsp4_request_exclusive(cd, cd->dev,
1690 CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT);
1691 if (rc < 0) {
1692 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
1693 __func__, cd->exclusive_dev, cd->dev);
1694 goto exit;
1695 }
1696
1697 rc = cyttsp4_startup_(cd);
1698
1699 if (cyttsp4_release_exclusive(cd, cd->dev) < 0)
1700 /* Don't return fail code, mode is already changed. */
1701 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
1702 else
1703 dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
1704
1705exit:
1706 mutex_lock(&cd->system_lock);
1707 cd->startup_state = STARTUP_NONE;
1708 mutex_unlock(&cd->system_lock);
1709
1710 /* Wake the waiters for end of startup */
1711 wake_up(&cd->wait_q);
1712
1713 return rc;
1714}
1715
1716static void cyttsp4_startup_work_function(struct work_struct *work)
1717{
1718 struct cyttsp4 *cd = container_of(work, struct cyttsp4, startup_work);
1719 int rc;
1720
1721 rc = cyttsp4_startup(cd);
1722 if (rc < 0)
1723 dev_err(cd->dev, "%s: Fail queued startup r=%d\n",
1724 __func__, rc);
1725}
1726
1727static void cyttsp4_free_si_ptrs(struct cyttsp4 *cd)
1728{
1729 struct cyttsp4_sysinfo *si = &cd->sysinfo;
1730
1731 if (!si)
1732 return;
1733
1734 kfree(si->si_ptrs.cydata);
1735 kfree(si->si_ptrs.test);
1736 kfree(si->si_ptrs.pcfg);
1737 kfree(si->si_ptrs.opcfg);
1738 kfree(si->si_ptrs.ddata);
1739 kfree(si->si_ptrs.mdata);
1740 kfree(si->btn);
1741 kfree(si->xy_mode);
1742 kfree(si->xy_data);
1743 kfree(si->btn_rec_data);
1744}
1745
1746static int cyttsp4_core_sleep(struct cyttsp4 *cd)
1747{
1748 int rc;
1749
1750 rc = cyttsp4_request_exclusive(cd, cd->dev,
1751 CY_CORE_SLEEP_REQUEST_EXCLUSIVE_TIMEOUT);
1752 if (rc < 0) {
1753 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
1754 __func__, cd->exclusive_dev, cd->dev);
1755 return 0;
1756 }
1757
1758 rc = cyttsp4_core_sleep_(cd);
1759
1760 if (cyttsp4_release_exclusive(cd, cd->dev) < 0)
1761 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
1762 else
1763 dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
1764
1765 return rc;
1766}
1767
1768static int cyttsp4_core_wake_(struct cyttsp4 *cd)
1769{
1770 struct device *dev = cd->dev;
1771 int rc;
1772 u8 mode;
1773 int t;
1774
1775 /* Already woken? */
1776 mutex_lock(&cd->system_lock);
1777 if (cd->sleep_state == SS_SLEEP_OFF) {
1778 mutex_unlock(&cd->system_lock);
1779 return 0;
1780 }
1781 cd->int_status &= ~CY_INT_IGNORE;
1782 cd->int_status |= CY_INT_AWAKE;
1783 cd->sleep_state = SS_WAKING;
1784
1785 if (cd->cpdata->power) {
1786 dev_dbg(dev, "%s: Power up HW\n", __func__);
1787 rc = cd->cpdata->power(cd->cpdata, 1, dev, &cd->ignore_irq);
1788 } else {
1789 dev_dbg(dev, "%s: No power function\n", __func__);
1790 rc = -ENOSYS;
1791 }
1792 if (rc < 0) {
1793 dev_err(dev, "%s: HW Power up fails r=%d\n",
1794 __func__, rc);
1795
1796 /* Initiate a read transaction to wake up */
1797 cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), &mode);
1798 } else
1799 dev_vdbg(cd->dev, "%s: HW power up succeeds\n",
1800 __func__);
1801 mutex_unlock(&cd->system_lock);
1802
1803 t = wait_event_timeout(cd->wait_q,
1804 (cd->int_status & CY_INT_AWAKE) == 0,
1805 msecs_to_jiffies(CY_CORE_WAKEUP_TIMEOUT));
1806 if (IS_TMO(t)) {
1807 dev_err(dev, "%s: TMO waiting for wakeup\n", __func__);
1808 mutex_lock(&cd->system_lock);
1809 cd->int_status &= ~CY_INT_AWAKE;
1810 /* Try starting up */
1811 cyttsp4_queue_startup_(cd);
1812 mutex_unlock(&cd->system_lock);
1813 }
1814
1815 mutex_lock(&cd->system_lock);
1816 cd->sleep_state = SS_SLEEP_OFF;
1817 mutex_unlock(&cd->system_lock);
1818
1819 cyttsp4_start_wd_timer(cd);
1820
1821 return 0;
1822}
1823
1824static int cyttsp4_core_wake(struct cyttsp4 *cd)
1825{
1826 int rc;
1827
1828 rc = cyttsp4_request_exclusive(cd, cd->dev,
1829 CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT);
1830 if (rc < 0) {
1831 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
1832 __func__, cd->exclusive_dev, cd->dev);
1833 return 0;
1834 }
1835
1836 rc = cyttsp4_core_wake_(cd);
1837
1838 if (cyttsp4_release_exclusive(cd, cd->dev) < 0)
1839 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
1840 else
1841 dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
1842
1843 return rc;
1844}
1845
1846static int cyttsp4_core_suspend(struct device *dev)
1847{
1848 struct cyttsp4 *cd = dev_get_drvdata(dev);
1849 struct cyttsp4_mt_data *md = &cd->md;
1850 int rc;
1851
1852 md->is_suspended = true;
1853
1854 rc = cyttsp4_core_sleep(cd);
1855 if (rc < 0) {
1856 dev_err(dev, "%s: Error on sleep\n", __func__);
1857 return -EAGAIN;
1858 }
1859 return 0;
1860}
1861
1862static int cyttsp4_core_resume(struct device *dev)
1863{
1864 struct cyttsp4 *cd = dev_get_drvdata(dev);
1865 struct cyttsp4_mt_data *md = &cd->md;
1866 int rc;
1867
1868 md->is_suspended = false;
1869
1870 rc = cyttsp4_core_wake(cd);
1871 if (rc < 0) {
1872 dev_err(dev, "%s: Error on wake\n", __func__);
1873 return -EAGAIN;
1874 }
1875
1876 return 0;
1877}
1878
1879EXPORT_GPL_RUNTIME_DEV_PM_OPS(cyttsp4_pm_ops,
1880 cyttsp4_core_suspend, cyttsp4_core_resume, NULL);
1881
1882static int cyttsp4_mt_open(struct input_dev *input)
1883{
1884 pm_runtime_get(input->dev.parent);
1885 return 0;
1886}
1887
1888static void cyttsp4_mt_close(struct input_dev *input)
1889{
1890 struct cyttsp4_mt_data *md = input_get_drvdata(input);
1891 mutex_lock(&md->report_lock);
1892 if (!md->is_suspended)
1893 pm_runtime_put(input->dev.parent);
1894 mutex_unlock(&md->report_lock);
1895}
1896
1897
1898static int cyttsp4_setup_input_device(struct cyttsp4 *cd)
1899{
1900 struct device *dev = cd->dev;
1901 struct cyttsp4_mt_data *md = &cd->md;
1902 int signal = CY_IGNORE_VALUE;
1903 int max_x, max_y, max_p, min, max;
1904 int max_x_tmp, max_y_tmp;
1905 int i;
1906 int rc;
1907
1908 dev_vdbg(dev, "%s: Initialize event signals\n", __func__);
1909 __set_bit(EV_ABS, md->input->evbit);
1910 __set_bit(EV_REL, md->input->evbit);
1911 __set_bit(EV_KEY, md->input->evbit);
1912
1913 max_x_tmp = md->si->si_ofs.max_x;
1914 max_y_tmp = md->si->si_ofs.max_y;
1915
1916 /* get maximum values from the sysinfo data */
1917 if (md->pdata->flags & CY_FLAG_FLIP) {
1918 max_x = max_y_tmp - 1;
1919 max_y = max_x_tmp - 1;
1920 } else {
1921 max_x = max_x_tmp - 1;
1922 max_y = max_y_tmp - 1;
1923 }
1924 max_p = md->si->si_ofs.max_p;
1925
1926 /* set event signal capabilities */
1927 for (i = 0; i < (md->pdata->frmwrk->size / CY_NUM_ABS_SET); i++) {
1928 signal = md->pdata->frmwrk->abs
1929 [(i * CY_NUM_ABS_SET) + CY_SIGNAL_OST];
1930 if (signal != CY_IGNORE_VALUE) {
1931 __set_bit(signal, md->input->absbit);
1932 min = md->pdata->frmwrk->abs
1933 [(i * CY_NUM_ABS_SET) + CY_MIN_OST];
1934 max = md->pdata->frmwrk->abs
1935 [(i * CY_NUM_ABS_SET) + CY_MAX_OST];
1936 if (i == CY_ABS_ID_OST) {
1937 /* shift track ids down to start at 0 */
1938 max = max - min;
1939 min = min - min;
1940 } else if (i == CY_ABS_X_OST)
1941 max = max_x;
1942 else if (i == CY_ABS_Y_OST)
1943 max = max_y;
1944 else if (i == CY_ABS_P_OST)
1945 max = max_p;
1946 input_set_abs_params(md->input, signal, min, max,
1947 md->pdata->frmwrk->abs
1948 [(i * CY_NUM_ABS_SET) + CY_FUZZ_OST],
1949 md->pdata->frmwrk->abs
1950 [(i * CY_NUM_ABS_SET) + CY_FLAT_OST]);
1951 dev_dbg(dev, "%s: register signal=%02X min=%d max=%d\n",
1952 __func__, signal, min, max);
1953 if ((i == CY_ABS_ID_OST) &&
1954 (md->si->si_ofs.tch_rec_size <
1955 CY_TMA4XX_TCH_REC_SIZE))
1956 break;
1957 }
1958 }
1959
1960 input_mt_init_slots(md->input, md->si->si_ofs.tch_abs[CY_TCH_T].max,
1961 INPUT_MT_DIRECT);
1962 rc = input_register_device(md->input);
1963 if (rc < 0)
1964 dev_err(dev, "%s: Error, failed register input device r=%d\n",
1965 __func__, rc);
1966 return rc;
1967}
1968
1969static int cyttsp4_mt_probe(struct cyttsp4 *cd)
1970{
1971 struct device *dev = cd->dev;
1972 struct cyttsp4_mt_data *md = &cd->md;
1973 struct cyttsp4_mt_platform_data *pdata = cd->pdata->mt_pdata;
1974 int rc = 0;
1975
1976 mutex_init(&md->report_lock);
1977 md->pdata = pdata;
1978 /* Create the input device and register it. */
1979 dev_vdbg(dev, "%s: Create the input device and register it\n",
1980 __func__);
1981 md->input = input_allocate_device();
1982 if (md->input == NULL) {
1983 dev_err(dev, "%s: Error, failed to allocate input device\n",
1984 __func__);
1985 rc = -ENOSYS;
1986 goto error_alloc_failed;
1987 }
1988
1989 md->input->name = pdata->inp_dev_name;
1990 scnprintf(md->phys, sizeof(md->phys)-1, "%s", dev_name(dev));
1991 md->input->phys = md->phys;
1992 md->input->id.bustype = cd->bus_ops->bustype;
1993 md->input->dev.parent = dev;
1994 md->input->open = cyttsp4_mt_open;
1995 md->input->close = cyttsp4_mt_close;
1996 input_set_drvdata(md->input, md);
1997
1998 /* get sysinfo */
1999 md->si = &cd->sysinfo;
2000
2001 rc = cyttsp4_setup_input_device(cd);
2002 if (rc)
2003 goto error_init_input;
2004
2005 return 0;
2006
2007error_init_input:
2008 input_free_device(md->input);
2009error_alloc_failed:
2010 dev_err(dev, "%s failed.\n", __func__);
2011 return rc;
2012}
2013
2014struct cyttsp4 *cyttsp4_probe(const struct cyttsp4_bus_ops *ops,
2015 struct device *dev, u16 irq, size_t xfer_buf_size)
2016{
2017 struct cyttsp4 *cd;
2018 struct cyttsp4_platform_data *pdata = dev_get_platdata(dev);
2019 unsigned long irq_flags;
2020 int rc = 0;
2021
2022 if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
2023 dev_err(dev, "%s: Missing platform data\n", __func__);
2024 rc = -ENODEV;
2025 goto error_no_pdata;
2026 }
2027
2028 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
2029 if (!cd) {
2030 dev_err(dev, "%s: Error, kzalloc\n", __func__);
2031 rc = -ENOMEM;
2032 goto error_alloc_data;
2033 }
2034
2035 cd->xfer_buf = kzalloc(xfer_buf_size, GFP_KERNEL);
2036 if (!cd->xfer_buf) {
2037 dev_err(dev, "%s: Error, kzalloc\n", __func__);
2038 rc = -ENOMEM;
2039 goto error_free_cd;
2040 }
2041
2042 /* Initialize device info */
2043 cd->dev = dev;
2044 cd->pdata = pdata;
2045 cd->cpdata = pdata->core_pdata;
2046 cd->bus_ops = ops;
2047
2048 /* Initialize mutexes and spinlocks */
2049 mutex_init(&cd->system_lock);
2050 mutex_init(&cd->adap_lock);
2051
2052 /* Initialize wait queue */
2053 init_waitqueue_head(&cd->wait_q);
2054
2055 /* Initialize works */
2056 INIT_WORK(&cd->startup_work, cyttsp4_startup_work_function);
2057 INIT_WORK(&cd->watchdog_work, cyttsp4_watchdog_work);
2058
2059 /* Initialize IRQ */
2060 cd->irq = gpio_to_irq(cd->cpdata->irq_gpio);
2061 if (cd->irq < 0) {
2062 rc = -EINVAL;
2063 goto error_free_xfer;
2064 }
2065
2066 dev_set_drvdata(dev, cd);
2067
2068 /* Call platform init function */
2069 if (cd->cpdata->init) {
2070 dev_dbg(cd->dev, "%s: Init HW\n", __func__);
2071 rc = cd->cpdata->init(cd->cpdata, 1, cd->dev);
2072 } else {
2073 dev_dbg(cd->dev, "%s: No HW INIT function\n", __func__);
2074 rc = 0;
2075 }
2076 if (rc < 0)
2077 dev_err(cd->dev, "%s: HW Init fail r=%d\n", __func__, rc);
2078
2079 dev_dbg(dev, "%s: initialize threaded irq=%d\n", __func__, cd->irq);
2080 if (cd->cpdata->level_irq_udelay > 0)
2081 /* use level triggered interrupts */
2082 irq_flags = IRQF_TRIGGER_LOW | IRQF_ONESHOT;
2083 else
2084 /* use edge triggered interrupts */
2085 irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
2086
2087 rc = request_threaded_irq(cd->irq, NULL, cyttsp4_irq, irq_flags,
2088 dev_name(dev), cd);
2089 if (rc < 0) {
2090 dev_err(dev, "%s: Error, could not request irq\n", __func__);
2091 goto error_request_irq;
2092 }
2093
2094 /* Setup watchdog timer */
2095 timer_setup(&cd->watchdog_timer, cyttsp4_watchdog_timer, 0);
2096
2097 /*
2098 * call startup directly to ensure that the device
2099 * is tested before leaving the probe
2100 */
2101 rc = cyttsp4_startup(cd);
2102
2103 /* Do not fail probe if startup fails but the device is detected */
2104 if (rc < 0 && cd->mode == CY_MODE_UNKNOWN) {
2105 dev_err(cd->dev, "%s: Fail initial startup r=%d\n",
2106 __func__, rc);
2107 goto error_startup;
2108 }
2109
2110 rc = cyttsp4_mt_probe(cd);
2111 if (rc < 0) {
2112 dev_err(dev, "%s: Error, fail mt probe\n", __func__);
2113 goto error_startup;
2114 }
2115
2116 pm_runtime_enable(dev);
2117
2118 return cd;
2119
2120error_startup:
2121 cancel_work_sync(&cd->startup_work);
2122 cyttsp4_stop_wd_timer(cd);
2123 pm_runtime_disable(dev);
2124 cyttsp4_free_si_ptrs(cd);
2125 free_irq(cd->irq, cd);
2126error_request_irq:
2127 if (cd->cpdata->init)
2128 cd->cpdata->init(cd->cpdata, 0, dev);
2129error_free_xfer:
2130 kfree(cd->xfer_buf);
2131error_free_cd:
2132 kfree(cd);
2133error_alloc_data:
2134error_no_pdata:
2135 dev_err(dev, "%s failed.\n", __func__);
2136 return ERR_PTR(rc);
2137}
2138EXPORT_SYMBOL_GPL(cyttsp4_probe);
2139
2140static void cyttsp4_mt_release(struct cyttsp4_mt_data *md)
2141{
2142 input_unregister_device(md->input);
2143 input_set_drvdata(md->input, NULL);
2144}
2145
2146int cyttsp4_remove(struct cyttsp4 *cd)
2147{
2148 struct device *dev = cd->dev;
2149
2150 cyttsp4_mt_release(&cd->md);
2151
2152 /*
2153 * Suspend the device before freeing the startup_work and stopping
2154 * the watchdog since sleep function restarts watchdog on failure
2155 */
2156 pm_runtime_suspend(dev);
2157 pm_runtime_disable(dev);
2158
2159 cancel_work_sync(&cd->startup_work);
2160
2161 cyttsp4_stop_wd_timer(cd);
2162
2163 free_irq(cd->irq, cd);
2164 if (cd->cpdata->init)
2165 cd->cpdata->init(cd->cpdata, 0, dev);
2166 cyttsp4_free_si_ptrs(cd);
2167 kfree(cd);
2168 return 0;
2169}
2170EXPORT_SYMBOL_GPL(cyttsp4_remove);
2171
2172MODULE_LICENSE("GPL");
2173MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard touchscreen core driver");
2174MODULE_AUTHOR("Cypress");
1/*
2 * cyttsp4_core.c
3 * Cypress TrueTouch(TM) Standard Product V4 Core driver module.
4 * For use with Cypress Txx4xx parts.
5 * Supported parts include:
6 * TMA4XX
7 * TMA1036
8 *
9 * Copyright (C) 2012 Cypress Semiconductor
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * version 2, and only version 2, as published by the
14 * Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * Contact Cypress Semiconductor at www.cypress.com <ttdrivers@cypress.com>
22 *
23 */
24
25#include "cyttsp4_core.h"
26#include <linux/delay.h>
27#include <linux/gpio.h>
28#include <linux/input/mt.h>
29#include <linux/interrupt.h>
30#include <linux/pm_runtime.h>
31#include <linux/sched.h>
32#include <linux/slab.h>
33
34/* Timeout in ms. */
35#define CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT 500
36#define CY_CORE_SLEEP_REQUEST_EXCLUSIVE_TIMEOUT 5000
37#define CY_CORE_MODE_CHANGE_TIMEOUT 1000
38#define CY_CORE_RESET_AND_WAIT_TIMEOUT 500
39#define CY_CORE_WAKEUP_TIMEOUT 500
40
41#define CY_CORE_STARTUP_RETRY_COUNT 3
42
43static const u8 ldr_exit[] = {
44 0xFF, 0x01, 0x3B, 0x00, 0x00, 0x4F, 0x6D, 0x17
45};
46
47static const u8 ldr_err_app[] = {
48 0x01, 0x02, 0x00, 0x00, 0x55, 0xDD, 0x17
49};
50
51static inline size_t merge_bytes(u8 high, u8 low)
52{
53 return (high << 8) + low;
54}
55
56#ifdef VERBOSE_DEBUG
57static void cyttsp4_pr_buf(struct device *dev, u8 *pr_buf, u8 *dptr, int size,
58 const char *data_name)
59{
60 int i, k;
61 const char fmt[] = "%02X ";
62 int max;
63
64 if (!size)
65 return;
66
67 max = (CY_MAX_PRBUF_SIZE - 1) - sizeof(CY_PR_TRUNCATED);
68
69 pr_buf[0] = 0;
70 for (i = k = 0; i < size && k < max; i++, k += 3)
71 scnprintf(pr_buf + k, CY_MAX_PRBUF_SIZE, fmt, dptr[i]);
72
73 dev_vdbg(dev, "%s: %s[0..%d]=%s%s\n", __func__, data_name, size - 1,
74 pr_buf, size <= max ? "" : CY_PR_TRUNCATED);
75}
76#else
77#define cyttsp4_pr_buf(dev, pr_buf, dptr, size, data_name) do { } while (0)
78#endif
79
80static int cyttsp4_load_status_regs(struct cyttsp4 *cd)
81{
82 struct cyttsp4_sysinfo *si = &cd->sysinfo;
83 struct device *dev = cd->dev;
84 int rc;
85
86 rc = cyttsp4_adap_read(cd, CY_REG_BASE, si->si_ofs.mode_size,
87 si->xy_mode);
88 if (rc < 0)
89 dev_err(dev, "%s: fail read mode regs r=%d\n",
90 __func__, rc);
91 else
92 cyttsp4_pr_buf(dev, cd->pr_buf, si->xy_mode,
93 si->si_ofs.mode_size, "xy_mode");
94
95 return rc;
96}
97
98static int cyttsp4_handshake(struct cyttsp4 *cd, u8 mode)
99{
100 u8 cmd = mode ^ CY_HST_TOGGLE;
101 int rc;
102
103 /*
104 * Mode change issued, handshaking now will cause endless mode change
105 * requests, for sync mode modechange will do same with handshake
106 * */
107 if (mode & CY_HST_MODE_CHANGE)
108 return 0;
109
110 rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(cmd), &cmd);
111 if (rc < 0)
112 dev_err(cd->dev, "%s: bus write fail on handshake (ret=%d)\n",
113 __func__, rc);
114
115 return rc;
116}
117
118static int cyttsp4_hw_soft_reset(struct cyttsp4 *cd)
119{
120 u8 cmd = CY_HST_RESET;
121 int rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(cmd), &cmd);
122 if (rc < 0) {
123 dev_err(cd->dev, "%s: FAILED to execute SOFT reset\n",
124 __func__);
125 return rc;
126 }
127 return 0;
128}
129
130static int cyttsp4_hw_hard_reset(struct cyttsp4 *cd)
131{
132 if (cd->cpdata->xres) {
133 cd->cpdata->xres(cd->cpdata, cd->dev);
134 dev_dbg(cd->dev, "%s: execute HARD reset\n", __func__);
135 return 0;
136 }
137 dev_err(cd->dev, "%s: FAILED to execute HARD reset\n", __func__);
138 return -ENOSYS;
139}
140
141static int cyttsp4_hw_reset(struct cyttsp4 *cd)
142{
143 int rc = cyttsp4_hw_hard_reset(cd);
144 if (rc == -ENOSYS)
145 rc = cyttsp4_hw_soft_reset(cd);
146 return rc;
147}
148
149/*
150 * Gets number of bits for a touch filed as parameter,
151 * sets maximum value for field which is used as bit mask
152 * and returns number of bytes required for that field
153 */
154static int cyttsp4_bits_2_bytes(unsigned int nbits, size_t *max)
155{
156 *max = 1UL << nbits;
157 return (nbits + 7) / 8;
158}
159
160static int cyttsp4_si_data_offsets(struct cyttsp4 *cd)
161{
162 struct cyttsp4_sysinfo *si = &cd->sysinfo;
163 int rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(si->si_data),
164 &si->si_data);
165 if (rc < 0) {
166 dev_err(cd->dev, "%s: fail read sysinfo data offsets r=%d\n",
167 __func__, rc);
168 return rc;
169 }
170
171 /* Print sysinfo data offsets */
172 cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)&si->si_data,
173 sizeof(si->si_data), "sysinfo_data_offsets");
174
175 /* convert sysinfo data offset bytes into integers */
176
177 si->si_ofs.map_sz = merge_bytes(si->si_data.map_szh,
178 si->si_data.map_szl);
179 si->si_ofs.map_sz = merge_bytes(si->si_data.map_szh,
180 si->si_data.map_szl);
181 si->si_ofs.cydata_ofs = merge_bytes(si->si_data.cydata_ofsh,
182 si->si_data.cydata_ofsl);
183 si->si_ofs.test_ofs = merge_bytes(si->si_data.test_ofsh,
184 si->si_data.test_ofsl);
185 si->si_ofs.pcfg_ofs = merge_bytes(si->si_data.pcfg_ofsh,
186 si->si_data.pcfg_ofsl);
187 si->si_ofs.opcfg_ofs = merge_bytes(si->si_data.opcfg_ofsh,
188 si->si_data.opcfg_ofsl);
189 si->si_ofs.ddata_ofs = merge_bytes(si->si_data.ddata_ofsh,
190 si->si_data.ddata_ofsl);
191 si->si_ofs.mdata_ofs = merge_bytes(si->si_data.mdata_ofsh,
192 si->si_data.mdata_ofsl);
193 return rc;
194}
195
196static int cyttsp4_si_get_cydata(struct cyttsp4 *cd)
197{
198 struct cyttsp4_sysinfo *si = &cd->sysinfo;
199 int read_offset;
200 int mfgid_sz, calc_mfgid_sz;
201 void *p;
202 int rc;
203
204 si->si_ofs.cydata_size = si->si_ofs.test_ofs - si->si_ofs.cydata_ofs;
205 dev_dbg(cd->dev, "%s: cydata size: %Zd\n", __func__,
206 si->si_ofs.cydata_size);
207
208 p = krealloc(si->si_ptrs.cydata, si->si_ofs.cydata_size, GFP_KERNEL);
209 if (p == NULL) {
210 dev_err(cd->dev, "%s: fail alloc cydata memory\n", __func__);
211 return -ENOMEM;
212 }
213 si->si_ptrs.cydata = p;
214
215 read_offset = si->si_ofs.cydata_ofs;
216
217 /* Read the CYDA registers up to MFGID field */
218 rc = cyttsp4_adap_read(cd, read_offset,
219 offsetof(struct cyttsp4_cydata, mfgid_sz)
220 + sizeof(si->si_ptrs.cydata->mfgid_sz),
221 si->si_ptrs.cydata);
222 if (rc < 0) {
223 dev_err(cd->dev, "%s: fail read cydata r=%d\n",
224 __func__, rc);
225 return rc;
226 }
227
228 /* Check MFGID size */
229 mfgid_sz = si->si_ptrs.cydata->mfgid_sz;
230 calc_mfgid_sz = si->si_ofs.cydata_size - sizeof(struct cyttsp4_cydata);
231 if (mfgid_sz != calc_mfgid_sz) {
232 dev_err(cd->dev, "%s: mismatch in MFGID size, reported:%d calculated:%d\n",
233 __func__, mfgid_sz, calc_mfgid_sz);
234 return -EINVAL;
235 }
236
237 read_offset += offsetof(struct cyttsp4_cydata, mfgid_sz)
238 + sizeof(si->si_ptrs.cydata->mfgid_sz);
239
240 /* Read the CYDA registers for MFGID field */
241 rc = cyttsp4_adap_read(cd, read_offset, si->si_ptrs.cydata->mfgid_sz,
242 si->si_ptrs.cydata->mfg_id);
243 if (rc < 0) {
244 dev_err(cd->dev, "%s: fail read cydata r=%d\n",
245 __func__, rc);
246 return rc;
247 }
248
249 read_offset += si->si_ptrs.cydata->mfgid_sz;
250
251 /* Read the rest of the CYDA registers */
252 rc = cyttsp4_adap_read(cd, read_offset,
253 sizeof(struct cyttsp4_cydata)
254 - offsetof(struct cyttsp4_cydata, cyito_idh),
255 &si->si_ptrs.cydata->cyito_idh);
256 if (rc < 0) {
257 dev_err(cd->dev, "%s: fail read cydata r=%d\n",
258 __func__, rc);
259 return rc;
260 }
261
262 cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)si->si_ptrs.cydata,
263 si->si_ofs.cydata_size, "sysinfo_cydata");
264 return rc;
265}
266
267static int cyttsp4_si_get_test_data(struct cyttsp4 *cd)
268{
269 struct cyttsp4_sysinfo *si = &cd->sysinfo;
270 void *p;
271 int rc;
272
273 si->si_ofs.test_size = si->si_ofs.pcfg_ofs - si->si_ofs.test_ofs;
274
275 p = krealloc(si->si_ptrs.test, si->si_ofs.test_size, GFP_KERNEL);
276 if (p == NULL) {
277 dev_err(cd->dev, "%s: fail alloc test memory\n", __func__);
278 return -ENOMEM;
279 }
280 si->si_ptrs.test = p;
281
282 rc = cyttsp4_adap_read(cd, si->si_ofs.test_ofs, si->si_ofs.test_size,
283 si->si_ptrs.test);
284 if (rc < 0) {
285 dev_err(cd->dev, "%s: fail read test data r=%d\n",
286 __func__, rc);
287 return rc;
288 }
289
290 cyttsp4_pr_buf(cd->dev, cd->pr_buf,
291 (u8 *)si->si_ptrs.test, si->si_ofs.test_size,
292 "sysinfo_test_data");
293 if (si->si_ptrs.test->post_codel &
294 CY_POST_CODEL_WDG_RST)
295 dev_info(cd->dev, "%s: %s codel=%02X\n",
296 __func__, "Reset was a WATCHDOG RESET",
297 si->si_ptrs.test->post_codel);
298
299 if (!(si->si_ptrs.test->post_codel &
300 CY_POST_CODEL_CFG_DATA_CRC_FAIL))
301 dev_info(cd->dev, "%s: %s codel=%02X\n", __func__,
302 "Config Data CRC FAIL",
303 si->si_ptrs.test->post_codel);
304
305 if (!(si->si_ptrs.test->post_codel &
306 CY_POST_CODEL_PANEL_TEST_FAIL))
307 dev_info(cd->dev, "%s: %s codel=%02X\n",
308 __func__, "PANEL TEST FAIL",
309 si->si_ptrs.test->post_codel);
310
311 dev_info(cd->dev, "%s: SCANNING is %s codel=%02X\n",
312 __func__, si->si_ptrs.test->post_codel & 0x08 ?
313 "ENABLED" : "DISABLED",
314 si->si_ptrs.test->post_codel);
315 return rc;
316}
317
318static int cyttsp4_si_get_pcfg_data(struct cyttsp4 *cd)
319{
320 struct cyttsp4_sysinfo *si = &cd->sysinfo;
321 void *p;
322 int rc;
323
324 si->si_ofs.pcfg_size = si->si_ofs.opcfg_ofs - si->si_ofs.pcfg_ofs;
325
326 p = krealloc(si->si_ptrs.pcfg, si->si_ofs.pcfg_size, GFP_KERNEL);
327 if (p == NULL) {
328 rc = -ENOMEM;
329 dev_err(cd->dev, "%s: fail alloc pcfg memory r=%d\n",
330 __func__, rc);
331 return rc;
332 }
333 si->si_ptrs.pcfg = p;
334
335 rc = cyttsp4_adap_read(cd, si->si_ofs.pcfg_ofs, si->si_ofs.pcfg_size,
336 si->si_ptrs.pcfg);
337 if (rc < 0) {
338 dev_err(cd->dev, "%s: fail read pcfg data r=%d\n",
339 __func__, rc);
340 return rc;
341 }
342
343 si->si_ofs.max_x = merge_bytes((si->si_ptrs.pcfg->res_xh
344 & CY_PCFG_RESOLUTION_X_MASK), si->si_ptrs.pcfg->res_xl);
345 si->si_ofs.x_origin = !!(si->si_ptrs.pcfg->res_xh
346 & CY_PCFG_ORIGIN_X_MASK);
347 si->si_ofs.max_y = merge_bytes((si->si_ptrs.pcfg->res_yh
348 & CY_PCFG_RESOLUTION_Y_MASK), si->si_ptrs.pcfg->res_yl);
349 si->si_ofs.y_origin = !!(si->si_ptrs.pcfg->res_yh
350 & CY_PCFG_ORIGIN_Y_MASK);
351 si->si_ofs.max_p = merge_bytes(si->si_ptrs.pcfg->max_zh,
352 si->si_ptrs.pcfg->max_zl);
353
354 cyttsp4_pr_buf(cd->dev, cd->pr_buf,
355 (u8 *)si->si_ptrs.pcfg,
356 si->si_ofs.pcfg_size, "sysinfo_pcfg_data");
357 return rc;
358}
359
360static int cyttsp4_si_get_opcfg_data(struct cyttsp4 *cd)
361{
362 struct cyttsp4_sysinfo *si = &cd->sysinfo;
363 struct cyttsp4_tch_abs_params *tch;
364 struct cyttsp4_tch_rec_params *tch_old, *tch_new;
365 enum cyttsp4_tch_abs abs;
366 int i;
367 void *p;
368 int rc;
369
370 si->si_ofs.opcfg_size = si->si_ofs.ddata_ofs - si->si_ofs.opcfg_ofs;
371
372 p = krealloc(si->si_ptrs.opcfg, si->si_ofs.opcfg_size, GFP_KERNEL);
373 if (p == NULL) {
374 dev_err(cd->dev, "%s: fail alloc opcfg memory\n", __func__);
375 rc = -ENOMEM;
376 goto cyttsp4_si_get_opcfg_data_exit;
377 }
378 si->si_ptrs.opcfg = p;
379
380 rc = cyttsp4_adap_read(cd, si->si_ofs.opcfg_ofs, si->si_ofs.opcfg_size,
381 si->si_ptrs.opcfg);
382 if (rc < 0) {
383 dev_err(cd->dev, "%s: fail read opcfg data r=%d\n",
384 __func__, rc);
385 goto cyttsp4_si_get_opcfg_data_exit;
386 }
387 si->si_ofs.cmd_ofs = si->si_ptrs.opcfg->cmd_ofs;
388 si->si_ofs.rep_ofs = si->si_ptrs.opcfg->rep_ofs;
389 si->si_ofs.rep_sz = (si->si_ptrs.opcfg->rep_szh * 256) +
390 si->si_ptrs.opcfg->rep_szl;
391 si->si_ofs.num_btns = si->si_ptrs.opcfg->num_btns;
392 si->si_ofs.num_btn_regs = (si->si_ofs.num_btns +
393 CY_NUM_BTN_PER_REG - 1) / CY_NUM_BTN_PER_REG;
394 si->si_ofs.tt_stat_ofs = si->si_ptrs.opcfg->tt_stat_ofs;
395 si->si_ofs.obj_cfg0 = si->si_ptrs.opcfg->obj_cfg0;
396 si->si_ofs.max_tchs = si->si_ptrs.opcfg->max_tchs &
397 CY_BYTE_OFS_MASK;
398 si->si_ofs.tch_rec_size = si->si_ptrs.opcfg->tch_rec_size &
399 CY_BYTE_OFS_MASK;
400
401 /* Get the old touch fields */
402 for (abs = CY_TCH_X; abs < CY_NUM_TCH_FIELDS; abs++) {
403 tch = &si->si_ofs.tch_abs[abs];
404 tch_old = &si->si_ptrs.opcfg->tch_rec_old[abs];
405
406 tch->ofs = tch_old->loc & CY_BYTE_OFS_MASK;
407 tch->size = cyttsp4_bits_2_bytes(tch_old->size,
408 &tch->max);
409 tch->bofs = (tch_old->loc & CY_BOFS_MASK) >> CY_BOFS_SHIFT;
410 }
411
412 /* button fields */
413 si->si_ofs.btn_rec_size = si->si_ptrs.opcfg->btn_rec_size;
414 si->si_ofs.btn_diff_ofs = si->si_ptrs.opcfg->btn_diff_ofs;
415 si->si_ofs.btn_diff_size = si->si_ptrs.opcfg->btn_diff_size;
416
417 if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE) {
418 /* Get the extended touch fields */
419 for (i = 0; i < CY_NUM_EXT_TCH_FIELDS; abs++, i++) {
420 tch = &si->si_ofs.tch_abs[abs];
421 tch_new = &si->si_ptrs.opcfg->tch_rec_new[i];
422
423 tch->ofs = tch_new->loc & CY_BYTE_OFS_MASK;
424 tch->size = cyttsp4_bits_2_bytes(tch_new->size,
425 &tch->max);
426 tch->bofs = (tch_new->loc & CY_BOFS_MASK) >> CY_BOFS_SHIFT;
427 }
428 }
429
430 for (abs = 0; abs < CY_TCH_NUM_ABS; abs++) {
431 dev_dbg(cd->dev, "%s: tch_rec_%s\n", __func__,
432 cyttsp4_tch_abs_string[abs]);
433 dev_dbg(cd->dev, "%s: ofs =%2Zd\n", __func__,
434 si->si_ofs.tch_abs[abs].ofs);
435 dev_dbg(cd->dev, "%s: siz =%2Zd\n", __func__,
436 si->si_ofs.tch_abs[abs].size);
437 dev_dbg(cd->dev, "%s: max =%2Zd\n", __func__,
438 si->si_ofs.tch_abs[abs].max);
439 dev_dbg(cd->dev, "%s: bofs=%2Zd\n", __func__,
440 si->si_ofs.tch_abs[abs].bofs);
441 }
442
443 si->si_ofs.mode_size = si->si_ofs.tt_stat_ofs + 1;
444 si->si_ofs.data_size = si->si_ofs.max_tchs *
445 si->si_ptrs.opcfg->tch_rec_size;
446
447 cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)si->si_ptrs.opcfg,
448 si->si_ofs.opcfg_size, "sysinfo_opcfg_data");
449
450cyttsp4_si_get_opcfg_data_exit:
451 return rc;
452}
453
454static int cyttsp4_si_get_ddata(struct cyttsp4 *cd)
455{
456 struct cyttsp4_sysinfo *si = &cd->sysinfo;
457 void *p;
458 int rc;
459
460 si->si_ofs.ddata_size = si->si_ofs.mdata_ofs - si->si_ofs.ddata_ofs;
461
462 p = krealloc(si->si_ptrs.ddata, si->si_ofs.ddata_size, GFP_KERNEL);
463 if (p == NULL) {
464 dev_err(cd->dev, "%s: fail alloc ddata memory\n", __func__);
465 return -ENOMEM;
466 }
467 si->si_ptrs.ddata = p;
468
469 rc = cyttsp4_adap_read(cd, si->si_ofs.ddata_ofs, si->si_ofs.ddata_size,
470 si->si_ptrs.ddata);
471 if (rc < 0)
472 dev_err(cd->dev, "%s: fail read ddata data r=%d\n",
473 __func__, rc);
474 else
475 cyttsp4_pr_buf(cd->dev, cd->pr_buf,
476 (u8 *)si->si_ptrs.ddata,
477 si->si_ofs.ddata_size, "sysinfo_ddata");
478 return rc;
479}
480
481static int cyttsp4_si_get_mdata(struct cyttsp4 *cd)
482{
483 struct cyttsp4_sysinfo *si = &cd->sysinfo;
484 void *p;
485 int rc;
486
487 si->si_ofs.mdata_size = si->si_ofs.map_sz - si->si_ofs.mdata_ofs;
488
489 p = krealloc(si->si_ptrs.mdata, si->si_ofs.mdata_size, GFP_KERNEL);
490 if (p == NULL) {
491 dev_err(cd->dev, "%s: fail alloc mdata memory\n", __func__);
492 return -ENOMEM;
493 }
494 si->si_ptrs.mdata = p;
495
496 rc = cyttsp4_adap_read(cd, si->si_ofs.mdata_ofs, si->si_ofs.mdata_size,
497 si->si_ptrs.mdata);
498 if (rc < 0)
499 dev_err(cd->dev, "%s: fail read mdata data r=%d\n",
500 __func__, rc);
501 else
502 cyttsp4_pr_buf(cd->dev, cd->pr_buf,
503 (u8 *)si->si_ptrs.mdata,
504 si->si_ofs.mdata_size, "sysinfo_mdata");
505 return rc;
506}
507
508static int cyttsp4_si_get_btn_data(struct cyttsp4 *cd)
509{
510 struct cyttsp4_sysinfo *si = &cd->sysinfo;
511 int btn;
512 int num_defined_keys;
513 u16 *key_table;
514 void *p;
515 int rc = 0;
516
517 if (si->si_ofs.num_btns) {
518 si->si_ofs.btn_keys_size = si->si_ofs.num_btns *
519 sizeof(struct cyttsp4_btn);
520
521 p = krealloc(si->btn, si->si_ofs.btn_keys_size,
522 GFP_KERNEL|__GFP_ZERO);
523 if (p == NULL) {
524 dev_err(cd->dev, "%s: %s\n", __func__,
525 "fail alloc btn_keys memory");
526 return -ENOMEM;
527 }
528 si->btn = p;
529
530 if (cd->cpdata->sett[CY_IC_GRPNUM_BTN_KEYS] == NULL)
531 num_defined_keys = 0;
532 else if (cd->cpdata->sett[CY_IC_GRPNUM_BTN_KEYS]->data == NULL)
533 num_defined_keys = 0;
534 else
535 num_defined_keys = cd->cpdata->sett
536 [CY_IC_GRPNUM_BTN_KEYS]->size;
537
538 for (btn = 0; btn < si->si_ofs.num_btns &&
539 btn < num_defined_keys; btn++) {
540 key_table = (u16 *)cd->cpdata->sett
541 [CY_IC_GRPNUM_BTN_KEYS]->data;
542 si->btn[btn].key_code = key_table[btn];
543 si->btn[btn].state = CY_BTN_RELEASED;
544 si->btn[btn].enabled = true;
545 }
546 for (; btn < si->si_ofs.num_btns; btn++) {
547 si->btn[btn].key_code = KEY_RESERVED;
548 si->btn[btn].state = CY_BTN_RELEASED;
549 si->btn[btn].enabled = true;
550 }
551
552 return rc;
553 }
554
555 si->si_ofs.btn_keys_size = 0;
556 kfree(si->btn);
557 si->btn = NULL;
558 return rc;
559}
560
561static int cyttsp4_si_get_op_data_ptrs(struct cyttsp4 *cd)
562{
563 struct cyttsp4_sysinfo *si = &cd->sysinfo;
564 void *p;
565
566 p = krealloc(si->xy_mode, si->si_ofs.mode_size, GFP_KERNEL|__GFP_ZERO);
567 if (p == NULL)
568 return -ENOMEM;
569 si->xy_mode = p;
570
571 p = krealloc(si->xy_data, si->si_ofs.data_size, GFP_KERNEL|__GFP_ZERO);
572 if (p == NULL)
573 return -ENOMEM;
574 si->xy_data = p;
575
576 p = krealloc(si->btn_rec_data,
577 si->si_ofs.btn_rec_size * si->si_ofs.num_btns,
578 GFP_KERNEL|__GFP_ZERO);
579 if (p == NULL)
580 return -ENOMEM;
581 si->btn_rec_data = p;
582
583 return 0;
584}
585
586static void cyttsp4_si_put_log_data(struct cyttsp4 *cd)
587{
588 struct cyttsp4_sysinfo *si = &cd->sysinfo;
589 dev_dbg(cd->dev, "%s: cydata_ofs =%4Zd siz=%4Zd\n", __func__,
590 si->si_ofs.cydata_ofs, si->si_ofs.cydata_size);
591 dev_dbg(cd->dev, "%s: test_ofs =%4Zd siz=%4Zd\n", __func__,
592 si->si_ofs.test_ofs, si->si_ofs.test_size);
593 dev_dbg(cd->dev, "%s: pcfg_ofs =%4Zd siz=%4Zd\n", __func__,
594 si->si_ofs.pcfg_ofs, si->si_ofs.pcfg_size);
595 dev_dbg(cd->dev, "%s: opcfg_ofs =%4Zd siz=%4Zd\n", __func__,
596 si->si_ofs.opcfg_ofs, si->si_ofs.opcfg_size);
597 dev_dbg(cd->dev, "%s: ddata_ofs =%4Zd siz=%4Zd\n", __func__,
598 si->si_ofs.ddata_ofs, si->si_ofs.ddata_size);
599 dev_dbg(cd->dev, "%s: mdata_ofs =%4Zd siz=%4Zd\n", __func__,
600 si->si_ofs.mdata_ofs, si->si_ofs.mdata_size);
601
602 dev_dbg(cd->dev, "%s: cmd_ofs =%4Zd\n", __func__,
603 si->si_ofs.cmd_ofs);
604 dev_dbg(cd->dev, "%s: rep_ofs =%4Zd\n", __func__,
605 si->si_ofs.rep_ofs);
606 dev_dbg(cd->dev, "%s: rep_sz =%4Zd\n", __func__,
607 si->si_ofs.rep_sz);
608 dev_dbg(cd->dev, "%s: num_btns =%4Zd\n", __func__,
609 si->si_ofs.num_btns);
610 dev_dbg(cd->dev, "%s: num_btn_regs =%4Zd\n", __func__,
611 si->si_ofs.num_btn_regs);
612 dev_dbg(cd->dev, "%s: tt_stat_ofs =%4Zd\n", __func__,
613 si->si_ofs.tt_stat_ofs);
614 dev_dbg(cd->dev, "%s: tch_rec_size =%4Zd\n", __func__,
615 si->si_ofs.tch_rec_size);
616 dev_dbg(cd->dev, "%s: max_tchs =%4Zd\n", __func__,
617 si->si_ofs.max_tchs);
618 dev_dbg(cd->dev, "%s: mode_size =%4Zd\n", __func__,
619 si->si_ofs.mode_size);
620 dev_dbg(cd->dev, "%s: data_size =%4Zd\n", __func__,
621 si->si_ofs.data_size);
622 dev_dbg(cd->dev, "%s: map_sz =%4Zd\n", __func__,
623 si->si_ofs.map_sz);
624
625 dev_dbg(cd->dev, "%s: btn_rec_size =%2Zd\n", __func__,
626 si->si_ofs.btn_rec_size);
627 dev_dbg(cd->dev, "%s: btn_diff_ofs =%2Zd\n", __func__,
628 si->si_ofs.btn_diff_ofs);
629 dev_dbg(cd->dev, "%s: btn_diff_size =%2Zd\n", __func__,
630 si->si_ofs.btn_diff_size);
631
632 dev_dbg(cd->dev, "%s: max_x = 0x%04ZX (%Zd)\n", __func__,
633 si->si_ofs.max_x, si->si_ofs.max_x);
634 dev_dbg(cd->dev, "%s: x_origin = %Zd (%s)\n", __func__,
635 si->si_ofs.x_origin,
636 si->si_ofs.x_origin == CY_NORMAL_ORIGIN ?
637 "left corner" : "right corner");
638 dev_dbg(cd->dev, "%s: max_y = 0x%04ZX (%Zd)\n", __func__,
639 si->si_ofs.max_y, si->si_ofs.max_y);
640 dev_dbg(cd->dev, "%s: y_origin = %Zd (%s)\n", __func__,
641 si->si_ofs.y_origin,
642 si->si_ofs.y_origin == CY_NORMAL_ORIGIN ?
643 "upper corner" : "lower corner");
644 dev_dbg(cd->dev, "%s: max_p = 0x%04ZX (%Zd)\n", __func__,
645 si->si_ofs.max_p, si->si_ofs.max_p);
646
647 dev_dbg(cd->dev, "%s: xy_mode=%p xy_data=%p\n", __func__,
648 si->xy_mode, si->xy_data);
649}
650
651static int cyttsp4_get_sysinfo_regs(struct cyttsp4 *cd)
652{
653 struct cyttsp4_sysinfo *si = &cd->sysinfo;
654 int rc;
655
656 rc = cyttsp4_si_data_offsets(cd);
657 if (rc < 0)
658 return rc;
659
660 rc = cyttsp4_si_get_cydata(cd);
661 if (rc < 0)
662 return rc;
663
664 rc = cyttsp4_si_get_test_data(cd);
665 if (rc < 0)
666 return rc;
667
668 rc = cyttsp4_si_get_pcfg_data(cd);
669 if (rc < 0)
670 return rc;
671
672 rc = cyttsp4_si_get_opcfg_data(cd);
673 if (rc < 0)
674 return rc;
675
676 rc = cyttsp4_si_get_ddata(cd);
677 if (rc < 0)
678 return rc;
679
680 rc = cyttsp4_si_get_mdata(cd);
681 if (rc < 0)
682 return rc;
683
684 rc = cyttsp4_si_get_btn_data(cd);
685 if (rc < 0)
686 return rc;
687
688 rc = cyttsp4_si_get_op_data_ptrs(cd);
689 if (rc < 0) {
690 dev_err(cd->dev, "%s: failed to get_op_data\n",
691 __func__);
692 return rc;
693 }
694
695 cyttsp4_si_put_log_data(cd);
696
697 /* provide flow control handshake */
698 rc = cyttsp4_handshake(cd, si->si_data.hst_mode);
699 if (rc < 0)
700 dev_err(cd->dev, "%s: handshake fail on sysinfo reg\n",
701 __func__);
702
703 si->ready = true;
704 return rc;
705}
706
707static void cyttsp4_queue_startup_(struct cyttsp4 *cd)
708{
709 if (cd->startup_state == STARTUP_NONE) {
710 cd->startup_state = STARTUP_QUEUED;
711 schedule_work(&cd->startup_work);
712 dev_dbg(cd->dev, "%s: cyttsp4_startup queued\n", __func__);
713 } else {
714 dev_dbg(cd->dev, "%s: startup_state = %d\n", __func__,
715 cd->startup_state);
716 }
717}
718
719static void cyttsp4_report_slot_liftoff(struct cyttsp4_mt_data *md,
720 int max_slots)
721{
722 int t;
723
724 if (md->num_prv_tch == 0)
725 return;
726
727 for (t = 0; t < max_slots; t++) {
728 input_mt_slot(md->input, t);
729 input_mt_report_slot_state(md->input,
730 MT_TOOL_FINGER, false);
731 }
732}
733
734static void cyttsp4_lift_all(struct cyttsp4_mt_data *md)
735{
736 if (!md->si)
737 return;
738
739 if (md->num_prv_tch != 0) {
740 cyttsp4_report_slot_liftoff(md,
741 md->si->si_ofs.tch_abs[CY_TCH_T].max);
742 input_sync(md->input);
743 md->num_prv_tch = 0;
744 }
745}
746
747static void cyttsp4_get_touch_axis(struct cyttsp4_mt_data *md,
748 int *axis, int size, int max, u8 *xy_data, int bofs)
749{
750 int nbyte;
751 int next;
752
753 for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
754 dev_vdbg(&md->input->dev,
755 "%s: *axis=%02X(%d) size=%d max=%08X xy_data=%p"
756 " xy_data[%d]=%02X(%d) bofs=%d\n",
757 __func__, *axis, *axis, size, max, xy_data, next,
758 xy_data[next], xy_data[next], bofs);
759 *axis = (*axis * 256) + (xy_data[next] >> bofs);
760 next++;
761 }
762
763 *axis &= max - 1;
764
765 dev_vdbg(&md->input->dev,
766 "%s: *axis=%02X(%d) size=%d max=%08X xy_data=%p"
767 " xy_data[%d]=%02X(%d)\n",
768 __func__, *axis, *axis, size, max, xy_data, next,
769 xy_data[next], xy_data[next]);
770}
771
772static void cyttsp4_get_touch(struct cyttsp4_mt_data *md,
773 struct cyttsp4_touch *touch, u8 *xy_data)
774{
775 struct device *dev = &md->input->dev;
776 struct cyttsp4_sysinfo *si = md->si;
777 enum cyttsp4_tch_abs abs;
778 int tmp;
779 bool flipped;
780
781 for (abs = CY_TCH_X; abs < CY_TCH_NUM_ABS; abs++) {
782 cyttsp4_get_touch_axis(md, &touch->abs[abs],
783 si->si_ofs.tch_abs[abs].size,
784 si->si_ofs.tch_abs[abs].max,
785 xy_data + si->si_ofs.tch_abs[abs].ofs,
786 si->si_ofs.tch_abs[abs].bofs);
787 dev_vdbg(dev, "%s: get %s=%04X(%d)\n", __func__,
788 cyttsp4_tch_abs_string[abs],
789 touch->abs[abs], touch->abs[abs]);
790 }
791
792 if (md->pdata->flags & CY_FLAG_FLIP) {
793 tmp = touch->abs[CY_TCH_X];
794 touch->abs[CY_TCH_X] = touch->abs[CY_TCH_Y];
795 touch->abs[CY_TCH_Y] = tmp;
796 flipped = true;
797 } else
798 flipped = false;
799
800 if (md->pdata->flags & CY_FLAG_INV_X) {
801 if (flipped)
802 touch->abs[CY_TCH_X] = md->si->si_ofs.max_y -
803 touch->abs[CY_TCH_X];
804 else
805 touch->abs[CY_TCH_X] = md->si->si_ofs.max_x -
806 touch->abs[CY_TCH_X];
807 }
808 if (md->pdata->flags & CY_FLAG_INV_Y) {
809 if (flipped)
810 touch->abs[CY_TCH_Y] = md->si->si_ofs.max_x -
811 touch->abs[CY_TCH_Y];
812 else
813 touch->abs[CY_TCH_Y] = md->si->si_ofs.max_y -
814 touch->abs[CY_TCH_Y];
815 }
816
817 dev_vdbg(dev, "%s: flip=%s inv-x=%s inv-y=%s x=%04X(%d) y=%04X(%d)\n",
818 __func__, flipped ? "true" : "false",
819 md->pdata->flags & CY_FLAG_INV_X ? "true" : "false",
820 md->pdata->flags & CY_FLAG_INV_Y ? "true" : "false",
821 touch->abs[CY_TCH_X], touch->abs[CY_TCH_X],
822 touch->abs[CY_TCH_Y], touch->abs[CY_TCH_Y]);
823}
824
825static void cyttsp4_final_sync(struct input_dev *input, int max_slots, int *ids)
826{
827 int t;
828
829 for (t = 0; t < max_slots; t++) {
830 if (ids[t])
831 continue;
832 input_mt_slot(input, t);
833 input_mt_report_slot_state(input, MT_TOOL_FINGER, false);
834 }
835
836 input_sync(input);
837}
838
839static void cyttsp4_get_mt_touches(struct cyttsp4_mt_data *md, int num_cur_tch)
840{
841 struct device *dev = &md->input->dev;
842 struct cyttsp4_sysinfo *si = md->si;
843 struct cyttsp4_touch tch;
844 int sig;
845 int i, j, t = 0;
846 int ids[max(CY_TMA1036_MAX_TCH, CY_TMA4XX_MAX_TCH)];
847
848 memset(ids, 0, si->si_ofs.tch_abs[CY_TCH_T].max * sizeof(int));
849 for (i = 0; i < num_cur_tch; i++) {
850 cyttsp4_get_touch(md, &tch, si->xy_data +
851 (i * si->si_ofs.tch_rec_size));
852 if ((tch.abs[CY_TCH_T] < md->pdata->frmwrk->abs
853 [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MIN_OST]) ||
854 (tch.abs[CY_TCH_T] > md->pdata->frmwrk->abs
855 [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MAX_OST])) {
856 dev_err(dev, "%s: tch=%d -> bad trk_id=%d max_id=%d\n",
857 __func__, i, tch.abs[CY_TCH_T],
858 md->pdata->frmwrk->abs[(CY_ABS_ID_OST *
859 CY_NUM_ABS_SET) + CY_MAX_OST]);
860 continue;
861 }
862
863 /* use 0 based track id's */
864 sig = md->pdata->frmwrk->abs
865 [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + 0];
866 if (sig != CY_IGNORE_VALUE) {
867 t = tch.abs[CY_TCH_T] - md->pdata->frmwrk->abs
868 [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MIN_OST];
869 if (tch.abs[CY_TCH_E] == CY_EV_LIFTOFF) {
870 dev_dbg(dev, "%s: t=%d e=%d lift-off\n",
871 __func__, t, tch.abs[CY_TCH_E]);
872 goto cyttsp4_get_mt_touches_pr_tch;
873 }
874 input_mt_slot(md->input, t);
875 input_mt_report_slot_state(md->input, MT_TOOL_FINGER,
876 true);
877 ids[t] = true;
878 }
879
880 /* all devices: position and pressure fields */
881 for (j = 0; j <= CY_ABS_W_OST; j++) {
882 sig = md->pdata->frmwrk->abs[((CY_ABS_X_OST + j) *
883 CY_NUM_ABS_SET) + 0];
884 if (sig != CY_IGNORE_VALUE)
885 input_report_abs(md->input, sig,
886 tch.abs[CY_TCH_X + j]);
887 }
888 if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE) {
889 /*
890 * TMA400 size and orientation fields:
891 * if pressure is non-zero and major touch
892 * signal is zero, then set major and minor touch
893 * signals to minimum non-zero value
894 */
895 if (tch.abs[CY_TCH_P] > 0 && tch.abs[CY_TCH_MAJ] == 0)
896 tch.abs[CY_TCH_MAJ] = tch.abs[CY_TCH_MIN] = 1;
897
898 /* Get the extended touch fields */
899 for (j = 0; j < CY_NUM_EXT_TCH_FIELDS; j++) {
900 sig = md->pdata->frmwrk->abs
901 [((CY_ABS_MAJ_OST + j) *
902 CY_NUM_ABS_SET) + 0];
903 if (sig != CY_IGNORE_VALUE)
904 input_report_abs(md->input, sig,
905 tch.abs[CY_TCH_MAJ + j]);
906 }
907 }
908
909cyttsp4_get_mt_touches_pr_tch:
910 if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE)
911 dev_dbg(dev,
912 "%s: t=%d x=%d y=%d z=%d M=%d m=%d o=%d e=%d\n",
913 __func__, t,
914 tch.abs[CY_TCH_X],
915 tch.abs[CY_TCH_Y],
916 tch.abs[CY_TCH_P],
917 tch.abs[CY_TCH_MAJ],
918 tch.abs[CY_TCH_MIN],
919 tch.abs[CY_TCH_OR],
920 tch.abs[CY_TCH_E]);
921 else
922 dev_dbg(dev,
923 "%s: t=%d x=%d y=%d z=%d e=%d\n", __func__,
924 t,
925 tch.abs[CY_TCH_X],
926 tch.abs[CY_TCH_Y],
927 tch.abs[CY_TCH_P],
928 tch.abs[CY_TCH_E]);
929 }
930
931 cyttsp4_final_sync(md->input, si->si_ofs.tch_abs[CY_TCH_T].max, ids);
932
933 md->num_prv_tch = num_cur_tch;
934
935 return;
936}
937
938/* read xy_data for all current touches */
939static int cyttsp4_xy_worker(struct cyttsp4 *cd)
940{
941 struct cyttsp4_mt_data *md = &cd->md;
942 struct device *dev = &md->input->dev;
943 struct cyttsp4_sysinfo *si = md->si;
944 u8 num_cur_tch;
945 u8 hst_mode;
946 u8 rep_len;
947 u8 rep_stat;
948 u8 tt_stat;
949 int rc = 0;
950
951 /*
952 * Get event data from cyttsp4 device.
953 * The event data includes all data
954 * for all active touches.
955 * Event data also includes button data
956 */
957 /*
958 * Use 2 reads:
959 * 1st read to get mode + button bytes + touch count (core)
960 * 2nd read (optional) to get touch 1 - touch n data
961 */
962 hst_mode = si->xy_mode[CY_REG_BASE];
963 rep_len = si->xy_mode[si->si_ofs.rep_ofs];
964 rep_stat = si->xy_mode[si->si_ofs.rep_ofs + 1];
965 tt_stat = si->xy_mode[si->si_ofs.tt_stat_ofs];
966 dev_vdbg(dev, "%s: %s%02X %s%d %s%02X %s%02X\n", __func__,
967 "hst_mode=", hst_mode, "rep_len=", rep_len,
968 "rep_stat=", rep_stat, "tt_stat=", tt_stat);
969
970 num_cur_tch = GET_NUM_TOUCHES(tt_stat);
971 dev_vdbg(dev, "%s: num_cur_tch=%d\n", __func__, num_cur_tch);
972
973 if (rep_len == 0 && num_cur_tch > 0) {
974 dev_err(dev, "%s: report length error rep_len=%d num_tch=%d\n",
975 __func__, rep_len, num_cur_tch);
976 goto cyttsp4_xy_worker_exit;
977 }
978
979 /* read touches */
980 if (num_cur_tch > 0) {
981 rc = cyttsp4_adap_read(cd, si->si_ofs.tt_stat_ofs + 1,
982 num_cur_tch * si->si_ofs.tch_rec_size,
983 si->xy_data);
984 if (rc < 0) {
985 dev_err(dev, "%s: read fail on touch regs r=%d\n",
986 __func__, rc);
987 goto cyttsp4_xy_worker_exit;
988 }
989 }
990
991 /* print xy data */
992 cyttsp4_pr_buf(dev, cd->pr_buf, si->xy_data, num_cur_tch *
993 si->si_ofs.tch_rec_size, "xy_data");
994
995 /* check any error conditions */
996 if (IS_BAD_PKT(rep_stat)) {
997 dev_dbg(dev, "%s: Invalid buffer detected\n", __func__);
998 rc = 0;
999 goto cyttsp4_xy_worker_exit;
1000 }
1001
1002 if (IS_LARGE_AREA(tt_stat))
1003 dev_dbg(dev, "%s: Large area detected\n", __func__);
1004
1005 if (num_cur_tch > si->si_ofs.max_tchs) {
1006 dev_err(dev, "%s: too many tch; set to max tch (n=%d c=%Zd)\n",
1007 __func__, num_cur_tch, si->si_ofs.max_tchs);
1008 num_cur_tch = si->si_ofs.max_tchs;
1009 }
1010
1011 /* extract xy_data for all currently reported touches */
1012 dev_vdbg(dev, "%s: extract data num_cur_tch=%d\n", __func__,
1013 num_cur_tch);
1014 if (num_cur_tch)
1015 cyttsp4_get_mt_touches(md, num_cur_tch);
1016 else
1017 cyttsp4_lift_all(md);
1018
1019 rc = 0;
1020
1021cyttsp4_xy_worker_exit:
1022 return rc;
1023}
1024
1025static int cyttsp4_mt_attention(struct cyttsp4 *cd)
1026{
1027 struct device *dev = cd->dev;
1028 struct cyttsp4_mt_data *md = &cd->md;
1029 int rc = 0;
1030
1031 if (!md->si)
1032 return 0;
1033
1034 mutex_lock(&md->report_lock);
1035 if (!md->is_suspended) {
1036 /* core handles handshake */
1037 rc = cyttsp4_xy_worker(cd);
1038 } else {
1039 dev_vdbg(dev, "%s: Ignoring report while suspended\n",
1040 __func__);
1041 }
1042 mutex_unlock(&md->report_lock);
1043 if (rc < 0)
1044 dev_err(dev, "%s: xy_worker error r=%d\n", __func__, rc);
1045
1046 return rc;
1047}
1048
1049static irqreturn_t cyttsp4_irq(int irq, void *handle)
1050{
1051 struct cyttsp4 *cd = handle;
1052 struct device *dev = cd->dev;
1053 enum cyttsp4_mode cur_mode;
1054 u8 cmd_ofs = cd->sysinfo.si_ofs.cmd_ofs;
1055 u8 mode[3];
1056 int rc;
1057
1058 /*
1059 * Check whether this IRQ should be ignored (external)
1060 * This should be the very first thing to check since
1061 * ignore_irq may be set for a very short period of time
1062 */
1063 if (atomic_read(&cd->ignore_irq)) {
1064 dev_vdbg(dev, "%s: Ignoring IRQ\n", __func__);
1065 return IRQ_HANDLED;
1066 }
1067
1068 dev_dbg(dev, "%s int:0x%x\n", __func__, cd->int_status);
1069
1070 mutex_lock(&cd->system_lock);
1071
1072 /* Just to debug */
1073 if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEPING)
1074 dev_vdbg(dev, "%s: Received IRQ while in sleep\n", __func__);
1075
1076 rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), mode);
1077 if (rc) {
1078 dev_err(cd->dev, "%s: Fail read adapter r=%d\n", __func__, rc);
1079 goto cyttsp4_irq_exit;
1080 }
1081 dev_vdbg(dev, "%s mode[0-2]:0x%X 0x%X 0x%X\n", __func__,
1082 mode[0], mode[1], mode[2]);
1083
1084 if (IS_BOOTLOADER(mode[0], mode[1])) {
1085 cur_mode = CY_MODE_BOOTLOADER;
1086 dev_vdbg(dev, "%s: bl running\n", __func__);
1087 if (cd->mode == CY_MODE_BOOTLOADER) {
1088 /* Signal bootloader heartbeat heard */
1089 wake_up(&cd->wait_q);
1090 goto cyttsp4_irq_exit;
1091 }
1092
1093 /* switch to bootloader */
1094 dev_dbg(dev, "%s: restart switch to bl m=%d -> m=%d\n",
1095 __func__, cd->mode, cur_mode);
1096
1097 /* catch operation->bl glitch */
1098 if (cd->mode != CY_MODE_UNKNOWN) {
1099 /* Incase startup_state do not let startup_() */
1100 cd->mode = CY_MODE_UNKNOWN;
1101 cyttsp4_queue_startup_(cd);
1102 goto cyttsp4_irq_exit;
1103 }
1104
1105 /*
1106 * do not wake thread on this switch since
1107 * it is possible to get an early heartbeat
1108 * prior to performing the reset
1109 */
1110 cd->mode = cur_mode;
1111
1112 goto cyttsp4_irq_exit;
1113 }
1114
1115 switch (mode[0] & CY_HST_MODE) {
1116 case CY_HST_OPERATE:
1117 cur_mode = CY_MODE_OPERATIONAL;
1118 dev_vdbg(dev, "%s: operational\n", __func__);
1119 break;
1120 case CY_HST_CAT:
1121 cur_mode = CY_MODE_CAT;
1122 dev_vdbg(dev, "%s: CaT\n", __func__);
1123 break;
1124 case CY_HST_SYSINFO:
1125 cur_mode = CY_MODE_SYSINFO;
1126 dev_vdbg(dev, "%s: sysinfo\n", __func__);
1127 break;
1128 default:
1129 cur_mode = CY_MODE_UNKNOWN;
1130 dev_err(dev, "%s: unknown HST mode 0x%02X\n", __func__,
1131 mode[0]);
1132 break;
1133 }
1134
1135 /* Check whether this IRQ should be ignored (internal) */
1136 if (cd->int_status & CY_INT_IGNORE) {
1137 dev_vdbg(dev, "%s: Ignoring IRQ\n", __func__);
1138 goto cyttsp4_irq_exit;
1139 }
1140
1141 /* Check for wake up interrupt */
1142 if (cd->int_status & CY_INT_AWAKE) {
1143 cd->int_status &= ~CY_INT_AWAKE;
1144 wake_up(&cd->wait_q);
1145 dev_vdbg(dev, "%s: Received wake up interrupt\n", __func__);
1146 goto cyttsp4_irq_handshake;
1147 }
1148
1149 /* Expecting mode change interrupt */
1150 if ((cd->int_status & CY_INT_MODE_CHANGE)
1151 && (mode[0] & CY_HST_MODE_CHANGE) == 0) {
1152 cd->int_status &= ~CY_INT_MODE_CHANGE;
1153 dev_dbg(dev, "%s: finish mode switch m=%d -> m=%d\n",
1154 __func__, cd->mode, cur_mode);
1155 cd->mode = cur_mode;
1156 wake_up(&cd->wait_q);
1157 goto cyttsp4_irq_handshake;
1158 }
1159
1160 /* compare current core mode to current device mode */
1161 dev_vdbg(dev, "%s: cd->mode=%d cur_mode=%d\n",
1162 __func__, cd->mode, cur_mode);
1163 if ((mode[0] & CY_HST_MODE_CHANGE) == 0 && cd->mode != cur_mode) {
1164 /* Unexpected mode change occurred */
1165 dev_err(dev, "%s %d->%d 0x%x\n", __func__, cd->mode,
1166 cur_mode, cd->int_status);
1167 dev_dbg(dev, "%s: Unexpected mode change, startup\n",
1168 __func__);
1169 cyttsp4_queue_startup_(cd);
1170 goto cyttsp4_irq_exit;
1171 }
1172
1173 /* Expecting command complete interrupt */
1174 dev_vdbg(dev, "%s: command byte:0x%x\n", __func__, mode[cmd_ofs]);
1175 if ((cd->int_status & CY_INT_EXEC_CMD)
1176 && mode[cmd_ofs] & CY_CMD_COMPLETE) {
1177 cd->int_status &= ~CY_INT_EXEC_CMD;
1178 dev_vdbg(dev, "%s: Received command complete interrupt\n",
1179 __func__);
1180 wake_up(&cd->wait_q);
1181 /*
1182 * It is possible to receive a single interrupt for
1183 * command complete and touch/button status report.
1184 * Continue processing for a possible status report.
1185 */
1186 }
1187
1188 /* This should be status report, read status regs */
1189 if (cd->mode == CY_MODE_OPERATIONAL) {
1190 dev_vdbg(dev, "%s: Read status registers\n", __func__);
1191 rc = cyttsp4_load_status_regs(cd);
1192 if (rc < 0)
1193 dev_err(dev, "%s: fail read mode regs r=%d\n",
1194 __func__, rc);
1195 }
1196
1197 cyttsp4_mt_attention(cd);
1198
1199cyttsp4_irq_handshake:
1200 /* handshake the event */
1201 dev_vdbg(dev, "%s: Handshake mode=0x%02X r=%d\n",
1202 __func__, mode[0], rc);
1203 rc = cyttsp4_handshake(cd, mode[0]);
1204 if (rc < 0)
1205 dev_err(dev, "%s: Fail handshake mode=0x%02X r=%d\n",
1206 __func__, mode[0], rc);
1207
1208 /*
1209 * a non-zero udelay period is required for using
1210 * IRQF_TRIGGER_LOW in order to delay until the
1211 * device completes isr deassert
1212 */
1213 udelay(cd->cpdata->level_irq_udelay);
1214
1215cyttsp4_irq_exit:
1216 mutex_unlock(&cd->system_lock);
1217 return IRQ_HANDLED;
1218}
1219
1220static void cyttsp4_start_wd_timer(struct cyttsp4 *cd)
1221{
1222 if (!CY_WATCHDOG_TIMEOUT)
1223 return;
1224
1225 mod_timer(&cd->watchdog_timer, jiffies +
1226 msecs_to_jiffies(CY_WATCHDOG_TIMEOUT));
1227}
1228
1229static void cyttsp4_stop_wd_timer(struct cyttsp4 *cd)
1230{
1231 if (!CY_WATCHDOG_TIMEOUT)
1232 return;
1233
1234 /*
1235 * Ensure we wait until the watchdog timer
1236 * running on a different CPU finishes
1237 */
1238 del_timer_sync(&cd->watchdog_timer);
1239 cancel_work_sync(&cd->watchdog_work);
1240 del_timer_sync(&cd->watchdog_timer);
1241}
1242
1243static void cyttsp4_watchdog_timer(unsigned long handle)
1244{
1245 struct cyttsp4 *cd = (struct cyttsp4 *)handle;
1246
1247 dev_vdbg(cd->dev, "%s: Watchdog timer triggered\n", __func__);
1248
1249 schedule_work(&cd->watchdog_work);
1250
1251 return;
1252}
1253
1254static int cyttsp4_request_exclusive(struct cyttsp4 *cd, void *ownptr,
1255 int timeout_ms)
1256{
1257 int t = msecs_to_jiffies(timeout_ms);
1258 bool with_timeout = (timeout_ms != 0);
1259
1260 mutex_lock(&cd->system_lock);
1261 if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
1262 cd->exclusive_dev = ownptr;
1263 goto exit;
1264 }
1265
1266 cd->exclusive_waits++;
1267wait:
1268 mutex_unlock(&cd->system_lock);
1269 if (with_timeout) {
1270 t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
1271 if (IS_TMO(t)) {
1272 dev_err(cd->dev, "%s: tmo waiting exclusive access\n",
1273 __func__);
1274 mutex_lock(&cd->system_lock);
1275 cd->exclusive_waits--;
1276 mutex_unlock(&cd->system_lock);
1277 return -ETIME;
1278 }
1279 } else {
1280 wait_event(cd->wait_q, !cd->exclusive_dev);
1281 }
1282 mutex_lock(&cd->system_lock);
1283 if (cd->exclusive_dev)
1284 goto wait;
1285 cd->exclusive_dev = ownptr;
1286 cd->exclusive_waits--;
1287exit:
1288 mutex_unlock(&cd->system_lock);
1289
1290 return 0;
1291}
1292
1293/*
1294 * returns error if was not owned
1295 */
1296static int cyttsp4_release_exclusive(struct cyttsp4 *cd, void *ownptr)
1297{
1298 mutex_lock(&cd->system_lock);
1299 if (cd->exclusive_dev != ownptr) {
1300 mutex_unlock(&cd->system_lock);
1301 return -EINVAL;
1302 }
1303
1304 dev_vdbg(cd->dev, "%s: exclusive_dev %p freed\n",
1305 __func__, cd->exclusive_dev);
1306 cd->exclusive_dev = NULL;
1307 wake_up(&cd->wait_q);
1308 mutex_unlock(&cd->system_lock);
1309 return 0;
1310}
1311
1312static int cyttsp4_wait_bl_heartbeat(struct cyttsp4 *cd)
1313{
1314 long t;
1315 int rc = 0;
1316
1317 /* wait heartbeat */
1318 dev_vdbg(cd->dev, "%s: wait heartbeat...\n", __func__);
1319 t = wait_event_timeout(cd->wait_q, cd->mode == CY_MODE_BOOTLOADER,
1320 msecs_to_jiffies(CY_CORE_RESET_AND_WAIT_TIMEOUT));
1321 if (IS_TMO(t)) {
1322 dev_err(cd->dev, "%s: tmo waiting bl heartbeat cd->mode=%d\n",
1323 __func__, cd->mode);
1324 rc = -ETIME;
1325 }
1326
1327 return rc;
1328}
1329
1330static int cyttsp4_wait_sysinfo_mode(struct cyttsp4 *cd)
1331{
1332 long t;
1333
1334 dev_vdbg(cd->dev, "%s: wait sysinfo...\n", __func__);
1335
1336 t = wait_event_timeout(cd->wait_q, cd->mode == CY_MODE_SYSINFO,
1337 msecs_to_jiffies(CY_CORE_MODE_CHANGE_TIMEOUT));
1338 if (IS_TMO(t)) {
1339 dev_err(cd->dev, "%s: tmo waiting exit bl cd->mode=%d\n",
1340 __func__, cd->mode);
1341 mutex_lock(&cd->system_lock);
1342 cd->int_status &= ~CY_INT_MODE_CHANGE;
1343 mutex_unlock(&cd->system_lock);
1344 return -ETIME;
1345 }
1346
1347 return 0;
1348}
1349
1350static int cyttsp4_reset_and_wait(struct cyttsp4 *cd)
1351{
1352 int rc;
1353
1354 /* reset hardware */
1355 mutex_lock(&cd->system_lock);
1356 dev_dbg(cd->dev, "%s: reset hw...\n", __func__);
1357 rc = cyttsp4_hw_reset(cd);
1358 cd->mode = CY_MODE_UNKNOWN;
1359 mutex_unlock(&cd->system_lock);
1360 if (rc < 0) {
1361 dev_err(cd->dev, "%s:Fail hw reset r=%d\n", __func__, rc);
1362 return rc;
1363 }
1364
1365 return cyttsp4_wait_bl_heartbeat(cd);
1366}
1367
1368/*
1369 * returns err if refused or timeout; block until mode change complete
1370 * bit is set (mode change interrupt)
1371 */
1372static int cyttsp4_set_mode(struct cyttsp4 *cd, int new_mode)
1373{
1374 u8 new_dev_mode;
1375 u8 mode;
1376 long t;
1377 int rc;
1378
1379 switch (new_mode) {
1380 case CY_MODE_OPERATIONAL:
1381 new_dev_mode = CY_HST_OPERATE;
1382 break;
1383 case CY_MODE_SYSINFO:
1384 new_dev_mode = CY_HST_SYSINFO;
1385 break;
1386 case CY_MODE_CAT:
1387 new_dev_mode = CY_HST_CAT;
1388 break;
1389 default:
1390 dev_err(cd->dev, "%s: invalid mode: %02X(%d)\n",
1391 __func__, new_mode, new_mode);
1392 return -EINVAL;
1393 }
1394
1395 /* change mode */
1396 dev_dbg(cd->dev, "%s: %s=%p new_dev_mode=%02X new_mode=%d\n",
1397 __func__, "have exclusive", cd->exclusive_dev,
1398 new_dev_mode, new_mode);
1399
1400 mutex_lock(&cd->system_lock);
1401 rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), &mode);
1402 if (rc < 0) {
1403 mutex_unlock(&cd->system_lock);
1404 dev_err(cd->dev, "%s: Fail read mode r=%d\n",
1405 __func__, rc);
1406 goto exit;
1407 }
1408
1409 /* Clear device mode bits and set to new mode */
1410 mode &= ~CY_HST_MODE;
1411 mode |= new_dev_mode | CY_HST_MODE_CHANGE;
1412
1413 cd->int_status |= CY_INT_MODE_CHANGE;
1414 rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(mode), &mode);
1415 mutex_unlock(&cd->system_lock);
1416 if (rc < 0) {
1417 dev_err(cd->dev, "%s: Fail write mode change r=%d\n",
1418 __func__, rc);
1419 goto exit;
1420 }
1421
1422 /* wait for mode change done interrupt */
1423 t = wait_event_timeout(cd->wait_q,
1424 (cd->int_status & CY_INT_MODE_CHANGE) == 0,
1425 msecs_to_jiffies(CY_CORE_MODE_CHANGE_TIMEOUT));
1426 dev_dbg(cd->dev, "%s: back from wait t=%ld cd->mode=%d\n",
1427 __func__, t, cd->mode);
1428
1429 if (IS_TMO(t)) {
1430 dev_err(cd->dev, "%s: %s\n", __func__,
1431 "tmo waiting mode change");
1432 mutex_lock(&cd->system_lock);
1433 cd->int_status &= ~CY_INT_MODE_CHANGE;
1434 mutex_unlock(&cd->system_lock);
1435 rc = -EINVAL;
1436 }
1437
1438exit:
1439 return rc;
1440}
1441
1442static void cyttsp4_watchdog_work(struct work_struct *work)
1443{
1444 struct cyttsp4 *cd =
1445 container_of(work, struct cyttsp4, watchdog_work);
1446 u8 *mode;
1447 int retval;
1448
1449 mutex_lock(&cd->system_lock);
1450 retval = cyttsp4_load_status_regs(cd);
1451 if (retval < 0) {
1452 dev_err(cd->dev,
1453 "%s: failed to access device in watchdog timer r=%d\n",
1454 __func__, retval);
1455 cyttsp4_queue_startup_(cd);
1456 goto cyttsp4_timer_watchdog_exit_error;
1457 }
1458 mode = &cd->sysinfo.xy_mode[CY_REG_BASE];
1459 if (IS_BOOTLOADER(mode[0], mode[1])) {
1460 dev_err(cd->dev,
1461 "%s: device found in bootloader mode when operational mode\n",
1462 __func__);
1463 cyttsp4_queue_startup_(cd);
1464 goto cyttsp4_timer_watchdog_exit_error;
1465 }
1466
1467 cyttsp4_start_wd_timer(cd);
1468cyttsp4_timer_watchdog_exit_error:
1469 mutex_unlock(&cd->system_lock);
1470 return;
1471}
1472
1473static int cyttsp4_core_sleep_(struct cyttsp4 *cd)
1474{
1475 enum cyttsp4_sleep_state ss = SS_SLEEP_ON;
1476 enum cyttsp4_int_state int_status = CY_INT_IGNORE;
1477 int rc = 0;
1478 u8 mode[2];
1479
1480 /* Already in sleep mode? */
1481 mutex_lock(&cd->system_lock);
1482 if (cd->sleep_state == SS_SLEEP_ON) {
1483 mutex_unlock(&cd->system_lock);
1484 return 0;
1485 }
1486 cd->sleep_state = SS_SLEEPING;
1487 mutex_unlock(&cd->system_lock);
1488
1489 cyttsp4_stop_wd_timer(cd);
1490
1491 /* Wait until currently running IRQ handler exits and disable IRQ */
1492 disable_irq(cd->irq);
1493
1494 dev_vdbg(cd->dev, "%s: write DEEP SLEEP...\n", __func__);
1495 mutex_lock(&cd->system_lock);
1496 rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), &mode);
1497 if (rc) {
1498 mutex_unlock(&cd->system_lock);
1499 dev_err(cd->dev, "%s: Fail read adapter r=%d\n", __func__, rc);
1500 goto error;
1501 }
1502
1503 if (IS_BOOTLOADER(mode[0], mode[1])) {
1504 mutex_unlock(&cd->system_lock);
1505 dev_err(cd->dev, "%s: Device in BOOTLADER mode.\n", __func__);
1506 rc = -EINVAL;
1507 goto error;
1508 }
1509
1510 mode[0] |= CY_HST_SLEEP;
1511 rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(mode[0]), &mode[0]);
1512 mutex_unlock(&cd->system_lock);
1513 if (rc) {
1514 dev_err(cd->dev, "%s: Fail write adapter r=%d\n", __func__, rc);
1515 goto error;
1516 }
1517 dev_vdbg(cd->dev, "%s: write DEEP SLEEP succeeded\n", __func__);
1518
1519 if (cd->cpdata->power) {
1520 dev_dbg(cd->dev, "%s: Power down HW\n", __func__);
1521 rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, &cd->ignore_irq);
1522 } else {
1523 dev_dbg(cd->dev, "%s: No power function\n", __func__);
1524 rc = 0;
1525 }
1526 if (rc < 0) {
1527 dev_err(cd->dev, "%s: HW Power down fails r=%d\n",
1528 __func__, rc);
1529 goto error;
1530 }
1531
1532 /* Give time to FW to sleep */
1533 msleep(50);
1534
1535 goto exit;
1536
1537error:
1538 ss = SS_SLEEP_OFF;
1539 int_status = CY_INT_NONE;
1540 cyttsp4_start_wd_timer(cd);
1541
1542exit:
1543 mutex_lock(&cd->system_lock);
1544 cd->sleep_state = ss;
1545 cd->int_status |= int_status;
1546 mutex_unlock(&cd->system_lock);
1547 enable_irq(cd->irq);
1548 return rc;
1549}
1550
1551static int cyttsp4_startup_(struct cyttsp4 *cd)
1552{
1553 int retry = CY_CORE_STARTUP_RETRY_COUNT;
1554 int rc;
1555
1556 cyttsp4_stop_wd_timer(cd);
1557
1558reset:
1559 if (retry != CY_CORE_STARTUP_RETRY_COUNT)
1560 dev_dbg(cd->dev, "%s: Retry %d\n", __func__,
1561 CY_CORE_STARTUP_RETRY_COUNT - retry);
1562
1563 /* reset hardware and wait for heartbeat */
1564 rc = cyttsp4_reset_and_wait(cd);
1565 if (rc < 0) {
1566 dev_err(cd->dev, "%s: Error on h/w reset r=%d\n", __func__, rc);
1567 if (retry--)
1568 goto reset;
1569 goto exit;
1570 }
1571
1572 /* exit bl into sysinfo mode */
1573 dev_vdbg(cd->dev, "%s: write exit ldr...\n", __func__);
1574 mutex_lock(&cd->system_lock);
1575 cd->int_status &= ~CY_INT_IGNORE;
1576 cd->int_status |= CY_INT_MODE_CHANGE;
1577
1578 rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(ldr_exit),
1579 (u8 *)ldr_exit);
1580 mutex_unlock(&cd->system_lock);
1581 if (rc < 0) {
1582 dev_err(cd->dev, "%s: Fail write r=%d\n", __func__, rc);
1583 if (retry--)
1584 goto reset;
1585 goto exit;
1586 }
1587
1588 rc = cyttsp4_wait_sysinfo_mode(cd);
1589 if (rc < 0) {
1590 u8 buf[sizeof(ldr_err_app)];
1591 int rc1;
1592
1593 /* Check for invalid/corrupted touch application */
1594 rc1 = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(ldr_err_app),
1595 buf);
1596 if (rc1) {
1597 dev_err(cd->dev, "%s: Fail read r=%d\n", __func__, rc1);
1598 } else if (!memcmp(buf, ldr_err_app, sizeof(ldr_err_app))) {
1599 dev_err(cd->dev, "%s: Error launching touch application\n",
1600 __func__);
1601 mutex_lock(&cd->system_lock);
1602 cd->invalid_touch_app = true;
1603 mutex_unlock(&cd->system_lock);
1604 goto exit_no_wd;
1605 }
1606
1607 if (retry--)
1608 goto reset;
1609 goto exit;
1610 }
1611
1612 mutex_lock(&cd->system_lock);
1613 cd->invalid_touch_app = false;
1614 mutex_unlock(&cd->system_lock);
1615
1616 /* read sysinfo data */
1617 dev_vdbg(cd->dev, "%s: get sysinfo regs..\n", __func__);
1618 rc = cyttsp4_get_sysinfo_regs(cd);
1619 if (rc < 0) {
1620 dev_err(cd->dev, "%s: failed to get sysinfo regs rc=%d\n",
1621 __func__, rc);
1622 if (retry--)
1623 goto reset;
1624 goto exit;
1625 }
1626
1627 rc = cyttsp4_set_mode(cd, CY_MODE_OPERATIONAL);
1628 if (rc < 0) {
1629 dev_err(cd->dev, "%s: failed to set mode to operational rc=%d\n",
1630 __func__, rc);
1631 if (retry--)
1632 goto reset;
1633 goto exit;
1634 }
1635
1636 cyttsp4_lift_all(&cd->md);
1637
1638 /* restore to sleep if was suspended */
1639 mutex_lock(&cd->system_lock);
1640 if (cd->sleep_state == SS_SLEEP_ON) {
1641 cd->sleep_state = SS_SLEEP_OFF;
1642 mutex_unlock(&cd->system_lock);
1643 cyttsp4_core_sleep_(cd);
1644 goto exit_no_wd;
1645 }
1646 mutex_unlock(&cd->system_lock);
1647
1648exit:
1649 cyttsp4_start_wd_timer(cd);
1650exit_no_wd:
1651 return rc;
1652}
1653
1654static int cyttsp4_startup(struct cyttsp4 *cd)
1655{
1656 int rc;
1657
1658 mutex_lock(&cd->system_lock);
1659 cd->startup_state = STARTUP_RUNNING;
1660 mutex_unlock(&cd->system_lock);
1661
1662 rc = cyttsp4_request_exclusive(cd, cd->dev,
1663 CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT);
1664 if (rc < 0) {
1665 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
1666 __func__, cd->exclusive_dev, cd->dev);
1667 goto exit;
1668 }
1669
1670 rc = cyttsp4_startup_(cd);
1671
1672 if (cyttsp4_release_exclusive(cd, cd->dev) < 0)
1673 /* Don't return fail code, mode is already changed. */
1674 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
1675 else
1676 dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
1677
1678exit:
1679 mutex_lock(&cd->system_lock);
1680 cd->startup_state = STARTUP_NONE;
1681 mutex_unlock(&cd->system_lock);
1682
1683 /* Wake the waiters for end of startup */
1684 wake_up(&cd->wait_q);
1685
1686 return rc;
1687}
1688
1689static void cyttsp4_startup_work_function(struct work_struct *work)
1690{
1691 struct cyttsp4 *cd = container_of(work, struct cyttsp4, startup_work);
1692 int rc;
1693
1694 rc = cyttsp4_startup(cd);
1695 if (rc < 0)
1696 dev_err(cd->dev, "%s: Fail queued startup r=%d\n",
1697 __func__, rc);
1698}
1699
1700static void cyttsp4_free_si_ptrs(struct cyttsp4 *cd)
1701{
1702 struct cyttsp4_sysinfo *si = &cd->sysinfo;
1703
1704 if (!si)
1705 return;
1706
1707 kfree(si->si_ptrs.cydata);
1708 kfree(si->si_ptrs.test);
1709 kfree(si->si_ptrs.pcfg);
1710 kfree(si->si_ptrs.opcfg);
1711 kfree(si->si_ptrs.ddata);
1712 kfree(si->si_ptrs.mdata);
1713 kfree(si->btn);
1714 kfree(si->xy_mode);
1715 kfree(si->xy_data);
1716 kfree(si->btn_rec_data);
1717}
1718
1719#if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_RUNTIME)
1720static int cyttsp4_core_sleep(struct cyttsp4 *cd)
1721{
1722 int rc;
1723
1724 rc = cyttsp4_request_exclusive(cd, cd->dev,
1725 CY_CORE_SLEEP_REQUEST_EXCLUSIVE_TIMEOUT);
1726 if (rc < 0) {
1727 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
1728 __func__, cd->exclusive_dev, cd->dev);
1729 return 0;
1730 }
1731
1732 rc = cyttsp4_core_sleep_(cd);
1733
1734 if (cyttsp4_release_exclusive(cd, cd->dev) < 0)
1735 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
1736 else
1737 dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
1738
1739 return rc;
1740}
1741
1742static int cyttsp4_core_wake_(struct cyttsp4 *cd)
1743{
1744 struct device *dev = cd->dev;
1745 int rc;
1746 u8 mode;
1747 int t;
1748
1749 /* Already woken? */
1750 mutex_lock(&cd->system_lock);
1751 if (cd->sleep_state == SS_SLEEP_OFF) {
1752 mutex_unlock(&cd->system_lock);
1753 return 0;
1754 }
1755 cd->int_status &= ~CY_INT_IGNORE;
1756 cd->int_status |= CY_INT_AWAKE;
1757 cd->sleep_state = SS_WAKING;
1758
1759 if (cd->cpdata->power) {
1760 dev_dbg(dev, "%s: Power up HW\n", __func__);
1761 rc = cd->cpdata->power(cd->cpdata, 1, dev, &cd->ignore_irq);
1762 } else {
1763 dev_dbg(dev, "%s: No power function\n", __func__);
1764 rc = -ENOSYS;
1765 }
1766 if (rc < 0) {
1767 dev_err(dev, "%s: HW Power up fails r=%d\n",
1768 __func__, rc);
1769
1770 /* Initiate a read transaction to wake up */
1771 cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), &mode);
1772 } else
1773 dev_vdbg(cd->dev, "%s: HW power up succeeds\n",
1774 __func__);
1775 mutex_unlock(&cd->system_lock);
1776
1777 t = wait_event_timeout(cd->wait_q,
1778 (cd->int_status & CY_INT_AWAKE) == 0,
1779 msecs_to_jiffies(CY_CORE_WAKEUP_TIMEOUT));
1780 if (IS_TMO(t)) {
1781 dev_err(dev, "%s: TMO waiting for wakeup\n", __func__);
1782 mutex_lock(&cd->system_lock);
1783 cd->int_status &= ~CY_INT_AWAKE;
1784 /* Try starting up */
1785 cyttsp4_queue_startup_(cd);
1786 mutex_unlock(&cd->system_lock);
1787 }
1788
1789 mutex_lock(&cd->system_lock);
1790 cd->sleep_state = SS_SLEEP_OFF;
1791 mutex_unlock(&cd->system_lock);
1792
1793 cyttsp4_start_wd_timer(cd);
1794
1795 return 0;
1796}
1797
1798static int cyttsp4_core_wake(struct cyttsp4 *cd)
1799{
1800 int rc;
1801
1802 rc = cyttsp4_request_exclusive(cd, cd->dev,
1803 CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT);
1804 if (rc < 0) {
1805 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
1806 __func__, cd->exclusive_dev, cd->dev);
1807 return 0;
1808 }
1809
1810 rc = cyttsp4_core_wake_(cd);
1811
1812 if (cyttsp4_release_exclusive(cd, cd->dev) < 0)
1813 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
1814 else
1815 dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
1816
1817 return rc;
1818}
1819
1820static int cyttsp4_core_suspend(struct device *dev)
1821{
1822 struct cyttsp4 *cd = dev_get_drvdata(dev);
1823 struct cyttsp4_mt_data *md = &cd->md;
1824 int rc;
1825
1826 md->is_suspended = true;
1827
1828 rc = cyttsp4_core_sleep(cd);
1829 if (rc < 0) {
1830 dev_err(dev, "%s: Error on sleep\n", __func__);
1831 return -EAGAIN;
1832 }
1833 return 0;
1834}
1835
1836static int cyttsp4_core_resume(struct device *dev)
1837{
1838 struct cyttsp4 *cd = dev_get_drvdata(dev);
1839 struct cyttsp4_mt_data *md = &cd->md;
1840 int rc;
1841
1842 md->is_suspended = false;
1843
1844 rc = cyttsp4_core_wake(cd);
1845 if (rc < 0) {
1846 dev_err(dev, "%s: Error on wake\n", __func__);
1847 return -EAGAIN;
1848 }
1849
1850 return 0;
1851}
1852#endif
1853
1854const struct dev_pm_ops cyttsp4_pm_ops = {
1855 SET_SYSTEM_SLEEP_PM_OPS(cyttsp4_core_suspend, cyttsp4_core_resume)
1856 SET_RUNTIME_PM_OPS(cyttsp4_core_suspend, cyttsp4_core_resume, NULL)
1857};
1858EXPORT_SYMBOL_GPL(cyttsp4_pm_ops);
1859
1860static int cyttsp4_mt_open(struct input_dev *input)
1861{
1862 pm_runtime_get(input->dev.parent);
1863 return 0;
1864}
1865
1866static void cyttsp4_mt_close(struct input_dev *input)
1867{
1868 struct cyttsp4_mt_data *md = input_get_drvdata(input);
1869 mutex_lock(&md->report_lock);
1870 if (!md->is_suspended)
1871 pm_runtime_put(input->dev.parent);
1872 mutex_unlock(&md->report_lock);
1873}
1874
1875
1876static int cyttsp4_setup_input_device(struct cyttsp4 *cd)
1877{
1878 struct device *dev = cd->dev;
1879 struct cyttsp4_mt_data *md = &cd->md;
1880 int signal = CY_IGNORE_VALUE;
1881 int max_x, max_y, max_p, min, max;
1882 int max_x_tmp, max_y_tmp;
1883 int i;
1884 int rc;
1885
1886 dev_vdbg(dev, "%s: Initialize event signals\n", __func__);
1887 __set_bit(EV_ABS, md->input->evbit);
1888 __set_bit(EV_REL, md->input->evbit);
1889 __set_bit(EV_KEY, md->input->evbit);
1890
1891 max_x_tmp = md->si->si_ofs.max_x;
1892 max_y_tmp = md->si->si_ofs.max_y;
1893
1894 /* get maximum values from the sysinfo data */
1895 if (md->pdata->flags & CY_FLAG_FLIP) {
1896 max_x = max_y_tmp - 1;
1897 max_y = max_x_tmp - 1;
1898 } else {
1899 max_x = max_x_tmp - 1;
1900 max_y = max_y_tmp - 1;
1901 }
1902 max_p = md->si->si_ofs.max_p;
1903
1904 /* set event signal capabilities */
1905 for (i = 0; i < (md->pdata->frmwrk->size / CY_NUM_ABS_SET); i++) {
1906 signal = md->pdata->frmwrk->abs
1907 [(i * CY_NUM_ABS_SET) + CY_SIGNAL_OST];
1908 if (signal != CY_IGNORE_VALUE) {
1909 __set_bit(signal, md->input->absbit);
1910 min = md->pdata->frmwrk->abs
1911 [(i * CY_NUM_ABS_SET) + CY_MIN_OST];
1912 max = md->pdata->frmwrk->abs
1913 [(i * CY_NUM_ABS_SET) + CY_MAX_OST];
1914 if (i == CY_ABS_ID_OST) {
1915 /* shift track ids down to start at 0 */
1916 max = max - min;
1917 min = min - min;
1918 } else if (i == CY_ABS_X_OST)
1919 max = max_x;
1920 else if (i == CY_ABS_Y_OST)
1921 max = max_y;
1922 else if (i == CY_ABS_P_OST)
1923 max = max_p;
1924 input_set_abs_params(md->input, signal, min, max,
1925 md->pdata->frmwrk->abs
1926 [(i * CY_NUM_ABS_SET) + CY_FUZZ_OST],
1927 md->pdata->frmwrk->abs
1928 [(i * CY_NUM_ABS_SET) + CY_FLAT_OST]);
1929 dev_dbg(dev, "%s: register signal=%02X min=%d max=%d\n",
1930 __func__, signal, min, max);
1931 if ((i == CY_ABS_ID_OST) &&
1932 (md->si->si_ofs.tch_rec_size <
1933 CY_TMA4XX_TCH_REC_SIZE))
1934 break;
1935 }
1936 }
1937
1938 input_mt_init_slots(md->input, md->si->si_ofs.tch_abs[CY_TCH_T].max,
1939 INPUT_MT_DIRECT);
1940 rc = input_register_device(md->input);
1941 if (rc < 0)
1942 dev_err(dev, "%s: Error, failed register input device r=%d\n",
1943 __func__, rc);
1944 return rc;
1945}
1946
1947static int cyttsp4_mt_probe(struct cyttsp4 *cd)
1948{
1949 struct device *dev = cd->dev;
1950 struct cyttsp4_mt_data *md = &cd->md;
1951 struct cyttsp4_mt_platform_data *pdata = cd->pdata->mt_pdata;
1952 int rc = 0;
1953
1954 mutex_init(&md->report_lock);
1955 md->pdata = pdata;
1956 /* Create the input device and register it. */
1957 dev_vdbg(dev, "%s: Create the input device and register it\n",
1958 __func__);
1959 md->input = input_allocate_device();
1960 if (md->input == NULL) {
1961 dev_err(dev, "%s: Error, failed to allocate input device\n",
1962 __func__);
1963 rc = -ENOSYS;
1964 goto error_alloc_failed;
1965 }
1966
1967 md->input->name = pdata->inp_dev_name;
1968 scnprintf(md->phys, sizeof(md->phys)-1, "%s", dev_name(dev));
1969 md->input->phys = md->phys;
1970 md->input->id.bustype = cd->bus_ops->bustype;
1971 md->input->dev.parent = dev;
1972 md->input->open = cyttsp4_mt_open;
1973 md->input->close = cyttsp4_mt_close;
1974 input_set_drvdata(md->input, md);
1975
1976 /* get sysinfo */
1977 md->si = &cd->sysinfo;
1978 if (!md->si) {
1979 dev_err(dev, "%s: Fail get sysinfo pointer from core p=%p\n",
1980 __func__, md->si);
1981 goto error_get_sysinfo;
1982 }
1983
1984 rc = cyttsp4_setup_input_device(cd);
1985 if (rc)
1986 goto error_init_input;
1987
1988 return 0;
1989
1990error_init_input:
1991 input_free_device(md->input);
1992error_get_sysinfo:
1993 input_set_drvdata(md->input, NULL);
1994error_alloc_failed:
1995 dev_err(dev, "%s failed.\n", __func__);
1996 return rc;
1997}
1998
1999struct cyttsp4 *cyttsp4_probe(const struct cyttsp4_bus_ops *ops,
2000 struct device *dev, u16 irq, size_t xfer_buf_size)
2001{
2002 struct cyttsp4 *cd;
2003 struct cyttsp4_platform_data *pdata = dev_get_platdata(dev);
2004 unsigned long irq_flags;
2005 int rc = 0;
2006
2007 if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
2008 dev_err(dev, "%s: Missing platform data\n", __func__);
2009 rc = -ENODEV;
2010 goto error_no_pdata;
2011 }
2012
2013 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
2014 if (!cd) {
2015 dev_err(dev, "%s: Error, kzalloc\n", __func__);
2016 rc = -ENOMEM;
2017 goto error_alloc_data;
2018 }
2019
2020 cd->xfer_buf = kzalloc(xfer_buf_size, GFP_KERNEL);
2021 if (!cd->xfer_buf) {
2022 dev_err(dev, "%s: Error, kzalloc\n", __func__);
2023 rc = -ENOMEM;
2024 goto error_free_cd;
2025 }
2026
2027 /* Initialize device info */
2028 cd->dev = dev;
2029 cd->pdata = pdata;
2030 cd->cpdata = pdata->core_pdata;
2031 cd->bus_ops = ops;
2032
2033 /* Initialize mutexes and spinlocks */
2034 mutex_init(&cd->system_lock);
2035 mutex_init(&cd->adap_lock);
2036
2037 /* Initialize wait queue */
2038 init_waitqueue_head(&cd->wait_q);
2039
2040 /* Initialize works */
2041 INIT_WORK(&cd->startup_work, cyttsp4_startup_work_function);
2042 INIT_WORK(&cd->watchdog_work, cyttsp4_watchdog_work);
2043
2044 /* Initialize IRQ */
2045 cd->irq = gpio_to_irq(cd->cpdata->irq_gpio);
2046 if (cd->irq < 0) {
2047 rc = -EINVAL;
2048 goto error_free_xfer;
2049 }
2050
2051 dev_set_drvdata(dev, cd);
2052
2053 /* Call platform init function */
2054 if (cd->cpdata->init) {
2055 dev_dbg(cd->dev, "%s: Init HW\n", __func__);
2056 rc = cd->cpdata->init(cd->cpdata, 1, cd->dev);
2057 } else {
2058 dev_dbg(cd->dev, "%s: No HW INIT function\n", __func__);
2059 rc = 0;
2060 }
2061 if (rc < 0)
2062 dev_err(cd->dev, "%s: HW Init fail r=%d\n", __func__, rc);
2063
2064 dev_dbg(dev, "%s: initialize threaded irq=%d\n", __func__, cd->irq);
2065 if (cd->cpdata->level_irq_udelay > 0)
2066 /* use level triggered interrupts */
2067 irq_flags = IRQF_TRIGGER_LOW | IRQF_ONESHOT;
2068 else
2069 /* use edge triggered interrupts */
2070 irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
2071
2072 rc = request_threaded_irq(cd->irq, NULL, cyttsp4_irq, irq_flags,
2073 dev_name(dev), cd);
2074 if (rc < 0) {
2075 dev_err(dev, "%s: Error, could not request irq\n", __func__);
2076 goto error_request_irq;
2077 }
2078
2079 /* Setup watchdog timer */
2080 setup_timer(&cd->watchdog_timer, cyttsp4_watchdog_timer,
2081 (unsigned long)cd);
2082
2083 /*
2084 * call startup directly to ensure that the device
2085 * is tested before leaving the probe
2086 */
2087 rc = cyttsp4_startup(cd);
2088
2089 /* Do not fail probe if startup fails but the device is detected */
2090 if (rc < 0 && cd->mode == CY_MODE_UNKNOWN) {
2091 dev_err(cd->dev, "%s: Fail initial startup r=%d\n",
2092 __func__, rc);
2093 goto error_startup;
2094 }
2095
2096 rc = cyttsp4_mt_probe(cd);
2097 if (rc < 0) {
2098 dev_err(dev, "%s: Error, fail mt probe\n", __func__);
2099 goto error_startup;
2100 }
2101
2102 pm_runtime_enable(dev);
2103
2104 return cd;
2105
2106error_startup:
2107 cancel_work_sync(&cd->startup_work);
2108 cyttsp4_stop_wd_timer(cd);
2109 pm_runtime_disable(dev);
2110 cyttsp4_free_si_ptrs(cd);
2111 free_irq(cd->irq, cd);
2112error_request_irq:
2113 if (cd->cpdata->init)
2114 cd->cpdata->init(cd->cpdata, 0, dev);
2115error_free_xfer:
2116 kfree(cd->xfer_buf);
2117error_free_cd:
2118 kfree(cd);
2119error_alloc_data:
2120error_no_pdata:
2121 dev_err(dev, "%s failed.\n", __func__);
2122 return ERR_PTR(rc);
2123}
2124EXPORT_SYMBOL_GPL(cyttsp4_probe);
2125
2126static void cyttsp4_mt_release(struct cyttsp4_mt_data *md)
2127{
2128 input_unregister_device(md->input);
2129 input_set_drvdata(md->input, NULL);
2130}
2131
2132int cyttsp4_remove(struct cyttsp4 *cd)
2133{
2134 struct device *dev = cd->dev;
2135
2136 cyttsp4_mt_release(&cd->md);
2137
2138 /*
2139 * Suspend the device before freeing the startup_work and stopping
2140 * the watchdog since sleep function restarts watchdog on failure
2141 */
2142 pm_runtime_suspend(dev);
2143 pm_runtime_disable(dev);
2144
2145 cancel_work_sync(&cd->startup_work);
2146
2147 cyttsp4_stop_wd_timer(cd);
2148
2149 free_irq(cd->irq, cd);
2150 if (cd->cpdata->init)
2151 cd->cpdata->init(cd->cpdata, 0, dev);
2152 cyttsp4_free_si_ptrs(cd);
2153 kfree(cd);
2154 return 0;
2155}
2156EXPORT_SYMBOL_GPL(cyttsp4_remove);
2157
2158MODULE_LICENSE("GPL");
2159MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard touchscreen core driver");
2160MODULE_AUTHOR("Cypress");