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.
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 <asm/uaccess.h>
35 #include "tpd_custom_gt9xx.h"
37 #include <linux/device.h>
38 #include <linux/proc_fs.h> /*proc*/
43 u8 wr
; //write read flag£¬0:R 1:W 2:PID 3:
44 u8 flag
; //0:no need flag/int 1: need flag 2:need int
45 u8 flag_addr
[2]; //flag address
46 u8 flag_val
; //flag val
47 u8 flag_relation
; //flag_val:flag 0:not equal 1:equal 2:> 3:<
48 u16 circle
; //polling cycle
49 u8 times
; //plling times
50 u8 retry
; //I2C retry times
51 u16 delay
; //delay befor read or after write
52 u16 data_len
; //data length
53 u8 addr_len
; //address length
56 u8
*data
; //data pointer
61 #define UPDATE_FUNCTIONS
62 #define DATA_LENGTH_UINT 512
63 #define CMD_HEAD_LENGTH (sizeof(st_cmd_head) - sizeof(u8*))
64 #define GOODIX_ENTRY_NAME "goodix_tool"
65 static char procname
[20] = {0};
66 extern struct i2c_client
*i2c_client_point
;
67 static struct i2c_client
*gt_client
= NULL
;
69 #ifdef UPDATE_FUNCTIONS
70 extern s32
gup_enter_update_mode(struct i2c_client
*client
);
71 extern void gup_leave_update_mode(void);
72 extern s32
gup_update_proc(void *dir
);
76 static struct proc_dir_entry
*goodix_proc_entry
;
79 static s32
goodix_tool_write(struct file
*filp
, const char __user
*buff
, unsigned long len
, void *data
);
80 static s32
goodix_tool_read(char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
);
81 static s32(*tool_i2c_read
)(u8
*, u16
);
82 static s32(*tool_i2c_write
)(u8
*, u16
);
85 extern void gtp_esd_switch(struct i2c_client
*client
, s32 on
);
88 s8 IC_TYPE
[16] = "GT9XX";
90 static void tool_set_proc_name(char * procname
)
92 char *months
[12] = {"Jan", "Feb", "Mar", "Apr", "May",
93 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
96 int i
= 0, n_month
= 1, n_day
= 0, n_year
= 0;
98 sprintf(date
, "%s", __DATE__
);
100 //GTP_DEBUG("compile date: %s", date);
102 sscanf(date
, "%s %d %d", month
, &n_day
, &n_year
);
104 for (i
= 0; i
< 12; ++i
)
106 if (!memcmp(months
[i
], month
, 3))
113 sprintf(procname
, "gmnode%04d%02d%02d", n_year
, n_month
, n_day
);
115 //GTP_DEBUG("procname = %s", procname);
117 static ssize_t
goodix_tool_upper_read(struct file
*file
, char __user
*buffer
,
118 size_t count
, loff_t
*ppos
)
120 return goodix_tool_read(buffer
, NULL
,0, count
, NULL
, ppos
);
123 static ssize_t
goodix_tool_upper_write(struct file
*file
, const char __user
*buffer
,
124 size_t count
, loff_t
*ppos
)
126 return goodix_tool_write(file
, buffer
, count
, ppos
);
129 static const struct file_operations gt_tool_fops
= {
130 .write
= goodix_tool_upper_write
,
131 .read
= goodix_tool_upper_read
134 static s32
tool_i2c_read_no_extra(u8
*buf
, u16 len
)
138 ret
= gtp_i2c_read(gt_client
, buf
, len
+ GTP_ADDR_LENGTH
);
142 static s32
tool_i2c_write_no_extra(u8
*buf
, u16 len
)
146 ret
= gtp_i2c_write(gt_client
, buf
, len
);
150 static s32
tool_i2c_read_with_extra(u8
*buf
, u16 len
)
153 u8 pre
[2] = {0x0f, 0xff};
154 u8 end
[2] = {0x80, 0x00};
156 tool_i2c_write_no_extra(pre
, 2);
157 ret
= tool_i2c_read_no_extra(buf
, len
);
158 tool_i2c_write_no_extra(end
, 2);
163 static s32
tool_i2c_write_with_extra(u8
*buf
, u16 len
)
166 u8 pre
[2] = {0x0f, 0xff};
167 u8 end
[2] = {0x80, 0x00};
169 tool_i2c_write_no_extra(pre
, 2);
170 ret
= tool_i2c_write_no_extra(buf
, len
);
171 tool_i2c_write_no_extra(end
, 2);
176 static void register_i2c_func(void)
178 // if (!strncmp(IC_TYPE, "GT818", 5) || !strncmp(IC_TYPE, "GT816", 5)
179 // || !strncmp(IC_TYPE, "GT811", 5) || !strncmp(IC_TYPE, "GT818F", 6)
180 // || !strncmp(IC_TYPE, "GT827", 5) || !strncmp(IC_TYPE,"GT828", 5)
181 // || !strncmp(IC_TYPE, "GT813", 5))
182 if (strncmp(IC_TYPE
, "GT8110", 6) && strncmp(IC_TYPE
, "GT8105", 6)
183 && strncmp(IC_TYPE
, "GT801", 5) && strncmp(IC_TYPE
, "GT800", 5)
184 && strncmp(IC_TYPE
, "GT801PLUS", 9) && strncmp(IC_TYPE
, "GT811", 5)
185 && strncmp(IC_TYPE
, "GTxxx", 5) && strncmp(IC_TYPE
, "GT9XX", 5))
187 tool_i2c_read
= tool_i2c_read_with_extra
;
188 tool_i2c_write
= tool_i2c_write_with_extra
;
189 GTP_DEBUG("I2C function: with pre and end cmd!");
193 tool_i2c_read
= tool_i2c_read_no_extra
;
194 tool_i2c_write
= tool_i2c_write_no_extra
;
195 GTP_INFO("I2C function: without pre and end cmd!");
199 static void unregister_i2c_func(void)
201 tool_i2c_read
= NULL
;
202 tool_i2c_write
= NULL
;
203 GTP_INFO("I2C function: unregister i2c transfer function!");
207 s32
init_wr_node(struct i2c_client
*client
)
211 gt_client
= i2c_client_point
;
213 memset(&cmd_head
, 0, sizeof(cmd_head
));
214 cmd_head
.data
= NULL
;
218 while ((!cmd_head
.data
) && i
)
220 cmd_head
.data
= kzalloc(i
* DATA_LENGTH_UINT
, GFP_KERNEL
);
222 if (NULL
!= cmd_head
.data
)
232 DATA_LENGTH
= i
* DATA_LENGTH_UINT
+ GTP_ADDR_LENGTH
;
233 GTP_INFO("Applied memory size:%d.", DATA_LENGTH
);
237 GTP_ERROR("Apply for memory failed.");
241 cmd_head
.addr_len
= 2;
246 tool_set_proc_name(procname
);
248 goodix_proc_entry
= create_proc_entry(gtp_tool_entry
, 0664, NULL
);
250 if (goodix_proc_entry
== NULL
)
252 GTP_ERROR("Couldn't create proc entry!");
257 GTP_INFO("Create proc entry success!");
258 goodix_proc_entry
->write_proc
= goodix_tool_write
;
259 goodix_proc_entry
->read_proc
= goodix_tool_read
;
262 if(proc_create(procname
, 0660, NULL
, >_tool_fops
)== NULL
)
264 GTP_ERROR("create_proc_entry %s failed", procname
);
271 void uninit_wr_node(void)
273 kfree(cmd_head
.data
);
274 cmd_head
.data
= NULL
;
275 unregister_i2c_func();
276 remove_proc_entry(procname
, NULL
);
279 static u8
relation(u8 src
, u8 dst
, u8 rlt
)
286 ret
= (src
!= dst
) ? true : false;
290 ret
= (src
== dst
) ? true : false;
291 GTP_DEBUG("equal:src:0x%02x dst:0x%02x ret:%d.", src
, dst
, (s32
)ret
);
295 ret
= (src
> dst
) ? true : false;
299 ret
= (src
< dst
) ? true : false;
303 ret
= (src
& dst
) ? true : false;
307 ret
= (!(src
| dst
)) ? true : false;
318 /*******************************************************
325 ********************************************************/
326 static u8
comfirm(void)
331 // memcpy(&buf[GTP_ADDR_LENGTH - cmd_head.addr_len], &cmd_head.flag_addr, cmd_head.addr_len);
332 // memcpy(buf, &cmd_head.flag_addr, cmd_head.addr_len);//Modified by Scott, 2012-02-17
333 memcpy(buf
, cmd_head
.flag_addr
, cmd_head
.addr_len
);
335 for (i
= 0; i
< cmd_head
.times
; i
++)
337 if (tool_i2c_read(buf
, 1) <= 0)
339 GTP_ERROR("Read flag data failed!");
343 if (true == relation(buf
[GTP_ADDR_LENGTH
], cmd_head
.flag_val
, cmd_head
.flag_relation
))
345 GTP_DEBUG("value at flag addr:0x%02x.", buf
[GTP_ADDR_LENGTH
]);
346 GTP_DEBUG("flag value:0x%02x.", cmd_head
.flag_val
);
350 msleep(cmd_head
.circle
);
353 if (i
>= cmd_head
.times
)
355 GTP_ERROR("Didn't get the flag to continue!");
362 /*******************************************************
364 Goodix tool write function.
366 standard proc write function param.
369 ********************************************************/
370 static s32
goodix_tool_write(struct file
*filp
, const char __user
*buff
, unsigned long len
, void *data
)
374 GTP_DEBUG_ARRAY((u8
*)buff
, len
);
376 ret
= copy_from_user(&cmd_head
, buff
, CMD_HEAD_LENGTH
);
380 GTP_ERROR("copy_from_user failed.");
383 GTP_DEBUG("wr :0x%02x.", cmd_head
.wr
);
384 GTP_DEBUG("flag:0x%02x.", cmd_head
.flag
);
385 GTP_DEBUG("flag addr:0x%02x%02x.", cmd_head
.flag_addr
[0], cmd_head
.flag_addr
[1]);
386 GTP_DEBUG("flag val:0x%02x.", cmd_head
.flag_val
);
387 GTP_DEBUG("flag rel:0x%02x.", cmd_head
.flag_relation
);
388 GTP_DEBUG("circle :%d.", (s32
)cmd_head
.circle
);
389 GTP_DEBUG("times :%d.", (s32
)cmd_head
.times
);
390 GTP_DEBUG("retry :%d.", (s32
)cmd_head
.retry
);
391 GTP_DEBUG("delay :%d.", (s32
)cmd_head
.delay
);
392 GTP_DEBUG("data len:%d.", (s32
)cmd_head
.data_len
);
393 GTP_DEBUG("addr len:%d.", (s32
)cmd_head
.addr_len
);
394 GTP_DEBUG("addr:0x%02x%02x.", cmd_head
.addr
[0], cmd_head
.addr
[1]);
395 GTP_DEBUG("len:%d.", (s32
)len
);
396 GTP_DEBUG("buf[20]:0x%02x.", buff
[CMD_HEAD_LENGTH
]);
398 if (1 == cmd_head
.wr
)
400 // copy_from_user(&cmd_head.data[cmd_head.addr_len], &buff[CMD_HEAD_LENGTH], cmd_head.data_len);
401 ret
= copy_from_user(&cmd_head
.data
[GTP_ADDR_LENGTH
], &buff
[CMD_HEAD_LENGTH
], cmd_head
.data_len
);
405 GTP_ERROR("copy_from_user failed.");
408 memcpy(&cmd_head
.data
[GTP_ADDR_LENGTH
- cmd_head
.addr_len
], cmd_head
.addr
, cmd_head
.addr_len
);
410 GTP_DEBUG_ARRAY(cmd_head
.data
, cmd_head
.data_len
+ cmd_head
.addr_len
);
411 GTP_DEBUG_ARRAY((u8
*)&buff
[CMD_HEAD_LENGTH
], cmd_head
.data_len
);
413 if (1 == cmd_head
.flag
)
415 if (FAIL
== comfirm())
417 GTP_ERROR("[WRITE]Comfirm fail!");
421 else if (2 == cmd_head
.flag
)
426 if (tool_i2c_write(&cmd_head
.data
[GTP_ADDR_LENGTH
- cmd_head
.addr_len
],
427 cmd_head
.data_len
+ cmd_head
.addr_len
) <= 0)
429 GTP_ERROR("[WRITE]Write data failed!");
433 GTP_DEBUG_ARRAY(&cmd_head
.data
[GTP_ADDR_LENGTH
- cmd_head
.addr_len
], cmd_head
.data_len
+ cmd_head
.addr_len
);
437 msleep(cmd_head
.delay
);
440 return cmd_head
.data_len
+ CMD_HEAD_LENGTH
;
442 else if (3 == cmd_head
.wr
) //Write ic type
444 memcpy(IC_TYPE
, cmd_head
.data
, cmd_head
.data_len
);
447 return cmd_head
.data_len
+ CMD_HEAD_LENGTH
;
449 else if (5 == cmd_head
.wr
)
451 //memcpy(IC_TYPE, cmd_head.data, cmd_head.data_len);
453 return cmd_head
.data_len
+ CMD_HEAD_LENGTH
;
455 else if (7 == cmd_head
.wr
)//disable irq!
457 mt_eint_mask(CUST_EINT_TOUCH_PANEL_NUM
);
459 gtp_esd_switch(i2c_client_point
, SWITCH_OFF
);
461 return CMD_HEAD_LENGTH
;
463 else if (9 == cmd_head
.wr
) //enable irq!
465 mt_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM
);
467 gtp_esd_switch(i2c_client_point
, SWITCH_ON
);
469 return CMD_HEAD_LENGTH
;
471 else if (17 == cmd_head
.wr
)
473 //struct goodix_ts_data *ts = i2c_get_clientdata(gt_client);
474 ret
= copy_from_user(&cmd_head
.data
[GTP_ADDR_LENGTH
], &buff
[CMD_HEAD_LENGTH
], cmd_head
.data_len
);
478 GTP_DEBUG("copy_from_user failed.");
481 if (cmd_head
.data
[GTP_ADDR_LENGTH
])
483 GTP_DEBUG("gtp enter rawdiff.");
484 gtp_rawdiff_mode
= true;
488 gtp_rawdiff_mode
= false;
489 GTP_DEBUG("gtp leave rawdiff.");
492 return CMD_HEAD_LENGTH
;
495 #ifdef UPDATE_FUNCTIONS
496 else if (11 == cmd_head
.wr
)//Enter update mode!
498 if (FAIL
== gup_enter_update_mode(gt_client
))
503 else if (13 == cmd_head
.wr
)//Leave update mode!
505 gup_leave_update_mode();
507 else if (15 == cmd_head
.wr
) //Update firmware!
511 if ((cmd_head
.data
== NULL
)
512 || (cmd_head
.data_len
>= DATA_LENGTH
)
513 || (cmd_head
.data_len
>= (len
- CMD_HEAD_LENGTH
))) {
514 GTP_ERROR("copy_from_user data out of range.");
518 memset(cmd_head
.data
, 0, cmd_head
.data_len
+ 1);
519 memcpy(cmd_head
.data
, &buff
[CMD_HEAD_LENGTH
], cmd_head
.data_len
);
520 GTP_DEBUG("update firmware, filename: %s", cmd_head
.data
);
521 if (FAIL
== gup_update_proc((void *)cmd_head
.data
))
529 return CMD_HEAD_LENGTH
;
532 /*******************************************************
534 Goodix tool read function.
536 standard proc read function param.
539 ********************************************************/
540 static s32
goodix_tool_read(char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
)
548 else if (!cmd_head
.wr
)
554 if (1 == cmd_head
.flag
)
556 if (FAIL
== comfirm())
558 GTP_ERROR("[READ]Comfirm fail!");
562 else if (2 == cmd_head
.flag
)
567 memcpy(cmd_head
.data
, cmd_head
.addr
, cmd_head
.addr_len
);
569 GTP_DEBUG("[CMD HEAD DATA] ADDR:0x%02x%02x.", cmd_head
.data
[0], cmd_head
.data
[1]);
570 GTP_DEBUG("[CMD HEAD ADDR] ADDR:0x%02x%02x.", cmd_head
.addr
[0], cmd_head
.addr
[1]);
574 msleep(cmd_head
.delay
);
577 data_len
= cmd_head
.data_len
;
581 if (data_len
> DATA_LENGTH
)
590 data_len
-= DATA_LENGTH
;
592 if (tool_i2c_read(cmd_head
.data
, len
) <= 0)
594 GTP_ERROR("[READ]Read data failed!");
598 memcpy(&page
[loc
], &cmd_head
.data
[GTP_ADDR_LENGTH
], len
);
601 GTP_DEBUG_ARRAY(&cmd_head
.data
[GTP_ADDR_LENGTH
], len
);
602 GTP_DEBUG_ARRAY(page
, len
);
605 else if (2 == cmd_head
.wr
)
607 // memcpy(page, "gt8", cmd_head.data_len);
608 // memcpy(page, "GT818", 5);
611 GTP_DEBUG("Return ic type:%s len:%d.", page
, (s32
)cmd_head
.data_len
);
612 return cmd_head
.data_len
;
613 //return sizeof(IC_TYPE_NAME);
615 else if (4 == cmd_head
.wr
)
617 page
[0] = show_len
>> 8;
618 page
[1] = show_len
& 0xff;
619 page
[2] = total_len
>> 8;
620 page
[3] = total_len
& 0xff;
622 return cmd_head
.data_len
;
624 else if (6 == cmd_head
.wr
)
628 else if (8 == cmd_head
.wr
) //Read driver version
630 // memcpy(page, GTP_DRIVER_VERSION, strlen(GTP_DRIVER_VERSION));
632 tmp_len
= strlen(GTP_DRIVER_VERSION
);
633 memcpy(page
, GTP_DRIVER_VERSION
, tmp_len
);
637 return cmd_head
.data_len
;