Bootstrap

Android 12 按键驱动中增加按键去抖功能

— a/alps/kernel-4.14/drivers/input/keyboard/dw_gpio_key.c
+++ b/alps/kernel-4.14/drivers/input/keyboard/dw_gpio_key.c
@@ -17,16 +17,32 @@ static int key_state; // 按键状态
static int key1_eint; // IRQ
static int key1_eint_gpio; // GPIO

+static int key1_value = 0;
+
+static unsigned long key1_swdebounce;
+static unsigned long key1_hwdebounce;
+static struct delayed_work key1_delaywork;
+
static irqreturn_t gpio_key_irq_handler(int irq, void *dev_id) {

  •   //pr_info("%s,%d-irq=%d", __func__, __LINE__, irq);
    
  •   disable_irq_nosync(irq);
    
  •   schedule_delayed_work(&key1_delaywork,
    
  •           msecs_to_jiffies(key1_swdebounce));
    
  • return IRQ_HANDLED;
    +}

+static void key1_eint_workcb(struct work_struct *work)
+{
int state = gpio_get_value(key1_eint_gpio);

  •   pr_info("%s %d state=%d key_state=%d\n", __func__, __LINE__,state, key_state);
    
  •   state = !state;
    
  •   //pr_info("%s %d state=%d key_state=%d\n", __func__, __LINE__,state, key_state);
    
    if (state != key_state) {
    key_state = state;
  •           pr_info("%s %d MY_KEY_1=%d\n", __func__, __LINE__,MY_KEY_1);
    
  •    input_report_key(input_dev, MY_KEY_1, key_state);  // 报告按键事件
    
  •    input_report_key(input_dev, /*MY_KEY_1*/key1_value, key_state);  // 报告按键事件
       input_sync(input_dev);                             // 同步输入设备
    
    }
  • return IRQ_HANDLED;
  •   enable_irq(key1_eint);
    

}

static int dw_gpio_key_probe(struct platform_device *pdev)
@@ -35,7 +51,8 @@ static int dw_gpio_key_probe(struct platform_device *pdev)
int error;

    struct device_node *node = NULL;
  •   //u32 ints[2] = {0};
    
  •   u32 ints[2] = {0};
    
      pr_info("%s %d E+\n", __func__, __LINE__);
    

@@ -71,6 +88,20 @@ static int dw_gpio_key_probe(struct platform_device *pdev)

            pr_info("%s,%d GPIO IRQ : %d\n", __func__,__LINE__, key1_eint);
            enable_irq_wake(key1_eint);
  •           error = of_property_read_u32_array(node, "key1-debounce",
    
  •                   ints, ARRAY_SIZE(ints));
    
  •           if (!error)
    
  •                   key1_hwdebounce = ints[1];
    
  •           key1_swdebounce = msecs_to_jiffies(100);
    
  •           of_property_read_u32(node, "key1-linux-code", &key1_value);
    
  •           if(!key1_value) {
    
  •                   key1_value = MY_KEY_1;
    
  •           }
    
  •           INIT_DELAYED_WORK(&key1_delaywork, key1_eint_workcb);
      }
    

@@ -86,7 +117,7 @@ static int dw_gpio_key_probe(struct platform_device *pdev)
input_dev->name = “dw_gpio_key”;
input_dev->id.bustype = BUS_HOST;
input_dev->evbit[0] = BIT_MASK(EV_KEY);

  • input_dev->keybit[BIT_WORD(MY_KEY_1)] = BIT_MASK(MY_KEY_1);
  • input_dev->keybit[BIT_WORD(/MY_KEY_1/key1_value)] = BIT_MASK(/MY_KEY_1/key1_value);

    /* 注册输入设备 */
    error = input_register_device(input_dev);

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;