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