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