1 /* drivers/input/touchscreen/gt818_ts.c
3 * Copyright (C) 2011 Rockcip, Inc.
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.
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.
14 * Author: hhb@rock-chips.com
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/time.h>
20 #include <linux/delay.h>
21 #include <linux/device.h>
22 #include <linux/earlysuspend.h>
23 #include <linux/hrtimer.h>
24 #include <linux/i2c.h>
25 #include <linux/input.h>
26 #include <linux/interrupt.h>
28 #include <linux/platform_device.h>
30 #include <linux/gpio.h>
31 #include <mach/iomux.h>
33 #include <linux/irq.h>
34 #include <linux/syscalls.h>
35 #include <linux/reboot.h>
36 #include <linux/proc_fs.h>
38 #include <linux/vmalloc.h>
40 #include <linux/string.h>
41 #include <linux/completion.h>
42 #include <asm/uaccess.h>
48 #if !defined(GT801_PLUS) && !defined(GT801_NUVOTON)
49 #error The code does not match this touchscreen.
52 static struct workqueue_struct *goodix_wq;
54 static const char *gt818_ts_name = "Goodix Capacitive TouchScreen";
56 static struct point_queue finger_list;
58 struct i2c_client * i2c_connect_client = NULL;
60 //EXPORT_SYMBOL(i2c_connect_client);
62 static struct proc_dir_entry *goodix_proc_entry;
64 #ifdef CONFIG_HAS_EARLYSUSPEND
65 static void goodix_ts_early_suspend(struct early_suspend *h);
66 static void goodix_ts_late_resume(struct early_suspend *h);
70 const uint16_t gt818_key_array[]={
76 #define MAX_KEY_NUM (sizeof(gt818_key_array)/sizeof(gt818_key_array[0]))
80 /*Function as i2c_master_send */
81 static int i2c_read_bytes(struct i2c_client *client, u8 *buf, int len)
83 struct i2c_msg msgs[2];
86 msgs[0].addr = client->addr;
87 msgs[0].flags = client->flags;
89 msgs[0].buf = &buf[0];
90 msgs[0].scl_rate = GT818_I2C_SCL;
91 msgs[0].udelay = client->udelay;
93 msgs[1].addr = client->addr;
94 msgs[1].flags = client->flags | I2C_M_RD;
96 msgs[1].buf = &buf[2];
97 msgs[1].scl_rate = GT818_I2C_SCL;
98 msgs[1].udelay = client->udelay;
100 ret = i2c_transfer(client->adapter, msgs, 2);
102 printk("%s:i2c_transfer fail =%d\n",__func__, ret);
106 /*Function as i2c_master_send */
107 static int i2c_write_bytes(struct i2c_client *client,u8 *data,int len)
112 msg.addr = client->addr;
113 msg.flags = client->flags; //鍐欐秷鎭�
116 msg.scl_rate = GT818_I2C_SCL;
117 msg.udelay = client->udelay;
118 ret = i2c_transfer(client->adapter, &msg, 1);
120 printk("%s:i2c_transfer fail =%d\n",__func__, ret);
125 static int i2c_pre_cmd(struct gt818_ts_data *ts)
128 u8 pre_cmd_data[2] = {0};
129 pre_cmd_data[0] = 0x0f;
130 pre_cmd_data[1] = 0xff;
131 ret = i2c_write_bytes(ts->client,pre_cmd_data,2);
137 static int i2c_end_cmd(struct gt818_ts_data *ts)
140 u8 end_cmd_data[2] = {0};
141 end_cmd_data[0] = 0x80;
142 end_cmd_data[1] = 0x00;
143 ret = i2c_write_bytes(ts->client,end_cmd_data,2);
150 static int goodix_init_panel(struct gt818_ts_data *ts)
157 0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,
158 0x10,0x12,0x00,0x00,0x10,0x00,0x20,0x00,
159 0x30,0x00,0x40,0x00,0x50,0x00,0x60,0x00,
160 0xE0,0x00,0xD0,0x00,0xC0,0x00,0xB0,0x00,
161 0xA0,0x00,0x90,0x00,0x80,0x00,0x70,0x00,
162 0x00,0x00,0x01,0x13,0x90,0x90,0x90,0x38,
163 0x38,0x38,0x0F,0x0E,0x0A,0x40 ,0x30,0x00,
164 0x3f,00,MAX_FINGER_NUM,0x00,0x14,0x00,0x1C,0x01,
165 0x01,0x3E,0x35,0x68,0x58,0x00,0x00,0x06,
166 0x19,0x05,0x00,0x00,0x00,0x00,0x00,0x00,
167 0x14,0x10,0x51,0x02,0x00,0x00,0x00,0x00,
168 0x00,0x00,0x20,0x40,0x60,0x90,0x08,0x40,
169 0x30,0x32,0x20,0x00,0x00,0x00,0x00,0x00,
177 0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,
178 0x10,0x12,0x00,0x00,0x10,0x00,0x20,0x00,
179 0x30,0x00,0x40,0x00,0x50,0x00,0x60,0x00,
180 0xE0,0x00,0xD0,0x00,0xC0,0x00,0xB0,0x00,
181 0xA0,0x00,0x90,0x00,0x80,0x00,0x70,0x00,
182 0x00,0x00,0x11,0x13,0x80,0x80,0x80,0x12,
183 0x12,0x12,0x0F,0x0F,0x0A,0x50,0x3C,0x49,
184 0x03,0x00,0x05,0x00,0x14,0xFA,0x1B,0x00,
185 0x00,0x4D,0x3D,0x81,0x65,0x00,0x00,0x06,
186 0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
187 0x14,0x10,0xAB,0x02,0x00,0x00,0x00,0x00,
188 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,
189 0x00,0x3C,0x28,0x00,0x00,0x00,0x00,0x00,
194 ret = i2c_write_bytes(ts->client, config_info, (sizeof(config_info)/sizeof(config_info[0])));
204 static int goodix_read_version(struct gt818_ts_data *ts)
207 u8 version_data[5] = {0}; //store touchscreen version infomation
208 memset(version_data, 0, 5);
209 version_data[0] = 0x07;
210 version_data[1] = 0x17;
212 ret = i2c_read_bytes(ts->client, version_data, 4);
215 dev_info(&ts->client->dev," Guitar Version: %d.%d\n",version_data[3],version_data[2]);
222 static void goodix_ts_work_func(struct work_struct *work)
224 u8 touch_status[8*MAX_FINGER_NUM + 18] = {READ_TOUCH_ADDR_H, READ_TOUCH_ADDR_L, 0};
225 u8 *key_value = NULL;
226 u8 *point_data = NULL;
227 static u8 finger_last[MAX_FINGER_NUM + 1]={0};
228 u8 finger_current[MAX_FINGER_NUM + 1] = {0};
229 u8 coor_data[6*MAX_FINGER_NUM] = {0};
230 static u8 last_key = 0;
235 unsigned int count = 0;
236 unsigned int position = 0;
239 int x = 0, y = 0 , pressure;
245 struct gt818_ts_data *ts = container_of(work, struct gt818_ts_data, work);
247 ret = i2c_read_bytes(ts->client, touch_status, sizeof(touch_status)/sizeof(touch_status[0]));
250 for(retry = 0; retry < 3; retry++)
252 ret = i2c_pre_cmd(ts);
259 dev_err(&(ts->client->dev),"I2C transfer error. Number:%d\n ", ret);
265 i2c_read_bytes(ts->client, touch_status, sizeof(touch_status)/sizeof(touch_status[0]));
268 //judge whether the data is ready
269 if((touch_status[2] & 0x30) != 0x20)
271 printk("%s:DATA_NO_READY\n", __func__);
274 //judge whether it is large area touch
275 if(touch_status[13] & 0x0f)
281 finger = touch_status[2] & 0x07;
282 key_value = touch_status + 15;
283 key = key_value[2] & 0x0f;
287 point_data = key_value + 3;
289 for(position = 0; position < (finger*8); position += 8)
291 temp = point_data[position];
292 //printk("track:%d\n", temp);
293 if(temp < (MAX_FINGER_NUM + 1))
295 finger_current[temp] = 1;
296 for(count = 0; count < 6; count++)
298 coor_data[(temp - 1) * 6 + count] = point_data[position+1+count];
303 //dev_err(&(ts->client->dev),"Track Id error:%d\n ",);
314 for(position = 1; position < MAX_FINGER_NUM+1; position++)
316 finger_current[position] = 0;
320 coor_point = (u16 *)coor_data;
322 for(position = 1; position < MAX_FINGER_NUM + 1; position++)
324 //printk("%s:positon:%d\n", __func__, position);
325 if((finger_current[position] == 0) && (finger_last[position] != 0))
327 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, 0);
328 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, 0);
329 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0);
330 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0);
331 input_mt_sync(ts->input_dev);
334 else if(finger_current[position])
337 x = (*(coor_point+3*(position-1)))*SCREEN_MAX_WIDTH/(TOUCH_MAX_WIDTH);
338 y = (*(coor_point+3*(position-1)+1))*SCREEN_MAX_HEIGHT/(TOUCH_MAX_HEIGHT);
339 pressure = (*(coor_point+3*(position-1)+2));
340 if(x < SCREEN_MAX_WIDTH){
341 x = SCREEN_MAX_WIDTH - x;
344 if(y < SCREEN_MAX_HEIGHT){
345 // y = SCREEN_MAX_HEIGHT-y;
347 input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, position - 1);
348 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 1);
349 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
350 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
351 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, pressure);
352 input_mt_sync(ts->input_dev);
358 #ifdef HAVE_TOUCH_KEY
359 if((last_key == 0) && (key == 0))
382 input_report_key(ts->input_dev, gt818_key_array[key - 1], 1);
385 input_report_key(ts->input_dev, gt818_key_array[last_key - 1], 0);
395 input_sync(ts->input_dev);
398 for(position = 1; position < MAX_FINGER_NUM + 1; position++)
400 finger_last[position] = finger_current[position];
407 enable_irq(ts->client->irq);
412 static enum hrtimer_restart goodix_ts_timer_func(struct hrtimer *timer)
414 struct gt818_ts_data *ts = container_of(timer, struct gt818_ts_data, timer);
415 queue_work(goodix_wq, &ts->work);
416 hrtimer_start(&ts->timer, ktime_set(0, (POLL_TIME+6)*1000000), HRTIMER_MODE_REL);
417 return HRTIMER_NORESTART;
421 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
423 struct gt818_ts_data *ts = dev_id;
424 disable_irq_nosync(ts->client->irq);
425 queue_work(goodix_wq, &ts->work);
429 static int goodix_ts_power(struct gt818_ts_data * ts, int on)
432 struct gt818_platform_data *pdata = ts->client->dev.platform_data;
433 unsigned char i2c_control_buf[3] = {0x06,0x92,0x01}; //suspend cmd
434 if(ts != NULL && !ts->use_irq)
440 // set the io port high level to avoid level change which might stop gt818 from sleeping
441 gpio_direction_output(pdata->gpio_reset, 1);
442 gpio_direction_output(pdata->gpio_pendown, 1);
444 ret = i2c_write_bytes(ts->client, i2c_control_buf, 3);
447 printk(KERN_INFO"**gt818 suspend fail**\n");
451 printk(KERN_INFO"**gt818 suspend**\n");
459 gpio_pull_updown(pdata->gpio_pendown, 1);
460 gpio_direction_output(pdata->gpio_pendown, 0);
462 gpio_direction_output(pdata->gpio_pendown, 1);
464 gpio_direction_input(pdata->gpio_pendown);
465 gpio_pull_updown(pdata->gpio_pendown, 0);
467 gpio_direction_output(pdata->gpio_reset, 0);
469 gpio_direction_input(pdata->gpio_reset);
470 gpio_pull_updown(pdata->gpio_reset, 0);
473 ret = i2c_pre_cmd(ts);
475 printk(KERN_INFO"**gt818 resume**\n");
478 printk(KERN_INFO"**gt818 resume fail**\n");
484 printk(KERN_DEBUG "%s: Cant't support this command.", gt818_ts_name);
491 static int goodix_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
495 u8 goodix_id[3] = {0,0xff,0};
496 struct gt818_ts_data *ts;
498 struct gt818_platform_data *pdata;
499 dev_dbg(&client->dev,"Install touch driver.\n");
500 printk("gt818: Install touch driver.\n");
502 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
504 dev_err(&client->dev, "Must have I2C_FUNC_I2C.\n");
506 goto err_check_functionality_failed;
509 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
512 goto err_alloc_data_failed;
515 i2c_connect_client = client; //used by Guitar_Update
516 pdata = client->dev.platform_data;
518 i2c_set_clientdata(client, ts);
520 //init int and reset ports
521 ret = gpio_request(pdata->gpio_pendown, "TS_INT"); //Request IO
523 dev_err(&client->dev, "Failed to request GPIO:%d, ERRNO:%d\n",(int)pdata->gpio_pendown, ret);
524 goto err_gpio_request_failed;
526 rk29_mux_api_set(pdata->pendown_iomux_name, pdata->pendown_iomux_mode);
527 gpio_direction_input(pdata->gpio_pendown);
528 gpio_pull_updown(pdata->gpio_pendown, 0);
530 ret = gpio_request(pdata->gpio_reset, "gt818_resetPin");
532 dev_err(&client->dev, "failed to request resetPin GPIO%d\n", pdata->gpio_reset);
533 goto err_gpio_request_failed;
535 rk29_mux_api_set(pdata->resetpin_iomux_name, pdata->resetpin_iomux_mode);
537 for(retry = 0; retry < 4; retry++)
539 gpio_pull_updown(pdata->gpio_reset, 1);
540 gpio_direction_output(pdata->gpio_reset, 0);
542 gpio_direction_input(pdata->gpio_reset);
543 gpio_pull_updown(pdata->gpio_reset, 0);
545 ret = i2c_pre_cmd(ts);
553 dev_err(&client->dev, "Warnning: I2C communication might be ERROR!\n");
558 for(retry = 0; retry < 3; retry++)
560 ret = goodix_init_panel(ts);
561 dev_info(&client->dev,"the config ret is :%d\n", ret);
563 if(ret < 0) //Initiall failed
571 goto err_init_godix_ts;
573 goodix_read_version(ts);
575 INIT_WORK(&ts->work, goodix_ts_work_func); //init work_struct
576 ts->input_dev = input_allocate_device();
577 if (ts->input_dev == NULL) {
579 dev_dbg(&client->dev,"goodix_ts_probe: Failed to allocate input device\n");
580 goto err_input_dev_alloc_failed;
583 ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;
584 ts->input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
585 ts->input_dev->absbit[0] = BIT_MASK(ABS_MT_POSITION_X) | BIT_MASK(ABS_MT_POSITION_Y) |
586 BIT_MASK(ABS_MT_TOUCH_MAJOR) | BIT_MASK(ABS_MT_WIDTH_MAJOR); // for android
589 #ifdef HAVE_TOUCH_KEY
590 for(retry = 0; retry < MAX_KEY_NUM; retry++)
592 input_set_capability(ts->input_dev, EV_KEY, gt818_key_array[retry]);
596 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&client->dev));
597 snprintf(ts->name, sizeof(ts->name), "gt818-touchscreen");
599 ts->input_dev->name = "gt818_ts";//ts->name;
600 ts->input_dev->phys = ts->phys;
601 ts->input_dev->dev.parent = &client->dev;
602 ts->input_dev->id.bustype = BUS_I2C;
603 ts->input_dev->id.vendor = 0xDEAD;
604 ts->input_dev->id.product = 0xBEEF;
605 ts->input_dev->id.version = 10427; //screen firmware version
607 #ifdef GOODIX_MULTI_TOUCH
609 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
610 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
611 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0, SCREEN_MAX_WIDTH, 0, 0);
612 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0, SCREEN_MAX_HEIGHT, 0, 0);
613 input_set_abs_params(ts->input_dev, ABS_MT_TRACKING_ID, 0, MAX_FINGER_NUM, 0, 0);
615 input_set_abs_params(ts->input_dev, ABS_X, 0, SCREEN_MAX_HEIGHT, 0, 0);
616 input_set_abs_params(ts->input_dev, ABS_Y, 0, SCREEN_MAX_WIDTH, 0, 0);
617 input_set_abs_params(ts->input_dev, ABS_PRESSURE, 0, 255, 0, 0);
620 ret = input_register_device(ts->input_dev);
622 dev_err(&client->dev,"Probe: Unable to register %s input device\n", ts->input_dev->name);
623 goto err_input_register_device_failed;
626 // finger_list.length = 0;
628 client->irq = gpio_to_irq(pdata->gpio_pendown); //If not defined in client
633 #define GT801_PLUS_IRQ_TYPE IRQ_TYPE_EDGE_RISING
635 #define GT801_PLUS_IRQ_TYPE IRQ_TYPE_EDGE_FALLING
637 #define GT801_PLUS_IRQ_TYPE IRQ_TYPE_LEVEL_LOW
639 #define GT801_PLUS_IRQ_TYPE IRQ_TYPE_LEVEL_HIGH
642 ret = request_irq(client->irq, goodix_ts_irq_handler, GT801_PLUS_IRQ_TYPE,
645 dev_err(&client->dev,"Cannot allocate ts INT!ERRNO:%d\n", ret);
646 gpio_direction_input(pdata->gpio_pendown);
647 gpio_free(pdata->gpio_pendown);
648 goto err_gpio_request_failed;
652 disable_irq(client->irq);
654 dev_dbg(&client->dev,"Reques EIRQ %d succesd on GPIO:%d\n", client->irq, pdata->gpio_pendown);
658 err_gpio_request_failed:
659 ts->power = goodix_ts_power;
660 #ifdef CONFIG_HAS_EARLYSUSPEND
661 ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
662 ts->early_suspend.suspend = goodix_ts_early_suspend;
663 ts->early_suspend.resume = goodix_ts_late_resume;
664 register_early_suspend(&ts->early_suspend);
666 dev_info(&client->dev,"Start %s in %s mode\n",
667 ts->input_dev->name, ts->use_irq ? "interrupt" : "polling");
671 enable_irq(client->irq);
675 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
676 ts->timer.function = goodix_ts_timer_func;
677 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
686 free_irq(client->irq,ts);
687 gpio_direction_input(pdata->gpio_pendown);
688 gpio_free(pdata->gpio_pendown);
691 hrtimer_cancel(&ts->timer);
693 err_input_register_device_failed:
694 input_free_device(ts->input_dev);
696 err_input_dev_alloc_failed:
697 i2c_set_clientdata(client, NULL);
700 err_alloc_data_failed:
701 err_check_functionality_failed:
702 err_create_proc_entry:
707 static int goodix_ts_remove(struct i2c_client *client)
709 struct gt818_ts_data *ts = i2c_get_clientdata(client);
710 struct gt818_platform_data *pdata = client->dev.platform_data;
712 #ifdef CONFIG_HAS_EARLYSUSPEND
713 unregister_early_suspend(&ts->early_suspend);
715 #ifdef CONFIG_TOUCHSCREEN_GOODIX_IAP
716 remove_proc_entry("goodix-update", NULL);
718 if (ts && ts->use_irq)
720 gpio_direction_input(pdata->gpio_pendown);
721 gpio_free(pdata->gpio_pendown);
722 free_irq(client->irq, ts);
725 hrtimer_cancel(&ts->timer);
727 dev_notice(&client->dev,"The driver is removing...\n");
728 i2c_set_clientdata(client, NULL);
729 input_unregister_device(ts->input_dev);
735 static int goodix_ts_suspend(struct i2c_client *client, pm_message_t mesg)
738 struct gt818_ts_data *ts = i2c_get_clientdata(client);
741 disable_irq(client->irq);
743 hrtimer_cancel(&ts->timer);
744 //ret = cancel_work_sync(&ts->work);
745 //if(ret && ts->use_irq)
746 //enable_irq(client->irq);
748 ret = ts->power(ts, 0);
750 printk(KERN_ERR "goodix_ts_resume power off failed\n");
756 static int goodix_ts_resume(struct i2c_client *client)
759 struct gt818_ts_data *ts = i2c_get_clientdata(client);
762 ret = ts->power(ts, 1);
764 printk(KERN_ERR "goodix_ts_resume power on failed\n");
768 enable_irq(client->irq);
770 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
775 #ifdef CONFIG_HAS_EARLYSUSPEND
776 static void goodix_ts_early_suspend(struct early_suspend *h)
778 struct gt818_ts_data *ts;
779 ts = container_of(h, struct gt818_ts_data, early_suspend);
780 goodix_ts_suspend(ts->client, PMSG_SUSPEND);
783 static void goodix_ts_late_resume(struct early_suspend *h)
785 struct gt818_ts_data *ts;
786 ts = container_of(h, struct gt818_ts_data, early_suspend);
787 goodix_ts_resume(ts->client);
793 static const struct i2c_device_id goodix_ts_id[] = {
794 { GOODIX_I2C_NAME, 0 },
799 static struct i2c_driver goodix_ts_driver = {
800 .probe = goodix_ts_probe,
801 .remove = goodix_ts_remove,
802 #ifndef CONFIG_HAS_EARLYSUSPEND
803 .suspend = goodix_ts_suspend,
804 .resume = goodix_ts_resume,
806 .id_table = goodix_ts_id,
808 .name = GOODIX_I2C_NAME,
809 .owner = THIS_MODULE,
814 static int __devinit goodix_ts_init(void)
817 goodix_wq = create_singlethread_workqueue("goodix_wq"); //create a work queue and worker thread
819 printk(KERN_ALERT "creat workqueue faiked\n");
822 ret = i2c_add_driver(&goodix_ts_driver);
827 static void __exit goodix_ts_exit(void)
829 printk(KERN_ALERT "Touchscreen driver of guitar exited.\n");
830 i2c_del_driver(&goodix_ts_driver);
832 destroy_workqueue(goodix_wq); //release our work queue
835 late_initcall(goodix_ts_init);
836 module_exit(goodix_ts_exit);
838 MODULE_DESCRIPTION("Goodix Touchscreen Driver");
839 MODULE_AUTHOR("hhb@rock-chips.com")
840 MODULE_LICENSE("GPL");