goodix_ts_core.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263
  1. /*
  2. * Goodix Touchscreen Driver
  3. * Copyright (C) 2020 - 2021 Goodix, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be a reference
  11. * to you, when you are integrating the GOODiX's CTP IC into your system,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. */
  17. #include <linux/version.h>
  18. #include <linux/fs.h>
  19. #include <linux/proc_fs.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/uaccess.h>
  22. #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
  23. #include <linux/input/mt.h>
  24. #define INPUT_TYPE_B_PROTOCOL
  25. #endif
  26. #include "goodix_ts_core.h"
  27. #define GOODIX_DEFAULT_CFG_NAME "goodix_cfg_group.cfg"
  28. #define GOOIDX_INPUT_PHYS "goodix_ts/input0"
  29. struct goodix_module goodix_modules;
  30. int core_module_prob_sate = CORE_MODULE_UNPROBED;
  31. static int goodix_send_ic_config(struct goodix_ts_core *cd, int type);
  32. /**
  33. * __do_register_ext_module - register external module
  34. * to register into touch core modules structure
  35. * return 0 on success, otherwise return < 0
  36. */
  37. static int __do_register_ext_module(struct goodix_ext_module *module)
  38. {
  39. struct goodix_ext_module *ext_module, *next;
  40. struct list_head *insert_point = &goodix_modules.head;
  41. /* prority level *must* be set */
  42. if (module->priority == EXTMOD_PRIO_RESERVED) {
  43. ts_err("Priority of module [%s] needs to be set",
  44. module->name);
  45. return -EINVAL;
  46. }
  47. mutex_lock(&goodix_modules.mutex);
  48. /* find insert point for the specified priority */
  49. if (!list_empty(&goodix_modules.head)) {
  50. list_for_each_entry_safe(ext_module, next,
  51. &goodix_modules.head, list) {
  52. if (ext_module == module) {
  53. ts_info("Module [%s] already exists",
  54. module->name);
  55. mutex_unlock(&goodix_modules.mutex);
  56. return 0;
  57. }
  58. }
  59. /* smaller priority value with higher priority level */
  60. list_for_each_entry_safe(ext_module, next,
  61. &goodix_modules.head, list) {
  62. if (ext_module->priority >= module->priority) {
  63. insert_point = &ext_module->list;
  64. break;
  65. }
  66. }
  67. }
  68. if (module->funcs && module->funcs->init) {
  69. if (module->funcs->init(goodix_modules.core_data,
  70. module) < 0) {
  71. ts_err("Module [%s] init error",
  72. module->name ? module->name : " ");
  73. mutex_unlock(&goodix_modules.mutex);
  74. return -EFAULT;
  75. }
  76. }
  77. list_add(&module->list, insert_point->prev);
  78. mutex_unlock(&goodix_modules.mutex);
  79. ts_info("Module [%s] registered,priority:%u", module->name,
  80. module->priority);
  81. return 0;
  82. }
  83. static void goodix_register_ext_module_work(struct work_struct *work) {
  84. struct goodix_ext_module *module =
  85. container_of(work, struct goodix_ext_module, work);
  86. ts_info("module register work IN");
  87. /* driver probe failed */
  88. if (core_module_prob_sate != CORE_MODULE_PROB_SUCCESS) {
  89. ts_err("Can't register ext_module core error");
  90. return;
  91. }
  92. if (__do_register_ext_module(module))
  93. ts_err("failed register module: %s", module->name);
  94. else
  95. ts_info("success register module: %s", module->name);
  96. }
  97. static void goodix_core_module_init(void)
  98. {
  99. if (goodix_modules.initilized)
  100. return;
  101. goodix_modules.initilized = true;
  102. INIT_LIST_HEAD(&goodix_modules.head);
  103. mutex_init(&goodix_modules.mutex);
  104. }
  105. /**
  106. * goodix_register_ext_module - interface for register external module
  107. * to the core. This will create a workqueue to finish the real register
  108. * work and return immediately. The user need to check the final result
  109. * to make sure registe is success or fail.
  110. *
  111. * @module: pointer to external module to be register
  112. * return: 0 ok, <0 failed
  113. */
  114. int goodix_register_ext_module(struct goodix_ext_module *module)
  115. {
  116. if (!module)
  117. return -EINVAL;
  118. ts_info("goodix_register_ext_module IN");
  119. goodix_core_module_init();
  120. INIT_WORK(&module->work, goodix_register_ext_module_work);
  121. schedule_work(&module->work);
  122. ts_info("goodix_register_ext_module OUT");
  123. return 0;
  124. }
  125. /**
  126. * goodix_register_ext_module_no_wait
  127. * return: 0 ok, <0 failed
  128. */
  129. int goodix_register_ext_module_no_wait(struct goodix_ext_module *module)
  130. {
  131. if (!module)
  132. return -EINVAL;
  133. ts_info("goodix_register_ext_module_no_wait IN");
  134. goodix_core_module_init();
  135. /* driver probe failed */
  136. if (core_module_prob_sate != CORE_MODULE_PROB_SUCCESS) {
  137. ts_err("Can't register ext_module core error");
  138. return -EINVAL;
  139. }
  140. return __do_register_ext_module(module);
  141. }
  142. /**
  143. * goodix_unregister_ext_module - interface for external module
  144. * to unregister external modules
  145. *
  146. * @module: pointer to external module
  147. * return: 0 ok, <0 failed
  148. */
  149. int goodix_unregister_ext_module(struct goodix_ext_module *module)
  150. {
  151. struct goodix_ext_module *ext_module, *next;
  152. bool found = false;
  153. if (!module)
  154. return -EINVAL;
  155. if (!goodix_modules.initilized)
  156. return -EINVAL;
  157. if (!goodix_modules.core_data)
  158. return -ENODEV;
  159. mutex_lock(&goodix_modules.mutex);
  160. if (!list_empty(&goodix_modules.head)) {
  161. list_for_each_entry_safe(ext_module, next,
  162. &goodix_modules.head, list) {
  163. if (ext_module == module) {
  164. found = true;
  165. break;
  166. }
  167. }
  168. } else {
  169. mutex_unlock(&goodix_modules.mutex);
  170. return 0;
  171. }
  172. if (!found) {
  173. ts_debug("Module [%s] never registed",
  174. module->name);
  175. mutex_unlock(&goodix_modules.mutex);
  176. return 0;
  177. }
  178. list_del(&module->list);
  179. mutex_unlock(&goodix_modules.mutex);
  180. if (module->funcs && module->funcs->exit)
  181. module->funcs->exit(goodix_modules.core_data, module);
  182. ts_info("Moudle [%s] unregistered",
  183. module->name ? module->name : " ");
  184. return 0;
  185. }
  186. static void goodix_ext_sysfs_release(struct kobject *kobj)
  187. {
  188. ts_info("Kobject released!");
  189. }
  190. #define to_ext_module(kobj) container_of(kobj,\
  191. struct goodix_ext_module, kobj)
  192. #define to_ext_attr(attr) container_of(attr,\
  193. struct goodix_ext_attribute, attr)
  194. static ssize_t goodix_ext_sysfs_show(struct kobject *kobj,
  195. struct attribute *attr, char *buf)
  196. {
  197. struct goodix_ext_module *module = to_ext_module(kobj);
  198. struct goodix_ext_attribute *ext_attr = to_ext_attr(attr);
  199. if (ext_attr->show)
  200. return ext_attr->show(module, buf);
  201. return -EIO;
  202. }
  203. static ssize_t goodix_ext_sysfs_store(struct kobject *kobj,
  204. struct attribute *attr, const char *buf, size_t count)
  205. {
  206. struct goodix_ext_module *module = to_ext_module(kobj);
  207. struct goodix_ext_attribute *ext_attr = to_ext_attr(attr);
  208. if (ext_attr->store)
  209. return ext_attr->store(module, buf, count);
  210. return -EIO;
  211. }
  212. static const struct sysfs_ops goodix_ext_ops = {
  213. .show = goodix_ext_sysfs_show,
  214. .store = goodix_ext_sysfs_store
  215. };
  216. static struct kobj_type goodix_ext_ktype = {
  217. .release = goodix_ext_sysfs_release,
  218. .sysfs_ops = &goodix_ext_ops,
  219. };
  220. struct kobj_type *goodix_get_default_ktype(void)
  221. {
  222. return &goodix_ext_ktype;
  223. }
  224. struct kobject *goodix_get_default_kobj(void)
  225. {
  226. struct kobject *kobj = NULL;
  227. if (goodix_modules.core_data &&
  228. goodix_modules.core_data->pdev)
  229. kobj = &goodix_modules.core_data->pdev->dev.kobj;
  230. return kobj;
  231. }
  232. /* show driver infomation */
  233. static ssize_t goodix_ts_driver_info_show(struct device *dev,
  234. struct device_attribute *attr, char *buf)
  235. {
  236. return snprintf(buf, PAGE_SIZE, "DriverVersion:%s\n",
  237. GOODIX_DRIVER_VERSION);
  238. }
  239. /* show chip infoamtion */
  240. static ssize_t goodix_ts_chip_info_show(struct device *dev,
  241. struct device_attribute *attr, char *buf)
  242. {
  243. struct goodix_ts_core *core_data = dev_get_drvdata(dev);
  244. struct goodix_ts_hw_ops *hw_ops = core_data->hw_ops;
  245. struct goodix_fw_version chip_ver;
  246. u8 temp_pid[8] = {0};
  247. int ret;
  248. int cnt = -EINVAL;
  249. if (hw_ops->read_version) {
  250. ret = hw_ops->read_version(core_data, &chip_ver);
  251. if (!ret) {
  252. memcpy(temp_pid, chip_ver.rom_pid, sizeof(chip_ver.rom_pid));
  253. cnt = snprintf(&buf[0], PAGE_SIZE,
  254. "rom_pid:%s\nrom_vid:%02x%02x%02x\n",
  255. temp_pid, chip_ver.rom_vid[0],
  256. chip_ver.rom_vid[1], chip_ver.rom_vid[2]);
  257. cnt += snprintf(&buf[cnt], PAGE_SIZE,
  258. "patch_pid:%s\npatch_vid:%02x%02x%02x%02x\n",
  259. chip_ver.patch_pid, chip_ver.patch_vid[0],
  260. chip_ver.patch_vid[1], chip_ver.patch_vid[2],
  261. chip_ver.patch_vid[3]);
  262. cnt += snprintf(&buf[cnt], PAGE_SIZE,
  263. "sensorid:%d\n", chip_ver.sensor_id);
  264. }
  265. }
  266. if (hw_ops->get_ic_info) {
  267. ret = hw_ops->get_ic_info(core_data, &core_data->ic_info);
  268. if (!ret) {
  269. cnt += snprintf(&buf[cnt], PAGE_SIZE,
  270. "config_id:%x\n", core_data->ic_info.version.config_id);
  271. cnt += snprintf(&buf[cnt], PAGE_SIZE,
  272. "config_version:%x\n", core_data->ic_info.version.config_version);
  273. }
  274. }
  275. return cnt;
  276. }
  277. /* reset chip */
  278. static ssize_t goodix_ts_reset_store(struct device *dev,
  279. struct device_attribute *attr,
  280. const char *buf,
  281. size_t count)
  282. {
  283. struct goodix_ts_core *core_data = dev_get_drvdata(dev);
  284. struct goodix_ts_hw_ops *hw_ops = core_data->hw_ops;
  285. if (!buf || count <= 0)
  286. return -EINVAL;
  287. if (buf[0] != '0')
  288. hw_ops->reset(core_data, GOODIX_NORMAL_RESET_DELAY_MS);
  289. return count;
  290. }
  291. /* read config */
  292. static ssize_t goodix_ts_read_cfg_show(struct device *dev,
  293. struct device_attribute *attr,
  294. char *buf)
  295. {
  296. struct goodix_ts_core *core_data = dev_get_drvdata(dev);
  297. struct goodix_ts_hw_ops *hw_ops = core_data->hw_ops;
  298. int ret;
  299. int i;
  300. int offset;
  301. char *cfg_buf = NULL;
  302. cfg_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  303. if (!cfg_buf)
  304. return -ENOMEM;
  305. if (hw_ops->read_config)
  306. ret = hw_ops->read_config(core_data, cfg_buf, PAGE_SIZE);
  307. else
  308. ret = -EINVAL;
  309. if (ret > 0) {
  310. offset = 0;
  311. for (i = 0; i < 200; i++) { // only print 200 bytes
  312. offset += snprintf(&buf[offset], PAGE_SIZE - offset,
  313. "%02x,", cfg_buf[i]);
  314. if ((i + 1) % 20 == 0)
  315. buf[offset++] = '\n';
  316. }
  317. }
  318. kfree(cfg_buf);
  319. if (ret <= 0)
  320. return ret;
  321. return offset;
  322. }
  323. static u8 ascii2hex(u8 a)
  324. {
  325. s8 value = 0;
  326. if (a >= '0' && a <= '9')
  327. value = a - '0';
  328. else if (a >= 'A' && a <= 'F')
  329. value = a - 'A' + 0x0A;
  330. else if (a >= 'a' && a <= 'f')
  331. value = a - 'a' + 0x0A;
  332. else
  333. value = 0xff;
  334. return value;
  335. }
  336. static int goodix_ts_convert_0x_data(const u8 *buf, int buf_size,
  337. u8 *out_buf, int *out_buf_len)
  338. {
  339. int i, m_size = 0;
  340. int temp_index = 0;
  341. u8 high, low;
  342. for (i = 0; i < buf_size; i++) {
  343. if (buf[i] == 'x' || buf[i] == 'X')
  344. m_size++;
  345. }
  346. if (m_size <= 1) {
  347. ts_err("cfg file ERROR, valid data count:%d", m_size);
  348. return -EINVAL;
  349. }
  350. *out_buf_len = m_size;
  351. for (i = 0; i < buf_size; i++) {
  352. if (buf[i] != 'x' && buf[i] != 'X')
  353. continue;
  354. if (temp_index >= m_size) {
  355. ts_err("exchange cfg data error, overflow,"
  356. "temp_index:%d,m_size:%d",
  357. temp_index, m_size);
  358. return -EINVAL;
  359. }
  360. high = ascii2hex(buf[i + 1]);
  361. low = ascii2hex(buf[i + 2]);
  362. if (high == 0xff || low == 0xff) {
  363. ts_err("failed convert: 0x%x, 0x%x",
  364. buf[i + 1], buf[i + 2]);
  365. return -EINVAL;
  366. }
  367. out_buf[temp_index++] = (high << 4) + low;
  368. }
  369. return 0;
  370. }
  371. /* send config */
  372. static ssize_t goodix_ts_send_cfg_store(struct device *dev,
  373. struct device_attribute *attr,
  374. const char *buf, size_t count)
  375. {
  376. struct goodix_ts_core *core_data = dev_get_drvdata(dev);
  377. struct goodix_ts_hw_ops *hw_ops = core_data->hw_ops;
  378. struct goodix_ic_config *config = NULL;
  379. const struct firmware *cfg_img = NULL;
  380. int en;
  381. int ret;
  382. if (sscanf(buf, "%d", &en) != 1)
  383. return -EINVAL;
  384. if (en != 1)
  385. return -EINVAL;
  386. hw_ops->irq_enable(core_data, false);
  387. ret = request_firmware(&cfg_img, GOODIX_DEFAULT_CFG_NAME, dev);
  388. if (ret < 0) {
  389. ts_err("cfg file [%s] not available,errno:%d",
  390. GOODIX_DEFAULT_CFG_NAME, ret);
  391. goto exit;
  392. } else {
  393. ts_info("cfg file [%s] is ready", GOODIX_DEFAULT_CFG_NAME);
  394. }
  395. config = kzalloc(sizeof(*config), GFP_KERNEL);
  396. if (!config)
  397. goto exit;
  398. if (goodix_ts_convert_0x_data(cfg_img->data, cfg_img->size,
  399. config->data, &config->len)) {
  400. ts_err("convert config data FAILED");
  401. goto exit;
  402. }
  403. if (hw_ops->send_config) {
  404. ret = hw_ops->send_config(core_data, config->data, config->len);
  405. if (ret < 0)
  406. ts_err("send config failed");
  407. }
  408. exit:
  409. hw_ops->irq_enable(core_data, true);
  410. kfree(config);
  411. if (cfg_img)
  412. release_firmware(cfg_img);
  413. return count;
  414. }
  415. /* reg read/write */
  416. static u32 rw_addr;
  417. static u32 rw_len;
  418. static u8 rw_flag;
  419. static u8 store_buf[32];
  420. static u8 show_buf[PAGE_SIZE];
  421. static ssize_t goodix_ts_reg_rw_show(struct device *dev,
  422. struct device_attribute *attr, char *buf)
  423. {
  424. struct goodix_ts_core *core_data = dev_get_drvdata(dev);
  425. struct goodix_ts_hw_ops *hw_ops = core_data->hw_ops;
  426. int ret;
  427. if (!rw_addr || !rw_len) {
  428. ts_err("address(0x%x) and length(%d) can't be null",
  429. rw_addr, rw_len);
  430. return -EINVAL;
  431. }
  432. if (rw_flag != 1) {
  433. ts_err("invalid rw flag %d, only support [1/2]", rw_flag);
  434. return -EINVAL;
  435. }
  436. ret = hw_ops->read(core_data, rw_addr, show_buf, rw_len);
  437. if (ret < 0) {
  438. ts_err("failed read addr(%x) length(%d)", rw_addr, rw_len);
  439. return snprintf(buf, PAGE_SIZE, "failed read addr(%x), len(%d)\n",
  440. rw_addr, rw_len);
  441. }
  442. return snprintf(buf, PAGE_SIZE, "0x%x,%d {%*ph}\n",
  443. rw_addr, rw_len, rw_len, show_buf);
  444. }
  445. static ssize_t goodix_ts_reg_rw_store(struct device *dev,
  446. struct device_attribute *attr,
  447. const char *buf, size_t count)
  448. {
  449. struct goodix_ts_core *core_data = dev_get_drvdata(dev);
  450. struct goodix_ts_hw_ops *hw_ops = core_data->hw_ops;
  451. char *pos = NULL;
  452. char *token = NULL;
  453. long result = 0;
  454. int ret;
  455. int i;
  456. if (!buf || !count) {
  457. ts_err("invalid parame");
  458. goto err_out;
  459. }
  460. if (buf[0] == 'r') {
  461. rw_flag = 1;
  462. } else if (buf[0] == 'w') {
  463. rw_flag = 2;
  464. } else {
  465. ts_err("string must start with 'r/w'");
  466. goto err_out;
  467. }
  468. /* get addr */
  469. pos = (char *)buf;
  470. pos += 2;
  471. token = strsep(&pos, ":");
  472. if (!token) {
  473. ts_err("invalid address info");
  474. goto err_out;
  475. } else {
  476. if (kstrtol(token, 16, &result)) {
  477. ts_err("failed get addr info");
  478. goto err_out;
  479. }
  480. rw_addr = (u32)result;
  481. ts_info("rw addr is 0x%x", rw_addr);
  482. }
  483. /* get length */
  484. token = strsep(&pos, ":");
  485. if (!token) {
  486. ts_err("invalid length info");
  487. goto err_out;
  488. } else {
  489. if (kstrtol(token, 0, &result)) {
  490. ts_err("failed get length info");
  491. goto err_out;
  492. }
  493. rw_len = (u32)result;
  494. ts_info("rw length info is %d", rw_len);
  495. if (rw_len > sizeof(store_buf)) {
  496. ts_err("data len > %lu", sizeof(store_buf));
  497. goto err_out;
  498. }
  499. }
  500. if (rw_flag == 1)
  501. return count;
  502. for (i = 0; i < rw_len; i++) {
  503. token = strsep(&pos, ":");
  504. if (!token) {
  505. ts_err("invalid data info");
  506. goto err_out;
  507. } else {
  508. if (kstrtol(token, 16, &result)) {
  509. ts_err("failed get data[%d] info", i);
  510. goto err_out;
  511. }
  512. store_buf[i] = (u8)result;
  513. ts_info("get data[%d]=0x%x", i, store_buf[i]);
  514. }
  515. }
  516. ret = hw_ops->write(core_data, rw_addr, store_buf, rw_len);
  517. if (ret < 0) {
  518. ts_err("failed write addr(%x) data %*ph", rw_addr,
  519. rw_len, store_buf);
  520. goto err_out;
  521. }
  522. ts_info("%s write to addr (%x) with data %*ph",
  523. "success", rw_addr, rw_len, store_buf);
  524. return count;
  525. err_out:
  526. snprintf(show_buf, PAGE_SIZE, "%s\n",
  527. "invalid params, format{r/w:4100:length:[41:21:31]}");
  528. return -EINVAL;
  529. }
  530. /* show irq infomation */
  531. static ssize_t goodix_ts_irq_info_show(struct device *dev,
  532. struct device_attribute *attr,
  533. char *buf)
  534. {
  535. struct goodix_ts_core *core_data = dev_get_drvdata(dev);
  536. struct irq_desc *desc;
  537. size_t offset = 0;
  538. int r;
  539. r = snprintf(&buf[offset], PAGE_SIZE, "irq:%u\n", core_data->irq);
  540. if (r < 0)
  541. return -EINVAL;
  542. offset += r;
  543. r = snprintf(&buf[offset], PAGE_SIZE - offset, "state:%s\n",
  544. atomic_read(&core_data->irq_enabled) ?
  545. "enabled" : "disabled");
  546. if (r < 0)
  547. return -EINVAL;
  548. desc = irq_to_desc(core_data->irq);
  549. offset += r;
  550. r = snprintf(&buf[offset], PAGE_SIZE - offset, "disable-depth:%d\n",
  551. desc->depth);
  552. if (r < 0)
  553. return -EINVAL;
  554. offset += r;
  555. r = snprintf(&buf[offset], PAGE_SIZE - offset, "trigger-count:%zu\n",
  556. core_data->irq_trig_cnt);
  557. if (r < 0)
  558. return -EINVAL;
  559. offset += r;
  560. r = snprintf(&buf[offset], PAGE_SIZE - offset,
  561. "echo 0/1 > irq_info to disable/enable irq\n");
  562. if (r < 0)
  563. return -EINVAL;
  564. offset += r;
  565. return offset;
  566. }
  567. /* enable/disable irq */
  568. static ssize_t goodix_ts_irq_info_store(struct device *dev,
  569. struct device_attribute *attr,
  570. const char *buf, size_t count)
  571. {
  572. struct goodix_ts_core *core_data = dev_get_drvdata(dev);
  573. struct goodix_ts_hw_ops *hw_ops = core_data->hw_ops;
  574. if (!buf || count <= 0)
  575. return -EINVAL;
  576. if (buf[0] != '0')
  577. hw_ops->irq_enable(core_data, true);
  578. else
  579. hw_ops->irq_enable(core_data, false);
  580. return count;
  581. }
  582. /* show esd status */
  583. static ssize_t goodix_ts_esd_info_show(struct device *dev,
  584. struct device_attribute *attr,
  585. char *buf)
  586. {
  587. struct goodix_ts_core *core_data = dev_get_drvdata(dev);
  588. struct goodix_ts_esd *ts_esd = &core_data->ts_esd;
  589. int r = 0;
  590. r = snprintf(buf, PAGE_SIZE, "state:%s\n",
  591. atomic_read(&ts_esd->esd_on) ?
  592. "enabled" : "disabled");
  593. return r;
  594. }
  595. /* enable/disable esd */
  596. static ssize_t goodix_ts_esd_info_store(struct device *dev,
  597. struct device_attribute *attr,
  598. const char *buf, size_t count)
  599. {
  600. if (!buf || count <= 0)
  601. return -EINVAL;
  602. if (buf[0] != '0')
  603. goodix_ts_blocking_notify(NOTIFY_ESD_ON, NULL);
  604. else
  605. goodix_ts_blocking_notify(NOTIFY_ESD_OFF, NULL);
  606. return count;
  607. }
  608. /* debug level show */
  609. static ssize_t goodix_ts_debug_log_show(struct device *dev,
  610. struct device_attribute *attr,
  611. char *buf)
  612. {
  613. int r = 0;
  614. r = snprintf(buf, PAGE_SIZE, "state:%s\n",
  615. debug_log_flag ?
  616. "enabled" : "disabled");
  617. return r;
  618. }
  619. /* debug level store */
  620. static ssize_t goodix_ts_debug_log_store(struct device *dev,
  621. struct device_attribute *attr,
  622. const char *buf, size_t count)
  623. {
  624. if (!buf || count <= 0)
  625. return -EINVAL;
  626. if (buf[0] != '0')
  627. debug_log_flag = true;
  628. else
  629. debug_log_flag = false;
  630. return count;
  631. }
  632. static DEVICE_ATTR(driver_info, 0444, goodix_ts_driver_info_show, NULL);
  633. static DEVICE_ATTR(chip_info, 0444, goodix_ts_chip_info_show, NULL);
  634. static DEVICE_ATTR(reset, 0220, NULL, goodix_ts_reset_store);
  635. static DEVICE_ATTR(send_cfg, 0220, NULL, goodix_ts_send_cfg_store);
  636. static DEVICE_ATTR(read_cfg, 0444, goodix_ts_read_cfg_show, NULL);
  637. static DEVICE_ATTR(reg_rw, 0664, goodix_ts_reg_rw_show, goodix_ts_reg_rw_store);
  638. static DEVICE_ATTR(irq_info, 0664, goodix_ts_irq_info_show, goodix_ts_irq_info_store);
  639. static DEVICE_ATTR(esd_info, 0664, goodix_ts_esd_info_show, goodix_ts_esd_info_store);
  640. static DEVICE_ATTR(debug_log, 0664, goodix_ts_debug_log_show, goodix_ts_debug_log_store);
  641. static struct attribute *sysfs_attrs[] = {
  642. &dev_attr_driver_info.attr,
  643. &dev_attr_chip_info.attr,
  644. &dev_attr_reset.attr,
  645. &dev_attr_send_cfg.attr,
  646. &dev_attr_read_cfg.attr,
  647. &dev_attr_reg_rw.attr,
  648. &dev_attr_irq_info.attr,
  649. &dev_attr_esd_info.attr,
  650. &dev_attr_debug_log.attr,
  651. NULL,
  652. };
  653. static const struct attribute_group sysfs_group = {
  654. .attrs = sysfs_attrs,
  655. };
  656. static int goodix_ts_sysfs_init(struct goodix_ts_core *core_data)
  657. {
  658. int ret;
  659. ret = sysfs_create_group(&core_data->pdev->dev.kobj, &sysfs_group);
  660. if (ret) {
  661. ts_err("failed create core sysfs group");
  662. return ret;
  663. }
  664. return ret;
  665. }
  666. static void goodix_ts_sysfs_exit(struct goodix_ts_core *core_data)
  667. {
  668. sysfs_remove_group(&core_data->pdev->dev.kobj, &sysfs_group);
  669. }
  670. /* prosfs create */
  671. static int rawdata_proc_show(struct seq_file *m, void *v)
  672. {
  673. struct ts_rawdata_info *info;
  674. struct goodix_ts_core *cd = m->private;
  675. int tx;
  676. int rx;
  677. int ret;
  678. int i;
  679. int index;
  680. if (!m || !v || !cd) {
  681. ts_err("rawdata_proc_show, input null ptr");
  682. return -EIO;
  683. }
  684. info = kzalloc(sizeof(*info), GFP_KERNEL);
  685. if (!info) {
  686. ts_err("Failed to alloc rawdata info memory");
  687. return -ENOMEM;
  688. }
  689. ret = cd->hw_ops->get_capacitance_data(cd, info);
  690. if (ret < 0) {
  691. ts_err("failed to get_capacitance_data, exit!");
  692. goto exit;
  693. }
  694. rx = info->buff[0];
  695. tx = info->buff[1];
  696. seq_printf(m, "TX:%d RX:%d\n", tx, rx);
  697. seq_printf(m, "mutual_rawdata:\n");
  698. index = 2;
  699. for (i = 0; i < tx * rx; i++) {
  700. seq_printf(m, "%5d,", info->buff[index + i]);
  701. if ((i + 1) % tx == 0)
  702. seq_printf(m, "\n");
  703. }
  704. seq_printf(m, "mutual_diffdata:\n");
  705. index += tx * rx;
  706. for (i = 0; i < tx * rx; i++) {
  707. seq_printf(m, "%3d,", info->buff[index + i]);
  708. if ((i + 1) % tx == 0)
  709. seq_printf(m, "\n");
  710. }
  711. exit:
  712. kfree(info);
  713. return ret;
  714. }
  715. static int rawdata_proc_open(struct inode *inode, struct file *file)
  716. {
  717. return single_open_size(file, rawdata_proc_show, PDE_DATA(inode), PAGE_SIZE * 10);
  718. }
  719. static const struct file_operations rawdata_proc_fops = {
  720. .open = rawdata_proc_open,
  721. .read = seq_read,
  722. .llseek = seq_lseek,
  723. .release = single_release,
  724. };
  725. static void goodix_ts_procfs_init(struct goodix_ts_core *core_data)
  726. {
  727. if (!proc_mkdir("goodix_ts", NULL))
  728. return;
  729. proc_create_data("goodix_ts/tp_capacitance_data",
  730. 0666, NULL, &rawdata_proc_fops, core_data);
  731. }
  732. static void goodix_ts_procfs_exit(struct goodix_ts_core *core_data)
  733. {
  734. remove_proc_entry("goodix_ts/tp_capacitance_data", NULL);
  735. remove_proc_entry("goodix_ts", NULL);
  736. }
  737. /* event notifier */
  738. static BLOCKING_NOTIFIER_HEAD(ts_notifier_list);
  739. /**
  740. * goodix_ts_register_client - register a client notifier
  741. * @nb: notifier block to callback on events
  742. * see enum ts_notify_event in goodix_ts_core.h
  743. */
  744. int goodix_ts_register_notifier(struct notifier_block *nb)
  745. {
  746. return blocking_notifier_chain_register(&ts_notifier_list, nb);
  747. }
  748. /**
  749. * goodix_ts_unregister_client - unregister a client notifier
  750. * @nb: notifier block to callback on events
  751. * see enum ts_notify_event in goodix_ts_core.h
  752. */
  753. int goodix_ts_unregister_notifier(struct notifier_block *nb)
  754. {
  755. return blocking_notifier_chain_unregister(&ts_notifier_list, nb);
  756. }
  757. /**
  758. * fb_notifier_call_chain - notify clients of fb_events
  759. * see enum ts_notify_event in goodix_ts_core.h
  760. */
  761. int goodix_ts_blocking_notify(enum ts_notify_event evt, void *v)
  762. {
  763. int ret;
  764. ret = blocking_notifier_call_chain(&ts_notifier_list,
  765. (unsigned long)evt, v);
  766. return ret;
  767. }
  768. #ifdef CONFIG_OF
  769. /**
  770. * goodix_parse_dt_resolution - parse resolution from dt
  771. * @node: devicetree node
  772. * @board_data: pointer to board data structure
  773. * return: 0 - no error, <0 error
  774. */
  775. static int goodix_parse_dt_resolution(struct device_node *node,
  776. struct goodix_ts_board_data *board_data)
  777. {
  778. int ret;
  779. ret = of_property_read_u32(node, "goodix,panel-max-x",
  780. &board_data->panel_max_x);
  781. if (ret) {
  782. ts_err("failed get panel-max-x");
  783. return ret;
  784. }
  785. ret = of_property_read_u32(node, "goodix,panel-max-y",
  786. &board_data->panel_max_y);
  787. if (ret) {
  788. ts_err("failed get panel-max-y");
  789. return ret;
  790. }
  791. ret = of_property_read_u32(node, "goodix,panel-max-w",
  792. &board_data->panel_max_w);
  793. if (ret) {
  794. ts_err("failed get panel-max-w");
  795. return ret;
  796. }
  797. ret = of_property_read_u32(node, "goodix,panel-max-p",
  798. &board_data->panel_max_p);
  799. if (ret) {
  800. ts_err("failed get panel-max-p, use default");
  801. board_data->panel_max_p = GOODIX_PEN_MAX_PRESSURE;
  802. }
  803. return 0;
  804. }
  805. /**
  806. * goodix_parse_dt - parse board data from dt
  807. * @dev: pointer to device
  808. * @board_data: pointer to board data structure
  809. * return: 0 - no error, <0 error
  810. */
  811. static int goodix_parse_dt(struct device_node *node,
  812. struct goodix_ts_board_data *board_data)
  813. {
  814. const char *name_tmp;
  815. int r;
  816. if (!board_data) {
  817. ts_err("invalid board data");
  818. return -EINVAL;
  819. }
  820. r = of_get_named_gpio(node, "goodix,avdd-gpio", 0);
  821. if (r < 0) {
  822. ts_info("can't find avdd-gpio, use other power supply");
  823. board_data->avdd_gpio = 0;
  824. } else {
  825. ts_info("get avdd-gpio[%d] from dt", r);
  826. board_data->avdd_gpio = r;
  827. }
  828. r = of_get_named_gpio(node, "goodix,iovdd-gpio", 0);
  829. if (r < 0) {
  830. ts_info("can't find iovdd-gpio, use other power supply");
  831. board_data->iovdd_gpio = 0;
  832. } else {
  833. ts_info("get iovdd-gpio[%d] from dt", r);
  834. board_data->iovdd_gpio = r;
  835. }
  836. r = of_get_named_gpio(node, "goodix,reset-gpio", 0);
  837. if (r < 0) {
  838. ts_err("invalid reset-gpio in dt: %d", r);
  839. return -EINVAL;
  840. }
  841. ts_info("get reset-gpio[%d] from dt", r);
  842. board_data->reset_gpio = r;
  843. r = of_get_named_gpio(node, "goodix,irq-gpio", 0);
  844. if (r < 0) {
  845. ts_err("invalid irq-gpio in dt: %d", r);
  846. return -EINVAL;
  847. }
  848. ts_info("get irq-gpio[%d] from dt", r);
  849. board_data->irq_gpio = r;
  850. r = of_property_read_u32(node, "goodix,irq-flags",
  851. &board_data->irq_flags);
  852. if (r) {
  853. ts_err("invalid irq-flags");
  854. return -EINVAL;
  855. }
  856. memset(board_data->avdd_name, 0, sizeof(board_data->avdd_name));
  857. r = of_property_read_string(node, "goodix,avdd-name", &name_tmp);
  858. if (!r) {
  859. ts_info("avdd name from dt: %s", name_tmp);
  860. if (strlen(name_tmp) < sizeof(board_data->avdd_name))
  861. strncpy(board_data->avdd_name,
  862. name_tmp, sizeof(board_data->avdd_name));
  863. else
  864. ts_info("invalied avdd name length: %ld > %ld",
  865. strlen(name_tmp),
  866. sizeof(board_data->avdd_name));
  867. }
  868. memset(board_data->iovdd_name, 0, sizeof(board_data->iovdd_name));
  869. r = of_property_read_string(node, "goodix,iovdd-name", &name_tmp);
  870. if (!r) {
  871. ts_info("iovdd name from dt: %s", name_tmp);
  872. if (strlen(name_tmp) < sizeof(board_data->iovdd_name))
  873. strncpy(board_data->iovdd_name,
  874. name_tmp, sizeof(board_data->iovdd_name));
  875. else
  876. ts_info("invalied iovdd name length: %ld > %ld",
  877. strlen(name_tmp),
  878. sizeof(board_data->iovdd_name));
  879. }
  880. /* get firmware file name */
  881. r = of_property_read_string(node, "goodix,firmware-name", &name_tmp);
  882. if (!r) {
  883. ts_info("firmware name from dt: %s", name_tmp);
  884. strncpy(board_data->fw_name, name_tmp, sizeof(board_data->fw_name));
  885. } else {
  886. ts_info("can't find firmware name, use default: %s", TS_DEFAULT_FIRMWARE);
  887. strncpy(board_data->fw_name, TS_DEFAULT_FIRMWARE, sizeof(board_data->fw_name));
  888. }
  889. /* get config file name */
  890. r = of_property_read_string(node, "goodix,config-name", &name_tmp);
  891. if (!r) {
  892. ts_info("config name from dt: %s", name_tmp);
  893. strncpy(board_data->cfg_bin_name, name_tmp, sizeof(board_data->cfg_bin_name));
  894. } else {
  895. ts_info("can't find config name, use default: %s", TS_DEFAULT_CFG_BIN);
  896. strncpy(board_data->cfg_bin_name, TS_DEFAULT_CFG_BIN, sizeof(board_data->cfg_bin_name));
  897. }
  898. /* get xyz resolutions */
  899. r = goodix_parse_dt_resolution(node, board_data);
  900. if (r) {
  901. ts_err("Failed to parse resolutions:%d", r);
  902. return r;
  903. }
  904. /*get pen-enable switch and pen keys, must after "key map"*/
  905. board_data->pen_enable = of_property_read_bool(node,
  906. "goodix,pen-enable");
  907. if (board_data->pen_enable)
  908. ts_info("goodix pen enabled");
  909. ts_debug("[DT]x:%d, y:%d, w:%d, p:%d", board_data->panel_max_x,
  910. board_data->panel_max_y, board_data->panel_max_w,
  911. board_data->panel_max_p);
  912. return 0;
  913. }
  914. #endif
  915. static void goodix_ts_report_pen(struct input_dev *dev,
  916. struct goodix_pen_data *pen_data)
  917. {
  918. int i;
  919. mutex_lock(&dev->mutex);
  920. if (pen_data->coords.status == TS_TOUCH) {
  921. input_report_key(dev, BTN_TOUCH, 1);
  922. input_report_key(dev, pen_data->coords.tool_type, 1);
  923. input_report_abs(dev, ABS_X, pen_data->coords.x);
  924. input_report_abs(dev, ABS_Y, pen_data->coords.y);
  925. input_report_abs(dev, ABS_PRESSURE, pen_data->coords.p);
  926. input_report_abs(dev, ABS_TILT_X, pen_data->coords.tilt_x);
  927. input_report_abs(dev, ABS_TILT_Y, pen_data->coords.tilt_y);
  928. ts_debug("pen_data:x %d, y %d, p %d, tilt_x %d tilt_y %d key[%d %d]",
  929. pen_data->coords.x, pen_data->coords.y,
  930. pen_data->coords.p, pen_data->coords.tilt_x,
  931. pen_data->coords.tilt_y, pen_data->keys[0].status == TS_TOUCH ? 1 : 0,
  932. pen_data->keys[1].status == TS_TOUCH ? 1 : 0);
  933. } else {
  934. input_report_key(dev, BTN_TOUCH, 0);
  935. input_report_key(dev, pen_data->coords.tool_type, 0);
  936. }
  937. /* report pen button */
  938. for (i = 0; i < GOODIX_MAX_PEN_KEY; i++) {
  939. if (pen_data->keys[i].status == TS_TOUCH)
  940. input_report_key(dev, pen_data->keys[i].code, 1);
  941. else
  942. input_report_key(dev, pen_data->keys[i].code, 0);
  943. }
  944. input_sync(dev);
  945. mutex_unlock(&dev->mutex);
  946. }
  947. static void goodix_ts_report_finger(struct input_dev *dev,
  948. struct goodix_touch_data *touch_data)
  949. {
  950. unsigned int touch_num = touch_data->touch_num;
  951. int i;
  952. mutex_lock(&dev->mutex);
  953. for (i = 0; i < GOODIX_MAX_TOUCH; i++) {
  954. if (touch_data->coords[i].status == TS_TOUCH) {
  955. ts_debug("report: id %d, x %d, y %d, w %d", i,
  956. touch_data->coords[i].x, touch_data->coords[i].y,
  957. touch_data->coords[i].w);
  958. input_mt_slot(dev, i);
  959. input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
  960. input_report_abs(dev, ABS_MT_POSITION_X,
  961. touch_data->coords[i].x);
  962. input_report_abs(dev, ABS_MT_POSITION_Y,
  963. touch_data->coords[i].y);
  964. input_report_abs(dev, ABS_MT_TOUCH_MAJOR,
  965. touch_data->coords[i].w);
  966. } else {
  967. input_mt_slot(dev, i);
  968. input_mt_report_slot_state(dev, MT_TOOL_FINGER, false);
  969. }
  970. }
  971. input_report_key(dev, BTN_TOUCH, touch_num > 0 ? 1 : 0);
  972. input_sync(dev);
  973. mutex_unlock(&dev->mutex);
  974. }
  975. static int goodix_ts_request_handle(struct goodix_ts_core *cd,
  976. struct goodix_ts_event *ts_event)
  977. {
  978. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  979. int ret = -1;
  980. if (ts_event->request_code == REQUEST_TYPE_CONFIG)
  981. ret = goodix_send_ic_config(cd, CONFIG_TYPE_NORMAL);
  982. else if (ts_event->request_code == REQUEST_TYPE_RESET)
  983. ret = hw_ops->reset(cd, GOODIX_NORMAL_RESET_DELAY_MS);
  984. else
  985. ts_info("can not handle request type 0x%x",
  986. ts_event->request_code);
  987. if (ret)
  988. ts_err("failed handle request 0x%x",
  989. ts_event->request_code);
  990. else
  991. ts_info("success handle ic request 0x%x",
  992. ts_event->request_code);
  993. return ret;
  994. }
  995. /**
  996. * goodix_ts_threadirq_func - Bottom half of interrupt
  997. * This functions is excuted in thread context,
  998. * sleep in this function is permit.
  999. *
  1000. * @data: pointer to touch core data
  1001. * return: 0 ok, <0 failed
  1002. */
  1003. static irqreturn_t goodix_ts_threadirq_func(int irq, void *data)
  1004. {
  1005. struct goodix_ts_core *core_data = data;
  1006. struct goodix_ts_hw_ops *hw_ops = core_data->hw_ops;
  1007. struct goodix_ext_module *ext_module, *next;
  1008. struct goodix_ts_event *ts_event = &core_data->ts_event;
  1009. struct goodix_ts_esd *ts_esd = &core_data->ts_esd;
  1010. int ret;
  1011. ts_esd->irq_status = true;
  1012. core_data->irq_trig_cnt++;
  1013. /* inform external module */
  1014. mutex_lock(&goodix_modules.mutex);
  1015. list_for_each_entry_safe(ext_module, next,
  1016. &goodix_modules.head, list) {
  1017. if (!ext_module->funcs->irq_event)
  1018. continue;
  1019. ret = ext_module->funcs->irq_event(core_data, ext_module);
  1020. if (ret == EVT_CANCEL_IRQEVT) {
  1021. mutex_unlock(&goodix_modules.mutex);
  1022. return IRQ_HANDLED;
  1023. }
  1024. }
  1025. mutex_unlock(&goodix_modules.mutex);
  1026. /* read touch data from touch device */
  1027. ret = hw_ops->event_handler(core_data, ts_event);
  1028. if (likely(!ret)) {
  1029. if (ts_event->event_type == EVENT_TOUCH) {
  1030. /* report touch */
  1031. goodix_ts_report_finger(core_data->input_dev,
  1032. &ts_event->touch_data);
  1033. }
  1034. if (core_data->board_data.pen_enable &&
  1035. ts_event->event_type == EVENT_PEN) {
  1036. goodix_ts_report_pen(core_data->pen_dev,
  1037. &ts_event->pen_data);
  1038. }
  1039. if (ts_event->event_type == EVENT_REQUEST) {
  1040. goodix_ts_request_handle(core_data, ts_event);
  1041. }
  1042. }
  1043. if (!core_data->tools_ctrl_sync && !ts_event->retry)
  1044. hw_ops->after_event_handler(core_data);
  1045. ts_event->retry = 0;
  1046. return IRQ_HANDLED;
  1047. }
  1048. /**
  1049. * goodix_ts_init_irq - Requset interrput line from system
  1050. * @core_data: pointer to touch core data
  1051. * return: 0 ok, <0 failed
  1052. */
  1053. static int goodix_ts_irq_setup(struct goodix_ts_core *core_data)
  1054. {
  1055. const struct goodix_ts_board_data *ts_bdata = board_data(core_data);
  1056. int ret;
  1057. /* if ts_bdata-> irq is invalid */
  1058. core_data->irq = gpio_to_irq(ts_bdata->irq_gpio);
  1059. if (core_data->irq < 0) {
  1060. ts_err("failed get irq num %d", core_data->irq);
  1061. return -EINVAL;
  1062. }
  1063. ts_info("IRQ:%u,flags:%d", core_data->irq, (int)ts_bdata->irq_flags);
  1064. ret = devm_request_threaded_irq(&core_data->pdev->dev,
  1065. core_data->irq, NULL,
  1066. goodix_ts_threadirq_func,
  1067. ts_bdata->irq_flags | IRQF_ONESHOT,
  1068. GOODIX_CORE_DRIVER_NAME,
  1069. core_data);
  1070. if (ret < 0)
  1071. ts_err("Failed to requeset threaded irq:%d", ret);
  1072. else
  1073. atomic_set(&core_data->irq_enabled, 1);
  1074. return ret;
  1075. }
  1076. /**
  1077. * goodix_ts_power_init - Get regulator for touch device
  1078. * @core_data: pointer to touch core data
  1079. * return: 0 ok, <0 failed
  1080. */
  1081. static int goodix_ts_power_init(struct goodix_ts_core *core_data)
  1082. {
  1083. struct goodix_ts_board_data *ts_bdata = board_data(core_data);
  1084. struct device *dev = core_data->bus->dev;
  1085. int ret = 0;
  1086. ts_info("Power init");
  1087. if (strlen(ts_bdata->avdd_name)) {
  1088. core_data->avdd = devm_regulator_get(dev,
  1089. ts_bdata->avdd_name);
  1090. if (IS_ERR_OR_NULL(core_data->avdd)) {
  1091. ret = PTR_ERR(core_data->avdd);
  1092. ts_err("Failed to get regulator avdd:%d", ret);
  1093. core_data->avdd = NULL;
  1094. return ret;
  1095. }
  1096. } else {
  1097. ts_info("Avdd name is NULL");
  1098. }
  1099. if (strlen(ts_bdata->iovdd_name)) {
  1100. core_data->iovdd = devm_regulator_get(dev,
  1101. ts_bdata->iovdd_name);
  1102. if (IS_ERR_OR_NULL(core_data->iovdd)) {
  1103. ret = PTR_ERR(core_data->iovdd);
  1104. ts_err("Failed to get regulator iovdd:%d", ret);
  1105. core_data->iovdd = NULL;
  1106. }
  1107. } else {
  1108. ts_info("iovdd name is NULL");
  1109. }
  1110. return ret;
  1111. }
  1112. /**
  1113. * goodix_ts_power_on - Turn on power to the touch device
  1114. * @core_data: pointer to touch core data
  1115. * return: 0 ok, <0 failed
  1116. */
  1117. int goodix_ts_power_on(struct goodix_ts_core *cd)
  1118. {
  1119. int ret = 0;
  1120. ts_info("Device power on");
  1121. if (cd->power_on)
  1122. return 0;
  1123. ret = cd->hw_ops->power_on(cd, true);
  1124. if (!ret)
  1125. cd->power_on = 1;
  1126. else
  1127. ts_err("failed power on, %d", ret);
  1128. return ret;
  1129. }
  1130. /**
  1131. * goodix_ts_power_off - Turn off power to the touch device
  1132. * @core_data: pointer to touch core data
  1133. * return: 0 ok, <0 failed
  1134. */
  1135. int goodix_ts_power_off(struct goodix_ts_core *cd)
  1136. {
  1137. int ret;
  1138. ts_info("Device power off");
  1139. if (!cd->power_on)
  1140. return 0;
  1141. ret = cd->hw_ops->power_on(cd, false);
  1142. if (!ret)
  1143. cd->power_on = 0;
  1144. else
  1145. ts_err("failed power off, %d", ret);
  1146. return ret;
  1147. }
  1148. /**
  1149. * goodix_ts_gpio_setup - Request gpio resources from GPIO subsysten
  1150. * @core_data: pointer to touch core data
  1151. * return: 0 ok, <0 failed
  1152. */
  1153. static int goodix_ts_gpio_setup(struct goodix_ts_core *core_data)
  1154. {
  1155. struct goodix_ts_board_data *ts_bdata = board_data(core_data);
  1156. int r = 0;
  1157. ts_info("GPIO setup,reset-gpio:%d, irq-gpio:%d",
  1158. ts_bdata->reset_gpio, ts_bdata->irq_gpio);
  1159. /*
  1160. * after kenerl3.13, gpio_ api is deprecated, new
  1161. * driver should use gpiod_ api.
  1162. */
  1163. r = devm_gpio_request_one(&core_data->pdev->dev, ts_bdata->reset_gpio,
  1164. GPIOF_OUT_INIT_LOW, "ts_reset_gpio");
  1165. if (r < 0) {
  1166. ts_err("Failed to request reset gpio, r:%d", r);
  1167. return r;
  1168. }
  1169. r = devm_gpio_request_one(&core_data->pdev->dev, ts_bdata->irq_gpio,
  1170. GPIOF_IN, "ts_irq_gpio");
  1171. if (r < 0) {
  1172. ts_err("Failed to request irq gpio, r:%d", r);
  1173. return r;
  1174. }
  1175. if (ts_bdata->avdd_gpio > 0) {
  1176. r = devm_gpio_request_one(&core_data->pdev->dev, ts_bdata->avdd_gpio,
  1177. GPIOF_OUT_INIT_LOW, "ts_avdd_gpio");
  1178. if (r < 0) {
  1179. ts_err("Failed to request avdd-gpio, r:%d", r);
  1180. return r;
  1181. }
  1182. }
  1183. if (ts_bdata->iovdd_gpio > 0) {
  1184. r = devm_gpio_request_one(&core_data->pdev->dev, ts_bdata->iovdd_gpio,
  1185. GPIOF_OUT_INIT_LOW, "ts_iovdd_gpio");
  1186. if (r < 0) {
  1187. ts_err("Failed to request iovdd-gpio, r:%d", r);
  1188. return r;
  1189. }
  1190. }
  1191. return 0;
  1192. }
  1193. /**
  1194. * goodix_ts_input_dev_config - Requset and config a input device
  1195. * then register it to input sybsystem.
  1196. * @core_data: pointer to touch core data
  1197. * return: 0 ok, <0 failed
  1198. */
  1199. static int goodix_ts_input_dev_config(struct goodix_ts_core *core_data)
  1200. {
  1201. struct goodix_ts_board_data *ts_bdata = board_data(core_data);
  1202. struct input_dev *input_dev = NULL;
  1203. int r;
  1204. input_dev = input_allocate_device();
  1205. if (!input_dev) {
  1206. ts_err("Failed to allocated input device");
  1207. return -ENOMEM;
  1208. }
  1209. core_data->input_dev = input_dev;
  1210. input_set_drvdata(input_dev, core_data);
  1211. input_dev->name = GOODIX_CORE_DRIVER_NAME;
  1212. input_dev->phys = GOOIDX_INPUT_PHYS;
  1213. input_dev->id.product = 0xDEAD;
  1214. input_dev->id.vendor = 0xBEEF;
  1215. input_dev->id.version = 10427;
  1216. __set_bit(EV_SYN, input_dev->evbit);
  1217. __set_bit(EV_KEY, input_dev->evbit);
  1218. __set_bit(EV_ABS, input_dev->evbit);
  1219. __set_bit(BTN_TOUCH, input_dev->keybit);
  1220. __set_bit(BTN_TOOL_FINGER, input_dev->keybit);
  1221. #ifdef INPUT_PROP_DIRECT
  1222. __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
  1223. #endif
  1224. /* set input parameters */
  1225. input_set_abs_params(input_dev, ABS_MT_POSITION_X,
  1226. 0, ts_bdata->panel_max_x, 0, 0);
  1227. input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
  1228. 0, ts_bdata->panel_max_y, 0, 0);
  1229. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
  1230. 0, ts_bdata->panel_max_w, 0, 0);
  1231. #ifdef INPUT_TYPE_B_PROTOCOL
  1232. #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 7, 0)
  1233. input_mt_init_slots(input_dev, GOODIX_MAX_TOUCH,
  1234. INPUT_MT_DIRECT);
  1235. #else
  1236. input_mt_init_slots(input_dev, GOODIX_MAX_TOUCH);
  1237. #endif
  1238. #endif
  1239. input_set_capability(input_dev, EV_KEY, KEY_POWER);
  1240. r = input_register_device(input_dev);
  1241. if (r < 0) {
  1242. ts_err("Unable to register input device");
  1243. input_free_device(input_dev);
  1244. return r;
  1245. }
  1246. return 0;
  1247. }
  1248. static int goodix_ts_pen_dev_config(struct goodix_ts_core *core_data)
  1249. {
  1250. struct goodix_ts_board_data *ts_bdata = board_data(core_data);
  1251. struct input_dev *pen_dev = NULL;
  1252. int r;
  1253. pen_dev = input_allocate_device();
  1254. if (!pen_dev) {
  1255. ts_err("Failed to allocated pen device");
  1256. return -ENOMEM;
  1257. }
  1258. core_data->pen_dev = pen_dev;
  1259. input_set_drvdata(pen_dev, core_data);
  1260. pen_dev->name = GOODIX_PEN_DRIVER_NAME;
  1261. pen_dev->id.product = 0xDEAD;
  1262. pen_dev->id.vendor = 0xBEEF;
  1263. pen_dev->id.version = 10427;
  1264. pen_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  1265. __set_bit(ABS_X, pen_dev->absbit);
  1266. __set_bit(ABS_Y, pen_dev->absbit);
  1267. __set_bit(ABS_TILT_X, pen_dev->absbit);
  1268. __set_bit(ABS_TILT_Y, pen_dev->absbit);
  1269. __set_bit(BTN_STYLUS, pen_dev->keybit);
  1270. __set_bit(BTN_STYLUS2, pen_dev->keybit);
  1271. __set_bit(BTN_TOUCH, pen_dev->keybit);
  1272. __set_bit(BTN_TOOL_PEN, pen_dev->keybit);
  1273. __set_bit(INPUT_PROP_DIRECT, pen_dev->propbit);
  1274. input_set_abs_params(pen_dev, ABS_X, 0, ts_bdata->panel_max_x, 0, 0);
  1275. input_set_abs_params(pen_dev, ABS_Y, 0, ts_bdata->panel_max_y, 0, 0);
  1276. input_set_abs_params(pen_dev, ABS_PRESSURE, 0,
  1277. ts_bdata->panel_max_p, 0, 0);
  1278. input_set_abs_params(pen_dev, ABS_TILT_X,
  1279. -GOODIX_PEN_MAX_TILT, GOODIX_PEN_MAX_TILT, 0, 0);
  1280. input_set_abs_params(pen_dev, ABS_TILT_Y,
  1281. -GOODIX_PEN_MAX_TILT, GOODIX_PEN_MAX_TILT, 0, 0);
  1282. r = input_register_device(pen_dev);
  1283. if (r < 0) {
  1284. ts_err("Unable to register pen device");
  1285. input_free_device(pen_dev);
  1286. return r;
  1287. }
  1288. return 0;
  1289. }
  1290. void goodix_ts_input_dev_remove(struct goodix_ts_core *core_data)
  1291. {
  1292. if (!core_data->input_dev)
  1293. return;
  1294. input_unregister_device(core_data->input_dev);
  1295. input_free_device(core_data->input_dev);
  1296. core_data->input_dev = NULL;
  1297. }
  1298. void goodix_ts_pen_dev_remove(struct goodix_ts_core *core_data)
  1299. {
  1300. if (!core_data->pen_dev)
  1301. return;
  1302. input_unregister_device(core_data->pen_dev);
  1303. input_free_device(core_data->pen_dev);
  1304. core_data->pen_dev = NULL;
  1305. }
  1306. /**
  1307. * goodix_ts_esd_work - check hardware status and recovery
  1308. * the hardware if needed.
  1309. */
  1310. static void goodix_ts_esd_work(struct work_struct *work)
  1311. {
  1312. struct delayed_work *dwork = to_delayed_work(work);
  1313. struct goodix_ts_esd *ts_esd = container_of(dwork,
  1314. struct goodix_ts_esd, esd_work);
  1315. struct goodix_ts_core *cd = container_of(ts_esd,
  1316. struct goodix_ts_core, ts_esd);
  1317. const struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  1318. int ret = 0;
  1319. if (ts_esd->irq_status)
  1320. goto exit;
  1321. if (!atomic_read(&ts_esd->esd_on))
  1322. return;
  1323. if (!hw_ops->esd_check)
  1324. return;
  1325. ret = hw_ops->esd_check(cd);
  1326. if (ret) {
  1327. ts_err("esd check failed");
  1328. goodix_ts_power_off(cd);
  1329. usleep_range(5000, 5100);
  1330. goodix_ts_power_on(cd);
  1331. }
  1332. exit:
  1333. ts_esd->irq_status = false;
  1334. if (atomic_read(&ts_esd->esd_on))
  1335. schedule_delayed_work(&ts_esd->esd_work, 2 * HZ);
  1336. }
  1337. /**
  1338. * goodix_ts_esd_on - turn on esd protection
  1339. */
  1340. static void goodix_ts_esd_on(struct goodix_ts_core *cd)
  1341. {
  1342. struct goodix_ic_info_misc *misc = &cd->ic_info.misc;
  1343. struct goodix_ts_esd *ts_esd = &cd->ts_esd;
  1344. if (!misc->esd_addr)
  1345. return;
  1346. if (atomic_read(&ts_esd->esd_on))
  1347. return;
  1348. atomic_set(&ts_esd->esd_on, 1);
  1349. if (!schedule_delayed_work(&ts_esd->esd_work, 2 * HZ)) {
  1350. ts_info("esd work already in workqueue");
  1351. }
  1352. ts_info("esd on");
  1353. }
  1354. /**
  1355. * goodix_ts_esd_off - turn off esd protection
  1356. */
  1357. static void goodix_ts_esd_off(struct goodix_ts_core *cd)
  1358. {
  1359. struct goodix_ts_esd *ts_esd = &cd->ts_esd;
  1360. int ret;
  1361. if (!atomic_read(&ts_esd->esd_on))
  1362. return;
  1363. atomic_set(&ts_esd->esd_on, 0);
  1364. ret = cancel_delayed_work_sync(&ts_esd->esd_work);
  1365. ts_info("Esd off, esd work state %d", ret);
  1366. }
  1367. /**
  1368. * goodix_esd_notifier_callback - notification callback
  1369. * under certain condition, we need to turn off/on the esd
  1370. * protector, we use kernel notify call chain to achieve this.
  1371. *
  1372. * for example: before firmware update we need to turn off the
  1373. * esd protector and after firmware update finished, we should
  1374. * turn on the esd protector.
  1375. */
  1376. static int goodix_esd_notifier_callback(struct notifier_block *nb,
  1377. unsigned long action, void *data)
  1378. {
  1379. struct goodix_ts_esd *ts_esd = container_of(nb,
  1380. struct goodix_ts_esd, esd_notifier);
  1381. switch (action) {
  1382. case NOTIFY_FWUPDATE_START:
  1383. case NOTIFY_SUSPEND:
  1384. case NOTIFY_ESD_OFF:
  1385. goodix_ts_esd_off(ts_esd->ts_core);
  1386. break;
  1387. case NOTIFY_FWUPDATE_FAILED:
  1388. case NOTIFY_FWUPDATE_SUCCESS:
  1389. case NOTIFY_RESUME:
  1390. case NOTIFY_ESD_ON:
  1391. goodix_ts_esd_on(ts_esd->ts_core);
  1392. break;
  1393. default:
  1394. break;
  1395. }
  1396. return 0;
  1397. }
  1398. /**
  1399. * goodix_ts_esd_init - initialize esd protection
  1400. */
  1401. int goodix_ts_esd_init(struct goodix_ts_core *cd)
  1402. {
  1403. struct goodix_ic_info_misc *misc = &cd->ic_info.misc;
  1404. struct goodix_ts_esd *ts_esd = &cd->ts_esd;
  1405. if (!cd->hw_ops->esd_check || !misc->esd_addr) {
  1406. ts_info("missing key info for esd check");
  1407. return 0;
  1408. }
  1409. INIT_DELAYED_WORK(&ts_esd->esd_work, goodix_ts_esd_work);
  1410. ts_esd->ts_core = cd;
  1411. atomic_set(&ts_esd->esd_on, 0);
  1412. ts_esd->esd_notifier.notifier_call = goodix_esd_notifier_callback;
  1413. goodix_ts_register_notifier(&ts_esd->esd_notifier);
  1414. goodix_ts_esd_on(cd);
  1415. return 0;
  1416. }
  1417. static void goodix_ts_release_connects(struct goodix_ts_core *core_data)
  1418. {
  1419. struct input_dev *input_dev = core_data->input_dev;
  1420. int i;
  1421. mutex_lock(&input_dev->mutex);
  1422. for (i = 0; i < GOODIX_MAX_TOUCH; i++) {
  1423. input_mt_slot(input_dev, i);
  1424. input_mt_report_slot_state(input_dev,
  1425. MT_TOOL_FINGER,
  1426. false);
  1427. }
  1428. input_report_key(input_dev, BTN_TOUCH, 0);
  1429. input_mt_sync_frame(input_dev);
  1430. input_sync(input_dev);
  1431. mutex_unlock(&input_dev->mutex);
  1432. }
  1433. /**
  1434. * goodix_ts_suspend - Touchscreen suspend function
  1435. * Called by PM/FB/EARLYSUSPEN module to put the device to sleep
  1436. */
  1437. static int goodix_ts_suspend(struct goodix_ts_core *core_data)
  1438. {
  1439. struct goodix_ext_module *ext_module, *next;
  1440. struct goodix_ts_hw_ops *hw_ops = core_data->hw_ops;
  1441. int ret;
  1442. if (core_data->init_stage < CORE_INIT_STAGE2 ||
  1443. atomic_read(&core_data->suspended))
  1444. return 0;
  1445. ts_info("Suspend start");
  1446. atomic_set(&core_data->suspended, 1);
  1447. /* disable irq */
  1448. hw_ops->irq_enable(core_data, false);
  1449. /*
  1450. * notify suspend event, inform the esd protector
  1451. * and charger detector to turn off the work
  1452. */
  1453. goodix_ts_blocking_notify(NOTIFY_SUSPEND, NULL);
  1454. /* inform external module */
  1455. mutex_lock(&goodix_modules.mutex);
  1456. if (!list_empty(&goodix_modules.head)) {
  1457. list_for_each_entry_safe(ext_module, next,
  1458. &goodix_modules.head, list) {
  1459. if (!ext_module->funcs->before_suspend)
  1460. continue;
  1461. ret = ext_module->funcs->before_suspend(core_data,
  1462. ext_module);
  1463. if (ret == EVT_CANCEL_SUSPEND) {
  1464. mutex_unlock(&goodix_modules.mutex);
  1465. ts_info("Canceled by module:%s",
  1466. ext_module->name);
  1467. goto out;
  1468. }
  1469. }
  1470. }
  1471. mutex_unlock(&goodix_modules.mutex);
  1472. /* enter sleep mode or power off */
  1473. if (hw_ops->suspend)
  1474. hw_ops->suspend(core_data);
  1475. /* inform exteranl modules */
  1476. mutex_lock(&goodix_modules.mutex);
  1477. if (!list_empty(&goodix_modules.head)) {
  1478. list_for_each_entry_safe(ext_module, next,
  1479. &goodix_modules.head, list) {
  1480. if (!ext_module->funcs->after_suspend)
  1481. continue;
  1482. ret = ext_module->funcs->after_suspend(core_data,
  1483. ext_module);
  1484. if (ret == EVT_CANCEL_SUSPEND) {
  1485. mutex_unlock(&goodix_modules.mutex);
  1486. ts_info("Canceled by module:%s",
  1487. ext_module->name);
  1488. goto out;
  1489. }
  1490. }
  1491. }
  1492. mutex_unlock(&goodix_modules.mutex);
  1493. out:
  1494. goodix_ts_release_connects(core_data);
  1495. ts_info("Suspend end");
  1496. return 0;
  1497. }
  1498. /**
  1499. * goodix_ts_resume - Touchscreen resume function
  1500. * Called by PM/FB/EARLYSUSPEN module to wakeup device
  1501. */
  1502. static int goodix_ts_resume(struct goodix_ts_core *core_data)
  1503. {
  1504. struct goodix_ext_module *ext_module, *next;
  1505. struct goodix_ts_hw_ops *hw_ops = core_data->hw_ops;
  1506. int ret;
  1507. if (core_data->init_stage < CORE_INIT_STAGE2 ||
  1508. !atomic_read(&core_data->suspended))
  1509. return 0;
  1510. ts_info("Resume start");
  1511. atomic_set(&core_data->suspended, 0);
  1512. mutex_lock(&goodix_modules.mutex);
  1513. if (!list_empty(&goodix_modules.head)) {
  1514. list_for_each_entry_safe(ext_module, next,
  1515. &goodix_modules.head, list) {
  1516. if (!ext_module->funcs->before_resume)
  1517. continue;
  1518. ret = ext_module->funcs->before_resume(core_data,
  1519. ext_module);
  1520. if (ret == EVT_CANCEL_RESUME) {
  1521. mutex_unlock(&goodix_modules.mutex);
  1522. ts_info("Canceled by module:%s",
  1523. ext_module->name);
  1524. goto out;
  1525. }
  1526. }
  1527. }
  1528. mutex_unlock(&goodix_modules.mutex);
  1529. /* reset device or power on*/
  1530. if (hw_ops->resume)
  1531. hw_ops->resume(core_data);
  1532. mutex_lock(&goodix_modules.mutex);
  1533. if (!list_empty(&goodix_modules.head)) {
  1534. list_for_each_entry_safe(ext_module, next,
  1535. &goodix_modules.head, list) {
  1536. if (!ext_module->funcs->after_resume)
  1537. continue;
  1538. ret = ext_module->funcs->after_resume(core_data,
  1539. ext_module);
  1540. if (ret == EVT_CANCEL_RESUME) {
  1541. mutex_unlock(&goodix_modules.mutex);
  1542. ts_info("Canceled by module:%s",
  1543. ext_module->name);
  1544. goto out;
  1545. }
  1546. }
  1547. }
  1548. mutex_unlock(&goodix_modules.mutex);
  1549. out:
  1550. /* enable irq */
  1551. hw_ops->irq_enable(core_data, true);
  1552. /* open esd */
  1553. goodix_ts_blocking_notify(NOTIFY_RESUME, NULL);
  1554. ts_info("Resume end");
  1555. return 0;
  1556. }
  1557. #ifdef CONFIG_FB
  1558. /**
  1559. * goodix_ts_fb_notifier_callback - Framebuffer notifier callback
  1560. * Called by kernel during framebuffer blanck/unblank phrase
  1561. */
  1562. int goodix_ts_fb_notifier_callback(struct notifier_block *self,
  1563. unsigned long event, void *data)
  1564. {
  1565. struct goodix_ts_core *core_data =
  1566. container_of(self, struct goodix_ts_core, fb_notifier);
  1567. struct fb_event *fb_event = data;
  1568. if (fb_event && fb_event->data && core_data) {
  1569. if (event == FB_EARLY_EVENT_BLANK) {
  1570. /* before fb blank */
  1571. } else if (event == FB_EVENT_BLANK) {
  1572. int *blank = fb_event->data;
  1573. if (*blank == FB_BLANK_UNBLANK)
  1574. goodix_ts_resume(core_data);
  1575. else if (*blank == FB_BLANK_POWERDOWN)
  1576. goodix_ts_suspend(core_data);
  1577. }
  1578. }
  1579. return 0;
  1580. }
  1581. #endif
  1582. #ifdef CONFIG_PM
  1583. #if !defined(CONFIG_FB) && !defined(CONFIG_HAS_EARLYSUSPEND)
  1584. /**
  1585. * goodix_ts_pm_suspend - PM suspend function
  1586. * Called by kernel during system suspend phrase
  1587. */
  1588. static int goodix_ts_pm_suspend(struct device *dev)
  1589. {
  1590. struct goodix_ts_core *core_data =
  1591. dev_get_drvdata(dev);
  1592. return goodix_ts_suspend(core_data);
  1593. }
  1594. /**
  1595. * goodix_ts_pm_resume - PM resume function
  1596. * Called by kernel during system wakeup
  1597. */
  1598. static int goodix_ts_pm_resume(struct device *dev)
  1599. {
  1600. struct goodix_ts_core *core_data =
  1601. dev_get_drvdata(dev);
  1602. return goodix_ts_resume(core_data);
  1603. }
  1604. #endif
  1605. #endif
  1606. /**
  1607. * goodix_generic_noti_callback - generic notifier callback
  1608. * for goodix touch notification event.
  1609. */
  1610. static int goodix_generic_noti_callback(struct notifier_block *self,
  1611. unsigned long action, void *data)
  1612. {
  1613. struct goodix_ts_core *cd = container_of(self,
  1614. struct goodix_ts_core, ts_notifier);
  1615. const struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  1616. if (cd->init_stage < CORE_INIT_STAGE2)
  1617. return 0;
  1618. ts_info("notify event type 0x%x", (unsigned int)action);
  1619. switch (action) {
  1620. case NOTIFY_FWUPDATE_START:
  1621. hw_ops->irq_enable(cd, 0);
  1622. break;
  1623. case NOTIFY_FWUPDATE_SUCCESS:
  1624. case NOTIFY_FWUPDATE_FAILED:
  1625. if (hw_ops->read_version(cd, &cd->fw_version))
  1626. ts_info("failed read fw version info[ignore]");
  1627. hw_ops->irq_enable(cd, 1);
  1628. break;
  1629. default:
  1630. break;
  1631. }
  1632. return 0;
  1633. }
  1634. int goodix_ts_stage2_init(struct goodix_ts_core *cd)
  1635. {
  1636. int ret;
  1637. /* alloc/config/register input device */
  1638. ret = goodix_ts_input_dev_config(cd);
  1639. if (ret < 0) {
  1640. ts_err("failed set input device");
  1641. return ret;
  1642. }
  1643. if (cd->board_data.pen_enable) {
  1644. ret = goodix_ts_pen_dev_config(cd);
  1645. if (ret < 0) {
  1646. ts_err("failed set pen device");
  1647. goto err_finger;
  1648. }
  1649. }
  1650. /* request irq line */
  1651. ret = goodix_ts_irq_setup(cd);
  1652. if (ret < 0) {
  1653. ts_info("failed set irq");
  1654. goto exit;
  1655. }
  1656. ts_info("success register irq");
  1657. #ifdef CONFIG_FB
  1658. cd->fb_notifier.notifier_call = goodix_ts_fb_notifier_callback;
  1659. if (fb_register_client(&cd->fb_notifier))
  1660. ts_err("Failed to register fb notifier client:%d", ret);
  1661. #endif
  1662. /* create sysfs files */
  1663. goodix_ts_sysfs_init(cd);
  1664. /* create procfs files */
  1665. goodix_ts_procfs_init(cd);
  1666. /* esd protector */
  1667. goodix_ts_esd_init(cd);
  1668. /* gesture init */
  1669. gesture_module_init();
  1670. /* inspect init */
  1671. inspect_module_init();
  1672. return 0;
  1673. exit:
  1674. goodix_ts_pen_dev_remove(cd);
  1675. err_finger:
  1676. goodix_ts_input_dev_remove(cd);
  1677. return ret;
  1678. }
  1679. /* try send the config specified with type */
  1680. static int goodix_send_ic_config(struct goodix_ts_core *cd, int type)
  1681. {
  1682. u32 config_id;
  1683. struct goodix_ic_config *cfg;
  1684. if (type >= GOODIX_MAX_CONFIG_GROUP) {
  1685. ts_err("unsupproted config type %d", type);
  1686. return -EINVAL;
  1687. }
  1688. cfg = cd->ic_configs[type];
  1689. if (!cfg || cfg->len <= 0) {
  1690. ts_info("no valid normal config found");
  1691. return -EINVAL;
  1692. }
  1693. config_id = goodix_get_file_config_id(cfg->data);
  1694. if (cd->ic_info.version.config_id == config_id) {
  1695. ts_info("config id is equal 0x%x, skiped", config_id);
  1696. return 0;
  1697. }
  1698. ts_info("try send config, id=0x%x", config_id);
  1699. return cd->hw_ops->send_config(cd, cfg->data, cfg->len);
  1700. }
  1701. /**
  1702. * goodix_later_init_thread - init IC fw and config
  1703. * @data: point to goodix_ts_core
  1704. *
  1705. * This function respond for get fw version and try upgrade fw and config.
  1706. * Note: when init encounter error, need release all resource allocated here.
  1707. */
  1708. static int goodix_later_init_thread(void *data)
  1709. {
  1710. int ret, i;
  1711. int update_flag = UPDATE_MODE_BLOCK | UPDATE_MODE_SRC_REQUEST;
  1712. struct goodix_ts_core *cd = data;
  1713. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  1714. /* step 1: read version */
  1715. ret = cd->hw_ops->read_version(cd, &cd->fw_version);
  1716. if (ret < 0) {
  1717. ts_err("failed to get version info, try to upgrade");
  1718. update_flag |= UPDATE_MODE_FORCE;
  1719. goto upgrade;
  1720. }
  1721. /* step 2: get config data from config bin */
  1722. ret = goodix_get_config_proc(cd);
  1723. if (ret)
  1724. ts_info("no valid ic config found");
  1725. else
  1726. ts_info("success get valid ic config");
  1727. upgrade:
  1728. /* step 3: init fw struct add try do fw upgrade */
  1729. ret = goodix_fw_update_init(cd);
  1730. if (ret) {
  1731. ts_err("failed init fw update module");
  1732. goto err_out;
  1733. }
  1734. ts_info("update flag: 0x%X", update_flag);
  1735. ret = goodix_do_fw_update(cd->ic_configs[CONFIG_TYPE_NORMAL], update_flag);
  1736. if (ret)
  1737. ts_err("failed do fw update");
  1738. /* step 4: get fw version and ic_info
  1739. * at this step we believe that the ic is in normal mode,
  1740. * if the version info is invalid there must have some
  1741. * problem we cann't cover so exit init directly.
  1742. */
  1743. ret = hw_ops->read_version(cd, &cd->fw_version);
  1744. if (ret) {
  1745. ts_err("invalid fw version, abort");
  1746. goto uninit_fw;
  1747. }
  1748. ret = hw_ops->get_ic_info(cd, &cd->ic_info);
  1749. if (ret) {
  1750. ts_err("invalid ic info, abort");
  1751. goto uninit_fw;
  1752. }
  1753. /* the recomend way to update ic config is throuth ISP,
  1754. * if not we will send config with interactive mode
  1755. */
  1756. goodix_send_ic_config(cd, CONFIG_TYPE_NORMAL);
  1757. /* init other resources */
  1758. ret = goodix_ts_stage2_init(cd);
  1759. if (ret) {
  1760. ts_err("stage2 init failed");
  1761. goto uninit_fw;
  1762. }
  1763. cd->init_stage = CORE_INIT_STAGE2;
  1764. return 0;
  1765. uninit_fw:
  1766. goodix_fw_update_uninit();
  1767. err_out:
  1768. ts_err("stage2 init failed");
  1769. cd->init_stage = CORE_INIT_FAIL;
  1770. for (i = 0; i < GOODIX_MAX_CONFIG_GROUP; i++) {
  1771. if (cd->ic_configs[i])
  1772. kfree(cd->ic_configs[i]);
  1773. cd->ic_configs[i] = NULL;
  1774. }
  1775. return ret;
  1776. }
  1777. static int goodix_start_later_init(struct goodix_ts_core *ts_core)
  1778. {
  1779. struct task_struct *init_thrd;
  1780. /* create and run update thread */
  1781. init_thrd = kthread_run(goodix_later_init_thread,
  1782. ts_core, "goodix_init_thread");
  1783. if (IS_ERR_OR_NULL(init_thrd)) {
  1784. ts_err("Failed to create update thread:%ld",
  1785. PTR_ERR(init_thrd));
  1786. return -EFAULT;
  1787. }
  1788. return 0;
  1789. }
  1790. /**
  1791. * goodix_ts_probe - called by kernel when Goodix touch
  1792. * platform driver is added.
  1793. */
  1794. static int goodix_ts_probe(struct platform_device *pdev)
  1795. {
  1796. struct goodix_ts_core *core_data = NULL;
  1797. struct goodix_bus_interface *bus_interface;
  1798. int ret;
  1799. ts_info("goodix_ts_probe IN");
  1800. bus_interface = pdev->dev.platform_data;
  1801. if (!bus_interface) {
  1802. ts_err("Invalid touch device");
  1803. core_module_prob_sate = CORE_MODULE_PROB_FAILED;
  1804. return -ENODEV;
  1805. }
  1806. core_data = devm_kzalloc(&pdev->dev,
  1807. sizeof(struct goodix_ts_core), GFP_KERNEL);
  1808. if (!core_data) {
  1809. ts_err("Failed to allocate memory for core data");
  1810. core_module_prob_sate = CORE_MODULE_PROB_FAILED;
  1811. return -ENOMEM;
  1812. }
  1813. if (IS_ENABLED(CONFIG_OF) && bus_interface->dev->of_node) {
  1814. /* parse devicetree property */
  1815. ret = goodix_parse_dt(bus_interface->dev->of_node,
  1816. &core_data->board_data);
  1817. if (ret) {
  1818. ts_err("failed parse device info form dts, %d", ret);
  1819. return -EINVAL;
  1820. }
  1821. } else {
  1822. ts_err("no valid device tree node found");
  1823. return -ENODEV;
  1824. }
  1825. core_data->hw_ops = goodix_get_hw_ops();
  1826. if (!core_data->hw_ops) {
  1827. ts_err("hw ops is NULL");
  1828. core_module_prob_sate = CORE_MODULE_PROB_FAILED;
  1829. return -EINVAL;
  1830. }
  1831. goodix_core_module_init();
  1832. /* touch core layer is a platform driver */
  1833. core_data->pdev = pdev;
  1834. core_data->bus = bus_interface;
  1835. platform_set_drvdata(pdev, core_data);
  1836. /* get GPIO resource */
  1837. ret = goodix_ts_gpio_setup(core_data);
  1838. if (ret) {
  1839. ts_err("failed init gpio");
  1840. goto err_out;
  1841. }
  1842. ret = goodix_ts_power_init(core_data);
  1843. if (ret) {
  1844. ts_err("failed init power");
  1845. goto err_out;
  1846. }
  1847. ret = goodix_ts_power_on(core_data);
  1848. if (ret) {
  1849. ts_err("failed power on");
  1850. goto err_out;
  1851. }
  1852. /* generic notifier callback */
  1853. core_data->ts_notifier.notifier_call = goodix_generic_noti_callback;
  1854. goodix_ts_register_notifier(&core_data->ts_notifier);
  1855. /* debug node init */
  1856. goodix_tools_init();
  1857. core_data->init_stage = CORE_INIT_STAGE1;
  1858. goodix_modules.core_data = core_data;
  1859. core_module_prob_sate = CORE_MODULE_PROB_SUCCESS;
  1860. /* Try start a thread to get config-bin info */
  1861. goodix_start_later_init(core_data);
  1862. ts_info("goodix_ts_core probe success");
  1863. return 0;
  1864. err_out:
  1865. core_data->init_stage = CORE_INIT_FAIL;
  1866. core_module_prob_sate = CORE_MODULE_PROB_FAILED;
  1867. ts_err("goodix_ts_core failed, ret:%d", ret);
  1868. return ret;
  1869. }
  1870. static int goodix_ts_remove(struct platform_device *pdev)
  1871. {
  1872. struct goodix_ts_core *core_data = platform_get_drvdata(pdev);
  1873. struct goodix_ts_hw_ops *hw_ops = core_data->hw_ops;
  1874. struct goodix_ts_esd *ts_esd = &core_data->ts_esd;
  1875. goodix_ts_unregister_notifier(&core_data->ts_notifier);
  1876. goodix_tools_exit();
  1877. if (core_data->init_stage >= CORE_INIT_STAGE2) {
  1878. gesture_module_exit();
  1879. inspect_module_exit();
  1880. hw_ops->irq_enable(core_data, false);
  1881. #ifdef CONFIG_FB
  1882. fb_unregister_client(&core_data->fb_notifier);
  1883. #endif
  1884. core_module_prob_sate = CORE_MODULE_REMOVED;
  1885. if (atomic_read(&core_data->ts_esd.esd_on))
  1886. goodix_ts_esd_off(core_data);
  1887. goodix_ts_unregister_notifier(&ts_esd->esd_notifier);
  1888. goodix_fw_update_uninit();
  1889. goodix_ts_input_dev_remove(core_data);
  1890. goodix_ts_pen_dev_remove(core_data);
  1891. goodix_ts_sysfs_exit(core_data);
  1892. goodix_ts_procfs_exit(core_data);
  1893. goodix_ts_power_off(core_data);
  1894. }
  1895. return 0;
  1896. }
  1897. #ifdef CONFIG_PM
  1898. static const struct dev_pm_ops dev_pm_ops = {
  1899. #if !defined(CONFIG_FB) && !defined(CONFIG_HAS_EARLYSUSPEND)
  1900. .suspend = goodix_ts_pm_suspend,
  1901. .resume = goodix_ts_pm_resume,
  1902. #endif
  1903. };
  1904. #endif
  1905. static const struct platform_device_id ts_core_ids[] = {
  1906. {.name = GOODIX_CORE_DRIVER_NAME},
  1907. {}
  1908. };
  1909. MODULE_DEVICE_TABLE(platform, ts_core_ids);
  1910. static struct platform_driver goodix_ts_driver = {
  1911. .driver = {
  1912. .name = GOODIX_CORE_DRIVER_NAME,
  1913. .owner = THIS_MODULE,
  1914. #ifdef CONFIG_PM
  1915. .pm = &dev_pm_ops,
  1916. #endif
  1917. },
  1918. .probe = goodix_ts_probe,
  1919. .remove = goodix_ts_remove,
  1920. .id_table = ts_core_ids,
  1921. };
  1922. static int __init goodix_ts_core_init(void)
  1923. {
  1924. int ret;
  1925. ts_info("Core layer init:%s", GOODIX_DRIVER_VERSION);
  1926. #ifdef CONFIG_TOUCHSCREEN_GOODIX_BRL_SPI
  1927. ret = goodix_spi_bus_init();
  1928. #else
  1929. ret = goodix_i2c_bus_init();
  1930. #endif
  1931. if (ret) {
  1932. ts_err("failed add bus driver");
  1933. return ret;
  1934. }
  1935. return platform_driver_register(&goodix_ts_driver);
  1936. }
  1937. static void __exit goodix_ts_core_exit(void)
  1938. {
  1939. ts_info("Core layer exit");
  1940. platform_driver_unregister(&goodix_ts_driver);
  1941. #ifdef CONFIG_TOUCHSCREEN_GOODIX_BRL_SPI
  1942. goodix_spi_bus_exit();
  1943. #else
  1944. goodix_i2c_bus_exit();
  1945. #endif
  1946. }
  1947. late_initcall(goodix_ts_core_init);
  1948. module_exit(goodix_ts_core_exit);
  1949. MODULE_DESCRIPTION("Goodix Touchscreen Core Module");
  1950. MODULE_AUTHOR("Goodix, Inc.");
  1951. MODULE_LICENSE("GPL v2");