hda_i915.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * hda_i915.c - routines for Haswell HDA controller power well support
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/pci.h>
  21. #include <linux/component.h>
  22. #include <drm/i915_component.h>
  23. #include <sound/core.h>
  24. #include "hda_controller.h"
  25. #include "hda_intel.h"
  26. /* Intel HSW/BDW display HDA controller Extended Mode registers.
  27. * EM4 (M value) and EM5 (N Value) are used to convert CDClk (Core Display
  28. * Clock) to 24MHz BCLK: BCLK = CDCLK * M / N
  29. * The values will be lost when the display power well is disabled.
  30. */
  31. #define AZX_REG_EM4 0x100c
  32. #define AZX_REG_EM5 0x1010
  33. int hda_display_power(struct hda_intel *hda, bool enable)
  34. {
  35. struct i915_audio_component *acomp = &hda->audio_component;
  36. if (!acomp->ops)
  37. return -ENODEV;
  38. dev_dbg(&hda->chip.pci->dev, "display power %s\n",
  39. enable ? "enable" : "disable");
  40. if (enable)
  41. acomp->ops->get_power(acomp->dev);
  42. else
  43. acomp->ops->put_power(acomp->dev);
  44. return 0;
  45. }
  46. void haswell_set_bclk(struct hda_intel *hda)
  47. {
  48. int cdclk_freq;
  49. unsigned int bclk_m, bclk_n;
  50. struct i915_audio_component *acomp = &hda->audio_component;
  51. struct pci_dev *pci = hda->chip.pci;
  52. /* Only Haswell/Broadwell need set BCLK */
  53. if (pci->device != 0x0a0c && pci->device != 0x0c0c
  54. && pci->device != 0x0d0c && pci->device != 0x160c)
  55. return;
  56. if (!acomp->ops)
  57. return;
  58. cdclk_freq = acomp->ops->get_cdclk_freq(acomp->dev);
  59. switch (cdclk_freq) {
  60. case 337500:
  61. bclk_m = 16;
  62. bclk_n = 225;
  63. break;
  64. case 450000:
  65. default: /* default CDCLK 450MHz */
  66. bclk_m = 4;
  67. bclk_n = 75;
  68. break;
  69. case 540000:
  70. bclk_m = 4;
  71. bclk_n = 90;
  72. break;
  73. case 675000:
  74. bclk_m = 8;
  75. bclk_n = 225;
  76. break;
  77. }
  78. azx_writew(&hda->chip, EM4, bclk_m);
  79. azx_writew(&hda->chip, EM5, bclk_n);
  80. }
  81. static int hda_component_master_bind(struct device *dev)
  82. {
  83. struct snd_card *card = dev_get_drvdata(dev);
  84. struct azx *chip = card->private_data;
  85. struct hda_intel *hda = container_of(chip, struct hda_intel, chip);
  86. struct i915_audio_component *acomp = &hda->audio_component;
  87. int ret;
  88. ret = component_bind_all(dev, acomp);
  89. if (ret < 0)
  90. return ret;
  91. if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power &&
  92. acomp->ops->put_power && acomp->ops->get_cdclk_freq))) {
  93. ret = -EINVAL;
  94. goto out_unbind;
  95. }
  96. /*
  97. * Atm, we don't support dynamic unbinding initiated by the child
  98. * component, so pin its containing module until we unbind.
  99. */
  100. if (!try_module_get(acomp->ops->owner)) {
  101. ret = -ENODEV;
  102. goto out_unbind;
  103. }
  104. return 0;
  105. out_unbind:
  106. component_unbind_all(dev, acomp);
  107. return ret;
  108. }
  109. static void hda_component_master_unbind(struct device *dev)
  110. {
  111. struct snd_card *card = dev_get_drvdata(dev);
  112. struct azx *chip = card->private_data;
  113. struct hda_intel *hda = container_of(chip, struct hda_intel, chip);
  114. struct i915_audio_component *acomp = &hda->audio_component;
  115. module_put(acomp->ops->owner);
  116. component_unbind_all(dev, acomp);
  117. WARN_ON(acomp->ops || acomp->dev);
  118. }
  119. static const struct component_master_ops hda_component_master_ops = {
  120. .bind = hda_component_master_bind,
  121. .unbind = hda_component_master_unbind,
  122. };
  123. static int hda_component_master_match(struct device *dev, void *data)
  124. {
  125. /* i915 is the only supported component */
  126. return !strcmp(dev->driver->name, "i915");
  127. }
  128. int hda_i915_init(struct hda_intel *hda)
  129. {
  130. struct component_match *match = NULL;
  131. struct device *dev = &hda->chip.pci->dev;
  132. struct i915_audio_component *acomp = &hda->audio_component;
  133. int ret;
  134. component_match_add(dev, &match, hda_component_master_match, hda);
  135. ret = component_master_add_with_match(dev, &hda_component_master_ops,
  136. match);
  137. if (ret < 0)
  138. goto out_err;
  139. /*
  140. * Atm, we don't support deferring the component binding, so make sure
  141. * i915 is loaded and that the binding successfully completes.
  142. */
  143. request_module("i915");
  144. if (!acomp->ops) {
  145. ret = -ENODEV;
  146. goto out_master_del;
  147. }
  148. dev_dbg(dev, "bound to i915 component master\n");
  149. return 0;
  150. out_master_del:
  151. component_master_del(dev, &hda_component_master_ops);
  152. out_err:
  153. dev_err(dev, "failed to add i915 component master (%d)\n", ret);
  154. return ret;
  155. }
  156. int hda_i915_exit(struct hda_intel *hda)
  157. {
  158. struct device *dev = &hda->chip.pci->dev;
  159. component_master_del(dev, &hda_component_master_ops);
  160. return 0;
  161. }