Merge tag 'ceph-for-4.13-rc8' of git://github.com/ceph/ceph-client
[linux/fpc-iii.git] / drivers / input / touchscreen / melfas_mip4.c
blob05108c2fea934f863e8eee2dc7a23d0d74bfe89c
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 5.4
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
84 #define MIP4_R1_INFO_PID 0x7C
85 #define MIP4_R1_INFO_VID 0x7E
86 #define MIP4_R1_INFO_SLAVE_ADDR 0x80
88 #define MIP4_R0_EVENT 0x02
89 #define MIP4_R1_EVENT_SUPPORTED_FUNC 0x00
90 #define MIP4_R1_EVENT_FORMAT 0x04
91 #define MIP4_R1_EVENT_SIZE 0x06
92 #define MIP4_R1_EVENT_PACKET_INFO 0x10
93 #define MIP4_R1_EVENT_PACKET_DATA 0x11
95 #define MIP4_R0_CTRL 0x06
96 #define MIP4_R1_CTRL_READY_STATUS 0x00
97 #define MIP4_R1_CTRL_EVENT_READY 0x01
98 #define MIP4_R1_CTRL_MODE 0x10
99 #define MIP4_R1_CTRL_EVENT_TRIGGER_TYPE 0x11
100 #define MIP4_R1_CTRL_RECALIBRATE 0x12
101 #define MIP4_R1_CTRL_POWER_STATE 0x13
102 #define MIP4_R1_CTRL_GESTURE_TYPE 0x14
103 #define MIP4_R1_CTRL_DISABLE_ESD_ALERT 0x18
104 #define MIP4_R1_CTRL_CHARGER_MODE 0x19
105 #define MIP4_R1_CTRL_HIGH_SENS_MODE 0x1A
106 #define MIP4_R1_CTRL_WINDOW_MODE 0x1B
107 #define MIP4_R1_CTRL_PALM_REJECTION 0x1C
108 #define MIP4_R1_CTRL_EDGE_CORRECTION 0x1D
109 #define MIP4_R1_CTRL_ENTER_GLOVE_MODE 0x1E
110 #define MIP4_R1_CTRL_I2C_ON_LPM 0x1F
111 #define MIP4_R1_CTRL_GESTURE_DEBUG 0x20
112 #define MIP4_R1_CTRL_PALM_EVENT 0x22
113 #define MIP4_R1_CTRL_PROXIMITY_SENSING 0x23
115 /* Value */
116 #define MIP4_BOOT_MODE_BOOT 0x01
117 #define MIP4_BOOT_MODE_APP 0x02
119 #define MIP4_BOOT_STATUS_BUSY 0x05
120 #define MIP4_BOOT_STATUS_ERROR 0x0E
121 #define MIP4_BOOT_STATUS_DONE 0xA0
123 #define MIP4_BOOT_CMD_MASS_ERASE 0x15
124 #define MIP4_BOOT_CMD_PROGRAM 0x54
125 #define MIP4_BOOT_CMD_ERASE 0x8F
126 #define MIP4_BOOT_CMD_WRITE 0xA5
127 #define MIP4_BOOT_CMD_READ 0xC2
129 #define MIP4_EVENT_INPUT_TYPE_KEY 0
130 #define MIP4_EVENT_INPUT_TYPE_SCREEN 1
131 #define MIP4_EVENT_INPUT_TYPE_PROXIMITY 2
133 #define I2C_RETRY_COUNT 3 /* 2~ */
135 #define MIP4_BUF_SIZE 128
136 #define MIP4_MAX_FINGERS 10
137 #define MIP4_MAX_KEYS 4
139 #define MIP4_TOUCH_MAJOR_MIN 0
140 #define MIP4_TOUCH_MAJOR_MAX 255
141 #define MIP4_TOUCH_MINOR_MIN 0
142 #define MIP4_TOUCH_MINOR_MAX 255
143 #define MIP4_PRESSURE_MIN 0
144 #define MIP4_PRESSURE_MAX 255
146 #define MIP4_FW_NAME "melfas_mip4.fw"
147 #define MIP4_FW_UPDATE_DEBUG 0 /* 0 (default) or 1 */
149 struct mip4_fw_version {
150 u16 boot;
151 u16 core;
152 u16 app;
153 u16 param;
156 struct mip4_ts {
157 struct i2c_client *client;
158 struct input_dev *input;
159 struct gpio_desc *gpio_ce;
161 char phys[32];
162 char product_name[16];
163 u16 product_id;
164 char ic_name[4];
165 char fw_name[32];
167 unsigned int max_x;
168 unsigned int max_y;
169 u8 node_x;
170 u8 node_y;
171 u8 node_key;
172 unsigned int ppm_x;
173 unsigned int ppm_y;
175 struct mip4_fw_version fw_version;
177 unsigned int event_size;
178 unsigned int event_format;
180 unsigned int key_num;
181 unsigned short key_code[MIP4_MAX_KEYS];
183 bool wake_irq_enabled;
185 u8 buf[MIP4_BUF_SIZE];
188 static int mip4_i2c_xfer(struct mip4_ts *ts,
189 char *write_buf, unsigned int write_len,
190 char *read_buf, unsigned int read_len)
192 struct i2c_msg msg[] = {
194 .addr = ts->client->addr,
195 .flags = 0,
196 .buf = write_buf,
197 .len = write_len,
198 }, {
199 .addr = ts->client->addr,
200 .flags = I2C_M_RD,
201 .buf = read_buf,
202 .len = read_len,
205 int retry = I2C_RETRY_COUNT;
206 int res;
207 int error;
209 do {
210 res = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
211 if (res == ARRAY_SIZE(msg))
212 return 0;
214 error = res < 0 ? res : -EIO;
215 dev_err(&ts->client->dev,
216 "%s - i2c_transfer failed: %d (%d)\n",
217 __func__, error, res);
218 } while (--retry);
220 return error;
223 static void mip4_parse_fw_version(const u8 *buf, struct mip4_fw_version *v)
225 v->boot = get_unaligned_le16(buf + 0);
226 v->core = get_unaligned_le16(buf + 2);
227 v->app = get_unaligned_le16(buf + 4);
228 v->param = get_unaligned_le16(buf + 6);
232 * Read chip firmware version
234 static int mip4_get_fw_version(struct mip4_ts *ts)
236 u8 cmd[] = { MIP4_R0_INFO, MIP4_R1_INFO_VERSION_BOOT };
237 u8 buf[sizeof(ts->fw_version)];
238 int error;
240 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, sizeof(buf));
241 if (error) {
242 memset(&ts->fw_version, 0xff, sizeof(ts->fw_version));
243 return error;
246 mip4_parse_fw_version(buf, &ts->fw_version);
248 return 0;
252 * Fetch device characteristics
254 static int mip4_query_device(struct mip4_ts *ts)
256 union i2c_smbus_data dummy;
257 int error;
258 u8 cmd[2];
259 u8 buf[14];
262 * Make sure there is something at this address as we do not
263 * consider subsequent failures as fatal.
265 if (i2c_smbus_xfer(ts->client->adapter, ts->client->addr,
266 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
267 dev_err(&ts->client->dev, "nothing at this address\n");
268 return -ENXIO;
271 /* Product name */
272 cmd[0] = MIP4_R0_INFO;
273 cmd[1] = MIP4_R1_INFO_PRODUCT_NAME;
274 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd),
275 ts->product_name, sizeof(ts->product_name));
276 if (error)
277 dev_warn(&ts->client->dev,
278 "Failed to retrieve product name: %d\n", error);
279 else
280 dev_dbg(&ts->client->dev, "product name: %.*s\n",
281 (int)sizeof(ts->product_name), ts->product_name);
283 /* Product ID */
284 cmd[0] = MIP4_R0_INFO;
285 cmd[1] = MIP4_R1_INFO_PID;
286 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 2);
287 if (error) {
288 dev_warn(&ts->client->dev,
289 "Failed to retrieve product id: %d\n", error);
290 } else {
291 ts->product_id = get_unaligned_le16(&buf[0]);
292 dev_dbg(&ts->client->dev, "product id: %04X\n", ts->product_id);
295 /* Firmware name */
296 snprintf(ts->fw_name, sizeof(ts->fw_name),
297 "melfas_mip4_%04X.fw", ts->product_id);
298 dev_dbg(&ts->client->dev, "firmware name: %s\n", ts->fw_name);
300 /* IC name */
301 cmd[0] = MIP4_R0_INFO;
302 cmd[1] = MIP4_R1_INFO_IC_NAME;
303 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd),
304 ts->ic_name, sizeof(ts->ic_name));
305 if (error)
306 dev_warn(&ts->client->dev,
307 "Failed to retrieve IC name: %d\n", error);
308 else
309 dev_dbg(&ts->client->dev, "IC name: %.*s\n",
310 (int)sizeof(ts->ic_name), ts->ic_name);
312 /* Firmware version */
313 error = mip4_get_fw_version(ts);
314 if (error)
315 dev_warn(&ts->client->dev,
316 "Failed to retrieve FW version: %d\n", error);
317 else
318 dev_dbg(&ts->client->dev, "F/W Version: %04X %04X %04X %04X\n",
319 ts->fw_version.boot, ts->fw_version.core,
320 ts->fw_version.app, ts->fw_version.param);
322 /* Resolution */
323 cmd[0] = MIP4_R0_INFO;
324 cmd[1] = MIP4_R1_INFO_RESOLUTION_X;
325 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 14);
326 if (error) {
327 dev_warn(&ts->client->dev,
328 "Failed to retrieve touchscreen parameters: %d\n",
329 error);
330 } else {
331 ts->max_x = get_unaligned_le16(&buf[0]);
332 ts->max_y = get_unaligned_le16(&buf[2]);
333 dev_dbg(&ts->client->dev, "max_x: %d, max_y: %d\n",
334 ts->max_x, ts->max_y);
336 ts->node_x = buf[4];
337 ts->node_y = buf[5];
338 ts->node_key = buf[6];
339 dev_dbg(&ts->client->dev,
340 "node_x: %d, node_y: %d, node_key: %d\n",
341 ts->node_x, ts->node_y, ts->node_key);
343 ts->ppm_x = buf[12];
344 ts->ppm_y = buf[13];
345 dev_dbg(&ts->client->dev, "ppm_x: %d, ppm_y: %d\n",
346 ts->ppm_x, ts->ppm_y);
348 /* Key ts */
349 if (ts->node_key > 0)
350 ts->key_num = ts->node_key;
353 /* Protocol */
354 cmd[0] = MIP4_R0_EVENT;
355 cmd[1] = MIP4_R1_EVENT_SUPPORTED_FUNC;
356 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 7);
357 if (error) {
358 dev_warn(&ts->client->dev,
359 "Failed to retrieve device type: %d\n", error);
360 ts->event_format = 0xff;
361 } else {
362 ts->event_format = get_unaligned_le16(&buf[4]);
363 ts->event_size = buf[6];
364 dev_dbg(&ts->client->dev, "event_format: %d, event_size: %d\n",
365 ts->event_format, ts->event_size);
367 if (ts->event_format == 2 || ts->event_format > 3)
368 dev_warn(&ts->client->dev,
369 "Unknown event format %d\n", ts->event_format);
372 return 0;
375 static int mip4_power_on(struct mip4_ts *ts)
377 if (ts->gpio_ce) {
378 gpiod_set_value_cansleep(ts->gpio_ce, 1);
380 /* Booting delay : 200~300ms */
381 usleep_range(200 * 1000, 300 * 1000);
384 return 0;
387 static void mip4_power_off(struct mip4_ts *ts)
389 if (ts->gpio_ce)
390 gpiod_set_value_cansleep(ts->gpio_ce, 0);
394 * Clear touch input event status
396 static void mip4_clear_input(struct mip4_ts *ts)
398 int i;
400 /* Screen */
401 for (i = 0; i < MIP4_MAX_FINGERS; i++) {
402 input_mt_slot(ts->input, i);
403 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 0);
406 /* Keys */
407 for (i = 0; i < ts->key_num; i++)
408 input_report_key(ts->input, ts->key_code[i], 0);
410 input_sync(ts->input);
413 static int mip4_enable(struct mip4_ts *ts)
415 int error;
417 error = mip4_power_on(ts);
418 if (error)
419 return error;
421 enable_irq(ts->client->irq);
423 return 0;
426 static void mip4_disable(struct mip4_ts *ts)
428 disable_irq(ts->client->irq);
430 mip4_power_off(ts);
432 mip4_clear_input(ts);
435 /*****************************************************************
436 * Input handling
437 *****************************************************************/
439 static void mip4_report_keys(struct mip4_ts *ts, u8 *packet)
441 u8 key;
442 bool down;
444 switch (ts->event_format) {
445 case 0:
446 case 1:
447 key = packet[0] & 0x0F;
448 down = packet[0] & 0x80;
449 break;
451 case 3:
452 default:
453 key = packet[0] & 0x0F;
454 down = packet[1] & 0x01;
455 break;
458 /* Report key event */
459 if (key >= 1 && key <= ts->key_num) {
460 unsigned short keycode = ts->key_code[key - 1];
462 dev_dbg(&ts->client->dev,
463 "Key - ID: %d, keycode: %d, state: %d\n",
464 key, keycode, down);
466 input_event(ts->input, EV_MSC, MSC_SCAN, keycode);
467 input_report_key(ts->input, keycode, down);
469 } else {
470 dev_err(&ts->client->dev, "Unknown key: %d\n", key);
474 static void mip4_report_touch(struct mip4_ts *ts, u8 *packet)
476 int id;
477 bool hover;
478 bool palm;
479 bool state;
480 u16 x, y;
481 u8 pressure_stage = 0;
482 u8 pressure;
483 u8 size;
484 u8 touch_major;
485 u8 touch_minor;
487 switch (ts->event_format) {
488 case 0:
489 case 1:
490 /* Touch only */
491 state = packet[0] & BIT(7);
492 hover = packet[0] & BIT(5);
493 palm = packet[0] & BIT(4);
494 id = (packet[0] & 0x0F) - 1;
495 x = ((packet[1] & 0x0F) << 8) | packet[2];
496 y = (((packet[1] >> 4) & 0x0F) << 8) |
497 packet[3];
498 pressure = packet[4];
499 size = packet[5];
500 if (ts->event_format == 0) {
501 touch_major = packet[5];
502 touch_minor = packet[5];
503 } else {
504 touch_major = packet[6];
505 touch_minor = packet[7];
507 break;
509 case 3:
510 default:
511 /* Touch + Force(Pressure) */
512 id = (packet[0] & 0x0F) - 1;
513 hover = packet[1] & BIT(2);
514 palm = packet[1] & BIT(1);
515 state = packet[1] & BIT(0);
516 x = ((packet[2] & 0x0F) << 8) | packet[3];
517 y = (((packet[2] >> 4) & 0x0F) << 8) |
518 packet[4];
519 size = packet[6];
520 pressure_stage = (packet[7] & 0xF0) >> 4;
521 pressure = ((packet[7] & 0x0F) << 8) |
522 packet[8];
523 touch_major = packet[9];
524 touch_minor = packet[10];
525 break;
528 dev_dbg(&ts->client->dev,
529 "Screen - Slot: %d State: %d X: %04d Y: %04d Z: %d\n",
530 id, state, x, y, pressure);
532 if (unlikely(id < 0 || id >= MIP4_MAX_FINGERS)) {
533 dev_err(&ts->client->dev, "Screen - invalid slot ID: %d\n", id);
534 } else if (state) {
535 /* Press or Move event */
536 input_mt_slot(ts->input, id);
537 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
538 input_report_abs(ts->input, ABS_MT_POSITION_X, x);
539 input_report_abs(ts->input, ABS_MT_POSITION_Y, y);
540 input_report_abs(ts->input, ABS_MT_PRESSURE, pressure);
541 input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, touch_major);
542 input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, touch_minor);
543 } else {
544 /* Release event */
545 input_mt_slot(ts->input, id);
546 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 0);
549 input_mt_sync_frame(ts->input);
552 static int mip4_handle_packet(struct mip4_ts *ts, u8 *packet)
554 u8 type;
556 switch (ts->event_format) {
557 case 0:
558 case 1:
559 type = (packet[0] & 0x40) >> 6;
560 break;
562 case 3:
563 type = (packet[0] & 0xF0) >> 4;
564 break;
566 default:
567 /* Should not happen unless we have corrupted firmware */
568 return -EINVAL;
571 dev_dbg(&ts->client->dev, "Type: %d\n", type);
573 /* Report input event */
574 switch (type) {
575 case MIP4_EVENT_INPUT_TYPE_KEY:
576 mip4_report_keys(ts, packet);
577 break;
579 case MIP4_EVENT_INPUT_TYPE_SCREEN:
580 mip4_report_touch(ts, packet);
581 break;
583 default:
584 dev_err(&ts->client->dev, "Unknown event type: %d\n", type);
585 break;
588 return 0;
591 static irqreturn_t mip4_interrupt(int irq, void *dev_id)
593 struct mip4_ts *ts = dev_id;
594 struct i2c_client *client = ts->client;
595 unsigned int i;
596 int error;
597 u8 cmd[2];
598 u8 size;
599 bool alert;
601 /* Read packet info */
602 cmd[0] = MIP4_R0_EVENT;
603 cmd[1] = MIP4_R1_EVENT_PACKET_INFO;
604 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), ts->buf, 1);
605 if (error) {
606 dev_err(&client->dev,
607 "Failed to read packet info: %d\n", error);
608 goto out;
611 size = ts->buf[0] & 0x7F;
612 alert = ts->buf[0] & BIT(7);
613 dev_dbg(&client->dev, "packet size: %d, alert: %d\n", size, alert);
615 /* Check size */
616 if (!size) {
617 dev_err(&client->dev, "Empty packet\n");
618 goto out;
621 /* Read packet data */
622 cmd[0] = MIP4_R0_EVENT;
623 cmd[1] = MIP4_R1_EVENT_PACKET_DATA;
624 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), ts->buf, size);
625 if (error) {
626 dev_err(&client->dev,
627 "Failed to read packet data: %d\n", error);
628 goto out;
631 if (alert) {
632 dev_dbg(&client->dev, "Alert: %d\n", ts->buf[0]);
633 } else {
634 for (i = 0; i < size; i += ts->event_size) {
635 error = mip4_handle_packet(ts, &ts->buf[i]);
636 if (error)
637 break;
640 input_sync(ts->input);
643 out:
644 return IRQ_HANDLED;
647 static int mip4_input_open(struct input_dev *dev)
649 struct mip4_ts *ts = input_get_drvdata(dev);
651 return mip4_enable(ts);
654 static void mip4_input_close(struct input_dev *dev)
656 struct mip4_ts *ts = input_get_drvdata(dev);
658 mip4_disable(ts);
661 /*****************************************************************
662 * Firmware update
663 *****************************************************************/
665 /* Firmware Info */
666 #define MIP4_BL_PAGE_SIZE 512 /* 512 */
667 #define MIP4_BL_PACKET_SIZE 512 /* 512, 256, 128, 64, ... */
670 * Firmware binary tail info
673 struct mip4_bin_tail {
674 u8 tail_mark[4];
675 u8 chip_name[4];
677 __le32 bin_start_addr;
678 __le32 bin_length;
680 __le16 ver_boot;
681 __le16 ver_core;
682 __le16 ver_app;
683 __le16 ver_param;
685 u8 boot_start;
686 u8 boot_end;
687 u8 core_start;
688 u8 core_end;
689 u8 app_start;
690 u8 app_end;
691 u8 param_start;
692 u8 param_end;
694 u8 checksum_type;
695 u8 hw_category;
697 __le16 param_id;
698 __le32 param_length;
699 __le32 build_date;
700 __le32 build_time;
702 __le32 reserved1;
703 __le32 reserved2;
704 __le16 reserved3;
705 __le16 tail_size;
706 __le32 crc;
707 } __packed;
709 #define MIP4_BIN_TAIL_MARK "MBT\001"
710 #define MIP4_BIN_TAIL_SIZE (sizeof(struct mip4_bin_tail))
713 * Bootloader - Read status
715 static int mip4_bl_read_status(struct mip4_ts *ts)
717 u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_STATUS };
718 u8 result;
719 struct i2c_msg msg[] = {
721 .addr = ts->client->addr,
722 .flags = 0,
723 .buf = cmd,
724 .len = sizeof(cmd),
725 }, {
726 .addr = ts->client->addr,
727 .flags = I2C_M_RD,
728 .buf = &result,
729 .len = sizeof(result),
732 int ret;
733 int error;
734 int retry = 1000;
736 do {
737 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
738 if (ret != ARRAY_SIZE(msg)) {
739 error = ret < 0 ? ret : -EIO;
740 dev_err(&ts->client->dev,
741 "Failed to read bootloader status: %d\n",
742 error);
743 return error;
746 switch (result) {
747 case MIP4_BOOT_STATUS_DONE:
748 dev_dbg(&ts->client->dev, "%s - done\n", __func__);
749 return 0;
751 case MIP4_BOOT_STATUS_ERROR:
752 dev_err(&ts->client->dev, "Bootloader failure\n");
753 return -EIO;
755 case MIP4_BOOT_STATUS_BUSY:
756 dev_dbg(&ts->client->dev, "%s - Busy\n", __func__);
757 error = -EBUSY;
758 break;
760 default:
761 dev_err(&ts->client->dev,
762 "Unexpected bootloader status: %#02x\n",
763 result);
764 error = -EINVAL;
765 break;
768 usleep_range(1000, 2000);
769 } while (--retry);
771 return error;
775 * Bootloader - Change mode
777 static int mip4_bl_change_mode(struct mip4_ts *ts, u8 mode)
779 u8 mode_chg_cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_MODE, mode };
780 u8 mode_read_cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_MODE };
781 u8 result;
782 struct i2c_msg msg[] = {
784 .addr = ts->client->addr,
785 .flags = 0,
786 .buf = mode_read_cmd,
787 .len = sizeof(mode_read_cmd),
788 }, {
789 .addr = ts->client->addr,
790 .flags = I2C_M_RD,
791 .buf = &result,
792 .len = sizeof(result),
795 int retry = 10;
796 int ret;
797 int error;
799 do {
800 /* Send mode change command */
801 ret = i2c_master_send(ts->client,
802 mode_chg_cmd, sizeof(mode_chg_cmd));
803 if (ret != sizeof(mode_chg_cmd)) {
804 error = ret < 0 ? ret : -EIO;
805 dev_err(&ts->client->dev,
806 "Failed to send %d mode change: %d (%d)\n",
807 mode, error, ret);
808 return error;
811 dev_dbg(&ts->client->dev,
812 "Sent mode change request (mode: %d)\n", mode);
814 /* Wait */
815 msleep(1000);
817 /* Verify target mode */
818 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
819 if (ret != ARRAY_SIZE(msg)) {
820 error = ret < 0 ? ret : -EIO;
821 dev_err(&ts->client->dev,
822 "Failed to read device mode: %d\n", error);
823 return error;
826 dev_dbg(&ts->client->dev,
827 "Current device mode: %d, want: %d\n", result, mode);
829 if (result == mode)
830 return 0;
832 } while (--retry);
834 return -EIO;
838 * Bootloader - Start bootloader mode
840 static int mip4_bl_enter(struct mip4_ts *ts)
842 return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_BOOT);
846 * Bootloader - Exit bootloader mode
848 static int mip4_bl_exit(struct mip4_ts *ts)
850 return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_APP);
853 static int mip4_bl_get_address(struct mip4_ts *ts, u16 *buf_addr)
855 u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_BUF_ADDR };
856 u8 result[sizeof(u16)];
857 struct i2c_msg msg[] = {
859 .addr = ts->client->addr,
860 .flags = 0,
861 .buf = cmd,
862 .len = sizeof(cmd),
863 }, {
864 .addr = ts->client->addr,
865 .flags = I2C_M_RD,
866 .buf = result,
867 .len = sizeof(result),
870 int ret;
871 int error;
873 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
874 if (ret != ARRAY_SIZE(msg)) {
875 error = ret < 0 ? ret : -EIO;
876 dev_err(&ts->client->dev,
877 "Failed to retrieve bootloader buffer address: %d\n",
878 error);
879 return error;
882 *buf_addr = get_unaligned_le16(result);
883 dev_dbg(&ts->client->dev,
884 "Bootloader buffer address %#04x\n", *buf_addr);
886 return 0;
889 static int mip4_bl_program_page(struct mip4_ts *ts, int offset,
890 const u8 *data, int length, u16 buf_addr)
892 u8 cmd[6];
893 u8 *data_buf;
894 u16 buf_offset;
895 int ret;
896 int error;
898 dev_dbg(&ts->client->dev, "Writing page @%#06x (%d)\n",
899 offset, length);
901 if (length > MIP4_BL_PAGE_SIZE || length % MIP4_BL_PACKET_SIZE) {
902 dev_err(&ts->client->dev,
903 "Invalid page length: %d\n", length);
904 return -EINVAL;
907 data_buf = kmalloc(2 + MIP4_BL_PACKET_SIZE, GFP_KERNEL);
908 if (!data_buf)
909 return -ENOMEM;
911 /* Addr */
912 cmd[0] = MIP4_R0_BOOT;
913 cmd[1] = MIP4_R1_BOOT_TARGET_ADDR;
914 put_unaligned_le32(offset, &cmd[2]);
915 ret = i2c_master_send(ts->client, cmd, 6);
916 if (ret != 6) {
917 error = ret < 0 ? ret : -EIO;
918 dev_err(&ts->client->dev,
919 "Failed to send write page address: %d\n", error);
920 goto out;
923 /* Size */
924 cmd[0] = MIP4_R0_BOOT;
925 cmd[1] = MIP4_R1_BOOT_SIZE;
926 put_unaligned_le32(length, &cmd[2]);
927 ret = i2c_master_send(ts->client, cmd, 6);
928 if (ret != 6) {
929 error = ret < 0 ? ret : -EIO;
930 dev_err(&ts->client->dev,
931 "Failed to send write page size: %d\n", error);
932 goto out;
935 /* Data */
936 for (buf_offset = 0;
937 buf_offset < length;
938 buf_offset += MIP4_BL_PACKET_SIZE) {
939 dev_dbg(&ts->client->dev,
940 "writing chunk at %#04x (size %d)\n",
941 buf_offset, MIP4_BL_PACKET_SIZE);
942 put_unaligned_be16(buf_addr + buf_offset, data_buf);
943 memcpy(&data_buf[2], &data[buf_offset], MIP4_BL_PACKET_SIZE);
944 ret = i2c_master_send(ts->client,
945 data_buf, 2 + MIP4_BL_PACKET_SIZE);
946 if (ret != 2 + MIP4_BL_PACKET_SIZE) {
947 error = ret < 0 ? ret : -EIO;
948 dev_err(&ts->client->dev,
949 "Failed to read chunk at %#04x (size %d): %d\n",
950 buf_offset, MIP4_BL_PACKET_SIZE, error);
951 goto out;
955 /* Command */
956 cmd[0] = MIP4_R0_BOOT;
957 cmd[1] = MIP4_R1_BOOT_CMD;
958 cmd[2] = MIP4_BOOT_CMD_PROGRAM;
959 ret = i2c_master_send(ts->client, cmd, 3);
960 if (ret != 3) {
961 error = ret < 0 ? ret : -EIO;
962 dev_err(&ts->client->dev,
963 "Failed to send 'write' command: %d\n", error);
964 goto out;
967 /* Status */
968 error = mip4_bl_read_status(ts);
970 out:
971 kfree(data_buf);
972 return error ? error : 0;
975 static int mip4_bl_verify_page(struct mip4_ts *ts, int offset,
976 const u8 *data, int length, int buf_addr)
978 u8 cmd[8];
979 u8 *read_buf;
980 int buf_offset;
981 struct i2c_msg msg[] = {
983 .addr = ts->client->addr,
984 .flags = 0,
985 .buf = cmd,
986 .len = 2,
987 }, {
988 .addr = ts->client->addr,
989 .flags = I2C_M_RD,
990 .len = MIP4_BL_PACKET_SIZE,
993 int ret;
994 int error;
996 dev_dbg(&ts->client->dev, "Validating page @%#06x (%d)\n",
997 offset, length);
999 /* Addr */
1000 cmd[0] = MIP4_R0_BOOT;
1001 cmd[1] = MIP4_R1_BOOT_TARGET_ADDR;
1002 put_unaligned_le32(offset, &cmd[2]);
1003 ret = i2c_master_send(ts->client, cmd, 6);
1004 if (ret != 6) {
1005 error = ret < 0 ? ret : -EIO;
1006 dev_err(&ts->client->dev,
1007 "Failed to send read page address: %d\n", error);
1008 return error;
1011 /* Size */
1012 cmd[0] = MIP4_R0_BOOT;
1013 cmd[1] = MIP4_R1_BOOT_SIZE;
1014 put_unaligned_le32(length, &cmd[2]);
1015 ret = i2c_master_send(ts->client, cmd, 6);
1016 if (ret != 6) {
1017 error = ret < 0 ? ret : -EIO;
1018 dev_err(&ts->client->dev,
1019 "Failed to send read page size: %d\n", error);
1020 return error;
1023 /* Command */
1024 cmd[0] = MIP4_R0_BOOT;
1025 cmd[1] = MIP4_R1_BOOT_CMD;
1026 cmd[2] = MIP4_BOOT_CMD_READ;
1027 ret = i2c_master_send(ts->client, cmd, 3);
1028 if (ret != 3) {
1029 error = ret < 0 ? ret : -EIO;
1030 dev_err(&ts->client->dev,
1031 "Failed to send 'read' command: %d\n", error);
1032 return error;
1035 /* Status */
1036 error = mip4_bl_read_status(ts);
1037 if (error)
1038 return error;
1040 /* Read */
1041 msg[1].buf = read_buf = kmalloc(MIP4_BL_PACKET_SIZE, GFP_KERNEL);
1042 if (!read_buf)
1043 return -ENOMEM;
1045 for (buf_offset = 0;
1046 buf_offset < length;
1047 buf_offset += MIP4_BL_PACKET_SIZE) {
1048 dev_dbg(&ts->client->dev,
1049 "reading chunk at %#04x (size %d)\n",
1050 buf_offset, MIP4_BL_PACKET_SIZE);
1051 put_unaligned_be16(buf_addr + buf_offset, cmd);
1052 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
1053 if (ret != ARRAY_SIZE(msg)) {
1054 error = ret < 0 ? ret : -EIO;
1055 dev_err(&ts->client->dev,
1056 "Failed to read chunk at %#04x (size %d): %d\n",
1057 buf_offset, MIP4_BL_PACKET_SIZE, error);
1058 break;
1061 if (memcmp(&data[buf_offset], read_buf, MIP4_BL_PACKET_SIZE)) {
1062 dev_err(&ts->client->dev,
1063 "Failed to validate chunk at %#04x (size %d)\n",
1064 buf_offset, MIP4_BL_PACKET_SIZE);
1065 #if MIP4_FW_UPDATE_DEBUG
1066 print_hex_dump(KERN_DEBUG,
1067 MIP4_DEVICE_NAME " F/W File: ",
1068 DUMP_PREFIX_OFFSET, 16, 1,
1069 data + offset, MIP4_BL_PACKET_SIZE,
1070 false);
1071 print_hex_dump(KERN_DEBUG,
1072 MIP4_DEVICE_NAME " F/W Chip: ",
1073 DUMP_PREFIX_OFFSET, 16, 1,
1074 read_buf, MIP4_BL_PAGE_SIZE, false);
1075 #endif
1076 error = -EINVAL;
1077 break;
1081 kfree(read_buf);
1082 return error ? error : 0;
1086 * Flash chip firmware
1088 static int mip4_flash_fw(struct mip4_ts *ts,
1089 const u8 *fw_data, u32 fw_size, u32 fw_offset)
1091 struct i2c_client *client = ts->client;
1092 int offset;
1093 u16 buf_addr;
1094 int error, error2;
1096 /* Enter bootloader mode */
1097 dev_dbg(&client->dev, "Entering bootloader mode\n");
1099 error = mip4_bl_enter(ts);
1100 if (error) {
1101 dev_err(&client->dev,
1102 "Failed to enter bootloader mode: %d\n",
1103 error);
1104 return error;
1107 /* Read info */
1108 error = mip4_bl_get_address(ts, &buf_addr);
1109 if (error)
1110 goto exit_bl;
1112 /* Program & Verify */
1113 dev_dbg(&client->dev,
1114 "Program & Verify, page size: %d, packet size: %d\n",
1115 MIP4_BL_PAGE_SIZE, MIP4_BL_PACKET_SIZE);
1117 for (offset = fw_offset;
1118 offset < fw_offset + fw_size;
1119 offset += MIP4_BL_PAGE_SIZE) {
1120 /* Program */
1121 error = mip4_bl_program_page(ts, offset, fw_data + offset,
1122 MIP4_BL_PAGE_SIZE, buf_addr);
1123 if (error)
1124 break;
1126 /* Verify */
1127 error = mip4_bl_verify_page(ts, offset, fw_data + offset,
1128 MIP4_BL_PAGE_SIZE, buf_addr);
1129 if (error)
1130 break;
1133 exit_bl:
1134 /* Exit bootloader mode */
1135 dev_dbg(&client->dev, "Exiting bootloader mode\n");
1137 error2 = mip4_bl_exit(ts);
1138 if (error2) {
1139 dev_err(&client->dev,
1140 "Failed to exit bootloader mode: %d\n", error2);
1141 if (!error)
1142 error = error2;
1145 /* Reset chip */
1146 mip4_power_off(ts);
1147 mip4_power_on(ts);
1149 mip4_query_device(ts);
1151 /* Refresh device parameters */
1152 input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0);
1153 input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0);
1154 input_set_abs_params(ts->input, ABS_X, 0, ts->max_x, 0, 0);
1155 input_set_abs_params(ts->input, ABS_Y, 0, ts->max_y, 0, 0);
1156 input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->ppm_x);
1157 input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->ppm_y);
1158 input_abs_set_res(ts->input, ABS_X, ts->ppm_x);
1159 input_abs_set_res(ts->input, ABS_Y, ts->ppm_y);
1161 return error ? error : 0;
1164 static int mip4_parse_firmware(struct mip4_ts *ts, const struct firmware *fw,
1165 u32 *fw_offset_start, u32 *fw_size,
1166 const struct mip4_bin_tail **pfw_info)
1168 const struct mip4_bin_tail *fw_info;
1169 struct mip4_fw_version fw_version;
1170 u16 tail_size;
1172 if (fw->size < MIP4_BIN_TAIL_SIZE) {
1173 dev_err(&ts->client->dev,
1174 "Invalid firmware, size mismatch (tail %zd vs %zd)\n",
1175 MIP4_BIN_TAIL_SIZE, fw->size);
1176 return -EINVAL;
1179 fw_info = (const void *)&fw->data[fw->size - MIP4_BIN_TAIL_SIZE];
1181 #if MIP4_FW_UPDATE_DEBUG
1182 print_hex_dump(KERN_ERR, MIP4_DEVICE_NAME " Bin Info: ",
1183 DUMP_PREFIX_OFFSET, 16, 1, *fw_info, tail_size, false);
1184 #endif
1186 tail_size = get_unaligned_le16(&fw_info->tail_size);
1187 if (tail_size != MIP4_BIN_TAIL_SIZE) {
1188 dev_err(&ts->client->dev,
1189 "wrong tail size: %d (expected %zd)\n",
1190 tail_size, MIP4_BIN_TAIL_SIZE);
1191 return -EINVAL;
1194 /* Check bin format */
1195 if (memcmp(fw_info->tail_mark, MIP4_BIN_TAIL_MARK,
1196 sizeof(fw_info->tail_mark))) {
1197 dev_err(&ts->client->dev,
1198 "unable to locate tail marker (%*ph vs %*ph)\n",
1199 (int)sizeof(fw_info->tail_mark), fw_info->tail_mark,
1200 (int)sizeof(fw_info->tail_mark), MIP4_BIN_TAIL_MARK);
1201 return -EINVAL;
1204 *fw_offset_start = get_unaligned_le32(&fw_info->bin_start_addr);
1205 *fw_size = get_unaligned_le32(&fw_info->bin_length);
1207 dev_dbg(&ts->client->dev,
1208 "F/W Data offset: %#08x, size: %d\n",
1209 *fw_offset_start, *fw_size);
1211 if (*fw_size % MIP4_BL_PAGE_SIZE) {
1212 dev_err(&ts->client->dev,
1213 "encoded fw length %d is not multiple of pages (%d)\n",
1214 *fw_size, MIP4_BL_PAGE_SIZE);
1215 return -EINVAL;
1218 if (fw->size != *fw_offset_start + *fw_size) {
1219 dev_err(&ts->client->dev,
1220 "Wrong firmware size, expected %d bytes, got %zd\n",
1221 *fw_offset_start + *fw_size, fw->size);
1222 return -EINVAL;
1225 mip4_parse_fw_version((const u8 *)&fw_info->ver_boot, &fw_version);
1227 dev_dbg(&ts->client->dev,
1228 "F/W file version %04X %04X %04X %04X\n",
1229 fw_version.boot, fw_version.core,
1230 fw_version.app, fw_version.param);
1232 dev_dbg(&ts->client->dev, "F/W chip version: %04X %04X %04X %04X\n",
1233 ts->fw_version.boot, ts->fw_version.core,
1234 ts->fw_version.app, ts->fw_version.param);
1236 /* Check F/W type */
1237 if (fw_version.boot != 0xEEEE && fw_version.boot != 0xFFFF &&
1238 fw_version.core == 0xEEEE &&
1239 fw_version.app == 0xEEEE &&
1240 fw_version.param == 0xEEEE) {
1241 dev_dbg(&ts->client->dev, "F/W type: Bootloader\n");
1242 } else if (fw_version.boot == 0xEEEE &&
1243 fw_version.core != 0xEEEE && fw_version.core != 0xFFFF &&
1244 fw_version.app != 0xEEEE && fw_version.app != 0xFFFF &&
1245 fw_version.param != 0xEEEE && fw_version.param != 0xFFFF) {
1246 dev_dbg(&ts->client->dev, "F/W type: Main\n");
1247 } else {
1248 dev_err(&ts->client->dev, "Wrong firmware type\n");
1249 return -EINVAL;
1252 return 0;
1255 static int mip4_execute_fw_update(struct mip4_ts *ts, const struct firmware *fw)
1257 const struct mip4_bin_tail *fw_info;
1258 u32 fw_start_offset;
1259 u32 fw_size;
1260 int retires = 3;
1261 int error;
1263 error = mip4_parse_firmware(ts, fw,
1264 &fw_start_offset, &fw_size, &fw_info);
1265 if (error)
1266 return error;
1268 if (ts->input->users) {
1269 disable_irq(ts->client->irq);
1270 } else {
1271 error = mip4_power_on(ts);
1272 if (error)
1273 return error;
1276 /* Update firmware */
1277 do {
1278 error = mip4_flash_fw(ts, fw->data, fw_size, fw_start_offset);
1279 if (!error)
1280 break;
1281 } while (--retires);
1283 if (error)
1284 dev_err(&ts->client->dev,
1285 "Failed to flash firmware: %d\n", error);
1287 /* Enable IRQ */
1288 if (ts->input->users)
1289 enable_irq(ts->client->irq);
1290 else
1291 mip4_power_off(ts);
1293 return error ? error : 0;
1296 static ssize_t mip4_sysfs_fw_update(struct device *dev,
1297 struct device_attribute *attr,
1298 const char *buf, size_t count)
1300 struct i2c_client *client = to_i2c_client(dev);
1301 struct mip4_ts *ts = i2c_get_clientdata(client);
1302 const struct firmware *fw;
1303 int error;
1305 error = request_firmware(&fw, ts->fw_name, dev);
1306 if (error) {
1307 dev_err(&ts->client->dev,
1308 "Failed to retrieve firmware %s: %d\n",
1309 ts->fw_name, error);
1310 return error;
1314 * Take input mutex to prevent racing with itself and also with
1315 * userspace opening and closing the device and also suspend/resume
1316 * transitions.
1318 mutex_lock(&ts->input->mutex);
1320 error = mip4_execute_fw_update(ts, fw);
1322 mutex_unlock(&ts->input->mutex);
1324 release_firmware(fw);
1326 if (error) {
1327 dev_err(&ts->client->dev,
1328 "Firmware update failed: %d\n", error);
1329 return error;
1332 return count;
1335 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mip4_sysfs_fw_update);
1337 static ssize_t mip4_sysfs_read_fw_version(struct device *dev,
1338 struct device_attribute *attr,
1339 char *buf)
1341 struct i2c_client *client = to_i2c_client(dev);
1342 struct mip4_ts *ts = i2c_get_clientdata(client);
1343 size_t count;
1345 /* Take lock to prevent racing with firmware update */
1346 mutex_lock(&ts->input->mutex);
1348 count = snprintf(buf, PAGE_SIZE, "%04X %04X %04X %04X\n",
1349 ts->fw_version.boot, ts->fw_version.core,
1350 ts->fw_version.app, ts->fw_version.param);
1352 mutex_unlock(&ts->input->mutex);
1354 return count;
1357 static DEVICE_ATTR(fw_version, S_IRUGO, mip4_sysfs_read_fw_version, NULL);
1359 static ssize_t mip4_sysfs_read_hw_version(struct device *dev,
1360 struct device_attribute *attr,
1361 char *buf)
1363 struct i2c_client *client = to_i2c_client(dev);
1364 struct mip4_ts *ts = i2c_get_clientdata(client);
1365 size_t count;
1367 /* Take lock to prevent racing with firmware update */
1368 mutex_lock(&ts->input->mutex);
1371 * product_name shows the name or version of the hardware
1372 * paired with current firmware in the chip.
1374 count = snprintf(buf, PAGE_SIZE, "%.*s\n",
1375 (int)sizeof(ts->product_name), ts->product_name);
1377 mutex_unlock(&ts->input->mutex);
1379 return count;
1382 static DEVICE_ATTR(hw_version, S_IRUGO, mip4_sysfs_read_hw_version, NULL);
1384 static ssize_t mip4_sysfs_read_product_id(struct device *dev,
1385 struct device_attribute *attr,
1386 char *buf)
1388 struct i2c_client *client = to_i2c_client(dev);
1389 struct mip4_ts *ts = i2c_get_clientdata(client);
1390 size_t count;
1392 mutex_lock(&ts->input->mutex);
1394 count = snprintf(buf, PAGE_SIZE, "%04X\n", ts->product_id);
1396 mutex_unlock(&ts->input->mutex);
1398 return count;
1401 static DEVICE_ATTR(product_id, S_IRUGO, mip4_sysfs_read_product_id, NULL);
1403 static ssize_t mip4_sysfs_read_ic_name(struct device *dev,
1404 struct device_attribute *attr,
1405 char *buf)
1407 struct i2c_client *client = to_i2c_client(dev);
1408 struct mip4_ts *ts = i2c_get_clientdata(client);
1409 size_t count;
1411 mutex_lock(&ts->input->mutex);
1413 count = snprintf(buf, PAGE_SIZE, "%.*s\n",
1414 (int)sizeof(ts->ic_name), ts->ic_name);
1416 mutex_unlock(&ts->input->mutex);
1418 return count;
1421 static DEVICE_ATTR(ic_name, S_IRUGO, mip4_sysfs_read_ic_name, NULL);
1423 static struct attribute *mip4_attrs[] = {
1424 &dev_attr_fw_version.attr,
1425 &dev_attr_hw_version.attr,
1426 &dev_attr_product_id.attr,
1427 &dev_attr_ic_name.attr,
1428 &dev_attr_update_fw.attr,
1429 NULL,
1432 static const struct attribute_group mip4_attr_group = {
1433 .attrs = mip4_attrs,
1436 static void mip4_sysfs_remove(void *_data)
1438 struct mip4_ts *ts = _data;
1440 sysfs_remove_group(&ts->client->dev.kobj, &mip4_attr_group);
1443 static int mip4_probe(struct i2c_client *client, const struct i2c_device_id *id)
1445 struct mip4_ts *ts;
1446 struct input_dev *input;
1447 int error;
1449 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1450 dev_err(&client->dev, "Not supported I2C adapter\n");
1451 return -ENXIO;
1454 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1455 if (!ts)
1456 return -ENOMEM;
1458 input = devm_input_allocate_device(&client->dev);
1459 if (!input)
1460 return -ENOMEM;
1462 ts->client = client;
1463 ts->input = input;
1465 snprintf(ts->phys, sizeof(ts->phys),
1466 "%s/input0", dev_name(&client->dev));
1468 ts->gpio_ce = devm_gpiod_get_optional(&client->dev,
1469 "ce", GPIOD_OUT_LOW);
1470 if (IS_ERR(ts->gpio_ce)) {
1471 error = PTR_ERR(ts->gpio_ce);
1472 if (error != EPROBE_DEFER)
1473 dev_err(&client->dev,
1474 "Failed to get gpio: %d\n", error);
1475 return error;
1478 error = mip4_power_on(ts);
1479 if (error)
1480 return error;
1481 error = mip4_query_device(ts);
1482 mip4_power_off(ts);
1483 if (error)
1484 return error;
1486 input->name = "MELFAS MIP4 Touchscreen";
1487 input->phys = ts->phys;
1489 input->id.bustype = BUS_I2C;
1490 input->id.vendor = 0x13c5;
1491 input->id.product = ts->product_id;
1493 input->open = mip4_input_open;
1494 input->close = mip4_input_close;
1496 input_set_drvdata(input, ts);
1498 input->keycode = ts->key_code;
1499 input->keycodesize = sizeof(*ts->key_code);
1500 input->keycodemax = ts->key_num;
1502 input_set_abs_params(input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0);
1503 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0);
1504 input_set_abs_params(input, ABS_MT_PRESSURE,
1505 MIP4_PRESSURE_MIN, MIP4_PRESSURE_MAX, 0, 0);
1506 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR,
1507 MIP4_TOUCH_MAJOR_MIN, MIP4_TOUCH_MAJOR_MAX, 0, 0);
1508 input_set_abs_params(input, ABS_MT_TOUCH_MINOR,
1509 MIP4_TOUCH_MINOR_MIN, MIP4_TOUCH_MINOR_MAX, 0, 0);
1510 input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->ppm_x);
1511 input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->ppm_y);
1513 error = input_mt_init_slots(input, MIP4_MAX_FINGERS, INPUT_MT_DIRECT);
1514 if (error)
1515 return error;
1517 i2c_set_clientdata(client, ts);
1519 error = devm_request_threaded_irq(&client->dev, client->irq,
1520 NULL, mip4_interrupt,
1521 IRQF_ONESHOT, MIP4_DEVICE_NAME, ts);
1522 if (error) {
1523 dev_err(&client->dev,
1524 "Failed to request interrupt %d: %d\n",
1525 client->irq, error);
1526 return error;
1529 disable_irq(client->irq);
1531 error = input_register_device(input);
1532 if (error) {
1533 dev_err(&client->dev,
1534 "Failed to register input device: %d\n", error);
1535 return error;
1538 error = sysfs_create_group(&client->dev.kobj, &mip4_attr_group);
1539 if (error) {
1540 dev_err(&client->dev,
1541 "Failed to create sysfs attribute group: %d\n", error);
1542 return error;
1545 error = devm_add_action(&client->dev, mip4_sysfs_remove, ts);
1546 if (error) {
1547 mip4_sysfs_remove(ts);
1548 dev_err(&client->dev,
1549 "Failed to install sysfs remoce action: %d\n", error);
1550 return error;
1553 return 0;
1556 static int __maybe_unused mip4_suspend(struct device *dev)
1558 struct i2c_client *client = to_i2c_client(dev);
1559 struct mip4_ts *ts = i2c_get_clientdata(client);
1560 struct input_dev *input = ts->input;
1562 mutex_lock(&input->mutex);
1564 if (device_may_wakeup(dev))
1565 ts->wake_irq_enabled = enable_irq_wake(client->irq) == 0;
1566 else if (input->users)
1567 mip4_disable(ts);
1569 mutex_unlock(&input->mutex);
1571 return 0;
1574 static int __maybe_unused mip4_resume(struct device *dev)
1576 struct i2c_client *client = to_i2c_client(dev);
1577 struct mip4_ts *ts = i2c_get_clientdata(client);
1578 struct input_dev *input = ts->input;
1580 mutex_lock(&input->mutex);
1582 if (ts->wake_irq_enabled)
1583 disable_irq_wake(client->irq);
1584 else if (input->users)
1585 mip4_enable(ts);
1587 mutex_unlock(&input->mutex);
1589 return 0;
1592 static SIMPLE_DEV_PM_OPS(mip4_pm_ops, mip4_suspend, mip4_resume);
1594 #ifdef CONFIG_OF
1595 static const struct of_device_id mip4_of_match[] = {
1596 { .compatible = "melfas,"MIP4_DEVICE_NAME, },
1597 { },
1599 MODULE_DEVICE_TABLE(of, mip4_of_match);
1600 #endif
1602 #ifdef CONFIG_ACPI
1603 static const struct acpi_device_id mip4_acpi_match[] = {
1604 { "MLFS0000", 0},
1605 { },
1607 MODULE_DEVICE_TABLE(acpi, mip4_acpi_match);
1608 #endif
1610 static const struct i2c_device_id mip4_i2c_ids[] = {
1611 { MIP4_DEVICE_NAME, 0 },
1612 { },
1614 MODULE_DEVICE_TABLE(i2c, mip4_i2c_ids);
1616 static struct i2c_driver mip4_driver = {
1617 .id_table = mip4_i2c_ids,
1618 .probe = mip4_probe,
1619 .driver = {
1620 .name = MIP4_DEVICE_NAME,
1621 .of_match_table = of_match_ptr(mip4_of_match),
1622 .acpi_match_table = ACPI_PTR(mip4_acpi_match),
1623 .pm = &mip4_pm_ops,
1626 module_i2c_driver(mip4_driver);
1628 MODULE_DESCRIPTION("MELFAS MIP4 Touchscreen");
1629 MODULE_VERSION("2016.10.31");
1630 MODULE_AUTHOR("Sangwon Jee <jeesw@melfas.com>");
1631 MODULE_LICENSE("GPL");