EDAC: i7core, sb_edac: Don't return NOTIFY_BAD from mce_decoder callback
[linux/fpc-iii.git] / sound / hda / hdac_i915.c
blob54babe1c0b1679b97ae43c3b745f0843b15ccfde
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 /* There is a fixed mapping between audio pin node and display port
122 * on current Intel platforms:
123 * Pin Widget 5 - PORT B (port = 1 in i915 driver)
124 * Pin Widget 6 - PORT C (port = 2 in i915 driver)
125 * Pin Widget 7 - PORT D (port = 3 in i915 driver)
127 static int pin2port(hda_nid_t pin_nid)
129 if (WARN_ON(pin_nid < 5 || pin_nid > 7))
130 return -1;
131 return pin_nid - 4;
135 * snd_hdac_sync_audio_rate - Set N/CTS based on the sample rate
136 * @bus: HDA core bus
137 * @nid: the pin widget NID
138 * @rate: the sample rate to set
140 * This function is supposed to be used only by a HD-audio controller
141 * driver that needs the interaction with i915 graphics.
143 * This function sets N/CTS value based on the given sample rate.
144 * Returns zero for success, or a negative error code.
146 int snd_hdac_sync_audio_rate(struct hdac_bus *bus, hda_nid_t nid, int rate)
148 struct i915_audio_component *acomp = bus->audio_component;
149 int port;
151 if (!acomp || !acomp->ops || !acomp->ops->sync_audio_rate)
152 return -ENODEV;
153 port = pin2port(nid);
154 if (port < 0)
155 return -EINVAL;
156 return acomp->ops->sync_audio_rate(acomp->dev, port, rate);
158 EXPORT_SYMBOL_GPL(snd_hdac_sync_audio_rate);
161 * snd_hdac_acomp_get_eld - Get the audio state and ELD via component
162 * @bus: HDA core bus
163 * @nid: the pin widget NID
164 * @audio_enabled: the pointer to store the current audio state
165 * @buffer: the buffer pointer to store ELD bytes
166 * @max_bytes: the max bytes to be stored on @buffer
168 * This function is supposed to be used only by a HD-audio controller
169 * driver that needs the interaction with i915 graphics.
171 * This function queries the current state of the audio on the given
172 * digital port and fetches the ELD bytes onto the given buffer.
173 * It returns the number of bytes for the total ELD data, zero for
174 * invalid ELD, or a negative error code.
176 * The return size is the total bytes required for the whole ELD bytes,
177 * thus it may be over @max_bytes. If it's over @max_bytes, it implies
178 * that only a part of ELD bytes have been fetched.
180 int snd_hdac_acomp_get_eld(struct hdac_bus *bus, hda_nid_t nid,
181 bool *audio_enabled, char *buffer, int max_bytes)
183 struct i915_audio_component *acomp = bus->audio_component;
184 int port;
186 if (!acomp || !acomp->ops || !acomp->ops->get_eld)
187 return -ENODEV;
189 port = pin2port(nid);
190 if (port < 0)
191 return -EINVAL;
192 return acomp->ops->get_eld(acomp->dev, port, audio_enabled,
193 buffer, max_bytes);
195 EXPORT_SYMBOL_GPL(snd_hdac_acomp_get_eld);
197 static int hdac_component_master_bind(struct device *dev)
199 struct i915_audio_component *acomp = hdac_acomp;
200 int ret;
202 ret = component_bind_all(dev, acomp);
203 if (ret < 0)
204 return ret;
206 if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power &&
207 acomp->ops->put_power && acomp->ops->get_cdclk_freq))) {
208 ret = -EINVAL;
209 goto out_unbind;
213 * Atm, we don't support dynamic unbinding initiated by the child
214 * component, so pin its containing module until we unbind.
216 if (!try_module_get(acomp->ops->owner)) {
217 ret = -ENODEV;
218 goto out_unbind;
221 return 0;
223 out_unbind:
224 component_unbind_all(dev, acomp);
226 return ret;
229 static void hdac_component_master_unbind(struct device *dev)
231 struct i915_audio_component *acomp = hdac_acomp;
233 module_put(acomp->ops->owner);
234 component_unbind_all(dev, acomp);
235 WARN_ON(acomp->ops || acomp->dev);
238 static const struct component_master_ops hdac_component_master_ops = {
239 .bind = hdac_component_master_bind,
240 .unbind = hdac_component_master_unbind,
243 static int hdac_component_master_match(struct device *dev, void *data)
245 /* i915 is the only supported component */
246 return !strcmp(dev->driver->name, "i915");
250 * snd_hdac_i915_register_notifier - Register i915 audio component ops
251 * @aops: i915 audio component ops
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 sets the given ops to be called by the i915 graphics driver.
258 * Returns zero for success or a negative error code.
260 int snd_hdac_i915_register_notifier(const struct i915_audio_component_audio_ops *aops)
262 if (WARN_ON(!hdac_acomp))
263 return -ENODEV;
265 hdac_acomp->audio_ops = aops;
266 return 0;
268 EXPORT_SYMBOL_GPL(snd_hdac_i915_register_notifier);
270 /* check whether intel graphics is present */
271 static bool i915_gfx_present(void)
273 static struct pci_device_id ids[] = {
274 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_ANY_ID),
275 .class = PCI_BASE_CLASS_DISPLAY << 16,
276 .class_mask = 0xff << 16 },
279 return pci_dev_present(ids);
283 * snd_hdac_i915_init - Initialize i915 audio component
284 * @bus: HDA core bus
286 * This function is supposed to be used only by a HD-audio controller
287 * driver that needs the interaction with i915 graphics.
289 * This function initializes and sets up the audio component to communicate
290 * with i915 graphics driver.
292 * Returns zero for success or a negative error code.
294 int snd_hdac_i915_init(struct hdac_bus *bus)
296 struct component_match *match = NULL;
297 struct device *dev = bus->dev;
298 struct i915_audio_component *acomp;
299 int ret;
301 if (!i915_gfx_present())
302 return -ENODEV;
304 acomp = kzalloc(sizeof(*acomp), GFP_KERNEL);
305 if (!acomp)
306 return -ENOMEM;
307 bus->audio_component = acomp;
308 hdac_acomp = acomp;
310 component_match_add(dev, &match, hdac_component_master_match, bus);
311 ret = component_master_add_with_match(dev, &hdac_component_master_ops,
312 match);
313 if (ret < 0)
314 goto out_err;
317 * Atm, we don't support deferring the component binding, so make sure
318 * i915 is loaded and that the binding successfully completes.
320 request_module("i915");
322 if (!acomp->ops) {
323 ret = -ENODEV;
324 goto out_master_del;
326 dev_dbg(dev, "bound to i915 component master\n");
328 return 0;
329 out_master_del:
330 component_master_del(dev, &hdac_component_master_ops);
331 out_err:
332 kfree(acomp);
333 bus->audio_component = NULL;
334 dev_info(dev, "failed to add i915 component master (%d)\n", ret);
336 return ret;
338 EXPORT_SYMBOL_GPL(snd_hdac_i915_init);
341 * snd_hdac_i915_exit - Finalize i915 audio component
342 * @bus: HDA core bus
344 * This function is supposed to be used only by a HD-audio controller
345 * driver that needs the interaction with i915 graphics.
347 * This function releases the i915 audio component that has been used.
349 * Returns zero for success or a negative error code.
351 int snd_hdac_i915_exit(struct hdac_bus *bus)
353 struct device *dev = bus->dev;
354 struct i915_audio_component *acomp = bus->audio_component;
356 if (!acomp)
357 return 0;
359 WARN_ON(bus->i915_power_refcount);
360 if (bus->i915_power_refcount > 0 && acomp->ops)
361 acomp->ops->put_power(acomp->dev);
363 component_master_del(dev, &hdac_component_master_ops);
365 kfree(acomp);
366 bus->audio_component = NULL;
368 return 0;
370 EXPORT_SYMBOL_GPL(snd_hdac_i915_exit);