nuc972 基于GPIO库的外部中断实验+timer_list实验

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <mach/regs-gpio.h>
#include <mach/regs-aic.h>
#include <mach/hardware.h>
#include <linux/device.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>
#include <linux/irq.h>
#include <linux/clk.h>
#include <linux/interrupt.h>
#include <mach/regs-clock.h>
#include <mach/regs-gcr.h>
#include <mach/regs-clock.h>
#include <linux/io.h>
#include <linux/timer.h>

#define DEVICE_NAME    "hello"    

static int MYDRIVER_Major = 0;
static struct class *hello_class;
struct timer_list mytimer;

static int hello_open(struct inode *inode, struct file *file)
{
	printk("%s enter\n", __func__);
	return 0;
}

static int hello_release(struct inode *inode, struct file *file)
{
	printk("%s enter\n", __func__);
	return 0;
}

static int hello_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
	printk("%s enter\n", __func__);
	return 0;
}

static int hello_write(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
	printk("%s enter\n", __func__);
	return 0;
}

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

static struct file_operations hello_fops =
{
	.owner  =   THIS_MODULE,
	.open   =   hello_open, 
	.release =  hello_release,
	.read   =   hello_read,
	.write  =   hello_write,
	.unlocked_ioctl  =   hello_ioctl,
};

irqreturn_t hello_irq(int irq, void *data)
{
	unsigned int val;
	
	printk("%s enter, data=%d\n", __func__, (unsigned int)data);
	
	return IRQ_HANDLED;
}

static show_reg_value(const volatile void __iomem *addr, char *reg_name)
{
	unsigned int val;
	val = __raw_readl(addr);
	printk("%s: 0x%x\n", reg_name, val);
}

void timer_function (unsigned long data)
{
	int i = 0;
	static int cnt = 0;	
	
	for (i = 0; i < 2; i++) {
		gpio_set_value(NUC970_PB4+i, cnt%2);
	}
	cnt++;
	mod_timer(&mytimer, jiffies+HZ);
}

static void timer_test(void)
{
	int i = 0;
	
	setup_timer(&mytimer, timer_function, 0);
	mod_timer(&mytimer, jiffies+HZ);
	
	for (i = 0; i < 2; i++) {
		gpio_request(NUC970_PB4+i, "zwy_gpio_led");
		gpio_direction_output(NUC970_PB4+i,0);
	}
}

static void eint_hw_init(struct device *dev)
{
	unsigned int val = 0, ret = 0;;
	struct pinctrl* p = NULL;
	struct clk *clk = NULL;
	int i = 0;

	/* Enable GPIO clock */
	clk = clk_get(NULL, "gpio");
	if (IS_ERR(clk)) {
		printk(KERN_ERR "nuc970-gpio:failed to get gpio clock source\n");
		return -1;
	}
	clk_prepare(clk);
	clk_enable(clk);

	clk = clk_get(NULL, "gpio_eclk");
	if (IS_ERR(clk)) {
		printk(KERN_ERR "nuc970-gpio:failed to get egpio clock source\n");
		return -1;
	}
	clk_prepare(clk);
	clk_enable(clk);

	/* set gpiof pin mux to int */
	val = __raw_readl(REG_MFP_GPF_H);
	val |= (0xffff<<12);
	__raw_writel(val, REG_MFP_GPF_H);
	show_reg_value(REG_MFP_GPF_H, "REG_MFP_GPF_H");

	/* set gpiof12 as input */
	for (i = 0; i < 4; i++) {
		ret = gpio_request(NUC970_PF11+i, "zwy_gpio_request");
		if (ret < 0) {
			printk("fail to request gpio\n");
		} else {
			ret = gpio_direction_input(NUC970_PF11+i);
			irq_set_irq_type(IRQ_EXT0_F11+i, IRQ_TYPE_EDGE_FALLING);
		}
	}
}

static int hello_probe(struct platform_device *pdev)
{
	int ret = 0;
	int i = 0;
	printk("%s enter\n", __func__);

	MYDRIVER_Major = register_chrdev(0, DEVICE_NAME, &hello_fops);
	if (MYDRIVER_Major < 0)
	{
		printk(DEVICE_NAME " can't register major number\n");
		return MYDRIVER_Major;
	}
	printk("register My Driver OK! Major = %d\n", MYDRIVER_Major);

	//注册一个类,使mdev可以在"/dev/"目录下面建立设备节点
	hello_class = class_create(THIS_MODULE, DEVICE_NAME);
	if(IS_ERR(hello_class))
	{
		printk("Err: failed in My Driver class. \n");
		return -1;
	}
	//创建一个设备节点,节点名为DEVICE_NAME
	device_create(hello_class, NULL, MKDEV(MYDRIVER_Major, 0), NULL, DEVICE_NAME);

	eint_hw_init(&pdev->dev);
	timer_test();

	for (i = 0; i < 4; i++) {
		ret = devm_request_irq(&pdev->dev, IRQ_EXT0_F11+i, hello_irq, IRQF_TRIGGER_FALLING, "hello_ext_irq", i);
		if (ret) {
			printk("request ext1 irq failed, ret=%d\n", ret);
		}
	}
	printk(DEVICE_NAME " initialized\n");

	return 0;
}

static int hello_remove(struct platform_device *pdev)
{
	printk("%s enter\n", __func__);

	del_timer(&mytimer);
	device_destroy(hello_class, MKDEV(MYDRIVER_Major, 0));
	class_destroy(hello_class);
	unregister_chrdev(MYDRIVER_Major, DEVICE_NAME);

	return 0;
}

static struct platform_driver hello_platform_driver = {
	.probe = hello_probe,
	.remove = hello_remove,
	.driver         = {
		.name   = "hello",
		.owner  = THIS_MODULE,
	},
};

module_platform_driver(hello_platform_driver);

MODULE_AUTHOR("zwy");
MODULE_DESCRIPTION("platform driver template");
MODULE_LICENSE("GPL");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值