cyapa_gen5.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910
  1. /*
  2. * Cypress APA trackpad with I2C interface
  3. *
  4. * Author: Dudley Du <[email protected]>
  5. *
  6. * Copyright (C) 2014-2015 Cypress Semiconductor, Inc.
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file COPYING in the main directory of this archive for
  10. * more details.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/i2c.h>
  14. #include <linux/input.h>
  15. #include <linux/input/mt.h>
  16. #include <linux/mutex.h>
  17. #include <linux/completion.h>
  18. #include <linux/slab.h>
  19. #include <asm/unaligned.h>
  20. #include <linux/crc-itu-t.h>
  21. #include <linux/pm_runtime.h>
  22. #include "cyapa.h"
  23. /* Macro of TSG firmware image */
  24. #define CYAPA_TSG_FLASH_MAP_BLOCK_SIZE 0x80
  25. #define CYAPA_TSG_IMG_FW_HDR_SIZE 13
  26. #define CYAPA_TSG_FW_ROW_SIZE (CYAPA_TSG_FLASH_MAP_BLOCK_SIZE)
  27. #define CYAPA_TSG_IMG_START_ROW_NUM 0x002e
  28. #define CYAPA_TSG_IMG_END_ROW_NUM 0x01fe
  29. #define CYAPA_TSG_IMG_APP_INTEGRITY_ROW_NUM 0x01ff
  30. #define CYAPA_TSG_IMG_MAX_RECORDS (CYAPA_TSG_IMG_END_ROW_NUM - \
  31. CYAPA_TSG_IMG_START_ROW_NUM + 1 + 1)
  32. #define CYAPA_TSG_IMG_READ_SIZE (CYAPA_TSG_FLASH_MAP_BLOCK_SIZE / 2)
  33. #define CYAPA_TSG_START_OF_APPLICATION 0x1700
  34. #define CYAPA_TSG_APP_INTEGRITY_SIZE 60
  35. #define CYAPA_TSG_FLASH_MAP_METADATA_SIZE 60
  36. #define CYAPA_TSG_BL_KEY_SIZE 8
  37. #define CYAPA_TSG_MAX_CMD_SIZE 256
  38. /* Macro of PIP interface */
  39. #define PIP_BL_INITIATE_RESP_LEN 11
  40. #define PIP_BL_FAIL_EXIT_RESP_LEN 11
  41. #define PIP_BL_FAIL_EXIT_STATUS_CODE 0x0c
  42. #define PIP_BL_VERIFY_INTEGRITY_RESP_LEN 12
  43. #define PIP_BL_INTEGRITY_CHEKC_PASS 0x00
  44. #define PIP_BL_BLOCK_WRITE_RESP_LEN 11
  45. #define PIP_TOUCH_REPORT_ID 0x01
  46. #define PIP_BTN_REPORT_ID 0x03
  47. #define PIP_WAKEUP_EVENT_REPORT_ID 0x04
  48. #define PIP_PUSH_BTN_REPORT_ID 0x06
  49. #define GEN5_OLD_PUSH_BTN_REPORT_ID 0x05 /* Special for old Gen5 TP. */
  50. #define PIP_PROXIMITY_REPORT_ID 0x07
  51. #define PIP_PROXIMITY_REPORT_SIZE 6
  52. #define PIP_PROXIMITY_DISTANCE_OFFSET 0x05
  53. #define PIP_PROXIMITY_DISTANCE_MASK 0x01
  54. #define PIP_TOUCH_REPORT_HEAD_SIZE 7
  55. #define PIP_TOUCH_REPORT_MAX_SIZE 127
  56. #define PIP_BTN_REPORT_HEAD_SIZE 6
  57. #define PIP_BTN_REPORT_MAX_SIZE 14
  58. #define PIP_WAKEUP_EVENT_SIZE 4
  59. #define PIP_NUMBER_OF_TOUCH_OFFSET 5
  60. #define PIP_NUMBER_OF_TOUCH_MASK 0x1f
  61. #define PIP_BUTTONS_OFFSET 5
  62. #define PIP_BUTTONS_MASK 0x0f
  63. #define PIP_GET_EVENT_ID(reg) (((reg) >> 5) & 0x03)
  64. #define PIP_GET_TOUCH_ID(reg) ((reg) & 0x1f)
  65. #define PIP_TOUCH_TYPE_FINGER 0x00
  66. #define PIP_TOUCH_TYPE_PROXIMITY 0x01
  67. #define PIP_TOUCH_TYPE_HOVER 0x02
  68. #define PIP_GET_TOUCH_TYPE(reg) ((reg) & 0x07)
  69. #define RECORD_EVENT_NONE 0
  70. #define RECORD_EVENT_TOUCHDOWN 1
  71. #define RECORD_EVENT_DISPLACE 2
  72. #define RECORD_EVENT_LIFTOFF 3
  73. #define PIP_SENSING_MODE_MUTUAL_CAP_FINE 0x00
  74. #define PIP_SENSING_MODE_SELF_CAP 0x02
  75. #define PIP_SET_PROXIMITY 0x49
  76. /* Macro of Gen5 */
  77. #define GEN5_BL_MAX_OUTPUT_LENGTH 0x0100
  78. #define GEN5_APP_MAX_OUTPUT_LENGTH 0x00fe
  79. #define GEN5_POWER_STATE_ACTIVE 0x01
  80. #define GEN5_POWER_STATE_LOOK_FOR_TOUCH 0x02
  81. #define GEN5_POWER_STATE_READY 0x03
  82. #define GEN5_POWER_STATE_IDLE 0x04
  83. #define GEN5_POWER_STATE_BTN_ONLY 0x05
  84. #define GEN5_POWER_STATE_OFF 0x06
  85. #define GEN5_POWER_READY_MAX_INTRVL_TIME 50 /* Unit: ms */
  86. #define GEN5_POWER_IDLE_MAX_INTRVL_TIME 250 /* Unit: ms */
  87. #define GEN5_CMD_GET_PARAMETER 0x05
  88. #define GEN5_CMD_SET_PARAMETER 0x06
  89. #define GEN5_PARAMETER_ACT_INTERVL_ID 0x4d
  90. #define GEN5_PARAMETER_ACT_INTERVL_SIZE 1
  91. #define GEN5_PARAMETER_ACT_LFT_INTERVL_ID 0x4f
  92. #define GEN5_PARAMETER_ACT_LFT_INTERVL_SIZE 2
  93. #define GEN5_PARAMETER_LP_INTRVL_ID 0x4c
  94. #define GEN5_PARAMETER_LP_INTRVL_SIZE 2
  95. #define GEN5_PARAMETER_DISABLE_PIP_REPORT 0x08
  96. #define GEN5_BL_REPORT_DESCRIPTOR_SIZE 0x1d
  97. #define GEN5_BL_REPORT_DESCRIPTOR_ID 0xfe
  98. #define GEN5_APP_REPORT_DESCRIPTOR_SIZE 0xee
  99. #define GEN5_APP_CONTRACT_REPORT_DESCRIPTOR_SIZE 0xfa
  100. #define GEN5_APP_REPORT_DESCRIPTOR_ID 0xf6
  101. #define GEN5_RETRIEVE_MUTUAL_PWC_DATA 0x00
  102. #define GEN5_RETRIEVE_SELF_CAP_PWC_DATA 0x01
  103. #define GEN5_RETRIEVE_DATA_ELEMENT_SIZE_MASK 0x07
  104. #define GEN5_CMD_EXECUTE_PANEL_SCAN 0x2a
  105. #define GEN5_CMD_RETRIEVE_PANEL_SCAN 0x2b
  106. #define GEN5_PANEL_SCAN_MUTUAL_RAW_DATA 0x00
  107. #define GEN5_PANEL_SCAN_MUTUAL_BASELINE 0x01
  108. #define GEN5_PANEL_SCAN_MUTUAL_DIFFCOUNT 0x02
  109. #define GEN5_PANEL_SCAN_SELF_RAW_DATA 0x03
  110. #define GEN5_PANEL_SCAN_SELF_BASELINE 0x04
  111. #define GEN5_PANEL_SCAN_SELF_DIFFCOUNT 0x05
  112. /* The offset only valid for retrieve PWC and panel scan commands */
  113. #define GEN5_RESP_DATA_STRUCTURE_OFFSET 10
  114. #define GEN5_PWC_DATA_ELEMENT_SIZE_MASK 0x07
  115. struct cyapa_pip_touch_record {
  116. /*
  117. * Bit 7 - 3: reserved
  118. * Bit 2 - 0: touch type;
  119. * 0 : standard finger;
  120. * 1 : proximity (Start supported in Gen5 TP).
  121. * 2 : finger hover (defined, but not used yet.)
  122. * 3 - 15 : reserved.
  123. */
  124. u8 touch_type;
  125. /*
  126. * Bit 7: indicates touch liftoff status.
  127. * 0 : touch is currently on the panel.
  128. * 1 : touch record indicates a liftoff.
  129. * Bit 6 - 5: indicates an event associated with this touch instance
  130. * 0 : no event
  131. * 1 : touchdown
  132. * 2 : significant displacement (> active distance)
  133. * 3 : liftoff (record reports last known coordinates)
  134. * Bit 4 - 0: An arbitrary ID tag associated with a finger
  135. * to allow tracking a touch as it moves around the panel.
  136. */
  137. u8 touch_tip_event_id;
  138. /* Bit 7 - 0 of X-axis coordinate of the touch in pixel. */
  139. u8 x_lo;
  140. /* Bit 15 - 8 of X-axis coordinate of the touch in pixel. */
  141. u8 x_hi;
  142. /* Bit 7 - 0 of Y-axis coordinate of the touch in pixel. */
  143. u8 y_lo;
  144. /* Bit 15 - 8 of Y-axis coordinate of the touch in pixel. */
  145. u8 y_hi;
  146. /*
  147. * The meaning of this value is different when touch_type is different.
  148. * For standard finger type:
  149. * Touch intensity in counts, pressure value.
  150. * For proximity type (Start supported in Gen5 TP):
  151. * The distance, in surface units, between the contact and
  152. * the surface.
  153. **/
  154. u8 z;
  155. /*
  156. * The length of the major axis of the ellipse of contact between
  157. * the finger and the panel (ABS_MT_TOUCH_MAJOR).
  158. */
  159. u8 major_axis_len;
  160. /*
  161. * The length of the minor axis of the ellipse of contact between
  162. * the finger and the panel (ABS_MT_TOUCH_MINOR).
  163. */
  164. u8 minor_axis_len;
  165. /*
  166. * The length of the major axis of the approaching tool.
  167. * (ABS_MT_WIDTH_MAJOR)
  168. */
  169. u8 major_tool_len;
  170. /*
  171. * The length of the minor axis of the approaching tool.
  172. * (ABS_MT_WIDTH_MINOR)
  173. */
  174. u8 minor_tool_len;
  175. /*
  176. * The angle between the panel vertical axis and
  177. * the major axis of the contact ellipse. This value is an 8-bit
  178. * signed integer. The range is -127 to +127 (corresponding to
  179. * -90 degree and +90 degree respectively).
  180. * The positive direction is clockwise from the vertical axis.
  181. * If the ellipse of contact degenerates into a circle,
  182. * orientation is reported as 0.
  183. */
  184. u8 orientation;
  185. } __packed;
  186. struct cyapa_pip_report_data {
  187. u8 report_head[PIP_TOUCH_REPORT_HEAD_SIZE];
  188. struct cyapa_pip_touch_record touch_records[10];
  189. } __packed;
  190. struct cyapa_tsg_bin_image_head {
  191. u8 head_size; /* Unit: bytes, including itself. */
  192. u8 ttda_driver_major_version; /* Reserved as 0. */
  193. u8 ttda_driver_minor_version; /* Reserved as 0. */
  194. u8 fw_major_version;
  195. u8 fw_minor_version;
  196. u8 fw_revision_control_number[8];
  197. u8 silicon_id_hi;
  198. u8 silicon_id_lo;
  199. u8 chip_revision;
  200. u8 family_id;
  201. u8 bl_ver_maj;
  202. u8 bl_ver_min;
  203. } __packed;
  204. struct cyapa_tsg_bin_image_data_record {
  205. u8 flash_array_id;
  206. __be16 row_number;
  207. /* The number of bytes of flash data contained in this record. */
  208. __be16 record_len;
  209. /* The flash program data. */
  210. u8 record_data[CYAPA_TSG_FW_ROW_SIZE];
  211. } __packed;
  212. struct cyapa_tsg_bin_image {
  213. struct cyapa_tsg_bin_image_head image_head;
  214. struct cyapa_tsg_bin_image_data_record records[];
  215. } __packed;
  216. struct pip_bl_packet_start {
  217. u8 sop; /* Start of packet, must be 01h */
  218. u8 cmd_code;
  219. __le16 data_length; /* Size of data parameter start from data[0] */
  220. } __packed;
  221. struct pip_bl_packet_end {
  222. __le16 crc;
  223. u8 eop; /* End of packet, must be 17h */
  224. } __packed;
  225. struct pip_bl_cmd_head {
  226. __le16 addr; /* Output report register address, must be 0004h */
  227. /* Size of packet not including output report register address */
  228. __le16 length;
  229. u8 report_id; /* Bootloader output report id, must be 40h */
  230. u8 rsvd; /* Reserved, must be 0 */
  231. struct pip_bl_packet_start packet_start;
  232. u8 data[]; /* Command data variable based on commands */
  233. } __packed;
  234. /* Initiate bootload command data structure. */
  235. struct pip_bl_initiate_cmd_data {
  236. /* Key must be "A5h 01h 02h 03h FFh FEh FDh 5Ah" */
  237. u8 key[CYAPA_TSG_BL_KEY_SIZE];
  238. u8 metadata_raw_parameter[CYAPA_TSG_FLASH_MAP_METADATA_SIZE];
  239. __le16 metadata_crc;
  240. } __packed;
  241. struct tsg_bl_metadata_row_params {
  242. __le16 size;
  243. __le16 maximum_size;
  244. __le32 app_start;
  245. __le16 app_len;
  246. __le16 app_crc;
  247. __le32 app_entry;
  248. __le32 upgrade_start;
  249. __le16 upgrade_len;
  250. __le16 entry_row_crc;
  251. u8 padding[36]; /* Padding data must be 0 */
  252. __le16 metadata_crc; /* CRC starts at offset of 60 */
  253. } __packed;
  254. /* Bootload program and verify row command data structure */
  255. struct tsg_bl_flash_row_head {
  256. u8 flash_array_id;
  257. __le16 flash_row_id;
  258. u8 flash_data[];
  259. } __packed;
  260. struct pip_app_cmd_head {
  261. __le16 addr; /* Output report register address, must be 0004h */
  262. /* Size of packet not including output report register address */
  263. __le16 length;
  264. u8 report_id; /* Application output report id, must be 2Fh */
  265. u8 rsvd; /* Reserved, must be 0 */
  266. /*
  267. * Bit 7: reserved, must be 0.
  268. * Bit 6-0: command code.
  269. */
  270. u8 cmd_code;
  271. u8 parameter_data[]; /* Parameter data variable based on cmd_code */
  272. } __packed;
  273. /* Application get/set parameter command data structure */
  274. struct gen5_app_set_parameter_data {
  275. u8 parameter_id;
  276. u8 parameter_size;
  277. __le32 value;
  278. } __packed;
  279. struct gen5_app_get_parameter_data {
  280. u8 parameter_id;
  281. } __packed;
  282. struct gen5_retrieve_panel_scan_data {
  283. __le16 read_offset;
  284. __le16 read_elements;
  285. u8 data_id;
  286. } __packed;
  287. u8 pip_read_sys_info[] = { 0x04, 0x00, 0x05, 0x00, 0x2f, 0x00, 0x02 };
  288. u8 pip_bl_read_app_info[] = { 0x04, 0x00, 0x0b, 0x00, 0x40, 0x00,
  289. 0x01, 0x3c, 0x00, 0x00, 0xb0, 0x42, 0x17
  290. };
  291. static u8 cyapa_pip_bl_cmd_key[] = { 0xa5, 0x01, 0x02, 0x03,
  292. 0xff, 0xfe, 0xfd, 0x5a };
  293. static int cyapa_pip_event_process(struct cyapa *cyapa,
  294. struct cyapa_pip_report_data *report_data);
  295. int cyapa_pip_cmd_state_initialize(struct cyapa *cyapa)
  296. {
  297. struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
  298. init_completion(&pip->cmd_ready);
  299. atomic_set(&pip->cmd_issued, 0);
  300. mutex_init(&pip->cmd_lock);
  301. mutex_init(&pip->pm_stage_lock);
  302. pip->pm_stage = CYAPA_PM_DEACTIVE;
  303. pip->resp_sort_func = NULL;
  304. pip->in_progress_cmd = PIP_INVALID_CMD;
  305. pip->resp_data = NULL;
  306. pip->resp_len = NULL;
  307. cyapa->dev_pwr_mode = UNINIT_PWR_MODE;
  308. cyapa->dev_sleep_time = UNINIT_SLEEP_TIME;
  309. return 0;
  310. }
  311. /* Return negative errno, or else the number of bytes read. */
  312. ssize_t cyapa_i2c_pip_read(struct cyapa *cyapa, u8 *buf, size_t size)
  313. {
  314. int ret;
  315. if (size == 0)
  316. return 0;
  317. if (!buf || size > CYAPA_REG_MAP_SIZE)
  318. return -EINVAL;
  319. ret = i2c_master_recv(cyapa->client, buf, size);
  320. if (ret != size)
  321. return (ret < 0) ? ret : -EIO;
  322. return size;
  323. }
  324. /*
  325. * Return a negative errno code else zero on success.
  326. */
  327. ssize_t cyapa_i2c_pip_write(struct cyapa *cyapa, u8 *buf, size_t size)
  328. {
  329. int ret;
  330. if (!buf || !size)
  331. return -EINVAL;
  332. ret = i2c_master_send(cyapa->client, buf, size);
  333. if (ret != size)
  334. return (ret < 0) ? ret : -EIO;
  335. return 0;
  336. }
  337. static void cyapa_set_pip_pm_state(struct cyapa *cyapa,
  338. enum cyapa_pm_stage pm_stage)
  339. {
  340. struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
  341. mutex_lock(&pip->pm_stage_lock);
  342. pip->pm_stage = pm_stage;
  343. mutex_unlock(&pip->pm_stage_lock);
  344. }
  345. static void cyapa_reset_pip_pm_state(struct cyapa *cyapa)
  346. {
  347. struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
  348. /* Indicates the pip->pm_stage is not valid. */
  349. mutex_lock(&pip->pm_stage_lock);
  350. pip->pm_stage = CYAPA_PM_DEACTIVE;
  351. mutex_unlock(&pip->pm_stage_lock);
  352. }
  353. static enum cyapa_pm_stage cyapa_get_pip_pm_state(struct cyapa *cyapa)
  354. {
  355. struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
  356. enum cyapa_pm_stage pm_stage;
  357. mutex_lock(&pip->pm_stage_lock);
  358. pm_stage = pip->pm_stage;
  359. mutex_unlock(&pip->pm_stage_lock);
  360. return pm_stage;
  361. }
  362. /*
  363. * This function is aimed to dump all not read data in Gen5 trackpad
  364. * before send any command, otherwise, the interrupt line will be blocked.
  365. */
  366. int cyapa_empty_pip_output_data(struct cyapa *cyapa,
  367. u8 *buf, int *len, cb_sort func)
  368. {
  369. struct input_dev *input = cyapa->input;
  370. struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
  371. enum cyapa_pm_stage pm_stage = cyapa_get_pip_pm_state(cyapa);
  372. int length;
  373. int report_count;
  374. int empty_count;
  375. int buf_len;
  376. int error;
  377. buf_len = 0;
  378. if (len) {
  379. buf_len = (*len < CYAPA_REG_MAP_SIZE) ?
  380. *len : CYAPA_REG_MAP_SIZE;
  381. *len = 0;
  382. }
  383. report_count = 8; /* max 7 pending data before command response data */
  384. empty_count = 0;
  385. do {
  386. /*
  387. * Depending on testing in cyapa driver, there are max 5 "02 00"
  388. * packets between two valid buffered data report in firmware.
  389. * So in order to dump all buffered data out and
  390. * make interrupt line release for reassert again,
  391. * we must set the empty_count check value bigger than 5 to
  392. * make it work. Otherwise, in some situation,
  393. * the interrupt line may unable to reactive again,
  394. * which will cause trackpad device unable to
  395. * report data any more.
  396. * for example, it may happen in EFT and ESD testing.
  397. */
  398. if (empty_count > 5)
  399. return 0;
  400. error = cyapa_i2c_pip_read(cyapa, pip->empty_buf,
  401. PIP_RESP_LENGTH_SIZE);
  402. if (error < 0)
  403. return error;
  404. length = get_unaligned_le16(pip->empty_buf);
  405. if (length == PIP_RESP_LENGTH_SIZE) {
  406. empty_count++;
  407. continue;
  408. } else if (length > CYAPA_REG_MAP_SIZE) {
  409. /* Should not happen */
  410. return -EINVAL;
  411. } else if (length == 0) {
  412. /* Application or bootloader launch data polled out. */
  413. length = PIP_RESP_LENGTH_SIZE;
  414. if (buf && buf_len && func &&
  415. func(cyapa, pip->empty_buf, length)) {
  416. length = min(buf_len, length);
  417. memcpy(buf, pip->empty_buf, length);
  418. *len = length;
  419. /* Response found, success. */
  420. return 0;
  421. }
  422. continue;
  423. }
  424. error = cyapa_i2c_pip_read(cyapa, pip->empty_buf, length);
  425. if (error < 0)
  426. return error;
  427. report_count--;
  428. empty_count = 0;
  429. length = get_unaligned_le16(pip->empty_buf);
  430. if (length <= PIP_RESP_LENGTH_SIZE) {
  431. empty_count++;
  432. } else if (buf && buf_len && func &&
  433. func(cyapa, pip->empty_buf, length)) {
  434. length = min(buf_len, length);
  435. memcpy(buf, pip->empty_buf, length);
  436. *len = length;
  437. /* Response found, success. */
  438. return 0;
  439. } else if (cyapa->operational &&
  440. input && input_device_enabled(input) &&
  441. (pm_stage == CYAPA_PM_RUNTIME_RESUME ||
  442. pm_stage == CYAPA_PM_RUNTIME_SUSPEND)) {
  443. /* Parse the data and report it if it's valid. */
  444. cyapa_pip_event_process(cyapa,
  445. (struct cyapa_pip_report_data *)pip->empty_buf);
  446. }
  447. error = -EINVAL;
  448. } while (report_count);
  449. return error;
  450. }
  451. static int cyapa_do_i2c_pip_cmd_irq_sync(
  452. struct cyapa *cyapa,
  453. u8 *cmd, size_t cmd_len,
  454. unsigned long timeout)
  455. {
  456. struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
  457. int error;
  458. /* Wait for interrupt to set ready completion */
  459. init_completion(&pip->cmd_ready);
  460. atomic_inc(&pip->cmd_issued);
  461. error = cyapa_i2c_pip_write(cyapa, cmd, cmd_len);
  462. if (error) {
  463. atomic_dec(&pip->cmd_issued);
  464. return (error < 0) ? error : -EIO;
  465. }
  466. /* Wait for interrupt to indicate command is completed. */
  467. timeout = wait_for_completion_timeout(&pip->cmd_ready,
  468. msecs_to_jiffies(timeout));
  469. if (timeout == 0) {
  470. atomic_dec(&pip->cmd_issued);
  471. return -ETIMEDOUT;
  472. }
  473. return 0;
  474. }
  475. static int cyapa_do_i2c_pip_cmd_polling(
  476. struct cyapa *cyapa,
  477. u8 *cmd, size_t cmd_len,
  478. u8 *resp_data, int *resp_len,
  479. unsigned long timeout,
  480. cb_sort func)
  481. {
  482. struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
  483. int tries;
  484. int length;
  485. int error;
  486. atomic_inc(&pip->cmd_issued);
  487. error = cyapa_i2c_pip_write(cyapa, cmd, cmd_len);
  488. if (error) {
  489. atomic_dec(&pip->cmd_issued);
  490. return error < 0 ? error : -EIO;
  491. }
  492. length = resp_len ? *resp_len : 0;
  493. if (resp_data && resp_len && length != 0 && func) {
  494. tries = timeout / 5;
  495. do {
  496. usleep_range(3000, 5000);
  497. *resp_len = length;
  498. error = cyapa_empty_pip_output_data(cyapa,
  499. resp_data, resp_len, func);
  500. if (error || *resp_len == 0)
  501. continue;
  502. else
  503. break;
  504. } while (--tries > 0);
  505. if ((error || *resp_len == 0) || tries <= 0)
  506. error = error ? error : -ETIMEDOUT;
  507. }
  508. atomic_dec(&pip->cmd_issued);
  509. return error;
  510. }
  511. int cyapa_i2c_pip_cmd_irq_sync(
  512. struct cyapa *cyapa,
  513. u8 *cmd, int cmd_len,
  514. u8 *resp_data, int *resp_len,
  515. unsigned long timeout,
  516. cb_sort func,
  517. bool irq_mode)
  518. {
  519. struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
  520. int error;
  521. if (!cmd || !cmd_len)
  522. return -EINVAL;
  523. /* Commands must be serialized. */
  524. error = mutex_lock_interruptible(&pip->cmd_lock);
  525. if (error)
  526. return error;
  527. pip->resp_sort_func = func;
  528. pip->resp_data = resp_data;
  529. pip->resp_len = resp_len;
  530. if (cmd_len >= PIP_MIN_APP_CMD_LENGTH &&
  531. cmd[4] == PIP_APP_CMD_REPORT_ID) {
  532. /* Application command */
  533. pip->in_progress_cmd = cmd[6] & 0x7f;
  534. } else if (cmd_len >= PIP_MIN_BL_CMD_LENGTH &&
  535. cmd[4] == PIP_BL_CMD_REPORT_ID) {
  536. /* Bootloader command */
  537. pip->in_progress_cmd = cmd[7];
  538. }
  539. /* Send command data, wait and read output response data's length. */
  540. if (irq_mode) {
  541. pip->is_irq_mode = true;
  542. error = cyapa_do_i2c_pip_cmd_irq_sync(cyapa, cmd, cmd_len,
  543. timeout);
  544. if (error == -ETIMEDOUT && resp_data &&
  545. resp_len && *resp_len != 0 && func) {
  546. /*
  547. * For some old version, there was no interrupt for
  548. * the command response data, so need to poll here
  549. * to try to get the response data.
  550. */
  551. error = cyapa_empty_pip_output_data(cyapa,
  552. resp_data, resp_len, func);
  553. if (error || *resp_len == 0)
  554. error = error ? error : -ETIMEDOUT;
  555. }
  556. } else {
  557. pip->is_irq_mode = false;
  558. error = cyapa_do_i2c_pip_cmd_polling(cyapa, cmd, cmd_len,
  559. resp_data, resp_len, timeout, func);
  560. }
  561. pip->resp_sort_func = NULL;
  562. pip->resp_data = NULL;
  563. pip->resp_len = NULL;
  564. pip->in_progress_cmd = PIP_INVALID_CMD;
  565. mutex_unlock(&pip->cmd_lock);
  566. return error;
  567. }
  568. bool cyapa_sort_tsg_pip_bl_resp_data(struct cyapa *cyapa,
  569. u8 *data, int len)
  570. {
  571. if (!data || len < PIP_MIN_BL_RESP_LENGTH)
  572. return false;
  573. /* Bootloader input report id 30h */
  574. if (data[PIP_RESP_REPORT_ID_OFFSET] == PIP_BL_RESP_REPORT_ID &&
  575. data[PIP_RESP_RSVD_OFFSET] == PIP_RESP_RSVD_KEY &&
  576. data[PIP_RESP_BL_SOP_OFFSET] == PIP_SOP_KEY)
  577. return true;
  578. return false;
  579. }
  580. bool cyapa_sort_tsg_pip_app_resp_data(struct cyapa *cyapa,
  581. u8 *data, int len)
  582. {
  583. struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
  584. int resp_len;
  585. if (!data || len < PIP_MIN_APP_RESP_LENGTH)
  586. return false;
  587. if (data[PIP_RESP_REPORT_ID_OFFSET] == PIP_APP_RESP_REPORT_ID &&
  588. data[PIP_RESP_RSVD_OFFSET] == PIP_RESP_RSVD_KEY) {
  589. resp_len = get_unaligned_le16(&data[PIP_RESP_LENGTH_OFFSET]);
  590. if (GET_PIP_CMD_CODE(data[PIP_RESP_APP_CMD_OFFSET]) == 0x00 &&
  591. resp_len == PIP_UNSUPPORTED_CMD_RESP_LENGTH &&
  592. data[5] == pip->in_progress_cmd) {
  593. /* Unsupported command code */
  594. return false;
  595. } else if (GET_PIP_CMD_CODE(data[PIP_RESP_APP_CMD_OFFSET]) ==
  596. pip->in_progress_cmd) {
  597. /* Correct command response received */
  598. return true;
  599. }
  600. }
  601. return false;
  602. }
  603. static bool cyapa_sort_pip_application_launch_data(struct cyapa *cyapa,
  604. u8 *buf, int len)
  605. {
  606. if (buf == NULL || len < PIP_RESP_LENGTH_SIZE)
  607. return false;
  608. /*
  609. * After reset or power on, trackpad device always sets to 0x00 0x00
  610. * to indicate a reset or power on event.
  611. */
  612. if (buf[0] == 0 && buf[1] == 0)
  613. return true;
  614. return false;
  615. }
  616. static bool cyapa_sort_gen5_hid_descriptor_data(struct cyapa *cyapa,
  617. u8 *buf, int len)
  618. {
  619. int resp_len;
  620. int max_output_len;
  621. /* Check hid descriptor. */
  622. if (len != PIP_HID_DESCRIPTOR_SIZE)
  623. return false;
  624. resp_len = get_unaligned_le16(&buf[PIP_RESP_LENGTH_OFFSET]);
  625. max_output_len = get_unaligned_le16(&buf[16]);
  626. if (resp_len == PIP_HID_DESCRIPTOR_SIZE) {
  627. if (buf[PIP_RESP_REPORT_ID_OFFSET] == PIP_HID_BL_REPORT_ID &&
  628. max_output_len == GEN5_BL_MAX_OUTPUT_LENGTH) {
  629. /* BL mode HID Descriptor */
  630. return true;
  631. } else if ((buf[PIP_RESP_REPORT_ID_OFFSET] ==
  632. PIP_HID_APP_REPORT_ID) &&
  633. max_output_len == GEN5_APP_MAX_OUTPUT_LENGTH) {
  634. /* APP mode HID Descriptor */
  635. return true;
  636. }
  637. }
  638. return false;
  639. }
  640. static bool cyapa_sort_pip_deep_sleep_data(struct cyapa *cyapa,
  641. u8 *buf, int len)
  642. {
  643. if (len == PIP_DEEP_SLEEP_RESP_LENGTH &&
  644. buf[PIP_RESP_REPORT_ID_OFFSET] ==
  645. PIP_APP_DEEP_SLEEP_REPORT_ID &&
  646. (buf[4] & PIP_DEEP_SLEEP_OPCODE_MASK) ==
  647. PIP_DEEP_SLEEP_OPCODE)
  648. return true;
  649. return false;
  650. }
  651. static int gen5_idle_state_parse(struct cyapa *cyapa)
  652. {
  653. u8 resp_data[PIP_HID_DESCRIPTOR_SIZE];
  654. int max_output_len;
  655. int length;
  656. u8 cmd[2];
  657. int ret;
  658. int error;
  659. /*
  660. * Dump all buffered data firstly for the situation
  661. * when the trackpad is just power on the cyapa go here.
  662. */
  663. cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);
  664. memset(resp_data, 0, sizeof(resp_data));
  665. ret = cyapa_i2c_pip_read(cyapa, resp_data, 3);
  666. if (ret != 3)
  667. return ret < 0 ? ret : -EIO;
  668. length = get_unaligned_le16(&resp_data[PIP_RESP_LENGTH_OFFSET]);
  669. if (length == PIP_RESP_LENGTH_SIZE) {
  670. /* Normal state of Gen5 with no data to response */
  671. cyapa->gen = CYAPA_GEN5;
  672. cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);
  673. /* Read description from trackpad device */
  674. cmd[0] = 0x01;
  675. cmd[1] = 0x00;
  676. length = PIP_HID_DESCRIPTOR_SIZE;
  677. error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
  678. cmd, PIP_RESP_LENGTH_SIZE,
  679. resp_data, &length,
  680. 300,
  681. cyapa_sort_gen5_hid_descriptor_data,
  682. false);
  683. if (error)
  684. return error;
  685. length = get_unaligned_le16(
  686. &resp_data[PIP_RESP_LENGTH_OFFSET]);
  687. max_output_len = get_unaligned_le16(&resp_data[16]);
  688. if ((length == PIP_HID_DESCRIPTOR_SIZE ||
  689. length == PIP_RESP_LENGTH_SIZE) &&
  690. (resp_data[PIP_RESP_REPORT_ID_OFFSET] ==
  691. PIP_HID_BL_REPORT_ID) &&
  692. max_output_len == GEN5_BL_MAX_OUTPUT_LENGTH) {
  693. /* BL mode HID Description read */
  694. cyapa->state = CYAPA_STATE_GEN5_BL;
  695. } else if ((length == PIP_HID_DESCRIPTOR_SIZE ||
  696. length == PIP_RESP_LENGTH_SIZE) &&
  697. (resp_data[PIP_RESP_REPORT_ID_OFFSET] ==
  698. PIP_HID_APP_REPORT_ID) &&
  699. max_output_len == GEN5_APP_MAX_OUTPUT_LENGTH) {
  700. /* APP mode HID Description read */
  701. cyapa->state = CYAPA_STATE_GEN5_APP;
  702. } else {
  703. /* Should not happen!!! */
  704. cyapa->state = CYAPA_STATE_NO_DEVICE;
  705. }
  706. }
  707. return 0;
  708. }
  709. static int gen5_hid_description_header_parse(struct cyapa *cyapa, u8 *reg_data)
  710. {
  711. int length;
  712. u8 resp_data[32];
  713. int max_output_len;
  714. int ret;
  715. /* 0x20 0x00 0xF7 is Gen5 Application HID Description Header;
  716. * 0x20 0x00 0xFF is Gen5 Bootloader HID Description Header.
  717. *
  718. * Must read HID Description content through out,
  719. * otherwise Gen5 trackpad cannot response next command
  720. * or report any touch or button data.
  721. */
  722. ret = cyapa_i2c_pip_read(cyapa, resp_data,
  723. PIP_HID_DESCRIPTOR_SIZE);
  724. if (ret != PIP_HID_DESCRIPTOR_SIZE)
  725. return ret < 0 ? ret : -EIO;
  726. length = get_unaligned_le16(&resp_data[PIP_RESP_LENGTH_OFFSET]);
  727. max_output_len = get_unaligned_le16(&resp_data[16]);
  728. if (length == PIP_RESP_LENGTH_SIZE) {
  729. if (reg_data[PIP_RESP_REPORT_ID_OFFSET] ==
  730. PIP_HID_BL_REPORT_ID) {
  731. /*
  732. * BL mode HID Description has been previously
  733. * read out.
  734. */
  735. cyapa->gen = CYAPA_GEN5;
  736. cyapa->state = CYAPA_STATE_GEN5_BL;
  737. } else {
  738. /*
  739. * APP mode HID Description has been previously
  740. * read out.
  741. */
  742. cyapa->gen = CYAPA_GEN5;
  743. cyapa->state = CYAPA_STATE_GEN5_APP;
  744. }
  745. } else if (length == PIP_HID_DESCRIPTOR_SIZE &&
  746. resp_data[2] == PIP_HID_BL_REPORT_ID &&
  747. max_output_len == GEN5_BL_MAX_OUTPUT_LENGTH) {
  748. /* BL mode HID Description read. */
  749. cyapa->gen = CYAPA_GEN5;
  750. cyapa->state = CYAPA_STATE_GEN5_BL;
  751. } else if (length == PIP_HID_DESCRIPTOR_SIZE &&
  752. (resp_data[PIP_RESP_REPORT_ID_OFFSET] ==
  753. PIP_HID_APP_REPORT_ID) &&
  754. max_output_len == GEN5_APP_MAX_OUTPUT_LENGTH) {
  755. /* APP mode HID Description read. */
  756. cyapa->gen = CYAPA_GEN5;
  757. cyapa->state = CYAPA_STATE_GEN5_APP;
  758. } else {
  759. /* Should not happen!!! */
  760. cyapa->state = CYAPA_STATE_NO_DEVICE;
  761. }
  762. return 0;
  763. }
  764. static int gen5_report_data_header_parse(struct cyapa *cyapa, u8 *reg_data)
  765. {
  766. int length;
  767. length = get_unaligned_le16(&reg_data[PIP_RESP_LENGTH_OFFSET]);
  768. switch (reg_data[PIP_RESP_REPORT_ID_OFFSET]) {
  769. case PIP_TOUCH_REPORT_ID:
  770. if (length < PIP_TOUCH_REPORT_HEAD_SIZE ||
  771. length > PIP_TOUCH_REPORT_MAX_SIZE)
  772. return -EINVAL;
  773. break;
  774. case PIP_BTN_REPORT_ID:
  775. case GEN5_OLD_PUSH_BTN_REPORT_ID:
  776. case PIP_PUSH_BTN_REPORT_ID:
  777. if (length < PIP_BTN_REPORT_HEAD_SIZE ||
  778. length > PIP_BTN_REPORT_MAX_SIZE)
  779. return -EINVAL;
  780. break;
  781. case PIP_WAKEUP_EVENT_REPORT_ID:
  782. if (length != PIP_WAKEUP_EVENT_SIZE)
  783. return -EINVAL;
  784. break;
  785. default:
  786. return -EINVAL;
  787. }
  788. cyapa->gen = CYAPA_GEN5;
  789. cyapa->state = CYAPA_STATE_GEN5_APP;
  790. return 0;
  791. }
  792. static int gen5_cmd_resp_header_parse(struct cyapa *cyapa, u8 *reg_data)
  793. {
  794. struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
  795. int length;
  796. int ret;
  797. /*
  798. * Must read report data through out,
  799. * otherwise Gen5 trackpad cannot response next command
  800. * or report any touch or button data.
  801. */
  802. length = get_unaligned_le16(&reg_data[PIP_RESP_LENGTH_OFFSET]);
  803. ret = cyapa_i2c_pip_read(cyapa, pip->empty_buf, length);
  804. if (ret != length)
  805. return ret < 0 ? ret : -EIO;
  806. if (length == PIP_RESP_LENGTH_SIZE) {
  807. /* Previous command has read the data through out. */
  808. if (reg_data[PIP_RESP_REPORT_ID_OFFSET] ==
  809. PIP_BL_RESP_REPORT_ID) {
  810. /* Gen5 BL command response data detected */
  811. cyapa->gen = CYAPA_GEN5;
  812. cyapa->state = CYAPA_STATE_GEN5_BL;
  813. } else {
  814. /* Gen5 APP command response data detected */
  815. cyapa->gen = CYAPA_GEN5;
  816. cyapa->state = CYAPA_STATE_GEN5_APP;
  817. }
  818. } else if ((pip->empty_buf[PIP_RESP_REPORT_ID_OFFSET] ==
  819. PIP_BL_RESP_REPORT_ID) &&
  820. (pip->empty_buf[PIP_RESP_RSVD_OFFSET] ==
  821. PIP_RESP_RSVD_KEY) &&
  822. (pip->empty_buf[PIP_RESP_BL_SOP_OFFSET] ==
  823. PIP_SOP_KEY) &&
  824. (pip->empty_buf[length - 1] ==
  825. PIP_EOP_KEY)) {
  826. /* Gen5 BL command response data detected */
  827. cyapa->gen = CYAPA_GEN5;
  828. cyapa->state = CYAPA_STATE_GEN5_BL;
  829. } else if (pip->empty_buf[PIP_RESP_REPORT_ID_OFFSET] ==
  830. PIP_APP_RESP_REPORT_ID &&
  831. pip->empty_buf[PIP_RESP_RSVD_OFFSET] ==
  832. PIP_RESP_RSVD_KEY) {
  833. /* Gen5 APP command response data detected */
  834. cyapa->gen = CYAPA_GEN5;
  835. cyapa->state = CYAPA_STATE_GEN5_APP;
  836. } else {
  837. /* Should not happen!!! */
  838. cyapa->state = CYAPA_STATE_NO_DEVICE;
  839. }
  840. return 0;
  841. }
  842. static int cyapa_gen5_state_parse(struct cyapa *cyapa, u8 *reg_data, int len)
  843. {
  844. int length;
  845. if (!reg_data || len < 3)
  846. return -EINVAL;
  847. cyapa->state = CYAPA_STATE_NO_DEVICE;
  848. /* Parse based on Gen5 characteristic registers and bits */
  849. length = get_unaligned_le16(&reg_data[PIP_RESP_LENGTH_OFFSET]);
  850. if (length == 0 || length == PIP_RESP_LENGTH_SIZE) {
  851. gen5_idle_state_parse(cyapa);
  852. } else if (length == PIP_HID_DESCRIPTOR_SIZE &&
  853. (reg_data[2] == PIP_HID_BL_REPORT_ID ||
  854. reg_data[2] == PIP_HID_APP_REPORT_ID)) {
  855. gen5_hid_description_header_parse(cyapa, reg_data);
  856. } else if ((length == GEN5_APP_REPORT_DESCRIPTOR_SIZE ||
  857. length == GEN5_APP_CONTRACT_REPORT_DESCRIPTOR_SIZE) &&
  858. reg_data[2] == GEN5_APP_REPORT_DESCRIPTOR_ID) {
  859. /* 0xEE 0x00 0xF6 is Gen5 APP report description header. */
  860. cyapa->gen = CYAPA_GEN5;
  861. cyapa->state = CYAPA_STATE_GEN5_APP;
  862. } else if (length == GEN5_BL_REPORT_DESCRIPTOR_SIZE &&
  863. reg_data[2] == GEN5_BL_REPORT_DESCRIPTOR_ID) {
  864. /* 0x1D 0x00 0xFE is Gen5 BL report descriptor header. */
  865. cyapa->gen = CYAPA_GEN5;
  866. cyapa->state = CYAPA_STATE_GEN5_BL;
  867. } else if (reg_data[2] == PIP_TOUCH_REPORT_ID ||
  868. reg_data[2] == PIP_BTN_REPORT_ID ||
  869. reg_data[2] == GEN5_OLD_PUSH_BTN_REPORT_ID ||
  870. reg_data[2] == PIP_PUSH_BTN_REPORT_ID ||
  871. reg_data[2] == PIP_WAKEUP_EVENT_REPORT_ID) {
  872. gen5_report_data_header_parse(cyapa, reg_data);
  873. } else if (reg_data[2] == PIP_BL_RESP_REPORT_ID ||
  874. reg_data[2] == PIP_APP_RESP_REPORT_ID) {
  875. gen5_cmd_resp_header_parse(cyapa, reg_data);
  876. }
  877. if (cyapa->gen == CYAPA_GEN5) {
  878. /*
  879. * Must read the content (e.g.: report description and so on)
  880. * from trackpad device throughout. Otherwise,
  881. * Gen5 trackpad cannot response to next command or
  882. * report any touch or button data later.
  883. */
  884. cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);
  885. if (cyapa->state == CYAPA_STATE_GEN5_APP ||
  886. cyapa->state == CYAPA_STATE_GEN5_BL)
  887. return 0;
  888. }
  889. return -EAGAIN;
  890. }
  891. static struct cyapa_tsg_bin_image_data_record *
  892. cyapa_get_image_record_data_num(const struct firmware *fw,
  893. int *record_num)
  894. {
  895. int head_size;
  896. head_size = fw->data[0] + 1;
  897. *record_num = (fw->size - head_size) /
  898. sizeof(struct cyapa_tsg_bin_image_data_record);
  899. return (struct cyapa_tsg_bin_image_data_record *)&fw->data[head_size];
  900. }
  901. int cyapa_pip_bl_initiate(struct cyapa *cyapa, const struct firmware *fw)
  902. {
  903. struct cyapa_tsg_bin_image_data_record *image_records;
  904. struct pip_bl_cmd_head *bl_cmd_head;
  905. struct pip_bl_packet_start *bl_packet_start;
  906. struct pip_bl_initiate_cmd_data *cmd_data;
  907. struct pip_bl_packet_end *bl_packet_end;
  908. u8 cmd[CYAPA_TSG_MAX_CMD_SIZE];
  909. int cmd_len;
  910. u16 cmd_data_len;
  911. u16 cmd_crc = 0;
  912. u16 meta_data_crc = 0;
  913. u8 resp_data[11];
  914. int resp_len;
  915. int records_num;
  916. u8 *data;
  917. int error;
  918. /* Try to dump all buffered report data before any send command. */
  919. cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);
  920. memset(cmd, 0, CYAPA_TSG_MAX_CMD_SIZE);
  921. bl_cmd_head = (struct pip_bl_cmd_head *)cmd;
  922. cmd_data_len = CYAPA_TSG_BL_KEY_SIZE + CYAPA_TSG_FLASH_MAP_BLOCK_SIZE;
  923. cmd_len = sizeof(struct pip_bl_cmd_head) + cmd_data_len +
  924. sizeof(struct pip_bl_packet_end);
  925. put_unaligned_le16(PIP_OUTPUT_REPORT_ADDR, &bl_cmd_head->addr);
  926. put_unaligned_le16(cmd_len - 2, &bl_cmd_head->length);
  927. bl_cmd_head->report_id = PIP_BL_CMD_REPORT_ID;
  928. bl_packet_start = &bl_cmd_head->packet_start;
  929. bl_packet_start->sop = PIP_SOP_KEY;
  930. bl_packet_start->cmd_code = PIP_BL_CMD_INITIATE_BL;
  931. /* 8 key bytes and 128 bytes block size */
  932. put_unaligned_le16(cmd_data_len, &bl_packet_start->data_length);
  933. cmd_data = (struct pip_bl_initiate_cmd_data *)bl_cmd_head->data;
  934. memcpy(cmd_data->key, cyapa_pip_bl_cmd_key, CYAPA_TSG_BL_KEY_SIZE);
  935. image_records = cyapa_get_image_record_data_num(fw, &records_num);
  936. /* APP_INTEGRITY row is always the last row block */
  937. data = image_records[records_num - 1].record_data;
  938. memcpy(cmd_data->metadata_raw_parameter, data,
  939. CYAPA_TSG_FLASH_MAP_METADATA_SIZE);
  940. meta_data_crc = crc_itu_t(0xffff, cmd_data->metadata_raw_parameter,
  941. CYAPA_TSG_FLASH_MAP_METADATA_SIZE);
  942. put_unaligned_le16(meta_data_crc, &cmd_data->metadata_crc);
  943. bl_packet_end = (struct pip_bl_packet_end *)(bl_cmd_head->data +
  944. cmd_data_len);
  945. cmd_crc = crc_itu_t(0xffff, (u8 *)bl_packet_start,
  946. sizeof(struct pip_bl_packet_start) + cmd_data_len);
  947. put_unaligned_le16(cmd_crc, &bl_packet_end->crc);
  948. bl_packet_end->eop = PIP_EOP_KEY;
  949. resp_len = sizeof(resp_data);
  950. error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
  951. cmd, cmd_len,
  952. resp_data, &resp_len, 12000,
  953. cyapa_sort_tsg_pip_bl_resp_data, true);
  954. if (error || resp_len != PIP_BL_INITIATE_RESP_LEN ||
  955. resp_data[2] != PIP_BL_RESP_REPORT_ID ||
  956. !PIP_CMD_COMPLETE_SUCCESS(resp_data))
  957. return error ? error : -EAGAIN;
  958. return 0;
  959. }
  960. static bool cyapa_sort_pip_bl_exit_data(struct cyapa *cyapa, u8 *buf, int len)
  961. {
  962. if (buf == NULL || len < PIP_RESP_LENGTH_SIZE)
  963. return false;
  964. if (buf[0] == 0 && buf[1] == 0)
  965. return true;
  966. /* Exit bootloader failed for some reason. */
  967. if (len == PIP_BL_FAIL_EXIT_RESP_LEN &&
  968. buf[PIP_RESP_REPORT_ID_OFFSET] ==
  969. PIP_BL_RESP_REPORT_ID &&
  970. buf[PIP_RESP_RSVD_OFFSET] == PIP_RESP_RSVD_KEY &&
  971. buf[PIP_RESP_BL_SOP_OFFSET] == PIP_SOP_KEY &&
  972. buf[10] == PIP_EOP_KEY)
  973. return true;
  974. return false;
  975. }
  976. int cyapa_pip_bl_exit(struct cyapa *cyapa)
  977. {
  978. u8 bl_gen5_bl_exit[] = { 0x04, 0x00,
  979. 0x0B, 0x00, 0x40, 0x00, 0x01, 0x3b, 0x00, 0x00,
  980. 0x20, 0xc7, 0x17
  981. };
  982. u8 resp_data[11];
  983. int resp_len;
  984. int error;
  985. resp_len = sizeof(resp_data);
  986. error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
  987. bl_gen5_bl_exit, sizeof(bl_gen5_bl_exit),
  988. resp_data, &resp_len,
  989. 5000, cyapa_sort_pip_bl_exit_data, false);
  990. if (error)
  991. return error;
  992. if (resp_len == PIP_BL_FAIL_EXIT_RESP_LEN ||
  993. resp_data[PIP_RESP_REPORT_ID_OFFSET] ==
  994. PIP_BL_RESP_REPORT_ID)
  995. return -EAGAIN;
  996. if (resp_data[0] == 0x00 && resp_data[1] == 0x00)
  997. return 0;
  998. return -ENODEV;
  999. }
  1000. int cyapa_pip_bl_enter(struct cyapa *cyapa)
  1001. {
  1002. u8 cmd[] = { 0x04, 0x00, 0x05, 0x00, 0x2F, 0x00, 0x01 };
  1003. u8 resp_data[2];
  1004. int resp_len;
  1005. int error;
  1006. error = cyapa_poll_state(cyapa, 500);
  1007. if (error < 0)
  1008. return error;
  1009. /* Already in bootloader mode, Skipping exit. */
  1010. if (cyapa_is_pip_bl_mode(cyapa))
  1011. return 0;
  1012. else if (!cyapa_is_pip_app_mode(cyapa))
  1013. return -EINVAL;
  1014. /* Try to dump all buffered report data before any send command. */
  1015. cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);
  1016. /*
  1017. * Send bootloader enter command to trackpad device,
  1018. * after enter bootloader, the response data is two bytes of 0x00 0x00.
  1019. */
  1020. resp_len = sizeof(resp_data);
  1021. memset(resp_data, 0, resp_len);
  1022. error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
  1023. cmd, sizeof(cmd),
  1024. resp_data, &resp_len,
  1025. 5000, cyapa_sort_pip_application_launch_data,
  1026. true);
  1027. if (error || resp_data[0] != 0x00 || resp_data[1] != 0x00)
  1028. return error < 0 ? error : -EAGAIN;
  1029. cyapa->operational = false;
  1030. if (cyapa->gen == CYAPA_GEN5)
  1031. cyapa->state = CYAPA_STATE_GEN5_BL;
  1032. else if (cyapa->gen == CYAPA_GEN6)
  1033. cyapa->state = CYAPA_STATE_GEN6_BL;
  1034. return 0;
  1035. }
  1036. static int cyapa_pip_fw_head_check(struct cyapa *cyapa,
  1037. struct cyapa_tsg_bin_image_head *image_head)
  1038. {
  1039. if (image_head->head_size != 0x0C && image_head->head_size != 0x12)
  1040. return -EINVAL;
  1041. switch (cyapa->gen) {
  1042. case CYAPA_GEN6:
  1043. if (image_head->family_id != 0x9B ||
  1044. image_head->silicon_id_hi != 0x0B)
  1045. return -EINVAL;
  1046. break;
  1047. case CYAPA_GEN5:
  1048. /* Gen5 without proximity support. */
  1049. if (cyapa->platform_ver < 2) {
  1050. if (image_head->head_size == 0x0C)
  1051. break;
  1052. return -EINVAL;
  1053. }
  1054. if (image_head->family_id != 0x91 ||
  1055. image_head->silicon_id_hi != 0x02)
  1056. return -EINVAL;
  1057. break;
  1058. default:
  1059. return -EINVAL;
  1060. }
  1061. return 0;
  1062. }
  1063. int cyapa_pip_check_fw(struct cyapa *cyapa, const struct firmware *fw)
  1064. {
  1065. struct device *dev = &cyapa->client->dev;
  1066. struct cyapa_tsg_bin_image_data_record *image_records;
  1067. const struct cyapa_tsg_bin_image_data_record *app_integrity;
  1068. const struct tsg_bl_metadata_row_params *metadata;
  1069. int flash_records_count;
  1070. u32 fw_app_start, fw_upgrade_start;
  1071. u16 fw_app_len, fw_upgrade_len;
  1072. u16 app_crc;
  1073. u16 app_integrity_crc;
  1074. int i;
  1075. /* Verify the firmware image not miss-used for Gen5 and Gen6. */
  1076. if (cyapa_pip_fw_head_check(cyapa,
  1077. (struct cyapa_tsg_bin_image_head *)fw->data)) {
  1078. dev_err(dev, "%s: firmware image not match TP device.\n",
  1079. __func__);
  1080. return -EINVAL;
  1081. }
  1082. image_records =
  1083. cyapa_get_image_record_data_num(fw, &flash_records_count);
  1084. /*
  1085. * APP_INTEGRITY row is always the last row block,
  1086. * and the row id must be 0x01ff.
  1087. */
  1088. app_integrity = &image_records[flash_records_count - 1];
  1089. if (app_integrity->flash_array_id != 0x00 ||
  1090. get_unaligned_be16(&app_integrity->row_number) != 0x01ff) {
  1091. dev_err(dev, "%s: invalid app_integrity data.\n", __func__);
  1092. return -EINVAL;
  1093. }
  1094. metadata = (const void *)app_integrity->record_data;
  1095. /* Verify app_integrity crc */
  1096. app_integrity_crc = crc_itu_t(0xffff, app_integrity->record_data,
  1097. CYAPA_TSG_APP_INTEGRITY_SIZE);
  1098. if (app_integrity_crc != get_unaligned_le16(&metadata->metadata_crc)) {
  1099. dev_err(dev, "%s: invalid app_integrity crc.\n", __func__);
  1100. return -EINVAL;
  1101. }
  1102. fw_app_start = get_unaligned_le32(&metadata->app_start);
  1103. fw_app_len = get_unaligned_le16(&metadata->app_len);
  1104. fw_upgrade_start = get_unaligned_le32(&metadata->upgrade_start);
  1105. fw_upgrade_len = get_unaligned_le16(&metadata->upgrade_len);
  1106. if (fw_app_start % CYAPA_TSG_FW_ROW_SIZE ||
  1107. fw_app_len % CYAPA_TSG_FW_ROW_SIZE ||
  1108. fw_upgrade_start % CYAPA_TSG_FW_ROW_SIZE ||
  1109. fw_upgrade_len % CYAPA_TSG_FW_ROW_SIZE) {
  1110. dev_err(dev, "%s: invalid image alignment.\n", __func__);
  1111. return -EINVAL;
  1112. }
  1113. /* Verify application image CRC. */
  1114. app_crc = 0xffffU;
  1115. for (i = 0; i < fw_app_len / CYAPA_TSG_FW_ROW_SIZE; i++) {
  1116. const u8 *data = image_records[i].record_data;
  1117. app_crc = crc_itu_t(app_crc, data, CYAPA_TSG_FW_ROW_SIZE);
  1118. }
  1119. if (app_crc != get_unaligned_le16(&metadata->app_crc)) {
  1120. dev_err(dev, "%s: invalid firmware app crc check.\n", __func__);
  1121. return -EINVAL;
  1122. }
  1123. return 0;
  1124. }
  1125. static int cyapa_pip_write_fw_block(struct cyapa *cyapa,
  1126. struct cyapa_tsg_bin_image_data_record *flash_record)
  1127. {
  1128. struct pip_bl_cmd_head *bl_cmd_head;
  1129. struct pip_bl_packet_start *bl_packet_start;
  1130. struct tsg_bl_flash_row_head *flash_row_head;
  1131. struct pip_bl_packet_end *bl_packet_end;
  1132. u8 cmd[CYAPA_TSG_MAX_CMD_SIZE];
  1133. u16 cmd_len;
  1134. u8 flash_array_id;
  1135. u16 flash_row_id;
  1136. u16 record_len;
  1137. u8 *record_data;
  1138. u16 data_len;
  1139. u16 crc;
  1140. u8 resp_data[11];
  1141. int resp_len;
  1142. int error;
  1143. flash_array_id = flash_record->flash_array_id;
  1144. flash_row_id = get_unaligned_be16(&flash_record->row_number);
  1145. record_len = get_unaligned_be16(&flash_record->record_len);
  1146. record_data = flash_record->record_data;
  1147. memset(cmd, 0, CYAPA_TSG_MAX_CMD_SIZE);
  1148. bl_cmd_head = (struct pip_bl_cmd_head *)cmd;
  1149. bl_packet_start = &bl_cmd_head->packet_start;
  1150. cmd_len = sizeof(struct pip_bl_cmd_head) +
  1151. sizeof(struct tsg_bl_flash_row_head) +
  1152. CYAPA_TSG_FLASH_MAP_BLOCK_SIZE +
  1153. sizeof(struct pip_bl_packet_end);
  1154. put_unaligned_le16(PIP_OUTPUT_REPORT_ADDR, &bl_cmd_head->addr);
  1155. /* Don't include 2 bytes register address */
  1156. put_unaligned_le16(cmd_len - 2, &bl_cmd_head->length);
  1157. bl_cmd_head->report_id = PIP_BL_CMD_REPORT_ID;
  1158. bl_packet_start->sop = PIP_SOP_KEY;
  1159. bl_packet_start->cmd_code = PIP_BL_CMD_PROGRAM_VERIFY_ROW;
  1160. /* 1 (Flash Array ID) + 2 (Flash Row ID) + 128 (flash data) */
  1161. data_len = sizeof(struct tsg_bl_flash_row_head) + record_len;
  1162. put_unaligned_le16(data_len, &bl_packet_start->data_length);
  1163. flash_row_head = (struct tsg_bl_flash_row_head *)bl_cmd_head->data;
  1164. flash_row_head->flash_array_id = flash_array_id;
  1165. put_unaligned_le16(flash_row_id, &flash_row_head->flash_row_id);
  1166. memcpy(flash_row_head->flash_data, record_data, record_len);
  1167. bl_packet_end = (struct pip_bl_packet_end *)(bl_cmd_head->data +
  1168. data_len);
  1169. crc = crc_itu_t(0xffff, (u8 *)bl_packet_start,
  1170. sizeof(struct pip_bl_packet_start) + data_len);
  1171. put_unaligned_le16(crc, &bl_packet_end->crc);
  1172. bl_packet_end->eop = PIP_EOP_KEY;
  1173. resp_len = sizeof(resp_data);
  1174. error = cyapa_i2c_pip_cmd_irq_sync(cyapa, cmd, cmd_len,
  1175. resp_data, &resp_len,
  1176. 500, cyapa_sort_tsg_pip_bl_resp_data, true);
  1177. if (error || resp_len != PIP_BL_BLOCK_WRITE_RESP_LEN ||
  1178. resp_data[2] != PIP_BL_RESP_REPORT_ID ||
  1179. !PIP_CMD_COMPLETE_SUCCESS(resp_data))
  1180. return error < 0 ? error : -EAGAIN;
  1181. return 0;
  1182. }
  1183. int cyapa_pip_do_fw_update(struct cyapa *cyapa,
  1184. const struct firmware *fw)
  1185. {
  1186. struct device *dev = &cyapa->client->dev;
  1187. struct cyapa_tsg_bin_image_data_record *image_records;
  1188. int flash_records_count;
  1189. int i;
  1190. int error;
  1191. cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);
  1192. image_records =
  1193. cyapa_get_image_record_data_num(fw, &flash_records_count);
  1194. /*
  1195. * The last flash row 0x01ff has been written through bl_initiate
  1196. * command, so DO NOT write flash 0x01ff to trackpad device.
  1197. */
  1198. for (i = 0; i < (flash_records_count - 1); i++) {
  1199. error = cyapa_pip_write_fw_block(cyapa, &image_records[i]);
  1200. if (error) {
  1201. dev_err(dev, "%s: Gen5 FW update aborted: %d\n",
  1202. __func__, error);
  1203. return error;
  1204. }
  1205. }
  1206. return 0;
  1207. }
  1208. static int cyapa_gen5_change_power_state(struct cyapa *cyapa, u8 power_state)
  1209. {
  1210. u8 cmd[8] = { 0x04, 0x00, 0x06, 0x00, 0x2f, 0x00, 0x08, 0x01 };
  1211. u8 resp_data[6];
  1212. int resp_len;
  1213. int error;
  1214. cmd[7] = power_state;
  1215. resp_len = sizeof(resp_data);
  1216. error = cyapa_i2c_pip_cmd_irq_sync(cyapa, cmd, sizeof(cmd),
  1217. resp_data, &resp_len,
  1218. 500, cyapa_sort_tsg_pip_app_resp_data, false);
  1219. if (error || !VALID_CMD_RESP_HEADER(resp_data, 0x08) ||
  1220. !PIP_CMD_COMPLETE_SUCCESS(resp_data))
  1221. return error < 0 ? error : -EINVAL;
  1222. return 0;
  1223. }
  1224. static int cyapa_gen5_set_interval_time(struct cyapa *cyapa,
  1225. u8 parameter_id, u16 interval_time)
  1226. {
  1227. struct pip_app_cmd_head *app_cmd_head;
  1228. struct gen5_app_set_parameter_data *parameter_data;
  1229. u8 cmd[CYAPA_TSG_MAX_CMD_SIZE];
  1230. int cmd_len;
  1231. u8 resp_data[7];
  1232. int resp_len;
  1233. u8 parameter_size;
  1234. int error;
  1235. memset(cmd, 0, CYAPA_TSG_MAX_CMD_SIZE);
  1236. app_cmd_head = (struct pip_app_cmd_head *)cmd;
  1237. parameter_data = (struct gen5_app_set_parameter_data *)
  1238. app_cmd_head->parameter_data;
  1239. cmd_len = sizeof(struct pip_app_cmd_head) +
  1240. sizeof(struct gen5_app_set_parameter_data);
  1241. switch (parameter_id) {
  1242. case GEN5_PARAMETER_ACT_INTERVL_ID:
  1243. parameter_size = GEN5_PARAMETER_ACT_INTERVL_SIZE;
  1244. break;
  1245. case GEN5_PARAMETER_ACT_LFT_INTERVL_ID:
  1246. parameter_size = GEN5_PARAMETER_ACT_LFT_INTERVL_SIZE;
  1247. break;
  1248. case GEN5_PARAMETER_LP_INTRVL_ID:
  1249. parameter_size = GEN5_PARAMETER_LP_INTRVL_SIZE;
  1250. break;
  1251. default:
  1252. return -EINVAL;
  1253. }
  1254. put_unaligned_le16(PIP_OUTPUT_REPORT_ADDR, &app_cmd_head->addr);
  1255. /*
  1256. * Don't include unused parameter value bytes and
  1257. * 2 bytes register address.
  1258. */
  1259. put_unaligned_le16(cmd_len - (4 - parameter_size) - 2,
  1260. &app_cmd_head->length);
  1261. app_cmd_head->report_id = PIP_APP_CMD_REPORT_ID;
  1262. app_cmd_head->cmd_code = GEN5_CMD_SET_PARAMETER;
  1263. parameter_data->parameter_id = parameter_id;
  1264. parameter_data->parameter_size = parameter_size;
  1265. put_unaligned_le32((u32)interval_time, &parameter_data->value);
  1266. resp_len = sizeof(resp_data);
  1267. error = cyapa_i2c_pip_cmd_irq_sync(cyapa, cmd, cmd_len,
  1268. resp_data, &resp_len,
  1269. 500, cyapa_sort_tsg_pip_app_resp_data, false);
  1270. if (error || resp_data[5] != parameter_id ||
  1271. resp_data[6] != parameter_size ||
  1272. !VALID_CMD_RESP_HEADER(resp_data, GEN5_CMD_SET_PARAMETER))
  1273. return error < 0 ? error : -EINVAL;
  1274. return 0;
  1275. }
  1276. static int cyapa_gen5_get_interval_time(struct cyapa *cyapa,
  1277. u8 parameter_id, u16 *interval_time)
  1278. {
  1279. struct pip_app_cmd_head *app_cmd_head;
  1280. struct gen5_app_get_parameter_data *parameter_data;
  1281. u8 cmd[CYAPA_TSG_MAX_CMD_SIZE];
  1282. int cmd_len;
  1283. u8 resp_data[11];
  1284. int resp_len;
  1285. u8 parameter_size;
  1286. u16 mask, i;
  1287. int error;
  1288. memset(cmd, 0, CYAPA_TSG_MAX_CMD_SIZE);
  1289. app_cmd_head = (struct pip_app_cmd_head *)cmd;
  1290. parameter_data = (struct gen5_app_get_parameter_data *)
  1291. app_cmd_head->parameter_data;
  1292. cmd_len = sizeof(struct pip_app_cmd_head) +
  1293. sizeof(struct gen5_app_get_parameter_data);
  1294. *interval_time = 0;
  1295. switch (parameter_id) {
  1296. case GEN5_PARAMETER_ACT_INTERVL_ID:
  1297. parameter_size = GEN5_PARAMETER_ACT_INTERVL_SIZE;
  1298. break;
  1299. case GEN5_PARAMETER_ACT_LFT_INTERVL_ID:
  1300. parameter_size = GEN5_PARAMETER_ACT_LFT_INTERVL_SIZE;
  1301. break;
  1302. case GEN5_PARAMETER_LP_INTRVL_ID:
  1303. parameter_size = GEN5_PARAMETER_LP_INTRVL_SIZE;
  1304. break;
  1305. default:
  1306. return -EINVAL;
  1307. }
  1308. put_unaligned_le16(PIP_OUTPUT_REPORT_ADDR, &app_cmd_head->addr);
  1309. /* Don't include 2 bytes register address */
  1310. put_unaligned_le16(cmd_len - 2, &app_cmd_head->length);
  1311. app_cmd_head->report_id = PIP_APP_CMD_REPORT_ID;
  1312. app_cmd_head->cmd_code = GEN5_CMD_GET_PARAMETER;
  1313. parameter_data->parameter_id = parameter_id;
  1314. resp_len = sizeof(resp_data);
  1315. error = cyapa_i2c_pip_cmd_irq_sync(cyapa, cmd, cmd_len,
  1316. resp_data, &resp_len,
  1317. 500, cyapa_sort_tsg_pip_app_resp_data, false);
  1318. if (error || resp_data[5] != parameter_id || resp_data[6] == 0 ||
  1319. !VALID_CMD_RESP_HEADER(resp_data, GEN5_CMD_GET_PARAMETER))
  1320. return error < 0 ? error : -EINVAL;
  1321. mask = 0;
  1322. for (i = 0; i < parameter_size; i++)
  1323. mask |= (0xff << (i * 8));
  1324. *interval_time = get_unaligned_le16(&resp_data[7]) & mask;
  1325. return 0;
  1326. }
  1327. static int cyapa_gen5_disable_pip_report(struct cyapa *cyapa)
  1328. {
  1329. struct pip_app_cmd_head *app_cmd_head;
  1330. u8 cmd[10];
  1331. u8 resp_data[7];
  1332. int resp_len;
  1333. int error;
  1334. memset(cmd, 0, sizeof(cmd));
  1335. app_cmd_head = (struct pip_app_cmd_head *)cmd;
  1336. put_unaligned_le16(PIP_OUTPUT_REPORT_ADDR, &app_cmd_head->addr);
  1337. put_unaligned_le16(sizeof(cmd) - 2, &app_cmd_head->length);
  1338. app_cmd_head->report_id = PIP_APP_CMD_REPORT_ID;
  1339. app_cmd_head->cmd_code = GEN5_CMD_SET_PARAMETER;
  1340. app_cmd_head->parameter_data[0] = GEN5_PARAMETER_DISABLE_PIP_REPORT;
  1341. app_cmd_head->parameter_data[1] = 0x01;
  1342. app_cmd_head->parameter_data[2] = 0x01;
  1343. resp_len = sizeof(resp_data);
  1344. error = cyapa_i2c_pip_cmd_irq_sync(cyapa, cmd, sizeof(cmd),
  1345. resp_data, &resp_len,
  1346. 500, cyapa_sort_tsg_pip_app_resp_data, false);
  1347. if (error || resp_data[5] != GEN5_PARAMETER_DISABLE_PIP_REPORT ||
  1348. !VALID_CMD_RESP_HEADER(resp_data, GEN5_CMD_SET_PARAMETER) ||
  1349. resp_data[6] != 0x01)
  1350. return error < 0 ? error : -EINVAL;
  1351. return 0;
  1352. }
  1353. int cyapa_pip_set_proximity(struct cyapa *cyapa, bool enable)
  1354. {
  1355. u8 cmd[] = { 0x04, 0x00, 0x06, 0x00, 0x2f, 0x00, PIP_SET_PROXIMITY,
  1356. (u8)!!enable
  1357. };
  1358. u8 resp_data[6];
  1359. int resp_len;
  1360. int error;
  1361. resp_len = sizeof(resp_data);
  1362. error = cyapa_i2c_pip_cmd_irq_sync(cyapa, cmd, sizeof(cmd),
  1363. resp_data, &resp_len,
  1364. 500, cyapa_sort_tsg_pip_app_resp_data, false);
  1365. if (error || !VALID_CMD_RESP_HEADER(resp_data, PIP_SET_PROXIMITY) ||
  1366. !PIP_CMD_COMPLETE_SUCCESS(resp_data)) {
  1367. error = (error == -ETIMEDOUT) ? -EOPNOTSUPP : error;
  1368. return error < 0 ? error : -EINVAL;
  1369. }
  1370. return 0;
  1371. }
  1372. int cyapa_pip_deep_sleep(struct cyapa *cyapa, u8 state)
  1373. {
  1374. u8 cmd[] = { 0x05, 0x00, 0x00, 0x08};
  1375. u8 resp_data[5];
  1376. int resp_len;
  1377. int error;
  1378. cmd[2] = state & PIP_DEEP_SLEEP_STATE_MASK;
  1379. resp_len = sizeof(resp_data);
  1380. error = cyapa_i2c_pip_cmd_irq_sync(cyapa, cmd, sizeof(cmd),
  1381. resp_data, &resp_len,
  1382. 500, cyapa_sort_pip_deep_sleep_data, false);
  1383. if (error || ((resp_data[3] & PIP_DEEP_SLEEP_STATE_MASK) != state))
  1384. return -EINVAL;
  1385. return 0;
  1386. }
  1387. static int cyapa_gen5_set_power_mode(struct cyapa *cyapa,
  1388. u8 power_mode, u16 sleep_time, enum cyapa_pm_stage pm_stage)
  1389. {
  1390. struct device *dev = &cyapa->client->dev;
  1391. u8 power_state;
  1392. int error = 0;
  1393. if (cyapa->state != CYAPA_STATE_GEN5_APP)
  1394. return 0;
  1395. cyapa_set_pip_pm_state(cyapa, pm_stage);
  1396. if (PIP_DEV_GET_PWR_STATE(cyapa) == UNINIT_PWR_MODE) {
  1397. /*
  1398. * Assume TP in deep sleep mode when driver is loaded,
  1399. * avoid driver unload and reload command IO issue caused by TP
  1400. * has been set into deep sleep mode when unloading.
  1401. */
  1402. PIP_DEV_SET_PWR_STATE(cyapa, PWR_MODE_OFF);
  1403. }
  1404. if (PIP_DEV_UNINIT_SLEEP_TIME(cyapa) &&
  1405. PIP_DEV_GET_PWR_STATE(cyapa) != PWR_MODE_OFF)
  1406. if (cyapa_gen5_get_interval_time(cyapa,
  1407. GEN5_PARAMETER_LP_INTRVL_ID,
  1408. &cyapa->dev_sleep_time) != 0)
  1409. PIP_DEV_SET_SLEEP_TIME(cyapa, UNINIT_SLEEP_TIME);
  1410. if (PIP_DEV_GET_PWR_STATE(cyapa) == power_mode) {
  1411. if (power_mode == PWR_MODE_OFF ||
  1412. power_mode == PWR_MODE_FULL_ACTIVE ||
  1413. power_mode == PWR_MODE_BTN_ONLY ||
  1414. PIP_DEV_GET_SLEEP_TIME(cyapa) == sleep_time) {
  1415. /* Has in correct power mode state, early return. */
  1416. goto out;
  1417. }
  1418. }
  1419. if (power_mode == PWR_MODE_OFF) {
  1420. error = cyapa_pip_deep_sleep(cyapa, PIP_DEEP_SLEEP_STATE_OFF);
  1421. if (error) {
  1422. dev_err(dev, "enter deep sleep fail: %d\n", error);
  1423. goto out;
  1424. }
  1425. PIP_DEV_SET_PWR_STATE(cyapa, PWR_MODE_OFF);
  1426. goto out;
  1427. }
  1428. /*
  1429. * When trackpad in power off mode, it cannot change to other power
  1430. * state directly, must be wake up from sleep firstly, then
  1431. * continue to do next power sate change.
  1432. */
  1433. if (PIP_DEV_GET_PWR_STATE(cyapa) == PWR_MODE_OFF) {
  1434. error = cyapa_pip_deep_sleep(cyapa, PIP_DEEP_SLEEP_STATE_ON);
  1435. if (error) {
  1436. dev_err(dev, "deep sleep wake fail: %d\n", error);
  1437. goto out;
  1438. }
  1439. }
  1440. if (power_mode == PWR_MODE_FULL_ACTIVE) {
  1441. error = cyapa_gen5_change_power_state(cyapa,
  1442. GEN5_POWER_STATE_ACTIVE);
  1443. if (error) {
  1444. dev_err(dev, "change to active fail: %d\n", error);
  1445. goto out;
  1446. }
  1447. PIP_DEV_SET_PWR_STATE(cyapa, PWR_MODE_FULL_ACTIVE);
  1448. } else if (power_mode == PWR_MODE_BTN_ONLY) {
  1449. error = cyapa_gen5_change_power_state(cyapa,
  1450. GEN5_POWER_STATE_BTN_ONLY);
  1451. if (error) {
  1452. dev_err(dev, "fail to button only mode: %d\n", error);
  1453. goto out;
  1454. }
  1455. PIP_DEV_SET_PWR_STATE(cyapa, PWR_MODE_BTN_ONLY);
  1456. } else {
  1457. /*
  1458. * Continue to change power mode even failed to set
  1459. * interval time, it won't affect the power mode change.
  1460. * except the sleep interval time is not correct.
  1461. */
  1462. if (PIP_DEV_UNINIT_SLEEP_TIME(cyapa) ||
  1463. sleep_time != PIP_DEV_GET_SLEEP_TIME(cyapa))
  1464. if (cyapa_gen5_set_interval_time(cyapa,
  1465. GEN5_PARAMETER_LP_INTRVL_ID,
  1466. sleep_time) == 0)
  1467. PIP_DEV_SET_SLEEP_TIME(cyapa, sleep_time);
  1468. if (sleep_time <= GEN5_POWER_READY_MAX_INTRVL_TIME)
  1469. power_state = GEN5_POWER_STATE_READY;
  1470. else
  1471. power_state = GEN5_POWER_STATE_IDLE;
  1472. error = cyapa_gen5_change_power_state(cyapa, power_state);
  1473. if (error) {
  1474. dev_err(dev, "set power state to 0x%02x failed: %d\n",
  1475. power_state, error);
  1476. goto out;
  1477. }
  1478. /*
  1479. * Disable pip report for a little time, firmware will
  1480. * re-enable it automatically. It's used to fix the issue
  1481. * that trackpad unable to report signal to wake system up
  1482. * in the special situation that system is in suspending, and
  1483. * at the same time, user touch trackpad to wake system up.
  1484. * This function can avoid the data to be buffered when system
  1485. * is suspending which may cause interrupt line unable to be
  1486. * asserted again.
  1487. */
  1488. if (pm_stage == CYAPA_PM_SUSPEND)
  1489. cyapa_gen5_disable_pip_report(cyapa);
  1490. PIP_DEV_SET_PWR_STATE(cyapa,
  1491. cyapa_sleep_time_to_pwr_cmd(sleep_time));
  1492. }
  1493. out:
  1494. cyapa_reset_pip_pm_state(cyapa);
  1495. return error;
  1496. }
  1497. int cyapa_pip_resume_scanning(struct cyapa *cyapa)
  1498. {
  1499. u8 cmd[] = { 0x04, 0x00, 0x05, 0x00, 0x2f, 0x00, 0x04 };
  1500. u8 resp_data[6];
  1501. int resp_len;
  1502. int error;
  1503. /* Try to dump all buffered data before doing command. */
  1504. cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);
  1505. resp_len = sizeof(resp_data);
  1506. error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
  1507. cmd, sizeof(cmd),
  1508. resp_data, &resp_len,
  1509. 500, cyapa_sort_tsg_pip_app_resp_data, true);
  1510. if (error || !VALID_CMD_RESP_HEADER(resp_data, 0x04))
  1511. return -EINVAL;
  1512. /* Try to dump all buffered data when resuming scanning. */
  1513. cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);
  1514. return 0;
  1515. }
  1516. int cyapa_pip_suspend_scanning(struct cyapa *cyapa)
  1517. {
  1518. u8 cmd[] = { 0x04, 0x00, 0x05, 0x00, 0x2f, 0x00, 0x03 };
  1519. u8 resp_data[6];
  1520. int resp_len;
  1521. int error;
  1522. /* Try to dump all buffered data before doing command. */
  1523. cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);
  1524. resp_len = sizeof(resp_data);
  1525. error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
  1526. cmd, sizeof(cmd),
  1527. resp_data, &resp_len,
  1528. 500, cyapa_sort_tsg_pip_app_resp_data, true);
  1529. if (error || !VALID_CMD_RESP_HEADER(resp_data, 0x03))
  1530. return -EINVAL;
  1531. /* Try to dump all buffered data when suspending scanning. */
  1532. cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);
  1533. return 0;
  1534. }
  1535. static int cyapa_pip_calibrate_pwcs(struct cyapa *cyapa,
  1536. u8 calibrate_sensing_mode_type)
  1537. {
  1538. struct pip_app_cmd_head *app_cmd_head;
  1539. u8 cmd[8];
  1540. u8 resp_data[6];
  1541. int resp_len;
  1542. int error;
  1543. /* Try to dump all buffered data before doing command. */
  1544. cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);
  1545. memset(cmd, 0, sizeof(cmd));
  1546. app_cmd_head = (struct pip_app_cmd_head *)cmd;
  1547. put_unaligned_le16(PIP_OUTPUT_REPORT_ADDR, &app_cmd_head->addr);
  1548. put_unaligned_le16(sizeof(cmd) - 2, &app_cmd_head->length);
  1549. app_cmd_head->report_id = PIP_APP_CMD_REPORT_ID;
  1550. app_cmd_head->cmd_code = PIP_CMD_CALIBRATE;
  1551. app_cmd_head->parameter_data[0] = calibrate_sensing_mode_type;
  1552. resp_len = sizeof(resp_data);
  1553. error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
  1554. cmd, sizeof(cmd),
  1555. resp_data, &resp_len,
  1556. 5000, cyapa_sort_tsg_pip_app_resp_data, true);
  1557. if (error || !VALID_CMD_RESP_HEADER(resp_data, PIP_CMD_CALIBRATE) ||
  1558. !PIP_CMD_COMPLETE_SUCCESS(resp_data))
  1559. return error < 0 ? error : -EAGAIN;
  1560. return 0;
  1561. }
  1562. ssize_t cyapa_pip_do_calibrate(struct device *dev,
  1563. struct device_attribute *attr,
  1564. const char *buf, size_t count)
  1565. {
  1566. struct cyapa *cyapa = dev_get_drvdata(dev);
  1567. int error, calibrate_error;
  1568. /* 1. Suspend Scanning*/
  1569. error = cyapa_pip_suspend_scanning(cyapa);
  1570. if (error)
  1571. return error;
  1572. /* 2. Do mutual capacitance fine calibrate. */
  1573. calibrate_error = cyapa_pip_calibrate_pwcs(cyapa,
  1574. PIP_SENSING_MODE_MUTUAL_CAP_FINE);
  1575. if (calibrate_error)
  1576. goto resume_scanning;
  1577. /* 3. Do self capacitance calibrate. */
  1578. calibrate_error = cyapa_pip_calibrate_pwcs(cyapa,
  1579. PIP_SENSING_MODE_SELF_CAP);
  1580. if (calibrate_error)
  1581. goto resume_scanning;
  1582. resume_scanning:
  1583. /* 4. Resume Scanning*/
  1584. error = cyapa_pip_resume_scanning(cyapa);
  1585. if (error || calibrate_error)
  1586. return error ? error : calibrate_error;
  1587. return count;
  1588. }
  1589. static s32 twos_complement_to_s32(s32 value, int num_bits)
  1590. {
  1591. if (value >> (num_bits - 1))
  1592. value |= -1 << num_bits;
  1593. return value;
  1594. }
  1595. static s32 cyapa_parse_structure_data(u8 data_format, u8 *buf, int buf_len)
  1596. {
  1597. int data_size;
  1598. bool big_endian;
  1599. bool unsigned_type;
  1600. s32 value;
  1601. data_size = (data_format & 0x07);
  1602. big_endian = ((data_format & 0x10) == 0x00);
  1603. unsigned_type = ((data_format & 0x20) == 0x00);
  1604. if (buf_len < data_size)
  1605. return 0;
  1606. switch (data_size) {
  1607. case 1:
  1608. value = buf[0];
  1609. break;
  1610. case 2:
  1611. if (big_endian)
  1612. value = get_unaligned_be16(buf);
  1613. else
  1614. value = get_unaligned_le16(buf);
  1615. break;
  1616. case 4:
  1617. if (big_endian)
  1618. value = get_unaligned_be32(buf);
  1619. else
  1620. value = get_unaligned_le32(buf);
  1621. break;
  1622. default:
  1623. /* Should not happen, just as default case here. */
  1624. value = 0;
  1625. break;
  1626. }
  1627. if (!unsigned_type)
  1628. value = twos_complement_to_s32(value, data_size * 8);
  1629. return value;
  1630. }
  1631. static void cyapa_gen5_guess_electrodes(struct cyapa *cyapa,
  1632. int *electrodes_rx, int *electrodes_tx)
  1633. {
  1634. if (cyapa->electrodes_rx != 0) {
  1635. *electrodes_rx = cyapa->electrodes_rx;
  1636. *electrodes_tx = (cyapa->electrodes_x == *electrodes_rx) ?
  1637. cyapa->electrodes_y : cyapa->electrodes_x;
  1638. } else {
  1639. *electrodes_tx = min(cyapa->electrodes_x, cyapa->electrodes_y);
  1640. *electrodes_rx = max(cyapa->electrodes_x, cyapa->electrodes_y);
  1641. }
  1642. }
  1643. /*
  1644. * Read all the global mutual or self idac data or mutual or self local PWC
  1645. * data based on the @idac_data_type.
  1646. * If the input value of @data_size is 0, then means read global mutual or
  1647. * self idac data. For read global mutual idac data, @idac_max, @idac_min and
  1648. * @idac_ave are in order used to return the max value of global mutual idac
  1649. * data, the min value of global mutual idac and the average value of the
  1650. * global mutual idac data. For read global self idac data, @idac_max is used
  1651. * to return the global self cap idac data in Rx direction, @idac_min is used
  1652. * to return the global self cap idac data in Tx direction. @idac_ave is not
  1653. * used.
  1654. * If the input value of @data_size is not 0, than means read the mutual or
  1655. * self local PWC data. The @idac_max, @idac_min and @idac_ave are used to
  1656. * return the max, min and average value of the mutual or self local PWC data.
  1657. * Note, in order to read mutual local PWC data, must read invoke this function
  1658. * to read the mutual global idac data firstly to set the correct Rx number
  1659. * value, otherwise, the read mutual idac and PWC data may not correct.
  1660. */
  1661. static int cyapa_gen5_read_idac_data(struct cyapa *cyapa,
  1662. u8 cmd_code, u8 idac_data_type, int *data_size,
  1663. int *idac_max, int *idac_min, int *idac_ave)
  1664. {
  1665. struct pip_app_cmd_head *cmd_head;
  1666. u8 cmd[12];
  1667. u8 resp_data[256];
  1668. int resp_len;
  1669. int read_len;
  1670. int value;
  1671. u16 offset;
  1672. int read_elements;
  1673. bool read_global_idac;
  1674. int sum, count, max_element_cnt;
  1675. int tmp_max, tmp_min, tmp_ave, tmp_sum, tmp_count;
  1676. int electrodes_rx, electrodes_tx;
  1677. int i;
  1678. int error;
  1679. if (cmd_code != PIP_RETRIEVE_DATA_STRUCTURE ||
  1680. (idac_data_type != GEN5_RETRIEVE_MUTUAL_PWC_DATA &&
  1681. idac_data_type != GEN5_RETRIEVE_SELF_CAP_PWC_DATA) ||
  1682. !data_size || !idac_max || !idac_min || !idac_ave)
  1683. return -EINVAL;
  1684. *idac_max = INT_MIN;
  1685. *idac_min = INT_MAX;
  1686. sum = count = tmp_count = 0;
  1687. electrodes_rx = electrodes_tx = 0;
  1688. if (*data_size == 0) {
  1689. /*
  1690. * Read global idac values firstly.
  1691. * Currently, no idac data exceed 4 bytes.
  1692. */
  1693. read_global_idac = true;
  1694. offset = 0;
  1695. *data_size = 4;
  1696. tmp_max = INT_MIN;
  1697. tmp_min = INT_MAX;
  1698. tmp_ave = tmp_sum = tmp_count = 0;
  1699. if (idac_data_type == GEN5_RETRIEVE_MUTUAL_PWC_DATA) {
  1700. if (cyapa->aligned_electrodes_rx == 0) {
  1701. cyapa_gen5_guess_electrodes(cyapa,
  1702. &electrodes_rx, &electrodes_tx);
  1703. cyapa->aligned_electrodes_rx =
  1704. (electrodes_rx + 3) & ~3u;
  1705. }
  1706. max_element_cnt =
  1707. (cyapa->aligned_electrodes_rx + 7) & ~7u;
  1708. } else {
  1709. max_element_cnt = 2;
  1710. }
  1711. } else {
  1712. read_global_idac = false;
  1713. if (*data_size > 4)
  1714. *data_size = 4;
  1715. /* Calculate the start offset in bytes of local PWC data. */
  1716. if (idac_data_type == GEN5_RETRIEVE_MUTUAL_PWC_DATA) {
  1717. offset = cyapa->aligned_electrodes_rx * (*data_size);
  1718. if (cyapa->electrodes_rx == cyapa->electrodes_x)
  1719. electrodes_tx = cyapa->electrodes_y;
  1720. else
  1721. electrodes_tx = cyapa->electrodes_x;
  1722. max_element_cnt = ((cyapa->aligned_electrodes_rx + 7) &
  1723. ~7u) * electrodes_tx;
  1724. } else {
  1725. offset = 2;
  1726. max_element_cnt = cyapa->electrodes_x +
  1727. cyapa->electrodes_y;
  1728. max_element_cnt = (max_element_cnt + 3) & ~3u;
  1729. }
  1730. }
  1731. memset(cmd, 0, sizeof(cmd));
  1732. cmd_head = (struct pip_app_cmd_head *)cmd;
  1733. put_unaligned_le16(PIP_OUTPUT_REPORT_ADDR, &cmd_head->addr);
  1734. put_unaligned_le16(sizeof(cmd) - 2, &cmd_head->length);
  1735. cmd_head->report_id = PIP_APP_CMD_REPORT_ID;
  1736. cmd_head->cmd_code = cmd_code;
  1737. do {
  1738. read_elements = (256 - GEN5_RESP_DATA_STRUCTURE_OFFSET) /
  1739. (*data_size);
  1740. read_elements = min(read_elements, max_element_cnt - count);
  1741. read_len = read_elements * (*data_size);
  1742. put_unaligned_le16(offset, &cmd_head->parameter_data[0]);
  1743. put_unaligned_le16(read_len, &cmd_head->parameter_data[2]);
  1744. cmd_head->parameter_data[4] = idac_data_type;
  1745. resp_len = GEN5_RESP_DATA_STRUCTURE_OFFSET + read_len;
  1746. error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
  1747. cmd, sizeof(cmd),
  1748. resp_data, &resp_len,
  1749. 500, cyapa_sort_tsg_pip_app_resp_data,
  1750. true);
  1751. if (error || resp_len < GEN5_RESP_DATA_STRUCTURE_OFFSET ||
  1752. !VALID_CMD_RESP_HEADER(resp_data, cmd_code) ||
  1753. !PIP_CMD_COMPLETE_SUCCESS(resp_data) ||
  1754. resp_data[6] != idac_data_type)
  1755. return (error < 0) ? error : -EAGAIN;
  1756. read_len = get_unaligned_le16(&resp_data[7]);
  1757. if (read_len == 0)
  1758. break;
  1759. *data_size = (resp_data[9] & GEN5_PWC_DATA_ELEMENT_SIZE_MASK);
  1760. if (read_len < *data_size)
  1761. return -EINVAL;
  1762. if (read_global_idac &&
  1763. idac_data_type == GEN5_RETRIEVE_SELF_CAP_PWC_DATA) {
  1764. /* Rx's self global idac data. */
  1765. *idac_max = cyapa_parse_structure_data(
  1766. resp_data[9],
  1767. &resp_data[GEN5_RESP_DATA_STRUCTURE_OFFSET],
  1768. *data_size);
  1769. /* Tx's self global idac data. */
  1770. *idac_min = cyapa_parse_structure_data(
  1771. resp_data[9],
  1772. &resp_data[GEN5_RESP_DATA_STRUCTURE_OFFSET +
  1773. *data_size],
  1774. *data_size);
  1775. break;
  1776. }
  1777. /* Read mutual global idac or local mutual/self PWC data. */
  1778. offset += read_len;
  1779. for (i = 10; i < (read_len + GEN5_RESP_DATA_STRUCTURE_OFFSET);
  1780. i += *data_size) {
  1781. value = cyapa_parse_structure_data(resp_data[9],
  1782. &resp_data[i], *data_size);
  1783. *idac_min = min(value, *idac_min);
  1784. *idac_max = max(value, *idac_max);
  1785. if (idac_data_type == GEN5_RETRIEVE_MUTUAL_PWC_DATA &&
  1786. tmp_count < cyapa->aligned_electrodes_rx &&
  1787. read_global_idac) {
  1788. /*
  1789. * The value gap between global and local mutual
  1790. * idac data must bigger than 50%.
  1791. * Normally, global value bigger than 50,
  1792. * local values less than 10.
  1793. */
  1794. if (!tmp_ave || value > tmp_ave / 2) {
  1795. tmp_min = min(value, tmp_min);
  1796. tmp_max = max(value, tmp_max);
  1797. tmp_sum += value;
  1798. tmp_count++;
  1799. tmp_ave = tmp_sum / tmp_count;
  1800. }
  1801. }
  1802. sum += value;
  1803. count++;
  1804. if (count >= max_element_cnt)
  1805. goto out;
  1806. }
  1807. } while (true);
  1808. out:
  1809. *idac_ave = count ? (sum / count) : 0;
  1810. if (read_global_idac &&
  1811. idac_data_type == GEN5_RETRIEVE_MUTUAL_PWC_DATA) {
  1812. if (tmp_count == 0)
  1813. return 0;
  1814. if (tmp_count == cyapa->aligned_electrodes_rx) {
  1815. cyapa->electrodes_rx = cyapa->electrodes_rx ?
  1816. cyapa->electrodes_rx : electrodes_rx;
  1817. } else if (tmp_count == electrodes_rx) {
  1818. cyapa->electrodes_rx = cyapa->electrodes_rx ?
  1819. cyapa->electrodes_rx : electrodes_rx;
  1820. cyapa->aligned_electrodes_rx = electrodes_rx;
  1821. } else {
  1822. cyapa->electrodes_rx = cyapa->electrodes_rx ?
  1823. cyapa->electrodes_rx : electrodes_tx;
  1824. cyapa->aligned_electrodes_rx = tmp_count;
  1825. }
  1826. *idac_min = tmp_min;
  1827. *idac_max = tmp_max;
  1828. *idac_ave = tmp_ave;
  1829. }
  1830. return 0;
  1831. }
  1832. static int cyapa_gen5_read_mutual_idac_data(struct cyapa *cyapa,
  1833. int *gidac_mutual_max, int *gidac_mutual_min, int *gidac_mutual_ave,
  1834. int *lidac_mutual_max, int *lidac_mutual_min, int *lidac_mutual_ave)
  1835. {
  1836. int data_size;
  1837. int error;
  1838. *gidac_mutual_max = *gidac_mutual_min = *gidac_mutual_ave = 0;
  1839. *lidac_mutual_max = *lidac_mutual_min = *lidac_mutual_ave = 0;
  1840. data_size = 0;
  1841. error = cyapa_gen5_read_idac_data(cyapa,
  1842. PIP_RETRIEVE_DATA_STRUCTURE,
  1843. GEN5_RETRIEVE_MUTUAL_PWC_DATA,
  1844. &data_size,
  1845. gidac_mutual_max, gidac_mutual_min, gidac_mutual_ave);
  1846. if (error)
  1847. return error;
  1848. error = cyapa_gen5_read_idac_data(cyapa,
  1849. PIP_RETRIEVE_DATA_STRUCTURE,
  1850. GEN5_RETRIEVE_MUTUAL_PWC_DATA,
  1851. &data_size,
  1852. lidac_mutual_max, lidac_mutual_min, lidac_mutual_ave);
  1853. return error;
  1854. }
  1855. static int cyapa_gen5_read_self_idac_data(struct cyapa *cyapa,
  1856. int *gidac_self_rx, int *gidac_self_tx,
  1857. int *lidac_self_max, int *lidac_self_min, int *lidac_self_ave)
  1858. {
  1859. int data_size;
  1860. int error;
  1861. *gidac_self_rx = *gidac_self_tx = 0;
  1862. *lidac_self_max = *lidac_self_min = *lidac_self_ave = 0;
  1863. data_size = 0;
  1864. error = cyapa_gen5_read_idac_data(cyapa,
  1865. PIP_RETRIEVE_DATA_STRUCTURE,
  1866. GEN5_RETRIEVE_SELF_CAP_PWC_DATA,
  1867. &data_size,
  1868. lidac_self_max, lidac_self_min, lidac_self_ave);
  1869. if (error)
  1870. return error;
  1871. *gidac_self_rx = *lidac_self_max;
  1872. *gidac_self_tx = *lidac_self_min;
  1873. error = cyapa_gen5_read_idac_data(cyapa,
  1874. PIP_RETRIEVE_DATA_STRUCTURE,
  1875. GEN5_RETRIEVE_SELF_CAP_PWC_DATA,
  1876. &data_size,
  1877. lidac_self_max, lidac_self_min, lidac_self_ave);
  1878. return error;
  1879. }
  1880. static ssize_t cyapa_gen5_execute_panel_scan(struct cyapa *cyapa)
  1881. {
  1882. struct pip_app_cmd_head *app_cmd_head;
  1883. u8 cmd[7];
  1884. u8 resp_data[6];
  1885. int resp_len;
  1886. int error;
  1887. memset(cmd, 0, sizeof(cmd));
  1888. app_cmd_head = (struct pip_app_cmd_head *)cmd;
  1889. put_unaligned_le16(PIP_OUTPUT_REPORT_ADDR, &app_cmd_head->addr);
  1890. put_unaligned_le16(sizeof(cmd) - 2, &app_cmd_head->length);
  1891. app_cmd_head->report_id = PIP_APP_CMD_REPORT_ID;
  1892. app_cmd_head->cmd_code = GEN5_CMD_EXECUTE_PANEL_SCAN;
  1893. resp_len = sizeof(resp_data);
  1894. error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
  1895. cmd, sizeof(cmd),
  1896. resp_data, &resp_len,
  1897. 500, cyapa_sort_tsg_pip_app_resp_data, true);
  1898. if (error || resp_len != sizeof(resp_data) ||
  1899. !VALID_CMD_RESP_HEADER(resp_data,
  1900. GEN5_CMD_EXECUTE_PANEL_SCAN) ||
  1901. !PIP_CMD_COMPLETE_SUCCESS(resp_data))
  1902. return error ? error : -EAGAIN;
  1903. return 0;
  1904. }
  1905. static int cyapa_gen5_read_panel_scan_raw_data(struct cyapa *cyapa,
  1906. u8 cmd_code, u8 raw_data_type, int raw_data_max_num,
  1907. int *raw_data_max, int *raw_data_min, int *raw_data_ave,
  1908. u8 *buffer)
  1909. {
  1910. struct pip_app_cmd_head *app_cmd_head;
  1911. struct gen5_retrieve_panel_scan_data *panel_sacn_data;
  1912. u8 cmd[12];
  1913. u8 resp_data[256]; /* Max bytes can transfer one time. */
  1914. int resp_len;
  1915. int read_elements;
  1916. int read_len;
  1917. u16 offset;
  1918. s32 value;
  1919. int sum, count;
  1920. int data_size;
  1921. s32 *intp;
  1922. int i;
  1923. int error;
  1924. if (cmd_code != GEN5_CMD_RETRIEVE_PANEL_SCAN ||
  1925. (raw_data_type > GEN5_PANEL_SCAN_SELF_DIFFCOUNT) ||
  1926. !raw_data_max || !raw_data_min || !raw_data_ave)
  1927. return -EINVAL;
  1928. intp = (s32 *)buffer;
  1929. *raw_data_max = INT_MIN;
  1930. *raw_data_min = INT_MAX;
  1931. sum = count = 0;
  1932. offset = 0;
  1933. /* Assume max element size is 4 currently. */
  1934. read_elements = (256 - GEN5_RESP_DATA_STRUCTURE_OFFSET) / 4;
  1935. read_len = read_elements * 4;
  1936. app_cmd_head = (struct pip_app_cmd_head *)cmd;
  1937. put_unaligned_le16(PIP_OUTPUT_REPORT_ADDR, &app_cmd_head->addr);
  1938. put_unaligned_le16(sizeof(cmd) - 2, &app_cmd_head->length);
  1939. app_cmd_head->report_id = PIP_APP_CMD_REPORT_ID;
  1940. app_cmd_head->cmd_code = cmd_code;
  1941. panel_sacn_data = (struct gen5_retrieve_panel_scan_data *)
  1942. app_cmd_head->parameter_data;
  1943. do {
  1944. put_unaligned_le16(offset, &panel_sacn_data->read_offset);
  1945. put_unaligned_le16(read_elements,
  1946. &panel_sacn_data->read_elements);
  1947. panel_sacn_data->data_id = raw_data_type;
  1948. resp_len = GEN5_RESP_DATA_STRUCTURE_OFFSET + read_len;
  1949. error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
  1950. cmd, sizeof(cmd),
  1951. resp_data, &resp_len,
  1952. 500, cyapa_sort_tsg_pip_app_resp_data, true);
  1953. if (error || resp_len < GEN5_RESP_DATA_STRUCTURE_OFFSET ||
  1954. !VALID_CMD_RESP_HEADER(resp_data, cmd_code) ||
  1955. !PIP_CMD_COMPLETE_SUCCESS(resp_data) ||
  1956. resp_data[6] != raw_data_type)
  1957. return error ? error : -EAGAIN;
  1958. read_elements = get_unaligned_le16(&resp_data[7]);
  1959. if (read_elements == 0)
  1960. break;
  1961. data_size = (resp_data[9] & GEN5_PWC_DATA_ELEMENT_SIZE_MASK);
  1962. offset += read_elements;
  1963. if (read_elements) {
  1964. for (i = GEN5_RESP_DATA_STRUCTURE_OFFSET;
  1965. i < (read_elements * data_size +
  1966. GEN5_RESP_DATA_STRUCTURE_OFFSET);
  1967. i += data_size) {
  1968. value = cyapa_parse_structure_data(resp_data[9],
  1969. &resp_data[i], data_size);
  1970. *raw_data_min = min(value, *raw_data_min);
  1971. *raw_data_max = max(value, *raw_data_max);
  1972. if (intp)
  1973. put_unaligned_le32(value, &intp[count]);
  1974. sum += value;
  1975. count++;
  1976. }
  1977. }
  1978. if (count >= raw_data_max_num)
  1979. break;
  1980. read_elements = (sizeof(resp_data) -
  1981. GEN5_RESP_DATA_STRUCTURE_OFFSET) / data_size;
  1982. read_len = read_elements * data_size;
  1983. } while (true);
  1984. *raw_data_ave = count ? (sum / count) : 0;
  1985. return 0;
  1986. }
  1987. static ssize_t cyapa_gen5_show_baseline(struct device *dev,
  1988. struct device_attribute *attr, char *buf)
  1989. {
  1990. struct cyapa *cyapa = dev_get_drvdata(dev);
  1991. int gidac_mutual_max, gidac_mutual_min, gidac_mutual_ave;
  1992. int lidac_mutual_max, lidac_mutual_min, lidac_mutual_ave;
  1993. int gidac_self_rx, gidac_self_tx;
  1994. int lidac_self_max, lidac_self_min, lidac_self_ave;
  1995. int raw_cap_mutual_max, raw_cap_mutual_min, raw_cap_mutual_ave;
  1996. int raw_cap_self_max, raw_cap_self_min, raw_cap_self_ave;
  1997. int mutual_diffdata_max, mutual_diffdata_min, mutual_diffdata_ave;
  1998. int self_diffdata_max, self_diffdata_min, self_diffdata_ave;
  1999. int mutual_baseline_max, mutual_baseline_min, mutual_baseline_ave;
  2000. int self_baseline_max, self_baseline_min, self_baseline_ave;
  2001. int error, resume_error;
  2002. int size;
  2003. if (!cyapa_is_pip_app_mode(cyapa))
  2004. return -EBUSY;
  2005. /* 1. Suspend Scanning*/
  2006. error = cyapa_pip_suspend_scanning(cyapa);
  2007. if (error)
  2008. return error;
  2009. /* 2. Read global and local mutual IDAC data. */
  2010. gidac_self_rx = gidac_self_tx = 0;
  2011. error = cyapa_gen5_read_mutual_idac_data(cyapa,
  2012. &gidac_mutual_max, &gidac_mutual_min,
  2013. &gidac_mutual_ave, &lidac_mutual_max,
  2014. &lidac_mutual_min, &lidac_mutual_ave);
  2015. if (error)
  2016. goto resume_scanning;
  2017. /* 3. Read global and local self IDAC data. */
  2018. error = cyapa_gen5_read_self_idac_data(cyapa,
  2019. &gidac_self_rx, &gidac_self_tx,
  2020. &lidac_self_max, &lidac_self_min,
  2021. &lidac_self_ave);
  2022. if (error)
  2023. goto resume_scanning;
  2024. /* 4. Execute panel scan. It must be executed before read data. */
  2025. error = cyapa_gen5_execute_panel_scan(cyapa);
  2026. if (error)
  2027. goto resume_scanning;
  2028. /* 5. Retrieve panel scan, mutual cap raw data. */
  2029. error = cyapa_gen5_read_panel_scan_raw_data(cyapa,
  2030. GEN5_CMD_RETRIEVE_PANEL_SCAN,
  2031. GEN5_PANEL_SCAN_MUTUAL_RAW_DATA,
  2032. cyapa->electrodes_x * cyapa->electrodes_y,
  2033. &raw_cap_mutual_max, &raw_cap_mutual_min,
  2034. &raw_cap_mutual_ave,
  2035. NULL);
  2036. if (error)
  2037. goto resume_scanning;
  2038. /* 6. Retrieve panel scan, self cap raw data. */
  2039. error = cyapa_gen5_read_panel_scan_raw_data(cyapa,
  2040. GEN5_CMD_RETRIEVE_PANEL_SCAN,
  2041. GEN5_PANEL_SCAN_SELF_RAW_DATA,
  2042. cyapa->electrodes_x + cyapa->electrodes_y,
  2043. &raw_cap_self_max, &raw_cap_self_min,
  2044. &raw_cap_self_ave,
  2045. NULL);
  2046. if (error)
  2047. goto resume_scanning;
  2048. /* 7. Retrieve panel scan, mutual cap diffcount raw data. */
  2049. error = cyapa_gen5_read_panel_scan_raw_data(cyapa,
  2050. GEN5_CMD_RETRIEVE_PANEL_SCAN,
  2051. GEN5_PANEL_SCAN_MUTUAL_DIFFCOUNT,
  2052. cyapa->electrodes_x * cyapa->electrodes_y,
  2053. &mutual_diffdata_max, &mutual_diffdata_min,
  2054. &mutual_diffdata_ave,
  2055. NULL);
  2056. if (error)
  2057. goto resume_scanning;
  2058. /* 8. Retrieve panel scan, self cap diffcount raw data. */
  2059. error = cyapa_gen5_read_panel_scan_raw_data(cyapa,
  2060. GEN5_CMD_RETRIEVE_PANEL_SCAN,
  2061. GEN5_PANEL_SCAN_SELF_DIFFCOUNT,
  2062. cyapa->electrodes_x + cyapa->electrodes_y,
  2063. &self_diffdata_max, &self_diffdata_min,
  2064. &self_diffdata_ave,
  2065. NULL);
  2066. if (error)
  2067. goto resume_scanning;
  2068. /* 9. Retrieve panel scan, mutual cap baseline raw data. */
  2069. error = cyapa_gen5_read_panel_scan_raw_data(cyapa,
  2070. GEN5_CMD_RETRIEVE_PANEL_SCAN,
  2071. GEN5_PANEL_SCAN_MUTUAL_BASELINE,
  2072. cyapa->electrodes_x * cyapa->electrodes_y,
  2073. &mutual_baseline_max, &mutual_baseline_min,
  2074. &mutual_baseline_ave,
  2075. NULL);
  2076. if (error)
  2077. goto resume_scanning;
  2078. /* 10. Retrieve panel scan, self cap baseline raw data. */
  2079. error = cyapa_gen5_read_panel_scan_raw_data(cyapa,
  2080. GEN5_CMD_RETRIEVE_PANEL_SCAN,
  2081. GEN5_PANEL_SCAN_SELF_BASELINE,
  2082. cyapa->electrodes_x + cyapa->electrodes_y,
  2083. &self_baseline_max, &self_baseline_min,
  2084. &self_baseline_ave,
  2085. NULL);
  2086. if (error)
  2087. goto resume_scanning;
  2088. resume_scanning:
  2089. /* 11. Resume Scanning*/
  2090. resume_error = cyapa_pip_resume_scanning(cyapa);
  2091. if (resume_error || error)
  2092. return resume_error ? resume_error : error;
  2093. /* 12. Output data strings */
  2094. size = scnprintf(buf, PAGE_SIZE, "%d %d %d %d %d %d %d %d %d %d %d ",
  2095. gidac_mutual_min, gidac_mutual_max, gidac_mutual_ave,
  2096. lidac_mutual_min, lidac_mutual_max, lidac_mutual_ave,
  2097. gidac_self_rx, gidac_self_tx,
  2098. lidac_self_min, lidac_self_max, lidac_self_ave);
  2099. size += scnprintf(buf + size, PAGE_SIZE - size,
  2100. "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
  2101. raw_cap_mutual_min, raw_cap_mutual_max, raw_cap_mutual_ave,
  2102. raw_cap_self_min, raw_cap_self_max, raw_cap_self_ave,
  2103. mutual_diffdata_min, mutual_diffdata_max, mutual_diffdata_ave,
  2104. self_diffdata_min, self_diffdata_max, self_diffdata_ave,
  2105. mutual_baseline_min, mutual_baseline_max, mutual_baseline_ave,
  2106. self_baseline_min, self_baseline_max, self_baseline_ave);
  2107. return size;
  2108. }
  2109. bool cyapa_pip_sort_system_info_data(struct cyapa *cyapa,
  2110. u8 *buf, int len)
  2111. {
  2112. /* Check the report id and command code */
  2113. if (VALID_CMD_RESP_HEADER(buf, 0x02))
  2114. return true;
  2115. return false;
  2116. }
  2117. static int cyapa_gen5_bl_query_data(struct cyapa *cyapa)
  2118. {
  2119. u8 resp_data[PIP_BL_APP_INFO_RESP_LENGTH];
  2120. int resp_len;
  2121. int error;
  2122. resp_len = sizeof(resp_data);
  2123. error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
  2124. pip_bl_read_app_info, PIP_BL_READ_APP_INFO_CMD_LENGTH,
  2125. resp_data, &resp_len,
  2126. 500, cyapa_sort_tsg_pip_bl_resp_data, false);
  2127. if (error || resp_len < PIP_BL_APP_INFO_RESP_LENGTH ||
  2128. !PIP_CMD_COMPLETE_SUCCESS(resp_data))
  2129. return error ? error : -EIO;
  2130. memcpy(&cyapa->product_id[0], &resp_data[8], 5);
  2131. cyapa->product_id[5] = '-';
  2132. memcpy(&cyapa->product_id[6], &resp_data[13], 6);
  2133. cyapa->product_id[12] = '-';
  2134. memcpy(&cyapa->product_id[13], &resp_data[19], 2);
  2135. cyapa->product_id[15] = '\0';
  2136. cyapa->fw_maj_ver = resp_data[22];
  2137. cyapa->fw_min_ver = resp_data[23];
  2138. cyapa->platform_ver = (resp_data[26] >> PIP_BL_PLATFORM_VER_SHIFT) &
  2139. PIP_BL_PLATFORM_VER_MASK;
  2140. return 0;
  2141. }
  2142. static int cyapa_gen5_get_query_data(struct cyapa *cyapa)
  2143. {
  2144. u8 resp_data[PIP_READ_SYS_INFO_RESP_LENGTH];
  2145. int resp_len;
  2146. u16 product_family;
  2147. int error;
  2148. resp_len = sizeof(resp_data);
  2149. error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
  2150. pip_read_sys_info, PIP_READ_SYS_INFO_CMD_LENGTH,
  2151. resp_data, &resp_len,
  2152. 2000, cyapa_pip_sort_system_info_data, false);
  2153. if (error || resp_len < sizeof(resp_data))
  2154. return error ? error : -EIO;
  2155. product_family = get_unaligned_le16(&resp_data[7]);
  2156. if ((product_family & PIP_PRODUCT_FAMILY_MASK) !=
  2157. PIP_PRODUCT_FAMILY_TRACKPAD)
  2158. return -EINVAL;
  2159. cyapa->platform_ver = (resp_data[49] >> PIP_BL_PLATFORM_VER_SHIFT) &
  2160. PIP_BL_PLATFORM_VER_MASK;
  2161. if (cyapa->gen == CYAPA_GEN5 && cyapa->platform_ver < 2) {
  2162. /* Gen5 firmware that does not support proximity. */
  2163. cyapa->fw_maj_ver = resp_data[15];
  2164. cyapa->fw_min_ver = resp_data[16];
  2165. } else {
  2166. cyapa->fw_maj_ver = resp_data[9];
  2167. cyapa->fw_min_ver = resp_data[10];
  2168. }
  2169. cyapa->electrodes_x = resp_data[52];
  2170. cyapa->electrodes_y = resp_data[53];
  2171. cyapa->physical_size_x = get_unaligned_le16(&resp_data[54]) / 100;
  2172. cyapa->physical_size_y = get_unaligned_le16(&resp_data[56]) / 100;
  2173. cyapa->max_abs_x = get_unaligned_le16(&resp_data[58]);
  2174. cyapa->max_abs_y = get_unaligned_le16(&resp_data[60]);
  2175. cyapa->max_z = get_unaligned_le16(&resp_data[62]);
  2176. cyapa->x_origin = resp_data[64] & 0x01;
  2177. cyapa->y_origin = resp_data[65] & 0x01;
  2178. cyapa->btn_capability = (resp_data[70] << 3) & CAPABILITY_BTN_MASK;
  2179. memcpy(&cyapa->product_id[0], &resp_data[33], 5);
  2180. cyapa->product_id[5] = '-';
  2181. memcpy(&cyapa->product_id[6], &resp_data[38], 6);
  2182. cyapa->product_id[12] = '-';
  2183. memcpy(&cyapa->product_id[13], &resp_data[44], 2);
  2184. cyapa->product_id[15] = '\0';
  2185. if (!cyapa->electrodes_x || !cyapa->electrodes_y ||
  2186. !cyapa->physical_size_x || !cyapa->physical_size_y ||
  2187. !cyapa->max_abs_x || !cyapa->max_abs_y || !cyapa->max_z)
  2188. return -EINVAL;
  2189. return 0;
  2190. }
  2191. static int cyapa_gen5_do_operational_check(struct cyapa *cyapa)
  2192. {
  2193. struct device *dev = &cyapa->client->dev;
  2194. int error;
  2195. if (cyapa->gen != CYAPA_GEN5)
  2196. return -ENODEV;
  2197. switch (cyapa->state) {
  2198. case CYAPA_STATE_GEN5_BL:
  2199. error = cyapa_pip_bl_exit(cyapa);
  2200. if (error) {
  2201. /* Try to update trackpad product information. */
  2202. cyapa_gen5_bl_query_data(cyapa);
  2203. goto out;
  2204. }
  2205. cyapa->state = CYAPA_STATE_GEN5_APP;
  2206. fallthrough;
  2207. case CYAPA_STATE_GEN5_APP:
  2208. /*
  2209. * If trackpad device in deep sleep mode,
  2210. * the app command will fail.
  2211. * So always try to reset trackpad device to full active when
  2212. * the device state is required.
  2213. */
  2214. error = cyapa_gen5_set_power_mode(cyapa,
  2215. PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_ACTIVE);
  2216. if (error)
  2217. dev_warn(dev, "%s: failed to set power active mode.\n",
  2218. __func__);
  2219. /* By default, the trackpad proximity function is enabled. */
  2220. if (cyapa->platform_ver >= 2) {
  2221. error = cyapa_pip_set_proximity(cyapa, true);
  2222. if (error)
  2223. dev_warn(dev,
  2224. "%s: failed to enable proximity.\n",
  2225. __func__);
  2226. }
  2227. /* Get trackpad product information. */
  2228. error = cyapa_gen5_get_query_data(cyapa);
  2229. if (error)
  2230. goto out;
  2231. /* Only support product ID starting with CYTRA */
  2232. if (memcmp(cyapa->product_id, product_id,
  2233. strlen(product_id)) != 0) {
  2234. dev_err(dev, "%s: unknown product ID (%s)\n",
  2235. __func__, cyapa->product_id);
  2236. error = -EINVAL;
  2237. }
  2238. break;
  2239. default:
  2240. error = -EINVAL;
  2241. }
  2242. out:
  2243. return error;
  2244. }
  2245. /*
  2246. * Return false, do not continue process
  2247. * Return true, continue process.
  2248. */
  2249. bool cyapa_pip_irq_cmd_handler(struct cyapa *cyapa)
  2250. {
  2251. struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
  2252. int length;
  2253. if (atomic_read(&pip->cmd_issued)) {
  2254. /* Polling command response data. */
  2255. if (pip->is_irq_mode == false)
  2256. return false;
  2257. /*
  2258. * Read out all none command response data.
  2259. * these output data may caused by user put finger on
  2260. * trackpad when host waiting the command response.
  2261. */
  2262. cyapa_i2c_pip_read(cyapa, pip->irq_cmd_buf,
  2263. PIP_RESP_LENGTH_SIZE);
  2264. length = get_unaligned_le16(pip->irq_cmd_buf);
  2265. length = (length <= PIP_RESP_LENGTH_SIZE) ?
  2266. PIP_RESP_LENGTH_SIZE : length;
  2267. if (length > PIP_RESP_LENGTH_SIZE)
  2268. cyapa_i2c_pip_read(cyapa,
  2269. pip->irq_cmd_buf, length);
  2270. if (!(pip->resp_sort_func &&
  2271. pip->resp_sort_func(cyapa,
  2272. pip->irq_cmd_buf, length))) {
  2273. /*
  2274. * Cover the Gen5 V1 firmware issue.
  2275. * The issue is no interrupt would be asserted from
  2276. * trackpad device to host for the command response
  2277. * ready event. Because when there was a finger touch
  2278. * on trackpad device, and the firmware output queue
  2279. * won't be empty (always with touch report data), so
  2280. * the interrupt signal won't be asserted again until
  2281. * the output queue was previous emptied.
  2282. * This issue would happen in the scenario that
  2283. * user always has his/her fingers touched on the
  2284. * trackpad device during system booting/rebooting.
  2285. */
  2286. length = 0;
  2287. if (pip->resp_len)
  2288. length = *pip->resp_len;
  2289. cyapa_empty_pip_output_data(cyapa,
  2290. pip->resp_data,
  2291. &length,
  2292. pip->resp_sort_func);
  2293. if (pip->resp_len && length != 0) {
  2294. *pip->resp_len = length;
  2295. atomic_dec(&pip->cmd_issued);
  2296. complete(&pip->cmd_ready);
  2297. }
  2298. return false;
  2299. }
  2300. if (pip->resp_data && pip->resp_len) {
  2301. *pip->resp_len = (*pip->resp_len < length) ?
  2302. *pip->resp_len : length;
  2303. memcpy(pip->resp_data, pip->irq_cmd_buf,
  2304. *pip->resp_len);
  2305. }
  2306. atomic_dec(&pip->cmd_issued);
  2307. complete(&pip->cmd_ready);
  2308. return false;
  2309. }
  2310. return true;
  2311. }
  2312. static void cyapa_pip_report_buttons(struct cyapa *cyapa,
  2313. const struct cyapa_pip_report_data *report_data)
  2314. {
  2315. struct input_dev *input = cyapa->input;
  2316. u8 buttons = report_data->report_head[PIP_BUTTONS_OFFSET];
  2317. buttons = (buttons << CAPABILITY_BTN_SHIFT) & CAPABILITY_BTN_MASK;
  2318. if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK) {
  2319. input_report_key(input, BTN_LEFT,
  2320. !!(buttons & CAPABILITY_LEFT_BTN_MASK));
  2321. }
  2322. if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK) {
  2323. input_report_key(input, BTN_MIDDLE,
  2324. !!(buttons & CAPABILITY_MIDDLE_BTN_MASK));
  2325. }
  2326. if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK) {
  2327. input_report_key(input, BTN_RIGHT,
  2328. !!(buttons & CAPABILITY_RIGHT_BTN_MASK));
  2329. }
  2330. input_sync(input);
  2331. }
  2332. static void cyapa_pip_report_proximity(struct cyapa *cyapa,
  2333. const struct cyapa_pip_report_data *report_data)
  2334. {
  2335. struct input_dev *input = cyapa->input;
  2336. u8 distance = report_data->report_head[PIP_PROXIMITY_DISTANCE_OFFSET] &
  2337. PIP_PROXIMITY_DISTANCE_MASK;
  2338. input_report_abs(input, ABS_DISTANCE, distance);
  2339. input_sync(input);
  2340. }
  2341. static void cyapa_pip_report_slot_data(struct cyapa *cyapa,
  2342. const struct cyapa_pip_touch_record *touch)
  2343. {
  2344. struct input_dev *input = cyapa->input;
  2345. u8 event_id = PIP_GET_EVENT_ID(touch->touch_tip_event_id);
  2346. int slot = PIP_GET_TOUCH_ID(touch->touch_tip_event_id);
  2347. int x, y;
  2348. if (event_id == RECORD_EVENT_LIFTOFF)
  2349. return;
  2350. input_mt_slot(input, slot);
  2351. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  2352. x = (touch->x_hi << 8) | touch->x_lo;
  2353. if (cyapa->x_origin)
  2354. x = cyapa->max_abs_x - x;
  2355. y = (touch->y_hi << 8) | touch->y_lo;
  2356. if (cyapa->y_origin)
  2357. y = cyapa->max_abs_y - y;
  2358. input_report_abs(input, ABS_MT_POSITION_X, x);
  2359. input_report_abs(input, ABS_MT_POSITION_Y, y);
  2360. input_report_abs(input, ABS_DISTANCE, 0);
  2361. input_report_abs(input, ABS_MT_PRESSURE,
  2362. touch->z);
  2363. input_report_abs(input, ABS_MT_TOUCH_MAJOR,
  2364. touch->major_axis_len);
  2365. input_report_abs(input, ABS_MT_TOUCH_MINOR,
  2366. touch->minor_axis_len);
  2367. input_report_abs(input, ABS_MT_WIDTH_MAJOR,
  2368. touch->major_tool_len);
  2369. input_report_abs(input, ABS_MT_WIDTH_MINOR,
  2370. touch->minor_tool_len);
  2371. input_report_abs(input, ABS_MT_ORIENTATION,
  2372. touch->orientation);
  2373. }
  2374. static void cyapa_pip_report_touches(struct cyapa *cyapa,
  2375. const struct cyapa_pip_report_data *report_data)
  2376. {
  2377. struct input_dev *input = cyapa->input;
  2378. unsigned int touch_num;
  2379. int i;
  2380. touch_num = report_data->report_head[PIP_NUMBER_OF_TOUCH_OFFSET] &
  2381. PIP_NUMBER_OF_TOUCH_MASK;
  2382. for (i = 0; i < touch_num; i++)
  2383. cyapa_pip_report_slot_data(cyapa,
  2384. &report_data->touch_records[i]);
  2385. input_mt_sync_frame(input);
  2386. input_sync(input);
  2387. }
  2388. int cyapa_pip_irq_handler(struct cyapa *cyapa)
  2389. {
  2390. struct device *dev = &cyapa->client->dev;
  2391. struct cyapa_pip_report_data report_data;
  2392. unsigned int report_len;
  2393. int ret;
  2394. if (!cyapa_is_pip_app_mode(cyapa)) {
  2395. dev_err(dev, "invalid device state, gen=%d, state=0x%02x\n",
  2396. cyapa->gen, cyapa->state);
  2397. return -EINVAL;
  2398. }
  2399. ret = cyapa_i2c_pip_read(cyapa, (u8 *)&report_data,
  2400. PIP_RESP_LENGTH_SIZE);
  2401. if (ret != PIP_RESP_LENGTH_SIZE) {
  2402. dev_err(dev, "failed to read length bytes, (%d)\n", ret);
  2403. return -EINVAL;
  2404. }
  2405. report_len = get_unaligned_le16(
  2406. &report_data.report_head[PIP_RESP_LENGTH_OFFSET]);
  2407. if (report_len < PIP_RESP_LENGTH_SIZE) {
  2408. /* Invalid length or internal reset happened. */
  2409. dev_err(dev, "invalid report_len=%d. bytes: %02x %02x\n",
  2410. report_len, report_data.report_head[0],
  2411. report_data.report_head[1]);
  2412. return -EINVAL;
  2413. }
  2414. /* Idle, no data for report. */
  2415. if (report_len == PIP_RESP_LENGTH_SIZE)
  2416. return 0;
  2417. ret = cyapa_i2c_pip_read(cyapa, (u8 *)&report_data, report_len);
  2418. if (ret != report_len) {
  2419. dev_err(dev, "failed to read %d bytes report data, (%d)\n",
  2420. report_len, ret);
  2421. return -EINVAL;
  2422. }
  2423. return cyapa_pip_event_process(cyapa, &report_data);
  2424. }
  2425. static int cyapa_pip_event_process(struct cyapa *cyapa,
  2426. struct cyapa_pip_report_data *report_data)
  2427. {
  2428. struct device *dev = &cyapa->client->dev;
  2429. unsigned int report_len;
  2430. u8 report_id;
  2431. report_len = get_unaligned_le16(
  2432. &report_data->report_head[PIP_RESP_LENGTH_OFFSET]);
  2433. /* Idle, no data for report. */
  2434. if (report_len == PIP_RESP_LENGTH_SIZE)
  2435. return 0;
  2436. report_id = report_data->report_head[PIP_RESP_REPORT_ID_OFFSET];
  2437. if (report_id == PIP_WAKEUP_EVENT_REPORT_ID &&
  2438. report_len == PIP_WAKEUP_EVENT_SIZE) {
  2439. /*
  2440. * Device wake event from deep sleep mode for touch.
  2441. * This interrupt event is used to wake system up.
  2442. *
  2443. * Note:
  2444. * It will introduce about 20~40 ms additional delay
  2445. * time in receiving for first valid touch report data.
  2446. * The time is used to execute device runtime resume
  2447. * process.
  2448. */
  2449. pm_runtime_get_sync(dev);
  2450. pm_runtime_mark_last_busy(dev);
  2451. pm_runtime_put_sync_autosuspend(dev);
  2452. return 0;
  2453. } else if (report_id != PIP_TOUCH_REPORT_ID &&
  2454. report_id != PIP_BTN_REPORT_ID &&
  2455. report_id != GEN5_OLD_PUSH_BTN_REPORT_ID &&
  2456. report_id != PIP_PUSH_BTN_REPORT_ID &&
  2457. report_id != PIP_PROXIMITY_REPORT_ID) {
  2458. /* Running in BL mode or unknown response data read. */
  2459. dev_err(dev, "invalid report_id=0x%02x\n", report_id);
  2460. return -EINVAL;
  2461. }
  2462. if (report_id == PIP_TOUCH_REPORT_ID &&
  2463. (report_len < PIP_TOUCH_REPORT_HEAD_SIZE ||
  2464. report_len > PIP_TOUCH_REPORT_MAX_SIZE)) {
  2465. /* Invalid report data length for finger packet. */
  2466. dev_err(dev, "invalid touch packet length=%d\n", report_len);
  2467. return 0;
  2468. }
  2469. if ((report_id == PIP_BTN_REPORT_ID ||
  2470. report_id == GEN5_OLD_PUSH_BTN_REPORT_ID ||
  2471. report_id == PIP_PUSH_BTN_REPORT_ID) &&
  2472. (report_len < PIP_BTN_REPORT_HEAD_SIZE ||
  2473. report_len > PIP_BTN_REPORT_MAX_SIZE)) {
  2474. /* Invalid report data length of button packet. */
  2475. dev_err(dev, "invalid button packet length=%d\n", report_len);
  2476. return 0;
  2477. }
  2478. if (report_id == PIP_PROXIMITY_REPORT_ID &&
  2479. report_len != PIP_PROXIMITY_REPORT_SIZE) {
  2480. /* Invalid report data length of proximity packet. */
  2481. dev_err(dev, "invalid proximity data, length=%d\n", report_len);
  2482. return 0;
  2483. }
  2484. if (report_id == PIP_TOUCH_REPORT_ID)
  2485. cyapa_pip_report_touches(cyapa, report_data);
  2486. else if (report_id == PIP_PROXIMITY_REPORT_ID)
  2487. cyapa_pip_report_proximity(cyapa, report_data);
  2488. else
  2489. cyapa_pip_report_buttons(cyapa, report_data);
  2490. return 0;
  2491. }
  2492. int cyapa_pip_bl_activate(struct cyapa *cyapa) { return 0; }
  2493. int cyapa_pip_bl_deactivate(struct cyapa *cyapa) { return 0; }
  2494. const struct cyapa_dev_ops cyapa_gen5_ops = {
  2495. .check_fw = cyapa_pip_check_fw,
  2496. .bl_enter = cyapa_pip_bl_enter,
  2497. .bl_initiate = cyapa_pip_bl_initiate,
  2498. .update_fw = cyapa_pip_do_fw_update,
  2499. .bl_activate = cyapa_pip_bl_activate,
  2500. .bl_deactivate = cyapa_pip_bl_deactivate,
  2501. .show_baseline = cyapa_gen5_show_baseline,
  2502. .calibrate_store = cyapa_pip_do_calibrate,
  2503. .initialize = cyapa_pip_cmd_state_initialize,
  2504. .state_parse = cyapa_gen5_state_parse,
  2505. .operational_check = cyapa_gen5_do_operational_check,
  2506. .irq_handler = cyapa_pip_irq_handler,
  2507. .irq_cmd_handler = cyapa_pip_irq_cmd_handler,
  2508. .sort_empty_output_data = cyapa_empty_pip_output_data,
  2509. .set_power_mode = cyapa_gen5_set_power_mode,
  2510. .set_proximity = cyapa_pip_set_proximity,
  2511. };