atmel_mxt_ts.c 75 KB

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