#define pwm_device_MAJOR 50
#define DEVICE_NAME "pwm_device"
#define FREQ 50
#define PWM_CLOSE 0
#define PWM_OPEN 1
static int pwm_device_close(struct inode *inode,struct file *file);
static int pwm_device_open(struct inode *inode,struct file *file);
static int pwm_device_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg);
static int __init pwm_device_init(void);
static void __exit pwm_device_cleanup(void);
static struct file_operations pwm_device_fops=
{
owner: THIS_MODULE,
open: pwm_device_open,
release: pwm_device_close,
ioctl: pwm_device_ioctl,
};
static void set_pwm_duty(unsigned long duty)
{
unsigned long tcon;
unsigned long tcnt;
unsigned long tcmp;
unsigned long tcfg0;
unsigned long tcfg1;
struct clk *clk_p;
unsigned long pclk;
s3c2410_gpio_cfgpin(S3C2410_GPB0,S3C2410_GPB0_TOUT0);
tcfg0 = __raw_readl(S3C2410_TCFG0);
tcfg1 = __raw_readl(S3C2410_TCFG1);
tcfg0 &= ~S3C2410_TCFG_PRESCALER0_MASK;
tcfg0 |= (256 - 1);
tcfg1 &= ~S3C2410_TCFG1_MUX0_MASK;
tcfg1 |= S3C2410_TCFG1_MUX0_DIV16;
__raw_writel(tcfg0, S3C2410_TCFG0);
__raw_writel(tcfg1, S3C2410_TCFG1);
clk_p = clk_get(NULL, "pclk");
pclk = clk_get_rate(clk_p);
tcnt = (pclk/256/16)/FREQ;
tcmp = (tcnt*duty)/100;
__raw_writel(tcnt, S3C2410_TCNTB(0));
__raw_writel(tcmp, S3C2410_TCMPB(0));
tcon = __raw_readl(S3C2410_TCON);
tcon &= ~0x1f;
tcon |= 0xb;
__raw_writel(tcon, S3C2410_TCON);
tcon &= ~2;
__raw_writel(tcon, S3C2410_TCON);
}
static int __init pwm_device_init(void)
{
int result;
result=register_chrdev(pwm_device_MAJOR,DEVICE_NAME,&pwm_device_fops);
if (result<0)
{
printk(KERN_INFO "[FAILED:Cannot register pwm_device Device!]\n");
return -EIO;
}
else
printk("Initializing pwm_device Device\t----->\t");
printk("[OK]\n");
return 0;
}
static int pwm_device_open(struct inode *inode,struct file *file)
{
printk(KERN_CRIT "DEMO:pwm_device Device Open!\n");
return 0;
}
static int pwm_device_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
{
switch(cmd)
{
case PWM_CLOSE:
break;
case PWM_OPEN:
if (arg == 0)
return -EINVAL;
set_pwm_duty(arg);
break;
default:
break;
}
return 0;
}
static int pwm_device_close(struct inode *inode,struct file *file)
{
printk(KERN_CRIT "DEMO:pwm_device Device close!\n");
return 0;
}
static void __exit pwm_device_cleanup(void)
{
unregister_chrdev(pwm_device_MAJOR,DEVICE_NAME);
printk("DEMO:pwm_device Device Is Cleanup!\n");
}
module_init(pwm_device_init);
modele_exit(pwm_device_cleanup);
MODULE_LICENSE("GPL");