| 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/drm_print.h> |
| 72 | #include <drm/intel/intel_lpe_audio.h> |
| 73 | |
| 74 | #include "i915_irq.h" |
| 75 | #include "intel_audio_regs.h" |
| 76 | #include "intel_de.h" |
| 77 | #include "intel_lpe_audio.h" |
| 78 | #include "intel_pci_config.h" |
| 79 | |
| 80 | #define HAS_LPE_AUDIO(display) ((display)->audio.lpe.platdev) |
| 81 | |
| 82 | static struct platform_device * |
| 83 | lpe_audio_platdev_create(struct intel_display *display) |
| 84 | { |
| 85 | struct pci_dev *pdev = to_pci_dev(display->drm->dev); |
| 86 | struct platform_device_info pinfo = {}; |
| 87 | struct resource *rsc; |
| 88 | struct platform_device *platdev; |
| 89 | struct intel_hdmi_lpe_audio_pdata *pdata; |
| 90 | |
| 91 | pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); |
| 92 | if (!pdata) |
| 93 | return ERR_PTR(error: -ENOMEM); |
| 94 | |
| 95 | rsc = kcalloc(2, sizeof(*rsc), GFP_KERNEL); |
| 96 | if (!rsc) { |
| 97 | kfree(objp: pdata); |
| 98 | return ERR_PTR(error: -ENOMEM); |
| 99 | } |
| 100 | |
| 101 | rsc[0].start = display->audio.lpe.irq; |
| 102 | rsc[0].end = display->audio.lpe.irq; |
| 103 | rsc[0].flags = IORESOURCE_IRQ; |
| 104 | rsc[0].name = "hdmi-lpe-audio-irq" ; |
| 105 | |
| 106 | rsc[1].start = pci_resource_start(pdev, GEN4_GTTMMADR_BAR) + |
| 107 | I915_HDMI_LPE_AUDIO_BASE; |
| 108 | rsc[1].end = pci_resource_start(pdev, GEN4_GTTMMADR_BAR) + |
| 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 = display->drm->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(display); |
| 123 | pdata->num_ports = display->platform.cherryview ? 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(pdevinfo: &pinfo); |
| 130 | kfree(objp: rsc); |
| 131 | kfree(objp: pdata); |
| 132 | |
| 133 | if (IS_ERR(ptr: platdev)) { |
| 134 | drm_err(display->drm, |
| 135 | "Failed to allocate LPE audio platform device\n" ); |
| 136 | return platdev; |
| 137 | } |
| 138 | |
| 139 | pm_runtime_no_callbacks(dev: &platdev->dev); |
| 140 | |
| 141 | return platdev; |
| 142 | } |
| 143 | |
| 144 | static void lpe_audio_platdev_destroy(struct intel_display *display) |
| 145 | { |
| 146 | /* XXX Note that platform_device_register_full() allocates a dma_mask |
| 147 | * and never frees it. We can't free it here as we cannot guarantee |
| 148 | * this is the last reference (i.e. that the dma_mask will not be |
| 149 | * used after our unregister). So ee choose to leak the sizeof(u64) |
| 150 | * allocation here - it should be fixed in the platform_device rather |
| 151 | * than us fiddle with its internals. |
| 152 | */ |
| 153 | |
| 154 | platform_device_unregister(display->audio.lpe.platdev); |
| 155 | } |
| 156 | |
| 157 | static void lpe_audio_irq_unmask(struct irq_data *d) |
| 158 | { |
| 159 | } |
| 160 | |
| 161 | static void lpe_audio_irq_mask(struct irq_data *d) |
| 162 | { |
| 163 | } |
| 164 | |
| 165 | static struct irq_chip lpe_audio_irqchip = { |
| 166 | .name = "hdmi_lpe_audio_irqchip" , |
| 167 | .irq_mask = lpe_audio_irq_mask, |
| 168 | .irq_unmask = lpe_audio_irq_unmask, |
| 169 | }; |
| 170 | |
| 171 | static int lpe_audio_irq_init(struct intel_display *display) |
| 172 | { |
| 173 | int irq = display->audio.lpe.irq; |
| 174 | |
| 175 | irq_set_chip_and_handler_name(irq, chip: &lpe_audio_irqchip, |
| 176 | handle: handle_simple_irq, |
| 177 | name: "hdmi_lpe_audio_irq_handler" ); |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static bool lpe_audio_detect(struct intel_display *display) |
| 183 | { |
| 184 | int lpe_present = false; |
| 185 | |
| 186 | if (display->platform.valleyview || display->platform.cherryview) { |
| 187 | static const struct pci_device_id atom_hdaudio_ids[] = { |
| 188 | /* Baytrail */ |
| 189 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x0f04)}, |
| 190 | /* Braswell */ |
| 191 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2284)}, |
| 192 | {} |
| 193 | }; |
| 194 | |
| 195 | if (!pci_dev_present(ids: atom_hdaudio_ids)) { |
| 196 | drm_info(display->drm, |
| 197 | "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 intel_display *display) |
| 205 | { |
| 206 | int ret; |
| 207 | |
| 208 | display->audio.lpe.irq = irq_alloc_desc(0); |
| 209 | if (display->audio.lpe.irq < 0) { |
| 210 | drm_err(display->drm, "Failed to allocate IRQ desc: %d\n" , |
| 211 | display->audio.lpe.irq); |
| 212 | ret = display->audio.lpe.irq; |
| 213 | goto err; |
| 214 | } |
| 215 | |
| 216 | drm_dbg(display->drm, "irq = %d\n" , display->audio.lpe.irq); |
| 217 | |
| 218 | ret = lpe_audio_irq_init(display); |
| 219 | |
| 220 | if (ret) { |
| 221 | drm_err(display->drm, |
| 222 | "Failed to initialize irqchip for lpe audio: %d\n" , |
| 223 | ret); |
| 224 | goto err_free_irq; |
| 225 | } |
| 226 | |
| 227 | display->audio.lpe.platdev = lpe_audio_platdev_create(display); |
| 228 | |
| 229 | if (IS_ERR(ptr: display->audio.lpe.platdev)) { |
| 230 | ret = PTR_ERR(ptr: display->audio.lpe.platdev); |
| 231 | drm_err(display->drm, |
| 232 | "Failed to create lpe audio platform device: %d\n" , |
| 233 | ret); |
| 234 | goto err_free_irq; |
| 235 | } |
| 236 | |
| 237 | /* enable chicken bit; at least this is required for Dell Wyse 3040 |
| 238 | * with DP outputs (but only sometimes by some reason!) |
| 239 | */ |
| 240 | intel_de_write(display, VLV_AUD_CHICKEN_BIT_REG, |
| 241 | VLV_CHICKEN_BIT_DBG_ENABLE); |
| 242 | |
| 243 | return 0; |
| 244 | err_free_irq: |
| 245 | irq_free_desc(irq: display->audio.lpe.irq); |
| 246 | err: |
| 247 | display->audio.lpe.irq = -1; |
| 248 | display->audio.lpe.platdev = NULL; |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * intel_lpe_audio_irq_handler() - forwards the LPE audio irq |
| 254 | * @display: display device |
| 255 | * |
| 256 | * the LPE Audio irq is forwarded to the irq handler registered by LPE audio |
| 257 | * driver. |
| 258 | */ |
| 259 | void intel_lpe_audio_irq_handler(struct intel_display *display) |
| 260 | { |
| 261 | int ret; |
| 262 | |
| 263 | if (!HAS_LPE_AUDIO(display)) |
| 264 | return; |
| 265 | |
| 266 | ret = generic_handle_irq(irq: display->audio.lpe.irq); |
| 267 | if (ret) |
| 268 | drm_err_ratelimited(display->drm, |
| 269 | "error handling LPE audio irq: %d\n" , ret); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * intel_lpe_audio_init() - detect and setup the bridge between HDMI LPE Audio |
| 274 | * driver and i915 |
| 275 | * @display: display device |
| 276 | * |
| 277 | * Return: 0 if successful. non-zero if detection or |
| 278 | * llocation/initialization fails |
| 279 | */ |
| 280 | int intel_lpe_audio_init(struct intel_display *display) |
| 281 | { |
| 282 | int ret = -ENODEV; |
| 283 | |
| 284 | if (lpe_audio_detect(display)) { |
| 285 | ret = lpe_audio_setup(display); |
| 286 | if (ret < 0) |
| 287 | drm_err(display->drm, |
| 288 | "failed to setup LPE Audio bridge\n" ); |
| 289 | } |
| 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * intel_lpe_audio_teardown() - destroy the bridge between HDMI LPE |
| 295 | * audio driver and i915 |
| 296 | * @display: display device |
| 297 | * |
| 298 | * release all the resources for LPE audio <-> i915 bridge. |
| 299 | */ |
| 300 | void intel_lpe_audio_teardown(struct intel_display *display) |
| 301 | { |
| 302 | if (!HAS_LPE_AUDIO(display)) |
| 303 | return; |
| 304 | |
| 305 | lpe_audio_platdev_destroy(display); |
| 306 | |
| 307 | irq_free_desc(irq: display->audio.lpe.irq); |
| 308 | |
| 309 | display->audio.lpe.irq = -1; |
| 310 | display->audio.lpe.platdev = NULL; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * intel_lpe_audio_notify() - notify lpe audio event |
| 315 | * audio driver and i915 |
| 316 | * @display: display device |
| 317 | * @cpu_transcoder: CPU transcoder |
| 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 intel_display *display, |
| 326 | enum transcoder cpu_transcoder, 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(display)) |
| 335 | return; |
| 336 | |
| 337 | pdata = dev_get_platdata(dev: &display->audio.lpe.platdev->dev); |
| 338 | ppdata = &pdata->port[port - PORT_B]; |
| 339 | |
| 340 | spin_lock_irqsave(&pdata->lpe_audio_slock, irqflags); |
| 341 | |
| 342 | audio_enable = intel_de_read(display, VLV_AUD_PORT_EN_DBG(port)); |
| 343 | |
| 344 | if (eld != NULL) { |
| 345 | memcpy(ppdata->eld, eld, HDMI_MAX_ELD_BYTES); |
| 346 | ppdata->pipe = cpu_transcoder; |
| 347 | ppdata->ls_clock = ls_clock; |
| 348 | ppdata->dp_output = dp_output; |
| 349 | |
| 350 | /* Unmute the amp for both DP and HDMI */ |
| 351 | intel_de_write(display, VLV_AUD_PORT_EN_DBG(port), |
| 352 | val: 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 | intel_de_write(display, VLV_AUD_PORT_EN_DBG(port), |
| 361 | val: audio_enable | VLV_AMP_MUTE); |
| 362 | } |
| 363 | |
| 364 | if (pdata->notify_audio_lpe) |
| 365 | pdata->notify_audio_lpe(display->audio.lpe.platdev, port - PORT_B); |
| 366 | |
| 367 | spin_unlock_irqrestore(lock: &pdata->lpe_audio_slock, flags: irqflags); |
| 368 | } |
| 369 | |