edt-ft5x06.c 29.0 KB
Newer Older
1 2
/*
 * Copyright (C) 2012 Simon Budig, <simon.budig@kernelconcepts.de>
3
 * Daniel Wagener <daniel.wagener@kernelconcepts.de> (M09 firmware support)
4
 * Lothar Waßmann <LW@KARO-electronics.de> (DT support)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * This is a driver for the EDT "Polytouch" family of touch controllers
 * based on the FocalTech FT5x06 line of chips.
 *
 * Development of this driver has been sponsored by Glyn:
 *    http://www.glyn.com/Products/Displays
 */

#include <linux/module.h>
#include <linux/ratelimit.h>
30
#include <linux/irq.h>
31 32 33 34 35 36 37
#include <linux/interrupt.h>
#include <linux/input.h>
#include <linux/i2c.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/debugfs.h>
#include <linux/slab.h>
38
#include <linux/gpio/consumer.h>
39
#include <linux/input/mt.h>
40
#include <linux/input/touchscreen.h>
41
#include <linux/of_device.h>
42 43 44 45 46 47 48 49

#define WORK_REGISTER_THRESHOLD		0x00
#define WORK_REGISTER_REPORT_RATE	0x08
#define WORK_REGISTER_GAIN		0x30
#define WORK_REGISTER_OFFSET		0x31
#define WORK_REGISTER_NUM_X		0x33
#define WORK_REGISTER_NUM_Y		0x34

50 51 52 53 54 55 56 57
#define M09_REGISTER_THRESHOLD		0x80
#define M09_REGISTER_GAIN		0x92
#define M09_REGISTER_OFFSET		0x93
#define M09_REGISTER_NUM_X		0x94
#define M09_REGISTER_NUM_Y		0x95

#define NO_REGISTER			0xff

58 59 60 61 62 63 64 65 66 67 68 69
#define WORK_REGISTER_OPMODE		0x3c
#define FACTORY_REGISTER_OPMODE		0x01

#define TOUCH_EVENT_DOWN		0x00
#define TOUCH_EVENT_UP			0x01
#define TOUCH_EVENT_ON			0x02
#define TOUCH_EVENT_RESERVED		0x03

#define EDT_NAME_LEN			23
#define EDT_SWITCH_MODE_RETRIES		10
#define EDT_SWITCH_MODE_DELAY		5 /* msec */
#define EDT_RAW_DATA_RETRIES		100
70
#define EDT_RAW_DATA_DELAY		1000 /* usec */
71

72
enum edt_ver {
73 74
	EDT_M06,
	EDT_M09,
75
	EDT_M12,
76
	GENERIC_FT,
77 78 79 80 81 82 83 84 85 86 87
};

struct edt_reg_addr {
	int reg_threshold;
	int reg_report_rate;
	int reg_gain;
	int reg_offset;
	int reg_num_x;
	int reg_num_y;
};

88 89 90
struct edt_ft5x06_ts_data {
	struct i2c_client *client;
	struct input_dev *input;
91
	struct touchscreen_properties prop;
92 93 94
	u16 num_x;
	u16 num_y;

95 96
	struct gpio_desc *reset_gpio;
	struct gpio_desc *wake_gpio;
97

98 99 100 101 102 103 104 105 106 107 108 109
#if defined(CONFIG_DEBUG_FS)
	struct dentry *debug_dir;
	u8 *raw_buffer;
	size_t raw_bufsize;
#endif

	struct mutex mutex;
	bool factory_mode;
	int threshold;
	int gain;
	int offset;
	int report_rate;
110
	int max_support_points;
111 112

	char name[EDT_NAME_LEN];
113 114 115

	struct edt_reg_addr reg_addr;
	enum edt_ver version;
116 117
};

118 119 120 121
struct edt_i2c_chip_data {
	int  max_support_points;
};

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
static int edt_ft5x06_ts_readwrite(struct i2c_client *client,
				   u16 wr_len, u8 *wr_buf,
				   u16 rd_len, u8 *rd_buf)
{
	struct i2c_msg wrmsg[2];
	int i = 0;
	int ret;

	if (wr_len) {
		wrmsg[i].addr  = client->addr;
		wrmsg[i].flags = 0;
		wrmsg[i].len = wr_len;
		wrmsg[i].buf = wr_buf;
		i++;
	}
	if (rd_len) {
		wrmsg[i].addr  = client->addr;
		wrmsg[i].flags = I2C_M_RD;
		wrmsg[i].len = rd_len;
		wrmsg[i].buf = rd_buf;
		i++;
	}

	ret = i2c_transfer(client->adapter, wrmsg, i);
	if (ret < 0)
		return ret;
	if (ret != i)
		return -EIO;

	return 0;
}

static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata,
				    u8 *buf, int buflen)
{
	int i;
	u8 crc = 0;

	for (i = 0; i < buflen - 1; i++)
		crc ^= buf[i];

	if (crc != buf[buflen-1]) {
		dev_err_ratelimited(&tsdata->client->dev,
				    "crc error: 0x%02x expected, got 0x%02x\n",
				    crc, buf[buflen-1]);
		return false;
	}

	return true;
}

static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id)
{
	struct edt_ft5x06_ts_data *tsdata = dev_id;
	struct device *dev = &tsdata->client->dev;
177
	u8 cmd;
178
	u8 rdbuf[63];
179
	int i, type, x, y, id;
180
	int offset, tplen, datalen, crclen;
181 182
	int error;

183
	switch (tsdata->version) {
184
	case EDT_M06:
185 186 187
		cmd = 0xf9; /* tell the controller to send touch data */
		offset = 5; /* where the actual touch data starts */
		tplen = 4;  /* data comes in so called frames */
188
		crclen = 1; /* length of the crc data */
189 190
		break;

191
	case EDT_M09:
192
	case EDT_M12:
193
	case GENERIC_FT:
194 195
		cmd = 0x0;
		offset = 3;
196
		tplen = 6;
197
		crclen = 0;
198 199 200 201 202 203
		break;

	default:
		goto out;
	}

204
	memset(rdbuf, 0, sizeof(rdbuf));
205
	datalen = tplen * tsdata->max_support_points + offset + crclen;
206 207 208

	error = edt_ft5x06_ts_readwrite(tsdata->client,
					sizeof(cmd), &cmd,
209
					datalen, rdbuf);
210 211 212 213 214 215
	if (error) {
		dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
				    error);
		goto out;
	}

216
	/* M09/M12 does not send header or CRC */
217
	if (tsdata->version == EDT_M06) {
218 219 220 221 222 223 224
		if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa ||
			rdbuf[2] != datalen) {
			dev_err_ratelimited(dev,
					"Unexpected header: %02x%02x%02x!\n",
					rdbuf[0], rdbuf[1], rdbuf[2]);
			goto out;
		}
225

226 227 228
		if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, datalen))
			goto out;
	}
229

230
	for (i = 0; i < tsdata->max_support_points; i++) {
231
		u8 *buf = &rdbuf[i * tplen + offset];
232 233 234 235 236 237 238
		bool down;

		type = buf[0] >> 6;
		/* ignore Reserved events */
		if (type == TOUCH_EVENT_RESERVED)
			continue;

239
		/* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
240
		if (tsdata->version == EDT_M06 && type == TOUCH_EVENT_DOWN)
241 242
			continue;

243 244 245
		x = ((buf[0] << 8) | buf[1]) & 0x0fff;
		y = ((buf[2] << 8) | buf[3]) & 0x0fff;
		id = (buf[2] >> 4) & 0x0f;
246
		down = type != TOUCH_EVENT_UP;
247 248 249 250 251 252 253

		input_mt_slot(tsdata->input, id);
		input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, down);

		if (!down)
			continue;

254 255
		touchscreen_report_pos(tsdata->input, &tsdata->prop, x, y,
				       true);
256 257 258 259 260 261 262 263 264 265 266 267 268 269
	}

	input_mt_report_pointer_emulation(tsdata->input, true);
	input_sync(tsdata->input);

out:
	return IRQ_HANDLED;
}

static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata,
				     u8 addr, u8 value)
{
	u8 wrbuf[4];

270
	switch (tsdata->version) {
271
	case EDT_M06:
272 273 274 275 276 277
		wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
		wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
		wrbuf[2] = value;
		wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2];
		return edt_ft5x06_ts_readwrite(tsdata->client, 4,
					wrbuf, 0, NULL);
278
	case EDT_M09:
279
	case EDT_M12:
280
	case GENERIC_FT:
281 282 283
		wrbuf[0] = addr;
		wrbuf[1] = value;

284
		return edt_ft5x06_ts_readwrite(tsdata->client, 2,
285 286 287 288 289
					wrbuf, 0, NULL);

	default:
		return -EINVAL;
	}
290 291 292 293 294 295 296 297
}

static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata,
				    u8 addr)
{
	u8 wrbuf[2], rdbuf[2];
	int error;

298
	switch (tsdata->version) {
299
	case EDT_M06:
300 301 302
		wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
		wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
		wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40;
303

304 305 306 307
		error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2,
						rdbuf);
		if (error)
			return error;
308

309 310 311 312 313 314 315 316 317
		if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) {
			dev_err(&tsdata->client->dev,
				"crc error: 0x%02x expected, got 0x%02x\n",
				wrbuf[0] ^ wrbuf[1] ^ rdbuf[0],
				rdbuf[1]);
			return -EIO;
		}
		break;

318
	case EDT_M09:
319
	case EDT_M12:
320
	case GENERIC_FT:
321 322 323 324 325 326 327 328 329
		wrbuf[0] = addr;
		error = edt_ft5x06_ts_readwrite(tsdata->client, 1,
						wrbuf, 1, rdbuf);
		if (error)
			return error;
		break;

	default:
		return -EINVAL;
330 331 332 333 334 335 336 337 338 339
	}

	return rdbuf[0];
}

struct edt_ft5x06_attribute {
	struct device_attribute dattr;
	size_t field_offset;
	u8 limit_low;
	u8 limit_high;
340 341
	u8 addr_m06;
	u8 addr_m09;
342 343
};

344 345
#define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09,			\
		_limit_low, _limit_high)				\
346 347 348 349
	struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = {	\
		.dattr = __ATTR(_field, _mode,				\
				edt_ft5x06_setting_show,		\
				edt_ft5x06_setting_store),		\
350 351 352
		.field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
		.addr_m06 = _addr_m06,					\
		.addr_m09 = _addr_m09,					\
353 354 355 356 357 358 359 360 361 362 363 364
		.limit_low = _limit_low,				\
		.limit_high = _limit_high,				\
	}

static ssize_t edt_ft5x06_setting_show(struct device *dev,
				       struct device_attribute *dattr,
				       char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
	struct edt_ft5x06_attribute *attr =
			container_of(dattr, struct edt_ft5x06_attribute, dattr);
365
	u8 *field = (u8 *)tsdata + attr->field_offset;
366 367 368
	int val;
	size_t count = 0;
	int error = 0;
369
	u8 addr;
370 371 372 373 374 375 376 377

	mutex_lock(&tsdata->mutex);

	if (tsdata->factory_mode) {
		error = -EIO;
		goto out;
	}

378
	switch (tsdata->version) {
379
	case EDT_M06:
380 381 382
		addr = attr->addr_m06;
		break;

383
	case EDT_M09:
384
	case EDT_M12:
385
	case GENERIC_FT:
386 387 388 389 390
		addr = attr->addr_m09;
		break;

	default:
		error = -ENODEV;
391 392 393
		goto out;
	}

394 395 396 397 398 399 400 401 402 403 404 405 406
	if (addr != NO_REGISTER) {
		val = edt_ft5x06_register_read(tsdata, addr);
		if (val < 0) {
			error = val;
			dev_err(&tsdata->client->dev,
				"Failed to fetch attribute %s, error %d\n",
				dattr->attr.name, error);
			goto out;
		}
	} else {
		val = *field;
	}

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
	if (val != *field) {
		dev_warn(&tsdata->client->dev,
			 "%s: read (%d) and stored value (%d) differ\n",
			 dattr->attr.name, val, *field);
		*field = val;
	}

	count = scnprintf(buf, PAGE_SIZE, "%d\n", val);
out:
	mutex_unlock(&tsdata->mutex);
	return error ?: count;
}

static ssize_t edt_ft5x06_setting_store(struct device *dev,
					struct device_attribute *dattr,
					const char *buf, size_t count)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
	struct edt_ft5x06_attribute *attr =
			container_of(dattr, struct edt_ft5x06_attribute, dattr);
428
	u8 *field = (u8 *)tsdata + attr->field_offset;
429 430
	unsigned int val;
	int error;
431
	u8 addr;
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448

	mutex_lock(&tsdata->mutex);

	if (tsdata->factory_mode) {
		error = -EIO;
		goto out;
	}

	error = kstrtouint(buf, 0, &val);
	if (error)
		goto out;

	if (val < attr->limit_low || val > attr->limit_high) {
		error = -ERANGE;
		goto out;
	}

449
	switch (tsdata->version) {
450
	case EDT_M06:
451 452 453
		addr = attr->addr_m06;
		break;

454
	case EDT_M09:
455
	case EDT_M12:
456
	case GENERIC_FT:
457 458 459 460 461
		addr = attr->addr_m09;
		break;

	default:
		error = -ENODEV;
462 463 464
		goto out;
	}

465 466 467 468 469 470 471 472 473
	if (addr != NO_REGISTER) {
		error = edt_ft5x06_register_write(tsdata, addr, val);
		if (error) {
			dev_err(&tsdata->client->dev,
				"Failed to update attribute %s, error: %d\n",
				dattr->attr.name, error);
			goto out;
		}
	}
474 475 476 477 478 479 480
	*field = val;

out:
	mutex_unlock(&tsdata->mutex);
	return error ?: count;
}

481
/* m06, m09: range 0-31, m12: range 0-5 */
482 483
static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN,
		M09_REGISTER_GAIN, 0, 31);
484
/* m06, m09: range 0-31, m12: range 0-16 */
485 486
static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET,
		M09_REGISTER_OFFSET, 0, 31);
487
/* m06: range 20 to 80, m09: range 0 to 30, m12: range 1 to 255... */
488
static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD,
489 490
		M09_REGISTER_THRESHOLD, 0, 255);
/* m06: range 3 to 14, m12: (0x64: 100Hz) */
491
static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE,
492
		NO_REGISTER, 0, 255);
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526

static struct attribute *edt_ft5x06_attrs[] = {
	&edt_ft5x06_attr_gain.dattr.attr,
	&edt_ft5x06_attr_offset.dattr.attr,
	&edt_ft5x06_attr_threshold.dattr.attr,
	&edt_ft5x06_attr_report_rate.dattr.attr,
	NULL
};

static const struct attribute_group edt_ft5x06_attr_group = {
	.attrs = edt_ft5x06_attrs,
};

#ifdef CONFIG_DEBUG_FS
static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
{
	struct i2c_client *client = tsdata->client;
	int retries = EDT_SWITCH_MODE_RETRIES;
	int ret;
	int error;

	disable_irq(client->irq);

	if (!tsdata->raw_buffer) {
		tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y *
				      sizeof(u16);
		tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL);
		if (!tsdata->raw_buffer) {
			error = -ENOMEM;
			goto err_out;
		}
	}

	/* mode register is 0x3c when in the work mode */
527
	if (tsdata->version != EDT_M06)
528 529
		goto m09_out;

530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
	error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03);
	if (error) {
		dev_err(&client->dev,
			"failed to switch to factory mode, error %d\n", error);
		goto err_out;
	}

	tsdata->factory_mode = true;
	do {
		mdelay(EDT_SWITCH_MODE_DELAY);
		/* mode register is 0x01 when in factory mode */
		ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE);
		if (ret == 0x03)
			break;
	} while (--retries > 0);

	if (retries == 0) {
		dev_err(&client->dev, "not in factory mode after %dms.\n",
			EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
		error = -EIO;
		goto err_out;
	}

	return 0;

err_out:
	kfree(tsdata->raw_buffer);
	tsdata->raw_buffer = NULL;
	tsdata->factory_mode = false;
	enable_irq(client->irq);

	return error;
562 563

m09_out:
564
	dev_err(&client->dev, "No factory mode support for M09/M12/GENERIC_FT\n");
565 566
	return -EINVAL;

567 568 569 570 571 572
}

static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata)
{
	struct i2c_client *client = tsdata->client;
	int retries = EDT_SWITCH_MODE_RETRIES;
573
	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
	int ret;
	int error;

	/* mode register is 0x01 when in the factory mode */
	error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1);
	if (error) {
		dev_err(&client->dev,
			"failed to switch to work mode, error: %d\n", error);
		return error;
	}

	tsdata->factory_mode = false;

	do {
		mdelay(EDT_SWITCH_MODE_DELAY);
		/* mode register is 0x01 when in factory mode */
		ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE);
		if (ret == 0x01)
			break;
	} while (--retries > 0);

	if (retries == 0) {
		dev_err(&client->dev, "not in work mode after %dms.\n",
			EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
		tsdata->factory_mode = true;
		return -EIO;
	}

602
	kfree(tsdata->raw_buffer);
603 604 605
	tsdata->raw_buffer = NULL;

	/* restore parameters */
606
	edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold,
607
				  tsdata->threshold);
608
	edt_ft5x06_register_write(tsdata, reg_addr->reg_gain,
609
				  tsdata->gain);
610
	edt_ft5x06_register_write(tsdata, reg_addr->reg_offset,
611
				  tsdata->offset);
612
	if (reg_addr->reg_report_rate != NO_REGISTER)
613
		edt_ft5x06_register_write(tsdata, reg_addr->reg_report_rate,
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
				  tsdata->report_rate);

	enable_irq(client->irq);

	return 0;
}

static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode)
{
	struct edt_ft5x06_ts_data *tsdata = data;

	*mode = tsdata->factory_mode;

	return 0;
};

static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode)
{
	struct edt_ft5x06_ts_data *tsdata = data;
	int retval = 0;

	if (mode > 1)
		return -ERANGE;

	mutex_lock(&tsdata->mutex);

	if (mode != tsdata->factory_mode) {
		retval = mode ? edt_ft5x06_factory_mode(tsdata) :
642
				edt_ft5x06_work_mode(tsdata);
643 644 645 646 647 648 649 650 651 652 653 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
	}

	mutex_unlock(&tsdata->mutex);

	return retval;
};

DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get,
			edt_ft5x06_debugfs_mode_set, "%llu\n");

static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file,
				char __user *buf, size_t count, loff_t *off)
{
	struct edt_ft5x06_ts_data *tsdata = file->private_data;
	struct i2c_client *client = tsdata->client;
	int retries  = EDT_RAW_DATA_RETRIES;
	int val, i, error;
	size_t read = 0;
	int colbytes;
	char wrbuf[3];
	u8 *rdbuf;

	if (*off < 0 || *off >= tsdata->raw_bufsize)
		return 0;

	mutex_lock(&tsdata->mutex);

	if (!tsdata->factory_mode || !tsdata->raw_buffer) {
		error = -EIO;
		goto out;
	}

	error = edt_ft5x06_register_write(tsdata, 0x08, 0x01);
	if (error) {
		dev_dbg(&client->dev,
			"failed to write 0x08 register, error %d\n", error);
		goto out;
	}

	do {
683
		usleep_range(EDT_RAW_DATA_DELAY, EDT_RAW_DATA_DELAY + 100);
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
		val = edt_ft5x06_register_read(tsdata, 0x08);
		if (val < 1)
			break;
	} while (--retries > 0);

	if (val < 0) {
		error = val;
		dev_dbg(&client->dev,
			"failed to read 0x08 register, error %d\n", error);
		goto out;
	}

	if (retries == 0) {
		dev_dbg(&client->dev,
			"timed out waiting for register to settle\n");
		error = -ETIMEDOUT;
		goto out;
	}

	rdbuf = tsdata->raw_buffer;
	colbytes = tsdata->num_y * sizeof(u16);

	wrbuf[0] = 0xf5;
	wrbuf[1] = 0x0e;
	for (i = 0; i < tsdata->num_x; i++) {
		wrbuf[2] = i;  /* column index */
		error = edt_ft5x06_ts_readwrite(tsdata->client,
						sizeof(wrbuf), wrbuf,
						colbytes, rdbuf);
		if (error)
			goto out;

		rdbuf += colbytes;
	}

	read = min_t(size_t, count, tsdata->raw_bufsize - *off);
720 721 722 723 724 725
	if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) {
		error = -EFAULT;
		goto out;
	}

	*off += read;
726 727 728 729 730 731
out:
	mutex_unlock(&tsdata->mutex);
	return error ?: read;
};

static const struct file_operations debugfs_raw_data_fops = {
732
	.open = simple_open,
733 734 735
	.read = edt_ft5x06_debugfs_raw_data_read,
};

B
Bill Pemberton 已提交
736
static void
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
			      const char *debugfs_name)
{
	tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL);
	if (!tsdata->debug_dir)
		return;

	debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x);
	debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y);

	debugfs_create_file("mode", S_IRUSR | S_IWUSR,
			    tsdata->debug_dir, tsdata, &debugfs_mode_fops);
	debugfs_create_file("raw_data", S_IRUSR,
			    tsdata->debug_dir, tsdata, &debugfs_raw_data_fops);
}

B
Bill Pemberton 已提交
753
static void
754 755
edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
{
756
	debugfs_remove_recursive(tsdata->debug_dir);
757
	kfree(tsdata->raw_buffer);
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
}

#else

static inline void
edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
			      const char *debugfs_name)
{
}

static inline void
edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
{
}

#endif /* CONFIG_DEBUGFS */

B
Bill Pemberton 已提交
775
static int edt_ft5x06_ts_identify(struct i2c_client *client,
776 777
					struct edt_ft5x06_ts_data *tsdata,
					char *fw_version)
778 779 780 781
{
	u8 rdbuf[EDT_NAME_LEN];
	char *p;
	int error;
782
	char *model_name = tsdata->name;
783

784 785 786 787 788
	/* see what we find if we assume it is a M06 *
	 * if we get less than EDT_NAME_LEN, we don't want
	 * to have garbage in there
	 */
	memset(rdbuf, 0, sizeof(rdbuf));
789
	error = edt_ft5x06_ts_readwrite(client, 1, "\xBB",
790 791 792 793
					EDT_NAME_LEN - 1, rdbuf);
	if (error)
		return error;

794 795 796
	/* Probe content for something consistent.
	 * M06 starts with a response byte, M12 gives the data directly.
	 * M09/Generic does not provide model number information.
797
	 */
798
	if (!strncasecmp(rdbuf + 1, "EP0", 3)) {
799
		tsdata->version = EDT_M06;
800 801 802 803 804 805 806 807 808 809 810 811

		/* remove last '$' end marker */
		rdbuf[EDT_NAME_LEN - 1] = '\0';
		if (rdbuf[EDT_NAME_LEN - 2] == '$')
			rdbuf[EDT_NAME_LEN - 2] = '\0';

		/* look for Model/Version separator */
		p = strchr(rdbuf, '*');
		if (p)
			*p++ = '\0';
		strlcpy(model_name, rdbuf + 1, EDT_NAME_LEN);
		strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
812 813 814 815 816 817 818 819 820 821 822 823 824 825
	} else if (!strncasecmp(rdbuf, "EP0", 3)) {
		tsdata->version = EDT_M12;

		/* remove last '$' end marker */
		rdbuf[EDT_NAME_LEN - 2] = '\0';
		if (rdbuf[EDT_NAME_LEN - 3] == '$')
			rdbuf[EDT_NAME_LEN - 3] = '\0';

		/* look for Model/Version separator */
		p = strchr(rdbuf, '*');
		if (p)
			*p++ = '\0';
		strlcpy(model_name, rdbuf, EDT_NAME_LEN);
		strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
826
	} else {
827
		/* If it is not an EDT M06/M12 touchscreen, then the model
828 829 830 831 832 833 834 835 836
		 * detection is a bit hairy. The different ft5x06
		 * firmares around don't reliably implement the
		 * identification registers. Well, we'll take a shot.
		 *
		 * The main difference between generic focaltec based
		 * touches and EDT M09 is that we know how to retrieve
		 * the max coordinates for the latter.
		 */
		tsdata->version = GENERIC_FT;
837 838 839 840 841

		error = edt_ft5x06_ts_readwrite(client, 1, "\xA6",
						2, rdbuf);
		if (error)
			return error;
842

843
		strlcpy(fw_version, rdbuf, 2);
844

845 846 847 848 849
		error = edt_ft5x06_ts_readwrite(client, 1, "\xA8",
						1, rdbuf);
		if (error)
			return error;

850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
		/* This "model identification" is not exact. Unfortunately
		 * not all firmwares for the ft5x06 put useful values in
		 * the identification registers.
		 */
		switch (rdbuf[0]) {
		case 0x35:   /* EDT EP0350M09 */
		case 0x43:   /* EDT EP0430M09 */
		case 0x50:   /* EDT EP0500M09 */
		case 0x57:   /* EDT EP0570M09 */
		case 0x70:   /* EDT EP0700M09 */
			tsdata->version = EDT_M09;
			snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09",
				rdbuf[0] >> 4, rdbuf[0] & 0x0F);
			break;
		case 0xa1:   /* EDT EP1010ML00 */
			tsdata->version = EDT_M09;
			snprintf(model_name, EDT_NAME_LEN, "EP%i%i0ML00",
				rdbuf[0] >> 4, rdbuf[0] & 0x0F);
			break;
		case 0x5a:   /* Solomon Goldentek Display */
			snprintf(model_name, EDT_NAME_LEN, "GKTW50SCED1R0");
			break;
		default:
			snprintf(model_name, EDT_NAME_LEN,
				 "generic ft5x06 (%02x)",
				 rdbuf[0]);
			break;
		}
878
	}
879 880 881 882

	return 0;
}

883
static void edt_ft5x06_ts_get_defaults(struct device *dev,
884
				       struct edt_ft5x06_ts_data *tsdata)
885
{
886
	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
887 888 889 890
	u32 val;
	int error;

	error = device_property_read_u32(dev, "threshold", &val);
891 892 893 894
	if (!error) {
		edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, val);
		tsdata->threshold = val;
	}
895 896

	error = device_property_read_u32(dev, "gain", &val);
897 898 899 900
	if (!error) {
		edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, val);
		tsdata->gain = val;
	}
901

902
	error = device_property_read_u32(dev, "offset", &val);
903 904 905 906
	if (!error) {
		edt_ft5x06_register_write(tsdata, reg_addr->reg_offset, val);
		tsdata->offset = val;
	}
907 908
}

B
Bill Pemberton 已提交
909
static void
910 911
edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata)
{
912 913
	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;

914
	tsdata->threshold = edt_ft5x06_register_read(tsdata,
915 916 917 918 919 920
						     reg_addr->reg_threshold);
	tsdata->gain = edt_ft5x06_register_read(tsdata, reg_addr->reg_gain);
	tsdata->offset = edt_ft5x06_register_read(tsdata, reg_addr->reg_offset);
	if (reg_addr->reg_report_rate != NO_REGISTER)
		tsdata->report_rate = edt_ft5x06_register_read(tsdata,
						reg_addr->reg_report_rate);
921
	if (tsdata->version == EDT_M06 ||
922 923
	    tsdata->version == EDT_M09 ||
	    tsdata->version == EDT_M12) {
924 925 926 927 928 929 930 931
		tsdata->num_x = edt_ft5x06_register_read(tsdata,
							 reg_addr->reg_num_x);
		tsdata->num_y = edt_ft5x06_register_read(tsdata,
							 reg_addr->reg_num_y);
	} else {
		tsdata->num_x = -1;
		tsdata->num_y = -1;
	}
932 933 934 935 936 937 938 939
}

static void
edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata)
{
	struct edt_reg_addr *reg_addr = &tsdata->reg_addr;

	switch (tsdata->version) {
940
	case EDT_M06:
941 942 943 944 945 946 947 948
		reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD;
		reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE;
		reg_addr->reg_gain = WORK_REGISTER_GAIN;
		reg_addr->reg_offset = WORK_REGISTER_OFFSET;
		reg_addr->reg_num_x = WORK_REGISTER_NUM_X;
		reg_addr->reg_num_y = WORK_REGISTER_NUM_Y;
		break;

949
	case EDT_M09:
950
	case EDT_M12:
951
		reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
952
		reg_addr->reg_report_rate = NO_REGISTER;
953 954 955 956 957
		reg_addr->reg_gain = M09_REGISTER_GAIN;
		reg_addr->reg_offset = M09_REGISTER_OFFSET;
		reg_addr->reg_num_x = M09_REGISTER_NUM_X;
		reg_addr->reg_num_y = M09_REGISTER_NUM_Y;
		break;
958 959 960 961 962 963 964

	case GENERIC_FT:
		/* this is a guesswork */
		reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
		reg_addr->reg_gain = M09_REGISTER_GAIN;
		reg_addr->reg_offset = M09_REGISTER_OFFSET;
		break;
965
	}
966 967
}

B
Bill Pemberton 已提交
968
static int edt_ft5x06_ts_probe(struct i2c_client *client,
969 970
					 const struct i2c_device_id *id)
{
971
	const struct edt_i2c_chip_data *chip_data;
972 973
	struct edt_ft5x06_ts_data *tsdata;
	struct input_dev *input;
974
	unsigned long irq_flags;
975 976 977 978 979
	int error;
	char fw_version[EDT_NAME_LEN];

	dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n");

980 981 982 983 984 985
	tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
	if (!tsdata) {
		dev_err(&client->dev, "failed to allocate driver data.\n");
		return -ENOMEM;
	}

986 987 988 989 990 991 992 993 994 995
	chip_data = of_device_get_match_data(&client->dev);
	if (!chip_data)
		chip_data = (const struct edt_i2c_chip_data *)id->driver_data;
	if (!chip_data || !chip_data->max_support_points) {
		dev_err(&client->dev, "invalid or missing chip data\n");
		return -EINVAL;
	}

	tsdata->max_support_points = chip_data->max_support_points;

996 997 998 999 1000 1001 1002
	tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev,
						     "reset", GPIOD_OUT_HIGH);
	if (IS_ERR(tsdata->reset_gpio)) {
		error = PTR_ERR(tsdata->reset_gpio);
		dev_err(&client->dev,
			"Failed to request GPIO reset pin, error %d\n", error);
		return error;
1003 1004
	}

1005 1006 1007 1008 1009 1010
	tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev,
						    "wake", GPIOD_OUT_LOW);
	if (IS_ERR(tsdata->wake_gpio)) {
		error = PTR_ERR(tsdata->wake_gpio);
		dev_err(&client->dev,
			"Failed to request GPIO wake pin, error %d\n", error);
1011
		return error;
1012
	}
1013

1014 1015 1016 1017 1018 1019 1020 1021 1022
	if (tsdata->wake_gpio) {
		usleep_range(5000, 6000);
		gpiod_set_value_cansleep(tsdata->wake_gpio, 1);
	}

	if (tsdata->reset_gpio) {
		usleep_range(5000, 6000);
		gpiod_set_value_cansleep(tsdata->reset_gpio, 0);
		msleep(300);
1023 1024
	}

1025 1026 1027 1028
	input = devm_input_allocate_device(&client->dev);
	if (!input) {
		dev_err(&client->dev, "failed to allocate input device.\n");
		return -ENOMEM;
1029 1030 1031 1032 1033 1034 1035
	}

	mutex_init(&tsdata->mutex);
	tsdata->client = client;
	tsdata->input = input;
	tsdata->factory_mode = false;

1036
	error = edt_ft5x06_ts_identify(client, tsdata, fw_version);
1037 1038
	if (error) {
		dev_err(&client->dev, "touchscreen probe failed\n");
1039
		return error;
1040 1041
	}

1042
	edt_ft5x06_ts_set_regs(tsdata);
1043
	edt_ft5x06_ts_get_defaults(&client->dev, tsdata);
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
	edt_ft5x06_ts_get_parameters(tsdata);

	dev_dbg(&client->dev,
		"Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
		tsdata->name, fw_version, tsdata->num_x, tsdata->num_y);

	input->name = tsdata->name;
	input->id.bustype = BUS_I2C;
	input->dev.parent = &client->dev;

1054
	if (tsdata->version == EDT_M06 ||
1055 1056
	    tsdata->version == EDT_M09 ||
	    tsdata->version == EDT_M12) {
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
		input_set_abs_params(input, ABS_MT_POSITION_X,
				     0, tsdata->num_x * 64 - 1, 0, 0);
		input_set_abs_params(input, ABS_MT_POSITION_Y,
				     0, tsdata->num_y * 64 - 1, 0, 0);
	} else {
		/* Unknown maximum values. Specify via devicetree */
		input_set_abs_params(input, ABS_MT_POSITION_X,
				     0, 65535, 0, 0);
		input_set_abs_params(input, ABS_MT_POSITION_Y,
				     0, 65535, 0, 0);
	}
1068

1069
	touchscreen_parse_properties(input, true, &tsdata->prop);
1070

1071 1072
	error = input_mt_init_slots(input, tsdata->max_support_points,
				INPUT_MT_DIRECT);
1073 1074
	if (error) {
		dev_err(&client->dev, "Unable to init MT slots.\n");
1075
		return error;
1076 1077 1078 1079
	}

	i2c_set_clientdata(client, tsdata);

1080 1081 1082 1083 1084 1085 1086
	irq_flags = irq_get_trigger_type(client->irq);
	if (irq_flags == IRQF_TRIGGER_NONE)
		irq_flags = IRQF_TRIGGER_FALLING;
	irq_flags |= IRQF_ONESHOT;

	error = devm_request_threaded_irq(&client->dev, client->irq,
					NULL, edt_ft5x06_ts_isr, irq_flags,
1087
					client->name, tsdata);
1088 1089
	if (error) {
		dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
1090
		return error;
1091 1092
	}

1093
	error = devm_device_add_group(&client->dev, &edt_ft5x06_attr_group);
1094
	if (error)
1095
		return error;
1096 1097

	error = input_register_device(input);
1098
	if (error)
1099
		return error;
1100 1101 1102 1103 1104

	edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev));
	device_init_wakeup(&client->dev, 1);

	dev_dbg(&client->dev,
1105
		"EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1106 1107 1108
		client->irq,
		tsdata->wake_gpio ? desc_to_gpio(tsdata->wake_gpio) : -1,
		tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1);
1109 1110 1111 1112

	return 0;
}

B
Bill Pemberton 已提交
1113
static int edt_ft5x06_ts_remove(struct i2c_client *client)
1114 1115 1116 1117 1118 1119 1120 1121
{
	struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);

	edt_ft5x06_ts_teardown_debugfs(tsdata);

	return 0;
}

1122
static int __maybe_unused edt_ft5x06_ts_suspend(struct device *dev)
1123 1124 1125 1126 1127 1128 1129 1130 1131
{
	struct i2c_client *client = to_i2c_client(dev);

	if (device_may_wakeup(dev))
		enable_irq_wake(client->irq);

	return 0;
}

1132
static int __maybe_unused edt_ft5x06_ts_resume(struct device *dev)
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
{
	struct i2c_client *client = to_i2c_client(dev);

	if (device_may_wakeup(dev))
		disable_irq_wake(client->irq);

	return 0;
}

static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops,
			 edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume);

1145 1146 1147 1148
static const struct edt_i2c_chip_data edt_ft5x06_data = {
	.max_support_points = 5,
};

1149 1150 1151 1152
static const struct edt_i2c_chip_data edt_ft5506_data = {
	.max_support_points = 10,
};

1153 1154 1155 1156
static const struct edt_i2c_chip_data edt_ft6236_data = {
	.max_support_points = 2,
};

1157
static const struct i2c_device_id edt_ft5x06_ts_id[] = {
1158
	{ .name = "edt-ft5x06", .driver_data = (long)&edt_ft5x06_data },
1159
	{ .name = "edt-ft5506", .driver_data = (long)&edt_ft5506_data },
1160 1161
	/* Note no edt- prefix for compatibility with the ft6236.c driver */
	{ .name = "ft6236", .driver_data = (long)&edt_ft6236_data },
1162
	{ /* sentinel */ }
1163 1164 1165
};
MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id);

1166 1167
#ifdef CONFIG_OF
static const struct of_device_id edt_ft5x06_of_match[] = {
1168 1169 1170
	{ .compatible = "edt,edt-ft5206", .data = &edt_ft5x06_data },
	{ .compatible = "edt,edt-ft5306", .data = &edt_ft5x06_data },
	{ .compatible = "edt,edt-ft5406", .data = &edt_ft5x06_data },
1171
	{ .compatible = "edt,edt-ft5506", .data = &edt_ft5506_data },
1172 1173
	/* Note focaltech vendor prefix for compatibility with ft6236.c */
	{ .compatible = "focaltech,ft6236", .data = &edt_ft6236_data },
1174 1175 1176 1177 1178
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match);
#endif

1179 1180 1181
static struct i2c_driver edt_ft5x06_ts_driver = {
	.driver = {
		.name = "edt_ft5x06",
1182
		.of_match_table = of_match_ptr(edt_ft5x06_of_match),
1183 1184 1185 1186
		.pm = &edt_ft5x06_ts_pm_ops,
	},
	.id_table = edt_ft5x06_ts_id,
	.probe    = edt_ft5x06_ts_probe,
B
Bill Pemberton 已提交
1187
	.remove   = edt_ft5x06_ts_remove,
1188 1189 1190 1191 1192 1193 1194
};

module_i2c_driver(edt_ft5x06_ts_driver);

MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
MODULE_LICENSE("GPL");