blob: b5e910a44cdf0ae03ab5c5cab7ebc8fbb5fa4a23 [file] [log] [blame]
Bastien Noceraca96ea82014-10-31 09:26:16 -07001/*
2 * Driver for Goodix Touchscreens
3 *
4 * Copyright (c) 2014 Red Hat Inc.
Karsten Merkerad48cf52015-12-17 17:02:53 -08005 * Copyright (c) 2015 K. Merker <merker@debian.org>
Bastien Noceraca96ea82014-10-31 09:26:16 -07006 *
7 * This code is based on gt9xx.c authored by andrew@goodix.com:
8 *
9 * 2010 - 2012 Goodix Technology.
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; version 2 of the License.
16 */
17
18#include <linux/kernel.h>
Bastien Nocera8b5a3592015-07-24 09:08:53 -070019#include <linux/dmi.h>
Irina Tirdea68caf852015-12-17 16:05:42 -080020#include <linux/firmware.h>
Irina Tirdeaec6e1b42015-12-17 15:57:34 -080021#include <linux/gpio/consumer.h>
Bastien Noceraca96ea82014-10-31 09:26:16 -070022#include <linux/i2c.h>
23#include <linux/input.h>
24#include <linux/input/mt.h>
25#include <linux/module.h>
26#include <linux/delay.h>
27#include <linux/irq.h>
28#include <linux/interrupt.h>
29#include <linux/slab.h>
Aleksei Mamlin771d8f12015-03-06 16:43:38 -080030#include <linux/acpi.h>
31#include <linux/of.h>
Bastien Noceraca96ea82014-10-31 09:26:16 -070032#include <asm/unaligned.h>
33
34struct goodix_ts_data {
35 struct i2c_client *client;
36 struct input_dev *input_dev;
37 int abs_x_max;
38 int abs_y_max;
Karsten Merkerad48cf52015-12-17 17:02:53 -080039 bool swapped_x_y;
40 bool inverted_x;
41 bool inverted_y;
Bastien Noceraca96ea82014-10-31 09:26:16 -070042 unsigned int max_touch_num;
43 unsigned int int_trigger_type;
Bastien Nocera8b5a3592015-07-24 09:08:53 -070044 bool rotated_screen;
Irina Tirdeaa779fbc2015-12-17 15:55:21 -080045 int cfg_len;
Irina Tirdeaec6e1b42015-12-17 15:57:34 -080046 struct gpio_desc *gpiod_int;
47 struct gpio_desc *gpiod_rst;
Irina Tirdea68caf852015-12-17 16:05:42 -080048 u16 id;
49 u16 version;
50 const char *cfg_name;
51 struct completion firmware_loading_complete;
Irina Tirdea5ab09d62015-12-17 16:43:39 -080052 unsigned long irq_flags;
Bastien Noceraca96ea82014-10-31 09:26:16 -070053};
54
Irina Tirdeaec6e1b42015-12-17 15:57:34 -080055#define GOODIX_GPIO_INT_NAME "irq"
56#define GOODIX_GPIO_RST_NAME "reset"
57
Bastien Noceraca96ea82014-10-31 09:26:16 -070058#define GOODIX_MAX_HEIGHT 4096
59#define GOODIX_MAX_WIDTH 4096
60#define GOODIX_INT_TRIGGER 1
61#define GOODIX_CONTACT_SIZE 8
62#define GOODIX_MAX_CONTACTS 10
63
64#define GOODIX_CONFIG_MAX_LENGTH 240
Irina Tirdeaa779fbc2015-12-17 15:55:21 -080065#define GOODIX_CONFIG_911_LENGTH 186
66#define GOODIX_CONFIG_967_LENGTH 228
Bastien Noceraca96ea82014-10-31 09:26:16 -070067
68/* Register defines */
Irina Tirdea5ab09d62015-12-17 16:43:39 -080069#define GOODIX_REG_COMMAND 0x8040
70#define GOODIX_CMD_SCREEN_OFF 0x05
71
Bastien Noceraca96ea82014-10-31 09:26:16 -070072#define GOODIX_READ_COOR_ADDR 0x814E
73#define GOODIX_REG_CONFIG_DATA 0x8047
Irina Tirdeae70b0302015-06-09 11:04:40 -070074#define GOODIX_REG_ID 0x8140
Bastien Noceraca96ea82014-10-31 09:26:16 -070075
76#define RESOLUTION_LOC 1
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -080077#define MAX_CONTACTS_LOC 5
Bastien Noceraca96ea82014-10-31 09:26:16 -070078#define TRIGGER_LOC 6
79
80static const unsigned long goodix_irq_flags[] = {
81 IRQ_TYPE_EDGE_RISING,
82 IRQ_TYPE_EDGE_FALLING,
83 IRQ_TYPE_LEVEL_LOW,
84 IRQ_TYPE_LEVEL_HIGH,
85};
86
Bastien Nocera8b5a3592015-07-24 09:08:53 -070087/*
88 * Those tablets have their coordinates origin at the bottom right
89 * of the tablet, as if rotated 180 degrees
90 */
91static const struct dmi_system_id rotated_screen[] = {
92#if defined(CONFIG_DMI) && defined(CONFIG_X86)
93 {
94 .ident = "WinBook TW100",
95 .matches = {
96 DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
97 DMI_MATCH(DMI_PRODUCT_NAME, "TW100")
98 }
99 },
100 {
101 .ident = "WinBook TW700",
102 .matches = {
103 DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
104 DMI_MATCH(DMI_PRODUCT_NAME, "TW700")
105 },
106 },
107#endif
108 {}
109};
110
Bastien Noceraca96ea82014-10-31 09:26:16 -0700111/**
112 * goodix_i2c_read - read data from a register of the i2c slave device.
113 *
114 * @client: i2c device.
115 * @reg: the register to read from.
116 * @buf: raw write data buffer.
117 * @len: length of the buffer to write
118 */
119static int goodix_i2c_read(struct i2c_client *client,
Irina Tirdea0dfb35b2015-06-09 11:01:38 -0700120 u16 reg, u8 *buf, int len)
Bastien Noceraca96ea82014-10-31 09:26:16 -0700121{
122 struct i2c_msg msgs[2];
123 u16 wbuf = cpu_to_be16(reg);
124 int ret;
125
126 msgs[0].flags = 0;
127 msgs[0].addr = client->addr;
128 msgs[0].len = 2;
Irina Tirdea0dfb35b2015-06-09 11:01:38 -0700129 msgs[0].buf = (u8 *)&wbuf;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700130
131 msgs[1].flags = I2C_M_RD;
132 msgs[1].addr = client->addr;
133 msgs[1].len = len;
134 msgs[1].buf = buf;
135
136 ret = i2c_transfer(client->adapter, msgs, 2);
137 return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
138}
139
Irina Tirdea68caf852015-12-17 16:05:42 -0800140/**
141 * goodix_i2c_write - write data to a register of the i2c slave device.
142 *
143 * @client: i2c device.
144 * @reg: the register to write to.
145 * @buf: raw data buffer to write.
146 * @len: length of the buffer to write
147 */
148static int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf,
149 unsigned len)
150{
151 u8 *addr_buf;
152 struct i2c_msg msg;
153 int ret;
154
155 addr_buf = kmalloc(len + 2, GFP_KERNEL);
156 if (!addr_buf)
157 return -ENOMEM;
158
159 addr_buf[0] = reg >> 8;
160 addr_buf[1] = reg & 0xFF;
161 memcpy(&addr_buf[2], buf, len);
162
163 msg.flags = 0;
164 msg.addr = client->addr;
165 msg.buf = addr_buf;
166 msg.len = len + 2;
167
168 ret = i2c_transfer(client->adapter, &msg, 1);
169 kfree(addr_buf);
170 return ret < 0 ? ret : (ret != 1 ? -EIO : 0);
171}
172
Irina Tirdea5ab09d62015-12-17 16:43:39 -0800173static int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value)
174{
175 return goodix_i2c_write(client, reg, &value, sizeof(value));
176}
177
Irina Tirdeaa779fbc2015-12-17 15:55:21 -0800178static int goodix_get_cfg_len(u16 id)
179{
180 switch (id) {
181 case 911:
182 case 9271:
183 case 9110:
184 case 927:
185 case 928:
186 return GOODIX_CONFIG_911_LENGTH;
187
188 case 912:
189 case 967:
190 return GOODIX_CONFIG_967_LENGTH;
191
192 default:
193 return GOODIX_CONFIG_MAX_LENGTH;
194 }
195}
196
Bastien Noceraca96ea82014-10-31 09:26:16 -0700197static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
198{
199 int touch_num;
200 int error;
201
202 error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR, data,
203 GOODIX_CONTACT_SIZE + 1);
204 if (error) {
205 dev_err(&ts->client->dev, "I2C transfer error: %d\n", error);
206 return error;
207 }
208
Paul Cercueil5f6f1172015-05-06 16:52:13 -0700209 if (!(data[0] & 0x80))
210 return -EAGAIN;
211
Bastien Noceraca96ea82014-10-31 09:26:16 -0700212 touch_num = data[0] & 0x0f;
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -0800213 if (touch_num > ts->max_touch_num)
Bastien Noceraca96ea82014-10-31 09:26:16 -0700214 return -EPROTO;
215
216 if (touch_num > 1) {
217 data += 1 + GOODIX_CONTACT_SIZE;
218 error = goodix_i2c_read(ts->client,
219 GOODIX_READ_COOR_ADDR +
220 1 + GOODIX_CONTACT_SIZE,
221 data,
222 GOODIX_CONTACT_SIZE * (touch_num - 1));
223 if (error)
224 return error;
225 }
226
227 return touch_num;
228}
229
230static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data)
231{
232 int id = coor_data[0] & 0x0F;
233 int input_x = get_unaligned_le16(&coor_data[1]);
234 int input_y = get_unaligned_le16(&coor_data[3]);
235 int input_w = get_unaligned_le16(&coor_data[5]);
236
Bastien Nocera8b5a3592015-07-24 09:08:53 -0700237 if (ts->rotated_screen) {
238 input_x = ts->abs_x_max - input_x;
239 input_y = ts->abs_y_max - input_y;
240 }
241
Karsten Merkerad48cf52015-12-17 17:02:53 -0800242 /* Inversions have to happen before axis swapping */
243 if (ts->inverted_x)
244 input_x = ts->abs_x_max - input_x;
245 if (ts->inverted_y)
246 input_y = ts->abs_y_max - input_y;
247 if (ts->swapped_x_y)
248 swap(input_x, input_y);
249
Bastien Noceraca96ea82014-10-31 09:26:16 -0700250 input_mt_slot(ts->input_dev, id);
251 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
252 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, input_x);
253 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, input_y);
254 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
255 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
256}
257
258/**
259 * goodix_process_events - Process incoming events
260 *
261 * @ts: our goodix_ts_data pointer
262 *
263 * Called when the IRQ is triggered. Read the current device state, and push
264 * the input events to the user space.
265 */
266static void goodix_process_events(struct goodix_ts_data *ts)
267{
Irina Tirdea0e0432f2015-06-09 11:03:15 -0700268 u8 point_data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
Bastien Noceraca96ea82014-10-31 09:26:16 -0700269 int touch_num;
270 int i;
271
272 touch_num = goodix_ts_read_input_report(ts, point_data);
273 if (touch_num < 0)
274 return;
275
276 for (i = 0; i < touch_num; i++)
277 goodix_ts_report_touch(ts,
278 &point_data[1 + GOODIX_CONTACT_SIZE * i]);
279
280 input_mt_sync_frame(ts->input_dev);
281 input_sync(ts->input_dev);
282}
283
284/**
285 * goodix_ts_irq_handler - The IRQ handler
286 *
287 * @irq: interrupt number.
288 * @dev_id: private data pointer.
289 */
290static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
291{
Bastien Noceraca96ea82014-10-31 09:26:16 -0700292 struct goodix_ts_data *ts = dev_id;
293
294 goodix_process_events(ts);
295
Irina Tirdea5d655b32015-12-17 16:47:42 -0800296 if (goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0) < 0)
Bastien Noceraca96ea82014-10-31 09:26:16 -0700297 dev_err(&ts->client->dev, "I2C write end_cmd error\n");
298
299 return IRQ_HANDLED;
300}
301
Irina Tirdea5ab09d62015-12-17 16:43:39 -0800302static void goodix_free_irq(struct goodix_ts_data *ts)
303{
304 devm_free_irq(&ts->client->dev, ts->client->irq, ts);
305}
306
307static int goodix_request_irq(struct goodix_ts_data *ts)
308{
309 return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
310 NULL, goodix_ts_irq_handler,
311 ts->irq_flags, ts->client->name, ts);
312}
313
Irina Tirdea68caf852015-12-17 16:05:42 -0800314/**
315 * goodix_check_cfg - Checks if config fw is valid
316 *
317 * @ts: goodix_ts_data pointer
318 * @cfg: firmware config data
319 */
320static int goodix_check_cfg(struct goodix_ts_data *ts,
321 const struct firmware *cfg)
322{
323 int i, raw_cfg_len;
324 u8 check_sum = 0;
325
326 if (cfg->size > GOODIX_CONFIG_MAX_LENGTH) {
327 dev_err(&ts->client->dev,
328 "The length of the config fw is not correct");
329 return -EINVAL;
330 }
331
332 raw_cfg_len = cfg->size - 2;
333 for (i = 0; i < raw_cfg_len; i++)
334 check_sum += cfg->data[i];
335 check_sum = (~check_sum) + 1;
336 if (check_sum != cfg->data[raw_cfg_len]) {
337 dev_err(&ts->client->dev,
338 "The checksum of the config fw is not correct");
339 return -EINVAL;
340 }
341
342 if (cfg->data[raw_cfg_len + 1] != 1) {
343 dev_err(&ts->client->dev,
344 "Config fw must have Config_Fresh register set");
345 return -EINVAL;
346 }
347
348 return 0;
349}
350
351/**
352 * goodix_send_cfg - Write fw config to device
353 *
354 * @ts: goodix_ts_data pointer
355 * @cfg: config firmware to write to device
356 */
357static int goodix_send_cfg(struct goodix_ts_data *ts,
358 const struct firmware *cfg)
359{
360 int error;
361
362 error = goodix_check_cfg(ts, cfg);
363 if (error)
364 return error;
365
366 error = goodix_i2c_write(ts->client, GOODIX_REG_CONFIG_DATA, cfg->data,
367 cfg->size);
368 if (error) {
369 dev_err(&ts->client->dev, "Failed to write config data: %d",
370 error);
371 return error;
372 }
373 dev_dbg(&ts->client->dev, "Config sent successfully.");
374
375 /* Let the firmware reconfigure itself, so sleep for 10ms */
376 usleep_range(10000, 11000);
377
378 return 0;
379}
380
Irina Tirdeaec6e1b42015-12-17 15:57:34 -0800381static int goodix_int_sync(struct goodix_ts_data *ts)
382{
383 int error;
384
385 error = gpiod_direction_output(ts->gpiod_int, 0);
386 if (error)
387 return error;
388
389 msleep(50); /* T5: 50ms */
390
391 error = gpiod_direction_input(ts->gpiod_int);
392 if (error)
393 return error;
394
395 return 0;
396}
397
398/**
399 * goodix_reset - Reset device during power on
400 *
401 * @ts: goodix_ts_data pointer
402 */
403static int goodix_reset(struct goodix_ts_data *ts)
404{
405 int error;
406
407 /* begin select I2C slave addr */
408 error = gpiod_direction_output(ts->gpiod_rst, 0);
409 if (error)
410 return error;
411
412 msleep(20); /* T2: > 10ms */
413
414 /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
415 error = gpiod_direction_output(ts->gpiod_int, ts->client->addr == 0x14);
416 if (error)
417 return error;
418
419 usleep_range(100, 2000); /* T3: > 100us */
420
421 error = gpiod_direction_output(ts->gpiod_rst, 1);
422 if (error)
423 return error;
424
425 usleep_range(6000, 10000); /* T4: > 5ms */
426
427 /* end select I2C slave addr */
428 error = gpiod_direction_input(ts->gpiod_rst);
429 if (error)
430 return error;
431
432 error = goodix_int_sync(ts);
433 if (error)
434 return error;
435
436 return 0;
437}
438
439/**
440 * goodix_get_gpio_config - Get GPIO config from ACPI/DT
441 *
442 * @ts: goodix_ts_data pointer
443 */
444static int goodix_get_gpio_config(struct goodix_ts_data *ts)
445{
446 int error;
447 struct device *dev;
448 struct gpio_desc *gpiod;
449
450 if (!ts->client)
451 return -EINVAL;
452 dev = &ts->client->dev;
453
454 /* Get the interrupt GPIO pin number */
455 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
456 if (IS_ERR(gpiod)) {
457 error = PTR_ERR(gpiod);
458 if (error != -EPROBE_DEFER)
459 dev_dbg(dev, "Failed to get %s GPIO: %d\n",
460 GOODIX_GPIO_INT_NAME, error);
461 return error;
462 }
463
464 ts->gpiod_int = gpiod;
465
466 /* Get the reset line GPIO pin number */
467 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_IN);
468 if (IS_ERR(gpiod)) {
469 error = PTR_ERR(gpiod);
470 if (error != -EPROBE_DEFER)
471 dev_dbg(dev, "Failed to get %s GPIO: %d\n",
472 GOODIX_GPIO_RST_NAME, error);
473 return error;
474 }
475
476 ts->gpiod_rst = gpiod;
477
478 return 0;
479}
480
Bastien Noceraca96ea82014-10-31 09:26:16 -0700481/**
482 * goodix_read_config - Read the embedded configuration of the panel
483 *
484 * @ts: our goodix_ts_data pointer
485 *
486 * Must be called during probe
487 */
488static void goodix_read_config(struct goodix_ts_data *ts)
489{
490 u8 config[GOODIX_CONFIG_MAX_LENGTH];
491 int error;
492
493 error = goodix_i2c_read(ts->client, GOODIX_REG_CONFIG_DATA,
Irina Tirdeaa779fbc2015-12-17 15:55:21 -0800494 config, ts->cfg_len);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700495 if (error) {
496 dev_warn(&ts->client->dev,
497 "Error reading config (%d), using defaults\n",
498 error);
499 ts->abs_x_max = GOODIX_MAX_WIDTH;
500 ts->abs_y_max = GOODIX_MAX_HEIGHT;
Karsten Merkerad48cf52015-12-17 17:02:53 -0800501 if (ts->swapped_x_y)
502 swap(ts->abs_x_max, ts->abs_y_max);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700503 ts->int_trigger_type = GOODIX_INT_TRIGGER;
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -0800504 ts->max_touch_num = GOODIX_MAX_CONTACTS;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700505 return;
506 }
507
508 ts->abs_x_max = get_unaligned_le16(&config[RESOLUTION_LOC]);
509 ts->abs_y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]);
Karsten Merkerad48cf52015-12-17 17:02:53 -0800510 if (ts->swapped_x_y)
511 swap(ts->abs_x_max, ts->abs_y_max);
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -0800512 ts->int_trigger_type = config[TRIGGER_LOC] & 0x03;
513 ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f;
514 if (!ts->abs_x_max || !ts->abs_y_max || !ts->max_touch_num) {
Bastien Noceraca96ea82014-10-31 09:26:16 -0700515 dev_err(&ts->client->dev,
516 "Invalid config, using defaults\n");
517 ts->abs_x_max = GOODIX_MAX_WIDTH;
518 ts->abs_y_max = GOODIX_MAX_HEIGHT;
Karsten Merkerad48cf52015-12-17 17:02:53 -0800519 if (ts->swapped_x_y)
520 swap(ts->abs_x_max, ts->abs_y_max);
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -0800521 ts->max_touch_num = GOODIX_MAX_CONTACTS;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700522 }
Bastien Nocera8b5a3592015-07-24 09:08:53 -0700523
524 ts->rotated_screen = dmi_check_system(rotated_screen);
525 if (ts->rotated_screen)
526 dev_dbg(&ts->client->dev,
527 "Applying '180 degrees rotated screen' quirk\n");
Bastien Noceraca96ea82014-10-31 09:26:16 -0700528}
529
Bastien Noceraca96ea82014-10-31 09:26:16 -0700530/**
531 * goodix_read_version - Read goodix touchscreen version
532 *
Irina Tirdea68caf852015-12-17 16:05:42 -0800533 * @ts: our goodix_ts_data pointer
Bastien Noceraca96ea82014-10-31 09:26:16 -0700534 */
Irina Tirdea68caf852015-12-17 16:05:42 -0800535static int goodix_read_version(struct goodix_ts_data *ts)
Bastien Noceraca96ea82014-10-31 09:26:16 -0700536{
537 int error;
538 u8 buf[6];
Irina Tirdeae70b0302015-06-09 11:04:40 -0700539 char id_str[5];
Bastien Noceraca96ea82014-10-31 09:26:16 -0700540
Irina Tirdea68caf852015-12-17 16:05:42 -0800541 error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
Bastien Noceraca96ea82014-10-31 09:26:16 -0700542 if (error) {
Irina Tirdea68caf852015-12-17 16:05:42 -0800543 dev_err(&ts->client->dev, "read version failed: %d\n", error);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700544 return error;
545 }
546
Irina Tirdeae70b0302015-06-09 11:04:40 -0700547 memcpy(id_str, buf, 4);
548 id_str[4] = 0;
Irina Tirdea68caf852015-12-17 16:05:42 -0800549 if (kstrtou16(id_str, 10, &ts->id))
550 ts->id = 0x1001;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700551
Irina Tirdea68caf852015-12-17 16:05:42 -0800552 ts->version = get_unaligned_le16(&buf[4]);
Irina Tirdeae70b0302015-06-09 11:04:40 -0700553
Irina Tirdea68caf852015-12-17 16:05:42 -0800554 dev_info(&ts->client->dev, "ID %d, version: %04x\n", ts->id,
555 ts->version);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700556
557 return 0;
558}
559
560/**
561 * goodix_i2c_test - I2C test function to check if the device answers.
562 *
563 * @client: the i2c client
564 */
565static int goodix_i2c_test(struct i2c_client *client)
566{
567 int retry = 0;
568 int error;
569 u8 test;
570
571 while (retry++ < 2) {
572 error = goodix_i2c_read(client, GOODIX_REG_CONFIG_DATA,
573 &test, 1);
574 if (!error)
575 return 0;
576
577 dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
578 retry, error);
579 msleep(20);
580 }
581
582 return error;
583}
584
585/**
586 * goodix_request_input_dev - Allocate, populate and register the input device
587 *
588 * @ts: our goodix_ts_data pointer
589 *
590 * Must be called during probe
591 */
Irina Tirdea68caf852015-12-17 16:05:42 -0800592static int goodix_request_input_dev(struct goodix_ts_data *ts)
Bastien Noceraca96ea82014-10-31 09:26:16 -0700593{
594 int error;
595
596 ts->input_dev = devm_input_allocate_device(&ts->client->dev);
597 if (!ts->input_dev) {
598 dev_err(&ts->client->dev, "Failed to allocate input device.");
599 return -ENOMEM;
600 }
601
Irina Tirdea0dfb35b2015-06-09 11:01:38 -0700602 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X,
603 0, ts->abs_x_max, 0, 0);
604 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
605 0, ts->abs_y_max, 0, 0);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700606 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
607 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
608
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -0800609 input_mt_init_slots(ts->input_dev, ts->max_touch_num,
Bastien Noceraca96ea82014-10-31 09:26:16 -0700610 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
611
612 ts->input_dev->name = "Goodix Capacitive TouchScreen";
613 ts->input_dev->phys = "input/ts";
614 ts->input_dev->id.bustype = BUS_I2C;
615 ts->input_dev->id.vendor = 0x0416;
Irina Tirdea68caf852015-12-17 16:05:42 -0800616 ts->input_dev->id.product = ts->id;
617 ts->input_dev->id.version = ts->version;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700618
619 error = input_register_device(ts->input_dev);
620 if (error) {
621 dev_err(&ts->client->dev,
622 "Failed to register input device: %d", error);
623 return error;
624 }
625
626 return 0;
627}
628
Irina Tirdea68caf852015-12-17 16:05:42 -0800629/**
630 * goodix_configure_dev - Finish device initialization
631 *
632 * @ts: our goodix_ts_data pointer
633 *
634 * Must be called from probe to finish initialization of the device.
635 * Contains the common initialization code for both devices that
636 * declare gpio pins and devices that do not. It is either called
637 * directly from probe or from request_firmware_wait callback.
638 */
639static int goodix_configure_dev(struct goodix_ts_data *ts)
640{
641 int error;
Irina Tirdea68caf852015-12-17 16:05:42 -0800642
Karsten Merkerad48cf52015-12-17 17:02:53 -0800643 ts->swapped_x_y = device_property_read_bool(&ts->client->dev,
644 "touchscreen-swapped-x-y");
645 ts->inverted_x = device_property_read_bool(&ts->client->dev,
646 "touchscreen-inverted-x");
647 ts->inverted_y = device_property_read_bool(&ts->client->dev,
648 "touchscreen-inverted-y");
649
Irina Tirdea68caf852015-12-17 16:05:42 -0800650 goodix_read_config(ts);
651
652 error = goodix_request_input_dev(ts);
653 if (error)
654 return error;
655
Irina Tirdea5ab09d62015-12-17 16:43:39 -0800656 ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
657 error = goodix_request_irq(ts);
Irina Tirdea68caf852015-12-17 16:05:42 -0800658 if (error) {
659 dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
660 return error;
661 }
662
663 return 0;
664}
665
666/**
667 * goodix_config_cb - Callback to finish device init
668 *
669 * @ts: our goodix_ts_data pointer
670 *
671 * request_firmware_wait callback that finishes
672 * initialization of the device.
673 */
674static void goodix_config_cb(const struct firmware *cfg, void *ctx)
675{
676 struct goodix_ts_data *ts = ctx;
677 int error;
678
679 if (cfg) {
680 /* send device configuration to the firmware */
681 error = goodix_send_cfg(ts, cfg);
682 if (error)
683 goto err_release_cfg;
684 }
685
686 goodix_configure_dev(ts);
687
688err_release_cfg:
689 release_firmware(cfg);
690 complete_all(&ts->firmware_loading_complete);
691}
692
Bastien Noceraca96ea82014-10-31 09:26:16 -0700693static int goodix_ts_probe(struct i2c_client *client,
694 const struct i2c_device_id *id)
695{
696 struct goodix_ts_data *ts;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700697 int error;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700698
699 dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
700
701 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
702 dev_err(&client->dev, "I2C check functionality failed.\n");
703 return -ENXIO;
704 }
705
706 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
707 if (!ts)
708 return -ENOMEM;
709
710 ts->client = client;
711 i2c_set_clientdata(client, ts);
Irina Tirdea68caf852015-12-17 16:05:42 -0800712 init_completion(&ts->firmware_loading_complete);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700713
Irina Tirdeaec6e1b42015-12-17 15:57:34 -0800714 error = goodix_get_gpio_config(ts);
715 if (error)
716 return error;
717
718 if (ts->gpiod_int && ts->gpiod_rst) {
719 /* reset the controller */
720 error = goodix_reset(ts);
721 if (error) {
722 dev_err(&client->dev, "Controller reset failed.\n");
723 return error;
724 }
725 }
726
Bastien Noceraca96ea82014-10-31 09:26:16 -0700727 error = goodix_i2c_test(client);
728 if (error) {
729 dev_err(&client->dev, "I2C communication failure: %d\n", error);
730 return error;
731 }
732
Irina Tirdea68caf852015-12-17 16:05:42 -0800733 error = goodix_read_version(ts);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700734 if (error) {
735 dev_err(&client->dev, "Read version failed.\n");
736 return error;
737 }
738
Irina Tirdea68caf852015-12-17 16:05:42 -0800739 ts->cfg_len = goodix_get_cfg_len(ts->id);
Irina Tirdeaa779fbc2015-12-17 15:55:21 -0800740
Irina Tirdea68caf852015-12-17 16:05:42 -0800741 if (ts->gpiod_int && ts->gpiod_rst) {
742 /* update device config */
743 ts->cfg_name = devm_kasprintf(&client->dev, GFP_KERNEL,
744 "goodix_%d_cfg.bin", ts->id);
745 if (!ts->cfg_name)
746 return -ENOMEM;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700747
Irina Tirdea68caf852015-12-17 16:05:42 -0800748 error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
749 &client->dev, GFP_KERNEL, ts,
750 goodix_config_cb);
751 if (error) {
752 dev_err(&client->dev,
753 "Failed to invoke firmware loader: %d\n",
754 error);
755 return error;
756 }
Bastien Noceraca96ea82014-10-31 09:26:16 -0700757
Irina Tirdea68caf852015-12-17 16:05:42 -0800758 return 0;
759 } else {
760 error = goodix_configure_dev(ts);
761 if (error)
762 return error;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700763 }
764
765 return 0;
766}
767
Irina Tirdea68caf852015-12-17 16:05:42 -0800768static int goodix_ts_remove(struct i2c_client *client)
769{
770 struct goodix_ts_data *ts = i2c_get_clientdata(client);
771
772 if (ts->gpiod_int && ts->gpiod_rst)
773 wait_for_completion(&ts->firmware_loading_complete);
774
775 return 0;
776}
777
Irina Tirdea5ab09d62015-12-17 16:43:39 -0800778static int __maybe_unused goodix_suspend(struct device *dev)
779{
780 struct i2c_client *client = to_i2c_client(dev);
781 struct goodix_ts_data *ts = i2c_get_clientdata(client);
782 int error;
783
784 /* We need gpio pins to suspend/resume */
785 if (!ts->gpiod_int || !ts->gpiod_rst)
786 return 0;
787
788 wait_for_completion(&ts->firmware_loading_complete);
789
790 /* Free IRQ as IRQ pin is used as output in the suspend sequence */
791 goodix_free_irq(ts);
792
793 /* Output LOW on the INT pin for 5 ms */
794 error = gpiod_direction_output(ts->gpiod_int, 0);
795 if (error) {
796 goodix_request_irq(ts);
797 return error;
798 }
799
800 usleep_range(5000, 6000);
801
802 error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
803 GOODIX_CMD_SCREEN_OFF);
804 if (error) {
805 dev_err(&ts->client->dev, "Screen off command failed\n");
806 gpiod_direction_input(ts->gpiod_int);
807 goodix_request_irq(ts);
808 return -EAGAIN;
809 }
810
811 /*
812 * The datasheet specifies that the interval between sending screen-off
813 * command and wake-up should be longer than 58 ms. To avoid waking up
814 * sooner, delay 58ms here.
815 */
816 msleep(58);
817 return 0;
818}
819
820static int __maybe_unused goodix_resume(struct device *dev)
821{
822 struct i2c_client *client = to_i2c_client(dev);
823 struct goodix_ts_data *ts = i2c_get_clientdata(client);
824 int error;
825
826 if (!ts->gpiod_int || !ts->gpiod_rst)
827 return 0;
828
829 /*
830 * Exit sleep mode by outputting HIGH level to INT pin
831 * for 2ms~5ms.
832 */
833 error = gpiod_direction_output(ts->gpiod_int, 1);
834 if (error)
835 return error;
836
837 usleep_range(2000, 5000);
838
839 error = goodix_int_sync(ts);
840 if (error)
841 return error;
842
843 error = goodix_request_irq(ts);
844 if (error)
845 return error;
846
847 return 0;
848}
849
850static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
851
Bastien Noceraca96ea82014-10-31 09:26:16 -0700852static const struct i2c_device_id goodix_ts_id[] = {
853 { "GDIX1001:00", 0 },
854 { }
855};
Javier Martinez Canillas2e9e9102015-07-30 10:38:52 -0700856MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700857
Aleksei Mamlin771d8f12015-03-06 16:43:38 -0800858#ifdef CONFIG_ACPI
Bastien Noceraca96ea82014-10-31 09:26:16 -0700859static const struct acpi_device_id goodix_acpi_match[] = {
860 { "GDIX1001", 0 },
861 { }
862};
863MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
Aleksei Mamlin771d8f12015-03-06 16:43:38 -0800864#endif
865
866#ifdef CONFIG_OF
867static const struct of_device_id goodix_of_match[] = {
868 { .compatible = "goodix,gt911" },
869 { .compatible = "goodix,gt9110" },
870 { .compatible = "goodix,gt912" },
871 { .compatible = "goodix,gt927" },
872 { .compatible = "goodix,gt9271" },
873 { .compatible = "goodix,gt928" },
874 { .compatible = "goodix,gt967" },
875 { }
876};
877MODULE_DEVICE_TABLE(of, goodix_of_match);
878#endif
Bastien Noceraca96ea82014-10-31 09:26:16 -0700879
880static struct i2c_driver goodix_ts_driver = {
881 .probe = goodix_ts_probe,
Irina Tirdea68caf852015-12-17 16:05:42 -0800882 .remove = goodix_ts_remove,
Bastien Noceraca96ea82014-10-31 09:26:16 -0700883 .id_table = goodix_ts_id,
884 .driver = {
885 .name = "Goodix-TS",
Aleksei Mamlin771d8f12015-03-06 16:43:38 -0800886 .acpi_match_table = ACPI_PTR(goodix_acpi_match),
887 .of_match_table = of_match_ptr(goodix_of_match),
Irina Tirdea5ab09d62015-12-17 16:43:39 -0800888 .pm = &goodix_pm_ops,
Bastien Noceraca96ea82014-10-31 09:26:16 -0700889 },
890};
891module_i2c_driver(goodix_ts_driver);
892
893MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
894MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
895MODULE_DESCRIPTION("Goodix touchscreen driver");
896MODULE_LICENSE("GPL v2");