hc
2023-07-06 0d92c6001e626cf3cfa86b826ccc10a16115901e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*
 * cyttsp5_proximity.c
 * Parade TrueTouch(TM) Standard Product V5 Proximity Module.
 * For use with Parade touchscreen controllers.
 * Supported parts include:
 * CYTMA5XX
 * CYTMA448
 * CYTMA445A
 * CYTT21XXX
 * CYTT31XXX
 *
 * Copyright (C) 2015 Parade Technologies
 * Copyright (C) 2013-2015 Cypress Semiconductor
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, and only version 2, as published by the
 * Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Contact Parade Technologies at www.paradetech.com <ttdrivers@paradetech.com>
 *
 */
 
#include "cyttsp5_regs.h"
 
#define CYTTSP5_PROXIMITY_NAME "cyttsp5_proximity"
 
/* Timeout value in ms. */
#define CYTTSP5_PROXIMITY_REQUEST_EXCLUSIVE_TIMEOUT        1000
 
#define CYTTSP5_PROXIMITY_ON 0
#define CYTTSP5_PROXIMITY_OFF 1
 
static inline struct cyttsp5_proximity_data *get_prox_data(struct device *dev)
{
   struct cyttsp5_core_data *cd = dev_get_drvdata(dev);
 
   return &cd->pd;
}
 
static void cyttsp5_report_proximity(struct cyttsp5_proximity_data *pd,
   bool on)
{
   int val = on ? CYTTSP5_PROXIMITY_ON : CYTTSP5_PROXIMITY_OFF;
 
   input_report_abs(pd->input, ABS_DISTANCE, val);
   input_sync(pd->input);
}
 
static void cyttsp5_get_touch_axis(struct cyttsp5_proximity_data *pd,
   int *axis, int size, int max, u8 *xy_data, int bofs)
{
   int nbyte;
   int next;
 
   for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
       parade_debug(pd->dev, DEBUG_LEVEL_2,
           "%s: *axis=%02X(%d) size=%d max=%08X xy_data=%p xy_data[%d]=%02X(%d) bofs=%d\n",
           __func__, *axis, *axis, size, max, xy_data, next,
           xy_data[next], xy_data[next], bofs);
       *axis = *axis + ((xy_data[next] >> bofs) << (nbyte * 8));
       next++;
   }
 
   *axis &= max - 1;
 
   parade_debug(pd->dev, DEBUG_LEVEL_2,
       "%s: *axis=%02X(%d) size=%d max=%08X xy_data=%p xy_data[%d]=%02X(%d)\n",
       __func__, *axis, *axis, size, max, xy_data, next,
       xy_data[next], xy_data[next]);
}
 
static void cyttsp5_get_touch_hdr(struct cyttsp5_proximity_data *pd,
   struct cyttsp5_touch *touch, u8 *xy_mode)
{
   struct device *dev = pd->dev;
   struct cyttsp5_sysinfo *si = pd->si;
   enum cyttsp5_tch_hdr hdr;
 
   for (hdr = CY_TCH_TIME; hdr < CY_TCH_NUM_HDR; hdr++) {
       if (!si->tch_hdr[hdr].report)
           continue;
       cyttsp5_get_touch_axis(pd, &touch->hdr[hdr],
           si->tch_hdr[hdr].size,
           si->tch_hdr[hdr].max,
           xy_mode + si->tch_hdr[hdr].ofs,
           si->tch_hdr[hdr].bofs);
       parade_debug(dev, DEBUG_LEVEL_2, "%s: get %s=%04X(%d)\n",
           __func__, cyttsp5_tch_hdr_string[hdr],
           touch->hdr[hdr], touch->hdr[hdr]);
   }
}
 
static void cyttsp5_get_touch(struct cyttsp5_proximity_data *pd,
   struct cyttsp5_touch *touch, u8 *xy_data)
{
   struct device *dev = pd->dev;
   struct cyttsp5_sysinfo *si = pd->si;
   enum cyttsp5_tch_abs abs;
 
   for (abs = CY_TCH_X; abs < CY_TCH_NUM_ABS; abs++) {
       if (!si->tch_abs[abs].report)
           continue;
       cyttsp5_get_touch_axis(pd, &touch->abs[abs],
           si->tch_abs[abs].size,
           si->tch_abs[abs].max,
           xy_data + si->tch_abs[abs].ofs,
           si->tch_abs[abs].bofs);
       parade_debug(dev, DEBUG_LEVEL_2, "%s: get %s=%04X(%d)\n",
           __func__, cyttsp5_tch_abs_string[abs],
           touch->abs[abs], touch->abs[abs]);
   }
 
   parade_debug(dev, DEBUG_LEVEL_2, "%s: x=%04X(%d) y=%04X(%d)\n",
       __func__, touch->abs[CY_TCH_X], touch->abs[CY_TCH_X],
       touch->abs[CY_TCH_Y], touch->abs[CY_TCH_Y]);
}
 
static void cyttsp5_get_proximity_touch(struct cyttsp5_proximity_data *pd,
       struct cyttsp5_touch *tch, int num_cur_tch)
{
   struct cyttsp5_sysinfo *si = pd->si;
   int i;
 
   for (i = 0; i < num_cur_tch; i++) {
       cyttsp5_get_touch(pd, tch, si->xy_data +
           (i * si->desc.tch_record_size));
 
       /* Check for proximity event */
       if (tch->abs[CY_TCH_O] == CY_OBJ_PROXIMITY) {
           if (tch->abs[CY_TCH_E] == CY_EV_TOUCHDOWN)
               cyttsp5_report_proximity(pd, true);
           else if (tch->abs[CY_TCH_E] == CY_EV_LIFTOFF)
               cyttsp5_report_proximity(pd, false);
           break;
       }
   }
}
 
/* read xy_data for all current touches */
static int cyttsp5_xy_worker(struct cyttsp5_proximity_data *pd)
{
   struct device *dev = pd->dev;
   struct cyttsp5_sysinfo *si = pd->si;
   struct cyttsp5_touch tch;
   u8 num_cur_tch;
 
   cyttsp5_get_touch_hdr(pd, &tch, si->xy_mode + 3);
 
   num_cur_tch = tch.hdr[CY_TCH_NUM];
   if (num_cur_tch > si->sensing_conf_data.max_tch) {
       dev_err(dev, "%s: Num touch err detected (n=%d)\n",
           __func__, num_cur_tch);
       num_cur_tch = si->sensing_conf_data.max_tch;
   }
 
   if (tch.hdr[CY_TCH_LO])
       parade_debug(dev, DEBUG_LEVEL_1, "%s: Large area detected\n",
       __func__);
 
   /* extract xy_data for all currently reported touches */
   parade_debug(dev, DEBUG_LEVEL_2, "%s: extract data num_cur_rec=%d\n",
       __func__, num_cur_tch);
   if (num_cur_tch)
       cyttsp5_get_proximity_touch(pd, &tch, num_cur_tch);
   else
       cyttsp5_report_proximity(pd, false);
 
   return 0;
}
 
static int cyttsp5_proximity_attention(struct device *dev)
{
   struct cyttsp5_proximity_data *pd = get_prox_data(dev);
   int rc = 0;
 
   if (pd->si->xy_mode[2] != pd->si->desc.tch_report_id)
       return 0;
 
   mutex_lock(&pd->prox_lock);
   rc = cyttsp5_xy_worker(pd);
   mutex_unlock(&pd->prox_lock);
   if (rc < 0)
       dev_err(dev, "%s: xy_worker error r=%d\n", __func__, rc);
 
   return rc;
}
 
static int cyttsp5_startup_attention(struct device *dev)
{
   struct cyttsp5_proximity_data *pd = get_prox_data(dev);
 
   mutex_lock(&pd->prox_lock);
   cyttsp5_report_proximity(pd, false);
   mutex_unlock(&pd->prox_lock);
 
   return 0;
}
 
static int _cyttsp5_set_proximity_via_touchmode_enabled(
       struct cyttsp5_proximity_data *pd, bool enable)
{
   struct device *dev = pd->dev;
   u32 touchmode_enabled;
   int rc;
 
   rc = cyttsp5_request_nonhid_get_param(dev, 0,
           CY_RAM_ID_TOUCHMODE_ENABLED, &touchmode_enabled);
   if (rc)
       return rc;
 
   if (enable)
       touchmode_enabled |= 0x80;
   else
       touchmode_enabled &= 0x7F;
 
   rc = cyttsp5_request_nonhid_set_param(dev, 0,
           CY_RAM_ID_TOUCHMODE_ENABLED, touchmode_enabled,
           CY_RAM_ID_TOUCHMODE_ENABLED_SIZE);
 
   return rc;
}
 
static int _cyttsp5_set_proximity_via_proximity_enable(
       struct cyttsp5_proximity_data *pd, bool enable)
{
   struct device *dev = pd->dev;
   u32 proximity_enable;
   int rc;
 
   rc = cyttsp5_request_nonhid_get_param(dev, 0,
           CY_RAM_ID_PROXIMITY_ENABLE, &proximity_enable);
   if (rc)
       return rc;
 
   if (enable)
       proximity_enable |= 0x01;
   else
       proximity_enable &= 0xFE;
 
   rc = cyttsp5_request_nonhid_set_param(dev, 0,
           CY_RAM_ID_PROXIMITY_ENABLE, proximity_enable,
           CY_RAM_ID_PROXIMITY_ENABLE_SIZE);
 
   return rc;
}
 
static int _cyttsp5_set_proximity(struct cyttsp5_proximity_data *pd,
       bool enable)
{
   if (!IS_PIP_VER_GE(pd->si, 1, 4))
       return _cyttsp5_set_proximity_via_touchmode_enabled(pd,
               enable);
 
   return _cyttsp5_set_proximity_via_proximity_enable(pd, enable);
}
 
static int _cyttsp5_proximity_enable(struct cyttsp5_proximity_data *pd)
{
   struct device *dev = pd->dev;
   int rc = 0;
 
   pm_runtime_get_sync(dev);
 
   rc = cyttsp5_request_exclusive(dev,
           CYTTSP5_PROXIMITY_REQUEST_EXCLUSIVE_TIMEOUT);
   if (rc < 0) {
       dev_err(dev, "%s: Error on request exclusive r=%d\n",
               __func__, rc);
       goto exit;
   }
 
   rc = _cyttsp5_set_proximity(pd, true);
   if (rc < 0) {
       dev_err(dev, "%s: Error on request enable proximity scantype r=%d\n",
               __func__, rc);
       goto exit_release;
   }
 
   parade_debug(dev, DEBUG_LEVEL_2, "%s: setup subscriptions\n", __func__);
 
   /* set up touch call back */
   _cyttsp5_subscribe_attention(dev, CY_ATTEN_IRQ, CYTTSP5_PROXIMITY_NAME,
       cyttsp5_proximity_attention, CY_MODE_OPERATIONAL);
 
   /* set up startup call back */
   _cyttsp5_subscribe_attention(dev, CY_ATTEN_STARTUP,
       CYTTSP5_PROXIMITY_NAME, cyttsp5_startup_attention, 0);
 
exit_release:
   cyttsp5_release_exclusive(dev);
exit:
   return rc;
}
 
static int _cyttsp5_proximity_disable(struct cyttsp5_proximity_data *pd,
       bool force)
{
   struct device *dev = pd->dev;
   int rc = 0;
 
   rc = cyttsp5_request_exclusive(dev,
           CYTTSP5_PROXIMITY_REQUEST_EXCLUSIVE_TIMEOUT);
   if (rc < 0) {
       dev_err(dev, "%s: Error on request exclusive r=%d\n",
               __func__, rc);
       goto exit;
   }
 
   rc = _cyttsp5_set_proximity(pd, false);
   if (rc < 0) {
       dev_err(dev, "%s: Error on request disable proximity scan r=%d\n",
               __func__, rc);
       goto exit_release;
   }
 
exit_release:
   cyttsp5_release_exclusive(dev);
 
exit:
   if (!rc || force) {
       _cyttsp5_unsubscribe_attention(dev, CY_ATTEN_IRQ,
           CYTTSP5_PROXIMITY_NAME, cyttsp5_proximity_attention,
           CY_MODE_OPERATIONAL);
 
       _cyttsp5_unsubscribe_attention(dev, CY_ATTEN_STARTUP,
           CYTTSP5_PROXIMITY_NAME, cyttsp5_startup_attention, 0);
   }
 
   pm_runtime_put(dev);
 
   return rc;
}
 
static ssize_t cyttsp5_proximity_enable_show(struct device *dev,
       struct device_attribute *attr, char *buf)
{
   struct cyttsp5_proximity_data *pd = get_prox_data(dev);
   int val = 0;
 
   mutex_lock(&pd->sysfs_lock);
   val = pd->enable_count;
   mutex_unlock(&pd->sysfs_lock);
 
   return scnprintf(buf, CY_MAX_PRBUF_SIZE, "%d\n", val);
}
 
static ssize_t cyttsp5_proximity_enable_store(struct device *dev,
       struct device_attribute *attr, const char *buf, size_t size)
{
   struct cyttsp5_proximity_data *pd = get_prox_data(dev);
   unsigned long value;
   int rc;
 
   rc = kstrtoul(buf, 10, &value);
   if (rc < 0 || (value != 0 && value != 1)) {
       dev_err(dev, "%s: Invalid value\n", __func__);
       return -EINVAL;
   }
 
   mutex_lock(&pd->sysfs_lock);
   if (value) {
       if (pd->enable_count++) {
           parade_debug(dev, DEBUG_LEVEL_2, "%s: '%s' already enabled\n",
               __func__, pd->input->name);
       } else {
           rc = _cyttsp5_proximity_enable(pd);
           if (rc)
               pd->enable_count--;
       }
   } else {
       if (--pd->enable_count) {
           if (pd->enable_count < 0) {
               dev_err(dev, "%s: '%s' unbalanced disable\n",
                   __func__, pd->input->name);
               pd->enable_count = 0;
           }
       } else {
           rc = _cyttsp5_proximity_disable(pd, false);
           if (rc)
               pd->enable_count++;
       }
   }
   mutex_unlock(&pd->sysfs_lock);
 
   if (rc)
       return rc;
 
   return size;
}
 
static DEVICE_ATTR(prox_enable, S_IRUSR | S_IWUSR,
       cyttsp5_proximity_enable_show,
       cyttsp5_proximity_enable_store);
 
static int cyttsp5_setup_input_device_and_sysfs(struct device *dev)
{
   struct cyttsp5_proximity_data *pd = get_prox_data(dev);
   int signal = CY_IGNORE_VALUE;
   int i;
   int rc;
 
   rc = device_create_file(dev, &dev_attr_prox_enable);
   if (rc) {
       dev_err(dev, "%s: Error, could not create enable\n",
               __func__);
       goto exit;
   }
 
   parade_debug(dev, DEBUG_LEVEL_2, "%s: Initialize event signals\n",
               __func__);
 
   __set_bit(EV_ABS, pd->input->evbit);
 
   /* set event signal capabilities */
   for (i = 0; i < NUM_SIGNALS(pd->pdata->frmwrk); i++) {
       signal = PARAM_SIGNAL(pd->pdata->frmwrk, i);
       if (signal != CY_IGNORE_VALUE) {
           input_set_abs_params(pd->input, signal,
               PARAM_MIN(pd->pdata->frmwrk, i),
               PARAM_MAX(pd->pdata->frmwrk, i),
               PARAM_FUZZ(pd->pdata->frmwrk, i),
               PARAM_FLAT(pd->pdata->frmwrk, i));
       }
   }
 
   rc = input_register_device(pd->input);
   if (rc) {
       dev_err(dev, "%s: Error, failed register input device r=%d\n",
           __func__, rc);
       goto unregister_enable;
   }
 
   pd->input_device_registered = true;
   return rc;
 
unregister_enable:
   device_remove_file(dev, &dev_attr_prox_enable);
exit:
   return rc;
}
 
static int cyttsp5_setup_input_attention(struct device *dev)
{
   struct cyttsp5_proximity_data *pd = get_prox_data(dev);
   int rc;
 
   pd->si = _cyttsp5_request_sysinfo(dev);
   if (!pd->si)
       return -EINVAL;
 
   rc = cyttsp5_setup_input_device_and_sysfs(dev);
   if (!rc)
       rc = _cyttsp5_set_proximity(pd, false);
 
   _cyttsp5_unsubscribe_attention(dev, CY_ATTEN_STARTUP,
       CYTTSP5_PROXIMITY_NAME, cyttsp5_setup_input_attention, 0);
 
   return rc;
}
 
int cyttsp5_proximity_probe(struct device *dev)
{
   struct cyttsp5_core_data *cd = dev_get_drvdata(dev);
   struct cyttsp5_proximity_data *pd = &cd->pd;
   struct cyttsp5_platform_data *pdata = dev_get_platdata(dev);
   struct cyttsp5_proximity_platform_data *prox_pdata;
   int rc = 0;
 
   if (!pdata ||  !pdata->prox_pdata) {
       dev_err(dev, "%s: Missing platform data\n", __func__);
       rc = -ENODEV;
       goto error_no_pdata;
   }
   prox_pdata = pdata->prox_pdata;
 
   mutex_init(&pd->prox_lock);
   mutex_init(&pd->sysfs_lock);
   pd->dev = dev;
   pd->pdata = prox_pdata;
 
   /* Create the input device and register it. */
   parade_debug(dev, DEBUG_LEVEL_2,
       "%s: Create the input device and register it\n", __func__);
   pd->input = input_allocate_device();
   if (!pd->input) {
       dev_err(dev, "%s: Error, failed to allocate input device\n",
           __func__);
       rc = -ENODEV;
       goto error_alloc_failed;
   }
 
   if (pd->pdata->inp_dev_name)
       pd->input->name = pd->pdata->inp_dev_name;
   else
       pd->input->name = CYTTSP5_PROXIMITY_NAME;
   scnprintf(pd->phys, sizeof(pd->phys), "%s/input%d", dev_name(dev),
           cd->phys_num++);
   pd->input->phys = pd->phys;
   pd->input->dev.parent = pd->dev;
   input_set_drvdata(pd->input, pd);
 
   /* get sysinfo */
   pd->si = _cyttsp5_request_sysinfo(dev);
 
   if (pd->si) {
       rc = cyttsp5_setup_input_device_and_sysfs(dev);
       if (rc)
           goto error_init_input;
 
       rc = _cyttsp5_set_proximity(pd, false);
   } else {
       dev_err(dev, "%s: Fail get sysinfo pointer from core p=%p\n",
           __func__, pd->si);
       _cyttsp5_subscribe_attention(dev, CY_ATTEN_STARTUP,
           CYTTSP5_PROXIMITY_NAME, cyttsp5_setup_input_attention,
           0);
   }
 
   return 0;
 
error_init_input:
   input_free_device(pd->input);
error_alloc_failed:
error_no_pdata:
   dev_err(dev, "%s failed.\n", __func__);
   return rc;
}
 
int cyttsp5_proximity_release(struct device *dev)
{
   struct cyttsp5_proximity_data *pd = get_prox_data(dev);
 
   if (pd->input_device_registered) {
       /* Disable proximity sensing */
       mutex_lock(&pd->sysfs_lock);
       if (pd->enable_count)
           _cyttsp5_proximity_disable(pd, true);
       mutex_unlock(&pd->sysfs_lock);
       device_remove_file(dev, &dev_attr_prox_enable);
       input_unregister_device(pd->input);
   } else {
       input_free_device(pd->input);
       _cyttsp5_unsubscribe_attention(dev, CY_ATTEN_STARTUP,
           CYTTSP5_PROXIMITY_NAME, cyttsp5_setup_input_attention,
           0);
   }
 
   return 0;
}