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