atmel_mxt_ts.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770
  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. /* Only handle messages from first T9 instance */
  1337. data->T9_reportid_min = min_id;
  1338. data->T9_reportid_max = min_id +
  1339. object->num_report_ids - 1;
  1340. data->num_touchids = object->num_report_ids;
  1341. break;
  1342. case MXT_SPT_MESSAGECOUNT_T44:
  1343. data->T44_address = object->start_address;
  1344. break;
  1345. case MXT_SPT_GPIOPWM_T19:
  1346. data->T19_reportid = min_id;
  1347. break;
  1348. case MXT_TOUCH_MULTITOUCHSCREEN_T100:
  1349. data->multitouch = MXT_TOUCH_MULTITOUCHSCREEN_T100;
  1350. data->T100_reportid_min = min_id;
  1351. data->T100_reportid_max = max_id;
  1352. /* first two report IDs reserved */
  1353. data->num_touchids = object->num_report_ids - 2;
  1354. break;
  1355. }
  1356. end_address = object->start_address
  1357. + mxt_obj_size(object) * mxt_obj_instances(object) - 1;
  1358. if (end_address >= data->mem_size)
  1359. data->mem_size = end_address + 1;
  1360. }
  1361. /* Store maximum reportid */
  1362. data->max_reportid = reportid;
  1363. /* If T44 exists, T5 position has to be directly after */
  1364. if (data->T44_address && (data->T5_address != data->T44_address + 1)) {
  1365. dev_err(&client->dev, "Invalid T44 position\n");
  1366. error = -EINVAL;
  1367. goto free_object_table;
  1368. }
  1369. data->msg_buf = kcalloc(data->max_reportid,
  1370. data->T5_msg_size, GFP_KERNEL);
  1371. if (!data->msg_buf) {
  1372. dev_err(&client->dev, "Failed to allocate message buffer\n");
  1373. error = -ENOMEM;
  1374. goto free_object_table;
  1375. }
  1376. data->object_table = object_table;
  1377. return 0;
  1378. free_object_table:
  1379. mxt_free_object_table(data);
  1380. return error;
  1381. }
  1382. static int mxt_read_t9_resolution(struct mxt_data *data)
  1383. {
  1384. struct i2c_client *client = data->client;
  1385. int error;
  1386. struct t9_range range;
  1387. unsigned char orient;
  1388. struct mxt_object *object;
  1389. object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
  1390. if (!object)
  1391. return -EINVAL;
  1392. error = __mxt_read_reg(client,
  1393. object->start_address + MXT_T9_RANGE,
  1394. sizeof(range), &range);
  1395. if (error)
  1396. return error;
  1397. le16_to_cpus(&range.x);
  1398. le16_to_cpus(&range.y);
  1399. error = __mxt_read_reg(client,
  1400. object->start_address + MXT_T9_ORIENT,
  1401. 1, &orient);
  1402. if (error)
  1403. return error;
  1404. /* Handle default values */
  1405. if (range.x == 0)
  1406. range.x = 1023;
  1407. if (range.y == 0)
  1408. range.y = 1023;
  1409. if (orient & MXT_T9_ORIENT_SWITCH) {
  1410. data->max_x = range.y;
  1411. data->max_y = range.x;
  1412. } else {
  1413. data->max_x = range.x;
  1414. data->max_y = range.y;
  1415. }
  1416. dev_dbg(&client->dev,
  1417. "Touchscreen size X%uY%u\n", data->max_x, data->max_y);
  1418. return 0;
  1419. }
  1420. static int mxt_read_t100_config(struct mxt_data *data)
  1421. {
  1422. struct i2c_client *client = data->client;
  1423. int error;
  1424. struct mxt_object *object;
  1425. u16 range_x, range_y;
  1426. u8 cfg, tchaux;
  1427. u8 aux;
  1428. object = mxt_get_object(data, MXT_TOUCH_MULTITOUCHSCREEN_T100);
  1429. if (!object)
  1430. return -EINVAL;
  1431. error = __mxt_read_reg(client,
  1432. object->start_address + MXT_T100_XRANGE,
  1433. sizeof(range_x), &range_x);
  1434. if (error)
  1435. return error;
  1436. le16_to_cpus(&range_x);
  1437. error = __mxt_read_reg(client,
  1438. object->start_address + MXT_T100_YRANGE,
  1439. sizeof(range_y), &range_y);
  1440. if (error)
  1441. return error;
  1442. le16_to_cpus(&range_y);
  1443. error = __mxt_read_reg(client,
  1444. object->start_address + MXT_T100_CFG1,
  1445. 1, &cfg);
  1446. if (error)
  1447. return error;
  1448. error = __mxt_read_reg(client,
  1449. object->start_address + MXT_T100_TCHAUX,
  1450. 1, &tchaux);
  1451. if (error)
  1452. return error;
  1453. /* Handle default values */
  1454. if (range_x == 0)
  1455. range_x = 1023;
  1456. if (range_y == 0)
  1457. range_y = 1023;
  1458. if (cfg & MXT_T100_CFG_SWITCHXY) {
  1459. data->max_x = range_y;
  1460. data->max_y = range_x;
  1461. } else {
  1462. data->max_x = range_x;
  1463. data->max_y = range_y;
  1464. }
  1465. /* allocate aux bytes */
  1466. aux = 6;
  1467. if (tchaux & MXT_T100_TCHAUX_VECT)
  1468. data->t100_aux_vect = aux++;
  1469. if (tchaux & MXT_T100_TCHAUX_AMPL)
  1470. data->t100_aux_ampl = aux++;
  1471. if (tchaux & MXT_T100_TCHAUX_AREA)
  1472. data->t100_aux_area = aux++;
  1473. dev_dbg(&client->dev,
  1474. "T100 aux mappings vect:%u ampl:%u area:%u\n",
  1475. data->t100_aux_vect, data->t100_aux_ampl, data->t100_aux_area);
  1476. dev_info(&client->dev,
  1477. "T100 Touchscreen size X%uY%u\n", data->max_x, data->max_y);
  1478. return 0;
  1479. }
  1480. static int mxt_input_open(struct input_dev *dev);
  1481. static void mxt_input_close(struct input_dev *dev);
  1482. static void mxt_set_up_as_touchpad(struct input_dev *input_dev,
  1483. struct mxt_data *data)
  1484. {
  1485. const struct mxt_platform_data *pdata = data->pdata;
  1486. int i;
  1487. input_dev->name = "Atmel maXTouch Touchpad";
  1488. __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
  1489. input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
  1490. input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
  1491. input_abs_set_res(input_dev, ABS_MT_POSITION_X,
  1492. MXT_PIXELS_PER_MM);
  1493. input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
  1494. MXT_PIXELS_PER_MM);
  1495. for (i = 0; i < pdata->t19_num_keys; i++)
  1496. if (pdata->t19_keymap[i] != KEY_RESERVED)
  1497. input_set_capability(input_dev, EV_KEY,
  1498. pdata->t19_keymap[i]);
  1499. }
  1500. static int mxt_initialize_input_device(struct mxt_data *data)
  1501. {
  1502. const struct mxt_platform_data *pdata = data->pdata;
  1503. struct device *dev = &data->client->dev;
  1504. struct input_dev *input_dev;
  1505. int error;
  1506. unsigned int num_mt_slots;
  1507. unsigned int mt_flags = 0;
  1508. switch (data->multitouch) {
  1509. case MXT_TOUCH_MULTI_T9:
  1510. num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
  1511. error = mxt_read_t9_resolution(data);
  1512. if (error)
  1513. dev_warn(dev, "Failed to initialize T9 resolution\n");
  1514. break;
  1515. case MXT_TOUCH_MULTITOUCHSCREEN_T100:
  1516. num_mt_slots = data->num_touchids;
  1517. error = mxt_read_t100_config(data);
  1518. if (error)
  1519. dev_warn(dev, "Failed to read T100 config\n");
  1520. break;
  1521. default:
  1522. dev_err(dev, "Invalid multitouch object\n");
  1523. return -EINVAL;
  1524. }
  1525. input_dev = input_allocate_device();
  1526. if (!input_dev) {
  1527. dev_err(dev, "Failed to allocate memory\n");
  1528. return -ENOMEM;
  1529. }
  1530. input_dev->name = "Atmel maXTouch Touchscreen";
  1531. input_dev->phys = data->phys;
  1532. input_dev->id.bustype = BUS_I2C;
  1533. input_dev->dev.parent = dev;
  1534. input_dev->open = mxt_input_open;
  1535. input_dev->close = mxt_input_close;
  1536. input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
  1537. /* For single touch */
  1538. input_set_abs_params(input_dev, ABS_X, 0, data->max_x, 0, 0);
  1539. input_set_abs_params(input_dev, ABS_Y, 0, data->max_y, 0, 0);
  1540. if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
  1541. (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
  1542. data->t100_aux_ampl)) {
  1543. input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0);
  1544. }
  1545. /* If device has buttons we assume it is a touchpad */
  1546. if (pdata->t19_num_keys) {
  1547. mxt_set_up_as_touchpad(input_dev, data);
  1548. mt_flags |= INPUT_MT_POINTER;
  1549. } else {
  1550. mt_flags |= INPUT_MT_DIRECT;
  1551. }
  1552. /* For multi touch */
  1553. error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
  1554. if (error) {
  1555. dev_err(dev, "Error %d initialising slots\n", error);
  1556. goto err_free_mem;
  1557. }
  1558. if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100) {
  1559. input_set_abs_params(input_dev, ABS_MT_TOOL_TYPE,
  1560. 0, MT_TOOL_MAX, 0, 0);
  1561. input_set_abs_params(input_dev, ABS_MT_DISTANCE,
  1562. MXT_DISTANCE_ACTIVE_TOUCH,
  1563. MXT_DISTANCE_HOVERING,
  1564. 0, 0);
  1565. }
  1566. input_set_abs_params(input_dev, ABS_MT_POSITION_X,
  1567. 0, data->max_x, 0, 0);
  1568. input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
  1569. 0, data->max_y, 0, 0);
  1570. if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
  1571. (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
  1572. data->t100_aux_area)) {
  1573. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
  1574. 0, MXT_MAX_AREA, 0, 0);
  1575. }
  1576. if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
  1577. (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
  1578. data->t100_aux_ampl)) {
  1579. input_set_abs_params(input_dev, ABS_MT_PRESSURE,
  1580. 0, 255, 0, 0);
  1581. }
  1582. if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
  1583. data->t100_aux_vect) {
  1584. input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
  1585. 0, 255, 0, 0);
  1586. }
  1587. if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
  1588. data->t100_aux_ampl) {
  1589. input_set_abs_params(input_dev, ABS_MT_PRESSURE,
  1590. 0, 255, 0, 0);
  1591. }
  1592. if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
  1593. data->t100_aux_vect) {
  1594. input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
  1595. 0, 255, 0, 0);
  1596. }
  1597. input_set_drvdata(input_dev, data);
  1598. error = input_register_device(input_dev);
  1599. if (error) {
  1600. dev_err(dev, "Error %d registering input device\n", error);
  1601. goto err_free_mem;
  1602. }
  1603. data->input_dev = input_dev;
  1604. return 0;
  1605. err_free_mem:
  1606. input_free_device(input_dev);
  1607. return error;
  1608. }
  1609. static int mxt_configure_objects(struct mxt_data *data,
  1610. const struct firmware *cfg);
  1611. static void mxt_config_cb(const struct firmware *cfg, void *ctx)
  1612. {
  1613. mxt_configure_objects(ctx, cfg);
  1614. release_firmware(cfg);
  1615. }
  1616. static int mxt_initialize(struct mxt_data *data)
  1617. {
  1618. struct i2c_client *client = data->client;
  1619. int recovery_attempts = 0;
  1620. int error;
  1621. while (1) {
  1622. error = mxt_get_info(data);
  1623. if (!error)
  1624. break;
  1625. /* Check bootloader state */
  1626. error = mxt_probe_bootloader(data, false);
  1627. if (error) {
  1628. dev_info(&client->dev, "Trying alternate bootloader address\n");
  1629. error = mxt_probe_bootloader(data, true);
  1630. if (error) {
  1631. /* Chip is not in appmode or bootloader mode */
  1632. return error;
  1633. }
  1634. }
  1635. /* OK, we are in bootloader, see if we can recover */
  1636. if (++recovery_attempts > 1) {
  1637. dev_err(&client->dev, "Could not recover from bootloader mode\n");
  1638. /*
  1639. * We can reflash from this state, so do not
  1640. * abort initialization.
  1641. */
  1642. data->in_bootloader = true;
  1643. return 0;
  1644. }
  1645. /* Attempt to exit bootloader into app mode */
  1646. mxt_send_bootloader_cmd(data, false);
  1647. msleep(MXT_FW_RESET_TIME);
  1648. }
  1649. /* Get object table information */
  1650. error = mxt_get_object_table(data);
  1651. if (error) {
  1652. dev_err(&client->dev, "Error %d reading object table\n", error);
  1653. return error;
  1654. }
  1655. error = mxt_acquire_irq(data);
  1656. if (error)
  1657. goto err_free_object_table;
  1658. error = request_firmware_nowait(THIS_MODULE, true, MXT_CFG_NAME,
  1659. &client->dev, GFP_KERNEL, data,
  1660. mxt_config_cb);
  1661. if (error) {
  1662. dev_err(&client->dev, "Failed to invoke firmware loader: %d\n",
  1663. error);
  1664. goto err_free_object_table;
  1665. }
  1666. return 0;
  1667. err_free_object_table:
  1668. mxt_free_object_table(data);
  1669. return error;
  1670. }
  1671. static int mxt_set_t7_power_cfg(struct mxt_data *data, u8 sleep)
  1672. {
  1673. struct device *dev = &data->client->dev;
  1674. int error;
  1675. struct t7_config *new_config;
  1676. struct t7_config deepsleep = { .active = 0, .idle = 0 };
  1677. if (sleep == MXT_POWER_CFG_DEEPSLEEP)
  1678. new_config = &deepsleep;
  1679. else
  1680. new_config = &data->t7_cfg;
  1681. error = __mxt_write_reg(data->client, data->T7_address,
  1682. sizeof(data->t7_cfg), new_config);
  1683. if (error)
  1684. return error;
  1685. dev_dbg(dev, "Set T7 ACTV:%d IDLE:%d\n",
  1686. new_config->active, new_config->idle);
  1687. return 0;
  1688. }
  1689. static int mxt_init_t7_power_cfg(struct mxt_data *data)
  1690. {
  1691. struct device *dev = &data->client->dev;
  1692. int error;
  1693. bool retry = false;
  1694. recheck:
  1695. error = __mxt_read_reg(data->client, data->T7_address,
  1696. sizeof(data->t7_cfg), &data->t7_cfg);
  1697. if (error)
  1698. return error;
  1699. if (data->t7_cfg.active == 0 || data->t7_cfg.idle == 0) {
  1700. if (!retry) {
  1701. dev_dbg(dev, "T7 cfg zero, resetting\n");
  1702. mxt_soft_reset(data);
  1703. retry = true;
  1704. goto recheck;
  1705. } else {
  1706. dev_dbg(dev, "T7 cfg zero after reset, overriding\n");
  1707. data->t7_cfg.active = 20;
  1708. data->t7_cfg.idle = 100;
  1709. return mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
  1710. }
  1711. }
  1712. dev_dbg(dev, "Initialized power cfg: ACTV %d, IDLE %d\n",
  1713. data->t7_cfg.active, data->t7_cfg.idle);
  1714. return 0;
  1715. }
  1716. static int mxt_configure_objects(struct mxt_data *data,
  1717. const struct firmware *cfg)
  1718. {
  1719. struct device *dev = &data->client->dev;
  1720. struct mxt_info *info = &data->info;
  1721. int error;
  1722. error = mxt_init_t7_power_cfg(data);
  1723. if (error) {
  1724. dev_err(dev, "Failed to initialize power cfg\n");
  1725. return error;
  1726. }
  1727. if (cfg) {
  1728. error = mxt_update_cfg(data, cfg);
  1729. if (error)
  1730. dev_warn(dev, "Error %d updating config\n", error);
  1731. }
  1732. if (data->multitouch) {
  1733. error = mxt_initialize_input_device(data);
  1734. if (error)
  1735. return error;
  1736. } else {
  1737. dev_warn(dev, "No touch object detected\n");
  1738. }
  1739. dev_info(dev,
  1740. "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n",
  1741. info->family_id, info->variant_id, info->version >> 4,
  1742. info->version & 0xf, info->build, info->object_num);
  1743. return 0;
  1744. }
  1745. /* Firmware Version is returned as Major.Minor.Build */
  1746. static ssize_t mxt_fw_version_show(struct device *dev,
  1747. struct device_attribute *attr, char *buf)
  1748. {
  1749. struct mxt_data *data = dev_get_drvdata(dev);
  1750. struct mxt_info *info = &data->info;
  1751. return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
  1752. info->version >> 4, info->version & 0xf, info->build);
  1753. }
  1754. /* Hardware Version is returned as FamilyID.VariantID */
  1755. static ssize_t mxt_hw_version_show(struct device *dev,
  1756. struct device_attribute *attr, char *buf)
  1757. {
  1758. struct mxt_data *data = dev_get_drvdata(dev);
  1759. struct mxt_info *info = &data->info;
  1760. return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
  1761. info->family_id, info->variant_id);
  1762. }
  1763. static ssize_t mxt_show_instance(char *buf, int count,
  1764. struct mxt_object *object, int instance,
  1765. const u8 *val)
  1766. {
  1767. int i;
  1768. if (mxt_obj_instances(object) > 1)
  1769. count += scnprintf(buf + count, PAGE_SIZE - count,
  1770. "Instance %u\n", instance);
  1771. for (i = 0; i < mxt_obj_size(object); i++)
  1772. count += scnprintf(buf + count, PAGE_SIZE - count,
  1773. "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
  1774. count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
  1775. return count;
  1776. }
  1777. static ssize_t mxt_object_show(struct device *dev,
  1778. struct device_attribute *attr, char *buf)
  1779. {
  1780. struct mxt_data *data = dev_get_drvdata(dev);
  1781. struct mxt_object *object;
  1782. int count = 0;
  1783. int i, j;
  1784. int error;
  1785. u8 *obuf;
  1786. /* Pre-allocate buffer large enough to hold max sized object. */
  1787. obuf = kmalloc(256, GFP_KERNEL);
  1788. if (!obuf)
  1789. return -ENOMEM;
  1790. error = 0;
  1791. for (i = 0; i < data->info.object_num; i++) {
  1792. object = data->object_table + i;
  1793. if (!mxt_object_readable(object->type))
  1794. continue;
  1795. count += scnprintf(buf + count, PAGE_SIZE - count,
  1796. "T%u:\n", object->type);
  1797. for (j = 0; j < mxt_obj_instances(object); j++) {
  1798. u16 size = mxt_obj_size(object);
  1799. u16 addr = object->start_address + j * size;
  1800. error = __mxt_read_reg(data->client, addr, size, obuf);
  1801. if (error)
  1802. goto done;
  1803. count = mxt_show_instance(buf, count, object, j, obuf);
  1804. }
  1805. }
  1806. done:
  1807. kfree(obuf);
  1808. return error ?: count;
  1809. }
  1810. static int mxt_check_firmware_format(struct device *dev,
  1811. const struct firmware *fw)
  1812. {
  1813. unsigned int pos = 0;
  1814. char c;
  1815. while (pos < fw->size) {
  1816. c = *(fw->data + pos);
  1817. if (c < '0' || (c > '9' && c < 'A') || c > 'F')
  1818. return 0;
  1819. pos++;
  1820. }
  1821. /*
  1822. * To convert file try:
  1823. * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw
  1824. */
  1825. dev_err(dev, "Aborting: firmware file must be in binary format\n");
  1826. return -EINVAL;
  1827. }
  1828. static int mxt_load_fw(struct device *dev, const char *fn)
  1829. {
  1830. struct mxt_data *data = dev_get_drvdata(dev);
  1831. const struct firmware *fw = NULL;
  1832. unsigned int frame_size;
  1833. unsigned int pos = 0;
  1834. unsigned int retry = 0;
  1835. unsigned int frame = 0;
  1836. int ret;
  1837. ret = request_firmware(&fw, fn, dev);
  1838. if (ret) {
  1839. dev_err(dev, "Unable to open firmware %s\n", fn);
  1840. return ret;
  1841. }
  1842. /* Check for incorrect enc file */
  1843. ret = mxt_check_firmware_format(dev, fw);
  1844. if (ret)
  1845. goto release_firmware;
  1846. if (!data->in_bootloader) {
  1847. /* Change to the bootloader mode */
  1848. data->in_bootloader = true;
  1849. ret = mxt_t6_command(data, MXT_COMMAND_RESET,
  1850. MXT_BOOT_VALUE, false);
  1851. if (ret)
  1852. goto release_firmware;
  1853. msleep(MXT_RESET_TIME);
  1854. /* Do not need to scan since we know family ID */
  1855. ret = mxt_lookup_bootloader_address(data, 0);
  1856. if (ret)
  1857. goto release_firmware;
  1858. mxt_free_input_device(data);
  1859. mxt_free_object_table(data);
  1860. } else {
  1861. enable_irq(data->irq);
  1862. }
  1863. reinit_completion(&data->bl_completion);
  1864. ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD, false);
  1865. if (ret) {
  1866. /* Bootloader may still be unlocked from previous attempt */
  1867. ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, false);
  1868. if (ret)
  1869. goto disable_irq;
  1870. } else {
  1871. dev_info(dev, "Unlocking bootloader\n");
  1872. /* Unlock bootloader */
  1873. ret = mxt_send_bootloader_cmd(data, true);
  1874. if (ret)
  1875. goto disable_irq;
  1876. }
  1877. while (pos < fw->size) {
  1878. ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, true);
  1879. if (ret)
  1880. goto disable_irq;
  1881. frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
  1882. /* Take account of CRC bytes */
  1883. frame_size += 2;
  1884. /* Write one frame to device */
  1885. ret = mxt_bootloader_write(data, fw->data + pos, frame_size);
  1886. if (ret)
  1887. goto disable_irq;
  1888. ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS, true);
  1889. if (ret) {
  1890. retry++;
  1891. /* Back off by 20ms per retry */
  1892. msleep(retry * 20);
  1893. if (retry > 20) {
  1894. dev_err(dev, "Retry count exceeded\n");
  1895. goto disable_irq;
  1896. }
  1897. } else {
  1898. retry = 0;
  1899. pos += frame_size;
  1900. frame++;
  1901. }
  1902. if (frame % 50 == 0)
  1903. dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n",
  1904. frame, pos, fw->size);
  1905. }
  1906. /* Wait for flash. */
  1907. ret = mxt_wait_for_completion(data, &data->bl_completion,
  1908. MXT_FW_RESET_TIME);
  1909. if (ret)
  1910. goto disable_irq;
  1911. dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos);
  1912. /*
  1913. * Wait for device to reset. Some bootloader versions do not assert
  1914. * the CHG line after bootloading has finished, so ignore potential
  1915. * errors.
  1916. */
  1917. mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME);
  1918. data->in_bootloader = false;
  1919. disable_irq:
  1920. disable_irq(data->irq);
  1921. release_firmware:
  1922. release_firmware(fw);
  1923. return ret;
  1924. }
  1925. static ssize_t mxt_update_fw_store(struct device *dev,
  1926. struct device_attribute *attr,
  1927. const char *buf, size_t count)
  1928. {
  1929. struct mxt_data *data = dev_get_drvdata(dev);
  1930. int error;
  1931. error = mxt_load_fw(dev, MXT_FW_NAME);
  1932. if (error) {
  1933. dev_err(dev, "The firmware update failed(%d)\n", error);
  1934. count = error;
  1935. } else {
  1936. dev_info(dev, "The firmware update succeeded\n");
  1937. error = mxt_initialize(data);
  1938. if (error)
  1939. return error;
  1940. }
  1941. return count;
  1942. }
  1943. static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
  1944. static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
  1945. static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
  1946. static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
  1947. static struct attribute *mxt_attrs[] = {
  1948. &dev_attr_fw_version.attr,
  1949. &dev_attr_hw_version.attr,
  1950. &dev_attr_object.attr,
  1951. &dev_attr_update_fw.attr,
  1952. NULL
  1953. };
  1954. static const struct attribute_group mxt_attr_group = {
  1955. .attrs = mxt_attrs,
  1956. };
  1957. static void mxt_start(struct mxt_data *data)
  1958. {
  1959. switch (data->pdata->suspend_mode) {
  1960. case MXT_SUSPEND_T9_CTRL:
  1961. mxt_soft_reset(data);
  1962. /* Touch enable */
  1963. /* 0x83 = SCANEN | RPTEN | ENABLE */
  1964. mxt_write_object(data,
  1965. MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0x83);
  1966. break;
  1967. case MXT_SUSPEND_DEEP_SLEEP:
  1968. default:
  1969. mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
  1970. /* Recalibrate since chip has been in deep sleep */
  1971. mxt_t6_command(data, MXT_COMMAND_CALIBRATE, 1, false);
  1972. break;
  1973. }
  1974. }
  1975. static void mxt_stop(struct mxt_data *data)
  1976. {
  1977. switch (data->pdata->suspend_mode) {
  1978. case MXT_SUSPEND_T9_CTRL:
  1979. /* Touch disable */
  1980. mxt_write_object(data,
  1981. MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0);
  1982. break;
  1983. case MXT_SUSPEND_DEEP_SLEEP:
  1984. default:
  1985. mxt_set_t7_power_cfg(data, MXT_POWER_CFG_DEEPSLEEP);
  1986. break;
  1987. }
  1988. }
  1989. static int mxt_input_open(struct input_dev *dev)
  1990. {
  1991. struct mxt_data *data = input_get_drvdata(dev);
  1992. mxt_start(data);
  1993. return 0;
  1994. }
  1995. static void mxt_input_close(struct input_dev *dev)
  1996. {
  1997. struct mxt_data *data = input_get_drvdata(dev);
  1998. mxt_stop(data);
  1999. }
  2000. #ifdef CONFIG_OF
  2001. static const struct mxt_platform_data *mxt_parse_dt(struct i2c_client *client)
  2002. {
  2003. struct mxt_platform_data *pdata;
  2004. struct device_node *np = client->dev.of_node;
  2005. u32 *keymap;
  2006. int proplen, ret;
  2007. if (!np)
  2008. return ERR_PTR(-ENOENT);
  2009. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  2010. if (!pdata)
  2011. return ERR_PTR(-ENOMEM);
  2012. if (of_find_property(np, "linux,gpio-keymap", &proplen)) {
  2013. pdata->t19_num_keys = proplen / sizeof(u32);
  2014. keymap = devm_kzalloc(&client->dev,
  2015. pdata->t19_num_keys * sizeof(keymap[0]),
  2016. GFP_KERNEL);
  2017. if (!keymap)
  2018. return ERR_PTR(-ENOMEM);
  2019. ret = of_property_read_u32_array(np, "linux,gpio-keymap",
  2020. keymap, pdata->t19_num_keys);
  2021. if (ret)
  2022. dev_warn(&client->dev,
  2023. "Couldn't read linux,gpio-keymap: %d\n", ret);
  2024. pdata->t19_keymap = keymap;
  2025. }
  2026. pdata->suspend_mode = MXT_SUSPEND_DEEP_SLEEP;
  2027. return pdata;
  2028. }
  2029. #else
  2030. static const struct mxt_platform_data *mxt_parse_dt(struct i2c_client *client)
  2031. {
  2032. return ERR_PTR(-ENOENT);
  2033. }
  2034. #endif
  2035. #ifdef CONFIG_ACPI
  2036. struct mxt_acpi_platform_data {
  2037. const char *hid;
  2038. struct mxt_platform_data pdata;
  2039. };
  2040. static unsigned int samus_touchpad_buttons[] = {
  2041. KEY_RESERVED,
  2042. KEY_RESERVED,
  2043. KEY_RESERVED,
  2044. BTN_LEFT
  2045. };
  2046. static struct mxt_acpi_platform_data samus_platform_data[] = {
  2047. {
  2048. /* Touchpad */
  2049. .hid = "ATML0000",
  2050. .pdata = {
  2051. .t19_num_keys = ARRAY_SIZE(samus_touchpad_buttons),
  2052. .t19_keymap = samus_touchpad_buttons,
  2053. },
  2054. },
  2055. {
  2056. /* Touchscreen */
  2057. .hid = "ATML0001",
  2058. },
  2059. { }
  2060. };
  2061. static unsigned int chromebook_tp_buttons[] = {
  2062. KEY_RESERVED,
  2063. KEY_RESERVED,
  2064. KEY_RESERVED,
  2065. KEY_RESERVED,
  2066. KEY_RESERVED,
  2067. BTN_LEFT
  2068. };
  2069. static struct mxt_acpi_platform_data chromebook_platform_data[] = {
  2070. {
  2071. /* Touchpad */
  2072. .hid = "ATML0000",
  2073. .pdata = {
  2074. .t19_num_keys = ARRAY_SIZE(chromebook_tp_buttons),
  2075. .t19_keymap = chromebook_tp_buttons,
  2076. },
  2077. },
  2078. {
  2079. /* Touchscreen */
  2080. .hid = "ATML0001",
  2081. },
  2082. { }
  2083. };
  2084. static const struct dmi_system_id mxt_dmi_table[] = {
  2085. {
  2086. /* 2015 Google Pixel */
  2087. .ident = "Chromebook Pixel 2",
  2088. .matches = {
  2089. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  2090. DMI_MATCH(DMI_PRODUCT_NAME, "Samus"),
  2091. },
  2092. .driver_data = samus_platform_data,
  2093. },
  2094. {
  2095. /* Samsung Chromebook Pro */
  2096. .ident = "Samsung Chromebook Pro",
  2097. .matches = {
  2098. DMI_MATCH(DMI_SYS_VENDOR, "Google"),
  2099. DMI_MATCH(DMI_PRODUCT_NAME, "Caroline"),
  2100. },
  2101. .driver_data = samus_platform_data,
  2102. },
  2103. {
  2104. /* Other Google Chromebooks */
  2105. .ident = "Chromebook",
  2106. .matches = {
  2107. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  2108. },
  2109. .driver_data = chromebook_platform_data,
  2110. },
  2111. { }
  2112. };
  2113. static const struct mxt_platform_data *mxt_parse_acpi(struct i2c_client *client)
  2114. {
  2115. struct acpi_device *adev;
  2116. const struct dmi_system_id *system_id;
  2117. const struct mxt_acpi_platform_data *acpi_pdata;
  2118. /*
  2119. * Ignore ACPI devices representing bootloader mode.
  2120. *
  2121. * This is a bit of a hack: Google Chromebook BIOS creates ACPI
  2122. * devices for both application and bootloader modes, but we are
  2123. * interested in application mode only (if device is in bootloader
  2124. * mode we'll end up switching into application anyway). So far
  2125. * application mode addresses were all above 0x40, so we'll use it
  2126. * as a threshold.
  2127. */
  2128. if (client->addr < 0x40)
  2129. return ERR_PTR(-ENXIO);
  2130. adev = ACPI_COMPANION(&client->dev);
  2131. if (!adev)
  2132. return ERR_PTR(-ENOENT);
  2133. system_id = dmi_first_match(mxt_dmi_table);
  2134. if (!system_id)
  2135. return ERR_PTR(-ENOENT);
  2136. acpi_pdata = system_id->driver_data;
  2137. if (!acpi_pdata)
  2138. return ERR_PTR(-ENOENT);
  2139. while (acpi_pdata->hid) {
  2140. if (!strcmp(acpi_device_hid(adev), acpi_pdata->hid))
  2141. return &acpi_pdata->pdata;
  2142. acpi_pdata++;
  2143. }
  2144. return ERR_PTR(-ENOENT);
  2145. }
  2146. #else
  2147. static const struct mxt_platform_data *mxt_parse_acpi(struct i2c_client *client)
  2148. {
  2149. return ERR_PTR(-ENOENT);
  2150. }
  2151. #endif
  2152. static const struct mxt_platform_data *
  2153. mxt_get_platform_data(struct i2c_client *client)
  2154. {
  2155. const struct mxt_platform_data *pdata;
  2156. pdata = dev_get_platdata(&client->dev);
  2157. if (pdata)
  2158. return pdata;
  2159. pdata = mxt_parse_dt(client);
  2160. if (!IS_ERR(pdata) || PTR_ERR(pdata) != -ENOENT)
  2161. return pdata;
  2162. pdata = mxt_parse_acpi(client);
  2163. if (!IS_ERR(pdata) || PTR_ERR(pdata) != -ENOENT)
  2164. return pdata;
  2165. dev_err(&client->dev, "No platform data specified\n");
  2166. return ERR_PTR(-EINVAL);
  2167. }
  2168. static int mxt_probe(struct i2c_client *client, const struct i2c_device_id *id)
  2169. {
  2170. struct mxt_data *data;
  2171. const struct mxt_platform_data *pdata;
  2172. int error;
  2173. pdata = mxt_get_platform_data(client);
  2174. if (IS_ERR(pdata))
  2175. return PTR_ERR(pdata);
  2176. data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
  2177. if (!data) {
  2178. dev_err(&client->dev, "Failed to allocate memory\n");
  2179. return -ENOMEM;
  2180. }
  2181. snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
  2182. client->adapter->nr, client->addr);
  2183. data->client = client;
  2184. data->pdata = pdata;
  2185. data->irq = client->irq;
  2186. i2c_set_clientdata(client, data);
  2187. init_completion(&data->bl_completion);
  2188. init_completion(&data->reset_completion);
  2189. init_completion(&data->crc_completion);
  2190. error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
  2191. pdata->irqflags | IRQF_ONESHOT,
  2192. client->name, data);
  2193. if (error) {
  2194. dev_err(&client->dev, "Failed to register interrupt\n");
  2195. goto err_free_mem;
  2196. }
  2197. disable_irq(client->irq);
  2198. error = mxt_initialize(data);
  2199. if (error)
  2200. goto err_free_irq;
  2201. error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
  2202. if (error) {
  2203. dev_err(&client->dev, "Failure %d creating sysfs group\n",
  2204. error);
  2205. goto err_free_object;
  2206. }
  2207. return 0;
  2208. err_free_object:
  2209. mxt_free_input_device(data);
  2210. mxt_free_object_table(data);
  2211. err_free_irq:
  2212. free_irq(client->irq, data);
  2213. err_free_mem:
  2214. kfree(data);
  2215. return error;
  2216. }
  2217. static int mxt_remove(struct i2c_client *client)
  2218. {
  2219. struct mxt_data *data = i2c_get_clientdata(client);
  2220. sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
  2221. free_irq(data->irq, data);
  2222. mxt_free_input_device(data);
  2223. mxt_free_object_table(data);
  2224. kfree(data);
  2225. return 0;
  2226. }
  2227. static int __maybe_unused mxt_suspend(struct device *dev)
  2228. {
  2229. struct i2c_client *client = to_i2c_client(dev);
  2230. struct mxt_data *data = i2c_get_clientdata(client);
  2231. struct input_dev *input_dev = data->input_dev;
  2232. if (!input_dev)
  2233. return 0;
  2234. mutex_lock(&input_dev->mutex);
  2235. if (input_dev->users)
  2236. mxt_stop(data);
  2237. mutex_unlock(&input_dev->mutex);
  2238. return 0;
  2239. }
  2240. static int __maybe_unused mxt_resume(struct device *dev)
  2241. {
  2242. struct i2c_client *client = to_i2c_client(dev);
  2243. struct mxt_data *data = i2c_get_clientdata(client);
  2244. struct input_dev *input_dev = data->input_dev;
  2245. if (!input_dev)
  2246. return 0;
  2247. mutex_lock(&input_dev->mutex);
  2248. if (input_dev->users)
  2249. mxt_start(data);
  2250. mutex_unlock(&input_dev->mutex);
  2251. return 0;
  2252. }
  2253. static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
  2254. static const struct of_device_id mxt_of_match[] = {
  2255. { .compatible = "atmel,maxtouch", },
  2256. {},
  2257. };
  2258. MODULE_DEVICE_TABLE(of, mxt_of_match);
  2259. #ifdef CONFIG_ACPI
  2260. static const struct acpi_device_id mxt_acpi_id[] = {
  2261. { "ATML0000", 0 }, /* Touchpad */
  2262. { "ATML0001", 0 }, /* Touchscreen */
  2263. { }
  2264. };
  2265. MODULE_DEVICE_TABLE(acpi, mxt_acpi_id);
  2266. #endif
  2267. static const struct i2c_device_id mxt_id[] = {
  2268. { "qt602240_ts", 0 },
  2269. { "atmel_mxt_ts", 0 },
  2270. { "atmel_mxt_tp", 0 },
  2271. { "maxtouch", 0 },
  2272. { "mXT224", 0 },
  2273. { }
  2274. };
  2275. MODULE_DEVICE_TABLE(i2c, mxt_id);
  2276. static struct i2c_driver mxt_driver = {
  2277. .driver = {
  2278. .name = "atmel_mxt_ts",
  2279. .of_match_table = of_match_ptr(mxt_of_match),
  2280. .acpi_match_table = ACPI_PTR(mxt_acpi_id),
  2281. .pm = &mxt_pm_ops,
  2282. },
  2283. .probe = mxt_probe,
  2284. .remove = mxt_remove,
  2285. .id_table = mxt_id,
  2286. };
  2287. module_i2c_driver(mxt_driver);
  2288. /* Module information */
  2289. MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
  2290. MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
  2291. MODULE_LICENSE("GPL");