linux keypad driver
来源:互联网 发布:学弹钢琴软件 编辑:程序博客网 时间:2024/06/06 02:13
Linux kernel & device driver programming
Cross-Referenced Linux and Device Driver Code
[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
1 /*
2 * GPIO driven matrix keyboard driver
3 *
4 * Copyright (c) 2008 Marek Vasut <marek.vasut@gmail.com>
5 *
6 * Based on corgikbd.c
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13
14 #include <linux/types.h>
15 #include <linux/delay.h>
16 #include <linux/platform_device.h>
17 #include <linux/init.h>
18 #include <linux/input.h>
19 #include <linux/irq.h>
20 #include <linux/interrupt.h>
21 #include <linux/jiffies.h>
22 #include <linux/module.h>
23 #include <linux/gpio.h>
24 #include <linux/input/matrix_keypad.h>
25
26 struct matrix_keypad {
27 const struct matrix_keypad_platform_data *pdata;
28 struct input_dev *input_dev;
29 unsigned short *keycodes;
30 unsigned int row_shift;
31
32 uint32_t last_key_state[MATRIX_MAX_COLS];
33 struct delayed_work work;
34 bool scan_pending;
35 bool stopped;
36 spinlock_t lock;
37 };
38
39 /*
40 * NOTE: normally the GPIO has to be put into HiZ when de-activated to cause
41 * minmal side effect when scanning other columns, here it is configured to
42 * be input, and it should work on most platforms.
43 */
44 static void __activate_col(const struct matrix_keypad_platform_data *pdata,
45 int col, bool on)
46 {
47 bool level_on = !pdata->active_low;
48
49 if (on) {
50 gpio_direction_output(pdata->col_gpios[col], level_on);
51 } else {
52 gpio_set_value_cansleep(pdata->col_gpios[col], !level_on);
53 gpio_direction_input(pdata->col_gpios[col]);
54 }
55 }
56
57 static void activate_col(const struct matrix_keypad_platform_data *pdata,
58 int col, bool on)
59 {
60 __activate_col(pdata, col, on);
61
62 if (on && pdata->col_scan_delay_us)
63 udelay(pdata->col_scan_delay_us);
64 }
65
66 static void activate_all_cols(const struct matrix_keypad_platform_data *pdata,
67 bool on)
68 {
69 int col;
70
71 for (col = 0; col < pdata->num_col_gpios; col++)
72 __activate_col(pdata, col, on);
73 }
74
75 static bool row_asserted(const struct matrix_keypad_platform_data *pdata,
76 int row)
77 {
78 return gpio_get_value_cansleep(pdata->row_gpios[row]) ?
79 !pdata->active_low : pdata->active_low;
80 }
81
82 static void enable_row_irqs(struct matrix_keypad *keypad)
83 {
84 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
85 int i;
86
87 for (i = 0; i < pdata->num_row_gpios; i++)
//使能中断。
88 enable_irq(gpio_to_irq(pdata->row_gpios[i]));
89 }
90
91 static void disable_row_irqs(struct matrix_keypad *keypad)
92 {
93 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
94 int i;
95
96 for (i = 0; i < pdata->num_row_gpios; i++)
这个是关中断,记得以前使用disable_irq()时有时候会出错。
所以使用disable_irq_nosync()会更好友好一些。
97 disable_irq_nosync(gpio_to_irq(pdata->row_gpios[i]));
98 }
99
使用input子系统的一般流程为:
input_allocate_device()申请一个input_dev设备――>
初始化该input_dev――>
input_register_device()向子系统注册该设备――>
中断时input_event()向子系 统报告事件。
100 /*
101 * This gets the keys from keyboard and reports it to input subsystem
102 */
这就是延后的工作的函数体,依次进行列行扫描来判断哪个按键被按下,然后使用report_key()向
input core提交按键事件。
103 static void matrix_keypad_scan(struct work_struct *work)
104 {
105 struct matrix_keypad *keypad =
106 container_of(work, struct matrix_keypad, work.work);
107 struct input_dev *input_dev = keypad->input_dev;
108 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
109 uint32_t new_state[MATRIX_MAX_COLS];
110 int row, col, code;
111
112 /* de-activate all columns for scanning */
113 activate_all_cols(pdata, false);
114
115 memset(new_state, 0, sizeof(new_state));
116
117 /* assert each column and read the row status out */
118 for (col = 0; col < pdata->num_col_gpios; col++) {
119
120 activate_col(pdata, col, true);
121
122 for (row = 0; row < pdata->num_row_gpios; row++)
123 new_state[col] |=
124 row_asserted(pdata, row) ? (1 << row) : 0;
125
126 activate_col(pdata, col, false);
127 }
128
129 for (col = 0; col < pdata->num_col_gpios; col++) {
130 uint32_t bits_changed;
131
132 bits_changed = keypad->last_key_state[col] ^ new_state[col];
133 if (bits_changed == 0)
134 continue;
135
136 for (row = 0; row < pdata->num_row_gpios; row++) {
137 if ((bits_changed & (1 << row)) == 0)
138 continue;
139
140 code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
//提交输入事件
141 input_event(input_dev, EV_MSC, MSC_SCAN, code);
//提交按键值。
142 input_report_key(input_dev,
143 keypad->keycodes[code],
144 new_state[col] & (1 << row));
145 }
146 }
这个函数难道是flush input core让它立即向上层提交。
147 input_sync(input_dev);
148
149 memcpy(keypad->last_key_state, new_state, sizeof(new_state));
150
151 activate_all_cols(pdata, true);
152
153 /* Enable IRQs again */
154 spin_lock_irq(&keypad->lock);
155 keypad->scan_pending = false;
156 enable_row_irqs(keypad);
157 spin_unlock_irq(&keypad->lock);
158 }
159
中断函数。
160 static irqreturn_t matrix_keypad_interrupt(int irq, void *id)
161 {
指针强制转换,送入的irq为当前发生中断的中断号
id为在使用request_irq()注册时输入的参数,可以在中断上下文中使用。
162 struct matrix_keypad *keypad = id;
163 unsigned long flags;
164
165 spin_lock_irqsave(&keypad->lock, flags);
166
167 /*
168 * See if another IRQ beaten us to it and scheduled the
169 * scan already. In that case we should not try to
170 * disable IRQs again.
171 */
172 if (unlikely(keypad->scan_pending || keypad->stopped))
173 goto out;
174
175 disable_row_irqs(keypad);
176 keypad->scan_pending = true;
//中断中并不直接读取gpio,直接调度一个延时的任务,
//相当于中断上半部分和下半部分,
//上半部分:在中断处理函数中进行一些操作,然后调度一个延后的工作队列。
//下半部分:在工作队列中完成真正的操作。
//这样的好处是,尽可能少的占用中断的时间。
177 schedule_delayed_work(&keypad->work,
178 msecs_to_jiffies(keypad->pdata->debounce_ms));
179
180 out:
181 spin_unlock_irqrestore(&keypad->lock, flags);
182 return IRQ_HANDLED;
183 }
184
185 static int matrix_keypad_start(struct input_dev *dev)
186 {
//获取保存在输出设备中的指针。
187 struct matrix_keypad *keypad = input_get_drvdata(dev);
188
189 keypad->stopped = false;
190 mb();
191
192 /*
193 * Schedule an immediate key scan to capture current key state;
194 * columns will be activated and IRQs be enabled after the scan.
195 */
//调用一个延时的任务用于扫描矩阵键盘。
196 schedule_delayed_work(&keypad->work, 0);
197
198 return 0;
199 }
200
201 static void matrix_keypad_stop(struct input_dev *dev)
202 {
203 struct matrix_keypad *keypad = input_get_drvdata(dev);
204
205 keypad->stopped = true;
206 mb();
207 flush_work(&keypad->work.work);
208 /*
209 * matrix_keypad_scan() will leave IRQs enabled;
210 * we should disable them now.
211 */
212 disable_row_irqs(keypad);
213 }
214
这个应该是由内核输出的宏,在内核中启动电源管理就会定义这个宏PowerManagement
215 #ifdef CONFIG_PM
216 static int matrix_keypad_suspend(struct platform_device *pdev, pm_message_t state)
217 {
218 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
219 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
220 int i;
221
222 matrix_keypad_stop(keypad->input_dev);
223
224 if (device_may_wakeup(&pdev->dev))
225 for (i = 0; i < pdata->num_row_gpios; i++)
226 enable_irq_wake(gpio_to_irq(pdata->row_gpios[i]));
227
228 return 0;
229 }
230
231 static int matrix_keypad_resume(struct platform_device *pdev)
232 {
233 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
234 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
235 int i;
236
237 if (device_may_wakeup(&pdev->dev))
238 for (i = 0; i < pdata->num_row_gpios; i++)
239 disable_irq_wake(gpio_to_irq(pdata->row_gpios[i]));
240
241 matrix_keypad_start(keypad->input_dev);
242
243 return 0;
244 }
245 #else
如果没有电源功能,则这2个函数为空。
246 #define matrix_keypad_suspend NULL
247 #define matrix_keypad_resume NULL
248 #endif
249
初始化连接的GPIO引脚寄存器,包括:方向:输入还是输出,向内核申请GPIO,表示
这段区域我正在使用,别人不能用了。防止多人使用出现冲突错误。
同时完成申请中断的操作。
250 static int __devinit init_matrix_gpio(struct platform_device *pdev,
251 struct matrix_keypad *keypad)
252 {
253 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
254 int i, err = -EINVAL;
255
256 /* initialized strobe lines as outputs, activated */
257 for (i = 0; i < pdata->num_col_gpios; i++) {
258 err = gpio_request(pdata->col_gpios[i], "matrix_kbd_col");
259 if (err) {
260 dev_err(&pdev->dev,
261 "failed to request GPIO%d for COL%d\n",
262 pdata->col_gpios[i], i);
263 goto err_free_cols;
264 }
265
266 gpio_direction_output(pdata->col_gpios[i], !pdata->active_low);
267 }
268
269 for (i = 0; i < pdata->num_row_gpios; i++) {
270 err = gpio_request(pdata->row_gpios[i], "matrix_kbd_row");
271 if (err) {
272 dev_err(&pdev->dev,
273 "failed to request GPIO%d for ROW%d\n",
274 pdata->row_gpios[i], i);
275 goto err_free_rows;
276 }
277
278 gpio_direction_input(pdata->row_gpios[i]);
279 }
280
281 for (i = 0; i < pdata->num_row_gpios; i++) {
282 err = request_irq(gpio_to_irq(pdata->row_gpios[i]),
283 matrix_keypad_interrupt,
284 IRQF_DISABLED |
285 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
286 "matrix-keypad", keypad);
287 if (err) {
288 dev_err(&pdev->dev,
289 "Unable to acquire interrupt for GPIO line %i\n",
290 pdata->row_gpios[i]);
291 goto err_free_irqs;
292 }
293 }
294
295 /* initialized as disabled - enabled by input->open */
296 disable_row_irqs(keypad);
297 return 0;
298
299 err_free_irqs:
300 while (--i >= 0)
301 free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
302 i = pdata->num_row_gpios;
303 err_free_rows:
304 while (--i >= 0)
305 gpio_free(pdata->row_gpios[i]);
306 i = pdata->num_col_gpios;
307 err_free_cols:
308 while (--i >= 0)
309 gpio_free(pdata->col_gpios[i]);
310
311 return err;
312 }
313
这个函数应该是platform_device跟platform_driver匹配时被调用。
314 static int __devinit matrix_keypad_probe(struct platform_device *pdev)
315 {
316 const struct matrix_keypad_platform_data *pdata;
317 const struct matrix_keymap_data *keymap_data;
318 struct matrix_keypad *keypad;
319 struct input_dev *input_dev;//内核输入设备结构体
320 unsigned short *keycodes;
321 unsigned int row_shift;
322 int i;
323 int err;
324
获取平台设备的数据,这个应该是由platform_device提供,
由platform_driver使用。
325 pdata = pdev->dev.platform_data;
326 if (!pdata) {
327 dev_err(&pdev->dev, "no platform data defined\n");
328 return -EINVAL;
329 }
330
331 keymap_data = pdata->keymap_data;
332 if (!keymap_data) {
333 dev_err(&pdev->dev, "no keymap data defined\n");
334 return -EINVAL;
335 }
336
337 row_shift = get_count_order(pdata->num_col_gpios);
338
申请keypad设备结构体,为了防止指针丢失,
一会儿会将其保存在platform_device中的
私有指针域中。
339 keypad = kzalloc(sizeof(struct matrix_keypad), GFP_KERNEL);
这个指针会保存的keypad结构体中,作为二级指针。(见下面).
340 keycodes = kzalloc((pdata->num_row_gpios << row_shift) *
341 sizeof(*keycodes),
342 GFP_KERNEL);
内核的输入子系统接口,内部应该就是kmalloc一个结构体。
343 input_dev = input_allocate_device();
344 if (!keypad || !keycodes || !input_dev) {
345 err = -ENOMEM;
346 goto err_free_mem;
347 }
348 上面是申请内存,下面将这些值保存在设备结构体中.
349 keypad->input_dev = input_dev;内核输入设备结构体(保存这个指针不能丢失)
350 keypad->pdata = pdata;
351 keypad->keycodes = keycodes;
352 keypad->row_shift = row_shift;
353 keypad->stopped = true;
初始化延后的工作队列,当该工作被调度时将调用 matrix_keypad_scan函数。
354 INIT_DELAYED_WORK(&keypad->work, matrix_keypad_scan);延时的任务
355 spin_lock_init(&keypad->lock);自旋锁不可睡眠,难道在中断中用??
356
这里还是比较关键的,初始化输入设备结构。
Linux中输入设备的事件类型有(这里只列出了常用的一些,更多请看linux/input.h中):
EV_SYN 0x00 同步事件
EV_KEY 0x01 按键事件
EV_REL 0x02 相对坐标
EV_ABS 0x03 绝对坐标
EV_MSC 0x04 其它
EV_LED 0x11 LED
EV_SND 0x12 声音
EV_REP 0x14 Repeat
EV_FF 0x15 力反馈
~~~~~~~~~~~~~~~~~~~~~~~~
EV_PWR 电源
EV_FF_STATUS 状态
357 input_dev->name = pdev->name;
358 input_dev->id.bustype = BUS_HOST;
359 input_dev->dev.parent = &pdev->dev;
事件类型:按键和重复
360 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
361 input_dev->open = matrix_keypad_start;
362 input_dev->close = matrix_keypad_stop;
363
364 input_dev->keycode = keycodes;
365 input_dev->keycodesize = sizeof(*keycodes);
366 input_dev->keycodemax = pdata->num_row_gpios << keypad->row_shift;
367
368 for (i = 0; i < keymap_data->keymap_size; i++) {
369 unsigned int key = keymap_data->keymap[i];
370 unsigned int row = KEY_ROW(key);
371 unsigned int col = KEY_COL(key);
372 unsigned short code = KEY_VAL(key);
373
374 keycodes[MATRIX_SCAN_CODE(row, col, row_shift)] = code;
375 __set_bit(code, input_dev->keybit);
376 }
377 __clear_bit(KEY_RESERVED, input_dev->keybit);
378
379 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
380 input_set_drvdata(input_dev, keypad);
381
382 err = init_matrix_gpio(pdev, keypad);
383 if (err)
384 goto err_free_mem;
385
这里是标准的注册输入设备函数,属于内核API。
386 err = input_register_device(keypad->input_dev);
387 if (err)
388 goto err_free_mem;
389
//电源管理PM的功能,设置设备能否可唤醒以及是否唤醒
390 device_init_wakeup(&pdev->dev, pdata->wakeup);
//这里将临时申请的内存keypad保存到platform_device平台设备结构体中,防止丢失。
//往前看可以看到,keypad是使用kmalloc()申请的。如果我们不找个地方保存起来的话,
//就会丢失,造成野指针。
391 platform_set_drvdata(pdev, keypad);
392
393 return 0;
394
395 err_free_mem:
396 input_free_device(input_dev);
397 kfree(keycodes);
398 kfree(keypad);
399 return err;
400 }
401
402 static int __devexit matrix_keypad_remove(struct platform_device *pdev)
403 {
//在remove()函数中,我们从平台设备结构体中提取我们保存的指针。
//来获取我们的信息,并做释放操作。
//并手工释放内存。
404 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
//从保存的设备结构体中提取下一级提针。
405 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
406 int i;
407
408 device_init_wakeup(&pdev->dev, 0);
409
410 for (i = 0; i < pdata->num_row_gpios; i++) {
//释放中断
411 free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
//释放GPIO.
412 gpio_free(pdata->row_gpios[i]);
413 }
414
415 for (i = 0; i < pdata->num_col_gpios; i++)
416 gpio_free(pdata->col_gpios[i]);
417
注销输入设备。
418 input_unregister_device(keypad->input_dev);
//为了防止误操作,将平台设备的数据域设置为0。
419 platform_set_drvdata(pdev, NULL);
420 kfree(keypad->keycodes);
//platform_set_drvdata()和platform_get_drvdata()只是负责保存指针。
//到最后还是需要手工释放内存的。
421 kfree(keypad);
422
423 return 0;
424 }
425
426 static struct platform_driver matrix_keypad_driver = {
427 .probe = matrix_keypad_probe,
428 .remove = __devexit_p(matrix_keypad_remove),
429 .suspend = matrix_keypad_suspend,
430 .resume = matrix_keypad_resume,这是PM电源管理的功能。
休眠时内核会依次调用驱动链表上所有驱动的suspend()
函数,在这里应该将硬件设置为休眠低功耗状态。
当内核恢复时,也会依次调用所有驱动的resume()函数,
这里就必须将硬件设置为正常工作模式,退出休眠。
431 .driver = {
432 .name = "matrix-keypad",这里的name很重要,因为只有
平台驱动跟平台设备的name匹配时才会调用驱动的probe()。
433 .owner = THIS_MODULE,
434 },
435 };
436
437 static int __init matrix_keypad_init(void)
438 {
注册平台设备,这类GPIO矩阵键盘归为平台设备,属于SoC吗?
如果这样的话,那么是否还得注册一个platform_device,
因为在调试中发现只有platform_driver跟platform_device匹配时,才会
调用platform_driver中的probe()函数,才有完成初始化的工作。
439 return platform_driver_register(&matrix_keypad_driver);
440 }
441
442 static void __exit matrix_keypad_exit(void)
443 {
444 platform_driver_unregister(&matrix_keypad_driver);
445 }
446
447 module_init(matrix_keypad_init);
448 module_exit(matrix_keypad_exit);
449
450 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
451 MODULE_DESCRIPTION("GPIO Driven Matrix Keypad Driver");
452 MODULE_LICENSE("GPL v2");
453 MODULE_ALIAS("platform:matrix-keypad");
454
This page was automatically generated by the LXR engine.
0 0
- linux keypad driver
- linux keypad driver
- gpio matrix keypad linux driver
- keypad driver
- keypad driver
- keypad driver
- MTK keypad driver
- linux curses库的keypad模式
- mx53 linux gpio 模拟matrix keypad
- Analysis of Keypad Driver of Marvell (Marvell PXA310键盘驱动代码分析)
- Linux driver
- linux driver
- Linux Driver
- linux driver
- linux kernel, driver, network driver
- Optimal Keypad
- Numeric Keypad
- keypad 错误
- navicat for mysql只导出数据表结构
- 转:设计模式6大原则
- jvm参数
- startActivityForResult和setResult详解
- [leetcode][tree][dfs] Populating Next Right Pointers in Each Node
- linux keypad driver
- 跟踪EXPORT_SYMBOL
- svn多分支合并主干冲突 svn Working copy and merge source not ready for reintegration
- cookie和session的区别
- glClearBufferSubData
- 移动端基本touch事件实现
- MMU初始化
- Spring Batch示例: 读取CSV文件并写入MySQL数据库
- mysqli_connect(): (HY000/2002): Permission denied