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