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>
29 #include <linux/slab.h>
30 #include <asm/unaligned.h>
32 #define MIP4_DEVICE_NAME "mip4_ts"
34 /*****************************************************************
36 * Version : MIP 4.0 Rev 4.6
37 *****************************************************************/
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
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
{
154 struct i2c_client
*client
;
155 struct input_dev
*input
;
156 struct gpio_desc
*gpio_ce
;
159 char product_name
[16];
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
,
193 .addr
= ts
->client
->addr
,
199 int retry
= I2C_RETRY_COUNT
;
204 res
= i2c_transfer(ts
->client
->adapter
, msg
, ARRAY_SIZE(msg
));
205 if (res
== ARRAY_SIZE(msg
))
208 error
= res
< 0 ? res
: -EIO
;
209 dev_err(&ts
->client
->dev
,
210 "%s - i2c_transfer failed: %d (%d)\n",
211 __func__
, error
, res
);
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
)];
234 error
= mip4_i2c_xfer(ts
, cmd
, sizeof(cmd
), buf
, sizeof(buf
));
236 memset(&ts
->fw_version
, 0xff, sizeof(ts
->fw_version
));
240 mip4_parse_fw_version(buf
, &ts
->fw_version
);
246 * Fetch device characteristics
248 static int mip4_query_device(struct mip4_ts
*ts
)
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
));
260 dev_warn(&ts
->client
->dev
,
261 "Failed to retrieve product name: %d\n", error
);
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
);
269 dev_warn(&ts
->client
->dev
,
270 "Failed to retrieve FW version: %d\n", error
);
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
);
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);
281 dev_warn(&ts
->client
->dev
,
282 "Failed to retrieve touchscreen parameters: %d\n",
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
);
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
);
299 dev_dbg(&ts
->client
->dev
, "ppm_x: %d, ppm_y: %d\n",
300 ts
->ppm_x
, ts
->ppm_y
);
303 if (ts
->node_key
> 0)
304 ts
->key_num
= ts
->node_key
;
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);
312 dev_warn(&ts
->client
->dev
,
313 "Failed to retrieve device type: %d\n", error
);
314 ts
->event_format
= 0xff;
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
);
329 static int mip4_power_on(struct mip4_ts
*ts
)
332 gpiod_set_value_cansleep(ts
->gpio_ce
, 1);
334 /* Booting delay : 200~300ms */
335 usleep_range(200 * 1000, 300 * 1000);
341 static void mip4_power_off(struct mip4_ts
*ts
)
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
)
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);
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
)
371 error
= mip4_power_on(ts
);
375 enable_irq(ts
->client
->irq
);
380 static void mip4_disable(struct mip4_ts
*ts
)
382 disable_irq(ts
->client
->irq
);
386 mip4_clear_input(ts
);
389 /*****************************************************************
391 *****************************************************************/
393 static void mip4_report_keys(struct mip4_ts
*ts
, u8
*packet
)
398 switch (ts
->event_format
) {
401 key
= packet
[0] & 0x0F;
402 down
= packet
[0] & 0x80;
407 key
= packet
[0] & 0x0F;
408 down
= packet
[1] & 0x01;
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",
420 input_event(ts
->input
, EV_MSC
, MSC_SCAN
, keycode
);
421 input_report_key(ts
->input
, keycode
, down
);
424 dev_err(&ts
->client
->dev
, "Unknown key: %d\n", key
);
428 static void mip4_report_touch(struct mip4_ts
*ts
, u8
*packet
)
435 u8 pressure_stage
= 0;
441 switch (ts
->event_format
) {
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) |
452 pressure
= packet
[4];
454 if (ts
->event_format
== 0) {
455 touch_major
= packet
[5];
456 touch_minor
= packet
[5];
458 touch_major
= packet
[6];
459 touch_minor
= packet
[7];
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) |
474 pressure_stage
= (packet
[7] & 0xF0) >> 4;
475 pressure
= ((packet
[7] & 0x0F) << 8) |
477 touch_major
= packet
[9];
478 touch_minor
= packet
[10];
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
);
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
);
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
)
510 switch (ts
->event_format
) {
513 type
= (packet
[0] & 0x40) >> 6;
517 type
= (packet
[0] & 0xF0) >> 4;
521 /* Should not happen unless we have corrupted firmware */
525 dev_dbg(&ts
->client
->dev
, "Type: %d\n", type
);
527 /* Report input event */
529 case MIP4_EVENT_INPUT_TYPE_KEY
:
530 mip4_report_keys(ts
, packet
);
533 case MIP4_EVENT_INPUT_TYPE_SCREEN
:
534 mip4_report_touch(ts
, packet
);
538 dev_err(&ts
->client
->dev
, "Unknown event type: %d\n", type
);
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
;
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);
560 dev_err(&client
->dev
,
561 "Failed to read packet info: %d\n", error
);
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
);
571 dev_err(&client
->dev
, "Empty packet\n");
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
);
580 dev_err(&client
->dev
,
581 "Failed to read packet data: %d\n", error
);
586 dev_dbg(&client
->dev
, "Alert: %d\n", ts
->buf
[0]);
588 for (i
= 0; i
< size
; i
+= ts
->event_size
) {
589 error
= mip4_handle_packet(ts
, &ts
->buf
[i
]);
594 input_sync(ts
->input
);
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
);
615 /*****************************************************************
617 *****************************************************************/
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
{
631 __le32 bin_start_addr
;
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
};
673 struct i2c_msg msg
[] = {
675 .addr
= ts
->client
->addr
,
680 .addr
= ts
->client
->addr
,
683 .len
= sizeof(result
),
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",
701 case MIP4_BOOT_STATUS_DONE
:
702 dev_dbg(&ts
->client
->dev
, "%s - done\n", __func__
);
705 case MIP4_BOOT_STATUS_ERROR
:
706 dev_err(&ts
->client
->dev
, "Bootloader failure\n");
709 case MIP4_BOOT_STATUS_BUSY
:
710 dev_dbg(&ts
->client
->dev
, "%s - Busy\n", __func__
);
715 dev_err(&ts
->client
->dev
,
716 "Unexpected bootloader status: %#02x\n",
722 usleep_range(1000, 2000);
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
};
736 struct i2c_msg msg
[] = {
738 .addr
= ts
->client
->addr
,
740 .buf
= mode_read_cmd
,
741 .len
= sizeof(mode_read_cmd
),
743 .addr
= ts
->client
->addr
,
746 .len
= sizeof(result
),
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",
765 dev_dbg(&ts
->client
->dev
,
766 "Sent mode change request (mode: %d)\n", mode
);
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
);
780 dev_dbg(&ts
->client
->dev
,
781 "Current device mode: %d, want: %d\n", result
, mode
);
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
,
818 .addr
= ts
->client
->addr
,
821 .len
= sizeof(result
),
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",
836 *buf_addr
= get_unaligned_le16(result
);
837 dev_dbg(&ts
->client
->dev
,
838 "Bootloader buffer address %#04x\n", *buf_addr
);
843 static int mip4_bl_program_page(struct mip4_ts
*ts
, int offset
,
844 const u8
*data
, int length
, u16 buf_addr
)
852 dev_dbg(&ts
->client
->dev
, "Writing page @%#06x (%d)\n",
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
);
861 data_buf
= kmalloc(2 + MIP4_BL_PACKET_SIZE
, GFP_KERNEL
);
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);
871 error
= ret
< 0 ? ret
: -EIO
;
872 dev_err(&ts
->client
->dev
,
873 "Failed to send write page address: %d\n", error
);
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);
883 error
= ret
< 0 ? ret
: -EIO
;
884 dev_err(&ts
->client
->dev
,
885 "Failed to send write page size: %d\n", error
);
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
);
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);
915 error
= ret
< 0 ? ret
: -EIO
;
916 dev_err(&ts
->client
->dev
,
917 "Failed to send 'write' command: %d\n", error
);
922 error
= mip4_bl_read_status(ts
);
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
)
935 struct i2c_msg msg
[] = {
937 .addr
= ts
->client
->addr
,
942 .addr
= ts
->client
->addr
,
944 .len
= MIP4_BL_PACKET_SIZE
,
950 dev_dbg(&ts
->client
->dev
, "Validating page @%#06x (%d)\n",
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);
959 error
= ret
< 0 ? ret
: -EIO
;
960 dev_err(&ts
->client
->dev
,
961 "Failed to send read page address: %d\n", error
);
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);
971 error
= ret
< 0 ? ret
: -EIO
;
972 dev_err(&ts
->client
->dev
,
973 "Failed to send read page size: %d\n", error
);
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);
983 error
= ret
< 0 ? ret
: -EIO
;
984 dev_err(&ts
->client
->dev
,
985 "Failed to send 'read' command: %d\n", error
);
990 error
= mip4_bl_read_status(ts
);
995 msg
[1].buf
= read_buf
= kmalloc(MIP4_BL_PACKET_SIZE
, GFP_KERNEL
);
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
);
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
,
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);
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
;
1050 /* Enter bootloader mode */
1051 dev_dbg(&client
->dev
, "Entering bootloader mode\n");
1053 error
= mip4_bl_enter(ts
);
1055 dev_err(&client
->dev
,
1056 "Failed to enter bootloader mode: %d\n",
1062 error
= mip4_bl_get_address(ts
, &buf_addr
);
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
) {
1075 error
= mip4_bl_program_page(ts
, offset
, fw_data
+ offset
,
1076 MIP4_BL_PAGE_SIZE
, buf_addr
);
1081 error
= mip4_bl_verify_page(ts
, offset
, fw_data
+ offset
,
1082 MIP4_BL_PAGE_SIZE
, buf_addr
);
1088 /* Exit bootloader mode */
1089 dev_dbg(&client
->dev
, "Exiting bootloader mode\n");
1091 error2
= mip4_bl_exit(ts
);
1093 dev_err(&client
->dev
,
1094 "Failed to exit bootloader mode: %d\n", error2
);
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
;
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
);
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);
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
);
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
);
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
);
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
);
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");
1202 dev_err(&ts
->client
->dev
, "Wrong firmware type\n");
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
;
1217 error
= mip4_parse_firmware(ts
, fw
,
1218 &fw_start_offset
, &fw_size
, &fw_info
);
1222 if (ts
->input
->users
) {
1223 disable_irq(ts
->client
->irq
);
1225 error
= mip4_power_on(ts
);
1230 /* Update firmware */
1232 error
= mip4_flash_fw(ts
, fw
->data
, fw_size
, fw_start_offset
);
1235 } while (--retires
);
1238 dev_err(&ts
->client
->dev
,
1239 "Failed to flash firmware: %d\n", error
);
1242 if (ts
->input
->users
)
1243 enable_irq(ts
->client
->irq
);
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
;
1259 error
= request_firmware(&fw
, MIP4_FW_NAME
, dev
);
1261 dev_err(&ts
->client
->dev
,
1262 "Failed to retrieve firmware %s: %d\n",
1263 MIP4_FW_NAME
, error
);
1268 * Take input mutex to prevent racing with itself and also with
1269 * userspace opening and closing the device and also suspend/resume
1272 mutex_lock(&ts
->input
->mutex
);
1274 error
= mip4_execute_fw_update(ts
, fw
);
1276 mutex_unlock(&ts
->input
->mutex
);
1278 release_firmware(fw
);
1281 dev_err(&ts
->client
->dev
,
1282 "Firmware update failed: %d\n", error
);
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
,
1295 struct i2c_client
*client
= to_i2c_client(dev
);
1296 struct mip4_ts
*ts
= i2c_get_clientdata(client
);
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
);
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
,
1317 struct i2c_client
*client
= to_i2c_client(dev
);
1318 struct mip4_ts
*ts
= i2c_get_clientdata(client
);
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
);
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
,
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
)
1359 struct input_dev
*input
;
1362 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
1363 dev_err(&client
->dev
, "Not supported I2C adapter\n");
1367 ts
= devm_kzalloc(&client
->dev
, sizeof(*ts
), GFP_KERNEL
);
1371 input
= devm_input_allocate_device(&client
->dev
);
1375 ts
->client
= client
;
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
);
1391 error
= mip4_power_on(ts
);
1394 error
= mip4_query_device(ts
);
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
);
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
);
1435 dev_err(&client
->dev
,
1436 "Failed to request interrupt %d: %d\n",
1437 client
->irq
, error
);
1441 disable_irq(client
->irq
);
1443 error
= input_register_device(input
);
1445 dev_err(&client
->dev
,
1446 "Failed to register input device: %d\n", error
);
1450 error
= sysfs_create_group(&client
->dev
.kobj
, &mip4_attr_group
);
1452 dev_err(&client
->dev
,
1453 "Failed to create sysfs attribute group: %d\n", error
);
1457 error
= devm_add_action(&client
->dev
, mip4_sysfs_remove
, ts
);
1459 mip4_sysfs_remove(ts
);
1460 dev_err(&client
->dev
,
1461 "Failed to install sysfs remoce action: %d\n", error
);
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
)
1481 mutex_unlock(&input
->mutex
);
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
)
1499 mutex_unlock(&input
->mutex
);
1504 static SIMPLE_DEV_PM_OPS(mip4_pm_ops
, mip4_suspend
, mip4_resume
);
1507 static const struct of_device_id mip4_of_match
[] = {
1508 { .compatible
= "melfas,"MIP4_DEVICE_NAME
, },
1511 MODULE_DEVICE_TABLE(of
, mip4_of_match
);
1515 static const struct acpi_device_id mip4_acpi_match
[] = {
1519 MODULE_DEVICE_TABLE(acpi
, mip4_acpi_match
);
1522 static const struct i2c_device_id mip4_i2c_ids
[] = {
1523 { MIP4_DEVICE_NAME
, 0 },
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
,
1532 .name
= MIP4_DEVICE_NAME
,
1533 .of_match_table
= of_match_ptr(mip4_of_match
),
1534 .acpi_match_table
= ACPI_PTR(mip4_acpi_match
),
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");