raydium_driver.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122
  1. /* raydium_driver.c
  2. *
  3. * Raydium TouchScreen driver.
  4. *
  5. * Copyright (c) 2021 Raydium tech Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/i2c.h>
  19. #include <linux/input.h>
  20. #include <linux/input/mt.h>
  21. #include <linux/delay.h>
  22. #include <linux/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/kernel.h>
  25. #include <linux/mutex.h>
  26. #include <linux/module.h>
  27. #include <linux/gpio.h>
  28. #include <linux/unistd.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/string.h>
  31. #include <linux/timer.h>
  32. #include <linux/regulator/consumer.h>
  33. #include <linux/device.h>
  34. #include <linux/kprobes.h>
  35. #include <asm/traps.h>
  36. #include <linux/firmware.h>
  37. #include <linux/of_gpio.h>
  38. #if defined(CONFIG_FB)
  39. #include <linux/notifier.h>
  40. #include <linux/fb.h>
  41. #elif defined(CONFIG_HAS_EARLYSUSPEND)
  42. #include <linux/earlysuspend.h>
  43. #endif /*end of CONFIG_FB*/
  44. #include "raydium_driver.h"
  45. struct raydium_slot_status {
  46. unsigned char pt_id; /*Occupied point ID*/
  47. unsigned char need_update; /*Mark as info need to be updated*/
  48. unsigned char pt_report_offset; /*point info offset in report*/
  49. };
  50. /*The first 3 elements are currently occupied. therest is new coming points*/
  51. struct raydium_slot_status gst_slot[MAX_TOUCH_NUM * 2];
  52. struct raydium_slot_status gst_slot_init = {0xFF, 0, 0};
  53. #if (defined(CONFIG_RM_SYSFS_DEBUG))
  54. const struct attribute_group raydium_attr_group;
  55. #endif /*end of CONFIG_RM_SYSFS_DEBUG*/
  56. unsigned char g_u8_addr;
  57. unsigned char g_u8_raydium_flag;
  58. unsigned char g_u8_i2c_mode;
  59. unsigned char g_u8_upgrade_type;
  60. unsigned char g_u8_raw_data_type;
  61. unsigned int g_u32_raw_data_len; /* 128 bytes*/
  62. unsigned long g_u32_addr;
  63. unsigned int g_u32_length;
  64. unsigned int g_u32_driver_version;
  65. unsigned char *g_rad_fw_image, *g_rad_init_image;
  66. unsigned char *g_rad_boot_image, *g_rad_para_image;
  67. unsigned char *g_rad_testfw_image, *g_rad_testpara_image;
  68. unsigned char g_u8_table_setting, g_u8_table_init;
  69. unsigned char g_u8_resetflag;
  70. unsigned char g_u8_wakeup_flag;
  71. #ifdef ESD_SOLUTION_EN
  72. unsigned char g_u8_checkflag;
  73. #endif
  74. unsigned char g_u8_log_level;
  75. struct raydium_ts_data *g_raydium_ts;
  76. /*******************************************************************************
  77. * Name: raydium_variable_init
  78. * Brief:
  79. * Input:
  80. * Output:
  81. * Return:
  82. *******************************************************************************/
  83. static void raydium_variable_init(void)
  84. {
  85. g_u8_addr = RAYDIUM_PDA2_PDA_CFG_ADDR;
  86. g_u8_raydium_flag = NORMAL_MODE;
  87. g_u8_i2c_mode = PDA2_MODE;
  88. g_u8_upgrade_type = 0;
  89. g_u8_raw_data_type = RAYDIUM_FT_UPDATE;
  90. g_u32_raw_data_len = 64 * 2; /* 128 bytes*/
  91. g_u32_addr = RAYDIUM_CHK_I2C_CMD;
  92. g_u32_length = 1;
  93. g_u8_table_setting = 0;
  94. g_u8_table_init = 0;
  95. g_rad_fw_image = NULL;
  96. g_rad_init_image = NULL;
  97. g_rad_boot_image = NULL;
  98. g_rad_para_image = NULL;
  99. g_rad_testfw_image = NULL;
  100. g_rad_testpara_image = NULL;
  101. g_u32_driver_version = ((RAD_MAIN_VERSION << 24) |
  102. (RAD_MINOR_VERSION << 16) |
  103. (RAD_CUSTOMER_VERSION));
  104. g_u8_resetflag = false;
  105. g_u8_wakeup_flag = false;
  106. #ifdef ESD_SOLUTION_EN
  107. g_u8_checkflag = false;
  108. #endif
  109. g_u8_log_level = LOG_INFO;
  110. }
  111. /*******************************************************************************
  112. * Name: raydium_gpio_configure
  113. * Brief:
  114. * Input:
  115. * Output:
  116. * Return:
  117. *******************************************************************************/
  118. static int raydium_gpio_configure(bool on)
  119. {
  120. int i32_err = 0;
  121. if (on) {
  122. if (gpio_is_valid(g_raydium_ts->irq_gpio)) {
  123. i32_err = gpio_request(g_raydium_ts->irq_gpio,
  124. "raydium_irq_gpio");
  125. if (i32_err) {
  126. LOGD(LOG_ERR, "[touch]irq gpio request failed");
  127. goto err_irq_gpio_req;
  128. }
  129. i32_err = gpio_direction_input(g_raydium_ts->irq_gpio);
  130. if (i32_err) {
  131. LOGD(LOG_ERR, "[touch]set_direction for irq gpio failed\n");
  132. goto err_irq_gpio_dir;
  133. }
  134. }
  135. if (gpio_is_valid(g_raydium_ts->rst_gpio)) {
  136. i32_err = gpio_request(g_raydium_ts->rst_gpio,
  137. "raydium_rst_gpio");
  138. if (i32_err) {
  139. LOGD(LOG_ERR, "[touch]rst gpio request failed");
  140. goto err_irq_gpio_req;
  141. }
  142. i32_err = gpio_direction_output(g_raydium_ts->rst_gpio, 1);
  143. if (i32_err) {
  144. LOGD(LOG_ERR,
  145. "[touch]set_direction for irq gpio failed\n");
  146. goto err_rst_gpio_dir;
  147. }
  148. }
  149. } else {
  150. if (gpio_is_valid(g_raydium_ts->irq_gpio))
  151. gpio_free(g_raydium_ts->irq_gpio);
  152. }
  153. return 0;
  154. err_rst_gpio_dir:
  155. if (gpio_is_valid(g_raydium_ts->rst_gpio))
  156. gpio_free(g_raydium_ts->rst_gpio);
  157. return i32_err;
  158. err_irq_gpio_dir:
  159. if (gpio_is_valid(g_raydium_ts->irq_gpio))
  160. gpio_free(g_raydium_ts->irq_gpio);
  161. err_irq_gpio_req:
  162. return i32_err;
  163. }
  164. /*******************************************************************************
  165. * Name: raydium_ts_pinctrl_init
  166. * Brief:
  167. * Input:
  168. * Output:
  169. * Return:
  170. *******************************************************************************/
  171. #ifdef MSM_NEW_VER
  172. static int raydium_ts_pinctrl_init(void)
  173. {
  174. int i32_ret;
  175. /* Get pinctrl if target uses pinctrl */
  176. g_raydium_ts->ts_pinctrl = devm_pinctrl_get(&(g_raydium_ts->client->dev));
  177. if (IS_ERR_OR_NULL(g_raydium_ts->ts_pinctrl)) {
  178. i32_ret = PTR_ERR(g_raydium_ts->ts_pinctrl);
  179. LOGD(LOG_ERR, "[touch]target does not use pinctrl %d\n", i32_ret);
  180. goto err_pinctrl_get;
  181. }
  182. g_raydium_ts->pinctrl_state_active
  183. = pinctrl_lookup_state(g_raydium_ts->ts_pinctrl, PINCTRL_STATE_ACTIVE);
  184. if (IS_ERR_OR_NULL(g_raydium_ts->pinctrl_state_active)) {
  185. i32_ret = PTR_ERR(g_raydium_ts->pinctrl_state_active);
  186. LOGD(LOG_ERR, "[touch]Can not lookup %s pinstate %d\n",
  187. PINCTRL_STATE_ACTIVE, i32_ret);
  188. goto err_pinctrl_lookup;
  189. }
  190. g_raydium_ts->pinctrl_state_suspend
  191. = pinctrl_lookup_state(g_raydium_ts->ts_pinctrl,
  192. PINCTRL_STATE_SUSPEND);
  193. if (IS_ERR_OR_NULL(g_raydium_ts->pinctrl_state_suspend)) {
  194. i32_ret = PTR_ERR(g_raydium_ts->pinctrl_state_suspend);
  195. LOGD(LOG_ERR, "[touch]Can not lookup %s pinstate %d\n",
  196. PINCTRL_STATE_SUSPEND, i32_ret);
  197. goto err_pinctrl_lookup;
  198. }
  199. g_raydium_ts->pinctrl_state_release
  200. = pinctrl_lookup_state(g_raydium_ts->ts_pinctrl,
  201. PINCTRL_STATE_RELEASE);
  202. if (IS_ERR_OR_NULL(g_raydium_ts->pinctrl_state_release)) {
  203. i32_ret = PTR_ERR(g_raydium_ts->pinctrl_state_release);
  204. LOGD(LOG_ERR, "[touch]Can not lookup %s pinstate %d\n",
  205. PINCTRL_STATE_RELEASE, i32_ret);
  206. }
  207. return 0;
  208. err_pinctrl_lookup:
  209. devm_pinctrl_put(g_raydium_ts->ts_pinctrl);
  210. err_pinctrl_get:
  211. g_raydium_ts->ts_pinctrl = NULL;
  212. return i32_ret;
  213. }
  214. #endif/*end of MSM_NEW_VER*/
  215. #ifdef ESD_SOLUTION_EN
  216. static int raydium_hw_reset_fun(struct i2c_client *client)
  217. {
  218. int i32_ret = SUCCESS;
  219. LOGD(LOG_INFO, "[touch]HW reset\n");
  220. g_u8_raydium_flag |= ENG_MODE;
  221. g_u8_resetflag = true;
  222. /*HW reset*/
  223. gpio_set_value(g_raydium_ts->rst_gpio, 1);
  224. gpio_set_value(g_raydium_ts->rst_gpio, 0);
  225. msleep(RAYDIUM_RESET_INTERVAL_MSEC);
  226. gpio_set_value(g_raydium_ts->rst_gpio, 1);
  227. g_u8_i2c_mode = PDA2_MODE;
  228. i32_ret = wait_irq_state(client, 300, 2000);
  229. if (i32_ret != ERROR)
  230. msleep(35);
  231. g_u8_raydium_flag &= ~ENG_MODE;
  232. LOGD(LOG_INFO, "[touch]Raydium HW reset : %d\n", i32_ret);
  233. return i32_ret;
  234. }
  235. #endif
  236. int raydium_i2c_write_pda_via_pda2(struct i2c_client *client,
  237. unsigned int u32_addr, unsigned char *u8_w_data,
  238. unsigned short u16_length)
  239. {
  240. int i32_ret = -1;
  241. /*unsigned char u8_retry;*/
  242. unsigned char u8_mode = 0x00;
  243. unsigned char u8_buf[MAX_WRITE_PACKET_SIZE + 6];
  244. struct i2c_msg msg[] = {
  245. {
  246. .addr = RAYDIUM_I2C_NID,
  247. .flags = RAYDIUM_I2C_WRITE,
  248. .len = u16_length + 6,
  249. .buf = u8_buf,
  250. },
  251. };
  252. if (u16_length > MAX_WRITE_PACKET_SIZE)
  253. return -EINVAL;
  254. /*I2C access for register need to word mode*/
  255. if ((u16_length == 4) &&
  256. ((u32_addr & 0x50000000) || (u32_addr & 0x40000000)))
  257. u8_mode = I2C_PDA2_WORD_MODE;
  258. else
  259. u8_mode = I2C_PDA2_BYTE_MODE;
  260. if (u16_length > MAX_WRITE_PACKET_SIZE)
  261. return -EINVAL;
  262. g_u8_i2c_mode = PDA2_MODE;
  263. u8_buf[0] = RAYDIUM_I2C_PDA_CMD;
  264. u8_buf[1] = (unsigned char)u32_addr;
  265. u8_buf[2] = (unsigned char)(u32_addr >> 8);
  266. u8_buf[3] = (unsigned char)(u32_addr >> 16);
  267. u8_buf[4] = (unsigned char)(u32_addr >> 24);
  268. u8_buf[5] = u8_mode;
  269. memcpy(&u8_buf[6], u8_w_data, u16_length);
  270. /*for (u8_retry = 0; u8_retry < SYN_I2C_RETRY_TIMES; u8_retry++) {*/
  271. if (i2c_transfer(client->adapter, msg, 1) == 1) {
  272. i32_ret = u16_length;
  273. /*break;*/
  274. }
  275. i32_ret = u16_length;
  276. /*usleep_range(500, 1500);*/
  277. /*}*/
  278. /*if (u8_retry == SYN_I2C_RETRY_TIMES) {*/
  279. /* LOGD(LOG_ERR, "[touch]%s: I2C write over retry limit\n", __func__);*/
  280. /* i32_ret = -EIO;*/
  281. /*}*/
  282. return i32_ret;
  283. }
  284. int raydium_i2c_read_pda_via_pda2(struct i2c_client *client,
  285. unsigned int u32_addr, unsigned char *u8_r_data,
  286. unsigned short u16_length)
  287. {
  288. int i32_ret;
  289. /*unsigned char u8_retry;*/
  290. unsigned char u8_mode = 0x00;
  291. unsigned char u8_buf[6];
  292. struct i2c_msg msg[] = {
  293. {
  294. .addr = RAYDIUM_I2C_NID,
  295. .flags = RAYDIUM_I2C_WRITE,
  296. .len = 6,
  297. .buf = u8_buf,
  298. },
  299. {
  300. .addr = RAYDIUM_I2C_NID,
  301. .flags = RAYDIUM_I2C_READ,
  302. .len = u16_length,
  303. .buf = u8_r_data,
  304. },
  305. };
  306. if ((u32_addr & 0x50000000) || (u32_addr & 0x40000000))
  307. u8_mode = I2C_PDA2_WORD_MODE;
  308. else
  309. u8_mode = I2C_PDA2_BYTE_MODE;
  310. g_u8_i2c_mode = PDA2_MODE;
  311. u8_buf[0] = RAYDIUM_I2C_PDA_CMD;
  312. u8_buf[1] = (unsigned char)u32_addr;
  313. u8_buf[2] = (unsigned char)(u32_addr >> 8);
  314. u8_buf[3] = (unsigned char)(u32_addr >> 16);
  315. u8_buf[4] = (unsigned char)(u32_addr >> 24);
  316. u8_buf[5] = u8_mode;
  317. /*for (u8_retry = 0; u8_retry < SYN_I2C_RETRY_TIMES; u8_retry++) {*/
  318. if (i2c_transfer(g_raydium_ts->client->adapter, msg, 2) == 2) {
  319. i32_ret = u16_length;
  320. /*break;*/
  321. }
  322. i32_ret = u16_length;
  323. /*usleep_range(500, 1500);*/
  324. /*}*/
  325. /*if (u8_retry == SYN_I2C_RETRY_TIMES) {*/
  326. /* LOGD(LOG_ERR, "[touch]%s: I2C read over retry limit\n", __func__);*/
  327. /* i32_ret = -EIO;*/
  328. /*}*/
  329. return i32_ret;
  330. }
  331. int raydium_i2c_pda_set_address(unsigned int u32_address,
  332. unsigned char u8_mode)
  333. {
  334. int i32_ret = 0;
  335. unsigned char u8_retry;
  336. unsigned char u8_buf[RAD_I2C_PDA_ADDRESS_LENGTH];
  337. struct i2c_client *client = g_raydium_ts->client;
  338. client->addr = RAYDIUM_I2C_EID;
  339. u8_buf[0] = (u32_address & 0x0000FF00) >> 8;
  340. u8_buf[1] = (u32_address & 0x00FF0000) >> 16;
  341. u8_buf[2] = (u32_address & 0xFF000000) >> 24;
  342. u8_buf[3] = u8_mode;
  343. for (u8_retry = 0; u8_retry < SYN_I2C_RETRY_TIMES; u8_retry++) {
  344. i32_ret = i2c_master_send(client, u8_buf,
  345. RAD_I2C_PDA_ADDRESS_LENGTH);
  346. if (i32_ret != RAD_I2C_PDA_ADDRESS_LENGTH) {
  347. LOGD(LOG_ERR, "[touch]%s: I2C retry %d\n",
  348. __func__, u8_retry + 1);
  349. usleep_range(500, 1500);
  350. } else {
  351. break;
  352. }
  353. }
  354. return (i32_ret == RAD_I2C_PDA_ADDRESS_LENGTH) ? i32_ret : -EIO;
  355. }
  356. /*device attribute raydium_i2c_pda2_mode used*/
  357. int raydium_i2c_pda_read(struct i2c_client *client,
  358. unsigned int u32_addr, unsigned char *u8_r_data,
  359. unsigned short u16_length)
  360. {
  361. int i32_ret;
  362. unsigned char u8_retry;
  363. unsigned char u8_mode = 0x00;
  364. unsigned char u8_buf;
  365. struct i2c_msg msg[] = {
  366. {
  367. .addr = RAYDIUM_I2C_NID,
  368. .flags = RAYDIUM_I2C_WRITE,
  369. .len = 1,
  370. .buf = &u8_buf,
  371. },
  372. {
  373. .addr = RAYDIUM_I2C_NID,
  374. .flags = RAYDIUM_I2C_READ,
  375. .len = u16_length,
  376. .buf = u8_r_data,
  377. },
  378. };
  379. if (u16_length == 4)
  380. u8_mode |= RAD_I2C_PDA_MODE_ENABLE |
  381. RAD_I2C_PDA_2_MODE_DISABLE |
  382. RAD_I2C_PDA_MODE_WORD_MODE;
  383. else
  384. u8_mode |= RAD_I2C_PDA_MODE_ENABLE |
  385. RAD_I2C_PDA_2_MODE_DISABLE;
  386. u8_mode |= 0x03;
  387. u8_buf = u32_addr & MASK_8BIT;
  388. i32_ret = raydium_i2c_pda_set_address(u32_addr, u8_mode);
  389. if (i32_ret != RAD_I2C_PDA_ADDRESS_LENGTH)
  390. goto exit;
  391. usleep_range(50, 80);
  392. for (u8_retry = 0; u8_retry < SYN_I2C_RETRY_TIMES; u8_retry++) {
  393. if (i2c_transfer(g_raydium_ts->client->adapter, msg, 2) == 2) {
  394. i32_ret = u16_length;
  395. break;
  396. }
  397. LOGD(LOG_ERR, "%s: I2C retry %d\n", __func__, u8_retry + 1);
  398. usleep_range(500, 1500);
  399. }
  400. if (u8_retry == SYN_I2C_RETRY_TIMES) {
  401. LOGD(LOG_ERR, "%s: I2C read over retry limit\n", __func__);
  402. i32_ret = -EIO;
  403. }
  404. exit:
  405. return i32_ret;
  406. }
  407. int raydium_i2c_pda_write(struct i2c_client *client,
  408. unsigned int u32_addr, unsigned char *u8_w_data,
  409. unsigned short u16_length)
  410. {
  411. int i32_ret;
  412. unsigned char u8_retry;
  413. unsigned char u8_mode = 0x00;
  414. unsigned char u8_buf[MAX_WRITE_PACKET_SIZE + 1];
  415. struct i2c_msg msg[] = {
  416. {
  417. .addr = RAYDIUM_I2C_NID,
  418. .flags = RAYDIUM_I2C_WRITE,
  419. .len = u16_length + 1,
  420. .buf = u8_buf,
  421. },
  422. };
  423. if (u16_length > MAX_WRITE_PACKET_SIZE)
  424. return -EINVAL;
  425. if (u16_length == 4)
  426. u8_mode |= RAD_I2C_PDA_MODE_ENABLE |
  427. RAD_I2C_PDA_2_MODE_DISABLE |
  428. RAD_I2C_PDA_MODE_WORD_MODE;
  429. else
  430. u8_mode |= RAD_I2C_PDA_MODE_ENABLE |
  431. RAD_I2C_PDA_2_MODE_DISABLE;
  432. u8_buf[0] = u32_addr & MASK_8BIT;
  433. memcpy(&u8_buf[1], u8_w_data, u16_length);
  434. i32_ret = raydium_i2c_pda_set_address(u32_addr, u8_mode);
  435. if (i32_ret != RAD_I2C_PDA_ADDRESS_LENGTH)
  436. goto exit;
  437. usleep_range(50, 80);
  438. for (u8_retry = 0; u8_retry < SYN_I2C_RETRY_TIMES; u8_retry++) {
  439. if (i2c_transfer(client->adapter, msg, 1) == 1) {
  440. i32_ret = u16_length;
  441. break;
  442. }
  443. LOGD(LOG_ERR, "[touch]%s: I2C retry %d\n", __func__, u8_retry + 1);
  444. usleep_range(500, 1500);
  445. }
  446. if (u8_retry == SYN_I2C_RETRY_TIMES) {
  447. LOGD(LOG_ERR, "[touch]%s: I2C write over retry limit\n", __func__);
  448. i32_ret = -EIO;
  449. }
  450. exit:
  451. return i32_ret;
  452. }
  453. int handle_i2c_pda_read(struct i2c_client *client,
  454. unsigned int u32_addr, unsigned char *u8_r_data,
  455. unsigned short u16_length)
  456. {
  457. if ((g_u8_i2c_mode & PDA_MODE) != 0) {
  458. if (raydium_i2c_pda_read(client, u32_addr, u8_r_data, u16_length) == ERROR) {
  459. LOGD(LOG_ERR, "[touch] handle_ic_write I2C NG!\r\n");
  460. return ERROR;
  461. }
  462. } else {
  463. if (raydium_i2c_read_pda_via_pda2(client, u32_addr, u8_r_data, u16_length) == ERROR) {
  464. LOGD(LOG_ERR, "[touch] handle_ic_write I2C via_pda2 NG!\r\n");
  465. return ERROR;
  466. }
  467. }
  468. return SUCCESS;
  469. }
  470. int handle_i2c_pda_write(struct i2c_client *client,
  471. unsigned int u32_addr, unsigned char *u8_w_data,
  472. unsigned short u16_length)
  473. {
  474. if ((g_u8_i2c_mode & PDA_MODE) != 0) {
  475. if (raydium_i2c_pda_write(client, u32_addr, u8_w_data, u16_length) == ERROR) {
  476. LOGD(LOG_ERR, "[touch] handle_ic_write I2C NG!\r\n");
  477. return ERROR;
  478. }
  479. } else {
  480. if (raydium_i2c_write_pda_via_pda2(client, u32_addr, u8_w_data, u16_length) == ERROR) {
  481. LOGD(LOG_ERR, "[touch] handle_ic_write I2C via_pda2 NG!\r\n");
  482. return ERROR;
  483. }
  484. }
  485. return SUCCESS;
  486. }
  487. int raydium_i2c_pda2_set_page(struct i2c_client *client,
  488. unsigned int is_suspend,
  489. unsigned char u8_page)
  490. {
  491. int i32_ret = -1;
  492. unsigned char u8_retry;
  493. unsigned int u8_ret = (is_suspend) ? 10 : 2;
  494. unsigned char u8_buf[RAYDIUM_I2C_PDA2_PAGE_LENGTH];
  495. struct i2c_msg msg[] = {
  496. {
  497. .addr = RAYDIUM_I2C_NID,
  498. .flags = RAYDIUM_I2C_WRITE,
  499. .len = RAYDIUM_I2C_PDA2_PAGE_LENGTH,
  500. .buf = u8_buf,
  501. },
  502. };
  503. u8_buf[0] = RAYDIUM_PDA2_PAGE_ADDR;
  504. u8_buf[1] = u8_page;
  505. for (; u8_ret > 0; u8_ret--) {
  506. for (u8_retry = 0; u8_retry < SYN_I2C_RETRY_TIMES; u8_retry++) {
  507. if (i2c_transfer(client->adapter, msg, 1) == 1) {
  508. i32_ret = RAYDIUM_I2C_PDA2_PAGE_LENGTH;
  509. break;
  510. }
  511. usleep_range(500, 1500);
  512. }
  513. if (i32_ret == RAYDIUM_I2C_PDA2_PAGE_LENGTH)
  514. break;
  515. usleep_range(2000, 5000);
  516. }
  517. if (u8_ret == 0) {
  518. LOGD(LOG_ERR, "[touch]%s: I2C write over retry limit\n", __func__);
  519. i32_ret = -EIO;
  520. }
  521. return i32_ret;
  522. }
  523. int raydium_i2c_pda2_read(struct i2c_client *client,
  524. unsigned char u8_addr,
  525. unsigned char *u8_r_data,
  526. unsigned short u16_length)
  527. {
  528. int i32_ret = -1;
  529. unsigned char u8_retry;
  530. struct i2c_msg msg[] = {
  531. {
  532. .addr = RAYDIUM_I2C_NID,
  533. .flags = RAYDIUM_I2C_WRITE,
  534. .len = 1,
  535. .buf = &u8_addr,
  536. },
  537. {
  538. .addr = RAYDIUM_I2C_NID,
  539. .flags = RAYDIUM_I2C_READ,
  540. .len = u16_length,
  541. .buf = u8_r_data,
  542. },
  543. };
  544. g_u8_i2c_mode = PDA2_MODE;
  545. for (u8_retry = 0; u8_retry < SYN_I2C_RETRY_TIMES; u8_retry++) {
  546. if (i2c_transfer(g_raydium_ts->client->adapter, msg, 2) == 2) {
  547. i32_ret = u16_length;
  548. break;
  549. }
  550. usleep_range(500, 1500);
  551. }
  552. if (u8_retry == SYN_I2C_RETRY_TIMES) {
  553. LOGD(LOG_ERR, "[touch]%s: I2C read over retry limit\n", __func__);
  554. i32_ret = -EIO;
  555. }
  556. return i32_ret;
  557. }
  558. int raydium_i2c_pda2_write(struct i2c_client *client,
  559. unsigned char u8_addr,
  560. unsigned char *u8_w_data,
  561. unsigned short u16_length)
  562. {
  563. int i32_ret = -1;
  564. unsigned char u8_retry;
  565. unsigned char u8_buf[MAX_WRITE_PACKET_SIZE + 1];
  566. struct i2c_msg msg[] = {
  567. {
  568. .addr = RAYDIUM_I2C_NID,
  569. .flags = RAYDIUM_I2C_WRITE,
  570. .len = u16_length + 1,
  571. .buf = u8_buf,
  572. },
  573. };
  574. if (u16_length > MAX_WRITE_PACKET_SIZE)
  575. return -EINVAL;
  576. g_u8_i2c_mode = PDA2_MODE;
  577. u8_buf[0] = u8_addr;
  578. memcpy(&u8_buf[1], u8_w_data, u16_length);
  579. for (u8_retry = 0; u8_retry < SYN_I2C_RETRY_TIMES; u8_retry++) {
  580. if (i2c_transfer(client->adapter, msg, 1) == 1) {
  581. i32_ret = u16_length;
  582. break;
  583. }
  584. usleep_range(500, 1500);
  585. }
  586. if (u8_retry == SYN_I2C_RETRY_TIMES) {
  587. LOGD(LOG_ERR, "[touch]%s: I2C write over retry limit\n", __func__);
  588. i32_ret = -EIO;
  589. }
  590. return i32_ret;
  591. }
  592. void raydium_irq_control(bool enable)
  593. {
  594. if (enable) {
  595. if (g_raydium_ts->irq_enabled) {
  596. /*mutex_unlock(&ts->lock);*/
  597. LOGD(LOG_INFO, "[touch]Already enable irq\n");
  598. return;
  599. }
  600. /* Clear interrupts first */
  601. if (g_raydium_ts->blank != FB_BLANK_POWERDOWN) {
  602. if (g_u8_i2c_mode == PDA2_MODE) {
  603. mutex_lock(&g_raydium_ts->lock);
  604. if (raydium_i2c_pda2_set_page(g_raydium_ts->client,
  605. g_raydium_ts->is_suspend,
  606. RAYDIUM_PDA2_PAGE_0) < 0)
  607. LOGD(LOG_ERR, "[touch]set page fail%s\n",
  608. __func__);
  609. mutex_unlock(&g_raydium_ts->lock);
  610. usleep_range(500, 1500);
  611. }
  612. }
  613. while (g_raydium_ts->irq_desc->depth > 0) {
  614. LOGD(LOG_INFO, "[touch]irq enable\n");
  615. g_raydium_ts->irq_enabled = true;
  616. enable_irq(g_raydium_ts->irq);
  617. }
  618. } else {
  619. if (g_raydium_ts->irq_enabled) {
  620. if (g_raydium_ts->irq_desc->depth == 0) {
  621. disable_irq(g_raydium_ts->irq);
  622. g_raydium_ts->irq_enabled = false;
  623. LOGD(LOG_INFO, "[touch]irq disable\n");
  624. }
  625. }
  626. }
  627. }
  628. unsigned char raydium_disable_i2c_deglitch(void)
  629. {
  630. unsigned int u32_buf = 0;
  631. unsigned char u8_retry = 3, u8_comfirm_time = 3;
  632. unsigned char u8_check = 0, u8_i = 0;
  633. unsigned int u32_i2c_deglitch = 0x07060000;
  634. unsigned char u8_buf[4];
  635. while (u8_retry--) {
  636. u32_buf = 0;
  637. handle_i2c_pda_read(g_raydium_ts->client, RAYDIUM_CHK_I2C_CMD,
  638. (unsigned char *)(&u32_buf), 4);
  639. if ((u32_buf & 0xFFFF0000) == 0xF3030000)
  640. u8_check++;
  641. }
  642. if (u8_check == 3) {
  643. LOGD(LOG_INFO, "[touch]PDA2 OK\r\n");
  644. return SUCCESS;
  645. }
  646. g_u8_i2c_mode = PDA_MODE;
  647. u8_retry = 100;
  648. while (u8_retry--) {
  649. u8_check = 0;
  650. for (u8_i = 0; u8_i < u8_comfirm_time; u8_i++) {
  651. /*check I2C*/
  652. u32_buf = 0;
  653. if (handle_i2c_pda_read(g_raydium_ts->client,
  654. RAYDIUM_PDA_I2CENG,
  655. (unsigned char *)(&u32_buf), 4) == ERROR) {
  656. LOGD(LOG_ERR, "[touch]%s: 1.handle_ic_read I2C NG!\r\n", __func__);
  657. break;
  658. }
  659. if (u32_buf == u32_i2c_deglitch)
  660. u8_check++;
  661. else
  662. break;
  663. }
  664. if (u8_check == u8_comfirm_time)
  665. break;
  666. if (handle_i2c_pda_write(g_raydium_ts->client, RAYDIUM_PDA_I2CENG,
  667. (unsigned char *)(&u32_i2c_deglitch), 4) == ERROR) {
  668. LOGD(LOG_ERR, "[touch]%s:handle_ic_write I2C NG!\r\n", __func__);
  669. continue;
  670. }
  671. u8_check = 0;
  672. for (u8_i = 0; u8_i < u8_comfirm_time; u8_i++) {
  673. /*check I2C*/
  674. u32_buf = 0;
  675. if (handle_i2c_pda_read(g_raydium_ts->client,
  676. RAYDIUM_PDA_I2CENG,
  677. (unsigned char *)(&u32_buf), 4) == ERROR) {
  678. LOGD(LOG_ERR, "[touch]%s:2.handle_ic_read I2C NG!\r\n", __func__);
  679. break;
  680. }
  681. if (u32_buf == u32_i2c_deglitch)
  682. u8_check++;
  683. else
  684. break;
  685. }
  686. if (u8_check == u8_comfirm_time)
  687. break;
  688. }
  689. if (u8_retry == 0)
  690. return ERROR;
  691. u32_buf = 0x03;
  692. if (handle_i2c_pda_write(g_raydium_ts->client, RAYDIUM_REG_GPIO_DEGLITCH,
  693. (unsigned char *)(&u32_buf), 4) == ERROR) {
  694. LOGD(LOG_ERR, "[touch]%s:3.handle_ic_write I2C NG!\r\n", __func__);
  695. return ERROR;
  696. }
  697. /*Disable PDA*/
  698. handle_i2c_pda_read(g_raydium_ts->client, RAYDIUM_PDA_I2CREG, u8_buf, 4);
  699. u8_buf[0] |= RAD_ENABLE_PDA2 | RAD_ENABLE_SI2;
  700. handle_i2c_pda_write(g_raydium_ts->client, RAYDIUM_PDA_I2CREG, u8_buf, 4);
  701. raydium_i2c_pda_set_address(0x50000628, DISABLE);
  702. g_u8_i2c_mode = PDA2_MODE;
  703. return SUCCESS;
  704. }
  705. #ifdef CONFIG_RM_SYSFS_DEBUG
  706. int raydium_i2c_mode_control(struct i2c_client *client,
  707. unsigned char u8_mode)
  708. {
  709. unsigned char u8_buf[4];
  710. switch (u8_mode) {
  711. case 0: /* Disable INT flag */
  712. LOGD(LOG_INFO, "[touch]RAD INT flag : %d\n", g_raydium_ts->irq_enabled);
  713. disable_irq(g_raydium_ts->irq);
  714. g_raydium_ts->irq_enabled = false;
  715. LOGD(LOG_INFO, "[touch]RAD irq disable\n");
  716. break;
  717. case 1: /* Enable INT flag */
  718. LOGD(LOG_INFO, "[touch]RAD INT flag : %d\n", g_raydium_ts->irq_enabled);
  719. enable_irq(g_raydium_ts->irq);
  720. g_raydium_ts->irq_enabled = true;
  721. LOGD(LOG_INFO, "[touch]RAD irq enable\n");
  722. break;
  723. case 2: /* Disable INT by raydium_irq_control */
  724. raydium_irq_control(DISABLE);
  725. break;
  726. case 3: /* Enable INT by raydium_irq_control */
  727. raydium_irq_control(ENABLE);
  728. break;
  729. case 4: /* Show RAD INT depth */
  730. LOGD(LOG_INFO, "[touch]RAD INT depth : %d\n", g_raydium_ts->irq_desc->depth);
  731. break;
  732. case 7:
  733. raydium_i2c_pda2_set_page(client,
  734. g_raydium_ts->is_suspend, RAYDIUM_PDA2_2_PDA);
  735. g_u8_i2c_mode = PDA_MODE;
  736. LOGD(LOG_INFO, "[touch]Disable PDA2_MODE\n");
  737. break;
  738. case 8:
  739. raydium_i2c_pda_read(client, RAYDIUM_PDA_I2CREG, u8_buf, 4);
  740. u8_buf[0] |= RAD_ENABLE_PDA2 | RAD_ENABLE_SI2;
  741. raydium_i2c_pda_write(client, RAYDIUM_PDA_I2CREG, u8_buf, 4);
  742. raydium_i2c_pda_set_address(RAYDIUM_PDA_I2CREG, DISABLE);
  743. g_u8_i2c_mode = PDA2_MODE;
  744. LOGD(LOG_INFO, "[touch]Enable PDA2_MODE\n");
  745. break;
  746. }
  747. return 0;
  748. }
  749. const struct attribute_group raydium_attr_group = {
  750. .attrs = raydium_attributes
  751. };
  752. /*create sysfs for debug update firmware*/
  753. static int raydium_create_sysfs(struct i2c_client *client)
  754. {
  755. int ret = -1;
  756. ret = sysfs_create_group(&(client->dev.kobj), &raydium_attr_group);
  757. if (ret) {
  758. LOGD(LOG_ERR, "[touch]failed to register sysfs\n");
  759. sysfs_remove_group(&client->dev.kobj, &raydium_attr_group);
  760. ret = -EIO;
  761. } else {
  762. LOGD(LOG_INFO, "[touch]create raydium sysfs attr_group successful\n");
  763. }
  764. return ret;
  765. }
  766. static void raydium_release_sysfs(struct i2c_client *client)
  767. {
  768. sysfs_remove_group(&client->dev.kobj, &raydium_attr_group);
  769. }
  770. #endif /*end of CONFIG_RM_SYSFS_DEBUG*/
  771. #ifdef ESD_SOLUTION_EN
  772. int raydium_esd_check(void)
  773. {
  774. int i32_ret = 0;
  775. unsigned char u8_esd_status[MAX_TCH_STATUS_PACKET_SIZE];
  776. mutex_lock(&g_raydium_ts->lock);
  777. if (g_u8_i2c_mode == PDA2_MODE) {
  778. i32_ret = raydium_i2c_pda2_set_page(g_raydium_ts->client,
  779. g_raydium_ts->is_suspend,
  780. RAYDIUM_PDA2_PAGE_0);
  781. if (i32_ret < 0)
  782. goto exit;
  783. /*read esd status*/
  784. i32_ret = raydium_i2c_pda2_read(g_raydium_ts->client,
  785. RAYDIUM_PDA2_TCH_RPT_STATUS_ADDR,
  786. u8_esd_status, MAX_TCH_STATUS_PACKET_SIZE);
  787. if (i32_ret < 0) {
  788. LOGD(LOG_ERR, "[touch]%s: failed to read data: %d\n",
  789. __func__, __LINE__);
  790. goto exit;
  791. }
  792. if (u8_esd_status[POS_FW_STATE] != 0x1A &&
  793. u8_esd_status[POS_FW_STATE] != 0xAA) {
  794. if (g_u8_resetflag == true) {
  795. LOGD(LOG_ERR, "[touch]%s -> filter abnormal irq\n"
  796. , __func__);
  797. goto exit;
  798. }
  799. LOGD(LOG_ERR, "[touch]%s -> abnormal irq, FW state = 0x%x\n",
  800. __func__, u8_esd_status[POS_FW_STATE]);
  801. g_u8_resetflag = false;
  802. i32_ret = -1;
  803. goto exit;
  804. }
  805. g_u8_resetflag = false;
  806. }
  807. exit:
  808. mutex_unlock(&g_raydium_ts->lock);
  809. LOGD(LOG_INFO, "[touch]%s\n", __func__);
  810. return i32_ret;
  811. }
  812. #endif
  813. static int raydium_touch_report(unsigned char *p_u8_buf,
  814. unsigned char u8_points_amount)
  815. {
  816. unsigned char u8_i, u8_j, u8_offset = 0, u8_pt_id;
  817. signed short i16_wx, i16_wy;
  818. /* number of touch points */
  819. unsigned char u8_touch_count = 0;
  820. //DECLARE_BITMAP(ids, g_raydium_ts->u8_max_touchs);
  821. unsigned long *ids = NULL;
  822. ids = kzalloc(sizeof(*ids)*BITS_TO_LONGS(g_raydium_ts->u8_max_touchs), GFP_KERNEL);
  823. if (!ids)
  824. return -ENOMEM;
  825. bitmap_zero(ids, g_raydium_ts->u8_max_touchs);
  826. for (u8_i = 0; u8_i < (g_raydium_ts->u8_max_touchs * 2); u8_i++) {
  827. gst_slot[u8_i].need_update = 0;
  828. gst_slot[u8_i].pt_report_offset = 0;
  829. }
  830. /*Check incoming point info*/
  831. for (u8_i = 0; u8_i < u8_points_amount; u8_i++) {
  832. u8_pt_id = p_u8_buf[POS_PT_ID + u8_i * LEN_PT];
  833. /* Current*/
  834. for (u8_j = 0; u8_j < g_raydium_ts->u8_max_touchs; u8_j++) {
  835. if (u8_pt_id == gst_slot[u8_j].pt_id) {
  836. gst_slot[u8_j].need_update = 1;
  837. gst_slot[u8_j].pt_report_offset = u8_i;
  838. break;
  839. }
  840. }
  841. /* New coming*/
  842. if (u8_j == g_raydium_ts->u8_max_touchs) {
  843. for (u8_j = g_raydium_ts->u8_max_touchs;
  844. u8_j < (g_raydium_ts->u8_max_touchs * 2); u8_j++) {
  845. if (!gst_slot[u8_j].need_update) {
  846. gst_slot[u8_j].pt_id = u8_pt_id;
  847. gst_slot[u8_j].need_update = 1;
  848. gst_slot[u8_j].pt_report_offset = u8_i;
  849. LOGD(LOG_INFO, "[touch]x:%d,y:%d\n",
  850. p_u8_buf[POS_X_L + u8_offset] |
  851. p_u8_buf[POS_X_H + u8_offset] << 8,
  852. p_u8_buf[POS_Y_L + u8_offset] |
  853. p_u8_buf[POS_Y_H + u8_offset] << 8);
  854. break;
  855. }
  856. }
  857. }
  858. }
  859. /*Release slot with non-occupied point*/
  860. for (u8_i = 0; u8_i < g_raydium_ts->u8_max_touchs; u8_i++) {
  861. if (!gst_slot[u8_i].need_update) {
  862. input_mt_slot(g_raydium_ts->input_dev, u8_i);
  863. input_mt_report_slot_state(g_raydium_ts->input_dev,
  864. MT_TOOL_FINGER, false);
  865. gst_slot[u8_i].pt_id = 0xFF;
  866. gst_slot[u8_i].pt_report_offset = 0;
  867. gst_slot[u8_i].need_update = 0;
  868. }
  869. }
  870. /*Assign new one to non-occupied slot*/
  871. for (u8_i = g_raydium_ts->u8_max_touchs;
  872. u8_i < (g_raydium_ts->u8_max_touchs * 2); u8_i++) {
  873. if (gst_slot[u8_i].need_update) {
  874. for (u8_j = 0; u8_j < g_raydium_ts->u8_max_touchs; u8_j++) {
  875. if (!gst_slot[u8_j].need_update) {
  876. gst_slot[u8_j] = gst_slot[u8_i];
  877. gst_slot[u8_i] = gst_slot_init;
  878. break;
  879. }
  880. }
  881. } else {
  882. break;
  883. }
  884. }
  885. for (u8_i = 0; u8_i < g_raydium_ts->u8_max_touchs; u8_i++) {
  886. if (gst_slot[u8_i].need_update) {
  887. u8_offset = gst_slot[u8_i].pt_report_offset * LEN_PT;
  888. g_raydium_ts->x_pos[u8_i] = p_u8_buf[POS_X_L + u8_offset] |
  889. p_u8_buf[POS_X_H + u8_offset] << BYTE_SHIFT;
  890. g_raydium_ts->y_pos[u8_i] = p_u8_buf[POS_Y_L + u8_offset] |
  891. p_u8_buf[POS_Y_H + u8_offset] << BYTE_SHIFT;
  892. g_raydium_ts->pressure = p_u8_buf[POS_PRESSURE_L + u8_offset] |
  893. p_u8_buf[POS_PRESSURE_H + u8_offset] << BYTE_SHIFT;
  894. i16_wx = p_u8_buf[POS_WX_L + u8_offset] |
  895. p_u8_buf[POS_WX_H + u8_offset] << BYTE_SHIFT;
  896. i16_wy = p_u8_buf[POS_WY_L + u8_offset] |
  897. p_u8_buf[POS_WY_H + u8_offset] << BYTE_SHIFT;
  898. input_mt_slot(g_raydium_ts->input_dev, u8_i);
  899. input_mt_report_slot_state(g_raydium_ts->input_dev,
  900. MT_TOOL_FINGER, true);
  901. __set_bit(u8_i, ids);
  902. input_report_abs(g_raydium_ts->input_dev,
  903. ABS_MT_POSITION_X, g_raydium_ts->x_pos[u8_i]);
  904. input_report_abs(g_raydium_ts->input_dev,
  905. ABS_MT_POSITION_Y, g_raydium_ts->y_pos[u8_i]);
  906. input_report_abs(g_raydium_ts->input_dev,
  907. ABS_MT_PRESSURE, g_raydium_ts->pressure);
  908. input_report_abs(g_raydium_ts->input_dev,
  909. ABS_MT_TOUCH_MAJOR, max(i16_wx, i16_wy));
  910. input_report_abs(g_raydium_ts->input_dev,
  911. ABS_MT_TOUCH_MINOR, min(i16_wx, i16_wy));
  912. LOGD(LOG_DEBUG, "[touch:%d]x:%d,y:%d\n",
  913. u8_i,
  914. p_u8_buf[POS_X_L + u8_offset] |
  915. p_u8_buf[POS_X_H + u8_offset] << 8,
  916. p_u8_buf[POS_Y_L + u8_offset] |
  917. p_u8_buf[POS_Y_H + u8_offset] << 8);
  918. u8_touch_count++;
  919. }
  920. }
  921. input_report_key(g_raydium_ts->input_dev,
  922. BTN_TOUCH, u8_touch_count > 0);
  923. input_report_key(g_raydium_ts->input_dev,
  924. BTN_TOOL_FINGER, u8_touch_count > 0);
  925. for (u8_i = 0; u8_i < g_raydium_ts->u8_max_touchs; u8_i++) {
  926. if (test_bit(u8_i, ids))
  927. continue;
  928. input_mt_slot(g_raydium_ts->input_dev, u8_i);
  929. input_mt_report_slot_state(g_raydium_ts->input_dev,
  930. MT_TOOL_FINGER, false);
  931. }
  932. input_sync(g_raydium_ts->input_dev);
  933. kfree(ids);
  934. return 0;
  935. }
  936. int raydium_read_touchdata(unsigned char *p_u8_tp_status, unsigned char *p_u8_buf)
  937. {
  938. int i32_ret = 0;
  939. unsigned char u8_points_amount;
  940. static unsigned char u8_seq_no;
  941. unsigned char u8_retry;
  942. unsigned char u8_read_size;
  943. unsigned char u8_read_buf[MAX_REPORT_PACKET_SIZE];
  944. u8_retry = 3;
  945. mutex_lock(&g_raydium_ts->lock);
  946. while (u8_retry != 0) {
  947. i32_ret = raydium_i2c_pda2_set_page(g_raydium_ts->client,
  948. g_raydium_ts->is_suspend, RAYDIUM_PDA2_PAGE_0);
  949. if (i32_ret < 0) {
  950. msleep(250);
  951. u8_retry--;
  952. } else
  953. break;
  954. }
  955. if (u8_retry == 0) {
  956. LOGD(LOG_ERR, "[touch]%s: failed to set page\n", __func__);
  957. goto reset_error;
  958. }
  959. memset(u8_read_buf, 0, MAX_REPORT_PACKET_SIZE);
  960. memset(p_u8_buf, 0, MAX_REPORT_PACKET_SIZE);
  961. memset(p_u8_tp_status, 0, MAX_TCH_STATUS_PACKET_SIZE);
  962. u8_read_size = 4 + MAX_TOUCH_NUM * LEN_PT + 1;
  963. /*read touch point information*/
  964. i32_ret = raydium_i2c_pda2_read(g_raydium_ts->client,
  965. RAYDIUM_PDA2_TCH_RPT_STATUS_ADDR,
  966. u8_read_buf, u8_read_size);
  967. if (i32_ret < 0) {
  968. LOGD(LOG_ERR, "[touch]%s: failed to read data: %d\n",
  969. __func__, __LINE__);
  970. goto exit_error;
  971. }
  972. memcpy(p_u8_tp_status, &u8_read_buf[0], MAX_TCH_STATUS_PACKET_SIZE);
  973. #ifdef ESD_SOLUTION_EN
  974. if (p_u8_tp_status[POS_FW_STATE] != 0x1A &&
  975. p_u8_tp_status[POS_FW_STATE] != 0xAA) {
  976. if (g_u8_resetflag == true) {
  977. LOGD(LOG_ERR, "[touch]%s -> filter irq, FW state = 0x%x\n",
  978. __func__, p_u8_tp_status[POS_FW_STATE]);
  979. i32_ret = -1;
  980. g_u8_resetflag = false;
  981. goto exit_error;
  982. }
  983. LOGD(LOG_ERR, "[touch]%s -> abnormal irq, FW state = 0x%x\n",
  984. __func__, p_u8_tp_status[POS_FW_STATE]);
  985. i32_ret = -1;
  986. goto reset_error;
  987. }
  988. #endif
  989. /* inform IC to prepare next report*/
  990. if (u8_seq_no == p_u8_tp_status[POS_SEQ]) {
  991. p_u8_tp_status[POS_SEQ] = 0;
  992. i32_ret = raydium_i2c_pda2_write(g_raydium_ts->client,
  993. RAYDIUM_PDA2_TCH_RPT_STATUS_ADDR, p_u8_tp_status, 1);
  994. if (i32_ret < 0) {
  995. LOGD(LOG_ERR, "[touch]%s: write data failed: %d\n", __func__, i32_ret);
  996. goto exit_error;
  997. }
  998. LOGD(LOG_WARNING, "[touch]%s -> report not updated.\n", __func__);
  999. goto exit_error;
  1000. }
  1001. u8_seq_no = p_u8_tp_status[POS_SEQ];
  1002. p_u8_tp_status[POS_SEQ] = 0;
  1003. i32_ret = raydium_i2c_pda2_write(g_raydium_ts->client,
  1004. RAYDIUM_PDA2_TCH_RPT_STATUS_ADDR, p_u8_tp_status, 1);
  1005. if (i32_ret < 0) {
  1006. LOGD(LOG_ERR, "[touch]%s: write data failed: %d\n", __func__, i32_ret);
  1007. goto exit_error;
  1008. }
  1009. u8_points_amount = p_u8_tp_status[POS_PT_AMOUNT];
  1010. if (u8_points_amount > MAX_TOUCH_NUM)
  1011. goto exit_error;
  1012. memcpy(p_u8_buf, &u8_read_buf[4], u8_points_amount * LEN_PT);
  1013. raydium_touch_report(p_u8_buf, u8_points_amount);
  1014. exit_error:
  1015. mutex_unlock(&g_raydium_ts->lock);
  1016. return i32_ret;
  1017. reset_error:
  1018. mutex_unlock(&g_raydium_ts->lock);
  1019. #ifdef ESD_SOLUTION_EN
  1020. u8_retry = 3;
  1021. while (u8_retry != 0) {
  1022. i32_ret = raydium_hw_reset_fun(g_raydium_ts->client);
  1023. LOGD(LOG_ERR, "[touch]%s: HW reset\n", __func__);
  1024. if (i32_ret < 0) {
  1025. msleep(100);
  1026. u8_retry--;
  1027. } else
  1028. break;
  1029. }
  1030. #endif
  1031. return i32_ret;
  1032. }
  1033. static void raydium_work_handler(struct work_struct *work)
  1034. {
  1035. int i32_ret = 0;
  1036. unsigned char u8_tp_status[MAX_TCH_STATUS_PACKET_SIZE];
  1037. unsigned char u8_buf[MAX_REPORT_PACKET_SIZE];
  1038. #ifdef GESTURE_EN
  1039. unsigned char u8_i;
  1040. if (g_u8_i2c_mode == PDA2_MODE) {
  1041. i32_ret = raydium_read_touchdata(u8_tp_status, u8_buf);
  1042. if (i32_ret < 0) {
  1043. LOGD(LOG_ERR, "[touch]%s, read_touchdata error, ret:%d\n",
  1044. __func__, i32_ret);
  1045. return;
  1046. }
  1047. }
  1048. /*when display on can use palm to suspend*/
  1049. if (g_raydium_ts->blank == FB_BLANK_UNBLANK) {
  1050. if (u8_tp_status[POS_GES_STATUS] == RAD_PALM_ENABLE) {
  1051. if (g_raydium_ts->is_palm == 0) {
  1052. /* release all touches*/
  1053. for (u8_i = 0; u8_i < g_raydium_ts->u8_max_touchs;
  1054. u8_i++) {
  1055. input_mt_slot(g_raydium_ts->input_dev,
  1056. u8_i);
  1057. input_mt_report_slot_state(
  1058. g_raydium_ts->input_dev,
  1059. MT_TOOL_FINGER, false);
  1060. }
  1061. input_mt_report_pointer_emulation(
  1062. g_raydium_ts->input_dev,
  1063. false);
  1064. /*press sleep key*/
  1065. input_report_key(g_raydium_ts->input_dev,
  1066. KEY_SLEEP, true);
  1067. input_sync(g_raydium_ts->input_dev);
  1068. LOGD(LOG_INFO, "[touch]palm_status = %d.\n",
  1069. u8_tp_status[POS_GES_STATUS]);
  1070. g_raydium_ts->is_palm = 1;
  1071. /*goto exit;*/
  1072. }
  1073. } else if ((u8_tp_status[POS_GES_STATUS]
  1074. == RAD_PALM_DISABLE)
  1075. && (g_raydium_ts->is_palm == 1)) {
  1076. LOGD(LOG_INFO, "[touch]leave palm mode.\n");
  1077. input_report_key(g_raydium_ts->input_dev,
  1078. KEY_SLEEP, false);
  1079. input_sync(g_raydium_ts->input_dev);
  1080. /*raydium_irq_control(raydium_ts, DISABLE);*/
  1081. g_raydium_ts->is_palm = 0;
  1082. /*goto exit;*/
  1083. }
  1084. } else if (g_raydium_ts->blank == FB_BLANK_VSYNC_SUSPEND ||
  1085. g_raydium_ts->blank == FB_BLANK_POWERDOWN) {
  1086. /*need check small area*/
  1087. if (u8_tp_status[POS_GES_STATUS] == RAD_WAKE_UP
  1088. && g_u8_wakeup_flag == false) {
  1089. input_report_key(g_raydium_ts->input_dev, KEY_POWER, true);
  1090. usleep_range(9500, 10500);
  1091. input_sync(g_raydium_ts->input_dev);
  1092. input_report_key(g_raydium_ts->input_dev, KEY_POWER, false);
  1093. input_sync(g_raydium_ts->input_dev);
  1094. LOGD(LOG_INFO, "[touch]display wake up with g_u8_resetflag true\n");
  1095. /*goto exit;*/
  1096. }
  1097. }
  1098. #else
  1099. if (g_u8_i2c_mode == PDA2_MODE) {
  1100. i32_ret = raydium_read_touchdata(u8_tp_status, u8_buf);
  1101. if (i32_ret < 0) {
  1102. LOGD(LOG_ERR, "[touch]%s, read_touchdata error, ret:%d\n",
  1103. __func__, i32_ret);
  1104. }
  1105. }
  1106. #endif
  1107. }
  1108. /*The raydium device will signal the host about TRIGGER_FALLING.
  1109. *Processed when the interrupt is asserted.
  1110. */
  1111. static irqreturn_t raydium_ts_interrupt(int irq, void *dev_id)
  1112. {
  1113. bool result = false;
  1114. LOGD(LOG_DEBUG, "[touch]%s\n", __func__);
  1115. /*For bootloader wrt/erase flash and software reset interrupt*/
  1116. if ((g_u8_raydium_flag & ENG_MODE) != 0) {
  1117. LOGD(LOG_INFO, "[touch]RAD_ENG_MODE\n");
  1118. g_u8_raydium_flag |= INT_FLAG;
  1119. } else {
  1120. if (!work_pending(&g_raydium_ts->work)) {
  1121. /* Clear interrupts*/
  1122. result = queue_work(g_raydium_ts->workqueue,
  1123. &g_raydium_ts->work);
  1124. if (!result) {
  1125. /*queue_work fail*/
  1126. LOGD(LOG_ERR, "[touch]queue_work fail.\n");
  1127. }
  1128. } else {
  1129. /*work pending*/
  1130. mutex_lock(&g_raydium_ts->lock);
  1131. if (raydium_i2c_pda2_set_page(g_raydium_ts->client,
  1132. g_raydium_ts->is_suspend,
  1133. RAYDIUM_PDA2_PAGE_0) < 0) {
  1134. LOGD(LOG_ERR, "[touch]%s: failed to set page in work_pending\n",
  1135. __func__);
  1136. }
  1137. mutex_unlock(&g_raydium_ts->lock);
  1138. LOGD(LOG_WARNING, "[touch]work_pending\n");
  1139. }
  1140. }
  1141. return IRQ_HANDLED;
  1142. }
  1143. static int raydium_check_i2c_ready(unsigned short *u16_i2c_data)
  1144. {
  1145. unsigned char u8_buf[4];
  1146. int i32_ret = ERROR;
  1147. mutex_lock(&g_raydium_ts->lock);
  1148. if (g_u8_i2c_mode == PDA2_MODE) {
  1149. i32_ret = handle_i2c_pda_read(g_raydium_ts->client,
  1150. RAYDIUM_CHK_I2C_CMD, u8_buf, 4);
  1151. if (i32_ret < 0)
  1152. goto exit_error;
  1153. if (u8_buf[3] != 0xF3) {
  1154. LOGD(LOG_ERR, "[touch]PDA2 read i2c fail\n");
  1155. g_u8_i2c_mode = PDA_MODE;
  1156. i32_ret = handle_i2c_pda_read(g_raydium_ts->client,
  1157. RAYDIUM_CHK_I2C_CMD, u8_buf, 4);
  1158. if (i32_ret < 0)
  1159. goto exit_error;
  1160. }
  1161. } else {
  1162. i32_ret = handle_i2c_pda_read(g_raydium_ts->client,
  1163. RAYDIUM_CHK_I2C_CMD, u8_buf, 4);
  1164. if (i32_ret < 0)
  1165. goto exit_error;
  1166. }
  1167. *u16_i2c_data = u8_buf[3] << 8 | u8_buf[2];
  1168. LOGD(LOG_INFO, "[touch]RAD check I2C : 0x%02X%02X\n", u8_buf[3], u8_buf[2]);
  1169. exit_error:
  1170. mutex_unlock(&g_raydium_ts->lock);
  1171. return i32_ret;
  1172. }
  1173. #if defined(CONFIG_PM)
  1174. static void raydium_ts_do_suspend(void)
  1175. {
  1176. unsigned char u8_i = 0;
  1177. if (g_u8_raw_data_type == 0)
  1178. g_u8_resetflag = false;
  1179. if (g_raydium_ts->is_suspend == 1) {
  1180. LOGD(LOG_WARNING, "[touch]Already in suspend state\n");
  1181. return;
  1182. }
  1183. /*#ifndef GESTURE_EN*/
  1184. raydium_irq_control(DISABLE);
  1185. /*#endif*/
  1186. /*clear workqueue*/
  1187. if (!cancel_work_sync(&g_raydium_ts->work))
  1188. LOGD(LOG_DEBUG, "[touch]workqueue is empty!\n");
  1189. LOGD(LOG_INFO, "[touch]%s.\n", __func__);
  1190. /* release all touches */
  1191. for (u8_i = 0; u8_i < g_raydium_ts->u8_max_touchs; u8_i++) {
  1192. input_mt_slot(g_raydium_ts->input_dev, u8_i);
  1193. input_mt_report_slot_state(g_raydium_ts->input_dev,
  1194. MT_TOOL_FINGER,
  1195. false);
  1196. }
  1197. input_mt_report_pointer_emulation(g_raydium_ts->input_dev, false);
  1198. input_sync(g_raydium_ts->input_dev);
  1199. #ifdef GESTURE_EN
  1200. if (device_may_wakeup(&g_raydium_ts->client->dev)) {
  1201. LOGD(LOG_INFO, "[touch]Device may wakeup\n");
  1202. if (!enable_irq_wake(g_raydium_ts->irq))
  1203. g_raydium_ts->irq_wake = true;
  1204. } else {
  1205. LOGD(LOG_INFO, "[touch]Device not wakeup\n");
  1206. }
  1207. raydium_irq_control(ENABLE);
  1208. #endif
  1209. g_raydium_ts->is_suspend = 1;
  1210. }
  1211. static void raydium_ts_do_resume(void)
  1212. {
  1213. #ifdef ESD_SOLUTION_EN
  1214. int i32_ret = 0;
  1215. unsigned char u8_retry = 0;
  1216. #endif
  1217. LOGD(LOG_INFO, "[touch]%s, %d.\n", __func__, g_raydium_ts->is_suspend);
  1218. if (g_raydium_ts->is_suspend == 0) {
  1219. LOGD(LOG_WARNING, "[touch]Already in resume state\n");
  1220. return;
  1221. }
  1222. /* clear interrupts*/
  1223. mutex_lock(&g_raydium_ts->lock);
  1224. if (raydium_i2c_pda2_set_page(g_raydium_ts->client,
  1225. g_raydium_ts->is_suspend, RAYDIUM_PDA2_PAGE_0) < 0) {
  1226. LOGD(LOG_ERR, "[touch]%s: failed to set page\n", __func__);
  1227. mutex_unlock(&g_raydium_ts->lock);
  1228. return;
  1229. }
  1230. mutex_unlock(&g_raydium_ts->lock);
  1231. /* clear workqueue*/
  1232. if (!cancel_work_sync(&g_raydium_ts->work))
  1233. LOGD(LOG_DEBUG, "[ raydium ]workqueue is empty!\n");
  1234. #ifdef ESD_SOLUTION_EN
  1235. if (g_u8_checkflag == true) {
  1236. i32_ret = raydium_esd_check();
  1237. if (i32_ret < 0) {
  1238. u8_retry = 3;
  1239. while (u8_retry != 0) {
  1240. i32_ret = raydium_hw_reset_fun(g_raydium_ts->client);
  1241. if (i32_ret < 0) {
  1242. msleep(100);
  1243. u8_retry--;
  1244. } else
  1245. break;
  1246. }
  1247. }
  1248. g_u8_checkflag = false;
  1249. }
  1250. #endif
  1251. raydium_irq_control(ENABLE);
  1252. #ifdef GESTURE_EN
  1253. if (device_may_wakeup(&g_raydium_ts->client->dev)) {
  1254. LOGD(LOG_INFO, "[touch]Device may wakeup\n");
  1255. if (g_raydium_ts->irq_wake) {
  1256. disable_irq_wake(g_raydium_ts->irq);
  1257. g_raydium_ts->irq_wake = false;
  1258. }
  1259. } else
  1260. LOGD(LOG_INFO, "[touch]Device not wakeup\n");
  1261. #endif
  1262. g_raydium_ts->is_suspend = 0;
  1263. }
  1264. static int raydium_ts_suspend(struct device *dev)
  1265. {
  1266. raydium_ts_do_suspend();
  1267. return 0;
  1268. }
  1269. static int raydium_ts_resume(struct device *dev)
  1270. {
  1271. raydium_ts_do_resume();
  1272. return 0;
  1273. }
  1274. static const struct dev_pm_ops raydium_ts_pm_ops = {
  1275. #if (!defined(CONFIG_FB) && !defined(CONFIG_HAS_EARLYSUSPEND))
  1276. .suspend = raydium_ts_suspend,
  1277. .resume = raydium_ts_resume,
  1278. #endif /*end of CONFIG_PM*/
  1279. };
  1280. /*used for touch lock feature*/
  1281. static int raydium_ts_open(struct input_dev *input_dev)
  1282. {
  1283. //int i32_ret = 0;
  1284. LOGD(LOG_INFO, "[touch]%s()+\n", __func__);
  1285. LOGD(LOG_INFO, "[touch]ts->blank:%x\n", g_raydium_ts->blank);
  1286. if (g_raydium_ts->is_sleep == 1) {
  1287. mutex_lock(&g_raydium_ts->lock);
  1288. if (gpio_is_valid(g_raydium_ts->rst_gpio)) {
  1289. g_u8_resetflag = true;
  1290. gpio_set_value(g_raydium_ts->rst_gpio, 1);
  1291. gpio_set_value(g_raydium_ts->rst_gpio, 0);
  1292. msleep(RAYDIUM_RESET_INTERVAL_MSEC);/*5ms*/
  1293. gpio_set_value(g_raydium_ts->rst_gpio, 1);
  1294. msleep(RAYDIUM_RESET_DELAY_MSEC);/*100ms*/
  1295. g_u8_i2c_mode = PDA2_MODE;
  1296. }
  1297. mutex_unlock(&g_raydium_ts->lock);
  1298. raydium_irq_control(ENABLE);
  1299. g_raydium_ts->is_sleep = 0;
  1300. LOGD(LOG_INFO, "[touch]disable touch lock.\n");
  1301. }
  1302. //return i32_ret;
  1303. return 0;
  1304. }
  1305. static void raydium_ts_close(struct input_dev *input_dev)
  1306. {
  1307. int i32_ret = 0;
  1308. unsigned char u8_i = 0;
  1309. unsigned char u8_wbuffer[1];
  1310. LOGD(LOG_INFO, "[touch]%s()+\n", __func__);
  1311. if (g_raydium_ts->is_sleep == 1) {
  1312. LOGD(LOG_INFO, "[touch]touch lock already enabled.\n");
  1313. return;
  1314. }
  1315. raydium_irq_control(DISABLE);
  1316. for (u8_i = 0; u8_i < g_raydium_ts->u8_max_touchs; u8_i++) {
  1317. input_mt_slot(g_raydium_ts->input_dev, u8_i);
  1318. input_mt_report_slot_state(g_raydium_ts->input_dev,
  1319. MT_TOOL_FINGER,
  1320. false);
  1321. }
  1322. input_mt_report_pointer_emulation(g_raydium_ts->input_dev, false);
  1323. input_sync(g_raydium_ts->input_dev);
  1324. mutex_lock(&g_raydium_ts->lock);
  1325. i32_ret = raydium_i2c_pda2_set_page(g_raydium_ts->client,
  1326. g_raydium_ts->is_suspend, RAYDIUM_PDA2_PAGE_0);
  1327. if (i32_ret < 0) {
  1328. LOGD(LOG_ERR, "[touch]ret:%d\n", i32_ret);
  1329. goto exit_i2c_error;
  1330. }
  1331. u8_wbuffer[0] = RAYDIUM_HOST_CMD_PWR_SLEEP;
  1332. i32_ret = raydium_i2c_pda2_write(g_raydium_ts->client,
  1333. RAYDIUM_PDA2_HOST_CMD_ADDR,
  1334. u8_wbuffer,
  1335. 1);
  1336. if (i32_ret < 0) {
  1337. LOGD(LOG_ERR, "[touch]ret:%d\n", i32_ret);
  1338. goto exit_i2c_error;
  1339. }
  1340. mutex_unlock(&g_raydium_ts->lock);
  1341. g_raydium_ts->is_sleep = 1;
  1342. LOGD(LOG_INFO, "[touch]enable touch lock.\n");
  1343. return;
  1344. exit_i2c_error:
  1345. mutex_unlock(&g_raydium_ts->lock);
  1346. raydium_irq_control(ENABLE);
  1347. }
  1348. #else
  1349. static int raydium_ts_suspend(struct device *dev)
  1350. {
  1351. return 0;
  1352. }
  1353. static int raydium_ts_resume(struct device *dev)
  1354. {
  1355. return 0;
  1356. }
  1357. #endif /*end of CONFIG_FB*/
  1358. #if defined(CONFIG_FB)
  1359. static int fb_notifier_callback(struct notifier_block *self,
  1360. unsigned long event,
  1361. void *data)
  1362. {
  1363. struct fb_event *evdata = data;
  1364. int *blank;
  1365. if (evdata && evdata->data && event == FB_EVENT_BLANK &&
  1366. g_raydium_ts && g_raydium_ts->client) {
  1367. blank = evdata->data;
  1368. g_raydium_ts->blank = (*blank);
  1369. switch (*blank) {
  1370. /*screen on*/
  1371. case FB_BLANK_UNBLANK:
  1372. LOGD(LOG_INFO, "[touch]FB_BLANK_UNBLANK\n");
  1373. #ifdef GESTURE_EN
  1374. /* clear palm status */
  1375. g_raydium_ts->is_palm = 0;
  1376. #endif
  1377. raydium_ts_resume(&g_raydium_ts->client->dev);
  1378. break;
  1379. /*screen off*/
  1380. case FB_BLANK_POWERDOWN:
  1381. LOGD(LOG_INFO, "[touch]FB_BLANK_POWERDOWN\n");
  1382. #ifdef GESTURE_EN
  1383. /* clear palm status */
  1384. g_raydium_ts->is_palm = 0;
  1385. #endif
  1386. raydium_ts_suspend(&g_raydium_ts->client->dev);
  1387. break;
  1388. /*ambient mode*/
  1389. case FB_BLANK_VSYNC_SUSPEND:
  1390. LOGD(LOG_INFO, "[touch]FB_BLANK_VSYNC_SUSPEND\n");
  1391. #ifdef GESTURE_EN
  1392. /* clear palm status */
  1393. g_raydium_ts->is_palm = 0;
  1394. #endif
  1395. raydium_ts_suspend(&g_raydium_ts->client->dev);
  1396. break;
  1397. default:
  1398. break;
  1399. }
  1400. }
  1401. return 0;
  1402. }
  1403. static void raydium_register_notifier(void)
  1404. {
  1405. memset(&g_raydium_ts->fb_notif, 0, sizeof(g_raydium_ts->fb_notif));
  1406. g_raydium_ts->fb_notif.notifier_call = fb_notifier_callback;
  1407. /* register on the fb notifier and work with fb*/
  1408. if (fb_register_client(&g_raydium_ts->fb_notif))
  1409. LOGD(LOG_ERR, "[touch]register notifier failed\n");
  1410. }
  1411. static void raydium_unregister_notifier(void)
  1412. {
  1413. fb_unregister_client(&g_raydium_ts->fb_notif);
  1414. }
  1415. #elif defined(CONFIG_HAS_EARLYSUSPEND)
  1416. static void raydium_ts_early_suspend(struct early_suspend *handler)
  1417. {
  1418. raydium_ts_do_suspend();
  1419. }
  1420. static void raydium_ts_late_resume(struct early_suspend *handler)
  1421. {
  1422. raydium_ts_do_resume();
  1423. }
  1424. #endif /*end of CONFIG_FB*/
  1425. #ifdef CONFIG_OF
  1426. static int raydium_get_dt_coords(struct device *dev, char *name,
  1427. struct raydium_ts_platform_data *pdata)
  1428. {
  1429. u32 coords[COORDS_ARR_SIZE];
  1430. struct property *prop;
  1431. struct device_node *np = dev->of_node;
  1432. int coords_size, rc;
  1433. prop = of_find_property(np, name, NULL);
  1434. if (!prop)
  1435. return -EINVAL;
  1436. if (!prop->value)
  1437. return -ENODATA;
  1438. coords_size = prop->length / sizeof(u32);
  1439. if (coords_size != COORDS_ARR_SIZE) {
  1440. LOGD(LOG_ERR, "[touch]invalid %s\n", name);
  1441. return -EINVAL;
  1442. }
  1443. rc = of_property_read_u32_array(np, name, coords, coords_size);
  1444. if (rc && (rc != -EINVAL)) {
  1445. LOGD(LOG_ERR, "[touch]unable to read %s\n", name);
  1446. return rc;
  1447. }
  1448. if (!strcmp(name, "raydium,display-coords")) {
  1449. pdata->x_min = coords[0];
  1450. pdata->y_min = coords[1];
  1451. pdata->x_max = coords[2];
  1452. pdata->y_max = coords[3];
  1453. } else {
  1454. LOGD(LOG_ERR, "[touch]unsupported property %s\n", name);
  1455. return -EINVAL;
  1456. }
  1457. return 0;
  1458. }
  1459. static int raydium_parse_dt(struct device *dev,
  1460. struct raydium_ts_platform_data *pdata)
  1461. {
  1462. struct device_node *np = dev->of_node;
  1463. int rc = 0;
  1464. u32 temp_val = 0;
  1465. pdata->name = RAYDIUM_NAME;
  1466. rc = raydium_get_dt_coords(dev, "raydium,display-coords", pdata);
  1467. if (rc)
  1468. return rc;
  1469. /* reset, irq gpio info */
  1470. pdata->reset_gpio = of_get_named_gpio_flags(np,
  1471. "raydium,reset-gpio",
  1472. 0,
  1473. &pdata->reset_gpio_flags);
  1474. //if (pdata->reset_gpio < 0)
  1475. if ((s32)(pdata->reset_gpio) < 0)
  1476. return pdata->reset_gpio;
  1477. pdata->irq_gpio = of_get_named_gpio_flags(np,
  1478. "raydium,irq-gpio",
  1479. 0,
  1480. &pdata->irq_gpio_flags);
  1481. //if (pdata->irq_gpio < 0)
  1482. if ((s32)(pdata->irq_gpio) < 0)
  1483. return pdata->irq_gpio;
  1484. rc = of_property_read_u32(np,
  1485. "raydium,hard-reset-delay-ms", &temp_val);
  1486. if (!rc)
  1487. pdata->hard_rst_dly = temp_val;
  1488. else
  1489. return rc;
  1490. rc = of_property_read_u32(np,
  1491. "raydium,soft-reset-delay-ms", &temp_val);
  1492. if (!rc)
  1493. pdata->soft_rst_dly = temp_val;
  1494. else
  1495. return rc;
  1496. rc = of_property_read_u32(np, "raydium,num-max-touches", &temp_val);
  1497. if (!rc)
  1498. pdata->num_max_touches = temp_val;
  1499. else
  1500. return rc;
  1501. #ifdef FW_MAPPING_BYID_EN
  1502. rc = of_property_read_u32(np, "raydium,fw_id", &temp_val);
  1503. if (!rc)
  1504. pdata->fw_id = temp_val;
  1505. else
  1506. return rc;
  1507. #endif
  1508. return 0;
  1509. }
  1510. #else
  1511. static int raydium_parse_dt(struct device *dev,
  1512. struct raydium_ts_platform_data *pdata)
  1513. {
  1514. return -ENODEV;
  1515. }
  1516. #endif /*end of CONFIG_OF*/
  1517. static void raydium_input_set(struct input_dev *input_dev)
  1518. {
  1519. int ret = 0;
  1520. unsigned char i;
  1521. input_dev->name = "raydium_ts";/*name need same with .idc*/
  1522. input_dev->id.bustype = BUS_I2C;
  1523. input_dev->dev.parent = &g_raydium_ts->client->dev;
  1524. input_dev->open = raydium_ts_open;/*touch lock*/
  1525. input_dev->close = raydium_ts_close;
  1526. input_set_drvdata(input_dev, g_raydium_ts);
  1527. __set_bit(EV_KEY, input_dev->evbit);
  1528. __set_bit(EV_ABS, input_dev->evbit);
  1529. __set_bit(BTN_TOUCH, input_dev->keybit);
  1530. __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
  1531. LOGD(LOG_INFO, "[touch]set abs prarams x[%d], y[%d]\n",
  1532. g_raydium_ts->x_max, g_raydium_ts->y_max);
  1533. /* Multitouch input params setup */
  1534. input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
  1535. g_raydium_ts->x_max, 0, 0);
  1536. input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
  1537. g_raydium_ts->y_max, 0, 0);
  1538. input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, PRESS_MAX, 0, 0);
  1539. input_set_abs_params(input_dev,
  1540. ABS_MT_TOUCH_MAJOR, 0, WIDTH_MAX, 0, 0);
  1541. input_set_abs_params(input_dev,
  1542. ABS_MT_TOUCH_MINOR, 0, WIDTH_MAX, 0, 0);
  1543. ret = input_mt_init_slots(input_dev, MAX_TOUCH_NUM,
  1544. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  1545. if (ret)
  1546. LOGD(LOG_ERR, "[touch]failed to initialize MT slots: %d\n", ret);
  1547. for (i = 0; i < (MAX_TOUCH_NUM * 2); i++)
  1548. gst_slot[i] = gst_slot_init;
  1549. }
  1550. static int raydium_set_resolution(void)
  1551. {
  1552. unsigned char u8_buf[4];
  1553. int i32_ret = -1;
  1554. unsigned int u32_x, u32_y;
  1555. mutex_lock(&g_raydium_ts->lock);
  1556. i32_ret = raydium_i2c_pda2_set_page(g_raydium_ts->client,
  1557. g_raydium_ts->is_suspend,
  1558. RAYDIUM_PDA2_PAGE_0);
  1559. if (i32_ret < 0)
  1560. goto exit_error;
  1561. i32_ret = raydium_i2c_pda2_read(g_raydium_ts->client,
  1562. RAYDIUM_PDA2_DISPLAY_INFO_ADDR,
  1563. u8_buf, 4);
  1564. if (i32_ret < 0)
  1565. goto exit_error;
  1566. u32_x = u8_buf[3] << 8 | u8_buf[2];
  1567. u32_y = u8_buf[1] << 8 | u8_buf[0];
  1568. LOGD(LOG_INFO, "[touch]RAD display info x:%d, y:%d\n", u32_x, u32_y);
  1569. if (u32_x > 100 && u32_y > 100 &&
  1570. u32_x < 600 && u32_y < 600) {
  1571. g_raydium_ts->x_max = u32_x - 1;
  1572. g_raydium_ts->y_max = u32_y - 1;
  1573. }
  1574. exit_error:
  1575. mutex_unlock(&g_raydium_ts->lock);
  1576. return i32_ret;
  1577. }
  1578. static int raydium_ts_probe(struct i2c_client *client,
  1579. const struct i2c_device_id *id)
  1580. {
  1581. struct raydium_ts_platform_data *pdata =
  1582. (struct raydium_ts_platform_data *)client->dev.platform_data;
  1583. struct input_dev *input_dev;
  1584. unsigned short u16_i2c_data;
  1585. int ret = 0;
  1586. LOGD(LOG_INFO, "[touch] probe\n");
  1587. if (client->dev.of_node) {
  1588. pdata = devm_kzalloc(&client->dev,
  1589. sizeof(struct raydium_ts_platform_data),
  1590. GFP_KERNEL);
  1591. if (!pdata) {
  1592. LOGD(LOG_ERR, "[touch]failed to allocate memory\n");
  1593. return -ENOMEM;
  1594. }
  1595. ret = raydium_parse_dt(&client->dev, pdata);
  1596. if (ret) {
  1597. LOGD(LOG_ERR, "[touch]device tree parsing failed\n");
  1598. goto parse_dt_failed;
  1599. }
  1600. } else
  1601. pdata = client->dev.platform_data;
  1602. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  1603. ret = -ENODEV;
  1604. goto exit_check_functionality_failed;
  1605. }
  1606. g_raydium_ts = devm_kzalloc(&client->dev,
  1607. sizeof(struct raydium_ts_data),
  1608. GFP_KERNEL);
  1609. if (!g_raydium_ts) {
  1610. LOGD(LOG_ERR, "[touch]failed to allocate input driver data\n");
  1611. return -ENOMEM;
  1612. }
  1613. raydium_variable_init();
  1614. mutex_init(&g_raydium_ts->lock);
  1615. i2c_set_clientdata(client, g_raydium_ts);
  1616. g_raydium_ts->irq_enabled = false;
  1617. g_raydium_ts->irq_wake = false;
  1618. g_raydium_ts->irq_gpio = pdata->irq_gpio;
  1619. g_raydium_ts->rst_gpio = pdata->reset_gpio;
  1620. client->irq = g_raydium_ts->irq_gpio;
  1621. g_raydium_ts->u8_max_touchs = pdata->num_max_touches;
  1622. g_raydium_ts->client = client;
  1623. g_raydium_ts->x_max = pdata->x_max - 1;
  1624. g_raydium_ts->y_max = pdata->y_max - 1;
  1625. g_raydium_ts->is_suspend = 0;
  1626. g_raydium_ts->is_sleep = 0;
  1627. #ifdef GESTURE_EN
  1628. g_raydium_ts->is_palm = 0;
  1629. #endif
  1630. g_raydium_ts->fw_version = 0;
  1631. device_init_wakeup(&client->dev, 1);
  1632. #ifdef MSM_NEW_VER
  1633. ret = raydium_ts_pinctrl_init();
  1634. if (!ret && g_raydium_ts->ts_pinctrl) {
  1635. /*
  1636. * Pinctrl handle is optional. If pinctrl handle is found
  1637. * let pins to be configured in active state. If not
  1638. * found continue further without error.
  1639. */
  1640. ret = pinctrl_select_state(g_raydium_ts->ts_pinctrl,
  1641. g_raydium_ts->pinctrl_state_active);
  1642. if (ret < 0)
  1643. LOGD(LOG_ERR, "[touch]failed to set pin to active state\n");
  1644. }
  1645. #endif /*end of MSM_NEW_VER*/
  1646. ret = raydium_gpio_configure(true);
  1647. if (ret < 0) {
  1648. LOGD(LOG_ERR, "[touch]failed to configure the gpios\n");
  1649. goto err_gpio_req;
  1650. }
  1651. /*modify dtsi to 360*/
  1652. msleep(pdata->soft_rst_dly);
  1653. if (raydium_disable_i2c_deglitch() == ERROR) {
  1654. LOGD(LOG_ERR, "[touch]disable i2c deglicth NG!\r\n");
  1655. ret = -ENODEV;
  1656. goto exit_check_i2c;
  1657. }
  1658. /*print touch i2c ready*/
  1659. ret = raydium_check_i2c_ready(&u16_i2c_data);
  1660. if (ret < 0) {
  1661. LOGD(LOG_ERR, "[touch]Check I2C failed\n");
  1662. ret = -ENODEV;
  1663. goto exit_check_i2c;
  1664. }
  1665. /*input device initialization*/
  1666. input_dev = input_allocate_device();
  1667. if (!input_dev) {
  1668. ret = -ENOMEM;
  1669. LOGD(LOG_ERR, "[touch]failed to allocate input device\n");
  1670. goto exit_input_dev_alloc_failed;
  1671. }
  1672. raydium_set_resolution();
  1673. g_raydium_ts->input_dev = input_dev;
  1674. raydium_input_set(input_dev);
  1675. ret = input_register_device(input_dev);
  1676. if (ret) {
  1677. LOGD(LOG_ERR, "[touch]failed to register input device: %s\n",
  1678. dev_name(&client->dev));
  1679. goto exit_input_register_device_failed;
  1680. }
  1681. #ifdef GESTURE_EN
  1682. input_set_capability(input_dev, EV_KEY, KEY_SLEEP);
  1683. input_set_capability(input_dev, EV_KEY, KEY_POWER);
  1684. #endif
  1685. /*suspend/resume routine*/
  1686. #if defined(CONFIG_FB)
  1687. raydium_register_notifier();
  1688. #elif defined(CONFIG_HAS_EARLYSUSPEND)
  1689. /*Early-suspend level*/
  1690. g_raydium_ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  1691. g_raydium_ts->early_suspend.suspend = raydium_ts_early_suspend;
  1692. g_raydium_ts->early_suspend.resume = raydium_ts_late_resume;
  1693. register_early_suspend(&g_raydium_ts->early_suspend);
  1694. #endif/*end of CONFIG_FB*/
  1695. #ifdef CONFIG_RM_SYSFS_DEBUG
  1696. raydium_create_sysfs(client);
  1697. #endif/*end of CONFIG_RM_SYSFS_DEBUG*/
  1698. INIT_WORK(&g_raydium_ts->work, raydium_work_handler);
  1699. g_raydium_ts->workqueue = create_singlethread_workqueue("raydium_ts");
  1700. /*irq_gpio = 13 irqflags = 108*/
  1701. LOGD(LOG_INFO, "[touch]pdata irq : %d\n", g_raydium_ts->irq_gpio);
  1702. LOGD(LOG_INFO, "[touch]client irq : %d, pdata flags : %d\n",
  1703. client->irq, pdata->irqflags);
  1704. g_raydium_ts->irq = gpio_to_irq(pdata->irq_gpio);
  1705. ret = request_threaded_irq(g_raydium_ts->irq, NULL, raydium_ts_interrupt,
  1706. IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_NO_SUSPEND,
  1707. client->dev.driver->name, g_raydium_ts);
  1708. if (ret < 0) {
  1709. LOGD(LOG_ERR, "[touch]raydium_probe: request irq failed\n");
  1710. goto exit_irq_request_failed;
  1711. }
  1712. g_raydium_ts->irq_desc = irq_to_desc(g_raydium_ts->irq);
  1713. g_raydium_ts->irq_enabled = true;
  1714. /*disable_irq then enable_irq for avoid Unbalanced enable for IRQ */
  1715. /*raydium_irq_control(ts, ENABLE);*/
  1716. LOGD(LOG_INFO, "[touch]RAD Touch driver ver :0x%X\n", g_u32_driver_version);
  1717. /*fw update check*/
  1718. ret = raydium_fw_update_check(u16_i2c_data);
  1719. if (ret < 0) {
  1720. LOGD(LOG_ERR, "[touch]FW update check failed\n");
  1721. ret = -ENODEV;
  1722. goto exit_irq_request_failed;
  1723. }
  1724. return 0;
  1725. exit_irq_request_failed:
  1726. #if defined(CONFIG_FB)
  1727. raydium_unregister_notifier();
  1728. #endif/*end of CONFIG_FB*/
  1729. cancel_work_sync(&g_raydium_ts->work);
  1730. input_unregister_device(input_dev);
  1731. exit_input_register_device_failed:
  1732. input_free_device(input_dev);
  1733. exit_input_dev_alloc_failed:
  1734. exit_check_i2c:
  1735. if (gpio_is_valid(pdata->reset_gpio))
  1736. gpio_free(pdata->reset_gpio);
  1737. if (gpio_is_valid(pdata->irq_gpio))
  1738. gpio_free(pdata->irq_gpio);
  1739. err_gpio_req:
  1740. #ifdef MSM_NEW_VER
  1741. if (g_raydium_ts->ts_pinctrl) {
  1742. if (IS_ERR_OR_NULL(g_raydium_ts->pinctrl_state_release)) {
  1743. devm_pinctrl_put(g_raydium_ts->ts_pinctrl);
  1744. g_raydium_ts->ts_pinctrl = NULL;
  1745. } else {
  1746. ret = pinctrl_select_state(g_raydium_ts->ts_pinctrl,
  1747. g_raydium_ts->pinctrl_state_release);
  1748. if (ret)
  1749. LOGD(LOG_ERR, "[touch]pinctrl_select_state failed\n");
  1750. }
  1751. }
  1752. #endif/*end of MSM_NEW_VER*/
  1753. parse_dt_failed:
  1754. exit_check_functionality_failed:
  1755. return ret;
  1756. }
  1757. static int raydium_ts_remove(struct i2c_client *client)
  1758. {
  1759. #if defined(CONFIG_FB)
  1760. raydium_unregister_notifier();
  1761. #elif defined(CONFIG_HAS_EARLYSUSPEND)
  1762. unregister_early_suspend(&g_raydium_ts->early_suspend);
  1763. #endif/*end of CONFIG_FB*/
  1764. input_unregister_device(g_raydium_ts->input_dev);
  1765. input_free_device(g_raydium_ts->input_dev);
  1766. gpio_free(g_raydium_ts->rst_gpio);
  1767. #ifdef CONFIG_RM_SYSFS_DEBUG
  1768. raydium_release_sysfs(client);
  1769. #endif /*end of CONFIG_RM_SYSFS_DEBUG*/
  1770. free_irq(client->irq, g_raydium_ts);
  1771. if (gpio_is_valid(g_raydium_ts->rst_gpio))
  1772. gpio_free(g_raydium_ts->rst_gpio);
  1773. if (gpio_is_valid(g_raydium_ts->irq_gpio))
  1774. gpio_free(g_raydium_ts->irq_gpio);
  1775. cancel_work_sync(&g_raydium_ts->work);
  1776. destroy_workqueue(g_raydium_ts->workqueue);
  1777. //kfree(g_raydium_ts);
  1778. i2c_set_clientdata(client, NULL);
  1779. return 0;
  1780. }
  1781. static const struct i2c_device_id raydium_ts_id[] = {
  1782. {RAYDIUM_NAME, 0},
  1783. {}
  1784. };
  1785. MODULE_DEVICE_TABLE(i2c, raydium_ts_id);
  1786. #ifdef CONFIG_OF
  1787. static const struct of_device_id raydium_match_table[] = {
  1788. { .compatible = "raydium,raydium-ts",},
  1789. { },
  1790. };
  1791. #else
  1792. #define raydium_match_table NULL
  1793. #endif/*end of CONFIG_OF*/
  1794. static struct i2c_driver raydium_ts_driver = {
  1795. .probe = raydium_ts_probe,
  1796. .remove = raydium_ts_remove,
  1797. .id_table = raydium_ts_id,
  1798. .driver = {
  1799. .name = RAYDIUM_NAME,
  1800. .owner = THIS_MODULE,
  1801. .of_match_table = raydium_match_table,
  1802. #if defined(CONFIG_PM)
  1803. .pm = &raydium_ts_pm_ops,
  1804. #endif/*end of CONFIG_PM*/
  1805. },
  1806. };
  1807. static int __init raydium_ts_init(void)
  1808. {
  1809. int ret;
  1810. ret = i2c_add_driver(&raydium_ts_driver);
  1811. return ret;
  1812. }
  1813. static void __exit raydium_ts_exit(void)
  1814. {
  1815. i2c_del_driver(&raydium_ts_driver);
  1816. }
  1817. module_init(raydium_ts_init);
  1818. module_exit(raydium_ts_exit);
  1819. MODULE_AUTHOR("<Rejion>");
  1820. MODULE_DESCRIPTION("Raydium TouchScreen driver");
  1821. MODULE_LICENSE("GPL");