]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/gpu/drm/i915/intel_lpe_audio.c
b6db221c007e1d1e0c0dccdd5925aa622c05662c
[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/pci.h>
66 #include <linux/pm_runtime.h>
67
68 #include "i915_drv.h"
69 #include <linux/delay.h>
70 #include <drm/intel_lpe_audio.h>
71
72 #define HAS_LPE_AUDIO(dev_priv) ((dev_priv)->lpe_audio.platdev != NULL)
73
74 static struct platform_device *
75 lpe_audio_platdev_create(struct drm_i915_private *dev_priv)
76 {
77 int ret;
78 struct drm_device *dev = &dev_priv->drm;
79 struct platform_device_info pinfo = {};
80 struct resource *rsc;
81 struct platform_device *platdev;
82 struct intel_hdmi_lpe_audio_pdata *pdata;
83
84 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
85 if (!pdata)
86 return ERR_PTR(-ENOMEM);
87
88 rsc = kcalloc(2, sizeof(*rsc), GFP_KERNEL);
89 if (!rsc) {
90 kfree(pdata);
91 return ERR_PTR(-ENOMEM);
92 }
93
94 rsc[0].start = rsc[0].end = dev_priv->lpe_audio.irq;
95 rsc[0].flags = IORESOURCE_IRQ;
96 rsc[0].name = "hdmi-lpe-audio-irq";
97
98 rsc[1].start = pci_resource_start(dev->pdev, 0) +
99 I915_HDMI_LPE_AUDIO_BASE;
100 rsc[1].end = pci_resource_start(dev->pdev, 0) +
101 I915_HDMI_LPE_AUDIO_BASE + I915_HDMI_LPE_AUDIO_SIZE - 1;
102 rsc[1].flags = IORESOURCE_MEM;
103 rsc[1].name = "hdmi-lpe-audio-mmio";
104
105 pinfo.parent = dev->dev;
106 pinfo.name = "hdmi-lpe-audio";
107 pinfo.id = -1;
108 pinfo.res = rsc;
109 pinfo.num_res = 2;
110 pinfo.data = pdata;
111 pinfo.size_data = sizeof(*pdata);
112 pinfo.dma_mask = DMA_BIT_MASK(32);
113
114 pdata->num_pipes = INTEL_INFO(dev_priv)->num_pipes;
115 pdata->num_ports = IS_CHERRYVIEW(dev_priv) ? 3 : 2; /* B,C,D or B,C */
116 pdata->port[0].pipe = -1;
117 pdata->port[1].pipe = -1;
118 pdata->port[2].pipe = -1;
119 spin_lock_init(&pdata->lpe_audio_slock);
120
121 platdev = platform_device_register_full(&pinfo);
122 if (IS_ERR(platdev)) {
123 ret = PTR_ERR(platdev);
124 DRM_ERROR("Failed to allocate LPE audio platform device\n");
125 goto err;
126 }
127
128 kfree(rsc);
129
130 pm_runtime_forbid(&platdev->dev);
131 pm_runtime_set_active(&platdev->dev);
132 pm_runtime_enable(&platdev->dev);
133
134 return platdev;
135
136 err:
137 kfree(rsc);
138 kfree(pdata);
139 return ERR_PTR(ret);
140 }
141
142 static void lpe_audio_platdev_destroy(struct drm_i915_private *dev_priv)
143 {
144 /* XXX Note that platform_device_register_full() allocates a dma_mask
145 * and never frees it. We can't free it here as we cannot guarantee
146 * this is the last reference (i.e. that the dma_mask will not be
147 * used after our unregister). So ee choose to leak the sizeof(u64)
148 * allocation here - it should be fixed in the platform_device rather
149 * than us fiddle with its internals.
150 */
151
152 platform_device_unregister(dev_priv->lpe_audio.platdev);
153 }
154
155 static void lpe_audio_irq_unmask(struct irq_data *d)
156 {
157 }
158
159 static void lpe_audio_irq_mask(struct irq_data *d)
160 {
161 }
162
163 static struct irq_chip lpe_audio_irqchip = {
164 .name = "hdmi_lpe_audio_irqchip",
165 .irq_mask = lpe_audio_irq_mask,
166 .irq_unmask = lpe_audio_irq_unmask,
167 };
168
169 static int lpe_audio_irq_init(struct drm_i915_private *dev_priv)
170 {
171 int irq = dev_priv->lpe_audio.irq;
172
173 WARN_ON(!intel_irqs_enabled(dev_priv));
174 irq_set_chip_and_handler_name(irq,
175 &lpe_audio_irqchip,
176 handle_simple_irq,
177 "hdmi_lpe_audio_irq_handler");
178
179 static const struct pci_device_id irq_quirk_ids[] = {
180 /* Dell Wyse 3040 */
181 {PCI_DEVICE_SUB(PCI_VENDOR_ID_INTEL, 0x22b0, 0x1028, 0x07c1)},
182 {}
183 };
184
185 if (pci_dev_present(irq_quirk_ids))
186 return 0;
187 return irq_set_chip_data(irq, dev_priv);
188 }
189
190 static bool lpe_audio_detect(struct drm_i915_private *dev_priv)
191 {
192 int lpe_present = false;
193
194 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
195 static const struct pci_device_id atom_hdaudio_ids[] = {
196 /* Baytrail */
197 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x0f04)},
198 /* Braswell */
199 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2284)},
200 {}
201 };
202
203 if (!pci_dev_present(atom_hdaudio_ids)) {
204 DRM_INFO("HDaudio controller not detected, using LPE audio instead\n");
205 lpe_present = true;
206 }
207 }
208 return lpe_present;
209 }
210
211 static int lpe_audio_setup(struct drm_i915_private *dev_priv)
212 {
213 int ret;
214
215 dev_priv->lpe_audio.irq = irq_alloc_desc(0);
216 if (dev_priv->lpe_audio.irq < 0) {
217 DRM_ERROR("Failed to allocate IRQ desc: %d\n",
218 dev_priv->lpe_audio.irq);
219 ret = dev_priv->lpe_audio.irq;
220 goto err;
221 }
222
223 DRM_DEBUG("irq = %d\n", dev_priv->lpe_audio.irq);
224
225 ret = lpe_audio_irq_init(dev_priv);
226
227 if (ret) {
228 DRM_ERROR("Failed to initialize irqchip for lpe audio: %d\n",
229 ret);
230 goto err_free_irq;
231 }
232
233 dev_priv->lpe_audio.platdev = lpe_audio_platdev_create(dev_priv);
234
235 if (IS_ERR(dev_priv->lpe_audio.platdev)) {
236 ret = PTR_ERR(dev_priv->lpe_audio.platdev);
237 DRM_ERROR("Failed to create lpe audio platform device: %d\n",
238 ret);
239 goto err_free_irq;
240 }
241
242 /* enable chicken bit; at least this is required for Dell Wyse 3040
243 * with DP outputs (but only sometimes by some reason!)
244 */
245 I915_WRITE(VLV_AUD_CHICKEN_BIT_REG, VLV_CHICKEN_BIT_DBG_ENABLE);
246
247 return 0;
248 err_free_irq:
249 irq_free_desc(dev_priv->lpe_audio.irq);
250 err:
251 dev_priv->lpe_audio.irq = -1;
252 dev_priv->lpe_audio.platdev = NULL;
253 return ret;
254 }
255
256 /**
257 * intel_lpe_audio_irq_handler() - forwards the LPE audio irq
258 * @dev_priv: the i915 drm device private data
259 *
260 * the LPE Audio irq is forwarded to the irq handler registered by LPE audio
261 * driver.
262 */
263 void intel_lpe_audio_irq_handler(struct drm_i915_private *dev_priv)
264 {
265 int ret;
266
267 if (!HAS_LPE_AUDIO(dev_priv))
268 return;
269
270 ret = generic_handle_irq(dev_priv->lpe_audio.irq);
271 if (ret)
272 DRM_ERROR_RATELIMITED("error handling LPE audio irq: %d\n",
273 ret);
274 }
275
276 /**
277 * intel_lpe_audio_init() - detect and setup the bridge between HDMI LPE Audio
278 * driver and i915
279 * @dev_priv: the i915 drm device private data
280 *
281 * Return: 0 if successful. non-zero if detection or
282 * llocation/initialization fails
283 */
284 int intel_lpe_audio_init(struct drm_i915_private *dev_priv)
285 {
286 int ret = -ENODEV;
287
288 if (lpe_audio_detect(dev_priv)) {
289 ret = lpe_audio_setup(dev_priv);
290 if (ret < 0)
291 DRM_ERROR("failed to setup LPE Audio bridge\n");
292 }
293 return ret;
294 }
295
296 /**
297 * intel_lpe_audio_teardown() - destroy the bridge between HDMI LPE
298 * audio driver and i915
299 * @dev_priv: the i915 drm device private data
300 *
301 * release all the resources for LPE audio <-> i915 bridge.
302 */
303 void intel_lpe_audio_teardown(struct drm_i915_private *dev_priv)
304 {
305 struct irq_desc *desc;
306
307 if (!HAS_LPE_AUDIO(dev_priv))
308 return;
309
310 desc = irq_to_desc(dev_priv->lpe_audio.irq);
311
312 lpe_audio_platdev_destroy(dev_priv);
313
314 irq_free_desc(dev_priv->lpe_audio.irq);
315 }
316
317
318 /**
319 * intel_lpe_audio_notify() - notify lpe audio event
320 * audio driver and i915
321 * @dev_priv: the i915 drm device private data
322 * @pipe: pipe
323 * @port: port
324 * @eld : ELD data
325 * @ls_clock: Link symbol clock in kHz
326 * @dp_output: Driving a DP output?
327 *
328 * Notify lpe audio driver of eld change.
329 */
330 void intel_lpe_audio_notify(struct drm_i915_private *dev_priv,
331 enum pipe pipe, enum port port,
332 const void *eld, int ls_clock, bool dp_output)
333 {
334 unsigned long irqflags;
335 struct intel_hdmi_lpe_audio_pdata *pdata;
336 struct intel_hdmi_lpe_audio_port_pdata *ppdata;
337 u32 audio_enable;
338
339 if (!HAS_LPE_AUDIO(dev_priv))
340 return;
341
342 pdata = dev_get_platdata(&dev_priv->lpe_audio.platdev->dev);
343 ppdata = &pdata->port[port - PORT_B];
344
345 spin_lock_irqsave(&pdata->lpe_audio_slock, irqflags);
346
347 audio_enable = I915_READ(VLV_AUD_PORT_EN_DBG(port));
348
349 if (eld != NULL) {
350 memcpy(ppdata->eld, eld, HDMI_MAX_ELD_BYTES);
351 ppdata->pipe = pipe;
352 ppdata->ls_clock = ls_clock;
353 ppdata->dp_output = dp_output;
354
355 /* Unmute the amp for both DP and HDMI */
356 I915_WRITE(VLV_AUD_PORT_EN_DBG(port),
357 audio_enable & ~VLV_AMP_MUTE);
358 } else {
359 memset(ppdata->eld, 0, HDMI_MAX_ELD_BYTES);
360 ppdata->pipe = -1;
361 ppdata->ls_clock = 0;
362 ppdata->dp_output = false;
363
364 /* Mute the amp for both DP and HDMI */
365 I915_WRITE(VLV_AUD_PORT_EN_DBG(port),
366 audio_enable | VLV_AMP_MUTE);
367 }
368
369 if (pdata->notify_audio_lpe)
370 pdata->notify_audio_lpe(dev_priv->lpe_audio.platdev, port - PORT_B);
371
372 spin_unlock_irqrestore(&pdata->lpe_audio_slock, irqflags);
373 }