xref: /openbmc/linux/drivers/input/touchscreen/iqs5xx.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
17b5bb55dSJeff LaBundy // SPDX-License-Identifier: GPL-2.0+
27b5bb55dSJeff LaBundy /*
37b5bb55dSJeff LaBundy  * Azoteq IQS550/572/525 Trackpad/Touchscreen Controller
47b5bb55dSJeff LaBundy  *
5785a19d9SJeff LaBundy  * Copyright (C) 2018 Jeff LaBundy <jeff@labundy.com>
67b5bb55dSJeff LaBundy  *
77b5bb55dSJeff LaBundy  * These devices require firmware exported from a PC-based configuration tool
87b5bb55dSJeff LaBundy  * made available by the vendor. Firmware files may be pushed to the device's
97b5bb55dSJeff LaBundy  * nonvolatile memory by writing the filename to the 'fw_file' sysfs control.
107b5bb55dSJeff LaBundy  *
110cdd2e90SJeff LaBundy  * Link to PC-based configuration tool and datasheet: https://www.azoteq.com/
127b5bb55dSJeff LaBundy  */
137b5bb55dSJeff LaBundy 
14e10ba0d3SJeff LaBundy #include <linux/bits.h>
157b5bb55dSJeff LaBundy #include <linux/delay.h>
167b5bb55dSJeff LaBundy #include <linux/device.h>
177b5bb55dSJeff LaBundy #include <linux/err.h>
187b5bb55dSJeff LaBundy #include <linux/firmware.h>
197b5bb55dSJeff LaBundy #include <linux/gpio/consumer.h>
207b5bb55dSJeff LaBundy #include <linux/i2c.h>
217b5bb55dSJeff LaBundy #include <linux/input.h>
227b5bb55dSJeff LaBundy #include <linux/input/mt.h>
237b5bb55dSJeff LaBundy #include <linux/input/touchscreen.h>
247b5bb55dSJeff LaBundy #include <linux/interrupt.h>
257b5bb55dSJeff LaBundy #include <linux/kernel.h>
26*dbce1a7dSRob Herring #include <linux/mod_devicetable.h>
277b5bb55dSJeff LaBundy #include <linux/module.h>
287b5bb55dSJeff LaBundy #include <linux/slab.h>
297b5bb55dSJeff LaBundy #include <asm/unaligned.h>
307b5bb55dSJeff LaBundy 
317b5bb55dSJeff LaBundy #define IQS5XX_FW_FILE_LEN	64
327b5bb55dSJeff LaBundy #define IQS5XX_NUM_RETRIES	10
337b5bb55dSJeff LaBundy #define IQS5XX_NUM_CONTACTS	5
347b5bb55dSJeff LaBundy #define IQS5XX_WR_BYTES_MAX	2
357b5bb55dSJeff LaBundy 
367b5bb55dSJeff LaBundy #define IQS5XX_PROD_NUM_IQS550	40
377b5bb55dSJeff LaBundy #define IQS5XX_PROD_NUM_IQS572	58
387b5bb55dSJeff LaBundy #define IQS5XX_PROD_NUM_IQS525	52
397b5bb55dSJeff LaBundy 
40e10ba0d3SJeff LaBundy #define IQS5XX_SHOW_RESET	BIT(7)
41e10ba0d3SJeff LaBundy #define IQS5XX_ACK_RESET	BIT(7)
42e10ba0d3SJeff LaBundy 
43e10ba0d3SJeff LaBundy #define IQS5XX_SUSPEND		BIT(0)
44e10ba0d3SJeff LaBundy #define IQS5XX_RESUME		0
457b5bb55dSJeff LaBundy 
46050fac7fSJeff LaBundy #define IQS5XX_SETUP_COMPLETE	BIT(6)
47050fac7fSJeff LaBundy #define IQS5XX_WDT		BIT(5)
48050fac7fSJeff LaBundy #define IQS5XX_ALP_REATI	BIT(3)
49050fac7fSJeff LaBundy #define IQS5XX_REATI		BIT(2)
50050fac7fSJeff LaBundy 
51050fac7fSJeff LaBundy #define IQS5XX_TP_EVENT		BIT(2)
52050fac7fSJeff LaBundy #define IQS5XX_EVENT_MODE	BIT(0)
537b5bb55dSJeff LaBundy 
547b5bb55dSJeff LaBundy #define IQS5XX_PROD_NUM		0x0000
55e10ba0d3SJeff LaBundy #define IQS5XX_SYS_INFO0	0x000F
56e10ba0d3SJeff LaBundy #define IQS5XX_SYS_INFO1	0x0010
577b5bb55dSJeff LaBundy #define IQS5XX_SYS_CTRL0	0x0431
587b5bb55dSJeff LaBundy #define IQS5XX_SYS_CTRL1	0x0432
597b5bb55dSJeff LaBundy #define IQS5XX_SYS_CFG0		0x058E
607b5bb55dSJeff LaBundy #define IQS5XX_SYS_CFG1		0x058F
617b5bb55dSJeff LaBundy #define IQS5XX_X_RES		0x066E
627b5bb55dSJeff LaBundy #define IQS5XX_Y_RES		0x0670
63509c0083SJeff LaBundy #define IQS5XX_EXP_FILE		0x0677
647b5bb55dSJeff LaBundy #define IQS5XX_CHKSM		0x83C0
657b5bb55dSJeff LaBundy #define IQS5XX_APP		0x8400
667b5bb55dSJeff LaBundy #define IQS5XX_CSTM		0xBE00
677b5bb55dSJeff LaBundy #define IQS5XX_PMAP_END		0xBFFF
687b5bb55dSJeff LaBundy #define IQS5XX_END_COMM		0xEEEE
697b5bb55dSJeff LaBundy 
707b5bb55dSJeff LaBundy #define IQS5XX_CHKSM_LEN	(IQS5XX_APP - IQS5XX_CHKSM)
717b5bb55dSJeff LaBundy #define IQS5XX_APP_LEN		(IQS5XX_CSTM - IQS5XX_APP)
727b5bb55dSJeff LaBundy #define IQS5XX_CSTM_LEN		(IQS5XX_PMAP_END + 1 - IQS5XX_CSTM)
737b5bb55dSJeff LaBundy #define IQS5XX_PMAP_LEN		(IQS5XX_PMAP_END + 1 - IQS5XX_CHKSM)
747b5bb55dSJeff LaBundy 
757b5bb55dSJeff LaBundy #define IQS5XX_REC_HDR_LEN	4
767b5bb55dSJeff LaBundy #define IQS5XX_REC_LEN_MAX	255
777b5bb55dSJeff LaBundy #define IQS5XX_REC_TYPE_DATA	0x00
787b5bb55dSJeff LaBundy #define IQS5XX_REC_TYPE_EOF	0x01
797b5bb55dSJeff LaBundy 
807b5bb55dSJeff LaBundy #define IQS5XX_BL_ADDR_MASK	0x40
817b5bb55dSJeff LaBundy #define IQS5XX_BL_CMD_VER	0x00
827b5bb55dSJeff LaBundy #define IQS5XX_BL_CMD_READ	0x01
837b5bb55dSJeff LaBundy #define IQS5XX_BL_CMD_EXEC	0x02
847b5bb55dSJeff LaBundy #define IQS5XX_BL_CMD_CRC	0x03
857b5bb55dSJeff LaBundy #define IQS5XX_BL_BLK_LEN_MAX	64
867b5bb55dSJeff LaBundy #define IQS5XX_BL_ID		0x0200
877b5bb55dSJeff LaBundy #define IQS5XX_BL_STATUS_NONE	0xEE
887b5bb55dSJeff LaBundy #define IQS5XX_BL_CRC_PASS	0x00
897b5bb55dSJeff LaBundy #define IQS5XX_BL_CRC_FAIL	0x01
907b5bb55dSJeff LaBundy #define IQS5XX_BL_ATTEMPTS	3
917b5bb55dSJeff LaBundy 
927b5bb55dSJeff LaBundy struct iqs5xx_dev_id_info {
937b5bb55dSJeff LaBundy 	__be16 prod_num;
947b5bb55dSJeff LaBundy 	__be16 proj_num;
957b5bb55dSJeff LaBundy 	u8 major_ver;
967b5bb55dSJeff LaBundy 	u8 minor_ver;
977b5bb55dSJeff LaBundy 	u8 bl_status;
987b5bb55dSJeff LaBundy } __packed;
997b5bb55dSJeff LaBundy 
1007b5bb55dSJeff LaBundy struct iqs5xx_ihex_rec {
1017b5bb55dSJeff LaBundy 	char start;
1027b5bb55dSJeff LaBundy 	char len[2];
1037b5bb55dSJeff LaBundy 	char addr[4];
1047b5bb55dSJeff LaBundy 	char type[2];
1057b5bb55dSJeff LaBundy 	char data[2];
1067b5bb55dSJeff LaBundy } __packed;
1077b5bb55dSJeff LaBundy 
1087b5bb55dSJeff LaBundy struct iqs5xx_touch_data {
1097b5bb55dSJeff LaBundy 	__be16 abs_x;
1107b5bb55dSJeff LaBundy 	__be16 abs_y;
1117b5bb55dSJeff LaBundy 	__be16 strength;
1127b5bb55dSJeff LaBundy 	u8 area;
1137b5bb55dSJeff LaBundy } __packed;
1147b5bb55dSJeff LaBundy 
115e10ba0d3SJeff LaBundy struct iqs5xx_status {
116e10ba0d3SJeff LaBundy 	u8 sys_info[2];
117e10ba0d3SJeff LaBundy 	u8 num_active;
118e10ba0d3SJeff LaBundy 	__be16 rel_x;
119e10ba0d3SJeff LaBundy 	__be16 rel_y;
120e10ba0d3SJeff LaBundy 	struct iqs5xx_touch_data touch_data[IQS5XX_NUM_CONTACTS];
121e10ba0d3SJeff LaBundy } __packed;
122e10ba0d3SJeff LaBundy 
123509c0083SJeff LaBundy struct iqs5xx_private {
124509c0083SJeff LaBundy 	struct i2c_client *client;
125509c0083SJeff LaBundy 	struct input_dev *input;
126509c0083SJeff LaBundy 	struct gpio_desc *reset_gpio;
127509c0083SJeff LaBundy 	struct touchscreen_properties prop;
128509c0083SJeff LaBundy 	struct mutex lock;
129509c0083SJeff LaBundy 	struct iqs5xx_dev_id_info dev_id_info;
130509c0083SJeff LaBundy 	u8 exp_file[2];
131509c0083SJeff LaBundy };
132509c0083SJeff LaBundy 
iqs5xx_read_burst(struct i2c_client * client,u16 reg,void * val,u16 len)1337b5bb55dSJeff LaBundy static int iqs5xx_read_burst(struct i2c_client *client,
1347b5bb55dSJeff LaBundy 			     u16 reg, void *val, u16 len)
1357b5bb55dSJeff LaBundy {
1367b5bb55dSJeff LaBundy 	__be16 reg_buf = cpu_to_be16(reg);
1377b5bb55dSJeff LaBundy 	int ret, i;
1387b5bb55dSJeff LaBundy 	struct i2c_msg msg[] = {
1397b5bb55dSJeff LaBundy 		{
1407b5bb55dSJeff LaBundy 			.addr = client->addr,
1417b5bb55dSJeff LaBundy 			.flags = 0,
1427b5bb55dSJeff LaBundy 			.len = sizeof(reg_buf),
1437b5bb55dSJeff LaBundy 			.buf = (u8 *)&reg_buf,
1447b5bb55dSJeff LaBundy 		},
1457b5bb55dSJeff LaBundy 		{
1467b5bb55dSJeff LaBundy 			.addr = client->addr,
1477b5bb55dSJeff LaBundy 			.flags = I2C_M_RD,
1487b5bb55dSJeff LaBundy 			.len = len,
1497b5bb55dSJeff LaBundy 			.buf = (u8 *)val,
1507b5bb55dSJeff LaBundy 		},
1517b5bb55dSJeff LaBundy 	};
1527b5bb55dSJeff LaBundy 
1537b5bb55dSJeff LaBundy 	/*
1547b5bb55dSJeff LaBundy 	 * The first addressing attempt outside of a communication window fails
1557b5bb55dSJeff LaBundy 	 * and must be retried, after which the device clock stretches until it
1567b5bb55dSJeff LaBundy 	 * is available.
1577b5bb55dSJeff LaBundy 	 */
1587b5bb55dSJeff LaBundy 	for (i = 0; i < IQS5XX_NUM_RETRIES; i++) {
1597b5bb55dSJeff LaBundy 		ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
1607b5bb55dSJeff LaBundy 		if (ret == ARRAY_SIZE(msg))
1617b5bb55dSJeff LaBundy 			return 0;
1627b5bb55dSJeff LaBundy 
1637b5bb55dSJeff LaBundy 		usleep_range(200, 300);
1647b5bb55dSJeff LaBundy 	}
1657b5bb55dSJeff LaBundy 
1667b5bb55dSJeff LaBundy 	if (ret >= 0)
1677b5bb55dSJeff LaBundy 		ret = -EIO;
1687b5bb55dSJeff LaBundy 
1697b5bb55dSJeff LaBundy 	dev_err(&client->dev, "Failed to read from address 0x%04X: %d\n",
1707b5bb55dSJeff LaBundy 		reg, ret);
1717b5bb55dSJeff LaBundy 
1727b5bb55dSJeff LaBundy 	return ret;
1737b5bb55dSJeff LaBundy }
1747b5bb55dSJeff LaBundy 
iqs5xx_read_word(struct i2c_client * client,u16 reg,u16 * val)1757b5bb55dSJeff LaBundy static int iqs5xx_read_word(struct i2c_client *client, u16 reg, u16 *val)
1767b5bb55dSJeff LaBundy {
1777b5bb55dSJeff LaBundy 	__be16 val_buf;
1787b5bb55dSJeff LaBundy 	int error;
1797b5bb55dSJeff LaBundy 
1807b5bb55dSJeff LaBundy 	error = iqs5xx_read_burst(client, reg, &val_buf, sizeof(val_buf));
1817b5bb55dSJeff LaBundy 	if (error)
1827b5bb55dSJeff LaBundy 		return error;
1837b5bb55dSJeff LaBundy 
1847b5bb55dSJeff LaBundy 	*val = be16_to_cpu(val_buf);
1857b5bb55dSJeff LaBundy 
1867b5bb55dSJeff LaBundy 	return 0;
1877b5bb55dSJeff LaBundy }
1887b5bb55dSJeff LaBundy 
iqs5xx_write_burst(struct i2c_client * client,u16 reg,const void * val,u16 len)1897b5bb55dSJeff LaBundy static int iqs5xx_write_burst(struct i2c_client *client,
1907b5bb55dSJeff LaBundy 			      u16 reg, const void *val, u16 len)
1917b5bb55dSJeff LaBundy {
1927b5bb55dSJeff LaBundy 	int ret, i;
1937b5bb55dSJeff LaBundy 	u16 mlen = sizeof(reg) + len;
1947b5bb55dSJeff LaBundy 	u8 mbuf[sizeof(reg) + IQS5XX_WR_BYTES_MAX];
1957b5bb55dSJeff LaBundy 
1967b5bb55dSJeff LaBundy 	if (len > IQS5XX_WR_BYTES_MAX)
1977b5bb55dSJeff LaBundy 		return -EINVAL;
1987b5bb55dSJeff LaBundy 
1997b5bb55dSJeff LaBundy 	put_unaligned_be16(reg, mbuf);
2007b5bb55dSJeff LaBundy 	memcpy(mbuf + sizeof(reg), val, len);
2017b5bb55dSJeff LaBundy 
2027b5bb55dSJeff LaBundy 	/*
2037b5bb55dSJeff LaBundy 	 * The first addressing attempt outside of a communication window fails
2047b5bb55dSJeff LaBundy 	 * and must be retried, after which the device clock stretches until it
2057b5bb55dSJeff LaBundy 	 * is available.
2067b5bb55dSJeff LaBundy 	 */
2077b5bb55dSJeff LaBundy 	for (i = 0; i < IQS5XX_NUM_RETRIES; i++) {
2087b5bb55dSJeff LaBundy 		ret = i2c_master_send(client, mbuf, mlen);
2097b5bb55dSJeff LaBundy 		if (ret == mlen)
2107b5bb55dSJeff LaBundy 			return 0;
2117b5bb55dSJeff LaBundy 
2127b5bb55dSJeff LaBundy 		usleep_range(200, 300);
2137b5bb55dSJeff LaBundy 	}
2147b5bb55dSJeff LaBundy 
2157b5bb55dSJeff LaBundy 	if (ret >= 0)
2167b5bb55dSJeff LaBundy 		ret = -EIO;
2177b5bb55dSJeff LaBundy 
2187b5bb55dSJeff LaBundy 	dev_err(&client->dev, "Failed to write to address 0x%04X: %d\n",
2197b5bb55dSJeff LaBundy 		reg, ret);
2207b5bb55dSJeff LaBundy 
2217b5bb55dSJeff LaBundy 	return ret;
2227b5bb55dSJeff LaBundy }
2237b5bb55dSJeff LaBundy 
iqs5xx_write_word(struct i2c_client * client,u16 reg,u16 val)2247b5bb55dSJeff LaBundy static int iqs5xx_write_word(struct i2c_client *client, u16 reg, u16 val)
2257b5bb55dSJeff LaBundy {
2267b5bb55dSJeff LaBundy 	__be16 val_buf = cpu_to_be16(val);
2277b5bb55dSJeff LaBundy 
2287b5bb55dSJeff LaBundy 	return iqs5xx_write_burst(client, reg, &val_buf, sizeof(val_buf));
2297b5bb55dSJeff LaBundy }
2307b5bb55dSJeff LaBundy 
iqs5xx_write_byte(struct i2c_client * client,u16 reg,u8 val)2317b5bb55dSJeff LaBundy static int iqs5xx_write_byte(struct i2c_client *client, u16 reg, u8 val)
2327b5bb55dSJeff LaBundy {
2337b5bb55dSJeff LaBundy 	return iqs5xx_write_burst(client, reg, &val, sizeof(val));
2347b5bb55dSJeff LaBundy }
2357b5bb55dSJeff LaBundy 
iqs5xx_reset(struct i2c_client * client)2367b5bb55dSJeff LaBundy static void iqs5xx_reset(struct i2c_client *client)
2377b5bb55dSJeff LaBundy {
2387b5bb55dSJeff LaBundy 	struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
2397b5bb55dSJeff LaBundy 
2407b5bb55dSJeff LaBundy 	gpiod_set_value_cansleep(iqs5xx->reset_gpio, 1);
2417b5bb55dSJeff LaBundy 	usleep_range(200, 300);
2427b5bb55dSJeff LaBundy 
2437b5bb55dSJeff LaBundy 	gpiod_set_value_cansleep(iqs5xx->reset_gpio, 0);
2447b5bb55dSJeff LaBundy }
2457b5bb55dSJeff LaBundy 
iqs5xx_bl_cmd(struct i2c_client * client,u8 bl_cmd,u16 bl_addr)2467b5bb55dSJeff LaBundy static int iqs5xx_bl_cmd(struct i2c_client *client, u8 bl_cmd, u16 bl_addr)
2477b5bb55dSJeff LaBundy {
2487b5bb55dSJeff LaBundy 	struct i2c_msg msg;
2497b5bb55dSJeff LaBundy 	int ret;
2507b5bb55dSJeff LaBundy 	u8 mbuf[sizeof(bl_cmd) + sizeof(bl_addr)];
2517b5bb55dSJeff LaBundy 
2527b5bb55dSJeff LaBundy 	msg.addr = client->addr ^ IQS5XX_BL_ADDR_MASK;
2537b5bb55dSJeff LaBundy 	msg.flags = 0;
2547b5bb55dSJeff LaBundy 	msg.len = sizeof(bl_cmd);
2557b5bb55dSJeff LaBundy 	msg.buf = mbuf;
2567b5bb55dSJeff LaBundy 
2577b5bb55dSJeff LaBundy 	*mbuf = bl_cmd;
2587b5bb55dSJeff LaBundy 
2597b5bb55dSJeff LaBundy 	switch (bl_cmd) {
2607b5bb55dSJeff LaBundy 	case IQS5XX_BL_CMD_VER:
2617b5bb55dSJeff LaBundy 	case IQS5XX_BL_CMD_CRC:
2627b5bb55dSJeff LaBundy 	case IQS5XX_BL_CMD_EXEC:
2637b5bb55dSJeff LaBundy 		break;
2647b5bb55dSJeff LaBundy 	case IQS5XX_BL_CMD_READ:
2657b5bb55dSJeff LaBundy 		msg.len += sizeof(bl_addr);
2667b5bb55dSJeff LaBundy 		put_unaligned_be16(bl_addr, mbuf + sizeof(bl_cmd));
2677b5bb55dSJeff LaBundy 		break;
2687b5bb55dSJeff LaBundy 	default:
2697b5bb55dSJeff LaBundy 		return -EINVAL;
2707b5bb55dSJeff LaBundy 	}
2717b5bb55dSJeff LaBundy 
2727b5bb55dSJeff LaBundy 	ret = i2c_transfer(client->adapter, &msg, 1);
2737b5bb55dSJeff LaBundy 	if (ret != 1)
2747b5bb55dSJeff LaBundy 		goto msg_fail;
2757b5bb55dSJeff LaBundy 
2767b5bb55dSJeff LaBundy 	switch (bl_cmd) {
2777b5bb55dSJeff LaBundy 	case IQS5XX_BL_CMD_VER:
2787b5bb55dSJeff LaBundy 		msg.len = sizeof(u16);
2797b5bb55dSJeff LaBundy 		break;
2807b5bb55dSJeff LaBundy 	case IQS5XX_BL_CMD_CRC:
2817b5bb55dSJeff LaBundy 		msg.len = sizeof(u8);
2827b5bb55dSJeff LaBundy 		/*
2837b5bb55dSJeff LaBundy 		 * This delay saves the bus controller the trouble of having to
2847b5bb55dSJeff LaBundy 		 * tolerate a relatively long clock-stretching period while the
2857b5bb55dSJeff LaBundy 		 * CRC is calculated.
2867b5bb55dSJeff LaBundy 		 */
2877b5bb55dSJeff LaBundy 		msleep(50);
2887b5bb55dSJeff LaBundy 		break;
2897b5bb55dSJeff LaBundy 	case IQS5XX_BL_CMD_EXEC:
2907b5bb55dSJeff LaBundy 		usleep_range(10000, 10100);
2916f49c4f5SGustavo A. R. Silva 		fallthrough;
2927b5bb55dSJeff LaBundy 	default:
2937b5bb55dSJeff LaBundy 		return 0;
2947b5bb55dSJeff LaBundy 	}
2957b5bb55dSJeff LaBundy 
2967b5bb55dSJeff LaBundy 	msg.flags = I2C_M_RD;
2977b5bb55dSJeff LaBundy 
2987b5bb55dSJeff LaBundy 	ret = i2c_transfer(client->adapter, &msg, 1);
2997b5bb55dSJeff LaBundy 	if (ret != 1)
3007b5bb55dSJeff LaBundy 		goto msg_fail;
3017b5bb55dSJeff LaBundy 
3027b5bb55dSJeff LaBundy 	if (bl_cmd == IQS5XX_BL_CMD_VER &&
3037b5bb55dSJeff LaBundy 	    get_unaligned_be16(mbuf) != IQS5XX_BL_ID) {
3047b5bb55dSJeff LaBundy 		dev_err(&client->dev, "Unrecognized bootloader ID: 0x%04X\n",
3057b5bb55dSJeff LaBundy 			get_unaligned_be16(mbuf));
3067b5bb55dSJeff LaBundy 		return -EINVAL;
3077b5bb55dSJeff LaBundy 	}
3087b5bb55dSJeff LaBundy 
3097b5bb55dSJeff LaBundy 	if (bl_cmd == IQS5XX_BL_CMD_CRC && *mbuf != IQS5XX_BL_CRC_PASS) {
3107b5bb55dSJeff LaBundy 		dev_err(&client->dev, "Bootloader CRC failed\n");
3117b5bb55dSJeff LaBundy 		return -EIO;
3127b5bb55dSJeff LaBundy 	}
3137b5bb55dSJeff LaBundy 
3147b5bb55dSJeff LaBundy 	return 0;
3157b5bb55dSJeff LaBundy 
3167b5bb55dSJeff LaBundy msg_fail:
3177b5bb55dSJeff LaBundy 	if (ret >= 0)
3187b5bb55dSJeff LaBundy 		ret = -EIO;
3197b5bb55dSJeff LaBundy 
3207b5bb55dSJeff LaBundy 	if (bl_cmd != IQS5XX_BL_CMD_VER)
3217b5bb55dSJeff LaBundy 		dev_err(&client->dev,
3227b5bb55dSJeff LaBundy 			"Unsuccessful bootloader command 0x%02X: %d\n",
3237b5bb55dSJeff LaBundy 			bl_cmd, ret);
3247b5bb55dSJeff LaBundy 
3257b5bb55dSJeff LaBundy 	return ret;
3267b5bb55dSJeff LaBundy }
3277b5bb55dSJeff LaBundy 
iqs5xx_bl_open(struct i2c_client * client)3287b5bb55dSJeff LaBundy static int iqs5xx_bl_open(struct i2c_client *client)
3297b5bb55dSJeff LaBundy {
3307b5bb55dSJeff LaBundy 	int error, i, j;
3317b5bb55dSJeff LaBundy 
3327b5bb55dSJeff LaBundy 	/*
3337b5bb55dSJeff LaBundy 	 * The device opens a bootloader polling window for 2 ms following the
3347b5bb55dSJeff LaBundy 	 * release of reset. If the host cannot establish communication during
3357b5bb55dSJeff LaBundy 	 * this time frame, it must cycle reset again.
3367b5bb55dSJeff LaBundy 	 */
3377b5bb55dSJeff LaBundy 	for (i = 0; i < IQS5XX_BL_ATTEMPTS; i++) {
3387b5bb55dSJeff LaBundy 		iqs5xx_reset(client);
3391302c71aSJeff LaBundy 		usleep_range(350, 400);
3407b5bb55dSJeff LaBundy 
3417b5bb55dSJeff LaBundy 		for (j = 0; j < IQS5XX_NUM_RETRIES; j++) {
3427b5bb55dSJeff LaBundy 			error = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_VER, 0);
3431302c71aSJeff LaBundy 			if (!error)
3441302c71aSJeff LaBundy 				usleep_range(10000, 10100);
3451302c71aSJeff LaBundy 			else if (error != -EINVAL)
3461302c71aSJeff LaBundy 				continue;
3471302c71aSJeff LaBundy 
3487b5bb55dSJeff LaBundy 			return error;
3497b5bb55dSJeff LaBundy 		}
3507b5bb55dSJeff LaBundy 	}
3517b5bb55dSJeff LaBundy 
3527b5bb55dSJeff LaBundy 	dev_err(&client->dev, "Failed to open bootloader: %d\n", error);
3537b5bb55dSJeff LaBundy 
3547b5bb55dSJeff LaBundy 	return error;
3557b5bb55dSJeff LaBundy }
3567b5bb55dSJeff LaBundy 
iqs5xx_bl_write(struct i2c_client * client,u16 bl_addr,u8 * pmap_data,u16 pmap_len)3577b5bb55dSJeff LaBundy static int iqs5xx_bl_write(struct i2c_client *client,
3587b5bb55dSJeff LaBundy 			   u16 bl_addr, u8 *pmap_data, u16 pmap_len)
3597b5bb55dSJeff LaBundy {
3607b5bb55dSJeff LaBundy 	struct i2c_msg msg;
3617b5bb55dSJeff LaBundy 	int ret, i;
3627b5bb55dSJeff LaBundy 	u8 mbuf[sizeof(bl_addr) + IQS5XX_BL_BLK_LEN_MAX];
3637b5bb55dSJeff LaBundy 
3647b5bb55dSJeff LaBundy 	if (pmap_len % IQS5XX_BL_BLK_LEN_MAX)
3657b5bb55dSJeff LaBundy 		return -EINVAL;
3667b5bb55dSJeff LaBundy 
3677b5bb55dSJeff LaBundy 	msg.addr = client->addr ^ IQS5XX_BL_ADDR_MASK;
3687b5bb55dSJeff LaBundy 	msg.flags = 0;
3697b5bb55dSJeff LaBundy 	msg.len = sizeof(mbuf);
3707b5bb55dSJeff LaBundy 	msg.buf = mbuf;
3717b5bb55dSJeff LaBundy 
3727b5bb55dSJeff LaBundy 	for (i = 0; i < pmap_len; i += IQS5XX_BL_BLK_LEN_MAX) {
3737b5bb55dSJeff LaBundy 		put_unaligned_be16(bl_addr + i, mbuf);
3747b5bb55dSJeff LaBundy 		memcpy(mbuf + sizeof(bl_addr), pmap_data + i,
3757b5bb55dSJeff LaBundy 		       sizeof(mbuf) - sizeof(bl_addr));
3767b5bb55dSJeff LaBundy 
3777b5bb55dSJeff LaBundy 		ret = i2c_transfer(client->adapter, &msg, 1);
3787b5bb55dSJeff LaBundy 		if (ret != 1)
3797b5bb55dSJeff LaBundy 			goto msg_fail;
3807b5bb55dSJeff LaBundy 
3817b5bb55dSJeff LaBundy 		usleep_range(10000, 10100);
3827b5bb55dSJeff LaBundy 	}
3837b5bb55dSJeff LaBundy 
3847b5bb55dSJeff LaBundy 	return 0;
3857b5bb55dSJeff LaBundy 
3867b5bb55dSJeff LaBundy msg_fail:
3877b5bb55dSJeff LaBundy 	if (ret >= 0)
3887b5bb55dSJeff LaBundy 		ret = -EIO;
3897b5bb55dSJeff LaBundy 
3907b5bb55dSJeff LaBundy 	dev_err(&client->dev, "Failed to write block at address 0x%04X: %d\n",
3917b5bb55dSJeff LaBundy 		bl_addr + i, ret);
3927b5bb55dSJeff LaBundy 
3937b5bb55dSJeff LaBundy 	return ret;
3947b5bb55dSJeff LaBundy }
3957b5bb55dSJeff LaBundy 
iqs5xx_bl_verify(struct i2c_client * client,u16 bl_addr,u8 * pmap_data,u16 pmap_len)3967b5bb55dSJeff LaBundy static int iqs5xx_bl_verify(struct i2c_client *client,
3977b5bb55dSJeff LaBundy 			    u16 bl_addr, u8 *pmap_data, u16 pmap_len)
3987b5bb55dSJeff LaBundy {
3997b5bb55dSJeff LaBundy 	struct i2c_msg msg;
4007b5bb55dSJeff LaBundy 	int ret, i;
4017b5bb55dSJeff LaBundy 	u8 bl_data[IQS5XX_BL_BLK_LEN_MAX];
4027b5bb55dSJeff LaBundy 
4037b5bb55dSJeff LaBundy 	if (pmap_len % IQS5XX_BL_BLK_LEN_MAX)
4047b5bb55dSJeff LaBundy 		return -EINVAL;
4057b5bb55dSJeff LaBundy 
4067b5bb55dSJeff LaBundy 	msg.addr = client->addr ^ IQS5XX_BL_ADDR_MASK;
4077b5bb55dSJeff LaBundy 	msg.flags = I2C_M_RD;
4087b5bb55dSJeff LaBundy 	msg.len = sizeof(bl_data);
4097b5bb55dSJeff LaBundy 	msg.buf = bl_data;
4107b5bb55dSJeff LaBundy 
4117b5bb55dSJeff LaBundy 	for (i = 0; i < pmap_len; i += IQS5XX_BL_BLK_LEN_MAX) {
4127b5bb55dSJeff LaBundy 		ret = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_READ, bl_addr + i);
4137b5bb55dSJeff LaBundy 		if (ret)
4147b5bb55dSJeff LaBundy 			return ret;
4157b5bb55dSJeff LaBundy 
4167b5bb55dSJeff LaBundy 		ret = i2c_transfer(client->adapter, &msg, 1);
4177b5bb55dSJeff LaBundy 		if (ret != 1)
4187b5bb55dSJeff LaBundy 			goto msg_fail;
4197b5bb55dSJeff LaBundy 
4207b5bb55dSJeff LaBundy 		if (memcmp(bl_data, pmap_data + i, sizeof(bl_data))) {
4217b5bb55dSJeff LaBundy 			dev_err(&client->dev,
4227b5bb55dSJeff LaBundy 				"Failed to verify block at address 0x%04X\n",
4237b5bb55dSJeff LaBundy 				bl_addr + i);
4247b5bb55dSJeff LaBundy 			return -EIO;
4257b5bb55dSJeff LaBundy 		}
4267b5bb55dSJeff LaBundy 	}
4277b5bb55dSJeff LaBundy 
4287b5bb55dSJeff LaBundy 	return 0;
4297b5bb55dSJeff LaBundy 
4307b5bb55dSJeff LaBundy msg_fail:
4317b5bb55dSJeff LaBundy 	if (ret >= 0)
4327b5bb55dSJeff LaBundy 		ret = -EIO;
4337b5bb55dSJeff LaBundy 
4347b5bb55dSJeff LaBundy 	dev_err(&client->dev, "Failed to read block at address 0x%04X: %d\n",
4357b5bb55dSJeff LaBundy 		bl_addr + i, ret);
4367b5bb55dSJeff LaBundy 
4377b5bb55dSJeff LaBundy 	return ret;
4387b5bb55dSJeff LaBundy }
4397b5bb55dSJeff LaBundy 
iqs5xx_set_state(struct i2c_client * client,u8 state)4407b5bb55dSJeff LaBundy static int iqs5xx_set_state(struct i2c_client *client, u8 state)
4417b5bb55dSJeff LaBundy {
4427b5bb55dSJeff LaBundy 	struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
4437b5bb55dSJeff LaBundy 	int error1, error2;
4447b5bb55dSJeff LaBundy 
445509c0083SJeff LaBundy 	if (!iqs5xx->dev_id_info.bl_status)
4467b5bb55dSJeff LaBundy 		return 0;
4477b5bb55dSJeff LaBundy 
4487b5bb55dSJeff LaBundy 	mutex_lock(&iqs5xx->lock);
4497b5bb55dSJeff LaBundy 
4507b5bb55dSJeff LaBundy 	/*
4517b5bb55dSJeff LaBundy 	 * Addressing the device outside of a communication window prompts it
4527b5bb55dSJeff LaBundy 	 * to assert the RDY output, so disable the interrupt line to prevent
4537b5bb55dSJeff LaBundy 	 * the handler from servicing a false interrupt.
4547b5bb55dSJeff LaBundy 	 */
4557b5bb55dSJeff LaBundy 	disable_irq(client->irq);
4567b5bb55dSJeff LaBundy 
4577b5bb55dSJeff LaBundy 	error1 = iqs5xx_write_byte(client, IQS5XX_SYS_CTRL1, state);
4587b5bb55dSJeff LaBundy 	error2 = iqs5xx_write_byte(client, IQS5XX_END_COMM, 0);
4597b5bb55dSJeff LaBundy 
4607b5bb55dSJeff LaBundy 	usleep_range(50, 100);
4617b5bb55dSJeff LaBundy 	enable_irq(client->irq);
4627b5bb55dSJeff LaBundy 
4637b5bb55dSJeff LaBundy 	mutex_unlock(&iqs5xx->lock);
4647b5bb55dSJeff LaBundy 
4657b5bb55dSJeff LaBundy 	if (error1)
4667b5bb55dSJeff LaBundy 		return error1;
4677b5bb55dSJeff LaBundy 
4687b5bb55dSJeff LaBundy 	return error2;
4697b5bb55dSJeff LaBundy }
4707b5bb55dSJeff LaBundy 
iqs5xx_open(struct input_dev * input)4717b5bb55dSJeff LaBundy static int iqs5xx_open(struct input_dev *input)
4727b5bb55dSJeff LaBundy {
4737b5bb55dSJeff LaBundy 	struct iqs5xx_private *iqs5xx = input_get_drvdata(input);
4747b5bb55dSJeff LaBundy 
4757b5bb55dSJeff LaBundy 	return iqs5xx_set_state(iqs5xx->client, IQS5XX_RESUME);
4767b5bb55dSJeff LaBundy }
4777b5bb55dSJeff LaBundy 
iqs5xx_close(struct input_dev * input)4787b5bb55dSJeff LaBundy static void iqs5xx_close(struct input_dev *input)
4797b5bb55dSJeff LaBundy {
4807b5bb55dSJeff LaBundy 	struct iqs5xx_private *iqs5xx = input_get_drvdata(input);
4817b5bb55dSJeff LaBundy 
4827b5bb55dSJeff LaBundy 	iqs5xx_set_state(iqs5xx->client, IQS5XX_SUSPEND);
4837b5bb55dSJeff LaBundy }
4847b5bb55dSJeff LaBundy 
iqs5xx_axis_init(struct i2c_client * client)4857b5bb55dSJeff LaBundy static int iqs5xx_axis_init(struct i2c_client *client)
4867b5bb55dSJeff LaBundy {
4877b5bb55dSJeff LaBundy 	struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
4884a76d861SJeff LaBundy 	struct touchscreen_properties *prop = &iqs5xx->prop;
48907fc21b4SJeff LaBundy 	struct input_dev *input = iqs5xx->input;
4904a76d861SJeff LaBundy 	u16 max_x, max_y;
4917b5bb55dSJeff LaBundy 	int error;
4927b5bb55dSJeff LaBundy 
49307fc21b4SJeff LaBundy 	if (!input) {
4947b5bb55dSJeff LaBundy 		input = devm_input_allocate_device(&client->dev);
4957b5bb55dSJeff LaBundy 		if (!input)
4967b5bb55dSJeff LaBundy 			return -ENOMEM;
4977b5bb55dSJeff LaBundy 
4987b5bb55dSJeff LaBundy 		input->name = client->name;
4997b5bb55dSJeff LaBundy 		input->id.bustype = BUS_I2C;
5007b5bb55dSJeff LaBundy 		input->open = iqs5xx_open;
5017b5bb55dSJeff LaBundy 		input->close = iqs5xx_close;
5027b5bb55dSJeff LaBundy 
5037b5bb55dSJeff LaBundy 		input_set_drvdata(input, iqs5xx);
5047b5bb55dSJeff LaBundy 		iqs5xx->input = input;
5057b5bb55dSJeff LaBundy 	}
5067b5bb55dSJeff LaBundy 
5077b5bb55dSJeff LaBundy 	error = iqs5xx_read_word(client, IQS5XX_X_RES, &max_x);
5087b5bb55dSJeff LaBundy 	if (error)
5097b5bb55dSJeff LaBundy 		return error;
5107b5bb55dSJeff LaBundy 
5117b5bb55dSJeff LaBundy 	error = iqs5xx_read_word(client, IQS5XX_Y_RES, &max_y);
5127b5bb55dSJeff LaBundy 	if (error)
5137b5bb55dSJeff LaBundy 		return error;
5147b5bb55dSJeff LaBundy 
51507fc21b4SJeff LaBundy 	input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_x, 0, 0);
51607fc21b4SJeff LaBundy 	input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_y, 0, 0);
51707fc21b4SJeff LaBundy 	input_set_abs_params(input, ABS_MT_PRESSURE, 0, U16_MAX, 0, 0);
5184a76d861SJeff LaBundy 
51907fc21b4SJeff LaBundy 	touchscreen_parse_properties(input, true, prop);
5204a76d861SJeff LaBundy 
52140c3efdcSJeff LaBundy 	/*
52240c3efdcSJeff LaBundy 	 * The device reserves 0xFFFF for coordinates that correspond to slots
52340c3efdcSJeff LaBundy 	 * which are not in a state of touch.
52440c3efdcSJeff LaBundy 	 */
52540c3efdcSJeff LaBundy 	if (prop->max_x >= U16_MAX || prop->max_y >= U16_MAX) {
52640c3efdcSJeff LaBundy 		dev_err(&client->dev, "Invalid touchscreen size: %u*%u\n",
52740c3efdcSJeff LaBundy 			prop->max_x, prop->max_y);
5284a76d861SJeff LaBundy 		return -EINVAL;
52940c3efdcSJeff LaBundy 	}
53040c3efdcSJeff LaBundy 
53140c3efdcSJeff LaBundy 	if (prop->max_x != max_x) {
5324a76d861SJeff LaBundy 		error = iqs5xx_write_word(client, IQS5XX_X_RES, prop->max_x);
5334a76d861SJeff LaBundy 		if (error)
5344a76d861SJeff LaBundy 			return error;
5357b5bb55dSJeff LaBundy 	}
5367b5bb55dSJeff LaBundy 
53740c3efdcSJeff LaBundy 	if (prop->max_y != max_y) {
5384a76d861SJeff LaBundy 		error = iqs5xx_write_word(client, IQS5XX_Y_RES, prop->max_y);
5397b5bb55dSJeff LaBundy 		if (error)
5407b5bb55dSJeff LaBundy 			return error;
5414a76d861SJeff LaBundy 	}
542a876697bSJeff LaBundy 
54307fc21b4SJeff LaBundy 	error = input_mt_init_slots(input, IQS5XX_NUM_CONTACTS,
544a876697bSJeff LaBundy 				    INPUT_MT_DIRECT);
545a876697bSJeff LaBundy 	if (error)
546a876697bSJeff LaBundy 		dev_err(&client->dev, "Failed to initialize slots: %d\n",
547a876697bSJeff LaBundy 			error);
548a876697bSJeff LaBundy 
549a876697bSJeff LaBundy 	return error;
5507b5bb55dSJeff LaBundy }
5517b5bb55dSJeff LaBundy 
iqs5xx_dev_init(struct i2c_client * client)5527b5bb55dSJeff LaBundy static int iqs5xx_dev_init(struct i2c_client *client)
5537b5bb55dSJeff LaBundy {
5547b5bb55dSJeff LaBundy 	struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
5557b5bb55dSJeff LaBundy 	struct iqs5xx_dev_id_info *dev_id_info;
5567b5bb55dSJeff LaBundy 	int error;
5577b5bb55dSJeff LaBundy 	u8 buf[sizeof(*dev_id_info) + 1];
5587b5bb55dSJeff LaBundy 
5597b5bb55dSJeff LaBundy 	error = iqs5xx_read_burst(client, IQS5XX_PROD_NUM,
5607b5bb55dSJeff LaBundy 				  &buf[1], sizeof(*dev_id_info));
5617b5bb55dSJeff LaBundy 	if (error)
5627b5bb55dSJeff LaBundy 		return iqs5xx_bl_open(client);
5637b5bb55dSJeff LaBundy 
5647b5bb55dSJeff LaBundy 	/*
5657b5bb55dSJeff LaBundy 	 * A000 and B000 devices use 8-bit and 16-bit addressing, respectively.
5667b5bb55dSJeff LaBundy 	 * Querying an A000 device's version information with 16-bit addressing
5677b5bb55dSJeff LaBundy 	 * gives the appearance that the data is shifted by one byte; a nonzero
5687b5bb55dSJeff LaBundy 	 * leading array element suggests this could be the case (in which case
5697b5bb55dSJeff LaBundy 	 * the missing zero is prepended).
5707b5bb55dSJeff LaBundy 	 */
5717b5bb55dSJeff LaBundy 	buf[0] = 0;
572e7d8e88aSJeff LaBundy 	dev_id_info = (struct iqs5xx_dev_id_info *)&buf[buf[1] ? 0 : 1];
5737b5bb55dSJeff LaBundy 
5747b5bb55dSJeff LaBundy 	switch (be16_to_cpu(dev_id_info->prod_num)) {
5757b5bb55dSJeff LaBundy 	case IQS5XX_PROD_NUM_IQS550:
5767b5bb55dSJeff LaBundy 	case IQS5XX_PROD_NUM_IQS572:
5777b5bb55dSJeff LaBundy 	case IQS5XX_PROD_NUM_IQS525:
5787b5bb55dSJeff LaBundy 		break;
5797b5bb55dSJeff LaBundy 	default:
5807b5bb55dSJeff LaBundy 		dev_err(&client->dev, "Unrecognized product number: %u\n",
5817b5bb55dSJeff LaBundy 			be16_to_cpu(dev_id_info->prod_num));
5827b5bb55dSJeff LaBundy 		return -EINVAL;
5837b5bb55dSJeff LaBundy 	}
5847b5bb55dSJeff LaBundy 
585e7d8e88aSJeff LaBundy 	/*
586e7d8e88aSJeff LaBundy 	 * With the product number recognized yet shifted by one byte, open the
587e7d8e88aSJeff LaBundy 	 * bootloader and wait for user space to convert the A000 device into a
588e7d8e88aSJeff LaBundy 	 * B000 device via new firmware.
589e7d8e88aSJeff LaBundy 	 */
590e7d8e88aSJeff LaBundy 	if (buf[1]) {
591e7d8e88aSJeff LaBundy 		dev_err(&client->dev, "Opening bootloader for A000 device\n");
5927b5bb55dSJeff LaBundy 		return iqs5xx_bl_open(client);
5937b5bb55dSJeff LaBundy 	}
5947b5bb55dSJeff LaBundy 
595509c0083SJeff LaBundy 	error = iqs5xx_read_burst(client, IQS5XX_EXP_FILE,
596509c0083SJeff LaBundy 				  iqs5xx->exp_file, sizeof(iqs5xx->exp_file));
597509c0083SJeff LaBundy 	if (error)
598509c0083SJeff LaBundy 		return error;
599509c0083SJeff LaBundy 
6007b5bb55dSJeff LaBundy 	error = iqs5xx_axis_init(client);
6017b5bb55dSJeff LaBundy 	if (error)
6027b5bb55dSJeff LaBundy 		return error;
6037b5bb55dSJeff LaBundy 
604e10ba0d3SJeff LaBundy 	error = iqs5xx_write_byte(client, IQS5XX_SYS_CTRL0, IQS5XX_ACK_RESET);
605e10ba0d3SJeff LaBundy 	if (error)
606e10ba0d3SJeff LaBundy 		return error;
607e10ba0d3SJeff LaBundy 
608050fac7fSJeff LaBundy 	error = iqs5xx_write_byte(client, IQS5XX_SYS_CFG0,
609050fac7fSJeff LaBundy 				  IQS5XX_SETUP_COMPLETE | IQS5XX_WDT |
610050fac7fSJeff LaBundy 				  IQS5XX_ALP_REATI | IQS5XX_REATI);
6117b5bb55dSJeff LaBundy 	if (error)
6127b5bb55dSJeff LaBundy 		return error;
6137b5bb55dSJeff LaBundy 
614050fac7fSJeff LaBundy 	error = iqs5xx_write_byte(client, IQS5XX_SYS_CFG1,
615050fac7fSJeff LaBundy 				  IQS5XX_TP_EVENT | IQS5XX_EVENT_MODE);
6167b5bb55dSJeff LaBundy 	if (error)
6177b5bb55dSJeff LaBundy 		return error;
6187b5bb55dSJeff LaBundy 
6197b5bb55dSJeff LaBundy 	error = iqs5xx_write_byte(client, IQS5XX_END_COMM, 0);
6207b5bb55dSJeff LaBundy 	if (error)
6217b5bb55dSJeff LaBundy 		return error;
6227b5bb55dSJeff LaBundy 
623509c0083SJeff LaBundy 	iqs5xx->dev_id_info = *dev_id_info;
6247b5bb55dSJeff LaBundy 
6257b5bb55dSJeff LaBundy 	/*
6268e6a8b0cSJeff LaBundy 	 * The following delay allows ATI to complete before the open and close
6278e6a8b0cSJeff LaBundy 	 * callbacks are free to elicit I2C communication. Any attempts to read
6288e6a8b0cSJeff LaBundy 	 * from or write to the device during this time may face extended clock
6298e6a8b0cSJeff LaBundy 	 * stretching and prompt the I2C controller to report an error.
6307b5bb55dSJeff LaBundy 	 */
6318e6a8b0cSJeff LaBundy 	msleep(250);
6327b5bb55dSJeff LaBundy 
6337b5bb55dSJeff LaBundy 	return 0;
6347b5bb55dSJeff LaBundy }
6357b5bb55dSJeff LaBundy 
iqs5xx_irq(int irq,void * data)6367b5bb55dSJeff LaBundy static irqreturn_t iqs5xx_irq(int irq, void *data)
6377b5bb55dSJeff LaBundy {
6387b5bb55dSJeff LaBundy 	struct iqs5xx_private *iqs5xx = data;
639e10ba0d3SJeff LaBundy 	struct iqs5xx_status status;
6407b5bb55dSJeff LaBundy 	struct i2c_client *client = iqs5xx->client;
6417b5bb55dSJeff LaBundy 	struct input_dev *input = iqs5xx->input;
6427b5bb55dSJeff LaBundy 	int error, i;
6437b5bb55dSJeff LaBundy 
6447b5bb55dSJeff LaBundy 	/*
6457b5bb55dSJeff LaBundy 	 * This check is purely a precaution, as the device does not assert the
6467b5bb55dSJeff LaBundy 	 * RDY output during bootloader mode. If the device operates outside of
6477b5bb55dSJeff LaBundy 	 * bootloader mode, the input device is guaranteed to be allocated.
6487b5bb55dSJeff LaBundy 	 */
649509c0083SJeff LaBundy 	if (!iqs5xx->dev_id_info.bl_status)
6507b5bb55dSJeff LaBundy 		return IRQ_NONE;
6517b5bb55dSJeff LaBundy 
652e10ba0d3SJeff LaBundy 	error = iqs5xx_read_burst(client, IQS5XX_SYS_INFO0,
653e10ba0d3SJeff LaBundy 				  &status, sizeof(status));
6547b5bb55dSJeff LaBundy 	if (error)
6557b5bb55dSJeff LaBundy 		return IRQ_NONE;
6567b5bb55dSJeff LaBundy 
657e10ba0d3SJeff LaBundy 	if (status.sys_info[0] & IQS5XX_SHOW_RESET) {
658e10ba0d3SJeff LaBundy 		dev_err(&client->dev, "Unexpected device reset\n");
659e10ba0d3SJeff LaBundy 
660e10ba0d3SJeff LaBundy 		error = iqs5xx_dev_init(client);
661e10ba0d3SJeff LaBundy 		if (error) {
662e10ba0d3SJeff LaBundy 			dev_err(&client->dev,
663e10ba0d3SJeff LaBundy 				"Failed to re-initialize device: %d\n", error);
664e10ba0d3SJeff LaBundy 			return IRQ_NONE;
665e10ba0d3SJeff LaBundy 		}
666e10ba0d3SJeff LaBundy 
667e10ba0d3SJeff LaBundy 		return IRQ_HANDLED;
668e10ba0d3SJeff LaBundy 	}
669e10ba0d3SJeff LaBundy 
670e10ba0d3SJeff LaBundy 	for (i = 0; i < ARRAY_SIZE(status.touch_data); i++) {
671e10ba0d3SJeff LaBundy 		struct iqs5xx_touch_data *touch_data = &status.touch_data[i];
672e10ba0d3SJeff LaBundy 		u16 pressure = be16_to_cpu(touch_data->strength);
6737b5bb55dSJeff LaBundy 
6747b5bb55dSJeff LaBundy 		input_mt_slot(input, i);
6757b5bb55dSJeff LaBundy 		if (input_mt_report_slot_state(input, MT_TOOL_FINGER,
6767b5bb55dSJeff LaBundy 					       pressure != 0)) {
67707fc21b4SJeff LaBundy 			touchscreen_report_pos(input, &iqs5xx->prop,
6784a76d861SJeff LaBundy 					       be16_to_cpu(touch_data->abs_x),
6794a76d861SJeff LaBundy 					       be16_to_cpu(touch_data->abs_y),
6804a76d861SJeff LaBundy 					       true);
6817b5bb55dSJeff LaBundy 			input_report_abs(input, ABS_MT_PRESSURE, pressure);
6827b5bb55dSJeff LaBundy 		}
6837b5bb55dSJeff LaBundy 	}
6847b5bb55dSJeff LaBundy 
6857b5bb55dSJeff LaBundy 	input_mt_sync_frame(input);
6867b5bb55dSJeff LaBundy 	input_sync(input);
6877b5bb55dSJeff LaBundy 
6887b5bb55dSJeff LaBundy 	error = iqs5xx_write_byte(client, IQS5XX_END_COMM, 0);
6897b5bb55dSJeff LaBundy 	if (error)
6907b5bb55dSJeff LaBundy 		return IRQ_NONE;
6917b5bb55dSJeff LaBundy 
6927b5bb55dSJeff LaBundy 	/*
6937b5bb55dSJeff LaBundy 	 * Once the communication window is closed, a small delay is added to
6947b5bb55dSJeff LaBundy 	 * ensure the device's RDY output has been deasserted by the time the
6957b5bb55dSJeff LaBundy 	 * interrupt handler returns.
6967b5bb55dSJeff LaBundy 	 */
6977b5bb55dSJeff LaBundy 	usleep_range(50, 100);
6987b5bb55dSJeff LaBundy 
6997b5bb55dSJeff LaBundy 	return IRQ_HANDLED;
7007b5bb55dSJeff LaBundy }
7017b5bb55dSJeff LaBundy 
iqs5xx_fw_file_parse(struct i2c_client * client,const char * fw_file,u8 * pmap)7027b5bb55dSJeff LaBundy static int iqs5xx_fw_file_parse(struct i2c_client *client,
7037b5bb55dSJeff LaBundy 				const char *fw_file, u8 *pmap)
7047b5bb55dSJeff LaBundy {
7057b5bb55dSJeff LaBundy 	const struct firmware *fw;
7067b5bb55dSJeff LaBundy 	struct iqs5xx_ihex_rec *rec;
7077b5bb55dSJeff LaBundy 	size_t pos = 0;
7087b5bb55dSJeff LaBundy 	int error, i;
7097b5bb55dSJeff LaBundy 	u16 rec_num = 1;
7107b5bb55dSJeff LaBundy 	u16 rec_addr;
7117b5bb55dSJeff LaBundy 	u8 rec_len, rec_type, rec_chksm, chksm;
7127b5bb55dSJeff LaBundy 	u8 rec_hdr[IQS5XX_REC_HDR_LEN];
7137b5bb55dSJeff LaBundy 	u8 rec_data[IQS5XX_REC_LEN_MAX];
7147b5bb55dSJeff LaBundy 
7157b5bb55dSJeff LaBundy 	/*
7167b5bb55dSJeff LaBundy 	 * Firmware exported from the vendor's configuration tool deviates from
7177b5bb55dSJeff LaBundy 	 * standard ihex as follows: (1) the checksum for records corresponding
7187b5bb55dSJeff LaBundy 	 * to user-exported settings is not recalculated, and (2) an address of
7197b5bb55dSJeff LaBundy 	 * 0xFFFF is used for the EOF record.
7207b5bb55dSJeff LaBundy 	 *
7217b5bb55dSJeff LaBundy 	 * Because the ihex2fw tool tolerates neither (1) nor (2), the slightly
7227b5bb55dSJeff LaBundy 	 * nonstandard ihex firmware is parsed directly by the driver.
7237b5bb55dSJeff LaBundy 	 */
7247b5bb55dSJeff LaBundy 	error = request_firmware(&fw, fw_file, &client->dev);
7257b5bb55dSJeff LaBundy 	if (error) {
7267b5bb55dSJeff LaBundy 		dev_err(&client->dev, "Failed to request firmware %s: %d\n",
7277b5bb55dSJeff LaBundy 			fw_file, error);
7287b5bb55dSJeff LaBundy 		return error;
7297b5bb55dSJeff LaBundy 	}
7307b5bb55dSJeff LaBundy 
7317b5bb55dSJeff LaBundy 	do {
7327b5bb55dSJeff LaBundy 		if (pos + sizeof(*rec) > fw->size) {
7337b5bb55dSJeff LaBundy 			dev_err(&client->dev, "Insufficient firmware size\n");
7347b5bb55dSJeff LaBundy 			error = -EINVAL;
7357b5bb55dSJeff LaBundy 			break;
7367b5bb55dSJeff LaBundy 		}
7377b5bb55dSJeff LaBundy 		rec = (struct iqs5xx_ihex_rec *)(fw->data + pos);
7387b5bb55dSJeff LaBundy 		pos += sizeof(*rec);
7397b5bb55dSJeff LaBundy 
7407b5bb55dSJeff LaBundy 		if (rec->start != ':') {
7417b5bb55dSJeff LaBundy 			dev_err(&client->dev, "Invalid start at record %u\n",
7427b5bb55dSJeff LaBundy 				rec_num);
7437b5bb55dSJeff LaBundy 			error = -EINVAL;
7447b5bb55dSJeff LaBundy 			break;
7457b5bb55dSJeff LaBundy 		}
7467b5bb55dSJeff LaBundy 
7477b5bb55dSJeff LaBundy 		error = hex2bin(rec_hdr, rec->len, sizeof(rec_hdr));
7487b5bb55dSJeff LaBundy 		if (error) {
7497b5bb55dSJeff LaBundy 			dev_err(&client->dev, "Invalid header at record %u\n",
7507b5bb55dSJeff LaBundy 				rec_num);
7517b5bb55dSJeff LaBundy 			break;
7527b5bb55dSJeff LaBundy 		}
7537b5bb55dSJeff LaBundy 
7547b5bb55dSJeff LaBundy 		rec_len = *rec_hdr;
7557b5bb55dSJeff LaBundy 		rec_addr = get_unaligned_be16(rec_hdr + sizeof(rec_len));
7567b5bb55dSJeff LaBundy 		rec_type = *(rec_hdr + sizeof(rec_len) + sizeof(rec_addr));
7577b5bb55dSJeff LaBundy 
7587b5bb55dSJeff LaBundy 		if (pos + rec_len * 2 > fw->size) {
7597b5bb55dSJeff LaBundy 			dev_err(&client->dev, "Insufficient firmware size\n");
7607b5bb55dSJeff LaBundy 			error = -EINVAL;
7617b5bb55dSJeff LaBundy 			break;
7627b5bb55dSJeff LaBundy 		}
7637b5bb55dSJeff LaBundy 		pos += (rec_len * 2);
7647b5bb55dSJeff LaBundy 
7657b5bb55dSJeff LaBundy 		error = hex2bin(rec_data, rec->data, rec_len);
7667b5bb55dSJeff LaBundy 		if (error) {
7677b5bb55dSJeff LaBundy 			dev_err(&client->dev, "Invalid data at record %u\n",
7687b5bb55dSJeff LaBundy 				rec_num);
7697b5bb55dSJeff LaBundy 			break;
7707b5bb55dSJeff LaBundy 		}
7717b5bb55dSJeff LaBundy 
7727b5bb55dSJeff LaBundy 		error = hex2bin(&rec_chksm,
7737b5bb55dSJeff LaBundy 				rec->data + rec_len * 2, sizeof(rec_chksm));
7747b5bb55dSJeff LaBundy 		if (error) {
7757b5bb55dSJeff LaBundy 			dev_err(&client->dev, "Invalid checksum at record %u\n",
7767b5bb55dSJeff LaBundy 				rec_num);
7777b5bb55dSJeff LaBundy 			break;
7787b5bb55dSJeff LaBundy 		}
7797b5bb55dSJeff LaBundy 
7807b5bb55dSJeff LaBundy 		chksm = 0;
7817b5bb55dSJeff LaBundy 		for (i = 0; i < sizeof(rec_hdr); i++)
7827b5bb55dSJeff LaBundy 			chksm += rec_hdr[i];
7837b5bb55dSJeff LaBundy 		for (i = 0; i < rec_len; i++)
7847b5bb55dSJeff LaBundy 			chksm += rec_data[i];
7857b5bb55dSJeff LaBundy 		chksm = ~chksm + 1;
7867b5bb55dSJeff LaBundy 
7877b5bb55dSJeff LaBundy 		if (chksm != rec_chksm && rec_addr < IQS5XX_CSTM) {
7887b5bb55dSJeff LaBundy 			dev_err(&client->dev,
7897b5bb55dSJeff LaBundy 				"Incorrect checksum at record %u\n",
7907b5bb55dSJeff LaBundy 				rec_num);
7917b5bb55dSJeff LaBundy 			error = -EINVAL;
7927b5bb55dSJeff LaBundy 			break;
7937b5bb55dSJeff LaBundy 		}
7947b5bb55dSJeff LaBundy 
7957b5bb55dSJeff LaBundy 		switch (rec_type) {
7967b5bb55dSJeff LaBundy 		case IQS5XX_REC_TYPE_DATA:
7977b5bb55dSJeff LaBundy 			if (rec_addr < IQS5XX_CHKSM ||
7987b5bb55dSJeff LaBundy 			    rec_addr > IQS5XX_PMAP_END) {
7997b5bb55dSJeff LaBundy 				dev_err(&client->dev,
8007b5bb55dSJeff LaBundy 					"Invalid address at record %u\n",
8017b5bb55dSJeff LaBundy 					rec_num);
8027b5bb55dSJeff LaBundy 				error = -EINVAL;
8037b5bb55dSJeff LaBundy 			} else {
8047b5bb55dSJeff LaBundy 				memcpy(pmap + rec_addr - IQS5XX_CHKSM,
8057b5bb55dSJeff LaBundy 				       rec_data, rec_len);
8067b5bb55dSJeff LaBundy 			}
8077b5bb55dSJeff LaBundy 			break;
8087b5bb55dSJeff LaBundy 		case IQS5XX_REC_TYPE_EOF:
8097b5bb55dSJeff LaBundy 			break;
8107b5bb55dSJeff LaBundy 		default:
8117b5bb55dSJeff LaBundy 			dev_err(&client->dev, "Invalid type at record %u\n",
8127b5bb55dSJeff LaBundy 				rec_num);
8137b5bb55dSJeff LaBundy 			error = -EINVAL;
8147b5bb55dSJeff LaBundy 		}
8157b5bb55dSJeff LaBundy 
8167b5bb55dSJeff LaBundy 		if (error)
8177b5bb55dSJeff LaBundy 			break;
8187b5bb55dSJeff LaBundy 
8197b5bb55dSJeff LaBundy 		rec_num++;
8207b5bb55dSJeff LaBundy 		while (pos < fw->size) {
8217b5bb55dSJeff LaBundy 			if (*(fw->data + pos) == ':')
8227b5bb55dSJeff LaBundy 				break;
8237b5bb55dSJeff LaBundy 			pos++;
8247b5bb55dSJeff LaBundy 		}
8257b5bb55dSJeff LaBundy 	} while (rec_type != IQS5XX_REC_TYPE_EOF);
8267b5bb55dSJeff LaBundy 
8277b5bb55dSJeff LaBundy 	release_firmware(fw);
8287b5bb55dSJeff LaBundy 
8297b5bb55dSJeff LaBundy 	return error;
8307b5bb55dSJeff LaBundy }
8317b5bb55dSJeff LaBundy 
iqs5xx_fw_file_write(struct i2c_client * client,const char * fw_file)8327b5bb55dSJeff LaBundy static int iqs5xx_fw_file_write(struct i2c_client *client, const char *fw_file)
8337b5bb55dSJeff LaBundy {
8347b5bb55dSJeff LaBundy 	struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client);
83595a6d961SJeff LaBundy 	int error, error_init = 0;
8367b5bb55dSJeff LaBundy 	u8 *pmap;
8377b5bb55dSJeff LaBundy 
8387b5bb55dSJeff LaBundy 	pmap = kzalloc(IQS5XX_PMAP_LEN, GFP_KERNEL);
8397b5bb55dSJeff LaBundy 	if (!pmap)
8407b5bb55dSJeff LaBundy 		return -ENOMEM;
8417b5bb55dSJeff LaBundy 
8427b5bb55dSJeff LaBundy 	error = iqs5xx_fw_file_parse(client, fw_file, pmap);
8437b5bb55dSJeff LaBundy 	if (error)
8447b5bb55dSJeff LaBundy 		goto err_kfree;
8457b5bb55dSJeff LaBundy 
8467b5bb55dSJeff LaBundy 	mutex_lock(&iqs5xx->lock);
8477b5bb55dSJeff LaBundy 
8487b5bb55dSJeff LaBundy 	/*
8497b5bb55dSJeff LaBundy 	 * Disable the interrupt line in case the first attempt(s) to enter the
8507b5bb55dSJeff LaBundy 	 * bootloader don't happen quickly enough, in which case the device may
8517b5bb55dSJeff LaBundy 	 * assert the RDY output until the next attempt.
8527b5bb55dSJeff LaBundy 	 */
8537b5bb55dSJeff LaBundy 	disable_irq(client->irq);
8547b5bb55dSJeff LaBundy 
855509c0083SJeff LaBundy 	iqs5xx->dev_id_info.bl_status = 0;
8567b5bb55dSJeff LaBundy 
8577b5bb55dSJeff LaBundy 	error = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_VER, 0);
8587b5bb55dSJeff LaBundy 	if (error) {
8597b5bb55dSJeff LaBundy 		error = iqs5xx_bl_open(client);
8607b5bb55dSJeff LaBundy 		if (error)
8617b5bb55dSJeff LaBundy 			goto err_reset;
8627b5bb55dSJeff LaBundy 	}
8637b5bb55dSJeff LaBundy 
8647b5bb55dSJeff LaBundy 	error = iqs5xx_bl_write(client, IQS5XX_CHKSM, pmap, IQS5XX_PMAP_LEN);
8657b5bb55dSJeff LaBundy 	if (error)
8667b5bb55dSJeff LaBundy 		goto err_reset;
8677b5bb55dSJeff LaBundy 
8687b5bb55dSJeff LaBundy 	error = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_CRC, 0);
8697b5bb55dSJeff LaBundy 	if (error)
8707b5bb55dSJeff LaBundy 		goto err_reset;
8717b5bb55dSJeff LaBundy 
8727b5bb55dSJeff LaBundy 	error = iqs5xx_bl_verify(client, IQS5XX_CSTM,
8737b5bb55dSJeff LaBundy 				 pmap + IQS5XX_CHKSM_LEN + IQS5XX_APP_LEN,
8747b5bb55dSJeff LaBundy 				 IQS5XX_CSTM_LEN);
8757b5bb55dSJeff LaBundy 
8767b5bb55dSJeff LaBundy err_reset:
8777b5bb55dSJeff LaBundy 	iqs5xx_reset(client);
87895a6d961SJeff LaBundy 	usleep_range(15000, 15100);
8797b5bb55dSJeff LaBundy 
88095a6d961SJeff LaBundy 	error_init = iqs5xx_dev_init(client);
88195a6d961SJeff LaBundy 	if (!iqs5xx->dev_id_info.bl_status)
88295a6d961SJeff LaBundy 		error_init = error_init ? : -EINVAL;
8837b5bb55dSJeff LaBundy 
8847b5bb55dSJeff LaBundy 	enable_irq(client->irq);
8857b5bb55dSJeff LaBundy 
8867b5bb55dSJeff LaBundy 	mutex_unlock(&iqs5xx->lock);
8877b5bb55dSJeff LaBundy 
8887b5bb55dSJeff LaBundy err_kfree:
8897b5bb55dSJeff LaBundy 	kfree(pmap);
8907b5bb55dSJeff LaBundy 
89195a6d961SJeff LaBundy 	return error ? : error_init;
8927b5bb55dSJeff LaBundy }
8937b5bb55dSJeff LaBundy 
fw_file_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)894785a19d9SJeff LaBundy static ssize_t fw_file_store(struct device *dev,
895785a19d9SJeff LaBundy 			     struct device_attribute *attr, const char *buf,
896785a19d9SJeff LaBundy 			     size_t count)
8977b5bb55dSJeff LaBundy {
8987b5bb55dSJeff LaBundy 	struct iqs5xx_private *iqs5xx = dev_get_drvdata(dev);
8997b5bb55dSJeff LaBundy 	struct i2c_client *client = iqs5xx->client;
9007b5bb55dSJeff LaBundy 	size_t len = count;
9017b5bb55dSJeff LaBundy 	bool input_reg = !iqs5xx->input;
9027b5bb55dSJeff LaBundy 	char fw_file[IQS5XX_FW_FILE_LEN + 1];
9037b5bb55dSJeff LaBundy 	int error;
9047b5bb55dSJeff LaBundy 
9057b5bb55dSJeff LaBundy 	if (!len)
9067b5bb55dSJeff LaBundy 		return -EINVAL;
9077b5bb55dSJeff LaBundy 
9087b5bb55dSJeff LaBundy 	if (buf[len - 1] == '\n')
9097b5bb55dSJeff LaBundy 		len--;
9107b5bb55dSJeff LaBundy 
9117b5bb55dSJeff LaBundy 	if (len > IQS5XX_FW_FILE_LEN)
9127b5bb55dSJeff LaBundy 		return -ENAMETOOLONG;
9137b5bb55dSJeff LaBundy 
9147b5bb55dSJeff LaBundy 	memcpy(fw_file, buf, len);
9157b5bb55dSJeff LaBundy 	fw_file[len] = '\0';
9167b5bb55dSJeff LaBundy 
9177b5bb55dSJeff LaBundy 	error = iqs5xx_fw_file_write(client, fw_file);
9187b5bb55dSJeff LaBundy 	if (error)
9197b5bb55dSJeff LaBundy 		return error;
9207b5bb55dSJeff LaBundy 
9217b5bb55dSJeff LaBundy 	/*
9227b5bb55dSJeff LaBundy 	 * If the input device was not allocated already, it is guaranteed to
9237b5bb55dSJeff LaBundy 	 * be allocated by this point and can finally be registered.
9247b5bb55dSJeff LaBundy 	 */
9257b5bb55dSJeff LaBundy 	if (input_reg) {
9267b5bb55dSJeff LaBundy 		error = input_register_device(iqs5xx->input);
9277b5bb55dSJeff LaBundy 		if (error) {
9287b5bb55dSJeff LaBundy 			dev_err(&client->dev,
9297b5bb55dSJeff LaBundy 				"Failed to register device: %d\n",
9307b5bb55dSJeff LaBundy 				error);
9317b5bb55dSJeff LaBundy 			return error;
9327b5bb55dSJeff LaBundy 		}
9337b5bb55dSJeff LaBundy 	}
9347b5bb55dSJeff LaBundy 
9357b5bb55dSJeff LaBundy 	return count;
9367b5bb55dSJeff LaBundy }
9377b5bb55dSJeff LaBundy 
fw_info_show(struct device * dev,struct device_attribute * attr,char * buf)938509c0083SJeff LaBundy static ssize_t fw_info_show(struct device *dev,
939509c0083SJeff LaBundy 			    struct device_attribute *attr, char *buf)
940509c0083SJeff LaBundy {
941509c0083SJeff LaBundy 	struct iqs5xx_private *iqs5xx = dev_get_drvdata(dev);
942509c0083SJeff LaBundy 
943509c0083SJeff LaBundy 	if (!iqs5xx->dev_id_info.bl_status)
944509c0083SJeff LaBundy 		return -ENODATA;
945509c0083SJeff LaBundy 
946509c0083SJeff LaBundy 	return scnprintf(buf, PAGE_SIZE, "%u.%u.%u.%u:%u.%u\n",
947509c0083SJeff LaBundy 			 be16_to_cpu(iqs5xx->dev_id_info.prod_num),
948509c0083SJeff LaBundy 			 be16_to_cpu(iqs5xx->dev_id_info.proj_num),
949509c0083SJeff LaBundy 			 iqs5xx->dev_id_info.major_ver,
950509c0083SJeff LaBundy 			 iqs5xx->dev_id_info.minor_ver,
951509c0083SJeff LaBundy 			 iqs5xx->exp_file[0], iqs5xx->exp_file[1]);
952509c0083SJeff LaBundy }
953509c0083SJeff LaBundy 
9547b5bb55dSJeff LaBundy static DEVICE_ATTR_WO(fw_file);
955509c0083SJeff LaBundy static DEVICE_ATTR_RO(fw_info);
9567b5bb55dSJeff LaBundy 
9577b5bb55dSJeff LaBundy static struct attribute *iqs5xx_attrs[] = {
9587b5bb55dSJeff LaBundy 	&dev_attr_fw_file.attr,
959509c0083SJeff LaBundy 	&dev_attr_fw_info.attr,
9607b5bb55dSJeff LaBundy 	NULL,
9617b5bb55dSJeff LaBundy };
9627b5bb55dSJeff LaBundy 
iqs5xx_attr_is_visible(struct kobject * kobj,struct attribute * attr,int i)9639d41359cSJeff LaBundy static umode_t iqs5xx_attr_is_visible(struct kobject *kobj,
9649d41359cSJeff LaBundy 				      struct attribute *attr, int i)
9659d41359cSJeff LaBundy {
9669d41359cSJeff LaBundy 	struct device *dev = kobj_to_dev(kobj);
9679d41359cSJeff LaBundy 	struct iqs5xx_private *iqs5xx = dev_get_drvdata(dev);
9689d41359cSJeff LaBundy 
9699d41359cSJeff LaBundy 	if (attr == &dev_attr_fw_file.attr &&
9709d41359cSJeff LaBundy 	    (iqs5xx->dev_id_info.bl_status == IQS5XX_BL_STATUS_NONE ||
9719d41359cSJeff LaBundy 	    !iqs5xx->reset_gpio))
9729d41359cSJeff LaBundy 		return 0;
9739d41359cSJeff LaBundy 
9749d41359cSJeff LaBundy 	return attr->mode;
9759d41359cSJeff LaBundy }
9769d41359cSJeff LaBundy 
9777b5bb55dSJeff LaBundy static const struct attribute_group iqs5xx_attr_group = {
9789d41359cSJeff LaBundy 	.is_visible = iqs5xx_attr_is_visible,
9797b5bb55dSJeff LaBundy 	.attrs = iqs5xx_attrs,
9807b5bb55dSJeff LaBundy };
9817b5bb55dSJeff LaBundy 
iqs5xx_suspend(struct device * dev)9825662a37dSJonathan Cameron static int iqs5xx_suspend(struct device *dev)
9837b5bb55dSJeff LaBundy {
9847b5bb55dSJeff LaBundy 	struct iqs5xx_private *iqs5xx = dev_get_drvdata(dev);
9857b5bb55dSJeff LaBundy 	struct input_dev *input = iqs5xx->input;
9867b5bb55dSJeff LaBundy 	int error = 0;
9877b5bb55dSJeff LaBundy 
988ce996aa3SJeff LaBundy 	if (!input || device_may_wakeup(dev))
9897b5bb55dSJeff LaBundy 		return error;
9907b5bb55dSJeff LaBundy 
9917b5bb55dSJeff LaBundy 	mutex_lock(&input->mutex);
9927b5bb55dSJeff LaBundy 
993d69f0a43SAndrzej Pietrasiewicz 	if (input_device_enabled(input))
9947b5bb55dSJeff LaBundy 		error = iqs5xx_set_state(iqs5xx->client, IQS5XX_SUSPEND);
9957b5bb55dSJeff LaBundy 
9967b5bb55dSJeff LaBundy 	mutex_unlock(&input->mutex);
9977b5bb55dSJeff LaBundy 
9987b5bb55dSJeff LaBundy 	return error;
9997b5bb55dSJeff LaBundy }
10007b5bb55dSJeff LaBundy 
iqs5xx_resume(struct device * dev)10015662a37dSJonathan Cameron static int iqs5xx_resume(struct device *dev)
10027b5bb55dSJeff LaBundy {
10037b5bb55dSJeff LaBundy 	struct iqs5xx_private *iqs5xx = dev_get_drvdata(dev);
10047b5bb55dSJeff LaBundy 	struct input_dev *input = iqs5xx->input;
10057b5bb55dSJeff LaBundy 	int error = 0;
10067b5bb55dSJeff LaBundy 
1007ce996aa3SJeff LaBundy 	if (!input || device_may_wakeup(dev))
10087b5bb55dSJeff LaBundy 		return error;
10097b5bb55dSJeff LaBundy 
10107b5bb55dSJeff LaBundy 	mutex_lock(&input->mutex);
10117b5bb55dSJeff LaBundy 
1012d69f0a43SAndrzej Pietrasiewicz 	if (input_device_enabled(input))
10137b5bb55dSJeff LaBundy 		error = iqs5xx_set_state(iqs5xx->client, IQS5XX_RESUME);
10147b5bb55dSJeff LaBundy 
10157b5bb55dSJeff LaBundy 	mutex_unlock(&input->mutex);
10167b5bb55dSJeff LaBundy 
10177b5bb55dSJeff LaBundy 	return error;
10187b5bb55dSJeff LaBundy }
10197b5bb55dSJeff LaBundy 
10205662a37dSJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(iqs5xx_pm, iqs5xx_suspend, iqs5xx_resume);
10217b5bb55dSJeff LaBundy 
iqs5xx_probe(struct i2c_client * client)1022dd271dd9SUwe Kleine-König static int iqs5xx_probe(struct i2c_client *client)
10237b5bb55dSJeff LaBundy {
10247b5bb55dSJeff LaBundy 	struct iqs5xx_private *iqs5xx;
10257b5bb55dSJeff LaBundy 	int error;
10267b5bb55dSJeff LaBundy 
10277b5bb55dSJeff LaBundy 	iqs5xx = devm_kzalloc(&client->dev, sizeof(*iqs5xx), GFP_KERNEL);
10287b5bb55dSJeff LaBundy 	if (!iqs5xx)
10297b5bb55dSJeff LaBundy 		return -ENOMEM;
10307b5bb55dSJeff LaBundy 
10317b5bb55dSJeff LaBundy 	i2c_set_clientdata(client, iqs5xx);
10327b5bb55dSJeff LaBundy 	iqs5xx->client = client;
10337b5bb55dSJeff LaBundy 
10349d41359cSJeff LaBundy 	iqs5xx->reset_gpio = devm_gpiod_get_optional(&client->dev,
10357b5bb55dSJeff LaBundy 						     "reset", GPIOD_OUT_LOW);
10367b5bb55dSJeff LaBundy 	if (IS_ERR(iqs5xx->reset_gpio)) {
10377b5bb55dSJeff LaBundy 		error = PTR_ERR(iqs5xx->reset_gpio);
10387b5bb55dSJeff LaBundy 		dev_err(&client->dev, "Failed to request GPIO: %d\n", error);
10397b5bb55dSJeff LaBundy 		return error;
10407b5bb55dSJeff LaBundy 	}
10417b5bb55dSJeff LaBundy 
10427b5bb55dSJeff LaBundy 	mutex_init(&iqs5xx->lock);
10437b5bb55dSJeff LaBundy 
10447b5bb55dSJeff LaBundy 	error = iqs5xx_dev_init(client);
10457b5bb55dSJeff LaBundy 	if (error)
10467b5bb55dSJeff LaBundy 		return error;
10477b5bb55dSJeff LaBundy 
10487b5bb55dSJeff LaBundy 	error = devm_request_threaded_irq(&client->dev, client->irq,
10497b5bb55dSJeff LaBundy 					  NULL, iqs5xx_irq, IRQF_ONESHOT,
10507b5bb55dSJeff LaBundy 					  client->name, iqs5xx);
10517b5bb55dSJeff LaBundy 	if (error) {
10527b5bb55dSJeff LaBundy 		dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
10537b5bb55dSJeff LaBundy 		return error;
10547b5bb55dSJeff LaBundy 	}
10557b5bb55dSJeff LaBundy 
10567b5bb55dSJeff LaBundy 	error = devm_device_add_group(&client->dev, &iqs5xx_attr_group);
10577b5bb55dSJeff LaBundy 	if (error) {
10587b5bb55dSJeff LaBundy 		dev_err(&client->dev, "Failed to add attributes: %d\n", error);
10597b5bb55dSJeff LaBundy 		return error;
10607b5bb55dSJeff LaBundy 	}
10617b5bb55dSJeff LaBundy 
10627b5bb55dSJeff LaBundy 	if (iqs5xx->input) {
10637b5bb55dSJeff LaBundy 		error = input_register_device(iqs5xx->input);
10647b5bb55dSJeff LaBundy 		if (error)
10657b5bb55dSJeff LaBundy 			dev_err(&client->dev,
10667b5bb55dSJeff LaBundy 				"Failed to register device: %d\n",
10677b5bb55dSJeff LaBundy 				error);
10687b5bb55dSJeff LaBundy 	}
10697b5bb55dSJeff LaBundy 
10707b5bb55dSJeff LaBundy 	return error;
10717b5bb55dSJeff LaBundy }
10727b5bb55dSJeff LaBundy 
10737b5bb55dSJeff LaBundy static const struct i2c_device_id iqs5xx_id[] = {
10747b5bb55dSJeff LaBundy 	{ "iqs550", 0 },
10757b5bb55dSJeff LaBundy 	{ "iqs572", 1 },
10767b5bb55dSJeff LaBundy 	{ "iqs525", 2 },
10777b5bb55dSJeff LaBundy 	{ }
10787b5bb55dSJeff LaBundy };
10797b5bb55dSJeff LaBundy MODULE_DEVICE_TABLE(i2c, iqs5xx_id);
10807b5bb55dSJeff LaBundy 
10817b5bb55dSJeff LaBundy static const struct of_device_id iqs5xx_of_match[] = {
10827b5bb55dSJeff LaBundy 	{ .compatible = "azoteq,iqs550" },
10837b5bb55dSJeff LaBundy 	{ .compatible = "azoteq,iqs572" },
10847b5bb55dSJeff LaBundy 	{ .compatible = "azoteq,iqs525" },
10857b5bb55dSJeff LaBundy 	{ }
10867b5bb55dSJeff LaBundy };
10877b5bb55dSJeff LaBundy MODULE_DEVICE_TABLE(of, iqs5xx_of_match);
10887b5bb55dSJeff LaBundy 
10897b5bb55dSJeff LaBundy static struct i2c_driver iqs5xx_i2c_driver = {
10907b5bb55dSJeff LaBundy 	.driver = {
10917b5bb55dSJeff LaBundy 		.name		= "iqs5xx",
10927b5bb55dSJeff LaBundy 		.of_match_table	= iqs5xx_of_match,
10935662a37dSJonathan Cameron 		.pm		= pm_sleep_ptr(&iqs5xx_pm),
10947b5bb55dSJeff LaBundy 	},
10957b5bb55dSJeff LaBundy 	.id_table	= iqs5xx_id,
1096d8bde56dSUwe Kleine-König 	.probe		= iqs5xx_probe,
10977b5bb55dSJeff LaBundy };
10987b5bb55dSJeff LaBundy module_i2c_driver(iqs5xx_i2c_driver);
10997b5bb55dSJeff LaBundy 
11007b5bb55dSJeff LaBundy MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
11017b5bb55dSJeff LaBundy MODULE_DESCRIPTION("Azoteq IQS550/572/525 Trackpad/Touchscreen Controller");
11027b5bb55dSJeff LaBundy MODULE_LICENSE("GPL");
1103