Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Raydium touchscreen I2C driver.
   4 *
   5 * Copyright (C) 2012-2014, Raydium Semiconductor Corporation.
   6 *
   7 * Raydium reserves the right to make changes without further notice
   8 * to the materials described herein. Raydium does not assume any
   9 * liability arising out of the application described herein.
  10 *
  11 * Contact Raydium Semiconductor Corporation at www.rad-ic.com
  12 */
  13
  14#include <linux/acpi.h>
  15#include <linux/delay.h>
  16#include <linux/firmware.h>
  17#include <linux/gpio/consumer.h>
  18#include <linux/i2c.h>
  19#include <linux/input.h>
  20#include <linux/input/mt.h>
  21#include <linux/interrupt.h>
  22#include <linux/module.h>
  23#include <linux/of.h>
  24#include <linux/pm_wakeirq.h>
  25#include <linux/regulator/consumer.h>
  26#include <linux/slab.h>
  27#include <linux/unaligned.h>
  28
  29/* Slave I2C mode */
  30#define RM_BOOT_BLDR		0x02
  31#define RM_BOOT_MAIN		0x03
  32
  33/* I2C bootoloader commands */
  34#define RM_CMD_BOOT_PAGE_WRT	0x0B		/* send bl page write */
  35#define RM_CMD_BOOT_WRT		0x11		/* send bl write */
  36#define RM_CMD_BOOT_ACK		0x22		/* send ack*/
  37#define RM_CMD_BOOT_CHK		0x33		/* send data check */
  38#define RM_CMD_BOOT_READ	0x44		/* send wait bl data ready*/
  39
  40#define RM_BOOT_RDY		0xFF		/* bl data ready */
  41#define RM_BOOT_CMD_READHWID	0x0E		/* read hwid */
  42
  43/* I2C main commands */
  44#define RM_CMD_QUERY_BANK	0x2B
  45#define RM_CMD_DATA_BANK	0x4D
  46#define RM_CMD_ENTER_SLEEP	0x4E
  47#define RM_CMD_BANK_SWITCH	0xAA
  48
  49#define RM_RESET_MSG_ADDR	0x40000004
  50
  51#define RM_MAX_READ_SIZE	56
  52#define RM_PACKET_CRC_SIZE	2
  53
  54/* Touch relative info */
  55#define RM_MAX_RETRIES		3
  56#define RM_RETRY_DELAY_MS	20
  57#define RM_MAX_TOUCH_NUM	10
  58#define RM_BOOT_DELAY_MS	100
  59
  60/* Offsets in contact data */
  61#define RM_CONTACT_STATE_POS	0
  62#define RM_CONTACT_X_POS	1
  63#define RM_CONTACT_Y_POS	3
  64#define RM_CONTACT_PRESSURE_POS	5
  65#define RM_CONTACT_WIDTH_X_POS	6
  66#define RM_CONTACT_WIDTH_Y_POS	7
  67
  68/* Bootloader relative info */
  69#define RM_BL_WRT_CMD_SIZE	3	/* bl flash wrt cmd size */
  70#define RM_BL_WRT_PKG_SIZE	32	/* bl wrt pkg size */
  71#define RM_BL_WRT_LEN		(RM_BL_WRT_PKG_SIZE + RM_BL_WRT_CMD_SIZE)
  72#define RM_FW_PAGE_SIZE		128
  73#define RM_MAX_FW_RETRIES	30
  74#define RM_MAX_FW_SIZE		0xD000
  75
  76#define RM_POWERON_DELAY_USEC	500
  77#define RM_RESET_DELAY_MSEC	50
  78
  79enum raydium_bl_cmd {
  80	BL_HEADER = 0,
  81	BL_PAGE_STR,
  82	BL_PKG_IDX,
  83	BL_DATA_STR,
  84};
  85
  86enum raydium_bl_ack {
  87	RAYDIUM_ACK_NULL = 0,
  88	RAYDIUM_WAIT_READY,
  89	RAYDIUM_PATH_READY,
  90};
  91
  92enum raydium_boot_mode {
  93	RAYDIUM_TS_MAIN = 0,
  94	RAYDIUM_TS_BLDR,
  95};
  96
  97/* Response to RM_CMD_DATA_BANK request */
  98struct raydium_data_info {
  99	__le32 data_bank_addr;
 100	u8 pkg_size;
 101	u8 tp_info_size;
 102};
 103
 104struct raydium_info {
 105	__le32 hw_ver;		/*device version */
 106	u8 main_ver;
 107	u8 sub_ver;
 108	__le16 ft_ver;		/* test version */
 109	u8 x_num;
 110	u8 y_num;
 111	__le16 x_max;
 112	__le16 y_max;
 113	u8 x_res;		/* units/mm */
 114	u8 y_res;		/* units/mm */
 115};
 116
 117/* struct raydium_data - represents state of Raydium touchscreen device */
 118struct raydium_data {
 119	struct i2c_client *client;
 120	struct input_dev *input;
 121
 122	struct regulator *avdd;
 123	struct regulator *vccio;
 124	struct gpio_desc *reset_gpio;
 125
 126	struct raydium_info info;
 127
 128	struct mutex sysfs_mutex;
 129
 130	u8 *report_data;
 131
 132	u32 data_bank_addr;
 133	u8 report_size;
 134	u8 contact_size;
 135	u8 pkg_size;
 136
 137	enum raydium_boot_mode boot_mode;
 
 
 138};
 139
 140/*
 141 * Header to be sent for RM_CMD_BANK_SWITCH command. This is used by
 142 * raydium_i2c_{read|send} below.
 143 */
 144struct __packed raydium_bank_switch_header {
 145	u8 cmd;
 146	__be32 be_addr;
 147};
 148
 149static int raydium_i2c_xfer(struct i2c_client *client, u32 addr,
 150			    struct i2c_msg *xfer, size_t xfer_count)
 151{
 152	int ret;
 153	/*
 154	 * If address is greater than 255, then RM_CMD_BANK_SWITCH needs to be
 155	 * sent first. Else, skip the header i.e. xfer[0].
 156	 */
 157	int xfer_start_idx = (addr > 0xff) ? 0 : 1;
 158	xfer_count -= xfer_start_idx;
 159
 160	ret = i2c_transfer(client->adapter, &xfer[xfer_start_idx], xfer_count);
 161	if (likely(ret == xfer_count))
 162		return 0;
 163
 164	return ret < 0 ? ret : -EIO;
 165}
 166
 167static int raydium_i2c_send(struct i2c_client *client,
 168			    u32 addr, const void *data, size_t len)
 169{
 170	int tries = 0;
 171	int error;
 172	u8 *tx_buf;
 173	u8 reg_addr = addr & 0xff;
 174
 175	tx_buf = kmalloc(len + 1, GFP_KERNEL);
 176	if (!tx_buf)
 177		return -ENOMEM;
 178
 179	tx_buf[0] = reg_addr;
 180	memcpy(tx_buf + 1, data, len);
 181
 182	do {
 183		struct raydium_bank_switch_header header = {
 184			.cmd = RM_CMD_BANK_SWITCH,
 185			.be_addr = cpu_to_be32(addr),
 186		};
 187
 188		/*
 189		 * Perform as a single i2c_transfer transaction to ensure that
 190		 * no other I2C transactions are initiated on the bus to any
 191		 * other device in between. Initiating transacations to other
 192		 * devices after RM_CMD_BANK_SWITCH is sent is known to cause
 193		 * issues. This is also why regmap infrastructure cannot be used
 194		 * for this driver. Regmap handles page(bank) switch and reads
 195		 * as separate i2c_transfer() operations. This can result in
 196		 * problems if the Raydium device is on a shared I2C bus.
 197		 */
 198		struct i2c_msg xfer[] = {
 199			{
 200				.addr = client->addr,
 201				.len = sizeof(header),
 202				.buf = (u8 *)&header,
 203			},
 204			{
 205				.addr = client->addr,
 206				.len = len + 1,
 207				.buf = tx_buf,
 208			},
 209		};
 210
 211		error = raydium_i2c_xfer(client, addr, xfer, ARRAY_SIZE(xfer));
 212		if (likely(!error))
 213			goto out;
 214
 215		msleep(RM_RETRY_DELAY_MS);
 216	} while (++tries < RM_MAX_RETRIES);
 217
 218	dev_err(&client->dev, "%s failed: %d\n", __func__, error);
 219out:
 220	kfree(tx_buf);
 221	return error;
 222}
 223
 224static int raydium_i2c_read(struct i2c_client *client,
 225			    u32 addr, void *data, size_t len)
 226{
 227	int error;
 228
 229	while (len) {
 230		u8 reg_addr = addr & 0xff;
 231		struct raydium_bank_switch_header header = {
 232			.cmd = RM_CMD_BANK_SWITCH,
 233			.be_addr = cpu_to_be32(addr),
 234		};
 235		size_t xfer_len = min_t(size_t, len, RM_MAX_READ_SIZE);
 236
 237		/*
 238		 * Perform as a single i2c_transfer transaction to ensure that
 239		 * no other I2C transactions are initiated on the bus to any
 240		 * other device in between. Initiating transacations to other
 241		 * devices after RM_CMD_BANK_SWITCH is sent is known to cause
 242		 * issues. This is also why regmap infrastructure cannot be used
 243		 * for this driver. Regmap handles page(bank) switch and writes
 244		 * as separate i2c_transfer() operations. This can result in
 245		 * problems if the Raydium device is on a shared I2C bus.
 246		 */
 247		struct i2c_msg xfer[] = {
 248			{
 249				.addr = client->addr,
 250				.len = sizeof(header),
 251				.buf = (u8 *)&header,
 252			},
 253			{
 254				.addr = client->addr,
 255				.len = 1,
 256				.buf = &reg_addr,
 257			},
 258			{
 259				.addr = client->addr,
 260				.len = xfer_len,
 261				.buf = data,
 262				.flags = I2C_M_RD,
 263			}
 264		};
 265
 266		error = raydium_i2c_xfer(client, addr, xfer, ARRAY_SIZE(xfer));
 267		if (unlikely(error))
 268			return error;
 269
 270		len -= xfer_len;
 271		data += xfer_len;
 272		addr += xfer_len;
 273	}
 274
 275	return 0;
 276}
 277
 278static int raydium_i2c_sw_reset(struct i2c_client *client)
 279{
 280	const u8 soft_rst_cmd = 0x01;
 281	int error;
 282
 283	error = raydium_i2c_send(client, RM_RESET_MSG_ADDR, &soft_rst_cmd,
 284				 sizeof(soft_rst_cmd));
 285	if (error) {
 286		dev_err(&client->dev, "software reset failed: %d\n", error);
 287		return error;
 288	}
 289
 290	msleep(RM_RESET_DELAY_MSEC);
 291
 292	return 0;
 293}
 294
 295static int raydium_i2c_query_ts_bootloader_info(struct raydium_data *ts)
 296{
 297	struct i2c_client *client = ts->client;
 298	static const u8 get_hwid[] = { RM_BOOT_CMD_READHWID,
 299				       0x10, 0xc0, 0x01, 0x00, 0x04, 0x00 };
 300	u8 rbuf[5] = { 0 };
 301	u32 hw_ver;
 302	int error;
 303
 304	error = raydium_i2c_send(client, RM_CMD_BOOT_WRT,
 305				 get_hwid, sizeof(get_hwid));
 306	if (error) {
 307		dev_err(&client->dev, "WRT HWID command failed: %d\n", error);
 308		return error;
 309	}
 310
 311	error = raydium_i2c_send(client, RM_CMD_BOOT_ACK, rbuf, 1);
 312	if (error) {
 313		dev_err(&client->dev, "Ack HWID command failed: %d\n", error);
 314		return error;
 315	}
 316
 317	error = raydium_i2c_read(client, RM_CMD_BOOT_CHK, rbuf, sizeof(rbuf));
 318	if (error) {
 319		dev_err(&client->dev, "Read HWID command failed: %d (%4ph)\n",
 320			error, rbuf + 1);
 321		hw_ver = 0xffffffffUL;
 322	} else {
 323		hw_ver = get_unaligned_be32(rbuf + 1);
 324	}
 325
 326	ts->info.hw_ver = cpu_to_le32(hw_ver);
 327	ts->info.main_ver = 0xff;
 328	ts->info.sub_ver = 0xff;
 329
 330	return error;
 331}
 332
 333static int raydium_i2c_query_ts_info(struct raydium_data *ts)
 334{
 335	struct i2c_client *client = ts->client;
 336	struct raydium_data_info data_info;
 337	__le32 query_bank_addr;
 338
 339	int error, retry_cnt;
 340
 341	for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) {
 342		error = raydium_i2c_read(client, RM_CMD_DATA_BANK,
 343					 &data_info, sizeof(data_info));
 344		if (error)
 345			continue;
 346
 347		/*
 348		 * Warn user if we already allocated memory for reports and
 349		 * then the size changed (due to firmware update?) and keep
 350		 * old size instead.
 351		 */
 352		if (ts->report_data && ts->pkg_size != data_info.pkg_size) {
 353			dev_warn(&client->dev,
 354				 "report size changes, was: %d, new: %d\n",
 355				 ts->pkg_size, data_info.pkg_size);
 356		} else {
 357			ts->pkg_size = data_info.pkg_size;
 358			ts->report_size = ts->pkg_size - RM_PACKET_CRC_SIZE;
 359		}
 360
 361		ts->contact_size = data_info.tp_info_size;
 362		ts->data_bank_addr = le32_to_cpu(data_info.data_bank_addr);
 363
 364		dev_dbg(&client->dev,
 365			"data_bank_addr: %#08x, report_size: %d, contact_size: %d\n",
 366			ts->data_bank_addr, ts->report_size, ts->contact_size);
 367
 368		error = raydium_i2c_read(client, RM_CMD_QUERY_BANK,
 369					 &query_bank_addr,
 370					 sizeof(query_bank_addr));
 371		if (error)
 372			continue;
 373
 374		error = raydium_i2c_read(client, le32_to_cpu(query_bank_addr),
 375					 &ts->info, sizeof(ts->info));
 376		if (error)
 377			continue;
 378
 379		return 0;
 380	}
 381
 382	dev_err(&client->dev, "failed to query device parameters: %d\n", error);
 383	return error;
 384}
 385
 386static int raydium_i2c_check_fw_status(struct raydium_data *ts)
 387{
 388	struct i2c_client *client = ts->client;
 389	static const u8 bl_ack = 0x62;
 390	static const u8 main_ack = 0x66;
 391	u8 buf[4];
 392	int error;
 393
 394	error = raydium_i2c_read(client, RM_CMD_BOOT_READ, buf, sizeof(buf));
 395	if (!error) {
 396		if (buf[0] == bl_ack)
 397			ts->boot_mode = RAYDIUM_TS_BLDR;
 398		else if (buf[0] == main_ack)
 399			ts->boot_mode = RAYDIUM_TS_MAIN;
 400		return 0;
 401	}
 402
 403	return error;
 404}
 405
 406static int raydium_i2c_initialize(struct raydium_data *ts)
 407{
 408	struct i2c_client *client = ts->client;
 409	int error, retry_cnt;
 410
 411	for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) {
 412		/* Wait for Hello packet */
 413		msleep(RM_BOOT_DELAY_MS);
 414
 415		error = raydium_i2c_check_fw_status(ts);
 416		if (error) {
 417			dev_err(&client->dev,
 418				"failed to read 'hello' packet: %d\n", error);
 419			continue;
 420		}
 421
 422		if (ts->boot_mode == RAYDIUM_TS_BLDR ||
 423		    ts->boot_mode == RAYDIUM_TS_MAIN) {
 424			break;
 425		}
 426	}
 427
 428	if (error)
 429		ts->boot_mode = RAYDIUM_TS_BLDR;
 430
 431	if (ts->boot_mode == RAYDIUM_TS_BLDR)
 432		raydium_i2c_query_ts_bootloader_info(ts);
 433	else
 
 
 434		raydium_i2c_query_ts_info(ts);
 
 435
 436	return error;
 437}
 438
 439static int raydium_i2c_bl_chk_state(struct i2c_client *client,
 440				    enum raydium_bl_ack state)
 441{
 442	static const u8 ack_ok[] = { 0xFF, 0x39, 0x30, 0x30, 0x54 };
 443	u8 rbuf[sizeof(ack_ok)];
 444	u8 retry;
 445	int error;
 446
 447	for (retry = 0; retry < RM_MAX_FW_RETRIES; retry++) {
 448		switch (state) {
 449		case RAYDIUM_ACK_NULL:
 450			return 0;
 451
 452		case RAYDIUM_WAIT_READY:
 453			error = raydium_i2c_read(client, RM_CMD_BOOT_CHK,
 454						 &rbuf[0], 1);
 455			if (!error && rbuf[0] == RM_BOOT_RDY)
 456				return 0;
 457
 458			break;
 459
 460		case RAYDIUM_PATH_READY:
 461			error = raydium_i2c_read(client, RM_CMD_BOOT_CHK,
 462						 rbuf, sizeof(rbuf));
 463			if (!error && !memcmp(rbuf, ack_ok, sizeof(ack_ok)))
 464				return 0;
 465
 466			break;
 467
 468		default:
 469			dev_err(&client->dev, "%s: invalid target state %d\n",
 470				__func__, state);
 471			return -EINVAL;
 472		}
 473
 474		msleep(20);
 475	}
 476
 477	return -ETIMEDOUT;
 478}
 479
 480static int raydium_i2c_write_object(struct i2c_client *client,
 481				    const void *data, size_t len,
 482				    enum raydium_bl_ack state)
 483{
 484	int error;
 485	static const u8 cmd[] = { 0xFF, 0x39 };
 486
 487	error = raydium_i2c_send(client, RM_CMD_BOOT_WRT, data, len);
 488	if (error) {
 489		dev_err(&client->dev, "WRT obj command failed: %d\n",
 490			error);
 491		return error;
 492	}
 493
 494	error = raydium_i2c_send(client, RM_CMD_BOOT_ACK, cmd, sizeof(cmd));
 495	if (error) {
 496		dev_err(&client->dev, "Ack obj command failed: %d\n", error);
 497		return error;
 498	}
 499
 500	error = raydium_i2c_bl_chk_state(client, state);
 501	if (error) {
 502		dev_err(&client->dev, "BL check state failed: %d\n", error);
 503		return error;
 504	}
 505	return 0;
 506}
 507
 508static int raydium_i2c_boot_trigger(struct i2c_client *client)
 509{
 510	static const u8 cmd[7][6] = {
 511		{ 0x08, 0x0C, 0x09, 0x00, 0x50, 0xD7 },
 512		{ 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
 513		{ 0x08, 0x04, 0x09, 0x00, 0x50, 0x00 },
 514		{ 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
 515		{ 0x08, 0x0C, 0x09, 0x00, 0x50, 0x00 },
 516		{ 0x06, 0x01, 0x00, 0x00, 0x00, 0x00 },
 517		{ 0x02, 0xA2, 0x00, 0x00, 0x00, 0x00 },
 518	};
 519	int i;
 520	int error;
 521
 522	for (i = 0; i < 7; i++) {
 523		error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
 524						 RAYDIUM_WAIT_READY);
 525		if (error) {
 526			dev_err(&client->dev,
 527				"boot trigger failed at step %d: %d\n",
 528				i, error);
 529			return error;
 530		}
 531	}
 532
 533	return 0;
 534}
 535
 536static int raydium_i2c_fw_trigger(struct i2c_client *client)
 537{
 538	static const u8 cmd[5][11] = {
 539		{ 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0xD7, 0, 0, 0 },
 540		{ 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
 541		{ 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
 542		{ 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
 543		{ 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
 544	};
 545	int i;
 546	int error;
 547
 548	for (i = 0; i < 5; i++) {
 549		error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
 550						 RAYDIUM_ACK_NULL);
 551		if (error) {
 552			dev_err(&client->dev,
 553				"fw trigger failed at step %d: %d\n",
 554				i, error);
 555			return error;
 556		}
 557	}
 558
 559	return 0;
 560}
 561
 562static int raydium_i2c_check_path(struct i2c_client *client)
 563{
 564	static const u8 cmd[] = { 0x09, 0x00, 0x09, 0x00, 0x50, 0x10, 0x00 };
 565	int error;
 566
 567	error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
 568					 RAYDIUM_PATH_READY);
 569	if (error) {
 570		dev_err(&client->dev, "check path command failed: %d\n", error);
 571		return error;
 572	}
 573
 574	return 0;
 575}
 576
 577static int raydium_i2c_enter_bl(struct i2c_client *client)
 578{
 579	static const u8 cal_cmd[] = { 0x00, 0x01, 0x52 };
 580	int error;
 581
 582	error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
 583					 RAYDIUM_ACK_NULL);
 584	if (error) {
 585		dev_err(&client->dev, "enter bl command failed: %d\n", error);
 586		return error;
 587	}
 588
 589	msleep(RM_BOOT_DELAY_MS);
 590	return 0;
 591}
 592
 593static int raydium_i2c_leave_bl(struct i2c_client *client)
 594{
 595	static const u8 leave_cmd[] = { 0x05, 0x00 };
 596	int error;
 597
 598	error = raydium_i2c_write_object(client, leave_cmd, sizeof(leave_cmd),
 599					 RAYDIUM_ACK_NULL);
 600	if (error) {
 601		dev_err(&client->dev, "leave bl command failed: %d\n", error);
 602		return error;
 603	}
 604
 605	msleep(RM_BOOT_DELAY_MS);
 606	return 0;
 607}
 608
 609static int raydium_i2c_write_checksum(struct i2c_client *client,
 610				      size_t length, u16 checksum)
 611{
 612	u8 checksum_cmd[] = { 0x00, 0x05, 0x6D, 0x00, 0x00, 0x00, 0x00 };
 613	int error;
 614
 615	put_unaligned_le16(length, &checksum_cmd[3]);
 616	put_unaligned_le16(checksum, &checksum_cmd[5]);
 617
 618	error = raydium_i2c_write_object(client,
 619					 checksum_cmd, sizeof(checksum_cmd),
 620					 RAYDIUM_ACK_NULL);
 621	if (error) {
 622		dev_err(&client->dev, "failed to write checksum: %d\n",
 623			error);
 624		return error;
 625	}
 626
 627	return 0;
 628}
 629
 630static int raydium_i2c_disable_watch_dog(struct i2c_client *client)
 631{
 632	static const u8 cmd[] = { 0x0A, 0xAA };
 633	int error;
 634
 635	error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
 636					 RAYDIUM_WAIT_READY);
 637	if (error) {
 638		dev_err(&client->dev, "disable watchdog command failed: %d\n",
 639			error);
 640		return error;
 641	}
 642
 643	return 0;
 644}
 645
 646static int raydium_i2c_fw_write_page(struct i2c_client *client,
 647				     u16 page_idx, const void *data, size_t len)
 648{
 649	u8 buf[RM_BL_WRT_LEN];
 650	size_t xfer_len;
 651	int error;
 652	int i;
 653
 654	BUILD_BUG_ON((RM_FW_PAGE_SIZE % RM_BL_WRT_PKG_SIZE) != 0);
 655
 656	for (i = 0; i < RM_FW_PAGE_SIZE / RM_BL_WRT_PKG_SIZE; i++) {
 657		buf[BL_HEADER] = RM_CMD_BOOT_PAGE_WRT;
 658		buf[BL_PAGE_STR] = page_idx ? 0xff : 0;
 659		buf[BL_PKG_IDX] = i + 1;
 660
 661		xfer_len = min_t(size_t, len, RM_BL_WRT_PKG_SIZE);
 662		memcpy(&buf[BL_DATA_STR], data, xfer_len);
 663		if (len < RM_BL_WRT_PKG_SIZE)
 664			memset(&buf[BL_DATA_STR + xfer_len], 0xff,
 665				RM_BL_WRT_PKG_SIZE - xfer_len);
 666
 667		error = raydium_i2c_write_object(client, buf, RM_BL_WRT_LEN,
 668						 RAYDIUM_WAIT_READY);
 669		if (error) {
 670			dev_err(&client->dev,
 671				"page write command failed for page %d, chunk %d: %d\n",
 672				page_idx, i, error);
 673			return error;
 674		}
 675
 676		data += xfer_len;
 677		len -= xfer_len;
 678	}
 679
 680	return error;
 681}
 682
 683static u16 raydium_calc_chksum(const u8 *buf, u16 len)
 684{
 685	u16 checksum = 0;
 686	u16 i;
 687
 688	for (i = 0; i < len; i++)
 689		checksum += buf[i];
 690
 691	return checksum;
 692}
 693
 694static int raydium_i2c_do_update_firmware(struct raydium_data *ts,
 695					 const struct firmware *fw)
 696{
 697	struct i2c_client *client = ts->client;
 698	const void *data;
 699	size_t data_len;
 700	size_t len;
 701	int page_nr;
 702	int i;
 703	int error;
 704	u16 fw_checksum;
 705
 706	if (fw->size == 0 || fw->size > RM_MAX_FW_SIZE) {
 707		dev_err(&client->dev, "Invalid firmware length\n");
 708		return -EINVAL;
 709	}
 710
 711	error = raydium_i2c_check_fw_status(ts);
 712	if (error) {
 713		dev_err(&client->dev, "Unable to access IC %d\n", error);
 714		return error;
 715	}
 716
 717	if (ts->boot_mode == RAYDIUM_TS_MAIN) {
 718		for (i = 0; i < RM_MAX_RETRIES; i++) {
 719			error = raydium_i2c_enter_bl(client);
 720			if (!error) {
 721				error = raydium_i2c_check_fw_status(ts);
 722				if (error) {
 723					dev_err(&client->dev,
 724						"unable to access IC: %d\n",
 725						error);
 726					return error;
 727				}
 728
 729				if (ts->boot_mode == RAYDIUM_TS_BLDR)
 730					break;
 731			}
 732		}
 733
 734		if (ts->boot_mode == RAYDIUM_TS_MAIN) {
 735			dev_err(&client->dev,
 736				"failed to jump to boot loader: %d\n",
 737				error);
 738			return -EIO;
 739		}
 740	}
 741
 742	error = raydium_i2c_disable_watch_dog(client);
 743	if (error)
 744		return error;
 745
 746	error = raydium_i2c_check_path(client);
 747	if (error)
 748		return error;
 749
 750	error = raydium_i2c_boot_trigger(client);
 751	if (error) {
 752		dev_err(&client->dev, "send boot trigger fail: %d\n", error);
 753		return error;
 754	}
 755
 756	msleep(RM_BOOT_DELAY_MS);
 757
 758	data = fw->data;
 759	data_len = fw->size;
 760	page_nr = 0;
 761
 762	while (data_len) {
 763		len = min_t(size_t, data_len, RM_FW_PAGE_SIZE);
 764
 765		error = raydium_i2c_fw_write_page(client, page_nr++, data, len);
 766		if (error)
 767			return error;
 768
 769		msleep(20);
 770
 771		data += len;
 772		data_len -= len;
 773	}
 774
 775	error = raydium_i2c_leave_bl(client);
 776	if (error) {
 777		dev_err(&client->dev,
 778			"failed to leave boot loader: %d\n", error);
 779		return error;
 780	}
 781
 782	dev_dbg(&client->dev, "left boot loader mode\n");
 783	msleep(RM_BOOT_DELAY_MS);
 784
 785	error = raydium_i2c_check_fw_status(ts);
 786	if (error) {
 787		dev_err(&client->dev,
 788			"failed to check fw status after write: %d\n",
 789			error);
 790		return error;
 791	}
 792
 793	if (ts->boot_mode != RAYDIUM_TS_MAIN) {
 794		dev_err(&client->dev,
 795			"failed to switch to main fw after writing firmware: %d\n",
 796			error);
 797		return -EINVAL;
 798	}
 799
 800	error = raydium_i2c_fw_trigger(client);
 801	if (error) {
 802		dev_err(&client->dev, "failed to trigger fw: %d\n", error);
 803		return error;
 804	}
 805
 806	fw_checksum = raydium_calc_chksum(fw->data, fw->size);
 807
 808	error = raydium_i2c_write_checksum(client, fw->size, fw_checksum);
 809	if (error)
 810		return error;
 811
 812	return 0;
 813}
 814
 815static int raydium_i2c_fw_update(struct raydium_data *ts)
 816{
 817	struct i2c_client *client = ts->client;
 818	const struct firmware *fw = NULL;
 819	char *fw_file;
 820	int error;
 821
 822	fw_file = kasprintf(GFP_KERNEL, "raydium_%#04x.fw",
 823			    le32_to_cpu(ts->info.hw_ver));
 824	if (!fw_file)
 825		return -ENOMEM;
 826
 827	dev_dbg(&client->dev, "firmware name: %s\n", fw_file);
 828
 829	error = request_firmware(&fw, fw_file, &client->dev);
 830	if (error) {
 831		dev_err(&client->dev, "Unable to open firmware %s\n", fw_file);
 832		goto out_free_fw_file;
 833	}
 834
 835	disable_irq(client->irq);
 836
 837	error = raydium_i2c_do_update_firmware(ts, fw);
 838	if (error) {
 839		dev_err(&client->dev, "firmware update failed: %d\n", error);
 840		ts->boot_mode = RAYDIUM_TS_BLDR;
 841		goto out_enable_irq;
 842	}
 843
 844	error = raydium_i2c_initialize(ts);
 845	if (error) {
 846		dev_err(&client->dev,
 847			"failed to initialize device after firmware update: %d\n",
 848			error);
 849		ts->boot_mode = RAYDIUM_TS_BLDR;
 850		goto out_enable_irq;
 851	}
 852
 853	ts->boot_mode = RAYDIUM_TS_MAIN;
 854
 855out_enable_irq:
 856	enable_irq(client->irq);
 857	msleep(100);
 858
 859	release_firmware(fw);
 860
 861out_free_fw_file:
 862	kfree(fw_file);
 863
 864	return error;
 865}
 866
 867static void raydium_mt_event(struct raydium_data *ts)
 868{
 869	int i;
 870
 871	for (i = 0; i < ts->report_size / ts->contact_size; i++) {
 872		u8 *contact = &ts->report_data[ts->contact_size * i];
 873		bool state = contact[RM_CONTACT_STATE_POS];
 874		u8 wx, wy;
 875
 876		input_mt_slot(ts->input, i);
 877		input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, state);
 878
 879		if (!state)
 880			continue;
 881
 882		input_report_abs(ts->input, ABS_MT_POSITION_X,
 883				get_unaligned_le16(&contact[RM_CONTACT_X_POS]));
 884		input_report_abs(ts->input, ABS_MT_POSITION_Y,
 885				get_unaligned_le16(&contact[RM_CONTACT_Y_POS]));
 886		input_report_abs(ts->input, ABS_MT_PRESSURE,
 887				contact[RM_CONTACT_PRESSURE_POS]);
 888
 889		wx = contact[RM_CONTACT_WIDTH_X_POS];
 890		wy = contact[RM_CONTACT_WIDTH_Y_POS];
 891
 892		input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, max(wx, wy));
 893		input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, min(wx, wy));
 894	}
 895
 896	input_mt_sync_frame(ts->input);
 897	input_sync(ts->input);
 898}
 899
 900static irqreturn_t raydium_i2c_irq(int irq, void *_dev)
 901{
 902	struct raydium_data *ts = _dev;
 903	int error;
 904	u16 fw_crc;
 905	u16 calc_crc;
 906
 907	if (ts->boot_mode != RAYDIUM_TS_MAIN)
 908		goto out;
 909
 910	error = raydium_i2c_read(ts->client, ts->data_bank_addr,
 911				 ts->report_data, ts->pkg_size);
 912	if (error)
 913		goto out;
 914
 915	fw_crc = get_unaligned_le16(&ts->report_data[ts->report_size]);
 916	calc_crc = raydium_calc_chksum(ts->report_data, ts->report_size);
 917	if (unlikely(fw_crc != calc_crc)) {
 918		dev_warn(&ts->client->dev,
 919			 "%s: invalid packet crc %#04x vs %#04x\n",
 920			 __func__, calc_crc, fw_crc);
 921		goto out;
 922	}
 923
 924	raydium_mt_event(ts);
 925
 926out:
 927	return IRQ_HANDLED;
 928}
 929
 930static ssize_t raydium_i2c_fw_ver_show(struct device *dev,
 931				       struct device_attribute *attr, char *buf)
 932{
 933	struct i2c_client *client = to_i2c_client(dev);
 934	struct raydium_data *ts = i2c_get_clientdata(client);
 935
 936	return sprintf(buf, "%d.%d\n", ts->info.main_ver, ts->info.sub_ver);
 937}
 938
 939static ssize_t raydium_i2c_hw_ver_show(struct device *dev,
 940				       struct device_attribute *attr, char *buf)
 941{
 942	struct i2c_client *client = to_i2c_client(dev);
 943	struct raydium_data *ts = i2c_get_clientdata(client);
 944
 945	return sprintf(buf, "%#04x\n", le32_to_cpu(ts->info.hw_ver));
 946}
 947
 948static ssize_t raydium_i2c_boot_mode_show(struct device *dev,
 949					  struct device_attribute *attr,
 950					  char *buf)
 951{
 952	struct i2c_client *client = to_i2c_client(dev);
 953	struct raydium_data *ts = i2c_get_clientdata(client);
 954
 955	return sprintf(buf, "%s\n",
 956		       ts->boot_mode == RAYDIUM_TS_MAIN ?
 957				"Normal" : "Recovery");
 958}
 959
 960static ssize_t raydium_i2c_update_fw_store(struct device *dev,
 961					   struct device_attribute *attr,
 962					   const char *buf, size_t count)
 963{
 964	struct i2c_client *client = to_i2c_client(dev);
 965	struct raydium_data *ts = i2c_get_clientdata(client);
 966	int error;
 967
 968	error = mutex_lock_interruptible(&ts->sysfs_mutex);
 969	if (error)
 970		return error;
 971
 972	error = raydium_i2c_fw_update(ts);
 973
 974	mutex_unlock(&ts->sysfs_mutex);
 975
 976	return error ?: count;
 977}
 978
 979static ssize_t raydium_i2c_calibrate_store(struct device *dev,
 980					   struct device_attribute *attr,
 981					   const char *buf, size_t count)
 982{
 983	struct i2c_client *client = to_i2c_client(dev);
 984	struct raydium_data *ts = i2c_get_clientdata(client);
 985	static const u8 cal_cmd[] = { 0x00, 0x01, 0x9E };
 986	int error;
 987
 988	error = mutex_lock_interruptible(&ts->sysfs_mutex);
 989	if (error)
 990		return error;
 991
 992	error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
 993					 RAYDIUM_WAIT_READY);
 994	if (error)
 995		dev_err(&client->dev, "calibrate command failed: %d\n", error);
 996
 997	mutex_unlock(&ts->sysfs_mutex);
 998	return error ?: count;
 999}
1000
1001static DEVICE_ATTR(fw_version, S_IRUGO, raydium_i2c_fw_ver_show, NULL);
1002static DEVICE_ATTR(hw_version, S_IRUGO, raydium_i2c_hw_ver_show, NULL);
1003static DEVICE_ATTR(boot_mode, S_IRUGO, raydium_i2c_boot_mode_show, NULL);
1004static DEVICE_ATTR(update_fw, S_IWUSR, NULL, raydium_i2c_update_fw_store);
1005static DEVICE_ATTR(calibrate, S_IWUSR, NULL, raydium_i2c_calibrate_store);
1006
1007static struct attribute *raydium_i2c_attrs[] = {
1008	&dev_attr_update_fw.attr,
1009	&dev_attr_boot_mode.attr,
1010	&dev_attr_fw_version.attr,
1011	&dev_attr_hw_version.attr,
1012	&dev_attr_calibrate.attr,
1013	NULL
1014};
1015ATTRIBUTE_GROUPS(raydium_i2c);
 
 
 
1016
1017static int raydium_i2c_power_on(struct raydium_data *ts)
1018{
1019	int error;
1020
1021	if (!ts->reset_gpio)
1022		return 0;
1023
1024	gpiod_set_value_cansleep(ts->reset_gpio, 1);
1025
1026	error = regulator_enable(ts->avdd);
1027	if (error) {
1028		dev_err(&ts->client->dev,
1029			"failed to enable avdd regulator: %d\n", error);
1030		goto release_reset_gpio;
1031	}
1032
1033	error = regulator_enable(ts->vccio);
1034	if (error) {
1035		regulator_disable(ts->avdd);
1036		dev_err(&ts->client->dev,
1037			"failed to enable vccio regulator: %d\n", error);
1038		goto release_reset_gpio;
1039	}
1040
1041	udelay(RM_POWERON_DELAY_USEC);
1042
1043release_reset_gpio:
1044	gpiod_set_value_cansleep(ts->reset_gpio, 0);
1045
1046	if (error)
1047		return error;
1048
1049	msleep(RM_RESET_DELAY_MSEC);
1050
1051	return 0;
1052}
1053
1054static void raydium_i2c_power_off(void *_data)
1055{
1056	struct raydium_data *ts = _data;
1057
1058	if (ts->reset_gpio) {
1059		gpiod_set_value_cansleep(ts->reset_gpio, 1);
1060		regulator_disable(ts->vccio);
1061		regulator_disable(ts->avdd);
1062	}
1063}
1064
1065static int raydium_i2c_probe(struct i2c_client *client)
 
1066{
1067	union i2c_smbus_data dummy;
1068	struct raydium_data *ts;
1069	int error;
1070
1071	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1072		dev_err(&client->dev,
1073			"i2c check functionality error (need I2C_FUNC_I2C)\n");
1074		return -ENXIO;
1075	}
1076
1077	ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1078	if (!ts)
1079		return -ENOMEM;
1080
1081	mutex_init(&ts->sysfs_mutex);
1082
1083	ts->client = client;
1084	i2c_set_clientdata(client, ts);
1085
1086	ts->avdd = devm_regulator_get(&client->dev, "avdd");
1087	if (IS_ERR(ts->avdd))
1088		return dev_err_probe(&client->dev, PTR_ERR(ts->avdd),
1089				     "Failed to get 'avdd' regulator\n");
 
 
 
 
1090
1091	ts->vccio = devm_regulator_get(&client->dev, "vccio");
1092	if (IS_ERR(ts->vccio))
1093		return dev_err_probe(&client->dev, PTR_ERR(ts->vccio),
1094				     "Failed to get 'vccio' regulator\n");
 
 
 
 
1095
1096	ts->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
1097						 GPIOD_OUT_LOW);
1098	if (IS_ERR(ts->reset_gpio))
1099		return dev_err_probe(&client->dev, PTR_ERR(ts->reset_gpio),
1100				     "Failed to get reset gpio\n");
 
 
 
 
1101
1102	error = raydium_i2c_power_on(ts);
1103	if (error)
1104		return error;
1105
1106	error = devm_add_action_or_reset(&client->dev,
1107					 raydium_i2c_power_off, ts);
1108	if (error) {
1109		dev_err(&client->dev,
1110			"failed to install power off action: %d\n", error);
 
1111		return error;
1112	}
1113
1114	/* Make sure there is something at this address */
1115	if (i2c_smbus_xfer(client->adapter, client->addr, 0,
1116			   I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
1117		dev_err(&client->dev, "nothing at this address\n");
1118		return -ENXIO;
1119	}
1120
1121	error = raydium_i2c_initialize(ts);
1122	if (error) {
1123		dev_err(&client->dev, "failed to initialize: %d\n", error);
1124		return error;
1125	}
1126
1127	ts->report_data = devm_kmalloc(&client->dev,
1128				       ts->pkg_size, GFP_KERNEL);
1129	if (!ts->report_data)
1130		return -ENOMEM;
1131
1132	ts->input = devm_input_allocate_device(&client->dev);
1133	if (!ts->input) {
1134		dev_err(&client->dev, "Failed to allocate input device\n");
1135		return -ENOMEM;
1136	}
1137
1138	ts->input->name = "Raydium Touchscreen";
1139	ts->input->id.bustype = BUS_I2C;
1140
1141	input_set_abs_params(ts->input, ABS_MT_POSITION_X,
1142			     0, le16_to_cpu(ts->info.x_max), 0, 0);
1143	input_set_abs_params(ts->input, ABS_MT_POSITION_Y,
1144			     0, le16_to_cpu(ts->info.y_max), 0, 0);
1145	input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->info.x_res);
1146	input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->info.y_res);
1147
1148	input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1149	input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
1150
1151	error = input_mt_init_slots(ts->input, RM_MAX_TOUCH_NUM,
1152				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
1153	if (error) {
1154		dev_err(&client->dev,
1155			"failed to initialize MT slots: %d\n", error);
1156		return error;
1157	}
1158
1159	error = input_register_device(ts->input);
1160	if (error) {
1161		dev_err(&client->dev,
1162			"unable to register input device: %d\n", error);
1163		return error;
1164	}
1165
1166	error = devm_request_threaded_irq(&client->dev, client->irq,
1167					  NULL, raydium_i2c_irq,
1168					  IRQF_ONESHOT, client->name, ts);
1169	if (error) {
1170		dev_err(&client->dev, "Failed to register interrupt\n");
1171		return error;
1172	}
1173
 
 
 
 
 
 
 
 
1174	return 0;
1175}
1176
1177static void raydium_enter_sleep(struct i2c_client *client)
1178{
1179	static const u8 sleep_cmd[] = { 0x5A, 0xff, 0x00, 0x0f };
1180	int error;
1181
1182	error = raydium_i2c_send(client, RM_CMD_ENTER_SLEEP,
1183				 sleep_cmd, sizeof(sleep_cmd));
1184	if (error)
1185		dev_err(&client->dev,
1186			"sleep command failed: %d\n", error);
1187}
1188
1189static int raydium_i2c_suspend(struct device *dev)
1190{
1191	struct i2c_client *client = to_i2c_client(dev);
1192	struct raydium_data *ts = i2c_get_clientdata(client);
1193
1194	/* Sleep is not available in BLDR recovery mode */
1195	if (ts->boot_mode != RAYDIUM_TS_MAIN)
1196		return -EBUSY;
1197
1198	disable_irq(client->irq);
1199
1200	if (device_may_wakeup(dev)) {
1201		raydium_enter_sleep(client);
 
 
1202	} else {
1203		raydium_i2c_power_off(ts);
1204	}
1205
1206	return 0;
1207}
1208
1209static int raydium_i2c_resume(struct device *dev)
1210{
1211	struct i2c_client *client = to_i2c_client(dev);
1212	struct raydium_data *ts = i2c_get_clientdata(client);
1213
1214	if (device_may_wakeup(dev)) {
 
 
1215		raydium_i2c_sw_reset(client);
1216	} else {
1217		raydium_i2c_power_on(ts);
1218		raydium_i2c_initialize(ts);
1219	}
1220
1221	enable_irq(client->irq);
1222
1223	return 0;
1224}
1225
1226static DEFINE_SIMPLE_DEV_PM_OPS(raydium_i2c_pm_ops,
1227				raydium_i2c_suspend, raydium_i2c_resume);
1228
1229static const struct i2c_device_id raydium_i2c_id[] = {
1230	{ "raydium_i2c" },
1231	{ "rm32380" },
1232	{ /* sentinel */ }
1233};
1234MODULE_DEVICE_TABLE(i2c, raydium_i2c_id);
1235
1236#ifdef CONFIG_ACPI
1237static const struct acpi_device_id raydium_acpi_id[] = {
1238	{ "RAYD0001", 0 },
1239	{ /* sentinel */ }
1240};
1241MODULE_DEVICE_TABLE(acpi, raydium_acpi_id);
1242#endif
1243
1244#ifdef CONFIG_OF
1245static const struct of_device_id raydium_of_match[] = {
1246	{ .compatible = "raydium,rm32380", },
1247	{ /* sentinel */ }
1248};
1249MODULE_DEVICE_TABLE(of, raydium_of_match);
1250#endif
1251
1252static struct i2c_driver raydium_i2c_driver = {
1253	.probe = raydium_i2c_probe,
1254	.id_table = raydium_i2c_id,
1255	.driver = {
1256		.name = "raydium_ts",
1257		.dev_groups = raydium_i2c_groups,
1258		.pm = pm_sleep_ptr(&raydium_i2c_pm_ops),
1259		.acpi_match_table = ACPI_PTR(raydium_acpi_id),
1260		.of_match_table = of_match_ptr(raydium_of_match),
1261	},
1262};
1263module_i2c_driver(raydium_i2c_driver);
1264
1265MODULE_AUTHOR("Raydium");
1266MODULE_DESCRIPTION("Raydium I2c Touchscreen driver");
1267MODULE_LICENSE("GPL v2");
v5.14.15
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Raydium touchscreen I2C driver.
   4 *
   5 * Copyright (C) 2012-2014, Raydium Semiconductor Corporation.
   6 *
   7 * Raydium reserves the right to make changes without further notice
   8 * to the materials described herein. Raydium does not assume any
   9 * liability arising out of the application described herein.
  10 *
  11 * Contact Raydium Semiconductor Corporation at www.rad-ic.com
  12 */
  13
  14#include <linux/acpi.h>
  15#include <linux/delay.h>
  16#include <linux/firmware.h>
  17#include <linux/gpio/consumer.h>
  18#include <linux/i2c.h>
  19#include <linux/input.h>
  20#include <linux/input/mt.h>
  21#include <linux/interrupt.h>
  22#include <linux/module.h>
  23#include <linux/of.h>
 
  24#include <linux/regulator/consumer.h>
  25#include <linux/slab.h>
  26#include <asm/unaligned.h>
  27
  28/* Slave I2C mode */
  29#define RM_BOOT_BLDR		0x02
  30#define RM_BOOT_MAIN		0x03
  31
  32/* I2C bootoloader commands */
  33#define RM_CMD_BOOT_PAGE_WRT	0x0B		/* send bl page write */
  34#define RM_CMD_BOOT_WRT		0x11		/* send bl write */
  35#define RM_CMD_BOOT_ACK		0x22		/* send ack*/
  36#define RM_CMD_BOOT_CHK		0x33		/* send data check */
  37#define RM_CMD_BOOT_READ	0x44		/* send wait bl data ready*/
  38
  39#define RM_BOOT_RDY		0xFF		/* bl data ready */
 
  40
  41/* I2C main commands */
  42#define RM_CMD_QUERY_BANK	0x2B
  43#define RM_CMD_DATA_BANK	0x4D
  44#define RM_CMD_ENTER_SLEEP	0x4E
  45#define RM_CMD_BANK_SWITCH	0xAA
  46
  47#define RM_RESET_MSG_ADDR	0x40000004
  48
  49#define RM_MAX_READ_SIZE	56
  50#define RM_PACKET_CRC_SIZE	2
  51
  52/* Touch relative info */
  53#define RM_MAX_RETRIES		3
  54#define RM_RETRY_DELAY_MS	20
  55#define RM_MAX_TOUCH_NUM	10
  56#define RM_BOOT_DELAY_MS	100
  57
  58/* Offsets in contact data */
  59#define RM_CONTACT_STATE_POS	0
  60#define RM_CONTACT_X_POS	1
  61#define RM_CONTACT_Y_POS	3
  62#define RM_CONTACT_PRESSURE_POS	5
  63#define RM_CONTACT_WIDTH_X_POS	6
  64#define RM_CONTACT_WIDTH_Y_POS	7
  65
  66/* Bootloader relative info */
  67#define RM_BL_WRT_CMD_SIZE	3	/* bl flash wrt cmd size */
  68#define RM_BL_WRT_PKG_SIZE	32	/* bl wrt pkg size */
  69#define RM_BL_WRT_LEN		(RM_BL_WRT_PKG_SIZE + RM_BL_WRT_CMD_SIZE)
  70#define RM_FW_PAGE_SIZE		128
  71#define RM_MAX_FW_RETRIES	30
  72#define RM_MAX_FW_SIZE		0xD000
  73
  74#define RM_POWERON_DELAY_USEC	500
  75#define RM_RESET_DELAY_MSEC	50
  76
  77enum raydium_bl_cmd {
  78	BL_HEADER = 0,
  79	BL_PAGE_STR,
  80	BL_PKG_IDX,
  81	BL_DATA_STR,
  82};
  83
  84enum raydium_bl_ack {
  85	RAYDIUM_ACK_NULL = 0,
  86	RAYDIUM_WAIT_READY,
  87	RAYDIUM_PATH_READY,
  88};
  89
  90enum raydium_boot_mode {
  91	RAYDIUM_TS_MAIN = 0,
  92	RAYDIUM_TS_BLDR,
  93};
  94
  95/* Response to RM_CMD_DATA_BANK request */
  96struct raydium_data_info {
  97	__le32 data_bank_addr;
  98	u8 pkg_size;
  99	u8 tp_info_size;
 100};
 101
 102struct raydium_info {
 103	__le32 hw_ver;		/*device version */
 104	u8 main_ver;
 105	u8 sub_ver;
 106	__le16 ft_ver;		/* test version */
 107	u8 x_num;
 108	u8 y_num;
 109	__le16 x_max;
 110	__le16 y_max;
 111	u8 x_res;		/* units/mm */
 112	u8 y_res;		/* units/mm */
 113};
 114
 115/* struct raydium_data - represents state of Raydium touchscreen device */
 116struct raydium_data {
 117	struct i2c_client *client;
 118	struct input_dev *input;
 119
 120	struct regulator *avdd;
 121	struct regulator *vccio;
 122	struct gpio_desc *reset_gpio;
 123
 124	struct raydium_info info;
 125
 126	struct mutex sysfs_mutex;
 127
 128	u8 *report_data;
 129
 130	u32 data_bank_addr;
 131	u8 report_size;
 132	u8 contact_size;
 133	u8 pkg_size;
 134
 135	enum raydium_boot_mode boot_mode;
 136
 137	bool wake_irq_enabled;
 138};
 139
 140/*
 141 * Header to be sent for RM_CMD_BANK_SWITCH command. This is used by
 142 * raydium_i2c_{read|send} below.
 143 */
 144struct __packed raydium_bank_switch_header {
 145	u8 cmd;
 146	__be32 be_addr;
 147};
 148
 149static int raydium_i2c_xfer(struct i2c_client *client, u32 addr,
 150			    struct i2c_msg *xfer, size_t xfer_count)
 151{
 152	int ret;
 153	/*
 154	 * If address is greater than 255, then RM_CMD_BANK_SWITCH needs to be
 155	 * sent first. Else, skip the header i.e. xfer[0].
 156	 */
 157	int xfer_start_idx = (addr > 0xff) ? 0 : 1;
 158	xfer_count -= xfer_start_idx;
 159
 160	ret = i2c_transfer(client->adapter, &xfer[xfer_start_idx], xfer_count);
 161	if (likely(ret == xfer_count))
 162		return 0;
 163
 164	return ret < 0 ? ret : -EIO;
 165}
 166
 167static int raydium_i2c_send(struct i2c_client *client,
 168			    u32 addr, const void *data, size_t len)
 169{
 170	int tries = 0;
 171	int error;
 172	u8 *tx_buf;
 173	u8 reg_addr = addr & 0xff;
 174
 175	tx_buf = kmalloc(len + 1, GFP_KERNEL);
 176	if (!tx_buf)
 177		return -ENOMEM;
 178
 179	tx_buf[0] = reg_addr;
 180	memcpy(tx_buf + 1, data, len);
 181
 182	do {
 183		struct raydium_bank_switch_header header = {
 184			.cmd = RM_CMD_BANK_SWITCH,
 185			.be_addr = cpu_to_be32(addr),
 186		};
 187
 188		/*
 189		 * Perform as a single i2c_transfer transaction to ensure that
 190		 * no other I2C transactions are initiated on the bus to any
 191		 * other device in between. Initiating transacations to other
 192		 * devices after RM_CMD_BANK_SWITCH is sent is known to cause
 193		 * issues. This is also why regmap infrastructure cannot be used
 194		 * for this driver. Regmap handles page(bank) switch and reads
 195		 * as separate i2c_transfer() operations. This can result in
 196		 * problems if the Raydium device is on a shared I2C bus.
 197		 */
 198		struct i2c_msg xfer[] = {
 199			{
 200				.addr = client->addr,
 201				.len = sizeof(header),
 202				.buf = (u8 *)&header,
 203			},
 204			{
 205				.addr = client->addr,
 206				.len = len + 1,
 207				.buf = tx_buf,
 208			},
 209		};
 210
 211		error = raydium_i2c_xfer(client, addr, xfer, ARRAY_SIZE(xfer));
 212		if (likely(!error))
 213			return 0;
 214
 215		msleep(RM_RETRY_DELAY_MS);
 216	} while (++tries < RM_MAX_RETRIES);
 217
 218	dev_err(&client->dev, "%s failed: %d\n", __func__, error);
 
 
 219	return error;
 220}
 221
 222static int raydium_i2c_read(struct i2c_client *client,
 223			    u32 addr, void *data, size_t len)
 224{
 225	int error;
 226
 227	while (len) {
 228		u8 reg_addr = addr & 0xff;
 229		struct raydium_bank_switch_header header = {
 230			.cmd = RM_CMD_BANK_SWITCH,
 231			.be_addr = cpu_to_be32(addr),
 232		};
 233		size_t xfer_len = min_t(size_t, len, RM_MAX_READ_SIZE);
 234
 235		/*
 236		 * Perform as a single i2c_transfer transaction to ensure that
 237		 * no other I2C transactions are initiated on the bus to any
 238		 * other device in between. Initiating transacations to other
 239		 * devices after RM_CMD_BANK_SWITCH is sent is known to cause
 240		 * issues. This is also why regmap infrastructure cannot be used
 241		 * for this driver. Regmap handles page(bank) switch and writes
 242		 * as separate i2c_transfer() operations. This can result in
 243		 * problems if the Raydium device is on a shared I2C bus.
 244		 */
 245		struct i2c_msg xfer[] = {
 246			{
 247				.addr = client->addr,
 248				.len = sizeof(header),
 249				.buf = (u8 *)&header,
 250			},
 251			{
 252				.addr = client->addr,
 253				.len = 1,
 254				.buf = &reg_addr,
 255			},
 256			{
 257				.addr = client->addr,
 258				.len = xfer_len,
 259				.buf = data,
 260				.flags = I2C_M_RD,
 261			}
 262		};
 263
 264		error = raydium_i2c_xfer(client, addr, xfer, ARRAY_SIZE(xfer));
 265		if (unlikely(error))
 266			return error;
 267
 268		len -= xfer_len;
 269		data += xfer_len;
 270		addr += xfer_len;
 271	}
 272
 273	return 0;
 274}
 275
 276static int raydium_i2c_sw_reset(struct i2c_client *client)
 277{
 278	const u8 soft_rst_cmd = 0x01;
 279	int error;
 280
 281	error = raydium_i2c_send(client, RM_RESET_MSG_ADDR, &soft_rst_cmd,
 282				 sizeof(soft_rst_cmd));
 283	if (error) {
 284		dev_err(&client->dev, "software reset failed: %d\n", error);
 285		return error;
 286	}
 287
 288	msleep(RM_RESET_DELAY_MSEC);
 289
 290	return 0;
 291}
 292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 293static int raydium_i2c_query_ts_info(struct raydium_data *ts)
 294{
 295	struct i2c_client *client = ts->client;
 296	struct raydium_data_info data_info;
 297	__le32 query_bank_addr;
 298
 299	int error, retry_cnt;
 300
 301	for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) {
 302		error = raydium_i2c_read(client, RM_CMD_DATA_BANK,
 303					 &data_info, sizeof(data_info));
 304		if (error)
 305			continue;
 306
 307		/*
 308		 * Warn user if we already allocated memory for reports and
 309		 * then the size changed (due to firmware update?) and keep
 310		 * old size instead.
 311		 */
 312		if (ts->report_data && ts->pkg_size != data_info.pkg_size) {
 313			dev_warn(&client->dev,
 314				 "report size changes, was: %d, new: %d\n",
 315				 ts->pkg_size, data_info.pkg_size);
 316		} else {
 317			ts->pkg_size = data_info.pkg_size;
 318			ts->report_size = ts->pkg_size - RM_PACKET_CRC_SIZE;
 319		}
 320
 321		ts->contact_size = data_info.tp_info_size;
 322		ts->data_bank_addr = le32_to_cpu(data_info.data_bank_addr);
 323
 324		dev_dbg(&client->dev,
 325			"data_bank_addr: %#08x, report_size: %d, contact_size: %d\n",
 326			ts->data_bank_addr, ts->report_size, ts->contact_size);
 327
 328		error = raydium_i2c_read(client, RM_CMD_QUERY_BANK,
 329					 &query_bank_addr,
 330					 sizeof(query_bank_addr));
 331		if (error)
 332			continue;
 333
 334		error = raydium_i2c_read(client, le32_to_cpu(query_bank_addr),
 335					 &ts->info, sizeof(ts->info));
 336		if (error)
 337			continue;
 338
 339		return 0;
 340	}
 341
 342	dev_err(&client->dev, "failed to query device parameters: %d\n", error);
 343	return error;
 344}
 345
 346static int raydium_i2c_check_fw_status(struct raydium_data *ts)
 347{
 348	struct i2c_client *client = ts->client;
 349	static const u8 bl_ack = 0x62;
 350	static const u8 main_ack = 0x66;
 351	u8 buf[4];
 352	int error;
 353
 354	error = raydium_i2c_read(client, RM_CMD_BOOT_READ, buf, sizeof(buf));
 355	if (!error) {
 356		if (buf[0] == bl_ack)
 357			ts->boot_mode = RAYDIUM_TS_BLDR;
 358		else if (buf[0] == main_ack)
 359			ts->boot_mode = RAYDIUM_TS_MAIN;
 360		return 0;
 361	}
 362
 363	return error;
 364}
 365
 366static int raydium_i2c_initialize(struct raydium_data *ts)
 367{
 368	struct i2c_client *client = ts->client;
 369	int error, retry_cnt;
 370
 371	for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) {
 372		/* Wait for Hello packet */
 373		msleep(RM_BOOT_DELAY_MS);
 374
 375		error = raydium_i2c_check_fw_status(ts);
 376		if (error) {
 377			dev_err(&client->dev,
 378				"failed to read 'hello' packet: %d\n", error);
 379			continue;
 380		}
 381
 382		if (ts->boot_mode == RAYDIUM_TS_BLDR ||
 383		    ts->boot_mode == RAYDIUM_TS_MAIN) {
 384			break;
 385		}
 386	}
 387
 388	if (error)
 389		ts->boot_mode = RAYDIUM_TS_BLDR;
 390
 391	if (ts->boot_mode == RAYDIUM_TS_BLDR) {
 392		ts->info.hw_ver = cpu_to_le32(0xffffffffUL);
 393		ts->info.main_ver = 0xff;
 394		ts->info.sub_ver = 0xff;
 395	} else {
 396		raydium_i2c_query_ts_info(ts);
 397	}
 398
 399	return error;
 400}
 401
 402static int raydium_i2c_bl_chk_state(struct i2c_client *client,
 403				    enum raydium_bl_ack state)
 404{
 405	static const u8 ack_ok[] = { 0xFF, 0x39, 0x30, 0x30, 0x54 };
 406	u8 rbuf[sizeof(ack_ok)];
 407	u8 retry;
 408	int error;
 409
 410	for (retry = 0; retry < RM_MAX_FW_RETRIES; retry++) {
 411		switch (state) {
 412		case RAYDIUM_ACK_NULL:
 413			return 0;
 414
 415		case RAYDIUM_WAIT_READY:
 416			error = raydium_i2c_read(client, RM_CMD_BOOT_CHK,
 417						 &rbuf[0], 1);
 418			if (!error && rbuf[0] == RM_BOOT_RDY)
 419				return 0;
 420
 421			break;
 422
 423		case RAYDIUM_PATH_READY:
 424			error = raydium_i2c_read(client, RM_CMD_BOOT_CHK,
 425						 rbuf, sizeof(rbuf));
 426			if (!error && !memcmp(rbuf, ack_ok, sizeof(ack_ok)))
 427				return 0;
 428
 429			break;
 430
 431		default:
 432			dev_err(&client->dev, "%s: invalid target state %d\n",
 433				__func__, state);
 434			return -EINVAL;
 435		}
 436
 437		msleep(20);
 438	}
 439
 440	return -ETIMEDOUT;
 441}
 442
 443static int raydium_i2c_write_object(struct i2c_client *client,
 444				    const void *data, size_t len,
 445				    enum raydium_bl_ack state)
 446{
 447	int error;
 448	static const u8 cmd[] = { 0xFF, 0x39 };
 449
 450	error = raydium_i2c_send(client, RM_CMD_BOOT_WRT, data, len);
 451	if (error) {
 452		dev_err(&client->dev, "WRT obj command failed: %d\n",
 453			error);
 454		return error;
 455	}
 456
 457	error = raydium_i2c_send(client, RM_CMD_BOOT_ACK, cmd, sizeof(cmd));
 458	if (error) {
 459		dev_err(&client->dev, "Ack obj command failed: %d\n", error);
 460		return error;
 461	}
 462
 463	error = raydium_i2c_bl_chk_state(client, state);
 464	if (error) {
 465		dev_err(&client->dev, "BL check state failed: %d\n", error);
 466		return error;
 467	}
 468	return 0;
 469}
 470
 471static int raydium_i2c_boot_trigger(struct i2c_client *client)
 472{
 473	static const u8 cmd[7][6] = {
 474		{ 0x08, 0x0C, 0x09, 0x00, 0x50, 0xD7 },
 475		{ 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
 476		{ 0x08, 0x04, 0x09, 0x00, 0x50, 0x00 },
 477		{ 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
 478		{ 0x08, 0x0C, 0x09, 0x00, 0x50, 0x00 },
 479		{ 0x06, 0x01, 0x00, 0x00, 0x00, 0x00 },
 480		{ 0x02, 0xA2, 0x00, 0x00, 0x00, 0x00 },
 481	};
 482	int i;
 483	int error;
 484
 485	for (i = 0; i < 7; i++) {
 486		error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
 487						 RAYDIUM_WAIT_READY);
 488		if (error) {
 489			dev_err(&client->dev,
 490				"boot trigger failed at step %d: %d\n",
 491				i, error);
 492			return error;
 493		}
 494	}
 495
 496	return 0;
 497}
 498
 499static int raydium_i2c_fw_trigger(struct i2c_client *client)
 500{
 501	static const u8 cmd[5][11] = {
 502		{ 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0xD7, 0, 0, 0 },
 503		{ 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
 504		{ 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
 505		{ 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
 506		{ 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
 507	};
 508	int i;
 509	int error;
 510
 511	for (i = 0; i < 5; i++) {
 512		error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
 513						 RAYDIUM_ACK_NULL);
 514		if (error) {
 515			dev_err(&client->dev,
 516				"fw trigger failed at step %d: %d\n",
 517				i, error);
 518			return error;
 519		}
 520	}
 521
 522	return 0;
 523}
 524
 525static int raydium_i2c_check_path(struct i2c_client *client)
 526{
 527	static const u8 cmd[] = { 0x09, 0x00, 0x09, 0x00, 0x50, 0x10, 0x00 };
 528	int error;
 529
 530	error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
 531					 RAYDIUM_PATH_READY);
 532	if (error) {
 533		dev_err(&client->dev, "check path command failed: %d\n", error);
 534		return error;
 535	}
 536
 537	return 0;
 538}
 539
 540static int raydium_i2c_enter_bl(struct i2c_client *client)
 541{
 542	static const u8 cal_cmd[] = { 0x00, 0x01, 0x52 };
 543	int error;
 544
 545	error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
 546					 RAYDIUM_ACK_NULL);
 547	if (error) {
 548		dev_err(&client->dev, "enter bl command failed: %d\n", error);
 549		return error;
 550	}
 551
 552	msleep(RM_BOOT_DELAY_MS);
 553	return 0;
 554}
 555
 556static int raydium_i2c_leave_bl(struct i2c_client *client)
 557{
 558	static const u8 leave_cmd[] = { 0x05, 0x00 };
 559	int error;
 560
 561	error = raydium_i2c_write_object(client, leave_cmd, sizeof(leave_cmd),
 562					 RAYDIUM_ACK_NULL);
 563	if (error) {
 564		dev_err(&client->dev, "leave bl command failed: %d\n", error);
 565		return error;
 566	}
 567
 568	msleep(RM_BOOT_DELAY_MS);
 569	return 0;
 570}
 571
 572static int raydium_i2c_write_checksum(struct i2c_client *client,
 573				      size_t length, u16 checksum)
 574{
 575	u8 checksum_cmd[] = { 0x00, 0x05, 0x6D, 0x00, 0x00, 0x00, 0x00 };
 576	int error;
 577
 578	put_unaligned_le16(length, &checksum_cmd[3]);
 579	put_unaligned_le16(checksum, &checksum_cmd[5]);
 580
 581	error = raydium_i2c_write_object(client,
 582					 checksum_cmd, sizeof(checksum_cmd),
 583					 RAYDIUM_ACK_NULL);
 584	if (error) {
 585		dev_err(&client->dev, "failed to write checksum: %d\n",
 586			error);
 587		return error;
 588	}
 589
 590	return 0;
 591}
 592
 593static int raydium_i2c_disable_watch_dog(struct i2c_client *client)
 594{
 595	static const u8 cmd[] = { 0x0A, 0xAA };
 596	int error;
 597
 598	error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
 599					 RAYDIUM_WAIT_READY);
 600	if (error) {
 601		dev_err(&client->dev, "disable watchdog command failed: %d\n",
 602			error);
 603		return error;
 604	}
 605
 606	return 0;
 607}
 608
 609static int raydium_i2c_fw_write_page(struct i2c_client *client,
 610				     u16 page_idx, const void *data, size_t len)
 611{
 612	u8 buf[RM_BL_WRT_LEN];
 613	size_t xfer_len;
 614	int error;
 615	int i;
 616
 617	BUILD_BUG_ON((RM_FW_PAGE_SIZE % RM_BL_WRT_PKG_SIZE) != 0);
 618
 619	for (i = 0; i < RM_FW_PAGE_SIZE / RM_BL_WRT_PKG_SIZE; i++) {
 620		buf[BL_HEADER] = RM_CMD_BOOT_PAGE_WRT;
 621		buf[BL_PAGE_STR] = page_idx ? 0xff : 0;
 622		buf[BL_PKG_IDX] = i + 1;
 623
 624		xfer_len = min_t(size_t, len, RM_BL_WRT_PKG_SIZE);
 625		memcpy(&buf[BL_DATA_STR], data, xfer_len);
 626		if (len < RM_BL_WRT_PKG_SIZE)
 627			memset(&buf[BL_DATA_STR + xfer_len], 0xff,
 628				RM_BL_WRT_PKG_SIZE - xfer_len);
 629
 630		error = raydium_i2c_write_object(client, buf, RM_BL_WRT_LEN,
 631						 RAYDIUM_WAIT_READY);
 632		if (error) {
 633			dev_err(&client->dev,
 634				"page write command failed for page %d, chunk %d: %d\n",
 635				page_idx, i, error);
 636			return error;
 637		}
 638
 639		data += xfer_len;
 640		len -= xfer_len;
 641	}
 642
 643	return error;
 644}
 645
 646static u16 raydium_calc_chksum(const u8 *buf, u16 len)
 647{
 648	u16 checksum = 0;
 649	u16 i;
 650
 651	for (i = 0; i < len; i++)
 652		checksum += buf[i];
 653
 654	return checksum;
 655}
 656
 657static int raydium_i2c_do_update_firmware(struct raydium_data *ts,
 658					 const struct firmware *fw)
 659{
 660	struct i2c_client *client = ts->client;
 661	const void *data;
 662	size_t data_len;
 663	size_t len;
 664	int page_nr;
 665	int i;
 666	int error;
 667	u16 fw_checksum;
 668
 669	if (fw->size == 0 || fw->size > RM_MAX_FW_SIZE) {
 670		dev_err(&client->dev, "Invalid firmware length\n");
 671		return -EINVAL;
 672	}
 673
 674	error = raydium_i2c_check_fw_status(ts);
 675	if (error) {
 676		dev_err(&client->dev, "Unable to access IC %d\n", error);
 677		return error;
 678	}
 679
 680	if (ts->boot_mode == RAYDIUM_TS_MAIN) {
 681		for (i = 0; i < RM_MAX_RETRIES; i++) {
 682			error = raydium_i2c_enter_bl(client);
 683			if (!error) {
 684				error = raydium_i2c_check_fw_status(ts);
 685				if (error) {
 686					dev_err(&client->dev,
 687						"unable to access IC: %d\n",
 688						error);
 689					return error;
 690				}
 691
 692				if (ts->boot_mode == RAYDIUM_TS_BLDR)
 693					break;
 694			}
 695		}
 696
 697		if (ts->boot_mode == RAYDIUM_TS_MAIN) {
 698			dev_err(&client->dev,
 699				"failed to jump to boot loader: %d\n",
 700				error);
 701			return -EIO;
 702		}
 703	}
 704
 705	error = raydium_i2c_disable_watch_dog(client);
 706	if (error)
 707		return error;
 708
 709	error = raydium_i2c_check_path(client);
 710	if (error)
 711		return error;
 712
 713	error = raydium_i2c_boot_trigger(client);
 714	if (error) {
 715		dev_err(&client->dev, "send boot trigger fail: %d\n", error);
 716		return error;
 717	}
 718
 719	msleep(RM_BOOT_DELAY_MS);
 720
 721	data = fw->data;
 722	data_len = fw->size;
 723	page_nr = 0;
 724
 725	while (data_len) {
 726		len = min_t(size_t, data_len, RM_FW_PAGE_SIZE);
 727
 728		error = raydium_i2c_fw_write_page(client, page_nr++, data, len);
 729		if (error)
 730			return error;
 731
 732		msleep(20);
 733
 734		data += len;
 735		data_len -= len;
 736	}
 737
 738	error = raydium_i2c_leave_bl(client);
 739	if (error) {
 740		dev_err(&client->dev,
 741			"failed to leave boot loader: %d\n", error);
 742		return error;
 743	}
 744
 745	dev_dbg(&client->dev, "left boot loader mode\n");
 746	msleep(RM_BOOT_DELAY_MS);
 747
 748	error = raydium_i2c_check_fw_status(ts);
 749	if (error) {
 750		dev_err(&client->dev,
 751			"failed to check fw status after write: %d\n",
 752			error);
 753		return error;
 754	}
 755
 756	if (ts->boot_mode != RAYDIUM_TS_MAIN) {
 757		dev_err(&client->dev,
 758			"failed to switch to main fw after writing firmware: %d\n",
 759			error);
 760		return -EINVAL;
 761	}
 762
 763	error = raydium_i2c_fw_trigger(client);
 764	if (error) {
 765		dev_err(&client->dev, "failed to trigger fw: %d\n", error);
 766		return error;
 767	}
 768
 769	fw_checksum = raydium_calc_chksum(fw->data, fw->size);
 770
 771	error = raydium_i2c_write_checksum(client, fw->size, fw_checksum);
 772	if (error)
 773		return error;
 774
 775	return 0;
 776}
 777
 778static int raydium_i2c_fw_update(struct raydium_data *ts)
 779{
 780	struct i2c_client *client = ts->client;
 781	const struct firmware *fw = NULL;
 782	char *fw_file;
 783	int error;
 784
 785	fw_file = kasprintf(GFP_KERNEL, "raydium_%#04x.fw",
 786			    le32_to_cpu(ts->info.hw_ver));
 787	if (!fw_file)
 788		return -ENOMEM;
 789
 790	dev_dbg(&client->dev, "firmware name: %s\n", fw_file);
 791
 792	error = request_firmware(&fw, fw_file, &client->dev);
 793	if (error) {
 794		dev_err(&client->dev, "Unable to open firmware %s\n", fw_file);
 795		goto out_free_fw_file;
 796	}
 797
 798	disable_irq(client->irq);
 799
 800	error = raydium_i2c_do_update_firmware(ts, fw);
 801	if (error) {
 802		dev_err(&client->dev, "firmware update failed: %d\n", error);
 803		ts->boot_mode = RAYDIUM_TS_BLDR;
 804		goto out_enable_irq;
 805	}
 806
 807	error = raydium_i2c_initialize(ts);
 808	if (error) {
 809		dev_err(&client->dev,
 810			"failed to initialize device after firmware update: %d\n",
 811			error);
 812		ts->boot_mode = RAYDIUM_TS_BLDR;
 813		goto out_enable_irq;
 814	}
 815
 816	ts->boot_mode = RAYDIUM_TS_MAIN;
 817
 818out_enable_irq:
 819	enable_irq(client->irq);
 820	msleep(100);
 821
 822	release_firmware(fw);
 823
 824out_free_fw_file:
 825	kfree(fw_file);
 826
 827	return error;
 828}
 829
 830static void raydium_mt_event(struct raydium_data *ts)
 831{
 832	int i;
 833
 834	for (i = 0; i < ts->report_size / ts->contact_size; i++) {
 835		u8 *contact = &ts->report_data[ts->contact_size * i];
 836		bool state = contact[RM_CONTACT_STATE_POS];
 837		u8 wx, wy;
 838
 839		input_mt_slot(ts->input, i);
 840		input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, state);
 841
 842		if (!state)
 843			continue;
 844
 845		input_report_abs(ts->input, ABS_MT_POSITION_X,
 846				get_unaligned_le16(&contact[RM_CONTACT_X_POS]));
 847		input_report_abs(ts->input, ABS_MT_POSITION_Y,
 848				get_unaligned_le16(&contact[RM_CONTACT_Y_POS]));
 849		input_report_abs(ts->input, ABS_MT_PRESSURE,
 850				contact[RM_CONTACT_PRESSURE_POS]);
 851
 852		wx = contact[RM_CONTACT_WIDTH_X_POS];
 853		wy = contact[RM_CONTACT_WIDTH_Y_POS];
 854
 855		input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, max(wx, wy));
 856		input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, min(wx, wy));
 857	}
 858
 859	input_mt_sync_frame(ts->input);
 860	input_sync(ts->input);
 861}
 862
 863static irqreturn_t raydium_i2c_irq(int irq, void *_dev)
 864{
 865	struct raydium_data *ts = _dev;
 866	int error;
 867	u16 fw_crc;
 868	u16 calc_crc;
 869
 870	if (ts->boot_mode != RAYDIUM_TS_MAIN)
 871		goto out;
 872
 873	error = raydium_i2c_read(ts->client, ts->data_bank_addr,
 874				 ts->report_data, ts->pkg_size);
 875	if (error)
 876		goto out;
 877
 878	fw_crc = get_unaligned_le16(&ts->report_data[ts->report_size]);
 879	calc_crc = raydium_calc_chksum(ts->report_data, ts->report_size);
 880	if (unlikely(fw_crc != calc_crc)) {
 881		dev_warn(&ts->client->dev,
 882			 "%s: invalid packet crc %#04x vs %#04x\n",
 883			 __func__, calc_crc, fw_crc);
 884		goto out;
 885	}
 886
 887	raydium_mt_event(ts);
 888
 889out:
 890	return IRQ_HANDLED;
 891}
 892
 893static ssize_t raydium_i2c_fw_ver_show(struct device *dev,
 894				       struct device_attribute *attr, char *buf)
 895{
 896	struct i2c_client *client = to_i2c_client(dev);
 897	struct raydium_data *ts = i2c_get_clientdata(client);
 898
 899	return sprintf(buf, "%d.%d\n", ts->info.main_ver, ts->info.sub_ver);
 900}
 901
 902static ssize_t raydium_i2c_hw_ver_show(struct device *dev,
 903				       struct device_attribute *attr, char *buf)
 904{
 905	struct i2c_client *client = to_i2c_client(dev);
 906	struct raydium_data *ts = i2c_get_clientdata(client);
 907
 908	return sprintf(buf, "%#04x\n", le32_to_cpu(ts->info.hw_ver));
 909}
 910
 911static ssize_t raydium_i2c_boot_mode_show(struct device *dev,
 912					  struct device_attribute *attr,
 913					  char *buf)
 914{
 915	struct i2c_client *client = to_i2c_client(dev);
 916	struct raydium_data *ts = i2c_get_clientdata(client);
 917
 918	return sprintf(buf, "%s\n",
 919		       ts->boot_mode == RAYDIUM_TS_MAIN ?
 920				"Normal" : "Recovery");
 921}
 922
 923static ssize_t raydium_i2c_update_fw_store(struct device *dev,
 924					   struct device_attribute *attr,
 925					   const char *buf, size_t count)
 926{
 927	struct i2c_client *client = to_i2c_client(dev);
 928	struct raydium_data *ts = i2c_get_clientdata(client);
 929	int error;
 930
 931	error = mutex_lock_interruptible(&ts->sysfs_mutex);
 932	if (error)
 933		return error;
 934
 935	error = raydium_i2c_fw_update(ts);
 936
 937	mutex_unlock(&ts->sysfs_mutex);
 938
 939	return error ?: count;
 940}
 941
 942static ssize_t raydium_i2c_calibrate_store(struct device *dev,
 943					   struct device_attribute *attr,
 944					   const char *buf, size_t count)
 945{
 946	struct i2c_client *client = to_i2c_client(dev);
 947	struct raydium_data *ts = i2c_get_clientdata(client);
 948	static const u8 cal_cmd[] = { 0x00, 0x01, 0x9E };
 949	int error;
 950
 951	error = mutex_lock_interruptible(&ts->sysfs_mutex);
 952	if (error)
 953		return error;
 954
 955	error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
 956					 RAYDIUM_WAIT_READY);
 957	if (error)
 958		dev_err(&client->dev, "calibrate command failed: %d\n", error);
 959
 960	mutex_unlock(&ts->sysfs_mutex);
 961	return error ?: count;
 962}
 963
 964static DEVICE_ATTR(fw_version, S_IRUGO, raydium_i2c_fw_ver_show, NULL);
 965static DEVICE_ATTR(hw_version, S_IRUGO, raydium_i2c_hw_ver_show, NULL);
 966static DEVICE_ATTR(boot_mode, S_IRUGO, raydium_i2c_boot_mode_show, NULL);
 967static DEVICE_ATTR(update_fw, S_IWUSR, NULL, raydium_i2c_update_fw_store);
 968static DEVICE_ATTR(calibrate, S_IWUSR, NULL, raydium_i2c_calibrate_store);
 969
 970static struct attribute *raydium_i2c_attributes[] = {
 971	&dev_attr_update_fw.attr,
 972	&dev_attr_boot_mode.attr,
 973	&dev_attr_fw_version.attr,
 974	&dev_attr_hw_version.attr,
 975	&dev_attr_calibrate.attr,
 976	NULL
 977};
 978
 979static const struct attribute_group raydium_i2c_attribute_group = {
 980	.attrs = raydium_i2c_attributes,
 981};
 982
 983static int raydium_i2c_power_on(struct raydium_data *ts)
 984{
 985	int error;
 986
 987	if (!ts->reset_gpio)
 988		return 0;
 989
 990	gpiod_set_value_cansleep(ts->reset_gpio, 1);
 991
 992	error = regulator_enable(ts->avdd);
 993	if (error) {
 994		dev_err(&ts->client->dev,
 995			"failed to enable avdd regulator: %d\n", error);
 996		goto release_reset_gpio;
 997	}
 998
 999	error = regulator_enable(ts->vccio);
1000	if (error) {
1001		regulator_disable(ts->avdd);
1002		dev_err(&ts->client->dev,
1003			"failed to enable vccio regulator: %d\n", error);
1004		goto release_reset_gpio;
1005	}
1006
1007	udelay(RM_POWERON_DELAY_USEC);
1008
1009release_reset_gpio:
1010	gpiod_set_value_cansleep(ts->reset_gpio, 0);
1011
1012	if (error)
1013		return error;
1014
1015	msleep(RM_RESET_DELAY_MSEC);
1016
1017	return 0;
1018}
1019
1020static void raydium_i2c_power_off(void *_data)
1021{
1022	struct raydium_data *ts = _data;
1023
1024	if (ts->reset_gpio) {
1025		gpiod_set_value_cansleep(ts->reset_gpio, 1);
1026		regulator_disable(ts->vccio);
1027		regulator_disable(ts->avdd);
1028	}
1029}
1030
1031static int raydium_i2c_probe(struct i2c_client *client,
1032			     const struct i2c_device_id *id)
1033{
1034	union i2c_smbus_data dummy;
1035	struct raydium_data *ts;
1036	int error;
1037
1038	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1039		dev_err(&client->dev,
1040			"i2c check functionality error (need I2C_FUNC_I2C)\n");
1041		return -ENXIO;
1042	}
1043
1044	ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1045	if (!ts)
1046		return -ENOMEM;
1047
1048	mutex_init(&ts->sysfs_mutex);
1049
1050	ts->client = client;
1051	i2c_set_clientdata(client, ts);
1052
1053	ts->avdd = devm_regulator_get(&client->dev, "avdd");
1054	if (IS_ERR(ts->avdd)) {
1055		error = PTR_ERR(ts->avdd);
1056		if (error != -EPROBE_DEFER)
1057			dev_err(&client->dev,
1058				"Failed to get 'avdd' regulator: %d\n", error);
1059		return error;
1060	}
1061
1062	ts->vccio = devm_regulator_get(&client->dev, "vccio");
1063	if (IS_ERR(ts->vccio)) {
1064		error = PTR_ERR(ts->vccio);
1065		if (error != -EPROBE_DEFER)
1066			dev_err(&client->dev,
1067				"Failed to get 'vccio' regulator: %d\n", error);
1068		return error;
1069	}
1070
1071	ts->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
1072						 GPIOD_OUT_LOW);
1073	if (IS_ERR(ts->reset_gpio)) {
1074		error = PTR_ERR(ts->reset_gpio);
1075		if (error != -EPROBE_DEFER)
1076			dev_err(&client->dev,
1077				"failed to get reset gpio: %d\n", error);
1078		return error;
1079	}
1080
1081	error = raydium_i2c_power_on(ts);
1082	if (error)
1083		return error;
1084
1085	error = devm_add_action(&client->dev, raydium_i2c_power_off, ts);
 
1086	if (error) {
1087		dev_err(&client->dev,
1088			"failed to install power off action: %d\n", error);
1089		raydium_i2c_power_off(ts);
1090		return error;
1091	}
1092
1093	/* Make sure there is something at this address */
1094	if (i2c_smbus_xfer(client->adapter, client->addr, 0,
1095			   I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
1096		dev_err(&client->dev, "nothing at this address\n");
1097		return -ENXIO;
1098	}
1099
1100	error = raydium_i2c_initialize(ts);
1101	if (error) {
1102		dev_err(&client->dev, "failed to initialize: %d\n", error);
1103		return error;
1104	}
1105
1106	ts->report_data = devm_kmalloc(&client->dev,
1107				       ts->pkg_size, GFP_KERNEL);
1108	if (!ts->report_data)
1109		return -ENOMEM;
1110
1111	ts->input = devm_input_allocate_device(&client->dev);
1112	if (!ts->input) {
1113		dev_err(&client->dev, "Failed to allocate input device\n");
1114		return -ENOMEM;
1115	}
1116
1117	ts->input->name = "Raydium Touchscreen";
1118	ts->input->id.bustype = BUS_I2C;
1119
1120	input_set_abs_params(ts->input, ABS_MT_POSITION_X,
1121			     0, le16_to_cpu(ts->info.x_max), 0, 0);
1122	input_set_abs_params(ts->input, ABS_MT_POSITION_Y,
1123			     0, le16_to_cpu(ts->info.y_max), 0, 0);
1124	input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->info.x_res);
1125	input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->info.y_res);
1126
1127	input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1128	input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
1129
1130	error = input_mt_init_slots(ts->input, RM_MAX_TOUCH_NUM,
1131				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
1132	if (error) {
1133		dev_err(&client->dev,
1134			"failed to initialize MT slots: %d\n", error);
1135		return error;
1136	}
1137
1138	error = input_register_device(ts->input);
1139	if (error) {
1140		dev_err(&client->dev,
1141			"unable to register input device: %d\n", error);
1142		return error;
1143	}
1144
1145	error = devm_request_threaded_irq(&client->dev, client->irq,
1146					  NULL, raydium_i2c_irq,
1147					  IRQF_ONESHOT, client->name, ts);
1148	if (error) {
1149		dev_err(&client->dev, "Failed to register interrupt\n");
1150		return error;
1151	}
1152
1153	error = devm_device_add_group(&client->dev,
1154				   &raydium_i2c_attribute_group);
1155	if (error) {
1156		dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
1157			error);
1158		return error;
1159	}
1160
1161	return 0;
1162}
1163
1164static void __maybe_unused raydium_enter_sleep(struct i2c_client *client)
1165{
1166	static const u8 sleep_cmd[] = { 0x5A, 0xff, 0x00, 0x0f };
1167	int error;
1168
1169	error = raydium_i2c_send(client, RM_CMD_ENTER_SLEEP,
1170				 sleep_cmd, sizeof(sleep_cmd));
1171	if (error)
1172		dev_err(&client->dev,
1173			"sleep command failed: %d\n", error);
1174}
1175
1176static int __maybe_unused raydium_i2c_suspend(struct device *dev)
1177{
1178	struct i2c_client *client = to_i2c_client(dev);
1179	struct raydium_data *ts = i2c_get_clientdata(client);
1180
1181	/* Sleep is not available in BLDR recovery mode */
1182	if (ts->boot_mode != RAYDIUM_TS_MAIN)
1183		return -EBUSY;
1184
1185	disable_irq(client->irq);
1186
1187	if (device_may_wakeup(dev)) {
1188		raydium_enter_sleep(client);
1189
1190		ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0);
1191	} else {
1192		raydium_i2c_power_off(ts);
1193	}
1194
1195	return 0;
1196}
1197
1198static int __maybe_unused raydium_i2c_resume(struct device *dev)
1199{
1200	struct i2c_client *client = to_i2c_client(dev);
1201	struct raydium_data *ts = i2c_get_clientdata(client);
1202
1203	if (device_may_wakeup(dev)) {
1204		if (ts->wake_irq_enabled)
1205			disable_irq_wake(client->irq);
1206		raydium_i2c_sw_reset(client);
1207	} else {
1208		raydium_i2c_power_on(ts);
1209		raydium_i2c_initialize(ts);
1210	}
1211
1212	enable_irq(client->irq);
1213
1214	return 0;
1215}
1216
1217static SIMPLE_DEV_PM_OPS(raydium_i2c_pm_ops,
1218			 raydium_i2c_suspend, raydium_i2c_resume);
1219
1220static const struct i2c_device_id raydium_i2c_id[] = {
1221	{ "raydium_i2c" , 0 },
1222	{ "rm32380", 0 },
1223	{ /* sentinel */ }
1224};
1225MODULE_DEVICE_TABLE(i2c, raydium_i2c_id);
1226
1227#ifdef CONFIG_ACPI
1228static const struct acpi_device_id raydium_acpi_id[] = {
1229	{ "RAYD0001", 0 },
1230	{ /* sentinel */ }
1231};
1232MODULE_DEVICE_TABLE(acpi, raydium_acpi_id);
1233#endif
1234
1235#ifdef CONFIG_OF
1236static const struct of_device_id raydium_of_match[] = {
1237	{ .compatible = "raydium,rm32380", },
1238	{ /* sentinel */ }
1239};
1240MODULE_DEVICE_TABLE(of, raydium_of_match);
1241#endif
1242
1243static struct i2c_driver raydium_i2c_driver = {
1244	.probe = raydium_i2c_probe,
1245	.id_table = raydium_i2c_id,
1246	.driver = {
1247		.name = "raydium_ts",
1248		.pm = &raydium_i2c_pm_ops,
 
1249		.acpi_match_table = ACPI_PTR(raydium_acpi_id),
1250		.of_match_table = of_match_ptr(raydium_of_match),
1251	},
1252};
1253module_i2c_driver(raydium_i2c_driver);
1254
1255MODULE_AUTHOR("Raydium");
1256MODULE_DESCRIPTION("Raydium I2c Touchscreen driver");
1257MODULE_LICENSE("GPL v2");