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