]> bbs.cooldavid.org Git - net-next-2.6.git/blob - sound/pci/hda/patch_hdmi.c
ALSA: hda - Fix pin-detection of Nvidia HDMI
[net-next-2.6.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  *
7  *  Authors:
8  *                      Wu Fengguang <wfg@linux.intel.com>
9  *
10  *  Maintained by:
11  *                      Wu Fengguang <wfg@linux.intel.com>
12  *
13  *  This program is free software; you can redistribute it and/or modify it
14  *  under the terms of the GNU General Public License as published by the Free
15  *  Software Foundation; either version 2 of the License, or (at your option)
16  *  any later version.
17  *
18  *  This program is distributed in the hope that it will be useful, but
19  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20  *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21  *  for more details.
22  *
23  *  You should have received a copy of the GNU General Public License
24  *  along with this program; if not, write to the Free Software Foundation,
25  *  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28
29 struct hdmi_spec {
30         int num_cvts;
31         int num_pins;
32         hda_nid_t cvt[MAX_HDMI_CVTS+1];  /* audio sources */
33         hda_nid_t pin[MAX_HDMI_PINS+1];  /* audio sinks */
34
35         /*
36          * source connection for each pin
37          */
38         hda_nid_t pin_cvt[MAX_HDMI_PINS+1];
39
40         /*
41          * HDMI sink attached to each pin
42          */
43         struct hdmi_eld sink_eld[MAX_HDMI_PINS];
44
45         /*
46          * export one pcm per pipe
47          */
48         struct hda_pcm  pcm_rec[MAX_HDMI_CVTS];
49
50         /*
51          * nvhdmi specific
52          */
53         struct hda_multi_out multiout;
54         unsigned int codec_type;
55
56         /* misc flags */
57         /* PD bit indicates only the update, not the current state */
58         unsigned int old_pin_detect:1;
59 };
60
61
62 struct hdmi_audio_infoframe {
63         u8 type; /* 0x84 */
64         u8 ver;  /* 0x01 */
65         u8 len;  /* 0x0a */
66
67         u8 checksum;    /* PB0 */
68         u8 CC02_CT47;   /* CC in bits 0:2, CT in 4:7 */
69         u8 SS01_SF24;
70         u8 CXT04;
71         u8 CA;
72         u8 LFEPBL01_LSV36_DM_INH7;
73         u8 reserved[5]; /* PB6 - PB10 */
74 };
75
76 /*
77  * CEA speaker placement:
78  *
79  *        FLH       FCH        FRH
80  *  FLW    FL  FLC   FC   FRC   FR   FRW
81  *
82  *                                  LFE
83  *                     TC
84  *
85  *          RL  RLC   RC   RRC   RR
86  *
87  * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
88  * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
89  */
90 enum cea_speaker_placement {
91         FL  = (1 <<  0),        /* Front Left           */
92         FC  = (1 <<  1),        /* Front Center         */
93         FR  = (1 <<  2),        /* Front Right          */
94         FLC = (1 <<  3),        /* Front Left Center    */
95         FRC = (1 <<  4),        /* Front Right Center   */
96         RL  = (1 <<  5),        /* Rear Left            */
97         RC  = (1 <<  6),        /* Rear Center          */
98         RR  = (1 <<  7),        /* Rear Right           */
99         RLC = (1 <<  8),        /* Rear Left Center     */
100         RRC = (1 <<  9),        /* Rear Right Center    */
101         LFE = (1 << 10),        /* Low Frequency Effect */
102         FLW = (1 << 11),        /* Front Left Wide      */
103         FRW = (1 << 12),        /* Front Right Wide     */
104         FLH = (1 << 13),        /* Front Left High      */
105         FCH = (1 << 14),        /* Front Center High    */
106         FRH = (1 << 15),        /* Front Right High     */
107         TC  = (1 << 16),        /* Top Center           */
108 };
109
110 /*
111  * ELD SA bits in the CEA Speaker Allocation data block
112  */
113 static int eld_speaker_allocation_bits[] = {
114         [0] = FL | FR,
115         [1] = LFE,
116         [2] = FC,
117         [3] = RL | RR,
118         [4] = RC,
119         [5] = FLC | FRC,
120         [6] = RLC | RRC,
121         /* the following are not defined in ELD yet */
122         [7] = FLW | FRW,
123         [8] = FLH | FRH,
124         [9] = TC,
125         [10] = FCH,
126 };
127
128 struct cea_channel_speaker_allocation {
129         int ca_index;
130         int speakers[8];
131
132         /* derived values, just for convenience */
133         int channels;
134         int spk_mask;
135 };
136
137 /*
138  * ALSA sequence is:
139  *
140  *       surround40   surround41   surround50   surround51   surround71
141  * ch0   front left   =            =            =            =
142  * ch1   front right  =            =            =            =
143  * ch2   rear left    =            =            =            =
144  * ch3   rear right   =            =            =            =
145  * ch4                LFE          center       center       center
146  * ch5                                          LFE          LFE
147  * ch6                                                       side left
148  * ch7                                                       side right
149  *
150  * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
151  */
152 static int hdmi_channel_mapping[0x32][8] = {
153         /* stereo */
154         [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
155         /* 2.1 */
156         [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
157         /* Dolby Surround */
158         [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
159         /* surround40 */
160         [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
161         /* 4ch */
162         [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
163         /* surround41 */
164         [0x09] = { 0x00, 0x11, 0x24, 0x34, 0x43, 0xf2, 0xf6, 0xf7 },
165         /* surround50 */
166         [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
167         /* surround51 */
168         [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
169         /* 7.1 */
170         [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
171 };
172
173 /*
174  * This is an ordered list!
175  *
176  * The preceding ones have better chances to be selected by
177  * hdmi_setup_channel_allocation().
178  */
179 static struct cea_channel_speaker_allocation channel_allocations[] = {
180 /*                        channel:   7     6    5    4    3     2    1    0  */
181 { .ca_index = 0x00,  .speakers = {   0,    0,   0,   0,   0,    0,  FR,  FL } },
182                                  /* 2.1 */
183 { .ca_index = 0x01,  .speakers = {   0,    0,   0,   0,   0,  LFE,  FR,  FL } },
184                                  /* Dolby Surround */
185 { .ca_index = 0x02,  .speakers = {   0,    0,   0,   0,  FC,    0,  FR,  FL } },
186                                  /* surround40 */
187 { .ca_index = 0x08,  .speakers = {   0,    0,  RR,  RL,   0,    0,  FR,  FL } },
188                                  /* surround41 */
189 { .ca_index = 0x09,  .speakers = {   0,    0,  RR,  RL,   0,  LFE,  FR,  FL } },
190                                  /* surround50 */
191 { .ca_index = 0x0a,  .speakers = {   0,    0,  RR,  RL,  FC,    0,  FR,  FL } },
192                                  /* surround51 */
193 { .ca_index = 0x0b,  .speakers = {   0,    0,  RR,  RL,  FC,  LFE,  FR,  FL } },
194                                  /* 6.1 */
195 { .ca_index = 0x0f,  .speakers = {   0,   RC,  RR,  RL,  FC,  LFE,  FR,  FL } },
196                                  /* surround71 */
197 { .ca_index = 0x13,  .speakers = { RRC,  RLC,  RR,  RL,  FC,  LFE,  FR,  FL } },
198
199 { .ca_index = 0x03,  .speakers = {   0,    0,   0,   0,  FC,  LFE,  FR,  FL } },
200 { .ca_index = 0x04,  .speakers = {   0,    0,   0,  RC,   0,    0,  FR,  FL } },
201 { .ca_index = 0x05,  .speakers = {   0,    0,   0,  RC,   0,  LFE,  FR,  FL } },
202 { .ca_index = 0x06,  .speakers = {   0,    0,   0,  RC,  FC,    0,  FR,  FL } },
203 { .ca_index = 0x07,  .speakers = {   0,    0,   0,  RC,  FC,  LFE,  FR,  FL } },
204 { .ca_index = 0x0c,  .speakers = {   0,   RC,  RR,  RL,   0,    0,  FR,  FL } },
205 { .ca_index = 0x0d,  .speakers = {   0,   RC,  RR,  RL,   0,  LFE,  FR,  FL } },
206 { .ca_index = 0x0e,  .speakers = {   0,   RC,  RR,  RL,  FC,    0,  FR,  FL } },
207 { .ca_index = 0x10,  .speakers = { RRC,  RLC,  RR,  RL,   0,    0,  FR,  FL } },
208 { .ca_index = 0x11,  .speakers = { RRC,  RLC,  RR,  RL,   0,  LFE,  FR,  FL } },
209 { .ca_index = 0x12,  .speakers = { RRC,  RLC,  RR,  RL,  FC,    0,  FR,  FL } },
210 { .ca_index = 0x14,  .speakers = { FRC,  FLC,   0,   0,   0,    0,  FR,  FL } },
211 { .ca_index = 0x15,  .speakers = { FRC,  FLC,   0,   0,   0,  LFE,  FR,  FL } },
212 { .ca_index = 0x16,  .speakers = { FRC,  FLC,   0,   0,  FC,    0,  FR,  FL } },
213 { .ca_index = 0x17,  .speakers = { FRC,  FLC,   0,   0,  FC,  LFE,  FR,  FL } },
214 { .ca_index = 0x18,  .speakers = { FRC,  FLC,   0,  RC,   0,    0,  FR,  FL } },
215 { .ca_index = 0x19,  .speakers = { FRC,  FLC,   0,  RC,   0,  LFE,  FR,  FL } },
216 { .ca_index = 0x1a,  .speakers = { FRC,  FLC,   0,  RC,  FC,    0,  FR,  FL } },
217 { .ca_index = 0x1b,  .speakers = { FRC,  FLC,   0,  RC,  FC,  LFE,  FR,  FL } },
218 { .ca_index = 0x1c,  .speakers = { FRC,  FLC,  RR,  RL,   0,    0,  FR,  FL } },
219 { .ca_index = 0x1d,  .speakers = { FRC,  FLC,  RR,  RL,   0,  LFE,  FR,  FL } },
220 { .ca_index = 0x1e,  .speakers = { FRC,  FLC,  RR,  RL,  FC,    0,  FR,  FL } },
221 { .ca_index = 0x1f,  .speakers = { FRC,  FLC,  RR,  RL,  FC,  LFE,  FR,  FL } },
222 { .ca_index = 0x20,  .speakers = {   0,  FCH,  RR,  RL,  FC,    0,  FR,  FL } },
223 { .ca_index = 0x21,  .speakers = {   0,  FCH,  RR,  RL,  FC,  LFE,  FR,  FL } },
224 { .ca_index = 0x22,  .speakers = {  TC,    0,  RR,  RL,  FC,    0,  FR,  FL } },
225 { .ca_index = 0x23,  .speakers = {  TC,    0,  RR,  RL,  FC,  LFE,  FR,  FL } },
226 { .ca_index = 0x24,  .speakers = { FRH,  FLH,  RR,  RL,   0,    0,  FR,  FL } },
227 { .ca_index = 0x25,  .speakers = { FRH,  FLH,  RR,  RL,   0,  LFE,  FR,  FL } },
228 { .ca_index = 0x26,  .speakers = { FRW,  FLW,  RR,  RL,   0,    0,  FR,  FL } },
229 { .ca_index = 0x27,  .speakers = { FRW,  FLW,  RR,  RL,   0,  LFE,  FR,  FL } },
230 { .ca_index = 0x28,  .speakers = {  TC,   RC,  RR,  RL,  FC,    0,  FR,  FL } },
231 { .ca_index = 0x29,  .speakers = {  TC,   RC,  RR,  RL,  FC,  LFE,  FR,  FL } },
232 { .ca_index = 0x2a,  .speakers = { FCH,   RC,  RR,  RL,  FC,    0,  FR,  FL } },
233 { .ca_index = 0x2b,  .speakers = { FCH,   RC,  RR,  RL,  FC,  LFE,  FR,  FL } },
234 { .ca_index = 0x2c,  .speakers = {  TC,  FCH,  RR,  RL,  FC,    0,  FR,  FL } },
235 { .ca_index = 0x2d,  .speakers = {  TC,  FCH,  RR,  RL,  FC,  LFE,  FR,  FL } },
236 { .ca_index = 0x2e,  .speakers = { FRH,  FLH,  RR,  RL,  FC,    0,  FR,  FL } },
237 { .ca_index = 0x2f,  .speakers = { FRH,  FLH,  RR,  RL,  FC,  LFE,  FR,  FL } },
238 { .ca_index = 0x30,  .speakers = { FRW,  FLW,  RR,  RL,  FC,    0,  FR,  FL } },
239 { .ca_index = 0x31,  .speakers = { FRW,  FLW,  RR,  RL,  FC,  LFE,  FR,  FL } },
240 };
241
242
243 /*
244  * HDMI routines
245  */
246
247 static int hda_node_index(hda_nid_t *nids, hda_nid_t nid)
248 {
249         int i;
250
251         for (i = 0; nids[i]; i++)
252                 if (nids[i] == nid)
253                         return i;
254
255         snd_printk(KERN_WARNING "HDMI: nid %d not registered\n", nid);
256         return -EINVAL;
257 }
258
259 static void hdmi_get_show_eld(struct hda_codec *codec, hda_nid_t pin_nid,
260                               struct hdmi_eld *eld)
261 {
262         if (!snd_hdmi_get_eld(eld, codec, pin_nid))
263                 snd_hdmi_show_eld(eld);
264 }
265
266 #ifdef BE_PARANOID
267 static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
268                                 int *packet_index, int *byte_index)
269 {
270         int val;
271
272         val = snd_hda_codec_read(codec, pin_nid, 0,
273                                  AC_VERB_GET_HDMI_DIP_INDEX, 0);
274
275         *packet_index = val >> 5;
276         *byte_index = val & 0x1f;
277 }
278 #endif
279
280 static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
281                                 int packet_index, int byte_index)
282 {
283         int val;
284
285         val = (packet_index << 5) | (byte_index & 0x1f);
286
287         snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
288 }
289
290 static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
291                                 unsigned char val)
292 {
293         snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
294 }
295
296 static void hdmi_enable_output(struct hda_codec *codec, hda_nid_t pin_nid)
297 {
298         /* Unmute */
299         if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
300                 snd_hda_codec_write(codec, pin_nid, 0,
301                                 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
302         /* Enable pin out */
303         snd_hda_codec_write(codec, pin_nid, 0,
304                             AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
305 }
306
307 static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t nid)
308 {
309         return 1 + snd_hda_codec_read(codec, nid, 0,
310                                         AC_VERB_GET_CVT_CHAN_COUNT, 0);
311 }
312
313 static void hdmi_set_channel_count(struct hda_codec *codec,
314                                    hda_nid_t nid, int chs)
315 {
316         if (chs != hdmi_get_channel_count(codec, nid))
317                 snd_hda_codec_write(codec, nid, 0,
318                                     AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
319 }
320
321
322 /*
323  * Channel mapping routines
324  */
325
326 /*
327  * Compute derived values in channel_allocations[].
328  */
329 static void init_channel_allocations(void)
330 {
331         int i, j;
332         struct cea_channel_speaker_allocation *p;
333
334         for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
335                 p = channel_allocations + i;
336                 p->channels = 0;
337                 p->spk_mask = 0;
338                 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
339                         if (p->speakers[j]) {
340                                 p->channels++;
341                                 p->spk_mask |= p->speakers[j];
342                         }
343         }
344 }
345
346 /*
347  * The transformation takes two steps:
348  *
349  *      eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
350  *            spk_mask => (channel_allocations[])         => ai->CA
351  *
352  * TODO: it could select the wrong CA from multiple candidates.
353 */
354 static int hdmi_setup_channel_allocation(struct hda_codec *codec, hda_nid_t nid,
355                                          struct hdmi_audio_infoframe *ai)
356 {
357         struct hdmi_spec *spec = codec->spec;
358         struct hdmi_eld *eld;
359         int i;
360         int spk_mask = 0;
361         int channels = 1 + (ai->CC02_CT47 & 0x7);
362         char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
363
364         /*
365          * CA defaults to 0 for basic stereo audio
366          */
367         if (channels <= 2)
368                 return 0;
369
370         i = hda_node_index(spec->pin_cvt, nid);
371         if (i < 0)
372                 return 0;
373         eld = &spec->sink_eld[i];
374
375         /*
376          * HDMI sink's ELD info cannot always be retrieved for now, e.g.
377          * in console or for audio devices. Assume the highest speakers
378          * configuration, to _not_ prohibit multi-channel audio playback.
379          */
380         if (!eld->spk_alloc)
381                 eld->spk_alloc = 0xffff;
382
383         /*
384          * expand ELD's speaker allocation mask
385          *
386          * ELD tells the speaker mask in a compact(paired) form,
387          * expand ELD's notions to match the ones used by Audio InfoFrame.
388          */
389         for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
390                 if (eld->spk_alloc & (1 << i))
391                         spk_mask |= eld_speaker_allocation_bits[i];
392         }
393
394         /* search for the first working match in the CA table */
395         for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
396                 if (channels == channel_allocations[i].channels &&
397                     (spk_mask & channel_allocations[i].spk_mask) ==
398                                 channel_allocations[i].spk_mask) {
399                         ai->CA = channel_allocations[i].ca_index;
400                         break;
401                 }
402         }
403
404         snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
405         snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
406                     ai->CA, channels, buf);
407
408         return ai->CA;
409 }
410
411 static void hdmi_debug_channel_mapping(struct hda_codec *codec,
412                                        hda_nid_t pin_nid)
413 {
414 #ifdef CONFIG_SND_DEBUG_VERBOSE
415         int i;
416         int slot;
417
418         for (i = 0; i < 8; i++) {
419                 slot = snd_hda_codec_read(codec, pin_nid, 0,
420                                                 AC_VERB_GET_HDMI_CHAN_SLOT, i);
421                 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
422                                                 slot >> 4, slot & 0xf);
423         }
424 #endif
425 }
426
427
428 static void hdmi_setup_channel_mapping(struct hda_codec *codec,
429                                        hda_nid_t pin_nid,
430                                        struct hdmi_audio_infoframe *ai)
431 {
432         int i;
433         int ca = ai->CA;
434         int err;
435
436         if (hdmi_channel_mapping[ca][1] == 0) {
437                 for (i = 0; i < channel_allocations[ca].channels; i++)
438                         hdmi_channel_mapping[ca][i] = i | (i << 4);
439                 for (; i < 8; i++)
440                         hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
441         }
442
443         for (i = 0; i < 8; i++) {
444                 err = snd_hda_codec_write(codec, pin_nid, 0,
445                                           AC_VERB_SET_HDMI_CHAN_SLOT,
446                                           hdmi_channel_mapping[ca][i]);
447                 if (err) {
448                         snd_printdd(KERN_NOTICE
449                                     "HDMI: channel mapping failed\n");
450                         break;
451                 }
452         }
453
454         hdmi_debug_channel_mapping(codec, pin_nid);
455 }
456
457
458 /*
459  * Audio InfoFrame routines
460  */
461
462 /*
463  * Enable Audio InfoFrame Transmission
464  */
465 static void hdmi_start_infoframe_trans(struct hda_codec *codec,
466                                        hda_nid_t pin_nid)
467 {
468         hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
469         snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
470                                                 AC_DIPXMIT_BEST);
471 }
472
473 /*
474  * Disable Audio InfoFrame Transmission
475  */
476 static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
477                                       hda_nid_t pin_nid)
478 {
479         hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
480         snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
481                                                 AC_DIPXMIT_DISABLE);
482 }
483
484 static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
485 {
486 #ifdef CONFIG_SND_DEBUG_VERBOSE
487         int i;
488         int size;
489
490         size = snd_hdmi_get_eld_size(codec, pin_nid);
491         printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
492
493         for (i = 0; i < 8; i++) {
494                 size = snd_hda_codec_read(codec, pin_nid, 0,
495                                                 AC_VERB_GET_HDMI_DIP_SIZE, i);
496                 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
497         }
498 #endif
499 }
500
501 static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
502 {
503 #ifdef BE_PARANOID
504         int i, j;
505         int size;
506         int pi, bi;
507         for (i = 0; i < 8; i++) {
508                 size = snd_hda_codec_read(codec, pin_nid, 0,
509                                                 AC_VERB_GET_HDMI_DIP_SIZE, i);
510                 if (size == 0)
511                         continue;
512
513                 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
514                 for (j = 1; j < 1000; j++) {
515                         hdmi_write_dip_byte(codec, pin_nid, 0x0);
516                         hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
517                         if (pi != i)
518                                 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
519                                                 bi, pi, i);
520                         if (bi == 0) /* byte index wrapped around */
521                                 break;
522                 }
523                 snd_printd(KERN_INFO
524                         "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
525                         i, size, j);
526         }
527 #endif
528 }
529
530 static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *ai)
531 {
532         u8 *bytes = (u8 *)ai;
533         u8 sum = 0;
534         int i;
535
536         ai->checksum = 0;
537
538         for (i = 0; i < sizeof(*ai); i++)
539                 sum += bytes[i];
540
541         ai->checksum = -sum;
542 }
543
544 static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
545                                       hda_nid_t pin_nid,
546                                       struct hdmi_audio_infoframe *ai)
547 {
548         u8 *bytes = (u8 *)ai;
549         int i;
550
551         hdmi_debug_dip_size(codec, pin_nid);
552         hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
553
554         hdmi_checksum_audio_infoframe(ai);
555
556         hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
557         for (i = 0; i < sizeof(*ai); i++)
558                 hdmi_write_dip_byte(codec, pin_nid, bytes[i]);
559 }
560
561 static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
562                                     struct hdmi_audio_infoframe *ai)
563 {
564         u8 *bytes = (u8 *)ai;
565         u8 val;
566         int i;
567
568         if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
569                                                             != AC_DIPXMIT_BEST)
570                 return false;
571
572         hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
573         for (i = 0; i < sizeof(*ai); i++) {
574                 val = snd_hda_codec_read(codec, pin_nid, 0,
575                                          AC_VERB_GET_HDMI_DIP_DATA, 0);
576                 if (val != bytes[i])
577                         return false;
578         }
579
580         return true;
581 }
582
583 static void hdmi_setup_audio_infoframe(struct hda_codec *codec, hda_nid_t nid,
584                                         struct snd_pcm_substream *substream)
585 {
586         struct hdmi_spec *spec = codec->spec;
587         hda_nid_t pin_nid;
588         int i;
589         struct hdmi_audio_infoframe ai = {
590                 .type           = 0x84,
591                 .ver            = 0x01,
592                 .len            = 0x0a,
593                 .CC02_CT47      = substream->runtime->channels - 1,
594         };
595
596         hdmi_setup_channel_allocation(codec, nid, &ai);
597
598         for (i = 0; i < spec->num_pins; i++) {
599                 if (spec->pin_cvt[i] != nid)
600                         continue;
601                 if (!spec->sink_eld[i].monitor_present)
602                         continue;
603
604                 pin_nid = spec->pin[i];
605                 if (!hdmi_infoframe_uptodate(codec, pin_nid, &ai)) {
606                         snd_printdd("hdmi_setup_audio_infoframe: "
607                                     "cvt=%d pin=%d channels=%d\n",
608                                     nid, pin_nid,
609                                     substream->runtime->channels);
610                         hdmi_setup_channel_mapping(codec, pin_nid, &ai);
611                         hdmi_stop_infoframe_trans(codec, pin_nid);
612                         hdmi_fill_audio_infoframe(codec, pin_nid, &ai);
613                         hdmi_start_infoframe_trans(codec, pin_nid);
614                 }
615         }
616 }
617
618
619 /*
620  * Unsolicited events
621  */
622
623 static void hdmi_present_sense(struct hda_codec *codec, hda_nid_t pin_nid,
624                                struct hdmi_eld *eld);
625
626 static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
627 {
628         struct hdmi_spec *spec = codec->spec;
629         int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
630         int pind = !!(res & AC_UNSOL_RES_PD);
631         int eldv = !!(res & AC_UNSOL_RES_ELDV);
632         int index;
633
634         printk(KERN_INFO
635                 "HDMI hot plug event: Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
636                 tag, pind, eldv);
637
638         index = hda_node_index(spec->pin, tag);
639         if (index < 0)
640                 return;
641
642         if (spec->old_pin_detect) {
643                 if (pind)
644                         hdmi_present_sense(codec, tag, &spec->sink_eld[index]);
645                 pind = spec->sink_eld[index].monitor_present;
646         }
647
648         spec->sink_eld[index].monitor_present = pind;
649         spec->sink_eld[index].eld_valid = eldv;
650
651         if (pind && eldv) {
652                 hdmi_get_show_eld(codec, spec->pin[index],
653                                   &spec->sink_eld[index]);
654                 /* TODO: do real things about ELD */
655         }
656 }
657
658 static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
659 {
660         int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
661         int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
662         int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
663         int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
664
665         printk(KERN_INFO
666                 "HDMI CP event: PIN=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
667                 tag,
668                 subtag,
669                 cp_state,
670                 cp_ready);
671
672         /* TODO */
673         if (cp_state)
674                 ;
675         if (cp_ready)
676                 ;
677 }
678
679
680 static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
681 {
682         struct hdmi_spec *spec = codec->spec;
683         int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
684         int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
685
686         if (hda_node_index(spec->pin, tag) < 0) {
687                 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
688                 return;
689         }
690
691         if (subtag == 0)
692                 hdmi_intrinsic_event(codec, res);
693         else
694                 hdmi_non_intrinsic_event(codec, res);
695 }
696
697 /*
698  * Callbacks
699  */
700
701 static void hdmi_setup_stream(struct hda_codec *codec, hda_nid_t nid,
702                               u32 stream_tag, int format)
703 {
704         int tag;
705         int fmt;
706
707         tag = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0) >> 4;
708         fmt = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_STREAM_FORMAT, 0);
709
710         snd_printdd("hdmi_setup_stream: "
711                     "NID=0x%x, %sstream=0x%x, %sformat=0x%x\n",
712                     nid,
713                     tag == stream_tag ? "" : "new-",
714                     stream_tag,
715                     fmt == format ? "" : "new-",
716                     format);
717
718         if (tag != stream_tag)
719                 snd_hda_codec_write(codec, nid, 0,
720                                     AC_VERB_SET_CHANNEL_STREAMID,
721                                     stream_tag << 4);
722         if (fmt != format)
723                 snd_hda_codec_write(codec, nid, 0,
724                                     AC_VERB_SET_STREAM_FORMAT, format);
725 }
726
727 /*
728  * HDA/HDMI auto parsing
729  */
730
731 static int hdmi_read_pin_conn(struct hda_codec *codec, hda_nid_t pin_nid)
732 {
733         struct hdmi_spec *spec = codec->spec;
734         hda_nid_t conn_list[HDA_MAX_CONNECTIONS];
735         int conn_len, curr;
736         int index;
737
738         if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
739                 snd_printk(KERN_WARNING
740                            "HDMI: pin %d wcaps %#x "
741                            "does not support connection list\n",
742                            pin_nid, get_wcaps(codec, pin_nid));
743                 return -EINVAL;
744         }
745
746         conn_len = snd_hda_get_connections(codec, pin_nid, conn_list,
747                                            HDA_MAX_CONNECTIONS);
748         if (conn_len > 1)
749                 curr = snd_hda_codec_read(codec, pin_nid, 0,
750                                           AC_VERB_GET_CONNECT_SEL, 0);
751         else
752                 curr = 0;
753
754         index = hda_node_index(spec->pin, pin_nid);
755         if (index < 0)
756                 return -EINVAL;
757
758         spec->pin_cvt[index] = conn_list[curr];
759
760         return 0;
761 }
762
763 static void hdmi_present_sense(struct hda_codec *codec, hda_nid_t pin_nid,
764                                struct hdmi_eld *eld)
765 {
766         int present = snd_hda_pin_sense(codec, pin_nid);
767
768         eld->monitor_present    = !!(present & AC_PINSENSE_PRESENCE);
769         eld->eld_valid          = !!(present & AC_PINSENSE_ELDV);
770
771         if (present & AC_PINSENSE_ELDV)
772                 hdmi_get_show_eld(codec, pin_nid, eld);
773 }
774
775 static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
776 {
777         struct hdmi_spec *spec = codec->spec;
778
779         if (spec->num_pins >= MAX_HDMI_PINS) {
780                 snd_printk(KERN_WARNING
781                            "HDMI: no space for pin %d\n", pin_nid);
782                 return -E2BIG;
783         }
784
785         hdmi_present_sense(codec, pin_nid, &spec->sink_eld[spec->num_pins]);
786
787         spec->pin[spec->num_pins] = pin_nid;
788         spec->num_pins++;
789
790         /*
791          * It is assumed that converter nodes come first in the node list and
792          * hence have been registered and usable now.
793          */
794         return hdmi_read_pin_conn(codec, pin_nid);
795 }
796
797 static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t nid)
798 {
799         struct hdmi_spec *spec = codec->spec;
800
801         if (spec->num_cvts >= MAX_HDMI_CVTS) {
802                 snd_printk(KERN_WARNING
803                            "HDMI: no space for converter %d\n", nid);
804                 return -E2BIG;
805         }
806
807         spec->cvt[spec->num_cvts] = nid;
808         spec->num_cvts++;
809
810         return 0;
811 }
812
813 static int hdmi_parse_codec(struct hda_codec *codec)
814 {
815         hda_nid_t nid;
816         int i, nodes;
817
818         nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
819         if (!nid || nodes < 0) {
820                 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
821                 return -EINVAL;
822         }
823
824         for (i = 0; i < nodes; i++, nid++) {
825                 unsigned int caps;
826                 unsigned int type;
827
828                 caps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
829                 type = get_wcaps_type(caps);
830
831                 if (!(caps & AC_WCAP_DIGITAL))
832                         continue;
833
834                 switch (type) {
835                 case AC_WID_AUD_OUT:
836                         hdmi_add_cvt(codec, nid);
837                         break;
838                 case AC_WID_PIN:
839                         caps = snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
840                         if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
841                                 continue;
842                         hdmi_add_pin(codec, nid);
843                         break;
844                 }
845         }
846
847         /*
848          * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
849          * can be lost and presence sense verb will become inaccurate if the
850          * HDA link is powered off at hot plug or hw initialization time.
851          */
852 #ifdef CONFIG_SND_HDA_POWER_SAVE
853         if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
854               AC_PWRST_EPSS))
855                 codec->bus->power_keep_link_on = 1;
856 #endif
857
858         return 0;
859 }
860