一.input事件
对于所有的input设备,报告input事件时候都分这么几部分,首先在probe函数中设置设备发送的事件类型、按键类型,设置设备一些属性信息。然后在发送事件时候要根据probe的设置来发送事件,否则就会被判为无效忽略掉。
input子系统事件分为type、code、value三部分。type表示事件的类型,按键、绝对坐标等。code表示键值、触摸坐标等。value表示数值,如按键按下为1,抬起为0;对于触摸屏x、y坐标则为对应的数值。
输入子系统事件类型(type)
linux输入子系统的事件类型定义在路径:include/uapi/linux/input.h
中,事件类型含义如下表
触摸屏驱动事件类型配置
对于触摸屏驱动,必须支持的事件类型有以下三个:
//设备同步,每次触摸完成以后都要发送一个同步事件,来表明这次触摸已经完成
__set_bit(EV_SYN, input->evbit);
//绝对坐标事件,触摸屏每次发送的坐标都是绝对坐标,不同于鼠标的相对坐标
__set_bit(EV_ABS, input->evbit);
//按键事件,每次触摸都有一个BTN_TOUCH的按键事件
__set_bit(EV_KEY, input->evbit);
二.输入子系统多点触控协议
基于硬件的能力,该协议被分为两种类型。对于只能处理匿名接触(type A)的设备,该协议描述了如何把所有的原始触摸数据发送给接收者。对于那些有能力跟踪并识别每个触摸点的设备(type B),该协议描述了如何把每个触摸点的单独更新通过事件slots发送给接受者。
Type A 触摸点信息上报时序:
ABS_MT_POSITION_X x[0]
ABS_MT_POSITION_Y y[0]
SYN_MT_REPORT
ABS_MT_POSITION_X x[1]
ABS_MT_POSITION_Y y[1]
SYN_MT_REPORT
SYN_REPORT
第 1 行,通过 ABS_MT_POSITION_X 事件上报第一个触摸点的 X 坐标数据,通过 input_report_abs 函数实现,下面同理。 第 2 行,通过 ABS_MT_POSITION_Y 事件上报第一个触摸点的 Y 坐标数据。 第 3 行,上报 SYN_MT_REPORT 事件,通过调用 input_mt_sync 函数来实现。 第 4 行,通过 ABS_MT_POSITION_X 事件上报第二个触摸点的 X 坐标数据。 第 5 行,通过 ABS_MT_POSITION_Y 事件上报第二个触摸点的 Y 坐标数据。 第 6 行,上报 SYN_MT_REPORT 事件,通过调用 input_mt_sync 函数来实现。 第 7 行,上报 SYN_REPORT 事件,通过调用 input_sync 函数实现。
A协议事件上报:
for (i = 0; i < event->touch_point; i++) //循环处理 缓存中的所有点
{
input_mt_slot(data->input_dev, event->au8_finger_id[i]); //发送点的ID
if (event->au8_touch_event[i]== 0 || event->au8_touch_event[i] == 2) //如果点按下
{
input_mt_report_slot_state(data->input_dev, MT_TOOL_FINGER, true); //手指按下
input_report_abs(data->input_dev,ABS_MT_POSITION_X,event->au16_x[i]); //x坐标
input_report_abs(data->input_dev, ABS_MT_POSITION_Y,event->au16_y[i]); //y坐标
input_report_abs(data->input_dev,ABS_MT_TOUCH_MAJOR,event->pressure); //触摸点长轴长度
}
else
{
input_mt_report_slot_state(data->input_dev, MT_TOOL_FINGER,false); //手指抬起
}
input_mt_report_pointer_emulation(input_dev, true);//用模拟点的方法,来告知此次触摸已经完成。
input_sync(data->input_dev); //sync 设备同步
}
Type B 触摸点信息上报时序:
ABS_MT_SLOT 0
ABS_MT_TRACKING_ID 45
ABS_MT_POSITION_X x[0]
ABS_MT_POSITION_Y y[0]
ABS_MT_SLOT 1
ABS_MT_TRACKING_ID 46
ABS_MT_POSITION_X x[1]
ABS_MT_POSITION_Y y[1]
SYN_REPORT
第 1 行,上报 ABS_MT_SLOT 事件,也就是触摸点对应的 SLOT。每次上报一个触摸点坐 标之前要先使用input_mt_slot函数上报当前触摸点SLOT,触摸点的SLOT其实就是触摸点ID, 需要由触摸 IC 提供。 第 2 行,根据 Type B 的要求,每个 SLOT 必须关联一个 ABS_MT_TRACKING_ID,通过 修改 SLOT 关联的 ABS_MT_TRACKING_ID 来完成对触摸点的添加、替换或删除。具体用到 的函数就是 input_mt_report_slot_state,如果是添加一个新的触摸点,那么此函数的第三个参数 active 要设置为 true,linux 内核会自动分配一个 ABS_MT_TRACKING_ID 值,不需要用户去指 定具体的 ABS_MT_TRACKING_ID 值。 第 3 行,上报触摸点 0 的 X 轴坐标,使用函数 input_report_abs 来完成。 第 4 行,上报触摸点 0 的 Y 轴坐标,使用函数 input_report_abs 来完成。 第 5~8 行,和第 1~4 行类似,只是换成了上报触摸点 1 的(X,Y)坐标信息 第 9 行,当所有的触摸点坐标都上传完毕以后就得发送 SYN_REPORT 事件,使用 input_sync 函数来完成。
B协议事件上报:
input_report_key(input, BTN_TOUCH, event->touch_point > 0); (1)
for (i = 0; i < event->touch_point; i++) { (2)
input_mt_slot(input, event->au8_finger_id[i]); (3)
if (event->au8_touch_event[i] == 0 || event->au8_touch_event[i] == 2) { (4)
input_mt_report_slot_state(input, MT_TOOL_FINGER, true); (5)
input_report_abs(input,ABS_MT_TRACKING_ID,event->au8_finger_id[i]); (6)
/*motify coordinate info by mirror setting*/
if (priv->ts_config.x_mirror) {
event->au16_x[i] = priv->ts_config.x_max - event->au16_x[i];
}
input_report_abs(input, ABS_MT_POSITION_X, event->au16_x[i]); (7)
if (priv->ts_config.y_mirror) {
event->au16_y[i] = priv->ts_config.y_max - event->au16_y[i];
}
input_report_abs(input, ABS_MT_POSITION_Y, event->au16_y[i]); (8)
input_report_abs(input, ABS_MT_TOUCH_MAJOR, event->au16_w[i]); (9)
input_report_abs(input, ABS_MT_WIDTH_MAJOR, event->au16_w[i]);
printk(KERN_DEBUG "touch_point(%d) x(%d) y(%d)\n", event->au8_finger_id[i],
event->au16_x[i], event->au16_y[i]);
} else {
uppoint++;
printk(KERN_DEBUG "finger %d up \n", event->au8_finger_id[i]);
input_mt_report_slot_state(input, MT_TOOL_FINGER, false); (10)
}
}
if (event->touch_point == uppoint) {
input_report_key(input, BTN_TOUCH, 0); (11)
} else {
input_report_key(input, BTN_TOUCH, event->touch_point > 0); (12)
}
input_sync(input);
(1)上报触摸BTN_TOUCH按键键值,表示触摸按下
(2)循环处理驱动层上报的触控点数
(3)发送点的id作为slot记录的点
(4)如果点按下
(5)手指按下
(6)发送点的id事件
(7)上报X绝对坐标
(8)上报Y绝对坐标
(9)上报触摸点长轴宽度
(10)手指抬起,释放该点ID(-1)
(11)如果所有手指都抬起。上报BTN_TOUCH抬起事件
(12)如果还有手指没抬起,上报BTN_TOUCH按下
(13)sync 设备同步
三.多点触摸使用的API
input_mt_init_slots 函数:
input_mt_init_slots 函数用于初始化 MT 的输入 slots,编写 MT 驱动的时候必须先调用此函 数初始化 slots,此函数定义在文件 drivers/input/input-mt.c 中,函数原型如下所示:
int input_mt_init_slots( struct input_dev *dev,
unsigned int num_slots,
unsigned int flags)
函数参数和返回值含义如下: dev: MT 设备对应的 input_dev,因为 MT 设备隶属于 input_dev。 num_slots:设备要使用的 SLOT 数量,也就是触摸点的数量。 flags:其他一些 flags 信息。返回值:0,成功;负值,失败。
input_mt_slot 函数:
此函数用于 Type B 类型,产生 ABS_MT_SLOT 事件,告诉内核当前上报的是哪个触摸点 的坐标数据,此函数定义在文件 include/linux/input/mt.h 中,函数原型如下所示:
void input_mt_slot(struct input_dev *dev,
int slot)
函数参数和返回值含义如下: dev: MT 设备对应的 input_dev。 slot:当前发送的是哪个 slot 的坐标信息,也就是哪个触摸点。 返回值:无。
input_mt_report_slot_state 函数:
此函数用于 Type B 类型,用于产生 ABS_MT_TRACKING_ID 和 ABS_MT_TOOL_TYPE 事件, ABS_MT_TRACKING_ID 事件给 slot 关联一个 ABS_MT_TRACKING_ID , ABS_MT_TOOL_TYPE 事 件 指 定 触 摸 类 型 ( 是 笔 还 是 手 指 等 )。 此 函 数 定 义 在 文 件 drivers/input/input-mt.c 中,此函数原型如下所示:
bool input_mt_report_slot_state( struct input_dev *dev,
unsigned int tool_type,
bool active)
函数参数和返回值含义如下: dev: MT 设备对应的 input_dev。 tool_type:触摸类型,可以选择 MT_TOOL_FINGER(手指)、MT_TOOL_PEN(笔)或 MT_TOOL_PALM(手掌),对于多点电容触摸屏来说一般都是手指。 active:true,连续触摸,input 子系统内核会自动分配一个 ABS_MT_TRACKING_ID 给 slot。 false,触摸点抬起,表示某个触摸点无效了,input 子系统内核会分配一个-1 给 slot,表示触摸 点溢出。 返回值:触摸有效的话返回 true,否则返回 false。
input_report_abs 函数:
Type A 和 Type B 类型都使用此函数上报触摸点坐标信息,通过 ABS_MT_POSITION_X 和 ABS_MT_POSITION_Y 事 件 实 现 X 和 Y 轴 坐 标 信 息 上 报 。 此 函 数 定 义 在 文 件 include/linux/input.h 中,函数原型如下所示:
void input_report_abs( struct input_dev *dev,
unsigned int code,
int value)
函数参数和返回值含义如下: dev: MT 设备对应的 input_dev。 code:要上报的是什么数据,可以设置为 ABS_MT_POSITION_X 或 ABS_MT_POSITION_Y, 也就是 X 轴或者 Y 轴坐标数据。 value:具体的 X 轴或 Y 轴坐标数据值。 返回值:无。
四.FT5X06触摸驱动
#include <linux/module.h>
#include <linux/ratelimit.h>
#include <linux/interrupt.h>
#include <linux/input.h>
#include <linux/i2c.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/debugfs.h>
#include <linux/slab.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/input/mt.h>
#include <linux/input/touchscreen.h>
#include <linux/input/edt-ft5x06.h>
#include <linux/i2c.h>
#define MAX_SUPPORT_POINTS 5 /* 5点触摸 */
#define TOUCH_EVENT_DOWN 0x00 /* 按下 */
#define TOUCH_EVENT_UP 0x01 /* 抬起 */
#define TOUCH_EVENT_ON 0x02 /* 接触 */
#define TOUCH_EVENT_RESERVED 0x03 /* 保留 */
/* FT5X06寄存器相关宏定义 */
#define FT5X06_TD_STATUS_REG 0X02 /* 状态寄存器地址 */
#define FT5x06_DEVICE_MODE_REG 0X00 /* 模式寄存器 */
#define FT5426_IDG_MODE_REG 0XA4 /* 中断模式 */
#define FT5X06_READLEN 29 /* 要读取的寄存器个数 */
struct ft5x06_dev {
struct device_node *nd; /* 设备节点 */
int irq_pin,reset_pin; /* 中断和复位IO */
int irqnum; /* 中断号 */
void *private_data; /* 私有数据 */
struct input_dev *input; /* input结构体 */
struct i2c_client *client; /* I2C客户端 */
};
static struct ft5x06_dev ft5x06;
/*
* @description : 复位FT5X06
* @param - client : 要操作的i2c
* @param - multidev: 自定义的multitouch设备
* @return : 0,成功;其他负值,失败
*/
static int ft5x06_ts_reset(struct i2c_client *client, struct ft5x06_dev *dev)
{
int ret = 0;
if (gpio_is_valid(dev->reset_pin)) { /* 检查IO是否有效 */
/* 申请复位IO,并且默认输出低电平 */
ret = devm_gpio_request_one(&client->dev,
dev->reset_pin, GPIOF_OUT_INIT_LOW,
"edt-ft5x06 reset");
if (ret) {
return ret;
}
msleep(5);
gpio_set_value(dev->reset_pin, 1); /* 输出高电平,停止复位 */
msleep(300);
}
return 0;
}
/*
* @description : 从FT5X06读取多个寄存器数据
* @param - dev: ft5x06设备
* @param - reg: 要读取的寄存器首地址
* @param - val: 读取到的数据
* @param - len: 要读取的数据长度
* @return : 操作结果
*/
static int ft5x06_read_regs(struct ft5x06_dev *dev, u8 reg, void *val, int len)
{
int ret;
struct i2c_msg msg[2];
struct i2c_client *client = (struct i2c_client *)dev->client;
/* msg[0]为发送要读取的首地址 */
msg[0].addr = client->addr; /* ft5x06地址 */
msg[0].flags = 0; /* 标记为发送数据 */
msg[0].buf = ® /* 读取的首地址 */
msg[0].len = 1; /* reg长度*/
/* msg[1]读取数据 */
msg[1].addr = client->addr; /* ft5x06地址 */
msg[1].flags = I2C_M_RD; /* 标记为读取数据*/
msg[1].buf = val; /* 读取数据缓冲区 */
msg[1].len = len; /* 要读取的数据长度*/
ret = i2c_transfer(client->adapter, msg, 2);
if(ret == 2) {
ret = 0;
} else {
ret = -EREMOTEIO;
}
return ret;
}
/*
* @description : 向ft5x06多个寄存器写入数据
* @param - dev: ft5x06设备
* @param - reg: 要写入的寄存器首地址
* @param - val: 要写入的数据缓冲区
* @param - len: 要写入的数据长度
* @return : 操作结果
*/
static s32 ft5x06_write_regs(struct ft5x06_dev *dev, u8 reg, u8 *buf, u8 len)
{
u8 b[256];
struct i2c_msg msg;
struct i2c_client *client = (struct i2c_client *)dev->client;
b[0] = reg; /* 寄存器首地址 */
memcpy(&b[1],buf,len); /* 将要写入的数据拷贝到数组b里面 */
msg.addr = client->addr; /* ft5x06地址 */
msg.flags = 0; /* 标记为写数据 */
msg.buf = b; /* 要写入的数据缓冲区 */
msg.len = len + 1; /* 要写入的数据长度 */
return i2c_transfer(client->adapter, &msg, 1);
}
/*
* @description : 向ft5x06指定寄存器写入指定的值,写一个寄存器
* @param - dev: ft5x06设备
* @param - reg: 要写的寄存器
* @param - data: 要写入的值
* @return : 无
*/
static void ft5x06_write_reg(struct ft5x06_dev *dev, u8 reg, u8 data)
{
u8 buf = 0;
buf = data;
ft5x06_write_regs(dev, reg, &buf, 1);
}
/*
* @description : FT5X06中断服务函数
* @param - irq : 中断号
* @param - dev_id : 设备结构。
* @return : 中断执行结果
*/
static irqreturn_t ft5x06_handler(int irq, void *dev_id)
{
struct ft5x06_dev *multidata = dev_id;
u8 rdbuf[29];
int i, type, x, y, id;
int offset, tplen;
int ret;
bool down;
offset = 1; /* 偏移1,也就是0X02+1=0x03,从0X03开始是触摸值 */
tplen = 6; /* 一个触摸点有6个寄存器来保存触摸值 */
memset(rdbuf, 0, sizeof(rdbuf)); /* 清除 */
/* 读取FT5X06触摸点坐标从0X02寄存器开始,连续读取29个寄存器 */
ret = ft5x06_read_regs(multidata, FT5X06_TD_STATUS_REG, rdbuf, FT5X06_READLEN);
if (ret) {
goto fail;
}
/* 上报每一个触摸点坐标 */
for (i = 0; i < MAX_SUPPORT_POINTS; i++) {
u8 *buf = &rdbuf[i * tplen + offset];
/* 以第一个触摸点为例,寄存器TOUCH1_XH(地址0X03),各位描述如下:
* bit7:6 Event flag 0:按下 1:释放 2:接触 3:没有事件
* bit5:4 保留
* bit3:0 X轴触摸点的11~8位。
*/
type = buf[0] >> 6; /* 获取触摸类型 */
if (type == TOUCH_EVENT_RESERVED)
continue;
/* 我们所使用的触摸屏和FT5X06是反过来的 */
x = ((buf[2] << 8) | buf[3]) & 0x0fff;
y = ((buf[0] << 8) | buf[1]) & 0x0fff;
/* 以第一个触摸点为例,寄存器TOUCH1_YH(地址0X05),各位描述如下:
* bit7:4 Touch ID 触摸ID,表示是哪个触摸点
* bit3:0 Y轴触摸点的11~8位。
*/
id = (buf[2] >> 4) & 0x0f;
down = type != TOUCH_EVENT_UP;
input_mt_slot(multidata->input, id);
input_mt_report_slot_state(multidata->input, MT_TOOL_FINGER, down);
if (!down)
continue;
input_report_abs(multidata->input, ABS_MT_POSITION_X, x);
input_report_abs(multidata->input, ABS_MT_POSITION_Y, y);
}
input_mt_report_pointer_emulation(multidata->input, true);
input_sync(multidata->input);
fail:
return IRQ_HANDLED;
}
/*
* @description : FT5x06中断初始化
* @param - client : 要操作的i2c
* @param - multidev: 自定义的multitouch设备
* @return : 0,成功;其他负值,失败
*/
static int ft5x06_ts_irq(struct i2c_client *client, struct ft5x06_dev *dev)
{
int ret = 0;
/* 1,申请中断GPIO */
if (gpio_is_valid(dev->irq_pin)) {
ret = devm_gpio_request_one(&client->dev, dev->irq_pin,
GPIOF_IN, "edt-ft5x06 irq");
if (ret) {
dev_err(&client->dev,
"Failed to request GPIO %d, error %d\n",
dev->irq_pin, ret);
return ret;
}
}
/* 2,申请中断,client->irq就是IO中断, */
ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
ft5x06_handler, IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
client->name, &ft5x06);
if (ret) {
dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
return ret;
}
return 0;
}
/*
* @description : i2c驱动的probe函数,当驱动与
* 设备匹配以后此函数就会执行
* @param - client : i2c设备
* @param - id : i2c设备ID
* @return : 0,成功;其他负值,失败
*/
static int ft5x06_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
int ret = 0;
ft5x06.client = client;
/* 1,获取设备树中的中断和复位引脚 */
ft5x06.irq_pin = of_get_named_gpio(client->dev.of_node, "interrupt-gpios", 0);
ft5x06.reset_pin = of_get_named_gpio(client->dev.of_node, "reset-gpios", 0);
/* 2,复位FT5x06 */
ret = ft5x06_ts_reset(client, &ft5x06);
if(ret < 0) {
goto fail;
}
/* 3,初始化中断 */
ret = ft5x06_ts_irq(client, &ft5x06);
if(ret < 0) {
goto fail;
}
/* 4,初始化FT5X06 */
ft5x06_write_reg(&ft5x06, FT5x06_DEVICE_MODE_REG, 0); /* 进入正常模式 */
ft5x06_write_reg(&ft5x06, FT5426_IDG_MODE_REG, 1); /* FT5426中断模式 */
/* 5,input设备注册 */
ft5x06.input = devm_input_allocate_device(&client->dev);
if (!ft5x06.input) {
ret = -ENOMEM;
goto fail;
}
ft5x06.input->name = client->name;
ft5x06.input->id.bustype = BUS_I2C;
ft5x06.input->dev.parent = &client->dev;
__set_bit(EV_KEY, ft5x06.input->evbit);
__set_bit(EV_ABS, ft5x06.input->evbit);
__set_bit(BTN_TOUCH, ft5x06.input->keybit);
input_set_abs_params(ft5x06.input, ABS_X, 0, 1024, 0, 0);
input_set_abs_params(ft5x06.input, ABS_Y, 0, 600, 0, 0);
input_set_abs_params(ft5x06.input, ABS_MT_POSITION_X,0, 1024, 0, 0);
input_set_abs_params(ft5x06.input, ABS_MT_POSITION_Y,0, 600, 0, 0);
ret = input_mt_init_slots(ft5x06.input, MAX_SUPPORT_POINTS, 0);
if (ret) {
goto fail;
}
ret = input_register_device(ft5x06.input);
if (ret)
goto fail;
return 0;
fail:
return ret;
}
/*
* @description : i2c驱动的remove函数,移除i2c驱动的时候此函数会执行
* @param - client : i2c设备
* @return : 0,成功;其他负值,失败
*/
static int ft5x06_ts_remove(struct i2c_client *client)
{
/* 释放input_dev */
input_unregister_device(ft5x06.input);
return 0;
}
/*
* 传统驱动匹配表
*/
static const struct i2c_device_id ft5x06_ts_id[] = {
{ "edt-ft5206", 0, },
{ "edt-ft5426", 0, },
{ /* sentinel */ }
};
/*
* 设备树匹配表
*/
static const struct of_device_id ft5x06_of_match[] = {
{ .compatible = "edt,edt-ft5206", },
{ .compatible = "edt,edt-ft5426", },
{ /* sentinel */ }
};
/* i2c驱动结构体 */
static struct i2c_driver ft5x06_ts_driver = {
.driver = {
.owner = THIS_MODULE,
.name = "edt_ft5x06",
.of_match_table = of_match_ptr(ft5x06_of_match),
},
.id_table = ft5x06_ts_id,
.probe = ft5x06_ts_probe,
.remove = ft5x06_ts_remove,
};
/*
* @description : 驱动入口函数
* @param : 无
* @return : 无
*/
static int __init ft5x06_init(void)
{
int ret = 0;
ret = i2c_add_driver(&ft5x06_ts_driver);
return ret;
}
/*
* @description : 驱动出口函数
* @param : 无
* @return : 无
*/
static void __exit ft5x06_exit(void)
{
i2c_del_driver(&ft5x06_ts_driver);
}
module_init(ft5x06_init);
module_exit(ft5x06_exit);
MODULE_LICENSE("GPL");