atmel_mxt_ts.c 62 KB

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