gpiokey_acts.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (c) 2017 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief GPIO Keyboard 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. LOG_MODULE_REGISTER(gpiokey, CONFIG_LOG_DEFAULT_LEVEL);
  23. #define GPIOKEY_PIN_MODE_DEFAULT (GPIO_INPUT | GPIO_PULL_UP)
  24. #define GPIOKEY_PIN_MODE_IRQ (GPIO_INPUT | GPIO_ACTIVE_LOW)
  25. struct acts_gpiokey_data {
  26. input_notify_t notify;
  27. #ifdef GPIO_KEY_POLL_TIMER
  28. struct k_timer timer;
  29. #else
  30. struct k_delayed_work timer;
  31. #endif
  32. const struct device *this_dev;
  33. uint32_t time_wait;
  34. uint8_t scan_count;
  35. };
  36. struct acts_gpiokey_config {
  37. uint8_t pinmux_size;
  38. const struct acts_pin_config *pinmux;
  39. uint16_t poll_interval_ms;
  40. uint32_t poll_total_ms;
  41. uint8_t sample_filter_cnt;
  42. };
  43. typedef void (*gpio_config_irq_t)(struct device *dev);
  44. struct acts_gpio_config {
  45. uint32_t base;
  46. uint32_t irq_num;
  47. gpio_config_irq_t config_func;
  48. };
  49. static struct acts_gpiokey_data gpiokey_acts_ddata;
  50. static const struct acts_pin_config pins_gpiokey[] = {CONFIG_GPIOKEY_MFP};
  51. static const struct acts_gpiokey_config gpiokey_acts_cdata = {
  52. .pinmux = pins_gpiokey,
  53. .poll_interval_ms = CONFIG_GPIOKEY_POLL_INTERVAL_MS,
  54. .poll_total_ms = CONFIG_GPIOKEY_POLL_TOTAL_MS,
  55. .sample_filter_cnt = CONFIG_GPIOKEY_SAMPLE_FILTER_CNT,
  56. .pinmux_size = ARRAY_SIZE(pins_gpiokey),
  57. };
  58. struct gpio_status {
  59. const struct device *gpio_dev;
  60. uint8_t past_tatus;
  61. uint8_t curent_tatus;
  62. };
  63. static void gpiokey_acts_enable(const struct device *dev);
  64. static void gpiokey_acts_disable(const struct device *dev);
  65. struct gpio_status key_gpio_status[ARRAY_SIZE(pins_gpiokey)];
  66. struct gpio_callback key_gpio_cb[ARRAY_SIZE(pins_gpiokey)];
  67. uint8_t disable_cb;
  68. static void KEY_IRQ_callback(const struct device *port, struct gpio_callback *cb, uint32_t pins)
  69. {
  70. for( int i = 0; i < ARRAY_SIZE(pins_gpiokey); i++){
  71. gpio_pin_configure(key_gpio_status[i].gpio_dev, pins_gpiokey[i].pin_num % 32, GPIOKEY_PIN_MODE_DEFAULT);
  72. }
  73. gpiokey_acts_ddata.time_wait = k_uptime_get_32();
  74. #ifdef GPIO_KEY_POLL_TIMER
  75. k_timer_start(&gpiokey_acts_ddata.timer, K_MSEC(gpiokey_acts_cdata.poll_interval_ms), K_NO_WAIT);
  76. #else
  77. k_delayed_work_submit(&gpiokey_acts_ddata.timer, K_MSEC(gpiokey_acts_cdata.poll_interval_ms));
  78. #endif
  79. }
  80. #ifdef GPIO_KEY_POLL_TIMER
  81. static void gpiokey_acts_poll(struct k_timer *timer)
  82. #else
  83. static void gpiokey_acts_poll(struct k_work *work)
  84. #endif
  85. {
  86. #ifdef GPIO_KEY_POLL_TIMER
  87. const struct device *dev = k_timer_user_data_get(timer);
  88. struct acts_gpiokey_data *gpiokey = dev->data;
  89. #else
  90. struct acts_gpiokey_data *gpiokey = CONTAINER_OF(work, struct acts_gpiokey_data, timer);
  91. const struct device *dev = gpiokey->this_dev;
  92. #endif
  93. const struct acts_gpiokey_config *cfg = dev->config;
  94. const struct acts_pin_config *pinconf = cfg->pinmux;
  95. uint8_t val;
  96. int i;
  97. for (i = 0; i < cfg->pinmux_size; i++) {
  98. val = gpio_pin_get(key_gpio_status[i].gpio_dev, (pinconf[i].pin_num % 32));
  99. if (val < 0) {
  100. LOG_DBG("failed to get gpio:%d input state", pinconf[i].pin_num);
  101. continue;
  102. }
  103. LOG_DBG("gpiokey pin:%d state:%d stable_state:%d",
  104. pinconf[i].pin_num, val, key_gpio_status[i].past_tatus);
  105. if (val != key_gpio_status[i].past_tatus) {//a key is pressed or released
  106. if (++gpiokey->scan_count == cfg->sample_filter_cnt) {
  107. gpiokey->time_wait = k_uptime_get_32();
  108. key_gpio_status[i].past_tatus = val;
  109. if(gpiokey->notify) {
  110. struct input_value val_notify = {0};
  111. val_notify.keypad.type = EV_KEY;
  112. #ifdef CONFIG_GPIOKEY_USER_KEYCODE
  113. val_notify.keypad.code = CONFIG_GPIOKEY_USER_KEYCODE;
  114. #else
  115. val_notify.keypad.code = KEY_NUM0 + i;
  116. #endif
  117. if (CONFIG_GPIOKEY_PRESSED_VOLTAGE_LEVEL == val)
  118. val_notify.keypad.value = 1; /* key pressed */
  119. else
  120. val_notify.keypad.value = 0; /* key release */
  121. gpiokey->notify(NULL, &val_notify);
  122. }
  123. gpiokey->scan_count = 0;
  124. }
  125. goto out;
  126. }
  127. if (key_gpio_status[i].past_tatus == CONFIG_GPIOKEY_PRESSED_VOLTAGE_LEVEL) {
  128. if(gpiokey->notify) {
  129. struct input_value val_notify = {0};
  130. val_notify.keypad.type = EV_KEY;
  131. #ifdef CONFIG_GPIOKEY_USER_KEYCODE
  132. val_notify.keypad.code = CONFIG_GPIOKEY_USER_KEYCODE;
  133. #else
  134. val_notify.keypad.code = KEY_NUM0 + i;
  135. #endif
  136. val_notify.keypad.value = 1;
  137. gpiokey->notify(NULL, &val_notify);
  138. }
  139. }
  140. }
  141. out:
  142. if ((k_uptime_get_32() - gpiokey->time_wait) > cfg->poll_total_ms) {
  143. gpiokey_acts_disable(dev);
  144. gpiokey->scan_count = 0;
  145. for(i = 0; i < cfg->pinmux_size; i++) {
  146. gpio_pin_configure(key_gpio_status[i].gpio_dev, (pinconf[i].pin_num % 32), GPIOKEY_PIN_MODE_IRQ);
  147. gpio_pin_interrupt_configure(key_gpio_status[i].gpio_dev, (pinconf[i].pin_num % 32), GPIO_INT_EDGE_TO_INACTIVE);
  148. gpio_init_callback(&key_gpio_cb[i], KEY_IRQ_callback, BIT(pinconf[i].pin_num % 32));
  149. gpio_add_callback(key_gpio_status[i].gpio_dev, &key_gpio_cb[i]);
  150. gpio_pin_interrupt_configure(key_gpio_status[i].gpio_dev, (pinconf[i].pin_num % 32), GPIO_INT_EDGE_BOTH);
  151. }
  152. } else {
  153. #ifdef GPIO_KEY_POLL_TIMER
  154. k_timer_start(&gpiokey->timer, K_MSEC(cfg->poll_interval_ms), K_NO_WAIT);
  155. #else
  156. k_delayed_work_submit(&gpiokey->timer, K_MSEC(cfg->poll_interval_ms));
  157. #endif
  158. }
  159. }
  160. static void gpiokey_acts_enable(const struct device *dev)
  161. {
  162. struct acts_gpiokey_data *gpiokey = dev->data;
  163. gpiokey->time_wait = k_uptime_get_32();
  164. #ifdef GPIO_KEY_POLL_TIMER
  165. const struct acts_gpiokey_config *cfg = dev->config;
  166. k_timer_start(&gpiokey->timer, K_MSEC(cfg->poll_interval_ms), K_NO_WAIT);
  167. #else
  168. k_delayed_work_submit(&gpiokey->timer, K_NO_WAIT);
  169. #endif
  170. LOG_DBG("enable gpiokey");
  171. }
  172. static void gpiokey_acts_disable(const struct device *dev)
  173. {
  174. struct acts_gpiokey_data *gpiokey = dev->data;
  175. #ifdef GPIO_KEY_POLL_TIMER
  176. k_timer_stop(&gpiokey->timer);
  177. #else
  178. k_delayed_work_cancel(&gpiokey->timer);
  179. #endif
  180. LOG_DBG("disable gpiokey");
  181. }
  182. static void gpiokey_acts_register_notify(const struct device *dev, input_notify_t notify)
  183. {
  184. struct acts_gpiokey_data *gpiokey = dev->data;
  185. LOG_DBG("register notify 0x%x", (uint32_t)notify);
  186. gpiokey->notify = notify;
  187. }
  188. static void gpiokey_acts_unregister_notify(const struct device *dev, input_notify_t notify)
  189. {
  190. struct acts_gpiokey_data *gpiokey = dev->data;
  191. LOG_DBG("unregister notify 0x%x", (uint32_t)notify);
  192. gpiokey->notify = NULL;
  193. }
  194. const struct input_dev_driver_api gpiokey_acts_driver_api = {
  195. .enable = gpiokey_acts_enable,
  196. .disable = gpiokey_acts_disable,
  197. .register_notify = gpiokey_acts_register_notify,
  198. .unregister_notify = gpiokey_acts_unregister_notify,
  199. };
  200. int gpiokey_acts_init(const struct device *dev)
  201. {
  202. const struct acts_gpiokey_config *cfg = dev->config;
  203. const struct acts_pin_config *pinconf = cfg->pinmux;
  204. struct acts_gpiokey_data *gpiokey = dev->data;
  205. int val, i;
  206. for(i = 0; i < cfg->pinmux_size; i++) {
  207. const struct device *gpio_dev = device_get_binding(CONFIG_GPIO_PIN2NAME(pinconf->pin_num));
  208. if (!gpio_dev) {
  209. LOG_ERR("failed to bind GPIO:%d", pinconf->pin_num);
  210. continue;
  211. }
  212. gpio_pin_configure(gpio_dev, (pinconf[i].pin_num % 32), GPIOKEY_PIN_MODE_DEFAULT);
  213. val = gpio_pin_get(gpio_dev, (pinconf[i].pin_num % 32));
  214. if (val < 0) {
  215. LOG_ERR("failed to get gpio:%d input state", pinconf[i].pin_num);
  216. continue;
  217. }
  218. key_gpio_status[i].past_tatus = val;
  219. key_gpio_status[i].gpio_dev = gpio_dev;
  220. }
  221. gpiokey->this_dev = dev;
  222. gpiokey->scan_count = 0;
  223. #ifdef GPIO_KEY_POLL_TIMER
  224. k_timer_init(&gpiokey->timer, gpiokey_acts_poll, NULL);
  225. k_timer_user_data_set(&gpiokey->timer, (void *)dev);
  226. #else
  227. k_delayed_work_init(&gpiokey->timer, gpiokey_acts_poll);
  228. #endif
  229. return 0;
  230. }
  231. #if IS_ENABLED(CONFIG_GPIOKEY)
  232. DEVICE_DEFINE(gpiokey, CONFIG_INPUT_DEV_ACTS_GPIOKEY_NAME,
  233. gpiokey_acts_init, NULL,
  234. &gpiokey_acts_ddata, &gpiokey_acts_cdata,
  235. POST_KERNEL, 60,
  236. &gpiokey_acts_driver_api);
  237. #endif