1
2
3
4
5
6
7
8
9
10
11
12
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#include <sound/hda_register.h>
24
25static struct i915_audio_component *hdac_acomp;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40int snd_hdac_set_codec_wakeup(struct hdac_bus *bus, bool enable)
41{
42 struct i915_audio_component *acomp = bus->audio_component;
43
44 if (!acomp || !acomp->ops)
45 return -ENODEV;
46
47 if (!acomp->ops->codec_wake_override) {
48 dev_warn(bus->dev,
49 "Invalid codec wake callback\n");
50 return 0;
51 }
52
53 dev_dbg(bus->dev, "%s codec wakeup\n",
54 enable ? "enable" : "disable");
55
56 acomp->ops->codec_wake_override(acomp->dev, enable);
57
58 return 0;
59}
60EXPORT_SYMBOL_GPL(snd_hdac_set_codec_wakeup);
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75int snd_hdac_display_power(struct hdac_bus *bus, bool enable)
76{
77 struct i915_audio_component *acomp = bus->audio_component;
78
79 if (!acomp || !acomp->ops)
80 return -ENODEV;
81
82 dev_dbg(bus->dev, "display power %s\n",
83 enable ? "enable" : "disable");
84
85 if (enable) {
86 if (!bus->i915_power_refcount++) {
87 acomp->ops->get_power(acomp->dev);
88 snd_hdac_set_codec_wakeup(bus, true);
89 snd_hdac_set_codec_wakeup(bus, false);
90 }
91 } else {
92 WARN_ON(!bus->i915_power_refcount);
93 if (!--bus->i915_power_refcount)
94 acomp->ops->put_power(acomp->dev);
95 }
96
97 return 0;
98}
99EXPORT_SYMBOL_GPL(snd_hdac_display_power);
100
101#define CONTROLLER_IN_GPU(pci) (((pci)->device == 0x0a0c) || \
102 ((pci)->device == 0x0c0c) || \
103 ((pci)->device == 0x0d0c) || \
104 ((pci)->device == 0x160c))
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120void snd_hdac_i915_set_bclk(struct hdac_bus *bus)
121{
122 struct i915_audio_component *acomp = bus->audio_component;
123 struct pci_dev *pci = to_pci_dev(bus->dev);
124 int cdclk_freq;
125 unsigned int bclk_m, bclk_n;
126
127 if (!acomp || !acomp->ops || !acomp->ops->get_cdclk_freq)
128 return;
129 if (!CONTROLLER_IN_GPU(pci))
130 return;
131
132 cdclk_freq = acomp->ops->get_cdclk_freq(acomp->dev);
133 switch (cdclk_freq) {
134 case 337500:
135 bclk_m = 16;
136 bclk_n = 225;
137 break;
138
139 case 450000:
140 default:
141 bclk_m = 4;
142 bclk_n = 75;
143 break;
144
145 case 540000:
146 bclk_m = 4;
147 bclk_n = 90;
148 break;
149
150 case 675000:
151 bclk_m = 8;
152 bclk_n = 225;
153 break;
154 }
155
156 snd_hdac_chip_writew(bus, HSW_EM4, bclk_m);
157 snd_hdac_chip_writew(bus, HSW_EM5, bclk_n);
158}
159EXPORT_SYMBOL_GPL(snd_hdac_i915_set_bclk);
160
161
162
163
164
165
166
167
168
169
170
171
172static int pin2port(struct hdac_device *codec, hda_nid_t pin_nid)
173{
174 int base_nid;
175
176 switch (codec->vendor_id) {
177 case 0x80860054:
178 case 0x80862804:
179 case 0x80862882:
180 base_nid = 3;
181 break;
182 default:
183 base_nid = 4;
184 break;
185 }
186
187 if (WARN_ON(pin_nid <= base_nid || pin_nid > base_nid + 3))
188 return -1;
189 return pin_nid - base_nid;
190}
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205int snd_hdac_sync_audio_rate(struct hdac_device *codec, hda_nid_t nid,
206 int dev_id, int rate)
207{
208 struct hdac_bus *bus = codec->bus;
209 struct i915_audio_component *acomp = bus->audio_component;
210 int port, pipe;
211
212 if (!acomp || !acomp->ops || !acomp->ops->sync_audio_rate)
213 return -ENODEV;
214 port = pin2port(codec, nid);
215 if (port < 0)
216 return -EINVAL;
217 pipe = dev_id;
218 return acomp->ops->sync_audio_rate(acomp->dev, port, pipe, rate);
219}
220EXPORT_SYMBOL_GPL(snd_hdac_sync_audio_rate);
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243int snd_hdac_acomp_get_eld(struct hdac_device *codec, hda_nid_t nid, int dev_id,
244 bool *audio_enabled, char *buffer, int max_bytes)
245{
246 struct hdac_bus *bus = codec->bus;
247 struct i915_audio_component *acomp = bus->audio_component;
248 int port, pipe;
249
250 if (!acomp || !acomp->ops || !acomp->ops->get_eld)
251 return -ENODEV;
252
253 port = pin2port(codec, nid);
254 if (port < 0)
255 return -EINVAL;
256
257 pipe = dev_id;
258 return acomp->ops->get_eld(acomp->dev, port, pipe, audio_enabled,
259 buffer, max_bytes);
260}
261EXPORT_SYMBOL_GPL(snd_hdac_acomp_get_eld);
262
263static int hdac_component_master_bind(struct device *dev)
264{
265 struct i915_audio_component *acomp = hdac_acomp;
266 int ret;
267
268 ret = component_bind_all(dev, acomp);
269 if (ret < 0)
270 return ret;
271
272 if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power &&
273 acomp->ops->put_power && acomp->ops->get_cdclk_freq))) {
274 ret = -EINVAL;
275 goto out_unbind;
276 }
277
278
279
280
281
282 if (!try_module_get(acomp->ops->owner)) {
283 ret = -ENODEV;
284 goto out_unbind;
285 }
286
287 return 0;
288
289out_unbind:
290 component_unbind_all(dev, acomp);
291
292 return ret;
293}
294
295static void hdac_component_master_unbind(struct device *dev)
296{
297 struct i915_audio_component *acomp = hdac_acomp;
298
299 module_put(acomp->ops->owner);
300 component_unbind_all(dev, acomp);
301 WARN_ON(acomp->ops || acomp->dev);
302}
303
304static const struct component_master_ops hdac_component_master_ops = {
305 .bind = hdac_component_master_bind,
306 .unbind = hdac_component_master_unbind,
307};
308
309static int hdac_component_master_match(struct device *dev, void *data)
310{
311
312 return !strcmp(dev->driver->name, "i915");
313}
314
315
316
317
318
319
320
321
322
323
324
325
326int snd_hdac_i915_register_notifier(const struct i915_audio_component_audio_ops *aops)
327{
328 if (WARN_ON(!hdac_acomp))
329 return -ENODEV;
330
331 hdac_acomp->audio_ops = aops;
332 return 0;
333}
334EXPORT_SYMBOL_GPL(snd_hdac_i915_register_notifier);
335
336
337static bool i915_gfx_present(void)
338{
339 static const struct pci_device_id ids[] = {
340 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_ANY_ID),
341 .class = PCI_BASE_CLASS_DISPLAY << 16,
342 .class_mask = 0xff << 16 },
343 {}
344 };
345 return pci_dev_present(ids);
346}
347
348
349
350
351
352
353
354
355
356
357
358
359
360int snd_hdac_i915_init(struct hdac_bus *bus)
361{
362 struct component_match *match = NULL;
363 struct device *dev = bus->dev;
364 struct i915_audio_component *acomp;
365 int ret;
366
367 if (WARN_ON(hdac_acomp))
368 return -EBUSY;
369
370 if (!i915_gfx_present())
371 return -ENODEV;
372
373 acomp = kzalloc(sizeof(*acomp), GFP_KERNEL);
374 if (!acomp)
375 return -ENOMEM;
376 bus->audio_component = acomp;
377 hdac_acomp = acomp;
378
379 component_match_add(dev, &match, hdac_component_master_match, bus);
380 ret = component_master_add_with_match(dev, &hdac_component_master_ops,
381 match);
382 if (ret < 0)
383 goto out_err;
384
385
386
387
388
389 request_module("i915");
390
391 if (!acomp->ops) {
392 ret = -ENODEV;
393 goto out_master_del;
394 }
395 dev_dbg(dev, "bound to i915 component master\n");
396
397 return 0;
398out_master_del:
399 component_master_del(dev, &hdac_component_master_ops);
400out_err:
401 kfree(acomp);
402 bus->audio_component = NULL;
403 hdac_acomp = NULL;
404 dev_info(dev, "failed to add i915 component master (%d)\n", ret);
405
406 return ret;
407}
408EXPORT_SYMBOL_GPL(snd_hdac_i915_init);
409
410
411
412
413
414
415
416
417
418
419
420
421int snd_hdac_i915_exit(struct hdac_bus *bus)
422{
423 struct device *dev = bus->dev;
424 struct i915_audio_component *acomp = bus->audio_component;
425
426 if (!acomp)
427 return 0;
428
429 WARN_ON(bus->i915_power_refcount);
430 if (bus->i915_power_refcount > 0 && acomp->ops)
431 acomp->ops->put_power(acomp->dev);
432
433 component_master_del(dev, &hdac_component_master_ops);
434
435 kfree(acomp);
436 bus->audio_component = NULL;
437 hdac_acomp = NULL;
438
439 return 0;
440}
441EXPORT_SYMBOL_GPL(snd_hdac_i915_exit);
442