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