本文主要是介绍添加指定gpio到input系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近写驱动时,需要把器件的一个引脚设置为中断,当有电平变化时,将其上报给应用,我们这边选择使用input系统,下面记录一下添加的过程。水平有限,如有不对,欢迎指正。
1、申请内存
//在中断处理函数中最好加个锁,防止重入wake_lock_init(&talkback_pdata->wake_lock, WAKE_LOCK_SUSPEND, "talkback_wake");talkback_pdata->input_dev_sq = devm_input_allocate_device(&pdev->dev);if (!talkback_pdata->input_dev_sq) {err = -ENOMEM;pr_err("[TALKBACK]: %s: Failed to allocate input device als\n", __func__);goto exit1;}
2、申请中断
中断申请成功后,可通过cat /proc/interrupt查询
/* talkback sq gpio */ pdata->gpio_sq = of_get_named_gpio_flags(np, "Basewin,gpio_sq",0, (enum of_gpio_flags *)&pdata->talkback_gpio_flag);if (pdata->gpio_sq < 0){dev_err(dev, "[TALKBACK]: %s: Unable to read talkback gpio(sq)\n", __func__);return pdata->gpio_sq;}/* talkback sq gpio */if(pdata->gpio_sq >= 0){ret = gpio_request(pdata->gpio_sq, "talkback_gpio_sq");if (ret) {printk("[TALKBACK]: %s: Failed to request gpio %d,ret = %d\n",__func__, pdata->gpio_sq, ret);}else{pr_err("[TALKBACK]: %s: gpio requset talkback_gpio_sq successful.\n", __func__); //gpio_direction_input(pdata->gpio_sq);}}if (gpio_is_valid(pdata->gpio_sq)) {/*ret = gpio_request_one(data->platform_data->irq_gpio,GPIOF_DIR_IN,"apds993x_irq_gpio");if (ret) {dev_err(&client->dev, "unable to request gpio %d\n",data->platform_data->irq_gpio);}*/pdata->irq = gpio_to_irq(pdata->gpio_sq);} else {pr_err("[TALKBACK]: %s: irq gpio not provided\n", __func__);} if (talkback_pdata->irq > 0) {//申请中断,中断名字为talkback_sq,数据类型为talk_pdata,该类型会传给中断处理函数,//可在中断处理函数中进行获取,中断处理函数为talkback_sq_interrupt,设置为下降沿触发err = request_threaded_irq(talkback_pdata->irq, NULL, talkback_sq_interrupt,IRQF_TRIGGER_FALLING | IRQF_ONESHOT,"talkback_sq", (void *)talkback_pdata); if (err < 0) {dev_err(&pdev->dev,"[TALKBACK]: %s request irq failed.!\n", __func__);disable_irq(talkback_pdata->irq);goto fail_free_irq;}}else{pr_err("[TALKBACK]: %s talkback_pdata->irq is not value.\n", __func__);goto exit1;}
3、初始化相关成员变量,注册到input
主要是设置上报事件类型以及code码
set_bit(EV_KEY, talkback_pdata->input_dev_sq->evbit);//上报事件类型,按键事件set_bit(KEY_FN_F12, talkback_pdata->input_dev_sq->keybit);//code码,talkback_pdata->input_dev_sq->name = "talkback_sq";//getevnet 可看到err = input_register_device(talkback_pdata->input_dev_sq);//注册input事件if (err) {err = -ENOMEM;pr_err("[TALKBACK]: %s: Unable to register input device als: %s\n",__func__, talkback_pdata->input_dev_sq->name);goto fail_free_irq;}
4、中断处理函数
中断函数中调用input_report_key, input_sync之后,input事件就会上报上去,可使用getevent进行查看
static irqreturn_t talkback_sq_interrupt(int irq, void *dev_id)
{struct talkback_platform_data *pdata = dev_id;//pr_debug("[TALKBACK]: %s: enter.\n", __func__);wake_lock(&pdata->wake_lock);//最好添加锁if(gpio_is_valid(pdata->gpio_sq)){//pr_debug("[TALKBACK]: %s: report data.\n", __func__);input_report_key(pdata->input_dev_sq, KEY_FN_F12, gpio_get_value(pdata->gpio_sq));input_sync(pdata->input_dev_sq);}wake_unlock(&pdata->wake_lock);return IRQ_HANDLED;
}
上述步骤完成后,即完成了将gpio添加到input系统的工作,可用getevnt进行查看
这篇关于添加指定gpio到input系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!