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