focaltech_core.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. *
  3. * FocalTech TouchScreen driver.
  4. *
  5. * Copyright (c) 2012-2019, Focaltech Ltd. All rights reserved.
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. /*****************************************************************************
  18. *
  19. * File Name: focaltech_core.h
  20. * Author: Focaltech Driver Team
  21. *
  22. * Created: 2016-08-08
  23. *
  24. * Abstract:
  25. *
  26. * Reference:
  27. *
  28. *****************************************************************************/
  29. #ifndef __LINUX_FOCALTECH_CORE_H__
  30. #define __LINUX_FOCALTECH_CORE_H__
  31. /*****************************************************************************
  32. * Included header files
  33. *****************************************************************************/
  34. #include <linux/kernel.h>
  35. #include <linux/device.h>
  36. #include <linux/i2c.h>
  37. #include <linux/spi/spi.h>
  38. #include <linux/input.h>
  39. #include <linux/input/mt.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/irq.h>
  42. #include <linux/delay.h>
  43. #include <linux/slab.h>
  44. #include <linux/vmalloc.h>
  45. #include <linux/gpio.h>
  46. #include <linux/regulator/consumer.h>
  47. #include <asm/uaccess.h>
  48. #include <linux/firmware.h>
  49. #include <linux/debugfs.h>
  50. #include <linux/mutex.h>
  51. #include <linux/workqueue.h>
  52. #include <linux/wait.h>
  53. #include <linux/time.h>
  54. #include <linux/jiffies.h>
  55. #include <linux/fs.h>
  56. #include <linux/proc_fs.h>
  57. #include <linux/version.h>
  58. #include <linux/types.h>
  59. #include <linux/sched.h>
  60. #include <linux/kthread.h>
  61. #include <linux/dma-mapping.h>
  62. #include <linux/gunyah/gh_irq_lend.h>
  63. #include <linux/gunyah/gh_mem_notifier.h>
  64. #include "focaltech_common.h"
  65. /*****************************************************************************
  66. * Private constant and macro definitions using #define
  67. *****************************************************************************/
  68. #define FTS_MAX_POINTS_SUPPORT 10 /* constant value, can't be changed */
  69. #define FTS_MAX_KEYS 4
  70. #define FTS_KEY_DIM 10
  71. #define FTS_ONE_TCH_LEN 6
  72. #define FTS_TOUCH_DATA_LEN (FTS_MAX_POINTS_SUPPORT * FTS_ONE_TCH_LEN + 3)
  73. #define FTS_GESTURE_POINTS_MAX 6
  74. #define FTS_GESTURE_DATA_LEN (FTS_GESTURE_POINTS_MAX * 4 + 4)
  75. #define FTS_MAX_ID 0x0A
  76. #define FTS_TOUCH_X_H_POS 3
  77. #define FTS_TOUCH_X_L_POS 4
  78. #define FTS_TOUCH_Y_H_POS 5
  79. #define FTS_TOUCH_Y_L_POS 6
  80. #define FTS_TOUCH_PRE_POS 7
  81. #define FTS_TOUCH_AREA_POS 8
  82. #define FTS_TOUCH_POINT_NUM 2
  83. #define FTS_TOUCH_EVENT_POS 3
  84. #define FTS_TOUCH_ID_POS 5
  85. #define FTS_COORDS_ARR_SIZE 4
  86. #define FTS_X_MIN_DISPLAY_DEFAULT 0
  87. #define FTS_Y_MIN_DISPLAY_DEFAULT 0
  88. #define FTS_X_MAX_DISPLAY_DEFAULT 720
  89. #define FTS_Y_MAX_DISPLAY_DEFAULT 1280
  90. #define FTS_TOUCH_DOWN 0
  91. #define FTS_TOUCH_UP 1
  92. #define FTS_TOUCH_CONTACT 2
  93. #define EVENT_DOWN(flag) ((FTS_TOUCH_DOWN == flag) || (FTS_TOUCH_CONTACT == flag))
  94. #define EVENT_UP(flag) (FTS_TOUCH_UP == flag)
  95. #define EVENT_NO_DOWN(data) (!data->point_num)
  96. #define FTX_MAX_COMPATIBLE_TYPE 4
  97. #define FTX_MAX_COMMMAND_LENGTH 16
  98. /*****************************************************************************
  99. * Alternative mode (When something goes wrong, the modules may be able to solve the problem.)
  100. *****************************************************************************/
  101. /*
  102. * For commnication error in PM(deep sleep) state
  103. */
  104. #define FTS_PATCH_COMERR_PM 0
  105. #define FTS_TIMEOUT_COMERR_PM 700
  106. /*****************************************************************************
  107. * Private enumerations, structures and unions using typedef
  108. *****************************************************************************/
  109. struct ftxxxx_proc {
  110. struct proc_dir_entry *proc_entry;
  111. u8 opmode;
  112. u8 cmd_len;
  113. u8 cmd[FTX_MAX_COMMMAND_LENGTH];
  114. };
  115. struct fts_ts_platform_data {
  116. u32 type;
  117. u32 irq_gpio;
  118. u32 irq_gpio_flags;
  119. u32 reset_gpio;
  120. u32 reset_gpio_flags;
  121. bool have_key;
  122. u32 key_number;
  123. u32 keys[FTS_MAX_KEYS];
  124. u32 key_y_coords[FTS_MAX_KEYS];
  125. u32 key_x_coords[FTS_MAX_KEYS];
  126. u32 x_max;
  127. u32 y_max;
  128. u32 x_min;
  129. u32 y_min;
  130. u32 max_touch_number;
  131. };
  132. struct ts_event {
  133. int x; /*x coordinate */
  134. int y; /*y coordinate */
  135. int p; /* pressure */
  136. int flag; /* touch event flag: 0 -- down; 1-- up; 2 -- contact */
  137. int id; /*touch ID */
  138. int area;
  139. };
  140. enum trusted_touch_mode_config {
  141. TRUSTED_TOUCH_VM_MODE,
  142. TRUSTED_TOUCH_MODE_NONE
  143. };
  144. enum trusted_touch_pvm_states {
  145. TRUSTED_TOUCH_PVM_INIT,
  146. PVM_I2C_RESOURCE_ACQUIRED,
  147. PVM_INTERRUPT_DISABLED,
  148. PVM_IOMEM_LENT,
  149. PVM_IOMEM_LENT_NOTIFIED,
  150. PVM_IRQ_LENT,
  151. PVM_IRQ_LENT_NOTIFIED,
  152. PVM_IOMEM_RELEASE_NOTIFIED,
  153. PVM_IRQ_RELEASE_NOTIFIED,
  154. PVM_ALL_RESOURCES_RELEASE_NOTIFIED,
  155. PVM_IRQ_RECLAIMED,
  156. PVM_IOMEM_RECLAIMED,
  157. PVM_INTERRUPT_ENABLED,
  158. PVM_I2C_RESOURCE_RELEASED,
  159. TRUSTED_TOUCH_PVM_STATE_MAX
  160. };
  161. enum trusted_touch_tvm_states {
  162. TRUSTED_TOUCH_TVM_INIT,
  163. TVM_IOMEM_LENT_NOTIFIED,
  164. TVM_IRQ_LENT_NOTIFIED,
  165. TVM_ALL_RESOURCES_LENT_NOTIFIED,
  166. TVM_IOMEM_ACCEPTED,
  167. TVM_I2C_SESSION_ACQUIRED,
  168. TVM_IRQ_ACCEPTED,
  169. TVM_INTERRUPT_ENABLED,
  170. TVM_INTERRUPT_DISABLED,
  171. TVM_IRQ_RELEASED,
  172. TVM_I2C_SESSION_RELEASED,
  173. TVM_IOMEM_RELEASED,
  174. TRUSTED_TOUCH_TVM_STATE_MAX
  175. };
  176. #ifdef CONFIG_FTS_TRUSTED_TOUCH
  177. #define TRUSTED_TOUCH_MEM_LABEL 0x7
  178. #define TOUCH_RESET_GPIO_BASE 0xF114000
  179. #define TOUCH_RESET_GPIO_SIZE 0x1000
  180. #define TOUCH_RESET_GPIO_OFFSET 0x4
  181. #define TOUCH_INTR_GPIO_BASE 0xF115000
  182. #define TOUCH_INTR_GPIO_SIZE 0x1000
  183. #define TOUCH_INTR_GPIO_OFFSET 0x8
  184. #define TRUSTED_TOUCH_EVENT_LEND_FAILURE -1
  185. #define TRUSTED_TOUCH_EVENT_LEND_NOTIFICATION_FAILURE -2
  186. #define TRUSTED_TOUCH_EVENT_ACCEPT_FAILURE -3
  187. #define TRUSTED_TOUCH_EVENT_FUNCTIONAL_FAILURE -4
  188. #define TRUSTED_TOUCH_EVENT_RELEASE_FAILURE -5
  189. #define TRUSTED_TOUCH_EVENT_RECLAIM_FAILURE -6
  190. #define TRUSTED_TOUCH_EVENT_I2C_FAILURE -7
  191. #define TRUSTED_TOUCH_EVENT_NOTIFICATIONS_PENDING 5
  192. struct trusted_touch_vm_info {
  193. enum gh_irq_label irq_label;
  194. enum gh_mem_notifier_tag mem_tag;
  195. enum gh_vm_names vm_name;
  196. const char *trusted_touch_type;
  197. u32 hw_irq;
  198. gh_memparcel_handle_t vm_mem_handle;
  199. u32 *iomem_bases;
  200. u32 *iomem_sizes;
  201. u32 iomem_list_size;
  202. void *mem_cookie;
  203. atomic_t vm_state;
  204. };
  205. #endif
  206. struct fts_ts_data {
  207. struct i2c_client *client;
  208. struct spi_device *spi;
  209. struct device *dev;
  210. struct input_dev *input_dev;
  211. struct fts_ts_platform_data *pdata;
  212. struct ts_ic_info ic_info;
  213. struct workqueue_struct *ts_workqueue;
  214. struct work_struct fwupg_work;
  215. struct delayed_work esdcheck_work;
  216. struct delayed_work prc_work;
  217. struct work_struct resume_work;
  218. struct work_struct suspend_work;
  219. struct ftxxxx_proc proc;
  220. spinlock_t irq_lock;
  221. struct mutex report_mutex;
  222. struct mutex bus_lock;
  223. struct mutex transition_lock;
  224. int irq;
  225. int log_level;
  226. int fw_is_running; /* confirm fw is running when using spi:default 0 */
  227. int dummy_byte;
  228. #if defined(CONFIG_PM) && FTS_PATCH_COMERR_PM
  229. struct completion pm_completion;
  230. bool pm_suspend;
  231. #endif
  232. bool suspended;
  233. bool fw_loading;
  234. bool irq_disabled;
  235. bool power_disabled;
  236. bool glove_mode;
  237. bool cover_mode;
  238. bool charger_mode;
  239. bool gesture_mode; /* gesture enable or disable, default: disable */
  240. int report_rate;
  241. /* multi-touch */
  242. struct ts_event *events;
  243. u8 *bus_tx_buf;
  244. u8 *bus_rx_buf;
  245. int bus_type;
  246. u8 *point_buf;
  247. void *notifier_cookie;
  248. int pnt_buf_size;
  249. int touchs;
  250. int key_state;
  251. int touch_point;
  252. int point_num;
  253. struct regulator *vdd;
  254. struct regulator *vcc_i2c;
  255. #if FTS_PINCTRL_EN
  256. struct pinctrl *pinctrl;
  257. struct pinctrl_state *pins_active;
  258. struct pinctrl_state *pins_suspend;
  259. struct pinctrl_state *pins_release;
  260. #endif
  261. #if defined(CONFIG_FB) || defined(CONFIG_DRM)
  262. struct notifier_block fb_notif;
  263. #elif defined(CONFIG_HAS_EARLYSUSPEND)
  264. struct early_suspend early_suspend;
  265. #endif
  266. #ifdef CONFIG_FTS_TRUSTED_TOUCH
  267. struct trusted_touch_vm_info *vm_info;
  268. struct mutex fts_clk_io_ctrl_mutex;
  269. const char *touch_environment;
  270. struct completion trusted_touch_powerdown;
  271. struct clk *core_clk;
  272. struct clk *iface_clk;
  273. atomic_t trusted_touch_initialized;
  274. atomic_t trusted_touch_enabled;
  275. atomic_t trusted_touch_transition;
  276. atomic_t trusted_touch_event;
  277. atomic_t trusted_touch_abort_status;
  278. atomic_t delayed_vm_probe_pending;
  279. atomic_t trusted_touch_mode;
  280. #endif
  281. };
  282. enum _FTS_BUS_TYPE {
  283. BUS_TYPE_NONE,
  284. BUS_TYPE_I2C,
  285. BUS_TYPE_SPI,
  286. BUS_TYPE_SPI_V2,
  287. };
  288. /*****************************************************************************
  289. * Global variable or extern global variabls/functions
  290. *****************************************************************************/
  291. extern struct fts_ts_data *fts_data;
  292. /* communication interface */
  293. int fts_read(u8 *cmd, u32 cmdlen, u8 *data, u32 datalen);
  294. int fts_read_reg(u8 addr, u8 *value);
  295. int fts_write(u8 *writebuf, u32 writelen);
  296. int fts_write_reg(u8 addr, u8 value);
  297. void fts_hid2std(void);
  298. int fts_bus_init(struct fts_ts_data *ts_data);
  299. int fts_bus_exit(struct fts_ts_data *ts_data);
  300. /* Gesture functions */
  301. int fts_gesture_init(struct fts_ts_data *ts_data);
  302. int fts_gesture_exit(struct fts_ts_data *ts_data);
  303. void fts_gesture_recovery(struct fts_ts_data *ts_data);
  304. int fts_gesture_readdata(struct fts_ts_data *ts_data, u8 *data);
  305. int fts_gesture_suspend(struct fts_ts_data *ts_data);
  306. int fts_gesture_resume(struct fts_ts_data *ts_data);
  307. /* Apk and functions */
  308. int fts_create_apk_debug_channel(struct fts_ts_data *);
  309. void fts_release_apk_debug_channel(struct fts_ts_data *);
  310. /* ADB functions */
  311. int fts_create_sysfs(struct fts_ts_data *ts_data);
  312. int fts_remove_sysfs(struct fts_ts_data *ts_data);
  313. /* ESD */
  314. #if FTS_ESDCHECK_EN
  315. int fts_esdcheck_init(struct fts_ts_data *ts_data);
  316. int fts_esdcheck_exit(struct fts_ts_data *ts_data);
  317. int fts_esdcheck_switch(bool enable);
  318. int fts_esdcheck_proc_busy(bool proc_debug);
  319. int fts_esdcheck_set_intr(bool intr);
  320. int fts_esdcheck_suspend(void);
  321. int fts_esdcheck_resume(void);
  322. #endif
  323. /* Production test */
  324. #if FTS_TEST_EN
  325. int fts_test_init(struct fts_ts_data *ts_data);
  326. int fts_test_exit(struct fts_ts_data *ts_data);
  327. #endif
  328. /* Point Report Check*/
  329. #if FTS_POINT_REPORT_CHECK_EN
  330. int fts_point_report_check_init(struct fts_ts_data *ts_data);
  331. int fts_point_report_check_exit(struct fts_ts_data *ts_data);
  332. void fts_prc_queue_work(struct fts_ts_data *ts_data);
  333. #endif
  334. /* FW upgrade */
  335. int fts_fwupg_init(struct fts_ts_data *ts_data);
  336. int fts_fwupg_exit(struct fts_ts_data *ts_data);
  337. int fts_enter_test_environment(bool test_state);
  338. /* Other */
  339. int fts_reset_proc(int hdelayms);
  340. int fts_wait_tp_to_valid(void);
  341. void fts_release_all_finger(void);
  342. void fts_tp_state_recovery(struct fts_ts_data *ts_data);
  343. int fts_ex_mode_init(struct fts_ts_data *ts_data);
  344. int fts_ex_mode_exit(struct fts_ts_data *ts_data);
  345. int fts_ex_mode_recovery(struct fts_ts_data *ts_data);
  346. void fts_irq_disable(void);
  347. void fts_irq_enable(void);
  348. int fts_ts_handle_trusted_touch_pvm(struct fts_ts_data *ts_data, int value);
  349. int fts_ts_handle_trusted_touch_tvm(struct fts_ts_data *ts_data, int value);
  350. #ifdef CONFIG_FTS_TRUSTED_TOUCH
  351. #ifdef CONFIG_ARCH_QTI_VM
  352. void fts_ts_trusted_touch_tvm_i2c_failure_report(struct fts_ts_data *fts_data);
  353. #endif
  354. #endif
  355. #endif /* __LINUX_FOCALTECH_CORE_H__ */