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