pt_debug.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * pt_debug.c
  3. * Parade TrueTouch(TM) Standard Product Debug Module.
  4. * For use with Parade touchscreen controllers.
  5. * Supported parts include:
  6. * TMA5XX
  7. * TMA448
  8. * TMA445A
  9. * TT21XXX
  10. * TT31XXX
  11. * TT4XXXX
  12. * TT7XXX
  13. * TC3XXX
  14. *
  15. * Copyright (C) 2015-2020 Parade Technologies
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * version 2, and only version 2, as published by the
  20. * Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * Contact Parade Technologies at www.paradetech.com <[email protected]>
  28. */
  29. #include "pt_regs.h"
  30. #define PT_DEBUG_NAME "pt_debug"
  31. struct pt_debug_data {
  32. struct device *dev;
  33. struct pt_sysinfo *si;
  34. uint32_t interrupt_count;
  35. uint32_t formated_output;
  36. struct mutex sysfs_lock;
  37. u8 pr_buf[PT_MAX_PRBUF_SIZE];
  38. };
  39. static struct pt_core_commands *cmd;
  40. static struct pt_module debug_module;
  41. /*******************************************************************************
  42. * FUNCTION: pt_get_debug_data
  43. *
  44. * SUMMARY: Inline function to get pt_debug_data pointer from debug module.
  45. *
  46. * RETURN:
  47. * pointer to pt_debug_data structure
  48. *
  49. * PARAMETERS:
  50. * *dev - pointer to device structure
  51. ******************************************************************************/
  52. static inline struct pt_debug_data *pt_get_debug_data(
  53. struct device *dev)
  54. {
  55. return pt_get_module_data(dev, &debug_module);
  56. }
  57. /*******************************************************************************
  58. * FUNCTION: pt_pr_buf_op_mode
  59. *
  60. * SUMMARY: Formats touch/button report to pr_buf that combined xy_mode and
  61. * xy_data. The feature is required by TTHE.
  62. *
  63. * PARAMETERS:
  64. * *dd - pointer to pt_debug_data structure
  65. * *pr_buf - pointer to print buffer
  66. * *si - pointer to sysinfo structure
  67. * cur_touch - number of current touch
  68. ******************************************************************************/
  69. static void pt_pr_buf_op_mode(struct pt_debug_data *dd, u8 *pr_buf,
  70. struct pt_sysinfo *si, u8 cur_touch)
  71. {
  72. const char fmt[] = "%02X ";
  73. int max = (PT_MAX_PRBUF_SIZE - 1) - sizeof(PT_PR_TRUNCATED);
  74. u8 report_id = si->xy_mode[2];
  75. int header_size = 0;
  76. int report_size = 0;
  77. int total_size = 0;
  78. int i, k;
  79. if (report_id == si->desc.tch_report_id) {
  80. header_size = si->desc.tch_header_size;
  81. report_size = cur_touch * si->desc.tch_record_size;
  82. } else if (report_id == si->desc.btn_report_id) {
  83. header_size = BTN_INPUT_HEADER_SIZE;
  84. report_size = BTN_REPORT_SIZE;
  85. }
  86. total_size = header_size + report_size;
  87. pr_buf[0] = 0;
  88. for (i = k = 0; i < header_size && i < max; i++, k += 3)
  89. scnprintf(pr_buf + k, PT_MAX_PRBUF_SIZE, fmt, si->xy_mode[i]);
  90. for (i = 0; i < report_size && i < max; i++, k += 3)
  91. scnprintf(pr_buf + k, PT_MAX_PRBUF_SIZE, fmt, si->xy_data[i]);
  92. pr_info("%s=%s%s\n", "pt_OpModeData", pr_buf,
  93. total_size <= max ? "" : PT_PR_TRUNCATED);
  94. }
  95. /*******************************************************************************
  96. * FUNCTION: pt_debug_print
  97. *
  98. * SUMMARY: This function prints header to show data size and data_name and
  99. * content of "pr_buf" with hex base.
  100. *
  101. * PARAMETERS:
  102. * *dev - pointer to device structure
  103. * *pr_buf - pointer to input buffer which stores the formated data
  104. * *sptr - pointer to the buffer to print
  105. * size - size of data elements
  106. * *data_name - data name to print
  107. ******************************************************************************/
  108. static void pt_debug_print(struct device *dev, u8 *pr_buf, u8 *sptr,
  109. int size, const char *data_name)
  110. {
  111. int i, j;
  112. int elem_size = sizeof("XX ") - 1;
  113. int max = (PT_MAX_PRBUF_SIZE - 1) / elem_size;
  114. int limit = size < max ? size : max;
  115. if (limit < 0)
  116. limit = 0;
  117. pr_buf[0] = 0;
  118. for (i = j = 0; i < limit; i++, j += elem_size)
  119. scnprintf(pr_buf + j, PT_MAX_PRBUF_SIZE - j, "%02X ", sptr[i]);
  120. if (size)
  121. pr_info("%s[0..%d]=%s%s\n", data_name, size - 1, pr_buf,
  122. size <= max ? "" : PT_PR_TRUNCATED);
  123. else
  124. pr_info("%s[]\n", data_name);
  125. }
  126. /*******************************************************************************
  127. * FUNCTION: pt_debug_formated
  128. *
  129. * SUMMARY: Formats and prints touch & button report.
  130. *
  131. * PARAMETERS:
  132. * *dev - pointer to device structure
  133. * *pr_buf - pointer to print buffer
  134. * *si - pointer to sysinfo structure
  135. * cur_touch - number of current touch
  136. ******************************************************************************/
  137. static void pt_debug_formated(struct device *dev, u8 *pr_buf,
  138. struct pt_sysinfo *si, u8 num_cur_tch)
  139. {
  140. u8 report_id = si->xy_mode[2];
  141. int header_size = 0;
  142. int report_size = 0;
  143. u8 data_name[] = "touch[99]";
  144. int max_print_length = 20;
  145. int i;
  146. if (report_id == si->desc.tch_report_id) {
  147. header_size = si->desc.tch_header_size;
  148. report_size = num_cur_tch * si->desc.tch_record_size;
  149. } else if (report_id == si->desc.btn_report_id) {
  150. header_size = BTN_INPUT_HEADER_SIZE;
  151. report_size = BTN_REPORT_SIZE;
  152. }
  153. /* xy_mode */
  154. pt_debug_print(dev, pr_buf, si->xy_mode, header_size, "xy_mode");
  155. /* xy_data */
  156. if (report_size > max_print_length) {
  157. pr_info("xy_data[0..%d]:\n", report_size);
  158. for (i = 0; i < report_size - max_print_length;
  159. i += max_print_length) {
  160. pt_debug_print(dev, pr_buf, si->xy_data + i,
  161. max_print_length, " ");
  162. }
  163. if (report_size - i)
  164. pt_debug_print(dev, pr_buf, si->xy_data + i,
  165. report_size - i, " ");
  166. } else {
  167. pt_debug_print(dev, pr_buf, si->xy_data, report_size,
  168. "xy_data");
  169. }
  170. /* touches */
  171. if (report_id == si->desc.tch_report_id) {
  172. for (i = 0; i < num_cur_tch; i++) {
  173. scnprintf(data_name, sizeof(data_name) - 1,
  174. "touch[%u]", i);
  175. pt_debug_print(dev, pr_buf,
  176. si->xy_data + (i * si->desc.tch_record_size),
  177. si->desc.tch_record_size, data_name);
  178. }
  179. }
  180. /* buttons */
  181. if (report_id == si->desc.btn_report_id)
  182. pt_debug_print(dev, pr_buf, si->xy_data, report_size,
  183. "button");
  184. }
  185. /*******************************************************************************
  186. * FUNCTION: pt_xy_worker
  187. *
  188. * SUMMARY: Read xy_data for all touches for debug.
  189. *
  190. * RETURN:
  191. * 0 = success
  192. *
  193. * PARAMETERS:
  194. * *dd - pointer to pt_debug_data structure
  195. ******************************************************************************/
  196. static int pt_xy_worker(struct pt_debug_data *dd)
  197. {
  198. struct device *dev = dd->dev;
  199. struct pt_sysinfo *si = dd->si;
  200. u8 report_reg = si->xy_mode[TOUCH_COUNT_BYTE_OFFSET];
  201. u8 num_cur_tch = GET_NUM_TOUCHES(report_reg);
  202. uint32_t formated_output;
  203. mutex_lock(&dd->sysfs_lock);
  204. dd->interrupt_count++;
  205. formated_output = dd->formated_output;
  206. mutex_unlock(&dd->sysfs_lock);
  207. /* Interrupt */
  208. pr_info("Interrupt(%u)\n", dd->interrupt_count);
  209. if (formated_output)
  210. pt_debug_formated(dev, dd->pr_buf, si, num_cur_tch);
  211. else
  212. /* print data for TTHE */
  213. pt_pr_buf_op_mode(dd, dd->pr_buf, si, num_cur_tch);
  214. pr_info("\n");
  215. return 0;
  216. }
  217. /*******************************************************************************
  218. * FUNCTION: pt_debug_attention
  219. *
  220. * SUMMARY: Wrapper function for pt_xy_worker() to subcribe into TTDL attention
  221. * list.
  222. *
  223. * RETURN:
  224. * 0 = success
  225. * !0 = failure
  226. *
  227. * PARAMETERS:
  228. * *dev - pointer to device structure
  229. ******************************************************************************/
  230. static int pt_debug_attention(struct device *dev)
  231. {
  232. struct pt_debug_data *dd = pt_get_debug_data(dev);
  233. struct pt_sysinfo *si = dd->si;
  234. u8 report_id = si->xy_mode[2];
  235. int rc = 0;
  236. if (report_id != si->desc.tch_report_id
  237. && report_id != si->desc.btn_report_id)
  238. return 0;
  239. /* core handles handshake */
  240. rc = pt_xy_worker(dd);
  241. if (rc < 0)
  242. pt_debug(dev, DL_ERROR, "%s: xy_worker error r=%d\n",
  243. __func__, rc);
  244. return rc;
  245. }
  246. /*******************************************************************************
  247. * FUNCTION: pt_status_show
  248. *
  249. * SUMMARY: The show method for the int_count sysfs node. This node displays
  250. * the count of interrupt.
  251. *
  252. * PARAMETERS:
  253. * *dev - pointer to Device structure
  254. * *attr - pointer to the device attribute structure
  255. * *buf - pointer to buffer to print
  256. ******************************************************************************/
  257. static ssize_t pt_interrupt_count_show(struct device *dev,
  258. struct device_attribute *attr, char *buf)
  259. {
  260. struct pt_debug_data *dd = pt_get_debug_data(dev);
  261. int val;
  262. mutex_lock(&dd->sysfs_lock);
  263. val = dd->interrupt_count;
  264. mutex_unlock(&dd->sysfs_lock);
  265. return scnprintf(buf, PT_MAX_PRBUF_SIZE, "Interrupt Count: %d\n", val);
  266. }
  267. /*******************************************************************************
  268. * FUNCTION: pt_interrupt_count_store
  269. *
  270. * SUMMARY: The store method for the int_count sysfs node that allows the count
  271. * of interrput to be cleared.
  272. *
  273. * RETURN: Size of passed in buffer
  274. *
  275. * PARAMETERS:
  276. * *dev - pointer to device structure
  277. * *attr - pointer to device attributes
  278. * *buf - pointer to buffer that hold the command parameters
  279. * size - size of buf
  280. ******************************************************************************/
  281. static ssize_t pt_interrupt_count_store(struct device *dev,
  282. struct device_attribute *attr, const char *buf, size_t size)
  283. {
  284. struct pt_debug_data *dd = pt_get_debug_data(dev);
  285. mutex_lock(&dd->sysfs_lock);
  286. dd->interrupt_count = 0;
  287. mutex_unlock(&dd->sysfs_lock);
  288. return size;
  289. }
  290. static DEVICE_ATTR(int_count, 0600,
  291. pt_interrupt_count_show, pt_interrupt_count_store);
  292. /*******************************************************************************
  293. * FUNCTION: pt_formated_output_show
  294. *
  295. * SUMMARY: Show method for the formated_output sysfs node that will show
  296. * whether to format data to buffer or print directly.
  297. *
  298. * RETURN:
  299. * 0 = success
  300. * !0 = failure
  301. *
  302. * PARAMETERS:
  303. * *dev - pointer to device structure
  304. * *attr - pointer to device attributes
  305. * *buf - pointer to output buffer
  306. ******************************************************************************/
  307. static ssize_t pt_formated_output_show(struct device *dev,
  308. struct device_attribute *attr, char *buf)
  309. {
  310. struct pt_debug_data *dd = pt_get_debug_data(dev);
  311. int val;
  312. mutex_lock(&dd->sysfs_lock);
  313. val = dd->formated_output;
  314. mutex_unlock(&dd->sysfs_lock);
  315. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  316. "Formated debug output: %x\n", val);
  317. }
  318. /*******************************************************************************
  319. * FUNCTION: pt_formated_output_store
  320. *
  321. * SUMMARY: The store method for the formated_output sysfs node. Allows the
  322. * setting to format data to buffer or print directly.
  323. *
  324. * RETURN: Size of passed in buffer
  325. *
  326. * PARAMETERS:
  327. * *dev - pointer to device structure
  328. * *attr - pointer to device attributes
  329. * *buf - pointer to buffer that hold the command parameters
  330. * size - size of buf
  331. ******************************************************************************/
  332. static ssize_t pt_formated_output_store(struct device *dev,
  333. struct device_attribute *attr, const char *buf, size_t size)
  334. {
  335. struct pt_debug_data *dd = pt_get_debug_data(dev);
  336. unsigned long value;
  337. int rc;
  338. rc = kstrtoul(buf, 10, &value);
  339. if (rc < 0) {
  340. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  341. return size;
  342. }
  343. /* Expecting only 0 or 1 */
  344. if (value != 0 && value != 1) {
  345. pt_debug(dev, DL_ERROR, "%s: Invalid value %lu\n",
  346. __func__, value);
  347. return size;
  348. }
  349. mutex_lock(&dd->sysfs_lock);
  350. dd->formated_output = value;
  351. mutex_unlock(&dd->sysfs_lock);
  352. return size;
  353. }
  354. static DEVICE_ATTR(formated_output, 0600,
  355. pt_formated_output_show, pt_formated_output_store);
  356. /*******************************************************************************
  357. * FUNCTION: pt_mt_probe
  358. *
  359. * SUMMARY: The probe function for debug module to create sysfs nodes and
  360. * subscribe attention list.
  361. *
  362. * RETURN:
  363. * 0 = success
  364. *
  365. * PARAMETERS:
  366. * *dev - pointer to device structure
  367. * **data - double pointer tothe pt_debug_data structure to be created here
  368. ******************************************************************************/
  369. static int pt_debug_probe(struct device *dev, void **data)
  370. {
  371. struct pt_debug_data *dd;
  372. int rc;
  373. /* get context and debug print buffers */
  374. dd = kzalloc(sizeof(*dd), GFP_KERNEL);
  375. if (!dd) {
  376. rc = -ENOMEM;
  377. goto pt_debug_probe_alloc_failed;
  378. }
  379. rc = device_create_file(dev, &dev_attr_int_count);
  380. if (rc) {
  381. pt_debug(dev, DL_ERROR, "%s: Error, could not create int_count\n",
  382. __func__);
  383. goto pt_debug_probe_create_int_count_failed;
  384. }
  385. rc = device_create_file(dev, &dev_attr_formated_output);
  386. if (rc) {
  387. pt_debug(dev, DL_ERROR, "%s: Error, could not create formated_output\n",
  388. __func__);
  389. goto pt_debug_probe_create_formated_failed;
  390. }
  391. mutex_init(&dd->sysfs_lock);
  392. dd->dev = dev;
  393. *data = dd;
  394. dd->si = cmd->request_sysinfo(dev);
  395. if (!dd->si) {
  396. pt_debug(dev, DL_ERROR, "%s: Fail get sysinfo pointer from core\n",
  397. __func__);
  398. rc = -ENODEV;
  399. goto pt_debug_probe_sysinfo_failed;
  400. }
  401. rc = cmd->subscribe_attention(dev, PT_ATTEN_IRQ, PT_DEBUG_NAME,
  402. pt_debug_attention, PT_MODE_OPERATIONAL);
  403. if (rc < 0) {
  404. pt_debug(dev, DL_ERROR, "%s: Error, could not subscribe attention cb\n",
  405. __func__);
  406. goto pt_debug_probe_subscribe_failed;
  407. }
  408. return 0;
  409. pt_debug_probe_subscribe_failed:
  410. pt_debug_probe_sysinfo_failed:
  411. device_remove_file(dev, &dev_attr_formated_output);
  412. pt_debug_probe_create_formated_failed:
  413. device_remove_file(dev, &dev_attr_int_count);
  414. pt_debug_probe_create_int_count_failed:
  415. kfree(dd);
  416. pt_debug_probe_alloc_failed:
  417. pt_debug(dev, DL_ERROR, "%s failed.\n", __func__);
  418. return rc;
  419. }
  420. /*******************************************************************************
  421. * FUNCTION: pt_debug_release
  422. *
  423. * SUMMARY: Remove function for debug module that does following cleanup:
  424. * - Unsubscibe all registered attention tasks
  425. * - Removes all created sysfs nodes
  426. *
  427. * PARAMETERS:
  428. * *dev - pointer to device structure
  429. * *data - pointer to the pt_debug_data structure
  430. ******************************************************************************/
  431. static void pt_debug_release(struct device *dev, void *data)
  432. {
  433. struct pt_debug_data *dd = data;
  434. int rc;
  435. rc = cmd->unsubscribe_attention(dev, PT_ATTEN_IRQ, PT_DEBUG_NAME,
  436. pt_debug_attention, PT_MODE_OPERATIONAL);
  437. if (rc < 0) {
  438. pt_debug(dev, DL_ERROR, "%s: Error, could not un-subscribe attention\n",
  439. __func__);
  440. goto pt_debug_release_exit;
  441. }
  442. pt_debug_release_exit:
  443. device_remove_file(dev, &dev_attr_formated_output);
  444. device_remove_file(dev, &dev_attr_int_count);
  445. kfree(dd);
  446. }
  447. static struct pt_module debug_module = {
  448. .name = PT_DEBUG_NAME,
  449. .probe = pt_debug_probe,
  450. .release = pt_debug_release,
  451. };
  452. /*******************************************************************************
  453. * FUNCTION: pt_debug_init
  454. *
  455. * SUMMARY: Initialize function for debug module which to register
  456. * debug_module into TTDL module list.
  457. *
  458. * RETURN:
  459. * 0 = success
  460. ******************************************************************************/
  461. static int __init pt_debug_init(void)
  462. {
  463. int rc;
  464. cmd = pt_get_commands();
  465. if (!cmd)
  466. return -EINVAL;
  467. rc = pt_register_module(&debug_module);
  468. if (rc < 0) {
  469. pr_err("%s: Error, failed registering module\n",
  470. __func__);
  471. return rc;
  472. }
  473. pr_info("%s: Parade TTSP Debug Driver (Built %s) rc=%d\n",
  474. __func__, PT_DRIVER_VERSION, rc);
  475. return 0;
  476. }
  477. module_init(pt_debug_init);
  478. /*******************************************************************************
  479. * FUNCTION: pt_debug_exit
  480. *
  481. * SUMMARY: Exit function for debug module which to unregister debug_module
  482. * from TTDL module list.
  483. *
  484. ******************************************************************************/
  485. static void __exit pt_debug_exit(void)
  486. {
  487. pt_unregister_module(&debug_module);
  488. }
  489. module_exit(pt_debug_exit);
  490. MODULE_LICENSE("GPL");
  491. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Debug Driver");
  492. MODULE_AUTHOR("Parade Technologies <[email protected]>");