Skip to content

Commit 9cd3250

Browse files
committed
drivers: input: add ft6146 driver
add initial driver for ft6146 Signed-off-by: Qingsong Gou <[email protected]>
1 parent 9dfac5a commit 9cd3250

File tree

5 files changed

+402
-0
lines changed

5 files changed

+402
-0
lines changed

drivers/input/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ zephyr_library_sources_ifdef(CONFIG_INPUT_CST816S input_cst816s.c)
1515
zephyr_library_sources_ifdef(CONFIG_INPUT_CY8CMBR3XXX input_cy8cmbr3xxx.c)
1616
zephyr_library_sources_ifdef(CONFIG_INPUT_ESP32_TOUCH_SENSOR input_esp32_touch_sensor.c)
1717
zephyr_library_sources_ifdef(CONFIG_INPUT_FT5336 input_ft5336.c)
18+
zephyr_library_sources_ifdef(CONFIG_INPUT_FT6146 input_ft6146.c)
1819
zephyr_library_sources_ifdef(CONFIG_INPUT_GPIO_KBD_MATRIX input_gpio_kbd_matrix.c)
1920
zephyr_library_sources_ifdef(CONFIG_INPUT_GPIO_KEYS input_gpio_keys.c)
2021
zephyr_library_sources_ifdef(CONFIG_INPUT_GPIO_QDEC input_gpio_qdec.c)

drivers/input/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ source "drivers/input/Kconfig.cy8cmbr3xxx"
1717
source "drivers/input/Kconfig.esp32"
1818
source "drivers/input/Kconfig.evdev"
1919
source "drivers/input/Kconfig.ft5336"
20+
source "drivers/input/Kconfig.ft6146"
2021
source "drivers/input/Kconfig.gpio_kbd_matrix"
2122
source "drivers/input/Kconfig.gpio_keys"
2223
source "drivers/input/Kconfig.gpio_qdec"

drivers/input/Kconfig.ft6146

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
menuconfig INPUT_FT6146
2+
bool "FT6146 touch controller"
3+
default y
4+
depends on DT_HAS_FOCALTECH_FT6146_ENABLED
5+
select I2C
6+
help
7+
Enable driver for FT6146 capacitive touch controller.
8+
9+
if INPUT_FT6146
10+
11+
config INPUT_FT6416_PERIOD
12+
int "Sample period"
13+
depends on !INPUT_FT6146_INTERRUPT
14+
default 10
15+
help
16+
Sample period in milliseconds when in polling mode.
17+
18+
config INPUT_FT6146_INTERRUPT
19+
bool "FT6146 interrupt support"
20+
default y if $(dt_compat_any_has_prop,$(DT_COMPAT_FOCALTECH_FT56146),int-gpios)
21+
help
22+
Enable interrupt support for the FT6146 touch controller.
23+
24+
endif # INPUT_FT6146

drivers/input/input_ft6146.c

Lines changed: 356 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,356 @@
1+
/*
2+
* Copyright (c) 2025 Qingsong Gou <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#define DT_DRV_COMPAT focaltech_ft6146
7+
8+
#include <zephyr/kernel.h>
9+
#include <zephyr/drivers/i2c.h>
10+
#include <zephyr/drivers/gpio.h>
11+
#include <zephyr/input/input.h>
12+
#include <zephyr/logging/log.h>
13+
14+
LOG_MODULE_REGISTER(ft6146, CONFIG_INPUT_LOG_LEVEL);
15+
16+
/* FT6146 register definitions */
17+
#define FT6146_REG_DEVICE_MODE 0x00
18+
#define FT6146_REG_GEST_ID 0x01
19+
#define FT6146_REG_TD_STATUS 0x02
20+
#define FT6146_REG_P1_XH 0x03
21+
#define FT6146_REG_P1_XL 0x04
22+
#define FT6146_REG_P1_YH 0x05
23+
#define FT6146_REG_P1_YL 0x06
24+
#define FT6146_REG_P1_WEIGHT 0x07
25+
#define FT6146_REG_P1_MISC 0x08
26+
#define FT6146_REG_P2_XH 0x09
27+
#define FT6146_REG_P2_XL 0x0A
28+
#define FT6146_REG_P2_YH 0x0B
29+
#define FT6146_REG_P2_YL 0x0C
30+
#define FT6146_REG_P2_WEIGHT 0x0D
31+
#define FT6146_REG_P2_MISC 0x0E
32+
#define FT6146_REG_THRESHOLD 0x80
33+
#define FT6146_REG_FILTER_COE 0x85
34+
#define FT6146_REG_CTRL 0x86
35+
#define FT6146_REG_TIMEENTERMONITOR 0x87
36+
#define FT6146_REG_PERIODACTIVE 0x88
37+
#define FT6146_REG_PERIODMONITOR 0x89
38+
#define FT6146_REG_RADIAN_VALUE 0x91
39+
#define FT6146_REG_OFFSET_LEFT_RIGHT 0x92
40+
#define FT6146_REG_OFFSET_UP_DOWN 0x93
41+
#define FT6146_REG_DIST_LEFT_RIGHT 0x94
42+
#define FT6146_REG_DIST_UP_DOWN 0x95
43+
#define FT6146_REG_ZOOM_DIS_SQR 0x96
44+
#define FT6146_REG_RADIAN_THRESHOLD 0x97
45+
#define FT6146_REG_SMALL_OBJECT_THRESHOLD 0x98
46+
47+
/* Device mode values */
48+
#define FT6146_DEVICE_MODE_NORMAL 0x00
49+
#define FT6146_DEVICE_MODE_TEST 0x04
50+
#define FT6146_DEVICE_MODE_SYSTEM 0x01
51+
52+
/* Gesture IDs */
53+
#define FT6146_GESTURE_NO_GESTURE 0x00
54+
#define FT6146_GESTURE_MOVE_UP 0x10
55+
#define FT6146_GESTURE_MOVE_RIGHT 0x14
56+
#define FT6146_GESTURE_MOVE_DOWN 0x18
57+
#define FT6146_GESTURE_MOVE_LEFT 0x1C
58+
#define FT6146_GESTURE_ZOOM_IN 0x48
59+
#define FT6146_GESTURE_ZOOM_OUT 0x49
60+
61+
/* Reset timing */
62+
#define FT6146_RESET_DELAY_MS 10
63+
#define FT6146_POST_RESET_DELAY_MS 100
64+
65+
#define MAX_POINT_NUM 2
66+
67+
#define FT6146_READ_ID_H (0xA3)
68+
#define FT6146_READ_ID_L (0x9F)
69+
70+
struct ft6146_data {
71+
const struct device *dev;
72+
const struct device *int_gpio;
73+
struct gpio_callback int_cb;
74+
struct k_work work;
75+
struct k_timer poll_timer;
76+
uint8_t i2c_addr;
77+
uint16_t x_max;
78+
uint16_t y_max;
79+
bool gesture_enabled;
80+
bool last_touch_state;
81+
};
82+
83+
struct ft6146_config {
84+
struct i2c_dt_spec i2c;
85+
struct gpio_dt_spec int_gpio;
86+
struct gpio_dt_spec reset_gpio;
87+
uint16_t x_max;
88+
uint16_t y_max;
89+
bool gesture_enabled;
90+
};
91+
92+
static void ft6146_process_touch(const struct device *dev, uint8_t status)
93+
{
94+
const struct ft6146_config *config = dev->config;
95+
//struct ft6146_data *data = dev->data;
96+
uint8_t point_data[14];
97+
uint8_t touch_num = 0;
98+
uint8_t event_flag = 0;
99+
uint16_t x, y;
100+
int id = 0;
101+
int ret;
102+
//bool touch_active = (status & 0x0F) != 0;
103+
104+
/* Read touch data */
105+
ret = i2c_burst_read_dt(&config->i2c, FT6146_REG_GEST_ID, point_data, sizeof(point_data));
106+
if (ret < 0) {
107+
LOG_ERR("Failed to read touch data: %d", ret);
108+
return;
109+
}
110+
111+
touch_num = point_data[1] & 0x0f;
112+
LOG_DBG("%s:touch_num:%d", __func__, touch_num);
113+
if (touch_num > MAX_POINT_NUM) {
114+
return;
115+
} else if (touch_num == 0) {
116+
117+
} else {
118+
119+
for (int i = 0; i < touch_num; i++) {
120+
event_flag = (point_data[2 + i * 6] >> 6) & 0x03;
121+
id = point_data[4 + i * 6] >> 4;
122+
x = ((point_data[2 + i * 6] & 0x0F) << 8) + point_data[3 + i * 6];
123+
y = ((point_data[4 + i * 6] & 0x0F) << 8) + point_data[5 + i * 6];
124+
LOG_DBG("%s %d :event_flag:%d, id:%d, x:%d, y:%d", __func__, __LINE__, event_flag, id, x, y);
125+
#if 0
126+
if (1 == abnormal_recovery)
127+
{
128+
abnormal_recovery = 0;
129+
p_msg->event = TOUCH_EVENT_UP;
130+
p_msg->x = (uint16_t)input_x;
131+
p_msg->y = (uint16_t)input_y;
132+
LOG_D("%s %d :ft6146 abnormal", __func__, __LINE__);
133+
}
134+
else if ((eventFlag == CTP_DOWN) || (eventFlag == CTP_MOVE))
135+
{
136+
p_msg->event = TOUCH_EVENT_DOWN;
137+
p_msg->x = (uint16_t)input_x;
138+
p_msg->y = (uint16_t)input_y;
139+
point_report_check_en = 1;
140+
time_100ms_count = 0;
141+
}
142+
else if (eventFlag == CTP_UP)
143+
{
144+
p_msg->event = TOUCH_EVENT_UP;
145+
p_msg->x = (uint16_t)input_x;
146+
p_msg->y = (uint16_t)input_y;
147+
point_report_check_en = 0;
148+
time_100ms_count = 0;
149+
}
150+
#endif
151+
}
152+
}
153+
/* Process touch coordinates */
154+
//x = ((touch_data[0] & 0x0F) << 8) | touch_data[1];
155+
//y = ((touch_data[2] & 0x0F) << 8) | touch_data[3];
156+
157+
/* Report touch event */
158+
//input_report_abs(dev, INPUT_ABS_X, x, false, K_FOREVER);
159+
//input_report_abs(dev, INPUT_ABS_Y, y, false, K_FOREVER);
160+
//input_report_key(dev, INPUT_BTN_TOUCH, 1, true, K_FOREVER);
161+
}
162+
#if 0
163+
static void ft6146_process_gesture(const struct device *dev)
164+
{
165+
struct ft6146_data *data = dev->data;
166+
uint8_t gesture;
167+
int ret;
168+
169+
if (!data->gesture_enabled) {
170+
return;
171+
}
172+
173+
ret = ft6146_read_reg(dev, FT6146_REG_GEST_ID, &gesture);
174+
if (ret < 0) {
175+
return;
176+
}
177+
178+
if (gesture != FT6146_GESTURE_NO_GESTURE) {
179+
/* Report gesture event */
180+
input_report_key(dev, INPUT_BTN_TOUCH, 0, true, K_FOREVER);
181+
input_report_key(dev, gesture, 1, true, K_FOREVER);
182+
}
183+
}
184+
#endif
185+
static void ft6146_work_handler(struct k_work *work)
186+
{
187+
struct ft6146_data *data = CONTAINER_OF(work, struct ft6146_data, work);
188+
const struct device *dev = data->dev;
189+
190+
ft6146_process_touch(dev, 0);
191+
}
192+
193+
#ifndef CONFIG_INPUT_FT6146_INTERRUPT
194+
static void ft6146_poll_timer_handler(struct k_timer *timer)
195+
{
196+
struct ft6146_data *data = CONTAINER_OF(timer, struct ft6146_data, poll_timer);
197+
k_work_submit(&data->work);
198+
}
199+
#else
200+
static void ft6146_isr_handler(const struct device *dev, struct gpio_callback *cb,
201+
uint32_t pins)
202+
{
203+
struct ft6146_data *data = CONTAINER_OF(cb, struct ft6146_data, int_cb);
204+
205+
k_work_submit(&data->work);
206+
}
207+
#endif
208+
209+
static int ft6146_reset(const struct device *dev)
210+
{
211+
const struct ft6146_config *config = dev->config;
212+
int ret;
213+
214+
if (!config->reset_gpio.port) {
215+
return 0;
216+
}
217+
218+
/* Assert reset */
219+
ret = gpio_pin_set_dt(&config->reset_gpio, 0);
220+
if (ret < 0) {
221+
LOG_ERR("Failed to assert reset: %d", ret);
222+
return ret;
223+
}
224+
225+
k_msleep(FT6146_RESET_DELAY_MS);
226+
227+
/* De-assert reset */
228+
ret = gpio_pin_set_dt(&config->reset_gpio, 1);
229+
if (ret < 0) {
230+
LOG_ERR("Failed to de-assert reset: %d", ret);
231+
return ret;
232+
}
233+
234+
k_msleep(FT6146_POST_RESET_DELAY_MS);
235+
236+
return 0;
237+
}
238+
239+
static int ft6146_init(const struct device *dev)
240+
{
241+
const struct ft6146_config *config = dev->config;
242+
struct ft6146_data *data = dev->data;
243+
int ret;
244+
uint8_t id;
245+
246+
/* Initialize I2C */
247+
if (!device_is_ready(config->i2c.bus)) {
248+
LOG_ERR("I2C bus not ready");
249+
return -ENODEV;
250+
}
251+
252+
data->dev = dev;
253+
/* Initialize reset GPIO if present */
254+
if (config->reset_gpio.port) {
255+
if (!device_is_ready(config->reset_gpio.port)) {
256+
LOG_ERR("Reset GPIO not ready");
257+
return -ENODEV;
258+
}
259+
260+
ret = gpio_pin_configure_dt(&config->reset_gpio, GPIO_OUTPUT_INACTIVE);
261+
if (ret < 0) {
262+
LOG_ERR("Failed to configure reset GPIO: %d", ret);
263+
return ret;
264+
}
265+
266+
/* Perform reset sequence */
267+
ret = ft6146_reset(dev);
268+
if (ret < 0) {
269+
return ret;
270+
}
271+
}
272+
273+
ret = i2c_reg_read_byte_dt(&config->i2c, FT6146_READ_ID_H, &id);
274+
if (ret < 0) {
275+
LOG_ERR("Failed to read ID_H: %d", ret);
276+
return ret;
277+
}
278+
LOG_INF("ft6146 chip ID: 0x%x", id);
279+
ret = i2c_reg_read_byte_dt(&config->i2c, FT6146_READ_ID_L, &id);
280+
if (ret < 0) {
281+
LOG_ERR("Failed to read ID_L: %d", ret);
282+
return ret;
283+
}
284+
LOG_INF("ft6146 chip ID: 0x%x", id);
285+
286+
#ifdef CONFIG_INPUT_FT6146_INTERRUPT
287+
ret = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
288+
if (ret < 0) {
289+
LOG_ERR("Failed to configure interrupt GPIO: %d", ret);
290+
return ret;
291+
}
292+
293+
ret = gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
294+
if (ret < 0) {
295+
LOG_ERR("Failed to configure interrupt: %d", ret);
296+
return ret;
297+
}
298+
299+
gpio_init_callback(&data->int_cb, ft6146_isr_handler, BIT(config->int_gpio.pin));
300+
ret = gpio_add_callback(config->int_gpio.port, &data->int_cb);
301+
if (ret < 0) {
302+
LOG_ERR("Failed to add callback: %d", ret);
303+
return ret;
304+
}
305+
#else
306+
/* Initialize polling timer */
307+
k_timer_init(&data->poll_timer, ft6146_poll_timer_handler, NULL);
308+
k_timer_start(&data->poll_timer, K_MSEC(CONFIG_INPUT_FT6416_PERIOD),
309+
K_MSEC(CONFIG_INPUT_FT6416_PERIOD));
310+
#endif
311+
#if 0
312+
/* Initialize reset GPIO if present */
313+
if (config->reset_gpio.port) {
314+
if (!device_is_ready(config->reset_gpio.port)) {
315+
LOG_ERR("Reset GPIO not ready");
316+
return -ENODEV;
317+
}
318+
319+
ret = gpio_pin_configure_dt(&config->reset_gpio, GPIO_OUTPUT_INACTIVE);
320+
if (ret < 0) {
321+
LOG_ERR("Failed to configure reset GPIO: %d", ret);
322+
return ret;
323+
}
324+
325+
/* Perform reset sequence */
326+
ret = ft6146_reset(dev);
327+
if (ret < 0) {
328+
return ret;
329+
}
330+
}
331+
#endif
332+
/* Initialize work queue */
333+
k_work_init(&data->work, ft6146_work_handler);
334+
335+
LOG_INF("FT6146 initialized");
336+
return 0;
337+
}
338+
339+
#define FT6146_INIT(n) \
340+
static struct ft6146_data ft6146_data_##n; \
341+
\
342+
static const struct ft6146_config ft6146_config_##n = { \
343+
.i2c = I2C_DT_SPEC_INST_GET(n), \
344+
.reset_gpio = GPIO_DT_SPEC_INST_GET_OR(n, reset_gpios, {0}), \
345+
COND_CODE_1(CONFIG_INPUT_FT6146_INTERRUPT, \
346+
(.int_gpio = GPIO_DT_SPEC_INST_GET(n, int_gpios),), ()) \
347+
}; \
348+
\
349+
DEVICE_DT_INST_DEFINE(n, ft6146_init, NULL, \
350+
&ft6146_data_##n, \
351+
&ft6146_config_##n, \
352+
POST_KERNEL, \
353+
CONFIG_INPUT_INIT_PRIORITY, \
354+
NULL);
355+
356+
DT_INST_FOREACH_STATUS_OKAY(FT6146_INIT)

0 commit comments

Comments
 (0)