cyapa_gen5.c 83 KB

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