qts_core.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "%s: " fmt, __func__
  6. #include <linux/vmalloc.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/delay.h>
  9. #include <linux/input.h>
  10. #include <linux/input/mt.h>
  11. #include <linux/of_gpio.h>
  12. #include <linux/of_graph.h>
  13. #include <linux/of_device.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/sort.h>
  16. #include <linux/atomic.h>
  17. #include <linux/pinctrl/qcom-pinctrl.h>
  18. #include <linux/pm.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/component.h>
  21. #include <linux/sched.h>
  22. #include <linux/version.h>
  23. #include <linux/types.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/wait.h>
  27. #include <linux/time.h>
  28. #include "qts_core.h"
  29. static struct qts_data_entries *qts_data_entries;
  30. struct drm_panel *active_panel;
  31. static void qts_trusted_touch_abort_handler(struct qts_data *qts_data, int error);
  32. static struct gh_acl_desc *qts_vm_get_acl(enum gh_vm_names vm_name)
  33. {
  34. struct gh_acl_desc *acl_desc;
  35. gh_vmid_t vmid;
  36. #if (KERNEL_VERSION(6, 1, 0) <= LINUX_VERSION_CODE)
  37. ghd_rm_get_vmid(vm_name, &vmid);
  38. #else
  39. gh_rm_get_vmid(vm_name, &vmid);
  40. #endif
  41. acl_desc = kzalloc(offsetof(struct gh_acl_desc, acl_entries[1]),
  42. GFP_KERNEL);
  43. if (!acl_desc)
  44. return ERR_PTR(ENOMEM);
  45. acl_desc->n_acl_entries = 1;
  46. acl_desc->acl_entries[0].vmid = vmid;
  47. acl_desc->acl_entries[0].perms = GH_RM_ACL_R | GH_RM_ACL_W;
  48. return acl_desc;
  49. }
  50. static struct gh_sgl_desc *qts_vm_get_sgl(struct trusted_touch_vm_info *vm_info)
  51. {
  52. struct gh_sgl_desc *sgl_desc;
  53. int i;
  54. sgl_desc = kzalloc(offsetof(struct gh_sgl_desc,
  55. sgl_entries[vm_info->iomem_list_size]), GFP_KERNEL);
  56. if (!sgl_desc)
  57. return ERR_PTR(ENOMEM);
  58. sgl_desc->n_sgl_entries = vm_info->iomem_list_size;
  59. for (i = 0; i < vm_info->iomem_list_size; i++) {
  60. sgl_desc->sgl_entries[i].ipa_base = vm_info->iomem_bases[i];
  61. sgl_desc->sgl_entries[i].size = vm_info->iomem_sizes[i];
  62. }
  63. return sgl_desc;
  64. }
  65. static int qts_populate_vm_info_iomem(struct qts_data *qts_data)
  66. {
  67. int i, gpio, rc = 0;
  68. int num_regs, num_sizes, num_gpios, list_size;
  69. struct resource res;
  70. struct device_node *np = qts_data->dev->of_node;
  71. struct trusted_touch_vm_info *vm_info = qts_data->vm_info;
  72. num_regs = of_property_count_u32_elems(np, "qts,trusted-touch-io-bases");
  73. if (num_regs < 0) {
  74. pr_err("Invalid number of IO regions specified\n");
  75. return -EINVAL;
  76. }
  77. num_sizes = of_property_count_u32_elems(np, "qts,trusted-touch-io-sizes");
  78. if (num_sizes < 0) {
  79. pr_err("Invalid number of IO regions specified\n");
  80. return -EINVAL;
  81. }
  82. if (num_regs != num_sizes) {
  83. pr_err("IO bases and sizes array lengths mismatch\n");
  84. return -EINVAL;
  85. }
  86. num_gpios = of_gpio_named_count(np, "qts,trusted-touch-vm-gpio-list");
  87. if (num_gpios < 0) {
  88. pr_warn("Ignoring invalid trusted gpio list: %d\n", num_gpios);
  89. num_gpios = 0;
  90. }
  91. list_size = num_regs + num_gpios;
  92. vm_info->iomem_list_size = list_size;
  93. vm_info->iomem_bases = devm_kcalloc(qts_data->dev, list_size, sizeof(*vm_info->iomem_bases),
  94. GFP_KERNEL);
  95. if (!vm_info->iomem_bases)
  96. return -ENOMEM;
  97. vm_info->iomem_sizes = devm_kcalloc(qts_data->dev, list_size, sizeof(*vm_info->iomem_sizes),
  98. GFP_KERNEL);
  99. if (!vm_info->iomem_sizes)
  100. return -ENOMEM;
  101. for (i = 0; i < num_gpios; ++i) {
  102. gpio = of_get_named_gpio(np, "qts,trusted-touch-vm-gpio-list", i);
  103. if (gpio < 0 || !gpio_is_valid(gpio)) {
  104. pr_err("Invalid gpio %d at position %d\n", gpio, i);
  105. return gpio;
  106. }
  107. if (!msm_gpio_get_pin_address(gpio, &res)) {
  108. pr_err("Failed to retrieve gpio-%d resource\n", gpio);
  109. return -ENODATA;
  110. }
  111. vm_info->iomem_bases[i] = res.start;
  112. vm_info->iomem_sizes[i] = resource_size(&res);
  113. }
  114. rc = of_property_read_u32_array(np, "qts,trusted-touch-io-bases",
  115. &vm_info->iomem_bases[i], list_size - i);
  116. if (rc) {
  117. pr_err("Failed to read trusted touch io bases:%d\n", rc);
  118. return rc;
  119. }
  120. rc = of_property_read_u32_array(np, "qts,trusted-touch-io-sizes",
  121. &vm_info->iomem_sizes[i], list_size - i);
  122. if (rc) {
  123. pr_err("Failed to read trusted touch io sizes:%d\n", rc);
  124. return rc;
  125. }
  126. return 0;
  127. }
  128. static int qts_populate_vm_info(struct qts_data *qts_data)
  129. {
  130. int rc;
  131. struct trusted_touch_vm_info *vm_info;
  132. struct device_node *np = qts_data->dev->of_node;
  133. vm_info = devm_kzalloc(qts_data->dev, sizeof(struct trusted_touch_vm_info), GFP_KERNEL);
  134. if (!vm_info)
  135. return -ENOMEM;
  136. qts_data->vm_info = vm_info;
  137. vm_info->vm_name = GH_TRUSTED_VM;
  138. rc = of_property_read_u32(np, "qts,trusted-touch-spi-irq", &vm_info->hw_irq);
  139. if (rc) {
  140. pr_err("Failed to read trusted touch SPI irq:%d\n", rc);
  141. return rc;
  142. }
  143. rc = qts_populate_vm_info_iomem(qts_data);
  144. if (rc) {
  145. pr_err("Failed to read trusted touch mmio ranges:%d\n", rc);
  146. return rc;
  147. }
  148. rc = of_property_read_string(np, "qts,trusted-touch-type",
  149. &vm_info->trusted_touch_type);
  150. if (rc) {
  151. pr_warn("No trusted touch type selection mode\n");
  152. vm_info->mem_tag = GH_MEM_NOTIFIER_TAG_TOUCH_PRIMARY;
  153. vm_info->irq_label = GH_IRQ_LABEL_TRUSTED_TOUCH_PRIMARY;
  154. rc = 0;
  155. } else if (!strcmp(vm_info->trusted_touch_type, "primary")) {
  156. vm_info->mem_tag = GH_MEM_NOTIFIER_TAG_TOUCH_PRIMARY;
  157. vm_info->irq_label = GH_IRQ_LABEL_TRUSTED_TOUCH_PRIMARY;
  158. } else if (!strcmp(vm_info->trusted_touch_type, "secondary")) {
  159. vm_info->mem_tag = GH_MEM_NOTIFIER_TAG_TOUCH_SECONDARY;
  160. vm_info->irq_label = GH_IRQ_LABEL_TRUSTED_TOUCH_SECONDARY;
  161. }
  162. return 0;
  163. }
  164. static void qts_destroy_vm_info(struct qts_data *qts_data)
  165. {
  166. kfree(qts_data->vm_info->iomem_sizes);
  167. kfree(qts_data->vm_info->iomem_bases);
  168. kfree(qts_data->vm_info);
  169. }
  170. static void qts_vm_deinit(struct qts_data *qts_data)
  171. {
  172. if (qts_data->vm_info->mem_cookie)
  173. gh_mem_notifier_unregister(qts_data->vm_info->mem_cookie);
  174. qts_destroy_vm_info(qts_data);
  175. }
  176. static int qts_trusted_touch_get_vm_state(struct qts_data *qts_data)
  177. {
  178. return atomic_read(&qts_data->vm_info->vm_state);
  179. }
  180. static void qts_trusted_touch_set_vm_state(struct qts_data *qts_data,
  181. int state)
  182. {
  183. pr_debug("state %d\n", state);
  184. atomic_set(&qts_data->vm_info->vm_state, state);
  185. }
  186. #ifdef CONFIG_ARCH_QTI_VM
  187. static int qts_vm_mem_release(struct qts_data *qts_data);
  188. static void qts_trusted_touch_tvm_vm_mode_disable(struct qts_data *qts_data);
  189. static void qts_trusted_touch_abort_tvm(struct qts_data *qts_data);
  190. static void qts_trusted_touch_event_notify(struct qts_data *qts_data, int event);
  191. static void qts_irq_enable(struct qts_data *qts_data, bool en)
  192. {
  193. if (en) {
  194. if (qts_data->irq_disabled) {
  195. pr_debug("qts irq enable\n");
  196. enable_irq(qts_data->irq);
  197. qts_data->irq_disabled = false;
  198. }
  199. } else {
  200. if (!qts_data->irq_disabled) {
  201. pr_debug("qts irq disable\n");
  202. disable_irq_nosync(qts_data->irq);
  203. qts_data->irq_disabled = true;
  204. }
  205. }
  206. }
  207. static irqreturn_t qts_irq_handler(int irq, void *data)
  208. {
  209. struct qts_data *qts_data = data;
  210. if (!mutex_trylock(&qts_data->transition_lock))
  211. return IRQ_HANDLED;
  212. qts_data->vendor_ops.irq_handler(irq, qts_data->vendor_data);
  213. mutex_unlock(&qts_data->transition_lock);
  214. return IRQ_HANDLED;
  215. }
  216. static int qts_irq_registration(struct qts_data *qts_data)
  217. {
  218. int ret = 0;
  219. qts_data->irq_gpio_flags = IRQF_TRIGGER_RISING;
  220. pr_debug("irq:%d, flag:%x\n", qts_data->irq, qts_data->irq_gpio_flags);
  221. ret = request_threaded_irq(qts_data->irq, NULL, qts_irq_handler,
  222. qts_data->irq_gpio_flags | IRQF_ONESHOT,
  223. QTS_NAME, qts_data);
  224. if (ret != 0)
  225. pr_err("request_threaded_irq failed\n");
  226. if (ret == 0)
  227. qts_irq_enable(qts_data, false);
  228. return ret;
  229. }
  230. void qts_trusted_touch_tvm_i2c_failure_report(struct qts_data *qts_data)
  231. {
  232. pr_warn("initiating trusted touch abort due to i2c failure\n");
  233. qts_trusted_touch_abort_handler(qts_data, TRUSTED_TOUCH_EVENT_I2C_FAILURE);
  234. }
  235. static void qts_trusted_touch_reset_gpio_toggle(struct qts_data *qts_data)
  236. {
  237. void __iomem *base;
  238. if (qts_data->bus_type != QTS_BUS_TYPE_I2C)
  239. return;
  240. base = ioremap(TOUCH_RESET_GPIO_BASE, TOUCH_RESET_GPIO_SIZE);
  241. writel_relaxed(0x1, base + TOUCH_RESET_GPIO_OFFSET);
  242. /* wait until toggle to finish*/
  243. wmb();
  244. writel_relaxed(0x0, base + TOUCH_RESET_GPIO_OFFSET);
  245. /* wait until toggle to finish*/
  246. wmb();
  247. iounmap(base);
  248. }
  249. static void qts_trusted_touch_intr_gpio_toggle(struct qts_data *qts_data,
  250. bool enable)
  251. {
  252. void __iomem *base;
  253. u32 val;
  254. if (qts_data->bus_type != QTS_BUS_TYPE_I2C)
  255. return;
  256. base = ioremap(TOUCH_INTR_GPIO_BASE, TOUCH_INTR_GPIO_SIZE);
  257. val = readl_relaxed(base + TOUCH_RESET_GPIO_OFFSET);
  258. if (enable) {
  259. val |= BIT(0);
  260. writel_relaxed(val, base + TOUCH_INTR_GPIO_OFFSET);
  261. /* wait until toggle to finish*/
  262. wmb();
  263. } else {
  264. val &= ~BIT(0);
  265. writel_relaxed(val, base + TOUCH_INTR_GPIO_OFFSET);
  266. /* wait until toggle to finish*/
  267. wmb();
  268. }
  269. iounmap(base);
  270. }
  271. static int qts_sgl_cmp(const void *a, const void *b)
  272. {
  273. struct gh_sgl_entry *left = (struct gh_sgl_entry *)a;
  274. struct gh_sgl_entry *right = (struct gh_sgl_entry *)b;
  275. return (left->ipa_base - right->ipa_base);
  276. }
  277. static int qts_vm_compare_sgl_desc(struct gh_sgl_desc *expected,
  278. struct gh_sgl_desc *received)
  279. {
  280. int idx;
  281. if (expected->n_sgl_entries != received->n_sgl_entries)
  282. return -E2BIG;
  283. sort(received->sgl_entries, received->n_sgl_entries,
  284. sizeof(received->sgl_entries[0]), qts_sgl_cmp, NULL);
  285. sort(expected->sgl_entries, expected->n_sgl_entries,
  286. sizeof(expected->sgl_entries[0]), qts_sgl_cmp, NULL);
  287. for (idx = 0; idx < expected->n_sgl_entries; idx++) {
  288. struct gh_sgl_entry *left = &expected->sgl_entries[idx];
  289. struct gh_sgl_entry *right = &received->sgl_entries[idx];
  290. if ((left->ipa_base != right->ipa_base) ||
  291. (left->size != right->size)) {
  292. pr_err("sgl mismatch: left_base:%d right base:%d left size:%d right size:%d\n",
  293. left->ipa_base, right->ipa_base, left->size, right->size);
  294. return -EINVAL;
  295. }
  296. }
  297. return 0;
  298. }
  299. static int qts_vm_handle_vm_hardware(struct qts_data *qts_data)
  300. {
  301. int rc = 0;
  302. if (atomic_read(&qts_data->delayed_tvm_probe_pending)) {
  303. rc = qts_irq_registration(qts_data);
  304. if (rc) {
  305. pr_err("irq registration failure on TVM!\n");
  306. return rc;
  307. }
  308. atomic_set(&qts_data->delayed_tvm_probe_pending, 0);
  309. }
  310. qts_irq_enable(qts_data, true);
  311. qts_trusted_touch_set_vm_state(qts_data, TVM_INTERRUPT_ENABLED);
  312. return rc;
  313. }
  314. static void qts_trusted_touch_tvm_vm_mode_enable(struct qts_data *qts_data)
  315. {
  316. struct gh_sgl_desc *sgl_desc, *expected_sgl_desc;
  317. struct gh_acl_desc *acl_desc;
  318. struct irq_data *irq_data;
  319. int rc = 0;
  320. int irq = 0;
  321. mutex_lock(&qts_data->transition_lock);
  322. if (qts_trusted_touch_get_vm_state(qts_data) != TVM_ALL_RESOURCES_LENT_NOTIFIED) {
  323. pr_info("All lend notifications not received\n");
  324. qts_trusted_touch_event_notify(qts_data,
  325. TRUSTED_TOUCH_EVENT_NOTIFICATIONS_PENDING);
  326. mutex_unlock(&qts_data->transition_lock);
  327. return;
  328. }
  329. if (qts_data->vendor_ops.pre_le_tui_enable)
  330. qts_data->vendor_ops.pre_le_tui_enable(qts_data->vendor_data);
  331. acl_desc = qts_vm_get_acl(GH_TRUSTED_VM);
  332. if (IS_ERR(acl_desc)) {
  333. pr_err("failed to populated acl data:rc=%d\n", PTR_ERR(acl_desc));
  334. goto accept_fail;
  335. }
  336. sgl_desc = gh_rm_mem_accept(qts_data->vm_info->vm_mem_handle,
  337. GH_RM_MEM_TYPE_IO,
  338. GH_RM_TRANS_TYPE_LEND,
  339. GH_RM_MEM_ACCEPT_VALIDATE_ACL_ATTRS |
  340. GH_RM_MEM_ACCEPT_VALIDATE_LABEL |
  341. GH_RM_MEM_ACCEPT_DONE, TRUSTED_TOUCH_MEM_LABEL,
  342. acl_desc, NULL, NULL, 0);
  343. if (IS_ERR_OR_NULL(sgl_desc)) {
  344. pr_err("failed to do mem accept :rc=%d\n", PTR_ERR(sgl_desc));
  345. goto acl_fail;
  346. }
  347. qts_trusted_touch_set_vm_state(qts_data, TVM_IOMEM_ACCEPTED);
  348. /* Initiate session on tvm */
  349. if (qts_data->bus_type == QTS_BUS_TYPE_I2C)
  350. rc = pm_runtime_get_sync(qts_data->client->adapter->dev.parent);
  351. else
  352. rc = pm_runtime_get_sync(qts_data->spi->master->dev.parent);
  353. if (rc < 0) {
  354. pr_err("failed to get sync rc:%d\n", rc);
  355. goto acl_fail;
  356. }
  357. qts_trusted_touch_set_vm_state(qts_data, TVM_I2C_SESSION_ACQUIRED);
  358. expected_sgl_desc = qts_vm_get_sgl(qts_data->vm_info);
  359. if (qts_vm_compare_sgl_desc(expected_sgl_desc, sgl_desc)) {
  360. pr_err("IO sg list does not match\n");
  361. goto sgl_cmp_fail;
  362. }
  363. kfree(expected_sgl_desc);
  364. kfree(acl_desc);
  365. irq = gh_irq_accept(qts_data->vm_info->irq_label, -1, IRQ_TYPE_EDGE_RISING);
  366. qts_trusted_touch_intr_gpio_toggle(qts_data, false);
  367. if (irq < 0) {
  368. pr_err("failed to accept irq\n");
  369. goto accept_fail;
  370. }
  371. qts_trusted_touch_set_vm_state(qts_data, TVM_IRQ_ACCEPTED);
  372. irq_data = irq_get_irq_data(irq);
  373. if (!irq_data) {
  374. pr_err("Invalid irq data for trusted touch\n");
  375. goto accept_fail;
  376. }
  377. if (!irq_data->hwirq) {
  378. pr_err("Invalid irq in irq data\n");
  379. goto accept_fail;
  380. }
  381. if (irq_data->hwirq != qts_data->vm_info->hw_irq) {
  382. pr_err("Invalid irq lent\n");
  383. goto accept_fail;
  384. }
  385. pr_debug("irq:returned from accept:%d\n", irq);
  386. qts_data->irq = irq;
  387. rc = qts_vm_handle_vm_hardware(qts_data);
  388. if (rc) {
  389. pr_err("Delayed probe failure on TVM!\n");
  390. goto accept_fail;
  391. }
  392. atomic_set(&qts_data->trusted_touch_enabled, 1);
  393. if (qts_data->vendor_ops.post_le_tui_enable)
  394. qts_data->vendor_ops.post_le_tui_enable(qts_data->vendor_data);
  395. pr_info("Irq, iomem are accepted and trusted touch enabled\n");
  396. mutex_unlock(&qts_data->transition_lock);
  397. return;
  398. sgl_cmp_fail:
  399. kfree(expected_sgl_desc);
  400. acl_fail:
  401. kfree(acl_desc);
  402. accept_fail:
  403. qts_trusted_touch_abort_handler(qts_data,
  404. TRUSTED_TOUCH_EVENT_ACCEPT_FAILURE);
  405. mutex_unlock(&qts_data->transition_lock);
  406. }
  407. static void qts_vm_irq_on_lend_callback(void *data,
  408. unsigned long notif_type,
  409. enum gh_irq_label label)
  410. {
  411. struct qts_data *qts_data = data;
  412. pr_debug("received irq lend request for label:%d\n", label);
  413. if (qts_trusted_touch_get_vm_state(qts_data) == TVM_IOMEM_LENT_NOTIFIED)
  414. qts_trusted_touch_set_vm_state(qts_data, TVM_ALL_RESOURCES_LENT_NOTIFIED);
  415. else
  416. qts_trusted_touch_set_vm_state(qts_data, TVM_IRQ_LENT_NOTIFIED);
  417. }
  418. static void qts_vm_mem_on_lend_handler(enum gh_mem_notifier_tag tag,
  419. unsigned long notif_type, void *entry_data, void *notif_msg)
  420. {
  421. struct gh_rm_notif_mem_shared_payload *payload;
  422. struct trusted_touch_vm_info *vm_info;
  423. struct qts_data *qts_data;
  424. qts_data = (struct qts_data *)entry_data;
  425. vm_info = qts_data->vm_info;
  426. if (!vm_info) {
  427. pr_err("Invalid vm_info\n");
  428. return;
  429. }
  430. if (notif_type != GH_RM_NOTIF_MEM_SHARED ||
  431. tag != vm_info->mem_tag) {
  432. pr_err("Invalid command passed from rm\n");
  433. return;
  434. }
  435. if (!entry_data || !notif_msg) {
  436. pr_err("Invalid entry data passed from rm\n");
  437. return;
  438. }
  439. payload = (struct gh_rm_notif_mem_shared_payload *)notif_msg;
  440. if (payload->trans_type != GH_RM_TRANS_TYPE_LEND ||
  441. payload->label != TRUSTED_TOUCH_MEM_LABEL) {
  442. pr_err("Invalid label or transaction type\n");
  443. return;
  444. }
  445. vm_info->vm_mem_handle = payload->mem_handle;
  446. pr_debug("received mem lend request with handle:%d\n", vm_info->vm_mem_handle);
  447. if (qts_trusted_touch_get_vm_state(qts_data) == TVM_IRQ_LENT_NOTIFIED)
  448. qts_trusted_touch_set_vm_state(qts_data, TVM_ALL_RESOURCES_LENT_NOTIFIED);
  449. else
  450. qts_trusted_touch_set_vm_state(qts_data, TVM_IOMEM_LENT_NOTIFIED);
  451. }
  452. static int qts_vm_mem_release(struct qts_data *qts_data)
  453. {
  454. int rc = 0;
  455. if (!qts_data->vm_info->vm_mem_handle) {
  456. pr_err("Invalid memory handle\n");
  457. return -EINVAL;
  458. }
  459. rc = gh_rm_mem_release(qts_data->vm_info->vm_mem_handle, 0);
  460. if (rc)
  461. pr_err("VM mem release failed: rc=%d\n", rc);
  462. rc = gh_rm_mem_notify(qts_data->vm_info->vm_mem_handle,
  463. GH_RM_MEM_NOTIFY_OWNER_RELEASED,
  464. qts_data->vm_info->mem_tag, 0);
  465. if (rc)
  466. pr_err("Failed to notify mem release to PVM: rc=%d\n", rc);
  467. pr_debug("vm mem release success\n");
  468. qts_data->vm_info->vm_mem_handle = 0;
  469. return rc;
  470. }
  471. static void qts_trusted_touch_tvm_vm_mode_disable(struct qts_data *qts_data)
  472. {
  473. int rc = 0;
  474. mutex_lock(&qts_data->transition_lock);
  475. if (atomic_read(&qts_data->trusted_touch_abort_status)) {
  476. qts_trusted_touch_abort_tvm(qts_data);
  477. mutex_unlock(&qts_data->transition_lock);
  478. return;
  479. }
  480. if (qts_data->vendor_ops.pre_le_tui_disable)
  481. qts_data->vendor_ops.pre_le_tui_disable(qts_data->vendor_data);
  482. qts_irq_enable(qts_data, false);
  483. qts_trusted_touch_set_vm_state(qts_data, TVM_INTERRUPT_DISABLED);
  484. rc = gh_irq_release(qts_data->vm_info->irq_label);
  485. if (rc) {
  486. pr_err("Failed to release irq rc:%d\n", rc);
  487. goto error;
  488. } else {
  489. qts_trusted_touch_set_vm_state(qts_data, TVM_IRQ_RELEASED);
  490. }
  491. rc = gh_irq_release_notify(qts_data->vm_info->irq_label);
  492. if (rc)
  493. pr_err("Failed to notify release irq rc:%d\n", rc);
  494. pr_debug("vm irq release success\n");
  495. if (qts_data->bus_type == QTS_BUS_TYPE_I2C)
  496. pm_runtime_put_sync(qts_data->client->adapter->dev.parent);
  497. else
  498. pm_runtime_put_sync(qts_data->spi->master->dev.parent);
  499. qts_trusted_touch_set_vm_state(qts_data, TVM_I2C_SESSION_RELEASED);
  500. rc = qts_vm_mem_release(qts_data);
  501. if (rc) {
  502. pr_err("Failed to release mem rc:%d\n", rc);
  503. goto error;
  504. } else {
  505. qts_trusted_touch_set_vm_state(qts_data, TVM_IOMEM_RELEASED);
  506. }
  507. qts_trusted_touch_set_vm_state(qts_data, TRUSTED_TOUCH_TVM_INIT);
  508. atomic_set(&qts_data->trusted_touch_enabled, 0);
  509. if (qts_data->vendor_ops.post_le_tui_disable)
  510. qts_data->vendor_ops.post_le_tui_disable(qts_data->vendor_data);
  511. pr_info("Irq, iomem are released and trusted touch disabled\n");
  512. mutex_unlock(&qts_data->transition_lock);
  513. return;
  514. error:
  515. qts_trusted_touch_abort_handler(qts_data,
  516. TRUSTED_TOUCH_EVENT_RELEASE_FAILURE);
  517. mutex_unlock(&qts_data->transition_lock);
  518. }
  519. static int qts_handle_trusted_touch_tvm(struct qts_data *qts_data, int value)
  520. {
  521. int err = 0;
  522. switch (value) {
  523. case 0:
  524. if ((atomic_read(&qts_data->trusted_touch_enabled) == 0) &&
  525. (atomic_read(&qts_data->trusted_touch_abort_status) == 0)) {
  526. pr_err("Trusted touch is already disabled\n");
  527. break;
  528. }
  529. if (atomic_read(&qts_data->trusted_touch_mode) ==
  530. TRUSTED_TOUCH_VM_MODE) {
  531. qts_trusted_touch_tvm_vm_mode_disable(qts_data);
  532. } else {
  533. pr_err("Unsupported trusted touch mode\n");
  534. }
  535. break;
  536. case 1:
  537. if (atomic_read(&qts_data->trusted_touch_enabled)) {
  538. pr_err("Trusted touch usecase underway\n");
  539. err = -EBUSY;
  540. break;
  541. }
  542. if (atomic_read(&qts_data->trusted_touch_mode) ==
  543. TRUSTED_TOUCH_VM_MODE) {
  544. qts_trusted_touch_tvm_vm_mode_enable(qts_data);
  545. } else {
  546. pr_err("Unsupported trusted touch mode\n");
  547. }
  548. break;
  549. default:
  550. pr_err("unsupported value: %lu\n", value);
  551. err = -EINVAL;
  552. break;
  553. }
  554. return err;
  555. }
  556. static void qts_trusted_touch_abort_tvm(struct qts_data *qts_data)
  557. {
  558. int rc = 0;
  559. int vm_state = qts_trusted_touch_get_vm_state(qts_data);
  560. if (vm_state >= TRUSTED_TOUCH_TVM_STATE_MAX) {
  561. pr_err("invalid tvm driver state: %d\n", vm_state);
  562. return;
  563. }
  564. switch (vm_state) {
  565. case TVM_INTERRUPT_ENABLED:
  566. qts_irq_enable(qts_data, false);
  567. fallthrough;
  568. case TVM_IRQ_ACCEPTED:
  569. case TVM_INTERRUPT_DISABLED:
  570. rc = gh_irq_release(qts_data->vm_info->irq_label);
  571. if (rc)
  572. pr_err("Failed to release irq rc:%d\n", rc);
  573. rc = gh_irq_release_notify(qts_data->vm_info->irq_label);
  574. if (rc)
  575. pr_err("Failed to notify irq release rc:%d\n", rc);
  576. fallthrough;
  577. case TVM_I2C_SESSION_ACQUIRED:
  578. case TVM_IOMEM_ACCEPTED:
  579. case TVM_IRQ_RELEASED:
  580. if (qts_data->bus_type == QTS_BUS_TYPE_I2C)
  581. pm_runtime_put_sync(qts_data->client->adapter->dev.parent);
  582. else
  583. pm_runtime_put_sync(qts_data->spi->master->dev.parent);
  584. fallthrough;
  585. case TVM_I2C_SESSION_RELEASED:
  586. rc = qts_vm_mem_release(qts_data);
  587. if (rc)
  588. pr_err("Failed to release mem rc:%d\n", rc);
  589. fallthrough;
  590. case TVM_IOMEM_RELEASED:
  591. case TVM_ALL_RESOURCES_LENT_NOTIFIED:
  592. case TRUSTED_TOUCH_TVM_INIT:
  593. case TVM_IRQ_LENT_NOTIFIED:
  594. case TVM_IOMEM_LENT_NOTIFIED:
  595. atomic_set(&qts_data->trusted_touch_enabled, 0);
  596. }
  597. atomic_set(&qts_data->trusted_touch_abort_status, 0);
  598. qts_trusted_touch_set_vm_state(qts_data, TRUSTED_TOUCH_TVM_INIT);
  599. }
  600. #else
  601. static void qts_bus_put(struct qts_data *qts_data);
  602. static int qts_enable_reg(struct qts_data *qts_data, bool enable);
  603. static void qts_trusted_touch_abort_pvm(struct qts_data *qts_data)
  604. {
  605. int rc = 0;
  606. int vm_state = qts_trusted_touch_get_vm_state(qts_data);
  607. if (vm_state >= TRUSTED_TOUCH_PVM_STATE_MAX) {
  608. pr_err("Invalid driver state: %d\n", vm_state);
  609. return;
  610. }
  611. switch (vm_state) {
  612. case PVM_IRQ_RELEASE_NOTIFIED:
  613. case PVM_ALL_RESOURCES_RELEASE_NOTIFIED:
  614. case PVM_IRQ_LENT:
  615. case PVM_IRQ_LENT_NOTIFIED:
  616. rc = gh_irq_reclaim(qts_data->vm_info->irq_label);
  617. if (rc) {
  618. pr_err("failed to reclaim irq on pvm rc:%d\n", rc);
  619. return;
  620. }
  621. fallthrough;
  622. case PVM_IRQ_RECLAIMED:
  623. case PVM_IOMEM_LENT:
  624. case PVM_IOMEM_LENT_NOTIFIED:
  625. case PVM_IOMEM_RELEASE_NOTIFIED:
  626. #if (KERNEL_VERSION(6, 1, 0) <= LINUX_VERSION_CODE)
  627. rc = ghd_rm_mem_reclaim(qts_data->vm_info->vm_mem_handle, 0);
  628. #else
  629. rc = gh_rm_mem_reclaim(qts_data->vm_info->vm_mem_handle, 0);
  630. #endif
  631. if (rc) {
  632. pr_err("failed to reclaim iomem on pvm rc:%d\n", rc);
  633. qts_trusted_touch_set_vm_state(qts_data, PVM_IOMEM_RELEASE_NOTIFIED);
  634. return;
  635. }
  636. qts_data->vm_info->vm_mem_handle = 0;
  637. fallthrough;
  638. case PVM_IOMEM_RECLAIMED:
  639. case PVM_INTERRUPT_DISABLED:
  640. if (qts_data->vendor_ops.enable_touch_irq)
  641. qts_data->vendor_ops.enable_touch_irq(qts_data->vendor_data, true);
  642. fallthrough;
  643. case PVM_I2C_RESOURCE_ACQUIRED:
  644. case PVM_INTERRUPT_ENABLED:
  645. qts_bus_put(qts_data);
  646. fallthrough;
  647. case TRUSTED_TOUCH_PVM_INIT:
  648. case PVM_I2C_RESOURCE_RELEASED:
  649. atomic_set(&qts_data->trusted_touch_enabled, 0);
  650. atomic_set(&qts_data->trusted_touch_transition, 0);
  651. }
  652. atomic_set(&qts_data->trusted_touch_abort_status, 0);
  653. qts_trusted_touch_set_vm_state(qts_data, TRUSTED_TOUCH_PVM_INIT);
  654. }
  655. static int qts_clk_prepare_enable(struct qts_data *qts_data)
  656. {
  657. int ret;
  658. ret = clk_prepare_enable(qts_data->iface_clk);
  659. if (ret) {
  660. pr_err("error on clk_prepare_enable(iface_clk):%d\n", ret);
  661. return ret;
  662. }
  663. ret = clk_prepare_enable(qts_data->core_clk);
  664. if (ret) {
  665. clk_disable_unprepare(qts_data->iface_clk);
  666. pr_err("error clk_prepare_enable(core_clk):%d\n", ret);
  667. }
  668. return ret;
  669. }
  670. static void qts_clk_disable_unprepare(struct qts_data *qts_data)
  671. {
  672. clk_disable_unprepare(qts_data->core_clk);
  673. clk_disable_unprepare(qts_data->iface_clk);
  674. }
  675. static int qts_bus_get(struct qts_data *qts_data)
  676. {
  677. int rc = 0;
  678. struct device *dev = NULL;
  679. if (qts_data->schedule_suspend)
  680. cancel_work_sync(&qts_data->suspend_work);
  681. if (qts_data->schedule_resume)
  682. cancel_work_sync(&qts_data->resume_work);
  683. reinit_completion(&qts_data->trusted_touch_powerdown);
  684. qts_enable_reg(qts_data, true);
  685. if (qts_data->bus_type == QTS_BUS_TYPE_I2C)
  686. dev = qts_data->client->adapter->dev.parent;
  687. else
  688. dev = qts_data->spi->master->dev.parent;
  689. mutex_lock(&qts_data->qts_clk_io_ctrl_mutex);
  690. rc = pm_runtime_get_sync(dev);
  691. if (rc >= 0 && qts_data->core_clk != NULL &&
  692. qts_data->iface_clk != NULL) {
  693. rc = qts_clk_prepare_enable(qts_data);
  694. if (rc)
  695. pm_runtime_put_sync(dev);
  696. }
  697. mutex_unlock(&qts_data->qts_clk_io_ctrl_mutex);
  698. return rc;
  699. }
  700. static void qts_bus_put(struct qts_data *qts_data)
  701. {
  702. struct device *dev = NULL;
  703. if (qts_data->bus_type == QTS_BUS_TYPE_I2C)
  704. dev = qts_data->client->adapter->dev.parent;
  705. else
  706. dev = qts_data->spi->master->dev.parent;
  707. mutex_lock(&qts_data->qts_clk_io_ctrl_mutex);
  708. if (qts_data->core_clk != NULL && qts_data->iface_clk != NULL)
  709. qts_clk_disable_unprepare(qts_data);
  710. pm_runtime_put_sync(dev);
  711. mutex_unlock(&qts_data->qts_clk_io_ctrl_mutex);
  712. complete(&qts_data->trusted_touch_powerdown);
  713. qts_enable_reg(qts_data, false);
  714. }
  715. static struct gh_notify_vmid_desc *qts_vm_get_vmid(gh_vmid_t vmid)
  716. {
  717. struct gh_notify_vmid_desc *vmid_desc;
  718. vmid_desc = kzalloc(offsetof(struct gh_notify_vmid_desc,
  719. vmid_entries[1]), GFP_KERNEL);
  720. if (!vmid_desc)
  721. return ERR_PTR(ENOMEM);
  722. vmid_desc->n_vmid_entries = 1;
  723. vmid_desc->vmid_entries[0].vmid = vmid;
  724. return vmid_desc;
  725. }
  726. static void qts_trusted_touch_pvm_vm_mode_disable(struct qts_data *qts_data)
  727. {
  728. int rc = 0;
  729. atomic_set(&qts_data->trusted_touch_transition, 1);
  730. if (atomic_read(&qts_data->trusted_touch_abort_status)) {
  731. qts_trusted_touch_abort_pvm(qts_data);
  732. return;
  733. }
  734. if (qts_trusted_touch_get_vm_state(qts_data) != PVM_ALL_RESOURCES_RELEASE_NOTIFIED)
  735. pr_info("all release notifications are not received yet\n");
  736. if (qts_data->vendor_ops.pre_la_tui_disable)
  737. qts_data->vendor_ops.pre_la_tui_disable(qts_data->vendor_data);
  738. #if (KERNEL_VERSION(6, 1, 0) <= LINUX_VERSION_CODE)
  739. rc = ghd_rm_mem_reclaim(qts_data->vm_info->vm_mem_handle, 0);
  740. #else
  741. rc = gh_rm_mem_reclaim(qts_data->vm_info->vm_mem_handle, 0);
  742. #endif
  743. if (rc) {
  744. pr_err("Trusted touch VM mem reclaim failed rc:%d\n", rc);
  745. goto error;
  746. }
  747. qts_trusted_touch_set_vm_state(qts_data, PVM_IOMEM_RECLAIMED);
  748. qts_data->vm_info->vm_mem_handle = 0;
  749. pr_debug("vm mem reclaim success!\n");
  750. rc = gh_irq_reclaim(qts_data->vm_info->irq_label);
  751. if (rc) {
  752. pr_err("failed to reclaim irq on pvm rc:%d\n", rc);
  753. goto error;
  754. }
  755. qts_trusted_touch_set_vm_state(qts_data, PVM_IRQ_RECLAIMED);
  756. pr_debug("vm irq reclaim success!\n");
  757. if (qts_data->vendor_ops.enable_touch_irq)
  758. qts_data->vendor_ops.enable_touch_irq(qts_data->vendor_data, true);
  759. qts_trusted_touch_set_vm_state(qts_data, PVM_INTERRUPT_ENABLED);
  760. qts_bus_put(qts_data);
  761. atomic_set(&qts_data->trusted_touch_transition, 0);
  762. qts_trusted_touch_set_vm_state(qts_data, PVM_I2C_RESOURCE_RELEASED);
  763. qts_trusted_touch_set_vm_state(qts_data, TRUSTED_TOUCH_PVM_INIT);
  764. atomic_set(&qts_data->trusted_touch_enabled, 0);
  765. if (qts_data->vendor_ops.post_la_tui_disable)
  766. qts_data->vendor_ops.post_la_tui_disable(qts_data->vendor_data);
  767. pr_info("Irq, iomem are reclaimed and trusted touch disabled\n");
  768. return;
  769. error:
  770. qts_trusted_touch_abort_handler(qts_data,
  771. TRUSTED_TOUCH_EVENT_RECLAIM_FAILURE);
  772. }
  773. static void qts_vm_irq_on_release_callback(void *data,
  774. unsigned long notif_type,
  775. enum gh_irq_label label)
  776. {
  777. struct qts_data *qts_data = data;
  778. if (notif_type != GH_RM_NOTIF_VM_IRQ_RELEASED) {
  779. pr_err("invalid notification type\n");
  780. return;
  781. }
  782. if (qts_trusted_touch_get_vm_state(qts_data) == PVM_IOMEM_RELEASE_NOTIFIED)
  783. qts_trusted_touch_set_vm_state(qts_data, PVM_ALL_RESOURCES_RELEASE_NOTIFIED);
  784. else
  785. qts_trusted_touch_set_vm_state(qts_data, PVM_IRQ_RELEASE_NOTIFIED);
  786. }
  787. static void qts_vm_mem_on_release_handler(enum gh_mem_notifier_tag tag,
  788. unsigned long notif_type, void *entry_data, void *notif_msg)
  789. {
  790. struct gh_rm_notif_mem_released_payload *release_payload;
  791. struct trusted_touch_vm_info *vm_info;
  792. struct qts_data *qts_data;
  793. qts_data = (struct qts_data *)entry_data;
  794. vm_info = qts_data->vm_info;
  795. if (!vm_info) {
  796. pr_err("Invalid vm_info\n");
  797. return;
  798. }
  799. if (notif_type != GH_RM_NOTIF_MEM_RELEASED) {
  800. pr_err("Invalid notification type\n");
  801. return;
  802. }
  803. if (tag != vm_info->mem_tag) {
  804. pr_err("Invalid tag\n");
  805. return;
  806. }
  807. if (!entry_data || !notif_msg) {
  808. pr_err("Invalid data or notification message\n");
  809. return;
  810. }
  811. release_payload = (struct gh_rm_notif_mem_released_payload *)notif_msg;
  812. if (release_payload->mem_handle != vm_info->vm_mem_handle) {
  813. pr_err("Invalid mem handle detected\n");
  814. return;
  815. }
  816. if (qts_trusted_touch_get_vm_state(qts_data) == PVM_IRQ_RELEASE_NOTIFIED)
  817. qts_trusted_touch_set_vm_state(qts_data, PVM_ALL_RESOURCES_RELEASE_NOTIFIED);
  818. else
  819. qts_trusted_touch_set_vm_state(qts_data, PVM_IOMEM_RELEASE_NOTIFIED);
  820. }
  821. static int qts_vm_mem_lend(struct qts_data *qts_data)
  822. {
  823. struct gh_acl_desc *acl_desc;
  824. struct gh_sgl_desc *sgl_desc;
  825. struct gh_notify_vmid_desc *vmid_desc;
  826. gh_memparcel_handle_t mem_handle;
  827. gh_vmid_t trusted_vmid;
  828. int rc = 0;
  829. acl_desc = qts_vm_get_acl(GH_TRUSTED_VM);
  830. if (IS_ERR(acl_desc)) {
  831. pr_err("Failed to get acl of IO memories for Trusted touch\n");
  832. rc = PTR_ERR(acl_desc);
  833. return rc;
  834. }
  835. sgl_desc = qts_vm_get_sgl(qts_data->vm_info);
  836. if (IS_ERR(sgl_desc)) {
  837. pr_err("Failed to get sgl of IO memories for Trusted touch\n");
  838. rc = PTR_ERR(sgl_desc);
  839. goto sgl_error;
  840. }
  841. #if (KERNEL_VERSION(6, 1, 0) <= LINUX_VERSION_CODE)
  842. rc = ghd_rm_mem_lend(GH_RM_MEM_TYPE_IO, 0, TRUSTED_TOUCH_MEM_LABEL,
  843. acl_desc, sgl_desc, NULL, &mem_handle);
  844. #else
  845. rc = gh_rm_mem_lend(GH_RM_MEM_TYPE_IO, 0, TRUSTED_TOUCH_MEM_LABEL,
  846. acl_desc, sgl_desc, NULL, &mem_handle);
  847. #endif
  848. if (rc) {
  849. pr_err("Failed to lend IO memories for Trusted touch rc:%d\n", rc);
  850. goto error;
  851. }
  852. pr_debug("vm mem lend success\n");
  853. qts_trusted_touch_set_vm_state(qts_data, PVM_IOMEM_LENT);
  854. #if (KERNEL_VERSION(6, 1, 0) <= LINUX_VERSION_CODE)
  855. ghd_rm_get_vmid(GH_TRUSTED_VM, &trusted_vmid);
  856. #else
  857. gh_rm_get_vmid(GH_TRUSTED_VM, &trusted_vmid);
  858. #endif
  859. vmid_desc = qts_vm_get_vmid(trusted_vmid);
  860. rc = gh_rm_mem_notify(mem_handle, GH_RM_MEM_NOTIFY_RECIPIENT_SHARED,
  861. qts_data->vm_info->mem_tag, vmid_desc);
  862. if (rc) {
  863. pr_err("Failed to notify mem lend to hypervisor rc:%d\n", rc);
  864. goto vmid_error;
  865. }
  866. qts_trusted_touch_set_vm_state(qts_data, PVM_IOMEM_LENT_NOTIFIED);
  867. qts_data->vm_info->vm_mem_handle = mem_handle;
  868. vmid_error:
  869. kfree(vmid_desc);
  870. error:
  871. kfree(sgl_desc);
  872. sgl_error:
  873. kfree(acl_desc);
  874. return rc;
  875. }
  876. static int qts_trusted_touch_pvm_vm_mode_enable(struct qts_data *qts_data)
  877. {
  878. int rc = 0;
  879. struct trusted_touch_vm_info *vm_info = qts_data->vm_info;
  880. atomic_set(&qts_data->trusted_touch_transition, 1);
  881. mutex_lock(&qts_data->transition_lock);
  882. if (qts_data->suspended) {
  883. pr_err("Invalid power state for operation\n");
  884. atomic_set(&qts_data->trusted_touch_transition, 0);
  885. rc = -EPERM;
  886. goto error;
  887. }
  888. if (qts_data->vendor_ops.pre_la_tui_enable)
  889. qts_data->vendor_ops.pre_la_tui_enable(qts_data->vendor_data);
  890. /* i2c session start and resource acquire */
  891. if (qts_bus_get(qts_data) < 0) {
  892. pr_err("qts_bus_get failed\n");
  893. rc = -EIO;
  894. goto error;
  895. }
  896. qts_trusted_touch_set_vm_state(qts_data, PVM_I2C_RESOURCE_ACQUIRED);
  897. if (qts_data->vendor_ops.enable_touch_irq)
  898. qts_data->vendor_ops.enable_touch_irq(qts_data->vendor_data, false);
  899. qts_trusted_touch_set_vm_state(qts_data, PVM_INTERRUPT_DISABLED);
  900. rc = qts_vm_mem_lend(qts_data);
  901. if (rc) {
  902. pr_err("Failed to lend memory\n");
  903. goto abort_handler;
  904. }
  905. pr_debug("vm mem lend success\n");
  906. if (atomic_read(&qts_data->delayed_pvm_probe_pending)) {
  907. if (qts_data->vendor_ops.get_irq_num)
  908. qts_data->irq = qts_data->vendor_ops.get_irq_num(qts_data->vendor_data);
  909. atomic_set(&qts_data->delayed_tvm_probe_pending, 0);
  910. }
  911. rc = gh_irq_lend_v2(vm_info->irq_label, vm_info->vm_name,
  912. qts_data->irq, &qts_vm_irq_on_release_callback, qts_data);
  913. if (rc) {
  914. pr_err("Failed to lend irq\n");
  915. goto abort_handler;
  916. }
  917. pr_debug("vm irq lend success for irq:%d\n", qts_data->irq);
  918. qts_trusted_touch_set_vm_state(qts_data, PVM_IRQ_LENT);
  919. rc = gh_irq_lend_notify(vm_info->irq_label);
  920. if (rc) {
  921. pr_err("Failed to notify irq\n");
  922. goto abort_handler;
  923. }
  924. qts_trusted_touch_set_vm_state(qts_data, PVM_IRQ_LENT_NOTIFIED);
  925. if (qts_data->vendor_ops.post_la_tui_enable)
  926. qts_data->vendor_ops.post_la_tui_enable(qts_data->vendor_data);
  927. mutex_unlock(&qts_data->transition_lock);
  928. atomic_set(&qts_data->trusted_touch_transition, 0);
  929. atomic_set(&qts_data->trusted_touch_enabled, 1);
  930. pr_info("Irq, iomem are lent and trusted touch enabled\n");
  931. return rc;
  932. abort_handler:
  933. qts_trusted_touch_abort_handler(qts_data, TRUSTED_TOUCH_EVENT_LEND_FAILURE);
  934. error:
  935. mutex_unlock(&qts_data->transition_lock);
  936. return rc;
  937. }
  938. static int qts_handle_trusted_touch_pvm(struct qts_data *qts_data, int value)
  939. {
  940. int err = 0;
  941. switch (value) {
  942. case 0:
  943. if (atomic_read(&qts_data->trusted_touch_enabled) == 0 &&
  944. (atomic_read(&qts_data->trusted_touch_abort_status) == 0)) {
  945. pr_err("Trusted touch is already disabled\n");
  946. break;
  947. }
  948. if (atomic_read(&qts_data->trusted_touch_mode) ==
  949. TRUSTED_TOUCH_VM_MODE) {
  950. qts_trusted_touch_pvm_vm_mode_disable(qts_data);
  951. } else {
  952. pr_err("Unsupported trusted touch mode\n");
  953. }
  954. break;
  955. case 1:
  956. if (atomic_read(&qts_data->trusted_touch_enabled)) {
  957. pr_err("Trusted touch usecase underway\n");
  958. err = -EBUSY;
  959. break;
  960. }
  961. if (atomic_read(&qts_data->trusted_touch_mode) ==
  962. TRUSTED_TOUCH_VM_MODE) {
  963. err = qts_trusted_touch_pvm_vm_mode_enable(qts_data);
  964. } else {
  965. pr_err("Unsupported trusted touch mode\n");
  966. }
  967. break;
  968. default:
  969. pr_err("unsupported value: %lu\n", value);
  970. err = -EINVAL;
  971. break;
  972. }
  973. return err;
  974. }
  975. #endif
  976. static void qts_trusted_touch_event_notify(struct qts_data *qts_data, int event)
  977. {
  978. atomic_set(&qts_data->trusted_touch_event, event);
  979. sysfs_notify(&qts_data->dev->kobj, NULL, "trusted_touch_event");
  980. }
  981. static void qts_trusted_touch_abort_handler(struct qts_data *qts_data, int error)
  982. {
  983. atomic_set(&qts_data->trusted_touch_abort_status, error);
  984. pr_info("TUI session aborted with failure:%d\n", error);
  985. qts_trusted_touch_event_notify(qts_data, error);
  986. #ifdef CONFIG_ARCH_QTI_VM
  987. pr_info("Resetting touch controller\n");
  988. if (qts_trusted_touch_get_vm_state(qts_data) >= TVM_IOMEM_ACCEPTED &&
  989. error == TRUSTED_TOUCH_EVENT_I2C_FAILURE) {
  990. pr_info("Resetting touch controller\n");
  991. qts_trusted_touch_reset_gpio_toggle(qts_data);
  992. }
  993. #endif
  994. }
  995. static int qts_vm_init(struct qts_data *qts_data)
  996. {
  997. int rc = 0;
  998. struct trusted_touch_vm_info *vm_info;
  999. void *mem_cookie;
  1000. rc = qts_populate_vm_info(qts_data);
  1001. if (rc) {
  1002. pr_err("Cannot setup vm pipeline\n");
  1003. rc = -EINVAL;
  1004. goto fail;
  1005. }
  1006. vm_info = qts_data->vm_info;
  1007. #ifdef CONFIG_ARCH_QTI_VM
  1008. mem_cookie = gh_mem_notifier_register(vm_info->mem_tag,
  1009. qts_vm_mem_on_lend_handler, qts_data);
  1010. if (!mem_cookie) {
  1011. pr_err("Failed to register on lend mem notifier\n");
  1012. rc = -EINVAL;
  1013. goto init_fail;
  1014. }
  1015. vm_info->mem_cookie = mem_cookie;
  1016. rc = gh_irq_wait_for_lend_v2(vm_info->irq_label, GH_PRIMARY_VM,
  1017. &qts_vm_irq_on_lend_callback, qts_data);
  1018. qts_trusted_touch_set_vm_state(qts_data, TRUSTED_TOUCH_TVM_INIT);
  1019. #else
  1020. mem_cookie = gh_mem_notifier_register(vm_info->mem_tag,
  1021. qts_vm_mem_on_release_handler, qts_data);
  1022. if (!mem_cookie) {
  1023. pr_err("Failed to register on release mem notifier\n");
  1024. rc = -EINVAL;
  1025. goto init_fail;
  1026. }
  1027. vm_info->mem_cookie = mem_cookie;
  1028. qts_trusted_touch_set_vm_state(qts_data, TRUSTED_TOUCH_PVM_INIT);
  1029. #endif
  1030. return rc;
  1031. init_fail:
  1032. qts_vm_deinit(qts_data);
  1033. fail:
  1034. return rc;
  1035. }
  1036. static void qts_dt_parse_trusted_touch_info(struct qts_data *qts_data)
  1037. {
  1038. struct device_node *np = qts_data->dev->of_node;
  1039. int rc = 0;
  1040. const char *selection;
  1041. const char *environment;
  1042. rc = of_property_read_string(np, "qts,trusted-touch-mode", &selection);
  1043. if (rc) {
  1044. pr_err("No trusted touch mode selection made\n");
  1045. atomic_set(&qts_data->trusted_touch_mode,
  1046. TRUSTED_TOUCH_MODE_NONE);
  1047. return;
  1048. }
  1049. if (!strcmp(selection, "vm_mode")) {
  1050. atomic_set(&qts_data->trusted_touch_mode,
  1051. TRUSTED_TOUCH_VM_MODE);
  1052. pr_debug("Selected trusted touch mode to VM mode\n");
  1053. } else {
  1054. atomic_set(&qts_data->trusted_touch_mode,
  1055. TRUSTED_TOUCH_MODE_NONE);
  1056. pr_err("Invalid trusted_touch mode\n");
  1057. }
  1058. rc = of_property_read_string(np, "qts,touch-environment",
  1059. &environment);
  1060. if (rc)
  1061. pr_err("No trusted touch mode environment\n");
  1062. qts_data->touch_environment = environment;
  1063. qts_data->tui_supported = true;
  1064. pr_debug("Trusted touch environment:%s\n", qts_data->touch_environment);
  1065. }
  1066. static void qts_trusted_touch_init(struct qts_data *qts_data)
  1067. {
  1068. int rc = 0;
  1069. atomic_set(&qts_data->trusted_touch_initialized, 0);
  1070. qts_dt_parse_trusted_touch_info(qts_data);
  1071. if (atomic_read(&qts_data->trusted_touch_mode) == TRUSTED_TOUCH_MODE_NONE)
  1072. return;
  1073. init_completion(&qts_data->trusted_touch_powerdown);
  1074. /* Get clocks */
  1075. qts_data->core_clk = devm_clk_get(qts_data->dev->parent, "m-ahb");
  1076. if (IS_ERR(qts_data->core_clk)) {
  1077. qts_data->core_clk = NULL;
  1078. pr_err("core_clk is not defined\n");
  1079. }
  1080. qts_data->iface_clk = devm_clk_get(qts_data->dev->parent, "se-clk");
  1081. if (IS_ERR(qts_data->iface_clk)) {
  1082. qts_data->iface_clk = NULL;
  1083. pr_err("iface_clk is not defined\n");
  1084. }
  1085. if (atomic_read(&qts_data->trusted_touch_mode) ==
  1086. TRUSTED_TOUCH_VM_MODE) {
  1087. rc = qts_vm_init(qts_data);
  1088. if (rc)
  1089. pr_err("Failed to init VM\n");
  1090. }
  1091. atomic_set(&qts_data->trusted_touch_initialized, 1);
  1092. }
  1093. static bool qts_ts_is_primary(struct kobject *kobj)
  1094. {
  1095. char *path = NULL;
  1096. if (!kobj)
  1097. return true;
  1098. path = kobject_get_path(kobj, GFP_KERNEL);
  1099. if (strstr(path, "primary"))
  1100. return true;
  1101. else
  1102. return false;
  1103. }
  1104. static ssize_t trusted_touch_enable_show(struct kobject *kobj, struct kobj_attribute *attr,
  1105. char *buf)
  1106. {
  1107. struct qts_data *qts_data;
  1108. u32 idx = qts_ts_is_primary(kobj) ? 0 : 1;
  1109. qts_data = &qts_data_entries->info[idx];
  1110. return scnprintf(buf, PAGE_SIZE, "%d",
  1111. atomic_read(&qts_data->trusted_touch_enabled));
  1112. }
  1113. static ssize_t trusted_touch_enable_store(struct kobject *kobj, struct kobj_attribute *attr,
  1114. const char *buf, size_t count)
  1115. {
  1116. struct qts_data *qts_data;
  1117. unsigned long value;
  1118. int err = 0;
  1119. u32 idx = qts_ts_is_primary(kobj) ? 0 : 1;
  1120. if (count > 2)
  1121. return -EINVAL;
  1122. err = kstrtoul(buf, 10, &value);
  1123. if (err != 0)
  1124. return err;
  1125. qts_data = &qts_data_entries->info[idx];
  1126. if (!atomic_read(&qts_data->trusted_touch_initialized))
  1127. return -EIO;
  1128. pr_info("TUI trusted_touch_enable:%d\n", value);
  1129. #ifdef CONFIG_ARCH_QTI_VM
  1130. err = qts_handle_trusted_touch_tvm(qts_data, value);
  1131. if (err) {
  1132. pr_err("Failed to handle trusted touch in tvm\n");
  1133. return -EINVAL;
  1134. }
  1135. #else
  1136. err = qts_handle_trusted_touch_pvm(qts_data, value);
  1137. if (err) {
  1138. pr_err("Failed to handle trusted touch in pvm\n");
  1139. return -EINVAL;
  1140. }
  1141. #endif
  1142. err = count;
  1143. return err;
  1144. }
  1145. static ssize_t trusted_touch_event_show(struct kobject *kobj, struct kobj_attribute *attr,
  1146. char *buf)
  1147. {
  1148. struct qts_data *qts_data;
  1149. u32 idx = qts_ts_is_primary(kobj) ? 0 : 1;
  1150. qts_data = &qts_data_entries->info[idx];
  1151. return scnprintf(buf, PAGE_SIZE, "%d",
  1152. atomic_read(&qts_data->trusted_touch_event));
  1153. }
  1154. static ssize_t trusted_touch_event_store(struct kobject *kobj, struct kobj_attribute *attr,
  1155. const char *buf, size_t count)
  1156. {
  1157. struct qts_data *qts_data;
  1158. unsigned long value;
  1159. int err = 0;
  1160. u32 idx = qts_ts_is_primary(kobj) ? 0 : 1;
  1161. if (count > 2)
  1162. return -EINVAL;
  1163. err = kstrtoul(buf, 10, &value);
  1164. if (err != 0)
  1165. return err;
  1166. qts_data = &qts_data_entries->info[idx];
  1167. if (!atomic_read(&qts_data->trusted_touch_initialized))
  1168. return -EIO;
  1169. if (value)
  1170. return -EIO;
  1171. atomic_set(&qts_data->trusted_touch_event, value);
  1172. return count;
  1173. }
  1174. static ssize_t trusted_touch_type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  1175. {
  1176. struct qts_data *qts_data;
  1177. u32 idx = qts_ts_is_primary(kobj) ? 0 : 1;
  1178. qts_data = &qts_data_entries->info[idx];
  1179. return scnprintf(buf, PAGE_SIZE, "%s", qts_data->vm_info->trusted_touch_type);
  1180. }
  1181. static ssize_t trusted_touch_device_path_show(struct kobject *kobj,
  1182. struct kobj_attribute *attr, char *buf)
  1183. {
  1184. struct qts_data *qts_data;
  1185. char *path = NULL;
  1186. u32 idx = qts_ts_is_primary(kobj) ? 0 : 1;
  1187. qts_data = &qts_data_entries->info[idx];
  1188. if (qts_data && qts_data->dev)
  1189. path = kobject_get_path(&qts_data->dev->kobj, GFP_KERNEL);
  1190. return scnprintf(buf, PAGE_SIZE, "%s", path ? path : "");
  1191. }
  1192. static struct kobj_attribute trusted_touch_enable_attr =
  1193. __ATTR(trusted_touch_enable, 0664, trusted_touch_enable_show, trusted_touch_enable_store);
  1194. static struct kobj_attribute trusted_touch_event_attr =
  1195. __ATTR(trusted_touch_event, 0664, trusted_touch_event_show, trusted_touch_event_store);
  1196. static struct kobj_attribute trusted_touch_type_attr =
  1197. __ATTR(trusted_touch_type, 0664, trusted_touch_type_show, NULL);
  1198. static struct kobj_attribute trusted_touch_device_path_attr =
  1199. __ATTR(trusted_touch_device_path, 0444, trusted_touch_device_path_show, NULL);
  1200. static struct attribute *qts_attributes[] = {
  1201. &trusted_touch_enable_attr.attr,
  1202. &trusted_touch_event_attr.attr,
  1203. &trusted_touch_type_attr.attr,
  1204. &trusted_touch_device_path_attr.attr,
  1205. NULL,
  1206. };
  1207. static struct attribute_group qts_attribute_group = {
  1208. .attrs = qts_attributes,
  1209. };
  1210. static int qts_create_sysfs(struct qts_data *qts_data)
  1211. {
  1212. int ret = 0;
  1213. struct kobject *qts_kobj;
  1214. struct kobject *client_kobj;
  1215. qts_kobj = &qts_data_entries->qts_kset->kobj;
  1216. if (qts_data->client_type == QTS_CLIENT_PRIMARY_TOUCH) {
  1217. client_kobj = kobject_create_and_add("primary", qts_kobj);
  1218. if (!client_kobj) {
  1219. pr_err("primary kobject_create_and_add failed\n");
  1220. return -ENOMEM;
  1221. }
  1222. ret = sysfs_create_group(client_kobj, &qts_attribute_group);
  1223. if (ret) {
  1224. pr_err("[EX]: sysfs_create_group() failed!!\n");
  1225. sysfs_remove_group(client_kobj, &qts_attribute_group);
  1226. return -ENOMEM;
  1227. }
  1228. } else if (qts_data->client_type == QTS_CLIENT_SECONDARY_TOUCH) {
  1229. client_kobj = kobject_create_and_add("secondary", qts_kobj);
  1230. if (!client_kobj) {
  1231. pr_err("secondary kobject_create_and_add failed\n");
  1232. return -ENOMEM;
  1233. }
  1234. ret = sysfs_create_group(client_kobj, &qts_attribute_group);
  1235. if (ret) {
  1236. pr_err("[EX]: sysfs_create_group() failed!!\n");
  1237. sysfs_remove_group(client_kobj, &qts_attribute_group);
  1238. return -ENOMEM;
  1239. }
  1240. }
  1241. pr_debug("sysfs_create_group() succeeded\n");
  1242. return ret;
  1243. }
  1244. static int qts_ts_check_dt(struct device_node *np)
  1245. {
  1246. int i;
  1247. int count;
  1248. struct device_node *node;
  1249. struct drm_panel *panel;
  1250. count = of_count_phandle_with_args(np, "panel", NULL);
  1251. if (count <= 0)
  1252. return 0;
  1253. for (i = 0; i < count; i++) {
  1254. node = of_parse_phandle(np, "panel", i);
  1255. panel = of_drm_find_panel(node);
  1256. of_node_put(node);
  1257. if (!IS_ERR(panel)) {
  1258. active_panel = panel;
  1259. return 0;
  1260. }
  1261. }
  1262. return PTR_ERR(panel);
  1263. }
  1264. static void qts_power_source_init(struct qts_data *qts_data)
  1265. {
  1266. qts_data->vdd = regulator_get(qts_data->dev, "vdd");
  1267. if (IS_ERR_OR_NULL(qts_data->vdd))
  1268. pr_debug("get vdd regulator failed\n");
  1269. qts_data->avdd = regulator_get(qts_data->dev, "avdd");
  1270. if (IS_ERR_OR_NULL(qts_data->avdd))
  1271. pr_debug("get avdd regulator failed\n");
  1272. }
  1273. #ifndef CONFIG_ARCH_QTI_VM
  1274. static int qts_enable_reg(struct qts_data *qts_data, bool enable)
  1275. {
  1276. int ret = 0;
  1277. if (IS_ERR_OR_NULL(qts_data->vdd)) {
  1278. pr_err("vdd is invalid\n");
  1279. return ret;
  1280. }
  1281. if (enable) {
  1282. ret = regulator_enable(qts_data->vdd);
  1283. if (ret) {
  1284. pr_err("enable vdd regulator failed,ret=%d\n", ret);
  1285. goto error;
  1286. }
  1287. if (!IS_ERR_OR_NULL(qts_data->avdd)) {
  1288. ret = regulator_enable(qts_data->avdd);
  1289. if (ret) {
  1290. pr_err("enable avdd regulator failed,ret=%d\n", ret);
  1291. goto error_avdd_en;
  1292. }
  1293. }
  1294. } else {
  1295. ret = regulator_disable(qts_data->vdd);
  1296. if (ret)
  1297. pr_err("disable vdd regulator failed,ret=%d\n", ret);
  1298. if (!IS_ERR_OR_NULL(qts_data->avdd)) {
  1299. ret = regulator_disable(qts_data->avdd);
  1300. if (ret)
  1301. pr_err("disable avdd regulator failed,ret=%d\n", ret);
  1302. }
  1303. }
  1304. pr_debug("enable %d completed\n", enable);
  1305. return ret;
  1306. error_avdd_en:
  1307. (void)regulator_disable(qts_data->vdd);
  1308. error:
  1309. return ret;
  1310. }
  1311. #endif
  1312. static void qts_ts_suspend(struct qts_data *qts_data)
  1313. {
  1314. int rc = 0;
  1315. if (qts_data->suspended) {
  1316. pr_warn("already in suspend state\n");
  1317. return;
  1318. }
  1319. if (qts_data->tui_supported) {
  1320. if (atomic_read(&qts_data->trusted_touch_transition)
  1321. || atomic_read(&qts_data->trusted_touch_enabled))
  1322. wait_for_completion_interruptible(&qts_data->trusted_touch_powerdown);
  1323. }
  1324. mutex_lock(&qts_data->transition_lock);
  1325. rc = qts_data->vendor_ops.suspend(qts_data->vendor_data);
  1326. if (rc)
  1327. pr_err("suspend failed, rc = %d\n", rc);
  1328. qts_data->suspended = true;
  1329. mutex_unlock(&qts_data->transition_lock);
  1330. }
  1331. static void qts_ts_resume(struct qts_data *qts_data)
  1332. {
  1333. int rc = 0;
  1334. if (!qts_data->suspended) {
  1335. pr_warn("Already in awake state\n");
  1336. return;
  1337. }
  1338. if (qts_data->tui_supported)
  1339. if (atomic_read(&qts_data->trusted_touch_transition))
  1340. wait_for_completion_interruptible(&qts_data->trusted_touch_powerdown);
  1341. mutex_lock(&qts_data->transition_lock);
  1342. rc = qts_data->vendor_ops.resume(qts_data->vendor_data);
  1343. if (rc)
  1344. pr_err("resume failed, rc = %d\n", rc);
  1345. qts_data->suspended = false;
  1346. mutex_unlock(&qts_data->transition_lock);
  1347. }
  1348. static void qts_resume_work(struct work_struct *work)
  1349. {
  1350. struct qts_data *qts_data = container_of(work, struct qts_data,
  1351. resume_work);
  1352. qts_ts_resume(qts_data);
  1353. }
  1354. static void qts_suspend_work(struct work_struct *work)
  1355. {
  1356. struct qts_data *qts_data = container_of(work, struct qts_data,
  1357. suspend_work);
  1358. qts_ts_suspend(qts_data);
  1359. }
  1360. static void qts_panel_notifier_callback(enum panel_event_notifier_tag tag,
  1361. struct panel_event_notification *notification, void *client_data)
  1362. {
  1363. struct qts_data *qts_data = client_data;
  1364. if (!notification) {
  1365. pr_err("Invalid notification\n");
  1366. return;
  1367. }
  1368. pr_debug("Notification type:%d, early_trigger:%d\n",
  1369. notification->notif_type, notification->notif_data.early_trigger);
  1370. switch (notification->notif_type) {
  1371. case DRM_PANEL_EVENT_UNBLANK:
  1372. if (notification->notif_data.early_trigger) {
  1373. pr_debug("resume notification pre commit\n");
  1374. } else {
  1375. if (qts_data->schedule_resume)
  1376. queue_work(qts_data->ts_workqueue, &qts_data->resume_work);
  1377. else
  1378. qts_ts_resume(qts_data);
  1379. }
  1380. break;
  1381. case DRM_PANEL_EVENT_BLANK:
  1382. if (notification->notif_data.early_trigger) {
  1383. if (qts_data->schedule_resume)
  1384. cancel_work_sync(&qts_data->resume_work);
  1385. if (qts_data->schedule_suspend)
  1386. queue_work(qts_data->ts_workqueue, &qts_data->suspend_work);
  1387. else
  1388. qts_ts_suspend(qts_data);
  1389. } else {
  1390. pr_debug("suspend notification post commit\n");
  1391. }
  1392. break;
  1393. case DRM_PANEL_EVENT_BLANK_LP:
  1394. pr_debug("received lp event\n");
  1395. break;
  1396. case DRM_PANEL_EVENT_FPS_CHANGE:
  1397. pr_debug("Received fps change old fps:%d new fps:%d\n",
  1398. notification->notif_data.old_fps,
  1399. notification->notif_data.new_fps);
  1400. break;
  1401. default:
  1402. pr_debug("notification serviced :%d\n",
  1403. notification->notif_type);
  1404. break;
  1405. }
  1406. }
  1407. static void qts_ts_register_for_panel_events(struct qts_data *qts_data)
  1408. {
  1409. void *cookie = NULL;
  1410. if (qts_data->client_type != QTS_CLIENT_PRIMARY_TOUCH) {
  1411. pr_err("Invalid touch type\n");
  1412. return;
  1413. }
  1414. cookie = panel_event_notifier_register(PANEL_EVENT_NOTIFICATION_PRIMARY,
  1415. PANEL_EVENT_NOTIFIER_CLIENT_PRIMARY_TOUCH, qts_data->panel,
  1416. &qts_panel_notifier_callback, qts_data);
  1417. if (!cookie) {
  1418. pr_err("Failed to register for panel events\n");
  1419. return;
  1420. }
  1421. pr_debug("registered for panel notifications panel: 0x%x\n", qts_data->panel);
  1422. qts_data->notifier_cookie = cookie;
  1423. }
  1424. int qts_client_register(struct qts_vendor_data qts_vendor_data)
  1425. {
  1426. struct qts_data *qts_data;
  1427. struct device_node *dp;
  1428. int rc = 0;
  1429. if (!qts_data_entries) {
  1430. pr_debug("QTS client register\n");
  1431. qts_data_entries = kzalloc(sizeof(*qts_data_entries), GFP_KERNEL);
  1432. if (!qts_data_entries) {
  1433. pr_err("mem allocation failed\n");
  1434. return -EPROBE_DEFER;
  1435. }
  1436. mutex_init(&qts_data_entries->qts_data_entries_lock);
  1437. qts_data_entries->qts_kset = kset_create_and_add("qts", NULL, kernel_kobj);
  1438. if (!qts_data_entries->qts_kset) {
  1439. pr_err("qts kset create failed\n");
  1440. return -ENOMEM;
  1441. }
  1442. }
  1443. mutex_lock(&qts_data_entries->qts_data_entries_lock);
  1444. if (qts_vendor_data.bus_type == QTS_BUS_TYPE_I2C)
  1445. dp = qts_vendor_data.client->dev.of_node;
  1446. else
  1447. dp = qts_vendor_data.spi->dev.of_node;
  1448. rc = qts_ts_check_dt(dp);
  1449. if (rc) {
  1450. pr_debug("qts_ts_check_dt failed, rc = %d\n", rc);
  1451. goto qts_register_end;
  1452. }
  1453. pr_debug("QTS client register starts\n");
  1454. qts_data = &qts_data_entries->info[qts_vendor_data.client_type];
  1455. qts_data->client = qts_vendor_data.client;
  1456. qts_data->spi = qts_vendor_data.spi;
  1457. if (qts_vendor_data.bus_type == QTS_BUS_TYPE_I2C)
  1458. qts_data->dev = &qts_data->client->dev;
  1459. else
  1460. qts_data->dev = &qts_data->spi->dev;
  1461. qts_data->bus_type = qts_vendor_data.bus_type;
  1462. qts_data->client_type = qts_vendor_data.client_type;
  1463. qts_data->dp = dp;
  1464. qts_data->vendor_data = qts_vendor_data.vendor_data;
  1465. qts_data->panel = active_panel;
  1466. qts_data->vendor_ops = qts_vendor_data.qts_vendor_ops;
  1467. qts_data->schedule_suspend = qts_vendor_data.schedule_suspend;
  1468. qts_data->schedule_resume = qts_vendor_data.schedule_resume;
  1469. qts_trusted_touch_init(qts_data);
  1470. mutex_init(&(qts_data->qts_clk_io_ctrl_mutex));
  1471. if (qts_data->tui_supported)
  1472. qts_create_sysfs(qts_data);
  1473. mutex_init(&qts_data->transition_lock);
  1474. #ifdef CONFIG_ARCH_QTI_VM
  1475. atomic_set(&qts_data->delayed_tvm_probe_pending, 1);
  1476. goto qts_register_end;
  1477. #else
  1478. atomic_set(&qts_data->delayed_pvm_probe_pending, 1);
  1479. #endif
  1480. qts_power_source_init(qts_data);
  1481. qts_data->ts_workqueue = create_singlethread_workqueue("qts_wq");
  1482. if (!qts_data->ts_workqueue)
  1483. pr_err("create qts workqueue fail\n");
  1484. if (qts_data->ts_workqueue && qts_data->schedule_resume)
  1485. INIT_WORK(&qts_data->resume_work, qts_resume_work);
  1486. if (qts_data->ts_workqueue && qts_data->schedule_suspend)
  1487. INIT_WORK(&qts_data->suspend_work, qts_suspend_work);
  1488. qts_ts_register_for_panel_events(qts_data);
  1489. qts_register_end:
  1490. pr_debug("client register end\n");
  1491. mutex_unlock(&qts_data_entries->qts_data_entries_lock);
  1492. return rc;
  1493. }
  1494. EXPORT_SYMBOL(qts_client_register);