开发板:tiny4412SDK + S702 + 4GB Flash
要移植的内核版本:Linux-4.4.0 (支持device tree)
u-boot版本:友善之臂自带的 U-Boot 2010.12
busybox版本:busybox 1.25
目标:
学习设备树中 Clock 的使用,使能 PWM CLOCK 输出 PWM 脉冲,写简单的字符设备驱动程序,使蜂鸣器发出响声。
原理图:
tiny4412 底板上有一颗有源蜂鸣器,有源蜂鸣器的特点是给它一个高电平它就发出响声,无源蜂鸣器才是真正需要脉冲才能发声的。这里仅仅是学习设备树中 clock 资源的使用,使能 PWM 发出间歇性的响声。
设备树:
pwm_demo@139D0000{
compatible = "tiny4412,pwm_demo";
reg = <0x139D0000 0x14>;
pinctrl-names = "pwm_pin";
//这里可以设置为pinctrl-names = "default";代码中就免去 devm_pinctrl_get ...等等操作了
pinctrl-0 = <&pwm_pin>;
//PWM ADC 等等模块都需要使能时钟模块才能工作,CLK_PWM定义在Exynos4.h (include\dt-bindings\clock)
clock-names = "timers";
};
pwm_pin: pwm_pin{
samsung,pins = "gpd0-0";
samsung,pin-function = <2>;
samsung,pin-pud = <0>;
samsung,pin-drv = <0>;
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
首先,我们需要配置 gpd0_0 引脚为 pwm 输出功能,然后使能 pwm clock,这里多说一句,clock 也是电源管理的一种方式,设备不工作时关闭它的时钟,待其需要工作时再开启。
此外,这里 reg 中指定的是 pwm 相关的寄存器的地址范围,我们在代码中需要设置寄存器,分频系数等等。和平台设备驱动一样,我们可以使用 platform_get_resource(pdev, IORESOURCE_MEM, 0);来获取该类资源。
代码片段:
static int pwm_probe(struct platform_device *pdev) {
dev_t devid;
struct device *dev = &pdev->dev;
struct resource *res = NULL;
struct pinctrl *pctrl;
struct pinctrl_state *pstate;
struct clk *base_clk;
int ret;
printk("enter %s\n",__func__);
pctrl = devm_pinctrl_get(dev);
if(pctrl == NULL)
{
printk("devm_pinctrl_get error\n");
return -EINVAL;
}
pstate = pinctrl_lookup_state(pctrl, "pwm_pin");
if(pstate == NULL)
{
printk("pinctrl_lookup_state error\n");
return -EINVAL;
}
//设置引脚功能
pinctrl_select_state(pctrl, pstate);
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if(res == NULL)
{
printk("platform_get_resource error\n");
return -EINVAL;
}
printk("res: %x\n",(unsigned int)res->start);
base_clk = devm_clk_get(&pdev->dev, "timers");
if (IS_ERR(base_clk)) {
dev_err(dev, "failed to get timer base clk\n");
return PTR_ERR(base_clk);
}
ret = clk_prepare_enable(base_clk);
if (ret < 0) {
dev_err(dev, "failed to enable base clock\n");
return ret;
}
timer = devm_ioremap_resource(&pdev->dev, res);
if(timer == NULL)
{
printk("devm_ioremap_resource error\n");
return -EINVAL;
}
printk("timer: %x\n",(unsigned int)timer);
timer->TCFG0 = (timer->TCFG0 &~(0xff << 0)) | (0xfa << 0);
timer->TCFG1 = (timer->TCFG1 &~(0x0f << 0)) | (0x02 << 0);
timer->TCNTB0 = 100000;
timer->TCMPB0 = 90000;
timer->TCON = (timer->TCON &~(0x0f << 0)) | (0x06 << 0);
printk("%x %x %x %x %x\n",timer->TCFG0,timer->TCFG1,timer->TCNTB0,timer->TCMPB0,timer->TCON);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
完整代码:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/bitops.h>
#include <linux/clk.h>
#include <linux/export.h>
#include <linux/err.h>
#include <linux/io.h>
#include <linux/pwm.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/time.h>
/*
PWM 时钟频率 100M
100M / 250 / 4 = 100000
1/100000 = 10us
*/
#define MAGIC_NUMBER 'k'
#define BEEP_ON _IO(MAGIC_NUMBER ,0)
#define BEEP_OFF _IO(MAGIC_NUMBER ,1)
#define BEEP_FREQ _IO(MAGIC_NUMBER ,2)
static int major;
static struct cdev pwm_cdev;
static struct class *cls;
struct TIMER_BASE{
unsigned int TCFG0;
unsigned int TCFG1;
unsigned int TCON;
unsigned int TCNTB0;
unsigned int TCMPB0;
};
volatile static struct TIMER_BASE * timer = NULL;
#define BEPP_IN_FREQ 100000
static void beep_freq(unsigned long arg)
{
printk("ioctl %d\n",(unsigned int)arg);
timer->TCNTB0 = BEPP_IN_FREQ;
timer->TCMPB0 = BEPP_IN_FREQ / arg;
timer->TCON = (timer->TCON &~(0x0f << 0)) | (0x06 << 0);
timer->TCON = (timer->TCON &~(0xff))| 0x0d;
}
static void beep_on(void)
{
printk("beep on\n");
timer->TCON = (timer->TCON &~(0xff))| 0x0d;
printk("%x\n",timer->TCON);
}
static void beep_off(void)
{
timer->TCON = timer->TCON & ~(0x01);
}
static long pwm_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
{
switch(cmd)
{
case BEEP_ON:
beep_on();
break;
case BEEP_OFF:
beep_off();
break;
case BEEP_FREQ:
beep_freq(arg);
break;
default :
return -EINVAL;
};
return 0;
}
static int pwm_open(struct inode *inode, struct file *file)
{
printk("pwm_open\n");
return 0;
}
static int pwm_release(struct inode *inode, struct file *file)
{
printk("pwm_exit\n");
return 0;
}
static struct file_operations pwm_fops = {
.owner = THIS_MODULE,
.open = pwm_open,
.release = pwm_release,
.unlocked_ioctl = pwm_ioctl,
};
static int pwm_probe(struct platform_device *pdev) {
dev_t devid;
struct device *dev = &pdev->dev;
struct resource *res = NULL;
struct pinctrl *pctrl;
struct pinctrl_state *pstate;
struct clk *base_clk;
int ret;
printk("enter %s\n",__func__);
pctrl = devm_pinctrl_get(dev);
if(pctrl == NULL)
{
printk("devm_pinctrl_get error\n");
return -EINVAL;
}
pstate = pinctrl_lookup_state(pctrl, "pwm_pin");
if(pstate == NULL)
{
printk("pinctrl_lookup_state error\n");
return -EINVAL;
}
//设置引脚功能
pinctrl_select_state(pctrl, pstate);
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if(res == NULL)
{
printk("platform_get_resource error\n");
return -EINVAL;
}
printk("res: %x\n",(unsigned int)res->start);
base_clk = devm_clk_get(&pdev->dev, "timers");
if (IS_ERR(base_clk)) {
dev_err(dev, "failed to get timer base clk\n");
return PTR_ERR(base_clk);
}
ret = clk_prepare_enable(base_clk);
if (ret < 0) {
dev_err(dev, "failed to enable base clock\n");
return ret;
}
timer = devm_ioremap_resource(&pdev->dev, res);
if(timer == NULL)
{
printk("devm_ioremap_resource error\n");
return -EINVAL;
}
printk("timer: %x\n",(unsigned int)timer);
timer->TCFG0 = (timer->TCFG0 &~(0xff << 0)) | (0xfa << 0);
timer->TCFG1 = (timer->TCFG1 &~(0x0f << 0)) | (0x02 << 0);
timer->TCNTB0 = 100000;
timer->TCMPB0 = 90000;
timer->TCON = (timer->TCON &~(0x0f << 0)) | (0x06 << 0);
printk("%x %x %x %x %x\n",timer->TCFG0,timer->TCFG1,timer->TCNTB0,timer->TCMPB0,timer->TCON);
if(alloc_chrdev_region(&devid, 0, 1, "pwm") < 0)
{
printk("%s ERROR\n",__func__);
goto error;
}
major = MAJOR(devid);
cdev_init(&pwm_cdev, &pwm_fops);
cdev_add(&pwm_cdev, devid, 1);
cls = class_create(THIS_MODULE, "mypwm");
device_create(cls, NULL, MKDEV(major, 0), NULL, "pwm0");
error:
unregister_chrdev_region(MKDEV(major, 0), 1);
return 0;
}
static int pwm_remove(struct platform_device *pdev){
printk("enter %s\n",__func__);
device_destroy(cls, MKDEV(major, 0));
class_destroy(cls);
cdev_del(&pwm_cdev);
unregister_chrdev_region(MKDEV(major, 0), 1);
printk("%s enter.\n", __func__);
return 0;
}
static const struct of_device_id pwm_dt_ids[] = {
{ .compatible = "tiny4412,pwm_demo", },
{},
};
MODULE_DEVICE_TABLE(of, pwm_dt_ids);
static struct platform_driver pwm_driver = {
.driver = {
.name = "pwm_demo",
.of_match_table = of_match_ptr(pwm_dt_ids),
},
.probe = pwm_probe,
.remove = pwm_remove,
};
static int pwm_init(void){
int ret;
printk("enter %s\n",__func__);
ret = platform_driver_register(&pwm_driver);
if (ret)
printk(KERN_ERR "pwm demo: probe faipwm: %d\n", ret);
return ret;
}
static void pwm_exit(void)
{
printk("enter %s\n",__func__);
platform_driver_unregister(&pwm_driver);
}
module_init(pwm_init);
module_exit(pwm_exit);
MODULE_LICENSE("GPL");