xref: /linux/sound/hda/codecs/hdmi/hdmi.c (revision fdfa38e95e1229ec2fb2f18540987c97f861d470)
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, 0x83f3, "HP ProDesk 400", 1),
1553 	SND_PCI_QUIRK(0x103c, 0x870f, "HP", 1),
1554 	SND_PCI_QUIRK(0x103c, 0x871a, "HP", 1),
1555 	SND_PCI_QUIRK(0x103c, 0x8711, "HP", 1),
1556 	SND_PCI_QUIRK(0x103c, 0x8715, "HP", 1),
1557 	SND_PCI_QUIRK(0x1043, 0x86ae, "ASUS", 1),  /* Z170 PRO */
1558 	SND_PCI_QUIRK(0x1043, 0x86c7, "ASUS", 1),  /* Z170M PLUS */
1559 	SND_PCI_QUIRK(0x1462, 0xec94, "MS-7C94", 1),
1560 	SND_PCI_QUIRK(0x8086, 0x2060, "Intel NUC5CPYB", 1),
1561 	SND_PCI_QUIRK(0x8086, 0x2081, "Intel NUC 10", 1),
1562 	{}
1563 };
1564 
snd_hda_hdmi_parse_codec(struct hda_codec * codec)1565 int snd_hda_hdmi_parse_codec(struct hda_codec *codec)
1566 {
1567 	struct hdmi_spec *spec = codec->spec;
1568 	hda_nid_t start_nid;
1569 	unsigned int caps;
1570 	int i, nodes;
1571 	const struct snd_pci_quirk *q;
1572 
1573 	nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &start_nid);
1574 	if (!start_nid || nodes < 0) {
1575 		codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
1576 		return -EINVAL;
1577 	}
1578 
1579 	if (enable_all_pins)
1580 		spec->force_connect = true;
1581 
1582 	q = snd_pci_quirk_lookup(codec->bus->pci, force_connect_list);
1583 
1584 	if (q && q->value)
1585 		spec->force_connect = true;
1586 
1587 	/*
1588 	 * hdmi_add_pin() assumes total amount of converters to
1589 	 * be known, so first discover all converters
1590 	 */
1591 	for (i = 0; i < nodes; i++) {
1592 		hda_nid_t nid = start_nid + i;
1593 
1594 		caps = get_wcaps(codec, nid);
1595 
1596 		if (!(caps & AC_WCAP_DIGITAL))
1597 			continue;
1598 
1599 		if (get_wcaps_type(caps) == AC_WID_AUD_OUT)
1600 			hdmi_add_cvt(codec, nid);
1601 	}
1602 
1603 	/* discover audio pins */
1604 	for (i = 0; i < nodes; i++) {
1605 		hda_nid_t nid = start_nid + i;
1606 
1607 		caps = get_wcaps(codec, nid);
1608 
1609 		if (!(caps & AC_WCAP_DIGITAL))
1610 			continue;
1611 
1612 		if (get_wcaps_type(caps) == AC_WID_PIN)
1613 			hdmi_add_pin(codec, nid);
1614 	}
1615 
1616 	return 0;
1617 }
1618 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_parse_codec, "SND_HDA_CODEC_HDMI");
1619 
1620 /*
1621  */
check_non_pcm_per_cvt(struct hda_codec * codec,hda_nid_t cvt_nid)1622 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1623 {
1624 	struct hda_spdif_out *spdif;
1625 
1626 	guard(mutex)(&codec->spdif_mutex);
1627 	spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1628 	/* Add sanity check to pass klockwork check.
1629 	 * This should never happen.
1630 	 */
1631 	if (WARN_ON(spdif == NULL))
1632 		return true;
1633 	return !!(spdif->status & IEC958_AES0_NONAUDIO);
1634 }
1635 
1636 /*
1637  * HDMI callbacks
1638  */
1639 
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)1640 int snd_hda_hdmi_generic_pcm_prepare(struct hda_pcm_stream *hinfo,
1641 				     struct hda_codec *codec,
1642 				     unsigned int stream_tag,
1643 				     unsigned int format,
1644 				     struct snd_pcm_substream *substream)
1645 {
1646 	hda_nid_t cvt_nid = hinfo->nid;
1647 	struct hdmi_spec *spec = codec->spec;
1648 	int pin_idx;
1649 	struct hdmi_spec_per_pin *per_pin;
1650 	struct snd_pcm_runtime *runtime = substream->runtime;
1651 	bool non_pcm;
1652 	int pinctl, stripe;
1653 
1654 	guard(mutex)(&spec->pcm_lock);
1655 	pin_idx = hinfo_to_pin_index(codec, hinfo);
1656 	if (pin_idx < 0) {
1657 		/* when pcm is not bound to a pin skip pin setup and return 0
1658 		 * to make audio playback be ongoing
1659 		 */
1660 		pin_cvt_fixup(codec, NULL, cvt_nid);
1661 		snd_hda_codec_setup_stream(codec, cvt_nid,
1662 					stream_tag, 0, format);
1663 		return 0;
1664 	}
1665 
1666 	per_pin = get_pin(spec, pin_idx);
1667 
1668 	/* Verify pin:cvt selections to avoid silent audio after S3.
1669 	 * After S3, the audio driver restores pin:cvt selections
1670 	 * but this can happen before gfx is ready and such selection
1671 	 * is overlooked by HW. Thus multiple pins can share a same
1672 	 * default convertor and mute control will affect each other,
1673 	 * which can cause a resumed audio playback become silent
1674 	 * after S3.
1675 	 */
1676 	pin_cvt_fixup(codec, per_pin, 0);
1677 
1678 	/* Call sync_audio_rate to set the N/CTS/M manually if necessary */
1679 	/* Todo: add DP1.2 MST audio support later */
1680 	if (codec_has_acomp(codec))
1681 		snd_hdac_sync_audio_rate(&codec->core, per_pin->pin_nid,
1682 					 per_pin->dev_id, runtime->rate);
1683 
1684 	non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
1685 	scoped_guard(mutex, &per_pin->lock) {
1686 		per_pin->channels = substream->runtime->channels;
1687 		per_pin->setup = true;
1688 
1689 		if (get_wcaps(codec, cvt_nid) & AC_WCAP_STRIPE) {
1690 			stripe = snd_hdac_get_stream_stripe_ctl(&codec->bus->core,
1691 								substream);
1692 			snd_hda_codec_write(codec, cvt_nid, 0,
1693 					    AC_VERB_SET_STRIPE_CONTROL,
1694 					    stripe);
1695 		}
1696 
1697 		snd_hda_hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1698 	}
1699 	if (spec->dyn_pin_out) {
1700 		snd_hda_set_dev_select(codec, per_pin->pin_nid,
1701 				       per_pin->dev_id);
1702 		pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1703 					    AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1704 		snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1705 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
1706 				    pinctl | PIN_OUT);
1707 	}
1708 
1709 	/* snd_hda_set_dev_select() has been called before */
1710 	return spec->ops.setup_stream(codec, cvt_nid, per_pin->pin_nid,
1711 				      per_pin->dev_id, stream_tag, format);
1712 }
1713 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_pcm_prepare, "SND_HDA_CODEC_HDMI");
1714 
snd_hda_hdmi_generic_pcm_cleanup(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)1715 int snd_hda_hdmi_generic_pcm_cleanup(struct hda_pcm_stream *hinfo,
1716 				     struct hda_codec *codec,
1717 				     struct snd_pcm_substream *substream)
1718 {
1719 	snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1720 	return 0;
1721 }
1722 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_pcm_cleanup, "SND_HDA_CODEC_HDMI");
1723 
hdmi_pcm_close(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)1724 static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1725 			  struct hda_codec *codec,
1726 			  struct snd_pcm_substream *substream)
1727 {
1728 	struct hdmi_spec *spec = codec->spec;
1729 	int cvt_idx, pin_idx, pcm_idx;
1730 	struct hdmi_spec_per_cvt *per_cvt;
1731 	struct hdmi_spec_per_pin *per_pin;
1732 	int pinctl;
1733 
1734 	guard(mutex)(&spec->pcm_lock);
1735 	if (hinfo->nid) {
1736 		pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1737 		if (snd_BUG_ON(pcm_idx < 0))
1738 			return -EINVAL;
1739 		cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
1740 		if (snd_BUG_ON(cvt_idx < 0))
1741 			return -EINVAL;
1742 		per_cvt = get_cvt(spec, cvt_idx);
1743 		per_cvt->assigned = false;
1744 		hinfo->nid = 0;
1745 
1746 		azx_stream(get_azx_dev(substream))->stripe = 0;
1747 
1748 		snd_hda_spdif_ctls_unassign(codec, pcm_idx);
1749 		clear_bit(pcm_idx, &spec->pcm_in_use);
1750 		pin_idx = hinfo_to_pin_index(codec, hinfo);
1751 		/*
1752 		 * In such a case, return 0 to match the behavior in
1753 		 * hdmi_pcm_open()
1754 		 */
1755 		if (pin_idx < 0)
1756 			return 0;
1757 
1758 		per_pin = get_pin(spec, pin_idx);
1759 
1760 		if (spec->dyn_pin_out) {
1761 			snd_hda_set_dev_select(codec, per_pin->pin_nid,
1762 					       per_pin->dev_id);
1763 			pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1764 					AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1765 			snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1766 					    AC_VERB_SET_PIN_WIDGET_CONTROL,
1767 					    pinctl & ~PIN_OUT);
1768 		}
1769 
1770 		guard(mutex)(&per_pin->lock);
1771 		per_pin->chmap_set = false;
1772 		memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1773 
1774 		per_pin->setup = false;
1775 		per_pin->channels = 0;
1776 	}
1777 
1778 	return 0;
1779 }
1780 
1781 static const struct hda_pcm_ops generic_ops = {
1782 	.open = hdmi_pcm_open,
1783 	.close = hdmi_pcm_close,
1784 	.prepare = snd_hda_hdmi_generic_pcm_prepare,
1785 	.cleanup = snd_hda_hdmi_generic_pcm_cleanup,
1786 };
1787 
hdmi_get_spk_alloc(struct hdac_device * hdac,int pcm_idx)1788 static int hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
1789 {
1790 	struct hda_codec *codec = hdac_to_hda_codec(hdac);
1791 	struct hdmi_spec *spec = codec->spec;
1792 	struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1793 
1794 	if (!per_pin)
1795 		return 0;
1796 
1797 	return per_pin->sink_eld.info.spk_alloc;
1798 }
1799 
hdmi_get_chmap(struct hdac_device * hdac,int pcm_idx,unsigned char * chmap)1800 static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
1801 					unsigned char *chmap)
1802 {
1803 	struct hda_codec *codec = hdac_to_hda_codec(hdac);
1804 	struct hdmi_spec *spec = codec->spec;
1805 	struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1806 
1807 	/* chmap is already set to 0 in caller */
1808 	if (!per_pin)
1809 		return;
1810 
1811 	memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
1812 }
1813 
hdmi_set_chmap(struct hdac_device * hdac,int pcm_idx,unsigned char * chmap,int prepared)1814 static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
1815 				unsigned char *chmap, int prepared)
1816 {
1817 	struct hda_codec *codec = hdac_to_hda_codec(hdac);
1818 	struct hdmi_spec *spec = codec->spec;
1819 	struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1820 
1821 	if (!per_pin)
1822 		return;
1823 	guard(mutex)(&per_pin->lock);
1824 	per_pin->chmap_set = true;
1825 	memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
1826 	if (prepared)
1827 		snd_hda_hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1828 }
1829 
is_hdmi_pcm_attached(struct hdac_device * hdac,int pcm_idx)1830 static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
1831 {
1832 	struct hda_codec *codec = hdac_to_hda_codec(hdac);
1833 	struct hdmi_spec *spec = codec->spec;
1834 	struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1835 
1836 	return per_pin ? true:false;
1837 }
1838 
snd_hda_hdmi_generic_build_pcms(struct hda_codec * codec)1839 int snd_hda_hdmi_generic_build_pcms(struct hda_codec *codec)
1840 {
1841 	struct hdmi_spec *spec = codec->spec;
1842 	int idx, pcm_num;
1843 
1844 	/* limit the PCM devices to the codec converters or available PINs */
1845 	pcm_num = min(spec->num_cvts, spec->num_pins);
1846 	codec_dbg(codec, "hdmi: pcm_num set to %d\n", pcm_num);
1847 
1848 	for (idx = 0; idx < pcm_num; idx++) {
1849 		struct hdmi_spec_per_cvt *per_cvt;
1850 		struct hda_pcm *info;
1851 		struct hda_pcm_stream *pstr;
1852 
1853 		info = snd_hda_codec_pcm_new(codec, "HDMI %d", idx);
1854 		if (!info)
1855 			return -ENOMEM;
1856 
1857 		spec->pcm_rec[idx].pcm = info;
1858 		spec->pcm_used++;
1859 		info->pcm_type = HDA_PCM_TYPE_HDMI;
1860 		info->own_chmap = true;
1861 
1862 		pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
1863 		pstr->substreams = 1;
1864 		pstr->ops = generic_ops;
1865 
1866 		per_cvt = get_cvt(spec, 0);
1867 		pstr->channels_min = per_cvt->channels_min;
1868 		pstr->channels_max = per_cvt->channels_max;
1869 
1870 		/* pcm number is less than pcm_rec array size */
1871 		if (spec->pcm_used >= ARRAY_SIZE(spec->pcm_rec))
1872 			break;
1873 		/* other pstr fields are set in open */
1874 	}
1875 
1876 	return 0;
1877 }
1878 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_build_pcms, "SND_HDA_CODEC_HDMI");
1879 
free_hdmi_jack_priv(struct snd_jack * jack)1880 static void free_hdmi_jack_priv(struct snd_jack *jack)
1881 {
1882 	struct hdmi_pcm *pcm = jack->private_data;
1883 
1884 	pcm->jack = NULL;
1885 }
1886 
generic_hdmi_build_jack(struct hda_codec * codec,int pcm_idx)1887 static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
1888 {
1889 	char hdmi_str[32] = "HDMI/DP";
1890 	struct hdmi_spec *spec = codec->spec;
1891 	struct snd_jack *jack;
1892 	int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
1893 	int err;
1894 
1895 	if (pcmdev > 0)
1896 		sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
1897 
1898 	err = snd_jack_new(codec->card, hdmi_str, SND_JACK_AVOUT, &jack,
1899 			   true, false);
1900 	if (err < 0)
1901 		return err;
1902 
1903 	spec->pcm_rec[pcm_idx].jack = jack;
1904 	jack->private_data = &spec->pcm_rec[pcm_idx];
1905 	jack->private_free = free_hdmi_jack_priv;
1906 	return 0;
1907 }
1908 
snd_hda_hdmi_generic_build_controls(struct hda_codec * codec)1909 int snd_hda_hdmi_generic_build_controls(struct hda_codec *codec)
1910 {
1911 	struct hdmi_spec *spec = codec->spec;
1912 	int dev, err;
1913 	int pin_idx, pcm_idx;
1914 
1915 	for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
1916 		if (!get_pcm_rec(spec, pcm_idx)->pcm) {
1917 			/* no PCM: mark this for skipping permanently */
1918 			set_bit(pcm_idx, &spec->pcm_bitmap);
1919 			continue;
1920 		}
1921 
1922 		err = generic_hdmi_build_jack(codec, pcm_idx);
1923 		if (err < 0)
1924 			return err;
1925 
1926 		/* create the spdif for each pcm
1927 		 * pin will be bound when monitor is connected
1928 		 */
1929 		err = snd_hda_create_dig_out_ctls(codec,
1930 					  0, spec->cvt_nids[0],
1931 					  HDA_PCM_TYPE_HDMI);
1932 		if (err < 0)
1933 			return err;
1934 		snd_hda_spdif_ctls_unassign(codec, pcm_idx);
1935 
1936 		dev = get_pcm_rec(spec, pcm_idx)->device;
1937 		if (dev != SNDRV_PCM_INVALID_DEVICE) {
1938 			/* add control for ELD Bytes */
1939 			err = hdmi_create_eld_ctl(codec, pcm_idx, dev);
1940 			if (err < 0)
1941 				return err;
1942 		}
1943 	}
1944 
1945 	for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1946 		struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1947 		struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1948 
1949 		if (spec->static_pcm_mapping) {
1950 			hdmi_attach_hda_pcm(spec, per_pin);
1951 			hdmi_pcm_setup_pin(spec, per_pin);
1952 		}
1953 
1954 		pin_eld->eld_valid = false;
1955 		hdmi_present_sense(per_pin, 0);
1956 	}
1957 
1958 	/* add channel maps */
1959 	for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
1960 		struct hda_pcm *pcm;
1961 
1962 		pcm = get_pcm_rec(spec, pcm_idx);
1963 		if (!pcm || !pcm->pcm)
1964 			break;
1965 		err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap);
1966 		if (err < 0)
1967 			return err;
1968 	}
1969 
1970 	return 0;
1971 }
1972 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_build_controls, "SND_HDA_CODEC_HDMI");
1973 
snd_hda_hdmi_generic_init_per_pins(struct hda_codec * codec)1974 int snd_hda_hdmi_generic_init_per_pins(struct hda_codec *codec)
1975 {
1976 	struct hdmi_spec *spec = codec->spec;
1977 	int pin_idx;
1978 
1979 	for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1980 		struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1981 
1982 		per_pin->codec = codec;
1983 		mutex_init(&per_pin->lock);
1984 		INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
1985 		eld_proc_new(per_pin, pin_idx);
1986 	}
1987 	return 0;
1988 }
1989 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_init_per_pins, "SND_HDA_CODEC_HDMI");
1990 
snd_hda_hdmi_generic_init(struct hda_codec * codec)1991 int snd_hda_hdmi_generic_init(struct hda_codec *codec)
1992 {
1993 	struct hdmi_spec *spec = codec->spec;
1994 	int pin_idx;
1995 
1996 	guard(mutex)(&spec->bind_lock);
1997 	for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1998 		struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1999 		hda_nid_t pin_nid = per_pin->pin_nid;
2000 		int dev_id = per_pin->dev_id;
2001 
2002 		snd_hda_set_dev_select(codec, pin_nid, dev_id);
2003 		hdmi_init_pin(codec, pin_nid);
2004 		if (codec_has_acomp(codec))
2005 			continue;
2006 		snd_hda_jack_detect_enable_callback_mst(codec, pin_nid, dev_id,
2007 							jack_callback);
2008 	}
2009 	return 0;
2010 }
2011 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_init, "SND_HDA_CODEC_HDMI");
2012 
hdmi_array_init(struct hdmi_spec * spec,int nums)2013 static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2014 {
2015 	snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2016 	snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
2017 }
2018 
hdmi_array_free(struct hdmi_spec * spec)2019 static void hdmi_array_free(struct hdmi_spec *spec)
2020 {
2021 	snd_array_free(&spec->pins);
2022 	snd_array_free(&spec->cvts);
2023 }
2024 
snd_hda_hdmi_generic_spec_free(struct hda_codec * codec)2025 void snd_hda_hdmi_generic_spec_free(struct hda_codec *codec)
2026 {
2027 	struct hdmi_spec *spec = codec->spec;
2028 
2029 	if (spec) {
2030 		hdmi_array_free(spec);
2031 		kfree(spec);
2032 		codec->spec = NULL;
2033 	}
2034 	codec->dp_mst = false;
2035 }
2036 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_spec_free, "SND_HDA_CODEC_HDMI");
2037 
snd_hda_hdmi_generic_remove(struct hda_codec * codec)2038 void snd_hda_hdmi_generic_remove(struct hda_codec *codec)
2039 {
2040 	struct hdmi_spec *spec = codec->spec;
2041 	int pin_idx, pcm_idx;
2042 
2043 	if (spec->acomp_registered) {
2044 		snd_hdac_acomp_exit(&codec->bus->core);
2045 	} else if (codec_has_acomp(codec)) {
2046 		snd_hdac_acomp_register_notifier(&codec->bus->core, NULL);
2047 	}
2048 	codec->relaxed_resume = 0;
2049 
2050 	for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2051 		struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2052 		cancel_delayed_work_sync(&per_pin->work);
2053 		eld_proc_free(per_pin);
2054 	}
2055 
2056 	for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2057 		if (spec->pcm_rec[pcm_idx].jack == NULL)
2058 			continue;
2059 		snd_device_free(codec->card, spec->pcm_rec[pcm_idx].jack);
2060 	}
2061 
2062 	snd_hda_hdmi_generic_spec_free(codec);
2063 }
2064 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_remove, "SND_HDA_CODEC_HDMI");
2065 
snd_hda_hdmi_generic_suspend(struct hda_codec * codec)2066 int snd_hda_hdmi_generic_suspend(struct hda_codec *codec)
2067 {
2068 	struct hdmi_spec *spec = codec->spec;
2069 	int pin_idx;
2070 
2071 	for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2072 		struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2073 		cancel_delayed_work_sync(&per_pin->work);
2074 	}
2075 	return 0;
2076 }
2077 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_suspend, "SND_HDA_CODEC_HDMI");
2078 
snd_hda_hdmi_generic_resume(struct hda_codec * codec)2079 int snd_hda_hdmi_generic_resume(struct hda_codec *codec)
2080 {
2081 	struct hdmi_spec *spec = codec->spec;
2082 	int pin_idx;
2083 
2084 	snd_hda_codec_init(codec);
2085 	snd_hda_regmap_sync(codec);
2086 
2087 	for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2088 		struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2089 		hdmi_present_sense(per_pin, 1);
2090 	}
2091 	return 0;
2092 }
2093 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_resume, "SND_HDA_CODEC_HDMI");
2094 
2095 static const struct hdmi_ops generic_standard_hdmi_ops = {
2096 	.pin_get_eld				= hdmi_pin_get_eld,
2097 	.pin_setup_infoframe			= hdmi_pin_setup_infoframe,
2098 	.pin_hbr_setup				= hdmi_pin_hbr_setup,
2099 	.setup_stream				= snd_hda_hdmi_setup_stream,
2100 };
2101 
2102 /* allocate codec->spec and assign/initialize generic parser ops */
snd_hda_hdmi_generic_alloc(struct hda_codec * codec)2103 int snd_hda_hdmi_generic_alloc(struct hda_codec *codec)
2104 {
2105 	struct hdmi_spec *spec;
2106 
2107 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2108 	if (!spec)
2109 		return -ENOMEM;
2110 
2111 	spec->codec = codec;
2112 	spec->ops = generic_standard_hdmi_ops;
2113 	spec->dev_num = 1;	/* initialize to 1 */
2114 	mutex_init(&spec->pcm_lock);
2115 	mutex_init(&spec->bind_lock);
2116 	snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
2117 
2118 	spec->chmap.ops.get_chmap = hdmi_get_chmap;
2119 	spec->chmap.ops.set_chmap = hdmi_set_chmap;
2120 	spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
2121 	spec->chmap.ops.get_spk_alloc = hdmi_get_spk_alloc;
2122 
2123 	codec->spec = spec;
2124 	hdmi_array_init(spec, 4);
2125 
2126 	return 0;
2127 }
2128 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_alloc, "SND_HDA_CODEC_HDMI");
2129 
2130 /* generic HDMI parser */
snd_hda_hdmi_generic_probe(struct hda_codec * codec)2131 int snd_hda_hdmi_generic_probe(struct hda_codec *codec)
2132 {
2133 	int err;
2134 
2135 	err = snd_hda_hdmi_generic_alloc(codec);
2136 	if (err < 0)
2137 		return err;
2138 
2139 	err = snd_hda_hdmi_parse_codec(codec);
2140 	if (err < 0) {
2141 		snd_hda_hdmi_generic_spec_free(codec);
2142 		return err;
2143 	}
2144 
2145 	snd_hda_hdmi_generic_init_per_pins(codec);
2146 	return 0;
2147 }
2148 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_generic_probe, "SND_HDA_CODEC_HDMI");
2149 
2150 /*
2151  * generic audio component binding
2152  */
2153 
2154 /* 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)2155 static void reprogram_jack_detect(struct hda_codec *codec, hda_nid_t nid,
2156 				  int dev_id, bool use_acomp)
2157 {
2158 	struct hda_jack_tbl *tbl;
2159 
2160 	tbl = snd_hda_jack_tbl_get_mst(codec, nid, dev_id);
2161 	if (tbl) {
2162 		/* clear unsol even if component notifier is used, or re-enable
2163 		 * if notifier is cleared
2164 		 */
2165 		unsigned int val = use_acomp ? 0 : (AC_USRSP_EN | tbl->tag);
2166 		snd_hda_codec_write_cache(codec, nid, 0,
2167 					  AC_VERB_SET_UNSOLICITED_ENABLE, val);
2168 	}
2169 }
2170 
2171 /* set up / clear component notifier dynamically */
generic_acomp_notifier_set(struct drm_audio_component * acomp,bool use_acomp)2172 static void generic_acomp_notifier_set(struct drm_audio_component *acomp,
2173 				       bool use_acomp)
2174 {
2175 	struct hdmi_spec *spec;
2176 	int i;
2177 
2178 	spec = container_of(acomp->audio_ops, struct hdmi_spec, drm_audio_ops);
2179 	guard(mutex)(&spec->bind_lock);
2180 	spec->use_acomp_notifier = use_acomp;
2181 	spec->codec->relaxed_resume = use_acomp;
2182 	spec->codec->bus->keep_power = 0;
2183 	/* reprogram each jack detection logic depending on the notifier */
2184 	for (i = 0; i < spec->num_pins; i++)
2185 		reprogram_jack_detect(spec->codec,
2186 				      get_pin(spec, i)->pin_nid,
2187 				      get_pin(spec, i)->dev_id,
2188 				      use_acomp);
2189 }
2190 
2191 /* enable / disable the notifier via master bind / unbind */
snd_hda_hdmi_acomp_master_bind(struct device * dev,struct drm_audio_component * acomp)2192 int snd_hda_hdmi_acomp_master_bind(struct device *dev,
2193 				   struct drm_audio_component *acomp)
2194 {
2195 	generic_acomp_notifier_set(acomp, true);
2196 	return 0;
2197 }
2198 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_acomp_master_bind, "SND_HDA_CODEC_HDMI");
2199 
snd_hda_hdmi_acomp_master_unbind(struct device * dev,struct drm_audio_component * acomp)2200 void snd_hda_hdmi_acomp_master_unbind(struct device *dev,
2201 				      struct drm_audio_component *acomp)
2202 {
2203 	generic_acomp_notifier_set(acomp, false);
2204 }
2205 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_acomp_master_unbind, "SND_HDA_CODEC_HDMI");
2206 
2207 /* check whether both HD-audio and DRM PCI devices belong to the same bus */
match_bound_vga(struct device * dev,int subtype,void * data)2208 static int match_bound_vga(struct device *dev, int subtype, void *data)
2209 {
2210 	struct hdac_bus *bus = data;
2211 	struct pci_dev *pci, *master;
2212 
2213 	if (!dev_is_pci(dev) || !dev_is_pci(bus->dev))
2214 		return 0;
2215 	master = to_pci_dev(bus->dev);
2216 	pci = to_pci_dev(dev);
2217 	return master->bus == pci->bus;
2218 }
2219 
2220 /* audio component notifier for AMD/Nvidia HDMI codecs */
snd_hda_hdmi_acomp_pin_eld_notify(void * audio_ptr,int port,int dev_id)2221 void snd_hda_hdmi_acomp_pin_eld_notify(void *audio_ptr, int port, int dev_id)
2222 {
2223 	struct hda_codec *codec = audio_ptr;
2224 	struct hdmi_spec *spec = codec->spec;
2225 	hda_nid_t pin_nid = spec->port2pin(codec, port);
2226 
2227 	if (!pin_nid)
2228 		return;
2229 	if (get_wcaps_type(get_wcaps(codec, pin_nid)) != AC_WID_PIN)
2230 		return;
2231 	/* skip notification during system suspend (but not in runtime PM);
2232 	 * the state will be updated at resume
2233 	 */
2234 	if (codec->core.dev.power.power_state.event == PM_EVENT_SUSPEND)
2235 		return;
2236 
2237 	snd_hda_hdmi_check_presence_and_report(codec, pin_nid, dev_id);
2238 }
2239 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_acomp_pin_eld_notify, "SND_HDA_CODEC_HDMI");
2240 
2241 /* 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)2242 void snd_hda_hdmi_setup_drm_audio_ops(struct hda_codec *codec,
2243 				      const struct drm_audio_component_audio_ops *ops)
2244 {
2245 	struct hdmi_spec *spec = codec->spec;
2246 
2247 	spec->drm_audio_ops.audio_ptr = codec;
2248 	/* intel_audio_codec_enable() or intel_audio_codec_disable()
2249 	 * will call pin_eld_notify with using audio_ptr pointer
2250 	 * We need make sure audio_ptr is really setup
2251 	 */
2252 	wmb();
2253 	spec->drm_audio_ops.pin2port = ops->pin2port;
2254 	spec->drm_audio_ops.pin_eld_notify = ops->pin_eld_notify;
2255 	spec->drm_audio_ops.master_bind = ops->master_bind;
2256 	spec->drm_audio_ops.master_unbind = ops->master_unbind;
2257 }
2258 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_setup_drm_audio_ops, "SND_HDA_CODEC_HDMI");
2259 
2260 /* 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))2261 void snd_hda_hdmi_acomp_init(struct hda_codec *codec,
2262 			     const struct drm_audio_component_audio_ops *ops,
2263 			     int (*port2pin)(struct hda_codec *, int))
2264 {
2265 	struct hdmi_spec *spec = codec->spec;
2266 
2267 	if (!enable_acomp) {
2268 		codec_info(codec, "audio component disabled by module option\n");
2269 		return;
2270 	}
2271 
2272 	spec->port2pin = port2pin;
2273 	snd_hda_hdmi_setup_drm_audio_ops(codec, ops);
2274 	if (!snd_hdac_acomp_init(&codec->bus->core, &spec->drm_audio_ops,
2275 				 match_bound_vga, 0)) {
2276 		spec->acomp_registered = true;
2277 	}
2278 }
2279 EXPORT_SYMBOL_NS_GPL(snd_hda_hdmi_acomp_init, "SND_HDA_CODEC_HDMI");
2280 
2281 /*
2282  */
2283 
2284 enum {
2285 	MODEL_GENERIC,
2286 	MODEL_GF,
2287 };
2288 
generichdmi_probe(struct hda_codec * codec,const struct hda_device_id * id)2289 static int generichdmi_probe(struct hda_codec *codec,
2290 			     const struct hda_device_id *id)
2291 {
2292 	int err;
2293 
2294 	err = snd_hda_hdmi_generic_probe(codec);
2295 	if (err < 0)
2296 		return err;
2297 	/*
2298 	 * Glenfly GPUs have two codecs, stream switches from one codec to
2299 	 * another, need to do actual clean-ups in codec_cleanup_stream
2300 	 */
2301 	if (id->driver_data == MODEL_GF)
2302 		codec->no_sticky_stream = 1;
2303 
2304 	return 0;
2305 }
2306 
2307 static const struct hda_codec_ops generichdmi_codec_ops = {
2308 	.probe = generichdmi_probe,
2309 	.remove = snd_hda_hdmi_generic_remove,
2310 	.init = snd_hda_hdmi_generic_init,
2311 	.build_pcms = snd_hda_hdmi_generic_build_pcms,
2312 	.build_controls = snd_hda_hdmi_generic_build_controls,
2313 	.unsol_event = snd_hda_hdmi_generic_unsol_event,
2314 	.suspend = snd_hda_hdmi_generic_suspend,
2315 	.resume	 = snd_hda_hdmi_generic_resume,
2316 };
2317 
2318 /*
2319  */
2320 static const struct hda_device_id snd_hda_id_generichdmi[] = {
2321 	HDA_CODEC_ID_MODEL(0x00147a47, "Loongson HDMI",		MODEL_GENERIC),
2322 	HDA_CODEC_ID_MODEL(0x10951390, "SiI1390 HDMI",		MODEL_GENERIC),
2323 	HDA_CODEC_ID_MODEL(0x10951392, "SiI1392 HDMI",		MODEL_GENERIC),
2324 	HDA_CODEC_ID_MODEL(0x11069f84, "VX11 HDMI/DP",		MODEL_GENERIC),
2325 	HDA_CODEC_ID_MODEL(0x11069f85, "VX11 HDMI/DP",		MODEL_GENERIC),
2326 	HDA_CODEC_ID_MODEL(0x17e80047, "Chrontel HDMI",		MODEL_GENERIC),
2327 	HDA_CODEC_ID_MODEL(0x1d179f86, "ZX-100S HDMI/DP",	MODEL_GF),
2328 	HDA_CODEC_ID_MODEL(0x1d179f87, "ZX-100S HDMI/DP",	MODEL_GF),
2329 	HDA_CODEC_ID_MODEL(0x1d179f88, "KX-5000 HDMI/DP",	MODEL_GF),
2330 	HDA_CODEC_ID_MODEL(0x1d179f89, "KX-5000 HDMI/DP",	MODEL_GF),
2331 	HDA_CODEC_ID_MODEL(0x1d179f8a, "KX-6000 HDMI/DP",	MODEL_GF),
2332 	HDA_CODEC_ID_MODEL(0x1d179f8b, "KX-6000 HDMI/DP",	MODEL_GF),
2333 	HDA_CODEC_ID_MODEL(0x1d179f8c, "KX-6000G HDMI/DP",	MODEL_GF),
2334 	HDA_CODEC_ID_MODEL(0x1d179f8d, "KX-6000G HDMI/DP",	MODEL_GF),
2335 	HDA_CODEC_ID_MODEL(0x1d179f8e, "KX-7000 HDMI/DP",	MODEL_GF),
2336 	HDA_CODEC_ID_MODEL(0x1d179f8f, "KX-7000 HDMI/DP",	MODEL_GF),
2337 	HDA_CODEC_ID_MODEL(0x1d179f90, "KX-7000 HDMI/DP",	MODEL_GF),
2338 	HDA_CODEC_ID_MODEL(0x67663d82, "Arise 82 HDMI/DP",	MODEL_GF),
2339 	HDA_CODEC_ID_MODEL(0x67663d83, "Arise 83 HDMI/DP",	MODEL_GF),
2340 	HDA_CODEC_ID_MODEL(0x67663d84, "Arise 84 HDMI/DP",	MODEL_GF),
2341 	HDA_CODEC_ID_MODEL(0x67663d85, "Arise 85 HDMI/DP",	MODEL_GF),
2342 	HDA_CODEC_ID_MODEL(0x67663d86, "Arise 86 HDMI/DP",	MODEL_GF),
2343 	HDA_CODEC_ID_MODEL(0x67663d87, "Arise 87 HDMI/DP",	MODEL_GF),
2344 	HDA_CODEC_ID_MODEL(0x80862801, "Bearlake HDMI",		MODEL_GENERIC),
2345 	HDA_CODEC_ID_MODEL(0x80862802, "Cantiga HDMI",		MODEL_GENERIC),
2346 	HDA_CODEC_ID_MODEL(0x80862803, "Eaglelake HDMI",	MODEL_GENERIC),
2347 	HDA_CODEC_ID_MODEL(0x80862880, "CedarTrail HDMI",	MODEL_GENERIC),
2348 	HDA_CODEC_ID_MODEL(0x808629fb, "Crestline HDMI",	MODEL_GENERIC),
2349 	/* special ID for generic HDMI */
2350 	HDA_CODEC_ID_MODEL(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", MODEL_GENERIC),
2351 	{} /* terminator */
2352 };
2353 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generichdmi);
2354 
2355 MODULE_LICENSE("GPL");
2356 MODULE_DESCRIPTION("Generic HDMI HD-audio codec");
2357 
2358 static struct hda_codec_driver generichdmi_driver = {
2359 	.id = snd_hda_id_generichdmi,
2360 	.ops = &generichdmi_codec_ops,
2361 };
2362 
2363 module_hda_codec_driver(generichdmi_driver);
2364