2 * Driver for Goodix Touchscreens
4 * Copyright (c) 2014 Red Hat Inc.
6 * This code is based on gt9xx.c authored by andrew@goodix.com:
8 * 2010 - 2012 Goodix Technology.
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; version 2 of the License.
17 #include <linux/kernel.h>
18 #include <linux/dmi.h>
19 #include <linux/i2c.h>
20 #include <linux/input.h>
21 #include <linux/input/mt.h>
22 #include <linux/module.h>
23 #include <linux/delay.h>
24 #include <linux/irq.h>
25 #include <linux/interrupt.h>
26 #include <linux/slab.h>
27 #include <linux/acpi.h>
29 #include <asm/unaligned.h>
31 struct goodix_ts_data {
32 struct i2c_client *client;
33 struct input_dev *input_dev;
36 unsigned int max_touch_num;
37 unsigned int int_trigger_type;
41 #define GOODIX_MAX_HEIGHT 4096
42 #define GOODIX_MAX_WIDTH 4096
43 #define GOODIX_INT_TRIGGER 1
44 #define GOODIX_CONTACT_SIZE 8
45 #define GOODIX_MAX_CONTACTS 10
47 #define GOODIX_CONFIG_MAX_LENGTH 240
49 /* Register defines */
50 #define GOODIX_READ_COOR_ADDR 0x814E
51 #define GOODIX_REG_CONFIG_DATA 0x8047
52 #define GOODIX_REG_ID 0x8140
54 #define RESOLUTION_LOC 1
55 #define MAX_CONTACTS_LOC 5
58 static const unsigned long goodix_irq_flags[] = {
60 IRQ_TYPE_EDGE_FALLING,
66 * Those tablets have their coordinates origin at the bottom right
67 * of the tablet, as if rotated 180 degrees
69 static const struct dmi_system_id rotated_screen[] = {
70 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
72 .ident = "Teclast X89",
74 /* tPAD is too generic, also match on bios date */
75 DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
76 DMI_MATCH(DMI_BOARD_NAME, "tPAD"),
77 DMI_MATCH(DMI_BIOS_DATE, "12/19/2014"),
81 .ident = "Teclast X98 Pro",
84 * Only match BIOS date, because the manufacturers
85 * BIOS does not report the board name at all
88 DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
89 DMI_MATCH(DMI_BIOS_DATE, "10/28/2015"),
93 .ident = "WinBook TW100",
95 DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
96 DMI_MATCH(DMI_PRODUCT_NAME, "TW100")
100 .ident = "WinBook TW700",
102 DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
103 DMI_MATCH(DMI_PRODUCT_NAME, "TW700")
111 * goodix_i2c_read - read data from a register of the i2c slave device.
113 * @client: i2c device.
114 * @reg: the register to read from.
115 * @buf: raw write data buffer.
116 * @len: length of the buffer to write
118 static int goodix_i2c_read(struct i2c_client *client,
119 u16 reg, u8 *buf, int len)
121 struct i2c_msg msgs[2];
122 u16 wbuf = cpu_to_be16(reg);
126 msgs[0].addr = client->addr;
128 msgs[0].buf = (u8 *)&wbuf;
130 msgs[1].flags = I2C_M_RD;
131 msgs[1].addr = client->addr;
135 ret = i2c_transfer(client->adapter, msgs, 2);
136 return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
139 static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
144 error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR, data,
145 GOODIX_CONTACT_SIZE + 1);
147 dev_err(&ts->client->dev, "I2C transfer error: %d\n", error);
151 if (!(data[0] & 0x80))
154 touch_num = data[0] & 0x0f;
155 if (touch_num > ts->max_touch_num)
159 data += 1 + GOODIX_CONTACT_SIZE;
160 error = goodix_i2c_read(ts->client,
161 GOODIX_READ_COOR_ADDR +
162 1 + GOODIX_CONTACT_SIZE,
164 GOODIX_CONTACT_SIZE * (touch_num - 1));
172 static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data)
174 int id = coor_data[0] & 0x0F;
175 int input_x = get_unaligned_le16(&coor_data[1]);
176 int input_y = get_unaligned_le16(&coor_data[3]);
177 int input_w = get_unaligned_le16(&coor_data[5]);
179 if (ts->rotated_screen) {
180 input_x = ts->abs_x_max - input_x;
181 input_y = ts->abs_y_max - input_y;
184 input_mt_slot(ts->input_dev, id);
185 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
186 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, input_x);
187 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, input_y);
188 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
189 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
193 * goodix_process_events - Process incoming events
195 * @ts: our goodix_ts_data pointer
197 * Called when the IRQ is triggered. Read the current device state, and push
198 * the input events to the user space.
200 static void goodix_process_events(struct goodix_ts_data *ts)
202 u8 point_data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
206 touch_num = goodix_ts_read_input_report(ts, point_data);
210 for (i = 0; i < touch_num; i++)
211 goodix_ts_report_touch(ts,
212 &point_data[1 + GOODIX_CONTACT_SIZE * i]);
214 input_mt_sync_frame(ts->input_dev);
215 input_sync(ts->input_dev);
219 * goodix_ts_irq_handler - The IRQ handler
221 * @irq: interrupt number.
222 * @dev_id: private data pointer.
224 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
226 static const u8 end_cmd[] = {
227 GOODIX_READ_COOR_ADDR >> 8,
228 GOODIX_READ_COOR_ADDR & 0xff,
231 struct goodix_ts_data *ts = dev_id;
233 goodix_process_events(ts);
235 if (i2c_master_send(ts->client, end_cmd, sizeof(end_cmd)) < 0)
236 dev_err(&ts->client->dev, "I2C write end_cmd error\n");
242 * goodix_read_config - Read the embedded configuration of the panel
244 * @ts: our goodix_ts_data pointer
246 * Must be called during probe
248 static void goodix_read_config(struct goodix_ts_data *ts)
250 u8 config[GOODIX_CONFIG_MAX_LENGTH];
253 error = goodix_i2c_read(ts->client, GOODIX_REG_CONFIG_DATA,
255 GOODIX_CONFIG_MAX_LENGTH);
257 dev_warn(&ts->client->dev,
258 "Error reading config (%d), using defaults\n",
260 ts->abs_x_max = GOODIX_MAX_WIDTH;
261 ts->abs_y_max = GOODIX_MAX_HEIGHT;
262 ts->int_trigger_type = GOODIX_INT_TRIGGER;
263 ts->max_touch_num = GOODIX_MAX_CONTACTS;
267 ts->abs_x_max = get_unaligned_le16(&config[RESOLUTION_LOC]);
268 ts->abs_y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]);
269 ts->int_trigger_type = config[TRIGGER_LOC] & 0x03;
270 ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f;
271 if (!ts->abs_x_max || !ts->abs_y_max || !ts->max_touch_num) {
272 dev_err(&ts->client->dev,
273 "Invalid config, using defaults\n");
274 ts->abs_x_max = GOODIX_MAX_WIDTH;
275 ts->abs_y_max = GOODIX_MAX_HEIGHT;
276 ts->max_touch_num = GOODIX_MAX_CONTACTS;
279 ts->rotated_screen = dmi_check_system(rotated_screen);
280 if (ts->rotated_screen)
281 dev_dbg(&ts->client->dev,
282 "Applying '180 degrees rotated screen' quirk\n");
286 * goodix_read_version - Read goodix touchscreen version
288 * @client: the i2c client
289 * @version: output buffer containing the version on success
290 * @id: output buffer containing the id on success
292 static int goodix_read_version(struct i2c_client *client, u16 *version, u16 *id)
298 error = goodix_i2c_read(client, GOODIX_REG_ID, buf, sizeof(buf));
300 dev_err(&client->dev, "read version failed: %d\n", error);
304 memcpy(id_str, buf, 4);
306 if (kstrtou16(id_str, 10, id))
309 *version = get_unaligned_le16(&buf[4]);
311 dev_info(&client->dev, "ID %d, version: %04x\n", *id, *version);
317 * goodix_i2c_test - I2C test function to check if the device answers.
319 * @client: the i2c client
321 static int goodix_i2c_test(struct i2c_client *client)
327 while (retry++ < 2) {
328 error = goodix_i2c_read(client, GOODIX_REG_CONFIG_DATA,
333 dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
342 * goodix_request_input_dev - Allocate, populate and register the input device
344 * @ts: our goodix_ts_data pointer
345 * @version: device firmware version
348 * Must be called during probe
350 static int goodix_request_input_dev(struct goodix_ts_data *ts, u16 version,
355 ts->input_dev = devm_input_allocate_device(&ts->client->dev);
356 if (!ts->input_dev) {
357 dev_err(&ts->client->dev, "Failed to allocate input device.");
361 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X,
362 0, ts->abs_x_max, 0, 0);
363 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
364 0, ts->abs_y_max, 0, 0);
365 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
366 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
368 input_mt_init_slots(ts->input_dev, ts->max_touch_num,
369 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
371 ts->input_dev->name = "Goodix Capacitive TouchScreen";
372 ts->input_dev->phys = "input/ts";
373 ts->input_dev->id.bustype = BUS_I2C;
374 ts->input_dev->id.vendor = 0x0416;
375 ts->input_dev->id.product = id;
376 ts->input_dev->id.version = version;
378 error = input_register_device(ts->input_dev);
380 dev_err(&ts->client->dev,
381 "Failed to register input device: %d", error);
388 static int goodix_ts_probe(struct i2c_client *client,
389 const struct i2c_device_id *id)
391 struct goodix_ts_data *ts;
392 unsigned long irq_flags;
394 u16 version_info, id_info;
396 dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
398 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
399 dev_err(&client->dev, "I2C check functionality failed.\n");
403 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
408 i2c_set_clientdata(client, ts);
410 error = goodix_i2c_test(client);
412 dev_err(&client->dev, "I2C communication failure: %d\n", error);
416 error = goodix_read_version(client, &version_info, &id_info);
418 dev_err(&client->dev, "Read version failed.\n");
422 goodix_read_config(ts);
424 error = goodix_request_input_dev(ts, version_info, id_info);
428 irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
429 error = devm_request_threaded_irq(&ts->client->dev, client->irq,
430 NULL, goodix_ts_irq_handler,
431 irq_flags, client->name, ts);
433 dev_err(&client->dev, "request IRQ failed: %d\n", error);
440 static const struct i2c_device_id goodix_ts_id[] = {
441 { "GDIX1001:00", 0 },
444 MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
447 static const struct acpi_device_id goodix_acpi_match[] = {
452 MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
456 static const struct of_device_id goodix_of_match[] = {
457 { .compatible = "goodix,gt911" },
458 { .compatible = "goodix,gt9110" },
459 { .compatible = "goodix,gt912" },
460 { .compatible = "goodix,gt927" },
461 { .compatible = "goodix,gt9271" },
462 { .compatible = "goodix,gt928" },
463 { .compatible = "goodix,gt967" },
466 MODULE_DEVICE_TABLE(of, goodix_of_match);
469 static struct i2c_driver goodix_ts_driver = {
470 .probe = goodix_ts_probe,
471 .id_table = goodix_ts_id,
474 .acpi_match_table = ACPI_PTR(goodix_acpi_match),
475 .of_match_table = of_match_ptr(goodix_of_match),
478 module_i2c_driver(goodix_ts_driver);
480 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
481 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
482 MODULE_DESCRIPTION("Goodix touchscreen driver");
483 MODULE_LICENSE("GPL v2");