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