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