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