cyapa_gen5.c 81 KB

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