cyttsp5_debug.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * cyttsp5_debug.c
  3. * Cypress TrueTouch(TM) Standard Product V5 Debug Module.
  4. * For use with Cypress Txx5xx parts.
  5. * Supported parts include:
  6. * TMA5XX
  7. *
  8. * Copyright (C) 2012-2013 Cypress Semiconductor
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * version 2, and only version 2, as published by the
  13. * Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * Contact Cypress Semiconductor at www.cypress.com <ttdrivers@cypress.com>
  21. *
  22. */
  23. #include "cyttsp5_regs.h"
  24. #define CYTTSP5_DEBUG_NAME "cyttsp5_debug"
  25. struct cyttsp5_debug_data {
  26. struct device *dev;
  27. struct cyttsp5_sysinfo *si;
  28. uint32_t interrupt_count;
  29. uint32_t formated_output;
  30. struct mutex sysfs_lock;
  31. u8 pr_buf[CY_MAX_PRBUF_SIZE];
  32. };
  33. static struct cyttsp5_core_commands *cmd;
  34. static inline struct cyttsp5_debug_data *cyttsp5_get_debug_data(
  35. struct device *dev)
  36. {
  37. return cyttsp5_get_dynamic_data(dev, CY_MODULE_DEBUG);
  38. }
  39. /*
  40. * This function provide output of combined xy_mode and xy_data.
  41. * Required by TTHE.
  42. */
  43. static void cyttsp5_pr_buf_op_mode(struct cyttsp5_debug_data *dd, u8 *pr_buf,
  44. struct cyttsp5_sysinfo *si, u8 cur_touch)
  45. {
  46. const char fmt[] = "%02X ";
  47. int max = (CY_MAX_PRBUF_SIZE - 1) - sizeof(CY_PR_TRUNCATED);
  48. u8 report_id = si->xy_mode[2];
  49. int header_size = 0;
  50. int report_size = 0;
  51. int total_size = 0;
  52. int i, k;
  53. if (report_id == si->desc.tch_report_id) {
  54. header_size = si->desc.tch_header_size;
  55. report_size = cur_touch * si->desc.tch_record_size;
  56. } else if (report_id == si->desc.btn_report_id) {
  57. header_size = BTN_INPUT_HEADER_SIZE;
  58. report_size = BTN_REPORT_SIZE;
  59. }
  60. total_size = header_size + report_size;
  61. pr_buf[0] = 0;
  62. for (i = k = 0; i < header_size && i < max; i++, k += 3)
  63. scnprintf(pr_buf + k, CY_MAX_PRBUF_SIZE, fmt, si->xy_mode[i]);
  64. for (i = 0; i < report_size && i < max; i++, k += 3)
  65. scnprintf(pr_buf + k, CY_MAX_PRBUF_SIZE, fmt, si->xy_data[i]);
  66. pr_info("%s=%s%s\n", "cyttsp5_OpModeData", pr_buf,
  67. total_size <= max ? "" : CY_PR_TRUNCATED);
  68. }
  69. static void cyttsp5_debug_print(struct device *dev, u8 *pr_buf, u8 *sptr,
  70. int size, const char *data_name)
  71. {
  72. int i, j;
  73. int elem_size = sizeof("XX ") - 1;
  74. int max = (CY_MAX_PRBUF_SIZE - 1) / elem_size;
  75. int limit = size < max ? size : max;
  76. if (limit < 0)
  77. limit = 0;
  78. pr_buf[0] = 0;
  79. for (i = j = 0; i < limit; i++, j += elem_size)
  80. scnprintf(pr_buf + j, CY_MAX_PRBUF_SIZE - j, "%02X ", sptr[i]);
  81. if (size)
  82. pr_info("%s[0..%d]=%s%s\n", data_name, size - 1, pr_buf,
  83. size <= max ? "" : CY_PR_TRUNCATED);
  84. else
  85. pr_info("%s[]\n", data_name);
  86. }
  87. static void cyttsp5_debug_formated(struct device *dev, u8 *pr_buf,
  88. struct cyttsp5_sysinfo *si, u8 num_cur_tch)
  89. {
  90. u8 report_id = si->xy_mode[2];
  91. int header_size = 0;
  92. int report_size = 0;
  93. u8 data_name[] = "touch[99]";
  94. int max_print_length = 20;
  95. int i;
  96. if (report_id == si->desc.tch_report_id) {
  97. header_size = si->desc.tch_header_size;
  98. report_size = num_cur_tch * si->desc.tch_record_size;
  99. } else if (report_id == si->desc.btn_report_id) {
  100. header_size = BTN_INPUT_HEADER_SIZE;
  101. report_size = BTN_REPORT_SIZE;
  102. }
  103. /* xy_mode */
  104. cyttsp5_debug_print(dev, pr_buf, si->xy_mode, header_size, "xy_mode");
  105. /* xy_data */
  106. if (report_size > max_print_length) {
  107. pr_info("xy_data[0..%d]:\n", report_size);
  108. for (i = 0; i < report_size - max_print_length;
  109. i += max_print_length) {
  110. cyttsp5_debug_print(dev, pr_buf, si->xy_data + i,
  111. max_print_length, " ");
  112. }
  113. if (report_size - i)
  114. cyttsp5_debug_print(dev, pr_buf, si->xy_data + i,
  115. report_size - i, " ");
  116. } else {
  117. cyttsp5_debug_print(dev, pr_buf, si->xy_data, report_size,
  118. "xy_data");
  119. }
  120. /* touches */
  121. if (report_id == si->desc.tch_report_id) {
  122. for (i = 0; i < num_cur_tch; i++) {
  123. scnprintf(data_name, sizeof(data_name) - 1,
  124. "touch[%u]", i);
  125. cyttsp5_debug_print(dev, pr_buf,
  126. si->xy_data + (i * si->desc.tch_record_size),
  127. si->desc.tch_record_size, data_name);
  128. }
  129. }
  130. /* buttons */
  131. if (report_id == si->desc.btn_report_id)
  132. cyttsp5_debug_print(dev, pr_buf, si->xy_data, report_size,
  133. "button");
  134. }
  135. /* read xy_data for all touches for debug */
  136. static int cyttsp5_xy_worker(struct cyttsp5_debug_data *dd)
  137. {
  138. struct device *dev = dd->dev;
  139. struct cyttsp5_sysinfo *si = dd->si;
  140. u8 report_reg = si->xy_mode[TOUCH_COUNT_BYTE_OFFSET];
  141. u8 num_cur_tch = GET_NUM_TOUCHES(report_reg);
  142. uint32_t formated_output;
  143. mutex_lock(&dd->sysfs_lock);
  144. dd->interrupt_count++;
  145. formated_output = dd->formated_output;
  146. mutex_unlock(&dd->sysfs_lock);
  147. /* Interrupt */
  148. pr_info("Interrupt(%u)\n", dd->interrupt_count);
  149. if (formated_output)
  150. cyttsp5_debug_formated(dev, dd->pr_buf, si, num_cur_tch);
  151. else
  152. /* print data for TTHE */
  153. cyttsp5_pr_buf_op_mode(dd, dd->pr_buf, si, num_cur_tch);
  154. pr_info("\n");
  155. return 0;
  156. }
  157. static int cyttsp5_debug_attention(struct device *dev)
  158. {
  159. struct cyttsp5_debug_data *dd = cyttsp5_get_debug_data(dev);
  160. struct cyttsp5_sysinfo *si = dd->si;
  161. u8 report_id = si->xy_mode[2];
  162. int rc = 0;
  163. if (report_id != si->desc.tch_report_id
  164. && report_id != si->desc.btn_report_id)
  165. return 0;
  166. /* core handles handshake */
  167. rc = cyttsp5_xy_worker(dd);
  168. if (rc < 0)
  169. dev_err(dev, "%s: xy_worker error r=%d\n", __func__, rc);
  170. return rc;
  171. }
  172. static ssize_t cyttsp5_interrupt_count_show(struct device *dev,
  173. struct device_attribute *attr, char *buf)
  174. {
  175. struct cyttsp5_debug_data *dd = cyttsp5_get_debug_data(dev);
  176. int val;
  177. mutex_lock(&dd->sysfs_lock);
  178. val = dd->interrupt_count;
  179. mutex_unlock(&dd->sysfs_lock);
  180. return scnprintf(buf, CY_MAX_PRBUF_SIZE, "Interrupt Count: %d\n", val);
  181. }
  182. static ssize_t cyttsp5_interrupt_count_store(struct device *dev,
  183. struct device_attribute *attr, const char *buf, size_t size)
  184. {
  185. struct cyttsp5_debug_data *dd = cyttsp5_get_debug_data(dev);
  186. mutex_lock(&dd->sysfs_lock);
  187. dd->interrupt_count = 0;
  188. mutex_unlock(&dd->sysfs_lock);
  189. return size;
  190. }
  191. static DEVICE_ATTR(int_count, S_IRUSR | S_IWUSR,
  192. cyttsp5_interrupt_count_show, cyttsp5_interrupt_count_store);
  193. static ssize_t cyttsp5_formated_output_show(struct device *dev,
  194. struct device_attribute *attr, char *buf)
  195. {
  196. struct cyttsp5_debug_data *dd = cyttsp5_get_debug_data(dev);
  197. int val;
  198. mutex_lock(&dd->sysfs_lock);
  199. val = dd->formated_output;
  200. mutex_unlock(&dd->sysfs_lock);
  201. return scnprintf(buf, CY_MAX_PRBUF_SIZE,
  202. "Formated debug output: %x\n", val);
  203. }
  204. static ssize_t cyttsp5_formated_output_store(struct device *dev,
  205. struct device_attribute *attr, const char *buf, size_t size)
  206. {
  207. struct cyttsp5_debug_data *dd = cyttsp5_get_debug_data(dev);
  208. unsigned long value;
  209. int rc;
  210. rc = kstrtoul(buf, 10, &value);
  211. if (rc < 0) {
  212. dev_err(dev, "%s: Invalid value\n", __func__);
  213. return size;
  214. }
  215. /* Expecting only 0 or 1 */
  216. if (value != 0 && value != 1) {
  217. dev_err(dev, "%s: Invalid value %lu\n", __func__, value);
  218. return size;
  219. }
  220. mutex_lock(&dd->sysfs_lock);
  221. dd->formated_output = value;
  222. mutex_unlock(&dd->sysfs_lock);
  223. return size;
  224. }
  225. static DEVICE_ATTR(formated_output, S_IRUSR | S_IWUSR,
  226. cyttsp5_formated_output_show, cyttsp5_formated_output_store);
  227. int cyttsp5_debug_probe(struct device *dev)
  228. {
  229. struct cyttsp5_core_data *cd = dev_get_drvdata(dev);
  230. struct cyttsp5_debug_data *dd;
  231. int rc;
  232. cmd = cyttsp5_get_commands();
  233. if (!cmd)
  234. return -EINVAL;
  235. /* get context and debug print buffers */
  236. dd = kzalloc(sizeof(*dd), GFP_KERNEL);
  237. if (!dd) {
  238. dev_err(dev, "%s: Error, kzalloc\n", __func__);
  239. rc = -ENOMEM;
  240. goto cyttsp5_debug_probe_alloc_failed;
  241. }
  242. rc = device_create_file(dev, &dev_attr_int_count);
  243. if (rc) {
  244. dev_err(dev, "%s: Error, could not create int_count\n",
  245. __func__);
  246. goto cyttsp5_debug_probe_create_int_count_failed;
  247. }
  248. rc = device_create_file(dev, &dev_attr_formated_output);
  249. if (rc) {
  250. dev_err(dev, "%s: Error, could not create formated_output\n",
  251. __func__);
  252. goto cyttsp5_debug_probe_create_formated_failed;
  253. }
  254. mutex_init(&dd->sysfs_lock);
  255. dd->dev = dev;
  256. cd->cyttsp5_dynamic_data[CY_MODULE_DEBUG] = dd;
  257. dd->si = cmd->request_sysinfo(dev);
  258. if (!dd->si) {
  259. dev_err(dev, "%s: Fail get sysinfo pointer from core\n",
  260. __func__);
  261. rc = -ENODEV;
  262. goto cyttsp5_debug_probe_sysinfo_failed;
  263. }
  264. rc = cmd->subscribe_attention(dev, CY_ATTEN_IRQ, CY_MODULE_DEBUG,
  265. cyttsp5_debug_attention, CY_MODE_OPERATIONAL);
  266. if (rc < 0) {
  267. dev_err(dev, "%s: Error, could not subscribe attention cb\n",
  268. __func__);
  269. goto cyttsp5_debug_probe_subscribe_failed;
  270. }
  271. return 0;
  272. cyttsp5_debug_probe_subscribe_failed:
  273. cyttsp5_debug_probe_sysinfo_failed:
  274. device_remove_file(dev, &dev_attr_formated_output);
  275. cyttsp5_debug_probe_create_formated_failed:
  276. device_remove_file(dev, &dev_attr_int_count);
  277. cyttsp5_debug_probe_create_int_count_failed:
  278. kfree(dd);
  279. cyttsp5_debug_probe_alloc_failed:
  280. dev_err(dev, "%s failed.\n", __func__);
  281. return rc;
  282. }
  283. EXPORT_SYMBOL(cyttsp5_debug_probe);
  284. int cyttsp5_debug_release(struct device *dev)
  285. {
  286. struct cyttsp5_debug_data *dd = cyttsp5_get_debug_data(dev);
  287. int rc;
  288. rc = cmd->unsubscribe_attention(dev, CY_ATTEN_IRQ, CY_MODULE_DEBUG,
  289. cyttsp5_debug_attention, CY_MODE_OPERATIONAL);
  290. if (rc < 0) {
  291. dev_err(dev, "%s: Error, could not un-subscribe attention\n",
  292. __func__);
  293. goto cyttsp5_debug_release_exit;
  294. }
  295. cyttsp5_debug_release_exit:
  296. device_remove_file(dev, &dev_attr_formated_output);
  297. device_remove_file(dev, &dev_attr_int_count);
  298. kfree(dd);
  299. return rc;
  300. }
  301. EXPORT_SYMBOL(cyttsp5_debug_release);