summaryrefslogtreecommitdiff
path: root/nt36xxx/nt36xxx.h
blob: 95a8dd88c9a3344625ae440dcde47c9522405df9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* SPDX-License-Identifier: GPL-2.0-only */
/*
 * Copyright (C) 2010 - 2021 Novatek, Inc.
 *
 * $Revision: 83893 $
 * $Date: 2021-06-21 10:52:25 +0800 (週一, 21 六月 2021) $
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 */
#ifndef _LINUX_NVT_TOUCH_H
#define	_LINUX_NVT_TOUCH_H

#include <linux/delay.h>
#include <linux/input.h>
#include <linux/of.h>
#include <linux/spi/spi.h>
#include <linux/uaccess.h>
#include <linux/version.h>

#ifdef CONFIG_HAS_EARLYSUSPEND
#include <linux/earlysuspend.h>
#endif

#include "nt36xxx_mem_map.h"

#ifdef CONFIG_MTK_SPI
/* Please copy mt_spi.h file under mtk spi driver folder */
#include "mt_spi.h"
#endif

#ifdef CONFIG_SPI_MT65XX
#include <linux/platform_data/spi-mt65xx.h>
#endif

#include <drm/drm_panel.h> /* struct drm_panel */
#include <drm/drm_bridge.h> /* struct drm_bridge */
#include <drm/drm_connector.h> /* struct drm_connector */

#include "nt36xxx_goog.h"

#define NVT_MP_DEBUG 0

#if defined(CONFIG_SOC_GOOGLE)
#undef CONFIG_FB
#undef CONFIG_HAS_EARLYSUSPEND
#undef CONFIG_ARCH_QCOM
#undef CONFIG_ARCH_MSM
#endif

//---GPIO number---
#define NVTTOUCH_RST_PIN 980
#define NVTTOUCH_INT_PIN 943


//---INT trigger mode---
//#define IRQ_TYPE_EDGE_RISING 1
//#define IRQ_TYPE_EDGE_FALLING 2
#define INT_TRIGGER_TYPE IRQ_TYPE_EDGE_RISING


//---SPI driver info.---
#define NVT_SPI_NAME "NVT-ts"

#define NVT_DBG(fmt, args...)    pr_debug("[%s] %s %d: " fmt, NVT_SPI_NAME,\
					__func__, __LINE__, ##args)
#define NVT_LOG(fmt, args...)    pr_info("[%s] %s %d: " fmt, NVT_SPI_NAME,\
					__func__, __LINE__, ##args)
#define NVT_ERR(fmt, args...)    pr_err("[%s] %s %d: " fmt, NVT_SPI_NAME,\
					__func__, __LINE__, ##args)

//---Input device info.---
#define NVT_TS_NAME "NVTCapacitiveTouchScreen"
#define NVT_PEN_NAME "NVTCapacitivePen"
#define NVT_PEN_BATTERY_NAME "nvt-pen-battery"

//---Touch info.---
#define TOUCH_DEFAULT_MAX_WIDTH 1600
#define TOUCH_DEFAULT_MAX_HEIGHT 2560
#define TOUCH_MAX_FINGER_NUM 10
#define TOUCH_KEY_NUM 0
#if TOUCH_KEY_NUM > 0
extern const uint16_t touch_key_array[TOUCH_KEY_NUM];
#endif
//#define TOUCH_FORCE_NUM 1000
#ifdef TOUCH_FORCE_NUM
#define MT_PRESSURE_MAX TOUCH_FORCE_NUM
#else
#define MT_PRESSURE_MAX 256
#endif
//---for Pen---
//#define PEN_DISTANCE_SUPPORT
#define PEN_PRESSURE_MAX (4095)
#define PEN_DISTANCE_MAX (1)
#define PEN_TILT_MIN (-60)
#define PEN_TILT_MAX (60)
#define PEN_BATTERY_MAX (100)
#define PEN_BATTERY_MIN (0)

/* Enable only when module have tp reset pin and connected to host */
#define NVT_TOUCH_SUPPORT_HW_RST 1

//---Customerized func.---
#define NVT_TOUCH_PROC 1
#define NVT_TOUCH_EXT_PROC 1
#define NVT_TOUCH_EXT_API 1
#define NVT_TOUCH_EXT_USI 1
#define REPORT_PROTOCOL_A 1
#define REPORT_PROTOCOL_B 0
#define NVT_TOUCH_MP 1
#define WAKEUP_GESTURE 1
#define BOOT_UPDATE_FIRMWARE 1
#define BOOT_UPDATE_FIRMWARE_MS_DELAY 100
#define BOOT_UPDATE_FIRMWARE_NAME "novatek_ts_fw.bin"
#define MP_UPDATE_FIRMWARE_NAME   "novatek_ts_mp.bin"
#define POINT_DATA_CHECKSUM 0
#define POINT_DATA_CHECKSUM_LEN 65
#define NVT_HEATMAP_COMP_NOT_READY_SIZE (0xFFF << 1)

//---ESD Protect.---
#define NVT_TOUCH_ESD_PROTECT 1
#define NVT_TOUCH_ESD_CHECK_PERIOD 1500	/* ms */
#define NVT_TOUCH_WDT_RECOVERY 1

#define CHECK_PEN_DATA_CHECKSUM 0

// MP
#define NORMAL_MODE 0x00
#define TEST_MODE_2 0x22
#define MP_MODE_CC 0x41
#define ENTER_ENG_MODE 0x61
#define LEAVE_ENG_MODE 0x62
#define FREQ_HOP_DISABLE 0x66
#define FREQ_HOP_ENABLE 0x65

// NVT_MT_CUSTOM for Cancel Mode Finger Status
#define NVT_MT_CUSTOM 1
#if NVT_MT_CUSTOM
#define ABS_MT_CUSTOM 0x3e
#define GRIP_TOUCH 0x04
#define PALM_TOUCH 0x05
#endif

// HEATMAP
#if NVT_TOUCH_EXT_API
#define HEATMAP_TOUCH_ADDR 0x23200
#define HEATMAP_PEN_ADDR 0x2A50A
enum {
	HEATMAP_DATA_TYPE_DISABLE = 0,
	HEATMAP_DATA_TYPE_TOUCH_RAWDATA = 1,
	HEATMAP_DATA_TYPE_TOUCH_RAWDATA_UNCOMP = HEATMAP_DATA_TYPE_TOUCH_RAWDATA,
	HEATMAP_DATA_TYPE_TOUCH_BASELINE = 2,
	HEATMAP_DATA_TYPE_TOUCH_BASELINE_UNCOMP = HEATMAP_DATA_TYPE_TOUCH_BASELINE,
	HEATMAP_DATA_TYPE_TOUCH_STRENGTH = 3,
	HEATMAP_DATA_TYPE_TOUCH_STRENGTH_UNCOMP = HEATMAP_DATA_TYPE_TOUCH_STRENGTH,
	HEATMAP_DATA_TYPE_TOUCH_STRENGTH_COMP = 4,
	HEATMAP_DATA_TYPE_PEN_STRENGTH_COMP = 5,
	HEATMAP_DATA_TYPE_UNSUPPORTED,
};
#define HEATMAP_HOST_CMD_DISABLE             0x90
#define HEATMAP_HOST_CMD_TOUCH_STRENGTH      0x91
#define HEATMAP_HOST_CMD_TOUCH_STRENGTH_COMP 0x92
#define HEATMAP_HOST_CMD_TOUCH_RAWDATA       0x93
#define HEATMAP_HOST_CMD_TOUCH_BASELINE      0x94
#endif

/* PEN */
#define PEN_HASH_SECTION_ID_ADDR 0x2B31D

/* FW History */
#define NVT_HISTORY_BUF_LEN		(65 * 4)

enum gesture_id : u8 {
	GESTURE_WORD_C = 12,
	GESTURE_WORD_W = 13,
	GESTURE_SINGLE_TAP = 14,
	GESTURE_DOUBLE_TAP = 15,
	GESTURE_WORD_Z = 16,
	GESTURE_WORD_M = 17,
	GESTURE_WORD_O = 18,
	GESTURE_WORD_e = 19,
	GESTURE_WORD_S = 20,
	GESTURE_SLIDE_UP = 21,
	GESTURE_SLIDE_DOWN = 22,
	GESTURE_SLIDE_LEFT = 23,
	GESTURE_SLIDE_RIGHT = 24,
	GESTURE_ID_MAX,
};

struct nvt_ts_data {
	struct spi_device *client;
	struct input_dev *input_dev;
	struct delayed_work nvt_fwu_work;
	uint16_t addr;
	int8_t phys[32];
#if defined(CONFIG_FB) && !defined(CONFIG_SOC_GOOGLE)
#if defined(CONFIG_DRM_PANEL) && (defined(CONFIG_ARCH_QCOM) || defined(CONFIG_ARCH_MSM))
	struct notifier_block drm_panel_notif;
#elif defined(_MSM_DRM_NOTIFY_H_)
	struct notifier_block drm_notif;
#else
	struct notifier_block fb_notif;
#endif
#elif defined(CONFIG_HAS_EARLYSUSPEND)
	struct early_suspend early_suspend;
#endif
	uint8_t fw_ver;
	uint8_t x_num;
	uint8_t y_num;
	uint16_t touch_width;
	uint16_t touch_height;
	uint16_t abs_x_max;		/* abs report start from 0 to 'width-1' */
	uint16_t abs_y_max;		/* abs report start from 0 to 'height-1' */
	uint8_t max_touch_num;
	uint8_t max_button_num;
	uint8_t touch_freq_index;
	uint8_t pen_freq_index;
	uint32_t int_trigger_type;
	int32_t irq_gpio;
	uint32_t irq_flags;
	int32_t reset_gpio;
	uint32_t reset_flags;
	struct mutex lock;
#if defined(CONFIG_SOC_GOOGLE)
	const struct nvt_ts_trim_id_table *trim_table;
#endif
	const struct nvt_ts_mem_map *mmap;
	uint8_t hw_crc;
	uint16_t nvt_pid;
	uint8_t *rbuf;
	uint8_t *xbuf;
	char history_buf[NVT_HISTORY_BUF_LEN];
	struct mutex xbuf_lock;
	bool probe_done;
	bool irq_enabled;
	bool pen_support;
	bool wgp_stylus;
	uint8_t x_gang_num;
	uint8_t y_gang_num;
	struct input_dev *pen_input_dev;
	int8_t pen_phys[32];
#ifdef CONFIG_MTK_SPI
	struct mt_chip_conf spi_ctrl;
#endif
#ifdef CONFIG_SPI_MT65XX
	struct mtk_chip_config spi_ctrl;
#endif
	uint8_t report_protocol;
	uint8_t wkg_flag;
	uint8_t bTouchIsAwake;
	uint8_t pen_format_id;
	uint32_t pen_bat_capa;
	struct power_supply *pen_bat_psy;
#if NVT_TOUCH_EXT_API
	uint16_t dttw_touch_area_max;
	uint16_t dttw_touch_area_min;
	uint16_t dttw_contact_duration_max;
	uint16_t dttw_contact_duration_min;
	uint16_t dttw_tap_offset;
	uint16_t dttw_tap_gap_duration_max;
	uint16_t dttw_tap_gap_duration_min;
	uint16_t dttw_motion_tolerance;
	uint16_t dttw_detection_window_edge;
	uint8_t heatmap_data_type;
	uint16_t pen_hash_id;
	uint16_t pen_section_id;
	uint8_t pen_freq_seed;
#endif

	const char *fw_name;
	const char *mp_fw_name;

	/*
	 * Time that the event was first received from
	 * the touch IC, acquired during hard interrupt,
	 * in CLOCK_MONOTONIC
	 */
	ktime_t timestamp;

	/*
	 * Used for event handler, suspend and resume work.
	 */
	struct pinctrl *pinctrl;
	struct drm_panel *active_panel;
	u32 initial_panel_index;

	struct completion bus_resumed;
	struct drm_bridge panel_bridge;
	struct drm_connector *connector;
	bool is_panel_lp_mode;
	struct delayed_work suspend_work;
	struct delayed_work resume_work;
	struct workqueue_struct *event_wq;

	struct mutex bus_mutex;
	ktime_t bugreport_ktime_start;
	u8 force_release_fw;

	/*
	 * Used for google touch interface.
	 */
	struct goog_touch_interface *gti;
	uint8_t heatmap_host_cmd;
	uint32_t heatmap_host_cmd_addr;
	uint32_t heatmap_out_buf_size;
	uint8_t *heatmap_out_buf;
	uint32_t heatmap_spi_buf_size;
	uint8_t *heatmap_spi_buf;
	uint32_t extra_spi_buf_size;
	uint8_t *extra_spi_buf;
	uint32_t touch_heatmap_comp_len;

	/*
	 * Stylus context used by touch_offload
	 */
	struct TouchOffloadCoord pen_offload_coord;
	ktime_t pen_offload_coord_timestamp;
	u8 pen_active;
};

#if NVT_TOUCH_PROC
struct nvt_flash_data {
	rwlock_t lock;
};
#endif

typedef enum {
	RESET_STATE_INIT = 0xA0,// IC reset
	RESET_STATE_REK,		// ReK baseline
	RESET_STATE_REK_FINISH,	// baseline is ready
	RESET_STATE_NORMAL_RUN,	// normal run
	RESET_STATE_MAX  = 0xAF
} RST_COMPLETE_STATE;

typedef enum {
	EVENT_MAP_HOST_CMD                      = 0x50,
	EVENT_MAP_HANDSHAKING_or_SUB_CMD_BYTE   = 0x51,
	EVENT_MAP_RESET_COMPLETE                = 0x60,
	EVENT_MAP_FWINFO                        = 0x78,
	EVENT_MAP_PROJECTID                     = 0x9A,
} SPI_EVENT_MAP;

//---SPI READ/WRITE---
#define SPI_WRITE_MASK(a)	(a | 0x80)
#define SPI_READ_MASK(a)	(a & 0x7F)

#define DUMMY_BYTES (1)
#define NVT_TRANSFER_LEN	(63*1024)
#define NVT_READ_LEN		(4*1024)
#define NVT_XBUF_LEN		(NVT_TRANSFER_LEN+1+DUMMY_BYTES)

typedef enum {
	NVTWRITE = 0,
	NVTREAD  = 1
} NVT_SPI_RW;

//---extern structures---
extern struct nvt_ts_data *ts;

//---extern functions---
int32_t CTP_SPI_READ(struct spi_device *client, uint8_t *buf, uint16_t len);
int32_t CTP_SPI_WRITE(struct spi_device *client, uint8_t *buf, uint16_t len);
void nvt_bootloader_reset(void);
void nvt_eng_reset(void);
void nvt_sw_reset(void);
void nvt_sw_reset_idle(void);
void nvt_boot_ready(void);
void nvt_bld_crc_enable(void);
void nvt_fw_crc_enable(void);
void nvt_tx_auto_copy_mode(void);
int32_t nvt_check_fw_reset_state(RST_COMPLETE_STATE check_reset_state);
int32_t nvt_get_fw_info(void);
int32_t nvt_clear_fw_status(void);
int32_t nvt_check_fw_status(void);
int32_t nvt_check_spi_dma_tx_info(void);
int32_t nvt_set_page(uint32_t addr);
int32_t nvt_write_addr(uint32_t addr, uint8_t data);
extern void update_firmware_release(void);
extern int32_t nvt_update_firmware(const char *firmware_name, uint8_t full);
extern void nvt_change_mode(uint8_t mode);
extern int8_t nvt_switch_FreqHopEnDis(uint8_t FreqHopEnDis);
extern uint8_t nvt_get_fw_pipe(void);
extern void nvt_read_fw_history(uint32_t addr);
#if NVT_TOUCH_EXT_API
extern int32_t nvt_extra_api_init(void);
extern void nvt_extra_api_deinit(void);
extern void nvt_get_dttw_conf(void);
extern ssize_t nvt_set_dttw(uint8_t wkg_flag, bool check_result);
#endif
#if NVT_TOUCH_EXT_USI
extern int32_t nvt_extra_usi_init(void);
extern void nvt_extra_usi_deinit(void);
#endif

#if NVT_TOUCH_ESD_PROTECT
extern void nvt_esd_check_enable(uint8_t enable);
#endif /* #if NVT_TOUCH_ESD_PROTECT */

void nvt_irq_enable(bool enable);
inline const char *get_fw_name(void);
inline const char *get_mp_fw_name(void);
int nvt_ts_resume(struct device *dev);
int nvt_ts_suspend(struct device *dev);

void nvt_set_heatmap_host_cmd(struct nvt_ts_data *ts);
#endif /* _LINUX_NVT_TOUCH_H */