import PULS_20180308
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / input / touchscreen / mediatek / GT9XX_2 / 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 * V1.4: 2013/06/08, new proc name
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
38 #pragma pack(1)
39 typedef struct
40 {
41 u8 wr; //write read flag£¬0:R 1:W 2:PID 3:
42 u8 flag; //0:no need flag/int 1: need flag 2:need int
43 u8 flag_addr[2]; //flag address
44 u8 flag_val; //flag val
45 u8 flag_relation; //flag_val:flag 0:not equal 1:equal 2:> 3:<
46 u16 circle; //polling cycle
47 u8 times; //plling times
48 u8 retry; //I2C retry times
49 u16 delay; //delay befor read or after write
50 u16 data_len; //data length
51 u8 addr_len; //address length
52 u8 addr[2]; //address
53 u8 res[3]; //reserved
54 u8 *data; //data pointer
55 } st_cmd_head;
56 #pragma pack()
57 st_cmd_head cmd_head;
58
59 #define UPDATE_FUNCTIONS
60 #define DATA_LENGTH_UINT 512
61 #define CMD_HEAD_LENGTH (sizeof(st_cmd_head) - sizeof(u8*))
62 static char procname[20] = {0};
63 extern struct i2c_client *i2c_client_point;
64 static struct i2c_client *gt_client = NULL;
65
66 #ifdef UPDATE_FUNCTIONS
67 extern s32 gup_enter_update_mode(struct i2c_client *client);
68 extern void gup_leave_update_mode(void);
69 extern s32 gup_update_proc(void *dir);
70 #endif
71
72 static struct proc_dir_entry *goodix_proc_entry;
73
74 static s32 goodix_tool_write(struct file *filp, const char __user *buff, unsigned long len, void *data);
75 static s32 goodix_tool_read(char *page, char **start, off_t off, int count, int *eof, void *data);
76 static s32(*tool_i2c_read)(u8 *, u16);
77 static s32(*tool_i2c_write)(u8 *, u16);
78
79 #if GTP_ESD_PROTECT
80 extern void gtp_esd_switch(struct i2c_client *client, s32 on);
81 #endif
82
83 s32 DATA_LENGTH = 0;
84 s8 IC_TYPE[16] = "GT9XX";
85
86 static void tool_set_proc_name(char * procname)
87 {
88 char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May",
89 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
90 char date[20] = {0};
91 char month[4] = {0};
92 int i = 0, n_month = 1, n_day = 0, n_year = 0;
93
94 sprintf(date, "%s", __DATE__);
95
96 //GTP_DEBUG("compile date: %s", date);
97
98 sscanf(date, "%s %d %d", month, &n_day, &n_year);
99
100 for (i = 0; i < 12; ++i)
101 {
102 if (!memcmp(months[i], month, 3))
103 {
104 n_month = i+1;
105 break;
106 }
107 }
108
109 sprintf(procname, "gmnode%04d%02d%02d", n_year, n_month, n_day);
110
111 //GTP_DEBUG("procname = %s", procname);
112 }
113 static s32 tool_i2c_read_no_extra(u8 *buf, u16 len)
114 {
115 s32 ret = -1;
116
117 ret = gtp_i2c_read(gt_client, buf, len + GTP_ADDR_LENGTH);
118 return ret;
119 }
120
121 static s32 tool_i2c_write_no_extra(u8 *buf, u16 len)
122 {
123 s32 ret = -1;
124
125 ret = gtp_i2c_write(gt_client, buf, len);
126 return ret;
127 }
128
129 static s32 tool_i2c_read_with_extra(u8 *buf, u16 len)
130 {
131 s32 ret = -1;
132 u8 pre[2] = {0x0f, 0xff};
133 u8 end[2] = {0x80, 0x00};
134
135 tool_i2c_write_no_extra(pre, 2);
136 ret = tool_i2c_read_no_extra(buf, len);
137 tool_i2c_write_no_extra(end, 2);
138
139 return ret;
140 }
141
142 static s32 tool_i2c_write_with_extra(u8 *buf, u16 len)
143 {
144 s32 ret = -1;
145 u8 pre[2] = {0x0f, 0xff};
146 u8 end[2] = {0x80, 0x00};
147
148 tool_i2c_write_no_extra(pre, 2);
149 ret = tool_i2c_write_no_extra(buf, len);
150 tool_i2c_write_no_extra(end, 2);
151
152 return ret;
153 }
154
155 static void register_i2c_func(void)
156 {
157 // if (!strncmp(IC_TYPE, "GT818", 5) || !strncmp(IC_TYPE, "GT816", 5)
158 // || !strncmp(IC_TYPE, "GT811", 5) || !strncmp(IC_TYPE, "GT818F", 6)
159 // || !strncmp(IC_TYPE, "GT827", 5) || !strncmp(IC_TYPE,"GT828", 5)
160 // || !strncmp(IC_TYPE, "GT813", 5))
161 if (strncmp(IC_TYPE, "GT8110", 6) && strncmp(IC_TYPE, "GT8105", 6)
162 && strncmp(IC_TYPE, "GT801", 5) && strncmp(IC_TYPE, "GT800", 5)
163 && strncmp(IC_TYPE, "GT801PLUS", 9) && strncmp(IC_TYPE, "GT811", 5)
164 && strncmp(IC_TYPE, "GTxxx", 5) && strncmp(IC_TYPE, "GT9XX", 5))
165 {
166 tool_i2c_read = tool_i2c_read_with_extra;
167 tool_i2c_write = tool_i2c_write_with_extra;
168 GTP_DEBUG("I2C function: with pre and end cmd!");
169 }
170 else
171 {
172 tool_i2c_read = tool_i2c_read_no_extra;
173 tool_i2c_write = tool_i2c_write_no_extra;
174 GTP_INFO("I2C function: without pre and end cmd!");
175 }
176 }
177
178 static void unregister_i2c_func(void)
179 {
180 tool_i2c_read = NULL;
181 tool_i2c_write = NULL;
182 GTP_INFO("I2C function: unregister i2c transfer function!");
183 }
184
185 static ssize_t goodix_tool_upper_read(struct file *file, char __user *buffer,
186 size_t count, loff_t *ppos)
187 {
188 return goodix_tool_read(buffer, NULL,0, count, NULL, ppos);
189 }
190
191 static ssize_t goodix_tool_upper_write(struct file *file, const char __user *buffer,
192 size_t count, loff_t *ppos)
193 {
194 return goodix_tool_write(file, buffer, count, ppos);
195 }
196
197 static const struct file_operations gt_tool_fops = {
198 .write = goodix_tool_upper_read,
199 .read = goodix_tool_upper_write
200 };
201
202 s32 init_wr_node(struct i2c_client *client)
203 {
204 s32 i;
205
206 gt_client = i2c_client_point;
207
208 memset(&cmd_head, 0, sizeof(cmd_head));
209 cmd_head.data = NULL;
210
211 i = 5;
212
213 while ((!cmd_head.data) && i)
214 {
215 cmd_head.data = kzalloc(i * DATA_LENGTH_UINT, GFP_KERNEL);
216
217 if (NULL != cmd_head.data)
218 {
219 break;
220 }
221
222 i--;
223 }
224
225 if (i)
226 {
227 DATA_LENGTH = i * DATA_LENGTH_UINT + GTP_ADDR_LENGTH;
228 GTP_INFO("Applied memory size:%d.", DATA_LENGTH);
229 }
230 else
231 {
232 GTP_ERROR("Apply for memory failed.");
233 return FAIL;
234 }
235
236 cmd_head.addr_len = 2;
237 cmd_head.retry = 5;
238
239 register_i2c_func();
240
241 tool_set_proc_name(procname);
242 #if 0 //fix kernel 3.10
243 goodix_proc_entry = create_proc_entry(procname, 0666, NULL);
244
245 if (goodix_proc_entry == NULL)
246 {
247 GTP_ERROR("Couldn't create proc entry!");
248 return FAIL;
249 }
250 else
251 {
252 GTP_INFO("Create proc entry success!");
253 goodix_proc_entry->write_proc = goodix_tool_write;
254 goodix_proc_entry->read_proc = goodix_tool_read;
255 }
256 #else
257 if(proc_create(procname, 0660, NULL, &gt_tool_fops)== NULL)
258 {
259 GTP_ERROR("create_proc_entry %s failed", procname);
260 return -1;
261 }
262 #endif
263
264 return SUCCESS;
265 }
266
267 void uninit_wr_node(void)
268 {
269 kfree(cmd_head.data);
270 cmd_head.data = NULL;
271 unregister_i2c_func();
272 remove_proc_entry(procname, NULL);
273 }
274
275 static u8 relation(u8 src, u8 dst, u8 rlt)
276 {
277 u8 ret = 0;
278
279 switch (rlt)
280 {
281 case 0:
282 ret = (src != dst) ? true : false;
283 break;
284
285 case 1:
286 ret = (src == dst) ? true : false;
287 GTP_DEBUG("equal:src:0x%02x dst:0x%02x ret:%d.", src, dst, (s32)ret);
288 break;
289
290 case 2:
291 ret = (src > dst) ? true : false;
292 break;
293
294 case 3:
295 ret = (src < dst) ? true : false;
296 break;
297
298 case 4:
299 ret = (src & dst) ? true : false;
300 break;
301
302 case 5:
303 ret = (!(src | dst)) ? true : false;
304 break;
305
306 default:
307 ret = false;
308 break;
309 }
310
311 return ret;
312 }
313
314 /*******************************************************
315 Function:
316 Comfirm function.
317 Input:
318 None.
319 Output:
320 Return write length.
321 ********************************************************/
322 static u8 comfirm(void)
323 {
324 s32 i = 0;
325 u8 buf[32];
326
327 // memcpy(&buf[GTP_ADDR_LENGTH - cmd_head.addr_len], &cmd_head.flag_addr, cmd_head.addr_len);
328 // memcpy(buf, &cmd_head.flag_addr, cmd_head.addr_len);//Modified by Scott, 2012-02-17
329 memcpy(buf, cmd_head.flag_addr, cmd_head.addr_len);
330
331 for (i = 0; i < cmd_head.times; i++)
332 {
333 if (tool_i2c_read(buf, 1) <= 0)
334 {
335 GTP_ERROR("Read flag data failed!");
336 return FAIL;
337 }
338
339 if (true == relation(buf[GTP_ADDR_LENGTH], cmd_head.flag_val, cmd_head.flag_relation))
340 {
341 GTP_DEBUG("value at flag addr:0x%02x.", buf[GTP_ADDR_LENGTH]);
342 GTP_DEBUG("flag value:0x%02x.", cmd_head.flag_val);
343 break;
344 }
345
346 msleep(cmd_head.circle);
347 }
348
349 if (i >= cmd_head.times)
350 {
351 GTP_ERROR("Didn't get the flag to continue!");
352 return FAIL;
353 }
354
355 return SUCCESS;
356 }
357
358 /*******************************************************
359 Function:
360 Goodix tool write function.
361 Input:
362 standard proc write function param.
363 Output:
364 Return write length.
365 ********************************************************/
366 static s32 goodix_tool_write(struct file *filp, const char __user *buff, unsigned long len, void *data)
367 {
368 u64 ret = 0;
369 GTP_DEBUG_FUNC();
370 GTP_DEBUG_ARRAY((u8 *)buff, len);
371
372 ret = copy_from_user(&cmd_head, buff, CMD_HEAD_LENGTH);
373
374 if (ret)
375 {
376 GTP_ERROR("copy_from_user failed.");
377 }
378
379 GTP_DEBUG("wr :0x%02x.", cmd_head.wr);
380 GTP_DEBUG("flag:0x%02x.", cmd_head.flag);
381 GTP_DEBUG("flag addr:0x%02x%02x.", cmd_head.flag_addr[0], cmd_head.flag_addr[1]);
382 GTP_DEBUG("flag val:0x%02x.", cmd_head.flag_val);
383 GTP_DEBUG("flag rel:0x%02x.", cmd_head.flag_relation);
384 GTP_DEBUG("circle :%d.", (s32)cmd_head.circle);
385 GTP_DEBUG("times :%d.", (s32)cmd_head.times);
386 GTP_DEBUG("retry :%d.", (s32)cmd_head.retry);
387 GTP_DEBUG("delay :%d.", (s32)cmd_head.delay);
388 GTP_DEBUG("data len:%d.", (s32)cmd_head.data_len);
389 GTP_DEBUG("addr len:%d.", (s32)cmd_head.addr_len);
390 GTP_DEBUG("addr:0x%02x%02x.", cmd_head.addr[0], cmd_head.addr[1]);
391 GTP_DEBUG("len:%d.", (s32)len);
392 GTP_DEBUG("buf[20]:0x%02x.", buff[CMD_HEAD_LENGTH]);
393
394 if (1 == cmd_head.wr)
395 {
396 // copy_from_user(&cmd_head.data[cmd_head.addr_len], &buff[CMD_HEAD_LENGTH], cmd_head.data_len);
397 ret = copy_from_user(&cmd_head.data[GTP_ADDR_LENGTH], &buff[CMD_HEAD_LENGTH], cmd_head.data_len);
398
399 if (ret)
400 {
401 GTP_ERROR("copy_from_user failed.");
402 }
403
404 memcpy(&cmd_head.data[GTP_ADDR_LENGTH - cmd_head.addr_len], cmd_head.addr, cmd_head.addr_len);
405
406 GTP_DEBUG_ARRAY(cmd_head.data, cmd_head.data_len + cmd_head.addr_len);
407 GTP_DEBUG_ARRAY((u8 *)&buff[CMD_HEAD_LENGTH], cmd_head.data_len);
408
409 if (1 == cmd_head.flag)
410 {
411 if (FAIL == comfirm())
412 {
413 GTP_ERROR("[WRITE]Comfirm fail!");
414 return FAIL;
415 }
416 }
417 else if (2 == cmd_head.flag)
418 {
419 //Need interrupt!
420 }
421
422 if (tool_i2c_write(&cmd_head.data[GTP_ADDR_LENGTH - cmd_head.addr_len],
423 cmd_head.data_len + cmd_head.addr_len) <= 0)
424 {
425 GTP_ERROR("[WRITE]Write data failed!");
426 return FAIL;
427 }
428
429 GTP_DEBUG_ARRAY(&cmd_head.data[GTP_ADDR_LENGTH - cmd_head.addr_len], cmd_head.data_len + cmd_head.addr_len);
430
431 if (cmd_head.delay)
432 {
433 msleep(cmd_head.delay);
434 }
435
436 return cmd_head.data_len + CMD_HEAD_LENGTH;
437 }
438 else if (3 == cmd_head.wr) //Write ic type
439 {
440 memcpy(IC_TYPE, cmd_head.data, cmd_head.data_len);
441 register_i2c_func();
442
443 return cmd_head.data_len + CMD_HEAD_LENGTH;
444 }
445 else if (5 == cmd_head.wr)
446 {
447 //memcpy(IC_TYPE, cmd_head.data, cmd_head.data_len);
448
449 return cmd_head.data_len + CMD_HEAD_LENGTH;
450 }
451 else if (7 == cmd_head.wr)//disable irq!
452 {
453 mt_eint_mask(CUST_EINT_TOUCH_PANEL_NUM);
454 #if GTP_ESD_PROTECT
455 gtp_esd_switch(i2c_client_point, SWITCH_OFF);
456 #endif
457 return CMD_HEAD_LENGTH;
458 }
459 else if (9 == cmd_head.wr) //enable irq!
460 {
461 mt_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM);
462 #if GTP_ESD_PROTECT
463 gtp_esd_switch(i2c_client_point, SWITCH_ON);
464 #endif
465 return CMD_HEAD_LENGTH;
466 }
467 else if (17 == cmd_head.wr)
468 {
469 ret = copy_from_user(&cmd_head.data[GTP_ADDR_LENGTH], &buff[CMD_HEAD_LENGTH], cmd_head.data_len);
470
471 if (ret)
472 {
473 GTP_DEBUG("copy_from_user failed.");
474 }
475
476 if (cmd_head.data[GTP_ADDR_LENGTH])
477 {
478 GTP_DEBUG("gtp enter rawdiff.");
479 gtp_rawdiff_mode = true;
480 }
481 else
482 {
483 gtp_rawdiff_mode = false;
484 GTP_DEBUG("gtp leave rawdiff.");
485 }
486
487 return CMD_HEAD_LENGTH;
488 }
489
490 #ifdef UPDATE_FUNCTIONS
491 else if (11 == cmd_head.wr) //Enter update mode!
492 {
493 if (FAIL == gup_enter_update_mode(gt_client))
494 {
495 return FAIL;
496 }
497 }
498 else if (13 == cmd_head.wr)//Leave update mode!
499 {
500 gup_leave_update_mode();
501 }
502 else if (15 == cmd_head.wr) //Update firmware!
503 {
504 show_len = 0;
505 total_len = 0;
506 if ((cmd_head.data == NULL)
507 || (cmd_head.data_len >= DATA_LENGTH)
508 || (cmd_head.data_len >= (len - CMD_HEAD_LENGTH))) {
509 GTP_ERROR("copy_from_user data out of range.");
510 return -EINVAL;
511 }
512
513 memset(cmd_head.data, 0, cmd_head.data_len + 1);
514 memcpy(cmd_head.data, &buff[CMD_HEAD_LENGTH], cmd_head.data_len);
515 GTP_DEBUG("update firmware, filename: %s", cmd_head.data);
516 if (FAIL == gup_update_proc((void *)cmd_head.data))
517 {
518 return FAIL;
519 }
520 }
521
522 #endif
523
524 return CMD_HEAD_LENGTH;
525 }
526
527 /*******************************************************
528 Function:
529 Goodix tool read function.
530 Input:
531 standard proc read function param.
532 Output:
533 Return read length.
534 ********************************************************/
535 static s32 goodix_tool_read(char *page, char **start, off_t off, int count, int *eof, void *data)
536 {
537 GTP_DEBUG_FUNC();
538
539 if (cmd_head.wr % 2)
540 {
541 return FAIL;
542 }
543 else if (!cmd_head.wr)
544 {
545 u16 len = 0;
546 s16 data_len = 0;
547 u16 loc = 0;
548
549 if (1 == cmd_head.flag)
550 {
551 if (FAIL == comfirm())
552 {
553 GTP_ERROR("[READ]Comfirm fail!");
554 return FAIL;
555 }
556 }
557 else if (2 == cmd_head.flag)
558 {
559 //Need interrupt!
560 }
561
562 memcpy(cmd_head.data, cmd_head.addr, cmd_head.addr_len);
563
564 GTP_DEBUG("[CMD HEAD DATA] ADDR:0x%02x%02x.", cmd_head.data[0], cmd_head.data[1]);
565 GTP_DEBUG("[CMD HEAD ADDR] ADDR:0x%02x%02x.", cmd_head.addr[0], cmd_head.addr[1]);
566
567 if (cmd_head.delay)
568 {
569 msleep(cmd_head.delay);
570 }
571
572 data_len = cmd_head.data_len;
573
574 while (data_len > 0)
575 {
576 if (data_len > DATA_LENGTH)
577 {
578 len = DATA_LENGTH;
579 }
580 else
581 {
582 len = data_len;
583 }
584
585 data_len -= DATA_LENGTH;
586
587 if (tool_i2c_read(cmd_head.data, len) <= 0)
588 {
589 GTP_ERROR("[READ]Read data failed!");
590 return FAIL;
591 }
592
593 memcpy(&page[loc], &cmd_head.data[GTP_ADDR_LENGTH], len);
594 loc += len;
595
596 GTP_DEBUG_ARRAY(&cmd_head.data[GTP_ADDR_LENGTH], len);
597 GTP_DEBUG_ARRAY(page, len);
598 }
599 }
600 else if (2 == cmd_head.wr)
601 {
602 // memcpy(page, "gt8", cmd_head.data_len);
603 // memcpy(page, "GT818", 5);
604 // page[5] = 0;
605
606 GTP_DEBUG("Return ic type:%s len:%d.", page, (s32)cmd_head.data_len);
607 return cmd_head.data_len;
608 //return sizeof(IC_TYPE_NAME);
609 }
610 else if (4 == cmd_head.wr)
611 {
612 page[0] = show_len >> 8;
613 page[1] = show_len & 0xff;
614 page[2] = total_len >> 8;
615 page[3] = total_len & 0xff;
616
617 return cmd_head.data_len;
618 }
619 else if (6 == cmd_head.wr)
620 {
621 //Read error code!
622 }
623 else if (8 == cmd_head.wr) //Read driver version
624 {
625 // memcpy(page, GTP_DRIVER_VERSION, strlen(GTP_DRIVER_VERSION));
626 s32 tmp_len;
627 tmp_len = strlen(GTP_DRIVER_VERSION);
628 memcpy(page, GTP_DRIVER_VERSION, tmp_len);
629 page[tmp_len] = 0;
630 }
631
632 return cmd_head.data_len;
633 }