a29ba0af10a9dbf0e3e0a1da69c54056c816fb44
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / rockchip_gslX680_rk3128.c
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 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/delay.h>
15 #include <linux/hrtimer.h>
16 #include <linux/i2c.h>
17 #include <linux/input.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/platform_device.h>
21 #include <linux/async.h>
22 #include <linux/irq.h>
23 #include <linux/workqueue.h>
24 #include <linux/proc_fs.h>
25 #include <linux/input/mt.h>
26 #include "rockchip_gslX680_rk3128.h"
27 #include <linux/of_gpio.h>
28 #include <linux/gpio.h>
29 #include <linux/slab.h>
30 #include <linux/device.h>
31 #include "tp_suspend.h"
32
33 #define GPIO_HIGH 1
34 #define GPIO_LOW 0
35
36 /* #define GSL_DEBUG */
37 /* #define GSL_TIMER */
38 #define REPORT_DATA_ANDROID_4_0
39
40 /* #define HAVE_TOUCH_KEY */
41
42 #define GSLX680_I2C_NAME "gslX680"
43 #define GSLX680_I2C_ADDR 0x40
44
45 /* #define IRQ_PORT                     RK2928_PIN1_PB0 */
46 /* #define WAKE_PORT                    RK2928_PIN0_PD3 */
47
48 #define GSL_DATA_REG            0x80
49 #define GSL_STATUS_REG          0xe0
50 #define GSL_PAGE_REG            0xf0
51
52 #define PRESS_MAX       255
53 #define MAX_FINGERS     5
54 #define MAX_CONTACTS    10
55 #define DMA_TRANS_LEN   0x20
56 /* #define FILTER_POINT */
57 #ifdef FILTER_POINT
58 #define FILTER_MAX      6
59 #endif
60
61 #define WRITE_I2C_SPEED (350*1000)
62 #define I2C_SPEED  (200*1000)
63
64
65
66 #define CLOSE_TP_POWER  0
67
68 #if CLOSE_TP_POWER
69 static void set_tp_power(bool flag);
70 #endif
71
72 #ifdef HAVE_TOUCH_KEY
73 static u16 key;
74 static int key_state_flag;
75
76
77 struct key_data {
78         u16 key;
79         u16 x_min;
80         u16 x_max;
81         u16 y_min;
82         u16 y_max;
83 };
84
85 const u16 key_array[] = {
86         KEY_BACK,
87         KEY_HOME,
88         KEY_MENU,
89         KEY_SEARCH,
90         };
91
92 #define MAX_KEY_NUM     (sizeof(key_array)/sizeof(key_array[0]))
93
94 struct key_data gsl_key_data[MAX_KEY_NUM] = {
95         {KEY_BACK, 2048, 2048, 2048, 2048},
96         {KEY_HOME, 2048, 2048, 2048, 2048},
97         {KEY_MENU, 2048, 2048, 2048, 2048},
98         {KEY_SEARCH, 2048, 2048, 2048, 2048},
99 };
100 #endif
101
102 struct gsl_ts_data {
103         u8 x_index;
104         u8 y_index;
105         u8 z_index;
106         u8 id_index;
107         u8 touch_index;
108         u8 data_reg;
109         u8 status_reg;
110         u8 data_size;
111         u8 touch_bytes;
112         u8 update_data;
113         u8 touch_meta_data;
114         u8 finger_size;
115 };
116
117 static struct gsl_ts_data devices[] = {
118         {
119                 .x_index = 6,
120                 .y_index = 4,
121                 .z_index = 5,
122                 .id_index = 7,
123                 .data_reg = GSL_DATA_REG,
124                 .status_reg = GSL_STATUS_REG,
125                 .update_data = 0x4,
126                 .touch_bytes = 4,
127                 .touch_meta_data = 4,
128                 .finger_size = 70,
129         },
130 };
131
132 struct gsl_ts {
133         struct i2c_client *client;
134         struct input_dev *input;
135         struct work_struct work;
136         struct workqueue_struct *wq;
137         struct gsl_ts_data *dd;
138         u8 *touch_data;
139         u8 device_id;
140         int irq;
141 #if defined(CONFIG_HAS_EARLYSUSPEND)
142         struct early_suspend early_suspend;
143 #endif
144 #ifdef GSL_TIMER
145         struct timer_list gsl_timer;
146 #endif
147         int reset_gpio;
148 };
149
150 static struct gsl_ts *g_gsl_ts;
151
152 #ifdef GSL_DEBUG
153 #define print_info(fmt, args...) printk(fmt, ##args)
154 #else
155 #define print_info(fmt, args...)
156 #endif
157
158 static int ts_global_reset_pin;
159
160 static u32 id_sign[MAX_CONTACTS+1] = {0};
161 static u8 id_state_flag[MAX_CONTACTS+1] = {0};
162 static u8 id_state_old_flag[MAX_CONTACTS+1] = {0};
163 static u16 x_old[MAX_CONTACTS+1] = {0};
164 static u16 y_old[MAX_CONTACTS+1] = {0};
165 static u16 x_new;
166 static u16 y_new;
167
168 int i2c_master_normal_send(const struct i2c_client *client, const char *buf, int count, int scl_rate)
169 {
170         int ret;
171         struct i2c_adapter *adap = client->adapter;
172         struct i2c_msg msg;
173
174         msg.addr = client->addr;
175         msg.flags = client->flags;
176         msg.len = count;
177         msg.buf = (char *)buf;
178         msg.scl_rate = scl_rate;
179         ret = i2c_transfer(adap, &msg, 1);
180         return (ret == 1) ? count : ret;
181 }
182
183 int i2c_master_normal_recv(const struct i2c_client *client, char *buf, int count, int scl_rate)
184 {
185         struct i2c_adapter *adap = client->adapter;
186         struct i2c_msg msg;
187         int ret;
188
189         msg.addr = client->addr;
190         msg.flags = client->flags | I2C_M_RD;
191         msg.len = count;
192         msg.buf = (char *)buf;
193         msg.scl_rate = scl_rate;
194
195         ret = i2c_transfer(adap, &msg, 1);
196
197         return (ret == 1) ? count : ret;
198 }
199
200 static int gslX680_shutdown_low(void)
201 {
202         gpio_direction_output(ts_global_reset_pin, GPIO_LOW);
203         gpio_set_value(ts_global_reset_pin, GPIO_LOW);
204         return 0;
205 }
206
207 static int gslX680_shutdown_high(void)
208 {
209         gpio_direction_output(ts_global_reset_pin, GPIO_HIGH);
210         gpio_set_value(ts_global_reset_pin, GPIO_HIGH);
211         return 0;
212 }
213 static inline u16 join_bytes(u8 a, u8 b)
214 {
215         u16 ab = 0;
216
217         ab = ab | a;
218         ab = ab << 8 | b;
219         return ab;
220 }
221
222 static u32 gsl_write_interface(struct i2c_client *client, const u8 reg, u8 *buf, u32 num)
223 {
224         struct i2c_msg xfer_msg[1];
225
226         buf[0] = reg;
227
228         xfer_msg[0].addr = client->addr;
229         xfer_msg[0].len = num + 1;
230         xfer_msg[0].flags = client->flags & I2C_M_TEN;
231         xfer_msg[0].buf = buf;
232         xfer_msg[0].scl_rate = WRITE_I2C_SPEED;
233
234         return i2c_transfer(client->adapter, xfer_msg, 1) == 1 ? 0 : -EFAULT;
235 }
236
237 static int gsl_ts_write(struct i2c_client *client, u8 addr, u8 *pdata, int datalen)
238 {
239         int ret = 0;
240         u8 tmp_buf[128];
241         unsigned int bytelen;
242
243         bytelen = 0;
244
245         if (datalen > 125) {
246                 printk("%s too big datalen = %d!\n", __func__, datalen);
247                 return -1;
248         }
249
250         tmp_buf[0] = addr;
251         bytelen++;
252         if (datalen != 0 && pdata != NULL) {
253                 memcpy(&tmp_buf[bytelen], pdata, datalen);
254                 bytelen += datalen;
255         }
256         ret = i2c_master_normal_send(client, tmp_buf, bytelen, I2C_SPEED);
257         return ret;
258 }
259
260 static int gsl_ts_read(struct i2c_client *client, u8 addr, u8 *pdata, unsigned int datalen)
261 {
262         int ret = 0;
263
264         if (datalen > 126) {
265                 printk("%s too big datalen = %d!\n", __func__, datalen);
266                 return -1;
267         }
268
269         ret = gsl_ts_write(client, addr, NULL, 0);
270         if (ret < 0) {
271                 printk("%s set data address fail!\n", __func__);
272                 return ret;
273         }
274         return i2c_master_normal_recv(client, pdata, datalen, I2C_SPEED);
275 }
276
277
278 static __inline__ void fw2buf(u8 *buf, const u32 *fw)
279 {
280         u32 *u32_buf = (int *)buf;
281         *u32_buf = *fw;
282 }
283
284 static void gsl_load_fw(struct i2c_client *client)
285 {
286         u8 buf[DMA_TRANS_LEN*4 + 1] = {0};
287         u8 send_flag = 1;
288         u8 *cur = buf + 1;
289         u32 source_line = 0;
290         u32 source_len;
291         u8 read_buf[4] = {0};
292         const struct fw_data *ptr_fw;
293         printk("=============gsl_load_fw start==============\n");
294
295 #ifdef GSL1680E_COMPATIBLE
296         msleep(50);
297         gsl_ts_read(client, 0xfc, read_buf, 4);
298
299
300         if (read_buf[2] != 0x82 && read_buf[2] != 0x88) {
301                 msleep(100);
302                 gsl_ts_read(client, 0xfc, read_buf, 4);
303         }
304         if (read_buf[2] == 0x82) {
305                 ptr_fw = GSL1680E_FW;
306                 source_len = ARRAY_SIZE(GSL1680E_FW);
307         } else
308 #endif
309         {
310                 ptr_fw = GSLX680_FW;
311                 source_len = ARRAY_SIZE(GSLX680_FW);
312         }
313
314         for (source_line = 0; source_line < source_len; source_line++) {
315                 /* init page trans, set the page val */
316                 if (GSL_PAGE_REG == ptr_fw[source_line].offset) {
317                         fw2buf(cur, &ptr_fw[source_line].val);
318                         gsl_write_interface(client, GSL_PAGE_REG, buf, 4);
319                         send_flag = 1;
320                 } else {
321                         if (1 == send_flag % (DMA_TRANS_LEN < 0x20 ? DMA_TRANS_LEN : 0x20))
322                                 buf[0] = (u8)ptr_fw[source_line].offset;
323
324                         fw2buf(cur, &ptr_fw[source_line].val);
325                         cur += 4;
326
327                         if (0 == send_flag % (DMA_TRANS_LEN < 0x20 ? DMA_TRANS_LEN : 0x20)) {
328                                 gsl_write_interface(client, buf[0], buf, cur - buf - 1);
329                                 cur = buf + 1;
330                         }
331
332                         send_flag++;
333                 }
334         }
335
336         printk("=============gsl_load_fw end==============\n");
337 }
338
339
340 static void startup_chip(struct i2c_client *client)
341 {
342         u8 tmp = 0x00;
343         u8 buf[4] = {0x00};
344
345         buf[3] = 0x01;
346         buf[2] = 0xfe;
347         buf[1] = 0x10;
348         buf[0] = 0x00;
349         gsl_ts_write(client, 0xf0, buf, sizeof(buf));
350         buf[3] = 0x00;
351         buf[2] = 0x00;
352         buf[1] = 0x00;
353         buf[0] = 0x0f;
354         gsl_ts_write(client, 0x04, buf, sizeof(buf));
355         msleep(20);
356         gsl_ts_write(client, 0xe0, &tmp, 1);
357         msleep(10);
358 }
359
360 static void reset_chip(struct i2c_client *client)
361 {
362         u8 tmp = 0x88;
363         u8 buf[4] = {0x00};
364
365         gsl_ts_write(client, 0xe0, &tmp, sizeof(tmp));
366         msleep(20);
367         tmp = 0x04;
368         gsl_ts_write(client, 0xe4, &tmp, sizeof(tmp));
369         msleep(10);
370         gsl_ts_write(client, 0xbc, buf, sizeof(buf));
371         msleep(10);
372 }
373
374 static void clr_reg(struct i2c_client *client)
375 {
376         u8 write_buf[4] = {0};
377
378         write_buf[0] = 0x88;
379         gsl_ts_write(client, 0xe0, &write_buf[0], 1);
380         msleep(20);
381         write_buf[0] = 0x01;
382         gsl_ts_write(client, 0x80, &write_buf[0], 1);
383         msleep(5);
384         write_buf[0] = 0x04;
385         gsl_ts_write(client, 0xe4, &write_buf[0], 1);
386         msleep(5);
387         write_buf[0] = 0x00;
388         gsl_ts_write(client, 0xe0, &write_buf[0], 1);
389         msleep(20);
390 }
391
392 static void init_chip(struct i2c_client *client)
393 {
394         gslX680_shutdown_low();
395         msleep(20);
396         gslX680_shutdown_high();
397         msleep(20);
398         clr_reg(client);
399         reset_chip(client);
400         gsl_load_fw(client);
401         startup_chip(client);
402         reset_chip(client);
403         startup_chip(client);
404 }
405
406 static void check_mem_data(struct i2c_client *client)
407 {
408         u8 read_buf[4]  = {0};
409
410         msleep(50);
411         gsl_ts_read(client, 0xb0, read_buf, sizeof(read_buf));
412
413         if (read_buf[3] != 0x5a || read_buf[2] != 0x5a || read_buf[1] != 0x5a || read_buf[0] != 0x5a) {
414          printk("#########check mem read 0xb0 = %x %x %x %x #########\n", read_buf[3], read_buf[2], read_buf[1], read_buf[0]);
415    #if CLOSE_TP_POWER
416           set_tp_power(false);
417           msleep(200);
418           set_tp_power(true);
419           msleep(100);
420    #endif
421          init_chip(client);
422         }
423 }
424
425 static void record_point(u16 x, u16 y , u8 id)
426 {
427         u16 x_err = 0;
428         u16 y_err = 0;
429
430         id_sign[id] = id_sign[id] + 1;
431
432         if (id_sign[id] == 1) {
433                 x_old[id] = x;
434                 y_old[id] = y;
435         }
436
437         x = (x_old[id] + x)/2;
438         y = (y_old[id] + y)/2;
439
440         if (x > x_old[id]) {
441                 x_err = x - x_old[id];
442         } else {
443                 x_err = x_old[id]-x;
444         }
445
446         if (y > y_old[id]) {
447                 y_err = y - y_old[id];
448         } else {
449                 y_err = y_old[id] - y;
450         }
451
452         if ((x_err > 3 && y_err > 1) || (x_err > 1 && y_err > 3)) {
453                 x_new = x;
454                 x_old[id] = x;
455                 y_new = y;
456                 y_old[id] = y;
457         } else {
458                 if (x_err > 3) {
459                         x_new = x;
460                         x_old[id] = x;
461                 } else
462                         x_new = x_old[id];
463
464                 if (y_err > 3) {
465                         y_new = y;
466                         y_old[id] = y;
467                 } else
468                         y_new = y_old[id];
469         }
470
471         if (id_sign[id] == 1) {
472                 x_new = x_old[id];
473                 y_new = y_old[id];
474         }
475 }
476
477 static void report_data(struct gsl_ts *ts, u16 x, u16 y, u8 pressure, u8 id)
478 {
479         swap(x, y);
480
481         print_info("#####id=%d,x=%d,y=%d######\n", id, x, y);
482         x = 1024 - x;
483         y = 600 - y;
484
485         if (x > SCREEN_MAX_X || y > SCREEN_MAX_Y) {
486         #ifdef HAVE_TOUCH_KEY
487                 report_key(ts, x, y);
488         #endif
489                 return;
490         }
491
492 #ifdef REPORT_DATA_ANDROID_4_0
493         input_mt_slot(ts->input, id);
494         input_report_abs(ts->input, ABS_MT_TRACKING_ID, id);
495         input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, pressure);
496         input_report_abs(ts->input, ABS_MT_POSITION_X, x);
497         input_report_abs(ts->input, ABS_MT_POSITION_Y, y);
498         input_report_abs(ts->input, ABS_MT_WIDTH_MAJOR, 1);
499 #else
500         input_report_abs(ts->input, ABS_MT_TRACKING_ID, id);
501         input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, pressure);
502         input_report_abs(ts->input, ABS_MT_POSITION_X, x);
503         input_report_abs(ts->input, ABS_MT_POSITION_Y, y);
504         input_report_abs(ts->input, ABS_MT_WIDTH_MAJOR, 1);
505         input_mt_sync(ts->input);
506 #endif
507 }
508
509 static void process_gslX680_data(struct gsl_ts *ts)
510 {
511         u8 id, touches;
512         u16 x, y;
513         int i = 0;
514
515         touches = ts->touch_data[ts->dd->touch_index];
516         for (i = 1; i <= MAX_CONTACTS; i++) {
517                 if (touches == 0)
518                         id_sign[i] = 0;
519                 id_state_flag[i] = 0;
520         }
521
522         for (i = 0; i < (touches > MAX_FINGERS ? MAX_FINGERS : touches); i++) {
523                 x = join_bytes((ts->touch_data[ts->dd->x_index + 4*i + 1] & 0xf), ts->touch_data[ts->dd->x_index + 4*i]);
524                 y = join_bytes(ts->touch_data[ts->dd->y_index + 4*i + 1], ts->touch_data[ts->dd->y_index + 4*i]);
525                 id = ts->touch_data[ts->dd->id_index + 4*i] >> 4;
526
527                 if (1 <= id && id <= MAX_CONTACTS) {
528                 #ifdef FILTER_POINT
529                         filter_point(x, y, id);
530                 #else
531                         record_point(x, y, id);
532                 #endif
533                         report_data(ts, x_new, y_new, 50, id);
534                         id_state_flag[id] = 1;
535                 }
536         }
537
538         for (i = 1; i <= MAX_CONTACTS; i++) {
539                 if ((0 == touches) || ((0 != id_state_old_flag[i]) && (0 == id_state_flag[i]))) {
540                 #ifdef REPORT_DATA_ANDROID_4_0
541                         input_mt_slot(ts->input, i);
542                         input_report_abs(ts->input, ABS_MT_TRACKING_ID, -1);
543                         input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, false);
544                 #endif
545                         id_sign[i] = 0;
546                 }
547                 id_state_old_flag[i] = id_state_flag[i];
548         }
549 #ifndef REPORT_DATA_ANDROID_4_0
550         if (0 == touches) {
551                 input_mt_sync(ts->input);
552         #ifdef HAVE_TOUCH_KEY
553                 if (key_state_flag) {
554                         input_report_key(ts->input, key, 0);
555                         input_sync(ts->input);
556                         key_state_flag = 0;
557                 }
558         #endif
559         }
560 #endif
561         input_sync(ts->input);
562 }
563
564
565 static void gsl_ts_xy_worker(struct work_struct *work)
566 {
567         int rc;
568         u8 read_buf[4] = {0};
569
570         struct gsl_ts *ts = container_of(work, struct gsl_ts, work);
571         print_info("---gsl_ts_xy_worker---\n");
572
573         /* read data from DATA_REG */
574         rc = gsl_ts_read(ts->client, 0x80, ts->touch_data, ts->dd->data_size);
575         print_info("---touches: %d ---\n", ts->touch_data[0]);
576
577         if (rc < 0) {
578                 dev_err(&ts->client->dev, "read failed\n");
579                 goto schedule;
580         }
581
582         if (ts->touch_data[ts->dd->touch_index] == 0xff) {
583                 goto schedule;
584         }
585
586         rc = gsl_ts_read(ts->client, 0xbc, read_buf, sizeof(read_buf));
587         if (rc < 0) {
588                 dev_err(&ts->client->dev, "read 0xbc failed\n");
589                 goto schedule;
590         }
591         print_info("//////// reg %x : %x %x %x %x\n", 0xbc, read_buf[3], read_buf[2], read_buf[1], read_buf[0]);
592
593         if (read_buf[3] == 0 && read_buf[2] == 0 && read_buf[1] == 0 && read_buf[0] == 0) {
594                 process_gslX680_data(ts);
595         } else {
596                 reset_chip(ts->client);
597                 startup_chip(ts->client);
598         }
599 schedule:
600         enable_irq(ts->irq);
601 }
602
603 static irqreturn_t gsl_ts_irq(int irq, void *dev_id)
604 {
605         struct gsl_ts *ts = dev_id;
606
607         print_info("==========GSLX680 Interrupt============\n");
608
609         disable_irq_nosync(ts->irq);
610
611         if (!work_pending(&ts->work)) {
612                 queue_work(ts->wq, &ts->work);
613         }
614
615         return IRQ_HANDLED;
616
617 }
618
619 #ifdef GSL_TIMER
620 static void gsl_timer_handle(unsigned long data)
621 {
622         struct gsl_ts *ts = (struct gsl_ts *)data;
623
624 #ifdef GSL_DEBUG
625         printk("----------------gsl_timer_handle-----------------\n");
626 #endif
627
628         disable_irq_nosync(ts->irq);
629         check_mem_data(ts->client);
630         ts->gsl_timer.expires = jiffies + 3 * HZ;
631         add_timer(&ts->gsl_timer);
632         enable_irq(ts->irq);
633 }
634 #endif
635
636 static int gsl_ts_init_ts(struct i2c_client *client, struct gsl_ts *ts)
637 {
638         struct input_dev *input_device;
639 #ifdef CONFIG_MACH_RK_FAC
640         struct tp_platform_data *pdata = client->dev.platform_data;
641 #endif
642         int rc = 0;
643
644         printk("[GSLX680] Enter %s\n", __func__);
645
646         ts->dd = &devices[ts->device_id];
647
648         if (ts->device_id == 0) {
649                 ts->dd->data_size = MAX_FINGERS * ts->dd->touch_bytes + ts->dd->touch_meta_data;
650                 ts->dd->touch_index = 0;
651         }
652
653         ts->touch_data = kzalloc(ts->dd->data_size, GFP_KERNEL);
654         if (!ts->touch_data) {
655                 pr_err("%s: Unable to allocate memory\n", __func__);
656                 return -ENOMEM;
657         }
658
659
660         input_device = input_allocate_device();
661         if (!input_device) {
662                 rc = -ENOMEM;
663                 goto error_alloc_dev;
664         }
665
666         ts->input = input_device;
667         input_device->name = GSLX680_I2C_NAME;
668         input_device->id.bustype = BUS_I2C;
669         input_device->dev.parent = &client->dev;
670         input_set_drvdata(input_device, ts);
671
672 #ifdef REPORT_DATA_ANDROID_4_0
673         __set_bit(EV_ABS, input_device->evbit);
674         __set_bit(EV_KEY, input_device->evbit);
675         __set_bit(EV_REP, input_device->evbit);
676         __set_bit(INPUT_PROP_DIRECT, input_device->propbit);
677         input_mt_init_slots(input_device, (MAX_CONTACTS+1), 0);
678 #else
679         input_set_abs_params(input_device, ABS_MT_TRACKING_ID, 0, (MAX_CONTACTS+1), 0, 0);
680         set_bit(EV_ABS, input_device->evbit);
681         set_bit(EV_KEY, input_device->evbit);
682 #endif
683
684 #ifdef HAVE_TOUCH_KEY
685         input_device->evbit[0] = BIT_MASK(EV_KEY);
686         for (i = 0; i < MAX_KEY_NUM; i++)
687                 set_bit(key_array[i], input_device->keybit);
688 #endif
689
690         set_bit(ABS_MT_POSITION_X, input_device->absbit);
691         set_bit(ABS_MT_POSITION_Y, input_device->absbit);
692         set_bit(ABS_MT_TOUCH_MAJOR, input_device->absbit);
693         set_bit(ABS_MT_WIDTH_MAJOR, input_device->absbit);
694 #ifdef CONFIG_MACH_RK_FAC
695         input_set_abs_params(input_device, ABS_MT_POSITION_X, 0, pdata->x_max, 0, 0);
696         input_set_abs_params(input_device, ABS_MT_POSITION_Y, 0, pdata->y_max, 0, 0);
697 #else
698         input_set_abs_params(input_device, ABS_MT_POSITION_X, 0, SCREEN_MAX_X, 0, 0);
699         input_set_abs_params(input_device, ABS_MT_POSITION_Y, 0, SCREEN_MAX_Y, 0, 0);
700 #endif
701         input_set_abs_params(input_device, ABS_MT_TOUCH_MAJOR, 0, PRESS_MAX, 0, 0);
702         input_set_abs_params(input_device, ABS_MT_WIDTH_MAJOR, 0, 200, 0, 0);
703
704         client->irq = ts->irq;
705
706         ts->wq = create_singlethread_workqueue("kworkqueue_ts");
707         if (!ts->wq) {
708                 dev_err(&client->dev, "Could not create workqueue\n");
709                 goto error_wq_create;
710         }
711         flush_workqueue(ts->wq);
712
713         INIT_WORK(&ts->work, gsl_ts_xy_worker);
714
715         rc = input_register_device(input_device);
716         if (rc)
717                 goto error_unreg_device;
718
719         return 0;
720
721 error_unreg_device:
722         destroy_workqueue(ts->wq);
723 error_wq_create:
724         input_free_device(input_device);
725 error_alloc_dev:
726         kfree(ts->touch_data);
727         return rc;
728 }
729
730
731 #if 1
732 static int gsl_ts_suspend(void)
733 {
734         struct gsl_ts *ts = g_gsl_ts;
735         printk("I'am in gsl_ts_suspend() start\n");
736         flush_workqueue(ts->wq);
737 #ifdef GSL_TIMER
738         printk("gsl_ts_suspend () : delete gsl_timer\n");
739         del_timer(&ts->gsl_timer);
740 #endif
741         disable_irq_nosync(ts->irq);
742         gslX680_shutdown_low();
743         return 0;
744 }
745
746 static int gsl_ts_resume(void)
747 {
748         struct gsl_ts *ts = g_gsl_ts;
749         int i = 0;
750
751         printk("I'am in gsl_ts_resume() start\n");
752
753         gslX680_shutdown_high();
754         msleep(20);
755         reset_chip(ts->client);
756         startup_chip(ts->client);
757         check_mem_data(ts->client);
758
759         msleep(100);
760         reset_chip(ts->client);
761         startup_chip(ts->client);
762         msleep(100);
763         reset_chip(ts->client);
764         startup_chip(ts->client);
765 #ifdef GSL_TIMER
766         printk("gsl_ts_resume () : add gsl_timer\n");
767         init_timer(&ts->gsl_timer);
768         ts->gsl_timer.expires = jiffies + 3 * HZ;
769         ts->gsl_timer.function = &gsl_timer_handle;
770         ts->gsl_timer.data = (unsigned long)ts;
771         add_timer(&ts->gsl_timer);
772 #endif
773
774   for (i = 1; i <= MAX_CONTACTS; i++) {
775                 if (0 != id_state_old_flag[i]) {
776                 #ifdef REPORT_DATA_ANDROID_4_0
777                         input_mt_slot(ts->input, i);
778                         input_report_abs(ts->input, ABS_MT_TRACKING_ID, -1);
779                         input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, false);
780                         input_sync(ts->input);
781                 #endif
782                         id_sign[i] = 0;
783                 }
784                 id_state_old_flag[i] = id_state_flag[i] = 0;
785         }
786         enable_irq(ts->irq);
787         return 0;
788 }
789
790 static int gsl_ts_fb_event_notify(struct notifier_block *self,
791                                       unsigned long action, void *data)
792 {
793         struct fb_event *event = data;
794         int blank_mode = *((int *)event->data);
795
796         if (action == FB_EARLY_EVENT_BLANK) {
797                 switch (blank_mode) {
798                 case FB_BLANK_UNBLANK:
799                         break;
800                 default:
801                         gsl_ts_suspend();
802                         break;
803                 }
804         } else if (action == FB_EVENT_BLANK) {
805                 switch (blank_mode) {
806                 case FB_BLANK_UNBLANK:
807                         gsl_ts_resume();
808                         break;
809                 default:
810                         break;
811                 }
812         }
813
814         return NOTIFY_OK;
815 }
816
817 static struct notifier_block gsl_ts_fb_notifier = {
818         .notifier_call = gsl_ts_fb_event_notify,
819 };
820
821 #endif
822
823 int gslx680_config_hw(struct gsl_ts *ts)
824 {
825         struct device_node *np;
826         enum of_gpio_flags rst_flags;
827         unsigned long irq_flags;
828         unsigned int irq_gpio;
829         struct device *dev;
830
831         dev = &ts->client->dev;
832         np = dev->of_node;
833
834         irq_gpio = of_get_named_gpio_flags(np, "touch-gpio", 0, (enum of_gpio_flags *)&irq_flags);
835         ts->irq = gpio_to_irq(irq_gpio);
836         ts->reset_gpio = of_get_named_gpio_flags(np, "reset-gpio", 0, &rst_flags);
837
838         if (gpio_request(ts->reset_gpio, NULL) != 0) {
839                 gpio_free(ts->reset_gpio);
840                 printk("gslx680_init_platform_hw gpio_request error\n");
841                 return -EIO;
842         }
843
844         if (gpio_request(irq_gpio, NULL) != 0) {
845              gpio_free(irq_gpio);
846              printk("gslx680_init_platform_hw  gpio_request error\n");
847              return -EIO;
848         }
849
850         gpio_direction_output(ts->reset_gpio, GPIO_HIGH);
851         mdelay(10);
852         gpio_set_value(ts->reset_gpio, GPIO_LOW);
853         mdelay(10);
854         gpio_set_value(ts->reset_gpio, GPIO_HIGH);
855         msleep(300);
856         return 0;
857
858 }
859
860
861 static int  gsl_ts_probe(struct i2c_client *client,
862                         const struct i2c_device_id *id)
863 {
864         struct gsl_ts *ts;
865         int rc;
866         u8 read_buf = 0;
867         int ret;
868
869         printk("GSLX680 Enter %s\n", __func__);
870         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
871                 dev_err(&client->dev, "I2C functionality not supported\n");
872                 return -ENODEV;
873         }
874
875         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
876         if (!ts)
877                 return -ENOMEM;
878         printk("==kzalloc success=\n");
879         g_gsl_ts = ts;
880
881         ts->client = client;
882         ts->device_id = id->driver_data;
883
884         gslx680_config_hw(ts);
885         ts_global_reset_pin = ts->reset_gpio;
886         i2c_set_clientdata(client, ts);
887
888         rc = gsl_ts_init_ts(client, ts);
889         if (rc < 0) {
890                 dev_err(&client->dev, "GSLX680 init failed\n");
891                 goto error_mutex_destroy;
892         }
893
894         init_chip(ts->client);
895         check_mem_data(ts->client);
896
897         rc = request_irq(client->irq, gsl_ts_irq, IRQF_TRIGGER_RISING, client->name, ts);
898         if (rc < 0) {
899                 printk("gsl_probe: request irq failed\n");
900                 goto error_req_irq_fail;
901         }
902
903         disable_irq(client->irq);
904
905         ret = gsl_ts_read(client, 0xf0, &read_buf, sizeof(read_buf));
906         if (ret  < 0) {
907                 pr_info("gslx680  I2C transfer error!\n");
908                 goto error_req_irq_fail;
909         }
910
911 #ifdef GSL_TIMER
912         printk("gsl_ts_probe () : add gsl_timer\n");
913
914         init_timer(&ts->gsl_timer);
915         ts->gsl_timer.expires = jiffies + 3 * HZ;
916         ts->gsl_timer.function = &gsl_timer_handle;
917         ts->gsl_timer.data = (unsigned long)ts;
918         add_timer(&ts->gsl_timer);
919 #endif
920         fb_register_client(&gsl_ts_fb_notifier);
921
922         printk("[GSLX680] End %s\n", __func__);
923
924         return 0;
925
926 error_req_irq_fail:
927     free_irq(ts->irq, ts);
928
929 error_mutex_destroy:
930         input_free_device(ts->input);
931         kfree(ts);
932         return rc;
933 }
934
935 static int  gsl_ts_remove(struct i2c_client *client)
936 {
937         struct gsl_ts *ts = i2c_get_clientdata(client);
938         printk("==gsl_ts_remove=\n");
939         device_init_wakeup(&client->dev, 0);
940         cancel_work_sync(&ts->work);
941         free_irq(ts->irq, ts);
942         destroy_workqueue(ts->wq);
943         input_unregister_device(ts->input);
944
945         kfree(ts->touch_data);
946         kfree(ts);
947
948         return 0;
949 }
950
951 static const struct i2c_device_id gsl_ts_id[] = {
952         {GSLX680_I2C_NAME, 0},
953         {}
954 };
955 MODULE_DEVICE_TABLE(i2c, gsl_ts_id);
956
957 static struct of_device_id gslX680_dt_ids[] = {
958         { .compatible = "gslX680" },
959 };
960
961 static struct i2c_driver gsl_ts_driver = {
962         .driver = {
963                 .name = GSLX680_I2C_NAME,
964                 .owner = THIS_MODULE,
965                 .of_match_table = of_match_ptr(gslX680_dt_ids),
966         },
967         .probe          = gsl_ts_probe,
968         .remove         = gsl_ts_remove,
969         .id_table       = gsl_ts_id,
970 };
971
972 static int __init gsl_ts_init(void)
973 {
974     int ret;
975         printk("==gsl_ts_init==\n");
976         ret = i2c_add_driver(&gsl_ts_driver);
977         printk("ret=%d\n", ret);
978         return ret;
979 }
980 static void __exit gsl_ts_exit(void)
981 {
982         printk("==gsl_ts_exit==\n");
983         i2c_del_driver(&gsl_ts_driver);
984         return;
985 }
986
987 module_init(gsl_ts_init);
988 module_exit(gsl_ts_exit);
989
990 MODULE_LICENSE("GPL");
991 MODULE_DESCRIPTION("GSLX680 touchscreen controller driver");
992 MODULE_AUTHOR("Guan Yuwei, guanyuwei@basewin.com");
993 MODULE_ALIAS("platform:gsl_ts");