S5PV210(TQ210)学习笔记——触摸屏驱动编写

S5PV210(TQ210)学习笔记——触摸屏驱动编写


zzzz: http://blog.csdn.net/girlkoo/article/details/8756731

电阻式触摸屏的驱动比较简单,可以采用输入子系统驱动框架来编写,而电容式触摸屏的驱动程序相对比较复杂,因为电容触控一般采用I2C引脚才控制,我在自己编写电容触控驱动的时候郁闷了好几天,当然,并不是因为I2C电容触控驱动繁琐,主要是天嵌TQ210的触摸屏驱动程序是以模块方式提供的,并不开发源代码,也没有提供触控的芯片手册,我曾通过技术咨询群和电话咨询的方式咨询过天嵌相关人士,想跟他们索取触控协议而不要所谓的触控驱动程序源码,但是,天嵌相关人员以保密协议为由(尽管我完全不相信做LCD屏的商家会不告诉你怎么用屏)拒绝提供触控协议。我们不去追究这些无聊的问题,好在天嵌这样的公司(或者是与其合作开发的触摸屏驱动的公司)还没有实力自己做芯片,所以,只要找到触控芯片的型号并根据触控芯片型号找到对应的手册,然后就可以自己编写所谓的电容式触摸屏驱动了。

一 触控芯片分析

首先,卸下触摸屏的四个螺丝并翻过触摸屏来观察,可以在在触摸屏排线上看到触控芯片,仔细观察芯片型号(如果看不清可以用放大镜配合手电筒观看),我们可以看到,TQ210的触摸屏控制芯片是GT811,然后我找到了GT811的芯片手册(这些资料都上传到了我的CSDN资源里,请您支持一下),有了手册,编写驱动就不难了。

GT811引出了6根脚,分别是VCC、GND、I2CSDA、I2CSCL、INT和RESET,虽然INT脚不是必须的,但是开发高效省资源的触屏驱动程序往往都采用中断方式,下面是GT811的引脚图:

我用万能表实际测量了一下触控模块的各个引脚,实际线序是GND、SDA、SDL、INT、RESET和VDD。GT811的初始化顺序如下:

[cpp]  view plain copy
  1. (1) 初始化INT脚为悬浮输入态并初始化RESET脚为输出态,并输出低电平  
  2. (2) 延时1ms  
  3. (3) 初始化RESET脚为悬浮输入态,并使能上拉  
  4. (4) 写入GT811寄存器配置表  
  5. (5) 根据需要配置INT脚  
具体的操作可以参见代码部分。

二 I2C驱动编写

I2C驱动也是基于总线结构的,不过分为两种,一种是Legacy方式,另一种是New Style方式,其中,Legacy方式在新内核中已经不支持了,不过韦东山老师的视频中还是分析的Legacy方式,New Style方式你可以自己用Source Insight追踪分析一下,我这里就不多说了,具体的可以参考下面的代码。

[cpp]  view plain copy
  1. #include <linux/module.h>  
  2. #include <linux/input.h>  
  3. #include <linux/i2c.h>  
  4. #include <linux/gpio.h>  
  5. #include <linux/delay.h>  
  6. #include <linux/input.h>  
  7. #include <plat/gpio-cfg.h>  
  8. #include <linux/interrupt.h>  
  9.   
  10. const static unsigned short normal_address[] = {0x5d, I2C_CLIENT_END};  
  11. static unsigned gt811_rst;  
  12. static unsigned gt811_int;  
  13. static struct input_dev *ts_input;  
  14. static struct workqueue_struct *wq;  
  15. static struct work_struct work;  
  16.   
  17. static struct i2c_client * this_client = NULL;  
  18.   
  19. static unsigned int status = 0;  
  20.   
  21. static int i2c_read_bytes(struct i2c_client *client, uint8_t *buf, int len)  
  22. {  
  23.     struct i2c_msg msgs[2];  
  24.     int ret=-1;  
  25.       
  26.     msgs[0].flags=!I2C_M_RD;  
  27.     msgs[0].addr=client->addr;  
  28.     msgs[0].len=2;  
  29.     msgs[0].buf=&buf[0];  
  30.   
  31.     msgs[1].flags=I2C_M_RD;  
  32.     msgs[1].addr=client->addr;  
  33.     msgs[1].len=len-2;  
  34.     msgs[1].buf=&buf[2];  
  35.       
  36.     ret=i2c_transfer(client->adapter,msgs, 2);  
  37.     return ret;  
  38. }  
  39.   
  40. static int i2c_write_bytes(struct i2c_client *client,uint8_t *data,int len)  
  41. {  
  42.     struct i2c_msg msg;  
  43.     int ret=-1;  
  44.       
  45.     msg.flags=!I2C_M_RD;  
  46.     msg.addr=client->addr;  
  47.     msg.len=len;  
  48.     msg.buf=data;     
  49.       
  50.     ret=i2c_transfer(client->adapter,&msg, 1);  
  51.     return ret;  
  52. }  
  53.   
  54. static const struct i2c_device_id ts_id[] = {  
  55.     { "tq210-ts", 0 },  
  56.     { }  
  57. };  
  58.   
  59. static int ts_init_panel(struct i2c_client *client){  
  60.     short ret=-1;  
  61.     uint8_t config_info[] = {  
  62.         0x06,0xA2,  
  63.         0x12,0x10,0x0E,0x0C,0x0A,0x08,0x06,0x04,0x02,0x00,0xE2,0x53,0xD2,0x53,0xC2,0x53,  
  64.         0xB2,0x53,0xA2,0x53,0x92,0x53,0x82,0x53,0x72,0x53,0x62,0x53,0x52,0x53,0x42,0x53,  
  65.         0x32,0x53,0x22,0x53,0x12,0x53,0x02,0x53,0xF2,0x53,0x0F,0x13,0x40,0x40,0x40,0x10,  
  66.         0x10,0x10,0x0F,0x0F,0x0A,0x35,0x25,0x0C,0x03,0x00,0x05,0x20,0x03,0xE0,0x01,0x00,  
  67.         0x00,0x34,0x2C,0x36,0x2E,0x00,0x00,0x03,0x19,0x03,0x08,0x00,0x00,0x00,0x00,0x00,  
  68.         0x14,0x10,0xEC,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0D,0x40,  
  69.         0x30,0x3C,0x28,0x00,0x00,0x00,0x00,0xC0,0x12,0x01     
  70.     };  
  71.     config_info[62] = 480 >> 8;  
  72.     config_info[61] = 480 & 0xff;  
  73.     config_info[64] = 800 >> 8;  
  74.     config_info[63] = 800 & 0xff;  
  75.     ret = i2c_write_bytes(client, config_info, sizeof(config_info)/sizeof(config_info[0]));  
  76.     if(ret < 0)  {  
  77.         printk(KERN_ERR "GT811 Send config failed!\n");  
  78.         return ret;   
  79.     }  
  80.     return 0;  
  81. }  
  82.   
  83. static irqreturn_t gt811_int_handler(int irq, void *devid){  
  84.     disable_irq_nosync(this_client->irq);  
  85.     queue_work(wq, &work);  
  86.     return IRQ_RETVAL(IRQ_HANDLED);  
  87. }  
  88.   
  89. static void ts_work_func(struct work_struct* work){  
  90.     int ret;  
  91.     unsigned char point_data[19] = {0x07, 0x21, 0};  
  92.     unsigned short input_x = 0;  
  93.     unsigned short input_y = 0;  
  94.     unsigned short input_p = 0;  
  95.       
  96.     ret=i2c_read_bytes(this_client, point_data, sizeof(point_data)/sizeof(point_data[0]));  
  97.     if(ret <= 0){  
  98.         printk("Failed\n");  
  99.         return;  
  100.     }  
  101.   
  102.     if(point_data[2]&0x1){  
  103.         status = 1;  
  104.         input_y = 479-((point_data[4]<<8)|point_data[5]);  
  105.         input_x = 799-((point_data[6]<<8)|point_data[7]);  
  106.         input_p = point_data[8];  
  107.   
  108.         printk("stat: %d, x: %d, y: %d, p: %d\n", point_data[2], input_x, input_y,  
  109.             input_p);  
  110.     }  
  111.     else if(status){  
  112.         status = 0;  
  113.         printk("up\n");  
  114.     }  
  115.   
  116.     enable_irq(this_client->irq);  
  117. }  
  118.   
  119. static int ts_probe(struct i2c_client *client, const struct i2c_device_id *id){  
  120.     int retry, ret;  
  121.     char test_data;  
  122.   
  123.     printk("ts_probe\n");  
  124.   
  125.     test_data = 0;  
  126.   
  127.     gt811_rst = S5PV210_GPD0(3);  
  128.     gt811_int = S5PV210_GPH1(6);  
  129.     gpio_request(gt811_rst, "reset");  
  130.     gpio_request(gt811_rst, "tsint");  
  131.       
  132.     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))   
  133.     {  
  134.         dev_err(&client->dev, "Must have I2C_FUNC_I2C.\n");  
  135.         return -ENODEV;  
  136.     }  
  137.   
  138.     s3c_gpio_setpull(gt811_rst, S3C_GPIO_PULL_UP);  
  139.       
  140.     for(retry=0;retry < 5; retry++)  
  141.     {  
  142.         gpio_direction_output(gt811_rst, 0);  
  143.         msleep(1);  
  144.         gpio_direction_input(gt811_rst);  
  145.         msleep(100);  
  146.       
  147.         ret =i2c_write_bytes(client, &test_data, 1);  
  148.         if (ret > 0)  
  149.             break;  
  150.         dev_info(&client->dev, "GT811 I2C TEST FAILED!Please check the HARDWARE connect\n");  
  151.     }  
  152.   
  153.     if(ret <= 0)  
  154.     {  
  155.         dev_err(&client->dev, "Warnning: I2C communication might be ERROR!\n");  
  156.         return -ENODEV;  
  157.     }  
  158.   
  159.     for(retry = 0; retry != 5; ++ retry){  
  160.         ret = ts_init_panel(client);  
  161.         if(ret != 0){  
  162.             continue;  
  163.         }  
  164.         else{  
  165.             break;  
  166.         }  
  167.     }  
  168.   
  169.     if(ret != 0){  
  170.         printk("GT811 Configue failed!\n");  
  171.         return -ENODEV;  
  172.     }  
  173.   
  174.     this_client = client;  
  175.       
  176.     ts_input = input_allocate_device();  
  177.     if(IS_ERR(ts_input)){  
  178.         printk("GT811 allocate ts input device failed!\n");  
  179.         return -ENOMEM;  
  180.     }  
  181.   
  182.     ts_input->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;  
  183.     ts_input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);  
  184.     ts_input->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);  
  185.   
  186.     input_set_abs_params(ts_input, ABS_Y, 0,  799, 0, 0);  
  187.     input_set_abs_params(ts_input, ABS_X, 0, 479, 0, 0);  
  188.     input_set_abs_params(ts_input, ABS_PRESSURE, 0, 255, 0, 0);  
  189.   
  190.     ts_input->name = "tq210-ts";  
  191.     ts_input->phys = "input/ts";  
  192.     ts_input->id.bustype = BUS_I2C;  
  193.     ts_input->id.product = 0xBEEF;  
  194.     ts_input->id.vendor  =0xDEAD;  
  195.   
  196.     ret = input_register_device(ts_input);  
  197.     if(ret < 0){  
  198.         printk("Unable register %s input device!\n", ts_input->name);  
  199.         input_free_device(ts_input);  
  200.         return -ENOMEM;  
  201.     }  
  202.   
  203.     client->irq = IRQ_EINT(14);  
  204.   
  205.     s3c_gpio_setpull(gt811_int, S3C_GPIO_PULL_UP);  
  206.   
  207.     if(request_irq(IRQ_EINT(14), gt811_int_handler, IRQF_TRIGGER_FALLING, "gt811-int", NULL) < 0){  
  208.         printk("Request irq for gt811 failed!\n");  
  209.         input_unregister_device(ts_input);  
  210.         input_free_device(ts_input);  
  211.         return -ENOMEM;  
  212.     }  
  213.   
  214.     wq = create_workqueue("ts_handle_thread");  
  215.     if(wq == NULL){  
  216.         printk(KERN_ALERT "crete workqueue failed!\n");  
  217.         input_unregister_device(ts_input);  
  218.         input_free_device(ts_input);  
  219.         free_irq(IRQ_EINT(14), NULL);  
  220.         return -ENOMEM;  
  221.     }  
  222.   
  223.     INIT_WORK(&work, ts_work_func);  
  224.       
  225.     return 0;  
  226. }  
  227.   
  228. static int ts_remove(struct i2c_client *client){  
  229.       
  230.     free_irq(IRQ_EINT(14), NULL);  
  231.     enable_irq(client->irq);  
  232.     flush_workqueue(wq);  
  233.     destroy_workqueue(wq);  
  234.       
  235.     input_unregister_device(ts_input);  
  236.     input_free_device(ts_input);  
  237.   
  238.     gpio_free(gt811_rst);  
  239.     gpio_free(gt811_int);  
  240.     return 0;  
  241. }  
  242.   
  243. static struct i2c_driver ts_driver = {  
  244.     .driver = {  
  245.         .name = "tq210-ts",  
  246.         .owner = THIS_MODULE,  
  247.     },  
  248.   
  249.     .probe = ts_probe,  
  250.     .remove = ts_remove,  
  251.     .id_table = ts_id,  
  252.     .address_list = normal_address,  
  253. };  
  254.   
  255. static int ts_init(void){  
  256.     printk("init\n");  
  257.     i2c_add_driver(&ts_driver);  
  258.     return 0;  
  259. }  
  260.   
  261. static void ts_exit(void){  
  262.     i2c_del_driver(&ts_driver);  
  263.     printk("exit\n");  
  264. }  
  265.   
  266. module_init(ts_init);  
  267. module_exit(ts_exit);  
  268. MODULE_LICENSE("GPL");  
这并不是完整的代码,一方面是没有做异常处理,另一方面是没有上报消息,只是简单的驱动了TQ210的触摸屏部分,如果您需要拿去自己略作修改即可使用。

三 注册TS的I2C模块设备

注册TS的I2C模块很简单,在Linux内核文件arch/arm/mach-s5pv210/mach-smdkv210.c文件的I2C通道2结构体中加入TS的I2C地址,也就是0x5d,添加后如下
[plain]  view plain copy
  1. static struct i2c_board_info smdkv210_i2c_devs2[] __initdata = {  
  2.     /* To Be Updated */  
  3.     { I2C_BOARD_INFO("tq210-ts", 0x5d), },  
  4. ;  

四 tslib测试教程(ubuntu)

1. 安装git
[plain]  view plain copy
  1. sudo apt-get install git  

2.  下载最新的tslib
[plain]  view plain copy
  1. git clone https://github.com/kergoth/tslib  

3. 安装auto
[plain]  view plain copy
  1. sudo apt-get install autoconf  automake  libtool  

4. 编译tslib
[plain]  view plain copy
  1. ./autogen.sh   
  2. mkdir tmp  
  3. echo "ac_cv_func_malloc_0_nonnull=yes" >arm-linux.cache  
  4. ./configure --host=arm-linux --cache-file=arm-linux.cache --prefix=$(pwd)/tmp  
  5. make  
  6. make install  

5. 安装tslib
[plain]  view plain copy
  1. cd tmp  
  2. cp * /nfsroot/rootfs -rfd  

6. 配置tslib
[plain]  view plain copy
  1. 修改 /etc/ts.conf  
  2. 将行  
  3. # module_raw input  
  4. 改为:  
  5. module_raw input  
  6. (实际上就是去掉高行的#号和第一个空格)  

7. 配置tslib运行环境变量
[plain]  view plain copy
  1. export TSLIB_TSDEVICE=/dev/input/event1  //这里需要根据自己的event位置进行修改,新内核在/dev/input/event*  
  2. export TSLIB_CALIBFILE=/etc/pointercal  
  3. export TSLIB_CONFFILE=/etc/ts.conf  
  4. export TSLIB_PLUGINDIR=/lib/ts  
  5. export TSLIB_CONSOLEDEVICE=none  
  6. export TSLIB_FBDEVICE=/dev/fb0  

8. 校正(电容屏实际上不需要校正,仅为了测试触屏驱动)
[plain]  view plain copy
  1. 运行ts_calibrate,并根据提示进行校正  

9. 自由画图
[plain]  view plain copy
  1. 运行ts_test,点击draw按钮,可以自由画图,效果如下图。  

五 小结

本文中列举的代码是简单的实现了触摸坐标获取,没有实现触摸消息上报等操作,这些操作需要自己来实现。

我自己完善了一下上面讲到的驱动,下面是在TQ210上用最新版tslib测试的效果,同时也支持了多点触摸,代码我上传到了我的资源里,需要的朋友去下载,资源分有点贵啊,见谅。。。



本文作者:girlkoo

更多 0
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值