platform设备驱动之按键扫描(misc设备)

一、开发环境

1、硬件平台:FS2410

2、主机:Ubuntu 10.10

3、内核版本:linux 2.6.35

4、交叉编译工具链:arm-none-linux-gnueabi-


二、详细代码

1、button_device:

#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/kernel.h>
#include <asm/irq.h>

struct resource button_resource[] = {
	[0] = {
		.start = 0x56000040,
		.end = 0x56000064,
		.flags = IORESOURCE_MEM,
	},

	[1] = {
		.start = IRQ_EINT0,
		.end = IRQ_EINT0,
		.flags = IORESOURCE_IRQ,
	},

	[2] = {
		.start = IRQ_EINT2,
		.end = IRQ_EINT2,
		.flags = IORESOURCE_IRQ,
	},

	[3] = {
		.start = IRQ_EINT11,
		.end = IRQ_EINT11, 
		.flags = IORESOURCE_IRQ,
	},

	[4] = {
		.start = IRQ_EINT19,
		.end = IRQ_EINT19,
		.flags = IORESOURCE_IRQ,
	},

};

struct platform_device button_device = {
	.name = "button",
	.id = -1,
	.num_resources = ARRAY_SIZE(button_resource),
	.resource = button_resource,
};

static int __init button_device_init(void)
{
	int ret;

	ret = platform_device_register(&button_device);

	if(ret < 0)
		printk("failed to register button_device");

	return ret;
}

static void __exit button_device_exit(void)
{
	platform_device_unregister(&button_device);

	return;
}

module_init(button_device_init);
module_exit(button_device_exit);

MODULE_AUTHOR("yhr");
MODULE_LICENSE("GPL");

button_driver.c:

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/time.h>
#include <linux/irq.h>
#include <asm/delay.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include <linux/poll.h>
#include <asm/irq.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <mach/regs-gpio.h>
						
#define EXTINT0		*(volatile unsigned int *)S3C2410_EXTINT0
#define EXTINT1		*(volatile unsigned int *)S3C2410_EXTINT1
#define EXTINT2		*(volatile unsigned int *)S3C2410_EXTINT2


struct button_dev
{
	unsigned char key_value;
	wait_queue_head_t r_wait;
};

struct button_irq_desc
{
	int irq;
};

static struct button_irq_desc button_irqs[] = 
{
	{0}, {0}, {0}, {0},

};

struct button_dev dev;

static volatile unsigned int  *gpecon;
static volatile unsigned int  *gpedat;
static volatile unsigned int  *gpfcon;
static volatile unsigned int  *gpfdat;
static volatile unsigned int  *gpgcon;
static volatile unsigned int  *gpgdat;

static void init_gpio(void)
{
	int i;

	writel(readl(gpecon) & (~((3 << 26) | (3 << 22))), gpecon);
	writel(readl(gpecon) | ((1 << 26) | (1 << 22)), gpecon);
	
	writel(readl(gpedat) & ~((1 << 13) | (1 << 11)), gpedat);

	writel(readl(gpgcon) & (~((3 << 12) | (3 << 4))), gpgcon);
	writel(readl(gpgcon) | ((1 << 12) | (1 << 4)), gpgcon);
	
	writel(readl(gpgdat) & ~((1 << 6) | (1 << 2)), gpgdat);

	writel(readl(gpfcon) & (~((3 << 0) | (3 << 4))), gpfcon);
	writel(readl(gpfcon) | ((2 << 0) | (2 << 4)), gpfcon);
	
	writel(readl(gpgcon) & (~((3 << 6) | (3  << 22))), gpgcon);
	writel(readl(gpgcon) | ((2 << 6) | (2 << 22)), gpgcon);

	for(i = 0; i < 4; i++)
	{
		set_irq_type(button_irqs[i].irq, IRQF_TRIGGER_FALLING);
	}

    EXTINT0 = (EXTINT0 & (~0x07)) + 0x02; 
    EXTINT0 = (EXTINT0 & (~(0x07 << 8))) + (0x02 << 8);
	EXTINT1 = (EXTINT1 & (~(0x07 << 12))) + (0x02 << 12);
	EXTINT2 = (EXTINT2 & (~(0x07 << 12))) + (0x02 << 12);

}


static __inline unsigned char button_scan(int irq)
{
	long lGPF, lGPG;     	
	
	writel(readl(gpfcon) & ~(3 << 0), gpfcon);
	writel(readl(gpfcon) & ~(3 << 4), gpfcon);
	writel(readl(gpgcon) & ~(3 << 22), gpgcon);
	writel(readl(gpgcon) & ~(3 << 6), gpgcon);


	writel(readl(gpgdat) & ~(1 << 2), gpgdat);
	writel(readl(gpgdat) | (1 << 6), gpgdat);
	writel(readl(gpedat) | (1 << 11), gpedat);
	writel(readl(gpedat) | (1 << 13), gpedat);

	lGPF = readl(gpfdat);
	lGPG = readl(gpgdat);
	

	if ((lGPF & (1<<0)) == 0)		return 16;
	else if((lGPF & (1<<2)) == 0)	return 15;
	else if((lGPG & (1<<3)) == 0)	return 14;
	else if((lGPG & (1<<11)) == 0)	return 13;


	writel(readl(gpgdat) | (1 << 2), gpgdat);
	writel(readl(gpgdat) & ~(1 << 6), gpgdat);
	writel(readl(gpedat) | (1 << 11), gpedat);
	writel(readl(gpedat) | (1 << 13), gpedat);
	
	lGPF = readl(gpfdat);
	lGPG = readl(gpgdat);

	if ((lGPF & (1<<0)) == 0)		return 11;
	else if((lGPF & (1<<2)) == 0)	return 8;
	else if((lGPG & (1<<3)) == 0)	return 5;
	else if((lGPG & (1<<11)) == 0)	return 2;


	writel(readl(gpgdat) | (1 << 2), gpgdat);
	writel(readl(gpgdat) | (1 << 6), gpgdat);
	writel(readl(gpedat) & ~(1 << 11), gpedat);
	writel(readl(gpedat) | (1 << 13), gpedat);
	
	lGPF = readl(gpfdat);
	lGPG = readl(gpgdat);
	
	if ((lGPF & (1<<0)) == 0)		return 10;
	else if((lGPF & (1<<2)) == 0)	return 7;
	else if((lGPG & (1<<3)) == 0)	return 4;
	else if((lGPG & (1<<11)) == 0)	return 1;

	writel(readl(gpgdat) | (1 << 2), gpgdat);
	writel(readl(gpgdat) | (1 << 6), gpgdat);
	writel(readl(gpedat) | (1 << 11), gpedat);
	writel(readl(gpedat) & ~(1 << 13), gpedat);
	
	lGPF = readl(gpfdat);
	lGPG = readl(gpgdat);
	
	if ((lGPF & (1<<0)) == 0)		return 12;
	else if((lGPF & (1<<2)) == 0)	return 9;
	else if((lGPG & (1<<3)) == 0)	return 6;
	else if((lGPG & (1<<11)) == 0)	return 3;

	return 0 ;
}


static irqreturn_t button_interrupt(int irq, void *dev_id)
{
        
	__udelay(50000);
	
	dev.key_value = button_scan(irq);
	
	init_gpio();

	wake_up_interruptible(&dev.r_wait);

	return IRQ_HANDLED;
}

static __inline void free_irqs(void)
{
	int i;

	for(i = 0; i < 4; i++)
	{
		free_irq(button_irqs[i].irq, NULL);
	}

	return;
}

static int button_open(struct inode *inode,struct file *filp) 
{
	init_gpio();

	return 0;
}

static int button_release(struct inode *inode,struct file *filp)
{
    return 0;
}

static ssize_t button_read(struct file *filp, char *buf, size_t size, loff_t *ppos)
{
	int ret;

	if(copy_to_user(buf, &dev.key_value, size))
	{
		ret = -EAGAIN;
		return ret;
	}

	ret = size;

	dev.key_value = 0;

	return ret;

}

static ssize_t button_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos)
{
	return 0;
}

static int button_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
	return 0;
}

static unsigned int button_poll(struct file *filp, poll_table *wait)
{
	unsigned int mask = 0;

	poll_wait(filp, &dev.r_wait, wait);

	if(dev.key_value != 0)
		mask |= POLLIN | POLLRDNORM;

	return mask;
}

static struct file_operations button_fops =
{
	.owner = THIS_MODULE,
	.ioctl = button_ioctl,
	.open = button_open,
	.read = button_read,
	.write = button_write,
	.release = button_release,
	.poll = button_poll,
};

struct miscdevice button_misc_dev = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = "button",
	.fops = &button_fops,
};

void ioremap_gpio(struct resource *res)
{
	gpecon = ioremap(res->start, 0x4);
	gpedat = ioremap(res->start + 0x4, 0x4);

	gpfcon = ioremap(res->start + 0x10, 0x4);
	gpfdat = ioremap(res->start + 0x14, 0x4);

	gpgcon = ioremap(res->end - 0x4, 0x4);
	gpgdat = ioremap(res->end, 0x4);


	return ;
}

static int button_probe(struct platform_device *pdev)
{
	int ret;
	int size;
	int i;
	struct resource *res;
	struct resource *mem;

	ret = misc_register(&button_misc_dev);

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	
	size = resource_size(res);


	if(res == NULL)
		printk("failed to get resource\n");

	mem = request_mem_region(res->start, size, pdev->name);

	if(mem == NULL)
		printk("failed to request_mem_region\n");

	ioremap_gpio(res);

	init_waitqueue_head(&dev.r_wait);

	for(i = 0; i < 4; i++)
	{
		button_irqs[i].irq = platform_get_irq(pdev, i);

		ret = request_irq(button_irqs[i].irq, button_interrupt, IRQF_DISABLED, "button", NULL);

		if(ret < 0)
			free_irq(button_irqs[i].irq, NULL);
	}

	return 0;

}

void iounmap_gpio(void)
{
	iounmap(gpecon);
	iounmap(gpedat);
	iounmap(gpfcon);
	iounmap(gpfdat);
	iounmap(gpgcon);
	iounmap(gpgdat);

	return ;
}

static int button_remove(struct platform_device *pdev)
{
	struct resource *res;
	int size;

	free_irqs();
	iounmap_gpio();

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);

	if(res == NULL)
		printk("failed to get resource\n");

	size = resource_size(res);

	release_mem_region(res->start, size);

	misc_deregister(&button_misc_dev);

	return 0;
}

static struct platform_driver button_driver = {
	.probe = button_probe,
	.remove = button_remove,
	.driver = {
		.name = "button",
		.owner = THIS_MODULE,
	}
};

static int __init button_init(void)
{
	return platform_driver_register(&button_driver);
}

static void __exit button_exit(void)
{
	platform_driver_unregister(&button_driver);

	return ;
}
module_init(button_init);
module_exit(button_exit);	

MODULE_AUTHOR("yhr");
MODULE_LICENSE("GPL");


Makefile:

ifeq ($(KERNELRELEASE),)

# set your object kernel dir
KERNELDIR = /home/linux/linux-2.6.35 

PWD := $(shell pwd)

modules:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
	rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module* modules*

.PHONY: modules modules_install clean

else
    obj-m := button_device.o button_driver.o
endif


应用测试程序(button_test.c):

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

int main(void)
{
	int dev_fd;
	char buf;
	fd_set rfds;

	if((dev_fd = open("/dev/button", O_RDONLY | O_NONBLOCK)) < 0)
	{
		perror("failed to open button");

		exit(-1);
	}

	while(1)
	{
		FD_ZERO(&rfds);
		FD_SET(dev_fd, &rfds);

		select(dev_fd + 1, &rfds, NULL, NULL, NULL);

		if(FD_ISSET(dev_fd, &rfds))
		{
			read(dev_fd, &buf, 1);

			printf("key_value : %d\n", buf);
		}
	}

	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值