Skip to content

Commit 6226b7a

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

File tree

5 files changed

+328
-0
lines changed

5 files changed

+328
-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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
help
10+
Enable driver for FT6146 capacitive touch controller.
11+
12+
if INPUT_FT6146
13+
14+
config INPUT_FT6416_PERIOD
15+
int "Sample period"
16+
depends on !INPUT_FT6146_INTERRUPT
17+
default 10
18+
help
19+
Sample period in milliseconds when in polling mode.
20+
21+
config INPUT_FT6146_INTERRUPT
22+
bool "FT6146 interrupt support"
23+
default y if $(dt_compat_any_has_prop,$(DT_COMPAT_FOCALTECH_FT56146),int-gpios)
24+
help
25+
Enable interrupt support for the FT6146 touch controller.
26+
27+
endif # INPUT_FT6146

drivers/input/input_ft6146.c

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
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 MAX_POINT_NUM 2
67+
68+
#define FT6146_READ_ID_H (0xA3)
69+
#define FT6146_READ_ID_L (0x9F)
70+
71+
struct ft6146_data {
72+
const struct device *dev;
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 input_touchscreen_common_config common;
85+
struct i2c_dt_spec i2c;
86+
struct gpio_dt_spec int_gpio;
87+
struct gpio_dt_spec reset_gpio;
88+
uint16_t x_max;
89+
uint16_t y_max;
90+
bool gesture_enabled;
91+
};
92+
93+
static void ft6146_process_touch(const struct device *dev, uint8_t status)
94+
{
95+
const struct ft6146_config *config = dev->config;
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+
103+
/* Read touch data */
104+
ret = i2c_burst_read_dt(&config->i2c, FT6146_REG_GEST_ID, point_data, sizeof(point_data));
105+
if (ret < 0) {
106+
LOG_ERR("Failed to read touch data: %d", ret);
107+
return;
108+
}
109+
110+
touch_num = point_data[1] & 0x0f;
111+
LOG_DBG("%s:touch_num:%d", __func__, touch_num);
112+
if (touch_num > MAX_POINT_NUM) {
113+
return;
114+
} else {
115+
for (int i = 0; i < touch_num; i++) {
116+
event_flag = (point_data[2 + i * 6] >> 6) & 0x03;
117+
id = point_data[4 + i * 6] >> 4;
118+
x = ((point_data[2 + i * 6] & 0x0F) << 8) + point_data[3 + i * 6];
119+
y = ((point_data[4 + i * 6] & 0x0F) << 8) + point_data[5 + i * 6];
120+
input_touchscreen_report_pos(dev, x, y, K_FOREVER);
121+
input_report_key(dev, INPUT_BTN_TOUCH, 1, true, K_FOREVER);
122+
LOG_DBG("%s %d :event_flag:%d, id:%d, x:%d, y:%d", __func__, __LINE__, event_flag, id, x, y);
123+
}
124+
}
125+
}
126+
127+
static void ft6146_work_handler(struct k_work *work)
128+
{
129+
struct ft6146_data *data = CONTAINER_OF(work, struct ft6146_data, work);
130+
const struct device *dev = data->dev;
131+
132+
ft6146_process_touch(dev, 0);
133+
}
134+
135+
#ifndef CONFIG_INPUT_FT6146_INTERRUPT
136+
static void ft6146_poll_timer_handler(struct k_timer *timer)
137+
{
138+
struct ft6146_data *data = CONTAINER_OF(timer, struct ft6146_data, poll_timer);
139+
k_work_submit(&data->work);
140+
}
141+
#else
142+
static void ft6146_isr_handler(const struct device *dev, struct gpio_callback *cb,
143+
uint32_t pins)
144+
{
145+
struct ft6146_data *data = CONTAINER_OF(cb, struct ft6146_data, int_cb);
146+
147+
k_work_submit(&data->work);
148+
}
149+
#endif
150+
151+
static int ft6146_reset(const struct device *dev)
152+
{
153+
const struct ft6146_config *config = dev->config;
154+
int ret;
155+
156+
if (!config->reset_gpio.port) {
157+
return 0;
158+
}
159+
160+
/* Assert reset */
161+
ret = gpio_pin_set_dt(&config->reset_gpio, 0);
162+
if (ret < 0) {
163+
LOG_ERR("Failed to assert reset: %d", ret);
164+
return ret;
165+
}
166+
167+
k_msleep(FT6146_RESET_DELAY_MS);
168+
169+
/* De-assert reset */
170+
ret = gpio_pin_set_dt(&config->reset_gpio, 1);
171+
if (ret < 0) {
172+
LOG_ERR("Failed to de-assert reset: %d", ret);
173+
return ret;
174+
}
175+
176+
k_msleep(FT6146_POST_RESET_DELAY_MS);
177+
178+
return 0;
179+
}
180+
181+
static int ft6146_init(const struct device *dev)
182+
{
183+
const struct ft6146_config *config = dev->config;
184+
struct ft6146_data *data = dev->data;
185+
int ret;
186+
uint8_t id;
187+
188+
/* Initialize I2C */
189+
if (!device_is_ready(config->i2c.bus)) {
190+
LOG_ERR("I2C bus not ready");
191+
return -ENODEV;
192+
}
193+
194+
data->dev = dev;
195+
/* Initialize reset GPIO if present */
196+
if (config->reset_gpio.port) {
197+
if (!device_is_ready(config->reset_gpio.port)) {
198+
LOG_ERR("Reset GPIO not ready");
199+
return -ENODEV;
200+
}
201+
202+
ret = gpio_pin_configure_dt(&config->reset_gpio, GPIO_OUTPUT_INACTIVE);
203+
if (ret < 0) {
204+
LOG_ERR("Failed to configure reset GPIO: %d", ret);
205+
return ret;
206+
}
207+
208+
/* Perform reset sequence */
209+
ret = ft6146_reset(dev);
210+
if (ret < 0) {
211+
return ret;
212+
}
213+
}
214+
215+
ret = i2c_reg_read_byte_dt(&config->i2c, FT6146_READ_ID_H, &id);
216+
if (ret < 0) {
217+
LOG_ERR("Failed to read ID_H: %d", ret);
218+
return ret;
219+
}
220+
LOG_INF("ft6146 chip ID: 0x%x", id);
221+
ret = i2c_reg_read_byte_dt(&config->i2c, FT6146_READ_ID_L, &id);
222+
if (ret < 0) {
223+
LOG_ERR("Failed to read ID_L: %d", ret);
224+
return ret;
225+
}
226+
LOG_INF("ft6146 chip ID: 0x%x", id);
227+
228+
#ifdef CONFIG_INPUT_FT6146_INTERRUPT
229+
ret = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
230+
if (ret < 0) {
231+
LOG_ERR("Failed to configure interrupt GPIO: %d", ret);
232+
return ret;
233+
}
234+
235+
ret = gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
236+
if (ret < 0) {
237+
LOG_ERR("Failed to configure interrupt: %d", ret);
238+
return ret;
239+
}
240+
241+
gpio_init_callback(&data->int_cb, ft6146_isr_handler, BIT(config->int_gpio.pin));
242+
ret = gpio_add_callback(config->int_gpio.port, &data->int_cb);
243+
if (ret < 0) {
244+
LOG_ERR("Failed to add callback: %d", ret);
245+
return ret;
246+
}
247+
#else
248+
/* Initialize polling timer */
249+
k_timer_init(&data->poll_timer, ft6146_poll_timer_handler, NULL);
250+
k_timer_start(&data->poll_timer, K_MSEC(CONFIG_INPUT_FT6416_PERIOD),
251+
K_MSEC(CONFIG_INPUT_FT6416_PERIOD));
252+
#endif
253+
254+
/* Initialize work queue */
255+
k_work_init(&data->work, ft6146_work_handler);
256+
257+
LOG_INF("FT6146 initialized");
258+
return 0;
259+
}
260+
261+
#define FT6146_INIT(n) \
262+
static struct ft6146_data ft6146_data_##n; \
263+
\
264+
static const struct ft6146_config ft6146_config_##n = { \
265+
.common = INPUT_TOUCH_DT_INST_COMMON_CONFIG_INIT(index), \
266+
.i2c = I2C_DT_SPEC_INST_GET(n), \
267+
.reset_gpio = GPIO_DT_SPEC_INST_GET_OR(n, reset_gpios, {0}), \
268+
COND_CODE_1(CONFIG_INPUT_FT6146_INTERRUPT, \
269+
(.int_gpio = GPIO_DT_SPEC_INST_GET(n, int_gpios),), ()) \
270+
}; \
271+
\
272+
DEVICE_DT_INST_DEFINE(n, ft6146_init, NULL, \
273+
&ft6146_data_##n, \
274+
&ft6146_config_##n, \
275+
POST_KERNEL, \
276+
CONFIG_INPUT_INIT_PRIORITY, \
277+
NULL);
278+
279+
DT_INST_FOREACH_STATUS_OKAY(FT6146_INIT)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
description: FocalTech FT6146 capacitive touch controller
2+
3+
compatible: "focaltech,ft6146"
4+
5+
include: i2c-device.yaml
6+
7+
properties:
8+
reg:
9+
required: true
10+
description: I2C address of the FT6146 (typically 0x38)
11+
12+
int-gpios:
13+
type: phandle-array
14+
required: false
15+
description: Interrupt GPIO specification, active low
16+
17+
reset-gpios:
18+
type: phandle-array
19+
required: false
20+
description: Reset GPIO specification, active low

0 commit comments

Comments
 (0)