import PULS_20180308
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / input / touchscreen / mediatek / GT9110 / goodix_tool.c
1 /* drivers/input/touchscreen/goodix_tool.c
2 *
3 * 2010 - 2012 Goodix Technology.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be a reference
11 * to you, when you are integrating the GOODiX's CTP IC into your system,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * Version:1.2
17 * V1.0:2012/05/01,create file.
18 * V1.2:2012/10/17,reset_guitar etc.
19 *
20 */
21
22 #include "tpd.h"
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>
34
35 #include "tpd_custom_gt9xx.h"
36
37 #include <linux/device.h>
38 #include <linux/proc_fs.h> /*proc*/
39
40 #pragma pack(1)
41 typedef struct
42 {
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
54 u8 addr[2]; //address
55 u8 res[3]; //reserved
56 u8 *data; //data pointer
57 } st_cmd_head;
58 #pragma pack()
59 st_cmd_head cmd_head;
60
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;
68
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);
73 #endif
74
75 #if 0
76 static struct proc_dir_entry *goodix_proc_entry;
77 #endif
78
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);
83
84 #if GTP_ESD_PROTECT
85 extern void gtp_esd_switch(struct i2c_client *client, s32 on);
86 #endif
87 s32 DATA_LENGTH = 0;
88 s8 IC_TYPE[16] = "GT9XX";
89
90 static void tool_set_proc_name(char * procname)
91 {
92 char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May",
93 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
94 char date[20] = {0};
95 char month[4] = {0};
96 int i = 0, n_month = 1, n_day = 0, n_year = 0;
97
98 sprintf(date, "%s", __DATE__);
99
100 //GTP_DEBUG("compile date: %s", date);
101
102 sscanf(date, "%s %d %d", month, &n_day, &n_year);
103
104 for (i = 0; i < 12; ++i)
105 {
106 if (!memcmp(months[i], month, 3))
107 {
108 n_month = i+1;
109 break;
110 }
111 }
112
113 sprintf(procname, "gmnode%04d%02d%02d", n_year, n_month, n_day);
114
115 //GTP_DEBUG("procname = %s", procname);
116 }
117 static ssize_t goodix_tool_upper_read(struct file *file, char __user *buffer,
118 size_t count, loff_t *ppos)
119 {
120 return goodix_tool_read(buffer, NULL,0, count, NULL, ppos);
121 }
122
123 static ssize_t goodix_tool_upper_write(struct file *file, const char __user *buffer,
124 size_t count, loff_t *ppos)
125 {
126 return goodix_tool_write(file, buffer, count, ppos);
127 }
128
129 static const struct file_operations gt_tool_fops = {
130 .write = goodix_tool_upper_write,
131 .read = goodix_tool_upper_read
132 };
133
134 static s32 tool_i2c_read_no_extra(u8 *buf, u16 len)
135 {
136 s32 ret = -1;
137
138 ret = gtp_i2c_read(gt_client, buf, len + GTP_ADDR_LENGTH);
139 return ret;
140 }
141
142 static s32 tool_i2c_write_no_extra(u8 *buf, u16 len)
143 {
144 s32 ret = -1;
145
146 ret = gtp_i2c_write(gt_client, buf, len);
147 return ret;
148 }
149
150 static s32 tool_i2c_read_with_extra(u8 *buf, u16 len)
151 {
152 s32 ret = -1;
153 u8 pre[2] = {0x0f, 0xff};
154 u8 end[2] = {0x80, 0x00};
155
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);
159
160 return ret;
161 }
162
163 static s32 tool_i2c_write_with_extra(u8 *buf, u16 len)
164 {
165 s32 ret = -1;
166 u8 pre[2] = {0x0f, 0xff};
167 u8 end[2] = {0x80, 0x00};
168
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);
172
173 return ret;
174 }
175
176 static void register_i2c_func(void)
177 {
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))
186 {
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!");
190 }
191 else
192 {
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!");
196 }
197 }
198
199 static void unregister_i2c_func(void)
200 {
201 tool_i2c_read = NULL;
202 tool_i2c_write = NULL;
203 GTP_INFO("I2C function: unregister i2c transfer function!");
204 }
205
206
207 s32 init_wr_node(struct i2c_client *client)
208 {
209 s32 i;
210
211 gt_client = i2c_client_point;
212
213 memset(&cmd_head, 0, sizeof(cmd_head));
214 cmd_head.data = NULL;
215
216 i = 5;
217
218 while ((!cmd_head.data) && i)
219 {
220 cmd_head.data = kzalloc(i * DATA_LENGTH_UINT, GFP_KERNEL);
221
222 if (NULL != cmd_head.data)
223 {
224 break;
225 }
226
227 i--;
228 }
229
230 if (i)
231 {
232 DATA_LENGTH = i * DATA_LENGTH_UINT + GTP_ADDR_LENGTH;
233 GTP_INFO("Applied memory size:%d.", DATA_LENGTH);
234 }
235 else
236 {
237 GTP_ERROR("Apply for memory failed.");
238 return FAIL;
239 }
240
241 cmd_head.addr_len = 2;
242 cmd_head.retry = 5;
243
244 register_i2c_func();
245
246 tool_set_proc_name(procname);
247 #if 0 // fix 3.10
248 goodix_proc_entry = create_proc_entry(gtp_tool_entry, 0664, NULL);
249
250 if (goodix_proc_entry == NULL)
251 {
252 GTP_ERROR("Couldn't create proc entry!");
253 return FAIL;
254 }
255 else
256 {
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;
260 }
261 #else
262 if(proc_create(procname, 0660, NULL, &gt_tool_fops)== NULL)
263 {
264 GTP_ERROR("create_proc_entry %s failed", procname);
265 return -1;
266 }
267 #endif
268 return SUCCESS;
269 }
270
271 void uninit_wr_node(void)
272 {
273 kfree(cmd_head.data);
274 cmd_head.data = NULL;
275 unregister_i2c_func();
276 remove_proc_entry(procname, NULL);
277 }
278
279 static u8 relation(u8 src, u8 dst, u8 rlt)
280 {
281 u8 ret = 0;
282
283 switch (rlt)
284 {
285 case 0:
286 ret = (src != dst) ? true : false;
287 break;
288
289 case 1:
290 ret = (src == dst) ? true : false;
291 GTP_DEBUG("equal:src:0x%02x dst:0x%02x ret:%d.", src, dst, (s32)ret);
292 break;
293
294 case 2:
295 ret = (src > dst) ? true : false;
296 break;
297
298 case 3:
299 ret = (src < dst) ? true : false;
300 break;
301
302 case 4:
303 ret = (src & dst) ? true : false;
304 break;
305
306 case 5:
307 ret = (!(src | dst)) ? true : false;
308 break;
309
310 default:
311 ret = false;
312 break;
313 }
314
315 return ret;
316 }
317
318 /*******************************************************
319 Function:
320 Comfirm function.
321 Input:
322 None.
323 Output:
324 Return write length.
325 ********************************************************/
326 static u8 comfirm(void)
327 {
328 s32 i = 0;
329 u8 buf[32];
330
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);
334
335 for (i = 0; i < cmd_head.times; i++)
336 {
337 if (tool_i2c_read(buf, 1) <= 0)
338 {
339 GTP_ERROR("Read flag data failed!");
340 return FAIL;
341 }
342
343 if (true == relation(buf[GTP_ADDR_LENGTH], cmd_head.flag_val, cmd_head.flag_relation))
344 {
345 GTP_DEBUG("value at flag addr:0x%02x.", buf[GTP_ADDR_LENGTH]);
346 GTP_DEBUG("flag value:0x%02x.", cmd_head.flag_val);
347 break;
348 }
349
350 msleep(cmd_head.circle);
351 }
352
353 if (i >= cmd_head.times)
354 {
355 GTP_ERROR("Didn't get the flag to continue!");
356 return FAIL;
357 }
358
359 return SUCCESS;
360 }
361
362 /*******************************************************
363 Function:
364 Goodix tool write function.
365 Input:
366 standard proc write function param.
367 Output:
368 Return write length.
369 ********************************************************/
370 static s32 goodix_tool_write(struct file *filp, const char __user *buff, unsigned long len, void *data)
371 {
372 u64 ret = 0;
373 GTP_DEBUG_FUNC();
374 GTP_DEBUG_ARRAY((u8 *)buff, len);
375
376 ret = copy_from_user(&cmd_head, buff, CMD_HEAD_LENGTH);
377
378 if (ret)
379 {
380 GTP_ERROR("copy_from_user failed.");
381 }
382
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]);
397
398 if (1 == cmd_head.wr)
399 {
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);
402
403 if (ret)
404 {
405 GTP_ERROR("copy_from_user failed.");
406 }
407
408 memcpy(&cmd_head.data[GTP_ADDR_LENGTH - cmd_head.addr_len], cmd_head.addr, cmd_head.addr_len);
409
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);
412
413 if (1 == cmd_head.flag)
414 {
415 if (FAIL == comfirm())
416 {
417 GTP_ERROR("[WRITE]Comfirm fail!");
418 return FAIL;
419 }
420 }
421 else if (2 == cmd_head.flag)
422 {
423 //Need interrupt!
424 }
425
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)
428 {
429 GTP_ERROR("[WRITE]Write data failed!");
430 return FAIL;
431 }
432
433 GTP_DEBUG_ARRAY(&cmd_head.data[GTP_ADDR_LENGTH - cmd_head.addr_len], cmd_head.data_len + cmd_head.addr_len);
434
435 if (cmd_head.delay)
436 {
437 msleep(cmd_head.delay);
438 }
439
440 return cmd_head.data_len + CMD_HEAD_LENGTH;
441 }
442 else if (3 == cmd_head.wr) //Write ic type
443 {
444 memcpy(IC_TYPE, cmd_head.data, cmd_head.data_len);
445 register_i2c_func();
446
447 return cmd_head.data_len + CMD_HEAD_LENGTH;
448 }
449 else if (5 == cmd_head.wr)
450 {
451 //memcpy(IC_TYPE, cmd_head.data, cmd_head.data_len);
452
453 return cmd_head.data_len + CMD_HEAD_LENGTH;
454 }
455 else if (7 == cmd_head.wr)//disable irq!
456 {
457 mt_eint_mask(CUST_EINT_TOUCH_PANEL_NUM);
458 #if GTP_ESD_PROTECT
459 gtp_esd_switch(i2c_client_point, SWITCH_OFF);
460 #endif
461 return CMD_HEAD_LENGTH;
462 }
463 else if (9 == cmd_head.wr) //enable irq!
464 {
465 mt_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM);
466 #if GTP_ESD_PROTECT
467 gtp_esd_switch(i2c_client_point, SWITCH_ON);
468 #endif
469 return CMD_HEAD_LENGTH;
470 }
471 else if (17 == cmd_head.wr)
472 {
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);
475
476 if (ret)
477 {
478 GTP_DEBUG("copy_from_user failed.");
479 }
480
481 if (cmd_head.data[GTP_ADDR_LENGTH])
482 {
483 GTP_DEBUG("gtp enter rawdiff.");
484 gtp_rawdiff_mode = true;
485 }
486 else
487 {
488 gtp_rawdiff_mode = false;
489 GTP_DEBUG("gtp leave rawdiff.");
490 }
491
492 return CMD_HEAD_LENGTH;
493 }
494
495 #ifdef UPDATE_FUNCTIONS
496 else if (11 == cmd_head.wr)//Enter update mode!
497 {
498 if (FAIL == gup_enter_update_mode(gt_client))
499 {
500 return FAIL;
501 }
502 }
503 else if (13 == cmd_head.wr)//Leave update mode!
504 {
505 gup_leave_update_mode();
506 }
507 else if (15 == cmd_head.wr) //Update firmware!
508 {
509 show_len = 0;
510 total_len = 0;
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.");
515 return -EINVAL;
516 }
517
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))
522 {
523 return FAIL;
524 }
525 }
526
527 #endif
528
529 return CMD_HEAD_LENGTH;
530 }
531
532 /*******************************************************
533 Function:
534 Goodix tool read function.
535 Input:
536 standard proc read function param.
537 Output:
538 Return read length.
539 ********************************************************/
540 static s32 goodix_tool_read(char *page, char **start, off_t off, int count, int *eof, void *data)
541 {
542 GTP_DEBUG_FUNC();
543
544 if (cmd_head.wr % 2)
545 {
546 return FAIL;
547 }
548 else if (!cmd_head.wr)
549 {
550 u16 len = 0;
551 s16 data_len = 0;
552 u16 loc = 0;
553
554 if (1 == cmd_head.flag)
555 {
556 if (FAIL == comfirm())
557 {
558 GTP_ERROR("[READ]Comfirm fail!");
559 return FAIL;
560 }
561 }
562 else if (2 == cmd_head.flag)
563 {
564 //Need interrupt!
565 }
566
567 memcpy(cmd_head.data, cmd_head.addr, cmd_head.addr_len);
568
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]);
571
572 if (cmd_head.delay)
573 {
574 msleep(cmd_head.delay);
575 }
576
577 data_len = cmd_head.data_len;
578
579 while (data_len > 0)
580 {
581 if (data_len > DATA_LENGTH)
582 {
583 len = DATA_LENGTH;
584 }
585 else
586 {
587 len = data_len;
588 }
589
590 data_len -= DATA_LENGTH;
591
592 if (tool_i2c_read(cmd_head.data, len) <= 0)
593 {
594 GTP_ERROR("[READ]Read data failed!");
595 return FAIL;
596 }
597
598 memcpy(&page[loc], &cmd_head.data[GTP_ADDR_LENGTH], len);
599 loc += len;
600
601 GTP_DEBUG_ARRAY(&cmd_head.data[GTP_ADDR_LENGTH], len);
602 GTP_DEBUG_ARRAY(page, len);
603 }
604 }
605 else if (2 == cmd_head.wr)
606 {
607 // memcpy(page, "gt8", cmd_head.data_len);
608 // memcpy(page, "GT818", 5);
609 // page[5] = 0;
610
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);
614 }
615 else if (4 == cmd_head.wr)
616 {
617 page[0] = show_len >> 8;
618 page[1] = show_len & 0xff;
619 page[2] = total_len >> 8;
620 page[3] = total_len & 0xff;
621
622 return cmd_head.data_len;
623 }
624 else if (6 == cmd_head.wr)
625 {
626 //Read error code!
627 }
628 else if (8 == cmd_head.wr) //Read driver version
629 {
630 // memcpy(page, GTP_DRIVER_VERSION, strlen(GTP_DRIVER_VERSION));
631 s32 tmp_len;
632 tmp_len = strlen(GTP_DRIVER_VERSION);
633 memcpy(page, GTP_DRIVER_VERSION, tmp_len);
634 page[tmp_len] = 0;
635 }
636
637 return cmd_head.data_len;
638 }