GNU Linux-libre 4.4.284-gnu1
[releases.git] / drivers / input / touchscreen / goodix.c
1 /*
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/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>
28 #include <linux/of.h>
29 #include <asm/unaligned.h>
30
31 struct goodix_ts_data {
32         struct i2c_client *client;
33         struct input_dev *input_dev;
34         int abs_x_max;
35         int abs_y_max;
36         unsigned int max_touch_num;
37         unsigned int int_trigger_type;
38         bool rotated_screen;
39 };
40
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
46
47 #define GOODIX_CONFIG_MAX_LENGTH        240
48
49 /* Register defines */
50 #define GOODIX_READ_COOR_ADDR           0x814E
51 #define GOODIX_REG_CONFIG_DATA          0x8047
52 #define GOODIX_REG_ID                   0x8140
53
54 #define RESOLUTION_LOC          1
55 #define MAX_CONTACTS_LOC        5
56 #define TRIGGER_LOC             6
57
58 static const unsigned long goodix_irq_flags[] = {
59         IRQ_TYPE_EDGE_RISING,
60         IRQ_TYPE_EDGE_FALLING,
61         IRQ_TYPE_LEVEL_LOW,
62         IRQ_TYPE_LEVEL_HIGH,
63 };
64
65 /*
66  * Those tablets have their coordinates origin at the bottom right
67  * of the tablet, as if rotated 180 degrees
68  */
69 static const struct dmi_system_id rotated_screen[] = {
70 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
71         {
72                 .ident = "Teclast X89",
73                 .matches = {
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"),
78                 },
79         },
80         {
81                 .ident = "Teclast X98 Pro",
82                 .matches = {
83                         /*
84                          * Only match BIOS date, because the manufacturers
85                          * BIOS does not report the board name at all
86                          * (sometimes)...
87                          */
88                         DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
89                         DMI_MATCH(DMI_BIOS_DATE, "10/28/2015"),
90                 },
91         },
92         {
93                 .ident = "WinBook TW100",
94                 .matches = {
95                         DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
96                         DMI_MATCH(DMI_PRODUCT_NAME, "TW100")
97                 }
98         },
99         {
100                 .ident = "WinBook TW700",
101                 .matches = {
102                         DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
103                         DMI_MATCH(DMI_PRODUCT_NAME, "TW700")
104                 },
105         },
106 #endif
107         {}
108 };
109
110 /**
111  * goodix_i2c_read - read data from a register of the i2c slave device.
112  *
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
117  */
118 static int goodix_i2c_read(struct i2c_client *client,
119                            u16 reg, u8 *buf, int len)
120 {
121         struct i2c_msg msgs[2];
122         u16 wbuf = cpu_to_be16(reg);
123         int ret;
124
125         msgs[0].flags = 0;
126         msgs[0].addr  = client->addr;
127         msgs[0].len   = 2;
128         msgs[0].buf   = (u8 *)&wbuf;
129
130         msgs[1].flags = I2C_M_RD;
131         msgs[1].addr  = client->addr;
132         msgs[1].len   = len;
133         msgs[1].buf   = buf;
134
135         ret = i2c_transfer(client->adapter, msgs, 2);
136         return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
137 }
138
139 static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
140 {
141         int touch_num;
142         int error;
143
144         error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR, data,
145                                 GOODIX_CONTACT_SIZE + 1);
146         if (error) {
147                 dev_err(&ts->client->dev, "I2C transfer error: %d\n", error);
148                 return error;
149         }
150
151         if (!(data[0] & 0x80))
152                 return -EAGAIN;
153
154         touch_num = data[0] & 0x0f;
155         if (touch_num > ts->max_touch_num)
156                 return -EPROTO;
157
158         if (touch_num > 1) {
159                 data += 1 + GOODIX_CONTACT_SIZE;
160                 error = goodix_i2c_read(ts->client,
161                                         GOODIX_READ_COOR_ADDR +
162                                                 1 + GOODIX_CONTACT_SIZE,
163                                         data,
164                                         GOODIX_CONTACT_SIZE * (touch_num - 1));
165                 if (error)
166                         return error;
167         }
168
169         return touch_num;
170 }
171
172 static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data)
173 {
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]);
178
179         if (ts->rotated_screen) {
180                 input_x = ts->abs_x_max - input_x;
181                 input_y = ts->abs_y_max - input_y;
182         }
183
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);
190 }
191
192 /**
193  * goodix_process_events - Process incoming events
194  *
195  * @ts: our goodix_ts_data pointer
196  *
197  * Called when the IRQ is triggered. Read the current device state, and push
198  * the input events to the user space.
199  */
200 static void goodix_process_events(struct goodix_ts_data *ts)
201 {
202         u8  point_data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
203         int touch_num;
204         int i;
205
206         touch_num = goodix_ts_read_input_report(ts, point_data);
207         if (touch_num < 0)
208                 return;
209
210         for (i = 0; i < touch_num; i++)
211                 goodix_ts_report_touch(ts,
212                                 &point_data[1 + GOODIX_CONTACT_SIZE * i]);
213
214         input_mt_sync_frame(ts->input_dev);
215         input_sync(ts->input_dev);
216 }
217
218 /**
219  * goodix_ts_irq_handler - The IRQ handler
220  *
221  * @irq: interrupt number.
222  * @dev_id: private data pointer.
223  */
224 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
225 {
226         static const u8 end_cmd[] = {
227                 GOODIX_READ_COOR_ADDR >> 8,
228                 GOODIX_READ_COOR_ADDR & 0xff,
229                 0
230         };
231         struct goodix_ts_data *ts = dev_id;
232
233         goodix_process_events(ts);
234
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");
237
238         return IRQ_HANDLED;
239 }
240
241 /**
242  * goodix_read_config - Read the embedded configuration of the panel
243  *
244  * @ts: our goodix_ts_data pointer
245  *
246  * Must be called during probe
247  */
248 static void goodix_read_config(struct goodix_ts_data *ts)
249 {
250         u8 config[GOODIX_CONFIG_MAX_LENGTH];
251         int error;
252
253         error = goodix_i2c_read(ts->client, GOODIX_REG_CONFIG_DATA,
254                                 config,
255                                 GOODIX_CONFIG_MAX_LENGTH);
256         if (error) {
257                 dev_warn(&ts->client->dev,
258                          "Error reading config (%d), using defaults\n",
259                          error);
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;
264                 return;
265         }
266
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;
277         }
278
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");
283 }
284
285 /**
286  * goodix_read_version - Read goodix touchscreen version
287  *
288  * @client: the i2c client
289  * @version: output buffer containing the version on success
290  * @id: output buffer containing the id on success
291  */
292 static int goodix_read_version(struct i2c_client *client, u16 *version, u16 *id)
293 {
294         int error;
295         u8 buf[6];
296         char id_str[5];
297
298         error = goodix_i2c_read(client, GOODIX_REG_ID, buf, sizeof(buf));
299         if (error) {
300                 dev_err(&client->dev, "read version failed: %d\n", error);
301                 return error;
302         }
303
304         memcpy(id_str, buf, 4);
305         id_str[4] = 0;
306         if (kstrtou16(id_str, 10, id))
307                 *id = 0x1001;
308
309         *version = get_unaligned_le16(&buf[4]);
310
311         dev_info(&client->dev, "ID %d, version: %04x\n", *id, *version);
312
313         return 0;
314 }
315
316 /**
317  * goodix_i2c_test - I2C test function to check if the device answers.
318  *
319  * @client: the i2c client
320  */
321 static int goodix_i2c_test(struct i2c_client *client)
322 {
323         int retry = 0;
324         int error;
325         u8 test;
326
327         while (retry++ < 2) {
328                 error = goodix_i2c_read(client, GOODIX_REG_CONFIG_DATA,
329                                         &test, 1);
330                 if (!error)
331                         return 0;
332
333                 dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
334                         retry, error);
335                 msleep(20);
336         }
337
338         return error;
339 }
340
341 /**
342  * goodix_request_input_dev - Allocate, populate and register the input device
343  *
344  * @ts: our goodix_ts_data pointer
345  * @version: device firmware version
346  * @id: device ID
347  *
348  * Must be called during probe
349  */
350 static int goodix_request_input_dev(struct goodix_ts_data *ts, u16 version,
351                                     u16 id)
352 {
353         int error;
354
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.");
358                 return -ENOMEM;
359         }
360
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);
367
368         input_mt_init_slots(ts->input_dev, ts->max_touch_num,
369                             INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
370
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;
377
378         error = input_register_device(ts->input_dev);
379         if (error) {
380                 dev_err(&ts->client->dev,
381                         "Failed to register input device: %d", error);
382                 return error;
383         }
384
385         return 0;
386 }
387
388 static int goodix_ts_probe(struct i2c_client *client,
389                            const struct i2c_device_id *id)
390 {
391         struct goodix_ts_data *ts;
392         unsigned long irq_flags;
393         int error;
394         u16 version_info, id_info;
395
396         dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
397
398         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
399                 dev_err(&client->dev, "I2C check functionality failed.\n");
400                 return -ENXIO;
401         }
402
403         ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
404         if (!ts)
405                 return -ENOMEM;
406
407         ts->client = client;
408         i2c_set_clientdata(client, ts);
409
410         error = goodix_i2c_test(client);
411         if (error) {
412                 dev_err(&client->dev, "I2C communication failure: %d\n", error);
413                 return error;
414         }
415
416         error = goodix_read_version(client, &version_info, &id_info);
417         if (error) {
418                 dev_err(&client->dev, "Read version failed.\n");
419                 return error;
420         }
421
422         goodix_read_config(ts);
423
424         error = goodix_request_input_dev(ts, version_info, id_info);
425         if (error)
426                 return error;
427
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);
432         if (error) {
433                 dev_err(&client->dev, "request IRQ failed: %d\n", error);
434                 return error;
435         }
436
437         return 0;
438 }
439
440 static const struct i2c_device_id goodix_ts_id[] = {
441         { "GDIX1001:00", 0 },
442         { }
443 };
444 MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
445
446 #ifdef CONFIG_ACPI
447 static const struct acpi_device_id goodix_acpi_match[] = {
448         { "GDIX1001", 0 },
449         { "GDIX1002", 0 },
450         { }
451 };
452 MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
453 #endif
454
455 #ifdef CONFIG_OF
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" },
464         { }
465 };
466 MODULE_DEVICE_TABLE(of, goodix_of_match);
467 #endif
468
469 static struct i2c_driver goodix_ts_driver = {
470         .probe = goodix_ts_probe,
471         .id_table = goodix_ts_id,
472         .driver = {
473                 .name = "Goodix-TS",
474                 .acpi_match_table = ACPI_PTR(goodix_acpi_match),
475                 .of_match_table = of_match_ptr(goodix_of_match),
476         },
477 };
478 module_i2c_driver(goodix_ts_driver);
479
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");