intel_lpe_audio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 "i915_drv.h"
  65. #include <linux/delay.h>
  66. #include <drm/intel_lpe_audio.h>
  67. #define HAS_LPE_AUDIO(dev_priv) ((dev_priv)->lpe_audio.platdev != NULL)
  68. static struct platform_device *
  69. lpe_audio_platdev_create(struct drm_i915_private *dev_priv)
  70. {
  71. int ret;
  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. spin_lock_init(&pdata->lpe_audio_slock);
  103. platdev = platform_device_register_full(&pinfo);
  104. if (IS_ERR(platdev)) {
  105. ret = PTR_ERR(platdev);
  106. DRM_ERROR("Failed to allocate LPE audio platform device\n");
  107. goto err;
  108. }
  109. kfree(rsc);
  110. return platdev;
  111. err:
  112. kfree(rsc);
  113. kfree(pdata);
  114. return ERR_PTR(ret);
  115. }
  116. static void lpe_audio_platdev_destroy(struct drm_i915_private *dev_priv)
  117. {
  118. platform_device_unregister(dev_priv->lpe_audio.platdev);
  119. kfree(dev_priv->lpe_audio.platdev->dev.dma_mask);
  120. }
  121. static void lpe_audio_irq_unmask(struct irq_data *d)
  122. {
  123. struct drm_i915_private *dev_priv = d->chip_data;
  124. unsigned long irqflags;
  125. u32 val = (I915_LPE_PIPE_A_INTERRUPT |
  126. I915_LPE_PIPE_B_INTERRUPT);
  127. if (IS_CHERRYVIEW(dev_priv))
  128. val |= I915_LPE_PIPE_C_INTERRUPT;
  129. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  130. dev_priv->irq_mask &= ~val;
  131. I915_WRITE(VLV_IIR, val);
  132. I915_WRITE(VLV_IIR, val);
  133. I915_WRITE(VLV_IMR, dev_priv->irq_mask);
  134. POSTING_READ(VLV_IMR);
  135. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  136. }
  137. static void lpe_audio_irq_mask(struct irq_data *d)
  138. {
  139. struct drm_i915_private *dev_priv = d->chip_data;
  140. unsigned long irqflags;
  141. u32 val = (I915_LPE_PIPE_A_INTERRUPT |
  142. I915_LPE_PIPE_B_INTERRUPT);
  143. if (IS_CHERRYVIEW(dev_priv))
  144. val |= I915_LPE_PIPE_C_INTERRUPT;
  145. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  146. dev_priv->irq_mask |= val;
  147. I915_WRITE(VLV_IMR, dev_priv->irq_mask);
  148. I915_WRITE(VLV_IIR, val);
  149. I915_WRITE(VLV_IIR, val);
  150. POSTING_READ(VLV_IIR);
  151. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  152. }
  153. static struct irq_chip lpe_audio_irqchip = {
  154. .name = "hdmi_lpe_audio_irqchip",
  155. .irq_mask = lpe_audio_irq_mask,
  156. .irq_unmask = lpe_audio_irq_unmask,
  157. };
  158. static int lpe_audio_irq_init(struct drm_i915_private *dev_priv)
  159. {
  160. int irq = dev_priv->lpe_audio.irq;
  161. WARN_ON(!intel_irqs_enabled(dev_priv));
  162. irq_set_chip_and_handler_name(irq,
  163. &lpe_audio_irqchip,
  164. handle_simple_irq,
  165. "hdmi_lpe_audio_irq_handler");
  166. return irq_set_chip_data(irq, dev_priv);
  167. }
  168. static bool lpe_audio_detect(struct drm_i915_private *dev_priv)
  169. {
  170. int lpe_present = false;
  171. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  172. static const struct pci_device_id atom_hdaudio_ids[] = {
  173. /* Baytrail */
  174. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x0f04)},
  175. /* Braswell */
  176. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2284)},
  177. {}
  178. };
  179. if (!pci_dev_present(atom_hdaudio_ids)) {
  180. DRM_INFO("%s\n", "HDaudio controller not detected, using LPE audio instead\n");
  181. lpe_present = true;
  182. }
  183. }
  184. return lpe_present;
  185. }
  186. static int lpe_audio_setup(struct drm_i915_private *dev_priv)
  187. {
  188. int ret;
  189. dev_priv->lpe_audio.irq = irq_alloc_desc(0);
  190. if (dev_priv->lpe_audio.irq < 0) {
  191. DRM_ERROR("Failed to allocate IRQ desc: %d\n",
  192. dev_priv->lpe_audio.irq);
  193. ret = dev_priv->lpe_audio.irq;
  194. goto err;
  195. }
  196. DRM_DEBUG("irq = %d\n", dev_priv->lpe_audio.irq);
  197. ret = lpe_audio_irq_init(dev_priv);
  198. if (ret) {
  199. DRM_ERROR("Failed to initialize irqchip for lpe audio: %d\n",
  200. ret);
  201. goto err_free_irq;
  202. }
  203. dev_priv->lpe_audio.platdev = lpe_audio_platdev_create(dev_priv);
  204. if (IS_ERR(dev_priv->lpe_audio.platdev)) {
  205. ret = PTR_ERR(dev_priv->lpe_audio.platdev);
  206. DRM_ERROR("Failed to create lpe audio platform device: %d\n",
  207. ret);
  208. goto err_free_irq;
  209. }
  210. /* enable chicken bit; at least this is required for Dell Wyse 3040
  211. * with DP outputs (but only sometimes by some reason!)
  212. */
  213. I915_WRITE(VLV_AUD_CHICKEN_BIT_REG, VLV_CHICKEN_BIT_DBG_ENABLE);
  214. return 0;
  215. err_free_irq:
  216. irq_free_desc(dev_priv->lpe_audio.irq);
  217. err:
  218. dev_priv->lpe_audio.irq = -1;
  219. dev_priv->lpe_audio.platdev = NULL;
  220. return ret;
  221. }
  222. /**
  223. * intel_lpe_audio_irq_handler() - forwards the LPE audio irq
  224. * @dev_priv: the i915 drm device private data
  225. *
  226. * the LPE Audio irq is forwarded to the irq handler registered by LPE audio
  227. * driver.
  228. */
  229. void intel_lpe_audio_irq_handler(struct drm_i915_private *dev_priv)
  230. {
  231. int ret;
  232. if (!HAS_LPE_AUDIO(dev_priv))
  233. return;
  234. ret = generic_handle_irq(dev_priv->lpe_audio.irq);
  235. if (ret)
  236. DRM_ERROR_RATELIMITED("error handling LPE audio irq: %d\n",
  237. ret);
  238. }
  239. /**
  240. * intel_lpe_audio_init() - detect and setup the bridge between HDMI LPE Audio
  241. * driver and i915
  242. * @dev_priv: the i915 drm device private data
  243. *
  244. * Return: 0 if successful. non-zero if detection or
  245. * llocation/initialization fails
  246. */
  247. int intel_lpe_audio_init(struct drm_i915_private *dev_priv)
  248. {
  249. int ret = -ENODEV;
  250. if (lpe_audio_detect(dev_priv)) {
  251. ret = lpe_audio_setup(dev_priv);
  252. if (ret < 0)
  253. DRM_ERROR("failed to setup LPE Audio bridge\n");
  254. }
  255. return ret;
  256. }
  257. /**
  258. * intel_lpe_audio_teardown() - destroy the bridge between HDMI LPE
  259. * audio driver and i915
  260. * @dev_priv: the i915 drm device private data
  261. *
  262. * release all the resources for LPE audio <-> i915 bridge.
  263. */
  264. void intel_lpe_audio_teardown(struct drm_i915_private *dev_priv)
  265. {
  266. struct irq_desc *desc;
  267. if (!HAS_LPE_AUDIO(dev_priv))
  268. return;
  269. desc = irq_to_desc(dev_priv->lpe_audio.irq);
  270. lpe_audio_irq_mask(&desc->irq_data);
  271. lpe_audio_platdev_destroy(dev_priv);
  272. irq_free_desc(dev_priv->lpe_audio.irq);
  273. }
  274. /**
  275. * intel_lpe_audio_notify() - notify lpe audio event
  276. * audio driver and i915
  277. * @dev_priv: the i915 drm device private data
  278. * @eld : ELD data
  279. * @port: port id
  280. * @tmds_clk_speed: tmds clock frequency in Hz
  281. *
  282. * Notify lpe audio driver of eld change.
  283. */
  284. void intel_lpe_audio_notify(struct drm_i915_private *dev_priv,
  285. void *eld, int port, int pipe, int tmds_clk_speed,
  286. bool dp_output, int link_rate)
  287. {
  288. unsigned long irq_flags;
  289. struct intel_hdmi_lpe_audio_pdata *pdata = NULL;
  290. u32 audio_enable;
  291. if (!HAS_LPE_AUDIO(dev_priv))
  292. return;
  293. pdata = dev_get_platdata(
  294. &(dev_priv->lpe_audio.platdev->dev));
  295. spin_lock_irqsave(&pdata->lpe_audio_slock, irq_flags);
  296. audio_enable = I915_READ(VLV_AUD_PORT_EN_DBG(port));
  297. if (eld != NULL) {
  298. memcpy(pdata->eld.eld_data, eld,
  299. HDMI_MAX_ELD_BYTES);
  300. pdata->eld.port_id = port;
  301. pdata->eld.pipe_id = pipe;
  302. pdata->hdmi_connected = true;
  303. pdata->dp_output = dp_output;
  304. if (tmds_clk_speed)
  305. pdata->tmds_clock_speed = tmds_clk_speed;
  306. if (link_rate)
  307. pdata->link_rate = link_rate;
  308. /* Unmute the amp for both DP and HDMI */
  309. I915_WRITE(VLV_AUD_PORT_EN_DBG(port),
  310. audio_enable & ~VLV_AMP_MUTE);
  311. } else {
  312. memset(pdata->eld.eld_data, 0,
  313. HDMI_MAX_ELD_BYTES);
  314. pdata->hdmi_connected = false;
  315. pdata->dp_output = false;
  316. /* Mute the amp for both DP and HDMI */
  317. I915_WRITE(VLV_AUD_PORT_EN_DBG(port),
  318. audio_enable | VLV_AMP_MUTE);
  319. }
  320. if (pdata->notify_audio_lpe)
  321. pdata->notify_audio_lpe(dev_priv->lpe_audio.platdev);
  322. else
  323. pdata->notify_pending = true;
  324. spin_unlock_irqrestore(&pdata->lpe_audio_slock,
  325. irq_flags);
  326. }