bcma: claim only 14e4:4365 PCI Dell card with SoftMAC BCM43142
[linux/fpc-iii.git] / sound / hda / hdac_i915.c
blob8fef1b8d1fd8acc443bc86a2aedd66763d9b84fd
1 /*
2 * hdac_i915.c - routines for sync between HD-A core and i915 display driver
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.
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.
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/pci.h>
18 #include <linux/component.h>
19 #include <drm/i915_component.h>
20 #include <sound/core.h>
21 #include <sound/hdaudio.h>
22 #include <sound/hda_i915.h>
24 static struct i915_audio_component *hdac_acomp;
26 /**
27 * snd_hdac_set_codec_wakeup - Enable / disable HDMI/DP codec wakeup
28 * @bus: HDA core bus
29 * @enable: enable or disable the wakeup
31 * This function is supposed to be used only by a HD-audio controller
32 * driver that needs the interaction with i915 graphics.
34 * This function should be called during the chip reset, also called at
35 * resume for updating STATESTS register read.
37 * Returns zero for success or a negative error code.
39 int snd_hdac_set_codec_wakeup(struct hdac_bus *bus, bool enable)
41 struct i915_audio_component *acomp = bus->audio_component;
43 if (!acomp || !acomp->ops)
44 return -ENODEV;
46 if (!acomp->ops->codec_wake_override) {
47 dev_warn(bus->dev,
48 "Invalid codec wake callback\n");
49 return 0;
52 dev_dbg(bus->dev, "%s codec wakeup\n",
53 enable ? "enable" : "disable");
55 acomp->ops->codec_wake_override(acomp->dev, enable);
57 return 0;
59 EXPORT_SYMBOL_GPL(snd_hdac_set_codec_wakeup);
61 /**
62 * snd_hdac_display_power - Power up / down the power refcount
63 * @bus: HDA core bus
64 * @enable: power up or down
66 * This function is supposed to be used only by a HD-audio controller
67 * driver that needs the interaction with i915 graphics.
69 * This function manages a refcount and calls the i915 get_power() and
70 * put_power() ops accordingly, toggling the codec wakeup, too.
72 * Returns zero for success or a negative error code.
74 int snd_hdac_display_power(struct hdac_bus *bus, bool enable)
76 struct i915_audio_component *acomp = bus->audio_component;
78 if (!acomp || !acomp->ops)
79 return -ENODEV;
81 dev_dbg(bus->dev, "display power %s\n",
82 enable ? "enable" : "disable");
84 if (enable) {
85 if (!bus->i915_power_refcount++) {
86 acomp->ops->get_power(acomp->dev);
87 snd_hdac_set_codec_wakeup(bus, true);
88 snd_hdac_set_codec_wakeup(bus, false);
90 } else {
91 WARN_ON(!bus->i915_power_refcount);
92 if (!--bus->i915_power_refcount)
93 acomp->ops->put_power(acomp->dev);
96 return 0;
98 EXPORT_SYMBOL_GPL(snd_hdac_display_power);
101 * snd_hdac_get_display_clk - Get CDCLK in kHz
102 * @bus: HDA core bus
104 * This function is supposed to be used only by a HD-audio controller
105 * driver that needs the interaction with i915 graphics.
107 * This function queries CDCLK value in kHz from the graphics driver and
108 * returns the value. A negative code is returned in error.
110 int snd_hdac_get_display_clk(struct hdac_bus *bus)
112 struct i915_audio_component *acomp = bus->audio_component;
114 if (!acomp || !acomp->ops)
115 return -ENODEV;
117 return acomp->ops->get_cdclk_freq(acomp->dev);
119 EXPORT_SYMBOL_GPL(snd_hdac_get_display_clk);
121 static int hdac_component_master_bind(struct device *dev)
123 struct i915_audio_component *acomp = hdac_acomp;
124 int ret;
126 ret = component_bind_all(dev, acomp);
127 if (ret < 0)
128 return ret;
130 if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power &&
131 acomp->ops->put_power && acomp->ops->get_cdclk_freq))) {
132 ret = -EINVAL;
133 goto out_unbind;
137 * Atm, we don't support dynamic unbinding initiated by the child
138 * component, so pin its containing module until we unbind.
140 if (!try_module_get(acomp->ops->owner)) {
141 ret = -ENODEV;
142 goto out_unbind;
145 return 0;
147 out_unbind:
148 component_unbind_all(dev, acomp);
150 return ret;
153 static void hdac_component_master_unbind(struct device *dev)
155 struct i915_audio_component *acomp = hdac_acomp;
157 module_put(acomp->ops->owner);
158 component_unbind_all(dev, acomp);
159 WARN_ON(acomp->ops || acomp->dev);
162 static const struct component_master_ops hdac_component_master_ops = {
163 .bind = hdac_component_master_bind,
164 .unbind = hdac_component_master_unbind,
167 static int hdac_component_master_match(struct device *dev, void *data)
169 /* i915 is the only supported component */
170 return !strcmp(dev->driver->name, "i915");
174 * snd_hdac_i915_register_notifier - Register i915 audio component ops
175 * @aops: i915 audio component ops
177 * This function is supposed to be used only by a HD-audio controller
178 * driver that needs the interaction with i915 graphics.
180 * This function sets the given ops to be called by the i915 graphics driver.
182 * Returns zero for success or a negative error code.
184 int snd_hdac_i915_register_notifier(const struct i915_audio_component_audio_ops *aops)
186 if (WARN_ON(!hdac_acomp))
187 return -ENODEV;
189 hdac_acomp->audio_ops = aops;
190 return 0;
192 EXPORT_SYMBOL_GPL(snd_hdac_i915_register_notifier);
195 * snd_hdac_i915_init - Initialize i915 audio component
196 * @bus: HDA core bus
198 * This function is supposed to be used only by a HD-audio controller
199 * driver that needs the interaction with i915 graphics.
201 * This function initializes and sets up the audio component to communicate
202 * with i915 graphics driver.
204 * Returns zero for success or a negative error code.
206 int snd_hdac_i915_init(struct hdac_bus *bus)
208 struct component_match *match = NULL;
209 struct device *dev = bus->dev;
210 struct i915_audio_component *acomp;
211 int ret;
213 acomp = kzalloc(sizeof(*acomp), GFP_KERNEL);
214 if (!acomp)
215 return -ENOMEM;
216 bus->audio_component = acomp;
217 hdac_acomp = acomp;
219 component_match_add(dev, &match, hdac_component_master_match, bus);
220 ret = component_master_add_with_match(dev, &hdac_component_master_ops,
221 match);
222 if (ret < 0)
223 goto out_err;
226 * Atm, we don't support deferring the component binding, so make sure
227 * i915 is loaded and that the binding successfully completes.
229 request_module("i915");
231 if (!acomp->ops) {
232 ret = -ENODEV;
233 goto out_master_del;
235 dev_dbg(dev, "bound to i915 component master\n");
237 return 0;
238 out_master_del:
239 component_master_del(dev, &hdac_component_master_ops);
240 out_err:
241 kfree(acomp);
242 bus->audio_component = NULL;
243 dev_err(dev, "failed to add i915 component master (%d)\n", ret);
245 return ret;
247 EXPORT_SYMBOL_GPL(snd_hdac_i915_init);
250 * snd_hdac_i915_exit - Finalize i915 audio component
251 * @bus: HDA core bus
253 * This function is supposed to be used only by a HD-audio controller
254 * driver that needs the interaction with i915 graphics.
256 * This function releases the i915 audio component that has been used.
258 * Returns zero for success or a negative error code.
260 int snd_hdac_i915_exit(struct hdac_bus *bus)
262 struct device *dev = bus->dev;
263 struct i915_audio_component *acomp = bus->audio_component;
265 if (!acomp)
266 return 0;
268 WARN_ON(bus->i915_power_refcount);
269 if (bus->i915_power_refcount > 0 && acomp->ops)
270 acomp->ops->put_power(acomp->dev);
272 component_master_del(dev, &hdac_component_master_ops);
274 kfree(acomp);
275 bus->audio_component = NULL;
277 return 0;
279 EXPORT_SYMBOL_GPL(snd_hdac_i915_exit);