synaptics_i2c_rmi4.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700
  1. /*
  2. * Synaptics RMI4 touchscreen driver
  3. *
  4. * Copyright (C) 2012 Synaptics Incorporated
  5. *
  6. * Copyright (C) 2012 Alexandra Chin <alexandra.chin@tw.synaptics.com>
  7. * Copyright (C) 2012 Scott Lin <scott.lin@tw.synaptics.com>
  8. * Copyright (c) 2013, The Linux Foundation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/i2c.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/delay.h>
  26. #include <linux/input.h>
  27. #include <linux/gpio.h>
  28. #include <linux/regulator/consumer.h>
  29. #include <linux/input/synaptics_dsx.h>
  30. #include <linux/of_gpio.h>
  31. #include "synaptics_i2c_rmi4.h"
  32. #include <linux/input/mt.h>
  33. #define DRIVER_NAME "synaptics_rmi4_i2c"
  34. #define INPUT_PHYS_NAME "synaptics_rmi4_i2c/input0"
  35. #define RESET_DELAY 100
  36. #define TYPE_B_PROTOCOL
  37. #define NO_0D_WHILE_2D
  38. /*
  39. #define REPORT_2D_Z
  40. */
  41. #define REPORT_2D_W
  42. #define RPT_TYPE (1 << 0)
  43. #define RPT_X_LSB (1 << 1)
  44. #define RPT_X_MSB (1 << 2)
  45. #define RPT_Y_LSB (1 << 3)
  46. #define RPT_Y_MSB (1 << 4)
  47. #define RPT_Z (1 << 5)
  48. #define RPT_WX (1 << 6)
  49. #define RPT_WY (1 << 7)
  50. #define RPT_DEFAULT (RPT_TYPE | RPT_X_LSB | RPT_X_MSB | RPT_Y_LSB | RPT_Y_MSB)
  51. #define EXP_FN_DET_INTERVAL 1000 /* ms */
  52. #define POLLING_PERIOD 1 /* ms */
  53. #define SYN_I2C_RETRY_TIMES 10
  54. #define MAX_ABS_MT_TOUCH_MAJOR 15
  55. #define F01_STD_QUERY_LEN 21
  56. #define F01_BUID_ID_OFFSET 18
  57. #define F11_STD_QUERY_LEN 9
  58. #define F11_STD_CTRL_LEN 10
  59. #define F11_STD_DATA_LEN 12
  60. #define NORMAL_OPERATION (0 << 0)
  61. #define SENSOR_SLEEP (1 << 0)
  62. #define NO_SLEEP_OFF (0 << 2)
  63. #define NO_SLEEP_ON (1 << 2)
  64. enum device_status {
  65. STATUS_NO_ERROR = 0x00,
  66. STATUS_RESET_OCCURED = 0x01,
  67. STATUS_INVALID_CONFIG = 0x02,
  68. STATUS_DEVICE_FAILURE = 0x03,
  69. STATUS_CONFIG_CRC_FAILURE = 0x04,
  70. STATUS_FIRMWARE_CRC_FAILURE = 0x05,
  71. STATUS_CRC_IN_PROGRESS = 0x06
  72. };
  73. #define RMI4_VTG_MIN_UV 2700000
  74. #define RMI4_VTG_MAX_UV 3300000
  75. #define RMI4_ACTIVE_LOAD_UA 15000
  76. #define RMI4_LPM_LOAD_UA 10
  77. #define RMI4_I2C_VTG_MIN_UV 1800000
  78. #define RMI4_I2C_VTG_MAX_UV 1800000
  79. #define RMI4_I2C_LOAD_UA 10000
  80. #define RMI4_I2C_LPM_LOAD_UA 10
  81. #define RMI4_GPIO_SLEEP_LOW_US 10000
  82. static int synaptics_rmi4_i2c_read(struct synaptics_rmi4_data *rmi4_data,
  83. unsigned short addr, unsigned char *data,
  84. unsigned short length);
  85. static int synaptics_rmi4_i2c_write(struct synaptics_rmi4_data *rmi4_data,
  86. unsigned short addr, unsigned char *data,
  87. unsigned short length);
  88. static int synaptics_rmi4_reset_device(struct synaptics_rmi4_data *rmi4_data);
  89. #ifdef CONFIG_PM
  90. static int synaptics_rmi4_suspend(struct device *dev);
  91. static int synaptics_rmi4_resume(struct device *dev);
  92. #ifdef CONFIG_HAS_EARLYSUSPEND
  93. static ssize_t synaptics_rmi4_full_pm_cycle_show(struct device *dev,
  94. struct device_attribute *attr, char *buf);
  95. static ssize_t synaptics_rmi4_full_pm_cycle_store(struct device *dev,
  96. struct device_attribute *attr, const char *buf, size_t count);
  97. static void synaptics_rmi4_early_suspend(struct early_suspend *h);
  98. static void synaptics_rmi4_late_resume(struct early_suspend *h);
  99. #endif
  100. #endif
  101. static ssize_t synaptics_rmi4_f01_reset_store(struct device *dev,
  102. struct device_attribute *attr, const char *buf, size_t count);
  103. static ssize_t synaptics_rmi4_f01_productinfo_show(struct device *dev,
  104. struct device_attribute *attr, char *buf);
  105. static ssize_t synaptics_rmi4_f01_buildid_show(struct device *dev,
  106. struct device_attribute *attr, char *buf);
  107. static ssize_t synaptics_rmi4_f01_flashprog_show(struct device *dev,
  108. struct device_attribute *attr, char *buf);
  109. static ssize_t synaptics_rmi4_0dbutton_show(struct device *dev,
  110. struct device_attribute *attr, char *buf);
  111. static ssize_t synaptics_rmi4_0dbutton_store(struct device *dev,
  112. struct device_attribute *attr, const char *buf, size_t count);
  113. static ssize_t synaptics_rmi4_flipx_show(struct device *dev,
  114. struct device_attribute *attr, char *buf);
  115. static ssize_t synaptics_rmi4_flipx_store(struct device *dev,
  116. struct device_attribute *attr, const char *buf, size_t count);
  117. static ssize_t synaptics_rmi4_flipy_show(struct device *dev,
  118. struct device_attribute *attr, char *buf);
  119. static ssize_t synaptics_rmi4_flipy_store(struct device *dev,
  120. struct device_attribute *attr, const char *buf, size_t count);
  121. struct synaptics_rmi4_f01_device_status {
  122. union {
  123. struct {
  124. unsigned char status_code:4;
  125. unsigned char reserved:2;
  126. unsigned char flash_prog:1;
  127. unsigned char unconfigured:1;
  128. } __packed;
  129. unsigned char data[1];
  130. };
  131. };
  132. struct synaptics_rmi4_f1a_query {
  133. union {
  134. struct {
  135. unsigned char max_button_count:3;
  136. unsigned char reserved:5;
  137. unsigned char has_general_control:1;
  138. unsigned char has_interrupt_enable:1;
  139. unsigned char has_multibutton_select:1;
  140. unsigned char has_tx_rx_map:1;
  141. unsigned char has_perbutton_threshold:1;
  142. unsigned char has_release_threshold:1;
  143. unsigned char has_strongestbtn_hysteresis:1;
  144. unsigned char has_filter_strength:1;
  145. } __packed;
  146. unsigned char data[2];
  147. };
  148. };
  149. struct synaptics_rmi4_f1a_control_0 {
  150. union {
  151. struct {
  152. unsigned char multibutton_report:2;
  153. unsigned char filter_mode:2;
  154. unsigned char reserved:4;
  155. } __packed;
  156. unsigned char data[1];
  157. };
  158. };
  159. struct synaptics_rmi4_f1a_control_3_4 {
  160. unsigned char transmitterbutton;
  161. unsigned char receiverbutton;
  162. };
  163. struct synaptics_rmi4_f1a_control {
  164. struct synaptics_rmi4_f1a_control_0 general_control;
  165. unsigned char *button_int_enable;
  166. unsigned char *multi_button;
  167. struct synaptics_rmi4_f1a_control_3_4 *electrode_map;
  168. unsigned char *button_threshold;
  169. unsigned char button_release_threshold;
  170. unsigned char strongest_button_hysteresis;
  171. unsigned char filter_strength;
  172. };
  173. struct synaptics_rmi4_f1a_handle {
  174. int button_bitmask_size;
  175. unsigned char button_count;
  176. unsigned char valid_button_count;
  177. unsigned char *button_data_buffer;
  178. unsigned char *button_map;
  179. struct synaptics_rmi4_f1a_query button_query;
  180. struct synaptics_rmi4_f1a_control button_control;
  181. };
  182. struct synaptics_rmi4_exp_fn {
  183. enum exp_fn fn_type;
  184. bool inserted;
  185. int (*func_init)(struct synaptics_rmi4_data *rmi4_data);
  186. void (*func_remove)(struct synaptics_rmi4_data *rmi4_data);
  187. void (*func_attn)(struct synaptics_rmi4_data *rmi4_data,
  188. unsigned char intr_mask);
  189. struct list_head link;
  190. };
  191. static struct device_attribute attrs[] = {
  192. #ifdef CONFIG_HAS_EARLYSUSPEND
  193. __ATTR(full_pm_cycle, (S_IRUGO | S_IWUGO),
  194. synaptics_rmi4_full_pm_cycle_show,
  195. synaptics_rmi4_full_pm_cycle_store),
  196. #endif
  197. __ATTR(reset, S_IWUGO,
  198. synaptics_rmi4_show_error,
  199. synaptics_rmi4_f01_reset_store),
  200. __ATTR(productinfo, S_IRUGO,
  201. synaptics_rmi4_f01_productinfo_show,
  202. synaptics_rmi4_store_error),
  203. __ATTR(buildid, S_IRUGO,
  204. synaptics_rmi4_f01_buildid_show,
  205. synaptics_rmi4_store_error),
  206. __ATTR(flashprog, S_IRUGO,
  207. synaptics_rmi4_f01_flashprog_show,
  208. synaptics_rmi4_store_error),
  209. __ATTR(0dbutton, (S_IRUGO | S_IWUGO),
  210. synaptics_rmi4_0dbutton_show,
  211. synaptics_rmi4_0dbutton_store),
  212. __ATTR(flipx, (S_IRUGO | S_IWUGO),
  213. synaptics_rmi4_flipx_show,
  214. synaptics_rmi4_flipx_store),
  215. __ATTR(flipy, (S_IRUGO | S_IWUGO),
  216. synaptics_rmi4_flipy_show,
  217. synaptics_rmi4_flipy_store),
  218. };
  219. static bool exp_fn_inited;
  220. static struct mutex exp_fn_list_mutex;
  221. static struct list_head exp_fn_list;
  222. #ifdef CONFIG_HAS_EARLYSUSPEND
  223. static ssize_t synaptics_rmi4_full_pm_cycle_show(struct device *dev,
  224. struct device_attribute *attr, char *buf)
  225. {
  226. struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
  227. return snprintf(buf, PAGE_SIZE, "%u\n",
  228. rmi4_data->full_pm_cycle);
  229. }
  230. static ssize_t synaptics_rmi4_full_pm_cycle_store(struct device *dev,
  231. struct device_attribute *attr, const char *buf, size_t count)
  232. {
  233. unsigned int input;
  234. struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
  235. if (sscanf(buf, "%u", &input) != 1)
  236. return -EINVAL;
  237. rmi4_data->full_pm_cycle = input > 0 ? 1 : 0;
  238. return count;
  239. }
  240. #endif
  241. static ssize_t synaptics_rmi4_f01_reset_store(struct device *dev,
  242. struct device_attribute *attr, const char *buf, size_t count)
  243. {
  244. int retval;
  245. unsigned int reset;
  246. struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
  247. if (sscanf(buf, "%u", &reset) != 1)
  248. return -EINVAL;
  249. if (reset != 1)
  250. return -EINVAL;
  251. retval = synaptics_rmi4_reset_device(rmi4_data);
  252. if (retval < 0) {
  253. dev_err(dev,
  254. "%s: Failed to issue reset command, error = %d\n",
  255. __func__, retval);
  256. return retval;
  257. }
  258. return count;
  259. }
  260. static ssize_t synaptics_rmi4_f01_productinfo_show(struct device *dev,
  261. struct device_attribute *attr, char *buf)
  262. {
  263. struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
  264. return snprintf(buf, PAGE_SIZE, "0x%02x 0x%02x\n",
  265. (rmi4_data->rmi4_mod_info.product_info[0]),
  266. (rmi4_data->rmi4_mod_info.product_info[1]));
  267. }
  268. static ssize_t synaptics_rmi4_f01_buildid_show(struct device *dev,
  269. struct device_attribute *attr, char *buf)
  270. {
  271. unsigned int build_id;
  272. struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
  273. struct synaptics_rmi4_device_info *rmi;
  274. rmi = &(rmi4_data->rmi4_mod_info);
  275. build_id = (unsigned int)rmi->build_id[0] +
  276. (unsigned int)rmi->build_id[1] * 0x100 +
  277. (unsigned int)rmi->build_id[2] * 0x10000;
  278. return snprintf(buf, PAGE_SIZE, "%u\n",
  279. build_id);
  280. }
  281. static ssize_t synaptics_rmi4_f01_flashprog_show(struct device *dev,
  282. struct device_attribute *attr, char *buf)
  283. {
  284. int retval;
  285. struct synaptics_rmi4_f01_device_status device_status;
  286. struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
  287. retval = synaptics_rmi4_i2c_read(rmi4_data,
  288. rmi4_data->f01_data_base_addr,
  289. device_status.data,
  290. sizeof(device_status.data));
  291. if (retval < 0) {
  292. dev_err(dev,
  293. "%s: Failed to read device status, error = %d\n",
  294. __func__, retval);
  295. return retval;
  296. }
  297. return snprintf(buf, PAGE_SIZE, "%u\n",
  298. device_status.flash_prog);
  299. }
  300. static ssize_t synaptics_rmi4_0dbutton_show(struct device *dev,
  301. struct device_attribute *attr, char *buf)
  302. {
  303. struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
  304. return snprintf(buf, PAGE_SIZE, "%u\n",
  305. rmi4_data->button_0d_enabled);
  306. }
  307. static ssize_t synaptics_rmi4_0dbutton_store(struct device *dev,
  308. struct device_attribute *attr, const char *buf, size_t count)
  309. {
  310. int retval;
  311. unsigned int input;
  312. unsigned char ii;
  313. unsigned char intr_enable;
  314. struct synaptics_rmi4_fn *fhandler;
  315. struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
  316. struct synaptics_rmi4_device_info *rmi;
  317. rmi = &(rmi4_data->rmi4_mod_info);
  318. if (sscanf(buf, "%u", &input) != 1)
  319. return -EINVAL;
  320. input = input > 0 ? 1 : 0;
  321. if (rmi4_data->button_0d_enabled == input)
  322. return count;
  323. if (!list_empty(&rmi->support_fn_list)) {
  324. list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
  325. if (fhandler->fn_number == SYNAPTICS_RMI4_F1A) {
  326. ii = fhandler->intr_reg_num;
  327. retval = synaptics_rmi4_i2c_read(rmi4_data,
  328. rmi4_data->f01_ctrl_base_addr +
  329. 1 + ii,
  330. &intr_enable,
  331. sizeof(intr_enable));
  332. if (retval < 0)
  333. return retval;
  334. if (input == 1)
  335. intr_enable |= fhandler->intr_mask;
  336. else
  337. intr_enable &= ~fhandler->intr_mask;
  338. retval = synaptics_rmi4_i2c_write(rmi4_data,
  339. rmi4_data->f01_ctrl_base_addr +
  340. 1 + ii,
  341. &intr_enable,
  342. sizeof(intr_enable));
  343. if (retval < 0)
  344. return retval;
  345. }
  346. }
  347. }
  348. rmi4_data->button_0d_enabled = input;
  349. return count;
  350. }
  351. static ssize_t synaptics_rmi4_flipx_show(struct device *dev,
  352. struct device_attribute *attr, char *buf)
  353. {
  354. struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
  355. return snprintf(buf, PAGE_SIZE, "%u\n",
  356. rmi4_data->flip_x);
  357. }
  358. static ssize_t synaptics_rmi4_flipx_store(struct device *dev,
  359. struct device_attribute *attr, const char *buf, size_t count)
  360. {
  361. unsigned int input;
  362. struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
  363. if (sscanf(buf, "%u", &input) != 1)
  364. return -EINVAL;
  365. rmi4_data->flip_x = input > 0 ? 1 : 0;
  366. return count;
  367. }
  368. static ssize_t synaptics_rmi4_flipy_show(struct device *dev,
  369. struct device_attribute *attr, char *buf)
  370. {
  371. struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
  372. return snprintf(buf, PAGE_SIZE, "%u\n",
  373. rmi4_data->flip_y);
  374. }
  375. static ssize_t synaptics_rmi4_flipy_store(struct device *dev,
  376. struct device_attribute *attr, const char *buf, size_t count)
  377. {
  378. unsigned int input;
  379. struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
  380. if (sscanf(buf, "%u", &input) != 1)
  381. return -EINVAL;
  382. rmi4_data->flip_y = input > 0 ? 1 : 0;
  383. return count;
  384. }
  385. /**
  386. * synaptics_rmi4_set_page()
  387. *
  388. * Called by synaptics_rmi4_i2c_read() and synaptics_rmi4_i2c_write().
  389. *
  390. * This function writes to the page select register to switch to the
  391. * assigned page.
  392. */
  393. static int synaptics_rmi4_set_page(struct synaptics_rmi4_data *rmi4_data,
  394. unsigned int address)
  395. {
  396. int retval = 0;
  397. unsigned char retry;
  398. unsigned char buf[PAGE_SELECT_LEN];
  399. unsigned char page;
  400. struct i2c_client *i2c = rmi4_data->i2c_client;
  401. page = ((address >> 8) & MASK_8BIT);
  402. if (page != rmi4_data->current_page) {
  403. buf[0] = MASK_8BIT;
  404. buf[1] = page;
  405. for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
  406. retval = i2c_master_send(i2c, buf, PAGE_SELECT_LEN);
  407. if (retval != PAGE_SELECT_LEN) {
  408. dev_err(&i2c->dev,
  409. "%s: I2C retry %d\n",
  410. __func__, retry + 1);
  411. msleep(20);
  412. } else {
  413. rmi4_data->current_page = page;
  414. break;
  415. }
  416. }
  417. } else
  418. return PAGE_SELECT_LEN;
  419. return (retval == PAGE_SELECT_LEN) ? retval : -EIO;
  420. }
  421. /**
  422. * synaptics_rmi4_i2c_read()
  423. *
  424. * Called by various functions in this driver, and also exported to
  425. * other expansion Function modules such as rmi_dev.
  426. *
  427. * This function reads data of an arbitrary length from the sensor,
  428. * starting from an assigned register address of the sensor, via I2C
  429. * with a retry mechanism.
  430. */
  431. static int synaptics_rmi4_i2c_read(struct synaptics_rmi4_data *rmi4_data,
  432. unsigned short addr, unsigned char *data, unsigned short length)
  433. {
  434. int retval;
  435. unsigned char retry;
  436. unsigned char buf;
  437. struct i2c_msg msg[] = {
  438. {
  439. .addr = rmi4_data->i2c_client->addr,
  440. .flags = 0,
  441. .len = 1,
  442. .buf = &buf,
  443. },
  444. {
  445. .addr = rmi4_data->i2c_client->addr,
  446. .flags = I2C_M_RD,
  447. .len = length,
  448. .buf = data,
  449. },
  450. };
  451. buf = addr & MASK_8BIT;
  452. mutex_lock(&(rmi4_data->rmi4_io_ctrl_mutex));
  453. retval = synaptics_rmi4_set_page(rmi4_data, addr);
  454. if (retval != PAGE_SELECT_LEN)
  455. goto exit;
  456. for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
  457. if (i2c_transfer(rmi4_data->i2c_client->adapter, msg, 2) == 2) {
  458. retval = length;
  459. break;
  460. }
  461. dev_err(&rmi4_data->i2c_client->dev,
  462. "%s: I2C retry %d\n",
  463. __func__, retry + 1);
  464. msleep(20);
  465. }
  466. if (retry == SYN_I2C_RETRY_TIMES) {
  467. dev_err(&rmi4_data->i2c_client->dev,
  468. "%s: I2C read over retry limit\n",
  469. __func__);
  470. retval = -EIO;
  471. }
  472. exit:
  473. mutex_unlock(&(rmi4_data->rmi4_io_ctrl_mutex));
  474. return retval;
  475. }
  476. /**
  477. * synaptics_rmi4_i2c_write()
  478. *
  479. * Called by various functions in this driver, and also exported to
  480. * other expansion Function modules such as rmi_dev.
  481. *
  482. * This function writes data of an arbitrary length to the sensor,
  483. * starting from an assigned register address of the sensor, via I2C with
  484. * a retry mechanism.
  485. */
  486. static int synaptics_rmi4_i2c_write(struct synaptics_rmi4_data *rmi4_data,
  487. unsigned short addr, unsigned char *data, unsigned short length)
  488. {
  489. int retval;
  490. unsigned char retry;
  491. unsigned char buf[length + 1];
  492. struct i2c_msg msg[] = {
  493. {
  494. .addr = rmi4_data->i2c_client->addr,
  495. .flags = 0,
  496. .len = length + 1,
  497. .buf = buf,
  498. }
  499. };
  500. mutex_lock(&(rmi4_data->rmi4_io_ctrl_mutex));
  501. retval = synaptics_rmi4_set_page(rmi4_data, addr);
  502. if (retval != PAGE_SELECT_LEN)
  503. goto exit;
  504. buf[0] = addr & MASK_8BIT;
  505. memcpy(&buf[1], &data[0], length);
  506. for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
  507. if (i2c_transfer(rmi4_data->i2c_client->adapter, msg, 1) == 1) {
  508. retval = length;
  509. break;
  510. }
  511. dev_err(&rmi4_data->i2c_client->dev,
  512. "%s: I2C retry %d\n",
  513. __func__, retry + 1);
  514. msleep(20);
  515. }
  516. if (retry == SYN_I2C_RETRY_TIMES) {
  517. dev_err(&rmi4_data->i2c_client->dev,
  518. "%s: I2C write over retry limit\n",
  519. __func__);
  520. retval = -EIO;
  521. }
  522. exit:
  523. mutex_unlock(&(rmi4_data->rmi4_io_ctrl_mutex));
  524. return retval;
  525. }
  526. /**
  527. * synaptics_rmi4_f11_abs_report()
  528. *
  529. * Called by synaptics_rmi4_report_touch() when valid Function $11
  530. * finger data has been detected.
  531. *
  532. * This function reads the Function $11 data registers, determines the
  533. * status of each finger supported by the Function, processes any
  534. * necessary coordinate manipulation, reports the finger data to
  535. * the input subsystem, and returns the number of fingers detected.
  536. */
  537. static int synaptics_rmi4_f11_abs_report(struct synaptics_rmi4_data *rmi4_data,
  538. struct synaptics_rmi4_fn *fhandler)
  539. {
  540. int retval;
  541. unsigned char touch_count = 0; /* number of touch points */
  542. unsigned char reg_index;
  543. unsigned char finger;
  544. unsigned char fingers_supported;
  545. unsigned char num_of_finger_status_regs;
  546. unsigned char finger_shift;
  547. unsigned char finger_status;
  548. unsigned char data_reg_blk_size;
  549. unsigned char finger_status_reg[3];
  550. unsigned char data[F11_STD_DATA_LEN];
  551. unsigned short data_addr;
  552. unsigned short data_offset;
  553. int x;
  554. int y;
  555. int wx;
  556. int wy;
  557. int z;
  558. /*
  559. * The number of finger status registers is determined by the
  560. * maximum number of fingers supported - 2 bits per finger. So
  561. * the number of finger status registers to read is:
  562. * register_count = ceil(max_num_of_fingers / 4)
  563. */
  564. fingers_supported = fhandler->num_of_data_points;
  565. num_of_finger_status_regs = (fingers_supported + 3) / 4;
  566. data_addr = fhandler->full_addr.data_base;
  567. data_reg_blk_size = fhandler->size_of_data_register_block;
  568. retval = synaptics_rmi4_i2c_read(rmi4_data,
  569. data_addr,
  570. finger_status_reg,
  571. num_of_finger_status_regs);
  572. if (retval < 0)
  573. return 0;
  574. for (finger = 0; finger < fingers_supported; finger++) {
  575. reg_index = finger / 4;
  576. finger_shift = (finger % 4) * 2;
  577. finger_status = (finger_status_reg[reg_index] >> finger_shift)
  578. & MASK_2BIT;
  579. /*
  580. * Each 2-bit finger status field represents the following:
  581. * 00 = finger not present
  582. * 01 = finger present and data accurate
  583. * 10 = finger present but data may be inaccurate
  584. * 11 = reserved
  585. */
  586. #ifdef TYPE_B_PROTOCOL
  587. input_mt_slot(rmi4_data->input_dev, finger);
  588. input_mt_report_slot_state(rmi4_data->input_dev,
  589. MT_TOOL_FINGER, finger_status != 0);
  590. #endif
  591. if (finger_status) {
  592. data_offset = data_addr +
  593. num_of_finger_status_regs +
  594. (finger * data_reg_blk_size);
  595. retval = synaptics_rmi4_i2c_read(rmi4_data,
  596. data_offset,
  597. data,
  598. data_reg_blk_size);
  599. if (retval < 0)
  600. return 0;
  601. x = (data[0] << 4) | (data[2] & MASK_4BIT);
  602. y = (data[1] << 4) | ((data[2] >> 4) & MASK_4BIT);
  603. wx = (data[3] & MASK_4BIT);
  604. wy = (data[3] >> 4) & MASK_4BIT;
  605. z = data[4];
  606. if (rmi4_data->flip_x)
  607. x = rmi4_data->sensor_max_x - x;
  608. if (rmi4_data->flip_y)
  609. y = rmi4_data->sensor_max_y - y;
  610. dev_dbg(&rmi4_data->i2c_client->dev,
  611. "%s: Finger %d:\n"
  612. "status = 0x%02x\n"
  613. "x = %d\n"
  614. "y = %d\n"
  615. "wx = %d\n"
  616. "wy = %d\n",
  617. __func__, finger,
  618. finger_status,
  619. x, y, wx, wy);
  620. input_report_abs(rmi4_data->input_dev,
  621. ABS_MT_POSITION_X, x);
  622. input_report_abs(rmi4_data->input_dev,
  623. ABS_MT_POSITION_Y, y);
  624. input_report_abs(rmi4_data->input_dev,
  625. ABS_MT_PRESSURE, z);
  626. #ifdef REPORT_2D_W
  627. input_report_abs(rmi4_data->input_dev,
  628. ABS_MT_TOUCH_MAJOR, max(wx, wy));
  629. input_report_abs(rmi4_data->input_dev,
  630. ABS_MT_TOUCH_MINOR, min(wx, wy));
  631. #endif
  632. #ifndef TYPE_B_PROTOCOL
  633. input_mt_sync(rmi4_data->input_dev);
  634. #endif
  635. touch_count++;
  636. }
  637. }
  638. input_report_key(rmi4_data->input_dev, BTN_TOUCH, touch_count > 0);
  639. input_report_key(rmi4_data->input_dev,
  640. BTN_TOOL_FINGER, touch_count > 0);
  641. #ifndef TYPE_B_PROTOCOL
  642. if (!touch_count)
  643. input_mt_sync(rmi4_data->input_dev);
  644. #else
  645. /* sync after groups of events */
  646. #ifdef KERNEL_ABOVE_3_7
  647. input_mt_sync_frame(rmi4_data->input_dev);
  648. #endif
  649. #endif
  650. input_sync(rmi4_data->input_dev);
  651. return touch_count;
  652. }
  653. static void synaptics_rmi4_f1a_report(struct synaptics_rmi4_data *rmi4_data,
  654. struct synaptics_rmi4_fn *fhandler)
  655. {
  656. int retval;
  657. unsigned char button;
  658. unsigned char index;
  659. unsigned char shift;
  660. unsigned char status;
  661. unsigned char *data;
  662. unsigned short data_addr = fhandler->full_addr.data_base;
  663. struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
  664. static unsigned char do_once = 1;
  665. static bool current_status[MAX_NUMBER_OF_BUTTONS];
  666. #ifdef NO_0D_WHILE_2D
  667. static bool before_2d_status[MAX_NUMBER_OF_BUTTONS];
  668. static bool while_2d_status[MAX_NUMBER_OF_BUTTONS];
  669. #endif
  670. if (do_once) {
  671. memset(current_status, 0, sizeof(current_status));
  672. #ifdef NO_0D_WHILE_2D
  673. memset(before_2d_status, 0, sizeof(before_2d_status));
  674. memset(while_2d_status, 0, sizeof(while_2d_status));
  675. #endif
  676. do_once = 0;
  677. }
  678. retval = synaptics_rmi4_i2c_read(rmi4_data,
  679. data_addr,
  680. f1a->button_data_buffer,
  681. f1a->button_bitmask_size);
  682. if (retval < 0) {
  683. dev_err(&rmi4_data->i2c_client->dev,
  684. "%s: Failed to read button data registers\n",
  685. __func__);
  686. return;
  687. }
  688. data = f1a->button_data_buffer;
  689. for (button = 0; button < f1a->valid_button_count; button++) {
  690. index = button / 8;
  691. shift = button % 8;
  692. status = ((data[index] >> shift) & MASK_1BIT);
  693. if (current_status[button] == status)
  694. continue;
  695. else
  696. current_status[button] = status;
  697. dev_dbg(&rmi4_data->i2c_client->dev,
  698. "%s: Button %d (code %d) ->%d\n",
  699. __func__, button,
  700. f1a->button_map[button],
  701. status);
  702. #ifdef NO_0D_WHILE_2D
  703. if (rmi4_data->fingers_on_2d == false) {
  704. if (status == 1) {
  705. before_2d_status[button] = 1;
  706. } else {
  707. if (while_2d_status[button] == 1) {
  708. while_2d_status[button] = 0;
  709. continue;
  710. } else {
  711. before_2d_status[button] = 0;
  712. }
  713. }
  714. input_report_key(rmi4_data->input_dev,
  715. f1a->button_map[button],
  716. status);
  717. } else {
  718. if (before_2d_status[button] == 1) {
  719. before_2d_status[button] = 0;
  720. input_report_key(rmi4_data->input_dev,
  721. f1a->button_map[button],
  722. status);
  723. } else {
  724. if (status == 1)
  725. while_2d_status[button] = 1;
  726. else
  727. while_2d_status[button] = 0;
  728. }
  729. }
  730. #else
  731. input_report_key(rmi4_data->input_dev,
  732. f1a->button_map[button],
  733. status);
  734. #endif
  735. }
  736. input_sync(rmi4_data->input_dev);
  737. return;
  738. }
  739. /**
  740. * synaptics_rmi4_report_touch()
  741. *
  742. * Called by synaptics_rmi4_sensor_report().
  743. *
  744. * This function calls the appropriate finger data reporting function
  745. * based on the function handler it receives and returns the number of
  746. * fingers detected.
  747. */
  748. static void synaptics_rmi4_report_touch(struct synaptics_rmi4_data *rmi4_data,
  749. struct synaptics_rmi4_fn *fhandler,
  750. unsigned char *touch_count)
  751. {
  752. unsigned char touch_count_2d;
  753. dev_dbg(&rmi4_data->i2c_client->dev,
  754. "%s: Function %02x reporting\n",
  755. __func__, fhandler->fn_number);
  756. switch (fhandler->fn_number) {
  757. case SYNAPTICS_RMI4_F11:
  758. touch_count_2d = synaptics_rmi4_f11_abs_report(rmi4_data,
  759. fhandler);
  760. *touch_count += touch_count_2d;
  761. if (touch_count_2d)
  762. rmi4_data->fingers_on_2d = true;
  763. else
  764. rmi4_data->fingers_on_2d = false;
  765. break;
  766. case SYNAPTICS_RMI4_F1A:
  767. synaptics_rmi4_f1a_report(rmi4_data, fhandler);
  768. break;
  769. default:
  770. break;
  771. }
  772. return;
  773. }
  774. /**
  775. * synaptics_rmi4_sensor_report()
  776. *
  777. * Called by synaptics_rmi4_irq().
  778. *
  779. * This function determines the interrupt source(s) from the sensor
  780. * and calls synaptics_rmi4_report_touch() with the appropriate
  781. * function handler for each function with valid data inputs.
  782. */
  783. static int synaptics_rmi4_sensor_report(struct synaptics_rmi4_data *rmi4_data)
  784. {
  785. int retval;
  786. unsigned char touch_count = 0;
  787. unsigned char intr[MAX_INTR_REGISTERS];
  788. struct synaptics_rmi4_fn *fhandler;
  789. struct synaptics_rmi4_exp_fn *exp_fhandler;
  790. struct synaptics_rmi4_device_info *rmi;
  791. rmi = &(rmi4_data->rmi4_mod_info);
  792. /*
  793. * Get interrupt status information from F01 Data1 register to
  794. * determine the source(s) that are flagging the interrupt.
  795. */
  796. retval = synaptics_rmi4_i2c_read(rmi4_data,
  797. rmi4_data->f01_data_base_addr + 1,
  798. intr,
  799. rmi4_data->num_of_intr_regs);
  800. if (retval < 0)
  801. return retval;
  802. /*
  803. * Traverse the function handler list and service the source(s)
  804. * of the interrupt accordingly.
  805. */
  806. if (!list_empty(&rmi->support_fn_list)) {
  807. list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
  808. if (fhandler->num_of_data_sources) {
  809. if (fhandler->intr_mask &
  810. intr[fhandler->intr_reg_num]) {
  811. synaptics_rmi4_report_touch(rmi4_data,
  812. fhandler, &touch_count);
  813. }
  814. }
  815. }
  816. }
  817. mutex_lock(&exp_fn_list_mutex);
  818. if (!list_empty(&exp_fn_list)) {
  819. list_for_each_entry(exp_fhandler, &exp_fn_list, link) {
  820. if (exp_fhandler->inserted &&
  821. (exp_fhandler->func_attn != NULL))
  822. exp_fhandler->func_attn(rmi4_data, intr[0]);
  823. }
  824. }
  825. mutex_unlock(&exp_fn_list_mutex);
  826. return touch_count;
  827. }
  828. /**
  829. * synaptics_rmi4_irq()
  830. *
  831. * Called by the kernel when an interrupt occurs (when the sensor
  832. * asserts the attention irq).
  833. *
  834. * This function is the ISR thread and handles the acquisition
  835. * and the reporting of finger data when the presence of fingers
  836. * is detected.
  837. */
  838. static irqreturn_t synaptics_rmi4_irq(int irq, void *data)
  839. {
  840. struct synaptics_rmi4_data *rmi4_data = data;
  841. synaptics_rmi4_sensor_report(rmi4_data);
  842. return IRQ_HANDLED;
  843. }
  844. static int synaptics_rmi4_parse_dt(struct device *dev,
  845. struct synaptics_rmi4_platform_data *rmi4_pdata)
  846. {
  847. struct device_node *np = dev->of_node;
  848. struct property *prop;
  849. u32 temp_val, num_buttons;
  850. u32 button_map[MAX_NUMBER_OF_BUTTONS];
  851. int rc, i;
  852. rmi4_pdata->i2c_pull_up = of_property_read_bool(np,
  853. "synaptics,i2c-pull-up");
  854. rmi4_pdata->regulator_en = of_property_read_bool(np,
  855. "synaptics,reg-en");
  856. rmi4_pdata->x_flip = of_property_read_bool(np, "synaptics,x-flip");
  857. rmi4_pdata->y_flip = of_property_read_bool(np, "synaptics,y-flip");
  858. rc = of_property_read_u32(np, "synaptics,panel-x", &temp_val);
  859. if (rc && (rc != -EINVAL)) {
  860. dev_err(dev, "Unable to read panel X dimension\n");
  861. return rc;
  862. } else {
  863. rmi4_pdata->panel_x = temp_val;
  864. }
  865. rc = of_property_read_u32(np, "synaptics,panel-y", &temp_val);
  866. if (rc && (rc != -EINVAL)) {
  867. dev_err(dev, "Unable to read panel Y dimension\n");
  868. return rc;
  869. } else {
  870. rmi4_pdata->panel_y = temp_val;
  871. }
  872. rc = of_property_read_string(np, "synaptics,fw-image-name",
  873. &rmi4_pdata->fw_image_name);
  874. if (rc && (rc != -EINVAL)) {
  875. dev_err(dev, "Unable to read fw image name\n");
  876. return rc;
  877. }
  878. /* reset, irq gpio info */
  879. rmi4_pdata->reset_gpio = of_get_named_gpio_flags(np,
  880. "synaptics,reset-gpio", 0, &rmi4_pdata->reset_flags);
  881. rmi4_pdata->irq_gpio = of_get_named_gpio_flags(np,
  882. "synaptics,irq-gpio", 0, &rmi4_pdata->irq_flags);
  883. prop = of_find_property(np, "synaptics,button-map", NULL);
  884. if (prop) {
  885. num_buttons = prop->length / sizeof(temp_val);
  886. rmi4_pdata->capacitance_button_map = devm_kzalloc(dev,
  887. sizeof(*rmi4_pdata->capacitance_button_map),
  888. GFP_KERNEL);
  889. if (!rmi4_pdata->capacitance_button_map)
  890. return -ENOMEM;
  891. rmi4_pdata->capacitance_button_map->map = devm_kzalloc(dev,
  892. sizeof(*rmi4_pdata->capacitance_button_map->map) *
  893. MAX_NUMBER_OF_BUTTONS, GFP_KERNEL);
  894. if (!rmi4_pdata->capacitance_button_map->map)
  895. return -ENOMEM;
  896. if (num_buttons <= MAX_NUMBER_OF_BUTTONS) {
  897. rc = of_property_read_u32_array(np,
  898. "synaptics,button-map", button_map,
  899. num_buttons);
  900. if (rc) {
  901. dev_err(dev, "Unable to read key codes\n");
  902. return rc;
  903. }
  904. for (i = 0; i < num_buttons; i++)
  905. rmi4_pdata->capacitance_button_map->map[i] =
  906. button_map[i];
  907. rmi4_pdata->capacitance_button_map->nbuttons =
  908. num_buttons;
  909. } else {
  910. return -EINVAL;
  911. }
  912. }
  913. return 0;
  914. }
  915. /**
  916. * synaptics_rmi4_irq_enable()
  917. *
  918. * Called by synaptics_rmi4_probe() and the power management functions
  919. * in this driver and also exported to other expansion Function modules
  920. * such as rmi_dev.
  921. *
  922. * This function handles the enabling and disabling of the attention
  923. * irq including the setting up of the ISR thread.
  924. */
  925. static int synaptics_rmi4_irq_enable(struct synaptics_rmi4_data *rmi4_data,
  926. bool enable)
  927. {
  928. int retval = 0;
  929. unsigned char *intr_status;
  930. if (enable) {
  931. if (rmi4_data->irq_enabled)
  932. return retval;
  933. intr_status = kzalloc(rmi4_data->num_of_intr_regs, GFP_KERNEL);
  934. if (!intr_status) {
  935. dev_err(&rmi4_data->i2c_client->dev,
  936. "%s: Failed to alloc memory\n",
  937. __func__);
  938. return -ENOMEM;
  939. }
  940. /* Clear interrupts first */
  941. retval = synaptics_rmi4_i2c_read(rmi4_data,
  942. rmi4_data->f01_data_base_addr + 1,
  943. intr_status,
  944. rmi4_data->num_of_intr_regs);
  945. kfree(intr_status);
  946. if (retval < 0)
  947. return retval;
  948. enable_irq(rmi4_data->irq);
  949. rmi4_data->irq_enabled = true;
  950. } else {
  951. if (rmi4_data->irq_enabled) {
  952. disable_irq(rmi4_data->irq);
  953. rmi4_data->irq_enabled = false;
  954. }
  955. }
  956. return retval;
  957. }
  958. /**
  959. * synaptics_rmi4_f11_init()
  960. *
  961. * Called by synaptics_rmi4_query_device().
  962. *
  963. * This funtion parses information from the Function 11 registers
  964. * and determines the number of fingers supported, x and y data ranges,
  965. * offset to the associated interrupt status register, interrupt bit
  966. * mask, and gathers finger data acquisition capabilities from the query
  967. * registers.
  968. */
  969. static int synaptics_rmi4_f11_init(struct synaptics_rmi4_data *rmi4_data,
  970. struct synaptics_rmi4_fn *fhandler,
  971. struct synaptics_rmi4_fn_desc *fd,
  972. unsigned int intr_count)
  973. {
  974. int retval;
  975. unsigned char ii;
  976. unsigned char intr_offset;
  977. unsigned char abs_data_size;
  978. unsigned char abs_data_blk_size;
  979. unsigned char query[F11_STD_QUERY_LEN];
  980. unsigned char control[F11_STD_CTRL_LEN];
  981. fhandler->fn_number = fd->fn_number;
  982. fhandler->num_of_data_sources = fd->intr_src_count;
  983. retval = synaptics_rmi4_i2c_read(rmi4_data,
  984. fhandler->full_addr.query_base,
  985. query,
  986. sizeof(query));
  987. if (retval < 0)
  988. return retval;
  989. /* Maximum number of fingers supported */
  990. if ((query[1] & MASK_3BIT) <= 4)
  991. fhandler->num_of_data_points = (query[1] & MASK_3BIT) + 1;
  992. else if ((query[1] & MASK_3BIT) == 5)
  993. fhandler->num_of_data_points = 10;
  994. rmi4_data->num_of_fingers = fhandler->num_of_data_points;
  995. retval = synaptics_rmi4_i2c_read(rmi4_data,
  996. fhandler->full_addr.ctrl_base,
  997. control,
  998. sizeof(control));
  999. if (retval < 0)
  1000. return retval;
  1001. /* Maximum x and y */
  1002. rmi4_data->sensor_max_x = ((control[6] & MASK_8BIT) << 0) |
  1003. ((control[7] & MASK_4BIT) << 8);
  1004. rmi4_data->sensor_max_y = ((control[8] & MASK_8BIT) << 0) |
  1005. ((control[9] & MASK_4BIT) << 8);
  1006. dev_dbg(&rmi4_data->i2c_client->dev,
  1007. "%s: Function %02x max x = %d max y = %d\n",
  1008. __func__, fhandler->fn_number,
  1009. rmi4_data->sensor_max_x,
  1010. rmi4_data->sensor_max_y);
  1011. fhandler->intr_reg_num = (intr_count + 7) / 8;
  1012. if (fhandler->intr_reg_num != 0)
  1013. fhandler->intr_reg_num -= 1;
  1014. /* Set an enable bit for each data source */
  1015. intr_offset = intr_count % 8;
  1016. fhandler->intr_mask = 0;
  1017. for (ii = intr_offset;
  1018. ii < ((fd->intr_src_count & MASK_3BIT) +
  1019. intr_offset);
  1020. ii++)
  1021. fhandler->intr_mask |= 1 << ii;
  1022. abs_data_size = query[5] & MASK_2BIT;
  1023. abs_data_blk_size = 3 + (2 * (abs_data_size == 0 ? 1 : 0));
  1024. fhandler->size_of_data_register_block = abs_data_blk_size;
  1025. return retval;
  1026. }
  1027. static int synaptics_rmi4_f1a_alloc_mem(struct synaptics_rmi4_data *rmi4_data,
  1028. struct synaptics_rmi4_fn *fhandler)
  1029. {
  1030. int retval;
  1031. struct synaptics_rmi4_f1a_handle *f1a;
  1032. f1a = kzalloc(sizeof(*f1a), GFP_KERNEL);
  1033. if (!f1a) {
  1034. dev_err(&rmi4_data->i2c_client->dev,
  1035. "%s: Failed to alloc mem for function handle\n",
  1036. __func__);
  1037. return -ENOMEM;
  1038. }
  1039. fhandler->data = (void *)f1a;
  1040. retval = synaptics_rmi4_i2c_read(rmi4_data,
  1041. fhandler->full_addr.query_base,
  1042. f1a->button_query.data,
  1043. sizeof(f1a->button_query.data));
  1044. if (retval < 0) {
  1045. dev_err(&rmi4_data->i2c_client->dev,
  1046. "%s: Failed to read query registers\n",
  1047. __func__);
  1048. return retval;
  1049. }
  1050. f1a->button_count = f1a->button_query.max_button_count + 1;
  1051. f1a->button_bitmask_size = (f1a->button_count + 7) / 8;
  1052. f1a->button_data_buffer = kcalloc(f1a->button_bitmask_size,
  1053. sizeof(*(f1a->button_data_buffer)), GFP_KERNEL);
  1054. if (!f1a->button_data_buffer) {
  1055. dev_err(&rmi4_data->i2c_client->dev,
  1056. "%s: Failed to alloc mem for data buffer\n",
  1057. __func__);
  1058. return -ENOMEM;
  1059. }
  1060. f1a->button_map = kcalloc(f1a->button_count,
  1061. sizeof(*(f1a->button_map)), GFP_KERNEL);
  1062. if (!f1a->button_map) {
  1063. dev_err(&rmi4_data->i2c_client->dev,
  1064. "%s: Failed to alloc mem for button map\n",
  1065. __func__);
  1066. return -ENOMEM;
  1067. }
  1068. return 0;
  1069. }
  1070. static int synaptics_rmi4_capacitance_button_map(
  1071. struct synaptics_rmi4_data *rmi4_data,
  1072. struct synaptics_rmi4_fn *fhandler)
  1073. {
  1074. unsigned char ii;
  1075. struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
  1076. const struct synaptics_rmi4_platform_data *pdata = rmi4_data->board;
  1077. if (!pdata->capacitance_button_map) {
  1078. dev_err(&rmi4_data->i2c_client->dev,
  1079. "%s: capacitance_button_map is" \
  1080. "NULL in board file\n",
  1081. __func__);
  1082. return -ENODEV;
  1083. } else if (!pdata->capacitance_button_map->map) {
  1084. dev_err(&rmi4_data->i2c_client->dev,
  1085. "%s: Button map is missing in board file\n",
  1086. __func__);
  1087. return -ENODEV;
  1088. } else {
  1089. if (pdata->capacitance_button_map->nbuttons !=
  1090. f1a->button_count) {
  1091. f1a->valid_button_count = min(f1a->button_count,
  1092. pdata->capacitance_button_map->nbuttons);
  1093. } else {
  1094. f1a->valid_button_count = f1a->button_count;
  1095. }
  1096. for (ii = 0; ii < f1a->valid_button_count; ii++)
  1097. f1a->button_map[ii] =
  1098. pdata->capacitance_button_map->map[ii];
  1099. }
  1100. return 0;
  1101. }
  1102. static void synaptics_rmi4_f1a_kfree(struct synaptics_rmi4_fn *fhandler)
  1103. {
  1104. struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
  1105. if (f1a) {
  1106. kfree(f1a->button_data_buffer);
  1107. kfree(f1a->button_map);
  1108. kfree(f1a);
  1109. fhandler->data = NULL;
  1110. }
  1111. return;
  1112. }
  1113. static int synaptics_rmi4_f1a_init(struct synaptics_rmi4_data *rmi4_data,
  1114. struct synaptics_rmi4_fn *fhandler,
  1115. struct synaptics_rmi4_fn_desc *fd,
  1116. unsigned int intr_count)
  1117. {
  1118. int retval;
  1119. unsigned char ii;
  1120. unsigned short intr_offset;
  1121. fhandler->fn_number = fd->fn_number;
  1122. fhandler->num_of_data_sources = fd->intr_src_count;
  1123. fhandler->intr_reg_num = (intr_count + 7) / 8;
  1124. if (fhandler->intr_reg_num != 0)
  1125. fhandler->intr_reg_num -= 1;
  1126. /* Set an enable bit for each data source */
  1127. intr_offset = intr_count % 8;
  1128. fhandler->intr_mask = 0;
  1129. for (ii = intr_offset;
  1130. ii < ((fd->intr_src_count & MASK_3BIT) +
  1131. intr_offset);
  1132. ii++)
  1133. fhandler->intr_mask |= 1 << ii;
  1134. retval = synaptics_rmi4_f1a_alloc_mem(rmi4_data, fhandler);
  1135. if (retval < 0)
  1136. goto error_exit;
  1137. retval = synaptics_rmi4_capacitance_button_map(rmi4_data, fhandler);
  1138. if (retval < 0)
  1139. goto error_exit;
  1140. rmi4_data->button_0d_enabled = 1;
  1141. return 0;
  1142. error_exit:
  1143. synaptics_rmi4_f1a_kfree(fhandler);
  1144. return retval;
  1145. }
  1146. static int synaptics_rmi4_alloc_fh(struct synaptics_rmi4_fn **fhandler,
  1147. struct synaptics_rmi4_fn_desc *rmi_fd, int page_number)
  1148. {
  1149. *fhandler = kzalloc(sizeof(**fhandler), GFP_KERNEL);
  1150. if (!(*fhandler))
  1151. return -ENOMEM;
  1152. (*fhandler)->full_addr.data_base =
  1153. (rmi_fd->data_base_addr |
  1154. (page_number << 8));
  1155. (*fhandler)->full_addr.ctrl_base =
  1156. (rmi_fd->ctrl_base_addr |
  1157. (page_number << 8));
  1158. (*fhandler)->full_addr.cmd_base =
  1159. (rmi_fd->cmd_base_addr |
  1160. (page_number << 8));
  1161. (*fhandler)->full_addr.query_base =
  1162. (rmi_fd->query_base_addr |
  1163. (page_number << 8));
  1164. (*fhandler)->fn_number = rmi_fd->fn_number;
  1165. return 0;
  1166. }
  1167. /**
  1168. * synaptics_rmi4_query_device_info()
  1169. *
  1170. * Called by synaptics_rmi4_query_device().
  1171. *
  1172. */
  1173. static int synaptics_rmi4_query_device_info(
  1174. struct synaptics_rmi4_data *rmi4_data)
  1175. {
  1176. int retval;
  1177. unsigned char f01_query[F01_STD_QUERY_LEN];
  1178. struct synaptics_rmi4_device_info *rmi = &(rmi4_data->rmi4_mod_info);
  1179. retval = synaptics_rmi4_i2c_read(rmi4_data,
  1180. rmi4_data->f01_query_base_addr,
  1181. f01_query,
  1182. sizeof(f01_query));
  1183. if (retval < 0)
  1184. return retval;
  1185. /* RMI Version 4.0 currently supported */
  1186. rmi->version_major = 4;
  1187. rmi->version_minor = 0;
  1188. rmi->manufacturer_id = f01_query[0];
  1189. rmi->product_props = f01_query[1];
  1190. rmi->product_info[0] = f01_query[2] & MASK_7BIT;
  1191. rmi->product_info[1] = f01_query[3] & MASK_7BIT;
  1192. rmi->date_code[0] = f01_query[4] & MASK_5BIT;
  1193. rmi->date_code[1] = f01_query[5] & MASK_4BIT;
  1194. rmi->date_code[2] = f01_query[6] & MASK_5BIT;
  1195. rmi->tester_id = ((f01_query[7] & MASK_7BIT) << 8) |
  1196. (f01_query[8] & MASK_7BIT);
  1197. rmi->serial_number = ((f01_query[9] & MASK_7BIT) << 8) |
  1198. (f01_query[10] & MASK_7BIT);
  1199. memcpy(rmi->product_id_string, &f01_query[11], 10);
  1200. if (rmi->manufacturer_id != 1) {
  1201. dev_err(&rmi4_data->i2c_client->dev,
  1202. "%s: Non-Synaptics device found, manufacturer ID = %d\n",
  1203. __func__, rmi->manufacturer_id);
  1204. }
  1205. retval = synaptics_rmi4_i2c_read(rmi4_data,
  1206. rmi4_data->f01_query_base_addr + F01_BUID_ID_OFFSET,
  1207. rmi->build_id,
  1208. sizeof(rmi->build_id));
  1209. if (retval < 0) {
  1210. dev_err(&rmi4_data->i2c_client->dev,
  1211. "%s: Failed to read firmware build id (code %d)\n",
  1212. __func__, retval);
  1213. return retval;
  1214. }
  1215. return 0;
  1216. }
  1217. /**
  1218. * synaptics_rmi4_query_device()
  1219. *
  1220. * Called by synaptics_rmi4_probe().
  1221. *
  1222. * This funtion scans the page description table, records the offsets
  1223. * to the register types of Function $01, sets up the function handlers
  1224. * for Function $11 and Function $12, determines the number of interrupt
  1225. * sources from the sensor, adds valid Functions with data inputs to the
  1226. * Function linked list, parses information from the query registers of
  1227. * Function $01, and enables the interrupt sources from the valid Functions
  1228. * with data inputs.
  1229. */
  1230. static int synaptics_rmi4_query_device(struct synaptics_rmi4_data *rmi4_data)
  1231. {
  1232. int retval;
  1233. unsigned char ii;
  1234. unsigned char page_number;
  1235. unsigned char intr_count = 0;
  1236. unsigned char data_sources = 0;
  1237. unsigned short pdt_entry_addr;
  1238. unsigned short intr_addr;
  1239. struct synaptics_rmi4_f01_device_status status;
  1240. struct synaptics_rmi4_fn_desc rmi_fd;
  1241. struct synaptics_rmi4_fn *fhandler;
  1242. struct synaptics_rmi4_device_info *rmi;
  1243. rmi = &(rmi4_data->rmi4_mod_info);
  1244. INIT_LIST_HEAD(&rmi->support_fn_list);
  1245. /* Scan the page description tables of the pages to service */
  1246. for (page_number = 0; page_number < PAGES_TO_SERVICE; page_number++) {
  1247. for (pdt_entry_addr = PDT_START; pdt_entry_addr > PDT_END;
  1248. pdt_entry_addr -= PDT_ENTRY_SIZE) {
  1249. pdt_entry_addr |= (page_number << 8);
  1250. retval = synaptics_rmi4_i2c_read(rmi4_data,
  1251. pdt_entry_addr,
  1252. (unsigned char *)&rmi_fd,
  1253. sizeof(rmi_fd));
  1254. if (retval < 0)
  1255. return retval;
  1256. fhandler = NULL;
  1257. if (rmi_fd.fn_number == 0) {
  1258. dev_dbg(&rmi4_data->i2c_client->dev,
  1259. "%s: Reached end of PDT\n",
  1260. __func__);
  1261. break;
  1262. }
  1263. dev_dbg(&rmi4_data->i2c_client->dev,
  1264. "%s: F%02x found (page %d)\n",
  1265. __func__, rmi_fd.fn_number,
  1266. page_number);
  1267. switch (rmi_fd.fn_number) {
  1268. case SYNAPTICS_RMI4_F01:
  1269. rmi4_data->f01_query_base_addr =
  1270. rmi_fd.query_base_addr;
  1271. rmi4_data->f01_ctrl_base_addr =
  1272. rmi_fd.ctrl_base_addr;
  1273. rmi4_data->f01_data_base_addr =
  1274. rmi_fd.data_base_addr;
  1275. rmi4_data->f01_cmd_base_addr =
  1276. rmi_fd.cmd_base_addr;
  1277. retval =
  1278. synaptics_rmi4_query_device_info(rmi4_data);
  1279. if (retval < 0)
  1280. return retval;
  1281. retval = synaptics_rmi4_i2c_read(rmi4_data,
  1282. rmi4_data->f01_data_base_addr,
  1283. status.data,
  1284. sizeof(status.data));
  1285. if (retval < 0)
  1286. return retval;
  1287. while (status.status_code == STATUS_CRC_IN_PROGRESS) {
  1288. msleep(1);
  1289. retval = synaptics_rmi4_i2c_read(rmi4_data,
  1290. rmi4_data->f01_data_base_addr,
  1291. status.data,
  1292. sizeof(status.data));
  1293. if (retval < 0)
  1294. return retval;
  1295. }
  1296. if (status.flash_prog == 1) {
  1297. pr_notice("%s: In flash prog mode, status = 0x%02x\n",
  1298. __func__,
  1299. status.status_code);
  1300. goto flash_prog_mode;
  1301. }
  1302. break;
  1303. case SYNAPTICS_RMI4_F11:
  1304. if (rmi_fd.intr_src_count == 0)
  1305. break;
  1306. retval = synaptics_rmi4_alloc_fh(&fhandler,
  1307. &rmi_fd, page_number);
  1308. if (retval < 0) {
  1309. dev_err(&rmi4_data->i2c_client->dev,
  1310. "%s: Failed to alloc for F%d\n",
  1311. __func__,
  1312. rmi_fd.fn_number);
  1313. return retval;
  1314. }
  1315. retval = synaptics_rmi4_f11_init(rmi4_data,
  1316. fhandler, &rmi_fd, intr_count);
  1317. if (retval < 0)
  1318. return retval;
  1319. break;
  1320. case SYNAPTICS_RMI4_F1A:
  1321. if (rmi_fd.intr_src_count == 0)
  1322. break;
  1323. retval = synaptics_rmi4_alloc_fh(&fhandler,
  1324. &rmi_fd, page_number);
  1325. if (retval < 0) {
  1326. dev_err(&rmi4_data->i2c_client->dev,
  1327. "%s: Failed to alloc for F%d\n",
  1328. __func__,
  1329. rmi_fd.fn_number);
  1330. return retval;
  1331. }
  1332. retval = synaptics_rmi4_f1a_init(rmi4_data,
  1333. fhandler, &rmi_fd, intr_count);
  1334. if (retval < 0)
  1335. return retval;
  1336. break;
  1337. }
  1338. /* Accumulate the interrupt count */
  1339. intr_count += (rmi_fd.intr_src_count & MASK_3BIT);
  1340. if (fhandler && rmi_fd.intr_src_count) {
  1341. list_add_tail(&fhandler->link,
  1342. &rmi->support_fn_list);
  1343. }
  1344. }
  1345. }
  1346. flash_prog_mode:
  1347. rmi4_data->num_of_intr_regs = (intr_count + 7) / 8;
  1348. dev_dbg(&rmi4_data->i2c_client->dev,
  1349. "%s: Number of interrupt registers = %d\n",
  1350. __func__, rmi4_data->num_of_intr_regs);
  1351. memset(rmi4_data->intr_mask, 0x00, sizeof(rmi4_data->intr_mask));
  1352. /*
  1353. * Map out the interrupt bit masks for the interrupt sources
  1354. * from the registered function handlers.
  1355. */
  1356. if (!list_empty(&rmi->support_fn_list)) {
  1357. list_for_each_entry(fhandler, &rmi->support_fn_list, link)
  1358. data_sources += fhandler->num_of_data_sources;
  1359. }
  1360. if (data_sources) {
  1361. if (!list_empty(&rmi->support_fn_list)) {
  1362. list_for_each_entry(fhandler,
  1363. &rmi->support_fn_list, link) {
  1364. if (fhandler->num_of_data_sources) {
  1365. rmi4_data->intr_mask[fhandler->intr_reg_num] |=
  1366. fhandler->intr_mask;
  1367. }
  1368. }
  1369. }
  1370. }
  1371. /* Enable the interrupt sources */
  1372. for (ii = 0; ii < rmi4_data->num_of_intr_regs; ii++) {
  1373. if (rmi4_data->intr_mask[ii] != 0x00) {
  1374. dev_dbg(&rmi4_data->i2c_client->dev,
  1375. "%s: Interrupt enable mask %d = 0x%02x\n",
  1376. __func__, ii, rmi4_data->intr_mask[ii]);
  1377. intr_addr = rmi4_data->f01_ctrl_base_addr + 1 + ii;
  1378. retval = synaptics_rmi4_i2c_write(rmi4_data,
  1379. intr_addr,
  1380. &(rmi4_data->intr_mask[ii]),
  1381. sizeof(rmi4_data->intr_mask[ii]));
  1382. if (retval < 0)
  1383. return retval;
  1384. }
  1385. }
  1386. return 0;
  1387. }
  1388. static int synaptics_rmi4_reset_command(struct synaptics_rmi4_data *rmi4_data)
  1389. {
  1390. int retval;
  1391. int page_number;
  1392. unsigned char command = 0x01;
  1393. unsigned short pdt_entry_addr;
  1394. struct synaptics_rmi4_fn_desc rmi_fd;
  1395. bool done = false;
  1396. /* Scan the page description tables of the pages to service */
  1397. for (page_number = 0; page_number < PAGES_TO_SERVICE; page_number++) {
  1398. for (pdt_entry_addr = PDT_START; pdt_entry_addr > PDT_END;
  1399. pdt_entry_addr -= PDT_ENTRY_SIZE) {
  1400. retval = synaptics_rmi4_i2c_read(rmi4_data,
  1401. pdt_entry_addr,
  1402. (unsigned char *)&rmi_fd,
  1403. sizeof(rmi_fd));
  1404. if (retval < 0)
  1405. return retval;
  1406. if (rmi_fd.fn_number == 0)
  1407. break;
  1408. switch (rmi_fd.fn_number) {
  1409. case SYNAPTICS_RMI4_F01:
  1410. rmi4_data->f01_cmd_base_addr =
  1411. rmi_fd.cmd_base_addr;
  1412. done = true;
  1413. break;
  1414. }
  1415. }
  1416. if (done) {
  1417. dev_info(&rmi4_data->i2c_client->dev,
  1418. "%s: Find F01 in page description table 0x%x\n",
  1419. __func__, rmi4_data->f01_cmd_base_addr);
  1420. break;
  1421. }
  1422. }
  1423. if (!done) {
  1424. dev_err(&rmi4_data->i2c_client->dev,
  1425. "%s: Cannot find F01 in page description table\n",
  1426. __func__);
  1427. return -EINVAL;
  1428. }
  1429. retval = synaptics_rmi4_i2c_write(rmi4_data,
  1430. rmi4_data->f01_cmd_base_addr,
  1431. &command,
  1432. sizeof(command));
  1433. if (retval < 0) {
  1434. dev_err(&rmi4_data->i2c_client->dev,
  1435. "%s: Failed to issue reset command, error = %d\n",
  1436. __func__, retval);
  1437. return retval;
  1438. }
  1439. msleep(RESET_DELAY);
  1440. return retval;
  1441. };
  1442. static int synaptics_rmi4_reset_device(struct synaptics_rmi4_data *rmi4_data)
  1443. {
  1444. int retval;
  1445. struct synaptics_rmi4_fn *fhandler;
  1446. struct synaptics_rmi4_device_info *rmi;
  1447. rmi = &(rmi4_data->rmi4_mod_info);
  1448. retval = synaptics_rmi4_reset_command(rmi4_data);
  1449. if (retval < 0) {
  1450. dev_err(&rmi4_data->i2c_client->dev,
  1451. "%s: Failed to send command reset\n",
  1452. __func__);
  1453. return retval;
  1454. }
  1455. if (!list_empty(&rmi->support_fn_list)) {
  1456. list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
  1457. if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
  1458. synaptics_rmi4_f1a_kfree(fhandler);
  1459. else
  1460. kfree(fhandler->data);
  1461. kfree(fhandler);
  1462. }
  1463. }
  1464. retval = synaptics_rmi4_query_device(rmi4_data);
  1465. if (retval < 0) {
  1466. dev_err(&rmi4_data->i2c_client->dev,
  1467. "%s: Failed to query device\n",
  1468. __func__);
  1469. return retval;
  1470. }
  1471. return 0;
  1472. }
  1473. /**
  1474. * synaptics_rmi4_detection_work()
  1475. *
  1476. * Called by the kernel at the scheduled time.
  1477. *
  1478. * This function is a self-rearming work thread that checks for the
  1479. * insertion and removal of other expansion Function modules such as
  1480. * rmi_dev and calls their initialization and removal callback functions
  1481. * accordingly.
  1482. */
  1483. static void synaptics_rmi4_detection_work(struct work_struct *work)
  1484. {
  1485. struct synaptics_rmi4_exp_fn *exp_fhandler, *next_list_entry;
  1486. struct synaptics_rmi4_data *rmi4_data =
  1487. container_of(work, struct synaptics_rmi4_data,
  1488. det_work.work);
  1489. mutex_lock(&exp_fn_list_mutex);
  1490. if (!list_empty(&exp_fn_list)) {
  1491. list_for_each_entry_safe(exp_fhandler,
  1492. next_list_entry,
  1493. &exp_fn_list,
  1494. link) {
  1495. if ((exp_fhandler->func_init != NULL) &&
  1496. (exp_fhandler->inserted == false)) {
  1497. exp_fhandler->func_init(rmi4_data);
  1498. exp_fhandler->inserted = true;
  1499. } else if ((exp_fhandler->func_init == NULL) &&
  1500. (exp_fhandler->inserted == true)) {
  1501. exp_fhandler->func_remove(rmi4_data);
  1502. list_del(&exp_fhandler->link);
  1503. kfree(exp_fhandler);
  1504. }
  1505. }
  1506. }
  1507. mutex_unlock(&exp_fn_list_mutex);
  1508. return;
  1509. }
  1510. /**
  1511. * synaptics_rmi4_new_function()
  1512. *
  1513. * Called by other expansion Function modules in their module init and
  1514. * module exit functions.
  1515. *
  1516. * This function is used by other expansion Function modules such as
  1517. * rmi_dev to register themselves with the driver by providing their
  1518. * initialization and removal callback function pointers so that they
  1519. * can be inserted or removed dynamically at module init and exit times,
  1520. * respectively.
  1521. */
  1522. void synaptics_rmi4_new_function(enum exp_fn fn_type, bool insert,
  1523. int (*func_init)(struct synaptics_rmi4_data *rmi4_data),
  1524. void (*func_remove)(struct synaptics_rmi4_data *rmi4_data),
  1525. void (*func_attn)(struct synaptics_rmi4_data *rmi4_data,
  1526. unsigned char intr_mask))
  1527. {
  1528. struct synaptics_rmi4_exp_fn *exp_fhandler;
  1529. if (!exp_fn_inited) {
  1530. mutex_init(&exp_fn_list_mutex);
  1531. INIT_LIST_HEAD(&exp_fn_list);
  1532. exp_fn_inited = 1;
  1533. }
  1534. mutex_lock(&exp_fn_list_mutex);
  1535. if (insert) {
  1536. exp_fhandler = kzalloc(sizeof(*exp_fhandler), GFP_KERNEL);
  1537. if (!exp_fhandler) {
  1538. pr_err("%s: Failed to alloc mem for expansion function\n",
  1539. __func__);
  1540. goto exit;
  1541. }
  1542. exp_fhandler->fn_type = fn_type;
  1543. exp_fhandler->func_init = func_init;
  1544. exp_fhandler->func_attn = func_attn;
  1545. exp_fhandler->func_remove = func_remove;
  1546. exp_fhandler->inserted = false;
  1547. list_add_tail(&exp_fhandler->link, &exp_fn_list);
  1548. } else {
  1549. if (!list_empty(&exp_fn_list)) {
  1550. list_for_each_entry(exp_fhandler, &exp_fn_list, link) {
  1551. if (exp_fhandler->func_init == func_init) {
  1552. exp_fhandler->inserted = false;
  1553. exp_fhandler->func_init = NULL;
  1554. exp_fhandler->func_attn = NULL;
  1555. goto exit;
  1556. }
  1557. }
  1558. }
  1559. }
  1560. exit:
  1561. mutex_unlock(&exp_fn_list_mutex);
  1562. return;
  1563. }
  1564. EXPORT_SYMBOL(synaptics_rmi4_new_function);
  1565. static int reg_set_optimum_mode_check(struct regulator *reg, int load_uA)
  1566. {
  1567. return (regulator_count_voltages(reg) > 0) ?
  1568. regulator_set_optimum_mode(reg, load_uA) : 0;
  1569. }
  1570. static int synaptics_rmi4_regulator_configure(struct synaptics_rmi4_data
  1571. *rmi4_data, bool on)
  1572. {
  1573. int retval;
  1574. if (on == false)
  1575. goto hw_shutdown;
  1576. if (rmi4_data->board->regulator_en) {
  1577. rmi4_data->vdd = regulator_get(&rmi4_data->i2c_client->dev,
  1578. "vdd");
  1579. if (IS_ERR(rmi4_data->vdd)) {
  1580. dev_err(&rmi4_data->i2c_client->dev,
  1581. "%s: Failed to get vdd regulator\n",
  1582. __func__);
  1583. return PTR_ERR(rmi4_data->vdd);
  1584. }
  1585. if (regulator_count_voltages(rmi4_data->vdd) > 0) {
  1586. retval = regulator_set_voltage(rmi4_data->vdd,
  1587. RMI4_VTG_MIN_UV, RMI4_VTG_MAX_UV);
  1588. if (retval) {
  1589. dev_err(&rmi4_data->i2c_client->dev,
  1590. "regulator set_vtg failed retval =%d\n",
  1591. retval);
  1592. goto err_set_vtg_vdd;
  1593. }
  1594. }
  1595. }
  1596. if (rmi4_data->board->i2c_pull_up) {
  1597. rmi4_data->vcc_i2c = regulator_get(&rmi4_data->i2c_client->dev,
  1598. "vcc_i2c");
  1599. if (IS_ERR(rmi4_data->vcc_i2c)) {
  1600. dev_err(&rmi4_data->i2c_client->dev,
  1601. "%s: Failed to get i2c regulator\n",
  1602. __func__);
  1603. retval = PTR_ERR(rmi4_data->vcc_i2c);
  1604. goto err_get_vtg_i2c;
  1605. }
  1606. if (regulator_count_voltages(rmi4_data->vcc_i2c) > 0) {
  1607. retval = regulator_set_voltage(rmi4_data->vcc_i2c,
  1608. RMI4_I2C_VTG_MIN_UV, RMI4_I2C_VTG_MAX_UV);
  1609. if (retval) {
  1610. dev_err(&rmi4_data->i2c_client->dev,
  1611. "reg set i2c vtg failed retval =%d\n",
  1612. retval);
  1613. goto err_set_vtg_i2c;
  1614. }
  1615. }
  1616. }
  1617. return 0;
  1618. err_set_vtg_i2c:
  1619. if (rmi4_data->board->i2c_pull_up)
  1620. regulator_put(rmi4_data->vcc_i2c);
  1621. err_get_vtg_i2c:
  1622. if (rmi4_data->board->regulator_en)
  1623. if (regulator_count_voltages(rmi4_data->vdd) > 0)
  1624. regulator_set_voltage(rmi4_data->vdd, 0,
  1625. RMI4_VTG_MAX_UV);
  1626. err_set_vtg_vdd:
  1627. if (rmi4_data->board->regulator_en)
  1628. regulator_put(rmi4_data->vdd);
  1629. return retval;
  1630. hw_shutdown:
  1631. if (rmi4_data->board->regulator_en) {
  1632. if (regulator_count_voltages(rmi4_data->vdd) > 0)
  1633. regulator_set_voltage(rmi4_data->vdd, 0,
  1634. RMI4_VTG_MAX_UV);
  1635. regulator_put(rmi4_data->vdd);
  1636. }
  1637. if (rmi4_data->board->i2c_pull_up) {
  1638. if (regulator_count_voltages(rmi4_data->vcc_i2c) > 0)
  1639. regulator_set_voltage(rmi4_data->vcc_i2c, 0,
  1640. RMI4_I2C_VTG_MAX_UV);
  1641. regulator_put(rmi4_data->vcc_i2c);
  1642. }
  1643. return 0;
  1644. };
  1645. static int synaptics_rmi4_power_on(struct synaptics_rmi4_data *rmi4_data,
  1646. bool on) {
  1647. int retval;
  1648. if (on == false)
  1649. goto power_off;
  1650. if (rmi4_data->board->regulator_en) {
  1651. retval = reg_set_optimum_mode_check(rmi4_data->vdd,
  1652. RMI4_ACTIVE_LOAD_UA);
  1653. if (retval < 0) {
  1654. dev_err(&rmi4_data->i2c_client->dev,
  1655. "Regulator vdd set_opt failed rc=%d\n",
  1656. retval);
  1657. return retval;
  1658. }
  1659. retval = regulator_enable(rmi4_data->vdd);
  1660. if (retval) {
  1661. dev_err(&rmi4_data->i2c_client->dev,
  1662. "Regulator vdd enable failed rc=%d\n",
  1663. retval);
  1664. goto error_reg_en_vdd;
  1665. }
  1666. }
  1667. if (rmi4_data->board->i2c_pull_up) {
  1668. retval = reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
  1669. RMI4_I2C_LOAD_UA);
  1670. if (retval < 0) {
  1671. dev_err(&rmi4_data->i2c_client->dev,
  1672. "Regulator vcc_i2c set_opt failed rc=%d\n",
  1673. retval);
  1674. goto error_reg_opt_i2c;
  1675. }
  1676. retval = regulator_enable(rmi4_data->vcc_i2c);
  1677. if (retval) {
  1678. dev_err(&rmi4_data->i2c_client->dev,
  1679. "Regulator vcc_i2c enable failed rc=%d\n",
  1680. retval);
  1681. goto error_reg_en_vcc_i2c;
  1682. }
  1683. }
  1684. return 0;
  1685. error_reg_en_vcc_i2c:
  1686. if (rmi4_data->board->i2c_pull_up)
  1687. reg_set_optimum_mode_check(rmi4_data->vdd, 0);
  1688. error_reg_opt_i2c:
  1689. if (rmi4_data->board->regulator_en)
  1690. regulator_disable(rmi4_data->vdd);
  1691. error_reg_en_vdd:
  1692. if (rmi4_data->board->regulator_en)
  1693. reg_set_optimum_mode_check(rmi4_data->vdd, 0);
  1694. return retval;
  1695. power_off:
  1696. if (rmi4_data->board->regulator_en) {
  1697. reg_set_optimum_mode_check(rmi4_data->vdd, 0);
  1698. regulator_disable(rmi4_data->vdd);
  1699. }
  1700. if (rmi4_data->board->i2c_pull_up) {
  1701. reg_set_optimum_mode_check(rmi4_data->vcc_i2c, 0);
  1702. regulator_disable(rmi4_data->vcc_i2c);
  1703. }
  1704. return 0;
  1705. }
  1706. /**
  1707. * synaptics_rmi4_probe()
  1708. *
  1709. * Called by the kernel when an association with an I2C device of the
  1710. * same name is made (after doing i2c_add_driver).
  1711. *
  1712. * This funtion allocates and initializes the resources for the driver
  1713. * as an input driver, turns on the power to the sensor, queries the
  1714. * sensor for its supported Functions and characteristics, registers
  1715. * the driver to the input subsystem, sets up the interrupt, handles
  1716. * the registration of the early_suspend and late_resume functions,
  1717. * and creates a work queue for detection of other expansion Function
  1718. * modules.
  1719. */
  1720. static int __devinit synaptics_rmi4_probe(struct i2c_client *client,
  1721. const struct i2c_device_id *dev_id)
  1722. {
  1723. int retval = 0;
  1724. unsigned char ii;
  1725. unsigned char attr_count;
  1726. struct synaptics_rmi4_f1a_handle *f1a;
  1727. struct synaptics_rmi4_fn *fhandler;
  1728. struct synaptics_rmi4_data *rmi4_data;
  1729. struct synaptics_rmi4_device_info *rmi;
  1730. struct synaptics_rmi4_platform_data *platform_data =
  1731. client->dev.platform_data;
  1732. if (!i2c_check_functionality(client->adapter,
  1733. I2C_FUNC_SMBUS_BYTE_DATA)) {
  1734. dev_err(&client->dev,
  1735. "%s: SMBus byte data not supported\n",
  1736. __func__);
  1737. return -EIO;
  1738. }
  1739. if (client->dev.of_node) {
  1740. platform_data = devm_kzalloc(&client->dev,
  1741. sizeof(*platform_data),
  1742. GFP_KERNEL);
  1743. if (!platform_data) {
  1744. dev_err(&client->dev, "Failed to allocate memory\n");
  1745. return -ENOMEM;
  1746. }
  1747. retval = synaptics_rmi4_parse_dt(&client->dev, platform_data);
  1748. if (retval)
  1749. return retval;
  1750. } else {
  1751. platform_data = client->dev.platform_data;
  1752. }
  1753. if (!platform_data) {
  1754. dev_err(&client->dev,
  1755. "%s: No platform data found\n",
  1756. __func__);
  1757. return -EINVAL;
  1758. }
  1759. rmi4_data = kzalloc(sizeof(*rmi4_data) * 2, GFP_KERNEL);
  1760. if (!rmi4_data) {
  1761. dev_err(&client->dev,
  1762. "%s: Failed to alloc mem for rmi4_data\n",
  1763. __func__);
  1764. return -ENOMEM;
  1765. }
  1766. rmi = &(rmi4_data->rmi4_mod_info);
  1767. rmi4_data->input_dev = input_allocate_device();
  1768. if (rmi4_data->input_dev == NULL) {
  1769. dev_err(&client->dev,
  1770. "%s: Failed to allocate input device\n",
  1771. __func__);
  1772. retval = -ENOMEM;
  1773. goto err_input_device;
  1774. }
  1775. rmi4_data->i2c_client = client;
  1776. rmi4_data->current_page = MASK_8BIT;
  1777. rmi4_data->board = platform_data;
  1778. rmi4_data->touch_stopped = false;
  1779. rmi4_data->sensor_sleep = false;
  1780. rmi4_data->irq_enabled = false;
  1781. rmi4_data->i2c_read = synaptics_rmi4_i2c_read;
  1782. rmi4_data->i2c_write = synaptics_rmi4_i2c_write;
  1783. rmi4_data->irq_enable = synaptics_rmi4_irq_enable;
  1784. rmi4_data->reset_device = synaptics_rmi4_reset_device;
  1785. rmi4_data->flip_x = rmi4_data->board->x_flip;
  1786. rmi4_data->flip_y = rmi4_data->board->y_flip;
  1787. rmi4_data->fw_image_name = rmi4_data->board->fw_image_name;
  1788. rmi4_data->input_dev->name = DRIVER_NAME;
  1789. rmi4_data->input_dev->phys = INPUT_PHYS_NAME;
  1790. rmi4_data->input_dev->id.bustype = BUS_I2C;
  1791. rmi4_data->input_dev->id.product = SYNAPTICS_DSX_DRIVER_PRODUCT;
  1792. rmi4_data->input_dev->id.version = SYNAPTICS_DSX_DRIVER_VERSION;
  1793. rmi4_data->input_dev->dev.parent = &client->dev;
  1794. input_set_drvdata(rmi4_data->input_dev, rmi4_data);
  1795. set_bit(EV_SYN, rmi4_data->input_dev->evbit);
  1796. set_bit(EV_KEY, rmi4_data->input_dev->evbit);
  1797. set_bit(EV_ABS, rmi4_data->input_dev->evbit);
  1798. set_bit(BTN_TOUCH, rmi4_data->input_dev->keybit);
  1799. set_bit(BTN_TOOL_FINGER, rmi4_data->input_dev->keybit);
  1800. #ifdef INPUT_PROP_DIRECT
  1801. set_bit(INPUT_PROP_DIRECT, rmi4_data->input_dev->propbit);
  1802. #endif
  1803. retval = synaptics_rmi4_regulator_configure(rmi4_data, true);
  1804. if (retval < 0) {
  1805. dev_err(&client->dev, "Failed to configure regulators\n");
  1806. goto err_reg_configure;
  1807. }
  1808. retval = synaptics_rmi4_power_on(rmi4_data, true);
  1809. if (retval < 0) {
  1810. dev_err(&client->dev, "Failed to power on\n");
  1811. goto err_power_device;
  1812. }
  1813. if (gpio_is_valid(platform_data->irq_gpio)) {
  1814. /* configure touchscreen irq gpio */
  1815. retval = gpio_request(platform_data->irq_gpio, "rmi4_irq_gpio");
  1816. if (retval) {
  1817. dev_err(&client->dev, "unable to request gpio [%d]\n",
  1818. platform_data->irq_gpio);
  1819. goto err_irq_gpio_req;
  1820. }
  1821. retval = gpio_direction_input(platform_data->irq_gpio);
  1822. if (retval) {
  1823. dev_err(&client->dev,
  1824. "unable to set direction for gpio [%d]\n",
  1825. platform_data->irq_gpio);
  1826. goto err_irq_gpio_dir;
  1827. }
  1828. } else {
  1829. dev_err(&client->dev, "irq gpio not provided\n");
  1830. goto err_irq_gpio_req;
  1831. }
  1832. if (gpio_is_valid(platform_data->reset_gpio)) {
  1833. /* configure touchscreen reset out gpio */
  1834. retval = gpio_request(platform_data->reset_gpio,
  1835. "rmi4_reset_gpio");
  1836. if (retval) {
  1837. dev_err(&client->dev, "unable to request gpio [%d]\n",
  1838. platform_data->reset_gpio);
  1839. goto err_irq_gpio_dir;
  1840. }
  1841. retval = gpio_direction_output(platform_data->reset_gpio, 1);
  1842. if (retval) {
  1843. dev_err(&client->dev,
  1844. "unable to set direction for gpio [%d]\n",
  1845. platform_data->reset_gpio);
  1846. goto err_reset_gpio_dir;
  1847. }
  1848. gpio_set_value(platform_data->reset_gpio, 0);
  1849. usleep(RMI4_GPIO_SLEEP_LOW_US);
  1850. gpio_set_value(platform_data->reset_gpio, 1);
  1851. msleep(RESET_DELAY);
  1852. } else
  1853. synaptics_rmi4_reset_command(rmi4_data);
  1854. init_waitqueue_head(&rmi4_data->wait);
  1855. mutex_init(&(rmi4_data->rmi4_io_ctrl_mutex));
  1856. retval = synaptics_rmi4_query_device(rmi4_data);
  1857. if (retval < 0) {
  1858. dev_err(&client->dev,
  1859. "%s: Failed to query device\n",
  1860. __func__);
  1861. goto err_reset_gpio_dir;
  1862. }
  1863. input_set_abs_params(rmi4_data->input_dev,
  1864. ABS_MT_POSITION_X, 0,
  1865. rmi4_data->sensor_max_x, 0, 0);
  1866. input_set_abs_params(rmi4_data->input_dev,
  1867. ABS_MT_POSITION_Y, 0,
  1868. rmi4_data->sensor_max_y, 0, 0);
  1869. input_set_abs_params(rmi4_data->input_dev,
  1870. ABS_PRESSURE, 0, 255, 0, 0);
  1871. #ifdef REPORT_2D_W
  1872. input_set_abs_params(rmi4_data->input_dev,
  1873. ABS_MT_TOUCH_MAJOR, 0,
  1874. MAX_ABS_MT_TOUCH_MAJOR, 0, 0);
  1875. #endif
  1876. #ifdef TYPE_B_PROTOCOL
  1877. input_mt_init_slots(rmi4_data->input_dev,
  1878. rmi4_data->num_of_fingers);
  1879. #endif
  1880. i2c_set_clientdata(client, rmi4_data);
  1881. f1a = NULL;
  1882. if (!list_empty(&rmi->support_fn_list)) {
  1883. list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
  1884. if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
  1885. f1a = fhandler->data;
  1886. }
  1887. }
  1888. if (f1a) {
  1889. for (ii = 0; ii < f1a->valid_button_count; ii++) {
  1890. set_bit(f1a->button_map[ii],
  1891. rmi4_data->input_dev->keybit);
  1892. input_set_capability(rmi4_data->input_dev,
  1893. EV_KEY, f1a->button_map[ii]);
  1894. }
  1895. }
  1896. retval = input_register_device(rmi4_data->input_dev);
  1897. if (retval) {
  1898. dev_err(&client->dev,
  1899. "%s: Failed to register input device\n",
  1900. __func__);
  1901. goto err_register_input;
  1902. }
  1903. #ifdef CONFIG_HAS_EARLYSUSPEND
  1904. rmi4_data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  1905. rmi4_data->early_suspend.suspend = synaptics_rmi4_early_suspend;
  1906. rmi4_data->early_suspend.resume = synaptics_rmi4_late_resume;
  1907. register_early_suspend(&rmi4_data->early_suspend);
  1908. #endif
  1909. if (!exp_fn_inited) {
  1910. mutex_init(&exp_fn_list_mutex);
  1911. INIT_LIST_HEAD(&exp_fn_list);
  1912. exp_fn_inited = 1;
  1913. }
  1914. rmi4_data->det_workqueue =
  1915. create_singlethread_workqueue("rmi_det_workqueue");
  1916. INIT_DELAYED_WORK(&rmi4_data->det_work,
  1917. synaptics_rmi4_detection_work);
  1918. queue_delayed_work(rmi4_data->det_workqueue,
  1919. &rmi4_data->det_work,
  1920. msecs_to_jiffies(EXP_FN_DET_INTERVAL));
  1921. rmi4_data->irq = gpio_to_irq(platform_data->irq_gpio);
  1922. retval = request_threaded_irq(rmi4_data->irq, NULL,
  1923. synaptics_rmi4_irq, platform_data->irq_flags,
  1924. DRIVER_NAME, rmi4_data);
  1925. rmi4_data->irq_enabled = true;
  1926. if (retval < 0) {
  1927. dev_err(&client->dev,
  1928. "%s: Failed to create irq thread\n",
  1929. __func__);
  1930. goto err_enable_irq;
  1931. }
  1932. for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
  1933. retval = sysfs_create_file(&rmi4_data->input_dev->dev.kobj,
  1934. &attrs[attr_count].attr);
  1935. if (retval < 0) {
  1936. dev_err(&client->dev,
  1937. "%s: Failed to create sysfs attributes\n",
  1938. __func__);
  1939. goto err_sysfs;
  1940. }
  1941. }
  1942. retval = synaptics_rmi4_irq_enable(rmi4_data, true);
  1943. if (retval < 0) {
  1944. dev_err(&client->dev,
  1945. "%s: Failed to enable attention interrupt\n",
  1946. __func__);
  1947. goto err_sysfs;
  1948. }
  1949. return retval;
  1950. err_sysfs:
  1951. for (attr_count--; attr_count >= 0; attr_count--) {
  1952. sysfs_remove_file(&rmi4_data->input_dev->dev.kobj,
  1953. &attrs[attr_count].attr);
  1954. }
  1955. err_enable_irq:
  1956. cancel_delayed_work_sync(&rmi4_data->det_work);
  1957. flush_workqueue(rmi4_data->det_workqueue);
  1958. destroy_workqueue(rmi4_data->det_workqueue);
  1959. input_unregister_device(rmi4_data->input_dev);
  1960. err_register_input:
  1961. if (!list_empty(&rmi->support_fn_list)) {
  1962. list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
  1963. if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
  1964. synaptics_rmi4_f1a_kfree(fhandler);
  1965. else
  1966. kfree(fhandler->data);
  1967. kfree(fhandler);
  1968. }
  1969. }
  1970. err_reset_gpio_dir:
  1971. if (gpio_is_valid(platform_data->reset_gpio))
  1972. gpio_free(platform_data->reset_gpio);
  1973. err_irq_gpio_dir:
  1974. if (gpio_is_valid(platform_data->irq_gpio))
  1975. gpio_free(platform_data->irq_gpio);
  1976. err_irq_gpio_req:
  1977. synaptics_rmi4_power_on(rmi4_data, false);
  1978. err_power_device:
  1979. synaptics_rmi4_regulator_configure(rmi4_data, false);
  1980. err_reg_configure:
  1981. input_free_device(rmi4_data->input_dev);
  1982. rmi4_data->input_dev = NULL;
  1983. err_input_device:
  1984. kfree(rmi4_data);
  1985. return retval;
  1986. }
  1987. /**
  1988. * synaptics_rmi4_remove()
  1989. *
  1990. * Called by the kernel when the association with an I2C device of the
  1991. * same name is broken (when the driver is unloaded).
  1992. *
  1993. * This funtion terminates the work queue, stops sensor data acquisition,
  1994. * frees the interrupt, unregisters the driver from the input subsystem,
  1995. * turns off the power to the sensor, and frees other allocated resources.
  1996. */
  1997. static int __devexit synaptics_rmi4_remove(struct i2c_client *client)
  1998. {
  1999. unsigned char attr_count;
  2000. struct synaptics_rmi4_fn *fhandler;
  2001. struct synaptics_rmi4_data *rmi4_data = i2c_get_clientdata(client);
  2002. struct synaptics_rmi4_device_info *rmi;
  2003. rmi = &(rmi4_data->rmi4_mod_info);
  2004. cancel_delayed_work_sync(&rmi4_data->det_work);
  2005. flush_workqueue(rmi4_data->det_workqueue);
  2006. destroy_workqueue(rmi4_data->det_workqueue);
  2007. rmi4_data->touch_stopped = true;
  2008. wake_up(&rmi4_data->wait);
  2009. free_irq(rmi4_data->irq, rmi4_data);
  2010. for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
  2011. sysfs_remove_file(&rmi4_data->input_dev->dev.kobj,
  2012. &attrs[attr_count].attr);
  2013. }
  2014. input_unregister_device(rmi4_data->input_dev);
  2015. if (!list_empty(&rmi->support_fn_list)) {
  2016. list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
  2017. if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
  2018. synaptics_rmi4_f1a_kfree(fhandler);
  2019. else
  2020. kfree(fhandler->data);
  2021. kfree(fhandler);
  2022. }
  2023. }
  2024. if (gpio_is_valid(rmi4_data->board->reset_gpio))
  2025. gpio_free(rmi4_data->board->reset_gpio);
  2026. if (gpio_is_valid(rmi4_data->board->irq_gpio))
  2027. gpio_free(rmi4_data->board->irq_gpio);
  2028. synaptics_rmi4_power_on(rmi4_data, false);
  2029. synaptics_rmi4_regulator_configure(rmi4_data, false);
  2030. kfree(rmi4_data);
  2031. return 0;
  2032. }
  2033. #ifdef CONFIG_PM
  2034. /**
  2035. * synaptics_rmi4_sensor_sleep()
  2036. *
  2037. * Called by synaptics_rmi4_early_suspend() and synaptics_rmi4_suspend().
  2038. *
  2039. * This function stops finger data acquisition and puts the sensor to sleep.
  2040. */
  2041. static void synaptics_rmi4_sensor_sleep(struct synaptics_rmi4_data *rmi4_data)
  2042. {
  2043. int retval;
  2044. unsigned char device_ctrl;
  2045. retval = synaptics_rmi4_i2c_read(rmi4_data,
  2046. rmi4_data->f01_ctrl_base_addr,
  2047. &device_ctrl,
  2048. sizeof(device_ctrl));
  2049. if (retval < 0) {
  2050. dev_err(&(rmi4_data->input_dev->dev),
  2051. "%s: Failed to enter sleep mode\n",
  2052. __func__);
  2053. rmi4_data->sensor_sleep = false;
  2054. return;
  2055. }
  2056. device_ctrl = (device_ctrl & ~MASK_3BIT);
  2057. device_ctrl = (device_ctrl | NO_SLEEP_OFF | SENSOR_SLEEP);
  2058. retval = synaptics_rmi4_i2c_write(rmi4_data,
  2059. rmi4_data->f01_ctrl_base_addr,
  2060. &device_ctrl,
  2061. sizeof(device_ctrl));
  2062. if (retval < 0) {
  2063. dev_err(&(rmi4_data->input_dev->dev),
  2064. "%s: Failed to enter sleep mode\n",
  2065. __func__);
  2066. rmi4_data->sensor_sleep = false;
  2067. return;
  2068. } else {
  2069. rmi4_data->sensor_sleep = true;
  2070. }
  2071. return;
  2072. }
  2073. /**
  2074. * synaptics_rmi4_sensor_wake()
  2075. *
  2076. * Called by synaptics_rmi4_resume() and synaptics_rmi4_late_resume().
  2077. *
  2078. * This function wakes the sensor from sleep.
  2079. */
  2080. static void synaptics_rmi4_sensor_wake(struct synaptics_rmi4_data *rmi4_data)
  2081. {
  2082. int retval;
  2083. unsigned char device_ctrl;
  2084. retval = synaptics_rmi4_i2c_read(rmi4_data,
  2085. rmi4_data->f01_ctrl_base_addr,
  2086. &device_ctrl,
  2087. sizeof(device_ctrl));
  2088. if (retval < 0) {
  2089. dev_err(&(rmi4_data->input_dev->dev),
  2090. "%s: Failed to wake from sleep mode\n",
  2091. __func__);
  2092. rmi4_data->sensor_sleep = true;
  2093. return;
  2094. }
  2095. device_ctrl = (device_ctrl & ~MASK_3BIT);
  2096. device_ctrl = (device_ctrl | NO_SLEEP_OFF | NORMAL_OPERATION);
  2097. retval = synaptics_rmi4_i2c_write(rmi4_data,
  2098. rmi4_data->f01_ctrl_base_addr,
  2099. &device_ctrl,
  2100. sizeof(device_ctrl));
  2101. if (retval < 0) {
  2102. dev_err(&(rmi4_data->input_dev->dev),
  2103. "%s: Failed to wake from sleep mode\n",
  2104. __func__);
  2105. rmi4_data->sensor_sleep = true;
  2106. return;
  2107. } else {
  2108. rmi4_data->sensor_sleep = false;
  2109. }
  2110. return;
  2111. }
  2112. #ifdef CONFIG_HAS_EARLYSUSPEND
  2113. /**
  2114. * synaptics_rmi4_early_suspend()
  2115. *
  2116. * Called by the kernel during the early suspend phase when the system
  2117. * enters suspend.
  2118. *
  2119. * This function calls synaptics_rmi4_sensor_sleep() to stop finger
  2120. * data acquisition and put the sensor to sleep.
  2121. */
  2122. static void synaptics_rmi4_early_suspend(struct early_suspend *h)
  2123. {
  2124. struct synaptics_rmi4_data *rmi4_data =
  2125. container_of(h, struct synaptics_rmi4_data,
  2126. early_suspend);
  2127. rmi4_data->touch_stopped = true;
  2128. wake_up(&rmi4_data->wait);
  2129. synaptics_rmi4_irq_enable(rmi4_data, false);
  2130. synaptics_rmi4_sensor_sleep(rmi4_data);
  2131. if (rmi4_data->full_pm_cycle)
  2132. synaptics_rmi4_suspend(&(rmi4_data->input_dev->dev));
  2133. return;
  2134. }
  2135. /**
  2136. * synaptics_rmi4_late_resume()
  2137. *
  2138. * Called by the kernel during the late resume phase when the system
  2139. * wakes up from suspend.
  2140. *
  2141. * This function goes through the sensor wake process if the system wakes
  2142. * up from early suspend (without going into suspend).
  2143. */
  2144. static void synaptics_rmi4_late_resume(struct early_suspend *h)
  2145. {
  2146. struct synaptics_rmi4_data *rmi4_data =
  2147. container_of(h, struct synaptics_rmi4_data,
  2148. early_suspend);
  2149. if (rmi4_data->full_pm_cycle)
  2150. synaptics_rmi4_resume(&(rmi4_data->input_dev->dev));
  2151. if (rmi4_data->sensor_sleep == true) {
  2152. synaptics_rmi4_sensor_wake(rmi4_data);
  2153. rmi4_data->touch_stopped = false;
  2154. synaptics_rmi4_irq_enable(rmi4_data, true);
  2155. }
  2156. return;
  2157. }
  2158. #endif
  2159. static int synaptics_rmi4_regulator_lpm(struct synaptics_rmi4_data *rmi4_data,
  2160. bool on)
  2161. {
  2162. int retval;
  2163. if (on == false)
  2164. goto regulator_hpm;
  2165. retval = reg_set_optimum_mode_check(rmi4_data->vdd, RMI4_LPM_LOAD_UA);
  2166. if (retval < 0) {
  2167. dev_err(&rmi4_data->i2c_client->dev,
  2168. "Regulator vcc_ana set_opt failed rc=%d\n",
  2169. retval);
  2170. goto fail_regulator_lpm;
  2171. }
  2172. if (rmi4_data->board->i2c_pull_up) {
  2173. retval = reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
  2174. RMI4_I2C_LOAD_UA);
  2175. if (retval < 0) {
  2176. dev_err(&rmi4_data->i2c_client->dev,
  2177. "Regulator vcc_i2c set_opt failed rc=%d\n",
  2178. retval);
  2179. goto fail_regulator_lpm;
  2180. }
  2181. }
  2182. return 0;
  2183. regulator_hpm:
  2184. retval = reg_set_optimum_mode_check(rmi4_data->vdd,
  2185. RMI4_ACTIVE_LOAD_UA);
  2186. if (retval < 0) {
  2187. dev_err(&rmi4_data->i2c_client->dev,
  2188. "Regulator vcc_ana set_opt failed rc=%d\n",
  2189. retval);
  2190. goto fail_regulator_hpm;
  2191. }
  2192. if (rmi4_data->board->i2c_pull_up) {
  2193. retval = reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
  2194. RMI4_I2C_LOAD_UA);
  2195. if (retval < 0) {
  2196. dev_err(&rmi4_data->i2c_client->dev,
  2197. "Regulator vcc_i2c set_opt failed rc=%d\n",
  2198. retval);
  2199. goto fail_regulator_hpm;
  2200. }
  2201. }
  2202. return 0;
  2203. fail_regulator_lpm:
  2204. reg_set_optimum_mode_check(rmi4_data->vdd, RMI4_ACTIVE_LOAD_UA);
  2205. if (rmi4_data->board->i2c_pull_up)
  2206. reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
  2207. RMI4_I2C_LOAD_UA);
  2208. return retval;
  2209. fail_regulator_hpm:
  2210. reg_set_optimum_mode_check(rmi4_data->vdd, RMI4_LPM_LOAD_UA);
  2211. if (rmi4_data->board->i2c_pull_up)
  2212. reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
  2213. RMI4_I2C_LPM_LOAD_UA);
  2214. return retval;
  2215. }
  2216. /**
  2217. * synaptics_rmi4_suspend()
  2218. *
  2219. * Called by the kernel during the suspend phase when the system
  2220. * enters suspend.
  2221. *
  2222. * This function stops finger data acquisition and puts the sensor to
  2223. * sleep (if not already done so during the early suspend phase),
  2224. * disables the interrupt, and turns off the power to the sensor.
  2225. */
  2226. static int synaptics_rmi4_suspend(struct device *dev)
  2227. {
  2228. struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
  2229. int retval;
  2230. if (!rmi4_data->sensor_sleep) {
  2231. rmi4_data->touch_stopped = true;
  2232. wake_up(&rmi4_data->wait);
  2233. synaptics_rmi4_irq_enable(rmi4_data, false);
  2234. synaptics_rmi4_sensor_sleep(rmi4_data);
  2235. }
  2236. retval = synaptics_rmi4_regulator_lpm(rmi4_data, true);
  2237. if (retval < 0) {
  2238. dev_err(dev, "failed to enter low power mode\n");
  2239. return retval;
  2240. }
  2241. return 0;
  2242. }
  2243. /**
  2244. * synaptics_rmi4_resume()
  2245. *
  2246. * Called by the kernel during the resume phase when the system
  2247. * wakes up from suspend.
  2248. *
  2249. * This function turns on the power to the sensor, wakes the sensor
  2250. * from sleep, enables the interrupt, and starts finger data
  2251. * acquisition.
  2252. */
  2253. static int synaptics_rmi4_resume(struct device *dev)
  2254. {
  2255. struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
  2256. int retval;
  2257. retval = synaptics_rmi4_regulator_lpm(rmi4_data, false);
  2258. if (retval < 0) {
  2259. dev_err(dev, "failed to enter active power mode\n");
  2260. return retval;
  2261. }
  2262. synaptics_rmi4_sensor_wake(rmi4_data);
  2263. rmi4_data->touch_stopped = false;
  2264. synaptics_rmi4_irq_enable(rmi4_data, true);
  2265. return 0;
  2266. }
  2267. static const struct dev_pm_ops synaptics_rmi4_dev_pm_ops = {
  2268. .suspend = synaptics_rmi4_suspend,
  2269. .resume = synaptics_rmi4_resume,
  2270. };
  2271. #endif
  2272. static const struct i2c_device_id synaptics_rmi4_id_table[] = {
  2273. {DRIVER_NAME, 0},
  2274. {},
  2275. };
  2276. MODULE_DEVICE_TABLE(i2c, synaptics_rmi4_id_table);
  2277. #ifdef CONFIG_OF
  2278. static struct of_device_id rmi4_match_table[] = {
  2279. { .compatible = "synaptics,rmi4",},
  2280. { },
  2281. };
  2282. #else
  2283. #define rmi4_match_table NULL
  2284. #endif
  2285. static struct i2c_driver synaptics_rmi4_driver = {
  2286. .driver = {
  2287. .name = DRIVER_NAME,
  2288. .owner = THIS_MODULE,
  2289. .of_match_table = rmi4_match_table,
  2290. #ifdef CONFIG_PM
  2291. .pm = &synaptics_rmi4_dev_pm_ops,
  2292. #endif
  2293. },
  2294. .probe = synaptics_rmi4_probe,
  2295. .remove = __devexit_p(synaptics_rmi4_remove),
  2296. .id_table = synaptics_rmi4_id_table,
  2297. };
  2298. /**
  2299. * synaptics_rmi4_init()
  2300. *
  2301. * Called by the kernel during do_initcalls (if built-in)
  2302. * or when the driver is loaded (if a module).
  2303. *
  2304. * This function registers the driver to the I2C subsystem.
  2305. *
  2306. */
  2307. static int __init synaptics_rmi4_init(void)
  2308. {
  2309. return i2c_add_driver(&synaptics_rmi4_driver);
  2310. }
  2311. /**
  2312. * synaptics_rmi4_exit()
  2313. *
  2314. * Called by the kernel when the driver is unloaded.
  2315. *
  2316. * This funtion unregisters the driver from the I2C subsystem.
  2317. *
  2318. */
  2319. static void __exit synaptics_rmi4_exit(void)
  2320. {
  2321. i2c_del_driver(&synaptics_rmi4_driver);
  2322. }
  2323. module_init(synaptics_rmi4_init);
  2324. module_exit(synaptics_rmi4_exit);
  2325. MODULE_AUTHOR("Synaptics, Inc.");
  2326. MODULE_DESCRIPTION("Synaptics RMI4 I2C Touch Driver");
  2327. MODULE_LICENSE("GPL v2");