1 // SPDX-License-Identifier: GPL-2.0
4 * TINKER BOARD FT5406 touch driver.
6 * Copyright (c) 2016 ASUSTek Computer Inc.
7 * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/i2c.h>
24 #include <linux/input.h>
25 #include <linux/input/mt.h>
26 #include <linux/module.h>
27 #include <linux/workqueue.h>
29 #define LOG_DBG(fmt, arg...) pr_debug("tinker-ft5406: %s: "fmt, __func__, ##arg)
30 #define LOG_INFO(fmt, arg...) pr_info("tinker-ft5406: %s: "fmt, __func__, ##arg)
31 #define LOG_ERR(fmt, arg...) pr_err("tinker-ft5406: %s: "fmt, __func__, ##arg)
33 #define RETRY_COUNT 10
34 #define FT_ONE_TCH_LEN 6
36 #define FT_REG_FW_VER 0xA6
37 #define FT_REG_FW_MIN_VER 0xB2
38 #define FT_REG_FW_SUB_MIN_VER 0xB3
40 #define VALID_TD_STATUS_VAL 10
41 #define MAX_TOUCH_POINTS 1
44 #define FT_MAX_ID 0x0F
46 #define FT_TOUCH_X_H 0
47 #define FT_TOUCH_X_L 1
48 #define FT_TOUCH_Y_H 2
49 #define FT_TOUCH_Y_L 3
50 #define FT_TOUCH_EVENT 0
53 #define FT_TOUCH_X_H_REG 3
54 #define FT_TOUCH_X_L_REG 4
55 #define FT_TOUCH_Y_H_REG 5
56 #define FT_TOUCH_Y_L_REG 6
57 #define FT_TD_STATUS_REG 2
58 #define FT_TOUCH_EVENT_REG 3
59 #define FT_TOUCH_ID_REG 5
61 #define FT_TOUCH_DOWN 0
62 #define FT_TOUCH_CONTACT 2
65 u16 au16_x[MAX_TOUCH_POINTS]; /*x coordinate */
66 u16 au16_y[MAX_TOUCH_POINTS]; /*y coordinate */
67 u8 au8_touch_event[MAX_TOUCH_POINTS]; /*touch event: 0:down; 1:up; 2:contact */
68 u8 au8_finger_id[MAX_TOUCH_POINTS]; /*touch ID */
74 struct tinker_ft5406_data {
76 struct i2c_client *client;
77 struct input_dev *input_dev;
78 struct ts_event event;
79 struct work_struct ft5406_work;
89 struct tinker_ft5406_data *g_ts_data;
91 static int fts_i2c_read(struct i2c_client *client, char *writebuf,
92 int writelen, char *readbuf, int readlen)
97 struct i2c_msg msgs[] = {
105 .addr = client->addr,
111 ret = i2c_transfer(client->adapter, msgs, 2);
113 LOG_ERR("i2c read error, %d\n", ret);
115 struct i2c_msg msgs[] = {
117 .addr = client->addr,
123 ret = i2c_transfer(client->adapter, msgs, 1);
125 LOG_ERR("i2c read error, %d\n", ret);
131 static int fts_read_reg(struct i2c_client *client, u8 addr, u8 *val)
133 return fts_i2c_read(client, &addr, 1, val, 1);
136 static int fts_check_fw_ver(struct i2c_client *client)
138 u8 reg_addr, fw_ver[3];
141 reg_addr = FT_REG_FW_VER;
142 ret = fts_i2c_read(client, ®_addr, 1, &fw_ver[0], 1);
146 reg_addr = FT_REG_FW_MIN_VER;
147 ret = fts_i2c_read(client, ®_addr, 1, &fw_ver[1], 1);
151 reg_addr = FT_REG_FW_SUB_MIN_VER;
152 ret = fts_i2c_read(client, ®_addr, 1, &fw_ver[2], 1);
156 LOG_INFO("Firmware version = %d.%d.%d\n", fw_ver[0], fw_ver[1], fw_ver[2]);
163 static int fts_read_td_status(struct tinker_ft5406_data *ts_data)
168 ret = fts_read_reg(ts_data->client, FT_TD_STATUS_REG, &td_status);
170 LOG_ERR("get reg td_status failed, %d\n", ret);
173 return (int)td_status;
176 static int fts_read_touchdata(struct tinker_ft5406_data *ts_data)
178 struct ts_event *event = &ts_data->event;
180 u8 buf[FT_ONE_TCH_LEN-2] = { 0 };
181 u8 reg_addr, pointid = FT_MAX_ID;
183 for (i = 0; i < event->touch_point && i < MAX_TOUCH_POINTS; i++) {
184 reg_addr = FT_TOUCH_X_H_REG + (i * FT_ONE_TCH_LEN);
185 ret = fts_i2c_read(ts_data->client, ®_addr, 1, buf, FT_ONE_TCH_LEN-2);
187 LOG_ERR("read touchdata failed.\n");
191 pointid = (buf[FT_TOUCH_ID]) >> 4;
192 if (pointid >= MAX_TOUCH_POINTS)
194 event->au8_finger_id[i] = pointid;
195 event->au16_x[i] = (s16) (buf[FT_TOUCH_X_H] & 0x0F) << 8 | (s16) buf[FT_TOUCH_X_L];
196 event->au16_y[i] = (s16) (buf[FT_TOUCH_Y_H] & 0x0F) << 8 | (s16) buf[FT_TOUCH_Y_L];
197 event->au8_touch_event[i] = buf[FT_TOUCH_EVENT] >> 6;
199 if (ts_data->xy_reverse) {
200 event->au16_x[i] = ts_data->screen_width - event->au16_x[i] - 1;
201 event->au16_y[i] = ts_data->screen_height - event->au16_y[i] - 1;
204 event->pressure = FT_PRESS;
209 static void fts_report_value(struct tinker_ft5406_data *ts_data)
211 struct ts_event *event = &ts_data->event;
212 int i, modified_ids = 0, released_ids;
214 for (i = 0; i < event->touch_point && i < MAX_TOUCH_POINTS; i++) {
215 if (event->au8_touch_event[i] == FT_TOUCH_DOWN ||
216 event->au8_touch_event[i] == FT_TOUCH_CONTACT) {
217 modified_ids |= 1 << event->au8_finger_id[i];
218 input_mt_slot(ts_data->input_dev, event->au8_finger_id[i]);
219 input_mt_report_slot_state(ts_data->input_dev, MT_TOOL_FINGER,
221 input_report_abs(ts_data->input_dev, ABS_MT_TOUCH_MAJOR,
223 input_report_abs(ts_data->input_dev, ABS_MT_POSITION_X,
225 input_report_abs(ts_data->input_dev, ABS_MT_POSITION_Y,
228 if (!((1 << event->au8_finger_id[i]) & ts_data->known_ids))
229 LOG_DBG("Touch id-%d: x = %d, y = %d\n",
230 event->au8_finger_id[i],
236 released_ids = ts_data->known_ids & ~modified_ids;
237 for (i = 0; released_ids && i < MAX_TOUCH_POINTS; i++) {
238 if (released_ids & (1<<i)) {
239 LOG_DBG("Release id-%d, known = %x modified = %x\n",
240 i, ts_data->known_ids, modified_ids);
241 input_mt_slot(ts_data->input_dev, i);
242 input_mt_report_slot_state(ts_data->input_dev, MT_TOOL_FINGER, false);
243 modified_ids &= ~(1 << i);
246 ts_data->known_ids = modified_ids;
247 input_mt_report_pointer_emulation(ts_data->input_dev, true);
248 input_sync(ts_data->input_dev);
251 static void fts_retry_clear(struct tinker_ft5406_data *ts_data)
253 if (ts_data->retry_count != 0)
254 ts_data->retry_count = 0;
257 static int fts_retry_wait(struct tinker_ft5406_data *ts_data)
259 if (ts_data->retry_count < RETRY_COUNT) {
260 LOG_INFO("wait and retry, count = %d\n", ts_data->retry_count);
261 ts_data->retry_count++;
265 LOG_ERR("attach retry count\n");
269 static void tinker_ft5406_work(struct work_struct *work)
271 struct ts_event *event = &g_ts_data->event;
272 int ret = 0, td_status;
275 while (!g_ts_data->finish_work) {
276 td_status = fts_read_td_status(g_ts_data);
278 ret = fts_retry_wait(g_ts_data);
280 LOG_ERR("stop touch polling\n");
283 } else if (td_status < VALID_TD_STATUS_VAL + 1 &&
284 (td_status > 0 || g_ts_data->known_ids != 0)) {
285 fts_retry_clear(g_ts_data);
286 memset(event, -1, sizeof(struct ts_event));
287 event->touch_point = td_status;
288 ret = fts_read_touchdata(g_ts_data);
290 fts_report_value(g_ts_data);
292 msleep_interruptible(17);
296 static int tinker_ft5406_open(struct input_dev *dev)
298 schedule_work(&g_ts_data->ft5406_work);
302 static void tinker_ft5406_close(struct input_dev *dev)
304 g_ts_data->finish_work = true;
305 cancel_work_sync(&g_ts_data->ft5406_work);
306 g_ts_data->finish_work = false;
309 static int tinker_ft5406_probe(struct i2c_client *client,
310 const struct i2c_device_id *id)
312 struct input_dev *input_dev;
315 LOG_INFO("address = 0x%x\n", client->addr);
317 g_ts_data = kzalloc(sizeof(struct tinker_ft5406_data), GFP_KERNEL);
318 if (g_ts_data == NULL) {
319 LOG_ERR("no memory for device\n");
323 g_ts_data->client = client;
324 i2c_set_clientdata(client, g_ts_data);
326 g_ts_data->screen_width = 800;
327 g_ts_data->screen_height = 480;
328 g_ts_data->xy_reverse = 1;
330 LOG_INFO("width = %d, height = %d, reverse = %d\n",
331 g_ts_data->screen_width, g_ts_data->screen_height, g_ts_data->xy_reverse);
333 ret = fts_check_fw_ver(g_ts_data->client);
335 LOG_ERR("checking touch ic failed\n");
339 input_dev = input_allocate_device();
341 LOG_ERR("failed to allocate input device\n");
342 goto input_allocate_failed;
344 input_dev->name = "fts_ts";
345 input_dev->id.bustype = BUS_I2C;
346 input_dev->dev.parent = &g_ts_data->client->dev;
347 input_dev->open = tinker_ft5406_open;
348 input_dev->close = tinker_ft5406_close;
350 g_ts_data->input_dev = input_dev;
351 input_set_drvdata(input_dev, g_ts_data);
353 __set_bit(EV_SYN, input_dev->evbit);
354 __set_bit(EV_KEY, input_dev->evbit);
355 __set_bit(EV_ABS, input_dev->evbit);
356 __set_bit(BTN_TOUCH, input_dev->keybit);
358 input_mt_init_slots(input_dev, MAX_TOUCH_POINTS, 0);
359 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, g_ts_data->screen_width, 0, 0);
360 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, g_ts_data->screen_height, 0, 0);
362 ret = input_register_device(input_dev);
364 LOG_ERR("Input device registration failed\n");
365 goto input_register_failed;
370 INIT_WORK(&g_ts_data->ft5406_work, tinker_ft5406_work);
375 input_unregister_device(g_ts_data->input_dev);
376 input_register_failed:
377 input_free_device(input_dev);
378 input_allocate_failed:
385 static int tinker_ft5406_remove(struct i2c_client *client)
387 cancel_work_sync(&g_ts_data->ft5406_work);
388 if (g_ts_data->input_dev) {
389 input_unregister_device(g_ts_data->input_dev);
390 input_free_device(g_ts_data->input_dev);
397 static const struct i2c_device_id tinker_ft5406_id[] = {
398 {"tinker_ft5406", 0},
402 static struct i2c_driver tinker_ft5406_driver = {
404 .name = "tinker_ft5406",
406 .probe = tinker_ft5406_probe,
407 .remove = tinker_ft5406_remove,
408 .id_table = tinker_ft5406_id,
410 module_i2c_driver(tinker_ft5406_driver);
412 MODULE_DESCRIPTION("TINKER BOARD FT5406 Touch driver");
413 MODULE_LICENSE("GPL v2");