]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/gpu/drm/i915/intel_lpe_audio.c
x86: Don't include linux/irq.h from asm/hardirq.h
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_lpe_audio.c
1 /*
2 * Copyright © 2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
25 * Jerome Anand <jerome.anand@intel.com>
26 * based on VED patches
27 *
28 */
29
30 /**
31 * DOC: LPE Audio integration for HDMI or DP playback
32 *
33 * Motivation:
34 * Atom platforms (e.g. valleyview and cherryTrail) integrates a DMA-based
35 * interface as an alternative to the traditional HDaudio path. While this
36 * mode is unrelated to the LPE aka SST audio engine, the documentation refers
37 * to this mode as LPE so we keep this notation for the sake of consistency.
38 *
39 * The interface is handled by a separate standalone driver maintained in the
40 * ALSA subsystem for simplicity. To minimize the interaction between the two
41 * subsystems, a bridge is setup between the hdmi-lpe-audio and i915:
42 * 1. Create a platform device to share MMIO/IRQ resources
43 * 2. Make the platform device child of i915 device for runtime PM.
44 * 3. Create IRQ chip to forward the LPE audio irqs.
45 * the hdmi-lpe-audio driver probes the lpe audio device and creates a new
46 * sound card
47 *
48 * Threats:
49 * Due to the restriction in Linux platform device model, user need manually
50 * uninstall the hdmi-lpe-audio driver before uninstalling i915 module,
51 * otherwise we might run into use-after-free issues after i915 removes the
52 * platform device: even though hdmi-lpe-audio driver is released, the modules
53 * is still in "installed" status.
54 *
55 * Implementation:
56 * The MMIO/REG platform resources are created according to the registers
57 * specification.
58 * When forwarding LPE audio irqs, the flow control handler selection depends
59 * on the platform, for example on valleyview handle_simple_irq is enough.
60 *
61 */
62
63 #include <linux/acpi.h>
64 #include <linux/device.h>
65 #include <linux/irq.h>
66 #include <linux/pci.h>
67 #include <linux/pm_runtime.h>
68
69 #include "i915_drv.h"
70 #include <linux/delay.h>
71 #include <drm/intel_lpe_audio.h>
72
73 #define HAS_LPE_AUDIO(dev_priv) ((dev_priv)->lpe_audio.platdev != NULL)
74
75 static struct platform_device *
76 lpe_audio_platdev_create(struct drm_i915_private *dev_priv)
77 {
78 int ret;
79 struct drm_device *dev = &dev_priv->drm;
80 struct platform_device_info pinfo = {};
81 struct resource *rsc;
82 struct platform_device *platdev;
83 struct intel_hdmi_lpe_audio_pdata *pdata;
84
85 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
86 if (!pdata)
87 return ERR_PTR(-ENOMEM);
88
89 rsc = kcalloc(2, sizeof(*rsc), GFP_KERNEL);
90 if (!rsc) {
91 kfree(pdata);
92 return ERR_PTR(-ENOMEM);
93 }
94
95 rsc[0].start = rsc[0].end = dev_priv->lpe_audio.irq;
96 rsc[0].flags = IORESOURCE_IRQ;
97 rsc[0].name = "hdmi-lpe-audio-irq";
98
99 rsc[1].start = pci_resource_start(dev->pdev, 0) +
100 I915_HDMI_LPE_AUDIO_BASE;
101 rsc[1].end = pci_resource_start(dev->pdev, 0) +
102 I915_HDMI_LPE_AUDIO_BASE + I915_HDMI_LPE_AUDIO_SIZE - 1;
103 rsc[1].flags = IORESOURCE_MEM;
104 rsc[1].name = "hdmi-lpe-audio-mmio";
105
106 pinfo.parent = dev->dev;
107 pinfo.name = "hdmi-lpe-audio";
108 pinfo.id = -1;
109 pinfo.res = rsc;
110 pinfo.num_res = 2;
111 pinfo.data = pdata;
112 pinfo.size_data = sizeof(*pdata);
113 pinfo.dma_mask = DMA_BIT_MASK(32);
114
115 pdata->num_pipes = INTEL_INFO(dev_priv)->num_pipes;
116 pdata->num_ports = IS_CHERRYVIEW(dev_priv) ? 3 : 2; /* B,C,D or B,C */
117 pdata->port[0].pipe = -1;
118 pdata->port[1].pipe = -1;
119 pdata->port[2].pipe = -1;
120 spin_lock_init(&pdata->lpe_audio_slock);
121
122 platdev = platform_device_register_full(&pinfo);
123 if (IS_ERR(platdev)) {
124 ret = PTR_ERR(platdev);
125 DRM_ERROR("Failed to allocate LPE audio platform device\n");
126 goto err;
127 }
128
129 kfree(rsc);
130
131 pm_runtime_forbid(&platdev->dev);
132 pm_runtime_set_active(&platdev->dev);
133 pm_runtime_enable(&platdev->dev);
134
135 return platdev;
136
137 err:
138 kfree(rsc);
139 kfree(pdata);
140 return ERR_PTR(ret);
141 }
142
143 static void lpe_audio_platdev_destroy(struct drm_i915_private *dev_priv)
144 {
145 /* XXX Note that platform_device_register_full() allocates a dma_mask
146 * and never frees it. We can't free it here as we cannot guarantee
147 * this is the last reference (i.e. that the dma_mask will not be
148 * used after our unregister). So ee choose to leak the sizeof(u64)
149 * allocation here - it should be fixed in the platform_device rather
150 * than us fiddle with its internals.
151 */
152
153 platform_device_unregister(dev_priv->lpe_audio.platdev);
154 }
155
156 static void lpe_audio_irq_unmask(struct irq_data *d)
157 {
158 }
159
160 static void lpe_audio_irq_mask(struct irq_data *d)
161 {
162 }
163
164 static struct irq_chip lpe_audio_irqchip = {
165 .name = "hdmi_lpe_audio_irqchip",
166 .irq_mask = lpe_audio_irq_mask,
167 .irq_unmask = lpe_audio_irq_unmask,
168 };
169
170 static int lpe_audio_irq_init(struct drm_i915_private *dev_priv)
171 {
172 int irq = dev_priv->lpe_audio.irq;
173
174 WARN_ON(!intel_irqs_enabled(dev_priv));
175 irq_set_chip_and_handler_name(irq,
176 &lpe_audio_irqchip,
177 handle_simple_irq,
178 "hdmi_lpe_audio_irq_handler");
179
180 static const struct pci_device_id irq_quirk_ids[] = {
181 /* Dell Wyse 3040 */
182 {PCI_DEVICE_SUB(PCI_VENDOR_ID_INTEL, 0x22b0, 0x1028, 0x07c1)},
183 {}
184 };
185
186 if (pci_dev_present(irq_quirk_ids))
187 return 0;
188 return irq_set_chip_data(irq, dev_priv);
189 }
190
191 static bool lpe_audio_detect(struct drm_i915_private *dev_priv)
192 {
193 int lpe_present = false;
194
195 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
196 static const struct pci_device_id atom_hdaudio_ids[] = {
197 /* Baytrail */
198 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x0f04)},
199 /* Braswell */
200 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2284)},
201 {}
202 };
203
204 if (!pci_dev_present(atom_hdaudio_ids)) {
205 DRM_INFO("HDaudio controller not detected, using LPE audio instead\n");
206 lpe_present = true;
207 }
208 }
209 return lpe_present;
210 }
211
212 static int lpe_audio_setup(struct drm_i915_private *dev_priv)
213 {
214 int ret;
215
216 dev_priv->lpe_audio.irq = irq_alloc_desc(0);
217 if (dev_priv->lpe_audio.irq < 0) {
218 DRM_ERROR("Failed to allocate IRQ desc: %d\n",
219 dev_priv->lpe_audio.irq);
220 ret = dev_priv->lpe_audio.irq;
221 goto err;
222 }
223
224 DRM_DEBUG("irq = %d\n", dev_priv->lpe_audio.irq);
225
226 ret = lpe_audio_irq_init(dev_priv);
227
228 if (ret) {
229 DRM_ERROR("Failed to initialize irqchip for lpe audio: %d\n",
230 ret);
231 goto err_free_irq;
232 }
233
234 dev_priv->lpe_audio.platdev = lpe_audio_platdev_create(dev_priv);
235
236 if (IS_ERR(dev_priv->lpe_audio.platdev)) {
237 ret = PTR_ERR(dev_priv->lpe_audio.platdev);
238 DRM_ERROR("Failed to create lpe audio platform device: %d\n",
239 ret);
240 goto err_free_irq;
241 }
242
243 /* enable chicken bit; at least this is required for Dell Wyse 3040
244 * with DP outputs (but only sometimes by some reason!)
245 */
246 I915_WRITE(VLV_AUD_CHICKEN_BIT_REG, VLV_CHICKEN_BIT_DBG_ENABLE);
247
248 return 0;
249 err_free_irq:
250 irq_free_desc(dev_priv->lpe_audio.irq);
251 err:
252 dev_priv->lpe_audio.irq = -1;
253 dev_priv->lpe_audio.platdev = NULL;
254 return ret;
255 }
256
257 /**
258 * intel_lpe_audio_irq_handler() - forwards the LPE audio irq
259 * @dev_priv: the i915 drm device private data
260 *
261 * the LPE Audio irq is forwarded to the irq handler registered by LPE audio
262 * driver.
263 */
264 void intel_lpe_audio_irq_handler(struct drm_i915_private *dev_priv)
265 {
266 int ret;
267
268 if (!HAS_LPE_AUDIO(dev_priv))
269 return;
270
271 ret = generic_handle_irq(dev_priv->lpe_audio.irq);
272 if (ret)
273 DRM_ERROR_RATELIMITED("error handling LPE audio irq: %d\n",
274 ret);
275 }
276
277 /**
278 * intel_lpe_audio_init() - detect and setup the bridge between HDMI LPE Audio
279 * driver and i915
280 * @dev_priv: the i915 drm device private data
281 *
282 * Return: 0 if successful. non-zero if detection or
283 * llocation/initialization fails
284 */
285 int intel_lpe_audio_init(struct drm_i915_private *dev_priv)
286 {
287 int ret = -ENODEV;
288
289 if (lpe_audio_detect(dev_priv)) {
290 ret = lpe_audio_setup(dev_priv);
291 if (ret < 0)
292 DRM_ERROR("failed to setup LPE Audio bridge\n");
293 }
294 return ret;
295 }
296
297 /**
298 * intel_lpe_audio_teardown() - destroy the bridge between HDMI LPE
299 * audio driver and i915
300 * @dev_priv: the i915 drm device private data
301 *
302 * release all the resources for LPE audio <-> i915 bridge.
303 */
304 void intel_lpe_audio_teardown(struct drm_i915_private *dev_priv)
305 {
306 struct irq_desc *desc;
307
308 if (!HAS_LPE_AUDIO(dev_priv))
309 return;
310
311 desc = irq_to_desc(dev_priv->lpe_audio.irq);
312
313 lpe_audio_platdev_destroy(dev_priv);
314
315 irq_free_desc(dev_priv->lpe_audio.irq);
316 }
317
318
319 /**
320 * intel_lpe_audio_notify() - notify lpe audio event
321 * audio driver and i915
322 * @dev_priv: the i915 drm device private data
323 * @pipe: pipe
324 * @port: port
325 * @eld : ELD data
326 * @ls_clock: Link symbol clock in kHz
327 * @dp_output: Driving a DP output?
328 *
329 * Notify lpe audio driver of eld change.
330 */
331 void intel_lpe_audio_notify(struct drm_i915_private *dev_priv,
332 enum pipe pipe, enum port port,
333 const void *eld, int ls_clock, bool dp_output)
334 {
335 unsigned long irqflags;
336 struct intel_hdmi_lpe_audio_pdata *pdata;
337 struct intel_hdmi_lpe_audio_port_pdata *ppdata;
338 u32 audio_enable;
339
340 if (!HAS_LPE_AUDIO(dev_priv))
341 return;
342
343 pdata = dev_get_platdata(&dev_priv->lpe_audio.platdev->dev);
344 ppdata = &pdata->port[port - PORT_B];
345
346 spin_lock_irqsave(&pdata->lpe_audio_slock, irqflags);
347
348 audio_enable = I915_READ(VLV_AUD_PORT_EN_DBG(port));
349
350 if (eld != NULL) {
351 memcpy(ppdata->eld, eld, HDMI_MAX_ELD_BYTES);
352 ppdata->pipe = pipe;
353 ppdata->ls_clock = ls_clock;
354 ppdata->dp_output = dp_output;
355
356 /* Unmute the amp for both DP and HDMI */
357 I915_WRITE(VLV_AUD_PORT_EN_DBG(port),
358 audio_enable & ~VLV_AMP_MUTE);
359 } else {
360 memset(ppdata->eld, 0, HDMI_MAX_ELD_BYTES);
361 ppdata->pipe = -1;
362 ppdata->ls_clock = 0;
363 ppdata->dp_output = false;
364
365 /* Mute the amp for both DP and HDMI */
366 I915_WRITE(VLV_AUD_PORT_EN_DBG(port),
367 audio_enable | VLV_AMP_MUTE);
368 }
369
370 if (pdata->notify_audio_lpe)
371 pdata->notify_audio_lpe(dev_priv->lpe_audio.platdev, port - PORT_B);
372
373 spin_unlock_irqrestore(&pdata->lpe_audio_slock, irqflags);
374 }