17268a4f8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2842ff286SAnthony Kim /*
3842ff286SAnthony Kim  * Copyright (C) 2012-2017 Hideep, Inc.
4842ff286SAnthony Kim  */
5842ff286SAnthony Kim 
6842ff286SAnthony Kim #include <linux/module.h>
7842ff286SAnthony Kim #include <linux/of.h>
8842ff286SAnthony Kim #include <linux/firmware.h>
9842ff286SAnthony Kim #include <linux/delay.h>
108b7e9d9eSAnthony Kim #include <linux/gpio/consumer.h>
11842ff286SAnthony Kim #include <linux/i2c.h>
12842ff286SAnthony Kim #include <linux/acpi.h>
13842ff286SAnthony Kim #include <linux/interrupt.h>
14842ff286SAnthony Kim #include <linux/regmap.h>
15842ff286SAnthony Kim #include <linux/sysfs.h>
16842ff286SAnthony Kim #include <linux/input.h>
17842ff286SAnthony Kim #include <linux/input/mt.h>
18842ff286SAnthony Kim #include <linux/input/touchscreen.h>
19842ff286SAnthony Kim #include <linux/regulator/consumer.h>
20842ff286SAnthony Kim #include <asm/unaligned.h>
21842ff286SAnthony Kim 
22842ff286SAnthony Kim #define HIDEEP_TS_NAME			"HiDeep Touchscreen"
23842ff286SAnthony Kim #define HIDEEP_I2C_NAME			"hideep_ts"
24842ff286SAnthony Kim 
25842ff286SAnthony Kim #define HIDEEP_MT_MAX			10
26842ff286SAnthony Kim #define HIDEEP_KEY_MAX			3
27842ff286SAnthony Kim 
28842ff286SAnthony Kim /* count(2) + touch data(100) + key data(6) */
29842ff286SAnthony Kim #define HIDEEP_MAX_EVENT		108UL
30842ff286SAnthony Kim 
31842ff286SAnthony Kim #define HIDEEP_TOUCH_EVENT_INDEX	2
32842ff286SAnthony Kim #define HIDEEP_KEY_EVENT_INDEX		102
33842ff286SAnthony Kim 
34842ff286SAnthony Kim /* Touch & key event */
35842ff286SAnthony Kim #define HIDEEP_EVENT_ADDR		0x240
36842ff286SAnthony Kim 
37842ff286SAnthony Kim /* command list */
38842ff286SAnthony Kim #define HIDEEP_RESET_CMD		0x9800
39842ff286SAnthony Kim 
40842ff286SAnthony Kim /* event bit */
41842ff286SAnthony Kim #define HIDEEP_MT_RELEASED		BIT(4)
42842ff286SAnthony Kim #define HIDEEP_KEY_PRESSED		BIT(7)
43842ff286SAnthony Kim #define HIDEEP_KEY_FIRST_PRESSED	BIT(8)
44842ff286SAnthony Kim #define HIDEEP_KEY_PRESSED_MASK		(HIDEEP_KEY_PRESSED | \
45842ff286SAnthony Kim 					 HIDEEP_KEY_FIRST_PRESSED)
46842ff286SAnthony Kim 
47842ff286SAnthony Kim #define HIDEEP_KEY_IDX_MASK		0x0f
48842ff286SAnthony Kim 
49842ff286SAnthony Kim /* For NVM */
50842ff286SAnthony Kim #define HIDEEP_YRAM_BASE		0x40000000
51842ff286SAnthony Kim #define HIDEEP_PERIPHERAL_BASE		0x50000000
52842ff286SAnthony Kim #define HIDEEP_ESI_BASE			(HIDEEP_PERIPHERAL_BASE + 0x00000000)
53842ff286SAnthony Kim #define HIDEEP_FLASH_BASE		(HIDEEP_PERIPHERAL_BASE + 0x01000000)
54842ff286SAnthony Kim #define HIDEEP_SYSCON_BASE		(HIDEEP_PERIPHERAL_BASE + 0x02000000)
55842ff286SAnthony Kim 
56842ff286SAnthony Kim #define HIDEEP_SYSCON_MOD_CON		(HIDEEP_SYSCON_BASE + 0x0000)
57842ff286SAnthony Kim #define HIDEEP_SYSCON_SPC_CON		(HIDEEP_SYSCON_BASE + 0x0004)
58842ff286SAnthony Kim #define HIDEEP_SYSCON_CLK_CON		(HIDEEP_SYSCON_BASE + 0x0008)
59842ff286SAnthony Kim #define HIDEEP_SYSCON_CLK_ENA		(HIDEEP_SYSCON_BASE + 0x000C)
60842ff286SAnthony Kim #define HIDEEP_SYSCON_RST_CON		(HIDEEP_SYSCON_BASE + 0x0010)
61842ff286SAnthony Kim #define HIDEEP_SYSCON_WDT_CON		(HIDEEP_SYSCON_BASE + 0x0014)
62842ff286SAnthony Kim #define HIDEEP_SYSCON_WDT_CNT		(HIDEEP_SYSCON_BASE + 0x0018)
63842ff286SAnthony Kim #define HIDEEP_SYSCON_PWR_CON		(HIDEEP_SYSCON_BASE + 0x0020)
64842ff286SAnthony Kim #define HIDEEP_SYSCON_PGM_ID		(HIDEEP_SYSCON_BASE + 0x00F4)
65842ff286SAnthony Kim 
66842ff286SAnthony Kim #define HIDEEP_FLASH_CON		(HIDEEP_FLASH_BASE + 0x0000)
67842ff286SAnthony Kim #define HIDEEP_FLASH_STA		(HIDEEP_FLASH_BASE + 0x0004)
68842ff286SAnthony Kim #define HIDEEP_FLASH_CFG		(HIDEEP_FLASH_BASE + 0x0008)
69842ff286SAnthony Kim #define HIDEEP_FLASH_TIM		(HIDEEP_FLASH_BASE + 0x000C)
70842ff286SAnthony Kim #define HIDEEP_FLASH_CACHE_CFG		(HIDEEP_FLASH_BASE + 0x0010)
71842ff286SAnthony Kim #define HIDEEP_FLASH_PIO_SIG		(HIDEEP_FLASH_BASE + 0x400000)
72842ff286SAnthony Kim 
73842ff286SAnthony Kim #define HIDEEP_ESI_TX_INVALID		(HIDEEP_ESI_BASE + 0x0008)
74842ff286SAnthony Kim 
75842ff286SAnthony Kim #define HIDEEP_PERASE			0x00040000
76842ff286SAnthony Kim #define HIDEEP_WRONLY			0x00100000
77842ff286SAnthony Kim 
78842ff286SAnthony Kim #define HIDEEP_NVM_MASK_OFS		0x0000000C
79842ff286SAnthony Kim #define HIDEEP_NVM_DEFAULT_PAGE		0
80842ff286SAnthony Kim #define HIDEEP_NVM_SFR_WPAGE		1
81842ff286SAnthony Kim #define HIDEEP_NVM_SFR_RPAGE		2
82842ff286SAnthony Kim 
83842ff286SAnthony Kim #define HIDEEP_PIO_SIG			0x00400000
84842ff286SAnthony Kim #define HIDEEP_PROT_MODE		0x03400000
85842ff286SAnthony Kim 
86842ff286SAnthony Kim #define HIDEEP_NVM_PAGE_SIZE		128
87842ff286SAnthony Kim 
88842ff286SAnthony Kim #define HIDEEP_DWZ_INFO			0x000002C0
89842ff286SAnthony Kim 
90842ff286SAnthony Kim struct hideep_event {
91842ff286SAnthony Kim 	__le16 x;
92842ff286SAnthony Kim 	__le16 y;
93842ff286SAnthony Kim 	__le16 z;
94842ff286SAnthony Kim 	u8 w;
95842ff286SAnthony Kim 	u8 flag;
96842ff286SAnthony Kim 	u8 type;
97842ff286SAnthony Kim 	u8 index;
98842ff286SAnthony Kim };
99842ff286SAnthony Kim 
100842ff286SAnthony Kim struct dwz_info {
101842ff286SAnthony Kim 	__be32 code_start;
102842ff286SAnthony Kim 	u8 code_crc[12];
103842ff286SAnthony Kim 
104842ff286SAnthony Kim 	__be32 c_code_start;
105842ff286SAnthony Kim 	__be16 gen_ver;
106842ff286SAnthony Kim 	__be16 c_code_len;
107842ff286SAnthony Kim 
108842ff286SAnthony Kim 	__be32 vr_start;
109842ff286SAnthony Kim 	__be16 rsv0;
110842ff286SAnthony Kim 	__be16 vr_len;
111842ff286SAnthony Kim 
112842ff286SAnthony Kim 	__be32 ft_start;
113842ff286SAnthony Kim 	__be16 vr_version;
114842ff286SAnthony Kim 	__be16 ft_len;
115842ff286SAnthony Kim 
116842ff286SAnthony Kim 	__be16 core_ver;
117842ff286SAnthony Kim 	__be16 boot_ver;
118842ff286SAnthony Kim 
119842ff286SAnthony Kim 	__be16 release_ver;
120842ff286SAnthony Kim 	__be16 custom_ver;
121842ff286SAnthony Kim 
122842ff286SAnthony Kim 	u8 factory_id;
123842ff286SAnthony Kim 	u8 panel_type;
124842ff286SAnthony Kim 	u8 model_name[6];
125842ff286SAnthony Kim 
126842ff286SAnthony Kim 	__be16 extra_option;
127842ff286SAnthony Kim 	__be16 product_code;
128842ff286SAnthony Kim 
129842ff286SAnthony Kim 	__be16 vendor_id;
130842ff286SAnthony Kim 	__be16 product_id;
131842ff286SAnthony Kim };
132842ff286SAnthony Kim 
133842ff286SAnthony Kim struct pgm_packet {
134842ff286SAnthony Kim 	struct {
135842ff286SAnthony Kim 		u8 unused[3];
136842ff286SAnthony Kim 		u8 len;
137842ff286SAnthony Kim 		__be32 addr;
138842ff286SAnthony Kim 	} header;
139842ff286SAnthony Kim 	__be32 payload[HIDEEP_NVM_PAGE_SIZE / sizeof(__be32)];
140842ff286SAnthony Kim };
141842ff286SAnthony Kim 
142842ff286SAnthony Kim #define HIDEEP_XFER_BUF_SIZE	sizeof(struct pgm_packet)
143842ff286SAnthony Kim 
144842ff286SAnthony Kim struct hideep_ts {
145842ff286SAnthony Kim 	struct i2c_client *client;
146842ff286SAnthony Kim 	struct input_dev *input_dev;
147842ff286SAnthony Kim 	struct regmap *reg;
148842ff286SAnthony Kim 
149842ff286SAnthony Kim 	struct touchscreen_properties prop;
150842ff286SAnthony Kim 
151842ff286SAnthony Kim 	struct gpio_desc *reset_gpio;
152842ff286SAnthony Kim 
153842ff286SAnthony Kim 	struct regulator *vcc_vdd;
154842ff286SAnthony Kim 	struct regulator *vcc_vid;
155842ff286SAnthony Kim 
156842ff286SAnthony Kim 	struct mutex dev_mutex;
157842ff286SAnthony Kim 
158842ff286SAnthony Kim 	u32 tch_count;
159842ff286SAnthony Kim 	u32 lpm_count;
160842ff286SAnthony Kim 
161842ff286SAnthony Kim 	/*
162842ff286SAnthony Kim 	 * Data buffer to read packet from the device (contacts and key
163842ff286SAnthony Kim 	 * states). We align it on double-word boundary to keep word-sized
164842ff286SAnthony Kim 	 * fields in contact data and double-word-sized fields in program
165842ff286SAnthony Kim 	 * packet aligned.
166842ff286SAnthony Kim 	 */
167842ff286SAnthony Kim 	u8 xfer_buf[HIDEEP_XFER_BUF_SIZE] __aligned(4);
168842ff286SAnthony Kim 
169842ff286SAnthony Kim 	int key_num;
170842ff286SAnthony Kim 	u32 key_codes[HIDEEP_KEY_MAX];
171842ff286SAnthony Kim 
172842ff286SAnthony Kim 	struct dwz_info dwz_info;
173842ff286SAnthony Kim 
174842ff286SAnthony Kim 	unsigned int fw_size;
175842ff286SAnthony Kim 	u32 nvm_mask;
176842ff286SAnthony Kim };
177842ff286SAnthony Kim 
178842ff286SAnthony Kim static int hideep_pgm_w_mem(struct hideep_ts *ts, u32 addr,
179842ff286SAnthony Kim 			    const __be32 *data, size_t count)
180842ff286SAnthony Kim {
181842ff286SAnthony Kim 	struct pgm_packet *packet = (void *)ts->xfer_buf;
182842ff286SAnthony Kim 	size_t len = count * sizeof(*data);
183842ff286SAnthony Kim 	struct i2c_msg msg = {
184842ff286SAnthony Kim 		.addr	= ts->client->addr,
185842ff286SAnthony Kim 		.len	= len + sizeof(packet->header.len) +
186842ff286SAnthony Kim 				sizeof(packet->header.addr),
187842ff286SAnthony Kim 		.buf	= &packet->header.len,
188842ff286SAnthony Kim 	};
189842ff286SAnthony Kim 	int ret;
190842ff286SAnthony Kim 
191842ff286SAnthony Kim 	if (len > HIDEEP_NVM_PAGE_SIZE)
192842ff286SAnthony Kim 		return -EINVAL;
193842ff286SAnthony Kim 
194842ff286SAnthony Kim 	packet->header.len = 0x80 | (count - 1);
195842ff286SAnthony Kim 	packet->header.addr = cpu_to_be32(addr);
196842ff286SAnthony Kim 	memcpy(packet->payload, data, len);
197842ff286SAnthony Kim 
198842ff286SAnthony Kim 	ret = i2c_transfer(ts->client->adapter, &msg, 1);
199842ff286SAnthony Kim 	if (ret != 1)
200842ff286SAnthony Kim 		return ret < 0 ? ret : -EIO;
201842ff286SAnthony Kim 
202842ff286SAnthony Kim 	return 0;
203842ff286SAnthony Kim }
204842ff286SAnthony Kim 
205842ff286SAnthony Kim static int hideep_pgm_r_mem(struct hideep_ts *ts, u32 addr,
206842ff286SAnthony Kim 			    __be32 *data, size_t count)
207842ff286SAnthony Kim {
208842ff286SAnthony Kim 	struct pgm_packet *packet = (void *)ts->xfer_buf;
209842ff286SAnthony Kim 	size_t len = count * sizeof(*data);
210842ff286SAnthony Kim 	struct i2c_msg msg[] = {
211842ff286SAnthony Kim 		{
212842ff286SAnthony Kim 			.addr	= ts->client->addr,
213842ff286SAnthony Kim 			.len	= sizeof(packet->header.len) +
214842ff286SAnthony Kim 					sizeof(packet->header.addr),
215842ff286SAnthony Kim 			.buf	= &packet->header.len,
216842ff286SAnthony Kim 		},
217842ff286SAnthony Kim 		{
218842ff286SAnthony Kim 			.addr	= ts->client->addr,
219842ff286SAnthony Kim 			.flags	= I2C_M_RD,
220842ff286SAnthony Kim 			.len	= len,
221842ff286SAnthony Kim 			.buf	= (u8 *)data,
222842ff286SAnthony Kim 		},
223842ff286SAnthony Kim 	};
224842ff286SAnthony Kim 	int ret;
225842ff286SAnthony Kim 
226842ff286SAnthony Kim 	if (len > HIDEEP_NVM_PAGE_SIZE)
227842ff286SAnthony Kim 		return -EINVAL;
228842ff286SAnthony Kim 
229842ff286SAnthony Kim 	packet->header.len = count - 1;
230842ff286SAnthony Kim 	packet->header.addr = cpu_to_be32(addr);
231842ff286SAnthony Kim 
232842ff286SAnthony Kim 	ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
233842ff286SAnthony Kim 	if (ret != ARRAY_SIZE(msg))
234842ff286SAnthony Kim 		return ret < 0 ? ret : -EIO;
235842ff286SAnthony Kim 
236842ff286SAnthony Kim 	return 0;
237842ff286SAnthony Kim }
238842ff286SAnthony Kim 
239842ff286SAnthony Kim static int hideep_pgm_r_reg(struct hideep_ts *ts, u32 addr, u32 *val)
240842ff286SAnthony Kim {
241842ff286SAnthony Kim 	__be32 data;
242842ff286SAnthony Kim 	int error;
243842ff286SAnthony Kim 
244842ff286SAnthony Kim 	error = hideep_pgm_r_mem(ts, addr, &data, 1);
245842ff286SAnthony Kim 	if (error) {
246842ff286SAnthony Kim 		dev_err(&ts->client->dev,
247842ff286SAnthony Kim 			"read of register %#08x failed: %d\n",
248842ff286SAnthony Kim 			addr, error);
249842ff286SAnthony Kim 		return error;
250842ff286SAnthony Kim 	}
251842ff286SAnthony Kim 
252842ff286SAnthony Kim 	*val = be32_to_cpu(data);
253842ff286SAnthony Kim 	return 0;
254842ff286SAnthony Kim }
255842ff286SAnthony Kim 
256842ff286SAnthony Kim static int hideep_pgm_w_reg(struct hideep_ts *ts, u32 addr, u32 val)
257842ff286SAnthony Kim {
258842ff286SAnthony Kim 	__be32 data = cpu_to_be32(val);
259842ff286SAnthony Kim 	int error;
260842ff286SAnthony Kim 
261842ff286SAnthony Kim 	error = hideep_pgm_w_mem(ts, addr, &data, 1);
262842ff286SAnthony Kim 	if (error) {
263842ff286SAnthony Kim 		dev_err(&ts->client->dev,
264842ff286SAnthony Kim 			"write to register %#08x (%#08x) failed: %d\n",
265842ff286SAnthony Kim 			addr, val, error);
266842ff286SAnthony Kim 		return error;
267842ff286SAnthony Kim 	}
268842ff286SAnthony Kim 
269842ff286SAnthony Kim 	return 0;
270842ff286SAnthony Kim }
271842ff286SAnthony Kim 
272842ff286SAnthony Kim #define SW_RESET_IN_PGM(clk)					\
273842ff286SAnthony Kim {								\
274842ff286SAnthony Kim 	hideep_pgm_w_reg(ts, HIDEEP_SYSCON_WDT_CNT, (clk));	\
275842ff286SAnthony Kim 	hideep_pgm_w_reg(ts, HIDEEP_SYSCON_WDT_CON, 0x03);	\
276842ff286SAnthony Kim 	hideep_pgm_w_reg(ts, HIDEEP_SYSCON_WDT_CON, 0x01);	\
277842ff286SAnthony Kim }
278842ff286SAnthony Kim 
279842ff286SAnthony Kim #define SET_FLASH_PIO(ce)					\
280842ff286SAnthony Kim 	hideep_pgm_w_reg(ts, HIDEEP_FLASH_CON,			\
281842ff286SAnthony Kim 			 0x01 | ((ce) << 1))
282842ff286SAnthony Kim 
283842ff286SAnthony Kim #define SET_PIO_SIG(x, y)					\
284842ff286SAnthony Kim 	hideep_pgm_w_reg(ts, HIDEEP_FLASH_PIO_SIG + (x), (y))
285842ff286SAnthony Kim 
286842ff286SAnthony Kim #define SET_FLASH_HWCONTROL()					\
287842ff286SAnthony Kim 	hideep_pgm_w_reg(ts, HIDEEP_FLASH_CON, 0x00)
288842ff286SAnthony Kim 
289842ff286SAnthony Kim #define NVM_W_SFR(x, y)						\
290842ff286SAnthony Kim {								\
291842ff286SAnthony Kim 	SET_FLASH_PIO(1);					\
292842ff286SAnthony Kim 	SET_PIO_SIG(x, y);					\
293842ff286SAnthony Kim 	SET_FLASH_PIO(0);					\
294842ff286SAnthony Kim }
295842ff286SAnthony Kim 
296842ff286SAnthony Kim static void hideep_pgm_set(struct hideep_ts *ts)
297842ff286SAnthony Kim {
298842ff286SAnthony Kim 	hideep_pgm_w_reg(ts, HIDEEP_SYSCON_WDT_CON, 0x00);
299842ff286SAnthony Kim 	hideep_pgm_w_reg(ts, HIDEEP_SYSCON_SPC_CON, 0x00);
300842ff286SAnthony Kim 	hideep_pgm_w_reg(ts, HIDEEP_SYSCON_CLK_ENA, 0xFF);
301842ff286SAnthony Kim 	hideep_pgm_w_reg(ts, HIDEEP_SYSCON_CLK_CON, 0x01);
302842ff286SAnthony Kim 	hideep_pgm_w_reg(ts, HIDEEP_SYSCON_PWR_CON, 0x01);
303842ff286SAnthony Kim 	hideep_pgm_w_reg(ts, HIDEEP_FLASH_TIM, 0x03);
304842ff286SAnthony Kim 	hideep_pgm_w_reg(ts, HIDEEP_FLASH_CACHE_CFG, 0x00);
305842ff286SAnthony Kim }
306842ff286SAnthony Kim 
307842ff286SAnthony Kim static int hideep_pgm_get_pattern(struct hideep_ts *ts, u32 *pattern)
308842ff286SAnthony Kim {
309842ff286SAnthony Kim 	u16 p1 = 0xAF39;
310842ff286SAnthony Kim 	u16 p2 = 0xDF9D;
311842ff286SAnthony Kim 	int error;
312842ff286SAnthony Kim 
313842ff286SAnthony Kim 	error = regmap_bulk_write(ts->reg, p1, &p2, 1);
314842ff286SAnthony Kim 	if (error) {
315842ff286SAnthony Kim 		dev_err(&ts->client->dev,
316842ff286SAnthony Kim 			"%s: regmap_bulk_write() failed with %d\n",
317842ff286SAnthony Kim 			__func__, error);
318842ff286SAnthony Kim 		return error;
319842ff286SAnthony Kim 	}
320842ff286SAnthony Kim 
321842ff286SAnthony Kim 	usleep_range(1000, 1100);
322842ff286SAnthony Kim 
323842ff286SAnthony Kim 	/* flush invalid Tx load register */
324842ff286SAnthony Kim 	error = hideep_pgm_w_reg(ts, HIDEEP_ESI_TX_INVALID, 0x01);
325842ff286SAnthony Kim 	if (error)
326842ff286SAnthony Kim 		return error;
327842ff286SAnthony Kim 
328842ff286SAnthony Kim 	error = hideep_pgm_r_reg(ts, HIDEEP_SYSCON_PGM_ID, pattern);
329842ff286SAnthony Kim 	if (error)
330842ff286SAnthony Kim 		return error;
331842ff286SAnthony Kim 
332842ff286SAnthony Kim 	return 0;
333842ff286SAnthony Kim }
334842ff286SAnthony Kim 
335842ff286SAnthony Kim static int hideep_enter_pgm(struct hideep_ts *ts)
336842ff286SAnthony Kim {
337842ff286SAnthony Kim 	int retry_count = 10;
338842ff286SAnthony Kim 	u32 pattern;
339842ff286SAnthony Kim 	int error;
340842ff286SAnthony Kim 
341842ff286SAnthony Kim 	while (retry_count--) {
342842ff286SAnthony Kim 		error = hideep_pgm_get_pattern(ts, &pattern);
343842ff286SAnthony Kim 		if (error) {
344842ff286SAnthony Kim 			dev_err(&ts->client->dev,
345842ff286SAnthony Kim 				"hideep_pgm_get_pattern failed: %d\n", error);
346842ff286SAnthony Kim 		} else if (pattern != 0x39AF9DDF) {
347842ff286SAnthony Kim 			dev_err(&ts->client->dev, "%s: bad pattern: %#08x\n",
348842ff286SAnthony Kim 				__func__, pattern);
349842ff286SAnthony Kim 		} else {
350842ff286SAnthony Kim 			dev_dbg(&ts->client->dev, "found magic code");
351842ff286SAnthony Kim 
352842ff286SAnthony Kim 			hideep_pgm_set(ts);
353842ff286SAnthony Kim 			usleep_range(1000, 1100);
354842ff286SAnthony Kim 
355842ff286SAnthony Kim 			return 0;
356842ff286SAnthony Kim 		}
357842ff286SAnthony Kim 	}
358842ff286SAnthony Kim 
359842ff286SAnthony Kim 	dev_err(&ts->client->dev, "failed to  enter pgm mode\n");
360842ff286SAnthony Kim 	SW_RESET_IN_PGM(1000);
361842ff286SAnthony Kim 	return -EIO;
362842ff286SAnthony Kim }
363842ff286SAnthony Kim 
364842ff286SAnthony Kim static void hideep_nvm_unlock(struct hideep_ts *ts)
365842ff286SAnthony Kim {
366842ff286SAnthony Kim 	u32 unmask_code;
367842ff286SAnthony Kim 
368842ff286SAnthony Kim 	hideep_pgm_w_reg(ts, HIDEEP_FLASH_CFG, HIDEEP_NVM_SFR_RPAGE);
369842ff286SAnthony Kim 	hideep_pgm_r_reg(ts, 0x0000000C, &unmask_code);
370842ff286SAnthony Kim 	hideep_pgm_w_reg(ts, HIDEEP_FLASH_CFG, HIDEEP_NVM_DEFAULT_PAGE);
371842ff286SAnthony Kim 
372842ff286SAnthony Kim 	/* make it unprotected code */
373842ff286SAnthony Kim 	unmask_code &= ~HIDEEP_PROT_MODE;
374842ff286SAnthony Kim 
375842ff286SAnthony Kim 	/* compare unmask code */
376842ff286SAnthony Kim 	if (unmask_code != ts->nvm_mask)
377842ff286SAnthony Kim 		dev_warn(&ts->client->dev,
378842ff286SAnthony Kim 			 "read mask code different %#08x vs %#08x",
379842ff286SAnthony Kim 			 unmask_code, ts->nvm_mask);
380842ff286SAnthony Kim 
381842ff286SAnthony Kim 	hideep_pgm_w_reg(ts, HIDEEP_FLASH_CFG, HIDEEP_NVM_SFR_WPAGE);
382842ff286SAnthony Kim 	SET_FLASH_PIO(0);
383842ff286SAnthony Kim 
384842ff286SAnthony Kim 	NVM_W_SFR(HIDEEP_NVM_MASK_OFS, ts->nvm_mask);
385842ff286SAnthony Kim 	SET_FLASH_HWCONTROL();
386842ff286SAnthony Kim 	hideep_pgm_w_reg(ts, HIDEEP_FLASH_CFG, HIDEEP_NVM_DEFAULT_PAGE);
387842ff286SAnthony Kim }
388842ff286SAnthony Kim 
389842ff286SAnthony Kim static int hideep_check_status(struct hideep_ts *ts)
390842ff286SAnthony Kim {
391842ff286SAnthony Kim 	int time_out = 100;
392842ff286SAnthony Kim 	int status;
393842ff286SAnthony Kim 	int error;
394842ff286SAnthony Kim 
395842ff286SAnthony Kim 	while (time_out--) {
396842ff286SAnthony Kim 		error = hideep_pgm_r_reg(ts, HIDEEP_FLASH_STA, &status);
397842ff286SAnthony Kim 		if (!error && status)
398842ff286SAnthony Kim 			return 0;
399842ff286SAnthony Kim 
400842ff286SAnthony Kim 		usleep_range(1000, 1100);
401842ff286SAnthony Kim 	}
402842ff286SAnthony Kim 
403842ff286SAnthony Kim 	return -ETIMEDOUT;
404842ff286SAnthony Kim }
405842ff286SAnthony Kim 
406842ff286SAnthony Kim static int hideep_program_page(struct hideep_ts *ts, u32 addr,
407842ff286SAnthony Kim 			       const __be32 *ucode, size_t xfer_count)
408842ff286SAnthony Kim {
409842ff286SAnthony Kim 	u32 val;
410842ff286SAnthony Kim 	int error;
411842ff286SAnthony Kim 
412842ff286SAnthony Kim 	error = hideep_check_status(ts);
413842ff286SAnthony Kim 	if (error)
414842ff286SAnthony Kim 		return -EBUSY;
415842ff286SAnthony Kim 
416842ff286SAnthony Kim 	addr &= ~(HIDEEP_NVM_PAGE_SIZE - 1);
417842ff286SAnthony Kim 
418842ff286SAnthony Kim 	SET_FLASH_PIO(0);
419842ff286SAnthony Kim 	SET_FLASH_PIO(1);
420842ff286SAnthony Kim 
421842ff286SAnthony Kim 	/* erase page */
422842ff286SAnthony Kim 	SET_PIO_SIG(HIDEEP_PERASE | addr, 0xFFFFFFFF);
423842ff286SAnthony Kim 
424842ff286SAnthony Kim 	SET_FLASH_PIO(0);
425842ff286SAnthony Kim 
426842ff286SAnthony Kim 	error = hideep_check_status(ts);
427842ff286SAnthony Kim 	if (error)
428842ff286SAnthony Kim 		return -EBUSY;
429842ff286SAnthony Kim 
430842ff286SAnthony Kim 	/* write page */
431842ff286SAnthony Kim 	SET_FLASH_PIO(1);
432842ff286SAnthony Kim 
433842ff286SAnthony Kim 	val = be32_to_cpu(ucode[0]);
434842ff286SAnthony Kim 	SET_PIO_SIG(HIDEEP_WRONLY | addr, val);
435842ff286SAnthony Kim 
436842ff286SAnthony Kim 	hideep_pgm_w_mem(ts, HIDEEP_FLASH_PIO_SIG | HIDEEP_WRONLY,
437842ff286SAnthony Kim 			 ucode, xfer_count);
438842ff286SAnthony Kim 
439842ff286SAnthony Kim 	val = be32_to_cpu(ucode[xfer_count - 1]);
440842ff286SAnthony Kim 	SET_PIO_SIG(124, val);
441842ff286SAnthony Kim 
442842ff286SAnthony Kim 	SET_FLASH_PIO(0);
443842ff286SAnthony Kim 
444842ff286SAnthony Kim 	usleep_range(1000, 1100);
445842ff286SAnthony Kim 
446842ff286SAnthony Kim 	error = hideep_check_status(ts);
447842ff286SAnthony Kim 	if (error)
448842ff286SAnthony Kim 		return -EBUSY;
449842ff286SAnthony Kim 
450842ff286SAnthony Kim 	SET_FLASH_HWCONTROL();
451842ff286SAnthony Kim 
452842ff286SAnthony Kim 	return 0;
453842ff286SAnthony Kim }
454842ff286SAnthony Kim 
455842ff286SAnthony Kim static int hideep_program_nvm(struct hideep_ts *ts,
456842ff286SAnthony Kim 			      const __be32 *ucode, size_t ucode_len)
457842ff286SAnthony Kim {
458842ff286SAnthony Kim 	struct pgm_packet *packet_r = (void *)ts->xfer_buf;
459842ff286SAnthony Kim 	__be32 *current_ucode = packet_r->payload;
460842ff286SAnthony Kim 	size_t xfer_len;
461842ff286SAnthony Kim 	size_t xfer_count;
462842ff286SAnthony Kim 	u32 addr = 0;
463842ff286SAnthony Kim 	int error;
464842ff286SAnthony Kim 
465842ff286SAnthony Kim 	hideep_nvm_unlock(ts);
466842ff286SAnthony Kim 
467842ff286SAnthony Kim 	while (ucode_len > 0) {
468842ff286SAnthony Kim 		xfer_len = min_t(size_t, ucode_len, HIDEEP_NVM_PAGE_SIZE);
469842ff286SAnthony Kim 		xfer_count = xfer_len / sizeof(*ucode);
470842ff286SAnthony Kim 
471842ff286SAnthony Kim 		error = hideep_pgm_r_mem(ts, 0x00000000 + addr,
472842ff286SAnthony Kim 					 current_ucode, xfer_count);
473842ff286SAnthony Kim 		if (error) {
474842ff286SAnthony Kim 			dev_err(&ts->client->dev,
475842ff286SAnthony Kim 				"%s: failed to read page at offset %#08x: %d\n",
476842ff286SAnthony Kim 				__func__, addr, error);
477842ff286SAnthony Kim 			return error;
478842ff286SAnthony Kim 		}
479842ff286SAnthony Kim 
480842ff286SAnthony Kim 		/* See if the page needs updating */
481842ff286SAnthony Kim 		if (memcmp(ucode, current_ucode, xfer_len)) {
482842ff286SAnthony Kim 			error = hideep_program_page(ts, addr,
483842ff286SAnthony Kim 						    ucode, xfer_count);
484842ff286SAnthony Kim 			if (error) {
485842ff286SAnthony Kim 				dev_err(&ts->client->dev,
486842ff286SAnthony Kim 					"%s: iwrite failure @%#08x: %d\n",
487842ff286SAnthony Kim 					__func__, addr, error);
488842ff286SAnthony Kim 				return error;
489842ff286SAnthony Kim 			}
490842ff286SAnthony Kim 
491842ff286SAnthony Kim 			usleep_range(1000, 1100);
492842ff286SAnthony Kim 		}
493842ff286SAnthony Kim 
494842ff286SAnthony Kim 		ucode += xfer_count;
495842ff286SAnthony Kim 		addr += xfer_len;
496842ff286SAnthony Kim 		ucode_len -= xfer_len;
497842ff286SAnthony Kim 	}
498842ff286SAnthony Kim 
499842ff286SAnthony Kim 	return 0;
500842ff286SAnthony Kim }
501842ff286SAnthony Kim 
502842ff286SAnthony Kim static int hideep_verify_nvm(struct hideep_ts *ts,
503842ff286SAnthony Kim 			     const __be32 *ucode, size_t ucode_len)
504842ff286SAnthony Kim {
505842ff286SAnthony Kim 	struct pgm_packet *packet_r = (void *)ts->xfer_buf;
506842ff286SAnthony Kim 	__be32 *current_ucode = packet_r->payload;
507842ff286SAnthony Kim 	size_t xfer_len;
508842ff286SAnthony Kim 	size_t xfer_count;
509842ff286SAnthony Kim 	u32 addr = 0;
510842ff286SAnthony Kim 	int i;
511842ff286SAnthony Kim 	int error;
512842ff286SAnthony Kim 
513842ff286SAnthony Kim 	while (ucode_len > 0) {
514842ff286SAnthony Kim 		xfer_len = min_t(size_t, ucode_len, HIDEEP_NVM_PAGE_SIZE);
515842ff286SAnthony Kim 		xfer_count = xfer_len / sizeof(*ucode);
516842ff286SAnthony Kim 
517842ff286SAnthony Kim 		error = hideep_pgm_r_mem(ts, 0x00000000 + addr,
518842ff286SAnthony Kim 					 current_ucode, xfer_count);
519842ff286SAnthony Kim 		if (error) {
520842ff286SAnthony Kim 			dev_err(&ts->client->dev,
521842ff286SAnthony Kim 				"%s: failed to read page at offset %#08x: %d\n",
522842ff286SAnthony Kim 				__func__, addr, error);
523842ff286SAnthony Kim 			return error;
524842ff286SAnthony Kim 		}
525842ff286SAnthony Kim 
526842ff286SAnthony Kim 		if (memcmp(ucode, current_ucode, xfer_len)) {
527842ff286SAnthony Kim 			const u8 *ucode_bytes = (const u8 *)ucode;
528842ff286SAnthony Kim 			const u8 *current_bytes = (const u8 *)current_ucode;
529842ff286SAnthony Kim 
530842ff286SAnthony Kim 			for (i = 0; i < xfer_len; i++)
531842ff286SAnthony Kim 				if (ucode_bytes[i] != current_bytes[i])
532842ff286SAnthony Kim 					dev_err(&ts->client->dev,
533842ff286SAnthony Kim 						"%s: mismatch @%#08x: (%#02x vs %#02x)\n",
534842ff286SAnthony Kim 						__func__, addr + i,
535842ff286SAnthony Kim 						ucode_bytes[i],
536842ff286SAnthony Kim 						current_bytes[i]);
537842ff286SAnthony Kim 
538842ff286SAnthony Kim 			return -EIO;
539842ff286SAnthony Kim 		}
540842ff286SAnthony Kim 
541842ff286SAnthony Kim 		ucode += xfer_count;
542842ff286SAnthony Kim 		addr += xfer_len;
543842ff286SAnthony Kim 		ucode_len -= xfer_len;
544842ff286SAnthony Kim 	}
545842ff286SAnthony Kim 
546842ff286SAnthony Kim 	return 0;
547842ff286SAnthony Kim }
548842ff286SAnthony Kim 
549842ff286SAnthony Kim static int hideep_load_dwz(struct hideep_ts *ts)
550842ff286SAnthony Kim {
551842ff286SAnthony Kim 	u16 product_code;
552842ff286SAnthony Kim 	int error;
553842ff286SAnthony Kim 
554842ff286SAnthony Kim 	error = hideep_enter_pgm(ts);
555842ff286SAnthony Kim 	if (error)
556842ff286SAnthony Kim 		return error;
557842ff286SAnthony Kim 
558842ff286SAnthony Kim 	msleep(50);
559842ff286SAnthony Kim 
560842ff286SAnthony Kim 	error = hideep_pgm_r_mem(ts, HIDEEP_DWZ_INFO,
561842ff286SAnthony Kim 				 (void *)&ts->dwz_info,
562842ff286SAnthony Kim 				 sizeof(ts->dwz_info) / sizeof(__be32));
563842ff286SAnthony Kim 
564842ff286SAnthony Kim 	SW_RESET_IN_PGM(10);
565842ff286SAnthony Kim 	msleep(50);
566842ff286SAnthony Kim 
567842ff286SAnthony Kim 	if (error) {
568842ff286SAnthony Kim 		dev_err(&ts->client->dev,
569842ff286SAnthony Kim 			"failed to fetch DWZ data: %d\n", error);
570842ff286SAnthony Kim 		return error;
571842ff286SAnthony Kim 	}
572842ff286SAnthony Kim 
573842ff286SAnthony Kim 	product_code = be16_to_cpu(ts->dwz_info.product_code);
574842ff286SAnthony Kim 
575842ff286SAnthony Kim 	switch (product_code & 0xF0) {
576842ff286SAnthony Kim 	case 0x40:
577842ff286SAnthony Kim 		dev_dbg(&ts->client->dev, "used crimson IC");
578842ff286SAnthony Kim 		ts->fw_size = 1024 * 48;
579842ff286SAnthony Kim 		ts->nvm_mask = 0x00310000;
580842ff286SAnthony Kim 		break;
581842ff286SAnthony Kim 	case 0x60:
582842ff286SAnthony Kim 		dev_dbg(&ts->client->dev, "used lime IC");
583842ff286SAnthony Kim 		ts->fw_size = 1024 * 64;
584842ff286SAnthony Kim 		ts->nvm_mask = 0x0030027B;
585842ff286SAnthony Kim 		break;
586842ff286SAnthony Kim 	default:
587842ff286SAnthony Kim 		dev_err(&ts->client->dev, "product code is wrong: %#04x",
588842ff286SAnthony Kim 			product_code);
589842ff286SAnthony Kim 		return -EINVAL;
590842ff286SAnthony Kim 	}
591842ff286SAnthony Kim 
592842ff286SAnthony Kim 	dev_dbg(&ts->client->dev, "firmware release version: %#04x",
593842ff286SAnthony Kim 		be16_to_cpu(ts->dwz_info.release_ver));
594842ff286SAnthony Kim 
595842ff286SAnthony Kim 	return 0;
596842ff286SAnthony Kim }
597842ff286SAnthony Kim 
598842ff286SAnthony Kim static int hideep_flash_firmware(struct hideep_ts *ts,
599842ff286SAnthony Kim 				 const __be32 *ucode, size_t ucode_len)
600842ff286SAnthony Kim {
601842ff286SAnthony Kim 	int retry_cnt = 3;
602842ff286SAnthony Kim 	int error;
603842ff286SAnthony Kim 
604842ff286SAnthony Kim 	while (retry_cnt--) {
605842ff286SAnthony Kim 		error = hideep_program_nvm(ts, ucode, ucode_len);
606842ff286SAnthony Kim 		if (!error) {
607842ff286SAnthony Kim 			error = hideep_verify_nvm(ts, ucode, ucode_len);
608842ff286SAnthony Kim 			if (!error)
609842ff286SAnthony Kim 				return 0;
610842ff286SAnthony Kim 		}
611842ff286SAnthony Kim 	}
612842ff286SAnthony Kim 
613842ff286SAnthony Kim 	return error;
614842ff286SAnthony Kim }
615842ff286SAnthony Kim 
616842ff286SAnthony Kim static int hideep_update_firmware(struct hideep_ts *ts,
617842ff286SAnthony Kim 				  const __be32 *ucode, size_t ucode_len)
618842ff286SAnthony Kim {
619842ff286SAnthony Kim 	int error, error2;
620842ff286SAnthony Kim 
621842ff286SAnthony Kim 	dev_dbg(&ts->client->dev, "starting firmware update");
622842ff286SAnthony Kim 
623842ff286SAnthony Kim 	/* enter program mode */
624842ff286SAnthony Kim 	error = hideep_enter_pgm(ts);
625842ff286SAnthony Kim 	if (error)
626842ff286SAnthony Kim 		return error;
627842ff286SAnthony Kim 
628842ff286SAnthony Kim 	error = hideep_flash_firmware(ts, ucode, ucode_len);
629842ff286SAnthony Kim 	if (error)
630842ff286SAnthony Kim 		dev_err(&ts->client->dev,
631842ff286SAnthony Kim 			"firmware update failed: %d\n", error);
632842ff286SAnthony Kim 	else
633842ff286SAnthony Kim 		dev_dbg(&ts->client->dev, "firmware updated successfully\n");
634842ff286SAnthony Kim 
635842ff286SAnthony Kim 	SW_RESET_IN_PGM(1000);
636842ff286SAnthony Kim 
637842ff286SAnthony Kim 	error2 = hideep_load_dwz(ts);
638842ff286SAnthony Kim 	if (error2)
639842ff286SAnthony Kim 		dev_err(&ts->client->dev,
640842ff286SAnthony Kim 			"failed to load dwz after firmware update: %d\n",
641842ff286SAnthony Kim 			error2);
642842ff286SAnthony Kim 
643842ff286SAnthony Kim 	return error ?: error2;
644842ff286SAnthony Kim }
645842ff286SAnthony Kim 
646842ff286SAnthony Kim static int hideep_power_on(struct hideep_ts *ts)
647842ff286SAnthony Kim {
648842ff286SAnthony Kim 	int error = 0;
649842ff286SAnthony Kim 
650842ff286SAnthony Kim 	error = regulator_enable(ts->vcc_vdd);
651842ff286SAnthony Kim 	if (error)
652842ff286SAnthony Kim 		dev_err(&ts->client->dev,
653842ff286SAnthony Kim 			"failed to enable 'vdd' regulator: %d", error);
654842ff286SAnthony Kim 
655842ff286SAnthony Kim 	usleep_range(999, 1000);
656842ff286SAnthony Kim 
657842ff286SAnthony Kim 	error = regulator_enable(ts->vcc_vid);
658842ff286SAnthony Kim 	if (error)
659842ff286SAnthony Kim 		dev_err(&ts->client->dev,
660842ff286SAnthony Kim 			"failed to enable 'vcc_vid' regulator: %d",
661842ff286SAnthony Kim 			error);
662842ff286SAnthony Kim 
663842ff286SAnthony Kim 	msleep(30);
664842ff286SAnthony Kim 
665842ff286SAnthony Kim 	if (ts->reset_gpio) {
666842ff286SAnthony Kim 		gpiod_set_value_cansleep(ts->reset_gpio, 0);
667842ff286SAnthony Kim 	} else {
668842ff286SAnthony Kim 		error = regmap_write(ts->reg, HIDEEP_RESET_CMD, 0x01);
669842ff286SAnthony Kim 		if (error)
670842ff286SAnthony Kim 			dev_err(&ts->client->dev,
671842ff286SAnthony Kim 				"failed to send 'reset' command: %d\n", error);
672842ff286SAnthony Kim 	}
673842ff286SAnthony Kim 
674842ff286SAnthony Kim 	msleep(50);
675842ff286SAnthony Kim 
676842ff286SAnthony Kim 	return error;
677842ff286SAnthony Kim }
678842ff286SAnthony Kim 
679842ff286SAnthony Kim static void hideep_power_off(void *data)
680842ff286SAnthony Kim {
681842ff286SAnthony Kim 	struct hideep_ts *ts = data;
682842ff286SAnthony Kim 
683842ff286SAnthony Kim 	if (ts->reset_gpio)
684842ff286SAnthony Kim 		gpiod_set_value(ts->reset_gpio, 1);
685842ff286SAnthony Kim 
686842ff286SAnthony Kim 	regulator_disable(ts->vcc_vid);
687842ff286SAnthony Kim 	regulator_disable(ts->vcc_vdd);
688842ff286SAnthony Kim }
689842ff286SAnthony Kim 
690842ff286SAnthony Kim #define __GET_MT_TOOL_TYPE(type) ((type) == 0x01 ? MT_TOOL_FINGER : MT_TOOL_PEN)
691842ff286SAnthony Kim 
692842ff286SAnthony Kim static void hideep_report_slot(struct input_dev *input,
693842ff286SAnthony Kim 			       const struct hideep_event *event)
694842ff286SAnthony Kim {
695842ff286SAnthony Kim 	input_mt_slot(input, event->index & 0x0f);
696842ff286SAnthony Kim 	input_mt_report_slot_state(input,
697842ff286SAnthony Kim 				   __GET_MT_TOOL_TYPE(event->type),
698842ff286SAnthony Kim 				   !(event->flag & HIDEEP_MT_RELEASED));
699842ff286SAnthony Kim 	if (!(event->flag & HIDEEP_MT_RELEASED)) {
700842ff286SAnthony Kim 		input_report_abs(input, ABS_MT_POSITION_X,
701842ff286SAnthony Kim 				 le16_to_cpup(&event->x));
702842ff286SAnthony Kim 		input_report_abs(input, ABS_MT_POSITION_Y,
703842ff286SAnthony Kim 				 le16_to_cpup(&event->y));
704842ff286SAnthony Kim 		input_report_abs(input, ABS_MT_PRESSURE,
705842ff286SAnthony Kim 				 le16_to_cpup(&event->z));
706842ff286SAnthony Kim 		input_report_abs(input, ABS_MT_TOUCH_MAJOR, event->w);
707842ff286SAnthony Kim 	}
708842ff286SAnthony Kim }
709842ff286SAnthony Kim 
710842ff286SAnthony Kim static void hideep_parse_and_report(struct hideep_ts *ts)
711842ff286SAnthony Kim {
712842ff286SAnthony Kim 	const struct hideep_event *events =
713842ff286SAnthony Kim 			(void *)&ts->xfer_buf[HIDEEP_TOUCH_EVENT_INDEX];
714842ff286SAnthony Kim 	const u8 *keys = &ts->xfer_buf[HIDEEP_KEY_EVENT_INDEX];
715842ff286SAnthony Kim 	int touch_count = ts->xfer_buf[0];
716842ff286SAnthony Kim 	int key_count = ts->xfer_buf[1] & 0x0f;
717842ff286SAnthony Kim 	int lpm_count = ts->xfer_buf[1] & 0xf0;
718842ff286SAnthony Kim 	int i;
719842ff286SAnthony Kim 
720842ff286SAnthony Kim 	/* get touch event count */
721842ff286SAnthony Kim 	dev_dbg(&ts->client->dev, "mt = %d, key = %d, lpm = %02x",
722842ff286SAnthony Kim 		touch_count, key_count, lpm_count);
723842ff286SAnthony Kim 
724842ff286SAnthony Kim 	touch_count = min(touch_count, HIDEEP_MT_MAX);
725842ff286SAnthony Kim 	for (i = 0; i < touch_count; i++)
726842ff286SAnthony Kim 		hideep_report_slot(ts->input_dev, events + i);
727842ff286SAnthony Kim 
728842ff286SAnthony Kim 	key_count = min(key_count, HIDEEP_KEY_MAX);
729842ff286SAnthony Kim 	for (i = 0; i < key_count; i++) {
730842ff286SAnthony Kim 		u8 key_data = keys[i * 2];
731842ff286SAnthony Kim 
732842ff286SAnthony Kim 		input_report_key(ts->input_dev,
733842ff286SAnthony Kim 				 ts->key_codes[key_data & HIDEEP_KEY_IDX_MASK],
734842ff286SAnthony Kim 				 key_data & HIDEEP_KEY_PRESSED_MASK);
735842ff286SAnthony Kim 	}
736842ff286SAnthony Kim 
737842ff286SAnthony Kim 	input_mt_sync_frame(ts->input_dev);
738842ff286SAnthony Kim 	input_sync(ts->input_dev);
739842ff286SAnthony Kim }
740842ff286SAnthony Kim 
741842ff286SAnthony Kim static irqreturn_t hideep_irq(int irq, void *handle)
742842ff286SAnthony Kim {
743842ff286SAnthony Kim 	struct hideep_ts *ts = handle;
744842ff286SAnthony Kim 	int error;
745842ff286SAnthony Kim 
746842ff286SAnthony Kim 	BUILD_BUG_ON(HIDEEP_MAX_EVENT > HIDEEP_XFER_BUF_SIZE);
747842ff286SAnthony Kim 
748842ff286SAnthony Kim 	error = regmap_bulk_read(ts->reg, HIDEEP_EVENT_ADDR,
749842ff286SAnthony Kim 				 ts->xfer_buf, HIDEEP_MAX_EVENT / 2);
750842ff286SAnthony Kim 	if (error) {
751842ff286SAnthony Kim 		dev_err(&ts->client->dev, "failed to read events: %d\n", error);
752842ff286SAnthony Kim 		goto out;
753842ff286SAnthony Kim 	}
754842ff286SAnthony Kim 
755842ff286SAnthony Kim 	hideep_parse_and_report(ts);
756842ff286SAnthony Kim 
757842ff286SAnthony Kim out:
758842ff286SAnthony Kim 	return IRQ_HANDLED;
759842ff286SAnthony Kim }
760842ff286SAnthony Kim 
761842ff286SAnthony Kim static int hideep_get_axis_info(struct hideep_ts *ts)
762842ff286SAnthony Kim {
763842ff286SAnthony Kim 	__le16 val[2];
764842ff286SAnthony Kim 	int error;
765842ff286SAnthony Kim 
766842ff286SAnthony Kim 	error = regmap_bulk_read(ts->reg, 0x28, val, ARRAY_SIZE(val));
767842ff286SAnthony Kim 	if (error)
768842ff286SAnthony Kim 		return error;
769842ff286SAnthony Kim 
770842ff286SAnthony Kim 	ts->prop.max_x = le16_to_cpup(val);
771842ff286SAnthony Kim 	ts->prop.max_y = le16_to_cpup(val + 1);
772842ff286SAnthony Kim 
773842ff286SAnthony Kim 	dev_dbg(&ts->client->dev, "X: %d, Y: %d",
774842ff286SAnthony Kim 		ts->prop.max_x, ts->prop.max_y);
775842ff286SAnthony Kim 
776842ff286SAnthony Kim 	return 0;
777842ff286SAnthony Kim }
778842ff286SAnthony Kim 
779842ff286SAnthony Kim static int hideep_init_input(struct hideep_ts *ts)
780842ff286SAnthony Kim {
781842ff286SAnthony Kim 	struct device *dev = &ts->client->dev;
782842ff286SAnthony Kim 	int i;
783842ff286SAnthony Kim 	int error;
784842ff286SAnthony Kim 
785842ff286SAnthony Kim 	ts->input_dev = devm_input_allocate_device(dev);
786842ff286SAnthony Kim 	if (!ts->input_dev) {
787842ff286SAnthony Kim 		dev_err(dev, "failed to allocate input device\n");
788842ff286SAnthony Kim 		return -ENOMEM;
789842ff286SAnthony Kim 	}
790842ff286SAnthony Kim 
791842ff286SAnthony Kim 	ts->input_dev->name = HIDEEP_TS_NAME;
792842ff286SAnthony Kim 	ts->input_dev->id.bustype = BUS_I2C;
793842ff286SAnthony Kim 	input_set_drvdata(ts->input_dev, ts);
794842ff286SAnthony Kim 
795842ff286SAnthony Kim 	input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
796842ff286SAnthony Kim 	input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
797842ff286SAnthony Kim 	input_set_abs_params(ts->input_dev, ABS_MT_PRESSURE, 0, 65535, 0, 0);
798842ff286SAnthony Kim 	input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
799842ff286SAnthony Kim 	input_set_abs_params(ts->input_dev, ABS_MT_TOOL_TYPE,
800842ff286SAnthony Kim 			     0, MT_TOOL_MAX, 0, 0);
801842ff286SAnthony Kim 	touchscreen_parse_properties(ts->input_dev, true, &ts->prop);
802842ff286SAnthony Kim 
803842ff286SAnthony Kim 	if (ts->prop.max_x == 0 || ts->prop.max_y == 0) {
804842ff286SAnthony Kim 		error = hideep_get_axis_info(ts);
805842ff286SAnthony Kim 		if (error)
806842ff286SAnthony Kim 			return error;
807842ff286SAnthony Kim 	}
808842ff286SAnthony Kim 
809842ff286SAnthony Kim 	error = input_mt_init_slots(ts->input_dev, HIDEEP_MT_MAX,
810842ff286SAnthony Kim 				    INPUT_MT_DIRECT);
811842ff286SAnthony Kim 	if (error)
812842ff286SAnthony Kim 		return error;
813842ff286SAnthony Kim 
814842ff286SAnthony Kim 	ts->key_num = device_property_read_u32_array(dev, "linux,keycodes",
815842ff286SAnthony Kim 						     NULL, 0);
816842ff286SAnthony Kim 	if (ts->key_num > HIDEEP_KEY_MAX) {
817842ff286SAnthony Kim 		dev_err(dev, "too many keys defined: %d\n",
818842ff286SAnthony Kim 			ts->key_num);
819842ff286SAnthony Kim 		return -EINVAL;
820842ff286SAnthony Kim 	}
821842ff286SAnthony Kim 
822842ff286SAnthony Kim 	if (ts->key_num <= 0) {
823842ff286SAnthony Kim 		dev_dbg(dev,
824842ff286SAnthony Kim 			"missing or malformed 'linux,keycodes' property\n");
825842ff286SAnthony Kim 	} else {
826842ff286SAnthony Kim 		error = device_property_read_u32_array(dev, "linux,keycodes",
827842ff286SAnthony Kim 						       ts->key_codes,
828842ff286SAnthony Kim 						       ts->key_num);
829842ff286SAnthony Kim 		if (error) {
830842ff286SAnthony Kim 			dev_dbg(dev, "failed to read keymap: %d", error);
831842ff286SAnthony Kim 			return error;
832842ff286SAnthony Kim 		}
833842ff286SAnthony Kim 
834842ff286SAnthony Kim 		if (ts->key_num) {
835842ff286SAnthony Kim 			ts->input_dev->keycode = ts->key_codes;
836842ff286SAnthony Kim 			ts->input_dev->keycodesize = sizeof(ts->key_codes[0]);
837842ff286SAnthony Kim 			ts->input_dev->keycodemax = ts->key_num;
838842ff286SAnthony Kim 
839842ff286SAnthony Kim 			for (i = 0; i < ts->key_num; i++)
840842ff286SAnthony Kim 				input_set_capability(ts->input_dev, EV_KEY,
841842ff286SAnthony Kim 					ts->key_codes[i]);
842842ff286SAnthony Kim 		}
843842ff286SAnthony Kim 	}
844842ff286SAnthony Kim 
845842ff286SAnthony Kim 	error = input_register_device(ts->input_dev);
846842ff286SAnthony Kim 	if (error) {
847842ff286SAnthony Kim 		dev_err(dev, "failed to register input device: %d", error);
848842ff286SAnthony Kim 		return error;
849842ff286SAnthony Kim 	}
850842ff286SAnthony Kim 
851842ff286SAnthony Kim 	return 0;
852842ff286SAnthony Kim }
853842ff286SAnthony Kim 
854842ff286SAnthony Kim static ssize_t hideep_update_fw(struct device *dev,
855842ff286SAnthony Kim 				struct device_attribute *attr,
856842ff286SAnthony Kim 				const char *buf, size_t count)
857842ff286SAnthony Kim {
858842ff286SAnthony Kim 	struct i2c_client *client = to_i2c_client(dev);
859842ff286SAnthony Kim 	struct hideep_ts *ts = i2c_get_clientdata(client);
860842ff286SAnthony Kim 	const struct firmware *fw_entry;
861842ff286SAnthony Kim 	char *fw_name;
862842ff286SAnthony Kim 	int mode;
863842ff286SAnthony Kim 	int error;
864842ff286SAnthony Kim 
865842ff286SAnthony Kim 	error = kstrtoint(buf, 0, &mode);
866842ff286SAnthony Kim 	if (error)
867842ff286SAnthony Kim 		return error;
868842ff286SAnthony Kim 
869842ff286SAnthony Kim 	fw_name = kasprintf(GFP_KERNEL, "hideep_ts_%04x.bin",
870842ff286SAnthony Kim 			    be16_to_cpu(ts->dwz_info.product_id));
871842ff286SAnthony Kim 	if (!fw_name)
872842ff286SAnthony Kim 		return -ENOMEM;
873842ff286SAnthony Kim 
874842ff286SAnthony Kim 	error = request_firmware(&fw_entry, fw_name, dev);
875842ff286SAnthony Kim 	if (error) {
876842ff286SAnthony Kim 		dev_err(dev, "failed to request firmware %s: %d",
877842ff286SAnthony Kim 			fw_name, error);
878842ff286SAnthony Kim 		goto out_free_fw_name;
879842ff286SAnthony Kim 	}
880842ff286SAnthony Kim 
881842ff286SAnthony Kim 	if (fw_entry->size % sizeof(__be32)) {
882842ff286SAnthony Kim 		dev_err(dev, "invalid firmware size %zu\n", fw_entry->size);
883842ff286SAnthony Kim 		error = -EINVAL;
884842ff286SAnthony Kim 		goto out_release_fw;
885842ff286SAnthony Kim 	}
886842ff286SAnthony Kim 
887842ff286SAnthony Kim 	if (fw_entry->size > ts->fw_size) {
888842ff286SAnthony Kim 		dev_err(dev, "fw size (%zu) is too big (memory size %d)\n",
889842ff286SAnthony Kim 			fw_entry->size, ts->fw_size);
890842ff286SAnthony Kim 		error = -EFBIG;
891842ff286SAnthony Kim 		goto out_release_fw;
892842ff286SAnthony Kim 	}
893842ff286SAnthony Kim 
894842ff286SAnthony Kim 	mutex_lock(&ts->dev_mutex);
895842ff286SAnthony Kim 	disable_irq(client->irq);
896842ff286SAnthony Kim 
897842ff286SAnthony Kim 	error = hideep_update_firmware(ts, (const __be32 *)fw_entry->data,
898842ff286SAnthony Kim 				       fw_entry->size);
899842ff286SAnthony Kim 
900842ff286SAnthony Kim 	enable_irq(client->irq);
901842ff286SAnthony Kim 	mutex_unlock(&ts->dev_mutex);
902842ff286SAnthony Kim 
903842ff286SAnthony Kim out_release_fw:
904842ff286SAnthony Kim 	release_firmware(fw_entry);
905842ff286SAnthony Kim out_free_fw_name:
906842ff286SAnthony Kim 	kfree(fw_name);
907842ff286SAnthony Kim 
908842ff286SAnthony Kim 	return error ?: count;
909842ff286SAnthony Kim }
910842ff286SAnthony Kim 
911842ff286SAnthony Kim static ssize_t hideep_fw_version_show(struct device *dev,
912842ff286SAnthony Kim 				      struct device_attribute *attr, char *buf)
913842ff286SAnthony Kim {
914842ff286SAnthony Kim 	struct i2c_client *client = to_i2c_client(dev);
915842ff286SAnthony Kim 	struct hideep_ts *ts = i2c_get_clientdata(client);
916842ff286SAnthony Kim 	ssize_t len;
917842ff286SAnthony Kim 
918842ff286SAnthony Kim 	mutex_lock(&ts->dev_mutex);
919842ff286SAnthony Kim 	len = scnprintf(buf, PAGE_SIZE, "%04x\n",
920842ff286SAnthony Kim 			be16_to_cpu(ts->dwz_info.release_ver));
921842ff286SAnthony Kim 	mutex_unlock(&ts->dev_mutex);
922842ff286SAnthony Kim 
923842ff286SAnthony Kim 	return len;
924842ff286SAnthony Kim }
925842ff286SAnthony Kim 
926842ff286SAnthony Kim static ssize_t hideep_product_id_show(struct device *dev,
927842ff286SAnthony Kim 				      struct device_attribute *attr, char *buf)
928842ff286SAnthony Kim {
929842ff286SAnthony Kim 	struct i2c_client *client = to_i2c_client(dev);
930842ff286SAnthony Kim 	struct hideep_ts *ts = i2c_get_clientdata(client);
931842ff286SAnthony Kim 	ssize_t len;
932842ff286SAnthony Kim 
933842ff286SAnthony Kim 	mutex_lock(&ts->dev_mutex);
934842ff286SAnthony Kim 	len = scnprintf(buf, PAGE_SIZE, "%04x\n",
935842ff286SAnthony Kim 			be16_to_cpu(ts->dwz_info.product_id));
936842ff286SAnthony Kim 	mutex_unlock(&ts->dev_mutex);
937842ff286SAnthony Kim 
938842ff286SAnthony Kim 	return len;
939842ff286SAnthony Kim }
940842ff286SAnthony Kim 
941842ff286SAnthony Kim static DEVICE_ATTR(version, 0664, hideep_fw_version_show, NULL);
942842ff286SAnthony Kim static DEVICE_ATTR(product_id, 0664, hideep_product_id_show, NULL);
943842ff286SAnthony Kim static DEVICE_ATTR(update_fw, 0664, NULL, hideep_update_fw);
944842ff286SAnthony Kim 
945842ff286SAnthony Kim static struct attribute *hideep_ts_sysfs_entries[] = {
946842ff286SAnthony Kim 	&dev_attr_version.attr,
947842ff286SAnthony Kim 	&dev_attr_product_id.attr,
948842ff286SAnthony Kim 	&dev_attr_update_fw.attr,
949842ff286SAnthony Kim 	NULL,
950842ff286SAnthony Kim };
951842ff286SAnthony Kim 
952842ff286SAnthony Kim static const struct attribute_group hideep_ts_attr_group = {
953842ff286SAnthony Kim 	.attrs = hideep_ts_sysfs_entries,
954842ff286SAnthony Kim };
955842ff286SAnthony Kim 
956842ff286SAnthony Kim static int __maybe_unused hideep_suspend(struct device *dev)
957842ff286SAnthony Kim {
958842ff286SAnthony Kim 	struct i2c_client *client = to_i2c_client(dev);
959842ff286SAnthony Kim 	struct hideep_ts *ts = i2c_get_clientdata(client);
960842ff286SAnthony Kim 
961842ff286SAnthony Kim 	disable_irq(client->irq);
962842ff286SAnthony Kim 	hideep_power_off(ts);
963842ff286SAnthony Kim 
964842ff286SAnthony Kim 	return 0;
965842ff286SAnthony Kim }
966842ff286SAnthony Kim 
967842ff286SAnthony Kim static int __maybe_unused hideep_resume(struct device *dev)
968842ff286SAnthony Kim {
969842ff286SAnthony Kim 	struct i2c_client *client = to_i2c_client(dev);
970842ff286SAnthony Kim 	struct hideep_ts *ts = i2c_get_clientdata(client);
971842ff286SAnthony Kim 	int error;
972842ff286SAnthony Kim 
973842ff286SAnthony Kim 	error = hideep_power_on(ts);
974842ff286SAnthony Kim 	if (error) {
975842ff286SAnthony Kim 		dev_err(&client->dev, "power on failed");
976842ff286SAnthony Kim 		return error;
977842ff286SAnthony Kim 	}
978842ff286SAnthony Kim 
979842ff286SAnthony Kim 	enable_irq(client->irq);
980842ff286SAnthony Kim 
981842ff286SAnthony Kim 	return 0;
982842ff286SAnthony Kim }
983842ff286SAnthony Kim 
984842ff286SAnthony Kim static SIMPLE_DEV_PM_OPS(hideep_pm_ops, hideep_suspend, hideep_resume);
985842ff286SAnthony Kim 
986842ff286SAnthony Kim static const struct regmap_config hideep_regmap_config = {
987842ff286SAnthony Kim 	.reg_bits = 16,
988842ff286SAnthony Kim 	.reg_format_endian = REGMAP_ENDIAN_LITTLE,
989842ff286SAnthony Kim 	.val_bits = 16,
990842ff286SAnthony Kim 	.val_format_endian = REGMAP_ENDIAN_LITTLE,
991842ff286SAnthony Kim 	.max_register = 0xffff,
992842ff286SAnthony Kim };
993842ff286SAnthony Kim 
994842ff286SAnthony Kim static int hideep_probe(struct i2c_client *client,
995842ff286SAnthony Kim 			const struct i2c_device_id *id)
996842ff286SAnthony Kim {
997842ff286SAnthony Kim 	struct hideep_ts *ts;
998842ff286SAnthony Kim 	int error;
999842ff286SAnthony Kim 
1000842ff286SAnthony Kim 	/* check i2c bus */
1001842ff286SAnthony Kim 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1002842ff286SAnthony Kim 		dev_err(&client->dev, "check i2c device error");
1003842ff286SAnthony Kim 		return -ENODEV;
1004842ff286SAnthony Kim 	}
1005842ff286SAnthony Kim 
1006842ff286SAnthony Kim 	if (client->irq <= 0) {
1007842ff286SAnthony Kim 		dev_err(&client->dev, "missing irq: %d\n", client->irq);
1008842ff286SAnthony Kim 		return -EINVAL;
1009842ff286SAnthony Kim 	}
1010842ff286SAnthony Kim 
1011842ff286SAnthony Kim 	ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1012842ff286SAnthony Kim 	if (!ts)
1013842ff286SAnthony Kim 		return -ENOMEM;
1014842ff286SAnthony Kim 
1015842ff286SAnthony Kim 	ts->client = client;
1016842ff286SAnthony Kim 	i2c_set_clientdata(client, ts);
1017842ff286SAnthony Kim 	mutex_init(&ts->dev_mutex);
1018842ff286SAnthony Kim 
1019842ff286SAnthony Kim 	ts->reg = devm_regmap_init_i2c(client, &hideep_regmap_config);
1020842ff286SAnthony Kim 	if (IS_ERR(ts->reg)) {
1021842ff286SAnthony Kim 		error = PTR_ERR(ts->reg);
1022842ff286SAnthony Kim 		dev_err(&client->dev,
1023842ff286SAnthony Kim 			"failed to initialize regmap: %d\n", error);
1024842ff286SAnthony Kim 		return error;
1025842ff286SAnthony Kim 	}
1026842ff286SAnthony Kim 
1027842ff286SAnthony Kim 	ts->vcc_vdd = devm_regulator_get(&client->dev, "vdd");
1028842ff286SAnthony Kim 	if (IS_ERR(ts->vcc_vdd))
1029842ff286SAnthony Kim 		return PTR_ERR(ts->vcc_vdd);
1030842ff286SAnthony Kim 
1031842ff286SAnthony Kim 	ts->vcc_vid = devm_regulator_get(&client->dev, "vid");
1032842ff286SAnthony Kim 	if (IS_ERR(ts->vcc_vid))
1033842ff286SAnthony Kim 		return PTR_ERR(ts->vcc_vid);
1034842ff286SAnthony Kim 
1035842ff286SAnthony Kim 	ts->reset_gpio = devm_gpiod_get_optional(&client->dev,
1036842ff286SAnthony Kim 						 "reset", GPIOD_OUT_HIGH);
1037842ff286SAnthony Kim 	if (IS_ERR(ts->reset_gpio))
1038842ff286SAnthony Kim 		return PTR_ERR(ts->reset_gpio);
1039842ff286SAnthony Kim 
1040842ff286SAnthony Kim 	error = hideep_power_on(ts);
1041842ff286SAnthony Kim 	if (error) {
1042842ff286SAnthony Kim 		dev_err(&client->dev, "power on failed: %d\n", error);
1043842ff286SAnthony Kim 		return error;
1044842ff286SAnthony Kim 	}
1045842ff286SAnthony Kim 
1046842ff286SAnthony Kim 	error = devm_add_action_or_reset(&client->dev, hideep_power_off, ts);
1047842ff286SAnthony Kim 	if (error)
1048842ff286SAnthony Kim 		return error;
1049842ff286SAnthony Kim 
1050842ff286SAnthony Kim 	error = hideep_load_dwz(ts);
1051842ff286SAnthony Kim 	if (error) {
1052842ff286SAnthony Kim 		dev_err(&client->dev, "failed to load dwz: %d", error);
1053842ff286SAnthony Kim 		return error;
1054842ff286SAnthony Kim 	}
1055842ff286SAnthony Kim 
1056842ff286SAnthony Kim 	error = hideep_init_input(ts);
1057842ff286SAnthony Kim 	if (error)
1058842ff286SAnthony Kim 		return error;
1059842ff286SAnthony Kim 
1060842ff286SAnthony Kim 	error = devm_request_threaded_irq(&client->dev, client->irq,
1061842ff286SAnthony Kim 					  NULL, hideep_irq, IRQF_ONESHOT,
1062842ff286SAnthony Kim 					  client->name, ts);
1063842ff286SAnthony Kim 	if (error) {
1064842ff286SAnthony Kim 		dev_err(&client->dev, "failed to request irq %d: %d\n",
1065842ff286SAnthony Kim 			client->irq, error);
1066842ff286SAnthony Kim 		return error;
1067842ff286SAnthony Kim 	}
1068842ff286SAnthony Kim 
1069842ff286SAnthony Kim 	error = devm_device_add_group(&client->dev, &hideep_ts_attr_group);
1070842ff286SAnthony Kim 	if (error) {
1071842ff286SAnthony Kim 		dev_err(&client->dev,
1072842ff286SAnthony Kim 			"failed to add sysfs attributes: %d\n", error);
1073842ff286SAnthony Kim 		return error;
1074842ff286SAnthony Kim 	}
1075842ff286SAnthony Kim 
1076842ff286SAnthony Kim 	return 0;
1077842ff286SAnthony Kim }
1078842ff286SAnthony Kim 
1079842ff286SAnthony Kim static const struct i2c_device_id hideep_i2c_id[] = {
1080842ff286SAnthony Kim 	{ HIDEEP_I2C_NAME, 0 },
1081842ff286SAnthony Kim 	{ }
1082842ff286SAnthony Kim };
1083842ff286SAnthony Kim MODULE_DEVICE_TABLE(i2c, hideep_i2c_id);
1084842ff286SAnthony Kim 
1085842ff286SAnthony Kim #ifdef CONFIG_ACPI
1086842ff286SAnthony Kim static const struct acpi_device_id hideep_acpi_id[] = {
1087842ff286SAnthony Kim 	{ "HIDP0001", 0 },
1088842ff286SAnthony Kim 	{ }
1089842ff286SAnthony Kim };
1090842ff286SAnthony Kim MODULE_DEVICE_TABLE(acpi, hideep_acpi_id);
1091842ff286SAnthony Kim #endif
1092842ff286SAnthony Kim 
1093842ff286SAnthony Kim #ifdef CONFIG_OF
1094842ff286SAnthony Kim static const struct of_device_id hideep_match_table[] = {
1095842ff286SAnthony Kim 	{ .compatible = "hideep,hideep-ts" },
1096842ff286SAnthony Kim 	{ }
1097842ff286SAnthony Kim };
1098842ff286SAnthony Kim MODULE_DEVICE_TABLE(of, hideep_match_table);
1099842ff286SAnthony Kim #endif
1100842ff286SAnthony Kim 
1101842ff286SAnthony Kim static struct i2c_driver hideep_driver = {
1102842ff286SAnthony Kim 	.driver = {
1103842ff286SAnthony Kim 		.name			= HIDEEP_I2C_NAME,
1104842ff286SAnthony Kim 		.of_match_table		= of_match_ptr(hideep_match_table),
1105842ff286SAnthony Kim 		.acpi_match_table	= ACPI_PTR(hideep_acpi_id),
1106842ff286SAnthony Kim 		.pm			= &hideep_pm_ops,
1107842ff286SAnthony Kim 	},
1108842ff286SAnthony Kim 	.id_table	= hideep_i2c_id,
1109842ff286SAnthony Kim 	.probe		= hideep_probe,
1110842ff286SAnthony Kim };
1111842ff286SAnthony Kim 
1112842ff286SAnthony Kim module_i2c_driver(hideep_driver);
1113842ff286SAnthony Kim 
1114842ff286SAnthony Kim MODULE_DESCRIPTION("Driver for HiDeep Touchscreen Controller");
1115842ff286SAnthony Kim MODULE_AUTHOR("anthony.kim@hideep.com");
1116842ff286SAnthony Kim MODULE_LICENSE("GPL v2");
1117