| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Elan I2C/SMBus Touchpad driver |
| 4 | * |
| 5 | * Copyright (c) 2013 ELAN Microelectronics Corp. |
| 6 | * |
| 7 | * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw> |
| 8 | * Author: KT Liao <kt.liao@emc.com.tw> |
| 9 | * Version: 1.6.3 |
| 10 | * |
| 11 | * Based on cyapa driver: |
| 12 | * copyright (c) 2011-2012 Cypress Semiconductor, Inc. |
| 13 | * copyright (c) 2011-2012 Google, Inc. |
| 14 | * |
| 15 | * Trademarks are the property of their respective owners. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/acpi.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/device.h> |
| 21 | #include <linux/firmware.h> |
| 22 | #include <linux/i2c.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/input/mt.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/irq.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/sched.h> |
| 31 | #include <linux/string_choices.h> |
| 32 | #include <linux/input.h> |
| 33 | #include <linux/uaccess.h> |
| 34 | #include <linux/jiffies.h> |
| 35 | #include <linux/completion.h> |
| 36 | #include <linux/of.h> |
| 37 | #include <linux/pm_wakeirq.h> |
| 38 | #include <linux/property.h> |
| 39 | #include <linux/regulator/consumer.h> |
| 40 | #include <linux/unaligned.h> |
| 41 | |
| 42 | #include "elan_i2c.h" |
| 43 | |
| 44 | #define DRIVER_NAME "elan_i2c" |
| 45 | #define ELAN_VENDOR_ID 0x04f3 |
| 46 | #define ETP_MAX_PRESSURE 255 |
| 47 | #define ETP_FWIDTH_REDUCE 90 |
| 48 | #define ETP_FINGER_WIDTH 15 |
| 49 | #define ETP_RETRY_COUNT 3 |
| 50 | |
| 51 | /* quirks to control the device */ |
| 52 | #define ETP_QUIRK_QUICK_WAKEUP BIT(0) |
| 53 | |
| 54 | /* The main device structure */ |
| 55 | struct elan_tp_data { |
| 56 | struct i2c_client *client; |
| 57 | struct input_dev *input; |
| 58 | struct input_dev *tp_input; /* trackpoint input node */ |
| 59 | struct regulator *vcc; |
| 60 | |
| 61 | const struct elan_transport_ops *ops; |
| 62 | |
| 63 | /* for fw update */ |
| 64 | struct completion fw_completion; |
| 65 | bool in_fw_update; |
| 66 | |
| 67 | struct mutex sysfs_mutex; |
| 68 | |
| 69 | unsigned int max_x; |
| 70 | unsigned int max_y; |
| 71 | unsigned int width_x; |
| 72 | unsigned int width_y; |
| 73 | unsigned int x_res; |
| 74 | unsigned int y_res; |
| 75 | |
| 76 | u8 pattern; |
| 77 | u16 product_id; |
| 78 | u8 fw_version; |
| 79 | u8 sm_version; |
| 80 | u8 iap_version; |
| 81 | u16 fw_checksum; |
| 82 | unsigned int report_features; |
| 83 | unsigned int report_len; |
| 84 | int pressure_adjustment; |
| 85 | u8 mode; |
| 86 | u16 ic_type; |
| 87 | u16 fw_validpage_count; |
| 88 | u16 fw_page_size; |
| 89 | u32 fw_signature_address; |
| 90 | |
| 91 | u8 min_baseline; |
| 92 | u8 max_baseline; |
| 93 | bool baseline_ready; |
| 94 | u8 clickpad; |
| 95 | bool middle_button; |
| 96 | |
| 97 | u32 quirks; /* Various quirks */ |
| 98 | }; |
| 99 | |
| 100 | static u32 elan_i2c_lookup_quirks(u16 ic_type, u16 product_id) |
| 101 | { |
| 102 | static const struct { |
| 103 | u16 ic_type; |
| 104 | u16 product_id; |
| 105 | u32 quirks; |
| 106 | } elan_i2c_quirks[] = { |
| 107 | { 0x0D, ETP_PRODUCT_ID_DELBIN, ETP_QUIRK_QUICK_WAKEUP }, |
| 108 | { 0x0D, ETP_PRODUCT_ID_WHITEBOX, ETP_QUIRK_QUICK_WAKEUP }, |
| 109 | { 0x10, ETP_PRODUCT_ID_VOXEL, ETP_QUIRK_QUICK_WAKEUP }, |
| 110 | { 0x14, ETP_PRODUCT_ID_MAGPIE, ETP_QUIRK_QUICK_WAKEUP }, |
| 111 | { 0x14, ETP_PRODUCT_ID_BOBBA, ETP_QUIRK_QUICK_WAKEUP }, |
| 112 | }; |
| 113 | u32 quirks = 0; |
| 114 | int i; |
| 115 | |
| 116 | for (i = 0; i < ARRAY_SIZE(elan_i2c_quirks); i++) { |
| 117 | if (elan_i2c_quirks[i].ic_type == ic_type && |
| 118 | elan_i2c_quirks[i].product_id == product_id) { |
| 119 | quirks = elan_i2c_quirks[i].quirks; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if (ic_type >= 0x0D && product_id >= 0x123) |
| 124 | quirks |= ETP_QUIRK_QUICK_WAKEUP; |
| 125 | |
| 126 | return quirks; |
| 127 | } |
| 128 | |
| 129 | static int elan_get_fwinfo(u16 ic_type, u8 iap_version, u16 *validpage_count, |
| 130 | u32 *signature_address, u16 *page_size) |
| 131 | { |
| 132 | switch (ic_type) { |
| 133 | case 0x00: |
| 134 | case 0x06: |
| 135 | case 0x08: |
| 136 | *validpage_count = 512; |
| 137 | break; |
| 138 | case 0x03: |
| 139 | case 0x07: |
| 140 | case 0x09: |
| 141 | case 0x0A: |
| 142 | case 0x0B: |
| 143 | case 0x0C: |
| 144 | *validpage_count = 768; |
| 145 | break; |
| 146 | case 0x0D: |
| 147 | *validpage_count = 896; |
| 148 | break; |
| 149 | case 0x0E: |
| 150 | *validpage_count = 640; |
| 151 | break; |
| 152 | case 0x10: |
| 153 | *validpage_count = 1024; |
| 154 | break; |
| 155 | case 0x11: |
| 156 | *validpage_count = 1280; |
| 157 | break; |
| 158 | case 0x13: |
| 159 | *validpage_count = 2048; |
| 160 | break; |
| 161 | case 0x14: |
| 162 | case 0x15: |
| 163 | *validpage_count = 1024; |
| 164 | break; |
| 165 | default: |
| 166 | /* unknown ic type clear value */ |
| 167 | *validpage_count = 0; |
| 168 | *signature_address = 0; |
| 169 | *page_size = 0; |
| 170 | return -ENXIO; |
| 171 | } |
| 172 | |
| 173 | *signature_address = |
| 174 | (*validpage_count * ETP_FW_PAGE_SIZE) - ETP_FW_SIGNATURE_SIZE; |
| 175 | |
| 176 | if ((ic_type == 0x14 || ic_type == 0x15) && iap_version >= 2) { |
| 177 | *validpage_count /= 8; |
| 178 | *page_size = ETP_FW_PAGE_SIZE_512; |
| 179 | } else if (ic_type >= 0x0D && iap_version >= 1) { |
| 180 | *validpage_count /= 2; |
| 181 | *page_size = ETP_FW_PAGE_SIZE_128; |
| 182 | } else { |
| 183 | *page_size = ETP_FW_PAGE_SIZE; |
| 184 | } |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static int elan_set_power(struct elan_tp_data *data, bool on) |
| 190 | { |
| 191 | int repeat = ETP_RETRY_COUNT; |
| 192 | int error; |
| 193 | |
| 194 | do { |
| 195 | error = data->ops->power_control(data->client, on); |
| 196 | if (error >= 0) |
| 197 | return 0; |
| 198 | |
| 199 | msleep(msecs: 30); |
| 200 | } while (--repeat > 0); |
| 201 | |
| 202 | dev_err(&data->client->dev, "failed to set power %s: %d\n" , |
| 203 | str_on_off(on), error); |
| 204 | return error; |
| 205 | } |
| 206 | |
| 207 | static int elan_sleep(struct elan_tp_data *data) |
| 208 | { |
| 209 | int repeat = ETP_RETRY_COUNT; |
| 210 | int error; |
| 211 | |
| 212 | do { |
| 213 | error = data->ops->sleep_control(data->client, true); |
| 214 | if (!error) |
| 215 | return 0; |
| 216 | |
| 217 | msleep(msecs: 30); |
| 218 | } while (--repeat > 0); |
| 219 | |
| 220 | return error; |
| 221 | } |
| 222 | |
| 223 | static int elan_query_product(struct elan_tp_data *data) |
| 224 | { |
| 225 | int error; |
| 226 | |
| 227 | error = data->ops->get_product_id(data->client, &data->product_id); |
| 228 | if (error) |
| 229 | return error; |
| 230 | |
| 231 | error = data->ops->get_pattern(data->client, &data->pattern); |
| 232 | if (error) |
| 233 | return error; |
| 234 | |
| 235 | error = data->ops->get_sm_version(data->client, data->pattern, |
| 236 | &data->ic_type, &data->sm_version, |
| 237 | &data->clickpad); |
| 238 | if (error) |
| 239 | return error; |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static int elan_check_ASUS_special_fw(struct elan_tp_data *data) |
| 245 | { |
| 246 | if (data->ic_type == 0x0E) { |
| 247 | switch (data->product_id) { |
| 248 | case 0x05 ... 0x07: |
| 249 | case 0x09: |
| 250 | case 0x13: |
| 251 | return true; |
| 252 | } |
| 253 | } else if (data->ic_type == 0x08 && data->product_id == 0x26) { |
| 254 | /* ASUS EeeBook X205TA */ |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | static int __elan_initialize(struct elan_tp_data *data, bool skip_reset) |
| 262 | { |
| 263 | struct i2c_client *client = data->client; |
| 264 | bool woken_up = false; |
| 265 | int error; |
| 266 | |
| 267 | if (!skip_reset) { |
| 268 | error = data->ops->initialize(client); |
| 269 | if (error) { |
| 270 | dev_err(&client->dev, "device initialize failed: %d\n" , error); |
| 271 | return error; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | error = elan_query_product(data); |
| 276 | if (error) |
| 277 | return error; |
| 278 | |
| 279 | /* |
| 280 | * Some ASUS devices were shipped with firmware that requires |
| 281 | * touchpads to be woken up first, before attempting to switch |
| 282 | * them into absolute reporting mode. |
| 283 | */ |
| 284 | if (elan_check_ASUS_special_fw(data)) { |
| 285 | error = data->ops->sleep_control(client, false); |
| 286 | if (error) { |
| 287 | dev_err(&client->dev, |
| 288 | "failed to wake device up: %d\n" , error); |
| 289 | return error; |
| 290 | } |
| 291 | |
| 292 | msleep(msecs: 200); |
| 293 | woken_up = true; |
| 294 | } |
| 295 | |
| 296 | data->mode |= ETP_ENABLE_ABS; |
| 297 | error = data->ops->set_mode(client, data->mode); |
| 298 | if (error) { |
| 299 | dev_err(&client->dev, |
| 300 | "failed to switch to absolute mode: %d\n" , error); |
| 301 | return error; |
| 302 | } |
| 303 | |
| 304 | if (!woken_up) { |
| 305 | error = data->ops->sleep_control(client, false); |
| 306 | if (error) { |
| 307 | dev_err(&client->dev, |
| 308 | "failed to wake device up: %d\n" , error); |
| 309 | return error; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static int elan_initialize(struct elan_tp_data *data, bool skip_reset) |
| 317 | { |
| 318 | int repeat = ETP_RETRY_COUNT; |
| 319 | int error; |
| 320 | |
| 321 | do { |
| 322 | error = __elan_initialize(data, skip_reset); |
| 323 | if (!error) |
| 324 | return 0; |
| 325 | |
| 326 | skip_reset = false; |
| 327 | msleep(msecs: 30); |
| 328 | } while (--repeat > 0); |
| 329 | |
| 330 | return error; |
| 331 | } |
| 332 | |
| 333 | static int elan_query_device_info(struct elan_tp_data *data) |
| 334 | { |
| 335 | int error; |
| 336 | |
| 337 | error = data->ops->get_version(data->client, data->pattern, false, |
| 338 | &data->fw_version); |
| 339 | if (error) |
| 340 | return error; |
| 341 | |
| 342 | error = data->ops->get_checksum(data->client, false, |
| 343 | &data->fw_checksum); |
| 344 | if (error) |
| 345 | return error; |
| 346 | |
| 347 | error = data->ops->get_version(data->client, data->pattern, |
| 348 | true, &data->iap_version); |
| 349 | if (error) |
| 350 | return error; |
| 351 | |
| 352 | error = data->ops->get_pressure_adjustment(data->client, |
| 353 | &data->pressure_adjustment); |
| 354 | if (error) |
| 355 | return error; |
| 356 | |
| 357 | error = data->ops->get_report_features(data->client, data->pattern, |
| 358 | &data->report_features, |
| 359 | &data->report_len); |
| 360 | if (error) |
| 361 | return error; |
| 362 | |
| 363 | data->quirks = elan_i2c_lookup_quirks(ic_type: data->ic_type, product_id: data->product_id); |
| 364 | |
| 365 | error = elan_get_fwinfo(ic_type: data->ic_type, iap_version: data->iap_version, |
| 366 | validpage_count: &data->fw_validpage_count, |
| 367 | signature_address: &data->fw_signature_address, |
| 368 | page_size: &data->fw_page_size); |
| 369 | if (error) |
| 370 | dev_warn(&data->client->dev, |
| 371 | "unexpected iap version %#04x (ic type: %#04x), firmware update will not work\n" , |
| 372 | data->iap_version, data->ic_type); |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | static unsigned int elan_convert_resolution(u8 val, u8 pattern) |
| 378 | { |
| 379 | /* |
| 380 | * pattern <= 0x01: |
| 381 | * (value from firmware) * 10 + 790 = dpi |
| 382 | * else |
| 383 | * ((value from firmware) + 3) * 100 = dpi |
| 384 | */ |
| 385 | int res = pattern <= 0x01 ? |
| 386 | (int)(char)val * 10 + 790 : ((int)(char)val + 3) * 100; |
| 387 | /* |
| 388 | * We also have to convert dpi to dots/mm (*10/254 to avoid floating |
| 389 | * point). |
| 390 | */ |
| 391 | return res * 10 / 254; |
| 392 | } |
| 393 | |
| 394 | static int elan_query_device_parameters(struct elan_tp_data *data) |
| 395 | { |
| 396 | struct i2c_client *client = data->client; |
| 397 | unsigned int x_traces, y_traces; |
| 398 | u32 x_mm, y_mm; |
| 399 | u8 hw_x_res, hw_y_res; |
| 400 | int error; |
| 401 | |
| 402 | if (device_property_read_u32(dev: &client->dev, |
| 403 | propname: "touchscreen-size-x" , val: &data->max_x) || |
| 404 | device_property_read_u32(dev: &client->dev, |
| 405 | propname: "touchscreen-size-y" , val: &data->max_y)) { |
| 406 | error = data->ops->get_max(data->client, |
| 407 | &data->max_x, |
| 408 | &data->max_y); |
| 409 | if (error) |
| 410 | return error; |
| 411 | } else { |
| 412 | /* size is the maximum + 1 */ |
| 413 | --data->max_x; |
| 414 | --data->max_y; |
| 415 | } |
| 416 | |
| 417 | if (device_property_read_u32(dev: &client->dev, |
| 418 | propname: "elan,x_traces" , |
| 419 | val: &x_traces) || |
| 420 | device_property_read_u32(dev: &client->dev, |
| 421 | propname: "elan,y_traces" , |
| 422 | val: &y_traces)) { |
| 423 | error = data->ops->get_num_traces(data->client, |
| 424 | &x_traces, &y_traces); |
| 425 | if (error) |
| 426 | return error; |
| 427 | } |
| 428 | data->width_x = data->max_x / x_traces; |
| 429 | data->width_y = data->max_y / y_traces; |
| 430 | |
| 431 | if (device_property_read_u32(dev: &client->dev, |
| 432 | propname: "touchscreen-x-mm" , val: &x_mm) || |
| 433 | device_property_read_u32(dev: &client->dev, |
| 434 | propname: "touchscreen-y-mm" , val: &y_mm)) { |
| 435 | error = data->ops->get_resolution(data->client, |
| 436 | &hw_x_res, &hw_y_res); |
| 437 | if (error) |
| 438 | return error; |
| 439 | |
| 440 | data->x_res = elan_convert_resolution(val: hw_x_res, pattern: data->pattern); |
| 441 | data->y_res = elan_convert_resolution(val: hw_y_res, pattern: data->pattern); |
| 442 | } else { |
| 443 | data->x_res = (data->max_x + 1) / x_mm; |
| 444 | data->y_res = (data->max_y + 1) / y_mm; |
| 445 | } |
| 446 | |
| 447 | if (device_property_read_bool(dev: &client->dev, propname: "elan,clickpad" )) |
| 448 | data->clickpad = 1; |
| 449 | |
| 450 | if (device_property_read_bool(dev: &client->dev, propname: "elan,middle-button" )) |
| 451 | data->middle_button = true; |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | ********************************************************** |
| 458 | * IAP firmware updater related routines |
| 459 | ********************************************************** |
| 460 | */ |
| 461 | static int elan_write_fw_block(struct elan_tp_data *data, u16 page_size, |
| 462 | const u8 *page, u16 checksum, int idx) |
| 463 | { |
| 464 | int retry = ETP_RETRY_COUNT; |
| 465 | int error; |
| 466 | |
| 467 | do { |
| 468 | error = data->ops->write_fw_block(data->client, page_size, |
| 469 | page, checksum, idx); |
| 470 | if (!error) |
| 471 | return 0; |
| 472 | |
| 473 | dev_dbg(&data->client->dev, |
| 474 | "IAP retrying page %d (error: %d)\n" , idx, error); |
| 475 | } while (--retry > 0); |
| 476 | |
| 477 | return error; |
| 478 | } |
| 479 | |
| 480 | static int __elan_update_firmware(struct elan_tp_data *data, |
| 481 | const struct firmware *fw) |
| 482 | { |
| 483 | struct i2c_client *client = data->client; |
| 484 | struct device *dev = &client->dev; |
| 485 | int i, j; |
| 486 | int error; |
| 487 | u16 iap_start_addr; |
| 488 | u16 boot_page_count; |
| 489 | u16 sw_checksum = 0, fw_checksum = 0; |
| 490 | |
| 491 | error = data->ops->prepare_fw_update(client, data->ic_type, |
| 492 | data->iap_version, |
| 493 | data->fw_page_size); |
| 494 | if (error) |
| 495 | return error; |
| 496 | |
| 497 | iap_start_addr = get_unaligned_le16(p: &fw->data[ETP_IAP_START_ADDR * 2]); |
| 498 | |
| 499 | boot_page_count = (iap_start_addr * 2) / data->fw_page_size; |
| 500 | for (i = boot_page_count; i < data->fw_validpage_count; i++) { |
| 501 | u16 checksum = 0; |
| 502 | const u8 *page = &fw->data[i * data->fw_page_size]; |
| 503 | |
| 504 | for (j = 0; j < data->fw_page_size; j += 2) |
| 505 | checksum += ((page[j + 1] << 8) | page[j]); |
| 506 | |
| 507 | error = elan_write_fw_block(data, page_size: data->fw_page_size, |
| 508 | page, checksum, idx: i); |
| 509 | if (error) { |
| 510 | dev_err(dev, "write page %d fail: %d\n" , i, error); |
| 511 | return error; |
| 512 | } |
| 513 | |
| 514 | sw_checksum += checksum; |
| 515 | } |
| 516 | |
| 517 | /* Wait WDT reset and power on reset */ |
| 518 | msleep(msecs: 600); |
| 519 | |
| 520 | error = data->ops->finish_fw_update(client, &data->fw_completion); |
| 521 | if (error) |
| 522 | return error; |
| 523 | |
| 524 | error = data->ops->get_checksum(client, true, &fw_checksum); |
| 525 | if (error) |
| 526 | return error; |
| 527 | |
| 528 | if (sw_checksum != fw_checksum) { |
| 529 | dev_err(dev, "checksum diff sw=[%04X], fw=[%04X]\n" , |
| 530 | sw_checksum, fw_checksum); |
| 531 | return -EIO; |
| 532 | } |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | static int elan_update_firmware(struct elan_tp_data *data, |
| 538 | const struct firmware *fw) |
| 539 | { |
| 540 | struct i2c_client *client = data->client; |
| 541 | int retval; |
| 542 | |
| 543 | dev_dbg(&client->dev, "Starting firmware update....\n" ); |
| 544 | |
| 545 | guard(disable_irq)(l: &client->irq); |
| 546 | |
| 547 | data->in_fw_update = true; |
| 548 | |
| 549 | retval = __elan_update_firmware(data, fw); |
| 550 | if (retval) { |
| 551 | dev_err(&client->dev, "firmware update failed: %d\n" , retval); |
| 552 | data->ops->iap_reset(client); |
| 553 | } else { |
| 554 | /* Reinitialize TP after fw is updated */ |
| 555 | elan_initialize(data, skip_reset: false); |
| 556 | elan_query_device_info(data); |
| 557 | } |
| 558 | |
| 559 | data->in_fw_update = false; |
| 560 | |
| 561 | return retval; |
| 562 | } |
| 563 | |
| 564 | /* |
| 565 | ******************************************************************* |
| 566 | * SYSFS attributes |
| 567 | ******************************************************************* |
| 568 | */ |
| 569 | static ssize_t elan_sysfs_read_fw_checksum(struct device *dev, |
| 570 | struct device_attribute *attr, |
| 571 | char *buf) |
| 572 | { |
| 573 | struct i2c_client *client = to_i2c_client(dev); |
| 574 | struct elan_tp_data *data = i2c_get_clientdata(client); |
| 575 | |
| 576 | return sysfs_emit(buf, fmt: "0x%04x\n" , data->fw_checksum); |
| 577 | } |
| 578 | |
| 579 | static ssize_t elan_sysfs_read_product_id(struct device *dev, |
| 580 | struct device_attribute *attr, |
| 581 | char *buf) |
| 582 | { |
| 583 | struct i2c_client *client = to_i2c_client(dev); |
| 584 | struct elan_tp_data *data = i2c_get_clientdata(client); |
| 585 | |
| 586 | return sysfs_emit(buf, ETP_PRODUCT_ID_FORMAT_STRING "\n" , |
| 587 | data->product_id); |
| 588 | } |
| 589 | |
| 590 | static ssize_t elan_sysfs_read_fw_ver(struct device *dev, |
| 591 | struct device_attribute *attr, |
| 592 | char *buf) |
| 593 | { |
| 594 | struct i2c_client *client = to_i2c_client(dev); |
| 595 | struct elan_tp_data *data = i2c_get_clientdata(client); |
| 596 | |
| 597 | return sysfs_emit(buf, fmt: "%d.0\n" , data->fw_version); |
| 598 | } |
| 599 | |
| 600 | static ssize_t elan_sysfs_read_sm_ver(struct device *dev, |
| 601 | struct device_attribute *attr, |
| 602 | char *buf) |
| 603 | { |
| 604 | struct i2c_client *client = to_i2c_client(dev); |
| 605 | struct elan_tp_data *data = i2c_get_clientdata(client); |
| 606 | |
| 607 | return sysfs_emit(buf, fmt: "%d.0\n" , data->sm_version); |
| 608 | } |
| 609 | |
| 610 | static ssize_t elan_sysfs_read_iap_ver(struct device *dev, |
| 611 | struct device_attribute *attr, |
| 612 | char *buf) |
| 613 | { |
| 614 | struct i2c_client *client = to_i2c_client(dev); |
| 615 | struct elan_tp_data *data = i2c_get_clientdata(client); |
| 616 | |
| 617 | return sysfs_emit(buf, fmt: "%d.0\n" , data->iap_version); |
| 618 | } |
| 619 | |
| 620 | static ssize_t elan_sysfs_update_fw(struct device *dev, |
| 621 | struct device_attribute *attr, |
| 622 | const char *buf, size_t count) |
| 623 | { |
| 624 | struct elan_tp_data *data = dev_get_drvdata(dev); |
| 625 | int error; |
| 626 | const u8 *fw_signature; |
| 627 | static const u8 signature[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF}; |
| 628 | |
| 629 | if (data->fw_validpage_count == 0) |
| 630 | return -EINVAL; |
| 631 | |
| 632 | /* Look for a firmware with the product id appended. */ |
| 633 | const char *fw_name __free(kfree) = |
| 634 | kasprintf(GFP_KERNEL, ETP_FW_NAME, data->product_id); |
| 635 | if (!fw_name) { |
| 636 | dev_err(dev, "failed to allocate memory for firmware name\n" ); |
| 637 | return -ENOMEM; |
| 638 | } |
| 639 | |
| 640 | dev_info(dev, "requesting fw '%s'\n" , fw_name); |
| 641 | const struct firmware *fw __free(firmware) = NULL; |
| 642 | error = request_firmware(fw: &fw, name: fw_name, device: dev); |
| 643 | if (error) { |
| 644 | dev_err(dev, "failed to request firmware: %d\n" , error); |
| 645 | return error; |
| 646 | } |
| 647 | |
| 648 | /* Firmware file must match signature data */ |
| 649 | fw_signature = &fw->data[data->fw_signature_address]; |
| 650 | if (memcmp(p: fw_signature, q: signature, size: sizeof(signature)) != 0) { |
| 651 | dev_err(dev, "signature mismatch (expected %*ph, got %*ph)\n" , |
| 652 | (int)sizeof(signature), signature, |
| 653 | (int)sizeof(signature), fw_signature); |
| 654 | return -EBADF; |
| 655 | } |
| 656 | |
| 657 | scoped_cond_guard(mutex_intr, return -EINTR, &data->sysfs_mutex) { |
| 658 | error = elan_update_firmware(data, fw); |
| 659 | if (error) |
| 660 | return error; |
| 661 | } |
| 662 | |
| 663 | return count; |
| 664 | } |
| 665 | |
| 666 | static int elan_calibrate(struct elan_tp_data *data) |
| 667 | { |
| 668 | struct i2c_client *client = data->client; |
| 669 | struct device *dev = &client->dev; |
| 670 | int tries = 20; |
| 671 | int retval; |
| 672 | int error; |
| 673 | u8 val[ETP_CALIBRATE_MAX_LEN]; |
| 674 | |
| 675 | guard(disable_irq)(l: &client->irq); |
| 676 | |
| 677 | data->mode |= ETP_ENABLE_CALIBRATE; |
| 678 | retval = data->ops->set_mode(client, data->mode); |
| 679 | if (retval) { |
| 680 | data->mode &= ~ETP_ENABLE_CALIBRATE; |
| 681 | dev_err(dev, "failed to enable calibration mode: %d\n" , |
| 682 | retval); |
| 683 | return retval; |
| 684 | } |
| 685 | |
| 686 | retval = data->ops->calibrate(client); |
| 687 | if (retval) { |
| 688 | dev_err(dev, "failed to start calibration: %d\n" , |
| 689 | retval); |
| 690 | goto out_disable_calibrate; |
| 691 | } |
| 692 | |
| 693 | val[0] = 0xff; |
| 694 | do { |
| 695 | /* Wait 250ms before checking if calibration has completed. */ |
| 696 | msleep(msecs: 250); |
| 697 | |
| 698 | retval = data->ops->calibrate_result(client, val); |
| 699 | if (retval) |
| 700 | dev_err(dev, "failed to check calibration result: %d\n" , |
| 701 | retval); |
| 702 | else if (val[0] == 0) |
| 703 | break; /* calibration done */ |
| 704 | |
| 705 | } while (--tries); |
| 706 | |
| 707 | if (tries == 0) { |
| 708 | dev_err(dev, "failed to calibrate. Timeout.\n" ); |
| 709 | retval = -ETIMEDOUT; |
| 710 | } |
| 711 | |
| 712 | out_disable_calibrate: |
| 713 | data->mode &= ~ETP_ENABLE_CALIBRATE; |
| 714 | error = data->ops->set_mode(data->client, data->mode); |
| 715 | if (error) { |
| 716 | dev_err(dev, "failed to disable calibration mode: %d\n" , |
| 717 | error); |
| 718 | if (!retval) |
| 719 | retval = error; |
| 720 | } |
| 721 | return retval; |
| 722 | } |
| 723 | |
| 724 | static ssize_t calibrate_store(struct device *dev, |
| 725 | struct device_attribute *attr, |
| 726 | const char *buf, size_t count) |
| 727 | { |
| 728 | struct i2c_client *client = to_i2c_client(dev); |
| 729 | struct elan_tp_data *data = i2c_get_clientdata(client); |
| 730 | int error; |
| 731 | |
| 732 | scoped_cond_guard(mutex_intr, return -EINTR, &data->sysfs_mutex) { |
| 733 | error = elan_calibrate(data); |
| 734 | if (error) |
| 735 | return error; |
| 736 | } |
| 737 | |
| 738 | return count; |
| 739 | } |
| 740 | |
| 741 | static ssize_t elan_sysfs_read_mode(struct device *dev, |
| 742 | struct device_attribute *attr, |
| 743 | char *buf) |
| 744 | { |
| 745 | struct i2c_client *client = to_i2c_client(dev); |
| 746 | struct elan_tp_data *data = i2c_get_clientdata(client); |
| 747 | int error; |
| 748 | enum tp_mode mode; |
| 749 | |
| 750 | scoped_cond_guard(mutex_intr, return -EINTR, &data->sysfs_mutex) { |
| 751 | error = data->ops->iap_get_mode(data->client, &mode); |
| 752 | if (error) |
| 753 | return error; |
| 754 | } |
| 755 | |
| 756 | return sysfs_emit(buf, fmt: "%d\n" , (int)mode); |
| 757 | } |
| 758 | |
| 759 | static DEVICE_ATTR(product_id, S_IRUGO, elan_sysfs_read_product_id, NULL); |
| 760 | static DEVICE_ATTR(firmware_version, S_IRUGO, elan_sysfs_read_fw_ver, NULL); |
| 761 | static DEVICE_ATTR(sample_version, S_IRUGO, elan_sysfs_read_sm_ver, NULL); |
| 762 | static DEVICE_ATTR(iap_version, S_IRUGO, elan_sysfs_read_iap_ver, NULL); |
| 763 | static DEVICE_ATTR(fw_checksum, S_IRUGO, elan_sysfs_read_fw_checksum, NULL); |
| 764 | static DEVICE_ATTR(mode, S_IRUGO, elan_sysfs_read_mode, NULL); |
| 765 | static DEVICE_ATTR(update_fw, S_IWUSR, NULL, elan_sysfs_update_fw); |
| 766 | |
| 767 | static DEVICE_ATTR_WO(calibrate); |
| 768 | |
| 769 | static struct attribute *elan_sysfs_entries[] = { |
| 770 | &dev_attr_product_id.attr, |
| 771 | &dev_attr_firmware_version.attr, |
| 772 | &dev_attr_sample_version.attr, |
| 773 | &dev_attr_iap_version.attr, |
| 774 | &dev_attr_fw_checksum.attr, |
| 775 | &dev_attr_calibrate.attr, |
| 776 | &dev_attr_mode.attr, |
| 777 | &dev_attr_update_fw.attr, |
| 778 | NULL, |
| 779 | }; |
| 780 | |
| 781 | static const struct attribute_group elan_sysfs_group = { |
| 782 | .attrs = elan_sysfs_entries, |
| 783 | }; |
| 784 | |
| 785 | static int elan_acquire_baseline(struct elan_tp_data *data) |
| 786 | { |
| 787 | struct i2c_client *client = data->client; |
| 788 | struct device *dev = &client->dev; |
| 789 | int retval; |
| 790 | int error; |
| 791 | |
| 792 | guard(disable_irq)(l: &client->irq); |
| 793 | |
| 794 | data->baseline_ready = false; |
| 795 | |
| 796 | data->mode |= ETP_ENABLE_CALIBRATE; |
| 797 | retval = data->ops->set_mode(client, data->mode); |
| 798 | if (retval) { |
| 799 | data->mode &= ~ETP_ENABLE_CALIBRATE; |
| 800 | dev_err(dev, "Failed to enable calibration mode to get baseline: %d\n" , |
| 801 | retval); |
| 802 | return retval; |
| 803 | } |
| 804 | |
| 805 | msleep(msecs: 250); |
| 806 | |
| 807 | retval = data->ops->get_baseline_data(client, true, |
| 808 | &data->max_baseline); |
| 809 | if (retval) { |
| 810 | dev_err(dev, "Failed to read max baseline from device: %d\n" , |
| 811 | retval); |
| 812 | goto out_disable_calibrate; |
| 813 | } |
| 814 | |
| 815 | retval = data->ops->get_baseline_data(client, false, |
| 816 | &data->min_baseline); |
| 817 | if (retval) { |
| 818 | dev_err(dev, "Failed to read min baseline from device: %d\n" , |
| 819 | retval); |
| 820 | goto out_disable_calibrate; |
| 821 | } |
| 822 | |
| 823 | data->baseline_ready = true; |
| 824 | |
| 825 | out_disable_calibrate: |
| 826 | data->mode &= ~ETP_ENABLE_CALIBRATE; |
| 827 | error = data->ops->set_mode(client, data->mode); |
| 828 | if (error) { |
| 829 | dev_err(dev, "Failed to disable calibration mode after acquiring baseline: %d\n" , |
| 830 | error); |
| 831 | if (!retval) |
| 832 | retval = error; |
| 833 | } |
| 834 | |
| 835 | return retval; |
| 836 | } |
| 837 | |
| 838 | static ssize_t acquire_store(struct device *dev, struct device_attribute *attr, |
| 839 | const char *buf, size_t count) |
| 840 | { |
| 841 | struct i2c_client *client = to_i2c_client(dev); |
| 842 | struct elan_tp_data *data = i2c_get_clientdata(client); |
| 843 | int error; |
| 844 | |
| 845 | scoped_cond_guard(mutex_intr, return -EINTR, &data->sysfs_mutex) { |
| 846 | error = elan_acquire_baseline(data); |
| 847 | if (error) |
| 848 | return error; |
| 849 | } |
| 850 | |
| 851 | return count; |
| 852 | } |
| 853 | |
| 854 | static ssize_t min_show(struct device *dev, |
| 855 | struct device_attribute *attr, char *buf) |
| 856 | { |
| 857 | struct i2c_client *client = to_i2c_client(dev); |
| 858 | struct elan_tp_data *data = i2c_get_clientdata(client); |
| 859 | |
| 860 | scoped_guard(mutex_intr, &data->sysfs_mutex) { |
| 861 | if (!data->baseline_ready) |
| 862 | return -ENODATA; |
| 863 | |
| 864 | return sysfs_emit(buf, fmt: "%d" , data->min_baseline); |
| 865 | } |
| 866 | |
| 867 | return -EINTR; |
| 868 | } |
| 869 | |
| 870 | static ssize_t max_show(struct device *dev, |
| 871 | struct device_attribute *attr, char *buf) |
| 872 | { |
| 873 | struct i2c_client *client = to_i2c_client(dev); |
| 874 | struct elan_tp_data *data = i2c_get_clientdata(client); |
| 875 | |
| 876 | scoped_guard(mutex_intr, &data->sysfs_mutex) { |
| 877 | if (!data->baseline_ready) |
| 878 | return -ENODATA; |
| 879 | |
| 880 | return sysfs_emit(buf, fmt: "%d" , data->max_baseline); |
| 881 | } |
| 882 | |
| 883 | return -EINTR; |
| 884 | } |
| 885 | |
| 886 | static DEVICE_ATTR_WO(acquire); |
| 887 | static DEVICE_ATTR_RO(min); |
| 888 | static DEVICE_ATTR_RO(max); |
| 889 | |
| 890 | static struct attribute *elan_baseline_sysfs_entries[] = { |
| 891 | &dev_attr_acquire.attr, |
| 892 | &dev_attr_min.attr, |
| 893 | &dev_attr_max.attr, |
| 894 | NULL, |
| 895 | }; |
| 896 | |
| 897 | static const struct attribute_group elan_baseline_sysfs_group = { |
| 898 | .name = "baseline" , |
| 899 | .attrs = elan_baseline_sysfs_entries, |
| 900 | }; |
| 901 | |
| 902 | static const struct attribute_group *elan_sysfs_groups[] = { |
| 903 | &elan_sysfs_group, |
| 904 | &elan_baseline_sysfs_group, |
| 905 | NULL |
| 906 | }; |
| 907 | |
| 908 | /* |
| 909 | ****************************************************************** |
| 910 | * Elan isr functions |
| 911 | ****************************************************************** |
| 912 | */ |
| 913 | static void elan_report_contact(struct elan_tp_data *data, int contact_num, |
| 914 | bool contact_valid, bool high_precision, |
| 915 | u8 *packet, u8 *finger_data) |
| 916 | { |
| 917 | struct input_dev *input = data->input; |
| 918 | unsigned int pos_x, pos_y; |
| 919 | unsigned int pressure, scaled_pressure; |
| 920 | |
| 921 | if (contact_valid) { |
| 922 | if (high_precision) { |
| 923 | pos_x = get_unaligned_be16(p: &finger_data[0]); |
| 924 | pos_y = get_unaligned_be16(p: &finger_data[2]); |
| 925 | } else { |
| 926 | pos_x = ((finger_data[0] & 0xf0) << 4) | finger_data[1]; |
| 927 | pos_y = ((finger_data[0] & 0x0f) << 8) | finger_data[2]; |
| 928 | } |
| 929 | |
| 930 | if (pos_x > data->max_x || pos_y > data->max_y) { |
| 931 | dev_dbg(input->dev.parent, |
| 932 | "[%d] x=%d y=%d over max (%d, %d)" , |
| 933 | contact_num, pos_x, pos_y, |
| 934 | data->max_x, data->max_y); |
| 935 | return; |
| 936 | } |
| 937 | |
| 938 | pressure = finger_data[4]; |
| 939 | scaled_pressure = pressure + data->pressure_adjustment; |
| 940 | if (scaled_pressure > ETP_MAX_PRESSURE) |
| 941 | scaled_pressure = ETP_MAX_PRESSURE; |
| 942 | |
| 943 | input_mt_slot(dev: input, slot: contact_num); |
| 944 | input_mt_report_slot_state(dev: input, MT_TOOL_FINGER, active: true); |
| 945 | input_report_abs(dev: input, ABS_MT_POSITION_X, value: pos_x); |
| 946 | input_report_abs(dev: input, ABS_MT_POSITION_Y, value: data->max_y - pos_y); |
| 947 | input_report_abs(dev: input, ABS_MT_PRESSURE, value: scaled_pressure); |
| 948 | |
| 949 | if (data->report_features & ETP_FEATURE_REPORT_MK) { |
| 950 | unsigned int mk_x, mk_y, area_x, area_y; |
| 951 | u8 mk_data = high_precision ? |
| 952 | packet[ETP_MK_DATA_OFFSET + contact_num] : |
| 953 | finger_data[3]; |
| 954 | |
| 955 | mk_x = mk_data & 0x0f; |
| 956 | mk_y = mk_data >> 4; |
| 957 | |
| 958 | /* |
| 959 | * To avoid treating large finger as palm, let's reduce |
| 960 | * the width x and y per trace. |
| 961 | */ |
| 962 | area_x = mk_x * (data->width_x - ETP_FWIDTH_REDUCE); |
| 963 | area_y = mk_y * (data->width_y - ETP_FWIDTH_REDUCE); |
| 964 | |
| 965 | input_report_abs(dev: input, ABS_TOOL_WIDTH, value: mk_x); |
| 966 | input_report_abs(dev: input, ABS_MT_TOUCH_MAJOR, |
| 967 | max(area_x, area_y)); |
| 968 | input_report_abs(dev: input, ABS_MT_TOUCH_MINOR, |
| 969 | min(area_x, area_y)); |
| 970 | } |
| 971 | } else { |
| 972 | input_mt_slot(dev: input, slot: contact_num); |
| 973 | input_mt_report_slot_inactive(dev: input); |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | static void elan_report_absolute(struct elan_tp_data *data, u8 *packet, |
| 978 | bool high_precision) |
| 979 | { |
| 980 | struct input_dev *input = data->input; |
| 981 | u8 *finger_data = &packet[ETP_FINGER_DATA_OFFSET]; |
| 982 | int i; |
| 983 | u8 tp_info = packet[ETP_TOUCH_INFO_OFFSET]; |
| 984 | u8 hover_info = packet[ETP_HOVER_INFO_OFFSET]; |
| 985 | bool contact_valid, hover_event; |
| 986 | |
| 987 | pm_wakeup_event(dev: &data->client->dev, msec: 0); |
| 988 | |
| 989 | hover_event = hover_info & BIT(6); |
| 990 | |
| 991 | for (i = 0; i < ETP_MAX_FINGERS; i++) { |
| 992 | contact_valid = tp_info & BIT(3 + i); |
| 993 | elan_report_contact(data, contact_num: i, contact_valid, high_precision, |
| 994 | packet, finger_data); |
| 995 | if (contact_valid) |
| 996 | finger_data += ETP_FINGER_DATA_LEN; |
| 997 | } |
| 998 | |
| 999 | input_report_key(dev: input, BTN_LEFT, value: tp_info & BIT(0)); |
| 1000 | input_report_key(dev: input, BTN_MIDDLE, value: tp_info & BIT(2)); |
| 1001 | input_report_key(dev: input, BTN_RIGHT, value: tp_info & BIT(1)); |
| 1002 | input_report_abs(dev: input, ABS_DISTANCE, value: hover_event != 0); |
| 1003 | input_mt_report_pointer_emulation(dev: input, use_count: true); |
| 1004 | input_sync(dev: input); |
| 1005 | } |
| 1006 | |
| 1007 | static void elan_report_trackpoint(struct elan_tp_data *data, u8 *report) |
| 1008 | { |
| 1009 | struct input_dev *input = data->tp_input; |
| 1010 | u8 *packet = &report[ETP_REPORT_ID_OFFSET + 1]; |
| 1011 | int x, y; |
| 1012 | |
| 1013 | pm_wakeup_event(dev: &data->client->dev, msec: 0); |
| 1014 | |
| 1015 | if (!data->tp_input) { |
| 1016 | dev_warn_once(&data->client->dev, |
| 1017 | "received a trackpoint report while no trackpoint device has been created. Please report upstream.\n" ); |
| 1018 | return; |
| 1019 | } |
| 1020 | |
| 1021 | input_report_key(dev: input, BTN_LEFT, value: packet[0] & 0x01); |
| 1022 | input_report_key(dev: input, BTN_RIGHT, value: packet[0] & 0x02); |
| 1023 | input_report_key(dev: input, BTN_MIDDLE, value: packet[0] & 0x04); |
| 1024 | |
| 1025 | if ((packet[3] & 0x0F) == 0x06) { |
| 1026 | x = packet[4] - (int)((packet[1] ^ 0x80) << 1); |
| 1027 | y = (int)((packet[2] ^ 0x80) << 1) - packet[5]; |
| 1028 | |
| 1029 | input_report_rel(dev: input, REL_X, value: x); |
| 1030 | input_report_rel(dev: input, REL_Y, value: y); |
| 1031 | } |
| 1032 | |
| 1033 | input_sync(dev: input); |
| 1034 | } |
| 1035 | |
| 1036 | static irqreturn_t elan_isr(int irq, void *dev_id) |
| 1037 | { |
| 1038 | struct elan_tp_data *data = dev_id; |
| 1039 | int error; |
| 1040 | u8 report[ETP_MAX_REPORT_LEN]; |
| 1041 | |
| 1042 | /* |
| 1043 | * When device is connected to i2c bus, when all IAP page writes |
| 1044 | * complete, the driver will receive interrupt and must read |
| 1045 | * 0000 to confirm that IAP is finished. |
| 1046 | */ |
| 1047 | if (data->in_fw_update) { |
| 1048 | complete(&data->fw_completion); |
| 1049 | goto out; |
| 1050 | } |
| 1051 | |
| 1052 | error = data->ops->get_report(data->client, report, data->report_len); |
| 1053 | if (error) |
| 1054 | goto out; |
| 1055 | |
| 1056 | switch (report[ETP_REPORT_ID_OFFSET]) { |
| 1057 | case ETP_REPORT_ID: |
| 1058 | elan_report_absolute(data, packet: report, high_precision: false); |
| 1059 | break; |
| 1060 | case ETP_REPORT_ID2: |
| 1061 | elan_report_absolute(data, packet: report, high_precision: true); |
| 1062 | break; |
| 1063 | case ETP_TP_REPORT_ID: |
| 1064 | case ETP_TP_REPORT_ID2: |
| 1065 | elan_report_trackpoint(data, report); |
| 1066 | break; |
| 1067 | default: |
| 1068 | dev_err(&data->client->dev, "invalid report id data (%x)\n" , |
| 1069 | report[ETP_REPORT_ID_OFFSET]); |
| 1070 | } |
| 1071 | |
| 1072 | out: |
| 1073 | return IRQ_HANDLED; |
| 1074 | } |
| 1075 | |
| 1076 | /* |
| 1077 | ****************************************************************** |
| 1078 | * Elan initialization functions |
| 1079 | ****************************************************************** |
| 1080 | */ |
| 1081 | |
| 1082 | static int elan_setup_trackpoint_input_device(struct elan_tp_data *data) |
| 1083 | { |
| 1084 | struct device *dev = &data->client->dev; |
| 1085 | struct input_dev *input; |
| 1086 | |
| 1087 | input = devm_input_allocate_device(dev); |
| 1088 | if (!input) |
| 1089 | return -ENOMEM; |
| 1090 | |
| 1091 | input->name = "Elan TrackPoint" ; |
| 1092 | input->id.bustype = BUS_I2C; |
| 1093 | input->id.vendor = ELAN_VENDOR_ID; |
| 1094 | input->id.product = data->product_id; |
| 1095 | input_set_drvdata(dev: input, data); |
| 1096 | |
| 1097 | input_set_capability(dev: input, EV_REL, REL_X); |
| 1098 | input_set_capability(dev: input, EV_REL, REL_Y); |
| 1099 | input_set_capability(dev: input, EV_KEY, BTN_LEFT); |
| 1100 | input_set_capability(dev: input, EV_KEY, BTN_RIGHT); |
| 1101 | input_set_capability(dev: input, EV_KEY, BTN_MIDDLE); |
| 1102 | |
| 1103 | __set_bit(INPUT_PROP_POINTER, input->propbit); |
| 1104 | __set_bit(INPUT_PROP_POINTING_STICK, input->propbit); |
| 1105 | |
| 1106 | data->tp_input = input; |
| 1107 | |
| 1108 | return 0; |
| 1109 | } |
| 1110 | |
| 1111 | static int elan_setup_input_device(struct elan_tp_data *data) |
| 1112 | { |
| 1113 | struct device *dev = &data->client->dev; |
| 1114 | struct input_dev *input; |
| 1115 | unsigned int max_width = max(data->width_x, data->width_y); |
| 1116 | unsigned int min_width = min(data->width_x, data->width_y); |
| 1117 | int error; |
| 1118 | |
| 1119 | input = devm_input_allocate_device(dev); |
| 1120 | if (!input) |
| 1121 | return -ENOMEM; |
| 1122 | |
| 1123 | input->name = "Elan Touchpad" ; |
| 1124 | input->id.bustype = BUS_I2C; |
| 1125 | input->id.vendor = ELAN_VENDOR_ID; |
| 1126 | input->id.product = data->product_id; |
| 1127 | input_set_drvdata(dev: input, data); |
| 1128 | |
| 1129 | error = input_mt_init_slots(dev: input, ETP_MAX_FINGERS, |
| 1130 | INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED); |
| 1131 | if (error) { |
| 1132 | dev_err(dev, "failed to initialize MT slots: %d\n" , error); |
| 1133 | return error; |
| 1134 | } |
| 1135 | |
| 1136 | __set_bit(EV_ABS, input->evbit); |
| 1137 | __set_bit(INPUT_PROP_POINTER, input->propbit); |
| 1138 | if (data->clickpad) { |
| 1139 | __set_bit(INPUT_PROP_BUTTONPAD, input->propbit); |
| 1140 | } else { |
| 1141 | __set_bit(BTN_RIGHT, input->keybit); |
| 1142 | if (data->middle_button) |
| 1143 | __set_bit(BTN_MIDDLE, input->keybit); |
| 1144 | } |
| 1145 | __set_bit(BTN_LEFT, input->keybit); |
| 1146 | |
| 1147 | /* Set up ST parameters */ |
| 1148 | input_set_abs_params(dev: input, ABS_X, min: 0, max: data->max_x, fuzz: 0, flat: 0); |
| 1149 | input_set_abs_params(dev: input, ABS_Y, min: 0, max: data->max_y, fuzz: 0, flat: 0); |
| 1150 | input_abs_set_res(dev: input, ABS_X, val: data->x_res); |
| 1151 | input_abs_set_res(dev: input, ABS_Y, val: data->y_res); |
| 1152 | input_set_abs_params(dev: input, ABS_PRESSURE, min: 0, ETP_MAX_PRESSURE, fuzz: 0, flat: 0); |
| 1153 | if (data->report_features & ETP_FEATURE_REPORT_MK) |
| 1154 | input_set_abs_params(dev: input, ABS_TOOL_WIDTH, |
| 1155 | min: 0, ETP_FINGER_WIDTH, fuzz: 0, flat: 0); |
| 1156 | input_set_abs_params(dev: input, ABS_DISTANCE, min: 0, max: 1, fuzz: 0, flat: 0); |
| 1157 | |
| 1158 | /* And MT parameters */ |
| 1159 | input_set_abs_params(dev: input, ABS_MT_POSITION_X, min: 0, max: data->max_x, fuzz: 0, flat: 0); |
| 1160 | input_set_abs_params(dev: input, ABS_MT_POSITION_Y, min: 0, max: data->max_y, fuzz: 0, flat: 0); |
| 1161 | input_abs_set_res(dev: input, ABS_MT_POSITION_X, val: data->x_res); |
| 1162 | input_abs_set_res(dev: input, ABS_MT_POSITION_Y, val: data->y_res); |
| 1163 | input_set_abs_params(dev: input, ABS_MT_PRESSURE, min: 0, |
| 1164 | ETP_MAX_PRESSURE, fuzz: 0, flat: 0); |
| 1165 | if (data->report_features & ETP_FEATURE_REPORT_MK) { |
| 1166 | input_set_abs_params(dev: input, ABS_MT_TOUCH_MAJOR, |
| 1167 | min: 0, ETP_FINGER_WIDTH * max_width, fuzz: 0, flat: 0); |
| 1168 | input_set_abs_params(dev: input, ABS_MT_TOUCH_MINOR, |
| 1169 | min: 0, ETP_FINGER_WIDTH * min_width, fuzz: 0, flat: 0); |
| 1170 | } |
| 1171 | |
| 1172 | data->input = input; |
| 1173 | |
| 1174 | return 0; |
| 1175 | } |
| 1176 | |
| 1177 | static void elan_disable_regulator(void *_data) |
| 1178 | { |
| 1179 | struct elan_tp_data *data = _data; |
| 1180 | |
| 1181 | regulator_disable(regulator: data->vcc); |
| 1182 | } |
| 1183 | |
| 1184 | static int elan_probe(struct i2c_client *client) |
| 1185 | { |
| 1186 | const struct elan_transport_ops *transport_ops; |
| 1187 | struct device *dev = &client->dev; |
| 1188 | struct elan_tp_data *data; |
| 1189 | unsigned long irqflags; |
| 1190 | int error; |
| 1191 | |
| 1192 | if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C) && |
| 1193 | i2c_check_functionality(adap: client->adapter, I2C_FUNC_I2C)) { |
| 1194 | transport_ops = &elan_i2c_ops; |
| 1195 | } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) && |
| 1196 | i2c_check_functionality(adap: client->adapter, |
| 1197 | I2C_FUNC_SMBUS_BYTE_DATA | |
| 1198 | I2C_FUNC_SMBUS_BLOCK_DATA | |
| 1199 | I2C_FUNC_SMBUS_I2C_BLOCK)) { |
| 1200 | transport_ops = &elan_smbus_ops; |
| 1201 | } else { |
| 1202 | dev_err(dev, "not a supported I2C/SMBus adapter\n" ); |
| 1203 | return -EIO; |
| 1204 | } |
| 1205 | |
| 1206 | data = devm_kzalloc(dev, size: sizeof(struct elan_tp_data), GFP_KERNEL); |
| 1207 | if (!data) |
| 1208 | return -ENOMEM; |
| 1209 | |
| 1210 | i2c_set_clientdata(client, data); |
| 1211 | |
| 1212 | data->ops = transport_ops; |
| 1213 | data->client = client; |
| 1214 | init_completion(x: &data->fw_completion); |
| 1215 | mutex_init(&data->sysfs_mutex); |
| 1216 | |
| 1217 | data->vcc = devm_regulator_get(dev, id: "vcc" ); |
| 1218 | if (IS_ERR(ptr: data->vcc)) |
| 1219 | return dev_err_probe(dev, err: PTR_ERR(ptr: data->vcc), fmt: "Failed to get 'vcc' regulator\n" ); |
| 1220 | |
| 1221 | error = regulator_enable(regulator: data->vcc); |
| 1222 | if (error) { |
| 1223 | dev_err(dev, "Failed to enable regulator: %d\n" , error); |
| 1224 | return error; |
| 1225 | } |
| 1226 | |
| 1227 | error = devm_add_action_or_reset(dev, elan_disable_regulator, data); |
| 1228 | if (error) { |
| 1229 | dev_err(dev, "Failed to add disable regulator action: %d\n" , |
| 1230 | error); |
| 1231 | return error; |
| 1232 | } |
| 1233 | |
| 1234 | /* Make sure there is something at this address */ |
| 1235 | error = i2c_smbus_read_byte(client); |
| 1236 | if (error < 0) { |
| 1237 | dev_dbg(&client->dev, "nothing at this address: %d\n" , error); |
| 1238 | return -ENXIO; |
| 1239 | } |
| 1240 | |
| 1241 | /* Initialize the touchpad. */ |
| 1242 | error = elan_initialize(data, skip_reset: false); |
| 1243 | if (error) |
| 1244 | return error; |
| 1245 | |
| 1246 | error = elan_query_device_info(data); |
| 1247 | if (error) |
| 1248 | return error; |
| 1249 | |
| 1250 | error = elan_query_device_parameters(data); |
| 1251 | if (error) |
| 1252 | return error; |
| 1253 | |
| 1254 | dev_info(dev, |
| 1255 | "Elan Touchpad: Module ID: 0x%04x, Firmware: 0x%04x, Sample: 0x%04x, IAP: 0x%04x\n" , |
| 1256 | data->product_id, |
| 1257 | data->fw_version, |
| 1258 | data->sm_version, |
| 1259 | data->iap_version); |
| 1260 | |
| 1261 | dev_dbg(dev, |
| 1262 | "Elan Touchpad Extra Information:\n" |
| 1263 | " Max ABS X,Y: %d,%d\n" |
| 1264 | " Width X,Y: %d,%d\n" |
| 1265 | " Resolution X,Y: %d,%d (dots/mm)\n" |
| 1266 | " ic type: 0x%x\n" |
| 1267 | " info pattern: 0x%x\n" , |
| 1268 | data->max_x, data->max_y, |
| 1269 | data->width_x, data->width_y, |
| 1270 | data->x_res, data->y_res, |
| 1271 | data->ic_type, data->pattern); |
| 1272 | |
| 1273 | /* Set up input device properties based on queried parameters. */ |
| 1274 | error = elan_setup_input_device(data); |
| 1275 | if (error) |
| 1276 | return error; |
| 1277 | |
| 1278 | if (device_property_read_bool(dev: &client->dev, propname: "elan,trackpoint" )) { |
| 1279 | error = elan_setup_trackpoint_input_device(data); |
| 1280 | if (error) |
| 1281 | return error; |
| 1282 | } |
| 1283 | |
| 1284 | /* |
| 1285 | * Platform code (ACPI, DTS) should normally set up interrupt |
| 1286 | * for us, but in case it did not let's fall back to using falling |
| 1287 | * edge to be compatible with older Chromebooks. |
| 1288 | */ |
| 1289 | irqflags = irq_get_trigger_type(irq: client->irq); |
| 1290 | if (!irqflags) |
| 1291 | irqflags = IRQF_TRIGGER_FALLING; |
| 1292 | |
| 1293 | error = devm_request_threaded_irq(dev, irq: client->irq, NULL, thread_fn: elan_isr, |
| 1294 | irqflags: irqflags | IRQF_ONESHOT, |
| 1295 | devname: client->name, dev_id: data); |
| 1296 | if (error) { |
| 1297 | dev_err(dev, "cannot register irq=%d\n" , client->irq); |
| 1298 | return error; |
| 1299 | } |
| 1300 | |
| 1301 | error = input_register_device(data->input); |
| 1302 | if (error) { |
| 1303 | dev_err(dev, "failed to register input device: %d\n" , error); |
| 1304 | return error; |
| 1305 | } |
| 1306 | |
| 1307 | if (data->tp_input) { |
| 1308 | error = input_register_device(data->tp_input); |
| 1309 | if (error) { |
| 1310 | dev_err(&client->dev, |
| 1311 | "failed to register TrackPoint input device: %d\n" , |
| 1312 | error); |
| 1313 | return error; |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
| 1320 | static int __elan_suspend(struct elan_tp_data *data) |
| 1321 | { |
| 1322 | struct i2c_client *client = data->client; |
| 1323 | int error; |
| 1324 | |
| 1325 | if (device_may_wakeup(dev: &client->dev)) |
| 1326 | return elan_sleep(data); |
| 1327 | |
| 1328 | /* Touchpad is not a wakeup source */ |
| 1329 | error = elan_set_power(data, on: false); |
| 1330 | if (error) |
| 1331 | return error; |
| 1332 | |
| 1333 | error = regulator_disable(regulator: data->vcc); |
| 1334 | if (error) { |
| 1335 | dev_err(&client->dev, |
| 1336 | "failed to disable regulator when suspending: %d\n" , |
| 1337 | error); |
| 1338 | /* Attempt to power the chip back up */ |
| 1339 | elan_set_power(data, on: true); |
| 1340 | return error; |
| 1341 | } |
| 1342 | |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
| 1346 | static int elan_suspend(struct device *dev) |
| 1347 | { |
| 1348 | struct i2c_client *client = to_i2c_client(dev); |
| 1349 | struct elan_tp_data *data = i2c_get_clientdata(client); |
| 1350 | int error; |
| 1351 | |
| 1352 | /* |
| 1353 | * We are taking the mutex to make sure sysfs operations are |
| 1354 | * complete before we attempt to bring the device into low[er] |
| 1355 | * power mode. |
| 1356 | */ |
| 1357 | scoped_cond_guard(mutex_intr, return -EINTR, &data->sysfs_mutex) { |
| 1358 | disable_irq(irq: client->irq); |
| 1359 | |
| 1360 | error = __elan_suspend(data); |
| 1361 | if (error) { |
| 1362 | enable_irq(irq: client->irq); |
| 1363 | return error; |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | return 0; |
| 1368 | } |
| 1369 | |
| 1370 | static int elan_resume(struct device *dev) |
| 1371 | { |
| 1372 | struct i2c_client *client = to_i2c_client(dev); |
| 1373 | struct elan_tp_data *data = i2c_get_clientdata(client); |
| 1374 | int error; |
| 1375 | |
| 1376 | if (!device_may_wakeup(dev)) { |
| 1377 | error = regulator_enable(regulator: data->vcc); |
| 1378 | if (error) { |
| 1379 | dev_err(dev, "error %d enabling regulator\n" , error); |
| 1380 | goto err; |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | error = elan_set_power(data, on: true); |
| 1385 | if (error) { |
| 1386 | dev_err(dev, "power up when resuming failed: %d\n" , error); |
| 1387 | goto err; |
| 1388 | } |
| 1389 | |
| 1390 | error = elan_initialize(data, skip_reset: data->quirks & ETP_QUIRK_QUICK_WAKEUP); |
| 1391 | if (error) |
| 1392 | dev_err(dev, "initialize when resuming failed: %d\n" , error); |
| 1393 | |
| 1394 | err: |
| 1395 | enable_irq(irq: data->client->irq); |
| 1396 | return error; |
| 1397 | } |
| 1398 | |
| 1399 | static DEFINE_SIMPLE_DEV_PM_OPS(elan_pm_ops, elan_suspend, elan_resume); |
| 1400 | |
| 1401 | static const struct i2c_device_id elan_id[] = { |
| 1402 | { DRIVER_NAME }, |
| 1403 | { } |
| 1404 | }; |
| 1405 | MODULE_DEVICE_TABLE(i2c, elan_id); |
| 1406 | |
| 1407 | #ifdef CONFIG_ACPI |
| 1408 | #include <linux/input/elan-i2c-ids.h> |
| 1409 | MODULE_DEVICE_TABLE(acpi, elan_acpi_id); |
| 1410 | #endif |
| 1411 | |
| 1412 | #ifdef CONFIG_OF |
| 1413 | static const struct of_device_id elan_of_match[] = { |
| 1414 | { .compatible = "elan,ekth3000" }, |
| 1415 | { /* sentinel */ } |
| 1416 | }; |
| 1417 | MODULE_DEVICE_TABLE(of, elan_of_match); |
| 1418 | #endif |
| 1419 | |
| 1420 | static struct i2c_driver elan_driver = { |
| 1421 | .driver = { |
| 1422 | .name = DRIVER_NAME, |
| 1423 | .pm = pm_sleep_ptr(&elan_pm_ops), |
| 1424 | .acpi_match_table = ACPI_PTR(elan_acpi_id), |
| 1425 | .of_match_table = of_match_ptr(elan_of_match), |
| 1426 | .probe_type = PROBE_PREFER_ASYNCHRONOUS, |
| 1427 | .dev_groups = elan_sysfs_groups, |
| 1428 | }, |
| 1429 | .probe = elan_probe, |
| 1430 | .id_table = elan_id, |
| 1431 | }; |
| 1432 | |
| 1433 | module_i2c_driver(elan_driver); |
| 1434 | |
| 1435 | MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>" ); |
| 1436 | MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver" ); |
| 1437 | MODULE_LICENSE("GPL" ); |
| 1438 | |