f2610d67e1872ab7775b0bc296f451f37545df09
[safe/jmp/linux-2.6] / sound / pci / hda / patch_intelhdmi.c
1 /*
2  *
3  *  patch_intelhdmi.c - Patch for Intel HDMI codecs
4  *
5  *  Copyright(c) 2008 Intel Corporation. All rights reserved.
6  *
7  *  Authors:
8  *                      Jiang Zhe <zhe.jiang@intel.com>
9  *                      Wu Fengguang <wfg@linux.intel.com>
10  *
11  *  Maintained by:
12  *                      Wu Fengguang <wfg@linux.intel.com>
13  *
14  *  This program is free software; you can redistribute it and/or modify it
15  *  under the terms of the GNU General Public License as published by the Free
16  *  Software Foundation; either version 2 of the License, or (at your option)
17  *  any later version.
18  *
19  *  This program is distributed in the hope that it will be useful, but
20  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
22  *  for more details.
23  *
24  *  You should have received a copy of the GNU General Public License
25  *  along with this program; if not, write to the Free Software Foundation,
26  *  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 #include <linux/init.h>
30 #include <linux/delay.h>
31 #include <linux/slab.h>
32 #include <sound/core.h>
33 #include "hda_codec.h"
34 #include "hda_local.h"
35
36 #define CVT_NID         0x02    /* audio converter */
37 #define PIN_NID         0x03    /* HDMI output pin */
38
39 #define INTEL_HDMI_EVENT_TAG            0x08
40
41 struct intel_hdmi_spec {
42         struct hda_multi_out multiout;
43         struct hda_pcm pcm_rec;
44         struct hdmi_eld sink_eld;
45 };
46
47 static struct hda_verb pinout_enable_verb[] = {
48         {PIN_NID, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
49         {} /* terminator */
50 };
51
52 static struct hda_verb unsolicited_response_verb[] = {
53         {PIN_NID, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN |
54                                                   INTEL_HDMI_EVENT_TAG},
55         {}
56 };
57
58 static struct hda_verb def_chan_map[] = {
59         {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x00},
60         {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x11},
61         {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x22},
62         {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x33},
63         {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x44},
64         {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x55},
65         {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x66},
66         {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x77},
67         {}
68 };
69
70
71 struct hdmi_audio_infoframe {
72         u8 type; /* 0x84 */
73         u8 ver;  /* 0x01 */
74         u8 len;  /* 0x0a */
75
76         u8 checksum;    /* PB0 */
77         u8 CC02_CT47;   /* CC in bits 0:2, CT in 4:7 */
78         u8 SS01_SF24;
79         u8 CXT04;
80         u8 CA;
81         u8 LFEPBL01_LSV36_DM_INH7;
82         u8 reserved[5]; /* PB6 - PB10 */
83 };
84
85 /*
86  * CEA speaker placement:
87  *
88  *        FLH       FCH        FRH
89  *  FLW    FL  FLC   FC   FRC   FR   FRW
90  *
91  *                                  LFE
92  *                     TC
93  *
94  *          RL  RLC   RC   RRC   RR
95  *
96  * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
97  * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
98  */
99 enum cea_speaker_placement {
100         FL  = (1 <<  0),        /* Front Left           */
101         FC  = (1 <<  1),        /* Front Center         */
102         FR  = (1 <<  2),        /* Front Right          */
103         FLC = (1 <<  3),        /* Front Left Center    */
104         FRC = (1 <<  4),        /* Front Right Center   */
105         RL  = (1 <<  5),        /* Rear Left            */
106         RC  = (1 <<  6),        /* Rear Center          */
107         RR  = (1 <<  7),        /* Rear Right           */
108         RLC = (1 <<  8),        /* Rear Left Center     */
109         RRC = (1 <<  9),        /* Rear Right Center    */
110         LFE = (1 << 10),        /* Low Frequency Effect */
111         FLW = (1 << 11),        /* Front Left Wide      */
112         FRW = (1 << 12),        /* Front Right Wide     */
113         FLH = (1 << 13),        /* Front Left High      */
114         FCH = (1 << 14),        /* Front Center High    */
115         FRH = (1 << 15),        /* Front Right High     */
116         TC  = (1 << 16),        /* Top Center           */
117 };
118
119 /*
120  * ELD SA bits in the CEA Speaker Allocation data block
121  */
122 static int eld_speaker_allocation_bits[] = {
123         [0] = FL | FR,
124         [1] = LFE,
125         [2] = FC,
126         [3] = RL | RR,
127         [4] = RC,
128         [5] = FLC | FRC,
129         [6] = RLC | RRC,
130         /* the following are not defined in ELD yet */
131         [7] = FLW | FRW,
132         [8] = FLH | FRH,
133         [9] = TC,
134         [10] = FCH,
135 };
136
137 struct cea_channel_speaker_allocation {
138         int ca_index;
139         int speakers[8];
140
141         /* derived values, just for convenience */
142         int channels;
143         int spk_mask;
144 };
145
146 /*
147  * This is an ordered list!
148  *
149  * The preceding ones have better chances to be selected by
150  * hdmi_setup_channel_allocation().
151  */
152 static struct cea_channel_speaker_allocation channel_allocations[] = {
153 /*                        channel:   8     7    6    5    4     3    2    1  */
154 { .ca_index = 0x00,  .speakers = {   0,    0,   0,   0,   0,    0,  FR,  FL } },
155                                  /* 2.1 */
156 { .ca_index = 0x01,  .speakers = {   0,    0,   0,   0,   0,  LFE,  FR,  FL } },
157                                  /* Dolby Surround */
158 { .ca_index = 0x02,  .speakers = {   0,    0,   0,   0,  FC,    0,  FR,  FL } },
159 { .ca_index = 0x03,  .speakers = {   0,    0,   0,   0,  FC,  LFE,  FR,  FL } },
160 { .ca_index = 0x04,  .speakers = {   0,    0,   0,  RC,   0,    0,  FR,  FL } },
161 { .ca_index = 0x05,  .speakers = {   0,    0,   0,  RC,   0,  LFE,  FR,  FL } },
162 { .ca_index = 0x06,  .speakers = {   0,    0,   0,  RC,  FC,    0,  FR,  FL } },
163 { .ca_index = 0x07,  .speakers = {   0,    0,   0,  RC,  FC,  LFE,  FR,  FL } },
164 { .ca_index = 0x08,  .speakers = {   0,    0,  RR,  RL,   0,    0,  FR,  FL } },
165 { .ca_index = 0x09,  .speakers = {   0,    0,  RR,  RL,   0,  LFE,  FR,  FL } },
166 { .ca_index = 0x0a,  .speakers = {   0,    0,  RR,  RL,  FC,    0,  FR,  FL } },
167                                  /* 5.1 */
168 { .ca_index = 0x0b,  .speakers = {   0,    0,  RR,  RL,  FC,  LFE,  FR,  FL } },
169 { .ca_index = 0x0c,  .speakers = {   0,   RC,  RR,  RL,   0,    0,  FR,  FL } },
170 { .ca_index = 0x0d,  .speakers = {   0,   RC,  RR,  RL,   0,  LFE,  FR,  FL } },
171 { .ca_index = 0x0e,  .speakers = {   0,   RC,  RR,  RL,  FC,    0,  FR,  FL } },
172                                  /* 6.1 */
173 { .ca_index = 0x0f,  .speakers = {   0,   RC,  RR,  RL,  FC,  LFE,  FR,  FL } },
174 { .ca_index = 0x10,  .speakers = { RRC,  RLC,  RR,  RL,   0,    0,  FR,  FL } },
175 { .ca_index = 0x11,  .speakers = { RRC,  RLC,  RR,  RL,   0,  LFE,  FR,  FL } },
176 { .ca_index = 0x12,  .speakers = { RRC,  RLC,  RR,  RL,  FC,    0,  FR,  FL } },
177                                  /* 7.1 */
178 { .ca_index = 0x13,  .speakers = { RRC,  RLC,  RR,  RL,  FC,  LFE,  FR,  FL } },
179 { .ca_index = 0x14,  .speakers = { FRC,  FLC,   0,   0,   0,    0,  FR,  FL } },
180 { .ca_index = 0x15,  .speakers = { FRC,  FLC,   0,   0,   0,  LFE,  FR,  FL } },
181 { .ca_index = 0x16,  .speakers = { FRC,  FLC,   0,   0,  FC,    0,  FR,  FL } },
182 { .ca_index = 0x17,  .speakers = { FRC,  FLC,   0,   0,  FC,  LFE,  FR,  FL } },
183 { .ca_index = 0x18,  .speakers = { FRC,  FLC,   0,  RC,   0,    0,  FR,  FL } },
184 { .ca_index = 0x19,  .speakers = { FRC,  FLC,   0,  RC,   0,  LFE,  FR,  FL } },
185 { .ca_index = 0x1a,  .speakers = { FRC,  FLC,   0,  RC,  FC,    0,  FR,  FL } },
186 { .ca_index = 0x1b,  .speakers = { FRC,  FLC,   0,  RC,  FC,  LFE,  FR,  FL } },
187 { .ca_index = 0x1c,  .speakers = { FRC,  FLC,  RR,  RL,   0,    0,  FR,  FL } },
188 { .ca_index = 0x1d,  .speakers = { FRC,  FLC,  RR,  RL,   0,  LFE,  FR,  FL } },
189 { .ca_index = 0x1e,  .speakers = { FRC,  FLC,  RR,  RL,  FC,    0,  FR,  FL } },
190 { .ca_index = 0x1f,  .speakers = { FRC,  FLC,  RR,  RL,  FC,  LFE,  FR,  FL } },
191 { .ca_index = 0x20,  .speakers = {   0,  FCH,  RR,  RL,  FC,    0,  FR,  FL } },
192 { .ca_index = 0x21,  .speakers = {   0,  FCH,  RR,  RL,  FC,  LFE,  FR,  FL } },
193 { .ca_index = 0x22,  .speakers = {  TC,    0,  RR,  RL,  FC,    0,  FR,  FL } },
194 { .ca_index = 0x23,  .speakers = {  TC,    0,  RR,  RL,  FC,  LFE,  FR,  FL } },
195 { .ca_index = 0x24,  .speakers = { FRH,  FLH,  RR,  RL,   0,    0,  FR,  FL } },
196 { .ca_index = 0x25,  .speakers = { FRH,  FLH,  RR,  RL,   0,  LFE,  FR,  FL } },
197 { .ca_index = 0x26,  .speakers = { FRW,  FLW,  RR,  RL,   0,    0,  FR,  FL } },
198 { .ca_index = 0x27,  .speakers = { FRW,  FLW,  RR,  RL,   0,  LFE,  FR,  FL } },
199 { .ca_index = 0x28,  .speakers = {  TC,   RC,  RR,  RL,  FC,    0,  FR,  FL } },
200 { .ca_index = 0x29,  .speakers = {  TC,   RC,  RR,  RL,  FC,  LFE,  FR,  FL } },
201 { .ca_index = 0x2a,  .speakers = { FCH,   RC,  RR,  RL,  FC,    0,  FR,  FL } },
202 { .ca_index = 0x2b,  .speakers = { FCH,   RC,  RR,  RL,  FC,  LFE,  FR,  FL } },
203 { .ca_index = 0x2c,  .speakers = {  TC,  FCH,  RR,  RL,  FC,    0,  FR,  FL } },
204 { .ca_index = 0x2d,  .speakers = {  TC,  FCH,  RR,  RL,  FC,  LFE,  FR,  FL } },
205 { .ca_index = 0x2e,  .speakers = { FRH,  FLH,  RR,  RL,  FC,    0,  FR,  FL } },
206 { .ca_index = 0x2f,  .speakers = { FRH,  FLH,  RR,  RL,  FC,  LFE,  FR,  FL } },
207 { .ca_index = 0x30,  .speakers = { FRW,  FLW,  RR,  RL,  FC,    0,  FR,  FL } },
208 { .ca_index = 0x31,  .speakers = { FRW,  FLW,  RR,  RL,  FC,  LFE,  FR,  FL } },
209 };
210
211 /*
212  * HDMI routines
213  */
214
215 #ifdef BE_PARANOID
216 static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t nid,
217                                 int *packet_index, int *byte_index)
218 {
219         int val;
220
221         val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_INDEX, 0);
222
223         *packet_index = val >> 5;
224         *byte_index = val & 0x1f;
225 }
226 #endif
227
228 static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t nid,
229                                 int packet_index, int byte_index)
230 {
231         int val;
232
233         val = (packet_index << 5) | (byte_index & 0x1f);
234
235         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
236 }
237
238 static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t nid,
239                                 unsigned char val)
240 {
241         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
242 }
243
244 static void hdmi_enable_output(struct hda_codec *codec)
245 {
246         /* Unmute */
247         if (get_wcaps(codec, PIN_NID) & AC_WCAP_OUT_AMP)
248                 snd_hda_codec_write(codec, PIN_NID, 0,
249                                 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
250         /* Enable pin out */
251         snd_hda_sequence_write(codec, pinout_enable_verb);
252 }
253
254 /*
255  * Enable Audio InfoFrame Transmission
256  */
257 static void hdmi_start_infoframe_trans(struct hda_codec *codec)
258 {
259         hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
260         snd_hda_codec_write(codec, PIN_NID, 0, AC_VERB_SET_HDMI_DIP_XMIT,
261                                                 AC_DIPXMIT_BEST);
262 }
263
264 /*
265  * Disable Audio InfoFrame Transmission
266  */
267 static void hdmi_stop_infoframe_trans(struct hda_codec *codec)
268 {
269         hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
270         snd_hda_codec_write(codec, PIN_NID, 0, AC_VERB_SET_HDMI_DIP_XMIT,
271                                                 AC_DIPXMIT_DISABLE);
272 }
273
274 static int hdmi_get_channel_count(struct hda_codec *codec)
275 {
276         return 1 + snd_hda_codec_read(codec, CVT_NID, 0,
277                                         AC_VERB_GET_CVT_CHAN_COUNT, 0);
278 }
279
280 static void hdmi_set_channel_count(struct hda_codec *codec, int chs)
281 {
282         snd_hda_codec_write(codec, CVT_NID, 0,
283                                         AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
284
285         if (chs != hdmi_get_channel_count(codec))
286                 snd_printd(KERN_INFO "HDMI channel count: expect %d, get %d\n",
287                                         chs, hdmi_get_channel_count(codec));
288 }
289
290 static void hdmi_debug_channel_mapping(struct hda_codec *codec)
291 {
292 #ifdef CONFIG_SND_DEBUG_VERBOSE
293         int i;
294         int slot;
295
296         for (i = 0; i < 8; i++) {
297                 slot = snd_hda_codec_read(codec, CVT_NID, 0,
298                                                 AC_VERB_GET_HDMI_CHAN_SLOT, i);
299                 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
300                                                 slot >> 4, slot & 0x7);
301         }
302 #endif
303 }
304
305 static void hdmi_parse_eld(struct hda_codec *codec)
306 {
307         struct intel_hdmi_spec *spec = codec->spec;
308         struct hdmi_eld *eld = &spec->sink_eld;
309
310         if (!snd_hdmi_get_eld(eld, codec, PIN_NID))
311                 snd_hdmi_show_eld(eld);
312 }
313
314
315 /*
316  * Audio InfoFrame routines
317  */
318
319 static void hdmi_debug_dip_size(struct hda_codec *codec)
320 {
321 #ifdef CONFIG_SND_DEBUG_VERBOSE
322         int i;
323         int size;
324
325         size = snd_hdmi_get_eld_size(codec, PIN_NID);
326         printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
327
328         for (i = 0; i < 8; i++) {
329                 size = snd_hda_codec_read(codec, PIN_NID, 0,
330                                                 AC_VERB_GET_HDMI_DIP_SIZE, i);
331                 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
332         }
333 #endif
334 }
335
336 static void hdmi_clear_dip_buffers(struct hda_codec *codec)
337 {
338 #ifdef BE_PARANOID
339         int i, j;
340         int size;
341         int pi, bi;
342         for (i = 0; i < 8; i++) {
343                 size = snd_hda_codec_read(codec, PIN_NID, 0,
344                                                 AC_VERB_GET_HDMI_DIP_SIZE, i);
345                 if (size == 0)
346                         continue;
347
348                 hdmi_set_dip_index(codec, PIN_NID, i, 0x0);
349                 for (j = 1; j < 1000; j++) {
350                         hdmi_write_dip_byte(codec, PIN_NID, 0x0);
351                         hdmi_get_dip_index(codec, PIN_NID, &pi, &bi);
352                         if (pi != i)
353                                 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
354                                                 bi, pi, i);
355                         if (bi == 0) /* byte index wrapped around */
356                                 break;
357                 }
358                 snd_printd(KERN_INFO
359                         "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
360                         i, size, j);
361         }
362 #endif
363 }
364
365 static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
366                                         struct hdmi_audio_infoframe *ai)
367 {
368         u8 *params = (u8 *)ai;
369         int i;
370
371         hdmi_debug_dip_size(codec);
372         hdmi_clear_dip_buffers(codec); /* be paranoid */
373
374         hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
375         for (i = 0; i < sizeof(ai); i++)
376                 hdmi_write_dip_byte(codec, PIN_NID, params[i]);
377 }
378
379 /*
380  * Compute derived values in channel_allocations[].
381  */
382 static void init_channel_allocations(void)
383 {
384         int i, j;
385         struct cea_channel_speaker_allocation *p;
386
387         for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
388                 p = channel_allocations + i;
389                 p->channels = 0;
390                 p->spk_mask = 0;
391                 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
392                         if (p->speakers[j]) {
393                                 p->channels++;
394                                 p->spk_mask |= p->speakers[j];
395                         }
396         }
397 }
398
399 /*
400  * The transformation takes two steps:
401  *
402  *      eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
403  *            spk_mask => (channel_allocations[])         => ai->CA
404  *
405  * TODO: it could select the wrong CA from multiple candidates.
406 */
407 static int hdmi_setup_channel_allocation(struct hda_codec *codec,
408                                          struct hdmi_audio_infoframe *ai)
409 {
410         struct intel_hdmi_spec *spec = codec->spec;
411         struct hdmi_eld *eld = &spec->sink_eld;
412         int i;
413         int spk_mask = 0;
414         int channels = 1 + (ai->CC02_CT47 & 0x7);
415         char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
416
417         /*
418          * CA defaults to 0 for basic stereo audio
419          */
420         if (channels <= 2)
421                 return 0;
422
423         /*
424          * HDMI sink's ELD info cannot always be retrieved for now, e.g.
425          * in console or for audio devices. Assume the highest speakers
426          * configuration, to _not_ prohibit multi-channel audio playback.
427          */
428         if (!eld->spk_alloc)
429                 eld->spk_alloc = 0xffff;
430
431         /*
432          * expand ELD's speaker allocation mask
433          *
434          * ELD tells the speaker mask in a compact(paired) form,
435          * expand ELD's notions to match the ones used by Audio InfoFrame.
436          */
437         for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
438                 if (eld->spk_alloc & (1 << i))
439                         spk_mask |= eld_speaker_allocation_bits[i];
440         }
441
442         /* search for the first working match in the CA table */
443         for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
444                 if (channels == channel_allocations[i].channels &&
445                     (spk_mask & channel_allocations[i].spk_mask) ==
446                                 channel_allocations[i].spk_mask) {
447                         ai->CA = channel_allocations[i].ca_index;
448                         break;
449                 }
450         }
451
452         snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
453         snd_printdd(KERN_INFO
454                         "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
455                         ai->CA, channels, buf);
456
457         return ai->CA;
458 }
459
460 static void hdmi_setup_channel_mapping(struct hda_codec *codec,
461                                         struct hdmi_audio_infoframe *ai)
462 {
463         if (!ai->CA)
464                 return;
465
466         /*
467          * TODO: adjust channel mapping if necessary
468          * ALSA sequence is front/surr/clfe/side?
469          */
470
471         snd_hda_sequence_write(codec, def_chan_map);
472         hdmi_debug_channel_mapping(codec);
473 }
474
475
476 static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
477                                         struct snd_pcm_substream *substream)
478 {
479         struct hdmi_audio_infoframe ai = {
480                 .type           = 0x84,
481                 .ver            = 0x01,
482                 .len            = 0x0a,
483                 .CC02_CT47      = substream->runtime->channels - 1,
484         };
485
486         hdmi_setup_channel_allocation(codec, &ai);
487         hdmi_setup_channel_mapping(codec, &ai);
488
489         hdmi_fill_audio_infoframe(codec, &ai);
490         hdmi_start_infoframe_trans(codec);
491 }
492
493
494 /*
495  * Unsolicited events
496  */
497
498 static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
499 {
500         int pind = !!(res & AC_UNSOL_RES_PD);
501         int eldv = !!(res & AC_UNSOL_RES_ELDV);
502
503         printk(KERN_INFO
504                 "HDMI hot plug event: Presence_Detect=%d ELD_Valid=%d\n",
505                 pind, eldv);
506
507         if (pind && eldv) {
508                 hdmi_parse_eld(codec);
509                 /* TODO: do real things about ELD */
510         }
511 }
512
513 static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
514 {
515         int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
516         int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
517         int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
518
519         printk(KERN_INFO
520                 "HDMI content protection event: SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
521                 subtag,
522                 cp_state,
523                 cp_ready);
524
525         /* TODO */
526         if (cp_state)
527                 ;
528         if (cp_ready)
529                 ;
530 }
531
532
533 static void intel_hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
534 {
535         int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
536         int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
537
538         if (tag != INTEL_HDMI_EVENT_TAG) {
539                 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
540                 return;
541         }
542
543         if (subtag == 0)
544                 hdmi_intrinsic_event(codec, res);
545         else
546                 hdmi_non_intrinsic_event(codec, res);
547 }
548
549 /*
550  * Callbacks
551  */
552
553 static int intel_hdmi_playback_pcm_open(struct hda_pcm_stream *hinfo,
554                                         struct hda_codec *codec,
555                                         struct snd_pcm_substream *substream)
556 {
557         struct intel_hdmi_spec *spec = codec->spec;
558
559         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
560 }
561
562 static int intel_hdmi_playback_pcm_close(struct hda_pcm_stream *hinfo,
563                                          struct hda_codec *codec,
564                                          struct snd_pcm_substream *substream)
565 {
566         struct intel_hdmi_spec *spec = codec->spec;
567
568         hdmi_stop_infoframe_trans(codec);
569
570         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
571 }
572
573 static int intel_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
574                                            struct hda_codec *codec,
575                                            unsigned int stream_tag,
576                                            unsigned int format,
577                                            struct snd_pcm_substream *substream)
578 {
579         struct intel_hdmi_spec *spec = codec->spec;
580
581         snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
582                                              format, substream);
583
584         hdmi_set_channel_count(codec, substream->runtime->channels);
585
586         hdmi_setup_audio_infoframe(codec, substream);
587
588         return 0;
589 }
590
591 static struct hda_pcm_stream intel_hdmi_pcm_playback = {
592         .substreams = 1,
593         .channels_min = 2,
594         .channels_max = 8,
595         .nid = CVT_NID, /* NID to query formats and rates and setup streams */
596         .ops = {
597                 .open    = intel_hdmi_playback_pcm_open,
598                 .close   = intel_hdmi_playback_pcm_close,
599                 .prepare = intel_hdmi_playback_pcm_prepare
600         },
601 };
602
603 static int intel_hdmi_build_pcms(struct hda_codec *codec)
604 {
605         struct intel_hdmi_spec *spec = codec->spec;
606         struct hda_pcm *info = &spec->pcm_rec;
607
608         codec->num_pcms = 1;
609         codec->pcm_info = info;
610
611         info->name = "INTEL HDMI";
612         info->pcm_type = HDA_PCM_TYPE_HDMI;
613         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = intel_hdmi_pcm_playback;
614
615         return 0;
616 }
617
618 static int intel_hdmi_build_controls(struct hda_codec *codec)
619 {
620         struct intel_hdmi_spec *spec = codec->spec;
621         int err;
622
623         err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
624         if (err < 0)
625                 return err;
626
627         return 0;
628 }
629
630 static int intel_hdmi_init(struct hda_codec *codec)
631 {
632         hdmi_enable_output(codec);
633
634         snd_hda_sequence_write(codec, unsolicited_response_verb);
635
636         return 0;
637 }
638
639 static void intel_hdmi_free(struct hda_codec *codec)
640 {
641         struct intel_hdmi_spec *spec = codec->spec;
642
643         snd_hda_eld_proc_free(codec, &spec->sink_eld);
644         kfree(spec);
645 }
646
647 static struct hda_codec_ops intel_hdmi_patch_ops = {
648         .init                   = intel_hdmi_init,
649         .free                   = intel_hdmi_free,
650         .build_pcms             = intel_hdmi_build_pcms,
651         .build_controls         = intel_hdmi_build_controls,
652         .unsol_event            = intel_hdmi_unsol_event,
653 };
654
655 static int patch_intel_hdmi(struct hda_codec *codec)
656 {
657         struct intel_hdmi_spec *spec;
658
659         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
660         if (spec == NULL)
661                 return -ENOMEM;
662
663         spec->multiout.num_dacs = 0;      /* no analog */
664         spec->multiout.max_channels = 8;
665         spec->multiout.dig_out_nid = CVT_NID;
666
667         codec->spec = spec;
668         codec->patch_ops = intel_hdmi_patch_ops;
669
670         snd_hda_eld_proc_new(codec, &spec->sink_eld);
671
672         init_channel_allocations();
673
674         return 0;
675 }
676
677 static struct hda_codec_preset snd_hda_preset_intelhdmi[] = {
678         { .id = 0x808629fb, .name = "G45 DEVCL",  .patch = patch_intel_hdmi },
679         { .id = 0x80862801, .name = "G45 DEVBLC", .patch = patch_intel_hdmi },
680         { .id = 0x80862802, .name = "G45 DEVCTG", .patch = patch_intel_hdmi },
681         { .id = 0x80862803, .name = "G45 DEVELK", .patch = patch_intel_hdmi },
682         { .id = 0x10951392, .name = "SiI1392 HDMI",     .patch = patch_intel_hdmi },
683         {} /* terminator */
684 };
685
686 MODULE_ALIAS("snd-hda-codec-id:808629fb");
687 MODULE_ALIAS("snd-hda-codec-id:80862801");
688 MODULE_ALIAS("snd-hda-codec-id:80862802");
689 MODULE_ALIAS("snd-hda-codec-id:80862803");
690 MODULE_ALIAS("snd-hda-codec-id:10951392");
691
692 MODULE_LICENSE("GPL");
693 MODULE_DESCRIPTION("Intel HDMI HD-audio codec");
694
695 static struct hda_codec_preset_list intel_list = {
696         .preset = snd_hda_preset_intelhdmi,
697         .owner = THIS_MODULE,
698 };
699
700 static int __init patch_intelhdmi_init(void)
701 {
702         return snd_hda_add_codec_preset(&intel_list);
703 }
704
705 static void __exit patch_intelhdmi_exit(void)
706 {
707         snd_hda_delete_codec_preset(&intel_list);
708 }
709
710 module_init(patch_intelhdmi_init)
711 module_exit(patch_intelhdmi_exit)