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