blob: d3f44a8577f5f627c05dcb1546d7f5582a915de1 [file] [log] [blame]
Bastien Noceraca96ea82014-10-31 09:26:16 -07001/*
2 * Driver for Goodix Touchscreens
3 *
4 * Copyright (c) 2014 Red Hat Inc.
5 *
6 * This code is based on gt9xx.c authored by andrew@goodix.com:
7 *
8 * 2010 - 2012 Goodix Technology.
9 */
10
11/*
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.
15 */
16
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>
Aleksei Mamlin771d8f12015-03-06 16:43:38 -080026#include <linux/acpi.h>
27#include <linux/of.h>
Bastien Noceraca96ea82014-10-31 09:26:16 -070028#include <asm/unaligned.h>
29
30struct goodix_ts_data {
31 struct i2c_client *client;
32 struct input_dev *input_dev;
33 int abs_x_max;
34 int abs_y_max;
35 unsigned int max_touch_num;
36 unsigned int int_trigger_type;
37};
38
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
44
45#define GOODIX_CONFIG_MAX_LENGTH 240
46
47/* Register defines */
48#define GOODIX_READ_COOR_ADDR 0x814E
49#define GOODIX_REG_CONFIG_DATA 0x8047
Irina Tirdeae70b0302015-06-09 11:04:40 -070050#define GOODIX_REG_ID 0x8140
Bastien Noceraca96ea82014-10-31 09:26:16 -070051
52#define RESOLUTION_LOC 1
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -080053#define MAX_CONTACTS_LOC 5
Bastien Noceraca96ea82014-10-31 09:26:16 -070054#define TRIGGER_LOC 6
55
56static const unsigned long goodix_irq_flags[] = {
57 IRQ_TYPE_EDGE_RISING,
58 IRQ_TYPE_EDGE_FALLING,
59 IRQ_TYPE_LEVEL_LOW,
60 IRQ_TYPE_LEVEL_HIGH,
61};
62
63/**
64 * goodix_i2c_read - read data from a register of the i2c slave device.
65 *
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
70 */
71static int goodix_i2c_read(struct i2c_client *client,
Irina Tirdea0dfb35b2015-06-09 11:01:38 -070072 u16 reg, u8 *buf, int len)
Bastien Noceraca96ea82014-10-31 09:26:16 -070073{
74 struct i2c_msg msgs[2];
75 u16 wbuf = cpu_to_be16(reg);
76 int ret;
77
78 msgs[0].flags = 0;
79 msgs[0].addr = client->addr;
80 msgs[0].len = 2;
Irina Tirdea0dfb35b2015-06-09 11:01:38 -070081 msgs[0].buf = (u8 *)&wbuf;
Bastien Noceraca96ea82014-10-31 09:26:16 -070082
83 msgs[1].flags = I2C_M_RD;
84 msgs[1].addr = client->addr;
85 msgs[1].len = len;
86 msgs[1].buf = buf;
87
88 ret = i2c_transfer(client->adapter, msgs, 2);
89 return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
90}
91
92static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
93{
94 int touch_num;
95 int error;
96
97 error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR, data,
98 GOODIX_CONTACT_SIZE + 1);
99 if (error) {
100 dev_err(&ts->client->dev, "I2C transfer error: %d\n", error);
101 return error;
102 }
103
Paul Cercueil5f6f1172015-05-06 16:52:13 -0700104 if (!(data[0] & 0x80))
105 return -EAGAIN;
106
Bastien Noceraca96ea82014-10-31 09:26:16 -0700107 touch_num = data[0] & 0x0f;
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -0800108 if (touch_num > ts->max_touch_num)
Bastien Noceraca96ea82014-10-31 09:26:16 -0700109 return -EPROTO;
110
111 if (touch_num > 1) {
112 data += 1 + GOODIX_CONTACT_SIZE;
113 error = goodix_i2c_read(ts->client,
114 GOODIX_READ_COOR_ADDR +
115 1 + GOODIX_CONTACT_SIZE,
116 data,
117 GOODIX_CONTACT_SIZE * (touch_num - 1));
118 if (error)
119 return error;
120 }
121
122 return touch_num;
123}
124
125static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data)
126{
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]);
131
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);
138}
139
140/**
141 * goodix_process_events - Process incoming events
142 *
143 * @ts: our goodix_ts_data pointer
144 *
145 * Called when the IRQ is triggered. Read the current device state, and push
146 * the input events to the user space.
147 */
148static void goodix_process_events(struct goodix_ts_data *ts)
149{
Irina Tirdea0e0432f2015-06-09 11:03:15 -0700150 u8 point_data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
Bastien Noceraca96ea82014-10-31 09:26:16 -0700151 int touch_num;
152 int i;
153
154 touch_num = goodix_ts_read_input_report(ts, point_data);
155 if (touch_num < 0)
156 return;
157
158 for (i = 0; i < touch_num; i++)
159 goodix_ts_report_touch(ts,
160 &point_data[1 + GOODIX_CONTACT_SIZE * i]);
161
162 input_mt_sync_frame(ts->input_dev);
163 input_sync(ts->input_dev);
164}
165
166/**
167 * goodix_ts_irq_handler - The IRQ handler
168 *
169 * @irq: interrupt number.
170 * @dev_id: private data pointer.
171 */
172static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
173{
174 static const u8 end_cmd[] = {
175 GOODIX_READ_COOR_ADDR >> 8,
176 GOODIX_READ_COOR_ADDR & 0xff,
177 0
178 };
179 struct goodix_ts_data *ts = dev_id;
180
181 goodix_process_events(ts);
182
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");
185
186 return IRQ_HANDLED;
187}
188
189/**
190 * goodix_read_config - Read the embedded configuration of the panel
191 *
192 * @ts: our goodix_ts_data pointer
193 *
194 * Must be called during probe
195 */
196static void goodix_read_config(struct goodix_ts_data *ts)
197{
198 u8 config[GOODIX_CONFIG_MAX_LENGTH];
199 int error;
200
201 error = goodix_i2c_read(ts->client, GOODIX_REG_CONFIG_DATA,
Irina Tirdea0dfb35b2015-06-09 11:01:38 -0700202 config,
203 GOODIX_CONFIG_MAX_LENGTH);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700204 if (error) {
205 dev_warn(&ts->client->dev,
206 "Error reading config (%d), using defaults\n",
207 error);
208 ts->abs_x_max = GOODIX_MAX_WIDTH;
209 ts->abs_y_max = GOODIX_MAX_HEIGHT;
210 ts->int_trigger_type = GOODIX_INT_TRIGGER;
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -0800211 ts->max_touch_num = GOODIX_MAX_CONTACTS;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700212 return;
213 }
214
215 ts->abs_x_max = get_unaligned_le16(&config[RESOLUTION_LOC]);
216 ts->abs_y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]);
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -0800217 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) {
Bastien Noceraca96ea82014-10-31 09:26:16 -0700220 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;
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -0800224 ts->max_touch_num = GOODIX_MAX_CONTACTS;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700225 }
226}
227
Bastien Noceraca96ea82014-10-31 09:26:16 -0700228/**
229 * goodix_read_version - Read goodix touchscreen version
230 *
231 * @client: the i2c client
232 * @version: output buffer containing the version on success
Irina Tirdeae70b0302015-06-09 11:04:40 -0700233 * @id: output buffer containing the id on success
Bastien Noceraca96ea82014-10-31 09:26:16 -0700234 */
Irina Tirdeae70b0302015-06-09 11:04:40 -0700235static int goodix_read_version(struct i2c_client *client, u16 *version, u16 *id)
Bastien Noceraca96ea82014-10-31 09:26:16 -0700236{
237 int error;
238 u8 buf[6];
Irina Tirdeae70b0302015-06-09 11:04:40 -0700239 char id_str[5];
Bastien Noceraca96ea82014-10-31 09:26:16 -0700240
Irina Tirdeae70b0302015-06-09 11:04:40 -0700241 error = goodix_i2c_read(client, GOODIX_REG_ID, buf, sizeof(buf));
Bastien Noceraca96ea82014-10-31 09:26:16 -0700242 if (error) {
243 dev_err(&client->dev, "read version failed: %d\n", error);
244 return error;
245 }
246
Irina Tirdeae70b0302015-06-09 11:04:40 -0700247 memcpy(id_str, buf, 4);
248 id_str[4] = 0;
249 if (kstrtou16(id_str, 10, id))
250 *id = 0x1001;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700251
Irina Tirdeae70b0302015-06-09 11:04:40 -0700252 *version = get_unaligned_le16(&buf[4]);
253
254 dev_info(&client->dev, "ID %d, version: %04x\n", *id, *version);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700255
256 return 0;
257}
258
259/**
260 * goodix_i2c_test - I2C test function to check if the device answers.
261 *
262 * @client: the i2c client
263 */
264static int goodix_i2c_test(struct i2c_client *client)
265{
266 int retry = 0;
267 int error;
268 u8 test;
269
270 while (retry++ < 2) {
271 error = goodix_i2c_read(client, GOODIX_REG_CONFIG_DATA,
272 &test, 1);
273 if (!error)
274 return 0;
275
276 dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
277 retry, error);
278 msleep(20);
279 }
280
281 return error;
282}
283
284/**
285 * goodix_request_input_dev - Allocate, populate and register the input device
286 *
287 * @ts: our goodix_ts_data pointer
Irina Tirdeae70b0302015-06-09 11:04:40 -0700288 * @version: device firmware version
289 * @id: device ID
Bastien Noceraca96ea82014-10-31 09:26:16 -0700290 *
291 * Must be called during probe
292 */
Irina Tirdeae70b0302015-06-09 11:04:40 -0700293static int goodix_request_input_dev(struct goodix_ts_data *ts, u16 version,
294 u16 id)
Bastien Noceraca96ea82014-10-31 09:26:16 -0700295{
296 int error;
297
298 ts->input_dev = devm_input_allocate_device(&ts->client->dev);
299 if (!ts->input_dev) {
300 dev_err(&ts->client->dev, "Failed to allocate input device.");
301 return -ENOMEM;
302 }
303
Irina Tirdea0dfb35b2015-06-09 11:01:38 -0700304 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X,
305 0, ts->abs_x_max, 0, 0);
306 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
307 0, ts->abs_y_max, 0, 0);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700308 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
309 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
310
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -0800311 input_mt_init_slots(ts->input_dev, ts->max_touch_num,
Bastien Noceraca96ea82014-10-31 09:26:16 -0700312 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
313
314 ts->input_dev->name = "Goodix Capacitive TouchScreen";
315 ts->input_dev->phys = "input/ts";
316 ts->input_dev->id.bustype = BUS_I2C;
317 ts->input_dev->id.vendor = 0x0416;
Irina Tirdeae70b0302015-06-09 11:04:40 -0700318 ts->input_dev->id.product = id;
319 ts->input_dev->id.version = version;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700320
321 error = input_register_device(ts->input_dev);
322 if (error) {
323 dev_err(&ts->client->dev,
324 "Failed to register input device: %d", error);
325 return error;
326 }
327
328 return 0;
329}
330
331static int goodix_ts_probe(struct i2c_client *client,
332 const struct i2c_device_id *id)
333{
334 struct goodix_ts_data *ts;
335 unsigned long irq_flags;
336 int error;
Irina Tirdeae70b0302015-06-09 11:04:40 -0700337 u16 version_info, id_info;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700338
339 dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
340
341 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
342 dev_err(&client->dev, "I2C check functionality failed.\n");
343 return -ENXIO;
344 }
345
346 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
347 if (!ts)
348 return -ENOMEM;
349
350 ts->client = client;
351 i2c_set_clientdata(client, ts);
352
353 error = goodix_i2c_test(client);
354 if (error) {
355 dev_err(&client->dev, "I2C communication failure: %d\n", error);
356 return error;
357 }
358
Irina Tirdeae70b0302015-06-09 11:04:40 -0700359 error = goodix_read_version(client, &version_info, &id_info);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700360 if (error) {
361 dev_err(&client->dev, "Read version failed.\n");
362 return error;
363 }
364
365 goodix_read_config(ts);
366
Irina Tirdeae70b0302015-06-09 11:04:40 -0700367 error = goodix_request_input_dev(ts, version_info, id_info);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700368 if (error)
369 return error;
370
371 irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
372 error = devm_request_threaded_irq(&ts->client->dev, client->irq,
373 NULL, goodix_ts_irq_handler,
374 irq_flags, client->name, ts);
375 if (error) {
376 dev_err(&client->dev, "request IRQ failed: %d\n", error);
377 return error;
378 }
379
380 return 0;
381}
382
383static const struct i2c_device_id goodix_ts_id[] = {
384 { "GDIX1001:00", 0 },
385 { }
386};
Javier Martinez Canillas2e9e9102015-07-30 10:38:52 -0700387MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700388
Aleksei Mamlin771d8f12015-03-06 16:43:38 -0800389#ifdef CONFIG_ACPI
Bastien Noceraca96ea82014-10-31 09:26:16 -0700390static const struct acpi_device_id goodix_acpi_match[] = {
391 { "GDIX1001", 0 },
392 { }
393};
394MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
Aleksei Mamlin771d8f12015-03-06 16:43:38 -0800395#endif
396
397#ifdef CONFIG_OF
398static const struct of_device_id goodix_of_match[] = {
399 { .compatible = "goodix,gt911" },
400 { .compatible = "goodix,gt9110" },
401 { .compatible = "goodix,gt912" },
402 { .compatible = "goodix,gt927" },
403 { .compatible = "goodix,gt9271" },
404 { .compatible = "goodix,gt928" },
405 { .compatible = "goodix,gt967" },
406 { }
407};
408MODULE_DEVICE_TABLE(of, goodix_of_match);
409#endif
Bastien Noceraca96ea82014-10-31 09:26:16 -0700410
411static struct i2c_driver goodix_ts_driver = {
412 .probe = goodix_ts_probe,
413 .id_table = goodix_ts_id,
414 .driver = {
415 .name = "Goodix-TS",
Aleksei Mamlin771d8f12015-03-06 16:43:38 -0800416 .acpi_match_table = ACPI_PTR(goodix_acpi_match),
417 .of_match_table = of_match_ptr(goodix_of_match),
Bastien Noceraca96ea82014-10-31 09:26:16 -0700418 },
419};
420module_i2c_driver(goodix_ts_driver);
421
422MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
423MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
424MODULE_DESCRIPTION("Goodix touchscreen driver");
425MODULE_LICENSE("GPL v2");