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[ETP_SMBUS_HELLOPACKET_LEN] = { 0, 0, 0, 0, 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 121 error = i2c_smbus_read_block_data(client, 122 ETP_SMBUS_CALIBRATE_QUERY, val); 123 if (error < 0) 124 return error; 125 126 return 0; 127 } 128 129 static int elan_smbus_get_baseline_data(struct i2c_client *client, 130 bool max_baseline, u8 *value) 131 { 132 int error; 133 u8 val[3]; 134 135 error = i2c_smbus_read_block_data(client, 136 max_baseline ? 137 ETP_SMBUS_MAX_BASELINE_CMD : 138 ETP_SMBUS_MIN_BASELINE_CMD, 139 val); 140 if (error < 0) 141 return error; 142 143 *value = be16_to_cpup((__be16 *)val); 144 145 return 0; 146 } 147 148 static int elan_smbus_get_version(struct i2c_client *client, 149 bool iap, u8 *version) 150 { 151 int error; 152 u8 val[3]; 153 154 error = i2c_smbus_read_block_data(client, 155 iap ? ETP_SMBUS_IAP_VERSION_CMD : 156 ETP_SMBUS_FW_VERSION_CMD, 157 val); 158 if (error < 0) { 159 dev_err(&client->dev, "failed to get %s version: %d\n", 160 iap ? "IAP" : "FW", error); 161 return error; 162 } 163 164 *version = val[2]; 165 return 0; 166 } 167 168 static int elan_smbus_get_sm_version(struct i2c_client *client, 169 u16 *ic_type, u8 *version, 170 u8 *clickpad) 171 { 172 int error; 173 u8 val[3]; 174 175 error = i2c_smbus_read_block_data(client, 176 ETP_SMBUS_SM_VERSION_CMD, val); 177 if (error < 0) { 178 dev_err(&client->dev, "failed to get SM version: %d\n", error); 179 return error; 180 } 181 182 *version = val[0]; 183 *ic_type = val[1]; 184 *clickpad = val[0] & 0x10; 185 return 0; 186 } 187 188 static int elan_smbus_get_product_id(struct i2c_client *client, u16 *id) 189 { 190 int error; 191 u8 val[3]; 192 193 error = i2c_smbus_read_block_data(client, 194 ETP_SMBUS_UNIQUEID_CMD, val); 195 if (error < 0) { 196 dev_err(&client->dev, "failed to get product ID: %d\n", error); 197 return error; 198 } 199 200 *id = be16_to_cpup((__be16 *)val); 201 return 0; 202 } 203 204 static int elan_smbus_get_checksum(struct i2c_client *client, 205 bool iap, u16 *csum) 206 { 207 int error; 208 u8 val[3]; 209 210 error = i2c_smbus_read_block_data(client, 211 iap ? ETP_SMBUS_FW_CHECKSUM_CMD : 212 ETP_SMBUS_IAP_CHECKSUM_CMD, 213 val); 214 if (error < 0) { 215 dev_err(&client->dev, "failed to get %s checksum: %d\n", 216 iap ? "IAP" : "FW", error); 217 return error; 218 } 219 220 *csum = be16_to_cpup((__be16 *)val); 221 return 0; 222 } 223 224 static int elan_smbus_get_max(struct i2c_client *client, 225 unsigned int *max_x, unsigned int *max_y) 226 { 227 int ret; 228 int error; 229 u8 val[3]; 230 231 ret = i2c_smbus_read_block_data(client, ETP_SMBUS_RANGE_CMD, val); 232 if (ret != 3) { 233 error = ret < 0 ? ret : -EIO; 234 dev_err(&client->dev, "failed to get dimensions: %d\n", error); 235 return error; 236 } 237 238 *max_x = (0x0f & val[0]) << 8 | val[1]; 239 *max_y = (0xf0 & val[0]) << 4 | val[2]; 240 241 return 0; 242 } 243 244 static int elan_smbus_get_resolution(struct i2c_client *client, 245 u8 *hw_res_x, u8 *hw_res_y) 246 { 247 int ret; 248 int error; 249 u8 val[3]; 250 251 ret = i2c_smbus_read_block_data(client, ETP_SMBUS_RESOLUTION_CMD, val); 252 if (ret != 3) { 253 error = ret < 0 ? ret : -EIO; 254 dev_err(&client->dev, "failed to get resolution: %d\n", error); 255 return error; 256 } 257 258 *hw_res_x = val[1] & 0x0F; 259 *hw_res_y = (val[1] & 0xF0) >> 4; 260 261 return 0; 262 } 263 264 static int elan_smbus_get_num_traces(struct i2c_client *client, 265 unsigned int *x_traces, 266 unsigned int *y_traces) 267 { 268 int ret; 269 int error; 270 u8 val[3]; 271 272 ret = i2c_smbus_read_block_data(client, ETP_SMBUS_XY_TRACENUM_CMD, val); 273 if (ret != 3) { 274 error = ret < 0 ? ret : -EIO; 275 dev_err(&client->dev, "failed to get trace info: %d\n", error); 276 return error; 277 } 278 279 *x_traces = val[1]; 280 *y_traces = val[2]; 281 282 return 0; 283 } 284 285 static int elan_smbus_get_pressure_adjustment(struct i2c_client *client, 286 int *adjustment) 287 { 288 *adjustment = ETP_PRESSURE_OFFSET; 289 return 0; 290 } 291 292 static int elan_smbus_iap_get_mode(struct i2c_client *client, 293 enum tp_mode *mode) 294 { 295 int error; 296 u16 constant; 297 u8 val[3]; 298 299 error = i2c_smbus_read_block_data(client, ETP_SMBUS_IAP_CTRL_CMD, val); 300 if (error < 0) { 301 dev_err(&client->dev, "failed to read iap ctrol register: %d\n", 302 error); 303 return error; 304 } 305 306 constant = be16_to_cpup((__be16 *)val); 307 dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant); 308 309 *mode = (constant & ETP_SMBUS_IAP_MODE_ON) ? IAP_MODE : MAIN_MODE; 310 311 return 0; 312 } 313 314 static int elan_smbus_iap_reset(struct i2c_client *client) 315 { 316 int error; 317 318 error = i2c_smbus_write_byte(client, ETP_SMBUS_IAP_RESET_CMD); 319 if (error) { 320 dev_err(&client->dev, "cannot reset IC: %d\n", error); 321 return error; 322 } 323 324 return 0; 325 } 326 327 static int elan_smbus_set_flash_key(struct i2c_client *client) 328 { 329 int error; 330 u8 cmd[4] = { 0x00, 0x0B, 0x00, 0x5A }; 331 332 error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD, 333 sizeof(cmd), cmd); 334 if (error) { 335 dev_err(&client->dev, "cannot set flash key: %d\n", error); 336 return error; 337 } 338 339 return 0; 340 } 341 342 static int elan_smbus_prepare_fw_update(struct i2c_client *client) 343 { 344 struct device *dev = &client->dev; 345 int len; 346 int error; 347 enum tp_mode mode; 348 u8 val[3]; 349 u8 cmd[4] = {0x0F, 0x78, 0x00, 0x06}; 350 u16 password; 351 352 /* Get FW in which mode (IAP_MODE/MAIN_MODE) */ 353 error = elan_smbus_iap_get_mode(client, &mode); 354 if (error) 355 return error; 356 357 if (mode == MAIN_MODE) { 358 359 /* set flash key */ 360 error = elan_smbus_set_flash_key(client); 361 if (error) 362 return error; 363 364 /* write iap password */ 365 if (i2c_smbus_write_byte(client, 366 ETP_SMBUS_IAP_PASSWORD_WRITE) < 0) { 367 dev_err(dev, "cannot write iap password\n"); 368 return -EIO; 369 } 370 371 error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD, 372 sizeof(cmd), cmd); 373 if (error) { 374 dev_err(dev, "failed to write iap password: %d\n", 375 error); 376 return error; 377 } 378 379 /* 380 * Read back password to make sure we enabled flash 381 * successfully. 382 */ 383 len = i2c_smbus_read_block_data(client, 384 ETP_SMBUS_IAP_PASSWORD_READ, 385 val); 386 if (len < sizeof(u16)) { 387 error = len < 0 ? len : -EIO; 388 dev_err(dev, "failed to read iap password: %d\n", 389 error); 390 return error; 391 } 392 393 password = be16_to_cpup((__be16 *)val); 394 if (password != ETP_SMBUS_IAP_PASSWORD) { 395 dev_err(dev, "wrong iap password = 0x%X\n", password); 396 return -EIO; 397 } 398 399 /* Wait 30ms for MAIN_MODE change to IAP_MODE */ 400 msleep(30); 401 } 402 403 error = elan_smbus_set_flash_key(client); 404 if (error) 405 return error; 406 407 /* Reset IC */ 408 error = elan_smbus_iap_reset(client); 409 if (error) 410 return error; 411 412 return 0; 413 } 414 415 416 static int elan_smbus_write_fw_block(struct i2c_client *client, 417 const u8 *page, u16 checksum, int idx) 418 { 419 struct device *dev = &client->dev; 420 int error; 421 u16 result; 422 u8 val[3]; 423 424 /* 425 * Due to the limitation of smbus protocol limiting 426 * transfer to 32 bytes at a time, we must split block 427 * in 2 transfers. 428 */ 429 error = i2c_smbus_write_block_data(client, 430 ETP_SMBUS_WRITE_FW_BLOCK, 431 ETP_FW_PAGE_SIZE / 2, 432 page); 433 if (error) { 434 dev_err(dev, "Failed to write page %d (part %d): %d\n", 435 idx, 1, error); 436 return error; 437 } 438 439 error = i2c_smbus_write_block_data(client, 440 ETP_SMBUS_WRITE_FW_BLOCK, 441 ETP_FW_PAGE_SIZE / 2, 442 page + ETP_FW_PAGE_SIZE / 2); 443 if (error) { 444 dev_err(dev, "Failed to write page %d (part %d): %d\n", 445 idx, 2, error); 446 return error; 447 } 448 449 450 /* Wait for F/W to update one page ROM data. */ 451 usleep_range(8000, 10000); 452 453 error = i2c_smbus_read_block_data(client, 454 ETP_SMBUS_IAP_CTRL_CMD, val); 455 if (error < 0) { 456 dev_err(dev, "Failed to read IAP write result: %d\n", 457 error); 458 return error; 459 } 460 461 result = be16_to_cpup((__be16 *)val); 462 if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) { 463 dev_err(dev, "IAP reports failed write: %04hx\n", 464 result); 465 return -EIO; 466 } 467 468 return 0; 469 } 470 471 static int elan_smbus_get_report(struct i2c_client *client, u8 *report) 472 { 473 int len; 474 475 len = i2c_smbus_read_block_data(client, 476 ETP_SMBUS_PACKET_QUERY, 477 &report[ETP_SMBUS_REPORT_OFFSET]); 478 if (len < 0) { 479 dev_err(&client->dev, "failed to read report data: %d\n", len); 480 return len; 481 } 482 483 if (len != ETP_SMBUS_REPORT_LEN) { 484 dev_err(&client->dev, 485 "wrong report length (%d vs %d expected)\n", 486 len, ETP_SMBUS_REPORT_LEN); 487 return -EIO; 488 } 489 490 return 0; 491 } 492 493 static int elan_smbus_finish_fw_update(struct i2c_client *client, 494 struct completion *fw_completion) 495 { 496 /* No special handling unlike I2C transport */ 497 return 0; 498 } 499 500 static int elan_smbus_get_pattern(struct i2c_client *client, u8 *pattern) 501 { 502 *pattern = 0; 503 return 0; 504 } 505 506 const struct elan_transport_ops elan_smbus_ops = { 507 .initialize = elan_smbus_initialize, 508 .sleep_control = elan_smbus_sleep_control, 509 .power_control = elan_smbus_power_control, 510 .set_mode = elan_smbus_set_mode, 511 512 .calibrate = elan_smbus_calibrate, 513 .calibrate_result = elan_smbus_calibrate_result, 514 515 .get_baseline_data = elan_smbus_get_baseline_data, 516 517 .get_version = elan_smbus_get_version, 518 .get_sm_version = elan_smbus_get_sm_version, 519 .get_product_id = elan_smbus_get_product_id, 520 .get_checksum = elan_smbus_get_checksum, 521 .get_pressure_adjustment = elan_smbus_get_pressure_adjustment, 522 523 .get_max = elan_smbus_get_max, 524 .get_resolution = elan_smbus_get_resolution, 525 .get_num_traces = elan_smbus_get_num_traces, 526 527 .iap_get_mode = elan_smbus_iap_get_mode, 528 .iap_reset = elan_smbus_iap_reset, 529 530 .prepare_fw_update = elan_smbus_prepare_fw_update, 531 .write_fw_block = elan_smbus_write_fw_block, 532 .finish_fw_update = elan_smbus_finish_fw_update, 533 534 .get_report = elan_smbus_get_report, 535 .get_pattern = elan_smbus_get_pattern, 536 }; 537