cyttsp4_core.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177
  1. /*
  2. * cyttsp4_core.c
  3. * Cypress TrueTouch(TM) Standard Product V4 Core driver module.
  4. * For use with Cypress Txx4xx parts.
  5. * Supported parts include:
  6. * TMA4XX
  7. * TMA1036
  8. *
  9. * Copyright (C) 2012 Cypress Semiconductor
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * version 2, and only version 2, as published by the
  14. * Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * Contact Cypress Semiconductor at www.cypress.com <ttdrivers@cypress.com>
  22. *
  23. */
  24. #include "cyttsp4_core.h"
  25. #include <linux/delay.h>
  26. #include <linux/gpio.h>
  27. #include <linux/input/mt.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. /* Timeout in ms. */
  33. #define CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT 500
  34. #define CY_CORE_SLEEP_REQUEST_EXCLUSIVE_TIMEOUT 5000
  35. #define CY_CORE_MODE_CHANGE_TIMEOUT 1000
  36. #define CY_CORE_RESET_AND_WAIT_TIMEOUT 500
  37. #define CY_CORE_WAKEUP_TIMEOUT 500
  38. #define CY_CORE_STARTUP_RETRY_COUNT 3
  39. static const u8 ldr_exit[] = {
  40. 0xFF, 0x01, 0x3B, 0x00, 0x00, 0x4F, 0x6D, 0x17
  41. };
  42. static const u8 ldr_err_app[] = {
  43. 0x01, 0x02, 0x00, 0x00, 0x55, 0xDD, 0x17
  44. };
  45. static inline size_t merge_bytes(u8 high, u8 low)
  46. {
  47. return (high << 8) + low;
  48. }
  49. #ifdef VERBOSE_DEBUG
  50. static void cyttsp4_pr_buf(struct device *dev, u8 *pr_buf, u8 *dptr, int size,
  51. const char *data_name)
  52. {
  53. int i, k;
  54. const char fmt[] = "%02X ";
  55. int max;
  56. if (!size)
  57. return;
  58. max = (CY_MAX_PRBUF_SIZE - 1) - sizeof(CY_PR_TRUNCATED);
  59. pr_buf[0] = 0;
  60. for (i = k = 0; i < size && k < max; i++, k += 3)
  61. scnprintf(pr_buf + k, CY_MAX_PRBUF_SIZE, fmt, dptr[i]);
  62. dev_vdbg(dev, "%s: %s[0..%d]=%s%s\n", __func__, data_name, size - 1,
  63. pr_buf, size <= max ? "" : CY_PR_TRUNCATED);
  64. }
  65. #else
  66. #define cyttsp4_pr_buf(dev, pr_buf, dptr, size, data_name) do { } while (0)
  67. #endif
  68. static int cyttsp4_load_status_regs(struct cyttsp4 *cd)
  69. {
  70. struct cyttsp4_sysinfo *si = &cd->sysinfo;
  71. struct device *dev = cd->dev;
  72. int rc;
  73. rc = cyttsp4_adap_read(cd, CY_REG_BASE, si->si_ofs.mode_size,
  74. si->xy_mode);
  75. if (rc < 0)
  76. dev_err(dev, "%s: fail read mode regs r=%d\n",
  77. __func__, rc);
  78. else
  79. cyttsp4_pr_buf(dev, cd->pr_buf, si->xy_mode,
  80. si->si_ofs.mode_size, "xy_mode");
  81. return rc;
  82. }
  83. static int cyttsp4_handshake(struct cyttsp4 *cd, u8 mode)
  84. {
  85. u8 cmd = mode ^ CY_HST_TOGGLE;
  86. int rc;
  87. /*
  88. * Mode change issued, handshaking now will cause endless mode change
  89. * requests, for sync mode modechange will do same with handshake
  90. * */
  91. if (mode & CY_HST_MODE_CHANGE)
  92. return 0;
  93. rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(cmd), &cmd);
  94. if (rc < 0)
  95. dev_err(cd->dev, "%s: bus write fail on handshake (ret=%d)\n",
  96. __func__, rc);
  97. return rc;
  98. }
  99. static int cyttsp4_hw_soft_reset(struct cyttsp4 *cd)
  100. {
  101. u8 cmd = CY_HST_RESET;
  102. int rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(cmd), &cmd);
  103. if (rc < 0) {
  104. dev_err(cd->dev, "%s: FAILED to execute SOFT reset\n",
  105. __func__);
  106. return rc;
  107. }
  108. return 0;
  109. }
  110. static int cyttsp4_hw_hard_reset(struct cyttsp4 *cd)
  111. {
  112. if (cd->cpdata->xres) {
  113. cd->cpdata->xres(cd->cpdata, cd->dev);
  114. dev_dbg(cd->dev, "%s: execute HARD reset\n", __func__);
  115. return 0;
  116. }
  117. dev_err(cd->dev, "%s: FAILED to execute HARD reset\n", __func__);
  118. return -ENOSYS;
  119. }
  120. static int cyttsp4_hw_reset(struct cyttsp4 *cd)
  121. {
  122. int rc = cyttsp4_hw_hard_reset(cd);
  123. if (rc == -ENOSYS)
  124. rc = cyttsp4_hw_soft_reset(cd);
  125. return rc;
  126. }
  127. /*
  128. * Gets number of bits for a touch filed as parameter,
  129. * sets maximum value for field which is used as bit mask
  130. * and returns number of bytes required for that field
  131. */
  132. static int cyttsp4_bits_2_bytes(unsigned int nbits, size_t *max)
  133. {
  134. *max = 1UL << nbits;
  135. return (nbits + 7) / 8;
  136. }
  137. static int cyttsp4_si_data_offsets(struct cyttsp4 *cd)
  138. {
  139. struct cyttsp4_sysinfo *si = &cd->sysinfo;
  140. int rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(si->si_data),
  141. &si->si_data);
  142. if (rc < 0) {
  143. dev_err(cd->dev, "%s: fail read sysinfo data offsets r=%d\n",
  144. __func__, rc);
  145. return rc;
  146. }
  147. /* Print sysinfo data offsets */
  148. cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)&si->si_data,
  149. sizeof(si->si_data), "sysinfo_data_offsets");
  150. /* convert sysinfo data offset bytes into integers */
  151. si->si_ofs.map_sz = merge_bytes(si->si_data.map_szh,
  152. si->si_data.map_szl);
  153. si->si_ofs.map_sz = merge_bytes(si->si_data.map_szh,
  154. si->si_data.map_szl);
  155. si->si_ofs.cydata_ofs = merge_bytes(si->si_data.cydata_ofsh,
  156. si->si_data.cydata_ofsl);
  157. si->si_ofs.test_ofs = merge_bytes(si->si_data.test_ofsh,
  158. si->si_data.test_ofsl);
  159. si->si_ofs.pcfg_ofs = merge_bytes(si->si_data.pcfg_ofsh,
  160. si->si_data.pcfg_ofsl);
  161. si->si_ofs.opcfg_ofs = merge_bytes(si->si_data.opcfg_ofsh,
  162. si->si_data.opcfg_ofsl);
  163. si->si_ofs.ddata_ofs = merge_bytes(si->si_data.ddata_ofsh,
  164. si->si_data.ddata_ofsl);
  165. si->si_ofs.mdata_ofs = merge_bytes(si->si_data.mdata_ofsh,
  166. si->si_data.mdata_ofsl);
  167. return rc;
  168. }
  169. static int cyttsp4_si_get_cydata(struct cyttsp4 *cd)
  170. {
  171. struct cyttsp4_sysinfo *si = &cd->sysinfo;
  172. int read_offset;
  173. int mfgid_sz, calc_mfgid_sz;
  174. void *p;
  175. int rc;
  176. if (si->si_ofs.test_ofs <= si->si_ofs.cydata_ofs) {
  177. dev_err(cd->dev,
  178. "%s: invalid offset test_ofs: %zu, cydata_ofs: %zu\n",
  179. __func__, si->si_ofs.test_ofs, si->si_ofs.cydata_ofs);
  180. return -EINVAL;
  181. }
  182. si->si_ofs.cydata_size = si->si_ofs.test_ofs - si->si_ofs.cydata_ofs;
  183. dev_dbg(cd->dev, "%s: cydata size: %zd\n", __func__,
  184. si->si_ofs.cydata_size);
  185. p = krealloc(si->si_ptrs.cydata, si->si_ofs.cydata_size, GFP_KERNEL);
  186. if (p == NULL) {
  187. dev_err(cd->dev, "%s: failed to allocate cydata memory\n",
  188. __func__);
  189. return -ENOMEM;
  190. }
  191. si->si_ptrs.cydata = p;
  192. read_offset = si->si_ofs.cydata_ofs;
  193. /* Read the CYDA registers up to MFGID field */
  194. rc = cyttsp4_adap_read(cd, read_offset,
  195. offsetof(struct cyttsp4_cydata, mfgid_sz)
  196. + sizeof(si->si_ptrs.cydata->mfgid_sz),
  197. si->si_ptrs.cydata);
  198. if (rc < 0) {
  199. dev_err(cd->dev, "%s: fail read cydata r=%d\n",
  200. __func__, rc);
  201. return rc;
  202. }
  203. /* Check MFGID size */
  204. mfgid_sz = si->si_ptrs.cydata->mfgid_sz;
  205. calc_mfgid_sz = si->si_ofs.cydata_size - sizeof(struct cyttsp4_cydata);
  206. if (mfgid_sz != calc_mfgid_sz) {
  207. dev_err(cd->dev, "%s: mismatch in MFGID size, reported:%d calculated:%d\n",
  208. __func__, mfgid_sz, calc_mfgid_sz);
  209. return -EINVAL;
  210. }
  211. read_offset += offsetof(struct cyttsp4_cydata, mfgid_sz)
  212. + sizeof(si->si_ptrs.cydata->mfgid_sz);
  213. /* Read the CYDA registers for MFGID field */
  214. rc = cyttsp4_adap_read(cd, read_offset, si->si_ptrs.cydata->mfgid_sz,
  215. si->si_ptrs.cydata->mfg_id);
  216. if (rc < 0) {
  217. dev_err(cd->dev, "%s: fail read cydata r=%d\n",
  218. __func__, rc);
  219. return rc;
  220. }
  221. read_offset += si->si_ptrs.cydata->mfgid_sz;
  222. /* Read the rest of the CYDA registers */
  223. rc = cyttsp4_adap_read(cd, read_offset,
  224. sizeof(struct cyttsp4_cydata)
  225. - offsetof(struct cyttsp4_cydata, cyito_idh),
  226. &si->si_ptrs.cydata->cyito_idh);
  227. if (rc < 0) {
  228. dev_err(cd->dev, "%s: fail read cydata r=%d\n",
  229. __func__, rc);
  230. return rc;
  231. }
  232. cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)si->si_ptrs.cydata,
  233. si->si_ofs.cydata_size, "sysinfo_cydata");
  234. return rc;
  235. }
  236. static int cyttsp4_si_get_test_data(struct cyttsp4 *cd)
  237. {
  238. struct cyttsp4_sysinfo *si = &cd->sysinfo;
  239. void *p;
  240. int rc;
  241. if (si->si_ofs.pcfg_ofs <= si->si_ofs.test_ofs) {
  242. dev_err(cd->dev,
  243. "%s: invalid offset pcfg_ofs: %zu, test_ofs: %zu\n",
  244. __func__, si->si_ofs.pcfg_ofs, si->si_ofs.test_ofs);
  245. return -EINVAL;
  246. }
  247. si->si_ofs.test_size = si->si_ofs.pcfg_ofs - si->si_ofs.test_ofs;
  248. p = krealloc(si->si_ptrs.test, si->si_ofs.test_size, GFP_KERNEL);
  249. if (p == NULL) {
  250. dev_err(cd->dev, "%s: failed to allocate test memory\n",
  251. __func__);
  252. return -ENOMEM;
  253. }
  254. si->si_ptrs.test = p;
  255. rc = cyttsp4_adap_read(cd, si->si_ofs.test_ofs, si->si_ofs.test_size,
  256. si->si_ptrs.test);
  257. if (rc < 0) {
  258. dev_err(cd->dev, "%s: fail read test data r=%d\n",
  259. __func__, rc);
  260. return rc;
  261. }
  262. cyttsp4_pr_buf(cd->dev, cd->pr_buf,
  263. (u8 *)si->si_ptrs.test, si->si_ofs.test_size,
  264. "sysinfo_test_data");
  265. if (si->si_ptrs.test->post_codel &
  266. CY_POST_CODEL_WDG_RST)
  267. dev_info(cd->dev, "%s: %s codel=%02X\n",
  268. __func__, "Reset was a WATCHDOG RESET",
  269. si->si_ptrs.test->post_codel);
  270. if (!(si->si_ptrs.test->post_codel &
  271. CY_POST_CODEL_CFG_DATA_CRC_FAIL))
  272. dev_info(cd->dev, "%s: %s codel=%02X\n", __func__,
  273. "Config Data CRC FAIL",
  274. si->si_ptrs.test->post_codel);
  275. if (!(si->si_ptrs.test->post_codel &
  276. CY_POST_CODEL_PANEL_TEST_FAIL))
  277. dev_info(cd->dev, "%s: %s codel=%02X\n",
  278. __func__, "PANEL TEST FAIL",
  279. si->si_ptrs.test->post_codel);
  280. dev_info(cd->dev, "%s: SCANNING is %s codel=%02X\n",
  281. __func__, si->si_ptrs.test->post_codel & 0x08 ?
  282. "ENABLED" : "DISABLED",
  283. si->si_ptrs.test->post_codel);
  284. return rc;
  285. }
  286. static int cyttsp4_si_get_pcfg_data(struct cyttsp4 *cd)
  287. {
  288. struct cyttsp4_sysinfo *si = &cd->sysinfo;
  289. void *p;
  290. int rc;
  291. if (si->si_ofs.opcfg_ofs <= si->si_ofs.pcfg_ofs) {
  292. dev_err(cd->dev,
  293. "%s: invalid offset opcfg_ofs: %zu, pcfg_ofs: %zu\n",
  294. __func__, si->si_ofs.opcfg_ofs, si->si_ofs.pcfg_ofs);
  295. return -EINVAL;
  296. }
  297. si->si_ofs.pcfg_size = si->si_ofs.opcfg_ofs - si->si_ofs.pcfg_ofs;
  298. p = krealloc(si->si_ptrs.pcfg, si->si_ofs.pcfg_size, GFP_KERNEL);
  299. if (p == NULL) {
  300. dev_err(cd->dev, "%s: failed to allocate pcfg memory\n",
  301. __func__);
  302. return -ENOMEM;
  303. }
  304. si->si_ptrs.pcfg = p;
  305. rc = cyttsp4_adap_read(cd, si->si_ofs.pcfg_ofs, si->si_ofs.pcfg_size,
  306. si->si_ptrs.pcfg);
  307. if (rc < 0) {
  308. dev_err(cd->dev, "%s: fail read pcfg data r=%d\n",
  309. __func__, rc);
  310. return rc;
  311. }
  312. si->si_ofs.max_x = merge_bytes((si->si_ptrs.pcfg->res_xh
  313. & CY_PCFG_RESOLUTION_X_MASK), si->si_ptrs.pcfg->res_xl);
  314. si->si_ofs.x_origin = !!(si->si_ptrs.pcfg->res_xh
  315. & CY_PCFG_ORIGIN_X_MASK);
  316. si->si_ofs.max_y = merge_bytes((si->si_ptrs.pcfg->res_yh
  317. & CY_PCFG_RESOLUTION_Y_MASK), si->si_ptrs.pcfg->res_yl);
  318. si->si_ofs.y_origin = !!(si->si_ptrs.pcfg->res_yh
  319. & CY_PCFG_ORIGIN_Y_MASK);
  320. si->si_ofs.max_p = merge_bytes(si->si_ptrs.pcfg->max_zh,
  321. si->si_ptrs.pcfg->max_zl);
  322. cyttsp4_pr_buf(cd->dev, cd->pr_buf,
  323. (u8 *)si->si_ptrs.pcfg,
  324. si->si_ofs.pcfg_size, "sysinfo_pcfg_data");
  325. return rc;
  326. }
  327. static int cyttsp4_si_get_opcfg_data(struct cyttsp4 *cd)
  328. {
  329. struct cyttsp4_sysinfo *si = &cd->sysinfo;
  330. struct cyttsp4_tch_abs_params *tch;
  331. struct cyttsp4_tch_rec_params *tch_old, *tch_new;
  332. enum cyttsp4_tch_abs abs;
  333. int i;
  334. void *p;
  335. int rc;
  336. if (si->si_ofs.ddata_ofs <= si->si_ofs.opcfg_ofs) {
  337. dev_err(cd->dev,
  338. "%s: invalid offset ddata_ofs: %zu, opcfg_ofs: %zu\n",
  339. __func__, si->si_ofs.ddata_ofs, si->si_ofs.opcfg_ofs);
  340. return -EINVAL;
  341. }
  342. si->si_ofs.opcfg_size = si->si_ofs.ddata_ofs - si->si_ofs.opcfg_ofs;
  343. p = krealloc(si->si_ptrs.opcfg, si->si_ofs.opcfg_size, GFP_KERNEL);
  344. if (p == NULL) {
  345. dev_err(cd->dev, "%s: failed to allocate opcfg memory\n",
  346. __func__);
  347. return -ENOMEM;
  348. }
  349. si->si_ptrs.opcfg = p;
  350. rc = cyttsp4_adap_read(cd, si->si_ofs.opcfg_ofs, si->si_ofs.opcfg_size,
  351. si->si_ptrs.opcfg);
  352. if (rc < 0) {
  353. dev_err(cd->dev, "%s: fail read opcfg data r=%d\n",
  354. __func__, rc);
  355. return rc;
  356. }
  357. si->si_ofs.cmd_ofs = si->si_ptrs.opcfg->cmd_ofs;
  358. si->si_ofs.rep_ofs = si->si_ptrs.opcfg->rep_ofs;
  359. si->si_ofs.rep_sz = (si->si_ptrs.opcfg->rep_szh * 256) +
  360. si->si_ptrs.opcfg->rep_szl;
  361. si->si_ofs.num_btns = si->si_ptrs.opcfg->num_btns;
  362. si->si_ofs.num_btn_regs = (si->si_ofs.num_btns +
  363. CY_NUM_BTN_PER_REG - 1) / CY_NUM_BTN_PER_REG;
  364. si->si_ofs.tt_stat_ofs = si->si_ptrs.opcfg->tt_stat_ofs;
  365. si->si_ofs.obj_cfg0 = si->si_ptrs.opcfg->obj_cfg0;
  366. si->si_ofs.max_tchs = si->si_ptrs.opcfg->max_tchs &
  367. CY_BYTE_OFS_MASK;
  368. si->si_ofs.tch_rec_size = si->si_ptrs.opcfg->tch_rec_size &
  369. CY_BYTE_OFS_MASK;
  370. /* Get the old touch fields */
  371. for (abs = CY_TCH_X; abs < CY_NUM_TCH_FIELDS; abs++) {
  372. tch = &si->si_ofs.tch_abs[abs];
  373. tch_old = &si->si_ptrs.opcfg->tch_rec_old[abs];
  374. tch->ofs = tch_old->loc & CY_BYTE_OFS_MASK;
  375. tch->size = cyttsp4_bits_2_bytes(tch_old->size,
  376. &tch->max);
  377. tch->bofs = (tch_old->loc & CY_BOFS_MASK) >> CY_BOFS_SHIFT;
  378. }
  379. /* button fields */
  380. si->si_ofs.btn_rec_size = si->si_ptrs.opcfg->btn_rec_size;
  381. si->si_ofs.btn_diff_ofs = si->si_ptrs.opcfg->btn_diff_ofs;
  382. si->si_ofs.btn_diff_size = si->si_ptrs.opcfg->btn_diff_size;
  383. if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE) {
  384. /* Get the extended touch fields */
  385. for (i = 0; i < CY_NUM_EXT_TCH_FIELDS; abs++, i++) {
  386. tch = &si->si_ofs.tch_abs[abs];
  387. tch_new = &si->si_ptrs.opcfg->tch_rec_new[i];
  388. tch->ofs = tch_new->loc & CY_BYTE_OFS_MASK;
  389. tch->size = cyttsp4_bits_2_bytes(tch_new->size,
  390. &tch->max);
  391. tch->bofs = (tch_new->loc & CY_BOFS_MASK) >> CY_BOFS_SHIFT;
  392. }
  393. }
  394. for (abs = 0; abs < CY_TCH_NUM_ABS; abs++) {
  395. dev_dbg(cd->dev, "%s: tch_rec_%s\n", __func__,
  396. cyttsp4_tch_abs_string[abs]);
  397. dev_dbg(cd->dev, "%s: ofs =%2zd\n", __func__,
  398. si->si_ofs.tch_abs[abs].ofs);
  399. dev_dbg(cd->dev, "%s: siz =%2zd\n", __func__,
  400. si->si_ofs.tch_abs[abs].size);
  401. dev_dbg(cd->dev, "%s: max =%2zd\n", __func__,
  402. si->si_ofs.tch_abs[abs].max);
  403. dev_dbg(cd->dev, "%s: bofs=%2zd\n", __func__,
  404. si->si_ofs.tch_abs[abs].bofs);
  405. }
  406. si->si_ofs.mode_size = si->si_ofs.tt_stat_ofs + 1;
  407. si->si_ofs.data_size = si->si_ofs.max_tchs *
  408. si->si_ptrs.opcfg->tch_rec_size;
  409. cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)si->si_ptrs.opcfg,
  410. si->si_ofs.opcfg_size, "sysinfo_opcfg_data");
  411. return 0;
  412. }
  413. static int cyttsp4_si_get_ddata(struct cyttsp4 *cd)
  414. {
  415. struct cyttsp4_sysinfo *si = &cd->sysinfo;
  416. void *p;
  417. int rc;
  418. si->si_ofs.ddata_size = si->si_ofs.mdata_ofs - si->si_ofs.ddata_ofs;
  419. p = krealloc(si->si_ptrs.ddata, si->si_ofs.ddata_size, GFP_KERNEL);
  420. if (p == NULL) {
  421. dev_err(cd->dev, "%s: fail alloc ddata memory\n", __func__);
  422. return -ENOMEM;
  423. }
  424. si->si_ptrs.ddata = p;
  425. rc = cyttsp4_adap_read(cd, si->si_ofs.ddata_ofs, si->si_ofs.ddata_size,
  426. si->si_ptrs.ddata);
  427. if (rc < 0)
  428. dev_err(cd->dev, "%s: fail read ddata data r=%d\n",
  429. __func__, rc);
  430. else
  431. cyttsp4_pr_buf(cd->dev, cd->pr_buf,
  432. (u8 *)si->si_ptrs.ddata,
  433. si->si_ofs.ddata_size, "sysinfo_ddata");
  434. return rc;
  435. }
  436. static int cyttsp4_si_get_mdata(struct cyttsp4 *cd)
  437. {
  438. struct cyttsp4_sysinfo *si = &cd->sysinfo;
  439. void *p;
  440. int rc;
  441. si->si_ofs.mdata_size = si->si_ofs.map_sz - si->si_ofs.mdata_ofs;
  442. p = krealloc(si->si_ptrs.mdata, si->si_ofs.mdata_size, GFP_KERNEL);
  443. if (p == NULL) {
  444. dev_err(cd->dev, "%s: fail alloc mdata memory\n", __func__);
  445. return -ENOMEM;
  446. }
  447. si->si_ptrs.mdata = p;
  448. rc = cyttsp4_adap_read(cd, si->si_ofs.mdata_ofs, si->si_ofs.mdata_size,
  449. si->si_ptrs.mdata);
  450. if (rc < 0)
  451. dev_err(cd->dev, "%s: fail read mdata data r=%d\n",
  452. __func__, rc);
  453. else
  454. cyttsp4_pr_buf(cd->dev, cd->pr_buf,
  455. (u8 *)si->si_ptrs.mdata,
  456. si->si_ofs.mdata_size, "sysinfo_mdata");
  457. return rc;
  458. }
  459. static int cyttsp4_si_get_btn_data(struct cyttsp4 *cd)
  460. {
  461. struct cyttsp4_sysinfo *si = &cd->sysinfo;
  462. int btn;
  463. int num_defined_keys;
  464. u16 *key_table;
  465. void *p;
  466. int rc = 0;
  467. if (si->si_ofs.num_btns) {
  468. si->si_ofs.btn_keys_size = si->si_ofs.num_btns *
  469. sizeof(struct cyttsp4_btn);
  470. p = krealloc(si->btn, si->si_ofs.btn_keys_size,
  471. GFP_KERNEL|__GFP_ZERO);
  472. if (p == NULL) {
  473. dev_err(cd->dev, "%s: %s\n", __func__,
  474. "fail alloc btn_keys memory");
  475. return -ENOMEM;
  476. }
  477. si->btn = p;
  478. if (cd->cpdata->sett[CY_IC_GRPNUM_BTN_KEYS] == NULL)
  479. num_defined_keys = 0;
  480. else if (cd->cpdata->sett[CY_IC_GRPNUM_BTN_KEYS]->data == NULL)
  481. num_defined_keys = 0;
  482. else
  483. num_defined_keys = cd->cpdata->sett
  484. [CY_IC_GRPNUM_BTN_KEYS]->size;
  485. for (btn = 0; btn < si->si_ofs.num_btns &&
  486. btn < num_defined_keys; btn++) {
  487. key_table = (u16 *)cd->cpdata->sett
  488. [CY_IC_GRPNUM_BTN_KEYS]->data;
  489. si->btn[btn].key_code = key_table[btn];
  490. si->btn[btn].state = CY_BTN_RELEASED;
  491. si->btn[btn].enabled = true;
  492. }
  493. for (; btn < si->si_ofs.num_btns; btn++) {
  494. si->btn[btn].key_code = KEY_RESERVED;
  495. si->btn[btn].state = CY_BTN_RELEASED;
  496. si->btn[btn].enabled = true;
  497. }
  498. return rc;
  499. }
  500. si->si_ofs.btn_keys_size = 0;
  501. kfree(si->btn);
  502. si->btn = NULL;
  503. return rc;
  504. }
  505. static int cyttsp4_si_get_op_data_ptrs(struct cyttsp4 *cd)
  506. {
  507. struct cyttsp4_sysinfo *si = &cd->sysinfo;
  508. void *p;
  509. p = krealloc(si->xy_mode, si->si_ofs.mode_size, GFP_KERNEL|__GFP_ZERO);
  510. if (p == NULL)
  511. return -ENOMEM;
  512. si->xy_mode = p;
  513. p = krealloc(si->xy_data, si->si_ofs.data_size, GFP_KERNEL|__GFP_ZERO);
  514. if (p == NULL)
  515. return -ENOMEM;
  516. si->xy_data = p;
  517. p = krealloc(si->btn_rec_data,
  518. si->si_ofs.btn_rec_size * si->si_ofs.num_btns,
  519. GFP_KERNEL|__GFP_ZERO);
  520. if (p == NULL)
  521. return -ENOMEM;
  522. si->btn_rec_data = p;
  523. return 0;
  524. }
  525. static void cyttsp4_si_put_log_data(struct cyttsp4 *cd)
  526. {
  527. struct cyttsp4_sysinfo *si = &cd->sysinfo;
  528. dev_dbg(cd->dev, "%s: cydata_ofs =%4zd siz=%4zd\n", __func__,
  529. si->si_ofs.cydata_ofs, si->si_ofs.cydata_size);
  530. dev_dbg(cd->dev, "%s: test_ofs =%4zd siz=%4zd\n", __func__,
  531. si->si_ofs.test_ofs, si->si_ofs.test_size);
  532. dev_dbg(cd->dev, "%s: pcfg_ofs =%4zd siz=%4zd\n", __func__,
  533. si->si_ofs.pcfg_ofs, si->si_ofs.pcfg_size);
  534. dev_dbg(cd->dev, "%s: opcfg_ofs =%4zd siz=%4zd\n", __func__,
  535. si->si_ofs.opcfg_ofs, si->si_ofs.opcfg_size);
  536. dev_dbg(cd->dev, "%s: ddata_ofs =%4zd siz=%4zd\n", __func__,
  537. si->si_ofs.ddata_ofs, si->si_ofs.ddata_size);
  538. dev_dbg(cd->dev, "%s: mdata_ofs =%4zd siz=%4zd\n", __func__,
  539. si->si_ofs.mdata_ofs, si->si_ofs.mdata_size);
  540. dev_dbg(cd->dev, "%s: cmd_ofs =%4zd\n", __func__,
  541. si->si_ofs.cmd_ofs);
  542. dev_dbg(cd->dev, "%s: rep_ofs =%4zd\n", __func__,
  543. si->si_ofs.rep_ofs);
  544. dev_dbg(cd->dev, "%s: rep_sz =%4zd\n", __func__,
  545. si->si_ofs.rep_sz);
  546. dev_dbg(cd->dev, "%s: num_btns =%4zd\n", __func__,
  547. si->si_ofs.num_btns);
  548. dev_dbg(cd->dev, "%s: num_btn_regs =%4zd\n", __func__,
  549. si->si_ofs.num_btn_regs);
  550. dev_dbg(cd->dev, "%s: tt_stat_ofs =%4zd\n", __func__,
  551. si->si_ofs.tt_stat_ofs);
  552. dev_dbg(cd->dev, "%s: tch_rec_size =%4zd\n", __func__,
  553. si->si_ofs.tch_rec_size);
  554. dev_dbg(cd->dev, "%s: max_tchs =%4zd\n", __func__,
  555. si->si_ofs.max_tchs);
  556. dev_dbg(cd->dev, "%s: mode_size =%4zd\n", __func__,
  557. si->si_ofs.mode_size);
  558. dev_dbg(cd->dev, "%s: data_size =%4zd\n", __func__,
  559. si->si_ofs.data_size);
  560. dev_dbg(cd->dev, "%s: map_sz =%4zd\n", __func__,
  561. si->si_ofs.map_sz);
  562. dev_dbg(cd->dev, "%s: btn_rec_size =%2zd\n", __func__,
  563. si->si_ofs.btn_rec_size);
  564. dev_dbg(cd->dev, "%s: btn_diff_ofs =%2zd\n", __func__,
  565. si->si_ofs.btn_diff_ofs);
  566. dev_dbg(cd->dev, "%s: btn_diff_size =%2zd\n", __func__,
  567. si->si_ofs.btn_diff_size);
  568. dev_dbg(cd->dev, "%s: max_x = 0x%04zX (%zd)\n", __func__,
  569. si->si_ofs.max_x, si->si_ofs.max_x);
  570. dev_dbg(cd->dev, "%s: x_origin = %zd (%s)\n", __func__,
  571. si->si_ofs.x_origin,
  572. si->si_ofs.x_origin == CY_NORMAL_ORIGIN ?
  573. "left corner" : "right corner");
  574. dev_dbg(cd->dev, "%s: max_y = 0x%04zX (%zd)\n", __func__,
  575. si->si_ofs.max_y, si->si_ofs.max_y);
  576. dev_dbg(cd->dev, "%s: y_origin = %zd (%s)\n", __func__,
  577. si->si_ofs.y_origin,
  578. si->si_ofs.y_origin == CY_NORMAL_ORIGIN ?
  579. "upper corner" : "lower corner");
  580. dev_dbg(cd->dev, "%s: max_p = 0x%04zX (%zd)\n", __func__,
  581. si->si_ofs.max_p, si->si_ofs.max_p);
  582. dev_dbg(cd->dev, "%s: xy_mode=%p xy_data=%p\n", __func__,
  583. si->xy_mode, si->xy_data);
  584. }
  585. static int cyttsp4_get_sysinfo_regs(struct cyttsp4 *cd)
  586. {
  587. struct cyttsp4_sysinfo *si = &cd->sysinfo;
  588. int rc;
  589. rc = cyttsp4_si_data_offsets(cd);
  590. if (rc < 0)
  591. return rc;
  592. rc = cyttsp4_si_get_cydata(cd);
  593. if (rc < 0)
  594. return rc;
  595. rc = cyttsp4_si_get_test_data(cd);
  596. if (rc < 0)
  597. return rc;
  598. rc = cyttsp4_si_get_pcfg_data(cd);
  599. if (rc < 0)
  600. return rc;
  601. rc = cyttsp4_si_get_opcfg_data(cd);
  602. if (rc < 0)
  603. return rc;
  604. rc = cyttsp4_si_get_ddata(cd);
  605. if (rc < 0)
  606. return rc;
  607. rc = cyttsp4_si_get_mdata(cd);
  608. if (rc < 0)
  609. return rc;
  610. rc = cyttsp4_si_get_btn_data(cd);
  611. if (rc < 0)
  612. return rc;
  613. rc = cyttsp4_si_get_op_data_ptrs(cd);
  614. if (rc < 0) {
  615. dev_err(cd->dev, "%s: failed to get_op_data\n",
  616. __func__);
  617. return rc;
  618. }
  619. cyttsp4_si_put_log_data(cd);
  620. /* provide flow control handshake */
  621. rc = cyttsp4_handshake(cd, si->si_data.hst_mode);
  622. if (rc < 0)
  623. dev_err(cd->dev, "%s: handshake fail on sysinfo reg\n",
  624. __func__);
  625. si->ready = true;
  626. return rc;
  627. }
  628. static void cyttsp4_queue_startup_(struct cyttsp4 *cd)
  629. {
  630. if (cd->startup_state == STARTUP_NONE) {
  631. cd->startup_state = STARTUP_QUEUED;
  632. schedule_work(&cd->startup_work);
  633. dev_dbg(cd->dev, "%s: cyttsp4_startup queued\n", __func__);
  634. } else {
  635. dev_dbg(cd->dev, "%s: startup_state = %d\n", __func__,
  636. cd->startup_state);
  637. }
  638. }
  639. static void cyttsp4_report_slot_liftoff(struct cyttsp4_mt_data *md,
  640. int max_slots)
  641. {
  642. int t;
  643. if (md->num_prv_tch == 0)
  644. return;
  645. for (t = 0; t < max_slots; t++) {
  646. input_mt_slot(md->input, t);
  647. input_mt_report_slot_state(md->input,
  648. MT_TOOL_FINGER, false);
  649. }
  650. }
  651. static void cyttsp4_lift_all(struct cyttsp4_mt_data *md)
  652. {
  653. if (!md->si)
  654. return;
  655. if (md->num_prv_tch != 0) {
  656. cyttsp4_report_slot_liftoff(md,
  657. md->si->si_ofs.tch_abs[CY_TCH_T].max);
  658. input_sync(md->input);
  659. md->num_prv_tch = 0;
  660. }
  661. }
  662. static void cyttsp4_get_touch_axis(struct cyttsp4_mt_data *md,
  663. int *axis, int size, int max, u8 *xy_data, int bofs)
  664. {
  665. int nbyte;
  666. int next;
  667. for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
  668. dev_vdbg(&md->input->dev,
  669. "%s: *axis=%02X(%d) size=%d max=%08X xy_data=%p"
  670. " xy_data[%d]=%02X(%d) bofs=%d\n",
  671. __func__, *axis, *axis, size, max, xy_data, next,
  672. xy_data[next], xy_data[next], bofs);
  673. *axis = (*axis * 256) + (xy_data[next] >> bofs);
  674. next++;
  675. }
  676. *axis &= max - 1;
  677. dev_vdbg(&md->input->dev,
  678. "%s: *axis=%02X(%d) size=%d max=%08X xy_data=%p"
  679. " xy_data[%d]=%02X(%d)\n",
  680. __func__, *axis, *axis, size, max, xy_data, next,
  681. xy_data[next], xy_data[next]);
  682. }
  683. static void cyttsp4_get_touch(struct cyttsp4_mt_data *md,
  684. struct cyttsp4_touch *touch, u8 *xy_data)
  685. {
  686. struct device *dev = &md->input->dev;
  687. struct cyttsp4_sysinfo *si = md->si;
  688. enum cyttsp4_tch_abs abs;
  689. bool flipped;
  690. for (abs = CY_TCH_X; abs < CY_TCH_NUM_ABS; abs++) {
  691. cyttsp4_get_touch_axis(md, &touch->abs[abs],
  692. si->si_ofs.tch_abs[abs].size,
  693. si->si_ofs.tch_abs[abs].max,
  694. xy_data + si->si_ofs.tch_abs[abs].ofs,
  695. si->si_ofs.tch_abs[abs].bofs);
  696. dev_vdbg(dev, "%s: get %s=%04X(%d)\n", __func__,
  697. cyttsp4_tch_abs_string[abs],
  698. touch->abs[abs], touch->abs[abs]);
  699. }
  700. if (md->pdata->flags & CY_FLAG_FLIP) {
  701. swap(touch->abs[CY_TCH_X], touch->abs[CY_TCH_Y]);
  702. flipped = true;
  703. } else
  704. flipped = false;
  705. if (md->pdata->flags & CY_FLAG_INV_X) {
  706. if (flipped)
  707. touch->abs[CY_TCH_X] = md->si->si_ofs.max_y -
  708. touch->abs[CY_TCH_X];
  709. else
  710. touch->abs[CY_TCH_X] = md->si->si_ofs.max_x -
  711. touch->abs[CY_TCH_X];
  712. }
  713. if (md->pdata->flags & CY_FLAG_INV_Y) {
  714. if (flipped)
  715. touch->abs[CY_TCH_Y] = md->si->si_ofs.max_x -
  716. touch->abs[CY_TCH_Y];
  717. else
  718. touch->abs[CY_TCH_Y] = md->si->si_ofs.max_y -
  719. touch->abs[CY_TCH_Y];
  720. }
  721. dev_vdbg(dev, "%s: flip=%s inv-x=%s inv-y=%s x=%04X(%d) y=%04X(%d)\n",
  722. __func__, flipped ? "true" : "false",
  723. md->pdata->flags & CY_FLAG_INV_X ? "true" : "false",
  724. md->pdata->flags & CY_FLAG_INV_Y ? "true" : "false",
  725. touch->abs[CY_TCH_X], touch->abs[CY_TCH_X],
  726. touch->abs[CY_TCH_Y], touch->abs[CY_TCH_Y]);
  727. }
  728. static void cyttsp4_final_sync(struct input_dev *input, int max_slots, int *ids)
  729. {
  730. int t;
  731. for (t = 0; t < max_slots; t++) {
  732. if (ids[t])
  733. continue;
  734. input_mt_slot(input, t);
  735. input_mt_report_slot_state(input, MT_TOOL_FINGER, false);
  736. }
  737. input_sync(input);
  738. }
  739. static void cyttsp4_get_mt_touches(struct cyttsp4_mt_data *md, int num_cur_tch)
  740. {
  741. struct device *dev = &md->input->dev;
  742. struct cyttsp4_sysinfo *si = md->si;
  743. struct cyttsp4_touch tch;
  744. int sig;
  745. int i, j, t = 0;
  746. int ids[max(CY_TMA1036_MAX_TCH, CY_TMA4XX_MAX_TCH)];
  747. memset(ids, 0, si->si_ofs.tch_abs[CY_TCH_T].max * sizeof(int));
  748. for (i = 0; i < num_cur_tch; i++) {
  749. cyttsp4_get_touch(md, &tch, si->xy_data +
  750. (i * si->si_ofs.tch_rec_size));
  751. if ((tch.abs[CY_TCH_T] < md->pdata->frmwrk->abs
  752. [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MIN_OST]) ||
  753. (tch.abs[CY_TCH_T] > md->pdata->frmwrk->abs
  754. [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MAX_OST])) {
  755. dev_err(dev, "%s: tch=%d -> bad trk_id=%d max_id=%d\n",
  756. __func__, i, tch.abs[CY_TCH_T],
  757. md->pdata->frmwrk->abs[(CY_ABS_ID_OST *
  758. CY_NUM_ABS_SET) + CY_MAX_OST]);
  759. continue;
  760. }
  761. /* use 0 based track id's */
  762. sig = md->pdata->frmwrk->abs
  763. [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + 0];
  764. if (sig != CY_IGNORE_VALUE) {
  765. t = tch.abs[CY_TCH_T] - md->pdata->frmwrk->abs
  766. [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MIN_OST];
  767. if (tch.abs[CY_TCH_E] == CY_EV_LIFTOFF) {
  768. dev_dbg(dev, "%s: t=%d e=%d lift-off\n",
  769. __func__, t, tch.abs[CY_TCH_E]);
  770. goto cyttsp4_get_mt_touches_pr_tch;
  771. }
  772. input_mt_slot(md->input, t);
  773. input_mt_report_slot_state(md->input, MT_TOOL_FINGER,
  774. true);
  775. ids[t] = true;
  776. }
  777. /* all devices: position and pressure fields */
  778. for (j = 0; j <= CY_ABS_W_OST; j++) {
  779. sig = md->pdata->frmwrk->abs[((CY_ABS_X_OST + j) *
  780. CY_NUM_ABS_SET) + 0];
  781. if (sig != CY_IGNORE_VALUE)
  782. input_report_abs(md->input, sig,
  783. tch.abs[CY_TCH_X + j]);
  784. }
  785. if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE) {
  786. /*
  787. * TMA400 size and orientation fields:
  788. * if pressure is non-zero and major touch
  789. * signal is zero, then set major and minor touch
  790. * signals to minimum non-zero value
  791. */
  792. if (tch.abs[CY_TCH_P] > 0 && tch.abs[CY_TCH_MAJ] == 0)
  793. tch.abs[CY_TCH_MAJ] = tch.abs[CY_TCH_MIN] = 1;
  794. /* Get the extended touch fields */
  795. for (j = 0; j < CY_NUM_EXT_TCH_FIELDS; j++) {
  796. sig = md->pdata->frmwrk->abs
  797. [((CY_ABS_MAJ_OST + j) *
  798. CY_NUM_ABS_SET) + 0];
  799. if (sig != CY_IGNORE_VALUE)
  800. input_report_abs(md->input, sig,
  801. tch.abs[CY_TCH_MAJ + j]);
  802. }
  803. }
  804. cyttsp4_get_mt_touches_pr_tch:
  805. if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE)
  806. dev_dbg(dev,
  807. "%s: t=%d x=%d y=%d z=%d M=%d m=%d o=%d e=%d\n",
  808. __func__, t,
  809. tch.abs[CY_TCH_X],
  810. tch.abs[CY_TCH_Y],
  811. tch.abs[CY_TCH_P],
  812. tch.abs[CY_TCH_MAJ],
  813. tch.abs[CY_TCH_MIN],
  814. tch.abs[CY_TCH_OR],
  815. tch.abs[CY_TCH_E]);
  816. else
  817. dev_dbg(dev,
  818. "%s: t=%d x=%d y=%d z=%d e=%d\n", __func__,
  819. t,
  820. tch.abs[CY_TCH_X],
  821. tch.abs[CY_TCH_Y],
  822. tch.abs[CY_TCH_P],
  823. tch.abs[CY_TCH_E]);
  824. }
  825. cyttsp4_final_sync(md->input, si->si_ofs.tch_abs[CY_TCH_T].max, ids);
  826. md->num_prv_tch = num_cur_tch;
  827. return;
  828. }
  829. /* read xy_data for all current touches */
  830. static int cyttsp4_xy_worker(struct cyttsp4 *cd)
  831. {
  832. struct cyttsp4_mt_data *md = &cd->md;
  833. struct device *dev = &md->input->dev;
  834. struct cyttsp4_sysinfo *si = md->si;
  835. u8 num_cur_tch;
  836. u8 hst_mode;
  837. u8 rep_len;
  838. u8 rep_stat;
  839. u8 tt_stat;
  840. int rc = 0;
  841. /*
  842. * Get event data from cyttsp4 device.
  843. * The event data includes all data
  844. * for all active touches.
  845. * Event data also includes button data
  846. */
  847. /*
  848. * Use 2 reads:
  849. * 1st read to get mode + button bytes + touch count (core)
  850. * 2nd read (optional) to get touch 1 - touch n data
  851. */
  852. hst_mode = si->xy_mode[CY_REG_BASE];
  853. rep_len = si->xy_mode[si->si_ofs.rep_ofs];
  854. rep_stat = si->xy_mode[si->si_ofs.rep_ofs + 1];
  855. tt_stat = si->xy_mode[si->si_ofs.tt_stat_ofs];
  856. dev_vdbg(dev, "%s: %s%02X %s%d %s%02X %s%02X\n", __func__,
  857. "hst_mode=", hst_mode, "rep_len=", rep_len,
  858. "rep_stat=", rep_stat, "tt_stat=", tt_stat);
  859. num_cur_tch = GET_NUM_TOUCHES(tt_stat);
  860. dev_vdbg(dev, "%s: num_cur_tch=%d\n", __func__, num_cur_tch);
  861. if (rep_len == 0 && num_cur_tch > 0) {
  862. dev_err(dev, "%s: report length error rep_len=%d num_tch=%d\n",
  863. __func__, rep_len, num_cur_tch);
  864. goto cyttsp4_xy_worker_exit;
  865. }
  866. /* read touches */
  867. if (num_cur_tch > 0) {
  868. rc = cyttsp4_adap_read(cd, si->si_ofs.tt_stat_ofs + 1,
  869. num_cur_tch * si->si_ofs.tch_rec_size,
  870. si->xy_data);
  871. if (rc < 0) {
  872. dev_err(dev, "%s: read fail on touch regs r=%d\n",
  873. __func__, rc);
  874. goto cyttsp4_xy_worker_exit;
  875. }
  876. }
  877. /* print xy data */
  878. cyttsp4_pr_buf(dev, cd->pr_buf, si->xy_data, num_cur_tch *
  879. si->si_ofs.tch_rec_size, "xy_data");
  880. /* check any error conditions */
  881. if (IS_BAD_PKT(rep_stat)) {
  882. dev_dbg(dev, "%s: Invalid buffer detected\n", __func__);
  883. rc = 0;
  884. goto cyttsp4_xy_worker_exit;
  885. }
  886. if (IS_LARGE_AREA(tt_stat))
  887. dev_dbg(dev, "%s: Large area detected\n", __func__);
  888. if (num_cur_tch > si->si_ofs.max_tchs) {
  889. dev_err(dev, "%s: too many tch; set to max tch (n=%d c=%zd)\n",
  890. __func__, num_cur_tch, si->si_ofs.max_tchs);
  891. num_cur_tch = si->si_ofs.max_tchs;
  892. }
  893. /* extract xy_data for all currently reported touches */
  894. dev_vdbg(dev, "%s: extract data num_cur_tch=%d\n", __func__,
  895. num_cur_tch);
  896. if (num_cur_tch)
  897. cyttsp4_get_mt_touches(md, num_cur_tch);
  898. else
  899. cyttsp4_lift_all(md);
  900. rc = 0;
  901. cyttsp4_xy_worker_exit:
  902. return rc;
  903. }
  904. static int cyttsp4_mt_attention(struct cyttsp4 *cd)
  905. {
  906. struct device *dev = cd->dev;
  907. struct cyttsp4_mt_data *md = &cd->md;
  908. int rc = 0;
  909. if (!md->si)
  910. return 0;
  911. mutex_lock(&md->report_lock);
  912. if (!md->is_suspended) {
  913. /* core handles handshake */
  914. rc = cyttsp4_xy_worker(cd);
  915. } else {
  916. dev_vdbg(dev, "%s: Ignoring report while suspended\n",
  917. __func__);
  918. }
  919. mutex_unlock(&md->report_lock);
  920. if (rc < 0)
  921. dev_err(dev, "%s: xy_worker error r=%d\n", __func__, rc);
  922. return rc;
  923. }
  924. static irqreturn_t cyttsp4_irq(int irq, void *handle)
  925. {
  926. struct cyttsp4 *cd = handle;
  927. struct device *dev = cd->dev;
  928. enum cyttsp4_mode cur_mode;
  929. u8 cmd_ofs = cd->sysinfo.si_ofs.cmd_ofs;
  930. u8 mode[3];
  931. int rc;
  932. /*
  933. * Check whether this IRQ should be ignored (external)
  934. * This should be the very first thing to check since
  935. * ignore_irq may be set for a very short period of time
  936. */
  937. if (atomic_read(&cd->ignore_irq)) {
  938. dev_vdbg(dev, "%s: Ignoring IRQ\n", __func__);
  939. return IRQ_HANDLED;
  940. }
  941. dev_dbg(dev, "%s int:0x%x\n", __func__, cd->int_status);
  942. mutex_lock(&cd->system_lock);
  943. /* Just to debug */
  944. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEPING)
  945. dev_vdbg(dev, "%s: Received IRQ while in sleep\n", __func__);
  946. rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), mode);
  947. if (rc) {
  948. dev_err(cd->dev, "%s: Fail read adapter r=%d\n", __func__, rc);
  949. goto cyttsp4_irq_exit;
  950. }
  951. dev_vdbg(dev, "%s mode[0-2]:0x%X 0x%X 0x%X\n", __func__,
  952. mode[0], mode[1], mode[2]);
  953. if (IS_BOOTLOADER(mode[0], mode[1])) {
  954. cur_mode = CY_MODE_BOOTLOADER;
  955. dev_vdbg(dev, "%s: bl running\n", __func__);
  956. if (cd->mode == CY_MODE_BOOTLOADER) {
  957. /* Signal bootloader heartbeat heard */
  958. wake_up(&cd->wait_q);
  959. goto cyttsp4_irq_exit;
  960. }
  961. /* switch to bootloader */
  962. dev_dbg(dev, "%s: restart switch to bl m=%d -> m=%d\n",
  963. __func__, cd->mode, cur_mode);
  964. /* catch operation->bl glitch */
  965. if (cd->mode != CY_MODE_UNKNOWN) {
  966. /* Incase startup_state do not let startup_() */
  967. cd->mode = CY_MODE_UNKNOWN;
  968. cyttsp4_queue_startup_(cd);
  969. goto cyttsp4_irq_exit;
  970. }
  971. /*
  972. * do not wake thread on this switch since
  973. * it is possible to get an early heartbeat
  974. * prior to performing the reset
  975. */
  976. cd->mode = cur_mode;
  977. goto cyttsp4_irq_exit;
  978. }
  979. switch (mode[0] & CY_HST_MODE) {
  980. case CY_HST_OPERATE:
  981. cur_mode = CY_MODE_OPERATIONAL;
  982. dev_vdbg(dev, "%s: operational\n", __func__);
  983. break;
  984. case CY_HST_CAT:
  985. cur_mode = CY_MODE_CAT;
  986. dev_vdbg(dev, "%s: CaT\n", __func__);
  987. break;
  988. case CY_HST_SYSINFO:
  989. cur_mode = CY_MODE_SYSINFO;
  990. dev_vdbg(dev, "%s: sysinfo\n", __func__);
  991. break;
  992. default:
  993. cur_mode = CY_MODE_UNKNOWN;
  994. dev_err(dev, "%s: unknown HST mode 0x%02X\n", __func__,
  995. mode[0]);
  996. break;
  997. }
  998. /* Check whether this IRQ should be ignored (internal) */
  999. if (cd->int_status & CY_INT_IGNORE) {
  1000. dev_vdbg(dev, "%s: Ignoring IRQ\n", __func__);
  1001. goto cyttsp4_irq_exit;
  1002. }
  1003. /* Check for wake up interrupt */
  1004. if (cd->int_status & CY_INT_AWAKE) {
  1005. cd->int_status &= ~CY_INT_AWAKE;
  1006. wake_up(&cd->wait_q);
  1007. dev_vdbg(dev, "%s: Received wake up interrupt\n", __func__);
  1008. goto cyttsp4_irq_handshake;
  1009. }
  1010. /* Expecting mode change interrupt */
  1011. if ((cd->int_status & CY_INT_MODE_CHANGE)
  1012. && (mode[0] & CY_HST_MODE_CHANGE) == 0) {
  1013. cd->int_status &= ~CY_INT_MODE_CHANGE;
  1014. dev_dbg(dev, "%s: finish mode switch m=%d -> m=%d\n",
  1015. __func__, cd->mode, cur_mode);
  1016. cd->mode = cur_mode;
  1017. wake_up(&cd->wait_q);
  1018. goto cyttsp4_irq_handshake;
  1019. }
  1020. /* compare current core mode to current device mode */
  1021. dev_vdbg(dev, "%s: cd->mode=%d cur_mode=%d\n",
  1022. __func__, cd->mode, cur_mode);
  1023. if ((mode[0] & CY_HST_MODE_CHANGE) == 0 && cd->mode != cur_mode) {
  1024. /* Unexpected mode change occurred */
  1025. dev_err(dev, "%s %d->%d 0x%x\n", __func__, cd->mode,
  1026. cur_mode, cd->int_status);
  1027. dev_dbg(dev, "%s: Unexpected mode change, startup\n",
  1028. __func__);
  1029. cyttsp4_queue_startup_(cd);
  1030. goto cyttsp4_irq_exit;
  1031. }
  1032. /* Expecting command complete interrupt */
  1033. dev_vdbg(dev, "%s: command byte:0x%x\n", __func__, mode[cmd_ofs]);
  1034. if ((cd->int_status & CY_INT_EXEC_CMD)
  1035. && mode[cmd_ofs] & CY_CMD_COMPLETE) {
  1036. cd->int_status &= ~CY_INT_EXEC_CMD;
  1037. dev_vdbg(dev, "%s: Received command complete interrupt\n",
  1038. __func__);
  1039. wake_up(&cd->wait_q);
  1040. /*
  1041. * It is possible to receive a single interrupt for
  1042. * command complete and touch/button status report.
  1043. * Continue processing for a possible status report.
  1044. */
  1045. }
  1046. /* This should be status report, read status regs */
  1047. if (cd->mode == CY_MODE_OPERATIONAL) {
  1048. dev_vdbg(dev, "%s: Read status registers\n", __func__);
  1049. rc = cyttsp4_load_status_regs(cd);
  1050. if (rc < 0)
  1051. dev_err(dev, "%s: fail read mode regs r=%d\n",
  1052. __func__, rc);
  1053. }
  1054. cyttsp4_mt_attention(cd);
  1055. cyttsp4_irq_handshake:
  1056. /* handshake the event */
  1057. dev_vdbg(dev, "%s: Handshake mode=0x%02X r=%d\n",
  1058. __func__, mode[0], rc);
  1059. rc = cyttsp4_handshake(cd, mode[0]);
  1060. if (rc < 0)
  1061. dev_err(dev, "%s: Fail handshake mode=0x%02X r=%d\n",
  1062. __func__, mode[0], rc);
  1063. /*
  1064. * a non-zero udelay period is required for using
  1065. * IRQF_TRIGGER_LOW in order to delay until the
  1066. * device completes isr deassert
  1067. */
  1068. udelay(cd->cpdata->level_irq_udelay);
  1069. cyttsp4_irq_exit:
  1070. mutex_unlock(&cd->system_lock);
  1071. return IRQ_HANDLED;
  1072. }
  1073. static void cyttsp4_start_wd_timer(struct cyttsp4 *cd)
  1074. {
  1075. if (!CY_WATCHDOG_TIMEOUT)
  1076. return;
  1077. mod_timer(&cd->watchdog_timer, jiffies +
  1078. msecs_to_jiffies(CY_WATCHDOG_TIMEOUT));
  1079. }
  1080. static void cyttsp4_stop_wd_timer(struct cyttsp4 *cd)
  1081. {
  1082. if (!CY_WATCHDOG_TIMEOUT)
  1083. return;
  1084. /*
  1085. * Ensure we wait until the watchdog timer
  1086. * running on a different CPU finishes
  1087. */
  1088. del_timer_sync(&cd->watchdog_timer);
  1089. cancel_work_sync(&cd->watchdog_work);
  1090. del_timer_sync(&cd->watchdog_timer);
  1091. }
  1092. static void cyttsp4_watchdog_timer(struct timer_list *t)
  1093. {
  1094. struct cyttsp4 *cd = from_timer(cd, t, watchdog_timer);
  1095. dev_vdbg(cd->dev, "%s: Watchdog timer triggered\n", __func__);
  1096. schedule_work(&cd->watchdog_work);
  1097. return;
  1098. }
  1099. static int cyttsp4_request_exclusive(struct cyttsp4 *cd, void *ownptr,
  1100. int timeout_ms)
  1101. {
  1102. int t = msecs_to_jiffies(timeout_ms);
  1103. bool with_timeout = (timeout_ms != 0);
  1104. mutex_lock(&cd->system_lock);
  1105. if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
  1106. cd->exclusive_dev = ownptr;
  1107. goto exit;
  1108. }
  1109. cd->exclusive_waits++;
  1110. wait:
  1111. mutex_unlock(&cd->system_lock);
  1112. if (with_timeout) {
  1113. t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
  1114. if (IS_TMO(t)) {
  1115. dev_err(cd->dev, "%s: tmo waiting exclusive access\n",
  1116. __func__);
  1117. mutex_lock(&cd->system_lock);
  1118. cd->exclusive_waits--;
  1119. mutex_unlock(&cd->system_lock);
  1120. return -ETIME;
  1121. }
  1122. } else {
  1123. wait_event(cd->wait_q, !cd->exclusive_dev);
  1124. }
  1125. mutex_lock(&cd->system_lock);
  1126. if (cd->exclusive_dev)
  1127. goto wait;
  1128. cd->exclusive_dev = ownptr;
  1129. cd->exclusive_waits--;
  1130. exit:
  1131. mutex_unlock(&cd->system_lock);
  1132. return 0;
  1133. }
  1134. /*
  1135. * returns error if was not owned
  1136. */
  1137. static int cyttsp4_release_exclusive(struct cyttsp4 *cd, void *ownptr)
  1138. {
  1139. mutex_lock(&cd->system_lock);
  1140. if (cd->exclusive_dev != ownptr) {
  1141. mutex_unlock(&cd->system_lock);
  1142. return -EINVAL;
  1143. }
  1144. dev_vdbg(cd->dev, "%s: exclusive_dev %p freed\n",
  1145. __func__, cd->exclusive_dev);
  1146. cd->exclusive_dev = NULL;
  1147. wake_up(&cd->wait_q);
  1148. mutex_unlock(&cd->system_lock);
  1149. return 0;
  1150. }
  1151. static int cyttsp4_wait_bl_heartbeat(struct cyttsp4 *cd)
  1152. {
  1153. long t;
  1154. int rc = 0;
  1155. /* wait heartbeat */
  1156. dev_vdbg(cd->dev, "%s: wait heartbeat...\n", __func__);
  1157. t = wait_event_timeout(cd->wait_q, cd->mode == CY_MODE_BOOTLOADER,
  1158. msecs_to_jiffies(CY_CORE_RESET_AND_WAIT_TIMEOUT));
  1159. if (IS_TMO(t)) {
  1160. dev_err(cd->dev, "%s: tmo waiting bl heartbeat cd->mode=%d\n",
  1161. __func__, cd->mode);
  1162. rc = -ETIME;
  1163. }
  1164. return rc;
  1165. }
  1166. static int cyttsp4_wait_sysinfo_mode(struct cyttsp4 *cd)
  1167. {
  1168. long t;
  1169. dev_vdbg(cd->dev, "%s: wait sysinfo...\n", __func__);
  1170. t = wait_event_timeout(cd->wait_q, cd->mode == CY_MODE_SYSINFO,
  1171. msecs_to_jiffies(CY_CORE_MODE_CHANGE_TIMEOUT));
  1172. if (IS_TMO(t)) {
  1173. dev_err(cd->dev, "%s: tmo waiting exit bl cd->mode=%d\n",
  1174. __func__, cd->mode);
  1175. mutex_lock(&cd->system_lock);
  1176. cd->int_status &= ~CY_INT_MODE_CHANGE;
  1177. mutex_unlock(&cd->system_lock);
  1178. return -ETIME;
  1179. }
  1180. return 0;
  1181. }
  1182. static int cyttsp4_reset_and_wait(struct cyttsp4 *cd)
  1183. {
  1184. int rc;
  1185. /* reset hardware */
  1186. mutex_lock(&cd->system_lock);
  1187. dev_dbg(cd->dev, "%s: reset hw...\n", __func__);
  1188. rc = cyttsp4_hw_reset(cd);
  1189. cd->mode = CY_MODE_UNKNOWN;
  1190. mutex_unlock(&cd->system_lock);
  1191. if (rc < 0) {
  1192. dev_err(cd->dev, "%s:Fail hw reset r=%d\n", __func__, rc);
  1193. return rc;
  1194. }
  1195. return cyttsp4_wait_bl_heartbeat(cd);
  1196. }
  1197. /*
  1198. * returns err if refused or timeout; block until mode change complete
  1199. * bit is set (mode change interrupt)
  1200. */
  1201. static int cyttsp4_set_mode(struct cyttsp4 *cd, int new_mode)
  1202. {
  1203. u8 new_dev_mode;
  1204. u8 mode;
  1205. long t;
  1206. int rc;
  1207. switch (new_mode) {
  1208. case CY_MODE_OPERATIONAL:
  1209. new_dev_mode = CY_HST_OPERATE;
  1210. break;
  1211. case CY_MODE_SYSINFO:
  1212. new_dev_mode = CY_HST_SYSINFO;
  1213. break;
  1214. case CY_MODE_CAT:
  1215. new_dev_mode = CY_HST_CAT;
  1216. break;
  1217. default:
  1218. dev_err(cd->dev, "%s: invalid mode: %02X(%d)\n",
  1219. __func__, new_mode, new_mode);
  1220. return -EINVAL;
  1221. }
  1222. /* change mode */
  1223. dev_dbg(cd->dev, "%s: %s=%p new_dev_mode=%02X new_mode=%d\n",
  1224. __func__, "have exclusive", cd->exclusive_dev,
  1225. new_dev_mode, new_mode);
  1226. mutex_lock(&cd->system_lock);
  1227. rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), &mode);
  1228. if (rc < 0) {
  1229. mutex_unlock(&cd->system_lock);
  1230. dev_err(cd->dev, "%s: Fail read mode r=%d\n",
  1231. __func__, rc);
  1232. goto exit;
  1233. }
  1234. /* Clear device mode bits and set to new mode */
  1235. mode &= ~CY_HST_MODE;
  1236. mode |= new_dev_mode | CY_HST_MODE_CHANGE;
  1237. cd->int_status |= CY_INT_MODE_CHANGE;
  1238. rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(mode), &mode);
  1239. mutex_unlock(&cd->system_lock);
  1240. if (rc < 0) {
  1241. dev_err(cd->dev, "%s: Fail write mode change r=%d\n",
  1242. __func__, rc);
  1243. goto exit;
  1244. }
  1245. /* wait for mode change done interrupt */
  1246. t = wait_event_timeout(cd->wait_q,
  1247. (cd->int_status & CY_INT_MODE_CHANGE) == 0,
  1248. msecs_to_jiffies(CY_CORE_MODE_CHANGE_TIMEOUT));
  1249. dev_dbg(cd->dev, "%s: back from wait t=%ld cd->mode=%d\n",
  1250. __func__, t, cd->mode);
  1251. if (IS_TMO(t)) {
  1252. dev_err(cd->dev, "%s: %s\n", __func__,
  1253. "tmo waiting mode change");
  1254. mutex_lock(&cd->system_lock);
  1255. cd->int_status &= ~CY_INT_MODE_CHANGE;
  1256. mutex_unlock(&cd->system_lock);
  1257. rc = -EINVAL;
  1258. }
  1259. exit:
  1260. return rc;
  1261. }
  1262. static void cyttsp4_watchdog_work(struct work_struct *work)
  1263. {
  1264. struct cyttsp4 *cd =
  1265. container_of(work, struct cyttsp4, watchdog_work);
  1266. u8 *mode;
  1267. int retval;
  1268. mutex_lock(&cd->system_lock);
  1269. retval = cyttsp4_load_status_regs(cd);
  1270. if (retval < 0) {
  1271. dev_err(cd->dev,
  1272. "%s: failed to access device in watchdog timer r=%d\n",
  1273. __func__, retval);
  1274. cyttsp4_queue_startup_(cd);
  1275. goto cyttsp4_timer_watchdog_exit_error;
  1276. }
  1277. mode = &cd->sysinfo.xy_mode[CY_REG_BASE];
  1278. if (IS_BOOTLOADER(mode[0], mode[1])) {
  1279. dev_err(cd->dev,
  1280. "%s: device found in bootloader mode when operational mode\n",
  1281. __func__);
  1282. cyttsp4_queue_startup_(cd);
  1283. goto cyttsp4_timer_watchdog_exit_error;
  1284. }
  1285. cyttsp4_start_wd_timer(cd);
  1286. cyttsp4_timer_watchdog_exit_error:
  1287. mutex_unlock(&cd->system_lock);
  1288. return;
  1289. }
  1290. static int cyttsp4_core_sleep_(struct cyttsp4 *cd)
  1291. {
  1292. enum cyttsp4_sleep_state ss = SS_SLEEP_ON;
  1293. enum cyttsp4_int_state int_status = CY_INT_IGNORE;
  1294. int rc = 0;
  1295. u8 mode[2];
  1296. /* Already in sleep mode? */
  1297. mutex_lock(&cd->system_lock);
  1298. if (cd->sleep_state == SS_SLEEP_ON) {
  1299. mutex_unlock(&cd->system_lock);
  1300. return 0;
  1301. }
  1302. cd->sleep_state = SS_SLEEPING;
  1303. mutex_unlock(&cd->system_lock);
  1304. cyttsp4_stop_wd_timer(cd);
  1305. /* Wait until currently running IRQ handler exits and disable IRQ */
  1306. disable_irq(cd->irq);
  1307. dev_vdbg(cd->dev, "%s: write DEEP SLEEP...\n", __func__);
  1308. mutex_lock(&cd->system_lock);
  1309. rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), &mode);
  1310. if (rc) {
  1311. mutex_unlock(&cd->system_lock);
  1312. dev_err(cd->dev, "%s: Fail read adapter r=%d\n", __func__, rc);
  1313. goto error;
  1314. }
  1315. if (IS_BOOTLOADER(mode[0], mode[1])) {
  1316. mutex_unlock(&cd->system_lock);
  1317. dev_err(cd->dev, "%s: Device in BOOTLOADER mode.\n", __func__);
  1318. rc = -EINVAL;
  1319. goto error;
  1320. }
  1321. mode[0] |= CY_HST_SLEEP;
  1322. rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(mode[0]), &mode[0]);
  1323. mutex_unlock(&cd->system_lock);
  1324. if (rc) {
  1325. dev_err(cd->dev, "%s: Fail write adapter r=%d\n", __func__, rc);
  1326. goto error;
  1327. }
  1328. dev_vdbg(cd->dev, "%s: write DEEP SLEEP succeeded\n", __func__);
  1329. if (cd->cpdata->power) {
  1330. dev_dbg(cd->dev, "%s: Power down HW\n", __func__);
  1331. rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, &cd->ignore_irq);
  1332. } else {
  1333. dev_dbg(cd->dev, "%s: No power function\n", __func__);
  1334. rc = 0;
  1335. }
  1336. if (rc < 0) {
  1337. dev_err(cd->dev, "%s: HW Power down fails r=%d\n",
  1338. __func__, rc);
  1339. goto error;
  1340. }
  1341. /* Give time to FW to sleep */
  1342. msleep(50);
  1343. goto exit;
  1344. error:
  1345. ss = SS_SLEEP_OFF;
  1346. int_status = CY_INT_NONE;
  1347. cyttsp4_start_wd_timer(cd);
  1348. exit:
  1349. mutex_lock(&cd->system_lock);
  1350. cd->sleep_state = ss;
  1351. cd->int_status |= int_status;
  1352. mutex_unlock(&cd->system_lock);
  1353. enable_irq(cd->irq);
  1354. return rc;
  1355. }
  1356. static int cyttsp4_startup_(struct cyttsp4 *cd)
  1357. {
  1358. int retry = CY_CORE_STARTUP_RETRY_COUNT;
  1359. int rc;
  1360. cyttsp4_stop_wd_timer(cd);
  1361. reset:
  1362. if (retry != CY_CORE_STARTUP_RETRY_COUNT)
  1363. dev_dbg(cd->dev, "%s: Retry %d\n", __func__,
  1364. CY_CORE_STARTUP_RETRY_COUNT - retry);
  1365. /* reset hardware and wait for heartbeat */
  1366. rc = cyttsp4_reset_and_wait(cd);
  1367. if (rc < 0) {
  1368. dev_err(cd->dev, "%s: Error on h/w reset r=%d\n", __func__, rc);
  1369. if (retry--)
  1370. goto reset;
  1371. goto exit;
  1372. }
  1373. /* exit bl into sysinfo mode */
  1374. dev_vdbg(cd->dev, "%s: write exit ldr...\n", __func__);
  1375. mutex_lock(&cd->system_lock);
  1376. cd->int_status &= ~CY_INT_IGNORE;
  1377. cd->int_status |= CY_INT_MODE_CHANGE;
  1378. rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(ldr_exit),
  1379. (u8 *)ldr_exit);
  1380. mutex_unlock(&cd->system_lock);
  1381. if (rc < 0) {
  1382. dev_err(cd->dev, "%s: Fail write r=%d\n", __func__, rc);
  1383. if (retry--)
  1384. goto reset;
  1385. goto exit;
  1386. }
  1387. rc = cyttsp4_wait_sysinfo_mode(cd);
  1388. if (rc < 0) {
  1389. u8 buf[sizeof(ldr_err_app)];
  1390. int rc1;
  1391. /* Check for invalid/corrupted touch application */
  1392. rc1 = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(ldr_err_app),
  1393. buf);
  1394. if (rc1) {
  1395. dev_err(cd->dev, "%s: Fail read r=%d\n", __func__, rc1);
  1396. } else if (!memcmp(buf, ldr_err_app, sizeof(ldr_err_app))) {
  1397. dev_err(cd->dev, "%s: Error launching touch application\n",
  1398. __func__);
  1399. mutex_lock(&cd->system_lock);
  1400. cd->invalid_touch_app = true;
  1401. mutex_unlock(&cd->system_lock);
  1402. goto exit_no_wd;
  1403. }
  1404. if (retry--)
  1405. goto reset;
  1406. goto exit;
  1407. }
  1408. mutex_lock(&cd->system_lock);
  1409. cd->invalid_touch_app = false;
  1410. mutex_unlock(&cd->system_lock);
  1411. /* read sysinfo data */
  1412. dev_vdbg(cd->dev, "%s: get sysinfo regs..\n", __func__);
  1413. rc = cyttsp4_get_sysinfo_regs(cd);
  1414. if (rc < 0) {
  1415. dev_err(cd->dev, "%s: failed to get sysinfo regs rc=%d\n",
  1416. __func__, rc);
  1417. if (retry--)
  1418. goto reset;
  1419. goto exit;
  1420. }
  1421. rc = cyttsp4_set_mode(cd, CY_MODE_OPERATIONAL);
  1422. if (rc < 0) {
  1423. dev_err(cd->dev, "%s: failed to set mode to operational rc=%d\n",
  1424. __func__, rc);
  1425. if (retry--)
  1426. goto reset;
  1427. goto exit;
  1428. }
  1429. cyttsp4_lift_all(&cd->md);
  1430. /* restore to sleep if was suspended */
  1431. mutex_lock(&cd->system_lock);
  1432. if (cd->sleep_state == SS_SLEEP_ON) {
  1433. cd->sleep_state = SS_SLEEP_OFF;
  1434. mutex_unlock(&cd->system_lock);
  1435. cyttsp4_core_sleep_(cd);
  1436. goto exit_no_wd;
  1437. }
  1438. mutex_unlock(&cd->system_lock);
  1439. exit:
  1440. cyttsp4_start_wd_timer(cd);
  1441. exit_no_wd:
  1442. return rc;
  1443. }
  1444. static int cyttsp4_startup(struct cyttsp4 *cd)
  1445. {
  1446. int rc;
  1447. mutex_lock(&cd->system_lock);
  1448. cd->startup_state = STARTUP_RUNNING;
  1449. mutex_unlock(&cd->system_lock);
  1450. rc = cyttsp4_request_exclusive(cd, cd->dev,
  1451. CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT);
  1452. if (rc < 0) {
  1453. dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
  1454. __func__, cd->exclusive_dev, cd->dev);
  1455. goto exit;
  1456. }
  1457. rc = cyttsp4_startup_(cd);
  1458. if (cyttsp4_release_exclusive(cd, cd->dev) < 0)
  1459. /* Don't return fail code, mode is already changed. */
  1460. dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
  1461. else
  1462. dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
  1463. exit:
  1464. mutex_lock(&cd->system_lock);
  1465. cd->startup_state = STARTUP_NONE;
  1466. mutex_unlock(&cd->system_lock);
  1467. /* Wake the waiters for end of startup */
  1468. wake_up(&cd->wait_q);
  1469. return rc;
  1470. }
  1471. static void cyttsp4_startup_work_function(struct work_struct *work)
  1472. {
  1473. struct cyttsp4 *cd = container_of(work, struct cyttsp4, startup_work);
  1474. int rc;
  1475. rc = cyttsp4_startup(cd);
  1476. if (rc < 0)
  1477. dev_err(cd->dev, "%s: Fail queued startup r=%d\n",
  1478. __func__, rc);
  1479. }
  1480. static void cyttsp4_free_si_ptrs(struct cyttsp4 *cd)
  1481. {
  1482. struct cyttsp4_sysinfo *si = &cd->sysinfo;
  1483. if (!si)
  1484. return;
  1485. kfree(si->si_ptrs.cydata);
  1486. kfree(si->si_ptrs.test);
  1487. kfree(si->si_ptrs.pcfg);
  1488. kfree(si->si_ptrs.opcfg);
  1489. kfree(si->si_ptrs.ddata);
  1490. kfree(si->si_ptrs.mdata);
  1491. kfree(si->btn);
  1492. kfree(si->xy_mode);
  1493. kfree(si->xy_data);
  1494. kfree(si->btn_rec_data);
  1495. }
  1496. #ifdef CONFIG_PM
  1497. static int cyttsp4_core_sleep(struct cyttsp4 *cd)
  1498. {
  1499. int rc;
  1500. rc = cyttsp4_request_exclusive(cd, cd->dev,
  1501. CY_CORE_SLEEP_REQUEST_EXCLUSIVE_TIMEOUT);
  1502. if (rc < 0) {
  1503. dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
  1504. __func__, cd->exclusive_dev, cd->dev);
  1505. return 0;
  1506. }
  1507. rc = cyttsp4_core_sleep_(cd);
  1508. if (cyttsp4_release_exclusive(cd, cd->dev) < 0)
  1509. dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
  1510. else
  1511. dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
  1512. return rc;
  1513. }
  1514. static int cyttsp4_core_wake_(struct cyttsp4 *cd)
  1515. {
  1516. struct device *dev = cd->dev;
  1517. int rc;
  1518. u8 mode;
  1519. int t;
  1520. /* Already woken? */
  1521. mutex_lock(&cd->system_lock);
  1522. if (cd->sleep_state == SS_SLEEP_OFF) {
  1523. mutex_unlock(&cd->system_lock);
  1524. return 0;
  1525. }
  1526. cd->int_status &= ~CY_INT_IGNORE;
  1527. cd->int_status |= CY_INT_AWAKE;
  1528. cd->sleep_state = SS_WAKING;
  1529. if (cd->cpdata->power) {
  1530. dev_dbg(dev, "%s: Power up HW\n", __func__);
  1531. rc = cd->cpdata->power(cd->cpdata, 1, dev, &cd->ignore_irq);
  1532. } else {
  1533. dev_dbg(dev, "%s: No power function\n", __func__);
  1534. rc = -ENOSYS;
  1535. }
  1536. if (rc < 0) {
  1537. dev_err(dev, "%s: HW Power up fails r=%d\n",
  1538. __func__, rc);
  1539. /* Initiate a read transaction to wake up */
  1540. cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), &mode);
  1541. } else
  1542. dev_vdbg(cd->dev, "%s: HW power up succeeds\n",
  1543. __func__);
  1544. mutex_unlock(&cd->system_lock);
  1545. t = wait_event_timeout(cd->wait_q,
  1546. (cd->int_status & CY_INT_AWAKE) == 0,
  1547. msecs_to_jiffies(CY_CORE_WAKEUP_TIMEOUT));
  1548. if (IS_TMO(t)) {
  1549. dev_err(dev, "%s: TMO waiting for wakeup\n", __func__);
  1550. mutex_lock(&cd->system_lock);
  1551. cd->int_status &= ~CY_INT_AWAKE;
  1552. /* Try starting up */
  1553. cyttsp4_queue_startup_(cd);
  1554. mutex_unlock(&cd->system_lock);
  1555. }
  1556. mutex_lock(&cd->system_lock);
  1557. cd->sleep_state = SS_SLEEP_OFF;
  1558. mutex_unlock(&cd->system_lock);
  1559. cyttsp4_start_wd_timer(cd);
  1560. return 0;
  1561. }
  1562. static int cyttsp4_core_wake(struct cyttsp4 *cd)
  1563. {
  1564. int rc;
  1565. rc = cyttsp4_request_exclusive(cd, cd->dev,
  1566. CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT);
  1567. if (rc < 0) {
  1568. dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n",
  1569. __func__, cd->exclusive_dev, cd->dev);
  1570. return 0;
  1571. }
  1572. rc = cyttsp4_core_wake_(cd);
  1573. if (cyttsp4_release_exclusive(cd, cd->dev) < 0)
  1574. dev_err(cd->dev, "%s: fail to release exclusive\n", __func__);
  1575. else
  1576. dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__);
  1577. return rc;
  1578. }
  1579. static int cyttsp4_core_suspend(struct device *dev)
  1580. {
  1581. struct cyttsp4 *cd = dev_get_drvdata(dev);
  1582. struct cyttsp4_mt_data *md = &cd->md;
  1583. int rc;
  1584. md->is_suspended = true;
  1585. rc = cyttsp4_core_sleep(cd);
  1586. if (rc < 0) {
  1587. dev_err(dev, "%s: Error on sleep\n", __func__);
  1588. return -EAGAIN;
  1589. }
  1590. return 0;
  1591. }
  1592. static int cyttsp4_core_resume(struct device *dev)
  1593. {
  1594. struct cyttsp4 *cd = dev_get_drvdata(dev);
  1595. struct cyttsp4_mt_data *md = &cd->md;
  1596. int rc;
  1597. md->is_suspended = false;
  1598. rc = cyttsp4_core_wake(cd);
  1599. if (rc < 0) {
  1600. dev_err(dev, "%s: Error on wake\n", __func__);
  1601. return -EAGAIN;
  1602. }
  1603. return 0;
  1604. }
  1605. #endif
  1606. const struct dev_pm_ops cyttsp4_pm_ops = {
  1607. SET_SYSTEM_SLEEP_PM_OPS(cyttsp4_core_suspend, cyttsp4_core_resume)
  1608. SET_RUNTIME_PM_OPS(cyttsp4_core_suspend, cyttsp4_core_resume, NULL)
  1609. };
  1610. EXPORT_SYMBOL_GPL(cyttsp4_pm_ops);
  1611. static int cyttsp4_mt_open(struct input_dev *input)
  1612. {
  1613. pm_runtime_get(input->dev.parent);
  1614. return 0;
  1615. }
  1616. static void cyttsp4_mt_close(struct input_dev *input)
  1617. {
  1618. struct cyttsp4_mt_data *md = input_get_drvdata(input);
  1619. mutex_lock(&md->report_lock);
  1620. if (!md->is_suspended)
  1621. pm_runtime_put(input->dev.parent);
  1622. mutex_unlock(&md->report_lock);
  1623. }
  1624. static int cyttsp4_setup_input_device(struct cyttsp4 *cd)
  1625. {
  1626. struct device *dev = cd->dev;
  1627. struct cyttsp4_mt_data *md = &cd->md;
  1628. int signal = CY_IGNORE_VALUE;
  1629. int max_x, max_y, max_p, min, max;
  1630. int max_x_tmp, max_y_tmp;
  1631. int i;
  1632. int rc;
  1633. dev_vdbg(dev, "%s: Initialize event signals\n", __func__);
  1634. __set_bit(EV_ABS, md->input->evbit);
  1635. __set_bit(EV_REL, md->input->evbit);
  1636. __set_bit(EV_KEY, md->input->evbit);
  1637. max_x_tmp = md->si->si_ofs.max_x;
  1638. max_y_tmp = md->si->si_ofs.max_y;
  1639. /* get maximum values from the sysinfo data */
  1640. if (md->pdata->flags & CY_FLAG_FLIP) {
  1641. max_x = max_y_tmp - 1;
  1642. max_y = max_x_tmp - 1;
  1643. } else {
  1644. max_x = max_x_tmp - 1;
  1645. max_y = max_y_tmp - 1;
  1646. }
  1647. max_p = md->si->si_ofs.max_p;
  1648. /* set event signal capabilities */
  1649. for (i = 0; i < (md->pdata->frmwrk->size / CY_NUM_ABS_SET); i++) {
  1650. signal = md->pdata->frmwrk->abs
  1651. [(i * CY_NUM_ABS_SET) + CY_SIGNAL_OST];
  1652. if (signal != CY_IGNORE_VALUE) {
  1653. __set_bit(signal, md->input->absbit);
  1654. min = md->pdata->frmwrk->abs
  1655. [(i * CY_NUM_ABS_SET) + CY_MIN_OST];
  1656. max = md->pdata->frmwrk->abs
  1657. [(i * CY_NUM_ABS_SET) + CY_MAX_OST];
  1658. if (i == CY_ABS_ID_OST) {
  1659. /* shift track ids down to start at 0 */
  1660. max = max - min;
  1661. min = min - min;
  1662. } else if (i == CY_ABS_X_OST)
  1663. max = max_x;
  1664. else if (i == CY_ABS_Y_OST)
  1665. max = max_y;
  1666. else if (i == CY_ABS_P_OST)
  1667. max = max_p;
  1668. input_set_abs_params(md->input, signal, min, max,
  1669. md->pdata->frmwrk->abs
  1670. [(i * CY_NUM_ABS_SET) + CY_FUZZ_OST],
  1671. md->pdata->frmwrk->abs
  1672. [(i * CY_NUM_ABS_SET) + CY_FLAT_OST]);
  1673. dev_dbg(dev, "%s: register signal=%02X min=%d max=%d\n",
  1674. __func__, signal, min, max);
  1675. if ((i == CY_ABS_ID_OST) &&
  1676. (md->si->si_ofs.tch_rec_size <
  1677. CY_TMA4XX_TCH_REC_SIZE))
  1678. break;
  1679. }
  1680. }
  1681. input_mt_init_slots(md->input, md->si->si_ofs.tch_abs[CY_TCH_T].max,
  1682. INPUT_MT_DIRECT);
  1683. rc = input_register_device(md->input);
  1684. if (rc < 0)
  1685. dev_err(dev, "%s: Error, failed register input device r=%d\n",
  1686. __func__, rc);
  1687. return rc;
  1688. }
  1689. static int cyttsp4_mt_probe(struct cyttsp4 *cd)
  1690. {
  1691. struct device *dev = cd->dev;
  1692. struct cyttsp4_mt_data *md = &cd->md;
  1693. struct cyttsp4_mt_platform_data *pdata = cd->pdata->mt_pdata;
  1694. int rc = 0;
  1695. mutex_init(&md->report_lock);
  1696. md->pdata = pdata;
  1697. /* Create the input device and register it. */
  1698. dev_vdbg(dev, "%s: Create the input device and register it\n",
  1699. __func__);
  1700. md->input = input_allocate_device();
  1701. if (md->input == NULL) {
  1702. dev_err(dev, "%s: Error, failed to allocate input device\n",
  1703. __func__);
  1704. rc = -ENOSYS;
  1705. goto error_alloc_failed;
  1706. }
  1707. md->input->name = pdata->inp_dev_name;
  1708. scnprintf(md->phys, sizeof(md->phys)-1, "%s", dev_name(dev));
  1709. md->input->phys = md->phys;
  1710. md->input->id.bustype = cd->bus_ops->bustype;
  1711. md->input->dev.parent = dev;
  1712. md->input->open = cyttsp4_mt_open;
  1713. md->input->close = cyttsp4_mt_close;
  1714. input_set_drvdata(md->input, md);
  1715. /* get sysinfo */
  1716. md->si = &cd->sysinfo;
  1717. rc = cyttsp4_setup_input_device(cd);
  1718. if (rc)
  1719. goto error_init_input;
  1720. return 0;
  1721. error_init_input:
  1722. input_free_device(md->input);
  1723. error_alloc_failed:
  1724. dev_err(dev, "%s failed.\n", __func__);
  1725. return rc;
  1726. }
  1727. struct cyttsp4 *cyttsp4_probe(const struct cyttsp4_bus_ops *ops,
  1728. struct device *dev, u16 irq, size_t xfer_buf_size)
  1729. {
  1730. struct cyttsp4 *cd;
  1731. struct cyttsp4_platform_data *pdata = dev_get_platdata(dev);
  1732. unsigned long irq_flags;
  1733. int rc = 0;
  1734. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  1735. dev_err(dev, "%s: Missing platform data\n", __func__);
  1736. rc = -ENODEV;
  1737. goto error_no_pdata;
  1738. }
  1739. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  1740. if (!cd) {
  1741. dev_err(dev, "%s: Error, kzalloc\n", __func__);
  1742. rc = -ENOMEM;
  1743. goto error_alloc_data;
  1744. }
  1745. cd->xfer_buf = kzalloc(xfer_buf_size, GFP_KERNEL);
  1746. if (!cd->xfer_buf) {
  1747. dev_err(dev, "%s: Error, kzalloc\n", __func__);
  1748. rc = -ENOMEM;
  1749. goto error_free_cd;
  1750. }
  1751. /* Initialize device info */
  1752. cd->dev = dev;
  1753. cd->pdata = pdata;
  1754. cd->cpdata = pdata->core_pdata;
  1755. cd->bus_ops = ops;
  1756. /* Initialize mutexes and spinlocks */
  1757. mutex_init(&cd->system_lock);
  1758. mutex_init(&cd->adap_lock);
  1759. /* Initialize wait queue */
  1760. init_waitqueue_head(&cd->wait_q);
  1761. /* Initialize works */
  1762. INIT_WORK(&cd->startup_work, cyttsp4_startup_work_function);
  1763. INIT_WORK(&cd->watchdog_work, cyttsp4_watchdog_work);
  1764. /* Initialize IRQ */
  1765. cd->irq = gpio_to_irq(cd->cpdata->irq_gpio);
  1766. if (cd->irq < 0) {
  1767. rc = -EINVAL;
  1768. goto error_free_xfer;
  1769. }
  1770. dev_set_drvdata(dev, cd);
  1771. /* Call platform init function */
  1772. if (cd->cpdata->init) {
  1773. dev_dbg(cd->dev, "%s: Init HW\n", __func__);
  1774. rc = cd->cpdata->init(cd->cpdata, 1, cd->dev);
  1775. } else {
  1776. dev_dbg(cd->dev, "%s: No HW INIT function\n", __func__);
  1777. rc = 0;
  1778. }
  1779. if (rc < 0)
  1780. dev_err(cd->dev, "%s: HW Init fail r=%d\n", __func__, rc);
  1781. dev_dbg(dev, "%s: initialize threaded irq=%d\n", __func__, cd->irq);
  1782. if (cd->cpdata->level_irq_udelay > 0)
  1783. /* use level triggered interrupts */
  1784. irq_flags = IRQF_TRIGGER_LOW | IRQF_ONESHOT;
  1785. else
  1786. /* use edge triggered interrupts */
  1787. irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
  1788. rc = request_threaded_irq(cd->irq, NULL, cyttsp4_irq, irq_flags,
  1789. dev_name(dev), cd);
  1790. if (rc < 0) {
  1791. dev_err(dev, "%s: Error, could not request irq\n", __func__);
  1792. goto error_request_irq;
  1793. }
  1794. /* Setup watchdog timer */
  1795. timer_setup(&cd->watchdog_timer, cyttsp4_watchdog_timer, 0);
  1796. /*
  1797. * call startup directly to ensure that the device
  1798. * is tested before leaving the probe
  1799. */
  1800. rc = cyttsp4_startup(cd);
  1801. /* Do not fail probe if startup fails but the device is detected */
  1802. if (rc < 0 && cd->mode == CY_MODE_UNKNOWN) {
  1803. dev_err(cd->dev, "%s: Fail initial startup r=%d\n",
  1804. __func__, rc);
  1805. goto error_startup;
  1806. }
  1807. rc = cyttsp4_mt_probe(cd);
  1808. if (rc < 0) {
  1809. dev_err(dev, "%s: Error, fail mt probe\n", __func__);
  1810. goto error_startup;
  1811. }
  1812. pm_runtime_enable(dev);
  1813. return cd;
  1814. error_startup:
  1815. cancel_work_sync(&cd->startup_work);
  1816. cyttsp4_stop_wd_timer(cd);
  1817. pm_runtime_disable(dev);
  1818. cyttsp4_free_si_ptrs(cd);
  1819. free_irq(cd->irq, cd);
  1820. error_request_irq:
  1821. if (cd->cpdata->init)
  1822. cd->cpdata->init(cd->cpdata, 0, dev);
  1823. error_free_xfer:
  1824. kfree(cd->xfer_buf);
  1825. error_free_cd:
  1826. kfree(cd);
  1827. error_alloc_data:
  1828. error_no_pdata:
  1829. dev_err(dev, "%s failed.\n", __func__);
  1830. return ERR_PTR(rc);
  1831. }
  1832. EXPORT_SYMBOL_GPL(cyttsp4_probe);
  1833. static void cyttsp4_mt_release(struct cyttsp4_mt_data *md)
  1834. {
  1835. input_unregister_device(md->input);
  1836. input_set_drvdata(md->input, NULL);
  1837. }
  1838. int cyttsp4_remove(struct cyttsp4 *cd)
  1839. {
  1840. struct device *dev = cd->dev;
  1841. cyttsp4_mt_release(&cd->md);
  1842. /*
  1843. * Suspend the device before freeing the startup_work and stopping
  1844. * the watchdog since sleep function restarts watchdog on failure
  1845. */
  1846. pm_runtime_suspend(dev);
  1847. pm_runtime_disable(dev);
  1848. cancel_work_sync(&cd->startup_work);
  1849. cyttsp4_stop_wd_timer(cd);
  1850. free_irq(cd->irq, cd);
  1851. if (cd->cpdata->init)
  1852. cd->cpdata->init(cd->cpdata, 0, dev);
  1853. cyttsp4_free_si_ptrs(cd);
  1854. kfree(cd);
  1855. return 0;
  1856. }
  1857. EXPORT_SYMBOL_GPL(cyttsp4_remove);
  1858. MODULE_LICENSE("GPL");
  1859. MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard touchscreen core driver");
  1860. MODULE_AUTHOR("Cypress");