#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<asm/irq.h>
#include<asm/io.h>
#include<linux/interrupt.h>
#include<asm/uaccess.h>
#include<mach/hardware.h>
#include<plat/regs-timer.h>
#include<mach/regs-irq.h>
#include<asm/mach/time.h>
#include<linux/clk.h>
#include<linux/cdev.h>
#include<linux/device.h>
#include<linux/miscdevice.h>
#include<plat/map.h>
#include<plat/map-base.h>
#include<mach/gpio.h>
#include<plat/regs-clock.h>
#include<plat/regs-gpio.h>
#include<plat/gpio-bank-k.h>
#include<linux/timer.h>
#include<linux/irq.h>
#define DEVICE_NAME "iopwm"
#define iopwm_irq IRQ_TIMER1
struct cdev *iopwm_cdev;
unsigned int counter=0;
unsigned int pwm[]={100,75,50,25};
unsigned long tmp;
static irqreturn_t iopwm_interrupt(int irq,void *dev_id){
if(irq != IRQ_TIMER1){
printk("bad irq %d in timer \n",irq);
return -1;
}
//printk("good irq %d in timer \n",irq);
counter++;
if(counter == pwm[0]){
tmp = readl(S3C64XX_GPKDAT);
tmp |= (0x1<<8);//data of gpk8
writel(tmp,S3C64XX_GPKDAT);
}
if(counter == pwm[1]){
tmp = readl(S3C64XX_GPKDAT);
tmp |= (0x4<<8);//data of gpk10
writel(tmp,S3C64XX_GPKDAT);
}
if(counter ==pwm[2]){
tmp = readl(S3C64XX_GPKDAT);
tmp |= (0x1<<12);//data of gpk12
writel(tmp,S3C64XX_GPKDAT);
}
if(counter == pwm[3]){
tmp = readl(S3C64XX_GPKDAT);
tmp |= (0x4<<12);//data of gpk14
writel(tmp,S3C64XX_GPKDAT);
}
if(counter == 60){
tmp = readl(S3C64XX_GPKDAT);
tmp &= ~(0x55<<8);//clear gpk8,10,12,14 low level
writel(tmp,S3C64XX_GPKDAT);
counter = 0;
}
return IRQ_RETVAL(IRQ_HANDLED);
}
void iopwm_timerinit(void){
printk("\nEnter iopwm_timerinit Success\n");
unsigned long tcfg0,tcfg1,tcon,cstat;
struct clk *clk_p;
unsigned long pclk,tcnt;
unsigned long freq=1000;
unsigned temp;
temp = readl(S3C64XX_GPKPUD);
temp = (temp & ~(0x3333U<<16))|(0x2222U<<16); //pull up gpk8,10,12,14
writel(temp,S3C64XX_GPKPUD);
temp =readl(S3C64XX_GPKCON1);
temp = (temp & ~(0x0f0f0f0fU))|(0x01010101U);//con1 of gpk 8 10 12 14
writel(temp,S3C64XX_GPKCON1);
temp = readl(S3C64XX_GPKDAT);
temp |= (0x55<<8);
writel(temp,S3C64XX_GPKDAT);
tcon = __raw_readl(S3C_TCON);
tcfg1 = __raw_readl(S3C_TCFG1);
tcfg0 = __raw_readl(S3C_TCFG0);
tcfg0 &=~S3C_TCFG_PRESCALER0_MASK;
tcfg0 |=(50-1);
tcfg1 &= ~S3C_TCFG1_MUX1_MASK;
tcfg1 |= S3C_TCFG1_MUX1_DIV16;
__raw_writel(tcfg1,S3C_TCFG1);
__raw_writel(tcfg0,S3C_TCFG0);
clk_p = clk_get(NULL,"pclk");
pclk = clk_get_rate(clk_p);
tcnt = (pclk/50/16)/freq;
printk("TCNT is %ld\n",tcnt);
__raw_writel(tcnt,S3C_TCNTB(1));
__raw_writel(tcnt/2,S3C_TCMPB(1));
cstat = __raw_readl(S3C_TINT_CSTAT);
cstat |= (0x2);
__raw_writel(cstat,S3C_TINT_CSTAT);
tcon &= ~(0xf1<<4);
tcon |= (0xb<<8); //disable deadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, start timer 0
__raw_writel(tcon, S3C_TCON);
tcon &= ~(0x2<<8); //clear manual update bit
__raw_writel(tcon, S3C_TCON);
printk("\ninit iopwm Success\n");
}
static int iopwm_open(struct inode *inode,struct file * filp){
printk("iopwm_open Success\n");
return 0;
}
static int iopwm_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned int arg){
printk("\nEnter iopwm_ioctl cmd=%d\t arg=%d\n",cmd,arg);
switch(cmd){
case 0:
pwm[0] = arg;break;
case 1:
pwm[1] = arg;break;
case 2:
pwm[2] = arg;break;
case 4:
pwm[3] = arg;break;
default:
return -EINVAL;
}
//iopwm_timerinit();
return 0;
}
static int iopwm_close(struct inode *inode,struct file *filp){
printk("iopwm_close Success\n");
return 0;
}
static struct file_operations iopwm_fops={
.owner = THIS_MODULE,
.open = iopwm_open,
.ioctl = iopwm_ioctl,
.release = iopwm_close,
};
static struct miscdevice misc={
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &iopwm_fops,
};
static int __init iopwm_init(void){
int ret;
ret = misc_register(&misc);
printk(DEVICE_NAME"\tinitialized\n");
iopwm_timerinit();
ret = request_irq(iopwm_irq,iopwm_interrupt,IRQ_TYPE_LEVEL_HIGH,DEVICE_NAME,NULL);
if(ret < 0 ){printk("\nRegister IOPWMfailed!\n"); return ret;}
printk("\nRegister IOPWM Success\n");
return ret;
}
static void __exit iopwm_exit(void){
free_irq(iopwm_irq,NULL);
misc_deregister(&misc);
}
module_init(iopwm_init);
module_exit(iopwm_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("JCracker");
- 1
- 2
前往页