hdac_i915.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #define IS_HSW_CONTROLLER(pci) (((pci)->device == 0x0a0c) || \
  13. ((pci)->device == 0x0c0c) || \
  14. ((pci)->device == 0x0d0c) || \
  15. ((pci)->device == 0x160c))
  16. /**
  17. * snd_hdac_i915_set_bclk - Reprogram BCLK for HSW/BDW
  18. * @bus: HDA core bus
  19. *
  20. * Intel HSW/BDW display HDA controller is in GPU. Both its power and link BCLK
  21. * depends on GPU. Two Extended Mode registers EM4 (M value) and EM5 (N Value)
  22. * are used to convert CDClk (Core Display Clock) to 24MHz BCLK:
  23. * BCLK = CDCLK * M / N
  24. * The values will be lost when the display power well is disabled and need to
  25. * be restored to avoid abnormal playback speed.
  26. *
  27. * Call this function at initializing and changing power well, as well as
  28. * at ELD notifier for the hotplug.
  29. */
  30. void snd_hdac_i915_set_bclk(struct hdac_bus *bus)
  31. {
  32. struct drm_audio_component *acomp = bus->audio_component;
  33. struct pci_dev *pci = to_pci_dev(bus->dev);
  34. int cdclk_freq;
  35. unsigned int bclk_m, bclk_n;
  36. if (!acomp || !acomp->ops || !acomp->ops->get_cdclk_freq)
  37. return; /* only for i915 binding */
  38. if (!IS_HSW_CONTROLLER(pci))
  39. return; /* only HSW/BDW */
  40. cdclk_freq = acomp->ops->get_cdclk_freq(acomp->dev);
  41. switch (cdclk_freq) {
  42. case 337500:
  43. bclk_m = 16;
  44. bclk_n = 225;
  45. break;
  46. case 450000:
  47. default: /* default CDCLK 450MHz */
  48. bclk_m = 4;
  49. bclk_n = 75;
  50. break;
  51. case 540000:
  52. bclk_m = 4;
  53. bclk_n = 90;
  54. break;
  55. case 675000:
  56. bclk_m = 8;
  57. bclk_n = 225;
  58. break;
  59. }
  60. snd_hdac_chip_writew(bus, HSW_EM4, bclk_m);
  61. snd_hdac_chip_writew(bus, HSW_EM5, bclk_n);
  62. }
  63. EXPORT_SYMBOL_GPL(snd_hdac_i915_set_bclk);
  64. /* returns true if the devices can be connected for audio */
  65. static bool connectivity_check(struct pci_dev *i915, struct pci_dev *hdac)
  66. {
  67. struct pci_bus *bus_a = i915->bus, *bus_b = hdac->bus;
  68. /* directly connected on the same bus */
  69. if (bus_a == bus_b)
  70. return true;
  71. /*
  72. * on i915 discrete GPUs with embedded HDA audio, the two
  73. * devices are connected via 2nd level PCI bridge
  74. */
  75. bus_a = bus_a->parent;
  76. bus_b = bus_b->parent;
  77. if (!bus_a || !bus_b)
  78. return false;
  79. bus_a = bus_a->parent;
  80. bus_b = bus_b->parent;
  81. if (bus_a && bus_a == bus_b)
  82. return true;
  83. return false;
  84. }
  85. static int i915_component_master_match(struct device *dev, int subcomponent,
  86. void *data)
  87. {
  88. struct pci_dev *hdac_pci, *i915_pci;
  89. struct hdac_bus *bus = data;
  90. if (!dev_is_pci(dev))
  91. return 0;
  92. hdac_pci = to_pci_dev(bus->dev);
  93. i915_pci = to_pci_dev(dev);
  94. if (!strcmp(dev->driver->name, "i915") &&
  95. subcomponent == I915_COMPONENT_AUDIO &&
  96. connectivity_check(i915_pci, hdac_pci))
  97. return 1;
  98. return 0;
  99. }
  100. /* check whether intel graphics is present */
  101. static bool i915_gfx_present(void)
  102. {
  103. static const struct pci_device_id ids[] = {
  104. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_ANY_ID),
  105. .class = PCI_BASE_CLASS_DISPLAY << 16,
  106. .class_mask = 0xff << 16 },
  107. {}
  108. };
  109. return pci_dev_present(ids);
  110. }
  111. /**
  112. * snd_hdac_i915_init - Initialize i915 audio component
  113. * @bus: HDA core bus
  114. *
  115. * This function is supposed to be used only by a HD-audio controller
  116. * driver that needs the interaction with i915 graphics.
  117. *
  118. * This function initializes and sets up the audio component to communicate
  119. * with i915 graphics driver.
  120. *
  121. * Returns zero for success or a negative error code.
  122. */
  123. int snd_hdac_i915_init(struct hdac_bus *bus)
  124. {
  125. struct drm_audio_component *acomp;
  126. int err;
  127. if (!i915_gfx_present())
  128. return -ENODEV;
  129. err = snd_hdac_acomp_init(bus, NULL,
  130. i915_component_master_match,
  131. sizeof(struct i915_audio_component) - sizeof(*acomp));
  132. if (err < 0)
  133. return err;
  134. acomp = bus->audio_component;
  135. if (!acomp)
  136. return -ENODEV;
  137. if (!acomp->ops) {
  138. if (!IS_ENABLED(CONFIG_MODULES) ||
  139. !request_module("i915")) {
  140. /* 60s timeout */
  141. wait_for_completion_timeout(&acomp->master_bind_complete,
  142. msecs_to_jiffies(60 * 1000));
  143. }
  144. }
  145. if (!acomp->ops) {
  146. dev_info(bus->dev, "couldn't bind with audio component\n");
  147. snd_hdac_acomp_exit(bus);
  148. return -ENODEV;
  149. }
  150. return 0;
  151. }
  152. EXPORT_SYMBOL_GPL(snd_hdac_i915_init);