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