Sangwon Jee | 6ccb1d8 | 2016-01-14 17:55:54 +0900 | [diff] [blame^] | 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 |
| 36 | * Version : MIP 4.0 Rev 4.4 |
| 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 |
| 55 | #define MIP4_R1_INFO_VERSION_BOOT 0x20 |
| 56 | #define MIP4_R1_INFO_VERSION_CORE 0x22 |
| 57 | #define MIP4_R1_INFO_VERSION_APP 0x24 |
| 58 | #define MIP4_R1_INFO_VERSION_PARAM 0x26 |
| 59 | #define MIP4_R1_INFO_SECT_BOOT_START 0x30 |
| 60 | #define MIP4_R1_INFO_SECT_BOOT_END 0x31 |
| 61 | #define MIP4_R1_INFO_SECT_CORE_START 0x32 |
| 62 | #define MIP4_R1_INFO_SECT_CORE_END 0x33 |
| 63 | #define MIP4_R1_INFO_SECT_APP_START 0x34 |
| 64 | #define MIP4_R1_INFO_SECT_APP_END 0x35 |
| 65 | #define MIP4_R1_INFO_SECT_PARAM_START 0x36 |
| 66 | #define MIP4_R1_INFO_SECT_PARAM_END 0x37 |
| 67 | #define MIP4_R1_INFO_BUILD_DATE 0x40 |
| 68 | #define MIP4_R1_INFO_BUILD_TIME 0x44 |
| 69 | #define MIP4_R1_INFO_CHECKSUM_PRECALC 0x48 |
| 70 | #define MIP4_R1_INFO_CHECKSUM_REALTIME 0x4A |
| 71 | #define MIP4_R1_INFO_PROTOCOL_NAME 0x50 |
| 72 | #define MIP4_R1_INFO_PROTOCOL_VERSION 0x58 |
| 73 | #define MIP4_R1_INFO_IC_ID 0x70 |
| 74 | #define MIP4_R1_INFO_IC_NAME 0x71 |
| 75 | #define MIP4_R1_INFO_IC_VENDOR_ID 0x75 |
| 76 | #define MIP4_R1_INFO_IC_HW_CATEGORY 0x77 |
| 77 | #define MIP4_R1_INFO_CONTACT_THD_SCR 0x78 |
| 78 | #define MIP4_R1_INFO_CONTACT_THD_KEY 0x7A |
| 79 | |
| 80 | #define MIP4_R0_EVENT 0x02 |
| 81 | #define MIP4_R1_EVENT_SUPPORTED_FUNC 0x00 |
| 82 | #define MIP4_R1_EVENT_FORMAT 0x04 |
| 83 | #define MIP4_R1_EVENT_SIZE 0x06 |
| 84 | #define MIP4_R1_EVENT_PACKET_INFO 0x10 |
| 85 | #define MIP4_R1_EVENT_PACKET_DATA 0x11 |
| 86 | |
| 87 | #define MIP4_R0_CTRL 0x06 |
| 88 | #define MIP4_R1_CTRL_READY_STATUS 0x00 |
| 89 | #define MIP4_R1_CTRL_EVENT_READY 0x01 |
| 90 | #define MIP4_R1_CTRL_MODE 0x10 |
| 91 | #define MIP4_R1_CTRL_EVENT_TRIGGER_TYPE 0x11 |
| 92 | #define MIP4_R1_CTRL_RECALIBRATE 0x12 |
| 93 | #define MIP4_R1_CTRL_POWER_STATE 0x13 |
| 94 | #define MIP4_R1_CTRL_GESTURE_TYPE 0x14 |
| 95 | #define MIP4_R1_CTRL_DISABLE_ESD_ALERT 0x18 |
| 96 | #define MIP4_R1_CTRL_CHARGER_MODE 0x19 |
| 97 | #define MIP4_R1_CTRL_HIGH_SENS_MODE 0x1A |
| 98 | #define MIP4_R1_CTRL_WINDOW_MODE 0x1B |
| 99 | #define MIP4_R1_CTRL_PALM_REJECTION 0x1C |
| 100 | #define MIP4_R1_CTRL_EDGE_CORRECTION 0x1D |
| 101 | #define MIP4_R1_CTRL_ENTER_GLOVE_MODE 0x1E |
| 102 | #define MIP4_R1_CTRL_I2C_ON_LPM 0x1F |
| 103 | #define MIP4_R1_CTRL_GESTURE_DEBUG 0x20 |
| 104 | #define MIP4_R1_CTRL_PALM_EVENT 0x22 |
| 105 | #define MIP4_R1_CTRL_PROXIMITY_SENSING 0x23 |
| 106 | |
| 107 | /* Value */ |
| 108 | #define MIP4_BOOT_MODE_BOOT 0x01 |
| 109 | #define MIP4_BOOT_MODE_APP 0x02 |
| 110 | |
| 111 | #define MIP4_BOOT_STATUS_BUSY 0x05 |
| 112 | #define MIP4_BOOT_STATUS_ERROR 0x0E |
| 113 | #define MIP4_BOOT_STATUS_DONE 0xA0 |
| 114 | |
| 115 | #define MIP4_BOOT_CMD_MASS_ERASE 0x15 |
| 116 | #define MIP4_BOOT_CMD_PROGRAM 0x54 |
| 117 | #define MIP4_BOOT_CMD_ERASE 0x8F |
| 118 | #define MIP4_BOOT_CMD_WRITE 0xA5 |
| 119 | #define MIP4_BOOT_CMD_READ 0xC2 |
| 120 | |
| 121 | #define MIP4_EVENT_INPUT_TYPE_KEY 0 |
| 122 | #define MIP4_EVENT_INPUT_TYPE_SCREEN 1 |
| 123 | #define MIP4_EVENT_INPUT_TYPE_PROXIMITY 2 |
| 124 | |
| 125 | #define I2C_RETRY_COUNT 3 /* 2~ */ |
| 126 | |
| 127 | #define MIP4_BUF_SIZE 128 |
| 128 | #define MIP4_MAX_FINGERS 10 |
| 129 | #define MIP4_MAX_KEYS 4 |
| 130 | |
| 131 | #define MIP4_TOUCH_MAJOR_MIN 0 |
| 132 | #define MIP4_TOUCH_MAJOR_MAX 255 |
| 133 | #define MIP4_TOUCH_MINOR_MIN 0 |
| 134 | #define MIP4_TOUCH_MINOR_MAX 255 |
| 135 | #define MIP4_PRESSURE_MIN 0 |
| 136 | #define MIP4_PRESSURE_MAX 255 |
| 137 | |
| 138 | #define MIP4_FW_NAME "melfas_mip4.fw" |
| 139 | #define MIP4_FW_UPDATE_DEBUG 0 /* 0 (default) or 1 */ |
| 140 | |
| 141 | struct mip4_fw_version { |
| 142 | u16 boot; |
| 143 | u16 core; |
| 144 | u16 app; |
| 145 | u16 param; |
| 146 | }; |
| 147 | |
| 148 | struct mip4_ts { |
| 149 | struct i2c_client *client; |
| 150 | struct input_dev *input; |
| 151 | struct gpio_desc *gpio_ce; |
| 152 | |
| 153 | char phys[32]; |
| 154 | char product_name[16]; |
| 155 | |
| 156 | unsigned int max_x; |
| 157 | unsigned int max_y; |
| 158 | u8 node_x; |
| 159 | u8 node_y; |
| 160 | u8 node_key; |
| 161 | |
| 162 | struct mip4_fw_version fw_version; |
| 163 | |
| 164 | unsigned int event_size; |
| 165 | unsigned int event_format; |
| 166 | |
| 167 | unsigned int key_num; |
| 168 | unsigned short key_code[MIP4_MAX_KEYS]; |
| 169 | |
| 170 | bool wake_irq_enabled; |
| 171 | |
| 172 | u8 buf[MIP4_BUF_SIZE]; |
| 173 | }; |
| 174 | |
| 175 | static int mip4_i2c_xfer(struct mip4_ts *ts, |
| 176 | char *write_buf, unsigned int write_len, |
| 177 | char *read_buf, unsigned int read_len) |
| 178 | { |
| 179 | struct i2c_msg msg[] = { |
| 180 | { |
| 181 | .addr = ts->client->addr, |
| 182 | .flags = 0, |
| 183 | .buf = write_buf, |
| 184 | .len = write_len, |
| 185 | }, { |
| 186 | .addr = ts->client->addr, |
| 187 | .flags = I2C_M_RD, |
| 188 | .buf = read_buf, |
| 189 | .len = read_len, |
| 190 | }, |
| 191 | }; |
| 192 | int retry = I2C_RETRY_COUNT; |
| 193 | int res; |
| 194 | int error; |
| 195 | |
| 196 | do { |
| 197 | res = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg)); |
| 198 | if (res == ARRAY_SIZE(msg)) |
| 199 | return 0; |
| 200 | |
| 201 | error = res < 0 ? res : -EIO; |
| 202 | dev_err(&ts->client->dev, |
| 203 | "%s - i2c_transfer failed: %d (%d)\n", |
| 204 | __func__, error, res); |
| 205 | } while (--retry); |
| 206 | |
| 207 | return error; |
| 208 | } |
| 209 | |
| 210 | static void mip4_parse_fw_version(const u8 *buf, struct mip4_fw_version *v) |
| 211 | { |
| 212 | v->boot = get_unaligned_le16(buf + 0); |
| 213 | v->core = get_unaligned_le16(buf + 2); |
| 214 | v->app = get_unaligned_le16(buf + 4); |
| 215 | v->param = get_unaligned_le16(buf + 6); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Read chip firmware version |
| 220 | */ |
| 221 | static int mip4_get_fw_version(struct mip4_ts *ts) |
| 222 | { |
| 223 | u8 cmd[] = { MIP4_R0_INFO, MIP4_R1_INFO_VERSION_BOOT }; |
| 224 | u8 buf[sizeof(ts->fw_version)]; |
| 225 | int error; |
| 226 | |
| 227 | error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, sizeof(buf)); |
| 228 | if (error) { |
| 229 | memset(&ts->fw_version, 0xff, sizeof(ts->fw_version)); |
| 230 | return error; |
| 231 | } |
| 232 | |
| 233 | mip4_parse_fw_version(buf, &ts->fw_version); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Fetch device characteristics |
| 240 | */ |
| 241 | static int mip4_query_device(struct mip4_ts *ts) |
| 242 | { |
| 243 | int error; |
| 244 | u8 cmd[2]; |
| 245 | u8 buf[8]; |
| 246 | |
| 247 | /* Product name */ |
| 248 | cmd[0] = MIP4_R0_INFO; |
| 249 | cmd[1] = MIP4_R1_INFO_PRODUCT_NAME; |
| 250 | error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), |
| 251 | ts->product_name, sizeof(ts->product_name)); |
| 252 | if (error) |
| 253 | dev_warn(&ts->client->dev, |
| 254 | "Failed to retrieve product name: %d\n", error); |
| 255 | else |
| 256 | dev_dbg(&ts->client->dev, "product name: %.*s\n", |
| 257 | (int)sizeof(ts->product_name), ts->product_name); |
| 258 | |
| 259 | /* Firmware version */ |
| 260 | error = mip4_get_fw_version(ts); |
| 261 | if (error) |
| 262 | dev_warn(&ts->client->dev, |
| 263 | "Failed to retrieve FW version: %d\n", error); |
| 264 | else |
| 265 | dev_dbg(&ts->client->dev, "F/W Version: %04X %04X %04X %04X\n", |
| 266 | ts->fw_version.boot, ts->fw_version.core, |
| 267 | ts->fw_version.app, ts->fw_version.param); |
| 268 | |
| 269 | /* Resolution */ |
| 270 | cmd[0] = MIP4_R0_INFO; |
| 271 | cmd[1] = MIP4_R1_INFO_RESOLUTION_X; |
| 272 | error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 7); |
| 273 | if (error) { |
| 274 | dev_warn(&ts->client->dev, |
| 275 | "Failed to retrieve touchscreen parameters: %d\n", |
| 276 | error); |
| 277 | } else { |
| 278 | ts->max_x = get_unaligned_le16(&buf[0]); |
| 279 | ts->max_y = get_unaligned_le16(&buf[2]); |
| 280 | dev_dbg(&ts->client->dev, "max_x: %d, max_y: %d\n", |
| 281 | ts->max_x, ts->max_y); |
| 282 | |
| 283 | ts->node_x = buf[4]; |
| 284 | ts->node_y = buf[5]; |
| 285 | ts->node_key = buf[6]; |
| 286 | dev_dbg(&ts->client->dev, |
| 287 | "node_x: %d, node_y: %d, node_key: %d\n", |
| 288 | ts->node_x, ts->node_y, ts->node_key); |
| 289 | |
| 290 | /* Key ts */ |
| 291 | if (ts->node_key > 0) |
| 292 | ts->key_num = ts->node_key; |
| 293 | } |
| 294 | |
| 295 | /* Protocol */ |
| 296 | cmd[0] = MIP4_R0_EVENT; |
| 297 | cmd[1] = MIP4_R1_EVENT_SUPPORTED_FUNC; |
| 298 | error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 7); |
| 299 | if (error) { |
| 300 | dev_warn(&ts->client->dev, |
| 301 | "Failed to retrieve device type: %d\n", error); |
| 302 | ts->event_format = 0xff; |
| 303 | } else { |
| 304 | ts->event_format = get_unaligned_le16(&buf[4]); |
| 305 | ts->event_size = buf[6]; |
| 306 | dev_dbg(&ts->client->dev, "event_format: %d, event_size: %d\n", |
| 307 | ts->event_format, ts->event_size); |
| 308 | |
| 309 | if (ts->event_format == 2 || ts->event_format > 3) |
| 310 | dev_warn(&ts->client->dev, |
| 311 | "Unknown event format %d\n", ts->event_format); |
| 312 | } |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static int mip4_power_on(struct mip4_ts *ts) |
| 318 | { |
| 319 | if (ts->gpio_ce) { |
| 320 | gpiod_set_value_cansleep(ts->gpio_ce, 1); |
| 321 | |
| 322 | /* Booting delay : 200~300ms */ |
| 323 | usleep_range(200 * 1000, 300 * 1000); |
| 324 | } |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static void mip4_power_off(struct mip4_ts *ts) |
| 330 | { |
| 331 | if (ts->gpio_ce) |
| 332 | gpiod_set_value_cansleep(ts->gpio_ce, 0); |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Clear touch input event status |
| 337 | */ |
| 338 | static void mip4_clear_input(struct mip4_ts *ts) |
| 339 | { |
| 340 | int i; |
| 341 | |
| 342 | /* Screen */ |
| 343 | for (i = 0; i < MIP4_MAX_FINGERS; i++) { |
| 344 | input_mt_slot(ts->input, i); |
| 345 | input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 0); |
| 346 | } |
| 347 | |
| 348 | /* Keys */ |
| 349 | for (i = 0; i < ts->key_num; i++) |
| 350 | input_report_key(ts->input, ts->key_code[i], 0); |
| 351 | |
| 352 | input_sync(ts->input); |
| 353 | } |
| 354 | |
| 355 | static int mip4_enable(struct mip4_ts *ts) |
| 356 | { |
| 357 | int error; |
| 358 | |
| 359 | error = mip4_power_on(ts); |
| 360 | if (error) |
| 361 | return error; |
| 362 | |
| 363 | enable_irq(ts->client->irq); |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | static void mip4_disable(struct mip4_ts *ts) |
| 369 | { |
| 370 | disable_irq(ts->client->irq); |
| 371 | |
| 372 | mip4_power_off(ts); |
| 373 | |
| 374 | mip4_clear_input(ts); |
| 375 | } |
| 376 | |
| 377 | /***************************************************************** |
| 378 | * Input handling |
| 379 | *****************************************************************/ |
| 380 | |
| 381 | static void mip4_report_keys(struct mip4_ts *ts, u8 *packet) |
| 382 | { |
| 383 | u8 key; |
| 384 | bool down; |
| 385 | |
| 386 | switch (ts->event_format) { |
| 387 | case 0: |
| 388 | case 1: |
| 389 | key = packet[0] & 0x0F; |
| 390 | down = packet[0] & 0x80; |
| 391 | break; |
| 392 | |
| 393 | case 3: |
| 394 | default: |
| 395 | key = packet[0] & 0x0F; |
| 396 | down = packet[1] & 0x01; |
| 397 | break; |
| 398 | } |
| 399 | |
| 400 | /* Report key event */ |
| 401 | if (key >= 1 && key <= ts->key_num) { |
| 402 | unsigned short keycode = ts->key_code[key - 1]; |
| 403 | |
| 404 | dev_dbg(&ts->client->dev, |
| 405 | "Key - ID: %d, keycode: %d, state: %d\n", |
| 406 | key, keycode, down); |
| 407 | |
| 408 | input_event(ts->input, EV_MSC, MSC_SCAN, keycode); |
| 409 | input_report_key(ts->input, keycode, down); |
| 410 | |
| 411 | } else { |
| 412 | dev_err(&ts->client->dev, "Unknown key: %d\n", key); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | static void mip4_report_touch(struct mip4_ts *ts, u8 *packet) |
| 417 | { |
| 418 | int id; |
| 419 | bool hover; |
| 420 | bool palm; |
| 421 | bool state; |
| 422 | u16 x, y; |
| 423 | u8 pressure_stage = 0; |
| 424 | u8 pressure; |
| 425 | u8 size; |
| 426 | u8 touch_major; |
| 427 | u8 touch_minor; |
| 428 | |
| 429 | switch (ts->event_format) { |
| 430 | case 0: |
| 431 | case 1: |
| 432 | /* Touch only */ |
| 433 | state = packet[0] & BIT(7); |
| 434 | hover = packet[0] & BIT(5); |
| 435 | palm = packet[0] & BIT(4); |
| 436 | id = (packet[0] & 0x0F) - 1; |
| 437 | x = ((packet[1] & 0x0F) << 8) | packet[2]; |
| 438 | y = (((packet[1] >> 4) & 0x0F) << 8) | |
| 439 | packet[3]; |
| 440 | pressure = packet[4]; |
| 441 | size = packet[5]; |
| 442 | if (ts->event_format == 0) { |
| 443 | touch_major = packet[5]; |
| 444 | touch_minor = packet[5]; |
| 445 | } else { |
| 446 | touch_major = packet[6]; |
| 447 | touch_minor = packet[7]; |
| 448 | } |
| 449 | break; |
| 450 | |
| 451 | case 3: |
| 452 | default: |
| 453 | /* Touch + Force(Pressure) */ |
| 454 | id = (packet[0] & 0x0F) - 1; |
| 455 | hover = packet[1] & BIT(2); |
| 456 | palm = packet[1] & BIT(1); |
| 457 | state = packet[1] & BIT(0); |
| 458 | x = ((packet[2] & 0x0F) << 8) | packet[3]; |
| 459 | y = (((packet[2] >> 4) & 0x0F) << 8) | |
| 460 | packet[4]; |
| 461 | size = packet[6]; |
| 462 | pressure_stage = (packet[7] & 0xF0) >> 4; |
| 463 | pressure = ((packet[7] & 0x0F) << 8) | |
| 464 | packet[8]; |
| 465 | touch_major = packet[9]; |
| 466 | touch_minor = packet[10]; |
| 467 | break; |
| 468 | } |
| 469 | |
| 470 | dev_dbg(&ts->client->dev, |
| 471 | "Screen - Slot: %d State: %d X: %04d Y: %04d Z: %d\n", |
| 472 | id, state, x, y, pressure); |
| 473 | |
| 474 | if (unlikely(id < 0 || id >= MIP4_MAX_FINGERS)) { |
| 475 | dev_err(&ts->client->dev, "Screen - invalid slot ID: %d\n", id); |
| 476 | } else if (state) { |
| 477 | /* Press or Move event */ |
| 478 | input_mt_slot(ts->input, id); |
| 479 | input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true); |
| 480 | input_report_abs(ts->input, ABS_MT_POSITION_X, x); |
| 481 | input_report_abs(ts->input, ABS_MT_POSITION_Y, y); |
| 482 | input_report_abs(ts->input, ABS_MT_PRESSURE, pressure); |
| 483 | input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, touch_major); |
| 484 | input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, touch_minor); |
| 485 | } else { |
| 486 | /* Release event */ |
| 487 | input_mt_slot(ts->input, id); |
| 488 | input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 0); |
| 489 | } |
| 490 | |
| 491 | input_mt_sync_frame(ts->input); |
| 492 | } |
| 493 | |
| 494 | static int mip4_handle_packet(struct mip4_ts *ts, u8 *packet) |
| 495 | { |
| 496 | u8 type; |
| 497 | |
| 498 | switch (ts->event_format) { |
| 499 | case 0: |
| 500 | case 1: |
| 501 | type = (packet[0] & 0x40) >> 6; |
| 502 | break; |
| 503 | |
| 504 | case 3: |
| 505 | type = (packet[0] & 0xF0) >> 4; |
| 506 | break; |
| 507 | |
| 508 | default: |
| 509 | /* Should not happen unless we have corrupted firmware */ |
| 510 | return -EINVAL; |
| 511 | } |
| 512 | |
| 513 | dev_dbg(&ts->client->dev, "Type: %d\n", type); |
| 514 | |
| 515 | /* Report input event */ |
| 516 | switch (type) { |
| 517 | case MIP4_EVENT_INPUT_TYPE_KEY: |
| 518 | mip4_report_keys(ts, packet); |
| 519 | break; |
| 520 | |
| 521 | case MIP4_EVENT_INPUT_TYPE_SCREEN: |
| 522 | mip4_report_touch(ts, packet); |
| 523 | break; |
| 524 | |
| 525 | default: |
| 526 | dev_err(&ts->client->dev, "Unknown event type: %d\n", type); |
| 527 | break; |
| 528 | } |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | static irqreturn_t mip4_interrupt(int irq, void *dev_id) |
| 534 | { |
| 535 | struct mip4_ts *ts = dev_id; |
| 536 | struct i2c_client *client = ts->client; |
| 537 | unsigned int i; |
| 538 | int error; |
| 539 | u8 cmd[2]; |
| 540 | u8 size; |
| 541 | bool alert; |
| 542 | |
| 543 | /* Read packet info */ |
| 544 | cmd[0] = MIP4_R0_EVENT; |
| 545 | cmd[1] = MIP4_R1_EVENT_PACKET_INFO; |
| 546 | error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), ts->buf, 1); |
| 547 | if (error) { |
| 548 | dev_err(&client->dev, |
| 549 | "Failed to read packet info: %d\n", error); |
| 550 | goto out; |
| 551 | } |
| 552 | |
| 553 | size = ts->buf[0] & 0x7F; |
| 554 | alert = ts->buf[0] & BIT(7); |
| 555 | dev_dbg(&client->dev, "packet size: %d, alert: %d\n", size, alert); |
| 556 | |
| 557 | /* Check size */ |
| 558 | if (!size) { |
| 559 | dev_err(&client->dev, "Empty packet\n"); |
| 560 | goto out; |
| 561 | } |
| 562 | |
| 563 | /* Read packet data */ |
| 564 | cmd[0] = MIP4_R0_EVENT; |
| 565 | cmd[1] = MIP4_R1_EVENT_PACKET_DATA; |
| 566 | error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), ts->buf, size); |
| 567 | if (error) { |
| 568 | dev_err(&client->dev, |
| 569 | "Failed to read packet data: %d\n", error); |
| 570 | goto out; |
| 571 | } |
| 572 | |
| 573 | if (alert) { |
| 574 | dev_dbg(&client->dev, "Alert: %d\n", ts->buf[0]); |
| 575 | } else { |
| 576 | for (i = 0; i < size; i += ts->event_size) { |
| 577 | error = mip4_handle_packet(ts, &ts->buf[i]); |
| 578 | if (error) |
| 579 | break; |
| 580 | } |
| 581 | |
| 582 | input_sync(ts->input); |
| 583 | } |
| 584 | |
| 585 | out: |
| 586 | return IRQ_HANDLED; |
| 587 | } |
| 588 | |
| 589 | static int mip4_input_open(struct input_dev *dev) |
| 590 | { |
| 591 | struct mip4_ts *ts = input_get_drvdata(dev); |
| 592 | |
| 593 | return mip4_enable(ts); |
| 594 | } |
| 595 | |
| 596 | static void mip4_input_close(struct input_dev *dev) |
| 597 | { |
| 598 | struct mip4_ts *ts = input_get_drvdata(dev); |
| 599 | |
| 600 | mip4_disable(ts); |
| 601 | } |
| 602 | |
| 603 | /***************************************************************** |
| 604 | * Firmware update |
| 605 | *****************************************************************/ |
| 606 | |
| 607 | /* Firmware Info */ |
| 608 | #define MIP4_BL_PAGE_SIZE 512 /* 512 */ |
| 609 | #define MIP4_BL_PACKET_SIZE 512 /* 512, 256, 128, 64, ... */ |
| 610 | |
| 611 | /* |
| 612 | * Firmware binary tail info |
| 613 | */ |
| 614 | |
| 615 | struct mip4_bin_tail { |
| 616 | u8 tail_mark[4]; |
| 617 | u8 chip_name[4]; |
| 618 | |
| 619 | __le32 bin_start_addr; |
| 620 | __le32 bin_length; |
| 621 | |
| 622 | __le16 ver_boot; |
| 623 | __le16 ver_core; |
| 624 | __le16 ver_app; |
| 625 | __le16 ver_param; |
| 626 | |
| 627 | u8 boot_start; |
| 628 | u8 boot_end; |
| 629 | u8 core_start; |
| 630 | u8 core_end; |
| 631 | u8 app_start; |
| 632 | u8 app_end; |
| 633 | u8 param_start; |
| 634 | u8 param_end; |
| 635 | |
| 636 | u8 checksum_type; |
| 637 | u8 hw_category; |
| 638 | |
| 639 | __le16 param_id; |
| 640 | __le32 param_length; |
| 641 | __le32 build_date; |
| 642 | __le32 build_time; |
| 643 | |
| 644 | __le32 reserved1; |
| 645 | __le32 reserved2; |
| 646 | __le16 reserved3; |
| 647 | __le16 tail_size; |
| 648 | __le32 crc; |
| 649 | } __packed; |
| 650 | |
| 651 | #define MIP4_BIN_TAIL_MARK "MBT\001" |
| 652 | #define MIP4_BIN_TAIL_SIZE (sizeof(struct mip4_bin_tail)) |
| 653 | |
| 654 | /* |
| 655 | * Bootloader - Read status |
| 656 | */ |
| 657 | static int mip4_bl_read_status(struct mip4_ts *ts) |
| 658 | { |
| 659 | u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_STATUS }; |
| 660 | u8 result; |
| 661 | struct i2c_msg msg[] = { |
| 662 | { |
| 663 | .addr = ts->client->addr, |
| 664 | .flags = 0, |
| 665 | .buf = cmd, |
| 666 | .len = sizeof(cmd), |
| 667 | }, { |
| 668 | .addr = ts->client->addr, |
| 669 | .flags = I2C_M_RD, |
| 670 | .buf = &result, |
| 671 | .len = sizeof(result), |
| 672 | }, |
| 673 | }; |
| 674 | int ret; |
| 675 | int error; |
| 676 | int retry = 1000; |
| 677 | |
| 678 | do { |
| 679 | ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg)); |
| 680 | if (ret != ARRAY_SIZE(msg)) { |
| 681 | error = ret < 0 ? ret : -EIO; |
| 682 | dev_err(&ts->client->dev, |
| 683 | "Failed to read bootloader status: %d\n", |
| 684 | error); |
| 685 | return error; |
| 686 | } |
| 687 | |
| 688 | switch (result) { |
| 689 | case MIP4_BOOT_STATUS_DONE: |
| 690 | dev_dbg(&ts->client->dev, "%s - done\n", __func__); |
| 691 | return 0; |
| 692 | |
| 693 | case MIP4_BOOT_STATUS_ERROR: |
| 694 | dev_err(&ts->client->dev, "Bootloader failure\n"); |
| 695 | return -EIO; |
| 696 | |
| 697 | case MIP4_BOOT_STATUS_BUSY: |
| 698 | dev_dbg(&ts->client->dev, "%s - Busy\n", __func__); |
| 699 | error = -EBUSY; |
| 700 | break; |
| 701 | |
| 702 | default: |
| 703 | dev_err(&ts->client->dev, |
| 704 | "Unexpected bootloader status: %#02x\n", |
| 705 | result); |
| 706 | error = -EINVAL; |
| 707 | break; |
| 708 | } |
| 709 | |
| 710 | usleep_range(1000, 2000); |
| 711 | } while (--retry); |
| 712 | |
| 713 | return error; |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Bootloader - Change mode |
| 718 | */ |
| 719 | static int mip4_bl_change_mode(struct mip4_ts *ts, u8 mode) |
| 720 | { |
| 721 | u8 mode_chg_cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_MODE, mode }; |
| 722 | u8 mode_read_cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_MODE }; |
| 723 | u8 result; |
| 724 | struct i2c_msg msg[] = { |
| 725 | { |
| 726 | .addr = ts->client->addr, |
| 727 | .flags = 0, |
| 728 | .buf = mode_read_cmd, |
| 729 | .len = sizeof(mode_read_cmd), |
| 730 | }, { |
| 731 | .addr = ts->client->addr, |
| 732 | .flags = I2C_M_RD, |
| 733 | .buf = &result, |
| 734 | .len = sizeof(result), |
| 735 | }, |
| 736 | }; |
| 737 | int retry = 10; |
| 738 | int ret; |
| 739 | int error; |
| 740 | |
| 741 | do { |
| 742 | /* Send mode change command */ |
| 743 | ret = i2c_master_send(ts->client, |
| 744 | mode_chg_cmd, sizeof(mode_chg_cmd)); |
| 745 | if (ret != sizeof(mode_chg_cmd)) { |
| 746 | error = ret < 0 ? ret : -EIO; |
| 747 | dev_err(&ts->client->dev, |
| 748 | "Failed to send %d mode change: %d (%d)\n", |
| 749 | mode, error, ret); |
| 750 | return error; |
| 751 | } |
| 752 | |
| 753 | dev_dbg(&ts->client->dev, |
| 754 | "Sent mode change request (mode: %d)\n", mode); |
| 755 | |
| 756 | /* Wait */ |
| 757 | msleep(1000); |
| 758 | |
| 759 | /* Verify target mode */ |
| 760 | ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg)); |
| 761 | if (ret != ARRAY_SIZE(msg)) { |
| 762 | error = ret < 0 ? ret : -EIO; |
| 763 | dev_err(&ts->client->dev, |
| 764 | "Failed to read device mode: %d\n", error); |
| 765 | return error; |
| 766 | } |
| 767 | |
| 768 | dev_dbg(&ts->client->dev, |
| 769 | "Current device mode: %d, want: %d\n", result, mode); |
| 770 | |
| 771 | if (result == mode) |
| 772 | return 0; |
| 773 | |
| 774 | } while (--retry); |
| 775 | |
| 776 | return -EIO; |
| 777 | } |
| 778 | |
| 779 | /* |
| 780 | * Bootloader - Start bootloader mode |
| 781 | */ |
| 782 | static int mip4_bl_enter(struct mip4_ts *ts) |
| 783 | { |
| 784 | return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_BOOT); |
| 785 | } |
| 786 | |
| 787 | /* |
| 788 | * Bootloader - Exit bootloader mode |
| 789 | */ |
| 790 | static int mip4_bl_exit(struct mip4_ts *ts) |
| 791 | { |
| 792 | return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_APP); |
| 793 | } |
| 794 | |
| 795 | static int mip4_bl_get_address(struct mip4_ts *ts, u16 *buf_addr) |
| 796 | { |
| 797 | u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_BUF_ADDR }; |
| 798 | u8 result[sizeof(u16)]; |
| 799 | struct i2c_msg msg[] = { |
| 800 | { |
| 801 | .addr = ts->client->addr, |
| 802 | .flags = 0, |
| 803 | .buf = cmd, |
| 804 | .len = sizeof(cmd), |
| 805 | }, { |
| 806 | .addr = ts->client->addr, |
| 807 | .flags = I2C_M_RD, |
| 808 | .buf = result, |
| 809 | .len = sizeof(result), |
| 810 | }, |
| 811 | }; |
| 812 | int ret; |
| 813 | int error; |
| 814 | |
| 815 | ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg)); |
| 816 | if (ret != ARRAY_SIZE(msg)) { |
| 817 | error = ret < 0 ? ret : -EIO; |
| 818 | dev_err(&ts->client->dev, |
| 819 | "Failed to retrieve bootloader buffer address: %d\n", |
| 820 | error); |
| 821 | return error; |
| 822 | } |
| 823 | |
| 824 | *buf_addr = get_unaligned_le16(result); |
| 825 | dev_dbg(&ts->client->dev, |
| 826 | "Bootloader buffer address %#04x\n", *buf_addr); |
| 827 | |
| 828 | return 0; |
| 829 | } |
| 830 | |
| 831 | static int mip4_bl_program_page(struct mip4_ts *ts, int offset, |
| 832 | const u8 *data, int length, u16 buf_addr) |
| 833 | { |
| 834 | u8 cmd[6]; |
| 835 | u8 *data_buf; |
| 836 | u16 buf_offset; |
| 837 | int ret; |
| 838 | int error; |
| 839 | |
| 840 | dev_dbg(&ts->client->dev, "Writing page @%#06x (%d)\n", |
| 841 | offset, length); |
| 842 | |
| 843 | if (length > MIP4_BL_PAGE_SIZE || length % MIP4_BL_PACKET_SIZE) { |
| 844 | dev_err(&ts->client->dev, |
| 845 | "Invalid page length: %d\n", length); |
| 846 | return -EINVAL; |
| 847 | } |
| 848 | |
| 849 | data_buf = kmalloc(2 + MIP4_BL_PACKET_SIZE, GFP_KERNEL); |
| 850 | if (!data_buf) |
| 851 | return -ENOMEM; |
| 852 | |
| 853 | /* Addr */ |
| 854 | cmd[0] = MIP4_R0_BOOT; |
| 855 | cmd[1] = MIP4_R1_BOOT_TARGET_ADDR; |
| 856 | put_unaligned_le32(offset, &cmd[2]); |
| 857 | ret = i2c_master_send(ts->client, cmd, 6); |
| 858 | if (ret != 6) { |
| 859 | error = ret < 0 ? ret : -EIO; |
| 860 | dev_err(&ts->client->dev, |
| 861 | "Failed to send write page address: %d\n", error); |
| 862 | goto out; |
| 863 | } |
| 864 | |
| 865 | /* Size */ |
| 866 | cmd[0] = MIP4_R0_BOOT; |
| 867 | cmd[1] = MIP4_R1_BOOT_SIZE; |
| 868 | put_unaligned_le32(length, &cmd[2]); |
| 869 | ret = i2c_master_send(ts->client, cmd, 6); |
| 870 | if (ret != 6) { |
| 871 | error = ret < 0 ? ret : -EIO; |
| 872 | dev_err(&ts->client->dev, |
| 873 | "Failed to send write page size: %d\n", error); |
| 874 | goto out; |
| 875 | } |
| 876 | |
| 877 | /* Data */ |
| 878 | for (buf_offset = 0; |
| 879 | buf_offset < length; |
| 880 | buf_offset += MIP4_BL_PACKET_SIZE) { |
| 881 | dev_dbg(&ts->client->dev, |
| 882 | "writing chunk at %#04x (size %d)\n", |
| 883 | buf_offset, MIP4_BL_PACKET_SIZE); |
| 884 | put_unaligned_be16(buf_addr + buf_offset, data_buf); |
| 885 | memcpy(&data_buf[2], &data[buf_offset], MIP4_BL_PACKET_SIZE); |
| 886 | ret = i2c_master_send(ts->client, |
| 887 | data_buf, 2 + MIP4_BL_PACKET_SIZE); |
| 888 | if (ret != 2 + MIP4_BL_PACKET_SIZE) { |
| 889 | error = ret < 0 ? ret : -EIO; |
| 890 | dev_err(&ts->client->dev, |
| 891 | "Failed to read chunk at %#04x (size %d): %d\n", |
| 892 | buf_offset, MIP4_BL_PACKET_SIZE, error); |
| 893 | goto out; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | /* Command */ |
| 898 | cmd[0] = MIP4_R0_BOOT; |
| 899 | cmd[1] = MIP4_R1_BOOT_CMD; |
| 900 | cmd[2] = MIP4_BOOT_CMD_PROGRAM; |
| 901 | ret = i2c_master_send(ts->client, cmd, 3); |
| 902 | if (ret != 3) { |
| 903 | error = ret < 0 ? ret : -EIO; |
| 904 | dev_err(&ts->client->dev, |
| 905 | "Failed to send 'write' command: %d\n", error); |
| 906 | goto out; |
| 907 | } |
| 908 | |
| 909 | /* Status */ |
| 910 | error = mip4_bl_read_status(ts); |
| 911 | |
| 912 | out: |
| 913 | kfree(data_buf); |
| 914 | return error ? error : 0; |
| 915 | } |
| 916 | |
| 917 | static int mip4_bl_verify_page(struct mip4_ts *ts, int offset, |
| 918 | const u8 *data, int length, int buf_addr) |
| 919 | { |
| 920 | u8 cmd[8]; |
| 921 | u8 *read_buf; |
| 922 | int buf_offset; |
| 923 | struct i2c_msg msg[] = { |
| 924 | { |
| 925 | .addr = ts->client->addr, |
| 926 | .flags = 0, |
| 927 | .buf = cmd, |
| 928 | .len = 2, |
| 929 | }, { |
| 930 | .addr = ts->client->addr, |
| 931 | .flags = I2C_M_RD, |
| 932 | .len = MIP4_BL_PACKET_SIZE, |
| 933 | }, |
| 934 | }; |
| 935 | int ret; |
| 936 | int error; |
| 937 | |
| 938 | dev_dbg(&ts->client->dev, "Validating page @%#06x (%d)\n", |
| 939 | offset, length); |
| 940 | |
| 941 | /* Addr */ |
| 942 | cmd[0] = MIP4_R0_BOOT; |
| 943 | cmd[1] = MIP4_R1_BOOT_TARGET_ADDR; |
| 944 | put_unaligned_le32(offset, &cmd[2]); |
| 945 | ret = i2c_master_send(ts->client, cmd, 6); |
| 946 | if (ret != 6) { |
| 947 | error = ret < 0 ? ret : -EIO; |
| 948 | dev_err(&ts->client->dev, |
| 949 | "Failed to send read page address: %d\n", error); |
| 950 | return error; |
| 951 | } |
| 952 | |
| 953 | /* Size */ |
| 954 | cmd[0] = MIP4_R0_BOOT; |
| 955 | cmd[1] = MIP4_R1_BOOT_SIZE; |
| 956 | put_unaligned_le32(length, &cmd[2]); |
| 957 | ret = i2c_master_send(ts->client, cmd, 6); |
| 958 | if (ret != 6) { |
| 959 | error = ret < 0 ? ret : -EIO; |
| 960 | dev_err(&ts->client->dev, |
| 961 | "Failed to send read page size: %d\n", error); |
| 962 | return error; |
| 963 | } |
| 964 | |
| 965 | /* Command */ |
| 966 | cmd[0] = MIP4_R0_BOOT; |
| 967 | cmd[1] = MIP4_R1_BOOT_CMD; |
| 968 | cmd[2] = MIP4_BOOT_CMD_READ; |
| 969 | ret = i2c_master_send(ts->client, cmd, 3); |
| 970 | if (ret != 3) { |
| 971 | error = ret < 0 ? ret : -EIO; |
| 972 | dev_err(&ts->client->dev, |
| 973 | "Failed to send 'read' command: %d\n", error); |
| 974 | return error; |
| 975 | } |
| 976 | |
| 977 | /* Status */ |
| 978 | error = mip4_bl_read_status(ts); |
| 979 | if (error) |
| 980 | return error; |
| 981 | |
| 982 | /* Read */ |
| 983 | msg[1].buf = read_buf = kmalloc(MIP4_BL_PACKET_SIZE, GFP_KERNEL); |
| 984 | if (!read_buf) |
| 985 | return -ENOMEM; |
| 986 | |
| 987 | for (buf_offset = 0; |
| 988 | buf_offset < length; |
| 989 | buf_offset += MIP4_BL_PACKET_SIZE) { |
| 990 | dev_dbg(&ts->client->dev, |
| 991 | "reading chunk at %#04x (size %d)\n", |
| 992 | buf_offset, MIP4_BL_PACKET_SIZE); |
| 993 | put_unaligned_be16(buf_addr + buf_offset, cmd); |
| 994 | ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg)); |
| 995 | if (ret != ARRAY_SIZE(msg)) { |
| 996 | error = ret < 0 ? ret : -EIO; |
| 997 | dev_err(&ts->client->dev, |
| 998 | "Failed to read chunk at %#04x (size %d): %d\n", |
| 999 | buf_offset, MIP4_BL_PACKET_SIZE, error); |
| 1000 | break; |
| 1001 | } |
| 1002 | |
| 1003 | if (memcmp(&data[buf_offset], read_buf, MIP4_BL_PACKET_SIZE)) { |
| 1004 | dev_err(&ts->client->dev, |
| 1005 | "Failed to validate chunk at %#04x (size %d)\n", |
| 1006 | buf_offset, MIP4_BL_PACKET_SIZE); |
| 1007 | #if MIP4_FW_UPDATE_DEBUG |
| 1008 | print_hex_dump(KERN_DEBUG, |
| 1009 | MIP4_DEVICE_NAME " F/W File: ", |
| 1010 | DUMP_PREFIX_OFFSET, 16, 1, |
| 1011 | data + offset, MIP4_BL_PACKET_SIZE, |
| 1012 | false); |
| 1013 | print_hex_dump(KERN_DEBUG, |
| 1014 | MIP4_DEVICE_NAME " F/W Chip: ", |
| 1015 | DUMP_PREFIX_OFFSET, 16, 1, |
| 1016 | read_buf, MIP4_BL_PAGE_SIZE, false); |
| 1017 | #endif |
| 1018 | error = -EINVAL; |
| 1019 | break; |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | kfree(read_buf); |
| 1024 | return error ? error : 0; |
| 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * Flash chip firmware |
| 1029 | */ |
| 1030 | static int mip4_flash_fw(struct mip4_ts *ts, |
| 1031 | const u8 *fw_data, u32 fw_size, u32 fw_offset) |
| 1032 | { |
| 1033 | struct i2c_client *client = ts->client; |
| 1034 | int offset; |
| 1035 | u16 buf_addr; |
| 1036 | int error, error2; |
| 1037 | |
| 1038 | /* Enter bootloader mode */ |
| 1039 | dev_dbg(&client->dev, "Entering bootloader mode\n"); |
| 1040 | |
| 1041 | error = mip4_bl_enter(ts); |
| 1042 | if (error) { |
| 1043 | dev_err(&client->dev, |
| 1044 | "Failed to enter bootloader mode: %d\n", |
| 1045 | error); |
| 1046 | return error; |
| 1047 | } |
| 1048 | |
| 1049 | /* Read info */ |
| 1050 | error = mip4_bl_get_address(ts, &buf_addr); |
| 1051 | if (error) |
| 1052 | goto exit_bl; |
| 1053 | |
| 1054 | /* Program & Verify */ |
| 1055 | dev_dbg(&client->dev, |
| 1056 | "Program & Verify, page size: %d, packet size: %d\n", |
| 1057 | MIP4_BL_PAGE_SIZE, MIP4_BL_PACKET_SIZE); |
| 1058 | |
| 1059 | for (offset = fw_offset; |
| 1060 | offset < fw_offset + fw_size; |
| 1061 | offset += MIP4_BL_PAGE_SIZE) { |
| 1062 | /* Program */ |
| 1063 | error = mip4_bl_program_page(ts, offset, fw_data + offset, |
| 1064 | MIP4_BL_PAGE_SIZE, buf_addr); |
| 1065 | if (error) |
| 1066 | break; |
| 1067 | |
| 1068 | /* Verify */ |
| 1069 | error = mip4_bl_verify_page(ts, offset, fw_data + offset, |
| 1070 | MIP4_BL_PAGE_SIZE, buf_addr); |
| 1071 | if (error) |
| 1072 | break; |
| 1073 | } |
| 1074 | |
| 1075 | exit_bl: |
| 1076 | /* Exit bootloader mode */ |
| 1077 | dev_dbg(&client->dev, "Exiting bootloader mode\n"); |
| 1078 | |
| 1079 | error2 = mip4_bl_exit(ts); |
| 1080 | if (error2) { |
| 1081 | dev_err(&client->dev, |
| 1082 | "Failed to exit bootloader mode: %d\n", error2); |
| 1083 | if (!error) |
| 1084 | error = error2; |
| 1085 | } |
| 1086 | |
| 1087 | /* Reset chip */ |
| 1088 | mip4_power_off(ts); |
| 1089 | mip4_power_on(ts); |
| 1090 | |
| 1091 | mip4_query_device(ts); |
| 1092 | |
| 1093 | /* Refresh device parameters */ |
| 1094 | input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0); |
| 1095 | input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0); |
| 1096 | input_set_abs_params(ts->input, ABS_X, 0, ts->max_x, 0, 0); |
| 1097 | input_set_abs_params(ts->input, ABS_Y, 0, ts->max_y, 0, 0); |
| 1098 | |
| 1099 | return error ? error : 0; |
| 1100 | } |
| 1101 | |
| 1102 | static int mip4_parse_firmware(struct mip4_ts *ts, const struct firmware *fw, |
| 1103 | u32 *fw_offset_start, u32 *fw_size, |
| 1104 | const struct mip4_bin_tail **pfw_info) |
| 1105 | { |
| 1106 | const struct mip4_bin_tail *fw_info; |
| 1107 | struct mip4_fw_version fw_version; |
| 1108 | u16 tail_size; |
| 1109 | |
| 1110 | if (fw->size < MIP4_BIN_TAIL_SIZE) { |
| 1111 | dev_err(&ts->client->dev, |
| 1112 | "Invalid firmware, size mismatch (tail %zd vs %zd)\n", |
| 1113 | MIP4_BIN_TAIL_SIZE, fw->size); |
| 1114 | return -EINVAL; |
| 1115 | } |
| 1116 | |
| 1117 | fw_info = (const void *)&fw->data[fw->size - MIP4_BIN_TAIL_SIZE]; |
| 1118 | |
| 1119 | #if MIP4_FW_UPDATE_DEBUG |
| 1120 | print_hex_dump(KERN_ERR, MIP4_DEVICE_NAME " Bin Info: ", |
| 1121 | DUMP_PREFIX_OFFSET, 16, 1, *fw_info, tail_size, false); |
| 1122 | #endif |
| 1123 | |
| 1124 | tail_size = get_unaligned_le16(&fw_info->tail_size); |
| 1125 | if (tail_size != MIP4_BIN_TAIL_SIZE) { |
| 1126 | dev_err(&ts->client->dev, |
| 1127 | "wrong tail size: %d (expected %zd)\n", |
| 1128 | tail_size, MIP4_BIN_TAIL_SIZE); |
| 1129 | return -EINVAL; |
| 1130 | } |
| 1131 | |
| 1132 | /* Check bin format */ |
| 1133 | if (memcmp(fw_info->tail_mark, MIP4_BIN_TAIL_MARK, |
| 1134 | sizeof(fw_info->tail_mark))) { |
| 1135 | dev_err(&ts->client->dev, |
| 1136 | "unable to locate tail marker (%*ph vs %*ph)\n", |
| 1137 | (int)sizeof(fw_info->tail_mark), fw_info->tail_mark, |
| 1138 | (int)sizeof(fw_info->tail_mark), MIP4_BIN_TAIL_MARK); |
| 1139 | return -EINVAL; |
| 1140 | } |
| 1141 | |
| 1142 | *fw_offset_start = get_unaligned_le32(&fw_info->bin_start_addr); |
| 1143 | *fw_size = get_unaligned_le32(&fw_info->bin_length); |
| 1144 | |
| 1145 | dev_dbg(&ts->client->dev, |
| 1146 | "F/W Data offset: %#08x, size: %d\n", |
| 1147 | *fw_offset_start, *fw_size); |
| 1148 | |
| 1149 | if (*fw_size % MIP4_BL_PAGE_SIZE) { |
| 1150 | dev_err(&ts->client->dev, |
| 1151 | "encoded fw length %d is not multiple of pages (%d)\n", |
| 1152 | *fw_size, MIP4_BL_PAGE_SIZE); |
| 1153 | return -EINVAL; |
| 1154 | } |
| 1155 | |
| 1156 | if (fw->size != *fw_offset_start + *fw_size) { |
| 1157 | dev_err(&ts->client->dev, |
| 1158 | "Wrong firmware size, expected %d bytes, got %zd\n", |
| 1159 | *fw_offset_start + *fw_size, fw->size); |
| 1160 | return -EINVAL; |
| 1161 | } |
| 1162 | |
| 1163 | mip4_parse_fw_version((const u8 *)&fw_info->ver_boot, &fw_version); |
| 1164 | |
| 1165 | dev_dbg(&ts->client->dev, |
| 1166 | "F/W file version %04X %04X %04X %04X\n", |
| 1167 | fw_version.boot, fw_version.core, |
| 1168 | fw_version.app, fw_version.param); |
| 1169 | |
| 1170 | dev_dbg(&ts->client->dev, "F/W chip version: %04X %04X %04X %04X\n", |
| 1171 | ts->fw_version.boot, ts->fw_version.core, |
| 1172 | ts->fw_version.app, ts->fw_version.param); |
| 1173 | |
| 1174 | /* Check F/W type */ |
| 1175 | if (fw_version.boot != 0xEEEE && fw_version.boot != 0xFFFF && |
| 1176 | fw_version.core == 0xEEEE && |
| 1177 | fw_version.app == 0xEEEE && |
| 1178 | fw_version.param == 0xEEEE) { |
| 1179 | dev_dbg(&ts->client->dev, "F/W type: Bootloader\n"); |
| 1180 | } else if (fw_version.boot == 0xEEEE && |
| 1181 | fw_version.core != 0xEEEE && fw_version.core != 0xFFFF && |
| 1182 | fw_version.app != 0xEEEE && fw_version.app != 0xFFFF && |
| 1183 | fw_version.param != 0xEEEE && fw_version.param != 0xFFFF) { |
| 1184 | dev_dbg(&ts->client->dev, "F/W type: Main\n"); |
| 1185 | } else { |
| 1186 | dev_err(&ts->client->dev, "Wrong firmware type\n"); |
| 1187 | return -EINVAL; |
| 1188 | } |
| 1189 | |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | static int mip4_execute_fw_update(struct mip4_ts *ts, const struct firmware *fw) |
| 1194 | { |
| 1195 | const struct mip4_bin_tail *fw_info; |
| 1196 | u32 fw_start_offset; |
| 1197 | u32 fw_size; |
| 1198 | int retires = 3; |
| 1199 | int error; |
| 1200 | |
| 1201 | error = mip4_parse_firmware(ts, fw, |
| 1202 | &fw_start_offset, &fw_size, &fw_info); |
| 1203 | if (error) |
| 1204 | return error; |
| 1205 | |
| 1206 | if (ts->input->users) { |
| 1207 | disable_irq(ts->client->irq); |
| 1208 | } else { |
| 1209 | error = mip4_power_on(ts); |
| 1210 | if (error) |
| 1211 | return error; |
| 1212 | } |
| 1213 | |
| 1214 | /* Update firmware */ |
| 1215 | do { |
| 1216 | error = mip4_flash_fw(ts, fw->data, fw_size, fw_start_offset); |
| 1217 | if (!error) |
| 1218 | break; |
| 1219 | } while (--retires); |
| 1220 | |
| 1221 | if (error) |
| 1222 | dev_err(&ts->client->dev, |
| 1223 | "Failed to flash firmware: %d\n", error); |
| 1224 | |
| 1225 | /* Enable IRQ */ |
| 1226 | if (ts->input->users) |
| 1227 | enable_irq(ts->client->irq); |
| 1228 | else |
| 1229 | mip4_power_off(ts); |
| 1230 | |
| 1231 | return error ? error : 0; |
| 1232 | } |
| 1233 | |
| 1234 | static ssize_t mip4_sysfs_fw_update(struct device *dev, |
| 1235 | struct device_attribute *attr, |
| 1236 | const char *buf, size_t count) |
| 1237 | { |
| 1238 | struct i2c_client *client = to_i2c_client(dev); |
| 1239 | struct mip4_ts *ts = i2c_get_clientdata(client); |
| 1240 | const struct firmware *fw; |
| 1241 | int error; |
| 1242 | |
| 1243 | error = request_firmware(&fw, MIP4_FW_NAME, dev); |
| 1244 | if (error) { |
| 1245 | dev_err(&ts->client->dev, |
| 1246 | "Failed to retrieve firmware %s: %d\n", |
| 1247 | MIP4_FW_NAME, error); |
| 1248 | return error; |
| 1249 | } |
| 1250 | |
| 1251 | /* |
| 1252 | * Take input mutex to prevent racing with itself and also with |
| 1253 | * userspace opening and closing the device and also suspend/resume |
| 1254 | * transitions. |
| 1255 | */ |
| 1256 | mutex_lock(&ts->input->mutex); |
| 1257 | |
| 1258 | error = mip4_execute_fw_update(ts, fw); |
| 1259 | |
| 1260 | mutex_unlock(&ts->input->mutex); |
| 1261 | |
| 1262 | release_firmware(fw); |
| 1263 | |
| 1264 | if (error) { |
| 1265 | dev_err(&ts->client->dev, |
| 1266 | "Firmware update failed: %d\n", error); |
| 1267 | return error; |
| 1268 | } |
| 1269 | |
| 1270 | return count; |
| 1271 | } |
| 1272 | |
| 1273 | static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mip4_sysfs_fw_update); |
| 1274 | |
| 1275 | static ssize_t mip4_sysfs_read_fw_version(struct device *dev, |
| 1276 | struct device_attribute *attr, |
| 1277 | char *buf) |
| 1278 | { |
| 1279 | struct i2c_client *client = to_i2c_client(dev); |
| 1280 | struct mip4_ts *ts = i2c_get_clientdata(client); |
| 1281 | size_t count; |
| 1282 | |
| 1283 | /* Take lock to prevent racing with firmware update */ |
| 1284 | mutex_lock(&ts->input->mutex); |
| 1285 | |
| 1286 | count = snprintf(buf, PAGE_SIZE, "%04X %04X %04X %04X\n", |
| 1287 | ts->fw_version.boot, ts->fw_version.core, |
| 1288 | ts->fw_version.app, ts->fw_version.param); |
| 1289 | |
| 1290 | mutex_unlock(&ts->input->mutex); |
| 1291 | |
| 1292 | return count; |
| 1293 | } |
| 1294 | |
| 1295 | static DEVICE_ATTR(fw_version, S_IRUGO, mip4_sysfs_read_fw_version, NULL); |
| 1296 | |
| 1297 | static struct attribute *mip4_attrs[] = { |
| 1298 | &dev_attr_fw_version.attr, |
| 1299 | &dev_attr_update_fw.attr, |
| 1300 | NULL, |
| 1301 | }; |
| 1302 | |
| 1303 | static const struct attribute_group mip4_attr_group = { |
| 1304 | .attrs = mip4_attrs, |
| 1305 | }; |
| 1306 | |
| 1307 | static void mip4_sysfs_remove(void *_data) |
| 1308 | { |
| 1309 | struct mip4_ts *ts = _data; |
| 1310 | |
| 1311 | sysfs_remove_group(&ts->client->dev.kobj, &mip4_attr_group); |
| 1312 | } |
| 1313 | |
| 1314 | static int mip4_probe(struct i2c_client *client, const struct i2c_device_id *id) |
| 1315 | { |
| 1316 | struct mip4_ts *ts; |
| 1317 | struct input_dev *input; |
| 1318 | int error; |
| 1319 | |
| 1320 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 1321 | dev_err(&client->dev, "Not supported I2C adapter\n"); |
| 1322 | return -ENXIO; |
| 1323 | } |
| 1324 | |
| 1325 | ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL); |
| 1326 | if (!ts) |
| 1327 | return -ENOMEM; |
| 1328 | |
| 1329 | input = devm_input_allocate_device(&client->dev); |
| 1330 | if (!input) |
| 1331 | return -ENOMEM; |
| 1332 | |
| 1333 | ts->client = client; |
| 1334 | ts->input = input; |
| 1335 | |
| 1336 | snprintf(ts->phys, sizeof(ts->phys), |
| 1337 | "%s/input0", dev_name(&client->dev)); |
| 1338 | |
| 1339 | ts->gpio_ce = devm_gpiod_get_optional(&client->dev, |
| 1340 | "ce", GPIOD_OUT_LOW); |
| 1341 | if (IS_ERR(ts->gpio_ce)) { |
| 1342 | error = PTR_ERR(ts->gpio_ce); |
| 1343 | if (error != EPROBE_DEFER) |
| 1344 | dev_err(&client->dev, |
| 1345 | "Failed to get gpio: %d\n", error); |
| 1346 | return error; |
| 1347 | } |
| 1348 | |
| 1349 | error = mip4_power_on(ts); |
| 1350 | if (error) |
| 1351 | return error; |
| 1352 | error = mip4_query_device(ts); |
| 1353 | mip4_power_off(ts); |
| 1354 | if (error) |
| 1355 | return error; |
| 1356 | |
| 1357 | input->name = "MELFAS MIP4 Touchscreen"; |
| 1358 | input->phys = ts->phys; |
| 1359 | |
| 1360 | input->id.bustype = BUS_I2C; |
| 1361 | input->id.vendor = 0x13c5; |
| 1362 | |
| 1363 | input->open = mip4_input_open; |
| 1364 | input->close = mip4_input_close; |
| 1365 | |
| 1366 | input_set_drvdata(input, ts); |
| 1367 | |
| 1368 | input->keycode = ts->key_code; |
| 1369 | input->keycodesize = sizeof(*ts->key_code); |
| 1370 | input->keycodemax = ts->key_num; |
| 1371 | |
| 1372 | input_set_abs_params(input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0); |
| 1373 | input_set_abs_params(input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0); |
| 1374 | input_set_abs_params(input, ABS_MT_PRESSURE, |
| 1375 | MIP4_PRESSURE_MIN, MIP4_PRESSURE_MAX, 0, 0); |
| 1376 | input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, |
| 1377 | MIP4_TOUCH_MAJOR_MIN, MIP4_TOUCH_MAJOR_MAX, 0, 0); |
| 1378 | input_set_abs_params(input, ABS_MT_TOUCH_MINOR, |
| 1379 | MIP4_TOUCH_MINOR_MIN, MIP4_TOUCH_MINOR_MAX, 0, 0); |
| 1380 | |
| 1381 | error = input_mt_init_slots(input, MIP4_MAX_FINGERS, INPUT_MT_DIRECT); |
| 1382 | if (error) |
| 1383 | return error; |
| 1384 | |
| 1385 | i2c_set_clientdata(client, ts); |
| 1386 | |
| 1387 | error = devm_request_threaded_irq(&client->dev, client->irq, |
| 1388 | NULL, mip4_interrupt, |
| 1389 | IRQF_ONESHOT, MIP4_DEVICE_NAME, ts); |
| 1390 | if (error) { |
| 1391 | dev_err(&client->dev, |
| 1392 | "Failed to request interrupt %d: %d\n", |
| 1393 | client->irq, error); |
| 1394 | return error; |
| 1395 | } |
| 1396 | |
| 1397 | disable_irq(client->irq); |
| 1398 | |
| 1399 | error = input_register_device(input); |
| 1400 | if (error) { |
| 1401 | dev_err(&client->dev, |
| 1402 | "Failed to register input device: %d\n", error); |
| 1403 | return error; |
| 1404 | } |
| 1405 | |
| 1406 | error = sysfs_create_group(&client->dev.kobj, &mip4_attr_group); |
| 1407 | if (error) { |
| 1408 | dev_err(&client->dev, |
| 1409 | "Failed to create sysfs attribute group: %d\n", error); |
| 1410 | return error; |
| 1411 | } |
| 1412 | |
| 1413 | error = devm_add_action(&client->dev, mip4_sysfs_remove, ts); |
| 1414 | if (error) { |
| 1415 | mip4_sysfs_remove(ts); |
| 1416 | dev_err(&client->dev, |
| 1417 | "Failed to install sysfs remoce action: %d\n", error); |
| 1418 | return error; |
| 1419 | } |
| 1420 | |
| 1421 | return 0; |
| 1422 | } |
| 1423 | |
| 1424 | static int __maybe_unused mip4_suspend(struct device *dev) |
| 1425 | { |
| 1426 | struct i2c_client *client = to_i2c_client(dev); |
| 1427 | struct mip4_ts *ts = i2c_get_clientdata(client); |
| 1428 | struct input_dev *input = ts->input; |
| 1429 | |
| 1430 | mutex_lock(&input->mutex); |
| 1431 | |
| 1432 | if (device_may_wakeup(dev)) |
| 1433 | ts->wake_irq_enabled = enable_irq_wake(client->irq) == 0; |
| 1434 | else if (input->users) |
| 1435 | mip4_disable(ts); |
| 1436 | |
| 1437 | mutex_unlock(&input->mutex); |
| 1438 | |
| 1439 | return 0; |
| 1440 | } |
| 1441 | |
| 1442 | static int __maybe_unused mip4_resume(struct device *dev) |
| 1443 | { |
| 1444 | struct i2c_client *client = to_i2c_client(dev); |
| 1445 | struct mip4_ts *ts = i2c_get_clientdata(client); |
| 1446 | struct input_dev *input = ts->input; |
| 1447 | |
| 1448 | mutex_lock(&input->mutex); |
| 1449 | |
| 1450 | if (ts->wake_irq_enabled) |
| 1451 | disable_irq_wake(client->irq); |
| 1452 | else if (input->users) |
| 1453 | mip4_enable(ts); |
| 1454 | |
| 1455 | mutex_unlock(&input->mutex); |
| 1456 | |
| 1457 | return 0; |
| 1458 | } |
| 1459 | |
| 1460 | static SIMPLE_DEV_PM_OPS(mip4_pm_ops, mip4_suspend, mip4_resume); |
| 1461 | |
| 1462 | #ifdef CONFIG_OF |
| 1463 | static const struct of_device_id mip4_of_match[] = { |
| 1464 | { .compatible = "melfas,"MIP4_DEVICE_NAME, }, |
| 1465 | { }, |
| 1466 | }; |
| 1467 | MODULE_DEVICE_TABLE(of, mip4_of_match); |
| 1468 | #endif |
| 1469 | |
| 1470 | #ifdef CONFIG_ACPI |
| 1471 | static const struct acpi_device_id mip4_acpi_match[] = { |
| 1472 | { "MLFS0000", 0}, |
| 1473 | { }, |
| 1474 | }; |
| 1475 | MODULE_DEVICE_TABLE(acpi, mip4_acpi_match); |
| 1476 | #endif |
| 1477 | |
| 1478 | static const struct i2c_device_id mip4_i2c_ids[] = { |
| 1479 | { MIP4_DEVICE_NAME, 0 }, |
| 1480 | { }, |
| 1481 | }; |
| 1482 | MODULE_DEVICE_TABLE(i2c, mip4_i2c_ids); |
| 1483 | |
| 1484 | static struct i2c_driver mip4_driver = { |
| 1485 | .id_table = mip4_i2c_ids, |
| 1486 | .probe = mip4_probe, |
| 1487 | .driver = { |
| 1488 | .name = MIP4_DEVICE_NAME, |
| 1489 | .of_match_table = of_match_ptr(mip4_of_match), |
| 1490 | .acpi_match_table = ACPI_PTR(mip4_acpi_match), |
| 1491 | .pm = &mip4_pm_ops, |
| 1492 | }, |
| 1493 | }; |
| 1494 | module_i2c_driver(mip4_driver); |
| 1495 | |
| 1496 | MODULE_DESCRIPTION("MELFAS MIP4 Touchscreen"); |
| 1497 | MODULE_VERSION("2016.01.13"); |
| 1498 | MODULE_AUTHOR("Sangwon Jee <jeesw@melfas.com>"); |
| 1499 | MODULE_LICENSE("GPL"); |