2 * PCI glue for ISHTP provider device (ISH) driver
4 * Copyright (c) 2014-2016, Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/kernel.h>
19 #include <linux/device.h>
21 #include <linux/errno.h>
22 #include <linux/types.h>
23 #include <linux/pci.h>
24 #include <linux/sched.h>
25 #include <linux/interrupt.h>
26 #include <linux/workqueue.h>
27 #include <linux/miscdevice.h>
28 #define CREATE_TRACE_POINTS
29 #include <trace/events/intel_ish.h>
30 #include "ishtp-dev.h"
33 static const struct pci_device_id ish_pci_tbl
[] = {
34 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, CHV_DEVICE_ID
)},
35 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, BXT_Ax_DEVICE_ID
)},
36 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, BXT_Bx_DEVICE_ID
)},
37 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, APL_Ax_DEVICE_ID
)},
38 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, SPT_Ax_DEVICE_ID
)},
41 MODULE_DEVICE_TABLE(pci
, ish_pci_tbl
);
44 * ish_event_tracer() - Callback function to dump trace messages
46 * @format: printf style format
48 * Callback to direct log messages to Linux trace buffers
50 static void ish_event_tracer(struct ishtp_device
*dev
, char *format
, ...)
52 if (trace_ishtp_dump_enabled()) {
56 va_start(args
, format
);
57 vsnprintf(tmp_buf
, sizeof(tmp_buf
), format
, args
);
60 trace_ishtp_dump(tmp_buf
);
65 * ish_init() - Init function
68 * This function initialize wait queues for suspend/resume and call
69 * calls hadware initialization function. This will initiate
72 * Return: 0 for success or error code for failure
74 static int ish_init(struct ishtp_device
*dev
)
78 /* Set the state of ISH HW to start */
79 ret
= ish_hw_start(dev
);
81 dev_err(dev
->devc
, "ISH: hw start failed.\n");
85 /* Start the inter process communication to ISH processor */
86 ret
= ishtp_start(dev
);
88 dev_err(dev
->devc
, "ISHTP: Protocol init failed.\n");
96 * ish_probe() - PCI driver probe callback
100 * Initialize PCI function, setup interrupt and call for ISH initialization
102 * Return: 0 for success or error code for failure
104 static int ish_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
106 struct ishtp_device
*dev
;
111 ret
= pci_enable_device(pdev
);
113 dev_err(&pdev
->dev
, "ISH: Failed to enable PCI device\n");
117 /* set PCI host mastering */
118 pci_set_master(pdev
);
120 /* pci request regions for ISH driver */
121 ret
= pci_request_regions(pdev
, KBUILD_MODNAME
);
123 dev_err(&pdev
->dev
, "ISH: Failed to get PCI regions\n");
127 /* allocates and initializes the ISH dev structure */
128 dev
= ish_dev_init(pdev
);
131 goto release_regions
;
134 dev
->print_log
= ish_event_tracer
;
136 /* mapping IO device memory */
137 hw
->mem_addr
= pci_iomap(pdev
, 0, 0);
139 dev_err(&pdev
->dev
, "ISH: mapping I/O range failure\n");
146 pdev
->dev_flags
|= PCI_DEV_FLAGS_NO_D3
;
148 /* request and enable interrupt */
149 ret
= request_irq(pdev
->irq
, ish_irq_handler
, IRQF_SHARED
,
150 KBUILD_MODNAME
, dev
);
152 dev_err(&pdev
->dev
, "ISH: request IRQ failure (%d)\n",
157 dev_set_drvdata(dev
->devc
, dev
);
159 init_waitqueue_head(&dev
->suspend_wait
);
160 init_waitqueue_head(&dev
->resume_wait
);
169 free_irq(pdev
->irq
, dev
);
171 pci_iounmap(pdev
, hw
->mem_addr
);
174 pci_release_regions(pdev
);
176 pci_clear_master(pdev
);
177 pci_disable_device(pdev
);
178 dev_err(&pdev
->dev
, "ISH: PCI driver initialization failed.\n");
184 * ish_remove() - PCI driver remove callback
187 * This function does cleanup of ISH on pci remove callback
189 static void ish_remove(struct pci_dev
*pdev
)
191 struct ishtp_device
*ishtp_dev
= pci_get_drvdata(pdev
);
192 struct ish_hw
*hw
= to_ish_hw(ishtp_dev
);
194 ishtp_bus_remove_all_clients(ishtp_dev
, false);
195 ish_device_disable(ishtp_dev
);
197 free_irq(pdev
->irq
, ishtp_dev
);
198 pci_iounmap(pdev
, hw
->mem_addr
);
199 pci_release_regions(pdev
);
200 pci_clear_master(pdev
);
201 pci_disable_device(pdev
);
206 static struct device
*ish_resume_device
;
209 * ish_resume_handler() - Work function to complete resume
212 * The resume work function to complete resume function asynchronously.
213 * There are two types of platforms, one where ISH is not powered off,
214 * in that case a simple resume message is enough, others we need
217 static void ish_resume_handler(struct work_struct
*work
)
219 struct pci_dev
*pdev
= to_pci_dev(ish_resume_device
);
220 struct ishtp_device
*dev
= pci_get_drvdata(pdev
);
223 ishtp_send_resume(dev
);
225 /* 50 ms to get resume response */
226 if (dev
->resume_flag
)
227 ret
= wait_event_interruptible_timeout(dev
->resume_wait
,
229 msecs_to_jiffies(50));
232 * If no resume response. This platform is not S0ix compatible
233 * So on resume full reboot of ISH processor will happen, so
234 * need to go through init sequence again
236 if (dev
->resume_flag
)
241 * ish_suspend() - ISH suspend callback
242 * @device: device pointer
244 * ISH suspend callback
246 * Return: 0 to the pm core
248 static int ish_suspend(struct device
*device
)
250 struct pci_dev
*pdev
= to_pci_dev(device
);
251 struct ishtp_device
*dev
= pci_get_drvdata(pdev
);
253 enable_irq_wake(pdev
->irq
);
255 * If previous suspend hasn't been asnwered then ISH is likely dead,
256 * don't attempt nested notification
258 if (dev
->suspend_flag
)
261 dev
->resume_flag
= 0;
262 dev
->suspend_flag
= 1;
263 ishtp_send_suspend(dev
);
265 /* 25 ms should be enough for live ISH to flush all IPC buf */
266 if (dev
->suspend_flag
)
267 wait_event_interruptible_timeout(dev
->suspend_wait
,
269 msecs_to_jiffies(25));
274 static DECLARE_WORK(resume_work
, ish_resume_handler
);
276 * ish_resume() - ISH resume callback
277 * @device: device pointer
279 * ISH resume callback
281 * Return: 0 to the pm core
283 static int ish_resume(struct device
*device
)
285 struct pci_dev
*pdev
= to_pci_dev(device
);
286 struct ishtp_device
*dev
= pci_get_drvdata(pdev
);
288 ish_resume_device
= device
;
289 dev
->resume_flag
= 1;
291 disable_irq_wake(pdev
->irq
);
292 schedule_work(&resume_work
);
297 static const struct dev_pm_ops ish_pm_ops
= {
298 .suspend
= ish_suspend
,
299 .resume
= ish_resume
,
301 #define ISHTP_ISH_PM_OPS (&ish_pm_ops)
303 #define ISHTP_ISH_PM_OPS NULL
304 #endif /* CONFIG_PM */
306 static struct pci_driver ish_driver
= {
307 .name
= KBUILD_MODNAME
,
308 .id_table
= ish_pci_tbl
,
310 .remove
= ish_remove
,
311 .driver
.pm
= ISHTP_ISH_PM_OPS
,
314 module_pci_driver(ish_driver
);
316 /* Original author */
317 MODULE_AUTHOR("Daniel Drubin <daniel.drubin@intel.com>");
318 /* Adoption to upstream Linux kernel */
319 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
321 MODULE_DESCRIPTION("Intel(R) Integrated Sensor Hub PCI Device Driver");
322 MODULE_LICENSE("GPL");