Duson Lin | 6696777 | 2014-10-03 13:24:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Elan I2C/SMBus Touchpad driver - SMBus interface |
| 3 | * |
| 4 | * Copyright (c) 2013 ELAN Microelectronics Corp. |
| 5 | * |
| 6 | * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw> |
| 7 | * Version: 1.5.5 |
| 8 | * |
| 9 | * Based on cyapa driver: |
| 10 | * copyright (c) 2011-2012 Cypress Semiconductor, Inc. |
| 11 | * copyright (c) 2011-2012 Google, Inc. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify it |
| 14 | * under the terms of the GNU General Public License version 2 as published |
| 15 | * by the Free Software Foundation. |
| 16 | * |
| 17 | * Trademarks are the property of their respective owners. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/i2c.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/kernel.h> |
| 24 | |
| 25 | #include "elan_i2c.h" |
| 26 | |
| 27 | /* Elan SMbus commands */ |
| 28 | #define ETP_SMBUS_IAP_CMD 0x00 |
| 29 | #define ETP_SMBUS_ENABLE_TP 0x20 |
| 30 | #define ETP_SMBUS_SLEEP_CMD 0x21 |
| 31 | #define ETP_SMBUS_IAP_PASSWORD_WRITE 0x29 |
| 32 | #define ETP_SMBUS_IAP_PASSWORD_READ 0x80 |
| 33 | #define ETP_SMBUS_WRITE_FW_BLOCK 0x2A |
| 34 | #define ETP_SMBUS_IAP_RESET_CMD 0x2B |
| 35 | #define ETP_SMBUS_RANGE_CMD 0xA0 |
| 36 | #define ETP_SMBUS_FW_VERSION_CMD 0xA1 |
| 37 | #define ETP_SMBUS_XY_TRACENUM_CMD 0xA2 |
| 38 | #define ETP_SMBUS_SM_VERSION_CMD 0xA3 |
| 39 | #define ETP_SMBUS_UNIQUEID_CMD 0xA3 |
| 40 | #define ETP_SMBUS_RESOLUTION_CMD 0xA4 |
| 41 | #define ETP_SMBUS_HELLOPACKET_CMD 0xA7 |
| 42 | #define ETP_SMBUS_PACKET_QUERY 0xA8 |
| 43 | #define ETP_SMBUS_IAP_VERSION_CMD 0xAC |
| 44 | #define ETP_SMBUS_IAP_CTRL_CMD 0xAD |
| 45 | #define ETP_SMBUS_IAP_CHECKSUM_CMD 0xAE |
| 46 | #define ETP_SMBUS_FW_CHECKSUM_CMD 0xAF |
| 47 | #define ETP_SMBUS_MAX_BASELINE_CMD 0xC3 |
| 48 | #define ETP_SMBUS_MIN_BASELINE_CMD 0xC4 |
| 49 | #define ETP_SMBUS_CALIBRATE_QUERY 0xC5 |
| 50 | |
| 51 | #define ETP_SMBUS_REPORT_LEN 32 |
| 52 | #define ETP_SMBUS_REPORT_OFFSET 2 |
| 53 | #define ETP_SMBUS_HELLOPACKET_LEN 5 |
| 54 | #define ETP_SMBUS_IAP_PASSWORD 0x1234 |
| 55 | #define ETP_SMBUS_IAP_MODE_ON (1 << 6) |
| 56 | |
| 57 | static int elan_smbus_initialize(struct i2c_client *client) |
| 58 | { |
| 59 | u8 check[ETP_SMBUS_HELLOPACKET_LEN] = { 0x55, 0x55, 0x55, 0x55, 0x55 }; |
| 60 | u8 values[ETP_SMBUS_HELLOPACKET_LEN] = { 0, 0, 0, 0, 0 }; |
| 61 | int len, error; |
| 62 | |
| 63 | /* Get hello packet */ |
| 64 | len = i2c_smbus_read_block_data(client, |
| 65 | ETP_SMBUS_HELLOPACKET_CMD, values); |
| 66 | if (len != ETP_SMBUS_HELLOPACKET_LEN) { |
| 67 | dev_err(&client->dev, "hello packet length fail: %d\n", len); |
| 68 | error = len < 0 ? len : -EIO; |
| 69 | return error; |
| 70 | } |
| 71 | |
| 72 | /* compare hello packet */ |
| 73 | if (memcmp(values, check, ETP_SMBUS_HELLOPACKET_LEN)) { |
| 74 | dev_err(&client->dev, "hello packet fail [%*px]\n", |
| 75 | ETP_SMBUS_HELLOPACKET_LEN, values); |
| 76 | return -ENXIO; |
| 77 | } |
| 78 | |
| 79 | /* enable tp */ |
| 80 | error = i2c_smbus_write_byte(client, ETP_SMBUS_ENABLE_TP); |
| 81 | if (error) { |
| 82 | dev_err(&client->dev, "failed to enable touchpad: %d\n", error); |
| 83 | return error; |
| 84 | } |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int elan_smbus_set_mode(struct i2c_client *client, u8 mode) |
| 90 | { |
| 91 | u8 cmd[4] = { 0x00, 0x07, 0x00, mode }; |
| 92 | |
| 93 | return i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD, |
| 94 | sizeof(cmd), cmd); |
| 95 | } |
| 96 | |
| 97 | static int elan_smbus_sleep_control(struct i2c_client *client, bool sleep) |
| 98 | { |
| 99 | if (sleep) |
| 100 | return i2c_smbus_write_byte(client, ETP_SMBUS_SLEEP_CMD); |
| 101 | else |
| 102 | return 0; /* XXX should we send ETP_SMBUS_ENABLE_TP here? */ |
| 103 | } |
| 104 | |
| 105 | static int elan_smbus_power_control(struct i2c_client *client, bool enable) |
| 106 | { |
| 107 | return 0; /* A no-op */ |
| 108 | } |
| 109 | |
| 110 | static int elan_smbus_calibrate(struct i2c_client *client) |
| 111 | { |
| 112 | u8 cmd[4] = { 0x00, 0x08, 0x00, 0x01 }; |
| 113 | |
| 114 | return i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD, |
| 115 | sizeof(cmd), cmd); |
| 116 | } |
| 117 | |
| 118 | static int elan_smbus_calibrate_result(struct i2c_client *client, u8 *val) |
| 119 | { |
| 120 | int error; |
| 121 | |
| 122 | error = i2c_smbus_read_block_data(client, |
| 123 | ETP_SMBUS_CALIBRATE_QUERY, val); |
| 124 | if (error < 0) |
| 125 | return error; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static int elan_smbus_get_baseline_data(struct i2c_client *client, |
| 131 | bool max_baseline, u8 *value) |
| 132 | { |
| 133 | int error; |
| 134 | u8 val[3]; |
| 135 | |
| 136 | error = i2c_smbus_read_block_data(client, |
| 137 | max_baseline ? |
| 138 | ETP_SMBUS_MAX_BASELINE_CMD : |
| 139 | ETP_SMBUS_MIN_BASELINE_CMD, |
| 140 | val); |
| 141 | if (error < 0) |
| 142 | return error; |
| 143 | |
| 144 | *value = be16_to_cpup((__be16 *)val); |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static int elan_smbus_get_version(struct i2c_client *client, |
| 150 | bool iap, u8 *version) |
| 151 | { |
| 152 | int error; |
| 153 | u8 val[3]; |
| 154 | |
| 155 | error = i2c_smbus_read_block_data(client, |
| 156 | iap ? ETP_SMBUS_IAP_VERSION_CMD : |
| 157 | ETP_SMBUS_FW_VERSION_CMD, |
| 158 | val); |
| 159 | if (error < 0) { |
| 160 | dev_err(&client->dev, "failed to get %s version: %d\n", |
| 161 | iap ? "IAP" : "FW", error); |
| 162 | return error; |
| 163 | } |
| 164 | |
| 165 | *version = val[2]; |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static int elan_smbus_get_sm_version(struct i2c_client *client, u8 *version) |
| 170 | { |
| 171 | int error; |
| 172 | u8 val[3]; |
| 173 | |
| 174 | error = i2c_smbus_read_block_data(client, |
| 175 | ETP_SMBUS_SM_VERSION_CMD, val); |
| 176 | if (error < 0) { |
| 177 | dev_err(&client->dev, "failed to get SM version: %d\n", error); |
| 178 | return error; |
| 179 | } |
| 180 | |
| 181 | *version = val[0]; /* XXX Why 0 and not 2 as in IAP/FW versions? */ |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int elan_smbus_get_product_id(struct i2c_client *client, u8 *id) |
| 186 | { |
| 187 | int error; |
| 188 | u8 val[3]; |
| 189 | |
| 190 | error = i2c_smbus_read_block_data(client, |
| 191 | ETP_SMBUS_UNIQUEID_CMD, val); |
| 192 | if (error < 0) { |
| 193 | dev_err(&client->dev, "failed to get product ID: %d\n", error); |
| 194 | return error; |
| 195 | } |
| 196 | |
| 197 | *id = val[1]; |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static int elan_smbus_get_checksum(struct i2c_client *client, |
| 202 | bool iap, u16 *csum) |
| 203 | { |
| 204 | int error; |
| 205 | u8 val[3]; |
| 206 | |
| 207 | error = i2c_smbus_read_block_data(client, |
| 208 | iap ? ETP_SMBUS_FW_CHECKSUM_CMD : |
| 209 | ETP_SMBUS_IAP_CHECKSUM_CMD, |
| 210 | val); |
| 211 | if (error < 0) { |
| 212 | dev_err(&client->dev, "failed to get %s checksum: %d\n", |
| 213 | iap ? "IAP" : "FW", error); |
| 214 | return error; |
| 215 | } |
| 216 | |
| 217 | *csum = be16_to_cpup((__be16 *)val); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int elan_smbus_get_max(struct i2c_client *client, |
| 222 | unsigned int *max_x, unsigned int *max_y) |
| 223 | { |
| 224 | int error; |
| 225 | u8 val[3]; |
| 226 | |
| 227 | error = i2c_smbus_read_block_data(client, ETP_SMBUS_RANGE_CMD, val); |
| 228 | if (error) { |
| 229 | dev_err(&client->dev, "failed to get dimensions: %d\n", error); |
| 230 | return error; |
| 231 | } |
| 232 | |
| 233 | *max_x = (0x0f & val[0]) << 8 | val[1]; |
| 234 | *max_y = (0xf0 & val[0]) << 4 | val[2]; |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static int elan_smbus_get_resolution(struct i2c_client *client, |
| 240 | u8 *hw_res_x, u8 *hw_res_y) |
| 241 | { |
| 242 | int error; |
| 243 | u8 val[3]; |
| 244 | |
| 245 | error = i2c_smbus_read_block_data(client, |
| 246 | ETP_SMBUS_RESOLUTION_CMD, val); |
| 247 | if (error) { |
| 248 | dev_err(&client->dev, "failed to get resolution: %d\n", error); |
| 249 | return error; |
| 250 | } |
| 251 | |
| 252 | *hw_res_x = val[1] & 0x0F; |
| 253 | *hw_res_y = (val[1] & 0xF0) >> 4; |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static int elan_smbus_get_num_traces(struct i2c_client *client, |
| 259 | unsigned int *x_traces, |
| 260 | unsigned int *y_traces) |
| 261 | { |
| 262 | int error; |
| 263 | u8 val[3]; |
| 264 | |
| 265 | error = i2c_smbus_read_block_data(client, |
| 266 | ETP_SMBUS_XY_TRACENUM_CMD, val); |
| 267 | if (error) { |
| 268 | dev_err(&client->dev, "failed to get trace info: %d\n", error); |
| 269 | return error; |
| 270 | } |
| 271 | |
| 272 | *x_traces = val[1] - 1; |
| 273 | *y_traces = val[2] - 1; |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static int elan_smbus_iap_get_mode(struct i2c_client *client, |
| 279 | enum tp_mode *mode) |
| 280 | { |
| 281 | int error; |
| 282 | u16 constant; |
| 283 | u8 val[3]; |
| 284 | |
| 285 | error = i2c_smbus_read_block_data(client, ETP_SMBUS_IAP_CTRL_CMD, val); |
| 286 | if (error < 0) { |
| 287 | dev_err(&client->dev, "failed to read iap ctrol register: %d\n", |
| 288 | error); |
| 289 | return error; |
| 290 | } |
| 291 | |
| 292 | constant = be16_to_cpup((__be16 *)val); |
| 293 | dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant); |
| 294 | |
| 295 | *mode = (constant & ETP_SMBUS_IAP_MODE_ON) ? IAP_MODE : MAIN_MODE; |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static int elan_smbus_iap_reset(struct i2c_client *client) |
| 301 | { |
| 302 | int error; |
| 303 | |
| 304 | error = i2c_smbus_write_byte(client, ETP_SMBUS_IAP_RESET_CMD); |
| 305 | if (error) { |
| 306 | dev_err(&client->dev, "cannot reset IC: %d\n", error); |
| 307 | return error; |
| 308 | } |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static int elan_smbus_set_flash_key(struct i2c_client *client) |
| 314 | { |
| 315 | int error; |
| 316 | u8 cmd[4] = { 0x00, 0x0B, 0x00, 0x5A }; |
| 317 | |
| 318 | error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD, |
| 319 | sizeof(cmd), cmd); |
| 320 | if (error) { |
| 321 | dev_err(&client->dev, "cannot set flash key: %d\n", error); |
| 322 | return error; |
| 323 | } |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static int elan_smbus_prepare_fw_update(struct i2c_client *client) |
| 329 | { |
| 330 | struct device *dev = &client->dev; |
| 331 | int len; |
| 332 | int error; |
| 333 | enum tp_mode mode; |
| 334 | u8 val[3]; |
| 335 | u8 cmd[4] = {0x0F, 0x78, 0x00, 0x06}; |
| 336 | u16 password; |
| 337 | |
| 338 | /* Get FW in which mode (IAP_MODE/MAIN_MODE) */ |
| 339 | error = elan_smbus_iap_get_mode(client, &mode); |
| 340 | if (error) |
| 341 | return error; |
| 342 | |
| 343 | if (mode == MAIN_MODE) { |
| 344 | |
| 345 | /* set flash key */ |
| 346 | error = elan_smbus_set_flash_key(client); |
| 347 | if (error) |
| 348 | return error; |
| 349 | |
| 350 | /* write iap password */ |
| 351 | if (i2c_smbus_write_byte(client, |
| 352 | ETP_SMBUS_IAP_PASSWORD_WRITE) < 0) { |
| 353 | dev_err(dev, "cannot write iap password\n"); |
| 354 | return -EIO; |
| 355 | } |
| 356 | |
| 357 | error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD, |
| 358 | sizeof(cmd), cmd); |
| 359 | if (error) { |
| 360 | dev_err(dev, "failed to write iap password: %d\n", |
| 361 | error); |
| 362 | return error; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Read back password to make sure we enabled flash |
| 367 | * successfully. |
| 368 | */ |
| 369 | len = i2c_smbus_read_block_data(client, |
| 370 | ETP_SMBUS_IAP_PASSWORD_READ, |
| 371 | val); |
| 372 | if (len < sizeof(u16)) { |
| 373 | error = len < 0 ? len : -EIO; |
| 374 | dev_err(dev, "failed to read iap password: %d\n", |
| 375 | error); |
| 376 | return error; |
| 377 | } |
| 378 | |
| 379 | password = be16_to_cpup((__be16 *)val); |
| 380 | if (password != ETP_SMBUS_IAP_PASSWORD) { |
| 381 | dev_err(dev, "wrong iap password = 0x%X\n", password); |
| 382 | return -EIO; |
| 383 | } |
| 384 | |
| 385 | /* Wait 30ms for MAIN_MODE change to IAP_MODE */ |
| 386 | msleep(30); |
| 387 | } |
| 388 | |
| 389 | error = elan_smbus_set_flash_key(client); |
| 390 | if (error) |
| 391 | return error; |
| 392 | |
| 393 | /* Reset IC */ |
| 394 | error = elan_smbus_iap_reset(client); |
| 395 | if (error) |
| 396 | return error; |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | static int elan_smbus_write_fw_block(struct i2c_client *client, |
| 403 | const u8 *page, u16 checksum, int idx) |
| 404 | { |
| 405 | struct device *dev = &client->dev; |
| 406 | int error; |
| 407 | u16 result; |
| 408 | u8 val[3]; |
| 409 | |
| 410 | /* |
| 411 | * Due to the limitation of smbus protocol limiting |
| 412 | * transfer to 32 bytes at a time, we must split block |
| 413 | * in 2 transfers. |
| 414 | */ |
| 415 | error = i2c_smbus_write_block_data(client, |
| 416 | ETP_SMBUS_WRITE_FW_BLOCK, |
| 417 | ETP_FW_PAGE_SIZE / 2, |
| 418 | page); |
| 419 | if (error) { |
| 420 | dev_err(dev, "Failed to write page %d (part %d): %d\n", |
| 421 | idx, 1, error); |
| 422 | return error; |
| 423 | } |
| 424 | |
| 425 | error = i2c_smbus_write_block_data(client, |
| 426 | ETP_SMBUS_WRITE_FW_BLOCK, |
| 427 | ETP_FW_PAGE_SIZE / 2, |
| 428 | page + ETP_FW_PAGE_SIZE / 2); |
| 429 | if (error) { |
| 430 | dev_err(dev, "Failed to write page %d (part %d): %d\n", |
| 431 | idx, 2, error); |
| 432 | return error; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | /* Wait for F/W to update one page ROM data. */ |
| 437 | usleep_range(8000, 10000); |
| 438 | |
| 439 | error = i2c_smbus_read_block_data(client, |
| 440 | ETP_SMBUS_IAP_CTRL_CMD, val); |
| 441 | if (error < 0) { |
| 442 | dev_err(dev, "Failed to read IAP write result: %d\n", |
| 443 | error); |
| 444 | return error; |
| 445 | } |
| 446 | |
| 447 | result = be16_to_cpup((__be16 *)val); |
| 448 | if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) { |
| 449 | dev_err(dev, "IAP reports failed write: %04hx\n", |
| 450 | result); |
| 451 | return -EIO; |
| 452 | } |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | static int elan_smbus_get_report(struct i2c_client *client, u8 *report) |
| 458 | { |
| 459 | int len; |
| 460 | |
| 461 | len = i2c_smbus_read_block_data(client, |
| 462 | ETP_SMBUS_PACKET_QUERY, |
| 463 | &report[ETP_SMBUS_REPORT_OFFSET]); |
| 464 | if (len < 0) { |
| 465 | dev_err(&client->dev, "failed to read report data: %d\n", len); |
| 466 | return len; |
| 467 | } |
| 468 | |
| 469 | if (len != ETP_SMBUS_REPORT_LEN) { |
| 470 | dev_err(&client->dev, |
| 471 | "wrong report length (%d vs %d expected)\n", |
| 472 | len, ETP_SMBUS_REPORT_LEN); |
| 473 | return -EIO; |
| 474 | } |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | static int elan_smbus_finish_fw_update(struct i2c_client *client, |
| 480 | struct completion *fw_completion) |
| 481 | { |
| 482 | /* No special handling unlike I2C transport */ |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | const struct elan_transport_ops elan_smbus_ops = { |
| 487 | .initialize = elan_smbus_initialize, |
| 488 | .sleep_control = elan_smbus_sleep_control, |
| 489 | .power_control = elan_smbus_power_control, |
| 490 | .set_mode = elan_smbus_set_mode, |
| 491 | |
| 492 | .calibrate = elan_smbus_calibrate, |
| 493 | .calibrate_result = elan_smbus_calibrate_result, |
| 494 | |
| 495 | .get_baseline_data = elan_smbus_get_baseline_data, |
| 496 | |
| 497 | .get_version = elan_smbus_get_version, |
| 498 | .get_sm_version = elan_smbus_get_sm_version, |
| 499 | .get_product_id = elan_smbus_get_product_id, |
| 500 | .get_checksum = elan_smbus_get_checksum, |
| 501 | |
| 502 | .get_max = elan_smbus_get_max, |
| 503 | .get_resolution = elan_smbus_get_resolution, |
| 504 | .get_num_traces = elan_smbus_get_num_traces, |
| 505 | |
| 506 | .iap_get_mode = elan_smbus_iap_get_mode, |
| 507 | .iap_reset = elan_smbus_iap_reset, |
| 508 | |
| 509 | .prepare_fw_update = elan_smbus_prepare_fw_update, |
| 510 | .write_fw_block = elan_smbus_write_fw_block, |
| 511 | .finish_fw_update = elan_smbus_finish_fw_update, |
| 512 | |
| 513 | .get_report = elan_smbus_get_report, |
| 514 | }; |