Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/input/touch.h
3694 views
1
/*
2
* Touch driver for Nintendo Switch's STM FingerTip S (4CD60D) touch controller
3
*
4
* Copyright (c) 2018 langerhans
5
* Copyright (c) 2018-2026 CTCaer
6
*
7
* This program is free software; you can redistribute it and/or modify it
8
* under the terms and conditions of the GNU General Public License,
9
* version 2, as published by the Free Software Foundation.
10
*
11
* This program is distributed in the hope it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14
* more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
*/
19
20
#ifndef __TOUCH_H_
21
#define __TOUCH_H_
22
23
#include <utils/types.h>
24
25
#define FTS4_I2C_ADDR 0x49
26
27
#define FTS4_I2C_CHIP_ID 0x3670
28
29
/* I2C commands. */
30
#define FTS4_CMD_READ_INFO 0x80
31
#define FTS4_CMD_READ_STATUS 0x84
32
#define FTS4_CMD_READ_ONE_EVENT 0x85
33
#define FTS4_CMD_READ_ALL_EVENT 0x86
34
#define FTS4_CMD_LATEST_EVENT 0x87 // Clears event stack.
35
#define FTS4_CMD_SLEEP_IN 0x90
36
#define FTS4_CMD_SLEEP_OUT 0x91
37
#define FTS4_CMD_MS_MT_SENSE_OFF 0x92
38
#define FTS4_CMD_MS_MT_SENSE_ON 0x93
39
#define FTS4_CMD_SS_HOVER_SENSE_OFF 0x94
40
#define FTS4_CMD_SS_HOVER_SENSE_ON 0x95
41
#define FTS4_CMD_LP_TIMER_CALIB 0x97
42
#define FTS4_CMD_MS_KEY_SENSE_OFF 0x9A
43
#define FTS4_CMD_MS_KEY_SENSE_ON 0x9B
44
#define FTS4_CMD_SYSTEM_RESET 0xA0
45
#define FTS4_CMD_CLEAR_EVENT_STACK 0xA1
46
#define FTS4_CMD_FULL_FORCE_CALIB 0xA2
47
#define FTS4_CMD_MS_CX_TUNING 0xA3
48
#define FTS4_CMD_SS_CX_TUNING 0xA4
49
#define FTS4_CMD_ITO_CHECK 0xA7
50
#define FTS4_CMD_RELEASEINFO 0xAA
51
#define FTS4_CMD_HW_REG_READ 0xB6 // u16be address offset. Any size read.
52
#define FTS4_CMD_HW_REG_WRITE FTS4_CMD_HW_REG_READ // u16be address offset, bytes to write.
53
#define FTS4_CMD_SWITCH_SENSE_MODE 0xC3
54
#define FTS4_CMD_NOISE_WRITE 0xC7
55
#define FTS4_CMD_NOISE_READ 0xC8
56
#define FTS4_CMD_FB_REG_READ 0xD0
57
#define FTS4_CMD_FB_REG_WRITE FTS4_CMD_FB_REG_READ
58
#define FTS4_CMD_SAVE_CX_TUNING 0xFC
59
60
#define FTS4_CMD_DETECTION_CONFIG 0xB0
61
#define FTS4_CMD_REQ_CX_DATA 0xB8
62
#define FTS4_CMD_VENDOR 0xCF
63
#define FTS4_CMD_FLASH_UNLOCK 0xF7
64
#define FTS4_CMD_FLASH_WRITE_64K 0xF8
65
#define FTS4_CMD_FLASH_STATUS 0xF9
66
#define FTS4_CMD_FLASH_OP 0xFA
67
#define FTS4_CMD_UNK_62 0x62
68
69
/* Command parameters. */
70
#define FTS4_VENDOR_GPIO_STATE 0x01
71
#define FTS4_VENDOR_SENSE_MODE 0x02
72
#define FTS4_STYLUS_MODE 0x00
73
#define FTS4_FINGER_MODE 0x01
74
#define FTS4_HOVER_MODE 0x02
75
76
/* HW Registers */
77
#define FTS4_HW_REG_CHIP_ID_INFO 0x0004
78
#define FTS4_HW_REG_EVENT_COUNT 0x0023
79
#define FTS4_HW_REG_SYS_RESET 0x0028
80
81
/* FB Addresses */
82
#define FTS4_FB_REG_FW_INFO_ADDRESS 0x0060
83
84
/* Events. */
85
#define FTS4_EV_NO_EVENT 0x00
86
#define FTS4_EV_MULTI_TOUCH_DETECTED 0x02
87
#define FTS4_EV_MULTI_TOUCH_ENTER 0x03
88
#define FTS4_EV_MULTI_TOUCH_LEAVE 0x04
89
#define FTS4_EV_MULTI_TOUCH_MOTION 0x05
90
#define FTS4_EV_HOVER_ENTER 0x07
91
#define FTS4_EV_HOVER_LEAVE 0x08
92
#define FTS4_EV_HOVER_MOTION 0x09
93
#define FTS4_EV_KEY_STATUS 0x0E
94
#define FTS4_EV_ERROR 0x0F
95
#define FTS4_EV_CONTROLLER_READY 0x10
96
#define FTS4_EV_STATUS 0x16
97
#define FTS4_EV_NOISE_READ 0x17
98
#define FTS4_EV_NOISE_WRITE 0x18
99
#define FTS4_EV_VENDOR 0x20
100
#define FTS4_EV_DEBUG 0xDB
101
102
/* FTS4_EV_STATUS Events. */
103
#define FTS4_EV_STATUS_MS_CX_TUNING_DONE 0x01
104
#define FTS4_EV_STATUS_SS_CX_TUNING_DONE 0x02
105
#define FTS4_EV_STATUS_WRITE_CX_TUNE_DONE 0x04
106
107
#define FTS4_EV_ERROR_ITO_TEST 0x05
108
109
/* Multi touch related event masks. */
110
#define FTS4_MASK_EVENT_ID 0x0F
111
#define FTS4_MASK_TOUCH_ID 0xF0
112
#define FTS4_MASK_X_MSB 0x0F
113
#define FTS4_MASK_Y_LSB 0xF0
114
115
#define FTS4_EVENT_SIZE 8
116
#define FTS4_STACK_DEPTH 32 // Actual 128.
117
#define FTS4_DATA_MAX_SIZE (FTS4_EVENT_SIZE * FTS4_STACK_DEPTH)
118
#define FTS4_MAX_FINGERS 10
119
120
typedef enum _touch_ito_error {
121
ITO_NO_ERROR = 0,
122
ITO_FORCE_OPEN,
123
ITO_SENSE_OPEN,
124
ITO_FORCE_SHRT_GND,
125
ITO_SENSE_SHRT_GND,
126
ITO_FORCE_SHRT_VCM,
127
ITO_SENSE_SHRT_VCM,
128
ITO_FORCE_SHRT_FORCE,
129
ITO_SENSE_SHRT_SENSE,
130
ITO_F2E_SENSE,
131
ITO_FPC_FORCE_OPEN,
132
ITO_FPC_SENSE_OPEN,
133
ITO_KEY_FORCE_OPEN,
134
ITO_KEY_SENSE_OPEN,
135
ITO_RESERVED0,
136
ITO_RESERVED1,
137
ITO_RESERVED2,
138
ITO_MAX_ERR_REACHED = 0xFF
139
} touch_ito_error;
140
141
typedef struct _touch_event_t {
142
u8 raw[FTS4_EVENT_SIZE];
143
u16 x, y; // Coordinates.
144
u32 z; // Orientation.
145
bool touch;
146
int finger;
147
} touch_event_t;
148
149
typedef struct _touch_panel_info_t
150
{
151
u8 idx;
152
u8 gpio0;
153
u8 gpio1;
154
u8 gpio2;
155
char *vendor;
156
} touch_panel_info_t;
157
158
typedef struct _touch_info_t {
159
u16 chip_id;
160
u16 fw_ver;
161
u16 config_id;
162
u16 config_ver;
163
bool clone;
164
} touch_info_t;
165
166
typedef struct _touch_fw_info_t {
167
u32 fw_id;
168
u16 ftb_ver;
169
u16 fw_rev;
170
} touch_fw_info_t;
171
172
int touch_poll(touch_event_t *event);
173
touch_info_t *touch_get_chip_info();
174
touch_panel_info_t *touch_get_panel_vendor();
175
int touch_get_fw_info(touch_fw_info_t *fw);
176
int touch_get_event_count();
177
int touch_panel_ito_test(u8 *err);
178
int touch_execute_autotune();
179
int touch_switch_sense_mode(u8 mode, bool gis_6_2);
180
int touch_sense_enable();
181
int touch_power_on();
182
void touch_power_off();
183
184
#endif /* __TOUCH_H_ */
185
186