矩阵按键驱动
This commit is contained in:
commit
b8555c20cb
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
*.*.cmd
|
||||
*.mod*
|
||||
*.o
|
||||
*.ko
|
||||
*odule*.*
|
||||
bin/
|
||||
16
src/Makefile
Normal file
16
src/Makefile
Normal file
@ -0,0 +1,16 @@
|
||||
KERNEL_DIR=../../../kernel/
|
||||
|
||||
ARCH=arm64
|
||||
CROSS_COMPILE=/opt/gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-
|
||||
export ARCH CROSS_COMPILE
|
||||
|
||||
obj-m := Matrix_keyboard.o
|
||||
|
||||
all:
|
||||
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
|
||||
|
||||
.PHONE:clean
|
||||
|
||||
clean:
|
||||
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean
|
||||
rm $(out)
|
||||
228
src/Matrix_keyboard.c
Normal file
228
src/Matrix_keyboard.c
Normal file
@ -0,0 +1,228 @@
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/of.h> /*设备树*/
|
||||
#include <linux/platform_device.h> /*平台设备*/
|
||||
#include <linux/gpio/consumer.h> /*GPIO 接口描述符*/
|
||||
#include <linux/input.h>
|
||||
#include <linux/interrupt.h>
|
||||
|
||||
|
||||
struct Matrix_data {
|
||||
struct gpio_desc *Matrix_input_gpiod[4]; //GPIO
|
||||
struct input_dev *Matrix_input_dev; //按键输入子系统结构体
|
||||
struct platform_device *pdev; //平台设备结构体
|
||||
int irq[4]; //中断irq
|
||||
};
|
||||
|
||||
|
||||
/*按键中断处理函数*/
|
||||
static irqreturn_t Matrix_input_irq_hander(int irq, void *dev_id)
|
||||
{
|
||||
struct Matrix_data *priv = dev_id;
|
||||
int Matrix_satus;
|
||||
|
||||
/*读取按键引脚的电平,根据读取得到的结果输入按键状态*/
|
||||
Matrix_satus = (gpiod_get_value(priv->Matrix_input_gpiod[0]) & 1);
|
||||
if(Matrix_satus)
|
||||
{
|
||||
input_report_key(priv->Matrix_input_dev, BTN_3, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
input_report_key(priv->Matrix_input_dev, BTN_3, 0);
|
||||
}
|
||||
Matrix_satus = (gpiod_get_value(priv->Matrix_input_gpiod[1]) & 1);
|
||||
if(Matrix_satus)
|
||||
{
|
||||
input_report_key(priv->Matrix_input_dev, BTN_8, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
input_report_key(priv->Matrix_input_dev, BTN_8, 0);
|
||||
}
|
||||
Matrix_satus = (gpiod_get_value(priv->Matrix_input_gpiod[2]) & 1);
|
||||
if(Matrix_satus)
|
||||
{
|
||||
input_report_key(priv->Matrix_input_dev, BTN_5, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
input_report_key(priv->Matrix_input_dev, BTN_5, 0);
|
||||
}
|
||||
Matrix_satus = (gpiod_get_value(priv->Matrix_input_gpiod[3]) & 1);
|
||||
if(Matrix_satus)
|
||||
{
|
||||
input_report_key(priv->Matrix_input_dev, BTN_6, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
input_report_key(priv->Matrix_input_dev, BTN_6, 0);
|
||||
}
|
||||
input_sync(priv->Matrix_input_dev);
|
||||
|
||||
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 Matrix_probe(struct platform_device *pdev)
|
||||
{
|
||||
struct Matrix_data *priv;
|
||||
struct gpio_desc *gpiod;
|
||||
struct input_dev *i_dev;
|
||||
int ret;
|
||||
|
||||
pr_info("Matrix_probe\n");
|
||||
|
||||
priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
|
||||
if (!priv)
|
||||
return -ENOMEM;
|
||||
|
||||
i_dev = input_allocate_device();
|
||||
if (!i_dev)
|
||||
return -ENOMEM;
|
||||
|
||||
i_dev->open = btn_open;
|
||||
i_dev->close = btn_close;
|
||||
i_dev->name = "Yura key Matrix";
|
||||
i_dev->dev.parent = &pdev->dev;
|
||||
priv->Matrix_input_dev = i_dev;
|
||||
priv->pdev = pdev;
|
||||
|
||||
set_bit(EV_KEY, i_dev->evbit); /* 设置要使用的输入事件类型 */
|
||||
set_bit(BTN_3, i_dev->keybit); /* 按钮 0 */
|
||||
set_bit(BTN_8, i_dev->keybit); /* 按钮 1 */
|
||||
set_bit(BTN_5, i_dev->keybit); /* 按钮 2 */
|
||||
set_bit(BTN_6, i_dev->keybit); /* 按钮 3 */
|
||||
|
||||
/* 获取第一个 GPIO 引脚,并设置为输入 */
|
||||
gpiod = gpiod_get(&pdev->dev, "up_key", GPIOD_IN);
|
||||
if (IS_ERR(gpiod))
|
||||
return -ENODEV;
|
||||
printk(KERN_EMERG "gpio num up_key\n");
|
||||
priv->irq[0] = gpiod_to_irq(gpiod); // 为第一个按键获取中断号
|
||||
priv->Matrix_input_gpiod[0] = gpiod;
|
||||
|
||||
/* 获取第二个 GPIO 引脚,并设置为输入 */
|
||||
gpiod = gpiod_get(&pdev->dev, "down_key", GPIOD_IN);
|
||||
if (IS_ERR(gpiod))
|
||||
return -ENODEV;
|
||||
printk(KERN_EMERG "gpio num down_key\n");
|
||||
priv->irq[1] = gpiod_to_irq(gpiod); // 为第二个按键获取中断号
|
||||
priv->Matrix_input_gpiod[1] = gpiod;
|
||||
|
||||
/* 获取第三个 GPIO 引脚,并设置为输入 */
|
||||
gpiod = gpiod_get(&pdev->dev, "left_key", GPIOD_IN);
|
||||
if (IS_ERR(gpiod))
|
||||
return -ENODEV;
|
||||
printk(KERN_EMERG "gpio num left_key\n");
|
||||
priv->irq[2] = gpiod_to_irq(gpiod); // 为第三个按键获取中断号
|
||||
priv->Matrix_input_gpiod[2] = gpiod;
|
||||
|
||||
/* 获取第四个 GPIO 引脚,并设置为输入 */
|
||||
gpiod = gpiod_get(&pdev->dev, "right_key", GPIOD_IN);
|
||||
if (IS_ERR(gpiod))
|
||||
return -ENODEV;
|
||||
printk(KERN_EMERG "gpio num right_key\n");
|
||||
priv->irq[3] = gpiod_to_irq(gpiod); // 为第四个按键获取中断号
|
||||
priv->Matrix_input_gpiod[3] = gpiod;
|
||||
|
||||
/* 注册输入设备 */
|
||||
ret = input_register_device(priv->Matrix_input_dev);
|
||||
if (ret) {
|
||||
pr_err("Failed to register input device\n");
|
||||
goto err_input;
|
||||
}
|
||||
|
||||
platform_set_drvdata(pdev, priv);
|
||||
|
||||
/* 为每个按键请求中断 */
|
||||
ret = request_irq(priv->irq[0], Matrix_input_irq_hander, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "input-button-up", priv);
|
||||
if (ret < 0) {
|
||||
dev_err(&pdev->dev, "请求 gpio up_key 中断失败\n");
|
||||
goto err_btn;
|
||||
}
|
||||
|
||||
ret = request_irq(priv->irq[1], Matrix_input_irq_hander, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "input-button-down", priv);
|
||||
if (ret < 0) {
|
||||
dev_err(&pdev->dev, "请求 gpio down_key 中断失败\n");
|
||||
goto err_btn;
|
||||
}
|
||||
|
||||
ret = request_irq(priv->irq[2], Matrix_input_irq_hander, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "input-button-down", priv);
|
||||
if (ret < 0) {
|
||||
dev_err(&pdev->dev, "请求 gpio left_key 中断失败\n");
|
||||
goto err_btn;
|
||||
}
|
||||
|
||||
ret = request_irq(priv->irq[3], Matrix_input_irq_hander, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "input-button-down", priv);
|
||||
if (ret < 0) {
|
||||
dev_err(&pdev->dev, "请求 gpio right_key 中断失败\n");
|
||||
goto err_btn;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
err_btn:
|
||||
printk(KERN_EMERG "\t err_btn \n");
|
||||
gpiod_put(priv->Matrix_input_gpiod[0]);
|
||||
gpiod_put(priv->Matrix_input_gpiod[1]);
|
||||
gpiod_put(priv->Matrix_input_gpiod[2]);
|
||||
gpiod_put(priv->Matrix_input_gpiod[3]);
|
||||
err_input:
|
||||
printk(KERN_EMERG "\t err_input \n");
|
||||
input_free_device(priv->Matrix_input_dev);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static int Matrix_remove(struct platform_device *pdev)
|
||||
{
|
||||
struct Matrix_data *priv;
|
||||
priv = platform_get_drvdata(pdev);
|
||||
input_unregister_device(priv->Matrix_input_dev);
|
||||
input_free_device(priv->Matrix_input_dev);
|
||||
free_irq(priv->irq[0], priv);
|
||||
free_irq(priv->irq[1], priv);
|
||||
free_irq(priv->irq[2], priv);
|
||||
free_irq(priv->irq[3], priv);
|
||||
gpiod_put(priv->Matrix_input_gpiod[0]);
|
||||
gpiod_put(priv->Matrix_input_gpiod[1]);
|
||||
gpiod_put(priv->Matrix_input_gpiod[2]);
|
||||
gpiod_put(priv->Matrix_input_gpiod[3]);
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
static const struct of_device_id Matrix_dt_ids[] =
|
||||
{
|
||||
{ .compatible = "Yura,Matrix_keyboard", },
|
||||
{ /*标记结束*/ }
|
||||
};
|
||||
|
||||
static struct platform_driver Matrix_input = {
|
||||
.probe = Matrix_probe,
|
||||
.remove = Matrix_remove,
|
||||
.driver = {
|
||||
.name = "button-input",
|
||||
.of_match_table = of_match_ptr(Matrix_dt_ids),
|
||||
.owner = THIS_MODULE,
|
||||
},
|
||||
};
|
||||
|
||||
module_platform_driver(Matrix_input);
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
MODULE_AUTHOR("llh<zgwinli555@163.com>");
|
||||
MODULE_DESCRIPTION("Embedfire,Input key device");
|
||||
Loading…
Reference in New Issue
Block a user