#include
#include
#include
#include
static struct resource beep_resource[] =
{
[0] ={
.start = 0x7F0080A0,
.end = 0x7F0080A0 + 0x4,
.flags = IORESOURCE_MEM,
},
[1] ={
.start = 0x7F006000,
.end = 0x7F006000 + 0x4,
.flags = IORESOURCE_MEM,
}
};
static void hello_release(struct device *dev)
{
printk("hello_release\n");
return ;
}
static struct platform_device hello_device=
{
.name = "bigbang",
.id = -1,
.dev.release = hello_release,
.num_resources = ARRAY_SIZE(beep_resource),
.resource = beep_resource,
};
static int hello_init(void)
{
printk("hello_init");
return platform_device_register(&hello_device);
}
static void hello_exit(void)
{
printk("hello_exit");
platform_device_unregister(&hello_device);
return;
}
MODULE_LICENSE("GPL");
module_init(hello_init);
module_exit(hello_exit);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME "beep_pwm"
#define PWM_IOCTL_SET_FREQ 1
#define PWM_IOCTL_STOP 0
static struct semaphore lock; //信号量
static int major = 250;
static int minor=0;
static dev_t devno;
static struct class *cls;
static struct device *test_device;
static void *Define_S3C64XX_GPFCON;
static void *Define_S3C_TCFG0;
static void *Define_S3C_TCFG1;
static void *Define_S3C_TCON;
static void *Define_S3C_TCNTB0;
static void *Define_S3C_TCMPB0;
/* freq: pclk/50/16/65536 ~ pclk/50/16
* if pclk = 50MHz, freq is 1Hz to 62500Hz
* human ear : 20Hz~ 20000Hz
*/
static void PWM_Set_Freq( unsigned long freq )
{
unsigned long tcon;
unsigned long tcnt;
unsigned long tcfg1;
unsigned long tcfg0;
struct clk *clk_p;
unsigned long pclk;
unsigned tmp;
tmp = readl(Define_S3C64XX_GPFCON);
tmp &=~(0x3U << 30); //GPF15 [31:30] 10 = PWM TOUT[1]
tmp |= (0x2U << 30);
writel(tmp, Define_S3C64XX_GPFCON);// GPF15 设置为PWM TOUT 1
tcon = __raw_readl(Define_S3C_TCON); //定时器控制寄存器
tcfg1 = __raw_readl(Define_S3C_TCFG1); //时钟多路复用器和 DMA模式的选择
tcfg0 = __raw_readl(Define_S3C_TCFG0); //时钟预定标器和死区结构
//prescaler = 50 Prescaler 0 [7:0] R/W Prescaler 0 value for timer 0 & 1
tcfg0 &= ~S3C_TCFG_PRESCALER0_MASK; //{prescaler value} = 1~255
tcfg0 |= (50 - 1); //prescaler value=50
//mux = 1/16 TCFG1 Divider MUX0 [3:0] R/W Select Mux input for PWM Timer 0
tcfg1 &= ~S3C_TCFG1_MUX0_MASK;
tcfg1 |= S3C_TCFG1_MUX0_DIV16; // 0100: 1/16
__raw_writel(tcfg1, Define_S3C_TCFG1);
__raw_writel(tcfg0, Define_S3C_TCFG0);
/* clk_get获取一个名为id的时针
* 输入参数dev: 可以为NULL
* 输入参数id: 时针名称,如fclk、hclk、pclk等
* 返回值: 返回该时钟的clk结构体
*
*再将clk_get返回的clk结构体传递给clk_get_rate,获取该时钟的频率
*/
/*PCLK is used for APB bus, which is used by the peripherals
*such as WDT, IIS, I2C, PWM timer, MMC */
clk_p = clk_get(NULL, "pclk"); //获取一个名为id的时针
pclk = clk_get_rate(clk_p); //获取该时钟的频率
//定时器输入时钟频率
//Timer input clock Frequency = PCLK / ( {prescaler value + 1} ) / {divider value}
tcnt = (pclk/50/16)/freq;
__raw_writel(tcnt,/*S3C_TCNTB(0)*/Define_S3C_TCNTB0); //TCNTB0:定时器0计数缓冲器。
__raw_writel(tcnt/2,/*S3C_TCMPB(0)*/Define_S3C_TCMPB0);//TCMPB0:定时器0比较缓冲寄存器。
tcon &= ~0x1f;
tcon |= 0xb; //disable deadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, start timer 0
__raw_writel(tcon, Define_S3C_TCON);
/*Note: Manual update bit must be 1’b0 before Start/Stop bit is 1’b1.
If Manual update bit is 1’b1 and Start/Stop bit is 1’b1,
timer counter is not update by new value.
Timer counter value is last value. */
tcon &= ~2; //clear manual update bit
__raw_writel(tcon, Define_S3C_TCON);
}
void PWM_Stop( void )
{
unsigned tmp;
tmp = readl(Define_S3C64XX_GPFCON);
//tmp &= ~(0x3U << 28);
tmp &= ~(0x3U << 30);
writel(tmp, Define_S3C64XX_GPFCON);
}
/* 该函数尝试获得信号量sem,如果能够立刻获得,
它就获得该信号量并返回0,否则,返回非0值。
它不会导致调用者睡眠,可以在中断上下文使用。
*/
static int s3c64xx_pwm_open(struct inode *inode, struct file *file)
{
if (!down_trylock(&lock))
{
return 0;
}
else
{
return -EBUSY;
}
}
static int s3c64xx_pwm_close(struct inode *inode, struct file *file)
{
up(&lock); //该函数释放信号量sem,唤醒等待者
return 0;
}
static long s3c64xx_pwm_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
{
switch (cmd)
{
case PWM_IOCTL_SET_FREQ:
if (arg == 0)
{
return -EINVAL;
}
PWM_Set_Freq(arg);
break;
case PWM_IOCTL_STOP:
default:
PWM_Stop();
break;
}
return 0;
}
static struct file_operations dev_fops =
{
.owner = THIS_MODULE,
.open = s3c64xx_pwm_open,
.release = s3c64xx_pwm_close,
.unlocked_ioctl = s3c64xx_pwm_ioctl,
};
static void beep_unmap(void)
{
iounmap(Define_S3C64XX_GPFCON);
iounmap(Define_S3C_TCFG0);
}
static int beep_probe(struct platform_device *pdev)
{
int ret;
printk("match ok!");
Define_S3C64XX_GPFCON = ioremap(pdev->resource[0].start,pdev->resource[0].end - pdev->resource[0].start);
Define_S3C_TCFG0 = ioremap(pdev->resource[1].start, pdev->resource[1].end - pdev->resource[1].start);
Define_S3C_TCFG1=Define_S3C_TCFG0+4;
Define_S3C_TCON=Define_S3C_TCFG1+4;
Define_S3C_TCNTB0=Define_S3C_TCON+4;
Define_S3C_TCMPB0=Define_S3C_TCNTB0+4;
devno = MKDEV(major,minor);
ret = register_chrdev(major,"beep_pwm",&dev_fops);
cls = class_create(THIS_MODULE, "myclass");
if(IS_ERR(cls))
{
unregister_chrdev(major,"beep_pwm");
return -EBUSY;
}
test_device = device_create(cls,NULL,devno,NULL,"beep_pwm");//mknod /dev/hello
if(IS_ERR(test_device))
{
class_destroy(cls);
unregister_chrdev(major,"beep_pwm");
return -EBUSY;
}
PWM_Set_Freq(1000);
return 0;
}
static int beep_remove(struct platform_device *pdev)
{
beep_unmap();
device_destroy(cls,devno);
class_destroy(cls);
unregister_chrdev(major,"beep_pwm");
return 0;
}
static struct platform_driver beep_driver=
{
.driver.name = DEVICE_NAME,
.probe = beep_probe,
.remove = beep_remove,
};
static int __init dev_init(void)
{
int ret;
/* 该函数用于初始化一个互斥锁,即它把信号量sem的值设置为1,
等同于sema_init (struct semaphore *sem, 1)*/
init_MUTEX(&lock);
ret = platform_driver_register(&beep_driver);
printk (DEVICE_NAME"\tinitialized\n");
return ret;
}
static void __exit dev_exit(void)
{
platform_driver_unregister(&beep_driver);
}
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FORLINX Inc.");
MODULE_DESCRIPTION("S3C6410 PWM Driver");
#include
#include
#include
#include
#include
#define PWM_IOCTL_SET_FREQ 1
#define PWM_IOCTL_STOP 0
main()
{
int fd;
fd = open("/dev/pwm",O_RDWR);
if(fd<0)
{
perror("open fail \n");
return ;
}
ioctl(fd,PWM_IOCTL_SET_FREQ,100);
sleep(10);
ioctl(fd,PWM_IOCTL_STOP);
close(fd);
}