1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2024 Intel Corporation */
3 
4 #include <linux/acpi.h>
5 #include <linux/bitfield.h>
6 #include <linux/device.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/err.h>
9 #include <linux/interrupt.h>
10 #include <linux/irqreturn.h>
11 #include <linux/pci.h>
12 #include <linux/pm_runtime.h>
13 
14 #include "intel-thc-dev.h"
15 #include "intel-thc-hw.h"
16 
17 #include "quickspi-dev.h"
18 #include "quickspi-hid.h"
19 #include "quickspi-protocol.h"
20 
21 struct quickspi_driver_data mtl = {
22 	.max_packet_size_value = MAX_PACKET_SIZE_VALUE_MTL,
23 };
24 
25 struct quickspi_driver_data lnl = {
26 	.max_packet_size_value = MAX_PACKET_SIZE_VALUE_LNL,
27 };
28 
29 struct quickspi_driver_data ptl = {
30 	.max_packet_size_value = MAX_PACKET_SIZE_VALUE_LNL,
31 };
32 
33 /* THC QuickSPI ACPI method to get device properties */
34 /* HIDSPI Method: {6e2ac436-0fcf-41af-a265-b32a220dcfab} */
35 static guid_t hidspi_guid =
36 	GUID_INIT(0x6e2ac436, 0x0fcf, 0x41af, 0xa2, 0x65, 0xb3, 0x2a,
37 		  0x22, 0x0d, 0xcf, 0xab);
38 
39 /* QuickSpi Method: {300D35b7-ac20-413e-8e9c-92e4dafd0afe} */
40 static guid_t thc_quickspi_guid =
41 	GUID_INIT(0x300d35b7, 0xac20, 0x413e, 0x8e, 0x9c, 0x92, 0xe4,
42 		  0xda, 0xfd, 0x0a, 0xfe);
43 
44 /* Platform Method: {84005682-5b71-41a4-0x8d668130f787a138} */
45 static guid_t thc_platform_guid =
46 	GUID_INIT(0x84005682, 0x5b71, 0x41a4, 0x8d, 0x66, 0x81, 0x30,
47 		  0xf7, 0x87, 0xa1, 0x38);
48 
49 /**
50  * thc_acpi_get_property - Query device ACPI parameter
51  *
52  * @adev: point to ACPI device
53  * @guid: ACPI method's guid
54  * @rev: ACPI method's revision
55  * @func: ACPI method's function number
56  * @type: ACPI parameter's data type
57  * @prop_buf: point to return buffer
58  *
59  * This is a helper function for device to query its ACPI parameters.
60  *
61  * Return: 0 if successful or ENODEV on failed.
62  */
thc_acpi_get_property(struct acpi_device * adev,const guid_t * guid,u64 rev,u64 func,acpi_object_type type,void * prop_buf)63 static int thc_acpi_get_property(struct acpi_device *adev, const guid_t *guid,
64 				 u64 rev, u64 func, acpi_object_type type, void *prop_buf)
65 {
66 	acpi_handle handle = acpi_device_handle(adev);
67 	union acpi_object *obj;
68 
69 	obj = acpi_evaluate_dsm_typed(handle, guid, rev, func, NULL, type);
70 	if (!obj) {
71 		acpi_handle_err(handle,
72 				"Error _DSM call failed, rev: %llu, func: %llu, type: %u\n",
73 				rev, func, type);
74 		return -ENODEV;
75 	}
76 
77 	if (type == ACPI_TYPE_INTEGER)
78 		*(u32 *)prop_buf = (u32)obj->integer.value;
79 	else if (type == ACPI_TYPE_BUFFER)
80 		memcpy(prop_buf, obj->buffer.pointer, obj->buffer.length);
81 
82 	ACPI_FREE(obj);
83 
84 	return 0;
85 }
86 
87 /**
88  * quickspi_get_acpi_resources - Query all quickspi devices' ACPI parameters
89  *
90  * @qsdev: point to quickspi device
91  *
92  * This function gets all quickspi devices' ACPI resource.
93  *
94  * Return: 0 if successful or error code on failed.
95  */
quickspi_get_acpi_resources(struct quickspi_device * qsdev)96 static int quickspi_get_acpi_resources(struct quickspi_device *qsdev)
97 {
98 	struct acpi_device *adev = ACPI_COMPANION(qsdev->dev);
99 	int ret = -EINVAL;
100 
101 	if (!adev) {
102 		dev_err(qsdev->dev, "no valid ACPI companion\n");
103 		return ret;
104 	}
105 
106 	qsdev->acpi_dev = adev;
107 
108 	ret = thc_acpi_get_property(adev, &hidspi_guid,
109 				    ACPI_QUICKSPI_REVISION_NUM,
110 				    ACPI_QUICKSPI_FUNC_NUM_INPUT_REP_HDR_ADDR,
111 				    ACPI_TYPE_INTEGER,
112 				    &qsdev->input_report_hdr_addr);
113 	if (ret)
114 		return ret;
115 
116 	ret = thc_acpi_get_property(adev, &hidspi_guid,
117 				    ACPI_QUICKSPI_REVISION_NUM,
118 				    ACPI_QUICKSPI_FUNC_NUM_INPUT_REP_BDY_ADDR,
119 				    ACPI_TYPE_INTEGER,
120 				    &qsdev->input_report_bdy_addr);
121 	if (ret)
122 		return ret;
123 
124 	ret = thc_acpi_get_property(adev, &hidspi_guid,
125 				    ACPI_QUICKSPI_REVISION_NUM,
126 				    ACPI_QUICKSPI_FUNC_NUM_OUTPUT_REP_ADDR,
127 				    ACPI_TYPE_INTEGER,
128 				    &qsdev->output_report_addr);
129 	if (ret)
130 		return ret;
131 
132 	ret = thc_acpi_get_property(adev, &hidspi_guid,
133 				    ACPI_QUICKSPI_REVISION_NUM,
134 				    ACPI_QUICKSPI_FUNC_NUM_READ_OPCODE,
135 				    ACPI_TYPE_BUFFER,
136 				    &qsdev->spi_read_opcode);
137 	if (ret)
138 		return ret;
139 
140 	ret = thc_acpi_get_property(adev, &hidspi_guid,
141 				    ACPI_QUICKSPI_REVISION_NUM,
142 				    ACPI_QUICKSPI_FUNC_NUM_WRITE_OPCODE,
143 				    ACPI_TYPE_BUFFER,
144 				    &qsdev->spi_write_opcode);
145 	if (ret)
146 		return ret;
147 
148 	ret = thc_acpi_get_property(adev, &hidspi_guid,
149 				    ACPI_QUICKSPI_REVISION_NUM,
150 				    ACPI_QUICKSPI_FUNC_NUM_IO_MODE,
151 				    ACPI_TYPE_INTEGER,
152 				    &qsdev->spi_read_io_mode);
153 	if (ret)
154 		return ret;
155 
156 	if (qsdev->spi_read_io_mode & SPI_WRITE_IO_MODE)
157 		qsdev->spi_write_io_mode = FIELD_GET(SPI_IO_MODE_OPCODE, qsdev->spi_read_io_mode);
158 	else
159 		qsdev->spi_write_io_mode = THC_SINGLE_IO;
160 
161 	qsdev->spi_read_io_mode = FIELD_GET(SPI_IO_MODE_OPCODE, qsdev->spi_read_io_mode);
162 
163 	ret = thc_acpi_get_property(adev, &thc_quickspi_guid,
164 				    ACPI_QUICKSPI_REVISION_NUM,
165 				    ACPI_QUICKSPI_FUNC_NUM_CONNECTION_SPEED,
166 				    ACPI_TYPE_INTEGER,
167 				    &qsdev->spi_freq_val);
168 	if (ret)
169 		return ret;
170 
171 	ret = thc_acpi_get_property(adev, &thc_quickspi_guid,
172 				    ACPI_QUICKSPI_REVISION_NUM,
173 				    ACPI_QUICKSPI_FUNC_NUM_LIMIT_PACKET_SIZE,
174 				    ACPI_TYPE_INTEGER,
175 				    &qsdev->limit_packet_size);
176 	if (ret)
177 		return ret;
178 
179 	if (qsdev->limit_packet_size || !qsdev->driver_data)
180 		qsdev->spi_packet_size = DEFAULT_MIN_PACKET_SIZE_VALUE;
181 	else
182 		qsdev->spi_packet_size = qsdev->driver_data->max_packet_size_value;
183 
184 	ret = thc_acpi_get_property(adev, &thc_quickspi_guid,
185 				    ACPI_QUICKSPI_REVISION_NUM,
186 				    ACPI_QUICKSPI_FUNC_NUM_PERFORMANCE_LIMIT,
187 				    ACPI_TYPE_INTEGER,
188 				    &qsdev->performance_limit);
189 	if (ret)
190 		return ret;
191 
192 	qsdev->performance_limit = FIELD_GET(PERFORMANCE_LIMITATION, qsdev->performance_limit);
193 
194 	ret = thc_acpi_get_property(adev, &thc_platform_guid,
195 				    ACPI_QUICKSPI_REVISION_NUM,
196 				    ACPI_QUICKSPI_FUNC_NUM_ACTIVE_LTR,
197 				    ACPI_TYPE_INTEGER,
198 				    &qsdev->active_ltr_val);
199 	if (ret)
200 		return ret;
201 
202 	ret = thc_acpi_get_property(adev, &thc_platform_guid,
203 				    ACPI_QUICKSPI_REVISION_NUM,
204 				    ACPI_QUICKSPI_FUNC_NUM_LP_LTR,
205 				    ACPI_TYPE_INTEGER,
206 				    &qsdev->low_power_ltr_val);
207 	if (ret)
208 		return ret;
209 
210 	return 0;
211 }
212 
213 /**
214  * quickspi_irq_quick_handler - The ISR of the quickspi driver
215  *
216  * @irq: The irq number
217  * @dev_id: pointer to the device structure
218  *
219  * Return: IRQ_WAKE_THREAD if further process needed.
220  */
quickspi_irq_quick_handler(int irq,void * dev_id)221 static irqreturn_t quickspi_irq_quick_handler(int irq, void *dev_id)
222 {
223 	struct quickspi_device *qsdev = dev_id;
224 
225 	if (qsdev->state == QUICKSPI_DISABLED)
226 		return IRQ_HANDLED;
227 
228 	/* Disable THC interrupt before current interrupt be handled */
229 	thc_interrupt_enable(qsdev->thc_hw, false);
230 
231 	return IRQ_WAKE_THREAD;
232 }
233 
234 /**
235  * try_recover - Try to recovery THC and Device
236  * @qsdev: pointer to quickspi device
237  *
238  * This function is a error handler, called when fatal error happens.
239  * It try to reset Touch Device and re-configure THC to recovery
240  * transferring between Device and THC.
241  *
242  * Return: 0 if successful or error code on failed.
243  */
try_recover(struct quickspi_device * qsdev)244 static int try_recover(struct quickspi_device *qsdev)
245 {
246 	int ret;
247 
248 	ret = reset_tic(qsdev);
249 	if (ret) {
250 		dev_err(qsdev->dev, "Reset touch device failed, ret = %d\n", ret);
251 		return ret;
252 	}
253 
254 	thc_dma_unconfigure(qsdev->thc_hw);
255 
256 	ret = thc_dma_configure(qsdev->thc_hw);
257 	if (ret) {
258 		dev_err(qsdev->dev, "Re-configure THC DMA failed, ret = %d\n", ret);
259 		return ret;
260 	}
261 
262 	return 0;
263 }
264 
265 /**
266  * quickspi_irq_thread_handler - IRQ thread handler of quickspi driver
267  *
268  * @irq: The IRQ number
269  * @dev_id: pointer to the quickspi device structure
270  *
271  * Return: IRQ_HANDLED to finish this handler.
272  */
quickspi_irq_thread_handler(int irq,void * dev_id)273 static irqreturn_t quickspi_irq_thread_handler(int irq, void *dev_id)
274 {
275 	struct quickspi_device *qsdev = dev_id;
276 	size_t input_len;
277 	int read_finished = 0;
278 	int err_recover = 0;
279 	int int_mask;
280 	int ret;
281 
282 	if (qsdev->state == QUICKSPI_DISABLED)
283 		return IRQ_HANDLED;
284 
285 	ret = pm_runtime_resume_and_get(qsdev->dev);
286 	if (ret)
287 		return IRQ_HANDLED;
288 
289 	int_mask = thc_interrupt_handler(qsdev->thc_hw);
290 
291 	if (int_mask & BIT(THC_FATAL_ERR_INT) || int_mask & BIT(THC_TXN_ERR_INT)) {
292 		err_recover = 1;
293 		goto end;
294 	}
295 
296 	if (int_mask & BIT(THC_NONDMA_INT)) {
297 		if (qsdev->state == QUICKSPI_RESETING) {
298 			qsdev->reset_ack = true;
299 			wake_up_interruptible(&qsdev->reset_ack_wq);
300 		} else {
301 			qsdev->nondma_int_received = true;
302 			wake_up_interruptible(&qsdev->nondma_int_received_wq);
303 		}
304 	}
305 
306 	if (int_mask & BIT(THC_RXDMA2_INT)) {
307 		while (!read_finished) {
308 			ret = thc_rxdma_read(qsdev->thc_hw, THC_RXDMA2, qsdev->input_buf,
309 					     &input_len, &read_finished);
310 			if (ret) {
311 				err_recover = 1;
312 				goto end;
313 			}
314 
315 			quickspi_handle_input_data(qsdev, input_len);
316 		}
317 	}
318 
319 end:
320 	thc_interrupt_enable(qsdev->thc_hw, true);
321 
322 	if (err_recover)
323 		if (try_recover(qsdev))
324 			qsdev->state = QUICKSPI_DISABLED;
325 
326 	pm_runtime_mark_last_busy(qsdev->dev);
327 	pm_runtime_put_autosuspend(qsdev->dev);
328 
329 	return IRQ_HANDLED;
330 }
331 
332 /**
333  * quickspi_dev_init - Initialize quickspi device
334  *
335  * @pdev: pointer to the thc pci device
336  * @mem_addr: The pointer of MMIO memory address
337  * @id: point to pci_device_id structure
338  *
339  * Alloc quickspi device structure and initialized THC device,
340  * then configure THC to HIDSPI mode.
341  *
342  * If success, enable THC hardware interrupt.
343  *
344  * Return: pointer to the quickspi device structure if success
345  * or NULL on failed.
346  */
quickspi_dev_init(struct pci_dev * pdev,void __iomem * mem_addr,const struct pci_device_id * id)347 static struct quickspi_device *quickspi_dev_init(struct pci_dev *pdev, void __iomem *mem_addr,
348 						 const struct pci_device_id *id)
349 {
350 	struct device *dev = &pdev->dev;
351 	struct quickspi_device *qsdev;
352 	int ret;
353 
354 	qsdev = devm_kzalloc(dev, sizeof(struct quickspi_device), GFP_KERNEL);
355 	if (!qsdev)
356 		return ERR_PTR(-ENOMEM);
357 
358 	qsdev->pdev = pdev;
359 	qsdev->dev = dev;
360 	qsdev->mem_addr = mem_addr;
361 	qsdev->state = QUICKSPI_DISABLED;
362 	qsdev->driver_data = (struct quickspi_driver_data *)id->driver_data;
363 
364 	init_waitqueue_head(&qsdev->reset_ack_wq);
365 	init_waitqueue_head(&qsdev->nondma_int_received_wq);
366 	init_waitqueue_head(&qsdev->report_desc_got_wq);
367 	init_waitqueue_head(&qsdev->get_report_cmpl_wq);
368 	init_waitqueue_head(&qsdev->set_report_cmpl_wq);
369 
370 	/* thc hw init */
371 	qsdev->thc_hw = thc_dev_init(qsdev->dev, qsdev->mem_addr);
372 	if (IS_ERR(qsdev->thc_hw)) {
373 		ret = PTR_ERR(qsdev->thc_hw);
374 		dev_err(dev, "Failed to initialize THC device context, ret = %d.\n", ret);
375 		return ERR_PTR(ret);
376 	}
377 
378 	ret = thc_interrupt_quiesce(qsdev->thc_hw, true);
379 	if (ret)
380 		return ERR_PTR(ret);
381 
382 	ret = thc_port_select(qsdev->thc_hw, THC_PORT_TYPE_SPI);
383 	if (ret) {
384 		dev_err(dev, "Failed to select THC port, ret = %d.\n", ret);
385 		return ERR_PTR(ret);
386 	}
387 
388 	ret = quickspi_get_acpi_resources(qsdev);
389 	if (ret) {
390 		dev_err(dev, "Get ACPI resources failed, ret = %d\n", ret);
391 		return ERR_PTR(ret);
392 	}
393 
394 	/* THC config for input/output address */
395 	thc_spi_input_output_address_config(qsdev->thc_hw,
396 					    qsdev->input_report_hdr_addr,
397 					    qsdev->input_report_bdy_addr,
398 					    qsdev->output_report_addr);
399 
400 	/* THC config for spi read operation */
401 	ret = thc_spi_read_config(qsdev->thc_hw, qsdev->spi_freq_val,
402 				  qsdev->spi_read_io_mode,
403 				  qsdev->spi_read_opcode,
404 				  qsdev->spi_packet_size);
405 	if (ret) {
406 		dev_err(dev, "thc_spi_read_config failed, ret = %d\n", ret);
407 		return ERR_PTR(ret);
408 	}
409 
410 	/* THC config for spi write operation */
411 	ret = thc_spi_write_config(qsdev->thc_hw, qsdev->spi_freq_val,
412 				   qsdev->spi_write_io_mode,
413 				   qsdev->spi_write_opcode,
414 				   qsdev->spi_packet_size,
415 				   qsdev->performance_limit);
416 	if (ret) {
417 		dev_err(dev, "thc_spi_write_config failed, ret = %d\n", ret);
418 		return ERR_PTR(ret);
419 	}
420 
421 	thc_ltr_config(qsdev->thc_hw,
422 		       qsdev->active_ltr_val,
423 		       qsdev->low_power_ltr_val);
424 
425 	thc_interrupt_config(qsdev->thc_hw);
426 
427 	thc_interrupt_enable(qsdev->thc_hw, true);
428 
429 	qsdev->state = QUICKSPI_INITED;
430 
431 	return qsdev;
432 }
433 
434 /**
435  * quickspi_dev_deinit - De-initialize quickspi device
436  *
437  * @qsdev: pointer to the quickspi device structure
438  *
439  * Disable THC interrupt and deinitilize THC.
440  */
quickspi_dev_deinit(struct quickspi_device * qsdev)441 static void quickspi_dev_deinit(struct quickspi_device *qsdev)
442 {
443 	thc_interrupt_enable(qsdev->thc_hw, false);
444 	thc_ltr_unconfig(qsdev->thc_hw);
445 
446 	qsdev->state = QUICKSPI_DISABLED;
447 }
448 
449 /**
450  * quickspi_dma_init - Configure THC DMA for quickspi device
451  * @qsdev: pointer to the quickspi device structure
452  *
453  * This function uses TIC's parameters(such as max input length, max output
454  * length) to allocate THC DMA buffers and configure THC DMA engines.
455  *
456  * Return: 0 if successful or error code on failed.
457  */
quickspi_dma_init(struct quickspi_device * qsdev)458 static int quickspi_dma_init(struct quickspi_device *qsdev)
459 {
460 	int ret;
461 
462 	ret = thc_dma_set_max_packet_sizes(qsdev->thc_hw, 0,
463 					   le16_to_cpu(qsdev->dev_desc.max_input_len),
464 					   le16_to_cpu(qsdev->dev_desc.max_output_len),
465 					   0);
466 	if (ret)
467 		return ret;
468 
469 	ret = thc_dma_allocate(qsdev->thc_hw);
470 	if (ret) {
471 		dev_err(qsdev->dev, "Allocate THC DMA buffer failed, ret = %d\n", ret);
472 		return ret;
473 	}
474 
475 	/* Enable RxDMA */
476 	ret = thc_dma_configure(qsdev->thc_hw);
477 	if (ret) {
478 		dev_err(qsdev->dev, "Configure THC DMA failed, ret = %d\n", ret);
479 		thc_dma_unconfigure(qsdev->thc_hw);
480 		thc_dma_release(qsdev->thc_hw);
481 		return ret;
482 	}
483 
484 	return ret;
485 }
486 
487 /**
488  * quickspi_dma_deinit - Release THC DMA for quickspi device
489  * @qsdev: pointer to the quickspi device structure
490  *
491  * Stop THC DMA engines and release all DMA buffers.
492  *
493  */
quickspi_dma_deinit(struct quickspi_device * qsdev)494 static void quickspi_dma_deinit(struct quickspi_device *qsdev)
495 {
496 	thc_dma_unconfigure(qsdev->thc_hw);
497 	thc_dma_release(qsdev->thc_hw);
498 }
499 
500 /**
501  * quickspi_alloc_report_buf - Alloc report buffers
502  * @qsdev: pointer to the quickspi device structure
503  *
504  * Allocate report descriptor buffer, it will be used for restore TIC HID
505  * report descriptor.
506  *
507  * Allocate input report buffer, it will be used for receive HID input report
508  * data from TIC.
509  *
510  * Allocate output report buffer, it will be used for store HID output report,
511  * such as set feature.
512  *
513  * Return: 0 if successful or error code on failed.
514  */
quickspi_alloc_report_buf(struct quickspi_device * qsdev)515 static int quickspi_alloc_report_buf(struct quickspi_device *qsdev)
516 {
517 	size_t max_report_len;
518 	size_t max_input_len;
519 
520 	qsdev->report_descriptor = devm_kzalloc(qsdev->dev,
521 						le16_to_cpu(qsdev->dev_desc.rep_desc_len),
522 						GFP_KERNEL);
523 	if (!qsdev->report_descriptor)
524 		return -ENOMEM;
525 
526 	max_input_len = max(le16_to_cpu(qsdev->dev_desc.rep_desc_len),
527 			    le16_to_cpu(qsdev->dev_desc.max_input_len));
528 
529 	qsdev->input_buf = devm_kzalloc(qsdev->dev, max_input_len, GFP_KERNEL);
530 	if (!qsdev->input_buf)
531 		return -ENOMEM;
532 
533 	max_report_len = max(le16_to_cpu(qsdev->dev_desc.max_output_len),
534 			     le16_to_cpu(qsdev->dev_desc.max_input_len));
535 
536 	qsdev->report_buf = devm_kzalloc(qsdev->dev, max_report_len, GFP_KERNEL);
537 	if (!qsdev->report_buf)
538 		return -ENOMEM;
539 
540 	return 0;
541 }
542 
543 /*
544  * quickspi_probe: Quickspi driver probe function
545  *
546  * @pdev: point to pci device
547  * @id: point to pci_device_id structure
548  *
549  * This function initializes THC and HIDSPI device, the flow is:
550  * - do THC pci device initialization
551  * - query HIDSPI ACPI parameters
552  * - configure THC to HIDSPI mode
553  * - go through HIDSPI enumeration flow
554  *   |- reset HIDSPI device
555  *   |- read device descriptor
556  * - enable THC interrupt and DMA
557  * - read report descriptor
558  * - register HID device
559  * - enable runtime power management
560  *
561  * Return 0 if success or error code on failure.
562  */
quickspi_probe(struct pci_dev * pdev,const struct pci_device_id * id)563 static int quickspi_probe(struct pci_dev *pdev,
564 			  const struct pci_device_id *id)
565 {
566 	struct quickspi_device *qsdev;
567 	void __iomem *mem_addr;
568 	int ret;
569 
570 	ret = pcim_enable_device(pdev);
571 	if (ret) {
572 		dev_err(&pdev->dev, "Failed to enable PCI device, ret = %d.\n", ret);
573 		return ret;
574 	}
575 
576 	pci_set_master(pdev);
577 
578 	ret = pcim_iomap_regions(pdev, BIT(0), KBUILD_MODNAME);
579 	if (ret) {
580 		dev_err(&pdev->dev, "Failed to get PCI regions, ret = %d.\n", ret);
581 		goto disable_pci_device;
582 	}
583 
584 	mem_addr = pcim_iomap_table(pdev)[0];
585 
586 	ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
587 	if (ret) {
588 		ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
589 		if (ret) {
590 			dev_err(&pdev->dev, "No usable DMA configuration %d\n", ret);
591 			goto unmap_io_region;
592 		}
593 	}
594 
595 	ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
596 	if (ret < 0) {
597 		dev_err(&pdev->dev,
598 			"Failed to allocate IRQ vectors. ret = %d\n", ret);
599 		goto unmap_io_region;
600 	}
601 
602 	pdev->irq = pci_irq_vector(pdev, 0);
603 
604 	qsdev = quickspi_dev_init(pdev, mem_addr, id);
605 	if (IS_ERR(qsdev)) {
606 		dev_err(&pdev->dev, "QuickSPI device init failed\n");
607 		ret = PTR_ERR(qsdev);
608 		goto unmap_io_region;
609 	}
610 
611 	pci_set_drvdata(pdev, qsdev);
612 
613 	ret = devm_request_threaded_irq(&pdev->dev, pdev->irq,
614 					quickspi_irq_quick_handler,
615 					quickspi_irq_thread_handler,
616 					IRQF_ONESHOT, KBUILD_MODNAME,
617 					qsdev);
618 	if (ret) {
619 		dev_err(&pdev->dev,
620 			"Failed to request threaded IRQ, irq = %d.\n", pdev->irq);
621 		goto dev_deinit;
622 	}
623 
624 	ret = reset_tic(qsdev);
625 	if (ret) {
626 		dev_err(&pdev->dev, "Reset Touch Device failed, ret = %d\n", ret);
627 		goto dev_deinit;
628 	}
629 
630 	ret = quickspi_alloc_report_buf(qsdev);
631 	if (ret) {
632 		dev_err(&pdev->dev, "Alloc report buffers failed, ret= %d\n", ret);
633 		goto dev_deinit;
634 	}
635 
636 	ret = quickspi_dma_init(qsdev);
637 	if (ret) {
638 		dev_err(&pdev->dev, "Setup THC DMA failed, ret= %d\n", ret);
639 		goto dev_deinit;
640 	}
641 
642 	ret = quickspi_get_report_descriptor(qsdev);
643 	if (ret) {
644 		dev_err(&pdev->dev, "Get report descriptor failed, ret = %d\n", ret);
645 		goto dma_deinit;
646 	}
647 
648 	ret = quickspi_hid_probe(qsdev);
649 	if (ret) {
650 		dev_err(&pdev->dev, "Failed to register HID device, ret = %d\n", ret);
651 		goto dma_deinit;
652 	}
653 
654 	qsdev->state = QUICKSPI_ENABLED;
655 
656 	/* Enable runtime power management */
657 	pm_runtime_use_autosuspend(qsdev->dev);
658 	pm_runtime_set_autosuspend_delay(qsdev->dev, DEFAULT_AUTO_SUSPEND_DELAY_MS);
659 	pm_runtime_mark_last_busy(qsdev->dev);
660 	pm_runtime_put_noidle(qsdev->dev);
661 	pm_runtime_put_autosuspend(qsdev->dev);
662 
663 	dev_dbg(&pdev->dev, "QuickSPI probe success\n");
664 
665 	return 0;
666 
667 dma_deinit:
668 	quickspi_dma_deinit(qsdev);
669 dev_deinit:
670 	quickspi_dev_deinit(qsdev);
671 unmap_io_region:
672 	pcim_iounmap_regions(pdev, BIT(0));
673 disable_pci_device:
674 	pci_clear_master(pdev);
675 
676 	return ret;
677 }
678 
679 /**
680  * quickspi_remove - Device Removal Routine
681  *
682  * @pdev: PCI device structure
683  *
684  * This is called by the PCI subsystem to alert the driver
685  * that it should release a PCI device.
686  */
quickspi_remove(struct pci_dev * pdev)687 static void quickspi_remove(struct pci_dev *pdev)
688 {
689 	struct quickspi_device *qsdev;
690 
691 	qsdev = pci_get_drvdata(pdev);
692 	if (!qsdev)
693 		return;
694 
695 	quickspi_hid_remove(qsdev);
696 	quickspi_dma_deinit(qsdev);
697 
698 	pm_runtime_get_noresume(qsdev->dev);
699 
700 	quickspi_dev_deinit(qsdev);
701 
702 	pcim_iounmap_regions(pdev, BIT(0));
703 	pci_clear_master(pdev);
704 }
705 
706 /**
707  * quickspi_shutdown - Device Shutdown Routine
708  *
709  * @pdev: PCI device structure
710  *
711  * This is called from the reboot notifier
712  * it's a simplified version of remove so we go down
713  * faster.
714  */
quickspi_shutdown(struct pci_dev * pdev)715 static void quickspi_shutdown(struct pci_dev *pdev)
716 {
717 	struct quickspi_device *qsdev;
718 
719 	qsdev = pci_get_drvdata(pdev);
720 	if (!qsdev)
721 		return;
722 
723 	/* Must stop DMA before reboot to avoid DMA entering into unknown state */
724 	quickspi_dma_deinit(qsdev);
725 
726 	quickspi_dev_deinit(qsdev);
727 }
728 
quickspi_suspend(struct device * device)729 static int quickspi_suspend(struct device *device)
730 {
731 	struct pci_dev *pdev = to_pci_dev(device);
732 	struct quickspi_device *qsdev;
733 	int ret;
734 
735 	qsdev = pci_get_drvdata(pdev);
736 	if (!qsdev)
737 		return -ENODEV;
738 
739 	ret = quickspi_set_power(qsdev, HIDSPI_SLEEP);
740 	if (ret)
741 		return ret;
742 
743 	ret = thc_interrupt_quiesce(qsdev->thc_hw, true);
744 	if (ret)
745 		return ret;
746 
747 	thc_interrupt_enable(qsdev->thc_hw, false);
748 
749 	thc_dma_unconfigure(qsdev->thc_hw);
750 
751 	return 0;
752 }
753 
quickspi_resume(struct device * device)754 static int quickspi_resume(struct device *device)
755 {
756 	struct pci_dev *pdev = to_pci_dev(device);
757 	struct quickspi_device *qsdev;
758 	int ret;
759 
760 	qsdev = pci_get_drvdata(pdev);
761 	if (!qsdev)
762 		return -ENODEV;
763 
764 	ret = thc_port_select(qsdev->thc_hw, THC_PORT_TYPE_SPI);
765 	if (ret)
766 		return ret;
767 
768 	thc_interrupt_config(qsdev->thc_hw);
769 
770 	thc_interrupt_enable(qsdev->thc_hw, true);
771 
772 	ret = thc_dma_configure(qsdev->thc_hw);
773 	if (ret)
774 		return ret;
775 
776 	ret = thc_interrupt_quiesce(qsdev->thc_hw, false);
777 	if (ret)
778 		return ret;
779 
780 	ret = quickspi_set_power(qsdev, HIDSPI_ON);
781 	if (ret)
782 		return ret;
783 
784 	return 0;
785 }
786 
quickspi_freeze(struct device * device)787 static int quickspi_freeze(struct device *device)
788 {
789 	struct pci_dev *pdev = to_pci_dev(device);
790 	struct quickspi_device *qsdev;
791 	int ret;
792 
793 	qsdev = pci_get_drvdata(pdev);
794 	if (!qsdev)
795 		return -ENODEV;
796 
797 	ret = thc_interrupt_quiesce(qsdev->thc_hw, true);
798 	if (ret)
799 		return ret;
800 
801 	thc_interrupt_enable(qsdev->thc_hw, false);
802 
803 	thc_dma_unconfigure(qsdev->thc_hw);
804 
805 	return 0;
806 }
807 
quickspi_thaw(struct device * device)808 static int quickspi_thaw(struct device *device)
809 {
810 	struct pci_dev *pdev = to_pci_dev(device);
811 	struct quickspi_device *qsdev;
812 	int ret;
813 
814 	qsdev = pci_get_drvdata(pdev);
815 	if (!qsdev)
816 		return -ENODEV;
817 
818 	ret = thc_dma_configure(qsdev->thc_hw);
819 	if (ret)
820 		return ret;
821 
822 	thc_interrupt_enable(qsdev->thc_hw, true);
823 
824 	ret = thc_interrupt_quiesce(qsdev->thc_hw, false);
825 	if (ret)
826 		return ret;
827 
828 	return 0;
829 }
830 
quickspi_poweroff(struct device * device)831 static int quickspi_poweroff(struct device *device)
832 {
833 	struct pci_dev *pdev = to_pci_dev(device);
834 	struct quickspi_device *qsdev;
835 	int ret;
836 
837 	qsdev = pci_get_drvdata(pdev);
838 	if (!qsdev)
839 		return -ENODEV;
840 
841 	ret = thc_interrupt_quiesce(qsdev->thc_hw, true);
842 	if (ret)
843 		return ret;
844 
845 	thc_interrupt_enable(qsdev->thc_hw, false);
846 
847 	thc_ltr_unconfig(qsdev->thc_hw);
848 
849 	quickspi_dma_deinit(qsdev);
850 
851 	return 0;
852 }
853 
quickspi_restore(struct device * device)854 static int quickspi_restore(struct device *device)
855 {
856 	struct pci_dev *pdev = to_pci_dev(device);
857 	struct quickspi_device *qsdev;
858 	int ret;
859 
860 	qsdev = pci_get_drvdata(pdev);
861 	if (!qsdev)
862 		return -ENODEV;
863 
864 	ret = thc_interrupt_quiesce(qsdev->thc_hw, true);
865 	if (ret)
866 		return ret;
867 
868 	/* Reconfig THC HW when back from hibernate */
869 	ret = thc_port_select(qsdev->thc_hw, THC_PORT_TYPE_SPI);
870 	if (ret)
871 		return ret;
872 
873 	thc_spi_input_output_address_config(qsdev->thc_hw,
874 					    qsdev->input_report_hdr_addr,
875 					    qsdev->input_report_bdy_addr,
876 					    qsdev->output_report_addr);
877 
878 	ret = thc_spi_read_config(qsdev->thc_hw, qsdev->spi_freq_val,
879 				  qsdev->spi_read_io_mode,
880 				  qsdev->spi_read_opcode,
881 				  qsdev->spi_packet_size);
882 	if (ret)
883 		return ret;
884 
885 	ret = thc_spi_write_config(qsdev->thc_hw, qsdev->spi_freq_val,
886 				   qsdev->spi_write_io_mode,
887 				   qsdev->spi_write_opcode,
888 				   qsdev->spi_packet_size,
889 				   qsdev->performance_limit);
890 	if (ret)
891 		return ret;
892 
893 	thc_interrupt_config(qsdev->thc_hw);
894 
895 	thc_interrupt_enable(qsdev->thc_hw, true);
896 
897 	/* TIC may lose power, needs go through reset flow */
898 	ret = reset_tic(qsdev);
899 	if (ret)
900 		return ret;
901 
902 	ret = thc_dma_configure(qsdev->thc_hw);
903 	if (ret)
904 		return ret;
905 
906 	thc_ltr_config(qsdev->thc_hw,
907 		       qsdev->active_ltr_val,
908 		       qsdev->low_power_ltr_val);
909 
910 	thc_change_ltr_mode(qsdev->thc_hw, THC_LTR_MODE_ACTIVE);
911 
912 	qsdev->state = QUICKSPI_ENABLED;
913 
914 	return 0;
915 }
916 
quickspi_runtime_suspend(struct device * device)917 static int quickspi_runtime_suspend(struct device *device)
918 {
919 	struct pci_dev *pdev = to_pci_dev(device);
920 	struct quickspi_device *qsdev;
921 
922 	qsdev = pci_get_drvdata(pdev);
923 	if (!qsdev)
924 		return -ENODEV;
925 
926 	thc_change_ltr_mode(qsdev->thc_hw, THC_LTR_MODE_LP);
927 
928 	pci_save_state(pdev);
929 
930 	return 0;
931 }
932 
quickspi_runtime_resume(struct device * device)933 static int quickspi_runtime_resume(struct device *device)
934 {
935 	struct pci_dev *pdev = to_pci_dev(device);
936 	struct quickspi_device *qsdev;
937 
938 	qsdev = pci_get_drvdata(pdev);
939 	if (!qsdev)
940 		return -ENODEV;
941 
942 	thc_change_ltr_mode(qsdev->thc_hw, THC_LTR_MODE_ACTIVE);
943 
944 	return 0;
945 }
946 
947 static const struct dev_pm_ops quickspi_pm_ops = {
948 	.suspend = quickspi_suspend,
949 	.resume = quickspi_resume,
950 	.freeze = quickspi_freeze,
951 	.thaw = quickspi_thaw,
952 	.poweroff = quickspi_poweroff,
953 	.restore = quickspi_restore,
954 	.runtime_suspend = quickspi_runtime_suspend,
955 	.runtime_resume = quickspi_runtime_resume,
956 	.runtime_idle = NULL,
957 };
958 
959 static const struct pci_device_id quickspi_pci_tbl[] = {
960 	{PCI_DEVICE_DATA(INTEL, THC_MTL_DEVICE_ID_SPI_PORT1, &mtl), },
961 	{PCI_DEVICE_DATA(INTEL, THC_MTL_DEVICE_ID_SPI_PORT2, &mtl), },
962 	{PCI_DEVICE_DATA(INTEL, THC_LNL_DEVICE_ID_SPI_PORT1, &lnl), },
963 	{PCI_DEVICE_DATA(INTEL, THC_LNL_DEVICE_ID_SPI_PORT2, &lnl), },
964 	{PCI_DEVICE_DATA(INTEL, THC_PTL_H_DEVICE_ID_SPI_PORT1, &ptl), },
965 	{PCI_DEVICE_DATA(INTEL, THC_PTL_H_DEVICE_ID_SPI_PORT2, &ptl), },
966 	{PCI_DEVICE_DATA(INTEL, THC_PTL_U_DEVICE_ID_SPI_PORT1, &ptl), },
967 	{PCI_DEVICE_DATA(INTEL, THC_PTL_U_DEVICE_ID_SPI_PORT2, &ptl), },
968 	{}
969 };
970 MODULE_DEVICE_TABLE(pci, quickspi_pci_tbl);
971 
972 static struct pci_driver quickspi_driver = {
973 	.name = KBUILD_MODNAME,
974 	.id_table = quickspi_pci_tbl,
975 	.probe = quickspi_probe,
976 	.remove = quickspi_remove,
977 	.shutdown = quickspi_shutdown,
978 	.driver.pm = &quickspi_pm_ops,
979 	.driver.probe_type = PROBE_PREFER_ASYNCHRONOUS,
980 };
981 
982 module_pci_driver(quickspi_driver);
983 
984 MODULE_AUTHOR("Xinpeng Sun <[email protected]>");
985 MODULE_AUTHOR("Even Xu <[email protected]>");
986 
987 MODULE_DESCRIPTION("Intel(R) QuickSPI Driver");
988 MODULE_LICENSE("GPL");
989 MODULE_IMPORT_NS("INTEL_THC");
990