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;
90 struct tinker_ft5406_data *g_ts_data;
92 static int fts_i2c_read(struct i2c_client *client, char *writebuf,
93 int writelen, char *readbuf, int readlen)
98 struct i2c_msg msgs[] = {
100 .addr = client->addr,
106 .addr = client->addr,
112 ret = i2c_transfer(client->adapter, msgs, 2);
114 LOG_ERR("i2c read error, %d\n", ret);
116 struct i2c_msg msgs[] = {
118 .addr = client->addr,
124 ret = i2c_transfer(client->adapter, msgs, 1);
126 LOG_ERR("i2c read error, %d\n", ret);
132 static int fts_read_reg(struct i2c_client *client, u8 addr, u8 *val)
134 return fts_i2c_read(client, &addr, 1, val, 1);
137 static int fts_check_fw_ver(struct i2c_client *client)
139 u8 reg_addr, fw_ver[3];
142 reg_addr = FT_REG_FW_VER;
143 ret = fts_i2c_read(client, ®_addr, 1, &fw_ver[0], 1);
147 reg_addr = FT_REG_FW_MIN_VER;
148 ret = fts_i2c_read(client, ®_addr, 1, &fw_ver[1], 1);
152 reg_addr = FT_REG_FW_SUB_MIN_VER;
153 ret = fts_i2c_read(client, ®_addr, 1, &fw_ver[2], 1);
157 LOG_INFO("Firmware version = %d.%d.%d\n", fw_ver[0], fw_ver[1], fw_ver[2]);
164 static int fts_read_td_status(struct tinker_ft5406_data *ts_data)
169 ret = fts_read_reg(ts_data->client, FT_TD_STATUS_REG, &td_status);
171 LOG_ERR("get reg td_status failed, %d\n", ret);
174 return (int)td_status;
177 static int fts_read_touchdata(struct tinker_ft5406_data *ts_data)
179 struct ts_event *event = &ts_data->event;
181 u8 buf[FT_ONE_TCH_LEN-2] = { 0 };
182 u8 reg_addr, pointid = FT_MAX_ID;
184 for (i = 0; i < event->touch_point && i < MAX_TOUCH_POINTS; i++) {
185 reg_addr = FT_TOUCH_X_H_REG + (i * FT_ONE_TCH_LEN);
186 ret = fts_i2c_read(ts_data->client, ®_addr, 1, buf, FT_ONE_TCH_LEN-2);
188 LOG_ERR("read touchdata failed.\n");
192 pointid = (buf[FT_TOUCH_ID]) >> 4;
193 if (pointid >= MAX_TOUCH_POINTS)
195 event->au8_finger_id[i] = pointid;
196 event->au16_x[i] = (s16) (buf[FT_TOUCH_X_H] & 0x0F) << 8 | (s16) buf[FT_TOUCH_X_L];
197 event->au16_y[i] = (s16) (buf[FT_TOUCH_Y_H] & 0x0F) << 8 | (s16) buf[FT_TOUCH_Y_L];
198 event->au8_touch_event[i] = buf[FT_TOUCH_EVENT] >> 6;
200 if (ts_data->xy_reverse) {
201 event->au16_x[i] = ts_data->screen_width - event->au16_x[i] - 1;
202 event->au16_y[i] = ts_data->screen_height - event->au16_y[i] - 1;
205 event->pressure = FT_PRESS;
210 static void fts_report_value(struct tinker_ft5406_data *ts_data)
212 struct ts_event *event = &ts_data->event;
213 int i, modified_ids = 0, released_ids;
215 for (i = 0; i < event->touch_point && i < MAX_TOUCH_POINTS; i++) {
216 if (event->au8_touch_event[i] == FT_TOUCH_DOWN ||
217 event->au8_touch_event[i] == FT_TOUCH_CONTACT) {
218 modified_ids |= 1 << event->au8_finger_id[i];
219 input_mt_slot(ts_data->input_dev, event->au8_finger_id[i]);
220 input_mt_report_slot_state(ts_data->input_dev, MT_TOOL_FINGER,
222 input_report_abs(ts_data->input_dev, ABS_MT_TOUCH_MAJOR,
224 input_report_abs(ts_data->input_dev, ABS_MT_POSITION_X,
226 input_report_abs(ts_data->input_dev, ABS_MT_POSITION_Y,
229 if (!((1 << event->au8_finger_id[i]) & ts_data->known_ids))
230 LOG_DBG("Touch id-%d: x = %d, y = %d\n",
231 event->au8_finger_id[i],
237 released_ids = ts_data->known_ids & ~modified_ids;
238 for (i = 0; released_ids && i < MAX_TOUCH_POINTS; i++) {
239 if (released_ids & (1<<i)) {
240 LOG_DBG("Release id-%d, known = %x modified = %x\n",
241 i, ts_data->known_ids, modified_ids);
242 input_mt_slot(ts_data->input_dev, i);
243 input_mt_report_slot_state(ts_data->input_dev, MT_TOOL_FINGER, false);
244 modified_ids &= ~(1 << i);
247 ts_data->known_ids = modified_ids;
248 input_mt_report_pointer_emulation(ts_data->input_dev, true);
249 input_sync(ts_data->input_dev);
252 static void fts_retry_clear(struct tinker_ft5406_data *ts_data)
254 if (ts_data->retry_count != 0)
255 ts_data->retry_count = 0;
258 static int fts_retry_wait(struct tinker_ft5406_data *ts_data)
260 if (ts_data->retry_count < RETRY_COUNT) {
261 LOG_INFO("wait and retry, count = %d\n", ts_data->retry_count);
262 ts_data->retry_count++;
266 LOG_ERR("attach retry count\n");
270 static void tinker_ft5406_work(struct work_struct *work)
272 struct ts_event *event = &g_ts_data->event;
273 int ret = 0, count = 5, td_status;
276 ret = fts_check_fw_ver(g_ts_data->client);
279 LOG_INFO("checking touch ic, countdown: %d\n", count);
284 LOG_ERR("checking touch ic timeout, %d\n", ret);
285 g_ts_data->is_polling = 0;
290 while (!g_ts_data->finish_work) {
291 td_status = fts_read_td_status(g_ts_data);
293 ret = fts_retry_wait(g_ts_data);
295 LOG_ERR("stop touch polling\n");
296 g_ts_data->is_polling = 0;
299 } else if (td_status < VALID_TD_STATUS_VAL + 1 &&
300 (td_status > 0 || g_ts_data->known_ids != 0)) {
301 fts_retry_clear(g_ts_data);
302 memset(event, -1, sizeof(struct ts_event));
303 event->touch_point = td_status;
304 ret = fts_read_touchdata(g_ts_data);
306 fts_report_value(g_ts_data);
308 msleep_interruptible(17);
312 static int tinker_ft5406_open(struct input_dev *dev)
314 schedule_work(&g_ts_data->ft5406_work);
318 static void tinker_ft5406_close(struct input_dev *dev)
320 g_ts_data->finish_work = true;
321 cancel_work_sync(&g_ts_data->ft5406_work);
322 g_ts_data->finish_work = false;
325 static int tinker_ft5406_probe(struct i2c_client *client,
326 const struct i2c_device_id *id)
328 struct input_dev *input_dev;
331 LOG_INFO("address = 0x%x\n", client->addr);
333 g_ts_data = kzalloc(sizeof(struct tinker_ft5406_data), GFP_KERNEL);
334 if (g_ts_data == NULL) {
335 LOG_ERR("no memory for device\n");
339 g_ts_data->client = client;
340 i2c_set_clientdata(client, g_ts_data);
342 g_ts_data->screen_width = 800;
343 g_ts_data->screen_height = 480;
344 g_ts_data->xy_reverse = 1;
346 LOG_INFO("width = %d, height = %d, reverse = %d\n",
347 g_ts_data->screen_width, g_ts_data->screen_height, g_ts_data->xy_reverse);
349 input_dev = input_allocate_device();
351 LOG_ERR("failed to allocate input device\n");
352 goto input_allocate_failed;
354 input_dev->name = "fts_ts";
355 input_dev->id.bustype = BUS_I2C;
356 input_dev->dev.parent = &g_ts_data->client->dev;
357 input_dev->open = tinker_ft5406_open;
358 input_dev->close = tinker_ft5406_close;
360 g_ts_data->input_dev = input_dev;
361 input_set_drvdata(input_dev, g_ts_data);
363 __set_bit(EV_SYN, input_dev->evbit);
364 __set_bit(EV_KEY, input_dev->evbit);
365 __set_bit(EV_ABS, input_dev->evbit);
366 __set_bit(BTN_TOUCH, input_dev->keybit);
368 input_mt_init_slots(input_dev, MAX_TOUCH_POINTS, 0);
369 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, g_ts_data->screen_width, 0, 0);
370 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, g_ts_data->screen_height, 0, 0);
372 ret = input_register_device(input_dev);
374 LOG_ERR("Input device registration failed\n");
375 goto input_register_failed;
378 INIT_WORK(&g_ts_data->ft5406_work, tinker_ft5406_work);
382 input_register_failed:
383 input_free_device(input_dev);
384 input_allocate_failed:
390 static int tinker_ft5406_remove(struct i2c_client *client)
392 cancel_work_sync(&g_ts_data->ft5406_work);
393 if (g_ts_data->input_dev) {
394 input_unregister_device(g_ts_data->input_dev);
395 input_free_device(g_ts_data->input_dev);
402 static const struct i2c_device_id tinker_ft5406_id[] = {
403 {"tinker_ft5406", 0},
407 static struct i2c_driver tinker_ft5406_driver = {
409 .name = "tinker_ft5406",
411 .probe = tinker_ft5406_probe,
412 .remove = tinker_ft5406_remove,
413 .id_table = tinker_ft5406_id,
415 module_i2c_driver(tinker_ft5406_driver);
417 MODULE_DESCRIPTION("TINKER BOARD FT5406 Touch driver");
418 MODULE_LICENSE("GPL v2");