hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
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
/*
 * cyttsp5_debug.c
 * Parade TrueTouch(TM) Standard Product V5 Debug Module.
 * For use with Parade touchscreen controllers.
 * Supported parts include:
 * CYTMA5XX
 * CYTMA448
 * CYTMA445A
 * CYTT21XXX
 * CYTT31XXX
 *
 * Copyright (C) 2015 Parade Technologies
 * Copyright (C) 2012-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_DEBUG_NAME "cyttsp5_debug"
 
struct cyttsp5_debug_data {
   struct device *dev;
   struct cyttsp5_sysinfo *si;
   uint32_t interrupt_count;
   uint32_t formated_output;
   struct mutex sysfs_lock;
   u8 pr_buf[CY_MAX_PRBUF_SIZE];
};
 
static struct cyttsp5_core_commands *cmd;
 
static struct cyttsp5_module debug_module;
 
static inline struct cyttsp5_debug_data *cyttsp5_get_debug_data(
       struct device *dev)
{
   return cyttsp5_get_module_data(dev, &debug_module);
}
 
/*
 * This function provide output of combined xy_mode and xy_data.
 * Required by TTHE.
 */
static void cyttsp5_pr_buf_op_mode(struct cyttsp5_debug_data *dd, u8 *pr_buf,
       struct cyttsp5_sysinfo *si, u8 cur_touch)
{
   const char fmt[] = "%02X ";
   int max = (CY_MAX_PRBUF_SIZE - 1) - sizeof(CY_PR_TRUNCATED);
   u8 report_id = si->xy_mode[2];
   int header_size = 0;
   int report_size = 0;
   int total_size = 0;
   int i, k;
 
   if (report_id == si->desc.tch_report_id) {
       header_size = si->desc.tch_header_size;
       report_size = cur_touch * si->desc.tch_record_size;
   } else if (report_id == si->desc.btn_report_id) {
       header_size = BTN_INPUT_HEADER_SIZE;
       report_size = BTN_REPORT_SIZE;
   }
   total_size = header_size + report_size;
 
   pr_buf[0] = 0;
   for (i = k = 0; i < header_size && i < max; i++, k += 3)
       scnprintf(pr_buf + k, CY_MAX_PRBUF_SIZE, fmt, si->xy_mode[i]);
 
   for (i = 0; i < report_size && i < max; i++, k += 3)
       scnprintf(pr_buf + k, CY_MAX_PRBUF_SIZE, fmt, si->xy_data[i]);
 
   pr_info("%s=%s%s\n", "cyttsp5_OpModeData", pr_buf,
           total_size <= max ? "" : CY_PR_TRUNCATED);
}
 
static void cyttsp5_debug_print(struct device *dev, u8 *pr_buf, u8 *sptr,
       int size, const char *data_name)
{
   int i, j;
   int elem_size = sizeof("XX ") - 1;
   int max = (CY_MAX_PRBUF_SIZE - 1) / elem_size;
   int limit = size < max ? size : max;
 
   if (limit < 0)
       limit = 0;
 
   pr_buf[0] = 0;
   for (i = j = 0; i < limit; i++, j += elem_size)
       scnprintf(pr_buf + j, CY_MAX_PRBUF_SIZE - j, "%02X ", sptr[i]);
 
   if (size)
       pr_info("%s[0..%d]=%s%s\n", data_name, size - 1, pr_buf,
           size <= max ? "" : CY_PR_TRUNCATED);
   else
       pr_info("%s[]\n", data_name);
}
 
static void cyttsp5_debug_formated(struct device *dev, u8 *pr_buf,
       struct cyttsp5_sysinfo *si, u8 num_cur_tch)
{
   u8 report_id = si->xy_mode[2];
   int header_size = 0;
   int report_size = 0;
   u8 data_name[] = "touch[99]";
   int max_print_length = 20;
   int i;
 
   if (report_id == si->desc.tch_report_id) {
       header_size = si->desc.tch_header_size;
       report_size = num_cur_tch * si->desc.tch_record_size;
   } else if (report_id == si->desc.btn_report_id) {
       header_size = BTN_INPUT_HEADER_SIZE;
       report_size = BTN_REPORT_SIZE;
   }
 
   /* xy_mode */
   cyttsp5_debug_print(dev, pr_buf, si->xy_mode, header_size, "xy_mode");
 
   /* xy_data */
   if (report_size > max_print_length) {
       pr_info("xy_data[0..%d]:\n", report_size);
       for (i = 0; i < report_size - max_print_length;
               i += max_print_length) {
           cyttsp5_debug_print(dev, pr_buf, si->xy_data + i,
                   max_print_length, " ");
       }
       if (report_size - i)
           cyttsp5_debug_print(dev, pr_buf, si->xy_data + i,
                   report_size - i, " ");
   } else {
       cyttsp5_debug_print(dev, pr_buf, si->xy_data, report_size,
               "xy_data");
   }
 
   /* touches */
   if (report_id == si->desc.tch_report_id) {
       for (i = 0; i < num_cur_tch; i++) {
           scnprintf(data_name, sizeof(data_name) - 1,
                   "touch[%u]", i);
           cyttsp5_debug_print(dev, pr_buf,
               si->xy_data + (i * si->desc.tch_record_size),
               si->desc.tch_record_size, data_name);
       }
   }
 
   /* buttons */
   if (report_id == si->desc.btn_report_id)
       cyttsp5_debug_print(dev, pr_buf, si->xy_data, report_size,
               "button");
}
 
/* read xy_data for all touches for debug */
static int cyttsp5_xy_worker(struct cyttsp5_debug_data *dd)
{
   struct device *dev = dd->dev;
   struct cyttsp5_sysinfo *si = dd->si;
   u8 report_reg = si->xy_mode[TOUCH_COUNT_BYTE_OFFSET];
   u8 num_cur_tch = GET_NUM_TOUCHES(report_reg);
   uint32_t formated_output;
 
   mutex_lock(&dd->sysfs_lock);
   dd->interrupt_count++;
   formated_output = dd->formated_output;
   mutex_unlock(&dd->sysfs_lock);
 
   /* Interrupt */
   pr_info("Interrupt(%u)\n", dd->interrupt_count);
 
   if (formated_output)
       cyttsp5_debug_formated(dev, dd->pr_buf, si, num_cur_tch);
   else
       /* print data for TTHE */
       cyttsp5_pr_buf_op_mode(dd, dd->pr_buf, si, num_cur_tch);
 
   pr_info("\n");
 
   return 0;
}
 
static int cyttsp5_debug_attention(struct device *dev)
{
   struct cyttsp5_debug_data *dd = cyttsp5_get_debug_data(dev);
   struct cyttsp5_sysinfo *si = dd->si;
   u8 report_id = si->xy_mode[2];
   int rc = 0;
 
   if (report_id != si->desc.tch_report_id
           && report_id != si->desc.btn_report_id)
       return 0;
 
   /* core handles handshake */
   rc = cyttsp5_xy_worker(dd);
   if (rc < 0)
       dev_err(dev, "%s: xy_worker error r=%d\n", __func__, rc);
 
   return rc;
}
 
static ssize_t cyttsp5_interrupt_count_show(struct device *dev,
       struct device_attribute *attr, char *buf)
{
   struct cyttsp5_debug_data *dd = cyttsp5_get_debug_data(dev);
   int val;
 
   mutex_lock(&dd->sysfs_lock);
   val = dd->interrupt_count;
   mutex_unlock(&dd->sysfs_lock);
 
   return scnprintf(buf, CY_MAX_PRBUF_SIZE, "Interrupt Count: %d\n", val);
}
 
static ssize_t cyttsp5_interrupt_count_store(struct device *dev,
       struct device_attribute *attr, const char *buf, size_t size)
{
   struct cyttsp5_debug_data *dd = cyttsp5_get_debug_data(dev);
 
   mutex_lock(&dd->sysfs_lock);
   dd->interrupt_count = 0;
   mutex_unlock(&dd->sysfs_lock);
   return size;
}
 
static DEVICE_ATTR(int_count, S_IRUSR | S_IWUSR,
   cyttsp5_interrupt_count_show, cyttsp5_interrupt_count_store);
 
static ssize_t cyttsp5_formated_output_show(struct device *dev,
       struct device_attribute *attr, char *buf)
{
   struct cyttsp5_debug_data *dd = cyttsp5_get_debug_data(dev);
   int val;
 
   mutex_lock(&dd->sysfs_lock);
   val = dd->formated_output;
   mutex_unlock(&dd->sysfs_lock);
 
   return scnprintf(buf, CY_MAX_PRBUF_SIZE,
           "Formated debug output: %x\n", val);
}
 
static ssize_t cyttsp5_formated_output_store(struct device *dev,
       struct device_attribute *attr, const char *buf, size_t size)
{
   struct cyttsp5_debug_data *dd = cyttsp5_get_debug_data(dev);
   unsigned long value;
   int rc;
 
   rc = kstrtoul(buf, 10, &value);
   if (rc < 0) {
       dev_err(dev, "%s: Invalid value\n", __func__);
       return size;
   }
 
   /* Expecting only 0 or 1 */
   if (value != 0 && value != 1) {
       dev_err(dev, "%s: Invalid value %lu\n", __func__, value);
       return size;
   }
 
   mutex_lock(&dd->sysfs_lock);
   dd->formated_output = value;
   mutex_unlock(&dd->sysfs_lock);
   return size;
}
 
static DEVICE_ATTR(formated_output, S_IRUSR | S_IWUSR,
   cyttsp5_formated_output_show, cyttsp5_formated_output_store);
 
static int cyttsp5_debug_probe(struct device *dev, void **data)
{
   struct cyttsp5_debug_data *dd;
   int rc;
 
   /* get context and debug print buffers */
   dd = kzalloc(sizeof(*dd), GFP_KERNEL);
   if (!dd) {
       rc = -ENOMEM;
       goto cyttsp5_debug_probe_alloc_failed;
   }
 
   rc = device_create_file(dev, &dev_attr_int_count);
   if (rc) {
       dev_err(dev, "%s: Error, could not create int_count\n",
               __func__);
       goto cyttsp5_debug_probe_create_int_count_failed;
   }
 
   rc = device_create_file(dev, &dev_attr_formated_output);
   if (rc) {
       dev_err(dev, "%s: Error, could not create formated_output\n",
               __func__);
       goto cyttsp5_debug_probe_create_formated_failed;
   }
 
   mutex_init(&dd->sysfs_lock);
   dd->dev = dev;
   *data = dd;
 
   dd->si = cmd->request_sysinfo(dev);
   if (!dd->si) {
       dev_err(dev, "%s: Fail get sysinfo pointer from core\n",
               __func__);
       rc = -ENODEV;
       goto cyttsp5_debug_probe_sysinfo_failed;
   }
 
   rc = cmd->subscribe_attention(dev, CY_ATTEN_IRQ, CYTTSP5_DEBUG_NAME,
       cyttsp5_debug_attention, CY_MODE_OPERATIONAL);
   if (rc < 0) {
       dev_err(dev, "%s: Error, could not subscribe attention cb\n",
               __func__);
       goto cyttsp5_debug_probe_subscribe_failed;
   }
 
   return 0;
 
cyttsp5_debug_probe_subscribe_failed:
cyttsp5_debug_probe_sysinfo_failed:
   device_remove_file(dev, &dev_attr_formated_output);
cyttsp5_debug_probe_create_formated_failed:
   device_remove_file(dev, &dev_attr_int_count);
cyttsp5_debug_probe_create_int_count_failed:
   kfree(dd);
cyttsp5_debug_probe_alloc_failed:
   dev_err(dev, "%s failed.\n", __func__);
   return rc;
}
 
static void cyttsp5_debug_release(struct device *dev, void *data)
{
   struct cyttsp5_debug_data *dd = data;
   int rc;
 
   rc = cmd->unsubscribe_attention(dev, CY_ATTEN_IRQ, CYTTSP5_DEBUG_NAME,
       cyttsp5_debug_attention, CY_MODE_OPERATIONAL);
   if (rc < 0) {
       dev_err(dev, "%s: Error, could not un-subscribe attention\n",
               __func__);
       goto cyttsp5_debug_release_exit;
   }
 
cyttsp5_debug_release_exit:
   device_remove_file(dev, &dev_attr_formated_output);
   device_remove_file(dev, &dev_attr_int_count);
   kfree(dd);
}
 
static struct cyttsp5_module debug_module = {
   .name = CYTTSP5_DEBUG_NAME,
   .probe = cyttsp5_debug_probe,
   .release = cyttsp5_debug_release,
};
 
static int __init cyttsp5_debug_init(void)
{
   int rc;
 
   cmd = cyttsp5_get_commands();
   if (!cmd)
       return -EINVAL;
 
   rc = cyttsp5_register_module(&debug_module);
   if (rc < 0) {
       pr_err("%s: Error, failed registering module\n",
           __func__);
           return rc;
   }
 
   pr_info("%s: Parade TTSP Debug Driver (Built %s) rc=%d\n",
        __func__, CY_DRIVER_VERSION, rc);
   return 0;
}
module_init(cyttsp5_debug_init);
 
static void __exit cyttsp5_debug_exit(void)
{
   cyttsp5_unregister_module(&debug_module);
}
module_exit(cyttsp5_debug_exit);
 
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Debug Driver");
MODULE_AUTHOR("Parade Technologies <ttdrivers@paradetech.com>");