Merge remote-tracking branch 'lsk/v3.10/topic/gator' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.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  *
10  *  Authors:
11  *                      Wu Fengguang <wfg@linux.intel.com>
12  *
13  *  Maintained by:
14  *                      Wu Fengguang <wfg@linux.intel.com>
15  *
16  *  This program is free software; you can redistribute it and/or modify it
17  *  under the terms of the GNU General Public License as published by the Free
18  *  Software Foundation; either version 2 of the License, or (at your option)
19  *  any later version.
20  *
21  *  This program is distributed in the hope that it will be useful, but
22  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23  *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24  *  for more details.
25  *
26  *  You should have received a copy of the GNU General Public License
27  *  along with this program; if not, write to the Free Software Foundation,
28  *  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
29  */
30
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/slab.h>
34 #include <linux/module.h>
35 #include <sound/core.h>
36 #include <sound/jack.h>
37 #include <sound/asoundef.h>
38 #include <sound/tlv.h>
39 #include "hda_codec.h"
40 #include "hda_local.h"
41 #include "hda_jack.h"
42
43 static bool static_hdmi_pcm;
44 module_param(static_hdmi_pcm, bool, 0644);
45 MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
46
47 struct hdmi_spec_per_cvt {
48         hda_nid_t cvt_nid;
49         int assigned;
50         unsigned int channels_min;
51         unsigned int channels_max;
52         u32 rates;
53         u64 formats;
54         unsigned int maxbps;
55 };
56
57 /* max. connections to a widget */
58 #define HDA_MAX_CONNECTIONS     32
59
60 struct hdmi_spec_per_pin {
61         hda_nid_t pin_nid;
62         int num_mux_nids;
63         hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
64
65         struct hda_codec *codec;
66         struct hdmi_eld sink_eld;
67         struct delayed_work work;
68         struct snd_kcontrol *eld_ctl;
69         int repoll_count;
70         bool setup; /* the stream has been set up by prepare callback */
71         int channels; /* current number of channels */
72         bool non_pcm;
73         bool chmap_set;         /* channel-map override by ALSA API? */
74         unsigned char chmap[8]; /* ALSA API channel-map */
75         char pcm_name[8];       /* filled in build_pcm callbacks */
76 };
77
78 struct hdmi_spec {
79         int num_cvts;
80         struct snd_array cvts; /* struct hdmi_spec_per_cvt */
81         hda_nid_t cvt_nids[4]; /* only for haswell fix */
82
83         int num_pins;
84         struct snd_array pins; /* struct hdmi_spec_per_pin */
85         struct snd_array pcm_rec; /* struct hda_pcm */
86         unsigned int channels_max; /* max over all cvts */
87
88         struct hdmi_eld temp_eld;
89
90         bool dyn_pin_out;
91
92         /*
93          * Non-generic ATI/NVIDIA specific
94          */
95         struct hda_multi_out multiout;
96         struct hda_pcm_stream pcm_playback;
97 };
98
99
100 struct hdmi_audio_infoframe {
101         u8 type; /* 0x84 */
102         u8 ver;  /* 0x01 */
103         u8 len;  /* 0x0a */
104
105         u8 checksum;
106
107         u8 CC02_CT47;   /* CC in bits 0:2, CT in 4:7 */
108         u8 SS01_SF24;
109         u8 CXT04;
110         u8 CA;
111         u8 LFEPBL01_LSV36_DM_INH7;
112 };
113
114 struct dp_audio_infoframe {
115         u8 type; /* 0x84 */
116         u8 len;  /* 0x1b */
117         u8 ver;  /* 0x11 << 2 */
118
119         u8 CC02_CT47;   /* match with HDMI infoframe from this on */
120         u8 SS01_SF24;
121         u8 CXT04;
122         u8 CA;
123         u8 LFEPBL01_LSV36_DM_INH7;
124 };
125
126 union audio_infoframe {
127         struct hdmi_audio_infoframe hdmi;
128         struct dp_audio_infoframe dp;
129         u8 bytes[0];
130 };
131
132 /*
133  * CEA speaker placement:
134  *
135  *        FLH       FCH        FRH
136  *  FLW    FL  FLC   FC   FRC   FR   FRW
137  *
138  *                                  LFE
139  *                     TC
140  *
141  *          RL  RLC   RC   RRC   RR
142  *
143  * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
144  * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
145  */
146 enum cea_speaker_placement {
147         FL  = (1 <<  0),        /* Front Left           */
148         FC  = (1 <<  1),        /* Front Center         */
149         FR  = (1 <<  2),        /* Front Right          */
150         FLC = (1 <<  3),        /* Front Left Center    */
151         FRC = (1 <<  4),        /* Front Right Center   */
152         RL  = (1 <<  5),        /* Rear Left            */
153         RC  = (1 <<  6),        /* Rear Center          */
154         RR  = (1 <<  7),        /* Rear Right           */
155         RLC = (1 <<  8),        /* Rear Left Center     */
156         RRC = (1 <<  9),        /* Rear Right Center    */
157         LFE = (1 << 10),        /* Low Frequency Effect */
158         FLW = (1 << 11),        /* Front Left Wide      */
159         FRW = (1 << 12),        /* Front Right Wide     */
160         FLH = (1 << 13),        /* Front Left High      */
161         FCH = (1 << 14),        /* Front Center High    */
162         FRH = (1 << 15),        /* Front Right High     */
163         TC  = (1 << 16),        /* Top Center           */
164 };
165
166 /*
167  * ELD SA bits in the CEA Speaker Allocation data block
168  */
169 static int eld_speaker_allocation_bits[] = {
170         [0] = FL | FR,
171         [1] = LFE,
172         [2] = FC,
173         [3] = RL | RR,
174         [4] = RC,
175         [5] = FLC | FRC,
176         [6] = RLC | RRC,
177         /* the following are not defined in ELD yet */
178         [7] = FLW | FRW,
179         [8] = FLH | FRH,
180         [9] = TC,
181         [10] = FCH,
182 };
183
184 struct cea_channel_speaker_allocation {
185         int ca_index;
186         int speakers[8];
187
188         /* derived values, just for convenience */
189         int channels;
190         int spk_mask;
191 };
192
193 /*
194  * ALSA sequence is:
195  *
196  *       surround40   surround41   surround50   surround51   surround71
197  * ch0   front left   =            =            =            =
198  * ch1   front right  =            =            =            =
199  * ch2   rear left    =            =            =            =
200  * ch3   rear right   =            =            =            =
201  * ch4                LFE          center       center       center
202  * ch5                                          LFE          LFE
203  * ch6                                                       side left
204  * ch7                                                       side right
205  *
206  * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
207  */
208 static int hdmi_channel_mapping[0x32][8] = {
209         /* stereo */
210         [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
211         /* 2.1 */
212         [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
213         /* Dolby Surround */
214         [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
215         /* surround40 */
216         [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
217         /* 4ch */
218         [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
219         /* surround41 */
220         [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
221         /* surround50 */
222         [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
223         /* surround51 */
224         [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
225         /* 7.1 */
226         [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
227 };
228
229 /*
230  * This is an ordered list!
231  *
232  * The preceding ones have better chances to be selected by
233  * hdmi_channel_allocation().
234  */
235 static struct cea_channel_speaker_allocation channel_allocations[] = {
236 /*                        channel:   7     6    5    4    3     2    1    0  */
237 { .ca_index = 0x00,  .speakers = {   0,    0,   0,   0,   0,    0,  FR,  FL } },
238                                  /* 2.1 */
239 { .ca_index = 0x01,  .speakers = {   0,    0,   0,   0,   0,  LFE,  FR,  FL } },
240                                  /* Dolby Surround */
241 { .ca_index = 0x02,  .speakers = {   0,    0,   0,   0,  FC,    0,  FR,  FL } },
242                                  /* surround40 */
243 { .ca_index = 0x08,  .speakers = {   0,    0,  RR,  RL,   0,    0,  FR,  FL } },
244                                  /* surround41 */
245 { .ca_index = 0x09,  .speakers = {   0,    0,  RR,  RL,   0,  LFE,  FR,  FL } },
246                                  /* surround50 */
247 { .ca_index = 0x0a,  .speakers = {   0,    0,  RR,  RL,  FC,    0,  FR,  FL } },
248                                  /* surround51 */
249 { .ca_index = 0x0b,  .speakers = {   0,    0,  RR,  RL,  FC,  LFE,  FR,  FL } },
250                                  /* 6.1 */
251 { .ca_index = 0x0f,  .speakers = {   0,   RC,  RR,  RL,  FC,  LFE,  FR,  FL } },
252                                  /* surround71 */
253 { .ca_index = 0x13,  .speakers = { RRC,  RLC,  RR,  RL,  FC,  LFE,  FR,  FL } },
254
255 { .ca_index = 0x03,  .speakers = {   0,    0,   0,   0,  FC,  LFE,  FR,  FL } },
256 { .ca_index = 0x04,  .speakers = {   0,    0,   0,  RC,   0,    0,  FR,  FL } },
257 { .ca_index = 0x05,  .speakers = {   0,    0,   0,  RC,   0,  LFE,  FR,  FL } },
258 { .ca_index = 0x06,  .speakers = {   0,    0,   0,  RC,  FC,    0,  FR,  FL } },
259 { .ca_index = 0x07,  .speakers = {   0,    0,   0,  RC,  FC,  LFE,  FR,  FL } },
260 { .ca_index = 0x0c,  .speakers = {   0,   RC,  RR,  RL,   0,    0,  FR,  FL } },
261 { .ca_index = 0x0d,  .speakers = {   0,   RC,  RR,  RL,   0,  LFE,  FR,  FL } },
262 { .ca_index = 0x0e,  .speakers = {   0,   RC,  RR,  RL,  FC,    0,  FR,  FL } },
263 { .ca_index = 0x10,  .speakers = { RRC,  RLC,  RR,  RL,   0,    0,  FR,  FL } },
264 { .ca_index = 0x11,  .speakers = { RRC,  RLC,  RR,  RL,   0,  LFE,  FR,  FL } },
265 { .ca_index = 0x12,  .speakers = { RRC,  RLC,  RR,  RL,  FC,    0,  FR,  FL } },
266 { .ca_index = 0x14,  .speakers = { FRC,  FLC,   0,   0,   0,    0,  FR,  FL } },
267 { .ca_index = 0x15,  .speakers = { FRC,  FLC,   0,   0,   0,  LFE,  FR,  FL } },
268 { .ca_index = 0x16,  .speakers = { FRC,  FLC,   0,   0,  FC,    0,  FR,  FL } },
269 { .ca_index = 0x17,  .speakers = { FRC,  FLC,   0,   0,  FC,  LFE,  FR,  FL } },
270 { .ca_index = 0x18,  .speakers = { FRC,  FLC,   0,  RC,   0,    0,  FR,  FL } },
271 { .ca_index = 0x19,  .speakers = { FRC,  FLC,   0,  RC,   0,  LFE,  FR,  FL } },
272 { .ca_index = 0x1a,  .speakers = { FRC,  FLC,   0,  RC,  FC,    0,  FR,  FL } },
273 { .ca_index = 0x1b,  .speakers = { FRC,  FLC,   0,  RC,  FC,  LFE,  FR,  FL } },
274 { .ca_index = 0x1c,  .speakers = { FRC,  FLC,  RR,  RL,   0,    0,  FR,  FL } },
275 { .ca_index = 0x1d,  .speakers = { FRC,  FLC,  RR,  RL,   0,  LFE,  FR,  FL } },
276 { .ca_index = 0x1e,  .speakers = { FRC,  FLC,  RR,  RL,  FC,    0,  FR,  FL } },
277 { .ca_index = 0x1f,  .speakers = { FRC,  FLC,  RR,  RL,  FC,  LFE,  FR,  FL } },
278 { .ca_index = 0x20,  .speakers = {   0,  FCH,  RR,  RL,  FC,    0,  FR,  FL } },
279 { .ca_index = 0x21,  .speakers = {   0,  FCH,  RR,  RL,  FC,  LFE,  FR,  FL } },
280 { .ca_index = 0x22,  .speakers = {  TC,    0,  RR,  RL,  FC,    0,  FR,  FL } },
281 { .ca_index = 0x23,  .speakers = {  TC,    0,  RR,  RL,  FC,  LFE,  FR,  FL } },
282 { .ca_index = 0x24,  .speakers = { FRH,  FLH,  RR,  RL,   0,    0,  FR,  FL } },
283 { .ca_index = 0x25,  .speakers = { FRH,  FLH,  RR,  RL,   0,  LFE,  FR,  FL } },
284 { .ca_index = 0x26,  .speakers = { FRW,  FLW,  RR,  RL,   0,    0,  FR,  FL } },
285 { .ca_index = 0x27,  .speakers = { FRW,  FLW,  RR,  RL,   0,  LFE,  FR,  FL } },
286 { .ca_index = 0x28,  .speakers = {  TC,   RC,  RR,  RL,  FC,    0,  FR,  FL } },
287 { .ca_index = 0x29,  .speakers = {  TC,   RC,  RR,  RL,  FC,  LFE,  FR,  FL } },
288 { .ca_index = 0x2a,  .speakers = { FCH,   RC,  RR,  RL,  FC,    0,  FR,  FL } },
289 { .ca_index = 0x2b,  .speakers = { FCH,   RC,  RR,  RL,  FC,  LFE,  FR,  FL } },
290 { .ca_index = 0x2c,  .speakers = {  TC,  FCH,  RR,  RL,  FC,    0,  FR,  FL } },
291 { .ca_index = 0x2d,  .speakers = {  TC,  FCH,  RR,  RL,  FC,  LFE,  FR,  FL } },
292 { .ca_index = 0x2e,  .speakers = { FRH,  FLH,  RR,  RL,  FC,    0,  FR,  FL } },
293 { .ca_index = 0x2f,  .speakers = { FRH,  FLH,  RR,  RL,  FC,  LFE,  FR,  FL } },
294 { .ca_index = 0x30,  .speakers = { FRW,  FLW,  RR,  RL,  FC,    0,  FR,  FL } },
295 { .ca_index = 0x31,  .speakers = { FRW,  FLW,  RR,  RL,  FC,  LFE,  FR,  FL } },
296 };
297
298
299 /*
300  * HDMI routines
301  */
302
303 #define get_pin(spec, idx) \
304         ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
305 #define get_cvt(spec, idx) \
306         ((struct hdmi_spec_per_cvt  *)snd_array_elem(&spec->cvts, idx))
307 #define get_pcm_rec(spec, idx) \
308         ((struct hda_pcm *)snd_array_elem(&spec->pcm_rec, idx))
309
310 static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
311 {
312         int pin_idx;
313
314         for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
315                 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
316                         return pin_idx;
317
318         snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
319         return -EINVAL;
320 }
321
322 static int hinfo_to_pin_index(struct hdmi_spec *spec,
323                               struct hda_pcm_stream *hinfo)
324 {
325         int pin_idx;
326
327         for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
328                 if (get_pcm_rec(spec, pin_idx)->stream == hinfo)
329                         return pin_idx;
330
331         snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
332         return -EINVAL;
333 }
334
335 static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
336 {
337         int cvt_idx;
338
339         for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
340                 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
341                         return cvt_idx;
342
343         snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
344         return -EINVAL;
345 }
346
347 static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
348                         struct snd_ctl_elem_info *uinfo)
349 {
350         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
351         struct hdmi_spec *spec = codec->spec;
352         struct hdmi_eld *eld;
353         int pin_idx;
354
355         uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
356
357         pin_idx = kcontrol->private_value;
358         eld = &get_pin(spec, pin_idx)->sink_eld;
359
360         mutex_lock(&eld->lock);
361         uinfo->count = eld->eld_valid ? eld->eld_size : 0;
362         mutex_unlock(&eld->lock);
363
364         return 0;
365 }
366
367 static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
368                         struct snd_ctl_elem_value *ucontrol)
369 {
370         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
371         struct hdmi_spec *spec = codec->spec;
372         struct hdmi_eld *eld;
373         int pin_idx;
374
375         pin_idx = kcontrol->private_value;
376         eld = &get_pin(spec, pin_idx)->sink_eld;
377
378         mutex_lock(&eld->lock);
379         if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
380                 mutex_unlock(&eld->lock);
381                 snd_BUG();
382                 return -EINVAL;
383         }
384
385         memset(ucontrol->value.bytes.data, 0,
386                ARRAY_SIZE(ucontrol->value.bytes.data));
387         if (eld->eld_valid)
388                 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
389                        eld->eld_size);
390         mutex_unlock(&eld->lock);
391
392         return 0;
393 }
394
395 static struct snd_kcontrol_new eld_bytes_ctl = {
396         .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
397         .iface = SNDRV_CTL_ELEM_IFACE_PCM,
398         .name = "ELD",
399         .info = hdmi_eld_ctl_info,
400         .get = hdmi_eld_ctl_get,
401 };
402
403 static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
404                         int device)
405 {
406         struct snd_kcontrol *kctl;
407         struct hdmi_spec *spec = codec->spec;
408         int err;
409
410         kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
411         if (!kctl)
412                 return -ENOMEM;
413         kctl->private_value = pin_idx;
414         kctl->id.device = device;
415
416         err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
417         if (err < 0)
418                 return err;
419
420         get_pin(spec, pin_idx)->eld_ctl = kctl;
421         return 0;
422 }
423
424 #ifdef BE_PARANOID
425 static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
426                                 int *packet_index, int *byte_index)
427 {
428         int val;
429
430         val = snd_hda_codec_read(codec, pin_nid, 0,
431                                  AC_VERB_GET_HDMI_DIP_INDEX, 0);
432
433         *packet_index = val >> 5;
434         *byte_index = val & 0x1f;
435 }
436 #endif
437
438 static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
439                                 int packet_index, int byte_index)
440 {
441         int val;
442
443         val = (packet_index << 5) | (byte_index & 0x1f);
444
445         snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
446 }
447
448 static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
449                                 unsigned char val)
450 {
451         snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
452 }
453
454 static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
455 {
456         struct hdmi_spec *spec = codec->spec;
457         int pin_out;
458
459         /* Unmute */
460         if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
461                 snd_hda_codec_write(codec, pin_nid, 0,
462                                 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
463
464         if (spec->dyn_pin_out)
465                 /* Disable pin out until stream is active */
466                 pin_out = 0;
467         else
468                 /* Enable pin out: some machines with GM965 gets broken output
469                  * when the pin is disabled or changed while using with HDMI
470                  */
471                 pin_out = PIN_OUT;
472
473         snd_hda_codec_write(codec, pin_nid, 0,
474                             AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
475 }
476
477 static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
478 {
479         return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
480                                         AC_VERB_GET_CVT_CHAN_COUNT, 0);
481 }
482
483 static void hdmi_set_channel_count(struct hda_codec *codec,
484                                    hda_nid_t cvt_nid, int chs)
485 {
486         if (chs != hdmi_get_channel_count(codec, cvt_nid))
487                 snd_hda_codec_write(codec, cvt_nid, 0,
488                                     AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
489 }
490
491
492 /*
493  * Channel mapping routines
494  */
495
496 /*
497  * Compute derived values in channel_allocations[].
498  */
499 static void init_channel_allocations(void)
500 {
501         int i, j;
502         struct cea_channel_speaker_allocation *p;
503
504         for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
505                 p = channel_allocations + i;
506                 p->channels = 0;
507                 p->spk_mask = 0;
508                 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
509                         if (p->speakers[j]) {
510                                 p->channels++;
511                                 p->spk_mask |= p->speakers[j];
512                         }
513         }
514 }
515
516 static int get_channel_allocation_order(int ca)
517 {
518         int i;
519
520         for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
521                 if (channel_allocations[i].ca_index == ca)
522                         break;
523         }
524         return i;
525 }
526
527 /*
528  * The transformation takes two steps:
529  *
530  *      eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
531  *            spk_mask => (channel_allocations[])         => ai->CA
532  *
533  * TODO: it could select the wrong CA from multiple candidates.
534 */
535 static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
536 {
537         int i;
538         int ca = 0;
539         int spk_mask = 0;
540         char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
541
542         /*
543          * CA defaults to 0 for basic stereo audio
544          */
545         if (channels <= 2)
546                 return 0;
547
548         /*
549          * expand ELD's speaker allocation mask
550          *
551          * ELD tells the speaker mask in a compact(paired) form,
552          * expand ELD's notions to match the ones used by Audio InfoFrame.
553          */
554         for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
555                 if (eld->info.spk_alloc & (1 << i))
556                         spk_mask |= eld_speaker_allocation_bits[i];
557         }
558
559         /* search for the first working match in the CA table */
560         for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
561                 if (channels == channel_allocations[i].channels &&
562                     (spk_mask & channel_allocations[i].spk_mask) ==
563                                 channel_allocations[i].spk_mask) {
564                         ca = channel_allocations[i].ca_index;
565                         break;
566                 }
567         }
568
569         if (!ca) {
570                 /* if there was no match, select the regular ALSA channel
571                  * allocation with the matching number of channels */
572                 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
573                         if (channels == channel_allocations[i].channels) {
574                                 ca = channel_allocations[i].ca_index;
575                                 break;
576                         }
577                 }
578         }
579
580         snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
581         snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
582                     ca, channels, buf);
583
584         return ca;
585 }
586
587 static void hdmi_debug_channel_mapping(struct hda_codec *codec,
588                                        hda_nid_t pin_nid)
589 {
590 #ifdef CONFIG_SND_DEBUG_VERBOSE
591         int i;
592         int slot;
593
594         for (i = 0; i < 8; i++) {
595                 slot = snd_hda_codec_read(codec, pin_nid, 0,
596                                                 AC_VERB_GET_HDMI_CHAN_SLOT, i);
597                 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
598                                                 slot >> 4, slot & 0xf);
599         }
600 #endif
601 }
602
603
604 static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
605                                        hda_nid_t pin_nid,
606                                        bool non_pcm,
607                                        int ca)
608 {
609         int i;
610         int err;
611         int order;
612         int non_pcm_mapping[8];
613
614         order = get_channel_allocation_order(ca);
615
616         if (hdmi_channel_mapping[ca][1] == 0) {
617                 for (i = 0; i < channel_allocations[order].channels; i++)
618                         hdmi_channel_mapping[ca][i] = i | (i << 4);
619                 for (; i < 8; i++)
620                         hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
621         }
622
623         if (non_pcm) {
624                 for (i = 0; i < channel_allocations[order].channels; i++)
625                         non_pcm_mapping[i] = i | (i << 4);
626                 for (; i < 8; i++)
627                         non_pcm_mapping[i] = 0xf | (i << 4);
628         }
629
630         for (i = 0; i < 8; i++) {
631                 err = snd_hda_codec_write(codec, pin_nid, 0,
632                                           AC_VERB_SET_HDMI_CHAN_SLOT,
633                                           non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i]);
634                 if (err) {
635                         snd_printdd(KERN_NOTICE
636                                     "HDMI: channel mapping failed\n");
637                         break;
638                 }
639         }
640
641         hdmi_debug_channel_mapping(codec, pin_nid);
642 }
643
644 struct channel_map_table {
645         unsigned char map;              /* ALSA API channel map position */
646         unsigned char cea_slot;         /* CEA slot value */
647         int spk_mask;                   /* speaker position bit mask */
648 };
649
650 static struct channel_map_table map_tables[] = {
651         { SNDRV_CHMAP_FL,       0x00,   FL },
652         { SNDRV_CHMAP_FR,       0x01,   FR },
653         { SNDRV_CHMAP_RL,       0x04,   RL },
654         { SNDRV_CHMAP_RR,       0x05,   RR },
655         { SNDRV_CHMAP_LFE,      0x02,   LFE },
656         { SNDRV_CHMAP_FC,       0x03,   FC },
657         { SNDRV_CHMAP_RLC,      0x06,   RLC },
658         { SNDRV_CHMAP_RRC,      0x07,   RRC },
659         {} /* terminator */
660 };
661
662 /* from ALSA API channel position to speaker bit mask */
663 static int to_spk_mask(unsigned char c)
664 {
665         struct channel_map_table *t = map_tables;
666         for (; t->map; t++) {
667                 if (t->map == c)
668                         return t->spk_mask;
669         }
670         return 0;
671 }
672
673 /* from ALSA API channel position to CEA slot */
674 static int to_cea_slot(unsigned char c)
675 {
676         struct channel_map_table *t = map_tables;
677         for (; t->map; t++) {
678                 if (t->map == c)
679                         return t->cea_slot;
680         }
681         return 0x0f;
682 }
683
684 /* from CEA slot to ALSA API channel position */
685 static int from_cea_slot(unsigned char c)
686 {
687         struct channel_map_table *t = map_tables;
688         for (; t->map; t++) {
689                 if (t->cea_slot == c)
690                         return t->map;
691         }
692         return 0;
693 }
694
695 /* from speaker bit mask to ALSA API channel position */
696 static int spk_to_chmap(int spk)
697 {
698         struct channel_map_table *t = map_tables;
699         for (; t->map; t++) {
700                 if (t->spk_mask == spk)
701                         return t->map;
702         }
703         return 0;
704 }
705
706 /* get the CA index corresponding to the given ALSA API channel map */
707 static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
708 {
709         int i, spks = 0, spk_mask = 0;
710
711         for (i = 0; i < chs; i++) {
712                 int mask = to_spk_mask(map[i]);
713                 if (mask) {
714                         spk_mask |= mask;
715                         spks++;
716                 }
717         }
718
719         for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
720                 if ((chs == channel_allocations[i].channels ||
721                      spks == channel_allocations[i].channels) &&
722                     (spk_mask & channel_allocations[i].spk_mask) ==
723                                 channel_allocations[i].spk_mask)
724                         return channel_allocations[i].ca_index;
725         }
726         return -1;
727 }
728
729 /* set up the channel slots for the given ALSA API channel map */
730 static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
731                                              hda_nid_t pin_nid,
732                                              int chs, unsigned char *map)
733 {
734         int i;
735         for (i = 0; i < 8; i++) {
736                 int val, err;
737                 if (i < chs)
738                         val = to_cea_slot(map[i]);
739                 else
740                         val = 0xf;
741                 val |= (i << 4);
742                 err = snd_hda_codec_write(codec, pin_nid, 0,
743                                           AC_VERB_SET_HDMI_CHAN_SLOT, val);
744                 if (err)
745                         return -EINVAL;
746         }
747         return 0;
748 }
749
750 /* store ALSA API channel map from the current default map */
751 static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
752 {
753         int i;
754         int ordered_ca = get_channel_allocation_order(ca);
755         for (i = 0; i < 8; i++) {
756                 if (i < channel_allocations[ordered_ca].channels)
757                         map[i] = from_cea_slot(hdmi_channel_mapping[ca][i] & 0x0f);
758                 else
759                         map[i] = 0;
760         }
761 }
762
763 static void hdmi_setup_channel_mapping(struct hda_codec *codec,
764                                        hda_nid_t pin_nid, bool non_pcm, int ca,
765                                        int channels, unsigned char *map,
766                                        bool chmap_set)
767 {
768         if (!non_pcm && chmap_set) {
769                 hdmi_manual_setup_channel_mapping(codec, pin_nid,
770                                                   channels, map);
771         } else {
772                 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
773                 hdmi_setup_fake_chmap(map, ca);
774         }
775 }
776
777 /*
778  * Audio InfoFrame routines
779  */
780
781 /*
782  * Enable Audio InfoFrame Transmission
783  */
784 static void hdmi_start_infoframe_trans(struct hda_codec *codec,
785                                        hda_nid_t pin_nid)
786 {
787         hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
788         snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
789                                                 AC_DIPXMIT_BEST);
790 }
791
792 /*
793  * Disable Audio InfoFrame Transmission
794  */
795 static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
796                                       hda_nid_t pin_nid)
797 {
798         hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
799         snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
800                                                 AC_DIPXMIT_DISABLE);
801 }
802
803 static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
804 {
805 #ifdef CONFIG_SND_DEBUG_VERBOSE
806         int i;
807         int size;
808
809         size = snd_hdmi_get_eld_size(codec, pin_nid);
810         printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
811
812         for (i = 0; i < 8; i++) {
813                 size = snd_hda_codec_read(codec, pin_nid, 0,
814                                                 AC_VERB_GET_HDMI_DIP_SIZE, i);
815                 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
816         }
817 #endif
818 }
819
820 static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
821 {
822 #ifdef BE_PARANOID
823         int i, j;
824         int size;
825         int pi, bi;
826         for (i = 0; i < 8; i++) {
827                 size = snd_hda_codec_read(codec, pin_nid, 0,
828                                                 AC_VERB_GET_HDMI_DIP_SIZE, i);
829                 if (size == 0)
830                         continue;
831
832                 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
833                 for (j = 1; j < 1000; j++) {
834                         hdmi_write_dip_byte(codec, pin_nid, 0x0);
835                         hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
836                         if (pi != i)
837                                 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
838                                                 bi, pi, i);
839                         if (bi == 0) /* byte index wrapped around */
840                                 break;
841                 }
842                 snd_printd(KERN_INFO
843                         "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
844                         i, size, j);
845         }
846 #endif
847 }
848
849 static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
850 {
851         u8 *bytes = (u8 *)hdmi_ai;
852         u8 sum = 0;
853         int i;
854
855         hdmi_ai->checksum = 0;
856
857         for (i = 0; i < sizeof(*hdmi_ai); i++)
858                 sum += bytes[i];
859
860         hdmi_ai->checksum = -sum;
861 }
862
863 static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
864                                       hda_nid_t pin_nid,
865                                       u8 *dip, int size)
866 {
867         int i;
868
869         hdmi_debug_dip_size(codec, pin_nid);
870         hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
871
872         hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
873         for (i = 0; i < size; i++)
874                 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
875 }
876
877 static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
878                                     u8 *dip, int size)
879 {
880         u8 val;
881         int i;
882
883         if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
884                                                             != AC_DIPXMIT_BEST)
885                 return false;
886
887         hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
888         for (i = 0; i < size; i++) {
889                 val = snd_hda_codec_read(codec, pin_nid, 0,
890                                          AC_VERB_GET_HDMI_DIP_DATA, 0);
891                 if (val != dip[i])
892                         return false;
893         }
894
895         return true;
896 }
897
898 static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
899                                        struct hdmi_spec_per_pin *per_pin,
900                                        bool non_pcm)
901 {
902         hda_nid_t pin_nid = per_pin->pin_nid;
903         int channels = per_pin->channels;
904         struct hdmi_eld *eld;
905         int ca;
906         union audio_infoframe ai;
907
908         if (!channels)
909                 return;
910
911         eld = &per_pin->sink_eld;
912         if (!eld->monitor_present)
913                 return;
914
915         if (!non_pcm && per_pin->chmap_set)
916                 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
917         else
918                 ca = hdmi_channel_allocation(eld, channels);
919         if (ca < 0)
920                 ca = 0;
921
922         memset(&ai, 0, sizeof(ai));
923         if (eld->info.conn_type == 0) { /* HDMI */
924                 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
925
926                 hdmi_ai->type           = 0x84;
927                 hdmi_ai->ver            = 0x01;
928                 hdmi_ai->len            = 0x0a;
929                 hdmi_ai->CC02_CT47      = channels - 1;
930                 hdmi_ai->CA             = ca;
931                 hdmi_checksum_audio_infoframe(hdmi_ai);
932         } else if (eld->info.conn_type == 1) { /* DisplayPort */
933                 struct dp_audio_infoframe *dp_ai = &ai.dp;
934
935                 dp_ai->type             = 0x84;
936                 dp_ai->len              = 0x1b;
937                 dp_ai->ver              = 0x11 << 2;
938                 dp_ai->CC02_CT47        = channels - 1;
939                 dp_ai->CA               = ca;
940         } else {
941                 snd_printd("HDMI: unknown connection type at pin %d\n",
942                             pin_nid);
943                 return;
944         }
945
946         /*
947          * always configure channel mapping, it may have been changed by the
948          * user in the meantime
949          */
950         hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
951                                    channels, per_pin->chmap,
952                                    per_pin->chmap_set);
953
954         /*
955          * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
956          * sizeof(*dp_ai) to avoid partial match/update problems when
957          * the user switches between HDMI/DP monitors.
958          */
959         if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
960                                         sizeof(ai))) {
961                 snd_printdd("hdmi_setup_audio_infoframe: "
962                             "pin=%d channels=%d\n",
963                             pin_nid,
964                             channels);
965                 hdmi_stop_infoframe_trans(codec, pin_nid);
966                 hdmi_fill_audio_infoframe(codec, pin_nid,
967                                             ai.bytes, sizeof(ai));
968                 hdmi_start_infoframe_trans(codec, pin_nid);
969         }
970
971         per_pin->non_pcm = non_pcm;
972 }
973
974
975 /*
976  * Unsolicited events
977  */
978
979 static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
980
981 static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
982 {
983         struct hdmi_spec *spec = codec->spec;
984         int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
985         int pin_nid;
986         int pin_idx;
987         struct hda_jack_tbl *jack;
988
989         jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
990         if (!jack)
991                 return;
992         pin_nid = jack->nid;
993         jack->jack_dirty = 1;
994
995         _snd_printd(SND_PR_VERBOSE,
996                 "HDMI hot plug event: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
997                 codec->addr, pin_nid,
998                 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
999
1000         pin_idx = pin_nid_to_pin_index(spec, pin_nid);
1001         if (pin_idx < 0)
1002                 return;
1003
1004         hdmi_present_sense(get_pin(spec, pin_idx), 1);
1005         snd_hda_jack_report_sync(codec);
1006 }
1007
1008 static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
1009 {
1010         int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1011         int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1012         int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1013         int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1014
1015         printk(KERN_INFO
1016                 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
1017                 codec->addr,
1018                 tag,
1019                 subtag,
1020                 cp_state,
1021                 cp_ready);
1022
1023         /* TODO */
1024         if (cp_state)
1025                 ;
1026         if (cp_ready)
1027                 ;
1028 }
1029
1030
1031 static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1032 {
1033         int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1034         int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1035
1036         if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
1037                 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
1038                 return;
1039         }
1040
1041         if (subtag == 0)
1042                 hdmi_intrinsic_event(codec, res);
1043         else
1044                 hdmi_non_intrinsic_event(codec, res);
1045 }
1046
1047 static void haswell_verify_pin_D0(struct hda_codec *codec, hda_nid_t nid)
1048 {
1049         int pwr, lamp, ramp;
1050
1051         pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1052         pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1053         if (pwr != AC_PWRST_D0) {
1054                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1055                                     AC_PWRST_D0);
1056                 msleep(40);
1057                 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1058                 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1059                 snd_printd("Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
1060         }
1061
1062         lamp = snd_hda_codec_read(codec, nid, 0,
1063                                   AC_VERB_GET_AMP_GAIN_MUTE,
1064                                   AC_AMP_GET_LEFT | AC_AMP_GET_OUTPUT);
1065         ramp = snd_hda_codec_read(codec, nid, 0,
1066                                   AC_VERB_GET_AMP_GAIN_MUTE,
1067                                   AC_AMP_GET_RIGHT | AC_AMP_GET_OUTPUT);
1068         if (lamp != ramp) {
1069                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1070                                     AC_AMP_SET_RIGHT | AC_AMP_SET_OUTPUT | lamp);
1071
1072                 lamp = snd_hda_codec_read(codec, nid, 0,
1073                                   AC_VERB_GET_AMP_GAIN_MUTE,
1074                                   AC_AMP_GET_LEFT | AC_AMP_GET_OUTPUT);
1075                 ramp = snd_hda_codec_read(codec, nid, 0,
1076                                   AC_VERB_GET_AMP_GAIN_MUTE,
1077                                   AC_AMP_GET_RIGHT | AC_AMP_GET_OUTPUT);
1078                 snd_printd("Haswell HDMI audio: Mute after set on pin 0x%x: [0x%x 0x%x]\n", nid, lamp, ramp);
1079         }
1080 }
1081
1082 /*
1083  * Callbacks
1084  */
1085
1086 /* HBR should be Non-PCM, 8 channels */
1087 #define is_hbr_format(format) \
1088         ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1089
1090 static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1091                               hda_nid_t pin_nid, u32 stream_tag, int format)
1092 {
1093         int pinctl;
1094         int new_pinctl = 0;
1095
1096         if (codec->vendor_id == 0x80862807)
1097                 haswell_verify_pin_D0(codec, pin_nid);
1098
1099         if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1100                 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1101                                             AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1102
1103                 new_pinctl = pinctl & ~AC_PINCTL_EPT;
1104                 if (is_hbr_format(format))
1105                         new_pinctl |= AC_PINCTL_EPT_HBR;
1106                 else
1107                         new_pinctl |= AC_PINCTL_EPT_NATIVE;
1108
1109                 snd_printdd("hdmi_setup_stream: "
1110                             "NID=0x%x, %spinctl=0x%x\n",
1111                             pin_nid,
1112                             pinctl == new_pinctl ? "" : "new-",
1113                             new_pinctl);
1114
1115                 if (pinctl != new_pinctl)
1116                         snd_hda_codec_write(codec, pin_nid, 0,
1117                                             AC_VERB_SET_PIN_WIDGET_CONTROL,
1118                                             new_pinctl);
1119
1120         }
1121         if (is_hbr_format(format) && !new_pinctl) {
1122                 snd_printdd("hdmi_setup_stream: HBR is not supported\n");
1123                 return -EINVAL;
1124         }
1125
1126         snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
1127         return 0;
1128 }
1129
1130 /*
1131  * HDA PCM callbacks
1132  */
1133 static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1134                          struct hda_codec *codec,
1135                          struct snd_pcm_substream *substream)
1136 {
1137         struct hdmi_spec *spec = codec->spec;
1138         struct snd_pcm_runtime *runtime = substream->runtime;
1139         int pin_idx, cvt_idx, mux_idx = 0;
1140         struct hdmi_spec_per_pin *per_pin;
1141         struct hdmi_eld *eld;
1142         struct hdmi_spec_per_cvt *per_cvt = NULL;
1143
1144         /* Validate hinfo */
1145         pin_idx = hinfo_to_pin_index(spec, hinfo);
1146         if (snd_BUG_ON(pin_idx < 0))
1147                 return -EINVAL;
1148         per_pin = get_pin(spec, pin_idx);
1149         eld = &per_pin->sink_eld;
1150
1151         /* Dynamically assign converter to stream */
1152         for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1153                 per_cvt = get_cvt(spec, cvt_idx);
1154
1155                 /* Must not already be assigned */
1156                 if (per_cvt->assigned)
1157                         continue;
1158                 /* Must be in pin's mux's list of converters */
1159                 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1160                         if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1161                                 break;
1162                 /* Not in mux list */
1163                 if (mux_idx == per_pin->num_mux_nids)
1164                         continue;
1165                 break;
1166         }
1167         /* No free converters */
1168         if (cvt_idx == spec->num_cvts)
1169                 return -ENODEV;
1170
1171         /* Claim converter */
1172         per_cvt->assigned = 1;
1173         hinfo->nid = per_cvt->cvt_nid;
1174
1175         snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1176                             AC_VERB_SET_CONNECT_SEL,
1177                             mux_idx);
1178         snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
1179
1180         /* Initially set the converter's capabilities */
1181         hinfo->channels_min = per_cvt->channels_min;
1182         hinfo->channels_max = per_cvt->channels_max;
1183         hinfo->rates = per_cvt->rates;
1184         hinfo->formats = per_cvt->formats;
1185         hinfo->maxbps = per_cvt->maxbps;
1186
1187         /* Restrict capabilities by ELD if this isn't disabled */
1188         if (!static_hdmi_pcm && eld->eld_valid) {
1189                 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
1190                 if (hinfo->channels_min > hinfo->channels_max ||
1191                     !hinfo->rates || !hinfo->formats) {
1192                         per_cvt->assigned = 0;
1193                         hinfo->nid = 0;
1194                         snd_hda_spdif_ctls_unassign(codec, pin_idx);
1195                         return -ENODEV;
1196                 }
1197         }
1198
1199         /* Store the updated parameters */
1200         runtime->hw.channels_min = hinfo->channels_min;
1201         runtime->hw.channels_max = hinfo->channels_max;
1202         runtime->hw.formats = hinfo->formats;
1203         runtime->hw.rates = hinfo->rates;
1204
1205         snd_pcm_hw_constraint_step(substream->runtime, 0,
1206                                    SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1207         return 0;
1208 }
1209
1210 /*
1211  * HDA/HDMI auto parsing
1212  */
1213 static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
1214 {
1215         struct hdmi_spec *spec = codec->spec;
1216         struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1217         hda_nid_t pin_nid = per_pin->pin_nid;
1218
1219         if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1220                 snd_printk(KERN_WARNING
1221                            "HDMI: pin %d wcaps %#x "
1222                            "does not support connection list\n",
1223                            pin_nid, get_wcaps(codec, pin_nid));
1224                 return -EINVAL;
1225         }
1226
1227         per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1228                                                         per_pin->mux_nids,
1229                                                         HDA_MAX_CONNECTIONS);
1230
1231         return 0;
1232 }
1233
1234 static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1235 {
1236         struct hda_codec *codec = per_pin->codec;
1237         struct hdmi_spec *spec = codec->spec;
1238         struct hdmi_eld *eld = &spec->temp_eld;
1239         struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1240         hda_nid_t pin_nid = per_pin->pin_nid;
1241         /*
1242          * Always execute a GetPinSense verb here, even when called from
1243          * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1244          * response's PD bit is not the real PD value, but indicates that
1245          * the real PD value changed. An older version of the HD-audio
1246          * specification worked this way. Hence, we just ignore the data in
1247          * the unsolicited response to avoid custom WARs.
1248          */
1249         int present = snd_hda_pin_sense(codec, pin_nid);
1250         bool update_eld = false;
1251         bool eld_changed = false;
1252
1253         pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1254         if (pin_eld->monitor_present)
1255                 eld->eld_valid  = !!(present & AC_PINSENSE_ELDV);
1256         else
1257                 eld->eld_valid = false;
1258
1259         _snd_printd(SND_PR_VERBOSE,
1260                 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
1261                 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
1262
1263         if (eld->eld_valid) {
1264                 if (snd_hdmi_get_eld(codec, pin_nid, eld->eld_buffer,
1265                                                      &eld->eld_size) < 0)
1266                         eld->eld_valid = false;
1267                 else {
1268                         memset(&eld->info, 0, sizeof(struct parsed_hdmi_eld));
1269                         if (snd_hdmi_parse_eld(&eld->info, eld->eld_buffer,
1270                                                     eld->eld_size) < 0)
1271                                 eld->eld_valid = false;
1272                 }
1273
1274                 if (eld->eld_valid) {
1275                         snd_hdmi_show_eld(&eld->info);
1276                         update_eld = true;
1277                 }
1278                 else if (repoll) {
1279                         queue_delayed_work(codec->bus->workq,
1280                                            &per_pin->work,
1281                                            msecs_to_jiffies(300));
1282                         return;
1283                 }
1284         }
1285
1286         mutex_lock(&pin_eld->lock);
1287         if (pin_eld->eld_valid && !eld->eld_valid) {
1288                 update_eld = true;
1289                 eld_changed = true;
1290         }
1291         if (update_eld) {
1292                 bool old_eld_valid = pin_eld->eld_valid;
1293                 pin_eld->eld_valid = eld->eld_valid;
1294                 eld_changed = pin_eld->eld_size != eld->eld_size ||
1295                               memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1296                                      eld->eld_size) != 0;
1297                 if (eld_changed)
1298                         memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1299                                eld->eld_size);
1300                 pin_eld->eld_size = eld->eld_size;
1301                 pin_eld->info = eld->info;
1302
1303                 /* Haswell-specific workaround: re-setup when the transcoder is
1304                  * changed during the stream playback
1305                  */
1306                 if (codec->vendor_id == 0x80862807 &&
1307                     eld->eld_valid && !old_eld_valid && per_pin->setup) {
1308                         snd_hda_codec_write(codec, pin_nid, 0,
1309                                             AC_VERB_SET_AMP_GAIN_MUTE,
1310                                             AMP_OUT_UNMUTE);
1311                         hdmi_setup_audio_infoframe(codec, per_pin,
1312                                                    per_pin->non_pcm);
1313                 }
1314         }
1315         mutex_unlock(&pin_eld->lock);
1316
1317         if (eld_changed)
1318                 snd_ctl_notify(codec->bus->card,
1319                                SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
1320                                &per_pin->eld_ctl->id);
1321 }
1322
1323 static void hdmi_repoll_eld(struct work_struct *work)
1324 {
1325         struct hdmi_spec_per_pin *per_pin =
1326         container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1327
1328         if (per_pin->repoll_count++ > 6)
1329                 per_pin->repoll_count = 0;
1330
1331         hdmi_present_sense(per_pin, per_pin->repoll_count);
1332 }
1333
1334 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1335                                              hda_nid_t nid);
1336
1337 static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1338 {
1339         struct hdmi_spec *spec = codec->spec;
1340         unsigned int caps, config;
1341         int pin_idx;
1342         struct hdmi_spec_per_pin *per_pin;
1343         int err;
1344
1345         caps = snd_hda_query_pin_caps(codec, pin_nid);
1346         if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1347                 return 0;
1348
1349         config = snd_hda_codec_get_pincfg(codec, pin_nid);
1350         if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1351                 return 0;
1352
1353         if (codec->vendor_id == 0x80862807)
1354                 intel_haswell_fixup_connect_list(codec, pin_nid);
1355
1356         pin_idx = spec->num_pins;
1357         per_pin = snd_array_new(&spec->pins);
1358         if (!per_pin)
1359                 return -ENOMEM;
1360
1361         per_pin->pin_nid = pin_nid;
1362         per_pin->non_pcm = false;
1363
1364         err = hdmi_read_pin_conn(codec, pin_idx);
1365         if (err < 0)
1366                 return err;
1367
1368         spec->num_pins++;
1369
1370         return 0;
1371 }
1372
1373 static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1374 {
1375         struct hdmi_spec *spec = codec->spec;
1376         struct hdmi_spec_per_cvt *per_cvt;
1377         unsigned int chans;
1378         int err;
1379
1380         chans = get_wcaps(codec, cvt_nid);
1381         chans = get_wcaps_channels(chans);
1382
1383         per_cvt = snd_array_new(&spec->cvts);
1384         if (!per_cvt)
1385                 return -ENOMEM;
1386
1387         per_cvt->cvt_nid = cvt_nid;
1388         per_cvt->channels_min = 2;
1389         if (chans <= 16) {
1390                 per_cvt->channels_max = chans;
1391                 if (chans > spec->channels_max)
1392                         spec->channels_max = chans;
1393         }
1394
1395         err = snd_hda_query_supported_pcm(codec, cvt_nid,
1396                                           &per_cvt->rates,
1397                                           &per_cvt->formats,
1398                                           &per_cvt->maxbps);
1399         if (err < 0)
1400                 return err;
1401
1402         if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1403                 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1404         spec->num_cvts++;
1405
1406         return 0;
1407 }
1408
1409 static int hdmi_parse_codec(struct hda_codec *codec)
1410 {
1411         hda_nid_t nid;
1412         int i, nodes;
1413
1414         nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
1415         if (!nid || nodes < 0) {
1416                 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
1417                 return -EINVAL;
1418         }
1419
1420         for (i = 0; i < nodes; i++, nid++) {
1421                 unsigned int caps;
1422                 unsigned int type;
1423
1424                 caps = get_wcaps(codec, nid);
1425                 type = get_wcaps_type(caps);
1426
1427                 if (!(caps & AC_WCAP_DIGITAL))
1428                         continue;
1429
1430                 switch (type) {
1431                 case AC_WID_AUD_OUT:
1432                         hdmi_add_cvt(codec, nid);
1433                         break;
1434                 case AC_WID_PIN:
1435                         hdmi_add_pin(codec, nid);
1436                         break;
1437                 }
1438         }
1439
1440 #ifdef CONFIG_PM
1441         /* We're seeing some problems with unsolicited hot plug events on
1442          * PantherPoint after S3, if this is not enabled */
1443         if (codec->vendor_id == 0x80862806)
1444                 codec->bus->power_keep_link_on = 1;
1445         /*
1446          * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
1447          * can be lost and presence sense verb will become inaccurate if the
1448          * HDA link is powered off at hot plug or hw initialization time.
1449          */
1450         else if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
1451               AC_PWRST_EPSS))
1452                 codec->bus->power_keep_link_on = 1;
1453 #endif
1454
1455         return 0;
1456 }
1457
1458 /*
1459  */
1460 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1461 {
1462         struct hda_spdif_out *spdif;
1463         bool non_pcm;
1464
1465         mutex_lock(&codec->spdif_mutex);
1466         spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1467         non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1468         mutex_unlock(&codec->spdif_mutex);
1469         return non_pcm;
1470 }
1471
1472
1473 /*
1474  * HDMI callbacks
1475  */
1476
1477 static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1478                                            struct hda_codec *codec,
1479                                            unsigned int stream_tag,
1480                                            unsigned int format,
1481                                            struct snd_pcm_substream *substream)
1482 {
1483         hda_nid_t cvt_nid = hinfo->nid;
1484         struct hdmi_spec *spec = codec->spec;
1485         int pin_idx = hinfo_to_pin_index(spec, hinfo);
1486         struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1487         hda_nid_t pin_nid = per_pin->pin_nid;
1488         bool non_pcm;
1489         int pinctl;
1490
1491         non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
1492         per_pin->channels = substream->runtime->channels;
1493         per_pin->setup = true;
1494
1495         hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
1496
1497         hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1498
1499         if (spec->dyn_pin_out) {
1500                 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1501                                             AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1502                 snd_hda_codec_write(codec, pin_nid, 0,
1503                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1504                                     pinctl | PIN_OUT);
1505         }
1506
1507         return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
1508 }
1509
1510 static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1511                                              struct hda_codec *codec,
1512                                              struct snd_pcm_substream *substream)
1513 {
1514         snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1515         return 0;
1516 }
1517
1518 static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1519                           struct hda_codec *codec,
1520                           struct snd_pcm_substream *substream)
1521 {
1522         struct hdmi_spec *spec = codec->spec;
1523         int cvt_idx, pin_idx;
1524         struct hdmi_spec_per_cvt *per_cvt;
1525         struct hdmi_spec_per_pin *per_pin;
1526         int pinctl;
1527
1528         if (hinfo->nid) {
1529                 cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
1530                 if (snd_BUG_ON(cvt_idx < 0))
1531                         return -EINVAL;
1532                 per_cvt = get_cvt(spec, cvt_idx);
1533
1534                 snd_BUG_ON(!per_cvt->assigned);
1535                 per_cvt->assigned = 0;
1536                 hinfo->nid = 0;
1537
1538                 pin_idx = hinfo_to_pin_index(spec, hinfo);
1539                 if (snd_BUG_ON(pin_idx < 0))
1540                         return -EINVAL;
1541                 per_pin = get_pin(spec, pin_idx);
1542
1543                 if (spec->dyn_pin_out) {
1544                         pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1545                                         AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1546                         snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1547                                             AC_VERB_SET_PIN_WIDGET_CONTROL,
1548                                             pinctl & ~PIN_OUT);
1549                 }
1550
1551                 snd_hda_spdif_ctls_unassign(codec, pin_idx);
1552                 per_pin->chmap_set = false;
1553                 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1554
1555                 per_pin->setup = false;
1556                 per_pin->channels = 0;
1557         }
1558
1559         return 0;
1560 }
1561
1562 static const struct hda_pcm_ops generic_ops = {
1563         .open = hdmi_pcm_open,
1564         .close = hdmi_pcm_close,
1565         .prepare = generic_hdmi_playback_pcm_prepare,
1566         .cleanup = generic_hdmi_playback_pcm_cleanup,
1567 };
1568
1569 /*
1570  * ALSA API channel-map control callbacks
1571  */
1572 static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
1573                                struct snd_ctl_elem_info *uinfo)
1574 {
1575         struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1576         struct hda_codec *codec = info->private_data;
1577         struct hdmi_spec *spec = codec->spec;
1578         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1579         uinfo->count = spec->channels_max;
1580         uinfo->value.integer.min = 0;
1581         uinfo->value.integer.max = SNDRV_CHMAP_LAST;
1582         return 0;
1583 }
1584
1585 static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1586                               unsigned int size, unsigned int __user *tlv)
1587 {
1588         struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1589         struct hda_codec *codec = info->private_data;
1590         struct hdmi_spec *spec = codec->spec;
1591         const unsigned int valid_mask =
1592                 FL | FR | RL | RR | LFE | FC | RLC | RRC;
1593         unsigned int __user *dst;
1594         int chs, count = 0;
1595
1596         if (size < 8)
1597                 return -ENOMEM;
1598         if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
1599                 return -EFAULT;
1600         size -= 8;
1601         dst = tlv + 2;
1602         for (chs = 2; chs <= spec->channels_max; chs++) {
1603                 int i, c;
1604                 struct cea_channel_speaker_allocation *cap;
1605                 cap = channel_allocations;
1606                 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
1607                         int chs_bytes = chs * 4;
1608                         if (cap->channels != chs)
1609                                 continue;
1610                         if (cap->spk_mask & ~valid_mask)
1611                                 continue;
1612                         if (size < 8)
1613                                 return -ENOMEM;
1614                         if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
1615                             put_user(chs_bytes, dst + 1))
1616                                 return -EFAULT;
1617                         dst += 2;
1618                         size -= 8;
1619                         count += 8;
1620                         if (size < chs_bytes)
1621                                 return -ENOMEM;
1622                         size -= chs_bytes;
1623                         count += chs_bytes;
1624                         for (c = 7; c >= 0; c--) {
1625                                 int spk = cap->speakers[c];
1626                                 if (!spk)
1627                                         continue;
1628                                 if (put_user(spk_to_chmap(spk), dst))
1629                                         return -EFAULT;
1630                                 dst++;
1631                         }
1632                 }
1633         }
1634         if (put_user(count, tlv + 1))
1635                 return -EFAULT;
1636         return 0;
1637 }
1638
1639 static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1640                               struct snd_ctl_elem_value *ucontrol)
1641 {
1642         struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1643         struct hda_codec *codec = info->private_data;
1644         struct hdmi_spec *spec = codec->spec;
1645         int pin_idx = kcontrol->private_value;
1646         struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1647         int i;
1648
1649         for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
1650                 ucontrol->value.integer.value[i] = per_pin->chmap[i];
1651         return 0;
1652 }
1653
1654 static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1655                               struct snd_ctl_elem_value *ucontrol)
1656 {
1657         struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1658         struct hda_codec *codec = info->private_data;
1659         struct hdmi_spec *spec = codec->spec;
1660         int pin_idx = kcontrol->private_value;
1661         struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1662         unsigned int ctl_idx;
1663         struct snd_pcm_substream *substream;
1664         unsigned char chmap[8];
1665         int i, ca, prepared = 0;
1666
1667         ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1668         substream = snd_pcm_chmap_substream(info, ctl_idx);
1669         if (!substream || !substream->runtime)
1670                 return 0; /* just for avoiding error from alsactl restore */
1671         switch (substream->runtime->status->state) {
1672         case SNDRV_PCM_STATE_OPEN:
1673         case SNDRV_PCM_STATE_SETUP:
1674                 break;
1675         case SNDRV_PCM_STATE_PREPARED:
1676                 prepared = 1;
1677                 break;
1678         default:
1679                 return -EBUSY;
1680         }
1681         memset(chmap, 0, sizeof(chmap));
1682         for (i = 0; i < ARRAY_SIZE(chmap); i++)
1683                 chmap[i] = ucontrol->value.integer.value[i];
1684         if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
1685                 return 0;
1686         ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
1687         if (ca < 0)
1688                 return -EINVAL;
1689         per_pin->chmap_set = true;
1690         memcpy(per_pin->chmap, chmap, sizeof(chmap));
1691         if (prepared)
1692                 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1693
1694         return 0;
1695 }
1696
1697 static int generic_hdmi_build_pcms(struct hda_codec *codec)
1698 {
1699         struct hdmi_spec *spec = codec->spec;
1700         int pin_idx;
1701
1702         for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1703                 struct hda_pcm *info;
1704                 struct hda_pcm_stream *pstr;
1705                 struct hdmi_spec_per_pin *per_pin;
1706
1707                 per_pin = get_pin(spec, pin_idx);
1708                 sprintf(per_pin->pcm_name, "HDMI %d", pin_idx);
1709                 info = snd_array_new(&spec->pcm_rec);
1710                 if (!info)
1711                         return -ENOMEM;
1712                 info->name = per_pin->pcm_name;
1713                 info->pcm_type = HDA_PCM_TYPE_HDMI;
1714                 info->own_chmap = true;
1715
1716                 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
1717                 pstr->substreams = 1;
1718                 pstr->ops = generic_ops;
1719                 /* other pstr fields are set in open */
1720         }
1721
1722         codec->num_pcms = spec->num_pins;
1723         codec->pcm_info = spec->pcm_rec.list;
1724
1725         return 0;
1726 }
1727
1728 static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
1729 {
1730         char hdmi_str[32] = "HDMI/DP";
1731         struct hdmi_spec *spec = codec->spec;
1732         struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1733         int pcmdev = get_pcm_rec(spec, pin_idx)->device;
1734
1735         if (pcmdev > 0)
1736                 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
1737         if (!is_jack_detectable(codec, per_pin->pin_nid))
1738                 strncat(hdmi_str, " Phantom",
1739                         sizeof(hdmi_str) - strlen(hdmi_str) - 1);
1740
1741         return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
1742 }
1743
1744 static int generic_hdmi_build_controls(struct hda_codec *codec)
1745 {
1746         struct hdmi_spec *spec = codec->spec;
1747         int err;
1748         int pin_idx;
1749
1750         for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1751                 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1752
1753                 err = generic_hdmi_build_jack(codec, pin_idx);
1754                 if (err < 0)
1755                         return err;
1756
1757                 err = snd_hda_create_dig_out_ctls(codec,
1758                                                   per_pin->pin_nid,
1759                                                   per_pin->mux_nids[0],
1760                                                   HDA_PCM_TYPE_HDMI);
1761                 if (err < 0)
1762                         return err;
1763                 snd_hda_spdif_ctls_unassign(codec, pin_idx);
1764
1765                 /* add control for ELD Bytes */
1766                 err = hdmi_create_eld_ctl(codec, pin_idx,
1767                                           get_pcm_rec(spec, pin_idx)->device);
1768
1769                 if (err < 0)
1770                         return err;
1771
1772                 hdmi_present_sense(per_pin, 0);
1773         }
1774
1775         /* add channel maps */
1776         for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1777                 struct snd_pcm_chmap *chmap;
1778                 struct snd_kcontrol *kctl;
1779                 int i;
1780
1781                 if (!codec->pcm_info[pin_idx].pcm)
1782                         break;
1783                 err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
1784                                              SNDRV_PCM_STREAM_PLAYBACK,
1785                                              NULL, 0, pin_idx, &chmap);
1786                 if (err < 0)
1787                         return err;
1788                 /* override handlers */
1789                 chmap->private_data = codec;
1790                 kctl = chmap->kctl;
1791                 for (i = 0; i < kctl->count; i++)
1792                         kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1793                 kctl->info = hdmi_chmap_ctl_info;
1794                 kctl->get = hdmi_chmap_ctl_get;
1795                 kctl->put = hdmi_chmap_ctl_put;
1796                 kctl->tlv.c = hdmi_chmap_ctl_tlv;
1797         }
1798
1799         return 0;
1800 }
1801
1802 static int generic_hdmi_init_per_pins(struct hda_codec *codec)
1803 {
1804         struct hdmi_spec *spec = codec->spec;
1805         int pin_idx;
1806
1807         for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1808                 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1809                 struct hdmi_eld *eld = &per_pin->sink_eld;
1810
1811                 per_pin->codec = codec;
1812                 mutex_init(&eld->lock);
1813                 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
1814                 snd_hda_eld_proc_new(codec, eld, pin_idx);
1815         }
1816         return 0;
1817 }
1818
1819 static int generic_hdmi_init(struct hda_codec *codec)
1820 {
1821         struct hdmi_spec *spec = codec->spec;
1822         int pin_idx;
1823
1824         for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1825                 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1826                 hda_nid_t pin_nid = per_pin->pin_nid;
1827
1828                 hdmi_init_pin(codec, pin_nid);
1829                 snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
1830         }
1831         return 0;
1832 }
1833
1834 static void hdmi_array_init(struct hdmi_spec *spec, int nums)
1835 {
1836         snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
1837         snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
1838         snd_array_init(&spec->pcm_rec, sizeof(struct hda_pcm), nums);
1839 }
1840
1841 static void hdmi_array_free(struct hdmi_spec *spec)
1842 {
1843         snd_array_free(&spec->pins);
1844         snd_array_free(&spec->cvts);
1845         snd_array_free(&spec->pcm_rec);
1846 }
1847
1848 static void generic_hdmi_free(struct hda_codec *codec)
1849 {
1850         struct hdmi_spec *spec = codec->spec;
1851         int pin_idx;
1852
1853         for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1854                 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1855                 struct hdmi_eld *eld = &per_pin->sink_eld;
1856
1857                 cancel_delayed_work(&per_pin->work);
1858                 snd_hda_eld_proc_free(codec, eld);
1859         }
1860
1861         flush_workqueue(codec->bus->workq);
1862         hdmi_array_free(spec);
1863         kfree(spec);
1864 }
1865
1866 static const struct hda_codec_ops generic_hdmi_patch_ops = {
1867         .init                   = generic_hdmi_init,
1868         .free                   = generic_hdmi_free,
1869         .build_pcms             = generic_hdmi_build_pcms,
1870         .build_controls         = generic_hdmi_build_controls,
1871         .unsol_event            = hdmi_unsol_event,
1872 };
1873
1874
1875 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1876                                              hda_nid_t nid)
1877 {
1878         struct hdmi_spec *spec = codec->spec;
1879         hda_nid_t conns[4];
1880         int nconns;
1881
1882         nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
1883         if (nconns == spec->num_cvts &&
1884             !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
1885                 return;
1886
1887         /* override pins connection list */
1888         snd_printdd("hdmi: haswell: override pin connection 0x%x\n", nid);
1889         nconns = max(spec->num_cvts, 4);
1890         snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
1891 }
1892
1893 #define INTEL_VENDOR_NID 0x08
1894 #define INTEL_GET_VENDOR_VERB 0xf81
1895 #define INTEL_SET_VENDOR_VERB 0x781
1896 #define INTEL_EN_DP12                   0x02 /* enable DP 1.2 features */
1897 #define INTEL_EN_ALL_PIN_CVTS   0x01 /* enable 2nd & 3rd pins and convertors */
1898
1899 static void intel_haswell_enable_all_pins(struct hda_codec *codec,
1900                                           bool update_tree)
1901 {
1902         unsigned int vendor_param;
1903
1904         vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1905                                 INTEL_GET_VENDOR_VERB, 0);
1906         if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1907                 return;
1908
1909         vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1910         vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1911                                 INTEL_SET_VENDOR_VERB, vendor_param);
1912         if (vendor_param == -1)
1913                 return;
1914
1915         if (update_tree)
1916                 snd_hda_codec_update_widgets(codec);
1917 }
1918
1919 static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
1920 {
1921         unsigned int vendor_param;
1922
1923         vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1924                                 INTEL_GET_VENDOR_VERB, 0);
1925         if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1926                 return;
1927
1928         /* enable DP1.2 mode */
1929         vendor_param |= INTEL_EN_DP12;
1930         snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
1931                                 INTEL_SET_VENDOR_VERB, vendor_param);
1932 }
1933
1934 /* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
1935  * Otherwise you may get severe h/w communication errors.
1936  */
1937 static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1938                                 unsigned int power_state)
1939 {
1940         if (power_state == AC_PWRST_D0) {
1941                 intel_haswell_enable_all_pins(codec, false);
1942                 intel_haswell_fixup_enable_dp12(codec);
1943         }
1944
1945         snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
1946         snd_hda_codec_set_power_to_all(codec, fg, power_state);
1947 }
1948
1949 static int patch_generic_hdmi(struct hda_codec *codec)
1950 {
1951         struct hdmi_spec *spec;
1952
1953         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1954         if (spec == NULL)
1955                 return -ENOMEM;
1956
1957         codec->spec = spec;
1958         hdmi_array_init(spec, 4);
1959
1960         if (codec->vendor_id == 0x80862807) {
1961                 intel_haswell_enable_all_pins(codec, true);
1962                 intel_haswell_fixup_enable_dp12(codec);
1963         }
1964
1965         if (hdmi_parse_codec(codec) < 0) {
1966                 codec->spec = NULL;
1967                 kfree(spec);
1968                 return -EINVAL;
1969         }
1970         codec->patch_ops = generic_hdmi_patch_ops;
1971         if (codec->vendor_id == 0x80862807)
1972                 codec->patch_ops.set_power_state = haswell_set_power_state;
1973
1974         generic_hdmi_init_per_pins(codec);
1975
1976         init_channel_allocations();
1977
1978         return 0;
1979 }
1980
1981 /*
1982  * Shared non-generic implementations
1983  */
1984
1985 static int simple_playback_build_pcms(struct hda_codec *codec)
1986 {
1987         struct hdmi_spec *spec = codec->spec;
1988         struct hda_pcm *info;
1989         unsigned int chans;
1990         struct hda_pcm_stream *pstr;
1991         struct hdmi_spec_per_cvt *per_cvt;
1992
1993         per_cvt = get_cvt(spec, 0);
1994         chans = get_wcaps(codec, per_cvt->cvt_nid);
1995         chans = get_wcaps_channels(chans);
1996
1997         info = snd_array_new(&spec->pcm_rec);
1998         if (!info)
1999                 return -ENOMEM;
2000         info->name = get_pin(spec, 0)->pcm_name;
2001         sprintf(info->name, "HDMI 0");
2002         info->pcm_type = HDA_PCM_TYPE_HDMI;
2003         pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2004         *pstr = spec->pcm_playback;
2005         pstr->nid = per_cvt->cvt_nid;
2006         if (pstr->channels_max <= 2 && chans && chans <= 16)
2007                 pstr->channels_max = chans;
2008
2009         codec->num_pcms = 1;
2010         codec->pcm_info = info;
2011
2012         return 0;
2013 }
2014
2015 /* unsolicited event for jack sensing */
2016 static void simple_hdmi_unsol_event(struct hda_codec *codec,
2017                                     unsigned int res)
2018 {
2019         snd_hda_jack_set_dirty_all(codec);
2020         snd_hda_jack_report_sync(codec);
2021 }
2022
2023 /* generic_hdmi_build_jack can be used for simple_hdmi, too,
2024  * as long as spec->pins[] is set correctly
2025  */
2026 #define simple_hdmi_build_jack  generic_hdmi_build_jack
2027
2028 static int simple_playback_build_controls(struct hda_codec *codec)
2029 {
2030         struct hdmi_spec *spec = codec->spec;
2031         struct hdmi_spec_per_cvt *per_cvt;
2032         int err;
2033
2034         per_cvt = get_cvt(spec, 0);
2035         err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2036                                           per_cvt->cvt_nid,
2037                                           HDA_PCM_TYPE_HDMI);
2038         if (err < 0)
2039                 return err;
2040         return simple_hdmi_build_jack(codec, 0);
2041 }
2042
2043 static int simple_playback_init(struct hda_codec *codec)
2044 {
2045         struct hdmi_spec *spec = codec->spec;
2046         struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2047         hda_nid_t pin = per_pin->pin_nid;
2048
2049         snd_hda_codec_write(codec, pin, 0,
2050                             AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2051         /* some codecs require to unmute the pin */
2052         if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2053                 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2054                                     AMP_OUT_UNMUTE);
2055         snd_hda_jack_detect_enable(codec, pin, pin);
2056         return 0;
2057 }
2058
2059 static void simple_playback_free(struct hda_codec *codec)
2060 {
2061         struct hdmi_spec *spec = codec->spec;
2062
2063         hdmi_array_free(spec);
2064         kfree(spec);
2065 }
2066
2067 /*
2068  * Nvidia specific implementations
2069  */
2070
2071 #define Nv_VERB_SET_Channel_Allocation          0xF79
2072 #define Nv_VERB_SET_Info_Frame_Checksum         0xF7A
2073 #define Nv_VERB_SET_Audio_Protection_On         0xF98
2074 #define Nv_VERB_SET_Audio_Protection_Off        0xF99
2075
2076 #define nvhdmi_master_con_nid_7x        0x04
2077 #define nvhdmi_master_pin_nid_7x        0x05
2078
2079 static const hda_nid_t nvhdmi_con_nids_7x[4] = {
2080         /*front, rear, clfe, rear_surr */
2081         0x6, 0x8, 0xa, 0xc,
2082 };
2083
2084 static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2085         /* set audio protect on */
2086         { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2087         /* enable digital output on pin widget */
2088         { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2089         {} /* terminator */
2090 };
2091
2092 static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
2093         /* set audio protect on */
2094         { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2095         /* enable digital output on pin widget */
2096         { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2097         { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2098         { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2099         { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2100         { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2101         {} /* terminator */
2102 };
2103
2104 #ifdef LIMITED_RATE_FMT_SUPPORT
2105 /* support only the safe format and rate */
2106 #define SUPPORTED_RATES         SNDRV_PCM_RATE_48000
2107 #define SUPPORTED_MAXBPS        16
2108 #define SUPPORTED_FORMATS       SNDRV_PCM_FMTBIT_S16_LE
2109 #else
2110 /* support all rates and formats */
2111 #define SUPPORTED_RATES \
2112         (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2113         SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2114          SNDRV_PCM_RATE_192000)
2115 #define SUPPORTED_MAXBPS        24
2116 #define SUPPORTED_FORMATS \
2117         (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2118 #endif
2119
2120 static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
2121 {
2122         snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2123         return 0;
2124 }
2125
2126 static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2127 {
2128         snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
2129         return 0;
2130 }
2131
2132 static unsigned int channels_2_6_8[] = {
2133         2, 6, 8
2134 };
2135
2136 static unsigned int channels_2_8[] = {
2137         2, 8
2138 };
2139
2140 static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2141         .count = ARRAY_SIZE(channels_2_6_8),
2142         .list = channels_2_6_8,
2143         .mask = 0,
2144 };
2145
2146 static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2147         .count = ARRAY_SIZE(channels_2_8),
2148         .list = channels_2_8,
2149         .mask = 0,
2150 };
2151
2152 static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2153                                     struct hda_codec *codec,
2154                                     struct snd_pcm_substream *substream)
2155 {
2156         struct hdmi_spec *spec = codec->spec;
2157         struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2158
2159         switch (codec->preset->id) {
2160         case 0x10de0002:
2161         case 0x10de0003:
2162         case 0x10de0005:
2163         case 0x10de0006:
2164                 hw_constraints_channels = &hw_constraints_2_8_channels;
2165                 break;
2166         case 0x10de0007:
2167                 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2168                 break;
2169         default:
2170                 break;
2171         }
2172
2173         if (hw_constraints_channels != NULL) {
2174                 snd_pcm_hw_constraint_list(substream->runtime, 0,
2175                                 SNDRV_PCM_HW_PARAM_CHANNELS,
2176                                 hw_constraints_channels);
2177         } else {
2178                 snd_pcm_hw_constraint_step(substream->runtime, 0,
2179                                            SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2180         }
2181
2182         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2183 }
2184
2185 static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2186                                      struct hda_codec *codec,
2187                                      struct snd_pcm_substream *substream)
2188 {
2189         struct hdmi_spec *spec = codec->spec;
2190         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2191 }
2192
2193 static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2194                                        struct hda_codec *codec,
2195                                        unsigned int stream_tag,
2196                                        unsigned int format,
2197                                        struct snd_pcm_substream *substream)
2198 {
2199         struct hdmi_spec *spec = codec->spec;
2200         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2201                                              stream_tag, format, substream);
2202 }
2203
2204 static const struct hda_pcm_stream simple_pcm_playback = {
2205         .substreams = 1,
2206         .channels_min = 2,
2207         .channels_max = 2,
2208         .ops = {
2209                 .open = simple_playback_pcm_open,
2210                 .close = simple_playback_pcm_close,
2211                 .prepare = simple_playback_pcm_prepare
2212         },
2213 };
2214
2215 static const struct hda_codec_ops simple_hdmi_patch_ops = {
2216         .build_controls = simple_playback_build_controls,
2217         .build_pcms = simple_playback_build_pcms,
2218         .init = simple_playback_init,
2219         .free = simple_playback_free,
2220         .unsol_event = simple_hdmi_unsol_event,
2221 };
2222
2223 static int patch_simple_hdmi(struct hda_codec *codec,
2224                              hda_nid_t cvt_nid, hda_nid_t pin_nid)
2225 {
2226         struct hdmi_spec *spec;
2227         struct hdmi_spec_per_cvt *per_cvt;
2228         struct hdmi_spec_per_pin *per_pin;
2229
2230         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2231         if (!spec)
2232                 return -ENOMEM;
2233
2234         codec->spec = spec;
2235         hdmi_array_init(spec, 1);
2236
2237         spec->multiout.num_dacs = 0;  /* no analog */
2238         spec->multiout.max_channels = 2;
2239         spec->multiout.dig_out_nid = cvt_nid;
2240         spec->num_cvts = 1;
2241         spec->num_pins = 1;
2242         per_pin = snd_array_new(&spec->pins);
2243         per_cvt = snd_array_new(&spec->cvts);
2244         if (!per_pin || !per_cvt) {
2245                 simple_playback_free(codec);
2246                 return -ENOMEM;
2247         }
2248         per_cvt->cvt_nid = cvt_nid;
2249         per_pin->pin_nid = pin_nid;
2250         spec->pcm_playback = simple_pcm_playback;
2251
2252         codec->patch_ops = simple_hdmi_patch_ops;
2253
2254         return 0;
2255 }
2256
2257 static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2258                                                     int channels)
2259 {
2260         unsigned int chanmask;
2261         int chan = channels ? (channels - 1) : 1;
2262
2263         switch (channels) {
2264         default:
2265         case 0:
2266         case 2:
2267                 chanmask = 0x00;
2268                 break;
2269         case 4:
2270                 chanmask = 0x08;
2271                 break;
2272         case 6:
2273                 chanmask = 0x0b;
2274                 break;
2275         case 8:
2276                 chanmask = 0x13;
2277                 break;
2278         }
2279
2280         /* Set the audio infoframe channel allocation and checksum fields.  The
2281          * channel count is computed implicitly by the hardware. */
2282         snd_hda_codec_write(codec, 0x1, 0,
2283                         Nv_VERB_SET_Channel_Allocation, chanmask);
2284
2285         snd_hda_codec_write(codec, 0x1, 0,
2286                         Nv_VERB_SET_Info_Frame_Checksum,
2287                         (0x71 - chan - chanmask));
2288 }
2289
2290 static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2291                                    struct hda_codec *codec,
2292                                    struct snd_pcm_substream *substream)
2293 {
2294         struct hdmi_spec *spec = codec->spec;
2295         int i;
2296
2297         snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2298                         0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2299         for (i = 0; i < 4; i++) {
2300                 /* set the stream id */
2301                 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2302                                 AC_VERB_SET_CHANNEL_STREAMID, 0);
2303                 /* set the stream format */
2304                 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2305                                 AC_VERB_SET_STREAM_FORMAT, 0);
2306         }
2307
2308         /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2309          * streams are disabled. */
2310         nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2311
2312         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2313 }
2314
2315 static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2316                                      struct hda_codec *codec,
2317                                      unsigned int stream_tag,
2318                                      unsigned int format,
2319                                      struct snd_pcm_substream *substream)
2320 {
2321         int chs;
2322         unsigned int dataDCC2, channel_id;
2323         int i;
2324         struct hdmi_spec *spec = codec->spec;
2325         struct hda_spdif_out *spdif;
2326         struct hdmi_spec_per_cvt *per_cvt;
2327
2328         mutex_lock(&codec->spdif_mutex);
2329         per_cvt = get_cvt(spec, 0);
2330         spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
2331
2332         chs = substream->runtime->channels;
2333
2334         dataDCC2 = 0x2;
2335
2336         /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
2337         if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
2338                 snd_hda_codec_write(codec,
2339                                 nvhdmi_master_con_nid_7x,
2340                                 0,
2341                                 AC_VERB_SET_DIGI_CONVERT_1,
2342                                 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
2343
2344         /* set the stream id */
2345         snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2346                         AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2347
2348         /* set the stream format */
2349         snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2350                         AC_VERB_SET_STREAM_FORMAT, format);
2351
2352         /* turn on again (if needed) */
2353         /* enable and set the channel status audio/data flag */
2354         if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
2355                 snd_hda_codec_write(codec,
2356                                 nvhdmi_master_con_nid_7x,
2357                                 0,
2358                                 AC_VERB_SET_DIGI_CONVERT_1,
2359                                 spdif->ctls & 0xff);
2360                 snd_hda_codec_write(codec,
2361                                 nvhdmi_master_con_nid_7x,
2362                                 0,
2363                                 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2364         }
2365
2366         for (i = 0; i < 4; i++) {
2367                 if (chs == 2)
2368                         channel_id = 0;
2369                 else
2370                         channel_id = i * 2;
2371
2372                 /* turn off SPDIF once;
2373                  *otherwise the IEC958 bits won't be updated
2374                  */
2375                 if (codec->spdif_status_reset &&
2376                 (spdif->ctls & AC_DIG1_ENABLE))
2377                         snd_hda_codec_write(codec,
2378                                 nvhdmi_con_nids_7x[i],
2379                                 0,
2380                                 AC_VERB_SET_DIGI_CONVERT_1,
2381                                 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
2382                 /* set the stream id */
2383                 snd_hda_codec_write(codec,
2384                                 nvhdmi_con_nids_7x[i],
2385                                 0,
2386                                 AC_VERB_SET_CHANNEL_STREAMID,
2387                                 (stream_tag << 4) | channel_id);
2388                 /* set the stream format */
2389                 snd_hda_codec_write(codec,
2390                                 nvhdmi_con_nids_7x[i],
2391                                 0,
2392                                 AC_VERB_SET_STREAM_FORMAT,
2393                                 format);
2394                 /* turn on again (if needed) */
2395                 /* enable and set the channel status audio/data flag */
2396                 if (codec->spdif_status_reset &&
2397                 (spdif->ctls & AC_DIG1_ENABLE)) {
2398                         snd_hda_codec_write(codec,
2399                                         nvhdmi_con_nids_7x[i],
2400                                         0,
2401                                         AC_VERB_SET_DIGI_CONVERT_1,
2402                                         spdif->ctls & 0xff);
2403                         snd_hda_codec_write(codec,
2404                                         nvhdmi_con_nids_7x[i],
2405                                         0,
2406                                         AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2407                 }
2408         }
2409
2410         nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
2411
2412         mutex_unlock(&codec->spdif_mutex);
2413         return 0;
2414 }
2415
2416 static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
2417         .substreams = 1,
2418         .channels_min = 2,
2419         .channels_max = 8,
2420         .nid = nvhdmi_master_con_nid_7x,
2421         .rates = SUPPORTED_RATES,
2422         .maxbps = SUPPORTED_MAXBPS,
2423         .formats = SUPPORTED_FORMATS,
2424         .ops = {
2425                 .open = simple_playback_pcm_open,
2426                 .close = nvhdmi_8ch_7x_pcm_close,
2427                 .prepare = nvhdmi_8ch_7x_pcm_prepare
2428         },
2429 };
2430
2431 static int patch_nvhdmi_2ch(struct hda_codec *codec)
2432 {
2433         struct hdmi_spec *spec;
2434         int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2435                                     nvhdmi_master_pin_nid_7x);
2436         if (err < 0)
2437                 return err;
2438
2439         codec->patch_ops.init = nvhdmi_7x_init_2ch;
2440         /* override the PCM rates, etc, as the codec doesn't give full list */
2441         spec = codec->spec;
2442         spec->pcm_playback.rates = SUPPORTED_RATES;
2443         spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2444         spec->pcm_playback.formats = SUPPORTED_FORMATS;
2445         return 0;
2446 }
2447
2448 static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2449 {
2450         struct hdmi_spec *spec = codec->spec;
2451         int err = simple_playback_build_pcms(codec);
2452         if (!err) {
2453                 struct hda_pcm *info = get_pcm_rec(spec, 0);
2454                 info->own_chmap = true;
2455         }
2456         return err;
2457 }
2458
2459 static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2460 {
2461         struct hdmi_spec *spec = codec->spec;
2462         struct hda_pcm *info;
2463         struct snd_pcm_chmap *chmap;
2464         int err;
2465
2466         err = simple_playback_build_controls(codec);
2467         if (err < 0)
2468                 return err;
2469
2470         /* add channel maps */
2471         info = get_pcm_rec(spec, 0);
2472         err = snd_pcm_add_chmap_ctls(info->pcm,
2473                                      SNDRV_PCM_STREAM_PLAYBACK,
2474                                      snd_pcm_alt_chmaps, 8, 0, &chmap);
2475         if (err < 0)
2476                 return err;
2477         switch (codec->preset->id) {
2478         case 0x10de0002:
2479         case 0x10de0003:
2480         case 0x10de0005:
2481         case 0x10de0006:
2482                 chmap->channel_mask = (1U << 2) | (1U << 8);
2483                 break;
2484         case 0x10de0007:
2485                 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2486         }
2487         return 0;
2488 }
2489
2490 static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2491 {
2492         struct hdmi_spec *spec;
2493         int err = patch_nvhdmi_2ch(codec);
2494         if (err < 0)
2495                 return err;
2496         spec = codec->spec;
2497         spec->multiout.max_channels = 8;
2498         spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
2499         codec->patch_ops.init = nvhdmi_7x_init_8ch;
2500         codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2501         codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
2502
2503         /* Initialize the audio infoframe channel mask and checksum to something
2504          * valid */
2505         nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2506
2507         return 0;
2508 }
2509
2510 static int patch_nvhdmi(struct hda_codec *codec)
2511 {
2512         struct hdmi_spec *spec;
2513         int err;
2514
2515         err = patch_generic_hdmi(codec);
2516         if (err)
2517                 return err;
2518
2519         spec = codec->spec;
2520         spec->dyn_pin_out = true;
2521
2522         return 0;
2523 }
2524
2525 /*
2526  * ATI-specific implementations
2527  *
2528  * FIXME: we may omit the whole this and use the generic code once after
2529  * it's confirmed to work.
2530  */
2531
2532 #define ATIHDMI_CVT_NID         0x02    /* audio converter */
2533 #define ATIHDMI_PIN_NID         0x03    /* HDMI output pin */
2534
2535 static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2536                                         struct hda_codec *codec,
2537                                         unsigned int stream_tag,
2538                                         unsigned int format,
2539                                         struct snd_pcm_substream *substream)
2540 {
2541         struct hdmi_spec *spec = codec->spec;
2542         struct hdmi_spec_per_cvt *per_cvt = get_cvt(spec, 0);
2543         int chans = substream->runtime->channels;
2544         int i, err;
2545
2546         err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
2547                                           substream);
2548         if (err < 0)
2549                 return err;
2550         snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
2551                             AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
2552         /* FIXME: XXX */
2553         for (i = 0; i < chans; i++) {
2554                 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
2555                                     AC_VERB_SET_HDMI_CHAN_SLOT,
2556                                     (i << 4) | i);
2557         }
2558         return 0;
2559 }
2560
2561 static int patch_atihdmi(struct hda_codec *codec)
2562 {
2563         struct hdmi_spec *spec;
2564         int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
2565         if (err < 0)
2566                 return err;
2567         spec = codec->spec;
2568         spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
2569         return 0;
2570 }
2571
2572 /* VIA HDMI Implementation */
2573 #define VIAHDMI_CVT_NID 0x02    /* audio converter1 */
2574 #define VIAHDMI_PIN_NID 0x03    /* HDMI output pin1 */
2575
2576 static int patch_via_hdmi(struct hda_codec *codec)
2577 {
2578         return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
2579 }
2580
2581 /*
2582  * patch entries
2583  */
2584 static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
2585 { .id = 0x1002793c, .name = "RS600 HDMI",       .patch = patch_atihdmi },
2586 { .id = 0x10027919, .name = "RS600 HDMI",       .patch = patch_atihdmi },
2587 { .id = 0x1002791a, .name = "RS690/780 HDMI",   .patch = patch_atihdmi },
2588 { .id = 0x1002aa01, .name = "R6xx HDMI",        .patch = patch_generic_hdmi },
2589 { .id = 0x10951390, .name = "SiI1390 HDMI",     .patch = patch_generic_hdmi },
2590 { .id = 0x10951392, .name = "SiI1392 HDMI",     .patch = patch_generic_hdmi },
2591 { .id = 0x17e80047, .name = "Chrontel HDMI",    .patch = patch_generic_hdmi },
2592 { .id = 0x10de0002, .name = "MCP77/78 HDMI",    .patch = patch_nvhdmi_8ch_7x },
2593 { .id = 0x10de0003, .name = "MCP77/78 HDMI",    .patch = patch_nvhdmi_8ch_7x },
2594 { .id = 0x10de0005, .name = "MCP77/78 HDMI",    .patch = patch_nvhdmi_8ch_7x },
2595 { .id = 0x10de0006, .name = "MCP77/78 HDMI",    .patch = patch_nvhdmi_8ch_7x },
2596 { .id = 0x10de0007, .name = "MCP79/7A HDMI",    .patch = patch_nvhdmi_8ch_7x },
2597 { .id = 0x10de000a, .name = "GPU 0a HDMI/DP",   .patch = patch_nvhdmi },
2598 { .id = 0x10de000b, .name = "GPU 0b HDMI/DP",   .patch = patch_nvhdmi },
2599 { .id = 0x10de000c, .name = "MCP89 HDMI",       .patch = patch_nvhdmi },
2600 { .id = 0x10de000d, .name = "GPU 0d HDMI/DP",   .patch = patch_nvhdmi },
2601 { .id = 0x10de0010, .name = "GPU 10 HDMI/DP",   .patch = patch_nvhdmi },
2602 { .id = 0x10de0011, .name = "GPU 11 HDMI/DP",   .patch = patch_nvhdmi },
2603 { .id = 0x10de0012, .name = "GPU 12 HDMI/DP",   .patch = patch_nvhdmi },
2604 { .id = 0x10de0013, .name = "GPU 13 HDMI/DP",   .patch = patch_nvhdmi },
2605 { .id = 0x10de0014, .name = "GPU 14 HDMI/DP",   .patch = patch_nvhdmi },
2606 { .id = 0x10de0015, .name = "GPU 15 HDMI/DP",   .patch = patch_nvhdmi },
2607 { .id = 0x10de0016, .name = "GPU 16 HDMI/DP",   .patch = patch_nvhdmi },
2608 /* 17 is known to be absent */
2609 { .id = 0x10de0018, .name = "GPU 18 HDMI/DP",   .patch = patch_nvhdmi },
2610 { .id = 0x10de0019, .name = "GPU 19 HDMI/DP",   .patch = patch_nvhdmi },
2611 { .id = 0x10de001a, .name = "GPU 1a HDMI/DP",   .patch = patch_nvhdmi },
2612 { .id = 0x10de001b, .name = "GPU 1b HDMI/DP",   .patch = patch_nvhdmi },
2613 { .id = 0x10de001c, .name = "GPU 1c HDMI/DP",   .patch = patch_nvhdmi },
2614 { .id = 0x10de0040, .name = "GPU 40 HDMI/DP",   .patch = patch_nvhdmi },
2615 { .id = 0x10de0041, .name = "GPU 41 HDMI/DP",   .patch = patch_nvhdmi },
2616 { .id = 0x10de0042, .name = "GPU 42 HDMI/DP",   .patch = patch_nvhdmi },
2617 { .id = 0x10de0043, .name = "GPU 43 HDMI/DP",   .patch = patch_nvhdmi },
2618 { .id = 0x10de0044, .name = "GPU 44 HDMI/DP",   .patch = patch_nvhdmi },
2619 { .id = 0x10de0051, .name = "GPU 51 HDMI/DP",   .patch = patch_nvhdmi },
2620 { .id = 0x10de0060, .name = "GPU 60 HDMI/DP",   .patch = patch_nvhdmi },
2621 { .id = 0x10de0067, .name = "MCP67 HDMI",       .patch = patch_nvhdmi_2ch },
2622 { .id = 0x10de8001, .name = "MCP73 HDMI",       .patch = patch_nvhdmi_2ch },
2623 { .id = 0x11069f80, .name = "VX900 HDMI/DP",    .patch = patch_via_hdmi },
2624 { .id = 0x11069f81, .name = "VX900 HDMI/DP",    .patch = patch_via_hdmi },
2625 { .id = 0x11069f84, .name = "VX11 HDMI/DP",     .patch = patch_generic_hdmi },
2626 { .id = 0x11069f85, .name = "VX11 HDMI/DP",     .patch = patch_generic_hdmi },
2627 { .id = 0x80860054, .name = "IbexPeak HDMI",    .patch = patch_generic_hdmi },
2628 { .id = 0x80862801, .name = "Bearlake HDMI",    .patch = patch_generic_hdmi },
2629 { .id = 0x80862802, .name = "Cantiga HDMI",     .patch = patch_generic_hdmi },
2630 { .id = 0x80862803, .name = "Eaglelake HDMI",   .patch = patch_generic_hdmi },
2631 { .id = 0x80862804, .name = "IbexPeak HDMI",    .patch = patch_generic_hdmi },
2632 { .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
2633 { .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
2634 { .id = 0x80862807, .name = "Haswell HDMI",     .patch = patch_generic_hdmi },
2635 { .id = 0x80862880, .name = "CedarTrail HDMI",  .patch = patch_generic_hdmi },
2636 { .id = 0x808629fb, .name = "Crestline HDMI",   .patch = patch_generic_hdmi },
2637 {} /* terminator */
2638 };
2639
2640 MODULE_ALIAS("snd-hda-codec-id:1002793c");
2641 MODULE_ALIAS("snd-hda-codec-id:10027919");
2642 MODULE_ALIAS("snd-hda-codec-id:1002791a");
2643 MODULE_ALIAS("snd-hda-codec-id:1002aa01");
2644 MODULE_ALIAS("snd-hda-codec-id:10951390");
2645 MODULE_ALIAS("snd-hda-codec-id:10951392");
2646 MODULE_ALIAS("snd-hda-codec-id:10de0002");
2647 MODULE_ALIAS("snd-hda-codec-id:10de0003");
2648 MODULE_ALIAS("snd-hda-codec-id:10de0005");
2649 MODULE_ALIAS("snd-hda-codec-id:10de0006");
2650 MODULE_ALIAS("snd-hda-codec-id:10de0007");
2651 MODULE_ALIAS("snd-hda-codec-id:10de000a");
2652 MODULE_ALIAS("snd-hda-codec-id:10de000b");
2653 MODULE_ALIAS("snd-hda-codec-id:10de000c");
2654 MODULE_ALIAS("snd-hda-codec-id:10de000d");
2655 MODULE_ALIAS("snd-hda-codec-id:10de0010");
2656 MODULE_ALIAS("snd-hda-codec-id:10de0011");
2657 MODULE_ALIAS("snd-hda-codec-id:10de0012");
2658 MODULE_ALIAS("snd-hda-codec-id:10de0013");
2659 MODULE_ALIAS("snd-hda-codec-id:10de0014");
2660 MODULE_ALIAS("snd-hda-codec-id:10de0015");
2661 MODULE_ALIAS("snd-hda-codec-id:10de0016");
2662 MODULE_ALIAS("snd-hda-codec-id:10de0018");
2663 MODULE_ALIAS("snd-hda-codec-id:10de0019");
2664 MODULE_ALIAS("snd-hda-codec-id:10de001a");
2665 MODULE_ALIAS("snd-hda-codec-id:10de001b");
2666 MODULE_ALIAS("snd-hda-codec-id:10de001c");
2667 MODULE_ALIAS("snd-hda-codec-id:10de0040");
2668 MODULE_ALIAS("snd-hda-codec-id:10de0041");
2669 MODULE_ALIAS("snd-hda-codec-id:10de0042");
2670 MODULE_ALIAS("snd-hda-codec-id:10de0043");
2671 MODULE_ALIAS("snd-hda-codec-id:10de0044");
2672 MODULE_ALIAS("snd-hda-codec-id:10de0051");
2673 MODULE_ALIAS("snd-hda-codec-id:10de0060");
2674 MODULE_ALIAS("snd-hda-codec-id:10de0067");
2675 MODULE_ALIAS("snd-hda-codec-id:10de8001");
2676 MODULE_ALIAS("snd-hda-codec-id:11069f80");
2677 MODULE_ALIAS("snd-hda-codec-id:11069f81");
2678 MODULE_ALIAS("snd-hda-codec-id:11069f84");
2679 MODULE_ALIAS("snd-hda-codec-id:11069f85");
2680 MODULE_ALIAS("snd-hda-codec-id:17e80047");
2681 MODULE_ALIAS("snd-hda-codec-id:80860054");
2682 MODULE_ALIAS("snd-hda-codec-id:80862801");
2683 MODULE_ALIAS("snd-hda-codec-id:80862802");
2684 MODULE_ALIAS("snd-hda-codec-id:80862803");
2685 MODULE_ALIAS("snd-hda-codec-id:80862804");
2686 MODULE_ALIAS("snd-hda-codec-id:80862805");
2687 MODULE_ALIAS("snd-hda-codec-id:80862806");
2688 MODULE_ALIAS("snd-hda-codec-id:80862807");
2689 MODULE_ALIAS("snd-hda-codec-id:80862880");
2690 MODULE_ALIAS("snd-hda-codec-id:808629fb");
2691
2692 MODULE_LICENSE("GPL");
2693 MODULE_DESCRIPTION("HDMI HD-audio codec");
2694 MODULE_ALIAS("snd-hda-codec-intelhdmi");
2695 MODULE_ALIAS("snd-hda-codec-nvhdmi");
2696 MODULE_ALIAS("snd-hda-codec-atihdmi");
2697
2698 static struct hda_codec_preset_list intel_list = {
2699         .preset = snd_hda_preset_hdmi,
2700         .owner = THIS_MODULE,
2701 };
2702
2703 static int __init patch_hdmi_init(void)
2704 {
2705         return snd_hda_add_codec_preset(&intel_list);
2706 }
2707
2708 static void __exit patch_hdmi_exit(void)
2709 {
2710         snd_hda_delete_codec_preset(&intel_list);
2711 }
2712
2713 module_init(patch_hdmi_init)
2714 module_exit(patch_hdmi_exit)