1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 *
4 * hdmi.c - routines for HDMI/DisplayPort codecs
5 *
6 * Copyright(c) 2008-2010 Intel Corporation
7 * Copyright (c) 2006 ATI Technologies Inc.
8 * Copyright (c) 2008 NVIDIA Corp. All rights reserved.
9 * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
10 * Copyright (c) 2013 Anssi Hannula <anssi.hannula@iki.fi>
11 *
12 * Authors:
13 * Wu Fengguang <wfg@linux.intel.com>
14 *
15 * Maintained by:
16 * Wu Fengguang <wfg@linux.intel.com>
17 */
18
19 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/pci.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/pm_runtime.h>
25 #include <sound/core.h>
26 #include <sound/jack.h>
27 #include <sound/asoundef.h>
28 #include <sound/tlv.h>
29 #include <sound/hdaudio.h>
30 #include <sound/hda_i915.h>
31 #include <sound/hda_chmap.h>
32 #include <sound/hda_codec.h>
33 #include "hda_local.h"
34 #include "hda_jack.h"
35 #include "hda_controller.h"
36 #include "hdmi_local.h"
37
38 static bool static_hdmi_pcm;
39 module_param(static_hdmi_pcm, bool, 0644);
40 MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
41
42 static bool enable_acomp = true;
43 module_param(enable_acomp, bool, 0444);
44 MODULE_PARM_DESC(enable_acomp, "Enable audio component binding (default=yes)");
45
46 static bool enable_all_pins;
47 module_param(enable_all_pins, bool, 0444);
48 MODULE_PARM_DESC(enable_all_pins, "Forcibly enable all pins");
49
snd_hda_hdmi_pin_id_to_pin_index(struct hda_codec * codec,hda_nid_t pin_nid,int dev_id)50 int snd_hda_hdmi_pin_id_to_pin_index(struct hda_codec *codec,
51 hda_nid_t pin_nid, int dev_id)
52 {
53 struct hdmi_spec *spec = codec->spec;
54 int pin_idx;
55 struct hdmi_spec_per_pin *per_pin;
56
57 /*
58 * (dev_id == -1) means it is NON-MST pin
59 * return the first virtual pin on this port
60 */
61 if (dev_id == -1)
62 dev_id = 0;
63
64 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
65 per_pin = get_pin(spec, pin_idx);
66 if ((per_pin->pin_nid == pin_nid) &&
67 (per_pin->dev_id == dev_id))
68 return pin_idx;
69 }
70
71 codec_warn(codec, "HDMI: pin NID 0x%x not registered\n", pin_nid);
72 return -EINVAL;
73 }
74 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_pin_id_to_pin_index, "SND_HDA_CODEC_HDMI");
75
hinfo_to_pcm_index(struct hda_codec * codec,struct hda_pcm_stream * hinfo)76 static int hinfo_to_pcm_index(struct hda_codec *codec,
77 struct hda_pcm_stream *hinfo)
78 {
79 struct hdmi_spec *spec = codec->spec;
80 int pcm_idx;
81
82 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
83 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
84 return pcm_idx;
85
86 codec_warn(codec, "HDMI: hinfo %p not tied to a PCM\n", hinfo);
87 return -EINVAL;
88 }
89
hinfo_to_pin_index(struct hda_codec * codec,struct hda_pcm_stream * hinfo)90 static int hinfo_to_pin_index(struct hda_codec *codec,
91 struct hda_pcm_stream *hinfo)
92 {
93 struct hdmi_spec *spec = codec->spec;
94 struct hdmi_spec_per_pin *per_pin;
95 int pin_idx;
96
97 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
98 per_pin = get_pin(spec, pin_idx);
99 if (per_pin->pcm &&
100 per_pin->pcm->pcm->stream == hinfo)
101 return pin_idx;
102 }
103
104 codec_dbg(codec, "HDMI: hinfo %p (pcm %d) not registered\n", hinfo,
105 hinfo_to_pcm_index(codec, hinfo));
106 return -EINVAL;
107 }
108
pcm_idx_to_pin(struct hdmi_spec * spec,int pcm_idx)109 static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
110 int pcm_idx)
111 {
112 int i;
113 struct hdmi_spec_per_pin *per_pin;
114
115 for (i = 0; i < spec->num_pins; i++) {
116 per_pin = get_pin(spec, i);
117 if (per_pin->pcm_idx == pcm_idx)
118 return per_pin;
119 }
120 return NULL;
121 }
122
cvt_nid_to_cvt_index(struct hda_codec * codec,hda_nid_t cvt_nid)123 static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
124 {
125 struct hdmi_spec *spec = codec->spec;
126 int cvt_idx;
127
128 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
129 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
130 return cvt_idx;
131
132 codec_warn(codec, "HDMI: cvt NID 0x%x not registered\n", cvt_nid);
133 return -EINVAL;
134 }
135
hdmi_eld_ctl_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)136 static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
137 struct snd_ctl_elem_info *uinfo)
138 {
139 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
140 struct hdmi_spec *spec = codec->spec;
141 struct hdmi_spec_per_pin *per_pin;
142 struct hdmi_eld *eld;
143 int pcm_idx;
144
145 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
146
147 pcm_idx = kcontrol->private_value;
148 mutex_lock(&spec->pcm_lock);
149 per_pin = pcm_idx_to_pin(spec, pcm_idx);
150 if (!per_pin) {
151 /* no pin is bound to the pcm */
152 uinfo->count = 0;
153 goto unlock;
154 }
155 eld = &per_pin->sink_eld;
156 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
157
158 unlock:
159 mutex_unlock(&spec->pcm_lock);
160 return 0;
161 }
162
hdmi_eld_ctl_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)163 static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
164 struct snd_ctl_elem_value *ucontrol)
165 {
166 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
167 struct hdmi_spec *spec = codec->spec;
168 struct hdmi_spec_per_pin *per_pin;
169 struct hdmi_eld *eld;
170 int pcm_idx;
171 int err = 0;
172
173 pcm_idx = kcontrol->private_value;
174 mutex_lock(&spec->pcm_lock);
175 per_pin = pcm_idx_to_pin(spec, pcm_idx);
176 if (!per_pin) {
177 /* no pin is bound to the pcm */
178 memset(ucontrol->value.bytes.data, 0,
179 ARRAY_SIZE(ucontrol->value.bytes.data));
180 goto unlock;
181 }
182
183 eld = &per_pin->sink_eld;
184 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
185 eld->eld_size > ELD_MAX_SIZE) {
186 snd_BUG();
187 err = -EINVAL;
188 goto unlock;
189 }
190
191 memset(ucontrol->value.bytes.data, 0,
192 ARRAY_SIZE(ucontrol->value.bytes.data));
193 if (eld->eld_valid)
194 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
195 eld->eld_size);
196
197 unlock:
198 mutex_unlock(&spec->pcm_lock);
199 return err;
200 }
201
202 static const struct snd_kcontrol_new eld_bytes_ctl = {
203 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE |
204 SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK,
205 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
206 .name = "ELD",
207 .info = hdmi_eld_ctl_info,
208 .get = hdmi_eld_ctl_get,
209 };
210
hdmi_create_eld_ctl(struct hda_codec * codec,int pcm_idx,int device)211 static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
212 int device)
213 {
214 struct snd_kcontrol *kctl;
215 struct hdmi_spec *spec = codec->spec;
216 int err;
217
218 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
219 if (!kctl)
220 return -ENOMEM;
221 kctl->private_value = pcm_idx;
222 kctl->id.device = device;
223
224 /* no pin nid is associated with the kctl now
225 * tbd: associate pin nid to eld ctl later
226 */
227 err = snd_hda_ctl_add(codec, 0, kctl);
228 if (err < 0)
229 return err;
230
231 get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
232 return 0;
233 }
234
235 #ifdef BE_PARANOID
hdmi_get_dip_index(struct hda_codec * codec,hda_nid_t pin_nid,int * packet_index,int * byte_index)236 static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
237 int *packet_index, int *byte_index)
238 {
239 int val;
240
241 val = snd_hda_codec_read(codec, pin_nid, 0,
242 AC_VERB_GET_HDMI_DIP_INDEX, 0);
243
244 *packet_index = val >> 5;
245 *byte_index = val & 0x1f;
246 }
247 #endif
248
hdmi_set_dip_index(struct hda_codec * codec,hda_nid_t pin_nid,int packet_index,int byte_index)249 static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
250 int packet_index, int byte_index)
251 {
252 int val;
253
254 val = (packet_index << 5) | (byte_index & 0x1f);
255
256 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
257 }
258
hdmi_write_dip_byte(struct hda_codec * codec,hda_nid_t pin_nid,unsigned char val)259 static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
260 unsigned char val)
261 {
262 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
263 }
264
hdmi_init_pin(struct hda_codec * codec,hda_nid_t pin_nid)265 static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
266 {
267 struct hdmi_spec *spec = codec->spec;
268 int pin_out;
269
270 /* Unmute */
271 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
272 snd_hda_codec_write(codec, pin_nid, 0,
273 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
274
275 if (spec->dyn_pin_out)
276 /* Disable pin out until stream is active */
277 pin_out = 0;
278 else
279 /* Enable pin out: some machines with GM965 gets broken output
280 * when the pin is disabled or changed while using with HDMI
281 */
282 pin_out = PIN_OUT;
283
284 snd_hda_codec_write(codec, pin_nid, 0,
285 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
286 }
287
288 /*
289 * ELD proc files
290 */
291
292 #ifdef CONFIG_SND_PROC_FS
print_eld_info(struct snd_info_entry * entry,struct snd_info_buffer * buffer)293 static void print_eld_info(struct snd_info_entry *entry,
294 struct snd_info_buffer *buffer)
295 {
296 struct hdmi_spec_per_pin *per_pin = entry->private_data;
297
298 mutex_lock(&per_pin->lock);
299 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer, per_pin->pin_nid,
300 per_pin->dev_id, per_pin->cvt_nid);
301 mutex_unlock(&per_pin->lock);
302 }
303
write_eld_info(struct snd_info_entry * entry,struct snd_info_buffer * buffer)304 static void write_eld_info(struct snd_info_entry *entry,
305 struct snd_info_buffer *buffer)
306 {
307 struct hdmi_spec_per_pin *per_pin = entry->private_data;
308
309 mutex_lock(&per_pin->lock);
310 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
311 mutex_unlock(&per_pin->lock);
312 }
313
eld_proc_new(struct hdmi_spec_per_pin * per_pin,int index)314 static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
315 {
316 char name[32];
317 struct hda_codec *codec = per_pin->codec;
318 struct snd_info_entry *entry;
319 int err;
320
321 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
322 err = snd_card_proc_new(codec->card, name, &entry);
323 if (err < 0)
324 return err;
325
326 snd_info_set_text_ops(entry, per_pin, print_eld_info);
327 entry->c.text.write = write_eld_info;
328 entry->mode |= 0200;
329 per_pin->proc_entry = entry;
330
331 return 0;
332 }
333
eld_proc_free(struct hdmi_spec_per_pin * per_pin)334 static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
335 {
336 if (!per_pin->codec->bus->shutdown) {
337 snd_info_free_entry(per_pin->proc_entry);
338 per_pin->proc_entry = NULL;
339 }
340 }
341 #else
eld_proc_new(struct hdmi_spec_per_pin * per_pin,int index)342 static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
343 int index)
344 {
345 return 0;
346 }
eld_proc_free(struct hdmi_spec_per_pin * per_pin)347 static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
348 {
349 }
350 #endif
351
352 /*
353 * Audio InfoFrame routines
354 */
355
356 /*
357 * Enable Audio InfoFrame Transmission
358 */
hdmi_start_infoframe_trans(struct hda_codec * codec,hda_nid_t pin_nid)359 static void hdmi_start_infoframe_trans(struct hda_codec *codec,
360 hda_nid_t pin_nid)
361 {
362 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
363 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
364 AC_DIPXMIT_BEST);
365 }
366
367 /*
368 * Disable Audio InfoFrame Transmission
369 */
hdmi_stop_infoframe_trans(struct hda_codec * codec,hda_nid_t pin_nid)370 static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
371 hda_nid_t pin_nid)
372 {
373 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
374 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
375 AC_DIPXMIT_DISABLE);
376 }
377
hdmi_debug_dip_size(struct hda_codec * codec,hda_nid_t pin_nid)378 static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
379 {
380 #ifdef CONFIG_SND_DEBUG_VERBOSE
381 int i;
382 int size;
383
384 size = snd_hdmi_get_eld_size(codec, pin_nid);
385 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
386
387 for (i = 0; i < 8; i++) {
388 size = snd_hda_codec_read(codec, pin_nid, 0,
389 AC_VERB_GET_HDMI_DIP_SIZE, i);
390 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
391 }
392 #endif
393 }
394
hdmi_clear_dip_buffers(struct hda_codec * codec,hda_nid_t pin_nid)395 static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
396 {
397 #ifdef BE_PARANOID
398 int i, j;
399 int size;
400 int pi, bi;
401 for (i = 0; i < 8; i++) {
402 size = snd_hda_codec_read(codec, pin_nid, 0,
403 AC_VERB_GET_HDMI_DIP_SIZE, i);
404 if (size == 0)
405 continue;
406
407 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
408 for (j = 1; j < 1000; j++) {
409 hdmi_write_dip_byte(codec, pin_nid, 0x0);
410 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
411 if (pi != i)
412 codec_dbg(codec, "dip index %d: %d != %d\n",
413 bi, pi, i);
414 if (bi == 0) /* byte index wrapped around */
415 break;
416 }
417 codec_dbg(codec,
418 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
419 i, size, j);
420 }
421 #endif
422 }
423
hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe * hdmi_ai)424 static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
425 {
426 u8 *bytes = (u8 *)hdmi_ai;
427 u8 sum = 0;
428 int i;
429
430 hdmi_ai->checksum = 0;
431
432 for (i = 0; i < sizeof(*hdmi_ai); i++)
433 sum += bytes[i];
434
435 hdmi_ai->checksum = -sum;
436 }
437
hdmi_fill_audio_infoframe(struct hda_codec * codec,hda_nid_t pin_nid,u8 * dip,int size)438 static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
439 hda_nid_t pin_nid,
440 u8 *dip, int size)
441 {
442 int i;
443
444 hdmi_debug_dip_size(codec, pin_nid);
445 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
446
447 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
448 for (i = 0; i < size; i++)
449 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
450 }
451
hdmi_infoframe_uptodate(struct hda_codec * codec,hda_nid_t pin_nid,u8 * dip,int size)452 static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
453 u8 *dip, int size)
454 {
455 u8 val;
456 int i;
457
458 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
459 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
460 != AC_DIPXMIT_BEST)
461 return false;
462
463 for (i = 0; i < size; i++) {
464 val = snd_hda_codec_read(codec, pin_nid, 0,
465 AC_VERB_GET_HDMI_DIP_DATA, 0);
466 if (val != dip[i])
467 return false;
468 }
469
470 return true;
471 }
472
hdmi_pin_get_eld(struct hda_codec * codec,hda_nid_t nid,int dev_id,unsigned char * buf,int * eld_size)473 static int hdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
474 int dev_id, unsigned char *buf, int *eld_size)
475 {
476 snd_hda_set_dev_select(codec, nid, dev_id);
477
478 return snd_hdmi_get_eld(codec, nid, buf, eld_size);
479 }
480
hdmi_pin_setup_infoframe(struct hda_codec * codec,hda_nid_t pin_nid,int dev_id,int ca,int active_channels,int conn_type)481 static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
482 hda_nid_t pin_nid, int dev_id,
483 int ca, int active_channels,
484 int conn_type)
485 {
486 struct hdmi_spec *spec = codec->spec;
487 union audio_infoframe ai;
488
489 memset(&ai, 0, sizeof(ai));
490 if ((conn_type == 0) || /* HDMI */
491 /* Nvidia DisplayPort: Nvidia HW expects same layout as HDMI */
492 (conn_type == 1 && spec->nv_dp_workaround)) {
493 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
494
495 if (conn_type == 0) { /* HDMI */
496 hdmi_ai->type = 0x84;
497 hdmi_ai->ver = 0x01;
498 hdmi_ai->len = 0x0a;
499 } else {/* Nvidia DP */
500 hdmi_ai->type = 0x84;
501 hdmi_ai->ver = 0x1b;
502 hdmi_ai->len = 0x11 << 2;
503 }
504 hdmi_ai->CC02_CT47 = active_channels - 1;
505 hdmi_ai->CA = ca;
506 hdmi_checksum_audio_infoframe(hdmi_ai);
507 } else if (conn_type == 1) { /* DisplayPort */
508 struct dp_audio_infoframe *dp_ai = &ai.dp;
509
510 dp_ai->type = 0x84;
511 dp_ai->len = 0x1b;
512 dp_ai->ver = 0x11 << 2;
513 dp_ai->CC02_CT47 = active_channels - 1;
514 dp_ai->CA = ca;
515 } else {
516 codec_dbg(codec, "HDMI: unknown connection type at pin NID 0x%x\n", pin_nid);
517 return;
518 }
519
520 snd_hda_set_dev_select(codec, pin_nid, dev_id);
521
522 /*
523 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
524 * sizeof(*dp_ai) to avoid partial match/update problems when
525 * the user switches between HDMI/DP monitors.
526 */
527 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
528 sizeof(ai))) {
529 codec_dbg(codec, "%s: pin NID=0x%x channels=%d ca=0x%02x\n",
530 __func__, pin_nid, active_channels, ca);
531 hdmi_stop_infoframe_trans(codec, pin_nid);
532 hdmi_fill_audio_infoframe(codec, pin_nid,
533 ai.bytes, sizeof(ai));
534 hdmi_start_infoframe_trans(codec, pin_nid);
535 }
536 }
537
snd_hda_hdmi_setup_audio_infoframe(struct hda_codec * codec,struct hdmi_spec_per_pin * per_pin,bool non_pcm)538 void snd_hda_hdmi_setup_audio_infoframe(struct hda_codec *codec,
539 struct hdmi_spec_per_pin *per_pin,
540 bool non_pcm)
541 {
542 struct hdmi_spec *spec = codec->spec;
543 struct hdac_chmap *chmap = &spec->chmap;
544 hda_nid_t pin_nid = per_pin->pin_nid;
545 int dev_id = per_pin->dev_id;
546 int channels = per_pin->channels;
547 int active_channels;
548 struct hdmi_eld *eld;
549 int ca;
550
551 if (!channels)
552 return;
553
554 snd_hda_set_dev_select(codec, pin_nid, dev_id);
555
556 /* some HW (e.g. HSW+) needs reprogramming the amp at each time */
557 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
558 snd_hda_codec_write(codec, pin_nid, 0,
559 AC_VERB_SET_AMP_GAIN_MUTE,
560 AMP_OUT_UNMUTE);
561
562 eld = &per_pin->sink_eld;
563
564 ca = snd_hdac_channel_allocation(&codec->core,
565 eld->info.spk_alloc, channels,
566 per_pin->chmap_set, non_pcm, per_pin->chmap);
567
568 active_channels = snd_hdac_get_active_channels(ca);
569
570 chmap->ops.set_channel_count(&codec->core, per_pin->cvt_nid,
571 active_channels);
572
573 /*
574 * always configure channel mapping, it may have been changed by the
575 * user in the meantime
576 */
577 snd_hdac_setup_channel_mapping(&spec->chmap,
578 pin_nid, non_pcm, ca, channels,
579 per_pin->chmap, per_pin->chmap_set);
580
581 spec->ops.pin_setup_infoframe(codec, pin_nid, dev_id,
582 ca, active_channels, eld->info.conn_type);
583
584 per_pin->non_pcm = non_pcm;
585 }
586 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_setup_audio_infoframe, "SND_HDA_CODEC_HDMI");
587
588 /*
589 * Unsolicited events
590 */
591
592 static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
593
snd_hda_hdmi_check_presence_and_report(struct hda_codec * codec,hda_nid_t nid,int dev_id)594 void snd_hda_hdmi_check_presence_and_report(struct hda_codec *codec,
595 hda_nid_t nid, int dev_id)
596 {
597 struct hdmi_spec *spec = codec->spec;
598 int pin_idx = pin_id_to_pin_index(codec, nid, dev_id);
599
600 if (pin_idx < 0)
601 return;
602 mutex_lock(&spec->pcm_lock);
603 hdmi_present_sense(get_pin(spec, pin_idx), 1);
604 mutex_unlock(&spec->pcm_lock);
605 }
606 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_check_presence_and_report,
607 "SND_HDA_CODEC_HDMI");
608
jack_callback(struct hda_codec * codec,struct hda_jack_callback * jack)609 static void jack_callback(struct hda_codec *codec,
610 struct hda_jack_callback *jack)
611 {
612 /* stop polling when notification is enabled */
613 if (codec_has_acomp(codec))
614 return;
615
616 snd_hda_hdmi_check_presence_and_report(codec, jack->nid, jack->dev_id);
617 }
618
hdmi_intrinsic_event(struct hda_codec * codec,unsigned int res,struct hda_jack_tbl * jack)619 static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res,
620 struct hda_jack_tbl *jack)
621 {
622 jack->jack_dirty = 1;
623
624 codec_dbg(codec,
625 "HDMI hot plug event: Codec=%d NID=0x%x Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
626 codec->addr, jack->nid, jack->dev_id, !!(res & AC_UNSOL_RES_IA),
627 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
628
629 snd_hda_hdmi_check_presence_and_report(codec, jack->nid, jack->dev_id);
630 }
631
hdmi_non_intrinsic_event(struct hda_codec * codec,unsigned int res)632 static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
633 {
634 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
635 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
636 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
637 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
638
639 codec_info(codec,
640 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
641 codec->addr,
642 tag,
643 subtag,
644 cp_state,
645 cp_ready);
646
647 /* TODO */
648 if (cp_state) {
649 ;
650 }
651 if (cp_ready) {
652 ;
653 }
654 }
655
snd_hda_hdmi_generic_unsol_event(struct hda_codec * codec,unsigned int res)656 void snd_hda_hdmi_generic_unsol_event(struct hda_codec *codec, unsigned int res)
657 {
658 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
659 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
660 struct hda_jack_tbl *jack;
661
662 if (codec_has_acomp(codec))
663 return;
664
665 if (codec->dp_mst) {
666 int dev_entry =
667 (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
668
669 jack = snd_hda_jack_tbl_get_from_tag(codec, tag, dev_entry);
670 } else {
671 jack = snd_hda_jack_tbl_get_from_tag(codec, tag, 0);
672 }
673
674 if (!jack) {
675 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
676 return;
677 }
678
679 if (subtag == 0)
680 hdmi_intrinsic_event(codec, res, jack);
681 else
682 hdmi_non_intrinsic_event(codec, res);
683 }
684 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_unsol_event, "SND_HDA_CODEC_HDMI");
685
686 /*
687 * Callbacks
688 */
689
690 /* HBR should be Non-PCM, 8 channels */
691 #define is_hbr_format(format) \
692 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
693
hdmi_pin_hbr_setup(struct hda_codec * codec,hda_nid_t pin_nid,int dev_id,bool hbr)694 static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
695 int dev_id, bool hbr)
696 {
697 int pinctl, new_pinctl;
698
699 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
700 snd_hda_set_dev_select(codec, pin_nid, dev_id);
701 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
702 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
703
704 if (pinctl < 0)
705 return hbr ? -EINVAL : 0;
706
707 new_pinctl = pinctl & ~AC_PINCTL_EPT;
708 if (hbr)
709 new_pinctl |= AC_PINCTL_EPT_HBR;
710 else
711 new_pinctl |= AC_PINCTL_EPT_NATIVE;
712
713 codec_dbg(codec,
714 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
715 pin_nid,
716 pinctl == new_pinctl ? "" : "new-",
717 new_pinctl);
718
719 if (pinctl != new_pinctl)
720 snd_hda_codec_write(codec, pin_nid, 0,
721 AC_VERB_SET_PIN_WIDGET_CONTROL,
722 new_pinctl);
723 } else if (hbr)
724 return -EINVAL;
725
726 return 0;
727 }
728
snd_hda_hdmi_setup_stream(struct hda_codec * codec,hda_nid_t cvt_nid,hda_nid_t pin_nid,int dev_id,u32 stream_tag,int format)729 int snd_hda_hdmi_setup_stream(struct hda_codec *codec,
730 hda_nid_t cvt_nid,
731 hda_nid_t pin_nid, int dev_id,
732 u32 stream_tag, int format)
733 {
734 struct hdmi_spec *spec = codec->spec;
735 unsigned int param;
736 int err;
737
738 err = spec->ops.pin_hbr_setup(codec, pin_nid, dev_id,
739 is_hbr_format(format));
740
741 if (err) {
742 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
743 return err;
744 }
745
746 if (spec->intel_hsw_fixup) {
747
748 /*
749 * on recent platforms IEC Coding Type is required for HBR
750 * support, read current Digital Converter settings and set
751 * ICT bitfield if needed.
752 */
753 param = snd_hda_codec_read(codec, cvt_nid, 0,
754 AC_VERB_GET_DIGI_CONVERT_1, 0);
755
756 param = (param >> 16) & ~(AC_DIG3_ICT);
757
758 /* on recent platforms ICT mode is required for HBR support */
759 if (is_hbr_format(format))
760 param |= 0x1;
761
762 snd_hda_codec_write(codec, cvt_nid, 0,
763 AC_VERB_SET_DIGI_CONVERT_3, param);
764 }
765
766 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
767 return 0;
768 }
769 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_setup_stream, "SND_HDA_CODEC_HDMI");
770
771 /* Try to find an available converter
772 * If pin_idx is less then zero, just try to find an available converter.
773 * Otherwise, try to find an available converter and get the cvt mux index
774 * of the pin.
775 */
hdmi_choose_cvt(struct hda_codec * codec,int pin_idx,int * cvt_id,bool silent)776 static int hdmi_choose_cvt(struct hda_codec *codec,
777 int pin_idx, int *cvt_id,
778 bool silent)
779 {
780 struct hdmi_spec *spec = codec->spec;
781 struct hdmi_spec_per_pin *per_pin;
782 struct hdmi_spec_per_cvt *per_cvt = NULL;
783 int cvt_idx, mux_idx = 0;
784
785 /* pin_idx < 0 means no pin will be bound to the converter */
786 if (pin_idx < 0)
787 per_pin = NULL;
788 else
789 per_pin = get_pin(spec, pin_idx);
790
791 if (per_pin && per_pin->silent_stream) {
792 cvt_idx = cvt_nid_to_cvt_index(codec, per_pin->cvt_nid);
793 per_cvt = get_cvt(spec, cvt_idx);
794 if (per_cvt->assigned && !silent)
795 return -EBUSY;
796 if (cvt_id)
797 *cvt_id = cvt_idx;
798 return 0;
799 }
800
801 /* Dynamically assign converter to stream */
802 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
803 per_cvt = get_cvt(spec, cvt_idx);
804
805 /* Must not already be assigned */
806 if (per_cvt->assigned || per_cvt->silent_stream)
807 continue;
808 if (per_pin == NULL)
809 break;
810 /* Must be in pin's mux's list of converters */
811 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
812 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
813 break;
814 /* Not in mux list */
815 if (mux_idx == per_pin->num_mux_nids)
816 continue;
817 break;
818 }
819
820 /* No free converters */
821 if (cvt_idx == spec->num_cvts)
822 return -EBUSY;
823
824 if (per_pin != NULL)
825 per_pin->mux_idx = mux_idx;
826
827 if (cvt_id)
828 *cvt_id = cvt_idx;
829
830 return 0;
831 }
832
833 /* skeleton caller of pin_cvt_fixup ops */
pin_cvt_fixup(struct hda_codec * codec,struct hdmi_spec_per_pin * per_pin,hda_nid_t cvt_nid)834 static void pin_cvt_fixup(struct hda_codec *codec,
835 struct hdmi_spec_per_pin *per_pin,
836 hda_nid_t cvt_nid)
837 {
838 struct hdmi_spec *spec = codec->spec;
839
840 if (spec->ops.pin_cvt_fixup)
841 spec->ops.pin_cvt_fixup(codec, per_pin, cvt_nid);
842 }
843
844 /* called in hdmi_pcm_open when no pin is assigned to the PCM */
hdmi_pcm_open_no_pin(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)845 static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
846 struct hda_codec *codec,
847 struct snd_pcm_substream *substream)
848 {
849 struct hdmi_spec *spec = codec->spec;
850 struct snd_pcm_runtime *runtime = substream->runtime;
851 int cvt_idx, pcm_idx;
852 struct hdmi_spec_per_cvt *per_cvt = NULL;
853 int err;
854
855 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
856 if (pcm_idx < 0)
857 return -EINVAL;
858
859 err = hdmi_choose_cvt(codec, -1, &cvt_idx, false);
860 if (err)
861 return err;
862
863 per_cvt = get_cvt(spec, cvt_idx);
864 per_cvt->assigned = true;
865 hinfo->nid = per_cvt->cvt_nid;
866
867 pin_cvt_fixup(codec, NULL, per_cvt->cvt_nid);
868
869 set_bit(pcm_idx, &spec->pcm_in_use);
870 /* todo: setup spdif ctls assign */
871
872 /* Initially set the converter's capabilities */
873 hinfo->channels_min = per_cvt->channels_min;
874 hinfo->channels_max = per_cvt->channels_max;
875 hinfo->rates = per_cvt->rates;
876 hinfo->formats = per_cvt->formats;
877 hinfo->maxbps = per_cvt->maxbps;
878
879 /* Store the updated parameters */
880 runtime->hw.channels_min = hinfo->channels_min;
881 runtime->hw.channels_max = hinfo->channels_max;
882 runtime->hw.formats = hinfo->formats;
883 runtime->hw.rates = hinfo->rates;
884
885 snd_pcm_hw_constraint_step(substream->runtime, 0,
886 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
887 return 0;
888 }
889
890 /*
891 * HDA PCM callbacks
892 */
hdmi_pcm_open(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)893 static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
894 struct hda_codec *codec,
895 struct snd_pcm_substream *substream)
896 {
897 struct hdmi_spec *spec = codec->spec;
898 struct snd_pcm_runtime *runtime = substream->runtime;
899 int pin_idx, cvt_idx, pcm_idx;
900 struct hdmi_spec_per_pin *per_pin;
901 struct hdmi_eld *eld;
902 struct hdmi_spec_per_cvt *per_cvt = NULL;
903 int err;
904
905 /* Validate hinfo */
906 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
907 if (pcm_idx < 0)
908 return -EINVAL;
909
910 mutex_lock(&spec->pcm_lock);
911 pin_idx = hinfo_to_pin_index(codec, hinfo);
912 /* no pin is assigned to the PCM
913 * PA need pcm open successfully when probe
914 */
915 if (pin_idx < 0) {
916 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
917 goto unlock;
918 }
919
920 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, false);
921 if (err < 0)
922 goto unlock;
923
924 per_cvt = get_cvt(spec, cvt_idx);
925 /* Claim converter */
926 per_cvt->assigned = true;
927
928 set_bit(pcm_idx, &spec->pcm_in_use);
929 per_pin = get_pin(spec, pin_idx);
930 per_pin->cvt_nid = per_cvt->cvt_nid;
931 hinfo->nid = per_cvt->cvt_nid;
932
933 /* flip stripe flag for the assigned stream if supported */
934 if (get_wcaps(codec, per_cvt->cvt_nid) & AC_WCAP_STRIPE)
935 azx_stream(get_azx_dev(substream))->stripe = 1;
936
937 snd_hda_set_dev_select(codec, per_pin->pin_nid, per_pin->dev_id);
938 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
939 AC_VERB_SET_CONNECT_SEL,
940 per_pin->mux_idx);
941
942 /* configure unused pins to choose other converters */
943 pin_cvt_fixup(codec, per_pin, 0);
944
945 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
946
947 /* Initially set the converter's capabilities */
948 hinfo->channels_min = per_cvt->channels_min;
949 hinfo->channels_max = per_cvt->channels_max;
950 hinfo->rates = per_cvt->rates;
951 hinfo->formats = per_cvt->formats;
952 hinfo->maxbps = per_cvt->maxbps;
953
954 eld = &per_pin->sink_eld;
955 /* Restrict capabilities by ELD if this isn't disabled */
956 if (!static_hdmi_pcm && eld->eld_valid) {
957 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
958 if (hinfo->channels_min > hinfo->channels_max ||
959 !hinfo->rates || !hinfo->formats) {
960 per_cvt->assigned = false;
961 hinfo->nid = 0;
962 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
963 err = -ENODEV;
964 goto unlock;
965 }
966 }
967
968 /* Store the updated parameters */
969 runtime->hw.channels_min = hinfo->channels_min;
970 runtime->hw.channels_max = hinfo->channels_max;
971 runtime->hw.formats = hinfo->formats;
972 runtime->hw.rates = hinfo->rates;
973
974 snd_pcm_hw_constraint_step(substream->runtime, 0,
975 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
976 unlock:
977 mutex_unlock(&spec->pcm_lock);
978 return err;
979 }
980
981 /*
982 * HDA/HDMI auto parsing
983 */
hdmi_read_pin_conn(struct hda_codec * codec,int pin_idx)984 static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
985 {
986 struct hdmi_spec *spec = codec->spec;
987 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
988 hda_nid_t pin_nid = per_pin->pin_nid;
989 int dev_id = per_pin->dev_id;
990 int conns;
991
992 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
993 codec_warn(codec,
994 "HDMI: pin NID 0x%x wcaps %#x does not support connection list\n",
995 pin_nid, get_wcaps(codec, pin_nid));
996 return -EINVAL;
997 }
998
999 snd_hda_set_dev_select(codec, pin_nid, dev_id);
1000
1001 if (spec->intel_hsw_fixup) {
1002 conns = spec->num_cvts;
1003 memcpy(per_pin->mux_nids, spec->cvt_nids,
1004 sizeof(hda_nid_t) * conns);
1005 } else {
1006 conns = snd_hda_get_raw_connections(codec, pin_nid,
1007 per_pin->mux_nids,
1008 HDA_MAX_CONNECTIONS);
1009 }
1010
1011 /* all the device entries on the same pin have the same conn list */
1012 per_pin->num_mux_nids = conns;
1013
1014 return 0;
1015 }
1016
hdmi_find_pcm_slot(struct hdmi_spec * spec,struct hdmi_spec_per_pin * per_pin)1017 static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1018 struct hdmi_spec_per_pin *per_pin)
1019 {
1020 int i;
1021
1022 for (i = 0; i < spec->pcm_used; i++) {
1023 if (!test_bit(i, &spec->pcm_bitmap))
1024 return i;
1025 }
1026 return -EBUSY;
1027 }
1028
hdmi_attach_hda_pcm(struct hdmi_spec * spec,struct hdmi_spec_per_pin * per_pin)1029 static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1030 struct hdmi_spec_per_pin *per_pin)
1031 {
1032 int idx;
1033
1034 /* pcm already be attached to the pin */
1035 if (per_pin->pcm)
1036 return;
1037 /* try the previously used slot at first */
1038 idx = per_pin->prev_pcm_idx;
1039 if (idx >= 0) {
1040 if (!test_bit(idx, &spec->pcm_bitmap))
1041 goto found;
1042 per_pin->prev_pcm_idx = -1; /* no longer valid, clear it */
1043 }
1044 idx = hdmi_find_pcm_slot(spec, per_pin);
1045 if (idx == -EBUSY)
1046 return;
1047 found:
1048 per_pin->pcm_idx = idx;
1049 per_pin->pcm = get_hdmi_pcm(spec, idx);
1050 set_bit(idx, &spec->pcm_bitmap);
1051 }
1052
hdmi_detach_hda_pcm(struct hdmi_spec * spec,struct hdmi_spec_per_pin * per_pin)1053 static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1054 struct hdmi_spec_per_pin *per_pin)
1055 {
1056 int idx;
1057
1058 /* pcm already be detached from the pin */
1059 if (!per_pin->pcm)
1060 return;
1061 idx = per_pin->pcm_idx;
1062 per_pin->pcm_idx = -1;
1063 per_pin->prev_pcm_idx = idx; /* remember the previous index */
1064 per_pin->pcm = NULL;
1065 if (idx >= 0 && idx < spec->pcm_used)
1066 clear_bit(idx, &spec->pcm_bitmap);
1067 }
1068
hdmi_get_pin_cvt_mux(struct hdmi_spec * spec,struct hdmi_spec_per_pin * per_pin,hda_nid_t cvt_nid)1069 static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1070 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1071 {
1072 int mux_idx;
1073
1074 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1075 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1076 break;
1077 return mux_idx;
1078 }
1079
1080 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1081
hdmi_pcm_setup_pin(struct hdmi_spec * spec,struct hdmi_spec_per_pin * per_pin)1082 static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1083 struct hdmi_spec_per_pin *per_pin)
1084 {
1085 struct hda_codec *codec = per_pin->codec;
1086 struct hda_pcm *pcm;
1087 struct hda_pcm_stream *hinfo;
1088 struct snd_pcm_substream *substream;
1089 int mux_idx;
1090 bool non_pcm;
1091
1092 if (per_pin->pcm_idx < 0 || per_pin->pcm_idx >= spec->pcm_used)
1093 return;
1094 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
1095 if (!pcm->pcm)
1096 return;
1097 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1098 return;
1099
1100 /* hdmi audio only uses playback and one substream */
1101 hinfo = pcm->stream;
1102 substream = pcm->pcm->streams[0].substream;
1103
1104 per_pin->cvt_nid = hinfo->nid;
1105
1106 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1107 if (mux_idx < per_pin->num_mux_nids) {
1108 snd_hda_set_dev_select(codec, per_pin->pin_nid,
1109 per_pin->dev_id);
1110 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1111 AC_VERB_SET_CONNECT_SEL,
1112 mux_idx);
1113 }
1114 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1115
1116 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1117 if (substream->runtime)
1118 per_pin->channels = substream->runtime->channels;
1119 per_pin->setup = true;
1120 per_pin->mux_idx = mux_idx;
1121
1122 snd_hda_hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1123 }
1124
hdmi_pcm_reset_pin(struct hdmi_spec * spec,struct hdmi_spec_per_pin * per_pin)1125 static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1126 struct hdmi_spec_per_pin *per_pin)
1127 {
1128 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1129 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1130
1131 per_pin->chmap_set = false;
1132 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1133
1134 per_pin->setup = false;
1135 per_pin->channels = 0;
1136 }
1137
pin_idx_to_pcm_jack(struct hda_codec * codec,struct hdmi_spec_per_pin * per_pin)1138 static struct snd_jack *pin_idx_to_pcm_jack(struct hda_codec *codec,
1139 struct hdmi_spec_per_pin *per_pin)
1140 {
1141 struct hdmi_spec *spec = codec->spec;
1142
1143 if (per_pin->pcm_idx >= 0)
1144 return spec->pcm_rec[per_pin->pcm_idx].jack;
1145 else
1146 return NULL;
1147 }
1148
1149 /* update per_pin ELD from the given new ELD;
1150 * setup info frame and notification accordingly
1151 * also notify ELD kctl and report jack status changes
1152 */
update_eld(struct hda_codec * codec,struct hdmi_spec_per_pin * per_pin,struct hdmi_eld * eld,int repoll)1153 static void update_eld(struct hda_codec *codec,
1154 struct hdmi_spec_per_pin *per_pin,
1155 struct hdmi_eld *eld,
1156 int repoll)
1157 {
1158 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1159 struct hdmi_spec *spec = codec->spec;
1160 struct snd_jack *pcm_jack;
1161 bool old_eld_valid = pin_eld->eld_valid;
1162 bool eld_changed;
1163 int pcm_idx;
1164
1165 if (eld->eld_valid) {
1166 if (eld->eld_size <= 0 ||
1167 snd_parse_eld(hda_codec_dev(codec), &eld->info,
1168 eld->eld_buffer, eld->eld_size) < 0) {
1169 eld->eld_valid = false;
1170 if (repoll) {
1171 schedule_delayed_work(&per_pin->work,
1172 msecs_to_jiffies(300));
1173 return;
1174 }
1175 }
1176 }
1177
1178 if (!eld->eld_valid || eld->eld_size <= 0 || eld->info.sad_count <= 0) {
1179 eld->eld_valid = false;
1180 eld->eld_size = 0;
1181 }
1182
1183 /* for monitor disconnection, save pcm_idx firstly */
1184 pcm_idx = per_pin->pcm_idx;
1185
1186 /*
1187 * pcm_idx >=0 before update_eld() means it is in monitor
1188 * disconnected event. Jack must be fetched before update_eld().
1189 */
1190 pcm_jack = pin_idx_to_pcm_jack(codec, per_pin);
1191
1192 if (!spec->static_pcm_mapping) {
1193 if (eld->eld_valid) {
1194 hdmi_attach_hda_pcm(spec, per_pin);
1195 hdmi_pcm_setup_pin(spec, per_pin);
1196 } else {
1197 hdmi_pcm_reset_pin(spec, per_pin);
1198 hdmi_detach_hda_pcm(spec, per_pin);
1199 }
1200 }
1201
1202 /* if pcm_idx == -1, it means this is in monitor connection event
1203 * we can get the correct pcm_idx now.
1204 */
1205 if (pcm_idx == -1)
1206 pcm_idx = per_pin->pcm_idx;
1207 if (!pcm_jack)
1208 pcm_jack = pin_idx_to_pcm_jack(codec, per_pin);
1209
1210 if (eld->eld_valid)
1211 snd_show_eld(hda_codec_dev(codec), &eld->info);
1212
1213 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1214 eld_changed |= (pin_eld->monitor_present != eld->monitor_present);
1215 if (!eld_changed && eld->eld_valid && pin_eld->eld_valid)
1216 if (pin_eld->eld_size != eld->eld_size ||
1217 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1218 eld->eld_size) != 0)
1219 eld_changed = true;
1220
1221 if (eld_changed) {
1222 pin_eld->monitor_present = eld->monitor_present;
1223 pin_eld->eld_valid = eld->eld_valid;
1224 pin_eld->eld_size = eld->eld_size;
1225 if (eld->eld_valid)
1226 memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1227 eld->eld_size);
1228 pin_eld->info = eld->info;
1229 }
1230
1231 /*
1232 * Re-setup pin and infoframe. This is needed e.g. when
1233 * - sink is first plugged-in
1234 * - transcoder can change during stream playback on Haswell
1235 * and this can make HW reset converter selection on a pin.
1236 */
1237 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1238 pin_cvt_fixup(codec, per_pin, 0);
1239 snd_hda_hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1240 }
1241
1242 if (eld_changed && pcm_idx >= 0)
1243 snd_ctl_notify(codec->card,
1244 SNDRV_CTL_EVENT_MASK_VALUE |
1245 SNDRV_CTL_EVENT_MASK_INFO,
1246 &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
1247
1248 if (eld_changed && pcm_jack)
1249 snd_jack_report(pcm_jack,
1250 (eld->monitor_present && eld->eld_valid) ?
1251 SND_JACK_AVOUT : 0);
1252 }
1253
1254 /* update ELD and jack state via HD-audio verbs */
hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin * per_pin,int repoll)1255 static void hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1256 int repoll)
1257 {
1258 struct hda_codec *codec = per_pin->codec;
1259 struct hdmi_spec *spec = codec->spec;
1260 struct hdmi_eld *eld = &spec->temp_eld;
1261 struct device *dev = hda_codec_dev(codec);
1262 hda_nid_t pin_nid = per_pin->pin_nid;
1263 int dev_id = per_pin->dev_id;
1264 /*
1265 * Always execute a GetPinSense verb here, even when called from
1266 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1267 * response's PD bit is not the real PD value, but indicates that
1268 * the real PD value changed. An older version of the HD-audio
1269 * specification worked this way. Hence, we just ignore the data in
1270 * the unsolicited response to avoid custom WARs.
1271 */
1272 int present;
1273 int ret;
1274
1275 #ifdef CONFIG_PM
1276 if (dev->power.runtime_status == RPM_SUSPENDING)
1277 return;
1278 #endif
1279
1280 ret = snd_hda_power_up_pm(codec);
1281 if (ret < 0 && pm_runtime_suspended(dev))
1282 goto out;
1283
1284 present = snd_hda_jack_pin_sense(codec, pin_nid, dev_id);
1285
1286 mutex_lock(&per_pin->lock);
1287 eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1288 if (eld->monitor_present)
1289 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1290 else
1291 eld->eld_valid = false;
1292
1293 codec_dbg(codec,
1294 "HDMI status: Codec=%d NID=0x%x Presence_Detect=%d ELD_Valid=%d\n",
1295 codec->addr, pin_nid, eld->monitor_present, eld->eld_valid);
1296
1297 if (eld->eld_valid) {
1298 if (spec->ops.pin_get_eld(codec, pin_nid, dev_id,
1299 eld->eld_buffer, &eld->eld_size) < 0)
1300 eld->eld_valid = false;
1301 }
1302
1303 update_eld(codec, per_pin, eld, repoll);
1304 mutex_unlock(&per_pin->lock);
1305 out:
1306 snd_hda_power_down_pm(codec);
1307 }
1308
silent_stream_enable(struct hda_codec * codec,struct hdmi_spec_per_pin * per_pin)1309 static void silent_stream_enable(struct hda_codec *codec,
1310 struct hdmi_spec_per_pin *per_pin)
1311 {
1312 struct hdmi_spec *spec = codec->spec;
1313 struct hdmi_spec_per_cvt *per_cvt;
1314 int cvt_idx, pin_idx, err;
1315
1316 /*
1317 * Power-up will call hdmi_present_sense, so the PM calls
1318 * have to be done without mutex held.
1319 */
1320
1321 err = snd_hda_power_up_pm(codec);
1322 if (err < 0 && err != -EACCES) {
1323 codec_err(codec,
1324 "Failed to power up codec for silent stream enable ret=[%d]\n", err);
1325 snd_hda_power_down_pm(codec);
1326 return;
1327 }
1328
1329 mutex_lock(&per_pin->lock);
1330
1331 if (per_pin->setup) {
1332 codec_dbg(codec, "hdmi: PCM already open, no silent stream\n");
1333 err = -EBUSY;
1334 goto unlock_out;
1335 }
1336
1337 pin_idx = pin_id_to_pin_index(codec, per_pin->pin_nid, per_pin->dev_id);
1338 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, true);
1339 if (err) {
1340 codec_err(codec, "hdmi: no free converter to enable silent mode\n");
1341 goto unlock_out;
1342 }
1343
1344 per_cvt = get_cvt(spec, cvt_idx);
1345 per_cvt->silent_stream = true;
1346 per_pin->cvt_nid = per_cvt->cvt_nid;
1347 per_pin->silent_stream = true;
1348
1349 codec_dbg(codec, "hdmi: enabling silent stream pin-NID=0x%x cvt-NID=0x%x\n",
1350 per_pin->pin_nid, per_cvt->cvt_nid);
1351
1352 snd_hda_set_dev_select(codec, per_pin->pin_nid, per_pin->dev_id);
1353 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1354 AC_VERB_SET_CONNECT_SEL,
1355 per_pin->mux_idx);
1356
1357 /* configure unused pins to choose other converters */
1358 pin_cvt_fixup(codec, per_pin, 0);
1359
1360 spec->ops.silent_stream(codec, per_pin, true);
1361
1362 unlock_out:
1363 mutex_unlock(&per_pin->lock);
1364
1365 snd_hda_power_down_pm(codec);
1366 }
1367
silent_stream_disable(struct hda_codec * codec,struct hdmi_spec_per_pin * per_pin)1368 static void silent_stream_disable(struct hda_codec *codec,
1369 struct hdmi_spec_per_pin *per_pin)
1370 {
1371 struct hdmi_spec *spec = codec->spec;
1372 struct hdmi_spec_per_cvt *per_cvt;
1373 int cvt_idx, err;
1374
1375 err = snd_hda_power_up_pm(codec);
1376 if (err < 0 && err != -EACCES) {
1377 codec_err(codec,
1378 "Failed to power up codec for silent stream disable ret=[%d]\n",
1379 err);
1380 snd_hda_power_down_pm(codec);
1381 return;
1382 }
1383
1384 mutex_lock(&per_pin->lock);
1385 if (!per_pin->silent_stream)
1386 goto unlock_out;
1387
1388 codec_dbg(codec, "HDMI: disable silent stream on pin-NID=0x%x cvt-NID=0x%x\n",
1389 per_pin->pin_nid, per_pin->cvt_nid);
1390
1391 cvt_idx = cvt_nid_to_cvt_index(codec, per_pin->cvt_nid);
1392 if (cvt_idx >= 0 && cvt_idx < spec->num_cvts) {
1393 per_cvt = get_cvt(spec, cvt_idx);
1394 per_cvt->silent_stream = false;
1395 }
1396
1397 spec->ops.silent_stream(codec, per_pin, false);
1398
1399 per_pin->cvt_nid = 0;
1400 per_pin->silent_stream = false;
1401
1402 unlock_out:
1403 mutex_unlock(&per_pin->lock);
1404
1405 snd_hda_power_down_pm(codec);
1406 }
1407
1408 /* update ELD and jack state via audio component */
sync_eld_via_acomp(struct hda_codec * codec,struct hdmi_spec_per_pin * per_pin)1409 static void sync_eld_via_acomp(struct hda_codec *codec,
1410 struct hdmi_spec_per_pin *per_pin)
1411 {
1412 struct hdmi_spec *spec = codec->spec;
1413 struct hdmi_eld *eld = &spec->temp_eld;
1414 bool monitor_prev, monitor_next;
1415
1416 mutex_lock(&per_pin->lock);
1417 eld->monitor_present = false;
1418 monitor_prev = per_pin->sink_eld.monitor_present;
1419 eld->eld_size = snd_hdac_acomp_get_eld(&codec->core, per_pin->pin_nid,
1420 per_pin->dev_id, &eld->monitor_present,
1421 eld->eld_buffer, ELD_MAX_SIZE);
1422 eld->eld_valid = (eld->eld_size > 0);
1423 update_eld(codec, per_pin, eld, 0);
1424 monitor_next = per_pin->sink_eld.monitor_present;
1425 mutex_unlock(&per_pin->lock);
1426
1427 if (spec->silent_stream_type) {
1428 if (!monitor_prev && monitor_next)
1429 silent_stream_enable(codec, per_pin);
1430 else if (monitor_prev && !monitor_next)
1431 silent_stream_disable(codec, per_pin);
1432 }
1433 }
1434
hdmi_present_sense(struct hdmi_spec_per_pin * per_pin,int repoll)1435 static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1436 {
1437 struct hda_codec *codec = per_pin->codec;
1438
1439 if (!codec_has_acomp(codec))
1440 hdmi_present_sense_via_verbs(per_pin, repoll);
1441 else
1442 sync_eld_via_acomp(codec, per_pin);
1443 }
1444
hdmi_repoll_eld(struct work_struct * work)1445 static void hdmi_repoll_eld(struct work_struct *work)
1446 {
1447 struct hdmi_spec_per_pin *per_pin =
1448 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1449 struct hda_codec *codec = per_pin->codec;
1450 struct hdmi_spec *spec = codec->spec;
1451 struct hda_jack_tbl *jack;
1452
1453 jack = snd_hda_jack_tbl_get_mst(codec, per_pin->pin_nid,
1454 per_pin->dev_id);
1455 if (jack)
1456 jack->jack_dirty = 1;
1457
1458 if (per_pin->repoll_count++ > 6)
1459 per_pin->repoll_count = 0;
1460
1461 mutex_lock(&spec->pcm_lock);
1462 hdmi_present_sense(per_pin, per_pin->repoll_count);
1463 mutex_unlock(&spec->pcm_lock);
1464 }
1465
hdmi_add_pin(struct hda_codec * codec,hda_nid_t pin_nid)1466 static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1467 {
1468 struct hdmi_spec *spec = codec->spec;
1469 unsigned int caps, config;
1470 int pin_idx;
1471 struct hdmi_spec_per_pin *per_pin;
1472 int err;
1473 int dev_num, i;
1474
1475 caps = snd_hda_query_pin_caps(codec, pin_nid);
1476 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1477 return 0;
1478
1479 /*
1480 * For DP MST audio, Configuration Default is the same for
1481 * all device entries on the same pin
1482 */
1483 config = snd_hda_codec_get_pincfg(codec, pin_nid);
1484 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE &&
1485 !spec->force_connect)
1486 return 0;
1487
1488 /*
1489 * To simplify the implementation, malloc all
1490 * the virtual pins in the initialization statically
1491 */
1492 if (spec->intel_hsw_fixup) {
1493 /*
1494 * On Intel platforms, device entries count returned
1495 * by AC_PAR_DEVLIST_LEN is dynamic, and depends on
1496 * the type of receiver that is connected. Allocate pin
1497 * structures based on worst case.
1498 */
1499 dev_num = spec->dev_num;
1500 } else if (codec->dp_mst) {
1501 dev_num = snd_hda_get_num_devices(codec, pin_nid) + 1;
1502 /*
1503 * spec->dev_num is the maxinum number of device entries
1504 * among all the pins
1505 */
1506 spec->dev_num = (spec->dev_num > dev_num) ?
1507 spec->dev_num : dev_num;
1508 } else {
1509 /*
1510 * If the platform doesn't support DP MST,
1511 * manually set dev_num to 1. This means
1512 * the pin has only one device entry.
1513 */
1514 dev_num = 1;
1515 spec->dev_num = 1;
1516 }
1517
1518 for (i = 0; i < dev_num; i++) {
1519 pin_idx = spec->num_pins;
1520 per_pin = snd_array_new(&spec->pins);
1521
1522 if (!per_pin)
1523 return -ENOMEM;
1524
1525 per_pin->pcm = NULL;
1526 per_pin->pcm_idx = -1;
1527 per_pin->prev_pcm_idx = -1;
1528 per_pin->pin_nid = pin_nid;
1529 per_pin->pin_nid_idx = spec->num_nids;
1530 per_pin->dev_id = i;
1531 per_pin->non_pcm = false;
1532 snd_hda_set_dev_select(codec, pin_nid, i);
1533 err = hdmi_read_pin_conn(codec, pin_idx);
1534 if (err < 0)
1535 return err;
1536 if (!is_jack_detectable(codec, pin_nid))
1537 codec_warn(codec, "HDMI: pin NID 0x%x - jack not detectable\n", pin_nid);
1538 spec->num_pins++;
1539 }
1540 spec->num_nids++;
1541
1542 return 0;
1543 }
1544
hdmi_add_cvt(struct hda_codec * codec,hda_nid_t cvt_nid)1545 static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1546 {
1547 struct hdmi_spec *spec = codec->spec;
1548 struct hdmi_spec_per_cvt *per_cvt;
1549 unsigned int chans;
1550 int err;
1551
1552 chans = get_wcaps(codec, cvt_nid);
1553 chans = get_wcaps_channels(chans);
1554
1555 per_cvt = snd_array_new(&spec->cvts);
1556 if (!per_cvt)
1557 return -ENOMEM;
1558
1559 per_cvt->cvt_nid = cvt_nid;
1560 per_cvt->channels_min = 2;
1561 if (chans <= 16) {
1562 per_cvt->channels_max = chans;
1563 if (chans > spec->chmap.channels_max)
1564 spec->chmap.channels_max = chans;
1565 }
1566
1567 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1568 &per_cvt->rates,
1569 &per_cvt->formats,
1570 NULL,
1571 &per_cvt->maxbps);
1572 if (err < 0)
1573 return err;
1574
1575 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1576 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1577 spec->num_cvts++;
1578
1579 return 0;
1580 }
1581
1582 static const struct snd_pci_quirk force_connect_list[] = {
1583 SND_PCI_QUIRK(0x103c, 0x83e2, "HP EliteDesk 800 G4", 1),
1584 SND_PCI_QUIRK(0x103c, 0x83ef, "HP MP9 G4 Retail System AMS", 1),
1585 SND_PCI_QUIRK(0x103c, 0x870f, "HP", 1),
1586 SND_PCI_QUIRK(0x103c, 0x871a, "HP", 1),
1587 SND_PCI_QUIRK(0x103c, 0x8711, "HP", 1),
1588 SND_PCI_QUIRK(0x103c, 0x8715, "HP", 1),
1589 SND_PCI_QUIRK(0x1043, 0x86ae, "ASUS", 1), /* Z170 PRO */
1590 SND_PCI_QUIRK(0x1043, 0x86c7, "ASUS", 1), /* Z170M PLUS */
1591 SND_PCI_QUIRK(0x1462, 0xec94, "MS-7C94", 1),
1592 SND_PCI_QUIRK(0x8086, 0x2060, "Intel NUC5CPYB", 1),
1593 SND_PCI_QUIRK(0x8086, 0x2081, "Intel NUC 10", 1),
1594 {}
1595 };
1596
snd_hda_hdmi_parse_codec(struct hda_codec * codec)1597 int snd_hda_hdmi_parse_codec(struct hda_codec *codec)
1598 {
1599 struct hdmi_spec *spec = codec->spec;
1600 hda_nid_t start_nid;
1601 unsigned int caps;
1602 int i, nodes;
1603 const struct snd_pci_quirk *q;
1604
1605 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &start_nid);
1606 if (!start_nid || nodes < 0) {
1607 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
1608 return -EINVAL;
1609 }
1610
1611 if (enable_all_pins)
1612 spec->force_connect = true;
1613
1614 q = snd_pci_quirk_lookup(codec->bus->pci, force_connect_list);
1615
1616 if (q && q->value)
1617 spec->force_connect = true;
1618
1619 /*
1620 * hdmi_add_pin() assumes total amount of converters to
1621 * be known, so first discover all converters
1622 */
1623 for (i = 0; i < nodes; i++) {
1624 hda_nid_t nid = start_nid + i;
1625
1626 caps = get_wcaps(codec, nid);
1627
1628 if (!(caps & AC_WCAP_DIGITAL))
1629 continue;
1630
1631 if (get_wcaps_type(caps) == AC_WID_AUD_OUT)
1632 hdmi_add_cvt(codec, nid);
1633 }
1634
1635 /* discover audio pins */
1636 for (i = 0; i < nodes; i++) {
1637 hda_nid_t nid = start_nid + i;
1638
1639 caps = get_wcaps(codec, nid);
1640
1641 if (!(caps & AC_WCAP_DIGITAL))
1642 continue;
1643
1644 if (get_wcaps_type(caps) == AC_WID_PIN)
1645 hdmi_add_pin(codec, nid);
1646 }
1647
1648 return 0;
1649 }
1650 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_parse_codec, "SND_HDA_CODEC_HDMI");
1651
1652 /*
1653 */
check_non_pcm_per_cvt(struct hda_codec * codec,hda_nid_t cvt_nid)1654 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1655 {
1656 struct hda_spdif_out *spdif;
1657 bool non_pcm;
1658
1659 mutex_lock(&codec->spdif_mutex);
1660 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1661 /* Add sanity check to pass klockwork check.
1662 * This should never happen.
1663 */
1664 if (WARN_ON(spdif == NULL)) {
1665 mutex_unlock(&codec->spdif_mutex);
1666 return true;
1667 }
1668 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1669 mutex_unlock(&codec->spdif_mutex);
1670 return non_pcm;
1671 }
1672
1673 /*
1674 * HDMI callbacks
1675 */
1676
snd_hda_hdmi_generic_pcm_prepare(struct hda_pcm_stream * hinfo,struct hda_codec * codec,unsigned int stream_tag,unsigned int format,struct snd_pcm_substream * substream)1677 int snd_hda_hdmi_generic_pcm_prepare(struct hda_pcm_stream *hinfo,
1678 struct hda_codec *codec,
1679 unsigned int stream_tag,
1680 unsigned int format,
1681 struct snd_pcm_substream *substream)
1682 {
1683 hda_nid_t cvt_nid = hinfo->nid;
1684 struct hdmi_spec *spec = codec->spec;
1685 int pin_idx;
1686 struct hdmi_spec_per_pin *per_pin;
1687 struct snd_pcm_runtime *runtime = substream->runtime;
1688 bool non_pcm;
1689 int pinctl, stripe;
1690 int err = 0;
1691
1692 mutex_lock(&spec->pcm_lock);
1693 pin_idx = hinfo_to_pin_index(codec, hinfo);
1694 if (pin_idx < 0) {
1695 /* when pcm is not bound to a pin skip pin setup and return 0
1696 * to make audio playback be ongoing
1697 */
1698 pin_cvt_fixup(codec, NULL, cvt_nid);
1699 snd_hda_codec_setup_stream(codec, cvt_nid,
1700 stream_tag, 0, format);
1701 goto unlock;
1702 }
1703
1704 per_pin = get_pin(spec, pin_idx);
1705
1706 /* Verify pin:cvt selections to avoid silent audio after S3.
1707 * After S3, the audio driver restores pin:cvt selections
1708 * but this can happen before gfx is ready and such selection
1709 * is overlooked by HW. Thus multiple pins can share a same
1710 * default convertor and mute control will affect each other,
1711 * which can cause a resumed audio playback become silent
1712 * after S3.
1713 */
1714 pin_cvt_fixup(codec, per_pin, 0);
1715
1716 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
1717 /* Todo: add DP1.2 MST audio support later */
1718 if (codec_has_acomp(codec))
1719 snd_hdac_sync_audio_rate(&codec->core, per_pin->pin_nid,
1720 per_pin->dev_id, runtime->rate);
1721
1722 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
1723 mutex_lock(&per_pin->lock);
1724 per_pin->channels = substream->runtime->channels;
1725 per_pin->setup = true;
1726
1727 if (get_wcaps(codec, cvt_nid) & AC_WCAP_STRIPE) {
1728 stripe = snd_hdac_get_stream_stripe_ctl(&codec->bus->core,
1729 substream);
1730 snd_hda_codec_write(codec, cvt_nid, 0,
1731 AC_VERB_SET_STRIPE_CONTROL,
1732 stripe);
1733 }
1734
1735 snd_hda_hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1736 mutex_unlock(&per_pin->lock);
1737 if (spec->dyn_pin_out) {
1738 snd_hda_set_dev_select(codec, per_pin->pin_nid,
1739 per_pin->dev_id);
1740 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1741 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1742 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1743 AC_VERB_SET_PIN_WIDGET_CONTROL,
1744 pinctl | PIN_OUT);
1745 }
1746
1747 /* snd_hda_set_dev_select() has been called before */
1748 err = spec->ops.setup_stream(codec, cvt_nid, per_pin->pin_nid,
1749 per_pin->dev_id, stream_tag, format);
1750 unlock:
1751 mutex_unlock(&spec->pcm_lock);
1752 return err;
1753 }
1754 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_pcm_prepare, "SND_HDA_CODEC_HDMI");
1755
snd_hda_hdmi_generic_pcm_cleanup(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)1756 int snd_hda_hdmi_generic_pcm_cleanup(struct hda_pcm_stream *hinfo,
1757 struct hda_codec *codec,
1758 struct snd_pcm_substream *substream)
1759 {
1760 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1761 return 0;
1762 }
1763 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_pcm_cleanup, "SND_HDA_CODEC_HDMI");
1764
hdmi_pcm_close(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)1765 static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1766 struct hda_codec *codec,
1767 struct snd_pcm_substream *substream)
1768 {
1769 struct hdmi_spec *spec = codec->spec;
1770 int cvt_idx, pin_idx, pcm_idx;
1771 struct hdmi_spec_per_cvt *per_cvt;
1772 struct hdmi_spec_per_pin *per_pin;
1773 int pinctl;
1774 int err = 0;
1775
1776 mutex_lock(&spec->pcm_lock);
1777 if (hinfo->nid) {
1778 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1779 if (snd_BUG_ON(pcm_idx < 0)) {
1780 err = -EINVAL;
1781 goto unlock;
1782 }
1783 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
1784 if (snd_BUG_ON(cvt_idx < 0)) {
1785 err = -EINVAL;
1786 goto unlock;
1787 }
1788 per_cvt = get_cvt(spec, cvt_idx);
1789 per_cvt->assigned = false;
1790 hinfo->nid = 0;
1791
1792 azx_stream(get_azx_dev(substream))->stripe = 0;
1793
1794 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
1795 clear_bit(pcm_idx, &spec->pcm_in_use);
1796 pin_idx = hinfo_to_pin_index(codec, hinfo);
1797 /*
1798 * In such a case, return 0 to match the behavior in
1799 * hdmi_pcm_open()
1800 */
1801 if (pin_idx < 0)
1802 goto unlock;
1803
1804 per_pin = get_pin(spec, pin_idx);
1805
1806 if (spec->dyn_pin_out) {
1807 snd_hda_set_dev_select(codec, per_pin->pin_nid,
1808 per_pin->dev_id);
1809 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1810 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1811 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1812 AC_VERB_SET_PIN_WIDGET_CONTROL,
1813 pinctl & ~PIN_OUT);
1814 }
1815
1816 mutex_lock(&per_pin->lock);
1817 per_pin->chmap_set = false;
1818 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1819
1820 per_pin->setup = false;
1821 per_pin->channels = 0;
1822 mutex_unlock(&per_pin->lock);
1823 }
1824
1825 unlock:
1826 mutex_unlock(&spec->pcm_lock);
1827
1828 return err;
1829 }
1830
1831 static const struct hda_pcm_ops generic_ops = {
1832 .open = hdmi_pcm_open,
1833 .close = hdmi_pcm_close,
1834 .prepare = snd_hda_hdmi_generic_pcm_prepare,
1835 .cleanup = snd_hda_hdmi_generic_pcm_cleanup,
1836 };
1837
hdmi_get_spk_alloc(struct hdac_device * hdac,int pcm_idx)1838 static int hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
1839 {
1840 struct hda_codec *codec = hdac_to_hda_codec(hdac);
1841 struct hdmi_spec *spec = codec->spec;
1842 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1843
1844 if (!per_pin)
1845 return 0;
1846
1847 return per_pin->sink_eld.info.spk_alloc;
1848 }
1849
hdmi_get_chmap(struct hdac_device * hdac,int pcm_idx,unsigned char * chmap)1850 static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
1851 unsigned char *chmap)
1852 {
1853 struct hda_codec *codec = hdac_to_hda_codec(hdac);
1854 struct hdmi_spec *spec = codec->spec;
1855 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1856
1857 /* chmap is already set to 0 in caller */
1858 if (!per_pin)
1859 return;
1860
1861 memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
1862 }
1863
hdmi_set_chmap(struct hdac_device * hdac,int pcm_idx,unsigned char * chmap,int prepared)1864 static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
1865 unsigned char *chmap, int prepared)
1866 {
1867 struct hda_codec *codec = hdac_to_hda_codec(hdac);
1868 struct hdmi_spec *spec = codec->spec;
1869 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1870
1871 if (!per_pin)
1872 return;
1873 mutex_lock(&per_pin->lock);
1874 per_pin->chmap_set = true;
1875 memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
1876 if (prepared)
1877 snd_hda_hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1878 mutex_unlock(&per_pin->lock);
1879 }
1880
is_hdmi_pcm_attached(struct hdac_device * hdac,int pcm_idx)1881 static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
1882 {
1883 struct hda_codec *codec = hdac_to_hda_codec(hdac);
1884 struct hdmi_spec *spec = codec->spec;
1885 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1886
1887 return per_pin ? true:false;
1888 }
1889
snd_hda_hdmi_generic_build_pcms(struct hda_codec * codec)1890 int snd_hda_hdmi_generic_build_pcms(struct hda_codec *codec)
1891 {
1892 struct hdmi_spec *spec = codec->spec;
1893 int idx, pcm_num;
1894
1895 /* limit the PCM devices to the codec converters or available PINs */
1896 pcm_num = min(spec->num_cvts, spec->num_pins);
1897 codec_dbg(codec, "hdmi: pcm_num set to %d\n", pcm_num);
1898
1899 for (idx = 0; idx < pcm_num; idx++) {
1900 struct hdmi_spec_per_cvt *per_cvt;
1901 struct hda_pcm *info;
1902 struct hda_pcm_stream *pstr;
1903
1904 info = snd_hda_codec_pcm_new(codec, "HDMI %d", idx);
1905 if (!info)
1906 return -ENOMEM;
1907
1908 spec->pcm_rec[idx].pcm = info;
1909 spec->pcm_used++;
1910 info->pcm_type = HDA_PCM_TYPE_HDMI;
1911 info->own_chmap = true;
1912
1913 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
1914 pstr->substreams = 1;
1915 pstr->ops = generic_ops;
1916
1917 per_cvt = get_cvt(spec, 0);
1918 pstr->channels_min = per_cvt->channels_min;
1919 pstr->channels_max = per_cvt->channels_max;
1920
1921 /* pcm number is less than pcm_rec array size */
1922 if (spec->pcm_used >= ARRAY_SIZE(spec->pcm_rec))
1923 break;
1924 /* other pstr fields are set in open */
1925 }
1926
1927 return 0;
1928 }
1929 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_build_pcms, "SND_HDA_CODEC_HDMI");
1930
free_hdmi_jack_priv(struct snd_jack * jack)1931 static void free_hdmi_jack_priv(struct snd_jack *jack)
1932 {
1933 struct hdmi_pcm *pcm = jack->private_data;
1934
1935 pcm->jack = NULL;
1936 }
1937
generic_hdmi_build_jack(struct hda_codec * codec,int pcm_idx)1938 static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
1939 {
1940 char hdmi_str[32] = "HDMI/DP";
1941 struct hdmi_spec *spec = codec->spec;
1942 struct snd_jack *jack;
1943 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
1944 int err;
1945
1946 if (pcmdev > 0)
1947 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
1948
1949 err = snd_jack_new(codec->card, hdmi_str, SND_JACK_AVOUT, &jack,
1950 true, false);
1951 if (err < 0)
1952 return err;
1953
1954 spec->pcm_rec[pcm_idx].jack = jack;
1955 jack->private_data = &spec->pcm_rec[pcm_idx];
1956 jack->private_free = free_hdmi_jack_priv;
1957 return 0;
1958 }
1959
snd_hda_hdmi_generic_build_controls(struct hda_codec * codec)1960 int snd_hda_hdmi_generic_build_controls(struct hda_codec *codec)
1961 {
1962 struct hdmi_spec *spec = codec->spec;
1963 int dev, err;
1964 int pin_idx, pcm_idx;
1965
1966 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
1967 if (!get_pcm_rec(spec, pcm_idx)->pcm) {
1968 /* no PCM: mark this for skipping permanently */
1969 set_bit(pcm_idx, &spec->pcm_bitmap);
1970 continue;
1971 }
1972
1973 err = generic_hdmi_build_jack(codec, pcm_idx);
1974 if (err < 0)
1975 return err;
1976
1977 /* create the spdif for each pcm
1978 * pin will be bound when monitor is connected
1979 */
1980 err = snd_hda_create_dig_out_ctls(codec,
1981 0, spec->cvt_nids[0],
1982 HDA_PCM_TYPE_HDMI);
1983 if (err < 0)
1984 return err;
1985 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
1986
1987 dev = get_pcm_rec(spec, pcm_idx)->device;
1988 if (dev != SNDRV_PCM_INVALID_DEVICE) {
1989 /* add control for ELD Bytes */
1990 err = hdmi_create_eld_ctl(codec, pcm_idx, dev);
1991 if (err < 0)
1992 return err;
1993 }
1994 }
1995
1996 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1997 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1998 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1999
2000 if (spec->static_pcm_mapping) {
2001 hdmi_attach_hda_pcm(spec, per_pin);
2002 hdmi_pcm_setup_pin(spec, per_pin);
2003 }
2004
2005 pin_eld->eld_valid = false;
2006 hdmi_present_sense(per_pin, 0);
2007 }
2008
2009 /* add channel maps */
2010 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2011 struct hda_pcm *pcm;
2012
2013 pcm = get_pcm_rec(spec, pcm_idx);
2014 if (!pcm || !pcm->pcm)
2015 break;
2016 err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap);
2017 if (err < 0)
2018 return err;
2019 }
2020
2021 return 0;
2022 }
2023 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_build_controls, "SND_HDA_CODEC_HDMI");
2024
snd_hda_hdmi_generic_init_per_pins(struct hda_codec * codec)2025 int snd_hda_hdmi_generic_init_per_pins(struct hda_codec *codec)
2026 {
2027 struct hdmi_spec *spec = codec->spec;
2028 int pin_idx;
2029
2030 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2031 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2032
2033 per_pin->codec = codec;
2034 mutex_init(&per_pin->lock);
2035 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
2036 eld_proc_new(per_pin, pin_idx);
2037 }
2038 return 0;
2039 }
2040 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_init_per_pins, "SND_HDA_CODEC_HDMI");
2041
snd_hda_hdmi_generic_init(struct hda_codec * codec)2042 int snd_hda_hdmi_generic_init(struct hda_codec *codec)
2043 {
2044 struct hdmi_spec *spec = codec->spec;
2045 int pin_idx;
2046
2047 mutex_lock(&spec->bind_lock);
2048 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2049 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2050 hda_nid_t pin_nid = per_pin->pin_nid;
2051 int dev_id = per_pin->dev_id;
2052
2053 snd_hda_set_dev_select(codec, pin_nid, dev_id);
2054 hdmi_init_pin(codec, pin_nid);
2055 if (codec_has_acomp(codec))
2056 continue;
2057 snd_hda_jack_detect_enable_callback_mst(codec, pin_nid, dev_id,
2058 jack_callback);
2059 }
2060 mutex_unlock(&spec->bind_lock);
2061 return 0;
2062 }
2063 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_init, "SND_HDA_CODEC_HDMI");
2064
hdmi_array_init(struct hdmi_spec * spec,int nums)2065 static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2066 {
2067 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2068 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
2069 }
2070
hdmi_array_free(struct hdmi_spec * spec)2071 static void hdmi_array_free(struct hdmi_spec *spec)
2072 {
2073 snd_array_free(&spec->pins);
2074 snd_array_free(&spec->cvts);
2075 }
2076
snd_hda_hdmi_generic_spec_free(struct hda_codec * codec)2077 void snd_hda_hdmi_generic_spec_free(struct hda_codec *codec)
2078 {
2079 struct hdmi_spec *spec = codec->spec;
2080
2081 if (spec) {
2082 hdmi_array_free(spec);
2083 kfree(spec);
2084 codec->spec = NULL;
2085 }
2086 codec->dp_mst = false;
2087 }
2088 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_spec_free, "SND_HDA_CODEC_HDMI");
2089
snd_hda_hdmi_generic_remove(struct hda_codec * codec)2090 void snd_hda_hdmi_generic_remove(struct hda_codec *codec)
2091 {
2092 struct hdmi_spec *spec = codec->spec;
2093 int pin_idx, pcm_idx;
2094
2095 if (spec->acomp_registered) {
2096 snd_hdac_acomp_exit(&codec->bus->core);
2097 } else if (codec_has_acomp(codec)) {
2098 snd_hdac_acomp_register_notifier(&codec->bus->core, NULL);
2099 }
2100 codec->relaxed_resume = 0;
2101
2102 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2103 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2104 cancel_delayed_work_sync(&per_pin->work);
2105 eld_proc_free(per_pin);
2106 }
2107
2108 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2109 if (spec->pcm_rec[pcm_idx].jack == NULL)
2110 continue;
2111 snd_device_free(codec->card, spec->pcm_rec[pcm_idx].jack);
2112 }
2113
2114 snd_hda_hdmi_generic_spec_free(codec);
2115 }
2116 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_remove, "SND_HDA_CODEC_HDMI");
2117
snd_hda_hdmi_generic_suspend(struct hda_codec * codec)2118 int snd_hda_hdmi_generic_suspend(struct hda_codec *codec)
2119 {
2120 struct hdmi_spec *spec = codec->spec;
2121 int pin_idx;
2122
2123 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2124 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2125 cancel_delayed_work_sync(&per_pin->work);
2126 }
2127 return 0;
2128 }
2129 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_suspend, "SND_HDA_CODEC_HDMI");
2130
snd_hda_hdmi_generic_resume(struct hda_codec * codec)2131 int snd_hda_hdmi_generic_resume(struct hda_codec *codec)
2132 {
2133 struct hdmi_spec *spec = codec->spec;
2134 int pin_idx;
2135
2136 snd_hda_codec_init(codec);
2137 snd_hda_regmap_sync(codec);
2138
2139 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2140 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2141 hdmi_present_sense(per_pin, 1);
2142 }
2143 return 0;
2144 }
2145 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_resume, "SND_HDA_CODEC_HDMI");
2146
2147 static const struct hdmi_ops generic_standard_hdmi_ops = {
2148 .pin_get_eld = hdmi_pin_get_eld,
2149 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2150 .pin_hbr_setup = hdmi_pin_hbr_setup,
2151 .setup_stream = snd_hda_hdmi_setup_stream,
2152 };
2153
2154 /* allocate codec->spec and assign/initialize generic parser ops */
snd_hda_hdmi_generic_alloc(struct hda_codec * codec)2155 int snd_hda_hdmi_generic_alloc(struct hda_codec *codec)
2156 {
2157 struct hdmi_spec *spec;
2158
2159 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2160 if (!spec)
2161 return -ENOMEM;
2162
2163 spec->codec = codec;
2164 spec->ops = generic_standard_hdmi_ops;
2165 spec->dev_num = 1; /* initialize to 1 */
2166 mutex_init(&spec->pcm_lock);
2167 mutex_init(&spec->bind_lock);
2168 snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
2169
2170 spec->chmap.ops.get_chmap = hdmi_get_chmap;
2171 spec->chmap.ops.set_chmap = hdmi_set_chmap;
2172 spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
2173 spec->chmap.ops.get_spk_alloc = hdmi_get_spk_alloc;
2174
2175 codec->spec = spec;
2176 hdmi_array_init(spec, 4);
2177
2178 return 0;
2179 }
2180 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_alloc, "SND_HDA_CODEC_HDMI");
2181
2182 /* generic HDMI parser */
snd_hda_hdmi_generic_probe(struct hda_codec * codec)2183 int snd_hda_hdmi_generic_probe(struct hda_codec *codec)
2184 {
2185 int err;
2186
2187 err = snd_hda_hdmi_generic_alloc(codec);
2188 if (err < 0)
2189 return err;
2190
2191 err = snd_hda_hdmi_parse_codec(codec);
2192 if (err < 0) {
2193 snd_hda_hdmi_generic_spec_free(codec);
2194 return err;
2195 }
2196
2197 snd_hda_hdmi_generic_init_per_pins(codec);
2198 return 0;
2199 }
2200 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_probe, "SND_HDA_CODEC_HDMI");
2201
2202 /*
2203 * generic audio component binding
2204 */
2205
2206 /* turn on / off the unsol event jack detection dynamically */
reprogram_jack_detect(struct hda_codec * codec,hda_nid_t nid,int dev_id,bool use_acomp)2207 static void reprogram_jack_detect(struct hda_codec *codec, hda_nid_t nid,
2208 int dev_id, bool use_acomp)
2209 {
2210 struct hda_jack_tbl *tbl;
2211
2212 tbl = snd_hda_jack_tbl_get_mst(codec, nid, dev_id);
2213 if (tbl) {
2214 /* clear unsol even if component notifier is used, or re-enable
2215 * if notifier is cleared
2216 */
2217 unsigned int val = use_acomp ? 0 : (AC_USRSP_EN | tbl->tag);
2218 snd_hda_codec_write_cache(codec, nid, 0,
2219 AC_VERB_SET_UNSOLICITED_ENABLE, val);
2220 }
2221 }
2222
2223 /* set up / clear component notifier dynamically */
generic_acomp_notifier_set(struct drm_audio_component * acomp,bool use_acomp)2224 static void generic_acomp_notifier_set(struct drm_audio_component *acomp,
2225 bool use_acomp)
2226 {
2227 struct hdmi_spec *spec;
2228 int i;
2229
2230 spec = container_of(acomp->audio_ops, struct hdmi_spec, drm_audio_ops);
2231 mutex_lock(&spec->bind_lock);
2232 spec->use_acomp_notifier = use_acomp;
2233 spec->codec->relaxed_resume = use_acomp;
2234 spec->codec->bus->keep_power = 0;
2235 /* reprogram each jack detection logic depending on the notifier */
2236 for (i = 0; i < spec->num_pins; i++)
2237 reprogram_jack_detect(spec->codec,
2238 get_pin(spec, i)->pin_nid,
2239 get_pin(spec, i)->dev_id,
2240 use_acomp);
2241 mutex_unlock(&spec->bind_lock);
2242 }
2243
2244 /* enable / disable the notifier via master bind / unbind */
snd_hda_hdmi_acomp_master_bind(struct device * dev,struct drm_audio_component * acomp)2245 int snd_hda_hdmi_acomp_master_bind(struct device *dev,
2246 struct drm_audio_component *acomp)
2247 {
2248 generic_acomp_notifier_set(acomp, true);
2249 return 0;
2250 }
2251 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_acomp_master_bind, "SND_HDA_CODEC_HDMI");
2252
snd_hda_hdmi_acomp_master_unbind(struct device * dev,struct drm_audio_component * acomp)2253 void snd_hda_hdmi_acomp_master_unbind(struct device *dev,
2254 struct drm_audio_component *acomp)
2255 {
2256 generic_acomp_notifier_set(acomp, false);
2257 }
2258 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_acomp_master_unbind, "SND_HDA_CODEC_HDMI");
2259
2260 /* check whether both HD-audio and DRM PCI devices belong to the same bus */
match_bound_vga(struct device * dev,int subtype,void * data)2261 static int match_bound_vga(struct device *dev, int subtype, void *data)
2262 {
2263 struct hdac_bus *bus = data;
2264 struct pci_dev *pci, *master;
2265
2266 if (!dev_is_pci(dev) || !dev_is_pci(bus->dev))
2267 return 0;
2268 master = to_pci_dev(bus->dev);
2269 pci = to_pci_dev(dev);
2270 return master->bus == pci->bus;
2271 }
2272
2273 /* audio component notifier for AMD/Nvidia HDMI codecs */
snd_hda_hdmi_acomp_pin_eld_notify(void * audio_ptr,int port,int dev_id)2274 void snd_hda_hdmi_acomp_pin_eld_notify(void *audio_ptr, int port, int dev_id)
2275 {
2276 struct hda_codec *codec = audio_ptr;
2277 struct hdmi_spec *spec = codec->spec;
2278 hda_nid_t pin_nid = spec->port2pin(codec, port);
2279
2280 if (!pin_nid)
2281 return;
2282 if (get_wcaps_type(get_wcaps(codec, pin_nid)) != AC_WID_PIN)
2283 return;
2284 /* skip notification during system suspend (but not in runtime PM);
2285 * the state will be updated at resume
2286 */
2287 if (codec->core.dev.power.power_state.event == PM_EVENT_SUSPEND)
2288 return;
2289
2290 snd_hda_hdmi_check_presence_and_report(codec, pin_nid, dev_id);
2291 }
2292 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_acomp_pin_eld_notify, "SND_HDA_CODEC_HDMI");
2293
2294 /* set up the private drm_audio_ops from the template */
snd_hda_hdmi_setup_drm_audio_ops(struct hda_codec * codec,const struct drm_audio_component_audio_ops * ops)2295 void snd_hda_hdmi_setup_drm_audio_ops(struct hda_codec *codec,
2296 const struct drm_audio_component_audio_ops *ops)
2297 {
2298 struct hdmi_spec *spec = codec->spec;
2299
2300 spec->drm_audio_ops.audio_ptr = codec;
2301 /* intel_audio_codec_enable() or intel_audio_codec_disable()
2302 * will call pin_eld_notify with using audio_ptr pointer
2303 * We need make sure audio_ptr is really setup
2304 */
2305 wmb();
2306 spec->drm_audio_ops.pin2port = ops->pin2port;
2307 spec->drm_audio_ops.pin_eld_notify = ops->pin_eld_notify;
2308 spec->drm_audio_ops.master_bind = ops->master_bind;
2309 spec->drm_audio_ops.master_unbind = ops->master_unbind;
2310 }
2311 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_setup_drm_audio_ops, "SND_HDA_CODEC_HDMI");
2312
2313 /* initialize the generic HDMI audio component */
snd_hda_hdmi_acomp_init(struct hda_codec * codec,const struct drm_audio_component_audio_ops * ops,int (* port2pin)(struct hda_codec *,int))2314 void snd_hda_hdmi_acomp_init(struct hda_codec *codec,
2315 const struct drm_audio_component_audio_ops *ops,
2316 int (*port2pin)(struct hda_codec *, int))
2317 {
2318 struct hdmi_spec *spec = codec->spec;
2319
2320 if (!enable_acomp) {
2321 codec_info(codec, "audio component disabled by module option\n");
2322 return;
2323 }
2324
2325 spec->port2pin = port2pin;
2326 snd_hda_hdmi_setup_drm_audio_ops(codec, ops);
2327 if (!snd_hdac_acomp_init(&codec->bus->core, &spec->drm_audio_ops,
2328 match_bound_vga, 0)) {
2329 spec->acomp_registered = true;
2330 }
2331 }
2332 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_acomp_init, "SND_HDA_CODEC_HDMI");
2333
2334 /*
2335 */
2336
2337 enum {
2338 MODEL_GENERIC,
2339 MODEL_GF,
2340 };
2341
generichdmi_probe(struct hda_codec * codec,const struct hda_device_id * id)2342 static int generichdmi_probe(struct hda_codec *codec,
2343 const struct hda_device_id *id)
2344 {
2345 int err;
2346
2347 err = snd_hda_hdmi_generic_probe(codec);
2348 if (err < 0)
2349 return err;
2350 /*
2351 * Glenfly GPUs have two codecs, stream switches from one codec to
2352 * another, need to do actual clean-ups in codec_cleanup_stream
2353 */
2354 if (id->driver_data == MODEL_GF)
2355 codec->no_sticky_stream = 1;
2356
2357 return 0;
2358 }
2359
2360 static const struct hda_codec_ops generichdmi_codec_ops = {
2361 .probe = generichdmi_probe,
2362 .remove = snd_hda_hdmi_generic_remove,
2363 .init = snd_hda_hdmi_generic_init,
2364 .build_pcms = snd_hda_hdmi_generic_build_pcms,
2365 .build_controls = snd_hda_hdmi_generic_build_controls,
2366 .unsol_event = snd_hda_hdmi_generic_unsol_event,
2367 .suspend = snd_hda_hdmi_generic_suspend,
2368 .resume = snd_hda_hdmi_generic_resume,
2369 };
2370
2371 /*
2372 */
2373 static const struct hda_device_id snd_hda_id_generichdmi[] = {
2374 HDA_CODEC_ID_MODEL(0x00147a47, "Loongson HDMI", MODEL_GENERIC),
2375 HDA_CODEC_ID_MODEL(0x10951390, "SiI1390 HDMI", MODEL_GENERIC),
2376 HDA_CODEC_ID_MODEL(0x10951392, "SiI1392 HDMI", MODEL_GENERIC),
2377 HDA_CODEC_ID_MODEL(0x11069f84, "VX11 HDMI/DP", MODEL_GENERIC),
2378 HDA_CODEC_ID_MODEL(0x11069f85, "VX11 HDMI/DP", MODEL_GENERIC),
2379 HDA_CODEC_ID_MODEL(0x17e80047, "Chrontel HDMI", MODEL_GENERIC),
2380 HDA_CODEC_ID_MODEL(0x1d179f86, "ZX-100S HDMI/DP", MODEL_GF),
2381 HDA_CODEC_ID_MODEL(0x1d179f87, "ZX-100S HDMI/DP", MODEL_GF),
2382 HDA_CODEC_ID_MODEL(0x1d179f88, "KX-5000 HDMI/DP", MODEL_GF),
2383 HDA_CODEC_ID_MODEL(0x1d179f89, "KX-5000 HDMI/DP", MODEL_GF),
2384 HDA_CODEC_ID_MODEL(0x1d179f8a, "KX-6000 HDMI/DP", MODEL_GF),
2385 HDA_CODEC_ID_MODEL(0x1d179f8b, "KX-6000 HDMI/DP", MODEL_GF),
2386 HDA_CODEC_ID_MODEL(0x1d179f8c, "KX-6000G HDMI/DP", MODEL_GF),
2387 HDA_CODEC_ID_MODEL(0x1d179f8d, "KX-6000G HDMI/DP", MODEL_GF),
2388 HDA_CODEC_ID_MODEL(0x1d179f8e, "KX-7000 HDMI/DP", MODEL_GF),
2389 HDA_CODEC_ID_MODEL(0x1d179f8f, "KX-7000 HDMI/DP", MODEL_GF),
2390 HDA_CODEC_ID_MODEL(0x1d179f90, "KX-7000 HDMI/DP", MODEL_GF),
2391 HDA_CODEC_ID_MODEL(0x67663d82, "Arise 82 HDMI/DP", MODEL_GF),
2392 HDA_CODEC_ID_MODEL(0x67663d83, "Arise 83 HDMI/DP", MODEL_GF),
2393 HDA_CODEC_ID_MODEL(0x67663d84, "Arise 84 HDMI/DP", MODEL_GF),
2394 HDA_CODEC_ID_MODEL(0x67663d85, "Arise 85 HDMI/DP", MODEL_GF),
2395 HDA_CODEC_ID_MODEL(0x67663d86, "Arise 86 HDMI/DP", MODEL_GF),
2396 HDA_CODEC_ID_MODEL(0x67663d87, "Arise 87 HDMI/DP", MODEL_GF),
2397 HDA_CODEC_ID_MODEL(0x80862801, "Bearlake HDMI", MODEL_GENERIC),
2398 HDA_CODEC_ID_MODEL(0x80862802, "Cantiga HDMI", MODEL_GENERIC),
2399 HDA_CODEC_ID_MODEL(0x80862803, "Eaglelake HDMI", MODEL_GENERIC),
2400 HDA_CODEC_ID_MODEL(0x80862880, "CedarTrail HDMI", MODEL_GENERIC),
2401 HDA_CODEC_ID_MODEL(0x808629fb, "Crestline HDMI", MODEL_GENERIC),
2402 /* special ID for generic HDMI */
2403 HDA_CODEC_ID_MODEL(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", MODEL_GENERIC),
2404 {} /* terminator */
2405 };
2406 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generichdmi);
2407
2408 MODULE_LICENSE("GPL");
2409 MODULE_DESCRIPTION("Generic HDMI HD-audio codec");
2410
2411 static struct hda_codec_driver generichdmi_driver = {
2412 .id = snd_hda_id_generichdmi,
2413 .ops = &generichdmi_codec_ops,
2414 };
2415
2416 module_hda_codec_driver(generichdmi_driver);
2417