]>
Commit | Line | Data |
---|---|---|
6ccb1d8f SJ |
1 | /* |
2 | * MELFAS MIP4 Touchscreen | |
3 | * | |
4 | * Copyright (C) 2016 MELFAS Inc. | |
5 | * | |
6 | * Author : Sangwon Jee <jeesw@melfas.com> | |
7 | * | |
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. | |
12 | * | |
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. | |
17 | */ | |
18 | ||
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> | |
31 | ||
32 | #define MIP4_DEVICE_NAME "mip4_ts" | |
33 | ||
34 | /***************************************************************** | |
35 | * Protocol | |
e2aad666 | 36 | * Version : MIP 4.0 Rev 5.4 |
6ccb1d8f SJ |
37 | *****************************************************************/ |
38 | ||
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 | |
47 | ||
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 | |
7a3ed686 SJ |
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 | |
6ccb1d8f SJ |
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 | |
e2aad666 SJ |
84 | #define MIP4_R1_INFO_PID 0x7C |
85 | #define MIP4_R1_INFO_VID 0x7E | |
86 | #define MIP4_R1_INFO_SLAVE_ADDR 0x80 | |
6ccb1d8f SJ |
87 | |
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 | |
94 | ||
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 | |
114 | ||
115 | /* Value */ | |
116 | #define MIP4_BOOT_MODE_BOOT 0x01 | |
117 | #define MIP4_BOOT_MODE_APP 0x02 | |
118 | ||
119 | #define MIP4_BOOT_STATUS_BUSY 0x05 | |
120 | #define MIP4_BOOT_STATUS_ERROR 0x0E | |
121 | #define MIP4_BOOT_STATUS_DONE 0xA0 | |
122 | ||
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 | |
128 | ||
129 | #define MIP4_EVENT_INPUT_TYPE_KEY 0 | |
130 | #define MIP4_EVENT_INPUT_TYPE_SCREEN 1 | |
131 | #define MIP4_EVENT_INPUT_TYPE_PROXIMITY 2 | |
132 | ||
133 | #define I2C_RETRY_COUNT 3 /* 2~ */ | |
134 | ||
135 | #define MIP4_BUF_SIZE 128 | |
136 | #define MIP4_MAX_FINGERS 10 | |
137 | #define MIP4_MAX_KEYS 4 | |
138 | ||
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 | |
145 | ||
146 | #define MIP4_FW_NAME "melfas_mip4.fw" | |
147 | #define MIP4_FW_UPDATE_DEBUG 0 /* 0 (default) or 1 */ | |
148 | ||
149 | struct mip4_fw_version { | |
150 | u16 boot; | |
151 | u16 core; | |
152 | u16 app; | |
153 | u16 param; | |
154 | }; | |
155 | ||
156 | struct mip4_ts { | |
157 | struct i2c_client *client; | |
158 | struct input_dev *input; | |
159 | struct gpio_desc *gpio_ce; | |
160 | ||
161 | char phys[32]; | |
162 | char product_name[16]; | |
e2aad666 | 163 | u16 product_id; |
4e1bff07 | 164 | char ic_name[4]; |
c2c4ba82 | 165 | char fw_name[32]; |
6ccb1d8f SJ |
166 | |
167 | unsigned int max_x; | |
168 | unsigned int max_y; | |
169 | u8 node_x; | |
170 | u8 node_y; | |
171 | u8 node_key; | |
7a3ed686 SJ |
172 | unsigned int ppm_x; |
173 | unsigned int ppm_y; | |
6ccb1d8f SJ |
174 | |
175 | struct mip4_fw_version fw_version; | |
176 | ||
177 | unsigned int event_size; | |
178 | unsigned int event_format; | |
179 | ||
180 | unsigned int key_num; | |
181 | unsigned short key_code[MIP4_MAX_KEYS]; | |
182 | ||
183 | bool wake_irq_enabled; | |
184 | ||
185 | u8 buf[MIP4_BUF_SIZE]; | |
186 | }; | |
187 | ||
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) | |
191 | { | |
192 | struct i2c_msg msg[] = { | |
193 | { | |
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, | |
203 | }, | |
204 | }; | |
205 | int retry = I2C_RETRY_COUNT; | |
206 | int res; | |
207 | int error; | |
208 | ||
209 | do { | |
210 | res = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg)); | |
211 | if (res == ARRAY_SIZE(msg)) | |
212 | return 0; | |
213 | ||
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); | |
219 | ||
220 | return error; | |
221 | } | |
222 | ||
223 | static void mip4_parse_fw_version(const u8 *buf, struct mip4_fw_version *v) | |
224 | { | |
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); | |
229 | } | |
230 | ||
231 | /* | |
232 | * Read chip firmware version | |
233 | */ | |
234 | static int mip4_get_fw_version(struct mip4_ts *ts) | |
235 | { | |
236 | u8 cmd[] = { MIP4_R0_INFO, MIP4_R1_INFO_VERSION_BOOT }; | |
237 | u8 buf[sizeof(ts->fw_version)]; | |
238 | int error; | |
239 | ||
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; | |
244 | } | |
245 | ||
246 | mip4_parse_fw_version(buf, &ts->fw_version); | |
247 | ||
248 | return 0; | |
249 | } | |
250 | ||
251 | /* | |
252 | * Fetch device characteristics | |
253 | */ | |
254 | static int mip4_query_device(struct mip4_ts *ts) | |
255 | { | |
4896fb13 | 256 | union i2c_smbus_data dummy; |
6ccb1d8f SJ |
257 | int error; |
258 | u8 cmd[2]; | |
7a3ed686 | 259 | u8 buf[14]; |
6ccb1d8f | 260 | |
4896fb13 DT |
261 | /* |
262 | * Make sure there is something at this address as we do not | |
263 | * consider subsequent failures as fatal. | |
264 | */ | |
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; | |
269 | } | |
270 | ||
6ccb1d8f SJ |
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); | |
282 | ||
e2aad666 SJ |
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); | |
293 | } | |
294 | ||
c2c4ba82 SJ |
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); | |
299 | ||
4e1bff07 SJ |
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); | |
311 | ||
6ccb1d8f SJ |
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); | |
321 | ||
322 | /* Resolution */ | |
323 | cmd[0] = MIP4_R0_INFO; | |
324 | cmd[1] = MIP4_R1_INFO_RESOLUTION_X; | |
7a3ed686 | 325 | error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 14); |
6ccb1d8f SJ |
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); | |
335 | ||
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); | |
342 | ||
7a3ed686 SJ |
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); | |
347 | ||
6ccb1d8f SJ |
348 | /* Key ts */ |
349 | if (ts->node_key > 0) | |
350 | ts->key_num = ts->node_key; | |
351 | } | |
352 | ||
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); | |
366 | ||
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); | |
370 | } | |
371 | ||
372 | return 0; | |
373 | } | |
374 | ||
375 | static int mip4_power_on(struct mip4_ts *ts) | |
376 | { | |
377 | if (ts->gpio_ce) { | |
378 | gpiod_set_value_cansleep(ts->gpio_ce, 1); | |
379 | ||
380 | /* Booting delay : 200~300ms */ | |
381 | usleep_range(200 * 1000, 300 * 1000); | |
382 | } | |
383 | ||
384 | return 0; | |
385 | } | |
386 | ||
387 | static void mip4_power_off(struct mip4_ts *ts) | |
388 | { | |
389 | if (ts->gpio_ce) | |
390 | gpiod_set_value_cansleep(ts->gpio_ce, 0); | |
391 | } | |
392 | ||
393 | /* | |
394 | * Clear touch input event status | |
395 | */ | |
396 | static void mip4_clear_input(struct mip4_ts *ts) | |
397 | { | |
398 | int i; | |
399 | ||
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); | |
404 | } | |
405 | ||
406 | /* Keys */ | |
407 | for (i = 0; i < ts->key_num; i++) | |
408 | input_report_key(ts->input, ts->key_code[i], 0); | |
409 | ||
410 | input_sync(ts->input); | |
411 | } | |
412 | ||
413 | static int mip4_enable(struct mip4_ts *ts) | |
414 | { | |
415 | int error; | |
416 | ||
417 | error = mip4_power_on(ts); | |
418 | if (error) | |
419 | return error; | |
420 | ||
421 | enable_irq(ts->client->irq); | |
422 | ||
423 | return 0; | |
424 | } | |
425 | ||
426 | static void mip4_disable(struct mip4_ts *ts) | |
427 | { | |
428 | disable_irq(ts->client->irq); | |
429 | ||
430 | mip4_power_off(ts); | |
431 | ||
432 | mip4_clear_input(ts); | |
433 | } | |
434 | ||
435 | /***************************************************************** | |
436 | * Input handling | |
437 | *****************************************************************/ | |
438 | ||
439 | static void mip4_report_keys(struct mip4_ts *ts, u8 *packet) | |
440 | { | |
441 | u8 key; | |
442 | bool down; | |
443 | ||
444 | switch (ts->event_format) { | |
445 | case 0: | |
446 | case 1: | |
447 | key = packet[0] & 0x0F; | |
448 | down = packet[0] & 0x80; | |
449 | break; | |
450 | ||
451 | case 3: | |
452 | default: | |
453 | key = packet[0] & 0x0F; | |
454 | down = packet[1] & 0x01; | |
455 | break; | |
456 | } | |
457 | ||
458 | /* Report key event */ | |
459 | if (key >= 1 && key <= ts->key_num) { | |
460 | unsigned short keycode = ts->key_code[key - 1]; | |
461 | ||
462 | dev_dbg(&ts->client->dev, | |
463 | "Key - ID: %d, keycode: %d, state: %d\n", | |
464 | key, keycode, down); | |
465 | ||
466 | input_event(ts->input, EV_MSC, MSC_SCAN, keycode); | |
467 | input_report_key(ts->input, keycode, down); | |
468 | ||
469 | } else { | |
470 | dev_err(&ts->client->dev, "Unknown key: %d\n", key); | |
471 | } | |
472 | } | |
473 | ||
474 | static void mip4_report_touch(struct mip4_ts *ts, u8 *packet) | |
475 | { | |
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; | |
486 | ||
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]; | |
506 | } | |
507 | break; | |
508 | ||
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; | |
526 | } | |
527 | ||
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); | |
531 | ||
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); | |
547 | } | |
548 | ||
549 | input_mt_sync_frame(ts->input); | |
550 | } | |
551 | ||
552 | static int mip4_handle_packet(struct mip4_ts *ts, u8 *packet) | |
553 | { | |
554 | u8 type; | |
555 | ||
556 | switch (ts->event_format) { | |
557 | case 0: | |
558 | case 1: | |
559 | type = (packet[0] & 0x40) >> 6; | |
560 | break; | |
561 | ||
562 | case 3: | |
563 | type = (packet[0] & 0xF0) >> 4; | |
564 | break; | |
565 | ||
566 | default: | |
567 | /* Should not happen unless we have corrupted firmware */ | |
568 | return -EINVAL; | |
569 | } | |
570 | ||
571 | dev_dbg(&ts->client->dev, "Type: %d\n", type); | |
572 | ||
573 | /* Report input event */ | |
574 | switch (type) { | |
575 | case MIP4_EVENT_INPUT_TYPE_KEY: | |
576 | mip4_report_keys(ts, packet); | |
577 | break; | |
578 | ||
579 | case MIP4_EVENT_INPUT_TYPE_SCREEN: | |
580 | mip4_report_touch(ts, packet); | |
581 | break; | |
582 | ||
583 | default: | |
584 | dev_err(&ts->client->dev, "Unknown event type: %d\n", type); | |
585 | break; | |
586 | } | |
587 | ||
588 | return 0; | |
589 | } | |
590 | ||
591 | static irqreturn_t mip4_interrupt(int irq, void *dev_id) | |
592 | { | |
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; | |
600 | ||
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; | |
609 | } | |
610 | ||
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); | |
614 | ||
615 | /* Check size */ | |
616 | if (!size) { | |
617 | dev_err(&client->dev, "Empty packet\n"); | |
618 | goto out; | |
619 | } | |
620 | ||
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; | |
629 | } | |
630 | ||
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; | |
638 | } | |
639 | ||
640 | input_sync(ts->input); | |
641 | } | |
642 | ||
643 | out: | |
644 | return IRQ_HANDLED; | |
645 | } | |
646 | ||
647 | static int mip4_input_open(struct input_dev *dev) | |
648 | { | |
649 | struct mip4_ts *ts = input_get_drvdata(dev); | |
650 | ||
651 | return mip4_enable(ts); | |
652 | } | |
653 | ||
654 | static void mip4_input_close(struct input_dev *dev) | |
655 | { | |
656 | struct mip4_ts *ts = input_get_drvdata(dev); | |
657 | ||
658 | mip4_disable(ts); | |
659 | } | |
660 | ||
661 | /***************************************************************** | |
662 | * Firmware update | |
663 | *****************************************************************/ | |
664 | ||
665 | /* Firmware Info */ | |
666 | #define MIP4_BL_PAGE_SIZE 512 /* 512 */ | |
667 | #define MIP4_BL_PACKET_SIZE 512 /* 512, 256, 128, 64, ... */ | |
668 | ||
669 | /* | |
670 | * Firmware binary tail info | |
671 | */ | |
672 | ||
673 | struct mip4_bin_tail { | |
674 | u8 tail_mark[4]; | |
675 | u8 chip_name[4]; | |
676 | ||
677 | __le32 bin_start_addr; | |
678 | __le32 bin_length; | |
679 | ||
680 | __le16 ver_boot; | |
681 | __le16 ver_core; | |
682 | __le16 ver_app; | |
683 | __le16 ver_param; | |
684 | ||
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; | |
693 | ||
694 | u8 checksum_type; | |
695 | u8 hw_category; | |
696 | ||
697 | __le16 param_id; | |
698 | __le32 param_length; | |
699 | __le32 build_date; | |
700 | __le32 build_time; | |
701 | ||
702 | __le32 reserved1; | |
703 | __le32 reserved2; | |
704 | __le16 reserved3; | |
705 | __le16 tail_size; | |
706 | __le32 crc; | |
707 | } __packed; | |
708 | ||
709 | #define MIP4_BIN_TAIL_MARK "MBT\001" | |
710 | #define MIP4_BIN_TAIL_SIZE (sizeof(struct mip4_bin_tail)) | |
711 | ||
712 | /* | |
713 | * Bootloader - Read status | |
714 | */ | |
715 | static int mip4_bl_read_status(struct mip4_ts *ts) | |
716 | { | |
717 | u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_STATUS }; | |
718 | u8 result; | |
719 | struct i2c_msg msg[] = { | |
720 | { | |
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), | |
730 | }, | |
731 | }; | |
732 | int ret; | |
733 | int error; | |
734 | int retry = 1000; | |
735 | ||
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; | |
744 | } | |
745 | ||
746 | switch (result) { | |
747 | case MIP4_BOOT_STATUS_DONE: | |
748 | dev_dbg(&ts->client->dev, "%s - done\n", __func__); | |
749 | return 0; | |
750 | ||
751 | case MIP4_BOOT_STATUS_ERROR: | |
752 | dev_err(&ts->client->dev, "Bootloader failure\n"); | |
753 | return -EIO; | |
754 | ||
755 | case MIP4_BOOT_STATUS_BUSY: | |
756 | dev_dbg(&ts->client->dev, "%s - Busy\n", __func__); | |
757 | error = -EBUSY; | |
758 | break; | |
759 | ||
760 | default: | |
761 | dev_err(&ts->client->dev, | |
762 | "Unexpected bootloader status: %#02x\n", | |
763 | result); | |
764 | error = -EINVAL; | |
765 | break; | |
766 | } | |
767 | ||
768 | usleep_range(1000, 2000); | |
769 | } while (--retry); | |
770 | ||
771 | return error; | |
772 | } | |
773 | ||
774 | /* | |
775 | * Bootloader - Change mode | |
776 | */ | |
777 | static int mip4_bl_change_mode(struct mip4_ts *ts, u8 mode) | |
778 | { | |
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[] = { | |
783 | { | |
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), | |
793 | }, | |
794 | }; | |
795 | int retry = 10; | |
796 | int ret; | |
797 | int error; | |
798 | ||
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; | |
809 | } | |
810 | ||
811 | dev_dbg(&ts->client->dev, | |
812 | "Sent mode change request (mode: %d)\n", mode); | |
813 | ||
814 | /* Wait */ | |
815 | msleep(1000); | |
816 | ||
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; | |
824 | } | |
825 | ||
826 | dev_dbg(&ts->client->dev, | |
827 | "Current device mode: %d, want: %d\n", result, mode); | |
828 | ||
829 | if (result == mode) | |
830 | return 0; | |
831 | ||
832 | } while (--retry); | |
833 | ||
834 | return -EIO; | |
835 | } | |
836 | ||
837 | /* | |
838 | * Bootloader - Start bootloader mode | |
839 | */ | |
840 | static int mip4_bl_enter(struct mip4_ts *ts) | |
841 | { | |
842 | return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_BOOT); | |
843 | } | |
844 | ||
845 | /* | |
846 | * Bootloader - Exit bootloader mode | |
847 | */ | |
848 | static int mip4_bl_exit(struct mip4_ts *ts) | |
849 | { | |
850 | return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_APP); | |
851 | } | |
852 | ||
853 | static int mip4_bl_get_address(struct mip4_ts *ts, u16 *buf_addr) | |
854 | { | |
855 | u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_BUF_ADDR }; | |
856 | u8 result[sizeof(u16)]; | |
857 | struct i2c_msg msg[] = { | |
858 | { | |
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), | |
868 | }, | |
869 | }; | |
870 | int ret; | |
871 | int error; | |
872 | ||
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; | |
880 | } | |
881 | ||
882 | *buf_addr = get_unaligned_le16(result); | |
883 | dev_dbg(&ts->client->dev, | |
884 | "Bootloader buffer address %#04x\n", *buf_addr); | |
885 | ||
886 | return 0; | |
887 | } | |
888 | ||
889 | static int mip4_bl_program_page(struct mip4_ts *ts, int offset, | |
890 | const u8 *data, int length, u16 buf_addr) | |
891 | { | |
892 | u8 cmd[6]; | |
893 | u8 *data_buf; | |
894 | u16 buf_offset; | |
895 | int ret; | |
896 | int error; | |
897 | ||
898 | dev_dbg(&ts->client->dev, "Writing page @%#06x (%d)\n", | |
899 | offset, length); | |
900 | ||
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; | |
905 | } | |
906 | ||
907 | data_buf = kmalloc(2 + MIP4_BL_PACKET_SIZE, GFP_KERNEL); | |
908 | if (!data_buf) | |
909 | return -ENOMEM; | |
910 | ||
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; | |
921 | } | |
922 | ||
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; | |
933 | } | |
934 | ||
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; | |
952 | } | |
953 | } | |
954 | ||
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; | |
965 | } | |
966 | ||
967 | /* Status */ | |
968 | error = mip4_bl_read_status(ts); | |
969 | ||
970 | out: | |
971 | kfree(data_buf); | |
972 | return error ? error : 0; | |
973 | } | |
974 | ||
975 | static int mip4_bl_verify_page(struct mip4_ts *ts, int offset, | |
976 | const u8 *data, int length, int buf_addr) | |
977 | { | |
978 | u8 cmd[8]; | |
979 | u8 *read_buf; | |
980 | int buf_offset; | |
981 | struct i2c_msg msg[] = { | |
982 | { | |
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, | |
991 | }, | |
992 | }; | |
993 | int ret; | |
994 | int error; | |
995 | ||
996 | dev_dbg(&ts->client->dev, "Validating page @%#06x (%d)\n", | |
997 | offset, length); | |
998 | ||
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; | |
1009 | } | |
1010 | ||
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; | |
1021 | } | |
1022 | ||
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; | |
1033 | } | |
1034 | ||
1035 | /* Status */ | |
1036 | error = mip4_bl_read_status(ts); | |
1037 | if (error) | |
1038 | return error; | |
1039 | ||
1040 | /* Read */ | |
1041 | msg[1].buf = read_buf = kmalloc(MIP4_BL_PACKET_SIZE, GFP_KERNEL); | |
1042 | if (!read_buf) | |
1043 | return -ENOMEM; | |
1044 | ||
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; | |
1059 | } | |
1060 | ||
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; | |
1078 | } | |
1079 | } | |
1080 | ||
1081 | kfree(read_buf); | |
1082 | return error ? error : 0; | |
1083 | } | |
1084 | ||
1085 | /* | |
1086 | * Flash chip firmware | |
1087 | */ | |
1088 | static int mip4_flash_fw(struct mip4_ts *ts, | |
1089 | const u8 *fw_data, u32 fw_size, u32 fw_offset) | |
1090 | { | |
1091 | struct i2c_client *client = ts->client; | |
1092 | int offset; | |
1093 | u16 buf_addr; | |
1094 | int error, error2; | |
1095 | ||
1096 | /* Enter bootloader mode */ | |
1097 | dev_dbg(&client->dev, "Entering bootloader mode\n"); | |
1098 | ||
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; | |
1105 | } | |
1106 | ||
1107 | /* Read info */ | |
1108 | error = mip4_bl_get_address(ts, &buf_addr); | |
1109 | if (error) | |
1110 | goto exit_bl; | |
1111 | ||
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); | |
1116 | ||
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; | |
1125 | ||
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; | |
1131 | } | |
1132 | ||
1133 | exit_bl: | |
1134 | /* Exit bootloader mode */ | |
1135 | dev_dbg(&client->dev, "Exiting bootloader mode\n"); | |
1136 | ||
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; | |
1143 | } | |
1144 | ||
1145 | /* Reset chip */ | |
1146 | mip4_power_off(ts); | |
1147 | mip4_power_on(ts); | |
1148 | ||
1149 | mip4_query_device(ts); | |
1150 | ||
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); | |
7a3ed686 SJ |
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); | |
6ccb1d8f SJ |
1160 | |
1161 | return error ? error : 0; | |
1162 | } | |
1163 | ||
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) | |
1167 | { | |
1168 | const struct mip4_bin_tail *fw_info; | |
1169 | struct mip4_fw_version fw_version; | |
1170 | u16 tail_size; | |
1171 | ||
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; | |
1177 | } | |
1178 | ||
1179 | fw_info = (const void *)&fw->data[fw->size - MIP4_BIN_TAIL_SIZE]; | |
1180 | ||
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 | |
1185 | ||
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; | |
1192 | } | |
1193 | ||
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; | |
1202 | } | |
1203 | ||
1204 | *fw_offset_start = get_unaligned_le32(&fw_info->bin_start_addr); | |
1205 | *fw_size = get_unaligned_le32(&fw_info->bin_length); | |
1206 | ||
1207 | dev_dbg(&ts->client->dev, | |
1208 | "F/W Data offset: %#08x, size: %d\n", | |
1209 | *fw_offset_start, *fw_size); | |
1210 | ||
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; | |
1216 | } | |
1217 | ||
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; | |
1223 | } | |
1224 | ||
1225 | mip4_parse_fw_version((const u8 *)&fw_info->ver_boot, &fw_version); | |
1226 | ||
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); | |
1231 | ||
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); | |
1235 | ||
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; | |
1250 | } | |
1251 | ||
1252 | return 0; | |
1253 | } | |
1254 | ||
1255 | static int mip4_execute_fw_update(struct mip4_ts *ts, const struct firmware *fw) | |
1256 | { | |
1257 | const struct mip4_bin_tail *fw_info; | |
1258 | u32 fw_start_offset; | |
1259 | u32 fw_size; | |
1260 | int retires = 3; | |
1261 | int error; | |
1262 | ||
1263 | error = mip4_parse_firmware(ts, fw, | |
1264 | &fw_start_offset, &fw_size, &fw_info); | |
1265 | if (error) | |
1266 | return error; | |
1267 | ||
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; | |
1274 | } | |
1275 | ||
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); | |
1282 | ||
1283 | if (error) | |
1284 | dev_err(&ts->client->dev, | |
1285 | "Failed to flash firmware: %d\n", error); | |
1286 | ||
1287 | /* Enable IRQ */ | |
1288 | if (ts->input->users) | |
1289 | enable_irq(ts->client->irq); | |
1290 | else | |
1291 | mip4_power_off(ts); | |
1292 | ||
1293 | return error ? error : 0; | |
1294 | } | |
1295 | ||
1296 | static ssize_t mip4_sysfs_fw_update(struct device *dev, | |
1297 | struct device_attribute *attr, | |
1298 | const char *buf, size_t count) | |
1299 | { | |
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; | |
1304 | ||
c2c4ba82 | 1305 | error = request_firmware(&fw, ts->fw_name, dev); |
6ccb1d8f SJ |
1306 | if (error) { |
1307 | dev_err(&ts->client->dev, | |
1308 | "Failed to retrieve firmware %s: %d\n", | |
c2c4ba82 | 1309 | ts->fw_name, error); |
6ccb1d8f SJ |
1310 | return error; |
1311 | } | |
1312 | ||
1313 | /* | |
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. | |
1317 | */ | |
1318 | mutex_lock(&ts->input->mutex); | |
1319 | ||
1320 | error = mip4_execute_fw_update(ts, fw); | |
1321 | ||
1322 | mutex_unlock(&ts->input->mutex); | |
1323 | ||
1324 | release_firmware(fw); | |
1325 | ||
1326 | if (error) { | |
1327 | dev_err(&ts->client->dev, | |
1328 | "Firmware update failed: %d\n", error); | |
1329 | return error; | |
1330 | } | |
1331 | ||
1332 | return count; | |
1333 | } | |
1334 | ||
1335 | static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mip4_sysfs_fw_update); | |
1336 | ||
1337 | static ssize_t mip4_sysfs_read_fw_version(struct device *dev, | |
1338 | struct device_attribute *attr, | |
1339 | char *buf) | |
1340 | { | |
1341 | struct i2c_client *client = to_i2c_client(dev); | |
1342 | struct mip4_ts *ts = i2c_get_clientdata(client); | |
1343 | size_t count; | |
1344 | ||
1345 | /* Take lock to prevent racing with firmware update */ | |
1346 | mutex_lock(&ts->input->mutex); | |
1347 | ||
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); | |
1351 | ||
1352 | mutex_unlock(&ts->input->mutex); | |
1353 | ||
1354 | return count; | |
1355 | } | |
1356 | ||
1357 | static DEVICE_ATTR(fw_version, S_IRUGO, mip4_sysfs_read_fw_version, NULL); | |
1358 | ||
9aa40426 SJ |
1359 | static ssize_t mip4_sysfs_read_hw_version(struct device *dev, |
1360 | struct device_attribute *attr, | |
1361 | char *buf) | |
1362 | { | |
1363 | struct i2c_client *client = to_i2c_client(dev); | |
1364 | struct mip4_ts *ts = i2c_get_clientdata(client); | |
1365 | size_t count; | |
1366 | ||
1367 | /* Take lock to prevent racing with firmware update */ | |
1368 | mutex_lock(&ts->input->mutex); | |
1369 | ||
1370 | /* | |
1371 | * product_name shows the name or version of the hardware | |
1372 | * paired with current firmware in the chip. | |
1373 | */ | |
1374 | count = snprintf(buf, PAGE_SIZE, "%.*s\n", | |
4e1bff07 | 1375 | (int)sizeof(ts->product_name), ts->product_name); |
9aa40426 SJ |
1376 | |
1377 | mutex_unlock(&ts->input->mutex); | |
1378 | ||
1379 | return count; | |
1380 | } | |
1381 | ||
1382 | static DEVICE_ATTR(hw_version, S_IRUGO, mip4_sysfs_read_hw_version, NULL); | |
1383 | ||
e2aad666 SJ |
1384 | static ssize_t mip4_sysfs_read_product_id(struct device *dev, |
1385 | struct device_attribute *attr, | |
1386 | char *buf) | |
1387 | { | |
1388 | struct i2c_client *client = to_i2c_client(dev); | |
1389 | struct mip4_ts *ts = i2c_get_clientdata(client); | |
1390 | size_t count; | |
1391 | ||
1392 | mutex_lock(&ts->input->mutex); | |
1393 | ||
1394 | count = snprintf(buf, PAGE_SIZE, "%04X\n", ts->product_id); | |
1395 | ||
1396 | mutex_unlock(&ts->input->mutex); | |
1397 | ||
1398 | return count; | |
1399 | } | |
1400 | ||
1401 | static DEVICE_ATTR(product_id, S_IRUGO, mip4_sysfs_read_product_id, NULL); | |
1402 | ||
4e1bff07 SJ |
1403 | static ssize_t mip4_sysfs_read_ic_name(struct device *dev, |
1404 | struct device_attribute *attr, | |
1405 | char *buf) | |
1406 | { | |
1407 | struct i2c_client *client = to_i2c_client(dev); | |
1408 | struct mip4_ts *ts = i2c_get_clientdata(client); | |
1409 | size_t count; | |
1410 | ||
1411 | mutex_lock(&ts->input->mutex); | |
1412 | ||
1413 | count = snprintf(buf, PAGE_SIZE, "%.*s\n", | |
1414 | (int)sizeof(ts->ic_name), ts->ic_name); | |
1415 | ||
1416 | mutex_unlock(&ts->input->mutex); | |
1417 | ||
1418 | return count; | |
1419 | } | |
1420 | ||
1421 | static DEVICE_ATTR(ic_name, S_IRUGO, mip4_sysfs_read_ic_name, NULL); | |
1422 | ||
6ccb1d8f SJ |
1423 | static struct attribute *mip4_attrs[] = { |
1424 | &dev_attr_fw_version.attr, | |
9aa40426 | 1425 | &dev_attr_hw_version.attr, |
e2aad666 | 1426 | &dev_attr_product_id.attr, |
4e1bff07 | 1427 | &dev_attr_ic_name.attr, |
6ccb1d8f SJ |
1428 | &dev_attr_update_fw.attr, |
1429 | NULL, | |
1430 | }; | |
1431 | ||
1432 | static const struct attribute_group mip4_attr_group = { | |
1433 | .attrs = mip4_attrs, | |
1434 | }; | |
1435 | ||
6ccb1d8f SJ |
1436 | static int mip4_probe(struct i2c_client *client, const struct i2c_device_id *id) |
1437 | { | |
1438 | struct mip4_ts *ts; | |
1439 | struct input_dev *input; | |
1440 | int error; | |
1441 | ||
1442 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | |
1443 | dev_err(&client->dev, "Not supported I2C adapter\n"); | |
1444 | return -ENXIO; | |
1445 | } | |
1446 | ||
1447 | ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL); | |
1448 | if (!ts) | |
1449 | return -ENOMEM; | |
1450 | ||
1451 | input = devm_input_allocate_device(&client->dev); | |
1452 | if (!input) | |
1453 | return -ENOMEM; | |
1454 | ||
1455 | ts->client = client; | |
1456 | ts->input = input; | |
1457 | ||
1458 | snprintf(ts->phys, sizeof(ts->phys), | |
1459 | "%s/input0", dev_name(&client->dev)); | |
1460 | ||
1461 | ts->gpio_ce = devm_gpiod_get_optional(&client->dev, | |
1462 | "ce", GPIOD_OUT_LOW); | |
1463 | if (IS_ERR(ts->gpio_ce)) { | |
1464 | error = PTR_ERR(ts->gpio_ce); | |
1465 | if (error != EPROBE_DEFER) | |
1466 | dev_err(&client->dev, | |
1467 | "Failed to get gpio: %d\n", error); | |
1468 | return error; | |
1469 | } | |
1470 | ||
1471 | error = mip4_power_on(ts); | |
1472 | if (error) | |
1473 | return error; | |
1474 | error = mip4_query_device(ts); | |
1475 | mip4_power_off(ts); | |
1476 | if (error) | |
1477 | return error; | |
1478 | ||
1479 | input->name = "MELFAS MIP4 Touchscreen"; | |
1480 | input->phys = ts->phys; | |
1481 | ||
1482 | input->id.bustype = BUS_I2C; | |
1483 | input->id.vendor = 0x13c5; | |
c3fc6089 | 1484 | input->id.product = ts->product_id; |
6ccb1d8f SJ |
1485 | |
1486 | input->open = mip4_input_open; | |
1487 | input->close = mip4_input_close; | |
1488 | ||
1489 | input_set_drvdata(input, ts); | |
1490 | ||
1491 | input->keycode = ts->key_code; | |
1492 | input->keycodesize = sizeof(*ts->key_code); | |
1493 | input->keycodemax = ts->key_num; | |
1494 | ||
1495 | input_set_abs_params(input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0); | |
1496 | input_set_abs_params(input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0); | |
1497 | input_set_abs_params(input, ABS_MT_PRESSURE, | |
1498 | MIP4_PRESSURE_MIN, MIP4_PRESSURE_MAX, 0, 0); | |
1499 | input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, | |
1500 | MIP4_TOUCH_MAJOR_MIN, MIP4_TOUCH_MAJOR_MAX, 0, 0); | |
1501 | input_set_abs_params(input, ABS_MT_TOUCH_MINOR, | |
1502 | MIP4_TOUCH_MINOR_MIN, MIP4_TOUCH_MINOR_MAX, 0, 0); | |
7a3ed686 SJ |
1503 | input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->ppm_x); |
1504 | input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->ppm_y); | |
6ccb1d8f SJ |
1505 | |
1506 | error = input_mt_init_slots(input, MIP4_MAX_FINGERS, INPUT_MT_DIRECT); | |
1507 | if (error) | |
1508 | return error; | |
1509 | ||
1510 | i2c_set_clientdata(client, ts); | |
1511 | ||
1512 | error = devm_request_threaded_irq(&client->dev, client->irq, | |
1513 | NULL, mip4_interrupt, | |
1514 | IRQF_ONESHOT, MIP4_DEVICE_NAME, ts); | |
1515 | if (error) { | |
1516 | dev_err(&client->dev, | |
1517 | "Failed to request interrupt %d: %d\n", | |
1518 | client->irq, error); | |
1519 | return error; | |
1520 | } | |
1521 | ||
1522 | disable_irq(client->irq); | |
1523 | ||
1524 | error = input_register_device(input); | |
1525 | if (error) { | |
1526 | dev_err(&client->dev, | |
1527 | "Failed to register input device: %d\n", error); | |
1528 | return error; | |
1529 | } | |
1530 | ||
4c16f82e | 1531 | error = devm_device_add_group(&client->dev, &mip4_attr_group); |
6ccb1d8f SJ |
1532 | if (error) { |
1533 | dev_err(&client->dev, | |
1534 | "Failed to create sysfs attribute group: %d\n", error); | |
1535 | return error; | |
1536 | } | |
1537 | ||
6ccb1d8f SJ |
1538 | return 0; |
1539 | } | |
1540 | ||
1541 | static int __maybe_unused mip4_suspend(struct device *dev) | |
1542 | { | |
1543 | struct i2c_client *client = to_i2c_client(dev); | |
1544 | struct mip4_ts *ts = i2c_get_clientdata(client); | |
1545 | struct input_dev *input = ts->input; | |
1546 | ||
1547 | mutex_lock(&input->mutex); | |
1548 | ||
1549 | if (device_may_wakeup(dev)) | |
1550 | ts->wake_irq_enabled = enable_irq_wake(client->irq) == 0; | |
1551 | else if (input->users) | |
1552 | mip4_disable(ts); | |
1553 | ||
1554 | mutex_unlock(&input->mutex); | |
1555 | ||
1556 | return 0; | |
1557 | } | |
1558 | ||
1559 | static int __maybe_unused mip4_resume(struct device *dev) | |
1560 | { | |
1561 | struct i2c_client *client = to_i2c_client(dev); | |
1562 | struct mip4_ts *ts = i2c_get_clientdata(client); | |
1563 | struct input_dev *input = ts->input; | |
1564 | ||
1565 | mutex_lock(&input->mutex); | |
1566 | ||
1567 | if (ts->wake_irq_enabled) | |
1568 | disable_irq_wake(client->irq); | |
1569 | else if (input->users) | |
1570 | mip4_enable(ts); | |
1571 | ||
1572 | mutex_unlock(&input->mutex); | |
1573 | ||
1574 | return 0; | |
1575 | } | |
1576 | ||
1577 | static SIMPLE_DEV_PM_OPS(mip4_pm_ops, mip4_suspend, mip4_resume); | |
1578 | ||
1579 | #ifdef CONFIG_OF | |
1580 | static const struct of_device_id mip4_of_match[] = { | |
1581 | { .compatible = "melfas,"MIP4_DEVICE_NAME, }, | |
1582 | { }, | |
1583 | }; | |
1584 | MODULE_DEVICE_TABLE(of, mip4_of_match); | |
1585 | #endif | |
1586 | ||
1587 | #ifdef CONFIG_ACPI | |
1588 | static const struct acpi_device_id mip4_acpi_match[] = { | |
1589 | { "MLFS0000", 0}, | |
1590 | { }, | |
1591 | }; | |
1592 | MODULE_DEVICE_TABLE(acpi, mip4_acpi_match); | |
1593 | #endif | |
1594 | ||
1595 | static const struct i2c_device_id mip4_i2c_ids[] = { | |
1596 | { MIP4_DEVICE_NAME, 0 }, | |
1597 | { }, | |
1598 | }; | |
1599 | MODULE_DEVICE_TABLE(i2c, mip4_i2c_ids); | |
1600 | ||
1601 | static struct i2c_driver mip4_driver = { | |
1602 | .id_table = mip4_i2c_ids, | |
1603 | .probe = mip4_probe, | |
1604 | .driver = { | |
1605 | .name = MIP4_DEVICE_NAME, | |
1606 | .of_match_table = of_match_ptr(mip4_of_match), | |
1607 | .acpi_match_table = ACPI_PTR(mip4_acpi_match), | |
1608 | .pm = &mip4_pm_ops, | |
1609 | }, | |
1610 | }; | |
1611 | module_i2c_driver(mip4_driver); | |
1612 | ||
1613 | MODULE_DESCRIPTION("MELFAS MIP4 Touchscreen"); | |
6ccb1d8f SJ |
1614 | MODULE_AUTHOR("Sangwon Jee <jeesw@melfas.com>"); |
1615 | MODULE_LICENSE("GPL"); |