hda_i915.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_set_codec_wakeup(struct hda_intel *hda, bool enable)
  34. {
  35. struct i915_audio_component *acomp = &hda->audio_component;
  36. if (!acomp->ops)
  37. return -ENODEV;
  38. if (!acomp->ops->codec_wake_override) {
  39. dev_warn(&hda->chip.pci->dev,
  40. "Invalid codec wake callback\n");
  41. return 0;
  42. }
  43. dev_dbg(&hda->chip.pci->dev, "%s codec wakeup\n",
  44. enable ? "enable" : "disable");
  45. acomp->ops->codec_wake_override(acomp->dev, enable);
  46. return 0;
  47. }
  48. int hda_display_power(struct hda_intel *hda, bool enable)
  49. {
  50. struct i915_audio_component *acomp = &hda->audio_component;
  51. if (!acomp->ops)
  52. return -ENODEV;
  53. dev_dbg(&hda->chip.pci->dev, "display power %s\n",
  54. enable ? "enable" : "disable");
  55. if (enable) {
  56. if (!hda->i915_power_refcount++)
  57. acomp->ops->get_power(acomp->dev);
  58. } else {
  59. WARN_ON(!hda->i915_power_refcount);
  60. if (!--hda->i915_power_refcount)
  61. acomp->ops->put_power(acomp->dev);
  62. }
  63. return 0;
  64. }
  65. void haswell_set_bclk(struct hda_intel *hda)
  66. {
  67. int cdclk_freq;
  68. unsigned int bclk_m, bclk_n;
  69. struct i915_audio_component *acomp = &hda->audio_component;
  70. struct pci_dev *pci = hda->chip.pci;
  71. /* Only Haswell/Broadwell need set BCLK */
  72. if (pci->device != 0x0a0c && pci->device != 0x0c0c
  73. && pci->device != 0x0d0c && pci->device != 0x160c)
  74. return;
  75. if (!acomp->ops)
  76. return;
  77. cdclk_freq = acomp->ops->get_cdclk_freq(acomp->dev);
  78. switch (cdclk_freq) {
  79. case 337500:
  80. bclk_m = 16;
  81. bclk_n = 225;
  82. break;
  83. case 450000:
  84. default: /* default CDCLK 450MHz */
  85. bclk_m = 4;
  86. bclk_n = 75;
  87. break;
  88. case 540000:
  89. bclk_m = 4;
  90. bclk_n = 90;
  91. break;
  92. case 675000:
  93. bclk_m = 8;
  94. bclk_n = 225;
  95. break;
  96. }
  97. azx_writew(&hda->chip, EM4, bclk_m);
  98. azx_writew(&hda->chip, EM5, bclk_n);
  99. }
  100. static int hda_component_master_bind(struct device *dev)
  101. {
  102. struct snd_card *card = dev_get_drvdata(dev);
  103. struct azx *chip = card->private_data;
  104. struct hda_intel *hda = container_of(chip, struct hda_intel, chip);
  105. struct i915_audio_component *acomp = &hda->audio_component;
  106. int ret;
  107. ret = component_bind_all(dev, acomp);
  108. if (ret < 0)
  109. return ret;
  110. if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power &&
  111. acomp->ops->put_power && acomp->ops->get_cdclk_freq))) {
  112. ret = -EINVAL;
  113. goto out_unbind;
  114. }
  115. /*
  116. * Atm, we don't support dynamic unbinding initiated by the child
  117. * component, so pin its containing module until we unbind.
  118. */
  119. if (!try_module_get(acomp->ops->owner)) {
  120. ret = -ENODEV;
  121. goto out_unbind;
  122. }
  123. return 0;
  124. out_unbind:
  125. component_unbind_all(dev, acomp);
  126. return ret;
  127. }
  128. static void hda_component_master_unbind(struct device *dev)
  129. {
  130. struct snd_card *card = dev_get_drvdata(dev);
  131. struct azx *chip = card->private_data;
  132. struct hda_intel *hda = container_of(chip, struct hda_intel, chip);
  133. struct i915_audio_component *acomp = &hda->audio_component;
  134. module_put(acomp->ops->owner);
  135. component_unbind_all(dev, acomp);
  136. WARN_ON(acomp->ops || acomp->dev);
  137. }
  138. static const struct component_master_ops hda_component_master_ops = {
  139. .bind = hda_component_master_bind,
  140. .unbind = hda_component_master_unbind,
  141. };
  142. static int hda_component_master_match(struct device *dev, void *data)
  143. {
  144. /* i915 is the only supported component */
  145. return !strcmp(dev->driver->name, "i915");
  146. }
  147. int hda_i915_init(struct hda_intel *hda)
  148. {
  149. struct component_match *match = NULL;
  150. struct device *dev = &hda->chip.pci->dev;
  151. struct i915_audio_component *acomp = &hda->audio_component;
  152. int ret;
  153. component_match_add(dev, &match, hda_component_master_match, hda);
  154. ret = component_master_add_with_match(dev, &hda_component_master_ops,
  155. match);
  156. if (ret < 0)
  157. goto out_err;
  158. /*
  159. * Atm, we don't support deferring the component binding, so make sure
  160. * i915 is loaded and that the binding successfully completes.
  161. */
  162. request_module("i915");
  163. if (!acomp->ops) {
  164. ret = -ENODEV;
  165. goto out_master_del;
  166. }
  167. dev_dbg(dev, "bound to i915 component master\n");
  168. return 0;
  169. out_master_del:
  170. component_master_del(dev, &hda_component_master_ops);
  171. out_err:
  172. dev_err(dev, "failed to add i915 component master (%d)\n", ret);
  173. return ret;
  174. }
  175. int hda_i915_exit(struct hda_intel *hda)
  176. {
  177. struct device *dev = &hda->chip.pci->dev;
  178. struct i915_audio_component *acomp = &hda->audio_component;
  179. WARN_ON(hda->i915_power_refcount);
  180. if (hda->i915_power_refcount > 0 && acomp->ops)
  181. acomp->ops->put_power(acomp->dev);
  182. component_master_del(dev, &hda_component_master_ops);
  183. return 0;
  184. }