Skip to content

Commit f5498dc

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

File tree

6 files changed

+306
-0
lines changed

6 files changed

+306
-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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright (c) 2025 Qingsong Gou <[email protected]>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
menuconfig INPUT_FT6146
5+
bool "FT6146 touch controller"
6+
default y
7+
depends on DT_HAS_FOCALTECH_FT6146_ENABLED
8+
select I2C
9+
select INPUT_TOUCH
10+
help
11+
Enable driver for FT6146 capacitive touch controller.
12+
13+
if INPUT_FT6146
14+
15+
config INPUT_FT6146_PERIOD
16+
int "Sample period"
17+
depends on !INPUT_FT6146_INTERRUPT
18+
default 10
19+
help
20+
Sample period in milliseconds when in polling mode.
21+
22+
config INPUT_FT6146_INTERRUPT
23+
bool "FT6146 interrupt support"
24+
default y if $(dt_compat_any_has_prop,$(DT_COMPAT_FOCALTECH_FT6146),int-gpios)
25+
help
26+
Enable interrupt support for the FT6146 touch controller.
27+
28+
endif # INPUT_FT6146

drivers/input/input_ft6146.c

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
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/input/input_touch.h>
13+
#include <zephyr/logging/log.h>
14+
15+
LOG_MODULE_REGISTER(ft6146, CONFIG_INPUT_LOG_LEVEL);
16+
17+
/* FT6146 register definitions */
18+
#define FT6146_REG_DEVICE_MODE 0x00
19+
#define FT6146_REG_GEST_ID 0x01
20+
#define FT6146_REG_TD_STATUS 0x02
21+
#define FT6146_REG_P1_XH 0x03
22+
#define FT6146_REG_P1_XL 0x04
23+
#define FT6146_REG_P1_YH 0x05
24+
#define FT6146_REG_P1_YL 0x06
25+
#define FT6146_REG_P1_WEIGHT 0x07
26+
#define FT6146_REG_P1_MISC 0x08
27+
#define FT6146_REG_P2_XH 0x09
28+
#define FT6146_REG_P2_XL 0x0A
29+
#define FT6146_REG_P2_YH 0x0B
30+
#define FT6146_REG_P2_YL 0x0C
31+
#define FT6146_REG_P2_WEIGHT 0x0D
32+
#define FT6146_REG_P2_MISC 0x0E
33+
#define FT6146_REG_THRESHOLD 0x80
34+
#define FT6146_REG_FILTER_COE 0x85
35+
#define FT6146_REG_CTRL 0x86
36+
#define FT6146_REG_TIMEENTERMONITOR 0x87
37+
#define FT6146_REG_PERIODACTIVE 0x88
38+
#define FT6146_REG_PERIODMONITOR 0x89
39+
#define FT6146_REG_RADIAN_VALUE 0x91
40+
#define FT6146_REG_OFFSET_LEFT_RIGHT 0x92
41+
#define FT6146_REG_OFFSET_UP_DOWN 0x93
42+
#define FT6146_REG_DIST_LEFT_RIGHT 0x94
43+
#define FT6146_REG_DIST_UP_DOWN 0x95
44+
#define FT6146_REG_ZOOM_DIS_SQR 0x96
45+
#define FT6146_REG_RADIAN_THRESHOLD 0x97
46+
#define FT6146_REG_SMALL_OBJECT_THRESHOLD 0x98
47+
48+
/* Device mode values */
49+
#define FT6146_DEVICE_MODE_NORMAL 0x00
50+
#define FT6146_DEVICE_MODE_TEST 0x04
51+
#define FT6146_DEVICE_MODE_SYSTEM 0x01
52+
53+
/* Gesture IDs */
54+
#define FT6146_GESTURE_NO_GESTURE 0x00
55+
#define FT6146_GESTURE_MOVE_UP 0x10
56+
#define FT6146_GESTURE_MOVE_RIGHT 0x14
57+
#define FT6146_GESTURE_MOVE_DOWN 0x18
58+
#define FT6146_GESTURE_MOVE_LEFT 0x1C
59+
#define FT6146_GESTURE_ZOOM_IN 0x48
60+
#define FT6146_GESTURE_ZOOM_OUT 0x49
61+
62+
/* Reset timing */
63+
#define FT6146_RESET_DELAY_MS 10
64+
#define FT6146_POST_RESET_DELAY_MS 100
65+
66+
#define CTP_DOWN (0U)
67+
#define CTP_UP (1U)
68+
#define CTP_MOVE (2U)
69+
#define CTP_RESERVE (3U)
70+
71+
#define POSITION_H_MSK 0x0FU
72+
#define EVENT_FLAG_MASK 0xC0U
73+
74+
struct ft6146_data {
75+
const struct device *dev;
76+
struct gpio_callback int_cb;
77+
struct k_work work;
78+
struct k_timer poll_timer;
79+
};
80+
81+
struct ft6146_config {
82+
struct input_touchscreen_common_config common;
83+
struct i2c_dt_spec i2c;
84+
struct gpio_dt_spec reset_gpio;
85+
#ifdef CONFIG_INPUT_FT6146_INTERRUPT
86+
struct gpio_dt_spec int_gpio;
87+
#endif
88+
};
89+
90+
static void ft6146_process_touch(const struct device *dev, uint8_t status)
91+
{
92+
const struct ft6146_config *config = dev->config;
93+
uint8_t point_data[9];
94+
uint8_t event_flag;
95+
uint16_t x, y;
96+
int ret;
97+
98+
/* Read touch data */
99+
ret = i2c_burst_read_dt(&config->i2c, FT6146_REG_DEVICE_MODE, point_data,
100+
sizeof(point_data));
101+
if (ret < 0) {
102+
LOG_ERR("Failed to read touch data: %d", ret);
103+
return;
104+
}
105+
106+
event_flag = FIELD_GET(EVENT_FLAG_MASK, point_data[FT6146_REG_P1_XH]);
107+
x = ((point_data[FT6146_REG_P1_XH] & POSITION_H_MSK) << 8) + point_data[FT6146_REG_P1_XL];
108+
y = ((point_data[FT6146_REG_P1_YH] & POSITION_H_MSK) << 8) + point_data[FT6146_REG_P1_YL];
109+
110+
LOG_DBG("event_flag:%d, x:%d, y:%d", event_flag, x, y);
111+
if ((event_flag == CTP_DOWN) || (event_flag == CTP_MOVE)) {
112+
input_touchscreen_report_pos(dev, x, y, K_FOREVER);
113+
input_report_key(dev, INPUT_BTN_TOUCH, 1, true, K_FOREVER);
114+
} else if (event_flag == CTP_UP) {
115+
input_report_key(dev, INPUT_BTN_TOUCH, 0, true, K_FOREVER);
116+
}
117+
}
118+
119+
static void ft6146_work_handler(struct k_work *work)
120+
{
121+
struct ft6146_data *data = CONTAINER_OF(work, struct ft6146_data, work);
122+
const struct device *dev = data->dev;
123+
124+
ft6146_process_touch(dev, 0);
125+
}
126+
127+
#ifndef CONFIG_INPUT_FT6146_INTERRUPT
128+
static void ft6146_poll_timer_handler(struct k_timer *timer)
129+
{
130+
struct ft6146_data *data = CONTAINER_OF(timer, struct ft6146_data, poll_timer);
131+
132+
k_work_submit(&data->work);
133+
}
134+
#else
135+
static void ft6146_isr_handler(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
136+
{
137+
struct ft6146_data *data = CONTAINER_OF(cb, struct ft6146_data, int_cb);
138+
139+
k_work_submit(&data->work);
140+
}
141+
#endif
142+
143+
static int ft6146_reset(const struct device *dev)
144+
{
145+
const struct ft6146_config *config = dev->config;
146+
int ret;
147+
148+
if (!config->reset_gpio.port) {
149+
return 0;
150+
}
151+
152+
if (!device_is_ready(config->reset_gpio.port)) {
153+
LOG_ERR("Reset GPIO not ready");
154+
return -ENODEV;
155+
}
156+
157+
ret = gpio_pin_configure_dt(&config->reset_gpio, GPIO_OUTPUT_ACTIVE);
158+
if (ret < 0) {
159+
LOG_ERR("Failed to configure reset GPIO: %d", ret);
160+
return ret;
161+
}
162+
163+
k_msleep(FT6146_RESET_DELAY_MS);
164+
165+
/* De-assert reset */
166+
ret = gpio_pin_set_dt(&config->reset_gpio, 0);
167+
if (ret < 0) {
168+
LOG_ERR("Failed to de-assert reset: %d", ret);
169+
return ret;
170+
}
171+
172+
k_msleep(FT6146_POST_RESET_DELAY_MS);
173+
174+
return ret;
175+
}
176+
177+
static int ft6146_init(const struct device *dev)
178+
{
179+
const struct ft6146_config *config = dev->config;
180+
struct ft6146_data *data = dev->data;
181+
int ret;
182+
183+
if (!i2c_is_ready_dt(&config->i2c)) {
184+
LOG_ERR("I2C bus not ready");
185+
return -ENODEV;
186+
}
187+
188+
data->dev = dev;
189+
190+
/* Perform reset sequence */
191+
ret = ft6146_reset(dev);
192+
if (ret < 0) {
193+
return ret;
194+
}
195+
196+
#ifdef CONFIG_INPUT_FT6146_INTERRUPT
197+
if (!gpio_is_ready_dt(&config->int_gpio)) {
198+
LOG_ERR("Interrupt GPIO controller device not ready");
199+
return -ENODEV;
200+
}
201+
202+
ret = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
203+
if (ret < 0) {
204+
LOG_ERR("Failed to configure interrupt GPIO: %d", ret);
205+
return ret;
206+
}
207+
208+
ret = gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
209+
if (ret < 0) {
210+
LOG_ERR("Failed to configure interrupt: %d", ret);
211+
return ret;
212+
}
213+
214+
gpio_init_callback(&data->int_cb, ft6146_isr_handler, BIT(config->int_gpio.pin));
215+
ret = gpio_add_callback(config->int_gpio.port, &data->int_cb);
216+
if (ret < 0) {
217+
LOG_ERR("Failed to add callback: %d", ret);
218+
return ret;
219+
}
220+
#else
221+
/* Initialize polling timer */
222+
k_timer_init(&data->poll_timer, ft6146_poll_timer_handler, NULL);
223+
k_timer_start(&data->poll_timer, K_MSEC(CONFIG_INPUT_FT6146_PERIOD),
224+
K_MSEC(CONFIG_INPUT_FT6146_PERIOD));
225+
#endif
226+
227+
/* Initialize work queue */
228+
k_work_init(&data->work, ft6146_work_handler);
229+
230+
return ret;
231+
}
232+
233+
#define FT6146_INIT(n) \
234+
static struct ft6146_data ft6146_data_##n; \
235+
\
236+
static const struct ft6146_config ft6146_config_##n = { \
237+
.common = INPUT_TOUCH_DT_INST_COMMON_CONFIG_INIT(n), \
238+
.i2c = I2C_DT_SPEC_INST_GET(n), \
239+
.reset_gpio = GPIO_DT_SPEC_INST_GET_OR(n, reset_gpios, {0}), \
240+
COND_CODE_1(CONFIG_INPUT_FT6146_INTERRUPT, \
241+
(.int_gpio = GPIO_DT_SPEC_INST_GET(n, int_gpios),), ()) }; \
242+
\
243+
DEVICE_DT_INST_DEFINE(n, ft6146_init, NULL, &ft6146_data_##n, &ft6146_config_##n, \
244+
POST_KERNEL, CONFIG_INPUT_INIT_PRIORITY, NULL);
245+
246+
DT_INST_FOREACH_STATUS_OKAY(FT6146_INIT)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright (c) 2025 Qingsong Gou <[email protected]>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: FocalTech FT6146 capacitive touch controller
5+
6+
compatible: "focaltech,ft6146"
7+
8+
include: [i2c-device.yaml, touchscreen-common.yaml]
9+
10+
properties:
11+
int-gpios:
12+
type: phandle-array
13+
description: Interrupt GPIO specification, active low
14+
15+
reset-gpios:
16+
type: phandle-array
17+
description: Reset GPIO specification, active low

tests/drivers/build_all/input/app.overlay

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@
3939
compatible = "adc-keys";
4040
io-channels = <&test_adc 0>;
4141
keyup-threshold-mv = <0>;
42+
4243
button_0 {
4344
press-thresholds-mv = <1500>, <1750>;
4445
zephyr,code = <0>;
4546
};
47+
4648
button_1 {
4749
press-thresholds-mv = <2500>, <1750>;
4850
zephyr,code = <1>;
@@ -52,6 +54,7 @@
5254
gpio-keys {
5355
compatible = "gpio-keys";
5456
debounce-interval-ms = <30>;
57+
5558
button_0 {
5659
gpios = <&test_gpio 0 0>;
5760
zephyr,code = <0>;
@@ -61,6 +64,7 @@
6164
gpio-keys-polled {
6265
compatible = "gpio-keys";
6366
debounce-interval-ms = <30>;
67+
6468
button_0 {
6569
gpios = <&test_gpio 0 0>;
6670
zephyr,code = <0>;
@@ -134,6 +138,7 @@
134138

135139
analog_axis {
136140
compatible = "analog-axis";
141+
137142
axis-x {
138143
io-channels = <&test_adc 0>;
139144
out-min = <(-127)>;
@@ -174,6 +179,7 @@
174179

175180
sbus {
176181
compatible = "futaba,sbus";
182+
177183
right_stick_x {
178184
channel = <1>;
179185
type = <INPUT_EV_ABS>;
@@ -196,6 +202,13 @@
196202
int-gpios = <&test_gpio 0 0>;
197203
};
198204

205+
ft6146@0 {
206+
compatible = "focaltech,ft6146";
207+
reg = <0x0>;
208+
int-gpios = <&test_gpio 0 0>;
209+
reset-gpios = <&test_gpio 1 0>;
210+
};
211+
199212
gt911@1 {
200213
compatible = "goodix,gt911";
201214
reg = <0x1>;

0 commit comments

Comments
 (0)