knobencoder_acts.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2022 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief knob encoder driver for Actions SoC
  9. */
  10. #include <errno.h>
  11. #include <kernel.h>
  12. #include <string.h>
  13. #include <irq.h>
  14. #include <drivers/input/input_dev.h>
  15. #include <sys/util.h>
  16. #include <sys/byteorder.h>
  17. #include <device.h>
  18. #include <drivers/gpio.h>
  19. #include <soc.h>
  20. #include <board.h>
  21. #include <logging/log.h>
  22. static const struct acts_pin_config pins_knobencoder[] = {CONFIG_KNOBGPIO_INIA,CONFIG_KNOBGPIO_INIB};
  23. enum KNOB_ACTION_TYPE
  24. {
  25. KONB_NO_ACTION = 0,
  26. KONB_CLOCKWISE_PRE,
  27. KONB_CLOCKWISE,
  28. KONB_ANTICLOCKWISE_PRE,
  29. KONB_ANTICLOCKWISE,
  30. };
  31. struct acts_knobencoder_data {
  32. input_notify_t notify;
  33. const struct device *this_dev;
  34. struct gpio_callback key_gpio_cb[ARRAY_SIZE(pins_knobencoder)];
  35. uint8_t action;
  36. uint8_t rotation_speed;
  37. };
  38. struct acts_knobencoder_config {
  39. uint8_t pinmux_size;
  40. const struct acts_pin_config *pinmux;
  41. };
  42. static struct acts_knobencoder_data knobencoder_acts_data;
  43. static const struct acts_knobencoder_config knobencoder_acts_config = {
  44. .pinmux_size = ARRAY_SIZE(pins_knobencoder),
  45. .pinmux = pins_knobencoder,
  46. };
  47. static void knob_acts_register_notify(const struct device *dev, input_notify_t notify)
  48. {
  49. struct acts_knobencoder_data *knobkey = dev->data;
  50. // LOG_DBG("register notify 0x%x", (uint32_t)notify);
  51. knobkey->notify = notify;
  52. }
  53. static void knob_acts_unregister_notify(const struct device *dev, input_notify_t notify)
  54. {
  55. struct acts_knobencoder_data *knobkey = dev->data;
  56. // LOG_DBG("unregister notify 0x%x", (uint32_t)notify);
  57. knobkey->notify = NULL;
  58. }
  59. static void knob_acts_enable(const struct device *dev)
  60. {
  61. const struct acts_knobencoder_config *cfg = dev->config;
  62. const struct acts_pin_config *pinconf = cfg->pinmux;
  63. uint8_t i;
  64. for(i = 0; i < cfg->pinmux_size; i++)
  65. {
  66. const struct device *gpio_dev = device_get_binding(CONFIG_GPIO_PIN2NAME(pinconf[i].pin_num));
  67. if (!gpio_dev) {
  68. printk("failed to bind GPIO:%d\n", pinconf[i].pin_num);
  69. }
  70. gpio_pin_interrupt_configure(gpio_dev, (pinconf[i].pin_num % 32), GPIO_INT_EDGE_FALLING);
  71. }
  72. }
  73. static void knob_acts_disable(const struct device *dev)
  74. {
  75. const struct acts_knobencoder_config *cfg = dev->config;
  76. const struct acts_pin_config *pinconf = cfg->pinmux;
  77. uint8_t i;
  78. for(i = 0; i < cfg->pinmux_size; i++)
  79. {
  80. const struct device *gpio_dev = device_get_binding(CONFIG_GPIO_PIN2NAME(pinconf[i].pin_num));
  81. if (!gpio_dev) {
  82. printk("failed to bind GPIO:%d\n", pinconf[i].pin_num);
  83. }
  84. gpio_pin_interrupt_configure(gpio_dev, (pinconf[i].pin_num % 32), GPIO_INT_DISABLE);
  85. }
  86. }
  87. static void KNOB_IRQ_callback(const struct device *port, struct gpio_callback *cb, uint32_t pins)
  88. {
  89. const struct device *gpio_devA = device_get_binding(CONFIG_GPIO_PIN2NAME(pins_knobencoder[0].pin_num));
  90. const struct device *gpio_devB = device_get_binding(CONFIG_GPIO_PIN2NAME(pins_knobencoder[1].pin_num));
  91. if(cb->pin_mask == (1<<(pins_knobencoder[0].pin_num % 32)))
  92. {
  93. if(!gpio_pin_get(gpio_devA, pins_knobencoder[0].pin_num % 32))
  94. {
  95. gpio_pin_interrupt_configure(gpio_devA, (pins_knobencoder[0].pin_num % 32), GPIO_INT_EDGE_RISING);
  96. if(!gpio_pin_get(gpio_devB, pins_knobencoder[1].pin_num % 32))
  97. {
  98. //gpio_pin_interrupt_configure(gpio_devB, (pins_knobencoder[1].pin_num % 32), GPIO_INT_DISABLE);
  99. knobencoder_acts_data.action = KONB_CLOCKWISE_PRE;
  100. }
  101. }
  102. else
  103. {
  104. gpio_pin_interrupt_configure(gpio_devA, (pins_knobencoder[0].pin_num % 32), GPIO_INT_EDGE_FALLING);
  105. if((knobencoder_acts_data.action == KONB_CLOCKWISE_PRE) &&
  106. gpio_pin_get(gpio_devB, pins_knobencoder[1].pin_num % 32))
  107. {
  108. // printk("KONB_CLOCKWISE\n");
  109. knobencoder_acts_data.action = KONB_CLOCKWISE;
  110. if(knobencoder_acts_data.notify) {
  111. struct input_value val_notify = {0};
  112. val_notify.keypad.value = KEY_KONB_CLOCKWISE;
  113. knobencoder_acts_data.notify(NULL, &val_notify);
  114. }
  115. knobencoder_acts_data.action = KONB_NO_ACTION;
  116. gpio_pin_interrupt_configure(gpio_devB, (pins_knobencoder[1].pin_num % 32), GPIO_INT_EDGE_FALLING);
  117. }
  118. }
  119. }
  120. else if (cb->pin_mask == (1<<(pins_knobencoder[1].pin_num % 32)))
  121. {
  122. if(!gpio_pin_get(gpio_devB, pins_knobencoder[1].pin_num % 32))
  123. {
  124. gpio_pin_interrupt_configure(gpio_devB, (pins_knobencoder[1].pin_num % 32), GPIO_INT_EDGE_RISING);
  125. if(!gpio_pin_get(gpio_devA, pins_knobencoder[0].pin_num % 32))
  126. {
  127. //gpio_pin_interrupt_configure(gpio_devA, (pins_knobencoder[0].pin_num % 32), GPIO_INT_DISABLE);
  128. knobencoder_acts_data.action = KONB_ANTICLOCKWISE_PRE;
  129. }
  130. }
  131. else
  132. {
  133. gpio_pin_interrupt_configure(gpio_devB, (pins_knobencoder[1].pin_num % 32), GPIO_INT_EDGE_FALLING);
  134. if((knobencoder_acts_data.action == KONB_ANTICLOCKWISE_PRE) &&
  135. gpio_pin_get(gpio_devA, pins_knobencoder[0].pin_num % 32))
  136. {
  137. // printk("KONB_ANTICLOCKWISE\n");
  138. knobencoder_acts_data.action = KONB_ANTICLOCKWISE;
  139. if(knobencoder_acts_data.notify) {
  140. struct input_value val_notify = {0};
  141. val_notify.keypad.value = KEY_KONB_ANTICLOCKWISE;
  142. knobencoder_acts_data.notify(NULL, &val_notify);
  143. }
  144. knobencoder_acts_data.action = KONB_NO_ACTION;
  145. gpio_pin_interrupt_configure(gpio_devA, (pins_knobencoder[0].pin_num % 32), GPIO_INT_EDGE_FALLING);
  146. }
  147. }
  148. }
  149. }
  150. const struct input_dev_driver_api knobencoder_acts_driver_api = {
  151. .enable = knob_acts_enable,
  152. .disable = knob_acts_disable,
  153. .register_notify = knob_acts_register_notify,
  154. .unregister_notify = knob_acts_unregister_notify,
  155. };
  156. int knobencoder_acts_init(const struct device *dev)
  157. {
  158. const struct acts_knobencoder_config *cfg = dev->config;
  159. const struct acts_pin_config *pinconf = cfg->pinmux;
  160. struct acts_knobencoder_data *knobencoder = dev->data;
  161. uint8_t i;
  162. for(i = 0; i < cfg->pinmux_size; i++)
  163. {
  164. const struct device *gpio_dev = device_get_binding(CONFIG_GPIO_PIN2NAME(pinconf[i].pin_num));
  165. if (!gpio_dev) {
  166. printk("failed to bind GPIO:%d\n", pinconf[i].pin_num);
  167. }
  168. gpio_pin_configure(gpio_dev, (pinconf[i].pin_num % 32), (GPIO_PULL_UP | GPIO_INPUT | GPIO_INT_DEBOUNCE));
  169. gpio_pin_interrupt_configure(gpio_dev, (pinconf[i].pin_num % 32), GPIO_INT_EDGE_FALLING);
  170. gpio_init_callback(&knobencoder->key_gpio_cb[i], KNOB_IRQ_callback, BIT(pinconf[i].pin_num % 32));
  171. gpio_add_callback(gpio_dev, &knobencoder->key_gpio_cb[i]);
  172. }
  173. return 0;
  174. }
  175. #if IS_ENABLED(CONFIG_KNOB_ENCODER)
  176. DEVICE_DEFINE(knobencoder, CONFIG_KNOB_ENCODER_DEV_NAME,
  177. knobencoder_acts_init, NULL,
  178. &knobencoder_acts_data, &knobencoder_acts_config,
  179. POST_KERNEL, 60,
  180. &knobencoder_acts_driver_api);
  181. #endif