xref: /openbmc/linux/sound/pci/hda/patch_hdmi.c (revision 854577ac)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  *  patch_hdmi.c - routines for HDMI/DisplayPort codecs
5  *
6  *  Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
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 
37 static bool static_hdmi_pcm;
38 module_param(static_hdmi_pcm, bool, 0644);
39 MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
40 
41 static bool enable_acomp = true;
42 module_param(enable_acomp, bool, 0444);
43 MODULE_PARM_DESC(enable_acomp, "Enable audio component binding (default=yes)");
44 
45 static bool enable_silent_stream =
46 IS_ENABLED(CONFIG_SND_HDA_INTEL_HDMI_SILENT_STREAM);
47 module_param(enable_silent_stream, bool, 0644);
48 MODULE_PARM_DESC(enable_silent_stream, "Enable Silent Stream for HDMI devices");
49 
50 static bool enable_all_pins;
51 module_param(enable_all_pins, bool, 0444);
52 MODULE_PARM_DESC(enable_all_pins, "Forcibly enable all pins");
53 
54 struct hdmi_spec_per_cvt {
55 	hda_nid_t cvt_nid;
56 	int assigned;
57 	unsigned int channels_min;
58 	unsigned int channels_max;
59 	u32 rates;
60 	u64 formats;
61 	unsigned int maxbps;
62 };
63 
64 /* max. connections to a widget */
65 #define HDA_MAX_CONNECTIONS	32
66 
67 struct hdmi_spec_per_pin {
68 	hda_nid_t pin_nid;
69 	int dev_id;
70 	/* pin idx, different device entries on the same pin use the same idx */
71 	int pin_nid_idx;
72 	int num_mux_nids;
73 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
74 	int mux_idx;
75 	hda_nid_t cvt_nid;
76 
77 	struct hda_codec *codec;
78 	struct hdmi_eld sink_eld;
79 	struct mutex lock;
80 	struct delayed_work work;
81 	struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
82 	int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
83 	int repoll_count;
84 	bool setup; /* the stream has been set up by prepare callback */
85 	bool silent_stream;
86 	int channels; /* current number of channels */
87 	bool non_pcm;
88 	bool chmap_set;		/* channel-map override by ALSA API? */
89 	unsigned char chmap[8]; /* ALSA API channel-map */
90 #ifdef CONFIG_SND_PROC_FS
91 	struct snd_info_entry *proc_entry;
92 #endif
93 };
94 
95 /* operations used by generic code that can be overridden by patches */
96 struct hdmi_ops {
97 	int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
98 			   int dev_id, unsigned char *buf, int *eld_size);
99 
100 	void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
101 				    int dev_id,
102 				    int ca, int active_channels, int conn_type);
103 
104 	/* enable/disable HBR (HD passthrough) */
105 	int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid,
106 			     int dev_id, bool hbr);
107 
108 	int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
109 			    hda_nid_t pin_nid, int dev_id, u32 stream_tag,
110 			    int format);
111 
112 	void (*pin_cvt_fixup)(struct hda_codec *codec,
113 			      struct hdmi_spec_per_pin *per_pin,
114 			      hda_nid_t cvt_nid);
115 };
116 
117 struct hdmi_pcm {
118 	struct hda_pcm *pcm;
119 	struct snd_jack *jack;
120 	struct snd_kcontrol *eld_ctl;
121 };
122 
123 struct hdmi_spec {
124 	struct hda_codec *codec;
125 	int num_cvts;
126 	struct snd_array cvts; /* struct hdmi_spec_per_cvt */
127 	hda_nid_t cvt_nids[4]; /* only for haswell fix */
128 
129 	/*
130 	 * num_pins is the number of virtual pins
131 	 * for example, there are 3 pins, and each pin
132 	 * has 4 device entries, then the num_pins is 12
133 	 */
134 	int num_pins;
135 	/*
136 	 * num_nids is the number of real pins
137 	 * In the above example, num_nids is 3
138 	 */
139 	int num_nids;
140 	/*
141 	 * dev_num is the number of device entries
142 	 * on each pin.
143 	 * In the above example, dev_num is 4
144 	 */
145 	int dev_num;
146 	struct snd_array pins; /* struct hdmi_spec_per_pin */
147 	struct hdmi_pcm pcm_rec[16];
148 	struct mutex pcm_lock;
149 	struct mutex bind_lock; /* for audio component binding */
150 	/* pcm_bitmap means which pcms have been assigned to pins*/
151 	unsigned long pcm_bitmap;
152 	int pcm_used;	/* counter of pcm_rec[] */
153 	/* bitmap shows whether the pcm is opened in user space
154 	 * bit 0 means the first playback PCM (PCM3);
155 	 * bit 1 means the second playback PCM, and so on.
156 	 */
157 	unsigned long pcm_in_use;
158 
159 	struct hdmi_eld temp_eld;
160 	struct hdmi_ops ops;
161 
162 	bool dyn_pin_out;
163 	bool dyn_pcm_assign;
164 	bool dyn_pcm_no_legacy;
165 	bool intel_hsw_fixup;	/* apply Intel platform-specific fixups */
166 	/*
167 	 * Non-generic VIA/NVIDIA specific
168 	 */
169 	struct hda_multi_out multiout;
170 	struct hda_pcm_stream pcm_playback;
171 
172 	bool use_acomp_notifier; /* use eld_notify callback for hotplug */
173 	bool acomp_registered; /* audio component registered in this driver */
174 	bool force_connect; /* force connectivity */
175 	struct drm_audio_component_audio_ops drm_audio_ops;
176 	int (*port2pin)(struct hda_codec *, int); /* reverse port/pin mapping */
177 
178 	struct hdac_chmap chmap;
179 	hda_nid_t vendor_nid;
180 	const int *port_map;
181 	int port_num;
182 	bool send_silent_stream; /* Flag to enable silent stream feature */
183 };
184 
185 #ifdef CONFIG_SND_HDA_COMPONENT
186 static inline bool codec_has_acomp(struct hda_codec *codec)
187 {
188 	struct hdmi_spec *spec = codec->spec;
189 	return spec->use_acomp_notifier;
190 }
191 #else
192 #define codec_has_acomp(codec)	false
193 #endif
194 
195 struct hdmi_audio_infoframe {
196 	u8 type; /* 0x84 */
197 	u8 ver;  /* 0x01 */
198 	u8 len;  /* 0x0a */
199 
200 	u8 checksum;
201 
202 	u8 CC02_CT47;	/* CC in bits 0:2, CT in 4:7 */
203 	u8 SS01_SF24;
204 	u8 CXT04;
205 	u8 CA;
206 	u8 LFEPBL01_LSV36_DM_INH7;
207 };
208 
209 struct dp_audio_infoframe {
210 	u8 type; /* 0x84 */
211 	u8 len;  /* 0x1b */
212 	u8 ver;  /* 0x11 << 2 */
213 
214 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
215 	u8 SS01_SF24;
216 	u8 CXT04;
217 	u8 CA;
218 	u8 LFEPBL01_LSV36_DM_INH7;
219 };
220 
221 union audio_infoframe {
222 	struct hdmi_audio_infoframe hdmi;
223 	struct dp_audio_infoframe dp;
224 	u8 bytes[0];
225 };
226 
227 /*
228  * HDMI routines
229  */
230 
231 #define get_pin(spec, idx) \
232 	((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
233 #define get_cvt(spec, idx) \
234 	((struct hdmi_spec_per_cvt  *)snd_array_elem(&spec->cvts, idx))
235 /* obtain hdmi_pcm object assigned to idx */
236 #define get_hdmi_pcm(spec, idx)	(&(spec)->pcm_rec[idx])
237 /* obtain hda_pcm object assigned to idx */
238 #define get_pcm_rec(spec, idx)	(get_hdmi_pcm(spec, idx)->pcm)
239 
240 static int pin_id_to_pin_index(struct hda_codec *codec,
241 			       hda_nid_t pin_nid, int dev_id)
242 {
243 	struct hdmi_spec *spec = codec->spec;
244 	int pin_idx;
245 	struct hdmi_spec_per_pin *per_pin;
246 
247 	/*
248 	 * (dev_id == -1) means it is NON-MST pin
249 	 * return the first virtual pin on this port
250 	 */
251 	if (dev_id == -1)
252 		dev_id = 0;
253 
254 	for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
255 		per_pin = get_pin(spec, pin_idx);
256 		if ((per_pin->pin_nid == pin_nid) &&
257 			(per_pin->dev_id == dev_id))
258 			return pin_idx;
259 	}
260 
261 	codec_warn(codec, "HDMI: pin NID 0x%x not registered\n", pin_nid);
262 	return -EINVAL;
263 }
264 
265 static int hinfo_to_pcm_index(struct hda_codec *codec,
266 			struct hda_pcm_stream *hinfo)
267 {
268 	struct hdmi_spec *spec = codec->spec;
269 	int pcm_idx;
270 
271 	for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
272 		if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
273 			return pcm_idx;
274 
275 	codec_warn(codec, "HDMI: hinfo %p not tied to a PCM\n", hinfo);
276 	return -EINVAL;
277 }
278 
279 static int hinfo_to_pin_index(struct hda_codec *codec,
280 			      struct hda_pcm_stream *hinfo)
281 {
282 	struct hdmi_spec *spec = codec->spec;
283 	struct hdmi_spec_per_pin *per_pin;
284 	int pin_idx;
285 
286 	for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
287 		per_pin = get_pin(spec, pin_idx);
288 		if (per_pin->pcm &&
289 			per_pin->pcm->pcm->stream == hinfo)
290 			return pin_idx;
291 	}
292 
293 	codec_dbg(codec, "HDMI: hinfo %p (pcm %d) not registered\n", hinfo,
294 		  hinfo_to_pcm_index(codec, hinfo));
295 	return -EINVAL;
296 }
297 
298 static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
299 						int pcm_idx)
300 {
301 	int i;
302 	struct hdmi_spec_per_pin *per_pin;
303 
304 	for (i = 0; i < spec->num_pins; i++) {
305 		per_pin = get_pin(spec, i);
306 		if (per_pin->pcm_idx == pcm_idx)
307 			return per_pin;
308 	}
309 	return NULL;
310 }
311 
312 static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
313 {
314 	struct hdmi_spec *spec = codec->spec;
315 	int cvt_idx;
316 
317 	for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
318 		if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
319 			return cvt_idx;
320 
321 	codec_warn(codec, "HDMI: cvt NID 0x%x not registered\n", cvt_nid);
322 	return -EINVAL;
323 }
324 
325 static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
326 			struct snd_ctl_elem_info *uinfo)
327 {
328 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
329 	struct hdmi_spec *spec = codec->spec;
330 	struct hdmi_spec_per_pin *per_pin;
331 	struct hdmi_eld *eld;
332 	int pcm_idx;
333 
334 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
335 
336 	pcm_idx = kcontrol->private_value;
337 	mutex_lock(&spec->pcm_lock);
338 	per_pin = pcm_idx_to_pin(spec, pcm_idx);
339 	if (!per_pin) {
340 		/* no pin is bound to the pcm */
341 		uinfo->count = 0;
342 		goto unlock;
343 	}
344 	eld = &per_pin->sink_eld;
345 	uinfo->count = eld->eld_valid ? eld->eld_size : 0;
346 
347  unlock:
348 	mutex_unlock(&spec->pcm_lock);
349 	return 0;
350 }
351 
352 static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
353 			struct snd_ctl_elem_value *ucontrol)
354 {
355 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
356 	struct hdmi_spec *spec = codec->spec;
357 	struct hdmi_spec_per_pin *per_pin;
358 	struct hdmi_eld *eld;
359 	int pcm_idx;
360 	int err = 0;
361 
362 	pcm_idx = kcontrol->private_value;
363 	mutex_lock(&spec->pcm_lock);
364 	per_pin = pcm_idx_to_pin(spec, pcm_idx);
365 	if (!per_pin) {
366 		/* no pin is bound to the pcm */
367 		memset(ucontrol->value.bytes.data, 0,
368 		       ARRAY_SIZE(ucontrol->value.bytes.data));
369 		goto unlock;
370 	}
371 
372 	eld = &per_pin->sink_eld;
373 	if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
374 	    eld->eld_size > ELD_MAX_SIZE) {
375 		snd_BUG();
376 		err = -EINVAL;
377 		goto unlock;
378 	}
379 
380 	memset(ucontrol->value.bytes.data, 0,
381 	       ARRAY_SIZE(ucontrol->value.bytes.data));
382 	if (eld->eld_valid)
383 		memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
384 		       eld->eld_size);
385 
386  unlock:
387 	mutex_unlock(&spec->pcm_lock);
388 	return err;
389 }
390 
391 static const struct snd_kcontrol_new eld_bytes_ctl = {
392 	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE |
393 		SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK,
394 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
395 	.name = "ELD",
396 	.info = hdmi_eld_ctl_info,
397 	.get = hdmi_eld_ctl_get,
398 };
399 
400 static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
401 			int device)
402 {
403 	struct snd_kcontrol *kctl;
404 	struct hdmi_spec *spec = codec->spec;
405 	int err;
406 
407 	kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
408 	if (!kctl)
409 		return -ENOMEM;
410 	kctl->private_value = pcm_idx;
411 	kctl->id.device = device;
412 
413 	/* no pin nid is associated with the kctl now
414 	 * tbd: associate pin nid to eld ctl later
415 	 */
416 	err = snd_hda_ctl_add(codec, 0, kctl);
417 	if (err < 0)
418 		return err;
419 
420 	get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
421 	return 0;
422 }
423 
424 #ifdef BE_PARANOID
425 static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
426 				int *packet_index, int *byte_index)
427 {
428 	int val;
429 
430 	val = snd_hda_codec_read(codec, pin_nid, 0,
431 				 AC_VERB_GET_HDMI_DIP_INDEX, 0);
432 
433 	*packet_index = val >> 5;
434 	*byte_index = val & 0x1f;
435 }
436 #endif
437 
438 static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
439 				int packet_index, int byte_index)
440 {
441 	int val;
442 
443 	val = (packet_index << 5) | (byte_index & 0x1f);
444 
445 	snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
446 }
447 
448 static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
449 				unsigned char val)
450 {
451 	snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
452 }
453 
454 static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
455 {
456 	struct hdmi_spec *spec = codec->spec;
457 	int pin_out;
458 
459 	/* Unmute */
460 	if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
461 		snd_hda_codec_write(codec, pin_nid, 0,
462 				AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
463 
464 	if (spec->dyn_pin_out)
465 		/* Disable pin out until stream is active */
466 		pin_out = 0;
467 	else
468 		/* Enable pin out: some machines with GM965 gets broken output
469 		 * when the pin is disabled or changed while using with HDMI
470 		 */
471 		pin_out = PIN_OUT;
472 
473 	snd_hda_codec_write(codec, pin_nid, 0,
474 			    AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
475 }
476 
477 /*
478  * ELD proc files
479  */
480 
481 #ifdef CONFIG_SND_PROC_FS
482 static void print_eld_info(struct snd_info_entry *entry,
483 			   struct snd_info_buffer *buffer)
484 {
485 	struct hdmi_spec_per_pin *per_pin = entry->private_data;
486 
487 	mutex_lock(&per_pin->lock);
488 	snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
489 	mutex_unlock(&per_pin->lock);
490 }
491 
492 static void write_eld_info(struct snd_info_entry *entry,
493 			   struct snd_info_buffer *buffer)
494 {
495 	struct hdmi_spec_per_pin *per_pin = entry->private_data;
496 
497 	mutex_lock(&per_pin->lock);
498 	snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
499 	mutex_unlock(&per_pin->lock);
500 }
501 
502 static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
503 {
504 	char name[32];
505 	struct hda_codec *codec = per_pin->codec;
506 	struct snd_info_entry *entry;
507 	int err;
508 
509 	snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
510 	err = snd_card_proc_new(codec->card, name, &entry);
511 	if (err < 0)
512 		return err;
513 
514 	snd_info_set_text_ops(entry, per_pin, print_eld_info);
515 	entry->c.text.write = write_eld_info;
516 	entry->mode |= 0200;
517 	per_pin->proc_entry = entry;
518 
519 	return 0;
520 }
521 
522 static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
523 {
524 	if (!per_pin->codec->bus->shutdown) {
525 		snd_info_free_entry(per_pin->proc_entry);
526 		per_pin->proc_entry = NULL;
527 	}
528 }
529 #else
530 static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
531 			       int index)
532 {
533 	return 0;
534 }
535 static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
536 {
537 }
538 #endif
539 
540 /*
541  * Audio InfoFrame routines
542  */
543 
544 /*
545  * Enable Audio InfoFrame Transmission
546  */
547 static void hdmi_start_infoframe_trans(struct hda_codec *codec,
548 				       hda_nid_t pin_nid)
549 {
550 	hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
551 	snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
552 						AC_DIPXMIT_BEST);
553 }
554 
555 /*
556  * Disable Audio InfoFrame Transmission
557  */
558 static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
559 				      hda_nid_t pin_nid)
560 {
561 	hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
562 	snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
563 						AC_DIPXMIT_DISABLE);
564 }
565 
566 static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
567 {
568 #ifdef CONFIG_SND_DEBUG_VERBOSE
569 	int i;
570 	int size;
571 
572 	size = snd_hdmi_get_eld_size(codec, pin_nid);
573 	codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
574 
575 	for (i = 0; i < 8; i++) {
576 		size = snd_hda_codec_read(codec, pin_nid, 0,
577 						AC_VERB_GET_HDMI_DIP_SIZE, i);
578 		codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
579 	}
580 #endif
581 }
582 
583 static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
584 {
585 #ifdef BE_PARANOID
586 	int i, j;
587 	int size;
588 	int pi, bi;
589 	for (i = 0; i < 8; i++) {
590 		size = snd_hda_codec_read(codec, pin_nid, 0,
591 						AC_VERB_GET_HDMI_DIP_SIZE, i);
592 		if (size == 0)
593 			continue;
594 
595 		hdmi_set_dip_index(codec, pin_nid, i, 0x0);
596 		for (j = 1; j < 1000; j++) {
597 			hdmi_write_dip_byte(codec, pin_nid, 0x0);
598 			hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
599 			if (pi != i)
600 				codec_dbg(codec, "dip index %d: %d != %d\n",
601 						bi, pi, i);
602 			if (bi == 0) /* byte index wrapped around */
603 				break;
604 		}
605 		codec_dbg(codec,
606 			"HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
607 			i, size, j);
608 	}
609 #endif
610 }
611 
612 static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
613 {
614 	u8 *bytes = (u8 *)hdmi_ai;
615 	u8 sum = 0;
616 	int i;
617 
618 	hdmi_ai->checksum = 0;
619 
620 	for (i = 0; i < sizeof(*hdmi_ai); i++)
621 		sum += bytes[i];
622 
623 	hdmi_ai->checksum = -sum;
624 }
625 
626 static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
627 				      hda_nid_t pin_nid,
628 				      u8 *dip, int size)
629 {
630 	int i;
631 
632 	hdmi_debug_dip_size(codec, pin_nid);
633 	hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
634 
635 	hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
636 	for (i = 0; i < size; i++)
637 		hdmi_write_dip_byte(codec, pin_nid, dip[i]);
638 }
639 
640 static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
641 				    u8 *dip, int size)
642 {
643 	u8 val;
644 	int i;
645 
646 	hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
647 	if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
648 							    != AC_DIPXMIT_BEST)
649 		return false;
650 
651 	for (i = 0; i < size; i++) {
652 		val = snd_hda_codec_read(codec, pin_nid, 0,
653 					 AC_VERB_GET_HDMI_DIP_DATA, 0);
654 		if (val != dip[i])
655 			return false;
656 	}
657 
658 	return true;
659 }
660 
661 static int hdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
662 			    int dev_id, unsigned char *buf, int *eld_size)
663 {
664 	snd_hda_set_dev_select(codec, nid, dev_id);
665 
666 	return snd_hdmi_get_eld(codec, nid, buf, eld_size);
667 }
668 
669 static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
670 				     hda_nid_t pin_nid, int dev_id,
671 				     int ca, int active_channels,
672 				     int conn_type)
673 {
674 	union audio_infoframe ai;
675 
676 	memset(&ai, 0, sizeof(ai));
677 	if (conn_type == 0) { /* HDMI */
678 		struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
679 
680 		hdmi_ai->type		= 0x84;
681 		hdmi_ai->ver		= 0x01;
682 		hdmi_ai->len		= 0x0a;
683 		hdmi_ai->CC02_CT47	= active_channels - 1;
684 		hdmi_ai->CA		= ca;
685 		hdmi_checksum_audio_infoframe(hdmi_ai);
686 	} else if (conn_type == 1) { /* DisplayPort */
687 		struct dp_audio_infoframe *dp_ai = &ai.dp;
688 
689 		dp_ai->type		= 0x84;
690 		dp_ai->len		= 0x1b;
691 		dp_ai->ver		= 0x11 << 2;
692 		dp_ai->CC02_CT47	= active_channels - 1;
693 		dp_ai->CA		= ca;
694 	} else {
695 		codec_dbg(codec, "HDMI: unknown connection type at pin NID 0x%x\n", pin_nid);
696 		return;
697 	}
698 
699 	snd_hda_set_dev_select(codec, pin_nid, dev_id);
700 
701 	/*
702 	 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
703 	 * sizeof(*dp_ai) to avoid partial match/update problems when
704 	 * the user switches between HDMI/DP monitors.
705 	 */
706 	if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
707 					sizeof(ai))) {
708 		codec_dbg(codec, "%s: pin NID=0x%x channels=%d ca=0x%02x\n",
709 			  __func__, pin_nid, active_channels, ca);
710 		hdmi_stop_infoframe_trans(codec, pin_nid);
711 		hdmi_fill_audio_infoframe(codec, pin_nid,
712 					    ai.bytes, sizeof(ai));
713 		hdmi_start_infoframe_trans(codec, pin_nid);
714 	}
715 }
716 
717 static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
718 				       struct hdmi_spec_per_pin *per_pin,
719 				       bool non_pcm)
720 {
721 	struct hdmi_spec *spec = codec->spec;
722 	struct hdac_chmap *chmap = &spec->chmap;
723 	hda_nid_t pin_nid = per_pin->pin_nid;
724 	int dev_id = per_pin->dev_id;
725 	int channels = per_pin->channels;
726 	int active_channels;
727 	struct hdmi_eld *eld;
728 	int ca;
729 
730 	if (!channels)
731 		return;
732 
733 	snd_hda_set_dev_select(codec, pin_nid, dev_id);
734 
735 	/* some HW (e.g. HSW+) needs reprogramming the amp at each time */
736 	if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
737 		snd_hda_codec_write(codec, pin_nid, 0,
738 					    AC_VERB_SET_AMP_GAIN_MUTE,
739 					    AMP_OUT_UNMUTE);
740 
741 	eld = &per_pin->sink_eld;
742 
743 	ca = snd_hdac_channel_allocation(&codec->core,
744 			eld->info.spk_alloc, channels,
745 			per_pin->chmap_set, non_pcm, per_pin->chmap);
746 
747 	active_channels = snd_hdac_get_active_channels(ca);
748 
749 	chmap->ops.set_channel_count(&codec->core, per_pin->cvt_nid,
750 						active_channels);
751 
752 	/*
753 	 * always configure channel mapping, it may have been changed by the
754 	 * user in the meantime
755 	 */
756 	snd_hdac_setup_channel_mapping(&spec->chmap,
757 				pin_nid, non_pcm, ca, channels,
758 				per_pin->chmap, per_pin->chmap_set);
759 
760 	spec->ops.pin_setup_infoframe(codec, pin_nid, dev_id,
761 				      ca, active_channels, eld->info.conn_type);
762 
763 	per_pin->non_pcm = non_pcm;
764 }
765 
766 /*
767  * Unsolicited events
768  */
769 
770 static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
771 
772 static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid,
773 				      int dev_id)
774 {
775 	struct hdmi_spec *spec = codec->spec;
776 	int pin_idx = pin_id_to_pin_index(codec, nid, dev_id);
777 
778 	if (pin_idx < 0)
779 		return;
780 	mutex_lock(&spec->pcm_lock);
781 	hdmi_present_sense(get_pin(spec, pin_idx), 1);
782 	mutex_unlock(&spec->pcm_lock);
783 }
784 
785 static void jack_callback(struct hda_codec *codec,
786 			  struct hda_jack_callback *jack)
787 {
788 	/* stop polling when notification is enabled */
789 	if (codec_has_acomp(codec))
790 		return;
791 
792 	check_presence_and_report(codec, jack->nid, jack->dev_id);
793 }
794 
795 static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res,
796 				 struct hda_jack_tbl *jack)
797 {
798 	jack->jack_dirty = 1;
799 
800 	codec_dbg(codec,
801 		"HDMI hot plug event: Codec=%d NID=0x%x Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
802 		codec->addr, jack->nid, jack->dev_id, !!(res & AC_UNSOL_RES_IA),
803 		!!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
804 
805 	check_presence_and_report(codec, jack->nid, jack->dev_id);
806 }
807 
808 static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
809 {
810 	int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
811 	int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
812 	int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
813 	int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
814 
815 	codec_info(codec,
816 		"HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
817 		codec->addr,
818 		tag,
819 		subtag,
820 		cp_state,
821 		cp_ready);
822 
823 	/* TODO */
824 	if (cp_state) {
825 		;
826 	}
827 	if (cp_ready) {
828 		;
829 	}
830 }
831 
832 
833 static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
834 {
835 	int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
836 	int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
837 	struct hda_jack_tbl *jack;
838 
839 	if (codec_has_acomp(codec))
840 		return;
841 
842 	if (codec->dp_mst) {
843 		int dev_entry =
844 			(res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
845 
846 		jack = snd_hda_jack_tbl_get_from_tag(codec, tag, dev_entry);
847 	} else {
848 		jack = snd_hda_jack_tbl_get_from_tag(codec, tag, 0);
849 	}
850 
851 	if (!jack) {
852 		codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
853 		return;
854 	}
855 
856 	if (subtag == 0)
857 		hdmi_intrinsic_event(codec, res, jack);
858 	else
859 		hdmi_non_intrinsic_event(codec, res);
860 }
861 
862 static void haswell_verify_D0(struct hda_codec *codec,
863 		hda_nid_t cvt_nid, hda_nid_t nid)
864 {
865 	int pwr;
866 
867 	/* For Haswell, the converter 1/2 may keep in D3 state after bootup,
868 	 * thus pins could only choose converter 0 for use. Make sure the
869 	 * converters are in correct power state */
870 	if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
871 		snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
872 
873 	if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
874 		snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
875 				    AC_PWRST_D0);
876 		msleep(40);
877 		pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
878 		pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
879 		codec_dbg(codec, "Haswell HDMI audio: Power for NID 0x%x is now D%d\n", nid, pwr);
880 	}
881 }
882 
883 /*
884  * Callbacks
885  */
886 
887 /* HBR should be Non-PCM, 8 channels */
888 #define is_hbr_format(format) \
889 	((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
890 
891 static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
892 			      int dev_id, bool hbr)
893 {
894 	int pinctl, new_pinctl;
895 
896 	if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
897 		snd_hda_set_dev_select(codec, pin_nid, dev_id);
898 		pinctl = snd_hda_codec_read(codec, pin_nid, 0,
899 					    AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
900 
901 		if (pinctl < 0)
902 			return hbr ? -EINVAL : 0;
903 
904 		new_pinctl = pinctl & ~AC_PINCTL_EPT;
905 		if (hbr)
906 			new_pinctl |= AC_PINCTL_EPT_HBR;
907 		else
908 			new_pinctl |= AC_PINCTL_EPT_NATIVE;
909 
910 		codec_dbg(codec,
911 			  "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
912 			    pin_nid,
913 			    pinctl == new_pinctl ? "" : "new-",
914 			    new_pinctl);
915 
916 		if (pinctl != new_pinctl)
917 			snd_hda_codec_write(codec, pin_nid, 0,
918 					    AC_VERB_SET_PIN_WIDGET_CONTROL,
919 					    new_pinctl);
920 	} else if (hbr)
921 		return -EINVAL;
922 
923 	return 0;
924 }
925 
926 static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
927 			      hda_nid_t pin_nid, int dev_id,
928 			      u32 stream_tag, int format)
929 {
930 	struct hdmi_spec *spec = codec->spec;
931 	unsigned int param;
932 	int err;
933 
934 	err = spec->ops.pin_hbr_setup(codec, pin_nid, dev_id,
935 				      is_hbr_format(format));
936 
937 	if (err) {
938 		codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
939 		return err;
940 	}
941 
942 	if (spec->intel_hsw_fixup) {
943 
944 		/*
945 		 * on recent platforms IEC Coding Type is required for HBR
946 		 * support, read current Digital Converter settings and set
947 		 * ICT bitfield if needed.
948 		 */
949 		param = snd_hda_codec_read(codec, cvt_nid, 0,
950 					   AC_VERB_GET_DIGI_CONVERT_1, 0);
951 
952 		param = (param >> 16) & ~(AC_DIG3_ICT);
953 
954 		/* on recent platforms ICT mode is required for HBR support */
955 		if (is_hbr_format(format))
956 			param |= 0x1;
957 
958 		snd_hda_codec_write(codec, cvt_nid, 0,
959 				    AC_VERB_SET_DIGI_CONVERT_3, param);
960 	}
961 
962 	snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
963 	return 0;
964 }
965 
966 /* Try to find an available converter
967  * If pin_idx is less then zero, just try to find an available converter.
968  * Otherwise, try to find an available converter and get the cvt mux index
969  * of the pin.
970  */
971 static int hdmi_choose_cvt(struct hda_codec *codec,
972 			   int pin_idx, int *cvt_id)
973 {
974 	struct hdmi_spec *spec = codec->spec;
975 	struct hdmi_spec_per_pin *per_pin;
976 	struct hdmi_spec_per_cvt *per_cvt = NULL;
977 	int cvt_idx, mux_idx = 0;
978 
979 	/* pin_idx < 0 means no pin will be bound to the converter */
980 	if (pin_idx < 0)
981 		per_pin = NULL;
982 	else
983 		per_pin = get_pin(spec, pin_idx);
984 
985 	if (per_pin && per_pin->silent_stream) {
986 		cvt_idx = cvt_nid_to_cvt_index(codec, per_pin->cvt_nid);
987 		if (cvt_id)
988 			*cvt_id = cvt_idx;
989 		return 0;
990 	}
991 
992 	/* Dynamically assign converter to stream */
993 	for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
994 		per_cvt = get_cvt(spec, cvt_idx);
995 
996 		/* Must not already be assigned */
997 		if (per_cvt->assigned)
998 			continue;
999 		if (per_pin == NULL)
1000 			break;
1001 		/* Must be in pin's mux's list of converters */
1002 		for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1003 			if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1004 				break;
1005 		/* Not in mux list */
1006 		if (mux_idx == per_pin->num_mux_nids)
1007 			continue;
1008 		break;
1009 	}
1010 
1011 	/* No free converters */
1012 	if (cvt_idx == spec->num_cvts)
1013 		return -EBUSY;
1014 
1015 	if (per_pin != NULL)
1016 		per_pin->mux_idx = mux_idx;
1017 
1018 	if (cvt_id)
1019 		*cvt_id = cvt_idx;
1020 
1021 	return 0;
1022 }
1023 
1024 /* Assure the pin select the right convetor */
1025 static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
1026 			struct hdmi_spec_per_pin *per_pin)
1027 {
1028 	hda_nid_t pin_nid = per_pin->pin_nid;
1029 	int mux_idx, curr;
1030 
1031 	mux_idx = per_pin->mux_idx;
1032 	curr = snd_hda_codec_read(codec, pin_nid, 0,
1033 					  AC_VERB_GET_CONNECT_SEL, 0);
1034 	if (curr != mux_idx)
1035 		snd_hda_codec_write_cache(codec, pin_nid, 0,
1036 					    AC_VERB_SET_CONNECT_SEL,
1037 					    mux_idx);
1038 }
1039 
1040 /* get the mux index for the converter of the pins
1041  * converter's mux index is the same for all pins on Intel platform
1042  */
1043 static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
1044 			hda_nid_t cvt_nid)
1045 {
1046 	int i;
1047 
1048 	for (i = 0; i < spec->num_cvts; i++)
1049 		if (spec->cvt_nids[i] == cvt_nid)
1050 			return i;
1051 	return -EINVAL;
1052 }
1053 
1054 /* Intel HDMI workaround to fix audio routing issue:
1055  * For some Intel display codecs, pins share the same connection list.
1056  * So a conveter can be selected by multiple pins and playback on any of these
1057  * pins will generate sound on the external display, because audio flows from
1058  * the same converter to the display pipeline. Also muting one pin may make
1059  * other pins have no sound output.
1060  * So this function assures that an assigned converter for a pin is not selected
1061  * by any other pins.
1062  */
1063 static void intel_not_share_assigned_cvt(struct hda_codec *codec,
1064 					 hda_nid_t pin_nid,
1065 					 int dev_id, int mux_idx)
1066 {
1067 	struct hdmi_spec *spec = codec->spec;
1068 	hda_nid_t nid;
1069 	int cvt_idx, curr;
1070 	struct hdmi_spec_per_cvt *per_cvt;
1071 	struct hdmi_spec_per_pin *per_pin;
1072 	int pin_idx;
1073 
1074 	/* configure the pins connections */
1075 	for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1076 		int dev_id_saved;
1077 		int dev_num;
1078 
1079 		per_pin = get_pin(spec, pin_idx);
1080 		/*
1081 		 * pin not connected to monitor
1082 		 * no need to operate on it
1083 		 */
1084 		if (!per_pin->pcm)
1085 			continue;
1086 
1087 		if ((per_pin->pin_nid == pin_nid) &&
1088 			(per_pin->dev_id == dev_id))
1089 			continue;
1090 
1091 		/*
1092 		 * if per_pin->dev_id >= dev_num,
1093 		 * snd_hda_get_dev_select() will fail,
1094 		 * and the following operation is unpredictable.
1095 		 * So skip this situation.
1096 		 */
1097 		dev_num = snd_hda_get_num_devices(codec, per_pin->pin_nid) + 1;
1098 		if (per_pin->dev_id >= dev_num)
1099 			continue;
1100 
1101 		nid = per_pin->pin_nid;
1102 
1103 		/*
1104 		 * Calling this function should not impact
1105 		 * on the device entry selection
1106 		 * So let's save the dev id for each pin,
1107 		 * and restore it when return
1108 		 */
1109 		dev_id_saved = snd_hda_get_dev_select(codec, nid);
1110 		snd_hda_set_dev_select(codec, nid, per_pin->dev_id);
1111 		curr = snd_hda_codec_read(codec, nid, 0,
1112 					  AC_VERB_GET_CONNECT_SEL, 0);
1113 		if (curr != mux_idx) {
1114 			snd_hda_set_dev_select(codec, nid, dev_id_saved);
1115 			continue;
1116 		}
1117 
1118 
1119 		/* choose an unassigned converter. The conveters in the
1120 		 * connection list are in the same order as in the codec.
1121 		 */
1122 		for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1123 			per_cvt = get_cvt(spec, cvt_idx);
1124 			if (!per_cvt->assigned) {
1125 				codec_dbg(codec,
1126 					  "choose cvt %d for pin NID 0x%x\n",
1127 					  cvt_idx, nid);
1128 				snd_hda_codec_write_cache(codec, nid, 0,
1129 					    AC_VERB_SET_CONNECT_SEL,
1130 					    cvt_idx);
1131 				break;
1132 			}
1133 		}
1134 		snd_hda_set_dev_select(codec, nid, dev_id_saved);
1135 	}
1136 }
1137 
1138 /* A wrapper of intel_not_share_asigned_cvt() */
1139 static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1140 			hda_nid_t pin_nid, int dev_id, hda_nid_t cvt_nid)
1141 {
1142 	int mux_idx;
1143 	struct hdmi_spec *spec = codec->spec;
1144 
1145 	/* On Intel platform, the mapping of converter nid to
1146 	 * mux index of the pins are always the same.
1147 	 * The pin nid may be 0, this means all pins will not
1148 	 * share the converter.
1149 	 */
1150 	mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1151 	if (mux_idx >= 0)
1152 		intel_not_share_assigned_cvt(codec, pin_nid, dev_id, mux_idx);
1153 }
1154 
1155 /* skeleton caller of pin_cvt_fixup ops */
1156 static void pin_cvt_fixup(struct hda_codec *codec,
1157 			  struct hdmi_spec_per_pin *per_pin,
1158 			  hda_nid_t cvt_nid)
1159 {
1160 	struct hdmi_spec *spec = codec->spec;
1161 
1162 	if (spec->ops.pin_cvt_fixup)
1163 		spec->ops.pin_cvt_fixup(codec, per_pin, cvt_nid);
1164 }
1165 
1166 /* called in hdmi_pcm_open when no pin is assigned to the PCM
1167  * in dyn_pcm_assign mode.
1168  */
1169 static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1170 			 struct hda_codec *codec,
1171 			 struct snd_pcm_substream *substream)
1172 {
1173 	struct hdmi_spec *spec = codec->spec;
1174 	struct snd_pcm_runtime *runtime = substream->runtime;
1175 	int cvt_idx, pcm_idx;
1176 	struct hdmi_spec_per_cvt *per_cvt = NULL;
1177 	int err;
1178 
1179 	pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1180 	if (pcm_idx < 0)
1181 		return -EINVAL;
1182 
1183 	err = hdmi_choose_cvt(codec, -1, &cvt_idx);
1184 	if (err)
1185 		return err;
1186 
1187 	per_cvt = get_cvt(spec, cvt_idx);
1188 	per_cvt->assigned = 1;
1189 	hinfo->nid = per_cvt->cvt_nid;
1190 
1191 	pin_cvt_fixup(codec, NULL, per_cvt->cvt_nid);
1192 
1193 	set_bit(pcm_idx, &spec->pcm_in_use);
1194 	/* todo: setup spdif ctls assign */
1195 
1196 	/* Initially set the converter's capabilities */
1197 	hinfo->channels_min = per_cvt->channels_min;
1198 	hinfo->channels_max = per_cvt->channels_max;
1199 	hinfo->rates = per_cvt->rates;
1200 	hinfo->formats = per_cvt->formats;
1201 	hinfo->maxbps = per_cvt->maxbps;
1202 
1203 	/* Store the updated parameters */
1204 	runtime->hw.channels_min = hinfo->channels_min;
1205 	runtime->hw.channels_max = hinfo->channels_max;
1206 	runtime->hw.formats = hinfo->formats;
1207 	runtime->hw.rates = hinfo->rates;
1208 
1209 	snd_pcm_hw_constraint_step(substream->runtime, 0,
1210 				   SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1211 	return 0;
1212 }
1213 
1214 /*
1215  * HDA PCM callbacks
1216  */
1217 static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1218 			 struct hda_codec *codec,
1219 			 struct snd_pcm_substream *substream)
1220 {
1221 	struct hdmi_spec *spec = codec->spec;
1222 	struct snd_pcm_runtime *runtime = substream->runtime;
1223 	int pin_idx, cvt_idx, pcm_idx;
1224 	struct hdmi_spec_per_pin *per_pin;
1225 	struct hdmi_eld *eld;
1226 	struct hdmi_spec_per_cvt *per_cvt = NULL;
1227 	int err;
1228 
1229 	/* Validate hinfo */
1230 	pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1231 	if (pcm_idx < 0)
1232 		return -EINVAL;
1233 
1234 	mutex_lock(&spec->pcm_lock);
1235 	pin_idx = hinfo_to_pin_index(codec, hinfo);
1236 	if (!spec->dyn_pcm_assign) {
1237 		if (snd_BUG_ON(pin_idx < 0)) {
1238 			err = -EINVAL;
1239 			goto unlock;
1240 		}
1241 	} else {
1242 		/* no pin is assigned to the PCM
1243 		 * PA need pcm open successfully when probe
1244 		 */
1245 		if (pin_idx < 0) {
1246 			err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1247 			goto unlock;
1248 		}
1249 	}
1250 
1251 	err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx);
1252 	if (err < 0)
1253 		goto unlock;
1254 
1255 	per_cvt = get_cvt(spec, cvt_idx);
1256 	/* Claim converter */
1257 	per_cvt->assigned = 1;
1258 
1259 	set_bit(pcm_idx, &spec->pcm_in_use);
1260 	per_pin = get_pin(spec, pin_idx);
1261 	per_pin->cvt_nid = per_cvt->cvt_nid;
1262 	hinfo->nid = per_cvt->cvt_nid;
1263 
1264 	/* flip stripe flag for the assigned stream if supported */
1265 	if (get_wcaps(codec, per_cvt->cvt_nid) & AC_WCAP_STRIPE)
1266 		azx_stream(get_azx_dev(substream))->stripe = 1;
1267 
1268 	snd_hda_set_dev_select(codec, per_pin->pin_nid, per_pin->dev_id);
1269 	snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1270 			    AC_VERB_SET_CONNECT_SEL,
1271 			    per_pin->mux_idx);
1272 
1273 	/* configure unused pins to choose other converters */
1274 	pin_cvt_fixup(codec, per_pin, 0);
1275 
1276 	snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
1277 
1278 	/* Initially set the converter's capabilities */
1279 	hinfo->channels_min = per_cvt->channels_min;
1280 	hinfo->channels_max = per_cvt->channels_max;
1281 	hinfo->rates = per_cvt->rates;
1282 	hinfo->formats = per_cvt->formats;
1283 	hinfo->maxbps = per_cvt->maxbps;
1284 
1285 	eld = &per_pin->sink_eld;
1286 	/* Restrict capabilities by ELD if this isn't disabled */
1287 	if (!static_hdmi_pcm && eld->eld_valid) {
1288 		snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
1289 		if (hinfo->channels_min > hinfo->channels_max ||
1290 		    !hinfo->rates || !hinfo->formats) {
1291 			per_cvt->assigned = 0;
1292 			hinfo->nid = 0;
1293 			snd_hda_spdif_ctls_unassign(codec, pcm_idx);
1294 			err = -ENODEV;
1295 			goto unlock;
1296 		}
1297 	}
1298 
1299 	/* Store the updated parameters */
1300 	runtime->hw.channels_min = hinfo->channels_min;
1301 	runtime->hw.channels_max = hinfo->channels_max;
1302 	runtime->hw.formats = hinfo->formats;
1303 	runtime->hw.rates = hinfo->rates;
1304 
1305 	snd_pcm_hw_constraint_step(substream->runtime, 0,
1306 				   SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1307  unlock:
1308 	mutex_unlock(&spec->pcm_lock);
1309 	return err;
1310 }
1311 
1312 /*
1313  * HDA/HDMI auto parsing
1314  */
1315 static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
1316 {
1317 	struct hdmi_spec *spec = codec->spec;
1318 	struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1319 	hda_nid_t pin_nid = per_pin->pin_nid;
1320 	int dev_id = per_pin->dev_id;
1321 	int conns;
1322 
1323 	if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1324 		codec_warn(codec,
1325 			   "HDMI: pin NID 0x%x wcaps %#x does not support connection list\n",
1326 			   pin_nid, get_wcaps(codec, pin_nid));
1327 		return -EINVAL;
1328 	}
1329 
1330 	snd_hda_set_dev_select(codec, pin_nid, dev_id);
1331 
1332 	if (spec->intel_hsw_fixup) {
1333 		conns = spec->num_cvts;
1334 		memcpy(per_pin->mux_nids, spec->cvt_nids,
1335 		       sizeof(hda_nid_t) * conns);
1336 	} else {
1337 		conns = snd_hda_get_raw_connections(codec, pin_nid,
1338 						    per_pin->mux_nids,
1339 						    HDA_MAX_CONNECTIONS);
1340 	}
1341 
1342 	/* all the device entries on the same pin have the same conn list */
1343 	per_pin->num_mux_nids = conns;
1344 
1345 	return 0;
1346 }
1347 
1348 static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1349 			      struct hdmi_spec_per_pin *per_pin)
1350 {
1351 	int i;
1352 
1353 	/* on the new machines, try to assign the pcm slot dynamically,
1354 	 * not use the preferred fixed map (legacy way) anymore.
1355 	 */
1356 	if (spec->dyn_pcm_no_legacy)
1357 		goto last_try;
1358 
1359 	/*
1360 	 * generic_hdmi_build_pcms() may allocate extra PCMs on some
1361 	 * platforms (with maximum of 'num_nids + dev_num - 1')
1362 	 *
1363 	 * The per_pin of pin_nid_idx=n and dev_id=m prefers to get pcm-n
1364 	 * if m==0. This guarantees that dynamic pcm assignments are compatible
1365 	 * with the legacy static per_pin-pcm assignment that existed in the
1366 	 * days before DP-MST.
1367 	 *
1368 	 * Intel DP-MST prefers this legacy behavior for compatibility, too.
1369 	 *
1370 	 * per_pin of m!=0 prefers to get pcm=(num_nids + (m - 1)).
1371 	 */
1372 
1373 	if (per_pin->dev_id == 0 || spec->intel_hsw_fixup) {
1374 		if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1375 			return per_pin->pin_nid_idx;
1376 	} else {
1377 		i = spec->num_nids + (per_pin->dev_id - 1);
1378 		if (i < spec->pcm_used && !(test_bit(i, &spec->pcm_bitmap)))
1379 			return i;
1380 	}
1381 
1382 	/* have a second try; check the area over num_nids */
1383 	for (i = spec->num_nids; i < spec->pcm_used; i++) {
1384 		if (!test_bit(i, &spec->pcm_bitmap))
1385 			return i;
1386 	}
1387 
1388  last_try:
1389 	/* the last try; check the empty slots in pins */
1390 	for (i = 0; i < spec->num_nids; i++) {
1391 		if (!test_bit(i, &spec->pcm_bitmap))
1392 			return i;
1393 	}
1394 	return -EBUSY;
1395 }
1396 
1397 static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1398 				struct hdmi_spec_per_pin *per_pin)
1399 {
1400 	int idx;
1401 
1402 	/* pcm already be attached to the pin */
1403 	if (per_pin->pcm)
1404 		return;
1405 	idx = hdmi_find_pcm_slot(spec, per_pin);
1406 	if (idx == -EBUSY)
1407 		return;
1408 	per_pin->pcm_idx = idx;
1409 	per_pin->pcm = get_hdmi_pcm(spec, idx);
1410 	set_bit(idx, &spec->pcm_bitmap);
1411 }
1412 
1413 static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1414 				struct hdmi_spec_per_pin *per_pin)
1415 {
1416 	int idx;
1417 
1418 	/* pcm already be detached from the pin */
1419 	if (!per_pin->pcm)
1420 		return;
1421 	idx = per_pin->pcm_idx;
1422 	per_pin->pcm_idx = -1;
1423 	per_pin->pcm = NULL;
1424 	if (idx >= 0 && idx < spec->pcm_used)
1425 		clear_bit(idx, &spec->pcm_bitmap);
1426 }
1427 
1428 static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1429 		struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1430 {
1431 	int mux_idx;
1432 
1433 	for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1434 		if (per_pin->mux_nids[mux_idx] == cvt_nid)
1435 			break;
1436 	return mux_idx;
1437 }
1438 
1439 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1440 
1441 static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1442 			   struct hdmi_spec_per_pin *per_pin)
1443 {
1444 	struct hda_codec *codec = per_pin->codec;
1445 	struct hda_pcm *pcm;
1446 	struct hda_pcm_stream *hinfo;
1447 	struct snd_pcm_substream *substream;
1448 	int mux_idx;
1449 	bool non_pcm;
1450 
1451 	if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1452 		pcm = get_pcm_rec(spec, per_pin->pcm_idx);
1453 	else
1454 		return;
1455 	if (!pcm->pcm)
1456 		return;
1457 	if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1458 		return;
1459 
1460 	/* hdmi audio only uses playback and one substream */
1461 	hinfo = pcm->stream;
1462 	substream = pcm->pcm->streams[0].substream;
1463 
1464 	per_pin->cvt_nid = hinfo->nid;
1465 
1466 	mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1467 	if (mux_idx < per_pin->num_mux_nids) {
1468 		snd_hda_set_dev_select(codec, per_pin->pin_nid,
1469 				   per_pin->dev_id);
1470 		snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1471 				AC_VERB_SET_CONNECT_SEL,
1472 				mux_idx);
1473 	}
1474 	snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1475 
1476 	non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1477 	if (substream->runtime)
1478 		per_pin->channels = substream->runtime->channels;
1479 	per_pin->setup = true;
1480 	per_pin->mux_idx = mux_idx;
1481 
1482 	hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1483 }
1484 
1485 static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1486 			   struct hdmi_spec_per_pin *per_pin)
1487 {
1488 	if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1489 		snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1490 
1491 	per_pin->chmap_set = false;
1492 	memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1493 
1494 	per_pin->setup = false;
1495 	per_pin->channels = 0;
1496 }
1497 
1498 static struct snd_jack *pin_idx_to_pcm_jack(struct hda_codec *codec,
1499 					    struct hdmi_spec_per_pin *per_pin)
1500 {
1501 	struct hdmi_spec *spec = codec->spec;
1502 
1503 	if (per_pin->pcm_idx >= 0)
1504 		return spec->pcm_rec[per_pin->pcm_idx].jack;
1505 	else
1506 		return NULL;
1507 }
1508 
1509 /* update per_pin ELD from the given new ELD;
1510  * setup info frame and notification accordingly
1511  * also notify ELD kctl and report jack status changes
1512  */
1513 static void update_eld(struct hda_codec *codec,
1514 		       struct hdmi_spec_per_pin *per_pin,
1515 		       struct hdmi_eld *eld,
1516 		       int repoll)
1517 {
1518 	struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1519 	struct hdmi_spec *spec = codec->spec;
1520 	struct snd_jack *pcm_jack;
1521 	bool old_eld_valid = pin_eld->eld_valid;
1522 	bool eld_changed;
1523 	int pcm_idx;
1524 
1525 	if (eld->eld_valid) {
1526 		if (eld->eld_size <= 0 ||
1527 		    snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
1528 				       eld->eld_size) < 0) {
1529 			eld->eld_valid = false;
1530 			if (repoll) {
1531 				schedule_delayed_work(&per_pin->work,
1532 						      msecs_to_jiffies(300));
1533 				return;
1534 			}
1535 		}
1536 	}
1537 
1538 	if (!eld->eld_valid || eld->eld_size <= 0) {
1539 		eld->eld_valid = false;
1540 		eld->eld_size = 0;
1541 	}
1542 
1543 	/* for monitor disconnection, save pcm_idx firstly */
1544 	pcm_idx = per_pin->pcm_idx;
1545 
1546 	/*
1547 	 * pcm_idx >=0 before update_eld() means it is in monitor
1548 	 * disconnected event. Jack must be fetched before update_eld().
1549 	 */
1550 	pcm_jack = pin_idx_to_pcm_jack(codec, per_pin);
1551 
1552 	if (spec->dyn_pcm_assign) {
1553 		if (eld->eld_valid) {
1554 			hdmi_attach_hda_pcm(spec, per_pin);
1555 			hdmi_pcm_setup_pin(spec, per_pin);
1556 		} else {
1557 			hdmi_pcm_reset_pin(spec, per_pin);
1558 			hdmi_detach_hda_pcm(spec, per_pin);
1559 		}
1560 	}
1561 	/* if pcm_idx == -1, it means this is in monitor connection event
1562 	 * we can get the correct pcm_idx now.
1563 	 */
1564 	if (pcm_idx == -1)
1565 		pcm_idx = per_pin->pcm_idx;
1566 	if (!pcm_jack)
1567 		pcm_jack = pin_idx_to_pcm_jack(codec, per_pin);
1568 
1569 	if (eld->eld_valid)
1570 		snd_hdmi_show_eld(codec, &eld->info);
1571 
1572 	eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1573 	eld_changed |= (pin_eld->monitor_present != eld->monitor_present);
1574 	if (!eld_changed && eld->eld_valid && pin_eld->eld_valid)
1575 		if (pin_eld->eld_size != eld->eld_size ||
1576 		    memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1577 			   eld->eld_size) != 0)
1578 			eld_changed = true;
1579 
1580 	if (eld_changed) {
1581 		pin_eld->monitor_present = eld->monitor_present;
1582 		pin_eld->eld_valid = eld->eld_valid;
1583 		pin_eld->eld_size = eld->eld_size;
1584 		if (eld->eld_valid)
1585 			memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1586 			       eld->eld_size);
1587 		pin_eld->info = eld->info;
1588 	}
1589 
1590 	/*
1591 	 * Re-setup pin and infoframe. This is needed e.g. when
1592 	 * - sink is first plugged-in
1593 	 * - transcoder can change during stream playback on Haswell
1594 	 *   and this can make HW reset converter selection on a pin.
1595 	 */
1596 	if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1597 		pin_cvt_fixup(codec, per_pin, 0);
1598 		hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1599 	}
1600 
1601 	if (eld_changed && pcm_idx >= 0)
1602 		snd_ctl_notify(codec->card,
1603 			       SNDRV_CTL_EVENT_MASK_VALUE |
1604 			       SNDRV_CTL_EVENT_MASK_INFO,
1605 			       &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
1606 
1607 	if (eld_changed && pcm_jack)
1608 		snd_jack_report(pcm_jack,
1609 				(eld->monitor_present && eld->eld_valid) ?
1610 				SND_JACK_AVOUT : 0);
1611 }
1612 
1613 /* update ELD and jack state via HD-audio verbs */
1614 static void hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1615 					 int repoll)
1616 {
1617 	struct hda_codec *codec = per_pin->codec;
1618 	struct hdmi_spec *spec = codec->spec;
1619 	struct hdmi_eld *eld = &spec->temp_eld;
1620 	hda_nid_t pin_nid = per_pin->pin_nid;
1621 	int dev_id = per_pin->dev_id;
1622 	/*
1623 	 * Always execute a GetPinSense verb here, even when called from
1624 	 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1625 	 * response's PD bit is not the real PD value, but indicates that
1626 	 * the real PD value changed. An older version of the HD-audio
1627 	 * specification worked this way. Hence, we just ignore the data in
1628 	 * the unsolicited response to avoid custom WARs.
1629 	 */
1630 	int present;
1631 	int ret;
1632 
1633 	ret = snd_hda_power_up_pm(codec);
1634 	if (ret < 0 && pm_runtime_suspended(hda_codec_dev(codec)))
1635 		goto out;
1636 
1637 	present = snd_hda_jack_pin_sense(codec, pin_nid, dev_id);
1638 
1639 	mutex_lock(&per_pin->lock);
1640 	eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1641 	if (eld->monitor_present)
1642 		eld->eld_valid  = !!(present & AC_PINSENSE_ELDV);
1643 	else
1644 		eld->eld_valid = false;
1645 
1646 	codec_dbg(codec,
1647 		"HDMI status: Codec=%d NID=0x%x Presence_Detect=%d ELD_Valid=%d\n",
1648 		codec->addr, pin_nid, eld->monitor_present, eld->eld_valid);
1649 
1650 	if (eld->eld_valid) {
1651 		if (spec->ops.pin_get_eld(codec, pin_nid, dev_id,
1652 					  eld->eld_buffer, &eld->eld_size) < 0)
1653 			eld->eld_valid = false;
1654 	}
1655 
1656 	update_eld(codec, per_pin, eld, repoll);
1657 	mutex_unlock(&per_pin->lock);
1658  out:
1659 	snd_hda_power_down_pm(codec);
1660 }
1661 
1662 #define I915_SILENT_RATE		48000
1663 #define I915_SILENT_CHANNELS		2
1664 #define I915_SILENT_FORMAT		SNDRV_PCM_FORMAT_S16_LE
1665 #define I915_SILENT_FORMAT_BITS	16
1666 #define I915_SILENT_FMT_MASK		0xf
1667 
1668 static void silent_stream_enable(struct hda_codec *codec,
1669 				 struct hdmi_spec_per_pin *per_pin)
1670 {
1671 	struct hdmi_spec *spec = codec->spec;
1672 	struct hdmi_spec_per_cvt *per_cvt;
1673 	int cvt_idx, pin_idx, err;
1674 	unsigned int format;
1675 
1676 	mutex_lock(&per_pin->lock);
1677 
1678 	if (per_pin->setup) {
1679 		codec_dbg(codec, "hdmi: PCM already open, no silent stream\n");
1680 		goto unlock_out;
1681 	}
1682 
1683 	pin_idx = pin_id_to_pin_index(codec, per_pin->pin_nid, per_pin->dev_id);
1684 	err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx);
1685 	if (err) {
1686 		codec_err(codec, "hdmi: no free converter to enable silent mode\n");
1687 		goto unlock_out;
1688 	}
1689 
1690 	per_cvt = get_cvt(spec, cvt_idx);
1691 	per_cvt->assigned = 1;
1692 	per_pin->cvt_nid = per_cvt->cvt_nid;
1693 	per_pin->silent_stream = true;
1694 
1695 	codec_dbg(codec, "hdmi: enabling silent stream pin-NID=0x%x cvt-NID=0x%x\n",
1696 		  per_pin->pin_nid, per_cvt->cvt_nid);
1697 
1698 	snd_hda_set_dev_select(codec, per_pin->pin_nid, per_pin->dev_id);
1699 	snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1700 				  AC_VERB_SET_CONNECT_SEL,
1701 				  per_pin->mux_idx);
1702 
1703 	/* configure unused pins to choose other converters */
1704 	pin_cvt_fixup(codec, per_pin, 0);
1705 
1706 	snd_hdac_sync_audio_rate(&codec->core, per_pin->pin_nid,
1707 				 per_pin->dev_id, I915_SILENT_RATE);
1708 
1709 	/* trigger silent stream generation in hw */
1710 	format = snd_hdac_calc_stream_format(I915_SILENT_RATE, I915_SILENT_CHANNELS,
1711 					     I915_SILENT_FORMAT, I915_SILENT_FORMAT_BITS, 0);
1712 	snd_hda_codec_setup_stream(codec, per_pin->cvt_nid,
1713 				   I915_SILENT_FMT_MASK, I915_SILENT_FMT_MASK, format);
1714 	usleep_range(100, 200);
1715 	snd_hda_codec_setup_stream(codec, per_pin->cvt_nid, I915_SILENT_FMT_MASK, 0, format);
1716 
1717 	per_pin->channels = I915_SILENT_CHANNELS;
1718 	hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1719 
1720  unlock_out:
1721 	mutex_unlock(&per_pin->lock);
1722 }
1723 
1724 static void silent_stream_disable(struct hda_codec *codec,
1725 				  struct hdmi_spec_per_pin *per_pin)
1726 {
1727 	struct hdmi_spec *spec = codec->spec;
1728 	struct hdmi_spec_per_cvt *per_cvt;
1729 	int cvt_idx;
1730 
1731 	mutex_lock(&per_pin->lock);
1732 	if (!per_pin->silent_stream)
1733 		goto unlock_out;
1734 
1735 	codec_dbg(codec, "HDMI: disable silent stream on pin-NID=0x%x cvt-NID=0x%x\n",
1736 		  per_pin->pin_nid, per_pin->cvt_nid);
1737 
1738 	cvt_idx = cvt_nid_to_cvt_index(codec, per_pin->cvt_nid);
1739 	if (cvt_idx >= 0 && cvt_idx < spec->num_cvts) {
1740 		per_cvt = get_cvt(spec, cvt_idx);
1741 		per_cvt->assigned = 0;
1742 	}
1743 
1744 	per_pin->cvt_nid = 0;
1745 	per_pin->silent_stream = false;
1746 
1747  unlock_out:
1748 	mutex_unlock(&per_pin->lock);
1749 }
1750 
1751 /* update ELD and jack state via audio component */
1752 static void sync_eld_via_acomp(struct hda_codec *codec,
1753 			       struct hdmi_spec_per_pin *per_pin)
1754 {
1755 	struct hdmi_spec *spec = codec->spec;
1756 	struct hdmi_eld *eld = &spec->temp_eld;
1757 	bool monitor_prev, monitor_next;
1758 
1759 	mutex_lock(&per_pin->lock);
1760 	eld->monitor_present = false;
1761 	monitor_prev = per_pin->sink_eld.monitor_present;
1762 	eld->eld_size = snd_hdac_acomp_get_eld(&codec->core, per_pin->pin_nid,
1763 				      per_pin->dev_id, &eld->monitor_present,
1764 				      eld->eld_buffer, ELD_MAX_SIZE);
1765 	eld->eld_valid = (eld->eld_size > 0);
1766 	update_eld(codec, per_pin, eld, 0);
1767 	monitor_next = per_pin->sink_eld.monitor_present;
1768 	mutex_unlock(&per_pin->lock);
1769 
1770 	/*
1771 	 * Power-up will call hdmi_present_sense, so the PM calls
1772 	 * have to be done without mutex held.
1773 	 */
1774 
1775 	if (spec->send_silent_stream) {
1776 		int pm_ret;
1777 
1778 		if (!monitor_prev && monitor_next) {
1779 			pm_ret = snd_hda_power_up_pm(codec);
1780 			if (pm_ret < 0)
1781 				codec_err(codec,
1782 				"Monitor plugged-in, Failed to power up codec ret=[%d]\n",
1783 				pm_ret);
1784 			silent_stream_enable(codec, per_pin);
1785 		} else if (monitor_prev && !monitor_next) {
1786 			silent_stream_disable(codec, per_pin);
1787 			pm_ret = snd_hda_power_down_pm(codec);
1788 			if (pm_ret < 0)
1789 				codec_err(codec,
1790 				"Monitor plugged-out, Failed to power down codec ret=[%d]\n",
1791 				pm_ret);
1792 		}
1793 	}
1794 }
1795 
1796 static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1797 {
1798 	struct hda_codec *codec = per_pin->codec;
1799 
1800 	if (!codec_has_acomp(codec))
1801 		hdmi_present_sense_via_verbs(per_pin, repoll);
1802 	else
1803 		sync_eld_via_acomp(codec, per_pin);
1804 }
1805 
1806 static void hdmi_repoll_eld(struct work_struct *work)
1807 {
1808 	struct hdmi_spec_per_pin *per_pin =
1809 	container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1810 	struct hda_codec *codec = per_pin->codec;
1811 	struct hdmi_spec *spec = codec->spec;
1812 	struct hda_jack_tbl *jack;
1813 
1814 	jack = snd_hda_jack_tbl_get_mst(codec, per_pin->pin_nid,
1815 					per_pin->dev_id);
1816 	if (jack)
1817 		jack->jack_dirty = 1;
1818 
1819 	if (per_pin->repoll_count++ > 6)
1820 		per_pin->repoll_count = 0;
1821 
1822 	mutex_lock(&spec->pcm_lock);
1823 	hdmi_present_sense(per_pin, per_pin->repoll_count);
1824 	mutex_unlock(&spec->pcm_lock);
1825 }
1826 
1827 static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1828 {
1829 	struct hdmi_spec *spec = codec->spec;
1830 	unsigned int caps, config;
1831 	int pin_idx;
1832 	struct hdmi_spec_per_pin *per_pin;
1833 	int err;
1834 	int dev_num, i;
1835 
1836 	caps = snd_hda_query_pin_caps(codec, pin_nid);
1837 	if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1838 		return 0;
1839 
1840 	/*
1841 	 * For DP MST audio, Configuration Default is the same for
1842 	 * all device entries on the same pin
1843 	 */
1844 	config = snd_hda_codec_get_pincfg(codec, pin_nid);
1845 	if (get_defcfg_connect(config) == AC_JACK_PORT_NONE &&
1846 	    !spec->force_connect)
1847 		return 0;
1848 
1849 	/*
1850 	 * To simplify the implementation, malloc all
1851 	 * the virtual pins in the initialization statically
1852 	 */
1853 	if (spec->intel_hsw_fixup) {
1854 		/*
1855 		 * On Intel platforms, device entries count returned
1856 		 * by AC_PAR_DEVLIST_LEN is dynamic, and depends on
1857 		 * the type of receiver that is connected. Allocate pin
1858 		 * structures based on worst case.
1859 		 */
1860 		dev_num = spec->dev_num;
1861 	} else if (spec->dyn_pcm_assign && codec->dp_mst) {
1862 		dev_num = snd_hda_get_num_devices(codec, pin_nid) + 1;
1863 		/*
1864 		 * spec->dev_num is the maxinum number of device entries
1865 		 * among all the pins
1866 		 */
1867 		spec->dev_num = (spec->dev_num > dev_num) ?
1868 			spec->dev_num : dev_num;
1869 	} else {
1870 		/*
1871 		 * If the platform doesn't support DP MST,
1872 		 * manually set dev_num to 1. This means
1873 		 * the pin has only one device entry.
1874 		 */
1875 		dev_num = 1;
1876 		spec->dev_num = 1;
1877 	}
1878 
1879 	for (i = 0; i < dev_num; i++) {
1880 		pin_idx = spec->num_pins;
1881 		per_pin = snd_array_new(&spec->pins);
1882 
1883 		if (!per_pin)
1884 			return -ENOMEM;
1885 
1886 		if (spec->dyn_pcm_assign) {
1887 			per_pin->pcm = NULL;
1888 			per_pin->pcm_idx = -1;
1889 		} else {
1890 			per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
1891 			per_pin->pcm_idx = pin_idx;
1892 		}
1893 		per_pin->pin_nid = pin_nid;
1894 		per_pin->pin_nid_idx = spec->num_nids;
1895 		per_pin->dev_id = i;
1896 		per_pin->non_pcm = false;
1897 		snd_hda_set_dev_select(codec, pin_nid, i);
1898 		err = hdmi_read_pin_conn(codec, pin_idx);
1899 		if (err < 0)
1900 			return err;
1901 		spec->num_pins++;
1902 	}
1903 	spec->num_nids++;
1904 
1905 	return 0;
1906 }
1907 
1908 static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1909 {
1910 	struct hdmi_spec *spec = codec->spec;
1911 	struct hdmi_spec_per_cvt *per_cvt;
1912 	unsigned int chans;
1913 	int err;
1914 
1915 	chans = get_wcaps(codec, cvt_nid);
1916 	chans = get_wcaps_channels(chans);
1917 
1918 	per_cvt = snd_array_new(&spec->cvts);
1919 	if (!per_cvt)
1920 		return -ENOMEM;
1921 
1922 	per_cvt->cvt_nid = cvt_nid;
1923 	per_cvt->channels_min = 2;
1924 	if (chans <= 16) {
1925 		per_cvt->channels_max = chans;
1926 		if (chans > spec->chmap.channels_max)
1927 			spec->chmap.channels_max = chans;
1928 	}
1929 
1930 	err = snd_hda_query_supported_pcm(codec, cvt_nid,
1931 					  &per_cvt->rates,
1932 					  &per_cvt->formats,
1933 					  &per_cvt->maxbps);
1934 	if (err < 0)
1935 		return err;
1936 
1937 	if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1938 		spec->cvt_nids[spec->num_cvts] = cvt_nid;
1939 	spec->num_cvts++;
1940 
1941 	return 0;
1942 }
1943 
1944 static const struct snd_pci_quirk force_connect_list[] = {
1945 	SND_PCI_QUIRK(0x103c, 0x870f, "HP", 1),
1946 	SND_PCI_QUIRK(0x103c, 0x871a, "HP", 1),
1947 	{}
1948 };
1949 
1950 static int hdmi_parse_codec(struct hda_codec *codec)
1951 {
1952 	struct hdmi_spec *spec = codec->spec;
1953 	hda_nid_t start_nid;
1954 	unsigned int caps;
1955 	int i, nodes;
1956 	const struct snd_pci_quirk *q;
1957 
1958 	nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &start_nid);
1959 	if (!start_nid || nodes < 0) {
1960 		codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
1961 		return -EINVAL;
1962 	}
1963 
1964 	if (enable_all_pins)
1965 		spec->force_connect = true;
1966 
1967 	q = snd_pci_quirk_lookup(codec->bus->pci, force_connect_list);
1968 
1969 	if (q && q->value)
1970 		spec->force_connect = true;
1971 
1972 	/*
1973 	 * hdmi_add_pin() assumes total amount of converters to
1974 	 * be known, so first discover all converters
1975 	 */
1976 	for (i = 0; i < nodes; i++) {
1977 		hda_nid_t nid = start_nid + i;
1978 
1979 		caps = get_wcaps(codec, nid);
1980 
1981 		if (!(caps & AC_WCAP_DIGITAL))
1982 			continue;
1983 
1984 		if (get_wcaps_type(caps) == AC_WID_AUD_OUT)
1985 			hdmi_add_cvt(codec, nid);
1986 	}
1987 
1988 	/* discover audio pins */
1989 	for (i = 0; i < nodes; i++) {
1990 		hda_nid_t nid = start_nid + i;
1991 
1992 		caps = get_wcaps(codec, nid);
1993 
1994 		if (!(caps & AC_WCAP_DIGITAL))
1995 			continue;
1996 
1997 		if (get_wcaps_type(caps) == AC_WID_PIN)
1998 			hdmi_add_pin(codec, nid);
1999 	}
2000 
2001 	return 0;
2002 }
2003 
2004 /*
2005  */
2006 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
2007 {
2008 	struct hda_spdif_out *spdif;
2009 	bool non_pcm;
2010 
2011 	mutex_lock(&codec->spdif_mutex);
2012 	spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
2013 	/* Add sanity check to pass klockwork check.
2014 	 * This should never happen.
2015 	 */
2016 	if (WARN_ON(spdif == NULL)) {
2017 		mutex_unlock(&codec->spdif_mutex);
2018 		return true;
2019 	}
2020 	non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
2021 	mutex_unlock(&codec->spdif_mutex);
2022 	return non_pcm;
2023 }
2024 
2025 /*
2026  * HDMI callbacks
2027  */
2028 
2029 static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2030 					   struct hda_codec *codec,
2031 					   unsigned int stream_tag,
2032 					   unsigned int format,
2033 					   struct snd_pcm_substream *substream)
2034 {
2035 	hda_nid_t cvt_nid = hinfo->nid;
2036 	struct hdmi_spec *spec = codec->spec;
2037 	int pin_idx;
2038 	struct hdmi_spec_per_pin *per_pin;
2039 	struct snd_pcm_runtime *runtime = substream->runtime;
2040 	bool non_pcm;
2041 	int pinctl, stripe;
2042 	int err = 0;
2043 
2044 	mutex_lock(&spec->pcm_lock);
2045 	pin_idx = hinfo_to_pin_index(codec, hinfo);
2046 	if (spec->dyn_pcm_assign && pin_idx < 0) {
2047 		/* when dyn_pcm_assign and pcm is not bound to a pin
2048 		 * skip pin setup and return 0 to make audio playback
2049 		 * be ongoing
2050 		 */
2051 		pin_cvt_fixup(codec, NULL, cvt_nid);
2052 		snd_hda_codec_setup_stream(codec, cvt_nid,
2053 					stream_tag, 0, format);
2054 		goto unlock;
2055 	}
2056 
2057 	if (snd_BUG_ON(pin_idx < 0)) {
2058 		err = -EINVAL;
2059 		goto unlock;
2060 	}
2061 	per_pin = get_pin(spec, pin_idx);
2062 
2063 	/* Verify pin:cvt selections to avoid silent audio after S3.
2064 	 * After S3, the audio driver restores pin:cvt selections
2065 	 * but this can happen before gfx is ready and such selection
2066 	 * is overlooked by HW. Thus multiple pins can share a same
2067 	 * default convertor and mute control will affect each other,
2068 	 * which can cause a resumed audio playback become silent
2069 	 * after S3.
2070 	 */
2071 	pin_cvt_fixup(codec, per_pin, 0);
2072 
2073 	/* Call sync_audio_rate to set the N/CTS/M manually if necessary */
2074 	/* Todo: add DP1.2 MST audio support later */
2075 	if (codec_has_acomp(codec))
2076 		snd_hdac_sync_audio_rate(&codec->core, per_pin->pin_nid,
2077 					 per_pin->dev_id, runtime->rate);
2078 
2079 	non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
2080 	mutex_lock(&per_pin->lock);
2081 	per_pin->channels = substream->runtime->channels;
2082 	per_pin->setup = true;
2083 
2084 	if (get_wcaps(codec, cvt_nid) & AC_WCAP_STRIPE) {
2085 		stripe = snd_hdac_get_stream_stripe_ctl(&codec->bus->core,
2086 							substream);
2087 		snd_hda_codec_write(codec, cvt_nid, 0,
2088 				    AC_VERB_SET_STRIPE_CONTROL,
2089 				    stripe);
2090 	}
2091 
2092 	hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
2093 	mutex_unlock(&per_pin->lock);
2094 	if (spec->dyn_pin_out) {
2095 		snd_hda_set_dev_select(codec, per_pin->pin_nid,
2096 				       per_pin->dev_id);
2097 		pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
2098 					    AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2099 		snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2100 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
2101 				    pinctl | PIN_OUT);
2102 	}
2103 
2104 	/* snd_hda_set_dev_select() has been called before */
2105 	err = spec->ops.setup_stream(codec, cvt_nid, per_pin->pin_nid,
2106 				     per_pin->dev_id, stream_tag, format);
2107  unlock:
2108 	mutex_unlock(&spec->pcm_lock);
2109 	return err;
2110 }
2111 
2112 static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2113 					     struct hda_codec *codec,
2114 					     struct snd_pcm_substream *substream)
2115 {
2116 	snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2117 	return 0;
2118 }
2119 
2120 static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
2121 			  struct hda_codec *codec,
2122 			  struct snd_pcm_substream *substream)
2123 {
2124 	struct hdmi_spec *spec = codec->spec;
2125 	int cvt_idx, pin_idx, pcm_idx;
2126 	struct hdmi_spec_per_cvt *per_cvt;
2127 	struct hdmi_spec_per_pin *per_pin;
2128 	int pinctl;
2129 	int err = 0;
2130 
2131 	mutex_lock(&spec->pcm_lock);
2132 	if (hinfo->nid) {
2133 		pcm_idx = hinfo_to_pcm_index(codec, hinfo);
2134 		if (snd_BUG_ON(pcm_idx < 0)) {
2135 			err = -EINVAL;
2136 			goto unlock;
2137 		}
2138 		cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
2139 		if (snd_BUG_ON(cvt_idx < 0)) {
2140 			err = -EINVAL;
2141 			goto unlock;
2142 		}
2143 		per_cvt = get_cvt(spec, cvt_idx);
2144 		per_cvt->assigned = 0;
2145 		hinfo->nid = 0;
2146 
2147 		azx_stream(get_azx_dev(substream))->stripe = 0;
2148 
2149 		snd_hda_spdif_ctls_unassign(codec, pcm_idx);
2150 		clear_bit(pcm_idx, &spec->pcm_in_use);
2151 		pin_idx = hinfo_to_pin_index(codec, hinfo);
2152 		if (spec->dyn_pcm_assign && pin_idx < 0)
2153 			goto unlock;
2154 
2155 		if (snd_BUG_ON(pin_idx < 0)) {
2156 			err = -EINVAL;
2157 			goto unlock;
2158 		}
2159 		per_pin = get_pin(spec, pin_idx);
2160 
2161 		if (spec->dyn_pin_out) {
2162 			snd_hda_set_dev_select(codec, per_pin->pin_nid,
2163 					       per_pin->dev_id);
2164 			pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
2165 					AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2166 			snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2167 					    AC_VERB_SET_PIN_WIDGET_CONTROL,
2168 					    pinctl & ~PIN_OUT);
2169 		}
2170 
2171 		mutex_lock(&per_pin->lock);
2172 		per_pin->chmap_set = false;
2173 		memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
2174 
2175 		per_pin->setup = false;
2176 		per_pin->channels = 0;
2177 		mutex_unlock(&per_pin->lock);
2178 	}
2179 
2180 unlock:
2181 	mutex_unlock(&spec->pcm_lock);
2182 
2183 	return err;
2184 }
2185 
2186 static const struct hda_pcm_ops generic_ops = {
2187 	.open = hdmi_pcm_open,
2188 	.close = hdmi_pcm_close,
2189 	.prepare = generic_hdmi_playback_pcm_prepare,
2190 	.cleanup = generic_hdmi_playback_pcm_cleanup,
2191 };
2192 
2193 static int hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
2194 {
2195 	struct hda_codec *codec = hdac_to_hda_codec(hdac);
2196 	struct hdmi_spec *spec = codec->spec;
2197 	struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2198 
2199 	if (!per_pin)
2200 		return 0;
2201 
2202 	return per_pin->sink_eld.info.spk_alloc;
2203 }
2204 
2205 static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
2206 					unsigned char *chmap)
2207 {
2208 	struct hda_codec *codec = hdac_to_hda_codec(hdac);
2209 	struct hdmi_spec *spec = codec->spec;
2210 	struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2211 
2212 	/* chmap is already set to 0 in caller */
2213 	if (!per_pin)
2214 		return;
2215 
2216 	memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
2217 }
2218 
2219 static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
2220 				unsigned char *chmap, int prepared)
2221 {
2222 	struct hda_codec *codec = hdac_to_hda_codec(hdac);
2223 	struct hdmi_spec *spec = codec->spec;
2224 	struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2225 
2226 	if (!per_pin)
2227 		return;
2228 	mutex_lock(&per_pin->lock);
2229 	per_pin->chmap_set = true;
2230 	memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
2231 	if (prepared)
2232 		hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
2233 	mutex_unlock(&per_pin->lock);
2234 }
2235 
2236 static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
2237 {
2238 	struct hda_codec *codec = hdac_to_hda_codec(hdac);
2239 	struct hdmi_spec *spec = codec->spec;
2240 	struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2241 
2242 	return per_pin ? true:false;
2243 }
2244 
2245 static int generic_hdmi_build_pcms(struct hda_codec *codec)
2246 {
2247 	struct hdmi_spec *spec = codec->spec;
2248 	int idx, pcm_num;
2249 
2250 	/*
2251 	 * for non-mst mode, pcm number is the same as before
2252 	 * for DP MST mode without extra PCM, pcm number is same
2253 	 * for DP MST mode with extra PCMs, pcm number is
2254 	 *  (nid number + dev_num - 1)
2255 	 * dev_num is the device entry number in a pin
2256 	 */
2257 
2258 	if (codec->mst_no_extra_pcms)
2259 		pcm_num = spec->num_nids;
2260 	else
2261 		pcm_num = spec->num_nids + spec->dev_num - 1;
2262 
2263 	codec_dbg(codec, "hdmi: pcm_num set to %d\n", pcm_num);
2264 
2265 	for (idx = 0; idx < pcm_num; idx++) {
2266 		struct hda_pcm *info;
2267 		struct hda_pcm_stream *pstr;
2268 
2269 		info = snd_hda_codec_pcm_new(codec, "HDMI %d", idx);
2270 		if (!info)
2271 			return -ENOMEM;
2272 
2273 		spec->pcm_rec[idx].pcm = info;
2274 		spec->pcm_used++;
2275 		info->pcm_type = HDA_PCM_TYPE_HDMI;
2276 		info->own_chmap = true;
2277 
2278 		pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2279 		pstr->substreams = 1;
2280 		pstr->ops = generic_ops;
2281 		/* pcm number is less than 16 */
2282 		if (spec->pcm_used >= 16)
2283 			break;
2284 		/* other pstr fields are set in open */
2285 	}
2286 
2287 	return 0;
2288 }
2289 
2290 static void free_hdmi_jack_priv(struct snd_jack *jack)
2291 {
2292 	struct hdmi_pcm *pcm = jack->private_data;
2293 
2294 	pcm->jack = NULL;
2295 }
2296 
2297 static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
2298 {
2299 	char hdmi_str[32] = "HDMI/DP";
2300 	struct hdmi_spec *spec = codec->spec;
2301 	struct hdmi_spec_per_pin *per_pin = get_pin(spec, pcm_idx);
2302 	struct snd_jack *jack;
2303 	int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
2304 	int err;
2305 
2306 	if (pcmdev > 0)
2307 		sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
2308 	if (!spec->dyn_pcm_assign &&
2309 	    !is_jack_detectable(codec, per_pin->pin_nid))
2310 		strncat(hdmi_str, " Phantom",
2311 			sizeof(hdmi_str) - strlen(hdmi_str) - 1);
2312 
2313 	err = snd_jack_new(codec->card, hdmi_str, SND_JACK_AVOUT, &jack,
2314 			   true, false);
2315 	if (err < 0)
2316 		return err;
2317 
2318 	spec->pcm_rec[pcm_idx].jack = jack;
2319 	jack->private_data = &spec->pcm_rec[pcm_idx];
2320 	jack->private_free = free_hdmi_jack_priv;
2321 	return 0;
2322 }
2323 
2324 static int generic_hdmi_build_controls(struct hda_codec *codec)
2325 {
2326 	struct hdmi_spec *spec = codec->spec;
2327 	int dev, err;
2328 	int pin_idx, pcm_idx;
2329 
2330 	for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2331 		if (!get_pcm_rec(spec, pcm_idx)->pcm) {
2332 			/* no PCM: mark this for skipping permanently */
2333 			set_bit(pcm_idx, &spec->pcm_bitmap);
2334 			continue;
2335 		}
2336 
2337 		err = generic_hdmi_build_jack(codec, pcm_idx);
2338 		if (err < 0)
2339 			return err;
2340 
2341 		/* create the spdif for each pcm
2342 		 * pin will be bound when monitor is connected
2343 		 */
2344 		if (spec->dyn_pcm_assign)
2345 			err = snd_hda_create_dig_out_ctls(codec,
2346 					  0, spec->cvt_nids[0],
2347 					  HDA_PCM_TYPE_HDMI);
2348 		else {
2349 			struct hdmi_spec_per_pin *per_pin =
2350 				get_pin(spec, pcm_idx);
2351 			err = snd_hda_create_dig_out_ctls(codec,
2352 						  per_pin->pin_nid,
2353 						  per_pin->mux_nids[0],
2354 						  HDA_PCM_TYPE_HDMI);
2355 		}
2356 		if (err < 0)
2357 			return err;
2358 		snd_hda_spdif_ctls_unassign(codec, pcm_idx);
2359 
2360 		dev = get_pcm_rec(spec, pcm_idx)->device;
2361 		if (dev != SNDRV_PCM_INVALID_DEVICE) {
2362 			/* add control for ELD Bytes */
2363 			err = hdmi_create_eld_ctl(codec, pcm_idx, dev);
2364 			if (err < 0)
2365 				return err;
2366 		}
2367 	}
2368 
2369 	for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2370 		struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2371 		struct hdmi_eld *pin_eld = &per_pin->sink_eld;
2372 
2373 		pin_eld->eld_valid = false;
2374 		hdmi_present_sense(per_pin, 0);
2375 	}
2376 
2377 	/* add channel maps */
2378 	for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2379 		struct hda_pcm *pcm;
2380 
2381 		pcm = get_pcm_rec(spec, pcm_idx);
2382 		if (!pcm || !pcm->pcm)
2383 			break;
2384 		err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap);
2385 		if (err < 0)
2386 			return err;
2387 	}
2388 
2389 	return 0;
2390 }
2391 
2392 static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2393 {
2394 	struct hdmi_spec *spec = codec->spec;
2395 	int pin_idx;
2396 
2397 	for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2398 		struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2399 
2400 		per_pin->codec = codec;
2401 		mutex_init(&per_pin->lock);
2402 		INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
2403 		eld_proc_new(per_pin, pin_idx);
2404 	}
2405 	return 0;
2406 }
2407 
2408 static int generic_hdmi_init(struct hda_codec *codec)
2409 {
2410 	struct hdmi_spec *spec = codec->spec;
2411 	int pin_idx;
2412 
2413 	mutex_lock(&spec->bind_lock);
2414 	for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2415 		struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2416 		hda_nid_t pin_nid = per_pin->pin_nid;
2417 		int dev_id = per_pin->dev_id;
2418 
2419 		snd_hda_set_dev_select(codec, pin_nid, dev_id);
2420 		hdmi_init_pin(codec, pin_nid);
2421 		if (codec_has_acomp(codec))
2422 			continue;
2423 		snd_hda_jack_detect_enable_callback_mst(codec, pin_nid, dev_id,
2424 							jack_callback);
2425 	}
2426 	mutex_unlock(&spec->bind_lock);
2427 	return 0;
2428 }
2429 
2430 static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2431 {
2432 	snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2433 	snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
2434 }
2435 
2436 static void hdmi_array_free(struct hdmi_spec *spec)
2437 {
2438 	snd_array_free(&spec->pins);
2439 	snd_array_free(&spec->cvts);
2440 }
2441 
2442 static void generic_spec_free(struct hda_codec *codec)
2443 {
2444 	struct hdmi_spec *spec = codec->spec;
2445 
2446 	if (spec) {
2447 		hdmi_array_free(spec);
2448 		kfree(spec);
2449 		codec->spec = NULL;
2450 	}
2451 	codec->dp_mst = false;
2452 }
2453 
2454 static void generic_hdmi_free(struct hda_codec *codec)
2455 {
2456 	struct hdmi_spec *spec = codec->spec;
2457 	int pin_idx, pcm_idx;
2458 
2459 	if (spec->acomp_registered) {
2460 		snd_hdac_acomp_exit(&codec->bus->core);
2461 	} else if (codec_has_acomp(codec)) {
2462 		snd_hdac_acomp_register_notifier(&codec->bus->core, NULL);
2463 	}
2464 	codec->relaxed_resume = 0;
2465 
2466 	for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2467 		struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2468 		cancel_delayed_work_sync(&per_pin->work);
2469 		eld_proc_free(per_pin);
2470 	}
2471 
2472 	for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2473 		if (spec->pcm_rec[pcm_idx].jack == NULL)
2474 			continue;
2475 		if (spec->dyn_pcm_assign)
2476 			snd_device_free(codec->card,
2477 					spec->pcm_rec[pcm_idx].jack);
2478 		else
2479 			spec->pcm_rec[pcm_idx].jack = NULL;
2480 	}
2481 
2482 	generic_spec_free(codec);
2483 }
2484 
2485 #ifdef CONFIG_PM
2486 static int generic_hdmi_suspend(struct hda_codec *codec)
2487 {
2488 	struct hdmi_spec *spec = codec->spec;
2489 	int pin_idx;
2490 
2491 	for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2492 		struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2493 		cancel_delayed_work_sync(&per_pin->work);
2494 	}
2495 	return 0;
2496 }
2497 
2498 static int generic_hdmi_resume(struct hda_codec *codec)
2499 {
2500 	struct hdmi_spec *spec = codec->spec;
2501 	int pin_idx;
2502 
2503 	codec->patch_ops.init(codec);
2504 	snd_hda_regmap_sync(codec);
2505 
2506 	for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2507 		struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2508 		hdmi_present_sense(per_pin, 1);
2509 	}
2510 	return 0;
2511 }
2512 #endif
2513 
2514 static const struct hda_codec_ops generic_hdmi_patch_ops = {
2515 	.init			= generic_hdmi_init,
2516 	.free			= generic_hdmi_free,
2517 	.build_pcms		= generic_hdmi_build_pcms,
2518 	.build_controls		= generic_hdmi_build_controls,
2519 	.unsol_event		= hdmi_unsol_event,
2520 #ifdef CONFIG_PM
2521 	.suspend		= generic_hdmi_suspend,
2522 	.resume			= generic_hdmi_resume,
2523 #endif
2524 };
2525 
2526 static const struct hdmi_ops generic_standard_hdmi_ops = {
2527 	.pin_get_eld				= hdmi_pin_get_eld,
2528 	.pin_setup_infoframe			= hdmi_pin_setup_infoframe,
2529 	.pin_hbr_setup				= hdmi_pin_hbr_setup,
2530 	.setup_stream				= hdmi_setup_stream,
2531 };
2532 
2533 /* allocate codec->spec and assign/initialize generic parser ops */
2534 static int alloc_generic_hdmi(struct hda_codec *codec)
2535 {
2536 	struct hdmi_spec *spec;
2537 
2538 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2539 	if (!spec)
2540 		return -ENOMEM;
2541 
2542 	spec->codec = codec;
2543 	spec->ops = generic_standard_hdmi_ops;
2544 	spec->dev_num = 1;	/* initialize to 1 */
2545 	mutex_init(&spec->pcm_lock);
2546 	mutex_init(&spec->bind_lock);
2547 	snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
2548 
2549 	spec->chmap.ops.get_chmap = hdmi_get_chmap;
2550 	spec->chmap.ops.set_chmap = hdmi_set_chmap;
2551 	spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
2552 	spec->chmap.ops.get_spk_alloc = hdmi_get_spk_alloc;
2553 
2554 	codec->spec = spec;
2555 	hdmi_array_init(spec, 4);
2556 
2557 	codec->patch_ops = generic_hdmi_patch_ops;
2558 
2559 	return 0;
2560 }
2561 
2562 /* generic HDMI parser */
2563 static int patch_generic_hdmi(struct hda_codec *codec)
2564 {
2565 	int err;
2566 
2567 	err = alloc_generic_hdmi(codec);
2568 	if (err < 0)
2569 		return err;
2570 
2571 	err = hdmi_parse_codec(codec);
2572 	if (err < 0) {
2573 		generic_spec_free(codec);
2574 		return err;
2575 	}
2576 
2577 	generic_hdmi_init_per_pins(codec);
2578 	return 0;
2579 }
2580 
2581 /*
2582  * generic audio component binding
2583  */
2584 
2585 /* turn on / off the unsol event jack detection dynamically */
2586 static void reprogram_jack_detect(struct hda_codec *codec, hda_nid_t nid,
2587 				  int dev_id, bool use_acomp)
2588 {
2589 	struct hda_jack_tbl *tbl;
2590 
2591 	tbl = snd_hda_jack_tbl_get_mst(codec, nid, dev_id);
2592 	if (tbl) {
2593 		/* clear unsol even if component notifier is used, or re-enable
2594 		 * if notifier is cleared
2595 		 */
2596 		unsigned int val = use_acomp ? 0 : (AC_USRSP_EN | tbl->tag);
2597 		snd_hda_codec_write_cache(codec, nid, 0,
2598 					  AC_VERB_SET_UNSOLICITED_ENABLE, val);
2599 	}
2600 }
2601 
2602 /* set up / clear component notifier dynamically */
2603 static void generic_acomp_notifier_set(struct drm_audio_component *acomp,
2604 				       bool use_acomp)
2605 {
2606 	struct hdmi_spec *spec;
2607 	int i;
2608 
2609 	spec = container_of(acomp->audio_ops, struct hdmi_spec, drm_audio_ops);
2610 	mutex_lock(&spec->bind_lock);
2611 	spec->use_acomp_notifier = use_acomp;
2612 	spec->codec->relaxed_resume = use_acomp;
2613 	spec->codec->bus->keep_power = 0;
2614 	/* reprogram each jack detection logic depending on the notifier */
2615 	for (i = 0; i < spec->num_pins; i++)
2616 		reprogram_jack_detect(spec->codec,
2617 				      get_pin(spec, i)->pin_nid,
2618 				      get_pin(spec, i)->dev_id,
2619 				      use_acomp);
2620 	mutex_unlock(&spec->bind_lock);
2621 }
2622 
2623 /* enable / disable the notifier via master bind / unbind */
2624 static int generic_acomp_master_bind(struct device *dev,
2625 				     struct drm_audio_component *acomp)
2626 {
2627 	generic_acomp_notifier_set(acomp, true);
2628 	return 0;
2629 }
2630 
2631 static void generic_acomp_master_unbind(struct device *dev,
2632 					struct drm_audio_component *acomp)
2633 {
2634 	generic_acomp_notifier_set(acomp, false);
2635 }
2636 
2637 /* check whether both HD-audio and DRM PCI devices belong to the same bus */
2638 static int match_bound_vga(struct device *dev, int subtype, void *data)
2639 {
2640 	struct hdac_bus *bus = data;
2641 	struct pci_dev *pci, *master;
2642 
2643 	if (!dev_is_pci(dev) || !dev_is_pci(bus->dev))
2644 		return 0;
2645 	master = to_pci_dev(bus->dev);
2646 	pci = to_pci_dev(dev);
2647 	return master->bus == pci->bus;
2648 }
2649 
2650 /* audio component notifier for AMD/Nvidia HDMI codecs */
2651 static void generic_acomp_pin_eld_notify(void *audio_ptr, int port, int dev_id)
2652 {
2653 	struct hda_codec *codec = audio_ptr;
2654 	struct hdmi_spec *spec = codec->spec;
2655 	hda_nid_t pin_nid = spec->port2pin(codec, port);
2656 
2657 	if (!pin_nid)
2658 		return;
2659 	if (get_wcaps_type(get_wcaps(codec, pin_nid)) != AC_WID_PIN)
2660 		return;
2661 	/* skip notification during system suspend (but not in runtime PM);
2662 	 * the state will be updated at resume
2663 	 */
2664 	if (codec->core.dev.power.power_state.event == PM_EVENT_SUSPEND)
2665 		return;
2666 	/* ditto during suspend/resume process itself */
2667 	if (snd_hdac_is_in_pm(&codec->core))
2668 		return;
2669 
2670 	check_presence_and_report(codec, pin_nid, dev_id);
2671 }
2672 
2673 /* set up the private drm_audio_ops from the template */
2674 static void setup_drm_audio_ops(struct hda_codec *codec,
2675 				const struct drm_audio_component_audio_ops *ops)
2676 {
2677 	struct hdmi_spec *spec = codec->spec;
2678 
2679 	spec->drm_audio_ops.audio_ptr = codec;
2680 	/* intel_audio_codec_enable() or intel_audio_codec_disable()
2681 	 * will call pin_eld_notify with using audio_ptr pointer
2682 	 * We need make sure audio_ptr is really setup
2683 	 */
2684 	wmb();
2685 	spec->drm_audio_ops.pin2port = ops->pin2port;
2686 	spec->drm_audio_ops.pin_eld_notify = ops->pin_eld_notify;
2687 	spec->drm_audio_ops.master_bind = ops->master_bind;
2688 	spec->drm_audio_ops.master_unbind = ops->master_unbind;
2689 }
2690 
2691 /* initialize the generic HDMI audio component */
2692 static void generic_acomp_init(struct hda_codec *codec,
2693 			       const struct drm_audio_component_audio_ops *ops,
2694 			       int (*port2pin)(struct hda_codec *, int))
2695 {
2696 	struct hdmi_spec *spec = codec->spec;
2697 
2698 	if (!enable_acomp) {
2699 		codec_info(codec, "audio component disabled by module option\n");
2700 		return;
2701 	}
2702 
2703 	spec->port2pin = port2pin;
2704 	setup_drm_audio_ops(codec, ops);
2705 	if (!snd_hdac_acomp_init(&codec->bus->core, &spec->drm_audio_ops,
2706 				 match_bound_vga, 0)) {
2707 		spec->acomp_registered = true;
2708 	}
2709 }
2710 
2711 /*
2712  * Intel codec parsers and helpers
2713  */
2714 
2715 #define INTEL_GET_VENDOR_VERB	0xf81
2716 #define INTEL_SET_VENDOR_VERB	0x781
2717 #define INTEL_EN_DP12		0x02	/* enable DP 1.2 features */
2718 #define INTEL_EN_ALL_PIN_CVTS	0x01	/* enable 2nd & 3rd pins and convertors */
2719 
2720 static void intel_haswell_enable_all_pins(struct hda_codec *codec,
2721 					  bool update_tree)
2722 {
2723 	unsigned int vendor_param;
2724 	struct hdmi_spec *spec = codec->spec;
2725 
2726 	vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2727 				INTEL_GET_VENDOR_VERB, 0);
2728 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2729 		return;
2730 
2731 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2732 	vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2733 				INTEL_SET_VENDOR_VERB, vendor_param);
2734 	if (vendor_param == -1)
2735 		return;
2736 
2737 	if (update_tree)
2738 		snd_hda_codec_update_widgets(codec);
2739 }
2740 
2741 static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2742 {
2743 	unsigned int vendor_param;
2744 	struct hdmi_spec *spec = codec->spec;
2745 
2746 	vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2747 				INTEL_GET_VENDOR_VERB, 0);
2748 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2749 		return;
2750 
2751 	/* enable DP1.2 mode */
2752 	vendor_param |= INTEL_EN_DP12;
2753 	snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
2754 	snd_hda_codec_write_cache(codec, spec->vendor_nid, 0,
2755 				INTEL_SET_VENDOR_VERB, vendor_param);
2756 }
2757 
2758 /* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2759  * Otherwise you may get severe h/w communication errors.
2760  */
2761 static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2762 				unsigned int power_state)
2763 {
2764 	if (power_state == AC_PWRST_D0) {
2765 		intel_haswell_enable_all_pins(codec, false);
2766 		intel_haswell_fixup_enable_dp12(codec);
2767 	}
2768 
2769 	snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2770 	snd_hda_codec_set_power_to_all(codec, fg, power_state);
2771 }
2772 
2773 /* There is a fixed mapping between audio pin node and display port.
2774  * on SNB, IVY, HSW, BSW, SKL, BXT, KBL:
2775  * Pin Widget 5 - PORT B (port = 1 in i915 driver)
2776  * Pin Widget 6 - PORT C (port = 2 in i915 driver)
2777  * Pin Widget 7 - PORT D (port = 3 in i915 driver)
2778  *
2779  * on VLV, ILK:
2780  * Pin Widget 4 - PORT B (port = 1 in i915 driver)
2781  * Pin Widget 5 - PORT C (port = 2 in i915 driver)
2782  * Pin Widget 6 - PORT D (port = 3 in i915 driver)
2783  */
2784 static int intel_base_nid(struct hda_codec *codec)
2785 {
2786 	switch (codec->core.vendor_id) {
2787 	case 0x80860054: /* ILK */
2788 	case 0x80862804: /* ILK */
2789 	case 0x80862882: /* VLV */
2790 		return 4;
2791 	default:
2792 		return 5;
2793 	}
2794 }
2795 
2796 static int intel_pin2port(void *audio_ptr, int pin_nid)
2797 {
2798 	struct hda_codec *codec = audio_ptr;
2799 	struct hdmi_spec *spec = codec->spec;
2800 	int base_nid, i;
2801 
2802 	if (!spec->port_num) {
2803 		base_nid = intel_base_nid(codec);
2804 		if (WARN_ON(pin_nid < base_nid || pin_nid >= base_nid + 3))
2805 			return -1;
2806 		return pin_nid - base_nid + 1;
2807 	}
2808 
2809 	/*
2810 	 * looking for the pin number in the mapping table and return
2811 	 * the index which indicate the port number
2812 	 */
2813 	for (i = 0; i < spec->port_num; i++) {
2814 		if (pin_nid == spec->port_map[i])
2815 			return i;
2816 	}
2817 
2818 	codec_info(codec, "Can't find the HDMI/DP port for pin NID 0x%x\n", pin_nid);
2819 	return -1;
2820 }
2821 
2822 static int intel_port2pin(struct hda_codec *codec, int port)
2823 {
2824 	struct hdmi_spec *spec = codec->spec;
2825 
2826 	if (!spec->port_num) {
2827 		/* we assume only from port-B to port-D */
2828 		if (port < 1 || port > 3)
2829 			return 0;
2830 		return port + intel_base_nid(codec) - 1;
2831 	}
2832 
2833 	if (port < 0 || port >= spec->port_num)
2834 		return 0;
2835 	return spec->port_map[port];
2836 }
2837 
2838 static void intel_pin_eld_notify(void *audio_ptr, int port, int pipe)
2839 {
2840 	struct hda_codec *codec = audio_ptr;
2841 	int pin_nid;
2842 	int dev_id = pipe;
2843 
2844 	pin_nid = intel_port2pin(codec, port);
2845 	if (!pin_nid)
2846 		return;
2847 	/* skip notification during system suspend (but not in runtime PM);
2848 	 * the state will be updated at resume
2849 	 */
2850 	if (codec->core.dev.power.power_state.event == PM_EVENT_SUSPEND)
2851 		return;
2852 	/* ditto during suspend/resume process itself */
2853 	if (snd_hdac_is_in_pm(&codec->core))
2854 		return;
2855 
2856 	snd_hdac_i915_set_bclk(&codec->bus->core);
2857 	check_presence_and_report(codec, pin_nid, dev_id);
2858 }
2859 
2860 static const struct drm_audio_component_audio_ops intel_audio_ops = {
2861 	.pin2port = intel_pin2port,
2862 	.pin_eld_notify = intel_pin_eld_notify,
2863 };
2864 
2865 /* register i915 component pin_eld_notify callback */
2866 static void register_i915_notifier(struct hda_codec *codec)
2867 {
2868 	struct hdmi_spec *spec = codec->spec;
2869 
2870 	spec->use_acomp_notifier = true;
2871 	spec->port2pin = intel_port2pin;
2872 	setup_drm_audio_ops(codec, &intel_audio_ops);
2873 	snd_hdac_acomp_register_notifier(&codec->bus->core,
2874 					&spec->drm_audio_ops);
2875 	/* no need for forcible resume for jack check thanks to notifier */
2876 	codec->relaxed_resume = 1;
2877 }
2878 
2879 /* setup_stream ops override for HSW+ */
2880 static int i915_hsw_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
2881 				 hda_nid_t pin_nid, int dev_id, u32 stream_tag,
2882 				 int format)
2883 {
2884 	haswell_verify_D0(codec, cvt_nid, pin_nid);
2885 	return hdmi_setup_stream(codec, cvt_nid, pin_nid, dev_id,
2886 				 stream_tag, format);
2887 }
2888 
2889 /* pin_cvt_fixup ops override for HSW+ and VLV+ */
2890 static void i915_pin_cvt_fixup(struct hda_codec *codec,
2891 			       struct hdmi_spec_per_pin *per_pin,
2892 			       hda_nid_t cvt_nid)
2893 {
2894 	if (per_pin) {
2895 		haswell_verify_D0(codec, per_pin->cvt_nid, per_pin->pin_nid);
2896 		snd_hda_set_dev_select(codec, per_pin->pin_nid,
2897 			       per_pin->dev_id);
2898 		intel_verify_pin_cvt_connect(codec, per_pin);
2899 		intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
2900 				     per_pin->dev_id, per_pin->mux_idx);
2901 	} else {
2902 		intel_not_share_assigned_cvt_nid(codec, 0, 0, cvt_nid);
2903 	}
2904 }
2905 
2906 /* precondition and allocation for Intel codecs */
2907 static int alloc_intel_hdmi(struct hda_codec *codec)
2908 {
2909 	int err;
2910 
2911 	/* requires i915 binding */
2912 	if (!codec->bus->core.audio_component) {
2913 		codec_info(codec, "No i915 binding for Intel HDMI/DP codec\n");
2914 		/* set probe_id here to prevent generic fallback binding */
2915 		codec->probe_id = HDA_CODEC_ID_SKIP_PROBE;
2916 		return -ENODEV;
2917 	}
2918 
2919 	err = alloc_generic_hdmi(codec);
2920 	if (err < 0)
2921 		return err;
2922 	/* no need to handle unsol events */
2923 	codec->patch_ops.unsol_event = NULL;
2924 	return 0;
2925 }
2926 
2927 /* parse and post-process for Intel codecs */
2928 static int parse_intel_hdmi(struct hda_codec *codec)
2929 {
2930 	int err, retries = 3;
2931 
2932 	do {
2933 		err = hdmi_parse_codec(codec);
2934 	} while (err < 0 && retries--);
2935 
2936 	if (err < 0) {
2937 		generic_spec_free(codec);
2938 		return err;
2939 	}
2940 
2941 	generic_hdmi_init_per_pins(codec);
2942 	register_i915_notifier(codec);
2943 	return 0;
2944 }
2945 
2946 /* Intel Haswell and onwards; audio component with eld notifier */
2947 static int intel_hsw_common_init(struct hda_codec *codec, hda_nid_t vendor_nid,
2948 				 const int *port_map, int port_num, int dev_num)
2949 {
2950 	struct hdmi_spec *spec;
2951 	int err;
2952 
2953 	err = alloc_intel_hdmi(codec);
2954 	if (err < 0)
2955 		return err;
2956 	spec = codec->spec;
2957 	codec->dp_mst = true;
2958 	spec->dyn_pcm_assign = true;
2959 	spec->vendor_nid = vendor_nid;
2960 	spec->port_map = port_map;
2961 	spec->port_num = port_num;
2962 	spec->intel_hsw_fixup = true;
2963 	spec->dev_num = dev_num;
2964 
2965 	intel_haswell_enable_all_pins(codec, true);
2966 	intel_haswell_fixup_enable_dp12(codec);
2967 
2968 	codec->display_power_control = 1;
2969 
2970 	codec->patch_ops.set_power_state = haswell_set_power_state;
2971 	codec->depop_delay = 0;
2972 	codec->auto_runtime_pm = 1;
2973 
2974 	spec->ops.setup_stream = i915_hsw_setup_stream;
2975 	spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
2976 
2977 	/*
2978 	 * Enable silent stream feature, if it is enabled via
2979 	 * module param or Kconfig option
2980 	 */
2981 	if (enable_silent_stream)
2982 		spec->send_silent_stream = true;
2983 
2984 	return parse_intel_hdmi(codec);
2985 }
2986 
2987 static int patch_i915_hsw_hdmi(struct hda_codec *codec)
2988 {
2989 	return intel_hsw_common_init(codec, 0x08, NULL, 0, 3);
2990 }
2991 
2992 static int patch_i915_glk_hdmi(struct hda_codec *codec)
2993 {
2994 	return intel_hsw_common_init(codec, 0x0b, NULL, 0, 3);
2995 }
2996 
2997 static int patch_i915_icl_hdmi(struct hda_codec *codec)
2998 {
2999 	/*
3000 	 * pin to port mapping table where the value indicate the pin number and
3001 	 * the index indicate the port number.
3002 	 */
3003 	static const int map[] = {0x0, 0x4, 0x6, 0x8, 0xa, 0xb};
3004 
3005 	return intel_hsw_common_init(codec, 0x02, map, ARRAY_SIZE(map), 3);
3006 }
3007 
3008 static int patch_i915_tgl_hdmi(struct hda_codec *codec)
3009 {
3010 	/*
3011 	 * pin to port mapping table where the value indicate the pin number and
3012 	 * the index indicate the port number.
3013 	 */
3014 	static const int map[] = {0x4, 0x6, 0x8, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
3015 	int ret;
3016 
3017 	ret = intel_hsw_common_init(codec, 0x02, map, ARRAY_SIZE(map), 4);
3018 	if (!ret) {
3019 		struct hdmi_spec *spec = codec->spec;
3020 
3021 		spec->dyn_pcm_no_legacy = true;
3022 	}
3023 
3024 	return ret;
3025 }
3026 
3027 /* Intel Baytrail and Braswell; with eld notifier */
3028 static int patch_i915_byt_hdmi(struct hda_codec *codec)
3029 {
3030 	struct hdmi_spec *spec;
3031 	int err;
3032 
3033 	err = alloc_intel_hdmi(codec);
3034 	if (err < 0)
3035 		return err;
3036 	spec = codec->spec;
3037 
3038 	/* For Valleyview/Cherryview, only the display codec is in the display
3039 	 * power well and can use link_power ops to request/release the power.
3040 	 */
3041 	codec->display_power_control = 1;
3042 
3043 	codec->depop_delay = 0;
3044 	codec->auto_runtime_pm = 1;
3045 
3046 	spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
3047 
3048 	return parse_intel_hdmi(codec);
3049 }
3050 
3051 /* Intel IronLake, SandyBridge and IvyBridge; with eld notifier */
3052 static int patch_i915_cpt_hdmi(struct hda_codec *codec)
3053 {
3054 	int err;
3055 
3056 	err = alloc_intel_hdmi(codec);
3057 	if (err < 0)
3058 		return err;
3059 	return parse_intel_hdmi(codec);
3060 }
3061 
3062 /*
3063  * Shared non-generic implementations
3064  */
3065 
3066 static int simple_playback_build_pcms(struct hda_codec *codec)
3067 {
3068 	struct hdmi_spec *spec = codec->spec;
3069 	struct hda_pcm *info;
3070 	unsigned int chans;
3071 	struct hda_pcm_stream *pstr;
3072 	struct hdmi_spec_per_cvt *per_cvt;
3073 
3074 	per_cvt = get_cvt(spec, 0);
3075 	chans = get_wcaps(codec, per_cvt->cvt_nid);
3076 	chans = get_wcaps_channels(chans);
3077 
3078 	info = snd_hda_codec_pcm_new(codec, "HDMI 0");
3079 	if (!info)
3080 		return -ENOMEM;
3081 	spec->pcm_rec[0].pcm = info;
3082 	info->pcm_type = HDA_PCM_TYPE_HDMI;
3083 	pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
3084 	*pstr = spec->pcm_playback;
3085 	pstr->nid = per_cvt->cvt_nid;
3086 	if (pstr->channels_max <= 2 && chans && chans <= 16)
3087 		pstr->channels_max = chans;
3088 
3089 	return 0;
3090 }
3091 
3092 /* unsolicited event for jack sensing */
3093 static void simple_hdmi_unsol_event(struct hda_codec *codec,
3094 				    unsigned int res)
3095 {
3096 	snd_hda_jack_set_dirty_all(codec);
3097 	snd_hda_jack_report_sync(codec);
3098 }
3099 
3100 /* generic_hdmi_build_jack can be used for simple_hdmi, too,
3101  * as long as spec->pins[] is set correctly
3102  */
3103 #define simple_hdmi_build_jack	generic_hdmi_build_jack
3104 
3105 static int simple_playback_build_controls(struct hda_codec *codec)
3106 {
3107 	struct hdmi_spec *spec = codec->spec;
3108 	struct hdmi_spec_per_cvt *per_cvt;
3109 	int err;
3110 
3111 	per_cvt = get_cvt(spec, 0);
3112 	err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
3113 					  per_cvt->cvt_nid,
3114 					  HDA_PCM_TYPE_HDMI);
3115 	if (err < 0)
3116 		return err;
3117 	return simple_hdmi_build_jack(codec, 0);
3118 }
3119 
3120 static int simple_playback_init(struct hda_codec *codec)
3121 {
3122 	struct hdmi_spec *spec = codec->spec;
3123 	struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
3124 	hda_nid_t pin = per_pin->pin_nid;
3125 
3126 	snd_hda_codec_write(codec, pin, 0,
3127 			    AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
3128 	/* some codecs require to unmute the pin */
3129 	if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
3130 		snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3131 				    AMP_OUT_UNMUTE);
3132 	snd_hda_jack_detect_enable(codec, pin, per_pin->dev_id);
3133 	return 0;
3134 }
3135 
3136 static void simple_playback_free(struct hda_codec *codec)
3137 {
3138 	struct hdmi_spec *spec = codec->spec;
3139 
3140 	hdmi_array_free(spec);
3141 	kfree(spec);
3142 }
3143 
3144 /*
3145  * Nvidia specific implementations
3146  */
3147 
3148 #define Nv_VERB_SET_Channel_Allocation          0xF79
3149 #define Nv_VERB_SET_Info_Frame_Checksum         0xF7A
3150 #define Nv_VERB_SET_Audio_Protection_On         0xF98
3151 #define Nv_VERB_SET_Audio_Protection_Off        0xF99
3152 
3153 #define nvhdmi_master_con_nid_7x	0x04
3154 #define nvhdmi_master_pin_nid_7x	0x05
3155 
3156 static const hda_nid_t nvhdmi_con_nids_7x[4] = {
3157 	/*front, rear, clfe, rear_surr */
3158 	0x6, 0x8, 0xa, 0xc,
3159 };
3160 
3161 static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
3162 	/* set audio protect on */
3163 	{ 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3164 	/* enable digital output on pin widget */
3165 	{ 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3166 	{} /* terminator */
3167 };
3168 
3169 static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
3170 	/* set audio protect on */
3171 	{ 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3172 	/* enable digital output on pin widget */
3173 	{ 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3174 	{ 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3175 	{ 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3176 	{ 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3177 	{ 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3178 	{} /* terminator */
3179 };
3180 
3181 #ifdef LIMITED_RATE_FMT_SUPPORT
3182 /* support only the safe format and rate */
3183 #define SUPPORTED_RATES		SNDRV_PCM_RATE_48000
3184 #define SUPPORTED_MAXBPS	16
3185 #define SUPPORTED_FORMATS	SNDRV_PCM_FMTBIT_S16_LE
3186 #else
3187 /* support all rates and formats */
3188 #define SUPPORTED_RATES \
3189 	(SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
3190 	SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
3191 	 SNDRV_PCM_RATE_192000)
3192 #define SUPPORTED_MAXBPS	24
3193 #define SUPPORTED_FORMATS \
3194 	(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
3195 #endif
3196 
3197 static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
3198 {
3199 	snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
3200 	return 0;
3201 }
3202 
3203 static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
3204 {
3205 	snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
3206 	return 0;
3207 }
3208 
3209 static const unsigned int channels_2_6_8[] = {
3210 	2, 6, 8
3211 };
3212 
3213 static const unsigned int channels_2_8[] = {
3214 	2, 8
3215 };
3216 
3217 static const struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
3218 	.count = ARRAY_SIZE(channels_2_6_8),
3219 	.list = channels_2_6_8,
3220 	.mask = 0,
3221 };
3222 
3223 static const struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
3224 	.count = ARRAY_SIZE(channels_2_8),
3225 	.list = channels_2_8,
3226 	.mask = 0,
3227 };
3228 
3229 static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
3230 				    struct hda_codec *codec,
3231 				    struct snd_pcm_substream *substream)
3232 {
3233 	struct hdmi_spec *spec = codec->spec;
3234 	const struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
3235 
3236 	switch (codec->preset->vendor_id) {
3237 	case 0x10de0002:
3238 	case 0x10de0003:
3239 	case 0x10de0005:
3240 	case 0x10de0006:
3241 		hw_constraints_channels = &hw_constraints_2_8_channels;
3242 		break;
3243 	case 0x10de0007:
3244 		hw_constraints_channels = &hw_constraints_2_6_8_channels;
3245 		break;
3246 	default:
3247 		break;
3248 	}
3249 
3250 	if (hw_constraints_channels != NULL) {
3251 		snd_pcm_hw_constraint_list(substream->runtime, 0,
3252 				SNDRV_PCM_HW_PARAM_CHANNELS,
3253 				hw_constraints_channels);
3254 	} else {
3255 		snd_pcm_hw_constraint_step(substream->runtime, 0,
3256 					   SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3257 	}
3258 
3259 	return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3260 }
3261 
3262 static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
3263 				     struct hda_codec *codec,
3264 				     struct snd_pcm_substream *substream)
3265 {
3266 	struct hdmi_spec *spec = codec->spec;
3267 	return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3268 }
3269 
3270 static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3271 				       struct hda_codec *codec,
3272 				       unsigned int stream_tag,
3273 				       unsigned int format,
3274 				       struct snd_pcm_substream *substream)
3275 {
3276 	struct hdmi_spec *spec = codec->spec;
3277 	return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3278 					     stream_tag, format, substream);
3279 }
3280 
3281 static const struct hda_pcm_stream simple_pcm_playback = {
3282 	.substreams = 1,
3283 	.channels_min = 2,
3284 	.channels_max = 2,
3285 	.ops = {
3286 		.open = simple_playback_pcm_open,
3287 		.close = simple_playback_pcm_close,
3288 		.prepare = simple_playback_pcm_prepare
3289 	},
3290 };
3291 
3292 static const struct hda_codec_ops simple_hdmi_patch_ops = {
3293 	.build_controls = simple_playback_build_controls,
3294 	.build_pcms = simple_playback_build_pcms,
3295 	.init = simple_playback_init,
3296 	.free = simple_playback_free,
3297 	.unsol_event = simple_hdmi_unsol_event,
3298 };
3299 
3300 static int patch_simple_hdmi(struct hda_codec *codec,
3301 			     hda_nid_t cvt_nid, hda_nid_t pin_nid)
3302 {
3303 	struct hdmi_spec *spec;
3304 	struct hdmi_spec_per_cvt *per_cvt;
3305 	struct hdmi_spec_per_pin *per_pin;
3306 
3307 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3308 	if (!spec)
3309 		return -ENOMEM;
3310 
3311 	spec->codec = codec;
3312 	codec->spec = spec;
3313 	hdmi_array_init(spec, 1);
3314 
3315 	spec->multiout.num_dacs = 0;  /* no analog */
3316 	spec->multiout.max_channels = 2;
3317 	spec->multiout.dig_out_nid = cvt_nid;
3318 	spec->num_cvts = 1;
3319 	spec->num_pins = 1;
3320 	per_pin = snd_array_new(&spec->pins);
3321 	per_cvt = snd_array_new(&spec->cvts);
3322 	if (!per_pin || !per_cvt) {
3323 		simple_playback_free(codec);
3324 		return -ENOMEM;
3325 	}
3326 	per_cvt->cvt_nid = cvt_nid;
3327 	per_pin->pin_nid = pin_nid;
3328 	spec->pcm_playback = simple_pcm_playback;
3329 
3330 	codec->patch_ops = simple_hdmi_patch_ops;
3331 
3332 	return 0;
3333 }
3334 
3335 static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
3336 						    int channels)
3337 {
3338 	unsigned int chanmask;
3339 	int chan = channels ? (channels - 1) : 1;
3340 
3341 	switch (channels) {
3342 	default:
3343 	case 0:
3344 	case 2:
3345 		chanmask = 0x00;
3346 		break;
3347 	case 4:
3348 		chanmask = 0x08;
3349 		break;
3350 	case 6:
3351 		chanmask = 0x0b;
3352 		break;
3353 	case 8:
3354 		chanmask = 0x13;
3355 		break;
3356 	}
3357 
3358 	/* Set the audio infoframe channel allocation and checksum fields.  The
3359 	 * channel count is computed implicitly by the hardware. */
3360 	snd_hda_codec_write(codec, 0x1, 0,
3361 			Nv_VERB_SET_Channel_Allocation, chanmask);
3362 
3363 	snd_hda_codec_write(codec, 0x1, 0,
3364 			Nv_VERB_SET_Info_Frame_Checksum,
3365 			(0x71 - chan - chanmask));
3366 }
3367 
3368 static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
3369 				   struct hda_codec *codec,
3370 				   struct snd_pcm_substream *substream)
3371 {
3372 	struct hdmi_spec *spec = codec->spec;
3373 	int i;
3374 
3375 	snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
3376 			0, AC_VERB_SET_CHANNEL_STREAMID, 0);
3377 	for (i = 0; i < 4; i++) {
3378 		/* set the stream id */
3379 		snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3380 				AC_VERB_SET_CHANNEL_STREAMID, 0);
3381 		/* set the stream format */
3382 		snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3383 				AC_VERB_SET_STREAM_FORMAT, 0);
3384 	}
3385 
3386 	/* The audio hardware sends a channel count of 0x7 (8ch) when all the
3387 	 * streams are disabled. */
3388 	nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3389 
3390 	return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3391 }
3392 
3393 static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
3394 				     struct hda_codec *codec,
3395 				     unsigned int stream_tag,
3396 				     unsigned int format,
3397 				     struct snd_pcm_substream *substream)
3398 {
3399 	int chs;
3400 	unsigned int dataDCC2, channel_id;
3401 	int i;
3402 	struct hdmi_spec *spec = codec->spec;
3403 	struct hda_spdif_out *spdif;
3404 	struct hdmi_spec_per_cvt *per_cvt;
3405 
3406 	mutex_lock(&codec->spdif_mutex);
3407 	per_cvt = get_cvt(spec, 0);
3408 	spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
3409 
3410 	chs = substream->runtime->channels;
3411 
3412 	dataDCC2 = 0x2;
3413 
3414 	/* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3415 	if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
3416 		snd_hda_codec_write(codec,
3417 				nvhdmi_master_con_nid_7x,
3418 				0,
3419 				AC_VERB_SET_DIGI_CONVERT_1,
3420 				spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
3421 
3422 	/* set the stream id */
3423 	snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3424 			AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
3425 
3426 	/* set the stream format */
3427 	snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3428 			AC_VERB_SET_STREAM_FORMAT, format);
3429 
3430 	/* turn on again (if needed) */
3431 	/* enable and set the channel status audio/data flag */
3432 	if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
3433 		snd_hda_codec_write(codec,
3434 				nvhdmi_master_con_nid_7x,
3435 				0,
3436 				AC_VERB_SET_DIGI_CONVERT_1,
3437 				spdif->ctls & 0xff);
3438 		snd_hda_codec_write(codec,
3439 				nvhdmi_master_con_nid_7x,
3440 				0,
3441 				AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3442 	}
3443 
3444 	for (i = 0; i < 4; i++) {
3445 		if (chs == 2)
3446 			channel_id = 0;
3447 		else
3448 			channel_id = i * 2;
3449 
3450 		/* turn off SPDIF once;
3451 		 *otherwise the IEC958 bits won't be updated
3452 		 */
3453 		if (codec->spdif_status_reset &&
3454 		(spdif->ctls & AC_DIG1_ENABLE))
3455 			snd_hda_codec_write(codec,
3456 				nvhdmi_con_nids_7x[i],
3457 				0,
3458 				AC_VERB_SET_DIGI_CONVERT_1,
3459 				spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
3460 		/* set the stream id */
3461 		snd_hda_codec_write(codec,
3462 				nvhdmi_con_nids_7x[i],
3463 				0,
3464 				AC_VERB_SET_CHANNEL_STREAMID,
3465 				(stream_tag << 4) | channel_id);
3466 		/* set the stream format */
3467 		snd_hda_codec_write(codec,
3468 				nvhdmi_con_nids_7x[i],
3469 				0,
3470 				AC_VERB_SET_STREAM_FORMAT,
3471 				format);
3472 		/* turn on again (if needed) */
3473 		/* enable and set the channel status audio/data flag */
3474 		if (codec->spdif_status_reset &&
3475 		(spdif->ctls & AC_DIG1_ENABLE)) {
3476 			snd_hda_codec_write(codec,
3477 					nvhdmi_con_nids_7x[i],
3478 					0,
3479 					AC_VERB_SET_DIGI_CONVERT_1,
3480 					spdif->ctls & 0xff);
3481 			snd_hda_codec_write(codec,
3482 					nvhdmi_con_nids_7x[i],
3483 					0,
3484 					AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3485 		}
3486 	}
3487 
3488 	nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
3489 
3490 	mutex_unlock(&codec->spdif_mutex);
3491 	return 0;
3492 }
3493 
3494 static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
3495 	.substreams = 1,
3496 	.channels_min = 2,
3497 	.channels_max = 8,
3498 	.nid = nvhdmi_master_con_nid_7x,
3499 	.rates = SUPPORTED_RATES,
3500 	.maxbps = SUPPORTED_MAXBPS,
3501 	.formats = SUPPORTED_FORMATS,
3502 	.ops = {
3503 		.open = simple_playback_pcm_open,
3504 		.close = nvhdmi_8ch_7x_pcm_close,
3505 		.prepare = nvhdmi_8ch_7x_pcm_prepare
3506 	},
3507 };
3508 
3509 static int patch_nvhdmi_2ch(struct hda_codec *codec)
3510 {
3511 	struct hdmi_spec *spec;
3512 	int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3513 				    nvhdmi_master_pin_nid_7x);
3514 	if (err < 0)
3515 		return err;
3516 
3517 	codec->patch_ops.init = nvhdmi_7x_init_2ch;
3518 	/* override the PCM rates, etc, as the codec doesn't give full list */
3519 	spec = codec->spec;
3520 	spec->pcm_playback.rates = SUPPORTED_RATES;
3521 	spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3522 	spec->pcm_playback.formats = SUPPORTED_FORMATS;
3523 	return 0;
3524 }
3525 
3526 static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3527 {
3528 	struct hdmi_spec *spec = codec->spec;
3529 	int err = simple_playback_build_pcms(codec);
3530 	if (!err) {
3531 		struct hda_pcm *info = get_pcm_rec(spec, 0);
3532 		info->own_chmap = true;
3533 	}
3534 	return err;
3535 }
3536 
3537 static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3538 {
3539 	struct hdmi_spec *spec = codec->spec;
3540 	struct hda_pcm *info;
3541 	struct snd_pcm_chmap *chmap;
3542 	int err;
3543 
3544 	err = simple_playback_build_controls(codec);
3545 	if (err < 0)
3546 		return err;
3547 
3548 	/* add channel maps */
3549 	info = get_pcm_rec(spec, 0);
3550 	err = snd_pcm_add_chmap_ctls(info->pcm,
3551 				     SNDRV_PCM_STREAM_PLAYBACK,
3552 				     snd_pcm_alt_chmaps, 8, 0, &chmap);
3553 	if (err < 0)
3554 		return err;
3555 	switch (codec->preset->vendor_id) {
3556 	case 0x10de0002:
3557 	case 0x10de0003:
3558 	case 0x10de0005:
3559 	case 0x10de0006:
3560 		chmap->channel_mask = (1U << 2) | (1U << 8);
3561 		break;
3562 	case 0x10de0007:
3563 		chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3564 	}
3565 	return 0;
3566 }
3567 
3568 static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3569 {
3570 	struct hdmi_spec *spec;
3571 	int err = patch_nvhdmi_2ch(codec);
3572 	if (err < 0)
3573 		return err;
3574 	spec = codec->spec;
3575 	spec->multiout.max_channels = 8;
3576 	spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
3577 	codec->patch_ops.init = nvhdmi_7x_init_8ch;
3578 	codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3579 	codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
3580 
3581 	/* Initialize the audio infoframe channel mask and checksum to something
3582 	 * valid */
3583 	nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3584 
3585 	return 0;
3586 }
3587 
3588 /*
3589  * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3590  * - 0x10de0015
3591  * - 0x10de0040
3592  */
3593 static int nvhdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
3594 		struct hdac_cea_channel_speaker_allocation *cap, int channels)
3595 {
3596 	if (cap->ca_index == 0x00 && channels == 2)
3597 		return SNDRV_CTL_TLVT_CHMAP_FIXED;
3598 
3599 	/* If the speaker allocation matches the channel count, it is OK. */
3600 	if (cap->channels != channels)
3601 		return -1;
3602 
3603 	/* all channels are remappable freely */
3604 	return SNDRV_CTL_TLVT_CHMAP_VAR;
3605 }
3606 
3607 static int nvhdmi_chmap_validate(struct hdac_chmap *chmap,
3608 		int ca, int chs, unsigned char *map)
3609 {
3610 	if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3611 		return -EINVAL;
3612 
3613 	return 0;
3614 }
3615 
3616 /* map from pin NID to port; port is 0-based */
3617 /* for Nvidia: assume widget NID starting from 4, with step 1 (4, 5, 6, ...) */
3618 static int nvhdmi_pin2port(void *audio_ptr, int pin_nid)
3619 {
3620 	return pin_nid - 4;
3621 }
3622 
3623 /* reverse-map from port to pin NID: see above */
3624 static int nvhdmi_port2pin(struct hda_codec *codec, int port)
3625 {
3626 	return port + 4;
3627 }
3628 
3629 static const struct drm_audio_component_audio_ops nvhdmi_audio_ops = {
3630 	.pin2port = nvhdmi_pin2port,
3631 	.pin_eld_notify = generic_acomp_pin_eld_notify,
3632 	.master_bind = generic_acomp_master_bind,
3633 	.master_unbind = generic_acomp_master_unbind,
3634 };
3635 
3636 static int patch_nvhdmi(struct hda_codec *codec)
3637 {
3638 	struct hdmi_spec *spec;
3639 	int err;
3640 
3641 	err = alloc_generic_hdmi(codec);
3642 	if (err < 0)
3643 		return err;
3644 	codec->dp_mst = true;
3645 
3646 	spec = codec->spec;
3647 	spec->dyn_pcm_assign = true;
3648 
3649 	err = hdmi_parse_codec(codec);
3650 	if (err < 0) {
3651 		generic_spec_free(codec);
3652 		return err;
3653 	}
3654 
3655 	generic_hdmi_init_per_pins(codec);
3656 
3657 	spec->dyn_pin_out = true;
3658 
3659 	spec->chmap.ops.chmap_cea_alloc_validate_get_type =
3660 		nvhdmi_chmap_cea_alloc_validate_get_type;
3661 	spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
3662 
3663 	codec->link_down_at_suspend = 1;
3664 
3665 	generic_acomp_init(codec, &nvhdmi_audio_ops, nvhdmi_port2pin);
3666 
3667 	return 0;
3668 }
3669 
3670 static int patch_nvhdmi_legacy(struct hda_codec *codec)
3671 {
3672 	struct hdmi_spec *spec;
3673 	int err;
3674 
3675 	err = patch_generic_hdmi(codec);
3676 	if (err)
3677 		return err;
3678 
3679 	spec = codec->spec;
3680 	spec->dyn_pin_out = true;
3681 
3682 	spec->chmap.ops.chmap_cea_alloc_validate_get_type =
3683 		nvhdmi_chmap_cea_alloc_validate_get_type;
3684 	spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
3685 
3686 	codec->link_down_at_suspend = 1;
3687 
3688 	return 0;
3689 }
3690 
3691 /*
3692  * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3693  * accessed using vendor-defined verbs. These registers can be used for
3694  * interoperability between the HDA and HDMI drivers.
3695  */
3696 
3697 /* Audio Function Group node */
3698 #define NVIDIA_AFG_NID 0x01
3699 
3700 /*
3701  * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3702  * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3703  * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3704  * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3705  * additional bit (at position 30) to signal the validity of the format.
3706  *
3707  * | 31      | 30    | 29  16 | 15   0 |
3708  * +---------+-------+--------+--------+
3709  * | TRIGGER | VALID | UNUSED | FORMAT |
3710  * +-----------------------------------|
3711  *
3712  * Note that for the trigger bit to take effect it needs to change value
3713  * (i.e. it needs to be toggled).
3714  */
3715 #define NVIDIA_GET_SCRATCH0		0xfa6
3716 #define NVIDIA_SET_SCRATCH0_BYTE0	0xfa7
3717 #define NVIDIA_SET_SCRATCH0_BYTE1	0xfa8
3718 #define NVIDIA_SET_SCRATCH0_BYTE2	0xfa9
3719 #define NVIDIA_SET_SCRATCH0_BYTE3	0xfaa
3720 #define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3721 #define NVIDIA_SCRATCH_VALID   (1 << 6)
3722 
3723 #define NVIDIA_GET_SCRATCH1		0xfab
3724 #define NVIDIA_SET_SCRATCH1_BYTE0	0xfac
3725 #define NVIDIA_SET_SCRATCH1_BYTE1	0xfad
3726 #define NVIDIA_SET_SCRATCH1_BYTE2	0xfae
3727 #define NVIDIA_SET_SCRATCH1_BYTE3	0xfaf
3728 
3729 /*
3730  * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3731  * the format is invalidated so that the HDMI codec can be disabled.
3732  */
3733 static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3734 {
3735 	unsigned int value;
3736 
3737 	/* bits [31:30] contain the trigger and valid bits */
3738 	value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3739 				   NVIDIA_GET_SCRATCH0, 0);
3740 	value = (value >> 24) & 0xff;
3741 
3742 	/* bits [15:0] are used to store the HDA format */
3743 	snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3744 			    NVIDIA_SET_SCRATCH0_BYTE0,
3745 			    (format >> 0) & 0xff);
3746 	snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3747 			    NVIDIA_SET_SCRATCH0_BYTE1,
3748 			    (format >> 8) & 0xff);
3749 
3750 	/* bits [16:24] are unused */
3751 	snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3752 			    NVIDIA_SET_SCRATCH0_BYTE2, 0);
3753 
3754 	/*
3755 	 * Bit 30 signals that the data is valid and hence that HDMI audio can
3756 	 * be enabled.
3757 	 */
3758 	if (format == 0)
3759 		value &= ~NVIDIA_SCRATCH_VALID;
3760 	else
3761 		value |= NVIDIA_SCRATCH_VALID;
3762 
3763 	/*
3764 	 * Whenever the trigger bit is toggled, an interrupt is raised in the
3765 	 * HDMI codec. The HDMI driver will use that as trigger to update its
3766 	 * configuration.
3767 	 */
3768 	value ^= NVIDIA_SCRATCH_TRIGGER;
3769 
3770 	snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3771 			    NVIDIA_SET_SCRATCH0_BYTE3, value);
3772 }
3773 
3774 static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3775 				  struct hda_codec *codec,
3776 				  unsigned int stream_tag,
3777 				  unsigned int format,
3778 				  struct snd_pcm_substream *substream)
3779 {
3780 	int err;
3781 
3782 	err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3783 						format, substream);
3784 	if (err < 0)
3785 		return err;
3786 
3787 	/* notify the HDMI codec of the format change */
3788 	tegra_hdmi_set_format(codec, format);
3789 
3790 	return 0;
3791 }
3792 
3793 static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3794 				  struct hda_codec *codec,
3795 				  struct snd_pcm_substream *substream)
3796 {
3797 	/* invalidate the format in the HDMI codec */
3798 	tegra_hdmi_set_format(codec, 0);
3799 
3800 	return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3801 }
3802 
3803 static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3804 {
3805 	struct hdmi_spec *spec = codec->spec;
3806 	unsigned int i;
3807 
3808 	for (i = 0; i < spec->num_pins; i++) {
3809 		struct hda_pcm *pcm = get_pcm_rec(spec, i);
3810 
3811 		if (pcm->pcm_type == type)
3812 			return pcm;
3813 	}
3814 
3815 	return NULL;
3816 }
3817 
3818 static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3819 {
3820 	struct hda_pcm_stream *stream;
3821 	struct hda_pcm *pcm;
3822 	int err;
3823 
3824 	err = generic_hdmi_build_pcms(codec);
3825 	if (err < 0)
3826 		return err;
3827 
3828 	pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3829 	if (!pcm)
3830 		return -ENODEV;
3831 
3832 	/*
3833 	 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3834 	 * codec about format changes.
3835 	 */
3836 	stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3837 	stream->ops.prepare = tegra_hdmi_pcm_prepare;
3838 	stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3839 
3840 	return 0;
3841 }
3842 
3843 static int patch_tegra_hdmi(struct hda_codec *codec)
3844 {
3845 	struct hdmi_spec *spec;
3846 	int err;
3847 
3848 	err = patch_generic_hdmi(codec);
3849 	if (err)
3850 		return err;
3851 
3852 	codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3853 	spec = codec->spec;
3854 	spec->chmap.ops.chmap_cea_alloc_validate_get_type =
3855 		nvhdmi_chmap_cea_alloc_validate_get_type;
3856 	spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
3857 
3858 	return 0;
3859 }
3860 
3861 /*
3862  * ATI/AMD-specific implementations
3863  */
3864 
3865 #define is_amdhdmi_rev3_or_later(codec) \
3866 	((codec)->core.vendor_id == 0x1002aa01 && \
3867 	 ((codec)->core.revision_id & 0xff00) >= 0x0300)
3868 #define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
3869 
3870 /* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3871 #define ATI_VERB_SET_CHANNEL_ALLOCATION	0x771
3872 #define ATI_VERB_SET_DOWNMIX_INFO	0x772
3873 #define ATI_VERB_SET_MULTICHANNEL_01	0x777
3874 #define ATI_VERB_SET_MULTICHANNEL_23	0x778
3875 #define ATI_VERB_SET_MULTICHANNEL_45	0x779
3876 #define ATI_VERB_SET_MULTICHANNEL_67	0x77a
3877 #define ATI_VERB_SET_HBR_CONTROL	0x77c
3878 #define ATI_VERB_SET_MULTICHANNEL_1	0x785
3879 #define ATI_VERB_SET_MULTICHANNEL_3	0x786
3880 #define ATI_VERB_SET_MULTICHANNEL_5	0x787
3881 #define ATI_VERB_SET_MULTICHANNEL_7	0x788
3882 #define ATI_VERB_SET_MULTICHANNEL_MODE	0x789
3883 #define ATI_VERB_GET_CHANNEL_ALLOCATION	0xf71
3884 #define ATI_VERB_GET_DOWNMIX_INFO	0xf72
3885 #define ATI_VERB_GET_MULTICHANNEL_01	0xf77
3886 #define ATI_VERB_GET_MULTICHANNEL_23	0xf78
3887 #define ATI_VERB_GET_MULTICHANNEL_45	0xf79
3888 #define ATI_VERB_GET_MULTICHANNEL_67	0xf7a
3889 #define ATI_VERB_GET_HBR_CONTROL	0xf7c
3890 #define ATI_VERB_GET_MULTICHANNEL_1	0xf85
3891 #define ATI_VERB_GET_MULTICHANNEL_3	0xf86
3892 #define ATI_VERB_GET_MULTICHANNEL_5	0xf87
3893 #define ATI_VERB_GET_MULTICHANNEL_7	0xf88
3894 #define ATI_VERB_GET_MULTICHANNEL_MODE	0xf89
3895 
3896 /* AMD specific HDA cvt verbs */
3897 #define ATI_VERB_SET_RAMP_RATE		0x770
3898 #define ATI_VERB_GET_RAMP_RATE		0xf70
3899 
3900 #define ATI_OUT_ENABLE 0x1
3901 
3902 #define ATI_MULTICHANNEL_MODE_PAIRED	0
3903 #define ATI_MULTICHANNEL_MODE_SINGLE	1
3904 
3905 #define ATI_HBR_CAPABLE 0x01
3906 #define ATI_HBR_ENABLE 0x10
3907 
3908 static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3909 			       int dev_id, unsigned char *buf, int *eld_size)
3910 {
3911 	WARN_ON(dev_id != 0);
3912 	/* call hda_eld.c ATI/AMD-specific function */
3913 	return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3914 				    is_amdhdmi_rev3_or_later(codec));
3915 }
3916 
3917 static void atihdmi_pin_setup_infoframe(struct hda_codec *codec,
3918 					hda_nid_t pin_nid, int dev_id, int ca,
3919 					int active_channels, int conn_type)
3920 {
3921 	WARN_ON(dev_id != 0);
3922 	snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3923 }
3924 
3925 static int atihdmi_paired_swap_fc_lfe(int pos)
3926 {
3927 	/*
3928 	 * ATI/AMD have automatic FC/LFE swap built-in
3929 	 * when in pairwise mapping mode.
3930 	 */
3931 
3932 	switch (pos) {
3933 		/* see channel_allocations[].speakers[] */
3934 		case 2: return 3;
3935 		case 3: return 2;
3936 		default: break;
3937 	}
3938 
3939 	return pos;
3940 }
3941 
3942 static int atihdmi_paired_chmap_validate(struct hdac_chmap *chmap,
3943 			int ca, int chs, unsigned char *map)
3944 {
3945 	struct hdac_cea_channel_speaker_allocation *cap;
3946 	int i, j;
3947 
3948 	/* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3949 
3950 	cap = snd_hdac_get_ch_alloc_from_ca(ca);
3951 	for (i = 0; i < chs; ++i) {
3952 		int mask = snd_hdac_chmap_to_spk_mask(map[i]);
3953 		bool ok = false;
3954 		bool companion_ok = false;
3955 
3956 		if (!mask)
3957 			continue;
3958 
3959 		for (j = 0 + i % 2; j < 8; j += 2) {
3960 			int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3961 			if (cap->speakers[chan_idx] == mask) {
3962 				/* channel is in a supported position */
3963 				ok = true;
3964 
3965 				if (i % 2 == 0 && i + 1 < chs) {
3966 					/* even channel, check the odd companion */
3967 					int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
3968 					int comp_mask_req = snd_hdac_chmap_to_spk_mask(map[i+1]);
3969 					int comp_mask_act = cap->speakers[comp_chan_idx];
3970 
3971 					if (comp_mask_req == comp_mask_act)
3972 						companion_ok = true;
3973 					else
3974 						return -EINVAL;
3975 				}
3976 				break;
3977 			}
3978 		}
3979 
3980 		if (!ok)
3981 			return -EINVAL;
3982 
3983 		if (companion_ok)
3984 			i++; /* companion channel already checked */
3985 	}
3986 
3987 	return 0;
3988 }
3989 
3990 static int atihdmi_pin_set_slot_channel(struct hdac_device *hdac,
3991 		hda_nid_t pin_nid, int hdmi_slot, int stream_channel)
3992 {
3993 	struct hda_codec *codec = hdac_to_hda_codec(hdac);
3994 	int verb;
3995 	int ati_channel_setup = 0;
3996 
3997 	if (hdmi_slot > 7)
3998 		return -EINVAL;
3999 
4000 	if (!has_amd_full_remap_support(codec)) {
4001 		hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
4002 
4003 		/* In case this is an odd slot but without stream channel, do not
4004 		 * disable the slot since the corresponding even slot could have a
4005 		 * channel. In case neither have a channel, the slot pair will be
4006 		 * disabled when this function is called for the even slot. */
4007 		if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
4008 			return 0;
4009 
4010 		hdmi_slot -= hdmi_slot % 2;
4011 
4012 		if (stream_channel != 0xf)
4013 			stream_channel -= stream_channel % 2;
4014 	}
4015 
4016 	verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
4017 
4018 	/* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
4019 
4020 	if (stream_channel != 0xf)
4021 		ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
4022 
4023 	return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
4024 }
4025 
4026 static int atihdmi_pin_get_slot_channel(struct hdac_device *hdac,
4027 				hda_nid_t pin_nid, int asp_slot)
4028 {
4029 	struct hda_codec *codec = hdac_to_hda_codec(hdac);
4030 	bool was_odd = false;
4031 	int ati_asp_slot = asp_slot;
4032 	int verb;
4033 	int ati_channel_setup;
4034 
4035 	if (asp_slot > 7)
4036 		return -EINVAL;
4037 
4038 	if (!has_amd_full_remap_support(codec)) {
4039 		ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
4040 		if (ati_asp_slot % 2 != 0) {
4041 			ati_asp_slot -= 1;
4042 			was_odd = true;
4043 		}
4044 	}
4045 
4046 	verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
4047 
4048 	ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
4049 
4050 	if (!(ati_channel_setup & ATI_OUT_ENABLE))
4051 		return 0xf;
4052 
4053 	return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
4054 }
4055 
4056 static int atihdmi_paired_chmap_cea_alloc_validate_get_type(
4057 		struct hdac_chmap *chmap,
4058 		struct hdac_cea_channel_speaker_allocation *cap,
4059 		int channels)
4060 {
4061 	int c;
4062 
4063 	/*
4064 	 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
4065 	 * we need to take that into account (a single channel may take 2
4066 	 * channel slots if we need to carry a silent channel next to it).
4067 	 * On Rev3+ AMD codecs this function is not used.
4068 	 */
4069 	int chanpairs = 0;
4070 
4071 	/* We only produce even-numbered channel count TLVs */
4072 	if ((channels % 2) != 0)
4073 		return -1;
4074 
4075 	for (c = 0; c < 7; c += 2) {
4076 		if (cap->speakers[c] || cap->speakers[c+1])
4077 			chanpairs++;
4078 	}
4079 
4080 	if (chanpairs * 2 != channels)
4081 		return -1;
4082 
4083 	return SNDRV_CTL_TLVT_CHMAP_PAIRED;
4084 }
4085 
4086 static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
4087 		struct hdac_cea_channel_speaker_allocation *cap,
4088 		unsigned int *chmap, int channels)
4089 {
4090 	/* produce paired maps for pre-rev3 ATI/AMD codecs */
4091 	int count = 0;
4092 	int c;
4093 
4094 	for (c = 7; c >= 0; c--) {
4095 		int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
4096 		int spk = cap->speakers[chan];
4097 		if (!spk) {
4098 			/* add N/A channel if the companion channel is occupied */
4099 			if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
4100 				chmap[count++] = SNDRV_CHMAP_NA;
4101 
4102 			continue;
4103 		}
4104 
4105 		chmap[count++] = snd_hdac_spk_to_chmap(spk);
4106 	}
4107 
4108 	WARN_ON(count != channels);
4109 }
4110 
4111 static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
4112 				 int dev_id, bool hbr)
4113 {
4114 	int hbr_ctl, hbr_ctl_new;
4115 
4116 	WARN_ON(dev_id != 0);
4117 
4118 	hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
4119 	if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
4120 		if (hbr)
4121 			hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
4122 		else
4123 			hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
4124 
4125 		codec_dbg(codec,
4126 			  "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
4127 				pin_nid,
4128 				hbr_ctl == hbr_ctl_new ? "" : "new-",
4129 				hbr_ctl_new);
4130 
4131 		if (hbr_ctl != hbr_ctl_new)
4132 			snd_hda_codec_write(codec, pin_nid, 0,
4133 						ATI_VERB_SET_HBR_CONTROL,
4134 						hbr_ctl_new);
4135 
4136 	} else if (hbr)
4137 		return -EINVAL;
4138 
4139 	return 0;
4140 }
4141 
4142 static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
4143 				hda_nid_t pin_nid, int dev_id,
4144 				u32 stream_tag, int format)
4145 {
4146 	if (is_amdhdmi_rev3_or_later(codec)) {
4147 		int ramp_rate = 180; /* default as per AMD spec */
4148 		/* disable ramp-up/down for non-pcm as per AMD spec */
4149 		if (format & AC_FMT_TYPE_NON_PCM)
4150 			ramp_rate = 0;
4151 
4152 		snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
4153 	}
4154 
4155 	return hdmi_setup_stream(codec, cvt_nid, pin_nid, dev_id,
4156 				 stream_tag, format);
4157 }
4158 
4159 
4160 static int atihdmi_init(struct hda_codec *codec)
4161 {
4162 	struct hdmi_spec *spec = codec->spec;
4163 	int pin_idx, err;
4164 
4165 	err = generic_hdmi_init(codec);
4166 
4167 	if (err)
4168 		return err;
4169 
4170 	for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
4171 		struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
4172 
4173 		/* make sure downmix information in infoframe is zero */
4174 		snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
4175 
4176 		/* enable channel-wise remap mode if supported */
4177 		if (has_amd_full_remap_support(codec))
4178 			snd_hda_codec_write(codec, per_pin->pin_nid, 0,
4179 					    ATI_VERB_SET_MULTICHANNEL_MODE,
4180 					    ATI_MULTICHANNEL_MODE_SINGLE);
4181 	}
4182 	codec->auto_runtime_pm = 1;
4183 
4184 	return 0;
4185 }
4186 
4187 /* map from pin NID to port; port is 0-based */
4188 /* for AMD: assume widget NID starting from 3, with step 2 (3, 5, 7, ...) */
4189 static int atihdmi_pin2port(void *audio_ptr, int pin_nid)
4190 {
4191 	return pin_nid / 2 - 1;
4192 }
4193 
4194 /* reverse-map from port to pin NID: see above */
4195 static int atihdmi_port2pin(struct hda_codec *codec, int port)
4196 {
4197 	return port * 2 + 3;
4198 }
4199 
4200 static const struct drm_audio_component_audio_ops atihdmi_audio_ops = {
4201 	.pin2port = atihdmi_pin2port,
4202 	.pin_eld_notify = generic_acomp_pin_eld_notify,
4203 	.master_bind = generic_acomp_master_bind,
4204 	.master_unbind = generic_acomp_master_unbind,
4205 };
4206 
4207 static int patch_atihdmi(struct hda_codec *codec)
4208 {
4209 	struct hdmi_spec *spec;
4210 	struct hdmi_spec_per_cvt *per_cvt;
4211 	int err, cvt_idx;
4212 
4213 	err = patch_generic_hdmi(codec);
4214 
4215 	if (err)
4216 		return err;
4217 
4218 	codec->patch_ops.init = atihdmi_init;
4219 
4220 	spec = codec->spec;
4221 
4222 	spec->ops.pin_get_eld = atihdmi_pin_get_eld;
4223 	spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
4224 	spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
4225 	spec->ops.setup_stream = atihdmi_setup_stream;
4226 
4227 	spec->chmap.ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
4228 	spec->chmap.ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
4229 
4230 	if (!has_amd_full_remap_support(codec)) {
4231 		/* override to ATI/AMD-specific versions with pairwise mapping */
4232 		spec->chmap.ops.chmap_cea_alloc_validate_get_type =
4233 			atihdmi_paired_chmap_cea_alloc_validate_get_type;
4234 		spec->chmap.ops.cea_alloc_to_tlv_chmap =
4235 				atihdmi_paired_cea_alloc_to_tlv_chmap;
4236 		spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate;
4237 	}
4238 
4239 	/* ATI/AMD converters do not advertise all of their capabilities */
4240 	for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
4241 		per_cvt = get_cvt(spec, cvt_idx);
4242 		per_cvt->channels_max = max(per_cvt->channels_max, 8u);
4243 		per_cvt->rates |= SUPPORTED_RATES;
4244 		per_cvt->formats |= SUPPORTED_FORMATS;
4245 		per_cvt->maxbps = max(per_cvt->maxbps, 24u);
4246 	}
4247 
4248 	spec->chmap.channels_max = max(spec->chmap.channels_max, 8u);
4249 
4250 	/* AMD GPUs have neither EPSS nor CLKSTOP bits, hence preventing
4251 	 * the link-down as is.  Tell the core to allow it.
4252 	 */
4253 	codec->link_down_at_suspend = 1;
4254 
4255 	generic_acomp_init(codec, &atihdmi_audio_ops, atihdmi_port2pin);
4256 
4257 	return 0;
4258 }
4259 
4260 /* VIA HDMI Implementation */
4261 #define VIAHDMI_CVT_NID	0x02	/* audio converter1 */
4262 #define VIAHDMI_PIN_NID	0x03	/* HDMI output pin1 */
4263 
4264 static int patch_via_hdmi(struct hda_codec *codec)
4265 {
4266 	return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
4267 }
4268 
4269 /*
4270  * patch entries
4271  */
4272 static const struct hda_device_id snd_hda_id_hdmi[] = {
4273 HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI",	patch_atihdmi),
4274 HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI",	patch_atihdmi),
4275 HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI",	patch_atihdmi),
4276 HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI",	patch_atihdmi),
4277 HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI",	patch_generic_hdmi),
4278 HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI",	patch_generic_hdmi),
4279 HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI",	patch_generic_hdmi),
4280 HDA_CODEC_ENTRY(0x10de0001, "MCP73 HDMI",	patch_nvhdmi_2ch),
4281 HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI",	patch_nvhdmi_8ch_7x),
4282 HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI",	patch_nvhdmi_8ch_7x),
4283 HDA_CODEC_ENTRY(0x10de0004, "GPU 04 HDMI",	patch_nvhdmi_8ch_7x),
4284 HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI",	patch_nvhdmi_8ch_7x),
4285 HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI",	patch_nvhdmi_8ch_7x),
4286 HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI",	patch_nvhdmi_8ch_7x),
4287 HDA_CODEC_ENTRY(0x10de0008, "GPU 08 HDMI/DP",	patch_nvhdmi_legacy),
4288 HDA_CODEC_ENTRY(0x10de0009, "GPU 09 HDMI/DP",	patch_nvhdmi_legacy),
4289 HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP",	patch_nvhdmi_legacy),
4290 HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP",	patch_nvhdmi_legacy),
4291 HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI",	patch_nvhdmi_legacy),
4292 HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP",	patch_nvhdmi_legacy),
4293 HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP",	patch_nvhdmi_legacy),
4294 HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP",	patch_nvhdmi_legacy),
4295 HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP",	patch_nvhdmi_legacy),
4296 HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP",	patch_nvhdmi_legacy),
4297 HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP",	patch_nvhdmi_legacy),
4298 HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP",	patch_nvhdmi_legacy),
4299 HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP",	patch_nvhdmi_legacy),
4300 /* 17 is known to be absent */
4301 HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP",	patch_nvhdmi_legacy),
4302 HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP",	patch_nvhdmi_legacy),
4303 HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP",	patch_nvhdmi_legacy),
4304 HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP",	patch_nvhdmi_legacy),
4305 HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP",	patch_nvhdmi_legacy),
4306 HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI",	patch_tegra_hdmi),
4307 HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI",	patch_tegra_hdmi),
4308 HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI",	patch_tegra_hdmi),
4309 HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP",	patch_tegra_hdmi),
4310 HDA_CODEC_ENTRY(0x10de002d, "Tegra186 HDMI/DP0", patch_tegra_hdmi),
4311 HDA_CODEC_ENTRY(0x10de002e, "Tegra186 HDMI/DP1", patch_tegra_hdmi),
4312 HDA_CODEC_ENTRY(0x10de002f, "Tegra194 HDMI/DP2", patch_tegra_hdmi),
4313 HDA_CODEC_ENTRY(0x10de0030, "Tegra194 HDMI/DP3", patch_tegra_hdmi),
4314 HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP",	patch_nvhdmi),
4315 HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP",	patch_nvhdmi),
4316 HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP",	patch_nvhdmi),
4317 HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP",	patch_nvhdmi),
4318 HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP",	patch_nvhdmi),
4319 HDA_CODEC_ENTRY(0x10de0045, "GPU 45 HDMI/DP",	patch_nvhdmi),
4320 HDA_CODEC_ENTRY(0x10de0050, "GPU 50 HDMI/DP",	patch_nvhdmi),
4321 HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP",	patch_nvhdmi),
4322 HDA_CODEC_ENTRY(0x10de0052, "GPU 52 HDMI/DP",	patch_nvhdmi),
4323 HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP",	patch_nvhdmi),
4324 HDA_CODEC_ENTRY(0x10de0061, "GPU 61 HDMI/DP",	patch_nvhdmi),
4325 HDA_CODEC_ENTRY(0x10de0062, "GPU 62 HDMI/DP",	patch_nvhdmi),
4326 HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI",	patch_nvhdmi_2ch),
4327 HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP",	patch_nvhdmi),
4328 HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP",	patch_nvhdmi),
4329 HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP",	patch_nvhdmi),
4330 HDA_CODEC_ENTRY(0x10de0073, "GPU 73 HDMI/DP",	patch_nvhdmi),
4331 HDA_CODEC_ENTRY(0x10de0074, "GPU 74 HDMI/DP",	patch_nvhdmi),
4332 HDA_CODEC_ENTRY(0x10de0076, "GPU 76 HDMI/DP",	patch_nvhdmi),
4333 HDA_CODEC_ENTRY(0x10de007b, "GPU 7b HDMI/DP",	patch_nvhdmi),
4334 HDA_CODEC_ENTRY(0x10de007c, "GPU 7c HDMI/DP",	patch_nvhdmi),
4335 HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP",	patch_nvhdmi),
4336 HDA_CODEC_ENTRY(0x10de007e, "GPU 7e HDMI/DP",	patch_nvhdmi),
4337 HDA_CODEC_ENTRY(0x10de0080, "GPU 80 HDMI/DP",	patch_nvhdmi),
4338 HDA_CODEC_ENTRY(0x10de0081, "GPU 81 HDMI/DP",	patch_nvhdmi),
4339 HDA_CODEC_ENTRY(0x10de0082, "GPU 82 HDMI/DP",	patch_nvhdmi),
4340 HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP",	patch_nvhdmi),
4341 HDA_CODEC_ENTRY(0x10de0084, "GPU 84 HDMI/DP",	patch_nvhdmi),
4342 HDA_CODEC_ENTRY(0x10de0090, "GPU 90 HDMI/DP",	patch_nvhdmi),
4343 HDA_CODEC_ENTRY(0x10de0091, "GPU 91 HDMI/DP",	patch_nvhdmi),
4344 HDA_CODEC_ENTRY(0x10de0092, "GPU 92 HDMI/DP",	patch_nvhdmi),
4345 HDA_CODEC_ENTRY(0x10de0093, "GPU 93 HDMI/DP",	patch_nvhdmi),
4346 HDA_CODEC_ENTRY(0x10de0094, "GPU 94 HDMI/DP",	patch_nvhdmi),
4347 HDA_CODEC_ENTRY(0x10de0095, "GPU 95 HDMI/DP",	patch_nvhdmi),
4348 HDA_CODEC_ENTRY(0x10de0097, "GPU 97 HDMI/DP",	patch_nvhdmi),
4349 HDA_CODEC_ENTRY(0x10de0098, "GPU 98 HDMI/DP",	patch_nvhdmi),
4350 HDA_CODEC_ENTRY(0x10de0099, "GPU 99 HDMI/DP",	patch_nvhdmi),
4351 HDA_CODEC_ENTRY(0x10de009a, "GPU 9a HDMI/DP",	patch_nvhdmi),
4352 HDA_CODEC_ENTRY(0x10de009d, "GPU 9d HDMI/DP",	patch_nvhdmi),
4353 HDA_CODEC_ENTRY(0x10de009e, "GPU 9e HDMI/DP",	patch_nvhdmi),
4354 HDA_CODEC_ENTRY(0x10de009f, "GPU 9f HDMI/DP",	patch_nvhdmi),
4355 HDA_CODEC_ENTRY(0x10de00a0, "GPU a0 HDMI/DP",	patch_nvhdmi),
4356 HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI",	patch_nvhdmi_2ch),
4357 HDA_CODEC_ENTRY(0x10de8067, "MCP67/68 HDMI",	patch_nvhdmi_2ch),
4358 HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP",	patch_via_hdmi),
4359 HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP",	patch_via_hdmi),
4360 HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP",	patch_generic_hdmi),
4361 HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP",	patch_generic_hdmi),
4362 HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI",	patch_i915_cpt_hdmi),
4363 HDA_CODEC_ENTRY(0x80862800, "Geminilake HDMI",	patch_i915_glk_hdmi),
4364 HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI",	patch_generic_hdmi),
4365 HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI",	patch_generic_hdmi),
4366 HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI",	patch_generic_hdmi),
4367 HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI",	patch_i915_cpt_hdmi),
4368 HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI",	patch_i915_cpt_hdmi),
4369 HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_i915_cpt_hdmi),
4370 HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI",	patch_i915_hsw_hdmi),
4371 HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI",	patch_i915_hsw_hdmi),
4372 HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI",	patch_i915_hsw_hdmi),
4373 HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI",	patch_i915_hsw_hdmi),
4374 HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI",	patch_i915_hsw_hdmi),
4375 HDA_CODEC_ENTRY(0x8086280c, "Cannonlake HDMI",	patch_i915_glk_hdmi),
4376 HDA_CODEC_ENTRY(0x8086280d, "Geminilake HDMI",	patch_i915_glk_hdmi),
4377 HDA_CODEC_ENTRY(0x8086280f, "Icelake HDMI",	patch_i915_icl_hdmi),
4378 HDA_CODEC_ENTRY(0x80862812, "Tigerlake HDMI",	patch_i915_tgl_hdmi),
4379 HDA_CODEC_ENTRY(0x80862814, "DG1 HDMI",	patch_i915_tgl_hdmi),
4380 HDA_CODEC_ENTRY(0x80862815, "Alderlake HDMI",	patch_i915_tgl_hdmi),
4381 HDA_CODEC_ENTRY(0x8086281c, "Alderlake-P HDMI", patch_i915_tgl_hdmi),
4382 HDA_CODEC_ENTRY(0x80862816, "Rocketlake HDMI",	patch_i915_tgl_hdmi),
4383 HDA_CODEC_ENTRY(0x8086281a, "Jasperlake HDMI",	patch_i915_icl_hdmi),
4384 HDA_CODEC_ENTRY(0x8086281b, "Elkhartlake HDMI",	patch_i915_icl_hdmi),
4385 HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI",	patch_generic_hdmi),
4386 HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI",	patch_i915_byt_hdmi),
4387 HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI",	patch_i915_byt_hdmi),
4388 HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI",	patch_generic_hdmi),
4389 /* special ID for generic HDMI */
4390 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
4391 {} /* terminator */
4392 };
4393 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
4394 
4395 MODULE_LICENSE("GPL");
4396 MODULE_DESCRIPTION("HDMI HD-audio codec");
4397 MODULE_ALIAS("snd-hda-codec-intelhdmi");
4398 MODULE_ALIAS("snd-hda-codec-nvhdmi");
4399 MODULE_ALIAS("snd-hda-codec-atihdmi");
4400 
4401 static struct hda_codec_driver hdmi_driver = {
4402 	.id = snd_hda_id_hdmi,
4403 };
4404 
4405 module_hda_codec_driver(hdmi_driver);
4406