input: touchscreen: add touch screen of gslx680 for rk3399-firefly-edp
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / IT7260_ts_i2c.c
1 /* drivers/input/touchscreen/IT7260_ts_i2c.c
2  *
3  * Copyright (C) 2007 Google, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15
16 #include <linux/module.h>
17 #include <linux/delay.h>
18 #include <linux/earlysuspend.h>
19 #include <linux/i2c.h>
20 #include <asm/uaccess.h>
21 #include <linux/input.h>
22 #include <linux/interrupt.h>
23 #include <linux/io.h>
24 #include <linux/cdev.h>
25 #include <linux/platform_device.h>
26 #include "IT7260_ts.h"
27
28 // >>> [110308] protect touch panel [Derek]
29 #include <linux/timer.h>
30 #include <linux/gpio.h>
31
32 static struct timer_list tp_timer;
33 static void tp_irq_handler_reg(unsigned long arg);
34
35 // <<< [110308] protect touch panel [Derek]
36
37 extern void set_tp_status(int number, int status) ;
38 extern int get_tp_status(int number) ;  
39
40 #define IT7260_I2C_NAME "IT7260"
41 #include <linux/gpio.h>
42 static int ite7260_major = 0; // dynamic major by default
43 static int ite7260_minor = 0;
44 static struct cdev ite7260_cdev;
45 static struct class *ite7260_class = NULL;
46 static dev_t ite7260_dev;
47 static struct input_dev *input_dev;
48
49 #ifdef DEBUG
50 #define TS_DEBUG(fmt,args...)  printk( KERN_DEBUG "[it7260_i2c]: " fmt, ## args)
51 #define DBG() printk("[%s]:%d => \n",__FUNCTION__,__LINE__)
52 #else
53 #define TS_DEBUG(fmt,args...)
54 #define DBG()
55 #endif
56
57 static struct workqueue_struct *IT7260_wq;
58
59 struct IT7260_ts_data {
60         struct i2c_client *client;
61         struct input_dev *input_dev;
62         int use_irq;
63         struct work_struct work;
64         struct early_suspend early_suspend;
65         uint8_t debug_log_level;
66 };
67 #ifdef CONFIG_HAS_EARLYSUSPEND
68 static void IT7260_ts_early_suspend(struct early_suspend *h);
69 static void IT7260_ts_late_resume(struct early_suspend *h);
70 #endif
71
72 static struct IT7260_ts_data *gl_ts;
73
74 int i2cReadFromIt7260(struct i2c_client *client, unsigned char bufferIndex,
75                 unsigned char dataBuffer[], unsigned short dataLength) {
76         int ret;
77         struct i2c_msg msgs[2] = { { .addr = client->addr, .flags = I2C_M_NOSTART,
78                         .len = 1, .buf = &bufferIndex }, { .addr = client->addr, .flags =
79                         I2C_M_RD, .len = dataLength, .buf = dataBuffer } };
80
81         memset(dataBuffer, 0xFF, dataLength);
82         ret = i2c_transfer(client->adapter, msgs, 2);
83         return ret;
84 }
85
86 int i2cWriteToIt7260(struct i2c_client *client, unsigned char bufferIndex,
87                 unsigned char const dataBuffer[], unsigned short dataLength) {
88         unsigned char buffer4Write[256];
89         struct i2c_msg msgs[1] = { { .addr = client->addr, .flags = 0, .len =
90                         dataLength + 1, .buf = buffer4Write } };
91
92         buffer4Write[0] = bufferIndex;
93         memcpy(&(buffer4Write[1]), dataBuffer, dataLength);
94         return i2c_transfer(client->adapter, msgs, 1);
95 }
96
97 static int IdentifyCapSensor(struct IT7260_ts_data *ts);
98
99 static void Read_Point(struct IT7260_ts_data *ts) {
100         unsigned char ucQuery = 0;
101         unsigned char pucPoint[14];
102 #ifdef HAS_8_BYTES_LIMIT
103         unsigned char cPoint[8];
104         unsigned char ePoint[6];
105 #endif //HAS_8_BYTES_LIMIT
106         int ret = 0;
107         int finger2_pressed = 0;
108         int xraw, yraw, xtmp, ytmp;
109         int i = 0;
110         static int x[2] = { (int) -1, (int) -1 };
111         static int y[2] = { (int) -1, (int) -1 };
112         static bool finger[2] = { 0, 0 };
113         static bool flag = 0;
114
115         i2cReadFromIt7260(ts->client, 0x80, &ucQuery, 1);
116         if (ucQuery < 0) {
117                 //pr_info("=error Read_Point=\n");
118                 if (ts->use_irq)
119                         enable_irq(ts->client->irq);
120                 return;
121         } else {
122                 if (ucQuery & 0x80) {
123 #ifdef HAS_8_BYTES_LIMIT
124                         i2cReadFromIt7260(ts->client, 0xC0, cPoint, 8);
125                         ret = i2cReadFromIt7260(ts->client, 0xE0, ePoint, 6);
126                         for(i=0; i<6; i++) {
127                                 pucPoint[i] = ePoint[i];
128                         }
129                         for(i=0; i<8; i++) {
130                                 pucPoint[i+6] = cPoint[i];
131                         }
132 #else //HAS_8_BYTES_LIMIT
133                         ret = i2cReadFromIt7260(ts->client, 0xE0, pucPoint, 14);
134 #endif //HAS_8_BYTES_LIMIT
135                         //pr_info("=Read_Point read ret[%d]--point[%x][%x][%x][%x][%x][%x][%x][%x][%x][%x][%x][%x][%x][%x]=\n",
136                         //      ret,pucPoint[0],pucPoint[1],pucPoint[2],
137                         //      pucPoint[3],pucPoint[4],pucPoint[5],pucPoint[6],pucPoint[7],pucPoint[8],
138                         //      pucPoint[9],pucPoint[10],pucPoint[11],pucPoint[12],pucPoint[13]);
139                         if (ret) {
140                                 // gesture
141                                 if (pucPoint[0] & 0xF0) {
142                                         if (ts->use_irq)
143                                                 enable_irq(ts->client->irq);
144                                         //pr_info("(pucPoint[0] & 0xF0) is true, it's a gesture\n") ;
145                                         //pr_info("pucPoint[0]=%x\n", pucPoint[0]);
146                                         return;
147                                 }
148                                 // palm
149                                 if (pucPoint[1] & 0x01) {
150                                         if (ts->use_irq)
151                                                 enable_irq(ts->client->irq);
152                                         //pr_info("pucPoint 1 is 0x01, it's a palm\n") ;
153                                         return;
154                                 }
155                                 // no more data
156                                 if (!(pucPoint[0] & 0x08)) {
157                                         if (finger[0]) {
158                                                 input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, 1);
159                                                 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0);
160                                                 //input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, pressure_point);
161                                                 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0);
162                                                 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x[0]);
163                                                 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y[0]);
164                                                 input_mt_sync(ts->input_dev);
165                                                 finger[0] = 0;
166                                                 flag = 1;
167                                         }
168                                         if (finger[1]) {
169                                                 input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, 2);
170                                                 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0);
171                                                 //input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, pressure_point);
172                                                 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0);
173                                                 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x[1]);
174                                                 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y[1]);
175                                                 input_mt_sync(ts->input_dev);
176                                                 finger[1] = 0;
177                                                 flag = 1;
178                                         }
179                                         if (flag) {
180                                                 input_sync(ts->input_dev);
181                                                 flag = 0;
182                                         }
183                                         if (ts->use_irq)
184                                                 enable_irq(ts->client->irq);
185                                         //pr_info("(pucPoint[0] & 0x08) is false, means no more data\n") ;
186                                         return;
187                                 }
188                                 // 3 fingers
189                                 if (pucPoint[0] & 0x04) {
190                                         if (ts->use_irq)
191                                                 enable_irq(ts->client->irq);
192                                         //pr_info("(pucPoint[0] & 0x04) is true, we don't support three fingers\n") ;
193                                         return;
194                                 }
195
196                                 if (pucPoint[0] & 0x01) {
197                                         char pressure_point, z, w;
198
199                                         xraw = ((pucPoint[3] & 0x0F) << 8) + pucPoint[2];
200                                         yraw = ((pucPoint[3] & 0xF0) << 4) + pucPoint[4];
201
202                                         pressure_point = pucPoint[5] & 0x0f;
203                                         //pr_info("=Read_Point1 x=%d y=%d p=%d=\n",xraw,yraw,pressure_point);
204
205                                         input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, 1);
206                                         input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 1);
207                                         //input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, pressure_point);
208                                         input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0);
209                                         input_report_abs(ts->input_dev, ABS_MT_POSITION_X, xraw);
210                                         input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, yraw);
211                                         input_mt_sync(ts->input_dev);
212                                         x[0] = xraw;
213                                         y[0] = yraw;
214                                         finger[0] = 1;
215                                         //pr_info("=input Read_Point1 x=%d y=%d p=%d=\n",xraw,yraw,pressure_point);
216                                 } else {
217                                         input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, 1);
218                                         input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0);
219                                         //input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, pressure_point);
220                                         input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0);
221                                         input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x[0]);
222                                         input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y[0]);
223                                         input_mt_sync(ts->input_dev);
224                                         finger[0] = 0;
225                                 }
226
227                                 if (pucPoint[0] & 0x02) {
228                                         char pressure_point, z, w;
229                                         xraw = ((pucPoint[7] & 0x0F) << 8) + pucPoint[6];
230                                         yraw = ((pucPoint[7] & 0xF0) << 4) + pucPoint[8];
231
232                                         pressure_point = pucPoint[9] & 0x0f;
233
234                                         //pr_info("=Read_Point2 x=%d y=%d p=%d=\n",xraw,yraw,pressure_point);
235                                         input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, 2);
236                                         input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 1);
237                                         //input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, pressure_point);
238                                         input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0);
239                                         input_report_abs(ts->input_dev, ABS_MT_POSITION_X, xraw);
240                                         input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, yraw);
241                                         input_mt_sync(ts->input_dev);
242                                         x[1] = xraw;
243                                         y[1] = yraw;
244                                         finger[1] = 1;
245                                         //pr_info("input Read_Point2 x=%d y=%d p=%d=\n",xraw,yraw,pressure_point);
246                                 } else {
247                                         input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, 2);
248                                         input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0);
249                                         //input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, pressure_point);
250                                         input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0);
251                                         input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x[1]);
252                                         input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y[1]);
253                                         input_mt_sync(ts->input_dev);
254                                         finger[1] = 0;
255                                 }
256                                 input_sync(ts->input_dev);
257                         }
258                 }
259         }
260         if (ts->use_irq)
261                 enable_irq(ts->client->irq);
262         //pr_info("=end Read_Point=\n");
263
264         //IdentifyCapSensor(gl_ts);
265 }
266
267 ///////////////////////////////////////////////////////////////////////////////////////
268
269 static void IT7260_ts_work_func(struct work_struct *work) {
270         int i;
271         int ret;
272         int bad_data = 0;
273         struct i2c_msg msg[2];
274         uint8_t start_reg;
275         uint8_t buf[15];
276         //printk(KERN_INFO "=IT7260_ts_work_func=\n"); 
277         struct IT7260_ts_data *ts = container_of(work, struct IT7260_ts_data, work);
278         gl_ts = ts;
279         Read_Point(ts);
280 }
281
282 // >>> [110308] protect touch panel [Derek]
283 int delayCount = 1;
284 static irqreturn_t IT7260_ts_irq_handler(int irq, void *dev_id) {
285         struct IT7260_ts_data *ts = dev_id;
286
287     if (delayCount == 1)
288     {
289        pr_info("=IT7260_ts_irq_handler=\n");
290     }
291     
292     disable_irq_nosync(ts->client->irq);
293         queue_work(IT7260_wq, &ts->work);
294         return IRQ_HANDLED;
295 }
296 // <<< [110308] protect touch panel [Derek]
297
298 /////////////////////////////////////////////////////////
299 void sendCalibrationCmd(void) {
300         int ret = 0;
301         struct IT7260_ts_data *ts = gl_ts;
302         unsigned char data[] = { 0x13, 0x00, 0x00, 0x00, 0x00 };
303         unsigned char resp[2];
304
305         ret = i2cWriteToIt7260(ts->client, 0x20, data, 5);
306         printk(KERN_INFO "IT7260 sent calibration command [%d]!!!\n", ret);
307
308         //MUST sleep 5 seconds here!
309     mdelay(5000);
310
311         //Read out response to clear interrupt.
312         i2cReadFromIt7260(ts->client, 0xA0, resp, 2);
313 }
314
315 EXPORT_SYMBOL( sendCalibrationCmd);
316
317 // >>> [110308] protect touch panel [Derek]
318 static void tp_irq_handler_reg(unsigned long arg)
319 {
320     delayCount = 0;
321 }
322 // <<< [110308] protect touch panel [Derek]
323
324 static int IdentifyCapSensor(struct IT7260_ts_data *ts) {
325         unsigned char ucQuery;
326         unsigned char pucCmd[80];
327         int ret = 0;
328         int test_read_count = 0;
329         //pr_info("=entry IdentifyCapSensor=\n");
330         //pr_info("=entry IdentifyCapSensor---name[%s]---addr[%x]-flags[%d]=\n",ts->client->name,ts->client->addr,ts->client->flags);
331         i2cReadFromIt7260(ts->client, 0x80, &ucQuery, 1);
332         //pr_info("=IdentifyCapSensor read 0x80 =%d=\n",ucQuery);
333         if (ucQuery < 0) {
334                 msleep(250);
335                 ucQuery = 0xFF;
336         }
337         while (ucQuery & 0x01) {
338                 i2cReadFromIt7260(ts->client, 0x80, &ucQuery, 1);
339                 if (ucQuery < 0) {
340                         ucQuery = 0xFF;
341                 }
342         }
343         //pr_info("=IdentifyCapSensor write cmd=\n");
344         pucCmd[0] = 0x00;
345         ret = i2cWriteToIt7260(ts->client, 0x20, pucCmd, 1);
346         if (ret < 0) {
347                 printk(KERN_ERR "i2c_smbus_write_byte_data failed\n");
348                 /* fail? */
349                 return ret;
350         }
351
352         i2cReadFromIt7260(ts->client, 0x80, &ucQuery, 1);
353         if (ucQuery < 0) {
354                 ucQuery = 0xFF;
355         }
356         test_read_count = 0;
357         while ((ucQuery & 0x01) && (test_read_count < 0x2000)) {
358                 test_read_count++;
359                 i2cReadFromIt7260(ts->client, 0x80, &ucQuery, 1);
360                 if (ucQuery < 0) {
361                         ucQuery = 0xFF;
362                 }
363         }
364         //pr_info("=IdentifyCapSensor write read id=\n");
365         ret = i2cReadFromIt7260(ts->client, 0xA0, pucCmd, 8);
366         //pr_info(
367         //              "=IdentifyCapSensor read id--[%d][%x][%x][%x][%x][%x][%x][%x][%x][%x][%x]=\n",
368         //              ret, pucCmd[0], pucCmd[1], pucCmd[2], pucCmd[3], pucCmd[4],
369         //              pucCmd[5], pucCmd[6], pucCmd[7], pucCmd[8], pucCmd[9]);
370
371 }
372
373 static int IT7260_ts_probe(struct i2c_client *client,
374                 const struct i2c_device_id *id) {
375         struct IT7260_ts_data *ts;
376         int ret = 0;
377         struct IT7260_i2c_platform_data *pdata;
378         unsigned long irqflags;
379         unsigned char ucQuery = 0;
380         u8 cmdbuf[2] = { 0x07, 0 };
381
382         irqflags = IRQF_TRIGGER_HIGH;
383         pr_info("=entry IT7260_ts_probe=\n");
384
385         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
386                 printk(KERN_ERR "IT7260_ts_probe: need I2C_FUNC_I2C\n");
387                 ret = -ENODEV;
388                 goto err_check_functionality_failed;
389         }
390         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
391         if (ts == NULL) {
392                 ret = -ENOMEM;
393                 goto err_check_functionality_failed;
394         }
395         ts->client = client;
396
397         ts->debug_log_level = 0x3;
398         ts->input_dev = input_dev;
399
400         i2c_set_clientdata(client, ts);
401         pdata = client->dev.platform_data;
402
403 #if 0
404         //ret=IdentifyCapSensor(ts);
405         //if(ret<0)
406         //      goto err_power_failed;
407
408         // Ant start -- to identify if this device is exist
409         if (get_tp_status(1) == 0)
410         {
411             printk(KERN_ERR "The ite TP device is not exist\n");
412             ret = -ENODEV;
413                 goto err_power_failed;
414         } else {
415             set_tp_status(1, 1) ;
416         }
417         // Ant end
418 #endif
419
420 #ifdef CONFIG_HAS_EARLYSUSPEND
421         ts->early_suspend.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING - 1;
422         ts->early_suspend.suspend = IT7260_ts_early_suspend;
423         ts->early_suspend.resume = IT7260_ts_late_resume;
424         register_early_suspend(&ts->early_suspend);
425 #endif
426
427         //IT7260_wq = create_singlethread_workqueue("IT7260_wq");
428     IT7260_wq = create_workqueue("IT7260_wq");
429         if (!IT7260_wq)
430                 goto err_check_functionality_failed;
431         INIT_WORK(&ts->work, IT7260_ts_work_func);
432
433     // >>> [110308] protect touch panel [Derek]
434     init_timer(&tp_timer) ;
435
436     tp_timer.expires = jiffies + 30 * HZ;
437     tp_timer.function = &tp_irq_handler_reg;
438
439     add_timer(&tp_timer);
440     // >>> [110308] protect touch panel [Derek]
441
442         pr_info("IT7260_ts_probe-client->irq[%d]=\n", client->irq);
443         if (client->irq) {
444                 ret = request_irq(client->irq, IT7260_ts_irq_handler, IRQF_TRIGGER_LOW,
445 //              ret = request_irq(client->irq, IT7260_ts_irq_handler, IRQF_DISABLED | IRQF_TRIGGER_LOW,
446 //              ret = request_irq(client->irq, IT7260_ts_irq_handler, IRQF_SHARED | IRQF_TRIGGER_LOW,
447                                 client->name, ts);
448                 pr_info("IT7260_ts_probe-request_irq[%d]=\n", ret);
449                 if (ret == 0)
450                         ts->use_irq = 1;
451                 else
452                         dev_err(&client->dev, "request_irq failed\n");
453         }
454
455         gl_ts = ts;
456         pr_info("=end IT7260_ts_probe=\n");
457
458         //To reset point queue.
459         i2cWriteToIt7260(ts->client, 0x20, cmdbuf, 1);
460         mdelay(10);
461         i2cReadFromIt7260(ts->client, 0xA0, cmdbuf, 2);
462         mdelay(10);
463
464         return 0;
465         err_power_failed: kfree(ts);
466
467         err_check_functionality_failed: return ret;
468
469 }
470
471 static int IT7260_ts_remove(struct i2c_client *client) {
472         return 0;
473 }
474
475 static int IT7260_ts_suspend(struct i2c_client *client, pm_message_t mesg) {
476         char ret;
477         u8 cmdbuf[] = { 0x04, 0x00, 0x02 };
478
479     printk(KERN_DEBUG "IT7260_ts_i2c call suspend\n");
480         if (i2cWriteToIt7260(client, 0x20, cmdbuf, 3) >= 0)
481                 ret = 0;
482         else
483                 ret = -1;
484
485         return ret;
486 }
487
488 static int IT7260_ts_resume(struct i2c_client *client) {
489         unsigned char ucQuery;
490
491         printk(KERN_DEBUG "IT7260_ts_i2c call resume\n");
492 #ifdef INT_PIN_OPEN_DRAIN
493     //TODO: Here 2 is the pin number of INT pin, so please modify it to the one your system uses.
494         gpio_direction_output(2, 0);
495         mdelay(10);
496 #endif //INT_PIN_OPEN_DRAIN
497         i2cReadFromIt7260(client, 0x80, &ucQuery, 1);
498 #ifdef INT_PIN_OPEN_DRAIN
499         mdelay(10);
500         gpio_direction_output(2, 1);
501         mdelay(50);
502         gpio_direction_input(2);
503 #endif //INT_PIN_OPEN_DRAIN
504         return 0;
505 }
506
507 #ifdef CONFIG_HAS_EARLYSUSPEND
508 static void IT7260_ts_early_suspend(struct early_suspend *h)
509 {
510         struct IT7260_ts_data *ts;
511         ts = container_of(h, struct IT7260_ts_data, early_suspend);
512         IT7260_ts_suspend(ts->client, PMSG_SUSPEND);
513 }
514
515 static void IT7260_ts_late_resume(struct early_suspend *h)
516 {
517         struct IT7260_ts_data *ts;
518         ts = container_of(h, struct IT7260_ts_data, early_suspend);
519         IT7260_ts_resume(ts->client);
520 }
521 #endif
522
523 static const struct i2c_device_id IT7260_ts_id[] = { { IT7260_I2C_NAME, 0 },
524                 { } };
525
526 bool IT7260_Init(void) {
527         int i;
528         int tmp;
529         unsigned char ucQuery = 0;
530         unsigned char buffer[128];
531         struct IT7260_ts_data *ts = gl_ts;
532
533         // Identify Cap Sensor
534         do {
535                 i2cReadFromIt7260(ts->client, 0x80, &ucQuery, 1);
536         } while (ucQuery & 0x01);
537         buffer[0] = 0x00;
538         i2cWriteToIt7260(ts->client, 0x20, buffer, 1);
539         do {
540                 i2cReadFromIt7260(ts->client, 0x80, &ucQuery, 1);
541         } while (ucQuery & 0x01);
542
543         memset(&buffer, 0, sizeof(buffer));
544         i2cReadFromIt7260(ts->client, 0xA0, buffer, 8);
545         pr_info("=IT7260_Init --[%x][%x][%x][%x][%x][%x]=\n", buffer[0], buffer[1],
546                         buffer[2], buffer[3], buffer[4], buffer[5]);
547         if (buffer[1] != 'I' || buffer[2] != 'T' || buffer[3] != 'E') {
548                 //      return false;
549         }
550
551         // Get firmware information
552         do {
553                 i2cReadFromIt7260(ts->client, 0x80, &ucQuery, 1);
554         } while (ucQuery & 0x01);
555         buffer[0] = 0x01;
556         buffer[1] = 0x00;
557         i2cWriteToIt7260(ts->client, 0x20, buffer, 2);
558         do {
559                 i2cReadFromIt7260(ts->client, 0x80, &ucQuery, 1);
560         } while (ucQuery & 0x01);
561         memset(&buffer, 0, sizeof(buffer));
562         i2cReadFromIt7260(ts->client, 0xA0, buffer, 8);
563         tmp = 0;
564         //for (i = 5; i < 9; i++) {
565         for (i = 5; i < 8; i++) {
566                 tmp += buffer[i];
567         }
568         if (tmp == 0) {
569                 //      return false;
570         }
571
572         //// Reinitialize Firmware
573         //set_ite_i2c_nostop(1);
574         //do {
575         //      ucQuery = i2c_smbus_read_byte_data(ts->client, 0x80);
576         //} while (ucQuery & 0x01);
577         //buffer[0] = 0x6F;
578         //set_ite_i2c_nostop(0);
579         //i2c_smbus_write_byte_data(ts->client, 0x20, buffer[0]);
580
581         return true;
582 }
583
584 static struct i2c_driver IT7260_ts_driver = { .probe = IT7260_ts_probe,
585                 .remove = IT7260_ts_remove,
586 #ifndef CONFIG_HAS_EARLYSUSPEND
587                 .suspend = IT7260_ts_suspend, .resume = IT7260_ts_resume,
588 #endif
589                 .id_table = IT7260_ts_id, .driver = { .name = "IT7260-ts", }, };
590
591 struct ite7260_data {
592         rwlock_t lock;
593         unsigned short bufferIndex;
594         unsigned short length;
595         unsigned short buffer[MAX_BUFFER_SIZE];
596 };
597
598 int ite7260_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
599                 unsigned long arg) {
600         struct ite7260_data *dev = filp->private_data;
601         int retval = 0;
602         int i;
603         unsigned char ucQuery;
604         unsigned char buffer[MAX_BUFFER_SIZE];
605         struct ioctl_cmd168 data;
606         unsigned char datalen;
607         unsigned char ent[] = {0x60, 0x00, 0x49, 0x54, 0x37, 0x32};
608         unsigned char ext[] = {0x60, 0x80, 0x49, 0x54, 0x37, 0x32};
609
610         //pr_info("=ite7260_ioctl=\n");
611         memset(&data, 0, sizeof(struct ioctl_cmd168));
612
613         switch (cmd) {
614         case IOCTL_SET:
615                 //pr_info("=IOCTL_SET=\n");
616                 if (!access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd))) {
617                         retval = -EFAULT;
618                         goto done;
619                 }
620
621                 if ( copy_from_user(&data, (int __user *)arg, sizeof(struct ioctl_cmd168)) ) {
622                         retval = -EFAULT;
623                         goto done;
624                 }
625                 buffer[0] = (unsigned char) data.bufferIndex;
626                 //pr_info("%.2X ", buffer[0]);
627                 for (i = 1; i < data.length + 1; i++) {
628                         buffer[i] = (unsigned char) data.buffer[i - 1];
629                         //pr_info("%.2X ", buffer[i]);
630                 }
631         if (!memcmp(&(buffer[1]), ent, sizeof(ent))) {
632
633                 pr_info("Disabling IRQ.\n");
634
635                 disable_irq(gl_ts->client->irq);
636
637         }
638
639         if (!memcmp(&(buffer[1]), ext, sizeof(ext))) {
640
641                 pr_info("Enabling IRQ.\n");
642
643                 enable_irq(gl_ts->client->irq);
644
645         }
646
647                 //pr_info("=================================================\n");
648                 //pr_info("name[%s]---addr[%x]-flags[%d]=\n",gl_ts->client->name,gl_ts->client->addr,gl_ts->client->flags);
649                 datalen = (unsigned char) (data.length + 1);
650                 //pr_info("datalen=%d\n", datalen);
651                 //write_lock(&dev->lock);
652                 retval = i2cWriteToIt7260(gl_ts->client,
653                                 (unsigned char) data.bufferIndex, &(buffer[1]), datalen - 1);
654                 //write_unlock(&dev->lock);
655                 //pr_info("SET:retval=%x\n", retval);
656                 retval = 0;
657                 break;
658
659         case IOCTL_GET:
660                 //pr_info("=IOCTL_GET=\n");
661                 if (!access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd))) {
662                         retval = -EFAULT;
663                         goto done;
664                 }
665
666                 if (!access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd))) {
667                         retval = -EFAULT;
668                         goto done;
669                 }
670
671                 //pr_info("sizeof(struct ioctl_cmd168)=%d\n", sizeof(struct ioctl_cmd168));
672                 if ( copy_from_user(&data, (int __user *)arg, sizeof(struct ioctl_cmd168)) ) {
673                         retval = -EFAULT;
674                         goto done;
675                 }
676
677                 //pr_info("=================================================\n");
678                 //pr_info("name[%s]---addr[%x]-flags[%d]=\n",gl_ts->client->name,gl_ts->client->addr,gl_ts->client->flags);
679                 //read_lock(&dev->lock);
680                 retval = i2cReadFromIt7260(gl_ts->client,
681                                 (unsigned char) data.bufferIndex, (unsigned char*) buffer,
682                                 (unsigned char) data.length);
683                 //read_unlock(&dev->lock);
684                 //pr_info("GET:retval=%x\n", retval);
685                 retval = 0;
686                 for (i = 0; i < data.length; i++) {
687                         data.buffer[i] = (unsigned short) buffer[i];
688                 }
689                 //pr_info("GET:bufferIndex=%x, dataLength=%d, buffer[0]=%x, buffer[1]=%x, buffer[2]=%x, buffer[3]=%x\n", data.bufferIndex, data.length, buffer[0], buffer[1], buffer[2], buffer[3]);
690                 //pr_info("GET:bufferIndex=%x, dataLength=%d, buffer[0]=%x, buffer[1]=%x, buffer[2]=%x, buffer[3]=%x\n", data.bufferIndex, data.length, data.buffer[0], data.buffer[1], data.buffer[2], data.buffer[3]);
691                 //if (data.bufferIndex == 0x80)
692                 //      data.buffer[0] = 0x00;
693                 if ( copy_to_user((int __user *)arg, &data, sizeof(struct ioctl_cmd168)) ) {
694                         retval = -EFAULT;
695                         goto done;
696                 }
697                 break;
698
699         default:
700                 retval = -ENOTTY;
701                 break;
702         }
703
704         done:
705         //pr_info("DONE! retval=%d\n", retval);
706         return (retval);
707 }
708
709 int ite7260_open(struct inode *inode, struct file *filp) {
710         int i;
711         struct ite7260_data *dev;
712
713         pr_info("=ite7260_open=\n");
714         dev = kmalloc(sizeof(struct ite7260_data), GFP_KERNEL);
715         if (dev == NULL) {
716                 return -ENOMEM;
717         }
718
719         /* initialize members */
720         rwlock_init(&dev->lock);
721         for (i = 0; i < MAX_BUFFER_SIZE; i++) {
722                 dev->buffer[i] = 0xFF;
723         }
724
725         filp->private_data = dev;
726
727         return 0; /* success */
728 }
729
730 int ite7260_close(struct inode *inode, struct file *filp) {
731         struct ite7260_data *dev = filp->private_data;
732
733         if (dev) {
734                 kfree(dev);
735         }
736
737         return 0; /* success */
738 }
739
740 struct file_operations ite7260_fops = { .owner = THIS_MODULE, .open =
741                 ite7260_open, .release = ite7260_close, .ioctl = ite7260_ioctl, };
742
743 static int __devinit IT7260_ts_init(void) {
744         dev_t dev = MKDEV(ite7260_major, 0);
745         int alloc_ret = 0;
746         int cdev_err = 0;
747         int input_err = 0;
748         struct device *class_dev = NULL;
749
750         DBG();
751
752         //      if(!IT7260_Init()) {
753         //              TS_DEBUG("IT7260 cannot be connected or is in firmware upgrade mode.\n");
754         //              goto error;
755         //      }
756
757         alloc_ret = alloc_chrdev_region(&dev, 0, 1, DEVICE_NAME);
758         if (alloc_ret) {
759                 TS_DEBUG("IT7260 cdev can't get major number\n");
760                 goto error;
761         }
762         ite7260_major = MAJOR(dev);
763
764         // allocate the character device
765         cdev_init(&ite7260_cdev, &ite7260_fops);
766         ite7260_cdev.owner = THIS_MODULE;
767         ite7260_cdev.ops = &ite7260_fops;
768         cdev_err = cdev_add(&ite7260_cdev, MKDEV(ite7260_major, ite7260_minor), 1);
769         if(cdev_err) {
770                 goto error;
771         }
772
773         // register class
774         ite7260_class = class_create(THIS_MODULE, DEVICE_NAME);
775         if(IS_ERR(ite7260_class)) {
776                 TS_DEBUG("Err: failed in creating class.\n");
777                 goto error;
778         }
779
780         ite7260_dev = MKDEV(ite7260_major, ite7260_minor);
781         class_dev = device_create(ite7260_class, NULL, ite7260_dev, NULL, DEVICE_NAME);
782         if(class_dev == NULL)
783         {
784                 TS_DEBUG("Err: failed in creating device.\n");
785                 goto error;
786         }
787         TS_DEBUG("=========================================\n");
788         TS_DEBUG("register IT7260 cdev, major: %d, minor: %d \n", ite7260_major, ite7260_minor);
789         TS_DEBUG("=========================================\n");
790
791         input_dev = input_allocate_device();
792         if (input_dev == NULL) {
793                 input_err = -ENOMEM;
794                 printk(KERN_ERR "IT7260_ts_probe: Failed to allocate input device\n");
795                 goto error;
796         }
797         input_dev->name = "IT7260";
798         input_dev->phys = "I2C";
799         input_dev->id.bustype = BUS_I2C;
800         input_dev->id.vendor = 0x0001;
801         input_dev->id.product = 0x7260;
802         //set_bit(EV_SYN, input_dev->evbit);
803         //set_bit(EV_KEY, input_dev->evbit);
804         set_bit(EV_ABS, input_dev->evbit);
805         //set_bit(BTN_TOUCH, input_dev->keybit);
806         //set_bit(BTN_2, input_dev->keybit);
807
808         input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, 1024, 0, 0);
809         input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, 600, 0, 0);
810         input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 2, 0, 0);
811         input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, 15, 0, 0);
812         input_set_abs_params(input_dev, ABS_MT_TRACKING_ID, 0, 2, 0, 0);
813         input_set_abs_params(input_dev, ABS_X, 0, 1024, 0, 0);
814         input_set_abs_params(input_dev, ABS_Y, 0, 600, 0, 0);
815         input_set_abs_params(input_dev, ABS_PRESSURE, 0, 1, 0, 0);
816         input_err = input_register_device(input_dev);
817         if (input_err) goto error;
818         pr_info("it7260 driver is on###############################################################\n");
819
820         return i2c_add_driver(&IT7260_ts_driver);
821
822         error:
823         if(cdev_err == 0) {
824                 cdev_del(&ite7260_cdev);
825         }
826         if(alloc_ret == 0) {
827                 unregister_chrdev_region(dev, 1);
828         }
829         if(input_dev) {
830                 input_free_device(input_dev);
831         }
832         if (IT7260_wq)
833         destroy_workqueue(IT7260_wq);
834
835         return -1;
836 }
837
838 static void __exit IT7260_ts_exit(void) {
839         dev_t dev = MKDEV(ite7260_major, ite7260_minor);
840
841         // unregister class
842         device_destroy(ite7260_class, ite7260_dev);
843         class_destroy(ite7260_class);
844
845         // unregister driver handle
846         cdev_del(&ite7260_cdev);
847         unregister_chrdev_region(dev, 1);
848
849         i2c_del_driver(&IT7260_ts_driver);
850         if (IT7260_wq)
851         destroy_workqueue(IT7260_wq);
852 }
853
854 module_init( IT7260_ts_init);
855 module_exit( IT7260_ts_exit);
856
857 MODULE_DESCRIPTION("IT7260 Touchscreen Driver");
858 MODULE_LICENSE("GPL");