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