在tinker board 2s上实现使用设备树的按键驱动

本文介绍了如何在RK3399平台上配置GPIO引脚,使用设备树代码指定GPIO地址,并提供了一个驱动示例,包括打开设备、读写操作和中断处理。重点展示了如何通过设备树获取内存地址,以及设置GPIO方向和触发中断。
摘要由CSDN通过智能技术生成

1 需要添加的设备树代码如下:

my_platform_device002 {
		status = "okay";
		reg = <0x0 0xFF730000 0x0 0x8>;
		compatible ="my_platform_device_002";
	};

我们的按键模块使用的GPIO引脚是 物理编号是21,查RK3399手册得到首地址是
0xFF730000,具体可以看之前的文章。

2 在驱动中获取这个地址的代码是:

res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	direction = ioremap(res->start, 4);

也就是 res->start 这个值就是我们设备树中配置的 0xFF730000 。

以下是全部的驱动代码:

#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/cdev.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/mutex.h>
#include <linux/interrupt.h>
#include <linux/bug.h>			/* For BUG_ON.  */
#include <linux/cpu.h>
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h> /* Needed for pr_info() */
#include <linux/module.h> /* Needed by all modules */
#include <linux/delay.h>
#include <linux/smp.h>
#include <linux/kernel_stat.h>
#include <linux/sched.h>
#include <linux/percpu-defs.h>
#include <linux/wait.h>
#include <linux/gpio/driver.h>
#include <linux/atomic.h>
#include <linux/platform_device.h>
#include <linux/poll.h>
#include <linux/kfifo.h>

/**
 *
	my_platform_device002 {
		status = "okay";
		reg = <0x0 0xFF730000 0x0 0x8>;
		compatible ="my_platform_device_002";
	};


 */

#define MY_MAX_MINORS  1
#define   DEVICE_NAME  "device name 500"


#define  GPIO_PIN  39
#define LOCATION  7

DEFINE_KFIFO(myfifo, char, 1024);

struct my_device_data {
	struct cdev cdev;
};

DECLARE_WAIT_QUEUE_HEAD(wq);

static struct my_device_data devs[MY_MAX_MINORS];

static int my_open(struct inode *inode, struct file *file) {
	pr_info("a3 my_open \n");
	return 0;
}

static ssize_t my_write(struct file *filp, const char __user *user_buffer,
		size_t size, loff_t *offset) {
	int ret;
	unsigned int len = 0;
	pr_info("write");
	ret = kfifo_from_user(&myfifo, user_buffer, size, &len);
	if (ret != 0) {
		pr_err("kfifo_from_user error");
		return 0;
	}
	if (len <= 0)
		return 0;
	*offset += len;
	wake_up(&wq);
	return len;
}

static ssize_t my_read(struct file *filp, char __user *user_buffer,
		size_t count, loff_t *offset) {
	int ret;
	unsigned int len = 0;
	pr_info("read");
	ret = kfifo_to_user(&myfifo, user_buffer, count, &len);
	if (len <= 0)
		return 0;
	*offset += len;
	return len;
}

unsigned int my_poll(struct file *flip, struct poll_table_struct *table) {
	int mask = 0;
	pr_info("my_poll \n");
	poll_wait(flip, &wq, table);
	if (kfifo_is_empty(&myfifo)) {

	} else {
		mask |= POLLIN | POLLRDNORM;
	}
	return mask;
}

int my_close(struct inode *inode, struct file *flip) {
	pr_info("my_close \n");
	return 0;
}

struct fasync_struct *fasync_struct;

int my_fasync(int fd, struct file *flip, int on) {
	return fasync_helper(fd, flip, on, &fasync_struct);
}

static const struct file_operations my_fops = { .owner = THIS_MODULE, .open =
		my_open, .read = my_read, .write = my_write, .poll = my_poll, .release =
		my_close, .fasync = my_fasync, };

static dev_t mydev;

void key_tasklet_handler(unsigned long data) {
	pr_info("tasklet_handler start \n");
	mdelay(5);
	pr_info("tasklet_handler end\n");
}

DECLARE_TASKLET(key_tasklet, key_tasklet_handler, (unsigned long ) &mydev);

static irqreturn_t key_handler(int irq, void *dev_id) {
	pr_info("key_handler \n");
	kill_fasync(&fasync_struct, SIGIO, POLL_OUT);
	tasklet_schedule(&key_tasklet);
	return IRQ_HANDLED;
}

unsigned int num;

int myprobe(struct platform_device *pdev) {
	int i;
	int err;
	int ret;

	struct resource *res;

	unsigned int __iomem *irq ;
	unsigned int __iomem *direction;
	unsigned int a ;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	direction = ioremap(res->start, 4);
	a = readl(direction);
		pr_err("res->start :%lluX\n", res->start);
	irq = ioremap(res->start + 0x0030, 4);
	pr_info("myplatformdriver myprobe \n");

	err = alloc_chrdev_region(&mydev, 0, MY_MAX_MINORS, DEVICE_NAME);
	if (err != 0) {
		return err;
	}

	for (i = 0; i < MY_MAX_MINORS; i++) {
		/* initialize devs[i] fields */
		cdev_init(&devs[i].cdev, &my_fops);
		cdev_add(&devs[i].cdev, MKDEV(MAJOR(mydev), i), 1);
	}


	// set direction in
	writel(readl(direction) & (~(1 << LOCATION)), direction);
	//just check
	a = readl(direction);
	pr_err("direction: %d \n", a);

	pr_err("direction 1 or 0  ? : %d \n", (a & (1 << LOCATION)) >> LOCATION);

// enable Interrupt
	writel(readl(irq) | (1 << LOCATION), irq);

	pr_err("Interrupt 1 or 0  ? : %d \n",
			(readl(irq) & (1 << LOCATION)) >> LOCATION);
	num = gpio_to_irq(GPIO_PIN);
	pr_info("irq_num:%d\n", num);
	ret = request_irq(num, key_handler, IRQF_TRIGGER_FALLING, "my_key_driver",
			NULL);

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

int myremove(struct platform_device *pdev) {
	int i;
	free_irq(num, NULL);
	pr_info("myplatformdriver myremove \n");
	pr_info("a3 cleanup_module\n");
	for (i = 0; i < MY_MAX_MINORS; i++) {
		/* release devs[i] fields */
		cdev_del(&devs[i].cdev);
	}
	unregister_chrdev_region(mydev, MY_MAX_MINORS);
	return 0;
}

int mysuspend(struct device *pdev, pm_message_t state) {
	pr_info("myplatformdriver mysuspend \n");
	return 0;
}

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

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

struct platform_driver my_platform_driver = { .driver = { .of_match_table =
		&my_of_match_table, .name = "my-platform-driver", .owner = THIS_MODULE,
		.suspend = mysuspend, .resume = myresume, }, .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");

具体安装测试请看之前的文章。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值