蜂鸣器驱动基于PWM0(timer0),使用GPB0这个IO口控制,通用的驱动文件为:drivers/input/misc/pwm-beeper.c,它属于input设备。
发声原理:PWM输出的占空比保持不变(50%),改变PWM频率即可改变发声频率
在mach-xc2440.c中添加蜂鸣器驱动的支持:
在xc2440_devices[ ]结构体中加入:
&xc2440_beeper_device,
构建蜂鸣器设备的platform_device结构体:
- /* Buzzer */
-
static struct platform_device xc2440_beeper_device = {
-
.name = "pwm-beeper",
-
.dev = {
-
.parent = &s3c_device_timer[0].dev,
-
.platform_data = 0,
-
},
-
.id = 0,
-
};
修改drivers/input/misc/pwm-beeper.c驱动文件,在pwm_beeper_event函数中加入控制PWM频率的操作:
- static void xc2440_beeper_enable(void)
-
{
-
gpio_request(S3C2410_GPB(0), "beeper");
-
s3c_gpio_setpull(S3C2410_GPB(0), S3C_GPIO_PULL_NONE);
-
s3c_gpio_cfgpin(S3C2410_GPB(0), S3C2410_GPB0_TOUT0);
-
}
-
-
static void xc2440_beeper_disable(void)
-
{
-
s3c_gpio_cfgpin(S3C2410_GPB(0), S3C2410_GPIO_INPUT);
-
}
-
-
static int pwm_beeper_event(struct input_dev *input,
-
unsigned int type, unsigned int code, int value)
-
{
-
int ret = 0;
-
struct pwm_beeper *beeper = input_get_drvdata(input);
-
unsigned long period;
-
-
if (type != EV_SND || value < 0)
-
return -EINVAL;
-
-
switch (code) {
-
case SND_BELL:
-
value = value ? 1000 : 0;
-
break;
-
case SND_TONE:
-
break;
-
default:
-
return -EINVAL;
-
}
-
-
if (value == 0) {
-
//pwm_config(beeper->pwm, 0, 0);
-
pwm_disable(beeper->pwm);
-
xc2440_beeper_disable();
-
} else {
-
xc2440_beeper_enable();
-
period = HZ_TO_NANOSECONDS(value);
-
ret = pwm_config(beeper->pwm, period / 2, period);
-
if (ret)
-
return ret;
-
ret = pwm_enable(beeper->pwm);
-
if (ret)
-
return ret;
-
beeper->period = period;
-
}
-
-
return 0;
-
}
查看drivers/input/misc目录下的Kconfig文件中定义了pwm beeper的配置:
- config INPUT_PWM_BEEPER
- tristate "PWM beeper support"
- depends on HAVE_PWM
配置内核,支持按键PWM驱动:
- Device Drivers --->
- Input device support --->
-
[*]Miscellaneous devices --->
- <*>PWM beeper support
内核启动输出:
input: pwm-beeper as
/devices/platform/s3c24xx-pwm.0/pwm-beeper.0/input/input2
查看设备:
/dev/event2
应用程序使用input_event来操作
阅读(147) | 评论(0) | 转发(0) |