1 /* drivers/input/touchscreen/goodix_tool.c
3 * 2010 - 2012 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 * V1.0:2012/05/01,create file.
18 * V1.2:2012/10/17,reset_guitar etc.
19 * V1.4: 2013/06/08, new proc name
23 #include <linux/interrupt.h>
24 #include <cust_eint.h>
25 #include <linux/i2c.h>
26 #include <linux/sched.h>
27 #include <linux/kthread.h>
28 #include <linux/rtpm_prio.h>
29 #include <linux/wait.h>
30 #include <linux/time.h>
31 #include <linux/delay.h>
32 #include "cust_gpio_usage.h"
33 #include <linux/device.h>
34 #include <linux/miscdevice.h>
35 #include <asm/uaccess.h>
36 #include <linux/proc_fs.h> /*proc*/
38 #include "tpd_custom_gt9xx.h"
42 u8 wr
; //write read flag0:R 1:W 2:PID 3:
43 u8 flag
; //0:no need flag/int 1: need flag 2:need int
44 u8 flag_addr
[2]; //flag address
45 u8 flag_val
; //flag val
46 u8 flag_relation
; //flag_val:flag 0:not equal 1:equal 2:> 3:<
47 u16 circle
; //polling cycle
48 u8 times
; //plling times
49 u8 retry
; //I2C retry times
50 u16 delay
; //delay befor read or after write
51 u16 data_len
; //data length
52 u8 addr_len
; //address length
55 u8
*data
; //data pointer
60 #define UPDATE_FUNCTIONS
61 #define DATA_LENGTH_UINT 512
62 #define CMD_HEAD_LENGTH (sizeof(st_cmd_head) - sizeof(u8*))
63 static char procname
[20] = {0};
64 extern struct i2c_client
*i2c_client_point
;
65 static struct i2c_client
*gt_client
= NULL
;
67 #ifdef UPDATE_FUNCTIONS
68 extern s32
gup_enter_update_mode(struct i2c_client
*client
);
69 extern void gup_leave_update_mode(void);
70 extern s32
gup_update_proc(void *dir
);
72 extern s32
gup_load_hotknot_system(void);
73 extern s32
gup_load_fx_system(void);
74 extern s32
gup_recovery_main_system(void);
75 extern s32
gup_load_main_system(char *filepath
);
78 static struct proc_dir_entry
*goodix_proc_entry
;
81 static s32
goodix_tool_write(struct file
*filp
, const char __user
*buff
, unsigned long len
, void *data
);
82 static s32
goodix_tool_read(char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
);
83 static s32(*tool_i2c_read
)(u8
*, u16
);
84 static s32(*tool_i2c_write
)(u8
*, u16
);
87 extern void gtp_esd_switch(struct i2c_client
*client
, s32 on
);
90 #if (GTP_ESD_PROTECT || GTP_COMPATIBLE_MODE)
91 extern u8 is_reseting
;
96 s8 IC_TYPE
[16] = "GT9XX";
99 DECLARE_WAIT_QUEUE_HEAD(bp_waiter
);
100 u8 got_hotknot_state
= 0;
101 u8 got_hotknot_extra_state
= 0;
102 u8 wait_hotknot_state
= 0;
103 u8 force_wake_flag
= 0;
106 #define HOTKNOTNAME "hotknot"
108 static ssize_t
hotknot_read(struct file
*file
, char __user
*buffer
,
109 size_t count
, loff_t
*ppos
)
111 return goodix_tool_read(buffer
, NULL
,0, count
, NULL
, ppos
);
114 static ssize_t
hotknot_write(struct file
*file
, const char __user
*buffer
,
115 size_t count
, loff_t
*ppos
)
117 return goodix_tool_write(file
, buffer
, count
, ppos
);
120 static struct file_operations hotknot_fops
=
122 // .owner = THIS_MODULE,
123 .read
= hotknot_read
,
124 .write
= hotknot_write
,
127 static struct miscdevice hotknot_misc_device
=
129 .minor
= MISC_DYNAMIC_MINOR
,
131 .fops
= &hotknot_fops
,
133 static ssize_t
goodix_tool_upper_read(struct file
*file
, char __user
*buffer
,
134 size_t count
, loff_t
*ppos
)
136 return goodix_tool_read(buffer
, NULL
,0, count
, NULL
, ppos
);
139 static ssize_t
goodix_tool_upper_write(struct file
*file
, const char __user
*buffer
,
140 size_t count
, loff_t
*ppos
)
142 return goodix_tool_write(file
, buffer
, count
, ppos
);
145 static const struct file_operations gt_tool_fops
= {
146 .write
= goodix_tool_upper_write
,
147 .read
= goodix_tool_upper_read
,
150 static void tool_set_proc_name(char * procname
)
153 char *months
[12] = {"Jan", "Feb", "Mar", "Apr", "May",
154 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
157 int i
= 0, n_month
= 1, n_day
= 0, n_year
= 0;
159 sprintf(date
, "%s", __DATE__
);
161 //GTP_DEBUG("compile date: %s", date);
163 sscanf(date
, "%s %d %d", month
, &n_day
, &n_year
);
165 for (i
= 0; i
< 12; ++i
)
167 if (!memcmp(months
[i
], month
, 3))
174 sprintf(procname
, "gmnode%04d%02d%02d", n_year
, n_month
, n_day
);
176 sprintf(procname
, HOTKNOTNAME
);
178 //GTP_DEBUG("procname = %s", procname);
180 static s32
tool_i2c_read_no_extra(u8
*buf
, u16 len
)
184 ret
= gtp_i2c_read(gt_client
, buf
, len
+ GTP_ADDR_LENGTH
);
188 static s32
tool_i2c_write_no_extra(u8
*buf
, u16 len
)
192 ret
= gtp_i2c_write(gt_client
, buf
, len
);
196 static s32
tool_i2c_read_with_extra(u8
*buf
, u16 len
)
199 u8 pre
[2] = {0x0f, 0xff};
200 u8 end
[2] = {0x80, 0x00};
202 tool_i2c_write_no_extra(pre
, 2);
203 ret
= tool_i2c_read_no_extra(buf
, len
);
204 tool_i2c_write_no_extra(end
, 2);
209 static s32
tool_i2c_write_with_extra(u8
*buf
, u16 len
)
212 u8 pre
[2] = {0x0f, 0xff};
213 u8 end
[2] = {0x80, 0x00};
215 tool_i2c_write_no_extra(pre
, 2);
216 ret
= tool_i2c_write_no_extra(buf
, len
);
217 tool_i2c_write_no_extra(end
, 2);
222 static void register_i2c_func(void)
224 // if (!strncmp(IC_TYPE, "GT818", 5) || !strncmp(IC_TYPE, "GT816", 5)
225 // || !strncmp(IC_TYPE, "GT811", 5) || !strncmp(IC_TYPE, "GT818F", 6)
226 // || !strncmp(IC_TYPE, "GT827", 5) || !strncmp(IC_TYPE,"GT828", 5)
227 // || !strncmp(IC_TYPE, "GT813", 5))
228 if (strncmp(IC_TYPE
, "GT8110", 6) && strncmp(IC_TYPE
, "GT8105", 6)
229 && strncmp(IC_TYPE
, "GT801", 5) && strncmp(IC_TYPE
, "GT800", 5)
230 && strncmp(IC_TYPE
, "GT801PLUS", 9) && strncmp(IC_TYPE
, "GT811", 5)
231 && strncmp(IC_TYPE
, "GTxxx", 5) && strncmp(IC_TYPE
, "GT9XX", 5))
233 tool_i2c_read
= tool_i2c_read_with_extra
;
234 tool_i2c_write
= tool_i2c_write_with_extra
;
235 GTP_DEBUG("I2C function: with pre and end cmd!");
239 tool_i2c_read
= tool_i2c_read_no_extra
;
240 tool_i2c_write
= tool_i2c_write_no_extra
;
241 GTP_INFO("I2C function: without pre and end cmd!");
245 static void unregister_i2c_func(void)
247 tool_i2c_read
= NULL
;
248 tool_i2c_write
= NULL
;
249 GTP_INFO("I2C function: unregister i2c transfer function!");
252 s32
init_wr_node(struct i2c_client
*client
)
256 gt_client
= i2c_client_point
;
258 memset(&cmd_head
, 0, sizeof(cmd_head
));
259 cmd_head
.data
= NULL
;
263 while ((!cmd_head
.data
) && i
)
265 cmd_head
.data
= kzalloc(i
* DATA_LENGTH_UINT
, GFP_KERNEL
);
267 if (NULL
!= cmd_head
.data
)
277 DATA_LENGTH
= i
* DATA_LENGTH_UINT
+ GTP_ADDR_LENGTH
;
278 GTP_INFO("Applied memory size:%d.", DATA_LENGTH
);
282 GTP_ERROR("Apply for memory failed.");
286 cmd_head
.addr_len
= 2;
291 tool_set_proc_name(procname
);
293 goodix_proc_entry
= create_proc_entry(procname
, 0660, NULL
);
294 if (goodix_proc_entry
== NULL
)
296 GTP_ERROR("Couldn't create proc entry!");
301 GTP_INFO("Create proc entry success!");
302 goodix_proc_entry
->write_proc
= goodix_tool_write
;
303 goodix_proc_entry
->read_proc
= goodix_tool_read
;
306 if(proc_create(procname
, 0660, NULL
, >_tool_fops
)== NULL
)
308 GTP_ERROR("create_proc_entry %s failed", procname
);
313 #if 1 // setting by hotknot feature
314 if (misc_register(&hotknot_misc_device
))
316 printk("mtk_tpd: hotknot_device register failed\n");
324 void uninit_wr_node(void)
326 kfree(cmd_head
.data
);
327 cmd_head
.data
= NULL
;
328 unregister_i2c_func();
329 remove_proc_entry(procname
, NULL
);
332 static u8
relation(u8 src
, u8 dst
, u8 rlt
)
339 ret
= (src
!= dst
) ? true : false;
343 ret
= (src
== dst
) ? true : false;
344 GTP_DEBUG("equal:src:0x%02x dst:0x%02x ret:%d.", src
, dst
, (s32
)ret
);
348 ret
= (src
> dst
) ? true : false;
352 ret
= (src
< dst
) ? true : false;
356 ret
= (src
& dst
) ? true : false;
360 ret
= (!(src
| dst
)) ? true : false;
371 /*******************************************************
378 ********************************************************/
379 static u8
comfirm(void)
384 // memcpy(&buf[GTP_ADDR_LENGTH - cmd_head.addr_len], &cmd_head.flag_addr, cmd_head.addr_len);
385 // memcpy(buf, &cmd_head.flag_addr, cmd_head.addr_len);//Modified by Scott, 2012-02-17
386 memcpy(buf
, cmd_head
.flag_addr
, cmd_head
.addr_len
);
388 for (i
= 0; i
< cmd_head
.times
; i
++)
390 if (tool_i2c_read(buf
, 1) <= 0)
392 GTP_ERROR("Read flag data failed!");
396 if (true == relation(buf
[GTP_ADDR_LENGTH
], cmd_head
.flag_val
, cmd_head
.flag_relation
))
398 GTP_DEBUG("value at flag addr:0x%02x.", buf
[GTP_ADDR_LENGTH
]);
399 GTP_DEBUG("flag value:0x%02x.", cmd_head
.flag_val
);
403 msleep(cmd_head
.circle
);
406 if (i
>= cmd_head
.times
)
408 GTP_ERROR("Didn't get the flag to continue!");
415 /*******************************************************
417 Goodix tool write function.
419 standard proc write function param.
422 ********************************************************/
423 static s32
goodix_tool_write(struct file
*filp
, const char __user
*buff
, unsigned long len
, void *data
)
427 GTP_DEBUG_ARRAY((u8
*)buff
, len
);
429 if(tpd_halt
== 1||is_reseting
== 1)
431 //GTP_ERROR("[Write]tpd_halt =1 fail!");
435 ret
= copy_from_user(&cmd_head
, buff
, CMD_HEAD_LENGTH
);
439 GTP_ERROR("copy_from_user failed.");
442 GTP_DEBUG("wr :0x%02x.", cmd_head
.wr
);
444 GTP_DEBUG("flag:0x%02x.", cmd_head.flag);
445 GTP_DEBUG("flag addr:0x%02x%02x.", cmd_head.flag_addr[0], cmd_head.flag_addr[1]);
446 GTP_DEBUG("flag val:0x%02x.", cmd_head.flag_val);
447 GTP_DEBUG("flag rel:0x%02x.", cmd_head.flag_relation);
448 GTP_DEBUG("circle :%d.", (s32)cmd_head.circle);
449 GTP_DEBUG("times :%d.", (s32)cmd_head.times);
450 GTP_DEBUG("retry :%d.", (s32)cmd_head.retry);
451 GTP_DEBUG("delay :%d.", (s32)cmd_head.delay);
452 GTP_DEBUG("data len:%d.", (s32)cmd_head.data_len);
453 GTP_DEBUG("addr len:%d.", (s32)cmd_head.addr_len);
454 GTP_DEBUG("addr:0x%02x%02x.", cmd_head.addr[0], cmd_head.addr[1]);
455 GTP_DEBUG("len:%d.", (s32)len);
456 GTP_DEBUG("buf[20]:0x%02x.", buff[CMD_HEAD_LENGTH]);
459 if (1 == cmd_head
.wr
)
461 // copy_from_user(&cmd_head.data[cmd_head.addr_len], &buff[CMD_HEAD_LENGTH], cmd_head.data_len);
462 ret
= copy_from_user(&cmd_head
.data
[GTP_ADDR_LENGTH
], &buff
[CMD_HEAD_LENGTH
], cmd_head
.data_len
);
466 GTP_ERROR("copy_from_user failed.");
469 memcpy(&cmd_head
.data
[GTP_ADDR_LENGTH
- cmd_head
.addr_len
], cmd_head
.addr
, cmd_head
.addr_len
);
471 GTP_DEBUG_ARRAY(cmd_head
.data
, cmd_head
.data_len
+ cmd_head
.addr_len
);
472 GTP_DEBUG_ARRAY((u8
*)&buff
[CMD_HEAD_LENGTH
], cmd_head
.data_len
);
474 if (1 == cmd_head
.flag
)
476 if (FAIL
== comfirm())
478 GTP_ERROR("[WRITE]Comfirm fail!");
482 else if (2 == cmd_head
.flag
)
487 if (tool_i2c_write(&cmd_head
.data
[GTP_ADDR_LENGTH
- cmd_head
.addr_len
],
488 cmd_head
.data_len
+ cmd_head
.addr_len
) <= 0)
490 GTP_ERROR("[WRITE]Write data failed!");
494 GTP_DEBUG_ARRAY(&cmd_head
.data
[GTP_ADDR_LENGTH
- cmd_head
.addr_len
], cmd_head
.data_len
+ cmd_head
.addr_len
);
498 msleep(cmd_head
.delay
);
501 return cmd_head
.data_len
+ CMD_HEAD_LENGTH
;
503 else if (3 == cmd_head
.wr
) //Write ic type
505 memcpy(IC_TYPE
, cmd_head
.data
, cmd_head
.data_len
);
508 return cmd_head
.data_len
+ CMD_HEAD_LENGTH
;
510 else if (5 == cmd_head
.wr
)
512 //memcpy(IC_TYPE, cmd_head.data, cmd_head.data_len);
514 return cmd_head
.data_len
+ CMD_HEAD_LENGTH
;
516 else if (7 == cmd_head
.wr
)//disable irq!
518 mt_eint_mask(CUST_EINT_TOUCH_PANEL_NUM
);
520 gtp_esd_switch(i2c_client_point
, SWITCH_OFF
);
522 return CMD_HEAD_LENGTH
;
524 else if (9 == cmd_head
.wr
) //enable irq!
526 mt_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM
);
528 gtp_esd_switch(i2c_client_point
, SWITCH_ON
);
530 return CMD_HEAD_LENGTH
;
532 else if (17 == cmd_head
.wr
)
534 ret
= copy_from_user(&cmd_head
.data
[GTP_ADDR_LENGTH
], &buff
[CMD_HEAD_LENGTH
], cmd_head
.data_len
);
538 GTP_DEBUG("copy_from_user failed.");
541 if (cmd_head
.data
[GTP_ADDR_LENGTH
])
543 GTP_DEBUG("gtp enter rawdiff.");
544 gtp_rawdiff_mode
= true;
548 gtp_rawdiff_mode
= false;
549 GTP_DEBUG("gtp leave rawdiff.");
552 return CMD_HEAD_LENGTH
;
555 #ifdef UPDATE_FUNCTIONS
556 else if (11 == cmd_head
.wr
) //Enter update mode!
558 if (FAIL
== gup_enter_update_mode(gt_client
))
563 else if (13 == cmd_head
.wr
)//Leave update mode!
565 gup_leave_update_mode();
567 else if (15 == cmd_head
.wr
) //Update firmware!
571 memset(cmd_head
.data
, 0, cmd_head
.data_len
+ 1);
572 ret
= copy_from_user(cmd_head
.data
, &buff
[CMD_HEAD_LENGTH
], cmd_head
.data_len
);
575 GTP_ERROR("copy_from_user failed.");
577 GTP_DEBUG("update firmware, filename: %s", cmd_head
.data
);
578 if (FAIL
== gup_update_proc((void *)cmd_head
.data
))
585 else if (19 == cmd_head
.wr
) //load subsystem
587 ret
= copy_from_user(&cmd_head
.data
[0], &buff
[CMD_HEAD_LENGTH
], cmd_head
.data_len
);
588 if(0 == cmd_head
.data
[0])
590 if (FAIL
== gup_load_hotknot_system())
595 else if(1 == cmd_head
.data
[0])
597 if (FAIL
== gup_load_fx_system())
602 else if(2 == cmd_head
.data
[0])
604 if (FAIL
== gup_recovery_main_system())
609 else if(3 == cmd_head
.data
[0])
611 if (FAIL
== gup_load_main_system(NULL
))
618 else if (21 == cmd_head
.wr
)
620 u16 wait_hotknot_timeout
= 0;
621 u8 rqst_hotknot_state
;
623 ret
= copy_from_user(&cmd_head
.data
[GTP_ADDR_LENGTH
],
624 &buff
[CMD_HEAD_LENGTH
], cmd_head
.data_len
);
628 GTP_ERROR("copy_from_user failed.");
631 rqst_hotknot_state
= cmd_head
.data
[GTP_ADDR_LENGTH
];
632 wait_hotknot_state
|= rqst_hotknot_state
;
633 wait_hotknot_timeout
= (cmd_head
.data
[GTP_ADDR_LENGTH
+ 1]<<8) +
634 cmd_head
.data
[GTP_ADDR_LENGTH
+ 2];
635 GTP_DEBUG("Goodix tool received wait polling state:0x%x,timeout:%d, all wait state:0x%x",
636 rqst_hotknot_state
, wait_hotknot_timeout
, wait_hotknot_state
);
637 got_hotknot_state
&= (~rqst_hotknot_state
);
638 //got_hotknot_extra_state = 0;
639 switch(rqst_hotknot_state
)
641 set_current_state(TASK_INTERRUPTIBLE
);
642 case HN_DEVICE_PAIRED
:
643 hotknot_paired_flag
= 0;
644 wait_event_interruptible(bp_waiter
, force_wake_flag
||
645 rqst_hotknot_state
== (got_hotknot_state
&rqst_hotknot_state
));
646 wait_hotknot_state
&= (~rqst_hotknot_state
);
647 if(rqst_hotknot_state
!= (got_hotknot_state
&rqst_hotknot_state
))
649 GTP_ERROR("Wait 0x%x block polling waiter failed.", rqst_hotknot_state
);
655 case HN_SLAVE_RECEIVED
:
656 wait_event_interruptible_timeout(bp_waiter
, force_wake_flag
||
657 rqst_hotknot_state
== (got_hotknot_state
&rqst_hotknot_state
),
658 wait_hotknot_timeout
);
659 wait_hotknot_state
&= (~rqst_hotknot_state
);
660 if(rqst_hotknot_state
== (got_hotknot_state
&rqst_hotknot_state
))
662 return got_hotknot_extra_state
;
666 GTP_ERROR("Wait 0x%x block polling waiter timeout.", rqst_hotknot_state
);
671 case HN_MASTER_DEPARTED
:
672 case HN_SLAVE_DEPARTED
:
673 wait_event_interruptible_timeout(bp_waiter
, force_wake_flag
||
674 rqst_hotknot_state
== (got_hotknot_state
&rqst_hotknot_state
),
675 wait_hotknot_timeout
);
676 wait_hotknot_state
&= (~rqst_hotknot_state
);
677 if(rqst_hotknot_state
!= (got_hotknot_state
&rqst_hotknot_state
))
679 GTP_ERROR("Wait 0x%x block polling waitor timeout.", rqst_hotknot_state
);
685 GTP_ERROR("Invalid rqst_hotknot_state in goodix_tool.");
690 else if(23 == cmd_head
.wr
)
692 GTP_DEBUG("Manual wakeup all block polling waiter!");
693 got_hotknot_state
= 0;
694 wait_hotknot_state
= 0;
696 hotknot_paired_flag
= 0;
697 wake_up_interruptible(&bp_waiter
);
700 return CMD_HEAD_LENGTH
;
703 /*******************************************************
705 Goodix tool read function.
707 standard proc read function param.
710 ********************************************************/
711 static s32
goodix_tool_read(char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
)
717 if(tpd_halt
== 1||is_reseting
== 1)
719 //GTP_ERROR("[READ]tpd_halt =1 fail!");
724 GTP_ERROR("[READ] invaild operator fail!");
727 else if (!cmd_head
.wr
)
733 if (1 == cmd_head
.flag
)
735 if (FAIL
== comfirm())
737 GTP_ERROR("[READ]Comfirm fail!");
741 else if (2 == cmd_head
.flag
)
746 memcpy(cmd_head
.data
, cmd_head
.addr
, cmd_head
.addr_len
);
748 GTP_DEBUG("[CMD HEAD DATA] ADDR:0x%02x%02x.", cmd_head
.data
[0], cmd_head
.data
[1]);
749 GTP_DEBUG("[CMD HEAD ADDR] ADDR:0x%02x%02x.", cmd_head
.addr
[0], cmd_head
.addr
[1]);
753 msleep(cmd_head
.delay
);
756 data_len
= cmd_head
.data_len
;
760 if (data_len
> DATA_LENGTH
)
769 data_len
-= DATA_LENGTH
;
771 if (tool_i2c_read(cmd_head
.data
, len
) <= 0)
773 GTP_ERROR("[READ]Read data failed!");
777 ret
= copy_to_user(&page
[loc
], &cmd_head
.data
[GTP_ADDR_LENGTH
], len
);
780 GTP_ERROR("copy_from_user failed.");
785 GTP_DEBUG_ARRAY(&cmd_head
.data
[GTP_ADDR_LENGTH
], len
);
786 GTP_DEBUG_ARRAY(page
, len
);
789 else if (2 == cmd_head
.wr
)
791 // memcpy(page, "gt8", cmd_head.data_len);
792 // memcpy(page, "GT818", 5);
795 GTP_DEBUG("Return ic type:%s len:%d.", page
, (s32
)cmd_head
.data_len
);
796 return cmd_head
.data_len
;
797 //return sizeof(IC_TYPE_NAME);
799 else if (4 == cmd_head
.wr
)
801 page
[0] = show_len
>> 8;
802 page
[1] = show_len
& 0xff;
803 page
[2] = total_len
>> 8;
804 page
[3] = total_len
& 0xff;
806 return cmd_head
.data_len
;
808 else if (6 == cmd_head
.wr
)
812 else if (8 == cmd_head
.wr
) //Read driver version
814 // memcpy(page, GTP_DRIVER_VERSION, strlen(GTP_DRIVER_VERSION));
816 tmp_len
= strlen(GTP_DRIVER_VERSION
);
817 ret
= copy_to_user(page
, GTP_DRIVER_VERSION
, tmp_len
);
820 GTP_ERROR("copy_from_user failed.");
826 return cmd_head
.data_len
;