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 */
quicki2c_acpi_get_dsm_property(struct acpi_device * adev,const guid_t * guid,u64 rev,u64 func,acpi_object_type type,void * prop_buf)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 */
quicki2c_acpi_get_dsd_property(struct acpi_device * adev,acpi_string dsd_method_name,acpi_object_type type,void * prop_buf)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 */
quicki2c_get_acpi_resources(struct quicki2c_device * qcdev)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 */
quicki2c_irq_quick_handler(int irq,void * dev_id)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 */
try_recover(struct quicki2c_device * qcdev)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
handle_input_report(struct quicki2c_device * qcdev)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 */
quicki2c_irq_thread_handler(int irq,void * dev_id)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 */
quicki2c_dev_init(struct pci_dev * pdev,void __iomem * mem_addr,const struct quicki2c_ddata * ddata)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 */
quicki2c_dev_deinit(struct quicki2c_device * qcdev)420 static void quicki2c_dev_deinit(struct quicki2c_device *qcdev)
421 {
422 thc_interrupt_quiesce(qcdev->thc_hw, true);
423 thc_interrupt_enable(qcdev->thc_hw, false);
424 thc_ltr_unconfig(qcdev->thc_hw);
425 thc_wot_unconfig(qcdev->thc_hw);
426
427 qcdev->state = QUICKI2C_DISABLED;
428 }
429
430 /**
431 * quicki2c_dma_adv_enable - Configure and enable DMA advanced features
432 * @qcdev: Pointer to the quicki2c_device structure
433 *
434 * If platform supports THC DMA advanced features, such as max input size
435 * control or interrupt delay, configures and enables them.
436 */
quicki2c_dma_adv_enable(struct quicki2c_device * qcdev)437 static void quicki2c_dma_adv_enable(struct quicki2c_device *qcdev)
438 {
439 /*
440 * If platform supports max input size control feature and touch device
441 * max input length <= THC detect capability, enable the feature with device
442 * max input length.
443 */
444 if (qcdev->ddata->max_detect_size >=
445 le16_to_cpu(qcdev->dev_desc.max_input_len)) {
446 thc_i2c_set_rx_max_size(qcdev->thc_hw,
447 le16_to_cpu(qcdev->dev_desc.max_input_len));
448 thc_i2c_rx_max_size_enable(qcdev->thc_hw, true);
449 }
450
451 /* If platform supports interrupt delay feature, enable it with given delay */
452 if (qcdev->ddata->interrupt_delay) {
453 thc_i2c_set_rx_int_delay(qcdev->thc_hw,
454 qcdev->ddata->interrupt_delay);
455 thc_i2c_rx_int_delay_enable(qcdev->thc_hw, true);
456 }
457 }
458
459 /**
460 * quicki2c_dma_adv_disable - Disable DMA advanced features
461 * @qcdev: Pointer to the quicki2c device structure
462 *
463 * Disable all DMA advanced features if platform supports.
464 */
quicki2c_dma_adv_disable(struct quicki2c_device * qcdev)465 static void quicki2c_dma_adv_disable(struct quicki2c_device *qcdev)
466 {
467 if (qcdev->ddata->max_detect_size)
468 thc_i2c_rx_max_size_enable(qcdev->thc_hw, false);
469
470 if (qcdev->ddata->interrupt_delay)
471 thc_i2c_rx_int_delay_enable(qcdev->thc_hw, false);
472 }
473
474 /**
475 * quicki2c_dma_init - Configure THC DMA for QuickI2C device
476 * @qcdev: Pointer to the quicki2c_device structure
477 *
478 * This function uses TIC's parameters(such as max input length, max output
479 * length) to allocate THC DMA buffers and configure THC DMA engines.
480 *
481 * Return: 0 if success or error code on failure.
482 */
quicki2c_dma_init(struct quicki2c_device * qcdev)483 static int quicki2c_dma_init(struct quicki2c_device *qcdev)
484 {
485 size_t swdma_max_len;
486 int ret;
487
488 swdma_max_len = max(le16_to_cpu(qcdev->dev_desc.max_input_len),
489 le16_to_cpu(qcdev->dev_desc.report_desc_len));
490
491 ret = thc_dma_set_max_packet_sizes(qcdev->thc_hw, 0,
492 le16_to_cpu(qcdev->dev_desc.max_input_len),
493 le16_to_cpu(qcdev->dev_desc.max_output_len),
494 swdma_max_len);
495 if (ret)
496 return ret;
497
498 ret = thc_dma_allocate(qcdev->thc_hw);
499 if (ret) {
500 dev_err(qcdev->dev, "Allocate THC DMA buffer failed, ret = %d\n", ret);
501 return ret;
502 }
503
504 /* Enable RxDMA */
505 ret = thc_dma_configure(qcdev->thc_hw);
506 if (ret) {
507 dev_err(qcdev->dev, "Configure THC DMA failed, ret = %d\n", ret);
508 thc_dma_unconfigure(qcdev->thc_hw);
509 thc_dma_release(qcdev->thc_hw);
510 return ret;
511 }
512
513 if (qcdev->ddata)
514 quicki2c_dma_adv_enable(qcdev);
515
516 return 0;
517 }
518
519 /**
520 * quicki2c_dma_deinit - Release THC DMA for QuickI2C device
521 * @qcdev: Pointer to the quicki2c_device structure
522 *
523 * Stop THC DMA engines and release all DMA buffers.
524 *
525 */
quicki2c_dma_deinit(struct quicki2c_device * qcdev)526 static void quicki2c_dma_deinit(struct quicki2c_device *qcdev)
527 {
528 thc_dma_unconfigure(qcdev->thc_hw);
529 thc_dma_release(qcdev->thc_hw);
530
531 if (qcdev->ddata)
532 quicki2c_dma_adv_disable(qcdev);
533 }
534
535 /**
536 * quicki2c_alloc_report_buf - Alloc report buffers
537 * @qcdev: Pointer to the quicki2c_device structure
538 *
539 * Allocate report descriptor buffer, it will be used for restore TIC HID
540 * report descriptor.
541 *
542 * Allocate input report buffer, it will be used for receive HID input report
543 * data from TIC.
544 *
545 * Allocate output report buffer, it will be used for store HID output report,
546 * such as set feature.
547 *
548 * Return: 0 if success or error code on failure.
549 */
quicki2c_alloc_report_buf(struct quicki2c_device * qcdev)550 static int quicki2c_alloc_report_buf(struct quicki2c_device *qcdev)
551 {
552 size_t max_report_len;
553
554 qcdev->report_descriptor = devm_kzalloc(qcdev->dev,
555 le16_to_cpu(qcdev->dev_desc.report_desc_len),
556 GFP_KERNEL);
557 if (!qcdev->report_descriptor)
558 return -ENOMEM;
559
560 /*
561 * Some HIDI2C devices don't declare input/output max length correctly,
562 * give default 4K buffer to avoid DMA buffer overrun.
563 */
564 max_report_len = max(le16_to_cpu(qcdev->dev_desc.max_input_len), SZ_4K);
565
566 qcdev->input_buf = devm_kzalloc(qcdev->dev, max_report_len, GFP_KERNEL);
567 if (!qcdev->input_buf)
568 return -ENOMEM;
569
570 if (!le16_to_cpu(qcdev->dev_desc.max_output_len))
571 qcdev->dev_desc.max_output_len = cpu_to_le16(SZ_4K);
572
573 max_report_len = max(le16_to_cpu(qcdev->dev_desc.max_output_len),
574 max_report_len);
575
576 qcdev->report_buf = devm_kzalloc(qcdev->dev, max_report_len, GFP_KERNEL);
577 if (!qcdev->report_buf)
578 return -ENOMEM;
579
580 qcdev->report_len = max_report_len;
581
582 return 0;
583 }
584
585 /*
586 * quicki2c_probe: QuickI2C driver probe function
587 * @pdev: Point to PCI device
588 * @id: Point to pci_device_id structure
589 *
590 * This function initializes THC and HIDI2C device, the flow is:
591 * - Do THC pci device initialization
592 * - Query HIDI2C ACPI parameters
593 * - Configure THC to HIDI2C mode
594 * - Go through HIDI2C enumeration flow
595 * |- Read device descriptor
596 * |- Reset HIDI2C device
597 * - Enable THC interrupt and DMA
598 * - Read report descriptor
599 * - Register HID device
600 * - Enable runtime power management
601 *
602 * Return 0 if success or error code on failure.
603 */
quicki2c_probe(struct pci_dev * pdev,const struct pci_device_id * id)604 static int quicki2c_probe(struct pci_dev *pdev, const struct pci_device_id *id)
605 {
606 const struct quicki2c_ddata *ddata = (const struct quicki2c_ddata *)id->driver_data;
607 struct quicki2c_device *qcdev;
608 void __iomem *mem_addr;
609 int ret;
610
611 ret = pcim_enable_device(pdev);
612 if (ret) {
613 dev_err_once(&pdev->dev, "Failed to enable PCI device, ret = %d.\n", ret);
614 return ret;
615 }
616
617 pci_set_master(pdev);
618
619 mem_addr = pcim_iomap_region(pdev, 0, KBUILD_MODNAME);
620 ret = PTR_ERR_OR_ZERO(mem_addr);
621 if (ret) {
622 dev_err_once(&pdev->dev, "Failed to get PCI regions, ret = %d.\n", ret);
623 goto disable_pci_device;
624 }
625
626 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
627 if (ret) {
628 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
629 if (ret) {
630 dev_err_once(&pdev->dev, "No usable DMA configuration %d\n", ret);
631 goto disable_pci_device;
632 }
633 }
634
635 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
636 if (ret < 0) {
637 dev_err_once(&pdev->dev,
638 "Failed to allocate IRQ vectors. ret = %d\n", ret);
639 goto disable_pci_device;
640 }
641
642 pdev->irq = pci_irq_vector(pdev, 0);
643
644 qcdev = quicki2c_dev_init(pdev, mem_addr, ddata);
645 if (IS_ERR(qcdev)) {
646 dev_err_once(&pdev->dev, "QuickI2C device init failed\n");
647 ret = PTR_ERR(qcdev);
648 goto disable_pci_device;
649 }
650
651 pci_set_drvdata(pdev, qcdev);
652
653 ret = devm_request_threaded_irq(&pdev->dev, pdev->irq,
654 quicki2c_irq_quick_handler,
655 quicki2c_irq_thread_handler,
656 IRQF_ONESHOT, KBUILD_MODNAME,
657 qcdev);
658 if (ret) {
659 dev_err_once(&pdev->dev,
660 "Failed to request threaded IRQ, irq = %d.\n", pdev->irq);
661 goto dev_deinit;
662 }
663
664 ret = quicki2c_get_device_descriptor(qcdev);
665 if (ret) {
666 dev_err(&pdev->dev, "Get device descriptor failed, ret = %d\n", ret);
667 goto dev_deinit;
668 }
669
670 ret = quicki2c_alloc_report_buf(qcdev);
671 if (ret) {
672 dev_err(&pdev->dev, "Alloc report buffers failed, ret= %d\n", ret);
673 goto dev_deinit;
674 }
675
676 ret = quicki2c_dma_init(qcdev);
677 if (ret) {
678 dev_err(&pdev->dev, "Setup THC DMA failed, ret= %d\n", ret);
679 goto dev_deinit;
680 }
681
682 ret = thc_interrupt_quiesce(qcdev->thc_hw, false);
683 if (ret)
684 goto dev_deinit;
685
686 ret = quicki2c_set_power(qcdev, HIDI2C_ON);
687 if (ret) {
688 dev_err(&pdev->dev, "Set Power On command failed, ret= %d\n", ret);
689 goto dev_deinit;
690 }
691
692 ret = quicki2c_reset(qcdev);
693 if (ret) {
694 dev_err(&pdev->dev, "Reset HIDI2C device failed, ret= %d\n", ret);
695 goto dev_deinit;
696 }
697
698 ret = quicki2c_get_report_descriptor(qcdev);
699 if (ret) {
700 dev_err(&pdev->dev, "Get report descriptor failed, ret = %d\n", ret);
701 goto dma_deinit;
702 }
703
704 ret = quicki2c_hid_probe(qcdev);
705 if (ret) {
706 dev_err(&pdev->dev, "Failed to register HID device, ret = %d\n", ret);
707 goto dma_deinit;
708 }
709
710 qcdev->state = QUICKI2C_ENABLED;
711
712 /* Enable runtime power management */
713 pm_runtime_use_autosuspend(qcdev->dev);
714 pm_runtime_set_autosuspend_delay(qcdev->dev, DEFAULT_AUTO_SUSPEND_DELAY_MS);
715 pm_runtime_mark_last_busy(qcdev->dev);
716 pm_runtime_put_noidle(qcdev->dev);
717 pm_runtime_put_autosuspend(qcdev->dev);
718
719 dev_dbg(&pdev->dev, "QuickI2C probe success\n");
720
721 return 0;
722
723 dma_deinit:
724 quicki2c_dma_deinit(qcdev);
725 dev_deinit:
726 quicki2c_dev_deinit(qcdev);
727 disable_pci_device:
728 pci_clear_master(pdev);
729
730 return ret;
731 }
732
733 /**
734 * quicki2c_remove - Device Removal Routine
735 * @pdev: Point to PCI device structure
736 *
737 * This is called by the PCI subsystem to alert the driver that it should
738 * release a PCI device.
739 */
quicki2c_remove(struct pci_dev * pdev)740 static void quicki2c_remove(struct pci_dev *pdev)
741 {
742 struct quicki2c_device *qcdev;
743
744 qcdev = pci_get_drvdata(pdev);
745 if (!qcdev)
746 return;
747
748 quicki2c_hid_remove(qcdev);
749 quicki2c_dma_deinit(qcdev);
750
751 pm_runtime_get_noresume(qcdev->dev);
752
753 quicki2c_dev_deinit(qcdev);
754
755 pci_clear_master(pdev);
756 }
757
758 /**
759 * quicki2c_shutdown - Device Shutdown Routine
760 * @pdev: Point to PCI device structure
761 *
762 * This is called from the reboot notifier, it's a simplified version of remove
763 * so we go down faster.
764 */
quicki2c_shutdown(struct pci_dev * pdev)765 static void quicki2c_shutdown(struct pci_dev *pdev)
766 {
767 struct quicki2c_device *qcdev;
768
769 qcdev = pci_get_drvdata(pdev);
770 if (!qcdev)
771 return;
772
773 /* Must stop DMA before reboot to avoid DMA entering into unknown state */
774 quicki2c_dma_deinit(qcdev);
775
776 quicki2c_dev_deinit(qcdev);
777 }
778
quicki2c_suspend(struct device * device)779 static int quicki2c_suspend(struct device *device)
780 {
781 struct pci_dev *pdev = to_pci_dev(device);
782 struct quicki2c_device *qcdev;
783 int ret;
784
785 qcdev = pci_get_drvdata(pdev);
786 if (!qcdev)
787 return -ENODEV;
788
789 /*
790 * As I2C is THC subsystem, no register auto save/restore support,
791 * need driver to do that explicitly for every D3 case.
792 */
793 ret = thc_i2c_subip_regs_save(qcdev->thc_hw);
794 if (ret)
795 return ret;
796
797 ret = thc_interrupt_quiesce(qcdev->thc_hw, true);
798 if (ret)
799 return ret;
800
801 thc_interrupt_enable(qcdev->thc_hw, false);
802
803 thc_dma_unconfigure(qcdev->thc_hw);
804
805 return 0;
806 }
807
quicki2c_resume(struct device * device)808 static int quicki2c_resume(struct device *device)
809 {
810 struct pci_dev *pdev = to_pci_dev(device);
811 struct quicki2c_device *qcdev;
812 int ret;
813
814 qcdev = pci_get_drvdata(pdev);
815 if (!qcdev)
816 return -ENODEV;
817
818 ret = thc_port_select(qcdev->thc_hw, THC_PORT_TYPE_I2C);
819 if (ret)
820 return ret;
821
822 ret = thc_i2c_subip_regs_restore(qcdev->thc_hw);
823 if (ret)
824 return ret;
825
826 thc_interrupt_config(qcdev->thc_hw);
827
828 thc_interrupt_enable(qcdev->thc_hw, true);
829
830 ret = thc_dma_configure(qcdev->thc_hw);
831 if (ret)
832 return ret;
833
834 ret = thc_interrupt_quiesce(qcdev->thc_hw, false);
835 if (ret)
836 return ret;
837
838 return 0;
839 }
840
quicki2c_freeze(struct device * device)841 static int quicki2c_freeze(struct device *device)
842 {
843 struct pci_dev *pdev = to_pci_dev(device);
844 struct quicki2c_device *qcdev;
845 int ret;
846
847 qcdev = pci_get_drvdata(pdev);
848 if (!qcdev)
849 return -ENODEV;
850
851 ret = thc_interrupt_quiesce(qcdev->thc_hw, true);
852 if (ret)
853 return ret;
854
855 thc_interrupt_enable(qcdev->thc_hw, false);
856
857 thc_dma_unconfigure(qcdev->thc_hw);
858
859 return 0;
860 }
861
quicki2c_thaw(struct device * device)862 static int quicki2c_thaw(struct device *device)
863 {
864 struct pci_dev *pdev = to_pci_dev(device);
865 struct quicki2c_device *qcdev;
866 int ret;
867
868 qcdev = pci_get_drvdata(pdev);
869 if (!qcdev)
870 return -ENODEV;
871
872 ret = thc_dma_configure(qcdev->thc_hw);
873 if (ret)
874 return ret;
875
876 thc_interrupt_enable(qcdev->thc_hw, true);
877
878 ret = thc_interrupt_quiesce(qcdev->thc_hw, false);
879 if (ret)
880 return ret;
881
882 return 0;
883 }
884
quicki2c_poweroff(struct device * device)885 static int quicki2c_poweroff(struct device *device)
886 {
887 struct pci_dev *pdev = to_pci_dev(device);
888 struct quicki2c_device *qcdev;
889 int ret;
890
891 qcdev = pci_get_drvdata(pdev);
892 if (!qcdev)
893 return -ENODEV;
894
895 ret = thc_interrupt_quiesce(qcdev->thc_hw, true);
896 if (ret)
897 return ret;
898
899 thc_interrupt_enable(qcdev->thc_hw, false);
900
901 thc_ltr_unconfig(qcdev->thc_hw);
902
903 quicki2c_dma_deinit(qcdev);
904
905 return 0;
906 }
907
quicki2c_restore(struct device * device)908 static int quicki2c_restore(struct device *device)
909 {
910 struct pci_dev *pdev = to_pci_dev(device);
911 struct quicki2c_device *qcdev;
912 int ret;
913
914 qcdev = pci_get_drvdata(pdev);
915 if (!qcdev)
916 return -ENODEV;
917
918 /* Reconfig THC HW when back from hibernate */
919 ret = thc_port_select(qcdev->thc_hw, THC_PORT_TYPE_I2C);
920 if (ret)
921 return ret;
922
923 ret = thc_i2c_subip_init(qcdev->thc_hw, qcdev->i2c_slave_addr,
924 qcdev->i2c_speed_mode,
925 qcdev->i2c_clock_hcnt,
926 qcdev->i2c_clock_lcnt);
927 if (ret)
928 return ret;
929
930 thc_interrupt_config(qcdev->thc_hw);
931
932 thc_interrupt_enable(qcdev->thc_hw, true);
933
934 ret = thc_interrupt_quiesce(qcdev->thc_hw, false);
935 if (ret)
936 return ret;
937
938 ret = thc_dma_configure(qcdev->thc_hw);
939 if (ret)
940 return ret;
941
942 thc_ltr_config(qcdev->thc_hw,
943 qcdev->active_ltr_val,
944 qcdev->low_power_ltr_val);
945
946 thc_change_ltr_mode(qcdev->thc_hw, THC_LTR_MODE_ACTIVE);
947
948 return 0;
949 }
950
quicki2c_runtime_suspend(struct device * device)951 static int quicki2c_runtime_suspend(struct device *device)
952 {
953 struct pci_dev *pdev = to_pci_dev(device);
954 struct quicki2c_device *qcdev;
955
956 qcdev = pci_get_drvdata(pdev);
957 if (!qcdev)
958 return -ENODEV;
959
960 thc_change_ltr_mode(qcdev->thc_hw, THC_LTR_MODE_LP);
961
962 pci_save_state(pdev);
963
964 return 0;
965 }
966
quicki2c_runtime_resume(struct device * device)967 static int quicki2c_runtime_resume(struct device *device)
968 {
969 struct pci_dev *pdev = to_pci_dev(device);
970 struct quicki2c_device *qcdev;
971
972 qcdev = pci_get_drvdata(pdev);
973 if (!qcdev)
974 return -ENODEV;
975
976 thc_change_ltr_mode(qcdev->thc_hw, THC_LTR_MODE_ACTIVE);
977
978 return 0;
979 }
980
981 static const struct dev_pm_ops quicki2c_pm_ops = {
982 .suspend = quicki2c_suspend,
983 .resume = quicki2c_resume,
984 .freeze = quicki2c_freeze,
985 .thaw = quicki2c_thaw,
986 .poweroff = quicki2c_poweroff,
987 .restore = quicki2c_restore,
988 .runtime_suspend = quicki2c_runtime_suspend,
989 .runtime_resume = quicki2c_runtime_resume,
990 .runtime_idle = NULL,
991 };
992
993 static const struct pci_device_id quicki2c_pci_tbl[] = {
994 { PCI_DEVICE_DATA(INTEL, THC_LNL_DEVICE_ID_I2C_PORT1, NULL) },
995 { PCI_DEVICE_DATA(INTEL, THC_LNL_DEVICE_ID_I2C_PORT2, NULL) },
996 { PCI_DEVICE_DATA(INTEL, THC_PTL_H_DEVICE_ID_I2C_PORT1, &ptl_ddata) },
997 { PCI_DEVICE_DATA(INTEL, THC_PTL_H_DEVICE_ID_I2C_PORT2, &ptl_ddata) },
998 { PCI_DEVICE_DATA(INTEL, THC_PTL_U_DEVICE_ID_I2C_PORT1, &ptl_ddata) },
999 { PCI_DEVICE_DATA(INTEL, THC_PTL_U_DEVICE_ID_I2C_PORT2, &ptl_ddata) },
1000 { PCI_DEVICE_DATA(INTEL, THC_WCL_DEVICE_ID_I2C_PORT1, &ptl_ddata) },
1001 { PCI_DEVICE_DATA(INTEL, THC_WCL_DEVICE_ID_I2C_PORT2, &ptl_ddata) },
1002 { }
1003 };
1004 MODULE_DEVICE_TABLE(pci, quicki2c_pci_tbl);
1005
1006 static struct pci_driver quicki2c_driver = {
1007 .name = KBUILD_MODNAME,
1008 .id_table = quicki2c_pci_tbl,
1009 .probe = quicki2c_probe,
1010 .remove = quicki2c_remove,
1011 .shutdown = quicki2c_shutdown,
1012 .driver.pm = &quicki2c_pm_ops,
1013 .driver.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1014 };
1015
1016 module_pci_driver(quicki2c_driver);
1017
1018 MODULE_AUTHOR("Xinpeng Sun <xinpeng.sun@intel.com>");
1019 MODULE_AUTHOR("Even Xu <even.xu@intel.com>");
1020
1021 MODULE_DESCRIPTION("Intel(R) QuickI2C Driver");
1022 MODULE_LICENSE("GPL");
1023 MODULE_IMPORT_NS("INTEL_THC");
1024