blob: 0acefe49540d76c3e3107a593755178da1dd39fe [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>
Bastien Nocera8b5a3592015-07-24 09:08:53 -070018#include <linux/dmi.h>
Irina Tirdea68caf8582015-12-17 16:05:42 -080019#include <linux/firmware.h>
Irina Tirdeaec6e1b42015-12-17 15:57:34 -080020#include <linux/gpio/consumer.h>
Bastien Noceraca96ea82014-10-31 09:26:16 -070021#include <linux/i2c.h>
22#include <linux/input.h>
23#include <linux/input/mt.h>
24#include <linux/module.h>
25#include <linux/delay.h>
26#include <linux/irq.h>
27#include <linux/interrupt.h>
28#include <linux/slab.h>
Aleksei Mamlin771d8f12015-03-06 16:43:38 -080029#include <linux/acpi.h>
30#include <linux/of.h>
Bastien Noceraca96ea82014-10-31 09:26:16 -070031#include <asm/unaligned.h>
32
33struct goodix_ts_data {
34 struct i2c_client *client;
35 struct input_dev *input_dev;
36 int abs_x_max;
37 int abs_y_max;
38 unsigned int max_touch_num;
39 unsigned int int_trigger_type;
Bastien Nocera8b5a3592015-07-24 09:08:53 -070040 bool rotated_screen;
Irina Tirdeaa779fbc2015-12-17 15:55:21 -080041 int cfg_len;
Irina Tirdeaec6e1b42015-12-17 15:57:34 -080042 struct gpio_desc *gpiod_int;
43 struct gpio_desc *gpiod_rst;
Irina Tirdea68caf8582015-12-17 16:05:42 -080044 u16 id;
45 u16 version;
46 const char *cfg_name;
47 struct completion firmware_loading_complete;
Irina Tirdea5ab09d62015-12-17 16:43:39 -080048 unsigned long irq_flags;
Bastien Noceraca96ea82014-10-31 09:26:16 -070049};
50
Irina Tirdeaec6e1b42015-12-17 15:57:34 -080051#define GOODIX_GPIO_INT_NAME "irq"
52#define GOODIX_GPIO_RST_NAME "reset"
53
Bastien Noceraca96ea82014-10-31 09:26:16 -070054#define GOODIX_MAX_HEIGHT 4096
55#define GOODIX_MAX_WIDTH 4096
56#define GOODIX_INT_TRIGGER 1
57#define GOODIX_CONTACT_SIZE 8
58#define GOODIX_MAX_CONTACTS 10
59
60#define GOODIX_CONFIG_MAX_LENGTH 240
Irina Tirdeaa779fbc2015-12-17 15:55:21 -080061#define GOODIX_CONFIG_911_LENGTH 186
62#define GOODIX_CONFIG_967_LENGTH 228
Bastien Noceraca96ea82014-10-31 09:26:16 -070063
64/* Register defines */
Irina Tirdea5ab09d62015-12-17 16:43:39 -080065#define GOODIX_REG_COMMAND 0x8040
66#define GOODIX_CMD_SCREEN_OFF 0x05
67
Bastien Noceraca96ea82014-10-31 09:26:16 -070068#define GOODIX_READ_COOR_ADDR 0x814E
69#define GOODIX_REG_CONFIG_DATA 0x8047
Irina Tirdeae70b0302015-06-09 11:04:40 -070070#define GOODIX_REG_ID 0x8140
Bastien Noceraca96ea82014-10-31 09:26:16 -070071
72#define RESOLUTION_LOC 1
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -080073#define MAX_CONTACTS_LOC 5
Bastien Noceraca96ea82014-10-31 09:26:16 -070074#define TRIGGER_LOC 6
75
76static const unsigned long goodix_irq_flags[] = {
77 IRQ_TYPE_EDGE_RISING,
78 IRQ_TYPE_EDGE_FALLING,
79 IRQ_TYPE_LEVEL_LOW,
80 IRQ_TYPE_LEVEL_HIGH,
81};
82
Bastien Nocera8b5a3592015-07-24 09:08:53 -070083/*
84 * Those tablets have their coordinates origin at the bottom right
85 * of the tablet, as if rotated 180 degrees
86 */
87static const struct dmi_system_id rotated_screen[] = {
88#if defined(CONFIG_DMI) && defined(CONFIG_X86)
89 {
90 .ident = "WinBook TW100",
91 .matches = {
92 DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
93 DMI_MATCH(DMI_PRODUCT_NAME, "TW100")
94 }
95 },
96 {
97 .ident = "WinBook TW700",
98 .matches = {
99 DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
100 DMI_MATCH(DMI_PRODUCT_NAME, "TW700")
101 },
102 },
103#endif
104 {}
105};
106
Bastien Noceraca96ea82014-10-31 09:26:16 -0700107/**
108 * goodix_i2c_read - read data from a register of the i2c slave device.
109 *
110 * @client: i2c device.
111 * @reg: the register to read from.
112 * @buf: raw write data buffer.
113 * @len: length of the buffer to write
114 */
115static int goodix_i2c_read(struct i2c_client *client,
Irina Tirdea0dfb35b2015-06-09 11:01:38 -0700116 u16 reg, u8 *buf, int len)
Bastien Noceraca96ea82014-10-31 09:26:16 -0700117{
118 struct i2c_msg msgs[2];
119 u16 wbuf = cpu_to_be16(reg);
120 int ret;
121
122 msgs[0].flags = 0;
123 msgs[0].addr = client->addr;
124 msgs[0].len = 2;
Irina Tirdea0dfb35b2015-06-09 11:01:38 -0700125 msgs[0].buf = (u8 *)&wbuf;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700126
127 msgs[1].flags = I2C_M_RD;
128 msgs[1].addr = client->addr;
129 msgs[1].len = len;
130 msgs[1].buf = buf;
131
132 ret = i2c_transfer(client->adapter, msgs, 2);
133 return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
134}
135
Irina Tirdea68caf8582015-12-17 16:05:42 -0800136/**
137 * goodix_i2c_write - write data to a register of the i2c slave device.
138 *
139 * @client: i2c device.
140 * @reg: the register to write to.
141 * @buf: raw data buffer to write.
142 * @len: length of the buffer to write
143 */
144static int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf,
145 unsigned len)
146{
147 u8 *addr_buf;
148 struct i2c_msg msg;
149 int ret;
150
151 addr_buf = kmalloc(len + 2, GFP_KERNEL);
152 if (!addr_buf)
153 return -ENOMEM;
154
155 addr_buf[0] = reg >> 8;
156 addr_buf[1] = reg & 0xFF;
157 memcpy(&addr_buf[2], buf, len);
158
159 msg.flags = 0;
160 msg.addr = client->addr;
161 msg.buf = addr_buf;
162 msg.len = len + 2;
163
164 ret = i2c_transfer(client->adapter, &msg, 1);
165 kfree(addr_buf);
166 return ret < 0 ? ret : (ret != 1 ? -EIO : 0);
167}
168
Irina Tirdea5ab09d62015-12-17 16:43:39 -0800169static int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value)
170{
171 return goodix_i2c_write(client, reg, &value, sizeof(value));
172}
173
Irina Tirdeaa779fbc2015-12-17 15:55:21 -0800174static int goodix_get_cfg_len(u16 id)
175{
176 switch (id) {
177 case 911:
178 case 9271:
179 case 9110:
180 case 927:
181 case 928:
182 return GOODIX_CONFIG_911_LENGTH;
183
184 case 912:
185 case 967:
186 return GOODIX_CONFIG_967_LENGTH;
187
188 default:
189 return GOODIX_CONFIG_MAX_LENGTH;
190 }
191}
192
Bastien Noceraca96ea82014-10-31 09:26:16 -0700193static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
194{
195 int touch_num;
196 int error;
197
198 error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR, data,
199 GOODIX_CONTACT_SIZE + 1);
200 if (error) {
201 dev_err(&ts->client->dev, "I2C transfer error: %d\n", error);
202 return error;
203 }
204
Paul Cercueil5f6f1172015-05-06 16:52:13 -0700205 if (!(data[0] & 0x80))
206 return -EAGAIN;
207
Bastien Noceraca96ea82014-10-31 09:26:16 -0700208 touch_num = data[0] & 0x0f;
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -0800209 if (touch_num > ts->max_touch_num)
Bastien Noceraca96ea82014-10-31 09:26:16 -0700210 return -EPROTO;
211
212 if (touch_num > 1) {
213 data += 1 + GOODIX_CONTACT_SIZE;
214 error = goodix_i2c_read(ts->client,
215 GOODIX_READ_COOR_ADDR +
216 1 + GOODIX_CONTACT_SIZE,
217 data,
218 GOODIX_CONTACT_SIZE * (touch_num - 1));
219 if (error)
220 return error;
221 }
222
223 return touch_num;
224}
225
226static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data)
227{
228 int id = coor_data[0] & 0x0F;
229 int input_x = get_unaligned_le16(&coor_data[1]);
230 int input_y = get_unaligned_le16(&coor_data[3]);
231 int input_w = get_unaligned_le16(&coor_data[5]);
232
Bastien Nocera8b5a3592015-07-24 09:08:53 -0700233 if (ts->rotated_screen) {
234 input_x = ts->abs_x_max - input_x;
235 input_y = ts->abs_y_max - input_y;
236 }
237
Bastien Noceraca96ea82014-10-31 09:26:16 -0700238 input_mt_slot(ts->input_dev, id);
239 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
240 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, input_x);
241 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, input_y);
242 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
243 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
244}
245
246/**
247 * goodix_process_events - Process incoming events
248 *
249 * @ts: our goodix_ts_data pointer
250 *
251 * Called when the IRQ is triggered. Read the current device state, and push
252 * the input events to the user space.
253 */
254static void goodix_process_events(struct goodix_ts_data *ts)
255{
Irina Tirdea0e0432f2015-06-09 11:03:15 -0700256 u8 point_data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
Bastien Noceraca96ea82014-10-31 09:26:16 -0700257 int touch_num;
258 int i;
259
260 touch_num = goodix_ts_read_input_report(ts, point_data);
261 if (touch_num < 0)
262 return;
263
264 for (i = 0; i < touch_num; i++)
265 goodix_ts_report_touch(ts,
266 &point_data[1 + GOODIX_CONTACT_SIZE * i]);
267
268 input_mt_sync_frame(ts->input_dev);
269 input_sync(ts->input_dev);
270}
271
272/**
273 * goodix_ts_irq_handler - The IRQ handler
274 *
275 * @irq: interrupt number.
276 * @dev_id: private data pointer.
277 */
278static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
279{
Bastien Noceraca96ea82014-10-31 09:26:16 -0700280 struct goodix_ts_data *ts = dev_id;
281
282 goodix_process_events(ts);
283
Irina Tirdea5d655b32015-12-17 16:47:42 -0800284 if (goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0) < 0)
Bastien Noceraca96ea82014-10-31 09:26:16 -0700285 dev_err(&ts->client->dev, "I2C write end_cmd error\n");
286
287 return IRQ_HANDLED;
288}
289
Irina Tirdea5ab09d62015-12-17 16:43:39 -0800290static void goodix_free_irq(struct goodix_ts_data *ts)
291{
292 devm_free_irq(&ts->client->dev, ts->client->irq, ts);
293}
294
295static int goodix_request_irq(struct goodix_ts_data *ts)
296{
297 return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
298 NULL, goodix_ts_irq_handler,
299 ts->irq_flags, ts->client->name, ts);
300}
301
Irina Tirdea68caf8582015-12-17 16:05:42 -0800302/**
303 * goodix_check_cfg - Checks if config fw is valid
304 *
305 * @ts: goodix_ts_data pointer
306 * @cfg: firmware config data
307 */
308static int goodix_check_cfg(struct goodix_ts_data *ts,
309 const struct firmware *cfg)
310{
311 int i, raw_cfg_len;
312 u8 check_sum = 0;
313
314 if (cfg->size > GOODIX_CONFIG_MAX_LENGTH) {
315 dev_err(&ts->client->dev,
316 "The length of the config fw is not correct");
317 return -EINVAL;
318 }
319
320 raw_cfg_len = cfg->size - 2;
321 for (i = 0; i < raw_cfg_len; i++)
322 check_sum += cfg->data[i];
323 check_sum = (~check_sum) + 1;
324 if (check_sum != cfg->data[raw_cfg_len]) {
325 dev_err(&ts->client->dev,
326 "The checksum of the config fw is not correct");
327 return -EINVAL;
328 }
329
330 if (cfg->data[raw_cfg_len + 1] != 1) {
331 dev_err(&ts->client->dev,
332 "Config fw must have Config_Fresh register set");
333 return -EINVAL;
334 }
335
336 return 0;
337}
338
339/**
340 * goodix_send_cfg - Write fw config to device
341 *
342 * @ts: goodix_ts_data pointer
343 * @cfg: config firmware to write to device
344 */
345static int goodix_send_cfg(struct goodix_ts_data *ts,
346 const struct firmware *cfg)
347{
348 int error;
349
350 error = goodix_check_cfg(ts, cfg);
351 if (error)
352 return error;
353
354 error = goodix_i2c_write(ts->client, GOODIX_REG_CONFIG_DATA, cfg->data,
355 cfg->size);
356 if (error) {
357 dev_err(&ts->client->dev, "Failed to write config data: %d",
358 error);
359 return error;
360 }
361 dev_dbg(&ts->client->dev, "Config sent successfully.");
362
363 /* Let the firmware reconfigure itself, so sleep for 10ms */
364 usleep_range(10000, 11000);
365
366 return 0;
367}
368
Irina Tirdeaec6e1b42015-12-17 15:57:34 -0800369static int goodix_int_sync(struct goodix_ts_data *ts)
370{
371 int error;
372
373 error = gpiod_direction_output(ts->gpiod_int, 0);
374 if (error)
375 return error;
376
377 msleep(50); /* T5: 50ms */
378
379 error = gpiod_direction_input(ts->gpiod_int);
380 if (error)
381 return error;
382
383 return 0;
384}
385
386/**
387 * goodix_reset - Reset device during power on
388 *
389 * @ts: goodix_ts_data pointer
390 */
391static int goodix_reset(struct goodix_ts_data *ts)
392{
393 int error;
394
395 /* begin select I2C slave addr */
396 error = gpiod_direction_output(ts->gpiod_rst, 0);
397 if (error)
398 return error;
399
400 msleep(20); /* T2: > 10ms */
401
402 /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
403 error = gpiod_direction_output(ts->gpiod_int, ts->client->addr == 0x14);
404 if (error)
405 return error;
406
407 usleep_range(100, 2000); /* T3: > 100us */
408
409 error = gpiod_direction_output(ts->gpiod_rst, 1);
410 if (error)
411 return error;
412
413 usleep_range(6000, 10000); /* T4: > 5ms */
414
415 /* end select I2C slave addr */
416 error = gpiod_direction_input(ts->gpiod_rst);
417 if (error)
418 return error;
419
420 error = goodix_int_sync(ts);
421 if (error)
422 return error;
423
424 return 0;
425}
426
427/**
428 * goodix_get_gpio_config - Get GPIO config from ACPI/DT
429 *
430 * @ts: goodix_ts_data pointer
431 */
432static int goodix_get_gpio_config(struct goodix_ts_data *ts)
433{
434 int error;
435 struct device *dev;
436 struct gpio_desc *gpiod;
437
438 if (!ts->client)
439 return -EINVAL;
440 dev = &ts->client->dev;
441
442 /* Get the interrupt GPIO pin number */
443 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
444 if (IS_ERR(gpiod)) {
445 error = PTR_ERR(gpiod);
446 if (error != -EPROBE_DEFER)
447 dev_dbg(dev, "Failed to get %s GPIO: %d\n",
448 GOODIX_GPIO_INT_NAME, error);
449 return error;
450 }
451
452 ts->gpiod_int = gpiod;
453
454 /* Get the reset line GPIO pin number */
455 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_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_RST_NAME, error);
461 return error;
462 }
463
464 ts->gpiod_rst = gpiod;
465
466 return 0;
467}
468
Bastien Noceraca96ea82014-10-31 09:26:16 -0700469/**
470 * goodix_read_config - Read the embedded configuration of the panel
471 *
472 * @ts: our goodix_ts_data pointer
473 *
474 * Must be called during probe
475 */
476static void goodix_read_config(struct goodix_ts_data *ts)
477{
478 u8 config[GOODIX_CONFIG_MAX_LENGTH];
479 int error;
480
481 error = goodix_i2c_read(ts->client, GOODIX_REG_CONFIG_DATA,
Irina Tirdeaa779fbc2015-12-17 15:55:21 -0800482 config, ts->cfg_len);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700483 if (error) {
484 dev_warn(&ts->client->dev,
485 "Error reading config (%d), using defaults\n",
486 error);
487 ts->abs_x_max = GOODIX_MAX_WIDTH;
488 ts->abs_y_max = GOODIX_MAX_HEIGHT;
489 ts->int_trigger_type = GOODIX_INT_TRIGGER;
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -0800490 ts->max_touch_num = GOODIX_MAX_CONTACTS;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700491 return;
492 }
493
494 ts->abs_x_max = get_unaligned_le16(&config[RESOLUTION_LOC]);
495 ts->abs_y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]);
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -0800496 ts->int_trigger_type = config[TRIGGER_LOC] & 0x03;
497 ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f;
498 if (!ts->abs_x_max || !ts->abs_y_max || !ts->max_touch_num) {
Bastien Noceraca96ea82014-10-31 09:26:16 -0700499 dev_err(&ts->client->dev,
500 "Invalid config, using defaults\n");
501 ts->abs_x_max = GOODIX_MAX_WIDTH;
502 ts->abs_y_max = GOODIX_MAX_HEIGHT;
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -0800503 ts->max_touch_num = GOODIX_MAX_CONTACTS;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700504 }
Bastien Nocera8b5a3592015-07-24 09:08:53 -0700505
506 ts->rotated_screen = dmi_check_system(rotated_screen);
507 if (ts->rotated_screen)
508 dev_dbg(&ts->client->dev,
509 "Applying '180 degrees rotated screen' quirk\n");
Bastien Noceraca96ea82014-10-31 09:26:16 -0700510}
511
Bastien Noceraca96ea82014-10-31 09:26:16 -0700512/**
513 * goodix_read_version - Read goodix touchscreen version
514 *
Irina Tirdea68caf8582015-12-17 16:05:42 -0800515 * @ts: our goodix_ts_data pointer
Bastien Noceraca96ea82014-10-31 09:26:16 -0700516 */
Irina Tirdea68caf8582015-12-17 16:05:42 -0800517static int goodix_read_version(struct goodix_ts_data *ts)
Bastien Noceraca96ea82014-10-31 09:26:16 -0700518{
519 int error;
520 u8 buf[6];
Irina Tirdeae70b0302015-06-09 11:04:40 -0700521 char id_str[5];
Bastien Noceraca96ea82014-10-31 09:26:16 -0700522
Irina Tirdea68caf8582015-12-17 16:05:42 -0800523 error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
Bastien Noceraca96ea82014-10-31 09:26:16 -0700524 if (error) {
Irina Tirdea68caf8582015-12-17 16:05:42 -0800525 dev_err(&ts->client->dev, "read version failed: %d\n", error);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700526 return error;
527 }
528
Irina Tirdeae70b0302015-06-09 11:04:40 -0700529 memcpy(id_str, buf, 4);
530 id_str[4] = 0;
Irina Tirdea68caf8582015-12-17 16:05:42 -0800531 if (kstrtou16(id_str, 10, &ts->id))
532 ts->id = 0x1001;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700533
Irina Tirdea68caf8582015-12-17 16:05:42 -0800534 ts->version = get_unaligned_le16(&buf[4]);
Irina Tirdeae70b0302015-06-09 11:04:40 -0700535
Irina Tirdea68caf8582015-12-17 16:05:42 -0800536 dev_info(&ts->client->dev, "ID %d, version: %04x\n", ts->id,
537 ts->version);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700538
539 return 0;
540}
541
542/**
543 * goodix_i2c_test - I2C test function to check if the device answers.
544 *
545 * @client: the i2c client
546 */
547static int goodix_i2c_test(struct i2c_client *client)
548{
549 int retry = 0;
550 int error;
551 u8 test;
552
553 while (retry++ < 2) {
554 error = goodix_i2c_read(client, GOODIX_REG_CONFIG_DATA,
555 &test, 1);
556 if (!error)
557 return 0;
558
559 dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
560 retry, error);
561 msleep(20);
562 }
563
564 return error;
565}
566
567/**
568 * goodix_request_input_dev - Allocate, populate and register the input device
569 *
570 * @ts: our goodix_ts_data pointer
571 *
572 * Must be called during probe
573 */
Irina Tirdea68caf8582015-12-17 16:05:42 -0800574static int goodix_request_input_dev(struct goodix_ts_data *ts)
Bastien Noceraca96ea82014-10-31 09:26:16 -0700575{
576 int error;
577
578 ts->input_dev = devm_input_allocate_device(&ts->client->dev);
579 if (!ts->input_dev) {
580 dev_err(&ts->client->dev, "Failed to allocate input device.");
581 return -ENOMEM;
582 }
583
Irina Tirdea0dfb35b2015-06-09 11:01:38 -0700584 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X,
585 0, ts->abs_x_max, 0, 0);
586 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
587 0, ts->abs_y_max, 0, 0);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700588 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
589 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
590
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -0800591 input_mt_init_slots(ts->input_dev, ts->max_touch_num,
Bastien Noceraca96ea82014-10-31 09:26:16 -0700592 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
593
594 ts->input_dev->name = "Goodix Capacitive TouchScreen";
595 ts->input_dev->phys = "input/ts";
596 ts->input_dev->id.bustype = BUS_I2C;
597 ts->input_dev->id.vendor = 0x0416;
Irina Tirdea68caf8582015-12-17 16:05:42 -0800598 ts->input_dev->id.product = ts->id;
599 ts->input_dev->id.version = ts->version;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700600
601 error = input_register_device(ts->input_dev);
602 if (error) {
603 dev_err(&ts->client->dev,
604 "Failed to register input device: %d", error);
605 return error;
606 }
607
608 return 0;
609}
610
Irina Tirdea68caf8582015-12-17 16:05:42 -0800611/**
612 * goodix_configure_dev - Finish device initialization
613 *
614 * @ts: our goodix_ts_data pointer
615 *
616 * Must be called from probe to finish initialization of the device.
617 * Contains the common initialization code for both devices that
618 * declare gpio pins and devices that do not. It is either called
619 * directly from probe or from request_firmware_wait callback.
620 */
621static int goodix_configure_dev(struct goodix_ts_data *ts)
622{
623 int error;
Irina Tirdea68caf8582015-12-17 16:05:42 -0800624
625 goodix_read_config(ts);
626
627 error = goodix_request_input_dev(ts);
628 if (error)
629 return error;
630
Irina Tirdea5ab09d62015-12-17 16:43:39 -0800631 ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
632 error = goodix_request_irq(ts);
Irina Tirdea68caf8582015-12-17 16:05:42 -0800633 if (error) {
634 dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
635 return error;
636 }
637
638 return 0;
639}
640
641/**
642 * goodix_config_cb - Callback to finish device init
643 *
644 * @ts: our goodix_ts_data pointer
645 *
646 * request_firmware_wait callback that finishes
647 * initialization of the device.
648 */
649static void goodix_config_cb(const struct firmware *cfg, void *ctx)
650{
651 struct goodix_ts_data *ts = ctx;
652 int error;
653
654 if (cfg) {
655 /* send device configuration to the firmware */
656 error = goodix_send_cfg(ts, cfg);
657 if (error)
658 goto err_release_cfg;
659 }
660
661 goodix_configure_dev(ts);
662
663err_release_cfg:
664 release_firmware(cfg);
665 complete_all(&ts->firmware_loading_complete);
666}
667
Bastien Noceraca96ea82014-10-31 09:26:16 -0700668static int goodix_ts_probe(struct i2c_client *client,
669 const struct i2c_device_id *id)
670{
671 struct goodix_ts_data *ts;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700672 int error;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700673
674 dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
675
676 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
677 dev_err(&client->dev, "I2C check functionality failed.\n");
678 return -ENXIO;
679 }
680
681 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
682 if (!ts)
683 return -ENOMEM;
684
685 ts->client = client;
686 i2c_set_clientdata(client, ts);
Irina Tirdea68caf8582015-12-17 16:05:42 -0800687 init_completion(&ts->firmware_loading_complete);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700688
Irina Tirdeaec6e1b42015-12-17 15:57:34 -0800689 error = goodix_get_gpio_config(ts);
690 if (error)
691 return error;
692
693 if (ts->gpiod_int && ts->gpiod_rst) {
694 /* reset the controller */
695 error = goodix_reset(ts);
696 if (error) {
697 dev_err(&client->dev, "Controller reset failed.\n");
698 return error;
699 }
700 }
701
Bastien Noceraca96ea82014-10-31 09:26:16 -0700702 error = goodix_i2c_test(client);
703 if (error) {
704 dev_err(&client->dev, "I2C communication failure: %d\n", error);
705 return error;
706 }
707
Irina Tirdea68caf8582015-12-17 16:05:42 -0800708 error = goodix_read_version(ts);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700709 if (error) {
710 dev_err(&client->dev, "Read version failed.\n");
711 return error;
712 }
713
Irina Tirdea68caf8582015-12-17 16:05:42 -0800714 ts->cfg_len = goodix_get_cfg_len(ts->id);
Irina Tirdeaa779fbc2015-12-17 15:55:21 -0800715
Irina Tirdea68caf8582015-12-17 16:05:42 -0800716 if (ts->gpiod_int && ts->gpiod_rst) {
717 /* update device config */
718 ts->cfg_name = devm_kasprintf(&client->dev, GFP_KERNEL,
719 "goodix_%d_cfg.bin", ts->id);
720 if (!ts->cfg_name)
721 return -ENOMEM;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700722
Irina Tirdea68caf8582015-12-17 16:05:42 -0800723 error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
724 &client->dev, GFP_KERNEL, ts,
725 goodix_config_cb);
726 if (error) {
727 dev_err(&client->dev,
728 "Failed to invoke firmware loader: %d\n",
729 error);
730 return error;
731 }
Bastien Noceraca96ea82014-10-31 09:26:16 -0700732
Irina Tirdea68caf8582015-12-17 16:05:42 -0800733 return 0;
734 } else {
735 error = goodix_configure_dev(ts);
736 if (error)
737 return error;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700738 }
739
740 return 0;
741}
742
Irina Tirdea68caf8582015-12-17 16:05:42 -0800743static int goodix_ts_remove(struct i2c_client *client)
744{
745 struct goodix_ts_data *ts = i2c_get_clientdata(client);
746
747 if (ts->gpiod_int && ts->gpiod_rst)
748 wait_for_completion(&ts->firmware_loading_complete);
749
750 return 0;
751}
752
Irina Tirdea5ab09d62015-12-17 16:43:39 -0800753static int __maybe_unused goodix_suspend(struct device *dev)
754{
755 struct i2c_client *client = to_i2c_client(dev);
756 struct goodix_ts_data *ts = i2c_get_clientdata(client);
757 int error;
758
759 /* We need gpio pins to suspend/resume */
760 if (!ts->gpiod_int || !ts->gpiod_rst)
761 return 0;
762
763 wait_for_completion(&ts->firmware_loading_complete);
764
765 /* Free IRQ as IRQ pin is used as output in the suspend sequence */
766 goodix_free_irq(ts);
767
768 /* Output LOW on the INT pin for 5 ms */
769 error = gpiod_direction_output(ts->gpiod_int, 0);
770 if (error) {
771 goodix_request_irq(ts);
772 return error;
773 }
774
775 usleep_range(5000, 6000);
776
777 error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
778 GOODIX_CMD_SCREEN_OFF);
779 if (error) {
780 dev_err(&ts->client->dev, "Screen off command failed\n");
781 gpiod_direction_input(ts->gpiod_int);
782 goodix_request_irq(ts);
783 return -EAGAIN;
784 }
785
786 /*
787 * The datasheet specifies that the interval between sending screen-off
788 * command and wake-up should be longer than 58 ms. To avoid waking up
789 * sooner, delay 58ms here.
790 */
791 msleep(58);
792 return 0;
793}
794
795static int __maybe_unused goodix_resume(struct device *dev)
796{
797 struct i2c_client *client = to_i2c_client(dev);
798 struct goodix_ts_data *ts = i2c_get_clientdata(client);
799 int error;
800
801 if (!ts->gpiod_int || !ts->gpiod_rst)
802 return 0;
803
804 /*
805 * Exit sleep mode by outputting HIGH level to INT pin
806 * for 2ms~5ms.
807 */
808 error = gpiod_direction_output(ts->gpiod_int, 1);
809 if (error)
810 return error;
811
812 usleep_range(2000, 5000);
813
814 error = goodix_int_sync(ts);
815 if (error)
816 return error;
817
818 error = goodix_request_irq(ts);
819 if (error)
820 return error;
821
822 return 0;
823}
824
825static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
826
Bastien Noceraca96ea82014-10-31 09:26:16 -0700827static const struct i2c_device_id goodix_ts_id[] = {
828 { "GDIX1001:00", 0 },
829 { }
830};
Javier Martinez Canillas2e9e9102015-07-30 10:38:52 -0700831MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700832
Aleksei Mamlin771d8f12015-03-06 16:43:38 -0800833#ifdef CONFIG_ACPI
Bastien Noceraca96ea82014-10-31 09:26:16 -0700834static const struct acpi_device_id goodix_acpi_match[] = {
835 { "GDIX1001", 0 },
836 { }
837};
838MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
Aleksei Mamlin771d8f12015-03-06 16:43:38 -0800839#endif
840
841#ifdef CONFIG_OF
842static const struct of_device_id goodix_of_match[] = {
843 { .compatible = "goodix,gt911" },
844 { .compatible = "goodix,gt9110" },
845 { .compatible = "goodix,gt912" },
846 { .compatible = "goodix,gt927" },
847 { .compatible = "goodix,gt9271" },
848 { .compatible = "goodix,gt928" },
849 { .compatible = "goodix,gt967" },
850 { }
851};
852MODULE_DEVICE_TABLE(of, goodix_of_match);
853#endif
Bastien Noceraca96ea82014-10-31 09:26:16 -0700854
855static struct i2c_driver goodix_ts_driver = {
856 .probe = goodix_ts_probe,
Irina Tirdea68caf8582015-12-17 16:05:42 -0800857 .remove = goodix_ts_remove,
Bastien Noceraca96ea82014-10-31 09:26:16 -0700858 .id_table = goodix_ts_id,
859 .driver = {
860 .name = "Goodix-TS",
Aleksei Mamlin771d8f12015-03-06 16:43:38 -0800861 .acpi_match_table = ACPI_PTR(goodix_acpi_match),
862 .of_match_table = of_match_ptr(goodix_of_match),
Irina Tirdea5ab09d62015-12-17 16:43:39 -0800863 .pm = &goodix_pm_ops,
Bastien Noceraca96ea82014-10-31 09:26:16 -0700864 },
865};
866module_i2c_driver(goodix_ts_driver);
867
868MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
869MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
870MODULE_DESCRIPTION("Goodix touchscreen driver");
871MODULE_LICENSE("GPL v2");