intel_lpe_audio.c 10 KB

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