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