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)
153 // unsigned char i2c_control_buf[3] = {0x06,0x92,0x03};
158 0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,
159 0x10,0x12,0x00,0x00,0x10,0x00,0x20,0x00,
160 0x30,0x00,0x40,0x00,0x50,0x00,0x60,0x00,
161 0xE0,0x00,0xD0,0x00,0xC0,0x00,0xB0,0x00,
162 0xA0,0x00,0x90,0x00,0x80,0x00,0x70,0x00,
163 0x00,0x00,0x01,0x13,0x90,0x90,0x90,0x38,
164 0x38,0x38,0x0F,0x0E,0x0A,0x40 ,0x30,0x00,
165 0x3f,00,MAX_FINGER_NUM,0x00,0x14,0x00,0x1C,0x01,
166 0x01,0x3E,0x35,0x68,0x58,0x00,0x00,0x06,
167 0x19,0x05,0x00,0x00,0x00,0x00,0x00,0x00,
168 0x14,0x10,0x51,0x02,0x00,0x00,0x00,0x00,
169 0x00,0x00,0x20,0x40,0x60,0x90,0x08,0x40,
170 0x30,0x32,0x20,0x00,0x00,0x00,0x00,0x00,
178 0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,
179 0x10,0x12,0x00,0x00,0x10,0x00,0x20,0x00,
180 0x30,0x00,0x40,0x00,0x50,0x00,0x60,0x00,
181 0xE0,0x00,0xD0,0x00,0xC0,0x00,0xB0,0x00,
182 0xA0,0x00,0x90,0x00,0x80,0x00,0x70,0x00,
183 0x00,0x00,0x11,0x13,0x80,0x80,0x80,0x12,
184 0x12,0x12,0x0F,0x0F,0x0A,0x50,0x3C,0x49,
185 0x03,0x00,0x05,0x00,0x14,0xFA,0x1B,0x00,
186 0x00,0x4D,0x3D,0x81,0x65,0x00,0x00,0x06,
187 0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
188 0x14,0x10,0xAB,0x02,0x00,0x00,0x00,0x00,
189 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,
190 0x00,0x3C,0x28,0x00,0x00,0x00,0x00,0x00,
195 // ret = i2c_write_bytes(ts->client, i2c_control_buf, (sizeof(i2c_control_buf)/sizeof(i2c_control_buf[0])));
196 ret = i2c_write_bytes(ts->client, config_info, (sizeof(config_info)/sizeof(config_info[0])));
206 static int goodix_read_version(struct gt818_ts_data *ts)
209 u8 version_data[5] = {0}; //store touchscreen version infomation
210 memset(version_data, 0, 5);
211 version_data[0] = 0x07;
212 version_data[1] = 0x17;
214 ret = i2c_read_bytes(ts->client, version_data, 4);
217 dev_info(&ts->client->dev," Guitar Version: %d.%d\n",version_data[3],version_data[2]);
224 static void goodix_ts_work_func(struct work_struct *work)
226 u8 touch_status[8*MAX_FINGER_NUM + 18] = {READ_TOUCH_ADDR_H, READ_TOUCH_ADDR_L, 0};
227 u8 *key_value = NULL;
228 u8 *point_data = NULL;
229 static u8 finger_last[MAX_FINGER_NUM + 1]={0};
230 u8 finger_current[MAX_FINGER_NUM + 1] = {0};
231 u8 coor_data[6*MAX_FINGER_NUM] = {0};
232 static u8 last_key = 0;
237 unsigned int count = 0;
238 unsigned int position = 0;
241 int x = 0, y = 0 , pressure;
247 struct gt818_ts_data *ts = container_of(work, struct gt818_ts_data, work);
249 ret = i2c_read_bytes(ts->client, touch_status, sizeof(touch_status)/sizeof(touch_status[0]));
252 for(retry = 0; retry < 3; retry++)
254 ret = i2c_pre_cmd(ts);
261 dev_err(&(ts->client->dev),"I2C transfer error. Number:%d\n ", ret);
267 i2c_read_bytes(ts->client, touch_status, sizeof(touch_status)/sizeof(touch_status[0]));
270 //judge whether the data is ready
271 if((touch_status[2] & 0x30) != 0x20)
273 printk("%s:DATA_NO_READY\n", __func__);
276 //judge whether it is large area touch
277 if(touch_status[13] & 0x0f)
283 finger = touch_status[2] & 0x07;
284 key_value = touch_status + 15;
285 key = key_value[2] & 0x0f;
289 point_data = key_value + 3;
291 for(position = 0; position < (finger*8); position += 8)
293 temp = point_data[position];
294 //printk("track:%d\n", temp);
295 if(temp < (MAX_FINGER_NUM + 1))
297 finger_current[temp] = 1;
298 for(count = 0; count < 6; count++)
300 coor_data[(temp - 1) * 6 + count] = point_data[position+1+count];
305 //dev_err(&(ts->client->dev),"Track Id error:%d\n ",);
316 for(position = 1; position < MAX_FINGER_NUM+1; position++)
318 finger_current[position] = 0;
322 coor_point = (u16 *)coor_data;
324 for(position = 1; position < MAX_FINGER_NUM + 1; position++)
326 //printk("%s:positon:%d\n", __func__, position);
327 if((finger_current[position] == 0) && (finger_last[position] != 0))
329 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, 0);
330 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, 0);
331 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0);
332 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0);
333 input_mt_sync(ts->input_dev);
336 else if(finger_current[position])
339 x = (*(coor_point+3*(position-1)))*SCREEN_MAX_WIDTH/(TOUCH_MAX_WIDTH);
340 y = (*(coor_point+3*(position-1)+1))*SCREEN_MAX_HEIGHT/(TOUCH_MAX_HEIGHT);
341 pressure = (*(coor_point+3*(position-1)+2));
342 if(x < SCREEN_MAX_WIDTH){
343 x = SCREEN_MAX_WIDTH - x;
346 if(y < SCREEN_MAX_HEIGHT){
347 // y = SCREEN_MAX_HEIGHT-y;
349 input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, position - 1);
350 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 1);
351 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
352 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
353 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, pressure);
354 input_mt_sync(ts->input_dev);
360 #ifdef HAVE_TOUCH_KEY
361 if((last_key == 0) && (key == 0))
384 input_report_key(ts->input_dev, gt818_key_array[key - 1], 1);
387 input_report_key(ts->input_dev, gt818_key_array[last_key - 1], 0);
397 input_sync(ts->input_dev);
400 for(position = 1; position < MAX_FINGER_NUM + 1; position++)
402 finger_last[position] = finger_current[position];
409 enable_irq(ts->client->irq);
414 static enum hrtimer_restart goodix_ts_timer_func(struct hrtimer *timer)
416 struct gt818_ts_data *ts = container_of(timer, struct gt818_ts_data, timer);
417 queue_work(goodix_wq, &ts->work);
418 hrtimer_start(&ts->timer, ktime_set(0, (POLL_TIME+6)*1000000), HRTIMER_MODE_REL);
419 return HRTIMER_NORESTART;
423 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
425 struct gt818_ts_data *ts = dev_id;
426 disable_irq_nosync(ts->client->irq);
427 queue_work(goodix_wq, &ts->work);
431 static int goodix_ts_power(struct gt818_ts_data * ts, int on)
434 struct gt818_platform_data *pdata = ts->client->dev.platform_data;
435 unsigned char i2c_control_buf[3] = {0x06,0x92,0x01}; //suspend cmd
436 unsigned char i2c_config_buf[3] = {0x06,0x92,0x01};
439 if(ts != NULL && !ts->use_irq)
446 // set the io port high level to avoid level change which might stop gt818 from sleeping
447 gpio_direction_output(pdata->gpio_reset, 1);
448 gpio_direction_output(pdata->gpio_pendown, 1);
450 ret = i2c_write_bytes(ts->client, i2c_control_buf, 3);
453 printk(KERN_INFO"**gt818 suspend fail**\n");
457 printk(KERN_INFO"**gt818 suspend**\n");
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
522 ret = gpio_request(INT_PORT, "TS_INT"); //Request IO
524 dev_err(&client->dev, "Failed to request GPIO:%d, ERRNO:%d\n",(int)INT_PORT, ret);
525 goto err_gpio_request_failed;
527 rk29_mux_api_set(pdata->pendown_iomux_name, pdata->pendown_iomux_mode);
528 gpio_direction_input(INT_PORT);
529 gpio_pull_updown(INT_PORT, 0);
532 ret = gpio_request(pdata->gpio_reset, "gt818_resetPin");
534 dev_err(&client->dev, "failed to request resetPin GPIO%d\n", pdata->gpio_reset);
535 goto err_gpio_request_failed;
537 rk29_mux_api_set(pdata->resetpin_iomux_name, pdata->resetpin_iomux_mode);
539 for(retry = 0; retry < 4; retry++)
541 gpio_direction_output(pdata->gpio_reset, 0);
543 gpio_direction_input(pdata->gpio_reset);
544 gpio_pull_updown(pdata->gpio_reset, 0);
546 ret = i2c_pre_cmd(ts);
554 dev_err(&client->dev, "Warnning: I2C communication might be ERROR!\n");
559 for(retry = 0; retry < 3; retry++)
561 ret = goodix_init_panel(ts);
562 dev_info(&client->dev,"the config ret is :%d\n", ret);
564 if(ret < 0) //Initiall failed
572 goto err_init_godix_ts;
574 goodix_read_version(ts);
576 INIT_WORK(&ts->work, goodix_ts_work_func); //init work_struct
577 ts->input_dev = input_allocate_device();
578 if (ts->input_dev == NULL) {
580 dev_dbg(&client->dev,"goodix_ts_probe: Failed to allocate input device\n");
581 goto err_input_dev_alloc_failed;
584 ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;
585 ts->input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
586 // ts->input_dev->absbit[0] = BIT(ABS_MT_POSITION_X) | BIT(ABS_MT_POSITION_Y) |
587 // BIT(ABS_MT_TOUCH_MAJOR) | BIT(ABS_MT_WIDTH_MAJOR); // for android
590 #ifdef HAVE_TOUCH_KEY
591 for(retry = 0; retry < MAX_KEY_NUM; retry++)
593 input_set_capability(ts->input_dev, EV_KEY, gt818_key_array[retry]);
597 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&client->dev));
598 snprintf(ts->name, sizeof(ts->name), "gt818-touchscreen");
600 ts->input_dev->name = "gt818_ts";//ts->name;
601 ts->input_dev->phys = ts->phys;
602 ts->input_dev->dev.parent = &client->dev;
603 ts->input_dev->id.bustype = BUS_I2C;
604 ts->input_dev->id.vendor = 0xDEAD;
605 ts->input_dev->id.product = 0xBEEF;
606 ts->input_dev->id.version = 10427; //screen firmware version
608 #ifdef GOODIX_MULTI_TOUCH
610 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
611 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
612 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0, SCREEN_MAX_WIDTH, 0, 0);
613 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0, SCREEN_MAX_HEIGHT, 0, 0);
614 input_set_abs_params(ts->input_dev, ABS_MT_TRACKING_ID, 0, MAX_FINGER_NUM, 0, 0);
616 input_set_abs_params(ts->input_dev, ABS_X, 0, SCREEN_MAX_HEIGHT, 0, 0);
617 input_set_abs_params(ts->input_dev, ABS_Y, 0, SCREEN_MAX_WIDTH, 0, 0);
618 input_set_abs_params(ts->input_dev, ABS_PRESSURE, 0, 255, 0, 0);
621 ret = input_register_device(ts->input_dev);
623 dev_err(&client->dev,"Probe: Unable to register %s input device\n", ts->input_dev->name);
624 goto err_input_register_device_failed;
627 // finger_list.length = 0;
631 client->irq = TS_INT; //If not defined in client
636 #define GT801_PLUS_IRQ_TYPE IRQ_TYPE_EDGE_RISING
638 #define GT801_PLUS_IRQ_TYPE IRQ_TYPE_EDGE_FALLING
640 #define GT801_PLUS_IRQ_TYPE IRQ_TYPE_LEVEL_LOW
642 #define GT801_PLUS_IRQ_TYPE IRQ_TYPE_LEVEL_HIGH
645 ret = request_irq(client->irq, goodix_ts_irq_handler, GT801_PLUS_IRQ_TYPE,
648 dev_err(&client->dev,"Cannot allocate ts INT!ERRNO:%d\n", ret);
649 gpio_direction_input(INT_PORT);
651 goto err_gpio_request_failed;
655 disable_irq(client->irq);
657 dev_dbg(&client->dev,"Reques EIRQ %d succesd on GPIO:%d\n",TS_INT,INT_PORT);
662 err_gpio_request_failed:
663 ts->power = goodix_ts_power;
664 #ifdef CONFIG_HAS_EARLYSUSPEND
665 ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
666 ts->early_suspend.suspend = goodix_ts_early_suspend;
667 ts->early_suspend.resume = goodix_ts_late_resume;
668 register_early_suspend(&ts->early_suspend);
670 dev_info(&client->dev,"Start %s in %s mode\n",
671 ts->input_dev->name, ts->use_irq ? "interrupt" : "polling");
675 enable_irq(client->irq);
679 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
680 ts->timer.function = goodix_ts_timer_func;
681 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
690 free_irq(client->irq,ts);
692 gpio_direction_input(INT_PORT);
697 hrtimer_cancel(&ts->timer);
699 err_input_register_device_failed:
700 input_free_device(ts->input_dev);
702 err_input_dev_alloc_failed:
703 i2c_set_clientdata(client, NULL);
706 err_alloc_data_failed:
707 err_check_functionality_failed:
708 err_create_proc_entry:
713 static int goodix_ts_remove(struct i2c_client *client)
715 struct gt818_ts_data *ts = i2c_get_clientdata(client);
716 struct gt818_platform_data *pdata = client->dev.platform_data;
718 #ifdef CONFIG_HAS_EARLYSUSPEND
719 unregister_early_suspend(&ts->early_suspend);
721 #ifdef CONFIG_TOUCHSCREEN_GOODIX_IAP
722 remove_proc_entry("goodix-update", NULL);
724 if (ts && ts->use_irq)
727 gpio_direction_input(INT_PORT);
730 free_irq(client->irq, ts);
733 hrtimer_cancel(&ts->timer);
735 dev_notice(&client->dev,"The driver is removing...\n");
736 i2c_set_clientdata(client, NULL);
737 input_unregister_device(ts->input_dev);
743 static int goodix_ts_suspend(struct i2c_client *client, pm_message_t mesg)
746 struct gt818_ts_data *ts = i2c_get_clientdata(client);
749 disable_irq(client->irq);
751 hrtimer_cancel(&ts->timer);
752 //ret = cancel_work_sync(&ts->work);
753 //if(ret && ts->use_irq)
754 //enable_irq(client->irq);
756 ret = ts->power(ts, 0);
758 printk(KERN_ERR "goodix_ts_resume power off failed\n");
764 static int goodix_ts_resume(struct i2c_client *client)
767 struct gt818_ts_data *ts = i2c_get_clientdata(client);
770 ret = ts->power(ts, 1);
772 printk(KERN_ERR "goodix_ts_resume power on failed\n");
776 enable_irq(client->irq);
778 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
783 #ifdef CONFIG_HAS_EARLYSUSPEND
784 static void goodix_ts_early_suspend(struct early_suspend *h)
786 struct gt818_ts_data *ts;
787 ts = container_of(h, struct gt818_ts_data, early_suspend);
788 goodix_ts_suspend(ts->client, PMSG_SUSPEND);
791 static void goodix_ts_late_resume(struct early_suspend *h)
793 struct gt818_ts_data *ts;
794 ts = container_of(h, struct gt818_ts_data, early_suspend);
795 goodix_ts_resume(ts->client);
801 static const struct i2c_device_id goodix_ts_id[] = {
802 { GOODIX_I2C_NAME, 0 },
807 static struct i2c_driver goodix_ts_driver = {
808 .probe = goodix_ts_probe,
809 .remove = goodix_ts_remove,
810 #ifndef CONFIG_HAS_EARLYSUSPEND
811 .suspend = goodix_ts_suspend,
812 .resume = goodix_ts_resume,
814 .id_table = goodix_ts_id,
816 .name = GOODIX_I2C_NAME,
817 .owner = THIS_MODULE,
822 static int __devinit goodix_ts_init(void)
825 goodix_wq = create_singlethread_workqueue("goodix_wq"); //create a work queue and worker thread
827 printk(KERN_ALERT "creat workqueue faiked\n");
830 ret = i2c_add_driver(&goodix_ts_driver);
835 static void __exit goodix_ts_exit(void)
837 printk(KERN_ALERT "Touchscreen driver of guitar exited.\n");
838 i2c_del_driver(&goodix_ts_driver);
840 destroy_workqueue(goodix_wq); //release our work queue
843 late_initcall(goodix_ts_init);
844 module_exit(goodix_ts_exit);
846 MODULE_DESCRIPTION("Goodix Touchscreen Driver");
847 MODULE_AUTHOR("hhb@rock-chips.com")
848 MODULE_LICENSE("GPL");