at commit c03c1a14

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