import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / input / touchscreen / mediatek / GT818B / gt818b_driver.c
1 #include <linux/init.h>
2 #include <linux/module.h>
3 #include <linux/delay.h>
4 #include <linux/i2c.h>
5 #include <linux/input.h>
6 #include <linux/slab.h>
7 #include <linux/gpio.h>
8 #include <linux/sched.h>
9 #include <linux/kthread.h>
10 #include <linux/bitops.h>
11 #include <linux/kernel.h>
12 #include <linux/delay.h>
13 #include <linux/byteorder/generic.h>
14 #ifdef CONFIG_HAS_EARLYSUSPEND
15 #include <linux/earlysuspend.h>
16 #endif
17 #include <linux/interrupt.h>
18 #include <linux/time.h>
19 #include <linux/rtpm_prio.h>
20
21 #include <linux/proc_fs.h>
22 #include <asm/uaccess.h>
23
24
25
26 #include "tpd_custom_GT818B.h"
27 #include <mach/mt_pm_ldo.h>
28 #include <mach/mt_typedefs.h>
29 #include <mach/mt_boot.h>
30
31 #include "tpd.h"
32 #include <cust_eint.h>
33 #include <linux/jiffies.h>
34
35 #ifndef TPD_NO_GPIO
36 #include "cust_gpio_usage.h"
37 #endif
38
39 #include "gt818_fw.h"
40
41 extern struct tpd_device *tpd;
42
43 static int tpd_flag = 0;
44 static int tpd_halt=0;
45 static struct task_struct *thread = NULL;
46 static DECLARE_WAIT_QUEUE_HEAD(waiter);
47 static DEFINE_MUTEX(esd_check);
48
49
50 #ifdef TPD_HAVE_BUTTON
51 static int tpd_keys_local[TPD_KEY_COUNT] = TPD_KEYS;
52 static int tpd_keys_dim_local[TPD_KEY_COUNT][4] = TPD_KEYS_DIM;
53 #endif
54 #if (defined(TPD_WARP_START) && defined(TPD_WARP_END))
55 static int tpd_wb_start_local[TPD_WARP_CNT] = TPD_WARP_START;
56 static int tpd_wb_end_local[TPD_WARP_CNT] = TPD_WARP_END;
57 #endif
58 #if (defined(TPD_HAVE_CALIBRATION) && !defined(TPD_CUSTOM_CALIBRATION))
59 //static int tpd_calmat_local[8] = TPD_CALIBRATION_MATRIX;
60 static int tpd_def_calmat_local[8] = TPD_CALIBRATION_MATRIX;
61 #endif
62
63 static void tpd_eint_interrupt_handler(void);
64 static int touch_event_handler(void *unused);
65 static int tpd_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id);
66 static int tpd_i2c_detect(struct i2c_client *client, struct i2c_board_info *info);
67 static int tpd_i2c_remove(struct i2c_client *client);
68 #if 0
69 extern void mt65xx_eint_unmask(unsigned int line);
70 extern void mt65xx_eint_mask(unsigned int line);
71 extern void mt65xx_eint_set_hw_debounce(kal_uint8 eintno, kal_uint32 ms);
72 extern kal_uint32 mt65xx_eint_set_sens(kal_uint8 eintno, kal_bool sens);
73 extern void mt65xx_eint_registration(kal_uint8 eintno, kal_bool Dbounce_En,
74 kal_bool ACT_Polarity, void (EINT_FUNC_PTR)(void),
75 kal_bool auto_umask);
76 #endif
77 #ifdef CREATE_WR_NODE
78 extern s32 init_wr_node(struct i2c_client*);
79 extern void uninit_wr_node(void);
80 #endif
81
82 extern int gt818_downloader( struct i2c_client *client, unsigned short ver, unsigned char * data );
83 //#define TPD_DITO_SENSOR
84 //#define TPD_CONDITION_SWITCH
85 #define TPD_RESET_ISSUE_WORKAROUND
86 #define TPD_MAX_RESET_COUNT 3
87
88 #ifdef ESD_PROTECT
89 static struct delayed_work tpd_esd_check_work;
90 static struct workqueue_struct * tpd_esd_check_workqueue = NULL;
91 static void tpd_esd_check_func(struct work_struct *);
92 #define ESD_CHECK_CIRCLE 500
93 #define ESD_CHECK_DATA_LEN 6
94 #define ESD_CHECK_TIME 3
95 unsigned char esd_check_data[ESD_CHECK_TIME*ESD_CHECK_DATA_LEN];
96 int esd_checked_time = 0;
97 #endif
98 #define TPD_OK 0
99
100 #define TPD_CONFIG_REG_BASE 0x6A2
101 #define TPD_FREQ_CAL_RESULT 0x70F
102 #define TPD_TOUCH_INFO_REG_BASE 0x712
103 #define TPD_POINT_INFO_REG_BASE 0x722
104 #define TPD_VERSION_INFO_REG 0x713
105 #define TPD_VERSION_BASIC_REG 0x717
106 #define TPD_KEY_INFO_REG_BASE 0x721
107 #define TPD_POWER_MODE_REG 0x692
108 #define TPD_HANDSHAKING_START_REG 0xFFF
109 #define TPD_HANDSHAKING_END_REG 0x8000
110 #define TPD_FREQ_REG 0x1522
111 #define TPD_SOFT_RESET_MODE 0x01
112 #define TPD_POINT_INFO_LEN 8
113 #define TPD_MAX_POINTS 5
114 #define MAX_TRANSACTION_LENGTH 8
115 #define I2C_DEVICE_ADDRESS_LEN 2
116 #define I2C_MASTER_CLOCK 200 //400 ·¢ÏÖÔÚÖжÏÖÐIIC×ÊÔ´»áÉêÇë²»³É¹¦
117
118 //#define TPD_X_RES 480
119 //#define TPD_Y_RES 800
120
121 extern kal_bool upmu_is_chr_det(void);
122
123 #define MAX_I2C_TRANSFER_SIZE (MAX_TRANSACTION_LENGTH - I2C_DEVICE_ADDRESS_LEN)
124
125 #define GT818_CONFIG_PROC_FILE "gt818_config"
126 #define CONFIG_LEN (106)
127
128 struct tpd_info_t
129 {
130 u8 vendor_id_1;
131 u8 vendor_id_2;
132 u8 product_id_1;
133 u8 product_id_2;
134 u8 version_1;
135 u8 version_2;
136 };
137
138 #ifdef TPD_DITO_SENSOR
139 #else //SITO
140 static u8 *cfg_data_version_d = cfg_data_version_b;
141 static u8 *cfg_data_with_charger_version_d = cfg_data_with_charger_version_b;
142 #endif
143
144 static struct i2c_client *i2c_client = NULL;
145 static const struct i2c_device_id tpd_i2c_id[] = {{"gt818b",0},{}};
146 static unsigned short force[] = {0, 0xBA, I2C_CLIENT_END,I2C_CLIENT_END};
147 static const unsigned short * const forces[] = { force, NULL };
148 //static struct i2c_client_address_data addr_data = { .forces = forces,};
149 static struct i2c_board_info __initdata i2c_tpd={ I2C_BOARD_INFO("gt818b", (0xBA>>1))};
150 static struct i2c_driver tpd_i2c_driver =
151 {
152 .probe = tpd_i2c_probe,
153 .remove = tpd_i2c_remove,
154 .detect = tpd_i2c_detect,
155 .driver.name = "gt818b",
156 .id_table = tpd_i2c_id,
157 .address_list = (const unsigned short*) forces,
158 };
159 struct tpd_info_t tpd_info;
160 static u8 *cfg_data = NULL;
161 static u8 *cfg_data_with_charger = NULL;
162
163 /* proc file system */
164 static int i2c_read_bytes( struct i2c_client *client, u16 addr, u8 *rxbuf, int len );
165 static int i2c_write_bytes( struct i2c_client *client, u16 addr, u8 *txbuf, int len );
166 static int i2c_write_dummy( struct i2c_client *client, u16 addr );
167 static struct proc_dir_entry *gt818_config_proc = NULL;
168
169 #define VELOCITY_CUSTOM_GT818B
170 #ifdef VELOCITY_CUSTOM_GT818B
171 #include <linux/device.h>
172 #include <linux/miscdevice.h>
173 #include <asm/uaccess.h>
174
175 #ifndef TPD_VELOCITY_CUSTOM_X
176 #define TPD_VELOCITY_CUSTOM_X 10
177 #endif
178 #ifndef TPD_VELOCITY_CUSTOM_Y
179 #define TPD_VELOCITY_CUSTOM_Y 10
180 #endif
181
182 // for magnify velocity********************************************
183 #define TOUCH_IOC_MAGIC 'A'
184
185 #define TPD_GET_VELOCITY_CUSTOM_X _IO(TOUCH_IOC_MAGIC,0)
186 #define TPD_GET_VELOCITY_CUSTOM_Y _IO(TOUCH_IOC_MAGIC,1)
187
188 int g_v_magnify_x =TPD_VELOCITY_CUSTOM_X;
189 int g_v_magnify_y =TPD_VELOCITY_CUSTOM_Y;
190 static int tpd_misc_open(struct inode *inode, struct file *file)
191 {
192
193 return nonseekable_open(inode, file);
194 }
195 /*----------------------------------------------------------------------------*/
196 static int tpd_misc_release(struct inode *inode, struct file *file)
197 {
198 return 0;
199 }
200 /*----------------------------------------------------------------------------*/
201
202 static long tpd_unlocked_ioctl(struct file *file, unsigned int cmd,
203 unsigned long arg)
204 {
205
206 //char strbuf[256];
207 void __user *data;
208
209 long err = 0;
210
211 if(_IOC_DIR(cmd) & _IOC_READ)
212 {
213 err = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd));
214 }
215 else if(_IOC_DIR(cmd) & _IOC_WRITE)
216 {
217 err = !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd));
218 }
219
220 if(err)
221 {
222 printk("tpd: access error: %08X, (%2d, %2d)\n", cmd, _IOC_DIR(cmd), _IOC_SIZE(cmd));
223 return -EFAULT;
224 }
225
226 switch(cmd)
227 {
228 case TPD_GET_VELOCITY_CUSTOM_X:
229 data = (void __user *) arg;
230 if(data == NULL)
231 {
232 err = -EINVAL;
233 break;
234 }
235
236 if(copy_to_user(data, &g_v_magnify_x, sizeof(g_v_magnify_x)))
237 {
238 err = -EFAULT;
239 break;
240 }
241 break;
242
243 case TPD_GET_VELOCITY_CUSTOM_Y:
244 data = (void __user *) arg;
245 if(data == NULL)
246 {
247 err = -EINVAL;
248 break;
249 }
250
251 if(copy_to_user(data, &g_v_magnify_y, sizeof(g_v_magnify_y)))
252 {
253 err = -EFAULT;
254 break;
255 }
256 break;
257
258
259 default:
260 printk("tpd: unknown IOCTL: 0x%08x\n", cmd);
261 err = -ENOIOCTLCMD;
262 break;
263
264 }
265
266 return err;
267 }
268
269
270 static struct file_operations tpd_fops = {
271 // .owner = THIS_MODULE,
272 .open = tpd_misc_open,
273 .release = tpd_misc_release,
274 .unlocked_ioctl = tpd_unlocked_ioctl,
275 };
276 /*----------------------------------------------------------------------------*/
277 static struct miscdevice tpd_misc_device = {
278 .minor = MISC_DYNAMIC_MINOR,
279 .name = "touch",
280 .fops = &tpd_fops,
281 };
282
283 //**********************************************
284 #endif
285
286
287 static int gt818_config_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data)
288 {
289 char *ptr = page;
290 char temp_data[CONFIG_LEN] = {0};
291 int i;
292
293 ptr += sprintf( ptr, "==== GT818 config init value====\n" );
294
295 for ( i = 0 ; i < CONFIG_LEN ; i++ )
296 {
297 ptr += sprintf( ptr, "0x%02X ", cfg_data[i] );
298
299 if ( i%8 == 7 )
300 ptr += sprintf( ptr, "\n" );
301 }
302
303 ptr += sprintf( ptr, "\n" );
304 ptr += sprintf( ptr, "==== GT818 charger init config ====\n" );
305 for ( i = 0 ; i < CONFIG_LEN ; i++ )
306 {
307 ptr += sprintf( ptr, "0x%02X ", cfg_data_with_charger[i] );
308
309 if ( i%8 == 7 )
310 ptr += sprintf( ptr, "\n" );
311 }
312 ptr += sprintf( ptr, "\n" );
313 ptr += sprintf( ptr, "==== GT818 config real value====\n" );
314 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_START_REG );
315 i2c_read_bytes( i2c_client, TPD_CONFIG_REG_BASE, temp_data, CONFIG_LEN);
316 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_END_REG );
317 for ( i = 0 ; i < CONFIG_LEN ; i++ )
318 {
319 ptr += sprintf( ptr, "0x%02X ", temp_data[i] );
320
321 if ( i%8 == 7 )
322 ptr += sprintf( ptr, "\n" );
323 }
324 ptr += sprintf( ptr, "\n" );
325 *eof = 1;
326 return ( ptr - page );
327 }
328
329 static int gt818_config_write_proc(struct file *file, const char *buffer, unsigned long count, void *data)
330 {
331 kal_bool temp = upmu_is_chr_det();
332
333 TPD_DEBUG("write count %ld\n", count );
334
335 if ( count != (CONFIG_LEN*2 ) )
336 {
337 TPD_DEBUG("size not match [%d:%ld]\n", CONFIG_LEN*2, count );
338 return -EFAULT;
339 }
340
341 if (copy_from_user( cfg_data, buffer, count/2))
342 {
343 TPD_DEBUG("copy from user fail\n");
344 return -EFAULT;
345 }
346
347 if (copy_from_user( cfg_data_with_charger, buffer + CONFIG_LEN, count/2))
348 {
349 TPD_DEBUG("copy from user fail\n");
350 return -EFAULT;
351 }
352 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_START_REG );
353
354 if ( temp )
355 i2c_write_bytes( i2c_client, TPD_CONFIG_REG_BASE, cfg_data_with_charger, CONFIG_LEN );
356 else
357 i2c_write_bytes( i2c_client, TPD_CONFIG_REG_BASE, cfg_data, CONFIG_LEN );
358
359 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_END_REG );
360 return count;
361 }
362
363 #if 0 //#ifdef TPD_RESET_ISSUE_WORKAROUND
364 static int gt818_check_data(unsigned char *buffer, int count)
365 {
366 unsigned char buf[128] = {0}; // need > sizeof(buffer)
367 int i = 0, error = -1, ret = -1;
368
369 do
370 {
371 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_START_REG );
372 ret = i2c_read_bytes( i2c_client, TPD_CONFIG_REG_BASE, buf, CONFIG_LEN);
373 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_END_REG );
374 if(ret)
375 {
376 TPD_DMESG("read i2c error\n");
377 break;
378 }
379 for(i = 0; i < CONFIG_LEN; i++) // the last one byte will be changed
380 {
381 if(buf[i] != cfg_data[i])
382 {
383 TPD_DMESG(TPD_DEVICE " fail to write touch panel config, %d bytes, expect:0x%x, real:0x%x\n", i,cfg_data[i], buf[i]);
384 error = -1;
385 break;
386 }
387 }
388 if(i == CONFIG_LEN)
389 {
390 TPD_DMESG(TPD_DEVICE " write touch panel config OK, count:%d\n", count);
391 error = 0;
392 break;
393 }
394
395 if(error == -1)
396 {
397 for(i = 0; i < CONFIG_LEN - 1; i++)
398 {
399 printk(" 0x%02X", buf[i]);
400 if(i%8 == 7)
401 printk("\n");
402 }
403 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_START_REG );
404 i2c_write_bytes( i2c_client, TPD_CONFIG_REG_BASE, cfg_data, CONFIG_LEN );
405 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_END_REG );
406 }
407 }while(count--);
408
409 return error;
410 }
411
412 #endif
413 static int i2c_read_bytes( struct i2c_client *client, u16 addr, u8 *rxbuf, int len )
414 {
415 u8 buffer[I2C_DEVICE_ADDRESS_LEN];
416 u8 retry;
417 u16 left = len;
418 u16 offset = 0;
419
420 struct i2c_msg msg[2] =
421 {
422 {
423 .addr = ((client->addr&I2C_MASK_FLAG )|(I2C_ENEXT_FLAG )),
424 .flags = 0,
425 .buf = buffer,
426 .len = I2C_DEVICE_ADDRESS_LEN,
427 .timing = I2C_MASTER_CLOCK
428 },
429 {
430 .addr = ((client->addr&I2C_MASK_FLAG )|(I2C_ENEXT_FLAG )),
431 .flags = I2C_M_RD,
432 .timing = I2C_MASTER_CLOCK
433 },
434 };
435
436 if ( rxbuf == NULL )
437 return -1;
438
439 TPD_DEBUG("i2c_read_bytes to device %02X address %04X len %d\n", client->addr, addr, len );
440
441 while ( left > 0 )
442 {
443 buffer[0] = ( ( addr+offset ) >> 8 ) & 0xFF;
444 buffer[1] = ( addr+offset ) & 0xFF;
445
446 msg[1].buf = &rxbuf[offset];
447
448 if ( left > MAX_TRANSACTION_LENGTH )
449 {
450 msg[1].len = MAX_TRANSACTION_LENGTH;
451 left -= MAX_TRANSACTION_LENGTH;
452 offset += MAX_TRANSACTION_LENGTH;
453 }
454 else
455 {
456 msg[1].len = left;
457 left = 0;
458 }
459
460 retry = 0;
461
462 while ( i2c_transfer( client->adapter, &msg[0], 2 ) != 2 )
463 {
464 retry++;
465
466 if ( retry == 20 )
467 {
468 TPD_DEBUG("I2C read 0x%X length=%d failed\n", addr + offset, len);
469 TPD_DMESG("I2C read 0x%X length=%d failed\n", addr + offset, len);
470 return -1;
471 }
472 }
473 }
474
475 return 0;
476 }
477
478 static int i2c_write_bytes( struct i2c_client *client, u16 addr, u8 *txbuf, int len )
479 {
480 u8 buffer[MAX_TRANSACTION_LENGTH];
481 u16 left = len;
482 u16 offset = 0;
483 u8 retry = 0;
484
485 struct i2c_msg msg =
486 {
487 .addr = ((client->addr&I2C_MASK_FLAG )|(I2C_ENEXT_FLAG )),
488 .flags = 0,
489 .buf = buffer,
490 .timing = I2C_MASTER_CLOCK,
491 };
492
493
494 if ( txbuf == NULL )
495 return -1;
496
497 TPD_DEBUG("i2c_write_bytes to device %02X address %04X len %d\n", client->addr, addr, len );
498
499 while ( left > 0 )
500 {
501 retry = 0;
502
503 buffer[0] = ( (addr+offset) >> 8 ) & 0xFF;
504 buffer[1] = ( addr+offset ) & 0xFF;
505
506 if ( left > MAX_I2C_TRANSFER_SIZE )
507 {
508 memcpy( &buffer[I2C_DEVICE_ADDRESS_LEN], &txbuf[offset], MAX_I2C_TRANSFER_SIZE );
509 msg.len = MAX_TRANSACTION_LENGTH;
510 left -= MAX_I2C_TRANSFER_SIZE;
511 offset += MAX_I2C_TRANSFER_SIZE;
512 }
513 else
514 {
515 memcpy( &buffer[I2C_DEVICE_ADDRESS_LEN], &txbuf[offset], left );
516 msg.len = left + I2C_DEVICE_ADDRESS_LEN;
517 left = 0;
518 }
519
520 TPD_DEBUG("byte left %d offset %d\n", left, offset );
521
522 while ( i2c_transfer( client->adapter, &msg, 1 ) != 1 )
523 {
524 retry++;
525
526 if ( retry == 20 )
527 {
528 TPD_DEBUG("I2C write 0x%X%X length=%d failed\n", buffer[0], buffer[1], len);
529 TPD_DMESG("I2C write 0x%X%X length=%d failed\n", buffer[0], buffer[1], len);
530 return -1;
531 }
532 else
533 TPD_DEBUG("I2C write retry %d addr 0x%X%X\n", retry, buffer[0], buffer[1]);
534
535 }
536 }
537
538 return 0;
539 }
540
541 static int i2c_write_dummy( struct i2c_client *client, u16 addr )
542 {
543 u8 buffer[MAX_TRANSACTION_LENGTH];
544
545 struct i2c_msg msg =
546 {
547 .addr = client->addr,
548 .flags = 0,
549 .buf = buffer,
550 .timing = I2C_MASTER_CLOCK,
551 .len = 2
552 };
553
554 TPD_DEBUG("i2c_write_dummy to device %02X address %04X\n", client->addr, addr );
555
556 buffer[0] = (addr >> 8) & 0xFF;
557 buffer[1] = (addr) & 0xFF;
558
559 i2c_transfer( client->adapter, &msg, 1 );
560
561 return 0;
562 }
563
564 static int tpd_i2c_detect(struct i2c_client *client, struct i2c_board_info *info)
565 {
566 strcpy(info->type, "mtk-tpd");
567 return 0;
568 }
569
570 //
571 void tpd_reset_fuc(struct i2c_client *client)
572 {
573
574 int err=0;
575 char int_type = 0;
576 TPD_DMESG("[mtk-tpd] tpd_reset_fuc: \n");
577 //power off, need confirm with SA
578 #ifdef TPD_POWER_SOURCE_CUSTOM
579 hwPowerDown(TPD_POWER_SOURCE_CUSTOM, "TP");
580 #else
581 hwPowerDown(MT65XX_POWER_LDO_VGP2, "TP");
582 #endif
583 #ifdef TPD_POWER_SOURCE_1800
584 hwPowerDown(TPD_POWER_SOURCE_1800, "TP");
585 #endif
586
587 msleep(20);
588
589 //INT pin use gpio mode we do not want receive int before touch config done
590 //set gpio mode
591 mt_set_gpio_mode(GPIO_CTP_EINT_PIN, GPIO_CTP_EINT_PIN_M_GPIO);
592 mt_set_gpio_dir(GPIO_CTP_EINT_PIN, GPIO_DIR_IN);
593 mt_set_gpio_pull_enable(GPIO_CTP_EINT_PIN, GPIO_PULL_DISABLE);
594
595
596 //power on, need confirm with SA
597 #ifdef TPD_POWER_SOURCE_CUSTOM
598 hwPowerOn(TPD_POWER_SOURCE_CUSTOM, VOL_2800, "TP");
599 #else
600 hwPowerOn(MT65XX_POWER_LDO_VGP2, VOL_2800, "TP");
601 #endif
602 #ifdef TPD_POWER_SOURCE_1800
603 hwPowerOn(TPD_POWER_SOURCE_1800, VOL_1800, "TP");
604 #endif
605
606 msleep(20);
607
608
609 // reset
610 mt_set_gpio_mode(GPIO_CTP_RST_PIN, GPIO_CTP_RST_PIN_M_GPIO);
611 mt_set_gpio_dir(GPIO_CTP_RST_PIN, GPIO_DIR_OUT);
612 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ZERO);
613 msleep(1);
614
615 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ONE);
616 msleep(20);
617
618
619 //i2c_write_dummy( client, TPD_HANDSHAKING_START_REG );
620 err = i2c_read_bytes( client, TPD_VERSION_INFO_REG, (u8 *)&tpd_info, sizeof( struct tpd_info_t ) );
621 //i2c_write_dummy( client, TPD_HANDSHAKING_END_REG );
622 if ( err )
623 {
624 TPD_DMESG(TPD_DEVICE " fail to get tpd info %d\n", err );
625
626 //return err;
627 }
628 else
629 {
630 TPD_DMESG( "TPD info\n");
631 TPD_DMESG( "vendor %02X %02X\n", tpd_info.vendor_id_1, tpd_info.vendor_id_2 );
632 TPD_DMESG( "product %02X %02X\n", tpd_info.product_id_1, tpd_info.product_id_2 );
633 TPD_DMESG( "version %02X %02X\n", tpd_info.version_1, tpd_info.version_2 );
634 }
635
636
637 // setting resolution, RES_X, RES_Y
638 #ifdef RES_AUTO_CONFIG
639 cfg_data[59] = cfg_data_with_charger[59] = (TPD_X_RES&0xff);//(TPD_RES_X&0xff);
640 cfg_data[60] = cfg_data_with_charger[60] = ((TPD_X_RES>>8)&0xff);//((TPD_RES_X>>8)&0xff);
641 cfg_data[61] = cfg_data_with_charger[61] = (TPD_Y_RES&0xff);//(TPD_RES_Y&0xff);
642 cfg_data[62] = cfg_data_with_charger[62] = ((TPD_Y_RES>>8)&0xff);//((TPD_RES_Y>>8)&0xff);
643 #endif
644
645
646 err = i2c_write_bytes( client, TPD_CONFIG_REG_BASE, cfg_data, CONFIG_LEN );
647
648 msleep(100);
649
650 int_type = ((cfg_data[55]>>3)&0x01);
651 // set INT mode
652 mt_set_gpio_mode(GPIO_CTP_EINT_PIN, GPIO_CTP_EINT_PIN_M_EINT);
653 mt_set_gpio_dir(GPIO_CTP_EINT_PIN, GPIO_DIR_IN);
654 mt_set_gpio_pull_enable(GPIO_CTP_EINT_PIN, GPIO_PULL_DISABLE);
655 //mt_set_gpio_pull_select(GPIO_CTP_EINT_PIN, GPIO_PULL_UP);
656
657 msleep(50);
658
659 //mt65xx_eint_set_sens(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINT_TOUCH_PANEL_SENSITIVE);
660 //mt65xx_eint_set_hw_debounce(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINT_TOUCH_PANEL_DEBOUNCE_CN);
661 //mt65xx_eint_registration(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINT_TOUCH_PANEL_DEBOUNCE_EN, CUST_EINT_TOUCH_PANEL_POLARITY, tpd_eint_interrupt_handler, 1);
662 if(int_type)
663 {
664 mt_eint_registration(CUST_EINT_TOUCH_PANEL_NUM, EINTF_TRIGGER_RISING, tpd_eint_interrupt_handler, 1);
665 }
666 else
667 {
668 mt_eint_registration(CUST_EINT_TOUCH_PANEL_NUM, EINTF_TRIGGER_FALLING, tpd_eint_interrupt_handler, 1);
669 }
670
671 TPD_DMESG("[mtk-tpd] tpd_reset_fuc: done\n");
672 }
673 //
674 static int tpd_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
675 {
676 int err = 0;
677
678 #ifdef AUTOUPDATE_FIRMWARE
679
680 u8 version_tmp[2] = {0};
681 char cpf = 0;
682 char i;
683 #endif
684 //int ret = 0;
685 char int_type = 0;
686 #ifdef TPD_RESET_ISSUE_WORKAROUND
687 int reset_count = 0;
688
689 reset_proc:
690
691
692
693 #ifdef TPD_POWER_SOURCE_CUSTOM
694 //power on, need confirm with SA
695 hwPowerOn(TPD_POWER_SOURCE_CUSTOM, VOL_2800, "TP");
696 #else
697 hwPowerOn(MT65XX_POWER_LDO_VGP2, VOL_2800, "TP");
698 #endif
699 #ifdef TPD_POWER_SOURCE_1800
700 hwPowerOn(TPD_POWER_SOURCE_1800, VOL_1800, "TP");
701 #endif
702
703 // set INT mode
704 mt_set_gpio_mode(GPIO_CTP_EINT_PIN, GPIO_CTP_EINT_PIN_M_GPIO);
705 mt_set_gpio_dir(GPIO_CTP_EINT_PIN, GPIO_DIR_IN);
706 mt_set_gpio_pull_enable(GPIO_CTP_EINT_PIN, GPIO_PULL_DISABLE);
707
708 // reset
709 mt_set_gpio_mode(GPIO_CTP_RST_PIN, GPIO_CTP_RST_PIN_M_GPIO);
710 mt_set_gpio_dir(GPIO_CTP_RST_PIN, GPIO_DIR_OUT);
711 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ZERO);
712 msleep(1);
713
714 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ONE);
715 msleep(50);
716
717 #else
718
719 #ifdef TPD_POWER_SOURCE_CUSTOM
720 //power on, need confirm with SA
721 hwPowerOn(TPD_POWER_SOURCE_CUSTOM, VOL_2800, "TP");
722 #else
723 hwPowerOn(MT65XX_POWER_LDO_VGP2, VOL_2800, "TP");
724 #endif
725 #ifdef TPD_POWER_SOURCE_1800
726 hwPowerOn(TPD_POWER_SOURCE_1800, VOL_1800, "TP");
727 #endif
728
729
730 // set deep sleep off
731 mt_set_gpio_mode(GPIO_CTP_RST_PIN, GPIO_CTP_RST_PIN_M_GPIO);
732 mt_set_gpio_dir(GPIO_CTP_RST_PIN, GPIO_DIR_OUT);
733 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ONE);
734 msleep(10);
735
736 #endif
737
738 memset( &tpd_info, 0, sizeof( struct tpd_info_t ) );
739 i2c_write_dummy( client, TPD_HANDSHAKING_START_REG );
740 err = i2c_read_bytes( client, TPD_VERSION_INFO_REG, (u8 *)&tpd_info, sizeof( struct tpd_info_t ) );
741 i2c_write_dummy( client, TPD_HANDSHAKING_END_REG );
742 if ( err )
743 {
744 TPD_DMESG(TPD_DEVICE " fail to get tpd info %d\n", err );
745 #ifdef TPD_RESET_ISSUE_WORKAROUND
746 if ( reset_count < TPD_MAX_RESET_COUNT )
747 {
748 reset_count++;
749 goto reset_proc;
750 }
751 #endif
752 return err;
753 }
754 else
755 {
756 TPD_DMESG( "TPD info\n");
757 TPD_DMESG( "vendor %02X %02X\n", tpd_info.vendor_id_1, tpd_info.vendor_id_2 );
758 TPD_DMESG( "product %02X %02X\n", tpd_info.product_id_1, tpd_info.product_id_2 );
759 TPD_DMESG( "version %02X %02X\n", tpd_info.version_1, tpd_info.version_2 );
760
761 #ifdef AUTOUPDATE_FIRMWARE
762 //may be cause WCT time out if i2c ack error
763 for(i = 0;i < 10;i++)
764 {
765
766 err = i2c_read_bytes( client, TPD_VERSION_BASIC_REG, version_tmp, 2 );
767
768 if(((tpd_info.version_1) !=version_tmp[0])||((tpd_info.version_2) != version_tmp[1]))
769 {
770 tpd_info.version_1 = version_tmp[0];
771 tpd_info.version_2 = version_tmp[1];
772 msleep(5);
773 break;
774 }
775 msleep(5);
776 cpf++;
777 }
778
779 if(cpf == 10)
780 {
781 TPD_DMESG( "after check version %02X %02X\n", tpd_info.version_1, tpd_info.version_2 );
782 }
783 else
784 {
785 tpd_info.version_1 = 0xff;
786 tpd_info.version_2 = 0xff;
787 TPD_DMESG( "after check version %02X %02X\n", tpd_info.version_1, tpd_info.version_2 );
788 }
789
790 //check if upgradable
791 gt818_downloader( client, tpd_info.version_2*256 + tpd_info.version_1, goodix_gt818_firmware );
792 #endif
793 }
794
795 #ifdef VELOCITY_CUSTOM_GT818B
796 if((err = misc_register(&tpd_misc_device)))
797 {
798 printk("mtk_tpd: tpd_misc_device register failed\n");
799
800 }
801 #endif
802
803 i2c_client = client;
804
805 // Create proc file system
806 gt818_config_proc = create_proc_entry( GT818_CONFIG_PROC_FILE, 0664, NULL);
807
808 if ( gt818_config_proc == NULL )
809 {
810 TPD_DEBUG("create_proc_entry %s failed\n", GT818_CONFIG_PROC_FILE );
811 }
812 else
813 {
814 gt818_config_proc->read_proc = gt818_config_read_proc;
815 gt818_config_proc->write_proc = gt818_config_write_proc;
816 }
817
818 #ifdef CREATE_WR_NODE
819
820 init_wr_node(client);
821 #endif
822
823 if ( tpd_info.version_1 < 0x7A ) // Chip version B or Version C
824 {
825 TPD_DMESG(TPD_DEVICE " read version %02X , use B version config\n", tpd_info.version_1 );
826 TPD_DMESG(TPD_DEVICE " B version: 0x4B~0x59, C version 0x5A~0x79\n" );
827 cfg_data = cfg_data_version_b;
828 cfg_data_with_charger = cfg_data_with_charger_version_b;
829 }
830 else if ( tpd_info.version_1 < 0xA0 )
831 {
832 TPD_DMESG(TPD_DEVICE " read version %02X, use D version config\n", tpd_info.version_1 );
833 TPD_DMESG(TPD_DEVICE " D version: 0x7A~0x99, E version 0x9A~0xB9\n" );
834 cfg_data = cfg_data_version_d;
835 cfg_data_with_charger = cfg_data_with_charger_version_d;
836 }
837 else
838 {
839 TPD_DMESG(TPD_DEVICE " unknow Chip version %02X ,use B version config\n", tpd_info.version_1 );
840 cfg_data = cfg_data_version_b;
841 cfg_data_with_charger = cfg_data_with_charger_version_b;
842 }
843 // setting resolution, RES_X, RES_Y
844 #ifdef RES_AUTO_CONFIG
845 cfg_data[59] = cfg_data_with_charger[59] = (TPD_X_RES&0xff);//(TPD_RES_X&0xff);
846 cfg_data[60] = cfg_data_with_charger[60] = ((TPD_X_RES>>8)&0xff);//((TPD_RES_X>>8)&0xff);
847 cfg_data[61] = cfg_data_with_charger[61] = (TPD_Y_RES&0xff);//(TPD_RES_Y&0xff);
848 cfg_data[62] = cfg_data_with_charger[62] = ((TPD_Y_RES>>8)&0xff);//((TPD_RES_Y>>8)&0xff);
849 #endif
850 int_type = ((cfg_data[55]>>3)&0x01);
851 i2c_write_dummy( client, TPD_HANDSHAKING_START_REG );
852 err = i2c_write_bytes( client, TPD_CONFIG_REG_BASE, cfg_data, CONFIG_LEN );
853 i2c_write_dummy( client, TPD_HANDSHAKING_END_REG );
854 #if 0 //#ifdef TPD_RESET_ISSUE_WORKAROUND
855 err = gt818_check_data(cfg_data, TPD_MAX_RESET_COUNT);
856 if ( err )
857 {
858 TPD_DMESG(TPD_DEVICE " retry TPD_MAX_RESET_COUNT fail to write tpd cfg %d\n", err );
859 return err;
860 }
861 #endif
862
863 thread = kthread_run(touch_event_handler, 0, TPD_DEVICE);
864
865 if (IS_ERR(thread))
866 {
867 err = PTR_ERR(thread);
868 TPD_DMESG(TPD_DEVICE " failed to create kernel thread: %d\n", err);
869 }
870
871 #ifdef HAVE_TOUCH_KEY
872 //for(retry = 0; retry < MAX_KEY_NUM; retry++)
873 //{
874 /// input_set_capability(tpd->dev,EV_KEY,touch_key_array[retry]);
875 //}
876 #endif
877
878
879 #ifndef TPD_RESET_ISSUE_WORKAROUND
880 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ZERO);
881 msleep(10);
882 #endif
883
884 // set INT mode
885 mt_set_gpio_mode(GPIO_CTP_EINT_PIN, GPIO_CTP_EINT_PIN_M_EINT);
886 mt_set_gpio_dir(GPIO_CTP_EINT_PIN, GPIO_DIR_IN);
887 mt_set_gpio_pull_enable(GPIO_CTP_EINT_PIN, GPIO_PULL_DISABLE);
888 //mt_set_gpio_pull_select(GPIO_CTP_EINT_PIN, GPIO_PULL_UP);
889
890 msleep(50);
891
892 //mt65xx_eint_set_sens(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINT_TOUCH_PANEL_SENSITIVE);
893 //mt65xx_eint_set_hw_debounce(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINT_TOUCH_PANEL_DEBOUNCE_CN);
894 //mt65xx_eint_registration(CUST_EINT_TOUCH_PANEL_NUM, CUST_EINT_TOUCH_PANEL_DEBOUNCE_EN, CUST_EINT_TOUCH_PANEL_POLARITY, tpd_eint_interrupt_handler, 1);
895 if(int_type)
896 {
897 mt_eint_registration(CUST_EINT_TOUCH_PANEL_NUM, EINTF_TRIGGER_RISING, tpd_eint_interrupt_handler, 1);
898 }
899 else
900 {
901 mt_eint_registration(CUST_EINT_TOUCH_PANEL_NUM, EINTF_TRIGGER_FALLING, tpd_eint_interrupt_handler, 1);
902 }
903 mt_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM);
904
905 #ifndef TPD_RESET_ISSUE_WORKAROUND
906 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ONE);
907 #endif
908
909 #ifdef ESD_PROTECT
910
911 tpd_esd_check_workqueue = create_workqueue("tpd_esd_check");
912
913 INIT_DELAYED_WORK(&tpd_esd_check_work, tpd_esd_check_func);
914
915 err = queue_delayed_work(tpd_esd_check_workqueue, &tpd_esd_check_work,ESD_CHECK_CIRCLE);
916 #endif
917
918 tpd_load_status = 1;
919
920 return 0;
921 }
922
923 static void tpd_eint_interrupt_handler(void)
924 {
925 //TPD_DMESG( "TPD interrupt!!!!!!\n" );
926 TPD_DEBUG_PRINT_INT;
927 tpd_flag=1;
928 wake_up_interruptible(&waiter);
929 }
930 static int tpd_i2c_remove(struct i2c_client *client)
931 {
932 #ifdef CREATE_WR_NODE
933 uninit_wr_node();
934 #endif
935
936 #ifdef ESD_PROTECT
937 destroy_workqueue(tpd_esd_check_workqueue);
938 #endif
939
940 return 0;
941 }
942
943 #ifdef ESD_PROTECT
944 #if 0
945 static force_reset_guitar()
946 {
947 int i;
948
949 for ( i = 0; i < 5; i++)
950 {
951 //Reset Guitar
952 mt_set_gpio_mode(GPIO_CTP_RST_PIN, GPIO_CTP_RST_PIN_M_GPIO);
953 mt_set_gpio_dir(GPIO_CTP_RST_PIN, GPIO_DIR_OUT);
954 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ZERO);
955 msleep(10);
956 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ONE);
957 msleep(20);
958 //Send config
959 if (tpd_init_panel())
960 {
961 continue;
962 }
963 i2c_enable_commands(i2c_client, TPD_I2C_DISABLE_REG);
964 break;
965 }
966 }
967
968 #endif
969 static int check_IC_sample(void) //
970 {
971 int err =0;
972 int i=0;
973 // int j =0;
974 u8 raw_buffer_curr[10] = {0};
975 u8 raw_buffer_last[10] = {0};
976 int check_count =0;
977 for(i =0; i<4; i++)
978 {
979 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_START_REG );
980 err = i2c_read_bytes( i2c_client, 0x880, raw_buffer_curr, 10 );
981 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_END_REG );
982 #if 0
983 printk( " raw_buffer_curr: \n");
984 for(j=0;j<10;j++)
985 {
986 printk( " %x ",raw_buffer_curr[j]);
987 }
988
989
990 printk( "\n raw_buffer_last: \n");
991 for(j=0;j<10;j++)
992 {
993 printk( " %x ",raw_buffer_last[j]);
994 }
995
996 #endif
997 if(0 == memcmp(raw_buffer_last,raw_buffer_curr,10))
998 {
999
1000 check_count++;
1001 TPD_DMESG( " check_count =%d \n",check_count);
1002 }
1003
1004 memcpy(raw_buffer_last,raw_buffer_curr,10);
1005 msleep(500);
1006 }
1007
1008 if(3 == check_count)
1009 {
1010 TPD_DMESG( "ic do not sample \n");
1011 return -1;
1012 }
1013
1014 return 0;
1015
1016 }
1017
1018 static void tpd_esd_check_func(struct work_struct *work)
1019 {
1020 //int ret = -1;
1021 int i;
1022 int err = 0;
1023 TPD_DMESG( "tpd_esd_check_func++\n");
1024 mutex_lock(&esd_check);
1025 if (tpd_halt)
1026 {
1027 mutex_unlock(&esd_check);
1028 TPD_DMESG( "tpd_esd_check_func return ..\n");
1029 return;
1030 }
1031 //do not print error msg if ack error
1032 i2c_client->addr = i2c_client->addr | I2C_A_FILTER_MSG;
1033
1034 //check raw data if IC do not sample reset touch IC
1035 err = check_IC_sample();
1036 if(err)
1037 {
1038 TPD_DMESG( "IC do not sample \n");
1039 tpd_reset_fuc(i2c_client);
1040 }
1041 for (i = 0; i < 3; i++)
1042 {
1043 memset( &tpd_info, 0, sizeof( struct tpd_info_t ) );
1044 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_START_REG );
1045 err = i2c_read_bytes( i2c_client, TPD_VERSION_INFO_REG, (u8 *)&tpd_info, sizeof( struct tpd_info_t ) );
1046 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_END_REG );
1047
1048 if (err)
1049 {
1050 continue;
1051 }
1052
1053 break;
1054 }
1055
1056 if (i >= 3)
1057 {
1058 tpd_reset_fuc(i2c_client);
1059 }
1060
1061
1062 if(tpd_halt)
1063 {
1064 mt_eint_mask(CUST_EINT_TOUCH_PANEL_NUM);
1065 }
1066 else
1067 {
1068 queue_delayed_work(tpd_esd_check_workqueue, &tpd_esd_check_work, ESD_CHECK_CIRCLE);
1069 }
1070
1071 //do not print error msg if ack error
1072 i2c_client->addr = i2c_client->addr & (~I2C_A_FILTER_MSG);
1073 mutex_unlock(&esd_check);
1074 TPD_DMESG( "tpd_esd_check_func--\n");
1075 return;
1076 }
1077 #endif
1078
1079 static void tpd_down(int x, int y, int size, int id)
1080 {
1081 //TPD_DMESG( "TPD donw id=%d\n",id-1);
1082 input_report_abs(tpd->dev, ABS_PRESSURE, size/100);
1083 input_report_key(tpd->dev, BTN_TOUCH, 1);
1084 input_report_abs(tpd->dev, ABS_MT_TOUCH_MAJOR, size/100);
1085 input_report_abs(tpd->dev, ABS_MT_WIDTH_MAJOR, 0);
1086 input_report_abs(tpd->dev, ABS_MT_POSITION_X, x);
1087 input_report_abs(tpd->dev, ABS_MT_POSITION_Y, y);
1088 /* track id Start 0 */
1089 input_report_abs(tpd->dev, ABS_MT_TRACKING_ID, id-1);
1090 input_mt_sync(tpd->dev);
1091 //TPD_DEBUG_PRINT_POINT( x, y, 1 );
1092 TPD_EM_PRINT(x, y, x, y, id-1, 1);
1093
1094 if (FACTORY_BOOT == get_boot_mode()|| RECOVERY_BOOT == get_boot_mode())
1095 {
1096 tpd_button(x, y, 1);
1097 }
1098 }
1099
1100 static void tpd_up(int x, int y, int id)
1101 {
1102 //TPD_DMESG( "TPD up id=%d\n",id-1);
1103
1104 //input_report_abs(tpd->dev, ABS_PRESSURE, 0);
1105 input_report_key(tpd->dev, BTN_TOUCH, 0);
1106 //input_report_abs(tpd->dev, ABS_MT_TOUCH_MAJOR, 0);
1107 //input_report_abs(tpd->dev, ABS_MT_WIDTH_MAJOR, 0);
1108 //input_report_abs(tpd->dev, ABS_MT_POSITION_X, x);
1109 //input_report_abs(tpd->dev, ABS_MT_POSITION_Y, y);
1110 /* track id Start 0 */
1111 //input_report_abs(tpd->dev, ABS_MT_TRACKING_ID, id-1);
1112 input_mt_sync(tpd->dev);
1113 TPD_EM_PRINT(x, y, x, y, id, 0);
1114
1115 if (FACTORY_BOOT == get_boot_mode()|| RECOVERY_BOOT == get_boot_mode())
1116 {
1117 tpd_button(x, y, 0);
1118 }
1119 //TPD_DEBUG_PRINT_POINT( x, y, 0 );
1120 }
1121
1122 #ifdef HAVE_TOUCH_KEY
1123 static void tpd_down2(int x, int y)
1124 {
1125 input_report_abs(tpd->dev, ABS_PRESSURE, 8);
1126 input_report_key(tpd->dev, BTN_TOUCH, 1);
1127 input_report_abs(tpd->dev, ABS_MT_TOUCH_MAJOR, 8);
1128 input_report_abs(tpd->dev, ABS_MT_POSITION_X, x);
1129 input_report_abs(tpd->dev, ABS_MT_POSITION_Y, y);
1130 input_mt_sync(tpd->dev);
1131 //TPD_DMESG( "tpd_down2 \n");
1132 if (FACTORY_BOOT == get_boot_mode()|| RECOVERY_BOOT == get_boot_mode())
1133 {
1134 //TPD_DMESG( "tpd_down2 button \n");
1135 tpd_button(x, y, 1);
1136 }
1137 input_sync(tpd->dev);
1138
1139
1140 }
1141
1142 static void tpd_up2(int x, int y)
1143 {
1144 // input_report_abs(tpd->dev, ABS_PRESSURE, 0);
1145 input_report_key(tpd->dev, BTN_TOUCH, 0);
1146 //input_report_abs(tpd->dev, ABS_MT_TOUCH_MAJOR, 0);
1147 input_mt_sync(tpd->dev);
1148 //TPD_DMESG( "tpd_up2 \n");
1149 if (FACTORY_BOOT == get_boot_mode()|| RECOVERY_BOOT == get_boot_mode())
1150 {
1151 //TPD_DMESG( "tpd_up2 button\n");
1152 tpd_button(x, y, 0);
1153 }
1154 input_sync(tpd->dev);
1155
1156 }
1157
1158 #endif
1159 static int touch_event_handler(void *unused)
1160 {
1161 struct sched_param param = { .sched_priority = RTPM_PRIO_TPD };
1162 int x, y, size, finger_num = 0;
1163 int wrap_x,wrap_y=0;
1164 int id=0;
1165 static u8 buffer[ TPD_POINT_INFO_LEN*TPD_MAX_POINTS ];
1166 static char buf_status;
1167 //static u8 id_mask = 0;
1168 u8 cur_mask;
1169 int i =0;
1170 char temp_data[CONFIG_LEN] = {0};
1171 //int err=0;
1172 int idx;
1173 static int x_history[TPD_MAX_POINTS+1];
1174 static int y_history[TPD_MAX_POINTS+1];
1175
1176 #ifdef HAVE_TOUCH_KEY
1177 static u8 last_key = 0;
1178 u8 key = 0;
1179 unsigned int count = 0;
1180 #endif
1181
1182 #ifdef TPD_CONDITION_SWITCH
1183 u8 charger_plug = 0;
1184 u8 *cfg;
1185 u32 temp;
1186 #endif
1187
1188 sched_setscheduler(current, SCHED_RR, &param);
1189
1190 do
1191 {
1192 set_current_state(TASK_INTERRUPTIBLE);
1193
1194 while ( tpd_halt )
1195 {
1196 tpd_flag = 0;
1197 msleep(20);
1198 }
1199
1200 wait_event_interruptible(waiter, tpd_flag != 0);
1201 tpd_flag = 0;
1202 TPD_DEBUG_SET_TIME;
1203 set_current_state(TASK_RUNNING);
1204
1205 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_START_REG );
1206
1207 #ifdef TPD_CONDITION_SWITCH
1208 /* Workaround for common mode noise */
1209
1210 temp = upmu_is_chr_det();
1211 //TPD_DMESG("check charge, status:%d \n", upmu_is_chr_det());
1212
1213
1214 cfg = NULL;
1215
1216 if ( temp ) // charger is on
1217 {
1218 if ( charger_plug == 0 )
1219 {
1220 TPD_DEBUG("update configure for charger\n");
1221 //TPD_DMESG("update configure for charger\n");
1222 charger_plug = 1;
1223 cfg = cfg_data_with_charger;
1224 }
1225 }
1226 else
1227 {
1228 if ( charger_plug == 1 )
1229 {
1230 TPD_DEBUG("update configure for no charger\n");
1231 //TPD_DMESG("update configure for no charger\n");
1232 charger_plug = 0;
1233 cfg = cfg_data;
1234 }
1235 }
1236
1237 if ( cfg )
1238 {
1239 TPD_DMESG("[mtk-tpd] charger change rewrite config \n");
1240 i2c_write_bytes( i2c_client, TPD_CONFIG_REG_BASE, cfg, CONFIG_LEN );
1241 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_END_REG );
1242 continue;
1243 }
1244 #endif
1245
1246 i2c_read_bytes( i2c_client, TPD_TOUCH_INFO_REG_BASE, buffer, 1);
1247 TPD_DEBUG("[mtk-tpd] STATUS : %x\n", buffer[0]);
1248 #ifdef HAVE_TOUCH_KEY
1249 i2c_read_bytes( i2c_client, TPD_KEY_INFO_REG_BASE, &key, 1);
1250 TPD_DEBUG("[mtk-tpd] STATUS : %x\n", key);
1251
1252 key = key&0x0f;
1253 #endif
1254 finger_num = buffer[0] & 0x0f;
1255 buf_status = buffer[0] & 0xf0;
1256
1257 if ( tpd == NULL || tpd->dev == NULL )
1258 {
1259 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_END_REG ); //kuuga add 11082401
1260 TPD_DEBUG("[mtk-tpd] tpd=%x, tpd->dev=%x\n",tpd, tpd->dev );
1261 continue;
1262 }
1263
1264
1265 if ( 0x0f == buffer[0] ) // send cfg again!!
1266 {
1267 TPD_DMESG("[mtk-tpd] STATUS error : %x\n", buffer[0]);
1268 TPD_DMESG("[mtk-tpd] dumpt error config: \n");
1269
1270 //i2c_write_dummy( i2c_client, TPD_HANDSHAKING_START_REG );
1271 i2c_read_bytes( i2c_client, TPD_CONFIG_REG_BASE, temp_data, CONFIG_LEN);
1272 //i2c_write_dummy( i2c_client, TPD_HANDSHAKING_END_REG );
1273 for ( i = 0 ; i < CONFIG_LEN ; i++ )
1274 {
1275 printk( "0x%02X ", temp_data[i] );
1276 if ( i%8 == 7 )
1277 printk("\n" );
1278 }
1279
1280 TPD_DMESG("[mtk-tpd] dumpt error config done \n");
1281
1282 tpd_reset_fuc(i2c_client);
1283
1284 TPD_DMESG("[mtk-tpd] dumpt config again : \n");
1285 //i2c_write_dummy( i2c_client, TPD_HANDSHAKING_START_REG );
1286 i2c_read_bytes( i2c_client, TPD_CONFIG_REG_BASE, temp_data, CONFIG_LEN);
1287 //i2c_write_dummy( i2c_client, TPD_HANDSHAKING_END_REG );
1288 for ( i = 0 ; i < CONFIG_LEN ; i++ )
1289 {
1290 printk( "0x%02X ", temp_data[i] );
1291 if ( i%8 == 7 )
1292 printk("\n" );
1293 }
1294 TPD_DMESG("[mtk-tpd] dumpt config done \n");
1295 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_END_REG );
1296 continue;
1297 }
1298
1299 if ( (buf_status&0x30) != 0x20 ) //data not ready£¬so return
1300 {
1301 TPD_DMESG("[mtk-tpd] STATUS : %x\n", buffer[0]);
1302 TPD_DMESG("[mtk-tpd] data not ready return \n");
1303 // tpd_reset_fuc(i2c_client);
1304 //i2c_write_dummy( i2c_client, TPD_HANDSHAKING_END_REG ); //kuuga add 11082401
1305 continue;
1306 }
1307
1308 if ( finger_num > 5 ) //abnormal state £¬so return
1309 {
1310 TPD_DMESG("[mtk-tpd] finger_num =%d abnormal state !\n",finger_num);
1311 TPD_DMESG("[mtk-tpd] STATUS : %x\n", buffer[0]);
1312 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_END_REG ); //kuuga add 11082401
1313 continue;
1314 }
1315 if ( finger_num )
1316 {
1317 i2c_read_bytes( i2c_client, TPD_POINT_INFO_REG_BASE, buffer, finger_num*TPD_POINT_INFO_LEN);
1318
1319 }
1320 else
1321 {
1322 //In order to release INT
1323 i2c_read_bytes( i2c_client, TPD_POINT_INFO_REG_BASE, buffer, 1);
1324 }
1325
1326 cur_mask = 0;
1327
1328 for ( idx = 0 ; idx < finger_num ; idx++ )
1329 {
1330 u8 *ptr = &buffer[ idx*TPD_POINT_INFO_LEN ];
1331 id = ptr[0];
1332
1333 if ( id < TPD_MAX_POINTS+1 )
1334 {
1335 x = ptr[1] + (((int)ptr[2]) << 8);
1336 y = ptr[3] + (((int)ptr[4]) << 8);
1337 size = ptr[5] + (((int)ptr[6]) << 8);
1338
1339 wrap_x = TPD_WARP_X(x);
1340 wrap_y = TPD_WARP_Y(y);
1341
1342 if(TPD_X_RES== TPD_WARP_X(x))
1343 {
1344 wrap_x = wrap_x-1;
1345 }
1346 if(0==TPD_WARP_X(x))
1347 {
1348 wrap_x=wrap_x+1;
1349 }
1350 if(0==TPD_WARP_Y(y))
1351 {
1352 wrap_y = wrap_y+1;
1353 }
1354 if(TPD_Y_RES==TPD_WARP_Y(y))
1355 {
1356 wrap_y=wrap_y-1;
1357 }
1358
1359 tpd_down( wrap_x, wrap_y, size, id);
1360
1361 cur_mask |= ( 1 << id );
1362 x_history[id] = x;
1363 y_history[id] = y;
1364 }
1365 else
1366 TPD_DEBUG("Invalid id %d\n", id );
1367 }
1368 #if 0
1369 // linux kernel update from 2.6.35 --> 3.0
1370 if ( cur_mask != id_mask )
1371 {
1372 u8 diff = cur_mask^id_mask;
1373 idx = 0;
1374 //TPD_DMESG("diff= %x\n", diff );
1375 //TPD_DMESG("cur_mask= %d\n", cur_mask );
1376
1377 while ( diff )
1378 {
1379 if ( ( ( diff & 0x01 ) == 1 ) &&
1380 ( ( cur_mask >> idx ) & 0x01 ) == 0 )
1381 {
1382 // check if key release
1383 tpd_up( TPD_WARP_X(x_history[idx]), TPD_WARP_Y(y_history[idx]), idx);
1384 }
1385
1386 diff = ( diff >> 1 );
1387 idx++;
1388 }
1389 id_mask = cur_mask;
1390 }
1391
1392
1393 if ( tpd != NULL && tpd->dev != NULL )
1394 input_sync(tpd->dev);
1395 #endif
1396
1397 #ifdef HAVE_TOUCH_KEY
1398 if((last_key == 0)&&(key == 0))
1399 ;
1400 else
1401 {
1402 for(count = 0; count < 4; count++)
1403 {
1404 //input_report_key(tpd->dev, touch_key_array[count], !!(key&(0x01<<count)));
1405 if( key&(0x01<<count) ) //key=1 menu ;key=2 home; key =4 back; key=8 search
1406 {
1407 wrap_x =touch_key_point_maping_array[count].point_x;
1408 wrap_y = touch_key_point_maping_array[count].point_y;
1409 TPD_DMESG("button =%d %d\n",wrap_x,wrap_y);
1410
1411 tpd_down2( wrap_x, wrap_y);
1412 }
1413 }
1414 if((last_key!=0)&&(key ==0))
1415 {
1416 tpd_up2( 0, 0);
1417 }
1418 }
1419
1420
1421 #endif
1422 // linux kernel update from 2.6.35 --> 3.0
1423 if(finger_num)
1424 {
1425 if ( tpd != NULL && tpd->dev != NULL )
1426 input_sync(tpd->dev);
1427 }
1428 else
1429 {
1430 #ifdef HAVE_TOUCH_KEY
1431
1432 if ( tpd != NULL && tpd->dev != NULL && 0==key && 0==last_key )
1433 {
1434 //input_mt_sync(tpd->dev);
1435
1436 last_key = key;
1437
1438 tpd_up( TPD_WARP_X(x_history[idx]), TPD_WARP_Y(y_history[idx]), idx);
1439 input_sync(tpd->dev);
1440 }
1441 #else
1442 if ( tpd != NULL && tpd->dev != NULL )
1443 {
1444 //input_mt_sync(tpd->dev);
1445 tpd_up( TPD_WARP_X(x_history[idx]), TPD_WARP_Y(y_history[idx]), idx);
1446 input_sync(tpd->dev);
1447 }
1448
1449 #endif
1450 }
1451
1452
1453 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_END_REG );
1454
1455 } while ( !kthread_should_stop() );
1456
1457 return 0;
1458 }
1459
1460 static int tpd_local_init(void)
1461 {
1462
1463 if(i2c_add_driver(&tpd_i2c_driver)!=0)
1464 {
1465 TPD_DMESG("unable to add i2c driver.\n");
1466 return -1;
1467 }
1468 if(tpd_load_status == 0) //if(tpd_load_status == 0) // disable auto load touch driver for linux3.0 porting
1469 {
1470 TPD_DMESG("add error touch panel driver.\n");
1471 i2c_del_driver(&tpd_i2c_driver);
1472 return -1;
1473 }
1474
1475 #ifdef TPD_HAVE_BUTTON
1476 tpd_button_setting(TPD_KEY_COUNT, tpd_keys_local, tpd_keys_dim_local);// initialize tpd button data
1477 #endif
1478
1479 #if (defined(TPD_WARP_START) && defined(TPD_WARP_END))
1480 TPD_DO_WARP = 1;
1481 memcpy(tpd_wb_start, tpd_wb_start_local, TPD_WARP_CNT*4);
1482 memcpy(tpd_wb_end, tpd_wb_start_local, TPD_WARP_CNT*4);
1483 #endif
1484
1485 #if (defined(TPD_HAVE_CALIBRATION) && !defined(TPD_CUSTOM_CALIBRATION))
1486 memcpy(tpd_calmat, tpd_def_calmat_local, 8*4);
1487 memcpy(tpd_def_calmat, tpd_def_calmat_local, 8*4);
1488 #endif
1489
1490 // set vendor string
1491 tpd->dev->id.vendor = (tpd_info.vendor_id_2 << 8 ) | tpd_info.vendor_id_1;
1492 tpd->dev->id.product = (tpd_info.product_id_2 << 8 ) | tpd_info.product_id_1;
1493 tpd->dev->id.version = (tpd_info.version_2 << 8 ) | tpd_info.version_1;
1494
1495 TPD_DMESG("end %s, %d\n", __FUNCTION__, __LINE__);
1496 tpd_type_cap = 1;
1497
1498 return 0;
1499 }
1500
1501 /* Function to manage low power suspend */
1502 //void tpd_suspend(struct i2c_client *client, pm_message_t message)
1503 static void tpd_suspend( struct early_suspend *h )
1504 {
1505 u8 mode = 0x01;
1506 TPD_DMESG("tpd_suspend\n");
1507 mutex_lock(&esd_check);
1508 #if 0 // workaround for force tpd into sleep mode
1509 mt_set_gpio_mode(GPIO_CTP_EINT_PIN, GPIO_CTP_EINT_PIN_M_GPIO);
1510 mt_set_gpio_dir(GPIO_CTP_EINT_PIN, GPIO_DIR_OUT);
1511 mt_set_gpio_out(GPIO_CTP_EINT_PIN, GPIO_OUT_ZERO);
1512 #endif
1513 tpd_halt = 1;
1514 #ifdef ESD_PROTECT
1515 cancel_delayed_work(&tpd_esd_check_work);
1516 #endif
1517
1518 msleep(1);
1519 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_START_REG );
1520 i2c_write_bytes( i2c_client, TPD_POWER_MODE_REG, &mode, 1 );
1521 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_END_REG );
1522 mt_eint_mask(CUST_EINT_TOUCH_PANEL_NUM);
1523 //mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ZERO);
1524 //return 0;
1525 mutex_unlock(&esd_check);
1526 TPD_DMESG("tpd_suspend ok\n");
1527 }
1528
1529 /* Function to manage power-on resume */
1530 //void tpd_resume(struct i2c_client *client)
1531 static void tpd_resume( struct early_suspend *h )
1532 {
1533 #ifdef TPD_RESET_ISSUE_WORKAROUND
1534 struct tpd_info_t tpd_info;
1535 int err;
1536
1537 #endif
1538 TPD_DMESG(TPD_DEVICE " tpd_resume start \n");
1539 #ifdef TPD_RESET_ISSUE_WORKAROUND
1540
1541 #if 1
1542 // use raising edge of INT to wakeup
1543 mt_set_gpio_mode(GPIO_CTP_EINT_PIN, GPIO_CTP_EINT_PIN_M_GPIO);
1544 mt_set_gpio_dir(GPIO_CTP_EINT_PIN, GPIO_DIR_OUT);
1545 mt_set_gpio_out(GPIO_CTP_EINT_PIN, GPIO_OUT_ZERO);
1546 msleep(1);
1547 mt_set_gpio_out(GPIO_CTP_EINT_PIN, GPIO_OUT_ONE);
1548 msleep(1);
1549 mt_set_gpio_out(GPIO_CTP_EINT_PIN, GPIO_OUT_ZERO);
1550 msleep(1);
1551 mt_set_gpio_mode(GPIO_CTP_EINT_PIN, GPIO_CTP_EINT_PIN_M_EINT);
1552 mt_set_gpio_dir(GPIO_CTP_EINT_PIN, GPIO_DIR_IN);
1553 #endif
1554 #if 0
1555 // set INT mode
1556 mt_set_gpio_mode(GPIO_CTP_EINT_PIN, GPIO_CTP_EINT_PIN_M_GPIO);
1557 mt_set_gpio_dir(GPIO_CTP_EINT_PIN, GPIO_DIR_IN);
1558 mt_set_gpio_pull_enable(GPIO_CTP_EINT_PIN, GPIO_PULL_DISABLE);
1559
1560 // reset
1561 mt_set_gpio_mode(GPIO_CTP_RST_PIN, GPIO_CTP_RST_PIN_M_GPIO);
1562 mt_set_gpio_dir(GPIO_CTP_RST_PIN, GPIO_DIR_OUT);
1563 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ZERO);
1564 msleep(1);
1565
1566 mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ONE);
1567 msleep(20);// msleep(20)
1568
1569 // set INT mode
1570 mt_set_gpio_mode(GPIO_CTP_EINT_PIN, GPIO_CTP_EINT_PIN_M_EINT);
1571 mt_set_gpio_dir(GPIO_CTP_EINT_PIN, GPIO_DIR_IN);
1572 mt_set_gpio_pull_enable(GPIO_CTP_EINT_PIN, GPIO_PULL_DISABLE);
1573 //mt_set_gpio_pull_select(GPIO_CTP_EINT_PIN, GPIO_PULL_UP);
1574 #endif
1575 #endif
1576
1577 mt_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM);
1578 //mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ONE);
1579
1580 #ifdef TPD_RESET_ISSUE_WORKAROUND
1581
1582 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_START_REG );
1583 //TODO: Remove the code after the stabliity test is passed
1584 memset( &tpd_info, 0, sizeof( struct tpd_info_t ) );
1585 err = i2c_read_bytes( i2c_client, TPD_VERSION_INFO_REG, (u8 *)&tpd_info, sizeof( struct tpd_info_t ) );
1586
1587 if ( err )
1588 {
1589 TPD_DMESG(TPD_DEVICE " fail to get tpd info %d\n", err );
1590 tpd_reset_fuc(i2c_client);
1591 }
1592 else
1593 {
1594 TPD_DMESG( "TPD info\n");
1595 TPD_DMESG( "vendor %02X %02X\n", tpd_info.vendor_id_1, tpd_info.vendor_id_2 );
1596 TPD_DMESG( "product %02X %02X\n", tpd_info.product_id_1, tpd_info.product_id_2 );
1597 TPD_DMESG( "version %02X %02X\n", tpd_info.version_1, tpd_info.version_2 );
1598 }
1599
1600 i2c_write_dummy( i2c_client, TPD_HANDSHAKING_END_REG );
1601 #endif
1602 tpd_halt = 0;
1603 #ifdef ESD_PROTECT
1604 queue_delayed_work(tpd_esd_check_workqueue, &tpd_esd_check_work,ESD_CHECK_CIRCLE);
1605 #endif
1606
1607 TPD_DMESG(TPD_DEVICE " tpd_resume end \n" );
1608 //return 0;
1609 }
1610
1611 static struct tpd_driver_t tpd_device_driver =
1612 {
1613 .tpd_device_name = "gt818",
1614 .tpd_local_init = tpd_local_init,
1615 .suspend = tpd_suspend,
1616 .resume = tpd_resume,
1617 #ifdef TPD_HAVE_BUTTON
1618 .tpd_have_button = 1,
1619 #else
1620 .tpd_have_button = 0,
1621 #endif
1622 };
1623
1624 /* called when loaded into kernel */
1625 static int __init tpd_driver_init(void)
1626 {
1627 TPD_DMESG("MediaTek gt818 touch panel driver init\n");
1628 i2c_register_board_info(0, &i2c_tpd, 1);
1629 if ( tpd_driver_add(&tpd_device_driver) < 0)
1630 TPD_DMESG("add generic driver failed\n");
1631
1632 return 0;
1633 }
1634
1635 /* should never be called */
1636 static void __exit tpd_driver_exit(void)
1637 {
1638 TPD_DMESG("MediaTek gt818 touch panel driver exit\n");
1639 //input_unregister_device(tpd->dev);
1640 tpd_driver_remove(&tpd_device_driver);
1641 }
1642
1643 module_init(tpd_driver_init);
1644 module_exit(tpd_driver_exit);
1645