|
| 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 | +#define CTP_DOWN (0U) |
| 72 | +#define CTP_UP (1U) |
| 73 | +#define CTP_MOVE (2U) |
| 74 | +#define CTP_RESERVE (3U) |
| 75 | + |
| 76 | +struct ft6146_data { |
| 77 | + const struct device *dev; |
| 78 | + struct gpio_callback int_cb; |
| 79 | + struct k_work work; |
| 80 | + struct k_timer poll_timer; |
| 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 | +}; |
| 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[14]; |
| 94 | + uint8_t touch_num = 0; |
| 95 | + uint8_t event_flag = 0; |
| 96 | + uint16_t x, y; |
| 97 | + int id = 0; |
| 98 | + int ret; |
| 99 | + |
| 100 | + /* Read touch data */ |
| 101 | + ret = i2c_burst_read_dt(&config->i2c, FT6146_REG_GEST_ID, point_data, sizeof(point_data)); |
| 102 | + if (ret < 0) { |
| 103 | + LOG_ERR("Failed to read touch data: %d", ret); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + touch_num = point_data[1] & 0x0f; |
| 108 | + LOG_DBG("touch_num: %d", touch_num); |
| 109 | + |
| 110 | + if (touch_num < MAX_POINT_NUM) { |
| 111 | + for (int i = 0; i < touch_num; i++) { |
| 112 | + event_flag = (point_data[2 + i * 6] >> 6) & 0x03; |
| 113 | + id = point_data[4 + i * 6] >> 4; |
| 114 | + x = ((point_data[2 + i * 6] & 0x0F) << 8) + point_data[3 + i * 6]; |
| 115 | + y = ((point_data[4 + i * 6] & 0x0F) << 8) + point_data[5 + i * 6]; |
| 116 | + |
| 117 | + LOG_DBG("event_flag:%d, id:%d, x:%d, y:%d", event_flag, id, x, y); |
| 118 | + if (event_flag == CTP_DOWN) { |
| 119 | + input_touchscreen_report_pos(dev, x, y, K_FOREVER); |
| 120 | + input_report_key(dev, INPUT_BTN_TOUCH, 1, true, K_FOREVER); |
| 121 | + } else if (event_flag == CTP_UP) { |
| 122 | + input_report_key(dev, INPUT_BTN_TOUCH, 0, true, K_FOREVER); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +static void ft6146_work_handler(struct k_work *work) |
| 129 | +{ |
| 130 | + struct ft6146_data *data = CONTAINER_OF(work, struct ft6146_data, work); |
| 131 | + const struct device *dev = data->dev; |
| 132 | + |
| 133 | + ft6146_process_touch(dev, 0); |
| 134 | +} |
| 135 | + |
| 136 | +#ifndef CONFIG_INPUT_FT6146_INTERRUPT |
| 137 | +static void ft6146_poll_timer_handler(struct k_timer *timer) |
| 138 | +{ |
| 139 | + struct ft6146_data *data = CONTAINER_OF(timer, struct ft6146_data, poll_timer); |
| 140 | + |
| 141 | + k_work_submit(&data->work); |
| 142 | +} |
| 143 | +#else |
| 144 | +static void ft6146_isr_handler(const struct device *dev, struct gpio_callback *cb, uint32_t pins) |
| 145 | +{ |
| 146 | + struct ft6146_data *data = CONTAINER_OF(cb, struct ft6146_data, int_cb); |
| 147 | + |
| 148 | + k_work_submit(&data->work); |
| 149 | +} |
| 150 | +#endif |
| 151 | + |
| 152 | +static int ft6146_reset(const struct device *dev) |
| 153 | +{ |
| 154 | + const struct ft6146_config *config = dev->config; |
| 155 | + int ret; |
| 156 | + |
| 157 | + if (!config->reset_gpio.port) { |
| 158 | + return 0; |
| 159 | + } |
| 160 | + |
| 161 | + /* Assert reset */ |
| 162 | + ret = gpio_pin_set_dt(&config->reset_gpio, 0); |
| 163 | + if (ret < 0) { |
| 164 | + LOG_ERR("Failed to assert reset: %d", ret); |
| 165 | + return ret; |
| 166 | + } |
| 167 | + |
| 168 | + k_msleep(FT6146_RESET_DELAY_MS); |
| 169 | + |
| 170 | + /* De-assert reset */ |
| 171 | + ret = gpio_pin_set_dt(&config->reset_gpio, 1); |
| 172 | + if (ret < 0) { |
| 173 | + LOG_ERR("Failed to de-assert reset: %d", ret); |
| 174 | + return ret; |
| 175 | + } |
| 176 | + |
| 177 | + k_msleep(FT6146_POST_RESET_DELAY_MS); |
| 178 | + |
| 179 | + return ret; |
| 180 | +} |
| 181 | + |
| 182 | +static int ft6146_init(const struct device *dev) |
| 183 | +{ |
| 184 | + const struct ft6146_config *config = dev->config; |
| 185 | + struct ft6146_data *data = dev->data; |
| 186 | + int ret; |
| 187 | + uint8_t id; |
| 188 | + |
| 189 | + /* Initialize I2C */ |
| 190 | + if (!device_is_ready(config->i2c.bus)) { |
| 191 | + LOG_ERR("I2C bus not ready"); |
| 192 | + return -ENODEV; |
| 193 | + } |
| 194 | + |
| 195 | + data->dev = dev; |
| 196 | + /* Initialize reset GPIO if present */ |
| 197 | + if (config->reset_gpio.port) { |
| 198 | + if (!device_is_ready(config->reset_gpio.port)) { |
| 199 | + LOG_ERR("Reset GPIO not ready"); |
| 200 | + return -ENODEV; |
| 201 | + } |
| 202 | + |
| 203 | + ret = gpio_pin_configure_dt(&config->reset_gpio, GPIO_OUTPUT_INACTIVE); |
| 204 | + if (ret < 0) { |
| 205 | + LOG_ERR("Failed to configure reset GPIO: %d", ret); |
| 206 | + return ret; |
| 207 | + } |
| 208 | + |
| 209 | + /* Perform reset sequence */ |
| 210 | + ret = ft6146_reset(dev); |
| 211 | + if (ret < 0) { |
| 212 | + return ret; |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + ret = i2c_reg_read_byte_dt(&config->i2c, FT6146_READ_ID_H, &id); |
| 217 | + if (ret < 0) { |
| 218 | + LOG_ERR("Failed to read ID_H: %d", ret); |
| 219 | + return ret; |
| 220 | + } |
| 221 | + LOG_INF("ft6146 chip ID_H: 0x%x", id); |
| 222 | + ret = i2c_reg_read_byte_dt(&config->i2c, FT6146_READ_ID_L, &id); |
| 223 | + if (ret < 0) { |
| 224 | + LOG_ERR("Failed to read ID_L: %d", ret); |
| 225 | + return ret; |
| 226 | + } |
| 227 | + LOG_INF("ft6146 chip ID_L: 0x%x", id); |
| 228 | + |
| 229 | +#ifdef CONFIG_INPUT_FT6146_INTERRUPT |
| 230 | + ret = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT); |
| 231 | + if (ret < 0) { |
| 232 | + LOG_ERR("Failed to configure interrupt GPIO: %d", ret); |
| 233 | + return ret; |
| 234 | + } |
| 235 | + |
| 236 | + ret = gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE); |
| 237 | + if (ret < 0) { |
| 238 | + LOG_ERR("Failed to configure interrupt: %d", ret); |
| 239 | + return ret; |
| 240 | + } |
| 241 | + |
| 242 | + gpio_init_callback(&data->int_cb, ft6146_isr_handler, BIT(config->int_gpio.pin)); |
| 243 | + ret = gpio_add_callback(config->int_gpio.port, &data->int_cb); |
| 244 | + if (ret < 0) { |
| 245 | + LOG_ERR("Failed to add callback: %d", ret); |
| 246 | + return ret; |
| 247 | + } |
| 248 | +#else |
| 249 | + /* Initialize polling timer */ |
| 250 | + k_timer_init(&data->poll_timer, ft6146_poll_timer_handler, NULL); |
| 251 | + k_timer_start(&data->poll_timer, K_MSEC(CONFIG_INPUT_FT6416_PERIOD), |
| 252 | + K_MSEC(CONFIG_INPUT_FT6416_PERIOD)); |
| 253 | +#endif |
| 254 | + |
| 255 | + /* Initialize work queue */ |
| 256 | + k_work_init(&data->work, ft6146_work_handler); |
| 257 | + |
| 258 | + LOG_INF("FT6146 initialized"); |
| 259 | + |
| 260 | + return ret; |
| 261 | +} |
| 262 | + |
| 263 | +#define FT6146_INIT(n) \ |
| 264 | + static struct ft6146_data ft6146_data_##n; \ |
| 265 | + \ |
| 266 | + static const struct ft6146_config ft6146_config_##n = { \ |
| 267 | + .common = INPUT_TOUCH_DT_INST_COMMON_CONFIG_INIT(index), \ |
| 268 | + .i2c = I2C_DT_SPEC_INST_GET(n), \ |
| 269 | + .reset_gpio = GPIO_DT_SPEC_INST_GET_OR(n, reset_gpios, {0}), \ |
| 270 | + COND_CODE_1(CONFIG_INPUT_FT6146_INTERRUPT, \ |
| 271 | + (.int_gpio = GPIO_DT_SPEC_INST_GET(n, int_gpios),), ()) }; \ |
| 272 | + \ |
| 273 | + DEVICE_DT_INST_DEFINE(n, ft6146_init, NULL, &ft6146_data_##n, &ft6146_config_##n, \ |
| 274 | + POST_KERNEL, CONFIG_INPUT_INIT_PRIORITY, NULL); |
| 275 | + |
| 276 | +DT_INST_FOREACH_STATUS_OKAY(FT6146_INIT) |
0 commit comments