hdac_i915.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * hdac_i915.c - routines for sync between HD-A core and i915 display driver
  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. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/pci.h>
  17. #include <linux/component.h>
  18. #include <drm/i915_component.h>
  19. #include <sound/core.h>
  20. #include <sound/hdaudio.h>
  21. #include <sound/hda_i915.h>
  22. static struct i915_audio_component *hdac_acomp;
  23. int snd_hdac_set_codec_wakeup(struct hdac_bus *bus, bool enable)
  24. {
  25. struct i915_audio_component *acomp = bus->audio_component;
  26. if (!acomp || !acomp->ops)
  27. return -ENODEV;
  28. if (!acomp->ops->codec_wake_override) {
  29. dev_warn(bus->dev,
  30. "Invalid codec wake callback\n");
  31. return 0;
  32. }
  33. dev_dbg(bus->dev, "%s codec wakeup\n",
  34. enable ? "enable" : "disable");
  35. acomp->ops->codec_wake_override(acomp->dev, enable);
  36. return 0;
  37. }
  38. EXPORT_SYMBOL_GPL(snd_hdac_set_codec_wakeup);
  39. int snd_hdac_display_power(struct hdac_bus *bus, bool enable)
  40. {
  41. struct i915_audio_component *acomp = bus->audio_component;
  42. if (!acomp || !acomp->ops)
  43. return -ENODEV;
  44. dev_dbg(bus->dev, "display power %s\n",
  45. enable ? "enable" : "disable");
  46. if (enable) {
  47. if (!bus->i915_power_refcount++) {
  48. acomp->ops->get_power(acomp->dev);
  49. snd_hdac_set_codec_wakeup(bus, true);
  50. snd_hdac_set_codec_wakeup(bus, false);
  51. }
  52. } else {
  53. WARN_ON(!bus->i915_power_refcount);
  54. if (!--bus->i915_power_refcount)
  55. acomp->ops->put_power(acomp->dev);
  56. }
  57. return 0;
  58. }
  59. EXPORT_SYMBOL_GPL(snd_hdac_display_power);
  60. int snd_hdac_get_display_clk(struct hdac_bus *bus)
  61. {
  62. struct i915_audio_component *acomp = bus->audio_component;
  63. if (!acomp || !acomp->ops)
  64. return -ENODEV;
  65. return acomp->ops->get_cdclk_freq(acomp->dev);
  66. }
  67. EXPORT_SYMBOL_GPL(snd_hdac_get_display_clk);
  68. static int hdac_component_master_bind(struct device *dev)
  69. {
  70. struct i915_audio_component *acomp = hdac_acomp;
  71. int ret;
  72. ret = component_bind_all(dev, acomp);
  73. if (ret < 0)
  74. return ret;
  75. if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power &&
  76. acomp->ops->put_power && acomp->ops->get_cdclk_freq))) {
  77. ret = -EINVAL;
  78. goto out_unbind;
  79. }
  80. /*
  81. * Atm, we don't support dynamic unbinding initiated by the child
  82. * component, so pin its containing module until we unbind.
  83. */
  84. if (!try_module_get(acomp->ops->owner)) {
  85. ret = -ENODEV;
  86. goto out_unbind;
  87. }
  88. return 0;
  89. out_unbind:
  90. component_unbind_all(dev, acomp);
  91. return ret;
  92. }
  93. static void hdac_component_master_unbind(struct device *dev)
  94. {
  95. struct i915_audio_component *acomp = hdac_acomp;
  96. module_put(acomp->ops->owner);
  97. component_unbind_all(dev, acomp);
  98. WARN_ON(acomp->ops || acomp->dev);
  99. }
  100. static const struct component_master_ops hdac_component_master_ops = {
  101. .bind = hdac_component_master_bind,
  102. .unbind = hdac_component_master_unbind,
  103. };
  104. static int hdac_component_master_match(struct device *dev, void *data)
  105. {
  106. /* i915 is the only supported component */
  107. return !strcmp(dev->driver->name, "i915");
  108. }
  109. int snd_hdac_i915_init(struct hdac_bus *bus)
  110. {
  111. struct component_match *match = NULL;
  112. struct device *dev = bus->dev;
  113. struct i915_audio_component *acomp;
  114. int ret;
  115. acomp = kzalloc(sizeof(*acomp), GFP_KERNEL);
  116. if (!acomp)
  117. return -ENOMEM;
  118. bus->audio_component = acomp;
  119. hdac_acomp = acomp;
  120. component_match_add(dev, &match, hdac_component_master_match, bus);
  121. ret = component_master_add_with_match(dev, &hdac_component_master_ops,
  122. match);
  123. if (ret < 0)
  124. goto out_err;
  125. /*
  126. * Atm, we don't support deferring the component binding, so make sure
  127. * i915 is loaded and that the binding successfully completes.
  128. */
  129. request_module("i915");
  130. if (!acomp->ops) {
  131. ret = -ENODEV;
  132. goto out_master_del;
  133. }
  134. dev_dbg(dev, "bound to i915 component master\n");
  135. return 0;
  136. out_master_del:
  137. component_master_del(dev, &hdac_component_master_ops);
  138. out_err:
  139. kfree(acomp);
  140. bus->audio_component = NULL;
  141. dev_err(dev, "failed to add i915 component master (%d)\n", ret);
  142. return ret;
  143. }
  144. EXPORT_SYMBOL_GPL(snd_hdac_i915_init);
  145. int snd_hdac_i915_exit(struct hdac_bus *bus)
  146. {
  147. struct device *dev = bus->dev;
  148. struct i915_audio_component *acomp = bus->audio_component;
  149. if (!acomp)
  150. return 0;
  151. WARN_ON(bus->i915_power_refcount);
  152. if (bus->i915_power_refcount > 0 && acomp->ops)
  153. acomp->ops->put_power(acomp->dev);
  154. component_master_del(dev, &hdac_component_master_ops);
  155. kfree(acomp);
  156. bus->audio_component = NULL;
  157. return 0;
  158. }
  159. EXPORT_SYMBOL_GPL(snd_hdac_i915_exit);