1
2
3
4
5
6
7
8#include <linux/module.h>
9#include <linux/moduleparam.h>
10#include <linux/kernel.h>
11#include <linux/device.h>
12#include <linux/fs.h>
13#include <linux/errno.h>
14#include <linux/types.h>
15#include <linux/pci.h>
16#include <linux/sched.h>
17#include <linux/suspend.h>
18#include <linux/interrupt.h>
19#include <linux/workqueue.h>
20#define CREATE_TRACE_POINTS
21#include <trace/events/intel_ish.h>
22#include "ishtp-dev.h"
23#include "hw-ish.h"
24
25static const struct pci_device_id ish_pci_tbl[] = {
26 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CHV_DEVICE_ID)},
27 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, BXT_Ax_DEVICE_ID)},
28 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, BXT_Bx_DEVICE_ID)},
29 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, APL_Ax_DEVICE_ID)},
30 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, SPT_Ax_DEVICE_ID)},
31 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CNL_Ax_DEVICE_ID)},
32 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, GLK_Ax_DEVICE_ID)},
33 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CNL_H_DEVICE_ID)},
34 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, ICL_MOBILE_DEVICE_ID)},
35 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, SPT_H_DEVICE_ID)},
36 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CML_LP_DEVICE_ID)},
37 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CMP_H_DEVICE_ID)},
38 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, EHL_Ax_DEVICE_ID)},
39 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, TGL_LP_DEVICE_ID)},
40 {0, }
41};
42MODULE_DEVICE_TABLE(pci, ish_pci_tbl);
43
44
45
46
47
48
49
50
51static __printf(2, 3)
52void ish_event_tracer(struct ishtp_device *dev, const char *format, ...)
53{
54 if (trace_ishtp_dump_enabled()) {
55 va_list args;
56 char tmp_buf[100];
57
58 va_start(args, format);
59 vsnprintf(tmp_buf, sizeof(tmp_buf), format, args);
60 va_end(args);
61
62 trace_ishtp_dump(tmp_buf);
63 }
64}
65
66
67
68
69
70
71
72
73
74
75
76static int ish_init(struct ishtp_device *dev)
77{
78 int ret;
79
80
81 ret = ish_hw_start(dev);
82 if (ret) {
83 dev_err(dev->devc, "ISH: hw start failed.\n");
84 return ret;
85 }
86
87
88 ret = ishtp_start(dev);
89 if (ret) {
90 dev_err(dev->devc, "ISHTP: Protocol init failed.\n");
91 return ret;
92 }
93
94 return 0;
95}
96
97static const struct pci_device_id ish_invalid_pci_ids[] = {
98
99 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0xA309)},
100 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0xA30A)},
101 {}
102};
103
104static inline bool ish_should_enter_d0i3(struct pci_dev *pdev)
105{
106 return !pm_suspend_via_firmware() || pdev->device == CHV_DEVICE_ID;
107}
108
109static inline bool ish_should_leave_d0i3(struct pci_dev *pdev)
110{
111 return !pm_resume_via_firmware() || pdev->device == CHV_DEVICE_ID;
112}
113
114
115
116
117
118
119
120
121
122
123static int ish_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
124{
125 int ret;
126 struct ish_hw *hw;
127 unsigned long irq_flag = 0;
128 struct ishtp_device *ishtp;
129 struct device *dev = &pdev->dev;
130
131
132 if (pci_dev_present(ish_invalid_pci_ids))
133 return -ENODEV;
134
135
136 ret = pcim_enable_device(pdev);
137 if (ret) {
138 dev_err(dev, "ISH: Failed to enable PCI device\n");
139 return ret;
140 }
141
142
143 pci_set_master(pdev);
144
145
146 ret = pcim_iomap_regions(pdev, 1 << 0, KBUILD_MODNAME);
147 if (ret) {
148 dev_err(dev, "ISH: Failed to get PCI regions\n");
149 return ret;
150 }
151
152
153 ishtp = ish_dev_init(pdev);
154 if (!ishtp) {
155 ret = -ENOMEM;
156 return ret;
157 }
158 hw = to_ish_hw(ishtp);
159 ishtp->print_log = ish_event_tracer;
160
161
162 hw->mem_addr = pcim_iomap_table(pdev)[0];
163 ishtp->pdev = pdev;
164
165
166 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
167 if (!pdev->msi_enabled && !pdev->msix_enabled)
168 irq_flag = IRQF_SHARED;
169
170 ret = devm_request_irq(dev, pdev->irq, ish_irq_handler,
171 irq_flag, KBUILD_MODNAME, ishtp);
172 if (ret) {
173 dev_err(dev, "ISH: request IRQ %d failed\n", pdev->irq);
174 return ret;
175 }
176
177 dev_set_drvdata(ishtp->devc, ishtp);
178
179 init_waitqueue_head(&ishtp->suspend_wait);
180 init_waitqueue_head(&ishtp->resume_wait);
181
182 ret = ish_init(ishtp);
183 if (ret)
184 return ret;
185
186 return 0;
187}
188
189
190
191
192
193
194
195static void ish_remove(struct pci_dev *pdev)
196{
197 struct ishtp_device *ishtp_dev = pci_get_drvdata(pdev);
198
199 ishtp_bus_remove_all_clients(ishtp_dev, false);
200 ish_device_disable(ishtp_dev);
201}
202
203static struct device __maybe_unused *ish_resume_device;
204
205
206#define WAIT_FOR_RESUME_ACK_MS 50
207
208
209
210
211
212
213
214
215
216
217static void __maybe_unused ish_resume_handler(struct work_struct *work)
218{
219 struct pci_dev *pdev = to_pci_dev(ish_resume_device);
220 struct ishtp_device *dev = pci_get_drvdata(pdev);
221 int ret;
222
223 if (ish_should_leave_d0i3(pdev) && !dev->suspend_flag) {
224 disable_irq_wake(pdev->irq);
225
226 ishtp_send_resume(dev);
227
228
229 if (dev->resume_flag)
230 ret = wait_event_interruptible_timeout(dev->resume_wait,
231 !dev->resume_flag,
232 msecs_to_jiffies(WAIT_FOR_RESUME_ACK_MS));
233
234
235
236
237
238 if (dev->resume_flag)
239 ish_init(dev);
240 } else {
241
242
243
244
245 ish_init(dev);
246 }
247}
248
249
250
251
252
253
254
255
256
257static int __maybe_unused ish_suspend(struct device *device)
258{
259 struct pci_dev *pdev = to_pci_dev(device);
260 struct ishtp_device *dev = pci_get_drvdata(pdev);
261
262 if (ish_should_enter_d0i3(pdev)) {
263
264
265
266
267 if (dev->suspend_flag)
268 return 0;
269
270 dev->resume_flag = 0;
271 dev->suspend_flag = 1;
272 ishtp_send_suspend(dev);
273
274
275 if (dev->suspend_flag)
276 wait_event_interruptible_timeout(dev->suspend_wait,
277 !dev->suspend_flag,
278 msecs_to_jiffies(25));
279
280 if (dev->suspend_flag) {
281
282
283
284
285 ish_disable_dma(dev);
286 } else {
287
288
289
290
291 pci_save_state(pdev);
292
293 enable_irq_wake(pdev->irq);
294 }
295 } else {
296
297
298
299
300 ish_disable_dma(dev);
301 }
302
303 return 0;
304}
305
306static __maybe_unused DECLARE_WORK(resume_work, ish_resume_handler);
307
308
309
310
311
312
313
314
315static int __maybe_unused ish_resume(struct device *device)
316{
317 struct pci_dev *pdev = to_pci_dev(device);
318 struct ishtp_device *dev = pci_get_drvdata(pdev);
319
320 ish_resume_device = device;
321 dev->resume_flag = 1;
322
323 schedule_work(&resume_work);
324
325 return 0;
326}
327
328static SIMPLE_DEV_PM_OPS(ish_pm_ops, ish_suspend, ish_resume);
329
330static struct pci_driver ish_driver = {
331 .name = KBUILD_MODNAME,
332 .id_table = ish_pci_tbl,
333 .probe = ish_probe,
334 .remove = ish_remove,
335 .driver.pm = &ish_pm_ops,
336};
337
338module_pci_driver(ish_driver);
339
340
341MODULE_AUTHOR("Daniel Drubin <daniel.drubin@intel.com>");
342
343MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
344
345MODULE_DESCRIPTION("Intel(R) Integrated Sensor Hub PCI Device Driver");
346MODULE_LICENSE("GPL");
347