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 #define CREATE_TRACE_POINTS
28 #include <trace/events/intel_ish.h>
29 #include "ishtp-dev.h"
32 static const struct pci_device_id ish_pci_tbl[] = {
33 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CHV_DEVICE_ID)},
34 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, BXT_Ax_DEVICE_ID)},
35 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, BXT_Bx_DEVICE_ID)},
36 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, APL_Ax_DEVICE_ID)},
37 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, SPT_Ax_DEVICE_ID)},
38 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CNL_Ax_DEVICE_ID)},
39 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, GLK_Ax_DEVICE_ID)},
40 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CNL_H_DEVICE_ID)},
43 MODULE_DEVICE_TABLE(pci, ish_pci_tbl);
46 * ish_event_tracer() - Callback function to dump trace messages
48 * @format: printf style format
50 * Callback to direct log messages to Linux trace buffers
53 void ish_event_tracer(struct ishtp_device *dev, const char *format, ...)
55 if (trace_ishtp_dump_enabled()) {
59 va_start(args, format);
60 vsnprintf(tmp_buf, sizeof(tmp_buf), format, args);
63 trace_ishtp_dump(tmp_buf);
68 * ish_init() - Init function
71 * This function initialize wait queues for suspend/resume and call
72 * calls hadware initialization function. This will initiate
75 * Return: 0 for success or error code for failure
77 static int ish_init(struct ishtp_device *dev)
81 /* Set the state of ISH HW to start */
82 ret = ish_hw_start(dev);
84 dev_err(dev->devc, "ISH: hw start failed.\n");
88 /* Start the inter process communication to ISH processor */
89 ret = ishtp_start(dev);
91 dev_err(dev->devc, "ISHTP: Protocol init failed.\n");
98 static const struct pci_device_id ish_invalid_pci_ids[] = {
99 /* Mehlow platform special pci ids */
100 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0xA309)},
101 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0xA30A)},
106 * ish_probe() - PCI driver probe callback
108 * @ent: pci device id
110 * Initialize PCI function, setup interrupt and call for ISH initialization
112 * Return: 0 for success or error code for failure
114 static int ish_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
116 struct ishtp_device *dev;
120 /* Check for invalid platforms for ISH support */
121 if (pci_dev_present(ish_invalid_pci_ids))
125 ret = pci_enable_device(pdev);
127 dev_err(&pdev->dev, "ISH: Failed to enable PCI device\n");
131 /* set PCI host mastering */
132 pci_set_master(pdev);
134 /* pci request regions for ISH driver */
135 ret = pci_request_regions(pdev, KBUILD_MODNAME);
137 dev_err(&pdev->dev, "ISH: Failed to get PCI regions\n");
141 /* allocates and initializes the ISH dev structure */
142 dev = ish_dev_init(pdev);
145 goto release_regions;
148 dev->print_log = ish_event_tracer;
150 /* mapping IO device memory */
151 hw->mem_addr = pci_iomap(pdev, 0, 0);
153 dev_err(&pdev->dev, "ISH: mapping I/O range failure\n");
160 pdev->dev_flags |= PCI_DEV_FLAGS_NO_D3;
162 /* request and enable interrupt */
163 ret = request_irq(pdev->irq, ish_irq_handler, IRQF_SHARED,
164 KBUILD_MODNAME, dev);
166 dev_err(&pdev->dev, "ISH: request IRQ failure (%d)\n",
171 dev_set_drvdata(dev->devc, dev);
173 init_waitqueue_head(&dev->suspend_wait);
174 init_waitqueue_head(&dev->resume_wait);
183 free_irq(pdev->irq, dev);
185 pci_iounmap(pdev, hw->mem_addr);
187 pci_release_regions(pdev);
189 pci_clear_master(pdev);
190 pci_disable_device(pdev);
191 dev_err(&pdev->dev, "ISH: PCI driver initialization failed.\n");
197 * ish_remove() - PCI driver remove callback
200 * This function does cleanup of ISH on pci remove callback
202 static void ish_remove(struct pci_dev *pdev)
204 struct ishtp_device *ishtp_dev = pci_get_drvdata(pdev);
205 struct ish_hw *hw = to_ish_hw(ishtp_dev);
207 ishtp_bus_remove_all_clients(ishtp_dev, false);
208 ish_device_disable(ishtp_dev);
210 free_irq(pdev->irq, ishtp_dev);
211 pci_iounmap(pdev, hw->mem_addr);
212 pci_release_regions(pdev);
213 pci_clear_master(pdev);
214 pci_disable_device(pdev);
217 static struct device __maybe_unused *ish_resume_device;
219 /* 50ms to get resume response */
220 #define WAIT_FOR_RESUME_ACK_MS 50
223 * ish_resume_handler() - Work function to complete resume
226 * The resume work function to complete resume function asynchronously.
227 * There are two resume paths, one where ISH is not powered off,
228 * in that case a simple resume message is enough, others we need
231 static void __maybe_unused ish_resume_handler(struct work_struct *work)
233 struct pci_dev *pdev = to_pci_dev(ish_resume_device);
234 struct ishtp_device *dev = pci_get_drvdata(pdev);
238 /* Get ISH FW status */
239 fwsts = IPC_GET_ISH_FWSTS(dev->ops->get_fw_status(dev));
242 * If currently, in ISH FW, sensor app is loaded or beyond that,
243 * it means ISH isn't powered off, in this case, send a resume message.
245 if (fwsts >= FWSTS_SENSOR_APP_LOADED) {
246 ishtp_send_resume(dev);
248 /* Waiting to get resume response */
249 if (dev->resume_flag)
250 ret = wait_event_interruptible_timeout(dev->resume_wait,
252 msecs_to_jiffies(WAIT_FOR_RESUME_ACK_MS));
256 * If in ISH FW, sensor app isn't loaded yet, or no resume response.
257 * That means this platform is not S0ix compatible, or something is
258 * wrong with ISH FW. So on resume, full reboot of ISH processor will
259 * happen, so need to go through init sequence again.
261 if (dev->resume_flag)
266 * ish_suspend() - ISH suspend callback
267 * @device: device pointer
269 * ISH suspend callback
271 * Return: 0 to the pm core
273 static int __maybe_unused ish_suspend(struct device *device)
275 struct pci_dev *pdev = to_pci_dev(device);
276 struct ishtp_device *dev = pci_get_drvdata(pdev);
278 enable_irq_wake(pdev->irq);
280 * If previous suspend hasn't been asnwered then ISH is likely dead,
281 * don't attempt nested notification
283 if (dev->suspend_flag)
286 dev->resume_flag = 0;
287 dev->suspend_flag = 1;
288 ishtp_send_suspend(dev);
290 /* 25 ms should be enough for live ISH to flush all IPC buf */
291 if (dev->suspend_flag)
292 wait_event_interruptible_timeout(dev->suspend_wait,
294 msecs_to_jiffies(25));
299 static __maybe_unused DECLARE_WORK(resume_work, ish_resume_handler);
301 * ish_resume() - ISH resume callback
302 * @device: device pointer
304 * ISH resume callback
306 * Return: 0 to the pm core
308 static int __maybe_unused ish_resume(struct device *device)
310 struct pci_dev *pdev = to_pci_dev(device);
311 struct ishtp_device *dev = pci_get_drvdata(pdev);
313 ish_resume_device = device;
314 dev->resume_flag = 1;
316 disable_irq_wake(pdev->irq);
317 schedule_work(&resume_work);
322 static SIMPLE_DEV_PM_OPS(ish_pm_ops, ish_suspend, ish_resume);
324 static struct pci_driver ish_driver = {
325 .name = KBUILD_MODNAME,
326 .id_table = ish_pci_tbl,
328 .remove = ish_remove,
329 .driver.pm = &ish_pm_ops,
332 module_pci_driver(ish_driver);
334 /* Original author */
335 MODULE_AUTHOR("Daniel Drubin <daniel.drubin@intel.com>");
336 /* Adoption to upstream Linux kernel */
337 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
339 MODULE_DESCRIPTION("Intel(R) Integrated Sensor Hub PCI Device Driver");
340 MODULE_LICENSE("GPL");