s3c6410提供了一个8X8的键盘接口,使用的GPIO复用,提供了去抖过滤器。
s3c6410的键盘驱动是基于平台的驱动
一、定义的使用平台的资源
-
//资源定义
-
static struct resource samsung_keypad_resources[] = {
-
[0] = {
-
.start = SAMSUNG_PA_KEYPAD,//资源的开始位置
-
.end = SAMSUNG_PA_KEYPAD + 0x20 - 1,//资源的结束位置
-
.flags = IORESOURCE_MEM,//资源类型
-
},
-
[1] = {
-
.start = IRQ_KEYPAD,
-
.end = IRQ_KEYPAD,
-
.flags = IORESOURCE_IRQ,
-
},
-
};
-
//定义的平台设备
-
struct platform_device samsung_device_keypad = {
-
.name = "s3c-keypad",//设备名
-
.id = -1,//设备ID
-
.num_resources = ARRAY_SIZE(samsung_keypad_resources),//使用资源的大小
-
.resource = samsung_keypad_resources,//资源定义
-
};
二、定义的平台驱动结构
-
static struct platform_driver s3c_keypad_driver = {
-
.probe = s3c_keypad_probe,//探测方法
-
.remove = s3c_keypad_remove,//移除方法
-
.suspend = s3c_keypad_suspend,//挂起方法
-
.resume = s3c_keypad_resume,//恢复方法
-
.driver = {
-
.owner = THIS_MODULE,
-
.name = "s3c-keypad",//驱动名
-
},
-
};
三、驱动的注册和注销
-
//驱动加载函数
-
static int __init s3c_keypad_init(void)
-
{
-
int ret;
-
//注册平台设备驱动
-
ret = platform_driver_register(&s3c_keypad_driver);
-
-
if(!ret)
-
printk(KERN_INFO "S3C Keypad Driver\n");
-
-
return ret;
-
}
-
//驱动的卸载
-
static void __exit s3c_keypad_exit(void)
-
{
-
//注销平台设备驱动
-
platform_driver_unregister(&s3c_keypad_driver);
-
}
四、探测方法
-
//探测方法
-
static int __init s3c_keypad_probe(struct platform_device *pdev)
-
{
-
struct resource *res, *keypad_mem, *keypad_irq;
-
struct input_dev *input_dev;
-
struct s3c_keypad *s3c_keypad;
-
int ret, size;
-
int key, code;
-
//获取使用的IO资源
-
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-
if (res == NULL) {
-
dev_err(&pdev->dev,"no memory resource specified\n");
-
return -ENOENT;
-
}
-
//计算资源大小
-
size = (res->end - res->start) + 1;
-
//申请内存空间
-
keypad_mem = request_mem_region(res->start, size, pdev->name);
-
if (keypad_mem == NULL) {
-
dev_err(&pdev->dev, "failed to get memory region\n");
-
ret = -ENOENT;
-
goto err_req;
-
}
-
//映射IO到内存空间
-
key_base = ioremap(res->start, size);
-
if (key_base == NULL) {
-
printk(KERN_ERR "Failed to remap register block\n");
-
ret = -ENOMEM;
-
goto err_map;
-
}
-
//获取时钟
-
keypad_clock = clk_get(&pdev->dev, "keypad");
-
if (IS_ERR(keypad_clock)) {
-
dev_err(&pdev->dev, "failed to find keypad clock source\n");
-
ret = PTR_ERR(keypad_clock);
-
goto err_clk;
-
}
-
//使能时钟
-
clk_enable(keypad_clock);
-
//分配私有数据空间
-
s3c_keypad = kzalloc(sizeof(struct s3c_keypad), GFP_KERNEL);
-
//分配input_dev输入设备结构
-
input_dev = input_allocate_device();
-
if (!s3c_keypad || !input_dev) {
-
ret = -ENOMEM;
-
goto err_alloc;
-
}
-
//将s3c_keypad作为平台设备的私有数据保存
-
platform_set_drvdata(pdev, s3c_keypad);
-
//将input_dev赋值给s3c_keypad->dev
-
s3c_keypad->dev = input_dev;
-
-
writel(KEYIFCON_INIT, key_base+S3C_KEYIFCON);
-
writel(KEYIFFC_DIV, key_base+S3C_KEYIFFC);
-
-
/* 设置GPIO口键盘模式、禁用上拉电阻*/
-
s3c_setup_keypad_cfg_gpio(KEYPAD_ROWS, KEYPAD_COLUMNS);
-
-
writel(KEYIFCOL_CLEAR, key_base+S3C_KEYIFCOL);
-
-
/*创建并注册输入驱动程序 */
-
__set_bit(EV_KEY, input_dev->evbit);
-
__set_bit(EV_REP, input_dev->evbit);
-
s3c_keypad->nr_rows = KEYPAD_ROWS;
-
s3c_keypad->no_cols = KEYPAD_COLUMNS;
-
s3c_keypad->total_keys = MAX_KEYPAD_NR;
-
-
for(key = 0; key < s3c_keypad->total_keys; key++){
-
code = s3c_keypad->keycodes[key] = keypad_keycode[key];
-
if(code<=0)
-
continue;
-
__set_bit(code & KEY_MAX, input_dev->keybit);
-
}
-
//初始化input_dev结构
-
input_dev->name = DEVICE_NAME;
-
input_dev->phys = "s3c-keypad/input0";
-
input_dev->open = samsung_keypad_open;//打开方法
-
input_dev->close = samsung_keypad_close;//关闭方法
-
input_dev->id.bustype = BUS_HOST;
-
input_dev->id.vendor = 0x0001;
-
input_dev->id.product = 0x0001;
-
input_dev->id.version = 0x0001;
-
//设置键编码
-
input_dev->keycode = keypad_keycode;
-
-
/*扫描定时器初始化*/
-
init_timer(&keypad_timer);
-
keypad_timer.function = keypad_timer_handler;
-
keypad_timer.data = (unsigned long)s3c_keypad;
-
-
/* 获得中断资源 */
-
keypad_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
-
if (keypad_irq == NULL) {
-
dev_err(&pdev->dev, "no irq resource specified\n");
-
ret = -ENOENT;
-
goto err_irq;
-
}
-
//申请资源
-
ret = request_irq(keypad_irq->start, s3c_keypad_isr, IRQF_SAMPLE_RANDOM,
-
DEVICE_NAME, (void *) pdev);
-
if (ret) {
-
printk("request_irq failed (IRQ_KEYPAD) !!!\n");
-
ret = -EIO;
-
goto err_irq;
-
}
-
//注册输入设备
-
ret = input_register_device(input_dev);
-
if (ret) {
-
printk("Unable to register s3c-keypad input device!!!\n");
-
goto out;
-
}
-
-
printk( DEVICE_NAME " Initialized\n");
-
return 0;
-
-
out:
-
free_irq(keypad_irq->start, input_dev);
-
free_irq(keypad_irq->end, input_dev);
-
-
err_irq:
-
input_free_device(input_dev);
-
kfree(s3c_keypad);
-
-
err_alloc:
-
clk_disable(keypad_clock);
-
clk_put(keypad_clock);
-
-
err_clk:
-
iounmap(key_base);
-
-
err_map:
-
release_resource(keypad_mem);
-
kfree(keypad_mem);
-
-
err_req:
-
return ret;
-
}
中断处理方法:当有按键被按下时调用,使用定时器去抖
-
static irqreturn_t s3c_keypad_isr(int irq, void *dev_id)
-
{
-
//printk("[KEY]s3c_keypad_isr.\n");
-
/*禁用键盘按键中断、调度定时器处理程序 */
-
writel(readl(key_base+S3C_KEYIFCON) & ~(INT_F_EN|INT_R_EN), key_base+S3C_KEYIFCON);
-
-
keypad_timer.expires = jiffies + (HZ/100);
-
if ( is_timer_on == FALSE) {
-
add_timer(&keypad_timer);//再次添加定时器。循环处理
-
is_timer_on = TRUE;
-
} else {
-
mod_timer(&keypad_timer,keypad_timer.expires);
-
}
-
/*清除键盘中断状态*/
-
writel(KEYIFSTSCLR_CLEAR, key_base+S3C_KEYIFSTSCLR);
-
-
return IRQ_HANDLED;
-
}
定时器处理方法:当有按键事件产生时,扫描键盘接口寄存器,获得行和列的键值,得到被按下的键,并向输入子
系统核心传递事件
-
static void keypad_timer_handler(unsigned long data)
-
{
-
u32 keymask_low = 0, keymask_high = 0;
-
u32 press_mask_low, press_mask_high;
-
u32 release_mask_low, release_mask_high;
-
int i;
-
struct s3c_keypad *pdata = (struct s3c_keypad *)data;
-
struct input_dev *dev = pdata->dev;
-
//扫描键盘接口寄存器,获得行和列的键值
-
keypad_scan(&keymask_low, &keymask_high);
-
-
DPRINTK("[KEY]keypad_timer_handler.kl=%x,kh=%x\n",keymask_low, keymask_high);
-
//得到被按下的键
-
if (keymask_low != prevmask_low) {
-
press_mask_low =
-
((keymask_low ^ prevmask_low) & keymask_low);
-
release_mask_low =
-
((keymask_low ^ prevmask_low) & prevmask_low);
-
-
i = 0;
-
while (press_mask_low) {
-
if (press_mask_low & 1) {
-
//向输入子系统传递事件
-
input_report_key(dev,pdata->keycodes[i],1);
-
DPRINTK("low Pressed : %d\n",i);
-
}
-
press_mask_low >>= 1;
-
i++;
-
}
-
-
i = 0;
-
while (release_mask_low) {
-
if (release_mask_low & 1) {
-
input_report_key(dev,pdata->keycodes[i],0);
-
DPRINTK("low Released : %d\n",i);
-
}
-
release_mask_low >>= 1;
-
i++;
-
}
-
prevmask_low = keymask_low;
-
}
-
-
if (keymask_high != prevmask_high) {
-
press_mask_high =
-
((keymask_high ^ prevmask_high) & keymask_high);
-
release_mask_high =
-
((keymask_high ^ prevmask_high) & prevmask_high);
-
-
i = 0;
-
while (press_mask_high) {
-
if (press_mask_high & 1) {
-
input_report_key(dev,pdata->keycodes[i+MAX_KEYMASK_NR],1);
-
DPRINTK("high Pressed : %d %d\n",pdata->keycodes[i+MAX_KEYMASK_NR],i);
-
}
-
press_mask_high >>= 1;
-
i++;
-
}
-
-
i = 0;
-
while (release_mask_high) {
-
if (release_mask_high & 1) {
-
input_report_key(dev,pdata->keycodes[i+MAX_KEYMASK_NR],0);
-
DPRINTK("high Released : %d\n",pdata->keycodes[i+MAX_KEYMASK_NR]);
-
}
-
release_mask_high >>= 1;
-
i++;
-
}
-
prevmask_high = keymask_high;
-
}
-
-
if (keymask_low | keymask_high) {
-
mod_timer(&keypad_timer,jiffies + HZ/10);
-
} else {
-
writel(KEYIFCON_INIT, key_base+S3C_KEYIFCON);
-
is_timer_on = FALSE;
-
}
-
}
扫描键盘接口寄存器,获得行和列的键值
-
static int keypad_scan(u32 *keymask_low, u32 *keymask_high)
-
{
-
int i,j = 0;
-
u32 cval,rval;
-
//获得行和列的键值
-
for (i=0; i<KEYPAD_COLUMNS; i++) {
-
cval = readl(key_base+S3C_KEYIFCOL) | KEYCOL_DMASK;
-
cval &= ~(1 << i);
-
writel(cval, key_base+S3C_KEYIFCOL);
-
-
udelay(KEYPAD_DELAY);
-
-
rval = ~(readl(key_base+S3C_KEYIFROW)) & KEYROW_DMASK;
-
-
//printk("[KEY]rval=%x\n",rval);
-
-
//MAX is 64 buttons, so we can use 2 WORD to save the status of keyboards
-
if ((i*KEYPAD_ROWS) < MAX_KEYMASK_NR)
-
*keymask_low |= (rval << (i * KEYPAD_ROWS));
-
else {
-
*keymask_high |= (rval << (j * KEYPAD_ROWS));
-
j = j + 1;
-
}
-
}
-
-
writel(KEYIFCOL_CLEAR, key_base+S3C_KEYIFCOL);
-
-
return 0;
-
}
五、移除方法
-
static int s3c_keypad_remove(struct platform_device *pdev)
-
{
-
struct input_dev *input_dev = platform_get_drvdata(pdev);
-
writel(KEYIFCON_CLEAR, key_base+S3C_KEYIFCON);
-
//关闭时钟
-
if(keypad_clock) {
-
clk_disable(keypad_clock);
-
clk_put(keypad_clock);
-
keypad_clock = NULL;
-
}
-
//注销input驱动
-
input_unregister_device(input_dev);
-
//接触IO映射
-
iounmap(key_base);
-
kfree(pdev->dev.platform_data);
-
//释放中断
-
free_irq(IRQ_KEYPAD, (void *) pdev);
-
-
del_timer(&keypad_timer);
-
printk(DEVICE_NAME " Removed.\n");
-
return 0;
-
}
六、总结
键盘驱动的编写方法
1、定义平台设备结构,指明使用的资源
2、定义平台设备驱动结构
3、注册平台设备
4、在探测方法中分配IO资源并映射到内存,使能时钟,申请中断,分配input_dev结构,初始化input_dev结构
注册input驱动
5、在中断方法中判断抖动
6、在定时器方法中扫描键盘的行列键值,得到按下的键对应的事件,向输入子系统核心提交事件;
7、在移除方法中关闭时钟、释放内存等操作。
阅读(2091) | 评论(0) | 转发(0) |