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