import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / input / touchscreen / mediatek / cy8ctma / cyttsp4_core.c
1 /* BEGIN PN:DTS2013051703879 ,Added by l00184147, 2013/5/17*/
2 //add Touch driver for G610-T11
3 /* BEGIN PN:DTS2013012601133 ,Modified by l00184147, 2013/1/26*/
4 /* BEGIN PN:DTS2013011401860 ,Modified by l00184147, 2013/1/14*/
5 /* BEGIN PN:SPBB-1218 ,Added by l00184147, 2012/12/20*/
6 /*
7 * cyttsp4_core.c
8 * Cypress TrueTouch(TM) Standard Product V4 Core driver module.
9 * For use with Cypress Txx4xx parts.
10 * Supported parts include:
11 * TMA4XX
12 * TMA1036
13 *
14 * Copyright (C) 2012 Cypress Semiconductor
15 * Copyright (C) 2011 Sony Ericsson Mobile Communications AB.
16 *
17 * Author: Aleksej Makarov <aleksej.makarov@sonyericsson.com>
18 * Modified by: Cypress Semiconductor to add device functions
19 *
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License
22 * version 2, and only version 2, as published by the
23 * Free Software Foundation.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License along
31 * with this program; if not, write to the Free Software Foundation, Inc.,
32 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 *
34 * Contact Cypress Semiconductor at www.cypress.com <ttdrivers@cypress.com>
35 *
36 */
37 extern void cyttsp4_mtk_gpio_interrupt_register();
38 extern void cyttsp4_mtk_gpio_interrupt_enable();
39 extern void cyttsp4_mtk_gpio_interrupt_disable();
40
41
42
43 #include <asm/unaligned.h>
44 #include <linux/delay.h>
45 #include <linux/gpio.h>
46 #include <linux/input.h>
47 #include <linux/interrupt.h>
48 #include <linux/irq.h>
49 #include <linux/limits.h>
50 #include <linux/module.h>
51 #include <linux/pm_runtime.h>
52 #include <linux/sched.h>
53 #include <linux/slab.h>
54 #include <linux/workqueue.h>
55 #include <linux/kthread.h>
56 /* BEGIN PN:SPBB-1257 ,Added by l00184147, 2013/2/21*/
57 //add earlysuspend head file
58 #ifdef CONFIG_HAS_EARLYSUSPEND
59 #include <linux/earlysuspend.h>
60 #endif
61 /* END PN:SPBB-1257 ,Added by l00184147, 2013/2/21*/
62
63 #include "cyttsp4_bus.h"
64 #include "cyttsp4_core.h"
65 #include "cyttsp4_regs.h"
66 /* BEGIN PN:DTS2013040204005 ,Added by l00184147, 2013/4/2*/
67 //#include <linux/hardware_self_adapt.h>
68 #include <mach/mt_gpio.h>
69 #include <mach/mt_pm_ldo.h>
70 #include <cust_gpio_usage.h>
71
72 //#define GPIO_CTP_RST_PIN GPIO8
73 /* END PN:DTS2013040204005 ,Modified by l00184147, 2013/4/2*/
74
75 #define MTK
76 /* Timeout in ms. */
77 #define CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT 5000
78 /* BEGIN PN: SPBB-1254 ,Modified by f00184246, 2013/2/18*/
79 #define CY_CORE_MODE_CHANGE_TIMEOUT 5000
80 /* BEGIN PN: SPBB-1254 ,Modified by f00184246, 2013/2/18*/
81 #define CY_CORE_RESET_AND_WAIT_TIMEOUT 1000
82 #define CY_CORE_WAKEUP_TIMEOUT 1000
83
84 #define CY_CORE_BL_HOST_SYNC_BYTE 0xFF
85
86 #define CY_CORE_READTIMES 3
87
88 #define HI_BYTE(x) (u8)(((x) >> 8) & 0xFF)
89 #define LO_BYTE(x) (u8)((x) & 0xFF)
90 /* BEGIN PN:DTS2013021701945 ,Added by l00184147, 2013/2/17*/
91 #define CYTTSP4_WATCHDOG_NULL_CMD
92 /* END PN:DTS2013021701945 ,Added by l00184147, 2013/2/17*/
93 /* BEGIN PN:DTS2013033005872 ,Added by F00184246, 2013/3/30*/
94 #define HUAWEI_SET_FINGER_MODE_BY_DEFAULT
95 /* END PN:DTS2013033005872 ,Added by F00184246, 2013/3/30*/
96 static const u8 security_key[] = {
97 0xA5, 0x01, 0x02, 0x03, 0xFF, 0xFE, 0xFD, 0x5A
98 };
99
100 static const u8 ldr_exit[] = {
101 0xFF, 0x01, 0x3B, 0x00, 0x00, 0x4F, 0x6D, 0x17
102 };
103
104 static const u8 ldr_err_app[] = {
105 0x01, 0x02, 0x00, 0x00, 0x55, 0xDD, 0x17
106 };
107
108 /* BEGIN PN: SPBB-1253 ,Modified by l00184147, 2013/2/19*/
109 /* BEGIN PN:DTS2013053100307 ,Added by l00184147, 2013/06/17*/
110 MODULE_FIRMWARE(CY_FW_FILE_G750_NAME);
111 /* END PN:DTS2013053100307 ,Added by l00184147, 2013/06/17*/
112 /* END PN: SPBB-1253 ,Modified by l00184147, 2013/2/19*/
113
114 const char *cy_driver_core_name = CYTTSP4_CORE_NAME;
115 const char *cy_driver_core_version = CY_DRIVER_VERSION;
116 const char *cy_driver_core_date = CY_DRIVER_DATE;
117
118 enum cyttsp4_sleep_state {
119 SS_SLEEP_OFF,
120 SS_SLEEP_ON,
121 SS_SLEEPING,
122 SS_WAKING,
123 };
124
125 enum cyttsp4_startup_state {
126 STARTUP_NONE,
127 STARTUP_QUEUED,
128 STARTUP_RUNNING,
129 STARTUP_ILLEGAL,
130 };
131
132 enum cyttsp4_opmode {
133 OPMODE_NONE,
134 OPMODE_FINGER,
135 OPMODE_GLOVE,
136 };
137
138 struct cyttsp4_core_data {
139 struct device *dev;
140 struct cyttsp4_core *core;
141 struct list_head atten_list[CY_ATTEN_NUM_ATTEN];
142 struct mutex system_lock;
143 struct mutex adap_lock;
144 enum cyttsp4_mode mode;
145 enum cyttsp4_sleep_state sleep_state;
146 enum cyttsp4_startup_state startup_state;
147 enum cyttsp4_opmode opmode;
148 int int_status;
149 int cmd_toggle;
150 spinlock_t spinlock;
151 struct cyttsp4_core_platform_data *pdata;
152 wait_queue_head_t wait_q;
153 wait_queue_head_t sleep_q;
154 int irq;
155 struct workqueue_struct *startup_work_q;
156 struct work_struct startup_work;
157 struct workqueue_struct *mode_change_work_q;
158 struct work_struct mode_change_work;
159 struct cyttsp4_sysinfo sysinfo;
160 void *exclusive_dev;
161 int exclusive_waits;
162 atomic_t ignore_irq;
163 bool irq_enabled;
164 #ifdef VERBOSE_DEBUG
165 u8 pr_buf[CY_MAX_PRBUF_SIZE];
166 #endif
167 struct work_struct watchdog_work;
168 struct timer_list watchdog_timer;
169 /* BEGIN PN:SPBB-1257 ,Added by l00184147, 2013/2/21*/
170 //add the early_suspend structure
171 #ifdef CONFIG_HAS_EARLYSUSPEND
172 struct early_suspend es;
173 #endif
174 /* END PN:SPBB-1257 ,Added by l00184147, 2013/2/21*/
175 };
176
177 struct atten_node {
178 struct list_head node;
179 int (*func)(struct cyttsp4_device *);
180 struct cyttsp4_device *ttsp;
181 int mode;
182 };
183
184 static inline size_t merge_bytes(u8 high, u8 low)
185 {
186 return (high << 8) + low;
187 }
188
189 #ifdef VERBOSE_DEBUG
190 void cyttsp4_pr_buf(struct device *dev, u8 *pr_buf, u8 *dptr, int size,
191 const char *data_name)
192 {
193 int i, k;
194 const char fmt[] = "%02X ";
195 int max;
196
197 if (!size)
198 return;
199
200 max = (CY_MAX_PRBUF_SIZE - 1) - sizeof(CY_PR_TRUNCATED);
201
202 pr_buf[0] = 0;
203 for (i = k = 0; i < size && k < max; i++, k += 3)
204 scnprintf(pr_buf + k, CY_MAX_PRBUF_SIZE, fmt, dptr[i]);
205
206 dev_vdbg(dev, "%s: %s[0..%d]=%s%s\n", __func__, data_name, size - 1,
207 pr_buf, size <= max ? "" : CY_PR_TRUNCATED);
208 }
209 EXPORT_SYMBOL(cyttsp4_pr_buf);
210 #endif
211
212 static u16 cyttsp4_calc_partial_app_crc(const u8 *data, int size, u16 crc)
213 {
214 int i, j;
215
216 for (i = 0; i < size; i++) {
217 crc ^= ((u16)data[i] << 8);
218 for (j = 8; j > 0; j--)
219 if (crc & 0x8000)
220 crc = (crc << 1) ^ 0x1021;
221 else
222 crc <<= 1;
223 }
224
225 return crc;
226 }
227
228 static inline u16 cyttsp4_calc_app_crc(const u8 *data, int size)
229 {
230 return cyttsp4_calc_partial_app_crc(data, size, 0xFFFF);
231 }
232
233 static int cyttsp4_load_status_regs(struct cyttsp4_core_data *cd)
234 {
235 struct cyttsp4_sysinfo *si = &cd->sysinfo;
236 struct device *dev = cd->dev;
237 int rc;
238
239 if (!si->xy_mode) {
240 dev_err(cd->dev, "%s: NULL xy_mode pointer\n", __func__);
241 return -EINVAL;
242 }
243
244 rc = cyttsp4_adap_read(cd->core->adap, CY_REG_BASE,
245 si->xy_mode, si->si_ofs.mode_size);
246 if (rc < 0)
247 dev_err(dev, "%s: fail read mode regs r=%d\n",
248 __func__, rc);
249 else
250 cyttsp4_pr_buf(dev, cd->pr_buf, si->xy_mode,
251 si->si_ofs.mode_size, "xy_mode");
252
253 return rc;
254 }
255
256 static int cyttsp4_handshake(struct cyttsp4_core_data *cd, u8 mode)
257 {
258 u8 cmd = mode ^ CY_HST_TOGGLE;
259 int rc;
260
261 if (mode & CY_HST_MODE_CHANGE) {
262 dev_err(cd->dev, "%s: Host mode change bit set, NO handshake\n",
263 __func__);
264 return 0;
265 }
266
267 rc = cyttsp4_adap_write(cd->core->adap, CY_REG_BASE, &cmd,
268 sizeof(cmd));
269
270 if (rc < 0)
271 dev_err(cd->dev, "%s: bus write fail on handshake (ret=%d)\n",
272 __func__, rc);
273
274 return rc;
275 }
276
277 static int cyttsp4_toggle_low_power(struct cyttsp4_core_data *cd, u8 mode)
278 {
279 u8 cmd = mode ^ CY_HST_LOWPOW;
280 int rc = cyttsp4_adap_write(cd->core->adap, CY_REG_BASE, &cmd,
281 sizeof(cmd));
282 if (rc < 0)
283 dev_err(cd->dev,
284 "%s: bus write fail on toggle low power (ret=%d)\n",
285 __func__, rc);
286 return rc;
287 }
288
289 static int cyttsp4_set_low_power(struct cyttsp4_core_data *cd, bool enable)
290 {
291 u8 mode;
292 int rc = cyttsp4_adap_read(cd->core->adap, CY_REG_BASE, &mode,
293 sizeof(mode));
294 if (rc < 0) {
295 dev_err(cd->dev,
296 "%s: bus read fail on set low power (ret=%d)\n",
297 __func__, rc);
298 goto exit;
299 }
300
301 if (enable)
302 mode |= CY_HST_LOWPOW;
303 else
304 mode &= ~CY_HST_LOWPOW;
305
306 rc = cyttsp4_adap_write(cd->core->adap, CY_REG_BASE, &mode,
307 sizeof(mode));
308 if (rc < 0) {
309 dev_err(cd->dev,
310 "%s: bus write fail on set low power (ret=%d)\n",
311 __func__, rc);
312 }
313
314 exit:
315 return rc;
316 }
317
318 static int cyttsp4_get_low_power(struct cyttsp4_core_data *cd, bool *enable)
319 {
320 u8 mode;
321 int rc = cyttsp4_adap_read(cd->core->adap, CY_REG_BASE, &mode,
322 sizeof(mode));
323 if (rc < 0) {
324 dev_err(cd->dev,
325 "%s: bus read fail on set low power (ret=%d)\n",
326 __func__, rc);
327 goto exit;
328 }
329
330 if (mode & CY_HST_LOWPOW)
331 *enable = true;
332 else
333 *enable = false;
334
335 exit:
336 return rc;
337 }
338
339 static int cyttsp4_hw_soft_reset(struct cyttsp4_core_data *cd)
340 {
341 u8 cmd = CY_HST_RESET | CY_HST_MODE_CHANGE;
342 int rc = cyttsp4_adap_write(cd->core->adap, CY_REG_BASE, &cmd,
343 sizeof(cmd));
344 if (rc < 0) {
345 dev_err(cd->dev, "%s: FAILED to execute SOFT reset\n",
346 __func__);
347 return rc;
348 }
349 dev_dbg(cd->dev, "%s: execute SOFT reset\n", __func__);
350 return 0;
351 }
352
353 static int cyttsp4_hw_hard_reset(struct cyttsp4_core_data *cd)
354 {
355 if (cd->pdata->xres) {
356 cd->pdata->xres(cd->pdata, cd->dev);
357 dev_dbg(cd->dev, "%s: execute HARD reset\n", __func__);
358 return 0;
359 }
360 dev_err(cd->dev, "%s: FAILED to execute HARD reset\n", __func__);
361 return -ENOSYS;
362 }
363
364 static int cyttsp4_hw_reset(struct cyttsp4_core_data *cd)
365 {
366 int rc = cyttsp4_hw_hard_reset(cd);
367 if (rc == -ENOSYS)
368 rc = cyttsp4_hw_soft_reset(cd);
369 return rc;
370 }
371
372 static inline int cyttsp4_bits_2_bytes(int nbits, int *max)
373 {
374 *max = 1 << nbits;
375 return (nbits + 7) / 8;
376 }
377
378 static int cyttsp4_si_data_offsets(struct cyttsp4_core_data *cd)
379 {
380 struct cyttsp4_sysinfo *si = &cd->sysinfo;
381 int rc = cyttsp4_adap_read(cd->core->adap, CY_REG_BASE, &si->si_data,
382 sizeof(si->si_data));
383 if (rc < 0) {
384 dev_err(cd->dev, "%s: fail read sysinfo data offsets r=%d\n",
385 __func__, rc);
386 return rc;
387 }
388
389 /* Print sysinfo data offsets */
390 cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)&si->si_data,
391 sizeof(si->si_data), "sysinfo_data_offsets");
392
393 /* convert sysinfo data offset bytes into integers */
394
395 si->si_ofs.map_sz = merge_bytes(si->si_data.map_szh,
396 si->si_data.map_szl);
397 si->si_ofs.map_sz = merge_bytes(si->si_data.map_szh,
398 si->si_data.map_szl);
399 si->si_ofs.cydata_ofs = merge_bytes(si->si_data.cydata_ofsh,
400 si->si_data.cydata_ofsl);
401 si->si_ofs.test_ofs = merge_bytes(si->si_data.test_ofsh,
402 si->si_data.test_ofsl);
403 si->si_ofs.pcfg_ofs = merge_bytes(si->si_data.pcfg_ofsh,
404 si->si_data.pcfg_ofsl);
405 si->si_ofs.opcfg_ofs = merge_bytes(si->si_data.opcfg_ofsh,
406 si->si_data.opcfg_ofsl);
407 si->si_ofs.ddata_ofs = merge_bytes(si->si_data.ddata_ofsh,
408 si->si_data.ddata_ofsl);
409 si->si_ofs.mdata_ofs = merge_bytes(si->si_data.mdata_ofsh,
410 si->si_data.mdata_ofsl);
411 return rc;
412 }
413
414 static int cyttsp4_si_get_cydata(struct cyttsp4_core_data *cd)
415 {
416 struct cyttsp4_sysinfo *si = &cd->sysinfo;
417 int rc;
418 int size;
419 u8 *buf;
420 u8 *p;
421 struct cyttsp4_cydata *cydata;
422
423 /* Allocate a temp buffer for reading CYDATA registers */
424 si->si_ofs.cydata_size = si->si_ofs.test_ofs - si->si_ofs.cydata_ofs;
425 dev_dbg(cd->dev, "%s: cydata size: %d\n", __func__,
426 si->si_ofs.cydata_size);
427 buf = kzalloc(si->si_ofs.cydata_size, GFP_KERNEL);
428 if (buf == NULL) {
429 dev_err(cd->dev, "%s: fail alloc buffer for reading cydata\n",
430 __func__);
431 return -ENOMEM;
432 }
433
434 /* Read the CYDA registers to the temp buf */
435 rc = cyttsp4_adap_read(cd->core->adap, si->si_ofs.cydata_ofs,
436 buf, si->si_ofs.cydata_size);
437 if (rc < 0) {
438 dev_err(cd->dev, "%s: fail read cydata r=%d\n",
439 __func__, rc);
440 goto free_buf;
441 }
442
443 /* Allocate local cydata structure */
444 if (si->si_ptrs.cydata == NULL)
445 si->si_ptrs.cydata = kzalloc(sizeof(struct cyttsp4_cydata),
446 GFP_KERNEL);
447 if (si->si_ptrs.cydata == NULL) {
448 dev_err(cd->dev, "%s: fail alloc cydata memory\n", __func__);
449 rc = -ENOMEM;
450 goto free_buf;
451 }
452
453 cydata = (struct cyttsp4_cydata *)buf;
454
455 /* Allocate MFGID memory */
456 if (si->si_ptrs.cydata->mfg_id == NULL)
457 si->si_ptrs.cydata->mfg_id = kzalloc(cydata->mfgid_sz,
458 GFP_KERNEL);
459 if (si->si_ptrs.cydata->mfg_id == NULL) {
460 kfree(si->si_ptrs.cydata);
461 si->si_ptrs.cydata = NULL;
462 dev_err(cd->dev, "%s: fail alloc mfgid memory\n", __func__);
463 rc = -ENOMEM;
464 goto free_buf;
465 }
466
467 /* Copy all fields up to MFGID to local cydata structure */
468 p = buf;
469 size = offsetof(struct cyttsp4_cydata, mfgid_sz) + 1;
470 memcpy(si->si_ptrs.cydata, p, size);
471
472 cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)si->si_ptrs.cydata,
473 size, "sysinfo_cydata");
474
475 /* Copy MFGID */
476 p += size;
477 memcpy(si->si_ptrs.cydata->mfg_id, p, si->si_ptrs.cydata->mfgid_sz);
478
479 cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)si->si_ptrs.cydata->mfg_id,
480 si->si_ptrs.cydata->mfgid_sz, "sysinfo_cydata mfgid");
481
482 /* Copy remaining registers after MFGID */
483 p += si->si_ptrs.cydata->mfgid_sz;
484 size = sizeof(struct cyttsp4_cydata) -
485 offsetof(struct cyttsp4_cydata, cyito_idh);
486 memcpy((u8 *)&si->si_ptrs.cydata->cyito_idh, p, size);
487
488 cyttsp4_pr_buf(cd->dev, cd->pr_buf, &si->si_ptrs.cydata->cyito_idh,
489 size, "sysinfo_cydata");
490
491 free_buf:
492 kfree(buf);
493 return rc;
494 }
495
496 static int cyttsp4_si_get_test_data(struct cyttsp4_core_data *cd)
497 {
498 struct cyttsp4_sysinfo *si = &cd->sysinfo;
499 int rc;
500
501 si->si_ofs.test_size = si->si_ofs.pcfg_ofs - si->si_ofs.test_ofs;
502 if (si->si_ptrs.test == NULL)
503 si->si_ptrs.test = kzalloc(si->si_ofs.test_size, GFP_KERNEL);
504 if (si->si_ptrs.test == NULL) {
505 dev_err(cd->dev, "%s: fail alloc test memory\n", __func__);
506 return -ENOMEM;
507 }
508
509 rc = cyttsp4_adap_read(cd->core->adap, si->si_ofs.test_ofs,
510 si->si_ptrs.test, si->si_ofs.test_size);
511 if (rc < 0) {
512 dev_err(cd->dev, "%s: fail read test data r=%d\n",
513 __func__, rc);
514 return rc;
515 }
516
517 cyttsp4_pr_buf(cd->dev, cd->pr_buf,
518 (u8 *)si->si_ptrs.test, si->si_ofs.test_size,
519 "sysinfo_test_data");
520 if (si->si_ptrs.test->post_codel &
521 CY_POST_CODEL_WDG_RST)
522 dev_info(cd->dev, "%s: %s codel=%02X\n",
523 __func__, "Reset was a WATCHDOG RESET",
524 si->si_ptrs.test->post_codel);
525
526 if (!(si->si_ptrs.test->post_codel &
527 CY_POST_CODEL_CFG_DATA_CRC_FAIL))
528 dev_info(cd->dev, "%s: %s codel=%02X\n", __func__,
529 "Config Data CRC FAIL",
530 si->si_ptrs.test->post_codel);
531
532 if (!(si->si_ptrs.test->post_codel &
533 CY_POST_CODEL_PANEL_TEST_FAIL))
534 dev_info(cd->dev, "%s: %s codel=%02X\n",
535 __func__, "PANEL TEST FAIL",
536 si->si_ptrs.test->post_codel);
537
538 dev_info(cd->dev, "%s: SCANNING is %s codel=%02X\n",
539 __func__, si->si_ptrs.test->post_codel & 0x08 ?
540 "ENABLED" : "DISABLED",
541 si->si_ptrs.test->post_codel);
542 return rc;
543 }
544
545 static int cyttsp4_si_get_pcfg_data(struct cyttsp4_core_data *cd)
546 {
547 struct cyttsp4_sysinfo *si = &cd->sysinfo;
548 int rc;
549
550 dev_vdbg(cd->dev, "%s: get pcfg data\n", __func__);
551 si->si_ofs.pcfg_size = si->si_ofs.opcfg_ofs - si->si_ofs.pcfg_ofs;
552 if (si->si_ptrs.pcfg == NULL)
553 si->si_ptrs.pcfg = kzalloc(si->si_ofs.pcfg_size, GFP_KERNEL);
554 if (si->si_ptrs.pcfg == NULL) {
555 rc = -ENOMEM;
556 dev_err(cd->dev, "%s: fail alloc pcfg memory r=%d\n",
557 __func__, rc);
558 return rc;
559 }
560 rc = cyttsp4_adap_read(cd->core->adap, si->si_ofs.pcfg_ofs,
561 si->si_ptrs.pcfg, si->si_ofs.pcfg_size);
562 if (rc < 0) {
563 dev_err(cd->dev, "%s: fail read pcfg data r=%d\n",
564 __func__, rc);
565 return rc;
566 }
567
568 si->si_ofs.max_x = merge_bytes((si->si_ptrs.pcfg->res_xh
569 & CY_PCFG_RESOLUTION_X_MASK), si->si_ptrs.pcfg->res_xl);
570 si->si_ofs.x_origin = !!(si->si_ptrs.pcfg->res_xh
571 & CY_PCFG_ORIGIN_X_MASK);
572 si->si_ofs.max_y = merge_bytes((si->si_ptrs.pcfg->res_yh
573 & CY_PCFG_RESOLUTION_Y_MASK), si->si_ptrs.pcfg->res_yl);
574 si->si_ofs.y_origin = !!(si->si_ptrs.pcfg->res_yh
575 & CY_PCFG_ORIGIN_Y_MASK);
576 si->si_ofs.max_p = merge_bytes(si->si_ptrs.pcfg->max_zh,
577 si->si_ptrs.pcfg->max_zl);
578
579 cyttsp4_pr_buf(cd->dev, cd->pr_buf,
580 (u8 *)si->si_ptrs.pcfg,
581 si->si_ofs.pcfg_size, "sysinfo_pcfg_data");
582 return rc;
583 }
584
585 static int cyttsp4_si_get_opcfg_data(struct cyttsp4_core_data *cd)
586 {
587 struct cyttsp4_sysinfo *si = &cd->sysinfo;
588 int i;
589 enum cyttsp4_tch_abs abs;
590 int rc;
591
592 dev_vdbg(cd->dev, "%s: get opcfg data\n", __func__);
593 si->si_ofs.opcfg_size = si->si_ofs.ddata_ofs - si->si_ofs.opcfg_ofs;
594 if (si->si_ptrs.opcfg == NULL)
595 si->si_ptrs.opcfg = kzalloc(si->si_ofs.opcfg_size, GFP_KERNEL);
596 if (si->si_ptrs.opcfg == NULL) {
597 dev_err(cd->dev, "%s: fail alloc opcfg memory\n", __func__);
598 rc = -ENOMEM;
599 goto cyttsp4_si_get_opcfg_data_exit;
600 }
601
602 rc = cyttsp4_adap_read(cd->core->adap, si->si_ofs.opcfg_ofs,
603 si->si_ptrs.opcfg, si->si_ofs.opcfg_size);
604 if (rc < 0) {
605 dev_err(cd->dev, "%s: fail read opcfg data r=%d\n",
606 __func__, rc);
607 goto cyttsp4_si_get_opcfg_data_exit;
608 }
609 si->si_ofs.cmd_ofs = si->si_ptrs.opcfg->cmd_ofs;
610 si->si_ofs.rep_ofs = si->si_ptrs.opcfg->rep_ofs;
611 si->si_ofs.rep_sz = (si->si_ptrs.opcfg->rep_szh * 256) +
612 si->si_ptrs.opcfg->rep_szl;
613 si->si_ofs.num_btns = si->si_ptrs.opcfg->num_btns;
614 si->si_ofs.num_btn_regs = (si->si_ofs.num_btns +
615 CY_NUM_BTN_PER_REG - 1) / CY_NUM_BTN_PER_REG;
616 si->si_ofs.tt_stat_ofs = si->si_ptrs.opcfg->tt_stat_ofs;
617 si->si_ofs.obj_cfg0 = si->si_ptrs.opcfg->obj_cfg0;
618 si->si_ofs.max_tchs = si->si_ptrs.opcfg->max_tchs &
619 CY_BYTE_OFS_MASK;
620 si->si_ofs.tch_rec_size = si->si_ptrs.opcfg->tch_rec_size &
621 CY_BYTE_OFS_MASK;
622
623 /* Get the old touch fields */
624 for (abs = CY_TCH_X; abs < CY_NUM_TCH_FIELDS; abs++) {
625 si->si_ofs.tch_abs[abs].ofs =
626 si->si_ptrs.opcfg->tch_rec_old[abs].loc &
627 CY_BYTE_OFS_MASK;
628 si->si_ofs.tch_abs[abs].size =
629 cyttsp4_bits_2_bytes
630 (si->si_ptrs.opcfg->tch_rec_old[abs].size,
631 &si->si_ofs.tch_abs[abs].max);
632 si->si_ofs.tch_abs[abs].bofs =
633 (si->si_ptrs.opcfg->tch_rec_old[abs].loc &
634 CY_BOFS_MASK) >> CY_BOFS_SHIFT;
635 }
636
637 /* button fields */
638 si->si_ofs.btn_rec_size = si->si_ptrs.opcfg->btn_rec_size;
639 si->si_ofs.btn_diff_ofs = si->si_ptrs.opcfg->btn_diff_ofs;
640 si->si_ofs.btn_diff_size = si->si_ptrs.opcfg->btn_diff_size;
641
642 if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE) {
643 /* Get the extended touch fields */
644 for (i = 0; i < CY_NUM_EXT_TCH_FIELDS; abs++, i++) {
645 si->si_ofs.tch_abs[abs].ofs =
646 si->si_ptrs.opcfg->tch_rec_new[i].loc &
647 CY_BYTE_OFS_MASK;
648 si->si_ofs.tch_abs[abs].size =
649 cyttsp4_bits_2_bytes
650 (si->si_ptrs.opcfg->tch_rec_new[i].size,
651 &si->si_ofs.tch_abs[abs].max);
652 si->si_ofs.tch_abs[abs].bofs =
653 (si->si_ptrs.opcfg->tch_rec_new[i].loc
654 & CY_BOFS_MASK) >> CY_BOFS_SHIFT;
655 }
656 }
657
658 for (abs = 0; abs < CY_TCH_NUM_ABS; abs++) {
659 dev_dbg(cd->dev, "%s: tch_rec_%s\n", __func__,
660 cyttsp4_tch_abs_string[abs]);
661 dev_dbg(cd->dev, "%s: ofs =%2d\n", __func__,
662 si->si_ofs.tch_abs[abs].ofs);
663 dev_dbg(cd->dev, "%s: siz =%2d\n", __func__,
664 si->si_ofs.tch_abs[abs].size);
665 dev_dbg(cd->dev, "%s: max =%2d\n", __func__,
666 si->si_ofs.tch_abs[abs].max);
667 dev_dbg(cd->dev, "%s: bofs=%2d\n", __func__,
668 si->si_ofs.tch_abs[abs].bofs);
669 }
670
671 si->si_ofs.mode_size = si->si_ofs.tt_stat_ofs + 1;
672 si->si_ofs.data_size = si->si_ofs.max_tchs *
673 si->si_ptrs.opcfg->tch_rec_size;
674
675 cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)si->si_ptrs.opcfg,
676 si->si_ofs.opcfg_size, "sysinfo_opcfg_data");
677
678 cyttsp4_si_get_opcfg_data_exit:
679 return rc;
680 }
681
682 static int cyttsp4_si_get_ddata(struct cyttsp4_core_data *cd)
683 {
684 struct cyttsp4_sysinfo *si = &cd->sysinfo;
685 int rc;
686
687 dev_vdbg(cd->dev, "%s: get ddata data\n", __func__);
688 si->si_ofs.ddata_size = si->si_ofs.mdata_ofs - si->si_ofs.ddata_ofs;
689 if (si->si_ptrs.ddata == NULL)
690 si->si_ptrs.ddata = kzalloc(si->si_ofs.ddata_size, GFP_KERNEL);
691 if (si->si_ptrs.ddata == NULL) {
692 dev_err(cd->dev, "%s: fail alloc ddata memory\n", __func__);
693 return -ENOMEM;
694 }
695
696 rc = cyttsp4_adap_read(cd->core->adap, si->si_ofs.ddata_ofs,
697 si->si_ptrs.ddata, si->si_ofs.ddata_size);
698 if (rc < 0)
699 dev_err(cd->dev, "%s: fail read ddata data r=%d\n",
700 __func__, rc);
701 else
702 cyttsp4_pr_buf(cd->dev, cd->pr_buf,
703 (u8 *)si->si_ptrs.ddata,
704 si->si_ofs.ddata_size, "sysinfo_ddata");
705 return rc;
706 }
707
708 static int cyttsp4_si_get_mdata(struct cyttsp4_core_data *cd)
709 {
710 struct cyttsp4_sysinfo *si = &cd->sysinfo;
711 int rc;
712
713 dev_vdbg(cd->dev, "%s: get mdata data\n", __func__);
714 si->si_ofs.mdata_size = si->si_ofs.map_sz - si->si_ofs.mdata_ofs;
715 if (si->si_ptrs.mdata == NULL)
716 si->si_ptrs.mdata = kzalloc(si->si_ofs.mdata_size, GFP_KERNEL);
717 if (si->si_ptrs.mdata == NULL) {
718 dev_err(cd->dev, "%s: fail alloc mdata memory\n", __func__);
719 return -ENOMEM;
720 }
721
722 rc = cyttsp4_adap_read(cd->core->adap, si->si_ofs.mdata_ofs,
723 si->si_ptrs.mdata, si->si_ofs.mdata_size);
724 if (rc < 0)
725 dev_err(cd->dev, "%s: fail read mdata data r=%d\n",
726 __func__, rc);
727 else
728 cyttsp4_pr_buf(cd->dev, cd->pr_buf,
729 (u8 *)si->si_ptrs.mdata,
730 si->si_ofs.mdata_size, "sysinfo_mdata");
731 return rc;
732 }
733
734 static int cyttsp4_si_get_btn_data(struct cyttsp4_core_data *cd)
735 {
736 struct cyttsp4_sysinfo *si = &cd->sysinfo;
737 int btn;
738 int num_defined_keys;
739 u16 *key_table;
740 int rc = 0;
741
742 dev_vdbg(cd->dev, "%s: get btn data\n", __func__);
743 if (si->si_ofs.num_btns) {
744 si->si_ofs.btn_keys_size = si->si_ofs.num_btns *
745 sizeof(struct cyttsp4_btn);
746 if (si->btn == NULL)
747 si->btn = kzalloc(si->si_ofs.btn_keys_size, GFP_KERNEL);
748 if (si->btn == NULL) {
749 dev_err(cd->dev, "%s: %s\n", __func__,
750 "fail alloc btn_keys memory");
751 return -ENOMEM;
752 }
753 if (cd->pdata->sett[CY_IC_GRPNUM_BTN_KEYS] == NULL)
754 num_defined_keys = 0;
755 else if (cd->pdata->sett[CY_IC_GRPNUM_BTN_KEYS]->data == NULL)
756 num_defined_keys = 0;
757 else
758 num_defined_keys = cd->pdata->sett
759 [CY_IC_GRPNUM_BTN_KEYS]->size;
760
761 for (btn = 0; btn < si->si_ofs.num_btns &&
762 btn < num_defined_keys; btn++) {
763 key_table = (u16 *)cd->pdata->sett
764 [CY_IC_GRPNUM_BTN_KEYS]->data;
765 si->btn[btn].key_code = key_table[btn];
766 si->btn[btn].enabled = true;
767 }
768 for (; btn < si->si_ofs.num_btns; btn++) {
769 si->btn[btn].key_code = KEY_RESERVED;
770 si->btn[btn].enabled = true;
771 }
772
773 return rc;
774 }
775
776 si->si_ofs.btn_keys_size = 0;
777 kfree(si->btn);
778 si->btn = NULL;
779 return rc;
780 }
781
782 static int cyttsp4_si_get_op_data_ptrs(struct cyttsp4_core_data *cd)
783 {
784 struct cyttsp4_sysinfo *si = &cd->sysinfo;
785 if (si->xy_mode == NULL) {
786 si->xy_mode = kzalloc(si->si_ofs.mode_size, GFP_KERNEL);
787 if (si->xy_mode == NULL)
788 return -ENOMEM;
789 }
790
791 if (si->xy_data == NULL) {
792 si->xy_data = kzalloc(si->si_ofs.data_size, GFP_KERNEL);
793 if (si->xy_data == NULL)
794 return -ENOMEM;
795 }
796
797 if (si->btn_rec_data == NULL) {
798 si->btn_rec_data = kzalloc(si->si_ofs.btn_rec_size *
799 si->si_ofs.num_btns, GFP_KERNEL);
800 if (si->btn_rec_data == NULL)
801 return -ENOMEM;
802 }
803 #ifdef SHOK_SENSOR_DATA_MODE
804 /* initialize */
805 si->monitor.mntr_status = CY_MNTR_DISABLED;
806 memset(si->monitor.sensor_data, 0, sizeof(si->monitor.sensor_data));
807 #endif
808 return 0;
809 }
810
811 static void cyttsp4_si_put_log_data(struct cyttsp4_core_data *cd)
812 {
813 struct cyttsp4_sysinfo *si = &cd->sysinfo;
814 dev_dbg(cd->dev, "%s: cydata_ofs =%4d siz=%4d\n", __func__,
815 si->si_ofs.cydata_ofs, si->si_ofs.cydata_size);
816 dev_dbg(cd->dev, "%s: test_ofs =%4d siz=%4d\n", __func__,
817 si->si_ofs.test_ofs, si->si_ofs.test_size);
818 dev_dbg(cd->dev, "%s: pcfg_ofs =%4d siz=%4d\n", __func__,
819 si->si_ofs.pcfg_ofs, si->si_ofs.pcfg_size);
820 dev_dbg(cd->dev, "%s: opcfg_ofs =%4d siz=%4d\n", __func__,
821 si->si_ofs.opcfg_ofs, si->si_ofs.opcfg_size);
822 dev_dbg(cd->dev, "%s: ddata_ofs =%4d siz=%4d\n", __func__,
823 si->si_ofs.ddata_ofs, si->si_ofs.ddata_size);
824 dev_dbg(cd->dev, "%s: mdata_ofs =%4d siz=%4d\n", __func__,
825 si->si_ofs.mdata_ofs, si->si_ofs.mdata_size);
826
827 dev_dbg(cd->dev, "%s: cmd_ofs =%4d\n", __func__,
828 si->si_ofs.cmd_ofs);
829 dev_dbg(cd->dev, "%s: rep_ofs =%4d\n", __func__,
830 si->si_ofs.rep_ofs);
831 dev_dbg(cd->dev, "%s: rep_sz =%4d\n", __func__,
832 si->si_ofs.rep_sz);
833 dev_dbg(cd->dev, "%s: num_btns =%4d\n", __func__,
834 si->si_ofs.num_btns);
835 dev_dbg(cd->dev, "%s: num_btn_regs =%4d\n", __func__,
836 si->si_ofs.num_btn_regs);
837 dev_dbg(cd->dev, "%s: tt_stat_ofs =%4d\n", __func__,
838 si->si_ofs.tt_stat_ofs);
839 dev_dbg(cd->dev, "%s: tch_rec_size =%4d\n", __func__,
840 si->si_ofs.tch_rec_size);
841 dev_dbg(cd->dev, "%s: max_tchs =%4d\n", __func__,
842 si->si_ofs.max_tchs);
843 dev_dbg(cd->dev, "%s: mode_size =%4d\n", __func__,
844 si->si_ofs.mode_size);
845 dev_dbg(cd->dev, "%s: data_size =%4d\n", __func__,
846 si->si_ofs.data_size);
847 dev_dbg(cd->dev, "%s: map_sz =%4d\n", __func__,
848 si->si_ofs.map_sz);
849
850 dev_dbg(cd->dev, "%s: btn_rec_size =%2d\n", __func__,
851 si->si_ofs.btn_rec_size);
852 dev_dbg(cd->dev, "%s: btn_diff_ofs =%2d\n", __func__,
853 si->si_ofs.btn_diff_ofs);
854 dev_dbg(cd->dev, "%s: btn_diff_size =%2d\n", __func__,
855 si->si_ofs.btn_diff_size);
856
857 dev_dbg(cd->dev, "%s: max_x = 0x%04X (%d)\n", __func__,
858 si->si_ofs.max_x, si->si_ofs.max_x);
859 dev_dbg(cd->dev, "%s: x_origin = %d (%s)\n", __func__,
860 si->si_ofs.x_origin,
861 si->si_ofs.x_origin == CY_NORMAL_ORIGIN ?
862 "left corner" : "right corner");
863 dev_dbg(cd->dev, "%s: max_y = 0x%04X (%d)\n", __func__,
864 si->si_ofs.max_y, si->si_ofs.max_y);
865 dev_dbg(cd->dev, "%s: y_origin = %d (%s)\n", __func__,
866 si->si_ofs.y_origin,
867 si->si_ofs.y_origin == CY_NORMAL_ORIGIN ?
868 "upper corner" : "lower corner");
869 dev_dbg(cd->dev, "%s: max_p = 0x%04X (%d)\n", __func__,
870 si->si_ofs.max_p, si->si_ofs.max_p);
871
872 dev_dbg(cd->dev, "%s: xy_mode=%p xy_data=%p\n", __func__,
873 si->xy_mode, si->xy_data);
874 }
875
876 static int cyttsp4_get_sysinfo_regs(struct cyttsp4_core_data *cd)
877 {
878 struct cyttsp4_sysinfo *si = &cd->sysinfo;
879 int rc;
880
881 rc = cyttsp4_si_data_offsets(cd);
882 if (rc < 0)
883 return rc;
884
885 rc = cyttsp4_si_get_cydata(cd);
886 if (rc < 0)
887 return rc;
888
889 rc = cyttsp4_si_get_test_data(cd);
890 if (rc < 0)
891 return rc;
892
893 rc = cyttsp4_si_get_pcfg_data(cd);
894 if (rc < 0)
895 return rc;
896
897 rc = cyttsp4_si_get_opcfg_data(cd);
898 if (rc < 0)
899 return rc;
900
901 rc = cyttsp4_si_get_ddata(cd);
902 if (rc < 0)
903 return rc;
904
905 rc = cyttsp4_si_get_mdata(cd);
906 if (rc < 0)
907 return rc;
908
909 rc = cyttsp4_si_get_btn_data(cd);
910 if (rc < 0)
911 return rc;
912
913 rc = cyttsp4_si_get_op_data_ptrs(cd);
914 if (rc < 0) {
915 dev_err(cd->dev, "%s: failed to get_op_data\n",
916 __func__);
917 return rc;
918 }
919
920 cyttsp4_si_put_log_data(cd);
921
922 /* provide flow control handshake */
923 rc = cyttsp4_handshake(cd, si->si_data.hst_mode);
924 if (rc < 0)
925 dev_err(cd->dev, "%s: handshake fail on sysinfo reg\n",
926 __func__);
927
928 si->ready = true;
929 return rc;
930 }
931
932 static void cyttsp4_queue_startup_(struct cyttsp4_core_data *cd)
933 {
934 if (cd->startup_state == STARTUP_NONE) {
935 cd->startup_state = STARTUP_QUEUED;
936 queue_work(cd->startup_work_q, &cd->startup_work);
937 dev_info(cd->dev, "%s: cyttsp4_startup queued\n", __func__);
938 } else {
939 dev_dbg(cd->dev, "%s: startup_state = %d\n", __func__,
940 cd->startup_state);
941 }
942 }
943
944 static void cyttsp4_queue_startup(struct cyttsp4_core_data *cd)
945 {
946 dev_vdbg(cd->dev, "%s: enter\n", __func__);
947 mutex_lock(&cd->system_lock);
948 cyttsp4_queue_startup_(cd);
949 mutex_unlock(&cd->system_lock);
950 }
951
952 static void call_atten_cb(struct cyttsp4_core_data *cd,
953 enum cyttsp4_atten_type type, int mode)
954 {
955 struct atten_node *atten, *atten_n;
956
957 dev_vdbg(cd->dev, "%s: check list type=%d mode=%d\n",
958 __func__, type, mode);
959 spin_lock(&cd->spinlock);
960 list_for_each_entry_safe(atten, atten_n,
961 &cd->atten_list[type], node) {
962 if (!mode || atten->mode & mode) {
963 spin_unlock(&cd->spinlock);
964 dev_vdbg(cd->dev, "%s: attention for '%s'", __func__,
965 dev_name(&atten->ttsp->dev));
966 atten->func(atten->ttsp);
967 spin_lock(&cd->spinlock);
968 }
969 }
970 spin_unlock(&cd->spinlock);
971 }
972
973 static irqreturn_t cyttsp4_irq(int irq, void *handle)
974 {
975 struct cyttsp4_core_data *cd = handle;
976 struct device *dev = cd->dev;
977 enum cyttsp4_mode cur_mode;
978 u8 cmd_ofs = cd->sysinfo.si_ofs.cmd_ofs;
979 u8 mode[3];
980 int rc;
981 u8 cat_masked_cmd;
982
983 /*
984 * Check whether this IRQ should be ignored (external)
985 * This should be the very first thing to check since
986 * ignore_irq may be set for a very short period of time
987 */
988 if (atomic_read(&cd->ignore_irq)) {
989 dev_vdbg(dev, "%s: Ignoring IRQ\n", __func__);
990 return IRQ_HANDLED;
991 }
992
993 dev_dbg(dev, "%s int:0x%x\n", __func__, cd->int_status);
994
995 mutex_lock(&cd->system_lock);
996
997 /* Just to debug */
998 if (cd->sleep_state == SS_SLEEP_ON) {
999 dev_vdbg(dev, "%s: Received IRQ while in sleep\n",
1000 __func__);
1001 } else if (cd->sleep_state == SS_SLEEPING) {
1002 dev_vdbg(dev, "%s: Received IRQ while sleeping\n",
1003 __func__);
1004 }
1005
1006 rc = cyttsp4_adap_read(cd->core->adap, CY_REG_BASE, mode, sizeof(mode));
1007 if (rc) {
1008 dev_err(cd->dev, "%s: Fail read adapter r=%d\n", __func__, rc);
1009 goto cyttsp4_irq_exit;
1010 }
1011 dev_vdbg(dev, "%s mode[0-2]:0x%X 0x%X 0x%X\n", __func__,
1012 mode[0], mode[1], mode[2]);
1013
1014
1015 /* Check for false bootloader interrupt */
1016 if (unlikely(mode[0] == CY_CORE_BL_HOST_SYNC_BYTE)
1017 && unlikely(cd->mode == CY_MODE_BOOTLOADER)) {
1018 dev_err(dev, "%s: False interrupt in bootloader mode\n", __func__);
1019 goto cyttsp4_irq_exit;
1020 }
1021
1022 if (IS_BOOTLOADER(mode[0])) {
1023 cur_mode = CY_MODE_BOOTLOADER;
1024 dev_vdbg(dev, "%s: bl running\n", __func__);
1025 call_atten_cb(cd, CY_ATTEN_IRQ, cur_mode);
1026 if (cd->mode == CY_MODE_BOOTLOADER) {
1027 /* Signal bootloader heartbeat heard */
1028 wake_up(&cd->wait_q);
1029 goto cyttsp4_irq_exit;
1030 }
1031
1032 /* switch to bootloader */
1033 dev_dbg(dev, "%s: restart switch to bl m=%d -> m=%d\n",
1034 __func__, cd->mode, cur_mode);
1035
1036 /* catch operation->bl glitch */
1037 if (cd->mode != CY_MODE_UNKNOWN) {
1038 /* Incase startup_state do not let startup_() */
1039 cd->mode = CY_MODE_UNKNOWN;
1040 cyttsp4_queue_startup_(cd);
1041 goto cyttsp4_irq_exit;
1042 }
1043
1044 /*
1045 * do not wake thread on this switch since
1046 * it is possible to get an early heartbeat
1047 * prior to performing the reset
1048 */
1049 cd->mode = cur_mode;
1050
1051 goto cyttsp4_irq_exit;
1052 }
1053
1054 switch (mode[0] & CY_HST_MODE) {
1055 case CY_HST_OPERATE:
1056 cur_mode = CY_MODE_OPERATIONAL;
1057 dev_vdbg(dev, "%s: operational\n", __func__);
1058 break;
1059 case CY_HST_CAT:
1060 cur_mode = CY_MODE_CAT;
1061 /* set the start sensor mode state. */
1062 cat_masked_cmd = mode[2] & CY_CMD_MASK;
1063
1064 #ifdef SHOK_SENSOR_DATA_MODE
1065 if (cat_masked_cmd == CY_CMD_CAT_START_SENSOR_DATA_MODE)
1066 cd->sysinfo.monitor.mntr_status = CY_MNTR_INITIATED;
1067 else
1068 cd->sysinfo.monitor.mntr_status = CY_MNTR_DISABLED;
1069 #endif
1070 /* Get the Debug info for the interrupt. */
1071 if (cat_masked_cmd != CY_CMD_CAT_NULL &&
1072 cat_masked_cmd !=
1073 CY_CMD_CAT_RETRIEVE_PANEL_SCAN &&
1074 cat_masked_cmd != CY_CMD_CAT_EXEC_PANEL_SCAN)
1075 dev_info(cd->dev,
1076 "%s: cyttsp4_CaT_IRQ=%02X %02X %02X\n",
1077 __func__, mode[0], mode[1], mode[2]);
1078 dev_vdbg(dev, "%s: CaT\n", __func__);
1079 break;
1080 case CY_HST_SYSINFO:
1081 cur_mode = CY_MODE_SYSINFO;
1082 dev_vdbg(dev, "%s: sysinfo\n", __func__);
1083 break;
1084 default:
1085 cur_mode = CY_MODE_UNKNOWN;
1086 dev_err(dev, "%s: unknown HST mode 0x%02X\n", __func__,
1087 mode[0]);
1088 break;
1089 }
1090
1091 /* Check whether this IRQ should be ignored (internal) */
1092 if (cd->int_status & CY_INT_IGNORE) {
1093 dev_vdbg(dev, "%s: Ignoring IRQ\n", __func__);
1094 goto cyttsp4_irq_exit;
1095 }
1096
1097 /* Check for wake up interrupt */
1098 if (cd->int_status & CY_INT_AWAKE) {
1099 cd->int_status &= ~CY_INT_AWAKE;
1100 wake_up(&cd->sleep_q);
1101 dev_vdbg(dev, "%s: Received wake up interrupt\n", __func__);
1102 goto cyttsp4_irq_handshake;
1103 }
1104
1105 /* Expecting mode change interrupt */
1106 if ((cd->int_status & CY_INT_MODE_CHANGE)
1107 && (mode[0] & CY_HST_MODE_CHANGE) == 0) {
1108 cd->int_status &= ~CY_INT_MODE_CHANGE;
1109 dev_dbg(dev, "%s: finish mode switch m=%d -> m=%d\n",
1110 __func__, cd->mode, cur_mode);
1111 cd->mode = cur_mode;
1112 wake_up(&cd->wait_q);
1113 goto cyttsp4_irq_handshake;
1114 }
1115
1116 /* compare current core mode to current device mode */
1117 dev_vdbg(dev, "%s: cd->mode=%d cur_mode=%d\n",
1118 __func__, cd->mode, cur_mode);
1119 if (((mode[0] & CY_HST_MODE_CHANGE) == 0 || IS_BOOTLOADER(mode[1]))
1120 && cd->mode != cur_mode) {
1121 /* Unexpected mode change occurred */
1122 dev_err(dev, "%s %d->%d 0x%x\n", __func__, cd->mode,
1123 cur_mode, cd->int_status);
1124 dev_vdbg(dev, "%s: Unexpected mode change, startup\n",
1125 __func__);
1126 cyttsp4_queue_startup_(cd);
1127 goto cyttsp4_irq_exit;
1128 }
1129
1130 /* Expecting command complete interrupt */
1131 dev_vdbg(dev, "%s: command byte:0x%x, toggle:0x%x\n",
1132 __func__, mode[cmd_ofs], cd->cmd_toggle);
1133 if ((cd->int_status & CY_INT_EXEC_CMD)
1134 && mode[cmd_ofs] & CY_CMD_COMPLETE) {
1135 cd->int_status &= ~CY_INT_EXEC_CMD;
1136 dev_vdbg(dev, "%s: Received command complete interrupt\n",
1137 __func__);
1138 wake_up(&cd->wait_q);
1139 /*
1140 * It is possible to receive a single interrupt for
1141 * command complete and touch/button status report.
1142 * Continue processing for a possible status report.
1143 */
1144 }
1145
1146 /* This should be status report, read status regs */
1147 if (cd->mode == CY_MODE_OPERATIONAL) {
1148 dev_vdbg(dev, "%s: Read status registers\n", __func__);
1149 rc = cyttsp4_load_status_regs(cd);
1150 if (rc < 0)
1151 dev_err(dev, "%s: fail read mode regs r=%d\n",
1152 __func__, rc);
1153 }
1154
1155 /* attention IRQ */
1156 call_atten_cb(cd, CY_ATTEN_IRQ, cd->mode);
1157
1158 cyttsp4_irq_handshake:
1159 /* handshake the event */
1160 dev_vdbg(dev, "%s: Handshake mode=0x%02X r=%d\n",
1161 __func__, mode[0], rc);
1162 rc = cyttsp4_handshake(cd, mode[0]);
1163 if (rc < 0)
1164 dev_err(dev, "%s: Fail handshake mode=0x%02X r=%d\n",
1165 __func__, mode[0], rc);
1166
1167 /*
1168 * a non-zero udelay period is required for using
1169 * IRQF_TRIGGER_LOW in order to delay until the
1170 * device completes isr deassert
1171 */
1172 udelay(cd->pdata->level_irq_udelay);
1173
1174 cyttsp4_irq_exit:
1175 mutex_unlock(&cd->system_lock);
1176 dev_vdbg(dev, "%s: irq done\n", __func__);
1177 return IRQ_HANDLED;
1178 }
1179
1180 static void cyttsp4_start_wd_timer(struct cyttsp4_core_data *cd)
1181 {
1182 if (!CY_WATCHDOG_TIMEOUT)
1183 return;
1184
1185 mod_timer(&cd->watchdog_timer, jiffies +
1186 msecs_to_jiffies(CY_WATCHDOG_TIMEOUT));
1187 return;
1188 }
1189
1190 static void cyttsp4_stop_wd_timer(struct cyttsp4_core_data *cd)
1191 {
1192 if (!CY_WATCHDOG_TIMEOUT)
1193 return;
1194
1195 del_timer(&cd->watchdog_timer);
1196 cancel_work_sync(&cd->watchdog_work);
1197 del_timer(&cd->watchdog_timer);
1198 return;
1199 }
1200
1201 #ifndef CYTTSP4_WATCHDOG_NULL_CMD
1202 static void cyttsp4_watchdog_work(struct work_struct *work)
1203 {
1204 struct cyttsp4_core_data *cd =
1205 container_of(work, struct cyttsp4_core_data, watchdog_work);
1206 int rep_stat;
1207 int mode;
1208 int retval;
1209
1210 if (cd == NULL) {
1211 dev_err(cd->dev, "%s: NULL context pointer\n", __func__);
1212 return;
1213 }
1214
1215 mutex_lock(&cd->system_lock);
1216 retval = cyttsp4_load_status_regs(cd);
1217 if (retval < 0) {
1218 dev_err(cd->dev,
1219 "%s: failed to access device in watchdog timer r=%d\n",
1220 __func__, retval);
1221 cyttsp4_queue_startup_(cd);
1222 goto cyttsp4_timer_watchdog_exit_error;
1223 }
1224 mode = cd->sysinfo.xy_mode[CY_REG_BASE];
1225 rep_stat = cd->sysinfo.xy_mode[cd->sysinfo.si_ofs.rep_ofs];
1226 if (IS_BOOTLOADER(mode) && IS_BOOTLOADER(rep_stat)) {
1227 dev_err(cd->dev,
1228 "%s: device found in bootloader mode when operational mode rep_stat=0x%02X\n",
1229 __func__, rep_stat);
1230 cyttsp4_queue_startup_(cd);
1231 goto cyttsp4_timer_watchdog_exit_error;
1232 }
1233
1234 cyttsp4_start_wd_timer(cd);
1235 cyttsp4_timer_watchdog_exit_error:
1236 mutex_unlock(&cd->system_lock);
1237 return;
1238 }
1239 #endif
1240
1241 static void cyttsp4_watchdog_timer(unsigned long handle)
1242 {
1243 struct cyttsp4_core_data *cd = (struct cyttsp4_core_data *)handle;
1244
1245 dev_vdbg(cd->dev, "%s: Timer triggered\n", __func__);
1246
1247 if (!cd)
1248 return;
1249
1250 if (!work_pending(&cd->watchdog_work))
1251 schedule_work(&cd->watchdog_work);
1252
1253 return;
1254 }
1255
1256 static int cyttsp4_write_(struct cyttsp4_device *ttsp, int mode, u8 addr,
1257 const void *buf, int size)
1258 {
1259 struct device *dev = &ttsp->dev;
1260 struct cyttsp4_core *core = ttsp->core;
1261 struct cyttsp4_core_data *cd = dev_get_drvdata(&core->dev);
1262 int rc = 0;
1263
1264 mutex_lock(&cd->adap_lock);
1265 if (mode != cd->mode) {
1266 dev_dbg(dev, "%s: %s (having %x while %x requested)\n",
1267 __func__, "attempt to write in missing mode",
1268 cd->mode, mode);
1269 rc = -EACCES;
1270 goto exit;
1271 }
1272 rc = cyttsp4_adap_write(core->adap, addr, buf, size);
1273 exit:
1274 mutex_unlock(&cd->adap_lock);
1275 return rc;
1276 }
1277
1278 static int cyttsp4_read_(struct cyttsp4_device *ttsp, int mode, u8 addr,
1279 void *buf, int size)
1280 {
1281 struct device *dev = &ttsp->dev;
1282 struct cyttsp4_core *core = ttsp->core;
1283 struct cyttsp4_core_data *cd = dev_get_drvdata(&core->dev);
1284 int rc = 0;
1285
1286 mutex_lock(&cd->adap_lock);
1287 if (mode != cd->mode) {
1288 dev_dbg(dev, "%s: %s (having %x while %x requested)\n",
1289 __func__, "attempt to read in missing mode",
1290 cd->mode, mode);
1291 rc = -EACCES;
1292 goto exit;
1293 }
1294 rc = cyttsp4_adap_read(core->adap, addr, buf, size);
1295 exit:
1296 mutex_unlock(&cd->adap_lock);
1297 return rc;
1298 }
1299
1300 static int cyttsp4_subscribe_attention_(struct cyttsp4_device *ttsp,
1301 enum cyttsp4_atten_type type,
1302 int (*func)(struct cyttsp4_device *), int mode)
1303 {
1304 struct cyttsp4_core *core = ttsp->core;
1305 struct cyttsp4_core_data *cd = dev_get_drvdata(&core->dev);
1306 unsigned long flags;
1307 struct atten_node *atten, *atten_new;
1308
1309 atten_new = kzalloc(sizeof(*atten_new), GFP_KERNEL);
1310 if (!atten_new) {
1311 dev_err(cd->dev, "%s: Fail alloc atten node\n", __func__);
1312 return -ENOMEM;
1313 }
1314
1315 dev_dbg(cd->dev, "%s from '%s'\n", __func__, dev_name(cd->dev));
1316
1317 spin_lock_irqsave(&cd->spinlock, flags);
1318 list_for_each_entry(atten, &cd->atten_list[type], node) {
1319 if (atten->ttsp == ttsp && atten->mode == mode) {
1320 spin_unlock_irqrestore(&cd->spinlock, flags);
1321 dev_vdbg(cd->dev, "%s: %s=%p %s=%d\n",
1322 __func__,
1323 "already subscribed attention",
1324 ttsp, "mode", mode);
1325
1326 return 0;
1327 }
1328 }
1329
1330 atten_new->ttsp = ttsp;
1331 atten_new->mode = mode;
1332 atten_new->func = func;
1333
1334 list_add(&atten_new->node, &cd->atten_list[type]);
1335 spin_unlock_irqrestore(&cd->spinlock, flags);
1336
1337 return 0;
1338 }
1339
1340 static int cyttsp4_unsubscribe_attention_(struct cyttsp4_device *ttsp,
1341 enum cyttsp4_atten_type type, int (*func)(struct cyttsp4_device *),
1342 int mode)
1343 {
1344 struct cyttsp4_core *core = ttsp->core;
1345 struct cyttsp4_core_data *cd = dev_get_drvdata(&core->dev);
1346 struct atten_node *atten, *atten_n;
1347 unsigned long flags;
1348
1349 spin_lock_irqsave(&cd->spinlock, flags);
1350 list_for_each_entry_safe(atten, atten_n, &cd->atten_list[type], node) {
1351 if (atten->ttsp == ttsp && atten->mode == mode) {
1352 list_del(&atten->node);
1353 spin_unlock_irqrestore(&cd->spinlock, flags);
1354 kfree(atten);
1355 dev_vdbg(cd->dev, "%s: %s=%p %s=%d\n",
1356 __func__,
1357 "unsub for atten->ttsp", atten->ttsp,
1358 "atten->mode", atten->mode);
1359 return 0;
1360 }
1361 }
1362 spin_unlock_irqrestore(&cd->spinlock, flags);
1363
1364 return -ENODEV;
1365 }
1366
1367 static int request_exclusive(struct cyttsp4_core_data *cd, void *ownptr,
1368 int timeout_ms)
1369 {
1370 int t = msecs_to_jiffies(timeout_ms);
1371 bool with_timeout = (timeout_ms != 0);
1372
1373 mutex_lock(&cd->system_lock);
1374 if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
1375 cd->exclusive_dev = ownptr;
1376 goto exit;
1377 }
1378
1379 cd->exclusive_waits++;
1380 wait:
1381 mutex_unlock(&cd->system_lock);
1382 if (with_timeout) {
1383 t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
1384 if (IS_TMO(t)) {
1385 dev_err(cd->dev, "%s: tmo waiting exclusive access\n",
1386 __func__);
1387 return -ETIME;
1388 }
1389 } else {
1390 wait_event(cd->wait_q, !cd->exclusive_dev);
1391 }
1392 mutex_lock(&cd->system_lock);
1393 if (cd->exclusive_dev)
1394 goto wait;
1395 cd->exclusive_dev = ownptr;
1396 cd->exclusive_waits--;
1397 exit:
1398 mutex_unlock(&cd->system_lock);
1399 dev_vdbg(cd->dev, "%s: request_exclusive ok=%p\n",
1400 __func__, ownptr);
1401
1402 return 0;
1403 }
1404
1405 static int cyttsp4_request_exclusive_(struct cyttsp4_device *ttsp,
1406 int timeout_ms)
1407 {
1408 struct cyttsp4_core *core = ttsp->core;
1409 struct cyttsp4_core_data *cd = dev_get_drvdata(&core->dev);
1410 return request_exclusive(cd, (void *)ttsp, timeout_ms);
1411 }
1412
1413 /*
1414 * returns error if was not owned
1415 */
1416 static int release_exclusive(struct cyttsp4_core_data *cd, void *ownptr)
1417 {
1418 mutex_lock(&cd->system_lock);
1419 if (cd->exclusive_dev != ownptr) {
1420 mutex_unlock(&cd->system_lock);
1421 return -EINVAL;
1422 }
1423
1424 dev_vdbg(cd->dev, "%s: exclusive_dev %p freed\n",
1425 __func__, cd->exclusive_dev);
1426 cd->exclusive_dev = NULL;
1427 wake_up(&cd->wait_q);
1428 mutex_unlock(&cd->system_lock);
1429 return 0;
1430 }
1431
1432 static int cyttsp4_release_exclusive_(struct cyttsp4_device *ttsp)
1433 {
1434 struct cyttsp4_core *core = ttsp->core;
1435 struct cyttsp4_core_data *cd = dev_get_drvdata(&core->dev);
1436 return release_exclusive(cd, (void *)ttsp);
1437 }
1438
1439
1440 static int cyttsp4_reset_checkout(struct cyttsp4_core_data *cd)
1441 {
1442 int rc;
1443 int i;
1444 u8 buf;
1445
1446 /* reset hardware */
1447 mutex_lock(&cd->system_lock);
1448 dev_dbg(cd->dev, "%s: reset hw...\n", __func__);
1449 rc = cyttsp4_hw_reset(cd);
1450 cd->mode = CY_MODE_UNKNOWN;
1451 mutex_unlock(&cd->system_lock);
1452 if (rc < 0) {
1453 dev_err(cd->dev, "%s: %s adap='%s' r=%d\n", __func__,
1454 "Fail hw reset", cd->core->adap->id, rc);
1455 return rc;
1456 }
1457
1458 dev_dbg(cd->dev, "%s: msleep 70 ms\n", __func__);
1459 msleep(70);
1460 dev_dbg(cd->dev, "%s: begin to check cypress device\n", __func__);
1461 for (i = 0; i < CY_CORE_READTIMES; i++) {
1462 rc = cyttsp4_adap_read(cd->core->adap, CY_REG_BASE,
1463 &buf, sizeof(buf));
1464 dev_dbg(cd->dev, "%s: rc =%d\n", __func__,rc);
1465 if (rc < 0){
1466 dev_err(cd->dev,"%s:I2C cannot communicate, no cypress device, exit!!!\n",__func__);
1467 }
1468 else {
1469 dev_dbg(cd->dev, "%s: find cypress device!!!\n", __func__);
1470 return rc;
1471 }
1472 }
1473 return rc;
1474 }
1475
1476
1477 static int cyttsp4_wait_bl_heartbeat(struct cyttsp4_core_data *cd)
1478 {
1479 long t;
1480 int rc = 0;
1481
1482 /* wait heartbeat */
1483 dev_vdbg(cd->dev, "%s: wait heartbeat...\n", __func__);
1484 t = wait_event_timeout(cd->wait_q, cd->mode == CY_MODE_BOOTLOADER,
1485 msecs_to_jiffies(CY_CORE_RESET_AND_WAIT_TIMEOUT));
1486 if (IS_TMO(t)) {
1487 dev_err(cd->dev, "%s: tmo waiting bl heartbeat cd->mode=%d\n",
1488 __func__, cd->mode);
1489 rc = -ETIME;
1490 }
1491
1492 return rc;
1493 }
1494
1495 static int cyttsp4_wait_sysinfo_mode(struct cyttsp4_core_data *cd)
1496 {
1497 long t;
1498
1499 dev_vdbg(cd->dev, "%s: wait sysinfo...\n", __func__);
1500
1501 t = wait_event_timeout(cd->wait_q, cd->mode == CY_MODE_SYSINFO,
1502 msecs_to_jiffies(CY_CORE_MODE_CHANGE_TIMEOUT));
1503 if (IS_TMO(t)) {
1504 dev_err(cd->dev, "%s: tmo waiting exit bl cd->mode=%d\n",
1505 __func__, cd->mode);
1506 mutex_lock(&cd->system_lock);
1507 cd->int_status &= ~CY_INT_MODE_CHANGE;
1508 mutex_unlock(&cd->system_lock);
1509 return -ETIME;
1510 }
1511
1512 return 0;
1513 }
1514
1515 static int cyttsp4_reset_and_wait(struct cyttsp4_core_data *cd)
1516 {
1517 int rc;
1518
1519 /* reset hardware */
1520 mutex_lock(&cd->system_lock);
1521 dev_dbg(cd->dev, "%s: reset hw...\n", __func__);
1522 rc = cyttsp4_hw_reset(cd);
1523 cd->mode = CY_MODE_UNKNOWN;
1524 mutex_unlock(&cd->system_lock);
1525 if (rc < 0) {
1526 dev_err(cd->dev, "%s: %s adap='%s' r=%d\n", __func__,
1527 "Fail hw reset", cd->core->adap->id, rc);
1528 return rc;
1529 }
1530
1531 return cyttsp4_wait_bl_heartbeat(cd);
1532 }
1533
1534 /*
1535 * returns err if refused or timeout; block until mode change complete
1536 * bit is set (mode change interrupt)
1537 */
1538 static int set_mode(struct cyttsp4_core_data *cd, int new_mode)
1539 {
1540 u8 new_dev_mode;
1541 u8 mode;
1542 long t;
1543 int rc;
1544
1545 switch (new_mode) {
1546 case CY_MODE_OPERATIONAL:
1547 new_dev_mode = CY_HST_OPERATE;
1548 break;
1549 case CY_MODE_SYSINFO:
1550 new_dev_mode = CY_HST_SYSINFO;
1551 break;
1552 case CY_MODE_CAT:
1553 new_dev_mode = CY_HST_CAT;
1554 break;
1555 default:
1556 dev_err(cd->dev, "%s: invalid mode: %02X(%d)\n",
1557 __func__, new_mode, new_mode);
1558 return -EINVAL;
1559 }
1560
1561 /* change mode */
1562 dev_dbg(cd->dev, "%s: %s=%p new_dev_mode=%02X new_mode=%d\n",
1563 __func__, "have exclusive", cd->exclusive_dev,
1564 new_dev_mode, new_mode);
1565
1566 rc = cyttsp4_adap_read(cd->core->adap, CY_REG_BASE,
1567 &mode, sizeof(mode));
1568 if (rc < 0) {
1569 dev_err(cd->dev, "%s: Fail read mode r=%d\n",
1570 __func__, rc);
1571 goto exit;
1572 }
1573
1574 /* Clear device mode bits and set to new mode */
1575 mode &= ~CY_HST_MODE;
1576 mode |= new_dev_mode | CY_HST_MODE_CHANGE;
1577
1578 mutex_lock(&cd->system_lock);
1579 cd->int_status |= CY_INT_MODE_CHANGE;
1580 rc = cyttsp4_adap_write(cd->core->adap, CY_REG_BASE,
1581 &mode, sizeof(mode));
1582 if (rc < 0)
1583 dev_err(cd->dev, "%s: Fail write mode change r=%d\n",
1584 __func__, rc);
1585 mutex_unlock(&cd->system_lock);
1586
1587 /* wait for mode change done interrupt */
1588 t = wait_event_timeout(cd->wait_q,
1589 (cd->int_status & CY_INT_MODE_CHANGE) == 0,
1590 msecs_to_jiffies(CY_CORE_MODE_CHANGE_TIMEOUT));
1591 dev_dbg(cd->dev, "%s: back from wait t=%ld cd->mode=%d\n",
1592 __func__, t, cd->mode);
1593
1594 if (IS_TMO(t)) {
1595 dev_err(cd->dev, "%s: %s\n", __func__,
1596 "tmo waiting mode change");
1597 mutex_lock(&cd->system_lock);
1598 cd->int_status &= ~CY_INT_MODE_CHANGE;
1599 mutex_unlock(&cd->system_lock);
1600 rc = -EINVAL;
1601 }
1602
1603 exit:
1604 return rc;
1605 }
1606
1607 /*
1608 * returns err if refused or timeout(core uses fixed timeout period) occurs;
1609 * blocks until ISR occurs
1610 */
1611 static int cyttsp4_request_reset_(struct cyttsp4_device *ttsp)
1612 {
1613 struct cyttsp4_core *core = ttsp->core;
1614 struct cyttsp4_core_data *cd = dev_get_drvdata(&core->dev);
1615 int rc;
1616
1617 rc = cyttsp4_reset_and_wait(cd);
1618 if (rc < 0)
1619 dev_err(cd->dev, "%s: Error on h/w reset r=%d\n",
1620 __func__, rc);
1621
1622 return rc;
1623 }
1624
1625 /*
1626 * returns err if refused ; if no error then restart has completed
1627 * and system is in normal operating mode
1628 */
1629 static int cyttsp4_request_restart_(struct cyttsp4_device *ttsp, bool wait)
1630 {
1631 struct cyttsp4_core *core = ttsp->core;
1632 struct cyttsp4_core_data *cd = dev_get_drvdata(&core->dev);
1633
1634 cyttsp4_queue_startup(cd);
1635
1636 if (wait)
1637 wait_event(cd->wait_q, cd->startup_state == STARTUP_NONE);
1638
1639 return 0;
1640 }
1641 static int cyttsp4_request_set_mode_(struct cyttsp4_device *ttsp, int mode)
1642 {
1643 struct cyttsp4_core *core = ttsp->core;
1644 struct cyttsp4_core_data *cd = dev_get_drvdata(&core->dev);
1645 int rc;
1646
1647 rc = set_mode(cd, mode);
1648 if (rc < 0)
1649 dev_err(cd->dev, "%s: fail set_mode=%02X(%d)\n",
1650 __func__, cd->mode, cd->mode);
1651 #ifdef SHOK_SENSOR_DATA_MODE
1652 if (cd->sysinfo.monitor.mntr_status == CY_MNTR_INITIATED &&
1653 mode == CY_MODE_OPERATIONAL)
1654 cd->sysinfo.monitor.mntr_status = CY_MNTR_STARTED;
1655 else
1656 cd->sysinfo.monitor.mntr_status = CY_MNTR_DISABLED;
1657 #endif
1658
1659 return rc;
1660 }
1661
1662 /*
1663 * returns NULL if sysinfo has not been acquired from the device yet
1664 */
1665 static struct cyttsp4_sysinfo *cyttsp4_request_sysinfo_(
1666 struct cyttsp4_device *ttsp)
1667 {
1668 struct cyttsp4_core *core = ttsp->core;
1669 struct cyttsp4_core_data *cd = dev_get_drvdata(&core->dev);
1670
1671 if (cd->sysinfo.ready)
1672 return &cd->sysinfo;
1673
1674 return NULL;
1675 }
1676
1677 static struct cyttsp4_loader_platform_data *cyttsp4_request_loader_pdata_(
1678 struct cyttsp4_device *ttsp)
1679 {
1680 struct cyttsp4_core *core = ttsp->core;
1681 struct cyttsp4_core_data *cd = dev_get_drvdata(&core->dev);
1682 return cd->pdata->loader_pdata;
1683 }
1684
1685 static int cyttsp4_request_handshake_(struct cyttsp4_device *ttsp, u8 mode)
1686 {
1687 struct cyttsp4_core *core = ttsp->core;
1688 struct cyttsp4_core_data *cd = dev_get_drvdata(&core->dev);
1689 int rc;
1690
1691 rc = cyttsp4_handshake(cd, mode);
1692 if (rc < 0)
1693 dev_err(&core->dev, "%s: Fail handshake r=%d\n", __func__, rc);
1694
1695 return rc;
1696 }
1697
1698 static int cyttsp4_request_toggle_lowpower_(struct cyttsp4_device *ttsp,
1699 u8 mode)
1700 {
1701 struct cyttsp4_core *core = ttsp->core;
1702 struct cyttsp4_core_data *cd = dev_get_drvdata(&core->dev);
1703 int rc = cyttsp4_toggle_low_power(cd, mode);
1704 if (rc < 0)
1705 dev_err(&core->dev, "%s: Fail toggle low power r=%d\n",
1706 __func__, rc);
1707 return rc;
1708 }
1709
1710 /*
1711 * Send command to device for CAT and OP modes
1712 * return negative value on error, 0 on success
1713 */
1714 static int cyttsp4_exec_cmd(struct cyttsp4_core_data *cd, u8 mode,
1715 u8 *cmd_buf, size_t cmd_size, u8 *return_buf,
1716 size_t return_buf_size, int timeout_ms)
1717 {
1718 struct cyttsp4_sysinfo *si = &cd->sysinfo;
1719 struct device *dev = cd->dev;
1720 int cmd_ofs;
1721 u8 command;
1722 int rc;
1723
1724 mutex_lock(&cd->system_lock);
1725 if (mode != cd->mode) {
1726 dev_err(dev, "%s: %s (having %x while %x requested)\n",
1727 __func__, "attempt to exec cmd in missing mode",
1728 cd->mode, mode);
1729 mutex_unlock(&cd->system_lock);
1730 return -EACCES;
1731 }
1732
1733 switch (mode) {
1734 case CY_MODE_CAT:
1735 cmd_ofs = CY_REG_CAT_CMD;
1736 break;
1737 case CY_MODE_OPERATIONAL:
1738 cmd_ofs = si->si_ofs.cmd_ofs;
1739 break;
1740 default:
1741 dev_err(dev, "%s: Unsupported mode %x for exec cmd\n",
1742 __func__, mode);
1743 mutex_unlock(&cd->system_lock);
1744 return -EACCES;
1745 }
1746
1747 /* Check if complete is set, so write new command */
1748 rc = cyttsp4_adap_read(cd->core->adap, cmd_ofs, &command, 1);
1749 if (rc < 0) {
1750 dev_err(dev, "%s: Error on read r=%d\n", __func__, rc);
1751 mutex_unlock(&cd->system_lock);
1752 return rc;
1753 }
1754
1755 cd->cmd_toggle = GET_TOGGLE(command);
1756 cd->int_status |= CY_INT_EXEC_CMD;
1757
1758 if ((command & CY_CMD_COMPLETE_MASK) == 0) {
1759 /* Let irq handler run */
1760 mutex_unlock(&cd->system_lock);
1761 rc = wait_event_timeout(cd->wait_q,
1762 (cd->int_status & CY_INT_EXEC_CMD) == 0,
1763 msecs_to_jiffies(timeout_ms));
1764 if (IS_TMO(rc)) {
1765 dev_err(dev, "%s: Command execution timed out\n",
1766 __func__);
1767 cd->int_status &= ~CY_INT_EXEC_CMD;
1768 return -EINVAL;
1769 }
1770
1771 /* For next command */
1772 mutex_lock(&cd->system_lock);
1773 rc = cyttsp4_adap_read(cd->core->adap, cmd_ofs, &command, 1);
1774 if (rc < 0) {
1775 dev_err(dev, "%s: Error on read r=%d\n", __func__, rc);
1776 mutex_unlock(&cd->system_lock);
1777 return rc;
1778 }
1779 cd->cmd_toggle = GET_TOGGLE(command);
1780 cd->int_status |= CY_INT_EXEC_CMD;
1781 }
1782
1783 /*
1784 * Write new command
1785 * Only update command bits 0:5
1786 * Clear command complete bit & toggle bit
1787 */
1788 cmd_buf[0] = cmd_buf[0] & CY_CMD_MASK;
1789 rc = cyttsp4_adap_write(cd->core->adap, cmd_ofs, cmd_buf, cmd_size);
1790 if (rc < 0) {
1791 dev_err(dev, "%s: Error on write command r=%d\n",
1792 __func__, rc);
1793 mutex_unlock(&cd->system_lock);
1794 return rc;
1795 }
1796
1797 /*
1798 * Wait command to be completed
1799 */
1800 mutex_unlock(&cd->system_lock);
1801 rc = wait_event_timeout(cd->wait_q,
1802 (cd->int_status & CY_INT_EXEC_CMD) == 0,
1803 msecs_to_jiffies(timeout_ms));
1804 if (IS_TMO(rc)) {
1805 dev_err(dev, "%s: Command execution timed out\n", __func__);
1806 cd->int_status &= ~CY_INT_EXEC_CMD;
1807 return -EINVAL;
1808 }
1809
1810 if (return_buf_size == 0 || return_buf == NULL)
1811 return 0;
1812
1813 rc = cyttsp4_adap_read(cd->core->adap, cmd_ofs + 1, return_buf,
1814 return_buf_size);
1815 if (rc < 0) {
1816 dev_err(dev, "%s: Error on read 3 r=%d\n", __func__, rc);
1817 return rc;
1818 }
1819
1820 return 0;
1821 }
1822
1823 static int cyttsp4_request_exec_cmd_(struct cyttsp4_device *ttsp, u8 mode,
1824 u8 *cmd_buf, size_t cmd_size, u8 *return_buf,
1825 size_t return_buf_size, int timeout_ms)
1826 {
1827 struct cyttsp4_core *core = ttsp->core;
1828 struct cyttsp4_core_data *cd = dev_get_drvdata(&core->dev);
1829 return cyttsp4_exec_cmd(cd, mode, cmd_buf, cmd_size,
1830 return_buf, return_buf_size, timeout_ms);
1831 }
1832
1833 static int cyttsp4_read_config_block(struct cyttsp4_core_data *cd, u8 ebid,
1834 u16 row, u8 *data, u16 length)
1835 {
1836 u8 command_buf[6];
1837 u8 *return_buf;
1838 u16 crc;
1839 int rc;
1840
1841 /* Allocate buffer for read config block command response
1842 * Header(5) + Data(length) + CRC(2)
1843 */
1844 return_buf = kmalloc(length + 7, GFP_KERNEL);
1845 if (!return_buf) {
1846 dev_err(cd->dev, "%s: Cannot allocate buffer\n",
1847 __func__);
1848 rc = -ENOMEM;
1849 goto exit;
1850 }
1851
1852 command_buf[0] = CY_CMD_CAT_READ_CFG_BLK;
1853 command_buf[1] = HI_BYTE(row);
1854 command_buf[2] = LO_BYTE(row);
1855 command_buf[3] = HI_BYTE(length);
1856 command_buf[4] = LO_BYTE(length);
1857 command_buf[5] = ebid;
1858
1859 rc = cyttsp4_exec_cmd(cd, CY_MODE_CAT, command_buf, 6,
1860 return_buf, length + 7, CY_COMMAND_COMPLETE_TIMEOUT);
1861 if (rc) {
1862 dev_err(cd->dev, "%s: Error executing command r=%d\n",
1863 __func__, rc);
1864 goto free_buffer;
1865 }
1866
1867 crc = cyttsp4_calc_app_crc(&return_buf[5], length);
1868
1869 /* Validate response */
1870 if (return_buf[0] != CY_CMD_STATUS_SUCCESS
1871 || return_buf[1] != ebid
1872 || return_buf[2] != HI_BYTE(length)
1873 || return_buf[3] != LO_BYTE(length)
1874 || return_buf[5 + length] != HI_BYTE(crc)
1875 || return_buf[6 + length] != LO_BYTE(crc)) {
1876 dev_err(cd->dev, "%s: Fail executing command\n",
1877 __func__);
1878 rc = -1;
1879 goto free_buffer;
1880 }
1881
1882 memcpy(data, &return_buf[5], length);
1883
1884 cyttsp4_pr_buf(cd->dev, cd->pr_buf, data, length, "read_config_block");
1885
1886 free_buffer:
1887 kfree(return_buf);
1888 exit:
1889 return rc;
1890 }
1891
1892 static int cyttsp4_write_config_block(struct cyttsp4_core_data *cd, u8 ebid,
1893 u16 row, const u8 *data, u16 length)
1894 {
1895 u8 return_buf[5];
1896 u8 *command_buf;
1897 u16 crc;
1898 int rc;
1899
1900 /* Allocate buffer for write config block command
1901 * Header(6) + Data(length) + Security Key(8) + CRC(2)
1902 */
1903 command_buf = kmalloc(length + 8 + sizeof(security_key), GFP_KERNEL);
1904 if (!command_buf) {
1905 dev_err(cd->dev, "%s: Cannot allocate buffer\n",
1906 __func__);
1907 rc = -ENOMEM;
1908 goto exit;
1909 }
1910
1911 crc = cyttsp4_calc_app_crc(data, length);
1912
1913 command_buf[0] = CY_CMD_CAT_WRITE_CFG_BLK;
1914 command_buf[1] = HI_BYTE(row);
1915 command_buf[2] = LO_BYTE(row);
1916 command_buf[3] = HI_BYTE(length);
1917 command_buf[4] = LO_BYTE(length);
1918 command_buf[5] = ebid;
1919
1920 command_buf[6 + length + sizeof(security_key)] = HI_BYTE(crc);
1921 command_buf[7 + length + sizeof(security_key)] = LO_BYTE(crc);
1922
1923 memcpy(&command_buf[6], data, length);
1924 memcpy(&command_buf[6 + length], security_key, sizeof(security_key));
1925
1926
1927 cyttsp4_pr_buf(cd->dev, cd->pr_buf, command_buf, length + 8 +
1928 sizeof(security_key), "write_config_block");
1929
1930 rc = cyttsp4_exec_cmd(cd, CY_MODE_CAT,
1931 command_buf, length + 8 + sizeof(security_key),
1932 return_buf, 5, CY_COMMAND_COMPLETE_TIMEOUT);
1933 if (rc) {
1934 dev_err(cd->dev, "%s: Error executing command r=%d\n",
1935 __func__, rc);
1936 goto free_buffer;
1937 }
1938
1939 /* Validate response */
1940 if (return_buf[0] != CY_CMD_STATUS_SUCCESS
1941 || return_buf[1] != ebid
1942 || return_buf[2] != HI_BYTE(length)
1943 || return_buf[3] != LO_BYTE(length)) {
1944 dev_err(cd->dev, "%s: Fail executing command\n",
1945 __func__);
1946 rc = -1;
1947 goto free_buffer;
1948 }
1949
1950 free_buffer:
1951 kfree(command_buf);
1952 exit:
1953 return rc;
1954 }
1955
1956 static int cyttsp4_get_config_row_size(struct cyttsp4_core_data *cd,
1957 u16 *config_row_size)
1958 {
1959 u8 command_buf[1];
1960 u8 return_buf[2];
1961 int rc;
1962
1963 command_buf[0] = CY_CMD_CAT_GET_CFG_ROW_SZ;
1964
1965 rc = cyttsp4_exec_cmd(cd, CY_MODE_CAT, command_buf, 1,
1966 return_buf, 2, CY_COMMAND_COMPLETE_TIMEOUT);
1967 if (rc) {
1968 dev_err(cd->dev, "%s: Error executing command r=%d\n",
1969 __func__, rc);
1970 goto exit;
1971 }
1972
1973 *config_row_size = get_unaligned_be16(&return_buf[0]);
1974
1975 exit:
1976 return rc;
1977 }
1978
1979 static int cyttsp4_verify_config_block_crc(struct cyttsp4_core_data *cd,
1980 u8 ebid, u16 *calc_crc, u16 *stored_crc, bool *match)
1981 {
1982 u8 command_buf[2];
1983 u8 return_buf[5];
1984 int rc;
1985
1986 command_buf[0] = CY_CMD_CAT_VERIFY_CFG_BLK_CRC;
1987 command_buf[1] = ebid;
1988
1989 rc = cyttsp4_exec_cmd(cd, CY_MODE_CAT, command_buf, 2,
1990 return_buf, 5, CY_COMMAND_COMPLETE_TIMEOUT);
1991 if (rc) {
1992 dev_err(cd->dev, "%s: Error executing command r=%d\n",
1993 __func__, rc);
1994 goto exit;
1995 }
1996
1997 *calc_crc = get_unaligned_be16(&return_buf[1]);
1998 *stored_crc = get_unaligned_be16(&return_buf[3]);
1999 if (match)
2000 *match = !return_buf[0];
2001 exit:
2002 return rc;
2003 }
2004
2005 static int cyttsp4_get_config_block_crc(struct cyttsp4_core_data *cd,
2006 u8 ebid, u16 *crc)
2007 {
2008 u8 command_buf[2];
2009 u8 return_buf[3];
2010 int rc;
2011
2012 command_buf[0] = CY_CMD_OP_GET_CRC;
2013 command_buf[1] = ebid;
2014
2015 rc = cyttsp4_exec_cmd(cd, CY_MODE_OPERATIONAL, command_buf, 2,
2016 return_buf, 3, CY_COMMAND_COMPLETE_TIMEOUT);
2017 if (rc) {
2018 dev_err(cd->dev, "%s: Error executing command r=%d\n",
2019 __func__, rc);
2020 goto exit;
2021 }
2022
2023 /* Validate response */
2024 if (return_buf[0] != CY_CMD_STATUS_SUCCESS) {
2025 dev_err(cd->dev, "%s: Fail executing command\n",
2026 __func__);
2027 rc = -1;
2028 goto exit;
2029 }
2030
2031 *crc = get_unaligned_be16(&return_buf[1]);
2032
2033 exit:
2034 return rc;
2035 }
2036
2037 static int cyttsp4_get_ttconfig_version(struct cyttsp4_core_data *cd,
2038 u16 *version)
2039 {
2040 struct cyttsp4_sysinfo *si = &cd->sysinfo;
2041 u8 data[CY_TTCONFIG_OFFSET + 2];
2042 int rc;
2043
2044 if (!si->ready) {
2045 rc = -ENODEV;
2046 goto exit;
2047 }
2048
2049 rc = cyttsp4_read_config_block(cd, CY_TCH_PARM_EBID, 0, data,
2050 CY_TTCONFIG_OFFSET + 2);
2051 if (rc) {
2052 dev_err(cd->dev, "%s: Error on read config block\n",
2053 __func__);
2054 goto exit;
2055 }
2056
2057 if (IS_LITTLEENDIAN(si->si_ptrs.cydata->device_info))
2058 *version = get_unaligned_le16(&data[CY_TTCONFIG_OFFSET]);
2059 else
2060 *version = get_unaligned_be16(&data[CY_TTCONFIG_OFFSET]);
2061
2062 exit:
2063 return rc;
2064 }
2065
2066 static int cyttsp4_get_config_length(struct cyttsp4_core_data *cd, u8 ebid,
2067 u16 *length, u16 *max_length)
2068 {
2069 struct cyttsp4_sysinfo *si = &cd->sysinfo;
2070 u8 data[4];
2071 int rc;
2072
2073 if (!si->ready) {
2074 rc = -ENODEV;
2075 goto exit;
2076 }
2077
2078 rc = cyttsp4_read_config_block(cd, ebid, 0, data, 4);
2079 if (rc) {
2080 dev_err(cd->dev, "%s: Error on read config block\n",
2081 __func__);
2082 goto exit;
2083 }
2084
2085 if (IS_LITTLEENDIAN(si->si_ptrs.cydata->device_info)) {
2086 *length = get_unaligned_le16(&data[0]);
2087 *max_length = get_unaligned_le16(&data[2]);
2088 } else {
2089 *length = get_unaligned_be16(&data[0]);
2090 *max_length = get_unaligned_be16(&data[2]);
2091 }
2092
2093 exit:
2094 return rc;
2095 }
2096
2097 static int cyttsp4_write_config_common(struct cyttsp4_core_data *cd, u8 ebid,
2098 u16 offset, u8 *data, u16 length)
2099 {
2100 u16 cur_block, cur_off, end_block, end_off;
2101 int read_write_len, copy_len;
2102 u16 config_row_size = 0;
2103 u8 *row_data = NULL;
2104 int rc;
2105
2106 rc = cyttsp4_get_config_row_size(cd, &config_row_size);
2107 if (rc) {
2108 dev_err(cd->dev, "%s: Cannot get config row size\n",
2109 __func__);
2110 goto exit;
2111 }
2112
2113 cur_block = offset / config_row_size;
2114 cur_off = offset % config_row_size;
2115
2116 end_block = (offset + length) / config_row_size;
2117 end_off = (offset + length) % config_row_size;
2118
2119 /* Check whether we need to fetch the whole block first */
2120 if (cur_off == 0)
2121 goto no_offset;
2122
2123 row_data = kmalloc(config_row_size, GFP_KERNEL);
2124 if (!row_data) {
2125 dev_err(cd->dev, "%s: Cannot allocate buffer\n", __func__);
2126 rc = -ENOMEM;
2127 goto exit;
2128 }
2129
2130 if (cur_block == end_block) {
2131 read_write_len = length + cur_off;
2132 copy_len = length;
2133 } else {
2134 read_write_len = config_row_size;
2135 copy_len = config_row_size - cur_off;
2136 }
2137
2138 /* Get the block, update it and write it back */
2139 rc = cyttsp4_read_config_block(cd, ebid, cur_block, row_data,
2140 read_write_len);
2141 if (rc) {
2142 dev_err(cd->dev, "%s: Error on read config block\n", __func__);
2143 goto free_row_data;
2144 }
2145
2146 memcpy(&row_data[cur_off], data, copy_len);
2147
2148 rc = cyttsp4_write_config_block(cd, ebid, cur_block, row_data,
2149 read_write_len);
2150 if (rc) {
2151 dev_err(cd->dev, "%s: Error on write config block\n", __func__);
2152 goto free_row_data;
2153 }
2154
2155 data += config_row_size - cur_off;
2156 cur_off = 0;
2157 cur_block++;
2158
2159 no_offset:
2160 while (cur_block < end_block) {
2161 rc = cyttsp4_write_config_block(cd, ebid, cur_block, data,
2162 config_row_size);
2163 if (rc) {
2164 dev_err(cd->dev, "%s: Error on write config block\n",
2165 __func__);
2166 goto free_row_data;
2167 }
2168
2169 data += config_row_size;
2170 cur_block++;
2171 }
2172
2173 /* Last block */
2174 if (cur_block == end_block) {
2175 rc = cyttsp4_write_config_block(cd, ebid, end_block, data,
2176 end_off);
2177 if (rc) {
2178 dev_err(cd->dev, "%s: Error on write config block\n",
2179 __func__);
2180 goto free_row_data;
2181 }
2182 }
2183
2184 free_row_data:
2185 kfree(row_data);
2186 exit:
2187 return rc;
2188 }
2189
2190 static int cyttsp4_write_config(struct cyttsp4_core_data *cd, u8 ebid,
2191 u16 offset, u8 *data, u16 length) {
2192 struct cyttsp4_sysinfo *si = &cd->sysinfo;
2193 u16 crc_new, crc_old;
2194 u16 crc_offset;
2195 u16 conf_len;
2196 u8 crc_data[2];
2197 int rc;
2198
2199 if (!si->ready) {
2200 rc = -ENODEV;
2201 goto exit;
2202 }
2203
2204 /* CRC is stored at config max length offset */
2205 rc = cyttsp4_get_config_length(cd, ebid, &conf_len, &crc_offset);
2206 if (rc) {
2207 dev_err(cd->dev, "%s: Error on get config length\n",
2208 __func__);
2209 goto exit;
2210 }
2211
2212 /* Allow CRC update also */
2213 if (offset + length > crc_offset + 2) {
2214 dev_err(cd->dev, "%s: offset + length exceeds max length(%d)\n",
2215 __func__, crc_offset + 2);
2216 rc = -EINVAL;
2217 goto exit;
2218 }
2219
2220 rc = cyttsp4_write_config_common(cd, ebid, offset, data, length);
2221 if (rc) {
2222 dev_err(cd->dev, "%s: Error on write config\n",
2223 __func__);
2224 goto exit;
2225 }
2226
2227 /* Verify config block CRC */
2228 rc = cyttsp4_verify_config_block_crc(cd, ebid,
2229 &crc_new, &crc_old, NULL);
2230 if (rc) {
2231 dev_err(cd->dev, "%s: Error on verify config block crc\n",
2232 __func__);
2233 goto exit;
2234 }
2235
2236 dev_vdbg(cd->dev, "%s: crc_new:%04X crc_old:%04X\n",
2237 __func__, crc_new, crc_old);
2238
2239 if (crc_new == crc_old) {
2240 dev_vdbg(cd->dev, "%s: Calculated crc matches stored crc\n",
2241 __func__);
2242 goto exit;
2243 }
2244
2245 if (IS_LITTLEENDIAN(si->si_ptrs.cydata->device_info))
2246 put_unaligned_le16(crc_new, crc_data);
2247 else
2248 put_unaligned_be16(crc_new, crc_data);
2249
2250 rc = cyttsp4_write_config_common(cd, ebid, crc_offset, crc_data, 2);
2251 if (rc) {
2252 dev_err(cd->dev, "%s: Error on write config crc\n",
2253 __func__);
2254 goto exit;
2255 }
2256
2257 exit:
2258 return rc;
2259 }
2260
2261 static int cyttsp4_request_write_config_(struct cyttsp4_device *ttsp, u8 ebid,
2262 u16 offset, u8 *data, u16 length) {
2263 struct cyttsp4_core *core = ttsp->core;
2264 struct cyttsp4_core_data *cd = dev_get_drvdata(&core->dev);
2265
2266 return cyttsp4_write_config(cd, ebid, offset, data, length);
2267 }
2268
2269 /* BEGIN PN:DTS2013021701945 ,Modified by l00184147, 2013/2/17*/
2270 #ifdef CYTTSP4_WATCHDOG_NULL_CMD
2271 static void cyttsp4_watchdog_work_null(struct work_struct *work)
2272 {
2273 struct cyttsp4_core_data *cd =
2274 container_of(work, struct cyttsp4_core_data, watchdog_work);
2275 u8 cmd_buf[CY_CMD_OP_NULL_CMD_SZ];
2276 bool restart = false;
2277 int rc;
2278
2279 rc = request_exclusive(cd, cd->core, 1);
2280 if (rc < 0) {
2281 dev_vdbg(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
2282 __func__, cd->exclusive_dev, cd->core);
2283 goto exit;
2284 }
2285
2286 cmd_buf[0] = CY_CMD_OP_NULL;
2287 rc = cyttsp4_exec_cmd(cd, cd->mode,
2288 cmd_buf, CY_CMD_OP_NULL_CMD_SZ,
2289 NULL, CY_CMD_OP_NULL_RET_SZ,
2290 CY_COMMAND_COMPLETE_TIMEOUT);
2291 if (rc < 0) {
2292 dev_err(cd->dev, "%s: Watchdog NULL cmd failed.\n", __func__);
2293 restart = true;
2294 }
2295
2296 if (release_exclusive(cd, cd->core) < 0)
2297 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
2298 else
2299 dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
2300
2301 exit:
2302 if (restart)
2303 cyttsp4_queue_startup(cd);
2304 else
2305 cyttsp4_start_wd_timer(cd);
2306 }
2307 #endif
2308 /* END PN:DTS2013021701945 ,Modified by l00184147, 2013/2/17*/
2309
2310 static int cyttsp4_request_stop_wd_(struct cyttsp4_device *ttsp)
2311 {
2312 struct cyttsp4_core *core = ttsp->core;
2313 struct cyttsp4_core_data *cd = dev_get_drvdata(&core->dev);
2314 cyttsp4_stop_wd_timer(cd);
2315 return 0;
2316 }
2317
2318 static int cyttsp4_core_sleep_(struct cyttsp4_core_data *cd)
2319 {
2320 enum cyttsp4_sleep_state ss = SS_SLEEP_ON;
2321 enum cyttsp4_int_state int_status = CY_INT_IGNORE;
2322 int rc = 0;
2323 u8 mode;
2324
2325 printk("printk cyttsp4_core_sleep_\n");
2326
2327 dev_vdbg(cd->dev, "%s: enter...\n", __func__);
2328
2329 /* Already in sleep mode? */
2330 mutex_lock(&cd->system_lock);
2331 if (cd->sleep_state == SS_SLEEP_ON) {
2332 mutex_unlock(&cd->system_lock);
2333 return 0;
2334 }
2335 cd->sleep_state = SS_SLEEPING;
2336 mutex_unlock(&cd->system_lock);
2337
2338 /* Wait until currently running IRQ handler exits and disable IRQ */
2339 #ifdef MTK
2340 cyttsp4_mtk_gpio_interrupt_disable();
2341 #else
2342 /* disable_irq(cd->irq); */
2343 #endif
2344
2345 cyttsp4_stop_wd_timer(cd);
2346
2347 dev_vdbg(cd->dev, "%s: write DEEP SLEEP...\n", __func__);
2348 rc = cyttsp4_adap_read(cd->core->adap, CY_REG_BASE, &mode,
2349 sizeof(mode));
2350 if (rc) {
2351 dev_err(cd->dev, "%s: Fail read adapter r=%d\n", __func__, rc);
2352 goto error;
2353 }
2354
2355 if (IS_BOOTLOADER(mode)) {
2356 dev_err(cd->dev, "%s: Device in BOOTLADER mode.\n", __func__);
2357 rc = -EINVAL;
2358 goto error;
2359 }
2360
2361 mode |= CY_HST_SLEEP;
2362 rc = cyttsp4_adap_write(cd->core->adap, CY_REG_BASE, &mode,
2363 sizeof(mode));
2364 if (rc) {
2365 dev_err(cd->dev, "%s: Fail write adapter r=%d\n", __func__, rc);
2366 goto error;
2367 }
2368 dev_vdbg(cd->dev, "%s: write DEEP SLEEP succeeded\n", __func__);
2369
2370 if (cd->pdata->power) {
2371 dev_dbg(cd->dev, "%s: Power down HW\n", __func__);
2372 rc = cd->pdata->power(cd->pdata, 0, cd->dev, &cd->ignore_irq);
2373 } else {
2374 dev_dbg(cd->dev, "%s: No power function\n", __func__);
2375 rc = 0;
2376 }
2377 if (rc < 0) {
2378 dev_err(cd->dev, "%s: HW Power down fails r=%d\n",
2379 __func__, rc);
2380 goto error;
2381 }
2382
2383 /* Give time to FW to sleep */
2384 msleep(50);
2385
2386 goto exit;
2387
2388 error:
2389 ss = SS_SLEEP_OFF;
2390 int_status = CY_INT_NONE;
2391 cyttsp4_start_wd_timer(cd);
2392
2393 exit:
2394 mutex_lock(&cd->system_lock);
2395 cd->sleep_state = ss;
2396 cd->int_status |= int_status;
2397 mutex_unlock(&cd->system_lock);
2398 #ifdef MTK
2399 // mt65xx_eint_mask mt65xx_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM);
2400 cyttsp4_mtk_gpio_interrupt_enable();
2401 #else
2402 /* enable_irq(cd->irq); */
2403 #endif
2404 return rc;
2405 }
2406
2407 static int cyttsp4_core_sleep(struct cyttsp4_core_data *cd)
2408 {
2409 int rc;
2410
2411 /* First wait for any possible running startup to finish */
2412 wait_event(cd->wait_q, cd->startup_state == STARTUP_NONE);
2413
2414 rc = request_exclusive(cd, cd->core,
2415 CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT);
2416 if (rc < 0) {
2417 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
2418 __func__, cd->exclusive_dev, cd->core);
2419 return 0;
2420 }
2421
2422 rc = cyttsp4_core_sleep_(cd);
2423
2424 if (release_exclusive(cd, cd->core) < 0)
2425 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
2426 else
2427 dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
2428
2429 return rc;
2430 }
2431
2432 static int cyttsp4_core_wake_(struct cyttsp4_core_data *cd)
2433 {
2434 struct device *dev = cd->dev;
2435 int rc;
2436 u8 mode;
2437 int t;
2438
2439 dev_vdbg(cd->dev, "%s: enter...\n", __func__);
2440
2441 /* Already woken? */
2442 mutex_lock(&cd->system_lock);
2443 if (cd->sleep_state == SS_SLEEP_OFF) {
2444 mutex_unlock(&cd->system_lock);
2445 return 0;
2446 }
2447 cd->int_status &= ~CY_INT_IGNORE;
2448 cd->int_status |= CY_INT_AWAKE;
2449 cd->sleep_state = SS_WAKING;
2450
2451 /* Do not start watchdog in already woken state */
2452 cyttsp4_start_wd_timer(cd);
2453
2454 if (cd->pdata->power) {
2455 dev_dbg(dev, "%s: Power up HW\n", __func__);
2456 rc = cd->pdata->power(cd->pdata, 1, dev, &cd->ignore_irq);
2457 } else {
2458 dev_dbg(dev, "%s: No power function\n", __func__);
2459 rc = -ENOSYS;
2460 }
2461 if (rc < 0) {
2462 dev_err(dev, "%s: HW Power up fails r=%d\n",
2463 __func__, rc);
2464
2465 /* Initiate a read transaction to wake up */
2466 cyttsp4_adap_read(cd->core->adap, CY_REG_BASE, &mode,
2467 sizeof(mode));
2468 } else
2469 dev_vdbg(cd->dev, "%s: HW power up succeeds\n",
2470 __func__);
2471 mutex_unlock(&cd->system_lock);
2472
2473 t = wait_event_timeout(cd->sleep_q,
2474 (cd->int_status & CY_INT_AWAKE) == 0,
2475 msecs_to_jiffies(CY_CORE_WAKEUP_TIMEOUT));
2476 if (IS_TMO(t)) {
2477 dev_err(dev, "%s: TMO waiting for wakeup\n", __func__);
2478 mutex_lock(&cd->system_lock);
2479 cd->int_status &= ~CY_INT_AWAKE;
2480 /* Try starting up */
2481 cyttsp4_queue_startup_(cd);
2482 mutex_unlock(&cd->system_lock);
2483 }
2484
2485 mutex_lock(&cd->system_lock);
2486 cd->sleep_state = SS_SLEEP_OFF;
2487 mutex_unlock(&cd->system_lock);
2488
2489 return 0;
2490 }
2491
2492 static int cyttsp4_core_wake(struct cyttsp4_core_data *cd)
2493 {
2494 int rc;
2495
2496 rc = request_exclusive(cd, cd->core,
2497 CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT);
2498 if (rc < 0) {
2499 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
2500 __func__, cd->exclusive_dev, cd->core);
2501 return 0;
2502 }
2503
2504 rc = cyttsp4_core_wake_(cd);
2505
2506 if (release_exclusive(cd, cd->core) < 0)
2507 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
2508 else
2509 dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
2510
2511 return rc;
2512 }
2513
2514 static int cyttsp4_get_ttconfig_info(struct cyttsp4_core_data *cd)
2515 {
2516 struct cyttsp4_sysinfo *si = &cd->sysinfo;
2517 u16 length, max_length;
2518 u16 version = 0;
2519 u16 crc = 0;
2520 int rc;
2521
2522 rc = set_mode(cd, CY_MODE_CAT);
2523 if (rc < 0) {
2524 dev_err(cd->dev, "%s: failed to set mode to CAT rc=%d\n",
2525 __func__, rc);
2526 return rc;
2527 }
2528
2529 rc = cyttsp4_get_ttconfig_version(cd, &version);
2530 if (rc < 0) {
2531 dev_err(cd->dev, "%s: failed to get ttconfig version rc=%d\n",
2532 __func__, rc);
2533 return rc;
2534 }
2535
2536 rc = cyttsp4_get_config_length(cd, CY_TCH_PARM_EBID,
2537 &length, &max_length);
2538 if (rc < 0) {
2539 dev_err(cd->dev, "%s: failed to get ttconfig length rc=%d\n",
2540 __func__, rc);
2541 return rc;
2542 }
2543
2544 rc = set_mode(cd, CY_MODE_OPERATIONAL);
2545 if (rc < 0) {
2546 dev_err(cd->dev, "%s: failed to set mode to Operational rc=%d\n",
2547 __func__, rc);
2548 return rc;
2549 }
2550
2551 rc = cyttsp4_get_config_block_crc(cd, CY_TCH_PARM_EBID, &crc);
2552 if (rc < 0) {
2553 dev_err(cd->dev, "%s: failed to get ttconfig crc rc=%d\n",
2554 __func__, rc);
2555 return rc;
2556 }
2557
2558 si->ttconfig.version = version;
2559 si->ttconfig.length = length;
2560 si->ttconfig.max_length = max_length;
2561 si->ttconfig.crc = crc;
2562
2563 dev_vdbg(cd->dev, "%s: TT Config Version:%04X Length:%d Max Length:%d CRC:%04X\n",
2564 __func__, si->ttconfig.version, si->ttconfig.length,
2565 si->ttconfig.length, si->ttconfig.crc);
2566
2567 return 0;
2568 }
2569
2570 static int cyttsp4_set_opmode(struct cyttsp4_core_data *cd)
2571 {
2572 u8 cmd_buf[CY_CMD_OP_SET_PARA_CMD_SZ];
2573 u8 return_buf[CY_CMD_OP_SET_PARA_RET_SZ];
2574 u8 scan_type = 0;
2575 int rc;
2576
2577 mutex_lock(&cd->system_lock);
2578 switch (cd->opmode) {
2579 case OPMODE_NONE:
2580 mutex_unlock(&cd->system_lock);
2581 return 0;
2582 case OPMODE_FINGER:
2583 scan_type = CY_OP_PARA_SCAN_TYPE_NORMAL;
2584 break;
2585 case OPMODE_GLOVE:
2586 scan_type = CY_OP_PARA_SCAN_TYPE_APAMC_MASK |
2587 CY_OP_PARA_SCAN_TYPE_GLOVE_MASK;
2588 break;
2589 }
2590 mutex_unlock(&cd->system_lock);
2591
2592 cmd_buf[0] = CY_CMD_OP_SET_PARA;
2593 cmd_buf[1] = CY_OP_PARA_SCAN_TYPE;
2594 cmd_buf[2] = CY_OP_PARA_SCAN_TYPE_SZ;
2595 cmd_buf[3] = scan_type;
2596
2597 rc = cyttsp4_exec_cmd(cd, cd->mode,
2598 cmd_buf, sizeof(cmd_buf),
2599 return_buf, sizeof(return_buf),
2600 CY_COMMAND_COMPLETE_TIMEOUT);
2601 if (rc < 0)
2602 dev_err(cd->dev, "%s: exec cmd error.\n", __func__);
2603
2604 return rc;
2605 }
2606
2607 static int cyttsp4_startup_(struct cyttsp4_core_data *cd)
2608 {
2609 int rc;
2610
2611 dev_dbg(cd->dev, "%s: enter...\n", __func__);
2612
2613 cyttsp4_stop_wd_timer(cd);
2614
2615 /* reset hardware and wait for heartbeat */
2616 rc = cyttsp4_reset_and_wait(cd);
2617 /* BEGIN PN:DTS2013040204005 ,Modified by l00184147, 2013/4/2*/
2618 if (rc < 0){
2619 dev_err(cd->dev, "%s: Error on h/w reset r=%d\n", __func__, rc);
2620
2621 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ZERO);
2622 msleep(100);
2623 hwPowerDown(MT6323_POWER_LDO_VGP2, "TP");
2624 msleep(100);
2625 hwPowerOn(MT6323_POWER_LDO_VGP2, VOL_1800, "TP");
2626 msleep(100);
2627 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ONE);
2628 msleep(100);
2629
2630 rc = cyttsp4_reset_and_wait(cd);
2631 if (rc < 0)
2632 {
2633 dev_err(cd->dev, "%s: Error on h/w reset2 r=%d\n", __func__, rc);
2634 }
2635 }
2636 /* END PN:DTS2013040204005 ,Modified by l00184147, 2013/4/2*/
2637
2638 /* exit bl into sysinfo mode */
2639 dev_vdbg(cd->dev, "%s: write exit ldr...\n", __func__);
2640 mutex_lock(&cd->system_lock);
2641 cd->int_status &= ~CY_INT_IGNORE;
2642 cd->int_status |= CY_INT_MODE_CHANGE;
2643 rc = cyttsp4_adap_write(cd->core->adap, CY_REG_BASE,
2644 (u8 *)ldr_exit, sizeof(ldr_exit));
2645 if (rc < 0)
2646 dev_err(cd->dev, "%s: Fail write adap='%s' r=%d\n",
2647 __func__, cd->core->adap->id, rc);
2648 mutex_unlock(&cd->system_lock);
2649
2650 rc = cyttsp4_wait_sysinfo_mode(cd);
2651 if (rc) {
2652 u8 buf[sizeof(ldr_err_app)];
2653 int rc1;
2654
2655 rc1 = cyttsp4_adap_read(cd->core->adap, CY_REG_BASE,
2656 (u8 *)buf, sizeof(ldr_err_app));
2657 if (!rc1 && !memcmp(buf, ldr_err_app, sizeof(ldr_err_app)))
2658 dev_err(cd->dev, "%s: Invalid touch application\n", __func__);
2659
2660 cyttsp4_start_wd_timer(cd);
2661
2662 /*
2663 * Unable to switch to SYSINFO mode,
2664 * Corrupted FW may cause crash, exit here.
2665 */
2666 dev_err(cd->dev, "%s: HGZ: Fail enter sysinfo mode\n",
2667 __func__); //hgz
2668 //return rc;// return 0 --> return rc
2669 return 0;
2670 }
2671
2672 /* read sysinfo data */
2673 dev_vdbg(cd->dev, "%s: get sysinfo regs..\n", __func__);
2674 rc = cyttsp4_get_sysinfo_regs(cd);
2675 if (rc < 0)
2676 dev_err(cd->dev, "%s: failed to get sysinfo regs rc=%d\n",
2677 __func__, rc);
2678 /* BEGIN PN:SPBB-1257 ,Deleted by l00184147, 2013/2/21*/
2679 //restart watchdog timer in another place for resovling ESD issue
2680 //cyttsp4_start_wd_timer(cd);
2681 /* END PN:SPBB-1257 ,Deleted by l00184147, 2013/2/21*/
2682 /* switch to operational mode */
2683 dev_vdbg(cd->dev, "%s: set mode cd->core=%p hst_mode=%02X mode=%d...\n",
2684 __func__, cd->core, CY_HST_OPERATE, CY_MODE_OPERATIONAL);
2685 set_mode(cd, CY_MODE_OPERATIONAL);
2686
2687 rc = cyttsp4_get_ttconfig_info(cd);
2688 if (rc < 0) {
2689 dev_err(cd->dev, "%s: failed to get ttconfig info rc=%d\n",
2690 __func__, rc);
2691 }
2692
2693 rc = cyttsp4_set_opmode(cd);
2694 if (rc < 0)
2695 dev_err(cd->dev, "%s: failed to set opmode rc=%d\n",
2696 __func__, rc);
2697
2698 /* attention startup */
2699 call_atten_cb(cd, CY_ATTEN_STARTUP, 0);
2700 /* BEGIN PN:SPBB-1257 ,Modified by l00184147, 2013/2/21*/
2701 /* restore to sleep if was suspended */
2702 mutex_lock(&cd->system_lock);
2703 if (cd->sleep_state == SS_SLEEP_ON) {
2704 cd->sleep_state = SS_SLEEP_OFF;
2705 mutex_unlock(&cd->system_lock);
2706 cyttsp4_core_sleep_(cd);
2707 goto exit_no_wd;
2708 }
2709 mutex_unlock(&cd->system_lock);
2710
2711 cyttsp4_start_wd_timer(cd);//restart watchdog timer here for resovling ESD issue
2712
2713 exit_no_wd:
2714 /* Required for signal to the TTHE */
2715 dev_info(cd->dev, "%s: cyttsp4_exit startup r=%d...\n", __func__, rc);
2716 /* END PN:SPBB-1257 ,Modified by l00184147, 2013/2/21*/
2717 return rc;
2718 }
2719
2720 static int cyttsp4_startup(struct cyttsp4_core_data *cd)
2721 {
2722 int rc;
2723
2724 mutex_lock(&cd->system_lock);
2725 cd->startup_state = STARTUP_RUNNING;
2726 mutex_unlock(&cd->system_lock);
2727
2728 rc = request_exclusive(cd, cd->core,
2729 CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT);
2730 if (rc < 0) {
2731 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
2732 __func__, cd->exclusive_dev, cd->core);
2733 goto exit;
2734 }
2735
2736 rc = cyttsp4_startup_(cd);
2737
2738 if (release_exclusive(cd, cd->core) < 0)
2739 /* Don't return fail code, mode is already changed. */
2740 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
2741 else
2742 dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
2743
2744 exit:
2745 mutex_lock(&cd->system_lock);
2746 cd->startup_state = STARTUP_NONE;
2747 mutex_unlock(&cd->system_lock);
2748
2749 wake_up(&cd->wait_q);
2750
2751 return rc;
2752 }
2753
2754 static void cyttsp4_startup_work_function(struct work_struct *work)
2755 {
2756 struct cyttsp4_core_data *cd = container_of(work,
2757 struct cyttsp4_core_data, startup_work);
2758 int rc;
2759
2760 /*
2761 * Force clear exclusive access
2762 * startup queue is called for abnormal case,
2763 * and when a this called access can be acquired in other context
2764 */
2765 mutex_lock(&cd->system_lock);
2766 if (cd->exclusive_dev != cd->core)
2767 cd->exclusive_dev = NULL;
2768 mutex_unlock(&cd->system_lock);
2769 rc = cyttsp4_startup(cd);
2770 if (rc < 0)
2771 dev_err(cd->dev, "%s: Fail queued startup r=%d\n",
2772 __func__, rc);
2773 }
2774
2775 static void cyttsp4_free_si_ptrs(struct cyttsp4_core_data *cd)
2776 {
2777 struct cyttsp4_sysinfo *si = &cd->sysinfo;
2778
2779 if (!si)
2780 return;
2781
2782 if (si->si_ptrs.cydata)
2783 kfree(si->si_ptrs.cydata->mfg_id);
2784 kfree(si->si_ptrs.cydata);
2785 kfree(si->si_ptrs.test);
2786 kfree(si->si_ptrs.pcfg);
2787 kfree(si->si_ptrs.opcfg);
2788 kfree(si->si_ptrs.ddata);
2789 kfree(si->si_ptrs.mdata);
2790 kfree(si->btn);
2791 kfree(si->xy_mode);
2792 kfree(si->xy_data);
2793 kfree(si->btn_rec_data);
2794 }
2795
2796 #if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_RUNTIME)
2797 static int cyttsp4_core_suspend(struct device *dev)
2798 {
2799 struct cyttsp4_core_data *cd = dev_get_drvdata(dev);
2800 int rc;
2801
2802 dev_dbg(dev, "%s\n", __func__);
2803
2804 rc = cyttsp4_core_sleep(cd);
2805 if (rc < 0) {
2806 dev_err(dev, "%s: Error on sleep\n", __func__);
2807 return -EAGAIN;
2808 }
2809 return 0;
2810 }
2811
2812 static int cyttsp4_core_resume(struct device *dev)
2813 {
2814 struct cyttsp4_core_data *cd = dev_get_drvdata(dev);
2815 int rc;
2816
2817 dev_dbg(dev, "%s\n", __func__);
2818
2819 rc = cyttsp4_core_wake(cd);
2820 if (rc < 0) {
2821 dev_err(dev, "%s: Error on wake\n", __func__);
2822 return -EAGAIN;
2823 }
2824
2825 return 0;
2826 }
2827 #endif
2828 /* BEGIN PN:SPBB-1257 ,Modified by l00184147, 2013/2/21*/
2829 //Don't use the pm operation with PM sleep and runtime sleep
2830 //static const struct dev_pm_ops cyttsp4_core_pm_ops = {
2831 // SET_SYSTEM_SLEEP_PM_OPS(cyttsp4_core_suspend, cyttsp4_core_resume)
2832 // SET_RUNTIME_PM_OPS(cyttsp4_core_suspend, cyttsp4_core_resume, NULL)
2833 //};
2834
2835 #ifdef CONFIG_HAS_EARLYSUSPEND
2836 /******************************************************************************
2837 Function:cyttsp4_core_early_suspend
2838 Description:called by Power management to make the touch sleep earlier
2839 ******************************************************************************/
2840 static void cyttsp4_core_early_suspend(struct early_suspend *h)
2841 {
2842 struct cyttsp4_core_data *cd =
2843 container_of(h, struct cyttsp4_core_data, es);
2844 int rc = 0;
2845
2846 dev_dbg(cd->dev, "%s\n", __func__);
2847
2848 rc = cyttsp4_core_sleep(cd);
2849 if (rc < 0) {
2850 dev_err(cd->dev, "%s: Error on sleep\n", __func__);
2851 }
2852 }
2853
2854 /******************************************************************************
2855 Function:cyttsp4_core_late_resume
2856 Description:called by Power management to make the touch resume later
2857 ******************************************************************************/
2858 static void cyttsp4_core_late_resume(struct early_suspend *h)
2859 {
2860 struct cyttsp4_core_data *cd =
2861 container_of(h, struct cyttsp4_core_data, es);
2862
2863 dev_dbg(cd->dev, "%s\n", __func__);
2864 int rc = 0;
2865
2866 rc = cyttsp4_core_wake(cd);
2867 if (rc < 0) {
2868 dev_err(cd->dev, "%s: Error on wake\n", __func__);
2869 }
2870 }
2871
2872 /******************************************************************************
2873 Function:cyttsp4_core_setup_early_suspend
2874 Description:regist early suspend for touch driver
2875 ******************************************************************************/
2876 void cyttsp4_core_setup_early_suspend(struct cyttsp4_core_data *cd)
2877 {
2878 cd->es.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
2879 cd->es.suspend = cyttsp4_core_early_suspend;
2880 cd->es.resume = cyttsp4_core_late_resume;
2881
2882 register_early_suspend(&cd->es);
2883 }
2884 #endif
2885 /* END PN:SPBB-1257 ,Modified by l00184147, 2013/2/21*/
2886 /*
2887 * Show Firmware version via sysfs
2888 */
2889 static ssize_t cyttsp4_ic_ver_show(struct device *dev,
2890 struct device_attribute *attr, char *buf)
2891 {
2892 struct cyttsp4_core_data *cd = dev_get_drvdata(dev);
2893 struct cyttsp4_cydata *cydata;
2894
2895 if (!cd->sysinfo.ready)
2896 return sprintf(buf, "Corrupted Touch application!\n");
2897
2898 cydata = cd->sysinfo.si_ptrs.cydata;
2899
2900 return sprintf(buf,
2901 "%s: 0x%02X 0x%02X\n"
2902 "%s: 0x%02X\n"
2903 "%s: 0x%02X\n"
2904 "%s: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n"
2905 "%s: 0x%04X\n"
2906 "%s: 0x%02X\n"
2907 "%s: 0x%02X\n",
2908 "TrueTouch Product ID", cydata->ttpidh, cydata->ttpidl,
2909 "Firmware Major Version", cydata->fw_ver_major,
2910 "Firmware Minor Version", cydata->fw_ver_minor,
2911 "Revision Control Number", cydata->revctrl[0],
2912 cydata->revctrl[1], cydata->revctrl[2], cydata->revctrl[3],
2913 cydata->revctrl[4], cydata->revctrl[5], cydata->revctrl[6],
2914 cydata->revctrl[7],
2915 "TrueTouch Config Version", cd->sysinfo.ttconfig.version,
2916 "Bootloader Major Version", cydata->blver_major,
2917 "Bootloader Minor Version", cydata->blver_minor);
2918 }
2919
2920 /*
2921 * Show TT Config version via sysfs
2922 */
2923 static ssize_t cyttsp4_ttconfig_ver_show(struct device *dev,
2924 struct device_attribute *attr, char *buf)
2925 {
2926 struct cyttsp4_core_data *cd = dev_get_drvdata(dev);
2927
2928 return sprintf(buf, "0x%04X\n", cd->sysinfo.ttconfig.version);
2929 }
2930
2931 /*
2932 * Show Low power mode via sysfs
2933 */
2934 static ssize_t cyttsp4_low_power_show(struct device *dev,
2935 struct device_attribute *attr, char *buf)
2936 {
2937 struct cyttsp4_core_data *cd = dev_get_drvdata(dev);
2938 bool enabled;
2939 int rc;
2940
2941 rc = cyttsp4_get_low_power(cd, &enabled);
2942 if (rc)
2943 return rc;
2944
2945 if (enabled)
2946 rc = snprintf(buf, CY_MAX_PRBUF_SIZE,
2947 "Low power is ENABLED\n");
2948 else
2949 rc = snprintf(buf, CY_MAX_PRBUF_SIZE,
2950 "Low power is DISABLED\n");
2951
2952 return rc;
2953 }
2954
2955 /*
2956 * Set Low power mode via sysfs
2957 */
2958 static ssize_t cyttsp4_low_power_store(struct device *dev,
2959 struct device_attribute *attr, const char *buf, size_t size)
2960 {
2961 struct cyttsp4_core_data *cd = dev_get_drvdata(dev);
2962 unsigned long value;
2963 int rc;
2964
2965 rc = kstrtoul(buf, 10, &value);
2966 if (rc < 0) {
2967 dev_err(dev, "%s: Invalid value\n", __func__);
2968 goto exit;
2969 }
2970
2971 switch (value) {
2972 case 0:
2973 rc = cyttsp4_set_low_power(cd, false);
2974 break;
2975 case 1:
2976 rc = cyttsp4_set_low_power(cd, true);
2977 break;
2978 default:
2979 dev_err(dev, "%s: Invalid value\n", __func__);
2980 rc = -EINVAL;
2981 }
2982
2983 exit:
2984 if (rc)
2985 return rc;
2986 return size;
2987 }
2988
2989 /*
2990 * Show Panel ID via sysfs
2991 */
2992 static ssize_t cyttsp4_panel_id_show(struct device *dev,
2993 struct device_attribute *attr, char *buf)
2994 {
2995 struct cyttsp4_core_data *cd = dev_get_drvdata(dev);
2996
2997 if (!cd->sysinfo.ready)
2998 return sprintf(buf, "SysInfo not ready!\n");
2999
3000 return sprintf(buf, "%d\n",
3001 GET_PANELID(cd->sysinfo.si_ptrs.pcfg->panel_info0));
3002 }
3003
3004 /*
3005 * Show Driver version via sysfs
3006 */
3007 static ssize_t cyttsp4_drv_ver_show(struct device *dev,
3008 struct device_attribute *attr, char *buf)
3009 {
3010 return snprintf(buf, CY_MAX_PRBUF_SIZE,
3011 "Driver: %s\nVersion: %s\nDate: %s\n",
3012 cy_driver_core_name, cy_driver_core_version,
3013 cy_driver_core_date);
3014 }
3015
3016 /*
3017 * HW reset via sysfs
3018 */
3019 static ssize_t cyttsp4_hw_reset_store(struct device *dev,
3020 struct device_attribute *attr, const char *buf, size_t size)
3021 {
3022 struct cyttsp4_core_data *cd = dev_get_drvdata(dev);
3023 int rc = 0;
3024
3025 rc = cyttsp4_startup(cd);
3026 if (rc < 0)
3027 dev_err(dev, "%s: HW reset failed r=%d\n",
3028 __func__, rc);
3029
3030 return size;
3031 }
3032
3033 /*
3034 * Show IRQ status via sysfs
3035 */
3036 /* static ssize_t cyttsp4_hw_irq_stat_show(struct device *dev, */
3037 /* struct device_attribute *attr, char *buf) */
3038 /* { */
3039 /* struct cyttsp4_core_data *cd = dev_get_drvdata(dev); */
3040 /* int retval; */
3041
3042 /* if (cd->pdata->irq_stat) { */
3043 /* retval = cd->pdata->irq_stat(cd->pdata, dev); */
3044 /* switch (retval) { */
3045 /* case 0: */
3046 /* return snprintf(buf, CY_MAX_PRBUF_SIZE, */
3047 /* "Interrupt line is LOW.\n"); */
3048 /* case 1: */
3049 /* return snprintf(buf, CY_MAX_PRBUF_SIZE, */
3050 /* "Interrupt line is HIGH.\n"); */
3051 /* default: */
3052 /* return snprintf(buf, CY_MAX_PRBUF_SIZE, */
3053 /* "Function irq_stat() returned %d.\n", retval); */
3054 /* } */
3055 /* } */
3056
3057 /* return snprintf(buf, CY_MAX_PRBUF_SIZE, */
3058 /* "Function irq_stat() undefined.\n"); */
3059 /* } */
3060
3061 /* /\* */
3062 /* * Show IRQ enable/disable status via sysfs */
3063 /* *\/ */
3064 /* static ssize_t cyttsp4_drv_irq_show(struct device *dev, */
3065 /* struct device_attribute *attr, char *buf) */
3066 /* { */
3067 /* struct cyttsp4_core_data *cd = dev_get_drvdata(dev); */
3068 /* ssize_t ret; */
3069
3070 /* mutex_lock(&cd->system_lock); */
3071 /* if (cd->irq_enabled) */
3072 /* ret = snprintf(buf, CY_MAX_PRBUF_SIZE, */
3073 /* "Driver interrupt is ENABLED\n"); */
3074 /* else */
3075 /* ret = snprintf(buf, CY_MAX_PRBUF_SIZE, */
3076 /* "Driver interrupt is DISABLED\n"); */
3077 /* mutex_unlock(&cd->system_lock); */
3078
3079 /* return ret; */
3080 /* } */
3081
3082 /*
3083 * Enable/disable IRQ via sysfs
3084 */
3085 /* static ssize_t cyttsp4_drv_irq_store(struct device *dev, */
3086 /* struct device_attribute *attr, const char *buf, size_t size) */
3087 /* { */
3088 /* struct cyttsp4_core_data *cd = dev_get_drvdata(dev); */
3089 /* unsigned long value; */
3090 /* int retval = 0; */
3091
3092 /* if (cd->irq < 0) { */
3093 /* dev_err(dev, "%s: Invalid irq\n", __func__); */
3094 /* goto cyttsp4_drv_irq_store_error_exit; */
3095 /* } */
3096
3097 /* retval = kstrtoul(buf, 10, &value); */
3098 /* if (retval < 0) { */
3099 /* dev_err(dev, "%s: Invalid value\n", __func__); */
3100 /* goto cyttsp4_drv_irq_store_error_exit; */
3101 /* } */
3102
3103 /* mutex_lock(&cd->system_lock); */
3104 /* switch (value) { */
3105 /* case 0: */
3106 /* if (cd->irq_enabled) { */
3107 /* cd->irq_enabled = false; */
3108 /* /\* Disable IRQ *\/ */
3109 /* disable_irq_nosync(cd->irq); */
3110 /* dev_info(dev, "%s: Driver IRQ now disabled\n", */
3111 /* __func__); */
3112 /* } else */
3113 /* dev_info(dev, "%s: Driver IRQ already disabled\n", */
3114 /* __func__); */
3115 /* break; */
3116
3117 /* case 1: */
3118 /* if (cd->irq_enabled == false) { */
3119 /* cd->irq_enabled = true; */
3120 /* /\* Enable IRQ *\/ */
3121 /* enable_irq(cd->irq); */
3122 /* dev_info(dev, "%s: Driver IRQ now enabled\n", */
3123 /* __func__); */
3124 /* } else */
3125 /* dev_info(dev, "%s: Driver IRQ already enabled\n", */
3126 /* __func__); */
3127 /* break; */
3128
3129 /* default: */
3130 /* dev_err(dev, "%s: Invalid value\n", __func__); */
3131 /* } */
3132 /* mutex_unlock(&(cd->system_lock)); */
3133
3134 /* cyttsp4_drv_irq_store_error_exit: */
3135
3136 /* return size; */
3137 /* } */
3138
3139 /*
3140 * Debugging options via sysfs
3141 */
3142 static ssize_t cyttsp4_drv_debug_store(struct device *dev,
3143 struct device_attribute *attr, const char *buf, size_t size)
3144 {
3145 struct cyttsp4_core_data *cd = dev_get_drvdata(dev);
3146 unsigned long value = 0;
3147 int rc = 0;
3148 int rc1 = 0;
3149
3150 rc = kstrtoul(buf, 10, &value);
3151 if (rc < 0) {
3152 dev_err(dev, "%s: Invalid value\n", __func__);
3153 goto cyttsp4_drv_debug_store_exit;
3154 }
3155
3156 switch (value) {
3157 case CY_DBG_SUSPEND:
3158 dev_info(dev, "%s: SUSPEND (cd=%p)\n", __func__, cd);
3159 rc = cyttsp4_core_sleep(cd);
3160 printk("cyttsp4_drv_debug_store, rc1 = %d\n", rc1);
3161 if (rc)
3162 dev_err(dev, "%s: Suspend failed rc=%d\n",
3163 __func__, rc);
3164 else
3165 dev_info(dev, "%s: Suspend succeeded\n", __func__);
3166 break;
3167
3168 case CY_DBG_RESUME:
3169 dev_info(dev, "%s: RESUME (cd=%p)\n", __func__, cd);
3170 rc = cyttsp4_core_wake(cd);
3171 if (rc)
3172 dev_err(dev, "%s: Resume failed rc=%d\n",
3173 __func__, rc);
3174 else
3175 dev_info(dev, "%s: Resume succeeded\n", __func__);
3176 break;
3177 case CY_DBG_SOFT_RESET:
3178 dev_info(dev, "%s: SOFT RESET (cd=%p)\n", __func__, cd);
3179 rc = cyttsp4_hw_soft_reset(cd);
3180 break;
3181 case CY_DBG_RESET:
3182 dev_info(dev, "%s: HARD RESET (cd=%p)\n", __func__, cd);
3183 rc = cyttsp4_hw_hard_reset(cd);
3184 break;
3185 default:
3186 dev_err(dev, "%s: Invalid value\n", __func__);
3187 }
3188
3189 cyttsp4_drv_debug_store_exit:
3190 return size;
3191 }
3192
3193 /*
3194 * Show system status on deep sleep status via sysfs
3195 */
3196 static ssize_t cyttsp4_sleep_status_show(struct device *dev,
3197 struct device_attribute *attr, char *buf)
3198 {
3199 struct cyttsp4_core_data *cd = dev_get_drvdata(dev);
3200 ssize_t ret;
3201
3202 mutex_lock(&cd->system_lock);
3203 if (cd->sleep_state == SS_SLEEP_ON)
3204 ret = snprintf(buf, CY_MAX_PRBUF_SIZE,
3205 "Deep Sleep is ENABLED\n");
3206 else
3207 ret = snprintf(buf, CY_MAX_PRBUF_SIZE,
3208 "Deep Sleep is DISABLED\n");
3209 mutex_unlock(&cd->system_lock);
3210
3211 return ret;
3212 }
3213
3214 static ssize_t cyttsp4_signal_disparity_show(struct device *dev,
3215 struct device_attribute *attr, char *buf)
3216 {
3217 struct cyttsp4_core_data *cd = dev_get_drvdata(dev);
3218 u8 cmd_buf[CY_CMD_OP_GET_PARA_CMD_SZ];
3219 u8 return_buf[CY_CMD_OP_GET_PARA_RET_SZ];
3220 int rc;
3221
3222 cmd_buf[0] = CY_CMD_OP_GET_PARA;
3223 cmd_buf[1] = CY_OP_PARA_SCAN_TYPE;
3224
3225 pm_runtime_get_sync(dev);
3226
3227 rc = request_exclusive(cd, cd->core, CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT);
3228 if (rc < 0) {
3229 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
3230 __func__, cd->exclusive_dev, cd->core);
3231 goto exit_put;
3232 }
3233
3234 rc = cyttsp4_exec_cmd(cd, CY_MODE_OPERATIONAL,
3235 cmd_buf, sizeof(cmd_buf),
3236 return_buf, sizeof(return_buf),
3237 CY_COMMAND_COMPLETE_TIMEOUT);
3238 if (rc < 0) {
3239 dev_err(dev, "%s: exec cmd error.\n", __func__);
3240 goto exit_release;
3241 }
3242
3243 dev_dbg(dev, "%s: return_buf=0x%x,0x%x,0x%x\n", __func__,
3244 return_buf[0], return_buf[1], return_buf[2]);
3245
3246 if (return_buf[0] != CY_OP_PARA_SCAN_TYPE) {
3247 dev_err(dev, "%s: return data error.\n", __func__);
3248 rc = -EINVAL;
3249 goto exit_release;
3250 }
3251
3252 rc = return_buf[2];
3253
3254 exit_release:
3255 if (release_exclusive(cd, cd->core) < 0)
3256 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
3257 exit_put:
3258 pm_runtime_put(dev);
3259 return scnprintf(buf, CY_MAX_PRBUF_SIZE, "%d\n", rc);
3260 }
3261
3262 static ssize_t cyttsp4_signal_disparity_store(struct device *dev,
3263 struct device_attribute *attr, const char *buf, size_t size)
3264 {
3265 struct cyttsp4_core_data *cd = dev_get_drvdata(dev);
3266 unsigned long disparity_val;
3267 u8 cmd_buf[CY_CMD_OP_SET_PARA_CMD_SZ];
3268 u8 return_buf[CY_CMD_OP_SET_PARA_RET_SZ];
3269 u8 scan_type;
3270 int rc;
3271
3272 rc = kstrtoul(buf, 10, &disparity_val);
3273 if (rc < 0) {
3274 dev_err(dev, "%s: Invalid value.\n", __func__);
3275 goto exit;
3276 }
3277
3278 mutex_lock(&cd->system_lock);
3279 switch (disparity_val) {
3280 case CY_SIGNAL_DISPARITY_NONE:
3281 cd->opmode = OPMODE_FINGER;
3282 scan_type = CY_OP_PARA_SCAN_TYPE_NORMAL;
3283 break;
3284 case CY_SIGNAL_DISPARITY_SENSITIVITY:
3285 cd->opmode = OPMODE_GLOVE;
3286 scan_type = CY_OP_PARA_SCAN_TYPE_APAMC_MASK |
3287 CY_OP_PARA_SCAN_TYPE_GLOVE_MASK;
3288 break;
3289 default:
3290 mutex_unlock(&cd->system_lock);
3291 dev_err(dev, "%s: Invalid signal disparity=%d\n", __func__,
3292 (int)disparity_val);
3293 rc = -EINVAL;
3294 goto exit;
3295 }
3296 mutex_unlock(&cd->system_lock);
3297
3298 cmd_buf[0] = CY_CMD_OP_SET_PARA;
3299 cmd_buf[1] = CY_OP_PARA_SCAN_TYPE;
3300 cmd_buf[2] = CY_OP_PARA_SCAN_TYPE_SZ;
3301 cmd_buf[3] = scan_type;
3302
3303 pm_runtime_get_sync(dev);
3304
3305 rc = request_exclusive(cd, cd->core, CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT);
3306 if (rc < 0) {
3307 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
3308 __func__, cd->exclusive_dev, cd->core);
3309 goto exit_put;
3310 }
3311
3312 rc = cyttsp4_exec_cmd(cd, CY_MODE_OPERATIONAL,
3313 cmd_buf, sizeof(cmd_buf),
3314 return_buf, sizeof(return_buf),
3315 CY_COMMAND_COMPLETE_TIMEOUT);
3316 if (rc < 0) {
3317 dev_err(dev, "%s: exec cmd error.\n", __func__);
3318 goto exit_release;
3319 }
3320
3321 rc = size;
3322 dev_dbg(dev, "%s: return_buf=0x%x,0x%x\n", __func__,
3323 return_buf[0], return_buf[1]);
3324 exit_release:
3325 if (release_exclusive(cd, cd->core) < 0)
3326 /* Don't return fail code, mode is already changed. */
3327 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
3328 else
3329 dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
3330 exit_put:
3331 pm_runtime_put(dev);
3332 exit:
3333 return rc;
3334 }
3335
3336 /*
3337 * Show finger threshold value via sysfs
3338 */
3339 static ssize_t cyttsp4_finger_threshold_show(struct device *dev,
3340 struct device_attribute *attr, char *buf)
3341 {
3342 struct cyttsp4_core_data *cd = dev_get_drvdata(dev);
3343 u8 cmd_buf[CY_CMD_OP_GET_PARA_CMD_SZ];
3344 u8 return_buf[CY_CMD_OP_GET_PARA_RET_SZ];
3345 u8 finger_threshold_h, finger_threshold_l;
3346 int rc;
3347
3348 cmd_buf[0] = CY_CMD_OP_GET_PARA;
3349 cmd_buf[1] = CY_OP_PARA_FINGER_THRESHOLD;
3350
3351 pm_runtime_get_sync(dev);
3352 rc = request_exclusive(cd, cd->core, CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT);
3353 if (rc < 0) {
3354 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
3355 __func__, cd->exclusive_dev, cd->core);
3356 goto exit_put;
3357 }
3358
3359 rc = cyttsp4_exec_cmd(cd, CY_MODE_OPERATIONAL,
3360 cmd_buf, sizeof(cmd_buf),
3361 return_buf, sizeof(return_buf),
3362 CY_COMMAND_COMPLETE_TIMEOUT);
3363 if (rc < 0) {
3364 dev_err(dev, "%s: exec cmd error.\n", __func__);
3365 goto exit_release;
3366 }
3367
3368 dev_dbg(dev, "%s: return_buf=0x%x,0x%x,0x%x,0x%x\n", __func__,
3369 return_buf[0], return_buf[1], return_buf[2],
3370 return_buf[3]);
3371
3372 if (return_buf[0] != CY_OP_PARA_FINGER_THRESHOLD) {
3373 dev_err(dev, "%s: return data error.\n", __func__);
3374 rc = -EINVAL;
3375 goto exit_release;
3376 }
3377
3378 finger_threshold_h = return_buf[2];
3379 finger_threshold_l = return_buf[3];
3380 rc = merge_bytes(finger_threshold_h, finger_threshold_l);
3381
3382 exit_release:
3383 if (release_exclusive(cd, cd->core) < 0)
3384 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
3385 exit_put:
3386 pm_runtime_put(dev);
3387 return scnprintf(buf, CY_MAX_PRBUF_SIZE, "%d\n", rc);
3388 }
3389
3390 /*
3391 * change finger threshold via sysfs
3392 */
3393 static ssize_t cyttsp4_finger_threshold_store(struct device *dev,
3394 struct device_attribute *attr, const char *buf, size_t size)
3395 {
3396 struct cyttsp4_core_data *cd = dev_get_drvdata(dev);
3397 unsigned long threshold_val;
3398 u8 cmd_buf[CY_CMD_OP_SET_PARA_CMD_SZ];
3399 u8 return_buf[CY_CMD_OP_SET_PARA_RET_SZ];
3400 int rc;
3401
3402 rc = kstrtoul(buf, 10, &threshold_val);
3403 if (rc < 0) {
3404 dev_err(dev, "%s: Invalid value.\n", __func__);
3405 goto exit;
3406 }
3407
3408 if ((threshold_val < CY_OP_PARA_FINGER_THRESHOLD_MIN_VAL) ||
3409 (threshold_val > CY_OP_PARA_FINGER_THRESHOLD_MAX_VAL)) {
3410 dev_err(dev, "%s: Invalid value, value=%d.\n", __func__,
3411 (int)threshold_val);
3412 rc = -EINVAL;
3413 goto exit;
3414 }
3415
3416 cmd_buf[0] = CY_CMD_OP_SET_PARA;
3417 cmd_buf[1] = CY_OP_PARA_FINGER_THRESHOLD;
3418 cmd_buf[2] = CY_OP_PARA_FINGER_THRESHOLD_SZ;
3419 cmd_buf[3] = (u8)((threshold_val >> 8) & 0xFF);
3420 cmd_buf[4] = (u8)(threshold_val & 0xFF);
3421
3422 pm_runtime_get_sync(dev);
3423 rc = request_exclusive(cd, cd->core, CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT);
3424 if (rc < 0) {
3425 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
3426 __func__, cd->exclusive_dev, cd->core);
3427 goto exit_put;
3428 }
3429
3430 rc = cyttsp4_exec_cmd(cd, CY_MODE_OPERATIONAL,
3431 cmd_buf, sizeof(cmd_buf),
3432 return_buf, sizeof(return_buf),
3433 CY_COMMAND_COMPLETE_TIMEOUT);
3434 if (rc < 0) {
3435 dev_err(dev, "%s: exec cmd error.\n", __func__);
3436 goto exit_release;
3437 }
3438
3439 rc = size;
3440 dev_dbg(dev, "%s: return_buf=0x%x,0x%x\n", __func__,
3441 return_buf[0], return_buf[1]);
3442
3443 exit_release:
3444 if (release_exclusive(cd, cd->core) < 0)
3445 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
3446 exit_put:
3447 pm_runtime_put(dev);
3448 exit:
3449 return rc;
3450 }
3451
3452
3453 static struct device_attribute attributes[] = {
3454 __ATTR(ic_ver, S_IRUGO, cyttsp4_ic_ver_show, NULL),
3455 __ATTR(ttconfig_ver, S_IRUGO, cyttsp4_ttconfig_ver_show, NULL),
3456 __ATTR(low_power, S_IRUSR | S_IWUSR, cyttsp4_low_power_show,
3457 cyttsp4_low_power_store),
3458 __ATTR(panel_id, S_IRUSR, cyttsp4_panel_id_show, NULL),
3459 __ATTR(drv_ver, S_IRUGO, cyttsp4_drv_ver_show, NULL),
3460 __ATTR(hw_reset, S_IWUSR, NULL, cyttsp4_hw_reset_store),
3461 /* __ATTR(hw_irq_stat, S_IRUSR, cyttsp4_hw_irq_stat_show, NULL), */
3462 /* __ATTR(drv_irq, S_IRUSR | S_IWUSR, cyttsp4_drv_irq_show, */
3463 /* cyttsp4_drv_irq_store), */
3464 __ATTR(drv_debug, S_IWUSR, NULL, cyttsp4_drv_debug_store),
3465 __ATTR(sleep_status, S_IRUSR, cyttsp4_sleep_status_show, NULL),
3466 };
3467
3468 static int add_sysfs_interfaces(struct device *dev)
3469 {
3470 int i;
3471
3472 for (i = 0; i < ARRAY_SIZE(attributes); i++)
3473 if (device_create_file(dev, attributes + i))
3474 goto undo;
3475 return 0;
3476 undo:
3477 for (; i >= 0 ; i--)
3478 device_remove_file(dev, attributes + i);
3479 dev_err(dev, "%s: failed to create sysfs interface\n", __func__);
3480 return -ENODEV;
3481 }
3482
3483 static void remove_sysfs_interfaces(struct device *dev)
3484 {
3485 int i;
3486 for (i = 0; i < ARRAY_SIZE(attributes); i++)
3487 device_remove_file(dev, attributes + i);
3488 }
3489
3490 /* BEGIN PN:DTS2013033005872 ,Modified by F00184246, 2013/3/30*/
3491 /* BEGIN PN: SPBB-1264 ,Modified by l00184147, 2013/2/27*/
3492 static struct device_attribute sensitivity_attributes[] = {
3493 __ATTR(signal_disparity, S_IRUGO | S_IWUSR | S_IWGRP,
3494 cyttsp4_signal_disparity_show, cyttsp4_signal_disparity_store),
3495 __ATTR(finger_threshold, S_IRUGO | S_IWUSR |S_IWGRP,
3496 cyttsp4_finger_threshold_show, cyttsp4_finger_threshold_store),
3497 };
3498 /* END PN: SPBB-1264 ,Modified by l00184147, 2013/2/27*/
3499 /* END PN:DTS2013033005872 ,Modified by F00184246, 2013/3/30*/
3500
3501 static int add_sensitivity_sysfs_interfaces(struct device *dev)
3502 {
3503 int i;
3504
3505 for (i = 0; i < ARRAY_SIZE(sensitivity_attributes); i++)
3506 if (device_create_file(dev, sensitivity_attributes + i))
3507 goto undo;
3508 return 0;
3509 undo:
3510 for (i--; i >= 0 ; i--)
3511 device_remove_file(dev, sensitivity_attributes + i);
3512 dev_err(dev, "%s: failed to create sysfs interface\n", __func__);
3513 return -ENODEV;
3514 }
3515
3516 static void remove_sensitivity_sysfs_interfaces(struct device *dev)
3517 {
3518 int i;
3519 for (i = 0; i < ARRAY_SIZE(sensitivity_attributes); i++)
3520 device_remove_file(dev, sensitivity_attributes + i);
3521 }
3522
3523 static DECLARE_WAIT_QUEUE_HEAD(waiter);
3524 static int cyttsp4_event_flag = 0;
3525 static int tpd_halt=0;
3526 static struct task_struct *cyttsp4_event_thread = NULL;
3527
3528 void eint_interrupt_handler(void) {
3529 cyttsp4_event_flag=1;
3530 wake_up_interruptible(&waiter);
3531 }
3532
3533 static int cyttsp4_event_handler(void *unused) {
3534 int signal;
3535 struct cyttsp4_core_data *cd = (struct cyttsp4_core_data *)unused;
3536 struct sched_param param = { .sched_priority = RTPM_PRIO_TPD };
3537
3538 sched_setscheduler(current, SCHED_RR, &param);
3539
3540 do {
3541 set_current_state(TASK_INTERRUPTIBLE);
3542 signal = wait_event_interruptible(waiter, cyttsp4_event_flag != 0);
3543
3544 cyttsp4_event_flag = 0;
3545 set_current_state(TASK_RUNNING);
3546 if (!signal) {
3547 cyttsp4_irq(0, cd);
3548 }
3549 cyttsp4_mtk_gpio_interrupt_enable();
3550 } while (!kthread_should_stop());
3551 return 0;
3552 }
3553
3554 static int cyttsp4_core_probe(struct cyttsp4_core *core)
3555 {
3556 struct cyttsp4_core_data *cd;
3557 struct device *dev = &core->dev;
3558 struct cyttsp4_core_platform_data *pdata = dev_get_platdata(dev);
3559 enum cyttsp4_atten_type type;
3560 unsigned long irq_flags;
3561 int rc = 0;
3562 int err=0;
3563
3564 dev_info(dev, "%s: startup\n", __func__);
3565 dev_dbg(dev, "%s: debug on\n", __func__);
3566 dev_vdbg(dev, "%s: verbose debug on\n", __func__);
3567
3568 if (pdata == NULL) {
3569 dev_err(dev, "%s: Missing platform data\n", __func__);
3570 rc = -ENODEV;
3571 goto error_no_pdata;
3572 }
3573
3574 /* get context and debug print buffers */
3575 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
3576 if (cd == NULL) {
3577 dev_err(dev, "%s: Error, kzalloc\n", __func__);
3578 rc = -ENOMEM;
3579 goto error_alloc_data_failed;
3580 }
3581
3582 /* point to core device and init lists */
3583 cd->core = core;
3584 mutex_init(&cd->system_lock);
3585 mutex_init(&cd->adap_lock);
3586 for (type = 0; type < CY_ATTEN_NUM_ATTEN; type++)
3587 INIT_LIST_HEAD(&cd->atten_list[type]);
3588 init_waitqueue_head(&cd->wait_q);
3589 init_waitqueue_head(&cd->sleep_q);
3590 cd->startup_work_q = create_singlethread_workqueue("startup_work_q");
3591 if (cd->startup_work_q == NULL) {
3592 dev_err(dev, "%s: No memory for %s\n", __func__,
3593 "startup_work_q");
3594 goto error_init;
3595 }
3596
3597 dev_dbg(dev, "%s: initialize core data\n", __func__);
3598 spin_lock_init(&cd->spinlock);
3599 cd->dev = dev;
3600 cd->pdata = pdata;
3601 #ifdef HUAWEI_SET_FINGER_MODE_BY_DEFAULT
3602 /* Initialize with Finger mode */
3603 cd->opmode = OPMODE_FINGER;
3604 #endif
3605 /* BEGIN PN:DTS2013051404084 ,Added by f00184246, 2013/5/14*/
3606 /* init cd->startup_work before intertupt*/
3607 INIT_WORK(&cd->startup_work, cyttsp4_startup_work_function);
3608 /* BEGIN PN:DTS2013051404084 ,Added by f00184246, 2013/5/14*/
3609 #ifdef MTK
3610 cyttsp4_event_thread = kthread_run(cyttsp4_event_handler, cd, "cyttsp4_event_handler");
3611 if (IS_ERR(cyttsp4_event_thread)) {
3612 err = PTR_ERR(cyttsp4_event_thread);
3613 }
3614 cyttsp4_mtk_gpio_interrupt_register();
3615 cyttsp4_mtk_gpio_interrupt_enable();
3616 cd->irq = -1;
3617 #else
3618 /* cd->irq = gpio_to_irq(pdata->irq_gpio); */
3619 /* cd->irq_enabled = true; */
3620 /* if (cd->irq < 0) { */
3621 /* rc = -EINVAL; */
3622 /* goto error_gpio_irq; */
3623 /* } */
3624 #endif
3625 dev_set_drvdata(dev, cd);
3626
3627 if (cd->pdata->init) {
3628 dev_info(cd->dev, "%s: Init HW\n", __func__);
3629 rc = cd->pdata->init(cd->pdata, 1, cd->dev);
3630 } else {
3631 dev_info(cd->dev, "%s: No HW INIT function\n", __func__);
3632 rc = 0;
3633 }
3634 if (rc < 0)
3635 dev_err(cd->dev, "%s: HW Init fail r=%d\n", __func__, rc);
3636
3637 dev_info(cd->dev, "%s: check cypress device exit or not\n", __func__);
3638 rc = cyttsp4_reset_checkout(cd);
3639 if(rc < 0) {
3640 dev_err(cd->dev, "%s: there is no cypress device!!! rc=%d\n", __func__, rc);
3641 goto error_request_irq;
3642 }
3643 /* BEGIN PN:DTS2013051404084 ,Deleted by f00184246, 2013/5/14*/
3644 //INIT_WORK(&cd->startup_work, cyttsp4_startup_work_function);
3645 /* END PN:DTS2013051404084 ,Deleted by f00184246, 2013/5/14*/
3646 #ifndef MTK
3647 /* dev_dbg(dev, "%s: initialize threaded irq=%d\n", __func__, cd->irq); */
3648 /* if (cd->pdata->level_irq_udelay > 0) */
3649 /* /\* use level triggered interrupts *\/ */
3650 /* irq_flags = IRQF_TRIGGER_LOW | IRQF_ONESHOT; */
3651 /* else */
3652 /* /\* use edge triggered interrupts *\/ */
3653 /* irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT; */
3654
3655 /* rc = request_threaded_irq(cd->irq, NULL, cyttsp4_irq, irq_flags, */
3656 /* dev_name(dev), cd); */
3657 /* if (rc < 0) { */
3658 /* dev_err(dev, "%s: Error, could not request irq\n", __func__); */
3659 /* goto error_request_irq; */
3660 /* } */
3661 #endif
3662
3663 #ifdef CYTTSP4_WATCHDOG_NULL_CMD
3664 INIT_WORK(&cd->watchdog_work, cyttsp4_watchdog_work_null);
3665 #else
3666 INIT_WORK(&cd->watchdog_work, cyttsp4_watchdog_work);
3667 #endif
3668 setup_timer(&cd->watchdog_timer, cyttsp4_watchdog_timer,
3669 (unsigned long)cd);
3670
3671 dev_dbg(dev, "%s: add sysfs interfaces\n", __func__);
3672 rc = add_sysfs_interfaces(dev);
3673 if (rc < 0) {
3674 dev_err(dev, "%s: Error, fail sysfs init\n", __func__);
3675 goto error_attr_create;
3676 }
3677
3678 if (cd->pdata->use_configure_sensitivity) {
3679 rc = add_sensitivity_sysfs_interfaces(dev);
3680 if (rc < 0) {
3681 dev_err(dev, "%s: Error, fail sensitivity sysfs init\n",
3682 __func__);
3683 goto error_sens_attr_create;
3684 }
3685 }
3686
3687 pm_runtime_enable(dev);
3688
3689 /*
3690 * call startup directly to ensure that the device
3691 * is tested before leaving the probe
3692 */
3693 dev_dbg(dev, "%s: call startup\n", __func__);
3694
3695 pm_runtime_get_sync(dev);
3696 rc = cyttsp4_startup(cd);
3697 pm_runtime_put(dev);
3698
3699 if (rc < 0) {
3700 dev_err(cd->dev, "%s: Fail initial startup r=%d\n",
3701 __func__, rc);
3702 goto error_startup;
3703 }
3704 /* BEGIN PN:SPBB-1257 ,Added by l00184147, 2013/2/21*/
3705 #ifdef CONFIG_HAS_EARLYSUSPEND
3706 cyttsp4_core_setup_early_suspend(cd);
3707 #endif
3708 /* END PN:SPBB-1257 ,Added by l00184147, 2013/2/21*/
3709 dev_info(dev, "%s: ok\n", __func__);
3710 rc = 0;
3711 goto no_error;
3712
3713 error_startup:
3714 /* BEGIN PN:DTS2013062405322 ,Modified by l00184147, 2013/6/24*/
3715 cyttsp4_stop_wd_timer(cd);
3716 /* END PN:DTS2013062405322 ,Modified by l00184147, 2013/6/24*/
3717 pm_runtime_disable(dev);
3718 cyttsp4_free_si_ptrs(cd);
3719 error_sens_attr_create:
3720 remove_sysfs_interfaces(dev);
3721 error_attr_create:
3722 /* if (cd->irq >= 0) */
3723 /* free_irq(cd->irq, cd); */
3724 error_request_irq:
3725 //error_gpio_irq:
3726 destroy_workqueue(cd->startup_work_q);
3727 if (pdata->init)
3728 pdata->init(pdata, 0, dev);
3729 error_init:
3730 dev_set_drvdata(dev, NULL);
3731 kfree(cd);
3732 error_alloc_data_failed:
3733 error_no_pdata:
3734 dev_err(dev, "%s failed.\n", __func__);
3735 return -ENOMEM; //hgz
3736 no_error:
3737 return rc;
3738 }
3739
3740 static int cyttsp4_core_release(struct cyttsp4_core *core)
3741 {
3742 struct device *dev = &core->dev;
3743 struct cyttsp4_core_data *cd = dev_get_drvdata(dev);
3744
3745 dev_dbg(dev, "%s\n", __func__);
3746 /* BEGIN PN:SPBB-1257 ,Added by l00184147, 2013/2/21*/
3747 #ifdef CONFIG_HAS_EARLYSUSPEND
3748 unregister_early_suspend(&cd->es);
3749 #endif
3750 /* END PN:SPBB-1257 ,Added by l00184147, 2013/2/21*/
3751 /*
3752 * Suspend the device before freeing the startup_work and stopping
3753 * the watchdog since sleep function restarts watchdog on failure
3754 */
3755 pm_runtime_suspend(dev);
3756 pm_runtime_disable(dev);
3757
3758 cancel_work_sync(&cd->startup_work);
3759 destroy_workqueue(cd->startup_work_q);
3760
3761 cyttsp4_stop_wd_timer(cd);
3762
3763 remove_sysfs_interfaces(dev);
3764 /* if (cd->irq >= 0) */
3765 /* free_irq(cd->irq, cd); */
3766 if (cd->pdata->init)
3767 cd->pdata->init(cd->pdata, 0, dev);
3768 dev_set_drvdata(dev, NULL);
3769 cyttsp4_free_si_ptrs(cd);
3770 kfree(cd);
3771 return 0;
3772 }
3773
3774 struct cyttsp4_core_driver cyttsp4_core_driver = {
3775 .probe = cyttsp4_core_probe,
3776 .remove = cyttsp4_core_release,
3777 .subscribe_attention = cyttsp4_subscribe_attention_,
3778 .unsubscribe_attention = cyttsp4_unsubscribe_attention_,
3779 .request_exclusive = cyttsp4_request_exclusive_,
3780 .release_exclusive = cyttsp4_release_exclusive_,
3781 .request_reset = cyttsp4_request_reset_,
3782 .request_restart = cyttsp4_request_restart_,
3783 .request_set_mode = cyttsp4_request_set_mode_,
3784 .request_sysinfo = cyttsp4_request_sysinfo_,
3785 .request_loader_pdata = cyttsp4_request_loader_pdata_,
3786 .request_handshake = cyttsp4_request_handshake_,
3787 .request_exec_cmd = cyttsp4_request_exec_cmd_,
3788 .request_stop_wd = cyttsp4_request_stop_wd_,
3789 .request_toggle_lowpower = cyttsp4_request_toggle_lowpower_,
3790 .request_write_config = cyttsp4_request_write_config_,
3791 .write = cyttsp4_write_,
3792 .read = cyttsp4_read_,
3793 .driver = {
3794 .name = CYTTSP4_CORE_NAME,
3795 .bus = &cyttsp4_bus_type,
3796 .owner = THIS_MODULE,
3797 /* BEGIN PN:SPBB-1257 ,Deleted by l00184147, 2013/2/21*/
3798 //no longer to use pm operation
3799 //.pm = &cyttsp4_core_pm_ops,
3800 /* END PN:SPBB-1257 ,Deleted by l00184147, 2013/2/21*/
3801 },
3802 };
3803
3804 static int __init cyttsp4_core_init(void)
3805 {
3806 int rc = 0;
3807
3808 rc = cyttsp4_register_core_driver(&cyttsp4_core_driver);
3809 pr_info("%s: Cypress TTSP v4 core driver (Built %s) rc=%d\n",
3810 __func__, CY_DRIVER_DATE, rc);
3811 return rc;
3812 }
3813 module_init(cyttsp4_core_init);
3814
3815 static void __exit cyttsp4_core_exit(void)
3816 {
3817 cyttsp4_unregister_core_driver(&cyttsp4_core_driver);
3818 pr_info("%s: module exit\n", __func__);
3819 }
3820 module_exit(cyttsp4_core_exit);
3821
3822 MODULE_LICENSE("GPL");
3823 MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard touchscreen core driver");
3824 MODULE_AUTHOR("Aleksej Makarov <aleksej.makarov@sonyericsson.com>");
3825 /* END PN:SPBB-1218 ,Added by l00184147, 2012/12/20*/
3826 /* END PN:DTS2013011401860 ,Modified by l00184147, 2013/1/14*/
3827 /* END PN:DTS2013012601133 ,Modified by l00184147, 2013/1/26*/
3828 /* END PN:DTS2013051703879 ,Added by l00184147, 2013/5/17*/