1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* Copyright (c) 2024 Intel Corporation */ 3 4 #include <linux/acpi.h> 5 #include <linux/device.h> 6 #include <linux/dma-mapping.h> 7 #include <linux/err.h> 8 #include <linux/interrupt.h> 9 #include <linux/irqreturn.h> 10 #include <linux/pci.h> 11 #include <linux/sizes.h> 12 #include <linux/pm_runtime.h> 13 14 #include <linux/gpio/consumer.h> 15 16 #include "intel-thc-dev.h" 17 #include "intel-thc-hw.h" 18 #include "intel-thc-wot.h" 19 20 #include "quicki2c-dev.h" 21 #include "quicki2c-hid.h" 22 #include "quicki2c-protocol.h" 23 24 static struct quicki2c_ddata ptl_ddata = { 25 .max_detect_size = MAX_RX_DETECT_SIZE_PTL, 26 .max_interrupt_delay = MAX_RX_INTERRUPT_DELAY, 27 }; 28 29 static struct quicki2c_ddata nvl_ddata = { 30 .max_detect_size = MAX_RX_DETECT_SIZE_NVL, 31 .max_interrupt_delay = MAX_RX_INTERRUPT_DELAY, 32 }; 33 34 /* THC QuickI2C ACPI method to get device properties */ 35 /* HIDI2C device method */ 36 static guid_t i2c_hid_guid = 37 GUID_INIT(0x3cdff6f7, 0x4267, 0x4555, 0xad, 0x05, 0xb3, 0x0a, 0x3d, 0x89, 0x38, 0xde); 38 39 /* platform method */ 40 static guid_t thc_platform_guid = 41 GUID_INIT(0x84005682, 0x5b71, 0x41a4, 0x8d, 0x66, 0x81, 0x30, 0xf7, 0x87, 0xa1, 0x38); 42 43 /* QuickI2C Wake-on-Touch GPIO resource */ 44 static const struct acpi_gpio_params wake_gpio = { 0, 0, true }; 45 46 static const struct acpi_gpio_mapping quicki2c_gpios[] = { 47 { "wake-on-touch", &wake_gpio, 1 }, 48 { } 49 }; 50 51 /** 52 * quicki2c_acpi_get_dsm_property - Query device ACPI DSM parameter 53 * @adev: Point to ACPI device 54 * @guid: ACPI method's guid 55 * @rev: ACPI method's revision 56 * @func: ACPI method's function number 57 * @type: ACPI parameter's data type 58 * @prop_buf: Point to return buffer 59 * 60 * This is a helper function for device to query its ACPI DSM parameters. 61 * 62 * Return: 0 if success or ENODEV on failure. 63 */ 64 static int quicki2c_acpi_get_dsm_property(struct acpi_device *adev, const guid_t *guid, 65 u64 rev, u64 func, acpi_object_type type, void *prop_buf) 66 { 67 acpi_handle handle = acpi_device_handle(adev); 68 union acpi_object *obj; 69 70 obj = acpi_evaluate_dsm_typed(handle, guid, rev, func, NULL, type); 71 if (!obj) { 72 acpi_handle_err(handle, 73 "Error _DSM call failed, rev: %d, func: %d, type: %d\n", 74 (int)rev, (int)func, (int)type); 75 return -ENODEV; 76 } 77 78 if (type == ACPI_TYPE_INTEGER) 79 *(u32 *)prop_buf = (u32)obj->integer.value; 80 else if (type == ACPI_TYPE_BUFFER) 81 memcpy(prop_buf, obj->buffer.pointer, obj->buffer.length); 82 83 ACPI_FREE(obj); 84 85 return 0; 86 } 87 88 /** 89 * quicki2c_acpi_get_dsd_property - Query device ACPI DSD parameter 90 * @adev: Point to ACPI device 91 * @dsd_method_name: ACPI method's property name 92 * @type: ACPI parameter's data type 93 * @prop_buf: Point to return buffer 94 * 95 * This is a helper function for device to query its ACPI DSD parameters. 96 * 97 * Return: 0 if success or ENODEV on failed. 98 */ 99 static int quicki2c_acpi_get_dsd_property(struct acpi_device *adev, acpi_string dsd_method_name, 100 acpi_object_type type, void *prop_buf) 101 { 102 acpi_handle handle = acpi_device_handle(adev); 103 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 104 union acpi_object *ret_obj; 105 acpi_status status; 106 107 status = acpi_evaluate_object(handle, dsd_method_name, NULL, &buffer); 108 if (ACPI_FAILURE(status)) { 109 acpi_handle_err(handle, 110 "Can't evaluate %s method: %d\n", dsd_method_name, status); 111 return -ENODEV; 112 } 113 114 ret_obj = buffer.pointer; 115 116 memcpy(prop_buf, ret_obj->buffer.pointer, ret_obj->buffer.length); 117 118 return 0; 119 } 120 121 /** 122 * quicki2c_get_acpi_resources - Query all QuickI2C devices' ACPI parameters 123 * @qcdev: Point to quicki2c_device structure 124 * 125 * This function gets all QuickI2C devices' ACPI resource. 126 * 127 * Return: 0 if success or error code on failure. 128 */ 129 static int quicki2c_get_acpi_resources(struct quicki2c_device *qcdev) 130 { 131 struct acpi_device *adev = ACPI_COMPANION(qcdev->dev); 132 struct quicki2c_subip_acpi_parameter i2c_param; 133 struct quicki2c_subip_acpi_config i2c_config; 134 u32 hid_desc_addr; 135 int ret = -EINVAL; 136 137 if (!adev) { 138 dev_err(qcdev->dev, "Invalid acpi device pointer\n"); 139 return ret; 140 } 141 142 qcdev->acpi_dev = adev; 143 144 ret = quicki2c_acpi_get_dsm_property(adev, &i2c_hid_guid, 145 QUICKI2C_ACPI_REVISION_NUM, 146 QUICKI2C_ACPI_FUNC_NUM_HID_DESC_ADDR, 147 ACPI_TYPE_INTEGER, 148 &hid_desc_addr); 149 if (ret) 150 return ret; 151 152 qcdev->hid_desc_addr = (u16)hid_desc_addr; 153 154 ret = quicki2c_acpi_get_dsm_property(adev, &thc_platform_guid, 155 QUICKI2C_ACPI_REVISION_NUM, 156 QUICKI2C_ACPI_FUNC_NUM_ACTIVE_LTR_VAL, 157 ACPI_TYPE_INTEGER, 158 &qcdev->active_ltr_val); 159 if (ret) 160 return ret; 161 162 ret = quicki2c_acpi_get_dsm_property(adev, &thc_platform_guid, 163 QUICKI2C_ACPI_REVISION_NUM, 164 QUICKI2C_ACPI_FUNC_NUM_LP_LTR_VAL, 165 ACPI_TYPE_INTEGER, 166 &qcdev->low_power_ltr_val); 167 if (ret) 168 return ret; 169 170 ret = quicki2c_acpi_get_dsd_property(adev, QUICKI2C_ACPI_METHOD_NAME_ICRS, 171 ACPI_TYPE_BUFFER, &i2c_param); 172 if (ret) 173 return ret; 174 175 if (i2c_param.addressing_mode != HIDI2C_ADDRESSING_MODE_7BIT) 176 return -EOPNOTSUPP; 177 178 qcdev->i2c_config.addr_mode = HIDI2C_ADDRESSING_MODE_7BIT; 179 180 qcdev->i2c_config.target_addr = i2c_param.device_address; 181 182 ret = quicki2c_acpi_get_dsd_property(adev, QUICKI2C_ACPI_METHOD_NAME_ISUB, 183 ACPI_TYPE_BUFFER, &i2c_config); 184 if (ret) 185 return ret; 186 187 if (i2c_param.connection_speed > 0 && 188 i2c_param.connection_speed <= QUICKI2C_SUBIP_STANDARD_MODE_MAX_SPEED) { 189 qcdev->i2c_config.speed = THC_I2C_STANDARD; 190 qcdev->i2c_config.scl_hcnt = (u32)i2c_config.SMHX; 191 qcdev->i2c_config.scl_lcnt = (u32)i2c_config.SMLX; 192 qcdev->i2c_config.sda_tx_hold = (u32)i2c_config.SMTD; 193 qcdev->i2c_config.sda_rx_hold = (u32)i2c_config.SMRD; 194 } else if (i2c_param.connection_speed > QUICKI2C_SUBIP_STANDARD_MODE_MAX_SPEED && 195 i2c_param.connection_speed <= QUICKI2C_SUBIP_FAST_MODE_MAX_SPEED) { 196 qcdev->i2c_config.speed = THC_I2C_FAST_AND_PLUS; 197 qcdev->i2c_config.scl_hcnt = (u32)i2c_config.FMHX; 198 qcdev->i2c_config.scl_lcnt = (u32)i2c_config.FMLX; 199 qcdev->i2c_config.sda_tx_hold = (u32)i2c_config.FMTD; 200 qcdev->i2c_config.sda_rx_hold = (u32)i2c_config.FMRD; 201 } else if (i2c_param.connection_speed > QUICKI2C_SUBIP_FAST_MODE_MAX_SPEED && 202 i2c_param.connection_speed <= QUICKI2C_SUBIP_FASTPLUS_MODE_MAX_SPEED) { 203 qcdev->i2c_config.speed = THC_I2C_FAST_AND_PLUS; 204 qcdev->i2c_config.scl_hcnt = (u32)i2c_config.FPHX; 205 qcdev->i2c_config.scl_lcnt = (u32)i2c_config.FPLX; 206 qcdev->i2c_config.sda_tx_hold = (u32)i2c_config.FPTD; 207 qcdev->i2c_config.sda_rx_hold = (u32)i2c_config.FPRD; 208 } else if (i2c_param.connection_speed > QUICKI2C_SUBIP_FASTPLUS_MODE_MAX_SPEED && 209 i2c_param.connection_speed <= QUICKI2C_SUBIP_HIGH_SPEED_MODE_MAX_SPEED) { 210 qcdev->i2c_config.speed = THC_I2C_HIGH_SPEED; 211 qcdev->i2c_config.scl_hcnt = (u32)i2c_config.HMHX; 212 qcdev->i2c_config.scl_lcnt = (u32)i2c_config.HMLX; 213 qcdev->i2c_config.sda_tx_hold = (u32)i2c_config.HMTD; 214 qcdev->i2c_config.sda_rx_hold = (u32)i2c_config.HMRD; 215 } else { 216 return -EOPNOTSUPP; 217 } 218 219 if (qcdev->ddata) { 220 qcdev->i2c_max_frame_size_enable = i2c_config.FSEN; 221 qcdev->i2c_int_delay_enable = i2c_config.INDE; 222 223 if (i2c_config.FSVL <= qcdev->ddata->max_detect_size) 224 qcdev->i2c_max_frame_size = i2c_config.FSVL; 225 else 226 qcdev->i2c_max_frame_size = qcdev->ddata->max_detect_size; 227 228 if (i2c_config.INDV <= qcdev->ddata->max_interrupt_delay) 229 qcdev->i2c_int_delay = i2c_config.INDV; 230 else 231 qcdev->i2c_int_delay = qcdev->ddata->max_interrupt_delay; 232 } 233 234 return 0; 235 } 236 237 /** 238 * quicki2c_irq_quick_handler - The ISR of the QuickI2C driver 239 * @irq: The irq number 240 * @dev_id: Pointer to the quicki2c_device structure 241 * 242 * Return: IRQ_WAKE_THREAD if further process needed. 243 */ 244 static irqreturn_t quicki2c_irq_quick_handler(int irq, void *dev_id) 245 { 246 struct quicki2c_device *qcdev = dev_id; 247 248 if (qcdev->state == QUICKI2C_DISABLED) 249 return IRQ_HANDLED; 250 251 /* Disable THC interrupt before current interrupt be handled */ 252 thc_interrupt_enable(qcdev->thc_hw, false); 253 254 return IRQ_WAKE_THREAD; 255 } 256 257 /** 258 * try_recover - Recover callback to recover THC 259 * @work: pointer to work_struct 260 * 261 * This function is an error handler, called when fatal error happens. 262 * It try to reset Touch Device and re-configure THC to recover 263 * transferring between Device and THC. 264 */ 265 static void try_recover(struct work_struct *work) 266 { 267 struct quicki2c_device *qcdev = container_of(work, struct quicki2c_device, recover_work); 268 269 if (READ_ONCE(qcdev->recovery_disabled)) 270 return; 271 272 if (pm_runtime_resume_and_get(qcdev->dev)) 273 return; 274 275 thc_interrupt_enable(qcdev->thc_hw, false); 276 277 if (thc_rxdma_reset(qcdev->thc_hw)) { 278 qcdev->state = QUICKI2C_DISABLED; 279 dev_err(qcdev->dev, "RxDMA reset failed during recover, disable QuickI2C\n"); 280 } else { 281 thc_interrupt_enable(qcdev->thc_hw, true); 282 } 283 284 pm_runtime_put_autosuspend(qcdev->dev); 285 } 286 287 static int handle_input_report(struct quicki2c_device *qcdev) 288 { 289 struct hidi2c_report_packet *pkt = (struct hidi2c_report_packet *)qcdev->input_buf; 290 int rx_dma_finished = 0; 291 size_t report_len; 292 int ret; 293 294 while (!rx_dma_finished) { 295 ret = thc_rxdma_read(qcdev->thc_hw, THC_RXDMA2, 296 (u8 *)pkt, &report_len, 297 &rx_dma_finished); 298 if (ret) 299 return ret; 300 301 if (!pkt->len) { 302 if (qcdev->state == QUICKI2C_RESETING) { 303 qcdev->reset_ack = true; 304 wake_up(&qcdev->reset_ack_wq); 305 306 qcdev->state = QUICKI2C_RESETED; 307 } else { 308 dev_warn(qcdev->dev, "unexpected DIR happen\n"); 309 } 310 311 continue; 312 } 313 314 /* Discard samples before driver probe complete */ 315 if (qcdev->state != QUICKI2C_ENABLED) 316 continue; 317 318 quicki2c_hid_send_report(qcdev, pkt->data, 319 HIDI2C_DATA_LEN(le16_to_cpu(pkt->len))); 320 } 321 322 return 0; 323 } 324 325 /** 326 * quicki2c_irq_thread_handler - IRQ thread handler of QuickI2C driver 327 * @irq: The IRQ number 328 * @dev_id: Pointer to the quicki2c_device structure 329 * 330 * Return: IRQ_HANDLED to finish this handler. 331 */ 332 static irqreturn_t quicki2c_irq_thread_handler(int irq, void *dev_id) 333 { 334 struct quicki2c_device *qcdev = dev_id; 335 int err_recover = 0; 336 int int_mask; 337 int ret; 338 339 if (qcdev->state == QUICKI2C_DISABLED) 340 return IRQ_HANDLED; 341 342 ret = pm_runtime_resume_and_get(qcdev->dev); 343 if (ret) 344 return IRQ_HANDLED; 345 346 int_mask = thc_interrupt_handler(qcdev->thc_hw); 347 348 if (int_mask & BIT(THC_FATAL_ERR_INT) || int_mask & BIT(THC_TXN_ERR_INT) || 349 int_mask & BIT(THC_UNKNOWN_INT)) { 350 err_recover = 1; 351 goto exit; 352 } 353 354 if (int_mask & BIT(THC_RXDMA2_INT)) { 355 err_recover = handle_input_report(qcdev); 356 if (err_recover) 357 goto exit; 358 } 359 360 exit: 361 if (err_recover) 362 schedule_work(&qcdev->recover_work); 363 else 364 thc_interrupt_enable(qcdev->thc_hw, true); 365 366 pm_runtime_put_autosuspend(qcdev->dev); 367 368 return IRQ_HANDLED; 369 } 370 371 /** 372 * quicki2c_dev_init - Initialize QuickI2C device 373 * @pdev: Pointer to the THC PCI device 374 * @mem_addr: The Pointer of MMIO memory address 375 * @ddata: Point to quicki2c_ddata structure 376 * 377 * Alloc quicki2c_device structure and initialized THC device, 378 * then configure THC to HIDI2C mode. 379 * 380 * If success, enable THC hardware interrupt. 381 * 382 * Return: Pointer to the quicki2c_device structure if success 383 * or NULL on failure. 384 */ 385 static struct quicki2c_device *quicki2c_dev_init(struct pci_dev *pdev, void __iomem *mem_addr, 386 const struct quicki2c_ddata *ddata) 387 { 388 struct device *dev = &pdev->dev; 389 struct quicki2c_device *qcdev; 390 int ret; 391 392 qcdev = devm_kzalloc(dev, sizeof(struct quicki2c_device), GFP_KERNEL); 393 if (!qcdev) 394 return ERR_PTR(-ENOMEM); 395 396 qcdev->pdev = pdev; 397 qcdev->dev = dev; 398 qcdev->mem_addr = mem_addr; 399 qcdev->state = QUICKI2C_DISABLED; 400 qcdev->ddata = ddata; 401 402 init_waitqueue_head(&qcdev->reset_ack_wq); 403 WRITE_ONCE(qcdev->recovery_disabled, false); 404 INIT_WORK(&qcdev->recover_work, try_recover); 405 406 /* THC hardware init */ 407 qcdev->thc_hw = thc_dev_init(qcdev->dev, qcdev->mem_addr); 408 if (IS_ERR(qcdev->thc_hw)) { 409 ret = PTR_ERR(qcdev->thc_hw); 410 dev_err_once(dev, "Failed to initialize THC device context, ret = %d.\n", ret); 411 return ERR_PTR(ret); 412 } 413 414 ret = quicki2c_get_acpi_resources(qcdev); 415 if (ret) { 416 dev_err_once(dev, "Get ACPI resources failed, ret = %d\n", ret); 417 return ERR_PTR(ret); 418 } 419 420 ret = thc_interrupt_quiesce(qcdev->thc_hw, true); 421 if (ret) 422 return ERR_PTR(ret); 423 424 ret = thc_port_select(qcdev->thc_hw, THC_PORT_TYPE_I2C); 425 if (ret) { 426 dev_err_once(dev, "Failed to select THC port, ret = %d.\n", ret); 427 return ERR_PTR(ret); 428 } 429 430 ret = thc_i2c_subip_init(qcdev->thc_hw, &qcdev->i2c_config); 431 if (ret) 432 return ERR_PTR(ret); 433 434 thc_int_trigger_type_select(qcdev->thc_hw, false); 435 436 thc_interrupt_config(qcdev->thc_hw); 437 438 thc_interrupt_enable(qcdev->thc_hw, true); 439 440 thc_wot_config(qcdev->thc_hw, &quicki2c_gpios[0]); 441 442 qcdev->state = QUICKI2C_INITED; 443 444 return qcdev; 445 } 446 447 /** 448 * quicki2c_dev_deinit - De-initialize QuickI2C device 449 * @qcdev: Pointer to the quicki2c_device structure 450 * 451 * Disable THC interrupt and deinitilize THC. 452 */ 453 static void quicki2c_dev_deinit(struct quicki2c_device *qcdev) 454 { 455 WRITE_ONCE(qcdev->recovery_disabled, true); 456 cancel_work_sync(&qcdev->recover_work); 457 458 thc_interrupt_quiesce(qcdev->thc_hw, true); 459 thc_interrupt_enable(qcdev->thc_hw, false); 460 thc_ltr_unconfig(qcdev->thc_hw); 461 thc_wot_unconfig(qcdev->thc_hw); 462 463 qcdev->state = QUICKI2C_DISABLED; 464 } 465 466 /** 467 * quicki2c_dma_adv_enable - Configure and enable DMA advanced features 468 * @qcdev: Pointer to the quicki2c_device structure 469 * 470 * If platform supports THC DMA advanced features, such as max input size 471 * control or interrupt delay, configures and enables them. 472 */ 473 static void quicki2c_dma_adv_enable(struct quicki2c_device *qcdev) 474 { 475 /* 476 * If platform supports max input size control feature and touch device 477 * max input length <= THC detect capability, enable the feature with device 478 * max input length. 479 */ 480 if (qcdev->i2c_max_frame_size_enable) { 481 if (qcdev->i2c_max_frame_size >= 482 le16_to_cpu(qcdev->dev_desc.max_input_len)) { 483 thc_i2c_set_rx_max_size(qcdev->thc_hw, 484 le16_to_cpu(qcdev->dev_desc.max_input_len)); 485 } else { 486 dev_warn(qcdev->dev, 487 "Max frame size is smaller than hid max input length!"); 488 thc_i2c_set_rx_max_size(qcdev->thc_hw, 489 qcdev->i2c_max_frame_size); 490 } 491 thc_i2c_rx_max_size_enable(qcdev->thc_hw, true); 492 } 493 494 /* If platform supports interrupt delay feature, enable it with given delay */ 495 if (qcdev->i2c_int_delay_enable) { 496 thc_i2c_set_rx_int_delay(qcdev->thc_hw, 497 qcdev->i2c_int_delay * 10); 498 thc_i2c_rx_int_delay_enable(qcdev->thc_hw, true); 499 } 500 } 501 502 /** 503 * quicki2c_dma_adv_disable - Disable DMA advanced features 504 * @qcdev: Pointer to the quicki2c device structure 505 * 506 * Disable all DMA advanced features if platform supports. 507 */ 508 static void quicki2c_dma_adv_disable(struct quicki2c_device *qcdev) 509 { 510 if (qcdev->i2c_max_frame_size_enable) 511 thc_i2c_rx_max_size_enable(qcdev->thc_hw, false); 512 513 if (qcdev->i2c_int_delay_enable) 514 thc_i2c_rx_int_delay_enable(qcdev->thc_hw, false); 515 } 516 517 /** 518 * quicki2c_dma_init - Configure THC DMA for QuickI2C device 519 * @qcdev: Pointer to the quicki2c_device structure 520 * 521 * This function uses TIC's parameters(such as max input length, max output 522 * length) to allocate THC DMA buffers and configure THC DMA engines. 523 * 524 * Return: 0 if success or error code on failure. 525 */ 526 static int quicki2c_dma_init(struct quicki2c_device *qcdev) 527 { 528 size_t swdma_max_len; 529 int ret; 530 531 swdma_max_len = max(le16_to_cpu(qcdev->dev_desc.max_input_len), 532 le16_to_cpu(qcdev->dev_desc.report_desc_len)); 533 534 ret = thc_dma_set_max_packet_sizes(qcdev->thc_hw, 0, 535 le16_to_cpu(qcdev->dev_desc.max_input_len), 536 le16_to_cpu(qcdev->dev_desc.max_output_len), 537 swdma_max_len); 538 if (ret) 539 return ret; 540 541 ret = thc_dma_allocate(qcdev->thc_hw); 542 if (ret) { 543 dev_err(qcdev->dev, "Allocate THC DMA buffer failed, ret = %d\n", ret); 544 return ret; 545 } 546 547 /* Enable RxDMA */ 548 ret = thc_dma_configure(qcdev->thc_hw); 549 if (ret) { 550 dev_err(qcdev->dev, "Configure THC DMA failed, ret = %d\n", ret); 551 thc_dma_unconfigure(qcdev->thc_hw); 552 thc_dma_release(qcdev->thc_hw); 553 return ret; 554 } 555 556 if (qcdev->ddata) 557 quicki2c_dma_adv_enable(qcdev); 558 559 return 0; 560 } 561 562 /** 563 * quicki2c_dma_deinit - Release THC DMA for QuickI2C device 564 * @qcdev: Pointer to the quicki2c_device structure 565 * 566 * Stop THC DMA engines and release all DMA buffers. 567 * 568 */ 569 static void quicki2c_dma_deinit(struct quicki2c_device *qcdev) 570 { 571 thc_dma_unconfigure(qcdev->thc_hw); 572 thc_dma_release(qcdev->thc_hw); 573 574 if (qcdev->ddata) 575 quicki2c_dma_adv_disable(qcdev); 576 } 577 578 /** 579 * quicki2c_alloc_report_buf - Alloc report buffers 580 * @qcdev: Pointer to the quicki2c_device structure 581 * 582 * Allocate report descriptor buffer, it will be used for restore TIC HID 583 * report descriptor. 584 * 585 * Allocate input report buffer, it will be used for receive HID input report 586 * data from TIC. 587 * 588 * Allocate output report buffer, it will be used for store HID output report, 589 * such as set feature. 590 * 591 * Return: 0 if success or error code on failure. 592 */ 593 static int quicki2c_alloc_report_buf(struct quicki2c_device *qcdev) 594 { 595 size_t max_report_len; 596 597 qcdev->report_descriptor = devm_kzalloc(qcdev->dev, 598 le16_to_cpu(qcdev->dev_desc.report_desc_len), 599 GFP_KERNEL); 600 if (!qcdev->report_descriptor) 601 return -ENOMEM; 602 603 /* 604 * Some HIDI2C devices don't declare input/output max length correctly, 605 * give default 4K buffer to avoid DMA buffer overrun. 606 */ 607 max_report_len = max(le16_to_cpu(qcdev->dev_desc.max_input_len), SZ_4K); 608 609 qcdev->input_buf = devm_kzalloc(qcdev->dev, max_report_len, GFP_KERNEL); 610 if (!qcdev->input_buf) 611 return -ENOMEM; 612 613 if (!le16_to_cpu(qcdev->dev_desc.max_output_len)) 614 qcdev->dev_desc.max_output_len = cpu_to_le16(SZ_4K); 615 616 max_report_len = max(le16_to_cpu(qcdev->dev_desc.max_output_len), 617 max_report_len); 618 619 qcdev->report_buf = devm_kzalloc(qcdev->dev, max_report_len, GFP_KERNEL); 620 if (!qcdev->report_buf) 621 return -ENOMEM; 622 623 qcdev->report_len = max_report_len; 624 625 return 0; 626 } 627 628 /* 629 * quicki2c_probe: QuickI2C driver probe function 630 * @pdev: Point to PCI device 631 * @id: Point to pci_device_id structure 632 * 633 * This function initializes THC and HIDI2C device, the flow is: 634 * - Do THC pci device initialization 635 * - Query HIDI2C ACPI parameters 636 * - Configure THC to HIDI2C mode 637 * - Go through HIDI2C enumeration flow 638 * |- Read device descriptor 639 * |- Reset HIDI2C device 640 * - Enable THC interrupt and DMA 641 * - Read report descriptor 642 * - Register HID device 643 * - Enable runtime power management 644 * 645 * Return 0 if success or error code on failure. 646 */ 647 static int quicki2c_probe(struct pci_dev *pdev, const struct pci_device_id *id) 648 { 649 const struct quicki2c_ddata *ddata = (const struct quicki2c_ddata *)id->driver_data; 650 struct quicki2c_device *qcdev; 651 void __iomem *mem_addr; 652 int ret; 653 654 ret = pcim_enable_device(pdev); 655 if (ret) { 656 dev_err_once(&pdev->dev, "Failed to enable PCI device, ret = %d.\n", ret); 657 return ret; 658 } 659 660 pci_set_master(pdev); 661 662 mem_addr = pcim_iomap_region(pdev, 0, KBUILD_MODNAME); 663 ret = PTR_ERR_OR_ZERO(mem_addr); 664 if (ret) { 665 dev_err_once(&pdev->dev, "Failed to get PCI regions, ret = %d.\n", ret); 666 goto disable_pci_device; 667 } 668 669 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 670 if (ret) { 671 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 672 if (ret) { 673 dev_err_once(&pdev->dev, "No usable DMA configuration %d\n", ret); 674 goto disable_pci_device; 675 } 676 } 677 678 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES); 679 if (ret < 0) { 680 dev_err_once(&pdev->dev, 681 "Failed to allocate IRQ vectors. ret = %d\n", ret); 682 goto disable_pci_device; 683 } 684 685 pdev->irq = pci_irq_vector(pdev, 0); 686 687 qcdev = quicki2c_dev_init(pdev, mem_addr, ddata); 688 if (IS_ERR(qcdev)) { 689 dev_err_once(&pdev->dev, "QuickI2C device init failed\n"); 690 ret = PTR_ERR(qcdev); 691 goto disable_pci_device; 692 } 693 694 pci_set_drvdata(pdev, qcdev); 695 696 ret = devm_request_threaded_irq(&pdev->dev, pdev->irq, 697 quicki2c_irq_quick_handler, 698 quicki2c_irq_thread_handler, 699 IRQF_ONESHOT, KBUILD_MODNAME, 700 qcdev); 701 if (ret) 702 goto dev_deinit; 703 704 ret = quicki2c_get_device_descriptor(qcdev); 705 if (ret) { 706 dev_err(&pdev->dev, "Get device descriptor failed, ret = %d\n", ret); 707 goto dev_deinit; 708 } 709 710 ret = quicki2c_alloc_report_buf(qcdev); 711 if (ret) { 712 dev_err(&pdev->dev, "Alloc report buffers failed, ret= %d\n", ret); 713 goto dev_deinit; 714 } 715 716 ret = quicki2c_dma_init(qcdev); 717 if (ret) { 718 dev_err(&pdev->dev, "Setup THC DMA failed, ret= %d\n", ret); 719 goto dev_deinit; 720 } 721 722 ret = thc_interrupt_quiesce(qcdev->thc_hw, false); 723 if (ret) 724 goto dev_deinit; 725 726 ret = quicki2c_set_power(qcdev, HIDI2C_ON); 727 if (ret) { 728 dev_err(&pdev->dev, "Set Power On command failed, ret= %d\n", ret); 729 goto dev_deinit; 730 } 731 732 ret = quicki2c_reset(qcdev); 733 if (ret) { 734 dev_err(&pdev->dev, "Reset HIDI2C device failed, ret= %d\n", ret); 735 goto dev_deinit; 736 } 737 738 ret = quicki2c_get_report_descriptor(qcdev); 739 if (ret) { 740 dev_err(&pdev->dev, "Get report descriptor failed, ret = %d\n", ret); 741 goto dma_deinit; 742 } 743 744 ret = quicki2c_hid_probe(qcdev); 745 if (ret) { 746 dev_err(&pdev->dev, "Failed to register HID device, ret = %d\n", ret); 747 goto dma_deinit; 748 } 749 750 qcdev->state = QUICKI2C_ENABLED; 751 752 /* Enable runtime power management */ 753 pm_runtime_use_autosuspend(qcdev->dev); 754 pm_runtime_set_autosuspend_delay(qcdev->dev, DEFAULT_AUTO_SUSPEND_DELAY_MS); 755 pm_runtime_put_noidle(qcdev->dev); 756 pm_runtime_put_autosuspend(qcdev->dev); 757 758 dev_dbg(&pdev->dev, "QuickI2C probe success\n"); 759 760 return 0; 761 762 dma_deinit: 763 quicki2c_dma_deinit(qcdev); 764 dev_deinit: 765 quicki2c_dev_deinit(qcdev); 766 disable_pci_device: 767 pci_clear_master(pdev); 768 769 return ret; 770 } 771 772 /** 773 * quicki2c_remove - Device Removal Routine 774 * @pdev: Point to PCI device structure 775 * 776 * This is called by the PCI subsystem to alert the driver that it should 777 * release a PCI device. 778 */ 779 static void quicki2c_remove(struct pci_dev *pdev) 780 { 781 struct quicki2c_device *qcdev; 782 783 qcdev = pci_get_drvdata(pdev); 784 if (!qcdev) 785 return; 786 787 quicki2c_hid_remove(qcdev); 788 789 quicki2c_dev_deinit(qcdev); 790 791 quicki2c_dma_deinit(qcdev); 792 793 pm_runtime_dont_use_autosuspend(qcdev->dev); 794 pm_runtime_get_noresume(qcdev->dev); 795 796 pci_clear_master(pdev); 797 } 798 799 /** 800 * quicki2c_shutdown - Device Shutdown Routine 801 * @pdev: Point to PCI device structure 802 * 803 * This is called from the reboot notifier, it's a simplified version of remove 804 * so we go down faster. 805 */ 806 static void quicki2c_shutdown(struct pci_dev *pdev) 807 { 808 struct quicki2c_device *qcdev; 809 810 qcdev = pci_get_drvdata(pdev); 811 if (!qcdev) 812 return; 813 814 quicki2c_dev_deinit(qcdev); 815 816 /* Must stop DMA before reboot to avoid DMA entering into unknown state */ 817 quicki2c_dma_deinit(qcdev); 818 } 819 820 static int quicki2c_suspend(struct device *device) 821 { 822 struct pci_dev *pdev = to_pci_dev(device); 823 struct quicki2c_device *qcdev; 824 int ret; 825 826 qcdev = pci_get_drvdata(pdev); 827 if (!qcdev) 828 return -ENODEV; 829 830 if (!device_may_wakeup(qcdev->dev)) { 831 ret = quicki2c_set_power(qcdev, HIDI2C_SLEEP); 832 if (ret) 833 return ret; 834 } 835 836 /* 837 * As I2C is THC subsystem, no register auto save/restore support, 838 * need driver to do that explicitly for every D3 case. 839 */ 840 ret = thc_i2c_subip_regs_save(qcdev->thc_hw); 841 if (ret) 842 return ret; 843 844 WRITE_ONCE(qcdev->recovery_disabled, true); 845 cancel_work_sync(&qcdev->recover_work); 846 847 ret = thc_interrupt_quiesce(qcdev->thc_hw, true); 848 if (ret) 849 return ret; 850 851 thc_interrupt_enable(qcdev->thc_hw, false); 852 853 thc_dma_unconfigure(qcdev->thc_hw); 854 855 return 0; 856 } 857 858 static int quicki2c_resume(struct device *device) 859 { 860 struct pci_dev *pdev = to_pci_dev(device); 861 struct quicki2c_device *qcdev; 862 int ret; 863 864 qcdev = pci_get_drvdata(pdev); 865 if (!qcdev) 866 return -ENODEV; 867 868 ret = thc_port_select(qcdev->thc_hw, THC_PORT_TYPE_I2C); 869 if (ret) 870 return ret; 871 872 ret = thc_i2c_subip_regs_restore(qcdev->thc_hw); 873 if (ret) 874 return ret; 875 876 thc_interrupt_config(qcdev->thc_hw); 877 878 thc_interrupt_enable(qcdev->thc_hw, true); 879 880 ret = thc_dma_configure(qcdev->thc_hw); 881 if (ret) 882 return ret; 883 884 ret = thc_interrupt_quiesce(qcdev->thc_hw, false); 885 if (ret) 886 return ret; 887 888 WRITE_ONCE(qcdev->recovery_disabled, false); 889 890 if (!device_may_wakeup(qcdev->dev)) 891 return quicki2c_set_power(qcdev, HIDI2C_ON); 892 893 return 0; 894 } 895 896 static int quicki2c_freeze(struct device *device) 897 { 898 struct pci_dev *pdev = to_pci_dev(device); 899 struct quicki2c_device *qcdev; 900 int ret; 901 902 qcdev = pci_get_drvdata(pdev); 903 if (!qcdev) 904 return -ENODEV; 905 906 WRITE_ONCE(qcdev->recovery_disabled, true); 907 cancel_work_sync(&qcdev->recover_work); 908 909 ret = thc_interrupt_quiesce(qcdev->thc_hw, true); 910 if (ret) 911 return ret; 912 913 thc_interrupt_enable(qcdev->thc_hw, false); 914 915 thc_dma_unconfigure(qcdev->thc_hw); 916 917 return 0; 918 } 919 920 static int quicki2c_thaw(struct device *device) 921 { 922 struct pci_dev *pdev = to_pci_dev(device); 923 struct quicki2c_device *qcdev; 924 int ret; 925 926 qcdev = pci_get_drvdata(pdev); 927 if (!qcdev) 928 return -ENODEV; 929 930 ret = thc_dma_configure(qcdev->thc_hw); 931 if (ret) 932 return ret; 933 934 thc_interrupt_enable(qcdev->thc_hw, true); 935 936 ret = thc_interrupt_quiesce(qcdev->thc_hw, false); 937 if (ret) 938 return ret; 939 940 WRITE_ONCE(qcdev->recovery_disabled, false); 941 942 return 0; 943 } 944 945 static int quicki2c_poweroff(struct device *device) 946 { 947 struct pci_dev *pdev = to_pci_dev(device); 948 struct quicki2c_device *qcdev; 949 int ret; 950 951 qcdev = pci_get_drvdata(pdev); 952 if (!qcdev) 953 return -ENODEV; 954 955 /* Ignore the return value as platform will be poweroff soon */ 956 quicki2c_set_power(qcdev, HIDI2C_SLEEP); 957 958 ret = thc_interrupt_quiesce(qcdev->thc_hw, true); 959 if (ret) 960 return ret; 961 962 thc_interrupt_enable(qcdev->thc_hw, false); 963 964 thc_ltr_unconfig(qcdev->thc_hw); 965 966 quicki2c_dev_deinit(qcdev); 967 968 quicki2c_dma_deinit(qcdev); 969 970 return 0; 971 } 972 973 static int quicki2c_restore(struct device *device) 974 { 975 struct pci_dev *pdev = to_pci_dev(device); 976 struct quicki2c_device *qcdev; 977 int ret; 978 979 qcdev = pci_get_drvdata(pdev); 980 if (!qcdev) 981 return -ENODEV; 982 983 /* Reconfig THC HW when back from hibernate */ 984 ret = thc_port_select(qcdev->thc_hw, THC_PORT_TYPE_I2C); 985 if (ret) 986 return ret; 987 988 ret = thc_i2c_subip_init(qcdev->thc_hw, &qcdev->i2c_config); 989 if (ret) 990 return ret; 991 992 thc_interrupt_config(qcdev->thc_hw); 993 994 thc_interrupt_enable(qcdev->thc_hw, true); 995 996 ret = thc_interrupt_quiesce(qcdev->thc_hw, false); 997 if (ret) 998 return ret; 999 1000 ret = thc_dma_configure(qcdev->thc_hw); 1001 if (ret) 1002 return ret; 1003 1004 thc_ltr_config(qcdev->thc_hw, 1005 qcdev->active_ltr_val, 1006 qcdev->low_power_ltr_val); 1007 1008 thc_change_ltr_mode(qcdev->thc_hw, THC_LTR_MODE_ACTIVE); 1009 1010 return quicki2c_set_power(qcdev, HIDI2C_ON); 1011 } 1012 1013 static int quicki2c_runtime_suspend(struct device *device) 1014 { 1015 struct pci_dev *pdev = to_pci_dev(device); 1016 struct quicki2c_device *qcdev; 1017 1018 qcdev = pci_get_drvdata(pdev); 1019 if (!qcdev) 1020 return -ENODEV; 1021 1022 thc_change_ltr_mode(qcdev->thc_hw, THC_LTR_MODE_LP); 1023 1024 pci_save_state(pdev); 1025 1026 return 0; 1027 } 1028 1029 static int quicki2c_runtime_resume(struct device *device) 1030 { 1031 struct pci_dev *pdev = to_pci_dev(device); 1032 struct quicki2c_device *qcdev; 1033 1034 qcdev = pci_get_drvdata(pdev); 1035 if (!qcdev) 1036 return -ENODEV; 1037 1038 thc_change_ltr_mode(qcdev->thc_hw, THC_LTR_MODE_ACTIVE); 1039 1040 return 0; 1041 } 1042 1043 static const struct dev_pm_ops quicki2c_pm_ops = { 1044 .suspend = quicki2c_suspend, 1045 .resume = quicki2c_resume, 1046 .freeze = quicki2c_freeze, 1047 .thaw = quicki2c_thaw, 1048 .poweroff = quicki2c_poweroff, 1049 .restore = quicki2c_restore, 1050 .runtime_suspend = quicki2c_runtime_suspend, 1051 .runtime_resume = quicki2c_runtime_resume, 1052 .runtime_idle = NULL, 1053 }; 1054 1055 static const struct pci_device_id quicki2c_pci_tbl[] = { 1056 { PCI_DEVICE_DATA(INTEL, THC_LNL_DEVICE_ID_I2C_PORT1, NULL) }, 1057 { PCI_DEVICE_DATA(INTEL, THC_LNL_DEVICE_ID_I2C_PORT2, NULL) }, 1058 { PCI_DEVICE_DATA(INTEL, THC_PTL_H_DEVICE_ID_I2C_PORT1, &ptl_ddata) }, 1059 { PCI_DEVICE_DATA(INTEL, THC_PTL_H_DEVICE_ID_I2C_PORT2, &ptl_ddata) }, 1060 { PCI_DEVICE_DATA(INTEL, THC_PTL_U_DEVICE_ID_I2C_PORT1, &ptl_ddata) }, 1061 { PCI_DEVICE_DATA(INTEL, THC_PTL_U_DEVICE_ID_I2C_PORT2, &ptl_ddata) }, 1062 { PCI_DEVICE_DATA(INTEL, THC_WCL_DEVICE_ID_I2C_PORT1, &ptl_ddata) }, 1063 { PCI_DEVICE_DATA(INTEL, THC_WCL_DEVICE_ID_I2C_PORT2, &ptl_ddata) }, 1064 { PCI_DEVICE_DATA(INTEL, THC_NVL_H_DEVICE_ID_I2C_PORT1, &nvl_ddata) }, 1065 { PCI_DEVICE_DATA(INTEL, THC_NVL_H_DEVICE_ID_I2C_PORT2, &nvl_ddata) }, 1066 { } 1067 }; 1068 MODULE_DEVICE_TABLE(pci, quicki2c_pci_tbl); 1069 1070 static struct pci_driver quicki2c_driver = { 1071 .name = KBUILD_MODNAME, 1072 .id_table = quicki2c_pci_tbl, 1073 .probe = quicki2c_probe, 1074 .remove = quicki2c_remove, 1075 .shutdown = quicki2c_shutdown, 1076 .driver.pm = &quicki2c_pm_ops, 1077 .driver.probe_type = PROBE_PREFER_ASYNCHRONOUS, 1078 }; 1079 1080 module_pci_driver(quicki2c_driver); 1081 1082 MODULE_AUTHOR("Xinpeng Sun <xinpeng.sun@intel.com>"); 1083 MODULE_AUTHOR("Even Xu <even.xu@intel.com>"); 1084 1085 MODULE_DESCRIPTION("Intel(R) QuickI2C Driver"); 1086 MODULE_LICENSE("GPL"); 1087 MODULE_IMPORT_NS("INTEL_THC"); 1088