atmel_mxt_ts.c 62 KB

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