1 /* drivers/input/touchscreen/gt9xx.c
2 *
3 * 2010 - 2013 Goodix Technology.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be a reference
11 * to you, when you are integrating the GOODiX's CTP IC into your system,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * Version: 2.2
17 * Authors: andrew@goodix.com, meta@goodix.com
18 * Release Date: 2014/01/14
19 * Revision record:
20 * V1.0:
21 * first Release. By Andrew, 2012/08/31
22 * V1.2:
23 * modify gtp_reset_guitar,slot report,tracking_id & 0x0F. By Andrew, 2012/10/15
24 * V1.4:
25 * modify gt9xx_update.c. By Andrew, 2012/12/12
26 * V1.6:
27 * 1. new heartbeat/esd_protect mechanism(add external watchdog)
28 * 2. doze mode, sliding wakeup
29 * 3. 3 more cfg_group(GT9 Sensor_ID: 0~5)
30 * 3. config length verification
31 * 4. names & comments
32 * By Meta, 2013/03/11
33 * V1.8:
34 * 1. pen/stylus identification
35 * 2. read double check & fixed config support
36 * 3. new esd & slide wakeup optimization
37 * By Meta, 2013/06/08
38 * V2.0:
39 * 1. compatible with GT9XXF
40 * 2. send config after resume
41 * By Meta, 2013/08/06
42 * V2.2:
43 * 1. gt9xx_config for debug
44 * 2. gesture wakeup
45 * 3. pen separate input device, active-pen button support
46 * 4. coordinates & keys optimization
47 * By Meta, 2014/01/14
48 */
49
50 #include <linux/irq.h>
51 #include "gt9xx.h"
52 #include "gt9xx_cfg.h"
53 #include <linux/regulator/consumer.h>
54
55 #if GTP_ICS_SLOT_REPORT
56 #include <linux/input/mt.h>
57 #endif
58
59 static u8 m89or101 = TRUE;
60 static u8 bgt911 = FALSE;
61 static u8 bgt9110 = FALSE;
62 static u8 bgt9111 = FALSE;
63 static u8 bgt970 = FALSE;
64 static u8 bgt910 = FALSE;
65 static u8 gtp_change_x2y = TRUE;
66 static u8 gtp_x_reverse = FALSE;
67 static u8 gtp_y_reverse = TRUE;
68
69 static const char *goodix_ts_name = "goodix-ts";
70 static struct workqueue_struct *goodix_wq;
71 struct i2c_client * i2c_connect_client = NULL;
72 u8 config[GTP_CONFIG_MAX_LENGTH + GTP_ADDR_LENGTH]
73 = {GTP_REG_CONFIG_DATA >> 8, GTP_REG_CONFIG_DATA & 0xff};
74
75 #if GTP_HAVE_TOUCH_KEY
76 static const u16 touch_key_array[] = GTP_KEY_TAB;
77 #define GTP_MAX_KEY_NUM (sizeof(touch_key_array)/sizeof(touch_key_array[0]))
78
79 #if GTP_DEBUG_ON
80 static const int key_codes[] = {KEY_HOME, KEY_BACK, KEY_MENU, KEY_SEARCH};
81 static const char *key_names[] = {"Key_Home", "Key_Back", "Key_Menu", "Key_Search"};
82 #endif
83
84 #endif
85
86 static s8 gtp_i2c_test(struct i2c_client *client);
87 void gtp_reset_guitar(struct i2c_client *client, s32 ms);
88 s32 gtp_send_cfg(struct i2c_client *client);
89 void gtp_int_sync(s32 ms, struct goodix_ts_data *ts);
90
91 static ssize_t gt91xx_config_read_proc(struct file *, char __user *, size_t, loff_t *);
92 static ssize_t gt91xx_config_write_proc(struct file *, const char __user *, size_t, loff_t *);
93
94 static struct proc_dir_entry *gt91xx_config_proc = NULL;
95 static const struct file_operations config_proc_ops = {
96 .owner = THIS_MODULE,
97 .read = gt91xx_config_read_proc,
98 .write = gt91xx_config_write_proc,
99 };
100
101 #if GTP_CREATE_WR_NODE
102 extern s32 init_wr_node(struct i2c_client*);
103 extern void uninit_wr_node(void);
104 #endif
105
106 #if GTP_AUTO_UPDATE
107 extern u8 gup_init_update_proc(struct goodix_ts_data *);
108 #endif
109
110 #if GTP_ESD_PROTECT
111 static struct delayed_work gtp_esd_check_work;
112 static struct workqueue_struct * gtp_esd_check_workqueue = NULL;
113 static void gtp_esd_check_func(struct work_struct *);
114 static s32 gtp_init_ext_watchdog(struct i2c_client *client);
115 void gtp_esd_switch(struct i2c_client *, s32);
116 #endif
117
118 //*********** For GT9XXF Start **********//
119 #if GTP_COMPATIBLE_MODE
120 extern s32 i2c_read_bytes(struct i2c_client *client, u16 addr, u8 *buf, s32 len);
121 extern s32 i2c_write_bytes(struct i2c_client *client, u16 addr, u8 *buf, s32 len);
122 extern s32 gup_clk_calibration(void);
123 extern s32 gup_fw_download_proc(void *dir, u8 dwn_mode);
124 extern u8 gup_check_fs_mounted(char *path_name);
125
126 void gtp_recovery_reset(struct i2c_client *client);
127 static s32 gtp_esd_recovery(struct i2c_client *client);
128 s32 gtp_fw_startup(struct i2c_client *client);
129 static s32 gtp_main_clk_proc(struct goodix_ts_data *ts);
130 static s32 gtp_bak_ref_proc(struct goodix_ts_data *ts, u8 mode);
131
132 #endif
133 //********** For GT9XXF End **********//
134
135 #if GTP_GESTURE_WAKEUP
136 typedef enum
137 {
138 DOZE_DISABLED = 0,
139 DOZE_ENABLED = 1,
140 DOZE_WAKEUP = 2,
141 }DOZE_T;
142 static DOZE_T doze_status = DOZE_DISABLED;
143 static s8 gtp_enter_doze(struct goodix_ts_data *ts);
144 #endif
145
146 u8 grp_cfg_version = 0;
147
148 /*******************************************************
149 Function:
150 Read data from the i2c slave device.
151 Input:
152 client: i2c device.
153 buf[0~1]: read start address.
154 buf[2~len-1]: read data buffer.
155 len: GTP_ADDR_LENGTH + read bytes count
156 Output:
157 numbers of i2c_msgs to transfer:
158 2: succeed, otherwise: failed
159 *********************************************************/
gtp_i2c_read(struct i2c_client * client,u8 * buf,s32 len)160 s32 gtp_i2c_read(struct i2c_client *client, u8 *buf, s32 len)
161 {
162 struct i2c_msg msgs[2];
163 s32 ret=-1;
164 s32 retries = 0;
165
166 GTP_DEBUG_FUNC();
167
168 msgs[0].flags = !I2C_M_RD;
169 msgs[0].addr = client->addr;
170 msgs[0].len = GTP_ADDR_LENGTH;
171 msgs[0].buf = &buf[0];
172 #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
173 msgs[0].scl_rate=200 * 1000;
174 //msgs[0].scl_rate = 300 * 1000; // for Rockchip, etc.
175 #endif
176 msgs[1].flags = I2C_M_RD;
177 msgs[1].addr = client->addr;
178 msgs[1].len = len - GTP_ADDR_LENGTH;
179 msgs[1].buf = &buf[GTP_ADDR_LENGTH];
180 #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
181 msgs[1].scl_rate=200 * 1000;
182 //msgs[1].scl_rate = 300 * 1000; // for Rockchip, etc.
183 #endif
184
185 while(retries < 5)
186 {
187 ret = i2c_transfer(client->adapter, msgs, 2);
188 if(ret == 2)break;
189 retries++;
190 }
191 if((retries >= 5))
192 {
193 #if GTP_COMPATIBLE_MODE
194 struct goodix_ts_data *ts = i2c_get_clientdata(client);
195 #endif
196
197 #if GTP_GESTURE_WAKEUP
198 // reset chip would quit doze mode
199 if (DOZE_ENABLED == doze_status)
200 {
201 return ret;
202 }
203 #endif
204 GTP_ERROR("I2C Read: 0x%04X, %d bytes failed, errcode: %d! Process reset.", (((u16)(buf[0] << 8)) | buf[1]), len-2, ret);
205 #if GTP_COMPATIBLE_MODE
206 if (CHIP_TYPE_GT9F == ts->chip_type)
207 {
208 gtp_recovery_reset(client);
209 }
210 else
211 #endif
212 {
213 gtp_reset_guitar(client, 10);
214 }
215 }
216 return ret;
217 }
218
219
220
221 /*******************************************************
222 Function:
223 Write data to the i2c slave device.
224 Input:
225 client: i2c device.
226 buf[0~1]: write start address.
227 buf[2~len-1]: data buffer
228 len: GTP_ADDR_LENGTH + write bytes count
229 Output:
230 numbers of i2c_msgs to transfer:
231 1: succeed, otherwise: failed
232 *********************************************************/
gtp_i2c_write(struct i2c_client * client,u8 * buf,s32 len)233 s32 gtp_i2c_write(struct i2c_client *client,u8 *buf,s32 len)
234 {
235 struct i2c_msg msg;
236 s32 ret = -1;
237 s32 retries = 0;
238
239 GTP_DEBUG_FUNC();
240
241 msg.flags = !I2C_M_RD;
242 msg.addr = client->addr;
243 msg.len = len;
244 msg.buf = buf;
245 #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
246 msg.scl_rate=200 * 1000;
247 //msg.scl_rate = 300 * 1000; // for Rockchip, etc
248 #endif
249 while(retries < 5)
250 {
251 ret = i2c_transfer(client->adapter, &msg, 1);
252 if (ret == 1)break;
253 retries++;
254 }
255 if((retries >= 5))
256 {
257 #if GTP_COMPATIBLE_MODE
258 struct goodix_ts_data *ts = i2c_get_clientdata(client);
259 #endif
260
261 #if GTP_GESTURE_WAKEUP
262 if (DOZE_ENABLED == doze_status)
263 {
264 return ret;
265 }
266 #endif
267 GTP_ERROR("I2C Write: 0x%04X, %d bytes failed, errcode: %d! Process reset.", (((u16)(buf[0] << 8)) | buf[1]), len-2, ret);
268 #if GTP_COMPATIBLE_MODE
269 if (CHIP_TYPE_GT9F == ts->chip_type)
270 {
271 gtp_recovery_reset(client);
272 }
273 else
274 #endif
275 {
276 gtp_reset_guitar(client, 10);
277 }
278 }
279 return ret;
280 }
281
282
283 /*******************************************************
284 Function:
285 i2c read twice, compare the results
286 Input:
287 client: i2c device
288 addr: operate address
289 rxbuf: read data to store, if compare successful
290 len: bytes to read
291 Output:
292 FAIL: read failed
293 SUCCESS: read successful
294 *********************************************************/
gtp_i2c_read_dbl_check(struct i2c_client * client,u16 addr,u8 * rxbuf,int len)295 s32 gtp_i2c_read_dbl_check(struct i2c_client *client, u16 addr, u8 *rxbuf, int len)
296 {
297 u8 buf[16] = {0};
298 u8 confirm_buf[16] = {0};
299 u8 retry = 0;
300
301 while (retry++ < 3)
302 {
303 memset(buf, 0xAA, 16);
304 buf[0] = (u8)(addr >> 8);
305 buf[1] = (u8)(addr & 0xFF);
306 gtp_i2c_read(client, buf, len + 2);
307
308 memset(confirm_buf, 0xAB, 16);
309 confirm_buf[0] = (u8)(addr >> 8);
310 confirm_buf[1] = (u8)(addr & 0xFF);
311 gtp_i2c_read(client, confirm_buf, len + 2);
312
313 if (!memcmp(buf, confirm_buf, len+2))
314 {
315 memcpy(rxbuf, confirm_buf+2, len);
316 return SUCCESS;
317 }
318 }
319 GTP_ERROR("I2C read 0x%04X, %d bytes, double check failed!", addr, len);
320 return FAIL;
321 }
322
323 /*******************************************************
324 Function:
325 Send config.
326 Input:
327 client: i2c device.
328 Output:
329 result of i2c write operation.
330 1: succeed, otherwise: failed
331 *********************************************************/
332
gtp_send_cfg(struct i2c_client * client)333 s32 gtp_send_cfg(struct i2c_client *client)
334 {
335 s32 ret = 2;
336
337 #if GTP_DRIVER_SEND_CFG
338 s32 retry = 0;
339 struct goodix_ts_data *ts = i2c_get_clientdata(client);
340
341 if (ts->fixed_cfg)
342 {
343 GTP_INFO("Ic fixed config, no config sent!");
344 return 0;
345 }
346 else if (ts->pnl_init_error)
347 {
348 GTP_INFO("Error occured in init_panel, no config sent");
349 return 0;
350 }
351
352 GTP_INFO("Driver send config.");
353 for (retry = 0; retry < 5; retry++)
354 {
355 ret = gtp_i2c_write(client, config , GTP_CONFIG_MAX_LENGTH + GTP_ADDR_LENGTH);
356 if (ret > 0)
357 {
358 break;
359 }
360 }
361 #endif
362 return ret;
363 }
364 /*******************************************************
365 Function:
366 Disable irq function
367 Input:
368 ts: goodix i2c_client private data
369 Output:
370 None.
371 *********************************************************/
gtp_irq_disable(struct goodix_ts_data * ts)372 void gtp_irq_disable(struct goodix_ts_data *ts)
373 {
374 unsigned long irqflags;
375
376 GTP_DEBUG_FUNC();
377
378 spin_lock_irqsave(&ts->irq_lock, irqflags);
379 if (!ts->irq_is_disable)
380 {
381 ts->irq_is_disable = 1;
382 disable_irq_nosync(ts->client->irq);
383 }
384 spin_unlock_irqrestore(&ts->irq_lock, irqflags);
385 }
386
387 /*******************************************************
388 Function:
389 Enable irq function
390 Input:
391 ts: goodix i2c_client private data
392 Output:
393 None.
394 *********************************************************/
gtp_irq_enable(struct goodix_ts_data * ts)395 void gtp_irq_enable(struct goodix_ts_data *ts)
396 {
397 unsigned long irqflags = 0;
398
399 GTP_DEBUG_FUNC();
400
401 spin_lock_irqsave(&ts->irq_lock, irqflags);
402 if (ts->irq_is_disable)
403 {
404 enable_irq(ts->client->irq);
405 ts->irq_is_disable = 0;
406 }
407 spin_unlock_irqrestore(&ts->irq_lock, irqflags);
408 }
409
410
411 /*******************************************************
412 Function:
413 Report touch point event
414 Input:
415 ts: goodix i2c_client private data
416 id: trackId
417 x: input x coordinate
418 y: input y coordinate
419 w: input pressure
420 Output:
421 None.
422 *********************************************************/
gtp_touch_down(struct goodix_ts_data * ts,s32 id,s32 x,s32 y,s32 w)423 static void gtp_touch_down(struct goodix_ts_data* ts,s32 id,s32 x,s32 y,s32 w)
424 {
425 if (gtp_change_x2y)
426 GTP_SWAP(x, y);
427
428 if (!bgt911 && !bgt970) {
429 if (gtp_x_reverse)
430 x = ts->abs_x_max - x;
431
432 if (gtp_y_reverse)
433 y = ts->abs_y_max - y;
434 }
435
436 #if GTP_ICS_SLOT_REPORT
437 input_mt_slot(ts->input_dev, id);
438 input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, id);
439 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
440 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
441 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, w);
442 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, w);
443 #else
444 input_report_key(ts->input_dev, BTN_TOUCH, 1);
445 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
446 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
447 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, w);
448 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, w);
449 input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, id);
450 input_mt_sync(ts->input_dev);
451 #endif
452
453 GTP_DEBUG("ID:%d, X:%d, Y:%d, W:%d", id, x, y, w);
454 }
455
456 /*******************************************************
457 Function:
458 Report touch release event
459 Input:
460 ts: goodix i2c_client private data
461 Output:
462 None.
463 *********************************************************/
gtp_touch_up(struct goodix_ts_data * ts,s32 id)464 static void gtp_touch_up(struct goodix_ts_data* ts, s32 id)
465 {
466 #if GTP_ICS_SLOT_REPORT
467 input_mt_slot(ts->input_dev, id);
468 input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, -1);
469 GTP_DEBUG("Touch id[%2d] release!", id);
470 #else
471 input_report_key(ts->input_dev, BTN_TOUCH, 0);
472 #endif
473 }
474
475 #if GTP_WITH_PEN
476
gtp_pen_init(struct goodix_ts_data * ts)477 static void gtp_pen_init(struct goodix_ts_data *ts)
478 {
479 s32 ret = 0;
480
481 GTP_INFO("Request input device for pen/stylus.");
482
483 ts->pen_dev = input_allocate_device();
484 if (ts->pen_dev == NULL)
485 {
486 GTP_ERROR("Failed to allocate input device for pen/stylus.");
487 return;
488 }
489
490 ts->pen_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;
491
492 #if GTP_ICS_SLOT_REPORT
493 input_mt_init_slots(ts->pen_dev, 16, INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
494 #else
495 ts->pen_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
496 #endif
497
498 set_bit(BTN_TOOL_PEN, ts->pen_dev->keybit);
499 set_bit(INPUT_PROP_DIRECT, ts->pen_dev->propbit);
500 //set_bit(INPUT_PROP_POINTER, ts->pen_dev->propbit);
501
502 #if GTP_PEN_HAVE_BUTTON
503 input_set_capability(ts->pen_dev, EV_KEY, BTN_STYLUS);
504 input_set_capability(ts->pen_dev, EV_KEY, BTN_STYLUS2);
505 #endif
506
507 input_set_abs_params(ts->pen_dev, ABS_MT_POSITION_X, 0, ts->abs_x_max, 0, 0);
508 input_set_abs_params(ts->pen_dev, ABS_MT_POSITION_Y, 0, ts->abs_y_max, 0, 0);
509 input_set_abs_params(ts->pen_dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
510 input_set_abs_params(ts->pen_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
511 input_set_abs_params(ts->pen_dev, ABS_MT_TRACKING_ID, 0, 255, 0, 0);
512
513 ts->pen_dev->name = "goodix-pen";
514 ts->pen_dev->id.bustype = BUS_I2C;
515
516 ret = input_register_device(ts->pen_dev);
517 if (ret)
518 {
519 GTP_ERROR("Register %s input device failed", ts->pen_dev->name);
520 return;
521 }
522 }
523
gtp_pen_down(s32 x,s32 y,s32 w,s32 id)524 static void gtp_pen_down(s32 x, s32 y, s32 w, s32 id)
525 {
526 struct goodix_ts_data *ts = i2c_get_clientdata(i2c_connect_client);
527
528 if (gtp_change_x2y)
529 GTP_SWAP(x, y);
530
531
532 input_report_key(ts->pen_dev, BTN_TOOL_PEN, 1);
533 #if GTP_ICS_SLOT_REPORT
534 input_mt_slot(ts->pen_dev, id);
535 input_report_abs(ts->pen_dev, ABS_MT_TRACKING_ID, id);
536 input_report_abs(ts->pen_dev, ABS_MT_POSITION_X, x);
537 input_report_abs(ts->pen_dev, ABS_MT_POSITION_Y, y);
538 input_report_abs(ts->pen_dev, ABS_MT_PRESSURE, w);
539 input_report_abs(ts->pen_dev, ABS_MT_TOUCH_MAJOR, w);
540 #else
541 input_report_key(ts->pen_dev, BTN_TOUCH, 1);
542 input_report_abs(ts->pen_dev, ABS_MT_POSITION_X, x);
543 input_report_abs(ts->pen_dev, ABS_MT_POSITION_Y, y);
544 input_report_abs(ts->pen_dev, ABS_MT_PRESSURE, w);
545 input_report_abs(ts->pen_dev, ABS_MT_TOUCH_MAJOR, w);
546 input_report_abs(ts->pen_dev, ABS_MT_TRACKING_ID, id);
547 input_mt_sync(ts->pen_dev);
548 #endif
549 GTP_DEBUG("(%d)(%d, %d)[%d]", id, x, y, w);
550 }
551
gtp_pen_up(s32 id)552 static void gtp_pen_up(s32 id)
553 {
554 struct goodix_ts_data *ts = i2c_get_clientdata(i2c_connect_client);
555
556 input_report_key(ts->pen_dev, BTN_TOOL_PEN, 0);
557
558 #if GTP_ICS_SLOT_REPORT
559 input_mt_slot(ts->pen_dev, id);
560 input_report_abs(ts->pen_dev, ABS_MT_TRACKING_ID, -1);
561 #else
562
563 input_report_key(ts->pen_dev, BTN_TOUCH, 0);
564 #endif
565
566 }
567 #endif
568
569 /*******************************************************
570 Function:
571 Goodix touchscreen work function
572 Input:
573 work: work struct of goodix_workqueue
574 Output:
575 None.
576 *********************************************************/
goodix_ts_work_func(struct work_struct * work)577 static void goodix_ts_work_func(struct work_struct *work)
578 {
579 u8 end_cmd[3] = {GTP_READ_COOR_ADDR >> 8, GTP_READ_COOR_ADDR & 0xFF, 0};
580 u8 point_data[2 + 1 + 8 * GTP_MAX_TOUCH + 1]={GTP_READ_COOR_ADDR >> 8, GTP_READ_COOR_ADDR & 0xFF};
581 u8 touch_num = 0;
582 u8 finger = 0;
583 static u16 pre_touch = 0;
584 static u8 pre_key = 0;
585 #if GTP_WITH_PEN
586 u8 pen_active = 0;
587 static u8 pre_pen = 0;
588 #endif
589 u8 key_value = 0;
590 u8* coor_data = NULL;
591 s32 input_x = 0;
592 s32 input_y = 0;
593 s32 input_w = 0;
594 s32 id = 0;
595 s32 i = 0;
596 s32 ret = -1;
597 struct goodix_ts_data *ts = NULL;
598
599 #if GTP_COMPATIBLE_MODE
600 u8 rqst_buf[3] = {0x80, 0x43}; // for GT9XXF
601 #endif
602
603 #if GTP_GESTURE_WAKEUP
604 u8 doze_buf[3] = {0x81, 0x4B};
605 #endif
606
607 GTP_DEBUG_FUNC();
608 ts = container_of(work, struct goodix_ts_data, work);
609 if (ts->enter_update)
610 {
611 goto release_wakeup_lock;
612 }
613 #if GTP_GESTURE_WAKEUP
614 if (DOZE_ENABLED == doze_status)
615 {
616 ret = gtp_i2c_read(i2c_connect_client, doze_buf, 3);
617 GTP_DEBUG("0x814B = 0x%02X", doze_buf[2]);
618 if (ret > 0)
619 {
620 if ((doze_buf[2] == 'a') || (doze_buf[2] == 'b') || (doze_buf[2] == 'c') ||
621 (doze_buf[2] == 'd') || (doze_buf[2] == 'e') || (doze_buf[2] == 'g') ||
622 (doze_buf[2] == 'h') || (doze_buf[2] == 'm') || (doze_buf[2] == 'o') ||
623 (doze_buf[2] == 'q') || (doze_buf[2] == 's') || (doze_buf[2] == 'v') ||
624 (doze_buf[2] == 'w') || (doze_buf[2] == 'y') || (doze_buf[2] == 'z') ||
625 (doze_buf[2] == 0x5E) /* ^ */
626 )
627 {
628 if (doze_buf[2] != 0x5E)
629 {
630 GTP_INFO("Wakeup by gesture(%c), light up the screen!", doze_buf[2]);
631 }
632 else
633 {
634 GTP_INFO("Wakeup by gesture(^), light up the screen!");
635 }
636 doze_status = DOZE_WAKEUP;
637 input_report_key(ts->input_dev, KEY_POWER, 1);
638 input_sync(ts->input_dev);
639 input_report_key(ts->input_dev, KEY_POWER, 0);
640 input_sync(ts->input_dev);
641 // clear 0x814B
642 doze_buf[2] = 0x00;
643 gtp_i2c_write(i2c_connect_client, doze_buf, 3);
644 }
645 else if ( (doze_buf[2] == 0xAA) || (doze_buf[2] == 0xBB) ||
646 (doze_buf[2] == 0xAB) || (doze_buf[2] == 0xBA) )
647 {
648 char *direction[4] = {"Right", "Down", "Up", "Left"};
649 u8 type = ((doze_buf[2] & 0x0F) - 0x0A) + (((doze_buf[2] >> 4) & 0x0F) - 0x0A) * 2;
650
651 GTP_INFO("%s slide to light up the screen!", direction[type]);
652 doze_status = DOZE_WAKEUP;
653 input_report_key(ts->input_dev, KEY_POWER, 1);
654 input_sync(ts->input_dev);
655 input_report_key(ts->input_dev, KEY_POWER, 0);
656 input_sync(ts->input_dev);
657 // clear 0x814B
658 doze_buf[2] = 0x00;
659 gtp_i2c_write(i2c_connect_client, doze_buf, 3);
660 }
661 else if (0xCC == doze_buf[2])
662 {
663 GTP_INFO("Double click to light up the screen!");
664 doze_status = DOZE_WAKEUP;
665 input_report_key(ts->input_dev, KEY_POWER, 1);
666 input_sync(ts->input_dev);
667 input_report_key(ts->input_dev, KEY_POWER, 0);
668 input_sync(ts->input_dev);
669 // clear 0x814B
670 doze_buf[2] = 0x00;
671 gtp_i2c_write(i2c_connect_client, doze_buf, 3);
672 }
673 else
674 {
675 // clear 0x814B
676 doze_buf[2] = 0x00;
677 gtp_i2c_write(i2c_connect_client, doze_buf, 3);
678 gtp_enter_doze(ts);
679 }
680 }
681 if (ts->use_irq)
682 {
683 gtp_irq_enable(ts);
684 }
685 goto release_wakeup_lock;
686 }
687 #endif
688
689 ret = gtp_i2c_read(ts->client, point_data, 12);
690 if (ret < 0)
691 {
692 GTP_ERROR("I2C transfer error. errno:%d\n ", ret);
693 if (ts->use_irq)
694 {
695 gtp_irq_enable(ts);
696 }
697 goto release_wakeup_lock;
698 }
699
700 finger = point_data[GTP_ADDR_LENGTH];
701
702 #if GTP_COMPATIBLE_MODE
703 // GT9XXF
704 if ((finger == 0x00) && (CHIP_TYPE_GT9F == ts->chip_type)) // request arrived
705 {
706 ret = gtp_i2c_read(ts->client, rqst_buf, 3);
707 if (ret < 0)
708 {
709 GTP_ERROR("Read request status error!");
710 goto exit_work_func;
711 }
712
713 switch (rqst_buf[2])
714 {
715 case GTP_RQST_CONFIG:
716 GTP_INFO("Request for config.");
717 ret = gtp_send_cfg(ts->client);
718 if (ret < 0)
719 {
720 GTP_ERROR("Request for config unresponded!");
721 }
722 else
723 {
724 rqst_buf[2] = GTP_RQST_RESPONDED;
725 gtp_i2c_write(ts->client, rqst_buf, 3);
726 GTP_INFO("Request for config responded!");
727 }
728 break;
729
730 case GTP_RQST_BAK_REF:
731 /*
732 GTP_INFO("Request for backup reference.");
733 ts->rqst_processing = 1;
734 ret = gtp_bak_ref_proc(ts, GTP_BAK_REF_SEND);
735 if (SUCCESS == ret)
736 {
737 rqst_buf[2] = GTP_RQST_RESPONDED;
738 gtp_i2c_write(ts->client, rqst_buf, 3);
739 ts->rqst_processing = 0;
740 GTP_INFO("Request for backup reference responded!");
741 }
742 else
743 {
744 GTP_ERROR("Requeset for backup reference unresponed!");
745 }
746 */
747 break;
748
749 case GTP_RQST_RESET:
750 GTP_INFO("Request for reset.");
751 gtp_recovery_reset(ts->client);
752 break;
753
754 case GTP_RQST_MAIN_CLOCK:
755 GTP_INFO("Request for main clock.");
756 ts->rqst_processing = 1;
757 ret = gtp_main_clk_proc(ts);
758 if (FAIL == ret)
759 {
760 GTP_ERROR("Request for main clock unresponded!");
761 }
762 else
763 {
764 GTP_INFO("Request for main clock responded!");
765 rqst_buf[2] = GTP_RQST_RESPONDED;
766 gtp_i2c_write(ts->client, rqst_buf, 3);
767 ts->rqst_processing = 0;
768 ts->clk_chk_fs_times = 0;
769 }
770 break;
771
772 default:
773 GTP_INFO("Undefined request: 0x%02X", rqst_buf[2]);
774 rqst_buf[2] = GTP_RQST_RESPONDED;
775 gtp_i2c_write(ts->client, rqst_buf, 3);
776 break;
777 }
778 }
779 #endif
780 if (finger == 0x00)
781 {
782 if (ts->use_irq)
783 {
784 gtp_irq_enable(ts);
785 }
786 goto release_wakeup_lock;
787 }
788
789 if((finger & 0x80) == 0)
790 {
791 goto exit_work_func;
792 }
793
794 touch_num = finger & 0x0f;
795 if (touch_num > GTP_MAX_TOUCH)
796 {
797 goto exit_work_func;
798 }
799
800 if (touch_num > 1)
801 {
802 u8 buf[8 * GTP_MAX_TOUCH] = {(GTP_READ_COOR_ADDR + 10) >> 8, (GTP_READ_COOR_ADDR + 10) & 0xff};
803
804 ret = gtp_i2c_read(ts->client, buf, 2 + 8 * (touch_num - 1));
805 memcpy(&point_data[12], &buf[2], 8 * (touch_num - 1));
806 }
807
808 #if (GTP_HAVE_TOUCH_KEY || GTP_PEN_HAVE_BUTTON)
809 key_value = point_data[3 + 8 * touch_num];
810
811 if(key_value || pre_key)
812 {
813 #if GTP_PEN_HAVE_BUTTON
814 if (key_value == 0x40)
815 {
816 GTP_DEBUG("BTN_STYLUS & BTN_STYLUS2 Down.");
817 input_report_key(ts->pen_dev, BTN_STYLUS, 1);
818 input_report_key(ts->pen_dev, BTN_STYLUS2, 1);
819 pen_active = 1;
820 }
821 else if (key_value == 0x10)
822 {
823 GTP_DEBUG("BTN_STYLUS Down, BTN_STYLUS2 Up.");
824 input_report_key(ts->pen_dev, BTN_STYLUS, 1);
825 input_report_key(ts->pen_dev, BTN_STYLUS2, 0);
826 pen_active = 1;
827 }
828 else if (key_value == 0x20)
829 {
830 GTP_DEBUG("BTN_STYLUS Up, BTN_STYLUS2 Down.");
831 input_report_key(ts->pen_dev, BTN_STYLUS, 0);
832 input_report_key(ts->pen_dev, BTN_STYLUS2, 1);
833 pen_active = 1;
834 }
835 else
836 {
837 GTP_DEBUG("BTN_STYLUS & BTN_STYLUS2 Up.");
838 input_report_key(ts->pen_dev, BTN_STYLUS, 0);
839 input_report_key(ts->pen_dev, BTN_STYLUS2, 0);
840 if ( (pre_key == 0x40) || (pre_key == 0x20) ||
841 (pre_key == 0x10)
842 )
843 {
844 pen_active = 1;
845 }
846 }
847 if (pen_active)
848 {
849 touch_num = 0; // shield pen point
850 //pre_touch = 0; // clear last pen status
851 }
852 #endif
853
854 #if GTP_HAVE_TOUCH_KEY
855 if (!pre_touch)
856 {
857 for (i = 0; i < GTP_MAX_KEY_NUM; i++)
858 {
859 #if GTP_DEBUG_ON
860 for (ret = 0; ret < 4; ++ret)
861 {
862 if (key_codes[ret] == touch_key_array[i])
863 {
864 GTP_DEBUG("Key: %s %s", key_names[ret], (key_value & (0x01 << i)) ? "Down" : "Up");
865 break;
866 }
867 }
868 #endif
869 input_report_key(ts->input_dev, touch_key_array[i], key_value & (0x01<<i));
870 }
871 touch_num = 0; // shield fingers
872 }
873 #endif
874 }
875 #endif
876 pre_key = key_value;
877
878 GTP_DEBUG("pre_touch:%02x, finger:%02x.", pre_touch, finger);
879
880 #if GTP_ICS_SLOT_REPORT
881
882 #if GTP_WITH_PEN
883 if (pre_pen && (touch_num == 0))
884 {
885 GTP_DEBUG("Pen touch UP(Slot)!");
886 gtp_pen_up(0);
887 pen_active = 1;
888 pre_pen = 0;
889 }
890 #endif
891 if (pre_touch || touch_num)
892 {
893 s32 pos = 0;
894 u16 touch_index = 0;
895 u8 report_num = 0;
896 coor_data = &point_data[3];
897
898 if(touch_num)
899 {
900 id = coor_data[pos] & 0x0F;
901
902 #if GTP_WITH_PEN
903 id = coor_data[pos];
904 if ((id & 0x80))
905 {
906 GTP_DEBUG("Pen touch DOWN(Slot)!");
907 input_x = coor_data[pos + 1] | (coor_data[pos + 2] << 8);
908 input_y = coor_data[pos + 3] | (coor_data[pos + 4] << 8);
909 input_w = coor_data[pos + 5] | (coor_data[pos + 6] << 8);
910
911 gtp_pen_down(input_x, input_y, input_w, 0);
912 pre_pen = 1;
913 pre_touch = 0;
914 pen_active = 1;
915 }
916 #endif
917
918 touch_index |= (0x01<<id);
919 }
920
921 GTP_DEBUG("id = %d,touch_index = 0x%x, pre_touch = 0x%x\n",id, touch_index,pre_touch);
922 for (i = 0; i < GTP_MAX_TOUCH; i++)
923 {
924 #if GTP_WITH_PEN
925 if (pre_pen == 1)
926 {
927 break;
928 }
929 #endif
930
931 if ((touch_index & (0x01<<i)))
932 {
933 input_x = coor_data[pos + 1] | (coor_data[pos + 2] << 8);
934 input_y = coor_data[pos + 3] | (coor_data[pos + 4] << 8);
935 input_w = coor_data[pos + 5] | (coor_data[pos + 6] << 8);
936
937 gtp_touch_down(ts, id, input_x, input_y, input_w);
938 pre_touch |= 0x01 << i;
939
940 report_num++;
941 if (report_num < touch_num)
942 {
943 pos += 8;
944 id = coor_data[pos] & 0x0F;
945 touch_index |= (0x01<<id);
946 }
947 }
948 else
949 {
950 gtp_touch_up(ts, i);
951 pre_touch &= ~(0x01 << i);
952 }
953 }
954 }
955 #else //end if GTP_ICS_SLOT_REPORT
956
957 if (touch_num)
958 {
959 for (i = 0; i < touch_num; i++)
960 {
961 coor_data = &point_data[i * 8 + 3];
962
963 id = coor_data[0] & 0x0F;
964 input_x = coor_data[1] | (coor_data[2] << 8);
965 input_y = coor_data[3] | (coor_data[4] << 8);
966 input_w = coor_data[5] | (coor_data[6] << 8);
967
968 #if GTP_WITH_PEN
969 id = coor_data[0];
970 if (id & 0x80)
971 {
972 GTP_DEBUG("Pen touch DOWN!");
973 gtp_pen_down(input_x, input_y, input_w, 0);
974 pre_pen = 1;
975 pen_active = 1;
976 break;
977 }
978 else
979 #endif
980 {
981 gtp_touch_down(ts, id, input_x, input_y, input_w);
982 }
983 }
984 }
985 else if (pre_touch)
986 {
987 #if GTP_WITH_PEN
988 if (pre_pen == 1)
989 {
990 GTP_DEBUG("Pen touch UP!");
991 gtp_pen_up(0);
992 pre_pen = 0;
993 pen_active = 1;
994 }
995 else
996 #endif
997 {
998 GTP_DEBUG("Touch Release!");
999 gtp_touch_up(ts, 0);
1000 }
1001 }
1002
1003 pre_touch = touch_num;
1004 #endif
1005
1006 #if GTP_WITH_PEN
1007 if (pen_active)
1008 {
1009 pen_active = 0;
1010 input_sync(ts->pen_dev);
1011 }
1012 else
1013 #endif
1014 {
1015 input_sync(ts->input_dev);
1016 }
1017
1018 exit_work_func:
1019 if(!ts->gtp_rawdiff_mode)
1020 {
1021 ret = gtp_i2c_write(ts->client, end_cmd, 3);
1022 if (ret < 0)
1023 {
1024 GTP_INFO("I2C write end_cmd error!");
1025 }
1026 }
1027 if (ts->use_irq)
1028 {
1029 gtp_irq_enable(ts);
1030 }
1031
1032 release_wakeup_lock:
1033 if (device_can_wakeup(&ts->client->dev))
1034 pm_relax(&ts->client->dev);
1035 }
1036
1037 /*******************************************************
1038 Function:
1039 Timer interrupt service routine for polling mode.
1040 Input:
1041 timer: timer struct pointer
1042 Output:
1043 Timer work mode.
1044 HRTIMER_NORESTART: no restart mode
1045 *********************************************************/
goodix_ts_timer_handler(struct hrtimer * timer)1046 static enum hrtimer_restart goodix_ts_timer_handler(struct hrtimer *timer)
1047 {
1048 struct goodix_ts_data *ts = container_of(timer, struct goodix_ts_data, timer);
1049
1050 GTP_DEBUG_FUNC();
1051
1052 queue_work(goodix_wq, &ts->work);
1053 hrtimer_start(&ts->timer, ktime_set(0, (GTP_POLL_TIME+6)*1000000), HRTIMER_MODE_REL);
1054 return HRTIMER_NORESTART;
1055 }
1056
1057 /*******************************************************
1058 Function:
1059 External interrupt service routine for interrupt mode.
1060 Input:
1061 irq: interrupt number.
1062 dev_id: private data pointer
1063 Output:
1064 Handle Result.
1065 IRQ_HANDLED: interrupt handled successfully
1066 *********************************************************/
goodix_ts_irq_handler(int irq,void * dev_id)1067 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
1068 {
1069 struct goodix_ts_data *ts = dev_id;
1070
1071 GTP_DEBUG_FUNC();
1072
1073 gtp_irq_disable(ts);
1074
1075 if (device_can_wakeup(&ts->client->dev))
1076 pm_stay_awake(&ts->client->dev);
1077
1078 queue_work(goodix_wq, &ts->work);
1079
1080 return IRQ_HANDLED;
1081 }
1082 /*******************************************************
1083 Function:
1084 Synchronization.
1085 Input:
1086 ms: synchronization time in millisecond.
1087 Output:
1088 None.
1089 *******************************************************/
gtp_int_sync(s32 ms,struct goodix_ts_data * ts)1090 void gtp_int_sync(s32 ms, struct goodix_ts_data *ts)
1091 {
1092 GTP_GPIO_OUTPUT(ts->irq_pin, 0);
1093 msleep(ms);
1094 //GTP_GPIO_AS_INT(GTP_INT_PORT);
1095 gpio_direction_input(ts->irq_pin);
1096 //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1097 //s3c_gpio_cfgpin(pin, GTP_INT_CFG);
1098 }
1099
1100
1101 /*******************************************************
1102 Function:
1103 Reset chip.
1104 Input:
1105 ms: reset time in millisecond
1106 Output:
1107 None.
1108 *******************************************************/
gtp_reset_guitar(struct i2c_client * client,s32 ms)1109 void gtp_reset_guitar(struct i2c_client *client, s32 ms)
1110 {
1111 struct goodix_ts_data *ts = i2c_get_clientdata(client);
1112
1113 GTP_DEBUG_FUNC();
1114 GTP_INFO("Guitar reset");
1115 GTP_GPIO_OUTPUT(ts->rst_pin, 0); // begin select I2C slave addr
1116 msleep(ms); // T2: > 10ms
1117 // HIGH: 0x28/0x29, LOW: 0xBA/0xBB
1118 GTP_GPIO_OUTPUT(ts->irq_pin, client->addr == 0x14);
1119
1120 msleep(2); // T3: > 100us
1121 GTP_GPIO_OUTPUT(ts->rst_pin, 1);
1122
1123 msleep(6); // T4: > 5ms
1124
1125 //GTP_GPIO_AS_INPUT(GTP_RST_PORT); // end select I2C slave addr
1126 gpio_direction_input(ts->rst_pin);
1127 //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1128
1129 #if GTP_COMPATIBLE_MODE
1130 if (CHIP_TYPE_GT9F == ts->chip_type)
1131 {
1132 return;
1133 }
1134 #endif
1135
1136 gtp_int_sync(50, ts);
1137 #if GTP_ESD_PROTECT
1138 gtp_init_ext_watchdog(client);
1139 #endif
1140 }
1141
1142 #if GTP_GESTURE_WAKEUP
1143 /*******************************************************
1144 Function:
1145 Enter doze mode for sliding wakeup.
1146 Input:
1147 ts: goodix tp private data
1148 Output:
1149 1: succeed, otherwise failed
1150 *******************************************************/
gtp_enter_doze(struct goodix_ts_data * ts)1151 static s8 gtp_enter_doze(struct goodix_ts_data *ts)
1152 {
1153 s8 ret = -1;
1154 s8 retry = 0;
1155 u8 i2c_control_buf[3] = {(u8)(GTP_REG_SLEEP >> 8), (u8)GTP_REG_SLEEP, 8};
1156
1157 GTP_DEBUG_FUNC();
1158
1159 GTP_DEBUG("Entering gesture mode.");
1160 while(retry++ < 5)
1161 {
1162 i2c_control_buf[0] = 0x80;
1163 i2c_control_buf[1] = 0x46;
1164 ret = gtp_i2c_write(ts->client, i2c_control_buf, 3);
1165 if (ret < 0)
1166 {
1167 GTP_DEBUG("failed to set doze flag into 0x8046, %d", retry);
1168 continue;
1169 }
1170 i2c_control_buf[0] = 0x80;
1171 i2c_control_buf[1] = 0x40;
1172 ret = gtp_i2c_write(ts->client, i2c_control_buf, 3);
1173 if (ret > 0)
1174 {
1175 doze_status = DOZE_ENABLED;
1176 GTP_INFO("Gesture mode enabled.");
1177 return ret;
1178 }
1179 msleep(10);
1180 }
1181 GTP_ERROR("GTP send gesture cmd failed.");
1182 return ret;
1183 }
1184 #else
1185 /*******************************************************
1186 Function:
1187 Enter sleep mode.
1188 Input:
1189 ts: private data.
1190 Output:
1191 Executive outcomes.
1192 1: succeed, otherwise failed.
1193 *******************************************************/
gtp_enter_sleep(struct goodix_ts_data * ts)1194 static s8 gtp_enter_sleep(struct goodix_ts_data * ts)
1195 {
1196 s8 ret = -1;
1197 s8 retry = 0;
1198 u8 i2c_control_buf[3] = {(u8)(GTP_REG_SLEEP >> 8), (u8)GTP_REG_SLEEP, 5};
1199 #if GTP_COMPATIBLE_MODE
1200 u8 status_buf[3] = {0x80, 0x44};
1201 #endif
1202
1203 GTP_DEBUG_FUNC();
1204
1205 #if GTP_COMPATIBLE_MODE
1206 if (CHIP_TYPE_GT9F == ts->chip_type)
1207 {
1208 // GT9XXF: host interact with ic
1209 ret = gtp_i2c_read(ts->client, status_buf, 3);
1210 if (ret < 0)
1211 {
1212 GTP_ERROR("failed to get backup-reference status");
1213 }
1214
1215 if (status_buf[2] & 0x80)
1216 {
1217 ret = gtp_bak_ref_proc(ts, GTP_BAK_REF_STORE);
1218 if (FAIL == ret)
1219 {
1220 GTP_ERROR("failed to store bak_ref");
1221 }
1222 }
1223 }
1224 #endif
1225
1226 GTP_GPIO_OUTPUT(ts->irq_pin, 0);
1227 msleep(5);
1228
1229 while(retry++ < 5)
1230 {
1231 ret = gtp_i2c_write(ts->client, i2c_control_buf, 3);
1232 if (ret > 0)
1233 {
1234 GTP_INFO("GTP enter sleep!");
1235
1236 return ret;
1237 }
1238 msleep(10);
1239 }
1240 GTP_ERROR("GTP send sleep cmd failed.");
1241 return ret;
1242 }
1243 #endif
1244 /*******************************************************
1245 Function:
1246 Wakeup from sleep.
1247 Input:
1248 ts: private data.
1249 Output:
1250 Executive outcomes.
1251 >0: succeed, otherwise: failed.
1252 *******************************************************/
gtp_wakeup_sleep(struct goodix_ts_data * ts)1253 static s8 gtp_wakeup_sleep(struct goodix_ts_data * ts)
1254 {
1255 u8 retry = 0;
1256 s8 ret = -1;
1257
1258 GTP_DEBUG_FUNC();
1259
1260 #if GTP_COMPATIBLE_MODE
1261 if (CHIP_TYPE_GT9F == ts->chip_type)
1262 {
1263 u8 opr_buf[3] = {0x41, 0x80};
1264
1265 GTP_GPIO_OUTPUT(ts->irq_pin, 1);
1266 msleep(5);
1267
1268 for (retry = 0; retry < 10; ++retry)
1269 {
1270 // hold ss51 & dsp
1271 opr_buf[2] = 0x0C;
1272 ret = gtp_i2c_write(ts->client, opr_buf, 3);
1273 if (FAIL == ret)
1274 {
1275 GTP_ERROR("failed to hold ss51 & dsp!");
1276 continue;
1277 }
1278 opr_buf[2] = 0x00;
1279 ret = gtp_i2c_read(ts->client, opr_buf, 3);
1280 if (FAIL == ret)
1281 {
1282 GTP_ERROR("failed to get ss51 & dsp status!");
1283 continue;
1284 }
1285 if (0x0C != opr_buf[2])
1286 {
1287 GTP_DEBUG("ss51 & dsp not been hold, %d", retry+1);
1288 continue;
1289 }
1290 GTP_DEBUG("ss51 & dsp confirmed hold");
1291
1292 ret = gtp_fw_startup(ts->client);
1293 if (FAIL == ret)
1294 {
1295 GTP_ERROR("failed to startup GT9XXF, process recovery");
1296 gtp_esd_recovery(ts->client);
1297 }
1298 break;
1299 }
1300 if (retry >= 10)
1301 {
1302 GTP_ERROR("failed to wakeup, processing esd recovery");
1303 gtp_esd_recovery(ts->client);
1304 }
1305 else
1306 {
1307 GTP_INFO("GT9XXF gtp wakeup success");
1308 }
1309 return ret;
1310 }
1311 #endif
1312
1313 #if GTP_POWER_CTRL_SLEEP
1314 while(retry++ < 5)
1315 {
1316 gtp_reset_guitar(ts->client, 20);
1317
1318 GTP_INFO("GTP wakeup sleep.");
1319 return 1;
1320 }
1321 #else
1322 while(retry++ < 10)
1323 {
1324 #if GTP_GESTURE_WAKEUP
1325 if (DOZE_WAKEUP != doze_status)
1326 {
1327 GTP_INFO("Powerkey wakeup.");
1328 }
1329 else
1330 {
1331 GTP_INFO("Gesture wakeup.");
1332 }
1333 doze_status = DOZE_DISABLED;
1334 gtp_irq_disable(ts);
1335 gtp_reset_guitar(ts->client, 10);
1336 gtp_irq_enable(ts);
1337
1338 #else
1339 GTP_GPIO_OUTPUT(ts->irq_pin, 1);
1340 msleep(5);
1341 #endif
1342
1343 ret = gtp_i2c_test(ts->client);
1344 if (ret > 0)
1345 {
1346 GTP_INFO("GTP wakeup sleep.");
1347
1348 #if (!GTP_GESTURE_WAKEUP)
1349 {
1350 gtp_int_sync(25, ts);
1351 #if GTP_ESD_PROTECT
1352 gtp_init_ext_watchdog(ts->client);
1353 #endif
1354 }
1355 #endif
1356
1357 return ret;
1358 }
1359 gtp_reset_guitar(ts->client, 20);
1360 }
1361 #endif
1362
1363 GTP_ERROR("GTP wakeup sleep failed.");
1364 return ret;
1365 }
1366 #if GTP_DRIVER_SEND_CFG
gtp_get_info(struct goodix_ts_data * ts)1367 static s32 gtp_get_info(struct goodix_ts_data *ts)
1368 {
1369 u8 opr_buf[6] = {0};
1370 s32 ret = 0;
1371
1372 //ts->abs_x_max = GTP_MAX_WIDTH;
1373 //ts->abs_y_max = GTP_MAX_HEIGHT;
1374
1375 ts->int_trigger_type = GTP_INT_TRIGGER;
1376
1377 opr_buf[0] = (u8)((GTP_REG_CONFIG_DATA+1) >> 8);
1378 opr_buf[1] = (u8)((GTP_REG_CONFIG_DATA+1) & 0xFF);
1379
1380 ret = gtp_i2c_read(ts->client, opr_buf, 6);
1381 if (ret < 0)
1382 {
1383 return FAIL;
1384 }
1385
1386 ts->abs_x_max = (opr_buf[3] << 8) + opr_buf[2];
1387 ts->abs_y_max = (opr_buf[5] << 8) + opr_buf[4];
1388
1389 opr_buf[0] = (u8)((GTP_REG_CONFIG_DATA+6) >> 8);
1390 opr_buf[1] = (u8)((GTP_REG_CONFIG_DATA+6) & 0xFF);
1391
1392 ret = gtp_i2c_read(ts->client, opr_buf, 3);
1393 if (ret < 0)
1394 {
1395 return FAIL;
1396 }
1397 ts->int_trigger_type = opr_buf[2] & 0x03;
1398
1399 GTP_INFO("X_MAX = %d, Y_MAX = %d, TRIGGER = 0x%02x",
1400 ts->abs_x_max,ts->abs_y_max,ts->int_trigger_type);
1401
1402 return SUCCESS;
1403 }
1404 #endif
1405
1406 /*******************************************************
1407 Function:
1408 Initialize gtp.
1409 Input:
1410 ts: goodix private data
1411 Output:
1412 Executive outcomes.
1413 0: succeed, otherwise: failed
1414 *******************************************************/
gtp_init_panel(struct goodix_ts_data * ts)1415 static s32 gtp_init_panel(struct goodix_ts_data *ts)
1416 {
1417 s32 ret = -1;
1418 #if GTP_DRIVER_SEND_CFG
1419 s32 i = 0;
1420 u8 check_sum = 0;
1421 u8 opr_buf[16] = {0};
1422 u8 sensor_id = 0;
1423
1424 u8 cfg_info_group2[] = CTP_CFG_GROUP2;
1425 u8 cfg_info_group3[] = CTP_CFG_GROUP3;
1426 u8 cfg_info_group4[] = CTP_CFG_GROUP4;
1427 u8 cfg_info_group5[] = CTP_CFG_GROUP5;
1428 u8 cfg_info_group6[] = CTP_CFG_GROUP6;
1429 u8 *send_cfg_buf[] = {gtp_dat_10_1, cfg_info_group2, cfg_info_group3,
1430 cfg_info_group4, cfg_info_group5, cfg_info_group6};
1431 u8 cfg_info_len[] = { CFG_GROUP_LEN(gtp_dat_10_1),
1432 CFG_GROUP_LEN(cfg_info_group2),
1433 CFG_GROUP_LEN(cfg_info_group3),
1434 CFG_GROUP_LEN(cfg_info_group4),
1435 CFG_GROUP_LEN(cfg_info_group5),
1436 CFG_GROUP_LEN(cfg_info_group6)};;
1437
1438 GTP_INFO(" <%s>_%d \n", __func__, __LINE__);
1439
1440 if(m89or101){
1441 if (ts->cfg_file_num) {
1442 send_cfg_buf[0] = gtp_dat_8_9_1;
1443 cfg_info_len[0] = CFG_GROUP_LEN(gtp_dat_8_9_1);
1444 } else {
1445 send_cfg_buf[0] = gtp_dat_8_9;
1446 cfg_info_len[0] = CFG_GROUP_LEN(gtp_dat_8_9);
1447 }
1448 }
1449
1450 if (bgt911) {
1451 send_cfg_buf[0] = gtp_dat_gt11;
1452 cfg_info_len[0] = CFG_GROUP_LEN(gtp_dat_gt11);
1453 }
1454
1455 if (bgt9110) {
1456 send_cfg_buf[0] = gtp_dat_gt9110;
1457 cfg_info_len[0] = CFG_GROUP_LEN(gtp_dat_gt9110);
1458 }
1459
1460 if (bgt9111) {
1461 send_cfg_buf[0] = gtp_dat_gt9111;
1462 cfg_info_len[0] = CFG_GROUP_LEN(gtp_dat_gt9111);
1463 }
1464
1465 if (bgt970) {
1466 send_cfg_buf[0] = gtp_dat_9_7;
1467 cfg_info_len[0] = CFG_GROUP_LEN(gtp_dat_9_7);
1468 }
1469
1470 if (bgt910) {
1471 send_cfg_buf[0] = gtp_dat_7;
1472 cfg_info_len[0] = CFG_GROUP_LEN(gtp_dat_7);
1473 }
1474
1475 GTP_DEBUG_FUNC();
1476 GTP_DEBUG("Config Groups\' Lengths: %d, %d, %d, %d, %d, %d",
1477 cfg_info_len[0], cfg_info_len[1], cfg_info_len[2], cfg_info_len[3],
1478 cfg_info_len[4], cfg_info_len[5]);
1479
1480
1481 #if GTP_COMPATIBLE_MODE
1482 if (CHIP_TYPE_GT9F == ts->chip_type)
1483 {
1484 ts->fw_error = 0;
1485 }
1486 else
1487 #endif
1488 {
1489 ret = gtp_i2c_read_dbl_check(ts->client, 0x41E4, opr_buf, 1);
1490 if (SUCCESS == ret)
1491 {
1492 if (opr_buf[0] != 0xBE)
1493 {
1494 ts->fw_error = 1;
1495 GTP_ERROR("Firmware error, no config sent!");
1496 return -1;
1497 }
1498 }
1499 }
1500
1501 if ((!cfg_info_len[1]) && (!cfg_info_len[2]) &&
1502 (!cfg_info_len[3]) && (!cfg_info_len[4]) &&
1503 (!cfg_info_len[5]))
1504 {
1505 sensor_id = 0;
1506 }
1507 else
1508 {
1509 #if GTP_COMPATIBLE_MODE
1510 msleep(50);
1511 #endif
1512 ret = gtp_i2c_read_dbl_check(ts->client, GTP_REG_SENSOR_ID, &sensor_id, 1);
1513 if (SUCCESS == ret)
1514 {
1515 if (sensor_id >= 0x06)
1516 {
1517 GTP_ERROR("Invalid sensor_id(0x%02X), No Config Sent!", sensor_id);
1518 ts->pnl_init_error = 1;
1519 return -1;
1520 }
1521 }
1522 else
1523 {
1524 GTP_ERROR("Failed to get sensor_id, No config sent!");
1525 ts->pnl_init_error = 1;
1526 return -1;
1527 }
1528 GTP_INFO("Sensor_ID: %d", sensor_id);
1529 }
1530 ts->gtp_cfg_len = cfg_info_len[sensor_id];
1531 GTP_INFO("CTP_CONFIG_GROUP%d used, config length: %d", sensor_id + 1, ts->gtp_cfg_len);
1532
1533 if (ts->gtp_cfg_len < GTP_CONFIG_MIN_LENGTH)
1534 {
1535 GTP_ERROR("Config Group%d is INVALID CONFIG GROUP(Len: %d)! NO Config Sent! You need to check you header file CFG_GROUP section!", sensor_id+1, ts->gtp_cfg_len);
1536 ts->pnl_init_error = 1;
1537 return -1;
1538 }
1539
1540 #if GTP_COMPATIBLE_MODE
1541 if (CHIP_TYPE_GT9F == ts->chip_type)
1542 {
1543 ts->fixed_cfg = 0;
1544 }
1545 else
1546 #endif
1547 {
1548 ret = gtp_i2c_read_dbl_check(ts->client, GTP_REG_CONFIG_DATA, &opr_buf[0], 1);
1549
1550 if (ret == SUCCESS)
1551 {
1552 GTP_DEBUG("CFG_GROUP%d Config Version: %d, 0x%02X; IC Config Version: %d, 0x%02X", sensor_id+1,
1553 send_cfg_buf[sensor_id][0], send_cfg_buf[sensor_id][0], opr_buf[0], opr_buf[0]);
1554
1555 if (opr_buf[0] < 90)
1556 {
1557 GTP_INFO(" <%s>_%d \n", __func__, __LINE__);
1558 grp_cfg_version = send_cfg_buf[sensor_id][0]; // backup group config version
1559 send_cfg_buf[sensor_id][0] = 0x00;
1560 ts->fixed_cfg = 0;
1561 }
1562 else // treated as fixed config, not send config
1563 {
1564 GTP_INFO("Ic fixed config with config version(%d, 0x%02X)", opr_buf[0], opr_buf[0]);
1565 ts->fixed_cfg = 1;
1566 gtp_get_info(ts);
1567 return 0;
1568 }
1569 }
1570 else
1571 {
1572 GTP_ERROR("Failed to get ic config version!No config sent!");
1573 return -1;
1574 }
1575 }
1576
1577 memset(&config[GTP_ADDR_LENGTH], 0, GTP_CONFIG_MAX_LENGTH);
1578 memcpy(&config[GTP_ADDR_LENGTH], send_cfg_buf[sensor_id], ts->gtp_cfg_len);
1579
1580 #if GTP_CUSTOM_CFG
1581 config[RESOLUTION_LOC] = (u8)GTP_MAX_WIDTH;
1582 config[RESOLUTION_LOC + 1] = (u8)(GTP_MAX_WIDTH>>8);
1583 config[RESOLUTION_LOC + 2] = (u8)GTP_MAX_HEIGHT;
1584 config[RESOLUTION_LOC + 3] = (u8)(GTP_MAX_HEIGHT>>8);
1585
1586 if (GTP_INT_TRIGGER == 0) //RISING
1587 {
1588 config[TRIGGER_LOC] &= 0xfe;
1589 }
1590 else if (GTP_INT_TRIGGER == 1) //FALLING
1591 {
1592 config[TRIGGER_LOC] |= 0x01;
1593 }
1594 #endif // GTP_CUSTOM_CFG
1595
1596 check_sum = 0;
1597 for (i = GTP_ADDR_LENGTH; i < ts->gtp_cfg_len; i++)
1598 {
1599 check_sum += config[i];
1600 }
1601 config[ts->gtp_cfg_len] = (~check_sum) + 1;
1602
1603 #else // driver not send config
1604
1605 ts->gtp_cfg_len = GTP_CONFIG_MAX_LENGTH;
1606 ret = gtp_i2c_read(ts->client, config, ts->gtp_cfg_len + GTP_ADDR_LENGTH);
1607 if (ret < 0)
1608 {
1609 GTP_ERROR("Read Config Failed, Using Default Resolution & INT Trigger!");
1610 //ts->abs_x_max = GTP_MAX_WIDTH;
1611 //ts->abs_y_max = GTP_MAX_HEIGHT;
1612 ts->int_trigger_type = GTP_INT_TRIGGER;
1613 }
1614
1615 #endif // GTP_DRIVER_SEND_CFG
1616
1617 if ((ts->abs_x_max == 0) && (ts->abs_y_max == 0))
1618 {
1619 ts->abs_x_max = (config[RESOLUTION_LOC + 1] << 8) + config[RESOLUTION_LOC];
1620 ts->abs_y_max = (config[RESOLUTION_LOC + 3] << 8) + config[RESOLUTION_LOC + 2];
1621 ts->int_trigger_type = (config[TRIGGER_LOC]) & 0x03;
1622 GTP_INFO(" <%s>_%d <%d, %d>\n", __func__, __LINE__, ts->abs_x_max, ts->abs_y_max);
1623 }
1624 GTP_INFO(" <%s>_%d \n", __func__, __LINE__);
1625 #if GTP_COMPATIBLE_MODE
1626 if (CHIP_TYPE_GT9F == ts->chip_type)
1627 {
1628 u8 sensor_num = 0;
1629 u8 driver_num = 0;
1630 u8 have_key = 0;
1631
1632 have_key = (config[GTP_REG_HAVE_KEY - GTP_REG_CONFIG_DATA + 2] & 0x01);
1633
1634 if (1 == ts->is_950)
1635 {
1636 driver_num = config[GTP_REG_MATRIX_DRVNUM - GTP_REG_CONFIG_DATA + 2];
1637 sensor_num = config[GTP_REG_MATRIX_SENNUM - GTP_REG_CONFIG_DATA + 2];
1638 if (have_key)
1639 {
1640 driver_num--;
1641 }
1642 ts->bak_ref_len = (driver_num * (sensor_num - 1) + 2) * 2 * 6;
1643 }
1644 else
1645 {
1646 driver_num = (config[CFG_LOC_DRVA_NUM] & 0x1F) + (config[CFG_LOC_DRVB_NUM]&0x1F);
1647 if (have_key)
1648 {
1649 driver_num--;
1650 }
1651 sensor_num = (config[CFG_LOC_SENS_NUM] & 0x0F) + ((config[CFG_LOC_SENS_NUM] >> 4) & 0x0F);
1652 ts->bak_ref_len = (driver_num * (sensor_num - 2) + 2) * 2;
1653 }
1654
1655 GTP_INFO("Drv * Sen: %d * %d(key: %d), X_MAX: %d, Y_MAX: %d, TRIGGER: 0x%02x",
1656 driver_num, sensor_num, have_key, ts->abs_x_max,ts->abs_y_max,ts->int_trigger_type);
1657 return 0;
1658 }
1659 else
1660 #endif
1661 {
1662 #if GTP_DRIVER_SEND_CFG
1663 GTP_INFO(" <%s>_%d \n", __func__, __LINE__);
1664 ret = gtp_send_cfg(ts->client);
1665 if (ret < 0)
1666 {
1667 GTP_ERROR("Send config error.");
1668 }
1669 // set config version to CTP_CFG_GROUP, for resume to send config
1670 config[GTP_ADDR_LENGTH] = grp_cfg_version;
1671 check_sum = 0;
1672 for (i = GTP_ADDR_LENGTH; i < ts->gtp_cfg_len; i++)
1673 {
1674 check_sum += config[i];
1675 }
1676 config[ts->gtp_cfg_len] = (~check_sum) + 1;
1677 #endif
1678 GTP_INFO("X_MAX: %d, Y_MAX: %d, TRIGGER: 0x%02x", ts->abs_x_max,ts->abs_y_max,ts->int_trigger_type);
1679 }
1680
1681 msleep(10);
1682 return 0;
1683 }
1684
1685
gt91xx_config_read_proc(struct file * file,char __user * page,size_t size,loff_t * ppos)1686 static ssize_t gt91xx_config_read_proc(struct file *file, char __user *page, size_t size, loff_t *ppos)
1687 {
1688 char *ptr = page;
1689 u8 temp_data[GTP_CONFIG_MAX_LENGTH + 2] = {0x80, 0x47};
1690 int i;
1691
1692 if (*ppos)
1693 {
1694 return 0;
1695 }
1696 ptr += sprintf(ptr, "==== GT9XX config init value====\n");
1697
1698 for (i = 0 ; i < GTP_CONFIG_MAX_LENGTH ; i++)
1699 {
1700 ptr += sprintf(ptr, "0x%02X ", config[i + 2]);
1701
1702 if (i % 8 == 7)
1703 ptr += sprintf(ptr, "\n");
1704 }
1705
1706 ptr += sprintf(ptr, "\n");
1707
1708 ptr += sprintf(ptr, "==== GT9XX config real value====\n");
1709 gtp_i2c_read(i2c_connect_client, temp_data, GTP_CONFIG_MAX_LENGTH + 2);
1710 for (i = 0 ; i < GTP_CONFIG_MAX_LENGTH ; i++)
1711 {
1712 ptr += sprintf(ptr, "0x%02X ", temp_data[i+2]);
1713
1714 if (i % 8 == 7)
1715 ptr += sprintf(ptr, "\n");
1716 }
1717 *ppos += ptr - page;
1718 return (ptr - page);
1719 }
1720
gt91xx_config_write_proc(struct file * filp,const char __user * buffer,size_t count,loff_t * off)1721 static ssize_t gt91xx_config_write_proc(struct file *filp, const char __user *buffer, size_t count, loff_t *off)
1722 {
1723 s32 ret = 0;
1724
1725 if (count > GTP_CONFIG_MAX_LENGTH)
1726 {
1727 GTP_ERROR("size not match [%d:%zu]\n", GTP_CONFIG_MAX_LENGTH, count);
1728 return -EFAULT;
1729 }
1730
1731 if (copy_from_user(&config[2], buffer, count))
1732 {
1733 GTP_ERROR("copy from user fail\n");
1734 return -EFAULT;
1735 }
1736
1737 ret = gtp_send_cfg(i2c_connect_client);
1738
1739 if (ret < 0)
1740 {
1741 GTP_ERROR("send config failed.");
1742 }
1743
1744 return count;
1745 }
1746 /*******************************************************
1747 Function:
1748 Read chip version.
1749 Input:
1750 client: i2c device
1751 version: buffer to keep ic firmware version
1752 Output:
1753 read operation return.
1754 2: succeed, otherwise: failed
1755 *******************************************************/
gtp_read_version(struct i2c_client * client,u16 * version)1756 s32 gtp_read_version(struct i2c_client *client, u16* version)
1757 {
1758 s32 ret = -1;
1759 u8 buf[8] = {GTP_REG_VERSION >> 8, GTP_REG_VERSION & 0xff};
1760
1761 GTP_DEBUG_FUNC();
1762
1763 ret = gtp_i2c_read(client, buf, sizeof(buf));
1764 if (ret < 0)
1765 {
1766 GTP_ERROR("GTP read version failed");
1767 return ret;
1768 }
1769
1770 if (version)
1771 {
1772 *version = (buf[7] << 8) | buf[6];
1773 }
1774 if (buf[5] == 0x00)
1775 {
1776 GTP_INFO("IC Version: %c%c%c_%02x%02x", buf[2], buf[3], buf[4], buf[7], buf[6]);
1777 }
1778 else
1779 {
1780 GTP_INFO("IC Version: %c%c%c%c_%02x%02x", buf[2], buf[3], buf[4], buf[5], buf[7], buf[6]);
1781 }
1782 return ret;
1783 }
1784
1785 /*******************************************************
1786 Function:
1787 I2c test Function.
1788 Input:
1789 client:i2c client.
1790 Output:
1791 Executive outcomes.
1792 2: succeed, otherwise failed.
1793 *******************************************************/
gtp_i2c_test(struct i2c_client * client)1794 static s8 gtp_i2c_test(struct i2c_client *client)
1795 {
1796 u8 test[3] = {GTP_REG_CONFIG_DATA >> 8, GTP_REG_CONFIG_DATA & 0xff};
1797 u8 retry = 0;
1798 s8 ret = -1;
1799
1800 GTP_DEBUG_FUNC();
1801
1802 while(retry++ < 5)
1803 {
1804 ret = gtp_i2c_read(client, test, 3);
1805 if (ret > 0)
1806 {
1807 return ret;
1808 }
1809 GTP_ERROR("GTP i2c test failed time %d.",retry);
1810 msleep(10);
1811 }
1812 return ret;
1813 }
1814
1815 /*******************************************************
1816 Function:
1817 Request gpio(INT & RST) ports.
1818 Input:
1819 ts: private data.
1820 Output:
1821 Executive outcomes.
1822 >= 0: succeed, < 0: failed
1823 *******************************************************/
gtp_request_io_port(struct goodix_ts_data * ts)1824 static s8 gtp_request_io_port(struct goodix_ts_data *ts)
1825 {
1826 s32 ret = 0;
1827
1828 GTP_DEBUG_FUNC();
1829 /*
1830 ret = GTP_GPIO_REQUEST(ts->tp_select_pin, "GTP_tp_select_PORT");
1831 if (ret < 0)
1832 {
1833 GTP_ERROR("1Failed to request GPIO:%d, ERRNO:%d",(s32)ts->tp_select_pin, ret);
1834 return -ENODEV;
1835 }
1836 else
1837 {
1838 gpio_direction_input(ts->tp_select_pin);
1839 }
1840 */
1841 ret = GTP_GPIO_REQUEST(ts->rst_pin, "GTP_RST_PORT");
1842 if (ret < 0)
1843 {
1844 GTP_ERROR("2Failed to request GPIO:%d, ERRNO:%d",(s32)ts->rst_pin, ret);
1845 GTP_GPIO_FREE(ts->rst_pin);
1846 return -ENODEV;
1847 }
1848
1849 ret = GTP_GPIO_REQUEST(ts->irq_pin, "GTP_INT_IRQ");
1850 if (ret < 0)
1851 {
1852 GTP_ERROR("3Failed to request GPIO:%d, ERRNO:%d", (s32)ts->irq_pin, ret);
1853 GTP_GPIO_FREE(ts->irq_pin);
1854 return -ENODEV;
1855 }
1856 else
1857 {
1858 //GTP_GPIO_AS_INT(GTP_INT_PORT);
1859 gpio_direction_input(ts->irq_pin);
1860 //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1861 //s3c_gpio_cfgpin(pin, GTP_INT_CFG);
1862
1863 //ts->client->irq = ts->irq_pin;
1864 }
1865
1866 //GTP_GPIO_AS_INPUT(ts->rst_pin);
1867 gpio_direction_input(ts->rst_pin);
1868 //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1869
1870 gtp_reset_guitar(ts->client, 20);
1871
1872 return ret;
1873 }
1874
1875 /*******************************************************
1876 Function:
1877 Request interrupt.
1878 Input:
1879 ts: private data.
1880 Output:
1881 Executive outcomes.
1882 0: succeed, -1: failed.
1883 *******************************************************/
gtp_request_irq(struct goodix_ts_data * ts)1884 static s8 gtp_request_irq(struct goodix_ts_data *ts)
1885 {
1886 s32 ret = -1;
1887
1888 GTP_DEBUG_FUNC();
1889 GTP_DEBUG("INT trigger type:%x", ts->int_trigger_type);
1890
1891 ts->irq=gpio_to_irq(ts->irq_pin); //If not defined in client
1892 if (ts->irq)
1893 {
1894 ts->client->irq = ts->irq;
1895 ret = devm_request_threaded_irq(&(ts->client->dev), ts->irq, NULL,
1896 goodix_ts_irq_handler, ts->irq_flags | IRQF_ONESHOT /*irq_table[ts->int_trigger_type]*/,
1897 ts->client->name, ts);
1898 if (ret != 0) {
1899 GTP_ERROR("Cannot allocate ts INT!ERRNO:%d\n", ret);
1900 goto test_pit;
1901 }
1902 //gtp_irq_disable(ts->irq);
1903 GTP_INFO(" <%s>_%d ts->irq=%d ret = %d\n", __func__, __LINE__, ts->irq, ret);
1904 }else{
1905 GTP_ERROR(" ts->irq error \n");
1906 ret = 1;
1907 goto test_pit;
1908 }
1909 /*
1910 ret = request_irq(ts->client->irq,
1911 goodix_ts_irq_handler,
1912 irq_table[ts->int_trigger_type],
1913 ts->client->name,
1914 ts);
1915 */
1916 test_pit:
1917 if (ret)
1918 {
1919 GTP_ERROR("Request IRQ failed!ERRNO:%d.", ret);
1920 //GTP_GPIO_AS_INPUT(GTP_INT_PORT);
1921 gpio_direction_input(ts->irq_pin);
1922 //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1923
1924 GTP_GPIO_FREE(ts->irq_pin);
1925
1926 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1927 ts->timer.function = goodix_ts_timer_handler;
1928 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
1929 return -1;
1930 }
1931 else
1932 {
1933 GTP_INFO(" <%s>_%d ts->irq=%d ret = %d\n", __func__, __LINE__, ts->irq, ret);
1934 gtp_irq_disable(ts);
1935 ts->use_irq = 1;
1936 return 0;
1937 }
1938 }
1939
1940 /*******************************************************
1941 Function:
1942 Early suspend function.
1943 Input:
1944 h: early_suspend struct.
1945 Output:
1946 None.
1947 *******************************************************/
goodix_ts_early_suspend(struct tp_device * tp_d)1948 static int goodix_ts_early_suspend(struct tp_device *tp_d)
1949 {
1950 struct goodix_ts_data *ts;
1951 s8 ret = -1;
1952 int reg = 0;
1953
1954 ts = container_of(tp_d, struct goodix_ts_data, tp);
1955 GTP_DEBUG_FUNC();
1956
1957 GTP_INFO("System suspend.");
1958
1959 ts->gtp_is_suspend = 1;
1960 #if GTP_ESD_PROTECT
1961 gtp_esd_switch(ts->client, SWITCH_OFF);
1962 #endif
1963
1964 #if GTP_GESTURE_WAKEUP
1965 ret = gtp_enter_doze(ts);
1966 #else
1967 if (ts->use_irq)
1968 {
1969 gtp_irq_disable(ts);
1970 }
1971 else
1972 {
1973 hrtimer_cancel(&ts->timer);
1974 }
1975 ret = gtp_enter_sleep(ts);
1976 #endif
1977 if (ret < 0)
1978 {
1979 printk("GTP early suspend failed.");
1980 }
1981 // to avoid waking up while not sleeping
1982 // delay 48 + 10ms to ensure reliability
1983 msleep(58);
1984
1985 reg = regulator_disable(ts->tp_regulator);
1986 if (reg < 0)
1987 GTP_ERROR("failed to disable tp regulator\n");
1988 msleep(20);
1989 return 0;
1990 }
1991
1992 /*******************************************************
1993 Function:
1994 Late resume function.
1995 Input:
1996 h: early_suspend struct.
1997 Output:
1998 None.
1999 *******************************************************/
goodix_ts_early_resume(struct tp_device * tp_d)2000 static int goodix_ts_early_resume(struct tp_device *tp_d)
2001 {
2002 struct goodix_ts_data *ts;
2003 s8 ret = -1;
2004 int reg = 0;
2005 ts = container_of(tp_d, struct goodix_ts_data, tp);
2006 GTP_DEBUG_FUNC();
2007
2008 GTP_INFO("System resume.");
2009
2010 reg = regulator_enable(ts->tp_regulator);
2011 if (reg < 0)
2012 GTP_ERROR("failed to enable tp regulator\n");
2013 msleep(10);
2014
2015 ret = gtp_wakeup_sleep(ts);
2016
2017 #if GTP_GESTURE_WAKEUP
2018 doze_status = DOZE_DISABLED;
2019 #endif
2020
2021 if (ret < 0)
2022 {
2023 GTP_ERROR("GTP later resume failed.");
2024 }
2025 #if (GTP_COMPATIBLE_MODE)
2026 if (CHIP_TYPE_GT9F == ts->chip_type)
2027 {
2028 // do nothing
2029 }
2030 else
2031 #endif
2032 {
2033 gtp_send_cfg(ts->client);
2034 }
2035
2036 if (ts->use_irq)
2037 {
2038 gtp_irq_enable(ts);
2039 }
2040 else
2041 {
2042 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
2043 }
2044
2045 ts->gtp_is_suspend = 0;
2046 #if GTP_ESD_PROTECT
2047 gtp_esd_switch(ts->client, SWITCH_ON);
2048 #endif
2049
2050 return 0;
2051 }
2052
2053 /*******************************************************
2054 Function:
2055 Request input device Function.
2056 Input:
2057 ts:private data.
2058 Output:
2059 Executive outcomes.
2060 0: succeed, otherwise: failed.
2061 *******************************************************/
gtp_request_input_dev(struct i2c_client * client,struct goodix_ts_data * ts)2062 static s8 gtp_request_input_dev(struct i2c_client *client,
2063 struct goodix_ts_data *ts)
2064 {
2065 s8 ret = -1;
2066 s8 phys[32];
2067 #if GTP_HAVE_TOUCH_KEY
2068 u8 index = 0;
2069 #endif
2070 GTP_DEBUG_FUNC();
2071
2072 ts->input_dev = devm_input_allocate_device(&client->dev);
2073 if (ts->input_dev == NULL)
2074 {
2075 GTP_ERROR("Failed to allocate input device.");
2076 return -ENOMEM;
2077 }
2078
2079 ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;
2080 #if GTP_ICS_SLOT_REPORT
2081 input_mt_init_slots(ts->input_dev, 16, INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); // in case of "out of memory"
2082 #else
2083 ts->input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
2084 #endif
2085 __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);
2086
2087 #if GTP_HAVE_TOUCH_KEY
2088 for (index = 0; index < GTP_MAX_KEY_NUM; index++)
2089 {
2090 input_set_capability(ts->input_dev, EV_KEY, touch_key_array[index]);
2091 }
2092 #endif
2093
2094 #if GTP_GESTURE_WAKEUP
2095 input_set_capability(ts->input_dev, EV_KEY, KEY_POWER);
2096 #endif
2097
2098 if (gtp_change_x2y)
2099 GTP_SWAP(ts->abs_x_max, ts->abs_y_max);
2100
2101 #if defined(CONFIG_CHROME_PLATFORMS)
2102 input_set_abs_params(ts->input_dev, ABS_X, 0, ts->abs_x_max, 0, 0);
2103 input_set_abs_params(ts->input_dev, ABS_Y, 0, ts->abs_y_max, 0, 0);
2104 #endif
2105 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0, ts->abs_x_max, 0, 0);
2106 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0, ts->abs_y_max, 0, 0);
2107 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
2108 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
2109 input_set_abs_params(ts->input_dev, ABS_MT_TRACKING_ID, 0, 255, 0, 0);
2110
2111 sprintf(phys, "input/ts");
2112 ts->input_dev->name = goodix_ts_name;
2113 ts->input_dev->phys = phys;
2114 ts->input_dev->id.bustype = BUS_I2C;
2115 ts->input_dev->id.vendor = 0xDEAD;
2116 ts->input_dev->id.product = 0xBEEF;
2117 ts->input_dev->id.version = 10427;
2118
2119 ret = input_register_device(ts->input_dev);
2120 if (ret)
2121 {
2122 GTP_ERROR("Register %s input device failed", ts->input_dev->name);
2123 return -ENODEV;
2124 }
2125
2126 ts->tp.tp_resume = goodix_ts_early_resume;
2127 ts->tp.tp_suspend = goodix_ts_early_suspend;
2128 tp_register_fb(&ts->tp);
2129
2130 #if GTP_WITH_PEN
2131 gtp_pen_init(ts);
2132 #endif
2133
2134 return 0;
2135 }
2136
2137 //************** For GT9XXF Start *************//
2138 #if GTP_COMPATIBLE_MODE
2139
gtp_fw_startup(struct i2c_client * client)2140 s32 gtp_fw_startup(struct i2c_client *client)
2141 {
2142 u8 opr_buf[4];
2143 s32 ret = 0;
2144 struct goodix_ts_data *ts = i2c_get_clientdata(client);
2145 //init sw WDT
2146 opr_buf[0] = 0xAA;
2147 ret = i2c_write_bytes(client, 0x8041, opr_buf, 1);
2148 if (ret < 0)
2149 {
2150 return FAIL;
2151 }
2152
2153 //release SS51 & DSP
2154 opr_buf[0] = 0x00;
2155 ret = i2c_write_bytes(client, 0x4180, opr_buf, 1);
2156 if (ret < 0)
2157 {
2158 return FAIL;
2159 }
2160 //int sync
2161 gtp_int_sync(25, ts);
2162
2163 //check fw run status
2164 ret = i2c_read_bytes(client, 0x8041, opr_buf, 1);
2165 if (ret < 0)
2166 {
2167 return FAIL;
2168 }
2169 if(0xAA == opr_buf[0])
2170 {
2171 GTP_ERROR("IC works abnormally,startup failed.");
2172 return FAIL;
2173 }
2174 else
2175 {
2176 GTP_INFO("IC works normally, Startup success.");
2177 opr_buf[0] = 0xAA;
2178 i2c_write_bytes(client, 0x8041, opr_buf, 1);
2179 return SUCCESS;
2180 }
2181 }
2182
gtp_esd_recovery(struct i2c_client * client)2183 static s32 gtp_esd_recovery(struct i2c_client *client)
2184 {
2185 s32 retry = 0;
2186 s32 ret = 0;
2187 struct goodix_ts_data *ts;
2188
2189 ts = i2c_get_clientdata(client);
2190
2191 gtp_irq_disable(ts);
2192
2193 GTP_INFO("GT9XXF esd recovery mode");
2194 for (retry = 0; retry < 5; retry++)
2195 {
2196 ret = gup_fw_download_proc(NULL, GTP_FL_ESD_RECOVERY);
2197 if (FAIL == ret)
2198 {
2199 GTP_ERROR("esd recovery failed %d", retry+1);
2200 continue;
2201 }
2202 ret = gtp_fw_startup(ts->client);
2203 if (FAIL == ret)
2204 {
2205 GTP_ERROR("GT9XXF start up failed %d", retry+1);
2206 continue;
2207 }
2208 break;
2209 }
2210 gtp_irq_enable(ts);
2211
2212 if (retry >= 5)
2213 {
2214 GTP_ERROR("failed to esd recovery");
2215 return FAIL;
2216 }
2217
2218 GTP_INFO("Esd recovery successful");
2219 return SUCCESS;
2220 }
2221
gtp_recovery_reset(struct i2c_client * client)2222 void gtp_recovery_reset(struct i2c_client *client)
2223 {
2224 #if GTP_ESD_PROTECT
2225 gtp_esd_switch(client, SWITCH_OFF);
2226 #endif
2227 GTP_DEBUG_FUNC();
2228
2229 gtp_esd_recovery(client);
2230
2231 #if GTP_ESD_PROTECT
2232 gtp_esd_switch(client, SWITCH_ON);
2233 #endif
2234 }
2235
gtp_bak_ref_proc(struct goodix_ts_data * ts,u8 mode)2236 static s32 gtp_bak_ref_proc(struct goodix_ts_data *ts, u8 mode)
2237 {
2238 s32 ret = 0;
2239 s32 i = 0;
2240 s32 j = 0;
2241 u16 ref_sum = 0;
2242 u16 learn_cnt = 0;
2243 u16 chksum = 0;
2244 s32 ref_seg_len = 0;
2245 s32 ref_grps = 0;
2246 struct file *ref_filp = NULL;
2247 u8 *p_bak_ref;
2248
2249 ret = gup_check_fs_mounted("/data");
2250 if (FAIL == ret)
2251 {
2252 ts->ref_chk_fs_times++;
2253 GTP_DEBUG("Ref check /data times/MAX_TIMES: %d / %d", ts->ref_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2254 if (ts->ref_chk_fs_times < GTP_CHK_FS_MNT_MAX)
2255 {
2256 msleep(50);
2257 GTP_INFO("/data not mounted.");
2258 return FAIL;
2259 }
2260 GTP_INFO("check /data mount timeout...");
2261 }
2262 else
2263 {
2264 GTP_INFO("/data mounted!!!(%d/%d)", ts->ref_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2265 }
2266
2267 p_bak_ref = kzalloc(ts->bak_ref_len, GFP_KERNEL);
2268
2269 if (NULL == p_bak_ref)
2270 {
2271 GTP_ERROR("Allocate memory for p_bak_ref failed!");
2272 return FAIL;
2273 }
2274
2275 if (ts->is_950)
2276 {
2277 ref_seg_len = ts->bak_ref_len / 6;
2278 ref_grps = 6;
2279 }
2280 else
2281 {
2282 ref_seg_len = ts->bak_ref_len;
2283 ref_grps = 1;
2284 }
2285 ref_filp = filp_open(GTP_BAK_REF_PATH, O_RDWR | O_CREAT, 0666);
2286 if (IS_ERR(ref_filp))
2287 {
2288 GTP_ERROR("Failed to open/create %s.", GTP_BAK_REF_PATH);
2289 if (GTP_BAK_REF_SEND == mode)
2290 {
2291 goto bak_ref_default;
2292 }
2293 else
2294 {
2295 goto bak_ref_exit;
2296 }
2297 }
2298
2299 switch (mode)
2300 {
2301 case GTP_BAK_REF_SEND:
2302 GTP_INFO("Send backup-reference");
2303 ref_filp->f_op->llseek(ref_filp, 0, SEEK_SET);
2304 ret = ref_filp->f_op->read(ref_filp, (char*)p_bak_ref, ts->bak_ref_len, &ref_filp->f_pos);
2305 if (ret < 0)
2306 {
2307 GTP_ERROR("failed to read bak_ref info from file, sending defualt bak_ref");
2308 goto bak_ref_default;
2309 }
2310 for (j = 0; j < ref_grps; ++j)
2311 {
2312 ref_sum = 0;
2313 for (i = 0; i < (ref_seg_len); i += 2)
2314 {
2315 ref_sum += (p_bak_ref[i + j * ref_seg_len] << 8) + p_bak_ref[i+1 + j * ref_seg_len];
2316 }
2317 learn_cnt = (p_bak_ref[j * ref_seg_len + ref_seg_len -4] << 8) + (p_bak_ref[j * ref_seg_len + ref_seg_len -3]);
2318 chksum = (p_bak_ref[j * ref_seg_len + ref_seg_len -2] << 8) + (p_bak_ref[j * ref_seg_len + ref_seg_len -1]);
2319 GTP_DEBUG("learn count = %d", learn_cnt);
2320 GTP_DEBUG("chksum = %d", chksum);
2321 GTP_DEBUG("ref_sum = 0x%04X", ref_sum & 0xFFFF);
2322 // Sum(1~ref_seg_len) == 1
2323 if (1 != ref_sum)
2324 {
2325 GTP_INFO("wrong chksum for bak_ref, reset to 0x00 bak_ref");
2326 memset(&p_bak_ref[j * ref_seg_len], 0, ref_seg_len);
2327 p_bak_ref[ref_seg_len + j * ref_seg_len - 1] = 0x01;
2328 }
2329 else
2330 {
2331 if (j == (ref_grps - 1))
2332 {
2333 GTP_INFO("backup-reference data in %s used", GTP_BAK_REF_PATH);
2334 }
2335 }
2336 }
2337 ret = i2c_write_bytes(ts->client, GTP_REG_BAK_REF, p_bak_ref, ts->bak_ref_len);
2338 if (FAIL == ret)
2339 {
2340 GTP_ERROR("failed to send bak_ref because of iic comm error");
2341 goto bak_ref_exit;
2342 }
2343 break;
2344
2345 case GTP_BAK_REF_STORE:
2346 GTP_INFO("Store backup-reference");
2347 ret = i2c_read_bytes(ts->client, GTP_REG_BAK_REF, p_bak_ref, ts->bak_ref_len);
2348 if (ret < 0)
2349 {
2350 GTP_ERROR("failed to read bak_ref info, sending default back-reference");
2351 goto bak_ref_default;
2352 }
2353 ref_filp->f_op->llseek(ref_filp, 0, SEEK_SET);
2354 ref_filp->f_op->write(ref_filp, (char*)p_bak_ref, ts->bak_ref_len, &ref_filp->f_pos);
2355 break;
2356
2357 default:
2358 GTP_ERROR("invalid backup-reference request");
2359 break;
2360 }
2361 ret = SUCCESS;
2362 goto bak_ref_exit;
2363
2364 bak_ref_default:
2365
2366 for (j = 0; j < ref_grps; ++j)
2367 {
2368 memset(&p_bak_ref[j * ref_seg_len], 0, ref_seg_len);
2369 p_bak_ref[j * ref_seg_len + ref_seg_len - 1] = 0x01; // checksum = 1
2370 }
2371 ret = i2c_write_bytes(ts->client, GTP_REG_BAK_REF, p_bak_ref, ts->bak_ref_len);
2372 if (!IS_ERR(ref_filp))
2373 {
2374 GTP_INFO("write backup-reference data into %s", GTP_BAK_REF_PATH);
2375 ref_filp->f_op->llseek(ref_filp, 0, SEEK_SET);
2376 ref_filp->f_op->write(ref_filp, (char*)p_bak_ref, ts->bak_ref_len, &ref_filp->f_pos);
2377 }
2378 if (ret == FAIL)
2379 {
2380 GTP_ERROR("failed to load the default backup reference");
2381 }
2382
2383 bak_ref_exit:
2384
2385 if (p_bak_ref)
2386 {
2387 kfree(p_bak_ref);
2388 }
2389 if (ref_filp && !IS_ERR(ref_filp))
2390 {
2391 filp_close(ref_filp, NULL);
2392 }
2393 return ret;
2394 }
2395
2396
gtp_verify_main_clk(u8 * p_main_clk)2397 static s32 gtp_verify_main_clk(u8 *p_main_clk)
2398 {
2399 u8 chksum = 0;
2400 u8 main_clock = p_main_clk[0];
2401 s32 i = 0;
2402
2403 if (main_clock < 50 || main_clock > 120)
2404 {
2405 return FAIL;
2406 }
2407
2408 for (i = 0; i < 5; ++i)
2409 {
2410 if (main_clock != p_main_clk[i])
2411 {
2412 return FAIL;
2413 }
2414 chksum += p_main_clk[i];
2415 }
2416 chksum += p_main_clk[5];
2417 if ( (chksum) == 0)
2418 {
2419 return SUCCESS;
2420 }
2421 else
2422 {
2423 return FAIL;
2424 }
2425 }
2426
gtp_main_clk_proc(struct goodix_ts_data * ts)2427 static s32 gtp_main_clk_proc(struct goodix_ts_data *ts)
2428 {
2429 s32 ret = 0;
2430 s32 i = 0;
2431 s32 clk_chksum = 0;
2432 struct file *clk_filp = NULL;
2433
2434 #if GTP_USE_FIXED_CLK
2435 u8 p_main_clk[6] = {69, 69, 69, 69, 69, 167};
2436 #else
2437 u8 p_main_clk[6] = {0};
2438 #endif
2439
2440 #if GTP_USE_FIXED_CLK
2441 /*
2442 * here check kernel /data is not same as android /data,
2443 * so modify it to reduce fw download
2444 */
2445 ret = gtp_verify_main_clk(p_main_clk);
2446 if (ret == FAIL) {
2447 GTP_ERROR("main clock data verify error");
2448 } else {
2449 GTP_INFO("main clock data used fix freq: %d", p_main_clk[0]);
2450 goto update_main_clk;
2451 }
2452 #else
2453 ret = gup_check_fs_mounted("/data");
2454 if (FAIL == ret)
2455 {
2456 ts->clk_chk_fs_times++;
2457 GTP_DEBUG("Clock check /data times/MAX_TIMES: %d / %d", ts->clk_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2458 if (ts->clk_chk_fs_times < GTP_CHK_FS_MNT_MAX)
2459 {
2460 msleep(50);
2461 GTP_INFO("/data not mounted.");
2462 return FAIL;
2463 }
2464 GTP_INFO("Check /data mount timeout!");
2465 }
2466 else
2467 {
2468 GTP_INFO("/data mounted!!!(%d/%d)", ts->clk_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2469 }
2470
2471 clk_filp = filp_open(GTP_MAIN_CLK_PATH, O_RDWR | O_CREAT, 0666);
2472 if (IS_ERR(clk_filp))
2473 {
2474 GTP_ERROR("%s is unavailable, calculate main clock", GTP_MAIN_CLK_PATH);
2475 }
2476 else
2477 {
2478 clk_filp->f_op->llseek(clk_filp, 0, SEEK_SET);
2479 clk_filp->f_op->read(clk_filp, (char *)p_main_clk, 6, &clk_filp->f_pos);
2480
2481 ret = gtp_verify_main_clk(p_main_clk);
2482 if (FAIL == ret)
2483 {
2484 // recalculate main clock & rewrite main clock data to file
2485 GTP_ERROR("main clock data in %s is wrong, recalculate main clock", GTP_MAIN_CLK_PATH);
2486 }
2487 else
2488 {
2489 GTP_INFO("main clock data in %s used, main clock freq: %d", GTP_MAIN_CLK_PATH, p_main_clk[0]);
2490 filp_close(clk_filp, NULL);
2491 goto update_main_clk;
2492 }
2493 }
2494 #endif
2495
2496 #if GTP_ESD_PROTECT
2497 gtp_esd_switch(ts->client, SWITCH_OFF);
2498 #endif
2499 ret = gup_clk_calibration();
2500 gtp_esd_recovery(ts->client);
2501
2502 #if GTP_ESD_PROTECT
2503 gtp_esd_switch(ts->client, SWITCH_ON);
2504 #endif
2505
2506 GTP_INFO("calibrate main clock: %d", ret);
2507 if (ret < 50 || ret > 120)
2508 {
2509 GTP_ERROR("wrong main clock: %d", ret);
2510 goto exit_main_clk;
2511 }
2512
2513 // Sum{0x8020~0x8025} = 0
2514 for (i = 0; i < 5; ++i)
2515 {
2516 p_main_clk[i] = ret;
2517 clk_chksum += p_main_clk[i];
2518 }
2519 p_main_clk[5] = 0 - clk_chksum;
2520
2521 if (!IS_ERR(clk_filp))
2522 {
2523 GTP_DEBUG("write main clock data into %s", GTP_MAIN_CLK_PATH);
2524 clk_filp->f_op->llseek(clk_filp, 0, SEEK_SET);
2525 clk_filp->f_op->write(clk_filp, (char *)p_main_clk, 6, &clk_filp->f_pos);
2526 filp_close(clk_filp, NULL);
2527 }
2528
2529 update_main_clk:
2530 ret = i2c_write_bytes(ts->client, GTP_REG_MAIN_CLK, p_main_clk, 6);
2531 if (FAIL == ret)
2532 {
2533 GTP_ERROR("update main clock failed!");
2534 return FAIL;
2535 }
2536 return SUCCESS;
2537
2538 exit_main_clk:
2539 if (!IS_ERR(clk_filp))
2540 {
2541 filp_close(clk_filp, NULL);
2542 }
2543 return FAIL;
2544 }
2545
2546
gtp_gt9xxf_init(struct i2c_client * client)2547 s32 gtp_gt9xxf_init(struct i2c_client *client)
2548 {
2549 s32 ret = 0;
2550
2551 ret = gup_fw_download_proc(NULL, GTP_FL_FW_BURN);
2552 if (FAIL == ret)
2553 {
2554 return FAIL;
2555 }
2556
2557 ret = gtp_fw_startup(client);
2558 if (FAIL == ret)
2559 {
2560 return FAIL;
2561 }
2562 return SUCCESS;
2563 }
2564
gtp_get_chip_type(struct goodix_ts_data * ts)2565 void gtp_get_chip_type(struct goodix_ts_data *ts)
2566 {
2567 u8 opr_buf[10] = {0x00};
2568 s32 ret = 0;
2569
2570 msleep(10);
2571
2572 ret = gtp_i2c_read_dbl_check(ts->client, GTP_REG_CHIP_TYPE, opr_buf, 10);
2573
2574 if (FAIL == ret)
2575 {
2576 GTP_ERROR("Failed to get chip-type, set chip type default: GOODIX_GT9");
2577 ts->chip_type = CHIP_TYPE_GT9;
2578 return;
2579 }
2580
2581 if (!memcmp(opr_buf, "GOODIX_GT9", 10))
2582 {
2583 ts->chip_type = CHIP_TYPE_GT9;
2584 }
2585 else if (bgt9110)
2586 {
2587 ts->chip_type = CHIP_TYPE_GT9110;
2588 GTP_INFO("Chip Type: GOODIX_GT9110");
2589 return;
2590 }
2591 else // GT9XXF
2592 {
2593 ts->chip_type = CHIP_TYPE_GT9F;
2594 }
2595 GTP_INFO("Chip Type: %s", (ts->chip_type == CHIP_TYPE_GT9) ? "GOODIX_GT9" : "GOODIX_GT9F");
2596 }
2597
2598 #endif
2599 //************* For GT9XXF End ************//
2600
2601 /*******************************************************
2602 Function:
2603 I2c probe.
2604 Input:
2605 client: i2c device struct.
2606 id: device id.
2607 Output:
2608 Executive outcomes.
2609 0: succeed.
2610 *******************************************************/
goodix_ts_probe(struct i2c_client * client,const struct i2c_device_id * id)2611 static int goodix_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
2612 {
2613 s32 ret = -1;
2614 struct goodix_ts_data *ts;
2615 u16 version_info;
2616
2617 struct device_node *np = client->dev.of_node;
2618 enum of_gpio_flags rst_flags, pwr_flags;
2619 u32 val;
2620 printk("%s() start\n", __func__);
2621
2622
2623 GTP_DEBUG_FUNC();
2624
2625 //do NOT remove these logs
2626 GTP_INFO("GTP Driver Version: %s", GTP_DRIVER_VERSION);
2627 GTP_INFO("GTP I2C Address: 0x%02x", client->addr);
2628
2629 i2c_connect_client = client;
2630
2631 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
2632 {
2633 GTP_ERROR("I2C check functionality failed.");
2634 return -ENODEV;
2635 }
2636 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
2637 if (ts == NULL)
2638 {
2639 GTP_ERROR("Alloc GFP_KERNEL memory failed.");
2640 return -ENOMEM;
2641 }
2642
2643 memset(ts, 0, sizeof(*ts));
2644
2645 if (!np) {
2646 dev_err(&client->dev, "no device tree\n");
2647 return -EINVAL;
2648 }
2649 if (of_property_read_u32(np, "tp-size", &val)) {
2650 dev_err(&client->dev, "no max-x defined\n");
2651 return -EINVAL;
2652 }
2653
2654 if (val == 89) {
2655 m89or101 = TRUE;
2656 gtp_change_x2y = TRUE;
2657 gtp_x_reverse = FALSE;
2658 gtp_y_reverse = TRUE;
2659 } else if (val == 101) {
2660 m89or101 = FALSE;
2661 gtp_change_x2y = TRUE;
2662 gtp_x_reverse = TRUE;
2663 gtp_y_reverse = FALSE;
2664 } else if (val == 911) {
2665 m89or101 = FALSE;
2666 bgt911 = TRUE;
2667 gtp_change_x2y = TRUE;
2668 gtp_x_reverse = FALSE;
2669 gtp_y_reverse = TRUE;
2670 } else if (val == 9110) {
2671 m89or101 = FALSE;
2672 bgt9110 = TRUE;
2673 gtp_change_x2y = TRUE;
2674 gtp_x_reverse = TRUE;
2675 gtp_y_reverse = FALSE;
2676 } else if (val == 9111) {
2677 m89or101 = FALSE;
2678 bgt9111 = TRUE;
2679 gtp_change_x2y = TRUE;
2680 gtp_x_reverse = FALSE;
2681 gtp_y_reverse = FALSE;
2682 } else if (val == 970) {
2683 m89or101 = FALSE;
2684 bgt911 = FALSE;
2685 bgt970 = TRUE;
2686 gtp_change_x2y = FALSE;
2687 gtp_x_reverse = FALSE;
2688 gtp_y_reverse = TRUE;
2689 } else if (val == 910) {
2690 m89or101 = FALSE;
2691 bgt911 = FALSE;
2692 bgt970 = FALSE;
2693 bgt910 = TRUE;
2694 gtp_change_x2y = TRUE;
2695 gtp_x_reverse = FALSE;
2696 gtp_y_reverse = TRUE;
2697 }
2698
2699 ts->tp_regulator = devm_regulator_get(&client->dev, "tp");
2700 if (IS_ERR(ts->tp_regulator)) {
2701 dev_err(&client->dev, "failed to get regulator, %ld\n",
2702 PTR_ERR(ts->tp_regulator));
2703 return PTR_ERR(ts->tp_regulator);
2704 }
2705
2706 ret = regulator_enable(ts->tp_regulator);
2707 if (ret < 0)
2708 GTP_ERROR("failed to enable tp regulator\n");
2709 msleep(20);
2710
2711 ts->irq_pin = of_get_named_gpio_flags(np, "touch-gpio", 0, (enum of_gpio_flags *)(&ts->irq_flags));
2712 ts->rst_pin = of_get_named_gpio_flags(np, "reset-gpio", 0, &rst_flags);
2713 ts->pwr_pin = of_get_named_gpio_flags(np, "power-gpio", 0, &pwr_flags);
2714 //ts->tp_select_pin = of_get_named_gpio_flags(np, "tp-select-gpio", 0, &tp_select_flags);
2715 if (of_property_read_u32(np, "max-x", &val)) {
2716 dev_err(&client->dev, "no max-x defined\n");
2717 return -EINVAL;
2718 }
2719 //ts->abs_x_max = val;
2720 if (of_property_read_u32(np, "max-y", &val)) {
2721 dev_err(&client->dev, "no max-y defined\n");
2722 return -EINVAL;
2723 }
2724 //ts->abs_y_max = val;
2725 if (of_property_read_u32(np, "configfile-num", &val)) {
2726 ts->cfg_file_num = 0;
2727 } else {
2728 ts->cfg_file_num = val;
2729 }
2730 ts->pendown =PEN_RELEASE;
2731 ts->client = client;
2732
2733
2734 INIT_WORK(&ts->work, goodix_ts_work_func);
2735 ts->client = client;
2736 spin_lock_init(&ts->irq_lock); // 2.6.39 later
2737 // ts->irq_lock = SPIN_LOCK_UNLOCKED; // 2.6.39 & before
2738 #if GTP_ESD_PROTECT
2739 ts->clk_tick_cnt = 2 * HZ; // HZ: clock ticks in 1 second generated by system
2740 GTP_DEBUG("Clock ticks for an esd cycle: %d", ts->clk_tick_cnt);
2741 spin_lock_init(&ts->esd_lock);
2742 // ts->esd_lock = SPIN_LOCK_UNLOCKED;
2743 #endif
2744
2745 i2c_set_clientdata(client, ts);
2746
2747 ts->gtp_rawdiff_mode = 0;
2748
2749 ret = gtp_request_io_port(ts);
2750 if (ret < 0)
2751 {
2752 GTP_ERROR("GTP request IO port failed.");
2753 //return ret;
2754 goto probe_init_error_requireio;
2755 }
2756 /*
2757 if(gpio_get_value(ts->tp_select_pin))//WGJ
2758 {
2759 printk("tp 11111111111111111111111111111 WGJ\n\n");
2760 gtp_x_reverse = FALSE;
2761 gtp_y_reverse = TRUE;
2762 }
2763 else//DPT
2764 {
2765 printk("tp 00000000000000000000000000000 DPT\n\n");
2766 gtp_x_reverse = TRUE;//FALSE;
2767 gtp_y_reverse = TRUE;
2768 }
2769 */
2770 #if GTP_COMPATIBLE_MODE
2771 gtp_get_chip_type(ts);
2772
2773 if (CHIP_TYPE_GT9F == ts->chip_type)
2774 {
2775 ret = gtp_gt9xxf_init(ts->client);
2776 if (FAIL == ret)
2777 {
2778 GTP_INFO("Failed to init GT9XXF.");
2779 }
2780 }
2781 #endif
2782
2783 ret = gtp_i2c_test(client);
2784 if (ret < 0)
2785 {
2786 printk("<%s>_%d I2C communication ERROR!\n", __func__, __LINE__);
2787 goto probe_init_error;
2788 }
2789
2790 ret = gtp_read_version(client, &version_info);
2791 if (ret < 0)
2792 {
2793 GTP_ERROR("Read version failed.");
2794 }
2795
2796 ret = gtp_init_panel(ts);
2797 if (ret < 0)
2798 {
2799 GTP_ERROR("GTP init panel failed.");
2800 //ts->abs_x_max = GTP_MAX_WIDTH;
2801 //ts->abs_y_max = GTP_MAX_HEIGHT;
2802 ts->int_trigger_type = GTP_INT_TRIGGER;
2803 }
2804
2805 ts->irq_flags = ts->int_trigger_type ? IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
2806 // Create proc file system
2807 gt91xx_config_proc = proc_create(GT91XX_CONFIG_PROC_FILE, 0664, NULL, &config_proc_ops);
2808 if (gt91xx_config_proc == NULL)
2809 {
2810 GTP_ERROR("create_proc_entry %s failed\n", GT91XX_CONFIG_PROC_FILE);
2811 }
2812 else
2813 {
2814 GTP_INFO("create proc entry %s success", GT91XX_CONFIG_PROC_FILE);
2815 }
2816
2817 #if GTP_AUTO_UPDATE
2818 ret = gup_init_update_proc(ts);
2819 if (ret < 0)
2820 {
2821 GTP_ERROR("Create update thread error.");
2822 }
2823 #endif
2824
2825 ret = gtp_request_input_dev(client, ts);
2826 if (ret < 0)
2827 {
2828 GTP_ERROR("GTP request input dev failed");
2829 }
2830 ret = gtp_request_irq(ts);
2831 if (ret < 0)
2832 {
2833 GTP_INFO("GTP works in polling mode.");
2834 }
2835 else
2836 {
2837 GTP_INFO("GTP works in interrupt mode.");
2838 }
2839
2840 if (ts->use_irq)
2841 {
2842 gtp_irq_enable(ts);
2843 }
2844
2845 if (of_property_read_bool(np, "wakeup-source"))
2846 {
2847 device_init_wakeup(&client->dev, 1);
2848 enable_irq_wake(ts->irq);
2849 }
2850
2851 #if GTP_CREATE_WR_NODE
2852 init_wr_node(client);
2853 #endif
2854
2855 #if GTP_ESD_PROTECT
2856 gtp_esd_switch(client, SWITCH_ON);
2857 #endif
2858 return 0;
2859
2860 probe_init_error:
2861 printk(" <%s>_%d prob error !!!!!!!!!!!!!!!\n", __func__, __LINE__);
2862 GTP_GPIO_FREE(ts->rst_pin);
2863 GTP_GPIO_FREE(ts->irq_pin);
2864 probe_init_error_requireio:
2865 tp_unregister_fb(&ts->tp);
2866 kfree(ts);
2867 return ret;
2868 }
2869
2870
2871 /*******************************************************
2872 Function:
2873 Goodix touchscreen driver release function.
2874 Input:
2875 client: i2c device struct.
2876 Output:
2877 Executive outcomes. 0---succeed.
2878 *******************************************************/
goodix_ts_remove(struct i2c_client * client)2879 static int goodix_ts_remove(struct i2c_client *client)
2880 {
2881 struct goodix_ts_data *ts = i2c_get_clientdata(client);
2882
2883 tp_unregister_fb(&ts->tp);
2884
2885 GTP_DEBUG_FUNC();
2886
2887 #if GTP_CREATE_WR_NODE
2888 uninit_wr_node();
2889 #endif
2890
2891 #if GTP_ESD_PROTECT
2892 destroy_workqueue(gtp_esd_check_workqueue);
2893 #endif
2894
2895 if (ts)
2896 {
2897 if (ts->use_irq)
2898 {
2899 //GTP_GPIO_AS_INPUT(GTP_INT_PORT);
2900
2901 gpio_direction_input(ts->irq_pin);
2902 //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
2903
2904 GTP_GPIO_FREE(ts->irq_pin);
2905 free_irq(client->irq, ts);
2906 }
2907 else
2908 {
2909 hrtimer_cancel(&ts->timer);
2910 }
2911 GTP_INFO("GTP driver removing...");
2912 i2c_set_clientdata(client, NULL);
2913 input_unregister_device(ts->input_dev);
2914 kfree(ts);
2915 }
2916
2917 return 0;
2918 }
2919
2920
2921
2922
2923
2924 #if GTP_ESD_PROTECT
gtp_i2c_read_no_rst(struct i2c_client * client,u8 * buf,s32 len)2925 s32 gtp_i2c_read_no_rst(struct i2c_client *client, u8 *buf, s32 len)
2926 {
2927 struct i2c_msg msgs[2];
2928 s32 ret=-1;
2929 s32 retries = 0;
2930
2931 GTP_DEBUG_FUNC();
2932
2933 msgs[0].flags = !I2C_M_RD;
2934 msgs[0].addr = client->addr;
2935 msgs[0].len = GTP_ADDR_LENGTH;
2936 msgs[0].buf = &buf[0];
2937 //msgs[0].scl_rate = 300 * 1000; // for Rockchip, etc.
2938
2939 msgs[1].flags = I2C_M_RD;
2940 msgs[1].addr = client->addr;
2941 msgs[1].len = len - GTP_ADDR_LENGTH;
2942 msgs[1].buf = &buf[GTP_ADDR_LENGTH];
2943 //msgs[1].scl_rate = 300 * 1000;
2944
2945 while(retries < 5)
2946 {
2947 ret = i2c_transfer(client->adapter, msgs, 2);
2948 if(ret == 2)break;
2949 retries++;
2950 }
2951 if ((retries >= 5))
2952 {
2953 GTP_ERROR("I2C Read: 0x%04X, %d bytes failed, errcode: %d!", (((u16)(buf[0] << 8)) | buf[1]), len-2, ret);
2954 }
2955 return ret;
2956 }
2957
gtp_i2c_write_no_rst(struct i2c_client * client,u8 * buf,s32 len)2958 s32 gtp_i2c_write_no_rst(struct i2c_client *client,u8 *buf,s32 len)
2959 {
2960 struct i2c_msg msg;
2961 s32 ret = -1;
2962 s32 retries = 0;
2963
2964 GTP_DEBUG_FUNC();
2965
2966 msg.flags = !I2C_M_RD;
2967 msg.addr = client->addr;
2968 msg.len = len;
2969 msg.buf = buf;
2970 //msg.scl_rate = 300 * 1000; // for Rockchip, etc
2971
2972 while(retries < 5)
2973 {
2974 ret = i2c_transfer(client->adapter, &msg, 1);
2975 if (ret == 1)break;
2976 retries++;
2977 }
2978 if((retries >= 5))
2979 {
2980 GTP_ERROR("I2C Write: 0x%04X, %d bytes failed, errcode: %d!", (((u16)(buf[0] << 8)) | buf[1]), len-2, ret);
2981 }
2982 return ret;
2983 }
2984 /*******************************************************
2985 Function:
2986 switch on & off esd delayed work
2987 Input:
2988 client: i2c device
2989 on: SWITCH_ON / SWITCH_OFF
2990 Output:
2991 void
2992 *********************************************************/
gtp_esd_switch(struct i2c_client * client,s32 on)2993 void gtp_esd_switch(struct i2c_client *client, s32 on)
2994 {
2995 struct goodix_ts_data *ts;
2996
2997 ts = i2c_get_clientdata(client);
2998 spin_lock(&ts->esd_lock);
2999
3000 if (SWITCH_ON == on) // switch on esd
3001 {
3002 if (!ts->esd_running)
3003 {
3004 ts->esd_running = 1;
3005 spin_unlock(&ts->esd_lock);
3006 GTP_INFO("Esd started");
3007 queue_delayed_work(gtp_esd_check_workqueue, >p_esd_check_work, ts->clk_tick_cnt);
3008 }
3009 else
3010 {
3011 spin_unlock(&ts->esd_lock);
3012 }
3013 }
3014 else // switch off esd
3015 {
3016 if (ts->esd_running)
3017 {
3018 ts->esd_running = 0;
3019 spin_unlock(&ts->esd_lock);
3020 GTP_INFO("Esd cancelled");
3021 cancel_delayed_work_sync(>p_esd_check_work);
3022 }
3023 else
3024 {
3025 spin_unlock(&ts->esd_lock);
3026 }
3027 }
3028 }
3029
3030 /*******************************************************
3031 Function:
3032 Initialize external watchdog for esd protect
3033 Input:
3034 client: i2c device.
3035 Output:
3036 result of i2c write operation.
3037 1: succeed, otherwise: failed
3038 *********************************************************/
gtp_init_ext_watchdog(struct i2c_client * client)3039 static s32 gtp_init_ext_watchdog(struct i2c_client *client)
3040 {
3041 u8 opr_buffer[3] = {0x80, 0x41, 0xAA};
3042 GTP_DEBUG("[Esd]Init external watchdog");
3043 return gtp_i2c_write_no_rst(client, opr_buffer, 3);
3044 }
3045
3046 /*******************************************************
3047 Function:
3048 Esd protect function.
3049 External watchdog added by meta, 2013/03/07
3050 Input:
3051 work: delayed work
3052 Output:
3053 None.
3054 *******************************************************/
gtp_esd_check_func(struct work_struct * work)3055 static void gtp_esd_check_func(struct work_struct *work)
3056 {
3057 s32 i;
3058 s32 ret = -1;
3059 struct goodix_ts_data *ts = NULL;
3060 u8 esd_buf[5] = {0x80, 0x40};
3061
3062 GTP_DEBUG_FUNC();
3063
3064 ts = i2c_get_clientdata(i2c_connect_client);
3065
3066 if (ts->gtp_is_suspend)
3067 {
3068 GTP_INFO("Esd suspended!");
3069 return;
3070 }
3071
3072 for (i = 0; i < 3; i++)
3073 {
3074 ret = gtp_i2c_read_no_rst(ts->client, esd_buf, 4);
3075
3076 GTP_DEBUG("[Esd]0x8040 = 0x%02X, 0x8041 = 0x%02X", esd_buf[2], esd_buf[3]);
3077 if ((ret < 0))
3078 {
3079 // IIC communication problem
3080 continue;
3081 }
3082 else
3083 {
3084 if ((esd_buf[2] == 0xAA) || (esd_buf[3] != 0xAA))
3085 {
3086 // IC works abnormally..
3087 u8 chk_buf[4] = {0x80, 0x40};
3088
3089 gtp_i2c_read_no_rst(ts->client, chk_buf, 4);
3090
3091 GTP_DEBUG("[Check]0x8040 = 0x%02X, 0x8041 = 0x%02X", chk_buf[2], chk_buf[3]);
3092
3093 if ((chk_buf[2] == 0xAA) || (chk_buf[3] != 0xAA))
3094 {
3095 i = 3;
3096 break;
3097 }
3098 else
3099 {
3100 continue;
3101 }
3102 }
3103 else
3104 {
3105 // IC works normally, Write 0x8040 0xAA, feed the dog
3106 esd_buf[2] = 0xAA;
3107 gtp_i2c_write_no_rst(ts->client, esd_buf, 3);
3108 break;
3109 }
3110 }
3111 }
3112 if (i >= 3)
3113 {
3114 #if GTP_COMPATIBLE_MODE
3115 if (CHIP_TYPE_GT9F == ts->chip_type)
3116 {
3117 if (ts->rqst_processing)
3118 {
3119 GTP_INFO("Request processing, no esd recovery");
3120 }
3121 else
3122 {
3123 GTP_ERROR("IC working abnormally! Process esd recovery.");
3124 esd_buf[0] = 0x42;
3125 esd_buf[1] = 0x26;
3126 esd_buf[2] = 0x01;
3127 esd_buf[3] = 0x01;
3128 esd_buf[4] = 0x01;
3129 gtp_i2c_write_no_rst(ts->client, esd_buf, 5);
3130 msleep(50);
3131 gtp_esd_recovery(ts->client);
3132 }
3133 }
3134 else
3135 #endif
3136 {
3137 GTP_ERROR("IC working abnormally! Process reset guitar.");
3138 esd_buf[0] = 0x42;
3139 esd_buf[1] = 0x26;
3140 esd_buf[2] = 0x01;
3141 esd_buf[3] = 0x01;
3142 esd_buf[4] = 0x01;
3143 gtp_i2c_write_no_rst(ts->client, esd_buf, 5);
3144 msleep(50);
3145 gtp_reset_guitar(ts->client, 50);
3146 msleep(50);
3147 gtp_send_cfg(ts->client);
3148 }
3149 }
3150
3151 if(!ts->gtp_is_suspend)
3152 {
3153 queue_delayed_work(gtp_esd_check_workqueue, >p_esd_check_work, ts->clk_tick_cnt);
3154 }
3155 else
3156 {
3157 GTP_INFO("Esd suspended!");
3158 }
3159 return;
3160 }
3161 #endif
3162
3163 static const struct i2c_device_id goodix_ts_id[] = {
3164 { GTP_I2C_NAME, 0 },
3165 { }
3166 };
3167
3168 static struct of_device_id goodix_ts_dt_ids[] = {
3169 { .compatible = "goodix,gt9xx" },
3170 { }
3171 };
3172
3173 static struct i2c_driver goodix_ts_driver = {
3174 .probe = goodix_ts_probe,
3175 .remove = goodix_ts_remove,
3176 .id_table = goodix_ts_id,
3177 .driver = {
3178 .name = GTP_I2C_NAME,
3179 .of_match_table = of_match_ptr(goodix_ts_dt_ids),
3180 },
3181 };
3182
3183 /*******************************************************
3184 Function:
3185 Driver Install function.
3186 Input:
3187 None.
3188 Output:
3189 Executive Outcomes. 0---succeed.
3190 ********************************************************/
goodix_ts_init(void)3191 static int goodix_ts_init(void)
3192 {
3193 s32 ret;
3194
3195 GTP_DEBUG_FUNC();
3196 GTP_INFO("GTP driver installing...");
3197 goodix_wq = create_singlethread_workqueue("goodix_wq");
3198 if (!goodix_wq)
3199 {
3200 GTP_ERROR("Creat workqueue failed.");
3201 return -ENOMEM;
3202 }
3203 #if GTP_ESD_PROTECT
3204 INIT_DELAYED_WORK(>p_esd_check_work, gtp_esd_check_func);
3205 gtp_esd_check_workqueue = create_workqueue("gtp_esd_check");
3206 #endif
3207 ret = i2c_add_driver(&goodix_ts_driver);
3208 return ret;
3209 }
3210
3211 /*******************************************************
3212 Function:
3213 Driver uninstall function.
3214 Input:
3215 None.
3216 Output:
3217 Executive Outcomes. 0---succeed.
3218 ********************************************************/
goodix_ts_exit(void)3219 static void goodix_ts_exit(void)
3220 {
3221 GTP_DEBUG_FUNC();
3222 GTP_INFO("GTP driver exited.");
3223 i2c_del_driver(&goodix_ts_driver);
3224 if (goodix_wq)
3225 {
3226 destroy_workqueue(goodix_wq);
3227 }
3228 }
3229 //late_initcall(goodix_ts_init);
3230 module_init(goodix_ts_init);
3231
3232 module_exit(goodix_ts_exit);
3233
3234 MODULE_DESCRIPTION("GTP Series Driver");
3235 MODULE_LICENSE("GPL");
3236