treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / input / touchscreen / melfas_mip4.c
blob247c3aaba2d8f30fbb3643d36c54e2e8e65e0e09
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * MELFAS MIP4 Touchscreen
5 * Copyright (C) 2016 MELFAS Inc.
7 * Author : Sangwon Jee <jeesw@melfas.com>
8 */
10 #include <linux/acpi.h>
11 #include <linux/delay.h>
12 #include <linux/firmware.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/i2c.h>
15 #include <linux/input.h>
16 #include <linux/input/mt.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/slab.h>
21 #include <asm/unaligned.h>
23 #define MIP4_DEVICE_NAME "mip4_ts"
25 /*****************************************************************
26 * Protocol
27 * Version : MIP 4.0 Rev 5.4
28 *****************************************************************/
30 /* Address */
31 #define MIP4_R0_BOOT 0x00
32 #define MIP4_R1_BOOT_MODE 0x01
33 #define MIP4_R1_BOOT_BUF_ADDR 0x10
34 #define MIP4_R1_BOOT_STATUS 0x20
35 #define MIP4_R1_BOOT_CMD 0x30
36 #define MIP4_R1_BOOT_TARGET_ADDR 0x40
37 #define MIP4_R1_BOOT_SIZE 0x44
39 #define MIP4_R0_INFO 0x01
40 #define MIP4_R1_INFO_PRODUCT_NAME 0x00
41 #define MIP4_R1_INFO_RESOLUTION_X 0x10
42 #define MIP4_R1_INFO_RESOLUTION_Y 0x12
43 #define MIP4_R1_INFO_NODE_NUM_X 0x14
44 #define MIP4_R1_INFO_NODE_NUM_Y 0x15
45 #define MIP4_R1_INFO_KEY_NUM 0x16
46 #define MIP4_R1_INFO_PRESSURE_NUM 0x17
47 #define MIP4_R1_INFO_LENGTH_X 0x18
48 #define MIP4_R1_INFO_LENGTH_Y 0x1A
49 #define MIP4_R1_INFO_PPM_X 0x1C
50 #define MIP4_R1_INFO_PPM_Y 0x1D
51 #define MIP4_R1_INFO_VERSION_BOOT 0x20
52 #define MIP4_R1_INFO_VERSION_CORE 0x22
53 #define MIP4_R1_INFO_VERSION_APP 0x24
54 #define MIP4_R1_INFO_VERSION_PARAM 0x26
55 #define MIP4_R1_INFO_SECT_BOOT_START 0x30
56 #define MIP4_R1_INFO_SECT_BOOT_END 0x31
57 #define MIP4_R1_INFO_SECT_CORE_START 0x32
58 #define MIP4_R1_INFO_SECT_CORE_END 0x33
59 #define MIP4_R1_INFO_SECT_APP_START 0x34
60 #define MIP4_R1_INFO_SECT_APP_END 0x35
61 #define MIP4_R1_INFO_SECT_PARAM_START 0x36
62 #define MIP4_R1_INFO_SECT_PARAM_END 0x37
63 #define MIP4_R1_INFO_BUILD_DATE 0x40
64 #define MIP4_R1_INFO_BUILD_TIME 0x44
65 #define MIP4_R1_INFO_CHECKSUM_PRECALC 0x48
66 #define MIP4_R1_INFO_CHECKSUM_REALTIME 0x4A
67 #define MIP4_R1_INFO_PROTOCOL_NAME 0x50
68 #define MIP4_R1_INFO_PROTOCOL_VERSION 0x58
69 #define MIP4_R1_INFO_IC_ID 0x70
70 #define MIP4_R1_INFO_IC_NAME 0x71
71 #define MIP4_R1_INFO_IC_VENDOR_ID 0x75
72 #define MIP4_R1_INFO_IC_HW_CATEGORY 0x77
73 #define MIP4_R1_INFO_CONTACT_THD_SCR 0x78
74 #define MIP4_R1_INFO_CONTACT_THD_KEY 0x7A
75 #define MIP4_R1_INFO_PID 0x7C
76 #define MIP4_R1_INFO_VID 0x7E
77 #define MIP4_R1_INFO_SLAVE_ADDR 0x80
79 #define MIP4_R0_EVENT 0x02
80 #define MIP4_R1_EVENT_SUPPORTED_FUNC 0x00
81 #define MIP4_R1_EVENT_FORMAT 0x04
82 #define MIP4_R1_EVENT_SIZE 0x06
83 #define MIP4_R1_EVENT_PACKET_INFO 0x10
84 #define MIP4_R1_EVENT_PACKET_DATA 0x11
86 #define MIP4_R0_CTRL 0x06
87 #define MIP4_R1_CTRL_READY_STATUS 0x00
88 #define MIP4_R1_CTRL_EVENT_READY 0x01
89 #define MIP4_R1_CTRL_MODE 0x10
90 #define MIP4_R1_CTRL_EVENT_TRIGGER_TYPE 0x11
91 #define MIP4_R1_CTRL_RECALIBRATE 0x12
92 #define MIP4_R1_CTRL_POWER_STATE 0x13
93 #define MIP4_R1_CTRL_GESTURE_TYPE 0x14
94 #define MIP4_R1_CTRL_DISABLE_ESD_ALERT 0x18
95 #define MIP4_R1_CTRL_CHARGER_MODE 0x19
96 #define MIP4_R1_CTRL_HIGH_SENS_MODE 0x1A
97 #define MIP4_R1_CTRL_WINDOW_MODE 0x1B
98 #define MIP4_R1_CTRL_PALM_REJECTION 0x1C
99 #define MIP4_R1_CTRL_EDGE_CORRECTION 0x1D
100 #define MIP4_R1_CTRL_ENTER_GLOVE_MODE 0x1E
101 #define MIP4_R1_CTRL_I2C_ON_LPM 0x1F
102 #define MIP4_R1_CTRL_GESTURE_DEBUG 0x20
103 #define MIP4_R1_CTRL_PALM_EVENT 0x22
104 #define MIP4_R1_CTRL_PROXIMITY_SENSING 0x23
106 /* Value */
107 #define MIP4_BOOT_MODE_BOOT 0x01
108 #define MIP4_BOOT_MODE_APP 0x02
110 #define MIP4_BOOT_STATUS_BUSY 0x05
111 #define MIP4_BOOT_STATUS_ERROR 0x0E
112 #define MIP4_BOOT_STATUS_DONE 0xA0
114 #define MIP4_BOOT_CMD_MASS_ERASE 0x15
115 #define MIP4_BOOT_CMD_PROGRAM 0x54
116 #define MIP4_BOOT_CMD_ERASE 0x8F
117 #define MIP4_BOOT_CMD_WRITE 0xA5
118 #define MIP4_BOOT_CMD_READ 0xC2
120 #define MIP4_EVENT_INPUT_TYPE_KEY 0
121 #define MIP4_EVENT_INPUT_TYPE_SCREEN 1
122 #define MIP4_EVENT_INPUT_TYPE_PROXIMITY 2
124 #define I2C_RETRY_COUNT 3 /* 2~ */
126 #define MIP4_BUF_SIZE 128
127 #define MIP4_MAX_FINGERS 10
128 #define MIP4_MAX_KEYS 4
130 #define MIP4_TOUCH_MAJOR_MIN 0
131 #define MIP4_TOUCH_MAJOR_MAX 255
132 #define MIP4_TOUCH_MINOR_MIN 0
133 #define MIP4_TOUCH_MINOR_MAX 255
134 #define MIP4_PRESSURE_MIN 0
135 #define MIP4_PRESSURE_MAX 255
137 #define MIP4_FW_NAME "melfas_mip4.fw"
138 #define MIP4_FW_UPDATE_DEBUG 0 /* 0 (default) or 1 */
140 struct mip4_fw_version {
141 u16 boot;
142 u16 core;
143 u16 app;
144 u16 param;
147 struct mip4_ts {
148 struct i2c_client *client;
149 struct input_dev *input;
150 struct gpio_desc *gpio_ce;
152 char phys[32];
153 char product_name[16];
154 u16 product_id;
155 char ic_name[4];
156 char fw_name[32];
158 unsigned int max_x;
159 unsigned int max_y;
160 u8 node_x;
161 u8 node_y;
162 u8 node_key;
163 unsigned int ppm_x;
164 unsigned int ppm_y;
166 struct mip4_fw_version fw_version;
168 unsigned int event_size;
169 unsigned int event_format;
171 unsigned int key_num;
172 unsigned short key_code[MIP4_MAX_KEYS];
174 bool wake_irq_enabled;
176 u8 buf[MIP4_BUF_SIZE];
179 static int mip4_i2c_xfer(struct mip4_ts *ts,
180 char *write_buf, unsigned int write_len,
181 char *read_buf, unsigned int read_len)
183 struct i2c_msg msg[] = {
185 .addr = ts->client->addr,
186 .flags = 0,
187 .buf = write_buf,
188 .len = write_len,
189 }, {
190 .addr = ts->client->addr,
191 .flags = I2C_M_RD,
192 .buf = read_buf,
193 .len = read_len,
196 int retry = I2C_RETRY_COUNT;
197 int res;
198 int error;
200 do {
201 res = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
202 if (res == ARRAY_SIZE(msg))
203 return 0;
205 error = res < 0 ? res : -EIO;
206 dev_err(&ts->client->dev,
207 "%s - i2c_transfer failed: %d (%d)\n",
208 __func__, error, res);
209 } while (--retry);
211 return error;
214 static void mip4_parse_fw_version(const u8 *buf, struct mip4_fw_version *v)
216 v->boot = get_unaligned_le16(buf + 0);
217 v->core = get_unaligned_le16(buf + 2);
218 v->app = get_unaligned_le16(buf + 4);
219 v->param = get_unaligned_le16(buf + 6);
223 * Read chip firmware version
225 static int mip4_get_fw_version(struct mip4_ts *ts)
227 u8 cmd[] = { MIP4_R0_INFO, MIP4_R1_INFO_VERSION_BOOT };
228 u8 buf[sizeof(ts->fw_version)];
229 int error;
231 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, sizeof(buf));
232 if (error) {
233 memset(&ts->fw_version, 0xff, sizeof(ts->fw_version));
234 return error;
237 mip4_parse_fw_version(buf, &ts->fw_version);
239 return 0;
243 * Fetch device characteristics
245 static int mip4_query_device(struct mip4_ts *ts)
247 union i2c_smbus_data dummy;
248 int error;
249 u8 cmd[2];
250 u8 buf[14];
253 * Make sure there is something at this address as we do not
254 * consider subsequent failures as fatal.
256 if (i2c_smbus_xfer(ts->client->adapter, ts->client->addr,
257 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
258 dev_err(&ts->client->dev, "nothing at this address\n");
259 return -ENXIO;
262 /* Product name */
263 cmd[0] = MIP4_R0_INFO;
264 cmd[1] = MIP4_R1_INFO_PRODUCT_NAME;
265 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd),
266 ts->product_name, sizeof(ts->product_name));
267 if (error)
268 dev_warn(&ts->client->dev,
269 "Failed to retrieve product name: %d\n", error);
270 else
271 dev_dbg(&ts->client->dev, "product name: %.*s\n",
272 (int)sizeof(ts->product_name), ts->product_name);
274 /* Product ID */
275 cmd[0] = MIP4_R0_INFO;
276 cmd[1] = MIP4_R1_INFO_PID;
277 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 2);
278 if (error) {
279 dev_warn(&ts->client->dev,
280 "Failed to retrieve product id: %d\n", error);
281 } else {
282 ts->product_id = get_unaligned_le16(&buf[0]);
283 dev_dbg(&ts->client->dev, "product id: %04X\n", ts->product_id);
286 /* Firmware name */
287 snprintf(ts->fw_name, sizeof(ts->fw_name),
288 "melfas_mip4_%04X.fw", ts->product_id);
289 dev_dbg(&ts->client->dev, "firmware name: %s\n", ts->fw_name);
291 /* IC name */
292 cmd[0] = MIP4_R0_INFO;
293 cmd[1] = MIP4_R1_INFO_IC_NAME;
294 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd),
295 ts->ic_name, sizeof(ts->ic_name));
296 if (error)
297 dev_warn(&ts->client->dev,
298 "Failed to retrieve IC name: %d\n", error);
299 else
300 dev_dbg(&ts->client->dev, "IC name: %.*s\n",
301 (int)sizeof(ts->ic_name), ts->ic_name);
303 /* Firmware version */
304 error = mip4_get_fw_version(ts);
305 if (error)
306 dev_warn(&ts->client->dev,
307 "Failed to retrieve FW version: %d\n", error);
308 else
309 dev_dbg(&ts->client->dev, "F/W Version: %04X %04X %04X %04X\n",
310 ts->fw_version.boot, ts->fw_version.core,
311 ts->fw_version.app, ts->fw_version.param);
313 /* Resolution */
314 cmd[0] = MIP4_R0_INFO;
315 cmd[1] = MIP4_R1_INFO_RESOLUTION_X;
316 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 14);
317 if (error) {
318 dev_warn(&ts->client->dev,
319 "Failed to retrieve touchscreen parameters: %d\n",
320 error);
321 } else {
322 ts->max_x = get_unaligned_le16(&buf[0]);
323 ts->max_y = get_unaligned_le16(&buf[2]);
324 dev_dbg(&ts->client->dev, "max_x: %d, max_y: %d\n",
325 ts->max_x, ts->max_y);
327 ts->node_x = buf[4];
328 ts->node_y = buf[5];
329 ts->node_key = buf[6];
330 dev_dbg(&ts->client->dev,
331 "node_x: %d, node_y: %d, node_key: %d\n",
332 ts->node_x, ts->node_y, ts->node_key);
334 ts->ppm_x = buf[12];
335 ts->ppm_y = buf[13];
336 dev_dbg(&ts->client->dev, "ppm_x: %d, ppm_y: %d\n",
337 ts->ppm_x, ts->ppm_y);
339 /* Key ts */
340 if (ts->node_key > 0)
341 ts->key_num = ts->node_key;
344 /* Protocol */
345 cmd[0] = MIP4_R0_EVENT;
346 cmd[1] = MIP4_R1_EVENT_SUPPORTED_FUNC;
347 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 7);
348 if (error) {
349 dev_warn(&ts->client->dev,
350 "Failed to retrieve device type: %d\n", error);
351 ts->event_format = 0xff;
352 } else {
353 ts->event_format = get_unaligned_le16(&buf[4]);
354 ts->event_size = buf[6];
355 dev_dbg(&ts->client->dev, "event_format: %d, event_size: %d\n",
356 ts->event_format, ts->event_size);
358 if (ts->event_format == 2 || ts->event_format > 3)
359 dev_warn(&ts->client->dev,
360 "Unknown event format %d\n", ts->event_format);
363 return 0;
366 static int mip4_power_on(struct mip4_ts *ts)
368 if (ts->gpio_ce) {
369 gpiod_set_value_cansleep(ts->gpio_ce, 1);
371 /* Booting delay : 200~300ms */
372 usleep_range(200 * 1000, 300 * 1000);
375 return 0;
378 static void mip4_power_off(struct mip4_ts *ts)
380 if (ts->gpio_ce)
381 gpiod_set_value_cansleep(ts->gpio_ce, 0);
385 * Clear touch input event status
387 static void mip4_clear_input(struct mip4_ts *ts)
389 int i;
391 /* Screen */
392 for (i = 0; i < MIP4_MAX_FINGERS; i++) {
393 input_mt_slot(ts->input, i);
394 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 0);
397 /* Keys */
398 for (i = 0; i < ts->key_num; i++)
399 input_report_key(ts->input, ts->key_code[i], 0);
401 input_sync(ts->input);
404 static int mip4_enable(struct mip4_ts *ts)
406 int error;
408 error = mip4_power_on(ts);
409 if (error)
410 return error;
412 enable_irq(ts->client->irq);
414 return 0;
417 static void mip4_disable(struct mip4_ts *ts)
419 disable_irq(ts->client->irq);
421 mip4_power_off(ts);
423 mip4_clear_input(ts);
426 /*****************************************************************
427 * Input handling
428 *****************************************************************/
430 static void mip4_report_keys(struct mip4_ts *ts, u8 *packet)
432 u8 key;
433 bool down;
435 switch (ts->event_format) {
436 case 0:
437 case 1:
438 key = packet[0] & 0x0F;
439 down = packet[0] & 0x80;
440 break;
442 case 3:
443 default:
444 key = packet[0] & 0x0F;
445 down = packet[1] & 0x01;
446 break;
449 /* Report key event */
450 if (key >= 1 && key <= ts->key_num) {
451 unsigned short keycode = ts->key_code[key - 1];
453 dev_dbg(&ts->client->dev,
454 "Key - ID: %d, keycode: %d, state: %d\n",
455 key, keycode, down);
457 input_event(ts->input, EV_MSC, MSC_SCAN, keycode);
458 input_report_key(ts->input, keycode, down);
460 } else {
461 dev_err(&ts->client->dev, "Unknown key: %d\n", key);
465 static void mip4_report_touch(struct mip4_ts *ts, u8 *packet)
467 int id;
468 bool hover;
469 bool palm;
470 bool state;
471 u16 x, y;
472 u8 pressure_stage = 0;
473 u8 pressure;
474 u8 size;
475 u8 touch_major;
476 u8 touch_minor;
478 switch (ts->event_format) {
479 case 0:
480 case 1:
481 /* Touch only */
482 state = packet[0] & BIT(7);
483 hover = packet[0] & BIT(5);
484 palm = packet[0] & BIT(4);
485 id = (packet[0] & 0x0F) - 1;
486 x = ((packet[1] & 0x0F) << 8) | packet[2];
487 y = (((packet[1] >> 4) & 0x0F) << 8) |
488 packet[3];
489 pressure = packet[4];
490 size = packet[5];
491 if (ts->event_format == 0) {
492 touch_major = packet[5];
493 touch_minor = packet[5];
494 } else {
495 touch_major = packet[6];
496 touch_minor = packet[7];
498 break;
500 case 3:
501 default:
502 /* Touch + Force(Pressure) */
503 id = (packet[0] & 0x0F) - 1;
504 hover = packet[1] & BIT(2);
505 palm = packet[1] & BIT(1);
506 state = packet[1] & BIT(0);
507 x = ((packet[2] & 0x0F) << 8) | packet[3];
508 y = (((packet[2] >> 4) & 0x0F) << 8) |
509 packet[4];
510 size = packet[6];
511 pressure_stage = (packet[7] & 0xF0) >> 4;
512 pressure = ((packet[7] & 0x0F) << 8) |
513 packet[8];
514 touch_major = packet[9];
515 touch_minor = packet[10];
516 break;
519 dev_dbg(&ts->client->dev,
520 "Screen - Slot: %d State: %d X: %04d Y: %04d Z: %d\n",
521 id, state, x, y, pressure);
523 if (unlikely(id < 0 || id >= MIP4_MAX_FINGERS)) {
524 dev_err(&ts->client->dev, "Screen - invalid slot ID: %d\n", id);
525 } else if (state) {
526 /* Press or Move event */
527 input_mt_slot(ts->input, id);
528 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
529 input_report_abs(ts->input, ABS_MT_POSITION_X, x);
530 input_report_abs(ts->input, ABS_MT_POSITION_Y, y);
531 input_report_abs(ts->input, ABS_MT_PRESSURE, pressure);
532 input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, touch_major);
533 input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, touch_minor);
534 } else {
535 /* Release event */
536 input_mt_slot(ts->input, id);
537 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 0);
540 input_mt_sync_frame(ts->input);
543 static int mip4_handle_packet(struct mip4_ts *ts, u8 *packet)
545 u8 type;
547 switch (ts->event_format) {
548 case 0:
549 case 1:
550 type = (packet[0] & 0x40) >> 6;
551 break;
553 case 3:
554 type = (packet[0] & 0xF0) >> 4;
555 break;
557 default:
558 /* Should not happen unless we have corrupted firmware */
559 return -EINVAL;
562 dev_dbg(&ts->client->dev, "Type: %d\n", type);
564 /* Report input event */
565 switch (type) {
566 case MIP4_EVENT_INPUT_TYPE_KEY:
567 mip4_report_keys(ts, packet);
568 break;
570 case MIP4_EVENT_INPUT_TYPE_SCREEN:
571 mip4_report_touch(ts, packet);
572 break;
574 default:
575 dev_err(&ts->client->dev, "Unknown event type: %d\n", type);
576 break;
579 return 0;
582 static irqreturn_t mip4_interrupt(int irq, void *dev_id)
584 struct mip4_ts *ts = dev_id;
585 struct i2c_client *client = ts->client;
586 unsigned int i;
587 int error;
588 u8 cmd[2];
589 u8 size;
590 bool alert;
592 /* Read packet info */
593 cmd[0] = MIP4_R0_EVENT;
594 cmd[1] = MIP4_R1_EVENT_PACKET_INFO;
595 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), ts->buf, 1);
596 if (error) {
597 dev_err(&client->dev,
598 "Failed to read packet info: %d\n", error);
599 goto out;
602 size = ts->buf[0] & 0x7F;
603 alert = ts->buf[0] & BIT(7);
604 dev_dbg(&client->dev, "packet size: %d, alert: %d\n", size, alert);
606 /* Check size */
607 if (!size) {
608 dev_err(&client->dev, "Empty packet\n");
609 goto out;
612 /* Read packet data */
613 cmd[0] = MIP4_R0_EVENT;
614 cmd[1] = MIP4_R1_EVENT_PACKET_DATA;
615 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), ts->buf, size);
616 if (error) {
617 dev_err(&client->dev,
618 "Failed to read packet data: %d\n", error);
619 goto out;
622 if (alert) {
623 dev_dbg(&client->dev, "Alert: %d\n", ts->buf[0]);
624 } else {
625 for (i = 0; i < size; i += ts->event_size) {
626 error = mip4_handle_packet(ts, &ts->buf[i]);
627 if (error)
628 break;
631 input_sync(ts->input);
634 out:
635 return IRQ_HANDLED;
638 static int mip4_input_open(struct input_dev *dev)
640 struct mip4_ts *ts = input_get_drvdata(dev);
642 return mip4_enable(ts);
645 static void mip4_input_close(struct input_dev *dev)
647 struct mip4_ts *ts = input_get_drvdata(dev);
649 mip4_disable(ts);
652 /*****************************************************************
653 * Firmware update
654 *****************************************************************/
656 /* Firmware Info */
657 #define MIP4_BL_PAGE_SIZE 512 /* 512 */
658 #define MIP4_BL_PACKET_SIZE 512 /* 512, 256, 128, 64, ... */
661 * Firmware binary tail info
664 struct mip4_bin_tail {
665 u8 tail_mark[4];
666 u8 chip_name[4];
668 __le32 bin_start_addr;
669 __le32 bin_length;
671 __le16 ver_boot;
672 __le16 ver_core;
673 __le16 ver_app;
674 __le16 ver_param;
676 u8 boot_start;
677 u8 boot_end;
678 u8 core_start;
679 u8 core_end;
680 u8 app_start;
681 u8 app_end;
682 u8 param_start;
683 u8 param_end;
685 u8 checksum_type;
686 u8 hw_category;
688 __le16 param_id;
689 __le32 param_length;
690 __le32 build_date;
691 __le32 build_time;
693 __le32 reserved1;
694 __le32 reserved2;
695 __le16 reserved3;
696 __le16 tail_size;
697 __le32 crc;
698 } __packed;
700 #define MIP4_BIN_TAIL_MARK "MBT\001"
701 #define MIP4_BIN_TAIL_SIZE (sizeof(struct mip4_bin_tail))
704 * Bootloader - Read status
706 static int mip4_bl_read_status(struct mip4_ts *ts)
708 u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_STATUS };
709 u8 result;
710 struct i2c_msg msg[] = {
712 .addr = ts->client->addr,
713 .flags = 0,
714 .buf = cmd,
715 .len = sizeof(cmd),
716 }, {
717 .addr = ts->client->addr,
718 .flags = I2C_M_RD,
719 .buf = &result,
720 .len = sizeof(result),
723 int ret;
724 int error;
725 int retry = 1000;
727 do {
728 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
729 if (ret != ARRAY_SIZE(msg)) {
730 error = ret < 0 ? ret : -EIO;
731 dev_err(&ts->client->dev,
732 "Failed to read bootloader status: %d\n",
733 error);
734 return error;
737 switch (result) {
738 case MIP4_BOOT_STATUS_DONE:
739 dev_dbg(&ts->client->dev, "%s - done\n", __func__);
740 return 0;
742 case MIP4_BOOT_STATUS_ERROR:
743 dev_err(&ts->client->dev, "Bootloader failure\n");
744 return -EIO;
746 case MIP4_BOOT_STATUS_BUSY:
747 dev_dbg(&ts->client->dev, "%s - Busy\n", __func__);
748 error = -EBUSY;
749 break;
751 default:
752 dev_err(&ts->client->dev,
753 "Unexpected bootloader status: %#02x\n",
754 result);
755 error = -EINVAL;
756 break;
759 usleep_range(1000, 2000);
760 } while (--retry);
762 return error;
766 * Bootloader - Change mode
768 static int mip4_bl_change_mode(struct mip4_ts *ts, u8 mode)
770 u8 mode_chg_cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_MODE, mode };
771 u8 mode_read_cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_MODE };
772 u8 result;
773 struct i2c_msg msg[] = {
775 .addr = ts->client->addr,
776 .flags = 0,
777 .buf = mode_read_cmd,
778 .len = sizeof(mode_read_cmd),
779 }, {
780 .addr = ts->client->addr,
781 .flags = I2C_M_RD,
782 .buf = &result,
783 .len = sizeof(result),
786 int retry = 10;
787 int ret;
788 int error;
790 do {
791 /* Send mode change command */
792 ret = i2c_master_send(ts->client,
793 mode_chg_cmd, sizeof(mode_chg_cmd));
794 if (ret != sizeof(mode_chg_cmd)) {
795 error = ret < 0 ? ret : -EIO;
796 dev_err(&ts->client->dev,
797 "Failed to send %d mode change: %d (%d)\n",
798 mode, error, ret);
799 return error;
802 dev_dbg(&ts->client->dev,
803 "Sent mode change request (mode: %d)\n", mode);
805 /* Wait */
806 msleep(1000);
808 /* Verify target mode */
809 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
810 if (ret != ARRAY_SIZE(msg)) {
811 error = ret < 0 ? ret : -EIO;
812 dev_err(&ts->client->dev,
813 "Failed to read device mode: %d\n", error);
814 return error;
817 dev_dbg(&ts->client->dev,
818 "Current device mode: %d, want: %d\n", result, mode);
820 if (result == mode)
821 return 0;
823 } while (--retry);
825 return -EIO;
829 * Bootloader - Start bootloader mode
831 static int mip4_bl_enter(struct mip4_ts *ts)
833 return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_BOOT);
837 * Bootloader - Exit bootloader mode
839 static int mip4_bl_exit(struct mip4_ts *ts)
841 return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_APP);
844 static int mip4_bl_get_address(struct mip4_ts *ts, u16 *buf_addr)
846 u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_BUF_ADDR };
847 u8 result[sizeof(u16)];
848 struct i2c_msg msg[] = {
850 .addr = ts->client->addr,
851 .flags = 0,
852 .buf = cmd,
853 .len = sizeof(cmd),
854 }, {
855 .addr = ts->client->addr,
856 .flags = I2C_M_RD,
857 .buf = result,
858 .len = sizeof(result),
861 int ret;
862 int error;
864 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
865 if (ret != ARRAY_SIZE(msg)) {
866 error = ret < 0 ? ret : -EIO;
867 dev_err(&ts->client->dev,
868 "Failed to retrieve bootloader buffer address: %d\n",
869 error);
870 return error;
873 *buf_addr = get_unaligned_le16(result);
874 dev_dbg(&ts->client->dev,
875 "Bootloader buffer address %#04x\n", *buf_addr);
877 return 0;
880 static int mip4_bl_program_page(struct mip4_ts *ts, int offset,
881 const u8 *data, int length, u16 buf_addr)
883 u8 cmd[6];
884 u8 *data_buf;
885 u16 buf_offset;
886 int ret;
887 int error;
889 dev_dbg(&ts->client->dev, "Writing page @%#06x (%d)\n",
890 offset, length);
892 if (length > MIP4_BL_PAGE_SIZE || length % MIP4_BL_PACKET_SIZE) {
893 dev_err(&ts->client->dev,
894 "Invalid page length: %d\n", length);
895 return -EINVAL;
898 data_buf = kmalloc(2 + MIP4_BL_PACKET_SIZE, GFP_KERNEL);
899 if (!data_buf)
900 return -ENOMEM;
902 /* Addr */
903 cmd[0] = MIP4_R0_BOOT;
904 cmd[1] = MIP4_R1_BOOT_TARGET_ADDR;
905 put_unaligned_le32(offset, &cmd[2]);
906 ret = i2c_master_send(ts->client, cmd, 6);
907 if (ret != 6) {
908 error = ret < 0 ? ret : -EIO;
909 dev_err(&ts->client->dev,
910 "Failed to send write page address: %d\n", error);
911 goto out;
914 /* Size */
915 cmd[0] = MIP4_R0_BOOT;
916 cmd[1] = MIP4_R1_BOOT_SIZE;
917 put_unaligned_le32(length, &cmd[2]);
918 ret = i2c_master_send(ts->client, cmd, 6);
919 if (ret != 6) {
920 error = ret < 0 ? ret : -EIO;
921 dev_err(&ts->client->dev,
922 "Failed to send write page size: %d\n", error);
923 goto out;
926 /* Data */
927 for (buf_offset = 0;
928 buf_offset < length;
929 buf_offset += MIP4_BL_PACKET_SIZE) {
930 dev_dbg(&ts->client->dev,
931 "writing chunk at %#04x (size %d)\n",
932 buf_offset, MIP4_BL_PACKET_SIZE);
933 put_unaligned_be16(buf_addr + buf_offset, data_buf);
934 memcpy(&data_buf[2], &data[buf_offset], MIP4_BL_PACKET_SIZE);
935 ret = i2c_master_send(ts->client,
936 data_buf, 2 + MIP4_BL_PACKET_SIZE);
937 if (ret != 2 + MIP4_BL_PACKET_SIZE) {
938 error = ret < 0 ? ret : -EIO;
939 dev_err(&ts->client->dev,
940 "Failed to read chunk at %#04x (size %d): %d\n",
941 buf_offset, MIP4_BL_PACKET_SIZE, error);
942 goto out;
946 /* Command */
947 cmd[0] = MIP4_R0_BOOT;
948 cmd[1] = MIP4_R1_BOOT_CMD;
949 cmd[2] = MIP4_BOOT_CMD_PROGRAM;
950 ret = i2c_master_send(ts->client, cmd, 3);
951 if (ret != 3) {
952 error = ret < 0 ? ret : -EIO;
953 dev_err(&ts->client->dev,
954 "Failed to send 'write' command: %d\n", error);
955 goto out;
958 /* Status */
959 error = mip4_bl_read_status(ts);
961 out:
962 kfree(data_buf);
963 return error ? error : 0;
966 static int mip4_bl_verify_page(struct mip4_ts *ts, int offset,
967 const u8 *data, int length, int buf_addr)
969 u8 cmd[8];
970 u8 *read_buf;
971 int buf_offset;
972 struct i2c_msg msg[] = {
974 .addr = ts->client->addr,
975 .flags = 0,
976 .buf = cmd,
977 .len = 2,
978 }, {
979 .addr = ts->client->addr,
980 .flags = I2C_M_RD,
981 .len = MIP4_BL_PACKET_SIZE,
984 int ret;
985 int error;
987 dev_dbg(&ts->client->dev, "Validating page @%#06x (%d)\n",
988 offset, length);
990 /* Addr */
991 cmd[0] = MIP4_R0_BOOT;
992 cmd[1] = MIP4_R1_BOOT_TARGET_ADDR;
993 put_unaligned_le32(offset, &cmd[2]);
994 ret = i2c_master_send(ts->client, cmd, 6);
995 if (ret != 6) {
996 error = ret < 0 ? ret : -EIO;
997 dev_err(&ts->client->dev,
998 "Failed to send read page address: %d\n", error);
999 return error;
1002 /* Size */
1003 cmd[0] = MIP4_R0_BOOT;
1004 cmd[1] = MIP4_R1_BOOT_SIZE;
1005 put_unaligned_le32(length, &cmd[2]);
1006 ret = i2c_master_send(ts->client, cmd, 6);
1007 if (ret != 6) {
1008 error = ret < 0 ? ret : -EIO;
1009 dev_err(&ts->client->dev,
1010 "Failed to send read page size: %d\n", error);
1011 return error;
1014 /* Command */
1015 cmd[0] = MIP4_R0_BOOT;
1016 cmd[1] = MIP4_R1_BOOT_CMD;
1017 cmd[2] = MIP4_BOOT_CMD_READ;
1018 ret = i2c_master_send(ts->client, cmd, 3);
1019 if (ret != 3) {
1020 error = ret < 0 ? ret : -EIO;
1021 dev_err(&ts->client->dev,
1022 "Failed to send 'read' command: %d\n", error);
1023 return error;
1026 /* Status */
1027 error = mip4_bl_read_status(ts);
1028 if (error)
1029 return error;
1031 /* Read */
1032 msg[1].buf = read_buf = kmalloc(MIP4_BL_PACKET_SIZE, GFP_KERNEL);
1033 if (!read_buf)
1034 return -ENOMEM;
1036 for (buf_offset = 0;
1037 buf_offset < length;
1038 buf_offset += MIP4_BL_PACKET_SIZE) {
1039 dev_dbg(&ts->client->dev,
1040 "reading chunk at %#04x (size %d)\n",
1041 buf_offset, MIP4_BL_PACKET_SIZE);
1042 put_unaligned_be16(buf_addr + buf_offset, cmd);
1043 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
1044 if (ret != ARRAY_SIZE(msg)) {
1045 error = ret < 0 ? ret : -EIO;
1046 dev_err(&ts->client->dev,
1047 "Failed to read chunk at %#04x (size %d): %d\n",
1048 buf_offset, MIP4_BL_PACKET_SIZE, error);
1049 break;
1052 if (memcmp(&data[buf_offset], read_buf, MIP4_BL_PACKET_SIZE)) {
1053 dev_err(&ts->client->dev,
1054 "Failed to validate chunk at %#04x (size %d)\n",
1055 buf_offset, MIP4_BL_PACKET_SIZE);
1056 #if MIP4_FW_UPDATE_DEBUG
1057 print_hex_dump(KERN_DEBUG,
1058 MIP4_DEVICE_NAME " F/W File: ",
1059 DUMP_PREFIX_OFFSET, 16, 1,
1060 data + offset, MIP4_BL_PACKET_SIZE,
1061 false);
1062 print_hex_dump(KERN_DEBUG,
1063 MIP4_DEVICE_NAME " F/W Chip: ",
1064 DUMP_PREFIX_OFFSET, 16, 1,
1065 read_buf, MIP4_BL_PAGE_SIZE, false);
1066 #endif
1067 error = -EINVAL;
1068 break;
1072 kfree(read_buf);
1073 return error ? error : 0;
1077 * Flash chip firmware
1079 static int mip4_flash_fw(struct mip4_ts *ts,
1080 const u8 *fw_data, u32 fw_size, u32 fw_offset)
1082 struct i2c_client *client = ts->client;
1083 int offset;
1084 u16 buf_addr;
1085 int error, error2;
1087 /* Enter bootloader mode */
1088 dev_dbg(&client->dev, "Entering bootloader mode\n");
1090 error = mip4_bl_enter(ts);
1091 if (error) {
1092 dev_err(&client->dev,
1093 "Failed to enter bootloader mode: %d\n",
1094 error);
1095 return error;
1098 /* Read info */
1099 error = mip4_bl_get_address(ts, &buf_addr);
1100 if (error)
1101 goto exit_bl;
1103 /* Program & Verify */
1104 dev_dbg(&client->dev,
1105 "Program & Verify, page size: %d, packet size: %d\n",
1106 MIP4_BL_PAGE_SIZE, MIP4_BL_PACKET_SIZE);
1108 for (offset = fw_offset;
1109 offset < fw_offset + fw_size;
1110 offset += MIP4_BL_PAGE_SIZE) {
1111 /* Program */
1112 error = mip4_bl_program_page(ts, offset, fw_data + offset,
1113 MIP4_BL_PAGE_SIZE, buf_addr);
1114 if (error)
1115 break;
1117 /* Verify */
1118 error = mip4_bl_verify_page(ts, offset, fw_data + offset,
1119 MIP4_BL_PAGE_SIZE, buf_addr);
1120 if (error)
1121 break;
1124 exit_bl:
1125 /* Exit bootloader mode */
1126 dev_dbg(&client->dev, "Exiting bootloader mode\n");
1128 error2 = mip4_bl_exit(ts);
1129 if (error2) {
1130 dev_err(&client->dev,
1131 "Failed to exit bootloader mode: %d\n", error2);
1132 if (!error)
1133 error = error2;
1136 /* Reset chip */
1137 mip4_power_off(ts);
1138 mip4_power_on(ts);
1140 mip4_query_device(ts);
1142 /* Refresh device parameters */
1143 input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0);
1144 input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0);
1145 input_set_abs_params(ts->input, ABS_X, 0, ts->max_x, 0, 0);
1146 input_set_abs_params(ts->input, ABS_Y, 0, ts->max_y, 0, 0);
1147 input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->ppm_x);
1148 input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->ppm_y);
1149 input_abs_set_res(ts->input, ABS_X, ts->ppm_x);
1150 input_abs_set_res(ts->input, ABS_Y, ts->ppm_y);
1152 return error ? error : 0;
1155 static int mip4_parse_firmware(struct mip4_ts *ts, const struct firmware *fw,
1156 u32 *fw_offset_start, u32 *fw_size,
1157 const struct mip4_bin_tail **pfw_info)
1159 const struct mip4_bin_tail *fw_info;
1160 struct mip4_fw_version fw_version;
1161 u16 tail_size;
1163 if (fw->size < MIP4_BIN_TAIL_SIZE) {
1164 dev_err(&ts->client->dev,
1165 "Invalid firmware, size mismatch (tail %zd vs %zd)\n",
1166 MIP4_BIN_TAIL_SIZE, fw->size);
1167 return -EINVAL;
1170 fw_info = (const void *)&fw->data[fw->size - MIP4_BIN_TAIL_SIZE];
1172 #if MIP4_FW_UPDATE_DEBUG
1173 print_hex_dump(KERN_ERR, MIP4_DEVICE_NAME " Bin Info: ",
1174 DUMP_PREFIX_OFFSET, 16, 1, *fw_info, tail_size, false);
1175 #endif
1177 tail_size = get_unaligned_le16(&fw_info->tail_size);
1178 if (tail_size != MIP4_BIN_TAIL_SIZE) {
1179 dev_err(&ts->client->dev,
1180 "wrong tail size: %d (expected %zd)\n",
1181 tail_size, MIP4_BIN_TAIL_SIZE);
1182 return -EINVAL;
1185 /* Check bin format */
1186 if (memcmp(fw_info->tail_mark, MIP4_BIN_TAIL_MARK,
1187 sizeof(fw_info->tail_mark))) {
1188 dev_err(&ts->client->dev,
1189 "unable to locate tail marker (%*ph vs %*ph)\n",
1190 (int)sizeof(fw_info->tail_mark), fw_info->tail_mark,
1191 (int)sizeof(fw_info->tail_mark), MIP4_BIN_TAIL_MARK);
1192 return -EINVAL;
1195 *fw_offset_start = get_unaligned_le32(&fw_info->bin_start_addr);
1196 *fw_size = get_unaligned_le32(&fw_info->bin_length);
1198 dev_dbg(&ts->client->dev,
1199 "F/W Data offset: %#08x, size: %d\n",
1200 *fw_offset_start, *fw_size);
1202 if (*fw_size % MIP4_BL_PAGE_SIZE) {
1203 dev_err(&ts->client->dev,
1204 "encoded fw length %d is not multiple of pages (%d)\n",
1205 *fw_size, MIP4_BL_PAGE_SIZE);
1206 return -EINVAL;
1209 if (fw->size != *fw_offset_start + *fw_size) {
1210 dev_err(&ts->client->dev,
1211 "Wrong firmware size, expected %d bytes, got %zd\n",
1212 *fw_offset_start + *fw_size, fw->size);
1213 return -EINVAL;
1216 mip4_parse_fw_version((const u8 *)&fw_info->ver_boot, &fw_version);
1218 dev_dbg(&ts->client->dev,
1219 "F/W file version %04X %04X %04X %04X\n",
1220 fw_version.boot, fw_version.core,
1221 fw_version.app, fw_version.param);
1223 dev_dbg(&ts->client->dev, "F/W chip version: %04X %04X %04X %04X\n",
1224 ts->fw_version.boot, ts->fw_version.core,
1225 ts->fw_version.app, ts->fw_version.param);
1227 /* Check F/W type */
1228 if (fw_version.boot != 0xEEEE && fw_version.boot != 0xFFFF &&
1229 fw_version.core == 0xEEEE &&
1230 fw_version.app == 0xEEEE &&
1231 fw_version.param == 0xEEEE) {
1232 dev_dbg(&ts->client->dev, "F/W type: Bootloader\n");
1233 } else if (fw_version.boot == 0xEEEE &&
1234 fw_version.core != 0xEEEE && fw_version.core != 0xFFFF &&
1235 fw_version.app != 0xEEEE && fw_version.app != 0xFFFF &&
1236 fw_version.param != 0xEEEE && fw_version.param != 0xFFFF) {
1237 dev_dbg(&ts->client->dev, "F/W type: Main\n");
1238 } else {
1239 dev_err(&ts->client->dev, "Wrong firmware type\n");
1240 return -EINVAL;
1243 return 0;
1246 static int mip4_execute_fw_update(struct mip4_ts *ts, const struct firmware *fw)
1248 const struct mip4_bin_tail *fw_info;
1249 u32 fw_start_offset;
1250 u32 fw_size;
1251 int retires = 3;
1252 int error;
1254 error = mip4_parse_firmware(ts, fw,
1255 &fw_start_offset, &fw_size, &fw_info);
1256 if (error)
1257 return error;
1259 if (ts->input->users) {
1260 disable_irq(ts->client->irq);
1261 } else {
1262 error = mip4_power_on(ts);
1263 if (error)
1264 return error;
1267 /* Update firmware */
1268 do {
1269 error = mip4_flash_fw(ts, fw->data, fw_size, fw_start_offset);
1270 if (!error)
1271 break;
1272 } while (--retires);
1274 if (error)
1275 dev_err(&ts->client->dev,
1276 "Failed to flash firmware: %d\n", error);
1278 /* Enable IRQ */
1279 if (ts->input->users)
1280 enable_irq(ts->client->irq);
1281 else
1282 mip4_power_off(ts);
1284 return error ? error : 0;
1287 static ssize_t mip4_sysfs_fw_update(struct device *dev,
1288 struct device_attribute *attr,
1289 const char *buf, size_t count)
1291 struct i2c_client *client = to_i2c_client(dev);
1292 struct mip4_ts *ts = i2c_get_clientdata(client);
1293 const struct firmware *fw;
1294 int error;
1296 error = request_firmware(&fw, ts->fw_name, dev);
1297 if (error) {
1298 dev_err(&ts->client->dev,
1299 "Failed to retrieve firmware %s: %d\n",
1300 ts->fw_name, error);
1301 return error;
1305 * Take input mutex to prevent racing with itself and also with
1306 * userspace opening and closing the device and also suspend/resume
1307 * transitions.
1309 mutex_lock(&ts->input->mutex);
1311 error = mip4_execute_fw_update(ts, fw);
1313 mutex_unlock(&ts->input->mutex);
1315 release_firmware(fw);
1317 if (error) {
1318 dev_err(&ts->client->dev,
1319 "Firmware update failed: %d\n", error);
1320 return error;
1323 return count;
1326 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mip4_sysfs_fw_update);
1328 static ssize_t mip4_sysfs_read_fw_version(struct device *dev,
1329 struct device_attribute *attr,
1330 char *buf)
1332 struct i2c_client *client = to_i2c_client(dev);
1333 struct mip4_ts *ts = i2c_get_clientdata(client);
1334 size_t count;
1336 /* Take lock to prevent racing with firmware update */
1337 mutex_lock(&ts->input->mutex);
1339 count = snprintf(buf, PAGE_SIZE, "%04X %04X %04X %04X\n",
1340 ts->fw_version.boot, ts->fw_version.core,
1341 ts->fw_version.app, ts->fw_version.param);
1343 mutex_unlock(&ts->input->mutex);
1345 return count;
1348 static DEVICE_ATTR(fw_version, S_IRUGO, mip4_sysfs_read_fw_version, NULL);
1350 static ssize_t mip4_sysfs_read_hw_version(struct device *dev,
1351 struct device_attribute *attr,
1352 char *buf)
1354 struct i2c_client *client = to_i2c_client(dev);
1355 struct mip4_ts *ts = i2c_get_clientdata(client);
1356 size_t count;
1358 /* Take lock to prevent racing with firmware update */
1359 mutex_lock(&ts->input->mutex);
1362 * product_name shows the name or version of the hardware
1363 * paired with current firmware in the chip.
1365 count = snprintf(buf, PAGE_SIZE, "%.*s\n",
1366 (int)sizeof(ts->product_name), ts->product_name);
1368 mutex_unlock(&ts->input->mutex);
1370 return count;
1373 static DEVICE_ATTR(hw_version, S_IRUGO, mip4_sysfs_read_hw_version, NULL);
1375 static ssize_t mip4_sysfs_read_product_id(struct device *dev,
1376 struct device_attribute *attr,
1377 char *buf)
1379 struct i2c_client *client = to_i2c_client(dev);
1380 struct mip4_ts *ts = i2c_get_clientdata(client);
1381 size_t count;
1383 mutex_lock(&ts->input->mutex);
1385 count = snprintf(buf, PAGE_SIZE, "%04X\n", ts->product_id);
1387 mutex_unlock(&ts->input->mutex);
1389 return count;
1392 static DEVICE_ATTR(product_id, S_IRUGO, mip4_sysfs_read_product_id, NULL);
1394 static ssize_t mip4_sysfs_read_ic_name(struct device *dev,
1395 struct device_attribute *attr,
1396 char *buf)
1398 struct i2c_client *client = to_i2c_client(dev);
1399 struct mip4_ts *ts = i2c_get_clientdata(client);
1400 size_t count;
1402 mutex_lock(&ts->input->mutex);
1404 count = snprintf(buf, PAGE_SIZE, "%.*s\n",
1405 (int)sizeof(ts->ic_name), ts->ic_name);
1407 mutex_unlock(&ts->input->mutex);
1409 return count;
1412 static DEVICE_ATTR(ic_name, S_IRUGO, mip4_sysfs_read_ic_name, NULL);
1414 static struct attribute *mip4_attrs[] = {
1415 &dev_attr_fw_version.attr,
1416 &dev_attr_hw_version.attr,
1417 &dev_attr_product_id.attr,
1418 &dev_attr_ic_name.attr,
1419 &dev_attr_update_fw.attr,
1420 NULL,
1423 static const struct attribute_group mip4_attr_group = {
1424 .attrs = mip4_attrs,
1427 static int mip4_probe(struct i2c_client *client, const struct i2c_device_id *id)
1429 struct mip4_ts *ts;
1430 struct input_dev *input;
1431 int error;
1433 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1434 dev_err(&client->dev, "Not supported I2C adapter\n");
1435 return -ENXIO;
1438 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1439 if (!ts)
1440 return -ENOMEM;
1442 input = devm_input_allocate_device(&client->dev);
1443 if (!input)
1444 return -ENOMEM;
1446 ts->client = client;
1447 ts->input = input;
1449 snprintf(ts->phys, sizeof(ts->phys),
1450 "%s/input0", dev_name(&client->dev));
1452 ts->gpio_ce = devm_gpiod_get_optional(&client->dev,
1453 "ce", GPIOD_OUT_LOW);
1454 if (IS_ERR(ts->gpio_ce)) {
1455 error = PTR_ERR(ts->gpio_ce);
1456 if (error != EPROBE_DEFER)
1457 dev_err(&client->dev,
1458 "Failed to get gpio: %d\n", error);
1459 return error;
1462 error = mip4_power_on(ts);
1463 if (error)
1464 return error;
1465 error = mip4_query_device(ts);
1466 mip4_power_off(ts);
1467 if (error)
1468 return error;
1470 input->name = "MELFAS MIP4 Touchscreen";
1471 input->phys = ts->phys;
1473 input->id.bustype = BUS_I2C;
1474 input->id.vendor = 0x13c5;
1475 input->id.product = ts->product_id;
1477 input->open = mip4_input_open;
1478 input->close = mip4_input_close;
1480 input_set_drvdata(input, ts);
1482 input->keycode = ts->key_code;
1483 input->keycodesize = sizeof(*ts->key_code);
1484 input->keycodemax = ts->key_num;
1486 input_set_abs_params(input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0);
1487 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0);
1488 input_set_abs_params(input, ABS_MT_PRESSURE,
1489 MIP4_PRESSURE_MIN, MIP4_PRESSURE_MAX, 0, 0);
1490 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR,
1491 MIP4_TOUCH_MAJOR_MIN, MIP4_TOUCH_MAJOR_MAX, 0, 0);
1492 input_set_abs_params(input, ABS_MT_TOUCH_MINOR,
1493 MIP4_TOUCH_MINOR_MIN, MIP4_TOUCH_MINOR_MAX, 0, 0);
1494 input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->ppm_x);
1495 input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->ppm_y);
1497 error = input_mt_init_slots(input, MIP4_MAX_FINGERS, INPUT_MT_DIRECT);
1498 if (error)
1499 return error;
1501 i2c_set_clientdata(client, ts);
1503 error = devm_request_threaded_irq(&client->dev, client->irq,
1504 NULL, mip4_interrupt,
1505 IRQF_ONESHOT, MIP4_DEVICE_NAME, ts);
1506 if (error) {
1507 dev_err(&client->dev,
1508 "Failed to request interrupt %d: %d\n",
1509 client->irq, error);
1510 return error;
1513 disable_irq(client->irq);
1515 error = input_register_device(input);
1516 if (error) {
1517 dev_err(&client->dev,
1518 "Failed to register input device: %d\n", error);
1519 return error;
1522 error = devm_device_add_group(&client->dev, &mip4_attr_group);
1523 if (error) {
1524 dev_err(&client->dev,
1525 "Failed to create sysfs attribute group: %d\n", error);
1526 return error;
1529 return 0;
1532 static int __maybe_unused mip4_suspend(struct device *dev)
1534 struct i2c_client *client = to_i2c_client(dev);
1535 struct mip4_ts *ts = i2c_get_clientdata(client);
1536 struct input_dev *input = ts->input;
1538 mutex_lock(&input->mutex);
1540 if (device_may_wakeup(dev))
1541 ts->wake_irq_enabled = enable_irq_wake(client->irq) == 0;
1542 else if (input->users)
1543 mip4_disable(ts);
1545 mutex_unlock(&input->mutex);
1547 return 0;
1550 static int __maybe_unused mip4_resume(struct device *dev)
1552 struct i2c_client *client = to_i2c_client(dev);
1553 struct mip4_ts *ts = i2c_get_clientdata(client);
1554 struct input_dev *input = ts->input;
1556 mutex_lock(&input->mutex);
1558 if (ts->wake_irq_enabled)
1559 disable_irq_wake(client->irq);
1560 else if (input->users)
1561 mip4_enable(ts);
1563 mutex_unlock(&input->mutex);
1565 return 0;
1568 static SIMPLE_DEV_PM_OPS(mip4_pm_ops, mip4_suspend, mip4_resume);
1570 #ifdef CONFIG_OF
1571 static const struct of_device_id mip4_of_match[] = {
1572 { .compatible = "melfas,"MIP4_DEVICE_NAME, },
1573 { },
1575 MODULE_DEVICE_TABLE(of, mip4_of_match);
1576 #endif
1578 #ifdef CONFIG_ACPI
1579 static const struct acpi_device_id mip4_acpi_match[] = {
1580 { "MLFS0000", 0},
1581 { },
1583 MODULE_DEVICE_TABLE(acpi, mip4_acpi_match);
1584 #endif
1586 static const struct i2c_device_id mip4_i2c_ids[] = {
1587 { MIP4_DEVICE_NAME, 0 },
1588 { },
1590 MODULE_DEVICE_TABLE(i2c, mip4_i2c_ids);
1592 static struct i2c_driver mip4_driver = {
1593 .id_table = mip4_i2c_ids,
1594 .probe = mip4_probe,
1595 .driver = {
1596 .name = MIP4_DEVICE_NAME,
1597 .of_match_table = of_match_ptr(mip4_of_match),
1598 .acpi_match_table = ACPI_PTR(mip4_acpi_match),
1599 .pm = &mip4_pm_ops,
1602 module_i2c_driver(mip4_driver);
1604 MODULE_DESCRIPTION("MELFAS MIP4 Touchscreen");
1605 MODULE_AUTHOR("Sangwon Jee <jeesw@melfas.com>");
1606 MODULE_LICENSE("GPL");