驱动
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/irq.h>
#include <asm/irq.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/platform_device.h>
#include <linux/cdev.h>
#include <linux/miscdevice.h>
#include <plat/regs-timer.h>
#include <asm/io.h>
#include <mach/regs-irq.h>
#include <asm/mach/time.h>
#include <linux/clk.h>
#include <linux/device.h>
#define DEVICE_NAME "IRQ-Test"
static unsigned long gpio_table [] =
{
S3C2410_GPB5,
S3C2410_GPB6,
S3C2410_GPB7,
S3C2410_GPB8,
};
/* 用来指定GPIO引脚的功能:输出 */
static unsigned int gpio_cfg_table [] =
{
S3C2410_GPB5_OUTP,
S3C2410_GPB6_OUTP,
S3C2410_GPB7_OUTP,
S3C2410_GPB8_OUTP,
};
struct button_irq_desc {
int irq;
int pin;
int pin_setting;
int number;
char *name;
};
#if !defined (CONFIG_SKY2440_IRQ_TEST)
static struct button_irq_desc button_irqs [] = {
{IRQ_EINT1, S3C2410_GPF1, S3C2410_GPF1_EINT1, 0, "KEY1"}, /* K1 */
{IRQ_EINT4, S3C2410_GPF4, S3C2410_GPF4_EINT4, 1, "KEY2"}, /* K2 */
{IRQ_EINT2, S3C2410_GPF2, S3C2410_GPF2_EINT2, 2, "KEY3"}, /* K3 */
{IRQ_EINT0, S3C2410_GPF0, S3C2410_GPF0_EINT0, 3, "KEY4"}, /* K4 */
};
#else
static struct button_irq_desc button_irqs [] = {
{IRQ_EINT9, S3C2410_GPG1, S3C2410_GPG1_EINT9, 0, "KEY1"}, /* K1 */
{IRQ_EINT11, S3C2410_GPG3, S3C2410_GPG3_EINT11, 1, "KEY2"}, /* K2 */
{IRQ_EINT2, S3C2410_GPF2, S3C2410_GPF2_EINT2, 2, "KEY3"}, /* K3 */
{IRQ_EINT0, S3C2410_GPF0, S3C2410_GPF0_EINT0, 3, "KEY4"}, /* K4 */
};
#endif
static volatile char key_values [] = {'0', '0', '0', '0'};
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0,pp=0;
static irqreturn_t irq_interrupt(int irq, void *dev_id)
{
struct button_irq_desc *button_irqs = (struct button_irq_desc *)dev_id;
int down;
down = !s3c2410_gpio_getpin(button_irqs->pin);
if (down != (key_values[button_irqs->number] & 1))
{
key_values[button_irqs->number] = '0' + down;
s3c2410_gpio_setpin(gpio_table[button_irqs->number], 0);
ev_press = 1;
pp=!pp;
wake_up_interruptible(&button_waitq);
}
return IRQ_RETVAL(IRQ_HANDLED);
}
static int tq2440_irq_open(struct inode *inode, struct file *file)
{
int i;
int err = 0;
for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++)
{
if (button_irqs.irq < 0)
continue;
err = request_irq(button_irqs.irq, irq_interrupt, IRQ_TYPE_EDGE_BOTH,
button_irqs.name, (void *)&button_irqs);
if (err)
break;
}
if (err)
{
i--;
for (; i >= 0; i--)
{
if (button_irqs.irq < 0)
continue;
disable_irq(button_irqs.irq);
free_irq(button_irqs.irq, (void *)&button_irqs);
}
return -EBUSY;
}
ev_press = 1;
pp=1;
return 0;
}
static int tq2440_irq_close(struct inode *inode, struct file *file)
{
int i;
for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++)
{
if (button_irqs.irq < 0)
continue;
free_irq(button_irqs.irq, (void *)&button_irqs);
}
return 0;
}
static int tq2440_pwm_ioctl(struct inode *inode, struct file *file, unsigned int cmd)
{
unsigned long tcfg0;
unsigned long tcfg1;
unsigned long tcntb;
unsigned long tcmpb;
unsigned long tcon;
if(cmd <= 0)
{
s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPB0_OUTP);
s3c2410_gpio_setpin(S3C2410_GPB0, 0);
}
else
{
struct clk *clk_p;
unsigned long pclk;
//set GPB0 as tout0, pwm output
s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPB0_TOUT0);
tcon = __raw_readl(S3C2410_TCON);
tcfg1 = __raw_readl(S3C2410_TCFG1);
tcfg0 = __raw_readl(S3C2410_TCFG0);
//prescaler = 50
tcfg0 &= ~S3C2410_TCFG_PRESCALER0_MASK;
tcfg0 |= (50 - 1);
//mux = 1/16
tcfg1 &= ~S3C2410_TCFG1_MUX0_MASK;
tcfg1 |= S3C2410_TCFG1_MUX0_DIV16;
__raw_writel(tcfg1, S3C2410_TCFG1);
__raw_writel(tcfg0, S3C2410_TCFG0);
clk_p = clk_get(NULL, "pclk");
pclk = clk_get_rate(clk_p);
tcntb = (pclk/128)/500;
tcmpb = tcntb>>1;
__raw_writel(tcntb, S3C2410_TCNTB(0));
__raw_writel(tcmpb, S3C2410_TCMPB(0));
tcon &= ~0x1f;
tcon |= 0xb; //start timer
__raw_writel(tcon, S3C2410_TCON);
tcon &= ~2;
__raw_writel(tcon, S3C2410_TCON);
}
return 0;
}
static int tq2440_irq_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
unsigned long err;
int i,down;
if (!ev_press)
{
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
else
wait_event_interruptible(button_waitq, ev_press);
}
ev_press = 0;
err = copy_to_user(buff, (const void *)key_values, min(sizeof(key_values), count));
if(pp)
{
for(i=0;i<4;i++)
{
s3c2410_gpio_setpin(gpio_table, 1);
}
}
return err ? -EFAULT : min(sizeof(key_values), count);
}
static unsigned int tq2440_irq_poll( struct file *file, struct poll_table_struct *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait);
if (ev_press)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static struct file_operations dev_fops = {
.owner = THIS_MODULE,
.open = tq2440_irq_open,
.release = tq2440_irq_close,
.read = tq2440_irq_read,
.poll = tq2440_irq_poll,
.ioctl = tq2440_pwm_ioctl,
};
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &dev_fops,
};
static int __init dev_init(void)
{
int ret;
int i;
for (i = 0; i < 4; i++)
{
s3c2410_gpio_cfgpin(gpio_table, gpio_cfg_table);
s3c2410_gpio_setpin(gpio_table, 1);
}
ret = misc_register(&misc);
printk (DEVICE_NAME" initialized\n");
return ret;
}
static void __exit dev_exit(void)
{
misc_deregister(&misc);
}
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("www.embedsky.net");
MODULE_DESCRIPTION("IRQ Test for EmbedSky SKY2440/TQ2440 Board");