focaltech_core.h 12 KB

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