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)
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); //
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 GTP_DEBUG("Config Groups\' Lengths: %d, %d, %d, %d, %d, %d",
1435 cfg_info_len[0], cfg_info_len[1], cfg_info_len[2], cfg_info_len[3],
1436 cfg_info_len[4], cfg_info_len[5]);
1439 #if GTP_COMPATIBLE_MODE
1440 if (CHIP_TYPE_GT9F == ts->chip_type)
1447 ret = gtp_i2c_read_dbl_check(ts->client, 0x41E4, opr_buf, 1);
1450 if (opr_buf[0] != 0xBE)
1453 GTP_ERROR("Firmware error, no config sent!");
1459 if ((!cfg_info_len[1]) && (!cfg_info_len[2]) &&
1460 (!cfg_info_len[3]) && (!cfg_info_len[4]) &&
1467 #if GTP_COMPATIBLE_MODE
1470 ret = gtp_i2c_read_dbl_check(ts->client, GTP_REG_SENSOR_ID, &sensor_id, 1);
1473 if (sensor_id >= 0x06)
1475 GTP_ERROR("Invalid sensor_id(0x%02X), No Config Sent!", sensor_id);
1476 ts->pnl_init_error = 1;
1482 GTP_ERROR("Failed to get sensor_id, No config sent!");
1483 ts->pnl_init_error = 1;
1486 GTP_INFO("Sensor_ID: %d", sensor_id);
1488 ts->gtp_cfg_len = cfg_info_len[sensor_id];
1489 GTP_INFO("CTP_CONFIG_GROUP%d used, config length: %d", sensor_id + 1, ts->gtp_cfg_len);
1491 if (ts->gtp_cfg_len < GTP_CONFIG_MIN_LENGTH)
1493 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);
1494 ts->pnl_init_error = 1;
1498 #if GTP_COMPATIBLE_MODE
1499 if (CHIP_TYPE_GT9F == ts->chip_type)
1506 ret = gtp_i2c_read_dbl_check(ts->client, GTP_REG_CONFIG_DATA, &opr_buf[0], 1);
1510 GTP_DEBUG("CFG_GROUP%d Config Version: %d, 0x%02X; IC Config Version: %d, 0x%02X", sensor_id+1,
1511 send_cfg_buf[sensor_id][0], send_cfg_buf[sensor_id][0], opr_buf[0], opr_buf[0]);
1513 if (opr_buf[0] < 90)
1515 GTP_INFO(" <%s>_%d \n", __func__, __LINE__);
1516 grp_cfg_version = send_cfg_buf[sensor_id][0]; // backup group config version
1517 send_cfg_buf[sensor_id][0] = 0x00;
1520 else // treated as fixed config, not send config
1522 GTP_INFO("Ic fixed config with config version(%d, 0x%02X)", opr_buf[0], opr_buf[0]);
1530 GTP_ERROR("Failed to get ic config version!No config sent!");
1535 memset(&config[GTP_ADDR_LENGTH], 0, GTP_CONFIG_MAX_LENGTH);
1536 memcpy(&config[GTP_ADDR_LENGTH], send_cfg_buf[sensor_id], ts->gtp_cfg_len);
1539 config[RESOLUTION_LOC] = (u8)GTP_MAX_WIDTH;
1540 config[RESOLUTION_LOC + 1] = (u8)(GTP_MAX_WIDTH>>8);
1541 config[RESOLUTION_LOC + 2] = (u8)GTP_MAX_HEIGHT;
1542 config[RESOLUTION_LOC + 3] = (u8)(GTP_MAX_HEIGHT>>8);
1544 if (GTP_INT_TRIGGER == 0) //RISING
1546 config[TRIGGER_LOC] &= 0xfe;
1548 else if (GTP_INT_TRIGGER == 1) //FALLING
1550 config[TRIGGER_LOC] |= 0x01;
1552 #endif // GTP_CUSTOM_CFG
1555 for (i = GTP_ADDR_LENGTH; i < ts->gtp_cfg_len; i++)
1557 check_sum += config[i];
1559 config[ts->gtp_cfg_len] = (~check_sum) + 1;
1561 #else // driver not send config
1563 ts->gtp_cfg_len = GTP_CONFIG_MAX_LENGTH;
1564 ret = gtp_i2c_read(ts->client, config, ts->gtp_cfg_len + GTP_ADDR_LENGTH);
1567 GTP_ERROR("Read Config Failed, Using Default Resolution & INT Trigger!");
1568 //ts->abs_x_max = GTP_MAX_WIDTH;
1569 //ts->abs_y_max = GTP_MAX_HEIGHT;
1570 ts->int_trigger_type = GTP_INT_TRIGGER;
1573 #endif // GTP_DRIVER_SEND_CFG
1575 if ((ts->abs_x_max == 0) && (ts->abs_y_max == 0))
1577 ts->abs_x_max = (config[RESOLUTION_LOC + 1] << 8) + config[RESOLUTION_LOC];
1578 ts->abs_y_max = (config[RESOLUTION_LOC + 3] << 8) + config[RESOLUTION_LOC + 2];
1579 ts->int_trigger_type = (config[TRIGGER_LOC]) & 0x03;
1580 GTP_INFO(" <%s>_%d <%d, %d>\n", __func__, __LINE__, ts->abs_x_max, ts->abs_y_max);
1582 GTP_INFO(" <%s>_%d \n", __func__, __LINE__);
1583 #if GTP_COMPATIBLE_MODE
1584 if (CHIP_TYPE_GT9F == ts->chip_type)
1590 have_key = (config[GTP_REG_HAVE_KEY - GTP_REG_CONFIG_DATA + 2] & 0x01);
1592 if (1 == ts->is_950)
1594 driver_num = config[GTP_REG_MATRIX_DRVNUM - GTP_REG_CONFIG_DATA + 2];
1595 sensor_num = config[GTP_REG_MATRIX_SENNUM - GTP_REG_CONFIG_DATA + 2];
1600 ts->bak_ref_len = (driver_num * (sensor_num - 1) + 2) * 2 * 6;
1604 driver_num = (config[CFG_LOC_DRVA_NUM] & 0x1F) + (config[CFG_LOC_DRVB_NUM]&0x1F);
1609 sensor_num = (config[CFG_LOC_SENS_NUM] & 0x0F) + ((config[CFG_LOC_SENS_NUM] >> 4) & 0x0F);
1610 ts->bak_ref_len = (driver_num * (sensor_num - 2) + 2) * 2;
1613 GTP_INFO("Drv * Sen: %d * %d(key: %d), X_MAX: %d, Y_MAX: %d, TRIGGER: 0x%02x",
1614 driver_num, sensor_num, have_key, ts->abs_x_max,ts->abs_y_max,ts->int_trigger_type);
1620 #if GTP_DRIVER_SEND_CFG
1621 GTP_INFO(" <%s>_%d \n", __func__, __LINE__);
1622 ret = gtp_send_cfg(ts->client);
1625 GTP_ERROR("Send config error.");
1627 // set config version to CTP_CFG_GROUP, for resume to send config
1628 config[GTP_ADDR_LENGTH] = grp_cfg_version;
1630 for (i = GTP_ADDR_LENGTH; i < ts->gtp_cfg_len; i++)
1632 check_sum += config[i];
1634 config[ts->gtp_cfg_len] = (~check_sum) + 1;
1636 GTP_INFO("X_MAX: %d, Y_MAX: %d, TRIGGER: 0x%02x", ts->abs_x_max,ts->abs_y_max,ts->int_trigger_type);
1644 static ssize_t gt91xx_config_read_proc(struct file *file, char __user *page, size_t size, loff_t *ppos)
1647 char temp_data[GTP_CONFIG_MAX_LENGTH + 2] = {0x80, 0x47};
1654 ptr += sprintf(ptr, "==== GT9XX config init value====\n");
1656 for (i = 0 ; i < GTP_CONFIG_MAX_LENGTH ; i++)
1658 ptr += sprintf(ptr, "0x%02X ", config[i + 2]);
1661 ptr += sprintf(ptr, "\n");
1664 ptr += sprintf(ptr, "\n");
1666 ptr += sprintf(ptr, "==== GT9XX config real value====\n");
1667 gtp_i2c_read(i2c_connect_client, temp_data, GTP_CONFIG_MAX_LENGTH + 2);
1668 for (i = 0 ; i < GTP_CONFIG_MAX_LENGTH ; i++)
1670 ptr += sprintf(ptr, "0x%02X ", temp_data[i+2]);
1673 ptr += sprintf(ptr, "\n");
1675 *ppos += ptr - page;
1676 return (ptr - page);
1679 static ssize_t gt91xx_config_write_proc(struct file *filp, const char __user *buffer, size_t count, loff_t *off)
1683 if (count > GTP_CONFIG_MAX_LENGTH)
1685 GTP_ERROR("size not match [%d:%zu]\n", GTP_CONFIG_MAX_LENGTH, count);
1689 if (copy_from_user(&config[2], buffer, count))
1691 GTP_ERROR("copy from user fail\n");
1695 ret = gtp_send_cfg(i2c_connect_client);
1699 GTP_ERROR("send config failed.");
1704 /*******************************************************
1709 version: buffer to keep ic firmware version
1711 read operation return.
1712 2: succeed, otherwise: failed
1713 *******************************************************/
1714 s32 gtp_read_version(struct i2c_client *client, u16* version)
1717 u8 buf[8] = {GTP_REG_VERSION >> 8, GTP_REG_VERSION & 0xff};
1721 ret = gtp_i2c_read(client, buf, sizeof(buf));
1724 GTP_ERROR("GTP read version failed");
1730 *version = (buf[7] << 8) | buf[6];
1734 GTP_INFO("IC Version: %c%c%c_%02x%02x", buf[2], buf[3], buf[4], buf[7], buf[6]);
1738 GTP_INFO("IC Version: %c%c%c%c_%02x%02x", buf[2], buf[3], buf[4], buf[5], buf[7], buf[6]);
1743 /*******************************************************
1750 2: succeed, otherwise failed.
1751 *******************************************************/
1752 static s8 gtp_i2c_test(struct i2c_client *client)
1754 u8 test[3] = {GTP_REG_CONFIG_DATA >> 8, GTP_REG_CONFIG_DATA & 0xff};
1762 ret = gtp_i2c_read(client, test, 3);
1767 GTP_ERROR("GTP i2c test failed time %d.",retry);
1773 /*******************************************************
1775 Request gpio(INT & RST) ports.
1780 >= 0: succeed, < 0: failed
1781 *******************************************************/
1782 static s8 gtp_request_io_port(struct goodix_ts_data *ts)
1788 ret = GTP_GPIO_REQUEST(ts->tp_select_pin, "GTP_tp_select_PORT");
1791 GTP_ERROR("1Failed to request GPIO:%d, ERRNO:%d",(s32)ts->tp_select_pin, ret);
1796 gpio_direction_input(ts->tp_select_pin);
1799 ret = GTP_GPIO_REQUEST(ts->rst_pin, "GTP_RST_PORT");
1802 GTP_ERROR("2Failed to request GPIO:%d, ERRNO:%d",(s32)ts->rst_pin, ret);
1806 ret = GTP_GPIO_REQUEST(ts->irq_pin, "GTP_INT_IRQ");
1809 GTP_ERROR("3Failed to request GPIO:%d, ERRNO:%d", (s32)ts->irq_pin, ret);
1814 //GTP_GPIO_AS_INT(GTP_INT_PORT);
1815 gpio_direction_input(ts->irq_pin);
1816 //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1817 //s3c_gpio_cfgpin(pin, GTP_INT_CFG);
1819 //ts->client->irq = ts->irq_pin;
1822 //GTP_GPIO_AS_INPUT(ts->rst_pin);
1823 gpio_direction_input(ts->rst_pin);
1824 //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1826 gtp_reset_guitar(ts->client, 20);
1830 GTP_GPIO_FREE(ts->rst_pin);
1831 GTP_GPIO_FREE(ts->irq_pin);
1837 /*******************************************************
1844 0: succeed, -1: failed.
1845 *******************************************************/
1846 static s8 gtp_request_irq(struct goodix_ts_data *ts)
1851 GTP_DEBUG("INT trigger type:%x", ts->int_trigger_type);
1853 ts->irq=gpio_to_irq(ts->irq_pin); //If not defined in client
1856 ts->client->irq = ts->irq;
1857 ret = devm_request_threaded_irq(&(ts->client->dev), ts->irq, NULL,
1858 goodix_ts_irq_handler, ts->irq_flags | IRQF_ONESHOT /*irq_table[ts->int_trigger_type]*/,
1859 ts->client->name, ts);
1861 GTP_ERROR("Cannot allocate ts INT!ERRNO:%d\n", ret);
1864 //gtp_irq_disable(ts->irq);
1865 GTP_INFO(" <%s>_%d ts->irq=%d ret = %d\n", __func__, __LINE__, ts->irq, ret);
1867 GTP_ERROR(" ts->irq error \n");
1872 ret = request_irq(ts->client->irq,
1873 goodix_ts_irq_handler,
1874 irq_table[ts->int_trigger_type],
1881 GTP_ERROR("Request IRQ failed!ERRNO:%d.", ret);
1882 //GTP_GPIO_AS_INPUT(GTP_INT_PORT);
1883 gpio_direction_input(ts->irq_pin);
1884 //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
1886 GTP_GPIO_FREE(ts->irq_pin);
1888 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1889 ts->timer.function = goodix_ts_timer_handler;
1890 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
1895 GTP_INFO(" <%s>_%d ts->irq=%d ret = %d\n", __func__, __LINE__, ts->irq, ret);
1896 gtp_irq_disable(ts);
1902 /*******************************************************
1904 Early suspend function.
1906 h: early_suspend struct.
1909 *******************************************************/
1910 static int goodix_ts_early_suspend(struct tp_device *tp_d)
1912 struct goodix_ts_data *ts;
1914 struct regulator *regulator_tp = NULL;
1917 ts = container_of(tp_d, struct goodix_ts_data, tp);
1920 GTP_INFO("System suspend.");
1922 ts->gtp_is_suspend = 1;
1924 gtp_esd_switch(ts->client, SWITCH_OFF);
1927 #if GTP_GESTURE_WAKEUP
1928 ret = gtp_enter_doze(ts);
1932 gtp_irq_disable(ts);
1936 hrtimer_cancel(&ts->timer);
1938 ret = gtp_enter_sleep(ts);
1942 printk("GTP early suspend failed.");
1944 // to avoid waking up while not sleeping
1945 // delay 48 + 10ms to ensure reliability
1947 regulator_tp = regulator_get(NULL,"vcc_tp");
1948 if(IS_ERR(regulator_tp))
1950 printk("!!!!%s:%d:get regulator_tp failed\n",__func__,__LINE__);
1953 while(regulator_is_enabled(regulator_tp))
1955 reg = regulator_disable(regulator_tp);
1958 regulator_put(regulator_tp);
1963 /*******************************************************
1965 Late resume function.
1967 h: early_suspend struct.
1970 *******************************************************/
1971 static int goodix_ts_early_resume(struct tp_device *tp_d)
1973 struct goodix_ts_data *ts;
1975 struct regulator *regulator_tp = NULL;
1977 ts = container_of(tp_d, struct goodix_ts_data, tp);
1980 GTP_INFO("System resume.");
1982 regulator_tp = regulator_get(NULL, "vcc_tp");
1983 if(IS_ERR(regulator_tp))
1985 printk("!!!!%s:%d:get regulator_tp failed\n",__func__,__LINE__);
1988 while(!(regulator_is_enabled(regulator_tp)))
1990 reg = regulator_enable(regulator_tp);
1993 regulator_put(regulator_tp);
1996 ret = gtp_wakeup_sleep(ts);
1998 #if GTP_GESTURE_WAKEUP
1999 doze_status = DOZE_DISABLED;
2004 GTP_ERROR("GTP later resume failed.");
2006 #if (GTP_COMPATIBLE_MODE)
2007 if (CHIP_TYPE_GT9F == ts->chip_type)
2014 gtp_send_cfg(ts->client);
2023 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
2026 ts->gtp_is_suspend = 0;
2028 gtp_esd_switch(ts->client, SWITCH_ON);
2034 /*******************************************************
2036 Request input device Function.
2041 0: succeed, otherwise: failed.
2042 *******************************************************/
2043 static s8 gtp_request_input_dev(struct goodix_ts_data *ts)
2047 #if GTP_HAVE_TOUCH_KEY
2052 ts->input_dev = input_allocate_device();
2053 if (ts->input_dev == NULL)
2055 GTP_ERROR("Failed to allocate input device.");
2059 ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;
2060 #if GTP_ICS_SLOT_REPORT
2061 input_mt_init_slots(ts->input_dev, 16); // in case of "out of memory"
2063 ts->input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
2065 __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);
2067 #if GTP_HAVE_TOUCH_KEY
2068 for (index = 0; index < GTP_MAX_KEY_NUM; index++)
2070 input_set_capability(ts->input_dev, EV_KEY, touch_key_array[index]);
2074 #if GTP_GESTURE_WAKEUP
2075 input_set_capability(ts->input_dev, EV_KEY, KEY_POWER);
2079 GTP_SWAP(ts->abs_x_max, ts->abs_y_max);
2082 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0, ts->abs_x_max, 0, 0);
2083 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0, ts->abs_y_max, 0, 0);
2084 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
2085 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
2086 input_set_abs_params(ts->input_dev, ABS_MT_TRACKING_ID, 0, 255, 0, 0);
2088 sprintf(phys, "input/ts");
2089 ts->input_dev->name = goodix_ts_name;
2090 ts->input_dev->phys = phys;
2091 ts->input_dev->id.bustype = BUS_I2C;
2092 ts->input_dev->id.vendor = 0xDEAD;
2093 ts->input_dev->id.product = 0xBEEF;
2094 ts->input_dev->id.version = 10427;
2096 ret = input_register_device(ts->input_dev);
2099 GTP_ERROR("Register %s input device failed", ts->input_dev->name);
2103 ts->tp.tp_resume = goodix_ts_early_resume;
2104 ts->tp.tp_suspend = goodix_ts_early_suspend;
2105 tp_register_fb(&ts->tp);
2114 //************** For GT9XXF Start *************//
2115 #if GTP_COMPATIBLE_MODE
2117 s32 gtp_fw_startup(struct i2c_client *client)
2121 struct goodix_ts_data *ts = i2c_get_clientdata(client);
2124 ret = i2c_write_bytes(client, 0x8041, opr_buf, 1);
2130 //release SS51 & DSP
2132 ret = i2c_write_bytes(client, 0x4180, opr_buf, 1);
2138 gtp_int_sync(25, ts);
2140 //check fw run status
2141 ret = i2c_read_bytes(client, 0x8041, opr_buf, 1);
2146 if(0xAA == opr_buf[0])
2148 GTP_ERROR("IC works abnormally,startup failed.");
2153 GTP_INFO("IC works normally, Startup success.");
2155 i2c_write_bytes(client, 0x8041, opr_buf, 1);
2160 static s32 gtp_esd_recovery(struct i2c_client *client)
2164 struct goodix_ts_data *ts;
2166 ts = i2c_get_clientdata(client);
2168 gtp_irq_disable(ts);
2170 GTP_INFO("GT9XXF esd recovery mode");
2171 for (retry = 0; retry < 5; retry++)
2173 ret = gup_fw_download_proc(NULL, GTP_FL_ESD_RECOVERY);
2176 GTP_ERROR("esd recovery failed %d", retry+1);
2179 ret = gtp_fw_startup(ts->client);
2182 GTP_ERROR("GT9XXF start up failed %d", retry+1);
2191 GTP_ERROR("failed to esd recovery");
2195 GTP_INFO("Esd recovery successful");
2199 void gtp_recovery_reset(struct i2c_client *client)
2202 gtp_esd_switch(client, SWITCH_OFF);
2206 gtp_esd_recovery(client);
2209 gtp_esd_switch(client, SWITCH_ON);
2213 static s32 gtp_bak_ref_proc(struct goodix_ts_data *ts, u8 mode)
2221 s32 ref_seg_len = 0;
2223 struct file *ref_filp = NULL;
2226 ret = gup_check_fs_mounted("/data");
2229 ts->ref_chk_fs_times++;
2230 GTP_DEBUG("Ref check /data times/MAX_TIMES: %d / %d", ts->ref_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2231 if (ts->ref_chk_fs_times < GTP_CHK_FS_MNT_MAX)
2234 GTP_INFO("/data not mounted.");
2237 GTP_INFO("check /data mount timeout...");
2241 GTP_INFO("/data mounted!!!(%d/%d)", ts->ref_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2244 p_bak_ref = (u8 *)kzalloc(ts->bak_ref_len, GFP_KERNEL);
2246 if (NULL == p_bak_ref)
2248 GTP_ERROR("Allocate memory for p_bak_ref failed!");
2254 ref_seg_len = ts->bak_ref_len / 6;
2259 ref_seg_len = ts->bak_ref_len;
2262 ref_filp = filp_open(GTP_BAK_REF_PATH, O_RDWR | O_CREAT, 0666);
2263 if (IS_ERR(ref_filp))
2265 GTP_ERROR("Failed to open/create %s.", GTP_BAK_REF_PATH);
2266 if (GTP_BAK_REF_SEND == mode)
2268 goto bak_ref_default;
2278 case GTP_BAK_REF_SEND:
2279 GTP_INFO("Send backup-reference");
2280 ref_filp->f_op->llseek(ref_filp, 0, SEEK_SET);
2281 ret = ref_filp->f_op->read(ref_filp, (char*)p_bak_ref, ts->bak_ref_len, &ref_filp->f_pos);
2284 GTP_ERROR("failed to read bak_ref info from file, sending defualt bak_ref");
2285 goto bak_ref_default;
2287 for (j = 0; j < ref_grps; ++j)
2290 for (i = 0; i < (ref_seg_len); i += 2)
2292 ref_sum += (p_bak_ref[i + j * ref_seg_len] << 8) + p_bak_ref[i+1 + j * ref_seg_len];
2294 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]);
2295 chksum = (p_bak_ref[j * ref_seg_len + ref_seg_len -2] << 8) + (p_bak_ref[j * ref_seg_len + ref_seg_len -1]);
2296 GTP_DEBUG("learn count = %d", learn_cnt);
2297 GTP_DEBUG("chksum = %d", chksum);
2298 GTP_DEBUG("ref_sum = 0x%04X", ref_sum & 0xFFFF);
2299 // Sum(1~ref_seg_len) == 1
2302 GTP_INFO("wrong chksum for bak_ref, reset to 0x00 bak_ref");
2303 memset(&p_bak_ref[j * ref_seg_len], 0, ref_seg_len);
2304 p_bak_ref[ref_seg_len + j * ref_seg_len - 1] = 0x01;
2308 if (j == (ref_grps - 1))
2310 GTP_INFO("backup-reference data in %s used", GTP_BAK_REF_PATH);
2314 ret = i2c_write_bytes(ts->client, GTP_REG_BAK_REF, p_bak_ref, ts->bak_ref_len);
2317 GTP_ERROR("failed to send bak_ref because of iic comm error");
2322 case GTP_BAK_REF_STORE:
2323 GTP_INFO("Store backup-reference");
2324 ret = i2c_read_bytes(ts->client, GTP_REG_BAK_REF, p_bak_ref, ts->bak_ref_len);
2327 GTP_ERROR("failed to read bak_ref info, sending default back-reference");
2328 goto bak_ref_default;
2330 ref_filp->f_op->llseek(ref_filp, 0, SEEK_SET);
2331 ref_filp->f_op->write(ref_filp, (char*)p_bak_ref, ts->bak_ref_len, &ref_filp->f_pos);
2335 GTP_ERROR("invalid backup-reference request");
2343 for (j = 0; j < ref_grps; ++j)
2345 memset(&p_bak_ref[j * ref_seg_len], 0, ref_seg_len);
2346 p_bak_ref[j * ref_seg_len + ref_seg_len - 1] = 0x01; // checksum = 1
2348 ret = i2c_write_bytes(ts->client, GTP_REG_BAK_REF, p_bak_ref, ts->bak_ref_len);
2349 if (!IS_ERR(ref_filp))
2351 GTP_INFO("write backup-reference data into %s", GTP_BAK_REF_PATH);
2352 ref_filp->f_op->llseek(ref_filp, 0, SEEK_SET);
2353 ref_filp->f_op->write(ref_filp, (char*)p_bak_ref, ts->bak_ref_len, &ref_filp->f_pos);
2357 GTP_ERROR("failed to load the default backup reference");
2366 if (ref_filp && !IS_ERR(ref_filp))
2368 filp_close(ref_filp, NULL);
2374 static s32 gtp_verify_main_clk(u8 *p_main_clk)
2377 u8 main_clock = p_main_clk[0];
2380 if (main_clock < 50 || main_clock > 120)
2385 for (i = 0; i < 5; ++i)
2387 if (main_clock != p_main_clk[i])
2391 chksum += p_main_clk[i];
2393 chksum += p_main_clk[5];
2404 static s32 gtp_main_clk_proc(struct goodix_ts_data *ts)
2409 struct file *clk_filp = NULL;
2410 u8 p_main_clk[6] = {0};
2412 ret = gup_check_fs_mounted("/data");
2415 ts->clk_chk_fs_times++;
2416 GTP_DEBUG("Clock check /data times/MAX_TIMES: %d / %d", ts->clk_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2417 if (ts->clk_chk_fs_times < GTP_CHK_FS_MNT_MAX)
2420 GTP_INFO("/data not mounted.");
2423 GTP_INFO("Check /data mount timeout!");
2427 GTP_INFO("/data mounted!!!(%d/%d)", ts->clk_chk_fs_times, GTP_CHK_FS_MNT_MAX);
2430 clk_filp = filp_open(GTP_MAIN_CLK_PATH, O_RDWR | O_CREAT, 0666);
2431 if (IS_ERR(clk_filp))
2433 GTP_ERROR("%s is unavailable, calculate main clock", GTP_MAIN_CLK_PATH);
2437 clk_filp->f_op->llseek(clk_filp, 0, SEEK_SET);
2438 clk_filp->f_op->read(clk_filp, (char *)p_main_clk, 6, &clk_filp->f_pos);
2440 ret = gtp_verify_main_clk(p_main_clk);
2443 // recalculate main clock & rewrite main clock data to file
2444 GTP_ERROR("main clock data in %s is wrong, recalculate main clock", GTP_MAIN_CLK_PATH);
2448 GTP_INFO("main clock data in %s used, main clock freq: %d", GTP_MAIN_CLK_PATH, p_main_clk[0]);
2449 filp_close(clk_filp, NULL);
2450 goto update_main_clk;
2455 gtp_esd_switch(ts->client, SWITCH_OFF);
2457 ret = gup_clk_calibration();
2458 gtp_esd_recovery(ts->client);
2461 gtp_esd_switch(ts->client, SWITCH_ON);
2464 GTP_INFO("calibrate main clock: %d", ret);
2465 if (ret < 50 || ret > 120)
2467 GTP_ERROR("wrong main clock: %d", ret);
2471 // Sum{0x8020~0x8025} = 0
2472 for (i = 0; i < 5; ++i)
2474 p_main_clk[i] = ret;
2475 clk_chksum += p_main_clk[i];
2477 p_main_clk[5] = 0 - clk_chksum;
2479 if (!IS_ERR(clk_filp))
2481 GTP_DEBUG("write main clock data into %s", GTP_MAIN_CLK_PATH);
2482 clk_filp->f_op->llseek(clk_filp, 0, SEEK_SET);
2483 clk_filp->f_op->write(clk_filp, (char *)p_main_clk, 6, &clk_filp->f_pos);
2484 filp_close(clk_filp, NULL);
2488 ret = i2c_write_bytes(ts->client, GTP_REG_MAIN_CLK, p_main_clk, 6);
2491 GTP_ERROR("update main clock failed!");
2497 if (!IS_ERR(clk_filp))
2499 filp_close(clk_filp, NULL);
2505 s32 gtp_gt9xxf_init(struct i2c_client *client)
2509 ret = gup_fw_download_proc(NULL, GTP_FL_FW_BURN);
2515 ret = gtp_fw_startup(client);
2523 void gtp_get_chip_type(struct goodix_ts_data *ts)
2525 u8 opr_buf[10] = {0x00};
2530 ret = gtp_i2c_read_dbl_check(ts->client, GTP_REG_CHIP_TYPE, opr_buf, 10);
2534 GTP_ERROR("Failed to get chip-type, set chip type default: GOODIX_GT9");
2535 ts->chip_type = CHIP_TYPE_GT9;
2539 if (!memcmp(opr_buf, "GOODIX_GT9", 10))
2541 ts->chip_type = CHIP_TYPE_GT9;
2545 ts->chip_type = CHIP_TYPE_GT9F;
2547 GTP_INFO("Chip Type: %s", (ts->chip_type == CHIP_TYPE_GT9) ? "GOODIX_GT9" : "GOODIX_GT9F");
2551 //************* For GT9XXF End ************//
2553 /*******************************************************
2557 client: i2c device struct.
2562 *******************************************************/
2563 static int goodix_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
2566 struct goodix_ts_data *ts;
2569 struct device_node *np = client->dev.of_node;
2570 enum of_gpio_flags rst_flags, pwr_flags;
2572 printk("%s() start\n", __func__);
2577 //do NOT remove these logs
2578 GTP_INFO("GTP Driver Version: %s", GTP_DRIVER_VERSION);
2579 GTP_INFO("GTP I2C Address: 0x%02x", client->addr);
2581 i2c_connect_client = client;
2583 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
2585 GTP_ERROR("I2C check functionality failed.");
2588 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
2591 GTP_ERROR("Alloc GFP_KERNEL memory failed.");
2595 memset(ts, 0, sizeof(*ts));
2598 dev_err(&client->dev, "no device tree\n");
2601 if (of_property_read_u32(np, "tp-size", &val)) {
2602 dev_err(&client->dev, "no max-x defined\n");
2608 mGtpChange_X2Y = TRUE;
2609 mGtp_X_Reverse = FALSE;
2610 mGtp_Y_Reverse = TRUE;
2611 }else if(val == 101){
2613 mGtpChange_X2Y = TRUE;
2614 mGtp_X_Reverse = TRUE;
2615 mGtp_Y_Reverse = FALSE;
2616 } else if (val == 911) {
2619 mGtpChange_X2Y = TRUE;
2620 mGtp_X_Reverse = FALSE;
2621 mGtp_Y_Reverse = TRUE;
2624 ts->irq_pin = of_get_named_gpio_flags(np, "touch-gpio", 0, (enum of_gpio_flags *)(&ts->irq_flags));
2625 ts->rst_pin = of_get_named_gpio_flags(np, "reset-gpio", 0, &rst_flags);
2626 ts->pwr_pin = of_get_named_gpio_flags(np, "power-gpio", 0, &pwr_flags);
2627 //ts->tp_select_pin = of_get_named_gpio_flags(np, "tp-select-gpio", 0, &tp_select_flags);
2628 if (of_property_read_u32(np, "max-x", &val)) {
2629 dev_err(&client->dev, "no max-x defined\n");
2632 //ts->abs_x_max = val;
2633 if (of_property_read_u32(np, "max-y", &val)) {
2634 dev_err(&client->dev, "no max-y defined\n");
2637 //ts->abs_y_max = val;
2638 ts->pendown =PEN_RELEASE;
2639 ts->client = client;
2642 INIT_WORK(&ts->work, goodix_ts_work_func);
2643 ts->client = client;
2644 spin_lock_init(&ts->irq_lock); // 2.6.39 later
2645 // ts->irq_lock = SPIN_LOCK_UNLOCKED; // 2.6.39 & before
2647 ts->clk_tick_cnt = 2 * HZ; // HZ: clock ticks in 1 second generated by system
2648 GTP_DEBUG("Clock ticks for an esd cycle: %d", ts->clk_tick_cnt);
2649 spin_lock_init(&ts->esd_lock);
2650 // ts->esd_lock = SPIN_LOCK_UNLOCKED;
2653 i2c_set_clientdata(client, ts);
2655 ts->gtp_rawdiff_mode = 0;
2657 ret = gtp_request_io_port(ts);
2660 GTP_ERROR("GTP request IO port failed.");
2662 goto probe_init_error_requireio;
2665 if(gpio_get_value(ts->tp_select_pin))//WGJ
2667 printk("tp 11111111111111111111111111111 WGJ\n\n");
2668 mGtp_X_Reverse = FALSE;
2669 mGtp_Y_Reverse = TRUE;
2673 printk("tp 00000000000000000000000000000 DPT\n\n");
2674 mGtp_X_Reverse = TRUE;//FALSE;
2675 mGtp_Y_Reverse = TRUE;
2678 #if GTP_COMPATIBLE_MODE
2679 gtp_get_chip_type(ts);
2681 if (CHIP_TYPE_GT9F == ts->chip_type)
2683 ret = gtp_gt9xxf_init(ts->client);
2686 GTP_INFO("Failed to init GT9XXF.");
2691 ret = gtp_i2c_test(client);
2694 printk("<%s>_%d I2C communication ERROR!\n", __func__, __LINE__);
2695 goto probe_init_error;
2698 ret = gtp_read_version(client, &version_info);
2701 GTP_ERROR("Read version failed.");
2704 ret = gtp_init_panel(ts);
2707 GTP_ERROR("GTP init panel failed.");
2708 //ts->abs_x_max = GTP_MAX_WIDTH;
2709 //ts->abs_y_max = GTP_MAX_HEIGHT;
2710 ts->int_trigger_type = GTP_INT_TRIGGER;
2713 // Create proc file system
2714 gt91xx_config_proc = proc_create(GT91XX_CONFIG_PROC_FILE, 0666, NULL, &config_proc_ops);
2715 if (gt91xx_config_proc == NULL)
2717 GTP_ERROR("create_proc_entry %s failed\n", GT91XX_CONFIG_PROC_FILE);
2721 GTP_INFO("create proc entry %s success", GT91XX_CONFIG_PROC_FILE);
2725 ret = gup_init_update_proc(ts);
2728 GTP_ERROR("Create update thread error.");
2732 ret = gtp_request_input_dev(ts);
2735 GTP_ERROR("GTP request input dev failed");
2737 ret = gtp_request_irq(ts);
2740 GTP_INFO("GTP works in polling mode.");
2744 GTP_INFO("GTP works in interrupt mode.");
2752 #if GTP_CREATE_WR_NODE
2753 init_wr_node(client);
2757 gtp_esd_switch(client, SWITCH_ON);
2762 printk(" <%s>_%d prob error !!!!!!!!!!!!!!!\n", __func__, __LINE__);
2763 tp_unregister_fb(&ts->tp);
2764 GTP_GPIO_FREE(ts->rst_pin);
2765 GTP_GPIO_FREE(ts->irq_pin);
2766 probe_init_error_requireio:
2767 tp_unregister_fb(&ts->tp);
2773 /*******************************************************
2775 Goodix touchscreen driver release function.
2777 client: i2c device struct.
2779 Executive outcomes. 0---succeed.
2780 *******************************************************/
2781 static int goodix_ts_remove(struct i2c_client *client)
2783 struct goodix_ts_data *ts = i2c_get_clientdata(client);
2785 tp_unregister_fb(&ts->tp);
2789 #if GTP_CREATE_WR_NODE
2794 destroy_workqueue(gtp_esd_check_workqueue);
2801 //GTP_GPIO_AS_INPUT(GTP_INT_PORT);
2803 gpio_direction_input(ts->irq_pin);
2804 //s3c_gpio_setpull(pin, S3C_GPIO_PULL_NONE);
2806 GTP_GPIO_FREE(ts->irq_pin);
2807 free_irq(client->irq, ts);
2811 hrtimer_cancel(&ts->timer);
2815 GTP_INFO("GTP driver removing...");
2816 i2c_set_clientdata(client, NULL);
2817 input_unregister_device(ts->input_dev);
2828 s32 gtp_i2c_read_no_rst(struct i2c_client *client, u8 *buf, s32 len)
2830 struct i2c_msg msgs[2];
2836 msgs[0].flags = !I2C_M_RD;
2837 msgs[0].addr = client->addr;
2838 msgs[0].len = GTP_ADDR_LENGTH;
2839 msgs[0].buf = &buf[0];
2840 //msgs[0].scl_rate = 300 * 1000; // for Rockchip, etc.
2842 msgs[1].flags = I2C_M_RD;
2843 msgs[1].addr = client->addr;
2844 msgs[1].len = len - GTP_ADDR_LENGTH;
2845 msgs[1].buf = &buf[GTP_ADDR_LENGTH];
2846 //msgs[1].scl_rate = 300 * 1000;
2850 ret = i2c_transfer(client->adapter, msgs, 2);
2856 GTP_ERROR("I2C Read: 0x%04X, %d bytes failed, errcode: %d!", (((u16)(buf[0] << 8)) | buf[1]), len-2, ret);
2861 s32 gtp_i2c_write_no_rst(struct i2c_client *client,u8 *buf,s32 len)
2869 msg.flags = !I2C_M_RD;
2870 msg.addr = client->addr;
2873 //msg.scl_rate = 300 * 1000; // for Rockchip, etc
2877 ret = i2c_transfer(client->adapter, &msg, 1);
2883 GTP_ERROR("I2C Write: 0x%04X, %d bytes failed, errcode: %d!", (((u16)(buf[0] << 8)) | buf[1]), len-2, ret);
2887 /*******************************************************
2889 switch on & off esd delayed work
2892 on: SWITCH_ON / SWITCH_OFF
2895 *********************************************************/
2896 void gtp_esd_switch(struct i2c_client *client, s32 on)
2898 struct goodix_ts_data *ts;
2900 ts = i2c_get_clientdata(client);
2901 spin_lock(&ts->esd_lock);
2903 if (SWITCH_ON == on) // switch on esd
2905 if (!ts->esd_running)
2907 ts->esd_running = 1;
2908 spin_unlock(&ts->esd_lock);
2909 GTP_INFO("Esd started");
2910 queue_delayed_work(gtp_esd_check_workqueue, >p_esd_check_work, ts->clk_tick_cnt);
2914 spin_unlock(&ts->esd_lock);
2917 else // switch off esd
2919 if (ts->esd_running)
2921 ts->esd_running = 0;
2922 spin_unlock(&ts->esd_lock);
2923 GTP_INFO("Esd cancelled");
2924 cancel_delayed_work_sync(>p_esd_check_work);
2928 spin_unlock(&ts->esd_lock);
2933 /*******************************************************
2935 Initialize external watchdog for esd protect
2939 result of i2c write operation.
2940 1: succeed, otherwise: failed
2941 *********************************************************/
2942 static s32 gtp_init_ext_watchdog(struct i2c_client *client)
2944 u8 opr_buffer[3] = {0x80, 0x41, 0xAA};
2945 GTP_DEBUG("[Esd]Init external watchdog");
2946 return gtp_i2c_write_no_rst(client, opr_buffer, 3);
2949 /*******************************************************
2951 Esd protect function.
2952 External watchdog added by meta, 2013/03/07
2957 *******************************************************/
2958 static void gtp_esd_check_func(struct work_struct *work)
2962 struct goodix_ts_data *ts = NULL;
2963 u8 esd_buf[5] = {0x80, 0x40};
2967 ts = i2c_get_clientdata(i2c_connect_client);
2969 if (ts->gtp_is_suspend)
2971 GTP_INFO("Esd suspended!");
2975 for (i = 0; i < 3; i++)
2977 ret = gtp_i2c_read_no_rst(ts->client, esd_buf, 4);
2979 GTP_DEBUG("[Esd]0x8040 = 0x%02X, 0x8041 = 0x%02X", esd_buf[2], esd_buf[3]);
2982 // IIC communication problem
2987 if ((esd_buf[2] == 0xAA) || (esd_buf[3] != 0xAA))
2989 // IC works abnormally..
2990 u8 chk_buf[4] = {0x80, 0x40};
2992 gtp_i2c_read_no_rst(ts->client, chk_buf, 4);
2994 GTP_DEBUG("[Check]0x8040 = 0x%02X, 0x8041 = 0x%02X", chk_buf[2], chk_buf[3]);
2996 if ((chk_buf[2] == 0xAA) || (chk_buf[3] != 0xAA))
3008 // IC works normally, Write 0x8040 0xAA, feed the dog
3010 gtp_i2c_write_no_rst(ts->client, esd_buf, 3);
3017 #if GTP_COMPATIBLE_MODE
3018 if (CHIP_TYPE_GT9F == ts->chip_type)
3020 if (ts->rqst_processing)
3022 GTP_INFO("Request processing, no esd recovery");
3026 GTP_ERROR("IC working abnormally! Process esd recovery.");
3032 gtp_i2c_write_no_rst(ts->client, esd_buf, 5);
3034 gtp_esd_recovery(ts->client);
3040 GTP_ERROR("IC working abnormally! Process reset guitar.");
3046 gtp_i2c_write_no_rst(ts->client, esd_buf, 5);
3048 gtp_reset_guitar(ts->client, 50);
3050 gtp_send_cfg(ts->client);
3054 if(!ts->gtp_is_suspend)
3056 queue_delayed_work(gtp_esd_check_workqueue, >p_esd_check_work, ts->clk_tick_cnt);
3060 GTP_INFO("Esd suspended!");
3066 static const struct i2c_device_id goodix_ts_id[] = {
3067 { GTP_I2C_NAME, 0 },
3071 static struct of_device_id goodix_ts_dt_ids[] = {
3072 { .compatible = "goodix,gt9xx" },
3076 static struct i2c_driver goodix_ts_driver = {
3077 .probe = goodix_ts_probe,
3078 .remove = goodix_ts_remove,
3079 .id_table = goodix_ts_id,
3081 .name = GTP_I2C_NAME,
3082 .owner = THIS_MODULE,
3083 .of_match_table = of_match_ptr(goodix_ts_dt_ids),
3087 /*******************************************************
3089 Driver Install function.
3093 Executive Outcomes. 0---succeed.
3094 ********************************************************/
3095 static int goodix_ts_init(void)
3100 GTP_INFO("GTP driver installing...");
3101 goodix_wq = create_singlethread_workqueue("goodix_wq");
3104 GTP_ERROR("Creat workqueue failed.");
3108 INIT_DELAYED_WORK(>p_esd_check_work, gtp_esd_check_func);
3109 gtp_esd_check_workqueue = create_workqueue("gtp_esd_check");
3111 ret = i2c_add_driver(&goodix_ts_driver);
3115 /*******************************************************
3117 Driver uninstall function.
3121 Executive Outcomes. 0---succeed.
3122 ********************************************************/
3123 static void goodix_ts_exit(void)
3126 GTP_INFO("GTP driver exited.");
3127 i2c_del_driver(&goodix_ts_driver);
3130 destroy_workqueue(goodix_wq);
3133 //late_initcall(goodix_ts_init);
3134 module_init(goodix_ts_init);
3136 module_exit(goodix_ts_exit);
3138 MODULE_DESCRIPTION("GTP Series Driver");
3139 MODULE_LICENSE("GPL");