goodix.c 39.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5
/*
 *  Driver for Goodix Touchscreens
 *
 *  Copyright (c) 2014 Red Hat Inc.
6
 *  Copyright (c) 2015 K. Merker <merker@debian.org>
7 8 9 10 11 12 13 14
 *
 *  This code is based on gt9xx.c authored by andrew@goodix.com:
 *
 *  2010 - 2012 Goodix Technology.
 */


#include <linux/kernel.h>
15
#include <linux/dmi.h>
16
#include <linux/firmware.h>
17 18 19 20
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
21
#include <linux/platform_data/x86/soc.h>
22
#include <linux/slab.h>
23 24
#include <linux/acpi.h>
#include <linux/of.h>
25
#include <asm/unaligned.h>
26
#include "goodix.h"
27

28 29 30 31 32 33 34 35 36 37
#define GOODIX_GPIO_INT_NAME		"irq"
#define GOODIX_GPIO_RST_NAME		"reset"

#define GOODIX_MAX_HEIGHT		4096
#define GOODIX_MAX_WIDTH		4096
#define GOODIX_INT_TRIGGER		1
#define GOODIX_CONTACT_SIZE		8
#define GOODIX_MAX_CONTACT_SIZE		9
#define GOODIX_MAX_CONTACTS		10

38
#define GOODIX_CONFIG_MIN_LENGTH	186
39 40
#define GOODIX_CONFIG_911_LENGTH	186
#define GOODIX_CONFIG_967_LENGTH	228
41
#define GOODIX_CONFIG_GT9X_LENGTH	240
42 43

#define GOODIX_BUFFER_STATUS_READY	BIT(7)
44
#define GOODIX_HAVE_KEY			BIT(4)
45 46 47 48 49 50
#define GOODIX_BUFFER_STATUS_TIMEOUT	20

#define RESOLUTION_LOC		1
#define MAX_CONTACTS_LOC	5
#define TRIGGER_LOC		6

51 52 53 54 55
/* Our special handling for GPIO accesses through ACPI is x86 specific */
#if defined CONFIG_X86 && defined CONFIG_ACPI
#define ACPI_GPIO_SUPPORT
#endif

56 57 58 59 60
struct goodix_chip_id {
	const char *id;
	const struct goodix_chip_data *data;
};

61
static int goodix_check_cfg_8(struct goodix_ts_data *ts,
62
			      const u8 *cfg, int len);
63
static int goodix_check_cfg_16(struct goodix_ts_data *ts,
64
			       const u8 *cfg, int len);
65 66
static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts);
static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts);
67 68 69

static const struct goodix_chip_data gt1x_chip_data = {
	.config_addr		= GOODIX_GT1X_REG_CONFIG_DATA,
70
	.config_len		= GOODIX_CONFIG_GT9X_LENGTH,
71
	.check_config		= goodix_check_cfg_16,
72
	.calc_config_checksum	= goodix_calc_cfg_checksum_16,
73 74 75 76 77 78
};

static const struct goodix_chip_data gt911_chip_data = {
	.config_addr		= GOODIX_GT9X_REG_CONFIG_DATA,
	.config_len		= GOODIX_CONFIG_911_LENGTH,
	.check_config		= goodix_check_cfg_8,
79
	.calc_config_checksum	= goodix_calc_cfg_checksum_8,
80 81 82 83 84 85
};

static const struct goodix_chip_data gt967_chip_data = {
	.config_addr		= GOODIX_GT9X_REG_CONFIG_DATA,
	.config_len		= GOODIX_CONFIG_967_LENGTH,
	.check_config		= goodix_check_cfg_8,
86
	.calc_config_checksum	= goodix_calc_cfg_checksum_8,
87 88 89 90
};

static const struct goodix_chip_data gt9x_chip_data = {
	.config_addr		= GOODIX_GT9X_REG_CONFIG_DATA,
91
	.config_len		= GOODIX_CONFIG_GT9X_LENGTH,
92
	.check_config		= goodix_check_cfg_8,
93
	.calc_config_checksum	= goodix_calc_cfg_checksum_8,
94 95
};

96 97 98 99
static const struct goodix_chip_id goodix_chip_ids[] = {
	{ .id = "1151", .data = &gt1x_chip_data },
	{ .id = "5663", .data = &gt1x_chip_data },
	{ .id = "5688", .data = &gt1x_chip_data },
100
	{ .id = "917S", .data = &gt1x_chip_data },
101
	{ .id = "9286", .data = &gt1x_chip_data },
102 103 104 105

	{ .id = "911", .data = &gt911_chip_data },
	{ .id = "9271", .data = &gt911_chip_data },
	{ .id = "9110", .data = &gt911_chip_data },
106
	{ .id = "9111", .data = &gt911_chip_data },
107 108 109 110
	{ .id = "927", .data = &gt911_chip_data },
	{ .id = "928", .data = &gt911_chip_data },

	{ .id = "912", .data = &gt967_chip_data },
111
	{ .id = "9147", .data = &gt967_chip_data },
112 113 114 115
	{ .id = "967", .data = &gt967_chip_data },
	{ }
};

116 117 118 119 120 121 122
static const unsigned long goodix_irq_flags[] = {
	IRQ_TYPE_EDGE_RISING,
	IRQ_TYPE_EDGE_FALLING,
	IRQ_TYPE_LEVEL_LOW,
	IRQ_TYPE_LEVEL_HIGH,
};

123 124 125 126 127 128 129 130 131 132 133 134 135
static const struct dmi_system_id nine_bytes_report[] = {
#if defined(CONFIG_DMI) && defined(CONFIG_X86)
	{
		.ident = "Lenovo YogaBook",
		/* YB1-X91L/F and YB1-X90L/F */
		.matches = {
			DMI_MATCH(DMI_PRODUCT_NAME, "Lenovo YB1-X9")
		}
	},
#endif
	{}
};

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
/*
 * Those tablets have their x coordinate inverted
 */
static const struct dmi_system_id inverted_x_screen[] = {
#if defined(CONFIG_DMI) && defined(CONFIG_X86)
	{
		.ident = "Cube I15-TC",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Cube"),
			DMI_MATCH(DMI_PRODUCT_NAME, "I15-TC")
		},
	},
#endif
	{}
};

152 153 154 155 156 157 158 159
/**
 * goodix_i2c_read - read data from a register of the i2c slave device.
 *
 * @client: i2c device.
 * @reg: the register to read from.
 * @buf: raw write data buffer.
 * @len: length of the buffer to write
 */
160
int goodix_i2c_read(struct i2c_client *client, u16 reg, u8 *buf, int len)
161 162
{
	struct i2c_msg msgs[2];
163
	__be16 wbuf = cpu_to_be16(reg);
164 165 166 167 168
	int ret;

	msgs[0].flags = 0;
	msgs[0].addr  = client->addr;
	msgs[0].len   = 2;
169
	msgs[0].buf   = (u8 *)&wbuf;
170 171 172 173 174 175 176

	msgs[1].flags = I2C_M_RD;
	msgs[1].addr  = client->addr;
	msgs[1].len   = len;
	msgs[1].buf   = buf;

	ret = i2c_transfer(client->adapter, msgs, 2);
177 178 179 180 181 182 183
	if (ret >= 0)
		ret = (ret == ARRAY_SIZE(msgs) ? 0 : -EIO);

	if (ret)
		dev_err(&client->dev, "Error reading %d bytes from 0x%04x: %d\n",
			len, reg, ret);
	return ret;
184 185
}

186 187 188 189 190 191 192 193
/**
 * goodix_i2c_write - write data to a register of the i2c slave device.
 *
 * @client: i2c device.
 * @reg: the register to write to.
 * @buf: raw data buffer to write.
 * @len: length of the buffer to write
 */
194
int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf, int len)
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
{
	u8 *addr_buf;
	struct i2c_msg msg;
	int ret;

	addr_buf = kmalloc(len + 2, GFP_KERNEL);
	if (!addr_buf)
		return -ENOMEM;

	addr_buf[0] = reg >> 8;
	addr_buf[1] = reg & 0xFF;
	memcpy(&addr_buf[2], buf, len);

	msg.flags = 0;
	msg.addr = client->addr;
	msg.buf = addr_buf;
	msg.len = len + 2;

	ret = i2c_transfer(client->adapter, &msg, 1);
214 215 216
	if (ret >= 0)
		ret = (ret == 1 ? 0 : -EIO);

217
	kfree(addr_buf);
218 219 220 221 222

	if (ret)
		dev_err(&client->dev, "Error writing %d bytes to 0x%04x: %d\n",
			len, reg, ret);
	return ret;
223 224
}

225
int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value)
226 227 228 229
{
	return goodix_i2c_write(client, reg, &value, sizeof(value));
}

230
static const struct goodix_chip_data *goodix_get_chip_data(const char *id)
231
{
232
	unsigned int i;
233

234 235 236
	for (i = 0; goodix_chip_ids[i].id; i++) {
		if (!strcmp(goodix_chip_ids[i].id, id))
			return goodix_chip_ids[i].data;
237
	}
238 239

	return &gt9x_chip_data;
240 241
}

242 243
static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
{
244
	unsigned long max_timeout;
245 246
	int touch_num;
	int error;
247 248 249 250 251 252 253
	u16 addr = GOODIX_READ_COOR_ADDR;
	/*
	 * We are going to read 1-byte header,
	 * ts->contact_size * max(1, touch_num) bytes of coordinates
	 * and 1-byte footer which contains the touch-key code.
	 */
	const int header_contact_keycode_size = 1 + ts->contact_size + 1;
254

255 256 257 258 259 260 261
	/*
	 * The 'buffer status' bit, which indicates that the data is valid, is
	 * not set as soon as the interrupt is raised, but slightly after.
	 * This takes around 10 ms to happen, so we poll for 20 ms.
	 */
	max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT);
	do {
262 263
		error = goodix_i2c_read(ts->client, addr, data,
					header_contact_keycode_size);
264
		if (error)
265
			return error;
266 267 268 269 270 271 272

		if (data[0] & GOODIX_BUFFER_STATUS_READY) {
			touch_num = data[0] & 0x0f;
			if (touch_num > ts->max_touch_num)
				return -EPROTO;

			if (touch_num > 1) {
273 274
				addr += header_contact_keycode_size;
				data += header_contact_keycode_size;
275
				error = goodix_i2c_read(ts->client,
276
						addr, data,
277
						ts->contact_size *
278 279 280 281 282 283 284 285
							(touch_num - 1));
				if (error)
					return error;
			}

			return touch_num;
		}

286 287 288 289 290
		if (data[0] == 0 && ts->firmware_name) {
			if (goodix_handle_fw_request(ts))
				return 0;
		}

291 292
		usleep_range(1000, 2000); /* Poll every 1 - 2 ms */
	} while (time_before(jiffies, max_timeout));
293

294 295 296 297
	/*
	 * The Goodix panel will send spurious interrupts after a
	 * 'finger up' event, which will always cause a timeout.
	 */
298
	return -ENOMSG;
299 300
}

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
static struct input_dev *goodix_create_pen_input(struct goodix_ts_data *ts)
{
	struct device *dev = &ts->client->dev;
	struct input_dev *input;

	input = devm_input_allocate_device(dev);
	if (!input)
		return NULL;

	input_alloc_absinfo(input);
	if (!input->absinfo) {
		input_free_device(input);
		return NULL;
	}

	input->absinfo[ABS_X] = ts->input_dev->absinfo[ABS_MT_POSITION_X];
	input->absinfo[ABS_Y] = ts->input_dev->absinfo[ABS_MT_POSITION_Y];
	__set_bit(ABS_X, input->absbit);
	__set_bit(ABS_Y, input->absbit);
	input_set_abs_params(input, ABS_PRESSURE, 0, 255, 0, 0);

	input_set_capability(input, EV_KEY, BTN_TOUCH);
	input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
	input_set_capability(input, EV_KEY, BTN_STYLUS);
	input_set_capability(input, EV_KEY, BTN_STYLUS2);
	__set_bit(INPUT_PROP_DIRECT, input->propbit);
	/*
	 * The resolution of these touchscreens is about 10 units/mm, the actual
	 * resolution does not matter much since we set INPUT_PROP_DIRECT.
	 * Userspace wants something here though, so just set it to 10 units/mm.
	 */
	input_abs_set_res(input, ABS_X, 10);
	input_abs_set_res(input, ABS_Y, 10);

	input->name = "Goodix Active Pen";
	input->phys = "input/pen";
	input->id.bustype = BUS_I2C;
338
	input->id.vendor = 0x0416;
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
	if (kstrtou16(ts->id, 10, &input->id.product))
		input->id.product = 0x1001;
	input->id.version = ts->version;

	if (input_register_device(input) != 0) {
		input_free_device(input);
		return NULL;
	}

	return input;
}

static void goodix_ts_report_pen_down(struct goodix_ts_data *ts, u8 *data)
{
	int input_x, input_y, input_w;
	u8 key_value;

	if (!ts->input_pen) {
		ts->input_pen = goodix_create_pen_input(ts);
		if (!ts->input_pen)
			return;
	}

	if (ts->contact_size == 9) {
		input_x = get_unaligned_le16(&data[4]);
		input_y = get_unaligned_le16(&data[6]);
		input_w = get_unaligned_le16(&data[8]);
	} else {
		input_x = get_unaligned_le16(&data[2]);
		input_y = get_unaligned_le16(&data[4]);
		input_w = get_unaligned_le16(&data[6]);
	}

	touchscreen_report_pos(ts->input_pen, &ts->prop, input_x, input_y, false);
	input_report_abs(ts->input_pen, ABS_PRESSURE, input_w);

	input_report_key(ts->input_pen, BTN_TOUCH, 1);
	input_report_key(ts->input_pen, BTN_TOOL_PEN, 1);

	if (data[0] & GOODIX_HAVE_KEY) {
		key_value = data[1 + ts->contact_size];
		input_report_key(ts->input_pen, BTN_STYLUS, key_value & 0x10);
		input_report_key(ts->input_pen, BTN_STYLUS2, key_value & 0x20);
	} else {
		input_report_key(ts->input_pen, BTN_STYLUS, 0);
		input_report_key(ts->input_pen, BTN_STYLUS2, 0);
	}

	input_sync(ts->input_pen);
}

static void goodix_ts_report_pen_up(struct goodix_ts_data *ts)
{
	if (!ts->input_pen)
		return;

	input_report_key(ts->input_pen, BTN_TOUCH, 0);
	input_report_key(ts->input_pen, BTN_TOOL_PEN, 0);
	input_report_key(ts->input_pen, BTN_STYLUS, 0);
	input_report_key(ts->input_pen, BTN_STYLUS2, 0);

	input_sync(ts->input_pen);
}

403
static void goodix_ts_report_touch_8b(struct goodix_ts_data *ts, u8 *coor_data)
404 405 406 407 408 409 410 411
{
	int id = coor_data[0] & 0x0F;
	int input_x = get_unaligned_le16(&coor_data[1]);
	int input_y = get_unaligned_le16(&coor_data[3]);
	int input_w = get_unaligned_le16(&coor_data[5]);

	input_mt_slot(ts->input_dev, id);
	input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
412 413
	touchscreen_report_pos(ts->input_dev, &ts->prop,
			       input_x, input_y, true);
414 415 416 417
	input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
	input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
}

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
static void goodix_ts_report_touch_9b(struct goodix_ts_data *ts, u8 *coor_data)
{
	int id = coor_data[1] & 0x0F;
	int input_x = get_unaligned_le16(&coor_data[3]);
	int input_y = get_unaligned_le16(&coor_data[5]);
	int input_w = get_unaligned_le16(&coor_data[7]);

	input_mt_slot(ts->input_dev, id);
	input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
	touchscreen_report_pos(ts->input_dev, &ts->prop,
			       input_x, input_y, true);
	input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
	input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
}

433 434 435 436 437 438 439 440
static void goodix_ts_release_keys(struct goodix_ts_data *ts)
{
	int i;

	for (i = 0; i < GOODIX_MAX_KEYS; i++)
		input_report_key(ts->input_dev, ts->keymap[i], 0);
}

441 442 443 444 445 446 447 448 449 450 451 452 453 454
static void goodix_ts_report_key(struct goodix_ts_data *ts, u8 *data)
{
	int touch_num;
	u8 key_value;
	int i;

	if (data[0] & GOODIX_HAVE_KEY) {
		touch_num = data[0] & 0x0f;
		key_value = data[1 + ts->contact_size * touch_num];
		for (i = 0; i < GOODIX_MAX_KEYS; i++)
			if (key_value & BIT(i))
				input_report_key(ts->input_dev,
						 ts->keymap[i], 1);
	} else {
455
		goodix_ts_release_keys(ts);
456 457 458
	}
}

459 460 461 462 463 464 465 466 467 468
/**
 * goodix_process_events - Process incoming events
 *
 * @ts: our goodix_ts_data pointer
 *
 * Called when the IRQ is triggered. Read the current device state, and push
 * the input events to the user space.
 */
static void goodix_process_events(struct goodix_ts_data *ts)
{
469
	u8  point_data[2 + GOODIX_MAX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
470 471 472 473 474 475 476
	int touch_num;
	int i;

	touch_num = goodix_ts_read_input_report(ts, point_data);
	if (touch_num < 0)
		return;

477 478 479 480
	/* The pen being down is always reported as a single touch */
	if (touch_num == 1 && (point_data[1] & 0x80)) {
		goodix_ts_report_pen_down(ts, point_data);
		goodix_ts_release_keys(ts);
481
		goto sync; /* Release any previously registered touches */
482 483 484 485
	} else {
		goodix_ts_report_pen_up(ts);
	}

486
	goodix_ts_report_key(ts, point_data);
487

488
	for (i = 0; i < touch_num; i++)
489 490 491 492 493 494
		if (ts->contact_size == 9)
			goodix_ts_report_touch_9b(ts,
				&point_data[1 + ts->contact_size * i]);
		else
			goodix_ts_report_touch_8b(ts,
				&point_data[1 + ts->contact_size * i]);
495

496
sync:
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
	input_mt_sync_frame(ts->input_dev);
	input_sync(ts->input_dev);
}

/**
 * goodix_ts_irq_handler - The IRQ handler
 *
 * @irq: interrupt number.
 * @dev_id: private data pointer.
 */
static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
{
	struct goodix_ts_data *ts = dev_id;

	goodix_process_events(ts);
512
	goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0);
513 514 515 516

	return IRQ_HANDLED;
}

517 518 519 520 521 522 523 524 525 526 527 528
static void goodix_free_irq(struct goodix_ts_data *ts)
{
	devm_free_irq(&ts->client->dev, ts->client->irq, ts);
}

static int goodix_request_irq(struct goodix_ts_data *ts)
{
	return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
					 NULL, goodix_ts_irq_handler,
					 ts->irq_flags, ts->client->name, ts);
}

529
static int goodix_check_cfg_8(struct goodix_ts_data *ts, const u8 *cfg, int len)
530
{
531
	int i, raw_cfg_len = len - 2;
532 533 534
	u8 check_sum = 0;

	for (i = 0; i < raw_cfg_len; i++)
535
		check_sum += cfg[i];
536
	check_sum = (~check_sum) + 1;
537
	if (check_sum != cfg[raw_cfg_len]) {
538 539 540 541 542
		dev_err(&ts->client->dev,
			"The checksum of the config fw is not correct");
		return -EINVAL;
	}

543
	if (cfg[raw_cfg_len + 1] != 1) {
544 545 546 547 548 549 550 551
		dev_err(&ts->client->dev,
			"Config fw must have Config_Fresh register set");
		return -EINVAL;
	}

	return 0;
}

552 553 554 555 556 557 558 559 560 561 562 563 564
static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts)
{
	int i, raw_cfg_len = ts->chip->config_len - 2;
	u8 check_sum = 0;

	for (i = 0; i < raw_cfg_len; i++)
		check_sum += ts->config[i];
	check_sum = (~check_sum) + 1;

	ts->config[raw_cfg_len] = check_sum;
	ts->config[raw_cfg_len + 1] = 1; /* Set "config_fresh" bit */
}

565 566
static int goodix_check_cfg_16(struct goodix_ts_data *ts, const u8 *cfg,
			       int len)
567
{
568
	int i, raw_cfg_len = len - 3;
569 570 571
	u16 check_sum = 0;

	for (i = 0; i < raw_cfg_len; i += 2)
572
		check_sum += get_unaligned_be16(&cfg[i]);
573
	check_sum = (~check_sum) + 1;
574
	if (check_sum != get_unaligned_be16(&cfg[raw_cfg_len])) {
575 576 577 578 579
		dev_err(&ts->client->dev,
			"The checksum of the config fw is not correct");
		return -EINVAL;
	}

580
	if (cfg[raw_cfg_len + 2] != 1) {
581 582 583 584 585 586 587 588
		dev_err(&ts->client->dev,
			"Config fw must have Config_Fresh register set");
		return -EINVAL;
	}

	return 0;
}

589 590 591 592 593 594 595 596 597 598 599 600 601
static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts)
{
	int i, raw_cfg_len = ts->chip->config_len - 3;
	u16 check_sum = 0;

	for (i = 0; i < raw_cfg_len; i += 2)
		check_sum += get_unaligned_be16(&ts->config[i]);
	check_sum = (~check_sum) + 1;

	put_unaligned_be16(check_sum, &ts->config[raw_cfg_len]);
	ts->config[raw_cfg_len + 2] = 1; /* Set "config_fresh" bit */
}

602 603 604 605 606
/**
 * goodix_check_cfg - Checks if config fw is valid
 *
 * @ts: goodix_ts_data pointer
 * @cfg: firmware config data
607
 * @len: config data length
608
 */
609
static int goodix_check_cfg(struct goodix_ts_data *ts, const u8 *cfg, int len)
610
{
611 612
	if (len < GOODIX_CONFIG_MIN_LENGTH ||
	    len > GOODIX_CONFIG_MAX_LENGTH) {
613 614 615 616 617
		dev_err(&ts->client->dev,
			"The length of the config fw is not correct");
		return -EINVAL;
	}

618
	return ts->chip->check_config(ts, cfg, len);
619 620
}

621 622 623 624 625
/**
 * goodix_send_cfg - Write fw config to device
 *
 * @ts: goodix_ts_data pointer
 * @cfg: config firmware to write to device
626
 * @len: config data length
627
 */
628
int goodix_send_cfg(struct goodix_ts_data *ts, const u8 *cfg, int len)
629 630 631
{
	int error;

632
	error = goodix_check_cfg(ts, cfg, len);
633 634 635
	if (error)
		return error;

636
	error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg, len);
637
	if (error)
638
		return error;
639

640 641 642 643 644 645 646 647
	dev_dbg(&ts->client->dev, "Config sent successfully.");

	/* Let the firmware reconfigure itself, so sleep for 10ms */
	usleep_range(10000, 11000);

	return 0;
}

648 649
#ifdef ACPI_GPIO_SUPPORT
static int goodix_pin_acpi_direction_input(struct goodix_ts_data *ts)
650
{
651
	acpi_handle handle = ACPI_HANDLE(&ts->client->dev);
652 653
	acpi_status status;

654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
	status = acpi_evaluate_object(handle, "INTI", NULL, NULL);
	return ACPI_SUCCESS(status) ? 0 : -EIO;
}

static int goodix_pin_acpi_output_method(struct goodix_ts_data *ts, int value)
{
	acpi_handle handle = ACPI_HANDLE(&ts->client->dev);
	acpi_status status;

	status = acpi_execute_simple_method(handle, "INTO", value);
	return ACPI_SUCCESS(status) ? 0 : -EIO;
}
#else
static int goodix_pin_acpi_direction_input(struct goodix_ts_data *ts)
{
	dev_err(&ts->client->dev,
		"%s called on device without ACPI support\n", __func__);
	return -EINVAL;
}

static int goodix_pin_acpi_output_method(struct goodix_ts_data *ts, int value)
{
	dev_err(&ts->client->dev,
		"%s called on device without ACPI support\n", __func__);
	return -EINVAL;
}
#endif

static int goodix_irq_direction_output(struct goodix_ts_data *ts, int value)
{
684 685 686 687 688 689 690 691
	switch (ts->irq_pin_access_method) {
	case IRQ_PIN_ACCESS_NONE:
		dev_err(&ts->client->dev,
			"%s called without an irq_pin_access_method set\n",
			__func__);
		return -EINVAL;
	case IRQ_PIN_ACCESS_GPIO:
		return gpiod_direction_output(ts->gpiod_int, value);
692 693 694 695 696 697
	case IRQ_PIN_ACCESS_ACPI_GPIO:
		/*
		 * The IRQ pin triggers on a falling edge, so its gets marked
		 * as active-low, use output_raw to avoid the value inversion.
		 */
		return gpiod_direction_output_raw(ts->gpiod_int, value);
698
	case IRQ_PIN_ACCESS_ACPI_METHOD:
699
		return goodix_pin_acpi_output_method(ts, value);
700 701 702 703 704 705 706 707 708 709 710 711 712 713
	}

	return -EINVAL; /* Never reached */
}

static int goodix_irq_direction_input(struct goodix_ts_data *ts)
{
	switch (ts->irq_pin_access_method) {
	case IRQ_PIN_ACCESS_NONE:
		dev_err(&ts->client->dev,
			"%s called without an irq_pin_access_method set\n",
			__func__);
		return -EINVAL;
	case IRQ_PIN_ACCESS_GPIO:
714
		return gpiod_direction_input(ts->gpiod_int);
715
	case IRQ_PIN_ACCESS_ACPI_GPIO:
716
		return gpiod_direction_input(ts->gpiod_int);
717
	case IRQ_PIN_ACCESS_ACPI_METHOD:
718
		return goodix_pin_acpi_direction_input(ts);
719 720 721 722 723
	}

	return -EINVAL; /* Never reached */
}

724
int goodix_int_sync(struct goodix_ts_data *ts)
725 726 727
{
	int error;

728
	error = goodix_irq_direction_output(ts, 0);
729
	if (error)
730
		goto error;
731 732 733

	msleep(50);				/* T5: 50ms */

734
	error = goodix_irq_direction_input(ts);
735
	if (error)
736
		goto error;
737 738

	return 0;
739 740 741 742

error:
	dev_err(&ts->client->dev, "Controller irq sync failed.\n");
	return error;
743 744 745
}

/**
746
 * goodix_reset_no_int_sync - Reset device, leaving interrupt line in output mode
747 748 749
 *
 * @ts: goodix_ts_data pointer
 */
750
int goodix_reset_no_int_sync(struct goodix_ts_data *ts)
751 752 753 754 755 756
{
	int error;

	/* begin select I2C slave addr */
	error = gpiod_direction_output(ts->gpiod_rst, 0);
	if (error)
757
		goto error;
758 759 760 761

	msleep(20);				/* T2: > 10ms */

	/* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
762
	error = goodix_irq_direction_output(ts, ts->client->addr == 0x14);
763
	if (error)
764
		goto error;
765 766 767 768 769

	usleep_range(100, 2000);		/* T3: > 100us */

	error = gpiod_direction_output(ts->gpiod_rst, 1);
	if (error)
770
		goto error;
771 772 773

	usleep_range(6000, 10000);		/* T4: > 5ms */

774 775 776 777 778 779 780 781 782 783
	/*
	 * Put the reset pin back in to input / high-impedance mode to save
	 * power. Only do this in the non ACPI case since some ACPI boards
	 * don't have a pull-up, so there the reset pin must stay active-high.
	 */
	if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_GPIO) {
		error = gpiod_direction_input(ts->gpiod_rst);
		if (error)
			goto error;
	}
784

785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
	return 0;

error:
	dev_err(&ts->client->dev, "Controller reset failed.\n");
	return error;
}

/**
 * goodix_reset - Reset device during power on
 *
 * @ts: goodix_ts_data pointer
 */
static int goodix_reset(struct goodix_ts_data *ts)
{
	int error;

	error = goodix_reset_no_int_sync(ts);
802 803 804
	if (error)
		return error;

805
	return goodix_int_sync(ts);
806 807
}

808
#ifdef ACPI_GPIO_SUPPORT
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
static const struct acpi_gpio_params first_gpio = { 0, 0, false };
static const struct acpi_gpio_params second_gpio = { 1, 0, false };

static const struct acpi_gpio_mapping acpi_goodix_int_first_gpios[] = {
	{ GOODIX_GPIO_INT_NAME "-gpios", &first_gpio, 1 },
	{ GOODIX_GPIO_RST_NAME "-gpios", &second_gpio, 1 },
	{ },
};

static const struct acpi_gpio_mapping acpi_goodix_int_last_gpios[] = {
	{ GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
	{ GOODIX_GPIO_INT_NAME "-gpios", &second_gpio, 1 },
	{ },
};

824 825 826 827 828
static const struct acpi_gpio_mapping acpi_goodix_reset_only_gpios[] = {
	{ GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
	{ },
};

829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
static int goodix_resource(struct acpi_resource *ares, void *data)
{
	struct goodix_ts_data *ts = data;
	struct device *dev = &ts->client->dev;
	struct acpi_resource_gpio *gpio;

	switch (ares->type) {
	case ACPI_RESOURCE_TYPE_GPIO:
		gpio = &ares->data.gpio;
		if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT) {
			if (ts->gpio_int_idx == -1) {
				ts->gpio_int_idx = ts->gpio_count;
			} else {
				dev_err(dev, "More then one GpioInt resource, ignoring ACPI GPIO resources\n");
				ts->gpio_int_idx = -2;
			}
		}
		ts->gpio_count++;
		break;
	default:
		break;
	}

	return 0;
}

/*
 * This function gets called in case we fail to get the irq GPIO directly
 * because the ACPI tables lack GPIO-name to APCI _CRS index mappings
 * (no _DSD UUID daffd814-6eba-4d8c-8a91-bc9bbf4aa301 data).
 * In that case we add our own mapping and then goodix_get_gpio_config()
 * retries to get the GPIOs based on the added mapping.
 */
static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
{
	const struct acpi_gpio_mapping *gpio_mapping = NULL;
	struct device *dev = &ts->client->dev;
	LIST_HEAD(resources);
867
	int irq, ret;
868 869 870 871 872 873 874 875 876 877 878 879

	ts->gpio_count = 0;
	ts->gpio_int_idx = -1;
	ret = acpi_dev_get_resources(ACPI_COMPANION(dev), &resources,
				     goodix_resource, ts);
	if (ret < 0) {
		dev_err(dev, "Error getting ACPI resources: %d\n", ret);
		return ret;
	}

	acpi_dev_free_resource_list(&resources);

880 881 882 883 884 885 886 887 888 889 890 891 892 893
	/*
	 * CHT devices should have a GpioInt + a regular GPIO ACPI resource.
	 * Some CHT devices have a bug (where the also is bogus Interrupt
	 * resource copied from a previous BYT based generation). i2c-core-acpi
	 * will use the non-working Interrupt resource, fix this up.
	 */
	if (soc_intel_is_cht() && ts->gpio_count == 2 && ts->gpio_int_idx != -1) {
		irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
		if (irq > 0 && irq != ts->client->irq) {
			dev_warn(dev, "Overriding IRQ %d -> %d\n", ts->client->irq, irq);
			ts->client->irq = irq;
		}
	}

894 895 896 897 898 899
	if (ts->gpio_count == 2 && ts->gpio_int_idx == 0) {
		ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
		gpio_mapping = acpi_goodix_int_first_gpios;
	} else if (ts->gpio_count == 2 && ts->gpio_int_idx == 1) {
		ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
		gpio_mapping = acpi_goodix_int_last_gpios;
900 901 902 903 904 905
	} else if (ts->gpio_count == 1 && ts->gpio_int_idx == -1 &&
		   acpi_has_method(ACPI_HANDLE(dev), "INTI") &&
		   acpi_has_method(ACPI_HANDLE(dev), "INTO")) {
		dev_info(dev, "Using ACPI INTI and INTO methods for IRQ pin access\n");
		ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_METHOD;
		gpio_mapping = acpi_goodix_reset_only_gpios;
906
	} else if (soc_intel_is_byt() && ts->gpio_count == 2 && ts->gpio_int_idx == -1) {
907 908 909
		dev_info(dev, "No ACPI GpioInt resource, assuming that the GPIO order is reset, int\n");
		ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
		gpio_mapping = acpi_goodix_int_last_gpios;
910 911 912 913 914 915
	} else {
		dev_warn(dev, "Unexpected ACPI resources: gpio_count %d, gpio_int_idx %d\n",
			 ts->gpio_count, ts->gpio_int_idx);
		return -EINVAL;
	}

916 917 918 919 920 921 922 923
	/*
	 * Normally we put the reset pin in input / high-impedance mode to save
	 * power. But some x86/ACPI boards don't have a pull-up, so for the ACPI
	 * case, leave the pin as is. This results in the pin not being touched
	 * at all on x86/ACPI boards, except when needed for error-recover.
	 */
	ts->gpiod_rst_flags = GPIOD_ASIS;

924 925 926 927 928 929 930 931 932
	return devm_acpi_dev_add_driver_gpios(dev, gpio_mapping);
}
#else
static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
{
	return -EINVAL;
}
#endif /* CONFIG_X86 && CONFIG_ACPI */

933 934 935 936 937 938 939 940 941 942
/**
 * goodix_get_gpio_config - Get GPIO config from ACPI/DT
 *
 * @ts: goodix_ts_data pointer
 */
static int goodix_get_gpio_config(struct goodix_ts_data *ts)
{
	int error;
	struct device *dev;
	struct gpio_desc *gpiod;
943
	bool added_acpi_mappings = false;
944 945 946 947 948

	if (!ts->client)
		return -EINVAL;
	dev = &ts->client->dev;

949 950 951 952 953 954
	/*
	 * By default we request the reset pin as input, leaving it in
	 * high-impedance when not resetting the controller to save power.
	 */
	ts->gpiod_rst_flags = GPIOD_IN;

955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
	ts->avdd28 = devm_regulator_get(dev, "AVDD28");
	if (IS_ERR(ts->avdd28)) {
		error = PTR_ERR(ts->avdd28);
		if (error != -EPROBE_DEFER)
			dev_err(dev,
				"Failed to get AVDD28 regulator: %d\n", error);
		return error;
	}

	ts->vddio = devm_regulator_get(dev, "VDDIO");
	if (IS_ERR(ts->vddio)) {
		error = PTR_ERR(ts->vddio);
		if (error != -EPROBE_DEFER)
			dev_err(dev,
				"Failed to get VDDIO regulator: %d\n", error);
		return error;
	}

973
retry_get_irq_gpio:
974 975 976 977 978
	/* Get the interrupt GPIO pin number */
	gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
	if (IS_ERR(gpiod)) {
		error = PTR_ERR(gpiod);
		if (error != -EPROBE_DEFER)
979
			dev_err(dev, "Failed to get %s GPIO: %d\n",
980 981 982
				GOODIX_GPIO_INT_NAME, error);
		return error;
	}
983 984 985 986 987
	if (!gpiod && has_acpi_companion(dev) && !added_acpi_mappings) {
		added_acpi_mappings = true;
		if (goodix_add_acpi_gpio_mappings(ts) == 0)
			goto retry_get_irq_gpio;
	}
988 989 990 991

	ts->gpiod_int = gpiod;

	/* Get the reset line GPIO pin number */
992
	gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, ts->gpiod_rst_flags);
993 994 995
	if (IS_ERR(gpiod)) {
		error = PTR_ERR(gpiod);
		if (error != -EPROBE_DEFER)
996
			dev_err(dev, "Failed to get %s GPIO: %d\n",
997 998 999 1000 1001 1002
				GOODIX_GPIO_RST_NAME, error);
		return error;
	}

	ts->gpiod_rst = gpiod;

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
	switch (ts->irq_pin_access_method) {
	case IRQ_PIN_ACCESS_ACPI_GPIO:
		/*
		 * We end up here if goodix_add_acpi_gpio_mappings() has
		 * called devm_acpi_dev_add_driver_gpios() because the ACPI
		 * tables did not contain name to index mappings.
		 * Check that we successfully got both GPIOs after we've
		 * added our own acpi_gpio_mapping and if we did not get both
		 * GPIOs reset irq_pin_access_method to IRQ_PIN_ACCESS_NONE.
		 */
		if (!ts->gpiod_int || !ts->gpiod_rst)
			ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
		break;
1016 1017 1018 1019
	case IRQ_PIN_ACCESS_ACPI_METHOD:
		if (!ts->gpiod_rst)
			ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
		break;
1020 1021 1022 1023 1024 1025
	default:
		if (ts->gpiod_int && ts->gpiod_rst) {
			ts->reset_controller_at_probe = true;
			ts->load_cfg_from_disk = true;
			ts->irq_pin_access_method = IRQ_PIN_ACCESS_GPIO;
		}
1026
	}
1027

1028 1029 1030
	return 0;
}

1031 1032 1033 1034 1035 1036 1037 1038 1039
/**
 * goodix_read_config - Read the embedded configuration of the panel
 *
 * @ts: our goodix_ts_data pointer
 *
 * Must be called during probe
 */
static void goodix_read_config(struct goodix_ts_data *ts)
{
1040
	int x_max, y_max;
1041 1042
	int error;

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
	/*
	 * On controllers where we need to upload the firmware
	 * (controllers without flash) ts->config already has the config
	 * at this point and the controller itself does not have it yet!
	 */
	if (!ts->firmware_name) {
		error = goodix_i2c_read(ts->client, ts->chip->config_addr,
					ts->config, ts->chip->config_len);
		if (error) {
			ts->int_trigger_type = GOODIX_INT_TRIGGER;
			ts->max_touch_num = GOODIX_MAX_CONTACTS;
			return;
		}
1056 1057
	}

1058 1059
	ts->int_trigger_type = ts->config[TRIGGER_LOC] & 0x03;
	ts->max_touch_num = ts->config[MAX_CONTACTS_LOC] & 0x0f;
1060

1061 1062
	x_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC]);
	y_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC + 2]);
1063 1064 1065
	if (x_max && y_max) {
		input_abs_set_max(ts->input_dev, ABS_MT_POSITION_X, x_max - 1);
		input_abs_set_max(ts->input_dev, ABS_MT_POSITION_Y, y_max - 1);
1066
	}
1067 1068

	ts->chip->calc_config_checksum(ts);
1069 1070 1071 1072 1073
}

/**
 * goodix_read_version - Read goodix touchscreen version
 *
1074
 * @ts: our goodix_ts_data pointer
1075
 */
1076
static int goodix_read_version(struct goodix_ts_data *ts)
1077 1078 1079
{
	int error;
	u8 buf[6];
1080
	char id_str[GOODIX_ID_MAX_LEN + 1];
1081

1082
	error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
1083
	if (error)
1084 1085
		return error;

1086 1087 1088
	memcpy(id_str, buf, GOODIX_ID_MAX_LEN);
	id_str[GOODIX_ID_MAX_LEN] = 0;
	strscpy(ts->id, id_str, GOODIX_ID_MAX_LEN + 1);
1089

1090
	ts->version = get_unaligned_le16(&buf[4]);
1091

1092
	dev_info(&ts->client->dev, "ID %s, version: %04x\n", ts->id,
1093
		 ts->version);
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109

	return 0;
}

/**
 * goodix_i2c_test - I2C test function to check if the device answers.
 *
 * @client: the i2c client
 */
static int goodix_i2c_test(struct i2c_client *client)
{
	int retry = 0;
	int error;
	u8 test;

	while (retry++ < 2) {
1110
		error = goodix_i2c_read(client, GOODIX_REG_ID, &test, 1);
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
		if (!error)
			return 0;

		msleep(20);
	}

	return error;
}

/**
1121
 * goodix_configure_dev - Finish device initialization
1122 1123 1124
 *
 * @ts: our goodix_ts_data pointer
 *
1125 1126 1127 1128
 * Must be called from probe to finish initialization of the device.
 * Contains the common initialization code for both devices that
 * declare gpio pins and devices that do not. It is either called
 * directly from probe or from request_firmware_wait callback.
1129
 */
1130
static int goodix_configure_dev(struct goodix_ts_data *ts)
1131 1132
{
	int error;
1133
	int i;
1134

1135 1136 1137
	ts->int_trigger_type = GOODIX_INT_TRIGGER;
	ts->max_touch_num = GOODIX_MAX_CONTACTS;

1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
	ts->input_dev = devm_input_allocate_device(&ts->client->dev);
	if (!ts->input_dev) {
		dev_err(&ts->client->dev, "Failed to allocate input device.");
		return -ENOMEM;
	}

	ts->input_dev->name = "Goodix Capacitive TouchScreen";
	ts->input_dev->phys = "input/ts";
	ts->input_dev->id.bustype = BUS_I2C;
	ts->input_dev->id.vendor = 0x0416;
1148 1149
	if (kstrtou16(ts->id, 10, &ts->input_dev->id.product))
		ts->input_dev->id.product = 0x1001;
1150
	ts->input_dev->id.version = ts->version;
1151

1152 1153 1154 1155
	ts->input_dev->keycode = ts->keymap;
	ts->input_dev->keycodesize = sizeof(ts->keymap[0]);
	ts->input_dev->keycodemax = GOODIX_MAX_KEYS;

1156
	/* Capacitive Windows/Home button on some devices */
1157 1158 1159 1160 1161 1162 1163 1164
	for (i = 0; i < GOODIX_MAX_KEYS; ++i) {
		if (i == 0)
			ts->keymap[i] = KEY_LEFTMETA;
		else
			ts->keymap[i] = KEY_F1 + (i - 1);

		input_set_capability(ts->input_dev, EV_KEY, ts->keymap[i]);
	}
1165

1166 1167 1168 1169
	input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
	input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
	input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
	input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1170

1171 1172
	/* Read configuration and apply touchscreen parameters */
	goodix_read_config(ts);
1173

1174 1175
	/* Try overriding touchscreen parameters via device properties */
	touchscreen_parse_properties(ts->input_dev, true, &ts->prop);
1176

1177
	if (!ts->prop.max_x || !ts->prop.max_y || !ts->max_touch_num) {
1178 1179 1180
		dev_err(&ts->client->dev,
			"Invalid config (%d, %d, %d), using defaults\n",
			ts->prop.max_x, ts->prop.max_y, ts->max_touch_num);
1181 1182 1183 1184 1185 1186 1187 1188
		ts->prop.max_x = GOODIX_MAX_WIDTH - 1;
		ts->prop.max_y = GOODIX_MAX_HEIGHT - 1;
		ts->max_touch_num = GOODIX_MAX_CONTACTS;
		input_abs_set_max(ts->input_dev,
				  ABS_MT_POSITION_X, ts->prop.max_x);
		input_abs_set_max(ts->input_dev,
				  ABS_MT_POSITION_Y, ts->prop.max_y);
	}
1189

1190 1191 1192 1193 1194 1195 1196
	if (dmi_check_system(nine_bytes_report)) {
		ts->contact_size = 9;

		dev_dbg(&ts->client->dev,
			"Non-standard 9-bytes report format quirk\n");
	}

1197 1198 1199 1200 1201 1202
	if (dmi_check_system(inverted_x_screen)) {
		ts->prop.invert_x = true;
		dev_dbg(&ts->client->dev,
			"Applying 'inverted x screen' quirk\n");
	}

1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
	error = input_mt_init_slots(ts->input_dev, ts->max_touch_num,
				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
	if (error) {
		dev_err(&ts->client->dev,
			"Failed to initialize MT slots: %d", error);
		return error;
	}

	error = input_register_device(ts->input_dev);
	if (error) {
		dev_err(&ts->client->dev,
			"Failed to register input device: %d", error);
1215
		return error;
1216
	}
1217

1218 1219
	ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
	error = goodix_request_irq(ts);
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
	if (error) {
		dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
		return error;
	}

	return 0;
}

/**
 * goodix_config_cb - Callback to finish device init
 *
1231
 * @cfg: firmware config
1232
 * @ctx: our goodix_ts_data pointer
1233 1234 1235 1236 1237 1238 1239 1240 1241
 *
 * request_firmware_wait callback that finishes
 * initialization of the device.
 */
static void goodix_config_cb(const struct firmware *cfg, void *ctx)
{
	struct goodix_ts_data *ts = ctx;
	int error;

1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
	if (ts->firmware_name) {
		if (!cfg)
			goto err_release_cfg;

		error = goodix_check_cfg(ts, cfg->data, cfg->size);
		if (error)
			goto err_release_cfg;

		memcpy(ts->config, cfg->data, cfg->size);
	} else if (cfg) {
1252
		/* send device configuration to the firmware */
1253
		error = goodix_send_cfg(ts, cfg->data, cfg->size);
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
		if (error)
			goto err_release_cfg;
	}

	goodix_configure_dev(ts);

err_release_cfg:
	release_firmware(cfg);
	complete_all(&ts->firmware_loading_complete);
}

1265 1266 1267 1268 1269 1270 1271 1272
static void goodix_disable_regulators(void *arg)
{
	struct goodix_ts_data *ts = arg;

	regulator_disable(ts->vddio);
	regulator_disable(ts->avdd28);
}

1273 1274 1275 1276
static int goodix_ts_probe(struct i2c_client *client,
			   const struct i2c_device_id *id)
{
	struct goodix_ts_data *ts;
1277
	const char *cfg_name;
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
	int error;

	dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);

	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
		dev_err(&client->dev, "I2C check functionality failed.\n");
		return -ENXIO;
	}

	ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
	if (!ts)
		return -ENOMEM;

	ts->client = client;
	i2c_set_clientdata(client, ts);
1293
	init_completion(&ts->firmware_loading_complete);
1294
	ts->contact_size = GOODIX_CONTACT_SIZE;
1295

1296 1297 1298 1299
	error = goodix_get_gpio_config(ts);
	if (error)
		return error;

1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
	/* power up the controller */
	error = regulator_enable(ts->avdd28);
	if (error) {
		dev_err(&client->dev,
			"Failed to enable AVDD28 regulator: %d\n",
			error);
		return error;
	}

	error = regulator_enable(ts->vddio);
	if (error) {
		dev_err(&client->dev,
			"Failed to enable VDDIO regulator: %d\n",
			error);
		regulator_disable(ts->avdd28);
		return error;
	}

	error = devm_add_action_or_reset(&client->dev,
					 goodix_disable_regulators, ts);
	if (error)
		return error;

1323
reset:
1324
	if (ts->reset_controller_at_probe) {
1325 1326
		/* reset the controller */
		error = goodix_reset(ts);
1327
		if (error)
1328 1329 1330
			return error;
	}

1331 1332
	error = goodix_i2c_test(client);
	if (error) {
1333 1334 1335 1336 1337 1338
		if (!ts->reset_controller_at_probe &&
		    ts->irq_pin_access_method != IRQ_PIN_ACCESS_NONE) {
			/* Retry after a controller reset */
			ts->reset_controller_at_probe = true;
			goto reset;
		}
1339 1340 1341 1342
		dev_err(&client->dev, "I2C communication failure: %d\n", error);
		return error;
	}

1343 1344 1345 1346
	error = goodix_firmware_check(ts);
	if (error)
		return error;

1347
	error = goodix_read_version(ts);
1348
	if (error)
1349 1350
		return error;

1351
	ts->chip = goodix_get_chip_data(ts->id);
1352

1353
	if (ts->load_cfg_from_disk) {
1354
		/* update device config */
1355 1356 1357 1358 1359 1360 1361 1362 1363
		error = device_property_read_string(&client->dev,
						    "goodix,config-name",
						    &cfg_name);
		if (!error)
			snprintf(ts->cfg_name, sizeof(ts->cfg_name),
				 "goodix/%s", cfg_name);
		else
			snprintf(ts->cfg_name, sizeof(ts->cfg_name),
				 "goodix_%s_cfg.bin", ts->id);
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373

		error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
						&client->dev, GFP_KERNEL, ts,
						goodix_config_cb);
		if (error) {
			dev_err(&client->dev,
				"Failed to invoke firmware loader: %d\n",
				error);
			return error;
		}
1374

1375 1376 1377 1378 1379
		return 0;
	} else {
		error = goodix_configure_dev(ts);
		if (error)
			return error;
1380 1381 1382 1383 1384
	}

	return 0;
}

1385 1386 1387 1388
static int goodix_ts_remove(struct i2c_client *client)
{
	struct goodix_ts_data *ts = i2c_get_clientdata(client);

1389
	if (ts->load_cfg_from_disk)
1390 1391 1392 1393 1394
		wait_for_completion(&ts->firmware_loading_complete);

	return 0;
}

1395 1396 1397 1398 1399 1400
static int __maybe_unused goodix_suspend(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct goodix_ts_data *ts = i2c_get_clientdata(client);
	int error;

1401 1402 1403
	if (ts->load_cfg_from_disk)
		wait_for_completion(&ts->firmware_loading_complete);

1404
	/* We need gpio pins to suspend/resume */
1405
	if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
1406
		disable_irq(client->irq);
1407
		return 0;
1408
	}
1409 1410 1411 1412

	/* Free IRQ as IRQ pin is used as output in the suspend sequence */
	goodix_free_irq(ts);

1413 1414 1415
	/* Save reference (calibration) info if necessary */
	goodix_save_bak_ref(ts);

1416
	/* Output LOW on the INT pin for 5 ms */
1417
	error = goodix_irq_direction_output(ts, 0);
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
	if (error) {
		goodix_request_irq(ts);
		return error;
	}

	usleep_range(5000, 6000);

	error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
				    GOODIX_CMD_SCREEN_OFF);
	if (error) {
1428
		goodix_irq_direction_input(ts);
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
		goodix_request_irq(ts);
		return -EAGAIN;
	}

	/*
	 * The datasheet specifies that the interval between sending screen-off
	 * command and wake-up should be longer than 58 ms. To avoid waking up
	 * sooner, delay 58ms here.
	 */
	msleep(58);
	return 0;
}

static int __maybe_unused goodix_resume(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct goodix_ts_data *ts = i2c_get_clientdata(client);
1446
	u8 config_ver;
1447 1448
	int error;

1449
	if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
1450
		enable_irq(client->irq);
1451
		return 0;
1452
	}
1453 1454 1455 1456 1457

	/*
	 * Exit sleep mode by outputting HIGH level to INT pin
	 * for 2ms~5ms.
	 */
1458
	error = goodix_irq_direction_output(ts, 1);
1459 1460 1461 1462 1463 1464 1465 1466 1467
	if (error)
		return error;

	usleep_range(2000, 5000);

	error = goodix_int_sync(ts);
	if (error)
		return error;

1468 1469
	error = goodix_i2c_read(ts->client, ts->chip->config_addr,
				&config_ver, 1);
1470
	if (!error && config_ver != ts->config[0])
1471 1472 1473 1474 1475
		dev_info(dev, "Config version mismatch %d != %d, resetting controller\n",
			 config_ver, ts->config[0]);

	if (error != 0 || config_ver != ts->config[0]) {
		error = goodix_reset(ts);
1476
		if (error)
1477 1478 1479 1480 1481 1482 1483
			return error;

		error = goodix_send_cfg(ts, ts->config, ts->chip->config_len);
		if (error)
			return error;
	}

1484 1485 1486 1487 1488 1489 1490 1491 1492
	error = goodix_request_irq(ts);
	if (error)
		return error;

	return 0;
}

static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);

1493 1494 1495 1496
static const struct i2c_device_id goodix_ts_id[] = {
	{ "GDIX1001:00", 0 },
	{ }
};
1497
MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
1498

1499
#ifdef CONFIG_ACPI
1500 1501
static const struct acpi_device_id goodix_acpi_match[] = {
	{ "GDIX1001", 0 },
1502
	{ "GDIX1002", 0 },
1503 1504 1505
	{ }
};
MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
1506 1507 1508 1509
#endif

#ifdef CONFIG_OF
static const struct of_device_id goodix_of_match[] = {
1510
	{ .compatible = "goodix,gt1151" },
1511
	{ .compatible = "goodix,gt5663" },
1512
	{ .compatible = "goodix,gt5688" },
1513 1514 1515
	{ .compatible = "goodix,gt911" },
	{ .compatible = "goodix,gt9110" },
	{ .compatible = "goodix,gt912" },
1516
	{ .compatible = "goodix,gt9147" },
1517
	{ .compatible = "goodix,gt917s" },
1518 1519 1520
	{ .compatible = "goodix,gt927" },
	{ .compatible = "goodix,gt9271" },
	{ .compatible = "goodix,gt928" },
1521
	{ .compatible = "goodix,gt9286" },
1522 1523 1524 1525 1526
	{ .compatible = "goodix,gt967" },
	{ }
};
MODULE_DEVICE_TABLE(of, goodix_of_match);
#endif
1527 1528 1529

static struct i2c_driver goodix_ts_driver = {
	.probe = goodix_ts_probe,
1530
	.remove = goodix_ts_remove,
1531 1532 1533
	.id_table = goodix_ts_id,
	.driver = {
		.name = "Goodix-TS",
1534 1535
		.acpi_match_table = ACPI_PTR(goodix_acpi_match),
		.of_match_table = of_match_ptr(goodix_of_match),
1536
		.pm = &goodix_pm_ops,
1537 1538 1539 1540 1541 1542 1543 1544
	},
};
module_i2c_driver(goodix_ts_driver);

MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
MODULE_DESCRIPTION("Goodix touchscreen driver");
MODULE_LICENSE("GPL v2");
新手
引导
客服 返回
顶部