Linux GPIO中断编程入门之使用设备树的单按键模块入门

使用的设备树如下:

my_platform_device003 {
		status = "okay";
		compatible ="my_platform_device_003";
		touch-gpio = <&gpio2 18 IRQ_TYPE_EDGE_FALLING>;
		reset-gpio= <&gpio3 31 GPIO_ACTIVE_HIGH>;
	};
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 			/* For BUG_ON.  */
#include 
#include  /* Needed for the macros */
#include  /* Needed for pr_info() */
#include  /* Needed by all modules */
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

/**
 *
 *
 *
 my_platform_device003 {
 status = "okay";
 compatible ="my_platform_device_003";
 touch-gpio = <&gpio2 18 IRQ_TYPE_EDGE_FALLING>;
 reset-gpio= <&gpio3 31 GPIO_ACTIVE_HIGH>;
 };

 RK3399 has 5 groups of GPIO banks: GPIO0~GPIO4, and each group is distinguished by numbers A0~A7, B0~B7, C0~C7, D0~D7.


 */

static irqreturn_t key_handler(int irq, void *dev_id) {
	pr_info("key_handler \n");
	return IRQ_HANDLED;
}

int myprobe(struct platform_device *pdev) {
	int ret;
	int irq_pin;
	int irq_flags;
	int irq;
	struct device_node *np = pdev->dev.of_node;

	irq_pin = of_get_named_gpio_flags(np, "touch-gpio", 0,
			(enum of_gpio_flags*) (&irq_flags));
	if (irq_pin < 0) {
		pr_err("of_get_named_gpio_flags failed \n");
		return irq_pin;
	}

	irq = gpio_to_irq(irq_pin);

	pr_info("irq_flags:%d\n", irq_flags);

	ret = devm_request_irq(&pdev->dev, irq, key_handler, irq_flags,
			"my_key_driver",
			NULL);

	if (ret < 0) {
		pr_err("request_irq failed \n");
		return ret;
	}
	return 0;
}

int myremove(struct platform_device *pdev) {
	pr_info("myplatformdriver myremove \n");
	return 0;
}

struct of_device_id my_of_match_table =
		{ .compatible = "my_platform_device_003", };

struct platform_driver my_platform_driver = { .driver =
		{ .of_match_table = &my_of_match_table, .name = "my-platform-driver",
				.owner = THIS_MODULE, }, .probe = myprobe, .remove = myremove, };

module_platform_driver(my_platform_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");

此处是完整代码

你可能感兴趣的:(GPIO,linux,c++,运维)