pt_devtree.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. /*
  2. * pt_devtree.c
  3. * Parade TrueTouch(TM) Standard Product Device Tree Support Module.
  4. * For use with Parade touchscreen controllers.
  5. * Supported parts include:
  6. * TMA5XX
  7. * TMA448
  8. * TMA445A
  9. * TT21XXX
  10. * TT31XXX
  11. * TT4XXXX
  12. * TT7XXX
  13. * TC3XXX
  14. *
  15. * Copyright (C) 2015-2020 Parade Technologies
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * version 2, and only version 2, as published by the
  20. * Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * Contact Parade Technologies at www.paradetech.com <[email protected]>
  28. */
  29. #include <linux/device.h>
  30. #include <linux/err.h>
  31. #include <linux/of_device.h>
  32. #include <linux/slab.h>
  33. #include <linux/pt_platform.h>
  34. #include "pt_regs.h"
  35. #define MAX_NAME_LENGTH 64
  36. static bool is_create_and_get_pdata;
  37. enum pt_device_type {
  38. DEVICE_MT,
  39. DEVICE_BTN,
  40. DEVICE_PROXIMITY,
  41. DEVICE_TYPE_MAX,
  42. };
  43. struct pt_device_pdata_func {
  44. void * (*create_and_get_pdata)(struct device_node *dn);
  45. void (*free_pdata)(void *ptr);
  46. };
  47. struct pt_pdata_ptr {
  48. void **pdata;
  49. };
  50. #ifdef ENABLE_VIRTUAL_KEYS
  51. static struct kobject *board_properties_kobj;
  52. struct pt_virtual_keys {
  53. struct kobj_attribute kobj_attr;
  54. u16 *data;
  55. int size;
  56. };
  57. #endif
  58. struct pt_extended_mt_platform_data {
  59. struct pt_mt_platform_data pdata;
  60. #ifdef ENABLE_VIRTUAL_KEYS
  61. struct pt_virtual_keys vkeys;
  62. #endif
  63. };
  64. /*******************************************************************************
  65. * FUNCTION: get_inp_dev_name
  66. *
  67. * SUMMARY: Get the name of input device from dts.
  68. *
  69. * RETURN:
  70. * 0 = success
  71. * !0 = failure
  72. *
  73. * PARAMETERS:
  74. * *dev_node - pointer to device_node structure
  75. * **inp_dev_name - double pointer to the name of input device
  76. ******************************************************************************/
  77. static inline int get_inp_dev_name(struct device_node *dev_node,
  78. const char **inp_dev_name)
  79. {
  80. return of_property_read_string(dev_node, "parade,inp_dev_name",
  81. inp_dev_name);
  82. }
  83. /*******************************************************************************
  84. * FUNCTION: create_and_get_u16_array
  85. *
  86. * SUMMARY: Create and get u16 array from dts.
  87. *
  88. * RETURN:
  89. * success: the pointer of the created array
  90. * fail : error code with type of error pointer
  91. *
  92. * PARAMETERS:
  93. * *dev_node - pointer to device_node structure
  94. * *name - name of device node
  95. * size - number of u16 array elements
  96. ******************************************************************************/
  97. static s16 *create_and_get_u16_array(struct device_node *dev_node,
  98. const char *name, int *size)
  99. {
  100. const __be32 *values;
  101. s16 *val_array;
  102. int len;
  103. int sz;
  104. int rc;
  105. int i;
  106. values = of_get_property(dev_node, name, &len);
  107. if (values == NULL)
  108. return NULL;
  109. sz = len / sizeof(u32);
  110. pr_debug("%s: %s size:%d\n", __func__, name, sz);
  111. val_array = kcalloc(sz, sizeof(s16), GFP_KERNEL);
  112. if (!val_array) {
  113. rc = -ENOMEM;
  114. goto fail;
  115. }
  116. for (i = 0; i < sz; i++)
  117. val_array[i] = (s16)be32_to_cpup(values++);
  118. *size = sz;
  119. return val_array;
  120. fail:
  121. return ERR_PTR(rc);
  122. }
  123. /*******************************************************************************
  124. * FUNCTION: create_and_get_touch_framework
  125. *
  126. * SUMMARY: Create and get touch framework structure from dts.
  127. *
  128. * RETURN:
  129. * success: the pointer of the touch framework data
  130. * fail : error code with type of error pointer
  131. *
  132. * PARAMETERS:
  133. * *dev_node - pointer to device_node structure
  134. ******************************************************************************/
  135. static struct touch_framework *create_and_get_touch_framework(
  136. struct device_node *dev_node)
  137. {
  138. struct touch_framework *frmwrk;
  139. s16 *abs;
  140. int size;
  141. int rc;
  142. abs = create_and_get_u16_array(dev_node, "parade,abs", &size);
  143. if (IS_ERR_OR_NULL(abs))
  144. return (void *)abs;
  145. /* Check for valid abs size */
  146. if (size % PT_NUM_ABS_SET) {
  147. rc = -EINVAL;
  148. goto fail_free_abs;
  149. }
  150. frmwrk = kzalloc(sizeof(*frmwrk), GFP_KERNEL);
  151. if (!frmwrk) {
  152. rc = -ENOMEM;
  153. goto fail_free_abs;
  154. }
  155. frmwrk->abs = abs;
  156. frmwrk->size = size;
  157. return frmwrk;
  158. fail_free_abs:
  159. kfree(abs);
  160. return ERR_PTR(rc);
  161. }
  162. /*******************************************************************************
  163. * FUNCTION: free_touch_framework
  164. *
  165. * SUMMARY: Free all the pointer of touch framework structure.
  166. *
  167. * PARAMETERS:
  168. * *frmwrk - pointer to touch framework structure
  169. ******************************************************************************/
  170. static void free_touch_framework(struct touch_framework *frmwrk)
  171. {
  172. kfree(frmwrk->abs);
  173. kfree(frmwrk);
  174. }
  175. #ifdef ENABLE_VIRTUAL_KEYS
  176. #define VIRTUAL_KEY_ELEMENT_SIZE 5
  177. /*******************************************************************************
  178. * FUNCTION: virtual_keys_show
  179. *
  180. * SUMMARY: Show method for the board_properties sysfs node that will show the
  181. * information for all virtual keys
  182. *
  183. * RETURN: size of data written to sysfs node
  184. *
  185. * PARAMETERS:
  186. * *kobj - pointer to kobject structure
  187. * *attr - pointer to kobject attributes
  188. * *buf - pointer to print output buffer
  189. ******************************************************************************/
  190. static ssize_t virtual_keys_show(struct kobject *kobj,
  191. struct kobj_attribute *attr, char *buf)
  192. {
  193. struct pt_virtual_keys *vkeys = container_of(attr,
  194. struct pt_virtual_keys, kobj_attr);
  195. u16 *data = vkeys->data;
  196. int size = vkeys->size;
  197. int index;
  198. int i;
  199. index = 0;
  200. for (i = 0; i < size; i += VIRTUAL_KEY_ELEMENT_SIZE)
  201. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  202. "0x01:%d:%d:%d:%d:%d\n",
  203. data[i], data[i+1], data[i+2], data[i+3], data[i+4]);
  204. return index;
  205. }
  206. /*******************************************************************************
  207. * FUNCTION: setup_virtual_keys
  208. *
  209. * SUMMARY: Create virtual key data array from dts and set up dynamic sysfs for
  210. * board_properties node.
  211. *
  212. * RETURN:
  213. * 0 = success
  214. * !0 = failure
  215. *
  216. * PARAMETERS:
  217. * *dev_node - pointer to device_node structure
  218. * **inp_dev_name - double pointer to the name of input device
  219. * *vkeys - pointer to virtual key structure
  220. ******************************************************************************/
  221. static int setup_virtual_keys(struct device_node *dev_node,
  222. const char *inp_dev_name, struct pt_virtual_keys *vkeys)
  223. {
  224. char *name;
  225. u16 *data;
  226. int size;
  227. int rc;
  228. data = create_and_get_u16_array(dev_node, "parade,virtual_keys", &size);
  229. if (data == NULL)
  230. return 0;
  231. else if (IS_ERR(data)) {
  232. rc = PTR_ERR(data);
  233. goto fail;
  234. }
  235. /* Check for valid virtual keys size */
  236. if (size % VIRTUAL_KEY_ELEMENT_SIZE) {
  237. rc = -EINVAL;
  238. goto fail_free_data;
  239. }
  240. name = kzalloc(MAX_NAME_LENGTH, GFP_KERNEL);
  241. if (!name) {
  242. rc = -ENOMEM;
  243. goto fail_free_data;
  244. }
  245. snprintf(name, MAX_NAME_LENGTH, "virtualkeys.%s", inp_dev_name);
  246. vkeys->data = data;
  247. vkeys->size = size;
  248. /* TODO: Instantiate in board file and export it */
  249. if (board_properties_kobj == NULL)
  250. board_properties_kobj =
  251. kobject_create_and_add("board_properties", NULL);
  252. if (board_properties_kobj == NULL) {
  253. pr_err("%s: Cannot get board_properties kobject!\n", __func__);
  254. rc = -EINVAL;
  255. goto fail_free_name;
  256. }
  257. /* Initialize dynamic SysFs attribute */
  258. sysfs_attr_init(&vkeys->kobj_attr.attr);
  259. vkeys->kobj_attr.attr.name = name;
  260. vkeys->kobj_attr.attr.mode = 0444;
  261. vkeys->kobj_attr.show = virtual_keys_show;
  262. rc = sysfs_create_file(board_properties_kobj, &vkeys->kobj_attr.attr);
  263. if (rc)
  264. goto fail_del_kobj;
  265. return 0;
  266. fail_del_kobj:
  267. kobject_del(board_properties_kobj);
  268. fail_free_name:
  269. kfree(name);
  270. vkeys->kobj_attr.attr.name = NULL;
  271. fail_free_data:
  272. kfree(data);
  273. vkeys->data = NULL;
  274. fail:
  275. return rc;
  276. }
  277. /*******************************************************************************
  278. * FUNCTION: free_virtual_keys
  279. *
  280. * SUMMARY: Remove board_properties node and free all pointers.
  281. *
  282. * PARAMETERS:
  283. * *vkeys - pointer to virtual key structure
  284. ******************************************************************************/
  285. static void free_virtual_keys(struct pt_virtual_keys *vkeys)
  286. {
  287. if (board_properties_kobj)
  288. sysfs_remove_file(board_properties_kobj,
  289. &vkeys->kobj_attr.attr);
  290. kobject_del(board_properties_kobj);
  291. board_properties_kobj = NULL;
  292. kfree(vkeys->data);
  293. kfree(vkeys->kobj_attr.attr.name);
  294. }
  295. #endif
  296. /*******************************************************************************
  297. * FUNCTION: create_and_get_mt_pdata
  298. *
  299. * SUMMARY: Create and get touch platform data from dts.Touch framework and
  300. * virtual keys are set up in this function.
  301. *
  302. * RETURN:
  303. * success: the pointer of the platform data
  304. * fail : error code with type of error pointer
  305. *
  306. * PARAMETERS:
  307. * *dev_node - pointer to device_node structure
  308. ******************************************************************************/
  309. static void *create_and_get_mt_pdata(struct device_node *dev_node)
  310. {
  311. struct pt_extended_mt_platform_data *ext_pdata;
  312. struct pt_mt_platform_data *pdata;
  313. u32 value;
  314. int rc;
  315. ext_pdata = kzalloc(sizeof(*ext_pdata), GFP_KERNEL);
  316. if (!ext_pdata) {
  317. rc = -ENOMEM;
  318. goto fail;
  319. }
  320. pdata = &ext_pdata->pdata;
  321. rc = get_inp_dev_name(dev_node, &pdata->inp_dev_name);
  322. if (rc)
  323. goto fail_free_pdata;
  324. /* Optional fields */
  325. rc = of_property_read_u32(dev_node, "parade,flags", &value);
  326. if (!rc)
  327. pdata->flags = value;
  328. rc = of_property_read_u32(dev_node, "parade,vkeys_x", &value);
  329. if (!rc)
  330. pdata->vkeys_x = value;
  331. rc = of_property_read_u32(dev_node, "parade,vkeys_y", &value);
  332. if (!rc)
  333. pdata->vkeys_y = value;
  334. /* Required fields */
  335. pdata->frmwrk = create_and_get_touch_framework(dev_node);
  336. if (pdata->frmwrk == NULL) {
  337. rc = -EINVAL;
  338. goto fail_free_pdata;
  339. } else if (IS_ERR(pdata->frmwrk)) {
  340. rc = PTR_ERR(pdata->frmwrk);
  341. goto fail_free_pdata;
  342. }
  343. #ifdef ENABLE_VIRTUAL_KEYS
  344. rc = setup_virtual_keys(dev_node, pdata->inp_dev_name,
  345. &ext_pdata->vkeys);
  346. if (rc) {
  347. pr_err("%s: Cannot setup virtual keys!\n", __func__);
  348. goto fail_free_pdata;
  349. }
  350. #endif
  351. return pdata;
  352. fail_free_pdata:
  353. kfree(ext_pdata);
  354. fail:
  355. return ERR_PTR(rc);
  356. }
  357. /*******************************************************************************
  358. * FUNCTION: free_mt_pdata
  359. *
  360. * SUMMARY: Free all pointers that include touch framework, virtual keys and
  361. * touch data.
  362. *
  363. * PARAMETERS:
  364. * *vkeys - pointer to virtual key structure
  365. ******************************************************************************/
  366. static void free_mt_pdata(void *pdata)
  367. {
  368. struct pt_mt_platform_data *mt_pdata =
  369. (struct pt_mt_platform_data *)pdata;
  370. struct pt_extended_mt_platform_data *ext_mt_pdata =
  371. container_of(mt_pdata,
  372. struct pt_extended_mt_platform_data, pdata);
  373. free_touch_framework(mt_pdata->frmwrk);
  374. #ifdef ENABLE_VIRTUAL_KEYS
  375. free_virtual_keys(&ext_mt_pdata->vkeys);
  376. #endif
  377. kfree(ext_mt_pdata);
  378. }
  379. /*******************************************************************************
  380. * FUNCTION: create_and_get_btn_pdata
  381. *
  382. * SUMMARY: Create and get button platform data from dts.
  383. *
  384. * RETURN:
  385. * success: the pointer of the platform data
  386. * fail : error code with type of error pointer
  387. *
  388. * PARAMETERS:
  389. * *dev_node - pointer to device_node structure
  390. ******************************************************************************/
  391. static void *create_and_get_btn_pdata(struct device_node *dev_node)
  392. {
  393. struct pt_btn_platform_data *pdata;
  394. int rc;
  395. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  396. if (!pdata) {
  397. rc = -ENOMEM;
  398. goto fail;
  399. }
  400. rc = get_inp_dev_name(dev_node, &pdata->inp_dev_name);
  401. if (rc)
  402. goto fail_free_pdata;
  403. return pdata;
  404. fail_free_pdata:
  405. kfree(pdata);
  406. fail:
  407. return ERR_PTR(rc);
  408. }
  409. /*******************************************************************************
  410. * FUNCTION: free_btn_pdata
  411. *
  412. * SUMMARY: Free all pointers for button platform data.
  413. *
  414. * PARAMETERS:
  415. * *vkeys - pointer to virtual key structure
  416. ******************************************************************************/
  417. static void free_btn_pdata(void *pdata)
  418. {
  419. struct pt_btn_platform_data *btn_pdata =
  420. (struct pt_btn_platform_data *)pdata;
  421. kfree(btn_pdata);
  422. }
  423. /*******************************************************************************
  424. * FUNCTION: create_and_get_proximity_pdata
  425. *
  426. * SUMMARY: Create and get proximity platform data from dts.
  427. *
  428. * RETURN:
  429. * success: the pointer of the proximity platform data
  430. * fail : error code with type of error pointer
  431. *
  432. * PARAMETERS:
  433. * *dev_node - pointer to device_node structure
  434. ******************************************************************************/
  435. static void *create_and_get_proximity_pdata(struct device_node *dev_node)
  436. {
  437. struct pt_proximity_platform_data *pdata;
  438. int rc;
  439. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  440. if (!pdata) {
  441. rc = -ENOMEM;
  442. goto fail;
  443. }
  444. rc = get_inp_dev_name(dev_node, &pdata->inp_dev_name);
  445. if (rc)
  446. goto fail_free_pdata;
  447. pdata->frmwrk = create_and_get_touch_framework(dev_node);
  448. if (pdata->frmwrk == NULL) {
  449. rc = -EINVAL;
  450. goto fail_free_pdata;
  451. } else if (IS_ERR(pdata->frmwrk)) {
  452. rc = PTR_ERR(pdata->frmwrk);
  453. goto fail_free_pdata;
  454. }
  455. return pdata;
  456. fail_free_pdata:
  457. kfree(pdata);
  458. fail:
  459. return ERR_PTR(rc);
  460. }
  461. /*******************************************************************************
  462. * FUNCTION: free_proximity_pdata
  463. *
  464. * SUMMARY: Free all pointers for proximity platform data.
  465. *
  466. * PARAMETERS:
  467. * *vkeys - pointer to virtual key structure
  468. ******************************************************************************/
  469. static void free_proximity_pdata(void *pdata)
  470. {
  471. struct pt_proximity_platform_data *proximity_pdata =
  472. (struct pt_proximity_platform_data *)pdata;
  473. free_touch_framework(proximity_pdata->frmwrk);
  474. kfree(proximity_pdata);
  475. }
  476. static struct pt_device_pdata_func device_pdata_funcs[DEVICE_TYPE_MAX] = {
  477. [DEVICE_MT] = {
  478. .create_and_get_pdata = create_and_get_mt_pdata,
  479. .free_pdata = free_mt_pdata,
  480. },
  481. [DEVICE_BTN] = {
  482. .create_and_get_pdata = create_and_get_btn_pdata,
  483. .free_pdata = free_btn_pdata,
  484. },
  485. [DEVICE_PROXIMITY] = {
  486. .create_and_get_pdata = create_and_get_proximity_pdata,
  487. .free_pdata = free_proximity_pdata,
  488. },
  489. };
  490. static struct pt_pdata_ptr pdata_ptr[DEVICE_TYPE_MAX];
  491. static const char *device_names[DEVICE_TYPE_MAX] = {
  492. [DEVICE_MT] = "parade,mt",
  493. [DEVICE_BTN] = "parade,btn",
  494. [DEVICE_PROXIMITY] = "parade,proximity",
  495. };
  496. /*******************************************************************************
  497. * FUNCTION: set_pdata_ptr
  498. *
  499. * SUMMARY: Set platform data pointer for touch, button, proximity module.
  500. *
  501. * PARAMETERS:
  502. * *pdata - pointer to platform data structure
  503. ******************************************************************************/
  504. static void set_pdata_ptr(struct pt_platform_data *pdata)
  505. {
  506. pdata_ptr[DEVICE_MT].pdata = (void **)&pdata->mt_pdata;
  507. pdata_ptr[DEVICE_BTN].pdata = (void **)&pdata->btn_pdata;
  508. pdata_ptr[DEVICE_PROXIMITY].pdata = (void **)&pdata->prox_pdata;
  509. }
  510. /*******************************************************************************
  511. * FUNCTION: get_device_type
  512. *
  513. * SUMMARY: Determine the device type[mt,btn,proximity] from dts.
  514. *
  515. * RETURN:
  516. * 0 = success
  517. * !0 = failure
  518. *
  519. * PARAMETERS:
  520. * *dev_node - pointer to device_node structure
  521. * *type - pointer to store the device type
  522. ******************************************************************************/
  523. static int get_device_type(struct device_node *dev_node,
  524. enum pt_device_type *type)
  525. {
  526. const char *name;
  527. enum pt_device_type t;
  528. int rc;
  529. rc = of_property_read_string(dev_node, "name", &name);
  530. if (rc)
  531. return rc;
  532. for (t = 0; t < DEVICE_TYPE_MAX; t++)
  533. if (!strncmp(name, device_names[t], MAX_NAME_LENGTH)) {
  534. *type = t;
  535. return 0;
  536. }
  537. return -EINVAL;
  538. }
  539. /*******************************************************************************
  540. * FUNCTION: create_and_get_device_pdata
  541. *
  542. * SUMMARY: Create platform data for mt, btn, proximity module.
  543. *
  544. * RETURN:
  545. * success: the pointer of the platform data
  546. * fail : error code with type of error pointer
  547. *
  548. * PARAMETERS:
  549. * *dev_node - pointer to device_node structure
  550. * type - determine the device type
  551. ******************************************************************************/
  552. static inline void *create_and_get_device_pdata(struct device_node *dev_node,
  553. enum pt_device_type type)
  554. {
  555. return device_pdata_funcs[type].create_and_get_pdata(dev_node);
  556. }
  557. /*******************************************************************************
  558. * FUNCTION: free_device_pdata
  559. *
  560. * SUMMARY: Free platform data for mt, btn, proximity module.
  561. *
  562. * PARAMETERS:
  563. * type - determine the device type
  564. ******************************************************************************/
  565. static inline void free_device_pdata(enum pt_device_type type)
  566. {
  567. device_pdata_funcs[type].free_pdata(*pdata_ptr[type].pdata);
  568. }
  569. /*******************************************************************************
  570. * FUNCTION: create_and_get_touch_setting
  571. *
  572. * SUMMARY: Create and get touch settings from dts.
  573. *
  574. * RETURN:
  575. * success: the pointer of touch settings
  576. * fail : error code with type of error pointer
  577. *
  578. * PARAMETERS:
  579. * *core_node - pointer to device_node structure
  580. * name - name of touch setting
  581. ******************************************************************************/
  582. static struct touch_settings *create_and_get_touch_setting(
  583. struct device_node *core_node, const char *name)
  584. {
  585. struct touch_settings *setting;
  586. char *tag_name;
  587. u32 tag_value;
  588. u16 *data;
  589. int size;
  590. int rc;
  591. data = create_and_get_u16_array(core_node, name, &size);
  592. if (IS_ERR_OR_NULL(data))
  593. return (void *)data;
  594. pr_debug("%s: Touch setting:'%s' size:%d\n", __func__, name, size);
  595. setting = kzalloc(sizeof(*setting), GFP_KERNEL);
  596. if (!setting) {
  597. rc = -ENOMEM;
  598. goto fail_free_data;
  599. }
  600. setting->data = (u8 *)data;
  601. setting->size = size;
  602. tag_name = kzalloc(MAX_NAME_LENGTH, GFP_KERNEL);
  603. if (!tag_name) {
  604. rc = -ENOMEM;
  605. goto fail_free_setting;
  606. }
  607. snprintf(tag_name, MAX_NAME_LENGTH, "%s-tag", name);
  608. rc = of_property_read_u32(core_node, tag_name, &tag_value);
  609. if (!rc)
  610. setting->tag = tag_value;
  611. kfree(tag_name);
  612. return setting;
  613. fail_free_setting:
  614. kfree(setting);
  615. fail_free_data:
  616. kfree(data);
  617. return ERR_PTR(rc);
  618. }
  619. /*******************************************************************************
  620. * FUNCTION: free_touch_setting
  621. *
  622. * SUMMARY: Free touch setting data.
  623. *
  624. * PARAMETERS:
  625. * setting - pointer to touch setting
  626. ******************************************************************************/
  627. static void free_touch_setting(struct touch_settings *setting)
  628. {
  629. if (setting) {
  630. kfree(setting->data);
  631. kfree(setting);
  632. }
  633. }
  634. static char *touch_setting_names[PT_IC_GRPNUM_NUM] = {
  635. NULL, /* PT_IC_GRPNUM_RESERVED */
  636. "parade,cmd_regs", /* PT_IC_GRPNUM_CMD_REGS */
  637. "parade,tch_rep", /* PT_IC_GRPNUM_TCH_REP */
  638. "parade,data_rec", /* PT_IC_GRPNUM_DATA_REC */
  639. "parade,test_rec", /* PT_IC_GRPNUM_TEST_REC */
  640. "parade,pcfg_rec", /* PT_IC_GRPNUM_PCFG_REC */
  641. "parade,tch_parm_val", /* PT_IC_GRPNUM_TCH_PARM_VAL */
  642. "parade,tch_parm_size", /* PT_IC_GRPNUM_TCH_PARM_SIZE */
  643. NULL, /* PT_IC_GRPNUM_RESERVED1 */
  644. NULL, /* PT_IC_GRPNUM_RESERVED2 */
  645. "parade,opcfg_rec", /* PT_IC_GRPNUM_OPCFG_REC */
  646. "parade,ddata_rec", /* PT_IC_GRPNUM_DDATA_REC */
  647. "parade,mdata_rec", /* PT_IC_GRPNUM_MDATA_REC */
  648. "parade,test_regs", /* PT_IC_GRPNUM_TEST_REGS */
  649. "parade,btn_keys", /* PT_IC_GRPNUM_BTN_KEYS */
  650. NULL, /* PT_IC_GRPNUM_TTHE_REGS */
  651. };
  652. /*******************************************************************************
  653. * FUNCTION: create_and_get_core_pdata
  654. *
  655. * SUMMARY: Create and get core module platform data from dts.
  656. *
  657. * RETURN:
  658. * success: the pointer of core platform data
  659. * fail : error code with type of error pointer
  660. *
  661. * PARAMETERS:
  662. * *core_node - pointer to device_node structure
  663. ******************************************************************************/
  664. static struct pt_core_platform_data *create_and_get_core_pdata(
  665. struct device_node *core_node)
  666. {
  667. struct pt_core_platform_data *pdata;
  668. u32 value;
  669. int rc;
  670. int i;
  671. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  672. if (!pdata) {
  673. rc = -ENOMEM;
  674. goto fail;
  675. }
  676. /* Required fields */
  677. value = of_get_named_gpio_flags(core_node, "parade,irq_gpio", 0, &pdata->irq_gpio_flags);
  678. pdata->irq_gpio = value;
  679. rc = of_property_read_u32(core_node, "parade,hid_desc_register",
  680. &value);
  681. if (rc)
  682. goto fail_free;
  683. pdata->hid_desc_register = value;
  684. /* Optional fields */
  685. /* rst_gpio is optional since a platform may use
  686. * power cycling instead of using the XRES pin
  687. */
  688. value = of_get_named_gpio_flags(core_node, "parade,rst_gpio", 0, &pdata->rst_gpio_flags);
  689. pdata->rst_gpio = value;
  690. rc = of_property_read_u32(core_node, "parade,ddi_rst_gpio", &value);
  691. if (!rc)
  692. pdata->ddi_rst_gpio = value;
  693. rc = of_property_read_u32(core_node, "parade,vddi_gpio", &value);
  694. if (!rc)
  695. pdata->vddi_gpio = value;
  696. rc = of_property_read_u32(core_node, "parade,vcc_gpio", &value);
  697. if (!rc)
  698. pdata->vcc_gpio = value;
  699. rc = of_property_read_u32(core_node, "parade,avdd_gpio", &value);
  700. if (!rc)
  701. pdata->avdd_gpio = value;
  702. rc = of_property_read_u32(core_node, "parade,avee_gpio", &value);
  703. if (!rc)
  704. pdata->avee_gpio = value;
  705. rc = of_property_read_u32(core_node, "parade,level_irq_udelay", &value);
  706. if (!rc)
  707. pdata->level_irq_udelay = value;
  708. rc = of_property_read_u32(core_node, "parade,vendor_id", &value);
  709. if (!rc)
  710. pdata->vendor_id = value;
  711. rc = of_property_read_u32(core_node, "parade,product_id", &value);
  712. if (!rc)
  713. pdata->product_id = value;
  714. rc = of_property_read_u32(core_node, "parade,flags", &value);
  715. if (!rc)
  716. pdata->flags = value;
  717. rc = of_property_read_u32(core_node, "parade,easy_wakeup_gesture",
  718. &value);
  719. if (!rc)
  720. pdata->easy_wakeup_gesture = (u8)value;
  721. rc = of_property_read_u32(core_node, "parade,config_dut_generation",
  722. &value);
  723. if (!rc)
  724. pdata->config_dut_generation = (u8)value;
  725. else {
  726. pr_err("%s: dut_generation is not configured, set default: DUT_PIP2_CAPABLE!\n",
  727. __func__);
  728. pdata->config_dut_generation = CONFIG_DUT_PIP2_CAPABLE;
  729. }
  730. rc = of_property_read_u32(core_node, "parade,watchdog_force_stop",
  731. &value);
  732. if (!rc) {
  733. if (value)
  734. pdata->watchdog_force_stop = true;
  735. else
  736. pdata->watchdog_force_stop = false;
  737. } else {
  738. pr_err("%s: watchdog_force_stop is not configured, set default: false!\n",
  739. __func__);
  740. pdata->watchdog_force_stop = false;
  741. }
  742. rc = of_property_read_u32(core_node, "parade,panel_id_support",
  743. &value);
  744. if (!rc) {
  745. pdata->panel_id_support = (u8)value;
  746. } else {
  747. pr_err("%s: panel_id_support is not configured, set default: PT_PANEL_ID_DISABLE\n",
  748. __func__);
  749. pdata->panel_id_support = PT_PANEL_ID_DISABLE;
  750. }
  751. for (i = 0; (unsigned int)i < ARRAY_SIZE(touch_setting_names); i++) {
  752. if (touch_setting_names[i] == NULL)
  753. continue;
  754. pdata->sett[i] = create_and_get_touch_setting(core_node,
  755. touch_setting_names[i]);
  756. if (IS_ERR(pdata->sett[i])) {
  757. rc = PTR_ERR(pdata->sett[i]);
  758. goto fail_free_sett;
  759. } else if (pdata->sett[i] == NULL)
  760. pr_debug("%s: No data for setting '%s'\n", __func__,
  761. touch_setting_names[i]);
  762. }
  763. pr_debug("%s: irq_gpio:%d rst_gpio:%d\n"
  764. "hid_desc_reg:%d level_irq_udelay:%d vendor_id:%d prod_id:%d\n"
  765. "flags:%d easy_wakeup_gesture:%d\n", __func__,
  766. pdata->irq_gpio, pdata->rst_gpio,
  767. pdata->hid_desc_register,
  768. pdata->level_irq_udelay, pdata->vendor_id, pdata->product_id,
  769. pdata->flags, pdata->easy_wakeup_gesture);
  770. pdata->xres = pt_xres;
  771. pdata->init = pt_init;
  772. pdata->power = pt_power;
  773. pdata->detect = pt_detect;
  774. pdata->irq_stat = pt_irq_stat;
  775. pdata->setup_power = pt_setup_power;
  776. pdata->setup_irq = pt_setup_irq;
  777. return pdata;
  778. fail_free_sett:
  779. for (i--; i >= 0; i--)
  780. free_touch_setting(pdata->sett[i]);
  781. fail_free:
  782. kfree(pdata);
  783. fail:
  784. return ERR_PTR(rc);
  785. }
  786. /*******************************************************************************
  787. * FUNCTION: free_core_pdata
  788. *
  789. * SUMMARY: Free the core module platform data and touch settings data.
  790. *
  791. * RETURN:
  792. * success: the pointer of core platform data
  793. * fail : error code with type of error pointer
  794. *
  795. * PARAMETERS:
  796. * *core_node - pointer to device_node structure
  797. ******************************************************************************/
  798. static void free_core_pdata(void *pdata)
  799. {
  800. struct pt_core_platform_data *core_pdata = pdata;
  801. unsigned int i;
  802. for (i = 0; i < ARRAY_SIZE(touch_setting_names); i++)
  803. free_touch_setting(core_pdata->sett[i]);
  804. kfree(core_pdata);
  805. }
  806. /*******************************************************************************
  807. * FUNCTION: pt_devtree_create_and_get_pdata
  808. *
  809. * SUMMARY: Parse dts and set up platform data for core module, multi-touch(mt)
  810. * module, button(btn) module, proximity module.And Assign platform data
  811. * pointer for loader module.
  812. *
  813. * RETURN:
  814. * 0 = success
  815. * !0 = failure
  816. *
  817. * PARAMETERS:
  818. * *adap_dev - pointer to device structure
  819. ******************************************************************************/
  820. int pt_devtree_create_and_get_pdata(struct device *adap_dev)
  821. {
  822. struct pt_platform_data *pdata;
  823. struct device_node *core_node, *dev_node, *dev_node_fail;
  824. enum pt_device_type type;
  825. int count = 0;
  826. int rc = 0;
  827. if (is_create_and_get_pdata == true)
  828. return 0;
  829. if (!adap_dev->of_node)
  830. return 0;
  831. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  832. if (!pdata)
  833. return -ENOMEM;
  834. adap_dev->platform_data = pdata;
  835. set_pdata_ptr(pdata);
  836. /* There should be only one core node */
  837. for_each_child_of_node(adap_dev->of_node, core_node) {
  838. const char *name;
  839. rc = of_property_read_string(core_node, "name", &name);
  840. if (!rc)
  841. pr_debug("%s: name:%s\n", __func__, name);
  842. pdata->core_pdata = create_and_get_core_pdata(core_node);
  843. if (IS_ERR(pdata->core_pdata)) {
  844. rc = PTR_ERR(pdata->core_pdata);
  845. break;
  846. }
  847. /* Increment reference count */
  848. of_node_get(core_node);
  849. for_each_child_of_node(core_node, dev_node) {
  850. count++;
  851. rc = get_device_type(dev_node, &type);
  852. if (rc)
  853. break;
  854. *pdata_ptr[type].pdata
  855. = create_and_get_device_pdata(dev_node, type);
  856. if (IS_ERR(*pdata_ptr[type].pdata))
  857. rc = PTR_ERR(*pdata_ptr[type].pdata);
  858. if (rc)
  859. break;
  860. /* Increment reference count */
  861. of_node_get(dev_node);
  862. }
  863. if (rc) {
  864. free_core_pdata(pdata->core_pdata);
  865. of_node_put(core_node);
  866. for_each_child_of_node(core_node, dev_node_fail) {
  867. if (dev_node == dev_node_fail)
  868. break;
  869. rc = get_device_type(dev_node, &type);
  870. if (rc)
  871. break;
  872. free_device_pdata(type);
  873. of_node_put(dev_node);
  874. }
  875. break;
  876. }
  877. pdata->loader_pdata = &_pt_loader_platform_data;
  878. }
  879. is_create_and_get_pdata = true;
  880. pr_debug("%s: %d child node(s) found\n", __func__, count);
  881. return rc;
  882. }
  883. EXPORT_SYMBOL_GPL(pt_devtree_create_and_get_pdata);
  884. /*******************************************************************************
  885. * FUNCTION: pt_devtree_clean_pdata
  886. *
  887. * SUMMARY: Free all platform data for core module, multi-touch(mt) module,
  888. * button(btn) module, proximity module.
  889. *
  890. * RETURN:
  891. * 0 = success
  892. * !0 = failure
  893. *
  894. * PARAMETERS:
  895. * *adap_dev - pointer to device structure
  896. ******************************************************************************/
  897. int pt_devtree_clean_pdata(struct device *adap_dev)
  898. {
  899. struct pt_platform_data *pdata;
  900. struct device_node *core_node, *dev_node;
  901. enum pt_device_type type;
  902. int rc = 0;
  903. if (is_create_and_get_pdata == false)
  904. return 0;
  905. if (!adap_dev->of_node)
  906. return 0;
  907. pdata = dev_get_platdata(adap_dev);
  908. set_pdata_ptr(pdata);
  909. for_each_child_of_node(adap_dev->of_node, core_node) {
  910. free_core_pdata(pdata->core_pdata);
  911. of_node_put(core_node);
  912. for_each_child_of_node(core_node, dev_node) {
  913. rc = get_device_type(dev_node, &type);
  914. if (rc)
  915. break;
  916. free_device_pdata(type);
  917. of_node_put(dev_node);
  918. }
  919. }
  920. is_create_and_get_pdata = false;
  921. return rc;
  922. }
  923. EXPORT_SYMBOL_GPL(pt_devtree_clean_pdata);
  924. MODULE_LICENSE("GPL");
  925. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product DeviceTree Driver");
  926. MODULE_AUTHOR("Parade Technologies <[email protected]>");