一, Keypad Driver
分析一下 fsl 的 keypad 驱动,我们的析子是使用一个pin 对应一个 key的方法,并不是用矩阵的。
init
static int __init mxc_kpp_init(void)
{
platform_device_register(&gpio_keys_device);
return platform_driver_register(&mxc_kpd_driver);
}
static struct platform_device gpio_keys_device = {
.name = "mxc_keypad",
.id = -1,
.dev = {
.platform_data = &gpio_keys_platform_data,
},
};
static struct gpio_keys_platform_data gpio_keys_platform_data = {
.buttons = gpio_keys_buttons,
.nbuttons = ARRAY_SIZE(gpio_keys_buttons),
};
定义按键,除 power key外一共10个:
gpio_keys_button,gpio_keys_platform_data 的定义见 gpio_keys.h
static struct gpio_keys_button gpio_keys_buttons[] = {
[0] = {
.type = EV_KEY,
.code = KEY_MENU,
.gpio = gpio_sw1_pin,
.active_low = 1,
.wakeup = 1,
.desc = "MENU",
.debounce_interval = GPIO_KEY_DEBOUNCE_TIME,
},
[1] = {
.type = EV_KEY,
.code = KEY_CAMERA,
.gpio = gpio_sw2_pin,
.active_low = 1,
.wakeup = 1,
.desc = "MUSIC",
.debounce_interval = GPIO_KEY_DEBOUNCE_TIME,
},
[2] = {
.type = EV_KEY,
.code = KEY_ENTER,
.gpio = gpio_sw3_pin,
.active_low = 1,
.wakeup = 1,
.desc = "ENTER",
.debounce_interval = GPIO_KEY_DEBOUNCE_TIME,
},
[3] = {
.type = EV_KEY,
.code = KEY_HOME,
.gpio = gpio_sw4_pin,
.active_low = 1,
.wakeup = 1,
.desc = "HOME",
.debounce_interval = GPIO_KEY_DEBOUNCE_TIME,
},
[4] = {
.type = EV_KEY,
.code = KEY_LEFT,
.gpio = gpio_sw5_pin,
.active_low = 1,
.wakeup = 1,
.desc = "LEFT",
.debounce_interval = GPIO_KEY_DEBOUNCE_TIME,
},
[5] = {
.type = EV_KEY,
.code = KEY_RIGHT,
.gpio = gpio_sw6_pin,
.active_low = 1,
.wakeup = 1,
.desc = "RIGHT",
.debounce_interval = GPIO_KEY_DEBOUNCE_TIME,
},
[6] = {
.type = EV_KEY,
.code = KEY_UP,
.gpio = gpio_sw7_pin,
.active_low = 1,
.wakeup = 1,
.desc = "UP",
.debounce_interval = GPIO_KEY_DEBOUNCE_TIME,
},
[7] = {
.type = EV_KEY,
.code = KEY_DOWN,
.gpio = gpio_sw8_pin,
.active_low = 1,
.wakeup = 1,
.desc = "DOWN",
.debounce_interval = GPIO_KEY_DEBOUNCE_TIME,
},
[8] = {
.type = EV_KEY,
.code = KEY_BACK,
.gpio = gpio_sw9_pin,
.active_low = 1,
.wakeup = 1,
.desc = "BACK",
.debounce_interval = GPIO_KEY_DEBOUNCE_TIME,
},
[9] = {
.type = EV_KEY,
.code = KEY_WIMAX,
.gpio = gpio_WLESS_SW_pin,
.active_low = 1,
.wakeup = 1,
.desc = "WLESS_SW",
.debounce_interval = GPIO_KEY_DEBOUNCE_TIME,
}
};
入口函数:
static int __devinit eih_mxc_keypad_probe(struct platform_device *pdev)
{
// 即上文定义的 .platform_data = &gpio_keys_platform_data
struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
...
// 分配设备空间
ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
pdata->nbuttons * sizeof(struct gpio_button_data),
GFP_KERNEL);
input = input_allocate_device();
// 一般设备信息
platform_set_drvdata(pdev, ddata);
//input->name = pdev->name;
input->name = "mxckpd";
input->phys = "gpio-keys/input0";
input->dev.parent = &pdev->dev;
input->open = eih_mxc_keypad_open;
input->close = eih_mxc_keypad_close;
input->id.bustype = BUS_HOST;
input->id.vendor = 0x0001;
input->id.product = 0x0001;
input->id.version = 0x0100;
// 为每个按键申请
for (i = 0; i < pdata->nbuttons; i++) {
struct gpio_keys_button *button = &pdata->buttons[i];
//
struct gpio_button_data *bdata = &ddata->data[i];
int irq;
// button type
unsigned int type = button->type ?: EV_KEY;
bdata->input = input;
bdata->button = button;
// 初始化计时器
setup_timer(&bdata->timer,
gpio_keys_timer, (unsigned long)bdata);
// 增加一个工作任务
INIT_WORK(&bdata->work, gpio_keys_report_event);
error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
if (error < 0) {
pr_err("gpio-keys: failed to request GPIO %d,"
" error %d\n", button->gpio, error);
goto fail2;
}
error = gpio_direction_input(button->gpio);
if (error < 0) {
pr_err("gpio-keys: failed to configure input"
" direction for GPIO %d, error %d\n",
button->gpio, error);
gpio_free(button->gpio);
goto fail2;
}
irq = gpio_to_irq(button->gpio);
if (irq < 0) {
error = irq;
pr_err("gpio-keys: Unable to get irq number"
" for GPIO %d, error %d\n",
button->gpio, error);
gpio_free(button->gpio);
goto fail2;
}
error = request_irq(irq, gpio_keys_isr,
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
button->desc ? button->desc : "gpio_keys",
bdata);
if (error) {
pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
irq, error);
gpio_free(button->gpio);
goto fail2;
}
if (button->wakeup)
wakeup = 1;
// 定义只接受的事件
input_set_capability(input, type, button->code);
}
error = input_register_device(input);
if (error) {
pr_err("gpio-keys: Unable to register input device, "
"error: %d\n", error);
goto fail2;
}
device_init_wakeup(&pdev->dev, wakeup);
key_dev = &pdev->dev;
#ifdef CONFIG_EARLYSUSPEND
register_early_suspend(&eih_mxc_keypad_earlysuspend);
#endif
...
}
阅读(2029) | 评论(0) | 转发(0) |