2 * Copyright © 2016 Intel Corporation
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:
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
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
24 * Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
25 * Jerome Anand <jerome.anand@intel.com>
26 * based on VED patches
31 * DOC: LPE Audio integration for HDMI or DP playback
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.
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
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.
56 * The MMIO/REG platform resources are created according to the registers
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.
63 #include <linux/acpi.h>
64 #include <linux/device.h>
65 #include <linux/pci.h>
68 #include <linux/delay.h>
69 #include <drm/intel_lpe_audio.h>
71 #define HAS_LPE_AUDIO(dev_priv) ((dev_priv)->lpe_audio.platdev != NULL)
73 static struct platform_device
*
74 lpe_audio_platdev_create(struct drm_i915_private
*dev_priv
)
77 struct drm_device
*dev
= &dev_priv
->drm
;
78 struct platform_device_info pinfo
= {};
80 struct platform_device
*platdev
;
81 struct intel_hdmi_lpe_audio_pdata
*pdata
;
83 pdata
= kzalloc(sizeof(*pdata
), GFP_KERNEL
);
85 return ERR_PTR(-ENOMEM
);
87 rsc
= kcalloc(2, sizeof(*rsc
), GFP_KERNEL
);
90 return ERR_PTR(-ENOMEM
);
93 rsc
[0].start
= rsc
[0].end
= dev_priv
->lpe_audio
.irq
;
94 rsc
[0].flags
= IORESOURCE_IRQ
;
95 rsc
[0].name
= "hdmi-lpe-audio-irq";
97 rsc
[1].start
= pci_resource_start(dev
->pdev
, 0) +
98 I915_HDMI_LPE_AUDIO_BASE
;
99 rsc
[1].end
= pci_resource_start(dev
->pdev
, 0) +
100 I915_HDMI_LPE_AUDIO_BASE
+ I915_HDMI_LPE_AUDIO_SIZE
- 1;
101 rsc
[1].flags
= IORESOURCE_MEM
;
102 rsc
[1].name
= "hdmi-lpe-audio-mmio";
104 pinfo
.parent
= dev
->dev
;
105 pinfo
.name
= "hdmi-lpe-audio";
110 pinfo
.size_data
= sizeof(*pdata
);
111 pinfo
.dma_mask
= DMA_BIT_MASK(32);
113 spin_lock_init(&pdata
->lpe_audio_slock
);
115 platdev
= platform_device_register_full(&pinfo
);
116 if (IS_ERR(platdev
)) {
117 ret
= PTR_ERR(platdev
);
118 DRM_ERROR("Failed to allocate LPE audio platform device\n");
132 static void lpe_audio_platdev_destroy(struct drm_i915_private
*dev_priv
)
134 /* XXX Note that platform_device_register_full() allocates a dma_mask
135 * and never frees it. We can't free it here as we cannot guarantee
136 * this is the last reference (i.e. that the dma_mask will not be
137 * used after our unregister). So ee choose to leak the sizeof(u64)
138 * allocation here - it should be fixed in the platform_device rather
139 * than us fiddle with its internals.
142 platform_device_unregister(dev_priv
->lpe_audio
.platdev
);
145 static void lpe_audio_irq_unmask(struct irq_data
*d
)
147 struct drm_i915_private
*dev_priv
= d
->chip_data
;
148 unsigned long irqflags
;
149 u32 val
= (I915_LPE_PIPE_A_INTERRUPT
|
150 I915_LPE_PIPE_B_INTERRUPT
);
152 if (IS_CHERRYVIEW(dev_priv
))
153 val
|= I915_LPE_PIPE_C_INTERRUPT
;
155 spin_lock_irqsave(&dev_priv
->irq_lock
, irqflags
);
157 dev_priv
->irq_mask
&= ~val
;
158 I915_WRITE(VLV_IIR
, val
);
159 I915_WRITE(VLV_IIR
, val
);
160 I915_WRITE(VLV_IMR
, dev_priv
->irq_mask
);
161 POSTING_READ(VLV_IMR
);
163 spin_unlock_irqrestore(&dev_priv
->irq_lock
, irqflags
);
166 static void lpe_audio_irq_mask(struct irq_data
*d
)
168 struct drm_i915_private
*dev_priv
= d
->chip_data
;
169 unsigned long irqflags
;
170 u32 val
= (I915_LPE_PIPE_A_INTERRUPT
|
171 I915_LPE_PIPE_B_INTERRUPT
);
173 if (IS_CHERRYVIEW(dev_priv
))
174 val
|= I915_LPE_PIPE_C_INTERRUPT
;
176 spin_lock_irqsave(&dev_priv
->irq_lock
, irqflags
);
178 dev_priv
->irq_mask
|= val
;
179 I915_WRITE(VLV_IMR
, dev_priv
->irq_mask
);
180 I915_WRITE(VLV_IIR
, val
);
181 I915_WRITE(VLV_IIR
, val
);
182 POSTING_READ(VLV_IIR
);
184 spin_unlock_irqrestore(&dev_priv
->irq_lock
, irqflags
);
187 static struct irq_chip lpe_audio_irqchip
= {
188 .name
= "hdmi_lpe_audio_irqchip",
189 .irq_mask
= lpe_audio_irq_mask
,
190 .irq_unmask
= lpe_audio_irq_unmask
,
193 static int lpe_audio_irq_init(struct drm_i915_private
*dev_priv
)
195 int irq
= dev_priv
->lpe_audio
.irq
;
197 WARN_ON(!intel_irqs_enabled(dev_priv
));
198 irq_set_chip_and_handler_name(irq
,
201 "hdmi_lpe_audio_irq_handler");
203 return irq_set_chip_data(irq
, dev_priv
);
206 static bool lpe_audio_detect(struct drm_i915_private
*dev_priv
)
208 int lpe_present
= false;
210 if (IS_VALLEYVIEW(dev_priv
) || IS_CHERRYVIEW(dev_priv
)) {
211 static const struct pci_device_id atom_hdaudio_ids
[] = {
213 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, 0x0f04)},
215 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, 0x2284)},
219 if (!pci_dev_present(atom_hdaudio_ids
)) {
220 DRM_INFO("%s\n", "HDaudio controller not detected, using LPE audio instead\n");
227 static int lpe_audio_setup(struct drm_i915_private
*dev_priv
)
231 dev_priv
->lpe_audio
.irq
= irq_alloc_desc(0);
232 if (dev_priv
->lpe_audio
.irq
< 0) {
233 DRM_ERROR("Failed to allocate IRQ desc: %d\n",
234 dev_priv
->lpe_audio
.irq
);
235 ret
= dev_priv
->lpe_audio
.irq
;
239 DRM_DEBUG("irq = %d\n", dev_priv
->lpe_audio
.irq
);
241 ret
= lpe_audio_irq_init(dev_priv
);
244 DRM_ERROR("Failed to initialize irqchip for lpe audio: %d\n",
249 dev_priv
->lpe_audio
.platdev
= lpe_audio_platdev_create(dev_priv
);
251 if (IS_ERR(dev_priv
->lpe_audio
.platdev
)) {
252 ret
= PTR_ERR(dev_priv
->lpe_audio
.platdev
);
253 DRM_ERROR("Failed to create lpe audio platform device: %d\n",
258 /* enable chicken bit; at least this is required for Dell Wyse 3040
259 * with DP outputs (but only sometimes by some reason!)
261 I915_WRITE(VLV_AUD_CHICKEN_BIT_REG
, VLV_CHICKEN_BIT_DBG_ENABLE
);
265 irq_free_desc(dev_priv
->lpe_audio
.irq
);
267 dev_priv
->lpe_audio
.irq
= -1;
268 dev_priv
->lpe_audio
.platdev
= NULL
;
273 * intel_lpe_audio_irq_handler() - forwards the LPE audio irq
274 * @dev_priv: the i915 drm device private data
276 * the LPE Audio irq is forwarded to the irq handler registered by LPE audio
279 void intel_lpe_audio_irq_handler(struct drm_i915_private
*dev_priv
)
283 if (!HAS_LPE_AUDIO(dev_priv
))
286 ret
= generic_handle_irq(dev_priv
->lpe_audio
.irq
);
288 DRM_ERROR_RATELIMITED("error handling LPE audio irq: %d\n",
293 * intel_lpe_audio_init() - detect and setup the bridge between HDMI LPE Audio
295 * @dev_priv: the i915 drm device private data
297 * Return: 0 if successful. non-zero if detection or
298 * llocation/initialization fails
300 int intel_lpe_audio_init(struct drm_i915_private
*dev_priv
)
304 if (lpe_audio_detect(dev_priv
)) {
305 ret
= lpe_audio_setup(dev_priv
);
307 DRM_ERROR("failed to setup LPE Audio bridge\n");
313 * intel_lpe_audio_teardown() - destroy the bridge between HDMI LPE
314 * audio driver and i915
315 * @dev_priv: the i915 drm device private data
317 * release all the resources for LPE audio <-> i915 bridge.
319 void intel_lpe_audio_teardown(struct drm_i915_private
*dev_priv
)
321 struct irq_desc
*desc
;
323 if (!HAS_LPE_AUDIO(dev_priv
))
326 desc
= irq_to_desc(dev_priv
->lpe_audio
.irq
);
328 lpe_audio_irq_mask(&desc
->irq_data
);
330 lpe_audio_platdev_destroy(dev_priv
);
332 irq_free_desc(dev_priv
->lpe_audio
.irq
);
337 * intel_lpe_audio_notify() - notify lpe audio event
338 * audio driver and i915
339 * @dev_priv: the i915 drm device private data
343 * @tmds_clk_speed: tmds clock frequency in Hz
345 * Notify lpe audio driver of eld change.
347 void intel_lpe_audio_notify(struct drm_i915_private
*dev_priv
,
348 void *eld
, int port
, int pipe
, int tmds_clk_speed
,
349 bool dp_output
, int link_rate
)
351 unsigned long irq_flags
;
352 struct intel_hdmi_lpe_audio_pdata
*pdata
= NULL
;
355 if (!HAS_LPE_AUDIO(dev_priv
))
358 pdata
= dev_get_platdata(
359 &(dev_priv
->lpe_audio
.platdev
->dev
));
361 spin_lock_irqsave(&pdata
->lpe_audio_slock
, irq_flags
);
363 audio_enable
= I915_READ(VLV_AUD_PORT_EN_DBG(port
));
366 memcpy(pdata
->eld
.eld_data
, eld
,
368 pdata
->eld
.port_id
= port
;
369 pdata
->eld
.pipe_id
= pipe
;
370 pdata
->hdmi_connected
= true;
372 pdata
->dp_output
= dp_output
;
374 pdata
->tmds_clock_speed
= tmds_clk_speed
;
376 pdata
->link_rate
= link_rate
;
378 /* Unmute the amp for both DP and HDMI */
379 I915_WRITE(VLV_AUD_PORT_EN_DBG(port
),
380 audio_enable
& ~VLV_AMP_MUTE
);
383 memset(pdata
->eld
.eld_data
, 0,
385 pdata
->hdmi_connected
= false;
386 pdata
->dp_output
= false;
388 /* Mute the amp for both DP and HDMI */
389 I915_WRITE(VLV_AUD_PORT_EN_DBG(port
),
390 audio_enable
| VLV_AMP_MUTE
);
393 if (pdata
->notify_audio_lpe
)
394 pdata
->notify_audio_lpe(dev_priv
->lpe_audio
.platdev
);
396 pdata
->notify_pending
= true;
398 spin_unlock_irqrestore(&pdata
->lpe_audio_slock
,