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