2 * drivers/input/touchscreen/gt801_ts.c
4 * Copyright (C) 2010 ROCKCHIP, Inc.
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/module.h>
18 #include <linux/delay.h>
19 #include <linux/earlysuspend.h>
20 #include <linux/hrtimer.h>
21 #include <linux/i2c.h>
22 #include <linux/input.h>
23 #include <linux/interrupt.h>
25 #include <linux/gpio.h>
26 #include <mach/iomux.h>
27 #include <linux/platform_device.h>
33 #define gt801printk(msg...) printk(msg);
35 #define gt801printk(msg...)
38 #define SINGLTOUCH_MODE 0
39 #define GT801_REGS_NUM 53
42 #define TOUCH_NUMBER 1
44 #define TOUCH_NUMBER 2
47 #define TOUCH_REG_NUM 5 //ÿ�������Ҫ�ļĴ�����Ŀ
49 const unsigned char GT801_RegData[GT801_REGS_NUM]={
50 0x19,0x05,0x06,0x28,0x02,0x14,0x14,0x10,0x40,0xB0,0x01,0xE0,0x03,0x4C,0x78,
51 0x9A,0xBC,0xDE,0x65,0x43,0x20,0x11,0x00,0x00,0x00,0x00,0x05,0xCF,0x20,0x0B,
52 0x0D,0x8D,0x32,0x3C,0x1E,0x28,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
53 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
56 struct gt801_ts_data {
58 bool swap_xy; /* swap x and y axes */
65 int gpio_reset_active_low;
66 int pendown_iomux_mode;
67 int resetpin_iomux_mode;
68 char pendown_iomux_name[IOMUX_NAME_SIZE];
69 char resetpin_iomux_name[IOMUX_NAME_SIZE];
72 struct i2c_client *client;
73 struct input_dev *input_dev;
75 struct work_struct work;
76 struct early_suspend early_suspend;
78 /*tochscreen private data*/
79 static int touch_state[TOUCH_NUMBER] = {TOUCH_UP,TOUCH_UP};
80 static struct workqueue_struct *gt801_wq;
82 #ifdef CONFIG_HAS_EARLYSUSPEND
83 static void gt801_ts_early_suspend(struct early_suspend *h);
84 static void gt801_ts_late_resume(struct early_suspend *h);
87 static int verify_coord(struct gt801_ts_data *ts,unsigned short *x,unsigned short *y)
90 gt801printk("%s:(%d/%d)\n",__FUNCTION__,*x, *y);
91 if((*x< ts->x_min) || (*x > ts->x_max))
94 if((*y< ts->y_min) || (*y > ts->y_max))
96 // *x = ts->x_max - *x;
103 /*read the gt801 register ,used i2c bus*/
104 static int gt801_read_regs(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
107 ret =i2c_master_reg8_recv(client, reg, buf, len, 200*1000);
109 printk("gt801_ts_work_func:i2c_transfer fail =%d\n",ret);
112 /* set the gt801 registe,used i2c bus*/
113 static int gt801_write_regs(struct i2c_client *client, u8 reg, u8 const buf[], unsigned short len)
116 ret = i2c_master_reg8_send(client,reg, buf, len, 200*1000);
118 printk("gt801_ts_work_func:i2c_transfer fail =%d\n",ret);
122 static int gt801_init_panel(struct gt801_ts_data *ts)
127 static void gt801_ts_work_func(struct work_struct *work)
132 int touch_state_index = 0;
135 unsigned char start_reg = 0x02;
136 unsigned char buf[TOUCH_NUMBER*TOUCH_REG_NUM];
141 int bufLen = TOUCH_NUMBER*TOUCH_REG_NUM;
143 struct gt801_ts_data *ts = container_of(work, struct gt801_ts_data, work);
145 gt801printk("%s\n",__FUNCTION__);
147 ret=gt801_read_regs(ts->client, start_reg, buf,bufLen);
149 printk("%s:i2c_transfer fail =%d\n",__FUNCTION__,ret);
151 enable_irq(ts->client->irq);
158 if(buf[i+ptpressure] == 0)
160 gt801printk(" realse ts_dev->point.x=%d ,ts_dev->point.y=%d \n",ts->point.x,ts->point.y);
162 if (touch_state[i] == TOUCH_DOWN)
164 input_report_key(ts->input_dev,BTN_TOUCH,0);
166 touch_state[i] = TOUCH_UP;
167 gt801printk("SINGLTOUCH_MODE up\n");
172 x = ((( ((unsigned short)buf[i+ptxh] )<< 8) ) | buf[i+ptxl]);
173 y= (((((unsigned short)buf[i+ptyh] )<< 8) )| buf[i+ptyl]);
178 if (verify_coord(ts,&x,&y))
181 if (touch_state[i] == TOUCH_UP)
183 gt801printk("SINGLTOUCH_MODE down\n");
184 input_report_key(ts->input_dev,BTN_TOUCH,1);
185 touch_state[i] = TOUCH_DOWN;
188 gt801printk("input_report_abs(%d/%d)\n",x,y);
189 input_report_abs(ts->input_dev,ABS_X,x );
190 input_report_abs(ts->input_dev,ABS_Y,y );
196 for(i=0; i<bufLen; i+=TOUCH_REG_NUM)
198 if(buf[i+ptpressure] == 0){
199 gt801printk("%s:buf=%d touch up\n",__FUNCTION__,buf[i+ptpressure]);
200 if (touch_state[touch_state_index] == TOUCH_DOWN)
202 gt801printk("%s:%d touch up\n",__FUNCTION__,i);
203 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0); //Finger Size
204 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0); //Touch Size
205 input_mt_sync(ts->input_dev);
207 touch_state[touch_state_index] = TOUCH_UP;
211 x = ((( ((unsigned short)buf[i+ptxh] )<< 8) ) | buf[i+ptxl]);
212 y = (((((unsigned short)buf[i+ptyh] )<< 8) )| buf[i+ptyl]);
213 /* adjust the x and y to proper value added by hhb@rock-chips.com*/
226 if (verify_coord(ts,&x,&y));//goto out;
228 gt801printk("input_report_abs--%d-%d-(%d/%d)\n", i,touch_state_index, x, y);
229 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 1); //Finger Size
230 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
231 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
232 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 5); //Touch Size
233 input_mt_sync(ts->input_dev);
235 touch_state[touch_state_index] = TOUCH_DOWN;
244 input_sync(ts->input_dev);
249 enable_irq(ts->client->irq);
253 static enum hrtimer_restart gt801_ts_timer_func(struct hrtimer *timer)
255 struct gt801_ts_data *ts = container_of(timer, struct gt801_ts_data, timer);
256 gt801printk("%s\n",__FUNCTION__);
258 queue_work(gt801_wq, &ts->work);
260 hrtimer_start(&ts->timer, ktime_set(0, 12500000), HRTIMER_MODE_REL);
261 return HRTIMER_NORESTART;
264 static irqreturn_t gt801_ts_irq_handler(int irq, void *dev_id)
266 struct gt801_ts_data *ts = dev_id;
267 gt801printk("%s=%d,%d\n",__FUNCTION__,ts->client->irq,ts->use_irq);
270 disable_irq_nosync(ts->client->irq);
272 queue_work(gt801_wq, &ts->work);
275 static int __devinit setup_resetPin(struct i2c_client *client, struct gt801_ts_data *ts)
277 struct gt801_platform_data *pdata = client->dev.platform_data;
280 ts->gpio_reset = pdata->gpio_reset;
281 ts->gpio_reset_active_low = pdata->gpio_reset_active_low;
282 ts->resetpin_iomux_mode = pdata->resetpin_iomux_mode;
284 if(pdata->resetpin_iomux_name != NULL)
285 strcpy(ts->resetpin_iomux_name,pdata->resetpin_iomux_name);
287 gt801printk("%s=%d,%s,%d,%d\n",__FUNCTION__,ts->gpio_reset,ts->resetpin_iomux_name,ts->resetpin_iomux_mode,ts->gpio_reset_active_low);
288 if (!gpio_is_valid(ts->gpio_reset)) {
289 dev_err(&client->dev, "no gpio_reset?\n");
293 rk29_mux_api_set(ts->resetpin_iomux_name,ts->resetpin_iomux_mode);
295 err = gpio_request(ts->gpio_reset, "gt801_resetPin");
297 dev_err(&client->dev, "failed to request resetPin GPIO%d\n",
302 err = gpio_direction_output(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_LOW:GPIO_HIGH);
304 dev_err(&client->dev, "failed to pulldown resetPin GPIO%d,err%d\n",
306 gpio_free(ts->gpio_reset);
310 gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
316 static int __devinit setup_pendown(struct i2c_client *client, struct gt801_ts_data *ts)
319 struct gt801_platform_data *pdata = client->dev.platform_data;
322 dev_dbg(&client->dev, "no IRQ?\n");
326 if (!gpio_is_valid(pdata->gpio_pendown)) {
327 dev_err(&client->dev, "no gpio_pendown?\n");
331 ts->gpio_pendown = pdata->gpio_pendown;
332 strcpy(ts->pendown_iomux_name,pdata->pendown_iomux_name);
333 ts->pendown_iomux_mode = pdata->pendown_iomux_mode;
335 gt801printk("%s=%d,%s,%d\n",__FUNCTION__,ts->gpio_pendown,ts->pendown_iomux_name,ts->pendown_iomux_mode);
337 if (!gpio_is_valid(ts->gpio_pendown)) {
338 dev_err(&client->dev, "no gpio_pendown?\n");
342 rk29_mux_api_set(ts->pendown_iomux_name,ts->pendown_iomux_mode);
343 err = gpio_request(ts->gpio_pendown, "gt801_pendown");
345 dev_err(&client->dev, "failed to request pendown GPIO%d\n",
350 err = gpio_pull_updown(ts->gpio_pendown, GPIOPullUp);
352 dev_err(&client->dev, "failed to pullup pendown GPIO%d\n",
354 gpio_free(ts->gpio_pendown);
360 static int gt801_chip_Init(struct i2c_client *client)
365 u8 buf[GT801_REGS_NUM];
367 gt801printk("enter gt801_chip_Init!!!!\n");
371 ret=gt801_write_regs(client,start_reg, GT801_RegData,GT801_REGS_NUM);
374 printk("\n--%s--Set Register values error !!!\n",__FUNCTION__);
377 ret=gt801_read_regs(client, start_reg, buf,GT801_REGS_NUM);
380 printk("\n--%s--Read Register values error !!!\n",__FUNCTION__);
383 for(i=0;i<GT801_REGS_NUM-1;i++)
385 if(buf[i]!=GT801_RegData[i])
387 printk("!!!!!!!!gt801_chip_Init err may be i2c errorat adress=%x var=%x i=%x\n",0x30+i, buf[i],i);
391 if(i==GT801_REGS_NUM-1)
403 static int gt801_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
405 struct gt801_ts_data *ts;
406 struct gt801_platform_data *pdata = client->dev.platform_data;
409 gt801printk("%s \n",__FUNCTION__);
412 dev_err(&client->dev, "empty platform_data\n");
413 goto err_check_functionality_failed;
415 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
416 printk(KERN_ERR "gt801_ts_probe: need I2C_FUNC_I2C\n");
418 goto err_check_functionality_failed;
421 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
424 goto err_alloc_data_failed;
426 INIT_WORK(&ts->work, gt801_ts_work_func);
428 i2c_set_clientdata(client, ts);
430 ret = setup_resetPin(client,ts);
433 printk("%s:setup_resetPin fail\n",__FUNCTION__);
434 goto err_input_dev_alloc_failed;
437 ret=gt801_chip_Init(ts->client);
440 printk("%s:chips init failed\n",__FUNCTION__);
441 goto err_resetpin_failed;
444 /* allocate input device */
445 ts->input_dev = input_allocate_device();
446 if (ts->input_dev == NULL) {
448 printk(KERN_ERR "%s: Failed to allocate input device\n",__FUNCTION__);
449 goto err_input_dev_alloc_failed;
452 ts->model = pdata->model ? : 801;
453 ts->swap_xy = pdata->swap_xy;
454 ts->x_min = pdata->x_min;
455 ts->x_max = pdata->x_max;
456 ts->y_min = pdata->y_min;
457 ts->y_max = pdata->y_max;
458 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&client->dev));
459 snprintf(ts->name, sizeof(ts->name), "gt%d-touchscreen", ts->model);
460 ts->input_dev->phys = ts->phys;
461 ts->input_dev->name = ts->name;
462 ts->input_dev->dev.parent = &client->dev;
465 ts->input_dev->evbit[0] = BIT_MASK(EV_ABS)|BIT_MASK(EV_KEY);
466 ts->input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
467 input_set_abs_params(ts->input_dev,ABS_X,
471 input_set_abs_params(ts->input_dev,ABS_Y,
477 ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_ABS);
478 // ts->input_dev->absbit[0] =
479 // BIT(ABS_MT_POSITION_X) | BIT(ABS_MT_POSITION_Y) |
480 // BIT(ABS_MT_TOUCH_MAJOR) | BIT(ABS_MT_WIDTH_MAJOR); // for android
481 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X,
485 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
489 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 1, 0, 0); //Finger Size
490 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 10, 0, 0); //Touch Size
492 ret = input_register_device(ts->input_dev);
494 printk(KERN_ERR "%s: Unable to register %s input device\n", __FUNCTION__,ts->input_dev->name);
495 goto err_input_register_device_failed;
498 client->irq = gpio_to_irq(client->irq);
500 ret = setup_pendown(client,ts);
503 printk("%s:setup_pendown fail\n",__FUNCTION__);
504 goto err_input_register_device_failed;
507 ret = request_irq(client->irq, gt801_ts_irq_handler, IRQF_DISABLED | IRQF_TRIGGER_LOW, client->name, ts);
509 gt801printk("%s:register ISR (irq=%d)\n", __FUNCTION__,client->irq);
513 dev_err(&client->dev, "request_irq failed\n");
517 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
518 ts->timer.function = gt801_ts_timer_func;
519 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
521 #ifdef CONFIG_HAS_EARLYSUSPEND
522 ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
523 ts->early_suspend.suspend = gt801_ts_early_suspend;
524 ts->early_suspend.resume = gt801_ts_late_resume;
525 register_early_suspend(&ts->early_suspend);
528 printk(KERN_INFO "%s: Start touchscreen %s in %s mode\n", __FUNCTION__,ts->input_dev->name, ts->use_irq ? "interrupt" : "polling");
532 err_input_register_device_failed:
533 input_free_device(ts->input_dev);
535 gpio_free(ts->gpio_reset);
536 err_input_dev_alloc_failed:
538 err_alloc_data_failed:
539 err_check_functionality_failed:
544 static int gt801_ts_remove(struct i2c_client *client)
546 struct gt801_ts_data *ts = i2c_get_clientdata(client);
547 unregister_early_suspend(&ts->early_suspend);
549 free_irq(client->irq, ts);
551 hrtimer_cancel(&ts->timer);
552 input_unregister_device(ts->input_dev);
553 gpio_free(ts->gpio_pendown);
554 gpio_free(ts->gpio_reset);
559 static int gt801_ts_suspend(struct i2c_client *client, pm_message_t mesg)
562 struct gt801_ts_data *ts = i2c_get_clientdata(client);
564 printk("gt801 TS Suspend\n");
567 disable_irq(client->irq);
569 hrtimer_cancel(&ts->timer);
571 ret = cancel_work_sync(&ts->work);
572 if (ret && ts->use_irq) /* if work was pending disable-count is now 2 */
573 enable_irq(client->irq);
575 gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_LOW:GPIO_HIGH);
581 static void gt801_ts_resume_work_func(struct work_struct *work)
583 struct gt801_ts_data *ts = container_of(work, struct gt801_ts_data, work);
584 msleep(50); //touch panel will generate an interrupt when it sleeps out,so as to avoid tihs by delaying 50ms
585 enable_irq(ts->client->irq);
586 PREPARE_WORK(&ts->work, gt801_ts_work_func);
587 printk("enabling gt801_ts IRQ %d\n", ts->client->irq);
591 static int gt801_ts_resume(struct i2c_client *client)
593 struct gt801_ts_data *ts = i2c_get_clientdata(client);
595 gt801_init_panel(ts);
597 printk("gt801 TS Resume\n");
599 gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
602 if(!work_pending(&ts->work)){
603 PREPARE_WORK(&ts->work, gt801_ts_resume_work_func);
604 queue_work(gt801_wq, &ts->work);
608 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
614 #ifdef CONFIG_HAS_EARLYSUSPEND
615 static void gt801_ts_early_suspend(struct early_suspend *h)
617 struct gt801_ts_data *ts;
618 ts = container_of(h, struct gt801_ts_data, early_suspend);
619 gt801_ts_suspend(ts->client, PMSG_SUSPEND);
622 static void gt801_ts_late_resume(struct early_suspend *h)
624 struct gt801_ts_data *ts;
625 ts = container_of(h, struct gt801_ts_data, early_suspend);
626 gt801_ts_resume(ts->client);
630 #define gt801_TS_NAME "gt801_ts"
632 static const struct i2c_device_id gt801_ts_id[] = {
633 { gt801_TS_NAME, 0 },
637 static struct i2c_driver gt801_ts_driver = {
638 .probe = gt801_ts_probe,
639 .remove = gt801_ts_remove,
640 #ifndef CONFIG_HAS_EARLYSUSPEND
641 .suspend = gt801_ts_suspend,
642 .resume = gt801_ts_resume,
644 .id_table = gt801_ts_id,
646 .name = gt801_TS_NAME,
650 static int __devinit gt801_ts_init(void)
652 printk("%s\n",__FUNCTION__);
653 gt801_wq = create_singlethread_workqueue("gt801_wq");
656 return i2c_add_driver(>801_ts_driver);
659 static void __exit gt801_ts_exit(void)
661 printk("%s\n",__FUNCTION__);
662 i2c_del_driver(>801_ts_driver);
664 destroy_workqueue(gt801_wq);
667 module_init(gt801_ts_init);
668 module_exit(gt801_ts_exit);
670 MODULE_DESCRIPTION("gt801 Touchscreen Driver");
671 MODULE_LICENSE("GPL");