atmel_mxt_ts.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Atmel maXTouch Touchscreen driver
  4. *
  5. * Copyright (C) 2010 Samsung Electronics Co.Ltd
  6. * Copyright (C) 2011-2014 Atmel Corporation
  7. * Copyright (C) 2012 Google, Inc.
  8. * Copyright (C) 2016 Zodiac Inflight Innovations
  9. *
  10. * Author: Joonyoung Shim <[email protected]>
  11. */
  12. #include <linux/acpi.h>
  13. #include <linux/dmi.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/completion.h>
  17. #include <linux/delay.h>
  18. #include <linux/firmware.h>
  19. #include <linux/i2c.h>
  20. #include <linux/input.h>
  21. #include <linux/input/mt.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/irq.h>
  25. #include <linux/of.h>
  26. #include <linux/soc/qcom/panel_event_notifier.h>
  27. #include <linux/property.h>
  28. #include <linux/slab.h>
  29. #include <linux/gpio/consumer.h>
  30. #include <linux/property.h>
  31. #include <asm/unaligned.h>
  32. #include <media/v4l2-device.h>
  33. #include <media/v4l2-ioctl.h>
  34. #include <media/videobuf2-v4l2.h>
  35. #include <media/videobuf2-vmalloc.h>
  36. #include <linux/pinctrl/consumer.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/gpio.h>
  39. #ifdef CONFIG_DRM
  40. #include <drm/drm_panel.h>
  41. #endif
  42. /* Firmware files */
  43. #define MXT_FW_NAME "maxtouch.fw"
  44. #define MXT_CFG_NAME "maxtouch.cfg"
  45. #define MXT_CFG_MAGIC "OBP_RAW V1"
  46. /* Registers */
  47. #define MXT_OBJECT_START 0x07
  48. #define MXT_OBJECT_SIZE 6
  49. #define MXT_INFO_CHECKSUM_SIZE 3
  50. #define MXT_MAX_BLOCK_WRITE 256
  51. /* Object types */
  52. #define MXT_DEBUG_DIAGNOSTIC_T37 37
  53. #define MXT_GEN_MESSAGE_T5 5
  54. #define MXT_GEN_COMMAND_T6 6
  55. #define MXT_GEN_POWER_T7 7
  56. #define MXT_GEN_ACQUIRE_T8 8
  57. #define MXT_GEN_DATASOURCE_T53 53
  58. #define MXT_TOUCH_MULTI_T9 9
  59. #define MXT_TOUCH_KEYARRAY_T15 15
  60. #define MXT_TOUCH_PROXIMITY_T23 23
  61. #define MXT_TOUCH_PROXKEY_T52 52
  62. #define MXT_PROCI_GRIPFACE_T20 20
  63. #define MXT_PROCG_NOISE_T22 22
  64. #define MXT_PROCI_ONETOUCH_T24 24
  65. #define MXT_PROCI_TWOTOUCH_T27 27
  66. #define MXT_PROCI_GRIP_T40 40
  67. #define MXT_PROCI_PALM_T41 41
  68. #define MXT_PROCI_TOUCHSUPPRESSION_T42 42
  69. #define MXT_PROCI_STYLUS_T47 47
  70. #define MXT_PROCG_NOISESUPPRESSION_T48 48
  71. #define MXT_SPT_COMMSCONFIG_T18 18
  72. #define MXT_SPT_GPIOPWM_T19 19
  73. #define MXT_SPT_SELFTEST_T25 25
  74. #define MXT_SPT_CTECONFIG_T28 28
  75. #define MXT_SPT_USERDATA_T38 38
  76. #define MXT_SPT_DIGITIZER_T43 43
  77. #define MXT_SPT_MESSAGECOUNT_T44 44
  78. #define MXT_SPT_CTECONFIG_T46 46
  79. #define MXT_SPT_DYNAMICCONFIGURATIONCONTAINER_T71 71
  80. #define MXT_TOUCH_MULTITOUCHSCREEN_T100 100
  81. /* Delay times */
  82. #define MXT_BACKUP_TIME 50 /* msec */
  83. #define MXT_RESET_TIME 200 /* msec */
  84. #define MXT_RESET_TIMEOUT 3000 /* msec */
  85. #define MXT_CRC_TIMEOUT 1000 /* msec */
  86. #define MXT_FW_RESET_TIME 3000 /* msec */
  87. #define MXT_FW_CHG_TIMEOUT 300 /* msec */
  88. #define MXT_WAKEUP_TIME 25 /* msec */
  89. #define MXT_REGULATOR_DELAY 150 /* msec */
  90. #define MXT_POWERON_DELAY 150 /* msec */
  91. /* recommended voltage specifications */
  92. #define MXT_VDD_VTG_MIN_UV 1800000
  93. #define MXT_VDD_VTG_MAX_UV 1800000
  94. #define MXT_AVDD_VTG_MIN_UV 3000000
  95. #define MXT_AVDD_VTG_MAX_UV 3300000
  96. #define MXT_XVDD_VTG_MIN_UV 2700000
  97. #define MXT_XVDD_VTG_MAX_UV 10000000
  98. /* recommended load specifications */
  99. #define MXT_ENABLE_LOAD 10000
  100. #define MXT_DISABLE_LOAD 0
  101. /* MXT_GEN_MESSAGE_T5 object */
  102. #define MXT_RPTID_NOMSG 0xff
  103. /* MXT_GEN_COMMAND_T6 field */
  104. #define MXT_COMMAND_RESET 0
  105. #define MXT_COMMAND_BACKUPNV 1
  106. #define MXT_COMMAND_CALIBRATE 2
  107. #define MXT_COMMAND_REPORTALL 3
  108. #define MXT_COMMAND_DIAGNOSTIC 5
  109. /* Define for T6 status byte */
  110. #define MXT_T6_STATUS_RESET BIT(7)
  111. #define MXT_T6_STATUS_OFL BIT(6)
  112. #define MXT_T6_STATUS_SIGERR BIT(5)
  113. #define MXT_T6_STATUS_CAL BIT(4)
  114. #define MXT_T6_STATUS_CFGERR BIT(3)
  115. #define MXT_T6_STATUS_COMSERR BIT(2)
  116. /* MXT_GEN_POWER_T7 field */
  117. struct t7_config {
  118. u8 idle;
  119. u8 active;
  120. } __packed;
  121. #define MXT_POWER_CFG_RUN 0
  122. #define MXT_POWER_CFG_DEEPSLEEP 1
  123. /* MXT_TOUCH_MULTI_T9 field */
  124. #define MXT_T9_CTRL 0
  125. #define MXT_T9_XSIZE 3
  126. #define MXT_T9_YSIZE 4
  127. #define MXT_T9_ORIENT 9
  128. #define MXT_T9_RANGE 18
  129. /* MXT_TOUCH_MULTI_T9 status */
  130. #define MXT_T9_UNGRIP BIT(0)
  131. #define MXT_T9_SUPPRESS BIT(1)
  132. #define MXT_T9_AMP BIT(2)
  133. #define MXT_T9_VECTOR BIT(3)
  134. #define MXT_T9_MOVE BIT(4)
  135. #define MXT_T9_RELEASE BIT(5)
  136. #define MXT_T9_PRESS BIT(6)
  137. #define MXT_T9_DETECT BIT(7)
  138. struct t9_range {
  139. __le16 x;
  140. __le16 y;
  141. } __packed;
  142. /* MXT_TOUCH_MULTI_T9 orient */
  143. #define MXT_T9_ORIENT_SWITCH BIT(0)
  144. #define MXT_T9_ORIENT_INVERTX BIT(1)
  145. #define MXT_T9_ORIENT_INVERTY BIT(2)
  146. /* MXT_SPT_COMMSCONFIG_T18 */
  147. #define MXT_COMMS_CTRL 0
  148. #define MXT_COMMS_CMD 1
  149. #define MXT_COMMS_RETRIGEN BIT(6)
  150. /* MXT_DEBUG_DIAGNOSTIC_T37 */
  151. #define MXT_DIAGNOSTIC_PAGEUP 0x01
  152. #define MXT_DIAGNOSTIC_DELTAS 0x10
  153. #define MXT_DIAGNOSTIC_REFS 0x11
  154. #define MXT_DIAGNOSTIC_SIZE 128
  155. #define MXT_FAMILY_1386 160
  156. #define MXT1386_COLUMNS 3
  157. #define MXT1386_PAGES_PER_COLUMN 8
  158. struct t37_debug {
  159. #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
  160. u8 mode;
  161. u8 page;
  162. u8 data[MXT_DIAGNOSTIC_SIZE];
  163. #endif
  164. };
  165. /* Define for MXT_GEN_COMMAND_T6 */
  166. #define MXT_BOOT_VALUE 0xa5
  167. #define MXT_RESET_VALUE 0x01
  168. #define MXT_BACKUP_VALUE 0x55
  169. /* T100 Multiple Touch Touchscreen */
  170. #define MXT_T100_CTRL 0
  171. #define MXT_T100_CFG1 1
  172. #define MXT_T100_TCHAUX 3
  173. #define MXT_T100_XSIZE 9
  174. #define MXT_T100_XRANGE 13
  175. #define MXT_T100_YSIZE 20
  176. #define MXT_T100_YRANGE 24
  177. #define MXT_T100_CFG_SWITCHXY BIT(5)
  178. #define MXT_T100_CFG_INVERTY BIT(6)
  179. #define MXT_T100_CFG_INVERTX BIT(7)
  180. #define MXT_T100_TCHAUX_VECT BIT(0)
  181. #define MXT_T100_TCHAUX_AMPL BIT(1)
  182. #define MXT_T100_TCHAUX_AREA BIT(2)
  183. #define MXT_T100_DETECT BIT(7)
  184. #define MXT_T100_TYPE_MASK 0x70
  185. enum t100_type {
  186. MXT_T100_TYPE_FINGER = 1,
  187. MXT_T100_TYPE_PASSIVE_STYLUS = 2,
  188. MXT_T100_TYPE_HOVERING_FINGER = 4,
  189. MXT_T100_TYPE_GLOVE = 5,
  190. MXT_T100_TYPE_LARGE_TOUCH = 6,
  191. };
  192. #define MXT_DISTANCE_ACTIVE_TOUCH 0
  193. #define MXT_DISTANCE_HOVERING 1
  194. #define MXT_TOUCH_MAJOR_DEFAULT 1
  195. #define MXT_PRESSURE_DEFAULT 1
  196. /* Delay times */
  197. #define MXT_BACKUP_TIME 50 /* msec */
  198. #define MXT_RESET_GPIO_TIME 20 /* msec */
  199. #define MXT_RESET_INVALID_CHG 100 /* msec */
  200. #define MXT_RESET_TIME 200 /* msec */
  201. #define MXT_RESET_TIMEOUT 3000 /* msec */
  202. #define MXT_CRC_TIMEOUT 1000 /* msec */
  203. #define MXT_FW_RESET_TIME 3000 /* msec */
  204. #define MXT_FW_CHG_TIMEOUT 300 /* msec */
  205. /* Command to unlock bootloader */
  206. #define MXT_UNLOCK_CMD_MSB 0xaa
  207. #define MXT_UNLOCK_CMD_LSB 0xdc
  208. /* Bootloader mode status */
  209. #define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
  210. #define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
  211. #define MXT_FRAME_CRC_CHECK 0x02
  212. #define MXT_FRAME_CRC_FAIL 0x03
  213. #define MXT_FRAME_CRC_PASS 0x04
  214. #define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
  215. #define MXT_BOOT_STATUS_MASK 0x3f
  216. #define MXT_BOOT_EXTENDED_ID BIT(5)
  217. #define MXT_BOOT_ID_MASK 0x1f
  218. /* Touchscreen absolute values */
  219. #define MXT_MAX_AREA 0xff
  220. #define MXT_PIXELS_PER_MM 20
  221. #define MXT_COORDS_ARR_SIZE 4
  222. struct mxt_info {
  223. u8 family_id;
  224. u8 variant_id;
  225. u8 version;
  226. u8 build;
  227. u8 matrix_xsize;
  228. u8 matrix_ysize;
  229. u8 object_num;
  230. };
  231. struct mxt_object {
  232. u8 type;
  233. u16 start_address;
  234. u8 size_minus_one;
  235. u8 instances_minus_one;
  236. u8 num_report_ids;
  237. } __packed;
  238. struct mxt_dbg {
  239. u16 t37_address;
  240. u16 diag_cmd_address;
  241. struct t37_debug *t37_buf;
  242. unsigned int t37_pages;
  243. unsigned int t37_nodes;
  244. struct v4l2_device v4l2;
  245. struct v4l2_pix_format format;
  246. struct video_device vdev;
  247. struct vb2_queue queue;
  248. struct mutex lock;
  249. int input;
  250. };
  251. enum v4l_dbg_inputs {
  252. MXT_V4L_INPUT_DELTAS,
  253. MXT_V4L_INPUT_REFS,
  254. MXT_V4L_INPUT_MAX,
  255. };
  256. enum mxt_suspend_mode {
  257. MXT_SUSPEND_DEEP_SLEEP = 0,
  258. MXT_SUSPEND_T9_CTRL = 1,
  259. };
  260. /* Config update context */
  261. struct mxt_cfg {
  262. u8 *raw;
  263. size_t raw_size;
  264. off_t raw_pos;
  265. u8 *mem;
  266. size_t mem_size;
  267. int start_ofs;
  268. struct mxt_info info;
  269. };
  270. /* Each client has this additional data */
  271. struct mxt_data {
  272. struct i2c_client *client;
  273. struct input_dev *input_dev;
  274. char phys[64]; /* device physical location */
  275. struct mxt_object *object_table;
  276. struct mxt_info *info;
  277. void *raw_info_block;
  278. unsigned int irq;
  279. unsigned int max_x;
  280. unsigned int max_y;
  281. bool invertx;
  282. bool inverty;
  283. bool xy_switch;
  284. u8 xsize;
  285. u8 ysize;
  286. bool in_bootloader;
  287. u16 mem_size;
  288. u8 t100_aux_ampl;
  289. u8 t100_aux_area;
  290. u8 t100_aux_vect;
  291. u8 max_reportid;
  292. u32 config_crc;
  293. u32 info_crc;
  294. u8 bootloader_addr;
  295. u8 *msg_buf;
  296. u8 *cmd_buf;
  297. u8 *read_buf;
  298. u8 t6_status;
  299. bool update_input;
  300. u8 last_message_count;
  301. u8 num_touchids;
  302. u8 multitouch;
  303. struct t7_config t7_cfg;
  304. struct mxt_dbg dbg;
  305. struct gpio_desc *reset_gpio;
  306. struct gpio_desc *irq_gpio;
  307. bool use_retrigen_workaround;
  308. /* Cached parameters from object table */
  309. u16 T5_address;
  310. u8 T5_msg_size;
  311. u8 T6_reportid;
  312. u16 T6_address;
  313. u16 T7_address;
  314. u16 T71_address;
  315. u8 T9_reportid_min;
  316. u8 T9_reportid_max;
  317. u16 T18_address;
  318. u8 T19_reportid;
  319. u16 T44_address;
  320. u8 T100_reportid_min;
  321. u8 T100_reportid_max;
  322. /* for fw update in bootloader */
  323. struct completion bl_completion;
  324. /* for reset handling */
  325. struct completion reset_completion;
  326. /* for config update handling */
  327. struct completion crc_completion;
  328. /* Enable reporting of input events */
  329. bool enable_reporting;
  330. /* Indicates whether device is in suspend */
  331. bool suspended;
  332. u32 *t19_keymap;
  333. unsigned int t19_num_keys;
  334. enum mxt_suspend_mode suspend_mode;
  335. bool use_regulator;
  336. struct regulator *reg_vdd;
  337. struct regulator *reg_avdd;
  338. struct regulator *reg_xvdd;
  339. struct pinctrl *ts_pinctrl;
  340. struct pinctrl_state *gpio_state_active;
  341. struct pinctrl_state *gpio_state_suspend;
  342. u32 panel_minx, panel_miny, panel_maxx, panel_maxy;
  343. u32 disp_minx, disp_miny, disp_maxx, disp_maxy;
  344. void *notifier_cookie;
  345. };
  346. struct mxt_vb2_buffer {
  347. struct vb2_buffer vb;
  348. struct list_head list;
  349. };
  350. static size_t mxt_obj_size(const struct mxt_object *obj)
  351. {
  352. return obj->size_minus_one + 1;
  353. }
  354. static size_t mxt_obj_instances(const struct mxt_object *obj)
  355. {
  356. return obj->instances_minus_one + 1;
  357. }
  358. static int mxt_regulator_configure(struct mxt_data *data, bool enable);
  359. static bool mxt_object_readable(unsigned int type)
  360. {
  361. switch (type) {
  362. case MXT_GEN_COMMAND_T6:
  363. case MXT_GEN_POWER_T7:
  364. case MXT_GEN_ACQUIRE_T8:
  365. case MXT_GEN_DATASOURCE_T53:
  366. case MXT_TOUCH_MULTI_T9:
  367. case MXT_TOUCH_KEYARRAY_T15:
  368. case MXT_TOUCH_PROXIMITY_T23:
  369. case MXT_TOUCH_PROXKEY_T52:
  370. case MXT_TOUCH_MULTITOUCHSCREEN_T100:
  371. case MXT_PROCI_GRIPFACE_T20:
  372. case MXT_PROCG_NOISE_T22:
  373. case MXT_PROCI_ONETOUCH_T24:
  374. case MXT_PROCI_TWOTOUCH_T27:
  375. case MXT_PROCI_GRIP_T40:
  376. case MXT_PROCI_PALM_T41:
  377. case MXT_PROCI_TOUCHSUPPRESSION_T42:
  378. case MXT_PROCI_STYLUS_T47:
  379. case MXT_PROCG_NOISESUPPRESSION_T48:
  380. case MXT_SPT_COMMSCONFIG_T18:
  381. case MXT_SPT_GPIOPWM_T19:
  382. case MXT_SPT_SELFTEST_T25:
  383. case MXT_SPT_CTECONFIG_T28:
  384. case MXT_SPT_USERDATA_T38:
  385. case MXT_SPT_DIGITIZER_T43:
  386. case MXT_SPT_CTECONFIG_T46:
  387. case MXT_SPT_DYNAMICCONFIGURATIONCONTAINER_T71:
  388. return true;
  389. default:
  390. return false;
  391. }
  392. }
  393. static void mxt_dump_message(struct mxt_data *data, u8 *message)
  394. {
  395. dev_dbg(&data->client->dev, "message: %*ph\n",
  396. data->T5_msg_size, message);
  397. }
  398. static int mxt_wait_for_completion(struct mxt_data *data,
  399. struct completion *comp,
  400. unsigned int timeout_ms)
  401. {
  402. struct device *dev = &data->client->dev;
  403. unsigned long timeout = msecs_to_jiffies(timeout_ms);
  404. long ret;
  405. ret = wait_for_completion_interruptible_timeout(comp, timeout);
  406. if (ret < 0) {
  407. return ret;
  408. } else if (ret == 0) {
  409. dev_err(dev, "Wait for completion timed out.\n");
  410. return -ETIMEDOUT;
  411. }
  412. return 0;
  413. }
  414. static int mxt_bootloader_read(struct mxt_data *data,
  415. u8 *val, unsigned int count)
  416. {
  417. int ret;
  418. struct i2c_msg msg;
  419. msg.addr = data->bootloader_addr;
  420. msg.flags = data->client->flags & I2C_M_TEN;
  421. msg.flags |= I2C_M_RD;
  422. msg.len = count;
  423. msg.buf = data->read_buf;
  424. ret = i2c_transfer(data->client->adapter, &msg, 1);
  425. if (ret == 1) {
  426. memcpy(val, msg.buf, count);
  427. ret = 0;
  428. } else {
  429. ret = ret < 0 ? ret : -EIO;
  430. dev_err(&data->client->dev, "%s: i2c recv failed (%d)\n",
  431. __func__, ret);
  432. }
  433. return ret;
  434. }
  435. static int mxt_bootloader_write(struct mxt_data *data,
  436. const u8 * const val, unsigned int count)
  437. {
  438. int ret;
  439. struct i2c_msg msg;
  440. msg.addr = data->bootloader_addr;
  441. msg.flags = data->client->flags & I2C_M_TEN;
  442. msg.len = count;
  443. msg.buf = (u8 *)val;
  444. ret = i2c_transfer(data->client->adapter, &msg, 1);
  445. if (ret == 1) {
  446. ret = 0;
  447. } else {
  448. ret = ret < 0 ? ret : -EIO;
  449. dev_err(&data->client->dev, "%s: i2c send failed (%d)\n",
  450. __func__, ret);
  451. }
  452. return ret;
  453. }
  454. static int mxt_lookup_bootloader_address(struct mxt_data *data, bool retry)
  455. {
  456. u8 appmode = data->client->addr;
  457. u8 bootloader;
  458. u8 family_id = data->info ? data->info->family_id : 0;
  459. switch (appmode) {
  460. case 0x4a:
  461. case 0x4b:
  462. /* Chips after 1664S use different scheme */
  463. if (retry || family_id >= 0xa2) {
  464. bootloader = appmode - 0x24;
  465. break;
  466. }
  467. fallthrough; /* for normal case */
  468. case 0x4c:
  469. case 0x4d:
  470. case 0x5a:
  471. case 0x5b:
  472. bootloader = appmode - 0x26;
  473. break;
  474. default:
  475. dev_err(&data->client->dev,
  476. "Appmode i2c address 0x%02x not found\n",
  477. appmode);
  478. return -EINVAL;
  479. }
  480. data->bootloader_addr = bootloader;
  481. return 0;
  482. }
  483. static int mxt_probe_bootloader(struct mxt_data *data, bool alt_address)
  484. {
  485. struct device *dev = &data->client->dev;
  486. int error;
  487. u8 val;
  488. bool crc_failure;
  489. error = mxt_lookup_bootloader_address(data, alt_address);
  490. if (error)
  491. return error;
  492. error = mxt_bootloader_read(data, &val, 1);
  493. if (error)
  494. return error;
  495. /* Check app crc fail mode */
  496. crc_failure = (val & ~MXT_BOOT_STATUS_MASK) == MXT_APP_CRC_FAIL;
  497. dev_err(dev, "Detected bootloader, status:%02X%s\n",
  498. val, crc_failure ? ", APP_CRC_FAIL" : "");
  499. return 0;
  500. }
  501. static u8 mxt_get_bootloader_version(struct mxt_data *data, u8 val)
  502. {
  503. struct device *dev = &data->client->dev;
  504. u8 ver = 0;
  505. u8 *buf = kzalloc(4, GFP_KERNEL);
  506. if ((val & MXT_BOOT_EXTENDED_ID) && buf) {
  507. if (mxt_bootloader_read(data, buf, 3) != 0) {
  508. dev_err(dev, "%s: i2c failure\n", __func__);
  509. return val;
  510. }
  511. dev_dbg(dev, "Bootloader ID:%d Version:%d\n",
  512. *(buf + 1), *(buf + 2));
  513. ver = *(buf + 0);
  514. kfree(buf);
  515. return ver;
  516. } else {
  517. dev_dbg(dev, "Bootloader ID:%d\n", val & MXT_BOOT_ID_MASK);
  518. return val;
  519. }
  520. }
  521. static int mxt_check_bootloader(struct mxt_data *data, unsigned int state,
  522. bool wait)
  523. {
  524. struct device *dev = &data->client->dev;
  525. u8 val;
  526. int ret;
  527. recheck:
  528. if (wait) {
  529. /*
  530. * In application update mode, the interrupt
  531. * line signals state transitions. We must wait for the
  532. * CHG assertion before reading the status byte.
  533. * Once the status byte has been read, the line is deasserted.
  534. */
  535. ret = mxt_wait_for_completion(data, &data->bl_completion,
  536. MXT_FW_CHG_TIMEOUT);
  537. if (ret) {
  538. /*
  539. * TODO: handle -ERESTARTSYS better by terminating
  540. * fw update process before returning to userspace
  541. * by writing length 0x000 to device (iff we are in
  542. * WAITING_FRAME_DATA state).
  543. */
  544. dev_err(dev, "Update wait error %d\n", ret);
  545. return ret;
  546. }
  547. }
  548. ret = mxt_bootloader_read(data, &val, 1);
  549. if (ret)
  550. return ret;
  551. if (state == MXT_WAITING_BOOTLOAD_CMD)
  552. val = mxt_get_bootloader_version(data, val);
  553. switch (state) {
  554. case MXT_WAITING_BOOTLOAD_CMD:
  555. case MXT_WAITING_FRAME_DATA:
  556. case MXT_APP_CRC_FAIL:
  557. val &= ~MXT_BOOT_STATUS_MASK;
  558. break;
  559. case MXT_FRAME_CRC_PASS:
  560. if (val == MXT_FRAME_CRC_CHECK) {
  561. goto recheck;
  562. } else if (val == MXT_FRAME_CRC_FAIL) {
  563. dev_err(dev, "Bootloader CRC fail\n");
  564. return -EINVAL;
  565. }
  566. break;
  567. default:
  568. return -EINVAL;
  569. }
  570. if (val != state) {
  571. dev_err(dev, "Invalid bootloader state %02X != %02X\n",
  572. val, state);
  573. return -EINVAL;
  574. }
  575. return 0;
  576. }
  577. static int mxt_send_bootloader_cmd(struct mxt_data *data, bool unlock)
  578. {
  579. int ret;
  580. u8 *buf = data->cmd_buf;
  581. if (unlock) {
  582. *buf = MXT_UNLOCK_CMD_LSB;
  583. *(buf + 1) = MXT_UNLOCK_CMD_MSB;
  584. } else {
  585. *buf = 0x01;
  586. *(buf + 1) = 0x01;
  587. }
  588. ret = mxt_bootloader_write(data, buf, 2);
  589. if (ret)
  590. return ret;
  591. return 0;
  592. }
  593. static int __mxt_read_reg(struct mxt_data *data, u16 reg, u16 len, void *val)
  594. {
  595. struct i2c_msg xfer[2];
  596. int ret;
  597. u8 *buf = data->cmd_buf;
  598. struct i2c_client *client = data->client;
  599. *buf = reg & 0xff;
  600. *(buf + 1) = (reg >> 8) & 0xff;
  601. /* Write register */
  602. xfer[0].addr = client->addr;
  603. xfer[0].flags = 0;
  604. xfer[0].len = 2;
  605. xfer[0].buf = buf;
  606. /* Read data */
  607. xfer[1].addr = client->addr;
  608. xfer[1].flags = I2C_M_RD;
  609. xfer[1].len = len;
  610. xfer[1].buf = val;
  611. ret = i2c_transfer(client->adapter, xfer, 2);
  612. if (ret == 2) {
  613. ret = 0;
  614. } else {
  615. if (ret >= 0)
  616. ret = -EIO;
  617. dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
  618. __func__, ret);
  619. }
  620. return ret;
  621. }
  622. static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
  623. const void *val)
  624. {
  625. u8 *buf;
  626. size_t count;
  627. int ret;
  628. count = len + 2;
  629. buf = kmalloc(count, GFP_KERNEL);
  630. if (!buf)
  631. return -ENOMEM;
  632. buf[0] = reg & 0xff;
  633. buf[1] = (reg >> 8) & 0xff;
  634. memcpy(&buf[2], val, len);
  635. ret = i2c_master_send(client, buf, count);
  636. if (ret == count) {
  637. ret = 0;
  638. } else {
  639. if (ret >= 0)
  640. ret = -EIO;
  641. dev_dbg(&client->dev, "%s: i2c send failed (%d)\n",
  642. __func__, ret);
  643. }
  644. kfree(buf);
  645. return ret;
  646. }
  647. static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
  648. {
  649. return __mxt_write_reg(client, reg, 1, &val);
  650. }
  651. static struct mxt_object *
  652. mxt_get_object(struct mxt_data *data, u8 type)
  653. {
  654. struct mxt_object *object;
  655. int i;
  656. for (i = 0; i < data->info->object_num; i++) {
  657. object = data->object_table + i;
  658. if (object->type == type)
  659. return object;
  660. }
  661. dev_warn(&data->client->dev, "Invalid object type T%u\n", type);
  662. return NULL;
  663. }
  664. static void mxt_proc_t6_messages(struct mxt_data *data, u8 *msg)
  665. {
  666. struct device *dev = &data->client->dev;
  667. u8 status = msg[1];
  668. u32 crc = msg[2] | (msg[3] << 8) | (msg[4] << 16);
  669. if (crc != data->config_crc) {
  670. data->config_crc = crc;
  671. dev_dbg(dev, "T6 Config Checksum: 0x%06X\n", crc);
  672. }
  673. complete(&data->crc_completion);
  674. /* Detect reset */
  675. if (status & MXT_T6_STATUS_RESET)
  676. complete(&data->reset_completion);
  677. /* Output debug if status has changed */
  678. if (status != data->t6_status)
  679. dev_dbg(dev, "T6 Status 0x%02X%s%s%s%s%s%s%s\n",
  680. status,
  681. status == 0 ? " OK" : "",
  682. status & MXT_T6_STATUS_RESET ? " RESET" : "",
  683. status & MXT_T6_STATUS_OFL ? " OFL" : "",
  684. status & MXT_T6_STATUS_SIGERR ? " SIGERR" : "",
  685. status & MXT_T6_STATUS_CAL ? " CAL" : "",
  686. status & MXT_T6_STATUS_CFGERR ? " CFGERR" : "",
  687. status & MXT_T6_STATUS_COMSERR ? " COMSERR" : "");
  688. /* Save current status */
  689. data->t6_status = status;
  690. }
  691. static int mxt_write_object(struct mxt_data *data,
  692. u8 type, u8 offset, u8 val)
  693. {
  694. struct mxt_object *object;
  695. u16 reg;
  696. object = mxt_get_object(data, type);
  697. if (!object || offset >= mxt_obj_size(object))
  698. return -EINVAL;
  699. reg = object->start_address;
  700. return mxt_write_reg(data->client, reg + offset, val);
  701. }
  702. static void mxt_input_button(struct mxt_data *data, u8 *message)
  703. {
  704. struct input_dev *input = data->input_dev;
  705. int i;
  706. for (i = 0; i < data->t19_num_keys; i++) {
  707. if (data->t19_keymap[i] == KEY_RESERVED)
  708. continue;
  709. /* Active-low switch */
  710. input_report_key(input, data->t19_keymap[i],
  711. !(message[1] & BIT(i)));
  712. }
  713. }
  714. static void mxt_input_sync(struct mxt_data *data)
  715. {
  716. input_mt_report_pointer_emulation(data->input_dev,
  717. data->t19_num_keys);
  718. input_sync(data->input_dev);
  719. }
  720. static void mxt_proc_t9_message(struct mxt_data *data, u8 *message)
  721. {
  722. struct device *dev = &data->client->dev;
  723. struct input_dev *input_dev = data->input_dev;
  724. int id;
  725. u8 status;
  726. int x;
  727. int y;
  728. int area;
  729. int amplitude;
  730. id = message[0] - data->T9_reportid_min;
  731. status = message[1];
  732. x = (message[2] << 4) | ((message[4] >> 4) & 0xf);
  733. y = (message[3] << 4) | ((message[4] & 0xf));
  734. /* Handle 10/12 bit switching */
  735. if (data->max_x < 1024)
  736. x >>= 2;
  737. if (data->max_y < 1024)
  738. y >>= 2;
  739. area = message[5];
  740. amplitude = message[6];
  741. dev_dbg(dev,
  742. "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
  743. id,
  744. (status & MXT_T9_DETECT) ? 'D' : '.',
  745. (status & MXT_T9_PRESS) ? 'P' : '.',
  746. (status & MXT_T9_RELEASE) ? 'R' : '.',
  747. (status & MXT_T9_MOVE) ? 'M' : '.',
  748. (status & MXT_T9_VECTOR) ? 'V' : '.',
  749. (status & MXT_T9_AMP) ? 'A' : '.',
  750. (status & MXT_T9_SUPPRESS) ? 'S' : '.',
  751. (status & MXT_T9_UNGRIP) ? 'U' : '.',
  752. x, y, area, amplitude);
  753. input_mt_slot(input_dev, id);
  754. if (status & MXT_T9_DETECT) {
  755. /*
  756. * Multiple bits may be set if the host is slow to read
  757. * the status messages, indicating all the events that
  758. * have happened.
  759. */
  760. if (status & MXT_T9_RELEASE) {
  761. input_mt_report_slot_inactive(input_dev);
  762. mxt_input_sync(data);
  763. }
  764. /* if active, pressure must be non-zero */
  765. if (!amplitude)
  766. amplitude = MXT_PRESSURE_DEFAULT;
  767. /* Handle orientation */
  768. if (data->xy_switch)
  769. swap(x, y);
  770. if (data->invertx)
  771. x = data->max_x - x;
  772. if (data->inverty)
  773. y = data->max_y - y;
  774. /* Touch active */
  775. input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 1);
  776. input_report_abs(input_dev, ABS_MT_POSITION_X,
  777. x - data->disp_minx);
  778. input_report_abs(input_dev, ABS_MT_POSITION_Y,
  779. y - data->disp_miny);
  780. input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude);
  781. input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
  782. } else {
  783. /* Touch no longer active, close out slot */
  784. input_mt_report_slot_inactive(input_dev);
  785. }
  786. data->update_input = true;
  787. }
  788. static void mxt_proc_t100_message(struct mxt_data *data, u8 *message)
  789. {
  790. struct device *dev = &data->client->dev;
  791. struct input_dev *input_dev = data->input_dev;
  792. int id;
  793. u8 status;
  794. u8 type = 0;
  795. u16 x;
  796. u16 y;
  797. int distance = 0;
  798. int tool = 0;
  799. u8 major = 0;
  800. u8 pressure = 0;
  801. u8 orientation = 0;
  802. id = message[0] - data->T100_reportid_min - 2;
  803. /* ignore SCRSTATUS events */
  804. if (id < 0)
  805. return;
  806. status = message[1];
  807. x = get_unaligned_le16(&message[2]);
  808. y = get_unaligned_le16(&message[4]);
  809. if (status & MXT_T100_DETECT) {
  810. type = (status & MXT_T100_TYPE_MASK) >> 4;
  811. switch (type) {
  812. case MXT_T100_TYPE_HOVERING_FINGER:
  813. tool = MT_TOOL_FINGER;
  814. distance = MXT_DISTANCE_HOVERING;
  815. if (data->t100_aux_vect)
  816. orientation = message[data->t100_aux_vect];
  817. break;
  818. case MXT_T100_TYPE_FINGER:
  819. case MXT_T100_TYPE_GLOVE:
  820. tool = MT_TOOL_FINGER;
  821. distance = MXT_DISTANCE_ACTIVE_TOUCH;
  822. if (data->t100_aux_area)
  823. major = message[data->t100_aux_area];
  824. if (data->t100_aux_ampl)
  825. pressure = message[data->t100_aux_ampl];
  826. if (data->t100_aux_vect)
  827. orientation = message[data->t100_aux_vect];
  828. break;
  829. case MXT_T100_TYPE_PASSIVE_STYLUS:
  830. tool = MT_TOOL_PEN;
  831. /*
  832. * Passive stylus is reported with size zero so
  833. * hardcode.
  834. */
  835. major = MXT_TOUCH_MAJOR_DEFAULT;
  836. if (data->t100_aux_ampl)
  837. pressure = message[data->t100_aux_ampl];
  838. break;
  839. case MXT_T100_TYPE_LARGE_TOUCH:
  840. /* Ignore suppressed touch */
  841. break;
  842. default:
  843. dev_dbg(dev, "Unexpected T100 type\n");
  844. return;
  845. }
  846. }
  847. /*
  848. * Values reported should be non-zero if tool is touching the
  849. * device
  850. */
  851. if (!pressure && type != MXT_T100_TYPE_HOVERING_FINGER)
  852. pressure = MXT_PRESSURE_DEFAULT;
  853. input_mt_slot(input_dev, id);
  854. if (status & MXT_T100_DETECT) {
  855. dev_dbg(dev, "[%u] type:%u x:%u y:%u a:%02X p:%02X v:%02X\n",
  856. id, type, x, y, major, pressure, orientation);
  857. /* Handle orientation */
  858. if (data->xy_switch)
  859. swap(x, y);
  860. if (data->invertx)
  861. x = data->max_x - x;
  862. if (data->inverty)
  863. y = data->max_y - y;
  864. input_mt_report_slot_state(input_dev, tool, 1);
  865. input_report_abs(input_dev, ABS_MT_POSITION_X,
  866. x - data->disp_minx);
  867. input_report_abs(input_dev, ABS_MT_POSITION_Y,
  868. y - data->disp_miny);
  869. input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, major);
  870. input_report_abs(input_dev, ABS_MT_PRESSURE, pressure);
  871. input_report_abs(input_dev, ABS_MT_DISTANCE, distance);
  872. input_report_abs(input_dev, ABS_MT_ORIENTATION, orientation);
  873. } else {
  874. dev_dbg(dev, "[%u] release\n", id);
  875. /* close out slot */
  876. input_mt_report_slot_inactive(input_dev);
  877. }
  878. data->update_input = true;
  879. }
  880. static int mxt_proc_message(struct mxt_data *data, u8 *message)
  881. {
  882. u8 report_id = message[0];
  883. if (report_id == MXT_RPTID_NOMSG)
  884. return 0;
  885. if (report_id == data->T6_reportid) {
  886. mxt_proc_t6_messages(data, message);
  887. } else if (!data->input_dev) {
  888. /*
  889. * Do not report events if input device
  890. * is not yet registered.
  891. */
  892. mxt_dump_message(data, message);
  893. } else if (report_id >= data->T9_reportid_min &&
  894. report_id <= data->T9_reportid_max) {
  895. mxt_proc_t9_message(data, message);
  896. } else if (report_id >= data->T100_reportid_min &&
  897. report_id <= data->T100_reportid_max) {
  898. mxt_proc_t100_message(data, message);
  899. } else if (report_id == data->T19_reportid) {
  900. mxt_input_button(data, message);
  901. data->update_input = true;
  902. } else {
  903. mxt_dump_message(data, message);
  904. }
  905. return 1;
  906. }
  907. static int mxt_read_and_process_messages(struct mxt_data *data, u8 count)
  908. {
  909. struct device *dev = &data->client->dev;
  910. int ret;
  911. int i;
  912. u8 num_valid = 0;
  913. /* Safety check for msg_buf */
  914. if (count > data->max_reportid)
  915. return -EINVAL;
  916. /* Process remaining messages if necessary */
  917. ret = __mxt_read_reg(data, data->T5_address,
  918. data->T5_msg_size * count, data->msg_buf);
  919. if (ret) {
  920. dev_err(dev, "Failed to read %u messages (%d)\n", count, ret);
  921. return ret;
  922. }
  923. for (i = 0; i < count; i++) {
  924. ret = mxt_proc_message(data,
  925. data->msg_buf + data->T5_msg_size * i);
  926. if (ret == 1)
  927. num_valid++;
  928. }
  929. /* return number of messages read */
  930. return num_valid;
  931. }
  932. static irqreturn_t mxt_process_messages_t44(struct mxt_data *data)
  933. {
  934. struct device *dev = &data->client->dev;
  935. int ret;
  936. u8 count, num_left;
  937. /* Read T44 and T5 together */
  938. ret = __mxt_read_reg(data, data->T44_address,
  939. data->T5_msg_size + 1, data->msg_buf);
  940. if (ret) {
  941. dev_err(dev, "Failed to read T44 and T5 (%d)\n", ret);
  942. return IRQ_NONE;
  943. }
  944. count = data->msg_buf[0];
  945. /*
  946. * This condition may be caused by the CHG line being configured in
  947. * Mode 0. It results in unnecessary I2C operations but it is benign.
  948. */
  949. if (count == 0)
  950. return IRQ_NONE;
  951. if (count > data->max_reportid) {
  952. dev_warn(dev, "T44 count %d exceeded max report id\n", count);
  953. count = data->max_reportid;
  954. }
  955. /* Process first message */
  956. ret = mxt_proc_message(data, data->msg_buf + 1);
  957. if (ret < 0) {
  958. dev_warn(dev, "Unexpected invalid message\n");
  959. return IRQ_NONE;
  960. }
  961. num_left = count - 1;
  962. /* Process remaining messages if necessary */
  963. if (num_left) {
  964. ret = mxt_read_and_process_messages(data, num_left);
  965. if (ret < 0)
  966. goto end;
  967. else if (ret != num_left)
  968. dev_warn(dev, "Unexpected invalid message\n");
  969. }
  970. end:
  971. if (data->update_input) {
  972. mxt_input_sync(data);
  973. data->update_input = false;
  974. }
  975. return IRQ_HANDLED;
  976. }
  977. static int mxt_process_messages_until_invalid(struct mxt_data *data)
  978. {
  979. struct device *dev = &data->client->dev;
  980. int count, read;
  981. u8 tries = 2;
  982. count = data->max_reportid;
  983. /* Read messages until we force an invalid */
  984. do {
  985. read = mxt_read_and_process_messages(data, count);
  986. if (read < count)
  987. return 0;
  988. } while (--tries);
  989. if (data->update_input) {
  990. mxt_input_sync(data);
  991. data->update_input = false;
  992. }
  993. dev_err(dev, "CHG pin isn't cleared\n");
  994. return -EBUSY;
  995. }
  996. static irqreturn_t mxt_process_messages(struct mxt_data *data)
  997. {
  998. int total_handled, num_handled;
  999. u8 count = data->last_message_count;
  1000. if (count < 1 || count > data->max_reportid)
  1001. count = 1;
  1002. /* include final invalid message */
  1003. total_handled = mxt_read_and_process_messages(data, count + 1);
  1004. if (total_handled < 0)
  1005. return IRQ_NONE;
  1006. /* if there were invalid messages, then we are done */
  1007. else if (total_handled <= count)
  1008. goto update_count;
  1009. /* keep reading two msgs until one is invalid or reportid limit */
  1010. do {
  1011. num_handled = mxt_read_and_process_messages(data, 2);
  1012. if (num_handled < 0)
  1013. return IRQ_NONE;
  1014. total_handled += num_handled;
  1015. if (num_handled < 2)
  1016. break;
  1017. } while (total_handled < data->num_touchids);
  1018. update_count:
  1019. data->last_message_count = total_handled;
  1020. if (data->update_input) {
  1021. mxt_input_sync(data);
  1022. data->update_input = false;
  1023. }
  1024. return IRQ_HANDLED;
  1025. }
  1026. static irqreturn_t mxt_interrupt(int irq, void *dev_id)
  1027. {
  1028. struct mxt_data *data = dev_id;
  1029. if (data->in_bootloader) {
  1030. /* bootloader state transition completion */
  1031. complete(&data->bl_completion);
  1032. return IRQ_HANDLED;
  1033. }
  1034. if (!data->object_table)
  1035. return IRQ_HANDLED;
  1036. if (data->T44_address) {
  1037. return mxt_process_messages_t44(data);
  1038. } else {
  1039. return mxt_process_messages(data);
  1040. }
  1041. }
  1042. static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset,
  1043. u8 value, bool wait)
  1044. {
  1045. u16 reg;
  1046. u8 command_register;
  1047. int timeout_counter = 0;
  1048. int ret;
  1049. reg = data->T6_address + cmd_offset;
  1050. ret = mxt_write_reg(data->client, reg, value);
  1051. if (ret)
  1052. return ret;
  1053. if (!wait)
  1054. return 0;
  1055. do {
  1056. msleep(20);
  1057. ret = __mxt_read_reg(data, reg, 1, data->read_buf);
  1058. if (ret)
  1059. return ret;
  1060. command_register = *(data->read_buf);
  1061. } while (command_register != 0 && timeout_counter++ <= 100);
  1062. if (timeout_counter > 100) {
  1063. dev_err(&data->client->dev, "Command failed!\n");
  1064. return -EIO;
  1065. }
  1066. return 0;
  1067. }
  1068. static int mxt_acquire_irq(struct mxt_data *data)
  1069. {
  1070. int error;
  1071. enable_irq(data->irq);
  1072. if (data->use_retrigen_workaround) {
  1073. error = mxt_process_messages_until_invalid(data);
  1074. if (error)
  1075. return error;
  1076. }
  1077. return 0;
  1078. }
  1079. static int mxt_soft_reset(struct mxt_data *data)
  1080. {
  1081. struct device *dev = &data->client->dev;
  1082. int ret = 0;
  1083. dev_dbg(dev, "Resetting device\n");
  1084. disable_irq(data->irq);
  1085. reinit_completion(&data->reset_completion);
  1086. ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false);
  1087. if (ret)
  1088. return ret;
  1089. /* Ignore CHG line for 100ms after reset */
  1090. msleep(MXT_RESET_INVALID_CHG);
  1091. mxt_acquire_irq(data);
  1092. ret = mxt_wait_for_completion(data, &data->reset_completion,
  1093. MXT_RESET_TIMEOUT);
  1094. if (ret)
  1095. return ret;
  1096. return 0;
  1097. }
  1098. static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value)
  1099. {
  1100. /*
  1101. * On failure, CRC is set to 0 and config will always be
  1102. * downloaded.
  1103. */
  1104. data->config_crc = 0;
  1105. reinit_completion(&data->crc_completion);
  1106. mxt_t6_command(data, cmd, value, true);
  1107. /*
  1108. * Wait for crc message. On failure, CRC is set to 0 and config will
  1109. * always be downloaded.
  1110. */
  1111. mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT);
  1112. }
  1113. static void mxt_calc_crc24(u32 *crc, u8 firstbyte, u8 secondbyte)
  1114. {
  1115. static const unsigned int crcpoly = 0x80001B;
  1116. u32 result;
  1117. u32 data_word;
  1118. data_word = (secondbyte << 8) | firstbyte;
  1119. result = ((*crc << 1) ^ data_word);
  1120. if (result & 0x1000000)
  1121. result ^= crcpoly;
  1122. *crc = result;
  1123. }
  1124. static u32 mxt_calculate_crc(u8 *base, off_t start_off, off_t end_off)
  1125. {
  1126. u32 crc = 0;
  1127. u8 *ptr = base + start_off;
  1128. u8 *last_val = base + end_off - 1;
  1129. if (end_off < start_off)
  1130. return -EINVAL;
  1131. while (ptr < last_val) {
  1132. mxt_calc_crc24(&crc, *ptr, *(ptr + 1));
  1133. ptr += 2;
  1134. }
  1135. /* if len is odd, fill the last byte with 0 */
  1136. if (ptr == last_val)
  1137. mxt_calc_crc24(&crc, *ptr, 0);
  1138. /* Mask to 24-bit */
  1139. crc &= 0x00FFFFFF;
  1140. return crc;
  1141. }
  1142. static int mxt_check_retrigen(struct mxt_data *data)
  1143. {
  1144. struct i2c_client *client = data->client;
  1145. int error;
  1146. int val;
  1147. struct irq_data *irqd;
  1148. data->use_retrigen_workaround = false;
  1149. irqd = irq_get_irq_data(data->irq);
  1150. if (!irqd)
  1151. return -EINVAL;
  1152. if (irqd_is_level_type(irqd))
  1153. return 0;
  1154. if (data->T18_address) {
  1155. error = __mxt_read_reg(data,
  1156. data->T18_address + MXT_COMMS_CTRL,
  1157. 1, &val);
  1158. if (error)
  1159. return error;
  1160. if (val & MXT_COMMS_RETRIGEN)
  1161. return 0;
  1162. }
  1163. dev_warn(&client->dev, "Enabling RETRIGEN workaround\n");
  1164. data->use_retrigen_workaround = true;
  1165. return 0;
  1166. }
  1167. static int mxt_prepare_cfg_mem(struct mxt_data *data, struct mxt_cfg *cfg)
  1168. {
  1169. struct device *dev = &data->client->dev;
  1170. struct mxt_object *object;
  1171. unsigned int type, instance, size, byte_offset;
  1172. int offset;
  1173. int ret;
  1174. int i;
  1175. u16 reg;
  1176. u8 val;
  1177. while (cfg->raw_pos < cfg->raw_size) {
  1178. /* Read type, instance, length */
  1179. ret = sscanf(cfg->raw + cfg->raw_pos, "%x %x %x%n",
  1180. &type, &instance, &size, &offset);
  1181. if (ret == 0) {
  1182. /* EOF */
  1183. break;
  1184. } else if (ret != 3) {
  1185. dev_err(dev, "Bad format: failed to parse object\n");
  1186. return -EINVAL;
  1187. }
  1188. if (cfg->raw_pos + offset >= cfg->raw_size)
  1189. return -EINVAL;
  1190. cfg->raw_pos += offset;
  1191. object = mxt_get_object(data, type);
  1192. if (!object) {
  1193. /* Skip object */
  1194. for (i = 0; i < size; i++) {
  1195. ret = sscanf(cfg->raw + cfg->raw_pos, "%hhx%n",
  1196. &val, &offset);
  1197. if (ret != 1 || (cfg->raw_pos + offset >=
  1198. cfg->raw_size)) {
  1199. dev_err(dev, "Bad format in T%d at %d\n",
  1200. type, i);
  1201. return -EINVAL;
  1202. }
  1203. cfg->raw_pos += offset;
  1204. }
  1205. continue;
  1206. }
  1207. if (size > mxt_obj_size(object)) {
  1208. /*
  1209. * Either we are in fallback mode due to wrong
  1210. * config or config from a later fw version,
  1211. * or the file is corrupt or hand-edited.
  1212. */
  1213. dev_warn(dev, "Discarding %zu byte(s) in T%u\n",
  1214. size - mxt_obj_size(object), type);
  1215. } else if (mxt_obj_size(object) > size) {
  1216. /*
  1217. * If firmware is upgraded, new bytes may be added to
  1218. * end of objects. It is generally forward compatible
  1219. * to zero these bytes - previous behaviour will be
  1220. * retained. However this does invalidate the CRC and
  1221. * will force fallback mode until the configuration is
  1222. * updated. We warn here but do nothing else - the
  1223. * malloc has zeroed the entire configuration.
  1224. */
  1225. dev_warn(dev, "Zeroing %zu byte(s) in T%d\n",
  1226. mxt_obj_size(object) - size, type);
  1227. }
  1228. if (instance >= mxt_obj_instances(object)) {
  1229. dev_err(dev, "Object instances exceeded!\n");
  1230. return -EINVAL;
  1231. }
  1232. reg = object->start_address + mxt_obj_size(object) * instance;
  1233. for (i = 0; i < size; i++) {
  1234. ret = sscanf(cfg->raw + cfg->raw_pos, "%hhx%n",
  1235. &val,
  1236. &offset);
  1237. if (ret != 1 ||
  1238. (cfg->raw_pos + offset >= cfg->raw_size)) {
  1239. dev_err(dev, "Bad format in T%d at %d\n",
  1240. type, i);
  1241. return -EINVAL;
  1242. }
  1243. cfg->raw_pos += offset;
  1244. if (i > mxt_obj_size(object))
  1245. continue;
  1246. byte_offset = reg + i - cfg->start_ofs;
  1247. if (byte_offset >= 0 && byte_offset < cfg->mem_size) {
  1248. *(cfg->mem + byte_offset) = val;
  1249. } else {
  1250. dev_err(dev, "Bad object: reg:%d, T%d, ofs=%d\n",
  1251. reg, object->type, byte_offset);
  1252. return -EINVAL;
  1253. }
  1254. }
  1255. }
  1256. return 0;
  1257. }
  1258. static int mxt_upload_cfg_mem(struct mxt_data *data, struct mxt_cfg *cfg)
  1259. {
  1260. unsigned int byte_offset = 0;
  1261. int error;
  1262. /* Write configuration as blocks */
  1263. while (byte_offset < cfg->mem_size) {
  1264. unsigned int size = cfg->mem_size - byte_offset;
  1265. if (size > MXT_MAX_BLOCK_WRITE)
  1266. size = MXT_MAX_BLOCK_WRITE;
  1267. error = __mxt_write_reg(data->client,
  1268. cfg->start_ofs + byte_offset,
  1269. size, cfg->mem + byte_offset);
  1270. if (error) {
  1271. dev_err(&data->client->dev,
  1272. "Config write error, ret=%d\n", error);
  1273. return error;
  1274. }
  1275. byte_offset += size;
  1276. }
  1277. return 0;
  1278. }
  1279. static int mxt_init_t7_power_cfg(struct mxt_data *data);
  1280. /*
  1281. * mxt_update_cfg - download configuration to chip
  1282. *
  1283. * Atmel Raw Config File Format
  1284. *
  1285. * The first four lines of the raw config file contain:
  1286. * 1) Version
  1287. * 2) Chip ID Information (first 7 bytes of device memory)
  1288. * 3) Chip Information Block 24-bit CRC Checksum
  1289. * 4) Chip Configuration 24-bit CRC Checksum
  1290. *
  1291. * The rest of the file consists of one line per object instance:
  1292. * <TYPE> <INSTANCE> <SIZE> <CONTENTS>
  1293. *
  1294. * <TYPE> - 2-byte object type as hex
  1295. * <INSTANCE> - 2-byte object instance number as hex
  1296. * <SIZE> - 2-byte object size as hex
  1297. * <CONTENTS> - array of <SIZE> 1-byte hex values
  1298. */
  1299. static int mxt_update_cfg(struct mxt_data *data, const struct firmware *fw)
  1300. {
  1301. struct device *dev = &data->client->dev;
  1302. struct mxt_cfg cfg;
  1303. int ret;
  1304. int offset;
  1305. int i;
  1306. u32 info_crc, config_crc, calculated_crc;
  1307. u16 crc_start = 0;
  1308. /* Make zero terminated copy of the OBP_RAW file */
  1309. cfg.raw = kmemdup_nul(fw->data, fw->size, GFP_KERNEL);
  1310. if (!cfg.raw)
  1311. return -ENOMEM;
  1312. cfg.raw_size = fw->size;
  1313. mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1);
  1314. if (memcmp(cfg.raw, MXT_CFG_MAGIC, strlen(MXT_CFG_MAGIC))) {
  1315. dev_err(dev, "Unrecognised config file\n");
  1316. ret = -EINVAL;
  1317. goto release_raw;
  1318. }
  1319. cfg.raw_pos = strlen(MXT_CFG_MAGIC);
  1320. /* Load information block and check */
  1321. for (i = 0; i < sizeof(struct mxt_info); i++) {
  1322. ret = sscanf(cfg.raw + cfg.raw_pos, "%hhx%n",
  1323. (unsigned char *)&cfg.info + i,
  1324. &offset);
  1325. if (ret != 1 || (cfg.raw_pos + offset >= cfg.raw_size)) {
  1326. dev_err(dev, "Bad format\n");
  1327. ret = -EINVAL;
  1328. goto release_raw;
  1329. }
  1330. cfg.raw_pos += offset;
  1331. }
  1332. if (cfg.info.family_id != data->info->family_id) {
  1333. dev_err(dev, "Family ID mismatch!\n");
  1334. ret = -EINVAL;
  1335. goto release_raw;
  1336. }
  1337. if (cfg.info.variant_id != data->info->variant_id) {
  1338. dev_err(dev, "Variant ID mismatch!\n");
  1339. ret = -EINVAL;
  1340. goto release_raw;
  1341. }
  1342. /* Read CRCs */
  1343. ret = sscanf(cfg.raw + cfg.raw_pos, "%x%n", &info_crc, &offset);
  1344. if (ret != 1 || (cfg.raw_pos + offset >= cfg.raw_size)) {
  1345. dev_err(dev, "Bad format: failed to parse Info CRC\n");
  1346. ret = -EINVAL;
  1347. goto release_raw;
  1348. }
  1349. cfg.raw_pos += offset;
  1350. ret = sscanf(cfg.raw + cfg.raw_pos, "%x%n", &config_crc, &offset);
  1351. if (ret != 1 || (cfg.raw_pos + offset >= cfg.raw_size)) {
  1352. dev_err(dev, "Bad format: failed to parse Config CRC\n");
  1353. ret = -EINVAL;
  1354. goto release_raw;
  1355. }
  1356. cfg.raw_pos += offset;
  1357. /*
  1358. * The Info Block CRC is calculated over mxt_info and the object
  1359. * table. If it does not match then we are trying to load the
  1360. * configuration from a different chip or firmware version, so
  1361. * the configuration CRC is invalid anyway.
  1362. */
  1363. if (info_crc == data->info_crc) {
  1364. if (config_crc == 0 || data->config_crc == 0) {
  1365. dev_info(dev, "CRC zero, attempting to apply config\n");
  1366. } else if (config_crc == data->config_crc) {
  1367. dev_dbg(dev, "Config CRC 0x%06X: OK\n",
  1368. data->config_crc);
  1369. ret = 0;
  1370. goto release_raw;
  1371. } else {
  1372. dev_info(dev, "Config CRC 0x%06X: does not match file 0x%06X\n",
  1373. data->config_crc, config_crc);
  1374. }
  1375. } else {
  1376. dev_warn(dev,
  1377. "Warning: Info CRC error - device=0x%06X file=0x%06X\n",
  1378. data->info_crc, info_crc);
  1379. }
  1380. /* Malloc memory to store configuration */
  1381. cfg.start_ofs = MXT_OBJECT_START +
  1382. data->info->object_num * sizeof(struct mxt_object) +
  1383. MXT_INFO_CHECKSUM_SIZE;
  1384. cfg.mem_size = data->mem_size - cfg.start_ofs;
  1385. cfg.mem = kzalloc(cfg.mem_size, GFP_KERNEL);
  1386. if (!cfg.mem) {
  1387. ret = -ENOMEM;
  1388. goto release_raw;
  1389. }
  1390. ret = mxt_prepare_cfg_mem(data, &cfg);
  1391. if (ret)
  1392. goto release_mem;
  1393. /* Calculate crc of the received configs (not the raw config file) */
  1394. if (data->T71_address)
  1395. crc_start = data->T71_address;
  1396. else if (data->T7_address)
  1397. crc_start = data->T7_address;
  1398. else
  1399. dev_warn(dev, "Could not find CRC start\n");
  1400. if (crc_start > cfg.start_ofs) {
  1401. calculated_crc = mxt_calculate_crc(cfg.mem,
  1402. crc_start - cfg.start_ofs,
  1403. cfg.mem_size);
  1404. if (config_crc > 0 && config_crc != calculated_crc)
  1405. dev_warn(dev, "Config CRC in file inconsistent, calculated=%06X, file=%06X\n",
  1406. calculated_crc, config_crc);
  1407. }
  1408. ret = mxt_upload_cfg_mem(data, &cfg);
  1409. if (ret)
  1410. goto release_mem;
  1411. mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE);
  1412. ret = mxt_check_retrigen(data);
  1413. if (ret)
  1414. goto release_mem;
  1415. ret = mxt_soft_reset(data);
  1416. if (ret)
  1417. goto release_mem;
  1418. dev_info(dev, "Config successfully updated\n");
  1419. /* T7 config may have changed */
  1420. mxt_init_t7_power_cfg(data);
  1421. release_mem:
  1422. kfree(cfg.mem);
  1423. release_raw:
  1424. kfree(cfg.raw);
  1425. return ret;
  1426. }
  1427. static void mxt_free_input_device(struct mxt_data *data)
  1428. {
  1429. if (data->input_dev) {
  1430. input_unregister_device(data->input_dev);
  1431. data->input_dev = NULL;
  1432. }
  1433. }
  1434. static void mxt_free_object_table(struct mxt_data *data)
  1435. {
  1436. #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
  1437. video_unregister_device(&data->dbg.vdev);
  1438. v4l2_device_unregister(&data->dbg.v4l2);
  1439. #endif
  1440. data->object_table = NULL;
  1441. data->info = NULL;
  1442. kfree(data->raw_info_block);
  1443. data->raw_info_block = NULL;
  1444. kfree(data->msg_buf);
  1445. data->msg_buf = NULL;
  1446. data->T5_address = 0;
  1447. data->T5_msg_size = 0;
  1448. data->T6_reportid = 0;
  1449. data->T7_address = 0;
  1450. data->T71_address = 0;
  1451. data->T9_reportid_min = 0;
  1452. data->T9_reportid_max = 0;
  1453. data->T18_address = 0;
  1454. data->T19_reportid = 0;
  1455. data->T44_address = 0;
  1456. data->T100_reportid_min = 0;
  1457. data->T100_reportid_max = 0;
  1458. data->max_reportid = 0;
  1459. }
  1460. static int mxt_parse_object_table(struct mxt_data *data,
  1461. struct mxt_object *object_table)
  1462. {
  1463. struct i2c_client *client = data->client;
  1464. int i;
  1465. u8 reportid;
  1466. u16 end_address;
  1467. /* Valid Report IDs start counting from 1 */
  1468. reportid = 1;
  1469. data->mem_size = 0;
  1470. for (i = 0; i < data->info->object_num; i++) {
  1471. struct mxt_object *object = object_table + i;
  1472. u8 min_id, max_id;
  1473. le16_to_cpus(&object->start_address);
  1474. if (object->num_report_ids) {
  1475. min_id = reportid;
  1476. reportid += object->num_report_ids *
  1477. mxt_obj_instances(object);
  1478. max_id = reportid - 1;
  1479. } else {
  1480. min_id = 0;
  1481. max_id = 0;
  1482. }
  1483. dev_dbg(&data->client->dev,
  1484. "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
  1485. object->type, object->start_address,
  1486. mxt_obj_size(object), mxt_obj_instances(object),
  1487. min_id, max_id);
  1488. switch (object->type) {
  1489. case MXT_GEN_MESSAGE_T5:
  1490. if (data->info->family_id == 0x80 &&
  1491. data->info->version < 0x20) {
  1492. /*
  1493. * On mXT224 firmware versions prior to V2.0
  1494. * read and discard unused CRC byte otherwise
  1495. * DMA reads are misaligned.
  1496. */
  1497. data->T5_msg_size = mxt_obj_size(object);
  1498. } else {
  1499. /* CRC not enabled, so skip last byte */
  1500. data->T5_msg_size = mxt_obj_size(object) - 1;
  1501. }
  1502. data->T5_address = object->start_address;
  1503. break;
  1504. case MXT_GEN_COMMAND_T6:
  1505. data->T6_reportid = min_id;
  1506. data->T6_address = object->start_address;
  1507. break;
  1508. case MXT_GEN_POWER_T7:
  1509. data->T7_address = object->start_address;
  1510. break;
  1511. case MXT_SPT_DYNAMICCONFIGURATIONCONTAINER_T71:
  1512. data->T71_address = object->start_address;
  1513. break;
  1514. case MXT_TOUCH_MULTI_T9:
  1515. data->multitouch = MXT_TOUCH_MULTI_T9;
  1516. /* Only handle messages from first T9 instance */
  1517. data->T9_reportid_min = min_id;
  1518. data->T9_reportid_max = min_id +
  1519. object->num_report_ids - 1;
  1520. data->num_touchids = object->num_report_ids;
  1521. break;
  1522. case MXT_SPT_COMMSCONFIG_T18:
  1523. data->T18_address = object->start_address;
  1524. break;
  1525. case MXT_SPT_MESSAGECOUNT_T44:
  1526. data->T44_address = object->start_address;
  1527. break;
  1528. case MXT_SPT_GPIOPWM_T19:
  1529. data->T19_reportid = min_id;
  1530. break;
  1531. case MXT_TOUCH_MULTITOUCHSCREEN_T100:
  1532. data->multitouch = MXT_TOUCH_MULTITOUCHSCREEN_T100;
  1533. data->T100_reportid_min = min_id;
  1534. data->T100_reportid_max = max_id;
  1535. /* first two report IDs reserved */
  1536. data->num_touchids = object->num_report_ids - 2;
  1537. break;
  1538. }
  1539. end_address = object->start_address
  1540. + mxt_obj_size(object) * mxt_obj_instances(object) - 1;
  1541. if (end_address >= data->mem_size)
  1542. data->mem_size = end_address + 1;
  1543. }
  1544. /* Store maximum reportid */
  1545. data->max_reportid = reportid;
  1546. /* If T44 exists, T5 position has to be directly after */
  1547. if (data->T44_address && (data->T5_address != data->T44_address + 1)) {
  1548. dev_err(&client->dev, "Invalid T44 position\n");
  1549. return -EINVAL;
  1550. }
  1551. data->msg_buf = kcalloc(data->max_reportid,
  1552. data->T5_msg_size, GFP_KERNEL);
  1553. if (!data->msg_buf)
  1554. return -ENOMEM;
  1555. return 0;
  1556. }
  1557. static int mxt_read_info_block(struct mxt_data *data)
  1558. {
  1559. struct i2c_client *client = data->client;
  1560. int error;
  1561. size_t size;
  1562. void *id_buf, *buf;
  1563. uint8_t num_objects;
  1564. u32 calculated_crc;
  1565. u8 *crc_ptr;
  1566. /* If info block already allocated, free it */
  1567. if (data->raw_info_block)
  1568. mxt_free_object_table(data);
  1569. /* Read 7-byte ID information block starting at address 0 */
  1570. size = sizeof(struct mxt_info);
  1571. id_buf = kzalloc(size, GFP_KERNEL);
  1572. if (!id_buf)
  1573. return -ENOMEM;
  1574. error = __mxt_read_reg(data, 0, size, id_buf);
  1575. if (error)
  1576. goto err_free_mem;
  1577. /* Resize buffer to give space for rest of info block */
  1578. num_objects = ((struct mxt_info *)id_buf)->object_num;
  1579. size += (num_objects * sizeof(struct mxt_object))
  1580. + MXT_INFO_CHECKSUM_SIZE;
  1581. buf = krealloc(id_buf, size, GFP_KERNEL);
  1582. if (!buf) {
  1583. error = -ENOMEM;
  1584. goto err_free_mem;
  1585. }
  1586. id_buf = buf;
  1587. /* Read rest of info block */
  1588. error = __mxt_read_reg(data, MXT_OBJECT_START,
  1589. size - MXT_OBJECT_START,
  1590. id_buf + MXT_OBJECT_START);
  1591. if (error)
  1592. goto err_free_mem;
  1593. /* Extract & calculate checksum */
  1594. crc_ptr = id_buf + size - MXT_INFO_CHECKSUM_SIZE;
  1595. data->info_crc = crc_ptr[0] | (crc_ptr[1] << 8) | (crc_ptr[2] << 16);
  1596. calculated_crc = mxt_calculate_crc(id_buf, 0,
  1597. size - MXT_INFO_CHECKSUM_SIZE);
  1598. /*
  1599. * CRC mismatch can be caused by data corruption due to I2C comms
  1600. * issue or else device is not using Object Based Protocol (eg i2c-hid)
  1601. */
  1602. if ((data->info_crc == 0) || (data->info_crc != calculated_crc)) {
  1603. dev_err(&client->dev,
  1604. "Info Block CRC error calculated=0x%06X read=0x%06X\n",
  1605. calculated_crc, data->info_crc);
  1606. error = -EIO;
  1607. goto err_free_mem;
  1608. }
  1609. data->raw_info_block = id_buf;
  1610. data->info = (struct mxt_info *)id_buf;
  1611. dev_info(&client->dev,
  1612. "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n",
  1613. data->info->family_id, data->info->variant_id,
  1614. data->info->version >> 4, data->info->version & 0xf,
  1615. data->info->build, data->info->object_num);
  1616. /* Parse object table information */
  1617. error = mxt_parse_object_table(data, id_buf + MXT_OBJECT_START);
  1618. if (error) {
  1619. dev_err(&client->dev, "Error %d parsing object table\n", error);
  1620. mxt_free_object_table(data);
  1621. goto err_free_mem;
  1622. }
  1623. data->object_table = (struct mxt_object *)(id_buf + MXT_OBJECT_START);
  1624. return 0;
  1625. err_free_mem:
  1626. kfree(id_buf);
  1627. return error;
  1628. }
  1629. static int mxt_pinctrl_init(struct mxt_data *data)
  1630. {
  1631. int error;
  1632. /* Get pinctrl if target uses pinctrl */
  1633. data->ts_pinctrl = devm_pinctrl_get((&data->client->dev));
  1634. if (IS_ERR_OR_NULL(data->ts_pinctrl)) {
  1635. dev_dbg(&data->client->dev,
  1636. "Device does not use pinctrl\n");
  1637. error = PTR_ERR(data->ts_pinctrl);
  1638. data->ts_pinctrl = NULL;
  1639. return error;
  1640. }
  1641. data->gpio_state_active
  1642. = pinctrl_lookup_state(data->ts_pinctrl, "pmx_ts_active");
  1643. if (IS_ERR_OR_NULL(data->gpio_state_active)) {
  1644. dev_dbg(&data->client->dev,
  1645. "Can not get ts default pinstate\n");
  1646. error = PTR_ERR(data->gpio_state_active);
  1647. data->ts_pinctrl = NULL;
  1648. return error;
  1649. }
  1650. data->gpio_state_suspend
  1651. = pinctrl_lookup_state(data->ts_pinctrl, "pmx_ts_suspend");
  1652. if (IS_ERR_OR_NULL(data->gpio_state_suspend)) {
  1653. dev_dbg(&data->client->dev,
  1654. "Can not get ts sleep pinstate\n");
  1655. error = PTR_ERR(data->gpio_state_suspend);
  1656. data->ts_pinctrl = NULL;
  1657. return error;
  1658. }
  1659. return 0;
  1660. }
  1661. static int mxt_pinctrl_select(struct mxt_data *data, bool on)
  1662. {
  1663. struct pinctrl_state *pins_state;
  1664. int error;
  1665. pins_state = on ? data->gpio_state_active
  1666. : data->gpio_state_suspend;
  1667. if (!IS_ERR_OR_NULL(pins_state)) {
  1668. error = pinctrl_select_state(data->ts_pinctrl, pins_state);
  1669. if (error) {
  1670. dev_err(&data->client->dev,
  1671. "can not set %s pins\n",
  1672. on ? "pmx_ts_active" : "pmx_ts_suspend");
  1673. return error;
  1674. }
  1675. } else {
  1676. dev_err(&data->client->dev,
  1677. "not a valid '%s' pinstate\n",
  1678. on ? "pmx_ts_active" : "pmx_ts_suspend");
  1679. }
  1680. return 0;
  1681. }
  1682. static int mxt_gpio_enable(struct mxt_data *data, bool enable)
  1683. {
  1684. int error;
  1685. if (data->ts_pinctrl) {
  1686. error = mxt_pinctrl_select(data, enable);
  1687. if (error < 0)
  1688. return error;
  1689. }
  1690. if (enable) {
  1691. error = gpiod_direction_input(data->irq_gpio);
  1692. if (error)
  1693. dev_err(&data->client->dev,
  1694. "unable to set dir for %d gpio(%d)\n",
  1695. desc_to_gpio(data->irq_gpio), error);
  1696. if (data->reset_gpio)
  1697. gpiod_set_value(data->reset_gpio, 1);
  1698. } else {
  1699. if (data->reset_gpio)
  1700. gpiod_set_value(data->reset_gpio, 0);
  1701. }
  1702. return 0;
  1703. }
  1704. static int mxt_regulator_enable(struct mxt_data *data)
  1705. {
  1706. int error;
  1707. if (!data->use_regulator)
  1708. return 0;
  1709. gpiod_set_value(data->reset_gpio, 0);
  1710. error = mxt_regulator_configure(data, true);
  1711. if (error) {
  1712. dev_err(&data->client->dev, "Failed to configure regulators on\n");
  1713. return error;
  1714. }
  1715. error = regulator_enable(data->reg_avdd);
  1716. if (error) {
  1717. dev_err(&data->client->dev,
  1718. "avdd enable failed, error=%d\n", error);
  1719. goto err_dis_configure;
  1720. }
  1721. usleep_range(10000, 15000);
  1722. error = regulator_enable(data->reg_vdd);
  1723. if (error) {
  1724. dev_err(&data->client->dev,
  1725. "vdd enable failed, error=%d\n", error);
  1726. goto err_dis_avdd;
  1727. }
  1728. if (!IS_ERR(data->reg_xvdd)) {
  1729. usleep_range(10000, 15000);
  1730. error = regulator_enable(data->reg_xvdd);
  1731. if (error) {
  1732. dev_err(&data->client->dev,
  1733. "xvdd enable failed, error=%d\n", error);
  1734. goto err_dis_vdd;
  1735. }
  1736. }
  1737. msleep(MXT_REGULATOR_DELAY);
  1738. reinit_completion(&data->bl_completion);
  1739. gpiod_set_value(data->reset_gpio, 1);
  1740. mxt_wait_for_completion(data, &data->bl_completion, MXT_POWERON_DELAY);
  1741. return 0;
  1742. err_dis_vdd:
  1743. regulator_disable(data->reg_vdd);
  1744. err_dis_avdd:
  1745. regulator_disable(data->reg_avdd);
  1746. err_dis_configure:
  1747. mxt_regulator_configure(data, false);
  1748. return error;
  1749. }
  1750. static void mxt_regulator_disable(struct mxt_data *data)
  1751. {
  1752. int error;
  1753. regulator_disable(data->reg_vdd);
  1754. regulator_disable(data->reg_avdd);
  1755. if (!IS_ERR(data->reg_xvdd))
  1756. regulator_disable(data->reg_xvdd);
  1757. error = mxt_regulator_configure(data, false);
  1758. if (error)
  1759. dev_err(&data->client->dev, "Failed to configure regulators off\n");
  1760. }
  1761. static int mxt_regulator_parse(struct mxt_data *data)
  1762. {
  1763. struct device *dev = &data->client->dev;
  1764. struct device_node *np = dev->of_node;
  1765. struct property *prop;
  1766. int error = 0;
  1767. if (!data->reset_gpio) {
  1768. dev_warn(dev, "Must have reset GPIO to use regulator support\n");
  1769. return 0;
  1770. }
  1771. data->reg_vdd = regulator_get(dev, "vdd");
  1772. if (IS_ERR(data->reg_vdd)) {
  1773. error = PTR_ERR(data->reg_vdd);
  1774. dev_err(dev, "Error %d getting vdd regulator\n", error);
  1775. return error;
  1776. }
  1777. data->reg_avdd = regulator_get(dev, "avdd");
  1778. if (IS_ERR(data->reg_avdd)) {
  1779. error = PTR_ERR(data->reg_avdd);
  1780. dev_err(dev, "Error %d getting avdd regulator\n", error);
  1781. goto fail_put_vdd;
  1782. }
  1783. data->reg_xvdd = regulator_get(dev, "xvdd");
  1784. if (IS_ERR(data->reg_xvdd)) {
  1785. error = PTR_ERR(data->reg_xvdd);
  1786. prop = of_find_property(np, "xvdd-supply", NULL);
  1787. if (prop && (error == -EPROBE_DEFER))
  1788. return -EPROBE_DEFER;
  1789. dev_dbg(dev, "xvdd regulator is not used\n");
  1790. }
  1791. data->use_regulator = true;
  1792. dev_err(dev, "Initialised regulators\n");
  1793. return 0;
  1794. fail_put_vdd:
  1795. regulator_put(data->reg_vdd);
  1796. return error;
  1797. }
  1798. static int mxt_regulator_configure(struct mxt_data *data, bool enable)
  1799. {
  1800. struct device *dev = &data->client->dev;
  1801. int error = 0;
  1802. /* According to maXTouch power sequencing specification, RESET line
  1803. * must be kept low until some time after regulators come up to
  1804. * voltage
  1805. */
  1806. if (!data->reset_gpio) {
  1807. dev_warn(dev, "Must have reset GPIO to use regulator support\n");
  1808. return 0;
  1809. }
  1810. if (!enable) {
  1811. if (regulator_count_voltages(data->reg_avdd) > 0) {
  1812. error = regulator_set_load(data->reg_avdd, MXT_DISABLE_LOAD);
  1813. if (error) {
  1814. dev_err(&data->client->dev,
  1815. "avdd set_load failed err=%d\n", error);
  1816. return error;
  1817. }
  1818. }
  1819. if (regulator_count_voltages(data->reg_vdd) > 0) {
  1820. error = regulator_set_load(data->reg_vdd, MXT_DISABLE_LOAD);
  1821. if (error) {
  1822. dev_err(&data->client->dev,
  1823. "vdd set_load failed err=%d\n", error);
  1824. return error;
  1825. }
  1826. }
  1827. if (!IS_ERR(data->reg_xvdd)) {
  1828. if (regulator_count_voltages(data->reg_xvdd) > 0) {
  1829. error = regulator_set_load(data->reg_xvdd, MXT_DISABLE_LOAD);
  1830. if (error)
  1831. dev_err(&data->client->dev,
  1832. "xvdd set_load failed err=%d\n", error);
  1833. }
  1834. }
  1835. } else {
  1836. if (regulator_count_voltages(data->reg_vdd) > 0) {
  1837. error = regulator_set_load(data->reg_vdd, MXT_ENABLE_LOAD);
  1838. if (error) {
  1839. dev_err(&data->client->dev,
  1840. "vdd set_load failed err=%d\n", error);
  1841. return error;
  1842. }
  1843. error = regulator_set_voltage(data->reg_vdd,
  1844. MXT_VDD_VTG_MIN_UV, MXT_VDD_VTG_MAX_UV);
  1845. if (error) {
  1846. dev_err(&data->client->dev,
  1847. "vdd set_vtg failed err=%d\n", error);
  1848. goto err_vdd_load;
  1849. }
  1850. }
  1851. if (regulator_count_voltages(data->reg_avdd) > 0) {
  1852. error = regulator_set_load(data->reg_avdd, MXT_ENABLE_LOAD);
  1853. if (error) {
  1854. dev_err(&data->client->dev,
  1855. "avdd set_load failed err=%d\n", error);
  1856. goto err_vdd_load;
  1857. }
  1858. error = regulator_set_voltage(data->reg_avdd,
  1859. MXT_AVDD_VTG_MIN_UV, MXT_AVDD_VTG_MAX_UV);
  1860. if (error) {
  1861. dev_err(&data->client->dev,
  1862. "avdd set_vtg failed err=%d\n", error);
  1863. goto err_avdd_load;
  1864. }
  1865. }
  1866. if (!IS_ERR(data->reg_xvdd)) {
  1867. if (regulator_count_voltages(data->reg_xvdd) > 0) {
  1868. error = regulator_set_load(data->reg_xvdd, MXT_ENABLE_LOAD);
  1869. if (error) {
  1870. dev_err(&data->client->dev,
  1871. "xvdd set_load failed err=%d\n", error);
  1872. goto err_avdd_load;
  1873. }
  1874. error = regulator_set_voltage(data->reg_xvdd,
  1875. MXT_XVDD_VTG_MIN_UV, MXT_XVDD_VTG_MAX_UV);
  1876. if (error) {
  1877. dev_err(&data->client->dev,
  1878. "xvdd set_vtg failed err=%d\n", error);
  1879. goto err_xvdd_load;
  1880. }
  1881. }
  1882. }
  1883. }
  1884. return error;
  1885. err_xvdd_load:
  1886. regulator_set_load(data->reg_xvdd, MXT_DISABLE_LOAD);
  1887. err_avdd_load:
  1888. regulator_set_load(data->reg_avdd, MXT_DISABLE_LOAD);
  1889. err_vdd_load:
  1890. regulator_set_load(data->reg_vdd, MXT_DISABLE_LOAD);
  1891. return error;
  1892. }
  1893. static int mxt_read_t9_resolution(struct mxt_data *data)
  1894. {
  1895. int error;
  1896. unsigned char orient;
  1897. struct mxt_object *object;
  1898. object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
  1899. if (!object)
  1900. return -EINVAL;
  1901. error = __mxt_read_reg(data,
  1902. object->start_address + MXT_T9_XSIZE,
  1903. sizeof(data->xsize), &data->xsize);
  1904. if (error)
  1905. return error;
  1906. error = __mxt_read_reg(data,
  1907. object->start_address + MXT_T9_YSIZE,
  1908. sizeof(data->ysize), &data->ysize);
  1909. if (error)
  1910. return error;
  1911. error = __mxt_read_reg(data,
  1912. object->start_address + MXT_T9_RANGE,
  1913. sizeof(struct t9_range), data->read_buf);
  1914. if (error)
  1915. return error;
  1916. data->max_x = get_unaligned_le16(data->read_buf);
  1917. data->max_y = get_unaligned_le16(data->read_buf + sizeof(__le16));
  1918. error = __mxt_read_reg(data,
  1919. object->start_address + MXT_T9_ORIENT,
  1920. 1, data->read_buf);
  1921. if (error)
  1922. return error;
  1923. orient = *(data->read_buf);
  1924. if (data->xy_switch != (orient & MXT_T9_ORIENT_SWITCH))
  1925. dev_warn(&data->client->dev,
  1926. "T9 data->xy_switch:%d\n",
  1927. data->xy_switch);
  1928. if (data->invertx != (orient & MXT_T9_ORIENT_INVERTX))
  1929. dev_warn(&data->client->dev,
  1930. "T9 data->data->invertx:%d\n",
  1931. data->invertx);
  1932. if (data->inverty != (orient & MXT_T9_ORIENT_INVERTY))
  1933. dev_warn(&data->client->dev,
  1934. "T9 data->data->inverty:%d\n",
  1935. data->inverty);
  1936. return 0;
  1937. }
  1938. static int mxt_read_t100_config(struct mxt_data *data)
  1939. {
  1940. int error;
  1941. struct mxt_object *object;
  1942. u8 cfg, tchaux;
  1943. u8 aux;
  1944. object = mxt_get_object(data, MXT_TOUCH_MULTITOUCHSCREEN_T100);
  1945. if (!object)
  1946. return -EINVAL;
  1947. /* read touchscreen dimensions */
  1948. error = __mxt_read_reg(data,
  1949. object->start_address + MXT_T100_XRANGE,
  1950. sizeof(__le16), data->read_buf);
  1951. if (error)
  1952. return error;
  1953. data->max_x = get_unaligned_le16(data->read_buf);
  1954. error = __mxt_read_reg(data,
  1955. object->start_address + MXT_T100_YRANGE,
  1956. sizeof(__le16), data->read_buf);
  1957. if (error)
  1958. return error;
  1959. data->max_y = get_unaligned_le16(data->read_buf);
  1960. error = __mxt_read_reg(data,
  1961. object->start_address + MXT_T100_XSIZE,
  1962. sizeof(data->xsize), &data->xsize);
  1963. if (error)
  1964. return error;
  1965. error = __mxt_read_reg(data,
  1966. object->start_address + MXT_T100_YSIZE,
  1967. sizeof(data->ysize), &data->ysize);
  1968. if (error)
  1969. return error;
  1970. /* read orientation config */
  1971. error = __mxt_read_reg(data,
  1972. object->start_address + MXT_T100_CFG1,
  1973. 1, data->read_buf);
  1974. if (error)
  1975. return error;
  1976. cfg = *(data->read_buf);
  1977. if (data->xy_switch != (cfg & MXT_T100_CFG_SWITCHXY))
  1978. dev_warn(&data->client->dev,
  1979. "T100 data->xy_switch:%d\n",
  1980. data->xy_switch);
  1981. if (data->invertx != (cfg & MXT_T100_CFG_INVERTX))
  1982. dev_warn(&data->client->dev,
  1983. "T100 data->data->invertx:%d\n",
  1984. data->invertx);
  1985. if (data->inverty != (cfg & MXT_T100_CFG_INVERTY))
  1986. dev_warn(&data->client->dev,
  1987. "T100 data->data->inverty:%d\n",
  1988. data->inverty);
  1989. /* allocate aux bytes */
  1990. error = __mxt_read_reg(data,
  1991. object->start_address + MXT_T100_TCHAUX,
  1992. 1, data->read_buf);
  1993. if (error)
  1994. return error;
  1995. tchaux = *(data->read_buf);
  1996. aux = 6;
  1997. if (tchaux & MXT_T100_TCHAUX_VECT)
  1998. data->t100_aux_vect = aux++;
  1999. if (tchaux & MXT_T100_TCHAUX_AMPL)
  2000. data->t100_aux_ampl = aux++;
  2001. if (tchaux & MXT_T100_TCHAUX_AREA)
  2002. data->t100_aux_area = aux++;
  2003. dev_dbg(&data->client->dev,
  2004. "T100 aux mappings vect:%u ampl:%u area:%u\n",
  2005. data->t100_aux_vect, data->t100_aux_ampl, data->t100_aux_area);
  2006. return 0;
  2007. }
  2008. static int mxt_input_open(struct input_dev *dev);
  2009. static void mxt_input_close(struct input_dev *dev);
  2010. static void mxt_set_up_as_touchpad(struct input_dev *input_dev,
  2011. struct mxt_data *data)
  2012. {
  2013. int i;
  2014. input_dev->name = "Atmel maXTouch Touchpad";
  2015. __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
  2016. input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
  2017. input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
  2018. input_abs_set_res(input_dev, ABS_MT_POSITION_X,
  2019. MXT_PIXELS_PER_MM);
  2020. input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
  2021. MXT_PIXELS_PER_MM);
  2022. for (i = 0; i < data->t19_num_keys; i++)
  2023. if (data->t19_keymap[i] != KEY_RESERVED)
  2024. input_set_capability(input_dev, EV_KEY,
  2025. data->t19_keymap[i]);
  2026. }
  2027. static int mxt_initialize_input_device(struct mxt_data *data)
  2028. {
  2029. struct device *dev = &data->client->dev;
  2030. struct input_dev *input_dev;
  2031. int error;
  2032. unsigned int num_mt_slots;
  2033. unsigned int mt_flags = 0;
  2034. switch (data->multitouch) {
  2035. case MXT_TOUCH_MULTI_T9:
  2036. num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
  2037. error = mxt_read_t9_resolution(data);
  2038. if (error)
  2039. dev_warn(dev, "Failed to initialize T9 resolution\n");
  2040. break;
  2041. case MXT_TOUCH_MULTITOUCHSCREEN_T100:
  2042. num_mt_slots = data->num_touchids;
  2043. error = mxt_read_t100_config(data);
  2044. if (error)
  2045. dev_warn(dev, "Failed to read T100 config\n");
  2046. break;
  2047. default:
  2048. dev_err(dev, "Invalid multitouch object\n");
  2049. return -EINVAL;
  2050. }
  2051. /* Handle default values and orientation switch */
  2052. if (data->max_x == 0)
  2053. data->max_x = 1023;
  2054. if (data->max_y == 0)
  2055. data->max_y = 1023;
  2056. if (data->xy_switch)
  2057. swap(data->max_x, data->max_y);
  2058. dev_info(dev, "Touchscreen size X%uY%u\n", data->max_x, data->max_y);
  2059. /* Register input device */
  2060. input_dev = input_allocate_device();
  2061. if (!input_dev)
  2062. return -ENOMEM;
  2063. input_dev->name = "Atmel maXTouch Touchscreen";
  2064. input_dev->phys = data->phys;
  2065. input_dev->id.bustype = BUS_I2C;
  2066. input_dev->dev.parent = dev;
  2067. input_dev->open = mxt_input_open;
  2068. input_dev->close = mxt_input_close;
  2069. input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
  2070. if (data->disp_maxx == 0 || data->disp_maxx > data->max_x)
  2071. data->disp_maxx = data->max_x;
  2072. if (data->disp_maxy == 0 || data->disp_maxy > data->max_y)
  2073. data->disp_maxy = data->max_y;
  2074. if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
  2075. (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
  2076. data->t100_aux_ampl)) {
  2077. input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0);
  2078. }
  2079. /* If device has buttons we assume it is a touchpad */
  2080. if (data->t19_num_keys) {
  2081. mxt_set_up_as_touchpad(input_dev, data);
  2082. mt_flags |= INPUT_MT_POINTER;
  2083. } else {
  2084. mt_flags |= INPUT_MT_DIRECT;
  2085. }
  2086. /* For multi touch */
  2087. error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
  2088. if (error) {
  2089. dev_err(dev, "Error %d initialising slots\n", error);
  2090. goto err_free_mem;
  2091. }
  2092. if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100) {
  2093. input_set_abs_params(input_dev, ABS_MT_TOOL_TYPE,
  2094. 0, MT_TOOL_MAX, 0, 0);
  2095. input_set_abs_params(input_dev, ABS_MT_DISTANCE,
  2096. MXT_DISTANCE_ACTIVE_TOUCH,
  2097. MXT_DISTANCE_HOVERING,
  2098. 0, 0);
  2099. }
  2100. input_set_abs_params(input_dev, ABS_MT_POSITION_X,
  2101. 0, data->disp_maxx - data->disp_minx, 0, 0);
  2102. input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
  2103. 0, data->disp_maxy - data->disp_miny, 0, 0);
  2104. if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
  2105. (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
  2106. data->t100_aux_area)) {
  2107. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
  2108. 0, MXT_MAX_AREA, 0, 0);
  2109. }
  2110. if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
  2111. (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
  2112. data->t100_aux_ampl)) {
  2113. input_set_abs_params(input_dev, ABS_MT_PRESSURE,
  2114. 0, 255, 0, 0);
  2115. }
  2116. if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
  2117. data->t100_aux_vect) {
  2118. input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
  2119. 0, 255, 0, 0);
  2120. }
  2121. if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
  2122. data->t100_aux_vect) {
  2123. input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
  2124. 0, 255, 0, 0);
  2125. }
  2126. input_set_drvdata(input_dev, data);
  2127. error = input_register_device(input_dev);
  2128. if (error) {
  2129. dev_err(dev, "Error %d registering input device\n", error);
  2130. goto err_free_mem;
  2131. }
  2132. data->input_dev = input_dev;
  2133. return 0;
  2134. err_free_mem:
  2135. input_free_device(input_dev);
  2136. return error;
  2137. }
  2138. static int mxt_configure_objects(struct mxt_data *data,
  2139. const struct firmware *cfg);
  2140. static void mxt_config_cb(const struct firmware *cfg, void *ctx)
  2141. {
  2142. mxt_configure_objects(ctx, cfg);
  2143. release_firmware(cfg);
  2144. }
  2145. static int mxt_initialize(struct mxt_data *data)
  2146. {
  2147. struct i2c_client *client = data->client;
  2148. int recovery_attempts = 0;
  2149. int error;
  2150. while (1) {
  2151. error = mxt_read_info_block(data);
  2152. if (!error)
  2153. break;
  2154. /* Check bootloader state */
  2155. error = mxt_probe_bootloader(data, false);
  2156. if (error) {
  2157. dev_info(&client->dev, "Trying alternate bootloader address\n");
  2158. mxt_soft_reset(data);
  2159. error = mxt_probe_bootloader(data, true);
  2160. if (error) {
  2161. /* Chip is not in appmode or bootloader mode */
  2162. dev_err(&client->dev, "Fail probe boot, continue\n");
  2163. }
  2164. }
  2165. /* OK, we are in bootloader, see if we can recover */
  2166. if (++recovery_attempts > 10) {
  2167. dev_err(&client->dev, "Could not recover from bootloader mode\n");
  2168. /*
  2169. * We can reflash from this state, so do not
  2170. * abort initialization.
  2171. */
  2172. data->in_bootloader = true;
  2173. return 0;
  2174. }
  2175. /* Attempt to exit bootloader into app mode */
  2176. mxt_send_bootloader_cmd(data, false);
  2177. msleep(MXT_FW_RESET_TIME);
  2178. }
  2179. error = mxt_check_retrigen(data);
  2180. if (error)
  2181. return error;
  2182. error = mxt_acquire_irq(data);
  2183. if (error)
  2184. return error;
  2185. error = request_firmware_nowait(THIS_MODULE, true, MXT_CFG_NAME,
  2186. &client->dev, GFP_KERNEL, data,
  2187. mxt_config_cb);
  2188. if (error) {
  2189. dev_err(&client->dev, "Failed to invoke firmware loader: %d\n",
  2190. error);
  2191. return error;
  2192. }
  2193. return 0;
  2194. }
  2195. static int mxt_set_t7_power_cfg(struct mxt_data *data, u8 sleep)
  2196. {
  2197. struct device *dev = &data->client->dev;
  2198. int error;
  2199. struct t7_config *new_config;
  2200. struct t7_config deepsleep = { .active = 0, .idle = 0 };
  2201. if (sleep == MXT_POWER_CFG_DEEPSLEEP)
  2202. new_config = &deepsleep;
  2203. else
  2204. new_config = &data->t7_cfg;
  2205. error = __mxt_write_reg(data->client, data->T7_address,
  2206. sizeof(data->t7_cfg), new_config);
  2207. if (error)
  2208. return error;
  2209. dev_dbg(dev, "Set T7 ACTV:%d IDLE:%d\n",
  2210. new_config->active, new_config->idle);
  2211. return 0;
  2212. }
  2213. static int mxt_init_t7_power_cfg(struct mxt_data *data)
  2214. {
  2215. struct device *dev = &data->client->dev;
  2216. int error;
  2217. bool retry = false;
  2218. recheck:
  2219. error = __mxt_read_reg(data, data->T7_address,
  2220. sizeof(data->t7_cfg), &data->t7_cfg);
  2221. if (error)
  2222. return error;
  2223. if (data->t7_cfg.active == 0 || data->t7_cfg.idle == 0) {
  2224. if (!retry) {
  2225. dev_dbg(dev, "T7 cfg zero, resetting\n");
  2226. mxt_soft_reset(data);
  2227. retry = true;
  2228. goto recheck;
  2229. } else {
  2230. dev_dbg(dev, "T7 cfg zero after reset, overriding\n");
  2231. data->t7_cfg.active = 20;
  2232. data->t7_cfg.idle = 100;
  2233. return mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
  2234. }
  2235. }
  2236. dev_dbg(dev, "Initialized power cfg: ACTV %d, IDLE %d\n",
  2237. data->t7_cfg.active, data->t7_cfg.idle);
  2238. return 0;
  2239. }
  2240. #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
  2241. static const struct v4l2_file_operations mxt_video_fops = {
  2242. .owner = THIS_MODULE,
  2243. .open = v4l2_fh_open,
  2244. .release = vb2_fop_release,
  2245. .unlocked_ioctl = video_ioctl2,
  2246. .read = vb2_fop_read,
  2247. .mmap = vb2_fop_mmap,
  2248. .poll = vb2_fop_poll,
  2249. };
  2250. static u16 mxt_get_debug_value(struct mxt_data *data, unsigned int x,
  2251. unsigned int y)
  2252. {
  2253. struct mxt_info *info = data->info;
  2254. struct mxt_dbg *dbg = &data->dbg;
  2255. unsigned int ofs, page;
  2256. unsigned int col = 0;
  2257. unsigned int col_width;
  2258. if (info->family_id == MXT_FAMILY_1386) {
  2259. col_width = info->matrix_ysize / MXT1386_COLUMNS;
  2260. col = y / col_width;
  2261. y = y % col_width;
  2262. } else {
  2263. col_width = info->matrix_ysize;
  2264. }
  2265. ofs = (y + (x * col_width)) * sizeof(u16);
  2266. page = ofs / MXT_DIAGNOSTIC_SIZE;
  2267. ofs %= MXT_DIAGNOSTIC_SIZE;
  2268. if (info->family_id == MXT_FAMILY_1386)
  2269. page += col * MXT1386_PAGES_PER_COLUMN;
  2270. return get_unaligned_le16(&dbg->t37_buf[page].data[ofs]);
  2271. }
  2272. static int mxt_convert_debug_pages(struct mxt_data *data, u16 *outbuf)
  2273. {
  2274. struct mxt_dbg *dbg = &data->dbg;
  2275. unsigned int x = 0;
  2276. unsigned int y = 0;
  2277. unsigned int i, rx, ry;
  2278. for (i = 0; i < dbg->t37_nodes; i++) {
  2279. /* Handle orientation */
  2280. rx = data->xy_switch ? y : x;
  2281. ry = data->xy_switch ? x : y;
  2282. rx = data->invertx ? (data->xsize - 1 - rx) : rx;
  2283. ry = data->inverty ? (data->ysize - 1 - ry) : ry;
  2284. outbuf[i] = mxt_get_debug_value(data, rx, ry);
  2285. /* Next value */
  2286. if (++x >= (data->xy_switch ? data->ysize : data->xsize)) {
  2287. x = 0;
  2288. y++;
  2289. }
  2290. }
  2291. return 0;
  2292. }
  2293. static int mxt_read_diagnostic_debug(struct mxt_data *data, u8 mode,
  2294. u16 *outbuf)
  2295. {
  2296. struct mxt_dbg *dbg = &data->dbg;
  2297. int retries = 0;
  2298. int page;
  2299. int ret;
  2300. u8 cmd = mode;
  2301. struct t37_debug *p;
  2302. u8 cmd_poll;
  2303. for (page = 0; page < dbg->t37_pages; page++) {
  2304. p = dbg->t37_buf + page;
  2305. ret = mxt_write_reg(data->client, dbg->diag_cmd_address,
  2306. cmd);
  2307. if (ret)
  2308. return ret;
  2309. retries = 0;
  2310. msleep(20);
  2311. wait_cmd:
  2312. /* Read back command byte */
  2313. ret = __mxt_read_reg(data, dbg->diag_cmd_address,
  2314. sizeof(cmd_poll), data->read_buf);
  2315. if (ret)
  2316. return ret;
  2317. cmd_poll = *(data->read_buf);
  2318. /* Field is cleared once the command has been processed */
  2319. if (cmd_poll) {
  2320. if (retries++ > 100)
  2321. return -EINVAL;
  2322. msleep(20);
  2323. goto wait_cmd;
  2324. }
  2325. /* Read T37 page */
  2326. ret = __mxt_read_reg(data, dbg->t37_address,
  2327. sizeof(struct t37_debug), p);
  2328. if (ret)
  2329. return ret;
  2330. if (p->mode != mode || p->page != page) {
  2331. dev_err(&data->client->dev, "T37 page mismatch\n");
  2332. return -EINVAL;
  2333. }
  2334. dev_dbg(&data->client->dev, "%s page:%d retries:%d\n",
  2335. __func__, page, retries);
  2336. /* For remaining pages, write PAGEUP rather than mode */
  2337. cmd = MXT_DIAGNOSTIC_PAGEUP;
  2338. }
  2339. return mxt_convert_debug_pages(data, outbuf);
  2340. }
  2341. static int mxt_queue_setup(struct vb2_queue *q,
  2342. unsigned int *nbuffers, unsigned int *nplanes,
  2343. unsigned int sizes[], struct device *alloc_devs[])
  2344. {
  2345. struct mxt_data *data = q->drv_priv;
  2346. size_t size = data->dbg.t37_nodes * sizeof(u16);
  2347. if (*nplanes)
  2348. return sizes[0] < size ? -EINVAL : 0;
  2349. *nplanes = 1;
  2350. sizes[0] = size;
  2351. return 0;
  2352. }
  2353. static void mxt_buffer_queue(struct vb2_buffer *vb)
  2354. {
  2355. struct mxt_data *data = vb2_get_drv_priv(vb->vb2_queue);
  2356. u16 *ptr;
  2357. int ret;
  2358. u8 mode;
  2359. ptr = vb2_plane_vaddr(vb, 0);
  2360. if (!ptr) {
  2361. dev_err(&data->client->dev, "Error acquiring frame ptr\n");
  2362. goto fault;
  2363. }
  2364. switch (data->dbg.input) {
  2365. case MXT_V4L_INPUT_DELTAS:
  2366. default:
  2367. mode = MXT_DIAGNOSTIC_DELTAS;
  2368. break;
  2369. case MXT_V4L_INPUT_REFS:
  2370. mode = MXT_DIAGNOSTIC_REFS;
  2371. break;
  2372. }
  2373. ret = mxt_read_diagnostic_debug(data, mode, ptr);
  2374. if (ret)
  2375. goto fault;
  2376. vb2_set_plane_payload(vb, 0, data->dbg.t37_nodes * sizeof(u16));
  2377. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  2378. return;
  2379. fault:
  2380. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  2381. }
  2382. /* V4L2 structures */
  2383. static const struct vb2_ops mxt_queue_ops = {
  2384. .queue_setup = mxt_queue_setup,
  2385. .buf_queue = mxt_buffer_queue,
  2386. .wait_prepare = vb2_ops_wait_prepare,
  2387. .wait_finish = vb2_ops_wait_finish,
  2388. };
  2389. static const struct vb2_queue mxt_queue = {
  2390. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  2391. .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ,
  2392. .buf_struct_size = sizeof(struct mxt_vb2_buffer),
  2393. .ops = &mxt_queue_ops,
  2394. .mem_ops = &vb2_vmalloc_memops,
  2395. .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
  2396. .min_buffers_needed = 1,
  2397. };
  2398. static int mxt_vidioc_querycap(struct file *file, void *priv,
  2399. struct v4l2_capability *cap)
  2400. {
  2401. struct mxt_data *data = video_drvdata(file);
  2402. strlcpy(cap->driver, "atmel_mxt_ts", sizeof(cap->driver));
  2403. strlcpy(cap->card, "atmel_mxt_ts touch", sizeof(cap->card));
  2404. snprintf(cap->bus_info, sizeof(cap->bus_info),
  2405. "I2C:%s", dev_name(&data->client->dev));
  2406. return 0;
  2407. }
  2408. static int mxt_vidioc_enum_input(struct file *file, void *priv,
  2409. struct v4l2_input *i)
  2410. {
  2411. if (i->index >= MXT_V4L_INPUT_MAX)
  2412. return -EINVAL;
  2413. i->type = V4L2_INPUT_TYPE_TOUCH;
  2414. switch (i->index) {
  2415. case MXT_V4L_INPUT_REFS:
  2416. strlcpy(i->name, "Mutual Capacitance References",
  2417. sizeof(i->name));
  2418. break;
  2419. case MXT_V4L_INPUT_DELTAS:
  2420. strlcpy(i->name, "Mutual Capacitance Deltas", sizeof(i->name));
  2421. break;
  2422. }
  2423. return 0;
  2424. }
  2425. static int mxt_set_input(struct mxt_data *data, unsigned int i)
  2426. {
  2427. struct v4l2_pix_format *f = &data->dbg.format;
  2428. if (i >= MXT_V4L_INPUT_MAX)
  2429. return -EINVAL;
  2430. if (i == MXT_V4L_INPUT_DELTAS)
  2431. f->pixelformat = V4L2_TCH_FMT_DELTA_TD16;
  2432. else
  2433. f->pixelformat = V4L2_TCH_FMT_TU16;
  2434. f->width = data->xy_switch ? data->ysize : data->xsize;
  2435. f->height = data->xy_switch ? data->xsize : data->ysize;
  2436. f->field = V4L2_FIELD_NONE;
  2437. f->colorspace = V4L2_COLORSPACE_RAW;
  2438. f->bytesperline = f->width * sizeof(u16);
  2439. f->sizeimage = f->width * f->height * sizeof(u16);
  2440. data->dbg.input = i;
  2441. return 0;
  2442. }
  2443. static int mxt_vidioc_s_input(struct file *file, void *priv, unsigned int i)
  2444. {
  2445. return mxt_set_input(video_drvdata(file), i);
  2446. }
  2447. static int mxt_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  2448. {
  2449. struct mxt_data *data = video_drvdata(file);
  2450. *i = data->dbg.input;
  2451. return 0;
  2452. }
  2453. static int mxt_vidioc_fmt(struct file *file, void *priv, struct v4l2_format *f)
  2454. {
  2455. struct mxt_data *data = video_drvdata(file);
  2456. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2457. f->fmt.pix = data->dbg.format;
  2458. return 0;
  2459. }
  2460. static int mxt_vidioc_enum_fmt(struct file *file, void *priv,
  2461. struct v4l2_fmtdesc *fmt)
  2462. {
  2463. if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2464. return -EINVAL;
  2465. switch (fmt->index) {
  2466. case 0:
  2467. fmt->pixelformat = V4L2_TCH_FMT_TU16;
  2468. break;
  2469. case 1:
  2470. fmt->pixelformat = V4L2_TCH_FMT_DELTA_TD16;
  2471. break;
  2472. default:
  2473. return -EINVAL;
  2474. }
  2475. return 0;
  2476. }
  2477. static int mxt_vidioc_g_parm(struct file *file, void *fh,
  2478. struct v4l2_streamparm *a)
  2479. {
  2480. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2481. return -EINVAL;
  2482. a->parm.capture.readbuffers = 1;
  2483. a->parm.capture.timeperframe.numerator = 1;
  2484. a->parm.capture.timeperframe.denominator = 10;
  2485. return 0;
  2486. }
  2487. static const struct v4l2_ioctl_ops mxt_video_ioctl_ops = {
  2488. .vidioc_querycap = mxt_vidioc_querycap,
  2489. .vidioc_enum_fmt_vid_cap = mxt_vidioc_enum_fmt,
  2490. .vidioc_s_fmt_vid_cap = mxt_vidioc_fmt,
  2491. .vidioc_g_fmt_vid_cap = mxt_vidioc_fmt,
  2492. .vidioc_try_fmt_vid_cap = mxt_vidioc_fmt,
  2493. .vidioc_g_parm = mxt_vidioc_g_parm,
  2494. .vidioc_enum_input = mxt_vidioc_enum_input,
  2495. .vidioc_g_input = mxt_vidioc_g_input,
  2496. .vidioc_s_input = mxt_vidioc_s_input,
  2497. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  2498. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  2499. .vidioc_querybuf = vb2_ioctl_querybuf,
  2500. .vidioc_qbuf = vb2_ioctl_qbuf,
  2501. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  2502. .vidioc_expbuf = vb2_ioctl_expbuf,
  2503. .vidioc_streamon = vb2_ioctl_streamon,
  2504. .vidioc_streamoff = vb2_ioctl_streamoff,
  2505. };
  2506. static const struct video_device mxt_video_device = {
  2507. .name = "Atmel maxTouch",
  2508. .fops = &mxt_video_fops,
  2509. .ioctl_ops = &mxt_video_ioctl_ops,
  2510. .release = video_device_release_empty,
  2511. .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH |
  2512. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING,
  2513. };
  2514. static void mxt_debug_init(struct mxt_data *data)
  2515. {
  2516. struct mxt_info *info = data->info;
  2517. struct mxt_dbg *dbg = &data->dbg;
  2518. struct mxt_object *object;
  2519. int error;
  2520. object = mxt_get_object(data, MXT_GEN_COMMAND_T6);
  2521. if (!object)
  2522. goto error;
  2523. dbg->diag_cmd_address = object->start_address + MXT_COMMAND_DIAGNOSTIC;
  2524. object = mxt_get_object(data, MXT_DEBUG_DIAGNOSTIC_T37);
  2525. if (!object)
  2526. goto error;
  2527. if (mxt_obj_size(object) != sizeof(struct t37_debug)) {
  2528. dev_warn(&data->client->dev, "Bad T37 size");
  2529. goto error;
  2530. }
  2531. dbg->t37_address = object->start_address;
  2532. /* Calculate size of data and allocate buffer */
  2533. dbg->t37_nodes = data->xsize * data->ysize;
  2534. if (info->family_id == MXT_FAMILY_1386)
  2535. dbg->t37_pages = MXT1386_COLUMNS * MXT1386_PAGES_PER_COLUMN;
  2536. else
  2537. dbg->t37_pages = DIV_ROUND_UP(data->xsize *
  2538. info->matrix_ysize *
  2539. sizeof(u16),
  2540. sizeof(dbg->t37_buf->data));
  2541. dbg->t37_buf = devm_kmalloc_array(&data->client->dev, dbg->t37_pages,
  2542. sizeof(struct t37_debug), GFP_KERNEL);
  2543. if (!dbg->t37_buf)
  2544. goto error;
  2545. /* init channel to zero */
  2546. mxt_set_input(data, 0);
  2547. /* register video device */
  2548. snprintf(dbg->v4l2.name, sizeof(dbg->v4l2.name), "%s", "atmel_mxt_ts");
  2549. error = v4l2_device_register(&data->client->dev, &dbg->v4l2);
  2550. if (error)
  2551. goto error;
  2552. /* initialize the queue */
  2553. mutex_init(&dbg->lock);
  2554. dbg->queue = mxt_queue;
  2555. dbg->queue.drv_priv = data;
  2556. dbg->queue.lock = &dbg->lock;
  2557. dbg->queue.dev = &data->client->dev;
  2558. error = vb2_queue_init(&dbg->queue);
  2559. if (error)
  2560. goto error_unreg_v4l2;
  2561. dbg->vdev = mxt_video_device;
  2562. dbg->vdev.v4l2_dev = &dbg->v4l2;
  2563. dbg->vdev.lock = &dbg->lock;
  2564. dbg->vdev.vfl_dir = VFL_DIR_RX;
  2565. dbg->vdev.queue = &dbg->queue;
  2566. video_set_drvdata(&dbg->vdev, data);
  2567. error = video_register_device(&dbg->vdev, VFL_TYPE_TOUCH, -1);
  2568. if (error)
  2569. goto error_unreg_v4l2;
  2570. return;
  2571. error_unreg_v4l2:
  2572. v4l2_device_unregister(&dbg->v4l2);
  2573. error:
  2574. dev_warn(&data->client->dev, "Error initializing T37\n");
  2575. }
  2576. #else
  2577. static void mxt_debug_init(struct mxt_data *data)
  2578. {
  2579. }
  2580. #endif
  2581. static int mxt_configure_objects(struct mxt_data *data,
  2582. const struct firmware *cfg)
  2583. {
  2584. struct device *dev = &data->client->dev;
  2585. int error;
  2586. error = mxt_init_t7_power_cfg(data);
  2587. if (error) {
  2588. dev_err(dev, "Failed to initialize power cfg\n");
  2589. return error;
  2590. }
  2591. if (cfg) {
  2592. error = mxt_update_cfg(data, cfg);
  2593. if (error)
  2594. dev_warn(dev, "Error %d updating config\n", error);
  2595. }
  2596. if (data->multitouch) {
  2597. error = mxt_initialize_input_device(data);
  2598. if (error)
  2599. return error;
  2600. } else {
  2601. dev_warn(dev, "No touch object detected\n");
  2602. }
  2603. mxt_debug_init(data);
  2604. enable_irq(data->irq);
  2605. return 0;
  2606. }
  2607. /* Firmware Version is returned as Major.Minor.Build */
  2608. static ssize_t mxt_fw_version_show(struct device *dev,
  2609. struct device_attribute *attr, char *buf)
  2610. {
  2611. struct mxt_data *data = dev_get_drvdata(dev);
  2612. struct mxt_info *info = data->info;
  2613. return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
  2614. info->version >> 4, info->version & 0xf, info->build);
  2615. }
  2616. /* Hardware Version is returned as FamilyID.VariantID */
  2617. static ssize_t mxt_hw_version_show(struct device *dev,
  2618. struct device_attribute *attr, char *buf)
  2619. {
  2620. struct mxt_data *data = dev_get_drvdata(dev);
  2621. struct mxt_info *info = data->info;
  2622. return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
  2623. info->family_id, info->variant_id);
  2624. }
  2625. static ssize_t mxt_show_instance(char *buf, int count,
  2626. struct mxt_object *object, int instance,
  2627. const u8 *val)
  2628. {
  2629. int i;
  2630. if (mxt_obj_instances(object) > 1)
  2631. count += scnprintf(buf + count, PAGE_SIZE - count,
  2632. "Instance %u\n", instance);
  2633. for (i = 0; i < mxt_obj_size(object); i++)
  2634. count += scnprintf(buf + count, PAGE_SIZE - count,
  2635. "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
  2636. count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
  2637. return count;
  2638. }
  2639. static ssize_t mxt_object_show(struct device *dev,
  2640. struct device_attribute *attr, char *buf)
  2641. {
  2642. struct mxt_data *data = dev_get_drvdata(dev);
  2643. struct mxt_object *object;
  2644. int count = 0;
  2645. int i, j;
  2646. int error;
  2647. u8 *obuf;
  2648. /* Pre-allocate buffer large enough to hold max sized object. */
  2649. obuf = kmalloc(256, GFP_KERNEL);
  2650. if (!obuf)
  2651. return -ENOMEM;
  2652. error = 0;
  2653. for (i = 0; i < data->info->object_num; i++) {
  2654. object = data->object_table + i;
  2655. if (!mxt_object_readable(object->type))
  2656. continue;
  2657. count += scnprintf(buf + count, PAGE_SIZE - count,
  2658. "T%u:\n", object->type);
  2659. for (j = 0; j < mxt_obj_instances(object); j++) {
  2660. u16 size = mxt_obj_size(object);
  2661. u16 addr = object->start_address + j * size;
  2662. error = __mxt_read_reg(data, addr, size, obuf);
  2663. if (error)
  2664. goto done;
  2665. count = mxt_show_instance(buf, count, object, j, obuf);
  2666. }
  2667. }
  2668. done:
  2669. kfree(obuf);
  2670. return error ?: count;
  2671. }
  2672. static int mxt_check_firmware_format(struct device *dev,
  2673. const struct firmware *fw)
  2674. {
  2675. unsigned int pos = 0;
  2676. char c;
  2677. while (pos < fw->size) {
  2678. c = *(fw->data + pos);
  2679. if (c < '0' || (c > '9' && c < 'A') || c > 'F')
  2680. return 0;
  2681. pos++;
  2682. }
  2683. /*
  2684. * To convert file try:
  2685. * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw
  2686. */
  2687. dev_err(dev, "Aborting: firmware file must be in binary format\n");
  2688. return -EINVAL;
  2689. }
  2690. static int mxt_load_fw(struct device *dev, const char *fn)
  2691. {
  2692. struct mxt_data *data = dev_get_drvdata(dev);
  2693. const struct firmware *fw = NULL;
  2694. unsigned int frame_size;
  2695. unsigned int pos = 0;
  2696. unsigned int retry = 0;
  2697. unsigned int frame = 0;
  2698. int ret;
  2699. ret = request_firmware(&fw, fn, dev);
  2700. if (ret) {
  2701. dev_err(dev, "Unable to open firmware %s\n", fn);
  2702. return ret;
  2703. }
  2704. /* Check for incorrect enc file */
  2705. ret = mxt_check_firmware_format(dev, fw);
  2706. if (ret)
  2707. goto release_firmware;
  2708. if (!data->in_bootloader) {
  2709. /* Change to the bootloader mode */
  2710. data->in_bootloader = true;
  2711. ret = mxt_t6_command(data, MXT_COMMAND_RESET,
  2712. MXT_BOOT_VALUE, false);
  2713. if (ret)
  2714. goto release_firmware;
  2715. msleep(MXT_RESET_TIME);
  2716. /* Do not need to scan since we know family ID */
  2717. ret = mxt_lookup_bootloader_address(data, 0);
  2718. if (ret)
  2719. goto release_firmware;
  2720. mxt_free_input_device(data);
  2721. mxt_free_object_table(data);
  2722. } else {
  2723. enable_irq(data->irq);
  2724. }
  2725. reinit_completion(&data->bl_completion);
  2726. ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD, false);
  2727. if (ret) {
  2728. /* Bootloader may still be unlocked from previous attempt */
  2729. ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, false);
  2730. if (ret)
  2731. goto disable_irq;
  2732. } else {
  2733. dev_info(dev, "Unlocking bootloader\n");
  2734. /* Unlock bootloader */
  2735. ret = mxt_send_bootloader_cmd(data, true);
  2736. if (ret)
  2737. goto disable_irq;
  2738. }
  2739. while (pos < fw->size) {
  2740. ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, true);
  2741. if (ret)
  2742. goto disable_irq;
  2743. frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
  2744. /* Take account of CRC bytes */
  2745. frame_size += 2;
  2746. /* Write one frame to device */
  2747. ret = mxt_bootloader_write(data, fw->data + pos, frame_size);
  2748. if (ret)
  2749. goto disable_irq;
  2750. ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS, true);
  2751. if (ret) {
  2752. retry++;
  2753. /* Back off by 20ms per retry */
  2754. msleep(retry * 20);
  2755. if (retry > 20) {
  2756. dev_err(dev, "Retry count exceeded\n");
  2757. goto disable_irq;
  2758. }
  2759. } else {
  2760. retry = 0;
  2761. pos += frame_size;
  2762. frame++;
  2763. }
  2764. if (frame % 50 == 0)
  2765. dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n",
  2766. frame, pos, fw->size);
  2767. }
  2768. /* Wait for flash. */
  2769. ret = mxt_wait_for_completion(data, &data->bl_completion,
  2770. MXT_FW_RESET_TIME);
  2771. if (ret)
  2772. goto disable_irq;
  2773. dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos);
  2774. /*
  2775. * Wait for device to reset. Some bootloader versions do not assert
  2776. * the CHG line after bootloading has finished, so ignore potential
  2777. * errors.
  2778. */
  2779. mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME);
  2780. data->in_bootloader = false;
  2781. disable_irq:
  2782. disable_irq(data->irq);
  2783. release_firmware:
  2784. release_firmware(fw);
  2785. return ret;
  2786. }
  2787. static ssize_t mxt_update_fw_store(struct device *dev,
  2788. struct device_attribute *attr,
  2789. const char *buf, size_t count)
  2790. {
  2791. struct mxt_data *data = dev_get_drvdata(dev);
  2792. int error;
  2793. error = mxt_load_fw(dev, MXT_FW_NAME);
  2794. if (error) {
  2795. dev_err(dev, "The firmware update failed(%d)\n", error);
  2796. count = error;
  2797. } else {
  2798. dev_info(dev, "The firmware update succeeded\n");
  2799. error = mxt_initialize(data);
  2800. if (error)
  2801. return error;
  2802. }
  2803. return count;
  2804. }
  2805. static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
  2806. static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
  2807. static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
  2808. static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
  2809. static struct attribute *mxt_attrs[] = {
  2810. &dev_attr_fw_version.attr,
  2811. &dev_attr_hw_version.attr,
  2812. &dev_attr_object.attr,
  2813. &dev_attr_update_fw.attr,
  2814. NULL
  2815. };
  2816. static const struct attribute_group mxt_attr_group = {
  2817. .attrs = mxt_attrs,
  2818. };
  2819. static void mxt_start(struct mxt_data *data)
  2820. {
  2821. if (!data->suspended || data->in_bootloader)
  2822. return;
  2823. /* enable gpios */
  2824. mxt_gpio_enable(data, true);
  2825. /* enable regulators */
  2826. mxt_regulator_enable(data);
  2827. switch (data->suspend_mode) {
  2828. case MXT_SUSPEND_T9_CTRL:
  2829. mxt_soft_reset(data);
  2830. /* Touch enable */
  2831. /* 0x83 = SCANEN | RPTEN | ENABLE */
  2832. mxt_write_object(data,
  2833. MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0x83);
  2834. break;
  2835. case MXT_SUSPEND_DEEP_SLEEP:
  2836. default:
  2837. mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
  2838. /* Recalibrate since chip has been in deep sleep */
  2839. mxt_t6_command(data, MXT_COMMAND_CALIBRATE, 1, false);
  2840. break;
  2841. }
  2842. mxt_acquire_irq(data);
  2843. data->suspended = false;
  2844. }
  2845. static void mxt_stop(struct mxt_data *data)
  2846. {
  2847. if (data->suspended || data->in_bootloader)
  2848. return;
  2849. disable_irq(data->irq);
  2850. switch (data->suspend_mode) {
  2851. case MXT_SUSPEND_T9_CTRL:
  2852. /* Touch disable */
  2853. mxt_write_object(data,
  2854. MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0);
  2855. break;
  2856. case MXT_SUSPEND_DEEP_SLEEP:
  2857. default:
  2858. mxt_set_t7_power_cfg(data, MXT_POWER_CFG_DEEPSLEEP);
  2859. break;
  2860. }
  2861. /* disable regulators */
  2862. mxt_regulator_disable(data);
  2863. /* disable gpios */
  2864. mxt_gpio_enable(data, false);
  2865. data->suspended = true;
  2866. }
  2867. static int mxt_input_open(struct input_dev *dev)
  2868. {
  2869. #if !defined(CONFIG_DRM)
  2870. struct mxt_data *data = input_get_drvdata(dev);
  2871. mxt_start(data);
  2872. #endif
  2873. return 0;
  2874. }
  2875. static void mxt_input_close(struct input_dev *dev)
  2876. {
  2877. #if !defined(CONFIG_DRM)
  2878. struct mxt_data *data = input_get_drvdata(dev);
  2879. mxt_stop(data);
  2880. #endif
  2881. }
  2882. static int mxt_parse_device_properties(struct mxt_data *data)
  2883. {
  2884. static const char keymap_property[] = "linux,gpio-keymap";
  2885. struct device *dev = &data->client->dev;
  2886. u32 *keymap;
  2887. int n_keys;
  2888. int error;
  2889. if (device_property_present(dev, keymap_property)) {
  2890. n_keys = device_property_read_u32_array(dev, keymap_property,
  2891. NULL, 0);
  2892. if (n_keys <= 0) {
  2893. error = n_keys < 0 ? n_keys : -EINVAL;
  2894. dev_err(dev, "invalid/malformed '%s' property: %d\n",
  2895. keymap_property, error);
  2896. return error;
  2897. }
  2898. keymap = devm_kmalloc_array(dev, n_keys, sizeof(*keymap),
  2899. GFP_KERNEL);
  2900. if (!keymap)
  2901. return -ENOMEM;
  2902. error = device_property_read_u32_array(dev, keymap_property,
  2903. keymap, n_keys);
  2904. if (error) {
  2905. dev_err(dev, "failed to parse '%s' property: %d\n",
  2906. keymap_property, error);
  2907. return error;
  2908. }
  2909. data->t19_keymap = keymap;
  2910. data->t19_num_keys = n_keys;
  2911. }
  2912. return 0;
  2913. }
  2914. #ifdef CONFIG_OF
  2915. static int mxt_check_dedicated_touch(struct device_node *dt, const char *prop,
  2916. const char *active_prop)
  2917. {
  2918. const char *active_touch;
  2919. const char *compatible;
  2920. char *temp;
  2921. int ret = 0;
  2922. ret = of_property_read_string(dt->parent, active_prop, &active_touch);
  2923. if (ret < 0) {
  2924. pr_info(" %s:not dedicated active touch\n", __func__);
  2925. return -ENODEV;
  2926. }
  2927. ret = of_property_read_string(dt, prop, &compatible);
  2928. if (ret < 0) {
  2929. pr_err(" %s:fail to read %s %d\n", __func__, "compatible", ret);
  2930. return -ENODEV;
  2931. }
  2932. temp = strnstr(active_touch, compatible, strlen(active_touch));
  2933. if (!temp) {
  2934. pr_info(" %s:no match compatible, %s, %s\n",
  2935. __func__, compatible, active_touch);
  2936. return -ENODEV;
  2937. }
  2938. return ret;
  2939. }
  2940. #ifdef CONFIG_DRM
  2941. static struct drm_panel *active_panel;
  2942. static int mxt_check_dt(struct device_node *np)
  2943. {
  2944. int i;
  2945. int count;
  2946. struct device_node *node;
  2947. struct drm_panel *panel;
  2948. count = of_count_phandle_with_args(np, "panel", NULL);
  2949. if (count <= 0)
  2950. return 0;
  2951. for (i = 0; i < count; i++) {
  2952. node = of_parse_phandle(np, "panel", i);
  2953. panel = of_drm_find_panel(node);
  2954. of_node_put(node);
  2955. if (!IS_ERR(panel)) {
  2956. active_panel = panel;
  2957. return 0;
  2958. }
  2959. }
  2960. return PTR_ERR(panel);
  2961. }
  2962. #endif
  2963. static int mxt_get_dt_coords(struct device *dev, char *name,
  2964. struct mxt_data *data)
  2965. {
  2966. u32 coords[MXT_COORDS_ARR_SIZE];
  2967. struct property *prop;
  2968. struct device_node *np = dev->of_node;
  2969. size_t coords_size;
  2970. int error;
  2971. prop = of_find_property(np, name, NULL);
  2972. if (!prop)
  2973. return -EINVAL;
  2974. if (!prop->value)
  2975. return -ENODATA;
  2976. coords_size = prop->length / sizeof(u32);
  2977. if (coords_size != MXT_COORDS_ARR_SIZE) {
  2978. dev_err(dev, "invalid %s\n", name);
  2979. return -EINVAL;
  2980. }
  2981. error = of_property_read_u32_array(np, name, coords, coords_size);
  2982. if (error && (error != -EINVAL)) {
  2983. dev_err(dev, "Unable to read %s\n", name);
  2984. return error;
  2985. }
  2986. if (strcmp(name, "atmel,panel-coords") == 0) {
  2987. data->panel_minx = coords[0];
  2988. data->panel_miny = coords[1];
  2989. data->panel_maxx = coords[2];
  2990. data->panel_maxy = coords[3];
  2991. } else if (strcmp(name, "atmel,display-coords") == 0) {
  2992. data->disp_minx = coords[0];
  2993. data->disp_miny = coords[1];
  2994. data->disp_maxx = coords[2];
  2995. data->disp_maxy = coords[3];
  2996. } else {
  2997. dev_err(dev, "unsupported property %s\n", name);
  2998. return -EINVAL;
  2999. }
  3000. return 0;
  3001. }
  3002. #endif
  3003. static const struct dmi_system_id chromebook_T9_suspend_dmi[] = {
  3004. {
  3005. .matches = {
  3006. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  3007. DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
  3008. },
  3009. },
  3010. {
  3011. .matches = {
  3012. DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
  3013. },
  3014. },
  3015. { }
  3016. };
  3017. #ifdef CONFIG_DRM
  3018. static void mxt_panel_notifier_callback(enum panel_event_notifier_tag tag,
  3019. struct panel_event_notification *notification,
  3020. void *client_data)
  3021. {
  3022. struct mxt_data *mxt = client_data;
  3023. struct input_dev *input_dev;
  3024. if (!notification) {
  3025. pr_err("Invalid notification\n");
  3026. return;
  3027. }
  3028. if (!mxt) {
  3029. pr_err("Invalid mxt data\n");
  3030. return;
  3031. }
  3032. pr_debug("Notification type:%d, early_trigger:%d\n",
  3033. notification->notif_type,
  3034. notification->notif_data.early_trigger);
  3035. input_dev = mxt->input_dev;
  3036. switch (notification->notif_type) {
  3037. case DRM_PANEL_EVENT_UNBLANK:
  3038. if (!notification->notif_data.early_trigger) {
  3039. if (input_dev)
  3040. mutex_lock(&input_dev->mutex);
  3041. mxt_start(mxt);
  3042. if (input_dev)
  3043. mutex_unlock(&input_dev->mutex);
  3044. }
  3045. break;
  3046. case DRM_PANEL_EVENT_BLANK:
  3047. if (input_dev)
  3048. mutex_lock(&input_dev->mutex);
  3049. mxt_stop(mxt);
  3050. if (input_dev)
  3051. mutex_unlock(&input_dev->mutex);
  3052. break;
  3053. default:
  3054. break;
  3055. }
  3056. }
  3057. static void mxt_register_for_panel_events(struct device_node *dt,
  3058. struct mxt_data *data)
  3059. {
  3060. void *cookie = NULL;
  3061. cookie = panel_event_notifier_register(
  3062. PANEL_EVENT_NOTIFICATION_PRIMARY,
  3063. PANEL_EVENT_NOTIFIER_CLIENT_PRIMARY_TOUCH,
  3064. active_panel, &mxt_panel_notifier_callback, data);
  3065. if (!cookie) {
  3066. pr_err("Failed to register for panel events\n");
  3067. return;
  3068. }
  3069. pr_debug("registered for panel notifications on panel: 0x%x\n",
  3070. active_panel);
  3071. data->notifier_cookie = cookie;
  3072. }
  3073. #endif
  3074. static int mxt_probe(struct i2c_client *client, const struct i2c_device_id *id)
  3075. {
  3076. struct mxt_data *data;
  3077. int error = 0;
  3078. #ifdef CONFIG_OF
  3079. struct device_node *dt = client->dev.of_node;
  3080. #endif
  3081. /*
  3082. * Ignore devices that do not have device properties attached to
  3083. * them, as we need help determining whether we are dealing with
  3084. * touch screen or touchpad.
  3085. *
  3086. * So far on x86 the only users of Atmel touch controllers are
  3087. * Chromebooks, and chromeos_laptop driver will ensure that
  3088. * necessary properties are provided (if firmware does not do that).
  3089. */
  3090. if (!device_property_present(&client->dev, "compatible"))
  3091. return -ENXIO;
  3092. /*
  3093. * Ignore ACPI devices representing bootloader mode.
  3094. *
  3095. * This is a bit of a hack: Google Chromebook BIOS creates ACPI
  3096. * devices for both application and bootloader modes, but we are
  3097. * interested in application mode only (if device is in bootloader
  3098. * mode we'll end up switching into application anyway). So far
  3099. * application mode addresses were all above 0x40, so we'll use it
  3100. * as a threshold.
  3101. */
  3102. if (ACPI_COMPANION(&client->dev) && client->addr < 0x40)
  3103. return -ENXIO;
  3104. #ifdef CONFIG_OF
  3105. #ifdef CONFIG_DRM
  3106. error = mxt_check_dt(dt);
  3107. if (error == -EPROBE_DEFER)
  3108. return error;
  3109. if (error) {
  3110. if (!(mxt_check_dedicated_touch(dt, "compatible",
  3111. "qcom,i2c-touch-active")))
  3112. return -EPROBE_DEFER;
  3113. else
  3114. return -ENODEV;
  3115. }
  3116. #else
  3117. if (mxt_check_dedicated_touch(dt, "compatible",
  3118. "qcom,i2c-touch-active") < 0)
  3119. return -ENODEV;
  3120. #endif
  3121. #endif
  3122. data = devm_kzalloc(&client->dev, sizeof(struct mxt_data), GFP_KERNEL);
  3123. if (!data)
  3124. return -ENOMEM;
  3125. snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
  3126. client->adapter->nr, client->addr);
  3127. data->client = client;
  3128. data->irq = client->irq;
  3129. i2c_set_clientdata(client, data);
  3130. #ifdef CONFIG_OF
  3131. error = mxt_get_dt_coords(&client->dev, "atmel,display-coords", data);
  3132. if (error)
  3133. return error;
  3134. data->xy_switch = of_property_read_bool(dt, "atmel,xy_switch");
  3135. data->invertx = of_property_read_bool(dt, "atmel,invertx");
  3136. data->inverty = of_property_read_bool(dt, "atmel,inverty");
  3137. #endif
  3138. data->cmd_buf = devm_kzalloc(&client->dev, 64, GFP_KERNEL);
  3139. if (!data->cmd_buf)
  3140. return -ENOMEM;
  3141. data->read_buf = data->cmd_buf + 32;
  3142. init_completion(&data->bl_completion);
  3143. init_completion(&data->reset_completion);
  3144. init_completion(&data->crc_completion);
  3145. #if defined(CONFIG_OF) && defined(CONFIG_DRM)
  3146. data->suspend_mode = MXT_SUSPEND_T9_CTRL;
  3147. #else
  3148. data->suspend_mode = dmi_check_system(chromebook_T9_suspend_dmi) ?
  3149. MXT_SUSPEND_T9_CTRL : MXT_SUSPEND_DEEP_SLEEP;
  3150. #endif
  3151. error = mxt_parse_device_properties(data);
  3152. if (error)
  3153. return error;
  3154. error = mxt_pinctrl_init(data);
  3155. if (error)
  3156. dev_info(&client->dev, "No pinctrl support\n");
  3157. data->reset_gpio = devm_gpiod_get_optional(&client->dev,
  3158. "reset", GPIOD_OUT_LOW);
  3159. data->irq_gpio = devm_gpiod_get_optional(&client->dev,
  3160. "irq", GPIOD_IN);
  3161. if (IS_ERR(data->reset_gpio)) {
  3162. error = PTR_ERR(data->reset_gpio);
  3163. dev_err(&client->dev, "Failed to get reset gpio: %d\n", error);
  3164. return error;
  3165. }
  3166. gpiod_direction_input(data->irq_gpio);
  3167. gpiod_direction_output(data->reset_gpio, 0);
  3168. error = mxt_regulator_parse(data);
  3169. if (error) {
  3170. dev_err(&client->dev, "Failed to parse regulators\n");
  3171. return error;
  3172. }
  3173. error = mxt_regulator_enable(data);
  3174. if (error) {
  3175. dev_err(&client->dev, "Error %d enabling regulators\n", error);
  3176. return error;
  3177. }
  3178. error = devm_request_threaded_irq(&client->dev, client->irq,
  3179. NULL, mxt_interrupt, IRQF_ONESHOT,
  3180. client->name, data);
  3181. if (error) {
  3182. dev_err(&client->dev, "Failed to register interrupt\n");
  3183. goto err_disable_regulator;
  3184. }
  3185. disable_irq(client->irq);
  3186. if (data->reset_gpio) {
  3187. gpiod_set_value(data->reset_gpio, 0);
  3188. msleep(MXT_RESET_GPIO_TIME);
  3189. gpiod_set_value(data->reset_gpio, 1);
  3190. msleep(MXT_RESET_INVALID_CHG);
  3191. }
  3192. error = mxt_gpio_enable(data, true);
  3193. if (error)
  3194. dev_err(&client->dev, "Failed to configure gpios\n");
  3195. data->irq = data->client->irq =
  3196. gpiod_to_irq(data->irq_gpio);
  3197. error = mxt_initialize(data);
  3198. if (error)
  3199. goto err_disable_regulator;
  3200. error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
  3201. if (error) {
  3202. dev_err(&client->dev, "Failure %d creating sysfs group\n",
  3203. error);
  3204. goto err_free_object;
  3205. }
  3206. #ifdef CONFIG_DRM
  3207. if (active_panel)
  3208. mxt_register_for_panel_events(dt, data);
  3209. #endif
  3210. return 0;
  3211. err_free_object:
  3212. mxt_free_input_device(data);
  3213. mxt_free_object_table(data);
  3214. err_disable_regulator:
  3215. mxt_regulator_disable(data);
  3216. return error;
  3217. }
  3218. static int mxt_remove(struct i2c_client *client)
  3219. {
  3220. struct mxt_data *data = i2c_get_clientdata(client);
  3221. #ifdef CONFIG_DRM
  3222. if (active_panel && data->notifier_cookie)
  3223. panel_event_notifier_unregister(data->notifier_cookie);
  3224. #endif
  3225. disable_irq(data->irq);
  3226. mxt_regulator_disable(data);
  3227. sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
  3228. mxt_free_input_device(data);
  3229. mxt_free_object_table(data);
  3230. return 0;
  3231. }
  3232. static int __maybe_unused mxt_suspend(struct device *dev)
  3233. {
  3234. #if !defined(CONFIG_DRM)
  3235. struct i2c_client *client = to_i2c_client(dev);
  3236. struct mxt_data *data = i2c_get_clientdata(client);
  3237. struct input_dev *input_dev = data->input_dev;
  3238. if (!input_dev)
  3239. return 0;
  3240. mutex_lock(&input_dev->mutex);
  3241. if (input_dev->users)
  3242. mxt_stop(data);
  3243. mutex_unlock(&input_dev->mutex);
  3244. disable_irq(data->irq);
  3245. #endif
  3246. return 0;
  3247. }
  3248. static int __maybe_unused mxt_resume(struct device *dev)
  3249. {
  3250. #if !defined(CONFIG_DRM)
  3251. struct i2c_client *client = to_i2c_client(dev);
  3252. struct mxt_data *data = i2c_get_clientdata(client);
  3253. struct input_dev *input_dev = data->input_dev;
  3254. if (!input_dev)
  3255. return 0;
  3256. enable_irq(data->irq);
  3257. mutex_lock(&input_dev->mutex);
  3258. if (input_dev->users)
  3259. mxt_start(data);
  3260. mutex_unlock(&input_dev->mutex);
  3261. #endif
  3262. return 0;
  3263. }
  3264. static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
  3265. static const struct of_device_id mxt_of_match[] = {
  3266. { .compatible = "atmel,maxtouch", },
  3267. /* Compatibles listed below are deprecated */
  3268. { .compatible = "atmel,qt602240_ts", },
  3269. { .compatible = "atmel,atmel_mxt_ts", },
  3270. { .compatible = "atmel,atmel_mxt_tp", },
  3271. { .compatible = "atmel,mXT224", },
  3272. {},
  3273. };
  3274. MODULE_DEVICE_TABLE(of, mxt_of_match);
  3275. #ifdef CONFIG_ACPI
  3276. static const struct acpi_device_id mxt_acpi_id[] = {
  3277. { "ATML0000", 0 }, /* Touchpad */
  3278. { "ATML0001", 0 }, /* Touchscreen */
  3279. { }
  3280. };
  3281. MODULE_DEVICE_TABLE(acpi, mxt_acpi_id);
  3282. #endif
  3283. static const struct i2c_device_id mxt_id[] = {
  3284. { "qt602240_ts", 0 },
  3285. { "atmel_mxt_ts", 0 },
  3286. { "atmel_mxt_tp", 0 },
  3287. { "maxtouch", 0 },
  3288. { "mXT224", 0 },
  3289. { }
  3290. };
  3291. MODULE_DEVICE_TABLE(i2c, mxt_id);
  3292. static struct i2c_driver mxt_driver = {
  3293. .driver = {
  3294. .name = "atmel_mxt_ts",
  3295. .of_match_table = mxt_of_match,
  3296. .acpi_match_table = ACPI_PTR(mxt_acpi_id),
  3297. .pm = &mxt_pm_ops,
  3298. },
  3299. .probe = mxt_probe,
  3300. .remove = mxt_remove,
  3301. .id_table = mxt_id,
  3302. };
  3303. module_i2c_driver(mxt_driver);
  3304. /* Module information */
  3305. MODULE_AUTHOR("Joonyoung Shim <[email protected]>");
  3306. MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
  3307. MODULE_LICENSE("GPL");