keys-gpio.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-3-08 GuEe-GUI the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #define DBG_TAG "input.keyboard.gpio"
  13. #define DBG_LVL DBG_INFO
  14. #include <rtdbg.h>
  15. struct gpio_key
  16. {
  17. struct rt_input_device parent;
  18. rt_base_t pin;
  19. rt_uint8_t mode;
  20. rt_uint32_t code;
  21. };
  22. static void gpio_key_event(void *data)
  23. {
  24. struct gpio_key *gkey = data;
  25. rt_input_report_key(&gkey->parent, gkey->code, 1);
  26. rt_input_sync(&gkey->parent);
  27. rt_input_report_key(&gkey->parent, gkey->code, 0);
  28. rt_input_sync(&gkey->parent);
  29. }
  30. static rt_err_t ofw_append_gpio_key(struct rt_ofw_node *np)
  31. {
  32. rt_err_t err = RT_EOK;
  33. rt_uint32_t debounce;
  34. const char *propname;
  35. struct gpio_key *gkey = rt_calloc(1, sizeof(*gkey));
  36. if (!gkey)
  37. {
  38. return -RT_ENOMEM;
  39. }
  40. gkey->pin = rt_ofw_get_named_pin(np, RT_NULL, 0, &gkey->mode, RT_NULL);
  41. if (gkey->pin < 0)
  42. {
  43. err = gkey->pin;
  44. goto _fail;
  45. }
  46. if ((propname = rt_ofw_get_prop_fuzzy_name(np, ",code$")) &&
  47. !rt_ofw_prop_read_u32(np, propname, &gkey->code))
  48. {
  49. rt_input_set_capability(&gkey->parent, EV_KEY, gkey->code);
  50. if (!(err = rt_input_device_register(&gkey->parent)))
  51. {
  52. err = rt_pin_attach_irq(gkey->pin, gkey->mode, gpio_key_event, gkey);
  53. if (err)
  54. {
  55. rt_input_device_unregister(&gkey->parent);
  56. goto _fail;
  57. }
  58. rt_pin_irq_enable(gkey->pin, RT_TRUE);
  59. }
  60. }
  61. if (err)
  62. {
  63. goto _fail;
  64. }
  65. if (!rt_ofw_prop_read_u32(np, "debounce-interval", &debounce))
  66. {
  67. rt_pin_debounce(gkey->pin, debounce);
  68. }
  69. rt_ofw_data(np) = gkey;
  70. return RT_EOK;
  71. _fail:
  72. rt_free(gkey);
  73. return err;
  74. }
  75. static rt_err_t gpio_key_probe(struct rt_platform_device *pdev)
  76. {
  77. rt_err_t err = RT_EOK;
  78. struct rt_ofw_node *key_np, *np = pdev->parent.ofw_node;
  79. rt_ofw_foreach_available_child_node(np, key_np)
  80. {
  81. err = ofw_append_gpio_key(key_np);
  82. if (err == -RT_ENOMEM)
  83. {
  84. rt_ofw_node_put(key_np);
  85. return err;
  86. }
  87. else if (err)
  88. {
  89. LOG_E("%s: create KEY fail", rt_ofw_node_full_name(key_np));
  90. continue;
  91. }
  92. }
  93. return err;
  94. }
  95. static rt_err_t gpio_key_remove(struct rt_platform_device *pdev)
  96. {
  97. struct rt_ofw_node *key_np, *np = pdev->parent.ofw_node;
  98. rt_ofw_foreach_available_child_node(np, key_np)
  99. {
  100. struct gpio_key *gkey = rt_ofw_data(key_np);
  101. if (!gkey)
  102. {
  103. continue;
  104. }
  105. rt_ofw_data(key_np) = RT_NULL;
  106. rt_pin_irq_enable(gkey->pin, RT_FALSE);
  107. rt_pin_detach_irq(gkey->pin);
  108. rt_input_device_unregister(&gkey->parent);
  109. rt_free(gkey);
  110. }
  111. return RT_EOK;
  112. }
  113. static const struct rt_ofw_node_id gpio_key_ofw_ids[] =
  114. {
  115. { .compatible = "gpio-keys" },
  116. { /* sentinel */ }
  117. };
  118. static struct rt_platform_driver gpio_key_driver =
  119. {
  120. .name = "gpio-keys",
  121. .ids = gpio_key_ofw_ids,
  122. .probe = gpio_key_probe,
  123. .remove = gpio_key_remove,
  124. };
  125. RT_PLATFORM_DRIVER_EXPORT(gpio_key_driver);