18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Azoteq IQS550/572/525 Trackpad/Touchscreen Controller
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2018
68c2ecf20Sopenharmony_ci * Author: Jeff LaBundy <jeff@labundy.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * These devices require firmware exported from a PC-based configuration tool
98c2ecf20Sopenharmony_ci * made available by the vendor. Firmware files may be pushed to the device's
108c2ecf20Sopenharmony_ci * nonvolatile memory by writing the filename to the 'fw_file' sysfs control.
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Link to PC-based configuration tool and data sheet: http://www.azoteq.com/
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/delay.h>
168c2ecf20Sopenharmony_ci#include <linux/device.h>
178c2ecf20Sopenharmony_ci#include <linux/err.h>
188c2ecf20Sopenharmony_ci#include <linux/firmware.h>
198c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
208c2ecf20Sopenharmony_ci#include <linux/i2c.h>
218c2ecf20Sopenharmony_ci#include <linux/input.h>
228c2ecf20Sopenharmony_ci#include <linux/input/mt.h>
238c2ecf20Sopenharmony_ci#include <linux/input/touchscreen.h>
248c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
258c2ecf20Sopenharmony_ci#include <linux/kernel.h>
268c2ecf20Sopenharmony_ci#include <linux/module.h>
278c2ecf20Sopenharmony_ci#include <linux/of_device.h>
288c2ecf20Sopenharmony_ci#include <linux/slab.h>
298c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define IQS5XX_FW_FILE_LEN	64
328c2ecf20Sopenharmony_ci#define IQS5XX_NUM_RETRIES	10
338c2ecf20Sopenharmony_ci#define IQS5XX_NUM_POINTS	256
348c2ecf20Sopenharmony_ci#define IQS5XX_NUM_CONTACTS	5
358c2ecf20Sopenharmony_ci#define IQS5XX_WR_BYTES_MAX	2
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define IQS5XX_PROD_NUM_IQS550	40
388c2ecf20Sopenharmony_ci#define IQS5XX_PROD_NUM_IQS572	58
398c2ecf20Sopenharmony_ci#define IQS5XX_PROD_NUM_IQS525	52
408c2ecf20Sopenharmony_ci#define IQS5XX_PROJ_NUM_A000	0
418c2ecf20Sopenharmony_ci#define IQS5XX_PROJ_NUM_B000	15
428c2ecf20Sopenharmony_ci#define IQS5XX_MAJOR_VER_MIN	2
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define IQS5XX_RESUME		0x00
458c2ecf20Sopenharmony_ci#define IQS5XX_SUSPEND		0x01
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define IQS5XX_SW_INPUT_EVENT	0x10
488c2ecf20Sopenharmony_ci#define IQS5XX_SETUP_COMPLETE	0x40
498c2ecf20Sopenharmony_ci#define IQS5XX_EVENT_MODE	0x01
508c2ecf20Sopenharmony_ci#define IQS5XX_TP_EVENT		0x04
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define IQS5XX_FLIP_X		0x01
538c2ecf20Sopenharmony_ci#define IQS5XX_FLIP_Y		0x02
548c2ecf20Sopenharmony_ci#define IQS5XX_SWITCH_XY_AXIS	0x04
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define IQS5XX_PROD_NUM		0x0000
578c2ecf20Sopenharmony_ci#define IQS5XX_ABS_X		0x0016
588c2ecf20Sopenharmony_ci#define IQS5XX_ABS_Y		0x0018
598c2ecf20Sopenharmony_ci#define IQS5XX_SYS_CTRL0	0x0431
608c2ecf20Sopenharmony_ci#define IQS5XX_SYS_CTRL1	0x0432
618c2ecf20Sopenharmony_ci#define IQS5XX_SYS_CFG0		0x058E
628c2ecf20Sopenharmony_ci#define IQS5XX_SYS_CFG1		0x058F
638c2ecf20Sopenharmony_ci#define IQS5XX_TOTAL_RX		0x063D
648c2ecf20Sopenharmony_ci#define IQS5XX_TOTAL_TX		0x063E
658c2ecf20Sopenharmony_ci#define IQS5XX_XY_CFG0		0x0669
668c2ecf20Sopenharmony_ci#define IQS5XX_X_RES		0x066E
678c2ecf20Sopenharmony_ci#define IQS5XX_Y_RES		0x0670
688c2ecf20Sopenharmony_ci#define IQS5XX_CHKSM		0x83C0
698c2ecf20Sopenharmony_ci#define IQS5XX_APP		0x8400
708c2ecf20Sopenharmony_ci#define IQS5XX_CSTM		0xBE00
718c2ecf20Sopenharmony_ci#define IQS5XX_PMAP_END		0xBFFF
728c2ecf20Sopenharmony_ci#define IQS5XX_END_COMM		0xEEEE
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#define IQS5XX_CHKSM_LEN	(IQS5XX_APP - IQS5XX_CHKSM)
758c2ecf20Sopenharmony_ci#define IQS5XX_APP_LEN		(IQS5XX_CSTM - IQS5XX_APP)
768c2ecf20Sopenharmony_ci#define IQS5XX_CSTM_LEN		(IQS5XX_PMAP_END + 1 - IQS5XX_CSTM)
778c2ecf20Sopenharmony_ci#define IQS5XX_PMAP_LEN		(IQS5XX_PMAP_END + 1 - IQS5XX_CHKSM)
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define IQS5XX_REC_HDR_LEN	4
808c2ecf20Sopenharmony_ci#define IQS5XX_REC_LEN_MAX	255
818c2ecf20Sopenharmony_ci#define IQS5XX_REC_TYPE_DATA	0x00
828c2ecf20Sopenharmony_ci#define IQS5XX_REC_TYPE_EOF	0x01
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define IQS5XX_BL_ADDR_MASK	0x40
858c2ecf20Sopenharmony_ci#define IQS5XX_BL_CMD_VER	0x00
868c2ecf20Sopenharmony_ci#define IQS5XX_BL_CMD_READ	0x01
878c2ecf20Sopenharmony_ci#define IQS5XX_BL_CMD_EXEC	0x02
888c2ecf20Sopenharmony_ci#define IQS5XX_BL_CMD_CRC	0x03
898c2ecf20Sopenharmony_ci#define IQS5XX_BL_BLK_LEN_MAX	64
908c2ecf20Sopenharmony_ci#define IQS5XX_BL_ID		0x0200
918c2ecf20Sopenharmony_ci#define IQS5XX_BL_STATUS_RESET	0x00
928c2ecf20Sopenharmony_ci#define IQS5XX_BL_STATUS_AVAIL	0xA5
938c2ecf20Sopenharmony_ci#define IQS5XX_BL_STATUS_NONE	0xEE
948c2ecf20Sopenharmony_ci#define IQS5XX_BL_CRC_PASS	0x00
958c2ecf20Sopenharmony_ci#define IQS5XX_BL_CRC_FAIL	0x01
968c2ecf20Sopenharmony_ci#define IQS5XX_BL_ATTEMPTS	3
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistruct iqs5xx_private {
998c2ecf20Sopenharmony_ci	struct i2c_client *client;
1008c2ecf20Sopenharmony_ci	struct input_dev *input;
1018c2ecf20Sopenharmony_ci	struct gpio_desc *reset_gpio;
1028c2ecf20Sopenharmony_ci	struct mutex lock;
1038c2ecf20Sopenharmony_ci	u8 bl_status;
1048c2ecf20Sopenharmony_ci};
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistruct iqs5xx_dev_id_info {
1078c2ecf20Sopenharmony_ci	__be16 prod_num;
1088c2ecf20Sopenharmony_ci	__be16 proj_num;
1098c2ecf20Sopenharmony_ci	u8 major_ver;
1108c2ecf20Sopenharmony_ci	u8 minor_ver;
1118c2ecf20Sopenharmony_ci	u8 bl_status;
1128c2ecf20Sopenharmony_ci} __packed;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistruct iqs5xx_ihex_rec {
1158c2ecf20Sopenharmony_ci	char start;
1168c2ecf20Sopenharmony_ci	char len[2];
1178c2ecf20Sopenharmony_ci	char addr[4];
1188c2ecf20Sopenharmony_ci	char type[2];
1198c2ecf20Sopenharmony_ci	char data[2];
1208c2ecf20Sopenharmony_ci} __packed;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistruct iqs5xx_touch_data {
1238c2ecf20Sopenharmony_ci	__be16 abs_x;
1248c2ecf20Sopenharmony_ci	__be16 abs_y;
1258c2ecf20Sopenharmony_ci	__be16 strength;
1268c2ecf20Sopenharmony_ci	u8 area;
1278c2ecf20Sopenharmony_ci} __packed;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic int iqs5xx_read_burst(struct i2c_client *client,
1308c2ecf20Sopenharmony_ci			     u16 reg, void *val, u16 len)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	__be16 reg_buf = cpu_to_be16(reg);
1338c2ecf20Sopenharmony_ci	int ret, i;
1348c2ecf20Sopenharmony_ci	struct i2c_msg msg[] = {
1358c2ecf20Sopenharmony_ci		{
1368c2ecf20Sopenharmony_ci			.addr = client->addr,
1378c2ecf20Sopenharmony_ci			.flags = 0,
1388c2ecf20Sopenharmony_ci			.len = sizeof(reg_buf),
1398c2ecf20Sopenharmony_ci			.buf = (u8 *)&reg_buf,
1408c2ecf20Sopenharmony_ci		},
1418c2ecf20Sopenharmony_ci		{
1428c2ecf20Sopenharmony_ci			.addr = client->addr,
1438c2ecf20Sopenharmony_ci			.flags = I2C_M_RD,
1448c2ecf20Sopenharmony_ci			.len = len,
1458c2ecf20Sopenharmony_ci			.buf = (u8 *)val,
1468c2ecf20Sopenharmony_ci		},
1478c2ecf20Sopenharmony_ci	};
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	/*
1508c2ecf20Sopenharmony_ci	 * The first addressing attempt outside of a communication window fails
1518c2ecf20Sopenharmony_ci	 * and must be retried, after which the device clock stretches until it
1528c2ecf20Sopenharmony_ci	 * is available.
1538c2ecf20Sopenharmony_ci	 */
1548c2ecf20Sopenharmony_ci	for (i = 0; i < IQS5XX_NUM_RETRIES; i++) {
1558c2ecf20Sopenharmony_ci		ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
1568c2ecf20Sopenharmony_ci		if (ret == ARRAY_SIZE(msg))
1578c2ecf20Sopenharmony_ci			return 0;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci		usleep_range(200, 300);
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	if (ret >= 0)
1638c2ecf20Sopenharmony_ci		ret = -EIO;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	dev_err(&client->dev, "Failed to read from address 0x%04X: %d\n",
1668c2ecf20Sopenharmony_ci		reg, ret);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	return ret;
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic int iqs5xx_read_word(struct i2c_client *client, u16 reg, u16 *val)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	__be16 val_buf;
1748c2ecf20Sopenharmony_ci	int error;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	error = iqs5xx_read_burst(client, reg, &val_buf, sizeof(val_buf));
1778c2ecf20Sopenharmony_ci	if (error)
1788c2ecf20Sopenharmony_ci		return error;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	*val = be16_to_cpu(val_buf);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	return 0;
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic int iqs5xx_read_byte(struct i2c_client *client, u16 reg, u8 *val)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	return iqs5xx_read_burst(client, reg, val, sizeof(*val));
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic int iqs5xx_write_burst(struct i2c_client *client,
1918c2ecf20Sopenharmony_ci			      u16 reg, const void *val, u16 len)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	int ret, i;
1948c2ecf20Sopenharmony_ci	u16 mlen = sizeof(reg) + len;
1958c2ecf20Sopenharmony_ci	u8 mbuf[sizeof(reg) + IQS5XX_WR_BYTES_MAX];
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	if (len > IQS5XX_WR_BYTES_MAX)
1988c2ecf20Sopenharmony_ci		return -EINVAL;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	put_unaligned_be16(reg, mbuf);
2018c2ecf20Sopenharmony_ci	memcpy(mbuf + sizeof(reg), val, len);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	/*
2048c2ecf20Sopenharmony_ci	 * The first addressing attempt outside of a communication window fails
2058c2ecf20Sopenharmony_ci	 * and must be retried, after which the device clock stretches until it
2068c2ecf20Sopenharmony_ci	 * is available.
2078c2ecf20Sopenharmony_ci	 */
2088c2ecf20Sopenharmony_ci	for (i = 0; i < IQS5XX_NUM_RETRIES; i++) {
2098c2ecf20Sopenharmony_ci		ret = i2c_master_send(client, mbuf, mlen);
2108c2ecf20Sopenharmony_ci		if (ret == mlen)
2118c2ecf20Sopenharmony_ci			return 0;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci		usleep_range(200, 300);
2148c2ecf20Sopenharmony_ci	}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	if (ret >= 0)
2178c2ecf20Sopenharmony_ci		ret = -EIO;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	dev_err(&client->dev, "Failed to write to address 0x%04X: %d\n",
2208c2ecf20Sopenharmony_ci		reg, ret);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	return ret;
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic int iqs5xx_write_word(struct i2c_client *client, u16 reg, u16 val)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	__be16 val_buf = cpu_to_be16(val);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	return iqs5xx_write_burst(client, reg, &val_buf, sizeof(val_buf));
2308c2ecf20Sopenharmony_ci}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cistatic int iqs5xx_write_byte(struct i2c_client *client, u16 reg, u8 val)
2338c2ecf20Sopenharmony_ci{
2348c2ecf20Sopenharmony_ci	return iqs5xx_write_burst(client, reg, &val, sizeof(val));
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic void iqs5xx_reset(struct i2c_client *client)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	gpiod_set_value_cansleep(iqs5xx->reset_gpio, 1);
2428c2ecf20Sopenharmony_ci	usleep_range(200, 300);
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	gpiod_set_value_cansleep(iqs5xx->reset_gpio, 0);
2458c2ecf20Sopenharmony_ci}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_cistatic int iqs5xx_bl_cmd(struct i2c_client *client, u8 bl_cmd, u16 bl_addr)
2488c2ecf20Sopenharmony_ci{
2498c2ecf20Sopenharmony_ci	struct i2c_msg msg;
2508c2ecf20Sopenharmony_ci	int ret;
2518c2ecf20Sopenharmony_ci	u8 mbuf[sizeof(bl_cmd) + sizeof(bl_addr)];
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	msg.addr = client->addr ^ IQS5XX_BL_ADDR_MASK;
2548c2ecf20Sopenharmony_ci	msg.flags = 0;
2558c2ecf20Sopenharmony_ci	msg.len = sizeof(bl_cmd);
2568c2ecf20Sopenharmony_ci	msg.buf = mbuf;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	*mbuf = bl_cmd;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	switch (bl_cmd) {
2618c2ecf20Sopenharmony_ci	case IQS5XX_BL_CMD_VER:
2628c2ecf20Sopenharmony_ci	case IQS5XX_BL_CMD_CRC:
2638c2ecf20Sopenharmony_ci	case IQS5XX_BL_CMD_EXEC:
2648c2ecf20Sopenharmony_ci		break;
2658c2ecf20Sopenharmony_ci	case IQS5XX_BL_CMD_READ:
2668c2ecf20Sopenharmony_ci		msg.len += sizeof(bl_addr);
2678c2ecf20Sopenharmony_ci		put_unaligned_be16(bl_addr, mbuf + sizeof(bl_cmd));
2688c2ecf20Sopenharmony_ci		break;
2698c2ecf20Sopenharmony_ci	default:
2708c2ecf20Sopenharmony_ci		return -EINVAL;
2718c2ecf20Sopenharmony_ci	}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	ret = i2c_transfer(client->adapter, &msg, 1);
2748c2ecf20Sopenharmony_ci	if (ret != 1)
2758c2ecf20Sopenharmony_ci		goto msg_fail;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	switch (bl_cmd) {
2788c2ecf20Sopenharmony_ci	case IQS5XX_BL_CMD_VER:
2798c2ecf20Sopenharmony_ci		msg.len = sizeof(u16);
2808c2ecf20Sopenharmony_ci		break;
2818c2ecf20Sopenharmony_ci	case IQS5XX_BL_CMD_CRC:
2828c2ecf20Sopenharmony_ci		msg.len = sizeof(u8);
2838c2ecf20Sopenharmony_ci		/*
2848c2ecf20Sopenharmony_ci		 * This delay saves the bus controller the trouble of having to
2858c2ecf20Sopenharmony_ci		 * tolerate a relatively long clock-stretching period while the
2868c2ecf20Sopenharmony_ci		 * CRC is calculated.
2878c2ecf20Sopenharmony_ci		 */
2888c2ecf20Sopenharmony_ci		msleep(50);
2898c2ecf20Sopenharmony_ci		break;
2908c2ecf20Sopenharmony_ci	case IQS5XX_BL_CMD_EXEC:
2918c2ecf20Sopenharmony_ci		usleep_range(10000, 10100);
2928c2ecf20Sopenharmony_ci		fallthrough;
2938c2ecf20Sopenharmony_ci	default:
2948c2ecf20Sopenharmony_ci		return 0;
2958c2ecf20Sopenharmony_ci	}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	msg.flags = I2C_M_RD;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	ret = i2c_transfer(client->adapter, &msg, 1);
3008c2ecf20Sopenharmony_ci	if (ret != 1)
3018c2ecf20Sopenharmony_ci		goto msg_fail;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	if (bl_cmd == IQS5XX_BL_CMD_VER &&
3048c2ecf20Sopenharmony_ci	    get_unaligned_be16(mbuf) != IQS5XX_BL_ID) {
3058c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Unrecognized bootloader ID: 0x%04X\n",
3068c2ecf20Sopenharmony_ci			get_unaligned_be16(mbuf));
3078c2ecf20Sopenharmony_ci		return -EINVAL;
3088c2ecf20Sopenharmony_ci	}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	if (bl_cmd == IQS5XX_BL_CMD_CRC && *mbuf != IQS5XX_BL_CRC_PASS) {
3118c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Bootloader CRC failed\n");
3128c2ecf20Sopenharmony_ci		return -EIO;
3138c2ecf20Sopenharmony_ci	}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	return 0;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cimsg_fail:
3188c2ecf20Sopenharmony_ci	if (ret >= 0)
3198c2ecf20Sopenharmony_ci		ret = -EIO;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	if (bl_cmd != IQS5XX_BL_CMD_VER)
3228c2ecf20Sopenharmony_ci		dev_err(&client->dev,
3238c2ecf20Sopenharmony_ci			"Unsuccessful bootloader command 0x%02X: %d\n",
3248c2ecf20Sopenharmony_ci			bl_cmd, ret);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	return ret;
3278c2ecf20Sopenharmony_ci}
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_cistatic int iqs5xx_bl_open(struct i2c_client *client)
3308c2ecf20Sopenharmony_ci{
3318c2ecf20Sopenharmony_ci	int error, i, j;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	/*
3348c2ecf20Sopenharmony_ci	 * The device opens a bootloader polling window for 2 ms following the
3358c2ecf20Sopenharmony_ci	 * release of reset. If the host cannot establish communication during
3368c2ecf20Sopenharmony_ci	 * this time frame, it must cycle reset again.
3378c2ecf20Sopenharmony_ci	 */
3388c2ecf20Sopenharmony_ci	for (i = 0; i < IQS5XX_BL_ATTEMPTS; i++) {
3398c2ecf20Sopenharmony_ci		iqs5xx_reset(client);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci		for (j = 0; j < IQS5XX_NUM_RETRIES; j++) {
3428c2ecf20Sopenharmony_ci			error = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_VER, 0);
3438c2ecf20Sopenharmony_ci			if (!error || error == -EINVAL)
3448c2ecf20Sopenharmony_ci				return error;
3458c2ecf20Sopenharmony_ci		}
3468c2ecf20Sopenharmony_ci	}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	dev_err(&client->dev, "Failed to open bootloader: %d\n", error);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	return error;
3518c2ecf20Sopenharmony_ci}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_cistatic int iqs5xx_bl_write(struct i2c_client *client,
3548c2ecf20Sopenharmony_ci			   u16 bl_addr, u8 *pmap_data, u16 pmap_len)
3558c2ecf20Sopenharmony_ci{
3568c2ecf20Sopenharmony_ci	struct i2c_msg msg;
3578c2ecf20Sopenharmony_ci	int ret, i;
3588c2ecf20Sopenharmony_ci	u8 mbuf[sizeof(bl_addr) + IQS5XX_BL_BLK_LEN_MAX];
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	if (pmap_len % IQS5XX_BL_BLK_LEN_MAX)
3618c2ecf20Sopenharmony_ci		return -EINVAL;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	msg.addr = client->addr ^ IQS5XX_BL_ADDR_MASK;
3648c2ecf20Sopenharmony_ci	msg.flags = 0;
3658c2ecf20Sopenharmony_ci	msg.len = sizeof(mbuf);
3668c2ecf20Sopenharmony_ci	msg.buf = mbuf;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	for (i = 0; i < pmap_len; i += IQS5XX_BL_BLK_LEN_MAX) {
3698c2ecf20Sopenharmony_ci		put_unaligned_be16(bl_addr + i, mbuf);
3708c2ecf20Sopenharmony_ci		memcpy(mbuf + sizeof(bl_addr), pmap_data + i,
3718c2ecf20Sopenharmony_ci		       sizeof(mbuf) - sizeof(bl_addr));
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci		ret = i2c_transfer(client->adapter, &msg, 1);
3748c2ecf20Sopenharmony_ci		if (ret != 1)
3758c2ecf20Sopenharmony_ci			goto msg_fail;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci		usleep_range(10000, 10100);
3788c2ecf20Sopenharmony_ci	}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	return 0;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_cimsg_fail:
3838c2ecf20Sopenharmony_ci	if (ret >= 0)
3848c2ecf20Sopenharmony_ci		ret = -EIO;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	dev_err(&client->dev, "Failed to write block at address 0x%04X: %d\n",
3878c2ecf20Sopenharmony_ci		bl_addr + i, ret);
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	return ret;
3908c2ecf20Sopenharmony_ci}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_cistatic int iqs5xx_bl_verify(struct i2c_client *client,
3938c2ecf20Sopenharmony_ci			    u16 bl_addr, u8 *pmap_data, u16 pmap_len)
3948c2ecf20Sopenharmony_ci{
3958c2ecf20Sopenharmony_ci	struct i2c_msg msg;
3968c2ecf20Sopenharmony_ci	int ret, i;
3978c2ecf20Sopenharmony_ci	u8 bl_data[IQS5XX_BL_BLK_LEN_MAX];
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	if (pmap_len % IQS5XX_BL_BLK_LEN_MAX)
4008c2ecf20Sopenharmony_ci		return -EINVAL;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	msg.addr = client->addr ^ IQS5XX_BL_ADDR_MASK;
4038c2ecf20Sopenharmony_ci	msg.flags = I2C_M_RD;
4048c2ecf20Sopenharmony_ci	msg.len = sizeof(bl_data);
4058c2ecf20Sopenharmony_ci	msg.buf = bl_data;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	for (i = 0; i < pmap_len; i += IQS5XX_BL_BLK_LEN_MAX) {
4088c2ecf20Sopenharmony_ci		ret = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_READ, bl_addr + i);
4098c2ecf20Sopenharmony_ci		if (ret)
4108c2ecf20Sopenharmony_ci			return ret;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci		ret = i2c_transfer(client->adapter, &msg, 1);
4138c2ecf20Sopenharmony_ci		if (ret != 1)
4148c2ecf20Sopenharmony_ci			goto msg_fail;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci		if (memcmp(bl_data, pmap_data + i, sizeof(bl_data))) {
4178c2ecf20Sopenharmony_ci			dev_err(&client->dev,
4188c2ecf20Sopenharmony_ci				"Failed to verify block at address 0x%04X\n",
4198c2ecf20Sopenharmony_ci				bl_addr + i);
4208c2ecf20Sopenharmony_ci			return -EIO;
4218c2ecf20Sopenharmony_ci		}
4228c2ecf20Sopenharmony_ci	}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	return 0;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_cimsg_fail:
4278c2ecf20Sopenharmony_ci	if (ret >= 0)
4288c2ecf20Sopenharmony_ci		ret = -EIO;
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	dev_err(&client->dev, "Failed to read block at address 0x%04X: %d\n",
4318c2ecf20Sopenharmony_ci		bl_addr + i, ret);
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	return ret;
4348c2ecf20Sopenharmony_ci}
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_cistatic int iqs5xx_set_state(struct i2c_client *client, u8 state)
4378c2ecf20Sopenharmony_ci{
4388c2ecf20Sopenharmony_ci	struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
4398c2ecf20Sopenharmony_ci	int error1, error2;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	if (iqs5xx->bl_status == IQS5XX_BL_STATUS_RESET)
4428c2ecf20Sopenharmony_ci		return 0;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	mutex_lock(&iqs5xx->lock);
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	/*
4478c2ecf20Sopenharmony_ci	 * Addressing the device outside of a communication window prompts it
4488c2ecf20Sopenharmony_ci	 * to assert the RDY output, so disable the interrupt line to prevent
4498c2ecf20Sopenharmony_ci	 * the handler from servicing a false interrupt.
4508c2ecf20Sopenharmony_ci	 */
4518c2ecf20Sopenharmony_ci	disable_irq(client->irq);
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	error1 = iqs5xx_write_byte(client, IQS5XX_SYS_CTRL1, state);
4548c2ecf20Sopenharmony_ci	error2 = iqs5xx_write_byte(client, IQS5XX_END_COMM, 0);
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	usleep_range(50, 100);
4578c2ecf20Sopenharmony_ci	enable_irq(client->irq);
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	mutex_unlock(&iqs5xx->lock);
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	if (error1)
4628c2ecf20Sopenharmony_ci		return error1;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	return error2;
4658c2ecf20Sopenharmony_ci}
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_cistatic int iqs5xx_open(struct input_dev *input)
4688c2ecf20Sopenharmony_ci{
4698c2ecf20Sopenharmony_ci	struct iqs5xx_private *iqs5xx = input_get_drvdata(input);
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	return iqs5xx_set_state(iqs5xx->client, IQS5XX_RESUME);
4728c2ecf20Sopenharmony_ci}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_cistatic void iqs5xx_close(struct input_dev *input)
4758c2ecf20Sopenharmony_ci{
4768c2ecf20Sopenharmony_ci	struct iqs5xx_private *iqs5xx = input_get_drvdata(input);
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	iqs5xx_set_state(iqs5xx->client, IQS5XX_SUSPEND);
4798c2ecf20Sopenharmony_ci}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_cistatic int iqs5xx_axis_init(struct i2c_client *client)
4828c2ecf20Sopenharmony_ci{
4838c2ecf20Sopenharmony_ci	struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
4848c2ecf20Sopenharmony_ci	struct touchscreen_properties prop;
4858c2ecf20Sopenharmony_ci	struct input_dev *input;
4868c2ecf20Sopenharmony_ci	int error;
4878c2ecf20Sopenharmony_ci	u16 max_x, max_x_hw;
4888c2ecf20Sopenharmony_ci	u16 max_y, max_y_hw;
4898c2ecf20Sopenharmony_ci	u8 val;
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	if (!iqs5xx->input) {
4928c2ecf20Sopenharmony_ci		input = devm_input_allocate_device(&client->dev);
4938c2ecf20Sopenharmony_ci		if (!input)
4948c2ecf20Sopenharmony_ci			return -ENOMEM;
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci		input->name = client->name;
4978c2ecf20Sopenharmony_ci		input->id.bustype = BUS_I2C;
4988c2ecf20Sopenharmony_ci		input->open = iqs5xx_open;
4998c2ecf20Sopenharmony_ci		input->close = iqs5xx_close;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci		input_set_capability(input, EV_ABS, ABS_MT_POSITION_X);
5028c2ecf20Sopenharmony_ci		input_set_capability(input, EV_ABS, ABS_MT_POSITION_Y);
5038c2ecf20Sopenharmony_ci		input_set_capability(input, EV_ABS, ABS_MT_PRESSURE);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci		input_set_drvdata(input, iqs5xx);
5068c2ecf20Sopenharmony_ci		iqs5xx->input = input;
5078c2ecf20Sopenharmony_ci	}
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	touchscreen_parse_properties(iqs5xx->input, true, &prop);
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	error = iqs5xx_read_byte(client, IQS5XX_TOTAL_RX, &val);
5128c2ecf20Sopenharmony_ci	if (error)
5138c2ecf20Sopenharmony_ci		return error;
5148c2ecf20Sopenharmony_ci	max_x_hw = (val - 1) * IQS5XX_NUM_POINTS;
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	error = iqs5xx_read_byte(client, IQS5XX_TOTAL_TX, &val);
5178c2ecf20Sopenharmony_ci	if (error)
5188c2ecf20Sopenharmony_ci		return error;
5198c2ecf20Sopenharmony_ci	max_y_hw = (val - 1) * IQS5XX_NUM_POINTS;
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	error = iqs5xx_read_byte(client, IQS5XX_XY_CFG0, &val);
5228c2ecf20Sopenharmony_ci	if (error)
5238c2ecf20Sopenharmony_ci		return error;
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	if (val & IQS5XX_SWITCH_XY_AXIS)
5268c2ecf20Sopenharmony_ci		swap(max_x_hw, max_y_hw);
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	if (prop.swap_x_y)
5298c2ecf20Sopenharmony_ci		val ^= IQS5XX_SWITCH_XY_AXIS;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	if (prop.invert_x)
5328c2ecf20Sopenharmony_ci		val ^= prop.swap_x_y ? IQS5XX_FLIP_Y : IQS5XX_FLIP_X;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	if (prop.invert_y)
5358c2ecf20Sopenharmony_ci		val ^= prop.swap_x_y ? IQS5XX_FLIP_X : IQS5XX_FLIP_Y;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	error = iqs5xx_write_byte(client, IQS5XX_XY_CFG0, val);
5388c2ecf20Sopenharmony_ci	if (error)
5398c2ecf20Sopenharmony_ci		return error;
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	if (prop.max_x > max_x_hw) {
5428c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Invalid maximum x-coordinate: %u > %u\n",
5438c2ecf20Sopenharmony_ci			prop.max_x, max_x_hw);
5448c2ecf20Sopenharmony_ci		return -EINVAL;
5458c2ecf20Sopenharmony_ci	} else if (prop.max_x == 0) {
5468c2ecf20Sopenharmony_ci		error = iqs5xx_read_word(client, IQS5XX_X_RES, &max_x);
5478c2ecf20Sopenharmony_ci		if (error)
5488c2ecf20Sopenharmony_ci			return error;
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci		input_abs_set_max(iqs5xx->input,
5518c2ecf20Sopenharmony_ci				  prop.swap_x_y ? ABS_MT_POSITION_Y :
5528c2ecf20Sopenharmony_ci						  ABS_MT_POSITION_X,
5538c2ecf20Sopenharmony_ci				  max_x);
5548c2ecf20Sopenharmony_ci	} else {
5558c2ecf20Sopenharmony_ci		max_x = (u16)prop.max_x;
5568c2ecf20Sopenharmony_ci	}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	if (prop.max_y > max_y_hw) {
5598c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Invalid maximum y-coordinate: %u > %u\n",
5608c2ecf20Sopenharmony_ci			prop.max_y, max_y_hw);
5618c2ecf20Sopenharmony_ci		return -EINVAL;
5628c2ecf20Sopenharmony_ci	} else if (prop.max_y == 0) {
5638c2ecf20Sopenharmony_ci		error = iqs5xx_read_word(client, IQS5XX_Y_RES, &max_y);
5648c2ecf20Sopenharmony_ci		if (error)
5658c2ecf20Sopenharmony_ci			return error;
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci		input_abs_set_max(iqs5xx->input,
5688c2ecf20Sopenharmony_ci				  prop.swap_x_y ? ABS_MT_POSITION_X :
5698c2ecf20Sopenharmony_ci						  ABS_MT_POSITION_Y,
5708c2ecf20Sopenharmony_ci				  max_y);
5718c2ecf20Sopenharmony_ci	} else {
5728c2ecf20Sopenharmony_ci		max_y = (u16)prop.max_y;
5738c2ecf20Sopenharmony_ci	}
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	/*
5768c2ecf20Sopenharmony_ci	 * Write horizontal and vertical resolution to the device in case its
5778c2ecf20Sopenharmony_ci	 * original defaults were overridden or swapped as per the properties
5788c2ecf20Sopenharmony_ci	 * specified in the device tree.
5798c2ecf20Sopenharmony_ci	 */
5808c2ecf20Sopenharmony_ci	error = iqs5xx_write_word(client,
5818c2ecf20Sopenharmony_ci				  prop.swap_x_y ? IQS5XX_Y_RES : IQS5XX_X_RES,
5828c2ecf20Sopenharmony_ci				  max_x);
5838c2ecf20Sopenharmony_ci	if (error)
5848c2ecf20Sopenharmony_ci		return error;
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	error = iqs5xx_write_word(client,
5878c2ecf20Sopenharmony_ci				  prop.swap_x_y ? IQS5XX_X_RES : IQS5XX_Y_RES,
5888c2ecf20Sopenharmony_ci				  max_y);
5898c2ecf20Sopenharmony_ci	if (error)
5908c2ecf20Sopenharmony_ci		return error;
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	error = input_mt_init_slots(iqs5xx->input, IQS5XX_NUM_CONTACTS,
5938c2ecf20Sopenharmony_ci				    INPUT_MT_DIRECT);
5948c2ecf20Sopenharmony_ci	if (error)
5958c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to initialize slots: %d\n",
5968c2ecf20Sopenharmony_ci			error);
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	return error;
5998c2ecf20Sopenharmony_ci}
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_cistatic int iqs5xx_dev_init(struct i2c_client *client)
6028c2ecf20Sopenharmony_ci{
6038c2ecf20Sopenharmony_ci	struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
6048c2ecf20Sopenharmony_ci	struct iqs5xx_dev_id_info *dev_id_info;
6058c2ecf20Sopenharmony_ci	int error;
6068c2ecf20Sopenharmony_ci	u8 val;
6078c2ecf20Sopenharmony_ci	u8 buf[sizeof(*dev_id_info) + 1];
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	error = iqs5xx_read_burst(client, IQS5XX_PROD_NUM,
6108c2ecf20Sopenharmony_ci				  &buf[1], sizeof(*dev_id_info));
6118c2ecf20Sopenharmony_ci	if (error)
6128c2ecf20Sopenharmony_ci		return iqs5xx_bl_open(client);
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	/*
6158c2ecf20Sopenharmony_ci	 * A000 and B000 devices use 8-bit and 16-bit addressing, respectively.
6168c2ecf20Sopenharmony_ci	 * Querying an A000 device's version information with 16-bit addressing
6178c2ecf20Sopenharmony_ci	 * gives the appearance that the data is shifted by one byte; a nonzero
6188c2ecf20Sopenharmony_ci	 * leading array element suggests this could be the case (in which case
6198c2ecf20Sopenharmony_ci	 * the missing zero is prepended).
6208c2ecf20Sopenharmony_ci	 */
6218c2ecf20Sopenharmony_ci	buf[0] = 0;
6228c2ecf20Sopenharmony_ci	dev_id_info = (struct iqs5xx_dev_id_info *)&buf[(buf[1] > 0) ? 0 : 1];
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	switch (be16_to_cpu(dev_id_info->prod_num)) {
6258c2ecf20Sopenharmony_ci	case IQS5XX_PROD_NUM_IQS550:
6268c2ecf20Sopenharmony_ci	case IQS5XX_PROD_NUM_IQS572:
6278c2ecf20Sopenharmony_ci	case IQS5XX_PROD_NUM_IQS525:
6288c2ecf20Sopenharmony_ci		break;
6298c2ecf20Sopenharmony_ci	default:
6308c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Unrecognized product number: %u\n",
6318c2ecf20Sopenharmony_ci			be16_to_cpu(dev_id_info->prod_num));
6328c2ecf20Sopenharmony_ci		return -EINVAL;
6338c2ecf20Sopenharmony_ci	}
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	switch (be16_to_cpu(dev_id_info->proj_num)) {
6368c2ecf20Sopenharmony_ci	case IQS5XX_PROJ_NUM_A000:
6378c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Unsupported project number: %u\n",
6388c2ecf20Sopenharmony_ci			be16_to_cpu(dev_id_info->proj_num));
6398c2ecf20Sopenharmony_ci		return iqs5xx_bl_open(client);
6408c2ecf20Sopenharmony_ci	case IQS5XX_PROJ_NUM_B000:
6418c2ecf20Sopenharmony_ci		break;
6428c2ecf20Sopenharmony_ci	default:
6438c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Unrecognized project number: %u\n",
6448c2ecf20Sopenharmony_ci			be16_to_cpu(dev_id_info->proj_num));
6458c2ecf20Sopenharmony_ci		return -EINVAL;
6468c2ecf20Sopenharmony_ci	}
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	if (dev_id_info->major_ver < IQS5XX_MAJOR_VER_MIN) {
6498c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Unsupported major version: %u\n",
6508c2ecf20Sopenharmony_ci			dev_id_info->major_ver);
6518c2ecf20Sopenharmony_ci		return iqs5xx_bl_open(client);
6528c2ecf20Sopenharmony_ci	}
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	switch (dev_id_info->bl_status) {
6558c2ecf20Sopenharmony_ci	case IQS5XX_BL_STATUS_AVAIL:
6568c2ecf20Sopenharmony_ci	case IQS5XX_BL_STATUS_NONE:
6578c2ecf20Sopenharmony_ci		break;
6588c2ecf20Sopenharmony_ci	default:
6598c2ecf20Sopenharmony_ci		dev_err(&client->dev,
6608c2ecf20Sopenharmony_ci			"Unrecognized bootloader status: 0x%02X\n",
6618c2ecf20Sopenharmony_ci			dev_id_info->bl_status);
6628c2ecf20Sopenharmony_ci		return -EINVAL;
6638c2ecf20Sopenharmony_ci	}
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	error = iqs5xx_axis_init(client);
6668c2ecf20Sopenharmony_ci	if (error)
6678c2ecf20Sopenharmony_ci		return error;
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	error = iqs5xx_read_byte(client, IQS5XX_SYS_CFG0, &val);
6708c2ecf20Sopenharmony_ci	if (error)
6718c2ecf20Sopenharmony_ci		return error;
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	val |= IQS5XX_SETUP_COMPLETE;
6748c2ecf20Sopenharmony_ci	val &= ~IQS5XX_SW_INPUT_EVENT;
6758c2ecf20Sopenharmony_ci	error = iqs5xx_write_byte(client, IQS5XX_SYS_CFG0, val);
6768c2ecf20Sopenharmony_ci	if (error)
6778c2ecf20Sopenharmony_ci		return error;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	val = IQS5XX_TP_EVENT | IQS5XX_EVENT_MODE;
6808c2ecf20Sopenharmony_ci	error = iqs5xx_write_byte(client, IQS5XX_SYS_CFG1, val);
6818c2ecf20Sopenharmony_ci	if (error)
6828c2ecf20Sopenharmony_ci		return error;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	error = iqs5xx_write_byte(client, IQS5XX_END_COMM, 0);
6858c2ecf20Sopenharmony_ci	if (error)
6868c2ecf20Sopenharmony_ci		return error;
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	iqs5xx->bl_status = dev_id_info->bl_status;
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	/*
6918c2ecf20Sopenharmony_ci	 * Closure of the first communication window that appears following the
6928c2ecf20Sopenharmony_ci	 * release of reset appears to kick off an initialization period during
6938c2ecf20Sopenharmony_ci	 * which further communication is met with clock stretching. The return
6948c2ecf20Sopenharmony_ci	 * from this function is delayed so that further communication attempts
6958c2ecf20Sopenharmony_ci	 * avoid this period.
6968c2ecf20Sopenharmony_ci	 */
6978c2ecf20Sopenharmony_ci	msleep(100);
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	return 0;
7008c2ecf20Sopenharmony_ci}
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_cistatic irqreturn_t iqs5xx_irq(int irq, void *data)
7038c2ecf20Sopenharmony_ci{
7048c2ecf20Sopenharmony_ci	struct iqs5xx_private *iqs5xx = data;
7058c2ecf20Sopenharmony_ci	struct iqs5xx_touch_data touch_data[IQS5XX_NUM_CONTACTS];
7068c2ecf20Sopenharmony_ci	struct i2c_client *client = iqs5xx->client;
7078c2ecf20Sopenharmony_ci	struct input_dev *input = iqs5xx->input;
7088c2ecf20Sopenharmony_ci	int error, i;
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	/*
7118c2ecf20Sopenharmony_ci	 * This check is purely a precaution, as the device does not assert the
7128c2ecf20Sopenharmony_ci	 * RDY output during bootloader mode. If the device operates outside of
7138c2ecf20Sopenharmony_ci	 * bootloader mode, the input device is guaranteed to be allocated.
7148c2ecf20Sopenharmony_ci	 */
7158c2ecf20Sopenharmony_ci	if (iqs5xx->bl_status == IQS5XX_BL_STATUS_RESET)
7168c2ecf20Sopenharmony_ci		return IRQ_NONE;
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	error = iqs5xx_read_burst(client, IQS5XX_ABS_X,
7198c2ecf20Sopenharmony_ci				  touch_data, sizeof(touch_data));
7208c2ecf20Sopenharmony_ci	if (error)
7218c2ecf20Sopenharmony_ci		return IRQ_NONE;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(touch_data); i++) {
7248c2ecf20Sopenharmony_ci		u16 pressure = be16_to_cpu(touch_data[i].strength);
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci		input_mt_slot(input, i);
7278c2ecf20Sopenharmony_ci		if (input_mt_report_slot_state(input, MT_TOOL_FINGER,
7288c2ecf20Sopenharmony_ci					       pressure != 0)) {
7298c2ecf20Sopenharmony_ci			input_report_abs(input, ABS_MT_POSITION_X,
7308c2ecf20Sopenharmony_ci					 be16_to_cpu(touch_data[i].abs_x));
7318c2ecf20Sopenharmony_ci			input_report_abs(input, ABS_MT_POSITION_Y,
7328c2ecf20Sopenharmony_ci					 be16_to_cpu(touch_data[i].abs_y));
7338c2ecf20Sopenharmony_ci			input_report_abs(input, ABS_MT_PRESSURE, pressure);
7348c2ecf20Sopenharmony_ci		}
7358c2ecf20Sopenharmony_ci	}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	input_mt_sync_frame(input);
7388c2ecf20Sopenharmony_ci	input_sync(input);
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	error = iqs5xx_write_byte(client, IQS5XX_END_COMM, 0);
7418c2ecf20Sopenharmony_ci	if (error)
7428c2ecf20Sopenharmony_ci		return IRQ_NONE;
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	/*
7458c2ecf20Sopenharmony_ci	 * Once the communication window is closed, a small delay is added to
7468c2ecf20Sopenharmony_ci	 * ensure the device's RDY output has been deasserted by the time the
7478c2ecf20Sopenharmony_ci	 * interrupt handler returns.
7488c2ecf20Sopenharmony_ci	 */
7498c2ecf20Sopenharmony_ci	usleep_range(50, 100);
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
7528c2ecf20Sopenharmony_ci}
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_cistatic int iqs5xx_fw_file_parse(struct i2c_client *client,
7558c2ecf20Sopenharmony_ci				const char *fw_file, u8 *pmap)
7568c2ecf20Sopenharmony_ci{
7578c2ecf20Sopenharmony_ci	const struct firmware *fw;
7588c2ecf20Sopenharmony_ci	struct iqs5xx_ihex_rec *rec;
7598c2ecf20Sopenharmony_ci	size_t pos = 0;
7608c2ecf20Sopenharmony_ci	int error, i;
7618c2ecf20Sopenharmony_ci	u16 rec_num = 1;
7628c2ecf20Sopenharmony_ci	u16 rec_addr;
7638c2ecf20Sopenharmony_ci	u8 rec_len, rec_type, rec_chksm, chksm;
7648c2ecf20Sopenharmony_ci	u8 rec_hdr[IQS5XX_REC_HDR_LEN];
7658c2ecf20Sopenharmony_ci	u8 rec_data[IQS5XX_REC_LEN_MAX];
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	/*
7688c2ecf20Sopenharmony_ci	 * Firmware exported from the vendor's configuration tool deviates from
7698c2ecf20Sopenharmony_ci	 * standard ihex as follows: (1) the checksum for records corresponding
7708c2ecf20Sopenharmony_ci	 * to user-exported settings is not recalculated, and (2) an address of
7718c2ecf20Sopenharmony_ci	 * 0xFFFF is used for the EOF record.
7728c2ecf20Sopenharmony_ci	 *
7738c2ecf20Sopenharmony_ci	 * Because the ihex2fw tool tolerates neither (1) nor (2), the slightly
7748c2ecf20Sopenharmony_ci	 * nonstandard ihex firmware is parsed directly by the driver.
7758c2ecf20Sopenharmony_ci	 */
7768c2ecf20Sopenharmony_ci	error = request_firmware(&fw, fw_file, &client->dev);
7778c2ecf20Sopenharmony_ci	if (error) {
7788c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to request firmware %s: %d\n",
7798c2ecf20Sopenharmony_ci			fw_file, error);
7808c2ecf20Sopenharmony_ci		return error;
7818c2ecf20Sopenharmony_ci	}
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	do {
7848c2ecf20Sopenharmony_ci		if (pos + sizeof(*rec) > fw->size) {
7858c2ecf20Sopenharmony_ci			dev_err(&client->dev, "Insufficient firmware size\n");
7868c2ecf20Sopenharmony_ci			error = -EINVAL;
7878c2ecf20Sopenharmony_ci			break;
7888c2ecf20Sopenharmony_ci		}
7898c2ecf20Sopenharmony_ci		rec = (struct iqs5xx_ihex_rec *)(fw->data + pos);
7908c2ecf20Sopenharmony_ci		pos += sizeof(*rec);
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci		if (rec->start != ':') {
7938c2ecf20Sopenharmony_ci			dev_err(&client->dev, "Invalid start at record %u\n",
7948c2ecf20Sopenharmony_ci				rec_num);
7958c2ecf20Sopenharmony_ci			error = -EINVAL;
7968c2ecf20Sopenharmony_ci			break;
7978c2ecf20Sopenharmony_ci		}
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci		error = hex2bin(rec_hdr, rec->len, sizeof(rec_hdr));
8008c2ecf20Sopenharmony_ci		if (error) {
8018c2ecf20Sopenharmony_ci			dev_err(&client->dev, "Invalid header at record %u\n",
8028c2ecf20Sopenharmony_ci				rec_num);
8038c2ecf20Sopenharmony_ci			break;
8048c2ecf20Sopenharmony_ci		}
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci		rec_len = *rec_hdr;
8078c2ecf20Sopenharmony_ci		rec_addr = get_unaligned_be16(rec_hdr + sizeof(rec_len));
8088c2ecf20Sopenharmony_ci		rec_type = *(rec_hdr + sizeof(rec_len) + sizeof(rec_addr));
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci		if (pos + rec_len * 2 > fw->size) {
8118c2ecf20Sopenharmony_ci			dev_err(&client->dev, "Insufficient firmware size\n");
8128c2ecf20Sopenharmony_ci			error = -EINVAL;
8138c2ecf20Sopenharmony_ci			break;
8148c2ecf20Sopenharmony_ci		}
8158c2ecf20Sopenharmony_ci		pos += (rec_len * 2);
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci		error = hex2bin(rec_data, rec->data, rec_len);
8188c2ecf20Sopenharmony_ci		if (error) {
8198c2ecf20Sopenharmony_ci			dev_err(&client->dev, "Invalid data at record %u\n",
8208c2ecf20Sopenharmony_ci				rec_num);
8218c2ecf20Sopenharmony_ci			break;
8228c2ecf20Sopenharmony_ci		}
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci		error = hex2bin(&rec_chksm,
8258c2ecf20Sopenharmony_ci				rec->data + rec_len * 2, sizeof(rec_chksm));
8268c2ecf20Sopenharmony_ci		if (error) {
8278c2ecf20Sopenharmony_ci			dev_err(&client->dev, "Invalid checksum at record %u\n",
8288c2ecf20Sopenharmony_ci				rec_num);
8298c2ecf20Sopenharmony_ci			break;
8308c2ecf20Sopenharmony_ci		}
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci		chksm = 0;
8338c2ecf20Sopenharmony_ci		for (i = 0; i < sizeof(rec_hdr); i++)
8348c2ecf20Sopenharmony_ci			chksm += rec_hdr[i];
8358c2ecf20Sopenharmony_ci		for (i = 0; i < rec_len; i++)
8368c2ecf20Sopenharmony_ci			chksm += rec_data[i];
8378c2ecf20Sopenharmony_ci		chksm = ~chksm + 1;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci		if (chksm != rec_chksm && rec_addr < IQS5XX_CSTM) {
8408c2ecf20Sopenharmony_ci			dev_err(&client->dev,
8418c2ecf20Sopenharmony_ci				"Incorrect checksum at record %u\n",
8428c2ecf20Sopenharmony_ci				rec_num);
8438c2ecf20Sopenharmony_ci			error = -EINVAL;
8448c2ecf20Sopenharmony_ci			break;
8458c2ecf20Sopenharmony_ci		}
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci		switch (rec_type) {
8488c2ecf20Sopenharmony_ci		case IQS5XX_REC_TYPE_DATA:
8498c2ecf20Sopenharmony_ci			if (rec_addr < IQS5XX_CHKSM ||
8508c2ecf20Sopenharmony_ci			    rec_addr > IQS5XX_PMAP_END) {
8518c2ecf20Sopenharmony_ci				dev_err(&client->dev,
8528c2ecf20Sopenharmony_ci					"Invalid address at record %u\n",
8538c2ecf20Sopenharmony_ci					rec_num);
8548c2ecf20Sopenharmony_ci				error = -EINVAL;
8558c2ecf20Sopenharmony_ci			} else {
8568c2ecf20Sopenharmony_ci				memcpy(pmap + rec_addr - IQS5XX_CHKSM,
8578c2ecf20Sopenharmony_ci				       rec_data, rec_len);
8588c2ecf20Sopenharmony_ci			}
8598c2ecf20Sopenharmony_ci			break;
8608c2ecf20Sopenharmony_ci		case IQS5XX_REC_TYPE_EOF:
8618c2ecf20Sopenharmony_ci			break;
8628c2ecf20Sopenharmony_ci		default:
8638c2ecf20Sopenharmony_ci			dev_err(&client->dev, "Invalid type at record %u\n",
8648c2ecf20Sopenharmony_ci				rec_num);
8658c2ecf20Sopenharmony_ci			error = -EINVAL;
8668c2ecf20Sopenharmony_ci		}
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci		if (error)
8698c2ecf20Sopenharmony_ci			break;
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci		rec_num++;
8728c2ecf20Sopenharmony_ci		while (pos < fw->size) {
8738c2ecf20Sopenharmony_ci			if (*(fw->data + pos) == ':')
8748c2ecf20Sopenharmony_ci				break;
8758c2ecf20Sopenharmony_ci			pos++;
8768c2ecf20Sopenharmony_ci		}
8778c2ecf20Sopenharmony_ci	} while (rec_type != IQS5XX_REC_TYPE_EOF);
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	release_firmware(fw);
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	return error;
8828c2ecf20Sopenharmony_ci}
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_cistatic int iqs5xx_fw_file_write(struct i2c_client *client, const char *fw_file)
8858c2ecf20Sopenharmony_ci{
8868c2ecf20Sopenharmony_ci	struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
8878c2ecf20Sopenharmony_ci	int error;
8888c2ecf20Sopenharmony_ci	u8 *pmap;
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	if (iqs5xx->bl_status == IQS5XX_BL_STATUS_NONE)
8918c2ecf20Sopenharmony_ci		return -EPERM;
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci	pmap = kzalloc(IQS5XX_PMAP_LEN, GFP_KERNEL);
8948c2ecf20Sopenharmony_ci	if (!pmap)
8958c2ecf20Sopenharmony_ci		return -ENOMEM;
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	error = iqs5xx_fw_file_parse(client, fw_file, pmap);
8988c2ecf20Sopenharmony_ci	if (error)
8998c2ecf20Sopenharmony_ci		goto err_kfree;
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci	mutex_lock(&iqs5xx->lock);
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci	/*
9048c2ecf20Sopenharmony_ci	 * Disable the interrupt line in case the first attempt(s) to enter the
9058c2ecf20Sopenharmony_ci	 * bootloader don't happen quickly enough, in which case the device may
9068c2ecf20Sopenharmony_ci	 * assert the RDY output until the next attempt.
9078c2ecf20Sopenharmony_ci	 */
9088c2ecf20Sopenharmony_ci	disable_irq(client->irq);
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	iqs5xx->bl_status = IQS5XX_BL_STATUS_RESET;
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	error = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_VER, 0);
9138c2ecf20Sopenharmony_ci	if (error) {
9148c2ecf20Sopenharmony_ci		error = iqs5xx_bl_open(client);
9158c2ecf20Sopenharmony_ci		if (error)
9168c2ecf20Sopenharmony_ci			goto err_reset;
9178c2ecf20Sopenharmony_ci	}
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	error = iqs5xx_bl_write(client, IQS5XX_CHKSM, pmap, IQS5XX_PMAP_LEN);
9208c2ecf20Sopenharmony_ci	if (error)
9218c2ecf20Sopenharmony_ci		goto err_reset;
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	error = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_CRC, 0);
9248c2ecf20Sopenharmony_ci	if (error)
9258c2ecf20Sopenharmony_ci		goto err_reset;
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci	error = iqs5xx_bl_verify(client, IQS5XX_CSTM,
9288c2ecf20Sopenharmony_ci				 pmap + IQS5XX_CHKSM_LEN + IQS5XX_APP_LEN,
9298c2ecf20Sopenharmony_ci				 IQS5XX_CSTM_LEN);
9308c2ecf20Sopenharmony_ci	if (error)
9318c2ecf20Sopenharmony_ci		goto err_reset;
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci	error = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_EXEC, 0);
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_cierr_reset:
9368c2ecf20Sopenharmony_ci	if (error) {
9378c2ecf20Sopenharmony_ci		iqs5xx_reset(client);
9388c2ecf20Sopenharmony_ci		usleep_range(10000, 10100);
9398c2ecf20Sopenharmony_ci	}
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	error = iqs5xx_dev_init(client);
9428c2ecf20Sopenharmony_ci	if (!error && iqs5xx->bl_status == IQS5XX_BL_STATUS_RESET)
9438c2ecf20Sopenharmony_ci		error = -EINVAL;
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	enable_irq(client->irq);
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci	mutex_unlock(&iqs5xx->lock);
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_cierr_kfree:
9508c2ecf20Sopenharmony_ci	kfree(pmap);
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci	return error;
9538c2ecf20Sopenharmony_ci}
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_cistatic ssize_t fw_file_store(struct device *dev, struct device_attribute *attr,
9568c2ecf20Sopenharmony_ci				const char *buf, size_t count)
9578c2ecf20Sopenharmony_ci{
9588c2ecf20Sopenharmony_ci	struct iqs5xx_private *iqs5xx = dev_get_drvdata(dev);
9598c2ecf20Sopenharmony_ci	struct i2c_client *client = iqs5xx->client;
9608c2ecf20Sopenharmony_ci	size_t len = count;
9618c2ecf20Sopenharmony_ci	bool input_reg = !iqs5xx->input;
9628c2ecf20Sopenharmony_ci	char fw_file[IQS5XX_FW_FILE_LEN + 1];
9638c2ecf20Sopenharmony_ci	int error;
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci	if (!len)
9668c2ecf20Sopenharmony_ci		return -EINVAL;
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci	if (buf[len - 1] == '\n')
9698c2ecf20Sopenharmony_ci		len--;
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	if (len > IQS5XX_FW_FILE_LEN)
9728c2ecf20Sopenharmony_ci		return -ENAMETOOLONG;
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	memcpy(fw_file, buf, len);
9758c2ecf20Sopenharmony_ci	fw_file[len] = '\0';
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ci	error = iqs5xx_fw_file_write(client, fw_file);
9788c2ecf20Sopenharmony_ci	if (error)
9798c2ecf20Sopenharmony_ci		return error;
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci	/*
9828c2ecf20Sopenharmony_ci	 * If the input device was not allocated already, it is guaranteed to
9838c2ecf20Sopenharmony_ci	 * be allocated by this point and can finally be registered.
9848c2ecf20Sopenharmony_ci	 */
9858c2ecf20Sopenharmony_ci	if (input_reg) {
9868c2ecf20Sopenharmony_ci		error = input_register_device(iqs5xx->input);
9878c2ecf20Sopenharmony_ci		if (error) {
9888c2ecf20Sopenharmony_ci			dev_err(&client->dev,
9898c2ecf20Sopenharmony_ci				"Failed to register device: %d\n",
9908c2ecf20Sopenharmony_ci				error);
9918c2ecf20Sopenharmony_ci			return error;
9928c2ecf20Sopenharmony_ci		}
9938c2ecf20Sopenharmony_ci	}
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci	return count;
9968c2ecf20Sopenharmony_ci}
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_cistatic DEVICE_ATTR_WO(fw_file);
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_cistatic struct attribute *iqs5xx_attrs[] = {
10018c2ecf20Sopenharmony_ci	&dev_attr_fw_file.attr,
10028c2ecf20Sopenharmony_ci	NULL,
10038c2ecf20Sopenharmony_ci};
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_cistatic const struct attribute_group iqs5xx_attr_group = {
10068c2ecf20Sopenharmony_ci	.attrs = iqs5xx_attrs,
10078c2ecf20Sopenharmony_ci};
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_cistatic int __maybe_unused iqs5xx_suspend(struct device *dev)
10108c2ecf20Sopenharmony_ci{
10118c2ecf20Sopenharmony_ci	struct iqs5xx_private *iqs5xx = dev_get_drvdata(dev);
10128c2ecf20Sopenharmony_ci	struct input_dev *input = iqs5xx->input;
10138c2ecf20Sopenharmony_ci	int error = 0;
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci	if (!input)
10168c2ecf20Sopenharmony_ci		return error;
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci	mutex_lock(&input->mutex);
10198c2ecf20Sopenharmony_ci
10208c2ecf20Sopenharmony_ci	if (input->users)
10218c2ecf20Sopenharmony_ci		error = iqs5xx_set_state(iqs5xx->client, IQS5XX_SUSPEND);
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	mutex_unlock(&input->mutex);
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_ci	return error;
10268c2ecf20Sopenharmony_ci}
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_cistatic int __maybe_unused iqs5xx_resume(struct device *dev)
10298c2ecf20Sopenharmony_ci{
10308c2ecf20Sopenharmony_ci	struct iqs5xx_private *iqs5xx = dev_get_drvdata(dev);
10318c2ecf20Sopenharmony_ci	struct input_dev *input = iqs5xx->input;
10328c2ecf20Sopenharmony_ci	int error = 0;
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_ci	if (!input)
10358c2ecf20Sopenharmony_ci		return error;
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci	mutex_lock(&input->mutex);
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci	if (input->users)
10408c2ecf20Sopenharmony_ci		error = iqs5xx_set_state(iqs5xx->client, IQS5XX_RESUME);
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci	mutex_unlock(&input->mutex);
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci	return error;
10458c2ecf20Sopenharmony_ci}
10468c2ecf20Sopenharmony_ci
10478c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(iqs5xx_pm, iqs5xx_suspend, iqs5xx_resume);
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_cistatic int iqs5xx_probe(struct i2c_client *client,
10508c2ecf20Sopenharmony_ci			const struct i2c_device_id *id)
10518c2ecf20Sopenharmony_ci{
10528c2ecf20Sopenharmony_ci	struct iqs5xx_private *iqs5xx;
10538c2ecf20Sopenharmony_ci	int error;
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci	iqs5xx = devm_kzalloc(&client->dev, sizeof(*iqs5xx), GFP_KERNEL);
10568c2ecf20Sopenharmony_ci	if (!iqs5xx)
10578c2ecf20Sopenharmony_ci		return -ENOMEM;
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, iqs5xx);
10608c2ecf20Sopenharmony_ci	iqs5xx->client = client;
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	iqs5xx->reset_gpio = devm_gpiod_get(&client->dev,
10638c2ecf20Sopenharmony_ci					    "reset", GPIOD_OUT_LOW);
10648c2ecf20Sopenharmony_ci	if (IS_ERR(iqs5xx->reset_gpio)) {
10658c2ecf20Sopenharmony_ci		error = PTR_ERR(iqs5xx->reset_gpio);
10668c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to request GPIO: %d\n", error);
10678c2ecf20Sopenharmony_ci		return error;
10688c2ecf20Sopenharmony_ci	}
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_ci	mutex_init(&iqs5xx->lock);
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci	iqs5xx_reset(client);
10738c2ecf20Sopenharmony_ci	usleep_range(10000, 10100);
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci	error = iqs5xx_dev_init(client);
10768c2ecf20Sopenharmony_ci	if (error)
10778c2ecf20Sopenharmony_ci		return error;
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_ci	error = devm_request_threaded_irq(&client->dev, client->irq,
10808c2ecf20Sopenharmony_ci					  NULL, iqs5xx_irq, IRQF_ONESHOT,
10818c2ecf20Sopenharmony_ci					  client->name, iqs5xx);
10828c2ecf20Sopenharmony_ci	if (error) {
10838c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
10848c2ecf20Sopenharmony_ci		return error;
10858c2ecf20Sopenharmony_ci	}
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci	error = devm_device_add_group(&client->dev, &iqs5xx_attr_group);
10888c2ecf20Sopenharmony_ci	if (error) {
10898c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to add attributes: %d\n", error);
10908c2ecf20Sopenharmony_ci		return error;
10918c2ecf20Sopenharmony_ci	}
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_ci	if (iqs5xx->input) {
10948c2ecf20Sopenharmony_ci		error = input_register_device(iqs5xx->input);
10958c2ecf20Sopenharmony_ci		if (error)
10968c2ecf20Sopenharmony_ci			dev_err(&client->dev,
10978c2ecf20Sopenharmony_ci				"Failed to register device: %d\n",
10988c2ecf20Sopenharmony_ci				error);
10998c2ecf20Sopenharmony_ci	}
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci	return error;
11028c2ecf20Sopenharmony_ci}
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_cistatic const struct i2c_device_id iqs5xx_id[] = {
11058c2ecf20Sopenharmony_ci	{ "iqs550", 0 },
11068c2ecf20Sopenharmony_ci	{ "iqs572", 1 },
11078c2ecf20Sopenharmony_ci	{ "iqs525", 2 },
11088c2ecf20Sopenharmony_ci	{ }
11098c2ecf20Sopenharmony_ci};
11108c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, iqs5xx_id);
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_cistatic const struct of_device_id iqs5xx_of_match[] = {
11138c2ecf20Sopenharmony_ci	{ .compatible = "azoteq,iqs550" },
11148c2ecf20Sopenharmony_ci	{ .compatible = "azoteq,iqs572" },
11158c2ecf20Sopenharmony_ci	{ .compatible = "azoteq,iqs525" },
11168c2ecf20Sopenharmony_ci	{ }
11178c2ecf20Sopenharmony_ci};
11188c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, iqs5xx_of_match);
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_cistatic struct i2c_driver iqs5xx_i2c_driver = {
11218c2ecf20Sopenharmony_ci	.driver = {
11228c2ecf20Sopenharmony_ci		.name		= "iqs5xx",
11238c2ecf20Sopenharmony_ci		.of_match_table	= iqs5xx_of_match,
11248c2ecf20Sopenharmony_ci		.pm		= &iqs5xx_pm,
11258c2ecf20Sopenharmony_ci	},
11268c2ecf20Sopenharmony_ci	.id_table	= iqs5xx_id,
11278c2ecf20Sopenharmony_ci	.probe		= iqs5xx_probe,
11288c2ecf20Sopenharmony_ci};
11298c2ecf20Sopenharmony_cimodule_i2c_driver(iqs5xx_i2c_driver);
11308c2ecf20Sopenharmony_ci
11318c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
11328c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Azoteq IQS550/572/525 Trackpad/Touchscreen Controller");
11338c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1134