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_mark_last_busy(qcdev->dev); 348 pm_runtime_put_autosuspend(qcdev->dev); 349 350 return IRQ_HANDLED; 351 } 352 353 /** 354 * quicki2c_dev_init - Initialize QuickI2C device 355 * @pdev: Pointer to the THC PCI device 356 * @mem_addr: The Pointer of MMIO memory address 357 * @ddata: Point to quicki2c_ddata structure 358 * 359 * Alloc quicki2c_device structure and initialized THC device, 360 * then configure THC to HIDI2C mode. 361 * 362 * If success, enable THC hardware interrupt. 363 * 364 * Return: Pointer to the quicki2c_device structure if success 365 * or NULL on failure. 366 */ 367 static struct quicki2c_device *quicki2c_dev_init(struct pci_dev *pdev, void __iomem *mem_addr, 368 const struct quicki2c_ddata *ddata) 369 { 370 struct device *dev = &pdev->dev; 371 struct quicki2c_device *qcdev; 372 int ret; 373 374 qcdev = devm_kzalloc(dev, sizeof(struct quicki2c_device), GFP_KERNEL); 375 if (!qcdev) 376 return ERR_PTR(-ENOMEM); 377 378 qcdev->pdev = pdev; 379 qcdev->dev = dev; 380 qcdev->mem_addr = mem_addr; 381 qcdev->state = QUICKI2C_DISABLED; 382 qcdev->ddata = ddata; 383 384 init_waitqueue_head(&qcdev->reset_ack_wq); 385 386 /* THC hardware init */ 387 qcdev->thc_hw = thc_dev_init(qcdev->dev, qcdev->mem_addr); 388 if (IS_ERR(qcdev->thc_hw)) { 389 ret = PTR_ERR(qcdev->thc_hw); 390 dev_err_once(dev, "Failed to initialize THC device context, ret = %d.\n", ret); 391 return ERR_PTR(ret); 392 } 393 394 ret = quicki2c_get_acpi_resources(qcdev); 395 if (ret) { 396 dev_err_once(dev, "Get ACPI resources failed, ret = %d\n", ret); 397 return ERR_PTR(ret); 398 } 399 400 ret = thc_interrupt_quiesce(qcdev->thc_hw, true); 401 if (ret) 402 return ERR_PTR(ret); 403 404 ret = thc_port_select(qcdev->thc_hw, THC_PORT_TYPE_I2C); 405 if (ret) { 406 dev_err_once(dev, "Failed to select THC port, ret = %d.\n", ret); 407 return ERR_PTR(ret); 408 } 409 410 ret = thc_i2c_subip_init(qcdev->thc_hw, qcdev->i2c_slave_addr, 411 qcdev->i2c_speed_mode, 412 qcdev->i2c_clock_hcnt, 413 qcdev->i2c_clock_lcnt); 414 if (ret) 415 return ERR_PTR(ret); 416 417 thc_int_trigger_type_select(qcdev->thc_hw, false); 418 419 thc_interrupt_config(qcdev->thc_hw); 420 421 thc_interrupt_enable(qcdev->thc_hw, true); 422 423 thc_wot_config(qcdev->thc_hw, &quicki2c_gpios[0]); 424 425 qcdev->state = QUICKI2C_INITED; 426 427 return qcdev; 428 } 429 430 /** 431 * quicki2c_dev_deinit - De-initialize QuickI2C device 432 * @qcdev: Pointer to the quicki2c_device structure 433 * 434 * Disable THC interrupt and deinitilize THC. 435 */ 436 static void quicki2c_dev_deinit(struct quicki2c_device *qcdev) 437 { 438 thc_interrupt_quiesce(qcdev->thc_hw, true); 439 thc_interrupt_enable(qcdev->thc_hw, false); 440 thc_ltr_unconfig(qcdev->thc_hw); 441 thc_wot_unconfig(qcdev->thc_hw); 442 443 qcdev->state = QUICKI2C_DISABLED; 444 } 445 446 /** 447 * quicki2c_dma_adv_enable - Configure and enable DMA advanced features 448 * @qcdev: Pointer to the quicki2c_device structure 449 * 450 * If platform supports THC DMA advanced features, such as max input size 451 * control or interrupt delay, configures and enables them. 452 */ 453 static void quicki2c_dma_adv_enable(struct quicki2c_device *qcdev) 454 { 455 /* 456 * If platform supports max input size control feature and touch device 457 * max input length <= THC detect capability, enable the feature with device 458 * max input length. 459 */ 460 if (qcdev->i2c_max_frame_size_enable) { 461 if (qcdev->i2c_max_frame_size >= 462 le16_to_cpu(qcdev->dev_desc.max_input_len)) { 463 thc_i2c_set_rx_max_size(qcdev->thc_hw, 464 le16_to_cpu(qcdev->dev_desc.max_input_len)); 465 } else { 466 dev_warn(qcdev->dev, 467 "Max frame size is smaller than hid max input length!"); 468 thc_i2c_set_rx_max_size(qcdev->thc_hw, 469 le16_to_cpu(qcdev->i2c_max_frame_size)); 470 } 471 thc_i2c_rx_max_size_enable(qcdev->thc_hw, true); 472 } 473 474 /* If platform supports interrupt delay feature, enable it with given delay */ 475 if (qcdev->i2c_int_delay_enable) { 476 thc_i2c_set_rx_int_delay(qcdev->thc_hw, 477 qcdev->i2c_int_delay * 10); 478 thc_i2c_rx_int_delay_enable(qcdev->thc_hw, true); 479 } 480 } 481 482 /** 483 * quicki2c_dma_adv_disable - Disable DMA advanced features 484 * @qcdev: Pointer to the quicki2c device structure 485 * 486 * Disable all DMA advanced features if platform supports. 487 */ 488 static void quicki2c_dma_adv_disable(struct quicki2c_device *qcdev) 489 { 490 if (qcdev->i2c_max_frame_size_enable) 491 thc_i2c_rx_max_size_enable(qcdev->thc_hw, false); 492 493 if (qcdev->i2c_int_delay_enable) 494 thc_i2c_rx_int_delay_enable(qcdev->thc_hw, false); 495 } 496 497 /** 498 * quicki2c_dma_init - Configure THC DMA for QuickI2C device 499 * @qcdev: Pointer to the quicki2c_device structure 500 * 501 * This function uses TIC's parameters(such as max input length, max output 502 * length) to allocate THC DMA buffers and configure THC DMA engines. 503 * 504 * Return: 0 if success or error code on failure. 505 */ 506 static int quicki2c_dma_init(struct quicki2c_device *qcdev) 507 { 508 size_t swdma_max_len; 509 int ret; 510 511 swdma_max_len = max(le16_to_cpu(qcdev->dev_desc.max_input_len), 512 le16_to_cpu(qcdev->dev_desc.report_desc_len)); 513 514 ret = thc_dma_set_max_packet_sizes(qcdev->thc_hw, 0, 515 le16_to_cpu(qcdev->dev_desc.max_input_len), 516 le16_to_cpu(qcdev->dev_desc.max_output_len), 517 swdma_max_len); 518 if (ret) 519 return ret; 520 521 ret = thc_dma_allocate(qcdev->thc_hw); 522 if (ret) { 523 dev_err(qcdev->dev, "Allocate THC DMA buffer failed, ret = %d\n", ret); 524 return ret; 525 } 526 527 /* Enable RxDMA */ 528 ret = thc_dma_configure(qcdev->thc_hw); 529 if (ret) { 530 dev_err(qcdev->dev, "Configure THC DMA failed, ret = %d\n", ret); 531 thc_dma_unconfigure(qcdev->thc_hw); 532 thc_dma_release(qcdev->thc_hw); 533 return ret; 534 } 535 536 if (qcdev->ddata) 537 quicki2c_dma_adv_enable(qcdev); 538 539 return 0; 540 } 541 542 /** 543 * quicki2c_dma_deinit - Release THC DMA for QuickI2C device 544 * @qcdev: Pointer to the quicki2c_device structure 545 * 546 * Stop THC DMA engines and release all DMA buffers. 547 * 548 */ 549 static void quicki2c_dma_deinit(struct quicki2c_device *qcdev) 550 { 551 thc_dma_unconfigure(qcdev->thc_hw); 552 thc_dma_release(qcdev->thc_hw); 553 554 if (qcdev->ddata) 555 quicki2c_dma_adv_disable(qcdev); 556 } 557 558 /** 559 * quicki2c_alloc_report_buf - Alloc report buffers 560 * @qcdev: Pointer to the quicki2c_device structure 561 * 562 * Allocate report descriptor buffer, it will be used for restore TIC HID 563 * report descriptor. 564 * 565 * Allocate input report buffer, it will be used for receive HID input report 566 * data from TIC. 567 * 568 * Allocate output report buffer, it will be used for store HID output report, 569 * such as set feature. 570 * 571 * Return: 0 if success or error code on failure. 572 */ 573 static int quicki2c_alloc_report_buf(struct quicki2c_device *qcdev) 574 { 575 size_t max_report_len; 576 577 qcdev->report_descriptor = devm_kzalloc(qcdev->dev, 578 le16_to_cpu(qcdev->dev_desc.report_desc_len), 579 GFP_KERNEL); 580 if (!qcdev->report_descriptor) 581 return -ENOMEM; 582 583 /* 584 * Some HIDI2C devices don't declare input/output max length correctly, 585 * give default 4K buffer to avoid DMA buffer overrun. 586 */ 587 max_report_len = max(le16_to_cpu(qcdev->dev_desc.max_input_len), SZ_4K); 588 589 qcdev->input_buf = devm_kzalloc(qcdev->dev, max_report_len, GFP_KERNEL); 590 if (!qcdev->input_buf) 591 return -ENOMEM; 592 593 if (!le16_to_cpu(qcdev->dev_desc.max_output_len)) 594 qcdev->dev_desc.max_output_len = cpu_to_le16(SZ_4K); 595 596 max_report_len = max(le16_to_cpu(qcdev->dev_desc.max_output_len), 597 max_report_len); 598 599 qcdev->report_buf = devm_kzalloc(qcdev->dev, max_report_len, GFP_KERNEL); 600 if (!qcdev->report_buf) 601 return -ENOMEM; 602 603 qcdev->report_len = max_report_len; 604 605 return 0; 606 } 607 608 /* 609 * quicki2c_probe: QuickI2C driver probe function 610 * @pdev: Point to PCI device 611 * @id: Point to pci_device_id structure 612 * 613 * This function initializes THC and HIDI2C device, the flow is: 614 * - Do THC pci device initialization 615 * - Query HIDI2C ACPI parameters 616 * - Configure THC to HIDI2C mode 617 * - Go through HIDI2C enumeration flow 618 * |- Read device descriptor 619 * |- Reset HIDI2C device 620 * - Enable THC interrupt and DMA 621 * - Read report descriptor 622 * - Register HID device 623 * - Enable runtime power management 624 * 625 * Return 0 if success or error code on failure. 626 */ 627 static int quicki2c_probe(struct pci_dev *pdev, const struct pci_device_id *id) 628 { 629 const struct quicki2c_ddata *ddata = (const struct quicki2c_ddata *)id->driver_data; 630 struct quicki2c_device *qcdev; 631 void __iomem *mem_addr; 632 int ret; 633 634 ret = pcim_enable_device(pdev); 635 if (ret) { 636 dev_err_once(&pdev->dev, "Failed to enable PCI device, ret = %d.\n", ret); 637 return ret; 638 } 639 640 pci_set_master(pdev); 641 642 mem_addr = pcim_iomap_region(pdev, 0, KBUILD_MODNAME); 643 ret = PTR_ERR_OR_ZERO(mem_addr); 644 if (ret) { 645 dev_err_once(&pdev->dev, "Failed to get PCI regions, ret = %d.\n", ret); 646 goto disable_pci_device; 647 } 648 649 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 650 if (ret) { 651 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 652 if (ret) { 653 dev_err_once(&pdev->dev, "No usable DMA configuration %d\n", ret); 654 goto disable_pci_device; 655 } 656 } 657 658 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES); 659 if (ret < 0) { 660 dev_err_once(&pdev->dev, 661 "Failed to allocate IRQ vectors. ret = %d\n", ret); 662 goto disable_pci_device; 663 } 664 665 pdev->irq = pci_irq_vector(pdev, 0); 666 667 qcdev = quicki2c_dev_init(pdev, mem_addr, ddata); 668 if (IS_ERR(qcdev)) { 669 dev_err_once(&pdev->dev, "QuickI2C device init failed\n"); 670 ret = PTR_ERR(qcdev); 671 goto disable_pci_device; 672 } 673 674 pci_set_drvdata(pdev, qcdev); 675 676 ret = devm_request_threaded_irq(&pdev->dev, pdev->irq, 677 quicki2c_irq_quick_handler, 678 quicki2c_irq_thread_handler, 679 IRQF_ONESHOT, KBUILD_MODNAME, 680 qcdev); 681 if (ret) { 682 dev_err_once(&pdev->dev, 683 "Failed to request threaded IRQ, irq = %d.\n", pdev->irq); 684 goto dev_deinit; 685 } 686 687 ret = quicki2c_get_device_descriptor(qcdev); 688 if (ret) { 689 dev_err(&pdev->dev, "Get device descriptor failed, ret = %d\n", ret); 690 goto dev_deinit; 691 } 692 693 ret = quicki2c_alloc_report_buf(qcdev); 694 if (ret) { 695 dev_err(&pdev->dev, "Alloc report buffers failed, ret= %d\n", ret); 696 goto dev_deinit; 697 } 698 699 ret = quicki2c_dma_init(qcdev); 700 if (ret) { 701 dev_err(&pdev->dev, "Setup THC DMA failed, ret= %d\n", ret); 702 goto dev_deinit; 703 } 704 705 ret = thc_interrupt_quiesce(qcdev->thc_hw, false); 706 if (ret) 707 goto dev_deinit; 708 709 ret = quicki2c_set_power(qcdev, HIDI2C_ON); 710 if (ret) { 711 dev_err(&pdev->dev, "Set Power On command failed, ret= %d\n", ret); 712 goto dev_deinit; 713 } 714 715 ret = quicki2c_reset(qcdev); 716 if (ret) { 717 dev_err(&pdev->dev, "Reset HIDI2C device failed, ret= %d\n", ret); 718 goto dev_deinit; 719 } 720 721 ret = quicki2c_get_report_descriptor(qcdev); 722 if (ret) { 723 dev_err(&pdev->dev, "Get report descriptor failed, ret = %d\n", ret); 724 goto dma_deinit; 725 } 726 727 ret = quicki2c_hid_probe(qcdev); 728 if (ret) { 729 dev_err(&pdev->dev, "Failed to register HID device, ret = %d\n", ret); 730 goto dma_deinit; 731 } 732 733 qcdev->state = QUICKI2C_ENABLED; 734 735 /* Enable runtime power management */ 736 pm_runtime_use_autosuspend(qcdev->dev); 737 pm_runtime_set_autosuspend_delay(qcdev->dev, DEFAULT_AUTO_SUSPEND_DELAY_MS); 738 pm_runtime_mark_last_busy(qcdev->dev); 739 pm_runtime_put_noidle(qcdev->dev); 740 pm_runtime_put_autosuspend(qcdev->dev); 741 742 dev_dbg(&pdev->dev, "QuickI2C probe success\n"); 743 744 return 0; 745 746 dma_deinit: 747 quicki2c_dma_deinit(qcdev); 748 dev_deinit: 749 quicki2c_dev_deinit(qcdev); 750 disable_pci_device: 751 pci_clear_master(pdev); 752 753 return ret; 754 } 755 756 /** 757 * quicki2c_remove - Device Removal Routine 758 * @pdev: Point to PCI device structure 759 * 760 * This is called by the PCI subsystem to alert the driver that it should 761 * release a PCI device. 762 */ 763 static void quicki2c_remove(struct pci_dev *pdev) 764 { 765 struct quicki2c_device *qcdev; 766 767 qcdev = pci_get_drvdata(pdev); 768 if (!qcdev) 769 return; 770 771 quicki2c_hid_remove(qcdev); 772 quicki2c_dma_deinit(qcdev); 773 774 pm_runtime_get_noresume(qcdev->dev); 775 776 quicki2c_dev_deinit(qcdev); 777 778 pci_clear_master(pdev); 779 } 780 781 /** 782 * quicki2c_shutdown - Device Shutdown Routine 783 * @pdev: Point to PCI device structure 784 * 785 * This is called from the reboot notifier, it's a simplified version of remove 786 * so we go down faster. 787 */ 788 static void quicki2c_shutdown(struct pci_dev *pdev) 789 { 790 struct quicki2c_device *qcdev; 791 792 qcdev = pci_get_drvdata(pdev); 793 if (!qcdev) 794 return; 795 796 /* Must stop DMA before reboot to avoid DMA entering into unknown state */ 797 quicki2c_dma_deinit(qcdev); 798 799 quicki2c_dev_deinit(qcdev); 800 } 801 802 static int quicki2c_suspend(struct device *device) 803 { 804 struct pci_dev *pdev = to_pci_dev(device); 805 struct quicki2c_device *qcdev; 806 int ret; 807 808 qcdev = pci_get_drvdata(pdev); 809 if (!qcdev) 810 return -ENODEV; 811 812 /* 813 * As I2C is THC subsystem, no register auto save/restore support, 814 * need driver to do that explicitly for every D3 case. 815 */ 816 ret = thc_i2c_subip_regs_save(qcdev->thc_hw); 817 if (ret) 818 return ret; 819 820 ret = thc_interrupt_quiesce(qcdev->thc_hw, true); 821 if (ret) 822 return ret; 823 824 thc_interrupt_enable(qcdev->thc_hw, false); 825 826 thc_dma_unconfigure(qcdev->thc_hw); 827 828 return 0; 829 } 830 831 static int quicki2c_resume(struct device *device) 832 { 833 struct pci_dev *pdev = to_pci_dev(device); 834 struct quicki2c_device *qcdev; 835 int ret; 836 837 qcdev = pci_get_drvdata(pdev); 838 if (!qcdev) 839 return -ENODEV; 840 841 ret = thc_port_select(qcdev->thc_hw, THC_PORT_TYPE_I2C); 842 if (ret) 843 return ret; 844 845 ret = thc_i2c_subip_regs_restore(qcdev->thc_hw); 846 if (ret) 847 return ret; 848 849 thc_interrupt_config(qcdev->thc_hw); 850 851 thc_interrupt_enable(qcdev->thc_hw, true); 852 853 ret = thc_dma_configure(qcdev->thc_hw); 854 if (ret) 855 return ret; 856 857 ret = thc_interrupt_quiesce(qcdev->thc_hw, false); 858 if (ret) 859 return ret; 860 861 return 0; 862 } 863 864 static int quicki2c_freeze(struct device *device) 865 { 866 struct pci_dev *pdev = to_pci_dev(device); 867 struct quicki2c_device *qcdev; 868 int ret; 869 870 qcdev = pci_get_drvdata(pdev); 871 if (!qcdev) 872 return -ENODEV; 873 874 ret = thc_interrupt_quiesce(qcdev->thc_hw, true); 875 if (ret) 876 return ret; 877 878 thc_interrupt_enable(qcdev->thc_hw, false); 879 880 thc_dma_unconfigure(qcdev->thc_hw); 881 882 return 0; 883 } 884 885 static int quicki2c_thaw(struct device *device) 886 { 887 struct pci_dev *pdev = to_pci_dev(device); 888 struct quicki2c_device *qcdev; 889 int ret; 890 891 qcdev = pci_get_drvdata(pdev); 892 if (!qcdev) 893 return -ENODEV; 894 895 ret = thc_dma_configure(qcdev->thc_hw); 896 if (ret) 897 return ret; 898 899 thc_interrupt_enable(qcdev->thc_hw, true); 900 901 ret = thc_interrupt_quiesce(qcdev->thc_hw, false); 902 if (ret) 903 return ret; 904 905 return 0; 906 } 907 908 static int quicki2c_poweroff(struct device *device) 909 { 910 struct pci_dev *pdev = to_pci_dev(device); 911 struct quicki2c_device *qcdev; 912 int ret; 913 914 qcdev = pci_get_drvdata(pdev); 915 if (!qcdev) 916 return -ENODEV; 917 918 ret = thc_interrupt_quiesce(qcdev->thc_hw, true); 919 if (ret) 920 return ret; 921 922 thc_interrupt_enable(qcdev->thc_hw, false); 923 924 thc_ltr_unconfig(qcdev->thc_hw); 925 926 quicki2c_dma_deinit(qcdev); 927 928 return 0; 929 } 930 931 static int quicki2c_restore(struct device *device) 932 { 933 struct pci_dev *pdev = to_pci_dev(device); 934 struct quicki2c_device *qcdev; 935 int ret; 936 937 qcdev = pci_get_drvdata(pdev); 938 if (!qcdev) 939 return -ENODEV; 940 941 /* Reconfig THC HW when back from hibernate */ 942 ret = thc_port_select(qcdev->thc_hw, THC_PORT_TYPE_I2C); 943 if (ret) 944 return ret; 945 946 ret = thc_i2c_subip_init(qcdev->thc_hw, qcdev->i2c_slave_addr, 947 qcdev->i2c_speed_mode, 948 qcdev->i2c_clock_hcnt, 949 qcdev->i2c_clock_lcnt); 950 if (ret) 951 return ret; 952 953 thc_interrupt_config(qcdev->thc_hw); 954 955 thc_interrupt_enable(qcdev->thc_hw, true); 956 957 ret = thc_interrupt_quiesce(qcdev->thc_hw, false); 958 if (ret) 959 return ret; 960 961 ret = thc_dma_configure(qcdev->thc_hw); 962 if (ret) 963 return ret; 964 965 thc_ltr_config(qcdev->thc_hw, 966 qcdev->active_ltr_val, 967 qcdev->low_power_ltr_val); 968 969 thc_change_ltr_mode(qcdev->thc_hw, THC_LTR_MODE_ACTIVE); 970 971 return 0; 972 } 973 974 static int quicki2c_runtime_suspend(struct device *device) 975 { 976 struct pci_dev *pdev = to_pci_dev(device); 977 struct quicki2c_device *qcdev; 978 979 qcdev = pci_get_drvdata(pdev); 980 if (!qcdev) 981 return -ENODEV; 982 983 thc_change_ltr_mode(qcdev->thc_hw, THC_LTR_MODE_LP); 984 985 pci_save_state(pdev); 986 987 return 0; 988 } 989 990 static int quicki2c_runtime_resume(struct device *device) 991 { 992 struct pci_dev *pdev = to_pci_dev(device); 993 struct quicki2c_device *qcdev; 994 995 qcdev = pci_get_drvdata(pdev); 996 if (!qcdev) 997 return -ENODEV; 998 999 thc_change_ltr_mode(qcdev->thc_hw, THC_LTR_MODE_ACTIVE); 1000 1001 return 0; 1002 } 1003 1004 static const struct dev_pm_ops quicki2c_pm_ops = { 1005 .suspend = quicki2c_suspend, 1006 .resume = quicki2c_resume, 1007 .freeze = quicki2c_freeze, 1008 .thaw = quicki2c_thaw, 1009 .poweroff = quicki2c_poweroff, 1010 .restore = quicki2c_restore, 1011 .runtime_suspend = quicki2c_runtime_suspend, 1012 .runtime_resume = quicki2c_runtime_resume, 1013 .runtime_idle = NULL, 1014 }; 1015 1016 static const struct pci_device_id quicki2c_pci_tbl[] = { 1017 { PCI_DEVICE_DATA(INTEL, THC_LNL_DEVICE_ID_I2C_PORT1, NULL) }, 1018 { PCI_DEVICE_DATA(INTEL, THC_LNL_DEVICE_ID_I2C_PORT2, NULL) }, 1019 { PCI_DEVICE_DATA(INTEL, THC_PTL_H_DEVICE_ID_I2C_PORT1, &ptl_ddata) }, 1020 { PCI_DEVICE_DATA(INTEL, THC_PTL_H_DEVICE_ID_I2C_PORT2, &ptl_ddata) }, 1021 { PCI_DEVICE_DATA(INTEL, THC_PTL_U_DEVICE_ID_I2C_PORT1, &ptl_ddata) }, 1022 { PCI_DEVICE_DATA(INTEL, THC_PTL_U_DEVICE_ID_I2C_PORT2, &ptl_ddata) }, 1023 { PCI_DEVICE_DATA(INTEL, THC_WCL_DEVICE_ID_I2C_PORT1, &ptl_ddata) }, 1024 { PCI_DEVICE_DATA(INTEL, THC_WCL_DEVICE_ID_I2C_PORT2, &ptl_ddata) }, 1025 { } 1026 }; 1027 MODULE_DEVICE_TABLE(pci, quicki2c_pci_tbl); 1028 1029 static struct pci_driver quicki2c_driver = { 1030 .name = KBUILD_MODNAME, 1031 .id_table = quicki2c_pci_tbl, 1032 .probe = quicki2c_probe, 1033 .remove = quicki2c_remove, 1034 .shutdown = quicki2c_shutdown, 1035 .driver.pm = &quicki2c_pm_ops, 1036 .driver.probe_type = PROBE_PREFER_ASYNCHRONOUS, 1037 }; 1038 1039 module_pci_driver(quicki2c_driver); 1040 1041 MODULE_AUTHOR("Xinpeng Sun <xinpeng.sun@intel.com>"); 1042 MODULE_AUTHOR("Even Xu <even.xu@intel.com>"); 1043 1044 MODULE_DESCRIPTION("Intel(R) QuickI2C Driver"); 1045 MODULE_LICENSE("GPL"); 1046 MODULE_IMPORT_NS("INTEL_THC"); 1047