Linux驱动开发之触摸驱动

一、引言

前一篇文章介绍了Input子系统的基本架构和驱动编写流程。

这一篇文章来介绍一下在实际项目中,我们应该来如何编写触摸驱动。

二、触摸(TP)驱动

1、触摸原理

在学习如何编写触摸驱动之前,我觉得有必要先对触摸原理有一定的了解。

TP芯片就是触摸芯片,它一般都被放置在触摸面板上,当用户点击显示屏,TP芯片内部固件程序会计算出对应的坐标值数据,然后会将对应中断脚给拉低。

硬线中断拉低,会通过信号线透传到Cpu的INT脚上,Cpu感受到INT脚被拉低,触发硬件中断,这时候通过提前注册好的中断服务程序,内核驱动就知道有触摸事件来临。

然后中断服务程序会启动一个工作队列用以读取坐标。读取坐标是以I2C方式读取。

2、编写TP驱动流程

上面了解了触摸原理,知道了触摸驱动实际上就是一个中断服务程序外面包裹了一层I2C驱动。

因此,编写TP驱动第一步,就是先创建一个I2C子设备驱动。

(不熟悉I2C驱动框架的可以去看我的这一篇博客:https://blog.csdn.net/weixin_45244289/article/details/109246482

下面举一个没有设备树版本的Linux触摸驱动源码:

static const struct i2c_device_id goodix_ts_id[] = {
    { GTP_I2C_NAME, 0 },
    { }
};

static struct i2c_driver goodix_ts_driver = {
    .probe      = goodix_ts_probe,
    .remove     = goodix_ts_remove,
    .id_table   = goodix_ts_id,
    .driver = {
        .name     = GTP_I2C_NAME,
        .owner    = THIS_MODULE,
    },
};

static struct i2c_client *client;
static struct i2c_board_info goodix_i2c_clients = {

    I2C_BOARD_INFO(GTP_I2C_NAME, 0x5d),
    .type       = GTP_I2C_NAME,
};

static int goodix_dev_init(struct i2c_board_info *clients)
{
    int ret = 0;
    struct i2c_adapter *adapter;
    adapter = i2c_get_adapter(0);
    if(adapter)
    {
        client = i2c_new_device(adapter, clients);
        if(!client)
            ret=-ENOMEM;
    }
    else
        ret=-ENODEV;
    return ret;
}

static int __devinit goodix_ts_init(void)
{
    s32 ret;

    ret = goodix_dev_init(&goodix_i2c_clients);
    if( ret != 0 ) return ret;

    GTP_INFO("GTP driver installing...");
    goodix_wq = create_singlethread_workqueue("goodix_wq");
    if (!goodix_wq)
    {
        GTP_ERROR("Creat workqueue failed.");
        return -ENOMEM;
    }
    ret = i2c_add_driver(&goodix_ts_driver);
    return ret;
}

static void __exit goodix_ts_exit(void)
{
    GTP_DEBUG_FUNC();
    GTP_INFO("GTP driver exited.");
    i2c_del_driver(&goodix_ts_driver);
    if (goodix_wq)
    {
        destroy_workqueue(goodix_wq);
    }
}

module_init(goodix_ts_init);
module_exit(goodix_ts_exit);

MODULE_DESCRIPTION("GTP Series Driver");
MODULE_LICENSE("GPL");

第43行,因为没有设备树,所以I2C子设备需要自己注册。

第47行,创建一个工作队列,以备后面用来读取触摸坐标。对工作队列不了解的可以看我这一篇博客:https://mp.csdn.net/mp_blog/creation/editor/109445779

第53行添加一个I2C子设备驱动进入内核,这样前面注册的I2C子设备就能和现在添加的I2C子设备驱动匹配,进而调用其Probe函数。

static int goodix_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
    s32 ret;
    struct goodix_ts_data *ts;
    u16 version_info;
    static const char *sp_info[TOUCH_INFO_SIZE] = {
        [TOUCH_PANEL_TYPE]      =  "c-type",
        [TOUCH_PANEL_HAS_KEY]   =  "no",
        [TOUCH_DRV_NAME]        =  "c-gt9xx",
        [TOUCH_DRV_DATE]        =  "2016/09/07",
        [TOUCH_SUPPORT_MT]      =  "yes",};

    //do NOT remove these logs
    GTP_INFO("GTP Driver Version: %s", GTP_DRIVER_VERSION);
    GTP_INFO("GTP Driver Built@%s, %s", __TIME__, __DATE__);
    GTP_INFO("GTP I2C Address: 0x%02x", client->addr);

    i2c_connect_client = client;

    if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
    {
        GTP_ERROR("I2C check functionality failed.");
        return -ENODEV;
    }
    ts = kzalloc(sizeof(*ts), GFP_KERNEL);
    if (ts == NULL)
    {
        GTP_ERROR("Alloc GFP_KERNEL memory failed.");
        return -ENOMEM;
    }

    memset(ts, 0, sizeof(*ts));
    INIT_WORK(&ts->work, goodix_ts_work_func);
    ts->client = client;
    i2c_set_clientdata(client, ts);

    ts->gtp_rawdiff_mode = 0;

    ret = gtp_request_io_port(ts);
    if (ret < 0)
    {
        GTP_ERROR("GTP request IO port failed.");
        kfree(ts);
        return ret;
    }

    ret = gtp_i2c_test(client);
    if (ret < 0)
    {
        GTP_ERROR("I2C communication ERROR!");
    }

    ret = gtp_read_version(client, &version_info);
    if (ret < 0)
    {
        GTP_ERROR("Read version failed.");
    }

    ret = gtp_init_panel(ts);
    if (ret < 0)
    {
        GTP_ERROR("GTP init panel failed.");
        ts->abs_x_max = GTP_MAX_WIDTH;
        ts->abs_y_max = GTP_MAX_HEIGHT;
        ts->int_trigger_type = GTP_INT_TRIGGER;
    }

    ret = gtp_request_input_dev(ts);
    if (ret < 0)
    {
        GTP_ERROR("GTP request input dev failed");
    }

    ret = gtp_request_irq(ts);
    if (ret < 0)
    {
        GTP_INFO("GTP works in polling mode.");
    }
    else
    {
        GTP_INFO("GTP works in interrupt mode.");
    }

    if (ts->use_irq)
    {
        gtp_irq_enable(ts);
    }

    return 0;
}

第20行,检测I2C总线的I2C功能是否正常可用。

第33行,初始化工作结构体和工作函数。

第39行,向内核申请需要用到的GPIO,比如TP的复位脚和中断脚。(对于ARM而言,每一个Pin脚都有其规定好的功能,比如普通GPIO,比如串口,比如I2C等,所以你想要将一些Pin脚设置为你想要的功能,就必须向内核申请。)

第68行,申请一个输入子设备,并初始化好相应的事件类型和支持的事件值,源码如下:

static s8 gtp_request_input_dev(struct goodix_ts_data *ts)
{
    s8 ret;
    char phys[32];

    ts->input_dev = input_allocate_device();
    if (ts->input_dev == NULL)
    {
        GTP_ERROR("Failed to allocate input device.");
        return -ENOMEM;
    }

    ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;
    ts->input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
    __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);

    input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0, ts->abs_x_max, 0, 0);
    input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0, ts->abs_y_max, 0, 0);
    input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
    input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
    input_set_abs_params(ts->input_dev, ABS_MT_TRACKING_ID, 0, 255, 0, 0);

    sprintf(phys, "input/ts");
    ts->input_dev->name = goodix_ts_name;
    ts->input_dev->phys = phys;
    ts->input_dev->id.bustype = BUS_I2C;
    ts->input_dev->id.vendor = 0xDEAD;
    ts->input_dev->id.product = 0xBEEF;
    ts->input_dev->id.version = 10427;

    ret = input_register_device(ts->input_dev);
    if (ret)
    {
        GTP_ERROR("Register %s input device failed", ts->input_dev->name);
        return -ENODEV;
    }

    return 0;
}

第6行,申请一个input_dev。

第13行,设置事件类型位图支持同步事件、按键事件和绝对坐标事件。

第14行,设置按键事件支持触摸事件值。

第17~18行,设置支持的x,y坐标的最大最小值。

第24~29,初始化input_dev相关参数。

第31行,向内核注册input_dev。

继续回到probe函数。

第74行,用上面申请来的中断脚去向内核注册一个中断和相关中断服务函数,源码如下:

static s8 gtp_request_irq(struct goodix_ts_data *ts)
{
    s32 ret;
    const u8 irq_table[] = GTP_IRQ_TAB;

    GTP_DEBUG_FUNC();
    GTP_DEBUG("INT trigger type:%x", ts->int_trigger_type);

    ret  = request_irq(ts->client->irq,
                       goodix_ts_irq_handler,
                       irq_table[ts->int_trigger_type],
                       ts->client->name,
                       ts);
    if (ret)
    {
        GTP_ERROR("Request IRQ failed!ERRNO:%d.", ret);
        GTP_GPIO_AS_INPUT(GTP_INT_PORT);
        GTP_GPIO_FREE(GTP_INT_PORT);

        hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
        ts->timer.function = goodix_ts_timer_handler;
        hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
        return -1;
    }
    else
    {
        gtp_irq_disable(ts);
        ts->use_irq = 1;
        return 0;
    }
}

第9行,申请中断,goodix_ts_irq_handler为中断服务函数,当中断到来,这个函数就会被调用。

第25~30行,中断申请成功,先禁用中断。

回到probe函数。

第84~87行,如果使用中断,则使能中断。

到这里,probe函数就执行完了,接下来,就是中断到来的处理流程了。

我们先看到中断服务函数。内容如下:

static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
{
    struct goodix_ts_data *ts = dev_id;

    gtp_irq_disable(ts);

    queue_work(goodix_wq, &ts->work);

    return IRQ_HANDLED;
}

可以看到,中断服务函数内部实际上就只是禁用了中断和调用了queue_work。

Tips:Linux内核规定,中断服务函数要尽量的快,且不能在其中做睡眠等操作。

queue_work接口则是调用了工作结构体对应的工作函数,大家是否还记得probe函数里面的这一步操作——“INIT_WORK(&ts->work, goodix_ts_work_func);”。

goodix_ts_work_func就是具体的工作函数,来看源码:

static void goodix_ts_work_func(struct work_struct *work)
{
    ……
    ret = gtp_i2c_read(ts->client, point_data, 12);
    if (ret < 0)
    {
        GTP_ERROR("I2C transfer error. errno:%d\n ", ret);
        if (ts->use_irq)
        {
            gtp_irq_enable(ts);
        }
        return;
    }

    finger = point_data[GTP_ADDR_LENGTH];
    ……

    if (touch_num)
    {
        for (i = 0; i < touch_num; i++)
        {
            coor_data = &point_data[i * 8 + 3];

            id = coor_data[0] & 0x0F;
            input_x  = coor_data[1] | (coor_data[2] << 8);
            input_y  = coor_data[3] | (coor_data[4] << 8);
            input_w  = coor_data[5] | (coor_data[6] << 8);

            gtp_touch_down(ts, id, input_x, input_y, input_w);
        }
    }
    else if (pre_touch)
    {
        GTP_DEBUG("Touch Release!");
        gtp_touch_up(ts, 0);
    }

    pre_touch = touch_num;

    input_sync(ts->input_dev);
    
    if (ts->use_irq)
    {
        gtp_irq_enable(ts);
    }
}

工作函数里面主要是用I2C去读取来自触摸屏的I2C数据,然后根据触摸协议去解析x和y的值。

第29行,当解析到x和y坐标后,就调用gtp_touch_down接口上报触摸按下的事件。

gtp_touch_up则是上报触摸抬起事件。

一次完整的触摸事件,根据Input子系统协议规定,必须有按下和抬起的动作,否则用户层是检测不到你的触摸操作的。

最后,当坐标上报完之后就需要使用input_sync上报同步事件,告诉Linux内核,你的一次触摸完毕。同时,因为之前禁用了中断,所以这里需要重新使能中断,否则就再也收不到触摸中断了。

static void gtp_touch_down(struct goodix_ts_data* ts,s32 id,s32 x,s32 y,s32 w)
{
    input_mt_slot(ts->input_dev, id);
    input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, id);
    input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
    input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
    input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, w);
    input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, w);

    GTP_DEBUG("ID:%d, X:%d, Y:%d, W:%d", id, x, y, w);
}

static void gtp_touch_up(struct goodix_ts_data* ts, s32 id)
{
    input_mt_slot(ts->input_dev, id);
    input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, -1);
    GTP_DEBUG("Touch id[%2d] release!", id);
}

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值