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/i2c.h>
19 #include <linux/input.h>
20 #include <linux/input/mt.h>
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/irq.h>
24 #include <linux/interrupt.h>
25 #include <linux/slab.h>
26 #include <linux/acpi.h>
28 #include <asm/unaligned.h>
30 struct goodix_ts_data {
31 struct i2c_client *client;
32 struct input_dev *input_dev;
35 unsigned int max_touch_num;
36 unsigned int int_trigger_type;
39 #define GOODIX_MAX_HEIGHT 4096
40 #define GOODIX_MAX_WIDTH 4096
41 #define GOODIX_INT_TRIGGER 1
42 #define GOODIX_CONTACT_SIZE 8
43 #define GOODIX_MAX_CONTACTS 10
45 #define GOODIX_CONFIG_MAX_LENGTH 240
47 /* Register defines */
48 #define GOODIX_READ_COOR_ADDR 0x814E
49 #define GOODIX_REG_CONFIG_DATA 0x8047
50 #define GOODIX_REG_VERSION 0x8140
52 #define RESOLUTION_LOC 1
53 #define MAX_CONTACTS_LOC 5
56 static const unsigned long goodix_irq_flags[] = {
58 IRQ_TYPE_EDGE_FALLING,
64 * goodix_i2c_read - read data from a register of the i2c slave device.
66 * @client: i2c device.
67 * @reg: the register to read from.
68 * @buf: raw write data buffer.
69 * @len: length of the buffer to write
71 static int goodix_i2c_read(struct i2c_client *client,
72 u16 reg, u8 *buf, int len)
74 struct i2c_msg msgs[2];
75 u16 wbuf = cpu_to_be16(reg);
79 msgs[0].addr = client->addr;
81 msgs[0].buf = (u8 *)&wbuf;
83 msgs[1].flags = I2C_M_RD;
84 msgs[1].addr = client->addr;
88 ret = i2c_transfer(client->adapter, msgs, 2);
89 return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
92 static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
97 error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR, data,
98 GOODIX_CONTACT_SIZE + 1);
100 dev_err(&ts->client->dev, "I2C transfer error: %d\n", error);
104 if (!(data[0] & 0x80))
107 touch_num = data[0] & 0x0f;
108 if (touch_num > ts->max_touch_num)
112 data += 1 + GOODIX_CONTACT_SIZE;
113 error = goodix_i2c_read(ts->client,
114 GOODIX_READ_COOR_ADDR +
115 1 + GOODIX_CONTACT_SIZE,
117 GOODIX_CONTACT_SIZE * (touch_num - 1));
125 static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data)
127 int id = coor_data[0] & 0x0F;
128 int input_x = get_unaligned_le16(&coor_data[1]);
129 int input_y = get_unaligned_le16(&coor_data[3]);
130 int input_w = get_unaligned_le16(&coor_data[5]);
132 input_mt_slot(ts->input_dev, id);
133 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
134 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, input_x);
135 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, input_y);
136 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
137 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
141 * goodix_process_events - Process incoming events
143 * @ts: our goodix_ts_data pointer
145 * Called when the IRQ is triggered. Read the current device state, and push
146 * the input events to the user space.
148 static void goodix_process_events(struct goodix_ts_data *ts)
150 u8 point_data[1 + GOODIX_CONTACT_SIZE * ts->max_touch_num];
154 touch_num = goodix_ts_read_input_report(ts, point_data);
158 for (i = 0; i < touch_num; i++)
159 goodix_ts_report_touch(ts,
160 &point_data[1 + GOODIX_CONTACT_SIZE * i]);
162 input_mt_sync_frame(ts->input_dev);
163 input_sync(ts->input_dev);
167 * goodix_ts_irq_handler - The IRQ handler
169 * @irq: interrupt number.
170 * @dev_id: private data pointer.
172 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
174 static const u8 end_cmd[] = {
175 GOODIX_READ_COOR_ADDR >> 8,
176 GOODIX_READ_COOR_ADDR & 0xff,
179 struct goodix_ts_data *ts = dev_id;
181 goodix_process_events(ts);
183 if (i2c_master_send(ts->client, end_cmd, sizeof(end_cmd)) < 0)
184 dev_err(&ts->client->dev, "I2C write end_cmd error\n");
190 * goodix_read_config - Read the embedded configuration of the panel
192 * @ts: our goodix_ts_data pointer
194 * Must be called during probe
196 static void goodix_read_config(struct goodix_ts_data *ts)
198 u8 config[GOODIX_CONFIG_MAX_LENGTH];
201 error = goodix_i2c_read(ts->client, GOODIX_REG_CONFIG_DATA,
203 GOODIX_CONFIG_MAX_LENGTH);
205 dev_warn(&ts->client->dev,
206 "Error reading config (%d), using defaults\n",
208 ts->abs_x_max = GOODIX_MAX_WIDTH;
209 ts->abs_y_max = GOODIX_MAX_HEIGHT;
210 ts->int_trigger_type = GOODIX_INT_TRIGGER;
211 ts->max_touch_num = GOODIX_MAX_CONTACTS;
215 ts->abs_x_max = get_unaligned_le16(&config[RESOLUTION_LOC]);
216 ts->abs_y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]);
217 ts->int_trigger_type = config[TRIGGER_LOC] & 0x03;
218 ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f;
219 if (!ts->abs_x_max || !ts->abs_y_max || !ts->max_touch_num) {
220 dev_err(&ts->client->dev,
221 "Invalid config, using defaults\n");
222 ts->abs_x_max = GOODIX_MAX_WIDTH;
223 ts->abs_y_max = GOODIX_MAX_HEIGHT;
224 ts->max_touch_num = GOODIX_MAX_CONTACTS;
229 * goodix_read_version - Read goodix touchscreen version
231 * @client: the i2c client
232 * @version: output buffer containing the version on success
234 static int goodix_read_version(struct i2c_client *client, u16 *version)
239 error = goodix_i2c_read(client, GOODIX_REG_VERSION, buf, sizeof(buf));
241 dev_err(&client->dev, "read version failed: %d\n", error);
246 *version = get_unaligned_le16(&buf[4]);
248 dev_info(&client->dev, "IC VERSION: %6ph\n", buf);
254 * goodix_i2c_test - I2C test function to check if the device answers.
256 * @client: the i2c client
258 static int goodix_i2c_test(struct i2c_client *client)
264 while (retry++ < 2) {
265 error = goodix_i2c_read(client, GOODIX_REG_CONFIG_DATA,
270 dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
279 * goodix_request_input_dev - Allocate, populate and register the input device
281 * @ts: our goodix_ts_data pointer
283 * Must be called during probe
285 static int goodix_request_input_dev(struct goodix_ts_data *ts)
289 ts->input_dev = devm_input_allocate_device(&ts->client->dev);
290 if (!ts->input_dev) {
291 dev_err(&ts->client->dev, "Failed to allocate input device.");
295 ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) |
299 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X,
300 0, ts->abs_x_max, 0, 0);
301 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
302 0, ts->abs_y_max, 0, 0);
303 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
304 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
306 input_mt_init_slots(ts->input_dev, ts->max_touch_num,
307 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
309 ts->input_dev->name = "Goodix Capacitive TouchScreen";
310 ts->input_dev->phys = "input/ts";
311 ts->input_dev->id.bustype = BUS_I2C;
312 ts->input_dev->id.vendor = 0x0416;
313 ts->input_dev->id.product = 0x1001;
314 ts->input_dev->id.version = 10427;
316 error = input_register_device(ts->input_dev);
318 dev_err(&ts->client->dev,
319 "Failed to register input device: %d", error);
326 static int goodix_ts_probe(struct i2c_client *client,
327 const struct i2c_device_id *id)
329 struct goodix_ts_data *ts;
330 unsigned long irq_flags;
334 dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
336 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
337 dev_err(&client->dev, "I2C check functionality failed.\n");
341 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
346 i2c_set_clientdata(client, ts);
348 error = goodix_i2c_test(client);
350 dev_err(&client->dev, "I2C communication failure: %d\n", error);
354 error = goodix_read_version(client, &version_info);
356 dev_err(&client->dev, "Read version failed.\n");
360 goodix_read_config(ts);
362 error = goodix_request_input_dev(ts);
366 irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
367 error = devm_request_threaded_irq(&ts->client->dev, client->irq,
368 NULL, goodix_ts_irq_handler,
369 irq_flags, client->name, ts);
371 dev_err(&client->dev, "request IRQ failed: %d\n", error);
378 static const struct i2c_device_id goodix_ts_id[] = {
379 { "GDIX1001:00", 0 },
384 static const struct acpi_device_id goodix_acpi_match[] = {
388 MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
392 static const struct of_device_id goodix_of_match[] = {
393 { .compatible = "goodix,gt911" },
394 { .compatible = "goodix,gt9110" },
395 { .compatible = "goodix,gt912" },
396 { .compatible = "goodix,gt927" },
397 { .compatible = "goodix,gt9271" },
398 { .compatible = "goodix,gt928" },
399 { .compatible = "goodix,gt967" },
402 MODULE_DEVICE_TABLE(of, goodix_of_match);
405 static struct i2c_driver goodix_ts_driver = {
406 .probe = goodix_ts_probe,
407 .id_table = goodix_ts_id,
410 .owner = THIS_MODULE,
411 .acpi_match_table = ACPI_PTR(goodix_acpi_match),
412 .of_match_table = of_match_ptr(goodix_of_match),
415 module_i2c_driver(goodix_ts_driver);
417 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
418 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
419 MODULE_DESCRIPTION("Goodix touchscreen driver");
420 MODULE_LICENSE("GPL v2");