intel_lpe_audio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. * DOC: LPE Audio integration for HDMI or DP playback
  31. *
  32. * Motivation:
  33. * Atom platforms (e.g. valleyview and cherryTrail) integrates a DMA-based
  34. * interface as an alternative to the traditional HDaudio path. While this
  35. * mode is unrelated to the LPE aka SST audio engine, the documentation refers
  36. * to this mode as LPE so we keep this notation for the sake of consistency.
  37. *
  38. * The interface is handled by a separate standalone driver maintained in the
  39. * ALSA subsystem for simplicity. To minimize the interaction between the two
  40. * subsystems, a bridge is setup between the hdmi-lpe-audio and i915:
  41. * 1. Create a platform device to share MMIO/IRQ resources
  42. * 2. Make the platform device child of i915 device for runtime PM.
  43. * 3. Create IRQ chip to forward the LPE audio irqs.
  44. * the hdmi-lpe-audio driver probes the lpe audio device and creates a new
  45. * sound card
  46. *
  47. * Threats:
  48. * Due to the restriction in Linux platform device model, user need manually
  49. * uninstall the hdmi-lpe-audio driver before uninstalling i915 module,
  50. * otherwise we might run into use-after-free issues after i915 removes the
  51. * platform device: even though hdmi-lpe-audio driver is released, the modules
  52. * is still in "installed" status.
  53. *
  54. * Implementation:
  55. * The MMIO/REG platform resources are created according to the registers
  56. * specification.
  57. * When forwarding LPE audio irqs, the flow control handler selection depends
  58. * on the platform, for example on valleyview handle_simple_irq is enough.
  59. *
  60. */
  61. #include <linux/acpi.h>
  62. #include <linux/device.h>
  63. #include <linux/pci.h>
  64. #include <linux/pm_runtime.h>
  65. #include "i915_drv.h"
  66. #include <linux/delay.h>
  67. #include <drm/intel_lpe_audio.h>
  68. #define HAS_LPE_AUDIO(dev_priv) ((dev_priv)->lpe_audio.platdev != NULL)
  69. static struct platform_device *
  70. lpe_audio_platdev_create(struct drm_i915_private *dev_priv)
  71. {
  72. struct drm_device *dev = &dev_priv->drm;
  73. struct platform_device_info pinfo = {};
  74. struct resource *rsc;
  75. struct platform_device *platdev;
  76. struct intel_hdmi_lpe_audio_pdata *pdata;
  77. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  78. if (!pdata)
  79. return ERR_PTR(-ENOMEM);
  80. rsc = kcalloc(2, sizeof(*rsc), GFP_KERNEL);
  81. if (!rsc) {
  82. kfree(pdata);
  83. return ERR_PTR(-ENOMEM);
  84. }
  85. rsc[0].start = rsc[0].end = dev_priv->lpe_audio.irq;
  86. rsc[0].flags = IORESOURCE_IRQ;
  87. rsc[0].name = "hdmi-lpe-audio-irq";
  88. rsc[1].start = pci_resource_start(dev->pdev, 0) +
  89. I915_HDMI_LPE_AUDIO_BASE;
  90. rsc[1].end = pci_resource_start(dev->pdev, 0) +
  91. I915_HDMI_LPE_AUDIO_BASE + I915_HDMI_LPE_AUDIO_SIZE - 1;
  92. rsc[1].flags = IORESOURCE_MEM;
  93. rsc[1].name = "hdmi-lpe-audio-mmio";
  94. pinfo.parent = dev->dev;
  95. pinfo.name = "hdmi-lpe-audio";
  96. pinfo.id = -1;
  97. pinfo.res = rsc;
  98. pinfo.num_res = 2;
  99. pinfo.data = pdata;
  100. pinfo.size_data = sizeof(*pdata);
  101. pinfo.dma_mask = DMA_BIT_MASK(32);
  102. pdata->num_pipes = INTEL_INFO(dev_priv)->num_pipes;
  103. pdata->num_ports = IS_CHERRYVIEW(dev_priv) ? 3 : 2; /* B,C,D or B,C */
  104. pdata->port[0].pipe = -1;
  105. pdata->port[1].pipe = -1;
  106. pdata->port[2].pipe = -1;
  107. spin_lock_init(&pdata->lpe_audio_slock);
  108. platdev = platform_device_register_full(&pinfo);
  109. kfree(rsc);
  110. kfree(pdata);
  111. if (IS_ERR(platdev)) {
  112. DRM_ERROR("Failed to allocate LPE audio platform device\n");
  113. return platdev;
  114. }
  115. pm_runtime_forbid(&platdev->dev);
  116. pm_runtime_set_active(&platdev->dev);
  117. pm_runtime_enable(&platdev->dev);
  118. return platdev;
  119. }
  120. static void lpe_audio_platdev_destroy(struct drm_i915_private *dev_priv)
  121. {
  122. /* XXX Note that platform_device_register_full() allocates a dma_mask
  123. * and never frees it. We can't free it here as we cannot guarantee
  124. * this is the last reference (i.e. that the dma_mask will not be
  125. * used after our unregister). So ee choose to leak the sizeof(u64)
  126. * allocation here - it should be fixed in the platform_device rather
  127. * than us fiddle with its internals.
  128. */
  129. platform_device_unregister(dev_priv->lpe_audio.platdev);
  130. }
  131. static void lpe_audio_irq_unmask(struct irq_data *d)
  132. {
  133. }
  134. static void lpe_audio_irq_mask(struct irq_data *d)
  135. {
  136. }
  137. static struct irq_chip lpe_audio_irqchip = {
  138. .name = "hdmi_lpe_audio_irqchip",
  139. .irq_mask = lpe_audio_irq_mask,
  140. .irq_unmask = lpe_audio_irq_unmask,
  141. };
  142. static int lpe_audio_irq_init(struct drm_i915_private *dev_priv)
  143. {
  144. int irq = dev_priv->lpe_audio.irq;
  145. WARN_ON(!intel_irqs_enabled(dev_priv));
  146. irq_set_chip_and_handler_name(irq,
  147. &lpe_audio_irqchip,
  148. handle_simple_irq,
  149. "hdmi_lpe_audio_irq_handler");
  150. return irq_set_chip_data(irq, dev_priv);
  151. }
  152. static bool lpe_audio_detect(struct drm_i915_private *dev_priv)
  153. {
  154. int lpe_present = false;
  155. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  156. static const struct pci_device_id atom_hdaudio_ids[] = {
  157. /* Baytrail */
  158. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x0f04)},
  159. /* Braswell */
  160. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2284)},
  161. {}
  162. };
  163. if (!pci_dev_present(atom_hdaudio_ids)) {
  164. DRM_INFO("HDaudio controller not detected, using LPE audio instead\n");
  165. lpe_present = true;
  166. }
  167. }
  168. return lpe_present;
  169. }
  170. static int lpe_audio_setup(struct drm_i915_private *dev_priv)
  171. {
  172. int ret;
  173. dev_priv->lpe_audio.irq = irq_alloc_desc(0);
  174. if (dev_priv->lpe_audio.irq < 0) {
  175. DRM_ERROR("Failed to allocate IRQ desc: %d\n",
  176. dev_priv->lpe_audio.irq);
  177. ret = dev_priv->lpe_audio.irq;
  178. goto err;
  179. }
  180. DRM_DEBUG("irq = %d\n", dev_priv->lpe_audio.irq);
  181. ret = lpe_audio_irq_init(dev_priv);
  182. if (ret) {
  183. DRM_ERROR("Failed to initialize irqchip for lpe audio: %d\n",
  184. ret);
  185. goto err_free_irq;
  186. }
  187. dev_priv->lpe_audio.platdev = lpe_audio_platdev_create(dev_priv);
  188. if (IS_ERR(dev_priv->lpe_audio.platdev)) {
  189. ret = PTR_ERR(dev_priv->lpe_audio.platdev);
  190. DRM_ERROR("Failed to create lpe audio platform device: %d\n",
  191. ret);
  192. goto err_free_irq;
  193. }
  194. /* enable chicken bit; at least this is required for Dell Wyse 3040
  195. * with DP outputs (but only sometimes by some reason!)
  196. */
  197. I915_WRITE(VLV_AUD_CHICKEN_BIT_REG, VLV_CHICKEN_BIT_DBG_ENABLE);
  198. return 0;
  199. err_free_irq:
  200. irq_free_desc(dev_priv->lpe_audio.irq);
  201. err:
  202. dev_priv->lpe_audio.irq = -1;
  203. dev_priv->lpe_audio.platdev = NULL;
  204. return ret;
  205. }
  206. /**
  207. * intel_lpe_audio_irq_handler() - forwards the LPE audio irq
  208. * @dev_priv: the i915 drm device private data
  209. *
  210. * the LPE Audio irq is forwarded to the irq handler registered by LPE audio
  211. * driver.
  212. */
  213. void intel_lpe_audio_irq_handler(struct drm_i915_private *dev_priv)
  214. {
  215. int ret;
  216. if (!HAS_LPE_AUDIO(dev_priv))
  217. return;
  218. ret = generic_handle_irq(dev_priv->lpe_audio.irq);
  219. if (ret)
  220. DRM_ERROR_RATELIMITED("error handling LPE audio irq: %d\n",
  221. ret);
  222. }
  223. /**
  224. * intel_lpe_audio_init() - detect and setup the bridge between HDMI LPE Audio
  225. * driver and i915
  226. * @dev_priv: the i915 drm device private data
  227. *
  228. * Return: 0 if successful. non-zero if detection or
  229. * llocation/initialization fails
  230. */
  231. int intel_lpe_audio_init(struct drm_i915_private *dev_priv)
  232. {
  233. int ret = -ENODEV;
  234. if (lpe_audio_detect(dev_priv)) {
  235. ret = lpe_audio_setup(dev_priv);
  236. if (ret < 0)
  237. DRM_ERROR("failed to setup LPE Audio bridge\n");
  238. }
  239. return ret;
  240. }
  241. /**
  242. * intel_lpe_audio_teardown() - destroy the bridge between HDMI LPE
  243. * audio driver and i915
  244. * @dev_priv: the i915 drm device private data
  245. *
  246. * release all the resources for LPE audio <-> i915 bridge.
  247. */
  248. void intel_lpe_audio_teardown(struct drm_i915_private *dev_priv)
  249. {
  250. struct irq_desc *desc;
  251. if (!HAS_LPE_AUDIO(dev_priv))
  252. return;
  253. desc = irq_to_desc(dev_priv->lpe_audio.irq);
  254. lpe_audio_platdev_destroy(dev_priv);
  255. irq_free_desc(dev_priv->lpe_audio.irq);
  256. }
  257. /**
  258. * intel_lpe_audio_notify() - notify lpe audio event
  259. * audio driver and i915
  260. * @dev_priv: the i915 drm device private data
  261. * @pipe: pipe
  262. * @port: port
  263. * @eld : ELD data
  264. * @ls_clock: Link symbol clock in kHz
  265. * @dp_output: Driving a DP output?
  266. *
  267. * Notify lpe audio driver of eld change.
  268. */
  269. void intel_lpe_audio_notify(struct drm_i915_private *dev_priv,
  270. enum pipe pipe, enum port port,
  271. const void *eld, int ls_clock, bool dp_output)
  272. {
  273. unsigned long irqflags;
  274. struct intel_hdmi_lpe_audio_pdata *pdata;
  275. struct intel_hdmi_lpe_audio_port_pdata *ppdata;
  276. u32 audio_enable;
  277. if (!HAS_LPE_AUDIO(dev_priv))
  278. return;
  279. pdata = dev_get_platdata(&dev_priv->lpe_audio.platdev->dev);
  280. ppdata = &pdata->port[port - PORT_B];
  281. spin_lock_irqsave(&pdata->lpe_audio_slock, irqflags);
  282. audio_enable = I915_READ(VLV_AUD_PORT_EN_DBG(port));
  283. if (eld != NULL) {
  284. memcpy(ppdata->eld, eld, HDMI_MAX_ELD_BYTES);
  285. ppdata->pipe = pipe;
  286. ppdata->ls_clock = ls_clock;
  287. ppdata->dp_output = dp_output;
  288. /* Unmute the amp for both DP and HDMI */
  289. I915_WRITE(VLV_AUD_PORT_EN_DBG(port),
  290. audio_enable & ~VLV_AMP_MUTE);
  291. } else {
  292. memset(ppdata->eld, 0, HDMI_MAX_ELD_BYTES);
  293. ppdata->pipe = -1;
  294. ppdata->ls_clock = 0;
  295. ppdata->dp_output = false;
  296. /* Mute the amp for both DP and HDMI */
  297. I915_WRITE(VLV_AUD_PORT_EN_DBG(port),
  298. audio_enable | VLV_AMP_MUTE);
  299. }
  300. if (pdata->notify_audio_lpe)
  301. pdata->notify_audio_lpe(dev_priv->lpe_audio.platdev, port - PORT_B);
  302. spin_unlock_irqrestore(&pdata->lpe_audio_slock, irqflags);
  303. }