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