ALSA: hda: Fix regression by strip mask fix
[sfrench/cifs-2.6.git] / sound / pci / hda / patch_hdmi.c
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 struct hdmi_spec_per_cvt {
42         hda_nid_t cvt_nid;
43         int assigned;
44         unsigned int channels_min;
45         unsigned int channels_max;
46         u32 rates;
47         u64 formats;
48         unsigned int maxbps;
49 };
50
51 /* max. connections to a widget */
52 #define HDA_MAX_CONNECTIONS     32
53
54 struct hdmi_spec_per_pin {
55         hda_nid_t pin_nid;
56         int dev_id;
57         /* pin idx, different device entries on the same pin use the same idx */
58         int pin_nid_idx;
59         int num_mux_nids;
60         hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
61         int mux_idx;
62         hda_nid_t cvt_nid;
63
64         struct hda_codec *codec;
65         struct hdmi_eld sink_eld;
66         struct mutex lock;
67         struct delayed_work work;
68         struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
69         int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
70         int repoll_count;
71         bool setup; /* the stream has been set up by prepare callback */
72         int channels; /* current number of channels */
73         bool non_pcm;
74         bool chmap_set;         /* channel-map override by ALSA API? */
75         unsigned char chmap[8]; /* ALSA API channel-map */
76 #ifdef CONFIG_SND_PROC_FS
77         struct snd_info_entry *proc_entry;
78 #endif
79 };
80
81 /* operations used by generic code that can be overridden by patches */
82 struct hdmi_ops {
83         int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
84                            int dev_id, unsigned char *buf, int *eld_size);
85
86         void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
87                                     int dev_id,
88                                     int ca, int active_channels, int conn_type);
89
90         /* enable/disable HBR (HD passthrough) */
91         int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid,
92                              int dev_id, bool hbr);
93
94         int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
95                             hda_nid_t pin_nid, int dev_id, u32 stream_tag,
96                             int format);
97
98         void (*pin_cvt_fixup)(struct hda_codec *codec,
99                               struct hdmi_spec_per_pin *per_pin,
100                               hda_nid_t cvt_nid);
101 };
102
103 struct hdmi_pcm {
104         struct hda_pcm *pcm;
105         struct snd_jack *jack;
106         struct snd_kcontrol *eld_ctl;
107 };
108
109 struct hdmi_spec {
110         struct hda_codec *codec;
111         int num_cvts;
112         struct snd_array cvts; /* struct hdmi_spec_per_cvt */
113         hda_nid_t cvt_nids[4]; /* only for haswell fix */
114
115         /*
116          * num_pins is the number of virtual pins
117          * for example, there are 3 pins, and each pin
118          * has 4 device entries, then the num_pins is 12
119          */
120         int num_pins;
121         /*
122          * num_nids is the number of real pins
123          * In the above example, num_nids is 3
124          */
125         int num_nids;
126         /*
127          * dev_num is the number of device entries
128          * on each pin.
129          * In the above example, dev_num is 4
130          */
131         int dev_num;
132         struct snd_array pins; /* struct hdmi_spec_per_pin */
133         struct hdmi_pcm pcm_rec[16];
134         struct mutex pcm_lock;
135         struct mutex bind_lock; /* for audio component binding */
136         /* pcm_bitmap means which pcms have been assigned to pins*/
137         unsigned long pcm_bitmap;
138         int pcm_used;   /* counter of pcm_rec[] */
139         /* bitmap shows whether the pcm is opened in user space
140          * bit 0 means the first playback PCM (PCM3);
141          * bit 1 means the second playback PCM, and so on.
142          */
143         unsigned long pcm_in_use;
144
145         struct hdmi_eld temp_eld;
146         struct hdmi_ops ops;
147
148         bool dyn_pin_out;
149         bool dyn_pcm_assign;
150         bool intel_hsw_fixup;   /* apply Intel platform-specific fixups */
151         /*
152          * Non-generic VIA/NVIDIA specific
153          */
154         struct hda_multi_out multiout;
155         struct hda_pcm_stream pcm_playback;
156
157         bool use_jack_detect; /* jack detection enabled */
158         bool use_acomp_notifier; /* use eld_notify callback for hotplug */
159         bool acomp_registered; /* audio component registered in this driver */
160         struct drm_audio_component_audio_ops drm_audio_ops;
161         int (*port2pin)(struct hda_codec *, int); /* reverse port/pin mapping */
162
163         struct hdac_chmap chmap;
164         hda_nid_t vendor_nid;
165         const int *port_map;
166         int port_num;
167 };
168
169 #ifdef CONFIG_SND_HDA_COMPONENT
170 static inline bool codec_has_acomp(struct hda_codec *codec)
171 {
172         struct hdmi_spec *spec = codec->spec;
173         return spec->use_acomp_notifier;
174 }
175 #else
176 #define codec_has_acomp(codec)  false
177 #endif
178
179 struct hdmi_audio_infoframe {
180         u8 type; /* 0x84 */
181         u8 ver;  /* 0x01 */
182         u8 len;  /* 0x0a */
183
184         u8 checksum;
185
186         u8 CC02_CT47;   /* CC in bits 0:2, CT in 4:7 */
187         u8 SS01_SF24;
188         u8 CXT04;
189         u8 CA;
190         u8 LFEPBL01_LSV36_DM_INH7;
191 };
192
193 struct dp_audio_infoframe {
194         u8 type; /* 0x84 */
195         u8 len;  /* 0x1b */
196         u8 ver;  /* 0x11 << 2 */
197
198         u8 CC02_CT47;   /* match with HDMI infoframe from this on */
199         u8 SS01_SF24;
200         u8 CXT04;
201         u8 CA;
202         u8 LFEPBL01_LSV36_DM_INH7;
203 };
204
205 union audio_infoframe {
206         struct hdmi_audio_infoframe hdmi;
207         struct dp_audio_infoframe dp;
208         u8 bytes[0];
209 };
210
211 /*
212  * HDMI routines
213  */
214
215 #define get_pin(spec, idx) \
216         ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
217 #define get_cvt(spec, idx) \
218         ((struct hdmi_spec_per_cvt  *)snd_array_elem(&spec->cvts, idx))
219 /* obtain hdmi_pcm object assigned to idx */
220 #define get_hdmi_pcm(spec, idx) (&(spec)->pcm_rec[idx])
221 /* obtain hda_pcm object assigned to idx */
222 #define get_pcm_rec(spec, idx)  (get_hdmi_pcm(spec, idx)->pcm)
223
224 static int pin_id_to_pin_index(struct hda_codec *codec,
225                                hda_nid_t pin_nid, int dev_id)
226 {
227         struct hdmi_spec *spec = codec->spec;
228         int pin_idx;
229         struct hdmi_spec_per_pin *per_pin;
230
231         /*
232          * (dev_id == -1) means it is NON-MST pin
233          * return the first virtual pin on this port
234          */
235         if (dev_id == -1)
236                 dev_id = 0;
237
238         for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
239                 per_pin = get_pin(spec, pin_idx);
240                 if ((per_pin->pin_nid == pin_nid) &&
241                         (per_pin->dev_id == dev_id))
242                         return pin_idx;
243         }
244
245         codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
246         return -EINVAL;
247 }
248
249 static int hinfo_to_pcm_index(struct hda_codec *codec,
250                         struct hda_pcm_stream *hinfo)
251 {
252         struct hdmi_spec *spec = codec->spec;
253         int pcm_idx;
254
255         for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
256                 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
257                         return pcm_idx;
258
259         codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
260         return -EINVAL;
261 }
262
263 static int hinfo_to_pin_index(struct hda_codec *codec,
264                               struct hda_pcm_stream *hinfo)
265 {
266         struct hdmi_spec *spec = codec->spec;
267         struct hdmi_spec_per_pin *per_pin;
268         int pin_idx;
269
270         for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
271                 per_pin = get_pin(spec, pin_idx);
272                 if (per_pin->pcm &&
273                         per_pin->pcm->pcm->stream == hinfo)
274                         return pin_idx;
275         }
276
277         codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
278         return -EINVAL;
279 }
280
281 static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
282                                                 int pcm_idx)
283 {
284         int i;
285         struct hdmi_spec_per_pin *per_pin;
286
287         for (i = 0; i < spec->num_pins; i++) {
288                 per_pin = get_pin(spec, i);
289                 if (per_pin->pcm_idx == pcm_idx)
290                         return per_pin;
291         }
292         return NULL;
293 }
294
295 static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
296 {
297         struct hdmi_spec *spec = codec->spec;
298         int cvt_idx;
299
300         for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
301                 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
302                         return cvt_idx;
303
304         codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
305         return -EINVAL;
306 }
307
308 static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
309                         struct snd_ctl_elem_info *uinfo)
310 {
311         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
312         struct hdmi_spec *spec = codec->spec;
313         struct hdmi_spec_per_pin *per_pin;
314         struct hdmi_eld *eld;
315         int pcm_idx;
316
317         uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
318
319         pcm_idx = kcontrol->private_value;
320         mutex_lock(&spec->pcm_lock);
321         per_pin = pcm_idx_to_pin(spec, pcm_idx);
322         if (!per_pin) {
323                 /* no pin is bound to the pcm */
324                 uinfo->count = 0;
325                 goto unlock;
326         }
327         eld = &per_pin->sink_eld;
328         uinfo->count = eld->eld_valid ? eld->eld_size : 0;
329
330  unlock:
331         mutex_unlock(&spec->pcm_lock);
332         return 0;
333 }
334
335 static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
336                         struct snd_ctl_elem_value *ucontrol)
337 {
338         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
339         struct hdmi_spec *spec = codec->spec;
340         struct hdmi_spec_per_pin *per_pin;
341         struct hdmi_eld *eld;
342         int pcm_idx;
343         int err = 0;
344
345         pcm_idx = kcontrol->private_value;
346         mutex_lock(&spec->pcm_lock);
347         per_pin = pcm_idx_to_pin(spec, pcm_idx);
348         if (!per_pin) {
349                 /* no pin is bound to the pcm */
350                 memset(ucontrol->value.bytes.data, 0,
351                        ARRAY_SIZE(ucontrol->value.bytes.data));
352                 goto unlock;
353         }
354
355         eld = &per_pin->sink_eld;
356         if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
357             eld->eld_size > ELD_MAX_SIZE) {
358                 snd_BUG();
359                 err = -EINVAL;
360                 goto unlock;
361         }
362
363         memset(ucontrol->value.bytes.data, 0,
364                ARRAY_SIZE(ucontrol->value.bytes.data));
365         if (eld->eld_valid)
366                 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
367                        eld->eld_size);
368
369  unlock:
370         mutex_unlock(&spec->pcm_lock);
371         return err;
372 }
373
374 static const struct snd_kcontrol_new eld_bytes_ctl = {
375         .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
376         .iface = SNDRV_CTL_ELEM_IFACE_PCM,
377         .name = "ELD",
378         .info = hdmi_eld_ctl_info,
379         .get = hdmi_eld_ctl_get,
380 };
381
382 static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
383                         int device)
384 {
385         struct snd_kcontrol *kctl;
386         struct hdmi_spec *spec = codec->spec;
387         int err;
388
389         kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
390         if (!kctl)
391                 return -ENOMEM;
392         kctl->private_value = pcm_idx;
393         kctl->id.device = device;
394
395         /* no pin nid is associated with the kctl now
396          * tbd: associate pin nid to eld ctl later
397          */
398         err = snd_hda_ctl_add(codec, 0, kctl);
399         if (err < 0)
400                 return err;
401
402         get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
403         return 0;
404 }
405
406 #ifdef BE_PARANOID
407 static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
408                                 int *packet_index, int *byte_index)
409 {
410         int val;
411
412         val = snd_hda_codec_read(codec, pin_nid, 0,
413                                  AC_VERB_GET_HDMI_DIP_INDEX, 0);
414
415         *packet_index = val >> 5;
416         *byte_index = val & 0x1f;
417 }
418 #endif
419
420 static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
421                                 int packet_index, int byte_index)
422 {
423         int val;
424
425         val = (packet_index << 5) | (byte_index & 0x1f);
426
427         snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
428 }
429
430 static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
431                                 unsigned char val)
432 {
433         snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
434 }
435
436 static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
437 {
438         struct hdmi_spec *spec = codec->spec;
439         int pin_out;
440
441         /* Unmute */
442         if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
443                 snd_hda_codec_write(codec, pin_nid, 0,
444                                 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
445
446         if (spec->dyn_pin_out)
447                 /* Disable pin out until stream is active */
448                 pin_out = 0;
449         else
450                 /* Enable pin out: some machines with GM965 gets broken output
451                  * when the pin is disabled or changed while using with HDMI
452                  */
453                 pin_out = PIN_OUT;
454
455         snd_hda_codec_write(codec, pin_nid, 0,
456                             AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
457 }
458
459 /*
460  * ELD proc files
461  */
462
463 #ifdef CONFIG_SND_PROC_FS
464 static void print_eld_info(struct snd_info_entry *entry,
465                            struct snd_info_buffer *buffer)
466 {
467         struct hdmi_spec_per_pin *per_pin = entry->private_data;
468
469         mutex_lock(&per_pin->lock);
470         snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
471         mutex_unlock(&per_pin->lock);
472 }
473
474 static void write_eld_info(struct snd_info_entry *entry,
475                            struct snd_info_buffer *buffer)
476 {
477         struct hdmi_spec_per_pin *per_pin = entry->private_data;
478
479         mutex_lock(&per_pin->lock);
480         snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
481         mutex_unlock(&per_pin->lock);
482 }
483
484 static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
485 {
486         char name[32];
487         struct hda_codec *codec = per_pin->codec;
488         struct snd_info_entry *entry;
489         int err;
490
491         snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
492         err = snd_card_proc_new(codec->card, name, &entry);
493         if (err < 0)
494                 return err;
495
496         snd_info_set_text_ops(entry, per_pin, print_eld_info);
497         entry->c.text.write = write_eld_info;
498         entry->mode |= 0200;
499         per_pin->proc_entry = entry;
500
501         return 0;
502 }
503
504 static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
505 {
506         if (!per_pin->codec->bus->shutdown) {
507                 snd_info_free_entry(per_pin->proc_entry);
508                 per_pin->proc_entry = NULL;
509         }
510 }
511 #else
512 static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
513                                int index)
514 {
515         return 0;
516 }
517 static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
518 {
519 }
520 #endif
521
522 /*
523  * Audio InfoFrame routines
524  */
525
526 /*
527  * Enable Audio InfoFrame Transmission
528  */
529 static void hdmi_start_infoframe_trans(struct hda_codec *codec,
530                                        hda_nid_t pin_nid)
531 {
532         hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
533         snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
534                                                 AC_DIPXMIT_BEST);
535 }
536
537 /*
538  * Disable Audio InfoFrame Transmission
539  */
540 static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
541                                       hda_nid_t pin_nid)
542 {
543         hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
544         snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
545                                                 AC_DIPXMIT_DISABLE);
546 }
547
548 static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
549 {
550 #ifdef CONFIG_SND_DEBUG_VERBOSE
551         int i;
552         int size;
553
554         size = snd_hdmi_get_eld_size(codec, pin_nid);
555         codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
556
557         for (i = 0; i < 8; i++) {
558                 size = snd_hda_codec_read(codec, pin_nid, 0,
559                                                 AC_VERB_GET_HDMI_DIP_SIZE, i);
560                 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
561         }
562 #endif
563 }
564
565 static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
566 {
567 #ifdef BE_PARANOID
568         int i, j;
569         int size;
570         int pi, bi;
571         for (i = 0; i < 8; i++) {
572                 size = snd_hda_codec_read(codec, pin_nid, 0,
573                                                 AC_VERB_GET_HDMI_DIP_SIZE, i);
574                 if (size == 0)
575                         continue;
576
577                 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
578                 for (j = 1; j < 1000; j++) {
579                         hdmi_write_dip_byte(codec, pin_nid, 0x0);
580                         hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
581                         if (pi != i)
582                                 codec_dbg(codec, "dip index %d: %d != %d\n",
583                                                 bi, pi, i);
584                         if (bi == 0) /* byte index wrapped around */
585                                 break;
586                 }
587                 codec_dbg(codec,
588                         "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
589                         i, size, j);
590         }
591 #endif
592 }
593
594 static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
595 {
596         u8 *bytes = (u8 *)hdmi_ai;
597         u8 sum = 0;
598         int i;
599
600         hdmi_ai->checksum = 0;
601
602         for (i = 0; i < sizeof(*hdmi_ai); i++)
603                 sum += bytes[i];
604
605         hdmi_ai->checksum = -sum;
606 }
607
608 static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
609                                       hda_nid_t pin_nid,
610                                       u8 *dip, int size)
611 {
612         int i;
613
614         hdmi_debug_dip_size(codec, pin_nid);
615         hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
616
617         hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
618         for (i = 0; i < size; i++)
619                 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
620 }
621
622 static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
623                                     u8 *dip, int size)
624 {
625         u8 val;
626         int i;
627
628         if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
629                                                             != AC_DIPXMIT_BEST)
630                 return false;
631
632         hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
633         for (i = 0; i < size; i++) {
634                 val = snd_hda_codec_read(codec, pin_nid, 0,
635                                          AC_VERB_GET_HDMI_DIP_DATA, 0);
636                 if (val != dip[i])
637                         return false;
638         }
639
640         return true;
641 }
642
643 static int hdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
644                             int dev_id, unsigned char *buf, int *eld_size)
645 {
646         snd_hda_set_dev_select(codec, nid, dev_id);
647
648         return snd_hdmi_get_eld(codec, nid, buf, eld_size);
649 }
650
651 static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
652                                      hda_nid_t pin_nid, int dev_id,
653                                      int ca, int active_channels,
654                                      int conn_type)
655 {
656         union audio_infoframe ai;
657
658         memset(&ai, 0, sizeof(ai));
659         if (conn_type == 0) { /* HDMI */
660                 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
661
662                 hdmi_ai->type           = 0x84;
663                 hdmi_ai->ver            = 0x01;
664                 hdmi_ai->len            = 0x0a;
665                 hdmi_ai->CC02_CT47      = active_channels - 1;
666                 hdmi_ai->CA             = ca;
667                 hdmi_checksum_audio_infoframe(hdmi_ai);
668         } else if (conn_type == 1) { /* DisplayPort */
669                 struct dp_audio_infoframe *dp_ai = &ai.dp;
670
671                 dp_ai->type             = 0x84;
672                 dp_ai->len              = 0x1b;
673                 dp_ai->ver              = 0x11 << 2;
674                 dp_ai->CC02_CT47        = active_channels - 1;
675                 dp_ai->CA               = ca;
676         } else {
677                 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
678                             pin_nid);
679                 return;
680         }
681
682         snd_hda_set_dev_select(codec, pin_nid, dev_id);
683
684         /*
685          * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
686          * sizeof(*dp_ai) to avoid partial match/update problems when
687          * the user switches between HDMI/DP monitors.
688          */
689         if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
690                                         sizeof(ai))) {
691                 codec_dbg(codec,
692                           "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
693                             pin_nid,
694                             active_channels, ca);
695                 hdmi_stop_infoframe_trans(codec, pin_nid);
696                 hdmi_fill_audio_infoframe(codec, pin_nid,
697                                             ai.bytes, sizeof(ai));
698                 hdmi_start_infoframe_trans(codec, pin_nid);
699         }
700 }
701
702 static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
703                                        struct hdmi_spec_per_pin *per_pin,
704                                        bool non_pcm)
705 {
706         struct hdmi_spec *spec = codec->spec;
707         struct hdac_chmap *chmap = &spec->chmap;
708         hda_nid_t pin_nid = per_pin->pin_nid;
709         int dev_id = per_pin->dev_id;
710         int channels = per_pin->channels;
711         int active_channels;
712         struct hdmi_eld *eld;
713         int ca;
714
715         if (!channels)
716                 return;
717
718         snd_hda_set_dev_select(codec, pin_nid, dev_id);
719
720         /* some HW (e.g. HSW+) needs reprogramming the amp at each time */
721         if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
722                 snd_hda_codec_write(codec, pin_nid, 0,
723                                             AC_VERB_SET_AMP_GAIN_MUTE,
724                                             AMP_OUT_UNMUTE);
725
726         eld = &per_pin->sink_eld;
727
728         ca = snd_hdac_channel_allocation(&codec->core,
729                         eld->info.spk_alloc, channels,
730                         per_pin->chmap_set, non_pcm, per_pin->chmap);
731
732         active_channels = snd_hdac_get_active_channels(ca);
733
734         chmap->ops.set_channel_count(&codec->core, per_pin->cvt_nid,
735                                                 active_channels);
736
737         /*
738          * always configure channel mapping, it may have been changed by the
739          * user in the meantime
740          */
741         snd_hdac_setup_channel_mapping(&spec->chmap,
742                                 pin_nid, non_pcm, ca, channels,
743                                 per_pin->chmap, per_pin->chmap_set);
744
745         spec->ops.pin_setup_infoframe(codec, pin_nid, dev_id,
746                                       ca, active_channels, eld->info.conn_type);
747
748         per_pin->non_pcm = non_pcm;
749 }
750
751 /*
752  * Unsolicited events
753  */
754
755 static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
756
757 static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid,
758                                       int dev_id)
759 {
760         struct hdmi_spec *spec = codec->spec;
761         int pin_idx = pin_id_to_pin_index(codec, nid, dev_id);
762
763         if (pin_idx < 0)
764                 return;
765         mutex_lock(&spec->pcm_lock);
766         if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
767                 snd_hda_jack_report_sync(codec);
768         mutex_unlock(&spec->pcm_lock);
769 }
770
771 static void jack_callback(struct hda_codec *codec,
772                           struct hda_jack_callback *jack)
773 {
774         /* stop polling when notification is enabled */
775         if (codec_has_acomp(codec))
776                 return;
777
778         check_presence_and_report(codec, jack->nid, jack->dev_id);
779 }
780
781 static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
782 {
783         int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
784         struct hda_jack_tbl *jack;
785
786         if (codec->dp_mst) {
787                 int dev_entry =
788                         (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
789
790                 jack = snd_hda_jack_tbl_get_from_tag(codec, tag, dev_entry);
791         } else {
792                 jack = snd_hda_jack_tbl_get_from_tag(codec, tag, 0);
793         }
794         if (!jack)
795                 return;
796         jack->jack_dirty = 1;
797
798         codec_dbg(codec,
799                 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
800                 codec->addr, jack->nid, jack->dev_id, !!(res & AC_UNSOL_RES_IA),
801                 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
802
803         check_presence_and_report(codec, jack->nid, jack->dev_id);
804 }
805
806 static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
807 {
808         int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
809         int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
810         int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
811         int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
812
813         codec_info(codec,
814                 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
815                 codec->addr,
816                 tag,
817                 subtag,
818                 cp_state,
819                 cp_ready);
820
821         /* TODO */
822         if (cp_state)
823                 ;
824         if (cp_ready)
825                 ;
826 }
827
828
829 static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
830 {
831         int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
832         int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
833         struct hda_jack_tbl *jack;
834
835         if (codec_has_acomp(codec))
836                 return;
837
838         if (codec->dp_mst) {
839                 int dev_entry =
840                         (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
841
842                 jack = snd_hda_jack_tbl_get_from_tag(codec, tag, dev_entry);
843         } else {
844                 jack = snd_hda_jack_tbl_get_from_tag(codec, tag, 0);
845         }
846
847         if (!jack) {
848                 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
849                 return;
850         }
851
852         if (subtag == 0)
853                 hdmi_intrinsic_event(codec, res);
854         else
855                 hdmi_non_intrinsic_event(codec, res);
856 }
857
858 static void haswell_verify_D0(struct hda_codec *codec,
859                 hda_nid_t cvt_nid, hda_nid_t nid)
860 {
861         int pwr;
862
863         /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
864          * thus pins could only choose converter 0 for use. Make sure the
865          * converters are in correct power state */
866         if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
867                 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
868
869         if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
870                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
871                                     AC_PWRST_D0);
872                 msleep(40);
873                 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
874                 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
875                 codec_dbg(codec, "Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
876         }
877 }
878
879 /*
880  * Callbacks
881  */
882
883 /* HBR should be Non-PCM, 8 channels */
884 #define is_hbr_format(format) \
885         ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
886
887 static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
888                               int dev_id, bool hbr)
889 {
890         int pinctl, new_pinctl;
891
892         if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
893                 snd_hda_set_dev_select(codec, pin_nid, dev_id);
894                 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
895                                             AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
896
897                 if (pinctl < 0)
898                         return hbr ? -EINVAL : 0;
899
900                 new_pinctl = pinctl & ~AC_PINCTL_EPT;
901                 if (hbr)
902                         new_pinctl |= AC_PINCTL_EPT_HBR;
903                 else
904                         new_pinctl |= AC_PINCTL_EPT_NATIVE;
905
906                 codec_dbg(codec,
907                           "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
908                             pin_nid,
909                             pinctl == new_pinctl ? "" : "new-",
910                             new_pinctl);
911
912                 if (pinctl != new_pinctl)
913                         snd_hda_codec_write(codec, pin_nid, 0,
914                                             AC_VERB_SET_PIN_WIDGET_CONTROL,
915                                             new_pinctl);
916         } else if (hbr)
917                 return -EINVAL;
918
919         return 0;
920 }
921
922 static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
923                               hda_nid_t pin_nid, int dev_id,
924                               u32 stream_tag, int format)
925 {
926         struct hdmi_spec *spec = codec->spec;
927         unsigned int param;
928         int err;
929
930         err = spec->ops.pin_hbr_setup(codec, pin_nid, dev_id,
931                                       is_hbr_format(format));
932
933         if (err) {
934                 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
935                 return err;
936         }
937
938         if (spec->intel_hsw_fixup) {
939
940                 /*
941                  * on recent platforms IEC Coding Type is required for HBR
942                  * support, read current Digital Converter settings and set
943                  * ICT bitfield if needed.
944                  */
945                 param = snd_hda_codec_read(codec, cvt_nid, 0,
946                                            AC_VERB_GET_DIGI_CONVERT_1, 0);
947
948                 param = (param >> 16) & ~(AC_DIG3_ICT);
949
950                 /* on recent platforms ICT mode is required for HBR support */
951                 if (is_hbr_format(format))
952                         param |= 0x1;
953
954                 snd_hda_codec_write(codec, cvt_nid, 0,
955                                     AC_VERB_SET_DIGI_CONVERT_3, param);
956         }
957
958         snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
959         return 0;
960 }
961
962 /* Try to find an available converter
963  * If pin_idx is less then zero, just try to find an available converter.
964  * Otherwise, try to find an available converter and get the cvt mux index
965  * of the pin.
966  */
967 static int hdmi_choose_cvt(struct hda_codec *codec,
968                            int pin_idx, int *cvt_id)
969 {
970         struct hdmi_spec *spec = codec->spec;
971         struct hdmi_spec_per_pin *per_pin;
972         struct hdmi_spec_per_cvt *per_cvt = NULL;
973         int cvt_idx, mux_idx = 0;
974
975         /* pin_idx < 0 means no pin will be bound to the converter */
976         if (pin_idx < 0)
977                 per_pin = NULL;
978         else
979                 per_pin = get_pin(spec, pin_idx);
980
981         /* Dynamically assign converter to stream */
982         for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
983                 per_cvt = get_cvt(spec, cvt_idx);
984
985                 /* Must not already be assigned */
986                 if (per_cvt->assigned)
987                         continue;
988                 if (per_pin == NULL)
989                         break;
990                 /* Must be in pin's mux's list of converters */
991                 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
992                         if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
993                                 break;
994                 /* Not in mux list */
995                 if (mux_idx == per_pin->num_mux_nids)
996                         continue;
997                 break;
998         }
999
1000         /* No free converters */
1001         if (cvt_idx == spec->num_cvts)
1002                 return -EBUSY;
1003
1004         if (per_pin != NULL)
1005                 per_pin->mux_idx = mux_idx;
1006
1007         if (cvt_id)
1008                 *cvt_id = cvt_idx;
1009
1010         return 0;
1011 }
1012
1013 /* Assure the pin select the right convetor */
1014 static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
1015                         struct hdmi_spec_per_pin *per_pin)
1016 {
1017         hda_nid_t pin_nid = per_pin->pin_nid;
1018         int mux_idx, curr;
1019
1020         mux_idx = per_pin->mux_idx;
1021         curr = snd_hda_codec_read(codec, pin_nid, 0,
1022                                           AC_VERB_GET_CONNECT_SEL, 0);
1023         if (curr != mux_idx)
1024                 snd_hda_codec_write_cache(codec, pin_nid, 0,
1025                                             AC_VERB_SET_CONNECT_SEL,
1026                                             mux_idx);
1027 }
1028
1029 /* get the mux index for the converter of the pins
1030  * converter's mux index is the same for all pins on Intel platform
1031  */
1032 static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
1033                         hda_nid_t cvt_nid)
1034 {
1035         int i;
1036
1037         for (i = 0; i < spec->num_cvts; i++)
1038                 if (spec->cvt_nids[i] == cvt_nid)
1039                         return i;
1040         return -EINVAL;
1041 }
1042
1043 /* Intel HDMI workaround to fix audio routing issue:
1044  * For some Intel display codecs, pins share the same connection list.
1045  * So a conveter can be selected by multiple pins and playback on any of these
1046  * pins will generate sound on the external display, because audio flows from
1047  * the same converter to the display pipeline. Also muting one pin may make
1048  * other pins have no sound output.
1049  * So this function assures that an assigned converter for a pin is not selected
1050  * by any other pins.
1051  */
1052 static void intel_not_share_assigned_cvt(struct hda_codec *codec,
1053                                          hda_nid_t pin_nid,
1054                                          int dev_id, int mux_idx)
1055 {
1056         struct hdmi_spec *spec = codec->spec;
1057         hda_nid_t nid;
1058         int cvt_idx, curr;
1059         struct hdmi_spec_per_cvt *per_cvt;
1060         struct hdmi_spec_per_pin *per_pin;
1061         int pin_idx;
1062
1063         /* configure the pins connections */
1064         for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1065                 int dev_id_saved;
1066                 int dev_num;
1067
1068                 per_pin = get_pin(spec, pin_idx);
1069                 /*
1070                  * pin not connected to monitor
1071                  * no need to operate on it
1072                  */
1073                 if (!per_pin->pcm)
1074                         continue;
1075
1076                 if ((per_pin->pin_nid == pin_nid) &&
1077                         (per_pin->dev_id == dev_id))
1078                         continue;
1079
1080                 /*
1081                  * if per_pin->dev_id >= dev_num,
1082                  * snd_hda_get_dev_select() will fail,
1083                  * and the following operation is unpredictable.
1084                  * So skip this situation.
1085                  */
1086                 dev_num = snd_hda_get_num_devices(codec, per_pin->pin_nid) + 1;
1087                 if (per_pin->dev_id >= dev_num)
1088                         continue;
1089
1090                 nid = per_pin->pin_nid;
1091
1092                 /*
1093                  * Calling this function should not impact
1094                  * on the device entry selection
1095                  * So let's save the dev id for each pin,
1096                  * and restore it when return
1097                  */
1098                 dev_id_saved = snd_hda_get_dev_select(codec, nid);
1099                 snd_hda_set_dev_select(codec, nid, per_pin->dev_id);
1100                 curr = snd_hda_codec_read(codec, nid, 0,
1101                                           AC_VERB_GET_CONNECT_SEL, 0);
1102                 if (curr != mux_idx) {
1103                         snd_hda_set_dev_select(codec, nid, dev_id_saved);
1104                         continue;
1105                 }
1106
1107
1108                 /* choose an unassigned converter. The conveters in the
1109                  * connection list are in the same order as in the codec.
1110                  */
1111                 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1112                         per_cvt = get_cvt(spec, cvt_idx);
1113                         if (!per_cvt->assigned) {
1114                                 codec_dbg(codec,
1115                                           "choose cvt %d for pin nid %d\n",
1116                                         cvt_idx, nid);
1117                                 snd_hda_codec_write_cache(codec, nid, 0,
1118                                             AC_VERB_SET_CONNECT_SEL,
1119                                             cvt_idx);
1120                                 break;
1121                         }
1122                 }
1123                 snd_hda_set_dev_select(codec, nid, dev_id_saved);
1124         }
1125 }
1126
1127 /* A wrapper of intel_not_share_asigned_cvt() */
1128 static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1129                         hda_nid_t pin_nid, int dev_id, hda_nid_t cvt_nid)
1130 {
1131         int mux_idx;
1132         struct hdmi_spec *spec = codec->spec;
1133
1134         /* On Intel platform, the mapping of converter nid to
1135          * mux index of the pins are always the same.
1136          * The pin nid may be 0, this means all pins will not
1137          * share the converter.
1138          */
1139         mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1140         if (mux_idx >= 0)
1141                 intel_not_share_assigned_cvt(codec, pin_nid, dev_id, mux_idx);
1142 }
1143
1144 /* skeleton caller of pin_cvt_fixup ops */
1145 static void pin_cvt_fixup(struct hda_codec *codec,
1146                           struct hdmi_spec_per_pin *per_pin,
1147                           hda_nid_t cvt_nid)
1148 {
1149         struct hdmi_spec *spec = codec->spec;
1150
1151         if (spec->ops.pin_cvt_fixup)
1152                 spec->ops.pin_cvt_fixup(codec, per_pin, cvt_nid);
1153 }
1154
1155 /* called in hdmi_pcm_open when no pin is assigned to the PCM
1156  * in dyn_pcm_assign mode.
1157  */
1158 static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1159                          struct hda_codec *codec,
1160                          struct snd_pcm_substream *substream)
1161 {
1162         struct hdmi_spec *spec = codec->spec;
1163         struct snd_pcm_runtime *runtime = substream->runtime;
1164         int cvt_idx, pcm_idx;
1165         struct hdmi_spec_per_cvt *per_cvt = NULL;
1166         int err;
1167
1168         pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1169         if (pcm_idx < 0)
1170                 return -EINVAL;
1171
1172         err = hdmi_choose_cvt(codec, -1, &cvt_idx);
1173         if (err)
1174                 return err;
1175
1176         per_cvt = get_cvt(spec, cvt_idx);
1177         per_cvt->assigned = 1;
1178         hinfo->nid = per_cvt->cvt_nid;
1179
1180         pin_cvt_fixup(codec, NULL, per_cvt->cvt_nid);
1181
1182         set_bit(pcm_idx, &spec->pcm_in_use);
1183         /* todo: setup spdif ctls assign */
1184
1185         /* Initially set the converter's capabilities */
1186         hinfo->channels_min = per_cvt->channels_min;
1187         hinfo->channels_max = per_cvt->channels_max;
1188         hinfo->rates = per_cvt->rates;
1189         hinfo->formats = per_cvt->formats;
1190         hinfo->maxbps = per_cvt->maxbps;
1191
1192         /* Store the updated parameters */
1193         runtime->hw.channels_min = hinfo->channels_min;
1194         runtime->hw.channels_max = hinfo->channels_max;
1195         runtime->hw.formats = hinfo->formats;
1196         runtime->hw.rates = hinfo->rates;
1197
1198         snd_pcm_hw_constraint_step(substream->runtime, 0,
1199                                    SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1200         return 0;
1201 }
1202
1203 /*
1204  * HDA PCM callbacks
1205  */
1206 static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1207                          struct hda_codec *codec,
1208                          struct snd_pcm_substream *substream)
1209 {
1210         struct hdmi_spec *spec = codec->spec;
1211         struct snd_pcm_runtime *runtime = substream->runtime;
1212         int pin_idx, cvt_idx, pcm_idx;
1213         struct hdmi_spec_per_pin *per_pin;
1214         struct hdmi_eld *eld;
1215         struct hdmi_spec_per_cvt *per_cvt = NULL;
1216         int err;
1217
1218         /* Validate hinfo */
1219         pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1220         if (pcm_idx < 0)
1221                 return -EINVAL;
1222
1223         mutex_lock(&spec->pcm_lock);
1224         pin_idx = hinfo_to_pin_index(codec, hinfo);
1225         if (!spec->dyn_pcm_assign) {
1226                 if (snd_BUG_ON(pin_idx < 0)) {
1227                         err = -EINVAL;
1228                         goto unlock;
1229                 }
1230         } else {
1231                 /* no pin is assigned to the PCM
1232                  * PA need pcm open successfully when probe
1233                  */
1234                 if (pin_idx < 0) {
1235                         err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1236                         goto unlock;
1237                 }
1238         }
1239
1240         err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx);
1241         if (err < 0)
1242                 goto unlock;
1243
1244         per_cvt = get_cvt(spec, cvt_idx);
1245         /* Claim converter */
1246         per_cvt->assigned = 1;
1247
1248         set_bit(pcm_idx, &spec->pcm_in_use);
1249         per_pin = get_pin(spec, pin_idx);
1250         per_pin->cvt_nid = per_cvt->cvt_nid;
1251         hinfo->nid = per_cvt->cvt_nid;
1252
1253         /* flip stripe flag for the assigned stream if supported */
1254         if (get_wcaps(codec, per_cvt->cvt_nid) & AC_WCAP_STRIPE)
1255                 azx_stream(get_azx_dev(substream))->stripe = 1;
1256
1257         snd_hda_set_dev_select(codec, per_pin->pin_nid, per_pin->dev_id);
1258         snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1259                             AC_VERB_SET_CONNECT_SEL,
1260                             per_pin->mux_idx);
1261
1262         /* configure unused pins to choose other converters */
1263         pin_cvt_fixup(codec, per_pin, 0);
1264
1265         snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
1266
1267         /* Initially set the converter's capabilities */
1268         hinfo->channels_min = per_cvt->channels_min;
1269         hinfo->channels_max = per_cvt->channels_max;
1270         hinfo->rates = per_cvt->rates;
1271         hinfo->formats = per_cvt->formats;
1272         hinfo->maxbps = per_cvt->maxbps;
1273
1274         eld = &per_pin->sink_eld;
1275         /* Restrict capabilities by ELD if this isn't disabled */
1276         if (!static_hdmi_pcm && eld->eld_valid) {
1277                 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
1278                 if (hinfo->channels_min > hinfo->channels_max ||
1279                     !hinfo->rates || !hinfo->formats) {
1280                         per_cvt->assigned = 0;
1281                         hinfo->nid = 0;
1282                         snd_hda_spdif_ctls_unassign(codec, pcm_idx);
1283                         err = -ENODEV;
1284                         goto unlock;
1285                 }
1286         }
1287
1288         /* Store the updated parameters */
1289         runtime->hw.channels_min = hinfo->channels_min;
1290         runtime->hw.channels_max = hinfo->channels_max;
1291         runtime->hw.formats = hinfo->formats;
1292         runtime->hw.rates = hinfo->rates;
1293
1294         snd_pcm_hw_constraint_step(substream->runtime, 0,
1295                                    SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1296  unlock:
1297         mutex_unlock(&spec->pcm_lock);
1298         return err;
1299 }
1300
1301 /*
1302  * HDA/HDMI auto parsing
1303  */
1304 static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
1305 {
1306         struct hdmi_spec *spec = codec->spec;
1307         struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1308         hda_nid_t pin_nid = per_pin->pin_nid;
1309         int dev_id = per_pin->dev_id;
1310         int conns;
1311
1312         if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1313                 codec_warn(codec,
1314                            "HDMI: pin %d wcaps %#x does not support connection list\n",
1315                            pin_nid, get_wcaps(codec, pin_nid));
1316                 return -EINVAL;
1317         }
1318
1319         snd_hda_set_dev_select(codec, pin_nid, dev_id);
1320
1321         if (spec->intel_hsw_fixup) {
1322                 conns = spec->num_cvts;
1323                 memcpy(per_pin->mux_nids, spec->cvt_nids,
1324                        sizeof(hda_nid_t) * conns);
1325         } else {
1326                 conns = snd_hda_get_raw_connections(codec, pin_nid,
1327                                                     per_pin->mux_nids,
1328                                                     HDA_MAX_CONNECTIONS);
1329         }
1330
1331         /* all the device entries on the same pin have the same conn list */
1332         per_pin->num_mux_nids = conns;
1333
1334         return 0;
1335 }
1336
1337 static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1338                               struct hdmi_spec_per_pin *per_pin)
1339 {
1340         int i;
1341
1342         /*
1343          * generic_hdmi_build_pcms() may allocate extra PCMs on some
1344          * platforms (with maximum of 'num_nids + dev_num - 1')
1345          *
1346          * The per_pin of pin_nid_idx=n and dev_id=m prefers to get pcm-n
1347          * if m==0. This guarantees that dynamic pcm assignments are compatible
1348          * with the legacy static per_pin-pcm assignment that existed in the
1349          * days before DP-MST.
1350          *
1351          * Intel DP-MST prefers this legacy behavior for compatibility, too.
1352          *
1353          * per_pin of m!=0 prefers to get pcm=(num_nids + (m - 1)).
1354          */
1355
1356         if (per_pin->dev_id == 0 || spec->intel_hsw_fixup) {
1357                 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1358                         return per_pin->pin_nid_idx;
1359         } else {
1360                 i = spec->num_nids + (per_pin->dev_id - 1);
1361                 if (i < spec->pcm_used && !(test_bit(i, &spec->pcm_bitmap)))
1362                         return i;
1363         }
1364
1365         /* have a second try; check the area over num_nids */
1366         for (i = spec->num_nids; i < spec->pcm_used; i++) {
1367                 if (!test_bit(i, &spec->pcm_bitmap))
1368                         return i;
1369         }
1370
1371         /* the last try; check the empty slots in pins */
1372         for (i = 0; i < spec->num_nids; i++) {
1373                 if (!test_bit(i, &spec->pcm_bitmap))
1374                         return i;
1375         }
1376         return -EBUSY;
1377 }
1378
1379 static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1380                                 struct hdmi_spec_per_pin *per_pin)
1381 {
1382         int idx;
1383
1384         /* pcm already be attached to the pin */
1385         if (per_pin->pcm)
1386                 return;
1387         idx = hdmi_find_pcm_slot(spec, per_pin);
1388         if (idx == -EBUSY)
1389                 return;
1390         per_pin->pcm_idx = idx;
1391         per_pin->pcm = get_hdmi_pcm(spec, idx);
1392         set_bit(idx, &spec->pcm_bitmap);
1393 }
1394
1395 static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1396                                 struct hdmi_spec_per_pin *per_pin)
1397 {
1398         int idx;
1399
1400         /* pcm already be detached from the pin */
1401         if (!per_pin->pcm)
1402                 return;
1403         idx = per_pin->pcm_idx;
1404         per_pin->pcm_idx = -1;
1405         per_pin->pcm = NULL;
1406         if (idx >= 0 && idx < spec->pcm_used)
1407                 clear_bit(idx, &spec->pcm_bitmap);
1408 }
1409
1410 static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1411                 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1412 {
1413         int mux_idx;
1414
1415         for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1416                 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1417                         break;
1418         return mux_idx;
1419 }
1420
1421 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1422
1423 static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1424                            struct hdmi_spec_per_pin *per_pin)
1425 {
1426         struct hda_codec *codec = per_pin->codec;
1427         struct hda_pcm *pcm;
1428         struct hda_pcm_stream *hinfo;
1429         struct snd_pcm_substream *substream;
1430         int mux_idx;
1431         bool non_pcm;
1432
1433         if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1434                 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
1435         else
1436                 return;
1437         if (!pcm->pcm)
1438                 return;
1439         if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1440                 return;
1441
1442         /* hdmi audio only uses playback and one substream */
1443         hinfo = pcm->stream;
1444         substream = pcm->pcm->streams[0].substream;
1445
1446         per_pin->cvt_nid = hinfo->nid;
1447
1448         mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1449         if (mux_idx < per_pin->num_mux_nids) {
1450                 snd_hda_set_dev_select(codec, per_pin->pin_nid,
1451                                    per_pin->dev_id);
1452                 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1453                                 AC_VERB_SET_CONNECT_SEL,
1454                                 mux_idx);
1455         }
1456         snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1457
1458         non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1459         if (substream->runtime)
1460                 per_pin->channels = substream->runtime->channels;
1461         per_pin->setup = true;
1462         per_pin->mux_idx = mux_idx;
1463
1464         hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1465 }
1466
1467 static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1468                            struct hdmi_spec_per_pin *per_pin)
1469 {
1470         if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1471                 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1472
1473         per_pin->chmap_set = false;
1474         memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1475
1476         per_pin->setup = false;
1477         per_pin->channels = 0;
1478 }
1479
1480 /* update per_pin ELD from the given new ELD;
1481  * setup info frame and notification accordingly
1482  */
1483 static bool update_eld(struct hda_codec *codec,
1484                        struct hdmi_spec_per_pin *per_pin,
1485                        struct hdmi_eld *eld)
1486 {
1487         struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1488         struct hdmi_spec *spec = codec->spec;
1489         bool old_eld_valid = pin_eld->eld_valid;
1490         bool eld_changed;
1491         int pcm_idx;
1492
1493         /* for monitor disconnection, save pcm_idx firstly */
1494         pcm_idx = per_pin->pcm_idx;
1495         if (spec->dyn_pcm_assign) {
1496                 if (eld->eld_valid) {
1497                         hdmi_attach_hda_pcm(spec, per_pin);
1498                         hdmi_pcm_setup_pin(spec, per_pin);
1499                 } else {
1500                         hdmi_pcm_reset_pin(spec, per_pin);
1501                         hdmi_detach_hda_pcm(spec, per_pin);
1502                 }
1503         }
1504         /* if pcm_idx == -1, it means this is in monitor connection event
1505          * we can get the correct pcm_idx now.
1506          */
1507         if (pcm_idx == -1)
1508                 pcm_idx = per_pin->pcm_idx;
1509
1510         if (eld->eld_valid)
1511                 snd_hdmi_show_eld(codec, &eld->info);
1512
1513         eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1514         eld_changed |= (pin_eld->monitor_present != eld->monitor_present);
1515         if (!eld_changed && eld->eld_valid && pin_eld->eld_valid)
1516                 if (pin_eld->eld_size != eld->eld_size ||
1517                     memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1518                            eld->eld_size) != 0)
1519                         eld_changed = true;
1520
1521         if (eld_changed) {
1522                 pin_eld->monitor_present = eld->monitor_present;
1523                 pin_eld->eld_valid = eld->eld_valid;
1524                 pin_eld->eld_size = eld->eld_size;
1525                 if (eld->eld_valid)
1526                         memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1527                                eld->eld_size);
1528                 pin_eld->info = eld->info;
1529         }
1530
1531         /*
1532          * Re-setup pin and infoframe. This is needed e.g. when
1533          * - sink is first plugged-in
1534          * - transcoder can change during stream playback on Haswell
1535          *   and this can make HW reset converter selection on a pin.
1536          */
1537         if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1538                 pin_cvt_fixup(codec, per_pin, 0);
1539                 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1540         }
1541
1542         if (eld_changed && pcm_idx >= 0)
1543                 snd_ctl_notify(codec->card,
1544                                SNDRV_CTL_EVENT_MASK_VALUE |
1545                                SNDRV_CTL_EVENT_MASK_INFO,
1546                                &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
1547         return eld_changed;
1548 }
1549
1550 /* update ELD and jack state via HD-audio verbs */
1551 static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1552                                          int repoll)
1553 {
1554         struct hda_jack_tbl *jack;
1555         struct hda_codec *codec = per_pin->codec;
1556         struct hdmi_spec *spec = codec->spec;
1557         struct hdmi_eld *eld = &spec->temp_eld;
1558         hda_nid_t pin_nid = per_pin->pin_nid;
1559         int dev_id = per_pin->dev_id;
1560         /*
1561          * Always execute a GetPinSense verb here, even when called from
1562          * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1563          * response's PD bit is not the real PD value, but indicates that
1564          * the real PD value changed. An older version of the HD-audio
1565          * specification worked this way. Hence, we just ignore the data in
1566          * the unsolicited response to avoid custom WARs.
1567          */
1568         int present;
1569         bool ret;
1570         bool do_repoll = false;
1571
1572         present = snd_hda_jack_pin_sense(codec, pin_nid, dev_id);
1573
1574         mutex_lock(&per_pin->lock);
1575         eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1576         if (eld->monitor_present)
1577                 eld->eld_valid  = !!(present & AC_PINSENSE_ELDV);
1578         else
1579                 eld->eld_valid = false;
1580
1581         codec_dbg(codec,
1582                 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
1583                 codec->addr, pin_nid, eld->monitor_present, eld->eld_valid);
1584
1585         if (eld->eld_valid) {
1586                 if (spec->ops.pin_get_eld(codec, pin_nid, dev_id,
1587                                           eld->eld_buffer, &eld->eld_size) < 0)
1588                         eld->eld_valid = false;
1589                 else {
1590                         if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
1591                                                     eld->eld_size) < 0)
1592                                 eld->eld_valid = false;
1593                 }
1594                 if (!eld->eld_valid && repoll)
1595                         do_repoll = true;
1596         }
1597
1598         if (do_repoll)
1599                 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1600         else
1601                 update_eld(codec, per_pin, eld);
1602
1603         ret = !repoll || !eld->monitor_present || eld->eld_valid;
1604
1605         jack = snd_hda_jack_tbl_get_mst(codec, pin_nid, per_pin->dev_id);
1606         if (jack) {
1607                 jack->block_report = !ret;
1608                 jack->pin_sense = (eld->monitor_present && eld->eld_valid) ?
1609                         AC_PINSENSE_PRESENCE : 0;
1610         }
1611         mutex_unlock(&per_pin->lock);
1612         return ret;
1613 }
1614
1615 static struct snd_jack *pin_idx_to_jack(struct hda_codec *codec,
1616                                  struct hdmi_spec_per_pin *per_pin)
1617 {
1618         struct hdmi_spec *spec = codec->spec;
1619         struct snd_jack *jack = NULL;
1620         struct hda_jack_tbl *jack_tbl;
1621
1622         /* if !dyn_pcm_assign, get jack from hda_jack_tbl
1623          * in !dyn_pcm_assign case, spec->pcm_rec[].jack is not
1624          * NULL even after snd_hda_jack_tbl_clear() is called to
1625          * free snd_jack. This may cause access invalid memory
1626          * when calling snd_jack_report
1627          */
1628         if (per_pin->pcm_idx >= 0 && spec->dyn_pcm_assign)
1629                 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
1630         else if (!spec->dyn_pcm_assign) {
1631                 /*
1632                  * jack tbl doesn't support DP MST
1633                  * DP MST will use dyn_pcm_assign,
1634                  * so DP MST will never come here
1635                  */
1636                 jack_tbl = snd_hda_jack_tbl_get_mst(codec, per_pin->pin_nid,
1637                                                     per_pin->dev_id);
1638                 if (jack_tbl)
1639                         jack = jack_tbl->jack;
1640         }
1641         return jack;
1642 }
1643
1644 /* update ELD and jack state via audio component */
1645 static void sync_eld_via_acomp(struct hda_codec *codec,
1646                                struct hdmi_spec_per_pin *per_pin)
1647 {
1648         struct hdmi_spec *spec = codec->spec;
1649         struct hdmi_eld *eld = &spec->temp_eld;
1650         struct snd_jack *jack = NULL;
1651         bool changed;
1652         int size;
1653
1654         mutex_lock(&per_pin->lock);
1655         eld->monitor_present = false;
1656         size = snd_hdac_acomp_get_eld(&codec->core, per_pin->pin_nid,
1657                                       per_pin->dev_id, &eld->monitor_present,
1658                                       eld->eld_buffer, ELD_MAX_SIZE);
1659         if (size > 0) {
1660                 size = min(size, ELD_MAX_SIZE);
1661                 if (snd_hdmi_parse_eld(codec, &eld->info,
1662                                        eld->eld_buffer, size) < 0)
1663                         size = -EINVAL;
1664         }
1665
1666         if (size > 0) {
1667                 eld->eld_valid = true;
1668                 eld->eld_size = size;
1669         } else {
1670                 eld->eld_valid = false;
1671                 eld->eld_size = 0;
1672         }
1673
1674         /* pcm_idx >=0 before update_eld() means it is in monitor
1675          * disconnected event. Jack must be fetched before update_eld()
1676          */
1677         jack = pin_idx_to_jack(codec, per_pin);
1678         changed = update_eld(codec, per_pin, eld);
1679         if (jack == NULL)
1680                 jack = pin_idx_to_jack(codec, per_pin);
1681         if (changed && jack)
1682                 snd_jack_report(jack,
1683                                 (eld->monitor_present && eld->eld_valid) ?
1684                                 SND_JACK_AVOUT : 0);
1685         mutex_unlock(&per_pin->lock);
1686 }
1687
1688 static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1689 {
1690         struct hda_codec *codec = per_pin->codec;
1691         int ret;
1692
1693         /* no temporary power up/down needed for component notifier */
1694         if (!codec_has_acomp(codec)) {
1695                 ret = snd_hda_power_up_pm(codec);
1696                 if (ret < 0 && pm_runtime_suspended(hda_codec_dev(codec))) {
1697                         snd_hda_power_down_pm(codec);
1698                         return false;
1699                 }
1700                 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
1701                 snd_hda_power_down_pm(codec);
1702         } else {
1703                 sync_eld_via_acomp(codec, per_pin);
1704                 ret = false; /* don't call snd_hda_jack_report_sync() */
1705         }
1706
1707         return ret;
1708 }
1709
1710 static void hdmi_repoll_eld(struct work_struct *work)
1711 {
1712         struct hdmi_spec_per_pin *per_pin =
1713         container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1714         struct hda_codec *codec = per_pin->codec;
1715         struct hdmi_spec *spec = codec->spec;
1716         struct hda_jack_tbl *jack;
1717
1718         jack = snd_hda_jack_tbl_get_mst(codec, per_pin->pin_nid,
1719                                         per_pin->dev_id);
1720         if (jack)
1721                 jack->jack_dirty = 1;
1722
1723         if (per_pin->repoll_count++ > 6)
1724                 per_pin->repoll_count = 0;
1725
1726         mutex_lock(&spec->pcm_lock);
1727         if (hdmi_present_sense(per_pin, per_pin->repoll_count))
1728                 snd_hda_jack_report_sync(per_pin->codec);
1729         mutex_unlock(&spec->pcm_lock);
1730 }
1731
1732 static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1733 {
1734         struct hdmi_spec *spec = codec->spec;
1735         unsigned int caps, config;
1736         int pin_idx;
1737         struct hdmi_spec_per_pin *per_pin;
1738         int err;
1739         int dev_num, i;
1740
1741         caps = snd_hda_query_pin_caps(codec, pin_nid);
1742         if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1743                 return 0;
1744
1745         /*
1746          * For DP MST audio, Configuration Default is the same for
1747          * all device entries on the same pin
1748          */
1749         config = snd_hda_codec_get_pincfg(codec, pin_nid);
1750         if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1751                 return 0;
1752
1753         /*
1754          * To simplify the implementation, malloc all
1755          * the virtual pins in the initialization statically
1756          */
1757         if (spec->intel_hsw_fixup) {
1758                 /*
1759                  * On Intel platforms, device entries number is
1760                  * changed dynamically. If there is a DP MST
1761                  * hub connected, the device entries number is 3.
1762                  * Otherwise, it is 1.
1763                  * Here we manually set dev_num to 3, so that
1764                  * we can initialize all the device entries when
1765                  * bootup statically.
1766                  */
1767                 dev_num = 3;
1768                 spec->dev_num = 3;
1769         } else if (spec->dyn_pcm_assign && codec->dp_mst) {
1770                 dev_num = snd_hda_get_num_devices(codec, pin_nid) + 1;
1771                 /*
1772                  * spec->dev_num is the maxinum number of device entries
1773                  * among all the pins
1774                  */
1775                 spec->dev_num = (spec->dev_num > dev_num) ?
1776                         spec->dev_num : dev_num;
1777         } else {
1778                 /*
1779                  * If the platform doesn't support DP MST,
1780                  * manually set dev_num to 1. This means
1781                  * the pin has only one device entry.
1782                  */
1783                 dev_num = 1;
1784                 spec->dev_num = 1;
1785         }
1786
1787         for (i = 0; i < dev_num; i++) {
1788                 pin_idx = spec->num_pins;
1789                 per_pin = snd_array_new(&spec->pins);
1790
1791                 if (!per_pin)
1792                         return -ENOMEM;
1793
1794                 if (spec->dyn_pcm_assign) {
1795                         per_pin->pcm = NULL;
1796                         per_pin->pcm_idx = -1;
1797                 } else {
1798                         per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
1799                         per_pin->pcm_idx = pin_idx;
1800                 }
1801                 per_pin->pin_nid = pin_nid;
1802                 per_pin->pin_nid_idx = spec->num_nids;
1803                 per_pin->dev_id = i;
1804                 per_pin->non_pcm = false;
1805                 snd_hda_set_dev_select(codec, pin_nid, i);
1806                 err = hdmi_read_pin_conn(codec, pin_idx);
1807                 if (err < 0)
1808                         return err;
1809                 spec->num_pins++;
1810         }
1811         spec->num_nids++;
1812
1813         return 0;
1814 }
1815
1816 static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1817 {
1818         struct hdmi_spec *spec = codec->spec;
1819         struct hdmi_spec_per_cvt *per_cvt;
1820         unsigned int chans;
1821         int err;
1822
1823         chans = get_wcaps(codec, cvt_nid);
1824         chans = get_wcaps_channels(chans);
1825
1826         per_cvt = snd_array_new(&spec->cvts);
1827         if (!per_cvt)
1828                 return -ENOMEM;
1829
1830         per_cvt->cvt_nid = cvt_nid;
1831         per_cvt->channels_min = 2;
1832         if (chans <= 16) {
1833                 per_cvt->channels_max = chans;
1834                 if (chans > spec->chmap.channels_max)
1835                         spec->chmap.channels_max = chans;
1836         }
1837
1838         err = snd_hda_query_supported_pcm(codec, cvt_nid,
1839                                           &per_cvt->rates,
1840                                           &per_cvt->formats,
1841                                           &per_cvt->maxbps);
1842         if (err < 0)
1843                 return err;
1844
1845         if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1846                 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1847         spec->num_cvts++;
1848
1849         return 0;
1850 }
1851
1852 static int hdmi_parse_codec(struct hda_codec *codec)
1853 {
1854         hda_nid_t nid;
1855         int i, nodes;
1856
1857         nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
1858         if (!nid || nodes < 0) {
1859                 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
1860                 return -EINVAL;
1861         }
1862
1863         for (i = 0; i < nodes; i++, nid++) {
1864                 unsigned int caps;
1865                 unsigned int type;
1866
1867                 caps = get_wcaps(codec, nid);
1868                 type = get_wcaps_type(caps);
1869
1870                 if (!(caps & AC_WCAP_DIGITAL))
1871                         continue;
1872
1873                 switch (type) {
1874                 case AC_WID_AUD_OUT:
1875                         hdmi_add_cvt(codec, nid);
1876                         break;
1877                 case AC_WID_PIN:
1878                         hdmi_add_pin(codec, nid);
1879                         break;
1880                 }
1881         }
1882
1883         return 0;
1884 }
1885
1886 /*
1887  */
1888 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1889 {
1890         struct hda_spdif_out *spdif;
1891         bool non_pcm;
1892
1893         mutex_lock(&codec->spdif_mutex);
1894         spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1895         /* Add sanity check to pass klockwork check.
1896          * This should never happen.
1897          */
1898         if (WARN_ON(spdif == NULL))
1899                 return true;
1900         non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1901         mutex_unlock(&codec->spdif_mutex);
1902         return non_pcm;
1903 }
1904
1905 /*
1906  * HDMI callbacks
1907  */
1908
1909 static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1910                                            struct hda_codec *codec,
1911                                            unsigned int stream_tag,
1912                                            unsigned int format,
1913                                            struct snd_pcm_substream *substream)
1914 {
1915         hda_nid_t cvt_nid = hinfo->nid;
1916         struct hdmi_spec *spec = codec->spec;
1917         int pin_idx;
1918         struct hdmi_spec_per_pin *per_pin;
1919         struct snd_pcm_runtime *runtime = substream->runtime;
1920         bool non_pcm;
1921         int pinctl, stripe;
1922         int err = 0;
1923
1924         mutex_lock(&spec->pcm_lock);
1925         pin_idx = hinfo_to_pin_index(codec, hinfo);
1926         if (spec->dyn_pcm_assign && pin_idx < 0) {
1927                 /* when dyn_pcm_assign and pcm is not bound to a pin
1928                  * skip pin setup and return 0 to make audio playback
1929                  * be ongoing
1930                  */
1931                 pin_cvt_fixup(codec, NULL, cvt_nid);
1932                 snd_hda_codec_setup_stream(codec, cvt_nid,
1933                                         stream_tag, 0, format);
1934                 goto unlock;
1935         }
1936
1937         if (snd_BUG_ON(pin_idx < 0)) {
1938                 err = -EINVAL;
1939                 goto unlock;
1940         }
1941         per_pin = get_pin(spec, pin_idx);
1942
1943         /* Verify pin:cvt selections to avoid silent audio after S3.
1944          * After S3, the audio driver restores pin:cvt selections
1945          * but this can happen before gfx is ready and such selection
1946          * is overlooked by HW. Thus multiple pins can share a same
1947          * default convertor and mute control will affect each other,
1948          * which can cause a resumed audio playback become silent
1949          * after S3.
1950          */
1951         pin_cvt_fixup(codec, per_pin, 0);
1952
1953         /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
1954         /* Todo: add DP1.2 MST audio support later */
1955         if (codec_has_acomp(codec))
1956                 snd_hdac_sync_audio_rate(&codec->core, per_pin->pin_nid,
1957                                          per_pin->dev_id, runtime->rate);
1958
1959         non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
1960         mutex_lock(&per_pin->lock);
1961         per_pin->channels = substream->runtime->channels;
1962         per_pin->setup = true;
1963
1964         if (get_wcaps(codec, cvt_nid) & AC_WCAP_STRIPE) {
1965                 stripe = snd_hdac_get_stream_stripe_ctl(&codec->bus->core,
1966                                                         substream);
1967                 snd_hda_codec_write(codec, cvt_nid, 0,
1968                                     AC_VERB_SET_STRIPE_CONTROL,
1969                                     stripe);
1970         }
1971
1972         hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1973         mutex_unlock(&per_pin->lock);
1974         if (spec->dyn_pin_out) {
1975                 snd_hda_set_dev_select(codec, per_pin->pin_nid,
1976                                        per_pin->dev_id);
1977                 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1978                                             AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1979                 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1980                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1981                                     pinctl | PIN_OUT);
1982         }
1983
1984         /* snd_hda_set_dev_select() has been called before */
1985         err = spec->ops.setup_stream(codec, cvt_nid, per_pin->pin_nid,
1986                                      per_pin->dev_id, stream_tag, format);
1987  unlock:
1988         mutex_unlock(&spec->pcm_lock);
1989         return err;
1990 }
1991
1992 static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1993                                              struct hda_codec *codec,
1994                                              struct snd_pcm_substream *substream)
1995 {
1996         snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1997         return 0;
1998 }
1999
2000 static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
2001                           struct hda_codec *codec,
2002                           struct snd_pcm_substream *substream)
2003 {
2004         struct hdmi_spec *spec = codec->spec;
2005         int cvt_idx, pin_idx, pcm_idx;
2006         struct hdmi_spec_per_cvt *per_cvt;
2007         struct hdmi_spec_per_pin *per_pin;
2008         int pinctl;
2009         int err = 0;
2010
2011         if (hinfo->nid) {
2012                 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
2013                 if (snd_BUG_ON(pcm_idx < 0))
2014                         return -EINVAL;
2015                 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
2016                 if (snd_BUG_ON(cvt_idx < 0))
2017                         return -EINVAL;
2018                 per_cvt = get_cvt(spec, cvt_idx);
2019
2020                 snd_BUG_ON(!per_cvt->assigned);
2021                 per_cvt->assigned = 0;
2022                 hinfo->nid = 0;
2023
2024                 azx_stream(get_azx_dev(substream))->stripe = 0;
2025
2026                 mutex_lock(&spec->pcm_lock);
2027                 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
2028                 clear_bit(pcm_idx, &spec->pcm_in_use);
2029                 pin_idx = hinfo_to_pin_index(codec, hinfo);
2030                 if (spec->dyn_pcm_assign && pin_idx < 0)
2031                         goto unlock;
2032
2033                 if (snd_BUG_ON(pin_idx < 0)) {
2034                         err = -EINVAL;
2035                         goto unlock;
2036                 }
2037                 per_pin = get_pin(spec, pin_idx);
2038
2039                 if (spec->dyn_pin_out) {
2040                         snd_hda_set_dev_select(codec, per_pin->pin_nid,
2041                                                per_pin->dev_id);
2042                         pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
2043                                         AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2044                         snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2045                                             AC_VERB_SET_PIN_WIDGET_CONTROL,
2046                                             pinctl & ~PIN_OUT);
2047                 }
2048
2049                 mutex_lock(&per_pin->lock);
2050                 per_pin->chmap_set = false;
2051                 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
2052
2053                 per_pin->setup = false;
2054                 per_pin->channels = 0;
2055                 mutex_unlock(&per_pin->lock);
2056         unlock:
2057                 mutex_unlock(&spec->pcm_lock);
2058         }
2059
2060         return err;
2061 }
2062
2063 static const struct hda_pcm_ops generic_ops = {
2064         .open = hdmi_pcm_open,
2065         .close = hdmi_pcm_close,
2066         .prepare = generic_hdmi_playback_pcm_prepare,
2067         .cleanup = generic_hdmi_playback_pcm_cleanup,
2068 };
2069
2070 static int hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
2071 {
2072         struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2073         struct hdmi_spec *spec = codec->spec;
2074         struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2075
2076         if (!per_pin)
2077                 return 0;
2078
2079         return per_pin->sink_eld.info.spk_alloc;
2080 }
2081
2082 static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
2083                                         unsigned char *chmap)
2084 {
2085         struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2086         struct hdmi_spec *spec = codec->spec;
2087         struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2088
2089         /* chmap is already set to 0 in caller */
2090         if (!per_pin)
2091                 return;
2092
2093         memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
2094 }
2095
2096 static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
2097                                 unsigned char *chmap, int prepared)
2098 {
2099         struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2100         struct hdmi_spec *spec = codec->spec;
2101         struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2102
2103         if (!per_pin)
2104                 return;
2105         mutex_lock(&per_pin->lock);
2106         per_pin->chmap_set = true;
2107         memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
2108         if (prepared)
2109                 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
2110         mutex_unlock(&per_pin->lock);
2111 }
2112
2113 static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
2114 {
2115         struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2116         struct hdmi_spec *spec = codec->spec;
2117         struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2118
2119         return per_pin ? true:false;
2120 }
2121
2122 static int generic_hdmi_build_pcms(struct hda_codec *codec)
2123 {
2124         struct hdmi_spec *spec = codec->spec;
2125         int idx, pcm_num;
2126
2127         /*
2128          * for non-mst mode, pcm number is the same as before
2129          * for DP MST mode without extra PCM, pcm number is same
2130          * for DP MST mode with extra PCMs, pcm number is
2131          *  (nid number + dev_num - 1)
2132          * dev_num is the device entry number in a pin
2133          */
2134
2135         if (codec->mst_no_extra_pcms)
2136                 pcm_num = spec->num_nids;
2137         else
2138                 pcm_num = spec->num_nids + spec->dev_num - 1;
2139
2140         codec_dbg(codec, "hdmi: pcm_num set to %d\n", pcm_num);
2141
2142         for (idx = 0; idx < pcm_num; idx++) {
2143                 struct hda_pcm *info;
2144                 struct hda_pcm_stream *pstr;
2145
2146                 info = snd_hda_codec_pcm_new(codec, "HDMI %d", idx);
2147                 if (!info)
2148                         return -ENOMEM;
2149
2150                 spec->pcm_rec[idx].pcm = info;
2151                 spec->pcm_used++;
2152                 info->pcm_type = HDA_PCM_TYPE_HDMI;
2153                 info->own_chmap = true;
2154
2155                 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2156                 pstr->substreams = 1;
2157                 pstr->ops = generic_ops;
2158                 /* pcm number is less than 16 */
2159                 if (spec->pcm_used >= 16)
2160                         break;
2161                 /* other pstr fields are set in open */
2162         }
2163
2164         return 0;
2165 }
2166
2167 static void free_hdmi_jack_priv(struct snd_jack *jack)
2168 {
2169         struct hdmi_pcm *pcm = jack->private_data;
2170
2171         pcm->jack = NULL;
2172 }
2173
2174 static int add_hdmi_jack_kctl(struct hda_codec *codec,
2175                                struct hdmi_spec *spec,
2176                                int pcm_idx,
2177                                const char *name)
2178 {
2179         struct snd_jack *jack;
2180         int err;
2181
2182         err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
2183                            true, false);
2184         if (err < 0)
2185                 return err;
2186
2187         spec->pcm_rec[pcm_idx].jack = jack;
2188         jack->private_data = &spec->pcm_rec[pcm_idx];
2189         jack->private_free = free_hdmi_jack_priv;
2190         return 0;
2191 }
2192
2193 static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
2194 {
2195         char hdmi_str[32] = "HDMI/DP";
2196         struct hdmi_spec *spec = codec->spec;
2197         struct hdmi_spec_per_pin *per_pin;
2198         struct hda_jack_tbl *jack;
2199         int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
2200         bool phantom_jack;
2201         int ret;
2202
2203         if (pcmdev > 0)
2204                 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
2205
2206         if (spec->dyn_pcm_assign)
2207                 return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
2208
2209         /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
2210         /* if !dyn_pcm_assign, it must be non-MST mode.
2211          * This means pcms and pins are statically mapped.
2212          * And pcm_idx is pin_idx.
2213          */
2214         per_pin = get_pin(spec, pcm_idx);
2215         phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
2216         if (phantom_jack)
2217                 strncat(hdmi_str, " Phantom",
2218                         sizeof(hdmi_str) - strlen(hdmi_str) - 1);
2219         ret = snd_hda_jack_add_kctl_mst(codec, per_pin->pin_nid,
2220                                         per_pin->dev_id, hdmi_str, phantom_jack,
2221                                         0, NULL);
2222         if (ret < 0)
2223                 return ret;
2224         jack = snd_hda_jack_tbl_get_mst(codec, per_pin->pin_nid,
2225                                         per_pin->dev_id);
2226         if (jack == NULL)
2227                 return 0;
2228         /* assign jack->jack to pcm_rec[].jack to
2229          * align with dyn_pcm_assign mode
2230          */
2231         spec->pcm_rec[pcm_idx].jack = jack->jack;
2232         return 0;
2233 }
2234
2235 static int generic_hdmi_build_controls(struct hda_codec *codec)
2236 {
2237         struct hdmi_spec *spec = codec->spec;
2238         int dev, err;
2239         int pin_idx, pcm_idx;
2240
2241         for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2242                 if (!get_pcm_rec(spec, pcm_idx)->pcm) {
2243                         /* no PCM: mark this for skipping permanently */
2244                         set_bit(pcm_idx, &spec->pcm_bitmap);
2245                         continue;
2246                 }
2247
2248                 err = generic_hdmi_build_jack(codec, pcm_idx);
2249                 if (err < 0)
2250                         return err;
2251
2252                 /* create the spdif for each pcm
2253                  * pin will be bound when monitor is connected
2254                  */
2255                 if (spec->dyn_pcm_assign)
2256                         err = snd_hda_create_dig_out_ctls(codec,
2257                                           0, spec->cvt_nids[0],
2258                                           HDA_PCM_TYPE_HDMI);
2259                 else {
2260                         struct hdmi_spec_per_pin *per_pin =
2261                                 get_pin(spec, pcm_idx);
2262                         err = snd_hda_create_dig_out_ctls(codec,
2263                                                   per_pin->pin_nid,
2264                                                   per_pin->mux_nids[0],
2265                                                   HDA_PCM_TYPE_HDMI);
2266                 }
2267                 if (err < 0)
2268                         return err;
2269                 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
2270
2271                 dev = get_pcm_rec(spec, pcm_idx)->device;
2272                 if (dev != SNDRV_PCM_INVALID_DEVICE) {
2273                         /* add control for ELD Bytes */
2274                         err = hdmi_create_eld_ctl(codec, pcm_idx, dev);
2275                         if (err < 0)
2276                                 return err;
2277                 }
2278         }
2279
2280         for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2281                 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2282
2283                 hdmi_present_sense(per_pin, 0);
2284         }
2285
2286         /* add channel maps */
2287         for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2288                 struct hda_pcm *pcm;
2289
2290                 pcm = get_pcm_rec(spec, pcm_idx);
2291                 if (!pcm || !pcm->pcm)
2292                         break;
2293                 err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap);
2294                 if (err < 0)
2295                         return err;
2296         }
2297
2298         return 0;
2299 }
2300
2301 static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2302 {
2303         struct hdmi_spec *spec = codec->spec;
2304         int pin_idx;
2305
2306         for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2307                 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2308
2309                 per_pin->codec = codec;
2310                 mutex_init(&per_pin->lock);
2311                 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
2312                 eld_proc_new(per_pin, pin_idx);
2313         }
2314         return 0;
2315 }
2316
2317 static int generic_hdmi_init(struct hda_codec *codec)
2318 {
2319         struct hdmi_spec *spec = codec->spec;
2320         int pin_idx;
2321
2322         mutex_lock(&spec->bind_lock);
2323         spec->use_jack_detect = !codec->jackpoll_interval;
2324         for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2325                 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2326                 hda_nid_t pin_nid = per_pin->pin_nid;
2327                 int dev_id = per_pin->dev_id;
2328
2329                 snd_hda_set_dev_select(codec, pin_nid, dev_id);
2330                 hdmi_init_pin(codec, pin_nid);
2331                 if (codec_has_acomp(codec))
2332                         continue;
2333                 if (spec->use_jack_detect)
2334                         snd_hda_jack_detect_enable(codec, pin_nid, dev_id);
2335                 else
2336                         snd_hda_jack_detect_enable_callback_mst(codec, pin_nid,
2337                                                                 dev_id,
2338                                                                 jack_callback);
2339         }
2340         mutex_unlock(&spec->bind_lock);
2341         return 0;
2342 }
2343
2344 static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2345 {
2346         snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2347         snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
2348 }
2349
2350 static void hdmi_array_free(struct hdmi_spec *spec)
2351 {
2352         snd_array_free(&spec->pins);
2353         snd_array_free(&spec->cvts);
2354 }
2355
2356 static void generic_spec_free(struct hda_codec *codec)
2357 {
2358         struct hdmi_spec *spec = codec->spec;
2359
2360         if (spec) {
2361                 hdmi_array_free(spec);
2362                 kfree(spec);
2363                 codec->spec = NULL;
2364         }
2365         codec->dp_mst = false;
2366 }
2367
2368 static void generic_hdmi_free(struct hda_codec *codec)
2369 {
2370         struct hdmi_spec *spec = codec->spec;
2371         int pin_idx, pcm_idx;
2372
2373         if (spec->acomp_registered) {
2374                 snd_hdac_acomp_exit(&codec->bus->core);
2375         } else if (codec_has_acomp(codec)) {
2376                 snd_hdac_acomp_register_notifier(&codec->bus->core, NULL);
2377         }
2378         codec->relaxed_resume = 0;
2379
2380         for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2381                 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2382                 cancel_delayed_work_sync(&per_pin->work);
2383                 eld_proc_free(per_pin);
2384         }
2385
2386         for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2387                 if (spec->pcm_rec[pcm_idx].jack == NULL)
2388                         continue;
2389                 if (spec->dyn_pcm_assign)
2390                         snd_device_free(codec->card,
2391                                         spec->pcm_rec[pcm_idx].jack);
2392                 else
2393                         spec->pcm_rec[pcm_idx].jack = NULL;
2394         }
2395
2396         generic_spec_free(codec);
2397 }
2398
2399 #ifdef CONFIG_PM
2400 static int generic_hdmi_resume(struct hda_codec *codec)
2401 {
2402         struct hdmi_spec *spec = codec->spec;
2403         int pin_idx;
2404
2405         codec->patch_ops.init(codec);
2406         regcache_sync(codec->core.regmap);
2407
2408         for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2409                 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2410                 hdmi_present_sense(per_pin, 1);
2411         }
2412         return 0;
2413 }
2414 #endif
2415
2416 static const struct hda_codec_ops generic_hdmi_patch_ops = {
2417         .init                   = generic_hdmi_init,
2418         .free                   = generic_hdmi_free,
2419         .build_pcms             = generic_hdmi_build_pcms,
2420         .build_controls         = generic_hdmi_build_controls,
2421         .unsol_event            = hdmi_unsol_event,
2422 #ifdef CONFIG_PM
2423         .resume                 = generic_hdmi_resume,
2424 #endif
2425 };
2426
2427 static const struct hdmi_ops generic_standard_hdmi_ops = {
2428         .pin_get_eld                            = hdmi_pin_get_eld,
2429         .pin_setup_infoframe                    = hdmi_pin_setup_infoframe,
2430         .pin_hbr_setup                          = hdmi_pin_hbr_setup,
2431         .setup_stream                           = hdmi_setup_stream,
2432 };
2433
2434 /* allocate codec->spec and assign/initialize generic parser ops */
2435 static int alloc_generic_hdmi(struct hda_codec *codec)
2436 {
2437         struct hdmi_spec *spec;
2438
2439         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2440         if (!spec)
2441                 return -ENOMEM;
2442
2443         spec->codec = codec;
2444         spec->ops = generic_standard_hdmi_ops;
2445         spec->dev_num = 1;      /* initialize to 1 */
2446         mutex_init(&spec->pcm_lock);
2447         mutex_init(&spec->bind_lock);
2448         snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
2449
2450         spec->chmap.ops.get_chmap = hdmi_get_chmap;
2451         spec->chmap.ops.set_chmap = hdmi_set_chmap;
2452         spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
2453         spec->chmap.ops.get_spk_alloc = hdmi_get_spk_alloc,
2454
2455         codec->spec = spec;
2456         hdmi_array_init(spec, 4);
2457
2458         codec->patch_ops = generic_hdmi_patch_ops;
2459
2460         return 0;
2461 }
2462
2463 /* generic HDMI parser */
2464 static int patch_generic_hdmi(struct hda_codec *codec)
2465 {
2466         int err;
2467
2468         err = alloc_generic_hdmi(codec);
2469         if (err < 0)
2470                 return err;
2471
2472         err = hdmi_parse_codec(codec);
2473         if (err < 0) {
2474                 generic_spec_free(codec);
2475                 return err;
2476         }
2477
2478         generic_hdmi_init_per_pins(codec);
2479         return 0;
2480 }
2481
2482 /*
2483  * generic audio component binding
2484  */
2485
2486 /* turn on / off the unsol event jack detection dynamically */
2487 static void reprogram_jack_detect(struct hda_codec *codec, hda_nid_t nid,
2488                                   int dev_id, bool use_acomp)
2489 {
2490         struct hda_jack_tbl *tbl;
2491
2492         tbl = snd_hda_jack_tbl_get_mst(codec, nid, dev_id);
2493         if (tbl) {
2494                 /* clear unsol even if component notifier is used, or re-enable
2495                  * if notifier is cleared
2496                  */
2497                 unsigned int val = use_acomp ? 0 : (AC_USRSP_EN | tbl->tag);
2498                 snd_hda_codec_write_cache(codec, nid, 0,
2499                                           AC_VERB_SET_UNSOLICITED_ENABLE, val);
2500         } else {
2501                 /* if no jack entry was defined beforehand, create a new one
2502                  * at need (i.e. only when notifier is cleared)
2503                  */
2504                 if (!use_acomp)
2505                         snd_hda_jack_detect_enable(codec, nid, dev_id);
2506         }
2507 }
2508
2509 /* set up / clear component notifier dynamically */
2510 static void generic_acomp_notifier_set(struct drm_audio_component *acomp,
2511                                        bool use_acomp)
2512 {
2513         struct hdmi_spec *spec;
2514         int i;
2515
2516         spec = container_of(acomp->audio_ops, struct hdmi_spec, drm_audio_ops);
2517         mutex_lock(&spec->bind_lock);
2518         spec->use_acomp_notifier = use_acomp;
2519         spec->codec->relaxed_resume = use_acomp;
2520         /* reprogram each jack detection logic depending on the notifier */
2521         if (spec->use_jack_detect) {
2522                 for (i = 0; i < spec->num_pins; i++)
2523                         reprogram_jack_detect(spec->codec,
2524                                               get_pin(spec, i)->pin_nid,
2525                                               get_pin(spec, i)->dev_id,
2526                                               use_acomp);
2527         }
2528         mutex_unlock(&spec->bind_lock);
2529 }
2530
2531 /* enable / disable the notifier via master bind / unbind */
2532 static int generic_acomp_master_bind(struct device *dev,
2533                                      struct drm_audio_component *acomp)
2534 {
2535         generic_acomp_notifier_set(acomp, true);
2536         return 0;
2537 }
2538
2539 static void generic_acomp_master_unbind(struct device *dev,
2540                                         struct drm_audio_component *acomp)
2541 {
2542         generic_acomp_notifier_set(acomp, false);
2543 }
2544
2545 /* check whether both HD-audio and DRM PCI devices belong to the same bus */
2546 static int match_bound_vga(struct device *dev, int subtype, void *data)
2547 {
2548         struct hdac_bus *bus = data;
2549         struct pci_dev *pci, *master;
2550
2551         if (!dev_is_pci(dev) || !dev_is_pci(bus->dev))
2552                 return 0;
2553         master = to_pci_dev(bus->dev);
2554         pci = to_pci_dev(dev);
2555         return master->bus == pci->bus;
2556 }
2557
2558 /* audio component notifier for AMD/Nvidia HDMI codecs */
2559 static void generic_acomp_pin_eld_notify(void *audio_ptr, int port, int dev_id)
2560 {
2561         struct hda_codec *codec = audio_ptr;
2562         struct hdmi_spec *spec = codec->spec;
2563         hda_nid_t pin_nid = spec->port2pin(codec, port);
2564
2565         if (!pin_nid)
2566                 return;
2567         if (get_wcaps_type(get_wcaps(codec, pin_nid)) != AC_WID_PIN)
2568                 return;
2569         /* skip notification during system suspend (but not in runtime PM);
2570          * the state will be updated at resume
2571          */
2572         if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2573                 return;
2574         /* ditto during suspend/resume process itself */
2575         if (snd_hdac_is_in_pm(&codec->core))
2576                 return;
2577
2578         check_presence_and_report(codec, pin_nid, dev_id);
2579 }
2580
2581 /* set up the private drm_audio_ops from the template */
2582 static void setup_drm_audio_ops(struct hda_codec *codec,
2583                                 const struct drm_audio_component_audio_ops *ops)
2584 {
2585         struct hdmi_spec *spec = codec->spec;
2586
2587         spec->drm_audio_ops.audio_ptr = codec;
2588         /* intel_audio_codec_enable() or intel_audio_codec_disable()
2589          * will call pin_eld_notify with using audio_ptr pointer
2590          * We need make sure audio_ptr is really setup
2591          */
2592         wmb();
2593         spec->drm_audio_ops.pin2port = ops->pin2port;
2594         spec->drm_audio_ops.pin_eld_notify = ops->pin_eld_notify;
2595         spec->drm_audio_ops.master_bind = ops->master_bind;
2596         spec->drm_audio_ops.master_unbind = ops->master_unbind;
2597 }
2598
2599 /* initialize the generic HDMI audio component */
2600 static void generic_acomp_init(struct hda_codec *codec,
2601                                const struct drm_audio_component_audio_ops *ops,
2602                                int (*port2pin)(struct hda_codec *, int))
2603 {
2604         struct hdmi_spec *spec = codec->spec;
2605
2606         spec->port2pin = port2pin;
2607         setup_drm_audio_ops(codec, ops);
2608         if (!snd_hdac_acomp_init(&codec->bus->core, &spec->drm_audio_ops,
2609                                  match_bound_vga, 0)) {
2610                 spec->acomp_registered = true;
2611                 codec->bus->keep_power = 0;
2612         }
2613 }
2614
2615 /*
2616  * Intel codec parsers and helpers
2617  */
2618
2619 #define INTEL_GET_VENDOR_VERB   0xf81
2620 #define INTEL_SET_VENDOR_VERB   0x781
2621 #define INTEL_EN_DP12           0x02    /* enable DP 1.2 features */
2622 #define INTEL_EN_ALL_PIN_CVTS   0x01    /* enable 2nd & 3rd pins and convertors */
2623
2624 static void intel_haswell_enable_all_pins(struct hda_codec *codec,
2625                                           bool update_tree)
2626 {
2627         unsigned int vendor_param;
2628         struct hdmi_spec *spec = codec->spec;
2629
2630         vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2631                                 INTEL_GET_VENDOR_VERB, 0);
2632         if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2633                 return;
2634
2635         vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2636         vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2637                                 INTEL_SET_VENDOR_VERB, vendor_param);
2638         if (vendor_param == -1)
2639                 return;
2640
2641         if (update_tree)
2642                 snd_hda_codec_update_widgets(codec);
2643 }
2644
2645 static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2646 {
2647         unsigned int vendor_param;
2648         struct hdmi_spec *spec = codec->spec;
2649
2650         vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2651                                 INTEL_GET_VENDOR_VERB, 0);
2652         if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2653                 return;
2654
2655         /* enable DP1.2 mode */
2656         vendor_param |= INTEL_EN_DP12;
2657         snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
2658         snd_hda_codec_write_cache(codec, spec->vendor_nid, 0,
2659                                 INTEL_SET_VENDOR_VERB, vendor_param);
2660 }
2661
2662 /* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2663  * Otherwise you may get severe h/w communication errors.
2664  */
2665 static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2666                                 unsigned int power_state)
2667 {
2668         if (power_state == AC_PWRST_D0) {
2669                 intel_haswell_enable_all_pins(codec, false);
2670                 intel_haswell_fixup_enable_dp12(codec);
2671         }
2672
2673         snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2674         snd_hda_codec_set_power_to_all(codec, fg, power_state);
2675 }
2676
2677 /* There is a fixed mapping between audio pin node and display port.
2678  * on SNB, IVY, HSW, BSW, SKL, BXT, KBL:
2679  * Pin Widget 5 - PORT B (port = 1 in i915 driver)
2680  * Pin Widget 6 - PORT C (port = 2 in i915 driver)
2681  * Pin Widget 7 - PORT D (port = 3 in i915 driver)
2682  *
2683  * on VLV, ILK:
2684  * Pin Widget 4 - PORT B (port = 1 in i915 driver)
2685  * Pin Widget 5 - PORT C (port = 2 in i915 driver)
2686  * Pin Widget 6 - PORT D (port = 3 in i915 driver)
2687  */
2688 static int intel_base_nid(struct hda_codec *codec)
2689 {
2690         switch (codec->core.vendor_id) {
2691         case 0x80860054: /* ILK */
2692         case 0x80862804: /* ILK */
2693         case 0x80862882: /* VLV */
2694                 return 4;
2695         default:
2696                 return 5;
2697         }
2698 }
2699
2700 static int intel_pin2port(void *audio_ptr, int pin_nid)
2701 {
2702         struct hda_codec *codec = audio_ptr;
2703         struct hdmi_spec *spec = codec->spec;
2704         int base_nid, i;
2705
2706         if (!spec->port_num) {
2707                 base_nid = intel_base_nid(codec);
2708                 if (WARN_ON(pin_nid < base_nid || pin_nid >= base_nid + 3))
2709                         return -1;
2710                 return pin_nid - base_nid + 1;
2711         }
2712
2713         /*
2714          * looking for the pin number in the mapping table and return
2715          * the index which indicate the port number
2716          */
2717         for (i = 0; i < spec->port_num; i++) {
2718                 if (pin_nid == spec->port_map[i])
2719                         return i;
2720         }
2721
2722         codec_info(codec, "Can't find the HDMI/DP port for pin %d\n", pin_nid);
2723         return -1;
2724 }
2725
2726 static int intel_port2pin(struct hda_codec *codec, int port)
2727 {
2728         struct hdmi_spec *spec = codec->spec;
2729
2730         if (!spec->port_num) {
2731                 /* we assume only from port-B to port-D */
2732                 if (port < 1 || port > 3)
2733                         return 0;
2734                 return port + intel_base_nid(codec) - 1;
2735         }
2736
2737         if (port < 0 || port >= spec->port_num)
2738                 return 0;
2739         return spec->port_map[port];
2740 }
2741
2742 static void intel_pin_eld_notify(void *audio_ptr, int port, int pipe)
2743 {
2744         struct hda_codec *codec = audio_ptr;
2745         int pin_nid;
2746         int dev_id = pipe;
2747
2748         pin_nid = intel_port2pin(codec, port);
2749         if (!pin_nid)
2750                 return;
2751         /* skip notification during system suspend (but not in runtime PM);
2752          * the state will be updated at resume
2753          */
2754         if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2755                 return;
2756         /* ditto during suspend/resume process itself */
2757         if (snd_hdac_is_in_pm(&codec->core))
2758                 return;
2759
2760         snd_hdac_i915_set_bclk(&codec->bus->core);
2761         check_presence_and_report(codec, pin_nid, dev_id);
2762 }
2763
2764 static const struct drm_audio_component_audio_ops intel_audio_ops = {
2765         .pin2port = intel_pin2port,
2766         .pin_eld_notify = intel_pin_eld_notify,
2767 };
2768
2769 /* register i915 component pin_eld_notify callback */
2770 static void register_i915_notifier(struct hda_codec *codec)
2771 {
2772         struct hdmi_spec *spec = codec->spec;
2773
2774         spec->use_acomp_notifier = true;
2775         spec->port2pin = intel_port2pin;
2776         setup_drm_audio_ops(codec, &intel_audio_ops);
2777         snd_hdac_acomp_register_notifier(&codec->bus->core,
2778                                         &spec->drm_audio_ops);
2779         /* no need for forcible resume for jack check thanks to notifier */
2780         codec->relaxed_resume = 1;
2781 }
2782
2783 /* setup_stream ops override for HSW+ */
2784 static int i915_hsw_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
2785                                  hda_nid_t pin_nid, int dev_id, u32 stream_tag,
2786                                  int format)
2787 {
2788         haswell_verify_D0(codec, cvt_nid, pin_nid);
2789         return hdmi_setup_stream(codec, cvt_nid, pin_nid, dev_id,
2790                                  stream_tag, format);
2791 }
2792
2793 /* pin_cvt_fixup ops override for HSW+ and VLV+ */
2794 static void i915_pin_cvt_fixup(struct hda_codec *codec,
2795                                struct hdmi_spec_per_pin *per_pin,
2796                                hda_nid_t cvt_nid)
2797 {
2798         if (per_pin) {
2799                 snd_hda_set_dev_select(codec, per_pin->pin_nid,
2800                                per_pin->dev_id);
2801                 intel_verify_pin_cvt_connect(codec, per_pin);
2802                 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
2803                                      per_pin->dev_id, per_pin->mux_idx);
2804         } else {
2805                 intel_not_share_assigned_cvt_nid(codec, 0, 0, cvt_nid);
2806         }
2807 }
2808
2809 /* precondition and allocation for Intel codecs */
2810 static int alloc_intel_hdmi(struct hda_codec *codec)
2811 {
2812         int err;
2813
2814         /* requires i915 binding */
2815         if (!codec->bus->core.audio_component) {
2816                 codec_info(codec, "No i915 binding for Intel HDMI/DP codec\n");
2817                 /* set probe_id here to prevent generic fallback binding */
2818                 codec->probe_id = HDA_CODEC_ID_SKIP_PROBE;
2819                 return -ENODEV;
2820         }
2821
2822         err = alloc_generic_hdmi(codec);
2823         if (err < 0)
2824                 return err;
2825         /* no need to handle unsol events */
2826         codec->patch_ops.unsol_event = NULL;
2827         return 0;
2828 }
2829
2830 /* parse and post-process for Intel codecs */
2831 static int parse_intel_hdmi(struct hda_codec *codec)
2832 {
2833         int err;
2834
2835         err = hdmi_parse_codec(codec);
2836         if (err < 0) {
2837                 generic_spec_free(codec);
2838                 return err;
2839         }
2840
2841         generic_hdmi_init_per_pins(codec);
2842         register_i915_notifier(codec);
2843         return 0;
2844 }
2845
2846 /* Intel Haswell and onwards; audio component with eld notifier */
2847 static int intel_hsw_common_init(struct hda_codec *codec, hda_nid_t vendor_nid,
2848                                  const int *port_map, int port_num)
2849 {
2850         struct hdmi_spec *spec;
2851         int err;
2852
2853         err = alloc_intel_hdmi(codec);
2854         if (err < 0)
2855                 return err;
2856         spec = codec->spec;
2857         codec->dp_mst = true;
2858         spec->dyn_pcm_assign = true;
2859         spec->vendor_nid = vendor_nid;
2860         spec->port_map = port_map;
2861         spec->port_num = port_num;
2862         spec->intel_hsw_fixup = true;
2863
2864         intel_haswell_enable_all_pins(codec, true);
2865         intel_haswell_fixup_enable_dp12(codec);
2866
2867         codec->display_power_control = 1;
2868
2869         codec->patch_ops.set_power_state = haswell_set_power_state;
2870         codec->depop_delay = 0;
2871         codec->auto_runtime_pm = 1;
2872
2873         spec->ops.setup_stream = i915_hsw_setup_stream;
2874         spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
2875
2876         return parse_intel_hdmi(codec);
2877 }
2878
2879 static int patch_i915_hsw_hdmi(struct hda_codec *codec)
2880 {
2881         return intel_hsw_common_init(codec, 0x08, NULL, 0);
2882 }
2883
2884 static int patch_i915_glk_hdmi(struct hda_codec *codec)
2885 {
2886         return intel_hsw_common_init(codec, 0x0b, NULL, 0);
2887 }
2888
2889 static int patch_i915_icl_hdmi(struct hda_codec *codec)
2890 {
2891         /*
2892          * pin to port mapping table where the value indicate the pin number and
2893          * the index indicate the port number.
2894          */
2895         static const int map[] = {0x0, 0x4, 0x6, 0x8, 0xa, 0xb};
2896
2897         return intel_hsw_common_init(codec, 0x02, map, ARRAY_SIZE(map));
2898 }
2899
2900 static int patch_i915_tgl_hdmi(struct hda_codec *codec)
2901 {
2902         /*
2903          * pin to port mapping table where the value indicate the pin number and
2904          * the index indicate the port number.
2905          */
2906         static const int map[] = {0x4, 0x6, 0x8, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
2907
2908         return intel_hsw_common_init(codec, 0x02, map, ARRAY_SIZE(map));
2909 }
2910
2911 /* Intel Baytrail and Braswell; with eld notifier */
2912 static int patch_i915_byt_hdmi(struct hda_codec *codec)
2913 {
2914         struct hdmi_spec *spec;
2915         int err;
2916
2917         err = alloc_intel_hdmi(codec);
2918         if (err < 0)
2919                 return err;
2920         spec = codec->spec;
2921
2922         /* For Valleyview/Cherryview, only the display codec is in the display
2923          * power well and can use link_power ops to request/release the power.
2924          */
2925         codec->display_power_control = 1;
2926
2927         codec->depop_delay = 0;
2928         codec->auto_runtime_pm = 1;
2929
2930         spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
2931
2932         return parse_intel_hdmi(codec);
2933 }
2934
2935 /* Intel IronLake, SandyBridge and IvyBridge; with eld notifier */
2936 static int patch_i915_cpt_hdmi(struct hda_codec *codec)
2937 {
2938         int err;
2939
2940         err = alloc_intel_hdmi(codec);
2941         if (err < 0)
2942                 return err;
2943         return parse_intel_hdmi(codec);
2944 }
2945
2946 /*
2947  * Shared non-generic implementations
2948  */
2949
2950 static int simple_playback_build_pcms(struct hda_codec *codec)
2951 {
2952         struct hdmi_spec *spec = codec->spec;
2953         struct hda_pcm *info;
2954         unsigned int chans;
2955         struct hda_pcm_stream *pstr;
2956         struct hdmi_spec_per_cvt *per_cvt;
2957
2958         per_cvt = get_cvt(spec, 0);
2959         chans = get_wcaps(codec, per_cvt->cvt_nid);
2960         chans = get_wcaps_channels(chans);
2961
2962         info = snd_hda_codec_pcm_new(codec, "HDMI 0");
2963         if (!info)
2964                 return -ENOMEM;
2965         spec->pcm_rec[0].pcm = info;
2966         info->pcm_type = HDA_PCM_TYPE_HDMI;
2967         pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2968         *pstr = spec->pcm_playback;
2969         pstr->nid = per_cvt->cvt_nid;
2970         if (pstr->channels_max <= 2 && chans && chans <= 16)
2971                 pstr->channels_max = chans;
2972
2973         return 0;
2974 }
2975
2976 /* unsolicited event for jack sensing */
2977 static void simple_hdmi_unsol_event(struct hda_codec *codec,
2978                                     unsigned int res)
2979 {
2980         snd_hda_jack_set_dirty_all(codec);
2981         snd_hda_jack_report_sync(codec);
2982 }
2983
2984 /* generic_hdmi_build_jack can be used for simple_hdmi, too,
2985  * as long as spec->pins[] is set correctly
2986  */
2987 #define simple_hdmi_build_jack  generic_hdmi_build_jack
2988
2989 static int simple_playback_build_controls(struct hda_codec *codec)
2990 {
2991         struct hdmi_spec *spec = codec->spec;
2992         struct hdmi_spec_per_cvt *per_cvt;
2993         int err;
2994
2995         per_cvt = get_cvt(spec, 0);
2996         err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2997                                           per_cvt->cvt_nid,
2998                                           HDA_PCM_TYPE_HDMI);
2999         if (err < 0)
3000                 return err;
3001         return simple_hdmi_build_jack(codec, 0);
3002 }
3003
3004 static int simple_playback_init(struct hda_codec *codec)
3005 {
3006         struct hdmi_spec *spec = codec->spec;
3007         struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
3008         hda_nid_t pin = per_pin->pin_nid;
3009
3010         snd_hda_codec_write(codec, pin, 0,
3011                             AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
3012         /* some codecs require to unmute the pin */
3013         if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
3014                 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3015                                     AMP_OUT_UNMUTE);
3016         snd_hda_jack_detect_enable(codec, pin, per_pin->dev_id);
3017         return 0;
3018 }
3019
3020 static void simple_playback_free(struct hda_codec *codec)
3021 {
3022         struct hdmi_spec *spec = codec->spec;
3023
3024         hdmi_array_free(spec);
3025         kfree(spec);
3026 }
3027
3028 /*
3029  * Nvidia specific implementations
3030  */
3031
3032 #define Nv_VERB_SET_Channel_Allocation          0xF79
3033 #define Nv_VERB_SET_Info_Frame_Checksum         0xF7A
3034 #define Nv_VERB_SET_Audio_Protection_On         0xF98
3035 #define Nv_VERB_SET_Audio_Protection_Off        0xF99
3036
3037 #define nvhdmi_master_con_nid_7x        0x04
3038 #define nvhdmi_master_pin_nid_7x        0x05
3039
3040 static const hda_nid_t nvhdmi_con_nids_7x[4] = {
3041         /*front, rear, clfe, rear_surr */
3042         0x6, 0x8, 0xa, 0xc,
3043 };
3044
3045 static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
3046         /* set audio protect on */
3047         { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3048         /* enable digital output on pin widget */
3049         { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3050         {} /* terminator */
3051 };
3052
3053 static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
3054         /* set audio protect on */
3055         { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3056         /* enable digital output on pin widget */
3057         { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3058         { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3059         { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3060         { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3061         { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3062         {} /* terminator */
3063 };
3064
3065 #ifdef LIMITED_RATE_FMT_SUPPORT
3066 /* support only the safe format and rate */
3067 #define SUPPORTED_RATES         SNDRV_PCM_RATE_48000
3068 #define SUPPORTED_MAXBPS        16
3069 #define SUPPORTED_FORMATS       SNDRV_PCM_FMTBIT_S16_LE
3070 #else
3071 /* support all rates and formats */
3072 #define SUPPORTED_RATES \
3073         (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
3074         SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
3075          SNDRV_PCM_RATE_192000)
3076 #define SUPPORTED_MAXBPS        24
3077 #define SUPPORTED_FORMATS \
3078         (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
3079 #endif
3080
3081 static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
3082 {
3083         snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
3084         return 0;
3085 }
3086
3087 static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
3088 {
3089         snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
3090         return 0;
3091 }
3092
3093 static const unsigned int channels_2_6_8[] = {
3094         2, 6, 8
3095 };
3096
3097 static const unsigned int channels_2_8[] = {
3098         2, 8
3099 };
3100
3101 static const struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
3102         .count = ARRAY_SIZE(channels_2_6_8),
3103         .list = channels_2_6_8,
3104         .mask = 0,
3105 };
3106
3107 static const struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
3108         .count = ARRAY_SIZE(channels_2_8),
3109         .list = channels_2_8,
3110         .mask = 0,
3111 };
3112
3113 static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
3114                                     struct hda_codec *codec,
3115                                     struct snd_pcm_substream *substream)
3116 {
3117         struct hdmi_spec *spec = codec->spec;
3118         const struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
3119
3120         switch (codec->preset->vendor_id) {
3121         case 0x10de0002:
3122         case 0x10de0003:
3123         case 0x10de0005:
3124         case 0x10de0006:
3125                 hw_constraints_channels = &hw_constraints_2_8_channels;
3126                 break;
3127         case 0x10de0007:
3128                 hw_constraints_channels = &hw_constraints_2_6_8_channels;
3129                 break;
3130         default:
3131                 break;
3132         }
3133
3134         if (hw_constraints_channels != NULL) {
3135                 snd_pcm_hw_constraint_list(substream->runtime, 0,
3136                                 SNDRV_PCM_HW_PARAM_CHANNELS,
3137                                 hw_constraints_channels);
3138         } else {
3139                 snd_pcm_hw_constraint_step(substream->runtime, 0,
3140                                            SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3141         }
3142
3143         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3144 }
3145
3146 static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
3147                                      struct hda_codec *codec,
3148                                      struct snd_pcm_substream *substream)
3149 {
3150         struct hdmi_spec *spec = codec->spec;
3151         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3152 }
3153
3154 static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3155                                        struct hda_codec *codec,
3156                                        unsigned int stream_tag,
3157                                        unsigned int format,
3158                                        struct snd_pcm_substream *substream)
3159 {
3160         struct hdmi_spec *spec = codec->spec;
3161         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3162                                              stream_tag, format, substream);
3163 }
3164
3165 static const struct hda_pcm_stream simple_pcm_playback = {
3166         .substreams = 1,
3167         .channels_min = 2,
3168         .channels_max = 2,
3169         .ops = {
3170                 .open = simple_playback_pcm_open,
3171                 .close = simple_playback_pcm_close,
3172                 .prepare = simple_playback_pcm_prepare
3173         },
3174 };
3175
3176 static const struct hda_codec_ops simple_hdmi_patch_ops = {
3177         .build_controls = simple_playback_build_controls,
3178         .build_pcms = simple_playback_build_pcms,
3179         .init = simple_playback_init,
3180         .free = simple_playback_free,
3181         .unsol_event = simple_hdmi_unsol_event,
3182 };
3183
3184 static int patch_simple_hdmi(struct hda_codec *codec,
3185                              hda_nid_t cvt_nid, hda_nid_t pin_nid)
3186 {
3187         struct hdmi_spec *spec;
3188         struct hdmi_spec_per_cvt *per_cvt;
3189         struct hdmi_spec_per_pin *per_pin;
3190
3191         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3192         if (!spec)
3193                 return -ENOMEM;
3194
3195         spec->codec = codec;
3196         codec->spec = spec;
3197         hdmi_array_init(spec, 1);
3198
3199         spec->multiout.num_dacs = 0;  /* no analog */
3200         spec->multiout.max_channels = 2;
3201         spec->multiout.dig_out_nid = cvt_nid;
3202         spec->num_cvts = 1;
3203         spec->num_pins = 1;
3204         per_pin = snd_array_new(&spec->pins);
3205         per_cvt = snd_array_new(&spec->cvts);
3206         if (!per_pin || !per_cvt) {
3207                 simple_playback_free(codec);
3208                 return -ENOMEM;
3209         }
3210         per_cvt->cvt_nid = cvt_nid;
3211         per_pin->pin_nid = pin_nid;
3212         spec->pcm_playback = simple_pcm_playback;
3213
3214         codec->patch_ops = simple_hdmi_patch_ops;
3215
3216         return 0;
3217 }
3218
3219 static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
3220               &