Jerome Anand | eef5732 | 2017-01-25 04:27:49 +0530 | [diff] [blame] | 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 | |
| 67 | #include "i915_drv.h" |
| 68 | #include <linux/delay.h> |
| 69 | #include <drm/intel_lpe_audio.h> |
| 70 | |
| 71 | #define HAS_LPE_AUDIO(dev_priv) ((dev_priv)->lpe_audio.platdev != NULL) |
| 72 | |
| 73 | static struct platform_device * |
| 74 | lpe_audio_platdev_create(struct drm_i915_private *dev_priv) |
| 75 | { |
| 76 | int ret; |
| 77 | struct drm_device *dev = &dev_priv->drm; |
| 78 | struct platform_device_info pinfo = {}; |
| 79 | struct resource *rsc; |
| 80 | struct platform_device *platdev; |
| 81 | struct intel_hdmi_lpe_audio_pdata *pdata; |
| 82 | |
| 83 | pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); |
| 84 | if (!pdata) |
| 85 | return ERR_PTR(-ENOMEM); |
| 86 | |
| 87 | rsc = kcalloc(2, sizeof(*rsc), GFP_KERNEL); |
| 88 | if (!rsc) { |
| 89 | kfree(pdata); |
| 90 | return ERR_PTR(-ENOMEM); |
| 91 | } |
| 92 | |
| 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"; |
| 96 | |
| 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"; |
| 103 | |
| 104 | pinfo.parent = dev->dev; |
| 105 | pinfo.name = "hdmi-lpe-audio"; |
| 106 | pinfo.id = -1; |
| 107 | pinfo.res = rsc; |
| 108 | pinfo.num_res = 2; |
| 109 | pinfo.data = pdata; |
| 110 | pinfo.size_data = sizeof(*pdata); |
| 111 | pinfo.dma_mask = DMA_BIT_MASK(32); |
| 112 | |
| 113 | spin_lock_init(&pdata->lpe_audio_slock); |
| 114 | |
| 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"); |
| 119 | goto err; |
| 120 | } |
| 121 | |
| 122 | kfree(rsc); |
| 123 | |
| 124 | return platdev; |
| 125 | |
| 126 | err: |
| 127 | kfree(rsc); |
| 128 | kfree(pdata); |
| 129 | return ERR_PTR(ret); |
| 130 | } |
| 131 | |
| 132 | static void lpe_audio_platdev_destroy(struct drm_i915_private *dev_priv) |
| 133 | { |
| 134 | platform_device_unregister(dev_priv->lpe_audio.platdev); |
| 135 | kfree(dev_priv->lpe_audio.platdev->dev.dma_mask); |
| 136 | } |
| 137 | |
| 138 | static void lpe_audio_irq_unmask(struct irq_data *d) |
| 139 | { |
| 140 | struct drm_i915_private *dev_priv = d->chip_data; |
| 141 | unsigned long irqflags; |
| 142 | u32 val = (I915_LPE_PIPE_A_INTERRUPT | |
| 143 | I915_LPE_PIPE_B_INTERRUPT); |
| 144 | |
| 145 | if (IS_CHERRYVIEW(dev_priv)) |
| 146 | val |= I915_LPE_PIPE_C_INTERRUPT; |
| 147 | |
| 148 | spin_lock_irqsave(&dev_priv->irq_lock, irqflags); |
| 149 | |
| 150 | dev_priv->irq_mask &= ~val; |
| 151 | I915_WRITE(VLV_IIR, val); |
| 152 | I915_WRITE(VLV_IIR, val); |
| 153 | I915_WRITE(VLV_IMR, dev_priv->irq_mask); |
| 154 | POSTING_READ(VLV_IMR); |
| 155 | |
| 156 | spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags); |
| 157 | } |
| 158 | |
| 159 | static void lpe_audio_irq_mask(struct irq_data *d) |
| 160 | { |
| 161 | struct drm_i915_private *dev_priv = d->chip_data; |
| 162 | unsigned long irqflags; |
| 163 | u32 val = (I915_LPE_PIPE_A_INTERRUPT | |
| 164 | I915_LPE_PIPE_B_INTERRUPT); |
| 165 | |
| 166 | if (IS_CHERRYVIEW(dev_priv)) |
| 167 | val |= I915_LPE_PIPE_C_INTERRUPT; |
| 168 | |
| 169 | spin_lock_irqsave(&dev_priv->irq_lock, irqflags); |
| 170 | |
| 171 | dev_priv->irq_mask |= val; |
| 172 | I915_WRITE(VLV_IMR, dev_priv->irq_mask); |
| 173 | I915_WRITE(VLV_IIR, val); |
| 174 | I915_WRITE(VLV_IIR, val); |
| 175 | POSTING_READ(VLV_IIR); |
| 176 | |
| 177 | spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags); |
| 178 | } |
| 179 | |
| 180 | static struct irq_chip lpe_audio_irqchip = { |
| 181 | .name = "hdmi_lpe_audio_irqchip", |
| 182 | .irq_mask = lpe_audio_irq_mask, |
| 183 | .irq_unmask = lpe_audio_irq_unmask, |
| 184 | }; |
| 185 | |
| 186 | static int lpe_audio_irq_init(struct drm_i915_private *dev_priv) |
| 187 | { |
| 188 | int irq = dev_priv->lpe_audio.irq; |
| 189 | |
| 190 | WARN_ON(!intel_irqs_enabled(dev_priv)); |
| 191 | irq_set_chip_and_handler_name(irq, |
| 192 | &lpe_audio_irqchip, |
| 193 | handle_simple_irq, |
| 194 | "hdmi_lpe_audio_irq_handler"); |
| 195 | |
| 196 | return irq_set_chip_data(irq, dev_priv); |
| 197 | } |
| 198 | |
| 199 | static bool lpe_audio_detect(struct drm_i915_private *dev_priv) |
| 200 | { |
| 201 | int lpe_present = false; |
| 202 | |
| 203 | if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) { |
| 204 | static const struct pci_device_id atom_hdaudio_ids[] = { |
| 205 | /* Baytrail */ |
| 206 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x0f04)}, |
| 207 | /* Braswell */ |
| 208 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2284)}, |
| 209 | {} |
| 210 | }; |
| 211 | |
| 212 | if (!pci_dev_present(atom_hdaudio_ids)) { |
| 213 | DRM_INFO("%s\n", "HDaudio controller not detected, using LPE audio instead\n"); |
| 214 | lpe_present = true; |
| 215 | } |
| 216 | } |
| 217 | return lpe_present; |
| 218 | } |
| 219 | |
| 220 | static int lpe_audio_setup(struct drm_i915_private *dev_priv) |
| 221 | { |
| 222 | int ret; |
| 223 | |
| 224 | dev_priv->lpe_audio.irq = irq_alloc_desc(0); |
| 225 | if (dev_priv->lpe_audio.irq < 0) { |
| 226 | DRM_ERROR("Failed to allocate IRQ desc: %d\n", |
| 227 | dev_priv->lpe_audio.irq); |
| 228 | ret = dev_priv->lpe_audio.irq; |
| 229 | goto err; |
| 230 | } |
| 231 | |
| 232 | DRM_DEBUG("irq = %d\n", dev_priv->lpe_audio.irq); |
| 233 | |
| 234 | ret = lpe_audio_irq_init(dev_priv); |
| 235 | |
| 236 | if (ret) { |
| 237 | DRM_ERROR("Failed to initialize irqchip for lpe audio: %d\n", |
| 238 | ret); |
| 239 | goto err_free_irq; |
| 240 | } |
| 241 | |
| 242 | dev_priv->lpe_audio.platdev = lpe_audio_platdev_create(dev_priv); |
| 243 | |
| 244 | if (IS_ERR(dev_priv->lpe_audio.platdev)) { |
| 245 | ret = PTR_ERR(dev_priv->lpe_audio.platdev); |
| 246 | DRM_ERROR("Failed to create lpe audio platform device: %d\n", |
| 247 | ret); |
| 248 | goto err_free_irq; |
| 249 | } |
| 250 | |
Takashi Iwai | 9db13e5 | 2017-02-02 11:03:48 +0100 | [diff] [blame] | 251 | /* enable chicken bit; at least this is required for Dell Wyse 3040 |
| 252 | * with DP outputs (but only sometimes by some reason!) |
| 253 | */ |
| 254 | I915_WRITE(VLV_AUD_CHICKEN_BIT_REG, VLV_CHICKEN_BIT_DBG_ENABLE); |
| 255 | |
Jerome Anand | eef5732 | 2017-01-25 04:27:49 +0530 | [diff] [blame] | 256 | return 0; |
| 257 | err_free_irq: |
| 258 | irq_free_desc(dev_priv->lpe_audio.irq); |
| 259 | err: |
| 260 | dev_priv->lpe_audio.irq = -1; |
| 261 | dev_priv->lpe_audio.platdev = NULL; |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * intel_lpe_audio_irq_handler() - forwards the LPE audio irq |
| 267 | * @dev_priv: the i915 drm device private data |
| 268 | * |
| 269 | * the LPE Audio irq is forwarded to the irq handler registered by LPE audio |
| 270 | * driver. |
| 271 | */ |
| 272 | void intel_lpe_audio_irq_handler(struct drm_i915_private *dev_priv) |
| 273 | { |
| 274 | int ret; |
| 275 | |
| 276 | if (!HAS_LPE_AUDIO(dev_priv)) |
| 277 | return; |
| 278 | |
| 279 | ret = generic_handle_irq(dev_priv->lpe_audio.irq); |
| 280 | if (ret) |
| 281 | DRM_ERROR_RATELIMITED("error handling LPE audio irq: %d\n", |
| 282 | ret); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * intel_lpe_audio_init() - detect and setup the bridge between HDMI LPE Audio |
| 287 | * driver and i915 |
| 288 | * @dev_priv: the i915 drm device private data |
| 289 | * |
| 290 | * Return: 0 if successful. non-zero if detection or |
| 291 | * llocation/initialization fails |
| 292 | */ |
| 293 | int intel_lpe_audio_init(struct drm_i915_private *dev_priv) |
| 294 | { |
| 295 | int ret = -ENODEV; |
| 296 | |
| 297 | if (lpe_audio_detect(dev_priv)) { |
| 298 | ret = lpe_audio_setup(dev_priv); |
| 299 | if (ret < 0) |
| 300 | DRM_ERROR("failed to setup LPE Audio bridge\n"); |
| 301 | } |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * intel_lpe_audio_teardown() - destroy the bridge between HDMI LPE |
| 307 | * audio driver and i915 |
| 308 | * @dev_priv: the i915 drm device private data |
| 309 | * |
| 310 | * release all the resources for LPE audio <-> i915 bridge. |
| 311 | */ |
| 312 | void intel_lpe_audio_teardown(struct drm_i915_private *dev_priv) |
| 313 | { |
| 314 | struct irq_desc *desc; |
| 315 | |
| 316 | if (!HAS_LPE_AUDIO(dev_priv)) |
| 317 | return; |
| 318 | |
| 319 | desc = irq_to_desc(dev_priv->lpe_audio.irq); |
| 320 | |
| 321 | lpe_audio_irq_mask(&desc->irq_data); |
| 322 | |
| 323 | lpe_audio_platdev_destroy(dev_priv); |
| 324 | |
| 325 | irq_free_desc(dev_priv->lpe_audio.irq); |
| 326 | } |
Jerome Anand | 46d196e | 2017-01-25 04:27:50 +0530 | [diff] [blame] | 327 | |
| 328 | |
| 329 | /** |
| 330 | * intel_lpe_audio_notify() - notify lpe audio event |
| 331 | * audio driver and i915 |
| 332 | * @dev_priv: the i915 drm device private data |
| 333 | * @eld : ELD data |
Tamara Diaconita | 31581b6 | 2017-03-30 14:55:10 +0300 | [diff] [blame] | 334 | * @pipe: pipe id |
Jerome Anand | 46d196e | 2017-01-25 04:27:50 +0530 | [diff] [blame] | 335 | * @port: port id |
| 336 | * @tmds_clk_speed: tmds clock frequency in Hz |
| 337 | * |
| 338 | * Notify lpe audio driver of eld change. |
| 339 | */ |
| 340 | void intel_lpe_audio_notify(struct drm_i915_private *dev_priv, |
Takashi Iwai | f95e29b | 2017-01-31 14:16:51 -0600 | [diff] [blame] | 341 | void *eld, int port, int pipe, int tmds_clk_speed, |
Pierre-Louis Bossart | b5f2be9 | 2017-01-31 14:16:48 -0600 | [diff] [blame] | 342 | bool dp_output, int link_rate) |
Jerome Anand | 46d196e | 2017-01-25 04:27:50 +0530 | [diff] [blame] | 343 | { |
| 344 | unsigned long irq_flags; |
| 345 | struct intel_hdmi_lpe_audio_pdata *pdata = NULL; |
Pierre-Louis Bossart | d5d8c3a | 2017-01-31 14:16:49 -0600 | [diff] [blame] | 346 | u32 audio_enable; |
Jerome Anand | 46d196e | 2017-01-25 04:27:50 +0530 | [diff] [blame] | 347 | |
| 348 | if (!HAS_LPE_AUDIO(dev_priv)) |
| 349 | return; |
| 350 | |
| 351 | pdata = dev_get_platdata( |
| 352 | &(dev_priv->lpe_audio.platdev->dev)); |
| 353 | |
| 354 | spin_lock_irqsave(&pdata->lpe_audio_slock, irq_flags); |
| 355 | |
Pierre-Louis Bossart | d5d8c3a | 2017-01-31 14:16:49 -0600 | [diff] [blame] | 356 | audio_enable = I915_READ(VLV_AUD_PORT_EN_DBG(port)); |
| 357 | |
Jerome Anand | 46d196e | 2017-01-25 04:27:50 +0530 | [diff] [blame] | 358 | if (eld != NULL) { |
| 359 | memcpy(pdata->eld.eld_data, eld, |
| 360 | HDMI_MAX_ELD_BYTES); |
| 361 | pdata->eld.port_id = port; |
Takashi Iwai | f95e29b | 2017-01-31 14:16:51 -0600 | [diff] [blame] | 362 | pdata->eld.pipe_id = pipe; |
Jerome Anand | 46d196e | 2017-01-25 04:27:50 +0530 | [diff] [blame] | 363 | pdata->hdmi_connected = true; |
| 364 | |
Pierre-Louis Bossart | b5f2be9 | 2017-01-31 14:16:48 -0600 | [diff] [blame] | 365 | pdata->dp_output = dp_output; |
Jerome Anand | 46d196e | 2017-01-25 04:27:50 +0530 | [diff] [blame] | 366 | if (tmds_clk_speed) |
| 367 | pdata->tmds_clock_speed = tmds_clk_speed; |
Pierre-Louis Bossart | b5f2be9 | 2017-01-31 14:16:48 -0600 | [diff] [blame] | 368 | if (link_rate) |
| 369 | pdata->link_rate = link_rate; |
Pierre-Louis Bossart | d5d8c3a | 2017-01-31 14:16:49 -0600 | [diff] [blame] | 370 | |
| 371 | /* Unmute the amp for both DP and HDMI */ |
| 372 | I915_WRITE(VLV_AUD_PORT_EN_DBG(port), |
| 373 | audio_enable & ~VLV_AMP_MUTE); |
| 374 | |
Jerome Anand | 46d196e | 2017-01-25 04:27:50 +0530 | [diff] [blame] | 375 | } else { |
| 376 | memset(pdata->eld.eld_data, 0, |
| 377 | HDMI_MAX_ELD_BYTES); |
| 378 | pdata->hdmi_connected = false; |
Pierre-Louis Bossart | b5f2be9 | 2017-01-31 14:16:48 -0600 | [diff] [blame] | 379 | pdata->dp_output = false; |
Pierre-Louis Bossart | d5d8c3a | 2017-01-31 14:16:49 -0600 | [diff] [blame] | 380 | |
| 381 | /* Mute the amp for both DP and HDMI */ |
| 382 | I915_WRITE(VLV_AUD_PORT_EN_DBG(port), |
| 383 | audio_enable | VLV_AMP_MUTE); |
Jerome Anand | 46d196e | 2017-01-25 04:27:50 +0530 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | if (pdata->notify_audio_lpe) |
Takashi Iwai | b1c01f4 | 2017-01-30 17:56:39 +0100 | [diff] [blame] | 387 | pdata->notify_audio_lpe(dev_priv->lpe_audio.platdev); |
Jerome Anand | 46d196e | 2017-01-25 04:27:50 +0530 | [diff] [blame] | 388 | else |
| 389 | pdata->notify_pending = true; |
| 390 | |
| 391 | spin_unlock_irqrestore(&pdata->lpe_audio_slock, |
| 392 | irq_flags); |
| 393 | } |