ft5x0x_tpkey_acts.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 <stdbool.h>
  14. #include <init.h>
  15. #include <irq.h>
  16. #include <drivers/adc.h>
  17. #include <drivers/input/input_dev.h>
  18. #include <sys/util.h>
  19. #include <sys/byteorder.h>
  20. #include <board.h>
  21. #include <soc_pmu.h>
  22. #include <logging/log.h>
  23. #include <device.h>
  24. #include <drivers/gpio.h>
  25. #include <soc.h>
  26. #include <string.h>
  27. #include <drivers/i2c.h>
  28. #include <board_cfg.h>
  29. #include "capacitive_hynitron_cst816t_update.h"
  30. LOG_MODULE_REGISTER(tpkey, 2);
  31. #define TP_SLAVER_ADDR (0x70 >> 1) // 0x15
  32. //#define tp_slaver_boot_addr (0x70)
  33. #define REG_LEN_1B 1
  34. #define REG_LEN_2B 2
  35. #define POINT_REPORT_MODE 1
  36. #define GESTURE_REPORT_MODE 2
  37. #define GESTURE_AND_POINT_REPORT_MODE 3
  38. static const struct gpio_cfg reset_gpio_cfg = CONFIG_TPKEY_RESET_GPIO;
  39. static const struct gpio_cfg power_gpio_cfg = CONFIG_TPKEY_RESET_GPIO;
  40. static const struct gpio_cfg isr_gpio_cfg = CONFIG_TPKEY_RESET_GPIO;
  41. struct acts_tpkey_data {
  42. input_notify_t notify;
  43. const struct device *i2c_dev;
  44. const struct device *gpio_dev;
  45. const struct device *this_dev;
  46. struct gpio_callback key_gpio_cb;
  47. struct k_work init_timer;
  48. struct k_timer poll_timer;
  49. };
  50. struct acts_tpkey_config {
  51. uint16_t poll_interval_ms;
  52. uint32_t poll_total_ms;
  53. };
  54. uint16_t tp_crc[2] __attribute((used)) = {0};
  55. static struct acts_tpkey_data tpkey_acts_ddata;
  56. static void _ft5x0x_read_touch(struct device *i2c_dev, struct input_value *val);
  57. #define mode_pin (GPIO_PULL_UP | GPIO_INPUT | GPIO_INT_DEBOUNCE)
  58. static void _ft5x0x_poweron(const struct device *dev)
  59. {
  60. //const struct device *gpios_power = device_get_binding(power_gpio_cfg.gpio_dev_name);
  61. //gpio_pin_configure(gpios_power, power_gpio_cfg.gpion, GPIO_OUTPUT| GPIO_PULL_UP);
  62. //gpio_pin_set_raw(gpios_power, power_gpio_cfg.gpion, 0);
  63. }
  64. #if 0
  65. static void _tpkey_irq_callback(struct device *port, struct gpio_callback *cb, uint32_t pins)
  66. {
  67. struct acts_tpkey_data *tpkey = &tpkey_acts_ddata;
  68. struct input_value val;
  69. #if 0
  70. static int time_stame = 0;
  71. printk("irq duration : %d \n",k_cyc_to_ns_floor32(k_cycle_get_32() - time_stame));
  72. time_stame = k_cycle_get_32();
  73. #endif
  74. _ft5x0x_read_touch(tpkey->i2c_dev, &val);
  75. }
  76. #endif
  77. static void _ft5x0x_irq_config(const struct device *dev)
  78. {
  79. #if 0
  80. struct acts_tpkey_data *tpkey = dev->data;
  81. tpkey->gpio_dev = device_get_binding(isr_gpio_cfg.gpio_dev_name);
  82. gpio_pin_configure(tpkey->gpio_dev , isr_gpio_cfg.gpion, mode_pin);
  83. gpio_init_callback(&tpkey->key_gpio_cb , _tpkey_irq_callback, BIT(isr_gpio_cfg.gpion));
  84. gpio_add_callback(tpkey->gpio_dev , &tpkey->key_gpio_cb);
  85. #endif
  86. }
  87. static void _ft5x0x_reset(const struct device *dev)
  88. {
  89. const struct device *gpios_reset = device_get_binding(reset_gpio_cfg.gpio_dev_name);
  90. gpio_pin_configure(gpios_reset , reset_gpio_cfg.gpion, GPIO_OUTPUT | GPIO_PULL_UP);
  91. gpio_pin_set_raw(gpios_reset , reset_gpio_cfg.gpion, 1);
  92. k_busy_wait(50*1000);
  93. gpio_pin_set_raw(gpios_reset , reset_gpio_cfg.gpion, 0);
  94. k_busy_wait(200*1000);
  95. gpio_pin_set_raw(gpios_reset , reset_gpio_cfg.gpion, 1);
  96. k_busy_wait(100*1000);
  97. }
  98. static bool _ft5x0x_write_bytes(const struct device *dev, uint16_t reg,
  99. uint8_t *data, uint16_t len, uint8_t regLen)
  100. {
  101. int i2c_op_ret = 0;
  102. struct acts_tpkey_data *tpkey = dev->data;
  103. uint8_t retrycnt = 10;
  104. uint8_t sendBuf[140];
  105. int lenT=0;
  106. if (regLen == 2) {
  107. sendBuf[0] = reg / 0x100;
  108. sendBuf[1] = reg % 0x100;
  109. lenT = 2;
  110. } else {
  111. sendBuf[0]=reg % 0x100;
  112. lenT = 1;
  113. }
  114. memcpy(sendBuf + lenT, data, len);
  115. lenT += len;
  116. while (retrycnt--)
  117. {
  118. i2c_op_ret = i2c_write(tpkey->i2c_dev, sendBuf, lenT, TP_SLAVER_ADDR);
  119. if (0 != i2c_op_ret) {
  120. k_busy_wait(4*1000);
  121. LOG_ERR("err = %d\n", i2c_op_ret);
  122. } else {
  123. return true;
  124. }
  125. }
  126. return false;
  127. }
  128. static bool _ft5x0x_read_bytes(const struct device *dev, uint16_t reg, uint8_t *value, uint16_t len,uint8_t regLen)
  129. {
  130. int i2c_op_ret = 0;
  131. struct acts_tpkey_data *tpkey = dev->data;
  132. uint8_t retrycnt = 10;
  133. uint8_t sendBuf[4];
  134. int lenT=0;
  135. if (regLen==2) {
  136. sendBuf[0] = reg / 0x100;
  137. sendBuf[1] = reg % 0x100;
  138. lenT=2;
  139. } else {
  140. sendBuf[0] = reg % 0x100;
  141. lenT=1;
  142. }
  143. while (retrycnt--)
  144. {
  145. i2c_op_ret = i2c_write(tpkey->i2c_dev, sendBuf, lenT, TP_SLAVER_ADDR);
  146. if(0 != i2c_op_ret) {
  147. k_busy_wait(4*1000);
  148. LOG_ERR("err = %d\n", i2c_op_ret);
  149. if (retrycnt == 1)
  150. {
  151. return false;
  152. }
  153. }
  154. else
  155. {
  156. break;
  157. }
  158. }
  159. retrycnt = 10;
  160. while (retrycnt--)
  161. {
  162. i2c_op_ret = i2c_read(tpkey->i2c_dev, value, len, TP_SLAVER_ADDR);
  163. if(0 != i2c_op_ret) {
  164. k_busy_wait(4*1000);
  165. LOG_ERR("err = %d\n", i2c_op_ret);
  166. }
  167. else
  168. {
  169. return true;
  170. }
  171. }
  172. return false;
  173. }
  174. static int i2c_async_write_cb(void *cb_data, struct i2c_msg *msgs,
  175. uint8_t num_msgs, bool is_err)
  176. {
  177. if (is_err) {
  178. LOG_ERR("i2c write err\n");
  179. }
  180. return 0;
  181. }
  182. static int i2c_async_read_cb(void *cb_data, struct i2c_msg *msgs,
  183. uint8_t num_msgs, bool is_err)
  184. {
  185. #if 1
  186. if (!is_err) {
  187. struct input_value val;
  188. #if 1
  189. int temp_read = k_cycle_get_32();
  190. val.point.loc_x = (((uint16_t)(msgs->buf[1]&0x0f))<<8) | msgs->buf[2];
  191. val.point.loc_y = (((uint16_t)(msgs->buf[3]&0x0f))<<8) | msgs->buf[4];
  192. //val.point.pessure_value = msgs->buf[1];
  193. val.point.gesture = msgs->buf[0];
  194. #endif
  195. //printk("%d, %d, %d, %d. %d\n", msgs->buf[0], msgs->buf[1], msgs->buf[2], msgs->buf[3], msgs->buf[3]);
  196. if((val.point.gesture != 0) && (val.point.gesture != 0xff))
  197. {
  198. tpkey_put_point(&val, k_cycle_get_32());
  199. }
  200. #if 1
  201. LOG_DBG("finger_num = %d, gesture = %d, local:(%d,%d)\n",
  202. msgs->buf[0], val.point.gesture,val.point.loc_x, val.point.loc_y);
  203. #endif
  204. } else {
  205. LOG_ERR("i2c read err\n");
  206. }
  207. #endif
  208. return 0;
  209. }
  210. static void _ft5x0x_read_touch(struct device *i2c_dev, struct input_value *val)
  211. {
  212. #if 1
  213. static uint8_t write_cmd[1] = {0};
  214. static uint8_t read_cmd[5] = {0};
  215. int ret = 0;
  216. sys_trace_void(SYS_TRACE_ID_TP_READ);
  217. //sys_write32(0x3908, 0x40068064);
  218. //printk("\t +++++++++++GPIO57_CTL: 0x%08x\n", sys_read32(0x400680E8));
  219. //printk("\t +++++++++++GPIO58_CTL: 0x%08x\n", sys_read32(0x400680EC));
  220. //printk("\t +++++++++++GPIO24_CTL: 0x%08x\n", sys_read32(0x40068064));
  221. //printk("\t +++++++++++GPIO25_CTL: 0x%08x\n", sys_read32(0x40068068));
  222. //printk("\t +++++++++++GPIO26_CTL: 0x%08x\n", sys_read32(0x4006806C));
  223. write_cmd[0] = 0x02;
  224. ret = i2c_write_async(i2c_dev, write_cmd, 1, TP_SLAVER_ADDR, i2c_async_write_cb, NULL);
  225. if (ret == -1)
  226. goto exit;
  227. ret = i2c_read_async(i2c_dev, read_cmd, 5, TP_SLAVER_ADDR, i2c_async_read_cb, NULL);
  228. if (ret == -1)
  229. //goto exit;
  230. exit:
  231. sys_trace_end_call(SYS_TRACE_ID_TP_READ);
  232. return;
  233. #endif
  234. }
  235. static const struct acts_tpkey_config _tpkey_acts_cdata = {
  236. .poll_interval_ms = 0,
  237. .poll_total_ms = 0,
  238. };
  239. static void _tpkey_acts_enable(struct device *dev)
  240. {
  241. struct acts_tpkey_data *tpkey = dev->data;
  242. const struct acts_tpkey_config *cfg = dev->config;
  243. #if 0
  244. gpio_pin_interrupt_configure(tpkey->gpio_dev , isr_gpio_cfg.gpion, GPIO_INT_EDGE_FALLING);//GPIO_INT_DISABLE
  245. #endif
  246. LOG_DBG("enable tpkey");
  247. }
  248. static void _tpkey_acts_disable(struct device *dev)
  249. {
  250. struct acts_tpkey_data *tpkey = dev->data;
  251. const struct acts_tpkey_config *cfg = dev->config;
  252. #if 0
  253. gpio_pin_interrupt_configure(tpkey->gpio_dev , isr_gpio_cfg.gpion, GPIO_INT_DISABLE);//GPIO_INT_DISABLE
  254. #endif
  255. LOG_DBG("disable tpkey");
  256. }
  257. static void _tpkey_acts_inquiry(struct device *dev, struct input_value *val)
  258. {
  259. struct acts_tpkey_data *tpkey = dev->data;
  260. tpkey_get_last_point(val, k_cycle_get_32());
  261. }
  262. static void _tpkey_acts_register_notify(struct device *dev, input_notify_t notify)
  263. {
  264. struct acts_tpkey_data *tpkey = dev->data;
  265. LOG_DBG("register notify 0x%x", (uint32_t)notify);
  266. tpkey->notify = notify;
  267. }
  268. static void _tpkey_acts_unregister_notify(struct device *dev, input_notify_t notify)
  269. {
  270. struct acts_tpkey_data *tpkey = dev->data;
  271. LOG_DBG("unregister notify 0x%x", (uint32_t)notify);
  272. tpkey->notify = NULL;
  273. }
  274. static void tpkey_poll(struct k_timer *timer)
  275. {
  276. struct acts_tpkey_data *tpkey = &tpkey_acts_ddata;
  277. struct input_value val;
  278. _ft5x0x_read_touch(tpkey->i2c_dev, &val);
  279. }
  280. static void _tpkey_init_work(struct k_work *work)
  281. {
  282. struct acts_tpkey_data *tpkey = &tpkey_acts_ddata;
  283. struct input_value val;
  284. printk("+++++++++%s, %d++++++++++\n", __func__, __LINE__);
  285. _ft5x0x_poweron(tpkey->this_dev);
  286. _ft5x0x_irq_config(tpkey->this_dev);
  287. _ft5x0x_reset(tpkey->this_dev);
  288. _ft5x0x_read_touch(tpkey->i2c_dev, &val);
  289. k_timer_init(&tpkey->poll_timer, tpkey_poll, NULL);
  290. k_timer_user_data_set(&tpkey->poll_timer, (void *)tpkey);
  291. k_timer_start(&tpkey->poll_timer, K_MSEC(20), K_MSEC(20));
  292. //printk("\t +++++++++++GPIO24_CTL: 0x%08x\n", sys_read32(0x40068064));
  293. //printk("\t +++++++++++GPIO25_CTL: 0x%08x\n", sys_read32(0x40068068));
  294. //printk("\t +++++++++++GPIO26_CTL: 0x%08x\n", sys_read32(0x4006806C));
  295. }
  296. static int _tpkey_acts_init(const struct device *dev)
  297. {
  298. struct acts_tpkey_data *tpkey = dev->data;
  299. tpkey->this_dev = (struct device *)dev;
  300. tpkey->i2c_dev = (struct device *)device_get_binding(CONFIG_TPKEY_I2C_NAME);
  301. if (!tpkey->i2c_dev) {
  302. printk("can not access right i2c device\n");
  303. return -1;
  304. }
  305. k_work_init(&tpkey->init_timer, _tpkey_init_work);
  306. k_work_submit(&tpkey->init_timer);
  307. return 0;
  308. }
  309. void tpkey_acts_dump(void)
  310. {
  311. }
  312. static const struct input_dev_driver_api _tpkey_acts_driver_api = {
  313. .enable = _tpkey_acts_enable,
  314. .disable = _tpkey_acts_disable,
  315. .inquiry = _tpkey_acts_inquiry,
  316. .register_notify = _tpkey_acts_register_notify,
  317. .unregister_notify = _tpkey_acts_unregister_notify,
  318. };
  319. #if IS_ENABLED(CONFIG_TPKEY)
  320. DEVICE_DEFINE(tpkey, CONFIG_TPKEY_DEV_NAME, _tpkey_acts_init,
  321. NULL, &tpkey_acts_ddata, &_tpkey_acts_cdata, POST_KERNEL,
  322. 60, &_tpkey_acts_driver_api);
  323. #endif