mmc: rtsx_pci: Enable MMC_CAP_ERASE to allow erase/discard/trim requests
[linux/fpc-iii.git] / drivers / input / touchscreen / melfas_mip4.c
blobfb5fb9140ca9536c59f0163ccd4cb5e8b8f4d9ac
1 /*
2 * MELFAS MIP4 Touchscreen
4 * Copyright (C) 2016 MELFAS Inc.
6 * Author : Sangwon Jee <jeesw@melfas.com>
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.
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.
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>
32 #define MIP4_DEVICE_NAME "mip4_ts"
34 /*****************************************************************
35 * Protocol
36 * Version : MIP 4.0 Rev 4.6
37 *****************************************************************/
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
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
85 #define MIP4_R0_EVENT 0x02
86 #define MIP4_R1_EVENT_SUPPORTED_FUNC 0x00
87 #define MIP4_R1_EVENT_FORMAT 0x04
88 #define MIP4_R1_EVENT_SIZE 0x06
89 #define MIP4_R1_EVENT_PACKET_INFO 0x10
90 #define MIP4_R1_EVENT_PACKET_DATA 0x11
92 #define MIP4_R0_CTRL 0x06
93 #define MIP4_R1_CTRL_READY_STATUS 0x00
94 #define MIP4_R1_CTRL_EVENT_READY 0x01
95 #define MIP4_R1_CTRL_MODE 0x10
96 #define MIP4_R1_CTRL_EVENT_TRIGGER_TYPE 0x11
97 #define MIP4_R1_CTRL_RECALIBRATE 0x12
98 #define MIP4_R1_CTRL_POWER_STATE 0x13
99 #define MIP4_R1_CTRL_GESTURE_TYPE 0x14
100 #define MIP4_R1_CTRL_DISABLE_ESD_ALERT 0x18
101 #define MIP4_R1_CTRL_CHARGER_MODE 0x19
102 #define MIP4_R1_CTRL_HIGH_SENS_MODE 0x1A
103 #define MIP4_R1_CTRL_WINDOW_MODE 0x1B
104 #define MIP4_R1_CTRL_PALM_REJECTION 0x1C
105 #define MIP4_R1_CTRL_EDGE_CORRECTION 0x1D
106 #define MIP4_R1_CTRL_ENTER_GLOVE_MODE 0x1E
107 #define MIP4_R1_CTRL_I2C_ON_LPM 0x1F
108 #define MIP4_R1_CTRL_GESTURE_DEBUG 0x20
109 #define MIP4_R1_CTRL_PALM_EVENT 0x22
110 #define MIP4_R1_CTRL_PROXIMITY_SENSING 0x23
112 /* Value */
113 #define MIP4_BOOT_MODE_BOOT 0x01
114 #define MIP4_BOOT_MODE_APP 0x02
116 #define MIP4_BOOT_STATUS_BUSY 0x05
117 #define MIP4_BOOT_STATUS_ERROR 0x0E
118 #define MIP4_BOOT_STATUS_DONE 0xA0
120 #define MIP4_BOOT_CMD_MASS_ERASE 0x15
121 #define MIP4_BOOT_CMD_PROGRAM 0x54
122 #define MIP4_BOOT_CMD_ERASE 0x8F
123 #define MIP4_BOOT_CMD_WRITE 0xA5
124 #define MIP4_BOOT_CMD_READ 0xC2
126 #define MIP4_EVENT_INPUT_TYPE_KEY 0
127 #define MIP4_EVENT_INPUT_TYPE_SCREEN 1
128 #define MIP4_EVENT_INPUT_TYPE_PROXIMITY 2
130 #define I2C_RETRY_COUNT 3 /* 2~ */
132 #define MIP4_BUF_SIZE 128
133 #define MIP4_MAX_FINGERS 10
134 #define MIP4_MAX_KEYS 4
136 #define MIP4_TOUCH_MAJOR_MIN 0
137 #define MIP4_TOUCH_MAJOR_MAX 255
138 #define MIP4_TOUCH_MINOR_MIN 0
139 #define MIP4_TOUCH_MINOR_MAX 255
140 #define MIP4_PRESSURE_MIN 0
141 #define MIP4_PRESSURE_MAX 255
143 #define MIP4_FW_NAME "melfas_mip4.fw"
144 #define MIP4_FW_UPDATE_DEBUG 0 /* 0 (default) or 1 */
146 struct mip4_fw_version {
147 u16 boot;
148 u16 core;
149 u16 app;
150 u16 param;
153 struct mip4_ts {
154 struct i2c_client *client;
155 struct input_dev *input;
156 struct gpio_desc *gpio_ce;
158 char phys[32];
159 char product_name[16];
161 unsigned int max_x;
162 unsigned int max_y;
163 u8 node_x;
164 u8 node_y;
165 u8 node_key;
166 unsigned int ppm_x;
167 unsigned int ppm_y;
169 struct mip4_fw_version fw_version;
171 unsigned int event_size;
172 unsigned int event_format;
174 unsigned int key_num;
175 unsigned short key_code[MIP4_MAX_KEYS];
177 bool wake_irq_enabled;
179 u8 buf[MIP4_BUF_SIZE];
182 static int mip4_i2c_xfer(struct mip4_ts *ts,
183 char *write_buf, unsigned int write_len,
184 char *read_buf, unsigned int read_len)
186 struct i2c_msg msg[] = {
188 .addr = ts->client->addr,
189 .flags = 0,
190 .buf = write_buf,
191 .len = write_len,
192 }, {
193 .addr = ts->client->addr,
194 .flags = I2C_M_RD,
195 .buf = read_buf,
196 .len = read_len,
199 int retry = I2C_RETRY_COUNT;
200 int res;
201 int error;
203 do {
204 res = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
205 if (res == ARRAY_SIZE(msg))
206 return 0;
208 error = res < 0 ? res : -EIO;
209 dev_err(&ts->client->dev,
210 "%s - i2c_transfer failed: %d (%d)\n",
211 __func__, error, res);
212 } while (--retry);
214 return error;
217 static void mip4_parse_fw_version(const u8 *buf, struct mip4_fw_version *v)
219 v->boot = get_unaligned_le16(buf + 0);
220 v->core = get_unaligned_le16(buf + 2);
221 v->app = get_unaligned_le16(buf + 4);
222 v->param = get_unaligned_le16(buf + 6);
226 * Read chip firmware version
228 static int mip4_get_fw_version(struct mip4_ts *ts)
230 u8 cmd[] = { MIP4_R0_INFO, MIP4_R1_INFO_VERSION_BOOT };
231 u8 buf[sizeof(ts->fw_version)];
232 int error;
234 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, sizeof(buf));
235 if (error) {
236 memset(&ts->fw_version, 0xff, sizeof(ts->fw_version));
237 return error;
240 mip4_parse_fw_version(buf, &ts->fw_version);
242 return 0;
246 * Fetch device characteristics
248 static int mip4_query_device(struct mip4_ts *ts)
250 int error;
251 u8 cmd[2];
252 u8 buf[14];
254 /* Product name */
255 cmd[0] = MIP4_R0_INFO;
256 cmd[1] = MIP4_R1_INFO_PRODUCT_NAME;
257 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd),
258 ts->product_name, sizeof(ts->product_name));
259 if (error)
260 dev_warn(&ts->client->dev,
261 "Failed to retrieve product name: %d\n", error);
262 else
263 dev_dbg(&ts->client->dev, "product name: %.*s\n",
264 (int)sizeof(ts->product_name), ts->product_name);
266 /* Firmware version */
267 error = mip4_get_fw_version(ts);
268 if (error)
269 dev_warn(&ts->client->dev,
270 "Failed to retrieve FW version: %d\n", error);
271 else
272 dev_dbg(&ts->client->dev, "F/W Version: %04X %04X %04X %04X\n",
273 ts->fw_version.boot, ts->fw_version.core,
274 ts->fw_version.app, ts->fw_version.param);
276 /* Resolution */
277 cmd[0] = MIP4_R0_INFO;
278 cmd[1] = MIP4_R1_INFO_RESOLUTION_X;
279 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 14);
280 if (error) {
281 dev_warn(&ts->client->dev,
282 "Failed to retrieve touchscreen parameters: %d\n",
283 error);
284 } else {
285 ts->max_x = get_unaligned_le16(&buf[0]);
286 ts->max_y = get_unaligned_le16(&buf[2]);
287 dev_dbg(&ts->client->dev, "max_x: %d, max_y: %d\n",
288 ts->max_x, ts->max_y);
290 ts->node_x = buf[4];
291 ts->node_y = buf[5];
292 ts->node_key = buf[6];
293 dev_dbg(&ts->client->dev,
294 "node_x: %d, node_y: %d, node_key: %d\n",
295 ts->node_x, ts->node_y, ts->node_key);
297 ts->ppm_x = buf[12];
298 ts->ppm_y = buf[13];
299 dev_dbg(&ts->client->dev, "ppm_x: %d, ppm_y: %d\n",
300 ts->ppm_x, ts->ppm_y);
302 /* Key ts */
303 if (ts->node_key > 0)
304 ts->key_num = ts->node_key;
307 /* Protocol */
308 cmd[0] = MIP4_R0_EVENT;
309 cmd[1] = MIP4_R1_EVENT_SUPPORTED_FUNC;
310 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 7);
311 if (error) {
312 dev_warn(&ts->client->dev,
313 "Failed to retrieve device type: %d\n", error);
314 ts->event_format = 0xff;
315 } else {
316 ts->event_format = get_unaligned_le16(&buf[4]);
317 ts->event_size = buf[6];
318 dev_dbg(&ts->client->dev, "event_format: %d, event_size: %d\n",
319 ts->event_format, ts->event_size);
321 if (ts->event_format == 2 || ts->event_format > 3)
322 dev_warn(&ts->client->dev,
323 "Unknown event format %d\n", ts->event_format);
326 return 0;
329 static int mip4_power_on(struct mip4_ts *ts)
331 if (ts->gpio_ce) {
332 gpiod_set_value_cansleep(ts->gpio_ce, 1);
334 /* Booting delay : 200~300ms */
335 usleep_range(200 * 1000, 300 * 1000);
338 return 0;
341 static void mip4_power_off(struct mip4_ts *ts)
343 if (ts->gpio_ce)
344 gpiod_set_value_cansleep(ts->gpio_ce, 0);
348 * Clear touch input event status
350 static void mip4_clear_input(struct mip4_ts *ts)
352 int i;
354 /* Screen */
355 for (i = 0; i < MIP4_MAX_FINGERS; i++) {
356 input_mt_slot(ts->input, i);
357 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 0);
360 /* Keys */
361 for (i = 0; i < ts->key_num; i++)
362 input_report_key(ts->input, ts->key_code[i], 0);
364 input_sync(ts->input);
367 static int mip4_enable(struct mip4_ts *ts)
369 int error;
371 error = mip4_power_on(ts);
372 if (error)
373 return error;
375 enable_irq(ts->client->irq);
377 return 0;
380 static void mip4_disable(struct mip4_ts *ts)
382 disable_irq(ts->client->irq);
384 mip4_power_off(ts);
386 mip4_clear_input(ts);
389 /*****************************************************************
390 * Input handling
391 *****************************************************************/
393 static void mip4_report_keys(struct mip4_ts *ts, u8 *packet)
395 u8 key;
396 bool down;
398 switch (ts->event_format) {
399 case 0:
400 case 1:
401 key = packet[0] & 0x0F;
402 down = packet[0] & 0x80;
403 break;
405 case 3:
406 default:
407 key = packet[0] & 0x0F;
408 down = packet[1] & 0x01;
409 break;
412 /* Report key event */
413 if (key >= 1 && key <= ts->key_num) {
414 unsigned short keycode = ts->key_code[key - 1];
416 dev_dbg(&ts->client->dev,
417 "Key - ID: %d, keycode: %d, state: %d\n",
418 key, keycode, down);
420 input_event(ts->input, EV_MSC, MSC_SCAN, keycode);
421 input_report_key(ts->input, keycode, down);
423 } else {
424 dev_err(&ts->client->dev, "Unknown key: %d\n", key);
428 static void mip4_report_touch(struct mip4_ts *ts, u8 *packet)
430 int id;
431 bool hover;
432 bool palm;
433 bool state;
434 u16 x, y;
435 u8 pressure_stage = 0;
436 u8 pressure;
437 u8 size;
438 u8 touch_major;
439 u8 touch_minor;
441 switch (ts->event_format) {
442 case 0:
443 case 1:
444 /* Touch only */
445 state = packet[0] & BIT(7);
446 hover = packet[0] & BIT(5);
447 palm = packet[0] & BIT(4);
448 id = (packet[0] & 0x0F) - 1;
449 x = ((packet[1] & 0x0F) << 8) | packet[2];
450 y = (((packet[1] >> 4) & 0x0F) << 8) |
451 packet[3];
452 pressure = packet[4];
453 size = packet[5];
454 if (ts->event_format == 0) {
455 touch_major = packet[5];
456 touch_minor = packet[5];
457 } else {
458 touch_major = packet[6];
459 touch_minor = packet[7];
461 break;
463 case 3:
464 default:
465 /* Touch + Force(Pressure) */
466 id = (packet[0] & 0x0F) - 1;
467 hover = packet[1] & BIT(2);
468 palm = packet[1] & BIT(1);
469 state = packet[1] & BIT(0);
470 x = ((packet[2] & 0x0F) << 8) | packet[3];
471 y = (((packet[2] >> 4) & 0x0F) << 8) |
472 packet[4];
473 size = packet[6];
474 pressure_stage = (packet[7] & 0xF0) >> 4;
475 pressure = ((packet[7] & 0x0F) << 8) |
476 packet[8];
477 touch_major = packet[9];
478 touch_minor = packet[10];
479 break;
482 dev_dbg(&ts->client->dev,
483 "Screen - Slot: %d State: %d X: %04d Y: %04d Z: %d\n",
484 id, state, x, y, pressure);
486 if (unlikely(id < 0 || id >= MIP4_MAX_FINGERS)) {
487 dev_err(&ts->client->dev, "Screen - invalid slot ID: %d\n", id);
488 } else if (state) {
489 /* Press or Move event */
490 input_mt_slot(ts->input, id);
491 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
492 input_report_abs(ts->input, ABS_MT_POSITION_X, x);
493 input_report_abs(ts->input, ABS_MT_POSITION_Y, y);
494 input_report_abs(ts->input, ABS_MT_PRESSURE, pressure);
495 input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, touch_major);
496 input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, touch_minor);
497 } else {
498 /* Release event */
499 input_mt_slot(ts->input, id);
500 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 0);
503 input_mt_sync_frame(ts->input);
506 static int mip4_handle_packet(struct mip4_ts *ts, u8 *packet)
508 u8 type;
510 switch (ts->event_format) {
511 case 0:
512 case 1:
513 type = (packet[0] & 0x40) >> 6;
514 break;
516 case 3:
517 type = (packet[0] & 0xF0) >> 4;
518 break;
520 default:
521 /* Should not happen unless we have corrupted firmware */
522 return -EINVAL;
525 dev_dbg(&ts->client->dev, "Type: %d\n", type);
527 /* Report input event */
528 switch (type) {
529 case MIP4_EVENT_INPUT_TYPE_KEY:
530 mip4_report_keys(ts, packet);
531 break;
533 case MIP4_EVENT_INPUT_TYPE_SCREEN:
534 mip4_report_touch(ts, packet);
535 break;
537 default:
538 dev_err(&ts->client->dev, "Unknown event type: %d\n", type);
539 break;
542 return 0;
545 static irqreturn_t mip4_interrupt(int irq, void *dev_id)
547 struct mip4_ts *ts = dev_id;
548 struct i2c_client *client = ts->client;
549 unsigned int i;
550 int error;
551 u8 cmd[2];
552 u8 size;
553 bool alert;
555 /* Read packet info */
556 cmd[0] = MIP4_R0_EVENT;
557 cmd[1] = MIP4_R1_EVENT_PACKET_INFO;
558 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), ts->buf, 1);
559 if (error) {
560 dev_err(&client->dev,
561 "Failed to read packet info: %d\n", error);
562 goto out;
565 size = ts->buf[0] & 0x7F;
566 alert = ts->buf[0] & BIT(7);
567 dev_dbg(&client->dev, "packet size: %d, alert: %d\n", size, alert);
569 /* Check size */
570 if (!size) {
571 dev_err(&client->dev, "Empty packet\n");
572 goto out;
575 /* Read packet data */
576 cmd[0] = MIP4_R0_EVENT;
577 cmd[1] = MIP4_R1_EVENT_PACKET_DATA;
578 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), ts->buf, size);
579 if (error) {
580 dev_err(&client->dev,
581 "Failed to read packet data: %d\n", error);
582 goto out;
585 if (alert) {
586 dev_dbg(&client->dev, "Alert: %d\n", ts->buf[0]);
587 } else {
588 for (i = 0; i < size; i += ts->event_size) {
589 error = mip4_handle_packet(ts, &ts->buf[i]);
590 if (error)
591 break;
594 input_sync(ts->input);
597 out:
598 return IRQ_HANDLED;
601 static int mip4_input_open(struct input_dev *dev)
603 struct mip4_ts *ts = input_get_drvdata(dev);
605 return mip4_enable(ts);
608 static void mip4_input_close(struct input_dev *dev)
610 struct mip4_ts *ts = input_get_drvdata(dev);
612 mip4_disable(ts);
615 /*****************************************************************
616 * Firmware update
617 *****************************************************************/
619 /* Firmware Info */
620 #define MIP4_BL_PAGE_SIZE 512 /* 512 */
621 #define MIP4_BL_PACKET_SIZE 512 /* 512, 256, 128, 64, ... */
624 * Firmware binary tail info
627 struct mip4_bin_tail {
628 u8 tail_mark[4];
629 u8 chip_name[4];
631 __le32 bin_start_addr;
632 __le32 bin_length;
634 __le16 ver_boot;
635 __le16 ver_core;
636 __le16 ver_app;
637 __le16 ver_param;
639 u8 boot_start;
640 u8 boot_end;
641 u8 core_start;
642 u8 core_end;
643 u8 app_start;
644 u8 app_end;
645 u8 param_start;
646 u8 param_end;
648 u8 checksum_type;
649 u8 hw_category;
651 __le16 param_id;
652 __le32 param_length;
653 __le32 build_date;
654 __le32 build_time;
656 __le32 reserved1;
657 __le32 reserved2;
658 __le16 reserved3;
659 __le16 tail_size;
660 __le32 crc;
661 } __packed;
663 #define MIP4_BIN_TAIL_MARK "MBT\001"
664 #define MIP4_BIN_TAIL_SIZE (sizeof(struct mip4_bin_tail))
667 * Bootloader - Read status
669 static int mip4_bl_read_status(struct mip4_ts *ts)
671 u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_STATUS };
672 u8 result;
673 struct i2c_msg msg[] = {
675 .addr = ts->client->addr,
676 .flags = 0,
677 .buf = cmd,
678 .len = sizeof(cmd),
679 }, {
680 .addr = ts->client->addr,
681 .flags = I2C_M_RD,
682 .buf = &result,
683 .len = sizeof(result),
686 int ret;
687 int error;
688 int retry = 1000;
690 do {
691 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
692 if (ret != ARRAY_SIZE(msg)) {
693 error = ret < 0 ? ret : -EIO;
694 dev_err(&ts->client->dev,
695 "Failed to read bootloader status: %d\n",
696 error);
697 return error;
700 switch (result) {
701 case MIP4_BOOT_STATUS_DONE:
702 dev_dbg(&ts->client->dev, "%s - done\n", __func__);
703 return 0;
705 case MIP4_BOOT_STATUS_ERROR:
706 dev_err(&ts->client->dev, "Bootloader failure\n");
707 return -EIO;
709 case MIP4_BOOT_STATUS_BUSY:
710 dev_dbg(&ts->client->dev, "%s - Busy\n", __func__);
711 error = -EBUSY;
712 break;
714 default:
715 dev_err(&ts->client->dev,
716 "Unexpected bootloader status: %#02x\n",
717 result);
718 error = -EINVAL;
719 break;
722 usleep_range(1000, 2000);
723 } while (--retry);
725 return error;
729 * Bootloader - Change mode
731 static int mip4_bl_change_mode(struct mip4_ts *ts, u8 mode)
733 u8 mode_chg_cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_MODE, mode };
734 u8 mode_read_cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_MODE };
735 u8 result;
736 struct i2c_msg msg[] = {
738 .addr = ts->client->addr,
739 .flags = 0,
740 .buf = mode_read_cmd,
741 .len = sizeof(mode_read_cmd),
742 }, {
743 .addr = ts->client->addr,
744 .flags = I2C_M_RD,
745 .buf = &result,
746 .len = sizeof(result),
749 int retry = 10;
750 int ret;
751 int error;
753 do {
754 /* Send mode change command */
755 ret = i2c_master_send(ts->client,
756 mode_chg_cmd, sizeof(mode_chg_cmd));
757 if (ret != sizeof(mode_chg_cmd)) {
758 error = ret < 0 ? ret : -EIO;
759 dev_err(&ts->client->dev,
760 "Failed to send %d mode change: %d (%d)\n",
761 mode, error, ret);
762 return error;
765 dev_dbg(&ts->client->dev,
766 "Sent mode change request (mode: %d)\n", mode);
768 /* Wait */
769 msleep(1000);
771 /* Verify target mode */
772 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
773 if (ret != ARRAY_SIZE(msg)) {
774 error = ret < 0 ? ret : -EIO;
775 dev_err(&ts->client->dev,
776 "Failed to read device mode: %d\n", error);
777 return error;
780 dev_dbg(&ts->client->dev,
781 "Current device mode: %d, want: %d\n", result, mode);
783 if (result == mode)
784 return 0;
786 } while (--retry);
788 return -EIO;
792 * Bootloader - Start bootloader mode
794 static int mip4_bl_enter(struct mip4_ts *ts)
796 return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_BOOT);
800 * Bootloader - Exit bootloader mode
802 static int mip4_bl_exit(struct mip4_ts *ts)
804 return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_APP);
807 static int mip4_bl_get_address(struct mip4_ts *ts, u16 *buf_addr)
809 u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_BUF_ADDR };
810 u8 result[sizeof(u16)];
811 struct i2c_msg msg[] = {
813 .addr = ts->client->addr,
814 .flags = 0,
815 .buf = cmd,
816 .len = sizeof(cmd),
817 }, {
818 .addr = ts->client->addr,
819 .flags = I2C_M_RD,
820 .buf = result,
821 .len = sizeof(result),
824 int ret;
825 int error;
827 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
828 if (ret != ARRAY_SIZE(msg)) {
829 error = ret < 0 ? ret : -EIO;
830 dev_err(&ts->client->dev,
831 "Failed to retrieve bootloader buffer address: %d\n",
832 error);
833 return error;
836 *buf_addr = get_unaligned_le16(result);
837 dev_dbg(&ts->client->dev,
838 "Bootloader buffer address %#04x\n", *buf_addr);
840 return 0;
843 static int mip4_bl_program_page(struct mip4_ts *ts, int offset,
844 const u8 *data, int length, u16 buf_addr)
846 u8 cmd[6];
847 u8 *data_buf;
848 u16 buf_offset;
849 int ret;
850 int error;
852 dev_dbg(&ts->client->dev, "Writing page @%#06x (%d)\n",
853 offset, length);
855 if (length > MIP4_BL_PAGE_SIZE || length % MIP4_BL_PACKET_SIZE) {
856 dev_err(&ts->client->dev,
857 "Invalid page length: %d\n", length);
858 return -EINVAL;
861 data_buf = kmalloc(2 + MIP4_BL_PACKET_SIZE, GFP_KERNEL);
862 if (!data_buf)
863 return -ENOMEM;
865 /* Addr */
866 cmd[0] = MIP4_R0_BOOT;
867 cmd[1] = MIP4_R1_BOOT_TARGET_ADDR;
868 put_unaligned_le32(offset, &cmd[2]);
869 ret = i2c_master_send(ts->client, cmd, 6);
870 if (ret != 6) {
871 error = ret < 0 ? ret : -EIO;
872 dev_err(&ts->client->dev,
873 "Failed to send write page address: %d\n", error);
874 goto out;
877 /* Size */
878 cmd[0] = MIP4_R0_BOOT;
879 cmd[1] = MIP4_R1_BOOT_SIZE;
880 put_unaligned_le32(length, &cmd[2]);
881 ret = i2c_master_send(ts->client, cmd, 6);
882 if (ret != 6) {
883 error = ret < 0 ? ret : -EIO;
884 dev_err(&ts->client->dev,
885 "Failed to send write page size: %d\n", error);
886 goto out;
889 /* Data */
890 for (buf_offset = 0;
891 buf_offset < length;
892 buf_offset += MIP4_BL_PACKET_SIZE) {
893 dev_dbg(&ts->client->dev,
894 "writing chunk at %#04x (size %d)\n",
895 buf_offset, MIP4_BL_PACKET_SIZE);
896 put_unaligned_be16(buf_addr + buf_offset, data_buf);
897 memcpy(&data_buf[2], &data[buf_offset], MIP4_BL_PACKET_SIZE);
898 ret = i2c_master_send(ts->client,
899 data_buf, 2 + MIP4_BL_PACKET_SIZE);
900 if (ret != 2 + MIP4_BL_PACKET_SIZE) {
901 error = ret < 0 ? ret : -EIO;
902 dev_err(&ts->client->dev,
903 "Failed to read chunk at %#04x (size %d): %d\n",
904 buf_offset, MIP4_BL_PACKET_SIZE, error);
905 goto out;
909 /* Command */
910 cmd[0] = MIP4_R0_BOOT;
911 cmd[1] = MIP4_R1_BOOT_CMD;
912 cmd[2] = MIP4_BOOT_CMD_PROGRAM;
913 ret = i2c_master_send(ts->client, cmd, 3);
914 if (ret != 3) {
915 error = ret < 0 ? ret : -EIO;
916 dev_err(&ts->client->dev,
917 "Failed to send 'write' command: %d\n", error);
918 goto out;
921 /* Status */
922 error = mip4_bl_read_status(ts);
924 out:
925 kfree(data_buf);
926 return error ? error : 0;
929 static int mip4_bl_verify_page(struct mip4_ts *ts, int offset,
930 const u8 *data, int length, int buf_addr)
932 u8 cmd[8];
933 u8 *read_buf;
934 int buf_offset;
935 struct i2c_msg msg[] = {
937 .addr = ts->client->addr,
938 .flags = 0,
939 .buf = cmd,
940 .len = 2,
941 }, {
942 .addr = ts->client->addr,
943 .flags = I2C_M_RD,
944 .len = MIP4_BL_PACKET_SIZE,
947 int ret;
948 int error;
950 dev_dbg(&ts->client->dev, "Validating page @%#06x (%d)\n",
951 offset, length);
953 /* Addr */
954 cmd[0] = MIP4_R0_BOOT;
955 cmd[1] = MIP4_R1_BOOT_TARGET_ADDR;
956 put_unaligned_le32(offset, &cmd[2]);
957 ret = i2c_master_send(ts->client, cmd, 6);
958 if (ret != 6) {
959 error = ret < 0 ? ret : -EIO;
960 dev_err(&ts->client->dev,
961 "Failed to send read page address: %d\n", error);
962 return error;
965 /* Size */
966 cmd[0] = MIP4_R0_BOOT;
967 cmd[1] = MIP4_R1_BOOT_SIZE;
968 put_unaligned_le32(length, &cmd[2]);
969 ret = i2c_master_send(ts->client, cmd, 6);
970 if (ret != 6) {
971 error = ret < 0 ? ret : -EIO;
972 dev_err(&ts->client->dev,
973 "Failed to send read page size: %d\n", error);
974 return error;
977 /* Command */
978 cmd[0] = MIP4_R0_BOOT;
979 cmd[1] = MIP4_R1_BOOT_CMD;
980 cmd[2] = MIP4_BOOT_CMD_READ;
981 ret = i2c_master_send(ts->client, cmd, 3);
982 if (ret != 3) {
983 error = ret < 0 ? ret : -EIO;
984 dev_err(&ts->client->dev,
985 "Failed to send 'read' command: %d\n", error);
986 return error;
989 /* Status */
990 error = mip4_bl_read_status(ts);
991 if (error)
992 return error;
994 /* Read */
995 msg[1].buf = read_buf = kmalloc(MIP4_BL_PACKET_SIZE, GFP_KERNEL);
996 if (!read_buf)
997 return -ENOMEM;
999 for (buf_offset = 0;
1000 buf_offset < length;
1001 buf_offset += MIP4_BL_PACKET_SIZE) {
1002 dev_dbg(&ts->client->dev,
1003 "reading chunk at %#04x (size %d)\n",
1004 buf_offset, MIP4_BL_PACKET_SIZE);
1005 put_unaligned_be16(buf_addr + buf_offset, cmd);
1006 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
1007 if (ret != ARRAY_SIZE(msg)) {
1008 error = ret < 0 ? ret : -EIO;
1009 dev_err(&ts->client->dev,
1010 "Failed to read chunk at %#04x (size %d): %d\n",
1011 buf_offset, MIP4_BL_PACKET_SIZE, error);
1012 break;
1015 if (memcmp(&data[buf_offset], read_buf, MIP4_BL_PACKET_SIZE)) {
1016 dev_err(&ts->client->dev,
1017 "Failed to validate chunk at %#04x (size %d)\n",
1018 buf_offset, MIP4_BL_PACKET_SIZE);
1019 #if MIP4_FW_UPDATE_DEBUG
1020 print_hex_dump(KERN_DEBUG,
1021 MIP4_DEVICE_NAME " F/W File: ",
1022 DUMP_PREFIX_OFFSET, 16, 1,
1023 data + offset, MIP4_BL_PACKET_SIZE,
1024 false);
1025 print_hex_dump(KERN_DEBUG,
1026 MIP4_DEVICE_NAME " F/W Chip: ",
1027 DUMP_PREFIX_OFFSET, 16, 1,
1028 read_buf, MIP4_BL_PAGE_SIZE, false);
1029 #endif
1030 error = -EINVAL;
1031 break;
1035 kfree(read_buf);
1036 return error ? error : 0;
1040 * Flash chip firmware
1042 static int mip4_flash_fw(struct mip4_ts *ts,
1043 const u8 *fw_data, u32 fw_size, u32 fw_offset)
1045 struct i2c_client *client = ts->client;
1046 int offset;
1047 u16 buf_addr;
1048 int error, error2;
1050 /* Enter bootloader mode */
1051 dev_dbg(&client->dev, "Entering bootloader mode\n");
1053 error = mip4_bl_enter(ts);
1054 if (error) {
1055 dev_err(&client->dev,
1056 "Failed to enter bootloader mode: %d\n",
1057 error);
1058 return error;
1061 /* Read info */
1062 error = mip4_bl_get_address(ts, &buf_addr);
1063 if (error)
1064 goto exit_bl;
1066 /* Program & Verify */
1067 dev_dbg(&client->dev,
1068 "Program & Verify, page size: %d, packet size: %d\n",
1069 MIP4_BL_PAGE_SIZE, MIP4_BL_PACKET_SIZE);
1071 for (offset = fw_offset;
1072 offset < fw_offset + fw_size;
1073 offset += MIP4_BL_PAGE_SIZE) {
1074 /* Program */
1075 error = mip4_bl_program_page(ts, offset, fw_data + offset,
1076 MIP4_BL_PAGE_SIZE, buf_addr);
1077 if (error)
1078 break;
1080 /* Verify */
1081 error = mip4_bl_verify_page(ts, offset, fw_data + offset,
1082 MIP4_BL_PAGE_SIZE, buf_addr);
1083 if (error)
1084 break;
1087 exit_bl:
1088 /* Exit bootloader mode */
1089 dev_dbg(&client->dev, "Exiting bootloader mode\n");
1091 error2 = mip4_bl_exit(ts);
1092 if (error2) {
1093 dev_err(&client->dev,
1094 "Failed to exit bootloader mode: %d\n", error2);
1095 if (!error)
1096 error = error2;
1099 /* Reset chip */
1100 mip4_power_off(ts);
1101 mip4_power_on(ts);
1103 mip4_query_device(ts);
1105 /* Refresh device parameters */
1106 input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0);
1107 input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0);
1108 input_set_abs_params(ts->input, ABS_X, 0, ts->max_x, 0, 0);
1109 input_set_abs_params(ts->input, ABS_Y, 0, ts->max_y, 0, 0);
1110 input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->ppm_x);
1111 input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->ppm_y);
1112 input_abs_set_res(ts->input, ABS_X, ts->ppm_x);
1113 input_abs_set_res(ts->input, ABS_Y, ts->ppm_y);
1115 return error ? error : 0;
1118 static int mip4_parse_firmware(struct mip4_ts *ts, const struct firmware *fw,
1119 u32 *fw_offset_start, u32 *fw_size,
1120 const struct mip4_bin_tail **pfw_info)
1122 const struct mip4_bin_tail *fw_info;
1123 struct mip4_fw_version fw_version;
1124 u16 tail_size;
1126 if (fw->size < MIP4_BIN_TAIL_SIZE) {
1127 dev_err(&ts->client->dev,
1128 "Invalid firmware, size mismatch (tail %zd vs %zd)\n",
1129 MIP4_BIN_TAIL_SIZE, fw->size);
1130 return -EINVAL;
1133 fw_info = (const void *)&fw->data[fw->size - MIP4_BIN_TAIL_SIZE];
1135 #if MIP4_FW_UPDATE_DEBUG
1136 print_hex_dump(KERN_ERR, MIP4_DEVICE_NAME " Bin Info: ",
1137 DUMP_PREFIX_OFFSET, 16, 1, *fw_info, tail_size, false);
1138 #endif
1140 tail_size = get_unaligned_le16(&fw_info->tail_size);
1141 if (tail_size != MIP4_BIN_TAIL_SIZE) {
1142 dev_err(&ts->client->dev,
1143 "wrong tail size: %d (expected %zd)\n",
1144 tail_size, MIP4_BIN_TAIL_SIZE);
1145 return -EINVAL;
1148 /* Check bin format */
1149 if (memcmp(fw_info->tail_mark, MIP4_BIN_TAIL_MARK,
1150 sizeof(fw_info->tail_mark))) {
1151 dev_err(&ts->client->dev,
1152 "unable to locate tail marker (%*ph vs %*ph)\n",
1153 (int)sizeof(fw_info->tail_mark), fw_info->tail_mark,
1154 (int)sizeof(fw_info->tail_mark), MIP4_BIN_TAIL_MARK);
1155 return -EINVAL;
1158 *fw_offset_start = get_unaligned_le32(&fw_info->bin_start_addr);
1159 *fw_size = get_unaligned_le32(&fw_info->bin_length);
1161 dev_dbg(&ts->client->dev,
1162 "F/W Data offset: %#08x, size: %d\n",
1163 *fw_offset_start, *fw_size);
1165 if (*fw_size % MIP4_BL_PAGE_SIZE) {
1166 dev_err(&ts->client->dev,
1167 "encoded fw length %d is not multiple of pages (%d)\n",
1168 *fw_size, MIP4_BL_PAGE_SIZE);
1169 return -EINVAL;
1172 if (fw->size != *fw_offset_start + *fw_size) {
1173 dev_err(&ts->client->dev,
1174 "Wrong firmware size, expected %d bytes, got %zd\n",
1175 *fw_offset_start + *fw_size, fw->size);
1176 return -EINVAL;
1179 mip4_parse_fw_version((const u8 *)&fw_info->ver_boot, &fw_version);
1181 dev_dbg(&ts->client->dev,
1182 "F/W file version %04X %04X %04X %04X\n",
1183 fw_version.boot, fw_version.core,
1184 fw_version.app, fw_version.param);
1186 dev_dbg(&ts->client->dev, "F/W chip version: %04X %04X %04X %04X\n",
1187 ts->fw_version.boot, ts->fw_version.core,
1188 ts->fw_version.app, ts->fw_version.param);
1190 /* Check F/W type */
1191 if (fw_version.boot != 0xEEEE && fw_version.boot != 0xFFFF &&
1192 fw_version.core == 0xEEEE &&
1193 fw_version.app == 0xEEEE &&
1194 fw_version.param == 0xEEEE) {
1195 dev_dbg(&ts->client->dev, "F/W type: Bootloader\n");
1196 } else if (fw_version.boot == 0xEEEE &&
1197 fw_version.core != 0xEEEE && fw_version.core != 0xFFFF &&
1198 fw_version.app != 0xEEEE && fw_version.app != 0xFFFF &&
1199 fw_version.param != 0xEEEE && fw_version.param != 0xFFFF) {
1200 dev_dbg(&ts->client->dev, "F/W type: Main\n");
1201 } else {
1202 dev_err(&ts->client->dev, "Wrong firmware type\n");
1203 return -EINVAL;
1206 return 0;
1209 static int mip4_execute_fw_update(struct mip4_ts *ts, const struct firmware *fw)
1211 const struct mip4_bin_tail *fw_info;
1212 u32 fw_start_offset;
1213 u32 fw_size;
1214 int retires = 3;
1215 int error;
1217 error = mip4_parse_firmware(ts, fw,
1218 &fw_start_offset, &fw_size, &fw_info);
1219 if (error)
1220 return error;
1222 if (ts->input->users) {
1223 disable_irq(ts->client->irq);
1224 } else {
1225 error = mip4_power_on(ts);
1226 if (error)
1227 return error;
1230 /* Update firmware */
1231 do {
1232 error = mip4_flash_fw(ts, fw->data, fw_size, fw_start_offset);
1233 if (!error)
1234 break;
1235 } while (--retires);
1237 if (error)
1238 dev_err(&ts->client->dev,
1239 "Failed to flash firmware: %d\n", error);
1241 /* Enable IRQ */
1242 if (ts->input->users)
1243 enable_irq(ts->client->irq);
1244 else
1245 mip4_power_off(ts);
1247 return error ? error : 0;
1250 static ssize_t mip4_sysfs_fw_update(struct device *dev,
1251 struct device_attribute *attr,
1252 const char *buf, size_t count)
1254 struct i2c_client *client = to_i2c_client(dev);
1255 struct mip4_ts *ts = i2c_get_clientdata(client);
1256 const struct firmware *fw;
1257 int error;
1259 error = request_firmware(&fw, MIP4_FW_NAME, dev);
1260 if (error) {
1261 dev_err(&ts->client->dev,
1262 "Failed to retrieve firmware %s: %d\n",
1263 MIP4_FW_NAME, error);
1264 return error;
1268 * Take input mutex to prevent racing with itself and also with
1269 * userspace opening and closing the device and also suspend/resume
1270 * transitions.
1272 mutex_lock(&ts->input->mutex);
1274 error = mip4_execute_fw_update(ts, fw);
1276 mutex_unlock(&ts->input->mutex);
1278 release_firmware(fw);
1280 if (error) {
1281 dev_err(&ts->client->dev,
1282 "Firmware update failed: %d\n", error);
1283 return error;
1286 return count;
1289 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mip4_sysfs_fw_update);
1291 static ssize_t mip4_sysfs_read_fw_version(struct device *dev,
1292 struct device_attribute *attr,
1293 char *buf)
1295 struct i2c_client *client = to_i2c_client(dev);
1296 struct mip4_ts *ts = i2c_get_clientdata(client);
1297 size_t count;
1299 /* Take lock to prevent racing with firmware update */
1300 mutex_lock(&ts->input->mutex);
1302 count = snprintf(buf, PAGE_SIZE, "%04X %04X %04X %04X\n",
1303 ts->fw_version.boot, ts->fw_version.core,
1304 ts->fw_version.app, ts->fw_version.param);
1306 mutex_unlock(&ts->input->mutex);
1308 return count;
1311 static DEVICE_ATTR(fw_version, S_IRUGO, mip4_sysfs_read_fw_version, NULL);
1313 static ssize_t mip4_sysfs_read_hw_version(struct device *dev,
1314 struct device_attribute *attr,
1315 char *buf)
1317 struct i2c_client *client = to_i2c_client(dev);
1318 struct mip4_ts *ts = i2c_get_clientdata(client);
1319 size_t count;
1321 /* Take lock to prevent racing with firmware update */
1322 mutex_lock(&ts->input->mutex);
1325 * product_name shows the name or version of the hardware
1326 * paired with current firmware in the chip.
1328 count = snprintf(buf, PAGE_SIZE, "%.*s\n",
1329 (int)sizeof(ts->product_name), ts->product_name);
1331 mutex_unlock(&ts->input->mutex);
1333 return count;
1336 static DEVICE_ATTR(hw_version, S_IRUGO, mip4_sysfs_read_hw_version, NULL);
1338 static struct attribute *mip4_attrs[] = {
1339 &dev_attr_fw_version.attr,
1340 &dev_attr_hw_version.attr,
1341 &dev_attr_update_fw.attr,
1342 NULL,
1345 static const struct attribute_group mip4_attr_group = {
1346 .attrs = mip4_attrs,
1349 static void mip4_sysfs_remove(void *_data)
1351 struct mip4_ts *ts = _data;
1353 sysfs_remove_group(&ts->client->dev.kobj, &mip4_attr_group);
1356 static int mip4_probe(struct i2c_client *client, const struct i2c_device_id *id)
1358 struct mip4_ts *ts;
1359 struct input_dev *input;
1360 int error;
1362 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1363 dev_err(&client->dev, "Not supported I2C adapter\n");
1364 return -ENXIO;
1367 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1368 if (!ts)
1369 return -ENOMEM;
1371 input = devm_input_allocate_device(&client->dev);
1372 if (!input)
1373 return -ENOMEM;
1375 ts->client = client;
1376 ts->input = input;
1378 snprintf(ts->phys, sizeof(ts->phys),
1379 "%s/input0", dev_name(&client->dev));
1381 ts->gpio_ce = devm_gpiod_get_optional(&client->dev,
1382 "ce", GPIOD_OUT_LOW);
1383 if (IS_ERR(ts->gpio_ce)) {
1384 error = PTR_ERR(ts->gpio_ce);
1385 if (error != EPROBE_DEFER)
1386 dev_err(&client->dev,
1387 "Failed to get gpio: %d\n", error);
1388 return error;
1391 error = mip4_power_on(ts);
1392 if (error)
1393 return error;
1394 error = mip4_query_device(ts);
1395 mip4_power_off(ts);
1396 if (error)
1397 return error;
1399 input->name = "MELFAS MIP4 Touchscreen";
1400 input->phys = ts->phys;
1402 input->id.bustype = BUS_I2C;
1403 input->id.vendor = 0x13c5;
1405 input->open = mip4_input_open;
1406 input->close = mip4_input_close;
1408 input_set_drvdata(input, ts);
1410 input->keycode = ts->key_code;
1411 input->keycodesize = sizeof(*ts->key_code);
1412 input->keycodemax = ts->key_num;
1414 input_set_abs_params(input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0);
1415 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0);
1416 input_set_abs_params(input, ABS_MT_PRESSURE,
1417 MIP4_PRESSURE_MIN, MIP4_PRESSURE_MAX, 0, 0);
1418 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR,
1419 MIP4_TOUCH_MAJOR_MIN, MIP4_TOUCH_MAJOR_MAX, 0, 0);
1420 input_set_abs_params(input, ABS_MT_TOUCH_MINOR,
1421 MIP4_TOUCH_MINOR_MIN, MIP4_TOUCH_MINOR_MAX, 0, 0);
1422 input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->ppm_x);
1423 input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->ppm_y);
1425 error = input_mt_init_slots(input, MIP4_MAX_FINGERS, INPUT_MT_DIRECT);
1426 if (error)
1427 return error;
1429 i2c_set_clientdata(client, ts);
1431 error = devm_request_threaded_irq(&client->dev, client->irq,
1432 NULL, mip4_interrupt,
1433 IRQF_ONESHOT, MIP4_DEVICE_NAME, ts);
1434 if (error) {
1435 dev_err(&client->dev,
1436 "Failed to request interrupt %d: %d\n",
1437 client->irq, error);
1438 return error;
1441 disable_irq(client->irq);
1443 error = input_register_device(input);
1444 if (error) {
1445 dev_err(&client->dev,
1446 "Failed to register input device: %d\n", error);
1447 return error;
1450 error = sysfs_create_group(&client->dev.kobj, &mip4_attr_group);
1451 if (error) {
1452 dev_err(&client->dev,
1453 "Failed to create sysfs attribute group: %d\n", error);
1454 return error;
1457 error = devm_add_action(&client->dev, mip4_sysfs_remove, ts);
1458 if (error) {
1459 mip4_sysfs_remove(ts);
1460 dev_err(&client->dev,
1461 "Failed to install sysfs remoce action: %d\n", error);
1462 return error;
1465 return 0;
1468 static int __maybe_unused mip4_suspend(struct device *dev)
1470 struct i2c_client *client = to_i2c_client(dev);
1471 struct mip4_ts *ts = i2c_get_clientdata(client);
1472 struct input_dev *input = ts->input;
1474 mutex_lock(&input->mutex);
1476 if (device_may_wakeup(dev))
1477 ts->wake_irq_enabled = enable_irq_wake(client->irq) == 0;
1478 else if (input->users)
1479 mip4_disable(ts);
1481 mutex_unlock(&input->mutex);
1483 return 0;
1486 static int __maybe_unused mip4_resume(struct device *dev)
1488 struct i2c_client *client = to_i2c_client(dev);
1489 struct mip4_ts *ts = i2c_get_clientdata(client);
1490 struct input_dev *input = ts->input;
1492 mutex_lock(&input->mutex);
1494 if (ts->wake_irq_enabled)
1495 disable_irq_wake(client->irq);
1496 else if (input->users)
1497 mip4_enable(ts);
1499 mutex_unlock(&input->mutex);
1501 return 0;
1504 static SIMPLE_DEV_PM_OPS(mip4_pm_ops, mip4_suspend, mip4_resume);
1506 #ifdef CONFIG_OF
1507 static const struct of_device_id mip4_of_match[] = {
1508 { .compatible = "melfas,"MIP4_DEVICE_NAME, },
1509 { },
1511 MODULE_DEVICE_TABLE(of, mip4_of_match);
1512 #endif
1514 #ifdef CONFIG_ACPI
1515 static const struct acpi_device_id mip4_acpi_match[] = {
1516 { "MLFS0000", 0},
1517 { },
1519 MODULE_DEVICE_TABLE(acpi, mip4_acpi_match);
1520 #endif
1522 static const struct i2c_device_id mip4_i2c_ids[] = {
1523 { MIP4_DEVICE_NAME, 0 },
1524 { },
1526 MODULE_DEVICE_TABLE(i2c, mip4_i2c_ids);
1528 static struct i2c_driver mip4_driver = {
1529 .id_table = mip4_i2c_ids,
1530 .probe = mip4_probe,
1531 .driver = {
1532 .name = MIP4_DEVICE_NAME,
1533 .of_match_table = of_match_ptr(mip4_of_match),
1534 .acpi_match_table = ACPI_PTR(mip4_acpi_match),
1535 .pm = &mip4_pm_ops,
1538 module_i2c_driver(mip4_driver);
1540 MODULE_DESCRIPTION("MELFAS MIP4 Touchscreen");
1541 MODULE_VERSION("2016.03.12");
1542 MODULE_AUTHOR("Sangwon Jee <jeesw@melfas.com>");
1543 MODULE_LICENSE("GPL");