我们知道:rk3288 android7.1主板按键的使用主要有GPIO接口的和ADC接口的,而我的项目这里,需要添加两个GPIO类型的按键,现在记录一下,其实主要是dts的配置而已,驱动不需要改动:
1、首先看下原理图,对应找到具体使用的gpio:从原理图上可知,power按键的GPIO使用的是GPIO0_A1, recovery按键的GPIO使用的是GPIO8_A1
2、dts的配置方面
164 gpio_keys { //参考同目录下的 rk3036-echo.dts
165 compatible = "gpio-keys";
166 #address-cells = <1>;
167 #size-cells = <0>;
168
169 pinctrl-names = "default";
170 pinctrl-0 = <&pwr_key &rcvr_key>;
171
172 power_key: power-key { //power按键
173 label = "GPIO Key Power";
174 gpios = <&gpio0 1 GPIO_ACTIVE_LOW>; // [gpio2 25] to GPIO0_A1
175 linux,code = <KEY_POWER>; // 207
176 debounce-interval = <100>;
177 wakeup-source;
178 };
179 recovery_key: recovery_key { //recovery按键
180 label = "recovery";
181 gpios = <&gpio8 1 GPIO_ACTIVE_LOW>; //GPIO8_A1
182 linux,code = <KEY_CLEAR>; // 0x163 ==> 355
183 debounce-interval = <100>;
184 wakeup-source;
185 };
186 };
771 &pinctrl {
833 keys {
834 pwr_key: pwr-key {
835 rockchip,pins = <0 1 RK_FUNC_GPIO &pcfg_pull_none>; // pcfg_pull_default
836 };
837 rcvr_key: rcvr-key {
838 rockchip,pins = <8 1 RK_FUNC_GPIO &pcfg_pull_none>;
839 };
840 };
871 };
对应的gpio按键驱动在以下文件:
./drivers/input/keyboard/gpio_keys.c
上报键值的接口函数
339 static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
340 {
341 const struct gpio_keys_button *button = bdata->button;
342 struct input_dev *input = bdata->input;
343 unsigned int type = button->type ?: EV_KEY;
344 int state = gpio_get_value_cansleep(button->gpio); //get gpio state
345
346 printk("czd0710: enter [%s(), %d], gpio_no ==> %d\n", __func__, __LINE__, button->gpio);
347 printk("czd0710: enter [%s(), %d], get gpio state ==> %d\n", __func__, __LINE__, state);
348 if (state < 0) {
349 dev_err(input->dev.parent, "failed to get gpio state\n");
350 return;
351 }
352
353 state = (state ? 1 : 0) ^ button->active_low; //这里得出判断按键的状态,最终得到的state为1就是按下,为0就是抬起
354 printk("czd0710: enter [%s(), %d], state ==> %d\n", __func__, __LINE__, state);
355 if (type == EV_ABS) { //事件类型为EV_ABS ==> 0x03
356 if (state) {
357 input_event(input, type, button->code, button->value);
358 printk("czd0710: enter [%s(), %d], state ==> %d\n", __func__, __LINE__, state);
359 }
360 } else {
361 input_event(input, type, button->code, !!state); //上报键值和状态
362 printk("czd0710: enter [%s(), %d], state ==> %d\n", __func__, __LINE__, state);
363 printk("czd0710: enter [%s(), %d], !!state ==> %d\n", __func__, __LINE__, !!state);
364 }
365 input_sync(input); //sync一下,表示此次上报完成
366 }
更新kernel之后验证:
在串口输入getevent,然后去按recovery按键就可以得到input上报给上层的键值0x163:
rk3288:/ # getevent
add device 1: /dev/input/event4
name: "rk29-keypad"
add device 2: /dev/input/event2
name: "gpio_keys"
add device 3: /dev/input/event3
name: "gsensor"
add device 4: /dev/input/event0
name: "ff680020.pwm"
add device 5: /dev/input/event1
name: "gtinput"
/dev/input/event2: 0001 0163 00000001 //0x163对应上面dts配置的recovery_key linux,code;后面的1表示按下按键
/dev/input/event2: 0000 0000 00000000
/dev/input/event2: 0001 0163 00000000 //0x163对应上面dts配置的recovery_key linux,code;后面的0表示释放按键
/dev/input/event2: 0000 0000 00000000
/dev/input/event2: 0001 0163 00000001
/dev/input/event2: 0000 0000 00000000