1 /* drivers/input/touchscreen/gt9xx.c
3 * 2010 - 2013 Goodix Technology.
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.
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.
17 * Authors: andrew@goodix.com, meta@goodix.com
18 * Release Date: 2014/01/14
21 * first Release. By Andrew, 2012/08/31
23 * modify gtp_reset_guitar,slot report,tracking_id & 0x0F. By Andrew, 2012/10/15
25 * modify gt9xx_update.c. By Andrew, 2012/12/12
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
34 * 1. pen/stylus identification
35 * 2. read double check & fixed config support
36 * 3. new esd & slide wakeup optimization
39 * 1. compatible with GT9XXF
40 * 2. send config after resume
43 * 1. gt9xx_config for debug
45 * 3. pen separate input device, active-pen button support
46 * 4. coordinates & keys optimization
50 #include <linux/irq.h>
52 #include <linux/regulator/consumer.h>
54 #if GTP_ICS_SLOT_REPORT
55 #include <linux/input/mt.h>
58 static const char *goodix_ts_name = "goodix-ts";
59 static struct workqueue_struct *goodix_wq;
60 struct i2c_client * i2c_connect_client = NULL;
61 u8 config[GTP_CONFIG_MAX_LENGTH + GTP_ADDR_LENGTH]
62 = {GTP_REG_CONFIG_DATA >> 8, GTP_REG_CONFIG_DATA & 0xff};
64 #if GTP_HAVE_TOUCH_KEY
65 static const u16 touch_key_array[] = GTP_KEY_TAB;
66 #define GTP_MAX_KEY_NUM (sizeof(touch_key_array)/sizeof(touch_key_array[0]))
69 static const int key_codes[] = {KEY_HOME, KEY_BACK, KEY_MENU, KEY_SEARCH};
70 static const char *key_names[] = {"Key_Home", "Key_Back", "Key_Menu", "Key_Search"};
75 static s8 gtp_i2c_test(struct i2c_client *client);
76 void gtp_reset_guitar(struct i2c_client *client, s32 ms);
77 s32 gtp_send_cfg(struct i2c_client *client);
78 void gtp_int_sync(s32 ms, struct goodix_ts_data *ts);
80 static ssize_t gt91xx_config_read_proc(struct file *, char __user *, size_t, loff_t *);
81 static ssize_t gt91xx_config_write_proc(struct file *, const char __user *, size_t, loff_t *);
83 static struct proc_dir_entry *gt91xx_config_proc = NULL;
84 static const struct file_operations config_proc_ops = {
86 .read = gt91xx_config_read_proc,
87 .write = gt91xx_config_write_proc,
90 #if GTP_CREATE_WR_NODE
91 extern s32 init_wr_node(struct i2c_client*);
92 extern void uninit_wr_node(void);
96 extern u8 gup_init_update_proc(struct goodix_ts_data *);
100 static struct delayed_work gtp_esd_check_work;
101 static struct workqueue_struct * gtp_esd_check_workqueue = NULL;
102 static void gtp_esd_check_func(struct work_struct *);
103 static s32 gtp_init_ext_watchdog(struct i2c_client *client);
104 void gtp_esd_switch(struct i2c_client *, s32);
107 //*********** For GT9XXF Start **********//
108 #if GTP_COMPATIBLE_MODE
109 extern s32 i2c_read_bytes(struct i2c_client *client, u16 addr, u8 *buf, s32 len);
110 extern s32 i2c_write_bytes(struct i2c_client *client, u16 addr, u8 *buf, s32 len);
111 extern s32 gup_clk_calibration(void);
112 extern s32 gup_fw_download_proc(void *dir, u8 dwn_mode);
113 extern u8 gup_check_fs_mounted(char *path_name);
115 void gtp_recovery_reset(struct i2c_client *client);
116 static s32 gtp_esd_recovery(struct i2c_client *client);
117 s32 gtp_fw_startup(struct i2c_client *client);
118 static s32 gtp_main_clk_proc(struct goodix_ts_data *ts);
119 static s32 gtp_bak_ref_proc(struct goodix_ts_data *ts, u8 mode);
122 //********** For GT9XXF End **********//
124 #if GTP_GESTURE_WAKEUP
131 static DOZE_T doze_status = DOZE_DISABLED;
132 static s8 gtp_enter_doze(struct goodix_ts_data *ts);
135 u8 grp_cfg_version = 0;
137 /*******************************************************
139 Read data from the i2c slave device.
142 buf[0~1]: read start address.
143 buf[2~len-1]: read data buffer.
144 len: GTP_ADDR_LENGTH + read bytes count
146 numbers of i2c_msgs to transfer:
147 2: succeed, otherwise: failed
148 *********************************************************/
149 s32 gtp_i2c_read(struct i2c_client *client, u8 *buf, s32 len)
151 struct i2c_msg msgs[2];
157 msgs[0].flags = !I2C_M_RD;
158 msgs[0].addr = client->addr;
159 msgs[0].len = GTP_ADDR_LENGTH;
160 msgs[0].buf = &buf[0];
161 #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
162 msgs[0].scl_rate=200 * 1000;
163 //msgs[0].scl_rate = 300 * 1000; // for Rockchip, etc.
165 msgs[1].flags = I2C_M_RD;
166 msgs[1].addr = client->addr;
167 msgs[1].len = len - GTP_ADDR_LENGTH;
168 msgs[1].buf = &buf[GTP_ADDR_LENGTH];
169 #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
170 msgs[1].scl_rate=200 * 1000;
171 //msgs[1].scl_rate = 300 * 1000; // for Rockchip, etc.
176 ret = i2c_transfer(client->adapter, msgs, 2);
182 #if GTP_COMPATIBLE_MODE
183 struct goodix_ts_data *ts = i2c_get_clientdata(client);
186 #if GTP_GESTURE_WAKEUP
187 // reset chip would quit doze mode
188 if (DOZE_ENABLED == doze_status)
193 GTP_ERROR("I2C Read: 0x%04X, %d bytes failed, errcode: %d! Process reset.", (((u16)(buf[0] << 8)) | buf[1]), len-2, ret);
194 #if GTP_COMPATIBLE_MODE
195 if (CHIP_TYPE_GT9F == ts->chip_type)
197 gtp_recovery_reset(client);
202 gtp_reset_guitar(client, 10);
210 /*******************************************************
212 Write data to the i2c slave device.
215 buf[0~1]: write start address.
216 buf[2~len-1]: data buffer
217 len: GTP_ADDR_LENGTH + write bytes count
219 numbers of i2c_msgs to transfer:
220 1: succeed, otherwise: failed
221 *********************************************************/
222 s32 gtp_i2c_write(struct i2c_client *client,u8 *buf,s32 len)
230 msg.flags = !I2C_M_RD;
231 msg.addr = client->addr;
234 #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
235 msg.scl_rate=200 * 1000;
236 //msg.scl_rate = 300 * 1000; // for Rockchip, etc
240 ret = i2c_transfer(client->adapter, &msg, 1);
246 #if GTP_COMPATIBLE_MODE
247 struct goodix_ts_data *ts = i2c_get_clientdata(client);
250 #if GTP_GESTURE_WAKEUP
251 if (DOZE_ENABLED == doze_status)
256 GTP_ERROR("I2C Write: 0x%04X, %d bytes failed, errcode: %d! Process reset.", (((u16)(buf[0] << 8)) | buf[1]), len-2, ret);
257 #if GTP_COMPATIBLE_MODE
258 if (CHIP_TYPE_GT9F == ts->chip_type)
260 gtp_recovery_reset(client);
265 gtp_reset_guitar(client, 10);
272 /*******************************************************
274 i2c read twice, compare the results
277 addr: operate address
278 rxbuf: read data to store, if compare successful
282 SUCCESS: read successful
283 *********************************************************/
284 s32 gtp_i2c_read_dbl_check(struct i2c_client *client, u16 addr, u8 *rxbuf, int len)
287 u8 confirm_buf[16] = {0};
292 memset(buf, 0xAA, 16);
293 buf[0] = (u8)(addr >> 8);
294 buf[1] = (u8)(addr & 0xFF);
295 gtp_i2c_read(client, buf, len + 2);
297 memset(confirm_buf, 0xAB, 16);
298 confirm_buf[0] = (u8)(addr >> 8);
299 confirm_buf[1] = (u8)(addr & 0xFF);
300 gtp_i2c_read(client, confirm_buf, len + 2);
302 if (!memcmp(buf, confirm_buf, len+2))
304 memcpy(rxbuf, confirm_buf+2, len);
308 GTP_ERROR("I2C read 0x%04X, %d bytes, double check failed!", addr, len);
312 /*******************************************************
318 result of i2c write operation.
319 1: succeed, otherwise: failed
320 *********************************************************/
322 s32 gtp_send_cfg(struct i2c_client *client)
326 #if GTP_DRIVER_SEND_CFG
328 struct goodix_ts_data *ts = i2c_get_clientdata(client);
332 GTP_INFO("Ic fixed config, no config sent!");
335 else if (ts->pnl_init_error)
337 GTP_INFO("Error occured in init_panel, no config sent");
341 GTP_INFO("Driver send config.");
342 for (retry = 0; retry < 5; retry++)
344 ret = gtp_i2c_write(client, config , GTP_CONFIG_MAX_LENGTH + GTP_ADDR_LENGTH);
353 /*******************************************************
357 ts: goodix i2c_client private data
360 *********************************************************/
361 void gtp_irq_disable(struct goodix_ts_data *ts)
363 unsigned long irqflags;
367 spin_lock_irqsave(&ts->irq_lock, irqflags);
368 if (!ts->irq_is_disable)
370 ts->irq_is_disable = 1;
371 disable_irq_nosync(ts->client->irq);
373 spin_unlock_irqrestore(&ts->irq_lock, irqflags);
376 /*******************************************************
380 ts: goodix i2c_client private data
383 *********************************************************/
384 void gtp_irq_enable(struct goodix_ts_data *ts)
386 unsigned long irqflags = 0;
390 spin_lock_irqsave(&ts->irq_lock, irqflags);
391 if (ts->irq_is_disable)
393 enable_irq(ts->client->irq);
394 ts->irq_is_disable = 0;
396 spin_unlock_irqrestore(&ts->irq_lock, irqflags);
400 /*******************************************************
402 Report touch point event
404 ts: goodix i2c_client private data
406 x: input x coordinate
407 y: input y coordinate
411 *********************************************************/
412 static void gtp_touch_down(struct goodix_ts_data* ts,s32 id,s32 x,s32 y,s32 w)
418 if (!bgt911 && !bgt970) {
420 x = ts->abs_x_max - x;
424 y = ts->abs_y_max - y;
428 #if GTP_ICS_SLOT_REPORT
429 input_mt_slot(ts->input_dev, id);
430 input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, id);
431 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
432 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
433 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, w);
434 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, w);
436 input_report_key(ts->input_dev, BTN_TOUCH, 1);
437 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
438 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
439 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, w);
440 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, w);
441 input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, id);
442 input_mt_sync(ts->input_dev);
445 GTP_DEBUG("ID:%d, X:%d, Y:%d, W:%d", id, x, y, w);
448 /*******************************************************
450 Report touch release event
452 ts: goodix i2c_client private data
455 *********************************************************/
456 static void gtp_touch_up(struct goodix_ts_data* ts, s32 id)
458 #if GTP_ICS_SLOT_REPORT
459 input_mt_slot(ts->input_dev, id);
460 input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, -1);
461 GTP_DEBUG("Touch id[%2d] release!", id);
463 input_report_key(ts->input_dev, BTN_TOUCH, 0);
469 static void gtp_pen_init(struct goodix_ts_data *ts)
473 GTP_INFO("Request input device for pen/stylus.");
475 ts->pen_dev = input_allocate_device();
476 if (ts->pen_dev == NULL)
478 GTP_ERROR("Failed to allocate input device for pen/stylus.");
482 ts->pen_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;
484 #if GTP_ICS_SLOT_REPORT
485 input_mt_init_slots(ts->pen_dev, 16, INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
487 ts->pen_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
490 set_bit(BTN_TOOL_PEN, ts->pen_dev->keybit);
491 set_bit(INPUT_PROP_DIRECT, ts->pen_dev->propbit);
492 //set_bit(INPUT_PROP_POINTER, ts->pen_dev->propbit);
494 #if GTP_PEN_HAVE_BUTTON
495 input_set_capability(ts->pen_dev, EV_KEY, BTN_STYLUS);
496 input_set_capability(ts->pen_dev, EV_KEY, BTN_STYLUS2);
499 input_set_abs_params(ts->pen_dev, ABS_MT_POSITION_X, 0, ts->abs_x_max, 0, 0);
500 input_set_abs_params(ts->pen_dev, ABS_MT_POSITION_Y, 0, ts->abs_y_max, 0, 0);
501 input_set_abs_params(ts->pen_dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
502 input_set_abs_params(ts->pen_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
503 input_set_abs_params(ts->pen_dev, ABS_MT_TRACKING_ID, 0, 255, 0, 0);
505 ts->pen_dev->name = "goodix-pen";
506 ts->pen_dev->id.bustype = BUS_I2C;
508 ret = input_register_device(ts->pen_dev);
511 GTP_ERROR("Register %s input device failed", ts->pen_dev->name);
516 static void gtp_pen_down(s32 x, s32 y, s32 w, s32 id)
518 struct goodix_ts_data *ts = i2c_get_clientdata(i2c_connect_client);
524 input_report_key(ts->pen_dev, BTN_TOOL_PEN, 1);
525 #if GTP_ICS_SLOT_REPORT
526 input_mt_slot(ts->pen_dev, id);
527 input_report_abs(ts->pen_dev, ABS_MT_TRACKING_ID, id);
528 input_report_abs(ts->pen_dev, ABS_MT_POSITION_X, x);
529 input_report_abs(ts->pen_dev, ABS_MT_POSITION_Y, y);
530 input_report_abs(ts->pen_dev, ABS_MT_PRESSURE, w);
531 input_report_abs(ts->pen_dev, ABS_MT_TOUCH_MAJOR, w);
533 input_report_key(ts->pen_dev, BTN_TOUCH, 1);
534 input_report_abs(ts->pen_dev, ABS_MT_POSITION_X, x);
535 input_report_abs(ts->pen_dev, ABS_MT_POSITION_Y, y);
536 input_report_abs(ts->pen_dev, ABS_MT_PRESSURE, w);
537 input_report_abs(ts->pen_dev, ABS_MT_TOUCH_MAJOR, w);
538 input_report_abs(ts->pen_dev, ABS_MT_TRACKING_ID, id);
539 input_mt_sync(ts->pen_dev);
541 GTP_DEBUG("(%d)(%d, %d)[%d]", id, x, y, w);
544 static void gtp_pen_up(s32 id)
546 struct goodix_ts_data *ts = i2c_get_clientdata(i2c_connect_client);
548 input_report_key(ts->pen_dev, BTN_TOOL_PEN, 0);
550 #if GTP_ICS_SLOT_REPORT
551 input_mt_slot(ts->pen_dev, id);
552 input_report_abs(ts->pen_dev, ABS_MT_TRACKING_ID, -1);
555 input_report_key(ts->pen_dev, BTN_TOUCH, 0);
561 /*******************************************************
563 Goodix touchscreen work function
565 work: work struct of goodix_workqueue
568 *********************************************************/
569 static void goodix_ts_work_func(struct work_struct *work)
571 u8 end_cmd[3] = {GTP_READ_COOR_ADDR >> 8, GTP_READ_COOR_ADDR & 0xFF, 0};
572 u8 point_data[2 + 1 + 8 * GTP_MAX_TOUCH + 1]={GTP_READ_COOR_ADDR >> 8, GTP_READ_COOR_ADDR & 0xFF};
575 static u16 pre_touch = 0;
576 static u8 pre_key = 0;
579 static u8 pre_pen = 0;
582 u8* coor_data = NULL;
589 struct goodix_ts_data *ts = NULL;
591 #if GTP_COMPATIBLE_MODE
592 u8 rqst_buf[3] = {0x80, 0x43}; // for GT9XXF
595 #if GTP_GESTURE_WAKEUP
596 u8 doze_buf[3] = {0x81, 0x4B};
600 ts = container_of(work, struct goodix_ts_data, work);
601 if (ts->enter_update)
605 #if GTP_GESTURE_WAKEUP
606 if (DOZE_ENABLED == doze_status)
608 ret = gtp_i2c_read(i2c_connect_client, doze_buf, 3);
609 GTP_DEBUG("0x814B = 0x%02X", doze_buf[2]);
612 if ((doze_buf[2] == 'a') || (doze_buf[2] == 'b') || (doze_buf[2] == 'c') ||
613 (doze_buf[2] == 'd') || (doze_buf[2] == 'e') || (doze_buf[2] == 'g') ||
614 (doze_buf[2] == 'h') || (doze_buf[2] == 'm') || (doze_buf[2] == 'o') ||
615 (doze_buf[2] == 'q') || (doze_buf[2] == 's') || (doze_buf[2] == 'v') ||
616 (doze_buf[2] == 'w') || (doze_buf[2] == 'y') || (doze_buf[2] == 'z') ||
617 (doze_buf[2] == 0x5E) /* ^ */
620 if (doze_buf[2] != 0x5E)
622 GTP_INFO("Wakeup by gesture(%c), light up the screen!", doze_buf[2]);
626 GTP_INFO("Wakeup by gesture(^), light up the screen!");
628 doze_status = DOZE_WAKEUP;
629 input_report_key(ts->input_dev, KEY_POWER, 1);
630 input_sync(ts->input_dev);
631 input_report_key(ts->input_dev, KEY_POWER, 0);
632 input_sync(ts->input_dev);
635 gtp_i2c_write(i2c_connect_client, doze_buf, 3);
637 else if ( (doze_buf[2] == 0xAA) || (doze_buf[2] == 0xBB) ||
638 (doze_buf[2] == 0xAB) || (doze_buf[2] == 0xBA) )
640 char *direction[4] = {"Right", "Down", "Up", "Left"};
641 u8 type = ((doze_buf[2] & 0x0F) - 0x0A) + (((doze_buf[2] >> 4) & 0x0F) - 0x0A) * 2;
643 GTP_INFO("%s slide to light up the screen!", direction[type]);
644 doze_status = DOZE_WAKEUP;
645 input_report_key(ts->input_dev, KEY_POWER, 1);
646 input_sync(ts->input_dev);
647 input_report_key(ts->input_dev, KEY_POWER, 0);
648 input_sync(ts->input_dev);
651 gtp_i2c_write(i2c_connect_client, doze_buf, 3);
653 else if (0xCC == doze_buf[2])
655 GTP_INFO("Double click to light up the screen!");
656 doze_status = DOZE_WAKEUP;
657 input_report_key(ts->input_dev, KEY_POWER, 1);
658 input_sync(ts->input_dev);
659 input_report_key(ts->input_dev, KEY_POWER, 0);
660 input_sync(ts->input_dev);
663 gtp_i2c_write(i2c_connect_client, doze_buf, 3);
669 gtp_i2c_write(i2c_connect_client, doze_buf, 3);
681 ret = gtp_i2c_read(ts->client, point_data, 12);
684 GTP_ERROR("I2C transfer error. errno:%d\n ", ret);
692 finger = point_data[GTP_ADDR_LENGTH];
694 #if GTP_COMPATIBLE_MODE
696 if ((finger == 0x00) && (CHIP_TYPE_GT9F == ts->chip_type)) // request arrived
698 ret = gtp_i2c_read(ts->client, rqst_buf, 3);
701 GTP_ERROR("Read request status error!");
707 case GTP_RQST_CONFIG:
708 GTP_INFO("Request for config.");
709 ret = gtp_send_cfg(ts->client);
712 GTP_ERROR("Request for config unresponded!");
716 rqst_buf[2] = GTP_RQST_RESPONDED;
717 gtp_i2c_write(ts->client, rqst_buf, 3);
718 GTP_INFO("Request for config responded!");
722 case GTP_RQST_BAK_REF:
723 GTP_INFO("Request for backup reference.");
724 ts->rqst_processing = 1;
725 ret = gtp_bak_ref_proc(ts, GTP_BAK_REF_SEND);
728 rqst_buf[2] = GTP_RQST_RESPONDED;
729 gtp_i2c_write(ts->client, rqst_buf, 3);
730 ts->rqst_processing = 0;
731 GTP_INFO("Request for backup reference responded!");
735 GTP_ERROR("Requeset for backup reference unresponed!");
740 GTP_INFO("Request for reset.");
741 gtp_recovery_reset(ts->client);
744 case GTP_RQST_MAIN_CLOCK:
745 GTP_INFO("Request for main clock.");
746 ts->rqst_processing = 1;
747 ret = gtp_main_clk_proc(ts);
750 GTP_ERROR("Request for main clock unresponded!");
754 GTP_INFO("Request for main clock responded!");
755 rqst_buf[2] = GTP_RQST_RESPONDED;
756 gtp_i2c_write(ts->client, rqst_buf, 3);
757 ts->rqst_processing = 0;
758 ts->clk_chk_fs_times = 0;
763 GTP_INFO("Undefined request: 0x%02X", rqst_buf[2]);
764 rqst_buf[2] = GTP_RQST_RESPONDED;
765 gtp_i2c_write(ts->client, rqst_buf, 3);
779 if((finger & 0x80) == 0)
784 touch_num = finger & 0x0f;
785 if (touch_num > GTP_MAX_TOUCH)
792 u8 buf[8 * GTP_MAX_TOUCH] = {(GTP_READ_COOR_ADDR + 10) >> 8, (GTP_READ_COOR_ADDR + 10) & 0xff};
794 ret = gtp_i2c_read(ts->client, buf, 2 + 8 * (touch_num - 1));
795 memcpy(&point_data[12], &buf[2], 8 * (touch_num - 1));
798 #if (GTP_HAVE_TOUCH_KEY || GTP_PEN_HAVE_BUTTON)
799 key_value = point_data[3 + 8 * touch_num];
801 if(key_value || pre_key)
803 #if GTP_PEN_HAVE_BUTTON
804 if (key_value == 0x40)
806 GTP_DEBUG("BTN_STYLUS & BTN_STYLUS2 Down.");
807 input_report_key(ts->pen_dev, BTN_STYLUS, 1);
808 input_report_key(ts->pen_dev, BTN_STYLUS2, 1);
811 else if (key_value == 0x10)
813 GTP_DEBUG("BTN_STYLUS Down, BTN_STYLUS2 Up.");
814 input_report_key(ts->pen_dev, BTN_STYLUS, 1);
815 input_report_key(ts->pen_dev, BTN_STYLUS2, 0);
818 else if (key_value == 0x20)
820 GTP_DEBUG("BTN_STYLUS Up, BTN_STYLUS2 Down.");
821 input_report_key(ts->pen_dev, BTN_STYLUS, 0);
822 input_report_key(ts->pen_dev, BTN_STYLUS2, 1);
827 GTP_DEBUG("BTN_STYLUS & BTN_STYLUS2 Up.");
828 input_report_key(ts->pen_dev, BTN_STYLUS, 0);
829 input_report_key(ts->pen_dev, BTN_STYLUS2, 0);
830 if ( (pre_key == 0x40) || (pre_key == 0x20) ||
839 touch_num = 0; // shield pen point
840 //pre_touch = 0; // clear last pen status
844 #if GTP_HAVE_TOUCH_KEY
847 for (i = 0; i < GTP_MAX_KEY_NUM; i++)
850 for (ret = 0; ret < 4; ++ret)
852 if (key_codes[ret] == touch_key_array[i])
854 GTP_DEBUG("Key: %s %s", key_names[ret], (key_value & (0x01 << i)) ? "Down" : "Up");
859 input_report_key(ts->input_dev, touch_key_array[i], key_value & (0x01<<i));
861 touch_num = 0; // shield fingers
868 GTP_DEBUG("pre_touch:%02x, finger:%02x.", pre_touch, finger);
870 #if GTP_ICS_SLOT_REPORT
873 if (pre_pen && (touch_num == 0))
875 GTP_DEBUG("Pen touch UP(Slot)!");
881 if (pre_touch || touch_num)
886 coor_data = &point_data[3];
890 id = coor_data[pos] & 0x0F;
896 GTP_DEBUG("Pen touch DOWN(Slot)!");
897 input_x = coor_data[pos + 1] | (coor_data[pos + 2] << 8);
898 input_y = coor_data[pos + 3] | (coor_data[pos + 4] << 8);
899 input_w = coor_data[pos + 5] | (coor_data[pos + 6] << 8);
901 gtp_pen_down(input_x, input_y, input_w, 0);
908 touch_index |= (0x01<<id);
911 GTP_DEBUG("id = %d,touch_index = 0x%x, pre_touch = 0x%x\n",id, touch_index,pre_touch);
912 for (i = 0; i < GTP_MAX_TOUCH; i++)
921 if ((touch_index & (0x01<<i)))
923 input_x = coor_data[pos + 1] | (coor_data[pos + 2] << 8);
924 input_y = coor_data[pos + 3] | (coor_data[pos + 4] << 8);
925 input_w = coor_data[pos + 5] | (coor_data[pos + 6] << 8);
927 gtp_touch_down(ts, id, input_x, input_y, input_w);
928 pre_touch |= 0x01 << i;
931 if (report_num < touch_num)
934 id = coor_data[pos] & 0x0F;
935 touch_index |= (0x01<<id);
941 pre_touch &= ~(0x01 << i);
945 #else //end if GTP_ICS_SLOT_REPORT
949 for (i = 0; i < touch_num; i++)
951 coor_data = &point_data[i * 8 + 3];
953 id = coor_data[0] & 0x0F;
954 input_x = coor_data[1] | (coor_data[2] << 8);
955 input_y = coor_data[3] | (coor_data[4] << 8);
956 input_w = coor_data[5] | (coor_data[6] << 8);
962 GTP_DEBUG("Pen touch DOWN!");
963 gtp_pen_down(input_x, input_y, input_w, 0);
971 gtp_touch_down(ts, id, input_x, input_y, input_w);
980 GTP_DEBUG("Pen touch UP!");
988 GTP_DEBUG("Touch Release!");
993 pre_touch = touch_num;
1000 input_sync(ts->pen_dev);
1005 input_sync(ts->input_dev);
1009 if(!ts->gtp_rawdiff_mode)
1011 ret = gtp_i2c_write(ts->client, end_cmd, 3);
1014 GTP_INFO("I2C write end_cmd error!");
1023 /*******************************************************
1025 Timer interrupt service routine for polling mode.
1027 timer: timer struct pointer
1030 HRTIMER_NORESTART: no restart mode
1031 *********************************************************/
1032 static enum hrtimer_restart goodix_ts_timer_handler(struct hrtimer *timer)
1034 struct goodix_ts_data *ts = container_of(timer, struct goodix_ts_data, timer);
1038 queue_work(goodix_wq, &ts->work);
1039 hrtimer_start(&ts->timer, ktime_set(0, (GTP_POLL_TIME+6)*1000000), HRTIMER_MODE_REL);
1040 return HRTIMER_NORESTART;
1043 /*******************************************************
1045 External interrupt service routine for interrupt mode.
1047 irq: interrupt number.
1048 dev_id: private data pointer
1051 IRQ_HANDLED: interrupt handled successfully
1052 *********************************************************/
1053 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
1055 struct goodix_ts_data *ts = dev_id;
1059 gtp_irq_disable(ts);
1061 queue_work(goodix_wq, &ts->work);
1065 /*******************************************************
1069 ms: synchronization time in millisecond.
1072 *******************************************************/
1073 void gtp_int_sync(s32 ms, struct goodix_ts_data *ts)
1075 GTP_GPIO_OUTPUT(ts->irq_pin, 0);
1077 //GTP_GPIO_AS_INT(GTP_INT_PORT);
1078 gpio_direction_input(ts->irq_pin);
1079 //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1080 //s3c_gpio_cfgpin(pin, GTP_INT_CFG);
1084 /*******************************************************
1088 ms: reset time in millisecond
1091 *******************************************************/
1092 void gtp_reset_guitar(struct i2c_client *client, s32 ms)
1094 struct goodix_ts_data *ts = i2c_get_clientdata(client);
1097 GTP_INFO("Guitar reset");
1098 GTP_GPIO_OUTPUT(ts->rst_pin, 0); // begin select I2C slave addr
1099 msleep(ms); // T2: > 10ms
1100 // HIGH: 0x28/0x29, LOW: 0xBA/0xBB
1101 GTP_GPIO_OUTPUT(ts->irq_pin, client->addr == 0x14);
1103 msleep(2); // T3: > 100us
1104 GTP_GPIO_OUTPUT(ts->rst_pin, 1);
1106 msleep(6); // T4: > 5ms
1108 //GTP_GPIO_AS_INPUT(GTP_RST_PORT); // end select I2C slave addr
1109 gpio_direction_input(ts->rst_pin);
1110 //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1112 #if GTP_COMPATIBLE_MODE
1113 if (CHIP_TYPE_GT9F == ts->chip_type)
1119 gtp_int_sync(50, ts);
1121 gtp_init_ext_watchdog(client);
1125 #if GTP_GESTURE_WAKEUP
1126 /*******************************************************
1128 Enter doze mode for sliding wakeup.
1130 ts: goodix tp private data
1132 1: succeed, otherwise failed
1133 *******************************************************/
1134 static s8 gtp_enter_doze(struct goodix_ts_data *ts)
1138 u8 i2c_control_buf[3] = {(u8)(GTP_REG_SLEEP >> 8), (u8)GTP_REG_SLEEP, 8};
1142 GTP_DEBUG("Entering gesture mode.");
1145 i2c_control_buf[0] = 0x80;
1146 i2c_control_buf[1] = 0x46;
1147 ret = gtp_i2c_write(ts->client, i2c_control_buf, 3);
1150 GTP_DEBUG("failed to set doze flag into 0x8046, %d", retry);
1153 i2c_control_buf[0] = 0x80;
1154 i2c_control_buf[1] = 0x40;
1155 ret = gtp_i2c_write(ts->client, i2c_control_buf, 3);
1158 doze_status = DOZE_ENABLED;
1159 GTP_INFO("Gesture mode enabled.");
1164 GTP_ERROR("GTP send gesture cmd failed.");
1168 /*******************************************************
1175 1: succeed, otherwise failed.
1176 *******************************************************/
1177 static s8 gtp_enter_sleep(struct goodix_ts_data * ts)
1181 u8 i2c_control_buf[3] = {(u8)(GTP_REG_SLEEP >> 8), (u8)GTP_REG_SLEEP, 5};
1182 #if GTP_COMPATIBLE_MODE
1183 u8 status_buf[3] = {0x80, 0x44};
1188 #if GTP_COMPATIBLE_MODE
1189 if (CHIP_TYPE_GT9F == ts->chip_type)
1191 // GT9XXF: host interact with ic
1192 ret = gtp_i2c_read(ts->client, status_buf, 3);
1195 GTP_ERROR("failed to get backup-reference status");
1198 if (status_buf[2] & 0x80)
1200 ret = gtp_bak_ref_proc(ts, GTP_BAK_REF_STORE);
1203 GTP_ERROR("failed to store bak_ref");
1209 GTP_GPIO_OUTPUT(ts->irq_pin, 0);
1214 ret = gtp_i2c_write(ts->client, i2c_control_buf, 3);
1217 GTP_INFO("GTP enter sleep!");
1223 GTP_ERROR("GTP send sleep cmd failed.");
1227 /*******************************************************
1234 >0: succeed, otherwise: failed.
1235 *******************************************************/
1236 static s8 gtp_wakeup_sleep(struct goodix_ts_data * ts)
1243 #if GTP_COMPATIBLE_MODE
1244 if (CHIP_TYPE_GT9F == ts->chip_type)
1246 u8 opr_buf[3] = {0x41, 0x80};
1248 GTP_GPIO_OUTPUT(ts->irq_pin, 1);
1251 for (retry = 0; retry < 10; ++retry)
1255 ret = gtp_i2c_write(ts->client, opr_buf, 3);
1258 GTP_ERROR("failed to hold ss51 & dsp!");
1262 ret = gtp_i2c_read(ts->client, opr_buf, 3);
1265 GTP_ERROR("failed to get ss51 & dsp status!");
1268 if (0x0C != opr_buf[2])
1270 GTP_DEBUG("ss51 & dsp not been hold, %d", retry+1);
1273 GTP_DEBUG("ss51 & dsp confirmed hold");
1275 ret = gtp_fw_startup(ts->client);
1278 GTP_ERROR("failed to startup GT9XXF, process recovery");
1279 gtp_esd_recovery(ts->client);
1285 GTP_ERROR("failed to wakeup, processing esd recovery");
1286 gtp_esd_recovery(ts->client);
1290 GTP_INFO("GT9XXF gtp wakeup success");
1296 #if GTP_POWER_CTRL_SLEEP
1299 gtp_reset_guitar(ts->client, 20);
1301 GTP_INFO("GTP wakeup sleep.");
1307 #if GTP_GESTURE_WAKEUP
1308 if (DOZE_WAKEUP != doze_status)
1310 GTP_INFO("Powerkey wakeup.");
1314 GTP_INFO("Gesture wakeup.");
1316 doze_status = DOZE_DISABLED;
1317 gtp_irq_disable(ts);
1318 gtp_reset_guitar(ts->client, 10);
1322 GTP_GPIO_OUTPUT(ts->irq_pin, 1);
1326 ret = gtp_i2c_test(ts->client);
1329 GTP_INFO("GTP wakeup sleep.");
1331 #if (!GTP_GESTURE_WAKEUP)
1333 gtp_int_sync(25, ts);
1335 gtp_init_ext_watchdog(ts->client);
1342 gtp_reset_guitar(ts->client, 20);
1346 GTP_ERROR("GTP wakeup sleep failed.");
1349 #if GTP_DRIVER_SEND_CFG
1350 static s32 gtp_get_info(struct goodix_ts_data *ts)
1352 u8 opr_buf[6] = {0};
1355 //ts->abs_x_max = GTP_MAX_WIDTH;
1356 //ts->abs_y_max = GTP_MAX_HEIGHT;
1358 ts->int_trigger_type = GTP_INT_TRIGGER;
1360 opr_buf[0] = (u8)((GTP_REG_CONFIG_DATA+1) >> 8);
1361 opr_buf[1] = (u8)((GTP_REG_CONFIG_DATA+1) & 0xFF);
1363 ret = gtp_i2c_read(ts->client, opr_buf, 6);
1369 ts->abs_x_max = (opr_buf[3] << 8) + opr_buf[2];
1370 ts->abs_y_max = (opr_buf[5] << 8) + opr_buf[4];
1372 opr_buf[0] = (u8)((GTP_REG_CONFIG_DATA+6) >> 8);
1373 opr_buf[1] = (u8)((GTP_REG_CONFIG_DATA+6) & 0xFF);
1375 ret = gtp_i2c_read(ts->client, opr_buf, 3);
1380 ts->int_trigger_type = opr_buf[2] & 0x03;
1382 GTP_INFO("X_MAX = %d, Y_MAX = %d, TRIGGER = 0x%02x",
1383 ts->abs_x_max,ts->abs_y_max,ts->int_trigger_type);
1389 /*******************************************************
1393 ts: goodix private data
1396 0: succeed, otherwise: failed
1397 *******************************************************/
1398 static s32 gtp_init_panel(struct goodix_ts_data *ts)
1401 #if GTP_DRIVER_SEND_CFG
1404 u8 opr_buf[16] = {0};
1407 u8 cfg_info_group2[] = CTP_CFG_GROUP2;
1408 u8 cfg_info_group3[] = CTP_CFG_GROUP3;
1409 u8 cfg_info_group4[] = CTP_CFG_GROUP4;
1410 u8 cfg_info_group5[] = CTP_CFG_GROUP5;
1411 u8 cfg_info_group6[] = CTP_CFG_GROUP6;
1412 u8 *send_cfg_buf[] = {gtp_dat_10_1, cfg_info_group2, cfg_info_group3,
1413 cfg_info_group4, cfg_info_group5, cfg_info_group6};
1414 u8 cfg_info_len[] = { CFG_GROUP_LEN(gtp_dat_10_1),
1415 CFG_GROUP_LEN(cfg_info_group2),
1416 CFG_GROUP_LEN(cfg_info_group3),
1417 CFG_GROUP_LEN(cfg_info_group4),
1418 CFG_GROUP_LEN(cfg_info_group5),
1419 CFG_GROUP_LEN(cfg_info_group6)};;
1421 GTP_INFO(" <%s>_%d \n", __func__, __LINE__);
1424 send_cfg_buf[0] = gtp_dat_8_9;
1425 cfg_info_len[0] = CFG_GROUP_LEN(gtp_dat_8_9);
1429 send_cfg_buf[0] = gtp_dat_gt11;
1430 cfg_info_len[0] = CFG_GROUP_LEN(gtp_dat_gt11);
1434 send_cfg_buf[0] = gtp_dat_9_7;
1435 cfg_info_len[0] = CFG_GROUP_LEN(gtp_dat_9_7);
1439 GTP_DEBUG("Config Groups\' Lengths: %d, %d, %d, %d, %d, %d",
1440 cfg_info_len[0], cfg_info_len[1], cfg_info_len[2], cfg_info_len[3],
1441 cfg_info_len[4], cfg_info_len[5]);
1444 #if GTP_COMPATIBLE_MODE
1445 if (CHIP_TYPE_GT9F == ts->chip_type)
1452 ret = gtp_i2c_read_dbl_check(ts->client, 0x41E4, opr_buf, 1);
1455 if (opr_buf[0] != 0xBE)
1458 GTP_ERROR("Firmware error, no config sent!");
1464 if ((!cfg_info_len[1]) && (!cfg_info_len[2]) &&
1465 (!cfg_info_len[3]) && (!cfg_info_len[4]) &&
1472 #if GTP_COMPATIBLE_MODE
1475 ret = gtp_i2c_read_dbl_check(ts->client, GTP_REG_SENSOR_ID, &sensor_id, 1);
1478 if (sensor_id >= 0x06)
1480 GTP_ERROR("Invalid sensor_id(0x%02X), No Config Sent!", sensor_id);
1481 ts->pnl_init_error = 1;
1487 GTP_ERROR("Failed to get sensor_id, No config sent!");
1488 ts->pnl_init_error = 1;
1491 GTP_INFO("Sensor_ID: %d", sensor_id);
1493 ts->gtp_cfg_len = cfg_info_len[sensor_id];
1494 GTP_INFO("CTP_CONFIG_GROUP%d used, config length: %d", sensor_id + 1, ts->gtp_cfg_len);
1496 if (ts->gtp_cfg_len < GTP_CONFIG_MIN_LENGTH)
1498 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);
1499 ts->pnl_init_error = 1;
1503 #if GTP_COMPATIBLE_MODE
1504 if (CHIP_TYPE_GT9F == ts->chip_type)
1511 ret = gtp_i2c_read_dbl_check(ts->client, GTP_REG_CONFIG_DATA, &opr_buf[0], 1);
1515 GTP_DEBUG("CFG_GROUP%d Config Version: %d, 0x%02X; IC Config Version: %d, 0x%02X", sensor_id+1,
1516 send_cfg_buf[sensor_id][0], send_cfg_buf[sensor_id][0], opr_buf[0], opr_buf[0]);
1518 if (opr_buf[0] < 90)
1520 GTP_INFO(" <%s>_%d \n", __func__, __LINE__);
1521 grp_cfg_version = send_cfg_buf[sensor_id][0]; // backup group config version
1522 send_cfg_buf[sensor_id][0] = 0x00;
1525 else // treated as fixed config, not send config
1527 GTP_INFO("Ic fixed config with config version(%d, 0x%02X)", opr_buf[0], opr_buf[0]);
1535 GTP_ERROR("Failed to get ic config version!No config sent!");
1540 memset(&config[GTP_ADDR_LENGTH], 0, GTP_CONFIG_MAX_LENGTH);
1541 memcpy(&config[GTP_ADDR_LENGTH], send_cfg_buf[sensor_id], ts->gtp_cfg_len);
1544 config[RESOLUTION_LOC] = (u8)GTP_MAX_WIDTH;
1545 config[RESOLUTION_LOC + 1] = (u8)(GTP_MAX_WIDTH>>8);
1546 config[RESOLUTION_LOC + 2] = (u8)GTP_MAX_HEIGHT;
1547 config[RESOLUTION_LOC + 3] = (u8)(GTP_MAX_HEIGHT>>8);
1549 if (GTP_INT_TRIGGER == 0) //RISING
1551 config[TRIGGER_LOC] &= 0xfe;
1553 else if (GTP_INT_TRIGGER == 1) //FALLING
1555 config[TRIGGER_LOC] |= 0x01;
1557 #endif // GTP_CUSTOM_CFG
1560 for (i = GTP_ADDR_LENGTH; i < ts->gtp_cfg_len; i++)
1562 check_sum += config[i];
1564 config[ts->gtp_cfg_len] = (~check_sum) + 1;
1566 #else // driver not send config
1568 ts->gtp_cfg_len = GTP_CONFIG_MAX_LENGTH;
1569 ret = gtp_i2c_read(ts->client, config, ts->gtp_cfg_len + GTP_ADDR_LENGTH);
1572 GTP_ERROR("Read Config Failed, Using Default Resolution & INT Trigger!");
1573 //ts->abs_x_max = GTP_MAX_WIDTH;
1574 //ts->abs_y_max = GTP_MAX_HEIGHT;
1575 ts->int_trigger_type = GTP_INT_TRIGGER;
1578 #endif // GTP_DRIVER_SEND_CFG
1580 if ((ts->abs_x_max == 0) && (ts->abs_y_max == 0))
1582 ts->abs_x_max = (config[RESOLUTION_LOC + 1] << 8) + config[RESOLUTION_LOC];
1583 ts->abs_y_max = (config[RESOLUTION_LOC + 3] << 8) + config[RESOLUTION_LOC + 2];
1584 ts->int_trigger_type = (config[TRIGGER_LOC]) & 0x03;
1585 GTP_INFO(" <%s>_%d <%d, %d>\n", __func__, __LINE__, ts->abs_x_max, ts->abs_y_max);
1587 GTP_INFO(" <%s>_%d \n", __func__, __LINE__);
1588 #if GTP_COMPATIBLE_MODE
1589 if (CHIP_TYPE_GT9F == ts->chip_type)
1595 have_key = (config[GTP_REG_HAVE_KEY - GTP_REG_CONFIG_DATA + 2] & 0x01);
1597 if (1 == ts->is_950)
1599 driver_num = config[GTP_REG_MATRIX_DRVNUM - GTP_REG_CONFIG_DATA + 2];
1600 sensor_num = config[GTP_REG_MATRIX_SENNUM - GTP_REG_CONFIG_DATA + 2];
1605 ts->bak_ref_len = (driver_num * (sensor_num - 1) + 2) * 2 * 6;
1609 driver_num = (config[CFG_LOC_DRVA_NUM] & 0x1F) + (config[CFG_LOC_DRVB_NUM]&0x1F);
1614 sensor_num = (config[CFG_LOC_SENS_NUM] & 0x0F) + ((config[CFG_LOC_SENS_NUM] >> 4) & 0x0F);
1615 ts->bak_ref_len = (driver_num * (sensor_num - 2) + 2) * 2;
1618 GTP_INFO("Drv * Sen: %d * %d(key: %d), X_MAX: %d, Y_MAX: %d, TRIGGER: 0x%02x",
1619 driver_num, sensor_num, have_key, ts->abs_x_max,ts->abs_y_max,ts->int_trigger_type);
1625 #if GTP_DRIVER_SEND_CFG
1626 GTP_INFO(" <%s>_%d \n", __func__, __LINE__);
1627 ret = gtp_send_cfg(ts->client);
1630 GTP_ERROR("Send config error.");
1632 // set config version to CTP_CFG_GROUP, for resume to send config
1633 config[GTP_ADDR_LENGTH] = grp_cfg_version;
1635 for (i = GTP_ADDR_LENGTH; i < ts->gtp_cfg_len; i++)
1637 check_sum += config[i];
1639 config[ts->gtp_cfg_len] = (~check_sum) + 1;
1641 GTP_INFO("X_MAX: %d, Y_MAX: %d, TRIGGER: 0x%02x", ts->abs_x_max,ts->abs_y_max,ts->int_trigger_type);
1649 static ssize_t gt91xx_config_read_proc(struct file *file, char __user *page, size_t size, loff_t *ppos)
1652 char temp_data[GTP_CONFIG_MAX_LENGTH + 2] = {0x80, 0x47};
1659 ptr += sprintf(ptr, "==== GT9XX config init value====\n");
1661 for (i = 0 ; i < GTP_CONFIG_MAX_LENGTH ; i++)
1663 ptr += sprintf(ptr, "0x%02X ", config[i + 2]);
1666 ptr += sprintf(ptr, "\n");
1669 ptr += sprintf(ptr, "\n");
1671 ptr += sprintf(ptr, "==== GT9XX config real value====\n");
1672 gtp_i2c_read(i2c_connect_client, temp_data, GTP_CONFIG_MAX_LENGTH + 2);
1673 for (i = 0 ; i < GTP_CONFIG_MAX_LENGTH ; i++)
1675 ptr += sprintf(ptr, "0x%02X ", temp_data[i+2]);
1678 ptr += sprintf(ptr, "\n");
1680 *ppos += ptr - page;
1681 return (ptr - page);
1684 static ssize_t gt91xx_config_write_proc(struct file *filp, const char __user *buffer, size_t count, loff_t *off)
1688 if (count > GTP_CONFIG_MAX_LENGTH)
1690 GTP_ERROR("size not match [%d:%zu]\n", GTP_CONFIG_MAX_LENGTH, count);
1694 if (copy_from_user(&config[2], buffer, count))
1696 GTP_ERROR("copy from user fail\n");
1700 ret = gtp_send_cfg(i2c_connect_client);
1704 GTP_ERROR("send config failed.");
1709 /*******************************************************
1714 version: buffer to keep ic firmware version
1716 read operation return.
1717 2: succeed, otherwise: failed
1718 *******************************************************/
1719 s32 gtp_read_version(struct i2c_client *client, u16* version)
1722 u8 buf[8] = {GTP_REG_VERSION >> 8, GTP_REG_VERSION & 0xff};
1726 ret = gtp_i2c_read(client, buf, sizeof(buf));
1729 GTP_ERROR("GTP read version failed");
1735 *version = (buf[7] << 8) | buf[6];
1739 GTP_INFO("IC Version: %c%c%c_%02x%02x", buf[2], buf[3], buf[4], buf[7], buf[6]);
1743 GTP_INFO("IC Version: %c%c%c%c_%02x%02x", buf[2], buf[3], buf[4], buf[5], buf[7], buf[6]);
1748 /*******************************************************
1755 2: succeed, otherwise failed.
1756 *******************************************************/
1757 static s8 gtp_i2c_test(struct i2c_client *client)
1759 u8 test[3] = {GTP_REG_CONFIG_DATA >> 8, GTP_REG_CONFIG_DATA & 0xff};
1767 ret = gtp_i2c_read(client, test, 3);
1772 GTP_ERROR("GTP i2c test failed time %d.",retry);
1778 /*******************************************************
1780 Request gpio(INT & RST) ports.
1785 >= 0: succeed, < 0: failed
1786 *******************************************************/
1787 static s8 gtp_request_io_port(struct goodix_ts_data *ts)
1793 ret = GTP_GPIO_REQUEST(ts->tp_select_pin, "GTP_tp_select_PORT");
1796 GTP_ERROR("1Failed to request GPIO:%d, ERRNO:%d",(s32)ts->tp_select_pin, ret);
1801 gpio_direction_input(ts->tp_select_pin);
1804 ret = GTP_GPIO_REQUEST(ts->rst_pin, "GTP_RST_PORT");
1807 GTP_ERROR("2Failed to request GPIO:%d, ERRNO:%d",(s32)ts->rst_pin, ret);
1811 ret = GTP_GPIO_REQUEST(ts->irq_pin, "GTP_INT_IRQ");
1814 GTP_ERROR("3Failed to request GPIO:%d, ERRNO:%d", (s32)ts->irq_pin, ret);
1819 //GTP_GPIO_AS_INT(GTP_INT_PORT);
1820 gpio_direction_input(ts->irq_pin);
1821 //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1822 //s3c_gpio_cfgpin(pin, GTP_INT_CFG);
1824 //ts->client->irq = ts->irq_pin;
1827 //GTP_GPIO_AS_INPUT(ts->rst_pin);
1828 gpio_direction_input(ts->rst_pin);
1829 //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1831 gtp_reset_guitar(ts->client, 20);
1835 GTP_GPIO_FREE(ts->rst_pin);
1836 GTP_GPIO_FREE(ts->irq_pin);
1842 /*******************************************************
1849 0: succeed, -1: failed.
1850 *******************************************************/
1851 static s8 gtp_request_irq(struct goodix_ts_data *ts)
1856 GTP_DEBUG("INT trigger type:%x", ts->int_trigger_type);
1858 ts->irq=gpio_to_irq(ts->irq_pin); //If not defined in client
1861 ts->client->irq = ts->irq;
1862 ret = devm_request_threaded_irq(&(ts->client->dev), ts->irq, NULL,
1863 goodix_ts_irq_handler, ts->irq_flags | IRQF_ONESHOT /*irq_table[ts->int_trigger_type]*/,
1864 ts->client->name, ts);
1866 GTP_ERROR("Cannot allocate ts INT!ERRNO:%d\n", ret);
1869 //gtp_irq_disable(ts->irq);
1870 GTP_INFO(" <%s>_%d ts->irq=%d ret = %d\n", __func__, __LINE__, ts->irq, ret);
1872 GTP_ERROR(" ts->irq error \n");
1877 ret = request_irq(ts->client->irq,
1878 goodix_ts_irq_handler,
1879 irq_table[ts->int_trigger_type],
1886 GTP_ERROR("Request IRQ failed!ERRNO:%d.", ret);
1887 //GTP_GPIO_AS_INPUT(GTP_INT_PORT);
1888 gpio_direction_input(ts->irq_pin);
1889 //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1891 GTP_GPIO_FREE(ts->irq_pin);
1893 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1894 ts->timer.function = goodix_ts_timer_handler;
1895 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
1900 GTP_INFO(" <%s>_%d ts->irq=%d ret = %d\n", __func__, __LINE__, ts->irq, ret);
1901 gtp_irq_disable(ts);
1907 /*******************************************************
1909 Early suspend function.
1911 h: early_suspend struct.
1914 *******************************************************/
1915 static int goodix_ts_early_suspend(struct tp_device *tp_d)
1917 struct goodix_ts_data *ts;
1921 ts = container_of(tp_d, struct goodix_ts_data, tp);
1924 GTP_INFO("System suspend.");
1926 ts->gtp_is_suspend = 1;
1928 gtp_esd_switch(ts->client, SWITCH_OFF);
1931 #if GTP_GESTURE_WAKEUP
1932 ret = gtp_enter_doze(ts);
1936 gtp_irq_disable(ts);
1940 hrtimer_cancel(&ts->timer);
1942 ret = gtp_enter_sleep(ts);
1946 printk("GTP early suspend failed.");
1948 // to avoid waking up while not sleeping
1949 // delay 48 + 10ms to ensure reliability
1952 reg = regulator_disable(ts->tp_regulator);
1954 GTP_ERROR("failed to disable tp regulator\n");
1959 /*******************************************************
1961 Late resume function.
1963 h: early_suspend struct.
1966 *******************************************************/
1967 static int goodix_ts_early_resume(struct tp_device *tp_d)
1969 struct goodix_ts_data *ts;
1972 ts = container_of(tp_d, struct goodix_ts_data, tp);
1975 GTP_INFO("System resume.");
1977 reg = regulator_enable(ts->tp_regulator);
1979 GTP_ERROR("failed to enable tp regulator\n");
1982 ret = gtp_wakeup_sleep(ts);
1984 #if GTP_GESTURE_WAKEUP
1985 doze_status = DOZE_DISABLED;
1990 GTP_ERROR("GTP later resume failed.");
1992 #if (GTP_COMPATIBLE_MODE)
1993 if (CHIP_TYPE_GT9F == ts->chip_type)
2000 gtp_send_cfg(ts->client);
2009 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
2012 ts->gtp_is_suspend = 0;
2014 gtp_esd_switch(ts->client, SWITCH_ON);
2020 /*******************************************************
2022 Request input device Function.
2027 0: succeed, otherwise: failed.
2028 *******************************************************/
2029 static s8 gtp_request_input_dev(struct i2c_client *client,
2030 struct goodix_ts_data *ts)
2034 #if GTP_HAVE_TOUCH_KEY
2039 ts->input_dev = devm_input_allocate_device(&client->dev);
2040 if (ts->input_dev == NULL)
2042 GTP_ERROR("Failed to allocate input device.");
2046 ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;
2047 #if GTP_ICS_SLOT_REPORT
2048 input_mt_init_slots(ts->input_dev, 16, INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); // in case of "out of memory"
2050 ts->input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
2052 __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);
2054 #if GTP_HAVE_TOUCH_KEY
2055 for (index = 0; index < GTP_MAX_KEY_NUM; index++)
2057 input_set_capability(ts->input_dev, EV_KEY, touch_key_array[index]);
2061 #if GTP_GESTURE_WAKEUP
2062 input_set_capability(ts->input_dev, EV_KEY, KEY_POWER);
2066 GTP_SWAP(ts->abs_x_max, ts->abs_y_max);
2069 #if defined(CONFIG_CHROME_PLATFORMS)
2070 input_set_abs_params(ts->input_dev, ABS_X, 0, ts->abs_x_max, 0, 0);
2071 input_set_abs_params(ts->input_dev, ABS_Y, 0, ts->abs_y_max, 0, 0);
2073 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0, ts->abs_x_max, 0, 0);
2074 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0, ts->abs_y_max, 0, 0);
2075 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
2076 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
2077 input_set_abs_params(ts->input_dev, ABS_MT_TRACKING_ID, 0, 255, 0, 0);
2079 sprintf(phys, "input/ts");
2080 ts->input_dev->name = goodix_ts_name;
2081 ts->input_dev->phys = phys;
2082 ts->input_dev->id.bustype = BUS_I2C;
2083 ts->input_dev->id.vendor = 0xDEAD;
2084 ts->input_dev->id.product = 0xBEEF;
2085 ts->input_dev->id.version = 10427;
2087 ret = input_register_device(ts->input_dev);
2090 GTP_ERROR("Register %s input device failed", ts->input_dev->name);
2094 ts->tp.tp_resume = goodix_ts_early_resume;
2095 ts->tp.tp_suspend = goodix_ts_early_suspend;
2096 tp_register_fb(&ts->tp);
2105 //************** For GT9XXF Start *************//
2106 #if GTP_COMPATIBLE_MODE
2108 s32 gtp_fw_startup(struct i2c_client *client)
2112 struct goodix_ts_data *ts = i2c_get_clientdata(client);
2115 ret = i2c_write_bytes(client, 0x8041, opr_buf, 1);
2121 //release SS51 & DSP
2123 ret = i2c_write_bytes(client, 0x4180, opr_buf, 1);
2129 gtp_int_sync(25, ts);
2131 //check fw run status
2132 ret = i2c_read_bytes(client, 0x8041, opr_buf, 1);
2137 if(0xAA == opr_buf[0])
2139 GTP_ERROR("IC works abnormally,startup failed.");
2144 GTP_INFO("IC works normally, Startup success.");
2146 i2c_write_bytes(client, 0x8041, opr_buf, 1);
2151 static s32 gtp_esd_recovery(struct i2c_client *client)
2155 struct goodix_ts_data *ts;
2157 ts = i2c_get_clientdata(client);
2159 gtp_irq_disable(ts);
2161 GTP_INFO("GT9XXF esd recovery mode");
2162 for (retry = 0; retry < 5; retry++)
2164 ret = gup_fw_download_proc(NULL, GTP_FL_ESD_RECOVERY);
2167 GTP_ERROR("esd recovery failed %d", retry+1);
2170 ret = gtp_fw_startup(ts->client);
2173 GTP_ERROR("GT9XXF start up failed %d", retry+1);
2182 GTP_ERROR("failed to esd recovery");
2186 GTP_INFO("Esd recovery successful");
2190 void gtp_recovery_reset(struct i2c_client *client)
2193 gtp_esd_switch(client, SWITCH_OFF);
2197 gtp_esd_recovery(client);
2200 gtp_esd_switch(client, SWITCH_ON);
2204 static s32 gtp_bak_ref_proc(struct goodix_ts_data *ts, u8 mode)
2212 s32 ref_seg_len = 0;
2214 struct file *ref_filp = NULL;
2217 ret = gup_check_fs_mounted("/data");
2220 ts->ref_chk_fs_times++;
2221 GTP_DEBUG("Ref check /data times/MAX_TIMES: %d / %d", ts->ref_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2222 if (ts->ref_chk_fs_times < GTP_CHK_FS_MNT_MAX)
2225 GTP_INFO("/data not mounted.");
2228 GTP_INFO("check /data mount timeout...");
2232 GTP_INFO("/data mounted!!!(%d/%d)", ts->ref_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2235 p_bak_ref = (u8 *)kzalloc(ts->bak_ref_len, GFP_KERNEL);
2237 if (NULL == p_bak_ref)
2239 GTP_ERROR("Allocate memory for p_bak_ref failed!");
2245 ref_seg_len = ts->bak_ref_len / 6;
2250 ref_seg_len = ts->bak_ref_len;
2253 ref_filp = filp_open(GTP_BAK_REF_PATH, O_RDWR | O_CREAT, 0666);
2254 if (IS_ERR(ref_filp))
2256 GTP_ERROR("Failed to open/create %s.", GTP_BAK_REF_PATH);
2257 if (GTP_BAK_REF_SEND == mode)
2259 goto bak_ref_default;
2269 case GTP_BAK_REF_SEND:
2270 GTP_INFO("Send backup-reference");
2271 ref_filp->f_op->llseek(ref_filp, 0, SEEK_SET);
2272 ret = ref_filp->f_op->read(ref_filp, (char*)p_bak_ref, ts->bak_ref_len, &ref_filp->f_pos);
2275 GTP_ERROR("failed to read bak_ref info from file, sending defualt bak_ref");
2276 goto bak_ref_default;
2278 for (j = 0; j < ref_grps; ++j)
2281 for (i = 0; i < (ref_seg_len); i += 2)
2283 ref_sum += (p_bak_ref[i + j * ref_seg_len] << 8) + p_bak_ref[i+1 + j * ref_seg_len];
2285 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]);
2286 chksum = (p_bak_ref[j * ref_seg_len + ref_seg_len -2] << 8) + (p_bak_ref[j * ref_seg_len + ref_seg_len -1]);
2287 GTP_DEBUG("learn count = %d", learn_cnt);
2288 GTP_DEBUG("chksum = %d", chksum);
2289 GTP_DEBUG("ref_sum = 0x%04X", ref_sum & 0xFFFF);
2290 // Sum(1~ref_seg_len) == 1
2293 GTP_INFO("wrong chksum for bak_ref, reset to 0x00 bak_ref");
2294 memset(&p_bak_ref[j * ref_seg_len], 0, ref_seg_len);
2295 p_bak_ref[ref_seg_len + j * ref_seg_len - 1] = 0x01;
2299 if (j == (ref_grps - 1))
2301 GTP_INFO("backup-reference data in %s used", GTP_BAK_REF_PATH);
2305 ret = i2c_write_bytes(ts->client, GTP_REG_BAK_REF, p_bak_ref, ts->bak_ref_len);
2308 GTP_ERROR("failed to send bak_ref because of iic comm error");
2313 case GTP_BAK_REF_STORE:
2314 GTP_INFO("Store backup-reference");
2315 ret = i2c_read_bytes(ts->client, GTP_REG_BAK_REF, p_bak_ref, ts->bak_ref_len);
2318 GTP_ERROR("failed to read bak_ref info, sending default back-reference");
2319 goto bak_ref_default;
2321 ref_filp->f_op->llseek(ref_filp, 0, SEEK_SET);
2322 ref_filp->f_op->write(ref_filp, (char*)p_bak_ref, ts->bak_ref_len, &ref_filp->f_pos);
2326 GTP_ERROR("invalid backup-reference request");
2334 for (j = 0; j < ref_grps; ++j)
2336 memset(&p_bak_ref[j * ref_seg_len], 0, ref_seg_len);
2337 p_bak_ref[j * ref_seg_len + ref_seg_len - 1] = 0x01; // checksum = 1
2339 ret = i2c_write_bytes(ts->client, GTP_REG_BAK_REF, p_bak_ref, ts->bak_ref_len);
2340 if (!IS_ERR(ref_filp))
2342 GTP_INFO("write backup-reference data into %s", GTP_BAK_REF_PATH);
2343 ref_filp->f_op->llseek(ref_filp, 0, SEEK_SET);
2344 ref_filp->f_op->write(ref_filp, (char*)p_bak_ref, ts->bak_ref_len, &ref_filp->f_pos);
2348 GTP_ERROR("failed to load the default backup reference");
2357 if (ref_filp && !IS_ERR(ref_filp))
2359 filp_close(ref_filp, NULL);
2365 static s32 gtp_verify_main_clk(u8 *p_main_clk)
2368 u8 main_clock = p_main_clk[0];
2371 if (main_clock < 50 || main_clock > 120)
2376 for (i = 0; i < 5; ++i)
2378 if (main_clock != p_main_clk[i])
2382 chksum += p_main_clk[i];
2384 chksum += p_main_clk[5];
2395 static s32 gtp_main_clk_proc(struct goodix_ts_data *ts)
2400 struct file *clk_filp = NULL;
2401 u8 p_main_clk[6] = {0};
2403 ret = gup_check_fs_mounted("/data");
2406 ts->clk_chk_fs_times++;
2407 GTP_DEBUG("Clock check /data times/MAX_TIMES: %d / %d", ts->clk_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2408 if (ts->clk_chk_fs_times < GTP_CHK_FS_MNT_MAX)
2411 GTP_INFO("/data not mounted.");
2414 GTP_INFO("Check /data mount timeout!");
2418 GTP_INFO("/data mounted!!!(%d/%d)", ts->clk_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2421 clk_filp = filp_open(GTP_MAIN_CLK_PATH, O_RDWR | O_CREAT, 0666);
2422 if (IS_ERR(clk_filp))
2424 GTP_ERROR("%s is unavailable, calculate main clock", GTP_MAIN_CLK_PATH);
2428 clk_filp->f_op->llseek(clk_filp, 0, SEEK_SET);
2429 clk_filp->f_op->read(clk_filp, (char *)p_main_clk, 6, &clk_filp->f_pos);
2431 ret = gtp_verify_main_clk(p_main_clk);
2434 // recalculate main clock & rewrite main clock data to file
2435 GTP_ERROR("main clock data in %s is wrong, recalculate main clock", GTP_MAIN_CLK_PATH);
2439 GTP_INFO("main clock data in %s used, main clock freq: %d", GTP_MAIN_CLK_PATH, p_main_clk[0]);
2440 filp_close(clk_filp, NULL);
2441 goto update_main_clk;
2446 gtp_esd_switch(ts->client, SWITCH_OFF);
2448 ret = gup_clk_calibration();
2449 gtp_esd_recovery(ts->client);
2452 gtp_esd_switch(ts->client, SWITCH_ON);
2455 GTP_INFO("calibrate main clock: %d", ret);
2456 if (ret < 50 || ret > 120)
2458 GTP_ERROR("wrong main clock: %d", ret);
2462 // Sum{0x8020~0x8025} = 0
2463 for (i = 0; i < 5; ++i)
2465 p_main_clk[i] = ret;
2466 clk_chksum += p_main_clk[i];
2468 p_main_clk[5] = 0 - clk_chksum;
2470 if (!IS_ERR(clk_filp))
2472 GTP_DEBUG("write main clock data into %s", GTP_MAIN_CLK_PATH);
2473 clk_filp->f_op->llseek(clk_filp, 0, SEEK_SET);
2474 clk_filp->f_op->write(clk_filp, (char *)p_main_clk, 6, &clk_filp->f_pos);
2475 filp_close(clk_filp, NULL);
2479 ret = i2c_write_bytes(ts->client, GTP_REG_MAIN_CLK, p_main_clk, 6);
2482 GTP_ERROR("update main clock failed!");
2488 if (!IS_ERR(clk_filp))
2490 filp_close(clk_filp, NULL);
2496 s32 gtp_gt9xxf_init(struct i2c_client *client)
2500 ret = gup_fw_download_proc(NULL, GTP_FL_FW_BURN);
2506 ret = gtp_fw_startup(client);
2514 void gtp_get_chip_type(struct goodix_ts_data *ts)
2516 u8 opr_buf[10] = {0x00};
2521 ret = gtp_i2c_read_dbl_check(ts->client, GTP_REG_CHIP_TYPE, opr_buf, 10);
2525 GTP_ERROR("Failed to get chip-type, set chip type default: GOODIX_GT9");
2526 ts->chip_type = CHIP_TYPE_GT9;
2530 if (!memcmp(opr_buf, "GOODIX_GT9", 10))
2532 ts->chip_type = CHIP_TYPE_GT9;
2536 ts->chip_type = CHIP_TYPE_GT9F;
2538 GTP_INFO("Chip Type: %s", (ts->chip_type == CHIP_TYPE_GT9) ? "GOODIX_GT9" : "GOODIX_GT9F");
2542 //************* For GT9XXF End ************//
2544 /*******************************************************
2548 client: i2c device struct.
2553 *******************************************************/
2554 static int goodix_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
2557 struct goodix_ts_data *ts;
2560 struct device_node *np = client->dev.of_node;
2561 enum of_gpio_flags rst_flags, pwr_flags;
2563 printk("%s() start\n", __func__);
2568 //do NOT remove these logs
2569 GTP_INFO("GTP Driver Version: %s", GTP_DRIVER_VERSION);
2570 GTP_INFO("GTP I2C Address: 0x%02x", client->addr);
2572 i2c_connect_client = client;
2574 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
2576 GTP_ERROR("I2C check functionality failed.");
2579 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
2582 GTP_ERROR("Alloc GFP_KERNEL memory failed.");
2586 memset(ts, 0, sizeof(*ts));
2589 dev_err(&client->dev, "no device tree\n");
2592 if (of_property_read_u32(np, "tp-size", &val)) {
2593 dev_err(&client->dev, "no max-x defined\n");
2599 mGtpChange_X2Y = TRUE;
2600 mGtp_X_Reverse = FALSE;
2601 mGtp_Y_Reverse = TRUE;
2602 }else if(val == 101){
2604 mGtpChange_X2Y = TRUE;
2605 mGtp_X_Reverse = TRUE;
2606 mGtp_Y_Reverse = FALSE;
2607 } else if (val == 911) {
2610 mGtpChange_X2Y = TRUE;
2611 mGtp_X_Reverse = FALSE;
2612 mGtp_Y_Reverse = TRUE;
2613 } else if (val == 970) {
2617 mGtpChange_X2Y = FALSE;
2618 mGtp_X_Reverse = FALSE;
2619 mGtp_Y_Reverse = TRUE;
2622 ts->tp_regulator = devm_regulator_get(&client->dev, "tp");
2623 if (IS_ERR(ts->tp_regulator)) {
2624 dev_err(&client->dev, "failed to get regulator, %ld\n",
2625 PTR_ERR(ts->tp_regulator));
2626 return PTR_ERR(ts->tp_regulator);
2629 ts->irq_pin = of_get_named_gpio_flags(np, "touch-gpio", 0, (enum of_gpio_flags *)(&ts->irq_flags));
2630 ts->rst_pin = of_get_named_gpio_flags(np, "reset-gpio", 0, &rst_flags);
2631 ts->pwr_pin = of_get_named_gpio_flags(np, "power-gpio", 0, &pwr_flags);
2632 //ts->tp_select_pin = of_get_named_gpio_flags(np, "tp-select-gpio", 0, &tp_select_flags);
2633 if (of_property_read_u32(np, "max-x", &val)) {
2634 dev_err(&client->dev, "no max-x defined\n");
2637 //ts->abs_x_max = val;
2638 if (of_property_read_u32(np, "max-y", &val)) {
2639 dev_err(&client->dev, "no max-y defined\n");
2642 //ts->abs_y_max = val;
2643 ts->pendown =PEN_RELEASE;
2644 ts->client = client;
2647 INIT_WORK(&ts->work, goodix_ts_work_func);
2648 ts->client = client;
2649 spin_lock_init(&ts->irq_lock); // 2.6.39 later
2650 // ts->irq_lock = SPIN_LOCK_UNLOCKED; // 2.6.39 & before
2652 ts->clk_tick_cnt = 2 * HZ; // HZ: clock ticks in 1 second generated by system
2653 GTP_DEBUG("Clock ticks for an esd cycle: %d", ts->clk_tick_cnt);
2654 spin_lock_init(&ts->esd_lock);
2655 // ts->esd_lock = SPIN_LOCK_UNLOCKED;
2658 i2c_set_clientdata(client, ts);
2660 ts->gtp_rawdiff_mode = 0;
2662 ret = gtp_request_io_port(ts);
2665 GTP_ERROR("GTP request IO port failed.");
2667 goto probe_init_error_requireio;
2670 if(gpio_get_value(ts->tp_select_pin))//WGJ
2672 printk("tp 11111111111111111111111111111 WGJ\n\n");
2673 mGtp_X_Reverse = FALSE;
2674 mGtp_Y_Reverse = TRUE;
2678 printk("tp 00000000000000000000000000000 DPT\n\n");
2679 mGtp_X_Reverse = TRUE;//FALSE;
2680 mGtp_Y_Reverse = TRUE;
2683 #if GTP_COMPATIBLE_MODE
2684 gtp_get_chip_type(ts);
2686 if (CHIP_TYPE_GT9F == ts->chip_type)
2688 ret = gtp_gt9xxf_init(ts->client);
2691 GTP_INFO("Failed to init GT9XXF.");
2696 ret = gtp_i2c_test(client);
2699 printk("<%s>_%d I2C communication ERROR!\n", __func__, __LINE__);
2700 goto probe_init_error;
2703 ret = gtp_read_version(client, &version_info);
2706 GTP_ERROR("Read version failed.");
2709 ret = gtp_init_panel(ts);
2712 GTP_ERROR("GTP init panel failed.");
2713 //ts->abs_x_max = GTP_MAX_WIDTH;
2714 //ts->abs_y_max = GTP_MAX_HEIGHT;
2715 ts->int_trigger_type = GTP_INT_TRIGGER;
2718 // Create proc file system
2719 gt91xx_config_proc = proc_create(GT91XX_CONFIG_PROC_FILE, 0666, NULL, &config_proc_ops);
2720 if (gt91xx_config_proc == NULL)
2722 GTP_ERROR("create_proc_entry %s failed\n", GT91XX_CONFIG_PROC_FILE);
2726 GTP_INFO("create proc entry %s success", GT91XX_CONFIG_PROC_FILE);
2730 ret = gup_init_update_proc(ts);
2733 GTP_ERROR("Create update thread error.");
2737 ret = gtp_request_input_dev(client, ts);
2740 GTP_ERROR("GTP request input dev failed");
2742 ret = gtp_request_irq(ts);
2745 GTP_INFO("GTP works in polling mode.");
2749 GTP_INFO("GTP works in interrupt mode.");
2757 #if GTP_CREATE_WR_NODE
2758 init_wr_node(client);
2762 gtp_esd_switch(client, SWITCH_ON);
2767 printk(" <%s>_%d prob error !!!!!!!!!!!!!!!\n", __func__, __LINE__);
2768 tp_unregister_fb(&ts->tp);
2769 GTP_GPIO_FREE(ts->rst_pin);
2770 GTP_GPIO_FREE(ts->irq_pin);
2771 probe_init_error_requireio:
2772 tp_unregister_fb(&ts->tp);
2778 /*******************************************************
2780 Goodix touchscreen driver release function.
2782 client: i2c device struct.
2784 Executive outcomes. 0---succeed.
2785 *******************************************************/
2786 static int goodix_ts_remove(struct i2c_client *client)
2788 struct goodix_ts_data *ts = i2c_get_clientdata(client);
2790 tp_unregister_fb(&ts->tp);
2794 #if GTP_CREATE_WR_NODE
2799 destroy_workqueue(gtp_esd_check_workqueue);
2806 //GTP_GPIO_AS_INPUT(GTP_INT_PORT);
2808 gpio_direction_input(ts->irq_pin);
2809 //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
2811 GTP_GPIO_FREE(ts->irq_pin);
2812 free_irq(client->irq, ts);
2816 hrtimer_cancel(&ts->timer);
2820 GTP_INFO("GTP driver removing...");
2821 i2c_set_clientdata(client, NULL);
2822 input_unregister_device(ts->input_dev);
2833 s32 gtp_i2c_read_no_rst(struct i2c_client *client, u8 *buf, s32 len)
2835 struct i2c_msg msgs[2];
2841 msgs[0].flags = !I2C_M_RD;
2842 msgs[0].addr = client->addr;
2843 msgs[0].len = GTP_ADDR_LENGTH;
2844 msgs[0].buf = &buf[0];
2845 //msgs[0].scl_rate = 300 * 1000; // for Rockchip, etc.
2847 msgs[1].flags = I2C_M_RD;
2848 msgs[1].addr = client->addr;
2849 msgs[1].len = len - GTP_ADDR_LENGTH;
2850 msgs[1].buf = &buf[GTP_ADDR_LENGTH];
2851 //msgs[1].scl_rate = 300 * 1000;
2855 ret = i2c_transfer(client->adapter, msgs, 2);
2861 GTP_ERROR("I2C Read: 0x%04X, %d bytes failed, errcode: %d!", (((u16)(buf[0] << 8)) | buf[1]), len-2, ret);
2866 s32 gtp_i2c_write_no_rst(struct i2c_client *client,u8 *buf,s32 len)
2874 msg.flags = !I2C_M_RD;
2875 msg.addr = client->addr;
2878 //msg.scl_rate = 300 * 1000; // for Rockchip, etc
2882 ret = i2c_transfer(client->adapter, &msg, 1);
2888 GTP_ERROR("I2C Write: 0x%04X, %d bytes failed, errcode: %d!", (((u16)(buf[0] << 8)) | buf[1]), len-2, ret);
2892 /*******************************************************
2894 switch on & off esd delayed work
2897 on: SWITCH_ON / SWITCH_OFF
2900 *********************************************************/
2901 void gtp_esd_switch(struct i2c_client *client, s32 on)
2903 struct goodix_ts_data *ts;
2905 ts = i2c_get_clientdata(client);
2906 spin_lock(&ts->esd_lock);
2908 if (SWITCH_ON == on) // switch on esd
2910 if (!ts->esd_running)
2912 ts->esd_running = 1;
2913 spin_unlock(&ts->esd_lock);
2914 GTP_INFO("Esd started");
2915 queue_delayed_work(gtp_esd_check_workqueue, >p_esd_check_work, ts->clk_tick_cnt);
2919 spin_unlock(&ts->esd_lock);
2922 else // switch off esd
2924 if (ts->esd_running)
2926 ts->esd_running = 0;
2927 spin_unlock(&ts->esd_lock);
2928 GTP_INFO("Esd cancelled");
2929 cancel_delayed_work_sync(>p_esd_check_work);
2933 spin_unlock(&ts->esd_lock);
2938 /*******************************************************
2940 Initialize external watchdog for esd protect
2944 result of i2c write operation.
2945 1: succeed, otherwise: failed
2946 *********************************************************/
2947 static s32 gtp_init_ext_watchdog(struct i2c_client *client)
2949 u8 opr_buffer[3] = {0x80, 0x41, 0xAA};
2950 GTP_DEBUG("[Esd]Init external watchdog");
2951 return gtp_i2c_write_no_rst(client, opr_buffer, 3);
2954 /*******************************************************
2956 Esd protect function.
2957 External watchdog added by meta, 2013/03/07
2962 *******************************************************/
2963 static void gtp_esd_check_func(struct work_struct *work)
2967 struct goodix_ts_data *ts = NULL;
2968 u8 esd_buf[5] = {0x80, 0x40};
2972 ts = i2c_get_clientdata(i2c_connect_client);
2974 if (ts->gtp_is_suspend)
2976 GTP_INFO("Esd suspended!");
2980 for (i = 0; i < 3; i++)
2982 ret = gtp_i2c_read_no_rst(ts->client, esd_buf, 4);
2984 GTP_DEBUG("[Esd]0x8040 = 0x%02X, 0x8041 = 0x%02X", esd_buf[2], esd_buf[3]);
2987 // IIC communication problem
2992 if ((esd_buf[2] == 0xAA) || (esd_buf[3] != 0xAA))
2994 // IC works abnormally..
2995 u8 chk_buf[4] = {0x80, 0x40};
2997 gtp_i2c_read_no_rst(ts->client, chk_buf, 4);
2999 GTP_DEBUG("[Check]0x8040 = 0x%02X, 0x8041 = 0x%02X", chk_buf[2], chk_buf[3]);
3001 if ((chk_buf[2] == 0xAA) || (chk_buf[3] != 0xAA))
3013 // IC works normally, Write 0x8040 0xAA, feed the dog
3015 gtp_i2c_write_no_rst(ts->client, esd_buf, 3);
3022 #if GTP_COMPATIBLE_MODE
3023 if (CHIP_TYPE_GT9F == ts->chip_type)
3025 if (ts->rqst_processing)
3027 GTP_INFO("Request processing, no esd recovery");
3031 GTP_ERROR("IC working abnormally! Process esd recovery.");
3037 gtp_i2c_write_no_rst(ts->client, esd_buf, 5);
3039 gtp_esd_recovery(ts->client);
3045 GTP_ERROR("IC working abnormally! Process reset guitar.");
3051 gtp_i2c_write_no_rst(ts->client, esd_buf, 5);
3053 gtp_reset_guitar(ts->client, 50);
3055 gtp_send_cfg(ts->client);
3059 if(!ts->gtp_is_suspend)
3061 queue_delayed_work(gtp_esd_check_workqueue, >p_esd_check_work, ts->clk_tick_cnt);
3065 GTP_INFO("Esd suspended!");
3071 static const struct i2c_device_id goodix_ts_id[] = {
3072 { GTP_I2C_NAME, 0 },
3076 static struct of_device_id goodix_ts_dt_ids[] = {
3077 { .compatible = "goodix,gt9xx" },
3081 static struct i2c_driver goodix_ts_driver = {
3082 .probe = goodix_ts_probe,
3083 .remove = goodix_ts_remove,
3084 .id_table = goodix_ts_id,
3086 .name = GTP_I2C_NAME,
3087 .owner = THIS_MODULE,
3088 .of_match_table = of_match_ptr(goodix_ts_dt_ids),
3092 /*******************************************************
3094 Driver Install function.
3098 Executive Outcomes. 0---succeed.
3099 ********************************************************/
3100 static int goodix_ts_init(void)
3105 GTP_INFO("GTP driver installing...");
3106 goodix_wq = create_singlethread_workqueue("goodix_wq");
3109 GTP_ERROR("Creat workqueue failed.");
3113 INIT_DELAYED_WORK(>p_esd_check_work, gtp_esd_check_func);
3114 gtp_esd_check_workqueue = create_workqueue("gtp_esd_check");
3116 ret = i2c_add_driver(&goodix_ts_driver);
3120 /*******************************************************
3122 Driver uninstall function.
3126 Executive Outcomes. 0---succeed.
3127 ********************************************************/
3128 static void goodix_ts_exit(void)
3131 GTP_INFO("GTP driver exited.");
3132 i2c_del_driver(&goodix_ts_driver);
3135 destroy_workqueue(goodix_wq);
3138 //late_initcall(goodix_ts_init);
3139 module_init(goodix_ts_init);
3141 module_exit(goodix_ts_exit);
3143 MODULE_DESCRIPTION("GTP Series Driver");
3144 MODULE_LICENSE("GPL");