HID: intel-ish-hid: ipc: check FW status to distinguish ISH resume paths
[platform/kernel/linux-exynos.git] / drivers / hid / intel-ish-hid / ipc / pci-ish.c
1 /*
2  * PCI glue for ISHTP provider device (ISH) driver
3  *
4  * Copyright (c) 2014-2016, Intel Corporation.
5  *
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.
9  *
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
13  * more details.
14  */
15
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/kernel.h>
19 #include <linux/device.h>
20 #include <linux/fs.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"
31 #include "hw-ish.h"
32
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)},
39         {0, }
40 };
41 MODULE_DEVICE_TABLE(pci, ish_pci_tbl);
42
43 /**
44  * ish_event_tracer() - Callback function to dump trace messages
45  * @dev:        ishtp device
46  * @format:     printf style format
47  *
48  * Callback to direct log messages to Linux trace buffers
49  */
50 static __printf(2, 3)
51 void ish_event_tracer(struct ishtp_device *dev, const char *format, ...)
52 {
53         if (trace_ishtp_dump_enabled()) {
54                 va_list args;
55                 char tmp_buf[100];
56
57                 va_start(args, format);
58                 vsnprintf(tmp_buf, sizeof(tmp_buf), format, args);
59                 va_end(args);
60
61                 trace_ishtp_dump(tmp_buf);
62         }
63 }
64
65 /**
66  * ish_init() - Init function
67  * @dev:        ishtp device
68  *
69  * This function initialize wait queues for suspend/resume and call
70  * calls hadware initialization function. This will initiate
71  * startup sequence
72  *
73  * Return: 0 for success or error code for failure
74  */
75 static int ish_init(struct ishtp_device *dev)
76 {
77         int ret;
78
79         /* Set the state of ISH HW to start */
80         ret = ish_hw_start(dev);
81         if (ret) {
82                 dev_err(dev->devc, "ISH: hw start failed.\n");
83                 return ret;
84         }
85
86         /* Start the inter process communication to ISH processor */
87         ret = ishtp_start(dev);
88         if (ret) {
89                 dev_err(dev->devc, "ISHTP: Protocol init failed.\n");
90                 return ret;
91         }
92
93         return 0;
94 }
95
96 /**
97  * ish_probe() - PCI driver probe callback
98  * @pdev:       pci device
99  * @ent:        pci device id
100  *
101  * Initialize PCI function, setup interrupt and call for ISH initialization
102  *
103  * Return: 0 for success or error code for failure
104  */
105 static int ish_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
106 {
107         struct ishtp_device *dev;
108         struct ish_hw *hw;
109         int     ret;
110
111         /* enable pci dev */
112         ret = pci_enable_device(pdev);
113         if (ret) {
114                 dev_err(&pdev->dev, "ISH: Failed to enable PCI device\n");
115                 return ret;
116         }
117
118         /* set PCI host mastering */
119         pci_set_master(pdev);
120
121         /* pci request regions for ISH driver */
122         ret = pci_request_regions(pdev, KBUILD_MODNAME);
123         if (ret) {
124                 dev_err(&pdev->dev, "ISH: Failed to get PCI regions\n");
125                 goto disable_device;
126         }
127
128         /* allocates and initializes the ISH dev structure */
129         dev = ish_dev_init(pdev);
130         if (!dev) {
131                 ret = -ENOMEM;
132                 goto release_regions;
133         }
134         hw = to_ish_hw(dev);
135         dev->print_log = ish_event_tracer;
136
137         /* mapping IO device memory */
138         hw->mem_addr = pci_iomap(pdev, 0, 0);
139         if (!hw->mem_addr) {
140                 dev_err(&pdev->dev, "ISH: mapping I/O range failure\n");
141                 ret = -ENOMEM;
142                 goto free_device;
143         }
144
145         dev->pdev = pdev;
146
147         pdev->dev_flags |= PCI_DEV_FLAGS_NO_D3;
148
149         /* request and enable interrupt */
150         ret = request_irq(pdev->irq, ish_irq_handler, IRQF_SHARED,
151                           KBUILD_MODNAME, dev);
152         if (ret) {
153                 dev_err(&pdev->dev, "ISH: request IRQ failure (%d)\n",
154                         pdev->irq);
155                 goto free_device;
156         }
157
158         dev_set_drvdata(dev->devc, dev);
159
160         init_waitqueue_head(&dev->suspend_wait);
161         init_waitqueue_head(&dev->resume_wait);
162
163         ret = ish_init(dev);
164         if (ret)
165                 goto free_irq;
166
167         return 0;
168
169 free_irq:
170         free_irq(pdev->irq, dev);
171 free_device:
172         pci_iounmap(pdev, hw->mem_addr);
173         kfree(dev);
174 release_regions:
175         pci_release_regions(pdev);
176 disable_device:
177         pci_clear_master(pdev);
178         pci_disable_device(pdev);
179         dev_err(&pdev->dev, "ISH: PCI driver initialization failed.\n");
180
181         return ret;
182 }
183
184 /**
185  * ish_remove() - PCI driver remove callback
186  * @pdev:       pci device
187  *
188  * This function does cleanup of ISH on pci remove callback
189  */
190 static void ish_remove(struct pci_dev *pdev)
191 {
192         struct ishtp_device *ishtp_dev = pci_get_drvdata(pdev);
193         struct ish_hw *hw = to_ish_hw(ishtp_dev);
194
195         ishtp_bus_remove_all_clients(ishtp_dev, false);
196         ish_device_disable(ishtp_dev);
197
198         free_irq(pdev->irq, ishtp_dev);
199         pci_iounmap(pdev, hw->mem_addr);
200         pci_release_regions(pdev);
201         pci_clear_master(pdev);
202         pci_disable_device(pdev);
203         kfree(ishtp_dev);
204 }
205
206 #ifdef CONFIG_PM
207 static struct device *ish_resume_device;
208
209 /* 50ms to get resume response */
210 #define WAIT_FOR_RESUME_ACK_MS          50
211
212 /**
213  * ish_resume_handler() - Work function to complete resume
214  * @work:       work struct
215  *
216  * The resume work function to complete resume function asynchronously.
217  * There are two resume paths, one where ISH is not powered off,
218  * in that case a simple resume message is enough, others we need
219  * a reset sequence.
220  */
221 static void ish_resume_handler(struct work_struct *work)
222 {
223         struct pci_dev *pdev = to_pci_dev(ish_resume_device);
224         struct ishtp_device *dev = pci_get_drvdata(pdev);
225         uint32_t fwsts;
226         int ret;
227
228         /* Get ISH FW status */
229         fwsts = IPC_GET_ISH_FWSTS(dev->ops->get_fw_status(dev));
230
231         /*
232          * If currently, in ISH FW, sensor app is loaded or beyond that,
233          * it means ISH isn't powered off, in this case, send a resume message.
234          */
235         if (fwsts >= FWSTS_SENSOR_APP_LOADED) {
236                 ishtp_send_resume(dev);
237
238                 /* Waiting to get resume response */
239                 if (dev->resume_flag)
240                         ret = wait_event_interruptible_timeout(dev->resume_wait,
241                                 !dev->resume_flag,
242                                 msecs_to_jiffies(WAIT_FOR_RESUME_ACK_MS));
243         }
244
245         /*
246          * If in ISH FW, sensor app isn't loaded yet, or no resume response.
247          * That means this platform is not S0ix compatible, or something is
248          * wrong with ISH FW. So on resume, full reboot of ISH processor will
249          * happen, so need to go through init sequence again.
250          */
251         if (dev->resume_flag)
252                 ish_init(dev);
253 }
254
255 /**
256  * ish_suspend() - ISH suspend callback
257  * @device:     device pointer
258  *
259  * ISH suspend callback
260  *
261  * Return: 0 to the pm core
262  */
263 static int ish_suspend(struct device *device)
264 {
265         struct pci_dev *pdev = to_pci_dev(device);
266         struct ishtp_device *dev = pci_get_drvdata(pdev);
267
268         enable_irq_wake(pdev->irq);
269         /*
270          * If previous suspend hasn't been asnwered then ISH is likely dead,
271          * don't attempt nested notification
272          */
273         if (dev->suspend_flag)
274                 return  0;
275
276         dev->resume_flag = 0;
277         dev->suspend_flag = 1;
278         ishtp_send_suspend(dev);
279
280         /* 25 ms should be enough for live ISH to flush all IPC buf */
281         if (dev->suspend_flag)
282                 wait_event_interruptible_timeout(dev->suspend_wait,
283                                                  !dev->suspend_flag,
284                                                   msecs_to_jiffies(25));
285
286         return 0;
287 }
288
289 static DECLARE_WORK(resume_work, ish_resume_handler);
290 /**
291  * ish_resume() - ISH resume callback
292  * @device:     device pointer
293  *
294  * ISH resume callback
295  *
296  * Return: 0 to the pm core
297  */
298 static int ish_resume(struct device *device)
299 {
300         struct pci_dev *pdev = to_pci_dev(device);
301         struct ishtp_device *dev = pci_get_drvdata(pdev);
302
303         ish_resume_device = device;
304         dev->resume_flag = 1;
305
306         disable_irq_wake(pdev->irq);
307         schedule_work(&resume_work);
308
309         return 0;
310 }
311
312 static const struct dev_pm_ops ish_pm_ops = {
313         .suspend = ish_suspend,
314         .resume = ish_resume,
315 };
316 #define ISHTP_ISH_PM_OPS        (&ish_pm_ops)
317 #else
318 #define ISHTP_ISH_PM_OPS        NULL
319 #endif /* CONFIG_PM */
320
321 static struct pci_driver ish_driver = {
322         .name = KBUILD_MODNAME,
323         .id_table = ish_pci_tbl,
324         .probe = ish_probe,
325         .remove = ish_remove,
326         .driver.pm = ISHTP_ISH_PM_OPS,
327 };
328
329 module_pci_driver(ish_driver);
330
331 /* Original author */
332 MODULE_AUTHOR("Daniel Drubin <daniel.drubin@intel.com>");
333 /* Adoption to upstream Linux kernel */
334 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
335
336 MODULE_DESCRIPTION("Intel(R) Integrated Sensor Hub PCI Device Driver");
337 MODULE_LICENSE("GPL");