spi: bcm2835: Fix controller unregister order
[linux/fpc-iii.git] / drivers / input / touchscreen / melfas_mip4.c
blob552a3773f79d0b7815d6d180f7aabc9083ff6c92
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];
160 char ic_name[4];
162 unsigned int max_x;
163 unsigned int max_y;
164 u8 node_x;
165 u8 node_y;
166 u8 node_key;
167 unsigned int ppm_x;
168 unsigned int ppm_y;
170 struct mip4_fw_version fw_version;
172 unsigned int event_size;
173 unsigned int event_format;
175 unsigned int key_num;
176 unsigned short key_code[MIP4_MAX_KEYS];
178 bool wake_irq_enabled;
180 u8 buf[MIP4_BUF_SIZE];
183 static int mip4_i2c_xfer(struct mip4_ts *ts,
184 char *write_buf, unsigned int write_len,
185 char *read_buf, unsigned int read_len)
187 struct i2c_msg msg[] = {
189 .addr = ts->client->addr,
190 .flags = 0,
191 .buf = write_buf,
192 .len = write_len,
193 }, {
194 .addr = ts->client->addr,
195 .flags = I2C_M_RD,
196 .buf = read_buf,
197 .len = read_len,
200 int retry = I2C_RETRY_COUNT;
201 int res;
202 int error;
204 do {
205 res = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
206 if (res == ARRAY_SIZE(msg))
207 return 0;
209 error = res < 0 ? res : -EIO;
210 dev_err(&ts->client->dev,
211 "%s - i2c_transfer failed: %d (%d)\n",
212 __func__, error, res);
213 } while (--retry);
215 return error;
218 static void mip4_parse_fw_version(const u8 *buf, struct mip4_fw_version *v)
220 v->boot = get_unaligned_le16(buf + 0);
221 v->core = get_unaligned_le16(buf + 2);
222 v->app = get_unaligned_le16(buf + 4);
223 v->param = get_unaligned_le16(buf + 6);
227 * Read chip firmware version
229 static int mip4_get_fw_version(struct mip4_ts *ts)
231 u8 cmd[] = { MIP4_R0_INFO, MIP4_R1_INFO_VERSION_BOOT };
232 u8 buf[sizeof(ts->fw_version)];
233 int error;
235 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, sizeof(buf));
236 if (error) {
237 memset(&ts->fw_version, 0xff, sizeof(ts->fw_version));
238 return error;
241 mip4_parse_fw_version(buf, &ts->fw_version);
243 return 0;
247 * Fetch device characteristics
249 static int mip4_query_device(struct mip4_ts *ts)
251 int error;
252 u8 cmd[2];
253 u8 buf[14];
255 /* Product name */
256 cmd[0] = MIP4_R0_INFO;
257 cmd[1] = MIP4_R1_INFO_PRODUCT_NAME;
258 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd),
259 ts->product_name, sizeof(ts->product_name));
260 if (error)
261 dev_warn(&ts->client->dev,
262 "Failed to retrieve product name: %d\n", error);
263 else
264 dev_dbg(&ts->client->dev, "product name: %.*s\n",
265 (int)sizeof(ts->product_name), ts->product_name);
267 /* IC name */
268 cmd[0] = MIP4_R0_INFO;
269 cmd[1] = MIP4_R1_INFO_IC_NAME;
270 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd),
271 ts->ic_name, sizeof(ts->ic_name));
272 if (error)
273 dev_warn(&ts->client->dev,
274 "Failed to retrieve IC name: %d\n", error);
275 else
276 dev_dbg(&ts->client->dev, "IC name: %.*s\n",
277 (int)sizeof(ts->ic_name), ts->ic_name);
279 /* Firmware version */
280 error = mip4_get_fw_version(ts);
281 if (error)
282 dev_warn(&ts->client->dev,
283 "Failed to retrieve FW version: %d\n", error);
284 else
285 dev_dbg(&ts->client->dev, "F/W Version: %04X %04X %04X %04X\n",
286 ts->fw_version.boot, ts->fw_version.core,
287 ts->fw_version.app, ts->fw_version.param);
289 /* Resolution */
290 cmd[0] = MIP4_R0_INFO;
291 cmd[1] = MIP4_R1_INFO_RESOLUTION_X;
292 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 14);
293 if (error) {
294 dev_warn(&ts->client->dev,
295 "Failed to retrieve touchscreen parameters: %d\n",
296 error);
297 } else {
298 ts->max_x = get_unaligned_le16(&buf[0]);
299 ts->max_y = get_unaligned_le16(&buf[2]);
300 dev_dbg(&ts->client->dev, "max_x: %d, max_y: %d\n",
301 ts->max_x, ts->max_y);
303 ts->node_x = buf[4];
304 ts->node_y = buf[5];
305 ts->node_key = buf[6];
306 dev_dbg(&ts->client->dev,
307 "node_x: %d, node_y: %d, node_key: %d\n",
308 ts->node_x, ts->node_y, ts->node_key);
310 ts->ppm_x = buf[12];
311 ts->ppm_y = buf[13];
312 dev_dbg(&ts->client->dev, "ppm_x: %d, ppm_y: %d\n",
313 ts->ppm_x, ts->ppm_y);
315 /* Key ts */
316 if (ts->node_key > 0)
317 ts->key_num = ts->node_key;
320 /* Protocol */
321 cmd[0] = MIP4_R0_EVENT;
322 cmd[1] = MIP4_R1_EVENT_SUPPORTED_FUNC;
323 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 7);
324 if (error) {
325 dev_warn(&ts->client->dev,
326 "Failed to retrieve device type: %d\n", error);
327 ts->event_format = 0xff;
328 } else {
329 ts->event_format = get_unaligned_le16(&buf[4]);
330 ts->event_size = buf[6];
331 dev_dbg(&ts->client->dev, "event_format: %d, event_size: %d\n",
332 ts->event_format, ts->event_size);
334 if (ts->event_format == 2 || ts->event_format > 3)
335 dev_warn(&ts->client->dev,
336 "Unknown event format %d\n", ts->event_format);
339 return 0;
342 static int mip4_power_on(struct mip4_ts *ts)
344 if (ts->gpio_ce) {
345 gpiod_set_value_cansleep(ts->gpio_ce, 1);
347 /* Booting delay : 200~300ms */
348 usleep_range(200 * 1000, 300 * 1000);
351 return 0;
354 static void mip4_power_off(struct mip4_ts *ts)
356 if (ts->gpio_ce)
357 gpiod_set_value_cansleep(ts->gpio_ce, 0);
361 * Clear touch input event status
363 static void mip4_clear_input(struct mip4_ts *ts)
365 int i;
367 /* Screen */
368 for (i = 0; i < MIP4_MAX_FINGERS; i++) {
369 input_mt_slot(ts->input, i);
370 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 0);
373 /* Keys */
374 for (i = 0; i < ts->key_num; i++)
375 input_report_key(ts->input, ts->key_code[i], 0);
377 input_sync(ts->input);
380 static int mip4_enable(struct mip4_ts *ts)
382 int error;
384 error = mip4_power_on(ts);
385 if (error)
386 return error;
388 enable_irq(ts->client->irq);
390 return 0;
393 static void mip4_disable(struct mip4_ts *ts)
395 disable_irq(ts->client->irq);
397 mip4_power_off(ts);
399 mip4_clear_input(ts);
402 /*****************************************************************
403 * Input handling
404 *****************************************************************/
406 static void mip4_report_keys(struct mip4_ts *ts, u8 *packet)
408 u8 key;
409 bool down;
411 switch (ts->event_format) {
412 case 0:
413 case 1:
414 key = packet[0] & 0x0F;
415 down = packet[0] & 0x80;
416 break;
418 case 3:
419 default:
420 key = packet[0] & 0x0F;
421 down = packet[1] & 0x01;
422 break;
425 /* Report key event */
426 if (key >= 1 && key <= ts->key_num) {
427 unsigned short keycode = ts->key_code[key - 1];
429 dev_dbg(&ts->client->dev,
430 "Key - ID: %d, keycode: %d, state: %d\n",
431 key, keycode, down);
433 input_event(ts->input, EV_MSC, MSC_SCAN, keycode);
434 input_report_key(ts->input, keycode, down);
436 } else {
437 dev_err(&ts->client->dev, "Unknown key: %d\n", key);
441 static void mip4_report_touch(struct mip4_ts *ts, u8 *packet)
443 int id;
444 bool hover;
445 bool palm;
446 bool state;
447 u16 x, y;
448 u8 pressure_stage = 0;
449 u8 pressure;
450 u8 size;
451 u8 touch_major;
452 u8 touch_minor;
454 switch (ts->event_format) {
455 case 0:
456 case 1:
457 /* Touch only */
458 state = packet[0] & BIT(7);
459 hover = packet[0] & BIT(5);
460 palm = packet[0] & BIT(4);
461 id = (packet[0] & 0x0F) - 1;
462 x = ((packet[1] & 0x0F) << 8) | packet[2];
463 y = (((packet[1] >> 4) & 0x0F) << 8) |
464 packet[3];
465 pressure = packet[4];
466 size = packet[5];
467 if (ts->event_format == 0) {
468 touch_major = packet[5];
469 touch_minor = packet[5];
470 } else {
471 touch_major = packet[6];
472 touch_minor = packet[7];
474 break;
476 case 3:
477 default:
478 /* Touch + Force(Pressure) */
479 id = (packet[0] & 0x0F) - 1;
480 hover = packet[1] & BIT(2);
481 palm = packet[1] & BIT(1);
482 state = packet[1] & BIT(0);
483 x = ((packet[2] & 0x0F) << 8) | packet[3];
484 y = (((packet[2] >> 4) & 0x0F) << 8) |
485 packet[4];
486 size = packet[6];
487 pressure_stage = (packet[7] & 0xF0) >> 4;
488 pressure = ((packet[7] & 0x0F) << 8) |
489 packet[8];
490 touch_major = packet[9];
491 touch_minor = packet[10];
492 break;
495 dev_dbg(&ts->client->dev,
496 "Screen - Slot: %d State: %d X: %04d Y: %04d Z: %d\n",
497 id, state, x, y, pressure);
499 if (unlikely(id < 0 || id >= MIP4_MAX_FINGERS)) {
500 dev_err(&ts->client->dev, "Screen - invalid slot ID: %d\n", id);
501 } else if (state) {
502 /* Press or Move event */
503 input_mt_slot(ts->input, id);
504 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
505 input_report_abs(ts->input, ABS_MT_POSITION_X, x);
506 input_report_abs(ts->input, ABS_MT_POSITION_Y, y);
507 input_report_abs(ts->input, ABS_MT_PRESSURE, pressure);
508 input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, touch_major);
509 input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, touch_minor);
510 } else {
511 /* Release event */
512 input_mt_slot(ts->input, id);
513 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 0);
516 input_mt_sync_frame(ts->input);
519 static int mip4_handle_packet(struct mip4_ts *ts, u8 *packet)
521 u8 type;
523 switch (ts->event_format) {
524 case 0:
525 case 1:
526 type = (packet[0] & 0x40) >> 6;
527 break;
529 case 3:
530 type = (packet[0] & 0xF0) >> 4;
531 break;
533 default:
534 /* Should not happen unless we have corrupted firmware */
535 return -EINVAL;
538 dev_dbg(&ts->client->dev, "Type: %d\n", type);
540 /* Report input event */
541 switch (type) {
542 case MIP4_EVENT_INPUT_TYPE_KEY:
543 mip4_report_keys(ts, packet);
544 break;
546 case MIP4_EVENT_INPUT_TYPE_SCREEN:
547 mip4_report_touch(ts, packet);
548 break;
550 default:
551 dev_err(&ts->client->dev, "Unknown event type: %d\n", type);
552 break;
555 return 0;
558 static irqreturn_t mip4_interrupt(int irq, void *dev_id)
560 struct mip4_ts *ts = dev_id;
561 struct i2c_client *client = ts->client;
562 unsigned int i;
563 int error;
564 u8 cmd[2];
565 u8 size;
566 bool alert;
568 /* Read packet info */
569 cmd[0] = MIP4_R0_EVENT;
570 cmd[1] = MIP4_R1_EVENT_PACKET_INFO;
571 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), ts->buf, 1);
572 if (error) {
573 dev_err(&client->dev,
574 "Failed to read packet info: %d\n", error);
575 goto out;
578 size = ts->buf[0] & 0x7F;
579 alert = ts->buf[0] & BIT(7);
580 dev_dbg(&client->dev, "packet size: %d, alert: %d\n", size, alert);
582 /* Check size */
583 if (!size) {
584 dev_err(&client->dev, "Empty packet\n");
585 goto out;
588 /* Read packet data */
589 cmd[0] = MIP4_R0_EVENT;
590 cmd[1] = MIP4_R1_EVENT_PACKET_DATA;
591 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), ts->buf, size);
592 if (error) {
593 dev_err(&client->dev,
594 "Failed to read packet data: %d\n", error);
595 goto out;
598 if (alert) {
599 dev_dbg(&client->dev, "Alert: %d\n", ts->buf[0]);
600 } else {
601 for (i = 0; i < size; i += ts->event_size) {
602 error = mip4_handle_packet(ts, &ts->buf[i]);
603 if (error)
604 break;
607 input_sync(ts->input);
610 out:
611 return IRQ_HANDLED;
614 static int mip4_input_open(struct input_dev *dev)
616 struct mip4_ts *ts = input_get_drvdata(dev);
618 return mip4_enable(ts);
621 static void mip4_input_close(struct input_dev *dev)
623 struct mip4_ts *ts = input_get_drvdata(dev);
625 mip4_disable(ts);
628 /*****************************************************************
629 * Firmware update
630 *****************************************************************/
632 /* Firmware Info */
633 #define MIP4_BL_PAGE_SIZE 512 /* 512 */
634 #define MIP4_BL_PACKET_SIZE 512 /* 512, 256, 128, 64, ... */
637 * Firmware binary tail info
640 struct mip4_bin_tail {
641 u8 tail_mark[4];
642 u8 chip_name[4];
644 __le32 bin_start_addr;
645 __le32 bin_length;
647 __le16 ver_boot;
648 __le16 ver_core;
649 __le16 ver_app;
650 __le16 ver_param;
652 u8 boot_start;
653 u8 boot_end;
654 u8 core_start;
655 u8 core_end;
656 u8 app_start;
657 u8 app_end;
658 u8 param_start;
659 u8 param_end;
661 u8 checksum_type;
662 u8 hw_category;
664 __le16 param_id;
665 __le32 param_length;
666 __le32 build_date;
667 __le32 build_time;
669 __le32 reserved1;
670 __le32 reserved2;
671 __le16 reserved3;
672 __le16 tail_size;
673 __le32 crc;
674 } __packed;
676 #define MIP4_BIN_TAIL_MARK "MBT\001"
677 #define MIP4_BIN_TAIL_SIZE (sizeof(struct mip4_bin_tail))
680 * Bootloader - Read status
682 static int mip4_bl_read_status(struct mip4_ts *ts)
684 u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_STATUS };
685 u8 result;
686 struct i2c_msg msg[] = {
688 .addr = ts->client->addr,
689 .flags = 0,
690 .buf = cmd,
691 .len = sizeof(cmd),
692 }, {
693 .addr = ts->client->addr,
694 .flags = I2C_M_RD,
695 .buf = &result,
696 .len = sizeof(result),
699 int ret;
700 int error;
701 int retry = 1000;
703 do {
704 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
705 if (ret != ARRAY_SIZE(msg)) {
706 error = ret < 0 ? ret : -EIO;
707 dev_err(&ts->client->dev,
708 "Failed to read bootloader status: %d\n",
709 error);
710 return error;
713 switch (result) {
714 case MIP4_BOOT_STATUS_DONE:
715 dev_dbg(&ts->client->dev, "%s - done\n", __func__);
716 return 0;
718 case MIP4_BOOT_STATUS_ERROR:
719 dev_err(&ts->client->dev, "Bootloader failure\n");
720 return -EIO;
722 case MIP4_BOOT_STATUS_BUSY:
723 dev_dbg(&ts->client->dev, "%s - Busy\n", __func__);
724 error = -EBUSY;
725 break;
727 default:
728 dev_err(&ts->client->dev,
729 "Unexpected bootloader status: %#02x\n",
730 result);
731 error = -EINVAL;
732 break;
735 usleep_range(1000, 2000);
736 } while (--retry);
738 return error;
742 * Bootloader - Change mode
744 static int mip4_bl_change_mode(struct mip4_ts *ts, u8 mode)
746 u8 mode_chg_cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_MODE, mode };
747 u8 mode_read_cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_MODE };
748 u8 result;
749 struct i2c_msg msg[] = {
751 .addr = ts->client->addr,
752 .flags = 0,
753 .buf = mode_read_cmd,
754 .len = sizeof(mode_read_cmd),
755 }, {
756 .addr = ts->client->addr,
757 .flags = I2C_M_RD,
758 .buf = &result,
759 .len = sizeof(result),
762 int retry = 10;
763 int ret;
764 int error;
766 do {
767 /* Send mode change command */
768 ret = i2c_master_send(ts->client,
769 mode_chg_cmd, sizeof(mode_chg_cmd));
770 if (ret != sizeof(mode_chg_cmd)) {
771 error = ret < 0 ? ret : -EIO;
772 dev_err(&ts->client->dev,
773 "Failed to send %d mode change: %d (%d)\n",
774 mode, error, ret);
775 return error;
778 dev_dbg(&ts->client->dev,
779 "Sent mode change request (mode: %d)\n", mode);
781 /* Wait */
782 msleep(1000);
784 /* Verify target mode */
785 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
786 if (ret != ARRAY_SIZE(msg)) {
787 error = ret < 0 ? ret : -EIO;
788 dev_err(&ts->client->dev,
789 "Failed to read device mode: %d\n", error);
790 return error;
793 dev_dbg(&ts->client->dev,
794 "Current device mode: %d, want: %d\n", result, mode);
796 if (result == mode)
797 return 0;
799 } while (--retry);
801 return -EIO;
805 * Bootloader - Start bootloader mode
807 static int mip4_bl_enter(struct mip4_ts *ts)
809 return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_BOOT);
813 * Bootloader - Exit bootloader mode
815 static int mip4_bl_exit(struct mip4_ts *ts)
817 return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_APP);
820 static int mip4_bl_get_address(struct mip4_ts *ts, u16 *buf_addr)
822 u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_BUF_ADDR };
823 u8 result[sizeof(u16)];
824 struct i2c_msg msg[] = {
826 .addr = ts->client->addr,
827 .flags = 0,
828 .buf = cmd,
829 .len = sizeof(cmd),
830 }, {
831 .addr = ts->client->addr,
832 .flags = I2C_M_RD,
833 .buf = result,
834 .len = sizeof(result),
837 int ret;
838 int error;
840 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
841 if (ret != ARRAY_SIZE(msg)) {
842 error = ret < 0 ? ret : -EIO;
843 dev_err(&ts->client->dev,
844 "Failed to retrieve bootloader buffer address: %d\n",
845 error);
846 return error;
849 *buf_addr = get_unaligned_le16(result);
850 dev_dbg(&ts->client->dev,
851 "Bootloader buffer address %#04x\n", *buf_addr);
853 return 0;
856 static int mip4_bl_program_page(struct mip4_ts *ts, int offset,
857 const u8 *data, int length, u16 buf_addr)
859 u8 cmd[6];
860 u8 *data_buf;
861 u16 buf_offset;
862 int ret;
863 int error;
865 dev_dbg(&ts->client->dev, "Writing page @%#06x (%d)\n",
866 offset, length);
868 if (length > MIP4_BL_PAGE_SIZE || length % MIP4_BL_PACKET_SIZE) {
869 dev_err(&ts->client->dev,
870 "Invalid page length: %d\n", length);
871 return -EINVAL;
874 data_buf = kmalloc(2 + MIP4_BL_PACKET_SIZE, GFP_KERNEL);
875 if (!data_buf)
876 return -ENOMEM;
878 /* Addr */
879 cmd[0] = MIP4_R0_BOOT;
880 cmd[1] = MIP4_R1_BOOT_TARGET_ADDR;
881 put_unaligned_le32(offset, &cmd[2]);
882 ret = i2c_master_send(ts->client, cmd, 6);
883 if (ret != 6) {
884 error = ret < 0 ? ret : -EIO;
885 dev_err(&ts->client->dev,
886 "Failed to send write page address: %d\n", error);
887 goto out;
890 /* Size */
891 cmd[0] = MIP4_R0_BOOT;
892 cmd[1] = MIP4_R1_BOOT_SIZE;
893 put_unaligned_le32(length, &cmd[2]);
894 ret = i2c_master_send(ts->client, cmd, 6);
895 if (ret != 6) {
896 error = ret < 0 ? ret : -EIO;
897 dev_err(&ts->client->dev,
898 "Failed to send write page size: %d\n", error);
899 goto out;
902 /* Data */
903 for (buf_offset = 0;
904 buf_offset < length;
905 buf_offset += MIP4_BL_PACKET_SIZE) {
906 dev_dbg(&ts->client->dev,
907 "writing chunk at %#04x (size %d)\n",
908 buf_offset, MIP4_BL_PACKET_SIZE);
909 put_unaligned_be16(buf_addr + buf_offset, data_buf);
910 memcpy(&data_buf[2], &data[buf_offset], MIP4_BL_PACKET_SIZE);
911 ret = i2c_master_send(ts->client,
912 data_buf, 2 + MIP4_BL_PACKET_SIZE);
913 if (ret != 2 + MIP4_BL_PACKET_SIZE) {
914 error = ret < 0 ? ret : -EIO;
915 dev_err(&ts->client->dev,
916 "Failed to read chunk at %#04x (size %d): %d\n",
917 buf_offset, MIP4_BL_PACKET_SIZE, error);
918 goto out;
922 /* Command */
923 cmd[0] = MIP4_R0_BOOT;
924 cmd[1] = MIP4_R1_BOOT_CMD;
925 cmd[2] = MIP4_BOOT_CMD_PROGRAM;
926 ret = i2c_master_send(ts->client, cmd, 3);
927 if (ret != 3) {
928 error = ret < 0 ? ret : -EIO;
929 dev_err(&ts->client->dev,
930 "Failed to send 'write' command: %d\n", error);
931 goto out;
934 /* Status */
935 error = mip4_bl_read_status(ts);
937 out:
938 kfree(data_buf);
939 return error ? error : 0;
942 static int mip4_bl_verify_page(struct mip4_ts *ts, int offset,
943 const u8 *data, int length, int buf_addr)
945 u8 cmd[8];
946 u8 *read_buf;
947 int buf_offset;
948 struct i2c_msg msg[] = {
950 .addr = ts->client->addr,
951 .flags = 0,
952 .buf = cmd,
953 .len = 2,
954 }, {
955 .addr = ts->client->addr,
956 .flags = I2C_M_RD,
957 .len = MIP4_BL_PACKET_SIZE,
960 int ret;
961 int error;
963 dev_dbg(&ts->client->dev, "Validating page @%#06x (%d)\n",
964 offset, length);
966 /* Addr */
967 cmd[0] = MIP4_R0_BOOT;
968 cmd[1] = MIP4_R1_BOOT_TARGET_ADDR;
969 put_unaligned_le32(offset, &cmd[2]);
970 ret = i2c_master_send(ts->client, cmd, 6);
971 if (ret != 6) {
972 error = ret < 0 ? ret : -EIO;
973 dev_err(&ts->client->dev,
974 "Failed to send read page address: %d\n", error);
975 return error;
978 /* Size */
979 cmd[0] = MIP4_R0_BOOT;
980 cmd[1] = MIP4_R1_BOOT_SIZE;
981 put_unaligned_le32(length, &cmd[2]);
982 ret = i2c_master_send(ts->client, cmd, 6);
983 if (ret != 6) {
984 error = ret < 0 ? ret : -EIO;
985 dev_err(&ts->client->dev,
986 "Failed to send read page size: %d\n", error);
987 return error;
990 /* Command */
991 cmd[0] = MIP4_R0_BOOT;
992 cmd[1] = MIP4_R1_BOOT_CMD;
993 cmd[2] = MIP4_BOOT_CMD_READ;
994 ret = i2c_master_send(ts->client, cmd, 3);
995 if (ret != 3) {
996 error = ret < 0 ? ret : -EIO;
997 dev_err(&ts->client->dev,
998 "Failed to send 'read' command: %d\n", error);
999 return error;
1002 /* Status */
1003 error = mip4_bl_read_status(ts);
1004 if (error)
1005 return error;
1007 /* Read */
1008 msg[1].buf = read_buf = kmalloc(MIP4_BL_PACKET_SIZE, GFP_KERNEL);
1009 if (!read_buf)
1010 return -ENOMEM;
1012 for (buf_offset = 0;
1013 buf_offset < length;
1014 buf_offset += MIP4_BL_PACKET_SIZE) {
1015 dev_dbg(&ts->client->dev,
1016 "reading chunk at %#04x (size %d)\n",
1017 buf_offset, MIP4_BL_PACKET_SIZE);
1018 put_unaligned_be16(buf_addr + buf_offset, cmd);
1019 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
1020 if (ret != ARRAY_SIZE(msg)) {
1021 error = ret < 0 ? ret : -EIO;
1022 dev_err(&ts->client->dev,
1023 "Failed to read chunk at %#04x (size %d): %d\n",
1024 buf_offset, MIP4_BL_PACKET_SIZE, error);
1025 break;
1028 if (memcmp(&data[buf_offset], read_buf, MIP4_BL_PACKET_SIZE)) {
1029 dev_err(&ts->client->dev,
1030 "Failed to validate chunk at %#04x (size %d)\n",
1031 buf_offset, MIP4_BL_PACKET_SIZE);
1032 #if MIP4_FW_UPDATE_DEBUG
1033 print_hex_dump(KERN_DEBUG,
1034 MIP4_DEVICE_NAME " F/W File: ",
1035 DUMP_PREFIX_OFFSET, 16, 1,
1036 data + offset, MIP4_BL_PACKET_SIZE,
1037 false);
1038 print_hex_dump(KERN_DEBUG,
1039 MIP4_DEVICE_NAME " F/W Chip: ",
1040 DUMP_PREFIX_OFFSET, 16, 1,
1041 read_buf, MIP4_BL_PAGE_SIZE, false);
1042 #endif
1043 error = -EINVAL;
1044 break;
1048 kfree(read_buf);
1049 return error ? error : 0;
1053 * Flash chip firmware
1055 static int mip4_flash_fw(struct mip4_ts *ts,
1056 const u8 *fw_data, u32 fw_size, u32 fw_offset)
1058 struct i2c_client *client = ts->client;
1059 int offset;
1060 u16 buf_addr;
1061 int error, error2;
1063 /* Enter bootloader mode */
1064 dev_dbg(&client->dev, "Entering bootloader mode\n");
1066 error = mip4_bl_enter(ts);
1067 if (error) {
1068 dev_err(&client->dev,
1069 "Failed to enter bootloader mode: %d\n",
1070 error);
1071 return error;
1074 /* Read info */
1075 error = mip4_bl_get_address(ts, &buf_addr);
1076 if (error)
1077 goto exit_bl;
1079 /* Program & Verify */
1080 dev_dbg(&client->dev,
1081 "Program & Verify, page size: %d, packet size: %d\n",
1082 MIP4_BL_PAGE_SIZE, MIP4_BL_PACKET_SIZE);
1084 for (offset = fw_offset;
1085 offset < fw_offset + fw_size;
1086 offset += MIP4_BL_PAGE_SIZE) {
1087 /* Program */
1088 error = mip4_bl_program_page(ts, offset, fw_data + offset,
1089 MIP4_BL_PAGE_SIZE, buf_addr);
1090 if (error)
1091 break;
1093 /* Verify */
1094 error = mip4_bl_verify_page(ts, offset, fw_data + offset,
1095 MIP4_BL_PAGE_SIZE, buf_addr);
1096 if (error)
1097 break;
1100 exit_bl:
1101 /* Exit bootloader mode */
1102 dev_dbg(&client->dev, "Exiting bootloader mode\n");
1104 error2 = mip4_bl_exit(ts);
1105 if (error2) {
1106 dev_err(&client->dev,
1107 "Failed to exit bootloader mode: %d\n", error2);
1108 if (!error)
1109 error = error2;
1112 /* Reset chip */
1113 mip4_power_off(ts);
1114 mip4_power_on(ts);
1116 mip4_query_device(ts);
1118 /* Refresh device parameters */
1119 input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0);
1120 input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0);
1121 input_set_abs_params(ts->input, ABS_X, 0, ts->max_x, 0, 0);
1122 input_set_abs_params(ts->input, ABS_Y, 0, ts->max_y, 0, 0);
1123 input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->ppm_x);
1124 input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->ppm_y);
1125 input_abs_set_res(ts->input, ABS_X, ts->ppm_x);
1126 input_abs_set_res(ts->input, ABS_Y, ts->ppm_y);
1128 return error ? error : 0;
1131 static int mip4_parse_firmware(struct mip4_ts *ts, const struct firmware *fw,
1132 u32 *fw_offset_start, u32 *fw_size,
1133 const struct mip4_bin_tail **pfw_info)
1135 const struct mip4_bin_tail *fw_info;
1136 struct mip4_fw_version fw_version;
1137 u16 tail_size;
1139 if (fw->size < MIP4_BIN_TAIL_SIZE) {
1140 dev_err(&ts->client->dev,
1141 "Invalid firmware, size mismatch (tail %zd vs %zd)\n",
1142 MIP4_BIN_TAIL_SIZE, fw->size);
1143 return -EINVAL;
1146 fw_info = (const void *)&fw->data[fw->size - MIP4_BIN_TAIL_SIZE];
1148 #if MIP4_FW_UPDATE_DEBUG
1149 print_hex_dump(KERN_ERR, MIP4_DEVICE_NAME " Bin Info: ",
1150 DUMP_PREFIX_OFFSET, 16, 1, *fw_info, tail_size, false);
1151 #endif
1153 tail_size = get_unaligned_le16(&fw_info->tail_size);
1154 if (tail_size != MIP4_BIN_TAIL_SIZE) {
1155 dev_err(&ts->client->dev,
1156 "wrong tail size: %d (expected %zd)\n",
1157 tail_size, MIP4_BIN_TAIL_SIZE);
1158 return -EINVAL;
1161 /* Check bin format */
1162 if (memcmp(fw_info->tail_mark, MIP4_BIN_TAIL_MARK,
1163 sizeof(fw_info->tail_mark))) {
1164 dev_err(&ts->client->dev,
1165 "unable to locate tail marker (%*ph vs %*ph)\n",
1166 (int)sizeof(fw_info->tail_mark), fw_info->tail_mark,
1167 (int)sizeof(fw_info->tail_mark), MIP4_BIN_TAIL_MARK);
1168 return -EINVAL;
1171 *fw_offset_start = get_unaligned_le32(&fw_info->bin_start_addr);
1172 *fw_size = get_unaligned_le32(&fw_info->bin_length);
1174 dev_dbg(&ts->client->dev,
1175 "F/W Data offset: %#08x, size: %d\n",
1176 *fw_offset_start, *fw_size);
1178 if (*fw_size % MIP4_BL_PAGE_SIZE) {
1179 dev_err(&ts->client->dev,
1180 "encoded fw length %d is not multiple of pages (%d)\n",
1181 *fw_size, MIP4_BL_PAGE_SIZE);
1182 return -EINVAL;
1185 if (fw->size != *fw_offset_start + *fw_size) {
1186 dev_err(&ts->client->dev,
1187 "Wrong firmware size, expected %d bytes, got %zd\n",
1188 *fw_offset_start + *fw_size, fw->size);
1189 return -EINVAL;
1192 mip4_parse_fw_version((const u8 *)&fw_info->ver_boot, &fw_version);
1194 dev_dbg(&ts->client->dev,
1195 "F/W file version %04X %04X %04X %04X\n",
1196 fw_version.boot, fw_version.core,
1197 fw_version.app, fw_version.param);
1199 dev_dbg(&ts->client->dev, "F/W chip version: %04X %04X %04X %04X\n",
1200 ts->fw_version.boot, ts->fw_version.core,
1201 ts->fw_version.app, ts->fw_version.param);
1203 /* Check F/W type */
1204 if (fw_version.boot != 0xEEEE && fw_version.boot != 0xFFFF &&
1205 fw_version.core == 0xEEEE &&
1206 fw_version.app == 0xEEEE &&
1207 fw_version.param == 0xEEEE) {
1208 dev_dbg(&ts->client->dev, "F/W type: Bootloader\n");
1209 } else if (fw_version.boot == 0xEEEE &&
1210 fw_version.core != 0xEEEE && fw_version.core != 0xFFFF &&
1211 fw_version.app != 0xEEEE && fw_version.app != 0xFFFF &&
1212 fw_version.param != 0xEEEE && fw_version.param != 0xFFFF) {
1213 dev_dbg(&ts->client->dev, "F/W type: Main\n");
1214 } else {
1215 dev_err(&ts->client->dev, "Wrong firmware type\n");
1216 return -EINVAL;
1219 return 0;
1222 static int mip4_execute_fw_update(struct mip4_ts *ts, const struct firmware *fw)
1224 const struct mip4_bin_tail *fw_info;
1225 u32 fw_start_offset;
1226 u32 fw_size;
1227 int retires = 3;
1228 int error;
1230 error = mip4_parse_firmware(ts, fw,
1231 &fw_start_offset, &fw_size, &fw_info);
1232 if (error)
1233 return error;
1235 if (ts->input->users) {
1236 disable_irq(ts->client->irq);
1237 } else {
1238 error = mip4_power_on(ts);
1239 if (error)
1240 return error;
1243 /* Update firmware */
1244 do {
1245 error = mip4_flash_fw(ts, fw->data, fw_size, fw_start_offset);
1246 if (!error)
1247 break;
1248 } while (--retires);
1250 if (error)
1251 dev_err(&ts->client->dev,
1252 "Failed to flash firmware: %d\n", error);
1254 /* Enable IRQ */
1255 if (ts->input->users)
1256 enable_irq(ts->client->irq);
1257 else
1258 mip4_power_off(ts);
1260 return error ? error : 0;
1263 static ssize_t mip4_sysfs_fw_update(struct device *dev,
1264 struct device_attribute *attr,
1265 const char *buf, size_t count)
1267 struct i2c_client *client = to_i2c_client(dev);
1268 struct mip4_ts *ts = i2c_get_clientdata(client);
1269 const struct firmware *fw;
1270 int error;
1272 error = request_firmware(&fw, MIP4_FW_NAME, dev);
1273 if (error) {
1274 dev_err(&ts->client->dev,
1275 "Failed to retrieve firmware %s: %d\n",
1276 MIP4_FW_NAME, error);
1277 return error;
1281 * Take input mutex to prevent racing with itself and also with
1282 * userspace opening and closing the device and also suspend/resume
1283 * transitions.
1285 mutex_lock(&ts->input->mutex);
1287 error = mip4_execute_fw_update(ts, fw);
1289 mutex_unlock(&ts->input->mutex);
1291 release_firmware(fw);
1293 if (error) {
1294 dev_err(&ts->client->dev,
1295 "Firmware update failed: %d\n", error);
1296 return error;
1299 return count;
1302 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mip4_sysfs_fw_update);
1304 static ssize_t mip4_sysfs_read_fw_version(struct device *dev,
1305 struct device_attribute *attr,
1306 char *buf)
1308 struct i2c_client *client = to_i2c_client(dev);
1309 struct mip4_ts *ts = i2c_get_clientdata(client);
1310 size_t count;
1312 /* Take lock to prevent racing with firmware update */
1313 mutex_lock(&ts->input->mutex);
1315 count = snprintf(buf, PAGE_SIZE, "%04X %04X %04X %04X\n",
1316 ts->fw_version.boot, ts->fw_version.core,
1317 ts->fw_version.app, ts->fw_version.param);
1319 mutex_unlock(&ts->input->mutex);
1321 return count;
1324 static DEVICE_ATTR(fw_version, S_IRUGO, mip4_sysfs_read_fw_version, NULL);
1326 static ssize_t mip4_sysfs_read_hw_version(struct device *dev,
1327 struct device_attribute *attr,
1328 char *buf)
1330 struct i2c_client *client = to_i2c_client(dev);
1331 struct mip4_ts *ts = i2c_get_clientdata(client);
1332 size_t count;
1334 /* Take lock to prevent racing with firmware update */
1335 mutex_lock(&ts->input->mutex);
1338 * product_name shows the name or version of the hardware
1339 * paired with current firmware in the chip.
1341 count = snprintf(buf, PAGE_SIZE, "%.*s\n",
1342 (int)sizeof(ts->product_name), ts->product_name);
1344 mutex_unlock(&ts->input->mutex);
1346 return count;
1349 static DEVICE_ATTR(hw_version, S_IRUGO, mip4_sysfs_read_hw_version, NULL);
1351 static ssize_t mip4_sysfs_read_ic_name(struct device *dev,
1352 struct device_attribute *attr,
1353 char *buf)
1355 struct i2c_client *client = to_i2c_client(dev);
1356 struct mip4_ts *ts = i2c_get_clientdata(client);
1357 size_t count;
1359 mutex_lock(&ts->input->mutex);
1361 count = snprintf(buf, PAGE_SIZE, "%.*s\n",
1362 (int)sizeof(ts->ic_name), ts->ic_name);
1364 mutex_unlock(&ts->input->mutex);
1366 return count;
1369 static DEVICE_ATTR(ic_name, S_IRUGO, mip4_sysfs_read_ic_name, NULL);
1371 static struct attribute *mip4_attrs[] = {
1372 &dev_attr_fw_version.attr,
1373 &dev_attr_hw_version.attr,
1374 &dev_attr_ic_name.attr,
1375 &dev_attr_update_fw.attr,
1376 NULL,
1379 static const struct attribute_group mip4_attr_group = {
1380 .attrs = mip4_attrs,
1383 static void mip4_sysfs_remove(void *_data)
1385 struct mip4_ts *ts = _data;
1387 sysfs_remove_group(&ts->client->dev.kobj, &mip4_attr_group);
1390 static int mip4_probe(struct i2c_client *client, const struct i2c_device_id *id)
1392 struct mip4_ts *ts;
1393 struct input_dev *input;
1394 int error;
1396 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1397 dev_err(&client->dev, "Not supported I2C adapter\n");
1398 return -ENXIO;
1401 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1402 if (!ts)
1403 return -ENOMEM;
1405 input = devm_input_allocate_device(&client->dev);
1406 if (!input)
1407 return -ENOMEM;
1409 ts->client = client;
1410 ts->input = input;
1412 snprintf(ts->phys, sizeof(ts->phys),
1413 "%s/input0", dev_name(&client->dev));
1415 ts->gpio_ce = devm_gpiod_get_optional(&client->dev,
1416 "ce", GPIOD_OUT_LOW);
1417 if (IS_ERR(ts->gpio_ce)) {
1418 error = PTR_ERR(ts->gpio_ce);
1419 if (error != EPROBE_DEFER)
1420 dev_err(&client->dev,
1421 "Failed to get gpio: %d\n", error);
1422 return error;
1425 error = mip4_power_on(ts);
1426 if (error)
1427 return error;
1428 error = mip4_query_device(ts);
1429 mip4_power_off(ts);
1430 if (error)
1431 return error;
1433 input->name = "MELFAS MIP4 Touchscreen";
1434 input->phys = ts->phys;
1436 input->id.bustype = BUS_I2C;
1437 input->id.vendor = 0x13c5;
1439 input->open = mip4_input_open;
1440 input->close = mip4_input_close;
1442 input_set_drvdata(input, ts);
1444 input->keycode = ts->key_code;
1445 input->keycodesize = sizeof(*ts->key_code);
1446 input->keycodemax = ts->key_num;
1448 input_set_abs_params(input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0);
1449 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0);
1450 input_set_abs_params(input, ABS_MT_PRESSURE,
1451 MIP4_PRESSURE_MIN, MIP4_PRESSURE_MAX, 0, 0);
1452 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR,
1453 MIP4_TOUCH_MAJOR_MIN, MIP4_TOUCH_MAJOR_MAX, 0, 0);
1454 input_set_abs_params(input, ABS_MT_TOUCH_MINOR,
1455 MIP4_TOUCH_MINOR_MIN, MIP4_TOUCH_MINOR_MAX, 0, 0);
1456 input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->ppm_x);
1457 input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->ppm_y);
1459 error = input_mt_init_slots(input, MIP4_MAX_FINGERS, INPUT_MT_DIRECT);
1460 if (error)
1461 return error;
1463 i2c_set_clientdata(client, ts);
1465 error = devm_request_threaded_irq(&client->dev, client->irq,
1466 NULL, mip4_interrupt,
1467 IRQF_ONESHOT, MIP4_DEVICE_NAME, ts);
1468 if (error) {
1469 dev_err(&client->dev,
1470 "Failed to request interrupt %d: %d\n",
1471 client->irq, error);
1472 return error;
1475 disable_irq(client->irq);
1477 error = input_register_device(input);
1478 if (error) {
1479 dev_err(&client->dev,
1480 "Failed to register input device: %d\n", error);
1481 return error;
1484 error = sysfs_create_group(&client->dev.kobj, &mip4_attr_group);
1485 if (error) {
1486 dev_err(&client->dev,
1487 "Failed to create sysfs attribute group: %d\n", error);
1488 return error;
1491 error = devm_add_action(&client->dev, mip4_sysfs_remove, ts);
1492 if (error) {
1493 mip4_sysfs_remove(ts);
1494 dev_err(&client->dev,
1495 "Failed to install sysfs remoce action: %d\n", error);
1496 return error;
1499 return 0;
1502 static int __maybe_unused mip4_suspend(struct device *dev)
1504 struct i2c_client *client = to_i2c_client(dev);
1505 struct mip4_ts *ts = i2c_get_clientdata(client);
1506 struct input_dev *input = ts->input;
1508 mutex_lock(&input->mutex);
1510 if (device_may_wakeup(dev))
1511 ts->wake_irq_enabled = enable_irq_wake(client->irq) == 0;
1512 else if (input->users)
1513 mip4_disable(ts);
1515 mutex_unlock(&input->mutex);
1517 return 0;
1520 static int __maybe_unused mip4_resume(struct device *dev)
1522 struct i2c_client *client = to_i2c_client(dev);
1523 struct mip4_ts *ts = i2c_get_clientdata(client);
1524 struct input_dev *input = ts->input;
1526 mutex_lock(&input->mutex);
1528 if (ts->wake_irq_enabled)
1529 disable_irq_wake(client->irq);
1530 else if (input->users)
1531 mip4_enable(ts);
1533 mutex_unlock(&input->mutex);
1535 return 0;
1538 static SIMPLE_DEV_PM_OPS(mip4_pm_ops, mip4_suspend, mip4_resume);
1540 #ifdef CONFIG_OF
1541 static const struct of_device_id mip4_of_match[] = {
1542 { .compatible = "melfas,"MIP4_DEVICE_NAME, },
1543 { },
1545 MODULE_DEVICE_TABLE(of, mip4_of_match);
1546 #endif
1548 #ifdef CONFIG_ACPI
1549 static const struct acpi_device_id mip4_acpi_match[] = {
1550 { "MLFS0000", 0},
1551 { },
1553 MODULE_DEVICE_TABLE(acpi, mip4_acpi_match);
1554 #endif
1556 static const struct i2c_device_id mip4_i2c_ids[] = {
1557 { MIP4_DEVICE_NAME, 0 },
1558 { },
1560 MODULE_DEVICE_TABLE(i2c, mip4_i2c_ids);
1562 static struct i2c_driver mip4_driver = {
1563 .id_table = mip4_i2c_ids,
1564 .probe = mip4_probe,
1565 .driver = {
1566 .name = MIP4_DEVICE_NAME,
1567 .of_match_table = of_match_ptr(mip4_of_match),
1568 .acpi_match_table = ACPI_PTR(mip4_acpi_match),
1569 .pm = &mip4_pm_ops,
1572 module_i2c_driver(mip4_driver);
1574 MODULE_DESCRIPTION("MELFAS MIP4 Touchscreen");
1575 MODULE_VERSION("2016.09.28");
1576 MODULE_AUTHOR("Sangwon Jee <jeesw@melfas.com>");
1577 MODULE_LICENSE("GPL");