intel_lpe_audio.c 10 KB

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