focaltech_gesture.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. *
  3. * FocalTech TouchScreen driver.
  4. *
  5. * Copyright (c) 2012-2019, Focaltech Ltd. All rights reserved.
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. /*****************************************************************************
  18. *
  19. * File Name: focaltech_gestrue.c
  20. *
  21. * Author: Focaltech Driver Team
  22. *
  23. * Created: 2016-08-08
  24. *
  25. * Abstract:
  26. *
  27. * Reference:
  28. *
  29. *****************************************************************************/
  30. /*****************************************************************************
  31. * 1.Included header files
  32. *****************************************************************************/
  33. #include "focaltech_core.h"
  34. /******************************************************************************
  35. * Private constant and macro definitions using #define
  36. *****************************************************************************/
  37. #define KEY_GESTURE_U KEY_U
  38. #define KEY_GESTURE_UP KEY_UP
  39. #define KEY_GESTURE_DOWN KEY_DOWN
  40. #define KEY_GESTURE_LEFT KEY_LEFT
  41. #define KEY_GESTURE_RIGHT KEY_RIGHT
  42. #define KEY_GESTURE_O KEY_O
  43. #define KEY_GESTURE_E KEY_E
  44. #define KEY_GESTURE_M KEY_M
  45. #define KEY_GESTURE_L KEY_L
  46. #define KEY_GESTURE_W KEY_W
  47. #define KEY_GESTURE_S KEY_S
  48. #define KEY_GESTURE_V KEY_V
  49. #define KEY_GESTURE_C KEY_C
  50. #define KEY_GESTURE_Z KEY_Z
  51. #define GESTURE_LEFT 0x20
  52. #define GESTURE_RIGHT 0x21
  53. #define GESTURE_UP 0x22
  54. #define GESTURE_DOWN 0x23
  55. #define GESTURE_DOUBLECLICK 0x24
  56. #define GESTURE_O 0x30
  57. #define GESTURE_W 0x31
  58. #define GESTURE_M 0x32
  59. #define GESTURE_E 0x33
  60. #define GESTURE_L 0x44
  61. #define GESTURE_S 0x46
  62. #define GESTURE_V 0x54
  63. #define GESTURE_Z 0x41
  64. #define GESTURE_C 0x34
  65. /*****************************************************************************
  66. * Private enumerations, structures and unions using typedef
  67. *****************************************************************************/
  68. /*
  69. * gesture_id - mean which gesture is recognised
  70. * point_num - points number of this gesture
  71. * coordinate_x - All gesture point x coordinate
  72. * coordinate_y - All gesture point y coordinate
  73. * mode - gesture enable/disable, need enable by host
  74. * - 1:enable gesture function(default) 0:disable
  75. * active - gesture work flag,
  76. * always set 1 when suspend, set 0 when resume
  77. */
  78. struct fts_gesture_st {
  79. u8 gesture_id;
  80. u8 point_num;
  81. u16 coordinate_x[FTS_GESTURE_POINTS_MAX];
  82. u16 coordinate_y[FTS_GESTURE_POINTS_MAX];
  83. };
  84. /*****************************************************************************
  85. * Static variables
  86. *****************************************************************************/
  87. static struct fts_gesture_st fts_gesture_data;
  88. /*****************************************************************************
  89. * Global variable or extern global variabls/functions
  90. *****************************************************************************/
  91. /*****************************************************************************
  92. * Static function prototypes
  93. *****************************************************************************/
  94. static ssize_t fts_gesture_show(
  95. struct device *dev, struct device_attribute *attr, char *buf)
  96. {
  97. int count = 0;
  98. u8 val = 0;
  99. struct fts_ts_data *ts_data = fts_data;
  100. mutex_lock(&ts_data->input_dev->mutex);
  101. fts_read_reg(FTS_REG_GESTURE_EN, &val);
  102. count = snprintf(buf, PAGE_SIZE, "Gesture Mode:%s\n",
  103. ts_data->gesture_mode ? "On" : "Off");
  104. count += snprintf(buf + count, PAGE_SIZE, "Reg(0xD0)=%d\n", val);
  105. mutex_unlock(&ts_data->input_dev->mutex);
  106. return count;
  107. }
  108. static ssize_t fts_gesture_store(
  109. struct device *dev,
  110. struct device_attribute *attr, const char *buf, size_t count)
  111. {
  112. struct fts_ts_data *ts_data = fts_data;
  113. mutex_lock(&ts_data->input_dev->mutex);
  114. if (FTS_SYSFS_ECHO_ON(buf)) {
  115. FTS_DEBUG("enable gesture");
  116. ts_data->gesture_mode = ENABLE;
  117. } else if (FTS_SYSFS_ECHO_OFF(buf)) {
  118. FTS_DEBUG("disable gesture");
  119. ts_data->gesture_mode = DISABLE;
  120. }
  121. mutex_unlock(&ts_data->input_dev->mutex);
  122. return count;
  123. }
  124. static ssize_t fts_gesture_buf_show(
  125. struct device *dev, struct device_attribute *attr, char *buf)
  126. {
  127. int count = 0;
  128. int i = 0;
  129. struct input_dev *input_dev = fts_data->input_dev;
  130. struct fts_gesture_st *gesture = &fts_gesture_data;
  131. mutex_lock(&input_dev->mutex);
  132. count = snprintf(buf, PAGE_SIZE, "Gesture ID:%d\n", gesture->gesture_id);
  133. count += snprintf(buf + count, PAGE_SIZE, "Gesture PointNum:%d\n",
  134. gesture->point_num);
  135. count += snprintf(buf + count, PAGE_SIZE, "Gesture Points Buffer:\n");
  136. /* save point data,max:6 */
  137. for (i = 0; i < FTS_GESTURE_POINTS_MAX; i++) {
  138. count += snprintf(buf + count, PAGE_SIZE, "%3d(%4d,%4d) ", i,
  139. gesture->coordinate_x[i], gesture->coordinate_y[i]);
  140. if ((i + 1) % 4 == 0)
  141. count += snprintf(buf + count, PAGE_SIZE, "\n");
  142. }
  143. count += snprintf(buf + count, PAGE_SIZE, "\n");
  144. mutex_unlock(&input_dev->mutex);
  145. return count;
  146. }
  147. static ssize_t fts_gesture_buf_store(
  148. struct device *dev,
  149. struct device_attribute *attr, const char *buf, size_t count)
  150. {
  151. return -EPERM;
  152. }
  153. /* sysfs gesture node
  154. * read example: cat fts_gesture_mode ---read gesture mode
  155. * write example:echo 1 > fts_gesture_mode --- write gesture mode to 1
  156. *
  157. */
  158. static DEVICE_ATTR(fts_gesture_mode, S_IRUGO | S_IWUSR, fts_gesture_show,
  159. fts_gesture_store);
  160. /*
  161. * read example: cat fts_gesture_buf --- read gesture buf
  162. */
  163. static DEVICE_ATTR(fts_gesture_buf, S_IRUGO | S_IWUSR,
  164. fts_gesture_buf_show, fts_gesture_buf_store);
  165. static struct attribute *fts_gesture_mode_attrs[] = {
  166. &dev_attr_fts_gesture_mode.attr,
  167. &dev_attr_fts_gesture_buf.attr,
  168. NULL,
  169. };
  170. static struct attribute_group fts_gesture_group = {
  171. .attrs = fts_gesture_mode_attrs,
  172. };
  173. static int fts_create_gesture_sysfs(struct device *dev)
  174. {
  175. int ret = 0;
  176. ret = sysfs_create_group(&dev->kobj, &fts_gesture_group);
  177. if (ret) {
  178. FTS_ERROR("gesture sys node create fail");
  179. sysfs_remove_group(&dev->kobj, &fts_gesture_group);
  180. return ret;
  181. }
  182. return 0;
  183. }
  184. static void fts_gesture_report(struct input_dev *input_dev, int gesture_id)
  185. {
  186. int gesture;
  187. FTS_DEBUG("gesture_id:0x%x", gesture_id);
  188. switch (gesture_id) {
  189. case GESTURE_LEFT:
  190. gesture = KEY_GESTURE_LEFT;
  191. break;
  192. case GESTURE_RIGHT:
  193. gesture = KEY_GESTURE_RIGHT;
  194. break;
  195. case GESTURE_UP:
  196. gesture = KEY_GESTURE_UP;
  197. break;
  198. case GESTURE_DOWN:
  199. gesture = KEY_GESTURE_DOWN;
  200. break;
  201. case GESTURE_DOUBLECLICK:
  202. gesture = KEY_POWER;
  203. break;
  204. case GESTURE_O:
  205. gesture = KEY_GESTURE_O;
  206. break;
  207. case GESTURE_W:
  208. gesture = KEY_GESTURE_W;
  209. break;
  210. case GESTURE_M:
  211. gesture = KEY_GESTURE_M;
  212. break;
  213. case GESTURE_E:
  214. gesture = KEY_GESTURE_E;
  215. break;
  216. case GESTURE_L:
  217. gesture = KEY_GESTURE_L;
  218. break;
  219. case GESTURE_S:
  220. gesture = KEY_GESTURE_S;
  221. break;
  222. case GESTURE_V:
  223. gesture = KEY_GESTURE_V;
  224. break;
  225. case GESTURE_Z:
  226. gesture = KEY_GESTURE_Z;
  227. break;
  228. case GESTURE_C:
  229. gesture = KEY_GESTURE_C;
  230. break;
  231. default:
  232. gesture = -1;
  233. break;
  234. }
  235. /* report event key */
  236. if (gesture != -1) {
  237. FTS_DEBUG("Gesture Code=%d", gesture);
  238. input_report_key(input_dev, gesture, 1);
  239. input_sync(input_dev);
  240. input_report_key(input_dev, gesture, 0);
  241. input_sync(input_dev);
  242. }
  243. }
  244. /*****************************************************************************
  245. * Name: fts_gesture_readdata
  246. * Brief: Read information about gesture: enable flag/gesture points..., if ges-
  247. * ture enable, save gesture points' information, and report to OS.
  248. * It will be called this function every intrrupt when FTS_GESTURE_EN = 1
  249. *
  250. * gesture data length: 1(enable) + 1(reserve) + 2(header) + 6 * 4
  251. * Input: ts_data - global struct data
  252. * data - gesture data buffer if non-flash, else NULL
  253. * Output:
  254. * Return: 0 - read gesture data successfully, the report data is gesture data
  255. * 1 - tp not in suspend/gesture not enable in TP FW
  256. * -Exx - error
  257. *****************************************************************************/
  258. int fts_gesture_readdata(struct fts_ts_data *ts_data, u8 *data)
  259. {
  260. int ret = 0;
  261. int i = 0;
  262. int index = 0;
  263. u8 buf[FTS_GESTURE_DATA_LEN] = { 0 };
  264. struct input_dev *input_dev = ts_data->input_dev;
  265. struct fts_gesture_st *gesture = &fts_gesture_data;
  266. if (!ts_data->suspended || !ts_data->gesture_mode) {
  267. return 1;
  268. }
  269. ret = fts_read_reg(FTS_REG_GESTURE_EN, &buf[0]);
  270. if ((ret < 0) || (buf[0] != ENABLE)) {
  271. FTS_DEBUG("gesture not enable in fw, don't process gesture");
  272. return 1;
  273. }
  274. buf[2] = FTS_REG_GESTURE_OUTPUT_ADDRESS;
  275. ret = fts_read(&buf[2], 1, &buf[2], FTS_GESTURE_DATA_LEN - 2);
  276. if (ret < 0) {
  277. FTS_ERROR("read gesture header data fail");
  278. return ret;
  279. }
  280. /* init variable before read gesture point */
  281. memset(gesture->coordinate_x, 0, FTS_GESTURE_POINTS_MAX * sizeof(u16));
  282. memset(gesture->coordinate_y, 0, FTS_GESTURE_POINTS_MAX * sizeof(u16));
  283. gesture->gesture_id = buf[2];
  284. gesture->point_num = buf[3];
  285. FTS_DEBUG("gesture_id=%d, point_num=%d",
  286. gesture->gesture_id, gesture->point_num);
  287. /* save point data,max:6 */
  288. for (i = 0; i < FTS_GESTURE_POINTS_MAX; i++) {
  289. index = 4 * i + 4;
  290. gesture->coordinate_x[i] = (u16)(((buf[0 + index] & 0x0F) << 8)
  291. + buf[1 + index]);
  292. gesture->coordinate_y[i] = (u16)(((buf[2 + index] & 0x0F) << 8)
  293. + buf[3 + index]);
  294. }
  295. /* report gesture to OS */
  296. fts_gesture_report(input_dev, gesture->gesture_id);
  297. return 0;
  298. }
  299. void fts_gesture_recovery(struct fts_ts_data *ts_data)
  300. {
  301. if (ts_data->gesture_mode && ts_data->suspended) {
  302. FTS_DEBUG("gesture recovery...");
  303. fts_write_reg(0xD1, 0xFF);
  304. fts_write_reg(0xD2, 0xFF);
  305. fts_write_reg(0xD5, 0xFF);
  306. fts_write_reg(0xD6, 0xFF);
  307. fts_write_reg(0xD7, 0xFF);
  308. fts_write_reg(0xD8, 0xFF);
  309. fts_write_reg(FTS_REG_GESTURE_EN, ENABLE);
  310. }
  311. }
  312. int fts_gesture_suspend(struct fts_ts_data *ts_data)
  313. {
  314. int i = 0;
  315. u8 state = 0xFF;
  316. FTS_FUNC_ENTER();
  317. if (enable_irq_wake(ts_data->irq)) {
  318. FTS_DEBUG("enable_irq_wake(irq:%d) fail", ts_data->irq);
  319. }
  320. for (i = 0; i < 5; i++) {
  321. fts_write_reg(0xD1, 0xFF);
  322. fts_write_reg(0xD2, 0xFF);
  323. fts_write_reg(0xD5, 0xFF);
  324. fts_write_reg(0xD6, 0xFF);
  325. fts_write_reg(0xD7, 0xFF);
  326. fts_write_reg(0xD8, 0xFF);
  327. fts_write_reg(FTS_REG_GESTURE_EN, ENABLE);
  328. msleep(1);
  329. fts_read_reg(FTS_REG_GESTURE_EN, &state);
  330. if (state == ENABLE)
  331. break;
  332. }
  333. if (i >= 5)
  334. FTS_ERROR("make IC enter into gesture(suspend) fail,state:%x", state);
  335. else
  336. FTS_INFO("Enter into gesture(suspend) successfully");
  337. FTS_FUNC_EXIT();
  338. return 0;
  339. }
  340. int fts_gesture_resume(struct fts_ts_data *ts_data)
  341. {
  342. int i = 0;
  343. u8 state = 0xFF;
  344. FTS_FUNC_ENTER();
  345. if (disable_irq_wake(ts_data->irq)) {
  346. FTS_DEBUG("disable_irq_wake(irq:%d) fail", ts_data->irq);
  347. }
  348. for (i = 0; i < 5; i++) {
  349. fts_write_reg(FTS_REG_GESTURE_EN, DISABLE);
  350. msleep(1);
  351. fts_read_reg(FTS_REG_GESTURE_EN, &state);
  352. if (state == DISABLE)
  353. break;
  354. }
  355. if (i >= 5)
  356. FTS_ERROR("make IC exit gesture(resume) fail,state:%x", state);
  357. else
  358. FTS_INFO("resume from gesture successfully");
  359. FTS_FUNC_EXIT();
  360. return 0;
  361. }
  362. int fts_gesture_init(struct fts_ts_data *ts_data)
  363. {
  364. struct input_dev *input_dev = ts_data->input_dev;
  365. FTS_FUNC_ENTER();
  366. input_set_capability(input_dev, EV_KEY, KEY_POWER);
  367. input_set_capability(input_dev, EV_KEY, KEY_GESTURE_U);
  368. input_set_capability(input_dev, EV_KEY, KEY_GESTURE_UP);
  369. input_set_capability(input_dev, EV_KEY, KEY_GESTURE_DOWN);
  370. input_set_capability(input_dev, EV_KEY, KEY_GESTURE_LEFT);
  371. input_set_capability(input_dev, EV_KEY, KEY_GESTURE_RIGHT);
  372. input_set_capability(input_dev, EV_KEY, KEY_GESTURE_O);
  373. input_set_capability(input_dev, EV_KEY, KEY_GESTURE_E);
  374. input_set_capability(input_dev, EV_KEY, KEY_GESTURE_M);
  375. input_set_capability(input_dev, EV_KEY, KEY_GESTURE_L);
  376. input_set_capability(input_dev, EV_KEY, KEY_GESTURE_W);
  377. input_set_capability(input_dev, EV_KEY, KEY_GESTURE_S);
  378. input_set_capability(input_dev, EV_KEY, KEY_GESTURE_V);
  379. input_set_capability(input_dev, EV_KEY, KEY_GESTURE_Z);
  380. input_set_capability(input_dev, EV_KEY, KEY_GESTURE_C);
  381. __set_bit(KEY_GESTURE_RIGHT, input_dev->keybit);
  382. __set_bit(KEY_GESTURE_LEFT, input_dev->keybit);
  383. __set_bit(KEY_GESTURE_UP, input_dev->keybit);
  384. __set_bit(KEY_GESTURE_DOWN, input_dev->keybit);
  385. __set_bit(KEY_GESTURE_U, input_dev->keybit);
  386. __set_bit(KEY_GESTURE_O, input_dev->keybit);
  387. __set_bit(KEY_GESTURE_E, input_dev->keybit);
  388. __set_bit(KEY_GESTURE_M, input_dev->keybit);
  389. __set_bit(KEY_GESTURE_W, input_dev->keybit);
  390. __set_bit(KEY_GESTURE_L, input_dev->keybit);
  391. __set_bit(KEY_GESTURE_S, input_dev->keybit);
  392. __set_bit(KEY_GESTURE_V, input_dev->keybit);
  393. __set_bit(KEY_GESTURE_C, input_dev->keybit);
  394. __set_bit(KEY_GESTURE_Z, input_dev->keybit);
  395. fts_create_gesture_sysfs(ts_data->dev);
  396. memset(&fts_gesture_data, 0, sizeof(struct fts_gesture_st));
  397. ts_data->gesture_mode = FTS_GESTURE_EN;
  398. FTS_FUNC_EXIT();
  399. return 0;
  400. }
  401. int fts_gesture_exit(struct fts_ts_data *ts_data)
  402. {
  403. FTS_FUNC_ENTER();
  404. sysfs_remove_group(&ts_data->dev->kobj, &fts_gesture_group);
  405. FTS_FUNC_EXIT();
  406. return 0;
  407. }