pt_devtree.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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 "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: pt_check_dsi_panel_dt
  654. *
  655. * SUMMARY: Get the DSI active panel information from dtsi
  656. *
  657. * RETURN:
  658. * 0 = success
  659. * !0 = fail
  660. *
  661. * PARAMETERS:
  662. * np - pointer to device_node structure
  663. * active_panel - name of active DSI panel
  664. ******************************************************************************/
  665. static int pt_check_dsi_panel_dt(struct device_node *np, struct drm_panel **active_panel)
  666. {
  667. int i;
  668. int count;
  669. struct device_node *node;
  670. struct drm_panel *panel;
  671. count = of_count_phandle_with_args(np, "panel", NULL);
  672. pr_info("%s: Active panel count: %d\n", __func__, count);
  673. if (count <= 0)
  674. return 0;
  675. for (i = 0; i < count; i++) {
  676. node = of_parse_phandle(np, "panel", i);
  677. if (node != NULL)
  678. pr_info("%s: Node handle successfully parsed !\n", __func__);
  679. panel = of_drm_find_panel(node);
  680. of_node_put(node);
  681. if (!IS_ERR(panel)) {
  682. pr_info("%s: Active panel selected !\n", __func__);
  683. *active_panel = panel;
  684. return 0;
  685. }
  686. }
  687. pr_err("%s: Active panel NOT selected !\n", __func__);
  688. return PTR_ERR(panel);
  689. }
  690. /*******************************************************************************
  691. * FUNCTION: create_and_get_core_pdata
  692. *
  693. * SUMMARY: Create and get core module platform data from dts.
  694. *
  695. * RETURN:
  696. * success: the pointer of core platform data
  697. * fail : error code with type of error pointer
  698. *
  699. * PARAMETERS:
  700. * *core_node - pointer to device_node structure
  701. ******************************************************************************/
  702. static struct pt_core_platform_data *create_and_get_core_pdata(
  703. struct device_node *core_node)
  704. {
  705. struct pt_core_platform_data *pdata;
  706. u32 value;
  707. int rc;
  708. int i;
  709. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  710. if (!pdata) {
  711. rc = -ENOMEM;
  712. goto fail;
  713. }
  714. /* Required fields */
  715. value = of_get_named_gpio_flags(core_node, "parade,irq_gpio", 0, &pdata->irq_gpio_flags);
  716. pdata->irq_gpio = value;
  717. rc = of_property_read_u32(core_node, "parade,hid_desc_register",
  718. &value);
  719. if (rc)
  720. goto fail_free;
  721. pdata->hid_desc_register = value;
  722. /* Optional fields */
  723. /* rst_gpio is optional since a platform may use
  724. * power cycling instead of using the XRES pin
  725. */
  726. value = of_get_named_gpio_flags(core_node, "parade,rst_gpio", 0, &pdata->rst_gpio_flags);
  727. pdata->rst_gpio = value;
  728. rc = of_property_read_u32(core_node, "parade,ddi_rst_gpio", &value);
  729. if (!rc)
  730. pdata->ddi_rst_gpio = value;
  731. rc = of_property_read_u32(core_node, "parade,vddi_gpio", &value);
  732. if (!rc)
  733. pdata->vddi_gpio = value;
  734. rc = of_property_read_u32(core_node, "parade,vcc_gpio", &value);
  735. if (!rc)
  736. pdata->vcc_gpio = value;
  737. rc = of_property_read_u32(core_node, "parade,avdd_gpio", &value);
  738. if (!rc)
  739. pdata->avdd_gpio = value;
  740. rc = of_property_read_u32(core_node, "parade,avee_gpio", &value);
  741. if (!rc)
  742. pdata->avee_gpio = value;
  743. rc = of_property_read_u32(core_node, "parade,level_irq_udelay", &value);
  744. if (!rc)
  745. pdata->level_irq_udelay = value;
  746. rc = of_property_read_u32(core_node, "parade,vendor_id", &value);
  747. if (!rc)
  748. pdata->vendor_id = value;
  749. rc = of_property_read_u32(core_node, "parade,product_id", &value);
  750. if (!rc)
  751. pdata->product_id = value;
  752. rc = of_property_read_u32(core_node, "parade,flags", &value);
  753. if (!rc)
  754. pdata->flags = value;
  755. rc = of_property_read_u32(core_node, "parade,easy_wakeup_gesture",
  756. &value);
  757. if (!rc)
  758. pdata->easy_wakeup_gesture = (u8)value;
  759. rc = of_property_read_u32(core_node, "parade,config_dut_generation",
  760. &value);
  761. if (!rc)
  762. pdata->config_dut_generation = (u8)value;
  763. else {
  764. pr_err("%s: dut_generation is not configured, set default: DUT_PIP2_CAPABLE!\n",
  765. __func__);
  766. pdata->config_dut_generation = CONFIG_DUT_PIP2_CAPABLE;
  767. }
  768. rc = of_property_read_u32(core_node, "parade,watchdog_force_stop",
  769. &value);
  770. if (!rc) {
  771. if (value)
  772. pdata->watchdog_force_stop = true;
  773. else
  774. pdata->watchdog_force_stop = false;
  775. } else {
  776. pr_err("%s: watchdog_force_stop is not configured, set default: false!\n",
  777. __func__);
  778. pdata->watchdog_force_stop = false;
  779. }
  780. rc = of_property_read_u32(core_node, "parade,panel_id_support",
  781. &value);
  782. if (!rc) {
  783. pdata->panel_id_support = (u8)value;
  784. } else {
  785. pr_err("%s: panel_id_support is not configured, set default: PT_PANEL_ID_DISABLE\n",
  786. __func__);
  787. pdata->panel_id_support = PT_PANEL_ID_DISABLE;
  788. }
  789. for (i = 0; (unsigned int)i < ARRAY_SIZE(touch_setting_names); i++) {
  790. if (touch_setting_names[i] == NULL)
  791. continue;
  792. pdata->sett[i] = create_and_get_touch_setting(core_node,
  793. touch_setting_names[i]);
  794. if (IS_ERR(pdata->sett[i])) {
  795. rc = PTR_ERR(pdata->sett[i]);
  796. goto fail_free_sett;
  797. } else if (pdata->sett[i] == NULL)
  798. pr_debug("%s: No data for setting '%s'\n", __func__,
  799. touch_setting_names[i]);
  800. }
  801. pr_debug("%s: irq_gpio:%d rst_gpio:%d\n"
  802. "hid_desc_reg:%d level_irq_udelay:%d vendor_id:%d prod_id:%d\n"
  803. "flags:%d easy_wakeup_gesture:%d\n", __func__,
  804. pdata->irq_gpio, pdata->rst_gpio,
  805. pdata->hid_desc_register,
  806. pdata->level_irq_udelay, pdata->vendor_id, pdata->product_id,
  807. pdata->flags, pdata->easy_wakeup_gesture);
  808. pdata->xres = pt_xres;
  809. pdata->init = pt_init;
  810. pdata->power = pt_power;
  811. pdata->detect = pt_detect;
  812. pdata->irq_stat = pt_irq_stat;
  813. pdata->setup_power = pt_setup_power;
  814. pdata->setup_irq = pt_setup_irq;
  815. return pdata;
  816. fail_free_sett:
  817. for (i--; i >= 0; i--)
  818. free_touch_setting(pdata->sett[i]);
  819. fail_free:
  820. kfree(pdata);
  821. fail:
  822. return ERR_PTR(rc);
  823. }
  824. /*******************************************************************************
  825. * FUNCTION: free_core_pdata
  826. *
  827. * SUMMARY: Free the core module platform data and touch settings data.
  828. *
  829. * RETURN:
  830. * success: the pointer of core platform data
  831. * fail : error code with type of error pointer
  832. *
  833. * PARAMETERS:
  834. * *core_node - pointer to device_node structure
  835. ******************************************************************************/
  836. static void free_core_pdata(void *pdata)
  837. {
  838. struct pt_core_platform_data *core_pdata = pdata;
  839. unsigned int i;
  840. for (i = 0; i < ARRAY_SIZE(touch_setting_names); i++)
  841. free_touch_setting(core_pdata->sett[i]);
  842. kfree(core_pdata);
  843. }
  844. /*******************************************************************************
  845. * FUNCTION: pt_devtree_create_and_get_pdata
  846. *
  847. * SUMMARY: Parse dts and set up platform data for core module, multi-touch(mt)
  848. * module, button(btn) module, proximity module.And Assign platform data
  849. * pointer for loader module.
  850. *
  851. * RETURN:
  852. * 0 = success
  853. * !0 = failure
  854. *
  855. * PARAMETERS:
  856. * *adap_dev - pointer to device structure
  857. ******************************************************************************/
  858. int pt_devtree_create_and_get_pdata(struct device *adap_dev)
  859. {
  860. struct pt_platform_data *pdata;
  861. struct device_node *core_node, *dev_node, *dev_node_fail;
  862. struct drm_panel *active_panel = NULL;
  863. enum pt_device_type type;
  864. int count = 0;
  865. int rc = 0;
  866. pr_info("%s: Start of fetch dtsi..\n", __func__);
  867. if (is_create_and_get_pdata == true)
  868. return 0;
  869. if (!adap_dev->of_node)
  870. return 0;
  871. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  872. if (!pdata)
  873. return -ENOMEM;
  874. adap_dev->platform_data = pdata;
  875. set_pdata_ptr(pdata);
  876. /* There should be only one core node */
  877. for_each_child_of_node(adap_dev->of_node, core_node) {
  878. const char *name;
  879. rc = of_property_read_string(core_node, "name", &name);
  880. if (!rc)
  881. pr_debug("%s: name:%s\n", __func__, name);
  882. rc = pt_check_dsi_panel_dt(core_node, &active_panel);
  883. if (rc) {
  884. pr_err("%s: Panel not selected, rc=%d\n", __func__, rc);
  885. if (rc == -EPROBE_DEFER) {
  886. pr_err("%s: Probe defer selected, rc=%d\n", __func__, rc);
  887. kfree(pdata);
  888. return rc;
  889. }
  890. }
  891. pdata->core_pdata = create_and_get_core_pdata(core_node);
  892. if (IS_ERR(pdata->core_pdata)) {
  893. pr_err("%s: Error in fetch dtsi..\n", __func__);
  894. rc = PTR_ERR(pdata->core_pdata);
  895. break;
  896. }
  897. pr_info("%s: End of fetch dtsi..\n", __func__);
  898. pdata->core_pdata->active_panel = active_panel;
  899. pr_info("%s: Successful insert of active panel in core data\n",
  900. __func__);
  901. /* Increment reference count */
  902. of_node_get(core_node);
  903. for_each_child_of_node(core_node, dev_node) {
  904. count++;
  905. rc = get_device_type(dev_node, &type);
  906. if (rc)
  907. break;
  908. *pdata_ptr[type].pdata
  909. = create_and_get_device_pdata(dev_node, type);
  910. if (IS_ERR(*pdata_ptr[type].pdata))
  911. rc = PTR_ERR(*pdata_ptr[type].pdata);
  912. if (rc)
  913. break;
  914. /* Increment reference count */
  915. of_node_get(dev_node);
  916. }
  917. if (rc) {
  918. free_core_pdata(pdata->core_pdata);
  919. of_node_put(core_node);
  920. for_each_child_of_node(core_node, dev_node_fail) {
  921. if (dev_node == dev_node_fail)
  922. break;
  923. rc = get_device_type(dev_node, &type);
  924. if (rc)
  925. break;
  926. free_device_pdata(type);
  927. of_node_put(dev_node);
  928. }
  929. break;
  930. }
  931. pdata->loader_pdata = &_pt_loader_platform_data;
  932. }
  933. is_create_and_get_pdata = true;
  934. pr_debug("%s: %d child node(s) found\n", __func__, count);
  935. return rc;
  936. }
  937. EXPORT_SYMBOL_GPL(pt_devtree_create_and_get_pdata);
  938. /*******************************************************************************
  939. * FUNCTION: pt_devtree_clean_pdata
  940. *
  941. * SUMMARY: Free all platform data for core module, multi-touch(mt) module,
  942. * button(btn) module, proximity module.
  943. *
  944. * RETURN:
  945. * 0 = success
  946. * !0 = failure
  947. *
  948. * PARAMETERS:
  949. * *adap_dev - pointer to device structure
  950. ******************************************************************************/
  951. int pt_devtree_clean_pdata(struct device *adap_dev)
  952. {
  953. struct pt_platform_data *pdata;
  954. struct device_node *core_node, *dev_node;
  955. enum pt_device_type type;
  956. int rc = 0;
  957. if (is_create_and_get_pdata == false)
  958. return 0;
  959. if (!adap_dev->of_node)
  960. return 0;
  961. pdata = dev_get_platdata(adap_dev);
  962. set_pdata_ptr(pdata);
  963. free_core_pdata(pdata->core_pdata);
  964. for_each_child_of_node(adap_dev->of_node, core_node) {
  965. of_node_put(core_node);
  966. for_each_child_of_node(core_node, dev_node) {
  967. rc = get_device_type(dev_node, &type);
  968. if (rc)
  969. break;
  970. free_device_pdata(type);
  971. of_node_put(dev_node);
  972. }
  973. }
  974. is_create_and_get_pdata = false;
  975. return rc;
  976. }
  977. EXPORT_SYMBOL_GPL(pt_devtree_clean_pdata);
  978. MODULE_LICENSE("GPL");
  979. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product DeviceTree Driver");
  980. MODULE_AUTHOR("Parade Technologies <[email protected]>");