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