Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / input / touchscreen / cyttsp4_core.c
blob4b22d49a0f49add22299e9c2d0bcc6d6937bd276
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * cyttsp4_core.c
4 * Cypress TrueTouch(TM) Standard Product V4 Core driver module.
5 * For use with Cypress Txx4xx parts.
6 * Supported parts include:
7 * TMA4XX
8 * TMA1036
10 * Copyright (C) 2012 Cypress Semiconductor
12 * Contact Cypress Semiconductor at www.cypress.com <ttdrivers@cypress.com>
15 #include "cyttsp4_core.h"
16 #include <linux/delay.h>
17 #include <linux/gpio.h>
18 #include <linux/input/mt.h>
19 #include <linux/interrupt.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
24 /* Timeout in ms. */
25 #define CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT 500
26 #define CY_CORE_SLEEP_REQUEST_EXCLUSIVE_TIMEOUT 5000
27 #define CY_CORE_MODE_CHANGE_TIMEOUT 1000
28 #define CY_CORE_RESET_AND_WAIT_TIMEOUT 500
29 #define CY_CORE_WAKEUP_TIMEOUT 500
31 #define CY_CORE_STARTUP_RETRY_COUNT 3
33 static const u8 ldr_exit[] = {
34 0xFF, 0x01, 0x3B, 0x00, 0x00, 0x4F, 0x6D, 0x17
37 static const u8 ldr_err_app[] = {
38 0x01, 0x02, 0x00, 0x00, 0x55, 0xDD, 0x17
41 static inline size_t merge_bytes(u8 high, u8 low)
43 return (high << 8) + low;
46 #ifdef VERBOSE_DEBUG
47 static void cyttsp4_pr_buf(struct device *dev, u8 *pr_buf, u8 *dptr, int size,
48 const char *data_name)
50 int i, k;
51 const char fmt[] = "%02X ";
52 int max;
54 if (!size)
55 return;
57 max = (CY_MAX_PRBUF_SIZE - 1) - sizeof(CY_PR_TRUNCATED);
59 pr_buf[0] = 0;
60 for (i = k = 0; i < size && k < max; i++, k += 3)
61 scnprintf(pr_buf + k, CY_MAX_PRBUF_SIZE, fmt, dptr[i]);
63 dev_vdbg(dev, "%s: %s[0..%d]=%s%s\n", __func__, data_name, size - 1,
64 pr_buf, size <= max ? "" : CY_PR_TRUNCATED);
66 #else
67 #define cyttsp4_pr_buf(dev, pr_buf, dptr, size, data_name) do { } while (0)
68 #endif
70 static int cyttsp4_load_status_regs(struct cyttsp4 *cd)
72 struct cyttsp4_sysinfo *si = &cd->sysinfo;
73 struct device *dev = cd->dev;
74 int rc;
76 rc = cyttsp4_adap_read(cd, CY_REG_BASE, si->si_ofs.mode_size,
77 si->xy_mode);
78 if (rc < 0)
79 dev_err(dev, "%s: fail read mode regs r=%d\n",
80 __func__, rc);
81 else
82 cyttsp4_pr_buf(dev, cd->pr_buf, si->xy_mode,
83 si->si_ofs.mode_size, "xy_mode");
85 return rc;
88 static int cyttsp4_handshake(struct cyttsp4 *cd, u8 mode)
90 u8 cmd = mode ^ CY_HST_TOGGLE;
91 int rc;
94 * Mode change issued, handshaking now will cause endless mode change
95 * requests, for sync mode modechange will do same with handshake
96 * */
97 if (mode & CY_HST_MODE_CHANGE)
98 return 0;
100 rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(cmd), &cmd);
101 if (rc < 0)
102 dev_err(cd->dev, "%s: bus write fail on handshake (ret=%d)\n",
103 __func__, rc);
105 return rc;
108 static int cyttsp4_hw_soft_reset(struct cyttsp4 *cd)
110 u8 cmd = CY_HST_RESET;
111 int rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(cmd), &cmd);
112 if (rc < 0) {
113 dev_err(cd->dev, "%s: FAILED to execute SOFT reset\n",
114 __func__);
115 return rc;
117 return 0;
120 static int cyttsp4_hw_hard_reset(struct cyttsp4 *cd)
122 if (cd->cpdata->xres) {
123 cd->cpdata->xres(cd->cpdata, cd->dev);
124 dev_dbg(cd->dev, "%s: execute HARD reset\n", __func__);
125 return 0;
127 dev_err(cd->dev, "%s: FAILED to execute HARD reset\n", __func__);
128 return -ENOSYS;
131 static int cyttsp4_hw_reset(struct cyttsp4 *cd)
133 int rc = cyttsp4_hw_hard_reset(cd);
134 if (rc == -ENOSYS)
135 rc = cyttsp4_hw_soft_reset(cd);
136 return rc;
140 * Gets number of bits for a touch filed as parameter,
141 * sets maximum value for field which is used as bit mask
142 * and returns number of bytes required for that field
144 static int cyttsp4_bits_2_bytes(unsigned int nbits, size_t *max)
146 *max = 1UL << nbits;
147 return (nbits + 7) / 8;
150 static int cyttsp4_si_data_offsets(struct cyttsp4 *cd)
152 struct cyttsp4_sysinfo *si = &cd->sysinfo;
153 int rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(si->si_data),
154 &si->si_data);
155 if (rc < 0) {
156 dev_err(cd->dev, "%s: fail read sysinfo data offsets r=%d\n",
157 __func__, rc);
158 return rc;
161 /* Print sysinfo data offsets */
162 cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)&si->si_data,
163 sizeof(si->si_data), "sysinfo_data_offsets");
165 /* convert sysinfo data offset bytes into integers */
167 si->si_ofs.map_sz = merge_bytes(si->si_data.map_szh,
168 si->si_data.map_szl);
169 si->si_ofs.map_sz = merge_bytes(si->si_data.map_szh,
170 si->si_data.map_szl);
171 si->si_ofs.cydata_ofs = merge_bytes(si->si_data.cydata_ofsh,
172 si->si_data.cydata_ofsl);
173 si->si_ofs.test_ofs = merge_bytes(si->si_data.test_ofsh,
174 si->si_data.test_ofsl);
175 si->si_ofs.pcfg_ofs = merge_bytes(si->si_data.pcfg_ofsh,
176 si->si_data.pcfg_ofsl);
177 si->si_ofs.opcfg_ofs = merge_bytes(si->si_data.opcfg_ofsh,
178 si->si_data.opcfg_ofsl);
179 si->si_ofs.ddata_ofs = merge_bytes(si->si_data.ddata_ofsh,
180 si->si_data.ddata_ofsl);
181 si->si_ofs.mdata_ofs = merge_bytes(si->si_data.mdata_ofsh,
182 si->si_data.mdata_ofsl);
183 return rc;
186 static int cyttsp4_si_get_cydata(struct cyttsp4 *cd)
188 struct cyttsp4_sysinfo *si = &cd->sysinfo;
189 int read_offset;
190 int mfgid_sz, calc_mfgid_sz;
191 void *p;
192 int rc;
194 if (si->si_ofs.test_ofs <= si->si_ofs.cydata_ofs) {
195 dev_err(cd->dev,
196 "%s: invalid offset test_ofs: %zu, cydata_ofs: %zu\n",
197 __func__, si->si_ofs.test_ofs, si->si_ofs.cydata_ofs);
198 return -EINVAL;
201 si->si_ofs.cydata_size = si->si_ofs.test_ofs - si->si_ofs.cydata_ofs;
202 dev_dbg(cd->dev, "%s: cydata size: %zd\n", __func__,
203 si->si_ofs.cydata_size);
205 p = krealloc(si->si_ptrs.cydata, si->si_ofs.cydata_size, GFP_KERNEL);
206 if (p == NULL) {
207 dev_err(cd->dev, "%s: failed to allocate cydata memory\n",
208 __func__);
209 return -ENOMEM;
211 si->si_ptrs.cydata = p;
213 read_offset = si->si_ofs.cydata_ofs;
215 /* Read the CYDA registers up to MFGID field */
216 rc = cyttsp4_adap_read(cd, read_offset,
217 offsetof(struct cyttsp4_cydata, mfgid_sz)
218 + sizeof(si->si_ptrs.cydata->mfgid_sz),
219 si->si_ptrs.cydata);
220 if (rc < 0) {
221 dev_err(cd->dev, "%s: fail read cydata r=%d\n",
222 __func__, rc);
223 return rc;
226 /* Check MFGID size */
227 mfgid_sz = si->si_ptrs.cydata->mfgid_sz;
228 calc_mfgid_sz = si->si_ofs.cydata_size - sizeof(struct cyttsp4_cydata);
229 if (mfgid_sz != calc_mfgid_sz) {
230 dev_err(cd->dev, "%s: mismatch in MFGID size, reported:%d calculated:%d\n",
231 __func__, mfgid_sz, calc_mfgid_sz);
232 return -EINVAL;
235 read_offset += offsetof(struct cyttsp4_cydata, mfgid_sz)
236 + sizeof(si->si_ptrs.cydata->mfgid_sz);
238 /* Read the CYDA registers for MFGID field */
239 rc = cyttsp4_adap_read(cd, read_offset, si->si_ptrs.cydata->mfgid_sz,
240 si->si_ptrs.cydata->mfg_id);
241 if (rc < 0) {
242 dev_err(cd->dev, "%s: fail read cydata r=%d\n",
243 __func__, rc);
244 return rc;
247 read_offset += si->si_ptrs.cydata->mfgid_sz;
249 /* Read the rest of the CYDA registers */
250 rc = cyttsp4_adap_read(cd, read_offset,
251 sizeof(struct cyttsp4_cydata)
252 - offsetof(struct cyttsp4_cydata, cyito_idh),
253 &si->si_ptrs.cydata->cyito_idh);
254 if (rc < 0) {
255 dev_err(cd->dev, "%s: fail read cydata r=%d\n",
256 __func__, rc);
257 return rc;
260 cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)si->si_ptrs.cydata,
261 si->si_ofs.cydata_size, "sysinfo_cydata");
262 return rc;
265 static int cyttsp4_si_get_test_data(struct cyttsp4 *cd)
267 struct cyttsp4_sysinfo *si = &cd->sysinfo;
268 void *p;
269 int rc;
271 if (si->si_ofs.pcfg_ofs <= si->si_ofs.test_ofs) {
272 dev_err(cd->dev,
273 "%s: invalid offset pcfg_ofs: %zu, test_ofs: %zu\n",
274 __func__, si->si_ofs.pcfg_ofs, si->si_ofs.test_ofs);
275 return -EINVAL;
278 si->si_ofs.test_size = si->si_ofs.pcfg_ofs - si->si_ofs.test_ofs;
280 p = krealloc(si->si_ptrs.test, si->si_ofs.test_size, GFP_KERNEL);
281 if (p == NULL) {
282 dev_err(cd->dev, "%s: failed to allocate test memory\n",
283 __func__);
284 return -ENOMEM;
286 si->si_ptrs.test = p;
288 rc = cyttsp4_adap_read(cd, si->si_ofs.test_ofs, si->si_ofs.test_size,
289 si->si_ptrs.test);
290 if (rc < 0) {
291 dev_err(cd->dev, "%s: fail read test data r=%d\n",
292 __func__, rc);
293 return rc;
296 cyttsp4_pr_buf(cd->dev, cd->pr_buf,
297 (u8 *)si->si_ptrs.test, si->si_ofs.test_size,
298 "sysinfo_test_data");
299 if (si->si_ptrs.test->post_codel &
300 CY_POST_CODEL_WDG_RST)
301 dev_info(cd->dev, "%s: %s codel=%02X\n",
302 __func__, "Reset was a WATCHDOG RESET",
303 si->si_ptrs.test->post_codel);
305 if (!(si->si_ptrs.test->post_codel &
306 CY_POST_CODEL_CFG_DATA_CRC_FAIL))
307 dev_info(cd->dev, "%s: %s codel=%02X\n", __func__,
308 "Config Data CRC FAIL",
309 si->si_ptrs.test->post_codel);
311 if (!(si->si_ptrs.test->post_codel &
312 CY_POST_CODEL_PANEL_TEST_FAIL))
313 dev_info(cd->dev, "%s: %s codel=%02X\n",
314 __func__, "PANEL TEST FAIL",
315 si->si_ptrs.test->post_codel);
317 dev_info(cd->dev, "%s: SCANNING is %s codel=%02X\n",
318 __func__, si->si_ptrs.test->post_codel & 0x08 ?
319 "ENABLED" : "DISABLED",
320 si->si_ptrs.test->post_codel);
321 return rc;
324 static int cyttsp4_si_get_pcfg_data(struct cyttsp4 *cd)
326 struct cyttsp4_sysinfo *si = &cd->sysinfo;
327 void *p;
328 int rc;
330 if (si->si_ofs.opcfg_ofs <= si->si_ofs.pcfg_ofs) {
331 dev_err(cd->dev,
332 "%s: invalid offset opcfg_ofs: %zu, pcfg_ofs: %zu\n",
333 __func__, si->si_ofs.opcfg_ofs, si->si_ofs.pcfg_ofs);
334 return -EINVAL;
337 si->si_ofs.pcfg_size = si->si_ofs.opcfg_ofs - si->si_ofs.pcfg_ofs;
339 p = krealloc(si->si_ptrs.pcfg, si->si_ofs.pcfg_size, GFP_KERNEL);
340 if (p == NULL) {
341 dev_err(cd->dev, "%s: failed to allocate pcfg memory\n",
342 __func__);
343 return -ENOMEM;
345 si->si_ptrs.pcfg = p;
347 rc = cyttsp4_adap_read(cd, si->si_ofs.pcfg_ofs, si->si_ofs.pcfg_size,
348 si->si_ptrs.pcfg);
349 if (rc < 0) {
350 dev_err(cd->dev, "%s: fail read pcfg data r=%d\n",
351 __func__, rc);
352 return rc;
355 si->si_ofs.max_x = merge_bytes((si->si_ptrs.pcfg->res_xh
356 & CY_PCFG_RESOLUTION_X_MASK), si->si_ptrs.pcfg->res_xl);
357 si->si_ofs.x_origin = !!(si->si_ptrs.pcfg->res_xh
358 & CY_PCFG_ORIGIN_X_MASK);
359 si->si_ofs.max_y = merge_bytes((si->si_ptrs.pcfg->res_yh
360 & CY_PCFG_RESOLUTION_Y_MASK), si->si_ptrs.pcfg->res_yl);
361 si->si_ofs.y_origin = !!(si->si_ptrs.pcfg->res_yh
362 & CY_PCFG_ORIGIN_Y_MASK);
363 si->si_ofs.max_p = merge_bytes(si->si_ptrs.pcfg->max_zh,
364 si->si_ptrs.pcfg->max_zl);
366 cyttsp4_pr_buf(cd->dev, cd->pr_buf,
367 (u8 *)si->si_ptrs.pcfg,
368 si->si_ofs.pcfg_size, "sysinfo_pcfg_data");
369 return rc;
372 static int cyttsp4_si_get_opcfg_data(struct cyttsp4 *cd)
374 struct cyttsp4_sysinfo *si = &cd->sysinfo;
375 struct cyttsp4_tch_abs_params *tch;
376 struct cyttsp4_tch_rec_params *tch_old, *tch_new;
377 enum cyttsp4_tch_abs abs;
378 int i;
379 void *p;
380 int rc;
382 if (si->si_ofs.ddata_ofs <= si->si_ofs.opcfg_ofs) {
383 dev_err(cd->dev,
384 "%s: invalid offset ddata_ofs: %zu, opcfg_ofs: %zu\n",
385 __func__, si->si_ofs.ddata_ofs, si->si_ofs.opcfg_ofs);
386 return -EINVAL;
389 si->si_ofs.opcfg_size = si->si_ofs.ddata_ofs - si->si_ofs.opcfg_ofs;
391 p = krealloc(si->si_ptrs.opcfg, si->si_ofs.opcfg_size, GFP_KERNEL);
392 if (p == NULL) {
393 dev_err(cd->dev, "%s: failed to allocate opcfg memory\n",
394 __func__);
395 return -ENOMEM;
397 si->si_ptrs.opcfg = p;
399 rc = cyttsp4_adap_read(cd, si->si_ofs.opcfg_ofs, si->si_ofs.opcfg_size,
400 si->si_ptrs.opcfg);
401 if (rc < 0) {
402 dev_err(cd->dev, "%s: fail read opcfg data r=%d\n",
403 __func__, rc);
404 return rc;
406 si->si_ofs.cmd_ofs = si->si_ptrs.opcfg->cmd_ofs;
407 si->si_ofs.rep_ofs = si->si_ptrs.opcfg->rep_ofs;
408 si->si_ofs.rep_sz = (si->si_ptrs.opcfg->rep_szh * 256) +
409 si->si_ptrs.opcfg->rep_szl;
410 si->si_ofs.num_btns = si->si_ptrs.opcfg->num_btns;
411 si->si_ofs.num_btn_regs = (si->si_ofs.num_btns +
412 CY_NUM_BTN_PER_REG - 1) / CY_NUM_BTN_PER_REG;
413 si->si_ofs.tt_stat_ofs = si->si_ptrs.opcfg->tt_stat_ofs;
414 si->si_ofs.obj_cfg0 = si->si_ptrs.opcfg->obj_cfg0;
415 si->si_ofs.max_tchs = si->si_ptrs.opcfg->max_tchs &
416 CY_BYTE_OFS_MASK;
417 si->si_ofs.tch_rec_size = si->si_ptrs.opcfg->tch_rec_size &
418 CY_BYTE_OFS_MASK;
420 /* Get the old touch fields */
421 for (abs = CY_TCH_X; abs < CY_NUM_TCH_FIELDS; abs++) {
422 tch = &si->si_ofs.tch_abs[abs];
423 tch_old = &si->si_ptrs.opcfg->tch_rec_old[abs];
425 tch->ofs = tch_old->loc & CY_BYTE_OFS_MASK;
426 tch->size = cyttsp4_bits_2_bytes(tch_old->size,
427 &tch->max);
428 tch->bofs = (tch_old->loc & CY_BOFS_MASK) >> CY_BOFS_SHIFT;
431 /* button fields */
432 si->si_ofs.btn_rec_size = si->si_ptrs.opcfg->btn_rec_size;
433 si->si_ofs.btn_diff_ofs = si->si_ptrs.opcfg->btn_diff_ofs;
434 si->si_ofs.btn_diff_size = si->si_ptrs.opcfg->btn_diff_size;
436 if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE) {
437 /* Get the extended touch fields */
438 for (i = 0; i < CY_NUM_EXT_TCH_FIELDS; abs++, i++) {
439 tch = &si->si_ofs.tch_abs[abs];
440 tch_new = &si->si_ptrs.opcfg->tch_rec_new[i];
442 tch->ofs = tch_new->loc & CY_BYTE_OFS_MASK;
443 tch->size = cyttsp4_bits_2_bytes(tch_new->size,
444 &tch->max);
445 tch->bofs = (tch_new->loc & CY_BOFS_MASK) >> CY_BOFS_SHIFT;
449 for (abs = 0; abs < CY_TCH_NUM_ABS; abs++) {
450 dev_dbg(cd->dev, "%s: tch_rec_%s\n", __func__,
451 cyttsp4_tch_abs_string[abs]);
452 dev_dbg(cd->dev, "%s: ofs =%2zd\n", __func__,
453 si->si_ofs.tch_abs[abs].ofs);
454 dev_dbg(cd->dev, "%s: siz =%2zd\n", __func__,
455 si->si_ofs.tch_abs[abs].size);
456 dev_dbg(cd->dev, "%s: max =%2zd\n", __func__,
457 si->si_ofs.tch_abs[abs].max);
458 dev_dbg(cd->dev, "%s: bofs=%2zd\n", __func__,
459 si->si_ofs.tch_abs[abs].bofs);
462 si->si_ofs.mode_size = si->si_ofs.tt_stat_ofs + 1;
463 si->si_ofs.data_size = si->si_ofs.max_tchs *
464 si->si_ptrs.opcfg->tch_rec_size;
466 cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)si->si_ptrs.opcfg,
467 si->si_ofs.opcfg_size, "sysinfo_opcfg_data");
469 return 0;
472 static int cyttsp4_si_get_ddata(struct cyttsp4 *cd)
474 struct cyttsp4_sysinfo *si = &cd->sysinfo;
475 void *p;
476 int rc;
478 si->si_ofs.ddata_size = si->si_ofs.mdata_ofs - si->si_ofs.ddata_ofs;
480 p = krealloc(si->si_ptrs.ddata, si->si_ofs.ddata_size, GFP_KERNEL);
481 if (p == NULL) {
482 dev_err(cd->dev, "%s: fail alloc ddata memory\n", __func__);
483 return -ENOMEM;
485 si->si_ptrs.ddata = p;
487 rc = cyttsp4_adap_read(cd, si->si_ofs.ddata_ofs, si->si_ofs.ddata_size,
488 si->si_ptrs.ddata);
489 if (rc < 0)
490 dev_err(cd->dev, "%s: fail read ddata data r=%d\n",
491 __func__, rc);
492 else
493 cyttsp4_pr_buf(cd->dev, cd->pr_buf,
494 (u8 *)si->si_ptrs.ddata,
495 si->si_ofs.ddata_size, "sysinfo_ddata");
496 return rc;
499 static int cyttsp4_si_get_mdata(struct cyttsp4 *cd)
501 struct cyttsp4_sysinfo *si = &cd->sysinfo;
502 void *p;
503 int rc;
505 si->si_ofs.mdata_size = si->si_ofs.map_sz - si->si_ofs.mdata_ofs;
507 p = krealloc(si->si_ptrs.mdata, si->si_ofs.mdata_size, GFP_KERNEL);
508 if (p == NULL) {
509 dev_err(cd->dev, "%s: fail alloc mdata memory\n", __func__);
510 return -ENOMEM;
512 si->si_ptrs.mdata = p;
514 rc = cyttsp4_adap_read(cd, si->si_ofs.mdata_ofs, si->si_ofs.mdata_size,
515 si->si_ptrs.mdata);
516 if (rc < 0)
517 dev_err(cd->dev, "%s: fail read mdata data r=%d\n",
518 __func__, rc);
519 else
520 cyttsp4_pr_buf(cd->dev, cd->pr_buf,
521 (u8 *)si->si_ptrs.mdata,
522 si->si_ofs.mdata_size, "sysinfo_mdata");
523 return rc;
526 static int cyttsp4_si_get_btn_data(struct cyttsp4 *cd)
528 struct cyttsp4_sysinfo *si = &cd->sysinfo;
529 int btn;
530 int num_defined_keys;
531 u16 *key_table;
532 void *p;
533 int rc = 0;
535 if (si->si_ofs.num_btns) {
536 si->si_ofs.btn_keys_size = si->si_ofs.num_btns *
537 sizeof(struct cyttsp4_btn);
539 p = krealloc(si->btn, si->si_ofs.btn_keys_size,
540 GFP_KERNEL|__GFP_ZERO);
541 if (p == NULL) {
542 dev_err(cd->dev, "%s: %s\n", __func__,
543 "fail alloc btn_keys memory");
544 return -ENOMEM;
546 si->btn = p;
548 if (cd->cpdata->sett[CY_IC_GRPNUM_BTN_KEYS] == NULL)
549 num_defined_keys = 0;
550 else if (cd->cpdata->sett[CY_IC_GRPNUM_BTN_KEYS]->data == NULL)
551 num_defined_keys = 0;
552 else
553 num_defined_keys = cd->cpdata->sett
554 [CY_IC_GRPNUM_BTN_KEYS]->size;
556 for (btn = 0; btn < si->si_ofs.num_btns &&
557 btn < num_defined_keys; btn++) {
558 key_table = (u16 *)cd->cpdata->sett
559 [CY_IC_GRPNUM_BTN_KEYS]->data;
560 si->btn[btn].key_code = key_table[btn];
561 si->btn[btn].state = CY_BTN_RELEASED;
562 si->btn[btn].enabled = true;
564 for (; btn < si->si_ofs.num_btns; btn++) {
565 si->btn[btn].key_code = KEY_RESERVED;
566 si->btn[btn].state = CY_BTN_RELEASED;
567 si->btn[btn].enabled = true;
570 return rc;
573 si->si_ofs.btn_keys_size = 0;
574 kfree(si->btn);
575 si->btn = NULL;
576 return rc;
579 static int cyttsp4_si_get_op_data_ptrs(struct cyttsp4 *cd)
581 struct cyttsp4_sysinfo *si = &cd->sysinfo;
582 void *p;
584 p = krealloc(si->xy_mode, si->si_ofs.mode_size, GFP_KERNEL|__GFP_ZERO);
585 if (p == NULL)
586 return -ENOMEM;
587 si->xy_mode = p;
589 p = krealloc(si->xy_data, si->si_ofs.data_size, GFP_KERNEL|__GFP_ZERO);
590 if (p == NULL)
591 return -ENOMEM;
592 si->xy_data = p;
594 p = krealloc(si->btn_rec_data,
595 si->si_ofs.btn_rec_size * si->si_ofs.num_btns,
596 GFP_KERNEL|__GFP_ZERO);
597 if (p == NULL)
598 return -ENOMEM;
599 si->btn_rec_data = p;
601 return 0;
604 static void cyttsp4_si_put_log_data(struct cyttsp4 *cd)
606 struct cyttsp4_sysinfo *si = &cd->sysinfo;
607 dev_dbg(cd->dev, "%s: cydata_ofs =%4zd siz=%4zd\n", __func__,
608 si->si_ofs.cydata_ofs, si->si_ofs.cydata_size);
609 dev_dbg(cd->dev, "%s: test_ofs =%4zd siz=%4zd\n", __func__,
610 si->si_ofs.test_ofs, si->si_ofs.test_size);
611 dev_dbg(cd->dev, "%s: pcfg_ofs =%4zd siz=%4zd\n", __func__,
612 si->si_ofs.pcfg_ofs, si->si_ofs.pcfg_size);
613 dev_dbg(cd->dev, "%s: opcfg_ofs =%4zd siz=%4zd\n", __func__,
614 si->si_ofs.opcfg_ofs, si->si_ofs.opcfg_size);
615 dev_dbg(cd->dev, "%s: ddata_ofs =%4zd siz=%4zd\n", __func__,
616 si->si_ofs.ddata_ofs, si->si_ofs.ddata_size);
617 dev_dbg(cd->dev, "%s: mdata_ofs =%4zd siz=%4zd\n", __func__,
618 si->si_ofs.mdata_ofs, si->si_ofs.mdata_size);
620 dev_dbg(cd->dev, "%s: cmd_ofs =%4zd\n", __func__,
621 si->si_ofs.cmd_ofs);
622 dev_dbg(cd->dev, "%s: rep_ofs =%4zd\n", __func__,
623 si->si_ofs.rep_ofs);
624 dev_dbg(cd->dev, "%s: rep_sz =%4zd\n", __func__,
625 si->si_ofs.rep_sz);
626 dev_dbg(cd->dev, "%s: num_btns =%4zd\n", __func__,
627 si->si_ofs.num_btns);
628 dev_dbg(cd->dev, "%s: num_btn_regs =%4zd\n", __func__,
629 si->si_ofs.num_btn_regs);
630 dev_dbg(cd->dev, "%s: tt_stat_ofs =%4zd\n", __func__,
631 si->si_ofs.tt_stat_ofs);
632 dev_dbg(cd->dev, "%s: tch_rec_size =%4zd\n", __func__,
633 si->si_ofs.tch_rec_size);
634 dev_dbg(cd->dev, "%s: max_tchs =%4zd\n", __func__,
635 si->si_ofs.max_tchs);
636 dev_dbg(cd->dev, "%s: mode_size =%4zd\n", __func__,
637 si->si_ofs.mode_size);
638 dev_dbg(cd->dev, "%s: data_size =%4zd\n", __func__,
639 si->si_ofs.data_size);
640 dev_dbg(cd->dev, "%s: map_sz =%4zd\n", __func__,
641 si->si_ofs.map_sz);
643 dev_dbg(cd->dev, "%s: btn_rec_size =%2zd\n", __func__,
644 si->si_ofs.btn_rec_size);
645 dev_dbg(cd->dev, "%s: btn_diff_ofs =%2zd\n", __func__,
646 si->si_ofs.btn_diff_ofs);
647 dev_dbg(cd->dev, "%s: btn_diff_size =%2zd\n", __func__,
648 si->si_ofs.btn_diff_size);
650 dev_dbg(cd->dev, "%s: max_x = 0x%04zX (%zd)\n", __func__,
651 si->si_ofs.max_x, si->si_ofs.max_x);
652 dev_dbg(cd->dev, "%s: x_origin = %zd (%s)\n", __func__,
653 si->si_ofs.x_origin,
654 si->si_ofs.x_origin == CY_NORMAL_ORIGIN ?
655 "left corner" : "right corner");
656 dev_dbg(cd->dev, "%s: max_y = 0x%04zX (%zd)\n", __func__,
657 si->si_ofs.max_y, si->si_ofs.max_y);
658 dev_dbg(cd->dev, "%s: y_origin = %zd (%s)\n", __func__,
659 si->si_ofs.y_origin,
660 si->si_ofs.y_origin == CY_NORMAL_ORIGIN ?
661 "upper corner" : "lower corner");
662 dev_dbg(cd->dev, "%s: max_p = 0x%04zX (%zd)\n", __func__,
663 si->si_ofs.max_p, si->si_ofs.max_p);
665 dev_dbg(cd->dev, "%s: xy_mode=%p xy_data=%p\n", __func__,
666 si->xy_mode, si->xy_data);
669 static int cyttsp4_get_sysinfo_regs(struct cyttsp4 *cd)
671 struct cyttsp4_sysinfo *si = &cd->sysinfo;
672 int rc;
674 rc = cyttsp4_si_data_offsets(cd);
675 if (rc < 0)
676 return rc;
678 rc = cyttsp4_si_get_cydata(cd);
679 if (rc < 0)
680 return rc;
682 rc = cyttsp4_si_get_test_data(cd);
683 if (rc < 0)
684 return rc;
686 rc = cyttsp4_si_get_pcfg_data(cd);
687 if (rc < 0)
688 return rc;
690 rc = cyttsp4_si_get_opcfg_data(cd);
691 if (rc < 0)
692 return rc;
694 rc = cyttsp4_si_get_ddata(cd);
695 if (rc < 0)
696 return rc;
698 rc = cyttsp4_si_get_mdata(cd);
699 if (rc < 0)
700 return rc;
702 rc = cyttsp4_si_get_btn_data(cd);
703 if (rc < 0)
704 return rc;
706 rc = cyttsp4_si_get_op_data_ptrs(cd);
707 if (rc < 0) {
708 dev_err(cd->dev, "%s: failed to get_op_data\n",
709 __func__);
710 return rc;
713 cyttsp4_si_put_log_data(cd);
715 /* provide flow control handshake */
716 rc = cyttsp4_handshake(cd, si->si_data.hst_mode);
717 if (rc < 0)
718 dev_err(cd->dev, "%s: handshake fail on sysinfo reg\n",
719 __func__);
721 si->ready = true;
722 return rc;
725 static void cyttsp4_queue_startup_(struct cyttsp4 *cd)
727 if (cd->startup_state == STARTUP_NONE) {
728 cd->startup_state = STARTUP_QUEUED;
729 schedule_work(&cd->startup_work);
730 dev_dbg(cd->dev, "%s: cyttsp4_startup queued\n", __func__);
731 } else {
732 dev_dbg(cd->dev, "%s: startup_state = %d\n", __func__,
733 cd->startup_state);
737 static void cyttsp4_report_slot_liftoff(struct cyttsp4_mt_data *md,
738 int max_slots)
740 int t;
742 if (md->num_prv_tch == 0)
743 return;
745 for (t = 0; t < max_slots; t++) {
746 input_mt_slot(md->input, t);
747 input_mt_report_slot_state(md->input,
748 MT_TOOL_FINGER, false);
752 static void cyttsp4_lift_all(struct cyttsp4_mt_data *md)
754 if (!md->si)
755 return;
757 if (md->num_prv_tch != 0) {
758 cyttsp4_report_slot_liftoff(md,
759 md->si->si_ofs.tch_abs[CY_TCH_T].max);
760 input_sync(md->input);
761 md->num_prv_tch = 0;
765 static void cyttsp4_get_touch_axis(struct cyttsp4_mt_data *md,
766 int *axis, int size, int max, u8 *xy_data, int bofs)
768 int nbyte;
769 int next;
771 for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
772 dev_vdbg(&md->input->dev,
773 "%s: *axis=%02X(%d) size=%d max=%08X xy_data=%p"
774 " xy_data[%d]=%02X(%d) bofs=%d\n",
775 __func__, *axis, *axis, size, max, xy_data, next,
776 xy_data[next], xy_data[next], bofs);
777 *axis = (*axis * 256) + (xy_data[next] >> bofs);
778 next++;
781 *axis &= max - 1;
783 dev_vdbg(&md->input->dev,
784 "%s: *axis=%02X(%d) size=%d max=%08X xy_data=%p"
785 " xy_data[%d]=%02X(%d)\n",
786 __func__, *axis, *axis, size, max, xy_data, next,
787 xy_data[next], xy_data[next]);
790 static void cyttsp4_get_touch(struct cyttsp4_mt_data *md,
791 struct cyttsp4_touch *touch, u8 *xy_data)
793 struct device *dev = &md->input->dev;
794 struct cyttsp4_sysinfo *si = md->si;
795 enum cyttsp4_tch_abs abs;
796 bool flipped;
798 for (abs = CY_TCH_X; abs < CY_TCH_NUM_ABS; abs++) {
799 cyttsp4_get_touch_axis(md, &touch->abs[abs],
800 si->si_ofs.tch_abs[abs].size,
801 si->si_ofs.tch_abs[abs].max,
802 xy_data + si->si_ofs.tch_abs[abs].ofs,
803 si->si_ofs.tch_abs[abs].bofs);
804 dev_vdbg(dev, "%s: get %s=%04X(%d)\n", __func__,
805 cyttsp4_tch_abs_string[abs],
806 touch->abs[abs], touch->abs[abs]);
809 if (md->pdata->flags & CY_FLAG_FLIP) {
810 swap(touch->abs[CY_TCH_X], touch->abs[CY_TCH_Y]);
811 flipped = true;
812 } else
813 flipped = false;
815 if (md->pdata->flags & CY_FLAG_INV_X) {
816 if (flipped)
817 touch->abs[CY_TCH_X] = md->si->si_ofs.max_y -
818 touch->abs[CY_TCH_X];
819 else
820 touch->abs[CY_TCH_X] = md->si->si_ofs.max_x -
821 touch->abs[CY_TCH_X];
823 if (md->pdata->flags & CY_FLAG_INV_Y) {
824 if (flipped)
825 touch->abs[CY_TCH_Y] = md->si->si_ofs.max_x -
826 touch->abs[CY_TCH_Y];
827 else
828 touch->abs[CY_TCH_Y] = md->si->si_ofs.max_y -
829 touch->abs[CY_TCH_Y];
832 dev_vdbg(dev, "%s: flip=%s inv-x=%s inv-y=%s x=%04X(%d) y=%04X(%d)\n",
833 __func__, flipped ? "true" : "false",
834 md->pdata->flags & CY_FLAG_INV_X ? "true" : "false",
835 md->pdata->flags & CY_FLAG_INV_Y ? "true" : "false",
836 touch->abs[CY_TCH_X], touch->abs[CY_TCH_X],
837 touch->abs[CY_TCH_Y], touch->abs[CY_TCH_Y]);
840 static void cyttsp4_final_sync(struct input_dev *input, int max_slots, int *ids)
842 int t;
844 for (t = 0; t < max_slots; t++) {
845 if (ids[t])
846 continue;
847 input_mt_slot(input, t);
848 input_mt_report_slot_state(input, MT_TOOL_FINGER, false);
851 input_sync(input);
854 static void cyttsp4_get_mt_touches(struct cyttsp4_mt_data *md, int num_cur_tch)
856 struct device *dev = &md->input->dev;
857 struct cyttsp4_sysinfo *si = md->si;
858 struct cyttsp4_touch tch;
859 int sig;
860 int i, j, t = 0;
861 int ids[max(CY_TMA1036_MAX_TCH, CY_TMA4XX_MAX_TCH)];
863 memset(ids, 0, si->si_ofs.tch_abs[CY_TCH_T].max * sizeof(int));
864 for (i = 0; i < num_cur_tch; i++) {
865 cyttsp4_get_touch(md, &tch, si->xy_data +
866 (i * si->si_ofs.tch_rec_size));
867 if ((tch.abs[CY_TCH_T] < md->pdata->frmwrk->abs
868 [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MIN_OST]) ||
869 (tch.abs[CY_TCH_T] > md->pdata->frmwrk->abs
870 [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MAX_OST])) {
871 dev_err(dev, "%s: tch=%d -> bad trk_id=%d max_id=%d\n",
872 __func__, i, tch.abs[CY_TCH_T],
873 md->pdata->frmwrk->abs[(CY_ABS_ID_OST *
874 CY_NUM_ABS_SET) + CY_MAX_OST]);
875 continue;
878 /* use 0 based track id's */
879 sig = md->pdata->frmwrk->abs
880 [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + 0];
881 if (sig != CY_IGNORE_VALUE) {
882 t = tch.abs[CY_TCH_T] - md->pdata->frmwrk->abs
883 [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MIN_OST];
884 if (tch.abs[CY_TCH_E] == CY_EV_LIFTOFF) {
885 dev_dbg(dev, "%s: t=%d e=%d lift-off\n",
886 __func__, t, tch.abs[CY_TCH_E]);
887 goto cyttsp4_get_mt_touches_pr_tch;
889 input_mt_slot(md->input, t);
890 input_mt_report_slot_state(md->input, MT_TOOL_FINGER,
891 true);
892 ids[t] = true;
895 /* all devices: position and pressure fields */
896 for (j = 0; j <= CY_ABS_W_OST; j++) {
897 sig = md->pdata->frmwrk->abs[((CY_ABS_X_OST + j) *
898 CY_NUM_ABS_SET) + 0];
899 if (sig != CY_IGNORE_VALUE)
900 input_report_abs(md->input, sig,
901 tch.abs[CY_TCH_X + j]);
903 if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE) {
905 * TMA400 size and orientation fields:
906 * if pressure is non-zero and major touch
907 * signal is zero, then set major and minor touch
908 * signals to minimum non-zero value
910 if (tch.abs[CY_TCH_P] > 0 && tch.abs[CY_TCH_MAJ] == 0)
911 tch.abs[CY_TCH_MAJ] = tch.abs[CY_TCH_MIN] = 1;
913 /* Get the extended touch fields */
914 for (j = 0; j < CY_NUM_EXT_TCH_FIELDS; j++) {
915 sig = md->pdata->frmwrk->abs
916 [((CY_ABS_MAJ_OST + j) *
917 CY_NUM_ABS_SET) + 0];
918 if (sig != CY_IGNORE_VALUE)
919 input_report_abs(md->input, sig,
920 tch.abs[CY_TCH_MAJ + j]);
924 cyttsp4_get_mt_touches_pr_tch:
925 if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE)
926 dev_dbg(dev,
927 "%s: t=%d x=%d y=%d z=%d M=%d m=%d o=%d e=%d\n",
928 __func__, t,
929 tch.abs[CY_TCH_X],
930 tch.abs[CY_TCH_Y],
931 tch.abs[CY_TCH_P],
932 tch.abs[CY_TCH_MAJ],
933 tch.abs[CY_TCH_MIN],
934 tch.abs[CY_TCH_OR],
935 tch.abs[CY_TCH_E]);
936 else
937 dev_dbg(dev,
938 "%s: t=%d x=%d y=%d z=%d e=%d\n", __func__,
940 tch.abs[CY_TCH_X],
941 tch.abs[CY_TCH_Y],
942 tch.abs[CY_TCH_P],
943 tch.abs[CY_TCH_E]);
946 cyttsp4_final_sync(md->input, si->si_ofs.tch_abs[CY_TCH_T].max, ids);
948 md->num_prv_tch = num_cur_tch;
950 return;
953 /* read xy_data for all current touches */
954 static int cyttsp4_xy_worker(struct cyttsp4 *cd)
956 struct cyttsp4_mt_data *md = &cd->md;
957 struct device *dev = &md->input->dev;
958 struct cyttsp4_sysinfo *si = md->si;
959 u8 num_cur_tch;
960 u8 hst_mode;
961 u8 rep_len;
962 u8 rep_stat;
963 u8 tt_stat;
964 int rc = 0;
967 * Get event data from cyttsp4 device.
968 * The event data includes all data
969 * for all active touches.
970 * Event data also includes button data
973 * Use 2 reads:
974 * 1st read to get mode + button bytes + touch count (core)
975 * 2nd read (optional) to get touch 1 - touch n data
977 hst_mode = si->xy_mode[CY_REG_BASE];
978 rep_len = si->xy_mode[si->si_ofs.rep_ofs];
979 rep_stat = si->xy_mode[si->si_ofs.rep_ofs + 1];
980 tt_stat = si->xy_mode[si->si_ofs.tt_stat_ofs];
981 dev_vdbg(dev, "%s: %s%02X %s%d %s%02X %s%02X\n", __func__,
982 "hst_mode=", hst_mode, "rep_len=", rep_len,
983 "rep_stat=", rep_stat, "tt_stat=", tt_stat);
985 num_cur_tch = GET_NUM_TOUCHES(tt_stat);
986 dev_vdbg(dev, "%s: num_cur_tch=%d\n", __func__, num_cur_tch);
988 if (rep_len == 0 && num_cur_tch > 0) {
989 dev_err(dev, "%s: report length error rep_len=%d num_tch=%d\n",
990 __func__, rep_len, num_cur_tch);
991 goto cyttsp4_xy_worker_exit;
994 /* read touches */
995 if (num_cur_tch > 0) {
996 rc = cyttsp4_adap_read(cd, si->si_ofs.tt_stat_ofs + 1,
997 num_cur_tch * si->si_ofs.tch_rec_size,
998 si->xy_data);
999 if (rc < 0) {
1000 dev_err(dev, "%s: read fail on touch regs r=%d\n",
1001 __func__, rc);
1002 goto cyttsp4_xy_worker_exit;
1006 /* print xy data */
1007 cyttsp4_pr_buf(dev, cd->pr_buf, si->xy_data, num_cur_tch *
1008 si->si_ofs.tch_rec_size, "xy_data");
1010 /* check any error conditions */
1011 if (IS_BAD_PKT(rep_stat)) {
1012 dev_dbg(dev, "%s: Invalid buffer detected\n", __func__);
1013 rc = 0;
1014 goto cyttsp4_xy_worker_exit;
1017 if (IS_LARGE_AREA(tt_stat))
1018 dev_dbg(dev, "%s: Large area detected\n", __func__);
1020 if (num_cur_tch > si->si_ofs.max_tchs) {
1021 dev_err(dev, "%s: too many tch; set to max tch (n=%d c=%zd)\n",
1022 __func__, num_cur_tch, si->si_ofs.max_tchs);
1023 num_cur_tch = si->si_ofs.max_tchs;
1026 /* extract xy_data for all currently reported touches */
1027 dev_vdbg(dev, "%s: extract data num_cur_tch=%d\n", __func__,
1028 num_cur_tch);
1029 if (num_cur_tch)
1030 cyttsp4_get_mt_touches(md, num_cur_tch);
1031 else
1032 cyttsp4_lift_all(md);
1034 rc = 0;
1036 cyttsp4_xy_worker_exit:
1037 return rc;
1040 static int cyttsp4_mt_attention(struct cyttsp4 *cd)
1042 struct device *dev = cd->dev;
1043 struct cyttsp4_mt_data *md = &cd->md;
1044 int rc = 0;
1046 if (!md->si)
1047 return 0;
1049 mutex_lock(&md->report_lock);
1050 if (!md->is_suspended) {
1051 /* core handles handshake */
1052 rc = cyttsp4_xy_worker(cd);
1053 } else {
1054 dev_vdbg(dev, "%s: Ignoring report while suspended\n",
1055 __func__);
1057 mutex_unlock(&md->report_lock);
1058 if (rc < 0)
1059 dev_err(dev, "%s: xy_worker error r=%d\n", __func__, rc);
1061 return rc;
1064 static irqreturn_t cyttsp4_irq(int irq, void *handle)
1066 struct cyttsp4 *cd = handle;
1067 struct device *dev = cd->dev;
1068 enum cyttsp4_mode cur_mode;
1069 u8 cmd_ofs = cd->sysinfo.si_ofs.cmd_ofs;
1070 u8 mode[3];
1071 int rc;
1074 * Check whether this IRQ should be ignored (external)
1075 * This should be the very first thing to check since
1076 * ignore_irq may be set for a very short period of time
1078 if (atomic_read(&cd->ignore_irq)) {
1079 dev_vdbg(dev, "%s: Ignoring IRQ\n", __func__);
1080 return IRQ_HANDLED;
1083 dev_dbg(dev, "%s int:0x%x\n", __func__, cd->int_status);
1085 mutex_lock(&cd->system_lock);
1087 /* Just to debug */
1088 if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEPING)
1089 dev_vdbg(dev, "%s: Received IRQ while in sleep\n", __func__);
1091 rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), mode);
1092 if (rc) {
1093 dev_err(cd->dev, "%s: Fail read adapter r=%d\n", __func__, rc);
1094 goto cyttsp4_irq_exit;
1096 dev_vdbg(dev, "%s mode[0-2]:0x%X 0x%X 0x%X\n", __func__,
1097 mode[0], mode[1], mode[2]);
1099 if (IS_BOOTLOADER(mode[0], mode[1])) {
1100 cur_mode = CY_MODE_BOOTLOADER;
1101 dev_vdbg(dev, "%s: bl running\n", __func__);
1102 if (cd->mode == CY_MODE_BOOTLOADER) {
1103 /* Signal bootloader heartbeat heard */
1104 wake_up(&cd->wait_q);
1105 goto cyttsp4_irq_exit;
1108 /* switch to bootloader */
1109 dev_dbg(dev, "%s: restart switch to bl m=%d -> m=%d\n",
1110 __func__, cd->mode, cur_mode);
1112 /* catch operation->bl glitch */
1113 if (cd->mode != CY_MODE_UNKNOWN) {
1114 /* Incase startup_state do not let startup_() */
1115 cd->mode = CY_MODE_UNKNOWN;
1116 cyttsp4_queue_startup_(cd);
1117 goto cyttsp4_irq_exit;
1121 * do not wake thread on this switch since
1122 * it is possible to get an early heartbeat
1123 * prior to performing the reset
1125 cd->mode = cur_mode;
1127 goto cyttsp4_irq_exit;
1130 switch (mode[0] & CY_HST_MODE) {
1131 case CY_HST_OPERATE:
1132 cur_mode = CY_MODE_OPERATIONAL;
1133 dev_vdbg(dev, "%s: operational\n", __func__);
1134 break;
1135 case CY_HST_CAT:
1136 cur_mode = CY_MODE_CAT;
1137 dev_vdbg(dev, "%s: CaT\n", __func__);
1138 break;
1139 case CY_HST_SYSINFO:
1140 cur_mode = CY_MODE_SYSINFO;
1141 dev_vdbg(dev, "%s: sysinfo\n", __func__);
1142 break;
1143 default:
1144 cur_mode = CY_MODE_UNKNOWN;
1145 dev_err(dev, "%s: unknown HST mode 0x%02X\n", __func__,
1146 mode[0]);
1147 break;
1150 /* Check whether this IRQ should be ignored (internal) */
1151 if (cd->int_status & CY_INT_IGNORE) {
1152 dev_vdbg(dev, "%s: Ignoring IRQ\n", __func__);
1153 goto cyttsp4_irq_exit;
1156 /* Check for wake up interrupt */
1157 if (cd->int_status & CY_INT_AWAKE) {
1158 cd->int_status &= ~CY_INT_AWAKE;
1159 wake_up(&cd->wait_q);
1160 dev_vdbg(dev, "%s: Received wake up interrupt\n", __func__);
1161 goto cyttsp4_irq_handshake;
1164 /* Expecting mode change interrupt */
1165 if ((cd->int_status & CY_INT_MODE_CHANGE)
1166 && (mode[0] & CY_HST_MODE_CHANGE) == 0) {
1167 cd->int_status &= ~CY_INT_MODE_CHANGE;
1168 dev_dbg(dev, "%s: finish mode switch m=%d -> m=%d\n",
1169 __func__, cd->mode, cur_mode);
1170 cd->mode = cur_mode;
1171 wake_up(&cd->wait_q);
1172 goto cyttsp4_irq_handshake;
1175 /* compare current core mode to current device mode */
1176 dev_vdbg(dev, "%s: cd->mode=%d cur_mode=%d\n",
1177 __func__, cd->mode, cur_mode);
1178 if ((mode[0] & CY_HST_MODE_CHANGE) == 0 && cd->mode != cur_mode) {
1179 /* Unexpected mode change occurred */
1180 dev_err(dev, "%s %d->%d 0x%x\n", __func__, cd->mode,
1181 cur_mode, cd->int_status);
1182 dev_dbg(dev, "%s: Unexpected mode change, startup\n",
1183 __func__);
1184 cyttsp4_queue_startup_(cd);
1185 goto cyttsp4_irq_exit;
1188 /* Expecting command complete interrupt */
1189 dev_vdbg(dev, "%s: command byte:0x%x\n", __func__, mode[cmd_ofs]);
1190 if ((cd->int_status & CY_INT_EXEC_CMD)
1191 && mode[cmd_ofs] & CY_CMD_COMPLETE) {
1192 cd->int_status &= ~CY_INT_EXEC_CMD;
1193 dev_vdbg(dev, "%s: Received command complete interrupt\n",
1194 __func__);
1195 wake_up(&cd->wait_q);
1197 * It is possible to receive a single interrupt for
1198 * command complete and touch/button status report.
1199 * Continue processing for a possible status report.
1203 /* This should be status report, read status regs */
1204 if (cd->mode == CY_MODE_OPERATIONAL) {
1205 dev_vdbg(dev, "%s: Read status registers\n", __func__);
1206 rc = cyttsp4_load_status_regs(cd);
1207 if (rc < 0)
1208 dev_err(dev, "%s: fail read mode regs r=%d\n",
1209 __func__, rc);
1212 cyttsp4_mt_attention(cd);
1214 cyttsp4_irq_handshake:
1215 /* handshake the event */
1216 dev_vdbg(dev, "%s: Handshake mode=0x%02X r=%d\n",
1217 __func__, mode[0], rc);
1218 rc = cyttsp4_handshake(cd, mode[0]);
1219 if (rc < 0)
1220 dev_err(dev, "%s: Fail handshake mode=0x%02X r=%d\n",
1221 __func__, mode[0], rc);
1224 * a non-zero udelay period is required for using
1225 * IRQF_TRIGGER_LOW in order to delay until the
1226 * device completes isr deassert
1228 udelay(cd->cpdata->level_irq_udelay);
1230 cyttsp4_irq_exit:
1231 mutex_unlock(&cd->system_lock);
1232 return IRQ_HANDLED;
1235 static void cyttsp4_start_wd_timer(struct cyttsp4 *cd)
1237 if (!CY_WATCHDOG_TIMEOUT)
1238 return;
1240 mod_timer(&cd->watchdog_timer, jiffies +
1241 msecs_to_jiffies(CY_WATCHDOG_TIMEOUT));
1244 static void cyttsp4_stop_wd_timer(struct cyttsp4 *cd)
1246 if (!CY_WATCHDOG_TIMEOUT)
1247 return;
1250 * Ensure we wait until the watchdog timer
1251 * running on a different CPU finishes
1253 del_timer_sync(&cd->watchdog_timer);
1254 cancel_work_sync(&cd->watchdog_work);
1255 del_timer_sync(&cd->watchdog_timer);
1258 static void cyttsp4_watchdog_timer(struct timer_list *t)
1260 struct cyttsp4 *cd = from_timer(cd, t, watchdog_timer);
1262 dev_vdbg(cd->dev, "%s: Watchdog timer triggered\n", __func__);
1264 schedule_work(&cd->watchdog_work);
1266 return;
1269 static int cyttsp4_request_exclusive(struct cyttsp4 *cd, void *ownptr,
1270 int timeout_ms)
1272 int t = msecs_to_jiffies(timeout_ms);
1273 bool with_timeout = (timeout_ms != 0);
1275 mutex_lock(&cd->system_lock);
1276 if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
1277 cd->exclusive_dev = ownptr;
1278 goto exit;
1281 cd->exclusive_waits++;
1282 wait:
1283 mutex_unlock(&cd->system_lock);
1284 if (with_timeout) {
1285 t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
1286 if (IS_TMO(t)) {
1287 dev_err(cd->dev, "%s: tmo waiting exclusive access\n",
1288 __func__);
1289 mutex_lock(&cd->system_lock);
1290 cd->exclusive_waits--;
1291 mutex_unlock(&cd->system_lock);
1292 return -ETIME;
1294 } else {
1295 wait_event(cd->wait_q, !cd->exclusive_dev);
1297 mutex_lock(&cd->system_lock);
1298 if (cd->exclusive_dev)
1299 goto wait;
1300 cd->exclusive_dev = ownptr;
1301 cd->exclusive_waits--;
1302 exit:
1303 mutex_unlock(&cd->system_lock);
1305 return 0;
1309 * returns error if was not owned
1311 static int cyttsp4_release_exclusive(struct cyttsp4 *cd, void *ownptr)
1313 mutex_lock(&cd->system_lock);
1314 if (cd->exclusive_dev != ownptr) {
1315 mutex_unlock(&cd->system_lock);
1316 return -EINVAL;
1319 dev_vdbg(cd->dev, "%s: exclusive_dev %p freed\n",
1320 __func__, cd->exclusive_dev);
1321 cd->exclusive_dev = NULL;
1322 wake_up(&cd->wait_q);
1323 mutex_unlock(&cd->system_lock);
1324 return 0;
1327 static int cyttsp4_wait_bl_heartbeat(struct cyttsp4 *cd)
1329 long t;
1330 int rc = 0;
1332 /* wait heartbeat */
1333 dev_vdbg(cd->dev, "%s: wait heartbeat...\n", __func__);
1334 t = wait_event_timeout(cd->wait_q, cd->mode == CY_MODE_BOOTLOADER,
1335 msecs_to_jiffies(CY_CORE_RESET_AND_WAIT_TIMEOUT));
1336 if (IS_TMO(t)) {
1337 dev_err(cd->dev, "%s: tmo waiting bl heartbeat cd->mode=%d\n",
1338 __func__, cd->mode);
1339 rc = -ETIME;
1342 return rc;
1345 static int cyttsp4_wait_sysinfo_mode(struct cyttsp4 *cd)
1347 long t;
1349 dev_vdbg(cd->dev, "%s: wait sysinfo...\n", __func__);
1351 t = wait_event_timeout(cd->wait_q, cd->mode == CY_MODE_SYSINFO,
1352 msecs_to_jiffies(CY_CORE_MODE_CHANGE_TIMEOUT));
1353 if (IS_TMO(t)) {
1354 dev_err(cd->dev, "%s: tmo waiting exit bl cd->mode=%d\n",
1355 __func__, cd->mode);
1356 mutex_lock(&cd->system_lock);
1357 cd->int_status &= ~CY_INT_MODE_CHANGE;
1358 mutex_unlock(&cd->system_lock);
1359 return -ETIME;
1362 return 0;
1365 static int cyttsp4_reset_and_wait(struct cyttsp4 *cd)
1367 int rc;
1369 /* reset hardware */
1370 mutex_lock(&cd->system_lock);
1371 dev_dbg(cd->dev, "%s: reset hw...\n", __func__);
1372 rc = cyttsp4_hw_reset(cd);
1373 cd->mode = CY_MODE_UNKNOWN;
1374 mutex_unlock(&cd->system_lock);
1375 if (rc < 0) {
1376 dev_err(cd->dev, "%s:Fail hw reset r=%d\n", __func__, rc);
1377 return rc;
1380 return cyttsp4_wait_bl_heartbeat(cd);
1384 * returns err if refused or timeout; block until mode change complete
1385 * bit is set (mode change interrupt)
1387 static int cyttsp4_set_mode(struct cyttsp4 *cd, int new_mode)
1389 u8 new_dev_mode;
1390 u8 mode;
1391 long t;
1392 int rc;
1394 switch (new_mode) {
1395 case CY_MODE_OPERATIONAL:
1396 new_dev_mode = CY_HST_OPERATE;
1397 break;
1398 case CY_MODE_SYSINFO:
1399 new_dev_mode = CY_HST_SYSINFO;
1400 break;
1401 case CY_MODE_CAT:
1402 new_dev_mode = CY_HST_CAT;
1403 break;
1404 default:
1405 dev_err(cd->dev, "%s: invalid mode: %02X(%d)\n",
1406 __func__, new_mode, new_mode);
1407 return -EINVAL;
1410 /* change mode */
1411 dev_dbg(cd->dev, "%s: %s=%p new_dev_mode=%02X new_mode=%d\n",
1412 __func__, "have exclusive", cd->exclusive_dev,
1413 new_dev_mode, new_mode);
1415 mutex_lock(&cd->system_lock);
1416 rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), &mode);
1417 if (rc < 0) {
1418 mutex_unlock(&cd->system_lock);
1419 dev_err(cd->dev, "%s: Fail read mode r=%d\n",
1420 __func__, rc);
1421 goto exit;
1424 /* Clear device mode bits and set to new mode */
1425 mode &= ~CY_HST_MODE;
1426 mode |= new_dev_mode | CY_HST_MODE_CHANGE;
1428 cd->int_status |= CY_INT_MODE_CHANGE;
1429 rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(mode), &mode);
1430 mutex_unlock(&cd->system_lock);
1431 if (rc < 0) {
1432 dev_err(cd->dev, "%s: Fail write mode change r=%d\n",
1433 __func__, rc);
1434 goto exit;
1437 /* wait for mode change done interrupt */
1438 t = wait_event_timeout(cd->wait_q,
1439 (cd->int_status & CY_INT_MODE_CHANGE) == 0,
1440 msecs_to_jiffies(CY_CORE_MODE_CHANGE_TIMEOUT));
1441 dev_dbg(cd->dev, "%s: back from wait t=%ld cd->mode=%d\n",
1442 __func__, t, cd->mode);
1444 if (IS_TMO(t)) {
1445 dev_err(cd->dev, "%s: %s\n", __func__,
1446 "tmo waiting mode change");
1447 mutex_lock(&cd->system_lock);
1448 cd->int_status &= ~CY_INT_MODE_CHANGE;
1449 mutex_unlock(&cd->system_lock);
1450 rc = -EINVAL;
1453 exit:
1454 return rc;
1457 static void cyttsp4_watchdog_work(struct work_struct *work)
1459 struct cyttsp4 *cd =
1460 container_of(work, struct cyttsp4, watchdog_work);
1461 u8 *mode;
1462 int retval;
1464 mutex_lock(&cd->system_lock);
1465 retval = cyttsp4_load_status_regs(cd);
1466 if (retval < 0) {
1467 dev_err(cd->dev,
1468 "%s: failed to access device in watchdog timer r=%d\n",
1469 __func__, retval);
1470 cyttsp4_queue_startup_(cd);
1471 goto cyttsp4_timer_watchdog_exit_error;
1473 mode = &cd->sysinfo.xy_mode[CY_REG_BASE];
1474 if (IS_BOOTLOADER(mode[0], mode[1])) {
1475 dev_err(cd->dev,
1476 "%s: device found in bootloader mode when operational mode\n",
1477 __func__);
1478 cyttsp4_queue_startup_(cd);
1479 goto cyttsp4_timer_watchdog_exit_error;
1482 cyttsp4_start_wd_timer(cd);
1483 cyttsp4_timer_watchdog_exit_error:
1484 mutex_unlock(&cd->system_lock);
1485 return;
1488 static int cyttsp4_core_sleep_(struct cyttsp4 *cd)
1490 enum cyttsp4_sleep_state ss = SS_SLEEP_ON;
1491 enum cyttsp4_int_state int_status = CY_INT_IGNORE;
1492 int rc = 0;
1493 u8 mode[2];
1495 /* Already in sleep mode? */
1496 mutex_lock(&cd->system_lock);
1497 if (cd->sleep_state == SS_SLEEP_ON) {
1498 mutex_unlock(&cd->system_lock);
1499 return 0;
1501 cd->sleep_state = SS_SLEEPING;
1502 mutex_unlock(&cd->system_lock);
1504 cyttsp4_stop_wd_timer(cd);
1506 /* Wait until currently running IRQ handler exits and disable IRQ */
1507 disable_irq(cd->irq);
1509 dev_vdbg(cd->dev, "%s: write DEEP SLEEP...\n", __func__);
1510 mutex_lock(&cd->system_lock);
1511 rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), &mode);
1512 if (rc) {
1513 mutex_unlock(&cd->system_lock);
1514 dev_err(cd->dev, "%s: Fail read adapter r=%d\n", __func__, rc);
1515 goto error;
1518 if (IS_BOOTLOADER(mode[0], mode[1])) {
1519 mutex_unlock(&cd->system_lock);
1520 dev_err(cd->dev, "%s: Device in BOOTLOADER mode.\n", __func__);
1521 rc = -EINVAL;
1522 goto error;
1525 mode[0] |= CY_HST_SLEEP;
1526 rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(mode[0]), &mode[0]);
1527 mutex_unlock(&cd->system_lock);
1528 if (rc) {
1529 dev_err(cd->dev, "%s: Fail write adapter r=%d\n", __func__, rc);
1530 goto error;
1532 dev_vdbg(cd->dev, "%s: write DEEP SLEEP succeeded\n", __func__);
1534 if (cd->cpdata->power) {
1535 dev_dbg(cd->dev, "%s: Power down HW\n", __func__);
1536 rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, &cd->ignore_irq);
1537 } else {
1538 dev_dbg(cd->dev, "%s: No power function\n", __func__);
1539 rc = 0;
1541 if (rc < 0) {
1542 dev_err(cd->dev, "%s: HW Power down fails r=%d\n",
1543 __func__, rc);
1544 goto error;
1547 /* Give time to FW to sleep */
1548 msleep(50);
1550 goto exit;
1552 error:
1553 ss = SS_SLEEP_OFF;
1554 int_status = CY_INT_NONE;
1555 cyttsp4_start_wd_timer(cd);
1557 exit:
1558 mutex_lock(&cd->system_lock);
1559 cd->sleep_state = ss;
1560 cd->int_status |= int_status;
1561 mutex_unlock(&cd->system_lock);
1562 enable_irq(cd->irq);
1563 return rc;
1566 static int cyttsp4_startup_(struct cyttsp4 *cd)
1568 int retry = CY_CORE_STARTUP_RETRY_COUNT;
1569 int rc;
1571 cyttsp4_stop_wd_timer(cd);
1573 reset:
1574 if (retry != CY_CORE_STARTUP_RETRY_COUNT)
1575 dev_dbg(cd->dev, "%s: Retry %d\n", __func__,
1576 CY_CORE_STARTUP_RETRY_COUNT - retry);
1578 /* reset hardware and wait for heartbeat */
1579 rc = cyttsp4_reset_and_wait(cd);
1580 if (rc < 0) {
1581 dev_err(cd->dev, "%s: Error on h/w reset r=%d\n", __func__, rc);
1582 if (retry--)
1583 goto reset;
1584 goto exit;
1587 /* exit bl into sysinfo mode */
1588 dev_vdbg(cd->dev, "%s: write exit ldr...\n", __func__);
1589 mutex_lock(&cd->system_lock);
1590 cd->int_status &= ~CY_INT_IGNORE;
1591 cd->int_status |= CY_INT_MODE_CHANGE;
1593 rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(ldr_exit),
1594 (u8 *)ldr_exit);
1595 mutex_unlock(&cd->system_lock);
1596 if (rc < 0) {
1597 dev_err(cd->dev, "%s: Fail write r=%d\n", __func__, rc);
1598 if (retry--)
1599 goto reset;
1600 goto exit;
1603 rc = cyttsp4_wait_sysinfo_mode(cd);
1604 if (rc < 0) {
1605 u8 buf[sizeof(ldr_err_app)];
1606 int rc1;
1608 /* Check for invalid/corrupted touch application */
1609 rc1 = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(ldr_err_app),
1610 buf);
1611 if (rc1) {
1612 dev_err(cd->dev, "%s: Fail read r=%d\n", __func__, rc1);
1613 } else if (!memcmp(buf, ldr_err_app, sizeof(ldr_err_app))) {
1614 dev_err(cd->dev, "%s: Error launching touch application\n",
1615 __func__);
1616 mutex_lock(&cd->system_lock);
1617 cd->invalid_touch_app = true;
1618 mutex_unlock(&cd->system_lock);
1619 goto exit_no_wd;
1622 if (retry--)
1623 goto reset;
1624 goto exit;
1627 mutex_lock(&cd->system_lock);
1628 cd->invalid_touch_app = false;
1629 mutex_unlock(&cd->system_lock);
1631 /* read sysinfo data */
1632 dev_vdbg(cd->dev, "%s: get sysinfo regs..\n", __func__);
1633 rc = cyttsp4_get_sysinfo_regs(cd);
1634 if (rc < 0) {
1635 dev_err(cd->dev, "%s: failed to get sysinfo regs rc=%d\n",
1636 __func__, rc);
1637 if (retry--)
1638 goto reset;
1639 goto exit;
1642 rc = cyttsp4_set_mode(cd, CY_MODE_OPERATIONAL);
1643 if (rc < 0) {
1644 dev_err(cd->dev, "%s: failed to set mode to operational rc=%d\n",
1645 __func__, rc);
1646 if (retry--)
1647 goto reset;
1648 goto exit;
1651 cyttsp4_lift_all(&cd->md);
1653 /* restore to sleep if was suspended */
1654 mutex_lock(&cd->system_lock);
1655 if (cd->sleep_state == SS_SLEEP_ON) {
1656 cd->sleep_state = SS_SLEEP_OFF;
1657 mutex_unlock(&cd->system_lock);
1658 cyttsp4_core_sleep_(cd);
1659 goto exit_no_wd;
1661 mutex_unlock(&cd->system_lock);
1663 exit:
1664 cyttsp4_start_wd_timer(cd);
1665 exit_no_wd:
1666 return rc;
1669 static int cyttsp4_startup(struct cyttsp4 *cd)
1671 int rc;
1673 mutex_lock(&cd->system_lock);
1674 cd->startup_state = STARTUP_RUNNING;
1675 mutex_unlock(&cd->system_lock);
1677 rc = cyttsp4_request_exclusive(cd, cd->dev,
1678 CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT);
1679 if (rc < 0) {
1680 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
1681 __func__, cd->exclusive_dev, cd->dev);
1682 goto exit;
1685 rc = cyttsp4_startup_(cd);
1687 if (cyttsp4_release_exclusive(cd, cd->dev) < 0)
1688 /* Don't return fail code, mode is already changed. */
1689 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
1690 else
1691 dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
1693 exit:
1694 mutex_lock(&cd->system_lock);
1695 cd->startup_state = STARTUP_NONE;
1696 mutex_unlock(&cd->system_lock);
1698 /* Wake the waiters for end of startup */
1699 wake_up(&cd->wait_q);
1701 return rc;
1704 static void cyttsp4_startup_work_function(struct work_struct *work)
1706 struct cyttsp4 *cd = container_of(work, struct cyttsp4, startup_work);
1707 int rc;
1709 rc = cyttsp4_startup(cd);
1710 if (rc < 0)
1711 dev_err(cd->dev, "%s: Fail queued startup r=%d\n",
1712 __func__, rc);
1715 static void cyttsp4_free_si_ptrs(struct cyttsp4 *cd)
1717 struct cyttsp4_sysinfo *si = &cd->sysinfo;
1719 if (!si)
1720 return;
1722 kfree(si->si_ptrs.cydata);
1723 kfree(si->si_ptrs.test);
1724 kfree(si->si_ptrs.pcfg);
1725 kfree(si->si_ptrs.opcfg);
1726 kfree(si->si_ptrs.ddata);
1727 kfree(si->si_ptrs.mdata);
1728 kfree(si->btn);
1729 kfree(si->xy_mode);
1730 kfree(si->xy_data);
1731 kfree(si->btn_rec_data);
1734 #ifdef CONFIG_PM
1735 static int cyttsp4_core_sleep(struct cyttsp4 *cd)
1737 int rc;
1739 rc = cyttsp4_request_exclusive(cd, cd->dev,
1740 CY_CORE_SLEEP_REQUEST_EXCLUSIVE_TIMEOUT);
1741 if (rc < 0) {
1742 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
1743 __func__, cd->exclusive_dev, cd->dev);
1744 return 0;
1747 rc = cyttsp4_core_sleep_(cd);
1749 if (cyttsp4_release_exclusive(cd, cd->dev) < 0)
1750 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
1751 else
1752 dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
1754 return rc;
1757 static int cyttsp4_core_wake_(struct cyttsp4 *cd)
1759 struct device *dev = cd->dev;
1760 int rc;
1761 u8 mode;
1762 int t;
1764 /* Already woken? */
1765 mutex_lock(&cd->system_lock);
1766 if (cd->sleep_state == SS_SLEEP_OFF) {
1767 mutex_unlock(&cd->system_lock);
1768 return 0;
1770 cd->int_status &= ~CY_INT_IGNORE;
1771 cd->int_status |= CY_INT_AWAKE;
1772 cd->sleep_state = SS_WAKING;
1774 if (cd->cpdata->power) {
1775 dev_dbg(dev, "%s: Power up HW\n", __func__);
1776 rc = cd->cpdata->power(cd->cpdata, 1, dev, &cd->ignore_irq);
1777 } else {
1778 dev_dbg(dev, "%s: No power function\n", __func__);
1779 rc = -ENOSYS;
1781 if (rc < 0) {
1782 dev_err(dev, "%s: HW Power up fails r=%d\n",
1783 __func__, rc);
1785 /* Initiate a read transaction to wake up */
1786 cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), &mode);
1787 } else
1788 dev_vdbg(cd->dev, "%s: HW power up succeeds\n",
1789 __func__);
1790 mutex_unlock(&cd->system_lock);
1792 t = wait_event_timeout(cd->wait_q,
1793 (cd->int_status & CY_INT_AWAKE) == 0,
1794 msecs_to_jiffies(CY_CORE_WAKEUP_TIMEOUT));
1795 if (IS_TMO(t)) {
1796 dev_err(dev, "%s: TMO waiting for wakeup\n", __func__);
1797 mutex_lock(&cd->system_lock);
1798 cd->int_status &= ~CY_INT_AWAKE;
1799 /* Try starting up */
1800 cyttsp4_queue_startup_(cd);
1801 mutex_unlock(&cd->system_lock);
1804 mutex_lock(&cd->system_lock);
1805 cd->sleep_state = SS_SLEEP_OFF;
1806 mutex_unlock(&cd->system_lock);
1808 cyttsp4_start_wd_timer(cd);
1810 return 0;
1813 static int cyttsp4_core_wake(struct cyttsp4 *cd)
1815 int rc;
1817 rc = cyttsp4_request_exclusive(cd, cd->dev,
1818 CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT);
1819 if (rc < 0) {
1820 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
1821 __func__, cd->exclusive_dev, cd->dev);
1822 return 0;
1825 rc = cyttsp4_core_wake_(cd);
1827 if (cyttsp4_release_exclusive(cd, cd->dev) < 0)
1828 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
1829 else
1830 dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
1832 return rc;
1835 static int cyttsp4_core_suspend(struct device *dev)
1837 struct cyttsp4 *cd = dev_get_drvdata(dev);
1838 struct cyttsp4_mt_data *md = &cd->md;
1839 int rc;
1841 md->is_suspended = true;
1843 rc = cyttsp4_core_sleep(cd);
1844 if (rc < 0) {
1845 dev_err(dev, "%s: Error on sleep\n", __func__);
1846 return -EAGAIN;
1848 return 0;
1851 static int cyttsp4_core_resume(struct device *dev)
1853 struct cyttsp4 *cd = dev_get_drvdata(dev);
1854 struct cyttsp4_mt_data *md = &cd->md;
1855 int rc;
1857 md->is_suspended = false;
1859 rc = cyttsp4_core_wake(cd);
1860 if (rc < 0) {
1861 dev_err(dev, "%s: Error on wake\n", __func__);
1862 return -EAGAIN;
1865 return 0;
1867 #endif
1869 const struct dev_pm_ops cyttsp4_pm_ops = {
1870 SET_SYSTEM_SLEEP_PM_OPS(cyttsp4_core_suspend, cyttsp4_core_resume)
1871 SET_RUNTIME_PM_OPS(cyttsp4_core_suspend, cyttsp4_core_resume, NULL)
1873 EXPORT_SYMBOL_GPL(cyttsp4_pm_ops);
1875 static int cyttsp4_mt_open(struct input_dev *input)
1877 pm_runtime_get(input->dev.parent);
1878 return 0;
1881 static void cyttsp4_mt_close(struct input_dev *input)
1883 struct cyttsp4_mt_data *md = input_get_drvdata(input);
1884 mutex_lock(&md->report_lock);
1885 if (!md->is_suspended)
1886 pm_runtime_put(input->dev.parent);
1887 mutex_unlock(&md->report_lock);
1891 static int cyttsp4_setup_input_device(struct cyttsp4 *cd)
1893 struct device *dev = cd->dev;
1894 struct cyttsp4_mt_data *md = &cd->md;
1895 int signal = CY_IGNORE_VALUE;
1896 int max_x, max_y, max_p, min, max;
1897 int max_x_tmp, max_y_tmp;
1898 int i;
1899 int rc;
1901 dev_vdbg(dev, "%s: Initialize event signals\n", __func__);
1902 __set_bit(EV_ABS, md->input->evbit);
1903 __set_bit(EV_REL, md->input->evbit);
1904 __set_bit(EV_KEY, md->input->evbit);
1906 max_x_tmp = md->si->si_ofs.max_x;
1907 max_y_tmp = md->si->si_ofs.max_y;
1909 /* get maximum values from the sysinfo data */
1910 if (md->pdata->flags & CY_FLAG_FLIP) {
1911 max_x = max_y_tmp - 1;
1912 max_y = max_x_tmp - 1;
1913 } else {
1914 max_x = max_x_tmp - 1;
1915 max_y = max_y_tmp - 1;
1917 max_p = md->si->si_ofs.max_p;
1919 /* set event signal capabilities */
1920 for (i = 0; i < (md->pdata->frmwrk->size / CY_NUM_ABS_SET); i++) {
1921 signal = md->pdata->frmwrk->abs
1922 [(i * CY_NUM_ABS_SET) + CY_SIGNAL_OST];
1923 if (signal != CY_IGNORE_VALUE) {
1924 __set_bit(signal, md->input->absbit);
1925 min = md->pdata->frmwrk->abs
1926 [(i * CY_NUM_ABS_SET) + CY_MIN_OST];
1927 max = md->pdata->frmwrk->abs
1928 [(i * CY_NUM_ABS_SET) + CY_MAX_OST];
1929 if (i == CY_ABS_ID_OST) {
1930 /* shift track ids down to start at 0 */
1931 max = max - min;
1932 min = min - min;
1933 } else if (i == CY_ABS_X_OST)
1934 max = max_x;
1935 else if (i == CY_ABS_Y_OST)
1936 max = max_y;
1937 else if (i == CY_ABS_P_OST)
1938 max = max_p;
1939 input_set_abs_params(md->input, signal, min, max,
1940 md->pdata->frmwrk->abs
1941 [(i * CY_NUM_ABS_SET) + CY_FUZZ_OST],
1942 md->pdata->frmwrk->abs
1943 [(i * CY_NUM_ABS_SET) + CY_FLAT_OST]);
1944 dev_dbg(dev, "%s: register signal=%02X min=%d max=%d\n",
1945 __func__, signal, min, max);
1946 if ((i == CY_ABS_ID_OST) &&
1947 (md->si->si_ofs.tch_rec_size <
1948 CY_TMA4XX_TCH_REC_SIZE))
1949 break;
1953 input_mt_init_slots(md->input, md->si->si_ofs.tch_abs[CY_TCH_T].max,
1954 INPUT_MT_DIRECT);
1955 rc = input_register_device(md->input);
1956 if (rc < 0)
1957 dev_err(dev, "%s: Error, failed register input device r=%d\n",
1958 __func__, rc);
1959 return rc;
1962 static int cyttsp4_mt_probe(struct cyttsp4 *cd)
1964 struct device *dev = cd->dev;
1965 struct cyttsp4_mt_data *md = &cd->md;
1966 struct cyttsp4_mt_platform_data *pdata = cd->pdata->mt_pdata;
1967 int rc = 0;
1969 mutex_init(&md->report_lock);
1970 md->pdata = pdata;
1971 /* Create the input device and register it. */
1972 dev_vdbg(dev, "%s: Create the input device and register it\n",
1973 __func__);
1974 md->input = input_allocate_device();
1975 if (md->input == NULL) {
1976 dev_err(dev, "%s: Error, failed to allocate input device\n",
1977 __func__);
1978 rc = -ENOSYS;
1979 goto error_alloc_failed;
1982 md->input->name = pdata->inp_dev_name;
1983 scnprintf(md->phys, sizeof(md->phys)-1, "%s", dev_name(dev));
1984 md->input->phys = md->phys;
1985 md->input->id.bustype = cd->bus_ops->bustype;
1986 md->input->dev.parent = dev;
1987 md->input->open = cyttsp4_mt_open;
1988 md->input->close = cyttsp4_mt_close;
1989 input_set_drvdata(md->input, md);
1991 /* get sysinfo */
1992 md->si = &cd->sysinfo;
1993 if (!md->si) {
1994 dev_err(dev, "%s: Fail get sysinfo pointer from core p=%p\n",
1995 __func__, md->si);
1996 goto error_get_sysinfo;
1999 rc = cyttsp4_setup_input_device(cd);
2000 if (rc)
2001 goto error_init_input;
2003 return 0;
2005 error_init_input:
2006 input_free_device(md->input);
2007 error_get_sysinfo:
2008 input_set_drvdata(md->input, NULL);
2009 error_alloc_failed:
2010 dev_err(dev, "%s failed.\n", __func__);
2011 return rc;
2014 struct cyttsp4 *cyttsp4_probe(const struct cyttsp4_bus_ops *ops,
2015 struct device *dev, u16 irq, size_t xfer_buf_size)
2017 struct cyttsp4 *cd;
2018 struct cyttsp4_platform_data *pdata = dev_get_platdata(dev);
2019 unsigned long irq_flags;
2020 int rc = 0;
2022 if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
2023 dev_err(dev, "%s: Missing platform data\n", __func__);
2024 rc = -ENODEV;
2025 goto error_no_pdata;
2028 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
2029 if (!cd) {
2030 dev_err(dev, "%s: Error, kzalloc\n", __func__);
2031 rc = -ENOMEM;
2032 goto error_alloc_data;
2035 cd->xfer_buf = kzalloc(xfer_buf_size, GFP_KERNEL);
2036 if (!cd->xfer_buf) {
2037 dev_err(dev, "%s: Error, kzalloc\n", __func__);
2038 rc = -ENOMEM;
2039 goto error_free_cd;
2042 /* Initialize device info */
2043 cd->dev = dev;
2044 cd->pdata = pdata;
2045 cd->cpdata = pdata->core_pdata;
2046 cd->bus_ops = ops;
2048 /* Initialize mutexes and spinlocks */
2049 mutex_init(&cd->system_lock);
2050 mutex_init(&cd->adap_lock);
2052 /* Initialize wait queue */
2053 init_waitqueue_head(&cd->wait_q);
2055 /* Initialize works */
2056 INIT_WORK(&cd->startup_work, cyttsp4_startup_work_function);
2057 INIT_WORK(&cd->watchdog_work, cyttsp4_watchdog_work);
2059 /* Initialize IRQ */
2060 cd->irq = gpio_to_irq(cd->cpdata->irq_gpio);
2061 if (cd->irq < 0) {
2062 rc = -EINVAL;
2063 goto error_free_xfer;
2066 dev_set_drvdata(dev, cd);
2068 /* Call platform init function */
2069 if (cd->cpdata->init) {
2070 dev_dbg(cd->dev, "%s: Init HW\n", __func__);
2071 rc = cd->cpdata->init(cd->cpdata, 1, cd->dev);
2072 } else {
2073 dev_dbg(cd->dev, "%s: No HW INIT function\n", __func__);
2074 rc = 0;
2076 if (rc < 0)
2077 dev_err(cd->dev, "%s: HW Init fail r=%d\n", __func__, rc);
2079 dev_dbg(dev, "%s: initialize threaded irq=%d\n", __func__, cd->irq);
2080 if (cd->cpdata->level_irq_udelay > 0)
2081 /* use level triggered interrupts */
2082 irq_flags = IRQF_TRIGGER_LOW | IRQF_ONESHOT;
2083 else
2084 /* use edge triggered interrupts */
2085 irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
2087 rc = request_threaded_irq(cd->irq, NULL, cyttsp4_irq, irq_flags,
2088 dev_name(dev), cd);
2089 if (rc < 0) {
2090 dev_err(dev, "%s: Error, could not request irq\n", __func__);
2091 goto error_request_irq;
2094 /* Setup watchdog timer */
2095 timer_setup(&cd->watchdog_timer, cyttsp4_watchdog_timer, 0);
2098 * call startup directly to ensure that the device
2099 * is tested before leaving the probe
2101 rc = cyttsp4_startup(cd);
2103 /* Do not fail probe if startup fails but the device is detected */
2104 if (rc < 0 && cd->mode == CY_MODE_UNKNOWN) {
2105 dev_err(cd->dev, "%s: Fail initial startup r=%d\n",
2106 __func__, rc);
2107 goto error_startup;
2110 rc = cyttsp4_mt_probe(cd);
2111 if (rc < 0) {
2112 dev_err(dev, "%s: Error, fail mt probe\n", __func__);
2113 goto error_startup;
2116 pm_runtime_enable(dev);
2118 return cd;
2120 error_startup:
2121 cancel_work_sync(&cd->startup_work);
2122 cyttsp4_stop_wd_timer(cd);
2123 pm_runtime_disable(dev);
2124 cyttsp4_free_si_ptrs(cd);
2125 free_irq(cd->irq, cd);
2126 error_request_irq:
2127 if (cd->cpdata->init)
2128 cd->cpdata->init(cd->cpdata, 0, dev);
2129 error_free_xfer:
2130 kfree(cd->xfer_buf);
2131 error_free_cd:
2132 kfree(cd);
2133 error_alloc_data:
2134 error_no_pdata:
2135 dev_err(dev, "%s failed.\n", __func__);
2136 return ERR_PTR(rc);
2138 EXPORT_SYMBOL_GPL(cyttsp4_probe);
2140 static void cyttsp4_mt_release(struct cyttsp4_mt_data *md)
2142 input_unregister_device(md->input);
2143 input_set_drvdata(md->input, NULL);
2146 int cyttsp4_remove(struct cyttsp4 *cd)
2148 struct device *dev = cd->dev;
2150 cyttsp4_mt_release(&cd->md);
2153 * Suspend the device before freeing the startup_work and stopping
2154 * the watchdog since sleep function restarts watchdog on failure
2156 pm_runtime_suspend(dev);
2157 pm_runtime_disable(dev);
2159 cancel_work_sync(&cd->startup_work);
2161 cyttsp4_stop_wd_timer(cd);
2163 free_irq(cd->irq, cd);
2164 if (cd->cpdata->init)
2165 cd->cpdata->init(cd->cpdata, 0, dev);
2166 cyttsp4_free_si_ptrs(cd);
2167 kfree(cd);
2168 return 0;
2170 EXPORT_SYMBOL_GPL(cyttsp4_remove);
2172 MODULE_LICENSE("GPL");
2173 MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard touchscreen core driver");
2174 MODULE_AUTHOR("Cypress");