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