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