实现input设备的注册,点击触摸屏能够产生中断回调

This commit is contained in:
Yu Xifeng 2024-11-26 12:32:01 +00:00
parent d25c56a0e6
commit 74b90cef1a
2 changed files with 123 additions and 89 deletions

View File

@ -18,7 +18,7 @@
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/platform_device.h> /*平台设备*/
#include "cst816_update.h"
@ -27,10 +27,13 @@
#define DEV_CNT (1)
struct cst816x_data {
struct gpio_desc *Interrupt_gpiod; //GPIO
struct input_dev *button_input_dev; //按键输入子系统结构体
struct platform_device *pdev; //平台设备结构体
int irq; //中断irq
};
// struct class *class_cst816d; //保存创建的类
// struct device *device; // 保存创建的设备
// struct device_node *cst816d_device_node; //cst816d的设备树节点
int io_cst816d_reset; // 保存获取得到引脚编号
static dev_t mpu6050_devno; //定义字符设备的设备号
@ -44,37 +47,6 @@ struct device_node *mpu6050_device_node; //rgb_led的设备树节点结构体
struct i2c_client *mpu6050_client = NULL; //保存mpu6050设备对应的i2c_client结构体,匹配成功后由.prob函数带回。
/*通过i2c 向mpu6050写入数据
*mpu6050_client:mpu6050的i2c_client结构体。
*address, 数据要写入的地址,
*data, 要写入的数据
*返回值,错误,-1。成功,0
*/
// static int i2c_write_cst816x(struct i2c_client *mpu6050_client, u8 address, u8 data)
// {
// int error = 0;
// u8 write_data[2];
// struct i2c_msg send_msg; //要发送的数据结构体
// /*设置要发送的数据*/
// write_data[0] = address;
// write_data[1] = data;
// /*发送 iic要写入的地址 reg*/
// send_msg.addr = mpu6050_client->addr; //mpu6050在 iic 总线上的地址
// send_msg.flags = 0; //标记为发送数据
// send_msg.buf = write_data; //写入的首地址
// send_msg.len = 2; //reg长度
// /*执行发送*/
// error = i2c_transfer(mpu6050_client->adapter, &send_msg, 1);
// if (error != 1)
// {
// printk(KERN_DEBUG "\n i2c_transfer error \n");
// return -1;
// }
// return 0;
// }
static int i2c_write_16bit_address(struct i2c_client *client, u16 address, u8 data)
{
@ -304,10 +276,48 @@ static struct file_operations mpu6050_chr_dev_fops =
.release = NULL,
};
/*按键中断处理函数*/
static irqreturn_t button_input_irq_hander(int irq, void *dev_id)
{
// struct button_data *priv = dev_id;
// int button_satus;
// /*读取按键引脚的电平,根据读取得到的结果输入按键状态*/
// button_satus = (gpiod_get_value(priv->button_input_gpiod) & 1);
// if(button_satus)
// {
// input_report_key(priv->button_input_dev, BTN_0, 1);
// input_sync(priv->button_input_dev);
// }
// else
// {
// input_report_key(priv->button_input_dev, BTN_0, 0);
// input_sync(priv->button_input_dev);
// }
printk(KERN_EMERG "\t button_input_irq_hander \n");
return IRQ_HANDLED;
}
static int btn_open(struct input_dev *i_dev)
{
pr_info("input device opened()\n");
return 0;
}
static void btn_close(struct input_dev *i_dev)
{
pr_info("input device closed()\n");
}
/*----------------平台驱动函数集-----------------*/
static int cst816d_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
struct device_node *node=client->dev.of_node;
struct input_dev *i_dev;
struct cst816x_data *priv;
struct gpio_desc *irq_gpio;
int ret = -1; //保存错误状态码
char chip_id = 0;
int i=0;
@ -319,10 +329,31 @@ static int cst816d_probe(struct i2c_client *client, const struct i2c_device_id *
printk(KERN_EMERG "\t match successed \n");
/* 分配输入设备 */
i_dev = input_allocate_device();
if (!i_dev) {
pr_err("CST816D: Failed to allocate input device\n");
return -ENOMEM;
}
/* 分配私有数据 */
priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
if (!priv) {
ret = -ENOMEM;
goto err_free_input;
}
i2c_set_clientdata(client, priv); // 绑定私有数据
io_cst816d_reset = of_get_named_gpio(node, "reset-gpios", 0);
printk(KERN_EMERG "cst816d = %d \n",io_cst816d_reset);
irq_gpio = devm_gpiod_get(&client->dev, "intirq", GPIOD_IN);
if (IS_ERR(irq_gpio))
return -ENODEV;
ret = alloc_chrdev_region(&mpu6050_devno, 0, DEV_CNT, DEV_NAME);
if (ret < 0)
{
@ -330,6 +361,34 @@ static int cst816d_probe(struct i2c_client *client, const struct i2c_device_id *
goto alloc_err;
}
i_dev->open = btn_open;
i_dev->close = btn_close;
i_dev->name = "key input";
i_dev->dev.parent = &client->dev;
priv->button_input_dev = i_dev;
priv->irq = gpiod_to_irq(irq_gpio);
priv->Interrupt_gpiod = irq_gpio;
set_bit(EV_KEY, i_dev->evbit); /*设置要使用的输入事件类型*/
set_bit(BTN_0, i_dev->keybit); /* 按钮 0*/
ret = input_register_device(priv->button_input_dev);
if (ret) {
pr_err("Failed to register inputdevice\n");
goto err_input;
}
ret = request_any_context_irq(priv->irq, button_input_irq_hander,IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "input-button", priv);
if (ret < 0) {
dev_err(&client->dev,"请求gpio中断失败\n");
goto err_btn;
}
//关联字符设备结构体cdev与文件操作结构体file_operations
mpu6050_chr_dev.owner = THIS_MODULE;
cdev_init(&mpu6050_chr_dev, &mpu6050_chr_dev_fops);
@ -411,6 +470,14 @@ static int cst816d_probe(struct i2c_client *client, const struct i2c_device_id *
printk(KERN_EMERG "\n cst816d ok! \n");
return 0;
err_btn:
gpiod_put(priv->Interrupt_gpiod);
err_input:
input_free_device(priv->button_input_dev);
err_free_input:
input_free_device(i_dev);
return ret;
add_err:
// 添加设备失败时,需要注销设备号
unregister_chrdev_region(mpu6050_devno, DEV_CNT);
@ -420,68 +487,35 @@ alloc_err:
return -1;
}
/*----------------平台驱动函数集-----------------*/
// static int mpu6050_probe(struct i2c_client *client, const struct i2c_device_id *id)
// {
// int ret = -1; //保存错误状态码
// printk(KERN_EMERG "\t match successed \n");
// /*---------------------注册 字符设备部分-----------------*/
// //采用动态分配的方式,获取设备编号,次设备号为0,
// //设备名称为rgb-leds,可通过命令cat /proc/devices查看
// //DEV_CNT为1,当前只申请一个设备编号
// ret = alloc_chrdev_region(&mpu6050_devno, 0, DEV_CNT, DEV_NAME);
// if (ret < 0)
// {
// printk("fail to alloc mpu6050_devno\n");
// goto alloc_err;
// }
// //关联字符设备结构体cdev与文件操作结构体file_operations
// mpu6050_chr_dev.owner = THIS_MODULE;
// cdev_init(&mpu6050_chr_dev, &mpu6050_chr_dev_fops);
// // 添加设备至cdev_map散列表中
// ret = cdev_add(&mpu6050_chr_dev, mpu6050_devno, DEV_CNT);
// if (ret < 0)
// {
// printk("fail to add cdev\n");
// goto add_err;
// }
// /*创建类 */
// class_mpu6050 = class_create(THIS_MODULE, DEV_NAME);
// /*创建设备 DEV_NAME 指定设备名,*/
// device_mpu6050 = device_create(class_mpu6050, NULL, mpu6050_devno, NULL, DEV_NAME);
// mpu6050_client = client;
// return 0;
// add_err:
// // 添加设备失败时,需要注销设备号
// unregister_chrdev_region(mpu6050_devno, DEV_CNT);
// printk("\n error! \n");
// alloc_err:
// return -1;
// }
static int cst816d_remove(struct i2c_client *client)
{
/*删除设备*/
device_destroy(class_mpu6050, mpu6050_devno); //清除设备
class_destroy(class_mpu6050); //清除类
cdev_del(&mpu6050_chr_dev); //清除设备号
unregister_chrdev_region(mpu6050_devno, DEV_CNT); //取消注册字符设备
struct cst816x_data *priv = i2c_get_clientdata(client); // 获取驱动私有数据
if (!priv)
return -EINVAL;
/* 1. 注销输入子系统 */
input_unregister_device(priv->button_input_dev);
/* 2. 释放中断 */
free_irq(priv->irq, priv);
/* 3. 释放 GPIO */
gpiod_put(priv->Interrupt_gpiod);
/* 4. 清除字符设备相关资源 */
device_destroy(class_mpu6050, mpu6050_devno);
class_destroy(class_mpu6050);
cdev_del(&mpu6050_chr_dev);
unregister_chrdev_region(mpu6050_devno, DEV_CNT);
return 0;
}
static const struct of_device_id cst816x_of_match[] = {
{ .compatible = "Yura,cst816t"},
{ /* sentinel */ }

Binary file not shown.