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