前面几篇文章已经通过配置DTS的方式完成了多个驱动的移植,接下来我们解决TQ335x的触摸驱动问题。由于种种原因,TQ335x的触摸屏驱动是以模块方式提供的,且Linux官方内核中也没有带该触摸屏的驱动源码,单纯的配置DTS是无法完成TQ335x的触摸驱动移植工作的,因此,本文参考内核中原有的pixcir_i2c_ts驱动编写TQ335x的触摸屏(TN92)驱动。
在之前移植TQ210时,我已经编写过TQ210的触摸屏驱动,我的TQ335x还是使用的TQ210的屏,因此,难度不是很大。这里需要说明一点,在TQ210驱动移植时对多点触摸协议的理解还不够深入,当时编写的驱动单点触摸是可以正常使用的,但是多点触摸不对(这次编写TQ335x的触摸驱动是才意识到的)。但是编写的TQ210驱动多点触摸实际上使用的多点触摸的A协议,但是用错了一些地方,本文基于TQ335x的重新编写的触摸驱动是按照多点触摸B协议编写,使用tslib测试正常,文章末尾有效果图。
TN92触摸屏使用的触控芯片是GT811,下面我们来分析下触摸屏驱动的编写。
(1) 查看原理图
从触摸屏原理图中可以看到,GT811与开发板相连的引脚有四条,分别是SDA、SDL、INT和RESET。其中,I2C_SDA和I2C_SDL是连接到AM335x的I2C1端口上的,用来与SoC通信;INT引脚是连接在GPIO1的27号引脚上的,在检测到触摸时GT811通过该引脚向SoC发起中断请求;RESET引脚接到SoC的GPIO1的26号引脚上的,是用来接收SoC复位操作,对于本文,SoC就是AM335x。
其中,GT811与SoC的管脚连接信息可以从底板原理图中找到,SDA和SCL的我就不往外贴了,INT和RESET的连接关系如下:
YP连接到了GT811的RESET脚上,然后通过短路帽与GPIO1_26链接,YM连接到GT811的INT脚上,通过短路帽与GPIO1_27链接,因此,需要将GPIO1_27配置为终端输入引脚,GPIO1_26配置为输出引脚。此外,查看GT811的芯片手册可获得INT和RESET的操作信息:
- 1. GT811检测到触摸时会拉低中断引脚,因此,GPIO1_27需要配置为下降沿触发。
- 2. GT811的RESET脚为低电平有效,因此,上电时需要拉低RESET引脚。
- 3. GT811的RESET引脚自带上拉,因此,使用GPIO1_26将GT811的RESET拉低复位后切换为悬浮输入太即可。
(2) DTS配置Platform信息
通过前面的分析,我们知道需要配置AM335x的四条引脚才能使GT811正常工作。其中,GT811通过I2C接口连接到AM335x的I2C1上,因此,需要配置AM335x的I2C1的两条引脚为I2C功能;GPIO1_27需要配置为中断输入、下降沿触发,中断号也可以确定下来了,就是GPIO1的27号角(内核能将引脚转换为中断号);最后就是RESET脚,驱动初始化GT811时需要操作RESET引脚,且GT811为拉低复位,故可将GPIO1的26角设为输出电平状态。通过前面几篇文章的学习,我们知道DTS可以配置pinmux,然后分析内核自带的DTS文件可以,DTS也可以将连接信息传递给内核。经过分析及参考,我最TQ335x.dts文件做了如下修改:
Step1. 检查I2C引脚的pinmux配置
查找i2c1可以找到i2c1节点,该节点的pinctrl-0只想的phandler对i2c1的两个引脚进行了相关的配置,因此,不需任何修改。
Step2. 配置INT和RESET引脚
在am33xx_pinmux节点内添加引脚配置信息,具体内容如下:
- gt811_ts_pins: gt811_ts_pins {
- pinctrl-single,pins = <
- 0x68 (PIN_INPUT_PULLUP | MUX_MODE7)
- 0x6c (PIN_INPUT_PULLUP | MUX_MODE7)
- >;
- };
GT811的设备节点内需要提供以下信息,i2c设备地址、pinmux配置、中断信息、屏幕大小等,具体如下(这里就不细说了,有什么不清楚的可以留言讨论)。
- gt811_ts@5d {
- compatible = "gt811,gt811_ts";
- pinctrl-names = "default";
- pinctrl-0 = <>811_ts_pins>;
- reg = <0x5d>;
- interrupt-parent = <&gpio1>;
- interrupts = <27 2>;
- gpios = <&gpio1 26 0>;
- touchscreen-size-x = <800>;
- touchscreen-size-y = <480>;
- touchscreen-swap = <1>;
- touchscreen-revert-x = <1>;
- touchscreen-revert-y = <1>;
- };
(3)驱动编写
驱动编写根之前TQ210相比,没有多少变化,都是采用的新式I2C设备驱动架构,重要的区别在于支持了多点触摸,驱动的详细分析我就不多说了,具体可以参考韦东山老师的视频教程(绝对物有所值)。下面是GT811的多点触摸驱动源码。从GT811设备节点中获取坐标信息的部分我直接贴出来了,完整的代码还是请到资源里下载,还是有点贵哈,不过这个驱动可以直接拿去使用了,不需要任何修改。下面是不完整的代码,可以打印出触摸点坐标,该代码已经把本驱动的核心的部分都写出来了。其实很多朋友不需要下载源码就可以补充完善这个驱动了,而且我真心的希望阅读本文的朋友不需要下载源码就能自己写出触摸驱动。
- #include <linux/module.h>
- #include <linux/i2c.h>
- #include <linux/platform_device.h>
- #include <linux/gpio.h>
- #include <linux/of.h>
- #include <linux/of_platform.h>
- #include <linux/of_gpio.h>
- #include <linux/input.h>
- #include <linux/input/mt.h>
- #include <linux/interrupt.h>
- #include <linux/delay.h>
- struct gt811_ts_platdata
- {
- u32 size_x;
- u32 size_y;
- u32 size_p;
- u32 swap;
- u32 revert_x;
- u32 revert_y;
- u32 reset_pin;
- u32 interrupt_pin;
- u32 ponits_max;
- struct i2c_client *client;
- struct input_dev *input;
- struct work_struct work;
- };
- static const struct of_device_id gt811_ts_of_match[] = {
- { .compatible = "gt811,gt811_ts", .data = NULL },
- { }
- };
- static int i2c_write_bytes(struct i2c_client *client, uint8_t *data, int len){
- struct i2c_msg msg;
- msg.flags=!I2C_M_RD;
- msg.addr=client->addr;
- msg.len=len;
- msg.buf=data;
- return i2c_transfer(client->adapter,&msg, 1);
- }
- static int i2c_read_bytes(struct i2c_client *client, uint8_t *buf, int len){
- struct i2c_msg msgs[2];
- msgs[0].flags=!I2C_M_RD;
- msgs[0].addr=client->addr;
- msgs[0].len=2;
- msgs[0].buf=&buf[0];
- msgs[1].flags=I2C_M_RD;
- msgs[1].addr=client->addr;
- msgs[1].len=len-2;
- msgs[1].buf=&buf[2];
- return i2c_transfer(client->adapter,msgs, 2);
- }
- static void gt811_ts_handler(struct work_struct *work)
- {
- struct gt811_ts_platdata *pdata = container_of(work, struct gt811_ts_platdata, work);
- struct device *dev = &pdata->client->dev;
- uint8_t buffer[36] = {0x07, 0x21, 0};
- uint8_t count, index, flags, position;
- int x, y;
- buffer[0] = 0x0f;
- buffer[1] = 0xff;
- if (i2c_write_bytes(pdata->client,buffer,2) < 0) {
- dev_err(dev, "Failed to write wakeup message.\n");
- goto reenable_irq;
- }
- buffer[0] = 0x07;
- buffer[1] = 0x21;
- if (i2c_read_bytes(pdata->client, buffer, sizeof(buffer)) < 0) {
- dev_err(dev, "Failed to read touch message.\n");
- goto reenable_irq;
- }
- buffer[0] = 0x80;
- buffer[1] = 0x00;
- if (i2c_write_bytes(pdata->client, buffer, 2) < 0) {
- dev_err(dev, "Failed to write sleep message.\n");
- goto reenable_irq;
- }
- buffer[25] = buffer[19];
- buffer[19] = 0;
- flags = buffer[2]&0x1f;
- while (flags) {
- if (!(flags&0x1)) {
- continue;
- }
- if (index < 3) {
- position = 4 + index * 5;
- }
- else{
- position = 25 + (index - 3) * 5;
- }
- x = (buffer[position] << 8) | buffer[position + 1];
- y = (buffer[position + 2] << 8) | buffer[position + 3];
- if(pdata->swap) {
- swap(x, y);
- }
- if(pdata->revert_x){
- x = pdata->size_x - x;
- }
- if(pdata->revert_y){
- y = pdata->size_y - y;
- }
- printk("point:(x:%03d, y:%03d)\n", x, y);
- }
- // 组织检测出来的触摸点信息上报到输入子系统节点即可
- reenable_irq:
- enable_irq(pdata->client->irq);
- }
- static irqreturn_t gt811_ts_isr(int irq, void *dev_id)
- {
- struct gt811_ts_platdata* pdata = (struct gt811_ts_platdata*)dev_id;
- disable_irq_nosync(pdata->client->irq);
- schedule_work(&pdata->work);
- return IRQ_HANDLED;
- }
- static int gt811_ts_initilize(struct i2c_client *client)
- {
- struct device *dev = &client->dev;
- struct gt811_ts_platdata *pdata = (struct gt811_ts_platdata*)i2c_get_clientdata(client);
- int status = 0, count = 0;
- uint8_t version[4] = {0x7, 0x17, 0};
- uint8_t config[] = {
- 0x06,0xA2,
- 0x12,0x10,0x0E,0x0C,0x0A,0x08,0x06,0x04,0x02,0x00,0xE2,0x53,0xD2,0x53,0xC2,0x53,
- 0xB2,0x53,0xA2,0x53,0x92,0x53,0x82,0x53,0x72,0x53,0x62,0x53,0x52,0x53,0x42,0x53,
- 0x32,0x53,0x22,0x53,0x12,0x53,0x02,0x53,0xF2,0x53,0x0F,0x13,0x40,0x40,0x40,0x10,
- 0x10,0x10,0x0F,0x0F,0x0A,0x35,0x25,0x0C,0x03,0x00,0x05,0x20,0x03,0xE0,0x01,0x00,
- 0x00,0x34,0x2C,0x36,0x2E,0x00,0x00,0x03,0x19,0x03,0x08,0x00,0x00,0x00,0x00,0x00,
- 0x14,0x10,0xEC,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0D,0x40,
- 0x30,0x3C,0x28,0x00,0x00,0x00,0x00,0xC0,0x12,0x01
- };
- config[62] = 480 >> 8;
- config[61] = 480 & 0xff;
- config[64] = 800 >> 8;
- config[63] = 800 & 0xff;
- if (!gpio_is_valid(pdata->reset_pin)) {
- dev_err(dev, "The reset pin number is invalid.\n");
- return -EINVAL;
- }
- count = 3;
- while (count--) {
- gpio_direction_output(pdata->reset_pin, 0);
- msleep(10);
- gpio_direction_output(pdata->reset_pin, 1);
- msleep(100);
- if (i2c_read_bytes(client, version, sizeof(version)) < 0) {
- dev_err(dev, "Failed to get the version of GT811, try again...\n");
- status = -ENODEV;
- }
- else {
- dev_info(dev, "Gt811 detected, version(%04x)...\n", (version[2]<<8)|version[3]);
- status = 0;
- break;
- }
- }
- if (status) {
- return status;
- }
- count = 3;
- while (count--) {
- if (i2c_write_bytes(client, config, sizeof(config)) < 0) {
- dev_err(dev, "Failed to configure the GT811, try again...\n");
- status = -EINVAL;
- }
- else {
- dev_info(dev, "Gt811 configue succeed\n");
- status = 0;
- break;
- }
- }
- return status;
- }
- static struct gt811_ts_platdata *gt811_ts_parse_devtree(struct i2c_client *client)
- {
- struct device *dev = &client->dev;
- struct device_node *node;
- struct gt811_ts_platdata *pdata;
- enum of_gpio_flags flags;
- node = dev->of_node;
- if (!node) {
- dev_err(dev, "The of_node is NULL.\n");
- return ERR_PTR(-ENODEV);
- }
- pdata = devm_kzalloc(dev, sizeof(struct device_node), GFP_KERNEL);
- if (!pdata) {
- dev_err(dev, "No enough memory left.\n");
- return ERR_PTR(-ENOMEM);
- }
- pdata->reset_pin = of_get_gpio_flags(node, 0, &flags);
- if (pdata->reset_pin < 0) {
- dev_err(dev, "Get RST pin failed!\n");
- return ERR_PTR(-EINVAL);
- }
- if (of_property_read_u32(node, "touchscreen-size-x", &pdata->size_x )) {
- dev_err(dev, "Failed to get the touch screen x size.\n");
- return ERR_PTR(-EINVAL);
- }
- if (of_property_read_u32(node, "touchscreen-size-y", &pdata->size_y)) {
- dev_err(dev, "Failed to get the touch screen y size.\n");
- return ERR_PTR(-EINVAL);
- }
- if (of_property_read_u32(node, "touchscreen-size-p", &pdata->size_p)) {
- pdata->size_p = 255;
- }
- if (of_property_read_u32(node, "touchscreen-swap", &pdata->swap)) {
- pdata->swap = 1;
- }
- if (of_property_read_u32(node, "touchscreen-revert-x", &pdata->revert_x)) {
- pdata->revert_x = 1;
- }
- if (of_property_read_u32(node, "touchscreen-revert-x", &pdata->revert_y)) {
- pdata->revert_y = 1;
- }
- return pdata;
- }
- static int gt811_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
- {
- struct device *dev = &client->dev;
- struct gt811_ts_platdata *pdata = dev_get_platdata(dev);
- struct input_dev *input;
- int error = 0;
- if (!of_match_device(of_match_ptr(gt811_ts_of_match), dev)) {
- dev_err(dev, "Failed to match.\n");
- return -EINVAL;
- }
- if (!pdata) {
- pdata = gt811_ts_parse_devtree(client);
- if (IS_ERR(pdata)) {
- dev_err(dev, "Get device data from device tree failed!\n");
- error = -EINVAL;
- goto failed_exit;
- }
- }
- pdata->client = client;
- i2c_set_clientdata(client, pdata);
- input = devm_input_allocate_device(dev);
- if (!input) {
- dev_err(dev, "Failed to allocate input device\n");
- error = -ENOMEM;
- goto pdata_free;
- }
- pdata->input = input;
- input->name = client->name;
- input->id.bustype = BUS_I2C;
- input->id.product = 0xBEEF;
- input->id.vendor =0xDEAD;
- input->dev.parent = &client->dev;
- __set_bit(EV_KEY, input->evbit);
- __set_bit(EV_ABS, input->evbit);
- __set_bit(BTN_TOUCH, input->keybit);
- input_set_abs_params(input, ABS_X, 0, pdata->size_x, 0, 0);
- input_set_abs_params(input, ABS_Y, 0, pdata->size_y, 0, 0);
- input_set_abs_params(input, ABS_MT_POSITION_X, 0, pdata->size_x, 0, 0);
- input_set_abs_params(input, ABS_MT_POSITION_Y, 0, pdata->size_y, 0, 0);
- error = input_mt_init_slots(input, 5, INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
- if (error) {
- dev_err(dev, "Failed to initialize the multi-touch slots.\n");
- goto input_free;
- }
- input_set_drvdata(input, pdata);
- error = input_register_device(input);
- if (error) {
- dev_err(dev, "Register input device failed!\n");
- goto input_free;
- }
- if (gt811_ts_initilize(client)) {
- dev_err(dev, "Failed to initialize GT811.\n");
- }
- INIT_WORK(&pdata->work, gt811_ts_handler);
- error = devm_request_any_context_irq(dev, client->irq, gt811_ts_isr,
- IRQF_TRIGGER_FALLING, client->name, pdata);
- if (error) {
- dev_err(dev, "Failed to request irq(number:%d)\n", client->irq);
- goto input_free;
- }
- return 0;
- input_free:
- devm_kfree(dev, input);
- pdata_free:
- devm_kfree(dev, pdata);
- failed_exit:
- return error;
- }
- static int gt811_ts_remove(struct i2c_client *client)
- {
- struct gt811_ts_platdata *pdata = (struct gt811_ts_platdata*)i2c_get_clientdata(client);
- devm_free_irq(&client->dev, client->irq, i2c_get_clientdata(client));
- input_unregister_device(pdata->input);
- devm_kfree(&client->dev, pdata);
- return 0;
- }
- static const struct i2c_device_id gt811_ts_id[] = {
- { "gt811_ts", 0 },
- { }
- };
- static struct i2c_driver gt811_ts_driver = {
- .driver = {
- .owner = THIS_MODULE,
- .name = "gt811_ts",
- .of_match_table = of_match_ptr(gt811_ts_of_match),
- },
- .probe = gt811_ts_probe,
- .remove = gt811_ts_remove,
- .id_table = gt811_ts_id,
- };
- module_i2c_driver(gt811_ts_driver);
- MODULE_AUTHOR("girlkoo <nightmeng@gmail.com>");
- MODULE_DESCRIPTION("Gt811 I2C Touchscreen Driver");
- MODULE_LICENSE("GPL");
tslib的编译方法请参考本博客的另一片文章,链接如下:
本文就不再重复tslib的配置方法。
(5)效果展示
(6) 完整驱动代码
请到本人的资源中下载TQ335x的触摸屏驱动源码,链接如下: