hdac_i915.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * hdac_i915.c - routines for sync between HD-A core and i915 display driver
  4. */
  5. #include <linux/init.h>
  6. #include <linux/module.h>
  7. #include <linux/pci.h>
  8. #include <sound/core.h>
  9. #include <sound/hdaudio.h>
  10. #include <sound/hda_i915.h>
  11. #include <sound/hda_register.h>
  12. #include <video/nomodeset.h>
  13. static int gpu_bind = -1;
  14. module_param(gpu_bind, int, 0644);
  15. MODULE_PARM_DESC(gpu_bind, "Whether to bind sound component to GPU "
  16. "(1=always, 0=never, -1=on nomodeset(default))");
  17. /**
  18. * snd_hdac_i915_set_bclk - Reprogram BCLK for HSW/BDW
  19. * @bus: HDA core bus
  20. *
  21. * Intel HSW/BDW display HDA controller is in GPU. Both its power and link BCLK
  22. * depends on GPU. Two Extended Mode registers EM4 (M value) and EM5 (N Value)
  23. * are used to convert CDClk (Core Display Clock) to 24MHz BCLK:
  24. * BCLK = CDCLK * M / N
  25. * The values will be lost when the display power well is disabled and need to
  26. * be restored to avoid abnormal playback speed.
  27. *
  28. * Call this function at initializing and changing power well, as well as
  29. * at ELD notifier for the hotplug.
  30. */
  31. void snd_hdac_i915_set_bclk(struct hdac_bus *bus)
  32. {
  33. struct drm_audio_component *acomp = bus->audio_component;
  34. struct pci_dev *pci = to_pci_dev(bus->dev);
  35. int cdclk_freq;
  36. unsigned int bclk_m, bclk_n;
  37. if (!acomp || !acomp->ops || !acomp->ops->get_cdclk_freq)
  38. return; /* only for i915 binding */
  39. if (!HDA_CONTROLLER_IS_HSW(pci))
  40. return; /* only HSW/BDW */
  41. cdclk_freq = acomp->ops->get_cdclk_freq(acomp->dev);
  42. switch (cdclk_freq) {
  43. case 337500:
  44. bclk_m = 16;
  45. bclk_n = 225;
  46. break;
  47. case 450000:
  48. default: /* default CDCLK 450MHz */
  49. bclk_m = 4;
  50. bclk_n = 75;
  51. break;
  52. case 540000:
  53. bclk_m = 4;
  54. bclk_n = 90;
  55. break;
  56. case 675000:
  57. bclk_m = 8;
  58. bclk_n = 225;
  59. break;
  60. }
  61. snd_hdac_chip_writew(bus, HSW_EM4, bclk_m);
  62. snd_hdac_chip_writew(bus, HSW_EM5, bclk_n);
  63. }
  64. EXPORT_SYMBOL_GPL(snd_hdac_i915_set_bclk);
  65. /* returns true if the devices can be connected for audio */
  66. static bool connectivity_check(struct pci_dev *i915, struct pci_dev *hdac)
  67. {
  68. struct pci_bus *bus_a = i915->bus, *bus_b = hdac->bus;
  69. /* directly connected on the same bus */
  70. if (bus_a == bus_b)
  71. return true;
  72. bus_a = bus_a->parent;
  73. bus_b = bus_b->parent;
  74. /* connected via parent bus (may be NULL!) */
  75. if (bus_a == bus_b)
  76. return true;
  77. if (!bus_a || !bus_b)
  78. return false;
  79. /*
  80. * on i915 discrete GPUs with embedded HDA audio, the two
  81. * devices are connected via 2nd level PCI bridge
  82. */
  83. bus_a = bus_a->parent;
  84. bus_b = bus_b->parent;
  85. if (bus_a && bus_a == bus_b)
  86. return true;
  87. return false;
  88. }
  89. static int i915_component_master_match(struct device *dev, int subcomponent,
  90. void *data)
  91. {
  92. struct pci_dev *hdac_pci, *i915_pci;
  93. struct hdac_bus *bus = data;
  94. if (!dev_is_pci(dev))
  95. return 0;
  96. hdac_pci = to_pci_dev(bus->dev);
  97. i915_pci = to_pci_dev(dev);
  98. if ((!strcmp(dev->driver->name, "i915") ||
  99. !strcmp(dev->driver->name, "xe")) &&
  100. subcomponent == I915_COMPONENT_AUDIO &&
  101. connectivity_check(i915_pci, hdac_pci))
  102. return 1;
  103. return 0;
  104. }
  105. /* check whether Intel graphics is present and reachable */
  106. static int i915_gfx_present(struct pci_dev *hdac_pci)
  107. {
  108. /* List of known platforms with no i915 support. */
  109. static const struct pci_device_id denylist[] = {
  110. /* CNL */
  111. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a40), 0x030000, 0xff0000 },
  112. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a41), 0x030000, 0xff0000 },
  113. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a42), 0x030000, 0xff0000 },
  114. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a44), 0x030000, 0xff0000 },
  115. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a49), 0x030000, 0xff0000 },
  116. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a4a), 0x030000, 0xff0000 },
  117. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a4c), 0x030000, 0xff0000 },
  118. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a50), 0x030000, 0xff0000 },
  119. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a51), 0x030000, 0xff0000 },
  120. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a52), 0x030000, 0xff0000 },
  121. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a54), 0x030000, 0xff0000 },
  122. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a59), 0x030000, 0xff0000 },
  123. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a5a), 0x030000, 0xff0000 },
  124. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x5a5c), 0x030000, 0xff0000 },
  125. /* LKF */
  126. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x9840), 0x030000, 0xff0000 },
  127. {}
  128. };
  129. struct pci_dev *display_dev = NULL;
  130. if (!gpu_bind || (gpu_bind < 0 && video_firmware_drivers_only()))
  131. return false;
  132. for_each_pci_dev(display_dev) {
  133. if (display_dev->vendor != PCI_VENDOR_ID_INTEL ||
  134. (display_dev->class >> 16) != PCI_BASE_CLASS_DISPLAY)
  135. continue;
  136. if (pci_match_id(denylist, display_dev))
  137. continue;
  138. if (connectivity_check(display_dev, hdac_pci)) {
  139. pci_dev_put(display_dev);
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. /**
  146. * snd_hdac_i915_init - Initialize i915 audio component
  147. * @bus: HDA core bus
  148. *
  149. * This function is supposed to be used only by a HD-audio controller
  150. * driver that needs the interaction with i915 graphics.
  151. *
  152. * This function initializes and sets up the audio component to communicate
  153. * with i915 graphics driver.
  154. *
  155. * Returns zero for success or a negative error code.
  156. */
  157. int snd_hdac_i915_init(struct hdac_bus *bus)
  158. {
  159. struct drm_audio_component *acomp;
  160. int err;
  161. if (!i915_gfx_present(to_pci_dev(bus->dev)))
  162. return -ENODEV;
  163. err = snd_hdac_acomp_init(bus, NULL,
  164. i915_component_master_match,
  165. sizeof(struct i915_audio_component) - sizeof(*acomp));
  166. if (err < 0)
  167. return err;
  168. acomp = bus->audio_component;
  169. if (!acomp)
  170. return -ENODEV;
  171. if (!acomp->ops) {
  172. snd_hdac_acomp_exit(bus);
  173. return dev_err_probe(bus->dev, -EPROBE_DEFER,
  174. "couldn't bind with audio component\n");
  175. }
  176. return 0;
  177. }
  178. EXPORT_SYMBOL_GPL(snd_hdac_i915_init);