1 /*
2 * drivers/input/touchscreen/gslX680.c
3 *
4 * Copyright (c) 2012 Shanghai Basewin
5 * Guan Yuwei<guanyuwei@basewin.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12
13 #include <linux/module.h>
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/hrtimer.h>
17 #include <linux/i2c.h>
18 #include <linux/input.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/platform_device.h>
22 #include <linux/async.h>
23 #include <linux/irq.h>
24 #include <linux/uaccess.h>
25 #include <linux/workqueue.h>
26 #include <linux/proc_fs.h>
27 #include <linux/input/mt.h>
28
29 #include <linux/version.h>
30 #include <linux/slab.h>
31 #include <linux/of_gpio.h>
32 #include "tp_suspend.h"
33
34 #include "gslx680_pad.h"
35 #include <linux/wakelock.h>
36 //#define GSL_DEBUG
37 #define REPORT_DATA_ANDROID_4_0
38 //#define SLEEP_CLEAR_POINT
39 //#define FILTER_POINT
40 #ifdef FILTER_POINT
41 #define FILTER_MAX 9
42 #endif
43
44 #define GSLX680_I2C_NAME "gslX680-pad"
45 #define GSLX680_I2C_ADDR 0x40
46
47 int g_wake_pin=0;
48 int g_irq_pin=0;
49
50 #define GSL_DATA_REG 0x80
51 #define GSL_STATUS_REG 0xe0
52 #define GSL_PAGE_REG 0xf0
53
54 #define TPD_PROC_DEBUG
55 #ifdef TPD_PROC_DEBUG
56 #include <linux/proc_fs.h>
57 #include <linux/uaccess.h>
58 #include <linux/seq_file.h> //lzk
59 //static struct proc_dir_entry *gsl_config_proc = NULL;
60 #define GSL_CONFIG_PROC_FILE "gsl_config"
61 #define CONFIG_LEN 31
62 static char gsl_read[CONFIG_LEN];
63 static u8 gsl_data_proc[8] = {0};
64 static u8 gsl_proc_flag = 0;
65
66 #endif
67
68 #define PRESS_MAX 255
69 #define MAX_FINGERS 10
70 #define MAX_CONTACTS 10
71 #define DMA_TRANS_LEN 0x20
72 static struct i2c_client *gsl_client = NULL;
73
74 struct gsl_ts_data {
75 u8 x_index;
76 u8 y_index;
77 u8 z_index;
78 u8 id_index;
79 u8 touch_index;
80 u8 data_reg;
81 u8 status_reg;
82 u8 data_size;
83 u8 touch_bytes;
84 u8 update_data;
85 u8 touch_meta_data;
86 u8 finger_size;
87 };
88
89 static struct gsl_ts_data devices[] = {
90 {
91 .x_index = 6,
92 .y_index = 4,
93 .z_index = 5,
94 .id_index = 7,
95 .data_reg = GSL_DATA_REG,
96 .status_reg = GSL_STATUS_REG,
97 .update_data = 0x4,
98 .touch_bytes = 4,
99 .touch_meta_data = 4,
100 .finger_size = 70,
101 },
102 };
103
104 struct gsl_ts {
105 struct i2c_client *client;
106 struct input_dev *input;
107 struct work_struct work;
108 struct workqueue_struct *wq;
109 struct gsl_ts_data *dd;
110 u8 *touch_data;
111 u8 device_id;
112 int irq;
113 int irq_pin;
114 int wake_pin;
115 struct tp_device tp;
116 int screen_max_x;
117 int screen_max_y;
118 struct gsl_touch_chip_info *gsl_chip_info;
119 struct work_struct download_fw_work;
120 struct work_struct resume_work;
121 };
122
123 #ifdef GSL_DEBUG
124 #define print_info(fmt, args...) \
125 do{ \
126 printk(fmt, ##args); \
127 }while(0)
128 #else
129 #define print_info(fmt, args...)
130 #endif
131
132
133 static u32 id_sign[MAX_CONTACTS+1] = {0};
134 static u8 id_state_flag[MAX_CONTACTS+1] = {0};
135 static u8 id_state_old_flag[MAX_CONTACTS+1] = {0};
136 static u16 x_old[MAX_CONTACTS+1] = {0};
137 static u16 y_old[MAX_CONTACTS+1] = {0};
138 static u16 x_new = 0;
139 static u16 y_new = 0;
140
141 static struct gsl_touch_chip_info gsl_chip_info[] = {
142 {GSL680,GSLX680_FW,ARRAY_SIZE(GSLX680_FW),gsl_config_data_id,ARRAY_SIZE(gsl_config_data_id)},
143 {GSL3676,GSL3675B_FW_HK,ARRAY_SIZE(GSL3675B_FW_HK),gsl3678_config_data_id,ARRAY_SIZE(gsl3678_config_data_id)},
144 };
145
146
gslX680_init(void)147 static int gslX680_init(void)
148 {
149 gpio_set_value(g_wake_pin,1);
150 gpio_set_value(g_irq_pin,1);
151 return 0;
152 }
153
gslX680_shutdown_low(void)154 static int gslX680_shutdown_low(void)
155 {
156 if(g_wake_pin !=0)
157 {
158 gpio_direction_output(g_wake_pin, 0);
159 gpio_set_value(g_wake_pin,0);
160 }
161 return 0;
162 }
163
gslX680_shutdown_high(void)164 static int gslX680_shutdown_high(void)
165 {
166 if(g_wake_pin !=0)
167 {
168 gpio_direction_output(g_wake_pin, 0);
169 gpio_set_value(g_wake_pin,1);
170 }
171 return 0;
172 }
173
join_bytes(u8 a,u8 b)174 static inline u16 join_bytes(u8 a, u8 b)
175 {
176 u16 ab = 0;
177 ab = ab | a;
178 ab = ab << 8 | b;
179 return ab;
180 }
181
gsl_write_interface(struct i2c_client * client,const u8 reg,u8 * buf,u32 num)182 static u32 gsl_write_interface(struct i2c_client *client, const u8 reg, u8 *buf, u32 num)
183 {
184 struct i2c_msg xfer_msg[1];
185
186 buf[0] = reg;
187
188 xfer_msg[0].addr = client->addr;
189 xfer_msg[0].len = num + 1;
190 xfer_msg[0].flags = client->flags & I2C_M_TEN;
191 xfer_msg[0].buf = buf;
192 //xfer_msg[0].scl_rate = 400*1000; //RK3066 RK2926 I2C±¨´íʱ´ò¿ªÕâ¸ö
193
194 return i2c_transfer(client->adapter, xfer_msg, 1) == 1 ? 0 : -EFAULT;
195 }
196
gsl_ts_write(struct i2c_client * client,u8 addr,u8 * pdata,int datalen)197 static int gsl_ts_write(struct i2c_client *client, u8 addr, u8 *pdata, int datalen)
198 {
199 int ret = 0;
200 u8 tmp_buf[128];
201 unsigned int bytelen = 0;
202 if (datalen > 125)
203 {
204 dev_err(&client->dev,"%s too big datalen = %d!\n", __func__, datalen);
205 return -1;
206 }
207
208 tmp_buf[0] = addr;
209 bytelen++;
210
211 if (datalen != 0 && pdata != NULL)
212 {
213 memcpy(&tmp_buf[bytelen], pdata, datalen);
214 bytelen += datalen;
215 }
216
217 ret = i2c_master_send(client, tmp_buf, bytelen);
218 return ret;
219 }
220
gsl_ts_read(struct i2c_client * client,u8 addr,u8 * pdata,unsigned int datalen)221 static int gsl_ts_read(struct i2c_client *client, u8 addr, u8 *pdata, unsigned int datalen)
222 {
223 int ret = 0;
224
225 if (datalen > 126)
226 {
227 dev_err(&client->dev, "%s too big datalen = %d!\n", __func__, datalen);
228 return -1;
229 }
230
231 ret = gsl_ts_write(client, addr, NULL, 0);
232 if (ret < 0)
233 {
234 dev_err(&client->dev, "%s set data address fail!\n", __func__);
235 return ret;
236 }
237
238 return i2c_master_recv(client, pdata, datalen);
239 }
240
fw2buf(u8 * buf,const u32 * fw)241 static __inline__ void fw2buf(u8 *buf, const u32 *fw)
242 {
243 u32 *u32_buf = (int *)buf;
244 *u32_buf = *fw;
245 }
246
gsl_load_fw(struct i2c_client * client)247 static void gsl_load_fw(struct i2c_client *client)
248 {
249 u8 buf[DMA_TRANS_LEN*4 + 1] = {0};
250 u8 send_flag = 1;
251 u8 *cur = buf + 1;
252 u32 source_line = 0;
253 u32 source_len;
254 struct gsl_ts *ts = i2c_get_clientdata(gsl_client);
255 struct fw_data *ptr_fw;
256
257 ptr_fw = ts->gsl_chip_info->ptr_fw;
258
259 source_len = ts->gsl_chip_info->ptr_fw_len;
260 for (source_line = 0; source_line < source_len; source_line++)
261 {
262 /* init page trans, set the page val */
263 if (GSL_PAGE_REG == ptr_fw[source_line].offset)
264 {
265 fw2buf(cur, &ptr_fw[source_line].val);
266 gsl_write_interface(client, GSL_PAGE_REG, buf, 4);
267 //i2c_smbus_write_i2c_block_data(client, GSL_PAGE_REG,4, buf);
268 send_flag = 1;
269 }
270 else
271 {
272 if (1 == send_flag % (DMA_TRANS_LEN < 0x20 ? DMA_TRANS_LEN : 0x20))
273 buf[0] = (u8)ptr_fw[source_line].offset;
274
275 fw2buf(cur, &ptr_fw[source_line].val);
276 cur += 4;
277
278 if (0 == send_flag % (DMA_TRANS_LEN < 0x20 ? DMA_TRANS_LEN : 0x20))
279 {
280 gsl_write_interface(client, buf[0], buf, cur - buf - 1);
281 //i2c_smbus_write_i2c_block_data(client, buf[0], cur - buf - 1, buf);
282 cur = buf + 1;
283 }
284
285 send_flag++;
286 }
287 }
288 }
289
test_i2c(struct i2c_client * client)290 static int test_i2c(struct i2c_client *client)
291 {
292 u8 read_buf = 0;
293 u8 write_buf = 0x12;
294 int ret, rc = 1;
295
296 ret = gsl_ts_read( client, 0xf0, &read_buf, sizeof(read_buf) );
297 if (ret < 0)
298 rc --;
299 else
300 dev_info(&client->dev, "I read reg 0xf0 is %x\n", read_buf);
301
302 mdelay(2);
303 ret = gsl_ts_write(client, 0xf0, &write_buf, sizeof(write_buf));
304 if(ret >= 0 )
305 dev_info(&client->dev, "I write reg 0xf0 0x12\n");
306
307 mdelay(2);
308 ret = gsl_ts_read( client, 0xf0, &read_buf, sizeof(read_buf) );
309 if(ret < 0 )
310 rc --;
311 else
312 dev_info(&client->dev, "I read reg 0xf0 is 0x%x\n", read_buf);
313
314 return rc;
315 }
316
317
startup_chip(struct i2c_client * client)318 static void startup_chip(struct i2c_client *client)
319 {
320 u8 tmp = 0x00;
321
322 #ifdef GSL_NOID_VERSION
323 struct gsl_ts *ts = i2c_get_clientdata(client);
324 gsl_DataInit(ts->gsl_chip_info->conf_in);
325 #endif
326 gsl_ts_write(client, 0xe0, &tmp, 1);
327 }
328
reset_chip(struct i2c_client * client)329 static void reset_chip(struct i2c_client *client)
330 {
331 u8 tmp = 0x88;
332 u8 buf[4] = {0x00};
333
334 gsl_ts_write(client, 0xe0, &tmp, sizeof(tmp));
335 mdelay(5);
336 tmp = 0x04;
337 gsl_ts_write(client, 0xe4, &tmp, sizeof(tmp));
338 mdelay(5);
339 gsl_ts_write(client, 0xbc, buf, sizeof(buf));
340 }
341
clr_reg(struct i2c_client * client)342 static void clr_reg(struct i2c_client *client)
343 {
344 u8 write_buf[4] = {0};
345
346 write_buf[0] = 0x88;
347 gsl_ts_write(client, 0xe0, &write_buf[0], 1);
348 msleep(20);
349 write_buf[0] = 0x03;
350 gsl_ts_write(client, 0x80, &write_buf[0], 1);
351 msleep(5);
352 write_buf[0] = 0x04;
353 gsl_ts_write(client, 0xe4, &write_buf[0], 1);
354 msleep(5);
355 write_buf[0] = 0x00;
356 gsl_ts_write(client, 0xe0, &write_buf[0], 1);
357 msleep(20);
358 }
359
init_chip(struct i2c_client * client)360 static void init_chip(struct i2c_client *client)
361 {
362 int rc;
363 struct gsl_ts *ts = i2c_get_clientdata(client);
364
365 gslX680_shutdown_low();
366 mdelay(20);
367 gslX680_shutdown_high();
368 gpio_set_value(g_irq_pin,1);
369 msleep(20);
370 rc = test_i2c(client);
371 if(rc < 0)
372 {
373 dev_err(&client->dev, "------gslX680 test_i2c error------\n");
374 return;
375 }
376 queue_work(ts->wq, &ts->download_fw_work);
377 }
378
check_mem_data(struct i2c_client * client)379 static void check_mem_data(struct i2c_client *client)
380 {
381 u8 read_buf[4] = {0};
382
383 gsl_ts_read(client,0xb0, read_buf, sizeof(read_buf));
384
385 if (read_buf[3] != 0x5a || read_buf[2] != 0x5a || read_buf[1] != 0x5a || read_buf[0] != 0x5a)
386 {
387 dev_info(&client->dev, "#########check mem read 0xb0 = %x %x %x %x #########\n", read_buf[3], read_buf[2], read_buf[1], read_buf[0]);
388 init_chip(client);
389 }
390 }
391
392
393 #ifdef TPD_PROC_DEBUG
char_to_int(char ch)394 static int char_to_int(char ch)
395 {
396 if(ch>='0' && ch<='9')
397 return (ch-'0');
398 else
399 return (ch-'a'+10);
400 }
401
402
gsl_read_interface(struct i2c_client * client,u8 reg,u8 * buf,u32 num)403 static int gsl_read_interface(struct i2c_client *client, u8 reg, u8 *buf, u32 num)
404 {
405
406 int err = 0;
407 u8 temp = reg;
408 //mutex_lock(&gsl_i2c_lock);
409 if(temp < 0x80)
410 {
411 temp = (temp+8)&0x5c;
412 i2c_master_send(client,&temp,1);
413 err = i2c_master_recv(client,&buf[0],4);
414
415 temp = reg;
416 i2c_master_send(client,&temp,1);
417 err = i2c_master_recv(client,&buf[0],4);
418 }
419 i2c_master_send(client,®,1);
420 err = i2c_master_recv(client,&buf[0],num);
421 //mutex_unlock(&gsl_i2c_lock);
422 return err;
423 }
424
gsl_config_read_proc(struct seq_file * m,void * v)425 static int gsl_config_read_proc(struct seq_file *m,void *v)
426 {
427 //char *ptr = page;
428 char temp_data[5] = {0};
429 unsigned int tmp=0;
430
431 if('v'==gsl_read[0]&&'s'==gsl_read[1])
432 {
433 #ifdef GSL_NOID_VERSION
434 tmp=gsl_version_id();
435 #else
436 tmp=0x20121215;
437 #endif
438 seq_printf(m,"version:%x\n",tmp);
439 }
440 else if('r'==gsl_read[0]&&'e'==gsl_read[1])
441 {
442 if('i'==gsl_read[3])
443 {
444 #ifdef GSL_NOID_VERSION
445 tmp=(gsl_data_proc[5]<<8) | gsl_data_proc[4];
446 #endif
447 }
448 else
449 {
450 gsl_ts_write(gsl_client,0xf0,&gsl_data_proc[4],4);
451 gsl_read_interface(gsl_client,gsl_data_proc[0],temp_data,4);
452 seq_printf(m,"offset : {0x%02x,0x",gsl_data_proc[0]);
453 seq_printf(m,"%02x",temp_data[3]);
454 seq_printf(m,"%02x",temp_data[2]);
455 seq_printf(m,"%02x",temp_data[1]);
456 seq_printf(m,"%02x};\n",temp_data[0]);
457 }
458 }
459 return 0;
460 }
461
gsl_config_write_proc(struct file * file,const char * buffer,size_t count,loff_t * data)462 ssize_t gsl_config_write_proc(struct file *file, const char *buffer, size_t count, loff_t *data)
463 {
464 u8 buf[8] = {0};
465 char temp_buf[CONFIG_LEN];
466 char *path_buf;
467 int tmp = 0;
468 int tmp1 = 0;
469 struct gsl_ts *ts = i2c_get_clientdata(gsl_client);
470
471 print_info("[tp-gsl][%s] \n",__func__);
472 if(count > 512)
473 {
474 pr_err("size not match [%d:%ld]\n", CONFIG_LEN, count);
475 return -EFAULT;
476 }
477 path_buf=kzalloc(count,GFP_KERNEL);
478 if(!path_buf)
479 {
480 pr_err("alloc path_buf memory error \n");
481 }
482 if(copy_from_user(path_buf, buffer, count))
483 {
484 pr_err("copy from user fail\n");
485 goto exit_write_proc_out;
486 }
487 memcpy(temp_buf,path_buf,(count<CONFIG_LEN?count:CONFIG_LEN));
488
489 buf[3]=char_to_int(temp_buf[14])<<4 | char_to_int(temp_buf[15]);
490 buf[2]=char_to_int(temp_buf[16])<<4 | char_to_int(temp_buf[17]);
491 buf[1]=char_to_int(temp_buf[18])<<4 | char_to_int(temp_buf[19]);
492 buf[0]=char_to_int(temp_buf[20])<<4 | char_to_int(temp_buf[21]);
493
494 buf[7]=char_to_int(temp_buf[5])<<4 | char_to_int(temp_buf[6]);
495 buf[6]=char_to_int(temp_buf[7])<<4 | char_to_int(temp_buf[8]);
496 buf[5]=char_to_int(temp_buf[9])<<4 | char_to_int(temp_buf[10]);
497 buf[4]=char_to_int(temp_buf[11])<<4 | char_to_int(temp_buf[12]);
498 if('v'==temp_buf[0]&& 's'==temp_buf[1])//version //vs
499 {
500 memcpy(gsl_read,temp_buf,4);
501 }
502 else if('s'==temp_buf[0]&& 't'==temp_buf[1])//start //st
503 {
504 gsl_proc_flag = 1;
505 reset_chip(gsl_client);
506 }
507 else if('e'==temp_buf[0]&&'n'==temp_buf[1])//end //en
508 {
509 msleep(20);
510 reset_chip(gsl_client);
511 startup_chip(gsl_client);
512 gsl_proc_flag = 0;
513 }
514 else if('r'==temp_buf[0]&&'e'==temp_buf[1])//read buf //
515 {
516 memcpy(gsl_read,temp_buf,4);
517 memcpy(gsl_data_proc,buf,8);
518 }
519 else if('w'==temp_buf[0]&&'r'==temp_buf[1])//write buf
520 {
521 gsl_ts_write(gsl_client,buf[4],buf,4);
522 }
523 #ifdef GSL_NOID_VERSION
524 else if('i'==temp_buf[0]&&'d'==temp_buf[1])//write id config //
525 {
526 tmp1=(buf[7]<<24)|(buf[6]<<16)|(buf[5]<<8)|buf[4];
527 tmp=(buf[3]<<24)|(buf[2]<<16)|(buf[1]<<8)|buf[0];
528 if(tmp1>=0 && tmp1<ts->gsl_chip_info->conf_in_len)
529 {
530 ts->gsl_chip_info->conf_in[tmp1] = tmp;
531 }
532 }
533 #endif
534 exit_write_proc_out:
535 kfree(path_buf);
536 return count;
537 }
538
gsl_server_list_open(struct inode * inode,struct file * file)539 static int gsl_server_list_open(struct inode *inode,struct file *file)
540 {
541 return single_open(file,gsl_config_read_proc,NULL);
542 }
543 static const struct proc_ops gsl_seq_fops = {
544 .proc_open = gsl_server_list_open,
545 .proc_read = seq_read,
546 .proc_release = single_release,
547 .proc_write = gsl_config_write_proc,
548 };
549 #endif
550
551 #ifdef FILTER_POINT
filter_point(u16 x,u16 y,u8 id)552 static void filter_point(u16 x, u16 y , u8 id)
553 {
554 u16 x_err =0;
555 u16 y_err =0;
556 u16 filter_step_x = 0, filter_step_y = 0;
557
558 id_sign[id] = id_sign[id] + 1;
559 if(id_sign[id] == 1)
560 {
561 x_old[id] = x;
562 y_old[id] = y;
563 }
564
565 x_err = x > x_old[id] ? (x -x_old[id]) : (x_old[id] - x);
566 y_err = y > y_old[id] ? (y -y_old[id]) : (y_old[id] - y);
567
568 if( (x_err > FILTER_MAX && y_err > FILTER_MAX/3) || (x_err > FILTER_MAX/3 && y_err > FILTER_MAX) )
569 {
570 filter_step_x = x_err;
571 filter_step_y = y_err;
572 }
573 else
574 {
575 if(x_err > FILTER_MAX)
576 filter_step_x = x_err;
577 if(y_err> FILTER_MAX)
578 filter_step_y = y_err;
579 }
580
581 if(x_err <= 2*FILTER_MAX && y_err <= 2*FILTER_MAX)
582 {
583 filter_step_x >>= 2;
584 filter_step_y >>= 2;
585 }
586 else if(x_err <= 3*FILTER_MAX && y_err <= 3*FILTER_MAX)
587 {
588 filter_step_x >>= 1;
589 filter_step_y >>= 1;
590 }
591 else if(x_err <= 4*FILTER_MAX && y_err <= 4*FILTER_MAX)
592 {
593 filter_step_x = filter_step_x*3/4;
594 filter_step_y = filter_step_y*3/4;
595 }
596
597 x_new = x > x_old[id] ? (x_old[id] + filter_step_x) : (x_old[id] - filter_step_x);
598 y_new = y > y_old[id] ? (y_old[id] + filter_step_y) : (y_old[id] - filter_step_y);
599
600 x_old[id] = x_new;
601 y_old[id] = y_new;
602 }
603 #else
record_point(u16 x,u16 y,u8 id)604 static void record_point(u16 x, u16 y , u8 id)
605 {
606 u16 x_err =0;
607 u16 y_err =0;
608 return;
609 id_sign[id]=id_sign[id]+1;
610
611 if(id_sign[id]==1){
612 x_old[id]=x;
613 y_old[id]=y;
614 }
615
616 x = (x_old[id] + x)/2;
617 y = (y_old[id] + y)/2;
618
619 if(x>x_old[id]){
620 x_err=x -x_old[id];
621 }
622 else{
623 x_err=x_old[id]-x;
624 }
625
626 if(y>y_old[id]){
627 y_err=y -y_old[id];
628 }
629 else{
630 y_err=y_old[id]-y;
631 }
632
633 if( (x_err > 3 && y_err > 1) || (x_err > 1 && y_err > 3) ){
634 x_new = x; x_old[id] = x;
635 y_new = y; y_old[id] = y;
636 }
637 else{
638 if(x_err > 3){
639 x_new = x; x_old[id] = x;
640 }
641 else
642 x_new = x_old[id];
643 if(y_err> 3){
644 y_new = y; y_old[id] = y;
645 }
646 else
647 y_new = y_old[id];
648 }
649
650 if(id_sign[id]==1){
651 x_new= x_old[id];
652 y_new= y_old[id];
653 }
654
655 }
656 #endif
657
report_data(struct gsl_ts * ts,u16 x,u16 y,u8 pressure,u8 id)658 static void report_data(struct gsl_ts *ts, u16 x, u16 y, u8 pressure, u8 id)
659 {
660
661 if(revert_xy)
662 swap(x, y);
663
664 if(x > ts->screen_max_x || y > ts->screen_max_y)
665 {
666 return;
667 }
668
669 if(revert_x)
670 x = ts->screen_max_x-x;
671 if(revert_y) {
672 y = ts->screen_max_y-y;
673 }
674
675 #ifdef REPORT_DATA_ANDROID_4_0
676 input_mt_slot(ts->input, id);
677 input_report_abs(ts->input, ABS_MT_TRACKING_ID, id);
678 input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, pressure);
679 input_report_abs(ts->input, ABS_MT_POSITION_X, x);
680 input_report_abs(ts->input, ABS_MT_POSITION_Y, y);
681 input_report_abs(ts->input, ABS_MT_WIDTH_MAJOR, 1);
682 #else
683 input_report_abs(ts->input, ABS_MT_TRACKING_ID, id);
684 input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, pressure);
685 input_report_abs(ts->input, ABS_MT_POSITION_X,x);
686 input_report_abs(ts->input, ABS_MT_POSITION_Y, y);
687 input_report_abs(ts->input, ABS_MT_WIDTH_MAJOR, 1);
688 input_mt_sync(ts->input);
689 #endif
690 }
691
gslX680_ts_worker(struct work_struct * work)692 static void gslX680_ts_worker(struct work_struct *work)
693 {
694 int rc, i;
695 u8 id, touches;
696 u16 x, y;
697 #ifdef GSL_NOID_VERSION
698 unsigned int tmp1;
699 u8 buf[4] = {0};
700 struct gsl_touch_info cinfo = {{0}};
701 #endif
702 struct gsl_ts *ts = container_of(work, struct gsl_ts,work);
703
704 #ifdef TPD_PROC_DEBUG
705 if(gsl_proc_flag == 1)
706 goto schedule;
707 #endif
708
709 rc = gsl_ts_read(ts->client, 0x80, ts->touch_data, ts->dd->data_size);
710 if (rc < 0)
711 {
712 dev_err(&ts->client->dev, "read failed\n");
713 reset_chip(ts->client);
714 startup_chip(ts->client);
715 goto schedule;
716 }
717
718 touches = ts->touch_data[ts->dd->touch_index];
719 #ifdef GSL_NOID_VERSION
720 cinfo.finger_num = touches;
721 for(i = 0; i < (touches < MAX_CONTACTS ? touches : MAX_CONTACTS); i ++)
722 {
723 cinfo.x[i] = join_bytes( ( ts->touch_data[ts->dd->x_index + 4 * i + 1] & 0xf),
724 ts->touch_data[ts->dd->x_index + 4 * i]);
725 cinfo.y[i] = join_bytes(ts->touch_data[ts->dd->y_index + 4 * i + 1],
726 ts->touch_data[ts->dd->y_index + 4 * i ]);
727 cinfo.id[i] = ((ts->touch_data[ts->dd->x_index + 4 * i + 1] & 0xf0)>>4);
728 }
729 cinfo.finger_num=(ts->touch_data[3]<<24)|(ts->touch_data[2]<<16)
730 |(ts->touch_data[1]<<8)|(ts->touch_data[0]);
731 gsl_alg_id_main(&cinfo);
732 tmp1=gsl_mask_tiaoping();
733 if(tmp1>0&&tmp1<0xffffffff)
734 {
735 buf[0]=0xa;buf[1]=0;buf[2]=0;buf[3]=0;
736 gsl_ts_write(ts->client,0xf0,buf,4);
737 buf[0]=(u8)(tmp1 & 0xff);
738 buf[1]=(u8)((tmp1>>8) & 0xff);
739 buf[2]=(u8)((tmp1>>16) & 0xff);
740 buf[3]=(u8)((tmp1>>24) & 0xff);
741 gsl_ts_write(ts->client,0x8,buf,4);
742 }
743 touches = cinfo.finger_num;
744 #endif
745
746 for(i = 1; i <= MAX_CONTACTS; i ++)
747 {
748 if(touches == 0)
749 id_sign[i] = 0;
750 id_state_flag[i] = 0;
751 }
752 for(i= 0;i < (touches > MAX_FINGERS ? MAX_FINGERS : touches);i ++)
753 {
754 #ifdef GSL_NOID_VERSION
755 {
756 id = cinfo.id[i];
757 x = cinfo.x[i];
758 y = cinfo.y[i];
759 }
760 #else
761 {
762 x = join_bytes( ( ts->touch_data[ts->dd->x_index + 4 * i + 1] & 0xf),
763 ts->touch_data[ts->dd->x_index + 4 * i]);
764 y = join_bytes(ts->touch_data[ts->dd->y_index + 4 * i + 1],
765 ts->touch_data[ts->dd->y_index + 4 * i ]);
766 id = ts->touch_data[ts->dd->id_index + 4 * i] >> 4;
767 }
768 #endif
769 if(1 <=id && id <= MAX_CONTACTS)
770 {
771 #ifdef FILTER_POINT
772 filter_point(x, y ,id);
773 #else
774 record_point(x, y , id);
775 #endif
776 //report_data(ts, x_new, y_new, 10, id);
777 report_data(ts, x, y, 10, id);
778
779 id_state_flag[id] = 1;
780 }
781 }
782 for(i = 1; i <= MAX_CONTACTS; i ++)
783 {
784 if( (0 == touches) || ((0 != id_state_old_flag[i]) && (0 == id_state_flag[i])) )
785 {
786 #ifdef REPORT_DATA_ANDROID_4_0
787 input_mt_slot(ts->input, i);
788 input_report_abs(ts->input, ABS_MT_TRACKING_ID, -1);
789 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, false);
790 #endif
791 id_sign[i]=0;
792 }
793 id_state_old_flag[i] = id_state_flag[i];
794 }
795 if(0 == touches)
796 {
797 #ifndef REPORT_DATA_ANDROID_4_0
798 input_mt_sync(ts->input);
799 #endif
800 }
801 input_sync(ts->input);
802
803 schedule:
804 enable_irq(ts->irq);
805
806 }
807
gsl_ts_irq(int irq,void * dev_id)808 static irqreturn_t gsl_ts_irq(int irq, void *dev_id)
809 {
810 struct gsl_ts *ts = dev_id;
811
812 disable_irq_nosync(ts->irq);
813
814 if (!work_pending(&ts->work))
815 {
816 queue_work(ts->wq, &ts->work);
817 }
818
819 return IRQ_HANDLED;
820
821 }
822
gslX680_ts_init(struct i2c_client * client,struct gsl_ts * ts)823 static int gslX680_ts_init(struct i2c_client *client, struct gsl_ts *ts)
824 {
825 struct input_dev *input_device;
826 int rc = 0;
827
828 ts->dd = &devices[ts->device_id];
829
830 if (ts->device_id == 0) {
831 ts->dd->data_size = MAX_FINGERS * ts->dd->touch_bytes + ts->dd->touch_meta_data;
832 ts->dd->touch_index = 0;
833 }
834
835 ts->touch_data = kzalloc(ts->dd->data_size, GFP_KERNEL);
836 if (!ts->touch_data) {
837 pr_err("%s: Unable to allocate memory\n", __func__);
838 return -ENOMEM;
839 }
840
841 input_device = input_allocate_device();
842 if (!input_device) {
843 rc = -ENOMEM;
844 goto error_alloc_dev;
845 }
846
847 ts->input = input_device;
848 input_device->name = GSLX680_I2C_NAME;
849 input_device->id.bustype = BUS_I2C;
850 input_device->dev.parent = &client->dev;
851 input_set_drvdata(input_device, ts);
852
853 #ifdef REPORT_DATA_ANDROID_4_0
854 __set_bit(EV_ABS, input_device->evbit);
855 __set_bit(EV_KEY, input_device->evbit);
856 __set_bit(EV_REP, input_device->evbit);
857 __set_bit(INPUT_PROP_DIRECT, input_device->propbit);
858 input_mt_init_slots(input_device, (MAX_CONTACTS+1),0);
859 #else
860 input_set_abs_params(input_device,ABS_MT_TRACKING_ID, 0, (MAX_CONTACTS+1), 0, 0);
861 set_bit(EV_ABS, input_device->evbit);
862 set_bit(EV_KEY, input_device->evbit);
863 __set_bit(INPUT_PROP_DIRECT, input_device->propbit);
864 input_device->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
865 #endif
866
867 set_bit(ABS_MT_POSITION_X, input_device->absbit);
868 set_bit(ABS_MT_POSITION_Y, input_device->absbit);
869 set_bit(ABS_MT_TOUCH_MAJOR, input_device->absbit);
870 set_bit(ABS_MT_WIDTH_MAJOR, input_device->absbit);
871
872 input_set_abs_params(input_device,ABS_MT_POSITION_X, 0, ts->screen_max_x, 0, 0);
873 input_set_abs_params(input_device,ABS_MT_POSITION_Y, 0, ts->screen_max_y, 0, 0);
874 input_set_abs_params(input_device,ABS_MT_TOUCH_MAJOR, 0, PRESS_MAX, 0, 0);
875 input_set_abs_params(input_device,ABS_MT_WIDTH_MAJOR, 0, 200, 0, 0);
876
877 ts->wq = create_singlethread_workqueue("kworkqueue_ts");
878 if (!ts->wq) {
879 dev_err(&client->dev, "Could not create workqueue\n");
880 goto error_wq_create;
881 }
882 flush_workqueue(ts->wq);
883 INIT_WORK(&ts->work, gslX680_ts_worker);
884 rc = input_register_device(input_device);
885 if (rc)
886 goto error_unreg_device;
887 return 0;
888
889 error_unreg_device:
890 destroy_workqueue(ts->wq);
891 error_wq_create:
892 input_free_device(input_device);
893 error_alloc_dev:
894 kfree(ts->touch_data);
895 return rc;
896 }
897
gsl_ts_suspend(struct device * dev)898 static int gsl_ts_suspend(struct device *dev)
899 {
900 struct gsl_ts *ts = dev_get_drvdata(dev);
901 #ifdef SLEEP_CLEAR_POINT
902 #ifdef REPORT_DATA_ANDROID_4_0
903 int i;
904 #endif
905 #endif
906
907 disable_irq_nosync(ts->irq);
908 gslX680_shutdown_low();
909
910 #ifdef SLEEP_CLEAR_POINT
911 msleep(10);
912 #ifdef REPORT_DATA_ANDROID_4_0
913 for(i = 1; i <= MAX_CONTACTS ;i ++)
914 {
915 input_mt_slot(ts->input, i);
916 input_report_abs(ts->input, ABS_MT_TRACKING_ID, -1);
917 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, false);
918 }
919 #else
920 input_mt_sync(ts->input);
921 #endif
922 input_sync(ts->input);
923 msleep(10);
924 report_data(ts, 1, 1, 10, 1);
925 input_sync(ts->input);
926 #endif
927
928 return 0;
929 }
930
gsl_ts_resume(struct device * dev)931 static int gsl_ts_resume(struct device *dev)
932 {
933 struct gsl_ts *ts = dev_get_drvdata(dev);
934
935 queue_work(ts->wq, &ts->resume_work);
936
937 return 0;
938 }
939
gsl_ts_early_suspend(struct tp_device * tp_d)940 static int gsl_ts_early_suspend(struct tp_device *tp_d)
941 {
942 struct gsl_ts *ts = container_of(tp_d, struct gsl_ts, tp);
943
944 gsl_ts_suspend(&ts->client->dev);
945
946 return 0;
947 }
948
gsl_ts_late_resume(struct tp_device * tp_d)949 static int gsl_ts_late_resume(struct tp_device *tp_d)
950 {
951 struct gsl_ts *ts = container_of(tp_d, struct gsl_ts, tp);
952
953 gsl_ts_resume(&ts->client->dev);
954
955 return 0;
956 }
957
gsl_download_fw_work(struct work_struct * work)958 static void gsl_download_fw_work(struct work_struct *work)
959 {
960 struct gsl_ts *ts = dev_get_drvdata(&gsl_client->dev);
961
962 clr_reg(ts->client);
963 reset_chip(ts->client);
964 gsl_load_fw(ts->client);
965 startup_chip(ts->client);
966 reset_chip(ts->client);
967 startup_chip(ts->client);
968 }
969
gsl_resume_work(struct work_struct * work)970 static void gsl_resume_work(struct work_struct *work)
971 {
972 struct gsl_ts *ts = dev_get_drvdata(&gsl_client->dev);
973 #ifdef SLEEP_CLEAR_POINT
974 #ifdef REPORT_DATA_ANDROID_4_0
975 int i;
976 #endif
977 #endif
978 gslX680_shutdown_high();
979 msleep(20);
980 //reset_chip(ts->client);
981 //startup_chip(ts->client);
982 check_mem_data(ts->client);
983 check_mem_data(ts->client);
984
985 #ifdef SLEEP_CLEAR_POINT
986 #ifdef REPORT_DATA_ANDROID_4_0
987 for(i =1;i<=MAX_CONTACTS;i++)
988 {
989 input_mt_slot(ts->input, i);
990 input_report_abs(ts->input, ABS_MT_TRACKING_ID, -1);
991 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, false);
992 }
993 #else
994 input_mt_sync(ts->input);
995 #endif
996 input_sync(ts->input);
997 #endif
998 enable_irq(ts->irq);
999 }
1000
gsl_ts_probe(struct i2c_client * client,const struct i2c_device_id * id)1001 static int gsl_ts_probe(struct i2c_client *client,
1002 const struct i2c_device_id *id)
1003 {
1004 struct device_node *np = client->dev.of_node;
1005 enum of_gpio_flags wake_flags, irq_flags;
1006 struct gsl_ts *ts;
1007 int rc;
1008 int gsl_chip_id = 0;
1009 int i,ret;
1010
1011 printk("GSLX680 Enter %s\n", __func__);
1012
1013 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1014 dev_err(&client->dev, "I2C functionality not supported\n");
1015 return -ENODEV;
1016 }
1017
1018 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
1019 if (!ts)
1020 return -ENOMEM;
1021
1022 ts->client = client;
1023 i2c_set_clientdata(client, ts);
1024 ts->device_id = id->driver_data;
1025
1026
1027 of_property_read_u32(np,"screen_max_x",&(ts->screen_max_x));
1028 of_property_read_u32(np,"screen_max_y",&(ts->screen_max_y));
1029
1030 dev_info(&ts->client->dev, "[tp-gsl] screen_max_x =[%d] \n",ts->screen_max_x);
1031 dev_info(&ts->client->dev, "[tp-gsl] screen_max_y =[%d] \n",ts->screen_max_y);
1032
1033 of_property_read_u32(np, "revert_x", &revert_x);
1034 of_property_read_u32(np, "revert_y", &revert_y);
1035 of_property_read_u32(np, "revert_xy", &revert_xy);
1036
1037 dev_info(&ts->client->dev, "[tp-gsl] revert_x =[%d] \n",revert_x);
1038 dev_info(&ts->client->dev, "[tp-gsl] revert_y =[%d] \n",revert_y);
1039 dev_info(&ts->client->dev, "[tp-gsl] revert_xy =[%d] \n",revert_xy);
1040
1041 ts->irq_pin=of_get_named_gpio_flags(np, "touch-gpio", 0, &irq_flags);
1042 ts->wake_pin=of_get_named_gpio_flags(np, "reset-gpio", 0, &wake_flags);
1043
1044 ret = of_property_read_u32(np, "chip_id", &gsl_chip_id);
1045 if (ret)
1046 gsl_chip_id = GSL680;
1047
1048 dev_info(&ts->client->dev, "[tp-gsl] gsl_chip_id =[%d] \n",gsl_chip_id);
1049 for (i=0; i<ARRAY_SIZE(gsl_chip_info); i++) {
1050 if (gsl_chip_info[i].chip_id == gsl_chip_id) {
1051 ts->gsl_chip_info = &gsl_chip_info[i];
1052 break;
1053 }
1054 }
1055
1056 if (gpio_is_valid(ts->wake_pin)) {
1057 rc = devm_gpio_request_one(&ts->client->dev, ts->wake_pin, (wake_flags & OF_GPIO_ACTIVE_LOW) ? GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH, "gslX680 wake pin");
1058 if (rc != 0) {
1059 dev_err(&ts->client->dev, "gslX680 wake pin error\n");
1060 return -EIO;
1061 }
1062 g_wake_pin = ts->wake_pin;
1063 } else {
1064 dev_info(&ts->client->dev, "wake pin invalid\n");
1065 }
1066 if (gpio_is_valid(ts->irq_pin)) {
1067 rc = devm_gpio_request_one(&ts->client->dev, ts->irq_pin, (irq_flags & OF_GPIO_ACTIVE_LOW) ? GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH, "gslX680 irq pin");
1068 if (rc != 0) {
1069 dev_err(&ts->client->dev, "gslX680 irq pin error\n");
1070 return -EIO;
1071 }
1072 g_irq_pin = ts->irq_pin;
1073 } else {
1074 dev_info(&ts->client->dev, "irq pin invalid\n");
1075 }
1076
1077 INIT_WORK(&ts->download_fw_work, gsl_download_fw_work);
1078 INIT_WORK(&ts->resume_work, gsl_resume_work);
1079
1080 gslX680_init();
1081 rc = gslX680_ts_init(client, ts);
1082 if (rc < 0) {
1083 dev_err(&client->dev, "GSLX680 init failed\n");
1084 goto error_mutex_destroy;
1085 }
1086
1087 gsl_client = client;
1088 init_chip(ts->client);
1089 check_mem_data(ts->client);
1090
1091 ts->irq=gpio_to_irq(ts->irq_pin); //If not defined in client
1092 if (ts->irq)
1093 {
1094 rc = devm_request_threaded_irq(&client->dev, ts->irq, NULL, gsl_ts_irq, irq_flags | IRQF_ONESHOT, client->name, ts);
1095 if (rc != 0) {
1096 dev_err(&client->dev, "Cannot allocate ts INT!ERRNO:%d\n", rc);
1097 goto error_req_irq_fail;
1098 }
1099 disable_irq(ts->irq);
1100 }
1101 else
1102 {
1103 dev_err(&client->dev, "gsl x680 irq req fail\n");
1104 goto error_req_irq_fail;
1105 }
1106
1107 /* create debug attribute */
1108 #ifdef TPD_PROC_DEBUG
1109 proc_create(GSL_CONFIG_PROC_FILE,0666,NULL,&gsl_seq_fops);
1110 gsl_proc_flag = 0;
1111 #endif
1112
1113 gpio_set_value(ts->irq_pin, 0);
1114 enable_irq(ts->irq);
1115
1116 ts->tp.tp_resume = gsl_ts_late_resume;
1117 ts->tp.tp_suspend = gsl_ts_early_suspend;
1118 tp_register_fb(&ts->tp);
1119
1120 return 0;
1121
1122 //exit_set_irq_mode:
1123 error_req_irq_fail:
1124 free_irq(ts->irq, ts);
1125
1126 error_mutex_destroy:
1127 input_free_device(ts->input);
1128 kfree(ts);
1129 return rc;
1130 }
1131
gsl_ts_remove(struct i2c_client * client)1132 static int gsl_ts_remove(struct i2c_client *client)
1133 {
1134 struct gsl_ts *ts = i2c_get_clientdata(client);
1135
1136 device_init_wakeup(&client->dev, 0);
1137 cancel_work_sync(&ts->work);
1138 free_irq(ts->irq, ts);
1139 destroy_workqueue(ts->wq);
1140 input_unregister_device(ts->input);
1141
1142 kfree(ts->touch_data);
1143 kfree(ts);
1144
1145 return 0;
1146 }
1147
1148 static struct of_device_id gsl_ts_ids[] = {
1149 {.compatible = "gslX680-pad"},
1150 {}
1151 };
1152
1153 static const struct i2c_device_id gsl_ts_id[] = {
1154 {GSLX680_I2C_NAME, 0},
1155 {}
1156 };
1157 MODULE_DEVICE_TABLE(i2c, gsl_ts_id);
1158
1159 #if !defined(CONFIG_DRM_FBDEV_EMULATION) && defined(CONFIG_PM)
1160 static const struct dev_pm_ops gsl_pm_ops = {
1161 .suspend = gsl_ts_suspend,
1162 .resume = gsl_ts_resume,
1163 };
1164 #endif
1165
1166 static struct i2c_driver gsl_ts_driver = {
1167 .driver = {
1168 .name = GSLX680_I2C_NAME,
1169 .owner = THIS_MODULE,
1170 .of_match_table = of_match_ptr(gsl_ts_ids),
1171 #if !defined(CONFIG_DRM_FBDEV_EMULATION) && defined(CONFIG_PM)
1172 .pm = &gsl_pm_ops,
1173 #endif
1174 },
1175 .probe = gsl_ts_probe,
1176 .remove = gsl_ts_remove,
1177 .id_table = gsl_ts_id,
1178 };
1179
gsl_ts_init(void)1180 static int __init gsl_ts_init(void)
1181 {
1182 int ret;
1183 ret = i2c_add_driver(&gsl_ts_driver);
1184 return ret;
1185 }
gsl_ts_exit(void)1186 static void __exit gsl_ts_exit(void)
1187 {
1188 i2c_del_driver(&gsl_ts_driver);
1189 return;
1190 }
1191
1192 module_init(gsl_ts_init);
1193 module_exit(gsl_ts_exit);
1194
1195 MODULE_LICENSE("GPL");
1196 MODULE_DESCRIPTION("GSLX680 touchscreen controller driver");
1197 MODULE_AUTHOR("Guan Yuwei, guanyuwei@basewin.com");
1198 MODULE_ALIAS("platform:gsl_ts");
1199