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