1 /*
2  * MELFAS MIP4 Touchscreen
3  *
4  * Copyright (C) 2016 MELFAS Inc.
5  *
6  * Author : Sangwon Jee <jeesw@melfas.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */
18 
19 #include <linux/acpi.h>
20 #include <linux/delay.h>
21 #include <linux/firmware.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/i2c.h>
24 #include <linux/input.h>
25 #include <linux/input/mt.h>
26 #include <linux/interrupt.h>
27 #include <linux/module.h>
28 #include <linux/of.h>
29 #include <linux/slab.h>
30 #include <asm/unaligned.h>
31 
32 #define MIP4_DEVICE_NAME	"mip4_ts"
33 
34 /*****************************************************************
35  * Protocol
36  * Version : MIP 4.0 Rev 5.4
37  *****************************************************************/
38 
39 /* Address */
40 #define MIP4_R0_BOOT				0x00
41 #define MIP4_R1_BOOT_MODE			0x01
42 #define MIP4_R1_BOOT_BUF_ADDR			0x10
43 #define MIP4_R1_BOOT_STATUS			0x20
44 #define MIP4_R1_BOOT_CMD			0x30
45 #define MIP4_R1_BOOT_TARGET_ADDR		0x40
46 #define MIP4_R1_BOOT_SIZE			0x44
47 
48 #define MIP4_R0_INFO				0x01
49 #define MIP4_R1_INFO_PRODUCT_NAME		0x00
50 #define MIP4_R1_INFO_RESOLUTION_X		0x10
51 #define MIP4_R1_INFO_RESOLUTION_Y		0x12
52 #define MIP4_R1_INFO_NODE_NUM_X			0x14
53 #define MIP4_R1_INFO_NODE_NUM_Y			0x15
54 #define MIP4_R1_INFO_KEY_NUM			0x16
55 #define MIP4_R1_INFO_PRESSURE_NUM		0x17
56 #define MIP4_R1_INFO_LENGTH_X			0x18
57 #define MIP4_R1_INFO_LENGTH_Y			0x1A
58 #define MIP4_R1_INFO_PPM_X			0x1C
59 #define MIP4_R1_INFO_PPM_Y			0x1D
60 #define MIP4_R1_INFO_VERSION_BOOT		0x20
61 #define MIP4_R1_INFO_VERSION_CORE		0x22
62 #define MIP4_R1_INFO_VERSION_APP		0x24
63 #define MIP4_R1_INFO_VERSION_PARAM		0x26
64 #define MIP4_R1_INFO_SECT_BOOT_START		0x30
65 #define MIP4_R1_INFO_SECT_BOOT_END		0x31
66 #define MIP4_R1_INFO_SECT_CORE_START		0x32
67 #define MIP4_R1_INFO_SECT_CORE_END		0x33
68 #define MIP4_R1_INFO_SECT_APP_START		0x34
69 #define MIP4_R1_INFO_SECT_APP_END		0x35
70 #define MIP4_R1_INFO_SECT_PARAM_START		0x36
71 #define MIP4_R1_INFO_SECT_PARAM_END		0x37
72 #define MIP4_R1_INFO_BUILD_DATE			0x40
73 #define MIP4_R1_INFO_BUILD_TIME			0x44
74 #define MIP4_R1_INFO_CHECKSUM_PRECALC		0x48
75 #define MIP4_R1_INFO_CHECKSUM_REALTIME		0x4A
76 #define MIP4_R1_INFO_PROTOCOL_NAME		0x50
77 #define MIP4_R1_INFO_PROTOCOL_VERSION		0x58
78 #define MIP4_R1_INFO_IC_ID			0x70
79 #define MIP4_R1_INFO_IC_NAME			0x71
80 #define MIP4_R1_INFO_IC_VENDOR_ID		0x75
81 #define MIP4_R1_INFO_IC_HW_CATEGORY		0x77
82 #define MIP4_R1_INFO_CONTACT_THD_SCR		0x78
83 #define MIP4_R1_INFO_CONTACT_THD_KEY		0x7A
84 #define MIP4_R1_INFO_PID				0x7C
85 #define MIP4_R1_INFO_VID				0x7E
86 #define MIP4_R1_INFO_SLAVE_ADDR			0x80
87 
88 #define MIP4_R0_EVENT				0x02
89 #define MIP4_R1_EVENT_SUPPORTED_FUNC		0x00
90 #define MIP4_R1_EVENT_FORMAT			0x04
91 #define MIP4_R1_EVENT_SIZE			0x06
92 #define MIP4_R1_EVENT_PACKET_INFO		0x10
93 #define MIP4_R1_EVENT_PACKET_DATA		0x11
94 
95 #define MIP4_R0_CTRL				0x06
96 #define MIP4_R1_CTRL_READY_STATUS		0x00
97 #define MIP4_R1_CTRL_EVENT_READY		0x01
98 #define MIP4_R1_CTRL_MODE			0x10
99 #define MIP4_R1_CTRL_EVENT_TRIGGER_TYPE		0x11
100 #define MIP4_R1_CTRL_RECALIBRATE		0x12
101 #define MIP4_R1_CTRL_POWER_STATE		0x13
102 #define MIP4_R1_CTRL_GESTURE_TYPE		0x14
103 #define MIP4_R1_CTRL_DISABLE_ESD_ALERT		0x18
104 #define MIP4_R1_CTRL_CHARGER_MODE		0x19
105 #define MIP4_R1_CTRL_HIGH_SENS_MODE		0x1A
106 #define MIP4_R1_CTRL_WINDOW_MODE		0x1B
107 #define MIP4_R1_CTRL_PALM_REJECTION		0x1C
108 #define MIP4_R1_CTRL_EDGE_CORRECTION		0x1D
109 #define MIP4_R1_CTRL_ENTER_GLOVE_MODE		0x1E
110 #define MIP4_R1_CTRL_I2C_ON_LPM			0x1F
111 #define MIP4_R1_CTRL_GESTURE_DEBUG		0x20
112 #define MIP4_R1_CTRL_PALM_EVENT			0x22
113 #define MIP4_R1_CTRL_PROXIMITY_SENSING		0x23
114 
115 /* Value */
116 #define MIP4_BOOT_MODE_BOOT			0x01
117 #define MIP4_BOOT_MODE_APP			0x02
118 
119 #define MIP4_BOOT_STATUS_BUSY			0x05
120 #define MIP4_BOOT_STATUS_ERROR			0x0E
121 #define MIP4_BOOT_STATUS_DONE			0xA0
122 
123 #define MIP4_BOOT_CMD_MASS_ERASE		0x15
124 #define MIP4_BOOT_CMD_PROGRAM			0x54
125 #define MIP4_BOOT_CMD_ERASE			0x8F
126 #define MIP4_BOOT_CMD_WRITE			0xA5
127 #define MIP4_BOOT_CMD_READ			0xC2
128 
129 #define MIP4_EVENT_INPUT_TYPE_KEY		0
130 #define MIP4_EVENT_INPUT_TYPE_SCREEN		1
131 #define MIP4_EVENT_INPUT_TYPE_PROXIMITY		2
132 
133 #define I2C_RETRY_COUNT				3	/* 2~ */
134 
135 #define MIP4_BUF_SIZE				128
136 #define MIP4_MAX_FINGERS			10
137 #define MIP4_MAX_KEYS				4
138 
139 #define MIP4_TOUCH_MAJOR_MIN			0
140 #define MIP4_TOUCH_MAJOR_MAX			255
141 #define MIP4_TOUCH_MINOR_MIN			0
142 #define MIP4_TOUCH_MINOR_MAX			255
143 #define MIP4_PRESSURE_MIN			0
144 #define MIP4_PRESSURE_MAX			255
145 
146 #define MIP4_FW_NAME			"melfas_mip4.fw"
147 #define MIP4_FW_UPDATE_DEBUG		0	/* 0 (default) or 1 */
148 
149 struct mip4_fw_version {
150 	u16 boot;
151 	u16 core;
152 	u16 app;
153 	u16 param;
154 };
155 
156 struct mip4_ts {
157 	struct i2c_client *client;
158 	struct input_dev *input;
159 	struct gpio_desc *gpio_ce;
160 
161 	char phys[32];
162 	char product_name[16];
163 	u16 product_id;
164 	char ic_name[4];
165 	char fw_name[32];
166 
167 	unsigned int max_x;
168 	unsigned int max_y;
169 	u8 node_x;
170 	u8 node_y;
171 	u8 node_key;
172 	unsigned int ppm_x;
173 	unsigned int ppm_y;
174 
175 	struct mip4_fw_version fw_version;
176 
177 	unsigned int event_size;
178 	unsigned int event_format;
179 
180 	unsigned int key_num;
181 	unsigned short key_code[MIP4_MAX_KEYS];
182 
183 	bool wake_irq_enabled;
184 
185 	u8 buf[MIP4_BUF_SIZE];
186 };
187 
188 static int mip4_i2c_xfer(struct mip4_ts *ts,
189 			 char *write_buf, unsigned int write_len,
190 			 char *read_buf, unsigned int read_len)
191 {
192 	struct i2c_msg msg[] = {
193 		{
194 			.addr = ts->client->addr,
195 			.flags = 0,
196 			.buf = write_buf,
197 			.len = write_len,
198 		}, {
199 			.addr = ts->client->addr,
200 			.flags = I2C_M_RD,
201 			.buf = read_buf,
202 			.len = read_len,
203 		},
204 	};
205 	int retry = I2C_RETRY_COUNT;
206 	int res;
207 	int error;
208 
209 	do {
210 		res = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
211 		if (res == ARRAY_SIZE(msg))
212 			return 0;
213 
214 		error = res < 0 ? res : -EIO;
215 		dev_err(&ts->client->dev,
216 			"%s - i2c_transfer failed: %d (%d)\n",
217 			__func__, error, res);
218 	} while (--retry);
219 
220 	return error;
221 }
222 
223 static void mip4_parse_fw_version(const u8 *buf, struct mip4_fw_version *v)
224 {
225 	v->boot  = get_unaligned_le16(buf + 0);
226 	v->core  = get_unaligned_le16(buf + 2);
227 	v->app   = get_unaligned_le16(buf + 4);
228 	v->param = get_unaligned_le16(buf + 6);
229 }
230 
231 /*
232  * Read chip firmware version
233  */
234 static int mip4_get_fw_version(struct mip4_ts *ts)
235 {
236 	u8 cmd[] = { MIP4_R0_INFO, MIP4_R1_INFO_VERSION_BOOT };
237 	u8 buf[sizeof(ts->fw_version)];
238 	int error;
239 
240 	error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, sizeof(buf));
241 	if (error) {
242 		memset(&ts->fw_version, 0xff, sizeof(ts->fw_version));
243 		return error;
244 	}
245 
246 	mip4_parse_fw_version(buf, &ts->fw_version);
247 
248 	return 0;
249 }
250 
251 /*
252  * Fetch device characteristics
253  */
254 static int mip4_query_device(struct mip4_ts *ts)
255 {
256 	int error;
257 	u8 cmd[2];
258 	u8 buf[14];
259 
260 	/* Product name */
261 	cmd[0] = MIP4_R0_INFO;
262 	cmd[1] = MIP4_R1_INFO_PRODUCT_NAME;
263 	error = mip4_i2c_xfer(ts, cmd, sizeof(cmd),
264 			      ts->product_name, sizeof(ts->product_name));
265 	if (error)
266 		dev_warn(&ts->client->dev,
267 			 "Failed to retrieve product name: %d\n", error);
268 	else
269 		dev_dbg(&ts->client->dev, "product name: %.*s\n",
270 			(int)sizeof(ts->product_name), ts->product_name);
271 
272 	/* Product ID */
273 	cmd[0] = MIP4_R0_INFO;
274 	cmd[1] = MIP4_R1_INFO_PID;
275 	error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 2);
276 	if (error) {
277 		dev_warn(&ts->client->dev,
278 			 "Failed to retrieve product id: %d\n", error);
279 	} else {
280 		ts->product_id = get_unaligned_le16(&buf[0]);
281 		dev_dbg(&ts->client->dev, "product id: %04X\n", ts->product_id);
282 	}
283 
284 	/* Firmware name */
285 	snprintf(ts->fw_name, sizeof(ts->fw_name),
286 		"melfas_mip4_%04X.fw", ts->product_id);
287 	dev_dbg(&ts->client->dev, "firmware name: %s\n", ts->fw_name);
288 
289 	/* IC name */
290 	cmd[0] = MIP4_R0_INFO;
291 	cmd[1] = MIP4_R1_INFO_IC_NAME;
292 	error = mip4_i2c_xfer(ts, cmd, sizeof(cmd),
293 			      ts->ic_name, sizeof(ts->ic_name));
294 	if (error)
295 		dev_warn(&ts->client->dev,
296 			 "Failed to retrieve IC name: %d\n", error);
297 	else
298 		dev_dbg(&ts->client->dev, "IC name: %.*s\n",
299 			(int)sizeof(ts->ic_name), ts->ic_name);
300 
301 	/* Firmware version */
302 	error = mip4_get_fw_version(ts);
303 	if (error)
304 		dev_warn(&ts->client->dev,
305 			"Failed to retrieve FW version: %d\n", error);
306 	else
307 		dev_dbg(&ts->client->dev, "F/W Version: %04X %04X %04X %04X\n",
308 			 ts->fw_version.boot, ts->fw_version.core,
309 			 ts->fw_version.app, ts->fw_version.param);
310 
311 	/* Resolution */
312 	cmd[0] = MIP4_R0_INFO;
313 	cmd[1] = MIP4_R1_INFO_RESOLUTION_X;
314 	error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 14);
315 	if (error) {
316 		dev_warn(&ts->client->dev,
317 			 "Failed to retrieve touchscreen parameters: %d\n",
318 			 error);
319 	} else {
320 		ts->max_x = get_unaligned_le16(&buf[0]);
321 		ts->max_y = get_unaligned_le16(&buf[2]);
322 		dev_dbg(&ts->client->dev, "max_x: %d, max_y: %d\n",
323 			ts->max_x, ts->max_y);
324 
325 		ts->node_x = buf[4];
326 		ts->node_y = buf[5];
327 		ts->node_key = buf[6];
328 		dev_dbg(&ts->client->dev,
329 			"node_x: %d, node_y: %d, node_key: %d\n",
330 			ts->node_x, ts->node_y, ts->node_key);
331 
332 		ts->ppm_x = buf[12];
333 		ts->ppm_y = buf[13];
334 		dev_dbg(&ts->client->dev, "ppm_x: %d, ppm_y: %d\n",
335 			ts->ppm_x, ts->ppm_y);
336 
337 		/* Key ts */
338 		if (ts->node_key > 0)
339 			ts->key_num = ts->node_key;
340 	}
341 
342 	/* Protocol */
343 	cmd[0] = MIP4_R0_EVENT;
344 	cmd[1] = MIP4_R1_EVENT_SUPPORTED_FUNC;
345 	error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 7);
346 	if (error) {
347 		dev_warn(&ts->client->dev,
348 			"Failed to retrieve device type: %d\n", error);
349 		ts->event_format = 0xff;
350 	} else {
351 		ts->event_format = get_unaligned_le16(&buf[4]);
352 		ts->event_size = buf[6];
353 		dev_dbg(&ts->client->dev, "event_format: %d, event_size: %d\n",
354 			ts->event_format, ts->event_size);
355 
356 		if (ts->event_format == 2 || ts->event_format > 3)
357 			dev_warn(&ts->client->dev,
358 				 "Unknown event format %d\n", ts->event_format);
359 	}
360 
361 	return 0;
362 }
363 
364 static int mip4_power_on(struct mip4_ts *ts)
365 {
366 	if (ts->gpio_ce) {
367 		gpiod_set_value_cansleep(ts->gpio_ce, 1);
368 
369 		/* Booting delay : 200~300ms */
370 		usleep_range(200 * 1000, 300 * 1000);
371 	}
372 
373 	return 0;
374 }
375 
376 static void mip4_power_off(struct mip4_ts *ts)
377 {
378 	if (ts->gpio_ce)
379 		gpiod_set_value_cansleep(ts->gpio_ce, 0);
380 }
381 
382 /*
383  * Clear touch input event status
384  */
385 static void mip4_clear_input(struct mip4_ts *ts)
386 {
387 	int i;
388 
389 	/* Screen */
390 	for (i = 0; i < MIP4_MAX_FINGERS; i++) {
391 		input_mt_slot(ts->input, i);
392 		input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 0);
393 	}
394 
395 	/* Keys */
396 	for (i = 0; i < ts->key_num; i++)
397 		input_report_key(ts->input, ts->key_code[i], 0);
398 
399 	input_sync(ts->input);
400 }
401 
402 static int mip4_enable(struct mip4_ts *ts)
403 {
404 	int error;
405 
406 	error = mip4_power_on(ts);
407 	if (error)
408 		return error;
409 
410 	enable_irq(ts->client->irq);
411 
412 	return 0;
413 }
414 
415 static void mip4_disable(struct mip4_ts *ts)
416 {
417 	disable_irq(ts->client->irq);
418 
419 	mip4_power_off(ts);
420 
421 	mip4_clear_input(ts);
422 }
423 
424 /*****************************************************************
425  * Input handling
426  *****************************************************************/
427 
428 static void mip4_report_keys(struct mip4_ts *ts, u8 *packet)
429 {
430 	u8 key;
431 	bool down;
432 
433 	switch (ts->event_format) {
434 	case 0:
435 	case 1:
436 		key = packet[0] & 0x0F;
437 		down = packet[0] & 0x80;
438 		break;
439 
440 	case 3:
441 	default:
442 		key = packet[0] & 0x0F;
443 		down = packet[1] & 0x01;
444 		break;
445 	}
446 
447 	/* Report key event */
448 	if (key >= 1 && key <= ts->key_num) {
449 		unsigned short keycode = ts->key_code[key - 1];
450 
451 		dev_dbg(&ts->client->dev,
452 			"Key - ID: %d, keycode: %d, state: %d\n",
453 			key, keycode, down);
454 
455 		input_event(ts->input, EV_MSC, MSC_SCAN, keycode);
456 		input_report_key(ts->input, keycode, down);
457 
458 	} else {
459 		dev_err(&ts->client->dev, "Unknown key: %d\n", key);
460 	}
461 }
462 
463 static void mip4_report_touch(struct mip4_ts *ts, u8 *packet)
464 {
465 	int id;
466 	bool hover;
467 	bool palm;
468 	bool state;
469 	u16 x, y;
470 	u8 pressure_stage = 0;
471 	u8 pressure;
472 	u8 size;
473 	u8 touch_major;
474 	u8 touch_minor;
475 
476 	switch (ts->event_format) {
477 	case 0:
478 	case 1:
479 		/* Touch only */
480 		state = packet[0] & BIT(7);
481 		hover = packet[0] & BIT(5);
482 		palm = packet[0] & BIT(4);
483 		id = (packet[0] & 0x0F) - 1;
484 		x = ((packet[1] & 0x0F) << 8) | packet[2];
485 		y = (((packet[1] >> 4) & 0x0F) << 8) |
486 			packet[3];
487 		pressure = packet[4];
488 		size = packet[5];
489 		if (ts->event_format == 0) {
490 			touch_major = packet[5];
491 			touch_minor = packet[5];
492 		} else {
493 			touch_major = packet[6];
494 			touch_minor = packet[7];
495 		}
496 		break;
497 
498 	case 3:
499 	default:
500 		/* Touch + Force(Pressure) */
501 		id = (packet[0] & 0x0F) - 1;
502 		hover = packet[1] & BIT(2);
503 		palm = packet[1] & BIT(1);
504 		state = packet[1] & BIT(0);
505 		x = ((packet[2] & 0x0F) << 8) | packet[3];
506 		y = (((packet[2] >> 4) & 0x0F) << 8) |
507 			packet[4];
508 		size = packet[6];
509 		pressure_stage = (packet[7] & 0xF0) >> 4;
510 		pressure = ((packet[7] & 0x0F) << 8) |
511 			packet[8];
512 		touch_major = packet[9];
513 		touch_minor = packet[10];
514 		break;
515 	}
516 
517 	dev_dbg(&ts->client->dev,
518 		"Screen - Slot: %d State: %d X: %04d Y: %04d Z: %d\n",
519 		id, state, x, y, pressure);
520 
521 	if (unlikely(id < 0 || id >= MIP4_MAX_FINGERS)) {
522 		dev_err(&ts->client->dev, "Screen - invalid slot ID: %d\n", id);
523 	} else if (state) {
524 		/* Press or Move event */
525 		input_mt_slot(ts->input, id);
526 		input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
527 		input_report_abs(ts->input, ABS_MT_POSITION_X, x);
528 		input_report_abs(ts->input, ABS_MT_POSITION_Y, y);
529 		input_report_abs(ts->input, ABS_MT_PRESSURE, pressure);
530 		input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, touch_major);
531 		input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, touch_minor);
532 	} else {
533 		/* Release event */
534 		input_mt_slot(ts->input, id);
535 		input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 0);
536 	}
537 
538 	input_mt_sync_frame(ts->input);
539 }
540 
541 static int mip4_handle_packet(struct mip4_ts *ts, u8 *packet)
542 {
543 	u8 type;
544 
545 	switch (ts->event_format) {
546 	case 0:
547 	case 1:
548 		type = (packet[0] & 0x40) >> 6;
549 		break;
550 
551 	case 3:
552 		type = (packet[0] & 0xF0) >> 4;
553 		break;
554 
555 	default:
556 		/* Should not happen unless we have corrupted firmware */
557 		return -EINVAL;
558 	}
559 
560 	dev_dbg(&ts->client->dev, "Type: %d\n", type);
561 
562 	/* Report input event */
563 	switch (type) {
564 	case MIP4_EVENT_INPUT_TYPE_KEY:
565 		mip4_report_keys(ts, packet);
566 		break;
567 
568 	case MIP4_EVENT_INPUT_TYPE_SCREEN:
569 		mip4_report_touch(ts, packet);
570 		break;
571 
572 	default:
573 		dev_err(&ts->client->dev, "Unknown event type: %d\n", type);
574 		break;
575 	}
576 
577 	return 0;
578 }
579 
580 static irqreturn_t mip4_interrupt(int irq, void *dev_id)
581 {
582 	struct mip4_ts *ts = dev_id;
583 	struct i2c_client *client = ts->client;
584 	unsigned int i;
585 	int error;
586 	u8 cmd[2];
587 	u8 size;
588 	bool alert;
589 
590 	/* Read packet info */
591 	cmd[0] = MIP4_R0_EVENT;
592 	cmd[1] = MIP4_R1_EVENT_PACKET_INFO;
593 	error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), ts->buf, 1);
594 	if (error) {
595 		dev_err(&client->dev,
596 			"Failed to read packet info: %d\n", error);
597 		goto out;
598 	}
599 
600 	size = ts->buf[0] & 0x7F;
601 	alert = ts->buf[0] & BIT(7);
602 	dev_dbg(&client->dev, "packet size: %d, alert: %d\n", size, alert);
603 
604 	/* Check size */
605 	if (!size) {
606 		dev_err(&client->dev, "Empty packet\n");
607 		goto out;
608 	}
609 
610 	/* Read packet data */
611 	cmd[0] = MIP4_R0_EVENT;
612 	cmd[1] = MIP4_R1_EVENT_PACKET_DATA;
613 	error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), ts->buf, size);
614 	if (error) {
615 		dev_err(&client->dev,
616 			"Failed to read packet data: %d\n", error);
617 		goto out;
618 	}
619 
620 	if (alert) {
621 		dev_dbg(&client->dev, "Alert: %d\n", ts->buf[0]);
622 	} else {
623 		for (i = 0; i < size; i += ts->event_size) {
624 			error = mip4_handle_packet(ts, &ts->buf[i]);
625 			if (error)
626 				break;
627 		}
628 
629 		input_sync(ts->input);
630 	}
631 
632 out:
633 	return IRQ_HANDLED;
634 }
635 
636 static int mip4_input_open(struct input_dev *dev)
637 {
638 	struct mip4_ts *ts = input_get_drvdata(dev);
639 
640 	return mip4_enable(ts);
641 }
642 
643 static void mip4_input_close(struct input_dev *dev)
644 {
645 	struct mip4_ts *ts = input_get_drvdata(dev);
646 
647 	mip4_disable(ts);
648 }
649 
650 /*****************************************************************
651  * Firmware update
652  *****************************************************************/
653 
654 /* Firmware Info */
655 #define MIP4_BL_PAGE_SIZE		512	/* 512 */
656 #define MIP4_BL_PACKET_SIZE		512	/* 512, 256, 128, 64, ... */
657 
658 /*
659  * Firmware binary tail info
660  */
661 
662 struct mip4_bin_tail {
663 	u8 tail_mark[4];
664 	u8 chip_name[4];
665 
666 	__le32 bin_start_addr;
667 	__le32 bin_length;
668 
669 	__le16 ver_boot;
670 	__le16 ver_core;
671 	__le16 ver_app;
672 	__le16 ver_param;
673 
674 	u8 boot_start;
675 	u8 boot_end;
676 	u8 core_start;
677 	u8 core_end;
678 	u8 app_start;
679 	u8 app_end;
680 	u8 param_start;
681 	u8 param_end;
682 
683 	u8 checksum_type;
684 	u8 hw_category;
685 
686 	__le16 param_id;
687 	__le32 param_length;
688 	__le32 build_date;
689 	__le32 build_time;
690 
691 	__le32 reserved1;
692 	__le32 reserved2;
693 	__le16 reserved3;
694 	__le16 tail_size;
695 	__le32 crc;
696 } __packed;
697 
698 #define MIP4_BIN_TAIL_MARK	"MBT\001"
699 #define MIP4_BIN_TAIL_SIZE	(sizeof(struct mip4_bin_tail))
700 
701 /*
702 * Bootloader - Read status
703 */
704 static int mip4_bl_read_status(struct mip4_ts *ts)
705 {
706 	u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_STATUS };
707 	u8 result;
708 	struct i2c_msg msg[] = {
709 		{
710 			.addr = ts->client->addr,
711 			.flags = 0,
712 			.buf = cmd,
713 			.len = sizeof(cmd),
714 		}, {
715 			.addr = ts->client->addr,
716 			.flags = I2C_M_RD,
717 			.buf = &result,
718 			.len = sizeof(result),
719 		},
720 	};
721 	int ret;
722 	int error;
723 	int retry = 1000;
724 
725 	do {
726 		ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
727 		if (ret != ARRAY_SIZE(msg)) {
728 			error = ret < 0 ? ret : -EIO;
729 			dev_err(&ts->client->dev,
730 				"Failed to read bootloader status: %d\n",
731 				error);
732 			return error;
733 		}
734 
735 		switch (result) {
736 		case MIP4_BOOT_STATUS_DONE:
737 			dev_dbg(&ts->client->dev, "%s - done\n", __func__);
738 			return 0;
739 
740 		case MIP4_BOOT_STATUS_ERROR:
741 			dev_err(&ts->client->dev, "Bootloader failure\n");
742 			return -EIO;
743 
744 		case MIP4_BOOT_STATUS_BUSY:
745 			dev_dbg(&ts->client->dev, "%s - Busy\n", __func__);
746 			error = -EBUSY;
747 			break;
748 
749 		default:
750 			dev_err(&ts->client->dev,
751 				"Unexpected bootloader status: %#02x\n",
752 				result);
753 			error = -EINVAL;
754 			break;
755 		}
756 
757 		usleep_range(1000, 2000);
758 	} while (--retry);
759 
760 	return error;
761 }
762 
763 /*
764 * Bootloader - Change mode
765 */
766 static int mip4_bl_change_mode(struct mip4_ts *ts, u8 mode)
767 {
768 	u8 mode_chg_cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_MODE, mode };
769 	u8 mode_read_cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_MODE };
770 	u8 result;
771 	struct i2c_msg msg[] = {
772 		{
773 			.addr = ts->client->addr,
774 			.flags = 0,
775 			.buf = mode_read_cmd,
776 			.len = sizeof(mode_read_cmd),
777 		}, {
778 			.addr = ts->client->addr,
779 			.flags = I2C_M_RD,
780 			.buf = &result,
781 			.len = sizeof(result),
782 		},
783 	};
784 	int retry = 10;
785 	int ret;
786 	int error;
787 
788 	do {
789 		/* Send mode change command */
790 		ret = i2c_master_send(ts->client,
791 				      mode_chg_cmd, sizeof(mode_chg_cmd));
792 		if (ret != sizeof(mode_chg_cmd)) {
793 			error = ret < 0 ? ret : -EIO;
794 			dev_err(&ts->client->dev,
795 				"Failed to send %d mode change: %d (%d)\n",
796 				mode, error, ret);
797 			return error;
798 		}
799 
800 		dev_dbg(&ts->client->dev,
801 			"Sent mode change request (mode: %d)\n", mode);
802 
803 		/* Wait */
804 		msleep(1000);
805 
806 		/* Verify target mode */
807 		ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
808 		if (ret != ARRAY_SIZE(msg)) {
809 			error = ret < 0 ? ret : -EIO;
810 			dev_err(&ts->client->dev,
811 				"Failed to read device mode: %d\n", error);
812 			return error;
813 		}
814 
815 		dev_dbg(&ts->client->dev,
816 			"Current device mode: %d, want: %d\n", result, mode);
817 
818 		if (result == mode)
819 			return 0;
820 
821 	} while (--retry);
822 
823 	return -EIO;
824 }
825 
826 /*
827  * Bootloader - Start bootloader mode
828  */
829 static int mip4_bl_enter(struct mip4_ts *ts)
830 {
831 	return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_BOOT);
832 }
833 
834 /*
835  * Bootloader - Exit bootloader mode
836  */
837 static int mip4_bl_exit(struct mip4_ts *ts)
838 {
839 	return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_APP);
840 }
841 
842 static int mip4_bl_get_address(struct mip4_ts *ts, u16 *buf_addr)
843 {
844 	u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_BUF_ADDR };
845 	u8 result[sizeof(u16)];
846 	struct i2c_msg msg[] = {
847 		{
848 			.addr = ts->client->addr,
849 			.flags = 0,
850 			.buf = cmd,
851 			.len = sizeof(cmd),
852 		}, {
853 			.addr = ts->client->addr,
854 			.flags = I2C_M_RD,
855 			.buf = result,
856 			.len = sizeof(result),
857 		},
858 	};
859 	int ret;
860 	int error;
861 
862 	ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
863 	if (ret != ARRAY_SIZE(msg)) {
864 		error = ret < 0 ? ret : -EIO;
865 		dev_err(&ts->client->dev,
866 			"Failed to retrieve bootloader buffer address: %d\n",
867 			error);
868 		return error;
869 	}
870 
871 	*buf_addr = get_unaligned_le16(result);
872 	dev_dbg(&ts->client->dev,
873 		"Bootloader buffer address %#04x\n", *buf_addr);
874 
875 	return 0;
876 }
877 
878 static int mip4_bl_program_page(struct mip4_ts *ts, int offset,
879 				const u8 *data, int length, u16 buf_addr)
880 {
881 	u8 cmd[6];
882 	u8 *data_buf;
883 	u16 buf_offset;
884 	int ret;
885 	int error;
886 
887 	dev_dbg(&ts->client->dev, "Writing page @%#06x (%d)\n",
888 		offset, length);
889 
890 	if (length > MIP4_BL_PAGE_SIZE || length % MIP4_BL_PACKET_SIZE) {
891 		dev_err(&ts->client->dev,
892 			"Invalid page length: %d\n", length);
893 		return -EINVAL;
894 	}
895 
896 	data_buf = kmalloc(2 + MIP4_BL_PACKET_SIZE, GFP_KERNEL);
897 	if (!data_buf)
898 		return -ENOMEM;
899 
900 	/* Addr */
901 	cmd[0] = MIP4_R0_BOOT;
902 	cmd[1] = MIP4_R1_BOOT_TARGET_ADDR;
903 	put_unaligned_le32(offset, &cmd[2]);
904 	ret = i2c_master_send(ts->client, cmd, 6);
905 	if (ret != 6) {
906 		error = ret < 0 ? ret : -EIO;
907 		dev_err(&ts->client->dev,
908 			"Failed to send write page address: %d\n", error);
909 		goto out;
910 	}
911 
912 	/* Size */
913 	cmd[0] = MIP4_R0_BOOT;
914 	cmd[1] = MIP4_R1_BOOT_SIZE;
915 	put_unaligned_le32(length, &cmd[2]);
916 	ret = i2c_master_send(ts->client, cmd, 6);
917 	if (ret != 6) {
918 		error = ret < 0 ? ret : -EIO;
919 		dev_err(&ts->client->dev,
920 			"Failed to send write page size: %d\n", error);
921 		goto out;
922 	}
923 
924 	/* Data */
925 	for (buf_offset = 0;
926 	     buf_offset < length;
927 	     buf_offset += MIP4_BL_PACKET_SIZE) {
928 		dev_dbg(&ts->client->dev,
929 			"writing chunk at %#04x (size %d)\n",
930 			buf_offset, MIP4_BL_PACKET_SIZE);
931 		put_unaligned_be16(buf_addr + buf_offset, data_buf);
932 		memcpy(&data_buf[2], &data[buf_offset], MIP4_BL_PACKET_SIZE);
933 		ret = i2c_master_send(ts->client,
934 				      data_buf, 2 + MIP4_BL_PACKET_SIZE);
935 		if (ret != 2 + MIP4_BL_PACKET_SIZE) {
936 			error = ret < 0 ? ret : -EIO;
937 			dev_err(&ts->client->dev,
938 				"Failed to read chunk at %#04x (size %d): %d\n",
939 				buf_offset, MIP4_BL_PACKET_SIZE, error);
940 			goto out;
941 		}
942 	}
943 
944 	/* Command */
945 	cmd[0] = MIP4_R0_BOOT;
946 	cmd[1] = MIP4_R1_BOOT_CMD;
947 	cmd[2] = MIP4_BOOT_CMD_PROGRAM;
948 	ret = i2c_master_send(ts->client, cmd, 3);
949 	if (ret != 3) {
950 		error = ret < 0 ? ret : -EIO;
951 		dev_err(&ts->client->dev,
952 			"Failed to send 'write' command: %d\n", error);
953 		goto out;
954 	}
955 
956 	/* Status */
957 	error = mip4_bl_read_status(ts);
958 
959 out:
960 	kfree(data_buf);
961 	return error ? error : 0;
962 }
963 
964 static int mip4_bl_verify_page(struct mip4_ts *ts, int offset,
965 			       const u8 *data, int length, int buf_addr)
966 {
967 	u8 cmd[8];
968 	u8 *read_buf;
969 	int buf_offset;
970 	struct i2c_msg msg[] = {
971 		{
972 			.addr = ts->client->addr,
973 			.flags = 0,
974 			.buf = cmd,
975 			.len = 2,
976 		}, {
977 			.addr = ts->client->addr,
978 			.flags = I2C_M_RD,
979 			.len = MIP4_BL_PACKET_SIZE,
980 		},
981 	};
982 	int ret;
983 	int error;
984 
985 	dev_dbg(&ts->client->dev, "Validating page @%#06x (%d)\n",
986 		offset, length);
987 
988 	/* Addr */
989 	cmd[0] = MIP4_R0_BOOT;
990 	cmd[1] = MIP4_R1_BOOT_TARGET_ADDR;
991 	put_unaligned_le32(offset, &cmd[2]);
992 	ret = i2c_master_send(ts->client, cmd, 6);
993 	if (ret != 6) {
994 		error = ret < 0 ? ret : -EIO;
995 		dev_err(&ts->client->dev,
996 			"Failed to send read page address: %d\n", error);
997 		return error;
998 	}
999 
1000 	/* Size */
1001 	cmd[0] = MIP4_R0_BOOT;
1002 	cmd[1] = MIP4_R1_BOOT_SIZE;
1003 	put_unaligned_le32(length, &cmd[2]);
1004 	ret = i2c_master_send(ts->client, cmd, 6);
1005 	if (ret != 6) {
1006 		error = ret < 0 ? ret : -EIO;
1007 		dev_err(&ts->client->dev,
1008 			"Failed to send read page size: %d\n", error);
1009 		return error;
1010 	}
1011 
1012 	/* Command */
1013 	cmd[0] = MIP4_R0_BOOT;
1014 	cmd[1] = MIP4_R1_BOOT_CMD;
1015 	cmd[2] = MIP4_BOOT_CMD_READ;
1016 	ret = i2c_master_send(ts->client, cmd, 3);
1017 	if (ret != 3) {
1018 		error = ret < 0 ? ret : -EIO;
1019 		dev_err(&ts->client->dev,
1020 			"Failed to send 'read' command: %d\n", error);
1021 		return error;
1022 	}
1023 
1024 	/* Status */
1025 	error = mip4_bl_read_status(ts);
1026 	if (error)
1027 		return error;
1028 
1029 	/* Read */
1030 	msg[1].buf = read_buf = kmalloc(MIP4_BL_PACKET_SIZE, GFP_KERNEL);
1031 	if (!read_buf)
1032 		return -ENOMEM;
1033 
1034 	for (buf_offset = 0;
1035 	     buf_offset < length;
1036 	     buf_offset += MIP4_BL_PACKET_SIZE) {
1037 		dev_dbg(&ts->client->dev,
1038 			"reading chunk at %#04x (size %d)\n",
1039 			buf_offset, MIP4_BL_PACKET_SIZE);
1040 		put_unaligned_be16(buf_addr + buf_offset, cmd);
1041 		ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
1042 		if (ret != ARRAY_SIZE(msg)) {
1043 			error = ret < 0 ? ret : -EIO;
1044 			dev_err(&ts->client->dev,
1045 				"Failed to read chunk at %#04x (size %d): %d\n",
1046 				buf_offset, MIP4_BL_PACKET_SIZE, error);
1047 			break;
1048 		}
1049 
1050 		if (memcmp(&data[buf_offset], read_buf, MIP4_BL_PACKET_SIZE)) {
1051 			dev_err(&ts->client->dev,
1052 				"Failed to validate chunk at %#04x (size %d)\n",
1053 				buf_offset, MIP4_BL_PACKET_SIZE);
1054 #if MIP4_FW_UPDATE_DEBUG
1055 			print_hex_dump(KERN_DEBUG,
1056 				       MIP4_DEVICE_NAME " F/W File: ",
1057 				       DUMP_PREFIX_OFFSET, 16, 1,
1058 				       data + offset, MIP4_BL_PACKET_SIZE,
1059 				       false);
1060 			print_hex_dump(KERN_DEBUG,
1061 				       MIP4_DEVICE_NAME " F/W Chip: ",
1062 				       DUMP_PREFIX_OFFSET, 16, 1,
1063 				       read_buf, MIP4_BL_PAGE_SIZE, false);
1064 #endif
1065 			error = -EINVAL;
1066 			break;
1067 		}
1068 	}
1069 
1070 	kfree(read_buf);
1071 	return error ? error : 0;
1072 }
1073 
1074 /*
1075  * Flash chip firmware
1076  */
1077 static int mip4_flash_fw(struct mip4_ts *ts,
1078 			 const u8 *fw_data, u32 fw_size, u32 fw_offset)
1079 {
1080 	struct i2c_client *client = ts->client;
1081 	int offset;
1082 	u16 buf_addr;
1083 	int error, error2;
1084 
1085 	/* Enter bootloader mode */
1086 	dev_dbg(&client->dev, "Entering bootloader mode\n");
1087 
1088 	error = mip4_bl_enter(ts);
1089 	if (error) {
1090 		dev_err(&client->dev,
1091 			"Failed to enter bootloader mode: %d\n",
1092 			error);
1093 		return error;
1094 	}
1095 
1096 	/* Read info */
1097 	error = mip4_bl_get_address(ts, &buf_addr);
1098 	if (error)
1099 		goto exit_bl;
1100 
1101 	/* Program & Verify */
1102 	dev_dbg(&client->dev,
1103 		"Program & Verify, page size: %d, packet size: %d\n",
1104 		MIP4_BL_PAGE_SIZE, MIP4_BL_PACKET_SIZE);
1105 
1106 	for (offset = fw_offset;
1107 	     offset < fw_offset + fw_size;
1108 	     offset += MIP4_BL_PAGE_SIZE) {
1109 		/* Program */
1110 		error = mip4_bl_program_page(ts, offset, fw_data + offset,
1111 					     MIP4_BL_PAGE_SIZE, buf_addr);
1112 		if (error)
1113 			break;
1114 
1115 		/* Verify */
1116 		error = mip4_bl_verify_page(ts, offset, fw_data + offset,
1117 					    MIP4_BL_PAGE_SIZE, buf_addr);
1118 		if (error)
1119 			break;
1120 	}
1121 
1122 exit_bl:
1123 	/* Exit bootloader mode */
1124 	dev_dbg(&client->dev, "Exiting bootloader mode\n");
1125 
1126 	error2 = mip4_bl_exit(ts);
1127 	if (error2) {
1128 		dev_err(&client->dev,
1129 			"Failed to exit bootloader mode: %d\n", error2);
1130 		if (!error)
1131 			error = error2;
1132 	}
1133 
1134 	/* Reset chip */
1135 	mip4_power_off(ts);
1136 	mip4_power_on(ts);
1137 
1138 	mip4_query_device(ts);
1139 
1140 	/* Refresh device parameters */
1141 	input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0);
1142 	input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0);
1143 	input_set_abs_params(ts->input, ABS_X, 0, ts->max_x, 0, 0);
1144 	input_set_abs_params(ts->input, ABS_Y, 0, ts->max_y, 0, 0);
1145 	input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->ppm_x);
1146 	input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->ppm_y);
1147 	input_abs_set_res(ts->input, ABS_X, ts->ppm_x);
1148 	input_abs_set_res(ts->input, ABS_Y, ts->ppm_y);
1149 
1150 	return error ? error : 0;
1151 }
1152 
1153 static int mip4_parse_firmware(struct mip4_ts *ts, const struct firmware *fw,
1154 			       u32 *fw_offset_start, u32 *fw_size,
1155 			       const struct mip4_bin_tail **pfw_info)
1156 {
1157 	const struct mip4_bin_tail *fw_info;
1158 	struct mip4_fw_version fw_version;
1159 	u16 tail_size;
1160 
1161 	if (fw->size < MIP4_BIN_TAIL_SIZE) {
1162 		dev_err(&ts->client->dev,
1163 			"Invalid firmware, size mismatch (tail %zd vs %zd)\n",
1164 			MIP4_BIN_TAIL_SIZE, fw->size);
1165 		return -EINVAL;
1166 	}
1167 
1168 	fw_info = (const void *)&fw->data[fw->size - MIP4_BIN_TAIL_SIZE];
1169 
1170 #if MIP4_FW_UPDATE_DEBUG
1171 	print_hex_dump(KERN_ERR, MIP4_DEVICE_NAME " Bin Info: ",
1172 		       DUMP_PREFIX_OFFSET, 16, 1, *fw_info, tail_size, false);
1173 #endif
1174 
1175 	tail_size = get_unaligned_le16(&fw_info->tail_size);
1176 	if (tail_size != MIP4_BIN_TAIL_SIZE) {
1177 		dev_err(&ts->client->dev,
1178 			"wrong tail size: %d (expected %zd)\n",
1179 			tail_size, MIP4_BIN_TAIL_SIZE);
1180 		return -EINVAL;
1181 	}
1182 
1183 	/* Check bin format */
1184 	if (memcmp(fw_info->tail_mark, MIP4_BIN_TAIL_MARK,
1185 		   sizeof(fw_info->tail_mark))) {
1186 		dev_err(&ts->client->dev,
1187 			"unable to locate tail marker (%*ph vs %*ph)\n",
1188 			(int)sizeof(fw_info->tail_mark), fw_info->tail_mark,
1189 			(int)sizeof(fw_info->tail_mark), MIP4_BIN_TAIL_MARK);
1190 		return -EINVAL;
1191 	}
1192 
1193 	*fw_offset_start = get_unaligned_le32(&fw_info->bin_start_addr);
1194 	*fw_size = get_unaligned_le32(&fw_info->bin_length);
1195 
1196 	dev_dbg(&ts->client->dev,
1197 		"F/W Data offset: %#08x, size: %d\n",
1198 		*fw_offset_start, *fw_size);
1199 
1200 	if (*fw_size % MIP4_BL_PAGE_SIZE) {
1201 		dev_err(&ts->client->dev,
1202 			"encoded fw length %d is not multiple of pages (%d)\n",
1203 			*fw_size, MIP4_BL_PAGE_SIZE);
1204 		return -EINVAL;
1205 	}
1206 
1207 	if (fw->size != *fw_offset_start + *fw_size) {
1208 		dev_err(&ts->client->dev,
1209 			"Wrong firmware size, expected %d bytes, got %zd\n",
1210 			*fw_offset_start + *fw_size, fw->size);
1211 		return -EINVAL;
1212 	}
1213 
1214 	mip4_parse_fw_version((const u8 *)&fw_info->ver_boot, &fw_version);
1215 
1216 	dev_dbg(&ts->client->dev,
1217 		"F/W file version %04X %04X %04X %04X\n",
1218 		fw_version.boot, fw_version.core,
1219 		fw_version.app, fw_version.param);
1220 
1221 	dev_dbg(&ts->client->dev, "F/W chip version: %04X %04X %04X %04X\n",
1222 		 ts->fw_version.boot, ts->fw_version.core,
1223 		 ts->fw_version.app, ts->fw_version.param);
1224 
1225 	/* Check F/W type */
1226 	if (fw_version.boot != 0xEEEE && fw_version.boot != 0xFFFF &&
1227 	    fw_version.core == 0xEEEE &&
1228 	    fw_version.app == 0xEEEE &&
1229 	    fw_version.param == 0xEEEE) {
1230 		dev_dbg(&ts->client->dev, "F/W type: Bootloader\n");
1231 	} else if (fw_version.boot == 0xEEEE &&
1232 		   fw_version.core != 0xEEEE && fw_version.core != 0xFFFF &&
1233 		   fw_version.app != 0xEEEE && fw_version.app != 0xFFFF &&
1234 		   fw_version.param != 0xEEEE && fw_version.param != 0xFFFF) {
1235 		dev_dbg(&ts->client->dev, "F/W type: Main\n");
1236 	} else {
1237 		dev_err(&ts->client->dev, "Wrong firmware type\n");
1238 		return -EINVAL;
1239 	}
1240 
1241 	return 0;
1242 }
1243 
1244 static int mip4_execute_fw_update(struct mip4_ts *ts, const struct firmware *fw)
1245 {
1246 	const struct mip4_bin_tail *fw_info;
1247 	u32 fw_start_offset;
1248 	u32 fw_size;
1249 	int retires = 3;
1250 	int error;
1251 
1252 	error = mip4_parse_firmware(ts, fw,
1253 				    &fw_start_offset, &fw_size, &fw_info);
1254 	if (error)
1255 		return error;
1256 
1257 	if (ts->input->users) {
1258 		disable_irq(ts->client->irq);
1259 	} else {
1260 		error = mip4_power_on(ts);
1261 		if (error)
1262 			return error;
1263 	}
1264 
1265 	/* Update firmware */
1266 	do {
1267 		error = mip4_flash_fw(ts, fw->data, fw_size, fw_start_offset);
1268 		if (!error)
1269 			break;
1270 	} while (--retires);
1271 
1272 	if (error)
1273 		dev_err(&ts->client->dev,
1274 			"Failed to flash firmware: %d\n", error);
1275 
1276 	/* Enable IRQ */
1277 	if (ts->input->users)
1278 		enable_irq(ts->client->irq);
1279 	else
1280 		mip4_power_off(ts);
1281 
1282 	return error ? error : 0;
1283 }
1284 
1285 static ssize_t mip4_sysfs_fw_update(struct device *dev,
1286 				    struct device_attribute *attr,
1287 				    const char *buf, size_t count)
1288 {
1289 	struct i2c_client *client = to_i2c_client(dev);
1290 	struct mip4_ts *ts = i2c_get_clientdata(client);
1291 	const struct firmware *fw;
1292 	int error;
1293 
1294 	error = request_firmware(&fw, ts->fw_name, dev);
1295 	if (error) {
1296 		dev_err(&ts->client->dev,
1297 			"Failed to retrieve firmware %s: %d\n",
1298 			ts->fw_name, error);
1299 		return error;
1300 	}
1301 
1302 	/*
1303 	 * Take input mutex to prevent racing with itself and also with
1304 	 * userspace opening and closing the device and also suspend/resume
1305 	 * transitions.
1306 	 */
1307 	mutex_lock(&ts->input->mutex);
1308 
1309 	error = mip4_execute_fw_update(ts, fw);
1310 
1311 	mutex_unlock(&ts->input->mutex);
1312 
1313 	release_firmware(fw);
1314 
1315 	if (error) {
1316 		dev_err(&ts->client->dev,
1317 			"Firmware update failed: %d\n", error);
1318 		return error;
1319 	}
1320 
1321 	return count;
1322 }
1323 
1324 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mip4_sysfs_fw_update);
1325 
1326 static ssize_t mip4_sysfs_read_fw_version(struct device *dev,
1327 					  struct device_attribute *attr,
1328 					  char *buf)
1329 {
1330 	struct i2c_client *client = to_i2c_client(dev);
1331 	struct mip4_ts *ts = i2c_get_clientdata(client);
1332 	size_t count;
1333 
1334 	/* Take lock to prevent racing with firmware update */
1335 	mutex_lock(&ts->input->mutex);
1336 
1337 	count = snprintf(buf, PAGE_SIZE, "%04X %04X %04X %04X\n",
1338 			 ts->fw_version.boot, ts->fw_version.core,
1339 			 ts->fw_version.app, ts->fw_version.param);
1340 
1341 	mutex_unlock(&ts->input->mutex);
1342 
1343 	return count;
1344 }
1345 
1346 static DEVICE_ATTR(fw_version, S_IRUGO, mip4_sysfs_read_fw_version, NULL);
1347 
1348 static ssize_t mip4_sysfs_read_hw_version(struct device *dev,
1349 					  struct device_attribute *attr,
1350 					  char *buf)
1351 {
1352 	struct i2c_client *client = to_i2c_client(dev);
1353 	struct mip4_ts *ts = i2c_get_clientdata(client);
1354 	size_t count;
1355 
1356 	/* Take lock to prevent racing with firmware update */
1357 	mutex_lock(&ts->input->mutex);
1358 
1359 	/*
1360 	 * product_name shows the name or version of the hardware
1361 	 * paired with current firmware in the chip.
1362 	 */
1363 	count = snprintf(buf, PAGE_SIZE, "%.*s\n",
1364 			 (int)sizeof(ts->product_name), ts->product_name);
1365 
1366 	mutex_unlock(&ts->input->mutex);
1367 
1368 	return count;
1369 }
1370 
1371 static DEVICE_ATTR(hw_version, S_IRUGO, mip4_sysfs_read_hw_version, NULL);
1372 
1373 static ssize_t mip4_sysfs_read_product_id(struct device *dev,
1374 					  struct device_attribute *attr,
1375 					  char *buf)
1376 {
1377 	struct i2c_client *client = to_i2c_client(dev);
1378 	struct mip4_ts *ts = i2c_get_clientdata(client);
1379 	size_t count;
1380 
1381 	mutex_lock(&ts->input->mutex);
1382 
1383 	count = snprintf(buf, PAGE_SIZE, "%04X\n", ts->product_id);
1384 
1385 	mutex_unlock(&ts->input->mutex);
1386 
1387 	return count;
1388 }
1389 
1390 static DEVICE_ATTR(product_id, S_IRUGO, mip4_sysfs_read_product_id, NULL);
1391 
1392 static ssize_t mip4_sysfs_read_ic_name(struct device *dev,
1393 					  struct device_attribute *attr,
1394 					  char *buf)
1395 {
1396 	struct i2c_client *client = to_i2c_client(dev);
1397 	struct mip4_ts *ts = i2c_get_clientdata(client);
1398 	size_t count;
1399 
1400 	mutex_lock(&ts->input->mutex);
1401 
1402 	count = snprintf(buf, PAGE_SIZE, "%.*s\n",
1403 			 (int)sizeof(ts->ic_name), ts->ic_name);
1404 
1405 	mutex_unlock(&ts->input->mutex);
1406 
1407 	return count;
1408 }
1409 
1410 static DEVICE_ATTR(ic_name, S_IRUGO, mip4_sysfs_read_ic_name, NULL);
1411 
1412 static struct attribute *mip4_attrs[] = {
1413 	&dev_attr_fw_version.attr,
1414 	&dev_attr_hw_version.attr,
1415 	&dev_attr_product_id.attr,
1416 	&dev_attr_ic_name.attr,
1417 	&dev_attr_update_fw.attr,
1418 	NULL,
1419 };
1420 
1421 static const struct attribute_group mip4_attr_group = {
1422 	.attrs = mip4_attrs,
1423 };
1424 
1425 static void mip4_sysfs_remove(void *_data)
1426 {
1427 	struct mip4_ts *ts = _data;
1428 
1429 	sysfs_remove_group(&ts->client->dev.kobj, &mip4_attr_group);
1430 }
1431 
1432 static int mip4_probe(struct i2c_client *client, const struct i2c_device_id *id)
1433 {
1434 	struct mip4_ts *ts;
1435 	struct input_dev *input;
1436 	int error;
1437 
1438 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1439 		dev_err(&client->dev, "Not supported I2C adapter\n");
1440 		return -ENXIO;
1441 	}
1442 
1443 	ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1444 	if (!ts)
1445 		return -ENOMEM;
1446 
1447 	input = devm_input_allocate_device(&client->dev);
1448 	if (!input)
1449 		return -ENOMEM;
1450 
1451 	ts->client = client;
1452 	ts->input = input;
1453 
1454 	snprintf(ts->phys, sizeof(ts->phys),
1455 		 "%s/input0", dev_name(&client->dev));
1456 
1457 	ts->gpio_ce = devm_gpiod_get_optional(&client->dev,
1458 					      "ce", GPIOD_OUT_LOW);
1459 	if (IS_ERR(ts->gpio_ce)) {
1460 		error = PTR_ERR(ts->gpio_ce);
1461 		if (error != EPROBE_DEFER)
1462 			dev_err(&client->dev,
1463 				"Failed to get gpio: %d\n", error);
1464 		return error;
1465 	}
1466 
1467 	error = mip4_power_on(ts);
1468 	if (error)
1469 		return error;
1470 	error = mip4_query_device(ts);
1471 	mip4_power_off(ts);
1472 	if (error)
1473 		return error;
1474 
1475 	input->name = "MELFAS MIP4 Touchscreen";
1476 	input->phys = ts->phys;
1477 
1478 	input->id.bustype = BUS_I2C;
1479 	input->id.vendor = 0x13c5;
1480 	input->id.product = ts->product_id;
1481 
1482 	input->open = mip4_input_open;
1483 	input->close = mip4_input_close;
1484 
1485 	input_set_drvdata(input, ts);
1486 
1487 	input->keycode = ts->key_code;
1488 	input->keycodesize = sizeof(*ts->key_code);
1489 	input->keycodemax = ts->key_num;
1490 
1491 	input_set_abs_params(input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0);
1492 	input_set_abs_params(input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0);
1493 	input_set_abs_params(input, ABS_MT_PRESSURE,
1494 			     MIP4_PRESSURE_MIN, MIP4_PRESSURE_MAX, 0, 0);
1495 	input_set_abs_params(input, ABS_MT_TOUCH_MAJOR,
1496 			     MIP4_TOUCH_MAJOR_MIN, MIP4_TOUCH_MAJOR_MAX, 0, 0);
1497 	input_set_abs_params(input, ABS_MT_TOUCH_MINOR,
1498 			     MIP4_TOUCH_MINOR_MIN, MIP4_TOUCH_MINOR_MAX, 0, 0);
1499 	input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->ppm_x);
1500 	input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->ppm_y);
1501 
1502 	error = input_mt_init_slots(input, MIP4_MAX_FINGERS, INPUT_MT_DIRECT);
1503 	if (error)
1504 		return error;
1505 
1506 	i2c_set_clientdata(client, ts);
1507 
1508 	error = devm_request_threaded_irq(&client->dev, client->irq,
1509 					  NULL, mip4_interrupt,
1510 					  IRQF_ONESHOT, MIP4_DEVICE_NAME, ts);
1511 	if (error) {
1512 		dev_err(&client->dev,
1513 			"Failed to request interrupt %d: %d\n",
1514 			client->irq, error);
1515 		return error;
1516 	}
1517 
1518 	disable_irq(client->irq);
1519 
1520 	error = input_register_device(input);
1521 	if (error) {
1522 		dev_err(&client->dev,
1523 			"Failed to register input device: %d\n", error);
1524 		return error;
1525 	}
1526 
1527 	error = sysfs_create_group(&client->dev.kobj, &mip4_attr_group);
1528 	if (error) {
1529 		dev_err(&client->dev,
1530 			"Failed to create sysfs attribute group: %d\n", error);
1531 		return error;
1532 	}
1533 
1534 	error = devm_add_action(&client->dev, mip4_sysfs_remove, ts);
1535 	if (error) {
1536 		mip4_sysfs_remove(ts);
1537 		dev_err(&client->dev,
1538 			"Failed to install sysfs remoce action: %d\n", error);
1539 		return error;
1540 	}
1541 
1542 	return 0;
1543 }
1544 
1545 static int __maybe_unused mip4_suspend(struct device *dev)
1546 {
1547 	struct i2c_client *client = to_i2c_client(dev);
1548 	struct mip4_ts *ts = i2c_get_clientdata(client);
1549 	struct input_dev *input = ts->input;
1550 
1551 	mutex_lock(&input->mutex);
1552 
1553 	if (device_may_wakeup(dev))
1554 		ts->wake_irq_enabled = enable_irq_wake(client->irq) == 0;
1555 	else if (input->users)
1556 		mip4_disable(ts);
1557 
1558 	mutex_unlock(&input->mutex);
1559 
1560 	return 0;
1561 }
1562 
1563 static int __maybe_unused mip4_resume(struct device *dev)
1564 {
1565 	struct i2c_client *client = to_i2c_client(dev);
1566 	struct mip4_ts *ts = i2c_get_clientdata(client);
1567 	struct input_dev *input = ts->input;
1568 
1569 	mutex_lock(&input->mutex);
1570 
1571 	if (ts->wake_irq_enabled)
1572 		disable_irq_wake(client->irq);
1573 	else if (input->users)
1574 		mip4_enable(ts);
1575 
1576 	mutex_unlock(&input->mutex);
1577 
1578 	return 0;
1579 }
1580 
1581 static SIMPLE_DEV_PM_OPS(mip4_pm_ops, mip4_suspend, mip4_resume);
1582 
1583 #ifdef CONFIG_OF
1584 static const struct of_device_id mip4_of_match[] = {
1585 	{ .compatible = "melfas,"MIP4_DEVICE_NAME, },
1586 	{ },
1587 };
1588 MODULE_DEVICE_TABLE(of, mip4_of_match);
1589 #endif
1590 
1591 #ifdef CONFIG_ACPI
1592 static const struct acpi_device_id mip4_acpi_match[] = {
1593 	{ "MLFS0000", 0},
1594 	{ },
1595 };
1596 MODULE_DEVICE_TABLE(acpi, mip4_acpi_match);
1597 #endif
1598 
1599 static const struct i2c_device_id mip4_i2c_ids[] = {
1600 	{ MIP4_DEVICE_NAME, 0 },
1601 	{ },
1602 };
1603 MODULE_DEVICE_TABLE(i2c, mip4_i2c_ids);
1604 
1605 static struct i2c_driver mip4_driver = {
1606 	.id_table = mip4_i2c_ids,
1607 	.probe = mip4_probe,
1608 	.driver = {
1609 		.name = MIP4_DEVICE_NAME,
1610 		.of_match_table = of_match_ptr(mip4_of_match),
1611 		.acpi_match_table = ACPI_PTR(mip4_acpi_match),
1612 		.pm = &mip4_pm_ops,
1613 	},
1614 };
1615 module_i2c_driver(mip4_driver);
1616 
1617 MODULE_DESCRIPTION("MELFAS MIP4 Touchscreen");
1618 MODULE_VERSION("2016.10.31");
1619 MODULE_AUTHOR("Sangwon Jee <jeesw@melfas.com>");
1620 MODULE_LICENSE("GPL");
1621