cyttsp4_core.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184
  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. if (!md->si) {
  1718. dev_err(dev, "%s: Fail get sysinfo pointer from core p=%p\n",
  1719. __func__, md->si);
  1720. goto error_get_sysinfo;
  1721. }
  1722. rc = cyttsp4_setup_input_device(cd);
  1723. if (rc)
  1724. goto error_init_input;
  1725. return 0;
  1726. error_init_input:
  1727. input_free_device(md->input);
  1728. error_get_sysinfo:
  1729. input_set_drvdata(md->input, NULL);
  1730. error_alloc_failed:
  1731. dev_err(dev, "%s failed.\n", __func__);
  1732. return rc;
  1733. }
  1734. struct cyttsp4 *cyttsp4_probe(const struct cyttsp4_bus_ops *ops,
  1735. struct device *dev, u16 irq, size_t xfer_buf_size)
  1736. {
  1737. struct cyttsp4 *cd;
  1738. struct cyttsp4_platform_data *pdata = dev_get_platdata(dev);
  1739. unsigned long irq_flags;
  1740. int rc = 0;
  1741. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  1742. dev_err(dev, "%s: Missing platform data\n", __func__);
  1743. rc = -ENODEV;
  1744. goto error_no_pdata;
  1745. }
  1746. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  1747. if (!cd) {
  1748. dev_err(dev, "%s: Error, kzalloc\n", __func__);
  1749. rc = -ENOMEM;
  1750. goto error_alloc_data;
  1751. }
  1752. cd->xfer_buf = kzalloc(xfer_buf_size, GFP_KERNEL);
  1753. if (!cd->xfer_buf) {
  1754. dev_err(dev, "%s: Error, kzalloc\n", __func__);
  1755. rc = -ENOMEM;
  1756. goto error_free_cd;
  1757. }
  1758. /* Initialize device info */
  1759. cd->dev = dev;
  1760. cd->pdata = pdata;
  1761. cd->cpdata = pdata->core_pdata;
  1762. cd->bus_ops = ops;
  1763. /* Initialize mutexes and spinlocks */
  1764. mutex_init(&cd->system_lock);
  1765. mutex_init(&cd->adap_lock);
  1766. /* Initialize wait queue */
  1767. init_waitqueue_head(&cd->wait_q);
  1768. /* Initialize works */
  1769. INIT_WORK(&cd->startup_work, cyttsp4_startup_work_function);
  1770. INIT_WORK(&cd->watchdog_work, cyttsp4_watchdog_work);
  1771. /* Initialize IRQ */
  1772. cd->irq = gpio_to_irq(cd->cpdata->irq_gpio);
  1773. if (cd->irq < 0) {
  1774. rc = -EINVAL;
  1775. goto error_free_xfer;
  1776. }
  1777. dev_set_drvdata(dev, cd);
  1778. /* Call platform init function */
  1779. if (cd->cpdata->init) {
  1780. dev_dbg(cd->dev, "%s: Init HW\n", __func__);
  1781. rc = cd->cpdata->init(cd->cpdata, 1, cd->dev);
  1782. } else {
  1783. dev_dbg(cd->dev, "%s: No HW INIT function\n", __func__);
  1784. rc = 0;
  1785. }
  1786. if (rc < 0)
  1787. dev_err(cd->dev, "%s: HW Init fail r=%d\n", __func__, rc);
  1788. dev_dbg(dev, "%s: initialize threaded irq=%d\n", __func__, cd->irq);
  1789. if (cd->cpdata->level_irq_udelay > 0)
  1790. /* use level triggered interrupts */
  1791. irq_flags = IRQF_TRIGGER_LOW | IRQF_ONESHOT;
  1792. else
  1793. /* use edge triggered interrupts */
  1794. irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
  1795. rc = request_threaded_irq(cd->irq, NULL, cyttsp4_irq, irq_flags,
  1796. dev_name(dev), cd);
  1797. if (rc < 0) {
  1798. dev_err(dev, "%s: Error, could not request irq\n", __func__);
  1799. goto error_request_irq;
  1800. }
  1801. /* Setup watchdog timer */
  1802. timer_setup(&cd->watchdog_timer, cyttsp4_watchdog_timer, 0);
  1803. /*
  1804. * call startup directly to ensure that the device
  1805. * is tested before leaving the probe
  1806. */
  1807. rc = cyttsp4_startup(cd);
  1808. /* Do not fail probe if startup fails but the device is detected */
  1809. if (rc < 0 && cd->mode == CY_MODE_UNKNOWN) {
  1810. dev_err(cd->dev, "%s: Fail initial startup r=%d\n",
  1811. __func__, rc);
  1812. goto error_startup;
  1813. }
  1814. rc = cyttsp4_mt_probe(cd);
  1815. if (rc < 0) {
  1816. dev_err(dev, "%s: Error, fail mt probe\n", __func__);
  1817. goto error_startup;
  1818. }
  1819. pm_runtime_enable(dev);
  1820. return cd;
  1821. error_startup:
  1822. cancel_work_sync(&cd->startup_work);
  1823. cyttsp4_stop_wd_timer(cd);
  1824. pm_runtime_disable(dev);
  1825. cyttsp4_free_si_ptrs(cd);
  1826. free_irq(cd->irq, cd);
  1827. error_request_irq:
  1828. if (cd->cpdata->init)
  1829. cd->cpdata->init(cd->cpdata, 0, dev);
  1830. error_free_xfer:
  1831. kfree(cd->xfer_buf);
  1832. error_free_cd:
  1833. kfree(cd);
  1834. error_alloc_data:
  1835. error_no_pdata:
  1836. dev_err(dev, "%s failed.\n", __func__);
  1837. return ERR_PTR(rc);
  1838. }
  1839. EXPORT_SYMBOL_GPL(cyttsp4_probe);
  1840. static void cyttsp4_mt_release(struct cyttsp4_mt_data *md)
  1841. {
  1842. input_unregister_device(md->input);
  1843. input_set_drvdata(md->input, NULL);
  1844. }
  1845. int cyttsp4_remove(struct cyttsp4 *cd)
  1846. {
  1847. struct device *dev = cd->dev;
  1848. cyttsp4_mt_release(&cd->md);
  1849. /*
  1850. * Suspend the device before freeing the startup_work and stopping
  1851. * the watchdog since sleep function restarts watchdog on failure
  1852. */
  1853. pm_runtime_suspend(dev);
  1854. pm_runtime_disable(dev);
  1855. cancel_work_sync(&cd->startup_work);
  1856. cyttsp4_stop_wd_timer(cd);
  1857. free_irq(cd->irq, cd);
  1858. if (cd->cpdata->init)
  1859. cd->cpdata->init(cd->cpdata, 0, dev);
  1860. cyttsp4_free_si_ptrs(cd);
  1861. kfree(cd);
  1862. return 0;
  1863. }
  1864. EXPORT_SYMBOL_GPL(cyttsp4_remove);
  1865. MODULE_LICENSE("GPL");
  1866. MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard touchscreen core driver");
  1867. MODULE_AUTHOR("Cypress");