it7259_tpkey_acts.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright (c) 2017 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief TP Keyboard driver for Actions SoC
  9. */
  10. #include <errno.h>
  11. #include <kernel.h>
  12. #include <string.h>
  13. #include <init.h>
  14. #include <irq.h>
  15. #include <drivers/adc.h>
  16. #include <drivers/input/input_dev.h>
  17. #include <sys/util.h>
  18. #include <sys/byteorder.h>
  19. #include <board.h>
  20. #include <soc_pmu.h>
  21. #include <logging/log.h>
  22. #include <device.h>
  23. #include <drivers/gpio.h>
  24. #include <soc.h>
  25. #include <string.h>
  26. #include <drivers/i2c.h>
  27. #include <board_cfg.h>
  28. LOG_MODULE_REGISTER(tpkey, CONFIG_SYS_LOG_INPUT_DEV_LEVEL);
  29. #define tp_slaver_addr (0x8C>>1)
  30. #ifdef CONFIG_TPKEY_RESET_GPIO
  31. static const struct gpio_cfg reset_gpio_cfg = CONFIG_TPKEY_RESET_GPIO;
  32. #endif
  33. #ifdef CONFIG_TPKEY_POWER_GPIO
  34. static const struct gpio_cfg power_gpio_cfg = CONFIG_TPKEY_POWER_GPIO;
  35. #endif
  36. #ifdef CONFIG_TPKEY_ISR_GPIO
  37. static const struct gpio_cfg isr_gpio_cfg = CONFIG_TPKEY_ISR_GPIO;
  38. #endif
  39. struct acts_tpkey_data {
  40. input_notify_t notify;
  41. struct device *i2c_dev;
  42. struct device *gpio_dev;
  43. struct device *this_dev;
  44. struct gpio_callback key_gpio_cb;
  45. struct k_delayed_work timer;
  46. };
  47. struct acts_tpkey_config {
  48. uint16_t poll_interval_ms;
  49. uint32_t poll_total_ms;
  50. };
  51. struct k_timer tpkey_inquiry_timer;
  52. u8_t tpkey_flag_timeout;
  53. static struct acts_tpkey_data tpkey_acts_ddata;
  54. static const struct acts_tpkey_config tpkey_acts_cdata = {
  55. .poll_interval_ms = 0,
  56. .poll_total_ms = 0,
  57. };
  58. static void tpkey_acts_enable(struct device *dev);
  59. static void tpkey_acts_disable(struct device *dev);
  60. static void read_touch(struct input_value *val);
  61. void tpkey_detect(struct k_timer *timer)
  62. {
  63. LOG_DBG("go into tpkey_detect\n");
  64. tpkey_flag_timeout = 1;
  65. k_timer_stop(&tpkey_inquiry_timer);
  66. }
  67. static void KEY_IRQ_callback(struct device *port, struct gpio_callback *cb, uint32_t pins)
  68. {
  69. sys_trace_void(SYS_TRACE_ID_TP_IRQ);
  70. #if CONFIG_TPKEY_LOWPOWER
  71. LOG_DBG("inquiry mode\n");
  72. tpkey_flag_timeout = 0;
  73. k_timer_start(&tpkey_inquiry_timer, K_MSEC(20), K_MSEC(20));
  74. #endif
  75. #if CONFIG_TPKEY_READ_POLL_EN
  76. k_delayed_work_submit(&tpkey_acts_ddata.timer, K_NO_WAIT);
  77. #endif
  78. sys_trace_end_call(SYS_TRACE_ID_TP_IRQ);
  79. }
  80. static void tpkey_acts_poll(struct k_work *work)
  81. {
  82. struct acts_tpkey_data *tpkey = CONTAINER_OF(work, struct acts_tpkey_data, timer);
  83. struct device *dev = tpkey->this_dev;
  84. struct input_value val;
  85. read_touch(&val);
  86. tpkey->notify(dev,&val);
  87. k_delayed_work_cancel(&tpkey->timer);
  88. }
  89. static void tpkey_acts_enable(struct device *dev)
  90. {
  91. struct acts_tpkey_data *tpkey = dev->data;
  92. const struct acts_tpkey_config *cfg = dev->config;
  93. gpio_pin_interrupt_configure(tpkey->gpio_dev , isr_gpio_cfg.gpion, GPIO_INT_EDGE_FALLING);//GPIO_INT_DISABLE
  94. LOG_DBG("enable tpkey");
  95. }
  96. static void tpkey_acts_disable(struct device *dev)
  97. {
  98. struct acts_tpkey_data *tpkey = dev->data;
  99. const struct acts_tpkey_config *cfg = dev->config;
  100. gpio_pin_interrupt_configure(tpkey->gpio_dev , isr_gpio_cfg.gpion, GPIO_INT_DISABLE);//GPIO_INT_DISABLE
  101. LOG_DBG("disable tpkey");
  102. }
  103. static void tpkey_acts_inquiry(struct device *dev, struct input_value *val)
  104. {
  105. struct acts_tpkey_data *tpkey = dev->data;
  106. if(tpkey_flag_timeout)
  107. return;
  108. read_touch(val);
  109. LOG_DBG("inquiry tpkey");
  110. }
  111. static void tpkey_acts_register_notify(struct device *dev, input_notify_t notify)
  112. {
  113. struct acts_tpkey_data *tpkey = dev->data;
  114. LOG_DBG("register notify 0x%x", (uint32_t)notify);
  115. tpkey->notify = notify;
  116. }
  117. static void tpkey_acts_unregister_notify(struct device *dev, input_notify_t notify)
  118. {
  119. struct acts_tpkey_data *tpkey = dev->data;
  120. LOG_DBG("unregister notify 0x%x", (uint32_t)notify);
  121. tpkey->notify = NULL;
  122. }
  123. const struct input_dev_driver_api tpkey_acts_driver_api = {
  124. .enable = tpkey_acts_enable,
  125. .disable = tpkey_acts_disable,
  126. .inquiry = tpkey_acts_inquiry,
  127. .register_notify = tpkey_acts_register_notify,
  128. .unregister_notify = tpkey_acts_unregister_notify,
  129. };
  130. #define mode_pin (GPIO_PULL_UP | GPIO_INPUT | GPIO_INT_DEBOUNCE)
  131. void tp_reset(struct device *dev)
  132. {
  133. struct device *gpios_temp = NULL;
  134. gpios_temp = device_get_binding(reset_gpio_cfg.gpio_dev_name);
  135. gpio_pin_configure(gpios_temp , reset_gpio_cfg.gpion, GPIO_OUTPUT| GPIO_PULL_UP);
  136. gpio_pin_set_raw(gpios_temp , reset_gpio_cfg.gpion, 1);
  137. k_busy_wait(1000*1000);
  138. gpio_pin_set_raw(gpios_temp , reset_gpio_cfg.gpion, 0);
  139. k_busy_wait(10*1000);
  140. gpio_pin_set_raw(gpios_temp , reset_gpio_cfg.gpion, 1);
  141. }
  142. static uint8_t tp_enter_bootmode(struct device *dev)
  143. {
  144. struct acts_tpkey_data *tpkey = dev->data;
  145. uint8_t retrycnt = 10;
  146. tp_reset(dev);
  147. k_busy_wait(5*1000);
  148. while(retrycnt--){
  149. uint8_t cmd[4] = {0};
  150. cmd[0] = 0xA0;
  151. cmd[1] = 0x01;
  152. cmd[2] = 0xab;
  153. if(-1 == i2c_write(tpkey->i2c_dev,cmd, 3, 0X6A)) {
  154. k_busy_wait(4*1000);
  155. continue;
  156. }
  157. cmd[0] = 0xA0;
  158. cmd[1] = 0x03;
  159. if(-1 == i2c_write(tpkey->i2c_dev,cmd, 2, 0X6A)) {
  160. k_busy_wait(4*1000);
  161. continue;
  162. }
  163. if(-1 == i2c_read(tpkey->i2c_dev,cmd, 1, 0X6A)) {
  164. k_busy_wait(2*1000);
  165. continue;
  166. } else {
  167. if(cmd[0] != 0x55) {
  168. k_busy_wait(2*1000);
  169. continue;
  170. } else {
  171. return 0;
  172. }
  173. }
  174. }
  175. return -1;
  176. }
  177. #if 0
  178. void read_touch(struct input_value *val)
  179. {
  180. uint8_t cmd[5] = {0};
  181. cmd[0] = 0x02;
  182. if(-1 == i2c_write(tpkey_acts_ddata.i2c_dev, cmd, 1, tp_slaver_addr)) {
  183. return;
  184. } else if(-1 == i2c_read(tpkey_acts_ddata.i2c_dev, cmd, 5, tp_slaver_addr)) {//tp_slaver_addr
  185. return;
  186. }
  187. val->point.loc_x = (((uint16_t)(cmd[1]&0x0f))<<8) | cmd[2];
  188. val->point.loc_y = (((uint16_t)(cmd[3]&0x0f))<<8) | cmd[4];
  189. val->point.pessure_value = cmd[0];
  190. LOG_DBG("finger_num = %d, local:(%d,%d)\n",cmd[0], val->point.loc_x, val->point.loc_y);
  191. }
  192. #endif
  193. static void read_touch(struct input_value *val)
  194. {
  195. uint8_t cmd[14] = {0};
  196. sys_trace_void(SYS_TRACE_ID_TP_READ);
  197. cmd[0] = 0xE0;
  198. // if(-1 == i2c_read(tpkey_acts_ddata.i2c_dev, cmd, 14, tp_slaver_addr))
  199. // return;
  200. if(-1 == i2c_write(tpkey_acts_ddata.i2c_dev, cmd, 1, tp_slaver_addr)) {
  201. return;
  202. } else if(-1 == i2c_read(tpkey_acts_ddata.i2c_dev, cmd, 6, tp_slaver_addr)) {//tp_slaver_addr
  203. return;
  204. }
  205. val->point.loc_x = (((uint16_t)(cmd[3]&0x0f))<<8) | cmd[2];
  206. val->point.loc_y = (((uint16_t)(cmd[3]&0xf0))<<4) | cmd[4];
  207. val->point.pessure_value = cmd[1];
  208. val->point.gesture = cmd[0];
  209. LOG_DBG("finger_num = %d, gesture = %d,local:(%d,%d)\n",cmd[0], val->point.gesture,val->point.loc_x, val->point.loc_y);
  210. sys_trace_end_call(SYS_TRACE_ID_TP_READ);
  211. }
  212. static void enter_low_power_mode(const struct device *dev)
  213. {
  214. uint8_t cmd[6] = {0};
  215. printk("go into low power mode\n");
  216. cmd[0] = 0xfe;
  217. cmd[1] = 0;
  218. i2c_write(tpkey_acts_ddata.i2c_dev, cmd, 2, tp_slaver_addr);
  219. }
  220. static void turn_off_detector_mode()
  221. {
  222. uint8_t cmd[6] = {0};
  223. cmd[0] = 0xfb;
  224. cmd[1] = 0;
  225. i2c_write(tpkey_acts_ddata.i2c_dev, cmd, 2, tp_slaver_addr);
  226. cmd[0] = 0xfc;
  227. cmd[1] = 0;
  228. i2c_write(tpkey_acts_ddata.i2c_dev, cmd, 2, tp_slaver_addr);
  229. }
  230. int tpkey_acts_init(const struct device *dev)
  231. {
  232. struct acts_tpkey_data *tpkey = dev->data;
  233. const struct acts_tpkey_config *cfg = dev->config;
  234. struct device *gpios_power = NULL;
  235. #ifdef CONFIG_TPKEY_POWER_GPIO
  236. gpios_power = device_get_binding(power_gpio_cfg.gpio_dev_name);
  237. gpio_pin_configure(gpios_power, power_gpio_cfg.gpion, GPIO_OUTPUT| GPIO_PULL_UP);
  238. gpio_pin_set_raw(gpios_power, reset_gpio_cfg.gpion, 0);
  239. #endif
  240. tpkey->i2c_dev = (struct device *)device_get_binding(CONFIG_TPKEY_I2C_NAME);
  241. tpkey->gpio_dev = (struct device *)device_get_binding(isr_gpio_cfg.gpio_dev_name);
  242. if(tpkey->i2c_dev == NULL) {
  243. printk("can not access right i2c device\n");
  244. return -1;
  245. }
  246. if(tpkey->gpio_dev == NULL) {
  247. printk("can not access right gpio device\n");
  248. return -1;
  249. }
  250. tpkey->this_dev = (struct device *)dev;
  251. gpio_pin_configure(tpkey->gpio_dev , isr_gpio_cfg.gpion, mode_pin);
  252. gpio_init_callback(&tpkey->key_gpio_cb , KEY_IRQ_callback, BIT(isr_gpio_cfg.gpion));
  253. gpio_add_callback(tpkey->gpio_dev , &tpkey->key_gpio_cb);
  254. //if(-1 == tp_enter_bootmode(dev)) {
  255. // printk("error return \n");
  256. //}
  257. tpkey_flag_timeout = 0;
  258. tp_reset(dev);
  259. k_busy_wait(100*1000);//the time waiting too short,and the device can not wake up
  260. #if CONFIG_TPKEY_LOWPOWER
  261. // enter_low_power_mode(dev);
  262. k_timer_init(&tpkey_inquiry_timer, tpkey_detect, NULL);
  263. k_timer_start(&tpkey_inquiry_timer, K_MSEC(20), K_MSEC(20));
  264. #endif
  265. // turn_off_detector_mode();
  266. k_delayed_work_init(&tpkey->timer, tpkey_acts_poll);
  267. return 0;
  268. }
  269. #if IS_ENABLED(CONFIG_TPKEY)
  270. DEVICE_DEFINE(tpkey, CONFIG_TPKEY_DEV_NAME, tpkey_acts_init,
  271. NULL, &tpkey_acts_ddata, &tpkey_acts_cdata, POST_KERNEL,
  272. 60, &tpkey_acts_driver_api);
  273. #endif