ae0f305a7e4191675675dddc3885d1bdfdab27ca
[deliverable/linux.git] / sound / hda / hdac_i915.c
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
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>
23
24 static struct i915_audio_component *hdac_acomp;
25
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
30 *
31 * This function is supposed to be used only by a HD-audio controller
32 * driver that needs the interaction with i915 graphics.
33 *
34 * This function should be called during the chip reset, also called at
35 * resume for updating STATESTS register read.
36 *
37 * Returns zero for success or a negative error code.
38 */
39 int snd_hdac_set_codec_wakeup(struct hdac_bus *bus, bool enable)
40 {
41 struct i915_audio_component *acomp = bus->audio_component;
42
43 if (!acomp || !acomp->ops)
44 return -ENODEV;
45
46 if (!acomp->ops->codec_wake_override) {
47 dev_warn(bus->dev,
48 "Invalid codec wake callback\n");
49 return 0;
50 }
51
52 dev_dbg(bus->dev, "%s codec wakeup\n",
53 enable ? "enable" : "disable");
54
55 acomp->ops->codec_wake_override(acomp->dev, enable);
56
57 return 0;
58 }
59 EXPORT_SYMBOL_GPL(snd_hdac_set_codec_wakeup);
60
61 /**
62 * snd_hdac_display_power - Power up / down the power refcount
63 * @bus: HDA core bus
64 * @enable: power up or down
65 *
66 * This function is supposed to be used only by a HD-audio controller
67 * driver that needs the interaction with i915 graphics.
68 *
69 * This function manages a refcount and calls the i915 get_power() and
70 * put_power() ops accordingly, toggling the codec wakeup, too.
71 *
72 * Returns zero for success or a negative error code.
73 */
74 int snd_hdac_display_power(struct hdac_bus *bus, bool enable)
75 {
76 struct i915_audio_component *acomp = bus->audio_component;
77
78 if (!acomp || !acomp->ops)
79 return -ENODEV;
80
81 dev_dbg(bus->dev, "display power %s\n",
82 enable ? "enable" : "disable");
83
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);
89 }
90 } else {
91 WARN_ON(!bus->i915_power_refcount);
92 if (!--bus->i915_power_refcount)
93 acomp->ops->put_power(acomp->dev);
94 }
95
96 return 0;
97 }
98 EXPORT_SYMBOL_GPL(snd_hdac_display_power);
99
100 /**
101 * snd_hdac_get_display_clk - Get CDCLK in kHz
102 * @bus: HDA core bus
103 *
104 * This function is supposed to be used only by a HD-audio controller
105 * driver that needs the interaction with i915 graphics.
106 *
107 * This function queries CDCLK value in kHz from the graphics driver and
108 * returns the value. A negative code is returned in error.
109 */
110 int snd_hdac_get_display_clk(struct hdac_bus *bus)
111 {
112 struct i915_audio_component *acomp = bus->audio_component;
113
114 if (!acomp || !acomp->ops)
115 return -ENODEV;
116
117 return acomp->ops->get_cdclk_freq(acomp->dev);
118 }
119 EXPORT_SYMBOL_GPL(snd_hdac_get_display_clk);
120
121 /* There is a fixed mapping between audio pin node and display port.
122 * on SNB, IVY, HSW, BSW, SKL, BXT, KBL:
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)
126 *
127 * on VLV, ILK:
128 * Pin Widget 4 - PORT B (port = 1 in i915 driver)
129 * Pin Widget 5 - PORT C (port = 2 in i915 driver)
130 * Pin Widget 6 - PORT D (port = 3 in i915 driver)
131 */
132 static int pin2port(struct hdac_device *codec, hda_nid_t pin_nid)
133 {
134 int base_nid;
135
136 switch (codec->vendor_id) {
137 case 0x80860054: /* ILK */
138 case 0x80862804: /* ILK */
139 case 0x80862882: /* VLV */
140 base_nid = 3;
141 break;
142 default:
143 base_nid = 4;
144 break;
145 }
146
147 if (WARN_ON(pin_nid <= base_nid || pin_nid > base_nid + 3))
148 return -1;
149 return pin_nid - base_nid;
150 }
151
152 /**
153 * snd_hdac_sync_audio_rate - Set N/CTS based on the sample rate
154 * @codec: HDA codec
155 * @nid: the pin widget NID
156 * @rate: the sample rate to set
157 *
158 * This function is supposed to be used only by a HD-audio controller
159 * driver that needs the interaction with i915 graphics.
160 *
161 * This function sets N/CTS value based on the given sample rate.
162 * Returns zero for success, or a negative error code.
163 */
164 int snd_hdac_sync_audio_rate(struct hdac_device *codec, hda_nid_t nid, int rate)
165 {
166 struct hdac_bus *bus = codec->bus;
167 struct i915_audio_component *acomp = bus->audio_component;
168 int port;
169
170 if (!acomp || !acomp->ops || !acomp->ops->sync_audio_rate)
171 return -ENODEV;
172 port = pin2port(codec, nid);
173 if (port < 0)
174 return -EINVAL;
175 return acomp->ops->sync_audio_rate(acomp->dev, port, rate);
176 }
177 EXPORT_SYMBOL_GPL(snd_hdac_sync_audio_rate);
178
179 /**
180 * snd_hdac_acomp_get_eld - Get the audio state and ELD via component
181 * @codec: HDA codec
182 * @nid: the pin widget NID
183 * @audio_enabled: the pointer to store the current audio state
184 * @buffer: the buffer pointer to store ELD bytes
185 * @max_bytes: the max bytes to be stored on @buffer
186 *
187 * This function is supposed to be used only by a HD-audio controller
188 * driver that needs the interaction with i915 graphics.
189 *
190 * This function queries the current state of the audio on the given
191 * digital port and fetches the ELD bytes onto the given buffer.
192 * It returns the number of bytes for the total ELD data, zero for
193 * invalid ELD, or a negative error code.
194 *
195 * The return size is the total bytes required for the whole ELD bytes,
196 * thus it may be over @max_bytes. If it's over @max_bytes, it implies
197 * that only a part of ELD bytes have been fetched.
198 */
199 int snd_hdac_acomp_get_eld(struct hdac_device *codec, hda_nid_t nid,
200 bool *audio_enabled, char *buffer, int max_bytes)
201 {
202 struct hdac_bus *bus = codec->bus;
203 struct i915_audio_component *acomp = bus->audio_component;
204 int port;
205
206 if (!acomp || !acomp->ops || !acomp->ops->get_eld)
207 return -ENODEV;
208
209 port = pin2port(codec, nid);
210 if (port < 0)
211 return -EINVAL;
212 return acomp->ops->get_eld(acomp->dev, port, audio_enabled,
213 buffer, max_bytes);
214 }
215 EXPORT_SYMBOL_GPL(snd_hdac_acomp_get_eld);
216
217 static int hdac_component_master_bind(struct device *dev)
218 {
219 struct i915_audio_component *acomp = hdac_acomp;
220 int ret;
221
222 ret = component_bind_all(dev, acomp);
223 if (ret < 0)
224 return ret;
225
226 if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power &&
227 acomp->ops->put_power && acomp->ops->get_cdclk_freq))) {
228 ret = -EINVAL;
229 goto out_unbind;
230 }
231
232 /*
233 * Atm, we don't support dynamic unbinding initiated by the child
234 * component, so pin its containing module until we unbind.
235 */
236 if (!try_module_get(acomp->ops->owner)) {
237 ret = -ENODEV;
238 goto out_unbind;
239 }
240
241 return 0;
242
243 out_unbind:
244 component_unbind_all(dev, acomp);
245
246 return ret;
247 }
248
249 static void hdac_component_master_unbind(struct device *dev)
250 {
251 struct i915_audio_component *acomp = hdac_acomp;
252
253 module_put(acomp->ops->owner);
254 component_unbind_all(dev, acomp);
255 WARN_ON(acomp->ops || acomp->dev);
256 }
257
258 static const struct component_master_ops hdac_component_master_ops = {
259 .bind = hdac_component_master_bind,
260 .unbind = hdac_component_master_unbind,
261 };
262
263 static int hdac_component_master_match(struct device *dev, void *data)
264 {
265 /* i915 is the only supported component */
266 return !strcmp(dev->driver->name, "i915");
267 }
268
269 /**
270 * snd_hdac_i915_register_notifier - Register i915 audio component ops
271 * @aops: i915 audio component ops
272 *
273 * This function is supposed to be used only by a HD-audio controller
274 * driver that needs the interaction with i915 graphics.
275 *
276 * This function sets the given ops to be called by the i915 graphics driver.
277 *
278 * Returns zero for success or a negative error code.
279 */
280 int snd_hdac_i915_register_notifier(const struct i915_audio_component_audio_ops *aops)
281 {
282 if (WARN_ON(!hdac_acomp))
283 return -ENODEV;
284
285 hdac_acomp->audio_ops = aops;
286 return 0;
287 }
288 EXPORT_SYMBOL_GPL(snd_hdac_i915_register_notifier);
289
290 /**
291 * snd_hdac_i915_init - Initialize i915 audio component
292 * @bus: HDA core bus
293 *
294 * This function is supposed to be used only by a HD-audio controller
295 * driver that needs the interaction with i915 graphics.
296 *
297 * This function initializes and sets up the audio component to communicate
298 * with i915 graphics driver.
299 *
300 * Returns zero for success or a negative error code.
301 */
302 int snd_hdac_i915_init(struct hdac_bus *bus)
303 {
304 struct component_match *match = NULL;
305 struct device *dev = bus->dev;
306 struct i915_audio_component *acomp;
307 int ret;
308
309 if (WARN_ON(hdac_acomp))
310 return -EBUSY;
311
312 acomp = kzalloc(sizeof(*acomp), GFP_KERNEL);
313 if (!acomp)
314 return -ENOMEM;
315 bus->audio_component = acomp;
316 hdac_acomp = acomp;
317
318 component_match_add(dev, &match, hdac_component_master_match, bus);
319 ret = component_master_add_with_match(dev, &hdac_component_master_ops,
320 match);
321 if (ret < 0)
322 goto out_err;
323
324 /*
325 * Atm, we don't support deferring the component binding, so make sure
326 * i915 is loaded and that the binding successfully completes.
327 */
328 request_module("i915");
329
330 if (!acomp->ops) {
331 ret = -ENODEV;
332 goto out_master_del;
333 }
334 dev_dbg(dev, "bound to i915 component master\n");
335
336 return 0;
337 out_master_del:
338 component_master_del(dev, &hdac_component_master_ops);
339 out_err:
340 kfree(acomp);
341 bus->audio_component = NULL;
342 dev_info(dev, "failed to add i915 component master (%d)\n", ret);
343
344 return ret;
345 }
346 EXPORT_SYMBOL_GPL(snd_hdac_i915_init);
347
348 /**
349 * snd_hdac_i915_exit - Finalize i915 audio component
350 * @bus: HDA core bus
351 *
352 * This function is supposed to be used only by a HD-audio controller
353 * driver that needs the interaction with i915 graphics.
354 *
355 * This function releases the i915 audio component that has been used.
356 *
357 * Returns zero for success or a negative error code.
358 */
359 int snd_hdac_i915_exit(struct hdac_bus *bus)
360 {
361 struct device *dev = bus->dev;
362 struct i915_audio_component *acomp = bus->audio_component;
363
364 if (!acomp)
365 return 0;
366
367 WARN_ON(bus->i915_power_refcount);
368 if (bus->i915_power_refcount > 0 && acomp->ops)
369 acomp->ops->put_power(acomp->dev);
370
371 component_master_del(dev, &hdac_component_master_ops);
372
373 kfree(acomp);
374 bus->audio_component = NULL;
375 hdac_acomp = NULL;
376
377 return 0;
378 }
379 EXPORT_SYMBOL_GPL(snd_hdac_i915_exit);
This page took 0.037474 seconds and 4 git commands to generate.